summaryrefslogtreecommitdiff
path: root/opcode.h
blob: 990df5a7376ec00c6fd85cad0913e1b7a4aa4bc6 (plain)
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
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#define OPNAME(opcode) [opcode] = #opcode /* Get name of Opcode, for dissambly. */
#define CPS 0x00 /* Clear Processor Status. */
#define ADC 0x01 /* ADd with Carry. */
#define PHP 0x08 /* PusH Processor status to stack. */
#define PHA 0x09 /* PusH Accumulator to stack. */
#define PHY 0x0A /* PusH Y register to stack. */
#define TAY 0x0B /* Transfer Accumulator to Y. */
#define PHX 0x0C /* PusH X register to stack. */
#define TAX 0x0D /* Transfer Accumulator to X. */
#define TYX 0x0E /* Transfer Y to X. */
#define JMP 0x10 /* JuMP to memory location. */
#define SBC 0x11 /* SuBtract with Carry. */
#define PLP 0x18 /* PuLl Processor status from stack. */
#define PLA 0x19 /* PuLl Accumulator from stack. */
#define PLY 0x1A /* PuLl Y register from stack. */
#define TYA 0x1B /* Transfer Y to Accumulator. */
#define PLX 0x1C /* PuLl X register from stack. */
#define TXA 0x1D /* Transfer X to Accumulator. */
#define TXY 0x1E /* Transfer X to Y. */
#define JSR 0x20 /* Jump to SubRoutine. */
#define AND 0x21 /* bitwise AND with accumulator. */
#define ANY 0x22 /* bitwise ANd with Y register.*/
#define AAY 0x23 /* bitwise And with Accumulator, and Y register. */
#define ANX 0x24 /* bitwise ANd with X register. */
#define AAX 0x25 /* bitwise And with Accumulator, and X register. */
#define STT 0x28 /* STart Threads. */
#define TSX 0x2E /* Transfer Stack pointer to X. */
#define BPO 0x30 /* Branch if POsitive. */
#define ORA 0x31 /* bitwise OR with Accumulator. */
#define ORY 0x32 /* bitwise OR with Y register. */
#define OAY 0x33 /* bitwise Or with Accumulator, and Y register. */
#define ORX 0x34 /* bitwise OR with X register. */
#define OAX 0x35 /* bitwise Or with Accumulator, and X register. */
#define SEI 0x38 /* SEt Interupt flag. */
#define TXS 0x3E /* Transfer X to Stack pointer. */
#define BNG 0x40 /* Branch if NeGative. */
#define XOR 0x41 /* bitwise XOR with accumulator. */
#define XRY 0x42 /* bitwise XoR with Y register. */
#define XAY 0x43 /* bitwise Xor with Accumulator, and Y register. */
#define XRX 0x44 /* bitwise XoR with X register. */
#define XAX 0x45 /* bitwise Xor with Accumulator, and X register. */
#define CLI 0x48 /* CLear Interupt flag. */
#define BCS 0x50 /* Branch if Carry Set. */
#define LSL 0x51 /* Logical Shift Left. */
#define SEC 0x58 /* SEt Carry flag. */
#define STA 0x5B /* STore Accumulator. */
#define STY 0x5D /* STore Y register. */
#define STX 0x5E /* STore X register. */
#define BCC 0x60 /* Branch if Carry Clear. */
#define LSR 0x61 /* Logical Shift Right. */
#define CLC 0x68 /* CLear Carry flag. */
#define LDA 0x69 /* LoaD Accumulator. */
#define LDY 0x6A /* LoaD Y register. */
#define LDX 0x6C /* LoaD X register. */
#define BEQ 0x70 /* Branch if EQual. */
#define ROL 0x71 /* ROtate Left. */
#define SSP 0x78 /* Set Stack Protection flag. */
#define BNE 0x80 /* Branch if Not Equal. */
#define ROR 0x81 /* ROtate Right. */
#define CSP 0x88 /* Clear Stack Protection flag. */
#define BVS 0x90 /* Branch if oVerflow Set. */
#define MUL 0x91 /* MULtiply with accumulator. */
#define SEV 0x98 /* SEt oVerflow flag. */
#define BVC 0xA0 /* Branch if oVerflow Clear. */
#define DIV 0xA1 /* DIVide with accumulator. */
#define CLV 0xA8 /* CLear oVerflow flag. */
#define RTS 0xB0 /* ReTurn from Subroutine. */
#define CMP 0xB1 /* CoMPare accumulator. */
#define CPY 0xB2 /* ComPare Y register. */
#define CAY 0xB3 /* Compare Accumulator, and Y register. */
#define CPX 0xB4 /* ComPare X register. */
#define CAX 0xB5 /* Compare Accumulator, and X register. */
#define ENT 0xB8 /* ENd Threads. */
#define RTI 0xC0 /* ReTurn from Interupt. */
#define INC 0xC1 /* INCrement accumulator. */
#define INY 0xC2 /* INcrement Y register. */
#define IAY 0xC3 /* Increment Accumulator, and Y register. */
#define INX 0xC4 /* INcrement X register. */
#define IAX 0xC5 /* Increment Accumulator, and X register. */
#define DEC 0xD1 /* DECrement accumulator. */
#define DEY 0xD2 /* DEcrement Y register. */
#define DAY 0xD3 /* Decrement Accumulator, and Y register. */
#define DEX 0xD4 /* DEcrement X register. */
#define DAX 0xD5 /* Decrement Accumulator, and X register. */
#define JSL 0xE0 /* Jump to Subroutine Long. */
#define NOP 0xE8 /* No OPeration. */
#define RTL 0xF0 /* ReTurn from subroutine Long. */
#define BRK 0xF8 /* BReaK. */

#define C ((uint64_t)1 << 0)
#define Z ((uint64_t)1 << 1)
#define I ((uint64_t)1 << 2)
#define S ((uint64_t)1 << 3)
#define V ((uint64_t)1 << 6)
#define N ((uint64_t)1 << 7)

struct sux;

uint8_t *addr; /* Address Space. */
uint8_t ibcount; /* Number of bytes taken up by instruction. */

struct sux {
	uint64_t ps; /* The processor status register. */
	uint64_t a[8], y[8], x[8]; /* Registers A, X, and Y. */
	uint64_t pc[8]; /* Program counter. */
	uint16_t sp[8]; /* Stack pointer. */
	uint16_t stk_st[8]; /* Starting address of each threads stack. */
	uint8_t crt; /* Current running threads. */
	uint8_t c[8], z[8], i[8], s[8], v[8], n[8]; /* Processor Status Flags. */

};

typedef struct {
	char mnemonic[4];
	uint8_t imm;
	uint8_t abs;
	uint8_t zm;
	uint8_t zmy;
	uint8_t zmx;
	uint8_t impl;
} opent;

static const char *opname[0x100] = {
	OPNAME(CPS),
	[ADC] = "ADC #",
	[0x03] = "ADC a",
	[0x05] = "ADC zm",
	OPNAME(PHP),
	OPNAME(PHA),
	OPNAME(PHY),
	OPNAME(TAY),
	OPNAME(PHX),
	OPNAME(TAX),
	OPNAME(TYX),
	OPNAME(JMP),
	[SBC] = "SBC #",
	[0x13] = "SBC a",
	[0x15] = "SBC zm",
	OPNAME(PLP),
	OPNAME(PLA),
	OPNAME(PLY),
	OPNAME(TYA),
	OPNAME(PLX),
	OPNAME(TXA),
	OPNAME(TXY),
	OPNAME(JSR),
	[AND] = "AND #",
	[ANY] = "ANY #",
	OPNAME(AAY),
	[ANX] = "ANX #",
	OPNAME(AAX),
	OPNAME(STT),
	OPNAME(TSX),
	[0x29] = "AND a",
	[0x2B] = "AND zm",
	OPNAME(BPO),
	[ORA] = "ORA #",
	[ORY] = "ORY #",
	OPNAME(OAY),
	[ORX] = "ORX #",
	OPNAME(OAX),
	OPNAME(SEI),
	OPNAME(TXS),
	[0x39] = "ORA a",
	[0x3B] = "ORA zm",
	OPNAME(BNG),
	[XOR] = "XOR #",
	[XRY] = "XRY #",
	OPNAME(XAY),
	[XRX] = "XRX #",
	OPNAME(XAX),
	OPNAME(CLI),
	[0x49] = "XOR a",
	[0x4B] = "XOR zm",
	OPNAME(BCS),
	[LSL] = "LSL #",
	[0x52] = "ANY a",
	[0x53] = "LSL a",
	[0x54] = "ANX a",
	[0x55] = "LSL zm",
	OPNAME(SEC),
	[0x59] = "LDA a",
	[0x5A] = "LDY a",
	[STA] = "STA a",
	[0x5C] = "LDX a",
	[STY] = "STY a",
	[STX] = "STX a",
	OPNAME(BCC),
	[LSR] = "LSR #",
	[0x62] = "ORY a",
	[0x63] = "LSR a",
	[0x64] = "ORX a",
	[0x65] = "LSR zm",
	OPNAME(CLC),
	[LDA]= "LDA #",
	[LDY]= "LDY #",
	[LDX]= "LDX #",
	OPNAME(BEQ),
	[ROL] = "ROL #",
	[0x72] = "XRY a",
	[0x73] = "ROL a",
	[0x74] = "XRX a",
	[0x75] = "ROL zm",
	OPNAME(SSP),
	[0x79] = "LDA zm",
	[0x7A] = "LDY zm",
	[0x7B] = "STA zm",
	[0x7C] = "LDX zm",
	[0x7D] = "STY zm",
	[0x7E] = "STX zm",
	OPNAME(BNE),
	[ROR] = "ROR #",
	[0x82] = "ANY zm",
	[0x83] = "ROR a",
	[0x84] = "ANX zm",
	[0x85] = "ROR zm",
	OPNAME(CSP),
	[0x89] = "LDA zm, x",
	[0x8A] = "LDY zm, x",
	[0x8B] = "STA zm, x",
	[0x8D] = "STY zm, x",
	OPNAME(BVS),
	[MUL] = "MUL #",
	[0x92] = "ORY zm",
	[0x93] = "MUL a",
	[0x94] = "ORX zm",
	[0x95] = "MUL zm",
	OPNAME(SEV),
	[0x99] = "LDA zm, y",
	[0x9B] = "LDX zm, y",
	[0x9C] = "STA zm, y",
	[0x9E] = "STX zm, y",
	OPNAME(BVC),
	[DIV] = "DIV #",
	[0xA2] = "XRY zm",
	[0xA3] = "DIV a",
	[0xA4] = "XRX zm",
	[0xA5] = "DIV zm",
	OPNAME(CLV),
	OPNAME(RTS),
	[CMP] = "CMP #",
	[CPY] = "CPY #",
	OPNAME(CAY),
	[CPX] = "CPX #",
	OPNAME(CAX),
	OPNAME(ENT),
	OPNAME(RTI),
	[INC] = "INC A",
	OPNAME(INY),
	OPNAME(IAY),
	OPNAME(INX),
	OPNAME(IAX),
	[0xD0] = "JMP zm",
	[DEC] = "DEC A",
	OPNAME(DEY),
	OPNAME(DAY),
	OPNAME(DEX),
	OPNAME(DAX),
	OPNAME(JSL),
	[0xE1] = "INC a",
	[0xE2] = "CPY a",
	[0xE3] = "INC zm",
	[0xE4] = "CPX a",
	[0xE5] = "CMP a",
	OPNAME(NOP),
	OPNAME(RTL),
	[0xF1] = "DEC a",
	[0xF2] = "CPY zm",
	[0xF3] = "DEC zm",
	[0xF4] = "CPX zm",
	[0xF4] = "CMP zm",
	OPNAME(BRK),
};

extern int asmmon();
extern void adc(struct sux *cpu, uint64_t adr, uint8_t thread, uint8_t regsize);
extern void sbc(struct sux *cpu, uint64_t adr, uint8_t thread, uint8_t regsize);
extern void mul(struct sux *cpu, uint64_t adr, uint8_t thread, uint8_t regsize);
extern void divd(struct sux *cpu, uint64_t adr, uint8_t thread, uint8_t regsize);
extern uint64_t and(struct sux *cpu, uint64_t value, uint8_t thread);
extern void and_addr(struct sux *cpu, uint64_t* const reg, uint64_t adr, uint8_t thread, uint8_t regsize);
extern uint64_t or(struct sux *cpu, uint64_t value, uint8_t thread);
extern void or_addr(struct sux *cpu, uint64_t* const reg, uint64_t adr, uint8_t thread, uint8_t regsize);
extern uint64_t xor(struct sux *cpu, uint64_t value, uint8_t thread);
extern void xor_addr(struct sux *cpu, uint64_t* const reg, uint64_t adr, uint8_t thread, uint8_t regsize);
extern void rol(struct sux *cpu, uint64_t adr, uint8_t thread);
extern void ror(struct sux *cpu, uint64_t adr, uint8_t thread);
extern void lsl(struct sux *cpu, uint64_t adr, uint8_t thread);
extern void lsr(struct sux *cpu, uint64_t adr, uint8_t thread);
extern void inc(struct sux *cpu, uint64_t *reg, uint8_t thread);
extern void inc_addr(struct sux *cpu, uint64_t adr, uint8_t thread);
extern void dec(struct sux *cpu, uint64_t *reg, uint8_t thread);
extern void dec_addr(struct sux *cpu, uint64_t adr, uint8_t thread);
extern void stt(struct sux *cpu, uint8_t value);
extern void ent(struct sux *cpu, uint8_t value);
extern void ld(struct sux *cpu, uint64_t *reg, uint64_t adr, uint8_t thread, uint8_t regsize);
extern void st(struct sux *cpu, uint64_t *reg, uint64_t adr, uint8_t thread, uint8_t regsize);
extern void push(struct sux *cpu, uint8_t value);
extern uint8_t pull(struct sux *cpu);
extern void cmp_addr(struct sux *cpu, uint64_t reg, uint64_t adr, uint8_t thread, uint8_t regsize);
extern void cmp(struct sux *cpu, uint64_t reg1, uint64_t reg2, uint8_t thread);
extern void bfs(struct sux *cpu, uint8_t flag, uint64_t adr, uint8_t thread);
extern void bfc(struct sux *cpu, uint8_t flag, uint64_t adr, uint8_t thread);
extern void setps(struct sux *cpu, uint8_t thread);
extern uint64_t immaddr(struct sux *cpu, uint8_t thread, uint8_t size);
extern uint64_t absaddr(struct sux *cpu, uint8_t thread);
extern uint32_t zeromtx(struct sux *cpu, uint8_t thread);
extern uint32_t zeromx(struct sux *cpu, uint8_t thread);
extern uint32_t zeromy(struct sux *cpu, uint8_t thread);