Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 21 additions & 47 deletions packages/river_hdl/lib/src/core/decoder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -407,10 +407,18 @@ class DynamicInstructionDecoder extends InstructionDecoder {
return pm & nzf & zf;
}

final laneMatches = [for (var l = 0; l < lanes; l++) laneMatch(laneRow(l))];
var selectedMatch =
laneMatches.last & rowField(laneRow(lanes - 1), 'mask').neq(0);
var selData = laneRow(lanes - 1);
for (var l = lanes - 2; l >= 0; l--) {
selectedMatch = mux(
laneMatches[l],
rowField(laneRow(l), 'mask').neq(0),
selectedMatch,
);
selData = mux(
laneMatch(laneRow(l)),
laneMatches[l],
laneRow(l),
selData,
).named('decodeSelData_$l');
Expand Down Expand Up @@ -451,20 +459,9 @@ class DynamicInstructionDecoder extends InstructionDecoder {
.toList();
final opIdxWidth = pattern['opIndex']!.width;

final nzfMatch = mux(
pattern['nzfMask']!.neq(0),
(instr & pattern['nzfMask']!).neq(0),
Const(1),
).named('nzfMatch');
final zfMatch = mux(
pattern['zfMask']!.neq(0),
(instr & pattern['zfMask']!).eq(0),
Const(1),
).named('zfMatch');

final patternMatch = (instr & pattern['mask']!)
.eq(pattern['value']!)
.named('patternMatch');
// Carry the winning lane's match alongside its row instead of matching
// the priority-muxed row again. Preserve zero-mask tail rejection and the
// same lowest-lane priority for both the metadata and match decision.

// STAGE 2 field extraction, sourced from the REGISTERED match result. This
// is the identical field logic as before, just driven by _matchInstr /
Expand Down Expand Up @@ -512,14 +509,9 @@ class DynamicInstructionDecoder extends InstructionDecoder {
]),
];

// "No valid decode this cycle" output clear (fresh Conditionals each call).
List<Conditional> clearOutputs() => [
done < 0,
valid < 0,
index < 0,
...instrTypeMap.entries.map((entry) => entry.value < 0),
...fields.entries.map((entry) => entry.value < 0),
];
// Only completion status depends on the ROM match. Clear search-stage
// data independently so the match cone does not drive every field bit.
List<Conditional> clearOutputs() => [done < 0, valid < 0];

// First cycle of a NEW instruction: the fetch holds `pc_in` stable across the
// multi-cycle scan, so `pc_in != pc_out` (pc_out is last cycle's pc_in) is
Expand Down Expand Up @@ -547,6 +539,9 @@ class DynamicInstructionDecoder extends InstructionDecoder {
then: [microcodeRead.en < 0, ...computeFields()],
orElse: [
// STAGE 1: search the microcode ROM, one packed row per cycle.
index < 0,
...instrTypeMap.entries.map((entry) => entry.value < 0),
...fields.entries.map((entry) => entry.value < 0),
microcodeRead.en < 1,
// _counter is sized for the unpacked pattern count; the packed ROM
// has ceil(patterns/lanes) words, so its address port is narrower.
Expand Down Expand Up @@ -576,10 +571,7 @@ class DynamicInstructionDecoder extends InstructionDecoder {
microcodeRead.valid,
then: [
If(
patternMatch &
nzfMatch &
zfMatch &
pattern['mask']!.neq(0),
selectedMatch,
// Match: latch ONLY the result; the field Cases run next
// cycle in stage 2. done/valid stay low until then. Exec
// already tolerates variable decode latency (the ROM
Expand Down Expand Up @@ -630,17 +622,7 @@ class DynamicInstructionDecoder extends InstructionDecoder {
then: [
If(
_rescanned,
then: [
done < 1,
valid < 0,
index < 0,
...instrTypeMap.entries.map(
(entry) => entry.value < 0,
),
...fields.entries.map(
(entry) => entry.value < 0,
),
],
then: [done < 1, valid < 0],
orElse: [
_counter < 0,
_rescanned < 1,
Expand All @@ -656,15 +638,7 @@ class DynamicInstructionDecoder extends InstructionDecoder {
],
),
],
orElse: [
done < 1,
valid < 0,
index < 0,
...instrTypeMap.entries.map(
(entry) => entry.value < 0,
),
...fields.entries.map((entry) => entry.value < 0),
],
orElse: [done < 1, valid < 0],
),
],
orElse: clearOutputs(),
Expand Down
105 changes: 51 additions & 54 deletions packages/river_hdl/lib/src/core/exec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2031,67 +2031,64 @@ class DynamicExecutionUnit extends ExecutionUnit {
.slice(MicroOp.functRange.end, MicroOp.functRange.start)
.named('mopFunct');

Logic readSource(Logic source) => mux(
source.eq(Const(MicroOpSource.imm, width: MicroOpSource.width)),
imm,
mux(
source.eq(Const(MicroOpSource.alu, width: MicroOpSource.width)),
alu,
mux(
source.eq(Const(MicroOpSource.rs1, width: MicroOpSource.width)),
rs1,
mux(
source.eq(Const(MicroOpSource.rs2, width: MicroOpSource.width)),
rs2,
mux(
source.eq(Const(MicroOpSource.rd, width: MicroOpSource.width)),
rd,
nextPc,
),
),
Logic readSource(Logic source) {
final value = Logic(name: 'sourceValue', width: mxlen.size);
Combinational([
Case(
source,
[
for (final entry in {
MicroOpSource.imm: imm,
MicroOpSource.alu: alu,
MicroOpSource.rs1: rs1,
MicroOpSource.rs2: rs2,
MicroOpSource.rd: rd,
}.entries)
CaseItem(Const(entry.key, width: MicroOpSource.width), [
value < entry.value,
]),
],
defaultItem: [value < nextPc],
),
),
);
]);
return value;
}

// rs3, the third source of an r4-type fused multiply-add, exists only when
// the ISA carries such an op. Its decode field then appears in [fields].
final hasRs3 = fields.containsKey('rs3');

Logic readField(Logic field, {bool register = true}) => mux(
field.eq(Const(MicroOpField.rd, width: MicroOpField.width)),
(register ? rd : fields['rd']!).zeroExtend(mxlen.size),
mux(
field.eq(Const(MicroOpField.rs1, width: MicroOpField.width)),
(register ? rs1 : fields['rs1']!).zeroExtend(mxlen.size),
mux(
field.eq(Const(MicroOpField.rs2, width: MicroOpField.width)),
(register ? rs2 : fields['rs2']!).zeroExtend(mxlen.size),
mux(
field.eq(Const(MicroOpField.imm, width: MicroOpField.width)),
register ? imm : fields['imm']!,
mux(
field.eq(Const(MicroOpField.pc, width: MicroOpField.width)),
nextPc,
// rs3 is named ONLY as a register INDEX, by the extra ReadRegister
// that a fused multiply-add carries. The FPU takes its third
// operand straight from the rs3 latch, so no XLEN-wide data path
// ever selects rs3. Folding rs3 into the decode-field variant only
// keeps the register variant, which is the wide one and has many
// more users, at its existing five-way mux.
(hasRs3 && !register)
? mux(
field.eq(
Const(MicroOpField.rs3, width: MicroOpField.width),
),
fields['rs3']!.zeroExtend(mxlen.size),
nextSp,
)
: nextSp,
),
),
Logic readField(Logic field, {bool register = true}) {
final value = Logic(name: 'fieldValue', width: mxlen.size);
// Distinct field codes are parallel choices, not a priority mux chain.
// rs3 remains an INDEX-only source; the FPU uses its separate data latch.
final choices = {
MicroOpField.rd: (register ? rd : fields['rd']!).zeroExtend(mxlen.size),
MicroOpField.rs1: (register ? rs1 : fields['rs1']!).zeroExtend(
mxlen.size,
),
),
);
MicroOpField.rs2: (register ? rs2 : fields['rs2']!).zeroExtend(
mxlen.size,
),
MicroOpField.imm: register ? imm : fields['imm']!,
MicroOpField.pc: nextPc,
if (hasRs3 && !register)
MicroOpField.rs3: fields['rs3']!.zeroExtend(mxlen.size),
};
Combinational([
Case(
field,
[
for (final entry in choices.entries)
CaseItem(Const(entry.key, width: MicroOpField.width), [
value < entry.value,
]),
],
defaultItem: [value < nextSp],
),
]);
return value;
}

// Shared microcode-ALU operands: hoisted out of the ~19-arm funct Case so
// synth infers ONE operand mux per side instead of one per arm (mux-bound).
Expand Down
16 changes: 15 additions & 1 deletion packages/river_hdl/lib/src/core/microcode_alu.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,21 @@ class MicrocodeAlu extends Module {

// ONE shared adder: add/sub/addw/subw. sub-family inverts b and carries in.
final isSub = f(MicroOpAluFunct.sub) | f(MicroOpAluFunct.subw);
final sum = a + mux(isSub, ~b, b) + isSub.zeroExtend(xlen);
final addB = mux(isSub, ~b, b);
Logic sum;
if (xlen == 64) {
// Carry-select halves keep operand inversion off a full XLEN carry
// chain. Both upper carry outcomes are computed in parallel.
final low =
a.slice(31, 0).zeroExtend(33) +
addB.slice(31, 0).zeroExtend(33) +
isSub.zeroExtend(33);
final high0 = a.slice(63, 32) + addB.slice(63, 32);
final high1 = a.slice(63, 32) + addB.slice(63, 32) + Const(1, width: 32);
sum = [mux(low[32], high1, high0), low.slice(31, 0)].swizzle();
} else {
sum = a + addB + isSub.zeroExtend(xlen);
}
final sumW = sum.slice(31, 0).signExtend(xlen);

// Bitwise.
Expand Down
Loading
Loading