-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert_wladx_sdas.js
323 lines (271 loc) · 6.35 KB
/
convert_wladx_sdas.js
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
//
// Converts instructions from wla-dx to sdas format
// e.g. ld a, 4 => ld a, #4
// ld a, (ix + 4) => ld a, 4(ix)
//
// Joe Kennedy 2024
//
const fs = require('fs');
const readline = require('readline');
const register_names = ["a", "b", "c", "d", "e", "h", "l", "af", "bc", "de", "hl", "ix", "iy", "sp"];
function rearrange(tokens)
{
if (tokens.length == 0)
{
return tokens;
}
if (tokens.length == 1)
{
// just return tokens if it's a register on its own
if (register_names.indexOf(tokens[0]) != -1)
{
return tokens;
}
}
// if the first character is an upper/lower byte selector, make the following an immediate value
if (tokens[0] == "<" || tokens[0] == ">")
{
tokens.splice(0, 0, "#");
return tokens;
}
// either immediate addressing or using index register
if (tokens[0] == "(")
{
// index registers
if ((tokens[1] == "ix" || tokens[1] == "iy") && tokens[2] == "+")
{
return reorganise_ix_iy(tokens);
}
return tokens;
}
else
{
tokens.splice(0, 0, "#");
return tokens;
}
}
// transforms
// (ix + 5) -> 5(ix)
// (ix + n) -> n(ix)
function reorganise_ix_iy(tokens)
{
let out = tokens.slice(3, tokens.length - 1);
out.push("(")
out.push(tokens[1]);
out.push(")");
return out;
}
// split asm line into tokens
async function tokenize_lines()
{
let full_output = [];
let fileStream = fs.createReadStream(process.argv[2]);
const rl = readline.createInterface({
input: fileStream,
crlfDelay: Infinity,
});
for await (const line of rl)
{
let output = [];
let accumulator = "";
let inside_instruction = false;
for (var i = 0; i < line.length; i++)
{
// start of comment
if (line[i] == ";")
{
if (accumulator.length > 0)
{
output.push(accumulator);
accumulator = "";
}
output.push(line[i]);
output.push(line.substring(i + 1, line.length));
break;
}
// whitespace
else if (" \t".indexOf(line[i]) != -1)
{
if (accumulator.length > 0)
{
output.push(accumulator);
accumulator = "";
}
// if this whitespace came before the instruction
// then keep it to preserve indentation, otherwise ignore it
if (inside_instruction == false)
{
output.push(line[i]);
}
}
// these chars usually delimit in some way
else if (",()+-|&<>".indexOf(line[i]) != -1)
{
inside_instruction = true;
if (accumulator.length > 0)
{
output.push(accumulator);
accumulator = "";
}
output.push(line[i]);
}
// append this character to the accumulator
else
{
inside_instruction = true;
accumulator = accumulator + line[i];
}
// write the accumulator if this is the last character
if (i == line.length - 1 && accumulator.length > 0)
{
output.push(accumulator);
accumulator = "";
}
}
full_output.push(output);
}
return full_output;
}
// process the tokens
async function process_lines(lines)
{
// we're expecting certain labels to be declared in C or used in C
// add an underscore in front of these symbols so they match the C declared ones
let underscore_prefix = [
"song_channels", "song_state", "song_table",
"sfx_table", "sfx_channel", "sfx_state",
"banjo_init", "banjo_init:",
"banjo_update", "banjo_update:",
"banjo_queue_song", "banjo_queue_song:",
"banjo_queue_sfx", "banjo_queue_sfx:",
"banjo_play_song", "banjo_play_song:",
"banjo_play_sfx", "banjo_play_sfx:",
"banjo_update_song", "banjo_update_song:",
"banjo_update_sfx", "banjo_update_sfx:",
"banjo_mute_song_channel", "banjo_mute_song_channel:",
"banjo_unmute_song_channel", "banjo_unmute_song_channel:",
"banjo_set_song_table:", "banjo_set_sfx_table:",
"banjo_check_hardware:", "banjo_song_stop:", "banjo_sfx_stop:",
"banjo_queue_song_loop_mode:", "banjo_queue_sfx_loop_mode:",
"banjo_fm_unit_present", "banjo_game_gear_mode", "banjo_system_e", "banjo_mode",
];
var outfile = fs.createWriteStream(process.argv[3], {flags: "w+"});
console.log(lines);
for (let i = 0; i < lines.length; i++)
{
let line = lines[i];
let opcode = null;
let dest = [];
let source = [];
let whitespace = "";
let comment = "";
let search = "opcode";
for (let j = 0; j < line.length; j++)
{
let token = line[j];
// handle comments
if (token == ";")
{
comment = ";" + line[j + 1];
j++;
}
// handle whitespace
else if (token == " " || token == "\t")
{
whitespace += token;
}
// found the opcode
else if (search == "opcode")
{
if (underscore_prefix.indexOf(token) != -1)
{
token = "_" + token;
}
opcode = token;
search = "dest";
}
// found a destination token
else if (search == "dest")
{
if (token == ",")
{
search = "source";
}
else
{
dest.push(token);
}
}
// found a source token
else if (search == "source")
{
source.push(token);
}
}
// add underscore to front of selected symbols
for (let k = 0; k < dest.length; k++)
{
if (underscore_prefix.indexOf(dest[k]) != -1)
{
dest[k] = "_" + dest[k];
}
}
for (let k = 0; k < source.length; k++)
{
if (underscore_prefix.indexOf(source[k]) != -1)
{
source[k] = "_" + source[k];
}
}
//
// update opcode parameters to sdas formatting
//
// make port for out immediate if it's not register c
if (opcode == "out")
{
if (dest[0] == "(" && dest[1] != "c")
{
dest.splice(1, 0, "#");
}
}
else if (opcode == "in")
{
if (source[0] == "(" && source[1] != "c")
{
source.splice(1, 0, "#");
}
}
else if (opcode == "ld")
{
source = rearrange(source);
dest = rearrange(dest);
}
else if (opcode == "cp" || opcode == "add" || opcode == "adc" || opcode == "sub" || opcode == "sbc" || opcode == "and" || opcode == "or" || opcode == "xor")
{
source = rearrange(source);
}
else if (opcode == "inc" || opcode == "dec")
{
dest = rearrange(dest);
}
else if (opcode == "bit" || opcode == "set" || opcode == "res")
{
source = rearrange(source);
}
outfile.write(
whitespace +
(opcode ? (opcode + " ") : "") +
(dest.length ? dest.join("") : "") +
(source.length ? (", " + source.join("")) : "") +
comment +
"\n"
);
}
//console.log(output);
}
async function main()
{
let lines = await tokenize_lines();
process_lines(lines);
}
main();