prod2u_p4_normal_v3_output_state_tb.v 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. `timescale 1ns/1ps
  2. module prod2u_p4_normal_v3_output_state_tb;
  3. reg clk;
  4. reg rst_n;
  5. reg force_safe;
  6. reg snapshot_valid;
  7. wire snapshot_ready;
  8. reg [271:0] snapshot_payload;
  9. reg commit_authorize;
  10. wire result_valid;
  11. wire result_accepted;
  12. wire commit_pulse;
  13. wire [191:0] ptc_percent;
  14. wire [31:0] fan_percent;
  15. wire [7:0] solenoid_percent;
  16. wire [15:0] state_hash16;
  17. reg [191:0] held_ptc;
  18. reg [31:0] held_fan;
  19. reg [7:0] held_solenoid;
  20. reg [15:0] held_hash;
  21. reg [15:0] expected_hash;
  22. integer i;
  23. integer wait_cycles;
  24. prod2u_p4_normal_v3_output_state dut (
  25. .clk(clk),
  26. .rst_n(rst_n),
  27. .force_safe(force_safe),
  28. .snapshot_valid(snapshot_valid),
  29. .snapshot_ready(snapshot_ready),
  30. .snapshot_payload(snapshot_payload),
  31. .commit_authorize(commit_authorize),
  32. .result_valid(result_valid),
  33. .result_accepted(result_accepted),
  34. .commit_pulse(commit_pulse),
  35. .ptc_percent(ptc_percent),
  36. .fan_percent(fan_percent),
  37. .solenoid_percent(solenoid_percent),
  38. .state_hash16(state_hash16)
  39. );
  40. initial begin
  41. clk = 1'b0;
  42. forever #5 clk = ~clk;
  43. end
  44. function [15:0] crc16_byte;
  45. input [15:0] crc_in;
  46. input [7:0] data;
  47. reg [15:0] value;
  48. integer bit_index;
  49. begin
  50. value = crc_in ^ {data, 8'h00};
  51. for (bit_index = 0; bit_index < 8; bit_index = bit_index + 1) begin
  52. if (value[15]) begin
  53. value = (value << 1) ^ 16'h1021;
  54. end else begin
  55. value = value << 1;
  56. end
  57. end
  58. crc16_byte = value;
  59. end
  60. endfunction
  61. task build_valid_snapshot;
  62. reg [15:0] crc;
  63. begin
  64. snapshot_payload = 272'd0;
  65. snapshot_payload[7:0] = 8'h2a;
  66. snapshot_payload[15:8] = 8'h00;
  67. for (i = 0; i < 24; i = i + 1) begin
  68. snapshot_payload[(2 + i) * 8 +: 8] = i + 1;
  69. end
  70. snapshot_payload[26 * 8 +: 8] = 8'd30;
  71. snapshot_payload[27 * 8 +: 8] = 8'd40;
  72. snapshot_payload[28 * 8 +: 8] = 8'd50;
  73. snapshot_payload[29 * 8 +: 8] = 8'd60;
  74. snapshot_payload[30 * 8 +: 8] = 8'd100;
  75. snapshot_payload[31 * 8 +: 8] = 8'd0;
  76. snapshot_payload[32 * 8 +: 8] = 8'd100;
  77. snapshot_payload[33 * 8 +: 8] = 8'd0;
  78. crc = 16'hffff;
  79. for (i = 2; i <= 30; i = i + 1) begin
  80. crc = crc16_byte(crc, snapshot_payload[i * 8 +: 8]);
  81. end
  82. expected_hash = crc;
  83. end
  84. endtask
  85. task submit_snapshot;
  86. begin
  87. while (!snapshot_ready) begin
  88. @(negedge clk);
  89. end
  90. snapshot_valid = 1'b1;
  91. @(posedge clk);
  92. #1;
  93. @(negedge clk);
  94. snapshot_valid = 1'b0;
  95. end
  96. endtask
  97. task wait_for_result;
  98. input expected_accept;
  99. begin
  100. wait_cycles = 0;
  101. while (!result_valid && wait_cycles < 50) begin
  102. @(negedge clk);
  103. wait_cycles = wait_cycles + 1;
  104. end
  105. if (!result_valid || result_accepted !== expected_accept) begin
  106. $display(
  107. "snapshot result mismatch valid=%0d accepted=%0d/%0d",
  108. result_valid,
  109. result_accepted,
  110. expected_accept
  111. );
  112. $fatal;
  113. end
  114. end
  115. endtask
  116. task remember_outputs;
  117. begin
  118. held_ptc = ptc_percent;
  119. held_fan = fan_percent;
  120. held_solenoid = solenoid_percent;
  121. held_hash = state_hash16;
  122. end
  123. endtask
  124. task expect_outputs_unchanged;
  125. begin
  126. if (ptc_percent !== held_ptc || fan_percent !== held_fan ||
  127. solenoid_percent !== held_solenoid || state_hash16 !== held_hash ||
  128. commit_pulse) begin
  129. $display("rejected snapshot changed synchronous outputs");
  130. $fatal;
  131. end
  132. end
  133. endtask
  134. initial begin
  135. rst_n = 1'b0;
  136. force_safe = 1'b0;
  137. snapshot_valid = 1'b0;
  138. snapshot_payload = 272'd0;
  139. commit_authorize = 1'b0;
  140. repeat (4) @(negedge clk);
  141. rst_n = 1'b1;
  142. @(negedge clk);
  143. if (ptc_percent !== 192'd0 || fan_percent !== {4{8'd30}} ||
  144. solenoid_percent !== 8'd0 || state_hash16 !== 16'he177) begin
  145. $display("reset safe state mismatch hash=%04x", state_hash16);
  146. $fatal;
  147. end
  148. build_valid_snapshot();
  149. submit_snapshot();
  150. // The ready/valid handshake owns the submitted values. A producer may
  151. // change its bus immediately afterward without tearing validation.
  152. snapshot_payload = 272'h3;
  153. repeat (10) begin
  154. @(negedge clk);
  155. if (ptc_percent !== 192'd0 || fan_percent !== {4{8'd30}} ||
  156. solenoid_percent !== 8'd0 || commit_pulse) begin
  157. $display("snapshot tore outputs before validation completed");
  158. $fatal;
  159. end
  160. end
  161. wait_for_result(1'b1);
  162. if (commit_pulse || snapshot_ready ||
  163. ptc_percent !== 192'd0 || fan_percent !== {4{8'd30}} ||
  164. solenoid_percent !== 8'd0) begin
  165. $display("validated snapshot committed before explicit authorization");
  166. $fatal;
  167. end
  168. commit_authorize = 1'b1;
  169. @(posedge clk);
  170. #1;
  171. commit_authorize = 1'b0;
  172. if (!commit_pulse) begin
  173. $display("authorized snapshot did not emit one output commit");
  174. $fatal;
  175. end
  176. for (i = 0; i < 24; i = i + 1) begin
  177. if (ptc_percent[i * 8 +: 8] !== i + 1) begin
  178. $display("PTC%0d mismatch value=%0d", i + 1, ptc_percent[i * 8 +: 8]);
  179. $fatal;
  180. end
  181. end
  182. if (fan_percent !== {8'd60, 8'd50, 8'd40, 8'd30} ||
  183. solenoid_percent !== 8'd100 || state_hash16 !== expected_hash) begin
  184. $display("committed state mismatch hash=%04x/%04x", state_hash16, expected_hash);
  185. $fatal;
  186. end
  187. @(posedge clk);
  188. #1;
  189. @(negedge clk);
  190. if (result_valid || commit_pulse) begin
  191. $display("result and commit indications must be one-cycle pulses");
  192. $fatal;
  193. end
  194. remember_outputs();
  195. build_valid_snapshot();
  196. snapshot_payload[7:0] = 8'h21;
  197. submit_snapshot();
  198. wait_for_result(1'b0);
  199. expect_outputs_unchanged();
  200. remember_outputs();
  201. build_valid_snapshot();
  202. snapshot_payload[8 * 8 +: 8] = 8'd101;
  203. submit_snapshot();
  204. wait_for_result(1'b0);
  205. expect_outputs_unchanged();
  206. remember_outputs();
  207. build_valid_snapshot();
  208. snapshot_payload[8 * 8 +: 8] = 8'd127;
  209. submit_snapshot();
  210. wait_for_result(1'b0);
  211. expect_outputs_unchanged();
  212. remember_outputs();
  213. build_valid_snapshot();
  214. snapshot_payload[8 * 8 +: 8] = 8'd128;
  215. submit_snapshot();
  216. wait_for_result(1'b0);
  217. expect_outputs_unchanged();
  218. remember_outputs();
  219. build_valid_snapshot();
  220. snapshot_payload[8 * 8 +: 8] = 8'd255;
  221. submit_snapshot();
  222. wait_for_result(1'b0);
  223. expect_outputs_unchanged();
  224. remember_outputs();
  225. build_valid_snapshot();
  226. snapshot_payload[30 * 8 +: 8] = 8'd50;
  227. submit_snapshot();
  228. wait_for_result(1'b0);
  229. expect_outputs_unchanged();
  230. build_valid_snapshot();
  231. submit_snapshot();
  232. repeat (5) @(negedge clk);
  233. force_safe = 1'b1;
  234. @(posedge clk);
  235. #1;
  236. force_safe = 1'b0;
  237. if (!result_valid || result_accepted || !snapshot_ready ||
  238. ptc_percent !== 192'd0 || fan_percent !== {4{8'd30}} ||
  239. solenoid_percent !== 8'd0 || state_hash16 !== 16'he177) begin
  240. $display("force_safe did not reject and terminate an in-flight snapshot");
  241. $fatal;
  242. end
  243. @(negedge clk);
  244. build_valid_snapshot();
  245. if (!snapshot_ready) begin
  246. $display("snapshot interface was not ready before force_safe boundary test");
  247. $fatal;
  248. end
  249. snapshot_valid = 1'b1;
  250. force_safe = 1'b1;
  251. @(posedge clk);
  252. #1;
  253. snapshot_valid = 1'b0;
  254. force_safe = 1'b0;
  255. if (!result_valid || result_accepted || !snapshot_ready ||
  256. ptc_percent !== 192'd0 || fan_percent !== {4{8'd30}} ||
  257. solenoid_percent !== 8'd0 || state_hash16 !== 16'he177) begin
  258. $display("force_safe did not reject a snapshot presented on the same cycle");
  259. $fatal;
  260. end
  261. @(negedge clk);
  262. force_safe = 1'b1;
  263. @(posedge clk);
  264. #1;
  265. force_safe = 1'b0;
  266. if (ptc_percent !== 192'd0 || fan_percent !== {4{8'd30}} ||
  267. solenoid_percent !== 8'd0 || state_hash16 !== 16'he177) begin
  268. $display("force_safe did not restore the complete safe state");
  269. $fatal;
  270. end
  271. $display("PASS: prod2u_p4_normal_v3_output_state_tb");
  272. $finish;
  273. end
  274. endmodule