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.
23 #define NDEBUG /* Uncomment when done with debugging */
27 /*#include <malloc.h>*/
51 |N||V||F||B||D||I||Z||C|
58 CARRY_FL = 0, ZERO_FL = 1, INTERRUPT_FL = 2,
59 DECIMAL_FL = 3, BREAK_FL = 4, FUTURE_FL = 5,
60 OVERFLOW_FL = 6, NEGATIVE_FL = 7
64 typedef BOOL (*CharTest) (char);
66 /* A jump function takes a pointer to the current machine and a
67 opcode. The opcode is needed to figure out the memory mode. */
68 /*typedef void (*JumpFunc) (machine_6502* AddrMode);*/
72 Bit32 value[MAX_PARAM_VALUE];
73 unsigned int vp; /*value pointer, index into the value table.*/
79 Bit32 addr; /* Address of the label */
83 typedef struct AsmLine AsmLine;
85 BOOL labelDecl; /* Does the line have a label declaration? */
89 AsmLine *next; /* in list */
97 /* eprintf - Taken from "Practice of Programming" by Kernighan and Pike */
98 static void eprintf(char *fmt, ...){
101 char *progname = "Assembler";
104 if (progname != NULL)
105 fprintf(stderr, "%s: ", progname);
108 vfprintf(stderr, fmt, args);
111 if (fmt[0] != '\0' && fmt[strlen(fmt) -1] == ':')
112 fprintf(stderr, " %s", strerror(errno));
113 fprintf(stderr, "\n");
114 exit(2); /* conventional value for failed execution */
117 /* emalloc - Taken from "Practice of Programming" by Kernighan and
118 Pike. If memory allocatiion fails the program will print a message
120 static void *emalloc(size_t n) {
125 eprintf("malloc of %u bytes failed:", n);
129 /* ecalloc - Dose the same thing as emalloc just calls calloc instead. */
130 static void *ecalloc(uint32_t nelm, size_t nsize){
133 p = calloc(nelm, nsize);
135 eprintf("calloc of %u bytes failed:", nelm * nsize);
139 /* estrdup() - Allocates memory for a new string a returns a copy of the source sting in it. */
140 static char *estrdup(const char *source){
141 int ln = strlen(source) + 1;
142 char *s = ecalloc(ln, sizeof(char));
143 strncpy(s,source,ln);
147 static void checkAddress(Bit32 address){
148 /* XXX: Do we want to kill the program here? */
149 if (address >= MEM_64K)
150 fprintf(stderr, "Address %d is beyond 64k", address);
154 * stackPush() - Push byte to stack
158 static void stackPush(machine_6502 *machine, Bit8 value ) {
159 if(machine->regSP >= STACK_BOTTOM){
160 machine->memory[machine->regSP--] = value;
163 fprintf(stderr, "The stack is full: %.4x\n", machine->regSP);
164 machine->codeRunning = FALSE;
170 * stackPop() - Pop byte from stack
174 static Bit8 stackPop(machine_6502 *machine) {
175 if (machine->regSP < STACK_TOP){
176 Bit8 value =machine->memory[++machine->regSP];
180 /* fprintf(stderr, "The stack is empty.\n"); xxx */
181 machine->codeRunning = FALSE;
186 static void pushByte(machine_6502 *machine, Bit32 value ) {
187 Bit32 address = machine->defaultCodePC;
188 checkAddress(address);
189 machine->memory[address] = value & 0xff;
191 machine->defaultCodePC++;
195 * pushWord() - Push a word using pushByte twice
199 static void pushWord(machine_6502 *machine, Bit16 value ) {
200 pushByte(machine, value & 0xff );
201 pushByte(machine, (value>>8) & 0xff );
205 * popByte( machine_6502 *machine,) - Pops a byte
209 static Bit8 popByte( machine_6502 *machine) {
210 Bit8 value = machine->memory[machine->regPC];
216 * popWord() - Pops a word using popByte() twice
220 static int popWord(machine_6502 *machine) {
221 return popByte(machine) + (popByte(machine) << 8);
226 * memReadByte() - Peek a byte, don't touch any registers
230 static int memReadByte( machine_6502 *machine, int addr ) {
231 if( addr == 0xfe ) return floor( random()%255 );
232 return machine->memory[addr];
235 static void updateDisplayPixel(machine_6502 *machine, Bit16 addr){
236 Bit8 idx = memReadByte(machine,addr) & 0x0f;
242 machine->plot(x,y,idx,machine->plotterState);
247 * memStoreByte() - Poke a byte, don't touch any registers
251 static void memStoreByte( machine_6502 *machine, int addr, int value ) {
252 machine->memory[ addr ] = (value & 0xff);
253 if( (addr >= 0x200) && (addr<=0x5ff) )
254 updateDisplayPixel(machine, addr );
261 static Bit8 bitOn(Bit8 value,Flags bit){
264 return ((value & mask) > 0);
267 static Bit8 bitOff(Bit8 value, Flags bit){
268 return (! bitOn(value,bit));
271 static Bit8 setBit(Bit8 value, Flags bit, int on){
274 onMask = onMask << bit;
275 offMask = offMask ^ onMask;
276 return ((on) ? value | onMask : value & offMask);
279 static Bit8 nibble(Bit8 value, Side side){
281 case LEFT: return value & 0xf0;
282 case RIGHT: return value & 0xf;
284 fprintf(stderr,"nibble unknown side\n");
290 /* used for tracing. XXX: combined with function getvalue */
291 static BOOL peekValue(machine_6502 *machine, AddrMode adm, Pointer *pointer, Bit16 PC){
299 case IMMEDIATE_GREAT:
300 case IMMEDIATE_VALUE:
301 pointer->value = memReadByte(machine, PC);
304 zp = memReadByte(machine, PC) + machine->regX;
305 pointer->addr = memReadByte(machine,zp) +
306 (memReadByte(machine,zp+1)<<8);
307 pointer->value = memReadByte(machine, pointer->addr);
310 zp = memReadByte(machine, PC);
311 pointer->addr = memReadByte(machine,zp) +
312 (memReadByte(machine,zp+1)<<8) + machine->regY;
313 pointer->value = memReadByte(machine, pointer->addr);
316 pointer->addr = memReadByte(machine, PC);
317 pointer->value = memReadByte(machine, pointer->addr);
320 pointer->addr = memReadByte(machine, PC) + machine->regX;
321 pointer->value = memReadByte(machine, pointer->addr);
324 pointer->addr = memReadByte(machine, PC) + machine->regY;
325 pointer->value = memReadByte(machine, pointer->addr);
328 pointer->addr = memReadByte(machine, PC);
331 pointer->addr = memReadByte(machine, PC) + (memReadByte(machine, PC+1) << 8);
332 pointer->value = memReadByte(machine, pointer->addr);
336 pointer->addr = (memReadByte(machine, PC) +
337 (memReadByte(machine, PC+1) << 8)) + machine->regX;
338 pointer->value = memReadByte(machine, pointer->addr);
342 pointer->addr = (memReadByte(machine, PC) +
343 (memReadByte(machine, PC+1) << 8)) + machine->regY;
344 pointer->value = memReadByte(machine, pointer->addr);
347 /* Handled elsewhere */
355 /* Figure out how to get the value from the addrmode and get it.*/
356 static BOOL getValue(machine_6502 *machine, AddrMode adm, Pointer *pointer){
364 case IMMEDIATE_GREAT:
365 case IMMEDIATE_VALUE:
366 pointer->value = popByte(machine);
369 zp = popByte(machine) + machine->regX;
370 pointer->addr = memReadByte(machine,zp) +
371 (memReadByte(machine,zp+1)<<8);
372 pointer->value = memReadByte(machine, pointer->addr);
375 zp = popByte(machine);
376 pointer->addr = memReadByte(machine,zp) +
377 (memReadByte(machine,zp+1)<<8) + machine->regY;
378 pointer->value = memReadByte(machine, pointer->addr);
381 pointer->addr = popByte(machine);
382 pointer->value = memReadByte(machine, pointer->addr);
385 pointer->addr = popByte(machine) + machine->regX;
386 pointer->value = memReadByte(machine, pointer->addr);
389 pointer->addr = popByte(machine) + machine->regY;
390 pointer->value = memReadByte(machine, pointer->addr);
393 pointer->addr = popByte(machine);
396 pointer->addr = popWord(machine);
397 pointer->value = memReadByte(machine, pointer->addr);
401 pointer->addr = popWord(machine) + machine->regX;
402 pointer->value = memReadByte(machine, pointer->addr);
406 pointer->addr = popWord(machine) + machine->regY;
407 pointer->value = memReadByte(machine, pointer->addr);
410 /* Handled elsewhere */
417 static void dismem(machine_6502 *machine, AddrMode adm, char *output){
425 case IMMEDIATE_GREAT:
426 case IMMEDIATE_VALUE:
427 n = popByte(machine);
428 sprintf(output,"#$%x",n);
431 zp = popByte(machine);
432 n = memReadByte(machine,zp) +
433 (memReadByte(machine,zp+1)<<8);
434 sprintf(output,"($%x,x)",n);
437 zp = popByte(machine);
438 n = memReadByte(machine,zp) +
439 (memReadByte(machine,zp+1)<<8);
440 sprintf(output,"($%x),y",n);
444 n = popByte(machine);
445 sprintf(output,"$%x",n);
448 n = popByte(machine);
449 sprintf(output,"$%x,x",n);
452 n = popByte(machine);
453 sprintf(output,"$%x,y",n);
456 n = popWord(machine);
457 sprintf(output,"$%x",n);
461 n = popWord(machine);
462 sprintf(output,"$%x,x",n);
466 n = popWord(machine);
467 sprintf(output,"$%x,x",n);
475 /* manZeroNeg - Manage the negative and zero flags */
476 static void manZeroNeg(machine_6502 *machine, Bit8 value){
477 machine->regP = setBit(machine->regP, ZERO_FL, (value == 0));
478 machine->regP = setBit(machine->regP, NEGATIVE_FL, bitOn(value,NEGATIVE_FL));
481 static void warnValue(BOOL isValue){
483 fprintf(stderr,"Invalid Value from getValue.\n");
487 static void jmpADC(machine_6502 *machine, AddrMode adm){
490 Bit8 c = bitOn(machine->regP, CARRY_FL);
491 BOOL isValue = getValue(machine, adm, &ptr);
495 if (bitOn(machine->regA, NEGATIVE_FL) &&
496 bitOn(ptr.value, NEGATIVE_FL))
497 machine->regP = setBit(machine->regP, OVERFLOW_FL, 0);
499 machine->regP = setBit(machine->regP, OVERFLOW_FL, 1);
501 if (bitOn(machine->regP, DECIMAL_FL)) {
502 tmp = nibble(machine->regA,RIGHT) + nibble(ptr.value,RIGHT ) + c;
503 /* The decimal part is limited to 0 through 9 */
505 tmp = 0x10 | ((tmp + 6) & 0xf);
507 tmp += nibble(machine->regA,LEFT) + nibble(ptr.value,LEFT);
509 machine->regP = setBit(machine->regP,CARRY_FL,1);
510 if (bitOn(machine->regP, OVERFLOW_FL) && tmp >= 0x180)
511 machine->regP = setBit(machine->regP, OVERFLOW_FL, 0);
515 machine->regP = setBit(machine->regP,CARRY_FL,0);
516 if (bitOn(machine->regP, OVERFLOW_FL) && tmp < 0x80)
517 machine->regP = setBit(machine->regP, OVERFLOW_FL, 0);
521 tmp = machine->regA + ptr.value + c;
523 machine->regP = setBit(machine->regP,CARRY_FL,1);
524 if (bitOn(machine->regP, OVERFLOW_FL) && tmp >= 0x180)
525 machine->regP =setBit(machine->regP, OVERFLOW_FL, 0);
528 machine->regP = setBit(machine->regP,CARRY_FL,0);
529 if (bitOn(machine->regP, OVERFLOW_FL) && tmp < 0x80)
530 machine->regP =setBit(machine->regP, OVERFLOW_FL, 0);
535 manZeroNeg(machine,machine->regA);
538 static void jmpAND(machine_6502 *machine, AddrMode adm){
540 BOOL isValue = getValue(machine, adm, &ptr);
542 machine->regA &= ptr.value;
543 manZeroNeg(machine,machine->regA);
546 static void jmpASL(machine_6502 *machine, AddrMode adm){
548 BOOL isValue = getValue(machine, adm, &ptr);
550 machine->regP = setBit(machine->regP, CARRY_FL, bitOn(ptr.value, NEGATIVE_FL));
551 ptr.value = ptr.value << 1;
552 ptr.value = setBit(ptr.value, CARRY_FL, 0);
553 memStoreByte(machine, ptr.addr, ptr.value);
554 manZeroNeg(machine,ptr.value);
556 else { /* Accumulator */
557 machine->regP = setBit(machine->regP, CARRY_FL, bitOn(machine->regA, NEGATIVE_FL));
558 machine->regA = machine->regA << 1;
559 machine->regA = setBit(machine->regA, CARRY_FL, 0);
560 manZeroNeg(machine,machine->regA);
565 static void jmpBIT(machine_6502 *machine, AddrMode adm){
567 BOOL isValue = getValue(machine, adm, &ptr);
569 machine->regP = setBit(machine->regP, ZERO_FL, (ptr.value & machine->regA));
570 machine->regP = setBit(machine->regP, OVERFLOW_FL, bitOn(ptr.value, OVERFLOW_FL));
571 machine->regP = setBit(machine->regP, NEGATIVE_FL, bitOn(ptr.value, NEGATIVE_FL));
575 static void jumpBranch(machine_6502 *machine, Bit16 offset){
577 machine->regPC = machine->regPC - (0x100 - offset);
579 machine->regPC = machine->regPC + offset;
582 static void jmpBPL(machine_6502 *machine, AddrMode adm){
584 BOOL isValue = getValue(machine, adm, &ptr);
586 if (bitOff(machine->regP,NEGATIVE_FL))
587 jumpBranch(machine, ptr.addr);
591 static void jmpBMI(machine_6502 *machine, AddrMode adm){
593 BOOL isValue = getValue(machine, adm, &ptr);
595 if (bitOn(machine->regP,NEGATIVE_FL))
596 jumpBranch(machine, ptr.addr);
600 static void jmpBVC(machine_6502 *machine, AddrMode adm){
602 BOOL isValue = getValue(machine, adm, &ptr);
604 if (bitOff(machine->regP,OVERFLOW_FL))
605 jumpBranch(machine, ptr.addr);
608 static void jmpBVS(machine_6502 *machine, AddrMode adm){
610 BOOL isValue = getValue(machine, adm, &ptr);
612 if (bitOn(machine->regP,OVERFLOW_FL))
613 jumpBranch(machine, ptr.addr);
616 static void jmpBCC(machine_6502 *machine, AddrMode adm){
618 BOOL isValue = getValue(machine, adm, &ptr);
620 if (bitOff(machine->regP,CARRY_FL))
621 jumpBranch(machine, ptr.addr);
624 static void jmpBCS(machine_6502 *machine, AddrMode adm){
626 BOOL isValue = getValue(machine, adm, &ptr);
628 if (bitOn(machine->regP,CARRY_FL))
629 jumpBranch(machine, ptr.addr);
632 static void jmpBNE(machine_6502 *machine, AddrMode adm){
634 BOOL isValue = getValue(machine, adm, &ptr);
636 if (bitOff(machine->regP, ZERO_FL))
637 jumpBranch(machine, ptr.addr);
640 static void jmpBEQ(machine_6502 *machine, AddrMode adm){
642 BOOL isValue = getValue(machine, adm, &ptr);
644 if (bitOn(machine->regP, ZERO_FL))
645 jumpBranch(machine, ptr.addr);
648 static void doCompare(machine_6502 *machine, Bit16 reg, Pointer *ptr){
649 machine->regP = setBit(machine->regP,CARRY_FL, ((reg + ptr->value) > 0xff));
650 manZeroNeg(machine,(reg - ptr->value));
653 static void jmpCMP(machine_6502 *machine, AddrMode adm){
655 BOOL isValue = getValue(machine, adm, &ptr);
657 doCompare(machine,machine->regA,&ptr);
660 static void jmpCPX(machine_6502 *machine, AddrMode adm){
662 BOOL isValue = getValue(machine, adm, &ptr);
664 doCompare(machine,machine->regX,&ptr);
667 static void jmpCPY(machine_6502 *machine, AddrMode adm){
669 BOOL isValue = getValue(machine, adm, &ptr);
671 doCompare(machine,machine->regY,&ptr);
674 static void jmpDEC(machine_6502 *machine, AddrMode adm){
676 BOOL isValue = getValue(machine, adm, &ptr);
682 memStoreByte(machine, ptr.addr, ptr.value);
683 manZeroNeg(machine,ptr.value);
686 static void jmpEOR(machine_6502 *machine, AddrMode adm){
688 BOOL isValue = getValue(machine, adm, &ptr);
690 machine->regA ^= ptr.value;
691 manZeroNeg(machine, machine->regA);
694 static void jmpCLC(machine_6502 *machine, AddrMode adm){
695 machine->regP = setBit(machine->regP, CARRY_FL, 0);
698 static void jmpSEC(machine_6502 *machine, AddrMode adm){
699 machine->regP = setBit(machine->regP, CARRY_FL, 1);
702 static void jmpCLI(machine_6502 *machine, AddrMode adm){
703 machine->regP = setBit(machine->regP, INTERRUPT_FL, 0);
706 static void jmpSEI(machine_6502 *machine, AddrMode adm){
707 machine->regP = setBit(machine->regP, INTERRUPT_FL, 1);
710 static void jmpCLV(machine_6502 *machine, AddrMode adm){
711 machine->regP = setBit(machine->regP, OVERFLOW_FL, 0);
714 static void jmpCLD(machine_6502 *machine, AddrMode adm){
715 machine->regP = setBit(machine->regP, DECIMAL_FL, 0);
718 static void jmpSED(machine_6502 *machine, AddrMode adm){
719 machine->regP = setBit(machine->regP, DECIMAL_FL, 1);
722 static void jmpINC(machine_6502 *machine, AddrMode adm){
724 BOOL isValue = getValue(machine, adm, &ptr);
726 ptr.value = (ptr.value + 1) & 0xFF;
727 memStoreByte(machine, ptr.addr, ptr.value);
728 manZeroNeg(machine,ptr.value);
731 static void jmpJMP(machine_6502 *machine, AddrMode adm){
733 BOOL isValue = getValue(machine, adm, &ptr);
735 machine->regPC = ptr.addr;
738 static void jmpJSR(machine_6502 *machine, AddrMode adm){
740 /* Move past the 2 byte parameter. JSR is always followed by
742 Bit16 currAddr = machine->regPC + 2;
743 BOOL isValue = getValue(machine, adm, &ptr);
745 stackPush(machine, (currAddr >> 8) & 0xff);
746 stackPush(machine, currAddr & 0xff);
747 machine->regPC = ptr.addr;
750 static void jmpLDA(machine_6502 *machine, AddrMode adm){
752 BOOL isValue = getValue(machine, adm, &ptr);
754 machine->regA = ptr.value;
755 manZeroNeg(machine, machine->regA);
758 static void jmpLDX(machine_6502 *machine, AddrMode adm){
760 BOOL isValue = getValue(machine, adm, &ptr);
762 machine->regX = ptr.value;
763 manZeroNeg(machine, machine->regX);
766 static void jmpLDY(machine_6502 *machine, AddrMode adm){
768 BOOL isValue = getValue(machine, adm, &ptr);
770 machine->regY = ptr.value;
771 manZeroNeg(machine, machine->regY);
774 static void jmpLSR(machine_6502 *machine, AddrMode adm){
776 BOOL isValue = getValue(machine, adm, &ptr);
779 setBit(machine->regP, CARRY_FL,
780 bitOn(ptr.value, CARRY_FL));
781 ptr.value = ptr.value >> 1;
782 ptr.value = setBit(ptr.value,NEGATIVE_FL,0);
783 memStoreByte(machine,ptr.addr,ptr.value);
784 manZeroNeg(machine,ptr.value);
786 else { /* Accumulator */
788 setBit(machine->regP, CARRY_FL,
789 bitOn(machine->regA, CARRY_FL));
790 machine->regA = machine->regA >> 1;
791 machine->regA = setBit(machine->regA,NEGATIVE_FL,0);
792 manZeroNeg(machine,ptr.value);
796 static void jmpNOP(machine_6502 *machine, AddrMode adm){
800 static void jmpORA(machine_6502 *machine, AddrMode adm){
802 BOOL isValue = getValue(machine, adm, &ptr);
804 machine->regA |= ptr.value;
805 manZeroNeg(machine,machine->regA);
808 static void jmpTAX(machine_6502 *machine, AddrMode adm){
809 machine->regX = machine->regA;
810 manZeroNeg(machine,machine->regX);
813 static void jmpTXA(machine_6502 *machine, AddrMode adm){
814 machine->regA = machine->regX;
815 manZeroNeg(machine,machine->regA);
818 static void jmpDEX(machine_6502 *machine, AddrMode adm){
819 if (machine->regX > 0)
822 machine->regX = 0xFF;
823 manZeroNeg(machine, machine->regX);
826 static void jmpINX(machine_6502 *machine, AddrMode adm){
827 Bit16 value = machine->regX + 1;
828 machine->regX = value & 0xFF;
829 manZeroNeg(machine, machine->regX);
832 static void jmpTAY(machine_6502 *machine, AddrMode adm){
833 machine->regY = machine->regA;
834 manZeroNeg(machine, machine->regY);
837 static void jmpTYA(machine_6502 *machine, AddrMode adm){
838 machine->regA = machine->regY;
839 manZeroNeg(machine, machine->regA);
842 static void jmpDEY(machine_6502 *machine, AddrMode adm){
843 if (machine->regY > 0)
846 machine->regY = 0xFF;
847 manZeroNeg(machine, machine->regY);
850 static void jmpINY(machine_6502 *machine, AddrMode adm){
851 Bit16 value = machine->regY + 1;
852 machine->regY = value & 0xff;
853 manZeroNeg(machine, machine->regY);
856 static void jmpROR(machine_6502 *machine, AddrMode adm){
859 BOOL isValue = getValue(machine, adm, &ptr);
861 cf = bitOn(machine->regP, CARRY_FL);
863 setBit(machine->regP, CARRY_FL,
864 bitOn(ptr.value, CARRY_FL));
865 ptr.value = ptr.value >> 1;
866 ptr.value = setBit(ptr.value, NEGATIVE_FL, cf);
867 memStoreByte(machine, ptr.addr, ptr.value);
868 manZeroNeg(machine, ptr.value);
871 cf = bitOn(machine->regP, CARRY_FL);
873 setBit(machine->regP, CARRY_FL,
874 bitOn(machine->regA, CARRY_FL));
875 machine->regA = machine->regA >> 1;
876 machine->regA = setBit(machine->regA, NEGATIVE_FL, cf);
877 manZeroNeg(machine, machine->regA);
881 static void jmpROL(machine_6502 *machine, AddrMode adm){
884 BOOL isValue = getValue(machine, adm, &ptr);
886 cf = bitOn(machine->regP, CARRY_FL);
888 setBit(machine->regP, CARRY_FL,
889 bitOn(ptr.value, NEGATIVE_FL));
890 ptr.value = ptr.value << 1;
891 ptr.value = setBit(ptr.value, CARRY_FL, cf);
892 memStoreByte(machine, ptr.addr, ptr.value);
893 manZeroNeg(machine, ptr.value);
896 cf = bitOn(machine->regP, CARRY_FL);
898 setBit(machine->regP, CARRY_FL,
899 bitOn(machine->regA,NEGATIVE_FL));
900 machine->regA = machine->regA << 1;
901 machine->regA = setBit(machine->regA, CARRY_FL, cf);
902 manZeroNeg(machine, machine->regA);
906 static void jmpRTI(machine_6502 *machine, AddrMode adm){
907 machine->regP = stackPop(machine);
908 machine->regPC = stackPop(machine);
911 static void jmpRTS(machine_6502 *machine, AddrMode adm){
913 BOOL isValue = getValue(machine, adm, &ptr);
914 Bit16 nr = stackPop(machine);
915 Bit16 nl = stackPop(machine);
916 warnValue(! isValue);
917 machine->regPC = (nl << 8) | nr;
920 static void jmpSBC(machine_6502 *machine, AddrMode adm){
923 Bit8 c = bitOn(machine->regP, CARRY_FL);
925 BOOL isValue = getValue(machine, adm, &ptr);
927 vflag = (bitOn(machine->regA,NEGATIVE_FL) &&
928 bitOn(ptr.value, NEGATIVE_FL));
930 if (bitOn(machine->regP, DECIMAL_FL)) {
931 Bit8 ar = nibble(machine->regA, RIGHT);
932 Bit8 br = nibble(ptr.value, RIGHT);
933 Bit8 al = nibble(machine->regA, LEFT);
934 Bit8 bl = nibble(ptr.value, LEFT);
936 tmp = 0xf + ar - br + c;
947 machine->regP = setBit(machine->regP, CARRY_FL, 0);
948 if (bitOn(machine->regP, OVERFLOW_FL) && w < 0x80)
949 machine->regP = setBit(machine->regP, OVERFLOW_FL, 0);
953 machine->regP = setBit(machine->regP, CARRY_FL, 1);
954 if (bitOn(machine->regP, OVERFLOW_FL) && w >= 0x180)
955 machine->regP = setBit(machine->regP, OVERFLOW_FL, 0);
958 } /* end decimal mode */
960 w = 0xff + machine->regA - ptr.value + c;
962 machine->regP = setBit(machine->regP, CARRY_FL, 0);
963 if (bitOn(machine->regP, OVERFLOW_FL) && w < 0x80)
964 machine->regP = setBit(machine->regP, OVERFLOW_FL, 0);
967 machine->regP = setBit(machine->regP, CARRY_FL, 1);
968 if (bitOn(machine->regP, OVERFLOW_FL) && w >= 0x180)
969 machine->regP = setBit(machine->regP, OVERFLOW_FL, 0);
973 manZeroNeg(machine,machine->regA);
976 static void jmpSTA(machine_6502 *machine, AddrMode adm){
978 BOOL isValue = getValue(machine, adm, &ptr);
980 memStoreByte(machine,ptr.addr,machine->regA);
983 static void jmpTXS(machine_6502 *machine, AddrMode adm){
984 stackPush(machine,machine->regX);
987 static void jmpTSX(machine_6502 *machine, AddrMode adm){
988 machine->regX = stackPop(machine);
989 manZeroNeg(machine, machine->regX);
992 static void jmpPHA(machine_6502 *machine, AddrMode adm){
993 stackPush(machine, machine->regA);
996 static void jmpPLA(machine_6502 *machine, AddrMode adm){
997 machine->regA = stackPop(machine);
998 manZeroNeg(machine, machine->regA);
1001 static void jmpPHP(machine_6502 *machine, AddrMode adm){
1002 stackPush(machine,machine->regP);
1005 static void jmpPLP(machine_6502 *machine, AddrMode adm){
1006 machine->regP = stackPop(machine);
1007 machine->regP = setBit(machine->regP, FUTURE_FL, 1);
1010 static void jmpSTX(machine_6502 *machine, AddrMode adm){
1012 BOOL isValue = getValue(machine, adm, &ptr);
1014 memStoreByte(machine,ptr.addr,machine->regX);
1017 static void jmpSTY(machine_6502 *machine, AddrMode adm){
1019 BOOL isValue = getValue(machine, adm, &ptr);
1021 memStoreByte(machine,ptr.addr,machine->regY);
1027 static void assignOpCodes(Opcodes *opcodes){
1029 #define SETOP(num, _name, _Imm, _ZP, _ZPX, _ZPY, _ABS, _ABSX, _ABSY, _INDX, _INDY, _SNGL, _BRA, _func) \
1030 {opcodes[num].name[4] = '\0'; \
1031 strncpy(opcodes[num].name, _name, 3); opcodes[num].Imm = _Imm; opcodes[num].ZP = _ZP; \
1032 opcodes[num].ZPX = _ZPX; opcodes[num].ZPY = _ZPY; opcodes[num].ABS = _ABS; \
1033 opcodes[num].ABSX = _ABSX; opcodes[num].ABSY = _ABSY; opcodes[num].INDX = _INDX; \
1034 opcodes[num].INDY = _INDY; opcodes[num].SNGL = _SNGL; opcodes[num].BRA = _BRA; \
1035 opcodes[num].func = _func;}
1037 /* OPCODE Imm ZP ZPX ZPY ABS ABSX ABSY INDX INDY SGNL BRA Jump Function*/
1038 SETOP( 0, "ADC", 0x69, 0x65, 0x75, 0x00, 0x6d, 0x7d, 0x79, 0x61, 0x71, 0x00, 0x00, jmpADC);
1039 SETOP( 1, "AND", 0x29, 0x25, 0x35, 0x31, 0x2d, 0x3d, 0x39, 0x00, 0x00, 0x00, 0x00, jmpAND);
1040 SETOP( 2, "ASL", 0x00, 0x06, 0x16, 0x00, 0x0e, 0x1e, 0x00, 0x00, 0x00, 0x0a, 0x00, jmpASL);
1041 SETOP( 3, "BIT", 0x00, 0x24, 0x00, 0x00, 0x2c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, jmpBIT);
1042 SETOP( 4, "BPL", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, jmpBPL);
1043 SETOP( 5, "BMI", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, jmpBMI);
1044 SETOP( 6, "BVC", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x50, jmpBVC);
1045 SETOP( 7, "BVS", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, jmpBVS);
1046 SETOP( 8, "BCC", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x90, jmpBCC);
1047 SETOP( 9, "BCS", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xb0, jmpBCS);
1048 SETOP(10, "BNE", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xd0, jmpBNE);
1049 SETOP(11, "BEQ", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, jmpBEQ);
1050 SETOP(12, "CMP", 0xc9, 0xc5, 0xd5, 0x00, 0xcd, 0xdd, 0xd9, 0xc1, 0xd1, 0x00, 0x00, jmpCMP);
1051 SETOP(13, "CPX", 0xe0, 0xe4, 0x00, 0x00, 0xec, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, jmpCPX);
1052 SETOP(14, "CPY", 0xc0, 0xc4, 0x00, 0x00, 0xcc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, jmpCPY);
1053 SETOP(15, "DEC", 0x00, 0xc6, 0xd6, 0x00, 0xce, 0xde, 0x00, 0x00, 0x00, 0x00, 0x00, jmpDEC);
1054 SETOP(16, "EOR", 0x49, 0x45, 0x55, 0x00, 0x4d, 0x5d, 0x59, 0x41, 0x51, 0x00, 0x00, jmpEOR);
1055 SETOP(17, "CLC", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x00, jmpCLC);
1056 SETOP(18, "SEC", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x38, 0x00, jmpSEC);
1057 SETOP(19, "CLI", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x58, 0x00, jmpCLI);
1058 SETOP(20, "SEI", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x78, 0x00, jmpSEI);
1059 SETOP(21, "CLV", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xb8, 0x00, jmpCLV);
1060 SETOP(22, "CLD", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xd8, 0x00, jmpCLD);
1061 SETOP(23, "SED", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf8, 0x00, jmpSED);
1062 SETOP(24, "INC", 0x00, 0xe6, 0xf6, 0x00, 0xee, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, jmpINC);
1063 SETOP(25, "JMP", 0x00, 0x00, 0x00, 0x00, 0x4c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, jmpJMP);
1064 SETOP(26, "JSR", 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, jmpJSR);
1065 SETOP(27, "LDA", 0xa9, 0xa5, 0xb5, 0x00, 0xad, 0xbd, 0xb9, 0xa1, 0xb1, 0x00, 0x00, jmpLDA);
1066 SETOP(28, "LDX", 0xa2, 0xa6, 0x00, 0xb6, 0xae, 0x00, 0xbe, 0x00, 0x00, 0x00, 0x00, jmpLDX);
1067 SETOP(29, "LDY", 0xa0, 0xa4, 0xb4, 0x00, 0xac, 0xbc, 0x00, 0x00, 0x00, 0x00, 0x00, jmpLDY);
1068 SETOP(30, "LSR", 0x00, 0x46, 0x56, 0x00, 0x4e, 0x5e, 0x00, 0x00, 0x00, 0x4a, 0x00, jmpLSR);
1069 SETOP(31, "NOP", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xea, 0x00, jmpNOP);
1070 SETOP(32, "ORA", 0x09, 0x05, 0x15, 0x00, 0x0d, 0x1d, 0x19, 0x01, 0x11, 0x00, 0x00, jmpORA);
1071 SETOP(33, "TAX", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xaa, 0x00, jmpTAX);
1072 SETOP(34, "TXA", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x8a, 0x00, jmpTXA);
1073 SETOP(35, "DEX", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xca, 0x00, jmpDEX);
1074 SETOP(36, "INX", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe8, 0x00, jmpINX);
1075 SETOP(37, "TAY", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xa8, 0x00, jmpTAY);
1076 SETOP(38, "TYA", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x98, 0x00, jmpTYA);
1077 SETOP(39, "DEY", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x88, 0x00, jmpDEY);
1078 SETOP(40, "INY", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc8, 0x00, jmpINY);
1079 SETOP(41, "ROR", 0x00, 0x66, 0x76, 0x00, 0x6e, 0x7e, 0x00, 0x00, 0x00, 0x6a, 0x00, jmpROR);
1080 SETOP(42, "ROL", 0x00, 0x26, 0x36, 0x00, 0x2e, 0x3e, 0x00, 0x00, 0x00, 0x2a, 0x00, jmpROL);
1081 SETOP(43, "RTI", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, jmpRTI);
1082 SETOP(44, "RTS", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x00, jmpRTS);
1083 SETOP(45, "SBC", 0xe9, 0xe5, 0xf5, 0x00, 0xed, 0xfd, 0xf9, 0xe1, 0xf1, 0x00, 0x00, jmpSBC);
1084 SETOP(46, "STA", 0x00, 0x85, 0x95, 0x00, 0x8d, 0x9d, 0x99, 0x81, 0x91, 0x00, 0x00, jmpSTA);
1085 SETOP(47, "TXS", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x9a, 0x00, jmpTXS);
1086 SETOP(48, "TSX", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xba, 0x00, jmpTSX);
1087 SETOP(49, "PHA", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x48, 0x00, jmpPHA);
1088 SETOP(50, "PLA", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x68, 0x00, jmpPLA);
1089 SETOP(51, "PHP", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, jmpPHP);
1090 SETOP(52, "PLP", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x28, 0x00, jmpPLP);
1091 SETOP(53, "STX", 0x00, 0x86, 0x00, 0x96, 0x8e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, jmpSTX);
1092 SETOP(54, "STY", 0x00, 0x84, 0x94, 0x00, 0x8c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, jmpSTY);
1093 SETOP(55, "---", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, NULL);
1096 static void buildIndexCache(machine_6502 *machine){
1098 for (i = 0; i < NUM_OPCODES; i++) {
1099 if (machine->opcodes[i].Imm != 0x00){
1100 machine->opcache[machine->opcodes[i].Imm].adm = IMMEDIATE_VALUE;
1101 machine->opcache[machine->opcodes[i].Imm].index = i;
1103 if (machine->opcodes[i].ZP != 0x00){
1104 machine->opcache[machine->opcodes[i].ZP].adm = ZERO;
1105 machine->opcache[machine->opcodes[i].ZP].index = i;
1107 if (machine->opcodes[i].ZPX != 0x00){
1108 machine->opcache[machine->opcodes[i].ZPX].adm = ZERO_X;
1109 machine->opcache[machine->opcodes[i].ZPX].index = i;;
1111 if (machine->opcodes[i].ZPY != 0x00){
1112 machine->opcache[machine->opcodes[i].ZPY].adm = ZERO_Y;
1113 machine->opcache[machine->opcodes[i].ZPY].index = i;;
1115 if (machine->opcodes[i].ABS != 0x00){
1116 machine->opcache[machine->opcodes[i].ABS].adm = ABS_VALUE;
1117 machine->opcache[machine->opcodes[i].ABS].index = i;;
1119 if (machine->opcodes[i].ABSX != 0x00){
1120 machine->opcache[machine->opcodes[i].ABSX].adm = ABS_X;
1121 machine->opcache[machine->opcodes[i].ABSX].index = i;;
1123 if (machine->opcodes[i].ABSY != 0x00){
1124 machine->opcache[machine->opcodes[i].ABSY].adm = ABS_Y;
1125 machine->opcache[machine->opcodes[i].ABSY].index = i;;
1127 if (machine->opcodes[i].INDX != 0x00){
1128 machine->opcache[machine->opcodes[i].INDX].adm = INDIRECT_X;
1129 machine->opcache[machine->opcodes[i].INDX].index = i;;
1131 if (machine->opcodes[i].INDY != 0x00){
1132 machine->opcache[machine->opcodes[i].INDY].adm = INDIRECT_Y;
1133 machine->opcache[machine->opcodes[i].INDY].index = i;;
1135 if (machine->opcodes[i].SNGL != 0x00){
1136 machine->opcache[machine->opcodes[i].SNGL].adm = SINGLE;
1137 machine->opcache[machine->opcodes[i].SNGL].index = i;
1139 if (machine->opcodes[i].BRA != 0x00){
1140 machine->opcache[machine->opcodes[i].BRA].adm = ABS_OR_BRANCH;
1141 machine->opcache[machine->opcodes[i].BRA].index = i;
1146 /* opIndex() - Search the opcode table for a match. If found return
1147 the index into the optable and the address mode of the opcode. If
1148 the opcode is not found then return -1. */
1149 static int opIndex(machine_6502 *machine, Bit8 opcode, AddrMode *adm){
1150 /* XXX could catch errors by setting a addressmode of error or something */
1151 *adm = machine->opcache[opcode].adm;
1152 return machine->opcache[opcode].index;
1156 /* Assembly parser */
1158 static Param *newParam(void){
1162 newp = (Param *) emalloc(sizeof(Param));
1163 newp->type = SINGLE;
1164 for (i = 0; i < MAX_PARAM_VALUE; i++)
1167 newp->label = ecalloc(MAX_LABEL_LEN,sizeof(char));
1172 /* Copy the fields from p2 to p1 */
1173 static void copyParam(Param *p1, Param *p2){
1175 strncpy(p1->label,p2->label,MAX_LABEL_LEN);
1176 for(i = 0; i < MAX_PARAM_VALUE; i++)
1177 p1->value[i] = p2->value[i];
1179 p1->type = p2->type;
1182 static Label *newLabel(void){
1185 newp = (Label *) emalloc(sizeof(Label));
1187 newp->label = ecalloc(MAX_LABEL_LEN,sizeof(char));
1192 static AsmLine *newAsmLine(char *cmd, char *label, BOOL decl, Param *param, int lc)
1196 newp = (AsmLine *) emalloc(sizeof(AsmLine));
1197 newp->labelDecl = decl;
1198 newp->label = newLabel();
1199 strncpy(newp->label->label,label,MAX_LABEL_LEN);
1200 newp->command = estrdup(cmd);
1201 newp->param = newParam();
1202 copyParam(newp->param, param);
1207 static AsmLine *addend(AsmLine *listp, AsmLine *newp)
1212 for (p =listp; p->next != NULL; p = p->next)
1218 static BOOL apply(AsmLine *listp, BOOL(*fn)(AsmLine*, void*), void *arg)
1223 for (p = listp; p != NULL; p = p->next)
1229 static void freeParam(Param *param){
1234 static void freeLabel(Label *label){
1239 static void freeallAsmLine(AsmLine *listp)
1242 for(; listp != NULL; listp = next){
1244 freeParam(listp->param);
1245 freeLabel(listp->label);
1246 free(listp->command);
1251 static BOOL addvalue(Param *param,Bit32 value){
1252 if (0 <= param->vp && param->vp < MAX_PARAM_VALUE) {
1253 param->value[param->vp++] = value;
1257 fprintf(stderr,"Wrong number of parameters: %d. The limit is %d\n",param->vp+1, MAX_PARAM_VALUE);
1262 static void parseError(char *s){
1263 fprintf(stderr,"6502 Syntax Error: %s\n", s);
1266 /* stoupper() - Destructivley modifies the string making all letters upper case*/
1267 static void stoupper(char **s){
1269 while((*s)[i] != '\0'){
1270 (*s)[i] = toupper((*s)[i]);
1275 static BOOL isWhite(char c){
1276 return (c == '\r' || c == '\t' || c == ' ');
1279 static void skipSpace(char **s){
1280 for(; isWhite(**s); (*s)++)
1284 /* nullify() - fills a string with upto sourceLength null characters. */
1285 static void nullify(char *token, unsigned int sourceLength){
1287 while (i < sourceLength)
1291 static BOOL isBlank(const char *token){
1292 return (token[0] == '\0');
1295 static BOOL isCommand(machine_6502 *machine, const char *token){
1298 while (i < NUM_OPCODES) {
1299 if (strcmp(machine->opcodes[i].name,token) == 0)
1304 if (strcmp(token, "DCB") == 0) return TRUE;
1308 /* hasChar() - Check to see if the current line has a certain
1310 static BOOL hasChar(char *s, char c){
1311 for(; *s != '\0' && *s != '\n'; s++) {
1318 static BOOL ishexdigit(char c){
1322 char c1 = toupper(c);
1323 return ('A' <= c1 && c1 <= 'F');
1327 /* isCmdChar() - Is this a valid character for a command. All of the
1328 command are alpha except for the entry point code that is "*=" */
1329 static BOOL isCmdChar(char c){
1330 return (isalpha(c) || c == '*' || c == '=');
1334 /* command() - parse a command from the source code. We pass along a
1335 machine so the opcode can be validated. */
1336 static BOOL command(machine_6502 *machine, char **s, char **cmd){
1339 for(;isCmdChar(**s) && i < MAX_CMD_LEN; (*s)++)
1342 return TRUE; /* Could be a blank line. */
1343 else if (strcmp(*cmd,"*=") == 0)
1344 return TRUE; /* This is an entry point. */
1346 return isCommand(machine,*cmd);
1349 static BOOL declareLabel(char **s, char **label){
1352 for(;**s != ':' && **s != '\n' && **s != '\0'; (*s)++){
1355 (*label)[i++] = **s;
1358 return FALSE; /* Current line has to have a label */
1359 else if (**s == ':'){
1360 (*s)++; /* Skip colon */
1367 static BOOL parseHex(char **s, Bit32 *value){
1368 enum { MAX_HEX_LEN = 5 };
1370 char *hex = ecalloc(MAX_HEX_LEN, sizeof(char));
1373 (*s)++; /* move pass $ */
1374 for(; ishexdigit(**s) && i < MAX_HEX_LEN; (*s)++)
1377 *value = strtol(hex,NULL,16);
1385 static BOOL parseDec(char **s, Bit32 *value){
1386 enum { MAX_DEC_LEN = 4 };
1387 char *dec = ecalloc(MAX_DEC_LEN, sizeof(char));
1389 for(i = 0; isdigit(**s) && i < MAX_DEC_LEN; (*s)++)
1401 static BOOL parseValue(char **s, Bit32 *value){
1404 return parseHex(s, value);
1406 return parseDec(s, value);
1409 static BOOL paramLabel(char **s, char **label){
1411 for(i = 0; (isalnum(**s) || **s == '_') && i < MAX_LABEL_LEN; (*s)++)
1412 (*label)[i++] = **s;
1420 static BOOL immediate(char **s, Param *param){
1424 (*s)++; /*Move past hash */
1425 if (**s == '<' || **s == '>'){
1426 char *label = ecalloc(MAX_LABEL_LEN, sizeof(char));
1427 param->type = (**s == '<') ? IMMEDIATE_LESS : IMMEDIATE_GREAT;
1428 (*s)++; /* move past < or > */
1429 if (paramLabel(s, &label)){
1430 int ln = strlen(label) + 1;
1431 strncpy(param->label, label, ln);
1439 if (parseValue(s, &value)){
1441 parseError("Immediate value is too large.");
1444 param->type = IMMEDIATE_VALUE;
1445 return addvalue(param, value);
1451 static BOOL isDirection(char c){
1452 return (c == 'X' || c == 'Y');
1455 static BOOL getDirection(char **s, char *direction){
1460 if (isDirection(**s)){
1469 static BOOL indirect(char **s, Param *param){
1477 if (! parseHex(s,&value))
1480 parseError("Indirect value is too large.");
1483 if (!addvalue(param, value))
1488 if (getDirection(s,&c)) {
1490 param->type = INDIRECT_Y;
1495 else if (getDirection(s, &c)){
1500 param->type = INDIRECT_X;
1508 static BOOL dcbValue(char **s, Param *param){
1510 if (! parseValue(s,&val))
1516 if (!addvalue(param,val))
1519 param->type = DCB_PARAM;
1524 return dcbValue(s, param);
1530 static BOOL value(char **s, Param *param){
1535 if (! parseValue(s,&val))
1539 dir = getDirection(s,&c);
1540 if (!addvalue(param,val))
1545 param->type = ABS_X;
1547 param->type = ABS_Y;
1552 param->type = ABS_VALUE;
1555 param->type = ZERO_X;
1557 param->type = ZERO_Y;
1567 static BOOL label(char **s, Param *param){
1568 char *label = ecalloc(MAX_LABEL_LEN, sizeof(char));
1570 BOOL labelOk = FALSE;
1571 if (paramLabel(s, &label)){
1573 param->type = ABS_OR_BRANCH;
1574 if (getDirection(s, &c)){
1576 param->type = ABS_LABEL_X;
1578 param->type = ABS_LABEL_Y;
1582 strncpy(param->label,label,MAX_LABEL_LEN);
1588 static BOOL parameter(const char *cmd, char **s, Param *param){
1590 if (**s == '\0' || **s == '\n')
1592 else if (**s == '#')
1593 return immediate(s,param);
1594 else if (**s == '(')
1595 return indirect(s,param);
1596 else if (**s == '$' || isdigit(**s)){
1597 if (strcmp(cmd, "DCB") == 0)
1598 return dcbValue(s,param);
1600 return value(s,param);
1602 else if (isalpha(**s))
1603 return label(s ,param);
1605 return FALSE; /* Invalid Parameter */
1608 static void comment(char **s){
1611 for(;**s != '\n' && **s != '\0'; (*s)++)
1615 static void initParam(Param *param){
1617 param->type = SINGLE;
1618 for(i = 0; i < MAX_PARAM_VALUE; i++)
1619 param->value[i] = 0;
1621 nullify(param->label,MAX_LABEL_LEN);
1625 static AsmLine *parseAssembly(machine_6502 *machine, BOOL *codeOk, const char *code){
1627 char *cmd = ecalloc(MAX_CMD_LEN, sizeof(char));
1628 char *label = ecalloc(MAX_LABEL_LEN, sizeof(char));
1629 char *start; /*pointer to the start of the code.*/
1630 unsigned int lc = 1;
1633 AsmLine *listp = NULL;
1641 while(*s != '\0' && *codeOk){
1643 nullify(cmd, MAX_CMD_LEN);
1644 nullify(label, MAX_LABEL_LEN);
1651 continue; /* blank line */
1653 else if (*s == '\0')
1654 continue; /* no newline at the end of the code */
1655 else if (hasChar(s,':')){
1657 if(! declareLabel(&s,&label)){
1663 if(!command(machine, &s, &cmd)){
1669 if(!parameter(cmd, &s, param)){
1675 if (*s == '\n' || *s == '\0'){
1677 asmm = newAsmLine(cmd,label,decl,param,lc);
1678 listp = addend(listp,asmm);
1686 fprintf(stderr,"Syntax error at line %u\n", lc);
1694 /* fileToBuffer() - Allocates a buffer and loads all of the file into memory. */
1695 static char *fileToBuffer(const char *filename){
1696 const int defaultSize = 1024;
1699 int size = defaultSize;
1701 char *buffer = ecalloc(defaultSize,sizeof(char));
1704 eprintf("Could not allocate memory for buffer.");
1706 ifp = fopen(filename, "rb");
1708 eprintf("Could not open file.");
1710 while((c = getc(ifp)) != EOF){
1713 size += defaultSize;
1714 buffer = realloc(buffer, size);
1715 if (buffer == NULL) {
1717 eprintf("Could not resize buffer.");
1722 buffer = realloc(buffer, i+2);
1724 eprintf("Could not resize buffer.");
1725 /* Make sure we have a line feed at the end */
1734 /* reset() - Reset CPU and memory. */
1735 static void reset(machine_6502 *machine){
1737 for ( y = 0; y < 32; y++ ){
1738 for (x = 0; x < 32; x++){
1739 machine->screen[x][y] = 0;
1743 for(x=0; x < MEM_64K; x++)
1744 machine->memory[x] = 0;
1746 machine->codeCompiledOK = FALSE;
1750 machine->regP = setBit(machine->regP, FUTURE_FL, 1);
1751 machine->defaultCodePC = machine->regPC = PROG_START;
1752 machine->regSP = STACK_TOP;
1753 machine->runForever = FALSE;
1754 machine->labelPtr = 0;
1755 machine->codeRunning = FALSE;
1758 /* hexDump() - Dump the memory to output */
1759 void hexDump(machine_6502 *machine, Bit16 start, Bit16 numbytes, FILE *output){
1762 for( i = 0; i < numbytes; i++){
1763 address = start + i;
1764 if ( (i&15) == 0 ) {
1765 fprintf(output,"\n%.4x: ", address);
1767 fprintf(output,"%.2x%s",machine->memory[address], (i & 1) ? " ":"");
1769 fprintf(output,"%s\n",(i&1)?"--":"");
1773 /* void save_program(machine_6502 *machine, char *filename){ */
1775 /* Bit16 pc = PROG_START; */
1776 /* Bit16 end = pc + machine->codeLen; */
1778 /* ofp = fopen(filename, "w"); */
1779 /* if (ofp == NULL) */
1780 /* eprintf("Could not open file."); */
1782 /* fprintf(ofp,"Bit8 prog[%d] =\n{",machine->codeLen); */
1784 /* while(pc < end) */
1785 /* fprintf(ofp,"0x%.2x,%s",machine->memory[pc++],n++%10?" ":"\n"); */
1786 /* fseek(ofp,-2,SEEK_CUR); */
1787 /* fprintf(ofp,"};\n"); */
1792 static BOOL translate(Opcodes *op,Param *param, machine_6502 *machine){
1793 switch(param->type){
1796 pushByte(machine, op->SNGL);
1798 fprintf(stderr,"%s needs a parameter.\n",op->name);
1802 case IMMEDIATE_VALUE:
1804 pushByte(machine, op->Imm);
1805 pushByte(machine, param->value[0]);
1809 fprintf(stderr,"%s does not take IMMEDIATE_VALUE parameters.\n",op->name);
1812 case IMMEDIATE_GREAT:
1814 pushByte(machine, op->Imm);
1815 pushByte(machine, param->lbladdr >> 8);
1819 fprintf(stderr,"%s does not take IMMEDIATE_GREAT parameters.\n",op->name);
1822 case IMMEDIATE_LESS:
1824 pushByte(machine, op->Imm);
1825 pushByte(machine, param->lbladdr & 0xFF);
1829 fprintf(stderr,"%s does not take IMMEDIATE_LESS parameters.\n",op->name);
1834 pushByte(machine, op->INDX);
1835 pushByte(machine, param->value[0]);
1839 fprintf(stderr,"%s does not take INDIRECT_X parameters.\n",op->name);
1844 pushByte(machine, op->INDY);
1845 pushByte(machine, param->value[0]);
1849 fprintf(stderr,"%s does not take INDIRECT_Y parameters.\n",op->name);
1854 pushByte(machine, op->ZP);
1855 pushByte(machine, param->value[0]);
1859 fprintf(stderr,"%s does not take ZERO parameters.\n",op->name);
1864 pushByte(machine, op->ZPX);
1865 pushByte(machine, param->value[0]);
1869 fprintf(stderr,"%s does not take ZERO_X parameters.\n",op->name);
1874 pushByte(machine, op->ZPY);
1875 pushByte(machine, param->value[0]);
1879 fprintf(stderr,"%s does not take ZERO_Y parameters.\n",op->name);
1884 pushByte(machine, op->ABS);
1885 pushWord(machine, param->value[0]);
1889 fprintf(stderr,"%s does not take ABS_VALUE parameters.\n",op->name);
1894 pushByte(machine, op->ABS);
1895 pushWord(machine, param->lbladdr);
1899 pushByte(machine, op->BRA);
1900 if (param->lbladdr < (machine->codeLen + PROG_START))
1902 (0xff - (machine->codeLen-param->lbladdr)) & 0xff);
1905 (param->lbladdr - machine->codeLen-1) & 0xff);
1908 fprintf(stderr,"%s does not take BRANCH parameters.\n",op->name);
1915 pushByte(machine, op->ABSX);
1916 pushWord(machine, param->value[0]);
1920 fprintf(stderr,"%s does not take ABS_X parameters.\n",op->name);
1925 pushByte(machine, op->ABSY);
1926 pushWord(machine, param->value[0]);
1930 fprintf(stderr,"%s does not take ABS_Y parameters.\n",op->name);
1935 pushByte(machine, op->ABSX);
1936 pushWord(machine, param->lbladdr);
1940 fprintf(stderr,"%s does not take ABS_LABEL_X parameters.\n",op->name);
1945 pushByte(machine, op->ABSY);
1946 pushWord(machine, param->lbladdr);
1950 fprintf(stderr,"%s does not take ABS_LABEL_Y parameters.\n",op->name);
1954 /* Handled elsewhere */
1960 /* compileLine() - Compile one line of code. Returns
1961 TRUE if it compile successfully. */
1962 static BOOL compileLine(AsmLine *asmline, void *args){
1963 machine_6502 *machine;
1965 if (isBlank(asmline->command)) return TRUE;
1966 if (strcmp("*=",asmline->command) == 0){
1967 machine->defaultCodePC = asmline->param->value[0];
1969 else if (strcmp("DCB",asmline->command) == 0){
1971 for(i = 0; i < asmline->param->vp; i++)
1972 pushByte(machine, asmline->param->value[i]);
1976 char *command = asmline->command;
1978 for(i = 0; i < NUM_OPCODES; i++){
1979 if (strcmp(machine->opcodes[i].name, command) == 0){
1980 op = machine->opcodes[i];
1984 if (i == NUM_OPCODES)
1985 return FALSE; /* unknow upcode */
1987 return translate(&op,asmline->param,machine);
1992 /* indexLabels() - Get the address for each label */
1993 static BOOL indexLabels(AsmLine *asmline, void *arg){
1994 machine_6502 *machine;
1997 thisPC = machine->regPC;
1998 /* Figure out how many bytes this instruction takes */
1999 machine->codeLen = 0;
2000 if ( ! compileLine(asmline, machine) ){
2003 machine->regPC += machine->codeLen;
2004 if (asmline->labelDecl) {
2005 asmline->label->addr = thisPC;
2010 static BOOL changeParamLabelAddr(AsmLine *asmline, void *label){
2012 if (strcmp(asmline->param->label, la->label) == 0)
2013 asmline->param->lbladdr = la->addr;
2017 static BOOL linkit(AsmLine *asmline, void *asmlist){
2018 apply(asmlist,changeParamLabelAddr,asmline->label);
2022 /* linkLabels - Make sure all of the references to the labels contain
2024 static void linkLabels(AsmLine *asmlist){
2025 apply(asmlist,linkit,asmlist);
2028 /* compileCode() - Compile the current assembly code for the machine */
2029 static BOOL compileCode(machine_6502 *machine, const char *code){
2034 machine->defaultCodePC = machine->regPC = PROG_START;
2035 asmlist = parseAssembly(machine, &codeOk, code);
2038 /* First pass: Find the addresses for the labels */
2039 if (!apply(asmlist, indexLabels, machine))
2041 /* update label references */
2042 linkLabels(asmlist);
2043 /* Second pass: translate the instructions */
2044 machine->codeLen = 0;
2045 /* Link label call push_byte which increments defaultCodePC.
2046 We need to reset it so the compiled code goes in the
2048 machine->defaultCodePC = PROG_START;
2049 if (!apply(asmlist, compileLine, machine))
2052 if (machine->defaultCodePC > PROG_START ){
2053 machine->memory[machine->defaultCodePC] = 0x00;
2057 fprintf(stderr,"No Code to run.\n");
2062 fprintf(stderr,"An error occured while parsing the file.\n");
2065 freeallAsmLine(asmlist);
2071 * execute() - Executes one instruction.
2072 * This is the main part of the CPU emulator.
2076 static void execute(machine_6502 *machine){
2081 if(!machine->codeRunning) return;
2083 opcode = popByte(machine);
2085 machine->codeRunning = FALSE;
2087 opidx = opIndex(machine,opcode,&adm);
2089 machine->opcodes[opidx].func(machine, adm);
2091 fprintf(stderr,"Invalid opcode!\n");
2093 if( (machine->regPC == 0) ||
2094 (!machine->codeRunning) ) {
2095 machine->codeRunning = FALSE;
2099 machine_6502 *build6502(){
2100 machine_6502 *machine;
2101 machine = emalloc(sizeof(machine_6502));
2102 assignOpCodes(machine->opcodes);
2103 buildIndexCache(machine);
2108 void destroy6502(machine_6502 *machine){
2113 void trace(machine_6502 *machine, FILE *output){
2114 Bit8 opcode = memReadByte(machine,machine->regPC);
2117 int opidx = opIndex(machine,opcode,&adm);
2118 int stacksz = STACK_TOP - machine->regSP;
2120 fprintf(output,"\n NVFBDIZC\nP: %d%d%d%d%d%d%d%d ",
2121 bitOn(machine->regP,NEGATIVE_FL),
2122 bitOn(machine->regP,OVERFLOW_FL),
2123 bitOn(machine->regP,FUTURE_FL),
2124 bitOn(machine->regP,BREAK_FL),
2125 bitOn(machine->regP,DECIMAL_FL),
2126 bitOn(machine->regP,INTERRUPT_FL),
2127 bitOn(machine->regP,ZERO_FL),
2128 bitOn(machine->regP,CARRY_FL));
2129 fprintf(output,"A: %.2x X: %.2x Y: %.2x SP: %.4x PC: %.4x\n",
2130 machine->regA, machine->regX, machine->regY, machine->regSP, machine->regPC);
2132 Bit16 pc = machine->regPC;
2133 fprintf(output,"\n%.4x:\t%s",machine->regPC, machine->opcodes[opidx].name);
2134 if (peekValue(machine, adm, &ptr, pc+1))
2135 fprintf(output,"\tAddress:%.4x\tValue:%.4x\n",
2136 ptr.addr,ptr.value);
2138 fprintf(output,"\n");
2140 fprintf(output,"STACK:");
2141 hexDump(machine,(STACK_TOP - stacksz) + 1, stacksz, output);
2144 void disassemble(machine_6502 *machine, FILE *output){
2146 increment the program counter
2148 loop until end of program. */
2155 Bit16 opc = machine->regPC;
2156 mem = calloc(20,sizeof(char));
2157 machine->regPC = PROG_START;
2159 addr = machine->regPC;
2160 opcode = popByte(machine);
2161 opidx = opIndex(machine,opcode,&adm);
2162 for (i = 0; i < 20; i++) mem[i] = '\0';
2163 dismem(machine, adm, mem);
2164 fprintf(output,"%x\t%s\t%s\n",
2165 addr,machine->opcodes[opidx].name,mem);
2166 }while((machine->regPC - PROG_START) < machine->codeLen); /*XXX - may need to change since defaultCodePC */
2168 machine->regPC = opc;
2172 void eval_file(machine_6502 *machine, const char *filename, Plotter plot, void *plotterState){
2175 machine->plot = plot;
2176 machine->plotterState = plotterState;
2178 code = fileToBuffer(filename);
2180 if (! compileCode(machine, code) ){
2181 eprintf("Could not compile code.\n");
2186 machine->defaultCodePC = machine->regPC = PROG_START;
2187 machine->codeRunning = TRUE;
2194 }while(machine->codeRunning);
2197 void start_eval_file(machine_6502 *machine, const char *filename, Plotter plot, void *plotterState){
2201 machine->plot = plot;
2202 machine->plotterState = plotterState;
2204 code = fileToBuffer(filename);
2206 if (! compileCode(machine, code) ){
2207 eprintf("Could not compile code.\n");
2212 machine->defaultCodePC = machine->regPC = PROG_START;
2213 machine->codeRunning = TRUE;
2217 void start_eval_string(machine_6502 *machine, const char *code,
2218 Plotter plot, void *plotterState){
2221 machine->plot = plot;
2222 machine->plotterState = plotterState;
2224 if (! compileCode(machine, code) ){
2225 fprintf(stderr,"Could not compile code.\n");
2228 machine->defaultCodePC = machine->regPC = PROG_START;
2229 machine->codeRunning = TRUE;
2233 /* void start_eval_binary(machine_6502 *machine, Bit8 *program, */
2234 /* unsigned int proglen, */
2235 /* Plotter plot, void *plotterState){ */
2236 /* unsigned int pc, n; */
2237 /* reset(machine); */
2238 /* machine->plot = plot; */
2239 /* machine->plotterState = plotterState; */
2241 /* machine->regPC = PROG_START; */
2242 /* pc = machine->regPC; */
2243 /* machine->codeLen = proglen; */
2245 /* while (n < proglen){ */
2246 /* machine->memory[pc++] = program[n++]; */
2248 /* machine->codeRunning = TRUE; */
2249 /* execute(machine); */
2252 void next_eval(machine_6502 *machine, int insno){
2254 for (i = 1; i < insno; i++){
2255 if (machine->codeRunning){
2257 trace(machine, stdout);