virtual_machine.hh 3.82 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:
Benoit Perrot's avatar
Benoit Perrot committed
61
    VirtualMachine(bool check_callee_save_p,
Benoit Perrot's avatar
Benoit Perrot committed
62
		   bool trace_exec_p,
63
		   mode_type mode = normal,
Benoit Perrot's avatar
Benoit Perrot committed
64
		   std::istream& istr = std::cin,
65
		   std::ostream& ostr = std::cout):
66
67
      mode_(mode),
      status_(stop),
Benoit Perrot's avatar
Benoit Perrot committed
68
      mmu_(cp0_, memory_),
Benoit Perrot's avatar
Benoit Perrot committed
69
      cpu_(mmu_, cp0_, istr, ostr, check_callee_save_p, trace_exec_p)
70
71
    {
    }
Benoit Perrot's avatar
Benoit Perrot committed
72
    /** \} */
73

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

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


86
  public:
Benoit Perrot's avatar
Benoit Perrot committed
87
    /// Load a program into memory.
Benoit Perrot's avatar
Benoit Perrot committed
88
    void	load_program(const inst::Program& program);
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
100
101
    /** \name Shell entry point
	\{ */
  protected:
    /// Check if a label exists
    bool	has_label(const std::string& label) const
    {
Benoit Perrot's avatar
Benoit Perrot committed
102
      return mmu_.inst_has_label(label);
103
104
105
106
107
    }

    /// Return the offset of a label
    int		get_offset(const std::string& label) const
    {
Benoit Perrot's avatar
Benoit Perrot committed
108
      return mmu_.inst_get_offset(label);
109
110
111
112
    }

  public:
    /// Execute a program
Benoit Perrot's avatar
Benoit Perrot committed
113
    void	execute(bool trace_p);
114
115

    /// Add a breakpoint
Benoit Perrot's avatar
Benoit Perrot committed
116
    void	add_breakpoint(const std::string& label);
117
118
119
120
121
122
123
124

  protected:
    /// The list of breakpoints
    std::list<int> breakpoints_;


  public:
    /// Make the vm runnable
Benoit Perrot's avatar
Benoit Perrot committed
125
    void	set_runnable()
126
127
128
129
130
131
    {
      status_ = run;
      mode_ = normal;
    }

    /// Run only the next instruction
Benoit Perrot's avatar
Benoit Perrot committed
132
    void	set_run_next()
133
134
135
136
137
138
    {
      status_ = run;
      mode_ = step;
    }

    /// Return the status of the vm
Benoit Perrot's avatar
Benoit Perrot committed
139
    status_type	get_status() const
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
    {
      return status_;
    }


    /// Return the value of a register
    register_type	get_cpu_register(Cpu::kind_type k) const
    {
      return cpu_.get_register(k);
    }

    /// Return the value of the pc
    register_type	get_cpu_pc() const
    {
      return cpu_.get_pc();
    }

  protected:
    /// The virtual machine mode.
    mode_type	mode_;
    /// The virtual machine status.
    status_type	status_;
    /** \} */

164
  protected:
Benoit Perrot's avatar
Benoit Perrot committed
165
    /// Memory block.
Benoit Perrot's avatar
Benoit Perrot committed
166
    Memory		memory_;
Benoit Perrot's avatar
Benoit Perrot committed
167

Benoit Perrot's avatar
Benoit Perrot committed
168
    /// Memory management unit.
Benoit Perrot's avatar
Benoit Perrot committed
169
    Mmu			mmu_;
Benoit Perrot's avatar
Benoit Perrot committed
170
171
    /// Control coprocessor.
    Cp0			cp0_;
Benoit Perrot's avatar
Benoit Perrot committed
172
    /// Central processor unit.
Benoit Perrot's avatar
Benoit Perrot committed
173
    Cpu			cpu_;
174
175
  };

176
177
178
179
180
181
182
  inline std::ostream&
  operator<<(std::ostream &os, const VirtualMachine &vm)
  {
    vm.print(os);
    return os;
  }

183
184
185
} // namespace vm

#endif // !VM_VIRTUAL_MACHINE_HH