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

Subversion Repositories mblite

[/] [mblite/] [trunk/] [hw/] [std/] [std_Pkg.vhd] - Blame information for rev 8

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 takar
----------------------------------------------------------------------------------------------
2
--
3
--      Input file         : std_Pkg.vhd
4
--      Design name        : std_Pkg
5
--      Author             : Tamar Kranenburg
6
--      Company            : Delft University of Technology
7
--                         : Faculty EEMCS, Department ME&CE
8
--                         : Systems and Circuits group
9
--
10
--      Description        : Package with several standard components.
11
--
12
----------------------------------------------------------------------------------------------
13
 
14 8 takar
library ieee;
15
use ieee.std_logic_1164.all;
16
use ieee.numeric_std.all;
17 2 takar
 
18
PACKAGE std_Pkg IS
19
 
20
----------------------------------------------------------------------------------------------
21
-- STANDARD COMPONENTS IN STD_PKG
22
----------------------------------------------------------------------------------------------
23
 
24 8 takar
    component sram generic
25 2 takar
    (
26
        WIDTH : positive;
27
        SIZE  : positive
28
    );
29 8 takar
    port
30 2 takar
    (
31 8 takar
        dat_o : out std_logic_vector(WIDTH - 1 downto 0);
32
        dat_i : in std_logic_vector(WIDTH - 1 downto 0);
33
        adr_i : in std_logic_vector(SIZE - 1 downto 0);
34
        wre_i : in std_logic;
35
        ena_i : in std_logic;
36
        clk_i : in std_logic
37 2 takar
    );
38 8 takar
    end component;
39 2 takar
 
40 8 takar
    component sram_4en generic
41 2 takar
    (
42
        WIDTH : positive;
43
        SIZE  : positive
44
    );
45 8 takar
    port
46 2 takar
    (
47 8 takar
        dat_o : out std_logic_vector(WIDTH - 1 downto 0);
48
        dat_i : in std_logic_vector(WIDTH - 1 downto 0);
49
        adr_i : in std_logic_vector(SIZE - 1 downto 0);
50
        wre_i : in std_logic_vector(3 downto 0);
51
        ena_i : in std_logic;
52
        clk_i : in std_logic
53 2 takar
    );
54 8 takar
    end component;
55 2 takar
 
56 8 takar
    component dsram generic
57 2 takar
    (
58
        WIDTH : positive;
59
        SIZE  : positive
60
    );
61 8 takar
    port
62 2 takar
    (
63 8 takar
        dat_o   : out std_logic_vector(WIDTH - 1 downto 0);
64
        adr_i   : in std_logic_vector(SIZE - 1 downto 0);
65
        ena_i   : in std_logic;
66
        dat_w_i : in std_logic_vector(WIDTH - 1 downto 0);
67
        adr_w_i : in std_logic_vector(SIZE - 1 downto 0);
68
        wre_i   : in std_logic;
69
        clk_i   : in std_logic
70 2 takar
    );
71 8 takar
    end component;
72 2 takar
 
73
----------------------------------------------------------------------------------------------
74
-- FUNCTIONS IN STD_PKG
75
----------------------------------------------------------------------------------------------
76
 
77 8 takar
    function v_or(d : std_logic_vector) return std_logic;
78
    function is_zero(d : std_logic_vector) return std_logic;
79
    function is_not_zero(d : std_logic_vector) return std_logic;
80
    function my_conv_integer(a: std_logic_vector) return integer;
81
    function notx(d : std_logic_vector) return boolean;
82
    function compare(a, b : std_logic_vector) return std_logic;
83
    function multiply(a, b : std_logic_vector) return std_logic_vector;
84
    function sign_extend(value: std_logic_vector; fill: std_logic; size: positive) return std_logic_vector;
85
    function add(a, b : std_logic_vector; ci: std_logic) return std_logic_vector;
86
    function increment(a : std_logic_vector) return std_logic_vector;
87
    function shift(value : std_logic_vector(31 downto 0); shamt: std_logic_vector(4 downto 0); s: std_logic; t: std_logic) return std_logic_vector;
88
    function shift_left(value : std_logic_vector(31 downto 0); shamt : std_logic_vector(4 downto 0)) return std_logic_vector;
89
    function shift_right(value : std_logic_vector(31 downto 0); shamt : std_logic_vector(4 downto 0); padding: std_logic) return std_logic_vector;
90 2 takar
 
91 8 takar
end std_Pkg;
92 2 takar
 
93
PACKAGE BODY std_Pkg IS
94
 
95
-- Unary OR reduction
96 8 takar
    function v_or(d : std_logic_vector) return std_logic is
97
        variable z : std_logic;
98
    begin
99 2 takar
        z := '0';
100 8 takar
        if notx (d) then
101
            for i in d'range loop
102
                z := z or d(i);
103
            end loop;
104
        end if;
105
        return z;
106
    end;
107 2 takar
 
108
-- Check for ones in the vector
109 8 takar
    function is_not_zero(d : std_logic_vector) return std_logic is
110
        variable z : std_logic_vector(d'range);
111
    begin
112
        z := (others => '0');
113
        if notx(d) then
114 2 takar
 
115 8 takar
            if d = z then
116
                return '0';
117
            else
118
                return '1';
119
            end if;
120 2 takar
 
121 8 takar
        else
122
            return '0';
123
        end if;
124
    end;
125 2 takar
 
126
-- Check for ones in the vector
127 8 takar
    function is_zero(d : std_logic_vector) return std_logic is
128
    begin
129
        return not is_not_zero(d);
130
    end;
131 2 takar
 
132
    -- rewrite conv_integer to avoid modelsim warnings
133 8 takar
    function my_conv_integer(a : std_logic_vector) return integer is
134
        variable res : integer range 0 to 2**a'length-1;
135
    begin
136 2 takar
        res := 0;
137 8 takar
        if (notx(a)) then
138 2 takar
            res := to_integer(unsigned(a));
139 8 takar
        end if;
140
        return res;
141
    end;
142 2 takar
 
143 8 takar
    function compare(a, b : std_logic_vector) return std_logic is
144
        variable z : std_logic;
145
    begin
146
        if notx(a & b) and a = b then
147
            return '1';
148
        else
149
            return '0';
150
        end if;
151
    end;
152 2 takar
 
153
-- Unary NOT X test
154 8 takar
    function notx(d : std_logic_vector) return boolean is
155
        variable res : boolean;
156
    begin
157 2 takar
        res := true;
158
-- pragma translate_off
159 8 takar
        res := not is_x(d);
160 2 takar
-- pragma translate_on
161 8 takar
        return (res);
162
    end;
163 2 takar
 
164
-- -- 32 bit shifter
165
-- -- SYNOPSIS:
166
-- --    value: value to be shifted
167
-- --    shamt: shift amount
168
-- --    s 0 / 1: shift right / left
169
-- --    t 0 / 1: shift logical / arithmetic
170
-- -- PSEUDOCODE (from microblaze reference guide)
171
-- --     if S = 1 then
172 8 takar
-- --          (rD) = (rA) << (rB)[27:31]
173 2 takar
-- --     else
174
-- --      if T = 1 then
175 8 takar
-- --         if ((rB)[27:31]) != 0 then
176
-- --              (rD)[0:(rB)[27:31]-1] = (rA)[0]
177
-- --              (rD)[(rB)[27:31]:31] = (rA) >> (rB)[27:31]
178 2 takar
-- --         else
179 8 takar
-- --              (rD) = (rA)
180 2 takar
-- --      else
181 8 takar
-- --         (rD) = (rA) >> (rB)[27:31]
182 2 takar
 
183 8 takar
    function shift(value: std_logic_vector(31 downto 0); shamt: std_logic_vector(4 downto 0); s: std_logic; t: std_logic) return std_logic_vector is
184
    begin
185
        if s = '1' then
186 2 takar
            -- left arithmetic or logical shift
187 8 takar
            return shift_left(value, shamt);
188
        else
189
            if t = '1' then
190 2 takar
                -- right arithmetic shift
191 8 takar
                return shift_right(value, shamt, value(31));
192
            else
193 2 takar
                -- right logical shift
194 8 takar
                return shift_right(value, shamt, '0');
195
            end if;
196
        end if;
197
    end;
198 2 takar
 
199 8 takar
    function shift_left(value: std_logic_vector(31 downto 0); shamt: std_logic_vector(4 downto 0)) return std_logic_vector is
200
        variable result: std_logic_vector(31 downto 0);
201
        variable paddings: std_logic_vector(15 downto 0);
202
    begin
203
        paddings := (others => '0');
204 2 takar
        result := value;
205 8 takar
        if (shamt(4) = '1') then result := result(15 downto 0) & paddings(15 downto 0); end if;
206
        if (shamt(3) = '1') then result := result(23 downto 0) & paddings( 7 downto 0); end if;
207
        if (shamt(2) = '1') then result := result(27 downto 0) & paddings( 3 downto 0); end if;
208
        if (shamt(1) = '1') then result := result(29 downto 0) & paddings( 1 downto 0); end if;
209
        if (shamt(0) = '1') then result := result(30 downto 0) & paddings( 0 );         end if;
210
        return result;
211
    end;
212 2 takar
 
213 8 takar
    function shift_right(value: std_logic_vector(31 downto 0); shamt: std_logic_vector(4 downto 0); padding: std_logic) return std_logic_vector is
214
        variable result: std_logic_vector(31 downto 0);
215
        variable paddings: std_logic_vector(15 downto 0);
216
    begin
217
        paddings := (others => padding);
218 2 takar
        result := value;
219 8 takar
        if (shamt(4) = '1') then result := paddings(15 downto 0) & result(31 downto 16); end if;
220
        if (shamt(3) = '1') then result := paddings( 7 downto 0) & result(31 downto  8); end if;
221
        if (shamt(2) = '1') then result := paddings( 3 downto 0) & result(31 downto  4); end if;
222
        if (shamt(1) = '1') then result := paddings( 1 downto 0) & result(31 downto  2); end if;
223
        if (shamt(0) = '1') then result := paddings( 0 )         & result(31 downto  1); end if;
224
        return result;
225
    end;
226 2 takar
 
227 8 takar
    function multiply(a, b: std_logic_vector) return std_logic_vector is
228
        variable x: std_logic_vector (a'length + b'length - 1 downto 0);
229
    begin
230 6 takar
        x := std_logic_vector(signed(a) * signed(b));
231 8 takar
        return x(31 downto 0);
232
    end;
233 2 takar
 
234 8 takar
    function sign_extend(value: std_logic_vector; fill: std_logic; size: positive) return std_logic_vector is
235
        variable a: std_logic_vector (size - 1 downto 0);
236
    begin
237
        a(size - 1 downto value'length) := (others => fill);
238
        a(value'length - 1 downto 0) := value;
239 2 takar
        return a;
240 8 takar
    end;
241 2 takar
 
242 8 takar
    function add(a, b : std_logic_vector; ci: std_logic) return std_logic_vector is
243
        variable x : std_logic_vector(a'length + 1 downto 0);
244
    begin
245
        x := (others => '0');
246
        if notx (a & b & ci) then
247 6 takar
            x := std_logic_vector(signed('0' & a & '1') + signed('0' & b & ci));
248 8 takar
        end if;
249
        return x(a'length + 1 downto 1);
250
    end;
251 2 takar
 
252 8 takar
    function increment(a : std_logic_vector) return std_logic_vector is
253
        variable x : std_logic_vector(a'length-1 downto 0);
254
    begin
255
        x := (others => '0');
256
        if notx (a) then
257 6 takar
            x := std_logic_vector(signed(a) + 1);
258 8 takar
        end if;
259
        return x;
260
    end;
261 2 takar
 
262 8 takar
end std_Pkg;

powered by: WebSVN 2.1.0

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