thermal_host_command_v2_tb.v 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. `timescale 1ns/1ps
  2. module thermal_host_command_v2_tb;
  3. reg clk = 0, rst_n = 0, frame_valid = 0, tx_done = 0;
  4. reg [7:0] frame_type = 0, frame_length = 0;
  5. wire [7:0] payload_addr;
  6. reg [7:0] payload [0:32];
  7. reg safety_locked = 0, manual_done = 0, manual_ok = 1, snapshot_ready = 0;
  8. wire manual_wr, manual_commit, pid_commit, snapshot_capture;
  9. wire [4:0] manual_wr_addr;
  10. wire [7:0] manual_wr_data, pid_target;
  11. wire tx_start, transaction_done, busy;
  12. wire [7:0] tx_type, tx_length;
  13. reg [7:0] tx_payload_addr = 0;
  14. wire [7:0] ack_data;
  15. integer writes = 0, i;
  16. always #5 clk = ~clk;
  17. always @(posedge clk) if (manual_wr) writes <= writes + 1;
  18. thermal_host_command #(.SNAPSHOT_LENGTH(201)) dut (
  19. .clk(clk), .rst_n(rst_n), .frame_valid(frame_valid), .frame_ready(),
  20. .frame_accept_enable(), .frame_type(frame_type), .frame_length(frame_length),
  21. .payload_read_addr(payload_addr), .payload_read_data(payload[payload_addr]),
  22. .manual_wr_en(manual_wr), .manual_wr_addr(manual_wr_addr),
  23. .manual_wr_data(manual_wr_data), .manual_commit(manual_commit),
  24. .manual_commit_done(manual_done), .manual_commit_ok(manual_ok),
  25. .safety_locked(safety_locked),
  26. .pid_target_commit(pid_commit), .pid_target_celsius(pid_target),
  27. .snapshot_capture(snapshot_capture), .snapshot_ready(snapshot_ready),
  28. .tx_start(tx_start), .tx_frame_type(tx_type), .tx_payload_length(tx_length),
  29. .tx_payload_addr(tx_payload_addr), .ack_payload_data(ack_data),
  30. .tx_done(tx_done), .transaction_done(transaction_done), .busy(busy)
  31. );
  32. task request;
  33. input [7:0] kind;
  34. input [7:0] length;
  35. begin
  36. while (busy) @(negedge clk);
  37. @(negedge clk); frame_type = kind; frame_length = length; frame_valid = 1;
  38. @(negedge clk); frame_valid = 0;
  39. end
  40. endtask
  41. task expect_ack;
  42. input [7:0] ok;
  43. input [7:0] request_kind;
  44. input [7:0] status;
  45. input [7:0] detail;
  46. integer timeout;
  47. begin
  48. timeout = 0;
  49. while (!tx_start && timeout < 300) begin @(negedge clk); timeout = timeout + 1; end
  50. if (timeout == 300 || tx_type != 8'h06 || tx_length != 4)
  51. $fatal(1, "ACK timeout/type state=%0d req=%h len=%0d tx_start=%b tx_type=%h tx_len=%0d",
  52. dut.state, frame_type, frame_length, tx_start, tx_type, tx_length);
  53. tx_payload_addr = 0; #1; if (ack_data != ok) $fatal;
  54. tx_payload_addr = 1; #1; if (ack_data != request_kind) $fatal;
  55. tx_payload_addr = 2; #1; if (ack_data != status) $fatal;
  56. tx_payload_addr = 3; #1; if (ack_data != detail) $fatal;
  57. @(negedge clk); tx_done = 1;
  58. @(negedge clk); tx_done = 0;
  59. end
  60. endtask
  61. initial begin
  62. for (i = 0; i < 33; i = i + 1) payload[i] = 0;
  63. repeat (3) @(negedge clk); rst_n = 1;
  64. request(8'h01, 31); expect_ack(0, 8'h01, 2, 32);
  65. request(8'h03, 33); expect_ack(0, 8'h03, 1, 0);
  66. request(8'h55, 0); expect_ack(0, 8'h55, 1, 0);
  67. payload[7] = 101; writes = 0;
  68. request(8'h01, 32); expect_ack(0, 8'h01, 3, 7);
  69. if (writes != 0 || manual_commit) $fatal(1, "bad SET_ALL partially wrote");
  70. // A new lock after validation but before Manual Bank commit is
  71. // fail-closed and never emits manual_commit or ACK OK.
  72. for (i = 0; i < 32; i = i + 1) payload[i] = 20;
  73. writes = 0; request(8'h01, 32);
  74. wait(dut.state == 3); safety_locked = 1;
  75. expect_ack(0, 8'h01, 4, 0);
  76. if (manual_commit || writes != 32) $fatal(1, "validate race did not fail closed");
  77. safety_locked = 0;
  78. for (i = 0; i < 32; i = i + 1) payload[i] = (i < 24) ? i : 20;
  79. writes = 0; request(8'h01, 32);
  80. wait(manual_commit); if (writes != 32) $fatal(1, "valid writes=%0d", writes);
  81. repeat (3) @(negedge clk); if (tx_start) $fatal(1, "ACK before atomic commit boundary");
  82. manual_done = 1; @(negedge clk); manual_done = 0;
  83. expect_ack(1, 8'h01, 0, 0);
  84. // A hard lock discovered after the manual bank commit cancels ACK OK.
  85. writes = 0; request(8'h01, 32);
  86. wait(manual_commit); manual_ok = 0;
  87. @(negedge clk); manual_done = 1;
  88. @(negedge clk); manual_done = 0;
  89. expect_ack(0, 8'h01, 4, 0); manual_ok = 1;
  90. safety_locked = 1; request(8'h01, 32); expect_ack(0, 8'h01, 4, 0); safety_locked = 0;
  91. payload[0] = 19; request(8'h04, 1); expect_ack(0, 8'h04, 3, 0);
  92. payload[0] = 70; request(8'h04, 1);
  93. wait(pid_commit); if (pid_target != 70) $fatal;
  94. expect_ack(1, 8'h04, 0, 0);
  95. request(8'h02, 0); wait(snapshot_capture);
  96. snapshot_ready = 1; @(negedge clk); snapshot_ready = 0;
  97. expect_ack(1, 8'h02, 0, 0);
  98. wait(tx_start); if (tx_type != 8'h82 || tx_length != 201) $fatal(1, "GET DATA order");
  99. @(negedge clk); tx_done = 1; @(negedge clk); tx_done = 0;
  100. wait(transaction_done);
  101. $display("PASS: unified ACK/NACK, atomic SET_ALL, PID and GET 06->82");
  102. $finish;
  103. end
  104. endmodule