|
@@ -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
|