Просмотр исходного кода

test(motor): expose tick, clamp, direction and abort contract failures

codex 2 месяцев назад
Родитель
Сommit
6cde6c3ff6
1 измененных файлов с 114 добавлено и 0 удалено
  1. 114 0
      tests/sim/motor_axis_contract_tb.v

+ 114 - 0
tests/sim/motor_axis_contract_tb.v

@@ -0,0 +1,114 @@
+`timescale 1ns/1ps
+// Contract 10.4 at a real 25 MHz clock and 1 us enable. No physical devices.
+module motor_axis_contract_tb;
+    reg clk=0; always #20 clk=~clk;
+    reg rst_n=0, tick_us=0;
+    integer tick_div=0;
+    always @(negedge clk) begin
+        if (!rst_n) begin tick_div=0; tick_us=0; end
+        else if (tick_div==24) begin tick_div=0; tick_us=1; end
+        else begin tick_div=tick_div+1; tick_us=0; end
+    end
+    reg ll=0,ul=0,load=0,start=0,abort_req=0,preempt=0,armed_in=0,upper=0;
+    reg [2:0] mode=0;
+    reg [31:0] count=0,half_us=2;
+    wire step,dir,active,armed,ready,done,ref_seen;
+    wire [3:0] reason,last_reason;
+    wire [31:0] pos,total,last_exec,executed;
+    wire [15:0] lh,uh,tag;
+    wire llatch,ulatch,both;
+    motor_axis_core #(.DIR_SETUP_TICKS(3)) dut (
+        .clk(clk),.rst_n(rst_n),.tick_us(tick_us),.limit_lower(ll),.limit_upper(ul),
+        .seg_load(load),.seg_mode(mode),.seg_dir_upper(upper),.seg_armed(armed_in),
+        .seg_count(count),.seg_half_us(half_us),.seg_tag(16'h1234),.start_armed(start),
+        .abort_req(abort_req),.preempt_req(preempt),.step_out(step),.dir_out(dir),
+        .active(active),.armed(armed),.load_ready(ready),.done_pulse(done),.done_reason(reason),
+        .done_tag(tag),.done_executed(executed),.position(pos),.total_steps(total),
+        .last_executed(last_exec),.position_reference_seen(ref_seen),.lower_hit_count(lh),
+        .upper_hit_count(uh),.lower_latched(llatch),.upper_latched(ulatch),.both_latched(both),
+        .last_stop_reason(last_reason),.clear_limit_latch(1'b0),.position_load(1'b0),
+        .position_value(32'd0),.counters_clear(1'b0),.pos_zero_with_counters(1'b0));
+    integer rises=0,cycles=0,high_at=0;
+    reg prev_step=0,check_full=0,prev_tick;
+    always @(posedge clk) begin
+        prev_tick=tick_us;
+        #1;
+        cycles=cycles+1;
+        if (!rst_n) begin rises=0;prev_step=0;end
+        else begin
+            if (step && !prev_step) begin
+                rises=rises+1;high_at=cycles;
+                if (!prev_tick) $fatal(1,"STEP rose outside the 1 us enable");
+            end
+            if (!step && prev_step && check_full && cycles-high_at != half_us*25)
+                $fatal(1,"final high width: actual %0d expected %0d",cycles-high_at,half_us*25);
+            prev_step=step;
+        end
+    end
+    task reset;
+        begin
+            @(negedge clk);rst_n=0;load=0;start=0;abort_req=0;preempt=0;ll=0;ul=0;check_full=0;
+            repeat(4) @(negedge clk);rst_n=1;repeat(3) @(negedge clk);
+        end
+    endtask
+    task segment(input [2:0] m,input u,input a,input [31:0] n,input [31:0] h);
+        begin
+            @(negedge clk);mode=m;upper=u;armed_in=a;count=n;half_us=h;load=1;
+            @(negedge clk);load=0;
+        end
+    endtask
+    task bounded_done;
+        integer k;
+        begin
+            k=0;
+            while(!done && k<2000) begin @(negedge clk);k=k+1;end
+            if(!done) $fatal(1,"no completion within bounded wait");
+        end
+    endtask
+    initial begin
+        reset();check_full=1;
+        segment(0,0,0,3,2);bounded_done();
+        if(rises!=3 || total!=3 || last_exec!=3 || last_reason!=1 || reason!=1)
+            $fatal(1,"count/reason mismatch rises=%0d total=%0d last=%0d reason=%0d",rises,total,last_exec,last_reason);
+        $display("CASE counted_pulse_width PASS");
+
+        reset();ll=1;repeat(3) @(negedge clk);
+        if(!ref_seen || pos!=0 || lh!=1) $fatal(1,"initial held lower did not establish reference");
+        segment(1,0,0,0,2);bounded_done();
+        if(step || rises!=0 || reason!=2) $fatal(1,"held target limit did not terminate seek");
+        ll=0;repeat(100) @(negedge clk);
+        if(active || rises!=0 || lh!=1) $fatal(1,"release restarted a stopped segment");
+        $display("CASE initial_limit_release PASS");
+
+        reset();segment(2,0,0,0,2);repeat(2) @(negedge clk);
+        if(!dir) $fatal(1,"SEEK_UPPER did not update physical DIR");
+        // Abort must terminate setup immediately, without waiting for a tick.
+        abort_req=1;@(negedge clk);abort_req=0;
+        if(active || armed || step || !done || reason!=4) $fatal(1,"abort during DIR setup");
+        $display("CASE seek_direction_abort_setup PASS");
+
+        reset();segment(0,0,1,2,2);
+        if(!armed) $fatal(1,"segment was not armed");
+        abort_req=1;@(negedge clk);abort_req=0;
+        if(armed || active || !done || reason!=4) $fatal(1,"abort armed segment");
+        $display("CASE abort_armed PASS");
+
+        reset();segment(3,0,0,0,2);wait(step);@(negedge clk);ll=1;
+        @(negedge clk);
+        if(step || active || !done || reason!=2 || last_reason!=2) $fatal(1,"limit high clamp/reason");
+        // Reach the other end then move away without clearing historical latches.
+        segment(2,0,0,0,2);wait(step);@(negedge clk);ll=0;ul=1;
+        @(negedge clk);
+        if(step || active || reason!=3) $fatal(1,"upper limit clamp");
+        segment(0,0,0,1,2);bounded_done();
+        if(reason!=1) $fatal(1,"historical lower/upper latches blocked moving away");
+        $display("CASE live_limits_not_historical_lock PASS");
+
+        reset();segment(3,0,0,0,2);wait(step);@(negedge clk);ll=1;ul=1;
+        @(negedge clk);
+        if(step || active || reason!=7 || last_reason!=7) $fatal(1,"both live limits priority");
+        $display("CASE both_limits PASS");
+        $display("motor_axis_contract_tb PASS");$finish;
+    end
+    initial begin #1000000;$fatal(1,"bounded test deadline");end
+endmodule