| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
- `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;
- // 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_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(),
- .step_out(step_bus), .dir_out(dir_bus)
- );
- integer i;
- reg [127:0] seg;
- 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
- seg = {112'd0, tag, half, count, 4'd0, flags, 3'b0, mode, 2'b0, 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);
- 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);
- 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) @(posedge clk);
- rst_n = 1'b1;
- repeat (5) @(posedge 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.g_axis[0].u_axis.active, "X seek active after power-on");
- check(dut.g_axis[1].u_axis.active, "Y seek active after power-on");
- check(dut.g_axis[2].u_axis.active, "Z seek active after power-on");
- check(dut.g_axis[0].u_axis.r_half_us == 32'd4, "X uses default half period");
- check(dut.g_axis[2].u_axis.r_half_us == 32'd8, "Z uses default half period");
- ctl(3'd1, 16'b001, 8'd0, 32'd0); // ABORT X
- repeat (10) @(posedge clk);
- check(!dut.g_axis[0].u_axis.active, "X aborted");
- check(dut.g_axis[1].u_axis.active, "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.g_axis[0].u_axis.r_executed == 32'd6, "exactly 6 executed rises");
- check(!dut.g_axis[0].u_axis.active, "relative segment done");
- check(dut.g_axis[0].u_axis.last_stop_reason == 4'd1, "stop reason COUNT");
- check(dut.g_axis[0].u_axis.step_out == 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.g_axis[1].u_axis.r_active == 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.g_axis[1].u_axis.step_out == 1'b0, "hold emits no STEP");
- repeat (400) @(posedge clk);
- check(!dut.g_axis[1].u_axis.active, "hold completed");
- check(dut.g_axis[1].u_axis.last_stop_reason == 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.g_axis[1].u_axis.active, "seek lower running");
- limit_lower_raw[1] = 1'b1;
- repeat (10) @(posedge clk);
- check(!dut.g_axis[1].u_axis.active, "seek terminated by lower limit");
- check(dut.g_axis[1].u_axis.last_stop_reason == 4'd2, "stop reason LOWER_LIMIT");
- check(dut.g_axis[1].u_axis.position == 32'd0, "position zeroed at reference");
- check(dut.g_axis[1].u_axis.r_ref_seen == 1'b1, "reference seen");
- check(dut.g_axis[1].u_axis.lower_hit_count == 16'd1, "one hit counted");
- // hold the limit high: no repeated counting while active
- repeat (2000) @(posedge clk);
- check(dut.g_axis[1].u_axis.lower_hit_count == 16'd1, "held limit counts once");
- limit_lower_raw[1] = 1'b0;
- repeat (10) @(posedge clk);
- check(dut.g_axis[1].u_axis.lower_hit_count == 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.g_axis[0].u_axis.active, "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'b0010, 32'd20, 32'd3, 16'h00A1, 0);
- submit(4'd1);
- repeat (30) @(posedge clk);
- check(dut.g_axis[0].u_axis.active, "preempted segment became active");
- check(dut.g_axis[0].u_axis.r_dir_upper == 1'b1, "urgent segment direction");
- repeat (4000) @(posedge clk);
- check(dut.g_axis[0].u_axis.r_executed == 32'd20, "preempted segment ran to completion");
- check(dut.g_axis[0].u_axis.last_stop_reason == 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)");
- $display("motor_engine_tb PASS");
- $finish;
- end
- endmodule
|