motor_counter_math_tb.v 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. `timescale 1ns/1ps
  2. module motor_counter_math_tb;
  3. reg clk=0;always #20 clk=~clk;
  4. reg rst_n=0,up=0,rise=0,unlimited=0;
  5. reg [31:0] total=0,executed=0,position=0,remaining=0;
  6. reg [63:0] hits=0;
  7. reg [3:0] lower_inc=0,upper_inc=0;
  8. wire commit,overflow;
  9. wire [32:0] total_sum,exec_sum,lower_sum,upper_sum;
  10. wire [31:0] position_next,remaining_next;
  11. motor_counter_math dut(clk,rst_n,total,executed,position,remaining,hits,up,rise,unlimited,
  12. lower_inc,upper_inc,commit,total_sum,exec_sum,lower_sum,upper_sum,position_next,remaining_next,overflow);
  13. reg [31:0] random_state=32'h819234AB;
  14. function [31:0] random_word;
  15. begin random_state=random_state^(random_state<<13);random_state=random_state^(random_state>>17);random_state=random_state^(random_state<<5);random_word=random_state;end
  16. endfunction
  17. reg [31:0] boundary[0:7];
  18. integer test_id,phase_id,checks=0;
  19. reg [31:0] ref_pos;
  20. task check_result;
  21. begin
  22. ref_pos=rise ? (up ? position+32'd1 : position-32'd1) : position;
  23. if(total_sum!==({1'b0,total}+rise) || exec_sum!==({1'b0,executed}+rise))$fatal(1,"increment/carry mismatch");
  24. if(position_next!==ref_pos || remaining_next!==((rise && !unlimited)?remaining-32'd1:remaining))$fatal(1,"position/remaining mismatch");
  25. if(overflow!==(rise && (up ? position==32'h7FFFFFFF : position==32'h80000000)))$fatal(1,"signed overflow mismatch");
  26. if(lower_sum!==({1'b0,hits[31:0]}+lower_inc) || upper_sum!==({1'b0,hits[63:32]}+upper_inc))$fatal(1,"late hit delta/carry mismatch");
  27. checks=checks+1;
  28. end
  29. endtask
  30. initial begin
  31. boundary[0]=0;boundary[1]=1;boundary[2]=32'hFFFF;boundary[3]=32'h10000;
  32. boundary[4]=32'h7FFFFFFF;boundary[5]=32'h80000000;boundary[6]=32'hFFFFFFF0;boundary[7]=32'hFFFFFFFF;
  33. repeat(3)@(negedge clk);rst_n=1;
  34. for(test_id=0;test_id<1024;test_id=test_id+1)begin
  35. total=test_id<128?boundary[test_id%8]:random_word();
  36. executed=test_id<128?boundary[(test_id/8)%8]:random_word();
  37. position=test_id<128?boundary[test_id%8]:random_word();
  38. remaining=test_id<128?boundary[(test_id/8)%8]:random_word();
  39. hits=test_id<128?{boundary[(test_id/8)%8],boundary[test_id%8]}:{random_word(),random_word()};
  40. up=test_id[3];
  41. for(phase_id=0;phase_id<6;phase_id=phase_id+1)begin
  42. // Only the final delta inputs may affect committed arithmetic.
  43. rise=random_word();lower_inc=random_word();upper_inc=random_word();unlimited=random_word();
  44. if(phase_id==5 && test_id<128)begin
  45. rise=test_id[4];unlimited=test_id[5];lower_inc=(test_id>>3)&15;upper_inc=15-lower_inc;
  46. end
  47. #1;
  48. if(commit!==(phase_id==5))$fatal(1,"service phase/bound mismatch");
  49. if(commit)check_result();
  50. @(negedge clk);
  51. end
  52. end
  53. $display("COUNTER_CHECKS=%0d SERVICE_CYCLES=6",checks);
  54. $display("motor_counter_math_tb PASS");$finish;
  55. end
  56. initial begin #1000000;$fatal(1,"counter test deadline");end
  57. endmodule