"weizbox" <
[email protected]> wrote in message
news:
[email protected] om...
> Hello,
>
> I was wondering if there is a way to easily change a bin number to a
> bcd without a lookup table or anyhting else like that if the numbers
> do not need to convert.
>
> Example: I have a decimal value of 1234 in a 12-bit binary reg. I
> would like to transfer those values to a 16-bit bcd. No conversions
> needed, I still want 1234, just in bcd. Is there an easy way to do
> this?
>
> -Mark
How about this for 4 digits: (some of the LSbits can be truncated - I just
haven't done an error analysis or testbench)
wire [22:0] res3;
wire [21:0] res2;
wire [20:0] res1;
wire [19:0] res2;
wore [3:0] bcd3, bcd2, bcd1, bcd0;
assign {bcd3,res3} = bin[12:0] * 14'h20c5; // 5 adds, 4 adders in a tree
assign {bcd2,res2} = res3 * 3'h5;
assign {bcd1,res1} = res2 * 3'h5;
assign {bcd0,res0} = res1 * 3'h5;
The idea here is use a fixed/flaoting-point hybrid. The first thing you'd
like is to "effectively" divide by 1000 which - though close at 131/131072
(131/2^17) provides better results at 8389/8388608 (8389/2^23). The first
BCD digit is the multiplied result shifted 23 bits. The fractional part is
effectively multiplied by 10 ("fractional" multiplied by 5 and shifted 1)
to get the next digit 22 bits out.
The implementation looks better in code and can be manipulated further for a
smaller implementation with the appropriate error analysis.
The idea is the same in software: find the first digit by dividing by 1000
(or 10000, whatever) then get successive digits from 10x the fractional
part.