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

Subversion Repositories qfp32

[/] [qfp32/] [trunk/] [qfp_p.vhd] - Blame information for rev 3

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 3 mgraep
-- Copyright (c) 2013 Malte Graeper (mgraep@t-online.de) All rights reserved.
2
 
3 2 mgraep
library IEEE;
4
use IEEE.std_logic_1164.all;
5
use IEEE.numeric_std.all;
6
 
7
use IEEE.std_logic_1164.all;            -- basic logic types
8
use STD.textio.all;                     -- basic I/O
9
use IEEE.std_logic_textio.all;          -- I/O for logic types
10
 
11
library work;
12
use work.cla_p.all;
13
 
14
package qfp_p is
15
 
16
  -- support 8 different formates
17
  type qfp_fmt_t is record
18
    exp  : unsigned(1 downto 0);
19
    sign : std_ulogic;
20
  end record;
21
 
22
  type qfp32_t is record
23
    mant : unsigned(28 downto 0);
24
    fmt  : qfp_fmt_t;
25
  end record;
26
 
27
  -- intermediate format (unnormalized)
28
  type qfp32_raw_t is record
29
    extMant : unsigned(52 downto 0);-- mant+x
30
    ov   : unsigned(4 downto 0);
31
    exp  : unsigned(2 downto 0);
32
    sign : std_ulogic;
33
  end record;
34
 
35
  subtype qfp_scmd_t is std_ulogic_vector(1 downto 0);-- sub command
36
 
37
  constant qfp_x0 : unsigned(1 downto 0) := to_unsigned(0,2);
38
  constant qfp_x8 : unsigned(1 downto 0) := to_unsigned(1,2);
39
  constant qfp_x16 : unsigned(1 downto 0) := to_unsigned(2,2);
40
  constant qfp_x24 : unsigned(1 downto 0) := to_unsigned(3,2);
41
 
42
  -- greater than
43
  function fast_gt (
44
    a : unsigned;
45
    b : unsigned)
46
    return boolean;
47
 
48
  -- add
49
  function fast_add (
50
    a : unsigned;
51
    b : unsigned;
52
    cy_in : std_ulogic)
53
    return unsigned;
54
 
55
  constant fast_shift_left : std_ulogic := '0';
56
  constant fast_shift_rigtht : std_ulogic := '1';
57
 
58
  -- implements a barrel shifter
59
  function fast_shift (
60
    data_in : unsigned;
61
    shft : natural;
62
    mode  : std_ulogic; -- left=0
63
    extend : std_ulogic := '0')
64
    return unsigned;
65
 
66
  function to_ulogic (
67
    cond : boolean)
68
    return std_ulogic;
69
 
70
  function min (
71
    a : unsigned;
72
    b : unsigned)
73
    return unsigned;
74
 
75
  function max (
76
    a : unsigned;
77
    b : unsigned)
78
    return unsigned;
79
 
80
  function log2 (
81
    x : integer)
82
    return integer;
83
 
84
end package qfp_p;
85
 
86
package body qfp_p is
87
 
88
   -- greater than a > b SURE
89
  function fast_gt (
90
    a : unsigned;
91
    b : unsigned)
92
    return boolean is
93
 
94
    variable l1 : cla_level_t((a'length-1)/4 downto 0);
95
    variable l2 : cla_level_t((a'length-1)/16 downto 0);
96
    variable l3 : cla_level_t((a'length-1)/32 downto 0);
97
    variable cy : std_ulogic_vector(l3'length downto 0);-- groupsize+1
98
 
99
    variable a_inv : unsigned(a'length-1 downto 0);
100
 
101
  begin  -- function fast_gt
102
 
103
    a_inv := not a;
104
    l1 := CLALevelMk(a_inv,b,4);
105
    l2 := CLALevelMk(l1,4);
106
    l3 := CLALevelMk(l2,2);
107
 
108
    cy := CLAExpandCy(l3,'1');
109
 
110
    return cy(cy'length-1) = '0';
111
 
112
  end function fast_gt;
113
 
114
  function fast_add (
115
    a : unsigned;
116
    b : unsigned;
117
    cy_in : std_ulogic)
118
    return unsigned is
119
 
120
    variable l1 : cla_level_t((a'length-1)/4 downto 0);
121
    variable l2 : cla_level_t((a'length-1)/16 downto 0);
122
    variable l3 : cla_level_t((a'length-1)/32 downto 0);
123
    variable cy : std_ulogic_vector(l3'length downto 0);-- groupsize+1
124
    variable result : unsigned(a'length downto 0);
125
 
126
  begin  -- function fast_add
127
 
128
    l1 := CLALevelMk(a,b,4);
129
    l2 := CLALevelMk(l1,4);
130
    l3 := CLALevelMk(l2,2);
131
 
132
    cy := CLAExpandCy(l3,cy_in);
133
 
134
    return cy(cy'length-1) & CLAParallelAdd(a,b,CLAExpandCy(l1,CLAExpandCy(l2,cy)));
135
 
136
  end function fast_add;
137
 
138
  -- implements a barrel shifter
139
  function fast_shift (
140
    data_in : unsigned;
141
    shft : natural;
142
    mode  : std_ulogic; -- left=0
143
    extend : std_ulogic := '0')
144
    return unsigned is
145
 
146
    constant size_lg2 : integer := log2(data_in'length);
147
 
148
    variable shft_rem : natural;
149
    variable data : unsigned(data_in'length-1 downto 0);
150
  begin  -- fast_shift
151
 
152
    shft_rem := shft;
153
 
154
    data := data_in;
155
 
156
    for i in size_lg2-1 downto 0 loop
157
      if shft_rem >= 2**i then
158
        case mode is
159
          when '0' => -- shift left
160
            data := shift_left(data,2**i);
161
          when '1' => -- shift right 
162
            data := shift_right(data,2**i);
163
            -- overwrite top bits with extend
164
            data(data'length-1 downto (data'length-2**i)) := (others => extend);
165
          when others => null;
166
        end case;
167
        shft_rem := shft_rem-2**i;
168
      end if;
169
    end loop;  -- i
170
 
171
    return data;
172
 
173
  end fast_shift;
174
 
175
  function to_ulogic (
176
    cond : boolean)
177
    return std_ulogic is
178
  begin
179
    if cond then
180
      return '1';
181
    end if;
182
 
183
    return '0';
184
  end to_ulogic;
185
 
186
  function min (
187
    a : unsigned;
188
    b : unsigned)
189
    return unsigned is
190
  begin  -- function min
191
    if a < b then
192
      return a;
193
    end if;
194
    return b;
195
  end function min;
196
 
197
  function max (
198
    a : unsigned;
199
    b : unsigned)
200
    return unsigned is
201
  begin  -- function max
202
    if a > b then
203
      return a;
204
    end if;
205
    return b;
206
  end function max;
207
 
208
  function log2 (
209
    x : integer)
210
    return integer is
211
 
212
    variable i : integer;
213
    variable result : integer;
214
 
215
  begin  -- log2
216
    i := 1;
217
    result := x-1;
218
    while result >= 2 loop
219
      i := i+1;
220
      result := result/2;
221
    end loop;
222
    return i;
223
  end log2;
224
 
225
end package body qfp_p;

powered by: WebSVN 2.1.0

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