memory.hh 3.42 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
//
Benoit Perrot's avatar
Benoit Perrot committed
19
20
/** \file vm/memory.hh
    \brief Declare Memory class. */
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#ifndef VM_MEMORY_HH
# define VM_MEMORY_HH

# include "misc/contract.hh"

# include "inst/data_section.hh"
# include "vm/segment.hh"

// FIXME: lack of compatibility
# include <stdint.h>
# ifndef INT32_MAX
#  define INT32_MAX		2147483647
# endif

namespace vm
{

Benoit Perrot's avatar
Benoit Perrot committed
38
  /// Memory abstraction
39
40
41
  class Memory
  {
  public:
Benoit Perrot's avatar
Benoit Perrot committed
42
    static const int	stack_bottom = INT32_MAX - 3;
Benoit Perrot's avatar
Benoit Perrot committed
43
    static const int	default_stack_size = 512 * 1024;
Benoit Perrot's avatar
Benoit Perrot committed
44
    
Benoit Perrot's avatar
Benoit Perrot committed
45
46
    /** \name Constructor and destructor 
	\{ */
47
  public:
Benoit Perrot's avatar
Benoit Perrot committed
48
    /// Construct a Memory.
Benoit Perrot's avatar
Benoit Perrot committed
49
50
    Memory(int stack_size = default_stack_size):
      heap(0), stack(stack_size), stack_top(stack_bottom - stack_size + 4)
51
    {
Benoit Perrot's avatar
Benoit Perrot committed
52
      assertion(stack_size > 0);
53
    }
Benoit Perrot's avatar
Benoit Perrot committed
54
    /** \} */
55
56
57
58
59
60
61
62
63
64
65
66
67

  protected:
    int			translate(int offset) const
    {
      precondition(offset >= stack_top);
      // Stack grows down
      return offset - stack_top;
    }

  public:
    int			sbrk(int size)
    {
      precondition(size >= 0);
Benoit Perrot's avatar
Benoit Perrot committed
68
      int		ptr = heap.size();
69
70
      if (size)
	// FIXME: check collision with stack
Benoit Perrot's avatar
Benoit Perrot committed
71
	heap.resize(size + heap.size());
72
73
74
      return ptr;
    }

75
  public:
Benoit Perrot's avatar
Benoit Perrot committed
76
    int			heap_size() const
77
    {
Benoit Perrot's avatar
Benoit Perrot committed
78
      return heap.size ();      
79
    }
Benoit Perrot's avatar
Benoit Perrot committed
80
    int			stack_size() const
81
    {
Benoit Perrot's avatar
Benoit Perrot committed
82
      return stack.size ();
83
84
    }

Benoit Perrot's avatar
Benoit Perrot committed
85
86
    /** \name Store instructions.
	\{ */
87
88
89
  public:
    void		store(const inst::DataSection& data_section)
    {
Benoit Perrot's avatar
Benoit Perrot committed
90
      heap.resize(data_section.size());
91
      for (int i = 0; i < data_section.size(); ++i)
Benoit Perrot's avatar
Benoit Perrot committed
92
	heap.store_byte(i, data_section.load_byte(i));
93
94
    }

Benoit Perrot's avatar
Benoit Perrot committed
95
    /// Store a byte in memory.
96
97
98
    void		store_byte(int offset, int b)
    {
      precondition(offset >= 0);
Benoit Perrot's avatar
Benoit Perrot committed
99
100
      if (offset < heap.size())
	heap.store_byte(offset, b);
101
      else
Benoit Perrot's avatar
Benoit Perrot committed
102
	stack.store_byte(translate(offset), b);
103
    }
Benoit Perrot's avatar
Benoit Perrot committed
104
    /// Store a word in memory.
105
106
107
    void		store_word(int offset, int w)
    {
      precondition(offset >= 0);
Benoit Perrot's avatar
Benoit Perrot committed
108
109
      if (offset < heap.size())
	heap.store_word(offset, w);
110
      else
Benoit Perrot's avatar
Benoit Perrot committed
111
	stack.store_word(translate(offset), w);
112
    }
Benoit Perrot's avatar
Benoit Perrot committed
113
    /** \} */
114
    
Benoit Perrot's avatar
Benoit Perrot committed
115
116
    /** \name Load instructions.
	\{ */
117
  public:
Benoit Perrot's avatar
Benoit Perrot committed
118
    /// Load a byte from memory.
119
120
121
    int			load_byte(int offset) const
    {
      precondition(offset >= 0);
Benoit Perrot's avatar
Benoit Perrot committed
122
123
124
      if (offset < heap.size())
	return heap.load_byte(offset);
      return stack.load_byte(translate(offset));
125
    }
Benoit Perrot's avatar
Benoit Perrot committed
126
    /// Load a word from memory.
127
128
129
    int			load_word(int offset) const
    {
      precondition(offset >= 0);
Benoit Perrot's avatar
Benoit Perrot committed
130
131
132
      if (offset < heap.size())
	return heap.load_word(offset);
      return stack.load_word(translate(offset));
133
    }
Benoit Perrot's avatar
Benoit Perrot committed
134
    /** \} */
135
136

  protected:
Benoit Perrot's avatar
Benoit Perrot committed
137
138
139
    Segment		heap;
    Segment		stack;
    const int		stack_top;
140
141
142
143
144
  };

} // namespace vm

#endif // !VM_MEMORY_HH