cpu.hh 3.73 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
25
26
27
28
29
30
31
32
33
34
35
36
#ifndef VM_CPU_HH
# define VM_CPU_HH

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

# include "inst/register.hh"
# include "vm/memory.hh"
# include "vm/table.hh"

namespace vm
{

  typedef int32_t	register_t;
  typedef uint32_t	uregister_t;
  
  class Cpu
  {
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
  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
      };

54
55
56
57
58
59
60
61
62
  public:
    Cpu()
    {
      reset();
    }

  public:
    void		reset()
    {
63
64
      for (unsigned i = 0; i < 32; ++i)
	GPR[i] = 0;
65
66
      GPR[sp] = Memory::stack_bottom;
      GPR[fp] = Memory::stack_bottom;
67
68
69

      lo = 0;
      hi = 0;
70
71
72
    }

  public:
73
74
    register_t		get_at() const { return GPR[at]; }
    void		set_at(register_t r) { GPR[at] = r; }
75

76
77
    register_t		get_v0() const { return GPR[v0]; }
    void		set_v0(register_t r) { GPR[v0] = r; }
78

79
80
    register_t		get_a0() const { return GPR[a0]; }
    register_t		get_a1() const { return GPR[a1]; }
81

82
83
    register_t		get_sp() const { return GPR[sp]; }
    void		set_sp(register_t r) { GPR[sp] = r; }
84

85
86
    register_t		get_fp() const { return GPR[fp]; }
    void		set_fp(register_t r) { GPR[fp] = r; }
87

88
89
    register_t		get_ra() const { return GPR[ra]; }
    void		set_ra(register_t r) { GPR[ra] = r; }
90

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
97
98
    {
      GPR[k] = r;
    }
99

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

    register_t		get_register(const inst::Register& reg) const
    {
116
117
118
119
120
121
122
      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()];
123
124
125
    }
    void		set_register(const inst::Register& reg, register_t r)
    {
126
      if (reg.get_kind() == inst::Register::unlimited)
127
128
	{
	  unlimited.put(reg.get_index (), r);
129
	  return;
130
	}
131
132
133
      precondition(reg.get_kind() == inst::Register::general);
      if (reg.get_index() != 0)
	GPR[reg.get_index()] = r;
134
135
    }

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


147
148
149
150
151
152
153
154
155
156
157
  public:
    void			begin_scope()
    {
      unlimited.begin_scope();
    }
    void			end_scope()
    {
      unlimited.end_scope();
    }

  protected:
158
159
    register_t			GPR[32];

160
    Table<int, register_t>	unlimited;
161
162
163

    register_t			hi, lo;
    register_t			pc;
164
165
166
167
168
  };

} // namespace vm

#endif // !VM_CPU_HH