-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmemory.c
580 lines (469 loc) · 14.6 KB
/
memory.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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
/*#include <sys/file.h>*/
#pragma warning(disable : 4996)
#include <fcntl.h>
#include <io.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <windows.h>
#include <io.h>
//#include <mem.h>
#include <direct.h>
#include "defines.h"
#include "memory.h"
#include "st20.h"
/* this structure hold the state of the memory */
typedef struct memblk_struct {
long startAddr;
unsigned char data[BLKSIZE];
unsigned char used[BLKSIZE/8];
struct memblk_struct *next;
} MEMBLK;
/* this will hold the state of the memory */
static MEMBLK *memoryMap=NULL;
MEMBLK *getMemBlk (long, int);
int byteUsedBit (MEMBLK *, int, int);
int memoryInit (PARMS *userParms, FILE *outFp) {
/* the emulator will get into an infinite loop if this initialization isn't done */
storeBytes((long)0x3004, 4, 0);
//below for sti5518 ASC1 20005000 base
storeBytes((long)0x20005014, 4, 0x00000000);
//below for sti5517
// storeBytes((long)0x200130f8, 4, 0x00000001);
return (0);
}
/**************************
* There is one bit to flag whether a byte in the data array
* is used or not. used[byte 0, bit 0] indicates whether data[0]
* is used, used[byte 1, bit 0] indicates whether data[8] is used.
*
* returns true if the Used bit was set for the specified byte in
* the specified memory block before this function was called
*/
int byteUsedBit (MEMBLK *block, int offset, int usedOperation) {
int usedByteAddr=0;
int usedBitAddr=0;
int usedByte=0;
int usedBit=0;
/* get the address of the byte's used bit */
usedByteAddr = (offset & ADDR_IN_BLK_MASK) >> 3;
usedBitAddr = (offset & 7);
/* get the state of the requested bit */
usedByte = block->used[usedByteAddr];
usedBit = usedByte & (1 << usedBitAddr);
if (usedOperation == SET_BIT) {
/* mark the bit as used */
block->used[usedByteAddr] |= (1 << usedBitAddr);
}
else if (usedOperation == CLEAR_BIT) {
/* clear the used bit */
block->used[usedByteAddr] &= 0xFF - (1 << usedBitAddr);
}
/* return 1 if the used bit was originally set */
return (usedBit != 0);
}
/* THIS ROUTINE ISN'T NEEDED */
/**************************
* returns true if the requested byte was defined for the
* specified block. *Value is the value of the requested byte.
*/
/*int readBlockByte (MEMBLK *block, int offset, unsigned char *value) {
*value = (unsigned char) block->data[offset];
*/
/* if the byte was stored, return true */
/*
return (byteUsedBit(block, offset, (int) DONT_ALTER_BIT));
}
*/
/**************************
* This function reads from one to four bytes from the proper memory block.
*
* Returns TRUE if the bytes were read properly.
*/
int readBytes (long address, int nBytes, long *value) {
int i;
MEMBLK *cBlk;
unsigned char cByte;
unsigned long cWord=0;
int offset=0;
*value = UNDEFINED_WORD;
/* this routine won't work with anything longer than a double word */
if (nBytes > 4) {
return (READ_TOO_LARGE);
}
/* read each of the requested bytes */
for (i=0; i<nBytes; i++) {
/* get the block that the current byte is in */
if ((cBlk = getMemBlk (address+i, (int) FALSE)) == NULL) {
return (READ_UNUSED_MEM);
}
/* get the offset of the byte in the block */
offset = (address+i) & ADDR_IN_BLK_MASK;
/* if the byte isn't defined, return error */
if (!byteUsedBit(cBlk, offset, (int) DONT_ALTER_BIT)) {
return (READ_UNUSED_MEM);
}
/* get the data byte */
cByte = cBlk->data[offset] & 0xFF;
/* build the return value from its component bytes */
cWord += cByte << (8*i);
}
*value = cWord;
return (0);
}
int readInvBytes (long address, int nBytes, long *value) {
int i;
MEMBLK *cBlk;
unsigned char cByte;
unsigned long cWord=0;
int offset=0;
*value = UNDEFINED_WORD;
/* this routine won't work with anything longer than a double word */
if (nBytes > 4) {
return (READ_TOO_LARGE);
}
/* read each of the requested bytes */
for (i=0; i<nBytes; i++) {
/* get the block that the current byte is in */
if ((cBlk = getMemBlk (address+i, (int) FALSE)) == NULL) {
return (READ_UNUSED_MEM);
}
/* get the offset of the byte in the block */
offset = (address+i) & ADDR_IN_BLK_MASK;
/* if the byte isn't defined, return error */
if (!byteUsedBit(cBlk, offset, (int) DONT_ALTER_BIT)) {
return (READ_UNUSED_MEM);
}
/* get the data byte */
cByte = cBlk->data[offset] & 0xFF;
/* build the return value from its component bytes */
cWord += cByte << (8*(3-i));
}
*value = cWord;
return (0);
}
/**************************
* This function stores from one to four bytes in the proper memory block.
*
* Returns TRUE if the bytes were stored properly.
*/
int storeBytes (long address, int nBytes, long value) {
int i;
MEMBLK *cBlk;
unsigned char cByte;
unsigned long cWord=0;
int offset;
/* this routine won't work with anything longer than a double word */
if (nBytes > 4) {
return (FALSE);
}
cWord = value;
for (i=0; i<nBytes; i++) {
/* get the block that the current byte is in */
/* if the memory block can't be created, return an error */
if ((cBlk = getMemBlk (address+i, (int) TRUE)) == NULL) {
return (READ_UNUSED_MEM);
}
/* get each of the requested bytes */
cByte = cWord & 0xFF;
cWord >>= 8;
/* get the offset of the byte in the block */
offset = (address+i) & ADDR_IN_BLK_MASK;
/* save the byte in the memory block */
cBlk->data[offset] = cByte;
/* mark the byte as used */
byteUsedBit (cBlk, offset, (int) SET_BIT);
}
return (0);
}
/**************************
* This function stores from one to four bytes in the proper memory block.
*
* Returns TRUE if the bytes were stored properly.
*/
int storeByteRange (long srcAddr, long destAddr, int nBytes) {
int i;
int result=0;
unsigned long cWord=0;
for (i=0; i<nBytes; i++) {
/* get the block that the current byte is in */
/* if the memory block can't be created, return an error */
if (result = readBytes (srcAddr+i, 1, &cWord)) {
return (result);
}
cWord &= 0xFF;
if (result = storeBytes (destAddr+i, 1, cWord)) {
return (result);
}
}
return (0);
}
int allocBytes (long address, int nBytes) {
return (storeBytes (address, nBytes, UNDEFINED_WORD));
}
/**************************
* returns the address of the memory block containing the
* byte with the specified address. A new memory block is
* created if there is no memory block with the address.
*/
MEMBLK *getMemBlk (long address, int create_flag) {
MEMBLK *blk=NULL;
MEMBLK **lastBlk=NULL;
/* check if the necessary block already exists */
for (lastBlk=&memoryMap; *lastBlk != NULL; lastBlk=&(*lastBlk)->next) {
if ((*lastBlk)->startAddr == (address & ADDR_OF_BLK_MASK)) {
return (*lastBlk);
}
else if ((*lastBlk)->startAddr > (address & ADDR_OF_BLK_MASK)) {
break;
}
}
/* The requested block wasn't found.
* Return NULL if we aren't supposed to create new blocks
*/
if (!create_flag) {
return (NULL);
}
/* allocate memory for a new memory block */
blk = malloc(sizeof(MEMBLK));
memset (blk, 0, sizeof(MEMBLK));
if (blk == NULL) {
return (NULL);
}
/* save this block in the list of blocks */
blk->next = *lastBlk;
*lastBlk = blk;
/* store the starting address for this block of memory */
blk->startAddr = address & ADDR_OF_BLK_MASK;
/* mark the memory block as unused */
memset (blk->used, 0, BLKSIZE/8);
return (blk);
}
/**************************
* stores a large number of bytes in memory blocks
*/
int bulkLoadBytes(long address, char *byteFile, char *usedFile, long *totalBytes) {
int i=0;
int nBytesRead=0;
int byteFd=-1;
int usedFd=-1;
long nextAddress=address;
char data[BLKSIZE];
MEMBLK *cBlk;
*totalBytes = 0;
/* check the name of the byte file */
if (byteFile == NULL || *byteFile == '\0') {
fprintf (stderr, "No byte filename specified\n");
return (INVALID_BYTE_FILENAME);
}
/* check if the byte file can be opened properly */
if ((byteFd = open(byteFile, O_BINARY|O_RDONLY)) < 0) {
perror ("Memory file cannot be opened");
return (INVALID_BYTE_FILE);
}
/* check the name of the used file */
if (usedFile == NULL || *usedFile == '\0') {
usedFd = -1;
}
/* check if the used file can be opened properly */
else if ((usedFd = open(usedFile, O_BINARY|O_RDONLY)) < 0) {
perror ("Used memory file cannot be opened");
return (INVALID_BYTE_FILE);
}
/* read one block of data at a time from the input file */
do {
/*
* since the memory blocks are allocated on BLKSIZE boundaries, we
* have to fill the block starting at the proper byte address within
* the block
*/
nBytesRead = read (byteFd, &data, BLKSIZE);
/* if we haven't reach the end of the file's data */
if (nBytesRead > 0) {
/* get the memory block to store the next block of bytes in */
cBlk = getMemBlk (nextAddress, (int) TRUE);
if (cBlk == NULL) {
fprintf (stderr, "Cannot allocate memory for byte file\n");
return (FAILED_MALLOC);
}
/* copy the data into the memory block */
memcpy(&(cBlk->data[nextAddress & ADDR_IN_BLK_MASK]),
&data, (size_t) BLKSIZE - (nextAddress & ADDR_IN_BLK_MASK));
/* if there is no used byte file, set all of the bytes to used */
if (usedFd < 0) {
/* set the bits in the used array to show which bytes have been stored */
for (i=0; i < nBytesRead; i++) {
/* set the used bit for the bytes that were read */
byteUsedBit (cBlk, (nextAddress & ADDR_IN_BLK_MASK) + i,
(int) SET_BIT);
}
}
/* if there is a used byte file, load it */
else {
if (read (usedFd, &(cBlk->used[(nextAddress & ADDR_IN_BLK_MASK)/8]),
(size_t) (BLKSIZE - (nextAddress & ADDR_IN_BLK_MASK))/8) <= 0) {
return (INVALID_OUT_FILE);
}
}
} /* end of if nBytesRead > 0 */
*totalBytes += nBytesRead;
nextAddress += nBytesRead;
} while (nBytesRead > 0);
close (byteFd);
if (usedFd >= 0) {
close (usedFd);
}
return (0);
}
int saveMemory (char *dirName, FILE *outFp) {
char dataFileName[NAME_SIZE];
int dataFileFd=0;
char usedFileName[NAME_SIZE];
int usedFileFd=0;
unsigned long nextAddress=0xFFFFFFFF;
MEMBLK *cBlk;
int result=0;
/* check the directory name */
if (dirName == NULL || *dirName == '\0') {
fprintf (stderr, "No output filename specified\n");
return (INVALID_BYTE_FILENAME);
}
/* create the directory */
if (mkdir (dirName)) {
return (INVALID_DIR);
}
for (cBlk=memoryMap; cBlk != NULL; cBlk=cBlk->next) {
/* omit the ROM code addresses */
/* if (cBlk->startAddr >= 0x7FF80000 && cBlk->startAddr <= 0x7FFFFFFF) {
continue;
}
*/
/* omit the Wptr addresses */
/* if (cBlk->startAddr >= WPTR_START_ADDR && cBlk->startAddr <= WPTR_END_ADDR) {
continue;
}
*/
/*
* If this memory block is contiguous with the last, append it to
* the last file rather than creating a new file.
* If not, create a new file.
*/
if (cBlk->startAddr != nextAddress) {
/* close the file if it is already open */
if (dataFileFd) {
close (dataFileFd);
}
if (usedFileFd) {
close (usedFileFd);
}
/* create the file names */
if (sprintf (dataFileName, "%s/%08x.bin", dirName, cBlk->startAddr) == EOF) {
return (INVALID_OUT_FILE);
}
if (sprintf (usedFileName, "%s/%08x.use", dirName, cBlk->startAddr) == EOF) {
return (INVALID_OUT_FILE);
}
/* check if the files can be opened properly */
if ((dataFileFd = open(dataFileName,
O_BINARY|O_WRONLY|O_CREAT|O_TRUNC,
S_IREAD|S_IWRITE)) < 0) {
perror ("Output memory file cannot be opened");
return (INVALID_OUT_FILE);
}
if ((usedFileFd = open(usedFileName,
O_BINARY|O_WRONLY|O_CREAT|O_TRUNC,
S_IREAD|S_IWRITE)) < 0) {
perror ("Output used memory file cannot be opened");
return (INVALID_OUT_FILE);
}
nextAddress = cBlk->startAddr;
}
if (write (dataFileFd, cBlk->data, BLKSIZE) < 0) {
return (WRITE_FAILURE);
}
if (write (usedFileFd, cBlk->used, BLKSIZE/8) < 0) {
return (WRITE_FAILURE);
}
nextAddress += BLKSIZE;
}
if (dataFileFd) {
close (dataFileFd);
}
if (usedFileFd) {
close (usedFileFd);
}
return (0);
}
int loadMemory (char *dirName, FILE *outFp) {
struct _finddata_t dirBlk;
int doneDir;
char fileMask[NAME_SIZE];
char byteFileName[NAME_SIZE];
char usedFileName[NAME_SIZE];
char *dotLoc;
char addressCh[NAME_SIZE];
long address;
long dataLength=0;
int result=0;
if (sprintf (fileMask, "%s/???????0.bin", dirName) == EOF) {
return (INVALID_OUT_FILE);
}
doneDir = _findfirst(fileMask, &dirBlk);
if (doneDir==-1) {
return (INVALID_OUT_FILE);
}
do { /* for each file */
/* the name contains the address to load the file at */
strcpy (addressCh, dirBlk.name);
if (sscanf (addressCh, "%8x.bin", &address) != 1) {
return (INVALID_OUT_FILE);
}
//fprintf (stderr, "%s \n",dirBlk.name);
/* add the directory name to the file name */
strcpy (byteFileName, dirName);
strcat (byteFileName, "/");
strcat (byteFileName, dirBlk.name);
/* build the used byte filename from the data file name */
strcpy (usedFileName, byteFileName);
dotLoc = strrchr (usedFileName, '.');
if (dotLoc == NULL) {
return (INVALID_OUT_FILE);
}
/* put a null at the location of the period in the filename */
*dotLoc = '\0';
/* add the suffix for the used filename */
strcat (usedFileName, ".use");
fprintf (stderr, "%s %s\n",byteFileName, usedFileName);
result = bulkLoadBytes (address, byteFileName, usedFileName, &dataLength);
if (result) {
_findclose(doneDir);
return (result);
}
} while(_findnext (doneDir,&dirBlk)==0); /* end of for each data file */
_findclose(doneDir);
return (0);
}
char *memoryError (int error) {
switch (error) {
case INVALID_BYTE_FILENAME:
return ("The byte filename is invalid");
break;
case INVALID_BYTE_FILE:
return ("Cannot open byte file");
break;
case FAILED_MALLOC:
return ("Ran out of room allocating memory for byte file");
break;
case READ_TOO_LARGE:
return ("Cannot read more than four bytes at one time");
break;
case READ_UNUSED_MEM:
return ("An uninit memory byte was accessed ");
break;
default:
return ("Unknown memory error");
break;
}
return (NULL);
}