0
|
1 /* 6809 Simulator V09,
|
|
2
|
|
3 created 1994 by L.C. Benschop.
|
|
4 copyleft (c) 1994-2014 by the sbc09 team, see AUTHORS for more details.
|
|
5 license: GNU General Public License version 2, see LICENSE for more details.
|
|
6
|
|
7 This program simulates a 6809 processor.
|
|
8
|
|
9 System dependencies: short must be 16 bits.
|
|
10 char must be 8 bits.
|
|
11 long must be more than 16 bits.
|
|
12 arrays up to 65536 bytes must be supported.
|
|
13 machine must be twos complement.
|
|
14 Most Unix machines will work. For MSODS you need long pointers
|
|
15 and you may have to malloc() the mem array of 65536 bytes.
|
|
16
|
|
17 Define CPU_BIG_ENDIAN with != 0 if you have a big-endian machine (680x0 etc)
|
|
18 Usually UNIX systems get this automatically from BIG_ENDIAN and BYTE_ORDER
|
|
19 definitions ...
|
|
20
|
|
21 Define TRACE if you want an instruction trace on stderr.
|
|
22 Define TERM_CONTROL if you want nonblocking non-echoing key input.
|
|
23 * THIS IS DIRTY !!! *
|
|
24
|
|
25 Special instructions:
|
|
26 SWI2 writes char to stdout from register B.
|
|
27 SWI3 reads char from stdout to register B, sets carry at EOF.
|
|
28 (or when no key available when using term control).
|
|
29 SWI retains its normal function.
|
|
30 CWAI and SYNC stop simulator.
|
|
31
|
|
32 The program reads a binary image file at $100 and runs it from there.
|
|
33 The file name must be given on the command line.
|
|
34
|
|
35 Revisions:
|
|
36 2012-06-05 johann AT klasek at
|
|
37 Fixed: com with C "NOT" operator ... 0^(value) did not work!
|
|
38 2012-06-06
|
|
39 Fixed: changes from 1994 release (flag handling)
|
|
40 reestablished.
|
|
41 2012-07-15 JK
|
|
42 New: option parsing, new option -d (dump memory on exit)
|
|
43 2013-10-07 JK
|
|
44 New: print ccreg with flag name in lower/upper case depending on flag state.
|
|
45 2013-10-20 JK
|
|
46 New: Show instruction disassembling in trace mode.
|
|
47 2014-07-01 JK
|
|
48 Fixed: disassembling output: cmpd
|
|
49 2014-07-11 JK
|
|
50 Fixed: undocumented tfr/exg register combinations.
|
|
51 http://www.6809.org.uk/dragon/illegal-opcodes.shtml
|
|
52 2016-10-06 JK
|
|
53 Fixed: wrong cmpu cycles
|
|
54 */
|
|
55
|
|
56 #include <stdio.h>
|
|
57 #ifdef TERM_CONTROL
|
|
58 #include <fcntl.h>
|
|
59 int tflags;
|
|
60 #endif
|
|
61 #include <stdlib.h>
|
|
62 #include <unistd.h>
|
|
63 #include <time.h>
|
|
64 #include <string.h>
|
|
65 #include <ctype.h>
|
|
66
|
|
67 void finish();
|
|
68
|
|
69 static int fdump=0;
|
|
70
|
|
71
|
|
72 /* Default: no big endian ... */
|
|
73 #ifndef CPU_BIG_ENDIAN
|
|
74 /* check if environment provides some information about this ... */
|
|
75 # if defined(BIG_ENDIAN) && defined(BYTE_ORDER)
|
|
76 # if BIG_ENDIAN == BYTE_ORDER
|
|
77 # define CPU_BIG_ENDIAN 1
|
|
78 # else
|
|
79 # define CPU_BIG_ENDIAN 0
|
|
80 # endif
|
|
81 # endif
|
|
82 #endif
|
|
83
|
|
84
|
|
85 typedef unsigned char Byte;
|
|
86 typedef unsigned short Word;
|
|
87
|
|
88 /* 6809 registers */
|
|
89 Byte ccreg,dpreg;
|
|
90 Word xreg,yreg,ureg,sreg,ureg,pcreg;
|
|
91
|
|
92 Byte fillreg = 0xff;
|
|
93 Word wfillreg = 0xffff;
|
|
94
|
|
95 Word pcreg_prev;
|
|
96
|
|
97 Byte d_reg[2];
|
|
98 Word *dreg=(Word *)d_reg;
|
|
99
|
|
100
|
|
101 /* This is a dirty aliasing trick, but fast! */
|
|
102 #if CPU_BIG_ENDIAN
|
|
103 Byte *areg=d_reg;
|
|
104 Byte *breg=d_reg+1;
|
|
105 #else
|
|
106 Byte *breg=d_reg;
|
|
107 Byte *areg=d_reg+1;
|
|
108 #endif
|
|
109
|
|
110
|
|
111 /* 6809 memory space */
|
|
112 static Byte mem[65536];
|
|
113
|
|
114 #define GETWORD(a) (mem[a]<<8|mem[(a)+1])
|
|
115 #define SETWORD(a,n) {mem[a]=(n)>>8;mem[(a)+1]=n;}
|
|
116 /* Two bytes of a word are fetched separately because of
|
|
117 the possible wrap-around at address $ffff and alignment
|
|
118 */
|
|
119
|
|
120
|
|
121 int iflag; /* flag to indicate prebyte $10 or $11 */
|
|
122 Byte ireg; /* Instruction register */
|
|
123
|
|
124 #define IMMBYTE(b) b=mem[pcreg++];
|
|
125 #define IMMWORD(w) {w=GETWORD(pcreg);pcreg+=2;}
|
|
126
|
|
127 /* sreg */
|
|
128 #define PUSHBYTE(b) mem[--sreg]=b;
|
|
129 #define PUSHWORD(w) {sreg-=2;SETWORD(sreg,w)}
|
|
130 #define PULLBYTE(b) b=mem[sreg++];
|
|
131 #define PULLWORD(w) {w=GETWORD(sreg);sreg+=2;}
|
|
132
|
|
133 /* ureg */
|
|
134 #define PUSHUBYTE(b) mem[--ureg]=b;
|
|
135 #define PUSHUWORD(w) {ureg-=2;SETWORD(ureg,w)}
|
|
136 #define PULLUBYTE(b) b=mem[ureg++];
|
|
137 #define PULLUWORD(w) {w=GETWORD(ureg);ureg+=2;}
|
|
138
|
|
139 #define SIGNED(b) ((Word)(b&0x80?b|0xff00:b))
|
|
140
|
|
141 Word *ixregs[]={&xreg,&yreg,&ureg,&sreg};
|
|
142
|
|
143 static int idx;
|
|
144
|
|
145 /* disassembled instruction buffer */
|
|
146 static char dinst[6];
|
|
147
|
|
148 /* disassembled operand buffer */
|
|
149 static char dops[32];
|
|
150
|
|
151 /* disassembled instruction len (optional, on demand) */
|
|
152 static int da_len;
|
|
153
|
|
154 /* instruction cycles */
|
|
155 static int cycles;
|
|
156 unsigned long cycles_sum;
|
|
157
|
|
158 void da_inst(char *inst, char *reg, int cyclecount) {
|
|
159 *dinst = 0;
|
|
160 *dops = 0;
|
|
161 if (inst != NULL) strcat(dinst, inst);
|
|
162 if (reg != NULL) strcat(dinst, reg);
|
|
163 cycles += cyclecount;
|
|
164 }
|
|
165
|
|
166 void da_inst_cat(char *inst, int cyclecount) {
|
|
167 if (inst != NULL) strcat(dinst, inst);
|
|
168 cycles += cyclecount;
|
|
169 }
|
|
170
|
|
171 void da_ops(char *part1, char* part2, int cyclecount) {
|
|
172 if (part1 != NULL) strcat(dops, part1);
|
|
173 if (part2 != NULL) strcat(dops, part2);
|
|
174 cycles += cyclecount;
|
|
175 }
|
|
176
|
|
177 void da_reg(Byte b)
|
|
178 {
|
|
179 char *reg[] = { "d", "x", "y", "u", "s", "pc", "?", "?",
|
|
180 "a", "b", "cc", "dp", "?", "?", "?", "?" };
|
|
181 da_ops( reg[(b>>4) & 0xf], ",", 0);
|
|
182 da_ops( reg[b & 0xf], NULL, 0);
|
|
183 }
|
|
184
|
|
185 /* Now follow the posbyte addressing modes. */
|
|
186
|
|
187 Word illaddr() /* illegal addressing mode, defaults to zero */
|
|
188 {
|
|
189 return 0;
|
|
190 }
|
|
191
|
|
192 static char *dixreg[] = { "x", "y", "u", "s" };
|
|
193
|
|
194 Word ainc()
|
|
195 {
|
|
196 da_ops(",",dixreg[idx],2);
|
|
197 da_ops("+",NULL,0);
|
|
198 return (*ixregs[idx])++;
|
|
199 }
|
|
200
|
|
201 Word ainc2()
|
|
202 {
|
|
203 Word temp;
|
|
204 da_ops(",",dixreg[idx],3);
|
|
205 da_ops("++",NULL,0);
|
|
206 temp=(*ixregs[idx]);
|
|
207 (*ixregs[idx])+=2;
|
|
208 return(temp);
|
|
209 }
|
|
210
|
|
211 Word adec()
|
|
212 {
|
|
213 da_ops(",-",dixreg[idx],2);
|
|
214 return --(*ixregs[idx]);
|
|
215 }
|
|
216
|
|
217 Word adec2()
|
|
218 {
|
|
219 Word temp;
|
|
220 da_ops(",--",dixreg[idx],3);
|
|
221 (*ixregs[idx])-=2;
|
|
222 temp=(*ixregs[idx]);
|
|
223 return(temp);
|
|
224 }
|
|
225
|
|
226 Word plus0()
|
|
227 {
|
|
228 da_ops(",",dixreg[idx],0);
|
|
229 return(*ixregs[idx]);
|
|
230 }
|
|
231
|
|
232 Word plusa()
|
|
233 {
|
|
234 da_ops("a,",dixreg[idx],1);
|
|
235 return(*ixregs[idx])+SIGNED(*areg);
|
|
236 }
|
|
237
|
|
238 Word plusb()
|
|
239 {
|
|
240 da_ops("b,",dixreg[idx],1);
|
|
241 return(*ixregs[idx])+SIGNED(*breg);
|
|
242 }
|
|
243
|
|
244 Word plusn()
|
|
245 {
|
|
246 Byte b;
|
|
247 char off[6];
|
|
248 IMMBYTE(b)
|
|
249 /* negative offsets alway decimal, otherwise hex */
|
|
250 if (b & 0x80) sprintf(off,"%d,", -(b ^ 0xff)-1);
|
|
251 else sprintf(off,"$%02x,",b);
|
|
252 da_ops(off,dixreg[idx],1);
|
|
253 return(*ixregs[idx])+SIGNED(b);
|
|
254 }
|
|
255
|
|
256 Word plusnn()
|
|
257 {
|
|
258 Word w;
|
|
259 IMMWORD(w)
|
|
260 char off[6];
|
|
261 sprintf(off,"$%04x,",w);
|
|
262 da_ops(off,dixreg[idx],4);
|
|
263 return(*ixregs[idx])+w;
|
|
264 }
|
|
265
|
|
266 Word plusd()
|
|
267 {
|
|
268 da_ops("d,",dixreg[idx],4);
|
|
269 return(*ixregs[idx])+*dreg;
|
|
270 }
|
|
271
|
|
272
|
|
273 Word npcr()
|
|
274 {
|
|
275 Byte b;
|
|
276 char off[11];
|
|
277
|
|
278 IMMBYTE(b)
|
|
279 sprintf(off,"$%04x,pcr",(pcreg+SIGNED(b))&0xffff);
|
|
280 da_ops(off,NULL,1);
|
|
281 return pcreg+SIGNED(b);
|
|
282 }
|
|
283
|
|
284 Word nnpcr()
|
|
285 {
|
|
286 Word w;
|
|
287 char off[11];
|
|
288
|
|
289 IMMWORD(w)
|
|
290 sprintf(off,"$%04x,pcr",(pcreg+w)&0xffff);
|
|
291 da_ops(off,NULL,5);
|
|
292 return pcreg+w;
|
|
293 }
|
|
294
|
|
295 Word direct()
|
|
296 {
|
|
297 Word(w);
|
|
298 char off[6];
|
|
299
|
|
300 IMMWORD(w)
|
|
301 sprintf(off,"$%04x",w);
|
|
302 da_ops(off,NULL,3);
|
|
303 return w;
|
|
304 }
|
|
305
|
|
306 Word zeropage()
|
|
307 {
|
|
308 Byte b;
|
|
309 char off[6];
|
|
310
|
|
311 IMMBYTE(b)
|
|
312 sprintf(off,"$%02x", b);
|
|
313 da_ops(off,NULL,2);
|
|
314 return dpreg<<8|b;
|
|
315 }
|
|
316
|
|
317
|
|
318 Word immediate()
|
|
319 {
|
|
320 char off[6];
|
|
321
|
|
322 sprintf(off,"#$%02x", mem[pcreg]);
|
|
323 da_ops(off,NULL,0);
|
|
324 return pcreg++;
|
|
325 }
|
|
326
|
|
327 Word immediate2()
|
|
328 {
|
|
329 Word temp;
|
|
330 char off[7];
|
|
331
|
|
332 temp=pcreg;
|
|
333 sprintf(off,"#$%04x", (mem[pcreg]<<8)+mem[(pcreg+1)&0xffff]);
|
|
334 da_ops(off,NULL,0);
|
|
335 pcreg+=2;
|
|
336 return temp;
|
|
337 }
|
|
338
|
|
339 Word (*pbtable[])()={ ainc, ainc2, adec, adec2,
|
|
340 plus0, plusb, plusa, illaddr,
|
|
341 plusn, plusnn, illaddr, plusd,
|
|
342 npcr, nnpcr, illaddr, direct, };
|
|
343
|
|
344 Word postbyte()
|
|
345 {
|
|
346 Byte pb;
|
|
347 Word temp;
|
|
348 char off[6];
|
|
349
|
|
350 IMMBYTE(pb)
|
|
351 idx=((pb & 0x60) >> 5);
|
|
352 if(pb & 0x80) {
|
|
353 if( pb & 0x10)
|
|
354 da_ops("[",NULL,3);
|
|
355 temp=(*pbtable[pb & 0x0f])();
|
|
356 if( pb & 0x10) {
|
|
357 temp=GETWORD(temp);
|
|
358 da_ops("]",NULL,0);
|
|
359 }
|
|
360 return temp;
|
|
361 } else {
|
|
362 temp=pb & 0x1f;
|
|
363 if(temp & 0x10) temp|=0xfff0; /* sign extend */
|
|
364 sprintf(off,"%d,",(temp & 0x10) ? -(temp ^ 0xffff)-1 : temp);
|
|
365 da_ops(off,dixreg[idx],1);
|
|
366 return (*ixregs[idx])+temp;
|
|
367 }
|
|
368 }
|
|
369
|
|
370 Byte * eaddr0() /* effective address for NEG..JMP as byte pointer */
|
|
371 {
|
|
372 switch( (ireg & 0x70) >> 4)
|
|
373 {
|
|
374 case 0: return mem+zeropage();
|
|
375 default:
|
|
376 case 1:case 2:case 3: return 0; /*canthappen*/
|
|
377
|
|
378 case 4: da_inst_cat("a",-2); return areg;
|
|
379 case 5: da_inst_cat("b",-2); return breg;
|
|
380 case 6: da_inst_cat(NULL,2); return mem+postbyte();
|
|
381 case 7: return mem+direct();
|
|
382 }
|
|
383
|
|
384 }
|
|
385
|
|
386 Word eaddr8() /* effective address for 8-bits ops. */
|
|
387 {
|
|
388 switch( (ireg & 0x30) >> 4)
|
|
389 {
|
|
390 default:
|
|
391 case 0: return immediate();
|
|
392 case 1: return zeropage();
|
|
393 case 2: da_inst_cat(NULL,2); return postbyte();
|
|
394 case 3: return direct();
|
|
395 }
|
|
396 }
|
|
397
|
|
398 Word eaddr16() /* effective address for 16-bits ops. */
|
|
399 {
|
|
400 switch( (ireg & 0x30) >> 4)
|
|
401 {
|
|
402 default:
|
|
403 case 0: da_inst_cat(NULL,-1); return immediate2();
|
|
404 case 1: da_inst_cat(NULL,-1); return zeropage();
|
|
405 case 2: da_inst_cat(NULL,1); return postbyte();
|
|
406 case 3: da_inst_cat(NULL,-1); return direct();
|
|
407 }
|
|
408 }
|
|
409
|
|
410 void
|
|
411 ill() /* illegal opcode==noop */
|
|
412 {
|
|
413 }
|
|
414
|
|
415 /* macros to set status flags */
|
|
416 #define SEC ccreg|=0x01;
|
|
417 #define CLC ccreg&=0xfe;
|
|
418 #define SEZ ccreg|=0x04;
|
|
419 #define CLZ ccreg&=0xfb;
|
|
420 #define SEN ccreg|=0x08;
|
|
421 #define CLN ccreg&=0xf7;
|
|
422 #define SEV ccreg|=0x02;
|
|
423 #define CLV ccreg&=0xfd;
|
|
424 #define SEH ccreg|=0x20;
|
|
425 #define CLH ccreg&=0xdf;
|
|
426
|
|
427 /* set N and Z flags depending on 8 or 16 bit result */
|
|
428 #define SETNZ8(b) {if(b)CLZ else SEZ if(b&0x80)SEN else CLN}
|
|
429 #define SETNZ16(b) {if(b)CLZ else SEZ if(b&0x8000)SEN else CLN}
|
|
430
|
|
431 #define SETSTATUS(a,b,res) if((a^b^res)&0x10) SEH else CLH \
|
|
432 if((a^b^res^(res>>1))&0x80)SEV else CLV \
|
|
433 if(res&0x100)SEC else CLC SETNZ8((Byte)res)
|
|
434
|
|
435 void
|
|
436 add()
|
|
437 {
|
|
438 Word aop,bop,res;
|
|
439 Byte* aaop;
|
|
440 da_inst("add",(ireg&0x40)?"b":"a",2);
|
|
441 aaop=(ireg&0x40)?breg:areg;
|
|
442 aop=*aaop;
|
|
443 bop=mem[eaddr8()];
|
|
444 res=aop+bop;
|
|
445 SETSTATUS(aop,bop,res)
|
|
446 *aaop=res;
|
|
447 }
|
|
448
|
|
449 void
|
|
450 sbc()
|
|
451 {
|
|
452 Word aop,bop,res;
|
|
453 Byte* aaop;
|
|
454 da_inst("sbc",(ireg&0x40)?"b":"a",2);
|
|
455 aaop=(ireg&0x40)?breg:areg;
|
|
456 aop=*aaop;
|
|
457 bop=mem[eaddr8()];
|
|
458 res=aop-bop-(ccreg&0x01);
|
|
459 SETSTATUS(aop,bop,res)
|
|
460 *aaop=res;
|
|
461 }
|
|
462
|
|
463 void
|
|
464 sub()
|
|
465 {
|
|
466 Word aop,bop,res;
|
|
467 Byte* aaop;
|
|
468 da_inst("sub",(ireg&0x40)?"b":"a",2);
|
|
469 aaop=(ireg&0x40)?breg:areg;
|
|
470 aop=*aaop;
|
|
471 bop=mem[eaddr8()];
|
|
472 res=aop-bop;
|
|
473 SETSTATUS(aop,bop,res)
|
|
474 *aaop=res;
|
|
475 }
|
|
476
|
|
477 void
|
|
478 adc()
|
|
479 {
|
|
480 Word aop,bop,res;
|
|
481 Byte* aaop;
|
|
482 da_inst("adc",(ireg&0x40)?"b":"a",2);
|
|
483 aaop=(ireg&0x40)?breg:areg;
|
|
484 aop=*aaop;
|
|
485 bop=mem[eaddr8()];
|
|
486 res=aop+bop+(ccreg&0x01);
|
|
487 SETSTATUS(aop,bop,res)
|
|
488 *aaop=res;
|
|
489 }
|
|
490
|
|
491 void
|
|
492 cmp()
|
|
493 {
|
|
494 Word aop,bop,res;
|
|
495 Byte* aaop;
|
|
496 da_inst("cmp",(ireg&0x40)?"b":"a",2);
|
|
497 aaop=(ireg&0x40)?breg:areg;
|
|
498 aop=*aaop;
|
|
499 bop=mem[eaddr8()];
|
|
500 res=aop-bop;
|
|
501 SETSTATUS(aop,bop,res)
|
|
502 }
|
|
503
|
|
504 void
|
|
505 and()
|
|
506 {
|
|
507 Byte aop,bop,res;
|
|
508 Byte* aaop;
|
|
509 da_inst("and",(ireg&0x40)?"b":"a",2);
|
|
510 aaop=(ireg&0x40)?breg:areg;
|
|
511 aop=*aaop;
|
|
512 bop=mem[eaddr8()];
|
|
513 res=aop&bop;
|
|
514 SETNZ8(res)
|
|
515 CLV
|
|
516 *aaop=res;
|
|
517 }
|
|
518
|
|
519 void
|
|
520 or()
|
|
521 {
|
|
522 Byte aop,bop,res;
|
|
523 Byte* aaop;
|
|
524 da_inst("or",(ireg&0x40)?"b":"a",2);
|
|
525 aaop=(ireg&0x40)?breg:areg;
|
|
526 aop=*aaop;
|
|
527 bop=mem[eaddr8()];
|
|
528 res=aop|bop;
|
|
529 SETNZ8(res)
|
|
530 CLV
|
|
531 *aaop=res;
|
|
532 }
|
|
533
|
|
534 void
|
|
535 eor()
|
|
536 {
|
|
537 Byte aop,bop,res;
|
|
538 Byte* aaop;
|
|
539 da_inst("eor",(ireg&0x40)?"b":"a",2);
|
|
540 aaop=(ireg&0x40)?breg:areg;
|
|
541 aop=*aaop;
|
|
542 bop=mem[eaddr8()];
|
|
543 res=aop^bop;
|
|
544 SETNZ8(res)
|
|
545 CLV
|
|
546 *aaop=res;
|
|
547 }
|
|
548
|
|
549 void
|
|
550 bit()
|
|
551 {
|
|
552 Byte aop,bop,res;
|
|
553 Byte* aaop;
|
|
554 da_inst("bit",(ireg&0x40)?"b":"a",2);
|
|
555 aaop=(ireg&0x40)?breg:areg;
|
|
556 aop=*aaop;
|
|
557 bop=mem[eaddr8()];
|
|
558 res=aop&bop;
|
|
559 SETNZ8(res)
|
|
560 CLV
|
|
561 }
|
|
562
|
|
563 void
|
|
564 ld()
|
|
565 {
|
|
566 Byte res;
|
|
567 Byte* aaop;
|
|
568 da_inst("ld",(ireg&0x40)?"b":"a",2);
|
|
569 aaop=(ireg&0x40)?breg:areg;
|
|
570 res=mem[eaddr8()];
|
|
571 SETNZ8(res)
|
|
572 CLV
|
|
573 *aaop=res;
|
|
574 }
|
|
575
|
|
576 void
|
|
577 st()
|
|
578 {
|
|
579 Byte res;
|
|
580 Byte* aaop;
|
|
581 da_inst("st",(ireg&0x40)?"b":"a",2);
|
|
582 aaop=(ireg&0x40)?breg:areg;
|
|
583 res=*aaop;
|
|
584 mem[eaddr8()]=res;
|
|
585 SETNZ8(res)
|
|
586 CLV
|
|
587 }
|
|
588
|
|
589 void
|
|
590 jsr()
|
|
591 {
|
|
592 Word w;
|
|
593
|
|
594 da_inst("jsr",NULL,5);
|
|
595 da_len=-pcreg;
|
|
596 w=eaddr8();
|
|
597 da_len += pcreg +1;
|
|
598 PUSHWORD(pcreg)
|
|
599 pcreg=w;
|
|
600 }
|
|
601
|
|
602 void
|
|
603 bsr()
|
|
604 {
|
|
605 Byte b;
|
|
606 char off[6];
|
|
607
|
|
608 IMMBYTE(b)
|
|
609 da_inst("bsr",NULL,7);
|
|
610 da_len = 2;
|
|
611 PUSHWORD(pcreg)
|
|
612 pcreg+=SIGNED(b);
|
|
613 sprintf(off,"$%04x", pcreg&0xffff);
|
|
614 da_ops(off,NULL,0);
|
|
615 }
|
|
616
|
|
617 void
|
|
618 neg()
|
|
619 {
|
|
620 Byte *ea;
|
|
621 Word a,r;
|
|
622
|
|
623 a=0;
|
|
624 da_inst("neg",NULL,4);
|
|
625 ea=eaddr0();
|
|
626 a=*ea;
|
|
627 r=-a;
|
|
628 SETSTATUS(0,a,r)
|
|
629 *ea=r;
|
|
630 }
|
|
631
|
|
632 void
|
|
633 com()
|
|
634 {
|
|
635 Byte *ea;
|
|
636 Byte r;
|
|
637
|
|
638 da_inst("com",NULL,4);
|
|
639 ea=eaddr0();
|
|
640 /*
|
|
641 fprintf(stderr,"DEBUG: com before r=%02X *ea=%02X\n", r, *ea);
|
|
642 */
|
|
643 r= ~*ea;
|
|
644 /*
|
|
645 fprintf(stderr,"DEBUG: com after r=%02X *ea=%02X\n", r, *ea);
|
|
646 */
|
|
647 SETNZ8(r)
|
|
648 SEC CLV
|
|
649 *ea=r;
|
|
650 }
|
|
651
|
|
652 void
|
|
653 lsr()
|
|
654 {
|
|
655 Byte *ea;
|
|
656 Byte r;
|
|
657
|
|
658 da_inst("lsr",NULL,4);
|
|
659 ea=eaddr0();
|
|
660 r=*ea;
|
|
661 if(r&0x01)SEC else CLC
|
|
662 if(r&0x10)SEH else CLH
|
|
663 r>>=1;
|
|
664 SETNZ8(r)
|
|
665 *ea=r;
|
|
666 }
|
|
667
|
|
668 void
|
|
669 ror()
|
|
670 {
|
|
671 Byte *ea;
|
|
672 Byte r,c;
|
|
673
|
|
674 c=(ccreg&0x01)<<7;
|
|
675 da_inst("ror",NULL,4);
|
|
676 ea=eaddr0();
|
|
677 r=*ea;
|
|
678 if(r&0x01)SEC else CLC
|
|
679 r=(r>>1)+c;
|
|
680 SETNZ8(r)
|
|
681 *ea=r;
|
|
682 }
|
|
683
|
|
684 void
|
|
685 asr()
|
|
686 {
|
|
687 Byte *ea;
|
|
688 Byte r;
|
|
689
|
|
690 da_inst("asr",NULL,4);
|
|
691 ea=eaddr0();
|
|
692 r=*ea;
|
|
693 if(r&0x01)SEC else CLC
|
|
694 if(r&0x10)SEH else CLH
|
|
695 r>>=1;
|
|
696 if(r&0x40)r|=0x80;
|
|
697 SETNZ8(r)
|
|
698 *ea=r;
|
|
699 }
|
|
700
|
|
701 void
|
|
702 asl()
|
|
703 {
|
|
704 Byte *ea;
|
|
705 Word a,r;
|
|
706
|
|
707 da_inst("asl",NULL,4);
|
|
708 ea=eaddr0();
|
|
709 a=*ea;
|
|
710 r=a<<1;
|
|
711 SETSTATUS(a,a,r)
|
|
712 *ea=r;
|
|
713 }
|
|
714
|
|
715 void
|
|
716 rol()
|
|
717 {
|
|
718 Byte *ea;
|
|
719 Byte r,c;
|
|
720
|
|
721 c=(ccreg&0x01);
|
|
722 da_inst("rol",NULL,4);
|
|
723 ea=eaddr0();
|
|
724 r=*ea;
|
|
725 if(r&0x80)SEC else CLC
|
|
726 if((r&0x80)^((r<<1)&0x80))SEV else CLV
|
|
727 r=(r<<1)+c;
|
|
728 SETNZ8(r)
|
|
729 *ea=r;
|
|
730 }
|
|
731
|
|
732 void
|
|
733 inc()
|
|
734 {
|
|
735 Byte *ea;
|
|
736 Byte r;
|
|
737
|
|
738 da_inst("inc",NULL,4);
|
|
739 ea=eaddr0();
|
|
740 r=*ea;
|
|
741 r++;
|
|
742 if(r==0x80)SEV else CLV
|
|
743 SETNZ8(r)
|
|
744 *ea=r;
|
|
745 }
|
|
746
|
|
747 void
|
|
748 dec()
|
|
749 {
|
|
750 Byte *ea;
|
|
751 Byte r;
|
|
752
|
|
753 da_inst("dec",NULL,4);
|
|
754 ea=eaddr0();
|
|
755 r=*ea;
|
|
756 r--;
|
|
757 if(r==0x7f)SEV else CLV
|
|
758 SETNZ8(r)
|
|
759 *ea=r;
|
|
760 }
|
|
761
|
|
762 void
|
|
763 tst()
|
|
764 {
|
|
765 Byte r;
|
|
766 Byte *ea;
|
|
767
|
|
768 da_inst("tst",NULL,4);
|
|
769 ea=eaddr0();
|
|
770 r=*ea;
|
|
771 SETNZ8(r)
|
|
772 CLV
|
|
773 }
|
|
774
|
|
775 void
|
|
776 jmp()
|
|
777 {
|
|
778 Byte *ea;
|
|
779
|
|
780 da_len = -pcreg;
|
|
781 da_inst("jmp",NULL,1);
|
|
782 ea=eaddr0();
|
|
783 da_len += pcreg + 1;
|
|
784 pcreg=ea-mem;
|
|
785 }
|
|
786
|
|
787 void
|
|
788 clr()
|
|
789 {
|
|
790 Byte *ea;
|
|
791
|
|
792 da_inst("clr",NULL,4);
|
|
793 ea=eaddr0();
|
|
794 *ea=0;CLN CLV SEZ CLC
|
|
795 }
|
|
796
|
|
797 extern void (*instrtable[])();
|
|
798
|
|
799 void
|
|
800 flag0()
|
|
801 {
|
|
802 if(iflag) /* in case flag already set by previous flag instr don't recurse */
|
|
803 {
|
|
804 pcreg--;
|
|
805 return;
|
|
806 }
|
|
807 iflag=1;
|
|
808 ireg=mem[pcreg++];
|
|
809 da_inst(NULL,NULL,1);
|
|
810 (*instrtable[ireg])();
|
|
811 iflag=0;
|
|
812 }
|
|
813
|
|
814 void
|
|
815 flag1()
|
|
816 {
|
|
817 if(iflag) /* in case flag already set by previous flag instr don't recurse */
|
|
818 {
|
|
819 pcreg--;
|
|
820 return;
|
|
821 }
|
|
822 iflag=2;
|
|
823 ireg=mem[pcreg++];
|
|
824 da_inst(NULL,NULL,1);
|
|
825 (*instrtable[ireg])();
|
|
826 iflag=0;
|
|
827 }
|
|
828
|
|
829 void
|
|
830 nop()
|
|
831 {
|
|
832 da_inst("nop",NULL,2);
|
|
833 }
|
|
834
|
|
835 void
|
|
836 sync_inst()
|
|
837 {
|
|
838 finish();
|
|
839 }
|
|
840
|
|
841 void
|
|
842 cwai()
|
|
843 {
|
|
844 sync_inst();
|
|
845 }
|
|
846
|
|
847 void
|
|
848 lbra()
|
|
849 {
|
|
850 Word w;
|
|
851 char off[6];
|
|
852
|
|
853 IMMWORD(w)
|
|
854 pcreg+=w;
|
|
855 da_len = 3;
|
|
856 da_inst("lbra",NULL,5);
|
|
857 sprintf(off,"$%04x", pcreg&0xffff);
|
|
858 da_ops(off,NULL,0);
|
|
859 }
|
|
860
|
|
861 void
|
|
862 lbsr()
|
|
863 {
|
|
864 Word w;
|
|
865 char off[6];
|
|
866
|
|
867 da_len = 3;
|
|
868 da_inst("lbsr",NULL,9);
|
|
869 IMMWORD(w)
|
|
870 PUSHWORD(pcreg)
|
|
871 pcreg+=w;
|
|
872 sprintf(off,"$%04x", pcreg&0xffff);
|
|
873 da_ops(off,NULL,0);
|
|
874 }
|
|
875
|
|
876 void
|
|
877 daa()
|
|
878 {
|
|
879 Word a;
|
|
880 da_inst("daa",NULL,2);
|
|
881 a=*areg;
|
|
882 if(ccreg&0x20)a+=6;
|
|
883 if((a&0x0f)>9)a+=6;
|
|
884 if(ccreg&0x01)a+=0x60;
|
|
885 if((a&0xf0)>0x90)a+=0x60;
|
|
886 if(a&0x100)SEC
|
|
887 *areg=a;
|
|
888 }
|
|
889
|
|
890 void
|
|
891 orcc()
|
|
892 {
|
|
893 Byte b;
|
|
894 char off[7];
|
|
895 IMMBYTE(b)
|
|
896 sprintf(off,"#$%02x", b);
|
|
897 da_inst("orcc",NULL,3);
|
|
898 da_ops(off,NULL,0);
|
|
899 ccreg|=b;
|
|
900 }
|
|
901
|
|
902 void
|
|
903 andcc()
|
|
904 {
|
|
905 Byte b;
|
|
906 char off[6];
|
|
907 IMMBYTE(b)
|
|
908 sprintf(off,"#$%02x", b);
|
|
909 da_inst("andcc",NULL,3);
|
|
910 da_ops(off,NULL,0);
|
|
911
|
|
912 ccreg&=b;
|
|
913 }
|
|
914
|
|
915 void
|
|
916 mul()
|
|
917 {
|
|
918 Word w;
|
|
919 w=*areg * *breg;
|
|
920 da_inst("mul",NULL,11);
|
|
921 if(w)CLZ else SEZ
|
|
922 if(w&0x80) SEC else CLC
|
|
923 *dreg=w;
|
|
924 }
|
|
925
|
|
926 void
|
|
927 sex()
|
|
928 {
|
|
929 Word w;
|
|
930 da_inst("sex",NULL,2);
|
|
931 w=SIGNED(*breg);
|
|
932 SETNZ16(w)
|
|
933 *dreg=w;
|
|
934 }
|
|
935
|
|
936 void
|
|
937 abx()
|
|
938 {
|
|
939 da_inst("abx",NULL,3);
|
|
940 xreg += *breg;
|
|
941 }
|
|
942
|
|
943 void
|
|
944 rts()
|
|
945 {
|
|
946 da_inst("rts",NULL,5);
|
|
947 da_len = 1;
|
|
948 PULLWORD(pcreg)
|
|
949 }
|
|
950
|
|
951 void
|
|
952 rti()
|
|
953 {
|
|
954 Byte x;
|
|
955 x=ccreg&0x80;
|
|
956 da_inst("rti",NULL,(x?15:6));
|
|
957 da_len = 1;
|
|
958 PULLBYTE(ccreg)
|
|
959 if(x)
|
|
960 {
|
|
961 PULLBYTE(*areg)
|
|
962 PULLBYTE(*breg)
|
|
963 PULLBYTE(dpreg)
|
|
964 PULLWORD(xreg)
|
|
965 PULLWORD(yreg)
|
|
966 PULLWORD(ureg)
|
|
967 }
|
|
968 PULLWORD(pcreg)
|
|
969 }
|
|
970
|
|
971 void
|
|
972 swi()
|
|
973 {
|
|
974 int w;
|
|
975 da_inst("swi",(iflag==1)?"2":(iflag==2)?"3":"",5);
|
|
976 switch(iflag)
|
|
977 {
|
|
978 case 0:
|
|
979 PUSHWORD(pcreg)
|
|
980 PUSHWORD(ureg)
|
|
981 PUSHWORD(yreg)
|
|
982 PUSHWORD(xreg)
|
|
983 PUSHBYTE(dpreg)
|
|
984 PUSHBYTE(*breg)
|
|
985 PUSHBYTE(*areg)
|
|
986 PUSHBYTE(ccreg)
|
|
987 ccreg|=0xd0;
|
|
988 pcreg=GETWORD(0xfffa);
|
|
989 break;
|
|
990 case 1:
|
|
991 putchar(*breg);
|
|
992 fflush(stdout);
|
|
993 break;
|
|
994 case 2:
|
|
995 w=getchar();
|
|
996 if(w==EOF)SEC else CLC
|
|
997 *breg=w;
|
|
998 }
|
|
999 }
|
|
1000
|
|
1001
|
|
1002 Word *wordregs[]={(Word*)d_reg,&xreg,&yreg,&ureg,&sreg,&pcreg,&wfillreg,&wfillreg};
|
|
1003
|
|
1004 #if CPU_BIG_ENDIAN
|
|
1005 Byte *byteregs[]={d_reg,d_reg+1,&ccreg,&dpreg,&fillreg,&fillreg,&fillreg,&fillreg};
|
|
1006 #else
|
|
1007 Byte *byteregs[]={d_reg+1,d_reg,&ccreg,&dpreg,&fillreg,&fillreg,&fillreg,&fillreg};
|
|
1008 #endif
|
|
1009
|
|
1010 void
|
|
1011 tfr()
|
|
1012 {
|
|
1013 Byte b;
|
|
1014 da_inst("tfr",NULL,7);
|
|
1015 IMMBYTE(b)
|
|
1016 da_reg(b);
|
|
1017 Word v;
|
|
1018 // source in higher nibble (highest bit set means 8 bit reg.)
|
|
1019 if(b&0x80) {
|
|
1020 v=*byteregs[(b&0x70)>>4] | (b&0x08 ? 0 : 0xff00);
|
|
1021 } else {
|
|
1022 v=*wordregs[(b&0x70)>>4];
|
|
1023 }
|
|
1024 // dest in lower nibble (highest bit set means 8 bit reg.)
|
|
1025 if(b&0x8) {
|
|
1026 *byteregs[b&0x07]=v&0xff;
|
|
1027 fillreg=0xff; // keep fillvalue
|
|
1028 } else {
|
|
1029 *wordregs[b&0x07]=v;
|
|
1030 wfillreg = 0xffff; // keep fillvalue
|
|
1031 }
|
|
1032 }
|
|
1033
|
|
1034 void
|
|
1035 exg()
|
|
1036 {
|
|
1037 Byte b;
|
|
1038 Word f;
|
|
1039 Word t;
|
|
1040 da_inst("exg",NULL,8);
|
|
1041 IMMBYTE(b)
|
|
1042 da_reg(b);
|
|
1043 if(b&0x80) {
|
|
1044 f=*byteregs[(b&0x70)>>4] | 0xff00;
|
|
1045 } else {
|
|
1046 f=*wordregs[(b>>4)&0x07];
|
|
1047 }
|
|
1048 if(b&0x8) {
|
|
1049 t=*byteregs[b&0x07] | 0xff00;
|
|
1050 } else {
|
|
1051 t=*wordregs[b&0x07];
|
|
1052 }
|
|
1053 if(b&0x80) {
|
|
1054 *byteregs[(b&0x70)>>4] = t;
|
|
1055 fillreg=0xff; // keep fillvalue
|
|
1056 } else {
|
|
1057 *wordregs[(b>>4)&0x07] = t;
|
|
1058 wfillreg = 0xffff; // keep fillvalue
|
|
1059 }
|
|
1060 if(b&0x8) {
|
|
1061 *byteregs[b&0x07] = f;
|
|
1062 fillreg=0xff; // keep fillvalue
|
|
1063 } else {
|
|
1064 *wordregs[b&0x07] = f;
|
|
1065 wfillreg = 0xffff; // keep fillvalue
|
|
1066 }
|
|
1067 }
|
|
1068
|
|
1069 void
|
|
1070 br(int f)
|
|
1071 {
|
|
1072 Byte b;
|
|
1073 Word w;
|
|
1074 char off[7];
|
|
1075 Word dest;
|
|
1076
|
|
1077 if(!iflag) {
|
|
1078 IMMBYTE(b)
|
|
1079 dest = pcreg+SIGNED(b);
|
|
1080 if(f) pcreg+=SIGNED(b);
|
|
1081 da_len = 2;
|
|
1082 } else {
|
|
1083 IMMWORD(w)
|
|
1084 dest = pcreg+w;
|
|
1085 if(f) pcreg+=w;
|
|
1086 da_len = 3;
|
|
1087 }
|
|
1088 sprintf(off,"$%04x", dest&0xffff);
|
|
1089 da_ops(off,NULL,0);
|
|
1090 }
|
|
1091
|
|
1092 #define NXORV ((ccreg&0x08)^(ccreg&0x02))
|
|
1093
|
|
1094 void
|
|
1095 bra()
|
|
1096 {
|
|
1097 da_inst(iflag?"l":"","bra",iflag?5:3);
|
|
1098 br(1);
|
|
1099 }
|
|
1100
|
|
1101 void
|
|
1102 brn()
|
|
1103 {
|
|
1104 da_inst(iflag?"l":"","brn",iflag?5:3);
|
|
1105 br(0);
|
|
1106 }
|
|
1107
|
|
1108 void
|
|
1109 bhi()
|
|
1110 {
|
|
1111 da_inst(iflag?"l":"","bhi",iflag?5:3);
|
|
1112 br(!(ccreg&0x05));
|
|
1113 }
|
|
1114
|
|
1115 void
|
|
1116 bls()
|
|
1117 {
|
|
1118 da_inst(iflag?"l":"","bls",iflag?5:3);
|
|
1119 br(ccreg&0x05);
|
|
1120 }
|
|
1121
|
|
1122 void
|
|
1123 bcc()
|
|
1124 {
|
|
1125 da_inst(iflag?"l":"","bcc",iflag?5:3);
|
|
1126 br(!(ccreg&0x01));
|
|
1127 }
|
|
1128
|
|
1129 void
|
|
1130 bcs()
|
|
1131 {
|
|
1132 da_inst(iflag?"l":"","bcs",iflag?5:3);
|
|
1133 br(ccreg&0x01);
|
|
1134 }
|
|
1135
|
|
1136 void
|
|
1137 bne()
|
|
1138 {
|
|
1139 da_inst(iflag?"l":"","bne",iflag?5:3);
|
|
1140 br(!(ccreg&0x04));
|
|
1141 }
|
|
1142
|
|
1143 void
|
|
1144 beq()
|
|
1145 {
|
|
1146 da_inst(iflag?"l":"","beq",iflag?5:3);
|
|
1147 br(ccreg&0x04);
|
|
1148 }
|
|
1149
|
|
1150 void
|
|
1151 bvc()
|
|
1152 {
|
|
1153 da_inst(iflag?"l":"","bvc",iflag?5:3);
|
|
1154 br(!(ccreg&0x02));
|
|
1155 }
|
|
1156
|
|
1157 void
|
|
1158 bvs()
|
|
1159 {
|
|
1160 da_inst(iflag?"l":"","bvs",iflag?5:3);
|
|
1161 br(ccreg&0x02);
|
|
1162 }
|
|
1163
|
|
1164 void
|
|
1165 bpl()
|
|
1166 {
|
|
1167 da_inst(iflag?"l":"","bpl",iflag?5:3);
|
|
1168 br(!(ccreg&0x08));
|
|
1169 }
|
|
1170
|
|
1171 void
|
|
1172 bmi()
|
|
1173 {
|
|
1174 da_inst(iflag?"l":"","bmi",iflag?5:3);
|
|
1175 br(ccreg&0x08);
|
|
1176 }
|
|
1177
|
|
1178 void
|
|
1179 bge()
|
|
1180 {
|
|
1181 da_inst(iflag?"l":"","bge",iflag?5:3);
|
|
1182 br(!NXORV);
|
|
1183 }
|
|
1184
|
|
1185 void
|
|
1186 blt()
|
|
1187 {
|
|
1188 da_inst(iflag?"l":"","blt",iflag?5:3);
|
|
1189 br(NXORV);
|
|
1190 }
|
|
1191
|
|
1192 void
|
|
1193 bgt()
|
|
1194 {
|
|
1195 da_inst(iflag?"l":"","bgt",iflag?5:3);
|
|
1196 br(!(NXORV||ccreg&0x04));
|
|
1197 }
|
|
1198
|
|
1199 void
|
|
1200 ble()
|
|
1201 {
|
|
1202 da_inst(iflag?"l":"","ble",iflag?5:3);
|
|
1203 br(NXORV||ccreg&0x04);
|
|
1204 }
|
|
1205
|
|
1206 void
|
|
1207 leax()
|
|
1208 {
|
|
1209 Word w;
|
|
1210 da_inst("leax",NULL,4);
|
|
1211 w=postbyte();
|
|
1212 if(w) CLZ else SEZ
|
|
1213 xreg=w;
|
|
1214 }
|
|
1215
|
|
1216 void
|
|
1217 leay()
|
|
1218 {
|
|
1219 Word w;
|
|
1220 da_inst("leay",NULL,4);
|
|
1221 w=postbyte();
|
|
1222 if(w) CLZ else SEZ
|
|
1223 yreg=w;
|
|
1224 }
|
|
1225
|
|
1226 void
|
|
1227 leau()
|
|
1228 {
|
|
1229 da_inst("leau",NULL,4);
|
|
1230 ureg=postbyte();
|
|
1231 }
|
|
1232
|
|
1233 void
|
|
1234 leas()
|
|
1235 {
|
|
1236 da_inst("leas",NULL,4);
|
|
1237 sreg=postbyte();
|
|
1238 }
|
|
1239
|
|
1240
|
|
1241 int bit_count(Byte b)
|
|
1242 {
|
|
1243 Byte mask=0x80;
|
|
1244 int count=0;
|
|
1245 int i;
|
|
1246 char *reg[] = { "pc", "u", "y", "x", "dp", "b", "a", "cc" };
|
|
1247
|
|
1248 for(i=0; i<=7; i++) {
|
|
1249 if (b & mask) {
|
|
1250 count++;
|
|
1251 da_ops(count > 1 ? ",":"", reg[i],1+(i<4?1:0));
|
|
1252 }
|
|
1253 mask >>= 1;
|
|
1254 }
|
|
1255 return count;
|
|
1256 }
|
|
1257
|
|
1258
|
|
1259 void
|
|
1260 pshs()
|
|
1261 {
|
|
1262 Byte b;
|
|
1263 IMMBYTE(b)
|
|
1264 da_inst("pshs",NULL,5);
|
|
1265 bit_count(b);
|
|
1266 if(b&0x80)PUSHWORD(pcreg)
|
|
1267 if(b&0x40)PUSHWORD(ureg)
|
|
1268 if(b&0x20)PUSHWORD(yreg)
|
|
1269 if(b&0x10)PUSHWORD(xreg)
|
|
1270 if(b&0x08)PUSHBYTE(dpreg)
|
|
1271 if(b&0x04)PUSHBYTE(*breg)
|
|
1272 if(b&0x02)PUSHBYTE(*areg)
|
|
1273 if(b&0x01)PUSHBYTE(ccreg)
|
|
1274 }
|
|
1275
|
|
1276 void
|
|
1277 puls()
|
|
1278 {
|
|
1279 Byte b;
|
|
1280 IMMBYTE(b)
|
|
1281 da_inst("puls",NULL,5);
|
|
1282 da_len = 2;
|
|
1283 bit_count(b);
|
|
1284 if(b&0x01)PULLBYTE(ccreg)
|
|
1285 if(b&0x02)PULLBYTE(*areg)
|
|
1286 if(b&0x04)PULLBYTE(*breg)
|
|
1287 if(b&0x08)PULLBYTE(dpreg)
|
|
1288 if(b&0x10)PULLWORD(xreg)
|
|
1289 if(b&0x20)PULLWORD(yreg)
|
|
1290 if(b&0x40)PULLWORD(ureg)
|
|
1291 if(b&0x80)PULLWORD(pcreg)
|
|
1292 }
|
|
1293
|
|
1294 void
|
|
1295 pshu()
|
|
1296 {
|
|
1297 Byte b;
|
|
1298 IMMBYTE(b)
|
|
1299 da_inst("pshu",NULL,5);
|
|
1300 bit_count(b);
|
|
1301 if(b&0x80)PUSHUWORD(pcreg)
|
|
1302 if(b&0x40)PUSHUWORD(ureg)
|
|
1303 if(b&0x20)PUSHUWORD(yreg)
|
|
1304 if(b&0x10)PUSHUWORD(xreg)
|
|
1305 if(b&0x08)PUSHUBYTE(dpreg)
|
|
1306 if(b&0x04)PUSHUBYTE(*breg)
|
|
1307 if(b&0x02)PUSHUBYTE(*areg)
|
|
1308 if(b&0x01)PUSHUBYTE(ccreg)
|
|
1309 }
|
|
1310
|
|
1311 void
|
|
1312 pulu()
|
|
1313 {
|
|
1314 Byte b;
|
|
1315 IMMBYTE(b)
|
|
1316 da_inst("pulu",NULL,5);
|
|
1317 da_len = 2;
|
|
1318 bit_count(b);
|
|
1319 if(b&0x01)PULLUBYTE(ccreg)
|
|
1320 if(b&0x02)PULLUBYTE(*areg)
|
|
1321 if(b&0x04)PULLUBYTE(*breg)
|
|
1322 if(b&0x08)PULLUBYTE(dpreg)
|
|
1323 if(b&0x10)PULLUWORD(xreg)
|
|
1324 if(b&0x20)PULLUWORD(yreg)
|
|
1325 if(b&0x40)PULLUWORD(ureg)
|
|
1326 if(b&0x80)PULLUWORD(pcreg)
|
|
1327 }
|
|
1328
|
|
1329 #define SETSTATUSD(a,b,res) {if(res&0x10000) SEC else CLC \
|
|
1330 if(((res>>1)^a^b^res)&0x8000) SEV else CLV \
|
|
1331 SETNZ16((Word)res)}
|
|
1332
|
|
1333 void
|
|
1334 addd()
|
|
1335 {
|
|
1336 unsigned long aop,bop,res;
|
|
1337 Word ea;
|
|
1338 da_inst("addd",NULL,5);
|
|
1339 aop=*dreg & 0xffff;
|
|
1340 ea=eaddr16();
|
|
1341 bop=GETWORD(ea);
|
|
1342 res=aop+bop;
|
|
1343 SETSTATUSD(aop,bop,res)
|
|
1344 *dreg=res;
|
|
1345 }
|
|
1346
|
|
1347 void
|
|
1348 subd()
|
|
1349 {
|
|
1350 unsigned long aop,bop,res;
|
|
1351 Word ea;
|
|
1352 aop=*dreg & 0xffff;
|
|
1353 switch(iflag) {
|
|
1354 case 0:
|
|
1355 da_inst("subd",NULL,5);
|
|
1356 break;
|
|
1357 case 1:
|
|
1358 da_inst("cmpd",NULL,5);
|
|
1359 aop=*dreg & 0xffff;
|
|
1360 break;
|
|
1361 case 2:
|
|
1362 da_inst("cmpu",NULL,5);
|
|
1363 aop=ureg;
|
|
1364 }
|
|
1365 ea=eaddr16();
|
|
1366 bop=GETWORD(ea);
|
|
1367 res=aop-bop;
|
|
1368 SETSTATUSD(aop,bop,res)
|
|
1369 if(iflag==0) *dreg=res; /* subd result */
|
|
1370 }
|
|
1371
|
|
1372 void
|
|
1373 cmpx()
|
|
1374 {
|
|
1375 unsigned long aop,bop,res;
|
|
1376 Word ea;
|
|
1377 switch(iflag) {
|
|
1378 case 0:
|
|
1379 da_inst("cmpx",NULL,5);
|
|
1380 aop=xreg;
|
|
1381 break;
|
|
1382 case 1:
|
|
1383 da_inst("cmpy",NULL,5);
|
|
1384 aop=yreg;
|
|
1385 break;
|
|
1386 case 2:
|
|
1387 da_inst("cmps",NULL,5);
|
|
1388 aop=sreg;
|
|
1389 }
|
|
1390 ea=eaddr16();
|
|
1391 bop=GETWORD(ea);
|
|
1392 res=aop-bop;
|
|
1393 SETSTATUSD(aop,bop,res)
|
|
1394 }
|
|
1395
|
|
1396 void
|
|
1397 ldd()
|
|
1398 {
|
|
1399 Word ea,w;
|
|
1400 da_inst("ldd",NULL,4);
|
|
1401 ea=eaddr16();
|
|
1402 w=GETWORD(ea);
|
|
1403 SETNZ16(w)
|
|
1404 *dreg=w;
|
|
1405 }
|
|
1406
|
|
1407 void
|
|
1408 ldx()
|
|
1409 {
|
|
1410 Word ea,w;
|
|
1411 if (iflag) da_inst("ldy",NULL,4);
|
|
1412 else da_inst("ldx",NULL,4);
|
|
1413 ea=eaddr16();
|
|
1414 w=GETWORD(ea);
|
|
1415 SETNZ16(w)
|
|
1416 if (iflag==0) xreg=w; else yreg=w;
|
|
1417 }
|
|
1418
|
|
1419 void
|
|
1420 ldu()
|
|
1421 {
|
|
1422 Word ea,w;
|
|
1423 if (iflag) da_inst("lds",NULL,4);
|
|
1424 else da_inst("ldu",NULL,4);
|
|
1425 ea=eaddr16();
|
|
1426 w=GETWORD(ea);
|
|
1427 SETNZ16(w)
|
|
1428 if (iflag==0) ureg=w; else sreg=w;
|
|
1429 }
|
|
1430
|
|
1431 void
|
|
1432 std()
|
|
1433 {
|
|
1434 Word ea,w;
|
|
1435 da_inst("std",NULL,4);
|
|
1436 ea=eaddr16();
|
|
1437 w=*dreg;
|
|
1438 SETNZ16(w)
|
|
1439 SETWORD(ea,w)
|
|
1440 }
|
|
1441
|
|
1442 void
|
|
1443 stx()
|
|
1444 {
|
|
1445 Word ea,w;
|
|
1446 if (iflag) da_inst("sty",NULL,4);
|
|
1447 else da_inst("stx",NULL,4);
|
|
1448 ea=eaddr16();
|
|
1449 if (iflag==0) w=xreg; else w=yreg;
|
|
1450 SETNZ16(w)
|
|
1451 SETWORD(ea,w)
|
|
1452 }
|
|
1453
|
|
1454 void
|
|
1455 stu()
|
|
1456 {
|
|
1457 Word ea,w;
|
|
1458 if (iflag) da_inst("sts",NULL,4);
|
|
1459 else da_inst("stu",NULL,4);
|
|
1460 ea=eaddr16();
|
|
1461 if (iflag==0) w=ureg; else w=sreg;
|
|
1462 SETNZ16(w)
|
|
1463 SETWORD(ea,w)
|
|
1464 }
|
|
1465
|
|
1466 void (*instrtable[])() = {
|
|
1467 neg , ill , ill , com , lsr , ill , ror , asr ,
|
|
1468 asl , rol , dec , ill , inc , tst , jmp , clr ,
|
|
1469 flag0 , flag1 , nop , sync_inst , ill , ill , lbra , lbsr ,
|
|
1470 ill , daa , orcc , ill , andcc , sex , exg , tfr ,
|
|
1471 bra , brn , bhi , bls , bcc , bcs , bne , beq ,
|
|
1472 bvc , bvs , bpl , bmi , bge , blt , bgt , ble ,
|
|
1473 leax , leay , leas , leau , pshs , puls , pshu , pulu ,
|
|
1474 ill , rts , abx , rti , cwai , mul , ill , swi ,
|
|
1475 neg , ill , ill , com , lsr , ill , ror , asr ,
|
|
1476 asl , rol , dec , ill , inc , tst , ill , clr ,
|
|
1477 neg , ill , ill , com , lsr , ill , ror , asr ,
|
|
1478 asl , rol , dec , ill , inc , tst , ill , clr ,
|
|
1479 neg , ill , ill , com , lsr , ill , ror , asr ,
|
|
1480 asl , rol , dec , ill , inc , tst , jmp , clr ,
|
|
1481 neg , ill , ill , com , lsr , ill , ror , asr ,
|
|
1482 asl , rol , dec , ill , inc , tst , jmp , clr ,
|
|
1483 sub , cmp , sbc , subd , and , bit , ld , st ,
|
|
1484 eor , adc , or , add , cmpx , bsr , ldx , stx ,
|
|
1485 sub , cmp , sbc , subd , and , bit , ld , st ,
|
|
1486 eor , adc , or , add , cmpx , jsr , ldx , stx ,
|
|
1487 sub , cmp , sbc , subd , and , bit , ld , st ,
|
|
1488 eor , adc , or , add , cmpx , jsr , ldx , stx ,
|
|
1489 sub , cmp , sbc , subd , and , bit , ld , st ,
|
|
1490 eor , adc , or , add , cmpx , jsr , ldx , stx ,
|
|
1491 sub , cmp , sbc , addd , and , bit , ld , st ,
|
|
1492 eor , adc , or , add , ldd , std , ldu , stu ,
|
|
1493 sub , cmp , sbc , addd , and , bit , ld , st ,
|
|
1494 eor , adc , or , add , ldd , std , ldu , stu ,
|
|
1495 sub , cmp , sbc , addd , and , bit , ld , st ,
|
|
1496 eor , adc , or , add , ldd , std , ldu , stu ,
|
|
1497 sub , cmp , sbc , addd , and , bit , ld , st ,
|
|
1498 eor , adc , or , add , ldd , std , ldu , stu ,
|
|
1499 };
|
|
1500
|
|
1501 void
|
|
1502 read_image(char* name)
|
|
1503 {
|
|
1504 FILE *image;
|
|
1505 if((image=fopen(name,"rb"))!=NULL) {
|
|
1506 fread(mem+0x100,0xff00,1,image);
|
|
1507 fclose(image);
|
|
1508 }
|
|
1509 }
|
|
1510
|
|
1511 void
|
|
1512 dump()
|
|
1513 {
|
|
1514 FILE *image;
|
|
1515 if((image=fopen("dump.v09","wb"))!=NULL) {
|
|
1516 fwrite(mem,0x10000,1,image);
|
|
1517 fclose(image);
|
|
1518 }
|
|
1519 }
|
|
1520
|
|
1521 /* E F H I N Z V C */
|
|
1522
|
|
1523 char *to_bin(Byte b)
|
|
1524 {
|
|
1525 static char binstr[9];
|
|
1526 Byte bm;
|
|
1527 char *ccbit="EFHINZVC";
|
|
1528 int i;
|
|
1529
|
|
1530 for(bm=0x80, i=0; bm>0; bm >>=1, i++)
|
|
1531 binstr[i] = (b & bm) ? toupper(ccbit[i]) : tolower(ccbit[i]);
|
|
1532 binstr[8] = 0;
|
|
1533 return binstr;
|
|
1534 }
|
|
1535
|
|
1536
|
|
1537 void cr() {
|
|
1538 #ifdef TERM_CONTROL
|
|
1539 fprintf(stderr,"%s","\r\n"); /* CR+LF because raw terminal ... */
|
|
1540 #else
|
|
1541 fprintf(stderr,"%s","\n");
|
|
1542 #endif
|
|
1543 }
|
|
1544
|
|
1545 #ifdef TRACE
|
|
1546
|
|
1547 /* max. bytes of instruction code per trace line */
|
|
1548 #define I_MAX 4
|
|
1549
|
|
1550 void trace()
|
|
1551 {
|
|
1552 int ilen;
|
|
1553 int i;
|
|
1554
|
|
1555 if (
|
|
1556 1 || ( /* no trace filtering ... */
|
|
1557 !(ureg > 0x09c0 && ureg < 0x09f3) && ( /* CMOVE ausblenden! */
|
|
1558 pcreg_prev == 0x01de || /* DOLST */
|
|
1559 pcreg_prev == 0x037a || /* FDOVAR */
|
|
1560 /*
|
|
1561 ureg >= 0x0300 && ureg < 0x03f0 ||
|
|
1562 ureg >=0x1900 ||
|
|
1563 ureg > 0x118b && ureg < 0x11b2 ||
|
|
1564 pcreg_prev >= 0x01de && pcreg_prev < 0x0300 ||
|
|
1565 xreg >=0x8000 ||
|
|
1566 pcreg_prev >= 0x01de && pcreg_prev < 0x0300 ||
|
|
1567 */
|
|
1568 0
|
|
1569 ))
|
|
1570 )
|
|
1571 {
|
|
1572 fprintf(stderr,"%04x ",pcreg_prev);
|
|
1573 if (da_len) ilen = da_len;
|
|
1574 else {
|
|
1575 ilen = pcreg-pcreg_prev; if (ilen < 0) ilen= -ilen;
|
|
1576 }
|
|
1577 for(i=0; i < I_MAX; i++) {
|
|
1578 if (i < ilen) fprintf(stderr,"%02x",mem[(pcreg_prev+i)&0xffff]);
|
|
1579 else fprintf(stderr," ");
|
|
1580 }
|
|
1581 fprintf(stderr," %-5s %-17s [%02d] ", dinst, dops, cycles);
|
|
1582 //if((ireg&0xfe)==0x10)
|
|
1583 // fprintf(stderr,"%02x ",mem[pcreg]);else fprintf(stderr," ");
|
|
1584 fprintf(stderr,"x=%04x y=%04x u=%04x s=%04x a=%02x b=%02x cc=%s",
|
|
1585 xreg,yreg,ureg,sreg,*areg,*breg,to_bin(ccreg));
|
|
1586 fprintf(stderr,", s: %04x %04x, r: %04x",
|
|
1587 mem[sreg]<<8|mem[sreg+1],
|
|
1588 mem[sreg+2]<<8|mem[sreg+3],
|
|
1589 mem[yreg]<<8|mem[yreg+1]
|
|
1590 );
|
|
1591 cr();
|
|
1592 }
|
|
1593 da_len = 0;
|
|
1594 }
|
|
1595
|
|
1596 #endif
|
|
1597
|
|
1598
|
|
1599 static char optstring[]="d";
|
|
1600
|
|
1601 int
|
|
1602 main(int argc,char *argv[])
|
|
1603 {
|
|
1604 char c;
|
|
1605 int a;
|
|
1606
|
|
1607 /* initialize memory with pseudo random data ... */
|
|
1608 srandom(time(NULL));
|
|
1609 for(a=0x0100; a<0x10000;a++) {
|
|
1610 mem[(Word)a] = (Byte) (random() & 0xff);
|
|
1611 }
|
|
1612
|
|
1613 while( (c=getopt(argc, argv, optstring)) >=0 ) {
|
|
1614 switch(c) {
|
|
1615 case 'd':
|
|
1616 fdump = 1;
|
|
1617 break;
|
|
1618 default:
|
|
1619 fprintf(stderr,"ERROR: Unknown option\n");
|
|
1620 exit(2);
|
|
1621 }
|
|
1622 }
|
|
1623
|
|
1624 if (optind < argc) {
|
|
1625 read_image(argv[optind]);
|
|
1626 }
|
|
1627 else {
|
|
1628 fprintf(stderr,"ERROR: Missing image name\n");
|
|
1629 exit(2);
|
|
1630 }
|
|
1631
|
|
1632 pcreg=0x100;
|
|
1633 sreg=0;
|
|
1634 dpreg=0;
|
|
1635 iflag=0;
|
|
1636 /* raw disables SIGINT, brkint reenables it ...
|
|
1637 */
|
|
1638 #if defined(TERM_CONTROL) && ! defined(TRACE)
|
|
1639 /* raw, but still allow key signaling, especial if ^C is desired
|
|
1640 - if not, remove brkint and isig!
|
|
1641 */
|
|
1642 system("stty -echo nl raw brkint isig");
|
|
1643 tflags=fcntl(0,F_GETFL,0);
|
|
1644 fcntl(0,F_SETFL,tflags|O_NDELAY);
|
|
1645 #endif
|
|
1646
|
|
1647 #ifdef TRACE
|
|
1648 da_len = 0;
|
|
1649 #endif
|
|
1650 cycles_sum = 0;
|
|
1651 pcreg_prev = pcreg;
|
|
1652
|
|
1653 for(;;){
|
|
1654
|
|
1655 ireg=mem[pcreg++];
|
|
1656 cycles=0;
|
|
1657 (*instrtable[ireg])(); /* process instruction */
|
|
1658 cycles_sum += cycles;
|
|
1659
|
|
1660 #ifdef TRACE
|
|
1661 trace();
|
|
1662 #endif
|
|
1663
|
|
1664 pcreg_prev = pcreg;
|
|
1665
|
|
1666 } /* for */
|
|
1667 return 0;
|
|
1668 }
|
|
1669
|
|
1670
|
|
1671
|
|
1672 void finish()
|
|
1673 {
|
|
1674 cr();
|
|
1675 fprintf(stderr,"Cycles: %lu", cycles_sum);
|
|
1676 cr();
|
|
1677 #if defined(TERM_CONTROL) && ! defined(TRACE)
|
|
1678 system("stty -raw -nl echo brkint");
|
|
1679 fcntl(0,F_SETFL,tflags&~O_NDELAY);
|
|
1680 #endif
|
|
1681 if (fdump) dump();
|
|
1682 exit(0);
|
|
1683 }
|