cpu.cc 12.6 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//
// This file is part of Mipsy, a tiny MIPS simulator
// Copyright (C) 2003 Benoit Perrot <benoit@lrde.epita.fr>
//
// Mipsy is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// Mipsy is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
#include "common.hh"

#include "vm/cpu.hh"

#include "inst/all.hh"

namespace vm
{

  // --------------------------------------------------------------------------
  // Arithmetic instructions
  // --------------------------------------------------------------------------

  void
  Cpu::visit(const inst::Add& add)
  {
    register_t	a = get_register(add.get_src1 ());
    register_t	b = get_register(add.get_src2 ());

    register_t	c = a + b;
    set_register(add.get_dest (), c);
    
    // FIXME: might be accelerated by testing only the sign bit.
    if ((a < 0 && b < 0 && c > 0) ||
	(a > 0 && b > 0 && c < 0))
      {
	std::cerr << "Runtime Exception: Overflow" << std::endl;
	exit_set(exit_runtime);
      }
  }
  void
  Cpu::visit(const inst::Addi& addi)
  {
    register_t	a = get_register(addi.get_src ());
    int		b = addi.get_imm ();

    register_t	c = a + b;
    set_register(addi.get_dest (), c);
    
    // FIXME: might be accelerated by testing only the sign bit.
    if ((a < 0 && b < 0 && c > 0) ||
	(a > 0 && b > 0 && c < 0))
      {
	std::cerr << "Runtime Exception: Overflow" << std::endl;
	exit_set(exit_runtime);
      }
  }
  void
  Cpu::visit(const inst::Addu& addu)
  {
    set_register(addu.get_dest (),
Benoit Perrot's avatar
Benoit Perrot committed
70
71
		 get_register(addu.get_src1 ()) +
		 get_register(addu.get_src2 ()));
72
73
74
75
76
  }
  void
  Cpu::visit(const inst::Addiu& addiu)
  {
    set_register(addiu.get_dest (),
Benoit Perrot's avatar
Benoit Perrot committed
77
		 get_register(addiu.get_src ()) + addiu.get_imm ());
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
  }

  void
  Cpu::visit(const inst::Sub& sub)
  {
    register_t	a = get_register(sub.get_src1 ());
    register_t	b = get_register(sub.get_src2 ());

    register_t	c = a - b;
    set_register(sub.get_dest (), c);

    if ((a < b && c > 0) || (a > b && c < 0))
      {
	std::cerr << "Runtime Exception: Overflow" << std::endl;
	exit_set(exit_runtime);
      }
  }
  void
  Cpu::visit(const inst::Subu& subu)
  {
    set_register(subu.get_dest (),
Benoit Perrot's avatar
Benoit Perrot committed
99
100
		 get_register(subu.get_src1 ()) - 
		 get_register(subu.get_src2 ()));
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
  }

  void
  Cpu::visit(const inst::Sll& sll)
  {
    uregister_t	a = get_register(sll.get_src ());
    unsigned	i = sll.get_imm ();
    register_t	c = a << i;
    set_register(sll.get_dest (), c);

    // FIXME: Check overflow !
  }
  void
  Cpu::visit(const inst::Sllv& slv)
  {
    uregister_t	a = get_register(slv.get_src1 ());
    uregister_t	b = get_register(slv.get_src2 ());

    register_t	c = a << b;
    set_register(slv.get_dest (), c);

    // FIXME: Check overflow !
  }

  void
  Cpu::visit(const inst::Sra& sra)
  {
    register_t	a = get_register(sra.get_src ());
    unsigned	b = sra.get_imm ();
    register_t	c = a >> b;
    set_register(sra.get_dest (), c);

    // FIXME: Check overflow !
  }
  void
  Cpu::visit(const inst::Srav& srav)
  {
    register_t	a = get_register(srav.get_src1 ());
    register_t	b = get_register(srav.get_src2 ());

    register_t	c = a >> b;
    set_register(srav.get_dest (), c);

    // FIXME: Check overflow !
  }

  void
  Cpu::visit(const inst::Srl& srl)
  {
    uregister_t	a = get_register(srl.get_src ());
    unsigned	i = srl.get_imm ();
    uregister_t	c = a >> i;
    set_register(srl.get_dest (), c);

    // FIXME: Check overflow !
  }
  void
  Cpu::visit(const inst::Srlv& srlv)
  {
    uregister_t	a = get_register(srlv.get_src1 ());
    uregister_t	b = get_register(srlv.get_src2 ());
    uregister_t	c = a >> b;
    set_register(srlv.get_dest (), c);

    // FIXME: Check overflow !
  }


  void
  Cpu::visit(const inst::Mul& mul)
  {
    register_t	a = get_register(mul.get_src1 ());
    register_t	b = get_register(mul.get_src2 ());

    register_t	c = a * b;
    set_register(mul.get_dest (), c);

    // FIXME: Check overflow !
  }

  void
  Cpu::visit(const inst::Div& div)
  {
    register_t	a = get_register(div.get_src1 ());
    register_t	b = get_register(div.get_src2 ());

    register_t	c = a / b;
    set_lo(c);

    c = a % b;
    set_hi(c);

    // FIXME: Check overflow !
  }
  void
  Cpu::visit(const inst::Divu& divu)
  {
    uregister_t	a = get_register(divu.get_src1 ());
    uregister_t	b = get_register(divu.get_src2 ());

    set_lo(a / b);
    set_hi(a % b);
  }


  // --------------------------------------------------------------------------
  // Binary instructions
  // --------------------------------------------------------------------------

  void
  Cpu::visit(const inst::And& _and)
  {
    register_t	a = get_register(_and.get_src1 ());
    register_t	b = get_register(_and.get_src2 ());

    register_t	c = a & b;
    set_register(_and.get_dest (), c);
  }
  void
  Cpu::visit(const inst::Andi& andi)
  {
    register_t	a = get_register(andi.get_src ());
    int		b = andi.get_imm ();

    register_t	c = a & b;
    set_register(andi.get_dest (), c);
  }

  void
  Cpu::visit(const inst::Or& _or)
  {
    register_t	a = get_register(_or.get_src1 ());
    register_t	b = get_register(_or.get_src2 ());

    register_t	c = a | b;
    set_register(_or.get_dest (), c);
  }
  void
  Cpu::visit(const inst::Ori& ori)
  {
    register_t	a = get_register(ori.get_src ());
    int		b = ori.get_imm ();

    register_t	c = a | b;
    set_register(ori.get_dest (), c);
  }

  void
  Cpu::visit(const inst::Nor& nor)
  {
    register_t	a = get_register(nor.get_src1 ());
    register_t	b = get_register(nor.get_src2 ());

    register_t	c = a | b;
    set_register(nor.get_dest (), ~c);
  }

  void
  Cpu::visit(const inst::Xor& _xor)
  {
    register_t	a = get_register(_xor.get_src1 ());
    register_t	b = get_register(_xor.get_src2 ());

    register_t	c = a ^ b;
    set_register(_xor.get_dest (), c);
  }
  void
  Cpu::visit(const inst::Xori& xori)
  {
    register_t	a = get_register(xori.get_src ());
    int		b = xori.get_imm ();

    register_t	c = a ^ b;
    set_register(xori.get_dest (), c);
  }


  // --------------------------------------------------------------------------
  // Move instructions
  // --------------------------------------------------------------------------
  
  void
  Cpu::visit(const inst::Li& li)
  {
    set_register(li.get_dest (), li.get_imm ());
  }

  // Store
  void
  Cpu::visit(const inst::Sb& sb)
  {
    register_t	addr = get_register(sb.get_base ()) + sb.get_offset ();
Benoit Perrot's avatar
Benoit Perrot committed
293
    mmu.data_store_byte(addr, get_register(sb.get_src ()));
294
295
296
297
298
299
  }

  void
  Cpu::visit(const inst::Sw& sw)
  {
    register_t	addr = get_register(sw.get_base ()) + sw.get_offset ();
Benoit Perrot's avatar
Benoit Perrot committed
300
    mmu.data_store_word(addr, get_register(sw.get_src ()));
301
302
303
304
305
306
307
  }

  // Load
  void
  Cpu::visit(const inst::Lb& lb)
  {
    register_t	addr = get_register(lb.get_base ()) + lb.get_offset ();
Benoit Perrot's avatar
Benoit Perrot committed
308
    set_register(lb.get_dest (), mmu.data_load_byte(addr));
309
310
311
312
313
314
  }

  void
  Cpu::visit(const inst::Lbu& lbu)
  {
    register_t	addr = get_register(lbu.get_base ()) + lbu.get_offset ();
Benoit Perrot's avatar
Benoit Perrot committed
315
    unsigned	b = mmu.data_load_byte(addr);
316
317
318
319
320
321
322
    set_register(lbu.get_dest (), b % 256);
  }

  void
  Cpu::visit(const inst::Lw& lw)
  {
    register_t	addr = get_register(lw.get_base ()) + lw.get_offset ();
Benoit Perrot's avatar
Benoit Perrot committed
323
    set_register(lw.get_dest (), mmu.data_load_word(addr));
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
  }

  // --------------------------------------------------------------------------
  // Test instructions
  // --------------------------------------------------------------------------

  void
  Cpu::visit(const inst::Slt& slt)
  {
    if (get_register(slt.get_src1 ()) < get_register(slt.get_src2 ()))
      set_register(slt.get_dest(), 1);
    else
      set_register(slt.get_dest(), 0);
  }
  void
  Cpu::visit(const inst::Slti& slti)
  {
    if (get_register(slti.get_src ()) < slti.get_imm ())
      set_register(slti.get_dest(), 1);
    else
      set_register(slti.get_dest(), 0);
  }
  void
  Cpu::visit(const inst::Sltu& sltu)
  {
    if ((uregister_t) get_register(sltu.get_src1 ()) < 
	(uregister_t) get_register(sltu.get_src2 ()))
      set_register(sltu.get_dest(), 1);
    else
      set_register(sltu.get_dest(), 0);
  }
  void
  Cpu::visit(const inst::Sltiu& sltiu)
  {
    if ((uregister_t) get_register(sltiu.get_src ()) < 
	(uregister_t) sltiu.get_imm ())
      set_register(sltiu.get_dest(), 1);
    else
      set_register(sltiu.get_dest(), 0);
  }
  

  // --------------------------------------------------------------------------
  // Branch and jump instructions
  // --------------------------------------------------------------------------

  // Unconditional
  void
  Cpu::visit(const inst::Jmp& jmp)
  {
    set_pc(get_pc() + jmp.get_label());
  }
  void
  Cpu::visit(const inst::Jr& jr)
  {
    // Assume it is a ret
    ret();

    set_pc(get_register(jr.get_dest()));
  }

  void
  Cpu::visit(const inst::Jal& jal)
  {
Benoit Perrot's avatar
Benoit Perrot committed
388
389
390
    set_register(Cpu::ra, get_pc());
    set_pc(get_register(Cpu::ra) + jal.get_label());
    
391
392
393
394
395
396
    // Assume it is a call
    call();
  }
  void
  Cpu::visit(const inst::Jalr& jalr)
  {
Benoit Perrot's avatar
Benoit Perrot committed
397
    set_register(Cpu::ra, get_pc());
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
    set_pc(get_register(jalr.get_dest()));

    // Assume it is a call
    call();
  }



  // Equality
  void
  Cpu::visit(const inst::Beq& beq)
  {
    if (get_register(beq.get_src1 ()) == get_register(beq.get_src2 ()))
      set_pc(get_pc() + beq.get_label());
  }
  void
  Cpu::visit(const inst::Bne& bne)
  {
    if (get_register(bne.get_src1 ()) != get_register(bne.get_src2 ()))
      set_pc(get_pc() + bne.get_label());
  }

  // Greater
  void
  Cpu::visit(const inst::Bgez& bgez)
  {
    if (get_register(bgez.get_src ()) >= 0)
      set_pc(get_pc() + bgez.get_label());
  }
  void
  Cpu::visit(const inst::Bgezal& bgezal)
  {
    if (get_register(bgezal.get_src ()) >= 0)
      {
	// Assume it is a call
	call();
	
Benoit Perrot's avatar
Benoit Perrot committed
435
	set_register(Cpu::ra, get_pc());
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
	set_pc(get_pc() + bgezal.get_label());
      }
  }
  void
  Cpu::visit(const inst::Bgtz& bgtz)
  {
    if (get_register(bgtz.get_src ()) > 0)
      set_pc(get_pc() + bgtz.get_label());
  }

  // Lower
  void
  Cpu::visit(const inst::Blez& blez)
  {
    if (get_register(blez.get_src ()) <= 0)
      set_pc(get_pc() + blez.get_label());
  }
  void
  Cpu::visit(const inst::Bltz& bltz)
  {
    if (get_register(bltz.get_src ()) < 0)
      set_pc(get_pc() + bltz.get_label());
  }
  void
  Cpu::visit(const inst::Bltzal& bltzal)
  {
    if (get_register(bltzal.get_src ()) < 0)
      {
	// Assume it is a call
	call();

Benoit Perrot's avatar
Benoit Perrot committed
467
	set_register(Cpu::ra, get_pc());	
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
	set_pc(get_pc() + bltzal.get_label());
      }
  }


  // --------------------------------------------------------------------------
  // Data movement instructions
  // --------------------------------------------------------------------------

  void
  Cpu::visit(const inst::Mfhi& mfhi)
  {
    set_register(mfhi.get_dest(), get_hi());
  }
  void
  Cpu::visit(const inst::Mflo& mflo)
  {
    set_register(mflo.get_dest(), get_lo());
  }
  void
  Cpu::visit(const inst::Mthi& mthi)
  {
    set_hi(get_register(mthi.get_src()));
  }
  void
  Cpu::visit(const inst::Mtlo& mtlo)
  {
    set_lo(get_register(mtlo.get_src()));
  }


  // --------------------------------------------------------------------------
  // Exception and trap instructions
  // --------------------------------------------------------------------------

  void
  Cpu::visit(const inst::Syscall&)
  {
Benoit Perrot's avatar
Benoit Perrot committed
506
    switch (get_register(Cpu::v0))
507
508
509
      {
	// print_int (integer: $a0)
      case 1:
Benoit Perrot's avatar
Benoit Perrot committed
510
	ostr << get_register(Cpu::a0);
511
512
513
514
	break;

	// print_string (buffer: $a0)
      case 4:
Benoit Perrot's avatar
Benoit Perrot committed
515
	for (int i = get_register(Cpu::a0); true; ++i)
516
	  {
Benoit Perrot's avatar
Benoit Perrot committed
517
	    char b = mmu.data_load_byte(i);
518
519
520
521
522
523
524
525
526
527
528
	    if (b == 0)
	      break;
	    ostr << b;
	  }
	break;
	
	// read_string (buffer: $a0, length: $a1)
      case 8:
	{
	  int i = 0;
	  int c = 0;
Benoit Perrot's avatar
Benoit Perrot committed
529
530
	  for (; (i < get_register(Cpu::a1) - 1) && (c != '\n') && (c != '\r');
	       ++i)
531
532
533
534
	    {
	      c = istr.get();
	      if (istr.eof())
		break;
Benoit Perrot's avatar
Benoit Perrot committed
535
	      mmu.data_store_byte(get_register(Cpu::a0) + i, c);
536
	    }
Benoit Perrot's avatar
Benoit Perrot committed
537
	  mmu.data_store_byte(get_register(Cpu::a0) + i, 0);
538
539
540
541
542
	}
	break;

	// sbrk (size: $a0)
      case 9: 
Benoit Perrot's avatar
Benoit Perrot committed
543
	set_register(Cpu::v0, mmu.data_sbrk(get_register(Cpu::a0)));
544
545
546
547
548
549
	break;

	// exit (status : $a0)
      case 10:
	halt = true;
	if (!exit_status)
Benoit Perrot's avatar
Benoit Perrot committed
550
	  exit_status = (exit_t) get_register(Cpu::a0);
551
552
553
554
555
556
557
558
559
	break;
	
      default:
	assertion(!"syscall: Not implemented yet");
      };
    ostr.flush();
  }

} // namespace vm