M
Mat18111992
Guest
Hello everyone. I had a code for fixed point division by Verilog. I tried to rewrite it with VHDL but not successfull. Can somebody help me? Thanks so much.
module division #(
//Parameterized values
parameter Q = 4, // number of fraction bits
parameter N = 8
)
(
input [N-1:0] i_dividend,
input [N-1:0] i_divisor,
input i_start,
input i_clk,
output [N-1:0] o_quotient_out,
output o_complete,
output o_overflow
);
reg [2*N+Q-3:0] reg_working_quotient;// Our working copy of the quotient
reg [N-1:0] reg_quotient; // Final quotient
reg [N-2+Q:0] reg_working_dividend;// Working copy of the dividend
reg [2*N+Q-3:0] reg_working_divisor; // Working copy of the divisor
reg [$clog2(N-1+Q):0] reg_count;
reg reg_done; // Computation completed flag
reg reg_sign; // The quotient's sign bit
reg reg_overflow; // Overflow flag
initial reg_done = 1'b1;
initial reg_overflow = 1'b0;
initial reg_sign = 1'b0;
initial reg_working_quotient = 0;
initial reg_quotient = 0;
initial reg_working_dividend = 0;
initial reg_working_divisor = 0;
initial reg_count = 0;
assign o_quotient_out[N-2:0] = reg_quotient[N-2:0];//division results
assign o_quotient_out[N-1] = reg_sign; //The sign of the quotient
assign o_complete = reg_done;
assign o_overflow = reg_overflow;
always @( posedge i_clk ) begin
if( reg_done && i_start ) begin //This is our startup condition
reg_done<=1'b0; reg_count<=N+Q-1; reg_working_quotient<=0; reg_working_dividend<=0; reg_working_divisor<=0; reg_overflow <= (i_divisor[N-2:0])?1'b0:1'b1;// check divisor =0 or !=0
reg_working_dividend[N+Q-2:Q] <= i_dividend[N-2:0];
reg_working_divisor[2*N+Q-3:N+Q-1] <= i_divisor[N-2:0];
reg_sign <= i_dividend[N-1] ^ i_divisor[N-1];
end
else if(!reg_done) begin
reg_working_divisor <= reg_working_divisor >> 1;
if(reg_working_dividend >= reg_working_divisor) begin
reg_working_quotient[reg_count<=1'b1; reg_working_dividend <= reg_working_dividend - reg_working_divisor;
end
if(!(reg_count | 0)) begin //count = 0
reg_done<=1'b1;
reg_quotient <= reg_working_quotient;
reg_overflow <= (reg_working_quotient[2*N+Q-3:N]) ? 1'b1 : 1'b0;
end
else
reg_count <= reg_count - 1;
end
end
endmodule
module division #(
//Parameterized values
parameter Q = 4, // number of fraction bits
parameter N = 8
)
(
input [N-1:0] i_dividend,
input [N-1:0] i_divisor,
input i_start,
input i_clk,
output [N-1:0] o_quotient_out,
output o_complete,
output o_overflow
);
reg [2*N+Q-3:0] reg_working_quotient;// Our working copy of the quotient
reg [N-1:0] reg_quotient; // Final quotient
reg [N-2+Q:0] reg_working_dividend;// Working copy of the dividend
reg [2*N+Q-3:0] reg_working_divisor; // Working copy of the divisor
reg [$clog2(N-1+Q):0] reg_count;
reg reg_done; // Computation completed flag
reg reg_sign; // The quotient's sign bit
reg reg_overflow; // Overflow flag
initial reg_done = 1'b1;
initial reg_overflow = 1'b0;
initial reg_sign = 1'b0;
initial reg_working_quotient = 0;
initial reg_quotient = 0;
initial reg_working_dividend = 0;
initial reg_working_divisor = 0;
initial reg_count = 0;
assign o_quotient_out[N-2:0] = reg_quotient[N-2:0];//division results
assign o_quotient_out[N-1] = reg_sign; //The sign of the quotient
assign o_complete = reg_done;
assign o_overflow = reg_overflow;
always @( posedge i_clk ) begin
if( reg_done && i_start ) begin //This is our startup condition
reg_done<=1'b0; reg_count<=N+Q-1; reg_working_quotient<=0; reg_working_dividend<=0; reg_working_divisor<=0; reg_overflow <= (i_divisor[N-2:0])?1'b0:1'b1;// check divisor =0 or !=0
reg_working_dividend[N+Q-2:Q] <= i_dividend[N-2:0];
reg_working_divisor[2*N+Q-3:N+Q-1] <= i_divisor[N-2:0];
reg_sign <= i_dividend[N-1] ^ i_divisor[N-1];
end
else if(!reg_done) begin
reg_working_divisor <= reg_working_divisor >> 1;
if(reg_working_dividend >= reg_working_divisor) begin
reg_working_quotient[reg_count<=1'b1; reg_working_dividend <= reg_working_dividend - reg_working_divisor;
end
if(!(reg_count | 0)) begin //count = 0
reg_done<=1'b1;
reg_quotient <= reg_working_quotient;
reg_overflow <= (reg_working_quotient[2*N+Q-3:N]) ? 1'b1 : 1'b0;
end
else
reg_count <= reg_count - 1;
end
end
endmodule