Bläddra i källkod

Prototype six-cycle shared counter arithmetic with late-event inputs

codex 2 månader sedan
förälder
incheckning
a148af9faf

+ 1 - 0
config/current-product-tests.json

@@ -40,6 +40,7 @@
         "motor_engine_12_tb",
         "motor_engine_12_tb",
         "motor_engine_tb",
         "motor_engine_tb",
         "motor_queue_storage_tb",
         "motor_queue_storage_tb",
+        "motor_counter_math_tb",
         "motor_event_ring_tb",
         "motor_event_ring_tb",
         "motor_history_shared_tb",
         "motor_history_shared_tb",
         "motor_history_shared_12_tb",
         "motor_history_shared_12_tb",

+ 62 - 0
src/motor/motor_counter_math.v

@@ -0,0 +1,62 @@
+`timescale 1ns/1ps
+// Six-cycle counter arithmetic service. State inputs and direction must remain
+// stable until commit; rise/lower_inc/upper_inc are sampled at commit and may
+// change at any earlier phase. No STEP scheduling or event state lives here.
+module motor_counter_math(
+ input wire clk,rst_n,
+ input wire [31:0] total,executed,position,remaining,
+ input wire [63:0] hits,
+ input wire up,rise,unlimited,
+ input wire [3:0] lower_inc,upper_inc,
+ output wire commit,
+ output wire [32:0] total_sum,exec_sum,lower_sum,upper_sum,
+ output wire [31:0] position_next,remaining_next,
+ output wire position_overflow
+);
+ reg [2:0] phase;
+ reg [32:0] total_increment,exec_increment;
+ reg [31:0] position_step,remaining_step;
+ reg [16:0] lower_high_increment,upper_high_increment;
+ reg carry_a,carry_b;
+ reg [15:0] left_a,right_a,left_b,right_b;
+ reg sub_a,sub_b;
+ wire [16:0] result_a={1'b0,left_a}+{1'b0,(right_a^{16{sub_a}})}+sub_a;
+ wire [16:0] result_b={1'b0,left_b}+{1'b0,(right_b^{16{sub_b}})}+sub_b;
+ assign commit=phase==5;
+ always @* begin
+  left_a=0;right_a=0;left_b=0;right_b=0;sub_a=0;sub_b=0;
+  case(phase)
+   0:begin left_a=total[15:0];left_b=executed[15:0];right_a=1;right_b=1;end
+   1:begin left_a=total[31:16];left_b=executed[31:16];right_a={15'd0,carry_a};right_b={15'd0,carry_b};end
+   2:begin left_a=position[15:0];left_b=remaining[15:0];right_a=1;right_b=1;sub_a=!up;sub_b=1;end
+   3:begin left_a=position[31:16];left_b=remaining[31:16];right_a={15'd0,carry_a};right_b={15'd0,carry_b};sub_a=!up;sub_b=1;end
+   4:begin left_a=hits[31:16];left_b=hits[63:48];right_a=1;right_b=1;end
+   5:begin left_a=hits[15:0];left_b=hits[47:32];right_a={12'd0,lower_inc};right_b={12'd0,upper_inc};end
+   default:begin end
+  endcase
+ end
+ always @(posedge clk or negedge rst_n)begin
+  if(!rst_n)phase<=0;
+  else phase<=commit ? 0 : phase+1'b1;
+ end
+ // Every intermediate is written before commit after reset.
+ always @(posedge clk)if(rst_n)begin
+  case(phase)
+   0:begin total_increment[15:0]<=result_a[15:0];exec_increment[15:0]<=result_b[15:0];carry_a<=result_a[16];carry_b<=result_b[16];end
+   1:begin total_increment[32:16]<=result_a;exec_increment[32:16]<=result_b;end
+   2:begin position_step[15:0]<=result_a[15:0];remaining_step[15:0]<=result_b[15:0];carry_a<=up ? result_a[16] : !result_a[16];carry_b<=!result_b[16];end
+   3:begin position_step[31:16]<=result_a[15:0];remaining_step[31:16]<=result_b[15:0];end
+   4:begin lower_high_increment<=result_a;upper_high_increment<=result_b;end
+   default:begin end
+  endcase
+ end
+ assign total_sum=rise ? total_increment : {1'b0,total};
+ assign exec_sum=rise ? exec_increment : {1'b0,executed};
+ assign position_next=rise ? position_step : position;
+ assign remaining_next=(rise && !unlimited) ? remaining_step : remaining;
+ assign position_overflow=rise && (up ? (!position[31] && position_step[31]) : (position[31] && !position_step[31]));
+ assign lower_sum={result_a[16] && lower_high_increment[16],
+  (result_a[16] ? lower_high_increment[15:0] : hits[31:16]),result_a[15:0]};
+ assign upper_sum={result_b[16] && upper_high_increment[16],
+  (result_b[16] ? upper_high_increment[15:0] : hits[63:48]),result_b[15:0]};
+endmodule

+ 58 - 0
tests/sim/motor_counter_math_tb.v

@@ -0,0 +1,58 @@
+`timescale 1ns/1ps
+`include "src/motor/motor_counter_math.v"
+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