virtual_machine.hh 3.44 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
25
# include <iostream>

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

# include "inst/program.hh"

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

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

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

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

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

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

76
    /// Print the vm
Benoit Perrot's avatar
Benoit Perrot committed
77
    void	print(std::ostream& ostr) const
78
79
80
81
82
83
    {
      // FIXME: print the whole cpu, and cp0.
      ostr << cpu_;
    }


84
  public:
Benoit Perrot's avatar
Benoit Perrot committed
85
    /// Load a program into memory.
Benoit Perrot's avatar
Benoit Perrot committed
86
    void	load_program(const inst::Program& program);
Benoit Perrot's avatar
Benoit Perrot committed
87
88
  protected:
    int main_offset_; // FIXME: Must disappear!
89

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

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

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

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

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


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

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

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

135
    const Cp0	&get_cp0() const { return cp0_; }
136

137
    const Cpu	&get_cpu() const { return cpu_; }
138
    Cpu	&get_cpu() { return cpu_; }
139

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

146
147
    /** \} */

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

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

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

167
168
169
} // namespace vm

#endif // !VM_VIRTUAL_MACHINE_HH