motor_axis_contract_tb.v 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. `timescale 1ns/1ps
  2. // Contract 10.4 at a real 25 MHz clock and 1 us enable. No physical devices.
  3. module motor_axis_contract_tb;
  4. reg clk=0; always #20 clk=~clk;
  5. reg rst_n=0, tick_us=0;
  6. integer tick_div=0;
  7. always @(negedge clk) begin
  8. if (!rst_n) begin tick_div=0; tick_us=0; end
  9. else if (tick_div==24) begin tick_div=0; tick_us=1; end
  10. else begin tick_div=tick_div+1; tick_us=0; end
  11. end
  12. reg ll=0,ul=0,load=0,start=0,abort_req=0,preempt=0,armed_in=0,upper=0;
  13. reg [2:0] mode=0;
  14. reg [31:0] count=0,half_us=2;
  15. wire step,dir,active,armed,ready,done,ref_seen;
  16. wire [3:0] reason,last_reason;
  17. wire [31:0] pos,total,last_exec,executed;
  18. wire [15:0] lh,uh,tag;
  19. wire llatch,ulatch,both;
  20. motor_axis_core #(.DIR_SETUP_TICKS(3)) dut (
  21. .clk(clk),.rst_n(rst_n),.tick_us(tick_us),.limit_lower(ll),.limit_upper(ul),
  22. .seg_load(load),.seg_mode(mode),.seg_dir_upper(upper),.seg_armed(armed_in),
  23. .seg_count(count),.seg_half_us(half_us),.seg_tag(16'h1234),.start_armed(start),
  24. .abort_req(abort_req),.preempt_req(preempt),.step_out(step),.dir_out(dir),
  25. .active(active),.armed(armed),.load_ready(ready),.done_pulse(done),.done_reason(reason),
  26. .done_tag(tag),.done_executed(executed),.position(pos),.total_steps(total),
  27. .last_executed(last_exec),.position_reference_seen(ref_seen),.lower_hit_count(lh),
  28. .upper_hit_count(uh),.lower_latched(llatch),.upper_latched(ulatch),.both_latched(both),
  29. .last_stop_reason(last_reason),.clear_limit_latch(1'b0),.position_load(1'b0),
  30. .position_value(32'd0),.counters_clear(1'b0),.pos_zero_with_counters(1'b0));
  31. integer rises=0,cycles=0,high_at=0;
  32. reg prev_step=0,check_full=0,prev_tick;
  33. always @(posedge clk) begin
  34. prev_tick=tick_us;
  35. #1;
  36. cycles=cycles+1;
  37. if (!rst_n) begin rises=0;prev_step=0;end
  38. else begin
  39. if (step && !prev_step) begin
  40. rises=rises+1;high_at=cycles;
  41. if (!prev_tick) $fatal(1,"STEP rose outside the 1 us enable");
  42. end
  43. if (!step && prev_step && check_full && cycles-high_at != half_us*25)
  44. $fatal(1,"final high width: actual %0d expected %0d",cycles-high_at,half_us*25);
  45. prev_step=step;
  46. end
  47. end
  48. task reset;
  49. begin
  50. @(negedge clk);rst_n=0;load=0;start=0;abort_req=0;preempt=0;ll=0;ul=0;check_full=0;
  51. repeat(4) @(negedge clk);rst_n=1;repeat(3) @(negedge clk);
  52. end
  53. endtask
  54. task segment(input [2:0] m,input u,input a,input [31:0] n,input [31:0] h);
  55. begin
  56. @(negedge clk);mode=m;upper=u;armed_in=a;count=n;half_us=h;load=1;
  57. @(negedge clk);load=0;
  58. end
  59. endtask
  60. task bounded_done;
  61. integer k;
  62. begin
  63. k=0;
  64. while(!done && k<2000) begin @(negedge clk);k=k+1;end
  65. if(!done) $fatal(1,"no completion within bounded wait");
  66. end
  67. endtask
  68. initial begin
  69. reset();check_full=1;
  70. segment(0,0,0,3,2);bounded_done();
  71. if(rises!=3 || total!=3 || last_exec!=3 || last_reason!=1 || reason!=1)
  72. $fatal(1,"count/reason mismatch rises=%0d total=%0d last=%0d reason=%0d",rises,total,last_exec,last_reason);
  73. $display("CASE counted_pulse_width PASS");
  74. reset();ll=1;repeat(3) @(negedge clk);
  75. if(!ref_seen || pos!=0 || lh!=1) $fatal(1,"initial held lower did not establish reference");
  76. segment(1,0,0,0,2);bounded_done();
  77. if(step || rises!=0 || reason!=2) $fatal(1,"held target limit did not terminate seek");
  78. ll=0;repeat(100) @(negedge clk);
  79. if(active || rises!=0 || lh!=1) $fatal(1,"release restarted a stopped segment");
  80. $display("CASE initial_limit_release PASS");
  81. reset();segment(2,0,0,0,2);repeat(2) @(negedge clk);
  82. if(!dir) $fatal(1,"SEEK_UPPER did not update physical DIR");
  83. // Abort must terminate setup immediately, without waiting for a tick.
  84. abort_req=1;@(negedge clk);abort_req=0;
  85. if(active || armed || step || !done || reason!=4) $fatal(1,"abort during DIR setup");
  86. $display("CASE seek_direction_abort_setup PASS");
  87. reset();segment(0,0,1,2,2);
  88. if(!armed) $fatal(1,"segment was not armed");
  89. abort_req=1;@(negedge clk);abort_req=0;
  90. if(armed || active || !done || reason!=4) $fatal(1,"abort armed segment");
  91. $display("CASE abort_armed PASS");
  92. reset();segment(3,0,0,0,2);wait(step);@(negedge clk);ll=1;
  93. @(negedge clk);
  94. if(step || active || !done || reason!=2 || last_reason!=2) $fatal(1,"limit high clamp/reason");
  95. // Reach the other end then move away without clearing historical latches.
  96. segment(2,0,0,0,2);wait(step);@(negedge clk);ll=0;ul=1;
  97. @(negedge clk);
  98. if(step || active || reason!=3) $fatal(1,"upper limit clamp");
  99. segment(0,0,0,1,2);bounded_done();
  100. if(reason!=1) $fatal(1,"historical lower/upper latches blocked moving away");
  101. $display("CASE live_limits_not_historical_lock PASS");
  102. reset();segment(3,0,0,0,2);wait(step);@(negedge clk);ll=1;ul=1;
  103. @(negedge clk);
  104. if(step || active || reason!=7 || last_reason!=7) $fatal(1,"both live limits priority");
  105. $display("CASE both_limits PASS");
  106. $display("motor_axis_contract_tb PASS");$finish;
  107. end
  108. initial begin #1000000;$fatal(1,"bounded test deadline");end
  109. endmodule