-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmemory.cpp
72 lines (61 loc) · 1.05 KB
/
memory.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
//
//
// memory.c
//
// (C) R.P.Bellis 2021
//
//
#include <cstdio>
#include <cstdlib>
#include "memory.h"
Byte fread_hex_byte(FILE *fp)
{
char str[3];
long l;
str[0] = fgetc(fp);
str[1] = fgetc(fp);
str[2] = '\0';
l = strtol(str, NULL, 16);
return (Byte)(l & 0xff);
}
Word fread_hex_word(FILE *fp)
{
Word ret;
ret = fread_hex_byte(fp);
ret <<= 8;
ret |= fread_hex_byte(fp);
return ret;
}
void ROM::load_intelhex(const char *filename, Word base)
{
FILE *fp;
int done = 0;
fp = fopen(filename, "r");
if (!fp) {
perror("filename");
exit(EXIT_FAILURE);
}
while (!done) {
Byte n, t;
Word addr;
Byte b;
(void)fgetc(fp);
n = fread_hex_byte(fp);
addr = fread_hex_word(fp);
t = fread_hex_byte(fp);
if (t == 0x00) {
while (n--) {
b = fread_hex_byte(fp);
if ((addr >= base) && (addr < ((DWord)base + size))) {
memory[addr - base] = b;
}
++addr;
}
} else if (t == 0x01) {
done = 1;
}
// Read and discard checksum byte
(void)fread_hex_byte(fp);
if (fgetc(fp) == '\r') (void)fgetc(fp);
}
}