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

Subversion Repositories ion

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

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

powered by: WebSVN 2.1.0

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