project_adapter_tb.v 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. `timescale 1ns/1ps
  2. module project_adapter_tb;
  3. reg clk = 1'b0;
  4. reg rst_n = 1'b0;
  5. reg frame_valid = 1'b0;
  6. wire frame_ready;
  7. wire frame_accept_enable;
  8. reg [7:0] frame_type = 8'd0;
  9. reg [7:0] frame_length = 8'd0;
  10. wire [7:0] payload_read_addr;
  11. reg [7:0] payload_read_data = 8'd0;
  12. wire target_wr_en;
  13. wire [3:0] target_wr_addr;
  14. wire [7:0] target_wr_data;
  15. wire valid_set_all_commit;
  16. wire motor_request_valid;
  17. wire [7:0] door_percent_x;
  18. wire [7:0] door_percent_y;
  19. wire [7:0] door_percent_z;
  20. reg startup_complete = 1'b0;
  21. wire temperature_start;
  22. reg temperature_done = 1'b0;
  23. wire status_capture;
  24. wire tx_start;
  25. wire [7:0] tx_frame_type;
  26. wire [7:0] tx_payload_length;
  27. wire tx_payload_is_status;
  28. reg tx_done = 1'b0;
  29. wire busy;
  30. reg [7:0] payload [0:15];
  31. integer motor_pulses;
  32. integer commits;
  33. always #1 clk = ~clk;
  34. project_adapter dut (
  35. .clk(clk), .rst_n(rst_n),
  36. .frame_valid(frame_valid), .frame_ready(frame_ready),
  37. .frame_accept_enable(frame_accept_enable),
  38. .frame_type(frame_type), .frame_length(frame_length),
  39. .payload_read_addr(payload_read_addr), .payload_read_data(payload_read_data),
  40. .target_wr_en(target_wr_en), .target_wr_addr(target_wr_addr),
  41. .target_wr_data(target_wr_data),
  42. .valid_set_all_commit(valid_set_all_commit),
  43. .motor_request_valid(motor_request_valid),
  44. .door_percent_x(door_percent_x),
  45. .door_percent_y(door_percent_y),
  46. .door_percent_z(door_percent_z),
  47. .startup_complete(startup_complete),
  48. .temperature_start(temperature_start), .temperature_done(temperature_done),
  49. .status_capture(status_capture),
  50. .tx_start(tx_start), .tx_frame_type(tx_frame_type),
  51. .tx_payload_length(tx_payload_length), .tx_payload_is_status(tx_payload_is_status),
  52. .tx_done(tx_done), .busy(busy)
  53. );
  54. // Combinational payload read (matches same-cycle validate sampling).
  55. always @* payload_read_data = payload[payload_read_addr];
  56. always @(posedge clk) begin
  57. if (motor_request_valid) motor_pulses = motor_pulses + 1;
  58. if (valid_set_all_commit) commits = commits + 1;
  59. end
  60. task present_frame;
  61. input [7:0] ftype;
  62. input [7:0] flen;
  63. begin
  64. @(negedge clk);
  65. while (!frame_accept_enable) @(negedge clk);
  66. frame_type = ftype;
  67. frame_length = flen;
  68. frame_valid = 1'b1;
  69. @(negedge clk);
  70. frame_valid = 1'b0;
  71. end
  72. endtask
  73. integer i;
  74. initial begin
  75. #200000 $fatal(1, "project_adapter_tb timeout");
  76. end
  77. initial begin
  78. for (i = 0; i < 16; i = i + 1) payload[i] = 8'd0;
  79. motor_pulses = 0;
  80. commits = 0;
  81. repeat (2) @(posedge clk);
  82. rst_n = 1'b1;
  83. // Pre-startup SET_ALL: thermal commit, no motor request
  84. payload[0] = 8'd10; payload[1] = 8'd10; payload[2] = 8'd10; payload[3] = 8'd10;
  85. payload[4] = 8'd20; payload[5] = 8'd20; payload[6] = 8'd20; payload[7] = 8'd20;
  86. payload[8] = 8'd50; payload[9] = 8'd50; payload[10] = 8'd50;
  87. present_frame(8'h01, 8'd11);
  88. wait (valid_set_all_commit);
  89. @(negedge clk);
  90. if (motor_pulses !== 0)
  91. $fatal(1, "pre-startup must ignore door percents");
  92. wait (frame_accept_enable);
  93. // Pre-startup GET_TEMP: silent drain, no TX
  94. present_frame(8'h02, 8'd0);
  95. repeat (20) @(negedge clk);
  96. if (temperature_start || tx_start)
  97. $fatal(1, "pre-startup GET_TEMP must not respond");
  98. wait (frame_accept_enable);
  99. // GET_MOTOR_STATUS always allowed
  100. present_frame(8'h03, 8'd0);
  101. wait (status_capture);
  102. wait (tx_start);
  103. if (tx_frame_type !== 8'h83 || tx_payload_length !== 8'd31 || !tx_payload_is_status)
  104. $fatal(1, "GET_STATUS response meta mismatch");
  105. @(negedge clk); tx_done = 1'b1;
  106. @(negedge clk); tx_done = 1'b0;
  107. wait (frame_accept_enable);
  108. // After startup: SET_ALL pulses motor_request_valid
  109. startup_complete = 1'b1;
  110. repeat (2) @(negedge clk);
  111. motor_pulses = 0;
  112. // Ensure payload still holds 50% doors before second commit
  113. payload[8] = 8'd50; payload[9] = 8'd50; payload[10] = 8'd50;
  114. present_frame(8'h01, 8'd11);
  115. wait (motor_request_valid);
  116. @(negedge clk);
  117. if (door_percent_z !== 8'd50 || door_percent_x !== 8'd50 || door_percent_y !== 8'd50)
  118. $fatal(1, "door percents not latched z=%0d x=%0d y=%0d",
  119. door_percent_z, door_percent_x, door_percent_y);
  120. wait (frame_accept_enable);
  121. // Reject door 101
  122. payload[8] = 8'd101;
  123. motor_pulses = 0;
  124. commits = 0;
  125. present_frame(8'h01, 8'd11);
  126. repeat (40) @(negedge clk);
  127. if (commits !== 0 || motor_pulses !== 0)
  128. $fatal(1, "door 101 must reject whole frame");
  129. wait (frame_accept_enable);
  130. $display("project_adapter_tb PASS");
  131. $finish;
  132. end
  133. endmodule