| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390 |
- `timescale 1ns/1ps
- // UART-level end-to-end test for the mrdimm-chamber product stack over the
- // unified 16-bit-length protocol: real UART byte timing -> frame_rx ->
- // mrdimm_host_command -> mrdimm_output_core + motor_engine. Vectors are
- // generated by the independent Python codec (hth_protocol).
- module prod2u_mrdimm_uart_e2e_tb;
- localparam integer CLKS = 8;
- reg clk = 1'b0;
- reg rst_n = 1'b0;
- reg rx = 1'b1;
- reg tick_us = 1'b0;
- reg tick_1s = 1'b0;
- reg [2:0] limit_lower_raw = 3'b000;
- reg [2:0] limit_upper_raw = 3'b000;
- wire frame_valid, frame_ready, frame_accept_enable;
- wire [7:0] frame_type;
- wire [15:0] frame_length;
- wire [15:0] payload_rd_addr;
- wire [7:0] payload_rd_data;
- wire payload_wr_en;
- wire [15:0] payload_wr_addr;
- wire [7:0] payload_wr_data;
- wire thermal_wr_en, thermal_commit, thermal_commit_done, thermal_commit_ok;
- wire [2:0] thermal_wr_addr;
- wire [7:0] thermal_wr_data;
- wire keepalive_reload, snapshot_capture, snapshot_ready;
- wire qw_byte_we, qw_submit, qw_accept, ctl_valid, ctl_accept;
- wire [6:0] qw_byte_addr;
- wire [7:0] qw_byte_data, qw_status, ctl_status;
- wire [3:0] qw_count;
- wire [15:0] qw_detail, ctl_detail;
- wire [2:0] ctl_op;
- wire [15:0] ctl_axis_mask;
- wire [7:0] ctl_flags;
- wire [31:0] ctl_value;
- wire [7:0] ev_rd_index_w;
- wire ev_hold;
- wire [127:0] ev_rd_data;
- wire ev_view_ready;
- wire [7:0] ev_view_count;
- wire [7:0] ev_count, ev_overflow;
- wire [31:0] ev_time_us;
- wire ev_clear_n_w;
- wire [7:0] ev_clear_count_w;
- wire [7:0] status_byte_in;
- wire tx_start, tx_done;
- wire [7:0] tx_frame_type, tx_payload_data;
- wire [15:0] tx_payload_length;
- wire host_command_busy;
- wire [3:0] fan_on, ptc_on;
- wire [7:0] applied_fan0;
- wire [2:0] step_bus, dir_bus;
- wire host_response_done_w;
- integer commits, acks;
- reg in_data_phase;
- reg [15:0] data_len_seen;
- always #1 clk = ~clk;
- // Accelerated wall clock, but the production25 clocks per microsecond
- // and one-clock enable are preserved for motor/snapshot scheduling.
- integer tick_divider=0;
- always @(negedge clk)begin
- if(!rst_n)begin tick_divider=0;tick_us=0;end
- else if(tick_divider==24)begin tick_divider=0;tick_us=1;end
- else begin tick_divider=tick_divider+1;tick_us=0;end
- end
- uart_rx_byte #(.CLKS_PER_BIT(CLKS)) u_uart_rx (
- .clk(clk), .rst_n(rst_n), .rx_i(rx),
- .byte_valid(), .byte_data(), .framing_error()
- );
- // Byte-level feeder shared by frame_rx: reuse uart_rx_byte output via
- // explicit wires (declared above as internal names).
- wire uart_rx_valid_w;
- wire [7:0] uart_rx_data_w;
- wire uart_framing_error_w;
- uart_rx_byte #(.CLKS_PER_BIT(CLKS)) u_host_rx (
- .clk(clk), .rst_n(rst_n), .rx_i(rx),
- .byte_valid(uart_rx_valid_w), .byte_data(uart_rx_data_w),
- .framing_error(uart_framing_error_w)
- );
- frame_rx #(.MAX_PAYLOAD(256), .INTER_BYTE_TIMEOUT_CYCLES(400), .EXTERNAL_CRC(1)) u_frame_rx (
- .clk(clk), .rst_n(rst_n), .byte_valid(uart_rx_valid_w),
- .byte_data(uart_rx_data_w), .accept_enable(frame_accept_enable),
- .frame_valid(frame_valid), .frame_ready(frame_ready),
- .frame_type(frame_type), .frame_length(frame_length),
- .payload_read_addr(payload_rd_addr), .payload_read_data(payload_rd_data),
- .payload_wr_en(payload_wr_en), .payload_wr_addr(payload_wr_addr),
- .payload_wr_data(payload_wr_data),
- .crc_start_o(crc_start_w), .crc_data_valid_o(crc_dv_w), .crc_data_o(crc_byte_w),
- .crc_busy_i(host_crc_busy_w), .crc_data_done_i(crc_done_w), .crc_value_i(crc_val_w)
- );
- payload_ram #(.DEPTH(256), .ADDR_WIDTH(8)) u_payload (
- .clk(clk), .wr_en(payload_wr_en), .wr_addr(payload_wr_addr),
- .wr_data(payload_wr_data), .rd_addr(payload_rd_addr), .rd_data(payload_rd_data)
- );
- wire host_crc_busy_w, crc_start_w, crc_dv_w, crc_done_w;
- wire [7:0] crc_byte_w;
- wire [15:0] crc_val_w;
- crc16_serial u_crc (
- .clk(clk), .rst_n(rst_n),
- .start(crc_start_w), .data_valid(crc_dv_w),
- .data(crc_byte_w), .busy(host_crc_busy_w),
- .data_done(crc_done_w), .crc(crc_val_w)
- );
- mrdimm_host_command #(.SNAPSHOT_LENGTH(352)) dut (
- .clk(clk), .rst_n(rst_n),
- .frame_valid(frame_valid), .frame_ready(frame_ready),
- .frame_accept_enable(frame_accept_enable), .frame_type(frame_type),
- .frame_length(frame_length),
- .payload_read_addr(payload_rd_addr), .payload_read_data(payload_rd_data),
- .thermal_wr_en(thermal_wr_en), .thermal_wr_addr(thermal_wr_addr),
- .thermal_wr_data(thermal_wr_data), .thermal_commit(thermal_commit),
- .thermal_commit_done(thermal_commit_done), .thermal_commit_ok(thermal_commit_ok),
- .lease_active(lease_active_w), .keepalive_reload(keepalive_reload),
- .snapshot_capture(snapshot_capture), .snapshot_ready(snapshot_ready),
- .qw_byte_we(qw_byte_we), .qw_byte_addr(qw_byte_addr),
- .qw_byte_data(qw_byte_data), .qw_submit(qw_submit), .qw_count(qw_count),
- .qw_accept(qw_accept), .qw_status(qw_status), .qw_detail(qw_detail),
- .ctl_valid(ctl_valid), .ctl_op(ctl_op), .ctl_axis_mask(ctl_axis_mask),
- .ctl_flags(ctl_flags), .ctl_value(ctl_value),
- .ctl_accept(ctl_accept), .ctl_status(ctl_status), .ctl_detail(ctl_detail),
- .ev_rd_index(ev_rd_index_w), .ev_hold(ev_hold),
- .ev_rd_data(ev_rd_data), .ev_count(ev_count), .ev_overflow(ev_overflow),
- .ev_view_ready(ev_view_ready), .ev_view_count(ev_view_count),
- .ev_clear_n(ev_clear_n_w), .ev_clear_count(ev_clear_count_w),
- .status_byte_in(status_byte_in),
- .tx_payload_addr(u_frame_tx.payload_addr),
- .ack_payload_data(), .tx_payload_data(tx_payload_data),
- .tx_start(tx_start), .tx_frame_type(tx_frame_type),
- .tx_payload_length(tx_payload_length), .tx_done(host_response_done_w),
- .transaction_done(), .busy(host_command_busy)
- );
- wire lease_active_w;
- wire [7:0] applied_fan [0:3];
- wire [7:0] applied_ptc [0:3];
- wire [7:0] watchdog_remaining_w;
- wire [31:0] applied_fan_packed;
- assign applied_fan[0]=applied_fan_packed[0*8 +:8];
- assign applied_fan[1]=applied_fan_packed[1*8 +:8];
- assign applied_fan[2]=applied_fan_packed[2*8 +:8];
- assign applied_fan[3]=applied_fan_packed[3*8 +:8];
- wire [31:0] applied_ptc_packed;
- assign applied_ptc[0]=applied_ptc_packed[0*8 +:8];
- assign applied_ptc[1]=applied_ptc_packed[1*8 +:8];
- assign applied_ptc[2]=applied_ptc_packed[2*8 +:8];
- assign applied_ptc[3]=applied_ptc_packed[3*8 +:8];
- mrdimm_output_core #(
- .LEASE_SECONDS(8'd30), .PWM_PERIOD_CLKS(1000), .PWM_CLKS_PER_PERCENT(10)
- ) u_output (
- .clk(clk), .rst_n(rst_n), .tick_1s(tick_1s),
- .wr_en(thermal_wr_en), .wr_addr(thermal_wr_addr), .wr_data(thermal_wr_data),
- .commit(thermal_commit), .commit_done(thermal_commit_done),
- .commit_ok(thermal_commit_ok), .keepalive_reload(keepalive_reload),
- .applied_fan(applied_fan_packed), .applied_ptc(applied_ptc_packed),
- .manual_active(), .lease_active(lease_active_w),
- .watchdog_remaining(watchdog_remaining_w),
- .fan_pwm_on(fan_on), .ptc_pwm_on(ptc_on)
- );
- wire [1535:0] axis_telemetry;
- wire [4:0] status_word_addr;
- wire [3:0] status_axis;
- wire [15:0] status_word_data;
- wire [31:0] unused_time;
- motor_engine #(
- .AXIS_COUNT(3),
- .X_DEFAULT_HALF_US(32'd500), .Y_DEFAULT_HALF_US(32'd500), .Z_DEFAULT_HALF_US(32'd20000),
- .X_MIN_HALF_US(32'd500), .Y_MIN_HALF_US(32'd500), .Z_MIN_HALF_US(32'd20000),
- .DIR_SETUP_TICKS(1000)
- ) u_motor (
- .clk(clk), .rst_n(rst_n), .tick_us(tick_us),
- .limit_lower_raw(limit_lower_raw), .limit_upper_raw(limit_upper_raw),
- .qw_byte_we(qw_byte_we), .qw_byte_addr(qw_byte_addr),
- .qw_byte_data(qw_byte_data), .qw_submit(qw_submit), .qw_count(qw_count),
- .qw_accept(qw_accept), .qw_status(qw_status), .qw_detail(qw_detail),
- .ctl_valid(ctl_valid), .ctl_op(ctl_op), .ctl_axis_mask(ctl_axis_mask),
- .ctl_flags(ctl_flags), .ctl_value(ctl_value),
- .ctl_accept(ctl_accept), .ctl_status(ctl_status), .ctl_detail(ctl_detail),
- .startup_seek(1'b0),
- .ev_rd_index(ev_rd_index_w), .ev_hold(ev_hold),
- .ev_rd_data(ev_rd_data), .ev_count(ev_count), .ev_overflow(ev_overflow),
- .ev_view_ready(ev_view_ready), .ev_view_count(ev_view_count),
- .ev_time_us(unused_time),
- .ev_clear_n(ev_clear_n_w), .ev_clear_count(ev_clear_count_w),
- .axis_telemetry(axis_telemetry),
- .telemetry_word_addr(status_word_addr),.telemetry_axis(status_axis),.telemetry_word_data(status_word_data),
- .step_out(step_bus), .dir_out(dir_bus)
- );
- mrdimm_snapshot_mux #(.SCANNER_WORD_PORT(1)) u_snapshot (
- .clk(clk),.rst_n(rst_n),.capture(snapshot_capture),.source_ready(u_motor.motor_telemetry_ready),.source_epoch(u_motor.motor_status_epoch),.ready(snapshot_ready),.locked(),
- .bmc_locked_bank(),.ct_locked_bank(),
- .fan_applied({applied_fan[3],applied_fan[2],applied_fan[1],applied_fan[0]}),
- .ptc_applied({applied_ptc[3],applied_ptc[2],applied_ptc[1],applied_ptc[0]}),
- .fan_requested({applied_fan[3],applied_fan[2],applied_fan[1],applied_fan[0]}),
- .ptc_requested({applied_ptc[3],applied_ptc[2],applied_ptc[1],applied_ptc[0]}),
- .watchdog_active(lease_active_w),.watchdog_remaining(watchdog_remaining_w),
- .bmc_active_bank(1'b0),.ct_active_bank(1'b0),.bmc_stale(1'b1),.ct_stale(1'b1),
- .bmc_epoch(16'd0),.ct_epoch(16'd0),.bmc_age(16'd0),.ct_age(16'd0),
- .bmc_valid_mask(24'd0),.ct_valid_mask(27'd0),.bmc_rd_index(),.ct_rd_index(),
- .bmc_rd_raw(16'd0),.ct_rd_raw(16'd0),.axis_data(axis_telemetry),
- .scanner_axis(status_axis),.scanner_word_data(status_word_data),.scanner_word_addr(status_word_addr),
- .time_us(unused_time),.time_high(32'd0),.event_overflow(32'd0),
- .event_pending_overflow(32'd0),.ev_count(ev_count),.global_flags(32'd0),
- .read_addr(frame_tx_payload_addr_w[9:0]),.read_data(status_byte_in)
- );
- // frame_tx consumes the command core payload for ACK/DATA lengths.
- wire frame_tx_done_w, frame_tx_busy_w, frame_tx_byte_valid_w;
- wire [7:0] frame_tx_byte_data_w;
- wire [15:0] frame_tx_payload_addr_w;
- frame_tx #(.PAYLOAD_IDX_BITS(10), .EXTERNAL_CRC(0), .PAYLOAD_READ_LATENCY(1)) u_frame_tx (
- .clk(clk), .rst_n(rst_n), .start(tx_start), .frame_type(tx_frame_type),
- .payload_length(tx_payload_length), .busy(frame_tx_busy_w),
- .byte_valid(frame_tx_byte_valid_w), .byte_data(frame_tx_byte_data_w),
- .byte_ready(1'b1), .payload_addr(frame_tx_payload_addr_w),
- .payload_data(tx_payload_data), .done(frame_tx_done_w),
- .crc_start_o(), .crc_data_valid_o(), .crc_data_o(),
- .crc_busy_i(1'b0), .crc_data_done_i(), .crc_value_i()
- );
- assign host_response_done_w = frame_tx_done_w;
- // ---- UART byte feeder (shared for all frames) ----
- task send_byte;
- input [7:0] value;
- integer k;
- begin
- rx = 1'b0;
- repeat (CLKS) @(posedge clk);
- for (k = 0; k < 8; k = k + 1) begin
- rx = value[k];
- repeat (CLKS) @(posedge clk);
- end
- rx = 1'b1;
- repeat (CLKS) @(posedge clk);
- end
- endtask
- initial begin
- commits = 0; acks = 0;
- forever begin
- @(posedge clk);
- if (thermal_commit) commits = commits + 1;
- if (tx_start && tx_frame_type==8'h80) begin
- acks = acks + 1;
- if (tx_frame_type !== 8'h80)
- $fatal(1, "frame %02x is not an ACK", tx_frame_type);
- if (tx_payload_length !== 16'd4)
- $fatal(1, "ACK payload length %0d", tx_payload_length);
- end
- end
- end
- initial begin
- wait (rst_n);
- // ---- SET_ALL fan20/ptc0, endpoints FF (no motor touch) ----
- send_byte(8'hA5); send_byte(8'h5A); send_byte(8'h01);
- send_byte(8'h0B); send_byte(8'h00);
- send_byte(8'h14); send_byte(8'h14); send_byte(8'h14); send_byte(8'h14);
- send_byte(8'h00); send_byte(8'h00); send_byte(8'h00); send_byte(8'h00);
- send_byte(8'hFF); send_byte(8'hFF); send_byte(8'hFF);
- send_byte(8'h89); send_byte(8'h04);
- repeat (2000) @(posedge clk);
- if (commits !== 1) $fatal(1, "SET_ALL commits=%0d", commits);
- if (acks !== 1) $fatal(1, "SET_ALL acks=%0d", acks);
- if (applied_fan[0] !== 8'd20) $fatal(1, "fan duty not applied");
- if (u_motor.ax_active[0] !== 1'b0)
- $fatal(1, "FF endpoint must not touch motors");
- // ---- SET_ALL with X=lower endpoint: commit + engine accepted ----
- send_byte(8'hA5); send_byte(8'h5A); send_byte(8'h01);
- send_byte(8'h0B); send_byte(8'h00);
- send_byte(8'h14); send_byte(8'h14); send_byte(8'h14); send_byte(8'h14);
- send_byte(8'h00); send_byte(8'h00); send_byte(8'h00); send_byte(8'h00);
- send_byte(8'hFF); send_byte(8'h00); send_byte(8'hFF);
- send_byte(8'h76); send_byte(8'h07);
- repeat (3000) @(posedge clk);
- if (commits !== 2) $fatal(1, "X-low commits=%0d", commits);
- if (u_motor.ax_active[0] !== 1'b1)
- $fatal(1, "X seek not accepted");
- if (u_motor.u_axes.r_mode[0] !== 3'd1)
- $fatal(1, "X must run seek_lower");
- // ---- bad CRC: silent ----
- send_byte(8'hA5); send_byte(8'h5A); send_byte(8'h01);
- send_byte(8'h0B); send_byte(8'h00);
- send_byte(8'h14); send_byte(8'h14); send_byte(8'h14); send_byte(8'h14);
- send_byte(8'h00); send_byte(8'h00); send_byte(8'h00); send_byte(8'h00);
- send_byte(8'hFF); send_byte(8'hFF); send_byte(8'hFF);
- send_byte(8'h89); send_byte(8'hFB);
- repeat (1000) @(posedge clk);
- if (commits !== 2) $fatal(1, "bad CRC committed");
- if (acks !== 2) $fatal(1, "bad CRC acked");
- // ---- MOTOR_QUEUE_WRITE with count=0: BAD_LENGTH NACK ----
- send_byte(8'hA5); send_byte(8'h5A); send_byte(8'h03);
- send_byte(8'h11); send_byte(8'h00);
- for (int j = 0; j < 17; j = j + 1) send_byte(8'h00);
- send_byte(8'h2F); send_byte(8'h46);
- repeat (2000) @(posedge clk);
- if (acks !== 3) $fatal(1, "QW acks=%0d", acks);
- if (commits !== 2) $fatal(1, "QW must not commit");
- // ---- KEEPALIVE contract vector: ACK only ----
- send_byte(8'hA5); send_byte(8'h5A); send_byte(8'h05);
- send_byte(8'h00); send_byte(8'h00);
- send_byte(8'h6C); send_byte(8'h27);
- repeat (500) @(posedge clk);
- if (acks !== 4) $fatal(1, "keepalive acks=%0d", acks);
- if (commits !== 2) $fatal(1, "keepalive must not commit");
- // ---- GET_ALL while a real seek is active: ACK then81 DATA(352B) ----
- wait(step_bus[0]);
- send_byte(8'hA5); send_byte(8'h5A); send_byte(8'h02);
- send_byte(8'h00); send_byte(8'h00);
- send_byte(8'hFC); send_byte(8'hA2);
- wait(response_checks==6);@(negedge clk);
- if (data_len_seen !== 16'd352) $fatal(1, "ALL_STATUS length %0d", data_len_seen);
- if (response_checks!==6) $fatal(1,"incomplete serialized responses: %0d",response_checks);
- // Abort the running X seek, then read its stable event through 80/83.
- send_byte(8'hA5);send_byte(8'h5A);send_byte(8'h04);send_byte(8'h08);send_byte(0);
- send_byte(1);send_byte(1);repeat(6) send_byte(0);send_byte(8'hB1);send_byte(8'h30);
- repeat(1000) @(posedge clk);
- if(u_motor.ax_active[0] || acks!=6) $fatal(1,"control abort transaction");
- send_byte(8'hA5);send_byte(8'h5A);send_byte(8'h08);send_byte(1);send_byte(0);
- send_byte(8);send_byte(8'h3B);send_byte(8'hB7);
- repeat(2000) @(posedge clk);
- if(response_checks!=9 || acks!=7) $fatal(1,"EVENT_READ did not complete ACK plus DATA");
- send_byte(8'hA5);send_byte(8'h5A);send_byte(4);send_byte(8);send_byte(0);
- send_byte(8);send_byte(1);repeat(6)send_byte(0);send_byte(8'hB8);send_byte(8'h69);
- repeat(1000)@(posedge clk);
- if(response_checks!=10 || acks!=8)$fatal(1,"invalid MOTOR_CONTROL did not NACK");
- $display("prod2u_mrdimm_uart_e2e_tb PASS");
- $finish;
- end
- // track the DATA phase length of GET_ALL responses
- always @(posedge clk) begin
- if (tx_start && tx_frame_type == 8'h81) data_len_seen <= tx_payload_length;
- end
- // Independently inspect the actual serializer bytes. Counting tx_start
- // alone would miss a one-bit payload bus or DATA being sent in an ACK.
- reg [7:0] response_bytes [0:1023];
- integer response_count=0, response_checks=0, rb, bitno;
- reg [15:0] crc_check;
- always @(posedge clk) begin
- if (frame_tx_byte_valid_w) begin
- response_bytes[response_count]=frame_tx_byte_data_w;
- response_count=response_count+1;
- end
- if (frame_tx_done_w) begin
- if(response_count != 7+{response_bytes[4],response_bytes[3]})
- $fatal(1,"response length does not match wire bytes");
- crc_check=16'hFFFF;
- for(rb=2;rb<response_count-2;rb=rb+1) begin
- crc_check=crc_check ^ {response_bytes[rb],8'd0};
- for(bitno=0;bitno<8;bitno=bitno+1)
- crc_check=crc_check[15] ? (crc_check<<1)^16'h1021 : crc_check<<1;
- end
- if(crc_check !== {response_bytes[response_count-1],response_bytes[response_count-2]})
- $fatal(1,"response CRC mismatch");
- if(response_bytes[2]==8'h80) begin
- if(response_bytes[5] !== ((acks<=2) ? 8'h01 : (acks==3 ? 8'h03 : (acks==4 ? 8'h05 : (acks==5 ? 8'h02 : (acks==7 ? 8'h08 : 8'h04))))))
- $fatal(1,"ACK request type corrupted: %02x",response_bytes[5]);
- if(response_bytes[6] !== (acks==3 ? 8'd2 : (acks==8 ? 8'd3 : 8'd0)))
- $fatal(1,"ACK business status corrupted: %02x",response_bytes[6]);
- end else if(response_bytes[2]==8'h81) begin
- if(acks!=5) $fatal(1,"DATA preceded its ACK");
- end else if(response_bytes[2]==8'h83) begin
- if(acks!=7 || response_bytes[5]!=2 || response_bytes[10]!=8'hFF || response_bytes[11]!=6 ||
- response_bytes[26]!=0 || response_bytes[27]!=4)
- $fatal(1,"EVENT payload count/axis/code corruption");
- end else $fatal(1,"unexpected response type");
- response_checks=response_checks+1;response_count=0;
- end
- end
- initial begin #1000000; $fatal(1,"MRDIMM e2e deadline");end
- initial begin
- clk = 1'b0;
- repeat (4) @(posedge clk);
- rst_n = 1'b1;
- end
- endmodule
|