| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- `timescale 1ns/1ps
- module prod2u_p4_trace_pending_coalescing_tb;
- localparam [7:0] EVENT_OVERFLOW = 8'h07;
- reg clk = 1'b0;
- reg rst_n = 1'b0;
- reg tick_1us = 1'b0;
- reg request_valid = 1'b0;
- wire request_ready;
- reg [7:0] request_type = 8'd0;
- reg [5:0] request_length = 6'd0;
- reg [271:0] request_payload = 272'd0;
- reg [15:0] request_crc16 = 16'd0;
- reg emergency_disable_valid = 1'b0;
- reg [15:0] emergency_disable_crc16 = 16'd0;
- wire boot_valid;
- reg boot_ready = 1'b1;
- wire [63:0] boot_payload;
- wire frame_valid;
- reg frame_ready = 1'b0;
- wire [7:0] frame_type;
- wire [7:0] frame_payload_len;
- wire [143:0] frame_payload;
- reg x_limit_fast = 1'b0;
- reg y_limit_fast = 1'b0;
- reg x_limit_active = 1'b0;
- reg y_limit_active = 1'b0;
- reg x_limit_ready = 1'b1;
- reg y_limit_ready = 1'b1;
- wire [191:0] ptc_percent;
- wire [31:0] fan_percent;
- wire [7:0] solenoid_percent;
- wire [15:0] state_hash16;
- wire [2:0] step_out;
- wire [2:0] dir_out;
- wire en_n;
- wire disabled;
- wire watchdog_timeout;
- reg [31:0] seq_before;
- reg [15:0] held_core_meta;
- reg [63:0] held_limit_data;
- reg [63:0] held_safety_data;
- reg [31:0] first_lost_event_seq;
- reg [31:0] first_post_gap_event_seq;
- reg [7:0] first_post_gap_event_type;
- reg [63:0] first_post_gap_event_data;
- reg [3:0] first_post_gap_selection;
- integer timeout;
- prod2u_p4_normal_v3_core #(
- .BUILD_ID(32'h1234abcd),
- .CLK_HZ(100),
- .WATCHDOG_SECONDS(1),
- .STANDARD_STEPS(8),
- .DIR_SETUP_US(2),
- .SPEED0_HALF_US(4),
- .SPEED1_HALF_US(3),
- .SPEED2_HALF_US(2),
- .SPEED3_HALF_US(1)
- ) dut (
- .clk(clk), .rst_n(rst_n), .tick_1us(tick_1us),
- .request_valid(request_valid), .request_ready(request_ready),
- .request_type(request_type), .request_length(request_length),
- .request_payload(request_payload), .request_crc16(request_crc16),
- .emergency_disable_valid(emergency_disable_valid),
- .emergency_disable_crc16(emergency_disable_crc16),
- .boot_valid(boot_valid), .boot_ready(boot_ready), .boot_payload(boot_payload),
- .frame_valid(frame_valid), .frame_ready(frame_ready), .frame_type(frame_type),
- .frame_payload_len(frame_payload_len), .frame_payload(frame_payload),
- .x_limit_fast(x_limit_fast), .y_limit_fast(y_limit_fast),
- .x_limit_active(x_limit_active), .y_limit_active(y_limit_active),
- .x_limit_ready(x_limit_ready), .y_limit_ready(y_limit_ready),
- .ptc_percent(ptc_percent), .fan_percent(fan_percent),
- .solenoid_percent(solenoid_percent), .state_hash16(state_hash16),
- .step_out(step_out), .dir_out(dir_out), .en_n(en_n),
- .disabled(disabled), .watchdog_timeout(watchdog_timeout)
- );
- always #5 clk = ~clk;
- task release_forces;
- begin
- release dut.state_hash16;
- release dut.current_safety_state;
- release dut.u_trace_buffer.pop_valid;
- release dut.u_trace_buffer.operation;
- end
- endtask
- task reset_and_block_trace;
- begin
- release_forces;
- rst_n = 1'b0;
- frame_ready = 1'b0;
- x_limit_fast = 1'b0;
- y_limit_fast = 1'b0;
- x_limit_active = 1'b0;
- y_limit_active = 1'b0;
- x_limit_ready = 1'b0;
- y_limit_ready = 1'b0;
- repeat (4) @(negedge clk);
- rst_n = 1'b1;
- timeout = 0;
- while (((dut.trace_event_count == 0) ||
- (dut.u_trace_buffer.operation != 2'd0)) && timeout < 80) begin
- @(posedge clk); #1;
- timeout = timeout + 1;
- end
- if (timeout >= 80 || dut.trace_event_count == 0) $fatal;
- // Keep the single BRAM port occupied so these tests isolate
- // pending-event collision/coalescing from normal writer service.
- force dut.u_trace_buffer.pop_valid = 1'b1;
- force dut.u_trace_buffer.operation = 2'd1;
- #1;
- if (!dut.trace_pop_valid || dut.trace_push_ready) $fatal;
- end
- endtask
- initial begin
- reset_and_block_trace;
- // CORE drop-new: the first pending payload is immutable and the
- // second observation consumes one sequence number.
- seq_before = dut.trace_event_seq;
- force dut.state_hash16 = 16'h1111;
- @(posedge clk); #1;
- if (!dut.core_event_pending || dut.core_event_meta !== 16'h1111) $fatal;
- if (!request_ready) begin
- $fatal(1, "pre-overflow command path was not ready");
- end
- held_core_meta = dut.core_event_meta;
- force dut.state_hash16 = 16'h2222;
- @(posedge clk); #1;
- if (!dut.core_event_pending || dut.core_event_meta !== held_core_meta) $fatal;
- if (dut.trace_event_seq !== seq_before + 32'd1 ||
- !dut.trace_overflow_pending || dut.trace_dropped_event_seq !== seq_before) $fatal;
- // Queue the gap explanation before accepting a later command result.
- // Otherwise the host sees the missing sequence on COMMAND_RESULT first.
- request_type = 8'h05;
- request_length = 6'd4;
- request_payload = 272'd0;
- request_payload[7:0] = 8'h29;
- request_crc16 = 16'hc001;
- request_valid = 1'b1;
- #1;
- if (request_ready) begin
- $fatal(1, "command remained ready while OVERFLOW was pending");
- end
- @(posedge clk); #1;
- if (dut.command_inflight || dut.command_slot_reserved) begin
- $fatal(1, "command was accepted before pending OVERFLOW was queued");
- end
- request_valid = 1'b0;
- reset_and_block_trace;
- // LIMIT uses drop-new coalescing. AXIS terminal records are held at
- // the motor-hub ready/valid boundary and are covered independently.
- x_limit_fast = 1'b1;
- @(posedge clk); #1;
- if (!dut.limit_event_pending) $fatal;
- held_limit_data = dut.limit_event_data;
- seq_before = dut.trace_event_seq;
- x_limit_active = 1'b1;
- @(posedge clk); #1;
- if (dut.limit_event_data !== held_limit_data) $fatal;
- if (dut.trace_event_seq !== seq_before + 32'd1 ||
- !dut.trace_overflow_pending || dut.trace_dropped_event_seq !== seq_before) begin
- $display("LIMIT_COALESCE_ACCOUNTING seq=%0d expected=%0d overflow=%0d dropped=%0d expected_dropped=%0d pending=%0d draining=%0d collision=%0d",
- dut.trace_event_seq, seq_before + 32'd1,
- dut.trace_overflow_pending, dut.trace_dropped_event_seq,
- seq_before, dut.limit_event_pending,
- dut.limit_event_draining, dut.limit_event_collision);
- $fatal;
- end
- reset_and_block_trace;
- // SAFETY evidence is critical. A second uncommitted observation is a
- // hard-safe condition and must not replace the first payload.
- force dut.current_safety_state = 4'b0010;
- @(posedge clk); #1;
- if (!dut.safety_event_pending) $fatal;
- held_safety_data = dut.safety_event_data;
- seq_before = dut.trace_event_seq;
- force dut.current_safety_state = 4'b0100;
- @(posedge clk); #1;
- if (dut.safety_event_data !== held_safety_data || !dut.trace_hard_safe) $fatal;
- if (dut.trace_event_seq !== seq_before + 32'd1) $fatal;
- reset_and_block_trace;
- // If a noncritical observation is lost, OVERFLOW is the only event
- // that can explain the resulting sequence gap. A SAFETY observation
- // becoming pending on the same edge must wait until that OVERFLOW has
- // been accepted, or the host will terminate on an unclassified gap.
- force dut.state_hash16 = 16'h3333;
- @(posedge clk); #1;
- if (!dut.core_event_pending) $fatal;
- first_lost_event_seq = dut.trace_event_seq;
- force dut.state_hash16 = 16'h4444;
- force dut.current_safety_state = 4'b0010;
- @(posedge clk); #1;
- if (!dut.trace_overflow_pending ||
- dut.trace_dropped_event_seq !== first_lost_event_seq ||
- !dut.safety_event_pending) begin
- $display("OVERFLOW_PRIORITY_PRECONDITION overflow=%0d dropped=%0d expected=%0d safety_pending=%0d seq=%0d",
- dut.trace_overflow_pending, dut.trace_dropped_event_seq,
- first_lost_event_seq, dut.safety_event_pending,
- dut.trace_event_seq);
- $fatal;
- end
- release dut.u_trace_buffer.pop_valid;
- release dut.u_trace_buffer.operation;
- timeout = 0;
- while (!(dut.trace_event_attempt && dut.trace_push_ready) &&
- timeout < 80) begin
- @(negedge clk);
- timeout = timeout + 1;
- end
- if (timeout >= 80) $fatal(1, "post-gap Trace event was not serviced");
- first_post_gap_event_seq = dut.trace_event_payload[31:0];
- first_post_gap_event_type = dut.trace_event_payload[39:32];
- first_post_gap_event_data = dut.trace_event_payload[127:64];
- first_post_gap_selection = dut.trace_selection;
- @(posedge clk); #1;
- if (!dut.trace_push_accepted) begin
- $fatal(1, "observed post-gap Trace candidate was not accepted");
- end
- if (first_post_gap_event_type !== EVENT_OVERFLOW ||
- first_post_gap_event_data !== {32'd0, first_lost_event_seq}) begin
- $display("OVERFLOW_PRIORITY_RED accepted_seq=%0d accepted_type=0x%02x accepted_data=0x%016x selection=%0d first_lost=%0d safety_pending=%0d overflow_pending=%0d",
- first_post_gap_event_seq, first_post_gap_event_type,
- first_post_gap_event_data, first_post_gap_selection,
- first_lost_event_seq, dut.safety_event_pending,
- dut.trace_overflow_pending);
- $fatal(1,
- "first accepted post-gap Trace must be OVERFLOW with exact first_lost_event_seq");
- end
- release_forces;
- $display("PASS: prod2u_p4_trace_pending_coalescing_tb");
- $finish;
- end
- endmodule
|