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

Subversion Repositories core_arm

[/] [core_arm/] [trunk/] [vhdl/] [libs/] [int.vhd] - Blame information for rev 4

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 tarookumic
library ieee;
2
use ieee.std_logic_1164.all;
3
use IEEE.std_logic_unsigned."+";
4
use IEEE.std_logic_unsigned."-";
5
use IEEE.std_logic_unsigned.conv_integer;
6
use IEEE.std_logic_arith.conv_unsigned;
7
use IEEE.std_logic_arith.all;
8
 
9
-- PREFIX: lin_xxx
10
package int is
11
 
12
constant LIN_ZERO    : std_logic_vector(31 downto 0) := "00000000000000000000000000000000";
13
constant LIN_ONE     : std_logic_vector(31 downto 0) := "00000000000000000000000000000001";
14
constant LIN_TWO     : std_logic_vector(31 downto 0) := "00000000000000000000000000000010";
15
constant LIN_THREE   : std_logic_vector(31 downto 0) := "00000000000000000000000000000011";
16
constant LIN_FOUR    : std_logic_vector(31 downto 0) := "00000000000000000000000000000100";
17
constant LIN_MINFOUR : std_logic_vector(31 downto 0) := "11111111111111111111111111111100";
18
 
19
-- increment/decrement wrapper
20
procedure lin_incdec(
21
  source : in    std_logic_vector;
22
  dest   : inout std_logic_vector;
23
  do     : in    std_logic;
24
  inc    : in    std_logic
25
);
26
 
27
-- convert std_logic_vector to integer
28
function lin_convint(
29
  op   : in std_logic_vector
30
) return integer;
31
 
32
-- set bit integer(v)
33
function lin_decode(
34
  v : std_logic_vector
35
) return std_logic_vector;
36
 
37
-- pos of first '1' from left
38
function lin_countzero(
39
  data : in    std_logic_vector
40
) return std_logic_vector;
41
 
42
-- adder with carryin
43
procedure lin_adder(
44
  op1   : in std_logic_vector(31 downto 0);
45
  op2   : in std_logic_vector(31 downto 0);
46
  carry : in std_logic;
47
  sub   : in std_logic;
48
  sum   : out std_logic_vector(31 downto 0)
49
);
50
 
51
----------------------------------------------------------------------------
52
-- log2 tables
53
----------------------------------------------------------------------------
54
 
55
type lin_log2arr is array(1 to 64) of integer;
56
constant lin_log2  : lin_log2arr := (0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,
57
                                5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,others => 6);
58
constant lin_log2x : lin_log2arr := (1,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,
59
                                5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,others => 6);
60
 
61
end int;
62
 
63
package body int is
64
 
65
procedure lin_incdec(
66
  source : in    std_logic_vector;
67
  dest   : inout std_logic_vector;
68
  do     : in    std_logic;
69
  inc    : in    std_logic
70
) is
71
variable tmp : std_logic_vector(source'range);
72
begin
73
 
74
  tmp := (others => '0');
75
-- pragma translate_off
76
    if not is_x(source) then
77
-- pragma translate_on
78
      if inc = '1' then
79
        tmp := source(source'range) + 1;
80
      else
81
        tmp := source(source'range) - 1;
82
      end if;
83
-- pragma translate_off
84
    else
85
      tmp := (others => 'X');
86
    end if;
87
-- pragma translate_on
88
 
89
    if (do) = '1' then
90
      dest := tmp;
91
    end if;
92
 
93
end;
94
 
95
 
96
function lin_countzero(
97
  data : in    std_logic_vector
98
) return std_logic_vector is
99
variable z02 : std_logic_vector((data'length/2)-1 downto 0);
100
variable z04 : std_logic_vector((data'length/4)-1 downto 0);
101
variable d04 : std_logic_vector(3 downto 0);
102
variable z08 : std_logic_vector((data'length/8)-1 downto 0);
103
variable d08 : std_logic_vector(7 downto 0);
104
variable z16 : std_logic_vector((data'length/16)-1 downto 0);
105
variable d16 : std_logic_vector(15 downto 0);
106
variable z32 : std_logic_vector((data'length/32)-1 downto 0);
107
variable d32 : std_logic_vector(31 downto 0);
108
variable z64 : std_logic_vector((data'length/64)-1 downto 0);
109
variable d64 : std_logic_vector(63 downto 0);
110
variable res : std_logic_vector(lin_log2x(data'length)-1 downto 0);
111
variable length : integer;
112
variable tmp : integer;
113
begin
114
 
115
  res := (others => '0');
116
  z32 := (others => '0');
117
  z02 := (others => '0');
118
  z04 := (others => '0');
119
  length := data'length;
120
 
121
  if (length / 2) >= 1 then
122
    tmp := 0;
123
L02:
124
    for i in (length/2)-1 downto 0 loop
125
      tmp := i;
126
      if    (data((i*2)+0) = '1') then
127
        z02(i) := '1';
128
        exit L02;
129
      elsif (data((i*2)+1) = '1') then
130
        z02(i) := '0';
131
        exit L02;
132
      else
133
        z02(i) := '0';
134
      end if;
135
    end loop;  -- i
136
    if (length = 2) then
137
      res(0) := z02(tmp);
138
    end if;
139
  end if;
140
 
141
  if (length / 4) >= 1 then
142
    tmp := 0;
143
L04:
144
    for i in (length/4)-1 downto 0 loop
145
      tmp := i;
146
      d04 := data((i*4)+3 downto i*4);
147
      z04(i) := '1';
148
      if    not (d04(3 downto 2) = "00") then
149
        z04(i) := '1';
150
        res(0) := z02(i*2);
151
        exit L04;
152
      elsif not (d04(1 downto 0) = "00") then
153
        z04(i) := '0';
154
        res(0) := z02((i*2)+1);
155
        exit L04;
156
      else
157
        z04(i) := '0';
158
      end if;
159
    end loop;  -- i
160
    if (length = 4) then
161
      res(1) := z04(tmp);
162
    end if;
163
  end if;
164
 
165
  if (length / 8) >= 1 then
166
    tmp := 0;
167
L08:
168
    for i in (length/8)-1 downto 0 loop
169
      tmp := i;
170
      d08 := data((i*8)+7 downto i*8);
171
      z08(i) := '1';
172
      if    not (d08(7 downto 4) = "0000") then
173
        z08(i) := '1';
174
        res(1) := z04(i*2);
175
        exit L08;
176
      elsif not (d08(3 downto 0) = "0000") then
177
        z08(i) := '0';
178
        res(1) := z04((i*2)+1);
179
        exit L08;
180
      else
181
        z08(i) := '0';
182
      end if;
183
    end loop;  -- i
184
    if (length = 8) then
185
      res(2) := z08(tmp);
186
    end if;
187
  end if;
188
 
189
  if (length / 16) >= 1 then
190
    tmp := 0;
191
L16:
192
    for i in (length/16)-1 downto 0 loop
193
      tmp := i;
194
      d16 := data((i*16)+15 downto i*16);
195
      z16(i) := '1';
196
      if    not (d16(15 downto 8) = "00000000") then
197
        z16(i) := '1';
198
        res(2) := z08(i*2);
199
        exit L16;
200
      elsif not (d16(7 downto 0) = "00000000") then
201
        z16(i) := '0';
202
        res(2) := z08((i*2)+1);
203
        exit L16;
204
      else
205
        z16(i) := '0';
206
      end if;
207
    end loop;  -- i
208
    if (length = 16) then
209
      res(3) := z16(tmp);
210
    end if;
211
  end if;
212
 
213
  if (length / 32) >= 1 then
214
    tmp := 0;
215
L32:
216
    for i in (length/32)-1 downto 0 loop
217
      tmp := i;
218
      d32 := data((i*32)+31 downto i*32);
219
      z32(i) := '1';
220
      if    not (d32(31 downto 16) = "0000000000000000") then
221
        z32(i) := '1';
222
        res(3) := z16(i*2);
223
        exit L32;
224
      elsif not (d32(15 downto 0) = "0000000000000000") then
225
        z32(i) := '0';
226
        res(3) := z16((i*2)+1);
227
        exit L32;
228
      else
229
        z32(i) := '0';
230
      end if;
231
    end loop;  -- i
232
    if (length = 32) then
233
      res(4) := z32(tmp);
234
    end if;
235
  end if;
236
 
237
  if (length / 64) >= 1 then
238
    tmp := 0;
239
L64:
240
    for i in (length/64)-1 downto 0 loop
241
      tmp := i;
242
      d64 := data((i*64)+63 downto i*64);
243
      z64(i) := '1';
244
      if    not (d64(63 downto 32) = "00000000000000000000000000000000") then
245
        z64(i) := '1';
246
        res(4) := z32(i*2);
247
        exit L64;
248
      elsif not (d64(31 downto 0) = "00000000000000000000000000000000") then
249
        z64(i) := '0';
250
        res(4) := z32((i*2)+1);
251
        exit L64;
252
      else
253
        z64(i) := '0';
254
      end if;
255
    end loop;  -- i
256
    if (length = 64) then
257
      res(5) := z64(tmp);
258
    end if;
259
  end if;
260
 
261
  return res;
262
end;
263
 
264
function lin_convint (
265
  op   : in std_logic_vector
266
) return integer is
267
variable tmp : integer;
268
begin
269
  tmp := 0;
270
-- pragma translate_off
271
    if not (is_x(op)) then
272
-- pragma translate_on
273
      tmp := conv_integer(op);
274
-- pragma translate_off
275
    end if;
276
-- pragma translate_on
277
  return tmp;
278
end;
279
 
280
function lin_decode(
281
  v : std_logic_vector
282
) return std_logic_vector is
283
variable res : std_logic_vector((2**v'length)-1 downto 0); --'
284
variable i : natural;
285
begin
286
  res := (others => '0');
287
-- pragma translate_off
288
  i := 0;
289
  if not is_x(v) then
290
-- pragma translate_on
291
    i := conv_integer(unsigned(v));
292
    res(i) := '1';
293
-- pragma translate_off
294
  else
295
    res := (others => 'X');
296
  end if;
297
-- pragma translate_on
298
  return(res);
299
end;
300
 
301
procedure lin_adder(
302
  op1   : in std_logic_vector(31 downto 0);
303
  op2   : in std_logic_vector(31 downto 0);
304
  carry : in std_logic;
305
  sub   : in std_logic;
306
  sum   : out std_logic_vector(31 downto 0)
307
) is
308
begin
309
-- pragma translate_off
310
    if not (is_x(op1) or is_x(op2) or is_x(carry)) then
311
-- pragma translate_on
312
      if sub = '1' then
313
        sum := op1 - op2 - carry;
314
      else
315
        sum := op1 + op2 + carry;
316
      end if;
317
-- pragma translate_off
318
    else
319
      sum := (others => 'X');
320
    end if;
321
-- pragma translate_on
322
 
323
end;
324
 
325
end int;

powered by: WebSVN 2.1.0

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