I want to make a calculator based on fpga board(spartan 3). I have this following code
module bcd_converter(
input [7:0] R,
output reg [3:0] Hundreds,
output reg [3:0] Tens,
output reg [3:0] Ones
);
integer i;
always @ (R)
begin
Hundreds = 4'd0;
Tens = 4'd0;
Ones = 4'd0;
for(i=7;i>=0;i = i-1) begin
if(Hundreds >= 5)
Hundreds = Hundreds + 3;
if(Tens >= 5)
Tens = Tens + 3;
if(Ones >= 5)
Ones = Ones + 3;
Hundreds = Hundreds<<1;
Hundreds[0] = Tens[3];
Tens = Tens << 1;
Tens[0] = Ones[3];
Ones = Ones<<1;
Ones[0] = R[i];
end
end
endmodule
However, the code provided does conversion only for binary into BCD. I am looking for a way to reverse this algorithm. Is there any better way of converting BCD into binary?
You could decode each BCD digit into binary with separate case statements, and just add the results. For example, for hundreds your case statement would look like this:
case(hundreds)
4'd0 : hundreds_bin = 10'd0;
4'd1 : hundreds_bin = 10'd100;
4'd2 : hundreds_bin = 10'd200;
4'd3 : hundreds_bin = 10'd300;
//..etc etc