-
Notifications
You must be signed in to change notification settings - Fork 3
/
segment.cpp
257 lines (199 loc) · 3.81 KB
/
segment.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
// segment.cpp
// Revision 11-oct-2006
#include "segment.h"
#include "trace.h"
#include <sstream>
#include <algorithm>
using std::fill_n;
namespace pasmo {
namespace impl {
//*********************************************************
// class Segment
//*********************************************************
const size_t Segment::max_seg_size;
Segment::Segment () :
minused (65535),
maxused (0),
curpos (0),
base (0),
size (0),
org_addr (65535),
has_org (false)
{
TRFUNC (tr, "Segment::Segment");
fill_n (mem, 0, max_seg_size);
}
void Segment::clear ()
{
if (! empty () )
{
size_t s= maxused - minused + 1;
fill_n (mem + minused, 0, s);
}
minused= 65535;
maxused= 0;
curpos= 0;
base= 0;
size= 0;
}
bool Segment::empty () const
{
return minused > maxused;
}
void Segment::setbase (address base_n)
{
base= base_n;
}
void Segment::setsize (address size_n)
{
size= size_n;
}
address Segment::getsize () const
{
return size;
}
address Segment::getminused () const
{
return minused;
}
address Segment::getmaxused () const
{
return maxused;
}
address Segment::getbase () const
{
return base;
}
void Segment::setbyte (address pos, byte b)
{
TRFUNC (tr, "Segment::setbyte");
TRSTREAM (tr, "At " << hex4 (pos) << " value " << hex2 (b) );
mem [pos]= b;
if (pos < minused)
minused= pos;
if (pos > maxused)
maxused= pos;
TRSTREAM (tr, "Min: " << hex4 (minused) <<
" max: " << hex4 (maxused) );
}
void Segment::setword (address pos, address value)
{
TRFUNC (tr, "Segment::setword");
TRSTREAM (tr, "At " << hex4 (pos) << " value " << hex4 (value) );
#if 0
ASSERT (pos < max_seg_size - 1);
mem [pos + 0]= lobyte (value);
mem [pos + 1]= hibyte (value);
if (pos < minused)
minused= pos;
++pos;
if (pos > maxused)
maxused= pos;
#else
setbyte (pos, lobyte (value) );
++pos;
setbyte (pos, hibyte (value) );
#endif
}
void Segment::offsetword (address pos, address value)
{
TRFUNC (tr, "Segment::offsetword");
TRSTREAM (tr, "At " << hex4 (pos) << " value " << hex4 (value) );
value+= getword (pos);
mem [pos + 0]= lobyte (value);
mem [pos + 1]= hibyte (value);
}
byte Segment::getbyte (address pos) const
{
return mem [pos];
}
address Segment::getword (address pos) const
{
ASSERT (pos < max_seg_size - 1);
return mem [pos] + static_cast <address> (mem [pos + 1]) * 256;
}
void Segment::setpos (address pos)
{
curpos= pos;
}
void Segment::setorg (address pos)
{
curpos= pos;
if (has_org)
{
if (pos < org_addr)
org_addr= pos;
}
else
{
has_org= true;
org_addr= pos;
}
}
address Segment::getpos () const
{
return curpos;
}
address Segment::getorg () const
{
return org_addr;
}
bool Segment::hasorg () const
{
return has_org;
}
void Segment::putbyte (byte b)
{
setbyte (curpos++, b);
}
void Segment::putword (address value)
{
//setword (curpos, value);
//curpos+= 2;
setbyte (curpos++, lobyte (value) );
setbyte (curpos++, hibyte (value) );
}
void Segment::evallimits ()
{
TRFUNC (tr, "Segment::evallimits");
ASSERT (size == 0);
if (! empty () )
{
size= maxused;
++size;
}
}
Segment::byte_iterator Segment::iter (address pos)
{
return mem + pos;
}
Segment::byte_const_iterator Segment::iter (address pos) const
{
return mem + pos;
}
void Segment::write (ostream & out, address pos, size_t size)
{
TRFUNC (tr, "Segment::write");
TRSTREAM (tr, "pos: " << hex4 (pos) << ", size: " << hex4 (size) );
//ASSERT (pos < max_seg_size - size);
ASSERT (pos <= max_seg_size - size);
out.write (reinterpret_cast <char *> (mem + pos), size);
}
void Segment::copy (Segment & dest)
{
if (! empty () )
{
#if 0
address realsize= size - minused;
#else
size_t realsize= size - minused;
#endif
for (address i= minused; realsize > 0; --realsize, ++i)
{
dest.setbyte (base + i, mem [i] );
}
}
}
} // namespace impl
} // namespace pasmo
// End of segment.cpp