virtual_machine.hh 1.98 KB
Newer Older
Benoit Perrot's avatar
Benoit Perrot committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//
// 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
//
19
20
21
#ifndef VM_VIRTUAL_MACHINE_HH
# define VM_VIRTUAL_MACHINE_HH

22
23
# include <iostream>

24
# include "misc/contract.hh"
25
# include "common.hh"
26
27
28
29

# include "inst/program.hh"

# include "vm/memory.hh"
Benoit Perrot's avatar
Benoit Perrot committed
30
31
# include "vm/mmu.hh"
# include "vm/cpu.hh"
32
33
34
35

namespace vm
{
  
36
  class VirtualMachine
37
38
  {
  public:
Benoit Perrot's avatar
Benoit Perrot committed
39
    VirtualMachine(bool check_callee_save_p,
Benoit Perrot's avatar
Benoit Perrot committed
40
		   bool trace_exec_p,
Benoit Perrot's avatar
Benoit Perrot committed
41
		   std::istream& istr = std::cin,
42
		   std::ostream& ostr = std::cout):
Benoit Perrot's avatar
Benoit Perrot committed
43
44
      mmu(memory),
      cpu(mmu, istr, ostr, check_callee_save_p, trace_exec_p)
45
46
47
48
49
    {
    }


  public:
50
    void		load_program(const inst::Program& program)
51
    {
52
      if (! program.text_section ().has_label(inst::Label("main")))
Benoit Perrot's avatar
Benoit Perrot committed
53
54
	{
	  std::cerr << "No `main' label in assembly file." << std::endl;
55
	  exit_set(exit_runtime);
Benoit Perrot's avatar
Benoit Perrot committed
56
57
	  return;
	}
Benoit Perrot's avatar
Benoit Perrot committed
58
59
      mmu.data_store(program.data_section());
      mmu.inst_store(program.text_section());
Benoit Perrot's avatar
Benoit Perrot committed
60
      cpu.set_pc(program.text_section ().get_offset(inst::Label("main")));
61
    }
62

63
  public:
Benoit Perrot's avatar
Benoit Perrot committed
64
    void		execute()
65
    {
Benoit Perrot's avatar
Benoit Perrot committed
66
67
      // FIXME: precondition on loaded program

Benoit Perrot's avatar
Benoit Perrot committed
68
69
      while (!cpu.get_halt())
	cpu.step();
70
71
72
    }

  protected:
73
    Memory		memory;
Benoit Perrot's avatar
Benoit Perrot committed
74
75
    Mmu			mmu;
    Cpu			cpu;
76
77
78
79
80
  };

} // namespace vm

#endif // !VM_VIRTUAL_MACHINE_HH