cpu.hh 7.06 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"
Benoit Perrot's avatar
Benoit Perrot committed
29
# include "inst/program.hh"
30

Benoit Perrot's avatar
Benoit Perrot committed
31
# include "vm/mmu.hh"
32
33
34
35
36
37
38
39
# include "vm/table.hh"

namespace vm
{

  typedef int32_t	register_t;
  typedef uint32_t	uregister_t;
  
40
  class Cpu:
Benoit Perrot's avatar
Benoit Perrot committed
41
    protected inst::ConstVisitor
42
  {
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
  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
      };

60
  public:
Benoit Perrot's avatar
Benoit Perrot committed
61
    Cpu(Mmu& mmu, 
62
63
	std::istream& istr, 
	std::ostream& ostr, 
Benoit Perrot's avatar
Benoit Perrot committed
64
65
	bool check_callee_save_p, 
	bool trace_p):
Benoit Perrot's avatar
Benoit Perrot committed
66
      mmu(mmu),
67
      istr(istr), ostr(ostr),
Benoit Perrot's avatar
Benoit Perrot committed
68
69
      check_callee_save_p(check_callee_save_p),
      trace_p(trace_p)
70
71
72
73
74
75
76
    {
      reset();
    }

  public:
    void		reset()
    {
77
78
79
      halt = false;

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

  public:
91
    register_t		get_register(kind_t k) const
92
93
94
    {
      return GPR[k];
    }
95
    void		set_register(kind_t k, register_t r)
96
    {
Benoit Perrot's avatar
Benoit Perrot committed
97
98
      if (k != Cpu::zero)
	GPR[k] = r;
99
    }
100

101
102
103
104
    bool		has_unlimited(int i) const
    {
      return unlimited.has(i);
    }
105
106
    register_t		get_unlimited(int i) const
    {
107
      precondition(has_unlimited(i));
108
109
110
111
      return unlimited.get(i);
    }
    void		set_unlimited(int i, register_t r)
    {
112
      unlimited.put(i, r);
113
114
115
116
    }

    register_t		get_register(const inst::Register& reg) const
    {
117
118
119
120
121
      if (reg.get_kind() == inst::Register::unlimited)
	{
	  precondition(has_unlimited(reg.get_index ()));
	  return unlimited.get(reg.get_index ());
	}
Benoit Perrot's avatar
Benoit Perrot committed
122
123
124
      precondition(reg.get_kind() == inst::Register::general &&
		   Cpu::zero <= reg.get_index() && reg.get_index() <= Cpu::ra);
      return get_register((kind_t) reg.get_index());
125
126
127
    }
    void		set_register(const inst::Register& reg, register_t r)
    {
128
      if (reg.get_kind() == inst::Register::unlimited)
129
130
	{
	  unlimited.put(reg.get_index (), r);
131
	  return;
132
	}
Benoit Perrot's avatar
Benoit Perrot committed
133
134
135
      precondition(reg.get_kind() == inst::Register::general &&
		   Cpu::zero <= reg.get_index() && reg.get_index() <= Cpu::ra);
      set_register((kind_t) reg.get_index(), r);
136
137
    }

138
139
140
141
142
143
144
145
146
    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; }

147
148
149
150
151
152
153
154
155
156
157
158
159
160
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
  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:
Benoit Perrot's avatar
Benoit Perrot committed
210
    Mmu&			mmu;
211
212
213
214
215
216
217

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

    Table<int, register_t>	unlimited;

218

219
  public:
220
221
222
223
224
225
226
227
228
229
    bool		get_halt() const { return halt; }
  protected:
    bool		halt;

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


  protected:
    void		call()
230
    {
231
232
233
234
235
      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 ());

236
237
      unlimited.begin_scope();
    }
238
    void		ret()
239
240
241
    {
      unlimited.end_scope();

242
243
244
245
246
247
248
249
250
251
252
253
254
      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();
    }
255
  protected:
256
257
    std::stack<register_t>	call_stack;
    bool		check_callee_save_p;
Benoit Perrot's avatar
Benoit Perrot committed
258
259
260
261

  public:
    void		step()
    {
Benoit Perrot's avatar
Benoit Perrot committed
262
      const inst::Inst& ri = mmu.inst()[pc / 4];
Benoit Perrot's avatar
Benoit Perrot committed
263
264
265
266
267
268
269
270
      pc = pc + 4;
      
      if (trace_p)
	std::cout << ri << std::endl;
      ri.accept(*this);
    }
  protected:
    bool		trace_p;
271
272
273
274
275
  };

} // namespace vm

#endif // !VM_CPU_HH