143 lines
4.1 KiB
Verilog
143 lines
4.1 KiB
Verilog
// Run a program image and print the OUT bytes and step count for comparison
|
|
// with r8forge_tape_run --loop-stack --retest. See rtl/README.md for image formats.
|
|
`timescale 1ns/1ps
|
|
`default_nettype none
|
|
|
|
module tb_rot8;
|
|
|
|
parameter integer MEMW = 16; // program memory address bits
|
|
parameter integer MAX_OUT = 4096;
|
|
|
|
reg clk = 0;
|
|
reg rst_n = 0;
|
|
reg run = 1;
|
|
reg step = 0;
|
|
reg in_valid = 0;
|
|
reg [7:0] in_data = 0;
|
|
|
|
wire [MEMW-1:0] prog_addr0, prog_addr1;
|
|
wire [7:0] prog_data0, prog_data1;
|
|
wire [MEMW-1:0] prog_len;
|
|
wire out_valid;
|
|
wire [7:0] out_data;
|
|
wire in_ready;
|
|
wire [MEMW-1:0] ip;
|
|
wire [5:0] dp;
|
|
wire [31:0] steps;
|
|
wire halted;
|
|
|
|
reg [7:0] mem [0:(1<<MEMW)-1];
|
|
reg [MEMW-1:0] length;
|
|
integer outs [0:MAX_OUT-1];
|
|
integer n_out = 0;
|
|
integer cycles = 0;
|
|
integer stalls = 0;
|
|
integer i;
|
|
|
|
reg [1023:0] hex_path;
|
|
reg [1023:0] in_path;
|
|
integer hex_fd;
|
|
integer unsigned requested_length, image_bytes;
|
|
integer in_fd = 0;
|
|
reg in_eof = 0;
|
|
integer next_char;
|
|
|
|
assign prog_data0 = mem[prog_addr0];
|
|
assign prog_data1 = mem[prog_addr1];
|
|
assign prog_len = length;
|
|
|
|
// Feed one byte per accepted ','. Exhausted input reads as zero.
|
|
// Without an input file, ',' waits until the testbench reaches its cycle limit.
|
|
task take_char;
|
|
begin
|
|
next_char = $fgetc(in_fd);
|
|
if (next_char < 0) begin
|
|
in_eof <= 1'b1;
|
|
in_data <= 8'd0;
|
|
end else begin
|
|
in_data <= next_char[7:0];
|
|
end
|
|
end
|
|
endtask
|
|
|
|
// In EPROM_MODE, supply an image generated with --eprom --hex.
|
|
`ifdef EPROM_MODE
|
|
localparam integer EPROM = 1;
|
|
`else
|
|
localparam integer EPROM = 0;
|
|
`endif
|
|
|
|
rot8 #(.WIDTH(8), .RING(64), .IPW(MEMW), .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(prog_len),
|
|
.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));
|
|
|
|
// The simulation clock lets us count cycles separately from instructions.
|
|
always #5 clk = ~clk;
|
|
|
|
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 << MEMW) - 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 < 100000000) begin
|
|
@(posedge clk);
|
|
cycles = cycles + 1;
|
|
if (in_ready && !in_valid)
|
|
stalls = stalls + 1;
|
|
if (in_ready && in_valid && in_fd != 0 && !in_eof)
|
|
take_char;
|
|
if (out_valid) begin
|
|
if (n_out == MAX_OUT)
|
|
$fatal(1, "OUT stream exceeds the %0d-byte buffer", MAX_OUT);
|
|
outs[n_out] = out_data;
|
|
n_out = n_out + 1;
|
|
end
|
|
end
|
|
|
|
if (!halted)
|
|
$fatal(1, "No halt after %0d cycles: ip %0d dp %0d, %0d input stalls",
|
|
cycles, ip, dp, stalls);
|
|
|
|
$write("out");
|
|
for (i = 0; i < n_out; i = i + 1)
|
|
$write(" %0d", outs[i]);
|
|
$display(" (%0d steps, %0d cycles, %0d stalled on input)", steps, cycles, stalls);
|
|
$finish;
|
|
end
|
|
|
|
endmodule
|
|
|
|
`default_nettype wire
|