shaiko
Advanced Member level 5
Hello,
I'm using the following division function in my VHDL code:
----------------------------------------------------------------
function divide (a : UNSIGNED; b : UNSIGNED) return UNSIGNED is
variable a1 : unsigned(a'length-1 downto 0):=a;
variable b1 : unsigned(b'length-1 downto 0):=b;
variable p1 : unsigned(b'length downto 0):= (others => '0');
variable i : integer:=0;
begin
for i in 0 to b'length-1 loop
p1(b'length-1 downto 1) := p1(b'length-2 downto 0);
p1(0) := a1(a'length-1);
a1(a'length-1 downto 1) := a1(a'length-2 downto 0);
p1 := p1-b1;
if(p1(b'length-1) ='1') then
a1(0) :='0';
p1 := p1+b1;
else
a1(0) :='1';
end if;
end loop;
return a1;
end divide;
----------------------------------------------------------------
I didn't write it myself. The code can be found at:
VHDL coding tips and tricks: A VHDL function for division two unsigned numbers
The function works well in cases when the numerator is bigger then the denominator but it fails in other cases.
For example:
result <= divide("11111", "10111"); -- should give "00001" (a decimal one)
However, it gives "10011" instead (wich is a decimal 19).
What's wrong with the code?
I'm using the following division function in my VHDL code:
----------------------------------------------------------------
function divide (a : UNSIGNED; b : UNSIGNED) return UNSIGNED is
variable a1 : unsigned(a'length-1 downto 0):=a;
variable b1 : unsigned(b'length-1 downto 0):=b;
variable p1 : unsigned(b'length downto 0):= (others => '0');
variable i : integer:=0;
begin
for i in 0 to b'length-1 loop
p1(b'length-1 downto 1) := p1(b'length-2 downto 0);
p1(0) := a1(a'length-1);
a1(a'length-1 downto 1) := a1(a'length-2 downto 0);
p1 := p1-b1;
if(p1(b'length-1) ='1') then
a1(0) :='0';
p1 := p1+b1;
else
a1(0) :='1';
end if;
end loop;
return a1;
end divide;
----------------------------------------------------------------
I didn't write it myself. The code can be found at:
VHDL coding tips and tricks: A VHDL function for division two unsigned numbers
The function works well in cases when the numerator is bigger then the denominator but it fails in other cases.
For example:
result <= divide("11111", "10111"); -- should give "00001" (a decimal one)
However, it gives "10011" instead (wich is a decimal 19).
What's wrong with the code?
Last edited: