I would like to know if there is any function/method, in VHDL, to
extract the digits of an integer.
What I wish to do is take, for example, an integer like 2043, and
extract first the number '3', then '4', '0', and '2'.
Each digit will then be converted to a std_logic_vector(3 downto 0).
Thank you!,
m m s
So you're trying to convert decimal to BCD ("binary coded decimal").
There's a neat shift-and-subtract trick that has been mentioned here
many times before, and maps easily to hardware - google for
"binary to BCD fpga" should do it.
How is your integer represented? Is it a binary number (an
integer signal or variable, or a std_logic_vector or numeric_std
signal or variable?
A non-synthesisable version might look like this - note it will do
hex, binary and octal conversion as well...
subtype BCD_digit is std_logic_vector(3 downto 0);
type BCD_number is array (natural range <>) of BCD_digit;
...
function to_BCD(N: natural; radix: integer := 10)
return BCD_number
is
variable digits: BCD_number (0 to 31); -- big enough for 2^31
variable more_digits: integer := N;
begin
assert (radix >= 2) and (radix <= 16)
report "Bad radix, must be between 2 and 16"
severity ERROR;
for i in digits'range loop
digits(i) := std_logic_vector(
to_unsigned(more_digits rem radix, BCD_digit'length) );
more_digits := more_digits / radix;
if (more_digits = 0) then
return digits(0 to i);
end if;
end loop;
report "Overflow in to_BCD" severity ERROR;
return (0 => "----");
end function to_BCD;
enjoy :-)
--
Jonathan Bromley, Consultant
DOULOS - Developing Design Know-how
VHDL * Verilog * SystemC * e * Perl * Tcl/Tk * Project Services
Doulos Ltd., 22 Market Place, Ringwood, BH24 1AW, UK
jonathan...@MYCOMPANY.com
http://www.MYCOMPANY.com
The contents of this message may contain personal views which
are not the views of Doulos Ltd., unless specifically stated.
It is an integer signal that holds the "i" of a counter. The counter
goes from 0 to 2046. I needed each digit separately to display it on
the LCD screen that has the Spartan-3A board.
--m m _s
-----------------
On Jul 13, 8:07 am, Jonathan Bromley <jonathan.brom...@MYCOMPANY.com>
wrote:
There is more than one way to skin a cat. If you are incrementing the
counter, then you can just form a BCD counter and increment that.
That is likely going to use fewer resources than the conversion
code.
Rick
you can have 4 counters for each integer.
each can advanced 1 when the before one reached 10.
On Jul 12, 11:31 pm, --mms <msmeer...@gmail.com> wrote:
(...)