OpenCores
URL https://opencores.org/ocsvn/qfp32/qfp32/trunk

Subversion Repositories qfp32

[/] [qfp32/] [trunk/] [Units/] [add.vhd] - Blame information for rev 2

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 mgraep
library IEEE;
2
use IEEE.std_logic_1164.all;
3
 
4
library work;
5
use work.qfp_p.all;
6
 
7
package qfp32_add_p is
8
 
9
  type qfp32_SCMD_ADD_t is (QFP_ADD,QFP_SUB);
10
 
11
  constant QFP_SCMD_ADD : qfp_scmd_t := "00";
12
  constant QFP_SCMD_SUB : qfp_scmd_t := "01";
13
 
14
end package qfp32_add_p;
15
 
16
library IEEE;
17
use IEEE.std_logic_1164.all;
18
use IEEE.numeric_std.all;
19
 
20
library work;
21
use work.qfp_p.all;
22
use work.cla_p.all;
23
use work.qfp32_add_p.all;
24
 
25
entity qfp32_add is
26
 
27
  port (
28
    clk_i     : in  std_ulogic;
29
    reset_n_i : in  std_ulogic;
30
 
31
    cmd_i : in qfp_scmd_t;
32
 
33
    start_i : in std_ulogic;
34
    ready_o : out std_ulogic;
35
 
36
    regA_i : in  qfp32_t;
37
    regB_i : in  qfp32_t;
38
 
39
    complete_o : out std_ulogic;
40
    result_o   : out qfp32_raw_t;
41
 
42
    cmp_le_o  : out std_ulogic);-- regA <= regB
43
 
44
end qfp32_add;
45
 
46
architecture Rtl of qfp32_add is
47
 
48
  signal p1_gt : std_ulogic;
49
  signal p1_fmt : qfp_fmt_t;
50
  signal p1_mant_a : unsigned(29 downto 0);
51
  signal p1_mant_b : unsigned(29 downto 0);
52
  signal p1_cy : std_ulogic;
53
  signal p1_op_a : unsigned(29 downto 0);
54
  signal p1_op_b : unsigned(29 downto 0);
55
  signal p1_clag1 : cla_level_t(7 downto 0);
56
  signal p1_clag2 : cla_level_t(1 downto 0);
57
  signal p1_cmp_gt : std_ulogic;
58
  signal p1_is_add : std_ulogic;
59
  signal p1_exp1 : std_ulogic_vector(2 downto 0);
60
 
61
  signal p2_complete : std_ulogic;
62
  signal p2_gt : std_ulogic;
63
  signal p2_fmt : qfp_fmt_t;
64
  signal p2_cy : std_ulogic;
65
  signal p2_op_a : unsigned(29 downto 0);
66
  signal p2_op_b : unsigned(29 downto 0);
67
  signal p2_clag1 : cla_level_t(7 downto 0);
68
  signal p2_clag2 : cla_level_t(1 downto 0);
69
  signal p2_is_add : std_ulogic;
70
 
71
  signal p2_exp1 : std_ulogic_vector(2 downto 0);
72
  signal p2_exp2 : std_ulogic_vector(8 downto 0);
73
 
74
  signal p2_result : unsigned(29 downto 0);
75
  signal p2_sign : std_ulogic;
76
  signal p2_cmp_gt : std_ulogic;
77
 
78
begin  -- Rtl
79
 
80
  process (clk_i, reset_n_i)
81
  begin  -- process
82
    if reset_n_i = '0' then             -- asynchronous reset (active low)
83
      p2_gt <= '0';
84
      p2_fmt <= (to_unsigned(0,2),'0');
85
      p2_is_add <= '0';
86
      p2_cy <= '0';
87
      p2_op_a <= to_unsigned(0,30);
88
      p2_op_b <= to_unsigned(0,30);
89
      p2_clag1 <= (others => ('0','0'));
90
      p2_clag2 <= (others => ('0','0'));
91
      p2_complete <= '0';
92
      p2_cmp_gt <= '0';
93
      --p2_exp1 <= (others => '0');-- 6Mhz faster design without this reset value
94
    elsif clk_i'event and clk_i = '1' then  -- rising clock edge
95
      p2_complete <= start_i;
96
 
97
      if start_i = '1' then
98
        p2_gt <= p1_gt;
99
        p2_fmt <= p1_fmt;
100
        p2_is_add <= p1_is_add;
101
        p2_cy <= p1_cy;
102
        p2_op_a <= p1_op_a;
103
        p2_op_b <= p1_op_b;
104
        p2_clag1 <= p1_clag1;
105
        p2_clag2 <= p1_clag2;
106
        p2_cmp_gt <= p1_cmp_gt;
107
        p2_exp1 <= p1_exp1;
108
      end if;
109
 
110
    end if;
111
  end process;
112
 
113
  pp: process (cmd_i, p1_clag1, p1_clag2, p1_cy, p1_fmt.exp, p1_gt,
114
               p1_mant_a(0), p1_mant_a(29 downto 1), p1_mant_b(0),
115
               p1_mant_b(29 downto 1), p1_op_a, p1_op_b, p2_clag1, p2_exp1,
116
               p2_exp2, p2_fmt.exp, p2_fmt.sign, p2_is_add, p2_op_a, p2_op_b,
117
               p2_result(28 downto 0), p2_result(29), p2_sign, regA_i.fmt.exp,
118
               regA_i.fmt.sign, regA_i.mant, regB_i.fmt.exp, regB_i.fmt.sign,
119
               regB_i.mant)
120
 
121
  begin  -- process
122
 
123
    --------------------------------------------------------------------------------------------------------------------
124
    -- stage 1
125
    --------------------------------------------------------------------------------------------------------------------
126
 
127
    -- check if mantissa of regb > rega
128
    p1_gt <= '0';
129
    if regb_i.fmt.exp > rega_i.fmt.exp or (regb_i.fmt.exp = rega_i.fmt.exp and fast_gt(regb_i.mant,rega_i.mant)) then
130
      p1_gt <= '1';
131
    end if;
132
 
133
    -- adjust mantissa to be aligned
134
    if regA_i.fmt.exp < regB_i.fmt.exp then
135
      p1_fmt.exp <= regB_i.fmt.exp;
136
    else
137
      p1_fmt.exp <= regA_i.fmt.exp;
138
    end if;
139
 
140
    -- determine sign
141
    if p1_gt = '1' then -- b > a
142
      -- greater value determines sign; invert sign if b > a and will be substracted
143
      if cmd_i = QFP_SCMD_SUB then
144
        p1_fmt.sign <= not regB_i.fmt.sign;
145
      else
146
        p1_fmt.sign <= regB_i.fmt.sign;
147
      end if;
148
    else
149
      p1_fmt.sign <= regA_i.fmt.sign;
150
    end if;
151
 
152
    -- extend for rounding
153
    p1_mant_a <= fast_shift(regA_i.mant & '0',to_integer(p1_fmt.exp-regA_i.fmt.exp)*8,'1');
154
    p1_mant_b <= fast_shift(regB_i.mant & '0',to_integer(p1_fmt.exp-regB_i.fmt.exp)*8,'1');
155
 
156
    -- negate operands for subtraction
157
    p1_cy <= '0';
158
    p1_is_add <= '0';
159
    p1_op_a <= '0' & p1_mant_a(29 downto 1);
160
    p1_op_b <= '0' & p1_mant_b(29 downto 1);
161
    if (cmd_i = QFP_SCMD_ADD and (regA_i.fmt.sign xor regB_i.fmt.sign) = '1') or
162
       (cmd_i = QFP_SCMD_SUB and (regA_i.fmt.sign xor regB_i.fmt.sign) = '0') then
163
      -- substraction
164
      if p1_gt = '1' then
165
        p1_op_a <= not ('0' & p1_mant_a(29 downto 1));
166
        p1_cy <= not p1_mant_a(0);-- use rounding
167
      else
168
        p1_op_b <= not ('0' & p1_mant_b(29 downto 1));
169
        p1_cy <= not p1_mant_b(0);-- use rounding
170
      end if;
171
    else -- addition
172
      p1_cy <= p1_mant_a(0) or p1_mant_b(0);-- rounding up
173
      p1_is_add <= '1';
174
    end if;
175
 
176
    -- calc greater than b > a
177
    p1_cmp_gt <= p1_gt;
178
    if regA_i.fmt.sign /= regB_i.fmt.sign then
179
      p1_cmp_gt <= regA_i.fmt.sign;
180
    elsif regA_i.fmt.sign = '1' then
181
      p1_cmp_gt <= not p1_gt;
182
    end if;
183
 
184
    -- calculate pre carry (first level of carray lookahead adder)
185
    p1_clag1 <= CLALevelMk(p1_op_a,p1_op_b,4);
186
 
187
    -- second level
188
    p1_clag2 <= CLALevelMk(p1_clag1,4);
189
 
190
    -- expand first level carry
191
    p1_exp1 <= CLAExpandCy(p1_clag2,p1_cy);
192
 
193
    --------------------------------------------------------------------------------------------------------------------
194
    -- stage 2
195
    --------------------------------------------------------------------------------------------------------------------
196
 
197
    -- expand second level carry
198
    p2_exp2 <= CLAExpandCy(p2_clag1,p2_exp1);
199
 
200
    -- propagate carry (level 2)
201
    p2_result <= CLAParallelAdd(p2_op_a,p2_op_b,p2_exp2);
202
 
203
    p2_sign <= p2_fmt.sign;
204
 
205
    result_o.extMant <= p2_result(28 downto 0) & (23 downto 0 => '0');
206
    result_o.ov <= "0000" & (p2_result(29) and p2_is_add); -- allow overflow only for real additions
207
    result_o.exp <= '0' & p2_fmt.exp;
208
    result_o.sign <= p2_sign;-- and not p2_cmp_eq;
209
 
210
  end process pp;
211
 
212
  cmp_le_o <= not p2_cmp_gt;-- less equal than
213
 
214
  ready_o <= '1';
215
  complete_o <= p2_complete;
216
 
217
end Rtl;

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.