| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297 |
- `timescale 1ns/1ps
- module prod2u_p4_normal_v3_output_state_tb;
- reg clk;
- reg rst_n;
- reg force_safe;
- reg snapshot_valid;
- wire snapshot_ready;
- reg [271:0] snapshot_payload;
- reg commit_authorize;
- wire result_valid;
- wire result_accepted;
- wire commit_pulse;
- wire [191:0] ptc_percent;
- wire [31:0] fan_percent;
- wire [7:0] solenoid_percent;
- wire [15:0] state_hash16;
- reg [191:0] held_ptc;
- reg [31:0] held_fan;
- reg [7:0] held_solenoid;
- reg [15:0] held_hash;
- reg [15:0] expected_hash;
- integer i;
- integer wait_cycles;
- prod2u_p4_normal_v3_output_state dut (
- .clk(clk),
- .rst_n(rst_n),
- .force_safe(force_safe),
- .snapshot_valid(snapshot_valid),
- .snapshot_ready(snapshot_ready),
- .snapshot_payload(snapshot_payload),
- .commit_authorize(commit_authorize),
- .result_valid(result_valid),
- .result_accepted(result_accepted),
- .commit_pulse(commit_pulse),
- .ptc_percent(ptc_percent),
- .fan_percent(fan_percent),
- .solenoid_percent(solenoid_percent),
- .state_hash16(state_hash16)
- );
- initial begin
- clk = 1'b0;
- forever #5 clk = ~clk;
- end
- function [15:0] crc16_byte;
- input [15:0] crc_in;
- input [7:0] data;
- reg [15:0] value;
- integer bit_index;
- begin
- value = crc_in ^ {data, 8'h00};
- for (bit_index = 0; bit_index < 8; bit_index = bit_index + 1) begin
- if (value[15]) begin
- value = (value << 1) ^ 16'h1021;
- end else begin
- value = value << 1;
- end
- end
- crc16_byte = value;
- end
- endfunction
- task build_valid_snapshot;
- reg [15:0] crc;
- begin
- snapshot_payload = 272'd0;
- snapshot_payload[7:0] = 8'h2a;
- snapshot_payload[15:8] = 8'h00;
- for (i = 0; i < 24; i = i + 1) begin
- snapshot_payload[(2 + i) * 8 +: 8] = i + 1;
- end
- snapshot_payload[26 * 8 +: 8] = 8'd30;
- snapshot_payload[27 * 8 +: 8] = 8'd40;
- snapshot_payload[28 * 8 +: 8] = 8'd50;
- snapshot_payload[29 * 8 +: 8] = 8'd60;
- snapshot_payload[30 * 8 +: 8] = 8'd100;
- snapshot_payload[31 * 8 +: 8] = 8'd0;
- snapshot_payload[32 * 8 +: 8] = 8'd100;
- snapshot_payload[33 * 8 +: 8] = 8'd0;
- crc = 16'hffff;
- for (i = 2; i <= 30; i = i + 1) begin
- crc = crc16_byte(crc, snapshot_payload[i * 8 +: 8]);
- end
- expected_hash = crc;
- end
- endtask
- task submit_snapshot;
- begin
- while (!snapshot_ready) begin
- @(negedge clk);
- end
- snapshot_valid = 1'b1;
- @(posedge clk);
- #1;
- @(negedge clk);
- snapshot_valid = 1'b0;
- end
- endtask
- task wait_for_result;
- input expected_accept;
- begin
- wait_cycles = 0;
- while (!result_valid && wait_cycles < 50) begin
- @(negedge clk);
- wait_cycles = wait_cycles + 1;
- end
- if (!result_valid || result_accepted !== expected_accept) begin
- $display(
- "snapshot result mismatch valid=%0d accepted=%0d/%0d",
- result_valid,
- result_accepted,
- expected_accept
- );
- $fatal;
- end
- end
- endtask
- task remember_outputs;
- begin
- held_ptc = ptc_percent;
- held_fan = fan_percent;
- held_solenoid = solenoid_percent;
- held_hash = state_hash16;
- end
- endtask
- task expect_outputs_unchanged;
- begin
- if (ptc_percent !== held_ptc || fan_percent !== held_fan ||
- solenoid_percent !== held_solenoid || state_hash16 !== held_hash ||
- commit_pulse) begin
- $display("rejected snapshot changed synchronous outputs");
- $fatal;
- end
- end
- endtask
- initial begin
- rst_n = 1'b0;
- force_safe = 1'b0;
- snapshot_valid = 1'b0;
- snapshot_payload = 272'd0;
- commit_authorize = 1'b0;
- repeat (4) @(negedge clk);
- rst_n = 1'b1;
- @(negedge clk);
- if (ptc_percent !== 192'd0 || fan_percent !== {4{8'd30}} ||
- solenoid_percent !== 8'd0 || state_hash16 !== 16'he177) begin
- $display("reset safe state mismatch hash=%04x", state_hash16);
- $fatal;
- end
- build_valid_snapshot();
- submit_snapshot();
- // The ready/valid handshake owns the submitted values. A producer may
- // change its bus immediately afterward without tearing validation.
- snapshot_payload = 272'h3;
- repeat (10) begin
- @(negedge clk);
- if (ptc_percent !== 192'd0 || fan_percent !== {4{8'd30}} ||
- solenoid_percent !== 8'd0 || commit_pulse) begin
- $display("snapshot tore outputs before validation completed");
- $fatal;
- end
- end
- wait_for_result(1'b1);
- if (commit_pulse || snapshot_ready ||
- ptc_percent !== 192'd0 || fan_percent !== {4{8'd30}} ||
- solenoid_percent !== 8'd0) begin
- $display("validated snapshot committed before explicit authorization");
- $fatal;
- end
- commit_authorize = 1'b1;
- @(posedge clk);
- #1;
- commit_authorize = 1'b0;
- if (!commit_pulse) begin
- $display("authorized snapshot did not emit one output commit");
- $fatal;
- end
- for (i = 0; i < 24; i = i + 1) begin
- if (ptc_percent[i * 8 +: 8] !== i + 1) begin
- $display("PTC%0d mismatch value=%0d", i + 1, ptc_percent[i * 8 +: 8]);
- $fatal;
- end
- end
- if (fan_percent !== {8'd60, 8'd50, 8'd40, 8'd30} ||
- solenoid_percent !== 8'd100 || state_hash16 !== expected_hash) begin
- $display("committed state mismatch hash=%04x/%04x", state_hash16, expected_hash);
- $fatal;
- end
- @(posedge clk);
- #1;
- @(negedge clk);
- if (result_valid || commit_pulse) begin
- $display("result and commit indications must be one-cycle pulses");
- $fatal;
- end
- remember_outputs();
- build_valid_snapshot();
- snapshot_payload[7:0] = 8'h21;
- submit_snapshot();
- wait_for_result(1'b0);
- expect_outputs_unchanged();
- remember_outputs();
- build_valid_snapshot();
- snapshot_payload[8 * 8 +: 8] = 8'd101;
- submit_snapshot();
- wait_for_result(1'b0);
- expect_outputs_unchanged();
- remember_outputs();
- build_valid_snapshot();
- snapshot_payload[8 * 8 +: 8] = 8'd127;
- submit_snapshot();
- wait_for_result(1'b0);
- expect_outputs_unchanged();
- remember_outputs();
- build_valid_snapshot();
- snapshot_payload[8 * 8 +: 8] = 8'd128;
- submit_snapshot();
- wait_for_result(1'b0);
- expect_outputs_unchanged();
- remember_outputs();
- build_valid_snapshot();
- snapshot_payload[8 * 8 +: 8] = 8'd255;
- submit_snapshot();
- wait_for_result(1'b0);
- expect_outputs_unchanged();
- remember_outputs();
- build_valid_snapshot();
- snapshot_payload[30 * 8 +: 8] = 8'd50;
- submit_snapshot();
- wait_for_result(1'b0);
- expect_outputs_unchanged();
- build_valid_snapshot();
- submit_snapshot();
- repeat (5) @(negedge clk);
- force_safe = 1'b1;
- @(posedge clk);
- #1;
- force_safe = 1'b0;
- if (!result_valid || result_accepted || !snapshot_ready ||
- ptc_percent !== 192'd0 || fan_percent !== {4{8'd30}} ||
- solenoid_percent !== 8'd0 || state_hash16 !== 16'he177) begin
- $display("force_safe did not reject and terminate an in-flight snapshot");
- $fatal;
- end
- @(negedge clk);
- build_valid_snapshot();
- if (!snapshot_ready) begin
- $display("snapshot interface was not ready before force_safe boundary test");
- $fatal;
- end
- snapshot_valid = 1'b1;
- force_safe = 1'b1;
- @(posedge clk);
- #1;
- snapshot_valid = 1'b0;
- force_safe = 1'b0;
- if (!result_valid || result_accepted || !snapshot_ready ||
- ptc_percent !== 192'd0 || fan_percent !== {4{8'd30}} ||
- solenoid_percent !== 8'd0 || state_hash16 !== 16'he177) begin
- $display("force_safe did not reject a snapshot presented on the same cycle");
- $fatal;
- end
- @(negedge clk);
- force_safe = 1'b1;
- @(posedge clk);
- #1;
- force_safe = 1'b0;
- if (ptc_percent !== 192'd0 || fan_percent !== {4{8'd30}} ||
- solenoid_percent !== 8'd0 || state_hash16 !== 16'he177) begin
- $display("force_safe did not restore the complete safe state");
- $fatal;
- end
- $display("PASS: prod2u_p4_normal_v3_output_state_tb");
- $finish;
- end
- endmodule
|