From ca330631404f875b8efc22a5501575085995db1a Mon Sep 17 00:00:00 2001 From: murdoa Date: Sat, 10 Oct 2026 11:01:08 +0100 Subject: [PATCH 1/6] chore(deps): pin Harbor physical-cache scaling prerequisite Use the exact tested head of Harbor #9 while that prerequisite is open. Keep the Dart lock representations and Nix git source hash aligned. --- nix/common-dart.nix | 2 +- pubspec.lock | 6 +++--- pubspec.lock.json | 6 +++--- pubspec.yaml | 4 ++-- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/nix/common-dart.nix b/nix/common-dart.nix index b847d55..5ed7123 100644 --- a/nix/common-dart.nix +++ b/nix/common-dart.nix @@ -2,6 +2,6 @@ lib: { pubspecLock = lib.importJSON ../pubspec.lock.json; gitHashes = { - harbor = "sha256-CrtRfSxeyQu1yZ4HV62zalDjy14VVDx+08EJz3YB4A4="; + harbor = "sha256-M0cdNv0oPOtKiWX6JYHKqMXuOfsMPPhooNDWbX8oBl8="; }; } diff --git a/pubspec.lock b/pubspec.lock index 622af88..384742e 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -125,9 +125,9 @@ packages: dependency: "direct overridden" description: path: "packages/harbor" - ref: ef4ec03a24c3b7d55fc5ee8841a7f8403b9ee095 - resolved-ref: ef4ec03a24c3b7d55fc5ee8841a7f8403b9ee095 - url: "https://git.lilithsemi.com/LilithSemi/harbor" + ref: "57b7bdccb1b2e2bef2c87c387f339d00b2274d2a" + resolved-ref: "57b7bdccb1b2e2bef2c87c387f339d00b2274d2a" + url: "https://github.com/murdoa/harbor.git" source: git version: "0.0.1" html: diff --git a/pubspec.lock.json b/pubspec.lock.json index b2d6b60..80252d3 100644 --- a/pubspec.lock.json +++ b/pubspec.lock.json @@ -154,9 +154,9 @@ "dependency": "direct overridden", "description": { "path": "packages/harbor", - "ref": "ef4ec03a24c3b7d55fc5ee8841a7f8403b9ee095", - "resolved-ref": "ef4ec03a24c3b7d55fc5ee8841a7f8403b9ee095", - "url": "https://git.lilithsemi.com/LilithSemi/harbor" + "ref": "57b7bdccb1b2e2bef2c87c387f339d00b2274d2a", + "resolved-ref": "57b7bdccb1b2e2bef2c87c387f339d00b2274d2a", + "url": "https://github.com/murdoa/harbor.git" }, "source": "git", "version": "0.0.1" diff --git a/pubspec.yaml b/pubspec.yaml index 39eff12..fa654de 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -15,9 +15,9 @@ workspace: dependency_overrides: harbor: git: - url: https://git.lilithsemi.com/LilithSemi/harbor + url: https://github.com/murdoa/harbor.git path: packages/harbor - ref: ef4ec03a24c3b7d55fc5ee8841a7f8403b9ee095 + ref: 57b7bdccb1b2e2bef2c87c387f339d00b2274d2a dev_dependencies: coverage: ^1.15.0 From da5f4d1a4900afc28df7dcadbf06d33bc5ac8896 Mon Sep 17 00:00:00 2001 From: murdoa Date: Sat, 10 Oct 2026 11:01:08 +0100 Subject: [PATCH 2/6] feat(cache): opt physical L1 into larger data-cache capacities Enable physical indexing only in the translated, whole-line-PMA-checked stage. Keep legacy virtual callers, default geometry and cached misaligned-load policy unchanged. --- packages/river_hdl/lib/src/core/physical_l1.dart | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/river_hdl/lib/src/core/physical_l1.dart b/packages/river_hdl/lib/src/core/physical_l1.dart index cb10b59..04d6c72 100644 --- a/packages/river_hdl/lib/src/core/physical_l1.dart +++ b/packages/river_hdl/lib/src/core/physical_l1.dart @@ -139,6 +139,8 @@ class RiverPhysicalL1 extends Module { xlen: xlen, reqAddrBits: xlen, cacheableBase: 0, + // This stage receives translated addresses and checks whole-line PMAs. + physicalAddresses: true, memFaultIn: Const(0), target: target, ); From 3bbe44cea710f90cab5d4af5be6549f178caec18 Mon Sep 17 00:00:00 2001 From: murdoa Date: Sat, 10 Oct 2026 11:01:08 +0100 Subject: [PATCH 3/6] test(cache): exercise physical cache capacities and index banks --- .../test/mmu/physical_alias_test.dart | 8 +- .../mmu/physical_capacity_guard_test.dart | 56 ++++++++ .../mmu/physical_capacity_index_test.dart | 127 ++++++++++++++++++ .../test/mmu/physical_capacity_test.dart | 17 +++ .../test/mmu/physical_l1_flush_test.dart | 8 +- .../test/mmu/physical_l1_pma_test.dart | 6 +- .../river_hdl/test/mmu/physical_l1_test.dart | 6 +- 7 files changed, 218 insertions(+), 10 deletions(-) create mode 100644 packages/river_hdl/test/mmu/physical_capacity_guard_test.dart create mode 100644 packages/river_hdl/test/mmu/physical_capacity_index_test.dart create mode 100644 packages/river_hdl/test/mmu/physical_capacity_test.dart diff --git a/packages/river_hdl/test/mmu/physical_alias_test.dart b/packages/river_hdl/test/mmu/physical_alias_test.dart index 216c4a0..4230602 100644 --- a/packages/river_hdl/test/mmu/physical_alias_test.dart +++ b/packages/river_hdl/test/mmu/physical_alias_test.dart @@ -4,7 +4,9 @@ import 'package:river_hdl/river_hdl.dart'; import 'package:rohd/rohd.dart'; import 'package:test/test.dart'; -void main() { +void main() => physicalAliasTests(); + +void physicalAliasTests({int dSize = 4096}) { tearDown(Simulator.reset); for (final microcoded in [false, true]) { for (final (cached, remap, device) in [ @@ -80,8 +82,8 @@ void main() { ? MicrocodeMode.full : MicrocodeMode.none, l1cache: cached - ? const HarborL1CacheConfig.unified( - HarborL1dCacheConfig(size: 4096, ways: 1, lineSize: 16), + ? HarborL1CacheConfig.unified( + HarborL1dCacheConfig(size: dSize, ways: 1, lineSize: 16), ) : null, mmu: HarborMmuConfig( diff --git a/packages/river_hdl/test/mmu/physical_capacity_guard_test.dart b/packages/river_hdl/test/mmu/physical_capacity_guard_test.dart new file mode 100644 index 0000000..a59fbd2 --- /dev/null +++ b/packages/river_hdl/test/mmu/physical_capacity_guard_test.dart @@ -0,0 +1,56 @@ +import 'package:river/river.dart'; +import 'package:river_hdl/river_hdl.dart'; +import 'package:rohd/rohd.dart'; +import 'package:test/test.dart'; + +void main() { + tearDown(Simulator.reset); + for (final size in [8192, 16384]) { + for (final profile in ['bare', 'out-of-order']) { + test('$profile keeps virtual D-cache capacity guard at $size bytes', () { + final config = RiverCoreConfig( + mxlen: RiscVMxlen.rv64, + type: RiverCoreType.general, + extensions: [rv32i, rv64i, rvZicsr, rvPriv], + interrupts: [], + resetVector: 0, + executionMode: profile == 'out-of-order' + ? ExecutionMode.outOfOrder + : ExecutionMode.inOrder, + l1cache: HarborL1CacheConfig.unified( + HarborL1dCacheConfig(size: size, ways: 1, lineSize: 16), + ), + mmu: HarborMmuConfig( + mxlen: RiscVMxlen.rv64, + pagingModes: profile == 'bare' + ? const [RiscVPagingMode.bare] + : const [RiscVPagingMode.bare, RiscVPagingMode.sv39], + tlbLevels: const [], + pmp: HarborPmpConfig.none, + ), + clock: const HarborClockConfig( + name: 'test', + rate: HarborFixedClockRate(100000000), + ), + ); + expect( + () => RiverCore( + config, + busConfig: WishboneConfig( + addressWidth: 64, + dataWidth: 64, + selWidth: 8, + ), + ), + throwsA( + isA().having( + (error) => error.message.toString(), + 'message', + contains('must not exceed'), + ), + ), + ); + }); + } + } +} diff --git a/packages/river_hdl/test/mmu/physical_capacity_index_test.dart b/packages/river_hdl/test/mmu/physical_capacity_index_test.dart new file mode 100644 index 0000000..97c4f9e --- /dev/null +++ b/packages/river_hdl/test/mmu/physical_capacity_index_test.dart @@ -0,0 +1,127 @@ +import 'dart:async'; + +import 'package:river/river.dart'; +import 'package:river_hdl/src/core/physical_l1.dart'; +import 'package:rohd/rohd.dart'; +import 'package:test/test.dart'; + +void main() { + tearDown(Simulator.reset); + for (final width in [32, 64]) { + for (final capacity in [4096, 8192, 16384]) { + test( + 'physical $width-bit $capacity-byte D-cache retains each 4KiB bank', + () async { + final clk = SimpleClockGenerator(10).clk; + final reset = Logic()..inject(1), valid = Logic()..inject(0); + final address = Logic(width: width)..inject(0); + final flush = Logic()..inject(0), ack = Logic()..inject(0); + final data = Logic(width: width)..inject(0); + final bytes = width ~/ 8; + final stage = RiverPhysicalL1( + clk, + reset, + valid, + address, + Const(0), + Const(0, width: width), + Const(bytes.bitLength - 1, width: 3), + Const(0), + Const(0), + flush, + ack, + Const(0), + data, + config: HarborL1CacheConfig.unified( + HarborL1dCacheConfig(size: capacity, ways: 1, lineSize: 16), + ), + pma: const HarborPmaConfig( + regions: [HarborPmaRegion.memory(start: 0x10000, size: 0x10000)], + ), + ); + await stage.build(); + final reads = []; + BigInt value(int a) => + BigInt.from(a) | + (width == 64 ? BigInt.from(a ^ 0x55aa) << 32 : BigInt.zero); + Future tick() async { + await clk.nextNegedge; + if (ack.value.toBool()) { + ack.inject(0); + } else if (stage.output('cyc').value.toBool()) { + final a = stage.output('addr').value.toInt(); + expect(stage.output('we').value.toBool(), isFalse); + reads.add(a); + data.inject(value(a)); + ack.inject(1); + } + } + + Future load(int a, {required bool hit}) async { + final before = reads.length; + address.inject(a); + valid.inject(1); + var completed = false; + for (var i = 0; i < 200; i++) { + await tick(); + expect(stage.output('response_error').value.toBool(), isFalse); + if (stage.output('response_ack').value.toBool()) { + expect( + stage.output('response_data').value.toBigInt(), + value(a), + ); + completed = true; + break; + } + } + expect(completed, isTrue, reason: 'bounded request completion'); + valid.inject(0); + await tick(); + await tick(); + expect(reads.length - before, hit ? 0 : 16 ~/ bytes); + if (!hit) { + expect(reads.sublist(before), [ + for (var beat = 0; beat < 16; beat += bytes) (a & ~15) + beat, + ]); + } + } + + Simulator.setMaxSimTime(50000); + unawaited(Simulator.run()); + try { + await tick(); + await tick(); + reset.inject(0); + await tick(); + final addresses = [ + for (var bank = 0; bank < capacity ~/ 4096; bank++) + 0x10000 + bank * 4096 + 4096 - bytes, + ]; + for (final a in addresses) { + await load(a, hit: false); + } + for (final a in addresses.reversed) { + await load(a, hit: true); + } + // Full-address tags must distinguish the next capacity-sized region. + await load(addresses.first + capacity, hit: false); + await load(addresses.first, hit: false); + for (final a in addresses) { + await load(a, hit: true); + } + flush.inject(1); + await tick(); + flush.inject(0); + await tick(); + for (final a in addresses) { + await load(a, hit: false); + } + } finally { + await Simulator.endSimulation(); + await Simulator.simulationEnded; + } + }, + ); + } + } +} diff --git a/packages/river_hdl/test/mmu/physical_capacity_test.dart b/packages/river_hdl/test/mmu/physical_capacity_test.dart new file mode 100644 index 0000000..58f28e0 --- /dev/null +++ b/packages/river_hdl/test/mmu/physical_capacity_test.dart @@ -0,0 +1,17 @@ +import 'package:test/test.dart'; + +import 'physical_alias_test.dart' as alias; +import 'physical_l1_flush_test.dart' as flush; +import 'physical_l1_pma_test.dart' as pma; +import 'physical_l1_test.dart' as mmu; + +void main() { + for (final dSize in [4096, 8192, 16384]) { + group('physical D-cache $dSize bytes', () { + alias.physicalAliasTests(dSize: dSize); + mmu.physicalL1Tests(dSize: dSize); + pma.physicalPmaTests(dSize: dSize); + flush.physicalFlushTests(dSize: dSize); + }); + } +} diff --git a/packages/river_hdl/test/mmu/physical_l1_flush_test.dart b/packages/river_hdl/test/mmu/physical_l1_flush_test.dart index 8be763f..80d3f04 100644 --- a/packages/river_hdl/test/mmu/physical_l1_flush_test.dart +++ b/packages/river_hdl/test/mmu/physical_l1_flush_test.dart @@ -4,7 +4,9 @@ import 'package:river_hdl/src/core/physical_l1.dart'; import 'package:rohd/rohd.dart'; import 'package:test/test.dart'; -void main() { +void main() => physicalFlushTests(); + +void physicalFlushTests({int dSize = 64}) { tearDown(Simulator.reset); for (final width in [32, 64]) { test( @@ -27,8 +29,8 @@ void main() { ack, Const(0), Const(0, width: width), - config: const HarborL1CacheConfig.unified( - HarborL1dCacheConfig(size: 64, ways: 1, lineSize: 16), + config: HarborL1CacheConfig.unified( + HarborL1dCacheConfig(size: dSize, ways: 1, lineSize: 16), ), pma: const HarborPmaConfig( regions: [HarborPmaRegion.memory(start: 0x100, size: 0x100)], diff --git a/packages/river_hdl/test/mmu/physical_l1_pma_test.dart b/packages/river_hdl/test/mmu/physical_l1_pma_test.dart index 40d0a0f..d65cf43 100644 --- a/packages/river_hdl/test/mmu/physical_l1_pma_test.dart +++ b/packages/river_hdl/test/mmu/physical_l1_pma_test.dart @@ -4,7 +4,9 @@ import 'package:river_hdl/src/core/physical_l1.dart'; import 'package:rohd/rohd.dart'; import 'package:test/test.dart'; -void main() { +void main() => physicalPmaTests(); + +void physicalPmaTests({int dSize = 64}) { tearDown(Simulator.reset); for (final width in [32, 64]) { for (final fetch in [false, true]) { @@ -55,7 +57,7 @@ void main() { rdata, config: HarborL1CacheConfig.split( iSize: 64, - dSize: 64, + dSize: dSize, ways: 1, lineSize: 16, ), diff --git a/packages/river_hdl/test/mmu/physical_l1_test.dart b/packages/river_hdl/test/mmu/physical_l1_test.dart index 3fcda5d..351af27 100644 --- a/packages/river_hdl/test/mmu/physical_l1_test.dart +++ b/packages/river_hdl/test/mmu/physical_l1_test.dart @@ -4,7 +4,9 @@ import 'package:river_hdl/river_hdl.dart'; import 'package:rohd/rohd.dart'; import 'package:test/test.dart'; -void main() { +void main() => physicalL1Tests(); + +void physicalL1Tests({int dSize = 128}) { tearDown(Simulator.reset); for (final lineSize in [16, 64]) { test( @@ -65,7 +67,7 @@ void main() { cacheFlush: flush, physicalL1: HarborL1CacheConfig.split( iSize: 128, - dSize: 128, + dSize: dSize, ways: 1, lineSize: lineSize, ), From 039d89cd8940fc51df78de764465589bdb482f06 Mon Sep 17 00:00:00 2001 From: murdoa Date: Sat, 10 Oct 2026 16:31:24 +0100 Subject: [PATCH 4/6] feat(cache): support instruction-only L1 consumers --- nix/common-dart.nix | 2 +- .../lib/src/plugins/cache_plugin.dart | 2 +- packages/river_hdl/lib/src/core.dart | 6 +- .../river_hdl/lib/src/core/physical_l1.dart | 78 +++++++++++-------- .../cache/dcache_atomic_coherence_test.dart | 2 +- pubspec.lock | 4 +- pubspec.lock.json | 4 +- pubspec.yaml | 2 +- 8 files changed, 56 insertions(+), 44 deletions(-) diff --git a/nix/common-dart.nix b/nix/common-dart.nix index 5ed7123..be05b8a 100644 --- a/nix/common-dart.nix +++ b/nix/common-dart.nix @@ -2,6 +2,6 @@ lib: { pubspecLock = lib.importJSON ../pubspec.lock.json; gitHashes = { - harbor = "sha256-M0cdNv0oPOtKiWX6JYHKqMXuOfsMPPhooNDWbX8oBl8="; + harbor = "sha256-jYuXoOWRfGWa7Bv6ZHtqxz6l9dB6pZkbel/YNhhT2bM="; }; } diff --git a/packages/river_emulator/lib/src/plugins/cache_plugin.dart b/packages/river_emulator/lib/src/plugins/cache_plugin.dart index 1e20b79..5aae5a4 100644 --- a/packages/river_emulator/lib/src/plugins/cache_plugin.dart +++ b/packages/river_emulator/lib/src/plugins/cache_plugin.dart @@ -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); }, diff --git a/packages/river_hdl/lib/src/core.dart b/packages/river_hdl/lib/src/core.dart index 339ce8b..28bda28 100644 --- a/packages/river_hdl/lib/src/core.dart +++ b/packages/river_hdl/lib/src/core.dart @@ -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 && @@ -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 @@ -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, diff --git a/packages/river_hdl/lib/src/core/physical_l1.dart b/packages/river_hdl/lib/src/core/physical_l1.dart index 04d6c72..63218ec 100644 --- a/packages/river_hdl/lib/src/core/physical_l1.dart +++ b/packages/river_hdl/lib/src/core/physical_l1.dart @@ -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) { @@ -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'); @@ -134,44 +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, - // 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(); + 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( @@ -222,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)); @@ -238,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; diff --git a/packages/river_hdl/test/cache/dcache_atomic_coherence_test.dart b/packages/river_hdl/test/cache/dcache_atomic_coherence_test.dart index c0b3940..b21edb4 100644 --- a/packages/river_hdl/test/cache/dcache_atomic_coherence_test.dart +++ b/packages/river_hdl/test/cache/dcache_atomic_coherence_test.dart @@ -44,7 +44,7 @@ void main() { dSize: 256, ways: 1, lineSize: 8, - ).d, + ).d!, xlen: 64, ); dc.input('clk').srcConnection! <= clk; diff --git a/pubspec.lock b/pubspec.lock index 384742e..fa1c9cc 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -125,8 +125,8 @@ packages: dependency: "direct overridden" description: path: "packages/harbor" - ref: "57b7bdccb1b2e2bef2c87c387f339d00b2274d2a" - resolved-ref: "57b7bdccb1b2e2bef2c87c387f339d00b2274d2a" + ref: "58e4e5e0643b83b83eada739fabb94bc27d6d470" + resolved-ref: "58e4e5e0643b83b83eada739fabb94bc27d6d470" url: "https://github.com/murdoa/harbor.git" source: git version: "0.0.1" diff --git a/pubspec.lock.json b/pubspec.lock.json index 80252d3..2bb9dfb 100644 --- a/pubspec.lock.json +++ b/pubspec.lock.json @@ -154,8 +154,8 @@ "dependency": "direct overridden", "description": { "path": "packages/harbor", - "ref": "57b7bdccb1b2e2bef2c87c387f339d00b2274d2a", - "resolved-ref": "57b7bdccb1b2e2bef2c87c387f339d00b2274d2a", + "ref": "58e4e5e0643b83b83eada739fabb94bc27d6d470", + "resolved-ref": "58e4e5e0643b83b83eada739fabb94bc27d6d470", "url": "https://github.com/murdoa/harbor.git" }, "source": "git", diff --git a/pubspec.yaml b/pubspec.yaml index fa654de..3b6032b 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -17,7 +17,7 @@ dependency_overrides: git: url: https://github.com/murdoa/harbor.git path: packages/harbor - ref: 57b7bdccb1b2e2bef2c87c387f339d00b2274d2a + ref: 58e4e5e0643b83b83eada739fabb94bc27d6d470 dev_dependencies: coverage: ^1.15.0 From 3f6ee55983e1e35bfff506948161dad5605f1ba7 Mon Sep 17 00:00:00 2001 From: murdoa Date: Sat, 10 Oct 2026 16:31:24 +0100 Subject: [PATCH 5/6] test(cache): cover instruction-only cache behavior --- .../test/instruction_only_cache_test.dart | 79 ++++++++ .../test/cache/core_icache_test.dart | 22 +-- .../test/cache/instruction_only_test.dart | 120 +++++++++++++ .../test/mmu/instruction_only_core_test.dart | 15 ++ .../test/mmu/instruction_only_stage_test.dart | 170 ++++++++++++++++++ .../test/mmu/physical_alias_test.dart | 24 ++- .../test/mmu/physical_l1_flush_test.dart | 10 +- 7 files changed, 419 insertions(+), 21 deletions(-) create mode 100644 packages/river_emulator/test/instruction_only_cache_test.dart create mode 100644 packages/river_hdl/test/cache/instruction_only_test.dart create mode 100644 packages/river_hdl/test/mmu/instruction_only_core_test.dart create mode 100644 packages/river_hdl/test/mmu/instruction_only_stage_test.dart diff --git a/packages/river_emulator/test/instruction_only_cache_test.dart b/packages/river_emulator/test/instruction_only_cache_test.dart new file mode 100644 index 0000000..53b5f1b --- /dev/null +++ b/packages/river_emulator/test/instruction_only_cache_test.dart @@ -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 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 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 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); + }, + ); +} diff --git a/packages/river_hdl/test/cache/core_icache_test.dart b/packages/river_hdl/test/cache/core_icache_test.dart index e968173..0187572 100644 --- a/packages/river_hdl/test/cache/core_icache_test.dart +++ b/packages/river_hdl/test/cache/core_icache_test.dart @@ -7,7 +7,9 @@ import '../core_harness.dart'; /// L1 instruction cache integration: the core fetches through the icache /// (hits in one cycle, misses fill a line from the MMU). Verifies correctness /// with the cache enabled, in both single- and dual-dispatch. -void main() { +void main() => instructionCacheTests(); + +void instructionCacheTests({HarborL1CacheConfig? cacheConfig}) { tearDown(() async { await Simulator.reset(); }); @@ -31,12 +33,9 @@ void main() { type: RiverCoreType.general, executionMode: ExecutionMode.outOfOrder, speculativeFetch: true, - l1cache: HarborL1CacheConfig.split( - iSize: 32, - dSize: 64, - ways: 1, - lineSize: 4, - ), + l1cache: + cacheConfig ?? + HarborL1CacheConfig.split(iSize: 32, dSize: 64, ways: 1, lineSize: 4), ); RiverCoreConfig icacheDual() => RiverCoreConfig( @@ -52,12 +51,9 @@ void main() { executionMode: ExecutionMode.outOfOrder, speculativeFetch: true, issueWidth: IssueWidth.dual, - l1cache: HarborL1CacheConfig.split( - iSize: 32, - dSize: 64, - ways: 1, - lineSize: 4, - ), + l1cache: + cacheConfig ?? + HarborL1CacheConfig.split(iSize: 32, dSize: 64, ways: 1, lineSize: 4), ); int iimm(int imm, int rs1, int f3, int rd) => diff --git a/packages/river_hdl/test/cache/instruction_only_test.dart b/packages/river_hdl/test/cache/instruction_only_test.dart new file mode 100644 index 0000000..e366799 --- /dev/null +++ b/packages/river_hdl/test/cache/instruction_only_test.dart @@ -0,0 +1,120 @@ +import 'dart:async'; + +import 'package:river/river.dart'; +import 'package:river_hdl/river_hdl.dart'; +import 'package:rohd/rohd.dart'; +import 'package:test/test.dart'; + +import 'core_icache_test.dart' as legacy; + +void main() { + group('instruction-only legacy frontend', () { + legacy.instructionCacheTests( + cacheConfig: const HarborL1CacheConfig.instructionOnly( + HarborL1iCacheConfig(size: 32, ways: 1, lineSize: 4), + ), + ); + }); + test( + 'bare instruction-only core observes every data read and caches its loop', + () async { + final core = RiverCore( + RiverCoreConfig( + mxlen: RiscVMxlen.rv32, + type: RiverCoreType.general, + extensions: [rv32i, rvZicsr, rvZifencei], + interrupts: [], + resetVector: 0, + clock: const HarborClockConfig( + name: 'test', + rate: HarborFixedClockRate(100000000), + ), + l1cache: const HarborL1CacheConfig.instructionOnly( + HarborL1iCacheConfig(size: 128, ways: 1, lineSize: 16), + ), + mmu: HarborMmuConfig( + mxlen: RiscVMxlen.rv32, + pagingModes: const [RiscVPagingMode.bare], + tlbLevels: const [], + pmp: HarborPmpConfig.none, + ), + ), + busConfig: WishboneConfig(addressWidth: 32, dataWidth: 32, selWidth: 4), + ); + final clk = SimpleClockGenerator(10).clk; + final reset = Logic()..inject(1), ack = Logic()..inject(0); + final rdata = Logic(width: 32)..inject(0); + core.input('clk').srcConnection! <= clk; + core.input('reset').srcConnection! <= reset; + core.input('dataBus_ACK').srcConnection! <= ack; + core.input('dataBus_DAT_MISO').srcConnection! <= rdata; + await core.build(); + final program = [ + 0x800002b7, // lui t0,0x80000 + 0x0002a503, // lw a0,0(t0) + 0x0002a583, // lw a1,0(t0) + 0x00b2a223, // sw a1,4(t0) + 0x06600913, // completion marker + 0x0000006f, + ]; + var dataReads = 0, instructionReads = 0, stores = 0; + Future tick() async { + await clk.nextNegedge; + if (ack.value.toBool()) { + ack.inject(0); + return; + } + if (!core.output('dataBus_CYC').value.toBool() || + !core.output('dataBus_STB').value.toBool()) { + return; + } + final a = core.output('dataBus_ADR').value.toInt(); + if (core.output('dataBus_WE').value.toBool()) { + expect(a, 0x80000004); + expect(core.output('dataBus_DAT_MOSI').value.toInt(), 0x2222); + stores++; + } else if (a == 0x80000000) { + dataReads++; + rdata.inject(dataReads * 0x1111); + } else { + instructionReads++; + rdata.inject(a ~/ 4 < program.length ? program[a ~/ 4] : 0x13); + } + ack.inject(1); + } + + int reg(int n) => core.regs.getData(LogicValue.ofInt(n, 5))!.toInt(); + Simulator.setMaxSimTime(50000); + unawaited(Simulator.run()); + try { + await tick(); + await tick(); + reset.inject(0); + for (var i = 0; i < 2000 && reg(18) != 0x66; i++) { + await tick(); + } + expect(reg(18), 0x66, reason: 'program completion, not timeout'); + expect(reg(10), 0x1111); + expect(reg(11), 0x2222); + expect(dataReads, 2); + expect(stores, 1); + expect(instructionReads, 8, reason: 'two 16-byte instruction refills'); + for (var i = 0; i < 40; i++) { + await tick(); + } + expect( + instructionReads, + 8, + reason: 'the loop stays in the instruction cache', + ); + expect(dataReads, 2); + expect(stores, 1); + expect(core.generateSynth(), isNot(contains('module HarborL1DCache'))); + } finally { + await Simulator.endSimulation(); + await Simulator.simulationEnded; + await Simulator.reset(); + } + }, + ); +} diff --git a/packages/river_hdl/test/mmu/instruction_only_core_test.dart b/packages/river_hdl/test/mmu/instruction_only_core_test.dart new file mode 100644 index 0000000..3d667ac --- /dev/null +++ b/packages/river_hdl/test/mmu/instruction_only_core_test.dart @@ -0,0 +1,15 @@ +import 'package:river/river.dart'; +import 'package:test/test.dart'; + +import 'physical_alias_test.dart' as alias; +import 'physical_l1_flush_test.dart' as flush; + +void main() { + const config = HarborL1CacheConfig.instructionOnly( + HarborL1iCacheConfig(size: 64, ways: 1, lineSize: 16), + ); + group('instruction-only', () { + alias.physicalAliasTests(cacheConfig: config, uncachedData: true); + flush.physicalFlushTests(config: config); + }); +} diff --git a/packages/river_hdl/test/mmu/instruction_only_stage_test.dart b/packages/river_hdl/test/mmu/instruction_only_stage_test.dart new file mode 100644 index 0000000..e9bd3a8 --- /dev/null +++ b/packages/river_hdl/test/mmu/instruction_only_stage_test.dart @@ -0,0 +1,170 @@ +import 'dart:async'; + +import 'package:river/river.dart'; +import 'package:river_hdl/src/core/physical_l1.dart'; +import 'package:rohd/rohd.dart'; +import 'package:test/test.dart'; + +void main() => instructionOnlyStageTests(); + +void instructionOnlyStageTests({ + HarborL1CacheConfig? config, + HarborPmaConfig? pma, + List widths = const [32, 64], +}) { + final cache = + config ?? + const HarborL1CacheConfig.instructionOnly( + HarborL1iCacheConfig(size: 64, ways: 1, lineSize: 16), + ); + final lineSize = cache.i!.lineSize; + tearDown(Simulator.reset); + for (final width in widths) { + test( + 'instruction-only $width-bit stage caches fetches but never data', + () async { + final clk = SimpleClockGenerator(10).clk; + final reset = Logic()..inject(1), valid = Logic()..inject(0); + final address = Logic(width: width)..inject(0); + final write = Logic()..inject(0), fetch = Logic()..inject(0); + final wdata = Logic(width: width)..inject(0); + final flush = Logic()..inject(0), ack = Logic()..inject(0); + final error = Logic()..inject(0), + rdata = Logic(width: width)..inject(0); + final bytes = width ~/ 8; + final stage = RiverPhysicalL1( + clk, + reset, + valid, + address, + write, + wdata, + Const(bytes.bitLength - 1, width: 3), + fetch, + Const(0), + flush, + ack, + error, + rdata, + config: cache, + pma: + pma ?? + const HarborPmaConfig( + regions: [ + HarborPmaRegion.memory(start: 0x10000, size: 0x10000), + ], + ), + ); + await stage.build(); + final memory = {0x10000: 0x11111111}; + final reads = []; + var stores = 0; + int? failingAddress; + Future tick() async { + await clk.nextNegedge; + if (ack.value.toBool() || error.value.toBool()) { + ack.inject(0); + error.inject(0); + return; + } + if (!stage.output('cyc').value.toBool()) return; + final a = stage.output('addr').value.toInt(); + expect(stage.output('sel').value.toInt(), (1 << bytes) - 1); + if (stage.output('we').value.toBool()) { + stores++; + memory[a] = stage.output('data').value.toInt(); + } else { + reads.add(a); + rdata.inject(memory[a] ?? a); + } + ack.inject(1); + error.inject(a == failingAddress ? 1 : 0); + } + + Future<(bool, int)> access( + int a, { + bool instruction = false, + int? store, + }) async { + address.inject(a); + fetch.inject(instruction ? 1 : 0); + write.inject(store == null ? 0 : 1); + wdata.inject(store ?? 0); + valid.inject(1); + for (var cycle = 0; cycle < 200; cycle++) { + await tick(); + final failed = stage.output('response_error').value.toBool(); + if (failed || stage.output('response_ack').value.toBool()) { + final value = stage.output('response_data').value.toInt(); + valid.inject(0); + await tick(); + await tick(); + return (failed, value); + } + } + fail('bounded request did not complete'); + } + + Simulator.setMaxSimTime(30000); + unawaited(Simulator.run()); + try { + await tick(); + await tick(); + reset.inject(0); + await tick(); + expect(await access(0x10000, instruction: true), (false, 0x11111111)); + expect(reads.length, lineSize ~/ bytes); + final coldReads = reads.length; + memory[0x10000] = 0x22222222; + expect(await access(0x10000, instruction: true), (false, 0x11111111)); + expect(reads.length, coldReads, reason: 'warm I-cache hit'); + expect(await access(0x10000), (false, 0x22222222)); + memory[0x10000] = 0x33333333; + expect(await access(0x10000), (false, 0x33333333)); + expect( + reads.length, + coldReads + 2, + reason: 'both RAM data reads bypass', + ); + expect((await access(0x10000, store: 0x44444444)).$1, isFalse); + expect(stores, 1); + expect(await access(0x10000), (false, 0x44444444)); + expect(await access(0x10000, instruction: true), (false, 0x11111111)); + final beforeFlush = reads.length; + flush.inject(1); + await tick(); + flush.inject(0); + await tick(); + expect(await access(0x10000, instruction: true), (false, 0x44444444)); + expect(reads.length - beforeFlush, lineSize ~/ bytes); + // A failed refill beat must not install a usable instruction line. + failingAddress = 0x10020 + (lineSize > bytes ? bytes : 0); + expect((await access(0x10020, instruction: true)).$1, isTrue); + failingAddress = null; + final beforeRetry = reads.length; + expect(await access(0x10020, instruction: true), (false, 0x10020)); + expect(reads.length - beforeRetry, lineSize ~/ bytes); + final filled = reads.length; + expect(await access(0x10020, instruction: true), (false, 0x10020)); + expect(reads.length, filled); + // Instruction addresses outside declared RAM must bypass as well. + final beforeBypass = reads.length; + expect(await access(0x30000, instruction: true), (false, 0x30000)); + memory[0x30000] = 0x55555555; + expect(await access(0x30000, instruction: true), (false, 0x55555555)); + expect(reads.length - beforeBypass, 2); + failingAddress = 0x10080; + expect((await access(0x10080)).$1, isTrue); + failingAddress = null; + expect(await access(0x10080), (false, 0x10080)); + final rtl = stage.generateSynth(); + expect(rtl, contains('module HarborL1ICache')); + expect(rtl, isNot(contains('module HarborL1DCache'))); + } finally { + await Simulator.endSimulation(); + await Simulator.simulationEnded; + } + }, + ); + } +} diff --git a/packages/river_hdl/test/mmu/physical_alias_test.dart b/packages/river_hdl/test/mmu/physical_alias_test.dart index 4230602..0297590 100644 --- a/packages/river_hdl/test/mmu/physical_alias_test.dart +++ b/packages/river_hdl/test/mmu/physical_alias_test.dart @@ -6,7 +6,11 @@ import 'package:test/test.dart'; void main() => physicalAliasTests(); -void physicalAliasTests({int dSize = 4096}) { +void physicalAliasTests({ + int dSize = 4096, + HarborL1CacheConfig? cacheConfig, + bool uncachedData = false, +}) { tearDown(Simulator.reset); for (final microcoded in [false, true]) { for (final (cached, remap, device) in [ @@ -82,9 +86,14 @@ void physicalAliasTests({int dSize = 4096}) { ? MicrocodeMode.full : MicrocodeMode.none, l1cache: cached - ? HarborL1CacheConfig.unified( - HarborL1dCacheConfig(size: dSize, ways: 1, lineSize: 16), - ) + ? cacheConfig ?? + HarborL1CacheConfig.unified( + HarborL1dCacheConfig( + size: dSize, + ways: 1, + lineSize: 16, + ), + ) : null, mmu: HarborMmuConfig( mxlen: RiscVMxlen.rv64, @@ -185,6 +194,13 @@ void physicalAliasTests({int dSize = 4096}) { expect(reg(10), 0x11111111); if (!device) expect(reg(12), 0x11111111); expect(stores, remap || device ? 0 : 1); + if (uncachedData && cached && !device) { + expect( + dataReads, + remap ? 2 : 3, + reason: 'data reads remain uncached', + ); + } if (remap) { expect( (reg(14) >> 44) & 0xffff, diff --git a/packages/river_hdl/test/mmu/physical_l1_flush_test.dart b/packages/river_hdl/test/mmu/physical_l1_flush_test.dart index 80d3f04..dd92abc 100644 --- a/packages/river_hdl/test/mmu/physical_l1_flush_test.dart +++ b/packages/river_hdl/test/mmu/physical_l1_flush_test.dart @@ -6,7 +6,7 @@ import 'package:test/test.dart'; void main() => physicalFlushTests(); -void physicalFlushTests({int dSize = 64}) { +void physicalFlushTests({int dSize = 64, HarborL1CacheConfig? config}) { tearDown(Simulator.reset); for (final width in [32, 64]) { test( @@ -29,9 +29,11 @@ void physicalFlushTests({int dSize = 64}) { ack, Const(0), Const(0, width: width), - config: HarborL1CacheConfig.unified( - HarborL1dCacheConfig(size: dSize, ways: 1, lineSize: 16), - ), + config: + config ?? + HarborL1CacheConfig.unified( + HarborL1dCacheConfig(size: dSize, ways: 1, lineSize: 16), + ), pma: const HarborPmaConfig( regions: [HarborPmaRegion.memory(start: 0x100, size: 0x100)], ), From c05b911fac42561733ad7a1e9bf87d7b9e8a3c32 Mon Sep 17 00:00:00 2001 From: murdoa Date: Sat, 10 Oct 2026 16:31:24 +0100 Subject: [PATCH 6/6] feat(nix): expose instruction-only CPU cache builds --- docs/core/instruction-only-cache.md | 40 ++++++++ packages/river_hdl/bin/river_genip.dart | 9 ++ packages/river_hdl/lib/src/genip.dart | 67 +++++++++++++ .../cache/genip_instruction_only_test.dart | 98 +++++++++++++++++++ pkgs/river-ip/default.nix | 12 ++- 5 files changed, 225 insertions(+), 1 deletion(-) create mode 100644 docs/core/instruction-only-cache.md create mode 100644 packages/river_hdl/test/cache/genip_instruction_only_test.dart diff --git a/docs/core/instruction-only-cache.md b/docs/core/instruction-only-cache.md new file mode 100644 index 0000000..601ee4a --- /dev/null +++ b/docs/core/instruction-only-cache.md @@ -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. diff --git a/packages/river_hdl/bin/river_genip.dart b/packages/river_hdl/bin/river_genip.dart index 23253d8..ef9f84f 100644 --- a/packages/river_hdl/bin/river_genip.dart +++ b/packages/river_hdl/bin/river_genip.dart @@ -21,6 +21,14 @@ Future main(List 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.', + ) ..addOption( 'interconnect', abbr: 'i', @@ -154,6 +162,7 @@ Future main(List 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')!), diff --git a/packages/river_hdl/lib/src/genip.dart b/packages/river_hdl/lib/src/genip.dart index 5f532ef..e9441f1 100644 --- a/packages/river_hdl/lib/src/genip.dart +++ b/packages/river_hdl/lib/src/genip.dart @@ -900,6 +900,12 @@ class PinAssignment { class RiverGenIpConfig { final String name; final List cores; + + /// Opt into a 64-byte, direct-mapped instruction-only L1 with 8-byte lines. + /// False preserves each core tier's existing cache defaults. On the physical + /// path only declared SRAM/DRAM is eligible; boot ROM and MMIO bypass. + /// Supported for scalar in-order cores, not the OoO rc1-m profile. + final bool instructionOnlyCache; final String interconnect; final int clockFrequency; final int oscFrequency; @@ -946,6 +952,7 @@ class RiverGenIpConfig { const RiverGenIpConfig({ required this.name, required this.cores, + this.instructionOnlyCache = false, this.interconnect = 'wishbone', this.clockFrequency = 48000000, this.oscFrequency = 12000000, @@ -1165,6 +1172,17 @@ class RiverGenIpConfig { // `HarborMmuConfig.hasPaging` is false: the core/MMU gate off the entire // Sv39 page-table-walk datapath + satp/SUM/MXR hookups, leaving only the // bare bus arbiter. Used for machine-mode bring-up bitstreams. + if (instructionOnlyCache && + (!const ['rc1-n', 'rc1-mi', 'rc1-s', 'rc1-f'].contains(coreModel) || + cores.any( + (c) => + const ['rc1-n', 'rc1-mi'].contains(c) != + (mxlen == RiscVMxlen.rv32), + ))) { + throw ArgumentError( + 'Instruction-only cache requires same-XLEN scalar in-order cores', + ); + } final pagingOn = enableMmu && mxlen == RiscVMxlen.rv64; final mmu = HarborMmuConfig( mxlen: mxlen, @@ -1175,6 +1193,9 @@ class RiverGenIpConfig { pmp: HarborPmpConfig.none, hasSupervisorUserMemory: pagingOn, hasMakeExecutableReadable: pagingOn, + pma: instructionOnlyCache && pagingOn + ? _instructionCachePma() + : const HarborPmaConfig(), ); final factory = _coreModels[coreModel]; @@ -1183,6 +1204,11 @@ class RiverGenIpConfig { } return factory( + l1cache: instructionOnlyCache + ? const HarborL1CacheConfig.instructionOnly( + HarborL1iCacheConfig(size: 64, ways: 1, lineSize: 8), + ) + : null, hartId: hartId, mmu: mmu, interrupts: [], @@ -1198,6 +1224,47 @@ class RiverGenIpConfig { ); } + HarborPmaConfig _instructionCachePma() { + // Use declared RAM windows, never an open-ended cacheability threshold. + // Unlisted addresses bypass; these PMAs do not enable misaligned loads. + final ram = devices.where((d) => d.type == 'sram' || d.type == 'dram'); + final regions = []; + for (final d in ram) { + final start = d.address; + final size = d.size; + if (start == null || + size == null || + start < 0 || + size <= 0 || + BigInt.from(start) + BigInt.from(size) > (BigInt.one << mxlen.size)) { + throw ArgumentError('Invalid cacheable RAM window: ${d.name}'); + } + final end = BigInt.from(start) + BigInt.from(size); + for (final other in devices) { + if (identical(d, other) || + other.address == null || + other.type == 'flash-firmware') { + continue; + } + final otherStart = BigInt.from(other.address!); + final otherEnd = otherStart + BigInt.from(other.effectiveSize); + if (BigInt.from(start) < otherEnd && otherStart < end) { + throw ArgumentError('Cacheable RAM overlaps device: ${other.name}'); + } + } + regions.add( + HarborPmaRegion( + start: start, + size: size, + atomicSupport: false, + misalignedSupport: false, + accessWidths: [mxlen.size ~/ 8], + ), + ); + } + return HarborPmaConfig(regions: regions); + } + WishboneConfig buildBusConfig() => WishboneConfig( addressWidth: mxlen.size, dataWidth: mxlen.size, diff --git a/packages/river_hdl/test/cache/genip_instruction_only_test.dart b/packages/river_hdl/test/cache/genip_instruction_only_test.dart new file mode 100644 index 0000000..0f63c80 --- /dev/null +++ b/packages/river_hdl/test/cache/genip_instruction_only_test.dart @@ -0,0 +1,98 @@ +import 'package:river/river.dart'; +import 'package:river_hdl/river_hdl.dart'; +import 'package:test/test.dart'; + +import '../mmu/instruction_only_stage_test.dart' as stage; + +final clock = HarborClockConfig( + name: 'sys', + rate: HarborFixedClockRate(48000000), +); + +RiverGenIpConfig config( + String model, { + bool enabled = false, + List? devices, +}) => RiverGenIpConfig( + name: 'cache_selection', + cores: [model], + instructionOnlyCache: enabled, + devices: + devices ?? + [ + Device.parse('sram:0x10000:64K'), + Device.parse('uart:0x10000000'), + Device.parse('flash:0x20000000:1M'), + ], +); + +void main() { + for (final model in ['rc1-n', 'rc1-mi', 'rc1-s', 'rc1-f', 'rc1-m']) { + test('$model default cache configuration unchanged', () { + final core = config(model).buildCoreConfig(clock, model); + if (model == 'rc1-n' || model == 'rc1-mi') { + expect(core.l1cache, isNull); + } else { + expect(core.l1cache!.i!.size, 64); + expect(core.l1cache!.d!.size, 256); + expect(core.l1cache!.i!.lineSize, 8); + } + expect(core.mmu.pma.regions, isEmpty); + }); + } + for (final model in ['rc1-n', 'rc1-mi', 'rc1-s', 'rc1-f']) { + test('$model explicitly selects instruction-only cache', () { + final core = config(model, enabled: true).buildCoreConfig(clock, model); + expect(core.l1cache!.i!.size, 64); + expect(core.l1cache!.i!.lineSize, 8); + expect(core.l1cache!.d, isNull); + if (core.mmu.hasPaging) { + final region = core.mmu.pma.regions.single; + expect(region.start, 0x10000); + expect(region.size, 0x10000); + expect(region.accessWidths, [8]); + expect(region.misalignedSupport, isFalse); + expect(region.atomicSupport, isFalse); + } else { + expect(core.mmu.pma.regions, isEmpty); + } + }); + } + test('reject unsupported OoO profile', () { + expect( + () => config('rc1-m', enabled: true).buildCoreConfig(clock, 'rc1-m'), + throwsArgumentError, + ); + }); + test('reject RAM overlapping MMIO', () { + expect( + () => config( + 'rc1-s', + enabled: true, + devices: [ + Device.parse('sram:0x10000:64K'), + Device.parse('uart:0x18000'), + ], + ).buildCoreConfig(clock, 'rc1-s'), + throwsArgumentError, + ); + }); + test('reject mixed-XLEN instruction-only SoC', () { + expect( + () => RiverGenIpConfig( + name: 'mixed', + cores: ['rc1-s', 'rc1-mi'], + instructionOnlyCache: true, + ).buildCoreConfig(clock, 'rc1-s'), + throwsArgumentError, + ); + }); + group('genip-generated physical configuration', () { + final core = config('rc1-s', enabled: true).buildCoreConfig(clock, 'rc1-s'); + stage.instructionOnlyStageTests( + config: core.l1cache, + pma: core.mmu.pma, + widths: const [64], + ); + }); +} diff --git a/pkgs/river-ip/default.nix b/pkgs/river-ip/default.nix index 73d3215..7888af3 100644 --- a/pkgs/river-ip/default.nix +++ b/pkgs/river-ip/default.nix @@ -14,6 +14,7 @@ lib.extendMkDerivation { excludeDrvArgNames = [ "socName" "cores" + "instructionOnlyCache" "interconnect" "clockFreq" "oscFreq" @@ -30,6 +31,7 @@ lib.extendMkDerivation { name ? "river-ip-${socName}", socName ? "river_soc", cores ? [ "rc1-s" ], + instructionOnlyCache ? false, interconnect ? "wishbone", clockFreq ? 48000000, # The board's physical oscillator; the clock generator synthesizes @@ -57,6 +59,11 @@ lib.extendMkDerivation { "rc1-f" ] ) cores) "river-ip: cores must each be one of [rc1-n, rc1-mi, rc1-s, rc1-m, rc1-f]"; + assert lib.assertMsg (builtins.isBool instructionOnlyCache) + "river-ip: instructionOnlyCache must be a boolean"; + assert lib.assertMsg ( + !instructionOnlyCache || !(builtins.elem "rc1-m" cores) + ) "river-ip: instructionOnlyCache requires scalar in-order cores"; assert lib.assertMsg (builtins.elem interconnect [ "wishbone" "axi" @@ -72,6 +79,7 @@ lib.extendMkDerivation { }; coreFlags = lib.concatMapStringsSep " " (c: "--core ${c}") cores; + cacheFlag = lib.optionalString instructionOnlyCache "--instruction-only-cache"; # Memory regions are devices in the unified genip interface. Reorder the # declarative addr:size:type[:board][:params] form to the genip device # form type:addr:size[:board][:params] and pass each via --device. @@ -96,6 +104,7 @@ lib.extendMkDerivation { builtins.removeAttrs args [ "socName" "cores" + "instructionOnlyCache" "interconnect" "clockFreq" "oscFreq" @@ -119,7 +128,7 @@ lib.extendMkDerivation { buildPhase = '' runHook preBuild - river-genip ${cliArgs} ${coreFlags} ${memoryFlags} ${deviceFlags} ${targetFlag} ${boardFlag} ${pdkRootFlag} ${pinFlags} ${bootProgramFlag} --output "$out" + river-genip ${cliArgs} ${coreFlags} ${cacheFlag} ${memoryFlags} ${deviceFlags} ${targetFlag} ${boardFlag} ${pdkRootFlag} ${pinFlags} ${bootProgramFlag} --output "$out" runHook postBuild ''; @@ -129,6 +138,7 @@ lib.extendMkDerivation { inherit socName cores + instructionOnlyCache interconnect clockFreq memories