virtual_machine.hh 3.47 KB
Newer Older
Benoit Perrot's avatar
Benoit Perrot committed
1
//
Benoit Perrot's avatar
Benoit Perrot committed
2
// This file is part of Nolimips, a MIPS simulator with unlimited registers
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
//
Benoit Perrot's avatar
Benoit Perrot committed
5
// Nolimips is free software; you can redistribute it and/or modify
Benoit Perrot's avatar
Benoit Perrot committed
6
7
8
9
// 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.
// 
Benoit Perrot's avatar
Benoit Perrot committed
10
// Nolimips is distributed in the hope that it will be useful,
Benoit Perrot's avatar
Benoit Perrot committed
11
12
13
14
15
16
17
18
// 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
# include <ostream>
25

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

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

35
36
namespace inst { class Program; }

37
38
namespace vm
{
39
  class SystemLibrary;
Benoit Perrot's avatar
Benoit Perrot committed
40
41

  /// Virtual Machine abstraction
42
  class VirtualMachine
43
  {
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
  public:
    enum mode_type
      {
	normal,
	step
      };

    enum status_type
      {
	run,
	pause,
	stop,
	halt
      };

Benoit Perrot's avatar
Benoit Perrot committed
59
60
    /** \name Constructor and destructor.
	\{ */
61
  public:
62
    VirtualMachine(mode_type mode = normal):
63
64
      mode_(mode),
      status_(stop),
Benoit Perrot's avatar
Benoit Perrot committed
65
      mmu_(cp0_, memory_),
66
      cpu_(mmu_, cp0_)
67
68
    {
    }
Benoit Perrot's avatar
Benoit Perrot committed
69
    /** \} */
70

71
72
  public:
    /// Reset the vm
Benoit Perrot's avatar
Benoit Perrot committed
73
74
    void	reset();

75
    /// Print the vm
76
    void	write(std::ostream& ostr) const
77
78
79
80
81
82
    {
      // FIXME: print the whole cpu, and cp0.
      ostr << cpu_;
    }


83
  public:
Benoit Perrot's avatar
Benoit Perrot committed
84
    /// Load a program into memory.
85
    void load_program(const inst::Program &program);
86
87
88

    void set_system_library(SystemLibrary *l) { cpu_.set_system_library(l); }

Benoit Perrot's avatar
Benoit Perrot committed
89
90
  protected:
    int main_offset_; // FIXME: Must disappear!
91

92
  public:
Benoit Perrot's avatar
Benoit Perrot committed
93
    /// Execute a program.
Benoit Perrot's avatar
Benoit Perrot committed
94
    void	execute();
95

96
97
98
99
    /** \name Shell entry point
	\{ */
  public:
    /// Execute a program
Benoit Perrot's avatar
Benoit Perrot committed
100
    void	execute(bool trace_p);
101
102

    /// Add a breakpoint
Benoit Perrot's avatar
Benoit Perrot committed
103
    void	add_breakpoint(int offset);
104

Benoit Perrot's avatar
Benoit Perrot committed
105
106
107
108
109
110
    /// Return CPU's call stack
    const std::vector<register_type> &	get_call_stack() const
    {
      return cpu_.get_call_stack();
    }

111
112
113
114
115
116
117
  protected:
    /// The list of breakpoints
    std::list<int> breakpoints_;


  public:
    /// Make the vm runnable
Benoit Perrot's avatar
Benoit Perrot committed
118
    void	set_runnable()
119
120
121
122
123
124
    {
      status_ = run;
      mode_ = normal;
    }

    /// Run only the next instruction
Benoit Perrot's avatar
Benoit Perrot committed
125
    void	set_run_next()
126
127
128
129
130
131
    {
      status_ = run;
      mode_ = step;
    }

    /// Return the status of the vm
Benoit Perrot's avatar
Benoit Perrot committed
132
    status_type	get_status() const
133
134
135
136
    {
      return status_;
    }

137
    const Cp0	&get_cp0() const { return cp0_; }
138

139
    const Cpu	&get_cpu() const { return cpu_; }
140
    Cpu	&get_cpu() { return cpu_; }
141

142
143
144
145
146
  protected:
    /// The virtual machine mode.
    mode_type	mode_;
    /// The virtual machine status.
    status_type	status_;
147

148
149
    /** \} */

150
  protected:
Benoit Perrot's avatar
Benoit Perrot committed
151
    /// Memory block.
Benoit Perrot's avatar
Benoit Perrot committed
152
    Memory		memory_;
Benoit Perrot's avatar
Benoit Perrot committed
153

Benoit Perrot's avatar
Benoit Perrot committed
154
    /// Memory management unit.
Benoit Perrot's avatar
Benoit Perrot committed
155
    Mmu			mmu_;
Benoit Perrot's avatar
Benoit Perrot committed
156
157
    /// Control coprocessor.
    Cp0			cp0_;
Benoit Perrot's avatar
Benoit Perrot committed
158
    /// Central processor unit.
Benoit Perrot's avatar
Benoit Perrot committed
159
    Cpu			cpu_;
160
161
  };

162
163
164
  inline std::ostream&
  operator<<(std::ostream &os, const VirtualMachine &vm)
  {
165
    vm.write(os);
166
167
168
    return os;
  }

169
170
171
} // namespace vm

#endif // !VM_VIRTUAL_MACHINE_HH