Binary Divider
Binary Divider
use ieee.std_logic_1164.all;
entity divider is
port (St, Clk: in std_logic;
dend: in std_logic_vector(7 downto 0);
dsor: in std_logic_vector(3 downto 0);
v: out std_logic;
qent: out std_logic_vector(3 downto 0)
Rem:out std_logic_vector(3 downto 0));
end divider;
Case state is
When 0=> if (St=1) then Ld<=1; nxState<=1;
Else nxstate<=0; end if;
When 1=> if(C=1) then V<=1; nxstate<=0;
Else Sh<=1; nxState<=2; end if;
When 2 => if (C=1) then Su<=1; nxstate<=State;
Else Sh<=1; nxstate<=state + 1; end if;
When 3 => if (C=1) then Su<=1; nxstate<=State;
Else Sh<=1; nxstate<=state + 1; end if;
When 4 => if (C=1) then Su<=1; nxstate<=State;
Else Sh<=1; nxstate<=state + 1; end if;
When 5 => if (C=1) then Su<=1; nxstate<=State;
Else Sh<=1; nxstate<=0; end if;
end case;
end process;
process (Clk)
begin
if (Clk=1 and Clkevent) then
state<=nxState;
if (Ld=1) then
DendR<=0 & dend;
DsorR<=dsor;
end if;
If (Sh=1) then
DendR<= DendR (7 downto 0) & 0;
end if;
If (Su=1) then
DendR(8 downto 4) <=sub(4 downto 0);
DendR(0)<=1;
end if;
End if;
End process;
End divider;
entity sdiv is
port(Clk,St: in bit;
Dbus: in bit_vector(15 downto 0); Quotient: out
bit_vector(15 downto 0);
V, Rdy: out bit);
end sdiv;
process
begin
wait until Clk = '1'; -- wait for rising edge of clock
case State is
when 0=>
if St = '1' then
Acc <= Dbus; -- load upper dividend
Sign <= Dbus(15); State <= 1;
V <= '0'; Count <= 0; -- initialize overflow// initialize counter
end if;
when 1=>
Q <= Dbus; State <= 2; -- load lower dividend
when 2=>
Divisor <= Dbus;
if Sign ='1'then -- two's complement Dividend if necessary
addvec(not Dividend,zero_vector,'1',Dividend,NC,32);
end if; State <= 3;
when 3=>
Dividend <= Dividend(30 downto 0) & '0'; -- left shift
Count <= Count+1; State <= 4;
when 4 =>
if C ='1' then -- C
v <= '1'; State <= 0;
else -- C'
Dividend <= Dividend(30 downto 0) & '0'; -- left shift
Count <= Count+1; State <= 5;
end if;
when 5 =>
if C = '1' then -- C
ACC <= Sum; -- subtract
Q(0)<= '1';
else
Dividend <= Dividend(30 downto 0) & '0'; -- left shift
if Count = 15 then -- KC'
count<= 0; State <= 6;
else Count <= Count+1;
end if;
end if;
when 6=>
if C = '1' then -- C
Acc <= Sum; -- subtract
Q(0) <= '1';
else if (Sign xor Divisor(15))='1' then -- C'Qneg
addvec(not Dividend,zero_vector,'1',Dividend,NC,32);
end if; -- 2's complement Dividend
state <= 0;
end if;
end case;
end process;
end signdiv