forked from spc476/mc6809
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmc09emulator.c
333 lines (273 loc) · 8.33 KB
/
mc09emulator.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
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
/********************************************
*
* Copyright 2012 by Sean Conner. All Rights Reserved.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 3 of the License, or (at your
* option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, see <http://www.gnu.org/licenses/>.
*
* Comments, questions and criticisms can be sent to: sean@conman.org
*
*********************************************/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "mc6809.h"
#include "mc6809dis.h"
/*************************************************************************/
struct userdata
{
mc6809byte__t *memory;
bool *readonly;
mc6809__t cpu;
mc6809dis__t dis;
};
/************************************************************************/
static bool loadimage (struct userdata *,char const *restrict,char const *restrict);
static bool dumpcore (struct userdata *);
static mc6809byte__t cpu_read (mc6809__t *,mc6809addr__t,bool);
static void cpu_write (mc6809__t *,mc6809addr__t,mc6809byte__t);
static void cpu_fault (mc6809__t *,mc6809fault__t);
static mc6809byte__t dis_read (mc6809dis__t *,mc6809addr__t);
static void dis_fault (mc6809dis__t *,mc6809fault__t);
/************************************************************************/
int main(int argc,char *argv[])
{
struct userdata ud;
int rc;
if (argc == 1)
{
fprintf(stderr,"usage: %s imagefile loadaddr ... \n",argv[0]);
fprintf(stderr,"\tloadaddr = HHHH[r] where H = hexdigit,\n");
fprintf(stderr,"\t and r = r (for read only)\n");
return EXIT_FAILURE;
}
ud.memory = malloc(65536uL * sizeof(mc6809byte__t));
if (ud.memory == NULL)
{
perror("malloc()");
return EXIT_FAILURE;
}
ud.readonly = calloc(65536uL,sizeof(bool));
if (ud.readonly == NULL)
{
perror("malloc()");
free(ud.memory);
return EXIT_FAILURE;
}
ud.cpu.user = &ud;
ud.cpu.read = cpu_read;
ud.cpu.write = cpu_write;
ud.cpu.fault = cpu_fault;
ud.dis.user = &ud;
ud.dis.read = dis_read;
ud.dis.fault = dis_fault;
for ( int i = 1 ; i < argc ; i += 2 )
{
if (!loadimage(&ud,argv[i],argv[i + 1]))
{
perror(argv[i]);
free(ud.readonly);
free(ud.memory);
return EXIT_FAILURE;
}
}
mc6809_reset(&ud.cpu);
rc = mc6809_run(&ud.cpu);
if (rc != 0)
{
switch(rc)
{
default:
case MC6809_FAULT_INTERNAL_ERROR:
fprintf(stderr,"An internal error occured that should not happen.\n");
break;
case MC6809_FAULT_INSTRUCTION:
fprintf(stderr,"Illegal instruction @ %04X\n",ud.cpu.pc.w - 1);
break;
case MC6809_FAULT_ADDRESS_MODE:
fprintf(stderr,"Illegal addressing mode in instruction @ %04X\n",ud.cpu.instpc);
break;
case MC6809_FAULT_EXG:
fprintf(stderr,"Undefined EXG behavior in instruction @ %04X\n",ud.cpu.instpc);
break;
case MC6809_FAULT_TFR:
fprintf(stderr,"Undefined TFR behavior in instruction @ %04X\n",ud.cpu.instpc);
break;
case MC6809_FAULT_user:
fprintf(stderr,
"This can be used to signal other errors, but you should\n"
"only see this if you check the source code.\n"
);
break;
}
dumpcore(&ud);
}
free(ud.readonly);
free(ud.memory);
return rc;
}
/*********************************************************************/
static bool loadimage(
struct userdata *ud,
char const *restrict filename,
char const *restrict taddr
)
{
mc6809addr__t addr;
FILE *fp;
char *ro;
int c;
assert(ud != NULL);
assert(filename != NULL);
assert(taddr != NULL);
addr = strtoul(taddr,&ro,16);
if (*ro != 'r')
ro = NULL;
fp = fopen(filename,"rb");
if (fp == NULL)
return false;
while((c = fgetc(fp)) != EOF)
{
ud->memory[addr] = c;
if (ro)
ud->readonly[addr] = true;
addr++;
}
fclose(fp);
return true;
}
/***********************************************************************/
static bool dumpcore(struct userdata *ud)
{
FILE *fp;
fp = fopen("mc6809-core","wb");
if (fp != NULL)
{
fwrite(ud->memory,1,65536uL,fp);
fputc(mc6809_cctobyte(&ud->cpu),fp);
fwrite(&ud->cpu.A,1,1,fp);
fwrite(&ud->cpu.B,1,1,fp);
fwrite(&ud->cpu.dp,1,1,fp);
fwrite(&ud->cpu.X.b[MSB],1,1,fp);
fwrite(&ud->cpu.X.b[LSB],1,1,fp);
fwrite(&ud->cpu.Y.b[MSB],1,1,fp);
fwrite(&ud->cpu.Y.b[LSB],1,1,fp);
fwrite(&ud->cpu.U.b[MSB],1,1,fp);
fwrite(&ud->cpu.U.b[LSB],1,1,fp);
fwrite(&ud->cpu.S.b[MSB],1,1,fp);
fwrite(&ud->cpu.S.b[LSB],1,1,fp);
fwrite(&ud->cpu.pc.b[MSB],1,1,fp);
fwrite(&ud->cpu.pc.b[LSB],1,1,fp);
fclose(fp);
return true;
}
else
return false;
}
/***********************************************************************/
static mc6809byte__t cpu_read(
mc6809__t *cpu,
mc6809addr__t addr,
bool ifetch __attribute__((unused))
)
{
struct userdata *ud;
char inst[128];
char regs[128];
assert(cpu != NULL);
assert(cpu->user != NULL);
ud = cpu->user;
assert(ud->memory != NULL);
if (cpu->instpc == addr) /* start of an instruction */
{
ud->dis.pc = addr;
mc6809dis_step(&ud->dis,cpu);
mc6809dis_format(&ud->dis,inst,sizeof(inst));
mc6809dis_registers(cpu,regs,sizeof(regs));
printf("%s | %s\n",regs,inst);
}
return ud->memory[addr];
}
/************************************************************************/
static void cpu_write(
mc6809__t *cpu,
mc6809addr__t addr,
mc6809byte__t b
)
{
struct userdata *ud;
assert(cpu != NULL);
assert(cpu->user != NULL);
ud = cpu->user;
assert(ud->memory != NULL);
assert(ud->readonly != NULL);
if (!ud->readonly[addr])
ud->memory[addr] = b;
}
/************************************************************************/
static void cpu_fault(
mc6809__t *cpu,
mc6809fault__t fault
)
{
assert(cpu != NULL);
longjmp(cpu->err,fault);
}
/***********************************************************************/
static mc6809byte__t dis_read(
mc6809dis__t *dis,
mc6809addr__t addr
)
{
struct userdata *ud;
assert(dis != NULL);
assert(dis->user != NULL);
ud = dis->user;
assert(ud->memory != NULL);
return ud->memory[addr];
}
/***********************************************************************/
static void dis_fault(
mc6809dis__t *dis,
mc6809fault__t fault
)
{
assert(dis != NULL);
switch(fault)
{
default:
case MC6809_FAULT_INTERNAL_ERROR:
fprintf(stderr,"An internal error occured during disassembly that should not happen.\n");
break;
case MC6809_FAULT_INSTRUCTION:
fprintf(stderr,"Illegal instruction decoded @ %04X\n",dis->next - 1);
break;
case MC6809_FAULT_ADDRESS_MODE:
fprintf(stderr,"Illegal addressing mode decoded @ %04X\n",dis->pc);
break;
case MC6809_FAULT_EXG:
fprintf(stderr,"Undefined EXG behavior decoded @ %04X\n",dis->pc);
break;
case MC6809_FAULT_TFR:
fprintf(stderr,"Undefined TFR behavior decoded @ %04X\n",dis->pc);
break;
case MC6809_FAULT_user:
fprintf(stderr,
"This can be used to signal other errors, but you should\n"
"only see this if you check the source code.\n"
);
break;
}
longjmp(dis->err,fault);
}
/***********************************************************************/