gary/lib/Testing.bsv

82 lines
1.9 KiB
Plaintext
Raw Normal View History

package Testing;
import Assert::*;
import StmtFSM::*;
2024-08-14 05:53:47 +02:00
import Cntrs::*;
2024-08-14 05:53:47 +02:00
// A CycleCounter keeps a count of total elapsed cycles in a
// simulation, as well as cycles since the last reset.
interface CycleCounter;
// _read returns the number of cycles since the last time reset()
// was called.
method Bit#(32) _read();
// reset resets the cycle counter. The counter will read 1 on the
// cycle following the call to reset.
method Action reset();
2024-08-14 05:53:47 +02:00
// all returns the number of cycles elapsed since simulation start,
// for use in $display and the like.
method Bit#(32) all();
endinterface
2024-08-14 05:53:47 +02:00
module mkCycleCounter(CycleCounter);
let total <- mkCount(0);
let cnt <- mkCount(0);
(* no_implicit_conditions, fire_when_enabled *)
rule count_up;
2024-08-14 05:53:47 +02:00
cnt.incr(1);
total.incr(1);
endrule
2024-08-14 05:53:47 +02:00
method _read = cnt._read;
method Action reset();
cnt.update(0);
endmethod
2024-08-14 05:53:47 +02:00
method all = total._read;
endmodule
interface TestFlags;
method Bool verbose();
endinterface
module mkTestFlags(TestFlags);
Wire#(Bool) verbose_val <- mkBypassWire();
rule every;
let v <- $test$plusargs("v");
verbose_val <= v;
endrule
method verbose = verbose_val;
endmodule
2024-08-14 05:53:47 +02:00
// mkTest runs the given test, printing status text before and after
// the run. Tests can be nested.
function Stmt mkTest(String name, Stmt test);
seq
$display("RUN %s", name);
test;
$display("OK %s", name);
endseq;
endfunction
// runTest runs the given test with a timeout.
module runTest(Integer cycle_limit, Stmt test, Empty ifc);
Bit#(32) max = fromInteger(cycle_limit);
let cnt <- mkCount(0);
(* no_implicit_conditions, fire_when_enabled *)
rule cycle_deadline;
dynamicAssert(cnt < max, "Test timed out");
cnt.incr(1);
endrule
mkAutoFSM(seq
2024-08-14 05:53:47 +02:00
$display("Running test with %0d cycle timeout", max);
test;
endseq);
endmodule
endpackage