| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- `timescale 1ns/1ps
- module motor_counter_math_tb;
- reg clk=0;always #20 clk=~clk;
- reg rst_n=0,up=0,rise=0,unlimited=0;
- reg [31:0] total=0,executed=0,position=0,remaining=0;
- reg [63:0] hits=0;
- reg [3:0] lower_inc=0,upper_inc=0;
- wire commit,overflow;
- wire [32:0] total_sum,exec_sum,lower_sum,upper_sum;
- wire [31:0] position_next,remaining_next;
- motor_counter_math dut(clk,rst_n,total,executed,position,remaining,hits,up,rise,unlimited,
- lower_inc,upper_inc,commit,total_sum,exec_sum,lower_sum,upper_sum,position_next,remaining_next,overflow);
- reg [31:0] random_state=32'h819234AB;
- function [31:0] random_word;
- 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
- endfunction
- reg [31:0] boundary[0:7];
- integer test_id,phase_id,checks=0;
- reg [31:0] ref_pos;
- task check_result;
- begin
- ref_pos=rise ? (up ? position+32'd1 : position-32'd1) : position;
- if(total_sum!==({1'b0,total}+rise) || exec_sum!==({1'b0,executed}+rise))$fatal(1,"increment/carry mismatch");
- if(position_next!==ref_pos || remaining_next!==((rise && !unlimited)?remaining-32'd1:remaining))$fatal(1,"position/remaining mismatch");
- if(overflow!==(rise && (up ? position==32'h7FFFFFFF : position==32'h80000000)))$fatal(1,"signed overflow mismatch");
- 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");
- checks=checks+1;
- end
- endtask
- initial begin
- boundary[0]=0;boundary[1]=1;boundary[2]=32'hFFFF;boundary[3]=32'h10000;
- boundary[4]=32'h7FFFFFFF;boundary[5]=32'h80000000;boundary[6]=32'hFFFFFFF0;boundary[7]=32'hFFFFFFFF;
- repeat(3)@(negedge clk);rst_n=1;
- for(test_id=0;test_id<1024;test_id=test_id+1)begin
- total=test_id<128?boundary[test_id%8]:random_word();
- executed=test_id<128?boundary[(test_id/8)%8]:random_word();
- position=test_id<128?boundary[test_id%8]:random_word();
- remaining=test_id<128?boundary[(test_id/8)%8]:random_word();
- hits=test_id<128?{boundary[(test_id/8)%8],boundary[test_id%8]}:{random_word(),random_word()};
- up=test_id[3];
- for(phase_id=0;phase_id<6;phase_id=phase_id+1)begin
- // Only the final delta inputs may affect committed arithmetic.
- rise=random_word();lower_inc=random_word();upper_inc=random_word();unlimited=random_word();
- if(phase_id==5 && test_id<128)begin
- rise=test_id[4];unlimited=test_id[5];lower_inc=(test_id>>3)&15;upper_inc=15-lower_inc;
- end
- #1;
- if(commit!==(phase_id==5))$fatal(1,"service phase/bound mismatch");
- if(commit)check_result();
- @(negedge clk);
- end
- end
- $display("COUNTER_CHECKS=%0d SERVICE_CYCLES=6",checks);
- $display("motor_counter_math_tb PASS");$finish;
- end
- initial begin #1000000;$fatal(1,"counter test deadline");end
- endmodule
|