Skip to content

Commit

Permalink
update barret mod overflow control
Browse files Browse the repository at this point in the history
  • Loading branch information
bsdevlin committed Sep 6, 2020
1 parent 0c6783c commit d33639a
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions ip_cores/util/src/rtl/barret_mod.sv
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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
endmodule

0 comments on commit d33639a

Please sign in to comment.