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

Subversion Repositories ion

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

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

powered by: WebSVN 2.1.0

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