| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661 |
- `timescale 1ns/1ps
- // Full E2E bit-level simulation testbench for MRDIMM Host TTL Interface (CON146: Pin1/H6=TX, Pin3/N7=RX, Pin2/4=GND).
- //
- // This testbench verifies:
- // 1. Bit-level UART communication at 921600 baud (25 MHz clock) through host_ttl_rx.
- // 2. Broadcast reply transmission through host_ttl_tx (and mirrored on host_rs232_tx).
- // 3. Complete GET_ALL / GET_ACK / GET_DATA cycle initiated over TTL.
- // 4. SET_ALL cycle over TTL with motor movement (Axis 0) and thermal PWM duty changes.
- // 5. Limit detection and clean MOTOR_DONE reporting over TTL.
- // 6. STOP_CLEAR abort over TTL.
- // 7. Interleaved dual-port operation: TTL and RS232 taking turns seamlessly.
- // 8. Corrupted / Bad CRC frame recovery over TTL.
- module prod2u_mrdimm_ttl_e2e_tb;
- localparam integer BIT_CLKS = 27; // 921600 baud @ 25 MHz
- reg clk = 1'b0;
- reg rst_n = 1'b0;
- // Both UART lines
- reg rs232_rx = 1'b1;
- wire rs232_tx;
- reg ttl_rx = 1'b1;
- wire ttl_tx;
- reg [11:0] limit_lower_raw = 12'd0;
- reg [11:0] limit_upper_raw = 12'd0;
- reg hood_switch_in = 1'b0;
- wire buzzer_out;
- wire [23:0] ptc_pwm_out;
- wire [3:0] fan_pwm_out;
- wire motor_en_n;
- wire [11:0] motor_step_gate;
- wire [11:0] motor_dir_gate;
- wire x_step = motor_step_gate[0];
- wire x_dir = motor_dir_gate[0];
- wire y_step = motor_step_gate[1];
- wire y_dir = motor_dir_gate[1];
- wire z_step = motor_step_gate[2];
- wire z_dir = motor_dir_gate[2];
- always #20 clk = ~clk; // 25 MHz (40ns period)
- prod2u_mrdimm_final_top #(
- .CLOCK_HZ(25000000),
- .HOST_BAUD(921600),
- .BMC_BAUD(115200),
- .PWM_PERIOD_CLKS(5000),
- .PWM_CLKS_PER_PERCENT(50),
- .HOST_TIMEOUT_CYCLES(2500)
- ) dut (
- .sys_clk(clk),
- .host_rs232_rx(rs232_rx),
- .host_rs232_tx(rs232_tx),
- .host_ttl_rx(ttl_rx),
- .host_ttl_tx(ttl_tx),
- .bmc_rs232_rx(1'b1),
- .bmc_rs232_tx(),
- .temp_scl(),
- .temp_sda(),
- .limit_lower_raw(limit_lower_raw),
- .limit_upper_raw(limit_upper_raw),
- .ptc_pwm_out(ptc_pwm_out),
- .fan_pwm_out(fan_pwm_out),
- .motor_en_n(motor_en_n),
- .motor_step_gate(motor_step_gate),
- .motor_dir_gate(motor_dir_gate),
- .hood_switch_in(hood_switch_in),
- .buzzer_out(buzzer_out)
- );
- // Byte-wise CRC over TYPE, LENGTH and PAYLOAD (excludes header A5 5A)
- reg [7:0] frame_bytes [0:255];
- function [15:0] crc_over_frame;
- input integer count;
- integer i;
- integer b;
- reg [15:0] c;
- reg [7:0] d;
- begin
- c = 16'hFFFF;
- for (i = 2; i < count; i = i + 1) begin
- d = frame_bytes[i];
- for (b = 7; b >= 0; b = b - 1)
- c = (c[15] ^ d[b]) ? ({c[14:0], 1'b0} ^ 16'h1021)
- : {c[14:0], 1'b0};
- end
- crc_over_frame = c;
- end
- endfunction
- // UART TX byte task to TTL RX
- task ttl_send_byte;
- input [7:0] value;
- integer b;
- begin
- ttl_rx = 1'b0;
- repeat (BIT_CLKS) @(posedge clk);
- for (b = 0; b < 8; b = b + 1) begin
- ttl_rx = value[b];
- repeat (BIT_CLKS) @(posedge clk);
- end
- ttl_rx = 1'b1;
- repeat (BIT_CLKS) @(posedge clk);
- end
- endtask
- // UART TX frame task to TTL RX
- task ttl_send_frame;
- input integer n;
- integer i;
- reg [15:0] c;
- begin
- c = crc_over_frame(n);
- for (i = 0; i < n; i = i + 1)
- ttl_send_byte(frame_bytes[i]);
- ttl_send_byte(c[7:0]);
- ttl_send_byte(c[15:8]);
- end
- endtask
- // UART TX frame task to RS232 RX (for dual-port tests)
- task rs232_send_byte;
- input [7:0] value;
- integer b;
- begin
- rs232_rx = 1'b0;
- repeat (BIT_CLKS) @(posedge clk);
- for (b = 0; b < 8; b = b + 1) begin
- rs232_rx = value[b];
- repeat (BIT_CLKS) @(posedge clk);
- end
- rs232_rx = 1'b1;
- repeat (BIT_CLKS) @(posedge clk);
- end
- endtask
- task rs232_send_frame;
- input integer n;
- integer i;
- reg [15:0] c;
- begin
- c = crc_over_frame(n);
- for (i = 0; i < n; i = i + 1)
- rs232_send_byte(frame_bytes[i]);
- rs232_send_byte(c[7:0]);
- rs232_send_byte(c[15:8]);
- end
- endtask
- // UART RX monitor from TTL TX
- reg [7:0] ttl_rx_bytes [0:511];
- integer ttl_rx_count = 0;
- reg [3:0] ttl_rx_bit = 0;
- reg [7:0] ttl_rx_shift = 0;
- integer ttl_rx_clk_cnt = 0;
- reg ttl_rx_busy = 0;
- initial begin
- forever begin
- @(posedge clk);
- if (!ttl_rx_busy && ttl_tx === 1'b0) begin
- ttl_rx_busy = 1'b1;
- ttl_rx_clk_cnt = BIT_CLKS + (BIT_CLKS / 2) - 1;
- ttl_rx_bit = 0;
- end else if (ttl_rx_busy) begin
- if (ttl_rx_clk_cnt == 0) begin
- if (ttl_rx_bit < 8) begin
- ttl_rx_shift[ttl_rx_bit] = ttl_tx;
- ttl_rx_bit = ttl_rx_bit + 1;
- ttl_rx_clk_cnt = BIT_CLKS - 1;
- end else begin
- ttl_rx_bytes[ttl_rx_count] = ttl_rx_shift;
- ttl_rx_count = ttl_rx_count + 1;
- ttl_rx_busy = 1'b0;
- end
- end else begin
- ttl_rx_clk_cnt = ttl_rx_clk_cnt - 1;
- end
- end
- end
- end
- // UART RX monitor from RS232 TX (to verify TX broadcast)
- reg [7:0] rs232_rx_bytes [0:511];
- integer rs232_rx_count = 0;
- reg [3:0] rs232_rx_bit = 0;
- reg [7:0] rs232_rx_shift = 0;
- integer rs232_rx_clk_cnt = 0;
- reg rs232_rx_busy = 0;
- initial begin
- forever begin
- @(posedge clk);
- if (!rs232_rx_busy && rs232_tx === 1'b0) begin
- rs232_rx_busy = 1'b1;
- rs232_rx_clk_cnt = BIT_CLKS + (BIT_CLKS / 2) - 1;
- rs232_rx_bit = 0;
- end else if (rs232_rx_busy) begin
- if (rs232_rx_clk_cnt == 0) begin
- if (rs232_rx_bit < 8) begin
- rs232_rx_shift[rs232_rx_bit] = rs232_tx;
- rs232_rx_bit = rs232_rx_bit + 1;
- rs232_rx_clk_cnt = BIT_CLKS - 1;
- end else begin
- rs232_rx_bytes[rs232_rx_count] = rs232_rx_shift;
- rs232_rx_count = rs232_rx_count + 1;
- rs232_rx_busy = 1'b0;
- end
- end else begin
- rs232_rx_clk_cnt = rs232_rx_clk_cnt - 1;
- end
- end
- end
- end
- // Step edge counters
- integer x_steps_count = 0;
- integer y_steps_count = 0;
- integer z_steps_count = 0;
- always @(posedge x_step) x_steps_count = x_steps_count + 1;
- always @(posedge y_step) y_steps_count = y_steps_count + 1;
- always @(posedge z_step) z_steps_count = z_steps_count + 1;
- // Timeout guard
- initial begin
- #40000000; // 40ms simulation time
- $display("FATAL: Simulation timeout!");
- $fflush;
- $finish;
- end
- integer start_idx;
- integer i;
- initial begin
- $display("==================================================");
- $display("=== STARTING MRDIMM HOST TTL E2E TESTBENCH ===");
- $display("=== Port: CON146 (H6=TX, N7=RX) @ 921600 8N1 ===");
- $display("==================================================");
- $fflush;
- rst_n = 1'b0;
- limit_lower_raw = 3'b000;
- limit_upper_raw = 3'b000;
- repeat (100) @(posedge clk);
- rst_n = 1'b1;
- repeat (2000) @(posedge clk);
- // --------------------------------------------------------------------
- // TEST 1: Legal GET_ALL sent over host_ttl_rx
- // --------------------------------------------------------------------
- $display("[TEST 1] Sending GET_ALL over host_ttl_rx...");
- $fflush;
- frame_bytes[0] = 8'hA5;
- frame_bytes[1] = 8'h5A;
- frame_bytes[2] = 8'h02; // GET_ALL
- frame_bytes[3] = 8'h00; // LEN 0
- start_idx = ttl_rx_count;
- ttl_send_frame(4);
- // Expect GET_ACK (6 bytes: A5 5A 82 00 F5 60) + GET_DATA (71 bytes: A5 5A 42 41 ... CRC)
- // Total bytes = 77 bytes
- wait(ttl_rx_count >= start_idx + 77);
- $display(" -> Received 77 bytes from GET_ALL on host_ttl_tx");
- $fflush;
- // Verify GET_ACK on TTL TX
- if (ttl_rx_bytes[start_idx] !== 8'hA5 || ttl_rx_bytes[start_idx+1] !== 8'h5A ||
- ttl_rx_bytes[start_idx+2] !== 8'h82 || ttl_rx_bytes[start_idx+3] !== 8'h00) begin
- $fatal(1, "FAIL 1: GET_ACK mismatch on ttl_tx: %02h %02h %02h %02h",
- ttl_rx_bytes[start_idx], ttl_rx_bytes[start_idx+1],
- ttl_rx_bytes[start_idx+2], ttl_rx_bytes[start_idx+3]);
- end
- $display(" -> Verified GET_ACK (82 00) on host_ttl_tx");
- // Verify GET_DATA header on TTL TX
- start_idx = start_idx + 6;
- if (ttl_rx_bytes[start_idx] !== 8'hA5 || ttl_rx_bytes[start_idx+1] !== 8'h5A ||
- ttl_rx_bytes[start_idx+2] !== 8'h42 || ttl_rx_bytes[start_idx+3] !== 8'h41) begin
- $fatal(1, "FAIL 1: GET_DATA header mismatch on ttl_tx: %02h %02h %02h %02h",
- ttl_rx_bytes[start_idx], ttl_rx_bytes[start_idx+1],
- ttl_rx_bytes[start_idx+2], ttl_rx_bytes[start_idx+3]);
- end
- // Verify FAN default = 20% on all 4 channels
- if (ttl_rx_bytes[start_idx+4] !== 8'd20 || ttl_rx_bytes[start_idx+5] !== 8'd20 ||
- ttl_rx_bytes[start_idx+6] !== 8'd20 || ttl_rx_bytes[start_idx+7] !== 8'd20) begin
- $fatal(1, "FAIL 1: default fan duty wrong on ttl_tx");
- end
- // Verify PTC default = 0% on all 4 channels
- if (ttl_rx_bytes[start_idx+8] !== 8'd0 || ttl_rx_bytes[start_idx+9] !== 8'd0 ||
- ttl_rx_bytes[start_idx+10] !== 8'd0 || ttl_rx_bytes[start_idx+11] !== 8'd0) begin
- $fatal(1, "FAIL 1: default ptc duty wrong on ttl_tx");
- end
- // Verify hood_switch=0, buzzer=0 defaults
- if (ttl_rx_bytes[start_idx+4+63] !== 8'd0 || ttl_rx_bytes[start_idx+4+64] !== 8'd0) begin
- $fatal(1, "FAIL 1: default hood/buzzer wrong on ttl_tx");
- end
- // Verify TX broadcast: rs232_tx must receive the exact same 77 bytes!
- wait(rs232_rx_count >= 77);
- for (i = 0; i < 77; i = i + 1) begin
- if (rs232_rx_bytes[i] !== ttl_rx_bytes[i]) begin
- $fatal(1, "FAIL 1: Broadcast mismatch at byte %0d: ttl=%02h, rs232=%02h",
- i, ttl_rx_bytes[i], rs232_rx_bytes[i]);
- end
- end
- $display(" -> Verified TX Broadcast: rs232_tx perfectly matches ttl_tx (77 bytes)!");
- $display(" -> TEST 1 PASSED!");
- $fflush;
- // --------------------------------------------------------------------
- // TEST 2: SET_ALL sent over host_ttl_rx (FAN 50%, PTC 30%, Axis 0 6 steps)
- // --------------------------------------------------------------------
- $display("[TEST 2] Sending SET_ALL over host_ttl_rx: FAN 50%%, PTC 30%%, Axis 0 PUSH_DOWN 6 steps...");
- $fflush;
- frame_bytes[0] = 8'hA5;
- frame_bytes[1] = 8'h5A;
- frame_bytes[2] = 8'h01; // SET_ALL
- frame_bytes[3] = 8'd68; // LEN 68
- // Payload 0..3: FAN 50%
- frame_bytes[4] = 8'd50; frame_bytes[5] = 8'd50; frame_bytes[6] = 8'd50; frame_bytes[7] = 8'd50;
- // Payload 4..7: PTC 30%
- frame_bytes[8] = 8'd30; frame_bytes[9] = 8'd30; frame_bytes[10] = 8'd30; frame_bytes[11] = 8'd30;
- // Axis 0: payload 8..12
- frame_bytes[12] = 8'h01; // action PUSH_DOWN
- frame_bytes[13] = 8'd6; // steps low = 6
- frame_bytes[14] = 8'd0; // steps high
- frame_bytes[15] = 8'd20; // half_period low = 20 us
- frame_bytes[16] = 8'd0; // half_period high
- // Axis 1..11: NOTHING
- for (i = 17; i < 4 + 68; i = i + 1) frame_bytes[i] = 8'd0;
- start_idx = ttl_rx_count;
- x_steps_count = 0;
- ttl_send_frame(72);
- // Expect SET_ACK (6 bytes: A5 5A 81 00 A6 35) on TTL TX
- wait(ttl_rx_count >= start_idx + 6);
- if (ttl_rx_bytes[start_idx+2] !== 8'h81 || ttl_rx_bytes[start_idx+3] !== 8'h00) begin
- $fatal(1, "FAIL 2: expected SET_ACK (81 00), got %02h %02h",
- ttl_rx_bytes[start_idx+2], ttl_rx_bytes[start_idx+3]);
- end
- $display(" -> Verified SET_ACK (81 00) on host_ttl_tx");
- $fflush;
- // Expect MOTOR_DONE (10 bytes) on TTL TX
- wait(ttl_rx_count >= start_idx + 6 + 10);
- start_idx = start_idx + 6;
- if (ttl_rx_bytes[start_idx+2] !== 8'h50 || ttl_rx_bytes[start_idx+3] !== 8'h04) begin
- $fatal(1, "FAIL 2: expected MOTOR_DONE (50 04), got %02h %02h",
- ttl_rx_bytes[start_idx+2], ttl_rx_bytes[start_idx+3]);
- end
- $display(" -> Got MOTOR_DONE: axis=%0d, reason=%0d, steps_done=%0d",
- ttl_rx_bytes[start_idx+4], ttl_rx_bytes[start_idx+5],
- {ttl_rx_bytes[start_idx+7], ttl_rx_bytes[start_idx+6]});
- $fflush;
- if (ttl_rx_bytes[start_idx+4] !== 8'd0) $fatal(1, "FAIL 2: wrong axis, expected 0");
- if (ttl_rx_bytes[start_idx+5] !== 8'd0) $fatal(1, "FAIL 2: wrong reason, expected 0 (complete)");
- if ({ttl_rx_bytes[start_idx+7], ttl_rx_bytes[start_idx+6]} !== 16'd6)
- $fatal(1, "FAIL 2: wrong steps_done in MOTOR_DONE, expected 6");
- if (x_steps_count !== 6)
- $fatal(1, "FAIL 2: physical pulse count mismatch on x_step pin: expected 6, got %0d", x_steps_count);
- $display(" -> Exactly 6 pulses emitted on x_step and completed normally!");
- $display(" -> TEST 2 PASSED!");
- $fflush;
- // --------------------------------------------------------------------
- // TEST 3: Interleaved commands between host_rs232_rx and host_ttl_rx
- // --------------------------------------------------------------------
- $display("[TEST 3] Interleaved communication test...");
- // 3A: Send GET_ALL via RS232 RX
- $display(" -> 3A: Sending GET_ALL via host_rs232_rx...");
- frame_bytes[0] = 8'hA5;
- frame_bytes[1] = 8'h5A;
- frame_bytes[2] = 8'h02;
- frame_bytes[3] = 8'h00;
- start_idx = ttl_rx_count;
- rs232_send_frame(4);
- wait(ttl_rx_count >= start_idx + 77);
- if (ttl_rx_bytes[start_idx+2] !== 8'h82) $fatal(1, "FAIL 3A: RS232 request didn't yield GET_ACK");
- $display(" -> 3A OK: RS232 request answered and broadcast on ttl_tx");
- // 3B: Send GET_ALL via TTL RX immediately afterwards
- $display(" -> 3B: Sending GET_ALL via host_ttl_rx...");
- start_idx = ttl_rx_count;
- ttl_send_frame(4);
- wait(ttl_rx_count >= start_idx + 77);
- if (ttl_rx_bytes[start_idx+2] !== 8'h82) $fatal(1, "FAIL 3B: TTL request didn't yield GET_ACK");
- $display(" -> 3B OK: TTL request answered and broadcast on ttl_tx");
- $display(" -> TEST 3 PASSED! Dual ports switch and operate seamlessly.");
- $fflush;
- // --------------------------------------------------------------------
- // TEST 4: Limit switch trip during move sent via host_ttl_rx
- // --------------------------------------------------------------------
- $display("[TEST 4] Limit trip test over TTL: Axis 1 moving, limit trips at step 3...");
- $fflush;
- frame_bytes[0] = 8'hA5;
- frame_bytes[1] = 8'h5A;
- frame_bytes[2] = 8'h01; // SET_ALL
- frame_bytes[3] = 8'd68; // LEN 68
- for (i = 4; i < 4 + 68; i = i + 1) frame_bytes[i] = 8'd0;
- frame_bytes[4] = 8'd20; frame_bytes[5] = 8'd20; frame_bytes[6] = 8'd20; frame_bytes[7] = 8'd20;
- // Axis 1: payload 13..17 (frame_bytes 17..21)
- frame_bytes[17] = 8'h01; // PUSH_DOWN
- frame_bytes[18] = 8'd40; // 40 steps
- frame_bytes[19] = 8'd0;
- frame_bytes[20] = 8'd25; // half_period = 25 us
- frame_bytes[21] = 8'd0;
- start_idx = ttl_rx_count;
- y_steps_count = 0;
- ttl_send_frame(72);
- // Wait for SET_ACK
- wait(ttl_rx_count >= start_idx + 6);
- $display(" -> SET_ACK received, motor Y running...");
- // Trip lower limit when y_steps_count reaches 3
- wait(y_steps_count == 3);
- @(negedge y_step);
- limit_lower_raw[1] = 1'b1; // assert lower limit on Axis 1
- $display(" -> Asserted Axis 1 lower limit at y_steps_count=3");
- // Expect MOTOR_DONE with reason=1 (lower limit)
- wait(ttl_rx_count >= start_idx + 6 + 10);
- start_idx = start_idx + 6;
- if (ttl_rx_bytes[start_idx+2] !== 8'h50 || ttl_rx_bytes[start_idx+4] !== 8'd1) begin
- $fatal(1, "FAIL 4: expected MOTOR_DONE for axis 1");
- end
- if (ttl_rx_bytes[start_idx+5] !== 8'd1) begin
- $fatal(1, "FAIL 4: expected reason=1 (lower limit), got %0d", ttl_rx_bytes[start_idx+5]);
- end
- $display(" -> Got MOTOR_DONE: axis=1, reason=1 (lower limit), steps_done=%0d",
- {ttl_rx_bytes[start_idx+7], ttl_rx_bytes[start_idx+6]});
- limit_lower_raw[1] = 1'b0; // release limit
- $display(" -> TEST 4 PASSED!");
- $fflush;
- // --------------------------------------------------------------------
- // TEST 5: STOP_CLEAR abort sent over host_ttl_rx
- // --------------------------------------------------------------------
- $display("[TEST 5] STOP_CLEAR test over TTL: start long move on Axis 0, then abort...");
- $fflush;
- frame_bytes[0] = 8'hA5;
- frame_bytes[1] = 8'h5A;
- frame_bytes[2] = 8'h01; // SET_ALL
- frame_bytes[3] = 8'd68; // LEN 68
- for (i = 4; i < 4 + 68; i = i + 1) frame_bytes[i] = 8'd0;
- frame_bytes[4] = 8'd20; frame_bytes[5] = 8'd20; frame_bytes[6] = 8'd20; frame_bytes[7] = 8'd20;
- // Axis 0: 500 steps
- frame_bytes[12] = 8'h01;
- frame_bytes[13] = 8'd244;
- frame_bytes[14] = 8'd1;
- frame_bytes[15] = 8'd30;
- frame_bytes[16] = 8'd0;
- start_idx = ttl_rx_count;
- x_steps_count = 0;
- ttl_send_frame(72);
- wait(ttl_rx_count >= start_idx + 6);
- $display(" -> Long move started on Axis 0...");
- // Wait until motor has made 5 steps
- wait(x_steps_count >= 5);
- $display(" -> Emitted %0d steps, issuing STOP_CLEAR (action=0xFF)...", x_steps_count);
- // Send STOP_CLEAR
- frame_bytes[0] = 8'hA5;
- frame_bytes[1] = 8'h5A;
- frame_bytes[2] = 8'h01;
- frame_bytes[3] = 8'd68;
- for (i = 4; i < 4 + 68; i = i + 1) frame_bytes[i] = 8'd0;
- frame_bytes[4] = 8'd20; frame_bytes[5] = 8'd20; frame_bytes[6] = 8'd20; frame_bytes[7] = 8'd20;
- frame_bytes[12] = 8'hFF; // STOP_CLEAR
- start_idx = ttl_rx_count;
- ttl_send_frame(72);
- wait(ttl_rx_count >= start_idx + 6);
- if (ttl_rx_bytes[start_idx+2] !== 8'h81) $fatal(1, "FAIL 5: expected SET_ACK for STOP_CLEAR");
- $display(" -> Received SET_ACK for STOP_CLEAR");
- // Verify motor stops and no more step pulses are produced
- i = x_steps_count;
- repeat (1000) @(posedge clk);
- if (x_steps_count !== i) $fatal(1, "FAIL 5: motor continued stepping after STOP_CLEAR!");
- $display(" -> Motor step pulses halted immediately at count %0d", x_steps_count);
- $display(" -> TEST 5 PASSED!");
- $fflush;
- // --------------------------------------------------------------------
- // TEST 6: Bad CRC corruption over host_ttl_rx
- // --------------------------------------------------------------------
- $display("[TEST 6] Bad CRC rejection & recovery test over TTL...");
- $fflush;
- start_idx = ttl_rx_count;
- // Send a frame with bad CRC
- ttl_send_byte(8'hA5);
- ttl_send_byte(8'h5A);
- ttl_send_byte(8'h02); // GET_ALL
- ttl_send_byte(8'h00);
- ttl_send_byte(8'hDE); // corrupted CRC
- ttl_send_byte(8'hAD);
- // Wait a timeout period
- repeat (3000) @(posedge clk);
- if (ttl_rx_count !== start_idx) begin
- $fatal(1, "FAIL 6: Corrupted frame should NOT produce any reply or ACK!");
- end
- $display(" -> Corrupted CRC frame correctly ignored (0 replies produced)");
- // Now send a valid GET_ALL to verify recovery
- start_idx = ttl_rx_count;
- frame_bytes[0] = 8'hA5;
- frame_bytes[1] = 8'h5A;
- frame_bytes[2] = 8'h02;
- frame_bytes[3] = 8'h00;
- ttl_send_frame(4);
- wait(ttl_rx_count >= start_idx + 77);
- if (ttl_rx_bytes[start_idx+2] !== 8'h82) begin
- $fatal(1, "FAIL 6: Link failed to recover after bad CRC frame!");
- end
- $display(" -> Subsequent valid GET_ALL succeeded with GET_ACK (82 00)");
- $display(" -> TEST 6 PASSED!");
- $fflush;
- // --------------------------------------------------------------------
- // TEST 7: Noise/glitch on host_ttl_rx does NOT lock out CON78 RS232
- // --------------------------------------------------------------------
- $display("[TEST 7] TTL glitch/noise immunity test for RS232 link...");
- $fflush;
- // Inject a glitch / floating pulse on TTL RX
- ttl_rx = 1'b0;
- repeat (10) @(posedge clk);
- ttl_rx = 1'b1;
- // Wait for owner_idle_counter to time out and release rx_owner
- repeat (3000) @(posedge clk);
- // Now issue SET_ALL via RS232 CON78 (69B including buzzer=1)
- $display(" -> Sending 69B SET_ALL on CON78 RS232 with 4 chamber duties: 50%%, 30%%, 70%%, 90%%, buzzer=1...");
- start_idx = rs232_rx_count;
- frame_bytes[0] = 8'hA5;
- frame_bytes[1] = 8'h5A;
- frame_bytes[2] = 8'h01; // SET_ALL
- frame_bytes[3] = 8'd69; // LEN 69
- for (i = 4; i < 4 + 69; i = i + 1) frame_bytes[i] = 8'd0;
- // FAN 0..3: 20%, 30%, 40%, 50%
- frame_bytes[4] = 8'd20; frame_bytes[5] = 8'd30; frame_bytes[6] = 8'd40; frame_bytes[7] = 8'd50;
- // Chamber 1..4 PTC: 50%, 30%, 70%, 90%
- frame_bytes[8] = 8'd50; frame_bytes[9] = 8'd30; frame_bytes[10] = 8'd70; frame_bytes[11] = 8'd90;
- frame_bytes[4 + 68] = 8'd1; // buzzer ON
- rs232_send_frame(73);
- wait(rs232_rx_count >= start_idx + 6);
- if (rs232_rx_bytes[start_idx+2] !== 8'h81) begin
- $fatal(1, "FAIL 7: CON78 RS232 failed to get SET_ACK after TTL glitch!");
- end
- $display(" -> CON78 RS232 successfully received SET_ACK (81 00) after TTL glitch!");
- $display(" -> TEST 7 PASSED!");
- $fflush;
- // --------------------------------------------------------------------
- // TEST 8: Verify all 24 physical PTC outputs driven across 4 chambers
- // --------------------------------------------------------------------
- $display("[TEST 8] 24-channel physical PTC output verification...");
- $fflush;
- // Advance into next PWM period so duties are active
- repeat (6000) @(posedge clk);
- // Sample ptc_pwm_out across multiple cycles
- for (i = 0; i < 100; i = i + 1) begin
- @(posedge clk);
- // Verify Chamber 1 (pins 0..5) all match
- if (ptc_pwm_out[0] !== ptc_pwm_out[1] || ptc_pwm_out[1] !== ptc_pwm_out[2] ||
- ptc_pwm_out[2] !== ptc_pwm_out[3] || ptc_pwm_out[3] !== ptc_pwm_out[4] ||
- ptc_pwm_out[4] !== ptc_pwm_out[5]) begin
- $fatal(1, "FAIL 8: Chamber 1 physical heaters mismatch: %b", ptc_pwm_out[5:0]);
- end
- // Verify Chamber 2 (pins 6..11) all match
- if (ptc_pwm_out[6] !== ptc_pwm_out[7] || ptc_pwm_out[7] !== ptc_pwm_out[8] ||
- ptc_pwm_out[8] !== ptc_pwm_out[9] || ptc_pwm_out[9] !== ptc_pwm_out[10] ||
- ptc_pwm_out[10] !== ptc_pwm_out[11]) begin
- $fatal(1, "FAIL 8: Chamber 2 physical heaters mismatch: %b", ptc_pwm_out[11:6]);
- end
- // Verify Chamber 3 (pins 12..17) all match
- if (ptc_pwm_out[12] !== ptc_pwm_out[13] || ptc_pwm_out[13] !== ptc_pwm_out[14] ||
- ptc_pwm_out[14] !== ptc_pwm_out[15] || ptc_pwm_out[15] !== ptc_pwm_out[16] ||
- ptc_pwm_out[16] !== ptc_pwm_out[17]) begin
- $fatal(1, "FAIL 8: Chamber 3 physical heaters mismatch: %b", ptc_pwm_out[17:12]);
- end
- // Verify Chamber 4 (pins 18..23) all match
- if (ptc_pwm_out[18] !== ptc_pwm_out[19] || ptc_pwm_out[19] !== ptc_pwm_out[20] ||
- ptc_pwm_out[20] !== ptc_pwm_out[21] || ptc_pwm_out[21] !== ptc_pwm_out[22] ||
- ptc_pwm_out[22] !== ptc_pwm_out[23]) begin
- $fatal(1, "FAIL 8: Chamber 4 physical heaters mismatch: %b", ptc_pwm_out[23:18]);
- end
- end
- $display(" -> All 24 physical PTC channels strictly track their respective chamber PWMs!");
- // Assert hood_switch_in, verify 4000Hz 80% buzzer waveform, and send GET_ALL on RS232
- hood_switch_in = 1'b1;
- repeat (20) @(posedge clk);
- begin : check_mr_ttl_bz
- integer bz_hi, bz_lo, bz_k;
- bz_hi = 0; bz_lo = 0;
- for (bz_k = 0; bz_k < 6250; bz_k = bz_k + 1) begin
- @(posedge clk);
- if (buzzer_out === 1'b1) bz_hi = bz_hi + 1;
- else if (buzzer_out === 1'b0) bz_lo = bz_lo + 1;
- end
- if (bz_hi !== 5000 || bz_lo !== 1250)
- $fatal(1, "FAIL 8: buzzer_out 4000Hz 80%% mismatch: high=%0d low=%0d", bz_hi, bz_lo);
- end
- start_idx = rs232_rx_count;
- frame_bytes[0] = 8'hA5;
- frame_bytes[1] = 8'h5A;
- frame_bytes[2] = 8'h02; // GET_ALL
- frame_bytes[3] = 8'h00;
- rs232_send_frame(4);
- wait(rs232_rx_count >= start_idx + 77);
- if (rs232_rx_bytes[start_idx+2] !== 8'h82) $fatal(1, "FAIL 8: expected GET_ACK on RS232");
- if (rs232_rx_bytes[start_idx+8] !== 8'h42 || rs232_rx_bytes[start_idx+9] !== 8'h41)
- $fatal(1, "FAIL 8: expected GET_DATA (65B) on RS232");
- // Check FAN duties (bytes 10..13)
- if (rs232_rx_bytes[start_idx+10] !== 8'd20 || rs232_rx_bytes[start_idx+11] !== 8'd30 ||
- rs232_rx_bytes[start_idx+12] !== 8'd40 || rs232_rx_bytes[start_idx+13] !== 8'd50) begin
- $fatal(1, "FAIL 8: GET_DATA FAN duties mismatch");
- end
- // Check Chamber PTC duties (bytes 14..17)
- if (rs232_rx_bytes[start_idx+14] !== 8'd50 || rs232_rx_bytes[start_idx+15] !== 8'd30 ||
- rs232_rx_bytes[start_idx+16] !== 8'd70 || rs232_rx_bytes[start_idx+17] !== 8'd90) begin
- $fatal(1, "FAIL 8: GET_DATA PTC duties mismatch");
- end
- if (rs232_rx_bytes[start_idx+10+63] !== 8'd1 || rs232_rx_bytes[start_idx+10+64] !== 8'd1) begin
- $fatal(1, "FAIL 8: GET_DATA hood/buzzer mismatch: hood=%h buzzer=%h",
- rs232_rx_bytes[start_idx+10+63], rs232_rx_bytes[start_idx+10+64]);
- end
- $display(" -> GET_DATA duties read back on RS232: FAN=[20,30,40,50], PTC=[50,30,70,90], hood=1, buzzer=1");
- $display(" -> TEST 8 PASSED!");
- $fflush;
- $display("==================================================");
- $display("=== ALL 8 HOST TTL & RS232 TESTS PASSED! ===");
- $display("prod2u_mrdimm_ttl_e2e_tb PASS");
- $display("==================================================");
- $fflush;
- $finish;
- end
- endmodule
|