2021-09-13 01:41:54 +00:00
///////////////////////////////////////////
2022-04-01 22:14:47 +00:00
// testbench.sv
2021-09-13 01:41:54 +00:00
//
// Written: David_Harris@hmc.edu 9 January 2021
// Modified:
//
// Purpose: Wally Testbench and helper modules
2022-04-01 22:14:47 +00:00
// Applies test programs from the riscv-arch-test and Imperas suites
2021-09-13 01:41:54 +00:00
//
// A component of the Wally configurable RISC-V project.
//
// Copyright (C) 2021 Harvey Mudd College & Oklahoma State University
//
2022-01-07 12:58:40 +00:00
// MIT LICENSE
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
2021-09-13 01:41:54 +00:00
//
2022-01-07 12:58:40 +00:00
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
2021-09-13 01:41:54 +00:00
//
2022-01-07 12:58:40 +00:00
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
// BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
// OR OTHER DEALINGS IN THE SOFTWARE.
////////////////////////////////////////////////////////////////////////////////////////////////
2021-09-13 01:41:54 +00:00
`include " wally-config.vh "
2022-04-01 22:14:47 +00:00
`include " tests.vh "
2021-09-13 01:41:54 +00:00
2022-04-01 22:14:47 +00:00
module testbench ;
parameter DEBUG = 0 ;
parameter TEST = " none " ;
2021-09-13 01:41:54 +00:00
logic clk ;
2022-04-01 22:14:47 +00:00
logic reset_ext , reset ;
2021-09-13 01:41:54 +00:00
parameter SIGNATURESIZE = 5000000 ;
int test , i , errors , totalerrors ;
logic [ 31 : 0 ] sig32 [ 0 : SIGNATURESIZE ] ;
logic [ `XLEN - 1 : 0 ] signature [ 0 : SIGNATURESIZE ] ;
2022-04-01 22:14:47 +00:00
logic [ `XLEN - 1 : 0 ] testadr , testadrNoBase ;
2021-09-13 01:41:54 +00:00
string InstrFName , InstrDName , InstrEName , InstrMName , InstrWName ;
logic [ 31 : 0 ] InstrW ;
2022-04-01 22:14:47 +00:00
string tests [ ] ;
logic [ 3 : 0 ] dummy ;
2021-09-13 01:41:54 +00:00
string ProgramAddrMapFile , ProgramLabelMapFile ;
logic [ `AHBW - 1 : 0 ] HRDATAEXT ;
2022-04-01 22:14:47 +00:00
logic HREADYEXT , HRESPEXT ;
2021-09-13 01:41:54 +00:00
logic [ 31 : 0 ] HADDR ;
logic [ `AHBW - 1 : 0 ] HWDATA ;
logic HWRITE ;
logic [ 2 : 0 ] HSIZE ;
logic [ 2 : 0 ] HBURST ;
logic [ 3 : 0 ] HPROT ;
logic [ 1 : 0 ] HTRANS ;
logic HMASTLOCK ;
logic HCLK , HRESETn ;
logic [ `XLEN - 1 : 0 ] PCW ;
logic DCacheFlushDone , DCacheFlushStart ;
2022-04-01 22:14:47 +00:00
flopenr # ( `XLEN ) PCWReg ( clk , reset , ~ dut . core . ieu . dp . StallW , dut . core . ifu . PCM , PCW ) ;
flopenr # ( 32 ) InstrWReg ( clk , reset , ~ dut . core . ieu . dp . StallW , dut . core . ifu . InstrM , InstrW ) ;
2021-09-13 01:41:54 +00:00
// check assertions for a legal configuration
riscvassertions riscvassertions ( ) ;
2022-04-01 22:14:47 +00:00
string signame , memfilename , pathname , romfilename , sdcfilename ;
2021-09-13 01:41:54 +00:00
2022-04-01 22:14:47 +00:00
logic [ 31 : 0 ] GPIOPinsIn , GPIOPinsOut , GPIOPinsEn ;
2021-09-13 01:41:54 +00:00
logic UARTSin , UARTSout ;
2022-04-01 22:14:47 +00:00
2021-09-30 16:23:09 +00:00
2021-09-13 01:41:54 +00:00
2021-09-24 17:31:51 +00:00
logic SDCCLK ;
2021-09-30 16:27:42 +00:00
logic SDCCmdIn ;
logic SDCCmdOut ;
logic SDCCmdOE ;
logic [ 3 : 0 ] SDCDatIn ;
2022-04-01 22:14:47 +00:00
tri1 [ 3 : 0 ] SDCDat ;
tri1 SDCCmd ;
2021-09-30 16:27:42 +00:00
2022-04-01 22:14:47 +00:00
logic HREADY ;
logic HSELEXT ;
2021-09-24 17:31:51 +00:00
sdModel sdcard
( . sdClk ( SDCCLK ) ,
. cmd ( SDCCmd ) ,
. dat ( SDCDat ) ) ;
2022-04-01 22:14:47 +00:00
assign SDCCmd = SDCCmdOE ? SDCCmdOut : 1 ' bz ;
assign SDCCmdIn = SDCCmd ;
assign SDCDatIn = SDCDat ;
2021-09-13 01:41:54 +00:00
// instantiate device to be tested
assign GPIOPinsIn = 0 ;
assign UARTSin = 1 ;
2022-04-01 22:14:47 +00:00
wallypipelinedsoc dut ( . clk , . reset_ext , . reset , . HRDATAEXT , . HREADYEXT , . HRESPEXT , . HSELEXT ,
2021-12-13 01:53:26 +00:00
. HCLK , . HRESETn , . HADDR , . HWDATA , . HWRITE , . HSIZE , . HBURST , . HPROT ,
2022-04-01 22:14:47 +00:00
. HTRANS , . HMASTLOCK , . HREADY , . TIMECLK ( 1 'b0 ) , . GPIOPinsIn , . GPIOPinsOut , . GPIOPinsEn ,
2021-12-13 01:53:26 +00:00
. UARTSin , . UARTSout , . SDCCmdIn , . SDCCmdOut , . SDCCmdOE , . SDCDatIn , . SDCCLK ) ;
2021-09-13 01:41:54 +00:00
// Track names of instructions
2022-04-01 22:14:47 +00:00
instrTrackerTB it ( clk , reset , dut . core . ieu . dp . FlushE ,
dut . core . ifu . FinalInstrRawF [ 31 : 0 ] ,
dut . core . ifu . InstrD , dut . core . ifu . InstrE ,
dut . core . ifu . InstrM , InstrW ,
2021-09-13 01:41:54 +00:00
InstrFName , InstrDName , InstrEName , InstrMName , InstrWName ) ;
// initialize tests
2021-12-14 21:43:06 +00:00
localparam integer MemStartAddr = `RAM_BASE > > ( 1 + `XLEN / 32 ) ;
localparam integer MemEndAddr = ( `RAM_RANGE + `RAM_BASE ) > > 1 + ( `XLEN / 32 ) ;
2021-09-13 01:41:54 +00:00
initial
begin
2022-04-01 22:14:47 +00:00
test = 1 ;
2021-09-13 01:41:54 +00:00
totalerrors = 0 ;
testadr = 0 ;
2022-04-01 22:14:47 +00:00
testadrNoBase = 0 ;
2021-09-13 01:41:54 +00:00
// fill memory with defined values to reduce Xs in simulation
// Quick note the memory will need to be initialized. The C library does not
// guarantee the initialized reads. For example a strcmp can read 6 byte
// strings, but uses a load double to read them in. If the last 2 bytes are
// not initialized the compare results in an 'x' which propagates through
// the design.
2022-04-01 22:14:47 +00:00
if ( TEST = = " coremark " )
2021-09-13 01:41:54 +00:00
// read test vectors into memory
2022-04-01 22:14:47 +00:00
pathname = tvpaths [ tests [ 0 ] . atoi ( ) ] ;
/ * if ( tests [ 0 ] = = `IMPERASTEST )
pathname = tvpaths [ 0 ] ;
else pathname = tvpaths [ 1 ] ; */
memfilename = { pathname , tests [ test ] , " .elf.memfile " } ;
2021-10-25 19:07:44 +00:00
romfilename = { " ../../tests/testsBP/fpga-test-sdc/bin/fpga-test-sdc.memfile " } ;
2022-04-01 22:14:47 +00:00
sdcfilename = { " ../testbench/sdc/ramdisk2.hex " } ;
//$readmemh(romfilename, dut.wallypipelinedsoc.uncore.bootrom.bootrom.memory.RAM);
2021-09-25 22:51:38 +00:00
$readmemh ( sdcfilename , sdcard . FLASHmem ) ;
2022-04-01 22:14:47 +00:00
ProgramAddrMapFile = { pathname , tests [ test ] , " .elf.objdump.addr " } ;
ProgramLabelMapFile = { pathname , tests [ test ] , " .elf.objdump.lab " } ;
2021-09-13 01:41:54 +00:00
$display ( " Read memfile %s " , memfilename ) ;
2022-04-01 22:14:47 +00:00
reset_ext = 0 ; # 97 reset_ext = 1 ; # 1000 ; reset_ext = 0 ;
2021-09-13 01:41:54 +00:00
end
// generate clock to sequence tests
always
begin
clk = 1 ; # 5 ; clk = 0 ; # 5 ;
2022-04-01 22:14:47 +00:00
// if ($time % 100000 == 0) $display("Time is %0t", $time);
2021-09-13 01:41:54 +00:00
end
// check results
always @ ( negedge clk )
begin
2022-04-01 22:14:47 +00:00
if ( TEST = = " coremark " )
if ( dut . core . priv . priv . ecallM ) begin
$display ( " Benchmark: coremark is done. " ) ;
$stop ;
end
2021-09-13 01:41:54 +00:00
if ( DCacheFlushDone ) begin
2022-04-01 22:14:47 +00:00
2021-09-13 01:41:54 +00:00
# 600 ; // give time for instructions in pipeline to finish
// clear signature to prevent contamination from previous tests
for ( i = 0 ; i < SIGNATURESIZE ; i = i + 1 ) begin
sig32 [ i ] = ' bx ;
end
// read signature, reformat in 64 bits if necessary
2022-04-01 22:14:47 +00:00
signame = { pathname , tests [ test ] , " .signature.output " } ;
2021-09-13 01:41:54 +00:00
$readmemh ( signame , sig32 ) ;
i = 0 ;
while ( i < SIGNATURESIZE ) begin
if ( `XLEN = = 32 ) begin
signature [ i ] = sig32 [ i ] ;
i = i + 1 ;
end else begin
signature [ i / 2 ] = { sig32 [ i + 1 ] , sig32 [ i ] } ;
i = i + 2 ;
end
2022-04-01 22:14:47 +00:00
if ( i > = 4 & sig32 [ i - 4 ] = = = ' bx ) begin
if ( i = = 4 ) begin
2021-09-13 01:41:54 +00:00
i = SIGNATURESIZE + 1 ; // flag empty file
$display ( " Error: empty test file " ) ;
end else i = SIGNATURESIZE ; // skip over the rest of the x's for efficiency
end
end
// Check errors
errors = ( i = = SIGNATURESIZE + 1 ) ; // error if file is empty
i = 0 ;
2021-12-14 21:43:06 +00:00
testadr = ( `RAM_BASE + tests [ test + 1 ] . atohex ( ) ) / ( `XLEN / 8 ) ;
2022-04-01 22:14:47 +00:00
testadrNoBase = ( tests [ test + 1 ] . atohex ( ) ) / ( `XLEN / 8 ) ;
2021-09-13 01:41:54 +00:00
/* verilator lint_off INFINITELOOP */
while ( signature [ i ] ! = = ' bx ) begin
2022-04-01 22:14:47 +00:00
logic [ `XLEN - 1 : 0 ] sig ;
/ * - - - - - \ / - - - - - EXCLUDED - - - - - \ / - - - - -
if ( `DMEM = = `MEM_TIM ) sig = dut . core . lsu . dtim . dtim . ram . memory . RAM [ testadrNoBase + i ] ;
else sig = dut . uncore . ram . ram . memory . RAM [ testadrNoBase + i ] ;
- - - - - / \ - - - - - EXCLUDED - - - - - / \ - - - - - */
//$display("signature[%h] = %h sig = %h", i, signature[i], sig);
if ( signature [ i ] ! = = sig &
//if (signature[i] !== dut.core.lsu.dtim.ram.memory.RAM[testadr+i] &
( signature [ i ] ! = = DCacheFlushFSM . ShadowRAM [ testadr + i ] ) ) begin // ***i+1?
if ( ( signature [ i ] ! = = '0 | signature [ i + 4 ] ! = = 'x ) ) begin
// if (signature[i+4] !== 'bx | (signature[i] !== 32'hFFFFFFFF & signature[i] !== 32'h00000000)) begin
2021-09-13 01:41:54 +00:00
// report errors unless they are garbage at the end of the sim
// kind of hacky test for garbage right now
2022-04-01 22:14:47 +00:00
$display ( " sig4 = %h ne %b " , signature [ i + 4 ] , signature [ i + 4 ] ! = = ' bx ) ;
2021-09-13 01:41:54 +00:00
errors = errors + 1 ;
2022-04-01 22:14:47 +00:00
$display ( " Error on test %s result %d: adr = %h sim (D$) %h sim (DMEM) = %h, signature = %h " ,
tests [ test ] , i , ( testadr + i ) * ( `XLEN / 8 ) , DCacheFlushFSM . ShadowRAM [ testadr + i ] , sig , signature [ i ] ) ;
// tests[test], i, (testadr+i)*(`XLEN/8), DCacheFlushFSM.ShadowRAM[testadr+i], dut.core.lsu.dtim.ram.memory.RAM[testadr+i], signature[i]);
2021-09-13 01:41:54 +00:00
$stop ; //***debug
end
end
i = i + 1 ;
end
/* verilator lint_on INFINITELOOP */
if ( errors = = 0 ) begin
$display ( " %s succeeded. Brilliant!!! " , tests [ test ] ) ;
end
else begin
$display ( " %s failed with %d errors. :( " , tests [ test ] , errors ) ;
totalerrors = totalerrors + 1 ;
end
test = test + 2 ;
if ( test = = tests . size ( ) ) begin
if ( totalerrors = = 0 ) $display ( " SUCCESS! All tests ran without failures. " ) ;
else $display ( " FAIL: %d test programs had errors " , totalerrors ) ;
$stop ;
end
else begin
2022-04-01 22:14:47 +00:00
//pathname = tvpaths[tests[0]];
memfilename = { pathname , tests [ test ] , " .elf.memfile " } ;
//$readmemh(memfilename, dut.uncore.ram.ram.memory.RAM);
/ * - - - - - \ / - - - - - EXCLUDED - - - - - \ / - - - - -
if ( `IMEM = = `MEM_TIM ) $readmemh ( memfilename , dut . core . ifu . irom . irom . ram . memory . RAM ) ;
else $readmemh ( memfilename , dut . uncore . ram . ram . memory . RAM ) ;
if ( `DMEM = = `MEM_TIM ) $readmemh ( memfilename , dut . core . lsu . dtim . dtim . ram . memory . RAM ) ;
- - - - - / \ - - - - - EXCLUDED - - - - - / \ - - - - - */
ProgramAddrMapFile = { pathname , tests [ test ] , " .elf.objdump.addr " } ;
ProgramLabelMapFile = { pathname , tests [ test ] , " .elf.objdump.lab " } ;
$display ( " Read memfile %s " , memfilename ) ;
reset_ext = 1 ; # 47 ; reset_ext = 0 ;
2021-09-13 01:41:54 +00:00
end
end
end // always @ (negedge clk)
// track the current function or global label
if ( DEBUG = = 1 ) begin : FunctionName
FunctionName FunctionName ( . reset ( reset ) ,
. clk ( clk ) ,
. ProgramAddrMapFile ( ProgramAddrMapFile ) ,
. ProgramLabelMapFile ( ProgramLabelMapFile ) ) ;
end
2022-04-01 22:14:47 +00:00
// Termination condition
// terminate on a specific ECALL after li x3,1 for old Imperas tests, *** remove this when old imperas tests are removed
// or sw gp,-56(t0) for new Imperas tests
// or sd gp, -56(t0)
// or on a jump to self infinite loop (6f) for RISC-V Arch tests
logic ecf ; // remove this once we don't rely on old Imperas tests with Ecalls
if ( `ZICSR_SUPPORTED ) assign ecf = dut . core . priv . priv . EcallFaultM ;
else assign ecf = 0 ;
assign DCacheFlushStart = ecf &
( dut . core . ieu . dp . regf . rf [ 3 ] = = 1 |
( dut . core . ieu . dp . regf . we3 &
dut . core . ieu . dp . regf . a3 = = 3 &
dut . core . ieu . dp . regf . wd3 = = 1 ) ) |
( dut . core . ifu . InstrM = = 32 'h6f | dut . core . ifu . InstrM = = 32 'hfc32a423 | dut . core . ifu . InstrM = = 32 'hfc32a823 ) & dut . core . ieu . c . InstrValidM ;
2021-09-13 01:41:54 +00:00
DCacheFlushFSM DCacheFlushFSM ( . clk ( clk ) ,
2022-04-01 22:14:47 +00:00
. reset ( reset ) ,
. start ( DCacheFlushStart ) ,
. done ( DCacheFlushDone ) ) ;
2021-09-13 01:41:54 +00:00
2022-01-05 16:41:17 +00:00
// initialize the branch predictor
2022-04-01 22:14:47 +00:00
if ( `BPRED_ENABLED = = 1 )
2022-01-05 16:41:17 +00:00
initial begin
2022-04-01 22:14:47 +00:00
$readmemb ( `TWO_BIT_PRELOAD , dut . core . ifu . bpred . bpred . Predictor . DirPredictor . PHT . mem ) ;
$readmemb ( `BTB_PRELOAD , dut . core . ifu . bpred . bpred . TargetPredictor . memory . mem ) ;
end
2021-09-13 01:41:54 +00:00
endmodule
2022-04-01 22:14:47 +00:00
module riscvassertions ;
2021-09-13 01:41:54 +00:00
initial begin
2022-01-02 21:47:21 +00:00
assert ( `PMP_ENTRIES = = 0 | `PMP_ENTRIES = = 16 | `PMP_ENTRIES = = 64 ) else $error ( " Illegal number of PMP entries: PMP_ENTRIES must be 0, 16, or 64 " ) ;
2022-04-01 22:14:47 +00:00
assert ( `S_SUPPORTED | `VIRTMEM_SUPPORTED = = 0 ) else $error ( " Virtual memory requires S mode support " ) ;
assert ( `DIV_BITSPERCYCLE = = 1 | `DIV_BITSPERCYCLE = = 2 | `DIV_BITSPERCYCLE = = 4 ) else $error ( " Illegal number of divider bits/cycle: DIV_BITSPERCYCLE must be 1, 2, or 4 " ) ;
assert ( `F_SUPPORTED | ~ `D_SUPPORTED ) else $error ( " Can't support double (D) without supporting float (F) " ) ;
assert ( `I_SUPPORTED ^ `E_SUPPORTED ) else $error ( " Exactly one of I and E must be supported " ) ;
2022-01-02 21:47:21 +00:00
assert ( `XLEN = = 64 | ~ `D_SUPPORTED ) else $error ( " Wally does not yet support D extensions on RV32 " ) ;
2022-02-03 01:08:34 +00:00
assert ( `DCACHE_WAYSIZEINBYTES < = 4096 | ( `DMEM ! = `MEM_CACHE ) | `VIRTMEM_SUPPORTED = = 0 ) else $error ( " DCACHE_WAYSIZEINBYTES cannot exceed 4 KiB when caches and vitual memory is enabled (to prevent aliasing) " ) ;
2022-02-03 00:41:09 +00:00
assert ( `DCACHE_LINELENINBITS > = 128 | ( `DMEM ! = `MEM_CACHE ) ) else $error ( " DCACHE_LINELENINBITS must be at least 128 when caches are enabled " ) ;
2022-01-05 04:08:18 +00:00
assert ( `DCACHE_LINELENINBITS < `DCACHE_WAYSIZEINBYTES * 8 ) else $error ( " DCACHE_LINELENINBITS must be smaller than way size " ) ;
2022-02-03 01:08:34 +00:00
assert ( `ICACHE_WAYSIZEINBYTES < = 4096 | ( `IMEM ! = `MEM_CACHE ) | `VIRTMEM_SUPPORTED = = 0 ) else $error ( " ICACHE_WAYSIZEINBYTES cannot exceed 4 KiB when caches and vitual memory is enabled (to prevent aliasing) " ) ;
2022-02-03 00:41:09 +00:00
assert ( `ICACHE_LINELENINBITS > = 32 | ( `IMEM ! = `MEM_CACHE ) ) else $error ( " ICACHE_LINELENINBITS must be at least 32 when caches are enabled " ) ;
2022-01-05 04:08:18 +00:00
assert ( `ICACHE_LINELENINBITS < `ICACHE_WAYSIZEINBYTES * 8 ) else $error ( " ICACHE_LINELENINBITS must be smaller than way size " ) ;
2022-04-01 22:14:47 +00:00
assert ( 2 * * $clog2 ( `DCACHE_LINELENINBITS ) = = `DCACHE_LINELENINBITS | ( `DMEM ! = `MEM_CACHE ) ) else $error ( " DCACHE_LINELENINBITS must be a power of 2 " ) ;
assert ( 2 * * $clog2 ( `DCACHE_WAYSIZEINBYTES ) = = `DCACHE_WAYSIZEINBYTES | ( `DMEM ! = `MEM_CACHE ) ) else $error ( " DCACHE_WAYSIZEINBYTES must be a power of 2 " ) ;
assert ( 2 * * $clog2 ( `ICACHE_LINELENINBITS ) = = `ICACHE_LINELENINBITS | ( `IMEM ! = `MEM_CACHE ) ) else $error ( " ICACHE_LINELENINBITS must be a power of 2 " ) ;
assert ( 2 * * $clog2 ( `ICACHE_WAYSIZEINBYTES ) = = `ICACHE_WAYSIZEINBYTES | ( `IMEM ! = `MEM_CACHE ) ) else $error ( " ICACHE_WAYSIZEINBYTES must be a power of 2 " ) ;
assert ( 2 * * $clog2 ( `ITLB_ENTRIES ) = = `ITLB_ENTRIES | `VIRTMEM_SUPPORTED = = 0 ) else $error ( " ITLB_ENTRIES must be a power of 2 " ) ;
assert ( 2 * * $clog2 ( `DTLB_ENTRIES ) = = `DTLB_ENTRIES | `VIRTMEM_SUPPORTED = = 0 ) else $error ( " DTLB_ENTRIES must be a power of 2 " ) ;
assert ( `RAM_RANGE > = 56 'h07FFFFFF ) else $warning ( " Some regression tests will fail if RAM_RANGE is less than 56'h07FFFFFF " ) ;
assert ( `ZICSR_SUPPORTED = = 1 | ( `PMP_ENTRIES = = 0 & `VIRTMEM_SUPPORTED = = 0 ) ) else $error ( " PMP_ENTRIES and VIRTMEM_SUPPORTED must be zero if ZICSR not supported. " ) ;
assert ( `ZICSR_SUPPORTED = = 1 | ( `S_SUPPORTED = = 0 & `U_SUPPORTED = = 0 ) ) else $error ( " S and U modes not supported if ZISR not supported " ) ;
assert ( `U_SUPPORTED | ( `S_SUPPORTED = = 0 ) ) else $error ( " S mode only supported if U also is supported " ) ;
// assert (`MEM_DCACHE == 0 | `MEM_DTIM == 0) else $error("Can't simultaneously have a data cache and TIM");
assert ( `DMEM = = `MEM_CACHE | `VIRTMEM_SUPPORTED = = 0 ) else $error ( " Virtual memory needs dcache " ) ;
assert ( `IMEM = = `MEM_CACHE | `VIRTMEM_SUPPORTED = = 0 ) else $error ( " Virtual memory needs icache " ) ;
assert ( `DMEM = = `MEM_CACHE | `DBUS = = 0 ) else $error ( " Dcache rquires DBUS. " ) ;
assert ( `IMEM = = `MEM_CACHE | `IBUS = = 0 ) else $error ( " Icache rquires IBUS. " ) ;
2021-09-13 01:41:54 +00:00
end
endmodule
/* verilator lint_on STMTDLY */
/* verilator lint_on WIDTH */
module DCacheFlushFSM
( input logic clk ,
input logic reset ,
input logic start ,
output logic done ) ;
genvar adr ;
2021-12-14 21:43:06 +00:00
logic [ `XLEN - 1 : 0 ] ShadowRAM [ `RAM_BASE > > ( 1 + `XLEN / 32 ) : ( `RAM_RANGE + `RAM_BASE ) > > 1 + ( `XLEN / 32 ) ] ;
2021-09-13 01:41:54 +00:00
2022-04-01 22:14:47 +00:00
if ( `DMEM = = `MEM_CACHE ) begin
localparam integer numlines = testbench . dut . core . lsu . bus . dcache . dcache . NUMLINES ;
localparam integer numways = testbench . dut . core . lsu . bus . dcache . dcache . NUMWAYS ;
localparam integer linebytelen = testbench . dut . core . lsu . bus . dcache . dcache . LINEBYTELEN ;
localparam integer numwords = testbench . dut . core . lsu . bus . dcache . dcache . LINELEN / `XLEN ;
localparam integer lognumlines = $clog2 ( numlines ) ;
localparam integer loglinebytelen = $clog2 ( linebytelen ) ;
localparam integer lognumways = $clog2 ( numways ) ;
localparam integer tagstart = lognumlines + loglinebytelen ;
genvar index , way , cacheWord ;
logic [ `XLEN - 1 : 0 ] CacheData [ numways - 1 : 0 ] [ numlines - 1 : 0 ] [ numwords - 1 : 0 ] ;
logic [ `XLEN - 1 : 0 ] CacheTag [ numways - 1 : 0 ] [ numlines - 1 : 0 ] [ numwords - 1 : 0 ] ;
logic CacheValid [ numways - 1 : 0 ] [ numlines - 1 : 0 ] [ numwords - 1 : 0 ] ;
logic CacheDirty [ numways - 1 : 0 ] [ numlines - 1 : 0 ] [ numwords - 1 : 0 ] ;
logic [ `PA_BITS - 1 : 0 ] CacheAdr [ numways - 1 : 0 ] [ numlines - 1 : 0 ] [ numwords - 1 : 0 ] ;
for ( index = 0 ; index < numlines ; index + + ) begin
for ( way = 0 ; way < numways ; way + + ) begin
for ( cacheWord = 0 ; cacheWord < numwords ; cacheWord + + ) begin
copyShadow # ( . tagstart ( tagstart ) ,
. loglinebytelen ( loglinebytelen ) )
copyShadow ( . clk ,
. start ,
. tag ( testbench . dut . core . lsu . bus . dcache . dcache . CacheWays [ way ] . CacheTagMem . StoredData [ index ] ) ,
. valid ( testbench . dut . core . lsu . bus . dcache . dcache . CacheWays [ way ] . ValidBits [ index ] ) ,
. dirty ( testbench . dut . core . lsu . bus . dcache . dcache . CacheWays [ way ] . DirtyBits [ index ] ) ,
. data ( testbench . dut . core . lsu . bus . dcache . dcache . CacheWays [ way ] . word [ cacheWord ] . CacheDataMem . StoredData [ index ] ) ,
. index ( index ) ,
. cacheWord ( cacheWord ) ,
. CacheData ( CacheData [ way ] [ index ] [ cacheWord ] ) ,
. CacheAdr ( CacheAdr [ way ] [ index ] [ cacheWord ] ) ,
. CacheTag ( CacheTag [ way ] [ index ] [ cacheWord ] ) ,
. CacheValid ( CacheValid [ way ] [ index ] [ cacheWord ] ) ,
. CacheDirty ( CacheDirty [ way ] [ index ] [ cacheWord ] ) ) ;
end
end
2021-09-13 01:41:54 +00:00
end
2022-04-01 22:14:47 +00:00
integer i , j , k ;
always @ ( posedge clk ) begin
if ( start ) begin # 1
# 1
for ( i = 0 ; i < numlines ; i + + ) begin
for ( j = 0 ; j < numways ; j + + ) begin
for ( k = 0 ; k < numwords ; k + + ) begin
if ( CacheValid [ j ] [ i ] [ k ] & CacheDirty [ j ] [ i ] [ k ] ) begin
ShadowRAM [ CacheAdr [ j ] [ i ] [ k ] > > $clog2 ( `XLEN / 8 ) ] = CacheData [ j ] [ i ] [ k ] ;
end
end
end
end
end
end
2021-09-13 01:41:54 +00:00
end
2022-04-01 22:14:47 +00:00
flop # ( 1 ) doneReg ( . clk , . d ( start ) , . q ( done ) ) ;
2021-09-13 01:41:54 +00:00
endmodule
module copyShadow
2022-01-05 04:08:18 +00:00
# ( parameter tagstart , loglinebytelen )
2021-09-13 01:41:54 +00:00
( input logic clk ,
input logic start ,
input logic [ `PA_BITS - 1 : tagstart ] tag ,
input logic valid , dirty ,
input logic [ `XLEN - 1 : 0 ] data ,
input logic [ 32 - 1 : 0 ] index ,
input logic [ 32 - 1 : 0 ] cacheWord ,
output logic [ `XLEN - 1 : 0 ] CacheData ,
output logic [ `PA_BITS - 1 : 0 ] CacheAdr ,
output logic [ `XLEN - 1 : 0 ] CacheTag ,
output logic CacheValid ,
output logic CacheDirty ) ;
always_ff @ ( posedge clk ) begin
if ( start ) begin
CacheTag = tag ;
CacheValid = valid ;
CacheDirty = dirty ;
CacheData = data ;
2022-01-05 04:08:18 +00:00
CacheAdr = ( tag < < tagstart ) + ( index < < loglinebytelen ) + ( cacheWord < < $clog2 ( `XLEN / 8 ) ) ;
2021-09-13 01:41:54 +00:00
end
end
endmodule