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

Subversion Repositories hdlc

[/] [hdlc/] [trunk/] [CODE/] [tools_pkg.vhd] - Blame information for rev 17

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 9 khatib
-------------------------------------------------------------------------------
2
-- Title      :  Tools Package
3
-- Project    :  Utility library
4
-------------------------------------------------------------------------------
5
-- File        : tools.vhd
6
-- Author      : Jamil Khatib  (khatib@ieee.org)
7
-- Organization: OpenIPCore Project
8
-- Created     : 2000/11/02
9
-- Last update : 2000/11/02
10
-- Platform    : 
11
-- Simulators  : Modelsim 5.3XE/Windows98
12
-- Synthesizers: 
13
-- Target      : 
14
-- Dependency  : ieee.std_logic_1164
15
--               ieee.std_logic_arith
16
--               ieee.std_logic_unsigned
17
--
18
-------------------------------------------------------------------------------
19
-- Description:  This package contains set of usefull functions and procedures
20
-------------------------------------------------------------------------------
21
-- Copyright (c) 2000 Jamil Khatib
22
-- 
23
-- This VHDL design file is an open design; you can redistribute it and/or
24
-- modify it and/or implement it after contacting the author
25
-- You can check the draft license at
26
-- http://www.opencores.org/OIPC/license.shtml
27
 
28
-------------------------------------------------------------------------------
29
-- Revisions  :
30
-- Revision Number :   1
31
-- Version         :   0.1
32
-- Date            :   2nd Nov 2000
33
-- Modifier        :   Jamil Khatib (khatib@ieee.org)
34
-- Desccription    :   Created
35
--
36
---------- Revisions  :
37
-- Revision Number :   2
38
-- Version         :   0.2
39
-- Date            :   14 Nov 2000
40
-- Modifier        :   Jamil Khatib (khatib@ieee.org)
41
-- Desccription    :   Shift functions and int_2_slv are added
42
-------------------------------------------------------------------------------
43
 
44
library ieee;
45
use ieee.std_logic_1164.all;
46
use ieee.std_logic_arith.all;
47
use ieee.std_logic_unsigned.all;
48
 
49
 
50
package tools_pkg is
51
-------------------------------------------------------------------------------
52
-- Types
53
 
54
-- Memory arraye type of std_logic_vector
55
--  type std_memory_array_typ is array (integer range <>) of std_logic_vector(5 downto 0);  --integer range <>);
56
 
57
-- Memory arraye type of std_ulogic_vector
58
--  type stdu_memory_array_typ is array (integer range <>) of std_ulogic_vector(integer range <>);
59
 
60
-- Sign magnitude numbers based on std_logic_vector (The msb represents the sign)
61
  type SIGN_MAG_typ is array (natural range <>) of std_logic;
62
 
63
 
64
-----------------------------------------------------------------------------  
65
-- Functions
66
 
67
 
68
  function Log2( input : integer ) return integer;  -- log2 functions
69
 
70
  function slv_2_int ( SLV : std_logic_vector) return integer;  --
71
                                                                --std_logic_vector
72
                                                                --to integer
73
 
74
  function "+"(A, B : SIGN_MAG_typ) return SIGN_MAG_typ;  -- sign_magnitude addition
75
 
76
  function "-"(A, B : SIGN_MAG_typ) return SIGN_MAG_typ;  -- sign_magnitude
77
                                                          -- subtraction (
78
                                                          -- based on
79
                                                          -- complement operations)
80
  function LeftShift (
81
    InReg           : std_logic_vector;                   -- Input Register
82
    ShSize          : std_logic_vector)                   -- Shift Size
83
    return std_logic_vector;
84
 
85
 
86
  function RightShift (
87
    InReg  : std_logic_vector;          -- Input register
88
    ShSize : std_logic_vector)          -- Shift Size  
89
    return std_logic_vector;
90
 
91
  function int_2_slv (val, SIZE : integer) return std_logic_vector;
92
 
93
-----------------------------------------------------------------------------  
94
end tools_pkg;
95
 
96
-------------------------------------------------------------------------------
97
-------------------------------------------------------------------------------
98
-------------------------------------------------------------------------------
99
-------------------------------------------------------------------------------
100
package body tools_pkg is
101
 
102
-----------------------------------------------------------------------------
103
  function Log2(
104
    input              : integer )      -- input number 
105
    return integer is
106
    variable temp, log : integer;
107
  begin
108
 
109
    assert input /= 0
110
      report "Error : function missuse : log2(zero)"
111
      severity failure;
112
    temp   := input;
113
    log    := 0;
114
    while (temp /= 0) loop
115
      temp := temp/2;
116
      log  := log+1;
117
    end loop;
118
    return log;
119
  end log2;
120
-------------------------------------------------------------------------------
121
 
122
-- function LOG2(COUNT:INTEGER) return INTEGER is  -- COUNT should be >0 variable TEMP:INTEGER;
123
-- variable TEMP : integer;
124
-- variable cnt : integer;
125
--  begin
126
-- cnt := COUNT;
127
-- 
128
--    TEMP:=0;
129
--    while COUNT>1 loop
130
--      TEMP:=TEMP+1;
131
--      cnt:=cnt/2;
132
--    end loop;
133
--    return TEMP;
134
--  end log2;
135
-------------------------------------------------------------------------------
136
  function slv_2_int (
137
    SLV : std_logic_vector)             -- std_logic_vector to convert
138
    return integer is
139
 
140
    variable Result : integer := 0;     -- conversion result
141
 
142
  begin
143
    for i in SLV'range loop
144
      Result                     := Result * 2;  -- shift the variable to left
145
      case SLV(i) is
146
        when '1' | 'H' => Result := Result + 1;
147
        when '0' | 'L' => Result := Result + 0;
148
        when others    => null;
149
      end case;
150
    end loop;
151
 
152
    return Result;
153
  end;
154
-------------------------------------------------------------------------------
155
  function "+"(A, B     : SIGN_MAG_typ) return SIGN_MAG_typ is
156
    variable VA, VB, VR : unsigned(A'length - 1 downto 0);
157
-- include the overflow bit
158
 
159
    variable SA, SB, SR : std_logic;
160
    variable TMP, RES   : SIGN_MAG_typ(A'length - 1 downto 0);
161
 
162
    variable casevar : std_logic_vector(1 downto 0);
163
    variable std_tmp : std_logic_vector(A'length - 1 downto 0) := (others => '0');
164
 
165
  begin
166
 
167
    assert A'length = B'length
168
      report "Error : length mismatch"
169
      severity failure;
170
 
171
 
172
    TMP := A;
173
    SA  := TMP(A'length - 1);
174
    VA  := '0' & unsigned(TMP(A'length - 2 downto 0));
175
    TMP := B;
176
    SB  := TMP(B'length - 1);
177
    VB  := '0' & unsigned(TMP(B'length - 2 downto 0));
178
 
179
    casevar := SA & SB;
180
    case casevar is
181
      when "00" |"11" =>
182
 
183
        VR := VA + VB;
184
        SR := SA;
185
 
186
      when "01" =>
187
 
188
        VR := VA - VB;
189
        SR := VR(VR'length - 1);
190
 
191
        if SR = '1' then
192
          std_tmp(VR'length -2 downto 0) := std_logic_vector(VR(VR'length -2 downto 0));
193
          std_tmp                        := not std_tmp;
194
 
195
          VR(VR'length -2 downto 0) := unsigned(std_tmp(VR'length -2 downto 0));
196
 
197
          VR(VR'length -2 downto 0) := VR(VR'length -2 downto 0) +1;
198
 
199
        end if;
200
 
201
 
202
      when "10" =>
203
        VR := VB - VA;
204
        SR := VR(VR'length - 1);
205
 
206
        if SR = '1' then
207
          std_tmp(VR'length -2 downto 0) := std_logic_vector(VR(VR'length -2 downto 0));
208
          std_tmp                        := not std_tmp;
209
 
210
          VR(VR'length -2 downto 0) := unsigned(std_tmp(VR'length -2 downto 0));
211
 
212
          VR(VR'length -2 downto 0) := VR(VR'length -2 downto 0) +1;
213
 
214
        end if;
215
 
216
      when others => null;
217
    end case;
218
 
219
 
220
    RES := SIGN_MAG_typ(SR & VR(VR'length -2 downto 0));
221
 
222
    return RES;
223
  end "+";
224
 
225
-------------------------------------------------------------------------------
226
--  function "+"(A, B: SIGN_MAG) return SIGN_MAG is
227
--  variable VA, VB, VR: UNSIGNED(A'length - 2 downto 0);
228
--  variable SA, SB, SR: STD_LOGIC;
229
--  variable TMP, RES: SIGN_MAG(A'length - 1 downto 0);
230
--begin
231
--  assert A'length = B'length
232
--    report "Error"
233
--    severity FAILURE;
234
--  TMP := A;
235
--  SA := TMP(A'length - 1);
236
--  VA := UNSIGNED(TMP(A'length - 2 downto 0));
237
--  TMP := B;
238
--  SB := TMP(B'length - 1);
239
--  VB := UNSIGNED(TMP(B'length - 2 downto 0));
240
--  if (SA = SB) then
241
--    VR := VA + VB;
242
--    SR := SA;
243
--  elsif (VA >= VB) then
244
--    VR := VA - VB;
245
--    SR := SA;
246
--  else
247
--    VR := VB - VA;
248
--    SR := SB;
249
--  end if;
250
--  RES := SIGN_MAG(SR & VR);
251
--  return RES;
252
--end "+";
253
-------------------------------------------------------------------------------
254
  function "-"(A, B : SIGN_MAG_typ) return SIGN_MAG_typ is
255
    variable TMP    : SIGN_MAG_typ(A'length - 1 downto 0);
256
  begin
257
    assert A'length = B'length
258
      report "Error : length mismach"
259
      severity failure;
260
    TMP               := B;
261
    TMP(B'length - 1) := not TMP(B'length - 1);
262
    return A + TMP;
263
  end "-";
264
 
265
-------------------------------------------------------------------------------
266
  -- purpose: combinational left shift register
267
  function LeftShift (
268
    InReg  : std_logic_vector;          -- Input Register
269
    ShSize : std_logic_vector)          -- Shift Size
270
    return std_logic_vector is
271
 
272
    constant REGSIZE   : integer := InReg'length;        -- Register Size
273
    variable VarReg    : std_logic_vector(InReg'length -1 downto 0);
274
                                                         -- Local storage for shifter
275
    constant SHIFTSIZE : integer := log2(InReg'length);  -- Shift size
276
  begin
277
 
278
    VarReg := inReg;
279
 
280
    for i in 0 to SHIFTSIZE -2 loop
281
 
282
 
283
      if ShSize(i) = '1' then
284
 
285
        VarReg(REGSIZE -1 downto 0) := VarReg( (REGSIZE-(2**i)-1) downto 0) & ((2**i)-1 downto 0 => '0');
286
 
287
      end if;
288
 
289
    end loop;  -- i
290
 
291
    if ShSize(SHIFTSIZE-1) = '1' then
292
      VarReg := (others => '0');
293
    end if;
294
 
295
    return VarReg;
296
 
297
  end LeftShift;
298
 
299
-------------------------------------------------------------------------------
300
-- purpose: combinational Right shift register
301
  function RightShift (
302
    InReg  : std_logic_vector;          -- Input register
303
    ShSize : std_logic_vector)          -- Shift Size  
304
    return std_logic_vector is
305
 
306
    constant REGSIZE   : integer := InReg'length;        -- Register Size
307
    variable VarReg    : std_logic_vector(InReg'length -1 downto 0);
308
                                                         -- Local storage for shifter
309
    constant SHIFTSIZE : integer := log2(InReg'length);  -- Shift size
310
 
311
  begin  -- RightShift
312
 
313
 
314
 
315
 
316
    VarReg := inReg;
317
 
318
    for i in 0 to SHIFTSIZE -2 loop
319
 
320
 
321
      if ShSize(i) = '1' then
322
 
323
        VarReg(REGSIZE -1 downto 0) := (REGSIZE-1 downto REGSIZE-(2**i) => '0') & VarReg(REGSIZE -1 downto (2**i));
324
 
325
      end if;
326
 
327
    end loop;  -- i
328
 
329
    if ShSize(SHIFTSIZE-1) = '1' then
330
      VarReg := (others => '0');
331
    end if;
332
 
333
    return VarReg;
334
 
335
 
336
  end RightShift;
337
-------------------------------------------------------------------------------
338
-- purpose: Integer to Std_logic_vector conversion
339
  function int_2_slv (val, SIZE : integer) return std_logic_vector is
340
    variable result             : std_logic_vector(SIZE-1 downto 0);
341
    variable l_val              : integer := val;
342
  begin
343
 
344
    assert SIZE > 1
345
      report "Error : function missuse : in_2_slv(val, negative size)"
346
      severity failure;
347
 
348
    for i in 0 to result'length-1 loop
349
 
350
      if (l_val mod 2) = 0 then
351
 
352
        result(i) := '0';
353
 
354
      else
355
        result(i) := '1';
356
 
357
      end if;
358
 
359
      l_val := l_val/2;
360
 
361
    end loop;
362
 
363
    return result;
364
 
365
  end int_2_slv;
366
-------------------------------------------------------------------------------
367
end tools_pkg;

powered by: WebSVN 2.1.0

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