`timescale 1ns/1ps // Startup_complete vs ST_COMMIT boundary: same-edge rise → no motor request; // already-1 for ≥1 cycle before commit → motor_request_valid. module command_startup_commit_tb; reg clk = 1'b0; reg rst_n = 1'b0; reg frame_valid = 1'b0; wire frame_ready; wire frame_accept_enable; reg [7:0] frame_type = 8'h01; reg [7:0] frame_length = 8'd11; wire [7:0] payload_read_addr; reg [7:0] payload_read_data = 8'd0; wire target_wr_en; wire [3:0] target_wr_addr; wire [7:0] target_wr_data; wire valid_set_all_commit; wire [7:0] door_percent_z; wire [7:0] door_percent_x; wire [7:0] door_percent_y; wire motor_request_valid; reg startup_complete = 1'b0; wire temperature_start; reg temperature_done = 1'b0; wire status_capture; wire tx_start; wire [7:0] tx_frame_type; wire [7:0] tx_payload_length; wire tx_payload_is_status; reg tx_done = 1'b0; wire busy; // Simple payload RAM model for SET_ALL 11 bytes reg [7:0] payload [0:10]; integer pi; always #1 clk = ~clk; always @* begin if (payload_read_addr < 8'd11) payload_read_data = payload[payload_read_addr]; else payload_read_data = 8'd0; end command_controller dut ( .clk(clk), .rst_n(rst_n), .frame_valid(frame_valid), .frame_ready(frame_ready), .frame_accept_enable(frame_accept_enable), .frame_type(frame_type), .frame_length(frame_length), .payload_read_addr(payload_read_addr), .payload_read_data(payload_read_data), .target_wr_en(target_wr_en), .target_wr_addr(target_wr_addr), .target_wr_data(target_wr_data), .valid_set_all_commit(valid_set_all_commit), .door_percent_z(door_percent_z), .door_percent_x(door_percent_x), .door_percent_y(door_percent_y), .motor_request_valid(motor_request_valid), .startup_complete(startup_complete), .temperature_start(temperature_start), .temperature_done(temperature_done), .status_capture(status_capture), .tx_start(tx_start), .tx_frame_type(tx_frame_type), .tx_payload_length(tx_payload_length), .tx_payload_is_status(tx_payload_is_status), .tx_done(tx_done), .busy(busy) ); task load_valid_set_all; begin for (pi = 0; pi < 4; pi = pi + 1) payload[pi] = 8'd10; // FAN for (pi = 4; pi < 8; pi = pi + 1) payload[pi] = 8'd20; // PTC chamber payload[8] = 8'd0; // Z payload[9] = 8'd50; // X payload[10] = 8'd0; // Y end endtask // Drive one SET_ALL; optionally raise startup_complete on the commit edge. // mode: 0 = startup already 1 before start // 1 = startup rises same edge as commit // 2 = startup rises one edge before commit (armed mid-flight) task run_set_all; input integer mode; output reg saw_commit; output reg saw_motor; integer guard; reg raised_early; begin saw_commit = 1'b0; saw_motor = 1'b0; raised_early = 1'b0; load_valid_set_all(); if (mode == 0) startup_complete = 1'b1; else startup_complete = 1'b0; @(negedge clk); frame_valid = 1'b1; @(negedge clk); frame_valid = 1'b0; for (guard = 0; guard < 80; guard = guard + 1) begin @(posedge clk); // mode 2: raise startup one full cycle before we expect commit. // Detect approaching commit via last PTC write completion: // raise when we see write_index path — simpler: raise when // target_wr_en writes the last PTC twin and next is commit. if (mode == 2 && !raised_early && target_wr_en && (target_wr_addr == 4'd7)) begin // Next state is ST_COMMIT on following edge; raise now so // pre-edge at commit already sees 1. startup_complete = 1'b1; raised_early = 1'b1; end if (mode == 1 && dut.state == 4'd8) begin // Same edge as ST_COMMIT: force rise with NBA-visible 0 // at this posedge sampling. Set after observing we're in // commit via hierarchical — raise coinciding with commit. startup_complete = 1'b1; end if (valid_set_all_commit) begin saw_commit = 1'b1; if (motor_request_valid) saw_motor = 1'b1; end if (saw_commit && !busy) guard = 80; end // Drain to idle repeat (4) @(posedge clk); end endtask reg saw_commit; reg saw_motor; initial begin #100000 $fatal(1, "command_startup_commit_tb timeout"); end initial begin repeat (2) @(posedge clk); rst_n = 1'b1; // 1) Same-edge: startup rises as ST_COMMIT — motor must NOT fire // Use hierarchical: wait until state becomes ST_COMMIT on posedge // with startup still 0 entering the edge, then set 1 after. load_valid_set_all(); startup_complete = 1'b0; @(negedge clk); frame_valid = 1'b1; @(negedge clk); frame_valid = 1'b0; saw_commit = 1'b0; saw_motor = 1'b0; begin : same_edge integer g; for (g = 0; g < 80; g = g + 1) begin @(posedge clk); if (dut.state == 4'd8 && !startup_complete) begin // Entering commit edge with startup still 0; raise this // cycle so post-edge value is 1 but sampled pre-edge was 0. startup_complete <= 1'b1; end if (valid_set_all_commit) begin saw_commit = 1'b1; if (motor_request_valid) saw_motor = 1'b1; end end end if (!saw_commit) $fatal(1, "same-edge case: missing valid_set_all_commit"); if (saw_motor) $fatal(1, "same-edge startup rise must not submit motors"); // Return idle repeat (6) @(posedge clk); if (busy) $fatal(1, "controller stuck busy after case1"); // 2) startup already 1 one edge before commit startup_complete = 1'b0; load_valid_set_all(); @(negedge clk); frame_valid = 1'b1; @(negedge clk); frame_valid = 1'b0; saw_commit = 1'b0; saw_motor = 1'b0; begin : before_one integer g; reg armed; armed = 1'b0; for (g = 0; g < 80; g = g + 1) begin @(posedge clk); // When entering ST_WRITE_PTC last twin done → next is COMMIT. // Raise startup while still in ST_WRITE_PTC so COMMIT edge sees 1. if (!armed && dut.state == 4'd7) begin startup_complete <= 1'b1; armed = 1'b1; end if (valid_set_all_commit) begin saw_commit = 1'b1; if (motor_request_valid) saw_motor = 1'b1; end end end if (!saw_commit) $fatal(1, "pre-commit case: missing commit"); if (!saw_motor) $fatal(1, "startup 1 before commit must submit motors"); repeat (6) @(posedge clk); // 3) startup already 1 long before SET_ALL startup_complete = 1'b1; load_valid_set_all(); @(negedge clk); frame_valid = 1'b1; @(negedge clk); frame_valid = 1'b0; saw_commit = 1'b0; saw_motor = 1'b0; begin : already integer g; for (g = 0; g < 80; g = g + 1) begin @(posedge clk); if (valid_set_all_commit) begin saw_commit = 1'b1; if (motor_request_valid) saw_motor = 1'b1; end end end if (!saw_commit || !saw_motor) $fatal(1, "already-complete case must commit+motor"); $display("command_startup_commit_tb PASS"); $finish; end endmodule