From d33639a2fcaddc6a0cfc3eb9aee0a119e03047b6 Mon Sep 17 00:00:00 2001 From: bsdevlin Date: Sun, 6 Sep 2020 18:50:06 -0400 Subject: [PATCH] update barret mod overflow control --- ip_cores/util/src/rtl/barret_mod.sv | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/ip_cores/util/src/rtl/barret_mod.sv b/ip_cores/util/src/rtl/barret_mod.sv index e90a029..9a3f940 100644 --- a/ip_cores/util/src/rtl/barret_mod.sv +++ b/ip_cores/util/src/rtl/barret_mod.sv @@ -24,7 +24,7 @@ module barret_mod #( parameter CTL_BITS = 8, parameter IN_BITS = 512, parameter [OUT_BITS-1:0] P = 256'hFFFFFFFF_FFFFFFFF_FFFFFFFF_FFFFFFFE_BAAEDCE6_AF48A03B_BFD25E8C_D0364141, - parameter K = $clog2(P), + parameter K = OUT_BITS, parameter MULTIPLIER = "EXTERNAL" // [ACCUM_MULT || KARATSUBA || EXTERNAL] )( input i_clk, @@ -48,11 +48,12 @@ logic [2:0][CTL_BITS-1:0] ctl_r; if_axi_stream #(.DAT_BITS(2*(OUT_BITS+2))) mult_in_if(i_clk); if_axi_stream #(.DAT_BITS(2*(OUT_BITS+2))) mult_out_if(i_clk); -logic [MAX_IN_BITS:0] c1, c2, c3, c4, c2_, P_; +logic [MAX_IN_BITS:0] c1, c2, c3, c4, P_; +logic c4_grt; always_comb begin - P_ = 0; - P_[OUT_BITS-1:0] = P; + P_ = {{(MAX_IN_BITS-OUT_BITS-1){1'b0}}, P}; + c4_grt = c4 >= P_; end typedef enum {IDLE, S0, S1, S2, FINISHED, WAIT_MULT} state_t; @@ -88,7 +89,6 @@ always_ff @ (posedge i_clk) begin mult_in_if.dat[0 +: OUT_BITS+1] <= i_dat >> (K-1); mult_in_if.dat[OUT_BITS+1 +: OUT_BITS+1] <= U; prev_state <= S0; - c2_ <= (i_dat >> (K - 1))*U; // Using multiplier interface end end {S0}: begin @@ -105,8 +105,9 @@ always_ff @ (posedge i_clk) begin ctl_r[2] <= ctl_r[1]; end {S2}: begin - if (c4 >= P_) begin + if (c4_grt) begin c4 <= c4 - P_; + state <= S2; end else begin state <= FINISHED; o_dat <= c4; @@ -128,9 +129,14 @@ always_ff @ (posedge i_clk) begin case(prev_state) S0: c2 <= mult_out_if.dat; S2: c4 <= c4 - mult_out_if.dat; + default: begin end endcase end end + default: begin + o_val <= 0; + state <= IDLE; + end endcase end end @@ -205,4 +211,4 @@ generate endgenerate initial assert (IN_BITS <= MAX_IN_BITS) else $fatal(1, "%m ERROR: IN_BITS[%d] > MAX_IN_BITS[%d] in barret_mod", IN_BITS, MAX_IN_BITS); -endmodule \ No newline at end of file +endmodule