-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsegment.c
373 lines (336 loc) · 7.87 KB
/
segment.c
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
// Copyright (C) 1999-2012 Core Technologies.
//
// This file is part of tpasm.
//
// tpasm is free software; you can redistribute it and/or modify
// it under the terms of the tpasm LICENSE AGREEMENT.
//
// tpasm 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
// tpasm LICENSE AGREEMENT for more details.
//
// You should have received a copy of the tpasm LICENSE AGREEMENT
// along with tpasm; see the file "LICENSE.TXT".
// Handle creation and manipulation of segments
#include "include.h"
static SYM_TABLE
*segmentSymbols; // segment symbol list is kept here
static CODE_PAGE *FindCodePage(SEGMENT_RECORD *segment,unsigned int address)
// find the code page of address, or the one immediately before it in the linked
// list of code pages hanging off segment
// NOTE: if the address is earlier than any code page within segment,
// NULL will be returned
// NOTE: this should probably be made faster
{
CODE_PAGE
*currentPage,
*bestPage;
bestPage=NULL;
currentPage=segment->firstPage;
if(segment->pageCache&&segment->pageCache->address<=address)
{
currentPage=segment->firstPage;
}
while(currentPage&¤tPage->address<=address)
{
bestPage=currentPage;
currentPage=currentPage->next;
}
segment->pageCache=bestPage;
return(bestPage);
}
static void DestroyCodePage(SEGMENT_RECORD *segment,CODE_PAGE *page)
// unlink page from segment, and destroy it
{
if(page->next)
{
page->next->previous=page->previous;
}
if(page->previous)
{
page->previous->next=page->next;
}
else
{
segment->firstPage=page->next;
}
if(segment->pageCache==page) // update the cache if needed
{
segment->pageCache=page->previous;
}
DisposePtr(page);
}
static CODE_PAGE *CreateCodePage(SEGMENT_RECORD *segment,CODE_PAGE *pageBefore)
// create a new code page
// link it to segment after pageBefore
// NOTE: pageBefore can be passed in as NULL to link it to the start of the
// segment
// NOTE: the code page's usage map is initialized to be completely unused
// NOTE: if this fails, it will return NULL
{
CODE_PAGE
*page;
int
i;
if((page=(CODE_PAGE *)NewPtr(sizeof(CODE_PAGE))))
{
for(i=0;i<32;i++)
{
page->usageMap[i]=0;
}
if(pageBefore)
{
page->next=pageBefore->next;
page->previous=pageBefore;
pageBefore->next=page;
}
else
{
if((page->next=segment->firstPage))
{
page->next->previous=page;
}
page->previous=NULL;
segment->firstPage=page;
}
return(page);
}
return(NULL);
}
bool AddBytesToSegment(SEGMENT_RECORD *segment,unsigned int address,unsigned char *bytes,unsigned int numBytes)
// add bytes to segment at address
// if there is a problem, report it, and return false
// NOTE: this manages creating code pages, and linking them to segment
// in the proper location.
{
CODE_PAGE
*page;
unsigned int
baseAddress;
unsigned int
index;
bool
fail;
fail=false;
while(numBytes&&!fail)
{
page=FindCodePage(segment,address); // get the nearest page to the one we want to write into
baseAddress=address&~0xFF; // mask off low part of address
if(!page||(page->address!=baseAddress)) // see if a new page needs to be created
{
if((page=CreateCodePage(segment,page)))
{
page->address=baseAddress; // set up the base address for this page
}
else
{
ReportComplaint(true,"Failed to create code page\n");
fail=true;
}
}
if(!fail) // make sure there's a good page to use
{
index=address&0xFF;
while(numBytes&&(index<0x100)) // copy bytes into the page
{
if(page->usageMap[index>>3]&(1<<(index&7)))
{
AssemblyComplaint(NULL,false,"Overwriting address 0x%08X in segment '%s'\n",address,STNodeName(segment->symbol));
}
page->usageMap[index>>3]|=(1<<(index&7)); // update the usage map
page->pageData[index]=*bytes; // drop the bytes into the page
bytes++;
index++;
address++;
numBytes--;
}
}
}
return(!fail);
}
bool AddSpaceToSegment(SEGMENT_RECORD *segment,unsigned int address,unsigned int numBytes)
// add bytes of empty space to segment at address
// Call this in response to a DS (define space) command
{
if(segment->sawDS)
{
if(segment->minDS>address)
{
segment->minDS=address;
}
if(segment->maxDS<address+numBytes-1)
{
segment->maxDS=address+numBytes-1;
}
}
else
{
if(numBytes)
{
segment->sawDS=true;
segment->minDS=address;
segment->maxDS=address+numBytes-1;
}
}
return(true);
}
SEGMENT_RECORD *MatchSegment(const char *segment)
// try to match segment against the list of segments which currently exist
{
void
*resultValue;
if((resultValue=STFindDataForName(segmentSymbols,segment)))
{
return((SEGMENT_RECORD *)resultValue);
}
return(NULL);
}
void DestroySegment(SEGMENT_RECORD *segment)
// remove segment from existence
{
while(segment->firstPage) // get rid of code page list
{
DestroyCodePage(segment,segment->firstPage);
}
STRemoveEntry(segmentSymbols,segment->symbol);
if(segment->next)
{
segment->next->previous=segment->previous;
}
else
{
segmentsTail=segment->previous;
}
if(segment->previous)
{
segment->previous->next=segment->next;
}
else
{
segmentsHead=segment->next;
}
DisposePtr(segment);
}
void DestroySegments()
// remove all segments
{
while(segmentsHead)
{
DestroySegment(segmentsHead);
}
}
SEGMENT_RECORD *CreateSegment(const char *segmentName,bool generateOutput)
// Create a segment record, link it into the end of the global list,
// create a symbol table entry for it
{
SEGMENT_RECORD
*record;
if((record=(SEGMENT_RECORD *)NewPtr(sizeof(SEGMENT_RECORD))))
{
record->generateOutput=generateOutput;
record->pageCache=NULL;
record->firstPage=NULL;
record->currentPC=0;
record->codeGenOffset=0;
record->sawDS=false;
record->minDS=0;
record->maxDS=0;
if((record->symbol=STAddEntryAtEnd(segmentSymbols,segmentName,record)))
{
if((record->previous=segmentsTail))
{
segmentsTail->next=record;
}
else
{
segmentsHead=record;
}
segmentsTail=record; // this record is the new tail
record->next=NULL; // no next for this one
return(record);
}
DisposePtr(record);
}
return(NULL);
}
static void GetSegmentMinMax(SEGMENT_RECORD *segment,unsigned int *minAddress,unsigned int *maxAddress)
// Work out the minimum and maximum addresses occupied by segment
{
CODE_PAGE
*page;
int
i;
*minAddress=0;
*maxAddress=0;
if((page=segment->firstPage))
{
i=0;
while(i<256&&(page->usageMap[i>>3]&(1<<(i&7)))==0)
{
i++;
}
*minAddress=page->address+i;
while(page->next)
{
page=page->next;
}
i=255;
while(i>0&&(page->usageMap[i>>3]&(1<<(i&7)))==0)
{
i--;
}
*maxAddress=page->address+i;
}
if(segment->sawDS)
{
if((!segment->firstPage)||(segment->minDS<*minAddress))
{
*minAddress=segment->minDS;
}
if((!segment->firstPage)||(segment->maxDS>*maxAddress))
{
*maxAddress=segment->maxDS;
}
}
}
static void DumpSegmentListing(FILE *file,SEGMENT_RECORD *segment)
// Create a listing of the given segment, and
// what memory ranges it spanned
{
unsigned int
minAddress,
maxAddress;
GetSegmentMinMax(segment,&minAddress,&maxAddress);
fprintf(file,"%08X %08X %s\n",minAddress,maxAddress,STNodeName(segment->symbol));
}
void DumpSegmentsListing(FILE *file)
// Create a listing of which segments were specified, and
// what memory ranges they spanned
{
SEGMENT_RECORD
*segment;
fprintf(file,"Segment Listing\n");
fprintf(file,"MinAddr MaxAddr Segment\n");
fprintf(file,"-------- -------- -------\n");
segment=segmentsHead;
while(segment)
{
DumpSegmentListing(file,segment);
segment=segment->next;
}
}
void UnInitSegments()
// undo what InitSegments did
{
STDisposeSymbolTable(segmentSymbols);
}
bool InitSegments()
// initialize symbol table for segments
{
if((segmentSymbols=STNewSymbolTable(100)))
{
return(true);
}
return(false);
}