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

Subversion Repositories ion

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

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 ja_rd
--------------------------------------------------------------------------------
2
-- ion_cpu.vhdl -- MIPS-I(tm) compatible CPU core
3
--------------------------------------------------------------------------------
4
-- project:       ION (http://www.opencores.org/project,ion_cpu)
5
-- author:        Jose A. Ruiz (ja_rd@hotmail.com)
6
-- created:       Jan/11/2011
7 200 ja_rd
-- last modified: Jul/31/2011 (ja_rd@hotmail.com)
8 2 ja_rd
--------------------------------------------------------------------------------
9 153 ja_rd
-- Please read file /doc/ion_project.txt for usage instructions.
10
--------------------------------------------------------------------------------
11 2 ja_rd
--### MIPS-I things not implemented
12
--
13 159 ja_rd
-- 1.- Most of the R3000 CP0 registers and of course all of the CP1.
14
-- 2.- External interrupts missing, with CP0.SR IR, NMI and IM7..0 flags.
15 2 ja_rd
--
16
--### Things with provisional implementation
17
-- 
18
-- 1.- Load interlocks: the pipeline is stalled for every load instruction, even
19
--     if the target register is not used in the following instruction. So that
20
--     every load takes two cycles.
21 121 ja_rd
--     The interlock logic should check register indices (@note2)
22 200 ja_rd
-- 2.- Invalid instruction side effects:
23 153 ja_rd
--     Invalid opcodes do trap but the logic that prevents bad opcodes from
24
--     having side affects has not been tested yet.
25 200 ja_rd
-- 3.- Kernel/user status.
26 153 ja_rd
--     When in user mode, COP* instructions will trigger a 'CpU' exception.
27
--     BUT there's no address checking and user code can still access kernel 
28
--     space in this version.
29 2 ja_rd
--
30
--------------------------------------------------------------------------------
31 159 ja_rd
-- KNOWN BUGS:
32
--
33 161 ja_rd
-- 1.- The instruction executed right after entering user mode (i.e. the 
34
--     instruction after the MTC0 or RFE that clears the KU flag) is executed 
35 200 ja_rd
--     in kernel mode (instead of user mode). This is a gaping security hole,
36 161 ja_rd
--     in case it makes any sense to speak of security in this project at this
37
--     stage. 
38 159 ja_rd
--     This can be easily fixed but is not very urgent.
39
--------------------------------------------------------------------------------
40 162 ja_rd
-- Copyright (C) 2011 Jose A. Ruiz
41 161 ja_rd
--                                                              
42
-- This source file may be used and distributed without         
43
-- restriction provided that this copyright statement is not    
44
-- removed from the file and that any derivative work contains  
45
-- the original copyright notice and the associated disclaimer. 
46
--                                                              
47
-- This source file is free software; you can redistribute it   
48
-- and/or modify it under the terms of the GNU Lesser General   
49
-- Public License as published by the Free Software Foundation; 
50
-- either version 2.1 of the License, or (at your option) any   
51
-- later version.                                               
52
--                                                              
53
-- This source is distributed in the hope that it will be       
54
-- useful, but WITHOUT ANY WARRANTY; without even the implied   
55
-- warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      
56
-- PURPOSE.  See the GNU Lesser General Public License for more 
57
-- details.                                                     
58
--                                                              
59
-- You should have received a copy of the GNU Lesser General    
60
-- Public License along with this source; if not, download it   
61
-- from http://www.opencores.org/lgpl.shtml
62
--------------------------------------------------------------------------------
63 2 ja_rd
 
64
library ieee;
65
use ieee.std_logic_1164.all;
66
use ieee.std_logic_arith.all;
67
use ieee.std_logic_unsigned.all;
68
use work.mips_pkg.all;
69
 
70
entity mips_cpu is
71
    generic(
72 62 ja_rd
        -- Reset vector address minus 4
73
        RESET_VECTOR_M4 : t_word    := RESET_VECTOR_M4;
74
        -- Trap vector address
75
        TRAP_VECTOR : t_word        := TRAP_VECTOR;
76
        -- Type of memory to be used for register bank in xilinx HW
77
        XILINX_REGBANK  : string    := "distributed" -- {distributed|block}
78 2 ja_rd
    );
79
    port(
80
        clk             : in std_logic;
81
        reset           : in std_logic;
82 200 ja_rd
        interrupt       : in std_logic_vector(7 downto 0);
83 2 ja_rd
 
84 96 ja_rd
        data_addr       : out std_logic_vector(31 downto 0);
85
 
86 2 ja_rd
        data_rd         : in std_logic_vector(31 downto 0);
87
        data_rd_vma     : out std_logic;
88 96 ja_rd
 
89
        byte_we         : out std_logic_vector(3 downto 0);
90
        data_wr         : out std_logic_vector(31 downto 0);
91 2 ja_rd
 
92
        code_rd_addr    : out std_logic_vector(31 downto 2);
93
        code_rd         : in std_logic_vector(31 downto 0);
94
        code_rd_vma     : out std_logic;
95 101 ja_rd
 
96
        cache_enable    : out std_logic;
97
        ic_invalidate   : out std_logic;
98 96 ja_rd
 
99 242 ja_rd
        mem_wait        : in std_logic;
100
        cache_ready     : in std_logic
101 2 ja_rd
    );
102
end; --entity mips_cpu
103
 
104
architecture rtl of mips_cpu is
105
 
106
--------------------------------------------------------------------------------
107
-- Pipeline stage 0
108
 
109
signal p0_pc_reg :          t_pc;
110 8 ja_rd
signal p0_pc_restart :      t_pc;
111 2 ja_rd
signal p0_pc_incremented :  t_pc;
112
signal p0_pc_jump :         t_pc;
113
signal p0_pc_branch :       t_pc;
114
signal p0_pc_target :       t_pc;
115
signal p0_pc_next :         t_pc;
116
signal p0_rs_num :          t_regnum;
117
signal p0_rt_num :          t_regnum;
118
signal p0_jump_cond_value : std_logic;
119
signal p0_rbank_rs_hazard : std_logic;
120
signal p0_rbank_rt_hazard : std_logic;
121 28 ja_rd
signal p0_uses_rs1 :        std_logic;
122
signal p0_uses_rs2 :        std_logic;
123 2 ja_rd
 
124 28 ja_rd
signal p1_rs1_hazard :      std_logic;
125
signal p1_rs2_hazard :      std_logic;
126
 
127 2 ja_rd
--------------------------------------------------------------------------------
128
-- Pipeline stage 1
129
 
130
 
131
signal p1_rbank :           t_rbank := (others => X"00000000");
132
 
133
-- IMPORTANT: This attribute is used by Xilinx tools to select how to implement
134
-- the register bank. If we don't use it, by default XST would infer 2 BRAMs for
135
-- the 1024-bit 3-port reg bank, which you probably don't want.
136
-- This can take the values {distributed|block}.
137
attribute ram_style :       string;
138 30 ja_rd
attribute ram_style of p1_rbank : signal is XILINX_REGBANK;
139 2 ja_rd
 
140
signal p1_rs, p1_rt :       t_word;
141
signal p1_rs_rbank :        t_word;
142
signal p1_rt_rbank :        t_word;
143
signal p1_rbank_forward :   t_word;
144
signal p1_rd_num :          t_regnum;
145 28 ja_rd
signal p1_c0_rs_num :       t_regnum;
146 2 ja_rd
signal p1_rbank_wr_addr :   t_regnum;
147
signal p1_rbank_we :        std_logic;
148
signal p1_rbank_wr_data :   t_word;
149
signal p1_alu_inp1 :        t_word;
150
signal p1_alu_inp2 :        t_word;
151
signal p1_alu_outp :        t_word;
152
-- ALU control inputs (shortened name for brevity in expressions)
153
signal p1_ac :              t_alu_control;
154
-- ALU flag outputs (comparison results)
155
signal p1_alu_flags :       t_alu_flags;
156
-- immediate data, sign- or zero-extended as required by IR
157
signal p1_data_imm :        t_word;
158
signal p1_branch_offset :   t_pc;
159
signal p1_branch_offset_sex:std_logic_vector(31 downto 18);
160
signal p1_rbank_rs_hazard : std_logic;
161
signal p1_rbank_rt_hazard : std_logic;
162
signal p1_jump_type_set0 :  std_logic_vector(1 downto 0);
163
signal p1_jump_type_set1 :  std_logic_vector(1 downto 0);
164
signal p1_ir_reg :          std_logic_vector(31 downto 0);
165
signal p1_ir_op :           std_logic_vector(31 downto 26);
166
signal p1_ir_fn :           std_logic_vector(5 downto 0);
167
signal p1_op_special :      std_logic;
168
signal p1_exception :       std_logic;
169
signal p1_do_reg_jump :     std_logic;
170
signal p1_do_zero_ext_imm : std_logic;
171 153 ja_rd
signal p1_set_cp :          std_logic;
172
signal p1_get_cp :          std_logic;
173 2 ja_rd
signal p1_set_cp0 :         std_logic;
174
signal p1_get_cp0 :         std_logic;
175 153 ja_rd
signal p1_rfe :             std_logic;
176 2 ja_rd
signal p1_alu_op2_sel :     std_logic_vector(1 downto 0);
177
signal p1_alu_op2_sel_set0: std_logic_vector(1 downto 0);
178
signal p1_alu_op2_sel_set1: std_logic_vector(1 downto 0);
179
signal p1_do_load :         std_logic;
180
signal p1_do_store :        std_logic;
181
signal p1_store_size :      std_logic_vector(1 downto 0);
182
signal p1_we_control :      std_logic_vector(5 downto 0);
183
signal p1_load_alu :        std_logic;
184
signal p1_load_alu_set0 :   std_logic;
185
signal p1_load_alu_set1 :   std_logic;
186
signal p1_ld_upper_hword :  std_logic;
187
signal p1_ld_upper_byte :   std_logic;
188
signal p1_ld_unsigned :     std_logic;
189
signal p1_jump_type :       std_logic_vector(1 downto 0);
190
signal p1_link :            std_logic;
191
signal p1_jump_cond_sel :   std_logic_vector(2 downto 0);
192
signal p1_data_addr :       t_addr;
193
signal p1_data_offset :     t_addr;
194
 
195 12 ja_rd
signal p1_muldiv_result :   t_word;
196
signal p1_muldiv_func :     t_mult_function;
197
signal p1_muldiv_running :  std_logic;
198
signal p1_muldiv_started :  std_logic;
199
signal p1_muldiv_stall :    std_logic;
200
 
201 23 ja_rd
signal p1_unknown_opcode :  std_logic;
202 153 ja_rd
signal p1_cp_unavailable :  std_logic;
203 12 ja_rd
 
204 2 ja_rd
--------------------------------------------------------------------------------
205
-- Pipeline stage 2
206
 
207 12 ja_rd
signal p2_muldiv_started :  std_logic;
208 2 ja_rd
signal p2_exception :       std_logic;
209
signal p2_rd_addr :         std_logic_vector(1 downto 0);
210
signal p2_rd_mux_control :  std_logic_vector(3 downto 0);
211
signal p2_load_target :     t_regnum;
212
signal p2_do_load :         std_logic;
213 132 ja_rd
signal p2_do_store :        std_logic;
214 2 ja_rd
signal p2_ld_upper_hword :  std_logic;
215
signal p2_ld_upper_byte :   std_logic;
216
signal p2_ld_unsigned :     std_logic;
217
signal p2_wback_mux_sel :   std_logic_vector(1 downto 0);
218
signal p2_data_word_rd :    t_word;
219
signal p2_data_word_ext :   std_logic;
220
 
221
--------------------------------------------------------------------------------
222
-- Global control signals 
223
 
224
signal load_interlock :     std_logic;
225
signal stall_pipeline :     std_logic;
226
-- pipeline is stalled for any reason
227
signal pipeline_stalled :   std_logic;
228 46 ja_rd
 
229
signal stalled_memwait :    std_logic;
230
signal stalled_muldiv :     std_logic;
231 2 ja_rd
-- pipeline is stalled because of a load instruction interlock
232 46 ja_rd
signal stalled_interlock :  std_logic;
233 2 ja_rd
 
234 101 ja_rd
signal reset_done :         std_logic;
235 46 ja_rd
 
236 2 ja_rd
--------------------------------------------------------------------------------
237 251 ja_rd
-- CP0 interface signals
238 2 ja_rd
 
239 251 ja_rd
signal cp0_mosi :           t_cop0_mosi;
240
signal cp0_miso :           t_cop0_miso;
241 2 ja_rd
 
242
begin
243
 
244
--##############################################################################
245
-- Register bank & datapath
246
 
247
-- Register indices are 'decoded' out of the instruction word BEFORE loading IR
248
p0_rs_num <= std_logic_vector(code_rd(25 downto 21));
249
with p1_ir_reg(31 downto 26) select p1_rd_num <=
250
    p1_ir_reg(15 downto 11)    when "000000",
251
    p1_ir_reg(20 downto 16)    when others;
252
 
253
p0_rt_num <= std_logic_vector(code_rd(20 downto 16)); -- also called rs2 in the docs
254
 
255
--------------------------------------------------------------------------------
256
-- Data input shifter & masker (LB,LBU,LH,LHU,LW)
257
 
258
p2_rd_mux_control <= p2_ld_upper_hword & p2_ld_upper_byte & p2_rd_addr;
259
 
260
-- Extension for unused bits will be zero or the sign (bit 7 or bit 15)
261 35 ja_rd
p2_data_word_ext <= '0'          when p2_ld_unsigned='1' else
262 83 ja_rd
                    -- LH
263
                    data_rd(31)  when p2_ld_upper_byte='1' and p2_rd_addr="00" else
264
                    data_rd(15)  when p2_ld_upper_byte='1' and p2_rd_addr="10" else
265
                    -- LB
266
                    data_rd(7)  when p2_rd_addr="11" else
267 2 ja_rd
                    data_rd(15)  when p2_rd_addr="10" else
268 83 ja_rd
                    data_rd(23)  when p2_rd_addr="01" else
269
                    data_rd(31);
270 2 ja_rd
 
271
-- byte 0 may come from any of the 4 bytes of the input word
272
with p2_rd_mux_control select p2_data_word_rd(7 downto 0) <=
273
    data_rd(31 downto 24)        when "0000",
274
    data_rd(23 downto 16)        when "0001",
275
    data_rd(23 downto 16)        when "0100",
276
    data_rd(15 downto  8)        when "0010",
277
    data_rd( 7 downto  0)        when others;
278
 
279
-- byte 1 may come from input bytes 1 or 3 or may be extended for LB, LBU
280
with p2_rd_mux_control select p2_data_word_rd(15 downto 8) <=
281
    data_rd(31 downto 24)        when "0100",
282
    data_rd(15 downto  8)        when "0110",
283
    data_rd(15 downto  8)        when "1100",
284
    data_rd(15 downto  8)        when "1101",
285
    data_rd(15 downto  8)        when "1110",
286
    data_rd(15 downto  8)        when "1111",
287
    (others => p2_data_word_ext) when others;
288
 
289
-- bytes 2,3 come straight from input or are extended for LH,LHU
290
with p2_ld_upper_hword select p2_data_word_rd(31 downto 16) <=
291 46 ja_rd
    (others => p2_data_word_ext) when '0',
292
    data_rd(31 downto 16)        when others;
293 2 ja_rd
 
294 121 ja_rd
--------------------------------------------------------------------------------
295
-- Reg bank input multiplexor
296
 
297 2 ja_rd
-- Select which data is to be written back to the reg bank and where
298
p1_rbank_wr_addr <= p1_rd_num   when p2_do_load='0' and p1_link='0' else
299
                    "11111"     when p2_do_load='0' and p1_link='1' else
300
                    p2_load_target;
301
 
302
p2_wback_mux_sel <=
303
    "00" when p2_do_load='0' and p1_get_cp0='0' and p1_link='0' else
304
    "01" when p2_do_load='1' and p1_get_cp0='0' and p1_link='0' else
305
    "10" when p2_do_load='0' and p1_get_cp0='1' and p1_link='0' else
306
    "11";
307
 
308
with (p2_wback_mux_sel) select p1_rbank_wr_data <=
309
    p1_alu_outp                when "00",
310
    p2_data_word_rd            when "01",
311
    p0_pc_incremented & "00"   when "11",
312 251 ja_rd
    cp0_miso.data              when others;
313 2 ja_rd
 
314 121 ja_rd
--------------------------------------------------------------------------------
315
-- Register bank RAM & Rbank WE logic
316
 
317 171 ja_rd
p1_rbank_we <= '1' when (p2_do_load='1' or p1_load_alu='1' or p1_link='1' or
318
                        -- if mfc0 triggers privilege trap, don't load reg
319
                        (p1_get_cp0='1' and p1_cp_unavailable='0')) and
320 35 ja_rd
                        -- If target register is $zero, ignore write
321 2 ja_rd
                        p1_rbank_wr_addr/="00000" and
322 35 ja_rd
                        -- if the cache controller keeps the cpu stopped, do
323
                        -- not writeback
324
                        mem_wait='0' and
325 46 ja_rd
                        -- if stalled because of muldiv, block writeback
326
                        stalled_muldiv='0' and --@note1
327 2 ja_rd
                        -- on exception, abort next instruction (by preventing 
328
                        -- regbank writeback).
329
                        p2_exception='0'
330
                else '0';
331
 
332
-- Register bank as triple-port RAM. Should synth to 2 BRAMs unless you use
333
-- synth attributes to prevent it (see 'ram_style' attribute above) or your
334
-- FPGA has 3-port BRAMS, or has none.
335
synchronous_reg_bank:
336
process(clk)
337
begin
338
    if clk'event and clk='1' then
339 46 ja_rd
        if p1_rbank_we='1' then
340 2 ja_rd
            p1_rbank(conv_integer(p1_rbank_wr_addr)) <= p1_rbank_wr_data;
341
        end if;
342 46 ja_rd
        -- the rbank read port loads in the same conditions as the IR: don't
343
        -- update Rs or Rt if the pipeline is frozen
344
        if stall_pipeline='0' then
345
            p1_rt_rbank <= p1_rbank(conv_integer(p0_rt_num));
346
            p1_rs_rbank <= p1_rbank(conv_integer(p0_rs_num));
347
        end if;
348 2 ja_rd
    end if;
349
end process synchronous_reg_bank;
350
 
351 121 ja_rd
--------------------------------------------------------------------------------
352
-- Reg bank 'writeback' data forwarding
353
 
354
-- Register writeback data in a DFF in case it needs to be forwarded.
355 2 ja_rd
data_forward_register:
356
process(clk)
357
begin
358
    if clk'event and clk='1' then
359
        if p1_rbank_we='1' then -- no need to check for stall cycles
360
            p1_rbank_forward <= p1_rbank_wr_data;
361
        end if;
362
    end if;
363
end process data_forward_register;
364
 
365
-- Bypass sync RAM if we're reading and writing to the same address. This saves
366
-- 1 stall cycle and fixes the data hazard.
367
p0_rbank_rs_hazard <= '1' when p1_rbank_wr_addr=p0_rs_num and p1_rbank_we='1'
368
                      else '0';
369
p0_rbank_rt_hazard <= '1' when p1_rbank_wr_addr=p0_rt_num and p1_rbank_we='1'
370
                      else '0';
371
 
372
p1_rs <= p1_rs_rbank when p1_rbank_rs_hazard='0' else p1_rbank_forward;
373
p1_rt <= p1_rt_rbank when p1_rbank_rt_hazard='0' else p1_rbank_forward;
374
 
375
 
376
--------------------------------------------------------------------------------
377
-- ALU & ALU input multiplexors
378
 
379
p1_alu_inp1 <= p1_rs;
380
 
381
with p1_alu_op2_sel select p1_alu_inp2 <=
382 12 ja_rd
    p1_data_imm         when "11",
383 23 ja_rd
    p1_muldiv_result    when "01",
384
    --p1_muldiv_result    when "10", -- FIXME mux input wasted!
385 12 ja_rd
    p1_rt               when others;
386 2 ja_rd
 
387
alu_inst : entity work.mips_alu
388
    port map (
389
        clk             => clk,
390
        reset           => reset,
391
        ac              => p1_ac,
392
        flags           => p1_alu_flags,
393
 
394
        inp1            => p1_alu_inp1,
395
        inp2            => p1_alu_inp2,
396
        outp            => p1_alu_outp
397
    );
398
 
399
 
400
--------------------------------------------------------------------------------
401
-- Mul/Div block interface
402
 
403 12 ja_rd
-- Compute the mdiv block function word. If p1_muldiv_func has any value other
404
-- than MULT_NOTHING a new mdiv operation will start, truncating whatever other
405
-- operation that may have been in course.
406
-- So we encode here the function to be performed and make sure the value stays
407
-- there for only one cycle (the first ALU cycle of the mul/div instruction).
408 2 ja_rd
 
409 12 ja_rd
-- This will be '1' for all mul/div operations other than NOP...
410
p1_muldiv_func(3) <= '1' when p1_op_special='1' and
411
                              p1_ir_fn(5 downto 4)="01" and
412
                              -- ...but only if the mdiv is not already running
413
                              p2_muldiv_started = '0' and
414
                              p1_muldiv_running ='0'
415
                      else '0';
416 2 ja_rd
 
417 12 ja_rd
-- When bit(3) is zero, the rest are zeroed too. Otherwise, they come from IR
418
p1_muldiv_func(2 downto 0) <=
419
    p1_ir_fn(3) & p1_ir_fn(1 downto 0) when p1_muldiv_func(3)='1'
420
    else "000";
421 2 ja_rd
 
422 12 ja_rd
mult_div: entity work.mips_mult
423
    port map (
424
        a           => p1_rs,
425
        b           => p1_rt,
426
        c_mult      => p1_muldiv_result,
427
        pause_out   => p1_muldiv_running,
428
        mult_func   => p1_muldiv_func,
429
        clk         => clk,
430
        reset_in    => reset
431
    );
432
 
433
-- Active only for the 1st ALU cycle of any mul/div instruction
434
p1_muldiv_started <= '1' when p1_op_special='1' and
435
                              p1_ir_fn(5 downto 3)="011" and
436
                              -- 
437
                              p1_muldiv_running='0'
438
                      else '0';
439
 
440
-- Stall the pipeline to enable mdiv operation completion.
441
-- We need p2_muldiv_started to distinguish the cycle before p1_muldiv_running
442
-- is asserted and the cycle after it deasserts.
443
-- Otherwise we would reexecute the same muldiv endlessly instruction after 
444
-- deassertion of p1_muldiv_running, since the IR was stalled and still contains 
445
-- the mul opcode...
446
p1_muldiv_stall <= '1' when
447
        -- Active for the cycle immediately before p1_muldiv_running asserts
448
        -- and NOT for the cycle after it deasserts
449
        (p1_muldiv_started='1' and p2_muldiv_started='0') or
450
        -- Active until operation is complete
451
        p1_muldiv_running = '1'
452
        else '0';
453
 
454
 
455 2 ja_rd
--##############################################################################
456
-- PC register and branch logic
457
 
458
-- p0_pc_reg will not be incremented on stall cycles
459
p0_pc_incremented <= p0_pc_reg + (not stall_pipeline);
460
 
461
-- main pc mux: jump or continue
462
p0_pc_next <=
463
    p0_pc_target when
464
        -- We jump on jump instructions whose condition is met...
465 6 ja_rd
        ((p1_jump_type(1)='1' and p0_jump_cond_value='1' and
466
        -- ...except we abort any jump that follows the victim of an exception
467
          p2_exception='0') or
468
        -- We jump on exceptions too...
469 2 ja_rd
        p1_exception='1')
470 6 ja_rd
        -- ... but we only jump at all if the pipeline is not stalled
471 2 ja_rd
        and stall_pipeline='0'
472
    else p0_pc_incremented;
473
 
474
pc_register:
475
process(clk)
476
begin
477
    if clk'event and clk='1' then
478
        if reset='1' then
479 62 ja_rd
            -- reset to <vector>-4 so that 1st fetch addr is <vector>
480
            p0_pc_reg <= RESET_VECTOR_M4(31 downto 2);
481 2 ja_rd
        else
482 101 ja_rd
            if reset_done='1' then
483 2 ja_rd
            -- p0_pc_reg holds the same value as external sync ram addr register
484
            p0_pc_reg <= p0_pc_next;
485 8 ja_rd
            -- p0_pc_restart = addr saved to EPC on interrupts (@note2)
486 28 ja_rd
            -- It's the addr of the instruction triggering the exception,
487
            -- except when the triggering instruction is in a delay slot. In 
488
            -- that case, this is the previous jump instruction address.
489
            -- I.e. all as per the mips-1 specs.
490 8 ja_rd
            if (p1_jump_type="00" or p0_jump_cond_value='0') then
491
                p0_pc_restart <= p0_pc_reg;
492 28 ja_rd
                -- remember if we are in a delay slot, in case there's a trap
493 251 ja_rd
                cp0_mosi.in_delay_slot <= '0'; -- NOT in a delay slot
494 28 ja_rd
            else
495 251 ja_rd
                cp0_mosi.in_delay_slot <= '1'; -- in a delay slot
496 8 ja_rd
            end if;
497 101 ja_rd
            end if;
498 2 ja_rd
        end if;
499
    end if;
500
end process pc_register;
501
 
502 96 ja_rd
-- Common rd/wr address; lowest 2 bits are output as debugging aid only
503
data_addr <= p1_data_addr(31 downto 0);
504 2 ja_rd
 
505 121 ja_rd
-- 'Memory enable' signals for both memory interfaces
506
data_rd_vma <= p1_do_load and not pipeline_stalled;
507
code_rd_vma <= (not stall_pipeline) and reset_done;
508 2 ja_rd
 
509 101 ja_rd
-- reset_done will be asserted after the reset process is finished, when the
510
-- CPU can start operating normally.
511
-- We only use it to make sure code_rd_vma is not asserted prematurely.
512
wait_for_end_of_reset:
513
process(clk)
514
begin
515
    if clk'event and clk='1' then
516
        if reset='1' then
517
            reset_done <= '0';
518
        else
519
            reset_done <= '1';
520
        end if;
521
    end if;
522
end process wait_for_end_of_reset;
523
 
524 121 ja_rd
-- The final value used to access code memory
525 2 ja_rd
code_rd_addr <= p0_pc_next;
526
 
527
-- compute target of J/JR instructions
528
p0_pc_jump <=   p1_rs(31 downto 2) when p1_do_reg_jump='1' else
529
                p0_pc_reg(31 downto 28) & p1_ir_reg(25 downto 0);
530
 
531
-- compute target of relative branch instructions
532
p1_branch_offset_sex <= (others => p1_ir_reg(15));
533
p1_branch_offset <= p1_branch_offset_sex & p1_ir_reg(15 downto 0);
534
-- p0_pc_reg is the addr of the instruction in delay slot
535
p0_pc_branch <= p0_pc_reg + p1_branch_offset;
536
 
537
-- decide which jump target is to be used
538 62 ja_rd
p0_pc_target <=
539
    TRAP_VECTOR(31 downto 2)    when p1_exception='1' else
540
    p0_pc_jump                  when p1_jump_type(0)='1' else
541
    p0_pc_branch;
542 2 ja_rd
 
543
 
544
--##############################################################################
545
-- Instruction decoding and IR
546
 
547
instruction_register:
548
process(clk)
549
begin
550
    if clk'event and clk='1' then
551
        if reset='1' then
552
            p1_ir_reg <= (others => '0');
553
        elsif stall_pipeline='0' then
554 242 ja_rd
            -- Load the IR with whatever the cache is giving us, UNLESS the
555
            -- cache is not ready (has not yet completed the first code refill
556
            -- after reset), in which case...
557
            if cache_ready='1' then
558
                p1_ir_reg <= code_rd;
559
            else
560
                -- ... load the IR with something innocuous so that the 
561
                -- instruction decoder does not derail.
562
                p1_ir_reg <= (others => '0');
563
            end if;
564 2 ja_rd
        end if;
565
    end if;
566
end process instruction_register;
567
 
568 121 ja_rd
-- Zero extension/Sign extension of instruction immediate data
569
p1_data_imm(15 downto 0)  <= p1_ir_reg(15 downto 0);
570
 
571
with p1_do_zero_ext_imm select p1_data_imm(31 downto 16) <=
572
    (others => '0')             when '1',
573
    (others => p1_ir_reg(15))   when others;
574
 
575
 
576 2 ja_rd
-- 'Extract' main fields from IR, for convenience
577
p1_ir_op <= p1_ir_reg(31 downto 26);
578
p1_ir_fn <= p1_ir_reg(5 downto 0);
579
 
580
-- Decode jump type, if any, for instructions with op/=0
581
with p1_ir_op select p1_jump_type_set0 <=
582 121 ja_rd
    -- FIXME verify that we actually weed out ALL invalid instructions
583 2 ja_rd
    "10" when "000001", -- BLTZ, BGEZ, BLTZAL, BGTZAL
584
    "11" when "000010", -- J
585
    "11" when "000011", -- JAL
586
    "10" when "000100", -- BEQ
587
    "10" when "000101", -- BNE
588
    "10" when "000110", -- BLEZ
589
    "10" when "000111", -- BGTZ
590
    "00" when others;   -- no jump
591
 
592
-- Decode jump type, if any, for instructions with op=0
593
p1_jump_type_set1 <= "11" when p1_op_special='1' and
594
                               p1_ir_reg(5 downto 1)="00100"
595
                     else "00";
596
 
597
-- Decode jump type for the instruction in IR (composite of two formats)
598
p1_jump_type <= p1_jump_type_set0 or p1_jump_type_set1;
599
 
600
p1_link <= '1' when (p1_ir_op="000000" and p1_ir_reg(5 downto 0)="001001") or
601
                    (p1_ir_op="000001" and p1_ir_reg(20)='1') or
602
                    (p1_ir_op="000011")
603
           else '0';
604
 
605
-- Decode jump condition: encode a mux control signal from IR...
606
p1_jump_cond_sel <=
607
    "001" when p1_ir_op="000001" and p1_ir_reg(16)='0' else --   op1 < 0   BLTZ*
608
    "101" when p1_ir_op="000001" and p1_ir_reg(16)='1' else -- !(op1 < 0) BNLTZ*
609
    "010" when p1_ir_op="000100" else                       --   op1 == op2  BEQ
610
    "110" when p1_ir_op="000101" else                       -- !(op1 == op2) BNE
611
    "011" when p1_ir_op="000110" else                       --   op1 <= 0   BLEZ
612
    "111" when p1_ir_op="000111" else                       -- !(op1 <= 0)  BGTZ
613
    "000";                                                  -- always
614
 
615
-- ... and use mux control signal to select the condition value
616
with p1_jump_cond_sel select p0_jump_cond_value <=
617
        p1_alu_flags.inp1_lt_zero       when "001",
618
    not p1_alu_flags.inp1_lt_zero       when "101",
619
        p1_alu_flags.inp1_eq_inp2       when "010",
620
    not p1_alu_flags.inp1_eq_inp2       when "110",
621
        (p1_alu_flags.inp1_lt_inp2 or
622
         p1_alu_flags.inp1_eq_inp2)     when "011",
623
    not (p1_alu_flags.inp1_lt_inp2 or
624
         p1_alu_flags.inp1_eq_inp2)     when "111",
625
    '1'                                 when others;
626
 
627
-- Decode instructions that launch exceptions
628 23 ja_rd
p1_exception <= '1' when
629 153 ja_rd
    (p1_op_special='1' and p1_ir_reg(5 downto 1)="00110") or -- syscall/break
630
    p1_unknown_opcode='1' or
631
    p1_cp_unavailable='1'
632 23 ja_rd
    else '0';
633 2 ja_rd
 
634 153 ja_rd
-- Decode MTC0/MFC0 instructions (see @note3)
635
p1_set_cp  <= '1' when p1_ir_reg(31 downto 28)="0100" and
636
                       p1_ir_reg(25 downto 21)="00100" else '0';
637
p1_get_cp  <= '1' when p1_ir_reg(31 downto 28)="0100" and
638
                       p1_ir_reg(25 downto 21)="00000" else '0';
639 2 ja_rd
 
640 153 ja_rd
p1_set_cp0 <= '1' when p1_ir_reg(27 downto 26)="00" and p1_set_cp='1' else '0';
641
p1_get_cp0 <= '1' when p1_ir_reg(27 downto 26)="00" and p1_get_cp='1' else '0';
642
 
643
-- Decode RFE instruction (see @note3)
644
p1_rfe <= '1' when p1_ir_reg(31 downto 21)="01000010000" and
645
                   p1_ir_reg(5 downto 0)="010000"
646
          else '0';
647
 
648 121 ja_rd
-- Raise some signals for some particular group of opcodes
649
p1_op_special <= '1' when p1_ir_op="000000" else '0'; -- group '0' opcodes
650 2 ja_rd
p1_do_reg_jump <= '1' when p1_op_special='1' and p1_ir_fn(5 downto 1)="00100" else '0';
651
p1_do_zero_ext_imm <= '1' when (p1_ir_op(31 downto 28)="0011") else '0';
652
 
653
-- Decode input data mux control (LW, LH, LB, LBU, LHU) and load enable
654 30 ja_rd
p1_do_load <= '1' when
655
    p1_ir_op(31 downto 29)="100" and
656
    p1_ir_op(28 downto 26)/="010" and -- LWL
657
    p1_ir_op(28 downto 26)/="110" and -- LWR
658
    p1_ir_op(28 downto 26)/="111" and -- LWR
659
    p2_exception='0'  -- abort load if previous instruction triggered trap
660
    else '0';
661 2 ja_rd
 
662
p1_load_alu_set0 <= '1'
663
    when p1_op_special='1' and
664
        ((p1_ir_op(31 downto 29)="000" and p1_ir_op(27 downto 26)="00") or
665
         (p1_ir_op(31 downto 29)="000" and p1_ir_op(27 downto 26)="10") or
666
         (p1_ir_op(31 downto 29)="000" and p1_ir_op(27 downto 26)="11") or
667
         (p1_ir_op(31 downto 29)="000" and p1_ir_op(27 downto 26)="00") or
668
         (p1_ir_op(31 downto 28)="0100" and p1_ir_op(27 downto 26)="00") or
669
         (p1_ir_op(31 downto 28)="0100" and p1_ir_op(27 downto 26)="10") or
670
         (p1_ir_op(31 downto 28)="1000") or
671
         (p1_ir_op(31 downto 28)="1001") or
672
         (p1_ir_op(31 downto 28)="1010" and p1_ir_op(27 downto 26)="10") or
673
         (p1_ir_op(31 downto 28)="1010" and p1_ir_op(27 downto 26)="11") or
674
         (p1_ir_op(31 downto 28)="0010" and p1_ir_op(27 downto 26)="01"))
675
    else '0';
676
 
677
with p1_ir_op select p1_load_alu_set1 <=
678 28 ja_rd
    '1' when "001000",  -- addi
679
    '1' when "001001",  -- addiu
680
    '1' when "001010",  -- slti
681
    '1' when "001011",  -- sltiu
682
    '1' when "001100",  -- andi
683
    '1' when "001101",  -- ori
684
    '1' when "001110",  -- xori
685
    '1' when "001111",  -- lui
686 2 ja_rd
    '0' when others;
687 28 ja_rd
p1_load_alu <= (p1_load_alu_set0 or p1_load_alu_set1) and
688
                not p1_unknown_opcode;
689 2 ja_rd
 
690
p1_ld_upper_hword <= p1_ir_op(27); -- use input upper hword vs. sign extend/zero
691
p1_ld_upper_byte <= p1_ir_op(26);  -- use input upper byte vs. sign extend/zero
692
p1_ld_unsigned <= p1_ir_op(28);    -- sign extend vs. zero extend
693
 
694
-- ALU input-2 selection: use external data for 2x opcodes (loads)
695
p1_alu_op2_sel_set0 <=
696
    "11" when    p1_ir_op(31 downto 30)="10" or p1_ir_op(29)='1' else
697
    "00";
698
 
699
-- ALU input-2 selection: use registers Hi and Lo for MFHI, MFLO
700 22 ja_rd
p1_alu_op2_sel_set1 <=
701
    "01" when p1_op_special='1' and (p1_ir_fn="010000" or p1_ir_fn="010010")
702
    else "00";
703 2 ja_rd
 
704
-- ALU input-2 final selection
705
p1_alu_op2_sel <= p1_alu_op2_sel_set0 or p1_alu_op2_sel_set1;
706
 
707
-- Decode store operations
708 30 ja_rd
p1_do_store <= '1' when
709
    p1_ir_op(31 downto 29)="101" and
710
    (p1_ir_op(28 downto 26)="000" or -- SB
711
     p1_ir_op(28 downto 26)="001" or -- SH
712
     p1_ir_op(28 downto 26)="011") and -- SWH
713 28 ja_rd
    p2_exception='0'    -- abort when previous instruction triggered exception
714
    else '0';
715 2 ja_rd
p1_store_size <= p1_ir_op(27 downto 26);
716
 
717
 
718 28 ja_rd
-- Extract source and destination C0 register indices
719
p1_c0_rs_num <= p1_ir_reg(15 downto 11);
720 2 ja_rd
 
721 121 ja_rd
-- Decode ALU control signals
722 2 ja_rd
 
723
p1_ac.use_slt <= '1' when (p1_ir_op="000001" and p1_ir_reg(20 downto 17)="01000") or
724
                        (p1_ir_op="000000" and p1_ir_reg(5 downto 1)="10101") or
725
                        p1_ir_op="001010" or p1_ir_op="001011"
726
               else '0';
727 83 ja_rd
p1_ac.arith_unsigned <= p1_ac.use_slt and (p1_ir_reg(0) or p1_ir_op(26));
728 2 ja_rd
 
729
p1_ac.use_logic(0) <= '1' when (p1_op_special='1' and p1_ir_fn(5 downto 3)/="000") or
730
                    -- all immediate arith and logic
731
                    p1_ir_op(31 downto 29)="001"
732
                 else '0';
733
p1_ac.use_logic(1) <= '1' when (p1_op_special='1' and p1_ir_fn="100111") else '0';
734
 
735
p1_ac.use_arith <= '1' when p1_ir_op(31 downto 28)="0010" or
736
                            (p1_op_special='1' and
737
                                (p1_ir_fn(5 downto 2)="1000" or
738
                                p1_ir_fn(5 downto 2)="1010"))
739
                 else '0';
740
 
741
-- selection of 2nd internal alu operand: {i2, /i2, i2<<16, 0x0}
742
p1_ac.neg_sel(1)<= '1' when p1_ir_op(29 downto 26) = "1111" else '0';
743
p1_ac.neg_sel(0)<= '1' when   p1_ir_op="001010" or
744
                            p1_ir_op="001011" or
745
                            p1_ir_op(31 downto 28)="0001" or
746
                            (p1_op_special='1' and
747
                                (p1_ir_fn="100010" or
748
                                 p1_ir_fn="100011" or
749
                                 p1_ir_fn(5 downto 2)="1010"))
750
                 else '0';
751
p1_ac.cy_in <= p1_ac.neg_sel(0);
752
 
753
p1_ac.shift_sel <= p1_ir_fn(1 downto 0);
754
 
755
p1_ac.logic_sel <= "00" when (p1_op_special='1' and p1_ir_fn="100100") else
756
                 "01" when (p1_op_special='1' and p1_ir_fn="100101") else
757
                 "10" when (p1_op_special='1' and p1_ir_fn="100110") else
758
                 "01" when (p1_op_special='1' and p1_ir_fn="100111") else
759
                 "00" when (p1_ir_op="001100") else
760
                 "01" when (p1_ir_op="001101") else
761
                 "10" when (p1_ir_op="001110") else
762
                 "11";
763
 
764
p1_ac.shift_amount <= p1_ir_reg(10 downto 6) when p1_ir_fn(2)='0' else p1_rs(4 downto 0);
765
 
766 23 ja_rd
 
767 2 ja_rd
--------------------------------------------------------------------------------
768 23 ja_rd
-- Decoding of unimplemented and privileged instructions
769 2 ja_rd
 
770 23 ja_rd
-- Unimplemented instructions include:
771
--  1.- All instructions above architecture MIPS-I except:
772
--      1.1.- eret
773
--  2.- Unaligned stores and loads (LWL,LWR,SWL,SWR)
774
--  3.- All CP0 instructions other than mfc0 and mtc0
775
--  4.- All CPi instructions
776
--  5.- All cache instructions
777
-- For the time being, we'll decode them all together.
778
 
779
-- FIXME: some of these should trap but others should just NOP (e.g. EHB)
780
 
781
p1_unknown_opcode <= '1' when
782
    -- decode by 'opcode' field
783
    p1_ir_op(31 downto 29)="011" or
784
    p1_ir_op(31 downto 29)="110" or
785
    p1_ir_op(31 downto 29)="111" or
786
    (p1_ir_op(31 downto 29)="010" and p1_ir_op(28 downto 26)/="000") or
787
    p1_ir_op="101111" or    -- CACHE
788
    p1_ir_op="100010" or    -- LWL
789
    p1_ir_op="100110" or    -- LWR
790
    p1_ir_op="101010" or    -- SWL
791
    p1_ir_op="101110" or    -- SWR
792
    p1_ir_op="100111" or
793
    p1_ir_op="101100" or
794
    p1_ir_op="101101" or
795
    -- decode instructions in the 'special' opcode group
796
    (p1_ir_op="000000" and
797
                (p1_ir_fn(5 downto 4)="11" or
798
                 p1_ir_fn="000001" or
799
                 p1_ir_fn="000101" or
800
                 p1_ir_fn="001010" or
801
                 p1_ir_fn="001011" or
802
                 p1_ir_fn="001110" or
803
                 p1_ir_fn(5 downto 2)="0101" or
804
                 p1_ir_fn(5 downto 2)="0111" or
805
                 p1_ir_fn(5 downto 2)="1011")) or
806
    -- decode instructions in the 'regimm' opcode group
807
    (p1_ir_op="000001" and
808
                (p1_ir_reg(20 downto 16)/="00000" and -- BLTZ is valid
809
                 p1_ir_reg(20 downto 16)/="00001" and -- BGEZ is valid
810
                 p1_ir_reg(20 downto 16)/="10000" and -- BLTZAL is valid 
811
                 p1_ir_reg(20 downto 16)/="10001")) -- BGEZAL is valid
812
 
813
    else '0';
814
 
815 153 ja_rd
p1_cp_unavailable <= '1' when
816
    (p1_set_cp='1' and p1_set_cp0='0') or   -- mtc1..3
817
    (p1_get_cp='1' and p1_get_cp0='0') or   -- mfc1..3
818
    ((p1_get_cp0='1' or p1_set_cp0='1' or p1_rfe='1')
819 251 ja_rd
                     and cp0_miso.kernel='0') -- COP0 user mode
820 157 ja_rd
    -- FIXME CP1..3 logic missing
821 153 ja_rd
    else '0';
822
 
823 121 ja_rd
--##############################################################################
824
-- Pipeline registers & pipeline control logic
825 23 ja_rd
 
826 2 ja_rd
-- Stage 1 pipeline register. Involved in ALU control.
827
pipeline_stage1_register:
828
process(clk)
829
begin
830
    if clk'event and clk='1' then
831
        if reset='1' then
832
            p1_rbank_rs_hazard <= '0';
833
            p1_rbank_rt_hazard <= '0';
834
        elsif stall_pipeline='0' then
835
            p1_rbank_rs_hazard <= p0_rbank_rs_hazard;
836
            p1_rbank_rt_hazard <= p0_rbank_rt_hazard;
837
        end if;
838
    end if;
839
end process pipeline_stage1_register;
840
 
841 12 ja_rd
pipeline_stage1_register2:
842
process(clk)
843
begin
844
    if clk'event and clk='1' then
845
        if reset='1' then
846
            p2_muldiv_started <= '0';
847
        else
848
            p2_muldiv_started <= p1_muldiv_running;
849
        end if;
850
    end if;
851
end process pipeline_stage1_register2;
852 6 ja_rd
 
853 12 ja_rd
 
854 6 ja_rd
-- Stage 2 pipeline register. Split in two for convenience.
855 2 ja_rd
-- This register deals with two kinds of stalls:
856
-- * When the pipeline stalls because of a load interlock, this register is 
857
--   allowed to update so that the load operation can complete while the rest of
858
--   the pipeline is frozen.
859
-- * When the stall is caused by any other reason, this register freezes with 
860
--   the rest of the machine.
861 6 ja_rd
 
862
-- Part of stage 2 register that controls load operation
863
pipeline_stage2_register_load_control:
864 2 ja_rd
process(clk)
865
begin
866
    if clk'event and clk='1' then
867 6 ja_rd
        -- Clear load control, effectively preventing load, at reset or if
868
        -- the previous instruction raised an exception.
869
        if reset='1' or p2_exception='1' then
870 132 ja_rd
            p2_do_store <= '0';
871 2 ja_rd
            p2_do_load <= '0';
872
            p2_ld_upper_hword <= '0';
873
            p2_ld_upper_byte <= '0';
874
            p2_ld_unsigned <= '0';
875
            p2_load_target <= "00000";
876 6 ja_rd
 
877
        -- Load signals from previous stage only if there is no pipeline stall
878
        -- unless the stall is caused by interlock (@note1).
879
        elsif (stall_pipeline='0' or load_interlock='1') then
880
            -- Disable reg bank writeback if pipeline is stalled; this prevents
881
            -- duplicate writes in case the stall is a mem_wait.
882 2 ja_rd
            if pipeline_stalled='0' then
883
                p2_do_load <= p1_do_load;
884
            else
885
                p2_do_load <= '0';
886
            end if;
887 132 ja_rd
            p2_do_store <= p1_do_store;
888 2 ja_rd
            p2_load_target <= p1_rd_num;
889
            p2_ld_upper_hword <= p1_ld_upper_hword;
890
            p2_ld_upper_byte <= p1_ld_upper_byte;
891
            p2_ld_unsigned <= p1_ld_unsigned;
892 6 ja_rd
        end if;
893
    end if;
894
end process pipeline_stage2_register_load_control;
895
 
896
-- All the rest of the stage 2 register
897
pipeline_stage2_register_others:
898
process(clk)
899
begin
900
    if clk'event and clk='1' then
901
        if reset='1' then
902
            p2_exception <= '0';
903
 
904
        -- Load signals from previous stage only if there is no pipeline stall
905
        -- unless the stall is caused by interlock (@note1).
906
        elsif (stall_pipeline='0' or load_interlock='1') then
907 2 ja_rd
            p2_rd_addr <= p1_data_addr(1 downto 0);
908 6 ja_rd
            p2_exception <= p1_exception;
909 2 ja_rd
        end if;
910
    end if;
911 6 ja_rd
end process pipeline_stage2_register_others;
912 2 ja_rd
 
913
--------------------------------------------------------------------------------
914 121 ja_rd
-- Pipeline control logic (stall control)
915 2 ja_rd
 
916 121 ja_rd
-- FIXME demonstrate that this combinational will not have bad glitches
917 46 ja_rd
stall_pipeline <= mem_wait or load_interlock or p1_muldiv_stall;
918
 
919
-- FIXME load interlock should happen only if the instruction following 
920
-- the load actually uses the load target register. Something like this:
921
-- (p1_do_load='1' and (p1_rd_num=p0_rs_num or p1_rd_num=p0_rt_num))
922
load_interlock <= '1' when
923
    p1_do_load='1' and      -- this is a load instruction
924
    pipeline_stalled='0' and -- not already stalled (i.e. assert for 1 cycle)
925
    (p1_rs1_hazard='1' or p1_rs2_hazard='1')
926
    else '0';
927
 
928
 
929
pipeline_stalled <= stalled_interlock or stalled_memwait or stalled_muldiv;
930
 
931 2 ja_rd
pipeline_stall_registers:
932
process(clk)
933
begin
934
    if clk'event and clk='1' then
935
        if reset='1' then
936 46 ja_rd
            stalled_interlock <= '0';
937
            stalled_memwait <= '0';
938
            stalled_muldiv <= '0';
939 2 ja_rd
        else
940 46 ja_rd
            if mem_wait='1' then
941
                stalled_memwait <= '1';
942 2 ja_rd
            else
943 46 ja_rd
                stalled_memwait <= '0';
944 2 ja_rd
            end if;
945 35 ja_rd
 
946 46 ja_rd
            if p1_muldiv_stall='1' then
947
                stalled_muldiv <= '1';
948
            else
949
                stalled_muldiv <= '0';
950
            end if;
951
 
952 35 ja_rd
            -- stalls caused by mem_wait and load_interlock are independent and
953
            -- must not overlap; so when mem_wait='1' the cache stall takes
954
            -- precedence and the loa interlock must wait.
955
            if mem_wait='0' then
956
                if load_interlock='1' then
957 46 ja_rd
                    stalled_interlock <= '1';
958 35 ja_rd
                else
959 46 ja_rd
                    stalled_interlock <= '0';
960 35 ja_rd
                end if;
961 2 ja_rd
            end if;
962
        end if;
963
    end if;
964
end process pipeline_stall_registers;
965
 
966 121 ja_rd
-- Here's where we stall the pipeline upon load reg bank hazards
967
-- FIXME (@note2) for the time being we stall the pipeline for ALL loads
968 28 ja_rd
p1_rs1_hazard <= '1'; --'1' when p0_uses_rs1='1' and p1_rd_num=p0_rs_num else '0';
969
p1_rs2_hazard <= '1'; --'1' when p0_uses_rs2='1' and p1_rd_num=p0_rt_num else '0';
970
 
971
with p1_ir_op select p0_uses_rs1 <=
972
    '0' when "000010",
973
    '0' when "000011",
974
    '0' when "001111",
975
    '0' when "001000",
976
    '1' when others;
977
 
978
with p1_ir_op select p0_uses_rs2 <=
979
    '1' when "000000",
980
    '1' when "000100",
981
    '1' when "000101",
982
    '1' when "000110",
983
    '1' when "000111",
984
    '1' when "010000",
985
    '1' when "101000",
986
    '1' when "101001",
987
    '1' when "101010",
988
    '1' when "101011",
989
    '1' when "101110",
990
    '0' when others;
991
 
992
 
993 121 ja_rd
--##############################################################################
994
-- Data memory interface
995
 
996 2 ja_rd
--------------------------------------------------------------------------------
997 121 ja_rd
-- Memory addressing adder (data address generation)
998 2 ja_rd
 
999
p1_data_offset(31 downto 16) <= (others => p1_data_imm(15));
1000
p1_data_offset(15 downto 0) <= p1_data_imm(15 downto 0);
1001
 
1002
p1_data_addr <= p1_rs + p1_data_offset;
1003
 
1004
--------------------------------------------------------------------------------
1005 121 ja_rd
-- Write enable vector
1006 2 ja_rd
 
1007
-- byte_we is a function of the write size and alignment
1008
-- size = {00=1,01=2,11=4}; we 3 is MSB, 0 is LSB; big endian => 00 is msb
1009
 
1010 140 ja_rd
p1_we_control <= (mem_wait) & p1_do_store & p1_store_size & p1_data_addr(1 downto 0);
1011 132 ja_rd
--p1_we_control <= (pipeline_stalled) & p1_do_store & p1_store_size & p1_data_addr(1 downto 0);
1012
 
1013
-- Bug: For two SW instructions in a row, the 2nd one will be stalled and lost: 
1014
-- the write will never be executed by the cache.
1015 140 ja_rd
-- Fixed by stalling immediately after asserting byte_we.
1016
-- FIXME the above fix has been tested but is still under trial (provisional)
1017
 
1018 132 ja_rd
-- The present code will not work in cache-less systems (such as the tb0) if 
1019
-- it stalls the CPU. Solution: don't allow stalls in cache-less systems.
1020
-- FIXME this little mess has to be documented.
1021
 
1022
 
1023 2 ja_rd
with p1_we_control select byte_we <=
1024
    "1000"  when "010000",    -- SB %0
1025
    "0100"  when "010001",    -- SB %1
1026
    "0010"  when "010010",    -- SB %2
1027
    "0001"  when "010011",    -- SB %3
1028
    "1100"  when "010100",    -- SH %0
1029
    "0011"  when "010110",    -- SH %2
1030
    "1111"  when "011100",    -- SW %4
1031
    "0000"  when others; -- all other combinations are spurious so don't write
1032
 
1033
-- Data to be stored always comes straight from the reg bank, but it needs to 
1034
-- be shifted so that the LSB is aligned to the write address:
1035
 
1036
data_wr(7 downto 0) <= p1_rt(7 downto 0);
1037
 
1038
with p1_we_control select data_wr(15 downto 8) <=
1039
    p1_rt( 7 downto  0) when "010010",  -- SB %2
1040
    p1_rt(15 downto  8) when others;
1041
 
1042
with p1_we_control select data_wr(23 downto 16) <=
1043
    p1_rt( 7 downto  0) when "010001",  -- SB %1
1044
    p1_rt( 7 downto  0) when "010100",  -- SH %0
1045
    p1_rt(23 downto 16) when others;
1046
 
1047
with p1_we_control select data_wr(31 downto 24) <=
1048
    p1_rt( 7 downto  0) when "010000",  -- SB %0
1049
    p1_rt(15 downto  8) when "010100",  -- SH %0
1050
    p1_rt(31 downto 24) when others;
1051
 
1052
 
1053
--##############################################################################
1054 153 ja_rd
-- CP0 and exception processing
1055 2 ja_rd
 
1056
 
1057 251 ja_rd
cp0_mosi.index <= p1_ir_reg(15 downto 11);
1058
cp0_mosi.we <= p1_set_cp0;
1059
cp0_mosi.data <= p1_rt;
1060
cp0_mosi.pc_restart <= p0_pc_restart;
1061
cp0_mosi.pipeline_stalled <= pipeline_stalled;
1062
cp0_mosi.exception <= p1_exception;
1063
cp0_mosi.rfe <= p1_rfe;
1064
cp0_mosi.unknown_opcode <= p1_unknown_opcode;
1065
cp0_mosi.missing_cop <= p1_cp_unavailable;
1066
cp0_mosi.syscall <= not p1_ir_fn(0);
1067
cp0_mosi.stall <= stall_pipeline;
1068 101 ja_rd
 
1069 28 ja_rd
 
1070 251 ja_rd
cache_enable <= cp0_miso.idcache_enable;
1071
ic_invalidate <= cp0_miso.icache_invalidate;
1072 2 ja_rd
 
1073 28 ja_rd
 
1074 251 ja_rd
cop0 : entity work.mips_cop0
1075
    port map (
1076
        clk             => clk,
1077
        reset           => reset,
1078
 
1079
        cpu_i           => cp0_mosi,
1080
        cpu_o           => cp0_miso
1081
    );
1082
 
1083
 
1084 2 ja_rd
end architecture rtl;
1085
 
1086
--------------------------------------------------------------------------------
1087
-- Implementation notes
1088
--------------------------------------------------------------------------------
1089
-- @note1 : 
1090
-- This is the meaning of these two signals:
1091 46 ja_rd
-- pipeline_stalled & stalled_interlock =>
1092 2 ja_rd
--  "00" => normal state
1093
--  "01" => normal state (makes for easier decoding)
1094
--  "10" => all stages of pipeline stalled, including rbank
1095
--  "11" => all stages of pipeline stalled, except reg bank write port
1096
-- 
1097
-- Just to clarify, 'stage X stalled' here means that the registers named 
1098
-- pX_* don't load.
1099
--
1100
-- The register bank WE is enabled when the pipeline is not stalled and when 
1101
-- it is stalled because of a load interlock; so that in case of interlock the
1102
-- load operation can complete while the rest of the pipeline is frozen.
1103 121 ja_rd
--
1104
-- @note2:
1105
-- The logic that checks register indices for data hazards is 'commented out'
1106
-- because it has not been tested yet.
1107 153 ja_rd
--
1108
-- @note3:
1109
-- CP0 instructions (mtc0, mfc0 and rfe) are only partially decoded.
1110
-- This is possible because no other VALID MIPS* opcode shares the decoded 
1111
-- part; that is, we're not going to misdecode a MIPS32 opcode, but we MIGHT
1112
-- mistake a bad opcode for a COP0; we'll live with that for the time being.
1113
--
1114 2 ja_rd
--------------------------------------------------------------------------------

powered by: WebSVN 2.1.0

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