about summary refs log tree commit diff stats
path: root/subx
diff options
context:
space:
mode:
Diffstat (limited to 'subx')
-rw-r--r--subx/010core.cc75
-rw-r--r--subx/022transform_immediate.cc28
2 files changed, 44 insertions, 59 deletions
diff --git a/subx/010core.cc b/subx/010core.cc
index 44419696..f6c79a78 100644
--- a/subx/010core.cc
+++ b/subx/010core.cc
@@ -198,61 +198,34 @@ void load_program(const string& text_bytes, uint32_t addr) {
   load_program(in, addr);
 }
 void load_program(istream& in, uint32_t addr) {
-  in >> std::noskipws;
   while (has_data(in)) {
-    char c1 = next_hex_byte(in);
-    if (c1 == '\0') break;
-    if (!has_data(in)) {
-      raise << "input program truncated mid-byte\n" << end();
-      return;
-    }
-    char c2 = next_hex_byte(in);
-    if (c2 == '\0') {
-      raise << "input program truncated mid-byte\n" << end();
-      return;
-    }
-    write_mem_u8(addr, to_byte(c1, c2));
-    trace(99, "load") << addr << " -> " << HEXBYTE << NUM(read_mem_u8(addr)) << end();
-    addr++;
-  }
-  End_of_program = addr;
-}
-
-char next_hex_byte(istream& in) {
-  while (has_data(in)) {
-    char c = '\0';
-    in >> c;
-    if (c == ' ' || c == '\n') continue;
-    while (c == '#') {
-      while (has_data(in)) {
-        in >> c;
-        if (c == '\n') {
-          in >> c;
-          break;
-        }
+    string line_data;
+    getline(in, line_data);
+//?     cerr << "line: " << SIZE(line_data) << ": " << line_data << '\n';
+    istringstream line(line_data);
+    while (has_data(line)) {
+      string word;
+      line >> word;
+      if (word.empty()) continue;
+      if (word[0] == '#') {
+        // comment
+        break;
       }
-    }
-    if (c == '\0') return c;
-    if (c >= '0' && c <= '9') return c;
-    if (c >= 'a' && c <= 'f') return c;
-    if (c >= 'A' && c <= 'F') return tolower(c);
-    // disallow any non-hex characters, including a '0x' prefix
-    if (!isspace(c)) {
-      raise << "invalid non-hex character " << NUM(c) << "\n" << end();
-      break;
+      // otherwise it's a hex byte
+      uint32_t next_byte = 0;
+      istringstream ss(word);
+      ss >> std::hex >> next_byte;
+      if (next_byte > 0xff) {
+        raise << "invalid hex byte " << word << '\n' << end();
+        return;
+      }
+      write_mem_u8(addr, static_cast<uint8_t>(next_byte));
+      trace(99, "load") << addr << " -> " << HEXBYTE << NUM(read_mem_u8(addr)) << end();
+//?       cerr << addr << " -> " << HEXBYTE << NUM(read_mem_u8(addr)) << '\n';
+      addr++;
     }
   }
-  return '\0';
-}
-
-uint8_t to_byte(char hex_byte1, char hex_byte2) {
-  return to_hex_num(hex_byte1)*16 + to_hex_num(hex_byte2);
-}
-uint8_t to_hex_num(char c) {
-  if (c >= '0' && c <= '9') return c - '0';
-  if (c >= 'a' && c <= 'f') return c - 'a' + 10;
-  assert(false);
-  return 0;
+  End_of_program = addr;
 }
 
 inline uint8_t next() {
diff --git a/subx/022transform_immediate.cc b/subx/022transform_immediate.cc
index eefe9a40..48f5dfa9 100644
--- a/subx/022transform_immediate.cc
+++ b/subx/022transform_immediate.cc
@@ -23,17 +23,29 @@ Transform.push_back(transform_immediate);
 :(code)
 void transform_immediate(const string& input, string& output) {
   istringstream in(input);
-  in >> std::noskipws;
   ostringstream out;
   while (has_data(in)) {
-    string word = next_word(in);
-    if (word.find("/imm") == string::npos)
-      out << word << ' ';
-    else {
-      string output = transform_immediate(word);
-      trace("translate") << "converting '" << word << "' to '" << output << "'" << end();
-      out << output << ' ';
+    string line_data;
+    getline(in, line_data);
+    istringstream line(line_data);
+    while (has_data(line)) {
+      string word;
+      line >> word;
+      if (word.empty()) continue;
+      if (word[0] == '#') {
+        // skip comment
+        break;
+      }
+      if (word.find("/imm") == string::npos) {
+        out << word << ' ';
+      }
+      else {
+        string output = transform_immediate(word);
+        trace("translate") << "converting '" << word << "' to '" << output << "'" << end();
+        out << output << ' ';
+      }
     }
+    out << '\n';
   }
   out.str().swap(output);
 }
ss='oid'>3cf03158 ^
d89af9bd ^
3cf03158 ^
fa805850 ^
fee1bbd8 ^
15676346 ^
d89af9bd ^



15676346 ^


27b1e19e ^
15676346 ^



















d89af9bd ^
606c1ab4 ^
8b85a07f ^



fa805850 ^



8b85a07f ^
606c1ab4 ^
0ef7f1d2 ^




























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