Files

125 lines
4.1 KiB
Verilog

// Count cycles in each RTL state and track the peak loop and scan depths.
// Compile with -DEPROM_MODE for one-instruction-per-byte images.
// Use tests/rtl_probe.sh states <image.hex> <instruction count> [input.txt] [eprom].
`timescale 1ns/1ps
`default_nettype none
module tb_state_mix;
reg clk = 0, rst_n = 0, run = 1, step = 0;
reg in_valid = 0;
reg [7:0] in_data = 0;
reg [15:0] length = 0;
wire [15:0] prog_addr0, prog_addr1;
wire [7:0] prog_data0, prog_data1;
wire in_ready, out_valid, halted;
wire [7:0] out_data;
wire [15:0] ip;
wire [5:0] dp;
wire [31:0] steps;
reg [7:0] mem [0:65535];
reg [1023:0] hex_path, in_path;
integer hex_fd;
integer unsigned requested_length, image_bytes;
integer in_fd = 0;
integer next_char, i;
integer cycles = 0, c_fetch = 0, c_scan = 0, c_halt = 0;
integer n_out = 0, stalls = 0, max_sp = 0, max_scan = 0;
reg in_eof = 0;
`ifdef EPROM_MODE
localparam integer EPROM = 1;
`else
localparam integer EPROM = 0;
`endif
rot8 #(.WIDTH(8), .RING(64), .IPW(16), .STACKW(4), .EPROM(EPROM)) dut (
.clk(clk), .rst_n(rst_n), .run(run), .step(step),
.prog_addr0(prog_addr0), .prog_addr1(prog_addr1),
.prog_data0(prog_data0), .prog_data1(prog_data1), .prog_len(length),
.in_valid(in_valid), .in_data(in_data), .in_ready(in_ready),
.out_valid(out_valid), .out_data(out_data),
.ip(ip), .dp(dp), .steps(steps), .halted(halted));
assign prog_data0 = mem[prog_addr0];
assign prog_data1 = mem[prog_addr1];
always #5 clk = ~clk;
task take_char;
begin
next_char = $fgetc(in_fd);
if (next_char < 0) begin
in_eof <= 1'b1;
in_data <= 8'd0;
end else
in_data <= next_char[7:0];
end
endtask
initial begin
if (!$value$plusargs("HEX=%s", hex_path))
$fatal(1, "Pass +HEX=<image>");
if (!$value$plusargs("LEN=%d", requested_length))
$fatal(1, "Pass +LEN=<instruction count>");
if (requested_length > (1 << 16) - 1)
$fatal(1, "Instruction count is too large: %0d", requested_length);
length = requested_length;
image_bytes = EPROM ? requested_length : (3 * requested_length + 7) / 8;
hex_fd = $fopen(hex_path, "rb");
if (hex_fd == 0)
$fatal(1, "Cannot open program image %0s", hex_path);
$fclose(hex_fd);
if (image_bytes != 0) begin
$readmemh(hex_path, mem, 0, image_bytes - 1);
for (i = 0; i < image_bytes; i = i + 1)
if ((^mem[i]) === 1'bx)
$fatal(1, "Missing or invalid byte at offset %0d in %0s", i, hex_path);
end
// A packed fetch can read one byte past the image for its fetch window.
mem[image_bytes] = 8'h00;
if ($value$plusargs("INFILE=%s", in_path)) begin
in_fd = $fopen(in_path, "rb");
if (in_fd == 0)
$fatal(1, "Cannot open input file %0s", in_path);
end
#20 rst_n = 1;
if (in_fd != 0) begin
in_valid = 1'b1;
take_char;
end
while (!halted && cycles < 200000000) begin
@(posedge clk);
cycles = cycles + 1;
case (dut.state)
2'd0: c_fetch = c_fetch + 1;
2'd1: c_scan = c_scan + 1;
2'd2: c_halt = c_halt + 1;
endcase
if (dut.sp > max_sp) max_sp = dut.sp;
if (dut.scan_depth > max_scan) max_scan = dut.scan_depth;
if (in_ready && !in_valid) stalls = stalls + 1;
if (in_ready && in_valid && in_fd != 0 && !in_eof) take_char;
if (out_valid) n_out = n_out + 1;
end
if (!halted)
$fatal(1, "No halt after %0d cycles: ip %0d dp %0d, %0d input stalls",
cycles, ip, dp, stalls);
$display("%s", EPROM ? "[EPROM 1 byte/instruction]" : "[packed 3 bit/instruction]");
$display("steps=%0d cycles=%0d outs=%0d stalls=%0d", steps, cycles, n_out, stalls);
if (cycles > 0)
$display(" S_FETCH=%0d (%0d%%) S_SCAN=%0d (%0d%%) S_HALT=%0d",
c_fetch, (64'd100*c_fetch)/cycles, c_scan, (64'd100*c_scan)/cycles, c_halt);
if (steps > 0)
$display(" cycles/step = %0d.%02d, max stack depth = %0d, max scan depth = %0d",
cycles/steps, (64'd100*(cycles%steps))/steps, max_sp, max_scan);
$finish;
end
endmodule
`default_nettype wire