virtual_machine.hh 2.42 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
# include "vm/mmu.hh"
Benoit Perrot's avatar
Benoit Perrot committed
33
# include "vm/cp0.hh"
Benoit Perrot's avatar
Benoit Perrot committed
34
# include "vm/cpu.hh"
35
36
37

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

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

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

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

Benoit Perrot's avatar
Benoit Perrot committed
76
      while (!cpu_.get_halt())
Benoit Perrot's avatar
Benoit Perrot committed
77
	{
Benoit Perrot's avatar
Benoit Perrot committed
78
	cpu_.step();
Benoit Perrot's avatar
Benoit Perrot committed
79
80
	  cp0_.set_count(cp0_.get_count() + 1);
	}
81
82
83
    }

  protected:
Benoit Perrot's avatar
Benoit Perrot committed
84
    /// Memory block.
Benoit Perrot's avatar
Benoit Perrot committed
85
    Memory		memory_;
Benoit Perrot's avatar
Benoit Perrot committed
86

Benoit Perrot's avatar
Benoit Perrot committed
87
    /// Memory management unit.
Benoit Perrot's avatar
Benoit Perrot committed
88
    Mmu			mmu_;
Benoit Perrot's avatar
Benoit Perrot committed
89
90
    /// Control coprocessor.
    Cp0			cp0_;
Benoit Perrot's avatar
Benoit Perrot committed
91
    /// Central processor unit.
Benoit Perrot's avatar
Benoit Perrot committed
92
    Cpu			cpu_;
93
94
95
96
97
  };

} // namespace vm

#endif // !VM_VIRTUAL_MACHINE_HH