| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383 |
- `timescale 1ns/1ps
- // Independent, wire-level endpoint regression. The sender and monitor use
- // exactly 1e9 / BAUD ns, not the DUT's integer divider. A 25 MHz clock is
- // supplied to the real production endpoint, UARTs, frame codecs and CRCs.
- // No DUT state is forced or used to calculate expected bytes or completion.
- module host_uart_endpoint_nominal_case #(
- parameter integer BAUD = 921600
- ) (output reg done);
- localparam integer DUT_DIVIDER = (25000000 + BAUD/2) / BAUD;
- localparam real BIT_NS = 1000000000.0 / BAUD;
- reg clk = 0;
- always #20 clk = ~clk;
- reg rst_n = 0;
- reg rs232_rx = 1, ttl_rx = 1;
- wire rs232_tx, ttl_tx;
- wire req_valid;
- reg req_ready = 0;
- wire [7:0] req_type, req_length;
- wire req_wr_en;
- wire [7:0] req_wr_addr, req_wr_data, req_rd_addr;
- reg [7:0] req_rd_data;
- reg tx_start = 0;
- reg [7:0] tx_type = 0, tx_length = 0;
- wire tx_busy, tx_done;
- wire [7:0] tx_rd_addr;
- reg [7:0] tx_rd_data;
- reg [7:0] req_mem [0:255];
- reg [7:0] tx_mem [0:255];
- reg [7:0] request_payload [0:127];
- reg [7:0] expected_tx [0:255];
- integer expected_tx_count = 0;
- integer captured_tx_count = 0;
- integer tx_done_count = 0;
- integer next_done_count = 0;
- integer accepted_count = 0;
- integer reply_count = 0;
- reg checking_tx = 0;
- reg previous_tx_done = 0;
- realtime measured_bit_ns = 0.0;
- realtime final_stop_end_ns = 0.0;
- realtime byte_start_ns;
- reg [7:0] captured_byte;
- integer bit_number;
- integer i, axis;
- host_uart_endpoint #(
- .CLKS_PER_BIT(DUT_DIVIDER),
- .INTER_BYTE_TIMEOUT_CYCLES(250000),
- .MAX_PAYLOAD(128), .ADDR_WIDTH(8)
- ) dut (
- .clk(clk), .rst_n(rst_n),
- .rs232_rx(rs232_rx), .rs232_tx(rs232_tx),
- .ttl_rx(ttl_rx), .ttl_tx(ttl_tx),
- .req_valid(req_valid), .req_ready(req_ready),
- .req_type(req_type), .req_length(req_length),
- .req_ram_wr_en(req_wr_en), .req_ram_wr_addr(req_wr_addr),
- .req_ram_wr_data(req_wr_data),
- .req_ram_rd_addr(req_rd_addr), .req_ram_rd_data(req_rd_data),
- .tx_start(tx_start), .tx_type(tx_type), .tx_length(tx_length),
- .tx_busy(tx_busy), .tx_done(tx_done),
- .tx_ram_rd_addr(tx_rd_addr), .tx_ram_rd_data(tx_rd_data)
- );
- // Real one-cycle synchronous memory semantics, including undefined
- // contents before a write. The test never assumes power-on RAM zeros.
- always @(posedge clk) begin
- if (req_wr_en) req_mem[req_wr_addr] <= req_wr_data;
- req_rd_data <= req_mem[req_rd_addr];
- tx_rd_data <= tx_mem[tx_rd_addr];
- if (rst_n && req_valid && req_ready)
- accepted_count = accepted_count + 1;
- end
- function [15:0] reference_crc_byte;
- input [15:0] prior;
- input [7:0] value;
- reg [15:0] result;
- integer b;
- begin
- result = prior ^ {value, 8'h00};
- for (b = 0; b < 8; b = b + 1)
- result = result[15] ? ((result << 1) ^ 16'h1021)
- : (result << 1);
- reference_crc_byte = result;
- end
- endfunction
- task automatic set_rx;
- input integer port_number;
- input value;
- begin
- if (port_number == 0) rs232_rx = value;
- else ttl_rx = value;
- end
- endtask
- task automatic send_byte;
- input integer port_number;
- input [7:0] value;
- integer b;
- begin
- set_rx(port_number, 0);
- #(BIT_NS);
- for (b = 0; b < 8; b = b + 1) begin
- set_rx(port_number, value[b]);
- #(BIT_NS);
- end
- set_rx(port_number, 1);
- #(BIT_NS);
- end
- endtask
- task automatic send_request;
- input integer port_number;
- input [7:0] kind;
- input integer count;
- reg [15:0] crc;
- integer p;
- begin
- crc = reference_crc_byte(16'hffff, kind);
- crc = reference_crc_byte(crc, count[7:0]);
- send_byte(port_number, 8'ha5);
- send_byte(port_number, 8'h5a);
- send_byte(port_number, kind);
- send_byte(port_number, count[7:0]);
- for (p = 0; p < count; p = p + 1) begin
- send_byte(port_number, request_payload[p]);
- crc = reference_crc_byte(crc, request_payload[p]);
- end
- send_byte(port_number, crc[7:0]);
- send_byte(port_number, crc[15:8]);
- end
- endtask
- task automatic accept_request;
- input [7:0] kind;
- input integer count;
- integer p, wait_cycles;
- begin
- wait_cycles = 0;
- while (!req_valid && wait_cycles < 1000) begin
- @(negedge clk);
- wait_cycles = wait_cycles + 1;
- end
- if (!req_valid || req_type !== kind || req_length !== count[7:0])
- $fatal(1, "baud=%0d bad request valid=%b type=%h length=%0d expected=%h/%0d",
- BAUD, req_valid, req_type, req_length, kind, count);
- for (p = 0; p < count; p = p + 1)
- if (req_mem[p] !== request_payload[p])
- $fatal(1, "baud=%0d request payload[%0d]=%h expected=%h",
- BAUD, p, req_mem[p], request_payload[p]);
- @(negedge clk);
- req_ready = 1;
- @(negedge clk);
- req_ready = 0;
- repeat (5) @(negedge clk);
- if (req_valid) $fatal(1, "baud=%0d duplicate request publication", BAUD);
- end
- endtask
- task automatic begin_reply;
- input [7:0] kind;
- input integer count;
- reg [15:0] crc;
- integer p;
- begin
- if (tx_busy || checking_tx)
- $fatal(1, "baud=%0d test attempted overlapping reply frames", BAUD);
- expected_tx[0] = 8'ha5;
- expected_tx[1] = 8'h5a;
- expected_tx[2] = kind;
- expected_tx[3] = count[7:0];
- crc = reference_crc_byte(16'hffff, kind);
- crc = reference_crc_byte(crc, count[7:0]);
- for (p = 0; p < count; p = p + 1) begin
- expected_tx[4+p] = tx_mem[p];
- crc = reference_crc_byte(crc, tx_mem[p]);
- end
- expected_tx[4+count] = crc[7:0];
- expected_tx[5+count] = crc[15:8];
- // Published protocol golden values are independent sanity
- // anchors for the reference implementation itself.
- if (kind == 8'h82 && count == 0 && crc !== 16'h60f5)
- $fatal(1, "GET_ACK golden CRC mismatch in test reference");
- if (kind == 8'h81 && count == 0 && crc !== 16'h35a6)
- $fatal(1, "SET_ACK golden CRC mismatch in test reference");
- expected_tx_count = count + 6;
- captured_tx_count = 0;
- final_stop_end_ns = 0.0;
- measured_bit_ns = 0.0;
- next_done_count = tx_done_count + 1;
- checking_tx = 1;
- reply_count = reply_count + 1;
- @(negedge clk);
- tx_type = kind;
- tx_length = count[7:0];
- tx_start = 1;
- @(negedge clk);
- tx_start = 0;
- end
- endtask
- task automatic finish_reply;
- integer wait_cycles;
- begin
- wait_cycles = 0;
- while (tx_done_count < next_done_count && wait_cycles < 300000) begin
- @(negedge clk);
- wait_cycles = wait_cycles + 1;
- end
- if (tx_done_count !== next_done_count ||
- captured_tx_count !== expected_tx_count || tx_busy)
- $fatal(1, "baud=%0d incomplete reply done=%0d target=%0d bytes=%0d/%0d busy=%b",
- BAUD, tx_done_count, next_done_count,
- captured_tx_count, expected_tx_count, tx_busy);
- checking_tx = 0;
- repeat (5) @(negedge clk);
- end
- endtask
- // Independent UART decoder. It samples at nominal receiver baud.
- // Only for the physical-stop completion assertion, measure the start
- // bit directly from the first 0xa5 byte (data bit 0 is high). This
- // permits integer-divider baud error without assuming its bit length.
- initial begin
- forever begin
- @(negedge rs232_tx);
- if (rst_n) begin
- if (!checking_tx || captured_tx_count >= expected_tx_count)
- $fatal(1, "baud=%0d unexpected TX byte", BAUD);
- byte_start_ns = $realtime;
- if (captured_tx_count == 0) begin
- @(posedge rs232_tx);
- measured_bit_ns = $realtime - byte_start_ns;
- if (measured_bit_ns < BIT_NS * 0.98 ||
- measured_bit_ns > BIT_NS * 1.02)
- $fatal(1, "baud=%0d TX bit duration %0.3f ns outside nominal tolerance", BAUD, measured_bit_ns);
- #(1.5 * BIT_NS - measured_bit_ns);
- end else begin
- #(1.5 * BIT_NS);
- end
- for (bit_number = 0; bit_number < 8; bit_number = bit_number + 1) begin
- captured_byte[bit_number] = rs232_tx;
- #(BIT_NS);
- end
- if (rs232_tx !== 1'b1)
- $fatal(1, "baud=%0d TX missing stop bit, byte=%0d", BAUD, captured_tx_count);
- if (captured_byte !== expected_tx[captured_tx_count])
- $fatal(1, "baud=%0d TX byte[%0d]=%h expected=%h",
- BAUD, captured_tx_count, captured_byte, expected_tx[captured_tx_count]);
- captured_tx_count = captured_tx_count + 1;
- if (captured_tx_count == expected_tx_count)
- final_stop_end_ns = byte_start_ns + 10.0 * measured_bit_ns;
- end
- end
- end
- // Observe after NBA updates, so the completion pulse and pins belong
- // to the same edge. One measured full stop bit must have elapsed.
- always @(posedge clk) begin
- #1;
- if (rst_n) begin
- if (rs232_tx !== ttl_tx)
- $fatal(1, "baud=%0d RS232/TTL broadcast diverged", BAUD);
- if (tx_done) begin
- if (previous_tx_done || !checking_tx ||
- captured_tx_count != expected_tx_count || final_stop_end_ns <= 0.0)
- $fatal(1, "baud=%0d premature/duplicate tx_done", BAUD);
- if ($realtime + 0.001 < final_stop_end_ns)
- $fatal(1, "baud=%0d tx_done precedes full final stop: now=%0.3f deadline=%0.3f",
- BAUD, $realtime, final_stop_end_ns);
- if ($realtime > final_stop_end_ns + 161.0)
- $fatal(1, "baud=%0d completion delayed more than four clocks", BAUD);
- if (tx_busy || rs232_tx !== 1'b1)
- $fatal(1, "baud=%0d tx_done while wire/serializer busy", BAUD);
- tx_done_count = tx_done_count + 1;
- end
- previous_tx_done = tx_done;
- end else begin
- previous_tx_done = 0;
- end
- end
- always @(rs232_tx or ttl_tx) begin
- #0.001;
- if (rst_n && rs232_tx !== ttl_tx)
- $fatal(1, "baud=%0d TX broadcast differs between clock samples", BAUD);
- end
- initial begin
- done = 0;
- if (reference_crc_byte(reference_crc_byte(16'hffff, 8'h02), 8'h00) !== 16'h7b6d)
- $fatal(1, "GET request golden CRC mismatch in test reference");
- repeat (10) @(negedge clk);
- rst_n = 1;
- #(2.37 * BIT_NS); // unrelated phase to the 25 MHz clock
- // RS232 GET; full GET_ACK followed by a real-length 83-byte DATA.
- send_request(0, 8'h02, 0);
- accept_request(8'h02, 0);
- begin_reply(8'h82, 0);
- finish_reply;
- for (i = 0; i < 83; i = i + 1)
- tx_mem[i] = (i * 29 + 37) & 255;
- tx_mem[32] = 0;
- tx_mem[55] = 8'hff;
- tx_mem[82] = 8'hff;
- for (i = 0; i < 32; i = i + 1)
- request_payload[i] = (i * 11 + 3) % 101;
- begin_reply(8'h42, 83);
- // RX and TX CRC streams intentionally overlap. RX uses nominal
- // wire timing and a different payload from the TX synchronous RAM.
- #(3.23 * BIT_NS);
- send_request(1, 8'h01, 32);
- accept_request(8'h01, 32);
- finish_reply;
- begin_reply(8'h81, 0);
- finish_reply;
- // RS232 full 68-byte MRDIMM SET, with valid finite axis slots and
- // embedded A5 5A in a step count. The endpoint must preserve it.
- for (i = 0; i < 8; i = i + 1)
- request_payload[i] = (i * 13 + 7) % 101;
- for (axis = 0; axis < 12; axis = axis + 1) begin
- request_payload[8 + axis*5] = (axis % 2) ? 8'h02 : 8'h01;
- request_payload[9 + axis*5] = 8'ha5 - axis;
- request_payload[10 + axis*5] = 8'h5a;
- request_payload[11 + axis*5] = 20 + axis;
- request_payload[12 + axis*5] = 0;
- end
- #(1.19 * BIT_NS);
- send_request(0, 8'h01, 68);
- accept_request(8'h01, 68);
- begin_reply(8'h81, 0);
- finish_reply;
- send_request(1, 8'h02, 0);
- accept_request(8'h02, 0);
- begin_reply(8'h82, 0);
- finish_reply;
- // A lone sync byte must time out in WAIT_5A and release ownership.
- // The next complete request comes from the other physical port.
- // Use the actual production 250000-clock (10 ms) gap, with only a
- // short external guard; no parser/owner state is inspected or forced.
- send_byte(0, 8'ha5);
- repeat (250500) @(negedge clk);
- send_request(1, 8'h02, 0);
- accept_request(8'h02, 0);
- begin_reply(8'h82, 0);
- finish_reply;
- send_byte(1, 8'ha5);
- repeat (250500) @(negedge clk);
- send_request(0, 8'h02, 0);
- accept_request(8'h02, 0);
- begin_reply(8'h82, 0);
- finish_reply;
- #(30 * BIT_NS);
- if (accepted_count != 6 || tx_done_count != 7 || reply_count != 7)
- $fatal(1, "baud=%0d wrong totals requests=%0d completed=%0d replies=%0d",
- BAUD, accepted_count, tx_done_count, reply_count);
- if (req_valid || tx_busy || tx_done || rs232_tx !== 1 || ttl_tx !== 1)
- $fatal(1, "baud=%0d endpoint did not return idle", BAUD);
- $display("PASS nominal UART baud=%0d divider=%0d RX=RS232+TTL requests=6 replies=7 full_stop_done=7 broadcast=identical synchronous_RAM=checked concurrent_CRC=checked half_A5_timeout=RS232_to_TTL+TTL_to_RS232",
- BAUD, DUT_DIVIDER);
- done = 1;
- end
- endmodule
- module host_uart_endpoint_nominal_tb;
- wire [3:0] done;
- host_uart_endpoint_nominal_case #(.BAUD(921600)) t921600(.done(done[0]));
- host_uart_endpoint_nominal_case #(.BAUD(460800)) t460800(.done(done[1]));
- host_uart_endpoint_nominal_case #(.BAUD(230400)) t230400(.done(done[2]));
- host_uart_endpoint_nominal_case #(.BAUD(115200)) t115200(.done(done[3]));
- initial begin
- wait (&done);
- $display("PASS host_uart_endpoint_nominal_tb: four nominal baud rates, both RX ports, full final stop, independent CRC and synchronous RAM");
- $finish;
- end
- initial begin
- #100000000;
- $fatal(1, "host_uart_endpoint_nominal_tb timeout");
- end
- endmodule
|