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

Subversion Repositories ion

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

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

powered by: WebSVN 2.1.0

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