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

Subversion Repositories ion

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

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

powered by: WebSVN 2.1.0

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