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

Subversion Repositories plasma

[/] [plasma/] [tags/] [arelease/] [vhdl/] [control.vhd] - Blame information for rev 397

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 rhoads
---------------------------------------------------------------------
2
-- TITLE: Controller / Opcode Decoder
3
-- AUTHOR: Steve Rhoads (rhoadss@yahoo.com)
4
-- DATE CREATED: 2/8/01
5
-- FILENAME: control.vhd
6
-- PROJECT: MIPS CPU core
7
-- COPYRIGHT: Software placed into the public domain by the author.
8
--    Software 'as is' without warranty.  Author liable for nothing.
9
-- DESCRIPTION:
10
--    Controls the CPU by decoding the opcode and generating control 
11
--    signals to the rest of the CPU.
12
--    This entity decodes the MIPS opcode into a Very-Long-Word-Instruction.
13
--    The 32-bit opcode is converted to a 
14
--       6+6+6+16+5+2+3+3+2+2+3+2+4 = 60 bit VLWI opcode.
15
--    Based on information found in:
16
--       "MIPS RISC Architecture" by Gerry Kane and Joe Heinrich
17
--       and "The Designer's Guide to VHDL" by Peter J. Ashenden
18
---------------------------------------------------------------------
19
library ieee;
20
use ieee.std_logic_1164.all;
21
use work.mips_pack.all;
22
 
23
entity control is
24
   port(opcode       : in  std_logic_vector(31 downto 0);
25
        intr_signal  : in  std_logic;
26
        rs_index     : out std_logic_vector(5 downto 0);
27
        rt_index     : out std_logic_vector(5 downto 0);
28
        rd_index     : out std_logic_vector(5 downto 0);
29
        imm_out      : out std_logic_vector(15 downto 0);
30
        alu_func     : out alu_function_type;
31
        shift_func   : out shift_function_type;
32
        mult_func    : out mult_function_type;
33
        branch_func  : out branch_function_type;
34
        a_source_out : out a_source_type;
35
        b_source_out : out b_source_type;
36
        c_source_out : out c_source_type;
37
        pc_source_out: out pc_source_type;
38
        mem_source_out:out mem_source_type);
39
end; --entity control
40
 
41
architecture logic of control is
42
--   type alu_function_type is (alu_nothing, alu_add, alu_subtract, 
43
--      alu_less_than, alu_less_than_signed, alu_equal, alu_not_equal,
44
--      alu_ltz, alu_lez, alu_eqz, alu_nez, alu_gez, alu_gtz,
45
--      alu_or, alu_and, alu_xor, alu_nor);
46
--   type shift_function_type is (
47
--      shift_nothing, shift_left_unsigned,  
48
--      shift_right_signed, shift_right_unsigned);
49
--   type mult_function_type is (
50
--      mult_nothing, mult_read_lo, mult_read_hi, mult_write_lo, 
51
--      mult_write_hi, mult_mult, mult_divide, mult_signed_divide);
52
--   type a_source_type is (from_reg_source, from_imm10_6);
53
--   type b_source_type is (from_reg_target, from_imm, from_signed_imm);
54
--   type c_source_type is (from_null, from_alu, from_shift, 
55
--      from_mult, from_memory, from_pc, from_imm_shift16,
56
--      from_reg_source_nez, from_reg_source_eqz);
57
--   type pc_source_type is (from_inc4, from_inc8, from_reg_source, 
58
--      from_opcode25_0, from_branch, from_lbranch);
59
begin
60
 
61
control_proc: process(opcode, intr_signal)
62
   variable op, func       : std_logic_vector(5 downto 0);
63
   variable rs, rt, rd     : std_logic_vector(5 downto 0);
64
   variable re, rtx        : std_logic_vector(4 downto 0);
65
   variable imm            : std_logic_vector(15 downto 0);
66
   variable alu_function   : alu_function_type;
67
   variable shift_function : shift_function_type;
68
   variable mult_function  : mult_function_type;
69
   variable a_source       : a_source_type;
70
   variable b_source       : b_source_type;
71
   variable c_source       : c_source_type;
72
   variable pc_source      : pc_source_type;
73
   variable branch_function: branch_function_type;
74
   variable mem_source     : mem_source_type;
75
begin
76
   alu_function := alu_nothing;
77
   shift_function := shift_nothing;
78
   mult_function := mult_nothing;
79
   a_source := a_from_reg_source;
80
   b_source := b_from_reg_target;
81
   c_source := c_from_null;
82
   pc_source := from_inc4;
83
   branch_function := branch_eq;
84
   mem_source := mem_none;
85
   op := opcode(31 downto 26);
86
   rs := '0' & opcode(25 downto 21);
87
   rt := '0' & opcode(20 downto 16);
88
   rtx := opcode(20 downto 16);
89
   rd := '0' & opcode(15 downto 11);
90
   re := opcode(10 downto 6);
91
   func := opcode(5 downto 0);
92
   imm := opcode(15 downto 0);
93
 
94
   case op is
95
   when "000000" =>   --00 SPECIAL
96
      case func is
97
      when "000000" =>   --00 SLL   r[rd]=r[rt]<<re;
98
         a_source := a_from_imm10_6;
99
         c_source := c_from_shift;
100
         shift_function := shift_left_unsigned;
101
      when "000010" =>   --02 SRL   r[rd]=u[rt]>>re;
102
         a_source := a_from_imm10_6;
103
         c_source := c_from_shift;
104
         shift_function := shift_right_unsigned;
105
      when "000011" =>   --03 SRA   r[rd]=r[rt]>>re;
106
         a_source := a_from_imm10_6;
107
         c_source := c_from_shift;
108
         shift_function := shift_right_signed;
109
      when "000100" =>   --04 SLLV  r[rd]=r[rt]<<r[rs];
110
         c_source := c_from_shift;
111
         shift_function := shift_left_unsigned;
112
      when "000110" =>   --06 SRLV  r[rd]=u[rt]>>r[rs];
113
         c_source := c_from_shift;
114
         shift_function := shift_right_unsigned;
115
      when "000111" =>   --07 SRAV  r[rd]=r[rt]>>r[rs];
116
         c_source := c_from_shift;
117
         shift_function := shift_right_signed;
118
      when "001000" =>   --08 JR    s->pc_next=r[rs];
119
         pc_source := from_branch;
120
         alu_function := alu_add;
121
         branch_function := branch_yes;
122
      when "001001" =>   --09 JALR  r[rd]=s->pc_next; s->pc_next=r[rs];
123
         c_source := c_from_pc;
124
         pc_source := from_branch;
125
         alu_function := alu_add;
126
         branch_function := branch_yes;
127
      when "001010" =>   --0a MOVZ  if(!r[rt]) r[rd]=r[rs]; /*IV*/
128
--         c_source := c_from_reg_source_eqz;
129
      when "001011" =>   --0b MOVN  if(r[rt]) r[rd]=r[rs];  /*IV*/
130
--         c_source := from_reg_source_nez;
131
      when "001100" =>   --0c SYSCALL
132
--         if(r[4]==0) printf("0x%8.8lx ",r[5]);
133
      when "001101" =>   --0d BREAK s->wakeup=1;
134
      when "001111" =>   --0f SYNC  s->wakeup=1;
135
      when "010000" =>   --10 MFHI  r[rd]=s->hi;
136
         c_source := c_from_mult;
137
         mult_function := mult_read_hi;
138
      when "010001" =>   --11 FTHI  s->hi=r[rs];
139
         mult_function := mult_write_hi;
140
      when "010010" =>   --12 MFLO  r[rd]=s->lo;
141
         c_source := c_from_mult;
142
         mult_function := mult_read_lo;
143
      when "010011" =>   --13 MTLO  s->lo=r[rs];
144
         mult_function := mult_write_lo;
145
      when "011000" =>   --18 MULT  s->lo=r[rs]*r[rt]; s->hi=0;
146
         mult_function := mult_mult;
147
      when "011001" =>   --19 MULTU s->lo=r[rs]*r[rt]; s->hi=0;
148
         mult_function := mult_mult;
149
      when "011010" =>   --1a DIV   s->lo=r[rs]/r[rt]; s->hi=r[rs]%r[rt];
150
         mult_function := mult_signed_divide;
151
      when "011011" =>   --1b DIVU  s->lo=r[rs]/r[rt]; s->hi=r[rs]%r[rt];
152
         mult_function := mult_divide;
153
      when "100000" =>   --20 ADD   r[rd]=r[rs]+r[rt];
154
         c_source := c_from_alu;
155
         alu_function := alu_add;
156
      when "100001" =>   --21 ADDU  r[rd]=r[rs]+r[rt];
157
         c_source := c_from_alu;
158
         alu_function := alu_add;
159
      when "100010" =>   --22 SUB   r[rd]=r[rs]-r[rt];
160
         c_source := c_from_alu;
161
         alu_function := alu_subtract;
162
      when "100011" =>   --23 SUBU  r[rd]=r[rs]-r[rt];
163
         c_source := c_from_alu;
164
         alu_function := alu_subtract;
165
      when "100100" =>   --24 AND   r[rd]=r[rs]&r[rt];
166
         c_source := c_from_alu;
167
         alu_function := alu_and;
168
      when "100101" =>   --25 OR    r[rd]=r[rs]|r[rt];
169
         c_source := c_from_alu;
170
         alu_function := alu_or;
171
      when "100110" =>   --26 XOR   r[rd]=r[rs]^r[rt];
172
         c_source := c_from_alu;
173
         alu_function := alu_xor;
174
      when "100111" =>   --27 NOR   r[rd]=~(r[rs]|r[rt]);
175
         c_source := c_from_alu;
176
         alu_function := alu_nor;
177
      when "101010" =>   --2a SLT   r[rd]=r[rs]<r[rt];
178
         c_source := c_from_alu;
179
         alu_function := alu_less_than_signed;
180
      when "101011" =>   --2b SLTU  r[rd]=u[rs]<u[rt];
181
         c_source := c_from_alu;
182
         alu_function := alu_less_than;
183
      when "101101" =>   --2d DADDU r[rd]=r[rs]+u[rt];
184
         c_source := c_from_alu;
185
         alu_function := alu_add;
186
      when "110001" =>   --31 TGEU
187
      when "110010" =>   --32 TLT
188
      when "110011" =>   --33 TLTU
189
      when "110100" =>   --34 TEQ 
190
      when "110110" =>   --36 TNE 
191
      when others =>
192
      end case;
193
   when "000001" =>   --00 REGIMM
194
      rt := "000000";
195
      rd := "011111";
196
      a_source := a_from_pc;
197
      b_source := b_from_immX4;
198
      alu_function := alu_add;
199
      pc_source := from_branch;
200
      branch_function := branch_gtz;
201
      --if(test) pc=pc+imm*4
202
      case rtx is
203
      when "10000" =>   --10 BLTZAL  r[31]=s->pc_next; branch=r[rs]<0;
204
         c_source := c_from_pc;
205
         branch_function := branch_ltz;
206
      when "00000" =>   --00 BLTZ    branch=r[rs]<0;
207
         branch_function := branch_ltz;
208
      when "10001" =>   --11 BGEZAL  r[31]=s->pc_next; branch=r[rs]>=0;
209
         c_source := c_from_pc;
210
         branch_function := branch_gez;
211
      when "00001" =>   --01 BGEZ    branch=r[rs]>=0;
212
         branch_function := branch_gez;
213
      when "10010" =>   --12 BLTZALL r[31]=s->pc_next; lbranch=r[rs]<0;
214
         c_source := c_from_pc;
215
         pc_source := from_lbranch;
216
         branch_function := branch_ltz;
217
      when "00010" =>   --02 BLTZL   lbranch=r[rs]<0;
218
         pc_source := from_lbranch;
219
         branch_function := branch_ltz;
220
      when "10011" =>   --13 BGEZALL r[31]=s->pc_next; lbranch=r[rs]>=0;
221
         c_source := c_from_pc;
222
         pc_source := from_lbranch;
223
         branch_function := branch_gez;
224
      when "00011" =>   --03 BGEZL   lbranch=r[rs]>=0;
225
         pc_source := from_lbranch;
226
         branch_function := branch_gez;
227
          when others =>
228
          end case;
229
   when "000011" =>   --03 JAL    r[31]=s->pc_next; s->pc_next=(s->pc&0xf0000000)|target;
230
      c_source := c_from_pc;
231
      rd := "011111";
232
      pc_source := from_opcode25_0;
233
   when "000010" =>   --02 J      s->pc_next=(s->pc&0xf0000000)|target; 
234
      pc_source := from_opcode25_0;
235
   when "000100" =>   --04 BEQ    branch=r[rs]==r[rt];
236
      a_source := a_from_pc;
237
      b_source := b_from_immX4;
238
      alu_function := alu_add;
239
      pc_source := from_branch;
240
      branch_function := branch_eq;
241
   when "000101" =>   --05 BNE    branch=r[rs]!=r[rt];
242
      a_source := a_from_pc;
243
      b_source := b_from_immX4;
244
      alu_function := alu_add;
245
      pc_source := from_branch;
246
      branch_function := branch_ne;
247
   when "000110" =>   --06 BLEZ   branch=r[rs]<=0;
248
      a_source := a_from_pc;
249
      b_source := b_from_immX4;
250
      alu_function := alu_add;
251
      pc_source := from_branch;
252
      branch_function := branch_ltz;
253
   when "000111" =>   --07 BGTZ   branch=r[rs]>0;
254
      a_source := a_from_pc;
255
      b_source := b_from_immX4;
256
      alu_function := alu_add;
257
      pc_source := from_branch;
258
      branch_function := branch_gtz;
259
   when "001000" =>   --08 ADDI   r[rt]=r[rs]+(short)imm;
260
      b_source := b_from_signed_imm;
261
      c_source := c_from_alu;
262
      rd := rt;
263
      alu_function := alu_add;
264
   when "001001" =>   --09 ADDIU  u[rt]=u[rs]+(short)imm;
265
      b_source := b_from_signed_imm;
266
      c_source := c_from_alu;
267
      rd := rt;
268
      alu_function := alu_add;
269
   when "001010" =>   --0a SLTI   r[rt]=r[rs]<(short)imm;
270
      b_source := b_from_signed_imm;
271
      c_source := c_from_alu;
272
      rd := rt;
273
      alu_function := alu_less_than;
274
   when "001011" =>   --0b SLTIU  u[rt]=u[rs]<(unsigned long)(short)imm;
275
      b_source := b_from_imm;
276
      c_source := c_from_alu;
277
      rd := rt;
278
      alu_function := alu_less_than;
279
   when "001100" =>   --0c ANDI   r[rt]=r[rs]&imm;
280
      b_source := b_from_imm;
281
      c_source := c_from_alu;
282
      rd := rt;
283
      alu_function := alu_and;
284
   when "001101" =>   --0d ORI    r[rt]=r[rs]|imm;
285
      b_source := b_from_imm;
286
      c_source := c_from_alu;
287
      rd := rt;
288
      alu_function := alu_or;
289
   when "001110" =>   --0e XORI   r[rt]=r[rs]^imm;
290
      b_source := b_from_imm;
291
      c_source := c_from_alu;
292
      rd := rt;
293
      alu_function := alu_xor;
294
   when "001111" =>   --0f LUI    r[rt]=(imm<<16);
295
      c_source := c_from_imm_shift16;
296
      rd := rt;
297
   when "010000" =>   --10 COP0
298
      alu_function := alu_or;
299
      c_source := c_from_alu;
300
      if opcode(23) = '0' then  --move from CP0
301
         rs := '1' & opcode(15 downto 11);
302
         rt := "000000";
303
         rd := '0' & opcode(20 downto 16);
304
      else                      --move to CP0
305
         rs := "000000";
306
         rd(5) := '1';
307
      end if;
308
   when "010001" =>   --11 COP1
309
   when "010010" =>   --12 COP2
310
   when "010011" =>   --13 COP3
311
   when "010100" =>   --14 BEQL   lbranch=r[rs]==r[rt];
312
      a_source := a_from_pc;
313
      b_source := b_from_immX4;
314
      alu_function := alu_add;
315
      pc_source := from_lbranch;
316
      branch_function := branch_eq;
317
   when "010101" =>   --15 BNEL   lbranch=r[rs]!=r[rt];
318
      a_source := a_from_pc;
319
      b_source := b_from_immX4;
320
      alu_function := alu_add;
321
      pc_source := from_lbranch;
322
      branch_function := branch_ne;
323
   when "010110" =>   --16 BLEZL  lbranch=r[rs]<=0;
324
      a_source := a_from_pc;
325
      b_source := b_from_immX4;
326
      alu_function := alu_add;
327
      pc_source := from_lbranch;
328
      branch_function := branch_lez;
329
   when "010111" =>   --17 BGTZL  lbranch=r[rs]>0;
330
      a_source := a_from_pc;
331
      b_source := b_from_immX4;
332
      alu_function := alu_add;
333
      pc_source := from_lbranch;
334
      branch_function := branch_gtz;
335
   when "100000" =>   --20 LB     r[rt]=*(signed char*)ptr;
336
          a_source := a_from_reg_source;
337
          b_source := b_from_imm;
338
          alu_function := alu_add;
339
          rd := rt;
340
      c_source := c_from_memory;
341
          mem_source := mem_read8s;    --address=(short)imm+r[rs];
342
   when "100001" =>   --21 LH     r[rt]=*(signed short*)ptr;
343
          a_source := a_from_reg_source;
344
          b_source := b_from_imm;
345
          alu_function := alu_add;
346
          rd := rt;
347
      c_source := c_from_memory;
348
          mem_source := mem_read16s;   --address=(short)imm+r[rs];
349
   when "100010" =>   --22 LWL    //fixme
350
   when "100011" =>   --23 LW     r[rt]=*(long*)ptr;
351
          a_source := a_from_reg_source;
352
          b_source := b_from_imm;
353
          alu_function := alu_add;
354
          rd := rt;
355
      c_source := c_from_memory;
356
          mem_source := mem_read32;
357
   when "100100" =>   --24 LBU    r[rt]=*(unsigned char*)ptr;
358
          a_source := a_from_reg_source;
359
          b_source := b_from_imm;
360
          alu_function := alu_add;
361
          rd := rt;
362
      c_source := c_from_memory;
363
          mem_source := mem_read8;    --address=(short)imm+r[rs];
364
   when "100101" =>   --25 LHU    r[rt]=*(unsigned short*)ptr;
365
          a_source := a_from_reg_source;
366
          b_source := b_from_imm;
367
          alu_function := alu_add;
368
          rd := rt;
369
      c_source := c_from_memory;
370
          mem_source := mem_read16;    --address=(short)imm+r[rs];
371
   when "100110" =>   --26 LWR    //fixme
372
   when "101000" =>   --28 SB     *(char*)ptr=(char)r[rt];
373
          a_source := a_from_reg_source;
374
          b_source := b_from_imm;
375
          alu_function := alu_add;
376
          mem_source := mem_write8;   --address=(short)imm+r[rs];
377
   when "101001" =>   --29 SH     *(short*)ptr=(short)r[rt];
378
          a_source := a_from_reg_source;
379
          b_source := b_from_imm;
380
          alu_function := alu_add;
381
          mem_source := mem_write16;
382
   when "101010" =>   --2a SWL    //fixme
383
   when "101011" =>   --2b SW     *(long*)ptr=r[rt];
384
          a_source := a_from_reg_source;
385
          b_source := b_from_imm;
386
          alu_function := alu_add;
387
          mem_source := mem_write32;  --address=(short)imm+r[rs];
388
   when "101110" =>   --2e SWR    //fixme
389
   when "101111" =>   --2f CACHE
390
   when "110000" =>   --30 LL     r[rt]=*(long*)ptr;
391
   when "110001" =>   --31 LWC1 
392
   when "110010" =>   --32 LWC2 
393
   when "110011" =>   --33 LWC3 
394
   when "110101" =>   --35 LDC1 
395
   when "110110" =>   --36 LDC2 
396
   when "110111" =>   --37 LDC3 
397
   when "111000" =>   --38 SC     *(long*)ptr=r[rt]; r[rt]=1;
398
   when "111001" =>   --39 SWC1 
399
   when "111010" =>   --3a SWC2 
400
   when "111011" =>   --3b SWC3 
401
   when "111101" =>   --3d SDC1 
402
   when "111110" =>   --3e SDC2 
403
   when "111111" =>   --3f SDC3 
404
   when others =>
405
   end case;
406
 
407
   if c_source = c_from_null then
408
      rd := "000000";
409
   end if;
410
 
411
   if intr_signal = '1' then
412
      rd := "101110";  --EPC
413
      c_source := c_from_pc;
414
      rs := "111111";  --interrupt vector
415
      rt := "000000";
416
      a_source := a_from_reg_source;
417
      b_source := b_from_reg_target;
418
      alu_function := alu_or;
419
      pc_source := from_branch;
420
      branch_function := branch_yes;
421
   end if;
422
 
423
   rs_index <= rs;
424
   rt_index <= rt;
425
   rd_index <= rd;
426
   imm_out <= imm;
427
   alu_func <= alu_function;
428
   shift_func <= shift_function;
429
   mult_func <= mult_function;
430
   branch_func <= branch_function;
431
   a_source_out <= a_source;
432
   b_source_out <= b_source;
433
   c_source_out <= c_source;
434
   pc_source_out <= pc_source;
435
   mem_source_out <= mem_source;
436
 
437
end process;
438
 
439
end; --logic
440
 

powered by: WebSVN 2.1.0

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