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

Subversion Repositories ion

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

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

powered by: WebSVN 2.1.0

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