1 /*-*- indent-tabs-mode:nil -*- */
2 /* Copyright (C) 2007 Jeremy English <jhe@jeremyenglish.org>
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation. No representations are made about the suitability of this
9 * software for any purpose. It is provided "as is" without express or
12 * Created: 12-April-2007
16 This is a port of the javascript 6502 assembler, compiler and
17 debugger. The orignal code was copyright 2006 by Stian Soreng -
20 I changed the structure of the assembler in this version.
25 /*#include <malloc.h>*/
54 |N||V||F||B||D||I||Z||C|
61 CARRY_FL = 0, ZERO_FL = 1, INTERRUPT_FL = 2,
62 DECIMAL_FL = 3, BREAK_FL = 4, FUTURE_FL = 5,
63 OVERFLOW_FL = 6, NEGATIVE_FL = 7
67 typedef BOOL (*CharTest) (char);
69 /* A jump function takes a pointer to the current machine and a
70 opcode. The opcode is needed to figure out the memory mode. */
71 /*typedef void (*JumpFunc) (machine_6502* AddrMode);*/
75 Bit32 value[MAX_PARAM_VALUE];
76 unsigned int vp; /*value pointer, index into the value table.*/
82 Bit32 addr; /* Address of the label */
86 typedef struct AsmLine AsmLine;
88 BOOL labelDecl; /* Does the line have a label declaration? */
92 AsmLine *next; /* in list */
101 /*static void *emalloc(size_t n) {
107 static void *ecalloc(uint32_t nelm, size_t nsize){
108 void *p = calloc(nelm, nsize);
113 /* estrdup() - Allocates memory for a new string a returns a copy of the source sting in it. */
114 static char *estrdup(const char *source){
115 int ln = strlen(source) + 1;
116 char *s = ecalloc(ln, sizeof(char));
117 strncpy(s,source,ln);
121 static void checkAddress(Bit32 address){
122 /* XXX: Do we want to kill the program here? */
123 if (address >= MEM_64K)
124 fprintf(stderr, "Address %d is beyond 64k", address);
128 * stackPush() - Push byte to stack
132 static void stackPush(machine_6502 *machine, Bit8 value ) {
133 if(machine->regSP >= STACK_BOTTOM){
134 machine->memory[machine->regSP--] = value;
137 fprintf(stderr, "The stack is full: %.4x\n", machine->regSP);
138 machine->codeRunning = FALSE;
144 * stackPop() - Pop byte from stack
148 static Bit8 stackPop(machine_6502 *machine) {
149 if (machine->regSP < STACK_TOP){
150 Bit8 value =machine->memory[++machine->regSP];
154 /* fprintf(stderr, "The stack is empty.\n"); xxx */
155 machine->codeRunning = FALSE;
160 static void pushByte(machine_6502 *machine, Bit32 value ) {
161 Bit32 address = machine->defaultCodePC;
162 checkAddress(address);
163 machine->memory[address] = value & 0xff;
165 machine->defaultCodePC++;
169 * pushWord() - Push a word using pushByte twice
173 static void pushWord(machine_6502 *machine, Bit16 value ) {
174 pushByte(machine, value & 0xff );
175 pushByte(machine, (value>>8) & 0xff );
179 * popByte( machine_6502 *machine,) - Pops a byte
183 static Bit8 popByte( machine_6502 *machine) {
184 Bit8 value = machine->memory[machine->regPC];
190 * popWord() - Pops a word using popByte() twice
194 static int popWord(machine_6502 *machine) {
195 return popByte(machine) + (popByte(machine) << 8);
200 * memReadByte() - Peek a byte, don't touch any registers
204 static int memReadByte( machine_6502 *machine, int addr ) {
205 if( addr == 0xfe ) return floor( random()%255 );
206 return machine->memory[addr];
209 static void updateDisplayPixel(machine_6502 *machine, Bit16 addr){
210 Bit8 idx = memReadByte(machine,addr) & 0x0f;
216 machine->plot(x,y,idx,machine->plotterState);
221 * memStoreByte() - Poke a byte, don't touch any registers
225 static void memStoreByte( machine_6502 *machine, int addr, int value ) {
226 machine->memory[ addr ] = (value & 0xff);
227 if( (addr >= 0x200) && (addr<=0x5ff) )
228 updateDisplayPixel(machine, addr );
235 static Bit8 bitOn(Bit8 value,Flags bit){
238 return ((value & mask) > 0);
241 static Bit8 bitOff(Bit8 value, Flags bit){
242 return (! bitOn(value,bit));
245 static Bit8 setBit(Bit8 value, Flags bit, int on){
248 onMask = onMask << bit;
249 offMask = offMask ^ onMask;
250 return ((on) ? value | onMask : value & offMask);
253 static Bit8 nibble(Bit8 value, Side side){
255 case LEFT: return value & 0xf0;
256 case RIGHT: return value & 0xf;
258 fprintf(stderr,"nibble unknown side\n");
264 /* used for tracing. XXX: combined with function getvalue */
265 static BOOL peekValue(machine_6502 *machine, m6502_AddrMode adm, Pointer *pointer, Bit16 PC){
273 case IMMEDIATE_GREAT:
274 case IMMEDIATE_VALUE:
275 pointer->value = memReadByte(machine, PC);
278 zp = memReadByte(machine, PC) + machine->regX;
279 pointer->addr = memReadByte(machine,zp) +
280 (memReadByte(machine,zp+1)<<8);
281 pointer->value = memReadByte(machine, pointer->addr);
284 zp = memReadByte(machine, PC);
285 pointer->addr = memReadByte(machine,zp) +
286 (memReadByte(machine,zp+1)<<8) + machine->regY;
287 pointer->value = memReadByte(machine, pointer->addr);
290 pointer->addr = memReadByte(machine, PC);
291 pointer->value = memReadByte(machine, pointer->addr);
294 pointer->addr = memReadByte(machine, PC) + machine->regX;
295 pointer->value = memReadByte(machine, pointer->addr);
298 pointer->addr = memReadByte(machine, PC) + machine->regY;
299 pointer->value = memReadByte(machine, pointer->addr);
302 pointer->addr = memReadByte(machine, PC);
305 pointer->addr = memReadByte(machine, PC) + (memReadByte(machine, PC+1) << 8);
306 pointer->value = memReadByte(machine, pointer->addr);
310 pointer->addr = (memReadByte(machine, PC) +
311 (memReadByte(machine, PC+1) << 8)) + machine->regX;
312 pointer->value = memReadByte(machine, pointer->addr);
316 pointer->addr = (memReadByte(machine, PC) +
317 (memReadByte(machine, PC+1) << 8)) + machine->regY;
318 pointer->value = memReadByte(machine, pointer->addr);
321 /* Handled elsewhere */
329 /* Figure out how to get the value from the addrmode and get it.*/
330 static BOOL getValue(machine_6502 *machine, m6502_AddrMode adm, Pointer *pointer){
338 case IMMEDIATE_GREAT:
339 case IMMEDIATE_VALUE:
340 pointer->value = popByte(machine);
343 zp = popByte(machine) + machine->regX;
344 pointer->addr = memReadByte(machine,zp) +
345 (memReadByte(machine,zp+1)<<8);
346 pointer->value = memReadByte(machine, pointer->addr);
349 zp = popByte(machine);
350 pointer->addr = memReadByte(machine,zp) +
351 (memReadByte(machine,zp+1)<<8) + machine->regY;
352 pointer->value = memReadByte(machine, pointer->addr);
355 pointer->addr = popByte(machine);
356 pointer->value = memReadByte(machine, pointer->addr);
359 pointer->addr = popByte(machine) + machine->regX;
360 pointer->value = memReadByte(machine, pointer->addr);
363 pointer->addr = popByte(machine) + machine->regY;
364 pointer->value = memReadByte(machine, pointer->addr);
367 pointer->addr = popByte(machine);
370 pointer->addr = popWord(machine);
371 pointer->value = memReadByte(machine, pointer->addr);
375 pointer->addr = popWord(machine) + machine->regX;
376 pointer->value = memReadByte(machine, pointer->addr);
380 pointer->addr = popWord(machine) + machine->regY;
381 pointer->value = memReadByte(machine, pointer->addr);
384 /* Handled elsewhere */
392 static void dismem(machine_6502 *machine, m6502_AddrMode adm, char *output){
400 case IMMEDIATE_GREAT:
401 case IMMEDIATE_VALUE:
402 n = popByte(machine);
403 sprintf(output,"#$%x",n);
406 zp = popByte(machine);
407 n = memReadByte(machine,zp) +
408 (memReadByte(machine,zp+1)<<8);
409 sprintf(output,"($%x,x)",n);
412 zp = popByte(machine);
413 n = memReadByte(machine,zp) +
414 (memReadByte(machine,zp+1)<<8);
415 sprintf(output,"($%x),y",n);
419 n = popByte(machine);
420 sprintf(output,"$%x",n);
423 n = popByte(machine);
424 sprintf(output,"$%x,x",n);
427 n = popByte(machine);
428 sprintf(output,"$%x,y",n);
431 n = popWord(machine);
432 sprintf(output,"$%x",n);
436 n = popWord(machine);
437 sprintf(output,"$%x,x",n);
441 n = popWord(machine);
442 sprintf(output,"$%x,x",n);
451 /* manZeroNeg - Manage the negative and zero flags */
452 static void manZeroNeg(machine_6502 *machine, Bit8 value){
453 machine->regP = setBit(machine->regP, ZERO_FL, (value == 0));
454 machine->regP = setBit(machine->regP, NEGATIVE_FL, bitOn(value,NEGATIVE_FL));
457 static void warnValue(BOOL isValue){
459 fprintf(stderr,"Invalid Value from getValue.\n");
463 static void jmpADC(machine_6502 *machine, m6502_AddrMode adm){
466 Bit8 c = bitOn(machine->regP, CARRY_FL);
467 BOOL isValue = getValue(machine, adm, &ptr);
471 if (bitOn(machine->regA, NEGATIVE_FL) &&
472 bitOn(ptr.value, NEGATIVE_FL))
473 machine->regP = setBit(machine->regP, OVERFLOW_FL, 0);
475 machine->regP = setBit(machine->regP, OVERFLOW_FL, 1);
477 if (bitOn(machine->regP, DECIMAL_FL)) {
478 tmp = nibble(machine->regA,RIGHT) + nibble(ptr.value,RIGHT ) + c;
479 /* The decimal part is limited to 0 through 9 */
481 tmp = 0x10 | ((tmp + 6) & 0xf);
483 tmp += nibble(machine->regA,LEFT) + nibble(ptr.value,LEFT);
485 machine->regP = setBit(machine->regP,CARRY_FL,1);
486 if (bitOn(machine->regP, OVERFLOW_FL) && tmp >= 0x180)
487 machine->regP = setBit(machine->regP, OVERFLOW_FL, 0);
491 machine->regP = setBit(machine->regP,CARRY_FL,0);
492 if (bitOn(machine->regP, OVERFLOW_FL) && tmp < 0x80)
493 machine->regP = setBit(machine->regP, OVERFLOW_FL, 0);
497 tmp = machine->regA + ptr.value + c;
499 machine->regP = setBit(machine->regP,CARRY_FL,1);
500 if (bitOn(machine->regP, OVERFLOW_FL) && tmp >= 0x180)
501 machine->regP =setBit(machine->regP, OVERFLOW_FL, 0);
504 machine->regP = setBit(machine->regP,CARRY_FL,0);
505 if (bitOn(machine->regP, OVERFLOW_FL) && tmp < 0x80)
506 machine->regP =setBit(machine->regP, OVERFLOW_FL, 0);
511 manZeroNeg(machine,machine->regA);
514 static void jmpAND(machine_6502 *machine, m6502_AddrMode adm){
516 BOOL isValue = getValue(machine, adm, &ptr);
518 machine->regA &= ptr.value;
519 manZeroNeg(machine,machine->regA);
522 static void jmpASL(machine_6502 *machine, m6502_AddrMode adm){
524 BOOL isValue = getValue(machine, adm, &ptr);
526 machine->regP = setBit(machine->regP, CARRY_FL, bitOn(ptr.value, NEGATIVE_FL));
527 ptr.value = ptr.value << 1;
528 ptr.value = setBit(ptr.value, CARRY_FL, 0);
529 memStoreByte(machine, ptr.addr, ptr.value);
530 manZeroNeg(machine,ptr.value);
532 else { /* Accumulator */
533 machine->regP = setBit(machine->regP, CARRY_FL, bitOn(machine->regA, NEGATIVE_FL));
534 machine->regA = machine->regA << 1;
535 machine->regA = setBit(machine->regA, CARRY_FL, 0);
536 manZeroNeg(machine,machine->regA);
541 static void jmpBIT(machine_6502 *machine, m6502_AddrMode adm){
543 BOOL isValue = getValue(machine, adm, &ptr);
545 machine->regP = setBit(machine->regP, ZERO_FL, (ptr.value & machine->regA));
546 machine->regP = setBit(machine->regP, OVERFLOW_FL, bitOn(ptr.value, OVERFLOW_FL));
547 machine->regP = setBit(machine->regP, NEGATIVE_FL, bitOn(ptr.value, NEGATIVE_FL));
551 static void jumpBranch(machine_6502 *machine, Bit16 offset){
553 machine->regPC = machine->regPC - (0x100 - offset);
555 machine->regPC = machine->regPC + offset;
558 static void jmpBPL(machine_6502 *machine, m6502_AddrMode adm){
560 BOOL isValue = getValue(machine, adm, &ptr);
562 if (bitOff(machine->regP,NEGATIVE_FL))
563 jumpBranch(machine, ptr.addr);
567 static void jmpBMI(machine_6502 *machine, m6502_AddrMode adm){
569 BOOL isValue = getValue(machine, adm, &ptr);
571 if (bitOn(machine->regP,NEGATIVE_FL))
572 jumpBranch(machine, ptr.addr);
576 static void jmpBVC(machine_6502 *machine, m6502_AddrMode adm){
578 BOOL isValue = getValue(machine, adm, &ptr);
580 if (bitOff(machine->regP,OVERFLOW_FL))
581 jumpBranch(machine, ptr.addr);
584 static void jmpBVS(machine_6502 *machine, m6502_AddrMode adm){
586 BOOL isValue = getValue(machine, adm, &ptr);
588 if (bitOn(machine->regP,OVERFLOW_FL))
589 jumpBranch(machine, ptr.addr);
592 static void jmpBCC(machine_6502 *machine, m6502_AddrMode adm){
594 BOOL isValue = getValue(machine, adm, &ptr);
596 if (bitOff(machine->regP,CARRY_FL))
597 jumpBranch(machine, ptr.addr);
600 static void jmpBCS(machine_6502 *machine, m6502_AddrMode adm){
602 BOOL isValue = getValue(machine, adm, &ptr);
604 if (bitOn(machine->regP,CARRY_FL))
605 jumpBranch(machine, ptr.addr);
608 static void jmpBNE(machine_6502 *machine, m6502_AddrMode adm){
610 BOOL isValue = getValue(machine, adm, &ptr);
612 if (bitOff(machine->regP, ZERO_FL))
613 jumpBranch(machine, ptr.addr);
616 static void jmpBEQ(machine_6502 *machine, m6502_AddrMode adm){
618 BOOL isValue = getValue(machine, adm, &ptr);
620 if (bitOn(machine->regP, ZERO_FL))
621 jumpBranch(machine, ptr.addr);
624 static void doCompare(machine_6502 *machine, Bit16 reg, Pointer *ptr){
625 machine->regP = setBit(machine->regP,CARRY_FL, ((reg + ptr->value) > 0xff));
626 manZeroNeg(machine,(reg - ptr->value));
629 static void jmpCMP(machine_6502 *machine, m6502_AddrMode adm){
631 BOOL isValue = getValue(machine, adm, &ptr);
633 doCompare(machine,machine->regA,&ptr);
636 static void jmpCPX(machine_6502 *machine, m6502_AddrMode adm){
638 BOOL isValue = getValue(machine, adm, &ptr);
640 doCompare(machine,machine->regX,&ptr);
643 static void jmpCPY(machine_6502 *machine, m6502_AddrMode adm){
645 BOOL isValue = getValue(machine, adm, &ptr);
647 doCompare(machine,machine->regY,&ptr);
650 static void jmpDEC(machine_6502 *machine, m6502_AddrMode adm){
652 BOOL isValue = getValue(machine, adm, &ptr);
658 memStoreByte(machine, ptr.addr, ptr.value);
659 manZeroNeg(machine,ptr.value);
662 static void jmpEOR(machine_6502 *machine, m6502_AddrMode adm){
664 BOOL isValue = getValue(machine, adm, &ptr);
666 machine->regA ^= ptr.value;
667 manZeroNeg(machine, machine->regA);
670 static void jmpCLC(machine_6502 *machine, m6502_AddrMode adm){
671 machine->regP = setBit(machine->regP, CARRY_FL, 0);
674 static void jmpSEC(machine_6502 *machine, m6502_AddrMode adm){
675 machine->regP = setBit(machine->regP, CARRY_FL, 1);
678 static void jmpCLI(machine_6502 *machine, m6502_AddrMode adm){
679 machine->regP = setBit(machine->regP, INTERRUPT_FL, 0);
682 static void jmpSEI(machine_6502 *machine, m6502_AddrMode adm){
683 machine->regP = setBit(machine->regP, INTERRUPT_FL, 1);
686 static void jmpCLV(machine_6502 *machine, m6502_AddrMode adm){
687 machine->regP = setBit(machine->regP, OVERFLOW_FL, 0);
690 static void jmpCLD(machine_6502 *machine, m6502_AddrMode adm){
691 machine->regP = setBit(machine->regP, DECIMAL_FL, 0);
694 static void jmpSED(machine_6502 *machine, m6502_AddrMode adm){
695 machine->regP = setBit(machine->regP, DECIMAL_FL, 1);
698 static void jmpINC(machine_6502 *machine, m6502_AddrMode adm){
700 BOOL isValue = getValue(machine, adm, &ptr);
702 ptr.value = (ptr.value + 1) & 0xFF;
703 memStoreByte(machine, ptr.addr, ptr.value);
704 manZeroNeg(machine,ptr.value);
707 static void jmpJMP(machine_6502 *machine, m6502_AddrMode adm){
709 BOOL isValue = getValue(machine, adm, &ptr);
711 machine->regPC = ptr.addr;
714 static void jmpJSR(machine_6502 *machine, m6502_AddrMode adm){
716 /* Move past the 2 byte parameter. JSR is always followed by
718 Bit16 currAddr = machine->regPC + 2;
719 BOOL isValue = getValue(machine, adm, &ptr);
721 stackPush(machine, (currAddr >> 8) & 0xff);
722 stackPush(machine, currAddr & 0xff);
723 machine->regPC = ptr.addr;
726 static void jmpLDA(machine_6502 *machine, m6502_AddrMode adm){
728 BOOL isValue = getValue(machine, adm, &ptr);
730 machine->regA = ptr.value;
731 manZeroNeg(machine, machine->regA);
734 static void jmpLDX(machine_6502 *machine, m6502_AddrMode adm){
736 BOOL isValue = getValue(machine, adm, &ptr);
738 machine->regX = ptr.value;
739 manZeroNeg(machine, machine->regX);
742 static void jmpLDY(machine_6502 *machine, m6502_AddrMode adm){
744 BOOL isValue = getValue(machine, adm, &ptr);
746 machine->regY = ptr.value;
747 manZeroNeg(machine, machine->regY);
750 static void jmpLSR(machine_6502 *machine, m6502_AddrMode adm){
752 BOOL isValue = getValue(machine, adm, &ptr);
755 setBit(machine->regP, CARRY_FL,
756 bitOn(ptr.value, CARRY_FL));
757 ptr.value = ptr.value >> 1;
758 ptr.value = setBit(ptr.value,NEGATIVE_FL,0);
759 memStoreByte(machine,ptr.addr,ptr.value);
760 manZeroNeg(machine,ptr.value);
762 else { /* Accumulator */
764 setBit(machine->regP, CARRY_FL,
765 bitOn(machine->regA, CARRY_FL));
766 machine->regA = machine->regA >> 1;
767 machine->regA = setBit(machine->regA,NEGATIVE_FL,0);
768 manZeroNeg(machine,ptr.value);
772 static void jmpNOP(machine_6502 *machine, m6502_AddrMode adm){
776 static void jmpORA(machine_6502 *machine, m6502_AddrMode adm){
778 BOOL isValue = getValue(machine, adm, &ptr);
780 machine->regA |= ptr.value;
781 manZeroNeg(machine,machine->regA);
784 static void jmpTAX(machine_6502 *machine, m6502_AddrMode adm){
785 machine->regX = machine->regA;
786 manZeroNeg(machine,machine->regX);
789 static void jmpTXA(machine_6502 *machine, m6502_AddrMode adm){
790 machine->regA = machine->regX;
791 manZeroNeg(machine,machine->regA);
794 static void jmpDEX(machine_6502 *machine, m6502_AddrMode adm){
795 if (machine->regX > 0)
798 machine->regX = 0xFF;
799 manZeroNeg(machine, machine->regX);
802 static void jmpINX(machine_6502 *machine, m6502_AddrMode adm){
803 Bit16 value = machine->regX + 1;
804 machine->regX = value & 0xFF;
805 manZeroNeg(machine, machine->regX);
808 static void jmpTAY(machine_6502 *machine, m6502_AddrMode adm){
809 machine->regY = machine->regA;
810 manZeroNeg(machine, machine->regY);
813 static void jmpTYA(machine_6502 *machine, m6502_AddrMode adm){
814 machine->regA = machine->regY;
815 manZeroNeg(machine, machine->regA);
818 static void jmpDEY(machine_6502 *machine, m6502_AddrMode adm){
819 if (machine->regY > 0)
822 machine->regY = 0xFF;
823 manZeroNeg(machine, machine->regY);
826 static void jmpINY(machine_6502 *machine, m6502_AddrMode adm){
827 Bit16 value = machine->regY + 1;
828 machine->regY = value & 0xff;
829 manZeroNeg(machine, machine->regY);
832 static void jmpROR(machine_6502 *machine, m6502_AddrMode adm){
835 BOOL isValue = getValue(machine, adm, &ptr);
837 cf = bitOn(machine->regP, CARRY_FL);
839 setBit(machine->regP, CARRY_FL,
840 bitOn(ptr.value, CARRY_FL));
841 ptr.value = ptr.value >> 1;
842 ptr.value = setBit(ptr.value, NEGATIVE_FL, cf);
843 memStoreByte(machine, ptr.addr, ptr.value);
844 manZeroNeg(machine, ptr.value);
847 cf = bitOn(machine->regP, CARRY_FL);
849 setBit(machine->regP, CARRY_FL,
850 bitOn(machine->regA, CARRY_FL));
851 machine->regA = machine->regA >> 1;
852 machine->regA = setBit(machine->regA, NEGATIVE_FL, cf);
853 manZeroNeg(machine, machine->regA);
857 static void jmpROL(machine_6502 *machine, m6502_AddrMode adm){
860 BOOL isValue = getValue(machine, adm, &ptr);
862 cf = bitOn(machine->regP, CARRY_FL);
864 setBit(machine->regP, CARRY_FL,
865 bitOn(ptr.value, NEGATIVE_FL));
866 ptr.value = ptr.value << 1;
867 ptr.value = setBit(ptr.value, CARRY_FL, cf);
868 memStoreByte(machine, ptr.addr, ptr.value);
869 manZeroNeg(machine, ptr.value);
872 cf = bitOn(machine->regP, CARRY_FL);
874 setBit(machine->regP, CARRY_FL,
875 bitOn(machine->regA,NEGATIVE_FL));
876 machine->regA = machine->regA << 1;
877 machine->regA = setBit(machine->regA, CARRY_FL, cf);
878 manZeroNeg(machine, machine->regA);
882 static void jmpRTI(machine_6502 *machine, m6502_AddrMode adm){
883 machine->regP = stackPop(machine);
884 machine->regPC = stackPop(machine);
887 static void jmpRTS(machine_6502 *machine, m6502_AddrMode adm){
889 BOOL isValue = getValue(machine, adm, &ptr);
890 Bit16 nr = stackPop(machine);
891 Bit16 nl = stackPop(machine);
892 warnValue(! isValue);
893 machine->regPC = (nl << 8) | nr;
896 static void jmpSBC(machine_6502 *machine, m6502_AddrMode adm){
899 Bit8 c = bitOn(machine->regP, CARRY_FL);
901 BOOL isValue = getValue(machine, adm, &ptr);
903 /*vflag = (bitOn(machine->regA,NEGATIVE_FL) &&
904 bitOn(ptr.value, NEGATIVE_FL));*/
906 if (bitOn(machine->regP, DECIMAL_FL)) {
907 Bit8 ar = nibble(machine->regA, RIGHT);
908 Bit8 br = nibble(ptr.value, RIGHT);
909 Bit8 al = nibble(machine->regA, LEFT);
910 Bit8 bl = nibble(ptr.value, LEFT);
912 tmp = 0xf + ar - br + c;
923 machine->regP = setBit(machine->regP, CARRY_FL, 0);
924 if (bitOn(machine->regP, OVERFLOW_FL) && w < 0x80)
925 machine->regP = setBit(machine->regP, OVERFLOW_FL, 0);
929 machine->regP = setBit(machine->regP, CARRY_FL, 1);
930 if (bitOn(machine->regP, OVERFLOW_FL) && w >= 0x180)
931 machine->regP = setBit(machine->regP, OVERFLOW_FL, 0);
934 } /* end decimal mode */
936 w = 0xff + machine->regA - ptr.value + c;
938 machine->regP = setBit(machine->regP, CARRY_FL, 0);
939 if (bitOn(machine->regP, OVERFLOW_FL) && w < 0x80)
940 machine->regP = setBit(machine->regP, OVERFLOW_FL, 0);
943 machine->regP = setBit(machine->regP, CARRY_FL, 1);
944 if (bitOn(machine->regP, OVERFLOW_FL) && w >= 0x180)
945 machine->regP = setBit(machine->regP, OVERFLOW_FL, 0);
949 manZeroNeg(machine,machine->regA);
952 static void jmpSTA(machine_6502 *machine, m6502_AddrMode adm){
954 BOOL isValue = getValue(machine, adm, &ptr);
956 memStoreByte(machine,ptr.addr,machine->regA);
959 static void jmpTXS(machine_6502 *machine, m6502_AddrMode adm){
960 stackPush(machine,machine->regX);
963 static void jmpTSX(machine_6502 *machine, m6502_AddrMode adm){
964 machine->regX = stackPop(machine);
965 manZeroNeg(machine, machine->regX);
968 static void jmpPHA(machine_6502 *machine, m6502_AddrMode adm){
969 stackPush(machine, machine->regA);
972 static void jmpPLA(machine_6502 *machine, m6502_AddrMode adm){
973 machine->regA = stackPop(machine);
974 manZeroNeg(machine, machine->regA);
977 static void jmpPHP(machine_6502 *machine, m6502_AddrMode adm){
978 stackPush(machine,machine->regP);
981 static void jmpPLP(machine_6502 *machine, m6502_AddrMode adm){
982 machine->regP = stackPop(machine);
983 machine->regP = setBit(machine->regP, FUTURE_FL, 1);
986 static void jmpSTX(machine_6502 *machine, m6502_AddrMode adm){
988 BOOL isValue = getValue(machine, adm, &ptr);
990 memStoreByte(machine,ptr.addr,machine->regX);
993 static void jmpSTY(machine_6502 *machine, m6502_AddrMode adm){
995 BOOL isValue = getValue(machine, adm, &ptr);
997 memStoreByte(machine,ptr.addr,machine->regY);
1003 static void assignOpCodes(m6502_Opcodes *opcodes){
1005 #define SETOP(num, _name, _Imm, _ZP, _ZPX, _ZPY, _ABS, _ABSX, _ABSY, _INDX, _INDY, _SNGL, _BRA, _func) \
1006 {opcodes[num].name[3] = '\0'; \
1007 strncpy(opcodes[num].name, _name, 3); opcodes[num].Imm = _Imm; opcodes[num].ZP = _ZP; \
1008 opcodes[num].ZPX = _ZPX; opcodes[num].ZPY = _ZPY; opcodes[num].ABS = _ABS; \
1009 opcodes[num].ABSX = _ABSX; opcodes[num].ABSY = _ABSY; opcodes[num].INDX = _INDX; \
1010 opcodes[num].INDY = _INDY; opcodes[num].SNGL = _SNGL; opcodes[num].BRA = _BRA; \
1011 opcodes[num].func = _func;}
1013 /* OPCODE Imm ZP ZPX ZPY ABS ABSX ABSY INDX INDY SGNL BRA Jump Function*/
1014 SETOP( 0, "ADC", 0x69, 0x65, 0x75, 0x00, 0x6d, 0x7d, 0x79, 0x61, 0x71, 0x00, 0x00, jmpADC);
1015 SETOP( 1, "AND", 0x29, 0x25, 0x35, 0x31, 0x2d, 0x3d, 0x39, 0x00, 0x00, 0x00, 0x00, jmpAND);
1016 SETOP( 2, "ASL", 0x00, 0x06, 0x16, 0x00, 0x0e, 0x1e, 0x00, 0x00, 0x00, 0x0a, 0x00, jmpASL);
1017 SETOP( 3, "BIT", 0x00, 0x24, 0x00, 0x00, 0x2c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, jmpBIT);
1018 SETOP( 4, "BPL", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, jmpBPL);
1019 SETOP( 5, "BMI", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, jmpBMI);
1020 SETOP( 6, "BVC", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x50, jmpBVC);
1021 SETOP( 7, "BVS", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, jmpBVS);
1022 SETOP( 8, "BCC", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x90, jmpBCC);
1023 SETOP( 9, "BCS", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xb0, jmpBCS);
1024 SETOP(10, "BNE", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xd0, jmpBNE);
1025 SETOP(11, "BEQ", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, jmpBEQ);
1026 SETOP(12, "CMP", 0xc9, 0xc5, 0xd5, 0x00, 0xcd, 0xdd, 0xd9, 0xc1, 0xd1, 0x00, 0x00, jmpCMP);
1027 SETOP(13, "CPX", 0xe0, 0xe4, 0x00, 0x00, 0xec, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, jmpCPX);
1028 SETOP(14, "CPY", 0xc0, 0xc4, 0x00, 0x00, 0xcc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, jmpCPY);
1029 SETOP(15, "DEC", 0x00, 0xc6, 0xd6, 0x00, 0xce, 0xde, 0x00, 0x00, 0x00, 0x00, 0x00, jmpDEC);
1030 SETOP(16, "EOR", 0x49, 0x45, 0x55, 0x00, 0x4d, 0x5d, 0x59, 0x41, 0x51, 0x00, 0x00, jmpEOR);
1031 SETOP(17, "CLC", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x00, jmpCLC);
1032 SETOP(18, "SEC", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x38, 0x00, jmpSEC);
1033 SETOP(19, "CLI", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x58, 0x00, jmpCLI);
1034 SETOP(20, "SEI", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x78, 0x00, jmpSEI);
1035 SETOP(21, "CLV", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xb8, 0x00, jmpCLV);
1036 SETOP(22, "CLD", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xd8, 0x00, jmpCLD);
1037 SETOP(23, "SED", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf8, 0x00, jmpSED);
1038 SETOP(24, "INC", 0x00, 0xe6, 0xf6, 0x00, 0xee, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, jmpINC);
1039 SETOP(25, "JMP", 0x00, 0x00, 0x00, 0x00, 0x4c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, jmpJMP);
1040 SETOP(26, "JSR", 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, jmpJSR);
1041 SETOP(27, "LDA", 0xa9, 0xa5, 0xb5, 0x00, 0xad, 0xbd, 0xb9, 0xa1, 0xb1, 0x00, 0x00, jmpLDA);
1042 SETOP(28, "LDX", 0xa2, 0xa6, 0x00, 0xb6, 0xae, 0x00, 0xbe, 0x00, 0x00, 0x00, 0x00, jmpLDX);
1043 SETOP(29, "LDY", 0xa0, 0xa4, 0xb4, 0x00, 0xac, 0xbc, 0x00, 0x00, 0x00, 0x00, 0x00, jmpLDY);
1044 SETOP(30, "LSR", 0x00, 0x46, 0x56, 0x00, 0x4e, 0x5e, 0x00, 0x00, 0x00, 0x4a, 0x00, jmpLSR);
1045 SETOP(31, "NOP", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xea, 0x00, jmpNOP);
1046 SETOP(32, "ORA", 0x09, 0x05, 0x15, 0x00, 0x0d, 0x1d, 0x19, 0x01, 0x11, 0x00, 0x00, jmpORA);
1047 SETOP(33, "TAX", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xaa, 0x00, jmpTAX);
1048 SETOP(34, "TXA", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x8a, 0x00, jmpTXA);
1049 SETOP(35, "DEX", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xca, 0x00, jmpDEX);
1050 SETOP(36, "INX", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe8, 0x00, jmpINX);
1051 SETOP(37, "TAY", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xa8, 0x00, jmpTAY);
1052 SETOP(38, "TYA", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x98, 0x00, jmpTYA);
1053 SETOP(39, "DEY", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x88, 0x00, jmpDEY);
1054 SETOP(40, "INY", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc8, 0x00, jmpINY);
1055 SETOP(41, "ROR", 0x00, 0x66, 0x76, 0x00, 0x6e, 0x7e, 0x00, 0x00, 0x00, 0x6a, 0x00, jmpROR);
1056 SETOP(42, "ROL", 0x00, 0x26, 0x36, 0x00, 0x2e, 0x3e, 0x00, 0x00, 0x00, 0x2a, 0x00, jmpROL);
1057 SETOP(43, "RTI", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, jmpRTI);
1058 SETOP(44, "RTS", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x00, jmpRTS);
1059 SETOP(45, "SBC", 0xe9, 0xe5, 0xf5, 0x00, 0xed, 0xfd, 0xf9, 0xe1, 0xf1, 0x00, 0x00, jmpSBC);
1060 SETOP(46, "STA", 0x00, 0x85, 0x95, 0x00, 0x8d, 0x9d, 0x99, 0x81, 0x91, 0x00, 0x00, jmpSTA);
1061 SETOP(47, "TXS", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x9a, 0x00, jmpTXS);
1062 SETOP(48, "TSX", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xba, 0x00, jmpTSX);
1063 SETOP(49, "PHA", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x48, 0x00, jmpPHA);
1064 SETOP(50, "PLA", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x68, 0x00, jmpPLA);
1065 SETOP(51, "PHP", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, jmpPHP);
1066 SETOP(52, "PLP", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x28, 0x00, jmpPLP);
1067 SETOP(53, "STX", 0x00, 0x86, 0x00, 0x96, 0x8e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, jmpSTX);
1068 SETOP(54, "STY", 0x00, 0x84, 0x94, 0x00, 0x8c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, jmpSTY);
1069 SETOP(55, "---", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, NULL);
1072 static void buildIndexCache(machine_6502 *machine){
1074 for (i = 0; i < NUM_OPCODES; i++) {
1075 if (machine->opcodes[i].Imm != 0x00){
1076 machine->opcache[machine->opcodes[i].Imm].adm = IMMEDIATE_VALUE;
1077 machine->opcache[machine->opcodes[i].Imm].index = i;
1079 if (machine->opcodes[i].ZP != 0x00){
1080 machine->opcache[machine->opcodes[i].ZP].adm = ZERO;
1081 machine->opcache[machine->opcodes[i].ZP].index = i;
1083 if (machine->opcodes[i].ZPX != 0x00){
1084 machine->opcache[machine->opcodes[i].ZPX].adm = ZERO_X;
1085 machine->opcache[machine->opcodes[i].ZPX].index = i;;
1087 if (machine->opcodes[i].ZPY != 0x00){
1088 machine->opcache[machine->opcodes[i].ZPY].adm = ZERO_Y;
1089 machine->opcache[machine->opcodes[i].ZPY].index = i;;
1091 if (machine->opcodes[i].ABS != 0x00){
1092 machine->opcache[machine->opcodes[i].ABS].adm = ABS_VALUE;
1093 machine->opcache[machine->opcodes[i].ABS].index = i;;
1095 if (machine->opcodes[i].ABSX != 0x00){
1096 machine->opcache[machine->opcodes[i].ABSX].adm = ABS_X;
1097 machine->opcache[machine->opcodes[i].ABSX].index = i;;
1099 if (machine->opcodes[i].ABSY != 0x00){
1100 machine->opcache[machine->opcodes[i].ABSY].adm = ABS_Y;
1101 machine->opcache[machine->opcodes[i].ABSY].index = i;;
1103 if (machine->opcodes[i].INDX != 0x00){
1104 machine->opcache[machine->opcodes[i].INDX].adm = INDIRECT_X;
1105 machine->opcache[machine->opcodes[i].INDX].index = i;;
1107 if (machine->opcodes[i].INDY != 0x00){
1108 machine->opcache[machine->opcodes[i].INDY].adm = INDIRECT_Y;
1109 machine->opcache[machine->opcodes[i].INDY].index = i;;
1111 if (machine->opcodes[i].SNGL != 0x00){
1112 machine->opcache[machine->opcodes[i].SNGL].adm = SINGLE;
1113 machine->opcache[machine->opcodes[i].SNGL].index = i;
1115 if (machine->opcodes[i].BRA != 0x00){
1116 machine->opcache[machine->opcodes[i].BRA].adm = ABS_OR_BRANCH;
1117 machine->opcache[machine->opcodes[i].BRA].index = i;
1122 /* opIndex() - Search the opcode table for a match. If found return
1123 the index into the optable and the address mode of the opcode. If
1124 the opcode is not found then return -1. */
1125 static int opIndex(machine_6502 *machine, Bit8 opcode, m6502_AddrMode *adm){
1126 /* XXX could catch errors by setting a addressmode of error or something */
1127 *adm = machine->opcache[opcode].adm;
1128 return machine->opcache[opcode].index;
1132 /* Assembly parser */
1134 static Param *newParam(void){
1138 newp = (Param *) ecalloc(1, sizeof(Param));
1139 newp->type = SINGLE;
1140 for (i = 0; i < MAX_PARAM_VALUE; i++)
1143 newp->label = ecalloc(MAX_LABEL_LEN,sizeof(char));
1148 /* Copy the fields from p2 to p1 */
1149 static void copyParam(Param *p1, Param *p2){
1151 strncpy(p1->label,p2->label,MAX_LABEL_LEN);
1152 for(i = 0; i < MAX_PARAM_VALUE; i++)
1153 p1->value[i] = p2->value[i];
1155 p1->type = p2->type;
1158 static Label *newLabel(void){
1161 newp = (Label *) ecalloc(1, sizeof(Label));
1163 newp->label = ecalloc(MAX_LABEL_LEN,sizeof(char));
1168 static AsmLine *newAsmLine(char *cmd, char *label, BOOL decl, Param *param, int lc)
1172 newp = (AsmLine *) ecalloc(1, sizeof(AsmLine));
1173 newp->labelDecl = decl;
1174 newp->label = newLabel();
1175 strncpy(newp->label->label,label,MAX_LABEL_LEN);
1176 newp->command = estrdup(cmd);
1177 newp->param = newParam();
1178 copyParam(newp->param, param);
1183 static AsmLine *addend(AsmLine *listp, AsmLine *newp)
1188 for (p =listp; p->next != NULL; p = p->next)
1194 static BOOL apply(AsmLine *listp, BOOL(*fn)(AsmLine*, void*), void *arg)
1199 for (p = listp; p != NULL; p = p->next)
1205 static void freeParam(Param *param){
1210 static void freeLabel(Label *label){
1215 static void freeallAsmLine(AsmLine *listp)
1218 for(; listp != NULL; listp = next){
1220 freeParam(listp->param);
1221 freeLabel(listp->label);
1222 free(listp->command);
1227 static BOOL addvalue(Param *param,Bit32 value){
1228 /* jwz: suppress "0 <= unsigned" warning */
1229 if (/*0 <= param->vp &&*/ param->vp < MAX_PARAM_VALUE) {
1230 param->value[param->vp++] = value;
1234 fprintf(stderr,"Wrong number of parameters: %d. The limit is %d\n",param->vp+1, MAX_PARAM_VALUE);
1239 static void parseError(char *s){
1240 fprintf(stderr,"6502 Syntax Error: %s\n", s);
1243 /* stoupper() - Destructivley modifies the string making all letters upper case*/
1244 static void stoupper(char **s){
1246 while((*s)[i] != '\0'){
1247 (*s)[i] = toupper((*s)[i]);
1252 static BOOL isWhite(char c){
1253 return (c == '\r' || c == '\t' || c == ' ');
1256 static void skipSpace(char **s){
1257 for(; isWhite(**s); (*s)++)
1261 /* nullify() - fills a string with upto sourceLength null characters. */
1262 static void nullify(char *token, unsigned int sourceLength){
1264 while (i < sourceLength)
1268 static BOOL isBlank(const char *token){
1269 return (token[0] == '\0');
1272 static BOOL isCommand(machine_6502 *machine, const char *token){
1275 while (i < NUM_OPCODES) {
1276 if (strcmp(machine->opcodes[i].name,token) == 0)
1281 if (strcmp(token, "DCB") == 0) return TRUE;
1285 /* hasChar() - Check to see if the current line has a certain
1287 static BOOL hasChar(char *s, char c){
1288 for(; *s != '\0' && *s != '\n'; s++) {
1295 static BOOL ishexdigit(char c){
1299 char c1 = toupper(c);
1300 return ('A' <= c1 && c1 <= 'F');
1304 /* isCmdChar() - Is this a valid character for a command. All of the
1305 command are alpha except for the entry point code that is "*=" */
1306 static BOOL isCmdChar(char c){
1307 return (isalpha(c) || c == '*' || c == '=');
1311 /* command() - parse a command from the source code. We pass along a
1312 machine so the opcode can be validated. */
1313 static BOOL command(machine_6502 *machine, char **s, char **cmd){
1316 for(;isCmdChar(**s) && i < MAX_CMD_LEN; (*s)++)
1319 return TRUE; /* Could be a blank line. */
1320 else if (strcmp(*cmd,"*=") == 0)
1321 return TRUE; /* This is an entry point. */
1323 return isCommand(machine,*cmd);
1326 static BOOL declareLabel(char **s, char **label){
1329 for(;**s != ':' && **s != '\n' && **s != '\0'; (*s)++){
1332 (*label)[i++] = **s;
1335 return FALSE; /* Current line has to have a label */
1336 else if (**s == ':'){
1337 (*s)++; /* Skip colon */
1344 static BOOL parseHex(char **s, Bit32 *value){
1345 enum { MAX_HEX_LEN = 5 };
1347 char *hex = ecalloc(MAX_HEX_LEN, sizeof(char));
1350 (*s)++; /* move pass $ */
1351 for(; ishexdigit(**s) && i < MAX_HEX_LEN; (*s)++)
1354 *value = strtol(hex,NULL,16);
1362 static BOOL parseDec(char **s, Bit32 *value){
1363 enum { MAX_DEC_LEN = 4 };
1364 char *dec = ecalloc(MAX_DEC_LEN, sizeof(char));
1366 for(i = 0; isdigit(**s) && i < MAX_DEC_LEN; (*s)++)
1380 static BOOL parseValue(char **s, Bit32 *value){
1383 return parseHex(s, value);
1385 return parseDec(s, value);
1388 static BOOL paramLabel(char **s, char **label){
1390 for(i = 0; (isalnum(**s) || **s == '_') && i < MAX_LABEL_LEN; (*s)++)
1391 (*label)[i++] = **s;
1399 static BOOL immediate(char **s, Param *param){
1403 (*s)++; /*Move past hash */
1404 if (**s == '<' || **s == '>'){
1405 char *label = ecalloc(MAX_LABEL_LEN, sizeof(char));
1406 param->type = (**s == '<') ? IMMEDIATE_LESS : IMMEDIATE_GREAT;
1407 (*s)++; /* move past < or > */
1408 if (paramLabel(s, &label)){
1409 int ln = strlen(label) + 1;
1410 strncpy(param->label, label, ln);
1418 if (parseValue(s, &value)){
1420 parseError("Immediate value is too large.");
1423 param->type = IMMEDIATE_VALUE;
1424 return addvalue(param, value);
1430 static BOOL isDirection(char c){
1431 return (c == 'X' || c == 'Y');
1434 static BOOL getDirection(char **s, char *direction){
1439 if (isDirection(**s)){
1448 static BOOL indirect(char **s, Param *param){
1456 if (! parseHex(s,&value))
1459 parseError("Indirect value is too large.");
1462 if (!addvalue(param, value))
1467 if (getDirection(s,&c)) {
1469 param->type = INDIRECT_Y;
1474 else if (getDirection(s, &c)){
1479 param->type = INDIRECT_X;
1487 static BOOL dcbValue(char **s, Param *param){
1489 if (! parseValue(s,&val))
1495 if (!addvalue(param,val))
1498 param->type = DCB_PARAM;
1503 return dcbValue(s, param);
1509 static BOOL value(char **s, Param *param){
1514 if (! parseValue(s,&val))
1518 dir = getDirection(s,&c);
1519 if (!addvalue(param,val))
1524 param->type = ABS_X;
1526 param->type = ABS_Y;
1531 param->type = ABS_VALUE;
1534 param->type = ZERO_X;
1536 param->type = ZERO_Y;
1546 static BOOL label(char **s, Param *param){
1547 char *label = ecalloc(MAX_LABEL_LEN, sizeof(char));
1549 BOOL labelOk = FALSE;
1550 if (paramLabel(s, &label)){
1552 param->type = ABS_OR_BRANCH;
1553 if (getDirection(s, &c)){
1555 param->type = ABS_LABEL_X;
1557 param->type = ABS_LABEL_Y;
1561 strncpy(param->label,label,MAX_LABEL_LEN);
1567 static BOOL parameter(const char *cmd, char **s, Param *param){
1569 if (**s == '\0' || **s == '\n')
1571 else if (**s == '#')
1572 return immediate(s,param);
1573 else if (**s == '(')
1574 return indirect(s,param);
1575 else if (**s == '$' || isdigit(**s)){
1576 if (strcmp(cmd, "DCB") == 0)
1577 return dcbValue(s,param);
1579 return value(s,param);
1581 else if (isalpha(**s))
1582 return label(s ,param);
1584 return FALSE; /* Invalid Parameter */
1587 static void comment(char **s){
1590 for(;**s != '\n' && **s != '\0'; (*s)++)
1594 static void initParam(Param *param){
1596 param->type = SINGLE;
1597 for(i = 0; i < MAX_PARAM_VALUE; i++)
1598 param->value[i] = 0;
1600 nullify(param->label,MAX_LABEL_LEN);
1604 static AsmLine *parseAssembly(machine_6502 *machine, BOOL *codeOk, const char *code){
1606 char *cmd = ecalloc(MAX_CMD_LEN, sizeof(char));
1607 char *label = ecalloc(MAX_LABEL_LEN, sizeof(char));
1608 char *start; /*pointer to the start of the code.*/
1609 unsigned int lc = 1;
1612 AsmLine *listp = NULL;
1620 while(*s != '\0' && *codeOk){
1622 nullify(cmd, MAX_CMD_LEN);
1623 nullify(label, MAX_LABEL_LEN);
1630 continue; /* blank line */
1632 else if (*s == '\0')
1633 continue; /* no newline at the end of the code */
1634 else if (hasChar(s,':')){
1636 if(! declareLabel(&s,&label)){
1642 if(!command(machine, &s, &cmd)){
1648 if(!parameter(cmd, &s, param)){
1654 if (*s == '\n' || *s == '\0'){
1656 asmm = newAsmLine(cmd,label,decl,param,lc);
1657 listp = addend(listp,asmm);
1665 fprintf(stderr,"Syntax error at line %u\n", lc);
1674 /* fileToBuffer() - Allocates a buffer and loads all of the file into memory. */
1675 static char *fileToBuffer(const char *filename){
1676 const int defaultSize = 1024;
1679 int size = defaultSize;
1681 char *buffer = ecalloc(defaultSize,sizeof(char));
1683 if (!buffer) abort();
1685 ifp = fopen(filename, "rb");
1688 while((c = getc(ifp)) != EOF){
1691 size += defaultSize;
1692 buffer = realloc(buffer, size);
1693 if (buffer == NULL) {
1699 buffer = realloc(buffer, i+2);
1700 if (!buffer) abort();
1701 /* Make sure we have a line feed at the end */
1711 /* reset() - Reset CPU and memory. */
1712 static void reset(machine_6502 *machine){
1714 for ( y = 0; y < 32; y++ ){
1715 for (x = 0; x < 32; x++){
1716 machine->screen[x][y] = 0;
1720 for(x=0; x < MEM_64K; x++)
1721 machine->memory[x] = 0;
1723 machine->codeCompiledOK = FALSE;
1727 machine->regP = setBit(machine->regP, FUTURE_FL, 1);
1728 machine->defaultCodePC = machine->regPC = PROG_START;
1729 machine->regSP = STACK_TOP;
1730 machine->runForever = FALSE;
1731 machine->labelPtr = 0;
1732 machine->codeRunning = FALSE;
1735 /* hexDump() - Dump the memory to output */
1736 void m6502_hexDump(machine_6502 *machine, Bit16 start, Bit16 numbytes, FILE *output){
1739 for( i = 0; i < numbytes; i++){
1740 address = start + i;
1741 if ( (i&15) == 0 ) {
1742 fprintf(output,"\n%.4x: ", address);
1744 fprintf(output,"%.2x%s",machine->memory[address], (i & 1) ? " ":"");
1746 fprintf(output,"%s\n",(i&1)?"--":"");
1750 /* void save_program(machine_6502 *machine, char *filename){ */
1752 /* Bit16 pc = PROG_START; */
1753 /* Bit16 end = pc + machine->codeLen; */
1755 /* ofp = fopen(filename, "w"); */
1756 /* if (!ofp) abort(); */
1758 /* fprintf(ofp,"Bit8 prog[%d] =\n{",machine->codeLen); */
1760 /* while(pc < end) */
1761 /* fprintf(ofp,"0x%.2x,%s",machine->memory[pc++],n++%10?" ":"\n"); */
1762 /* fseek(ofp,-2,SEEK_CUR); */
1763 /* fprintf(ofp,"};\n"); */
1768 static BOOL translate(m6502_Opcodes *op,Param *param, machine_6502 *machine){
1769 switch(param->type){
1772 pushByte(machine, op->SNGL);
1774 fprintf(stderr,"%s needs a parameter.\n",op->name);
1778 case IMMEDIATE_VALUE:
1780 pushByte(machine, op->Imm);
1781 pushByte(machine, param->value[0]);
1785 fprintf(stderr,"%s does not take IMMEDIATE_VALUE parameters.\n",op->name);
1788 case IMMEDIATE_GREAT:
1790 pushByte(machine, op->Imm);
1791 pushByte(machine, param->lbladdr >> 8);
1795 fprintf(stderr,"%s does not take IMMEDIATE_GREAT parameters.\n",op->name);
1798 case IMMEDIATE_LESS:
1800 pushByte(machine, op->Imm);
1801 pushByte(machine, param->lbladdr & 0xFF);
1805 fprintf(stderr,"%s does not take IMMEDIATE_LESS parameters.\n",op->name);
1810 pushByte(machine, op->INDX);
1811 pushByte(machine, param->value[0]);
1815 fprintf(stderr,"%s does not take INDIRECT_X parameters.\n",op->name);
1820 pushByte(machine, op->INDY);
1821 pushByte(machine, param->value[0]);
1825 fprintf(stderr,"%s does not take INDIRECT_Y parameters.\n",op->name);
1830 pushByte(machine, op->ZP);
1831 pushByte(machine, param->value[0]);
1835 fprintf(stderr,"%s does not take ZERO parameters.\n",op->name);
1840 pushByte(machine, op->ZPX);
1841 pushByte(machine, param->value[0]);
1845 fprintf(stderr,"%s does not take ZERO_X parameters.\n",op->name);
1850 pushByte(machine, op->ZPY);
1851 pushByte(machine, param->value[0]);
1855 fprintf(stderr,"%s does not take ZERO_Y parameters.\n",op->name);
1860 pushByte(machine, op->ABS);
1861 pushWord(machine, param->value[0]);
1865 fprintf(stderr,"%s does not take ABS_VALUE parameters.\n",op->name);
1870 pushByte(machine, op->ABS);
1871 pushWord(machine, param->lbladdr);
1875 pushByte(machine, op->BRA);
1877 int diff = abs((int)param->lbladdr - (int)machine->defaultCodePC);
1878 int backward = (param->lbladdr < machine->defaultCodePC);
1879 pushByte(machine, (backward) ? 0xff - diff : diff - 1);
1883 fprintf(stderr,"%s does not take BRANCH parameters.\n",op->name);
1890 pushByte(machine, op->ABSX);
1891 pushWord(machine, param->value[0]);
1895 fprintf(stderr,"%s does not take ABS_X parameters.\n",op->name);
1900 pushByte(machine, op->ABSY);
1901 pushWord(machine, param->value[0]);
1905 fprintf(stderr,"%s does not take ABS_Y parameters.\n",op->name);
1910 pushByte(machine, op->ABSX);
1911 pushWord(machine, param->lbladdr);
1915 fprintf(stderr,"%s does not take ABS_LABEL_X parameters.\n",op->name);
1920 pushByte(machine, op->ABSY);
1921 pushWord(machine, param->lbladdr);
1925 fprintf(stderr,"%s does not take ABS_LABEL_Y parameters.\n",op->name);
1929 /* Handled elsewhere */
1935 /* compileLine() - Compile one line of code. Returns
1936 TRUE if it compile successfully. */
1937 static BOOL compileLine(AsmLine *asmline, void *args){
1938 machine_6502 *machine;
1940 if (isBlank(asmline->command)) return TRUE;
1941 if (strcmp("*=",asmline->command) == 0){
1942 machine->defaultCodePC = asmline->param->value[0];
1944 else if (strcmp("DCB",asmline->command) == 0){
1946 for(i = 0; i < asmline->param->vp; i++)
1947 pushByte(machine, asmline->param->value[i]);
1951 char *command = asmline->command;
1953 for(i = 0; i < NUM_OPCODES; i++){
1954 if (strcmp(machine->opcodes[i].name, command) == 0){
1955 op = machine->opcodes[i];
1959 if (i == NUM_OPCODES)
1960 return FALSE; /* unknow upcode */
1962 return translate(&op,asmline->param,machine);
1967 /* indexLabels() - Get the address for each label */
1968 static BOOL indexLabels(AsmLine *asmline, void *arg){
1969 machine_6502 *machine;
1973 oldDefault = machine->defaultCodePC;
1974 thisPC = machine->regPC;
1975 /* Figure out how many bytes this instruction takes */
1976 machine->codeLen = 0;
1978 if ( ! compileLine(asmline, machine) ){
1982 /* If the machine's defaultCodePC has changed then we encountered a
1983 *= which changes the load address. We need to initials our code
1984 *counter with the current default. */
1985 if (oldDefault == machine->defaultCodePC){
1986 machine->regPC += machine->codeLen;
1989 machine->regPC = machine->defaultCodePC;
1990 /*oldDefault = machine->defaultCodePC;*/
1993 if (asmline->labelDecl) {
1994 asmline->label->addr = thisPC;
1999 static BOOL changeParamLabelAddr(AsmLine *asmline, void *label){
2001 if (strcmp(asmline->param->label, la->label) == 0)
2002 asmline->param->lbladdr = la->addr;
2006 static BOOL linkit(AsmLine *asmline, void *asmlist){
2007 apply(asmlist,changeParamLabelAddr,asmline->label);
2011 /* linkLabels - Make sure all of the references to the labels contain
2013 static void linkLabels(AsmLine *asmlist){
2014 apply(asmlist,linkit,asmlist);
2017 /* compileCode() - Compile the current assembly code for the machine */
2018 static BOOL compileCode(machine_6502 *machine, const char *code){
2023 machine->defaultCodePC = machine->regPC = PROG_START;
2024 asmlist = parseAssembly(machine, &codeOk, code);
2027 /* First pass: Find the addresses for the labels */
2028 if (!apply(asmlist, indexLabels, machine))
2030 /* update label references */
2031 linkLabels(asmlist);
2033 #if 0 /* prints out some debugging information */
2036 if(asmlist != NULL){
2037 for (p = asmlist; p != NULL; p = p->next)
2038 fprintf(stderr,"%s lbl: %s addr: %d ParamLbl: %s ParamAddr: %d\n",
2039 p->command, p->label->label, p->label->addr,
2040 p->param->label, p->param->lbladdr);
2046 /* Second pass: translate the instructions */
2047 machine->codeLen = 0;
2048 /* Link label call push_byte which increments defaultCodePC.
2049 We need to reset it so the compiled code goes in the
2051 machine->defaultCodePC = PROG_START;
2052 if (!apply(asmlist, compileLine, machine))
2055 if (machine->defaultCodePC > PROG_START ){
2056 machine->memory[machine->defaultCodePC] = 0x00;
2060 fprintf(stderr,"No Code to run.\n");
2065 fprintf(stderr,"An error occured while parsing the file.\n");
2068 freeallAsmLine(asmlist);
2074 * execute() - Executes one instruction.
2075 * This is the main part of the CPU emulator.
2079 static void execute(machine_6502 *machine){
2084 if(!machine->codeRunning) return;
2086 opcode = popByte(machine);
2088 machine->codeRunning = FALSE;
2090 opidx = opIndex(machine,opcode,&adm);
2092 machine->opcodes[opidx].func(machine, adm);
2094 fprintf(stderr,"Invalid opcode!\n");
2096 if( (machine->regPC == 0) ||
2097 (!machine->codeRunning) ) {
2098 machine->codeRunning = FALSE;
2102 machine_6502 *m6502_build(void){
2103 machine_6502 *machine;
2104 machine = ecalloc(1, sizeof(machine_6502));
2105 assignOpCodes(machine->opcodes);
2106 buildIndexCache(machine);
2111 void m6502_destroy6502(machine_6502 *machine){
2116 void m6502_trace(machine_6502 *machine, FILE *output){
2117 Bit8 opcode = memReadByte(machine,machine->regPC);
2120 int opidx = opIndex(machine,opcode,&adm);
2121 int stacksz = STACK_TOP - machine->regSP;
2123 fprintf(output,"\n NVFBDIZC\nP: %d%d%d%d%d%d%d%d ",
2124 bitOn(machine->regP,NEGATIVE_FL),
2125 bitOn(machine->regP,OVERFLOW_FL),
2126 bitOn(machine->regP,FUTURE_FL),
2127 bitOn(machine->regP,BREAK_FL),
2128 bitOn(machine->regP,DECIMAL_FL),
2129 bitOn(machine->regP,INTERRUPT_FL),
2130 bitOn(machine->regP,ZERO_FL),
2131 bitOn(machine->regP,CARRY_FL));
2132 fprintf(output,"A: %.2x X: %.2x Y: %.2x SP: %.4x PC: %.4x\n",
2133 machine->regA, machine->regX, machine->regY, machine->regSP, machine->regPC);
2135 Bit16 pc = machine->regPC;
2136 fprintf(output,"\n%.4x:\t%s",machine->regPC, machine->opcodes[opidx].name);
2137 if (peekValue(machine, adm, &ptr, pc+1))
2138 fprintf(output,"\tAddress:%.4x\tValue:%.4x\n",
2139 ptr.addr,ptr.value);
2141 fprintf(output,"\n");
2143 fprintf(output,"STACK:");
2144 m6502_hexDump(machine,(STACK_TOP - stacksz) + 1, stacksz, output);
2148 void disassemble(machine_6502 *machine, FILE *output){
2150 increment the program counter
2152 loop until end of program. */
2159 Bit16 opc = machine->regPC;
2160 mem = calloc(20,sizeof(char));
2161 machine->regPC = PROG_START;
2163 addr = machine->regPC;
2164 opcode = popByte(machine);
2165 opidx = opIndex(machine,opcode,&adm);
2166 for (i = 0; i < 20; i++) mem[i] = '\0';
2167 dismem(machine, adm, mem);
2168 fprintf(output,"%x\t%s\t%s\n",
2169 addr,machine->opcodes[opidx].name,mem);
2170 }while((machine->regPC - PROG_START) < machine->codeLen); /*XXX - may need to change since defaultCodePC */
2172 machine->regPC = opc;
2178 void m6502_eval_file(machine_6502 *machine, const char *filename, m6502_Plotter plot, void *plotterState){
2181 machine->plot = plot;
2182 machine->plotterState = plotterState;
2184 code = fileToBuffer(filename);
2186 if (! compileCode(machine, code) ) abort();
2190 machine->defaultCodePC = machine->regPC = PROG_START;
2191 machine->codeRunning = TRUE;
2198 }while(machine->codeRunning);
2201 void m6502_start_eval_file(machine_6502 *machine, const char *filename, m6502_Plotter plot, void *plotterState){
2205 machine->plot = plot;
2206 machine->plotterState = plotterState;
2208 code = fileToBuffer(filename);
2210 if (! compileCode(machine, code) ) abort();
2214 machine->defaultCodePC = machine->regPC = PROG_START;
2215 machine->codeRunning = TRUE;
2218 #endif /* READ_FILES */
2220 void m6502_start_eval_string(machine_6502 *machine, const char *code,
2221 m6502_Plotter plot, void *plotterState){
2224 machine->plot = plot;
2225 machine->plotterState = plotterState;
2227 if (! compileCode(machine, code) ){
2228 fprintf(stderr,"Could not compile code.\n");
2231 machine->defaultCodePC = machine->regPC = PROG_START;
2232 machine->codeRunning = TRUE;
2236 /* void start_eval_binary(machine_6502 *machine, Bit8 *program, */
2237 /* unsigned int proglen, */
2238 /* Plotter plot, void *plotterState){ */
2239 /* unsigned int pc, n; */
2240 /* reset(machine); */
2241 /* machine->plot = plot; */
2242 /* machine->plotterState = plotterState; */
2244 /* machine->regPC = PROG_START; */
2245 /* pc = machine->regPC; */
2246 /* machine->codeLen = proglen; */
2248 /* while (n < proglen){ */
2249 /* machine->memory[pc++] = program[n++]; */
2251 /* machine->codeRunning = TRUE; */
2252 /* execute(machine); */
2255 void m6502_next_eval(machine_6502 *machine, int insno){
2257 for (i = 1; i < insno; i++){
2258 if (machine->codeRunning){
2260 trace(machine, stdout);