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

Subversion Repositories ion

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

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

powered by: WebSVN 2.1.0

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