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

// FIXME: lack of compatibility
# include <stdint.h>

25
26
27
# include <stack>

# include "inst/visitor.hh"
28
# include "inst/register.hh"
29

30
31
32
33
34
35
36
37
38
# include "vm/memory.hh"
# include "vm/table.hh"

namespace vm
{

  typedef int32_t	register_t;
  typedef uint32_t	uregister_t;
  
39
40
  class Cpu:
    public inst::ConstVisitor // FIXME: might be protected
41
  {
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
  public:
    enum kind_t
      {
	zero = 0,
	at,
	v0, v1,
	a0, a1, a2, a3,
	t0, t1, t2, t3, t4, t5, t6, t7,
	s0, s1, s2, s3, s4, s5, s6, s7,
	t8, t9,
	k0, k1,
	gp,
	sp,
	fp,
	ra = 31
      };

59
  public:
60
61
62
63
64
65
66
    Cpu(Memory& mem, 
	std::istream& istr, 
	std::ostream& ostr, 
	bool check_callee_save_p):
      memory(mem),
      istr(istr), ostr(ostr),
      check_callee_save_p(check_callee_save_p)
67
68
69
70
71
72
73
    {
      reset();
    }

  public:
    void		reset()
    {
74
75
76
      halt = false;

      // Initialize general purpose registers to 0.
77
78
      for (unsigned i = 0; i < 32; ++i)
	GPR[i] = 0;
79
      // Initialize stack pointers to bottom of stack.
80
81
      GPR[sp] = Memory::stack_bottom;
      GPR[fp] = Memory::stack_bottom;
82
      // Initialize special registers to 0.
83
84
      lo = 0;
      hi = 0;
85
86
87
    }

  public:
88
89
    register_t		get_at() const { return GPR[at]; }
    void		set_at(register_t r) { GPR[at] = r; }
90

91
92
    register_t		get_v0() const { return GPR[v0]; }
    void		set_v0(register_t r) { GPR[v0] = r; }
93

94
95
    register_t		get_a0() const { return GPR[a0]; }
    register_t		get_a1() const { return GPR[a1]; }
96

97
98
    register_t		get_sp() const { return GPR[sp]; }
    void		set_sp(register_t r) { GPR[sp] = r; }
99

100
101
    register_t		get_fp() const { return GPR[fp]; }
    void		set_fp(register_t r) { GPR[fp] = r; }
102

103
104
    register_t		get_ra() const { return GPR[ra]; }
    void		set_ra(register_t r) { GPR[ra] = r; }
105

106
    register_t		get_register(kind_t k) const
107
108
109
    {
      return GPR[k];
    }
110
    void		set_register(kind_t k, register_t r)
111
112
113
    {
      GPR[k] = r;
    }
114

115
116
117
118
    bool		has_unlimited(int i) const
    {
      return unlimited.has(i);
    }
119
120
    register_t		get_unlimited(int i) const
    {
121
      precondition(has_unlimited(i));
122
123
124
125
      return unlimited.get(i);
    }
    void		set_unlimited(int i, register_t r)
    {
126
      unlimited.put(i, r);
127
128
129
130
    }

    register_t		get_register(const inst::Register& reg) const
    {
131
132
133
134
135
136
137
      if (reg.get_kind() == inst::Register::unlimited)
	{
	  precondition(has_unlimited(reg.get_index ()));
	  return unlimited.get(reg.get_index ());
	}
      precondition(reg.get_kind() == inst::Register::general);
      return GPR[reg.get_index()];
138
139
140
    }
    void		set_register(const inst::Register& reg, register_t r)
    {
141
      if (reg.get_kind() == inst::Register::unlimited)
142
143
	{
	  unlimited.put(reg.get_index (), r);
144
	  return;
145
	}
146
147
148
      precondition(reg.get_kind() == inst::Register::general);
      if (reg.get_index() != 0)
	GPR[reg.get_index()] = r;
149
150
    }

151
152
153
154
155
156
157
158
159
160
  public:
    register_t		get_hi() const { return hi; }
    void		set_hi(register_t r) { hi = r; }

    register_t		get_lo() const { return lo; }
    void		set_lo(register_t r) { lo = r; }

    register_t		get_pc() const { return pc; }
    void		set_pc(register_t r) { pc = r; }

161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
  protected:
    virtual void	visit(const inst::Add& add);
    virtual void	visit(const inst::Addi& addi);
    virtual void	visit(const inst::Addu& addu);
    virtual void	visit(const inst::Addiu& addiu);
    virtual void	visit(const inst::Sub& sub);
    virtual void	visit(const inst::Subu& subu);

    virtual void	visit(const inst::Sll& sll);
    virtual void	visit(const inst::Sllv& slv);
    virtual void	visit(const inst::Sra& sra);
    virtual void	visit(const inst::Srav& srav);
    virtual void	visit(const inst::Srl& srl);
    virtual void	visit(const inst::Srlv& srlv);

    virtual void	visit(const inst::Mul& mul);
    virtual void	visit(const inst::Div& div);
    virtual void	visit(const inst::Divu& divu);

    virtual void	visit(const inst::Lb& lb);
    virtual void	visit(const inst::Lbu& Lbu);
    virtual void	visit(const inst::Lw& lw);
    virtual void	visit(const inst::Li& li);
    virtual void	visit(const inst::Sb& sb);
    virtual void	visit(const inst::Sw& sw);

    virtual void	visit(const inst::And& _and);
    virtual void	visit(const inst::Andi& andi);
    virtual void	visit(const inst::Or& _or);
    virtual void	visit(const inst::Ori& ori);
    virtual void	visit(const inst::Nor& nor);
    virtual void	visit(const inst::Xor& _xor);
    virtual void	visit(const inst::Xori& xori);

    virtual void	visit(const inst::Slt& slt);
    virtual void	visit(const inst::Slti& slti);
    virtual void	visit(const inst::Sltu&);
    virtual void	visit(const inst::Sltiu& sltiu);

    virtual void	visit(const inst::Jmp& jmp);
    virtual void	visit(const inst::Jr& jr);
    virtual void	visit(const inst::Jal& jal);
    virtual void	visit(const inst::Jalr& jalr);

    virtual void	visit(const inst::Beq& beq);
    virtual void	visit(const inst::Bne& bne);

    virtual void	visit(const inst::Bgez& bgez);
    virtual void	visit(const inst::Bgezal& bgezal);
    virtual void	visit(const inst::Bgtz& bgtz);

    virtual void	visit(const inst::Blez& blez);
    virtual void	visit(const inst::Bltz& bltz);
    virtual void	visit(const inst::Bltzal& bltzal);

    virtual void	visit(const inst::Mfhi& mfhi);
    virtual void	visit(const inst::Mflo& mflo);
    virtual void	visit(const inst::Mthi& mthi);
    virtual void	visit(const inst::Mtlo& mtlo);

    virtual void	visit(const inst::Syscall& sycall);

  protected:
    // FIXME: Should use an MMU
    Memory&			memory;

    register_t			GPR[32];
    register_t			hi, lo;
    register_t			pc;

    Table<int, register_t>	unlimited;

233

234
  public:
235
236
237
238
239
240
241
242
243
244
    bool		get_halt() const { return halt; }
  protected:
    bool		halt;

    std::istream&	istr;
    std::ostream&	ostr;


  protected:
    void		call()
245
    {
246
247
248
249
250
      if (check_callee_save_p)
	for (int i = Cpu::s0; i <= Cpu::s7; ++i)
	  set_unlimited(-i, get_register((Cpu::kind_t) i));
      call_stack.push(get_pc ());

251
252
      unlimited.begin_scope();
    }
253
    void		ret()
254
255
256
    {
      unlimited.end_scope();

257
258
259
260
261
262
263
264
265
266
267
268
269
      if (check_callee_save_p)
	for (int i = Cpu::s0; i <= Cpu::s7; ++i)
	  if (get_unlimited(-i) != get_register((Cpu::kind_t) i))
	    {
	      std::cerr
		<< "Warning: callee save register `$s" << i - Cpu::s0
		<< "' was not preserved across last call to 0x"
		<< std::hex << call_stack.top()	<< std::dec << std::endl;
	      set_register((Cpu::kind_t) i, get_unlimited(-i));
	      exit_set(exit_runtime);
	    }
      call_stack.pop();
    }
270
  protected:
271
272
    std::stack<register_t>	call_stack;
    bool		check_callee_save_p;
273
274
275
276
277
  };

} // namespace vm

#endif // !VM_CPU_HH