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

Subversion Repositories ion

[/] [ion/] [trunk/] [vhdl/] [mips_pkg.vhdl] - Blame information for rev 251

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 64 ja_rd
--------------------------------------------------------------------------------
2
-- mips_pkg.vhdl -- Configuration constants & utility types and functions
3
--------------------------------------------------------------------------------
4
-- IMPORTANT:
5
-- Here's where you define the memory map of the system, in the implementation 
6
-- of function decode_addr. 
7
-- You need to change that function to change the memory map, independent of any
8
-- additional address decoding you may do out of the FPGA (e.g. if you have more
9
-- than one chip on any data bus) or out of the MCU module (e.g. when you add
10
-- new IO registers).
11
-- Please see the module c2sb_demo and mips_mcu for examples of memory decoding.
12
--------------------------------------------------------------------------------
13 162 ja_rd
-- Copyright (C) 2011 Jose A. Ruiz
14 161 ja_rd
--                                                              
15
-- This source file may be used and distributed without         
16
-- restriction provided that this copyright statement is not    
17
-- removed from the file and that any derivative work contains  
18
-- the original copyright notice and the associated disclaimer. 
19
--                                                              
20
-- This source file is free software; you can redistribute it   
21
-- and/or modify it under the terms of the GNU Lesser General   
22
-- Public License as published by the Free Software Foundation; 
23
-- either version 2.1 of the License, or (at your option) any   
24
-- later version.                                               
25
--                                                              
26
-- This source is distributed in the hope that it will be       
27
-- useful, but WITHOUT ANY WARRANTY; without even the implied   
28
-- warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      
29
-- PURPOSE.  See the GNU Lesser General Public License for more 
30
-- details.                                                     
31
--                                                              
32
-- You should have received a copy of the GNU Lesser General    
33
-- Public License along with this source; if not, download it   
34
-- from http://www.opencores.org/lgpl.shtml
35
--------------------------------------------------------------------------------
36 64 ja_rd
 
37 2 ja_rd
library ieee;
38
use ieee.std_logic_1164.all;
39
use ieee.std_logic_arith.all;
40
use ieee.std_logic_unsigned.all;
41
 
42
package mips_pkg is
43
 
44 64 ja_rd
---- Basic types ---------------------------------------------------------------
45
 
46
subtype t_word is std_logic_vector(31 downto 0);
47 225 ja_rd
subtype t_halfword is std_logic_vector(15 downto 0);
48
subtype t_byte is std_logic_vector(7 downto 0);
49 251 ja_rd
subtype t_pc is std_logic_vector(31 downto 2);
50 64 ja_rd
 
51 251 ja_rd
subtype t_regindex is std_logic_vector(4 downto 0);
52
 
53
---- Interface types -----------------------------------------------------------
54
 
55
type t_cop0_mosi is record
56
    index :             t_regindex;
57
    we :                std_logic;
58
    data :              t_word;
59
    pc_restart :        t_pc;
60
    in_delay_slot :     std_logic;
61
    pipeline_stalled :  std_logic;
62
    exception :         std_logic;
63
    rfe :               std_logic;
64
    unknown_opcode :    std_logic;
65
    missing_cop :       std_logic;
66
    syscall :           std_logic;
67
    stall :             std_logic;
68
end record t_cop0_mosi;
69
 
70
type t_cop0_miso is record
71
    data :              t_word;
72
    kernel :            std_logic;
73
    idcache_enable :    std_logic;
74
    icache_invalidate : std_logic;
75
end record t_cop0_miso;
76
 
77
 
78 64 ja_rd
---- System configuration constants --------------------------------------------
79
 
80
-- True to use standard-ish MIPS-1 memory map, false to use Plasma's
81
-- (see implementation of function decode_addr below).
82
constant USE_MIPS1_ADDR_MAP : boolean := true;
83
 
84
-- Reset vector address minus 4 (0xfffffffc for Plasma, 0xbfbffffc for mips1)
85
constant RESET_VECTOR_M4 : t_word   := X"bfbffffc";
86
 
87
-- Trap vector address (0x0000003c for Plasma, 0xbfc00180 for mips1)
88
constant TRAP_VECTOR : t_word       := X"bfc00180";
89
 
90 225 ja_rd
-- Object code in bytes, i.e. as read from a binary or HEX file.
91
-- This type is used to define BRAM init constants from external scripts.
92
type t_obj_code is array(integer range <>) of std_logic_vector(7 downto 0);
93 64 ja_rd
 
94 225 ja_rd
-- Types used to define memories for synthesis or simulation.
95
type t_word_table is array(integer range <>) of t_word;
96
type t_hword_table is array(integer range <>) of t_halfword;
97
type t_byte_table is array(integer range <>) of t_byte;
98
 
99
---- Object code management -- initialization helper functions -----------------
100
 
101
-- Dummy t_obj_code constant, to be used essentially as a syntactic placeholder.
102
constant default_object_code : t_obj_code(0 to 3) := (
103
    X"00", X"00", X"00", X"00"
104
    );
105
 
106
-- Builds BRAM initialization constant from a constant CONSTRAINED byte array
107
-- containing the application object code.
108
-- The constant is a 32-bit, big endian word table.
109
-- The object code is placed at the beginning of the BRAM and the rest is
110
-- filled with zeros.
111
-- The object code is truncated if it doesn't fit the given table size.
112
-- CAN BE USED IN SYNTHESIZABLE CODE to compute a BRAM initialization constant 
113
-- from a constant argument.
114
function objcode_to_wtable(oC : t_obj_code; size : integer) return t_word_table;
115
 
116
-- Builds BRAM initialization constant from a constant CONSTRAINED byte array
117
-- containing the application object code.
118
-- The constant is a 32-bit, big endian word table.
119
-- The object code is placed at the beginning of the BRAM and the rest is
120
-- filled with zeros.
121
-- The object code is truncated if it doesn't fit the given table size.
122
-- CAN BE USED IN SYNTHESIZABLE CODE to compute a BRAM initialization constant 
123
-- from a constant argument.
124
function objcode_to_htable(oC : t_obj_code; size : integer) return t_hword_table;
125
 
126
-- Builds BRAM initialization constant from a constant CONSTRAINED byte array
127
-- containing the application object code.
128
-- The constant is an 8-bit byte table.
129
-- The object code is placed at the beginning of the BRAM and the rest is
130
-- filled with zeros.
131
-- The object code is truncated if it doesn't fit the given table size.
132
-- CAN BE USED IN SYNTHESIZABLE CODE to compute a BRAM initialization constant 
133
-- from a constant argument.
134
function objcode_to_btable(oC : t_obj_code; size : integer) return t_byte_table;
135
 
136
 
137
 
138 64 ja_rd
---- Address decoding ----------------------------------------------------------
139
 
140
-- Note: it is the cache module that does all internal address decoding --------
141
 
142
-- This is the slice of the address that will be used to decode memory areas
143 48 ja_rd
subtype t_addr_decode is std_logic_vector(31 downto 24);
144 37 ja_rd
 
145 64 ja_rd
-- Part of the memory area attribute: the type of memory determines how the
146
-- cache module handles each block
147 72 ja_rd
subtype t_memory_type is std_logic_vector(7 downto 5);
148 64 ja_rd
-- These are all the types the cache knows about
149
constant MT_BRAM : t_memory_type            := "000";
150
constant MT_IO_SYNC : t_memory_type         := "001";
151
constant MT_SRAM_16B : t_memory_type        := "010";
152 75 ja_rd
constant MT_SRAM_8B : t_memory_type         := "011";
153 64 ja_rd
constant MT_DDR_16B : t_memory_type         := "100";
154
constant MT_UNMAPPED : t_memory_type        := "111";
155 37 ja_rd
 
156 72 ja_rd
-- Wait state counter -- we're supporting static memory from 10 to >100 ns
157
subtype t_wait_state_count is std_logic_vector(2 downto 0);
158 64 ja_rd
 
159 72 ja_rd
-- 'Attributes' of some memory block -- used when decoding memory addresses
160
type t_range_attr is record
161
    mem_type :          t_memory_type;
162
    writeable :         std_logic;
163
    cacheable :         std_logic;
164
    wait_states :       t_wait_state_count;
165
end record t_range_attr;
166
 
167
 
168
 
169 64 ja_rd
---- More basic types and constants --------------------------------------------
170
 
171 2 ja_rd
subtype t_addr is std_logic_vector(31 downto 0);
172
subtype t_dword is std_logic_vector(63 downto 0);
173
subtype t_regnum is std_logic_vector(4 downto 0);
174
type t_rbank is array(0 to 31) of t_word;
175 64 ja_rd
-- This is used as a textual shortcut only
176 2 ja_rd
constant ZERO : t_word := (others => '0');
177 64 ja_rd
-- control word for ALU
178 2 ja_rd
type t_alu_control is record
179
    logic_sel :         std_logic_vector(1 downto 0);
180
    shift_sel :         std_logic_vector(1 downto 0);
181
    shift_amount :      std_logic_vector(4 downto 0);
182
    neg_sel :           std_logic_vector(1 downto 0);
183
    use_arith :         std_logic;
184
    use_logic :         std_logic_vector(1 downto 0);
185
    cy_in :             std_logic;
186
    use_slt :           std_logic;
187
    arith_unsigned :    std_logic;
188
end record t_alu_control;
189 64 ja_rd
-- Flags coming from the ALU
190 2 ja_rd
type t_alu_flags is record
191
    inp1_lt_zero :      std_logic;
192
    inp1_eq_zero :      std_logic;
193
    inp1_lt_inp2 :      std_logic;
194
    inp1_eq_inp2 :      std_logic;
195
end record t_alu_flags;
196
 
197 134 ja_rd
-- Debug info output by sinthesizable MPU core; meant to debug the core itself, 
198
-- not to debug software!
199
type t_debug_info is record
200
    cache_enabled :     std_logic;
201
    unmapped_access :   std_logic;
202
end record t_debug_info;
203
 
204
 
205 12 ja_rd
-- 32-cycle mul/div module control. Bits 4-3 & 1-0 of IR.
206
subtype t_mult_function is std_logic_vector(3 downto 0);
207
constant MULT_NOTHING       : t_mult_function := "0000";
208
constant MULT_READ_LO       : t_mult_function := "1010"; -- 18
209
constant MULT_READ_HI       : t_mult_function := "1000"; -- 16
210
constant MULT_WRITE_LO      : t_mult_function := "1011"; -- 19
211
constant MULT_WRITE_HI      : t_mult_function := "1001"; -- 17
212
constant MULT_MULT          : t_mult_function := "1101"; -- 25
213
constant MULT_SIGNED_MULT   : t_mult_function := "1100"; -- 24
214
constant MULT_DIVIDE        : t_mult_function := "1111"; -- 26
215
constant MULT_SIGNED_DIVIDE : t_mult_function := "1110"; -- 27
216
 
217 37 ja_rd
-- Computes ceil(log2(A)), e.g. address width of memory block
218
-- CAN BE USED IN SYNTHESIZABLE CODE as long as called with constant arguments
219
function log2(A : natural) return natural;
220 12 ja_rd
 
221 64 ja_rd
-- Decodes a memory address, gives the type of memory
222
-- CAN BE USED IN SYNTHESIZABLE CODE, argument does not need to be constant
223
function decode_addr(addr : t_addr_decode) return t_range_attr;
224 37 ja_rd
 
225 64 ja_rd
 
226 2 ja_rd
end package;
227 37 ja_rd
 
228
package body mips_pkg is
229
 
230
function log2(A : natural) return natural is
231
begin
232
    for I in 1 to 30 loop -- Works for up to 32 bit integers
233 85 ja_rd
        if(2**I >= A) then
234
            return(I);
235 37 ja_rd
        end if;
236
    end loop;
237
    return(30);
238
end function log2;
239
 
240 64 ja_rd
-- Address decoding for Plasma-like system
241
function decode_addr_plasma(addr : t_addr_decode) return t_range_attr is
242
begin
243
 
244
    case addr(31 downto 27) is
245 72 ja_rd
    when "00000"    => return (MT_BRAM     ,'0','0',"000"); -- useg
246
    when "10000"    => return (MT_SRAM_16B ,'1','1',"000"); -- kseg0
247
    when "00100"    => return (MT_IO_SYNC  ,'1','0',"000"); -- kseg1 i/o
248
    when others     => return (MT_UNMAPPED ,'0','0',"000"); -- stray
249 64 ja_rd
    end case;
250
 
251
end function decode_addr_plasma;
252
 
253 75 ja_rd
-- Address decoding for MIPS-I-like system as implemented in target hardware
254 64 ja_rd
function decode_addr_mips1(addr : t_addr_decode) return t_range_attr is
255
begin
256
 
257
    case addr(31 downto 27) is
258 120 ja_rd
    when "00000"    => return (MT_SRAM_16B ,'1','1',"010"); -- useg
259
    when "10000"    => return (MT_SRAM_16B ,'1','1',"010"); -- kseg0
260 72 ja_rd
    --when "10100"    => return (MT_IO_SYNC  ,'1','0',"000"); -- kseg1 i/o
261
    when "00100"    => return (MT_IO_SYNC  ,'1','0',"000"); -- kseg1 i/o
262 120 ja_rd
    when "10110"    => return (MT_SRAM_8B  ,'0','0',"111"); -- kseg1 flash
263 72 ja_rd
    when "10111"    => return (MT_BRAM     ,'0','0',"000"); -- kseg1 boot rom
264
    when others     => return (MT_UNMAPPED ,'0','0',"000"); -- stray
265 64 ja_rd
    end case;
266
 
267
end function decode_addr_mips1;
268
 
269
 
270
function decode_addr(addr : t_addr_decode) return t_range_attr is
271
begin
272
    if USE_MIPS1_ADDR_MAP then
273
        return decode_addr_mips1(addr);
274
    else
275
        return decode_addr_plasma(addr);
276
    end if;
277
 
278
end function decode_addr;
279
 
280 225 ja_rd
function objcode_to_wtable(oC : t_obj_code;
281
                           size : integer)
282
                           return t_word_table is
283
variable br : t_word_table(integer range 0 to size-1):=(others => X"00000000");
284
variable i, address, index : integer;
285
begin
286
 
287
    -- Copy object code to start of BRAM...
288
    i := 0;
289
    for i in 0 to oC'length-1 loop
290
        case i mod 4 is
291
        when 0 =>       index := 24;
292
        when 1 =>       index := 16;
293
        when 2 =>       index := 8;
294
        when others =>  index := 0;
295
        end case;
296
 
297
        address := i / 4;
298
        if address >= size then
299
            exit;
300
        end if;
301
        br(address)(index+7 downto index) := oC(i);
302
    end loop;
303
 
304
    return br;
305
end function objcode_to_wtable;
306
 
307
 
308
function objcode_to_htable(oC : t_obj_code;
309
                           size : integer)
310
                           return t_hword_table is
311
variable br : t_hword_table(integer range 0 to size-1):=(others => X"0000");
312
variable i, address, index : integer;
313
begin
314
 
315
    -- Copy object code to start of BRAM...
316
    i := 0;
317
    for i in 0 to oC'length-1 loop
318
        case i mod 2 is
319
        when 1 =>       index := 8;
320
        when others =>  index := 0;
321
        end case;
322
 
323
        address := i / 2;
324
        if address >= size then
325
            exit;
326
        end if;
327
        br(address)(index+7 downto index) := oC(i);
328
    end loop;
329
 
330
 
331
    return br;
332
end function objcode_to_htable;
333
 
334
function objcode_to_btable(oC : t_obj_code;
335
                           size : integer)
336
                           return t_byte_table is
337
variable br : t_byte_table(integer range 0 to size-1):=(others => X"00");
338
variable i, address, index : integer;
339
begin
340
 
341
    -- Copy object code to start of table, leave the rest of the table
342
    for i in 0 to oC'length-1 loop
343
        if i >= size then
344
            exit;
345
        end if;
346
        br(i) := oC(i);
347
    end loop;
348
 
349
    return br;
350
end function objcode_to_btable;
351
 
352 37 ja_rd
end package body;

powered by: WebSVN 2.1.0

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