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

Subversion Repositories ion

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

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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