-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharch-spc700.cpp
More file actions
470 lines (453 loc) · 13.3 KB
/
arch-spc700.cpp
File metadata and controls
470 lines (453 loc) · 13.3 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
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
#include "asar.h"
#define error error<errblock>
#define write1 write1_pick
static int writesizeto=-1;
static int inlinestartpos=0;
extern bool emulatexkas;
static void inline_finalizeorg()
{
if (writesizeto>=0 && pass==2)
{
int pcpos=snestopc(writesizeto&0xFFFFFF);
if (pcpos<0) error(2, S"SNES address "+hex6(realsnespos)+" doesn't map to ROM");
int num=snespos-startpos;
romdata[pcpos ]=num ;
romdata[pcpos+1]=num>> 8;
}
writesizeto=-1;
}
static void inline_org(int num)
{
inline_finalizeorg();
if (num&~0xFFFF) error(0, "Address out of bounds");
writesizeto=realsnespos;
write2(0x0000);
write2(num);
snespos=num;
startpos=num;
}
static void inline_leavearch()
{
inline_finalizeorg();
write2(0x0000);
write2(inlinestartpos);
}
void asinit_spc700()
{
}
void asend_spc700()
{
if (arch==arch_spc700_inline) inline_leavearch();
}
static bool matchandwrite(const char * str, const char * left, const char * right, string& remainder)
{
for (int i=0;left[i];i++)
{
if (tolower(*str)!=left[i]) return false;
str++;
}
int mainlen=strlen(str)-strlen(right);
for (int i=0;right[i];i++)
{
if (tolower(str[mainlen+i])!=right[i]) return false;
}
remainder=substr(str, mainlen);
return true;
}
static bool bitmatch(const char * opnamein, string& opnameout, const char * str, string& math, int& bit)
{
const char * opnameend=strchr(opnamein, '\0');
const char * dot=strqrchr(str, '.');
if (dot && isdigit(dot[1]) && !dot[2])
{
bit=atoi(dot+1);
if (bit>=8) return false;
math=substr(str, dot-str);
if (opnameend[-1]=='1') opnameout=substr(opnamein, opnameend-opnamein-1);
else opnameout=opnamein;
return true;
}
if (opnameend[-1]>='0' && opnameend[-1]<='7')
{
math=str;
bit=opnameend[-1]-'0';
opnameout=substr(opnamein, opnameend-opnamein-1);
return true;
}
return false;
}
#define isop(test) (!stricmp(op, test))
static bool assinglebitwithc(const char * op, const char * math, int bits)
{
int num;
if (math[0]=='!')
{
if(0);
else if (isop("or")) write1(0x2A);
else if (isop("and")) write1(0x6A);
else return false;
num=getnum(math+1);
}
else
{
if(0);
else if (isop("or")) write1(0x0A);
else if (isop("and")) write1(0x4A);
else if (isop("eor")) write1(0x8A);
else if (isop("mov")) write1(0xAA);
else if (isop("not")) write1(0xEA);
else return false;
num=getnum(math);
}
if (num>=0x2000) error(2, "Address out of bounds");
write2((bits<<13)|num);
return true;
}
#undef isop
bool asblock_spc700(char** word, int numwords)
{
#define is(test) (!stricmp(word[0], test))
#define is0(test) (!stricmp(word[0], test) && numwords==1)
#define is1(test) (!stricmp(word[0], test) && numwords==2)
#define is2(test) (!stricmp(word[0], test) && numwords==3)
#define is3(test) (!stricmp(word[0], test) && numwords==4)
#define is4(test) (!stricmp(word[0], test) && numwords==5)
#define is5(test) (!stricmp(word[0], test) && numwords==6)
#define par word[1]
if(0);
else if (arch==arch_spc700_inline && is1("org"))
{
int num=getnum(par);
if (foundlabel) error(0, "org Label is not valid");
inline_org(num);
}
else if (arch==arch_spc700_inline && is1("arch"))
{
inline_leavearch();
return false;
}
else if (arch==arch_spc700_inline && is1("skip"))
{
int num=snespos+getnum(par);
if (foundlabel) error(0, "skip Label is not valid");
inline_org(num);
}
else if (arch==arch_spc700_inline && is1("base"))
{
error(0, "base is not implemented for architecture spc700-inline.");
}
else if (arch==arch_spc700_inline && is1("startpos"))
{
inlinestartpos=getnum(par);
}
else if (numwords==1)
{
#define op(name, val) else if (is(name)) do { write1(val); } while(0)
if(0);
op("nop", 0x00);
op("brk", 0x0F);
op("clrp", 0x20);
op("setp", 0x40);
op("clrc", 0x60);
op("ret", 0x6F);
op("reti", 0x7F);
op("setc", 0x80);
op("ei", 0xA0);
op("di", 0xC0);
op("clrv", 0xE0);
op("notc", 0xED);
op("sleep", 0xEF);
op("stop", 0xFF);
op("xcn", 0x9F);
else return false;
#undef op
}
else if (numwords==2)
{
int numwords;
autoptr<char*> parcpy=strdup(par);
autoptr<char**> arg=qpsplit(parcpy, ",", &numwords);
if (numwords==1)
{
string op;
string math;
int bits;
#define begin(str) (!strncasecmp(word[0], str, strlen(str)))
#define isop(str) (!stricmp(word[0], str))
#define isam(str) (!stricmp(arg[0], str))
#define ismatch(left, right) (matchandwrite(arg[0], left, right, math))
#define eq(str) if (isam(str))
#define w0(hex) do { write1(hex); return true; } while(0)
#define w1(hex) do { write1(hex); write1(getnum(math)); return true; } while(0)
#define w2(hex) do { write1(hex); write2(getnum(math)); return true; } while(0)
#define wv(hex1, hex2) do { if (getlen(math)==1) { write1(hex1); write1(getnum(math)); } else { write1(hex2); write2(getnum(math)); } return true; } while(0)
#define wr(hex) do { int len=getlen(math); int num=getnum(math); int pos=(len==1)?num:num-((snespos&0xFFFFFF)+2); write1(hex); write1(pos); \
if (pass==2 && foundlabel && (pos<-128 || pos>127)) error(2, S"Relative branch out of bounds (distance is "+dec(pos)+")"); \
return true; } while(0)
#define op0(str, hex) if (isop(str)) w0(hex)
#define op1(str, hex) if (isop(str)) w1(hex)
#define op2(str, hex) if (isop(str)) w2(hex)
#define opv(str, hex1, hex2) if (isop(str)) wv(hex1, hex2)
#define opr(str, hex) if (isop(str)) wr(hex)
#define match(left, right) if (ismatch(left, right))
eq("a")
{
op0("asl", 0x1C);
op0("das", 0xBE);
op0("daa", 0xDF);
op0("dec", 0x9C);
op0("inc", 0xBC);
op0("lsr", 0x5C);
op0("pop", 0xAE);
op0("push", 0x2D);
op0("rol", 0x3C);
op0("ror", 0x7C);
op0("xcn", 0x9F);
}
eq("x")
{
op0("dec", 0x1D);
op0("inc", 0x3D);
op0("pop", 0xCE);
op0("push", 0x4D);
}
eq("y")
{
op0("dec", 0xDC);
op0("inc", 0xFC);
op0("pop", 0xEE);
op0("push", 0x6D);
}
eq("p")
{
op0("pop", 0x8E);
op0("push", 0x0D);
}
if (isop("mul") && isam("ya")) w0(0xCF);
if (isop("jmp") && ismatch("(", "+x)")) w2(0x1F);
match("", "+x")
{
op1("asl", 0x1B);
op1("dec", 0x9B);
op1("inc", 0xBB);
op1("lsr", 0x5B);
op1("rol", 0x3B);
op1("ror", 0x7B);
}
if (bitmatch(word[0], op, arg[0], math, bits))
{
if (assinglebitwithc(op, math, bits)) return true;
else if (!stricmp(op, "set")) write1(0x02|(bits<<5));
else if (!stricmp(op, "clr")) write1(0x12|(bits<<5));
else return false;
int num=getnum(math);
if (num<0 || num>=0x100) error(2, "Address out of bounds");
write1(num);
return true;
}
if (true)
{
math=arg[0];
if (isop("tcall"))
{
int num=getnum(math);
if (num<0 || num>=16) error(2, "Invalid tcall");
write1((num<<4)|1);
return true;
}
opv("asl", 0x0B, 0x0C);
opv("dec", 0x8B, 0x8C);
opv("inc", 0xAB, 0xAC);
opv("lsr", 0x4B, 0x4C);
opv("rol", 0x2B, 0x2C);
opv("ror", 0x6B, 0x6C);
op2("jmp", 0x5F);
op2("call", 0x3F);
op1("decw", 0x1A);
op1("incw", 0x3A);
op1("pcall", 0x4F);
opr("bpl", 0x10);
opr("bra", 0x2F);
opr("bmi", 0x30);
opr("bvc", 0x50);
opr("bvs", 0x70);
opr("bcc", 0x90);
opr("bcs", 0xB0);
opr("bne", 0xD0);
opr("beq", 0xF0);
}
#undef isop
#undef isam
#undef eq
#undef w0
#undef w1
#undef w2
#undef wv
#undef wr
#undef op0
#undef op1
#undef op2
#undef opv
#undef opr
#undef match
return false;
}
if (numwords==2)
{
#define iscc(str1, str2) (!stricmp(arg[0], str1) && !stricmp(arg[1], str2))
#define iscv(str1, left2, right2) (!stricmp(arg[0], str1) && matchandwrite(arg[1], left2, right2, s2))
#define isvc(left1, right1, str2) (matchandwrite(arg[0], left1, right1, s1) && !stricmp(arg[1], str2))
#define isvv(left1, right1, left2, right2) (matchandwrite(arg[0], left1, right1, s1) && matchandwrite(arg[1], left2, right2, s2))
#define cc(str1, str2) if (iscc(str1, str2))
#define cv(str1, left2, right2) if (iscv(str1, left2, right2))
#define vc(left1, right1, str2) if (isvc(left1, right1, str2))
#define vv(left1, right1, left2, right2) if (isvv(left1, right1, left2, right2))
#define w0(opcode) do { write1(opcode); return true; } while(0)
#define w1(opcode, math) do { write1(opcode); int val=getnum(math); \
if (val&0xFF00) warn0("This opcode does not exist with 16-bit parameters, assuming 8-bit"); write1(val); return true; } while(0)
#define w2(opcode, math) do { write1(opcode); write2(getnum(math)); return true; } while(0)
#define wv(opcode1, opcode2, math) do { if (getlen(math)==1) { write1(opcode1); write1(getnum(math)); } \
else { write1(opcode2); write2(getnum(math)); } return true; } while(0)
#define w11(opcode, math1, math2) do { write1(opcode); write1(getnum(math1)); write1(getnum(math2)); return true; } while(0)
#define wr(opcode, math) do { int len=getlen(math); int num=getnum(math); int pos=(len==1)?num:num-(snespos+2); \
if (pass && foundlabel && (pos<-128 || pos>127)) error(2, S"Relative branch out of bounds (distance is "+dec(pos)+")"); \
write1(opcode); write1(pos); return true; } while(0)
#define w1r(opcode, math1, math2) do { int len=getlen(math2); int num=getnum(math2); int pos=(len==1)?num:num-(snespos+3); \
if (pass && foundlabel && (pos<-128 || pos>127)) error(2, S"Relative branch out of bounds (distance is "+dec(pos)+")"); \
write1(opcode); write1(getnum(math1)); write1(pos); return true; } while(0)
string s1;
string s2;
string op;
string math;
int bits;
#define isop(test) (!stricmp(op, test))
if (!stricmp(arg[0], "c") && bitmatch(word[0], op, arg[1], math, bits))
{
if (assinglebitwithc(op, math, bits)) return true;
}
if (bitmatch(word[0], op, arg[0], s1, bits))
{
if (isop("mov") && !stricmp(arg[1], "c"))
{
int num=getnum(s1);
if (num>=0x2000) error(2, "Address out of bounds");
write1(0xCA);
write2((bits<<13)|num);
return true;
}
if(0);
else if (isop("bbs")) write1(0x03|(bits<<5));
else if (isop("bbc")) write1(0x13|(bits<<5));
else return false;
int num=getnum(s1);
if (num>=0x100) error(2, "Address out of bounds");
write1(num);
write1(getnum(arg[1])-(snespos+1));
return true;
}
#undef isop
if (is("mov"))
{
if (iscc("(x)+", "a")) error(0, "Use (x+) instead.");
cc("(x+)" , "a" ) w0(0xAF);
cc("(x)" , "a" ) w0(0xC6);
if (iscc("a", "(x)+")) error(0, "Use (x+) instead.");
cc("a" , "(x+)" ) w0(0xBF);
cc("a" , "(x)" ) w0(0xE6);
cc("a" , "x" ) w0(0x7D);
cc("a" , "y" ) w0(0xDD);
cc("x" , "a" ) w0(0x5D);
cc("x" , "sp" ) w0(0x9D);
cc("y" , "a" ) w0(0xFD);
cc("sp" , "x" ) w0(0xBD);
vc("(","+x)", "a" ) w1(0xC7, s1);
vc("(",")+y", "a" ) w1(0xD7, s1);
vc("","+x" , "a" ) wv(0xD4, 0xD5, s1);
vc("","+y" , "a" ) w2(0xD6, s1);
vc("","" , "a" ) wv(0xC4, 0xC5, s1);
vc("","+x" , "y" ) w1(0xDB, s1);
vc("","+y" , "x" ) w1(0xD9, s1);
vc("","" , "x" ) wv(0xD8, 0xC9, s1);
vc("","" , "y" ) wv(0xCB, 0xCC, s1);
cv("a" , "#","" ) w1(0xE8, s2);
cv("a" , "(","+x)") w1(0xE7, s2);
cv("a" , "","+x" ) wv(0xF4, 0xF5, s2);
cv("a" , "","+y" ) wv(0xF7, 0xF6, s2);
cv("a" , "","" ) wv(0xE4, 0xE5, s2);
cv("x" , "#","" ) w1(0xCD, s2);
cv("x" , "","+y" ) w1(0xF9, s2);
cv("x" , "","" ) wv(0xF8, 0xE9, s2);
cv("y" , "#","" ) w1(0x8D, s2);
cv("y" , "","+x" ) w1(0xFB, s2);
cv("y" , "","" ) wv(0xEB, 0xEC, s2);
vv("","" , "#","" ) w11(0x8F, s2, s1);
vv("","" , "","" ) w11(0xFA, s2, s1);
}
if (is("cmp"))
{
cv("x", "#","") w1(0xC8, s2);
cv("x", "","" ) wv(0x3E, 0x1E, s2);
cv("y", "#","") w1(0xAD, s2);
cv("y", "","" ) wv(0x7E, 0x5E, s2);
}
if (is("or") || is("and") || is("eor") || is("cmp") || is("adc") || is("sbc"))
{
int offset;
if (is("or" )) offset=0x00;
if (is("and")) offset=0x20;
if (is("eor")) offset=0x40;
if (is("cmp")) offset=0x60;
if (is("adc")) offset=0x80;
if (is("sbc")) offset=0xA0;
cc("a" , "(x)" ) w0(offset+0x06);
cc("(x)", "(y)" ) w0(offset+0x19);
cv("a" , "#","" ) w1(offset+0x08, s2);
cv("a" , "(","+x)") w1(offset+0x07, s2);
cv("a" , "(",")+y") w1(offset+0x17, s2);
cv("a" , "","+x" ) wv(offset+0x14, offset+0x15, s2);
cv("a" , "","+y" ) w2(offset+0x16, s2);
cv("a" , "","" ) wv(offset+0x04, offset+0x05, s2);
vv("","", "#","" ) w11(offset+0x18, s2, s1);
vv("","", "","" ) w11(offset+0x09, s2, s1);
}
vc("","", "a")
{
if (is("tset")) w2(0x0E, s1);
if (is("tclr")) w2(0x4E, s1);
}
if (is("div") && iscc("ya", "x")) w0(0x9E);
cv("ya", "","")
{
if (is("cmpw")) w1(0x5A, s2);
if (is("addw")) w1(0x7A, s2);
if (is("subw")) w1(0x9A, s2);
if (is("movw")) w1(0xBA, s2);
}
if (is("movw") && isvc("","", "ya")) w1(0xDA, s1);
if (is("cbne") && isvv("","+x", "","")) w1r(0xDE, s1, s2);
if (is("dbnz") && iscv("y", "","")) wr(0xFE, s2);
vv("","", "","")
{
if (is("dbnz")) w1r(0x6E, s1, s2);
if (is("cbne")) w1r(0x2E, s1, s2);
}
#undef iscc
#undef iscv
#undef isvc
#undef isvv
#undef cc
#undef cv
#undef vc
#undef vv
#undef w0
#undef w1
#undef w2
#undef wv
#undef w11
return false;
}
return false;
}
else return false;
return true;
}