`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; reg position_load=0,clear_counters=0; reg [31:0] position_value=0; reg [63:0] simulation_time=64'h00000005FFFFFFF0,expected_limit_time; always @(posedge clk)if(tick_us)simulation_time<=simulation_time+1'b1; wire [511:0] telemetry; wire step,dir,active,armed,ready,done,ref_seen; wire [3:0] reason,last_reason; wire [31:0] pos,total,last_exec,executed; wire [31:0] lh,uh; wire [15:0] 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(position_load), .position_value(position_value),.counters_clear(clear_counters),.pos_zero_with_counters(1'b0), .event_clock_us(simulation_time[31:0]),.event_clock_high(simulation_time[63:32]),.telemetry_record(telemetry)); 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;position_load=0;clear_counters=0; repeat(4) @(negedge clk);rst_n=1;repeat(3) @(negedge clk); if(telemetry[36*8+:160]!==160'd0)$fatal(1,"reset exposed stale limit history storage"); 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"); reset();position_value=32'hFFFFFFFD;position_load=1;@(negedge clk);position_load=0; segment(0,1,0,2,2);wait(step);@(negedge clk);ul=1;expected_limit_time=simulation_time;@(negedge clk); if(step || active)$fatal(1,"limit clamp must not wait for telemetry service"); wait(dut.telemetry_ready);@(negedge clk); if(telemetry[8*8 +:32]!==32'hFFFFFFFE || telemetry[36*8 +:32]!==32'hFFFFFFFE || telemetry[40*8 +:32]!==1 || telemetry[44*8 +:16]!==16'h1234 || telemetry[48*8 +:32]!==expected_limit_time[31:0] || telemetry[52*8 +:32]!==expected_limit_time[63:32]) $fatal(1,"complete negative-position/limit/time telemetry"); $display("CASE full_limit_telemetry PASS"); reset();position_value=32'h7FFFFFFF;position_load=1;@(negedge clk);position_load=0; dut.r_total[0]=32'hFFFFFFFF;@(negedge clk); segment(0,1,0,1,2);bounded_done(); if(total!==0 || pos!==32'h80000000 || (telemetry[56*8 +:32]&32'h11)!==32'h11) $fatal(1,"counter and signed coordinate wrap telemetry"); $display("CASE counter_wrap_telemetry PASS"); reset();@(negedge clk);dut.hit_memory[0]=64'hFFFFFFFFFFFFFFFF;dut.hit_valid=1; ll=1;ul=1;repeat(5)@(negedge clk); if(lh!=0 || uh!=0 || telemetry[56*8+2+:2]!=2'b11)$fatal(1,"32-bit hit counter wrap flags"); clear_counters=1;@(negedge clk);clear_counters=0;repeat(4)@(negedge clk); if(lh!=0 || uh!=0 || telemetry[56*8+:32]!=0 || telemetry[47*8+:8]!=7) $fatal(1,"counter clear changed retained limit history or left wrap flags"); reset(); $display("CASE hit_counter_memory_wrap_clear_reset PASS"); $display("motor_axis_contract_tb PASS");$finish; end initial begin #1000000;$fatal(1,"bounded test deadline");end endmodule