2
votes

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?

1
For 2 BCD digits there are only 100 values, so you could use a case statement with 100 entries and let the compiler do the work for you.aja

1 Answers

1
votes

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 = 9'd0;
   4'd1 : hundreds_bin = 9'd100;
   4'd2 : hundreds_bin = 9'd200;
   4'd3 : hundreds_bin = 9'd300;
//..etc etc