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

Subversion Repositories mlite

[/] [mlite/] [trunk/] [vhdl/] [control.vhd] - Blame information for rev 44

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

powered by: WebSVN 2.1.0

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