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

Subversion Repositories ion

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

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

powered by: WebSVN 2.1.0

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