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

Subversion Repositories ion

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

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

powered by: WebSVN 2.1.0

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