forked from philpem/m68emu
-
Notifications
You must be signed in to change notification settings - Fork 1
/
m68emu.c
285 lines (241 loc) · 6.96 KB
/
m68emu.c
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
#include <assert.h>
#include <string.h>
#include <stdio.h>
#include "m68emu.h"
#include "m68_internal.h"
void m68_init(M68_CTX *ctx, const M68_CPUTYPE cpuType)
{
switch (cpuType) {
case M68_CPU_HC05C4:
// 68HC05SC21 is based on the 68HC05C4 core.
// SP is 13 bits.
// 7 MSBs are permanently set to 0000011
// 6 LSBs are passed through
// Address range: 0xC0 to 0xFF
ctx->sp_and = 0x003F;
ctx->sp_or = 0x00C0;
// PC is 13 bits too
ctx->pc_and = 0x1FFF;
break;
default:
assert(0);
}
ctx->cpuType = cpuType;
ctx->trace = false;
m68_reset(ctx);
}
void m68_reset(M68_CTX *ctx)
{
// Read the reset vector
ctx->reg_pc = _M68_RESET_VECTOR & ctx->pc_and;
uint16_t rstvec = (uint16_t)ctx->read_mem(ctx, ctx->reg_pc) << 8;
rstvec |= ctx->read_mem(ctx, ctx->reg_pc+1);
// Set PC to the reset vector
ctx->reg_pc = rstvec & ctx->pc_and;
ctx->pc_next = ctx->reg_pc;
// Reset stack pointer to 0xFF
ctx->reg_sp = 0xFF;
// Set the I bit in the CCR to 1 (mask off interrupts)
ctx->reg_ccr |= M68_CCR_I;
// Clear STOP and WAIT latches
ctx->is_stopped = ctx->is_waiting = 0;
// Clear external interrupt latch
ctx->irq = 0;
}
uint64_t m68_exec_cycle(M68_CTX *ctx)
{
uint8_t opval;
M68_OPTABLE_ENT *opcode;
// Save current program counter
ctx->reg_pc = ctx->pc_next;
// Fetch and decode opcode
opval = ctx->read_mem(ctx, ctx->pc_next++);
if (ctx->opdecode != NULL) {
opval = ctx->opdecode(ctx, opval);
}
switch (ctx->cpuType) {
case M68_CPU_HC05C4:
opcode = &m68hc05_optable[opval];
break;
default:
assert(0);
}
if (ctx->trace) {
printf("M68 EXEC: pc %04X sp %02X opval %02X mnem '%s' amode %d cycles %d\n",
ctx->reg_pc, ctx->reg_sp, opval, opcode->mnem, opcode->amode, opcode->cycles);
}
// Read the opcode parameter bytes, if any
uint8_t opParam; // parameter
uint16_t dirPtr; // direct pointer
uint16_t opNextPC; // next PC (if branch or jump)
bool opResult;
switch(opcode->amode) {
case AMODE_DIRECT:
// Direct addressing: parameter is an address in zero page
dirPtr = ctx->read_mem(ctx, ctx->pc_next++);
if (!opcode->write_only) {
opParam = ctx->read_mem(ctx, dirPtr);
}
break;
case AMODE_DIRECT_JUMP:
// Direct addressing, jump
opNextPC = ctx->read_mem(ctx, ctx->pc_next++);
opParam = -1;
break;
case AMODE_DIRECT_REL:
// Direct + relative addressing: parameter is an address in zero page
// followed by a relative jump address.
// Direct
dirPtr = ctx->read_mem(ctx, ctx->pc_next++);
opParam = ctx->read_mem(ctx, dirPtr);
// Relative
opNextPC = ctx->pc_next + 1;
opNextPC += (int8_t)ctx->read_mem(ctx, ctx->pc_next++);
break;
case AMODE_EXTENDED:
// Extended addressing: parameter is a 16-bit address
dirPtr = (uint16_t)ctx->read_mem(ctx, ctx->pc_next++) << 8;
dirPtr |= ctx->read_mem(ctx, ctx->pc_next++);
if (!opcode->write_only) {
opParam = ctx->read_mem(ctx, dirPtr);
}
break;
case AMODE_EXTENDED_JUMP:
// Extended addressing, jump
opNextPC = (uint16_t)ctx->read_mem(ctx, ctx->pc_next++) << 8;
opNextPC |= ctx->read_mem(ctx, ctx->pc_next++);
opParam = -1;
break;
case AMODE_IMMEDIATE:
// Immediate addressing: parameter is an immediate value following the opcode
opParam = ctx->read_mem(ctx, ctx->pc_next++);
break;
case AMODE_INDEXED0:
// Indexed with no offset. Take the X register as an address.
dirPtr = ctx->reg_x;
if (!opcode->write_only) {
opParam = ctx->read_mem(ctx, dirPtr);
}
break;
case AMODE_INDEXED0_JUMP:
// Indexed jump with no offset. Take the X register as an address.
opNextPC = ctx->reg_x;
opParam = -1;
break;
case AMODE_INDEXED1:
// Indexed with 1-byte offset. Add X and offset.
dirPtr = (uint16_t)ctx->read_mem(ctx, ctx->pc_next++) + ctx->reg_x;
if (!opcode->write_only) {
opParam = ctx->read_mem(ctx, dirPtr);
}
break;
case AMODE_INDEXED1_JUMP:
// Indexed jump with 1-byte offset. Take the X register as an address.
opNextPC = (uint16_t)ctx->read_mem(ctx, ctx->pc_next++) + ctx->reg_x;
opParam = -1;
break;
case AMODE_INDEXED2:
// Indexed with 2-byte offset. Add X and offset.
dirPtr = (uint16_t)ctx->read_mem(ctx, ctx->pc_next++) << 8;
dirPtr |= ctx->read_mem(ctx, ctx->pc_next++);
dirPtr += ctx->reg_x;
if (!opcode->write_only) {
opParam = ctx->read_mem(ctx, dirPtr);
}
break;
case AMODE_INDEXED2_JUMP:
// Indexed jump with 2-byte offset. Add X and offset.
opNextPC = (uint16_t)ctx->read_mem(ctx, ctx->pc_next++) << 8;
opNextPC |= ctx->read_mem(ctx, ctx->pc_next++);
opNextPC += ctx->reg_x;
opParam = -1;
break;
case AMODE_INHERENT:
// Inherent addressing, affects nothing.
opParam = -1;
break;
case AMODE_INHERENT_A:
// Inherent addressing, affects Accumulator.
opParam = ctx->reg_acc;
break;
case AMODE_INHERENT_X:
// Inherent addressing, affects X register.
opParam = ctx->reg_x;
break;
case AMODE_RELATIVE:
// Relative addressing: signed relative branch or jump.
opNextPC = ctx->pc_next + 1;
opNextPC += (int8_t)ctx->read_mem(ctx, ctx->pc_next++);
break;
case AMODE_ILLEGAL:
case AMODE_MAX:
// Illegal instruction
assert(1==2);
break;
}
// Execute opcode
opResult = opcode->opfunc(ctx, opval, &opParam);
if (ctx->trace) {
if (opResult) {
printf("\t-> %3d (0x%02X)\n", opParam, opParam);
}
}
// Write back result (param)
switch(opcode->amode) {
case AMODE_DIRECT:
case AMODE_EXTENDED:
case AMODE_INDEXED0:
case AMODE_INDEXED1:
case AMODE_INDEXED2:
// Direct addressing: parameter is an address in zero page
// Extended addressing: parameter is a 16-bit address
// Indexed with no offset. Take the X register as an address.
// Indexed with 1-byte offset. Add X and offset.
// Indexed with 2-byte offset. Add X and offset.
if (opResult) {
ctx->write_mem(ctx, dirPtr, opParam);
}
break;
case AMODE_DIRECT_JUMP:
case AMODE_EXTENDED_JUMP:
case AMODE_INDEXED0_JUMP:
case AMODE_INDEXED1_JUMP:
case AMODE_INDEXED2_JUMP:
case AMODE_DIRECT_REL:
case AMODE_RELATIVE:
// Direct + relative addressing: parameter is an address in zero page
// followed by a signed relative jump address.
// Relative addressing: signed relative branch or jump.
//
// If the opfunc returned true, take the jump.
if (opResult) {
ctx->pc_next = opNextPC & ctx->pc_and;
}
break;
case AMODE_IMMEDIATE:
// Immediate addressing: parameter is an immediate value and cannot be written back.
break;
case AMODE_INHERENT:
// Inherent addressing, affects nothing.
break;
case AMODE_INHERENT_A:
// Inherent addressing, affects Accumulator.
if (opResult) {
ctx->reg_acc = opParam;
}
break;
case AMODE_INHERENT_X:
// Inherent addressing, affects X register.
if (opResult) {
ctx->reg_x = opParam;
}
break;
case AMODE_ILLEGAL:
case AMODE_MAX:
// Illegal instruction
assert(1==2);
break;
}
// Return number of cycles executed
return opcode->cycles;
}