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

Subversion Repositories ion

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

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

powered by: WebSVN 2.1.0

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