-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharch-65816.cpp
More file actions
244 lines (239 loc) · 9 KB
/
arch-65816.cpp
File metadata and controls
244 lines (239 loc) · 9 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
#include "asar.h"
#define error error<errblock>
#define write1 write1_pick
void asinit_65816()
{
}
void asend_65816()
{
}
extern int optimizeforbank;
extern bool fastrom;
extern bool emulatexkas;
bool asblock_65816(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]
char * par=NULL;
if (word[1]) par=strdup(word[1]);
autoptr<char*> parptr=par;
int num;
int len=0;//declared here for A->generic fallback
if(0);
else if (assemblemapper(word, numwords)) {}
#define getvars(optbank) num=(pass!=0)?getnum(par):0; if (word[0][3]=='.') { len=getlenfromchar(word[0][4]); word[0][3]='\0'; } else len=getlen(par, optbank)
#define match(left, right) (word[1] && stribegin(par, left) && striend(par, right))
#define init(left, right) itrim(par, left, right); getvars(false)
#define init2(left, right) itrim(par, left, right); getvars(true)
#define bankoptinit(left, right) itrim(par, left, right); getvars(true)
#define blankinit() len=1; num=0
#define end() return false
#define as0( op, byte) if (is(op) ) { write1(byte); return true; }
#define as1( op, byte) if (is(op) && len==1) { write1(byte); write1(num); return true; }
#define as2( op, byte) if (is(op) && len==2) { write1(byte); write2(num); return true; } \
/*if (is(op) && len==3 && emulate) { write1(byte); write2(num); return true; }*/
#define as12( op, byte) if (is(op) && (len==1 || len==2)) { write1(byte); write2(num); return true; }
#define as3( op, byte) if (is(op) && len==3) { write1(byte); write3(num); return true; }
#define as23( op, byte) if (is(op) && (len==2 || len==3)) { write1(byte); write2(num); return true; }
#define as32( op, byte) if (is(op) && (len==2 || len==3)) { write1(byte); write3(num); return true; }
#define as_a( op, byte) if (is(op)) { if (len==1) { write1(byte); write1(num); } \
else { write1(byte); write2(num); } return true; }
#define as_xy( op, byte) if (is(op)) { if (len==1) { write1(byte); write1(num); } \
else { write1(byte); write2(num); } return true; }
#define as_rep( op, byte) if (is(op)) { if (pass==0) num=getnum(par); for (int i=0;i<num;i++) { write1(byte); } return true; }
#define as_rel1(op, byte) if (is(op)) { int pos=(!foundlabel)?num:num-((snespos&0xFFFFFF)+2); write1(byte); write1(pos); \
if (pass==2 && foundlabel && (pos<-128 || pos>127)) error(2, S"Relative branch out of bounds (distance is "+dec(pos)+")"); \
return true; }
#define as_rel2(op, byte) if (is(op)) { int pos=(!foundlabel)?num:num-((snespos&0xFFFFFF)+3); write1(byte); write2(pos);\
if (pass==2 && foundlabel && (pos<-32768 || pos>32767)) error(2, S"Relative branch out of bounds (distance is "+dec(pos)+")"); \
return true; }
#define the8(offset, len) as##len("ORA", offset+0x00); as##len("AND", offset+0x20); as##len("EOR", offset+0x40); as##len("ADC", offset+0x60); \
as##len("STA", offset+0x80); as##len("LDA", offset+0xA0); as##len("CMP", offset+0xC0); as##len("SBC", offset+0xE0)
#define thenext8(offset, len) as##len("ASL", offset+0x00); as##len("BIT", offset+0x1E); as##len("ROL", offset+0x20); as##len("LSR", offset+0x40); \
as##len("ROR", offset+0x60); as##len("LDY", offset+0x9E); as##len("DEC", offset+0xC0); as##len("INC", offset+0xE0)
#define thefinal7(offset, len) as##len("TSB", offset+0x00); as##len("TRB", offset+0x10); as##len("STY", offset+0x80); as##len("STX", offset+0x82); \
as##len("LDX", offset+0xA2); as##len("CPY", offset+0xC0); as##len("CPX", offset+0xE0)
#define onlythe8(left, right, offset) else if (match(left, right)) do { init(left, right); the8(offset, 1); end(); } while(0)
else if ((strlen(word[0])!=3 && (strlen(word[0])!=5 || word[0][3]!='.')) || (word[1] && word[2])) return false;
else if (!word[1])
{
blankinit();
as0("PHP", 0x08); as0("ASL", 0x0A); as0("PHD", 0x0B); as0("CLC", 0x18);
as0("INC", 0x1A); as0("TCS", 0x1B); as0("PLP", 0x28); as0("ROL", 0x2A);
as0("PLD", 0x2B); as0("SEC", 0x38); as0("DEC", 0x3A); as0("TSC", 0x3B);
as0("RTI", 0x40); as0("PHA", 0x48); as0("LSR", 0x4A); as0("PHK", 0x4B);
as0("CLI", 0x58); as0("PHY", 0x5A); as0("TCD", 0x5B); as0("RTS", 0x60);
as0("PLA", 0x68); as0("ROR", 0x6A); as0("RTL", 0x6B); as0("SEI", 0x78);
as0("PLY", 0x7A); as0("TDC", 0x7B); as0("DEY", 0x88); as0("TXA", 0x8A);//these tables are blatantly stolen from xkas
as0("PHB", 0x8B); as0("TYA", 0x98); as0("TXS", 0x9A); as0("TXY", 0x9B);
as0("TAY", 0xA8); as0("TAX", 0xAA); as0("PLB", 0xAB); as0("CLV", 0xB8);
as0("TSX", 0xBA); as0("TYX", 0xBB); as0("INY", 0xC8); as0("DEX", 0xCA);
as0("WAI", 0xCB); as0("CLD", 0xD8); as0("PHX", 0xDA); as0("STP", 0xDB);
as0("INX", 0xE8); as0("NOP", 0xEA); as0("XBA", 0xEB); as0("SED", 0xF8);
as0("PLX", 0xFA); as0("XCE", 0xFB);
as1("BRK", 0x00); as1("COP", 0x02); as1("WDM", 0x42);
//as0("DEA", 0x3A); as0("INA", 0x1A); as0("TAD", 0x5B); as0("TDA", 0x7B);//nobody cares about these, but keeping them does no harm
//as0("TAS", 0x1B); as0("TSA", 0x3B); as0("SWA", 0xEB); //actually, it does: it may make some users think it's correct.
end();
}
else if (!stricmp(word[1], "A"))
{
blankinit();
as0("ASL", 0x0A); as0("LSR", 0x4A); as0("ROL", 0x2A); as0("ROR", 0x6A);
as0("INC", 0x1A); as0("DEC", 0x3A);
goto opAFallback;//yay goto
}
else if (match("#", ""))
{
bankoptinit("#", "");
as_a("ORA", 0x09); as_a("AND", 0x29); as_a("EOR", 0x49); as_a("ADC", 0x69);
as_a("BIT", 0x89); as_a("LDA", 0xA9); as_a("CMP", 0xC9); as_a("SBC", 0xE9);
as_xy("CPX", 0xE0); as_xy("CPY", 0xC0); as_xy("LDX", 0xA2); as_xy("LDY", 0xA0);
as_rep("ASL", 0x0A); as_rep("LSR", 0x4A); as_rep("ROL", 0x2A); as_rep("ROR", 0x6A);
as_rep("INC", 0x1A); as_rep("DEC", 0x3A); as_rep("INX", 0xE8); as_rep("DEX", 0xCA);
as_rep("INY", 0xC8); as_rep("DEY", 0x88); as_rep("NOP", 0xEA);
as1("REP", 0xC2); as1("SEP", 0xE2);
as1("BRK", 0x00); as1("COP", 0x02); as1("WDM", 0x42);
end();
}
onlythe8("(", ",s),y", 0x13);
onlythe8("[", "],y", 0x17);
onlythe8("(", "),y", 0x11);
onlythe8("", ",s", 0x03);
else if (match("[", "]"))
{
init("[", "]");
the8(0x07, 1);
as2("JMP", 0xDC); as2("JML", 0xDC);
end();
}
else if (match("(", ",x)"))
{
init("(", ",x)");
the8(0x01, 1);
as23("JMP", 0x7C); as23("JSR", 0xFC);
end();
}
else if (match("(", ")") && confirmqpar(substr(word[1]+1, strlen(word[1]+1)-1)))
{
init2("(", ")");
the8(0x12, 1);
as1("PEI", 0xD4);
as12("JMP", 0x6C);
end();
}
else if (match("", ",x"))
{
init("", ",x");
if (match("(", ")") && confirmqpar(substr(word[1]+1, strlen(word[1]+1)-2-1))) warn0("($yy),x does not exist, assuming $yy,x");
the8(0x1F, 3);
the8(0x1D, 2);
the8(0x15, 1);
thenext8(0x16, 1);
thenext8(0x1E, 2);
as1("STZ", 0x74);
as1("STY", 0x94);
as2("STZ", 0x9E);
end();
}
else if (match("", ",y"))
{
init("", ",y");
if (len==3 && emulatexkas) len=2;
as1("LDX", 0xB6);
as1("STX", 0x96);
as2("LDX", 0xBE);
if (len==1 && (is("ORA") || is("AND") || is("EOR") || is("ADC") || is("STA") || is("LDA") || is("CMP") || is("SBC")))
{
warn0(S word[0]+" $xx,y is not valid with 8-bit parameters, assuming 16-bit");
len=2;
}
the8(0x19, 2);
end();
}
else
{
if ((is("MVN") || is("MVP")) && confirmqpar(par))
{
int num;
autoptr<char**>param=qpsplit(par, ",", &num);
if (num==2)
{
write1(is("MVN")?0x54:0x44);
write1(getnum(param[0]));
write1(getnum(param[1]));
return true;
}
}
if (false)
{
opAFallback:
unsigned int tmp=0;
if (pass && !labelval(par, &tmp)) return false;
len=getlen(par);
num=tmp;
}
if (is("JSR") || is("JMP"))
{
int tmp=optimizeforbank;
optimizeforbank=-1;
init("", "");
optimizeforbank=tmp;
}
else
{
init("", "");
}
the8(0x0F, 3);
the8(0x0D, 2);
the8(0x05, 1);
thenext8(0x06, 1);
thenext8(0x0E, 2);
thefinal7(0x04, 1);
thefinal7(0x0C, 2);
as1("STZ", 0x64);
as2("STZ", 0x9C);
as2("JMP", 0x4C);
as2("JSR", 0x20);
as32("JML", 0x5C);
as32("JSL", 0x22);
as2("MVN", 0x54);
as2("MVP", 0x44);
as2("PEA", 0xF4);
if (emulatexkas)
{
as3("JMP", 0x5C);//all my hate
//as3("JSR", 0x22);
}
// if (is("BRA"))
// {
//puts("");
//puts(hex8(num)+"-"+hex8(snespos));
// int pos=(!foundlabel)?num:num-((snespos&0xFFFFFF)+2);
//puts(hex8(pos));
// write1(0x80);
// write1(pos);
// if (pass==2 && foundlabel && (pos<-128 || pos>127)) error("Relative branch out of bounds");
// return true;
// }
as_rel1("BRA", 0x80);
as_rel1("BCC", 0x90);
as_rel1("BCS", 0xB0);
as_rel1("BEQ", 0xF0);
as_rel1("BNE", 0xD0);
as_rel1("BMI", 0x30);
as_rel1("BPL", 0x10);
as_rel1("BVC", 0x50);
as_rel1("BVS", 0x70);
as_rel2("BRL", 0x82);
as_rel2("PER", 0x62);
end();
}
return true;
}