// Check tape bounds and single-step completion for scanning and input. `timescale 1ns/1ps `default_nettype none module tb_step; reg clk = 0, rst_n = 1, run = 0, step = 0, in_valid = 0; reg [7:0] in_data = 8'h41; reg [7:0] length; reg [7:0] rom [0:255]; wire [7:0] address, ip; wire ready, halted, out_valid; wire [7:0] out_data; wire [31:0] steps; integer i; rot8 #(.IPW(8), .EPROM(1)) dut ( .clk(clk), .rst_n(rst_n), .run(run), .step(step), .prog_addr0(address), .prog_addr1(), .prog_data0(rom[address]), .prog_data1(8'd0), .prog_len(length), .in_valid(in_valid), .in_data(in_data), .in_ready(ready), .out_valid(out_valid), .out_data(out_data), .ip(ip), .dp(), .steps(steps), .halted(halted)); always #5 clk = ~clk; task reset; begin rst_n = 0; @(negedge clk); if (ready !== 0) $fatal(1, "Input advertised while in reset"); rst_n = 1; end endtask task request_step; begin step = 1; @(negedge clk); step = 0; end endtask initial begin // A ',' outside the image must not advertise a transfer. length = 1; rom[0] = 0; rom[1] = 7; run = 1; reset; @(negedge clk); if (ip !== 1 || ready !== 0) $fatal(1, "Invalid input handshake past program end"); @(negedge clk); if (halted !== 1) $fatal(1, "Program failed to halt"); // The scanner must never consume a matching bracket outside the image. rom[0] = 4; rom[1] = 5; reset; repeat (2) @(negedge clk); if (ip !== 1 || halted !== 1 || dut.state !== dut.S_HALT) $fatal(1, "Scanner ran past program end"); // A skipped loop can nest deeper than the active loop stack. run = 0; length = 81; for (i = 0; i < 40; i = i + 1) begin rom[i] = 4; rom[40 + i] = 5; end rom[80] = 6; reset; request_step; repeat (82) @(negedge clk); if (ip !== 80 || steps !== 1 || halted !== 0 || out_valid !== 0) $fatal(1, "Single step failed to finish the skip and pause"); request_step; if (out_valid !== 1 || steps !== 2) $fatal(1, "The next step failed to execute OUT"); // An input instruction can receive its byte after the step pulse ends. length = 2; rom[0] = 7; rom[1] = 6; reset; request_step; repeat (3) @(negedge clk); if (ready !== 1 || ip !== 0 || steps !== 0) $fatal(1, "Single-step input stopped waiting too early"); in_valid = 1; @(negedge clk); in_valid = 0; repeat (3) @(negedge clk); if (ready !== 0 || ip !== 1 || steps !== 1 || out_valid !== 0) $fatal(1, "Single-step input failed to store the byte and pause"); request_step; if (out_valid !== 1 || out_data !== 8'h41 || steps !== 2) $fatal(1, "OUT did not return the single-step input byte"); $display("Single-step and tape boundary checks passed"); $finish; end endmodule `default_nettype wire