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

Subversion Repositories ion

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

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

powered by: WebSVN 2.1.0

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