| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407 |
- `include "tests/sim/motor_control_hold_monitor.vh"
- `timescale 1ns/1ps
- // Motor engine testbench (2026 contract section 10).
- //
- // Sim-friendly parameters (small min half periods); production parameter
- // relations are checked separately against the generated params header.
- // Coverage: power-on seek, abort, relative count completion with the
- // final-high-pulse rule, hold semantics, batch validation atomicity,
- // preempt, lower-limit reference + hit statistics, event ring reads.
- module motor_engine_tb;
- reg clk = 1'b0;
- always #10 clk = ~clk; // 50 MHz for faster waves
- reg rst_n = 1'b0;
- reg tick_us = 1'b0;
- integer half_ticks = 25; // 1 us = 25 cycles at 50 MHz
- reg [2:0] limit_lower_raw = 3'b000;
- reg [2:0] limit_upper_raw = 3'b000;
- reg qw_byte_we = 1'b0;
- reg [6:0] qw_byte_addr = 7'd0;
- reg [7:0] qw_byte_data = 8'd0;
- reg qw_submit = 1'b0;
- reg [3:0] qw_count = 4'd0;
- wire qw_accept;
- wire [7:0] qw_status;
- wire [15:0] qw_detail;
- reg ctl_valid = 1'b0;
- reg [2:0] ctl_op = 3'd0;
- reg [15:0] ctl_axis_mask = 16'd0;
- reg [7:0] ctl_flags = 8'd0;
- reg [31:0] ctl_value = 32'd0;
- wire ctl_accept;
- wire [7:0] ctl_status;
- wire [15:0] ctl_detail;
- reg startup_seek = 1'b0;
- wire [7:0] ev_rd_index_w = 8'd0;
- wire [127:0] ev_rd_data;
- wire [7:0] ev_count, ev_overflow;
- wire [31:0] ev_time_us;
- wire [2:0] step_bus, dir_bus;
- wire [1535:0] status_record;
- wire [15:0] status_epoch;
- wire status_ready;
- reg [4:0] status_word_addr=0;
- wire [3:0] status_axis;
- wire [15:0] status_word_data;
- reg [1535:0] previous_status;
- reg [15:0] previous_epoch;
- reg check_epoch=0;
- reg previous_event_request=0;
- reg [127:0] previous_event_payload=0;
- reg [127:0] pending_descriptor[0:2];
- reg [2:0] descriptor_pending=0;
- integer descriptor_axis;
- always @(posedge clk)begin
- if(rst_n && (|dut.ax_start_armed) && (|dut.u_axes.start_pending))
- $fatal(1,"engine replaced a START timestamp before consumption");
- if(rst_n && previous_event_request && dut.ev_wr_req &&
- previous_event_payload!=={dut.ev_total,dut.ev_pos,dut.ev_tag,dut.ev_code,4'd0,dut.ev_axis,dut.ev_timestamp})
- $fatal(1,"event producer changed a record before writer acknowledgement");
- previous_event_request<=rst_n && dut.ev_wr_req;
- previous_event_payload<={dut.ev_total,dut.ev_pos,dut.ev_tag,dut.ev_code,4'd0,dut.ev_axis,dut.ev_timestamp};
- if(rst_n)for(descriptor_axis=0;descriptor_axis<3;descriptor_axis=descriptor_axis+1)begin
- if(dut.ax_done[descriptor_axis] && dut.ax_done_word!==
- {dut.ax_dtotal[descriptor_axis],dut.ax_dpos[descriptor_axis],dut.ax_dtag[descriptor_axis],
- 4'd0,dut.ax_reason[descriptor_axis],descriptor_axis[7:0],dut.ax_dtime[descriptor_axis]})
- $fatal(1,"shared completion word differs from diagnostic event state");
- if(dut.ax_limit_pulse[descriptor_axis] && dut.ax_limit_data!==dut.u_axes.limit_event_data[descriptor_axis*128+:128])
- $fatal(1,"shared physical-limit word differs from retained history");
- end
- status_word_addr<=status_word_addr+1'b1;
- if(rst_n && (((dut.ax_done & (dut.ax_done-1'b1))!=0) ||
- ((dut.ax_limit_pulse & (dut.ax_limit_pulse-1'b1))!=0)))
- $fatal(1,"shared service exceeded one write per pending record bank");
- if(!rst_n)descriptor_pending=0;
- else for(descriptor_axis=0;descriptor_axis<3;descriptor_axis=descriptor_axis+1)begin
- if(descriptor_pending[descriptor_axis] && dut.u_axes.load_pending[descriptor_axis] &&
- pending_descriptor[descriptor_axis]!=={43'd0,dut.ax_seg_mode[descriptor_axis],dut.ax_seg_upper[descriptor_axis],dut.ax_seg_armed[descriptor_axis],dut.ax_seg_count[descriptor_axis],dut.ax_seg_half[descriptor_axis],dut.ax_seg_tag[descriptor_axis]})
- $fatal(1,"shared loader changed a pending descriptor");
- if(dut.ax_seg_load[descriptor_axis])begin
- descriptor_pending[descriptor_axis]=1;
- pending_descriptor[descriptor_axis]={43'd0,dut.ax_seg_mode[descriptor_axis],dut.ax_seg_upper[descriptor_axis],dut.ax_seg_armed[descriptor_axis],dut.ax_seg_count[descriptor_axis],dut.ax_seg_half[descriptor_axis],dut.ax_seg_tag[descriptor_axis]};
- end else if(!dut.u_axes.load_pending[descriptor_axis])descriptor_pending[descriptor_axis]=0;
- end
- #1;
- if(rst_n && status_word_data!==status_record[status_axis*512+status_word_addr*16+:16])
- $fatal(1,"addressed status word disagrees with complete schema record");
- if(rst_n && check_epoch && status_record!==previous_status && status_epoch==previous_epoch && status_ready)
- $fatal(1,"visible motor status changed without version/busy protection");
- previous_status=status_record;previous_epoch=status_epoch;check_epoch=rst_n;
- end
- // watch the engine time base through a tick generator
- always begin
- #(half_ticks * 10) tick_us = 1'b1;
- #10 tick_us = 1'b0;
- end
- motor_engine #(
- .AXIS_COUNT(3),
- .X_DEFAULT_HALF_US(32'd4), .Y_DEFAULT_HALF_US(32'd4), .Z_DEFAULT_HALF_US(32'd8),
- .X_MIN_HALF_US(32'd2), .Y_MIN_HALF_US(32'd2), .Z_MIN_HALF_US(32'd4),
- .DIR_SETUP_TICKS(3)
- ) dut (
- .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(startup_seek),
- .ev_rd_byte_index(4'd0),.ev_rd_byte_data(),.ev_rd_index(8'd0), .ev_hold(1'b0),
- .ev_rd_data(ev_rd_data), .ev_count(ev_count), .ev_overflow(ev_overflow),
- .ev_time_us(ev_time_us),
- .ev_clear_n(1'b0), .ev_clear_count(8'd0),
- .axis_telemetry(status_record),.motor_status_epoch(status_epoch),.motor_telemetry_ready(status_ready),
- .telemetry_word_addr(status_word_addr),.telemetry_axis(status_axis),.telemetry_word_data(status_word_data),
- .step_out(step_bus), .dir_out(dir_bus)
- );
- integer i;
- reg [127:0] seg;
- reg [15:0] completed_tags [0:63];
- integer completed_count=0, completed_before, batch_no, slot_no, event_before;
- always @(posedge clk)
- if(rst_n && dut.ax_done[0] && dut.ax_reason[0]==1) begin
- completed_tags[completed_count]=dut.ax_dtag[0];
- completed_count=completed_count+1;
- end
- task push_byte(input [6:0] a, input [7:0] d);
- begin
- @(negedge clk);
- qw_byte_addr = a; qw_byte_data = d; qw_byte_we = 1'b1;
- @(negedge clk);
- qw_byte_we = 1'b0;
- end
- endtask
- task send_seg(input [1:0] axis, input [2:0] mode, input [3:0] flags,
- input [31:0] count, input [31:0] half, input [15:0] tag, input integer slot);
- begin
- // wire segment byte order: axis, mode, flags, res,
- // count, half, tag, res (contract 10.2)
- seg = {16'd0, tag, half, count, 8'd0, 4'd0, flags, 5'd0, mode, 6'd0, axis};
- for (i = 15; i >= 0; i = i - 1)
- push_byte(slot * 16 + i, seg[i*8 +: 8]);
- end
- endtask
- task submit(input [3:0] n);
- begin
- @(negedge clk);
- qw_count = n; qw_submit = 1'b1;
- @(negedge clk);
- qw_submit = 1'b0;
- wait (qw_accept);
- @(negedge clk);
- end
- endtask
- task ctl(input [2:0] op, input [15:0] mask, input [7:0] flags, input [31:0] value);
- integer control_axis;
- begin
- @(negedge clk);
- ctl_op = op; ctl_axis_mask = mask; ctl_flags = flags; ctl_value = value;
- ctl_valid = 1'b1;
- @(negedge clk);
- ctl_valid = 1'b0;
- wait (ctl_accept);
- @(negedge clk);
- if(op==4 && ctl_status==0)for(control_axis=0;control_axis<3;control_axis=control_axis+1)
- if(mask[control_axis] && dut.u_axes.r_position[control_axis]!==value)
- $fatal(1,"SET_POSITION acknowledged before consuming the shared value");
- if((op==6 || op==7) && ctl_status==0)for(control_axis=0;control_axis<3;control_axis=control_axis+1)
- if(mask[control_axis] && (dut.u_axes.r_mode[control_axis]!=(op==6?1:2) ||
- dut.u_axes.r_half_us[control_axis]!=(value!=0?value:(control_axis==2?8:4))))
- $fatal(1,"seek acknowledged before consuming held descriptor fields");
- end
- endtask
- task check(input cond, input [255:0] name);
- if (!cond) begin
- $display("TB_FAIL: %0s", name);
- $fatal(1, "testbench assertion failed");
- end
- endtask
- initial begin
- repeat (5) @(negedge clk);
- rst_n = 1'b1;
- repeat (5) @(negedge clk);
- // ---- 1. power-on seek on all axes, then abort X only ----
- startup_seek = 1'b1;
- @(negedge clk);
- startup_seek = 1'b0;
- repeat (40) @(posedge clk);
- check(dut.ax_active[0], "X seek active after power-on");
- check(dut.ax_active[1], "Y seek active after power-on");
- check(dut.ax_active[2], "Z seek active after power-on");
- check(dut.u_axes.r_half_us[0] == 32'd4, "X uses default half period");
- check(dut.u_axes.r_half_us[2] == 32'd8, "Z uses default half period");
- ctl(3'd1, 16'b001, 8'd0, 32'd0); // ABORT X
- repeat (10) @(posedge clk);
- check(!dut.ax_active[0], "X aborted");
- check(dut.ax_active[1], "Y unaffected by X abort");
- check(ev_count >= 8'd1, "abort event recorded");
- ctl(3'd1, 16'b111, 8'd0, 32'd0); // ABORT all
- repeat (10) @(posedge clk);
- // ---- 2. relative segment: 6 steps, final high pulse completes ----
- // X: axis 0, relative, dir lower (flags bit0=0), count 6, half 3us
- send_seg(2'd0, 3'd0, 4'd0, 32'd6, 32'd3, 16'h0042, 0);
- submit(4'd1);
- check(qw_status == 8'd0, "relative batch accepted");
- repeat (3000) @(posedge clk);
- check(dut.u_axes.r_executed[0] == 32'd6, "exactly 6 executed rises");
- check(!dut.ax_active[0], "relative segment done");
- check(dut.ax_last_reason[0] == 4'd1, "stop reason COUNT");
- check(dut.step_out_w[0] == 1'b0, "step low after completion");
- // ---- 3. hold semantics ----
- // hold with half_period != 0 must be rejected whole: second segment
- // is invalid so the batch (and its valid first segment) is a no-op
- send_seg(2'd1, 3'd4, 4'd0, 32'd3, 32'd0, 16'h0001, 0); // valid hold
- send_seg(2'd1, 3'd0, 4'd0, 32'd0, 32'd3, 16'h0002, 1); // invalid: rel count 0
- submit(4'd2);
- check(qw_status == 8'd3, "bad batch rejected BAD_VALUE");
- check(dut.ax_active[1] == 1'b0 || dut.depth_r[1] == 6'd0,
- "rejected batch changed nothing");
- // valid hold alone completes after 3 us with no STEP
- send_seg(2'd1, 3'd4, 4'd0, 32'd3, 32'd0, 16'h0003, 0);
- submit(4'd1);
- check(qw_status == 8'd0, "hold batch accepted");
- check(dut.step_out_w[1] == 1'b0, "hold emits no STEP");
- repeat (400) @(posedge clk);
- check(!dut.ax_active[1], "hold completed");
- check(dut.ax_last_reason[1] == 4'd9, "stop reason HOLD");
- // ---- 4. lower limit: termination, zero point, hit statistics ----
- // queue an unlimited lower seek on Y (direction toward lower)
- send_seg(2'd1, 3'd1, 4'd0, 32'd0, 32'd3, 16'h0007, 0);
- submit(4'd1);
- repeat (100) @(posedge clk);
- check(dut.ax_active[1], "seek lower running");
- limit_lower_raw[1] = 1'b1;
- repeat (10) @(posedge clk);
- check(!dut.ax_active[1], "seek terminated by lower limit");
- check(dut.ax_last_reason[1] == 4'd2, "stop reason LOWER_LIMIT");
- check(dut.u_axes.r_position[1] == 32'd0, "position zeroed at reference");
- check(dut.u_axes.r_ref_seen[1] == 1'b1, "reference seen");
- check(dut.ax_lhits[1] == 16'd1, "one hit counted");
- // hold the limit high: no repeated counting while active
- repeat (2000) @(posedge clk);
- check(dut.ax_lhits[1] == 16'd1, "held limit counts once");
- limit_lower_raw[1] = 1'b0;
- repeat (10) @(posedge clk);
- check(dut.ax_lhits[1] == 16'd1, "release adds no count");
- // ---- 5. preempt: running segment replaced by urgent segment ----
- // X runs a long relative segment
- send_seg(2'd0, 3'd0, 4'd0, 32'd500, 32'd3, 16'h00A0, 0);
- submit(4'd1);
- repeat (500) @(posedge clk);
- check(dut.ax_active[0], "long segment running");
- // preempting batch: first segment carries flags bit1; direction
- // flipped (upper) so the DIR change is observable in the wave
- send_seg(2'd0, 3'd0, 4'b0011, 32'd20, 32'd3, 16'h00A1, 0);
- submit(4'd1);
- repeat (30) @(posedge clk);
- check(dut.ax_active[0], "preempted segment became active");
- check(dut.u_axes.r_dir_upper[0] == 1'b1, "urgent segment direction");
- repeat (4000) @(posedge clk);
- check(dut.u_axes.r_executed[0] == 32'd20, "preempted segment ran to completion");
- check(dut.ax_last_reason[0] == 4'd1, "count complete after preempt");
- // ---- 6. event ring ----
- check(ev_count >= 8'd5, "several events recorded");
- check(ev_overflow == 8'd0, "no overflow at this depth");
- // clear-all is a control-side op; here just verify count stable
- // without writes while idle
- repeat (100) @(posedge clk);
- check(ev_count >= 8'd5, "records retained (read does not delete)");
- // ---- 7. armed head needs exactly one START; next plain segment runs ----
- ctl(3'd1,16'b111,8'd1,0);
- send_seg(0,0,4'b0100,2,3,16'hB0,0);
- send_seg(0,0,0,2,3,16'hB1,1);
- submit(2);
- repeat(100) @(posedge clk);
- check(!dut.ax_active[0],"armed head waits");
- ctl(0,16'b001,0,0);
- repeat(2000) @(posedge clk);
- $display("armed trace tag=%h state=%0d depth=%0d paused=%b credit=%b",dut.u_axes.r_tag[0],
- dut.u_axes.state[0],dut.depth_r[0],dut.queue_paused_r[0],dut.start_credit[0]);
- check(dut.u_axes.r_tag[0]==16'hB1 &&
- dut.ax_last_reason[0]==1,"one START releases armed head");
- // ---- 8. rejected later preempt/clear must not alter any axis ----
- ctl(3'd1,16'b111,8'd1,0);
- send_seg(0,0,4'b0100,2,3,16'hC0,0);
- send_seg(1,0,4'b0100,2,3,16'hC1,1);
- send_seg(0,0,4'b1000,2,3,16'hC2,2); // clear on second X segment: illegal
- submit(3);
- check(qw_status==3,"late clear must reject whole batch");
- repeat(100) @(posedge clk);
- check(dut.depth_r[0]==0 && dut.depth_r[1]==0 &&
- !dut.ax_armed[0] && !dut.ax_armed[1],
- "rejected multi-axis batch is unchanged");
- // ---- 9. preempt batch prefix comes before the preserved old queue ----
- ctl(3'd1,16'b111,8'd1,0);
- send_seg(0,3,0,0,3,16'hD0,0);
- send_seg(0,0,0,2,3,16'hD1,1);
- send_seg(0,0,0,2,3,16'hD2,2);
- submit(3);repeat(100) @(posedge clk);
- completed_before=completed_count;
- send_seg(0,0,4'b0011,2,3,16'hE0,0);
- send_seg(0,0,4'b0001,2,3,16'hE1,1);
- submit(2);repeat(4000) @(posedge clk);
- check(completed_count==completed_before+4,"preempt retained four completions");
- check(completed_tags[completed_before]==16'hE0 && completed_tags[completed_before+1]==16'hE1 &&
- completed_tags[completed_before+2]==16'hD1 && completed_tags[completed_before+3]==16'hD2,
- "preempt new batch before old wait");
- // ---- 10. 32 waiting slots, whole-batch full rejection, then clear ----
- ctl(3'd1,16'b111,8'd1,0);
- send_seg(0,3,0,0,3,16'hF0,0);submit(1);repeat(100) @(posedge clk);
- for(batch_no=0;batch_no<4;batch_no=batch_no+1) begin
- for(slot_no=0;slot_no<8;slot_no=slot_no+1)
- send_seg(0,0,0,1,3,16'hF1+batch_no*8+slot_no,slot_no);
- submit(8);check(qw_status==0,"fill all 32 waiting slots");
- end
- check(dut.depth_r[0]==32,"32 waiting slots exposed");
- send_seg(1,0,0,1,3,16'hA1,0);send_seg(0,0,0,1,3,16'hA2,1);submit(2);
- check(qw_status==5,"queue full status");
- check(dut.depth_r[0]==32 && dut.depth_r[1]==0,"full rejects every axis atomically");
- completed_before=completed_count;
- send_seg(0,0,4'b1010,1,3,16'hA3,0);send_seg(0,0,0,1,3,16'hA4,1);submit(2);
- check(qw_status==0,"preempt clear frees space before validation");
- repeat(2000) @(posedge clk);
- check(completed_count==completed_before+2 && dut.depth_r[0]==0,"clear keeps only new batch");
- check(completed_tags[completed_before]==16'hA3 && completed_tags[completed_before+1]==16'hA4,
- "clear preserves new batch order");
- // ---- 11. simultaneous axis completions must each reach the writer ----
- ctl(6,16'b111,0,0);repeat(100) @(posedge clk);
- event_before=ev_count;
- ctl(1,16'b111,0,0);repeat(100) @(posedge clk);
- check(ev_count==event_before+3,"all simultaneous abort events retained");
- // Physical edge while idle, then repeated HOME at an already-held
- // lower switch: one real edge event; HOME completion is separate.
- event_before=ev_count;limit_lower_raw[2]=1;repeat(60)@(posedge clk);
- check(ev_count==event_before+1 && dut.ax_lhits[2]==1,"idle physical limit recorded once");
- event_before=ev_count;ctl(6,16'b100,0,0);repeat(60)@(posedge clk);
- check(ev_count==event_before+1 && dut.ax_lhits[2]==1,"held HOME adds completion not physical hit");
- event_before=ev_count;ctl(6,16'b100,0,0);repeat(60)@(posedge clk);
- check(ev_count==event_before+1 && dut.ax_lhits[2]==1,"repeated held HOME does not forge edge");
- ctl(5,16'b100,0,0);repeat(20)@(posedge clk);
- check(!dut.ax_llat[2] && !dut.ax_active[2],"clear latch does not restart held limit");
- // Zero HOLD completes once through the normal queue, without STEP.
- send_seg(0,4,0,0,0,16'hA5,0);submit(1);repeat(60)@(posedge clk);
- check(!dut.ax_active[0] && dut.ax_last_reason[0]==9,"zero HOLD completes");
- $display("motor_engine_tb PASS");
- $finish;
- end
- initial begin #10000000; $fatal(1,"bounded motor engine deadline"); end
- motor_control_hold_monitor control_hold(clk,rst_n,ctl_valid,ctl_accept,ctl_op,ctl_axis_mask,ctl_flags,ctl_value);
- reg capacity_watch=0;reg[3:0]capacity_previous=0;
- reg[17:0]capacity_depth;
- integer capacity_axis;
- always @(negedge clk)begin
- if(!rst_n)begin capacity_watch=0;capacity_previous=0;end
- else begin
- if(capacity_previous==dut.QC_QUIESCE && dut.qc_state==dut.QC_STAGE_WAIT)begin
- capacity_watch=1;
- for(capacity_axis=0;capacity_axis<3;capacity_axis=capacity_axis+1)capacity_depth[capacity_axis*6+:6]=dut.depth_r[capacity_axis];
- end else if(capacity_watch)begin
- if(dut.qc_state!=dut.QC_PUBLISH)
- for(capacity_axis=0;capacity_axis<3;capacity_axis=capacity_axis+1)
- if(dut.depth_r[capacity_axis]!==capacity_depth[capacity_axis*6+:6])$fatal(1,"live depth changed before atomic queue publication");
- if(dut.qc_state==dut.QC_IDLE || dut.qc_state==dut.QC_PUBLISH)capacity_watch=0;
- end
- capacity_previous=dut.qc_state;
- end
- end
- reg [127:0] queue_capture_expected; reg queue_capture_check=0;
- always @(posedge clk)begin
- if(!rst_n)queue_capture_check<=0;
- else begin
- if(dut.qc_state==dut.QC_WRITE && dut.qc_index!=dut.qc_count && !dut.q_write_busy)
- queue_capture_expected<=dut.stage_data;
- queue_capture_check<=dut.q_we && !dut.u_queue.write_active;
- end
- end
- // The storage streams wr_data across four word writes; every written word
- // must come from the validated descriptor captured at q_we.
- always @(negedge clk)
- if(dut.u_queue.write_active &&
- dut.u_queue.write_wdata!==queue_capture_expected[dut.u_queue.write_word*32 +: 32])
- $fatal(1,"queue captured a changed descriptor instead of the validated producer word");
- endmodule
|