cpu.hh 6.85 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
    register_t		get_register(kind_t k) const
89
90
91
    {
      return GPR[k];
    }
92
    void		set_register(kind_t k, register_t r)
93
    {
Benoit Perrot's avatar
Benoit Perrot committed
94
95
      if (k != Cpu::zero)
	GPR[k] = r;
96
    }
97

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

    register_t		get_register(const inst::Register& reg) const
    {
114
115
116
117
118
      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
119
120
121
      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());
122
123
124
    }
    void		set_register(const inst::Register& reg, register_t r)
    {
125
      if (reg.get_kind() == inst::Register::unlimited)
126
127
	{
	  unlimited.put(reg.get_index (), r);
128
	  return;
129
	}
Benoit Perrot's avatar
Benoit Perrot committed
130
131
132
      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);
133
134
    }

135
136
137
138
139
140
141
142
143
    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; }

144
145
146
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
210
211
212
213
214
215
  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;

216

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

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


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

234
235
      unlimited.begin_scope();
    }
236
    void		ret()
237
238
239
    {
      unlimited.end_scope();

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

} // namespace vm

#endif // !VM_CPU_HH