-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreadfile.cpp
More file actions
executable file
·148 lines (140 loc) · 4.74 KB
/
Copy pathreadfile.cpp
File metadata and controls
executable file
·148 lines (140 loc) · 4.74 KB
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
#include <fstream>
#include <iostream>
#include <ios>
#include <string>
#include "mem.h"
using namespace mem;
namespace {
addr
readhex(std::istream& is, int width) {
int v = 0;
for (int i = 0; i < width; i++) {
char ch;
int digit = 0;
is >> ch;
if (!is) {
throw "Parse Error";
}
else if (ch >= 'A' && ch <= 'F') {
digit = ch - 'A' + 10;
}
else if (ch >= 'a' && ch <= 'f') {
digit = ch - 'a' + 10;
}
else if (ch >= '0' && ch <= '9') {
digit = ch - '0';
}
else {
throw "Unexpected digit";
}
v = v << 4 | digit;
}
return v;
}
void
chksum(byte chk, addr a, int lineno, std::istream& is, std::string const& filename)
{
chk += (a >> 24);
chk += (a >> 16);
chk += (a >> 8);
chk += (a >> 0);
chk += readhex(is, 2);
if (chk != 0xFF) {
std::cerr << filename << ":" << lineno << ": "
<< "chk err: "
<< std::hex << std::showbase << int(chk)
<< std::endl;
}
}
} // namespace
namespace mem {
void
readmoto(std::string const& filename, memory& mem) {
std::ifstream is(filename.c_str());
if (is.fail()) {
std::cerr << "couldn't open: \"" << filename << "\"" << std::endl;
return;
}
readmoto(is, mem, filename);
}
void
readmoto(std::istream& is, memory& mem, std::string const& filename) {
std::string line;
int lineno = 1;
unsigned rcnt = 0;
do {
char ch;
is >> ch;
if (ch != 'S') {
if (is)
std::cerr << filename << ":" << lineno << ": "
<< "not an srecord:\"" << line << "\"" << std::endl;
}
else {
is >> ch;
if (ch >= '1' && ch <= '3') {
int addrlen = 1 + ch - '0';
int count = readhex(is, 2);
byte chk = (byte) count;
count -= addrlen + 1; // just data bytes.
addr a = readhex(is, addrlen * 2);
if (0) std::cerr << std::hex << "addr=" << a
<< std::dec << " count=" << count
<< std::endl;
for (int i = 0; i < count; i++) {
byte b = (byte) readhex(is, 2);
mem[a+i] = b;
chk += b;
}
chksum(chk, a, lineno, is, filename);
rcnt++;
}
else if (ch == '5') {
int addrlen = 2;
int count = readhex(is, 2);
byte chk = (byte) count;
count -= addrlen + 1; // just data bytes.
addr a = readhex(is, addrlen * 2);
if (rcnt != a) {
std::cerr << filename << ":" << lineno << ": "
<< "record count: " << std::hex << int(rcnt) << "!=" << a
<< std::endl;
}
chksum(chk, a, lineno, is, filename);
}
else if (ch >= '7' && ch <= '9') {
int addrlen = 11 + '0' - ch;
int count = readhex(is, 2);
byte chk = (byte) count;
addr a = readhex(is, addrlen * 2);
mem.setexecaddr(a);
chksum(chk, a, lineno, is, filename);
}
else if (ch == '0') {
int addrlen = 2;
int count = readhex(is, 2);
byte chk = (byte) count;
count -= addrlen + 1; // just data bytes.
addr a = readhex(is, addrlen * 2);
std::string s;
for (int i = 0; i < count; i++) {
char ch = (char) readhex(is, 2);
chk += ch;
s.push_back(ch);
}
mem.setdesc(s);
chksum(chk, a, lineno, is, filename);
}
else {
std::cerr << filename << ":" << lineno << ": "
<< "ignored: S" << ch
<< std::endl;
}
}
std::string line;
getline(is, line);
lineno++;
}
while (is);
}
}