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

Subversion Repositories ion

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

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
-- NOTE: exceptions only partially implemented; jumps, loads and stores are
14
-- not aborted.
15
-- 
16
--
17
--------------------------------------------------------------------------------
18
--### PLASMA features not implemented yet
19
--  # MUL/DIV
20
--
21
--### MIPS-I things not implemented
22
--  # Invalid instruction detection
23
--  # Kernel/user status
24
--  # RTE instruction
25
--  # Most of the CP0 registers and of course all of the CP1
26
--  # External interrupts
27
--
28
--### Things implemented but not tested
29
--  # Syscall instruction (does a jal to 0x3c and that's it)
30
--  # Memory pause input
31
--
32
--### Things with provisional implementation
33
-- 
34
-- 1.- Load interlocks: the pipeline is stalled for every load instruction, even
35
--     if the target register is not used in the following instruction. So that
36
--     every load takes two cycles.
37
--     The interlock logic should check register indices.
38
--
39
-- 2.- Invalid instructions are not detected as such. Their behaviour is
40
--     undefined and inpredictable.
41
--     Invalid instructions should trigger an exception or at least just NOP.
42
--     This is closely related to privilege level so it will have to wait.
43
--------------------------------------------------------------------------------
44
 
45
library ieee;
46
use ieee.std_logic_1164.all;
47
use ieee.std_logic_arith.all;
48
use ieee.std_logic_unsigned.all;
49
use work.mips_pkg.all;
50
 
51
entity mips_cpu is
52
    generic(
53
        XILINX_REGBANK  : string  := "distributed" -- {distributed|block}
54
    );
55
    port(
56
        clk             : in std_logic;
57
        reset           : in std_logic;
58
        interrupt       : in std_logic;
59
 
60
        data_rd_addr    : out std_logic_vector(31 downto 0);
61
        data_rd         : in std_logic_vector(31 downto 0);
62
        data_rd_vma     : out std_logic;
63
 
64
        code_rd_addr    : out std_logic_vector(31 downto 2);
65
        code_rd         : in std_logic_vector(31 downto 0);
66
        code_rd_vma     : out std_logic;
67
 
68
        data_wr_addr    : out std_logic_vector(31 downto 2);
69
        byte_we         : out std_logic_vector(3 downto 0);
70
        data_wr         : out std_logic_vector(31 downto 0);
71
 
72
        -- NOTE: needs to be synchronous to clk
73
        mem_wait        : in std_logic
74
    );
75
end; --entity mips_cpu
76
 
77
architecture rtl of mips_cpu is
78
 
79
--------------------------------------------------------------------------------
80
-- Pipeline stage 0
81
 
82
signal p0_pc_reg :          t_pc;
83 8 ja_rd
signal p0_pc_restart :      t_pc;
84 2 ja_rd
signal p0_pc_incremented :  t_pc;
85
signal p0_pc_jump :         t_pc;
86
signal p0_pc_branch :       t_pc;
87
signal p0_pc_target :       t_pc;
88
signal p0_pc_next :         t_pc;
89
signal p0_rs_num :          t_regnum;
90
signal p0_rt_num :          t_regnum;
91
signal p0_jump_cond_value : std_logic;
92
signal p0_rbank_rs_hazard : std_logic;
93
signal p0_rbank_rt_hazard : std_logic;
94
 
95
--------------------------------------------------------------------------------
96
-- Pipeline stage 1
97
 
98
 
99
signal p1_rbank :           t_rbank := (others => X"00000000");
100
 
101
-- IMPORTANT: This attribute is used by Xilinx tools to select how to implement
102
-- the register bank. If we don't use it, by default XST would infer 2 BRAMs for
103
-- the 1024-bit 3-port reg bank, which you probably don't want.
104
-- This can take the values {distributed|block}.
105
attribute ram_style :       string;
106
attribute ram_style of p1_rbank : signal is "distributed";
107
 
108
signal p1_rs, p1_rt :       t_word;
109
signal p1_rs_rbank :        t_word;
110
signal p1_rt_rbank :        t_word;
111
signal p1_rbank_forward :   t_word;
112
signal p1_rd_num :          t_regnum;
113
signal p1_rbank_wr_addr :   t_regnum;
114
signal p1_rbank_we :        std_logic;
115
signal p1_rbank_wr_data :   t_word;
116
signal p1_alu_inp1 :        t_word;
117
signal p1_alu_inp2 :        t_word;
118
signal p1_alu_outp :        t_word;
119
-- ALU control inputs (shortened name for brevity in expressions)
120
signal p1_ac :              t_alu_control;
121
-- ALU flag outputs (comparison results)
122
signal p1_alu_flags :       t_alu_flags;
123
-- immediate data, sign- or zero-extended as required by IR
124
signal p1_data_imm :        t_word;
125
signal p1_branch_offset :   t_pc;
126
signal p1_branch_offset_sex:std_logic_vector(31 downto 18);
127
signal p1_rbank_rs_hazard : std_logic;
128
signal p1_rbank_rt_hazard : std_logic;
129
signal p1_jump_type_set0 :  std_logic_vector(1 downto 0);
130
signal p1_jump_type_set1 :  std_logic_vector(1 downto 0);
131
signal p1_ir_reg :          std_logic_vector(31 downto 0);
132
signal p1_ir_op :           std_logic_vector(31 downto 26);
133
signal p1_ir_fn :           std_logic_vector(5 downto 0);
134
signal p1_op_special :      std_logic;
135
signal p1_exception :       std_logic;
136
signal p1_do_reg_jump :     std_logic;
137
signal p1_do_zero_ext_imm : std_logic;
138
signal p1_set_cp0 :         std_logic;
139
signal p1_get_cp0 :         std_logic;
140
signal p1_load_hi :         std_logic;
141
signal p1_load_lo :         std_logic;
142
signal p1_alu_op2_sel :     std_logic_vector(1 downto 0);
143
signal p1_alu_op2_sel_set0: std_logic_vector(1 downto 0);
144
signal p1_alu_op2_sel_set1: std_logic_vector(1 downto 0);
145
signal p1_do_load :         std_logic;
146
signal p1_do_store :        std_logic;
147
signal p1_store_size :      std_logic_vector(1 downto 0);
148
signal p1_we_control :      std_logic_vector(5 downto 0);
149
signal p1_load_alu :        std_logic;
150
signal p1_load_alu_set0 :   std_logic;
151
signal p1_load_alu_set1 :   std_logic;
152
signal p1_ld_upper_hword :  std_logic;
153
signal p1_ld_upper_byte :   std_logic;
154
signal p1_ld_unsigned :     std_logic;
155
signal p1_jump_type :       std_logic_vector(1 downto 0);
156
signal p1_link :            std_logic;
157
signal p1_jump_cond_sel :   std_logic_vector(2 downto 0);
158
signal p1_data_addr :       t_addr;
159
signal p1_data_offset :     t_addr;
160
 
161 12 ja_rd
signal p1_muldiv_result :   t_word;
162
signal p1_muldiv_func :     t_mult_function;
163
signal p1_muldiv_running :  std_logic;
164
signal p1_muldiv_started :  std_logic;
165
signal p1_muldiv_stall :    std_logic;
166
 
167
 
168 2 ja_rd
--------------------------------------------------------------------------------
169
-- Pipeline stage 2
170
 
171 12 ja_rd
signal p2_muldiv_started :  std_logic;
172 2 ja_rd
signal p2_exception :       std_logic;
173
signal p2_rd_addr :         std_logic_vector(1 downto 0);
174
signal p2_rd_mux_control :  std_logic_vector(3 downto 0);
175
signal p2_load_target :     t_regnum;
176
signal p2_do_load :         std_logic;
177
signal p2_ld_upper_hword :  std_logic;
178
signal p2_ld_upper_byte :   std_logic;
179
signal p2_ld_unsigned :     std_logic;
180
signal p2_wback_mux_sel :   std_logic_vector(1 downto 0);
181
signal p2_data_word_rd :    t_word;
182
signal p2_data_word_ext :   std_logic;
183
 
184
--------------------------------------------------------------------------------
185
-- Global control signals 
186
 
187
signal load_interlock :     std_logic;
188
signal stall_pipeline :     std_logic;
189
-- pipeline is stalled for any reason
190
signal pipeline_stalled :   std_logic;
191
-- pipeline is stalled because of a load instruction interlock
192
signal pipeline_interlocked:std_logic;
193
 
194
--------------------------------------------------------------------------------
195
-- CP0 registers and signals
196
 
197
-- CP0[12]: status register 
198
-- FIXME status flags unimplemented
199
signal cp0_status :         std_logic_vector(1 downto 0);
200
-- Output of CP0 register bank (only a few regs are implemented)
201
signal cp0_reg_read :       t_word;
202
-- CP0[14]: EPC register (PC value saved at exceptions)
203
signal cp0_epc :            t_pc;
204
 
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
    p1_muldiv_result    when "01", -- FIXME mux input wasted!
331
    p1_muldiv_result    when "10",
332
    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
            -- It's the addr of the instruction triggering the exception
433
            -- FIXME handle delay slot case
434
            if (p1_jump_type="00" or p0_jump_cond_value='0') then
435
                p0_pc_restart <= p0_pc_reg;
436
            end if;
437 2 ja_rd
        end if;
438
    end if;
439
end process pc_register;
440
 
441
-- p0_pc_reg holds the same addr as the addr register of the external synchronous 
442
-- memory; what we put on the addr bus is p0_pc_next.
443
data_rd_addr <= p1_data_addr(31 downto 0);
444
 
445
-- FIXME these two need to pushed behind a register, they are glitch-prone
446
data_rd_vma <= p1_do_load and not pipeline_stalled; -- FIXME register
447
code_rd_vma <= not stall_pipeline; -- FIXME registe
448
 
449
code_rd_addr <= p0_pc_next;
450
 
451
data_wr_addr <= p1_data_addr(31 downto 2);
452
 
453
-- compute target of J/JR instructions
454
p0_pc_jump <=   p1_rs(31 downto 2) when p1_do_reg_jump='1' else
455
                p0_pc_reg(31 downto 28) & p1_ir_reg(25 downto 0);
456
 
457
-- compute target of relative branch instructions
458
p1_branch_offset_sex <= (others => p1_ir_reg(15));
459
p1_branch_offset <= p1_branch_offset_sex & p1_ir_reg(15 downto 0);
460
-- p0_pc_reg is the addr of the instruction in delay slot
461
p0_pc_branch <= p0_pc_reg + p1_branch_offset;
462
 
463
-- decide which jump target is to be used
464
p0_pc_target <= X"0000003"&"11"     when p1_exception='1' else
465
             p0_pc_jump             when p1_jump_type(0)='1' else
466
             p0_pc_branch;
467
 
468
 
469
--##############################################################################
470
-- Instruction decoding and IR
471
 
472
instruction_register:
473
process(clk)
474
begin
475
    if clk'event and clk='1' then
476
        if reset='1' then
477
            p1_ir_reg <= (others => '0');
478
        elsif stall_pipeline='0' then
479
            p1_ir_reg <= code_rd;
480
        end if;
481
    end if;
482
end process instruction_register;
483
 
484
-- 'Extract' main fields from IR, for convenience
485
p1_ir_op <= p1_ir_reg(31 downto 26);
486
p1_ir_fn <= p1_ir_reg(5 downto 0);
487
 
488
-- Decode jump type, if any, for instructions with op/=0
489
with p1_ir_op select p1_jump_type_set0 <=
490
    -- FIXME weed out invalid instructions
491
    "10" when "000001", -- BLTZ, BGEZ, BLTZAL, BGTZAL
492
    "11" when "000010", -- J
493
    "11" when "000011", -- JAL
494
    "10" when "000100", -- BEQ
495
    "10" when "000101", -- BNE
496
    "10" when "000110", -- BLEZ
497
    "10" when "000111", -- BGTZ
498
    "00" when others;   -- no jump
499
 
500
-- Decode jump type, if any, for instructions with op=0
501
p1_jump_type_set1 <= "11" when p1_op_special='1' and
502
                               p1_ir_reg(5 downto 1)="00100"
503
                     else "00";
504
 
505
-- Decode jump type for the instruction in IR (composite of two formats)
506
p1_jump_type <= p1_jump_type_set0 or p1_jump_type_set1;
507
 
508
p1_link <= '1' when (p1_ir_op="000000" and p1_ir_reg(5 downto 0)="001001") or
509
                    (p1_ir_op="000001" and p1_ir_reg(20)='1') or
510
                    (p1_ir_op="000011")
511
           else '0';
512
 
513
-- Decode jump condition: encode a mux control signal from IR...
514
p1_jump_cond_sel <=
515
    "001" when p1_ir_op="000001" and p1_ir_reg(16)='0' else --   op1 < 0   BLTZ*
516
    "101" when p1_ir_op="000001" and p1_ir_reg(16)='1' else -- !(op1 < 0) BNLTZ*
517
    "010" when p1_ir_op="000100" else                       --   op1 == op2  BEQ
518
    "110" when p1_ir_op="000101" else                       -- !(op1 == op2) BNE
519
    "011" when p1_ir_op="000110" else                       --   op1 <= 0   BLEZ
520
    "111" when p1_ir_op="000111" else                       -- !(op1 <= 0)  BGTZ
521
    "000";                                                  -- always
522
 
523
-- ... and use mux control signal to select the condition value
524
with p1_jump_cond_sel select p0_jump_cond_value <=
525
        p1_alu_flags.inp1_lt_zero       when "001",
526
    not p1_alu_flags.inp1_lt_zero       when "101",
527
        p1_alu_flags.inp1_eq_inp2       when "010",
528
    not p1_alu_flags.inp1_eq_inp2       when "110",
529
        (p1_alu_flags.inp1_lt_inp2 or
530
         p1_alu_flags.inp1_eq_inp2)     when "011",
531
    not (p1_alu_flags.inp1_lt_inp2 or
532
         p1_alu_flags.inp1_eq_inp2)     when "111",
533
    '1'                                 when others;
534
 
535
-- Decode instructions that launch exceptions
536
p1_exception <= '1' when p1_op_special='1' and p1_ir_reg(5 downto 1)="00110"
537
                else '0';
538
 
539
-- Decode MTC0/MFC0 instructions
540
p1_set_cp0 <= '1' when p1_ir_reg(31 downto 21)="01000000100" else '0';
541
p1_get_cp0 <= '1' when p1_ir_reg(31 downto 21)="01000000000" else '0';
542
 
543
-- FIXME elaborate and explain this
544
 
545
p1_op_special <= '1' when p1_ir_op="000000" else '0';
546
 
547
p1_do_reg_jump <= '1' when p1_op_special='1' and p1_ir_fn(5 downto 1)="00100" else '0';
548
p1_do_zero_ext_imm <= '1' when (p1_ir_op(31 downto 28)="0011") else '0';
549
 
550
-- Decode input data mux control (LW, LH, LB, LBU, LHU) and load enable
551 6 ja_rd
p1_do_load <= '1' when p1_ir_op(31 downto 29)="100" and
552
                       p2_exception='0'
553
              else '0';
554 2 ja_rd
 
555
p1_load_alu_set0 <= '1'
556
    when p1_op_special='1' and
557
        ((p1_ir_op(31 downto 29)="000" and p1_ir_op(27 downto 26)="00") or
558
         (p1_ir_op(31 downto 29)="000" and p1_ir_op(27 downto 26)="10") or
559
         (p1_ir_op(31 downto 29)="000" and p1_ir_op(27 downto 26)="11") or
560
         (p1_ir_op(31 downto 29)="000" and p1_ir_op(27 downto 26)="00") or
561
         (p1_ir_op(31 downto 28)="0100" and p1_ir_op(27 downto 26)="00") or
562
         (p1_ir_op(31 downto 28)="0100" and p1_ir_op(27 downto 26)="10") or
563
         (p1_ir_op(31 downto 28)="1000") or
564
         (p1_ir_op(31 downto 28)="1001") or
565
         (p1_ir_op(31 downto 28)="1010" and p1_ir_op(27 downto 26)="10") or
566
         (p1_ir_op(31 downto 28)="1010" and p1_ir_op(27 downto 26)="11") or
567
         (p1_ir_op(31 downto 28)="0010" and p1_ir_op(27 downto 26)="01"))
568
    else '0';
569
 
570
with p1_ir_op select p1_load_alu_set1 <=
571
    '1' when "001000",
572
    '1' when "001001",
573
    '1' when "001010",
574
    '1' when "001011",
575
    '1' when "001100",
576
    '1' when "001101",
577
    '1' when "001110",
578
    '1' when "001111",
579
    -- FIXME a few others missing: MFC0, etc
580
    '0' when others;
581
p1_load_alu <= p1_load_alu_set0 or p1_load_alu_set1;
582
 
583
p1_ld_upper_hword <= p1_ir_op(27); -- use input upper hword vs. sign extend/zero
584
p1_ld_upper_byte <= p1_ir_op(26);  -- use input upper byte vs. sign extend/zero
585
p1_ld_unsigned <= p1_ir_op(28);    -- sign extend vs. zero extend
586
 
587
-- ALU input-2 selection: use external data for 2x opcodes (loads)
588
p1_alu_op2_sel_set0 <=
589
    "11" when    p1_ir_op(31 downto 30)="10" or p1_ir_op(29)='1' else
590
    "00";
591
 
592
-- ALU input-2 selection: use registers Hi and Lo for MFHI, MFLO
593
with p1_ir_fn select p1_alu_op2_sel_set1 <=
594
    "01" when "010000",
595
    "10" when "010010",
596
    "00" when others;
597
 
598
-- ALU input-2 final selection
599
p1_alu_op2_sel <= p1_alu_op2_sel_set0 or p1_alu_op2_sel_set1;
600
 
601
-- Decode store operations
602
p1_do_store <= '1' when p1_ir_op(31 downto 29)="101" else '0';
603
p1_store_size <= p1_ir_op(27 downto 26);
604
 
605
 
606
-- Decode load enables for Hi and Lo registers (MTHI and MTLO)
607
p1_load_hi <= '1' when p1_op_special='1' and p1_ir_fn="010001" else '0';
608
p1_load_lo <= '1' when p1_op_special='1' and p1_ir_fn="010011" else '0';
609
 
610
-- Decode ALU control dignals
611
 
612
p1_ac.use_slt <= '1' when (p1_ir_op="000001" and p1_ir_reg(20 downto 17)="01000") or
613
                        (p1_ir_op="000000" and p1_ir_reg(5 downto 1)="10101") or
614
                        p1_ir_op="001010" or p1_ir_op="001011"
615
               else '0';
616
p1_ac.arith_unsigned <= p1_ac.use_slt and p1_ir_reg(0);
617
 
618
p1_ac.use_logic(0) <= '1' when (p1_op_special='1' and p1_ir_fn(5 downto 3)/="000") or
619
                    -- all immediate arith and logic
620
                    p1_ir_op(31 downto 29)="001"
621
                 else '0';
622
p1_ac.use_logic(1) <= '1' when (p1_op_special='1' and p1_ir_fn="100111") else '0';
623
 
624
p1_ac.use_arith <= '1' when p1_ir_op(31 downto 28)="0010" or
625
                            (p1_op_special='1' and
626
                                (p1_ir_fn(5 downto 2)="1000" or
627
                                p1_ir_fn(5 downto 2)="1010"))
628
                 else '0';
629
 
630
-- selection of 2nd internal alu operand: {i2, /i2, i2<<16, 0x0}
631
p1_ac.neg_sel(1)<= '1' when p1_ir_op(29 downto 26) = "1111" else '0';
632
p1_ac.neg_sel(0)<= '1' when   p1_ir_op="001010" or
633
                            p1_ir_op="001011" or
634
                            p1_ir_op(31 downto 28)="0001" or
635
                            (p1_op_special='1' and
636
                                (p1_ir_fn="100010" or
637
                                 p1_ir_fn="100011" or
638
                                 p1_ir_fn(5 downto 2)="1010"))
639
                 else '0';
640
p1_ac.cy_in <= p1_ac.neg_sel(0);
641
 
642
p1_ac.shift_sel <= p1_ir_fn(1 downto 0);
643
 
644
p1_ac.logic_sel <= "00" when (p1_op_special='1' and p1_ir_fn="100100") else
645
                 "01" when (p1_op_special='1' and p1_ir_fn="100101") else
646
                 "10" when (p1_op_special='1' and p1_ir_fn="100110") else
647
                 "01" when (p1_op_special='1' and p1_ir_fn="100111") else
648
                 "00" when (p1_ir_op="001100") else
649
                 "01" when (p1_ir_op="001101") else
650
                 "10" when (p1_ir_op="001110") else
651
                 "11";
652
 
653
p1_ac.shift_amount <= p1_ir_reg(10 downto 6) when p1_ir_fn(2)='0' else p1_rs(4 downto 0);
654
 
655
--------------------------------------------------------------------------------
656
 
657
-- Stage 1 pipeline register. Involved in ALU control.
658
pipeline_stage1_register:
659
process(clk)
660
begin
661
    if clk'event and clk='1' then
662
        if reset='1' then
663
            p1_rbank_rs_hazard <= '0';
664
            p1_rbank_rt_hazard <= '0';
665
        elsif stall_pipeline='0' then
666
            p1_rbank_rs_hazard <= p0_rbank_rs_hazard;
667
            p1_rbank_rt_hazard <= p0_rbank_rt_hazard;
668
        end if;
669
    end if;
670
end process pipeline_stage1_register;
671
 
672 12 ja_rd
pipeline_stage1_register2:
673
process(clk)
674
begin
675
    if clk'event and clk='1' then
676
        if reset='1' then
677
            p2_muldiv_started <= '0';
678
        else
679
            p2_muldiv_started <= p1_muldiv_running;
680
        end if;
681
    end if;
682
end process pipeline_stage1_register2;
683 6 ja_rd
 
684 12 ja_rd
 
685 6 ja_rd
-- Stage 2 pipeline register. Split in two for convenience.
686 2 ja_rd
-- This register deals with two kinds of stalls:
687
-- * When the pipeline stalls because of a load interlock, this register is 
688
--   allowed to update so that the load operation can complete while the rest of
689
--   the pipeline is frozen.
690
-- * When the stall is caused by any other reason, this register freezes with 
691
--   the rest of the machine.
692 6 ja_rd
 
693
-- Part of stage 2 register that controls load operation
694
pipeline_stage2_register_load_control:
695 2 ja_rd
process(clk)
696
begin
697
    if clk'event and clk='1' then
698 6 ja_rd
        -- Clear load control, effectively preventing load, at reset or if
699
        -- the previous instruction raised an exception.
700
        if reset='1' or p2_exception='1' then
701 2 ja_rd
            p2_do_load <= '0';
702
            p2_ld_upper_hword <= '0';
703
            p2_ld_upper_byte <= '0';
704
            p2_ld_unsigned <= '0';
705
            p2_load_target <= "00000";
706 6 ja_rd
 
707
        -- Load signals from previous stage only if there is no pipeline stall
708
        -- unless the stall is caused by interlock (@note1).
709
        elsif (stall_pipeline='0' or load_interlock='1') then
710
            -- Disable reg bank writeback if pipeline is stalled; this prevents
711
            -- duplicate writes in case the stall is a mem_wait.
712 2 ja_rd
            if pipeline_stalled='0' then
713
                p2_do_load <= p1_do_load;
714
            else
715
                p2_do_load <= '0';
716
            end if;
717
            p2_load_target <= p1_rd_num;
718
            p2_ld_upper_hword <= p1_ld_upper_hword;
719
            p2_ld_upper_byte <= p1_ld_upper_byte;
720
            p2_ld_unsigned <= p1_ld_unsigned;
721 6 ja_rd
        end if;
722
    end if;
723
end process pipeline_stage2_register_load_control;
724
 
725
-- All the rest of the stage 2 register
726
pipeline_stage2_register_others:
727
process(clk)
728
begin
729
    if clk'event and clk='1' then
730
        if reset='1' then
731
            p2_exception <= '0';
732
 
733
        -- Load signals from previous stage only if there is no pipeline stall
734
        -- unless the stall is caused by interlock (@note1).
735
        elsif (stall_pipeline='0' or load_interlock='1') then
736 2 ja_rd
            p2_rd_addr <= p1_data_addr(1 downto 0);
737 6 ja_rd
            p2_exception <= p1_exception;
738 2 ja_rd
        end if;
739
    end if;
740 6 ja_rd
end process pipeline_stage2_register_others;
741 2 ja_rd
 
742
--------------------------------------------------------------------------------
743
 
744
-- FIXME stall when needed: mem pause, mdiv pause and load interlock
745
 
746
pipeline_stall_registers:
747
process(clk)
748
begin
749
    if clk'event and clk='1' then
750
        if reset='1' then
751
            pipeline_stalled <= '0';
752
            pipeline_interlocked <= '0';
753
        else
754
            if stall_pipeline='1' then
755
                pipeline_stalled <= '1';
756
            else
757
                pipeline_stalled <= '0';
758
            end if;
759
            if load_interlock='1' then
760
                pipeline_interlocked <= '1';
761
            else
762
                pipeline_interlocked <= '0';
763
            end if;
764
        end if;
765
    end if;
766
end process pipeline_stall_registers;
767
 
768
-- FIXME make sure this combinational will not have bad glitches
769 12 ja_rd
stall_pipeline <= mem_wait or load_interlock or p1_muldiv_stall;
770 2 ja_rd
 
771
 
772
-- FIXME load interlock should happen only if the instruction following 
773
-- the load actually uses the load target register. Something like this:
774
-- (p1_do_load='1' and (p1_rd_num=p0_rs_num or p1_rd_num=p0_rt_num))
775
load_interlock <= '1' when (p1_do_load='1' and pipeline_stalled='0') else '0';
776
 
777
--------------------------------------------------------------------------------
778
 
779
p1_data_offset(31 downto 16) <= (others => p1_data_imm(15));
780
p1_data_offset(15 downto 0) <= p1_data_imm(15 downto 0);
781
 
782
p1_data_addr <= p1_rs + p1_data_offset;
783
 
784
--------------------------------------------------------------------------------
785
 
786
-- byte_we is a function of the write size and alignment
787
-- size = {00=1,01=2,11=4}; we 3 is MSB, 0 is LSB; big endian => 00 is msb
788
p1_we_control <= pipeline_stalled & p1_do_store & p1_store_size & p1_data_addr(1 downto 0);
789
 
790
with p1_we_control select byte_we <=
791
    "1000"  when "010000",    -- SB %0
792
    "0100"  when "010001",    -- SB %1
793
    "0010"  when "010010",    -- SB %2
794
    "0001"  when "010011",    -- SB %3
795
    "1100"  when "010100",    -- SH %0
796
    "0011"  when "010110",    -- SH %2
797
    "1111"  when "011100",    -- SW %4
798
    "0000"  when others; -- all other combinations are spurious so don't write
799
 
800
-- Data to be stored always comes straight from the reg bank, but it needs to 
801
-- be shifted so that the LSB is aligned to the write address:
802
 
803
data_wr(7 downto 0) <= p1_rt(7 downto 0);
804
 
805
with p1_we_control select data_wr(15 downto 8) <=
806
    p1_rt( 7 downto  0) when "010010",  -- SB %2
807
    p1_rt(15 downto  8) when others;
808
 
809
with p1_we_control select data_wr(23 downto 16) <=
810
    p1_rt( 7 downto  0) when "010001",  -- SB %1
811
    p1_rt( 7 downto  0) when "010100",  -- SH %0
812
    p1_rt(23 downto 16) when others;
813
 
814
with p1_we_control select data_wr(31 downto 24) <=
815
    p1_rt( 7 downto  0) when "010000",  -- SB %0
816
    p1_rt(15 downto  8) when "010100",  -- SH %0
817
    p1_rt(31 downto 24) when others;
818
 
819
 
820
--##############################################################################
821
-- CP0 (what little is implemented of it)
822
 
823
process(clk)
824
begin
825
    if clk'event and clk='1' then
826
        if reset='1' then
827
            -- "10" => mode=kernel; ints=disabled
828
            cp0_status <= "10";
829
        else
830
            -- no need to check for stall cycles when loading these
831
            if p1_set_cp0='1' then
832
                -- FIXME check for CP0 reg index
833
                cp0_status <= p1_rs(cp0_status'high downto 0);
834
            end if;
835
            if p1_exception='1' then
836 8 ja_rd
                cp0_epc <= p0_pc_restart;
837 2 ja_rd
            end if;
838
        end if;
839
    end if;
840
end process;
841
 
842
-- FIXME the mux should mask to zero for any unused reg index
843
cp0_reg_read <= X"0000000" & "00" & cp0_status when p1_rd_num="01100" else
844
                cp0_epc & "00";
845
 
846
end architecture rtl;
847
 
848
--------------------------------------------------------------------------------
849
-- Implementation notes
850
--------------------------------------------------------------------------------
851
-- @note1 : 
852
--
853
-- This is the meaning of these two signals:
854
-- pipeline_stalled & pipeline_interlocked =>
855
--  "00" => normal state
856
--  "01" => normal state (makes for easier decoding)
857
--  "10" => all stages of pipeline stalled, including rbank
858
--  "11" => all stages of pipeline stalled, except reg bank write port
859
-- 
860
-- Just to clarify, 'stage X stalled' here means that the registers named 
861
-- pX_* don't load.
862
--
863
-- The register bank WE is enabled when the pipeline is not stalled and when 
864
-- it is stalled because of a load interlock; so that in case of interlock the
865
-- load operation can complete while the rest of the pipeline is frozen.
866
--------------------------------------------------------------------------------

powered by: WebSVN 2.1.0

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