1 |
2 |
dimamali |
------------------------------------------------------------------------------
|
2 |
|
|
-- This file is a part of the GRLIB VHDL IP LIBRARY
|
3 |
|
|
-- Copyright (C) 2003, Gaisler Research
|
4 |
|
|
--
|
5 |
|
|
-- This program is free software; you can redistribute it and/or modify
|
6 |
|
|
-- it under the terms of the GNU General Public License as published by
|
7 |
|
|
-- the Free Software Foundation; either version 2 of the License, or
|
8 |
|
|
-- (at your option) any later version.
|
9 |
|
|
--
|
10 |
|
|
-- This program is distributed in the hope that it will be useful,
|
11 |
|
|
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
|
|
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
|
|
-- GNU General Public License for more details.
|
14 |
|
|
--
|
15 |
|
|
-- You should have received a copy of the GNU General Public License
|
16 |
|
|
-- along with this program; if not, write to the Free Software
|
17 |
|
|
-- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
18 |
|
|
-----------------------------------------------------------------------------
|
19 |
|
|
-- Entity: div32
|
20 |
|
|
-- File: div32.vhd
|
21 |
|
|
-- Author: Jiri Gaisler - Gaisler Research
|
22 |
|
|
-- Description: This unit implemets a divide unit to execute 64-bit by 32-bit
|
23 |
|
|
-- division. The divider leaves no remainder.
|
24 |
|
|
-- Overflow detection is performed according to the
|
25 |
|
|
-- SPARC V8 manual, method B (page 116)
|
26 |
|
|
-- Division is made using the non-restoring algorithm,
|
27 |
|
|
-- and takes 36 clocks. The operands must be stable during
|
28 |
|
|
-- the calculations. The result is available one clock after
|
29 |
|
|
-- the ready signal is asserted.
|
30 |
|
|
------------------------------------------------------------------------------
|
31 |
|
|
|
32 |
|
|
library ieee;
|
33 |
|
|
use ieee.std_logic_1164.all;
|
34 |
|
|
library grlib;
|
35 |
|
|
use grlib.stdlib.all;
|
36 |
|
|
library gaisler;
|
37 |
|
|
use gaisler.arith.all;
|
38 |
|
|
|
39 |
|
|
entity div32 is
|
40 |
|
|
port (
|
41 |
|
|
rst : in std_ulogic;
|
42 |
|
|
clk : in std_ulogic;
|
43 |
|
|
holdn : in std_ulogic;
|
44 |
|
|
divi : in div32_in_type;
|
45 |
|
|
divo : out div32_out_type
|
46 |
|
|
);
|
47 |
|
|
end;
|
48 |
|
|
|
49 |
|
|
architecture rtl of div32 is
|
50 |
|
|
|
51 |
|
|
type div_regtype is record
|
52 |
|
|
x : std_logic_vector(64 downto 0);
|
53 |
|
|
state : std_logic_vector(2 downto 0);
|
54 |
|
|
zero : std_logic;
|
55 |
|
|
zero2 : std_logic;
|
56 |
|
|
qcorr : std_logic;
|
57 |
|
|
zcorr : std_logic;
|
58 |
|
|
qzero : std_logic;
|
59 |
|
|
qmsb : std_logic;
|
60 |
|
|
ovf : std_logic;
|
61 |
|
|
neg : std_logic;
|
62 |
|
|
cnt : std_logic_vector(4 downto 0);
|
63 |
|
|
end record;
|
64 |
|
|
|
65 |
|
|
signal r, rin : div_regtype;
|
66 |
|
|
signal addin1, addin2, addout: std_logic_vector(32 downto 0);
|
67 |
|
|
signal addsub : std_logic;
|
68 |
|
|
|
69 |
|
|
begin
|
70 |
|
|
|
71 |
|
|
divcomb : process (r, rst, divi, addout)
|
72 |
|
|
variable v : div_regtype;
|
73 |
|
|
variable vready, vnready : std_logic;
|
74 |
|
|
variable vaddin1, vaddin2 : std_logic_vector(32 downto 0);
|
75 |
|
|
variable vaddsub, ymsb : std_logic;
|
76 |
|
|
constant zero33: std_logic_vector(32 downto 0) := "000000000000000000000000000000000";
|
77 |
|
|
begin
|
78 |
|
|
|
79 |
|
|
vready := '0'; vnready := '0'; v := r;
|
80 |
|
|
if addout = zero33 then v.zero := '1'; else v.zero := '0'; end if;
|
81 |
|
|
|
82 |
|
|
vaddin1 := r.x(63 downto 31); vaddin2 := divi.op2;
|
83 |
|
|
vaddsub := not (divi.op2(32) xor r.x(64));
|
84 |
|
|
v.zero2 := r.zero;
|
85 |
|
|
|
86 |
|
|
case r.state is
|
87 |
|
|
when "000" =>
|
88 |
|
|
v.cnt := "00000";
|
89 |
|
|
if (divi.start = '1') then
|
90 |
|
|
v.x(64) := divi.y(32); v.state := "001";
|
91 |
|
|
end if;
|
92 |
|
|
when "001" =>
|
93 |
|
|
v.x := divi.y & divi.op1(31 downto 0);
|
94 |
|
|
v.neg := divi.op2(32) xor divi.y(32);
|
95 |
|
|
if divi.signed = '1' then
|
96 |
|
|
vaddin1 := divi.y(31 downto 0) & divi.op1(31);
|
97 |
|
|
v.ovf := not (addout(32) xor divi.y(32));
|
98 |
|
|
else
|
99 |
|
|
vaddin1 := divi.y; vaddsub := '1';
|
100 |
|
|
v.ovf := not addout(32);
|
101 |
|
|
end if;
|
102 |
|
|
v.state := "010";
|
103 |
|
|
when "010" =>
|
104 |
|
|
if ((divi.signed and r.neg and r.zero) = '1') and (divi.op1 = zero33) then v.ovf := '0'; end if;
|
105 |
|
|
v.qmsb := vaddsub; v.qzero := '1';
|
106 |
|
|
v.x(64 downto 32) := addout;
|
107 |
|
|
v.x(31 downto 0) := r.x(30 downto 0) & vaddsub;
|
108 |
|
|
v.state := "011"; v.zcorr := v.zero;
|
109 |
|
|
v.cnt := r.cnt + 1;
|
110 |
|
|
when "011" =>
|
111 |
|
|
v.qzero := r.qzero and (vaddsub xor r.qmsb);
|
112 |
|
|
v.zcorr := r.zcorr or v.zero;
|
113 |
|
|
v.x(64 downto 32) := addout;
|
114 |
|
|
v.x(31 downto 0) := r.x(30 downto 0) & vaddsub;
|
115 |
|
|
if (r.cnt = "11111") then v.state := "100"; vnready := '1';
|
116 |
|
|
else v.cnt := r.cnt + 1; end if;
|
117 |
|
|
v.qcorr := v.x(64) xor divi.y(32);
|
118 |
|
|
when "100" =>
|
119 |
|
|
vaddin1 := r.x(64 downto 32);
|
120 |
|
|
v.state := "101";
|
121 |
|
|
when others =>
|
122 |
|
|
vaddin1 := ((not r.x(31)) & r.x(30 downto 0) & '1');
|
123 |
|
|
vaddin2 := (others => '0'); vaddin2(0) := '1';
|
124 |
|
|
vaddsub := (not r.neg);-- or (r.zcorr and not r.qcorr);
|
125 |
|
|
if ((r.qcorr = '1') or (r.zero = '1')) and (r.zero2 = '0') then
|
126 |
|
|
if (r.zero = '1') and ((r.qcorr = '0') and (r.zcorr = '1')) then
|
127 |
|
|
vaddsub := r.neg; v.qzero := '0';
|
128 |
|
|
end if;
|
129 |
|
|
v.x(64 downto 32) := addout;
|
130 |
|
|
else
|
131 |
|
|
v.x(64 downto 32) := vaddin1; v.qzero := '0';
|
132 |
|
|
end if;
|
133 |
|
|
if (r.ovf = '1') then
|
134 |
|
|
v.qzero := '0';
|
135 |
|
|
v.x(63 downto 32) := (others => '1');
|
136 |
|
|
if divi.signed = '1' then
|
137 |
|
|
if r.neg = '1' then v.x(62 downto 32) := (others => '0');
|
138 |
|
|
else v.x(63) := '0'; end if;
|
139 |
|
|
end if;
|
140 |
|
|
end if;
|
141 |
|
|
vready := '1';
|
142 |
|
|
v.state := "000";
|
143 |
|
|
end case;
|
144 |
|
|
|
145 |
|
|
divo.icc <= r.x(63) & r.qzero & r.ovf & '0';
|
146 |
|
|
if (divi.flush = '1') then v.state := "000"; end if;
|
147 |
|
|
if (rst = '0') then
|
148 |
|
|
v.state := "000"; v.cnt := (others => '0');
|
149 |
|
|
end if;
|
150 |
|
|
rin <= v;
|
151 |
|
|
divo.ready <= vready; divo.nready <= vnready;
|
152 |
|
|
divo.result(31 downto 0) <= r.x(63 downto 32);
|
153 |
|
|
addin1 <= vaddin1; addin2 <= vaddin2; addsub <= vaddsub;
|
154 |
|
|
|
155 |
|
|
end process;
|
156 |
|
|
|
157 |
|
|
divadd : process(addin1, addin2, addsub)
|
158 |
|
|
variable b : std_logic_vector(32 downto 0);
|
159 |
|
|
begin
|
160 |
|
|
if addsub = '1' then b := not addin2; else b := addin2; end if;
|
161 |
|
|
addout <= addin1 + b + addsub;
|
162 |
|
|
end process;
|
163 |
|
|
|
164 |
|
|
reg : process(clk)
|
165 |
|
|
begin
|
166 |
|
|
if rising_edge(clk) then
|
167 |
|
|
if (holdn = '1') then r <= rin; end if;
|
168 |
|
|
if (rst = '0') then r.state <= "000"; r.cnt <= (others => '0'); end if;
|
169 |
|
|
end if;
|
170 |
|
|
end process;
|
171 |
|
|
|
172 |
|
|
end;
|
173 |
|
|
|