2
\$\begingroup\$

I found the method for c language.But I do not know how to perform this in VHDL. Let a fixed point number (12 downto -19) like 3456.478396 I need break this number entirely into separate numbers 3456.478396 --> 3, 4, 5, 6, (dot) , 4, 7, 8, 3, 9, 6... into 11 integer type variables. How to perform this in VHDL?

signal example : sfixed(4 downto -4);
--'example' contains 9.75 then it is storage as "01001.1100".
--simply example ="10011100".
--10011100 --> integer 156

How to get back original 9.75 number from 156 to separate into single digits?

I USED,

library ieee;
library ieee_proposed;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
use ieee_proposed.float_pkg.ALL;
use ieee_proposed.fixed_pkg.ALL;
use ieee_proposed.fixed_float_types.ALL;
\$\endgroup\$
  • \$\begingroup\$ How is the original number represented? How do you want it to be represented once "broken"? \$\endgroup\$ – Eugene Sh. Jul 25 '17 at 16:07
  • 2
    \$\begingroup\$ depends how the number is held. If in BCD, then it's easy. If it's in binary, then you have to convert to decimal first. \$\endgroup\$ – Neil_UK Jul 25 '17 at 16:07
  • \$\begingroup\$ @Eugene Sh in the question I have mentioned that I need break this number entirely into separate numbers 3456.478396 --> 3, 4, 5, 6, (dot) , 4, 7, 8, 3, 9, 6... into 11 integer type variables. \$\endgroup\$ – oppo Jul 25 '17 at 16:38
  • \$\begingroup\$ See here: stackoverflow.com/questions/23871792/… Once your binary integer is converted to BCD, each digit is separate... \$\endgroup\$ – Mishyoshi Jul 25 '17 at 17:06
  • 1
    \$\begingroup\$ @Mishyoshi That doesn't solve the right hand side of the problem. The decimal values. \$\endgroup\$ – Harry Svensson Jul 25 '17 at 17:08
4
\$\begingroup\$

Here's some pseudocode.

Let's take the ones left of the decimal first, max number is \$15_{10}\$ => 2 decimal digits.

signal ex : sfixed(4 downto -4);

temp <= ex(4 downto 0);
L_0 <= temp mod 10;
temp <= temp/10;
L_1 <= temp mod 10;

Example:
ex=\$12_{10}\$=\$1100_{2}\$

temp <= 12
L_0 <= 12 mod 10 = 2
temp <= 12/10    = 1
L_1 <= 1 mod 10  = 1

And then the ones to the right, smallest number => 0.0625 => 4 decimal digits. Same method as before, just multiply by \$625_{10}\$ before you start. Because \$0001_{2}\$ in binary translates to \$.0625_{10}\$ in decimal. So the bit width of temp should be \$ceil(log_2(625×15))=\$ 14 bits because \$15_{10}\$ is \$1111_{2}\$ which is your highest value.

signal ex : sfixed(4 downto -4);

temp <= ex(0 downto -4);
temp <= temp*625

R_3 <= temp mod 10;
temp <= temp/10;
R_2 <= temp mod 10;
temp <= temp/10;
R_1 <= temp mod 10;
temp <= temp/10;
R_0 <= temp mod 10;

Example:
ex=\$0.1875_{10}\$=\$0011_{2}\$

temp <= 3 = 0011
temp <= 3*625 = 11101010011        = 1875

R_3 <= 1875 mod 10                 = 5
temp <= 1875/10                    = 187
R_2 <= 187 mod 10                  = 7
temp <= 187/10                     = 18
R_1 <= 18 mod 10                   = 8
temp <= 18/10                      = 1
R_0 <= 1 mod 10                    = 1

And it should be displayed like this:

\$L_1 L_0 . R_0 R_1 R_2 R_3\$

All the constant division by 10 can be replaced by a multiplication with \$\frac{1}{10}\$. So it's not too inefficient to implement. I'm fairly certain that the modulus operator comes with the standard library.

If you use 6 bits for the right hand side of the decimal point. Then the bit width for temp should be \$ceil(log2(63×15625))\$ = 20 bits. This is probably not the most efficient way, it's a way I came up with just now on my own. It should work without any major problems.

Since this is being done on an FPGA you can parallelize the multiplication, for R3,R2,R1 and R0, multiply their temp with 1, \$\frac{1}{10}\$, \$\frac{1}{100}\$ and \$\frac{1}{1000}\$ so you get the values straight away, instead of serially. And then use the mod 10 on them.


EDIT

Mishyoshi gave me an idea for how this could be solved in another way. \$L_0\$ and \$L_1\$ are solved like before, but the \$R_{0..3}\$ can be done in another more efficient way.

signal ex : sfixed(4 downto -4);

signal temp : sfixed(4 downto -4); 
//We need 4 to the left of the decimal point because then we can capture
//all the single digits

temp <= "0000" & ex(0 downto -4);
temp <= temp*10;

R_0 <= temp(3 downto 0); //Copy the 4 MSB bits 
temp <= "0000" & temp(-1 downto -4); //Delete the 4 MSB bits
temp <= temp*10;
R_1 <= temp(3 downto 0);
temp <= "0000" & temp(-1 downto -4);
temp <= temp*10;
R_2 <= temp(3 downto 0);
temp <= "0000" & temp(-1 downto -4);
temp <= temp*10;
R_3 <= temp(3 downto 0);

Example:
ex=\$0.1875_{10}\$=\$0011_{2}\$

signal temp : sfixed(4 downto -4);

temp <= "0000" & "0011"                     = 0000.0011 = 3
temp <= 3*10                           = 30 = 0001.1110 
R_0 <= "0001.1110"(3 downto 0)              = 0001 = 1
temp <= "0000" & temp(-1 downto -4)         = 0000.1110
temp <= 14*10                         = 140 = 1000.1100
R_1 <= "1000.1100"(3 downto 0)              = 1000 = 8
temp <= "0000" & "1100"                     = 0000.1100
temp <= 12*10                         = 120 = 0111.1000
R_2 <= "0111.1000"(3 downto 0)              = 0111 = 7
temp <= "0000" & "1000"                     = 0000.1000
temp <= 8*10                           = 80 = 0101.0000
R_3 <= "0101.0000"(3 downto 0)              = 0101 = 5

As you can see it's all about getting the bits to the "ones" place, the 4 bits to the left of the decimal point.

That's way fewer bits needed for this. In this other solution you can, like Mishyoshi said, put them in a pipeline.

\$\endgroup\$
  • \$\begingroup\$ I do not understand what is, and how is this got smallest number => 0.0625 => 4 decimal digits. the number 625 \$\endgroup\$ – oppo Jul 25 '17 at 18:52
  • 1
    \$\begingroup\$ if you put 1/16 in to the calculator, then it will show 0.0625, if you just look at the non-zero-numbers it says 625. That means that a 0000.0001 in binary is 0.0625 in decimal. So the "weight" of the 1 is 1/16. But since we're on the "right hand side", we can just ignore the decimal point and treat it as 0625. In other words, just multiply by 625. Because we're just converting to BCD, we're not doing anything fancy. \$\endgroup\$ – Harry Svensson Jul 25 '17 at 18:57
  • 1
    \$\begingroup\$ If it would be 6 bits then you would multiply by 15625 because the 0000.000001 in binary is 0.015625 in decimal. Remove the decimal and you got 000001 is 015625. So multiply the 1 by 015625 and then start chopping out the digits. \$\endgroup\$ – Harry Svensson Jul 25 '17 at 19:03
  • 1
    \$\begingroup\$ I might be wrong but for a 4 bit fixed point fractionnal value, you can perform a binary multiply of 10000 because it is valued 0.0625 (4digit => 10^4). Then convert that to BCD using link i provided. Value will be 10000 times too large, so consider decimal point 4 position to the left. There would only be a single multiply with readily available multiplier and a bunch of adders which could be pipelined. \$\endgroup\$ – Mishyoshi Jul 26 '17 at 3:06
  • \$\begingroup\$ Hmm, you're onto something @Mishyoshi. You need to do something more than just multiply by \$10^4\$ though. \$\endgroup\$ – Harry Svensson Jul 26 '17 at 3:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.