Files

103 lines
3.0 KiB
Verilog

// Verify input handshaking while paused, running, and halted.
// The test runs an in-memory tape: ',' opcode 7 followed by '.' opcode 6.
// Both opcodes fit in the first packed byte, 0x37.
`timescale 1ns/1ps
`default_nettype none
module tb_in_ready;
reg clk = 0, rst_n = 0, run = 0, step = 0;
reg in_valid = 0;
reg [7:0] in_data = 0;
reg [15:0] prog_len = 2;
wire [15:0] prog_addr0, prog_addr1;
reg [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:63];
integer errors = 0;
rot8 #(.WIDTH(8), .RING(64), .IPW(16), .STACKW(4)) 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));
always @(*) begin
prog_data0 = mem[prog_addr0[5:0]];
prog_data1 = mem[prog_addr1[5:0]];
end
always #5 clk = ~clk;
task expect_ready(input exp, input got);
begin
if (exp !== got) begin
$display(" expected %0b got %0b -> FAIL", exp, got);
errors = errors + 1;
end else
$display(" got %0b -> ok", got);
end
endtask
initial begin
mem[0] = 8'h37;
mem[1] = 8'h00;
rst_n = 0; #20 rst_n = 1; #20;
$display("1. Ready stays low while paused");
run = 0; step = 0; #30;
expect_ready(1'b0, in_ready);
$display("2. A valid pulse while paused must leave the cell unchanged");
in_valid = 1; in_data = 8'h41; #10;
expect_ready(1'b0, in_ready);
in_valid = 0; #20;
if (dut.cells[0] !== 8'h00) begin
$display(" cell0 = %02x -> FAIL (unexpected input transfer)", dut.cells[0]);
errors = errors + 1;
end else
$display(" cell0 remains 00 -> ok");
$display("3. Pending input is accepted when execution resumes");
in_valid = 1; in_data = 8'h43; #20;
run = 1; #45;
if (dut.cells[0] !== 8'h43) begin
$display(" cell0 = %02x -> FAIL", dut.cells[0]);
errors = errors + 1;
end else
$display(" cell0 = 43 -> ok");
$display("4. A running input instruction asserts ready and stores the byte");
rst_n = 0; #20 rst_n = 1; run = 1; in_valid = 0; #25;
expect_ready(1'b1, in_ready);
in_valid = 1; in_data = 8'h42; #10 in_valid = 0; #25;
if (dut.cells[0] !== 8'h42) begin
$display(" cell0 = %02x -> FAIL", dut.cells[0]);
errors = errors + 1;
end else
$display(" cell0 = 42 -> ok");
$display("5. Ready stays low after halting");
prog_len = 0; #40;
if (!halted) begin
$display(" Expected the machine to halt -> FAIL"); errors = errors + 1;
end
expect_ready(1'b0, in_ready);
if (errors != 0)
$fatal(1, "%0d input handshake checks failed", errors);
$display("ALL OK");
$finish;
end
endmodule
`default_nettype wire