Skip to content
Open
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
40 changes: 40 additions & 0 deletions docs/core/instruction-only-cache.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Instruction-only L1 builds

Select instruction-only caching through the existing SoC derivation:

```nix
river-hdl.mkSoC {
socName = "instruction_only_soc";
cores = [ "rc1-s" ];
memories = [ "0x80000000:4K:sram" ];
instructionOnlyCache = true;
}
```

The option passes `--instruction-only-cache` to `river-genip`. Direct CLI use:

```sh
river-genip --core rc1-s --device sram:0x80000000:4K \
--instruction-only-cache --output output
```

It selects a 64-byte, direct-mapped I-cache with 8-byte lines and **no D-cache**.
All data accesses bypass the cache. Omitting the option (or setting it to false
in Nix) preserves the selected tier's existing defaults; it does not select an
uncached CPU.

The switch applies to all harts. Supported profiles are `rc1-n`, `rc1-mi`,
`rc1-s`, and `rc1-f`, with the same XLEN throughout the SoC. The out-of-order
`rc1-m` profile is rejected for this option.

For the RV64 physical-cache path, only explicitly declared `sram` and `dram`
windows are eligible for instruction caching. The existing physical stage
requires the complete refill line to fit in a RAM PMA after translation and
permission checks. MMIO, flash, PSRAM, generated boot ROM and other unlisted
addresses bypass this path. RAM overlapping another declared bus device is
rejected. These PMAs do not advertise atomics or misaligned access support.
The legacy bare instruction-cache path retains its existing behavior.

This option does not change cache sizes on existing builds, enable cached
misaligned loads, change core ISA/pipeline profiles, or change board defaults.
It is not a timing or FPGA-boot guarantee.
2 changes: 1 addition & 1 deletion nix/common-dart.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ lib: {
pubspecLock = lib.importJSON ../pubspec.lock.json;

gitHashes = {
harbor = "sha256-CrtRfSxeyQu1yZ4HV62zalDjy14VVDx+08EJz3YB4A4=";
harbor = "sha256-jYuXoOWRfGWa7Bv6ZHtqxz6l9dB6pZkbel/YNhhT2bM=";
};
}
2 changes: 1 addition & 1 deletion packages/river_emulator/lib/src/plugins/cache_plugin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class CachePlugin extends FiberPlugin {

l1d = config.l1cache?.d != null
? Cache(
config.l1cache!.d,
config.l1cache!.d!,
fill: (addr, size) async {
return await mmu.readBlock(addr, size, pageTranslate: false);
},
Expand Down
79 changes: 79 additions & 0 deletions packages/river_emulator/test/instruction_only_cache_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import 'package:river/river.dart';
import 'package:river_emulator/river_emulator.dart';
import 'package:test/test.dart';

class _Memory extends DeviceAccessor {
final bytes = <int, int>{};
int reads = 0, writes = 0;

void putWord(int address, int value) {
for (var i = 0; i < 4; i++) {
bytes[address + i] = (value >> (8 * i)) & 255;
}
}

@override
Future<int> read(int addr, int width) async {
reads++;
var value = 0;
for (var i = 0; i < width; i++) {
value |= (bytes[addr + i] ?? 0) << (8 * i);
}
return value;
}

@override
Future<void> write(int addr, int value, int width) async {
writes++;
for (var i = 0; i < width; i++) {
bytes[addr + i] = (value >> (8 * i)) & 255;
}
}
}

void main() {
test(
'emulator instruction-only cache retains fetches but not RAM data',
() async {
final memory = _Memory()
..putWord(0, 0x13)
..putWord(0x100, 0x1111);
final core = RiverCore(
RiverCoreConfigV1.micro(
mmu: HarborMmuConfig(
mxlen: RiscVMxlen.rv32,
pagingModes: const [RiscVPagingMode.bare],
tlbLevels: const [],
pmp: HarborPmpConfig.none,
),
interrupts: [],
clock: const HarborClockConfig(
name: 'test',
rate: HarborFixedClockRate(100000000),
),
l1cache: const HarborL1CacheConfig.instructionOnly(
HarborL1iCacheConfig(size: 64, ways: 1, lineSize: 16),
),
),
memDevices: {const BusAddressRange(0, 4096): memory},
);
core.reset();
expect(core.l1i, isNotNull);
expect(core.l1d, isNull);
expect(await core.fetch(0), 0x13);
final coldReads = memory.reads;
memory.putWord(0, 0x00100093);
expect(await core.fetch(0), 0x13);
expect(memory.reads, coldReads);
expect(await core.read(0x100, 4), 0x1111);
memory.putWord(0x100, 0x2222);
expect(await core.read(0x100, 4), 0x2222);
expect(memory.reads, coldReads + 2);
await core.write(0x104, 0x3333, 4);
expect(memory.writes, 1);
expect(await core.read(0x104, 4), 0x3333);
core.reset();
expect(await core.fetch(0), 0x00100093);
},
);
}
9 changes: 9 additions & 0 deletions packages/river_hdl/bin/river_genip.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ Future<void> main(List<String> arguments) async {
defaultsTo: ['rc1-mi'],
allowed: ['rc1-n', 'rc1-mi', 'rc1-s', 'rc1-m', 'rc1-f'],
)
..addFlag(
'instruction-only-cache',
negatable: false,
help:
'Use an instruction-only L1 on scalar in-order cores. '
'Data stays uncached; physical instruction caching covers '
'declared SRAM/DRAM only.',
)
Comment on lines +24 to +31

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a properly of the River Core config, not a genip flag.

..addOption(
'interconnect',
abbr: 'i',
Expand Down Expand Up @@ -154,6 +162,7 @@ Future<void> main(List<String> arguments) async {
final config = RiverGenIpConfig(
name: args.option('name')!,
cores: args.multiOption('core'),
instructionOnlyCache: args.flag('instruction-only-cache'),
interconnect: args.option('interconnect')!,
clockFrequency: int.parse(args.option('clock-freq')!),
oscFrequency: int.parse(args.option('osc-freq')!),
Expand Down
6 changes: 3 additions & 3 deletions packages/river_hdl/lib/src/core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ class RiverCore extends BridgeModule {
final physicalCaches =
l1 != null &&
!dualDispatch &&
l1.d.lineSize <= 4096 &&
(l1.d == null || l1.d!.lineSize <= 4096) &&
(l1.i == null || l1.i!.lineSize <= 4096) &&
config.mmu.hasPaging &&
!config.hasHypervisor &&
Expand Down Expand Up @@ -527,7 +527,7 @@ class RiverCore extends BridgeModule {
// captures correctly, never a back-to-back burst. The D-cache extends the
// same pacing to data loads.
final useICache = l1?.i != null && !physicalCaches;
final useDCache = l1 != null && !physicalCaches;
final useDCache = l1?.d != null && !physicalCaches;

// Permission context for legacy L1 tags. Those caches are in FRONT of the MMU,
// so a HIT never reaches the MMU and no permission check runs on it. Without
Expand Down Expand Up @@ -631,7 +631,7 @@ class RiverCore extends BridgeModule {
HarborL1DCache? dcache;
if (useDCache) {
dcache = HarborL1DCache(
config: l1.d,
config: l1!.d!,
xlen: config.mxlen.size,
ctxBits: dCtx?.width ?? 0,
reqAddrBits: l1AddrBits,
Expand Down
76 changes: 45 additions & 31 deletions packages/river_hdl/lib/src/core/physical_l1.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class RiverPhysicalL1 extends Module {
final xlen = addr.width;
final laneBits = (xlen ~/ 8 - 1).bitLength;
for (final bytes in [
config.d.lineSize,
if (config.d != null) config.d!.lineSize,
if (config.i != null) config.i!.lineSize,
]) {
if (bytes < xlen ~/ 8 || bytes > 4096 || (bytes & (bytes - 1)) != 0) {
Expand Down Expand Up @@ -100,7 +100,11 @@ class RiverPhysicalL1 extends Module {
~walk &
fetch;
final selectD =
cacheableLine(config.d.lineSize, executable: false) & ~walk & ~fetch;
(config.d == null
? Const(0)
: cacheableLine(config.d!.lineSize, executable: false)) &
~walk &
~fetch;
final bypass = ~selectI & ~selectD;

final done = Logic(name: 'memoryDone');
Expand Down Expand Up @@ -134,42 +138,52 @@ class RiverPhysicalL1 extends Module {
icache.input('mem_fault').srcConnection! <= Const(0);
icache.input('mem_rdata').srcConnection! <= result;
}
final dcache = HarborL1DCache(
config: config.d,
xlen: xlen,
reqAddrBits: xlen,
cacheableBase: 0,
memFaultIn: Const(0),
target: target,
);
dcache.input('clk').srcConnection! <= clk;
dcache.input('reset').srcConnection! <= reset;
dcache.input('req_addr').srcConnection! <= addr;
dcache.input('req_valid').srcConnection! <= valid & selectD;
dcache.input('req_write').srcConnection! <= write;
dcache.input('req_data').srcConnection! <= data;
dcache.input('req_size').srcConnection! <= size;
dcache.input('flush').srcConnection! <= flush | (valid & bypass & write);
dcache.input('mem_done').srcConnection! <= dDone;
dcache.input('mem_valid').srcConnection! <= success;
// D-cache bypass responses are lane-zero; refill beats are aligned.
dcache.input('mem_rdata').srcConnection! <=
result >> [busAddr.getRange(0, laneBits), Const(0, width: 3)].swizzle();
HarborL1DCache? dcache;
if (config.d != null) {
dcache = HarborL1DCache(
config: config.d!,
xlen: xlen,
reqAddrBits: xlen,
cacheableBase: 0,
// This stage receives translated addresses and checks whole-line PMAs.
physicalAddresses: true,
memFaultIn: Const(0),
target: target,
);
dcache.input('clk').srcConnection! <= clk;
dcache.input('reset').srcConnection! <= reset;
dcache.input('req_addr').srcConnection! <= addr;
dcache.input('req_valid').srcConnection! <= valid & selectD;
dcache.input('req_write').srcConnection! <= write;
dcache.input('req_data').srcConnection! <= data;
dcache.input('req_size').srcConnection! <= size;
dcache.input('flush').srcConnection! <= flush | (valid & bypass & write);
dcache.input('mem_done').srcConnection! <= dDone;
dcache.input('mem_valid').srcConnection! <= success;
// D-cache bypass responses are lane-zero; refill beats are aligned.
dcache.input('mem_rdata').srcConnection! <=
result >>
[busAddr.getRange(0, laneBits), Const(0, width: 3)].swizzle();
}
final iEn = icache?.memEn ?? Const(0);
final dEn = dcache.memEn;
final dEn = dcache?.memEn ?? Const(0);
final bypassEn = valid & bypass;
final request = iEn | dEn | bypassEn;
final requestAddr = mux(
iEn,
icache?.memAddr ?? addr,
mux(dEn, dcache.memAddr, addr),
mux(dEn, dcache?.memAddr ?? addr, addr),
);
final requestWrite = mux(
iEn,
Const(0),
mux(dEn, dcache?.memWe ?? write, write),
);
final requestWrite = mux(iEn, Const(0), mux(dEn, dcache.memWe, write));
final requestData = mux(dEn, dcache.memWdata, data);
final requestData = mux(dEn, dcache?.memWdata ?? data, data);
final requestSize = mux(
iEn,
Const(laneBits, width: 3),
mux(dEn, dcache.memSize, size),
mux(dEn, dcache?.memSize ?? size, size),
);
Sequential(clk, [
If(
Expand Down Expand Up @@ -220,8 +234,8 @@ class RiverPhysicalL1 extends Module {
]);
final iOk = icache?.respValid ?? Const(0);
final iErr = icache?.respFault ?? Const(0);
final dOk = dcache.respValid;
final dErr = dcache.respFault;
final dOk = dcache?.respValid ?? Const(0);
final dErr = dcache?.respFault ?? Const(0);
final bypassDone = done & owner.eq(0);
addOutput('response_ack') <=
valid & mux(selectI, iOk, mux(selectD, dOk, bypassDone & success));
Expand All @@ -236,7 +250,7 @@ class RiverPhysicalL1 extends Module {
mux(
selectI,
icache?.respData ?? result,
mux(selectD, dcache.respData << laneShift, result),
mux(selectD, (dcache?.respData ?? result) << laneShift, result),
);
addOutput('cyc') <= active;
addOutput('we') <= busWrite;
Expand Down
Loading
Loading