|
|
@@ -0,0 +1,532 @@
|
|
|
+`timescale 1ns/1ps
|
|
|
+
|
|
|
+// Full E2E bit-level simulation testbench for MRDIMM Host TTL Interface (CON140: T11=TX, P13=RX).
|
|
|
+//
|
|
|
+// 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 [2:0] limit_lower_raw = 3'd0;
|
|
|
+ reg [2:0] limit_upper_raw = 3'd0;
|
|
|
+
|
|
|
+ wire [7:0] ptc_pwm_out;
|
|
|
+ wire [3:0] fan_pwm_out;
|
|
|
+ wire motor_en_n;
|
|
|
+ wire x_step, x_dir;
|
|
|
+ wire y_step, y_dir;
|
|
|
+ wire z_step, z_dir;
|
|
|
+
|
|
|
+ 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),
|
|
|
+ .x_step(x_step), .x_dir(x_dir),
|
|
|
+ .y_step(y_step), .y_dir(y_dir),
|
|
|
+ .z_step(z_step), .z_dir(z_dir)
|
|
|
+ );
|
|
|
+
|
|
|
+ // 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: CON140 (T11=TX, P13=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 (69 bytes: A5 5A 42 3F ... CRC)
|
|
|
+ // Total bytes = 75 bytes
|
|
|
+ wait(ttl_rx_count >= start_idx + 75);
|
|
|
+ $display(" -> Received 75 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'h3F) 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 TX broadcast: rs232_tx must receive the exact same 75 bytes!
|
|
|
+ wait(rs232_rx_count >= 75);
|
|
|
+ for (i = 0; i < 75; 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 (75 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 + 75);
|
|
|
+ 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 + 75);
|
|
|
+ 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 + 75);
|
|
|
+ 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;
|
|
|
+
|
|
|
+ $display("==================================================");
|
|
|
+ $display("=== ALL 6 HOST TTL TESTS PASSED SUCCESSFULLY! ===");
|
|
|
+ $display("prod2u_mrdimm_ttl_e2e_tb PASS");
|
|
|
+ $display("==================================================");
|
|
|
+ $fflush;
|
|
|
+ $finish;
|
|
|
+ end
|
|
|
+endmodule
|