`timescale 1ns/1ps // Unit testbench for duty_register_bank, watchdog_30s and get_data_6400 // with independent expected values. module final_core_units_tb; reg clk; reg rst_n; initial begin clk = 0; forever #20 clk = ~clk; // 40 ns period; functional timing only end integer failures; reg [7:0] expected; // ---------- duty_register_bank ---------- reg [4:0] pwm_addr; wire [7:0] pwm_duty; reg stage_we; reg [4:0] stage_addr; reg [7:0] stage_data; reg commit; reg invalidate; wire bank_valid; wire bank_active; duty_register_bank #(.CHANNEL_COUNT(32), .ADDR_WIDTH(5), .DEFAULT_PTC_MASK(32'h00FFFFFF)) u_bank ( .clk(clk), .rst_n(rst_n), .pwm_addr(pwm_addr), .pwm_duty(pwm_duty), .stage_we(stage_we), .stage_addr(stage_addr), .stage_data(stage_data), .commit(commit), .invalidate(invalidate), .valid(bank_valid), .active_bank(bank_active) ); // ---------- watchdog_30s (short timeout) ---------- reg arm; reg reload; wire expired; wire counting; watchdog_30s #(.TIMEOUT_CLKS(20)) u_wd ( .clk(clk), .rst_n(rst_n), .arm(arm), .reload(reload), .expired(expired), .counting(counting) ); // ---------- get_data_6400 ---------- reg gd_start; wire gd_ready; wire [4:0] gd_bank_addr; reg [7:0] gd_bank_duty; wire [5:0] gd_temp_addr; wire [7:0] gd_temp_data; wire gd_temp_valid; wire gd_we; wire [7:0] gd_addr; wire [7:0] gd_data; // Production temperature RAM: the assembler is proven against the // real synchronous model, pre-written with independently computed bytes. reg tw_en; reg [5:0] tw_addr; reg [7:0] tw_data; temp_ram u_temp ( .clk(clk), .rst_n(rst_n), .wr_en(tw_en), .wr_addr(tw_addr), .wr_data(tw_data), .rd_addr(gd_temp_addr), .rd_data(gd_temp_data), .rd_valid(gd_temp_valid) ); // Registered bank answer model: matches the real duty_register_bank // (BRAM registered output + combinational select = lag-1). reg [7:0] gd_bank_q; always @(posedge clk) gd_bank_q <= 8'd60 + gd_bank_addr; get_data_6400 u_gd ( .clk(clk), .rst_n(rst_n), .start(gd_start), .ready(gd_ready), .bank_addr(gd_bank_addr), .bank_duty(gd_bank_q), .temp_addr(gd_temp_addr), .temp_data(gd_temp_data), .temp_valid(gd_temp_valid), .reply_we(gd_we), .reply_addr(gd_addr), .reply_data(gd_data) ); reg [7:0] captured [0:82]; integer i; task check; input [7:0] got; input [7:0] want; input [255:0] name; begin if (got !== want) begin $display("FAIL %0s: got %0d want %0d", name, got, want); failures = failures + 1; end end endtask initial begin failures = 0; rst_n = 0; pwm_addr = 0; stage_we = 0; stage_addr = 0; stage_data = 0; commit = 0; invalidate = 0; arm = 0; reload = 0; gd_start = 0; tw_en = 0; tw_addr = 0; tw_data = 0; repeat (4) @(posedge clk); rst_n = 1; repeat (2) @(posedge clk); // --- bank: defaults presented while invalid --- pwm_addr = 5'd0; repeat (3) @(posedge clk); check(pwm_duty, 8'd0, "default ptc0"); pwm_addr = 5'd24; repeat (3) @(posedge clk); check(pwm_duty, 8'd20, "default front0"); if (bank_valid !== 1'b0) begin $display("FAIL valid should be 0 after reset"); failures = failures + 1; end // --- stage 32 values, commit, read back exact duty --- // Hold each byte across its sampling edge: present on the falling // edge so the synchronous BRAM sees stable address/data at posedge. for (i = 0; i < 32; i = i + 1) begin @(negedge clk); stage_we = 1; stage_addr = i[4:0]; stage_data = 8'd60 + i[7:0]; end @(negedge clk); stage_we = 0; @(negedge clk); commit = 1; @(posedge clk); #1; commit = 0; @(negedge clk); stage_we = 0; stage_addr = 5'd0; stage_data = 8'd0; pwm_addr = 5'd0; repeat (4) @(posedge clk); if (bank_valid !== 1'b1) begin $display("FAIL valid should be 1 after commit"); failures = failures + 1; end // The address is a registered input; the duty is valid on the // cycle after a full address-stable clock. Prime the address, // wait one full clock, then sample one channel per cycle. // The duty is the registered read result of the address present // at the last posedge, so the sample for channel i is taken // after presenting channel i+1 (one pipeline beat behind). // Prime the address, then sample one channel per cycle with the // address held across each sampling edge. // The duty is the registered read result of the address present // at the last posedge: sample channel i after presenting i+1. // The duty is the registered read result of the address present // at the last posedge: hold address i across its sampling edge, // then sample. Present the first address before the loop. // Sample channel i while presenting channel i: the negedge // presents the address, the posedge samples the settled beat. for (i = 0; i < 32; i = i + 1) begin @(negedge clk); pwm_addr = i[4:0]; @(posedge clk); @(posedge clk); #1; expected = 8'd60 + i[7:0]; check(pwm_duty, expected, "bank value"); end // --- invalidate restores defaults, bank content is kept but hidden --- invalidate = 1; @(posedge clk); invalidate = 0; repeat (3) @(posedge clk); pwm_addr = 5'd5; repeat (3) @(posedge clk); check(pwm_duty, 8'd0, "expiry ptc5"); // --- watchdog: arm then expire after 20 sampling edges --- arm = 1; @(posedge clk); #1; arm = 0; if (counting !== 1'b1) begin $display("FAIL watchdog should count after arm"); failures = failures + 1; end repeat (20) @(posedge clk); #1; if (expired !== 1'b1) begin $display("FAIL watchdog should expire after 20 clocks"); failures = failures + 1; end @(posedge clk); #1; if (expired !== 1'b0) begin $display("FAIL expiry pulse must be one cycle"); failures = failures + 1; end if (counting !== 1'b0) begin $display("FAIL watchdog should stop after expiry"); failures = failures + 1; end // reload wins over expiry on the same edge: reload at terminal count. // The counter was already fixed to fire exactly 20 edges after arm // (off-by-one repair), so the terminal count is reached after 19 // more edges here. arm = 1; @(posedge clk); #1; arm = 0; repeat (19) @(posedge clk); #1; reload = 1; @(posedge clk); #1; reload = 0; repeat (2) @(posedge clk); #1; if (expired !== 1'b0) begin $display("FAIL reload must suppress expiry at terminal count"); failures = failures + 1; end if (counting !== 1'b1) begin $display("FAIL watchdog should keep counting after reload"); failures = failures + 1; end repeat (18) @(posedge clk); #1; if (expired !== 1'b1) begin $display("FAIL watchdog should expire 20 clocks after reload"); failures = failures + 1; end // --- get_data_6400: 83-byte assembly against the real temp RAM --- // bank presents committed values (independently known: 60+i) // Pre-write the production temp RAM with independently computed bytes: // BMC channel i = 100+i, CT channel i = (i<24 ? i : i+8)+100. for (i = 0; i < 24; i = i + 1) begin @(negedge clk); tw_en = 1; tw_addr = i[5:0]; tw_data = i[7:0] + 8'd100; end for (i = 0; i < 27; i = i + 1) begin @(negedge clk); tw_en = 1; tw_addr = i[5:0] + 6'd32; tw_data = ((i < 24) ? i : (i + 8)) + 100; end @(negedge clk); tw_en = 0; gd_start = 1; @(posedge clk); #1; gd_start = 0; // feed sources every cycle: bank model answers next cycle, temperature // answers with a two-cycle lag so the assembler must wait on valid // The bank model answers with the registered synchronous-RAM // contract: the duty for the address present at the last edge. // Both sources answer with the registered synchronous-RAM // contract: the value for the address present at the last edge. // Drive them from one synchronous model AFTER the clock so the // assembler (which samples at the edge) sees last-edge values. for (i = 0; i < 900; i = i + 1) begin @(posedge clk); #1; if (gd_we) captured[gd_addr] = gd_data; if (gd_ready) begin // Sample the staged reply on the SAME edge as ready: the // blocking-assignment capture above already took it. i = 100000; end end if (gd_ready !== 1'b1) begin $display("FAIL get_data ready never fired"); failures = failures + 1; end for (i = 0; i < 32; i = i + 1) begin expected = 8'd60 + i[7:0]; check(captured[i], expected, "get ptc/front/rear"); end for (i = 0; i < 24; i = i + 1) begin expected = i[7:0] + 8'd100; check(captured[32 + i], expected, "get bmc"); end for (i = 0; i < 27; i = i + 1) begin expected = ((i < 24) ? i : (i + 8)) + 100; check(captured[56 + i], expected[7:0], "get ct"); end if (failures == 0) $display("final_core_units_tb PASS"); else $display("final_core_units_tb FAIL (%0d)", failures); if (failures != 0) $finish(1); $finish; end endmodule