virtual_machine.hh 2.29 KB
Newer Older
Benoit Perrot's avatar
Benoit Perrot committed
1
2
//
// This file is part of Mipsy, a tiny MIPS simulator
Benoit Perrot's avatar
Benoit Perrot committed
3
// Copyright (C) 2003, 2004 Benoit Perrot <benoit@lrde.epita.fr>
Benoit Perrot's avatar
Benoit Perrot committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//
// 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
//
Benoit Perrot's avatar
Benoit Perrot committed
19
20
/** \file vm/virtual_machine.hh
    \brief Declare Virtual Machine class. */
21
22
23
#ifndef VM_VIRTUAL_MACHINE_HH
# define VM_VIRTUAL_MACHINE_HH

24
25
# include <iostream>

26
# include "misc/contract.hh"
27
# include "common.hh"
28
29
30
31

# include "inst/program.hh"

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

namespace vm
{
Benoit Perrot's avatar
Benoit Perrot committed
37
38

  /// Virtual Machine abstraction
39
  class VirtualMachine
40
  {
Benoit Perrot's avatar
Benoit Perrot committed
41
42
    /** \name Constructor and destructor.
	\{ */
43
  public:
Benoit Perrot's avatar
Benoit Perrot committed
44
    VirtualMachine(bool check_callee_save_p,
Benoit Perrot's avatar
Benoit Perrot committed
45
		   bool trace_exec_p,
Benoit Perrot's avatar
Benoit Perrot committed
46
		   std::istream& istr = std::cin,
47
		   std::ostream& ostr = std::cout):
Benoit Perrot's avatar
Benoit Perrot committed
48
49
      mmu(memory),
      cpu(mmu, istr, ostr, check_callee_save_p, trace_exec_p)
50
51
    {
    }
Benoit Perrot's avatar
Benoit Perrot committed
52
    /** \} */
53
54

  public:
Benoit Perrot's avatar
Benoit Perrot committed
55
    /// Load a program into memory.
56
    void		load_program(const inst::Program& program)
57
    {
58
      if (! program.text_section ().has_label(inst::Label("main")))
Benoit Perrot's avatar
Benoit Perrot committed
59
60
	{
	  std::cerr << "No `main' label in assembly file." << std::endl;
61
	  exit_set(exit_runtime);
Benoit Perrot's avatar
Benoit Perrot committed
62
63
	  return;
	}
Benoit Perrot's avatar
Benoit Perrot committed
64
65
      mmu.data_store(program.data_section());
      mmu.inst_store(program.text_section());
Benoit Perrot's avatar
Benoit Perrot committed
66
      cpu.set_pc(program.text_section ().get_offset(inst::Label("main")));
67
    }
68

69
  public:
Benoit Perrot's avatar
Benoit Perrot committed
70
    /// Execute a program.
Benoit Perrot's avatar
Benoit Perrot committed
71
    void		execute()
72
    {
Benoit Perrot's avatar
Benoit Perrot committed
73
74
      // FIXME: precondition on loaded program

Benoit Perrot's avatar
Benoit Perrot committed
75
76
      while (!cpu.get_halt())
	cpu.step();
77
78
79
    }

  protected:
Benoit Perrot's avatar
Benoit Perrot committed
80
    /// Memory block.
81
    Memory		memory;
Benoit Perrot's avatar
Benoit Perrot committed
82
    /// Memory management unit.
Benoit Perrot's avatar
Benoit Perrot committed
83
    Mmu			mmu;
Benoit Perrot's avatar
Benoit Perrot committed
84
    /// Central processor unit.
Benoit Perrot's avatar
Benoit Perrot committed
85
    Cpu			cpu;
86
87
88
89
90
  };

} // namespace vm

#endif // !VM_VIRTUAL_MACHINE_HH