// ----------------------------------------------------------------------------- // tb_z2_demo.v -- Self-checking testbench for z2_demo_top // // Runs unchanged in three environments: // 1. Local event simulation (iverilog / xrun) -- logic verification // 2. Palladium Z2 SA mode: this TB stays on the host (Xcelium side), // the DUT is mapped into the emulator by IXCOM (+dut+z2_demo_top) // 3. Palladium Z2 ICE mode is NOT covered by this TB (no software TB there) // // The TB prints one "CHECK ..." line per check and finishes with // "TEST PASSED" or "TEST FAILED" so scripts can grep the log. // ----------------------------------------------------------------------------- `timescale 1ns/1ps module tb_z2_demo; reg clk; reg rst_n; reg [7:0] paddr; reg psel; reg penable; reg pwrite; reg [31:0] pwdata; wire [31:0] prdata; wire pready; wire [7:0] gpio_out; wire irq; wire cmp_hit; integer errors; integer i; reg [31:0] rdata; reg [31:0] count_a, count_b; reg saw_irq; z2_demo_top dut ( .clk (clk), .rst_n (rst_n), .paddr (paddr), .psel (psel), .penable (penable), .pwrite (pwrite), .pwdata (pwdata), .prdata (prdata), .pready (pready), .gpio_out (gpio_out), .irq (irq), .cmp_hit (cmp_hit) ); // 100 MHz clock initial clk = 1'b0; always #5 clk = ~clk; // ------------------------------------------------------------- APB tasks task apb_write; input [7:0] addr; input [31:0] data; begin @(negedge clk); paddr = addr; pwdata = data; pwrite = 1'b1; psel = 1'b1; penable = 1'b1; @(negedge clk); psel = 1'b0; penable = 1'b0; pwrite = 1'b0; end endtask task apb_read; input [7:0] addr; output [31:0] data; begin @(negedge clk); paddr = addr; pwrite = 1'b0; psel = 1'b1; penable = 1'b1; #1 data = prdata; // combinational read data @(negedge clk); psel = 1'b0; penable = 1'b0; end endtask task check; input [31:0] got; input [31:0] exp; input [8*40-1:0] name; begin if (got === exp) begin $display("CHECK OK : %0s = 0x%08h", name, got); end else begin $display("CHECK FAIL : %0s got 0x%08h, expected 0x%08h", name, got, exp); errors = errors + 1; end end endtask // ------------------------------------------------------------- stimulus initial begin `ifdef LOCAL_SIM // Waveform dump is a local-simulation debug aid only; it is compiled // out of the IXCOM build (run_local.sh defines LOCAL_SIM). $dumpfile("z2_demo.vcd"); $dumpvars(0, tb_z2_demo); `endif errors = 0; rst_n = 1'b0; paddr = 8'd0; psel = 1'b0; penable = 1'b0; pwrite = 1'b0; pwdata = 32'd0; repeat (4) @(negedge clk); rst_n = 1'b1; repeat (2) @(negedge clk); $display("---- z2_demo: reset released ----"); // 1) ID register apb_read(8'h18, rdata); check(rdata, 32'h5A32_0001, "ID register"); // 2) Default LOAD after reset = 9 apb_read(8'h04, rdata); check(rdata, 32'd9, "LOAD default"); // 3) Program the timer: period 20 cycles, compare at 5 apb_write(8'h04, 32'd19); // LOAD = 19 (also presets COUNT) apb_write(8'h0C, 32'd5); // CMP = 5 apb_read (8'h04, rdata); check(rdata, 32'd19, "LOAD programmed"); // 4) Enable timer + irq + pattern rotation (CTRL = 0b111) apb_write(8'h00, 32'd7); // 5) Wait for the timeout interrupt (COUNT: 19 -> 0) saw_irq = 1'b0; for (i = 0; i < 100 && !saw_irq; i = i + 1) begin @(negedge clk); if (irq) saw_irq = 1'b1; end check({31'd0, saw_irq}, 32'd1, "irq raised on timeout"); // 6) STATUS: both timeout and compare flags must be set by now apb_read(8'h10, rdata); check({30'd0, rdata[1:0]}, 32'd3, "STATUS timeout+cmp flags"); // 7) Pattern rotated once: GPIO 0x01 -> 0x02 apb_read(8'h14, rdata); check(rdata, 32'h0000_0002, "GPIO pattern rotated"); // 8) Stop the timer, W1C the flags, verify they stay cleared apb_write(8'h00, 32'd0); // CTRL = 0 (disable) apb_write(8'h10, 32'd3); // clear both flags apb_read (8'h10, rdata); check(rdata, 32'd0, "STATUS cleared by W1C"); // 9) With enable=0 the counter must be frozen apb_read(8'h08, count_a); repeat (5) @(negedge clk); apb_read(8'h08, count_b); check(count_b, count_a, "COUNT frozen when disabled"); // ------------------------------------------------------------ verdict if (errors == 0) $display("TEST PASSED (all checks)"); else $display("TEST FAILED (%0d check(s) failed)", errors); $finish; end // global watchdog so a hung run still terminates with a failure initial begin #100000; $display("TEST FAILED (watchdog timeout)"); $finish; end endmodule