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 */
98 /*static void *emalloc(size_t n) {
104 static void *ecalloc(uint32_t nelm, size_t nsize){
105 void *p = calloc(nelm, nsize);
110 /* estrdup() - Allocates memory for a new string a returns a copy of the source sting in it. */
111 static char *estrdup(const char *source){
112 int ln = strlen(source) + 1;
113 char *s = ecalloc(ln, sizeof(char));
114 strncpy(s,source,ln);
118 static void checkAddress(Bit32 address){
119 /* XXX: Do we want to kill the program here? */
120 if (address >= MEM_64K)
121 fprintf(stderr, "Address %d is beyond 64k", address);
125 * stackPush() - Push byte to stack
129 static void stackPush(machine_6502 *machine, Bit8 value ) {
130 if(machine->regSP >= STACK_BOTTOM){
131 machine->memory[machine->regSP--] = value;
134 fprintf(stderr, "The stack is full: %.4x\n", machine->regSP);
135 machine->codeRunning = FALSE;
141 * stackPop() - Pop byte from stack
145 static Bit8 stackPop(machine_6502 *machine) {
146 if (machine->regSP < STACK_TOP){
147 Bit8 value =machine->memory[++machine->regSP];
151 /* fprintf(stderr, "The stack is empty.\n"); xxx */
152 machine->codeRunning = FALSE;
157 static void pushByte(machine_6502 *machine, Bit32 value ) {
158 Bit32 address = machine->defaultCodePC;
159 checkAddress(address);
160 machine->memory[address] = value & 0xff;
162 machine->defaultCodePC++;
166 * pushWord() - Push a word using pushByte twice
170 static void pushWord(machine_6502 *machine, Bit16 value ) {
171 pushByte(machine, value & 0xff );
172 pushByte(machine, (value>>8) & 0xff );
176 * popByte( machine_6502 *machine,) - Pops a byte
180 static Bit8 popByte( machine_6502 *machine) {
181 Bit8 value = machine->memory[machine->regPC];
187 * popWord() - Pops a word using popByte() twice
191 static int popWord(machine_6502 *machine) {
192 return popByte(machine) + (popByte(machine) << 8);
197 * memReadByte() - Peek a byte, don't touch any registers
201 static int memReadByte( machine_6502 *machine, int addr ) {
202 if( addr == 0xfe ) return floor( random()%255 );
203 return machine->memory[addr];
206 static void updateDisplayPixel(machine_6502 *machine, Bit16 addr){
207 Bit8 idx = memReadByte(machine,addr) & 0x0f;
213 machine->plot(x,y,idx,machine->plotterState);
218 * memStoreByte() - Poke a byte, don't touch any registers
222 static void memStoreByte( machine_6502 *machine, int addr, int value ) {
223 machine->memory[ addr ] = (value & 0xff);
224 if( (addr >= 0x200) && (addr<=0x5ff) )
225 updateDisplayPixel(machine, addr );
232 static Bit8 bitOn(Bit8 value,Flags bit){
235 return ((value & mask) > 0);
238 static Bit8 bitOff(Bit8 value, Flags bit){
239 return (! bitOn(value,bit));
242 static Bit8 setBit(Bit8 value, Flags bit, int on){
245 onMask = onMask << bit;
246 offMask = offMask ^ onMask;
247 return ((on) ? value | onMask : value & offMask);
250 static Bit8 nibble(Bit8 value, Side side){
252 case LEFT: return value & 0xf0;
253 case RIGHT: return value & 0xf;
255 fprintf(stderr,"nibble unknown side\n");
261 /* used for tracing. XXX: combined with function getvalue */
262 static BOOL peekValue(machine_6502 *machine, AddrMode adm, Pointer *pointer, Bit16 PC){
270 case IMMEDIATE_GREAT:
271 case IMMEDIATE_VALUE:
272 pointer->value = memReadByte(machine, PC);
275 zp = memReadByte(machine, PC) + machine->regX;
276 pointer->addr = memReadByte(machine,zp) +
277 (memReadByte(machine,zp+1)<<8);
278 pointer->value = memReadByte(machine, pointer->addr);
281 zp = memReadByte(machine, PC);
282 pointer->addr = memReadByte(machine,zp) +
283 (memReadByte(machine,zp+1)<<8) + machine->regY;
284 pointer->value = memReadByte(machine, pointer->addr);
287 pointer->addr = memReadByte(machine, PC);
288 pointer->value = memReadByte(machine, pointer->addr);
291 pointer->addr = memReadByte(machine, PC) + machine->regX;
292 pointer->value = memReadByte(machine, pointer->addr);
295 pointer->addr = memReadByte(machine, PC) + machine->regY;
296 pointer->value = memReadByte(machine, pointer->addr);
299 pointer->addr = memReadByte(machine, PC);
302 pointer->addr = memReadByte(machine, PC) + (memReadByte(machine, PC+1) << 8);
303 pointer->value = memReadByte(machine, pointer->addr);
307 pointer->addr = (memReadByte(machine, PC) +
308 (memReadByte(machine, PC+1) << 8)) + machine->regX;
309 pointer->value = memReadByte(machine, pointer->addr);
313 pointer->addr = (memReadByte(machine, PC) +
314 (memReadByte(machine, PC+1) << 8)) + machine->regY;
315 pointer->value = memReadByte(machine, pointer->addr);
318 /* Handled elsewhere */
326 /* Figure out how to get the value from the addrmode and get it.*/
327 static BOOL getValue(machine_6502 *machine, AddrMode adm, Pointer *pointer){
335 case IMMEDIATE_GREAT:
336 case IMMEDIATE_VALUE:
337 pointer->value = popByte(machine);
340 zp = popByte(machine) + machine->regX;
341 pointer->addr = memReadByte(machine,zp) +
342 (memReadByte(machine,zp+1)<<8);
343 pointer->value = memReadByte(machine, pointer->addr);
346 zp = popByte(machine);
347 pointer->addr = memReadByte(machine,zp) +
348 (memReadByte(machine,zp+1)<<8) + machine->regY;
349 pointer->value = memReadByte(machine, pointer->addr);
352 pointer->addr = popByte(machine);
353 pointer->value = memReadByte(machine, pointer->addr);
356 pointer->addr = popByte(machine) + machine->regX;
357 pointer->value = memReadByte(machine, pointer->addr);
360 pointer->addr = popByte(machine) + machine->regY;
361 pointer->value = memReadByte(machine, pointer->addr);
364 pointer->addr = popByte(machine);
367 pointer->addr = popWord(machine);
368 pointer->value = memReadByte(machine, pointer->addr);
372 pointer->addr = popWord(machine) + machine->regX;
373 pointer->value = memReadByte(machine, pointer->addr);
377 pointer->addr = popWord(machine) + machine->regY;
378 pointer->value = memReadByte(machine, pointer->addr);
381 /* Handled elsewhere */
388 static void dismem(machine_6502 *machine, AddrMode adm, char *output){
396 case IMMEDIATE_GREAT:
397 case IMMEDIATE_VALUE:
398 n = popByte(machine);
399 sprintf(output,"#$%x",n);
402 zp = popByte(machine);
403 n = memReadByte(machine,zp) +
404 (memReadByte(machine,zp+1)<<8);
405 sprintf(output,"($%x,x)",n);
408 zp = popByte(machine);
409 n = memReadByte(machine,zp) +
410 (memReadByte(machine,zp+1)<<8);
411 sprintf(output,"($%x),y",n);
415 n = popByte(machine);
416 sprintf(output,"$%x",n);
419 n = popByte(machine);
420 sprintf(output,"$%x,x",n);
423 n = popByte(machine);
424 sprintf(output,"$%x,y",n);
427 n = popWord(machine);
428 sprintf(output,"$%x",n);
432 n = popWord(machine);
433 sprintf(output,"$%x,x",n);
437 n = popWord(machine);
438 sprintf(output,"$%x,x",n);
446 /* manZeroNeg - Manage the negative and zero flags */
447 static void manZeroNeg(machine_6502 *machine, Bit8 value){
448 machine->regP = setBit(machine->regP, ZERO_FL, (value == 0));
449 machine->regP = setBit(machine->regP, NEGATIVE_FL, bitOn(value,NEGATIVE_FL));
452 static void warnValue(BOOL isValue){
454 fprintf(stderr,"Invalid Value from getValue.\n");
458 static void jmpADC(machine_6502 *machine, AddrMode adm){
461 Bit8 c = bitOn(machine->regP, CARRY_FL);
462 BOOL isValue = getValue(machine, adm, &ptr);
466 if (bitOn(machine->regA, NEGATIVE_FL) &&
467 bitOn(ptr.value, NEGATIVE_FL))
468 machine->regP = setBit(machine->regP, OVERFLOW_FL, 0);
470 machine->regP = setBit(machine->regP, OVERFLOW_FL, 1);
472 if (bitOn(machine->regP, DECIMAL_FL)) {
473 tmp = nibble(machine->regA,RIGHT) + nibble(ptr.value,RIGHT ) + c;
474 /* The decimal part is limited to 0 through 9 */
476 tmp = 0x10 | ((tmp + 6) & 0xf);
478 tmp += nibble(machine->regA,LEFT) + nibble(ptr.value,LEFT);
480 machine->regP = setBit(machine->regP,CARRY_FL,1);
481 if (bitOn(machine->regP, OVERFLOW_FL) && tmp >= 0x180)
482 machine->regP = setBit(machine->regP, OVERFLOW_FL, 0);
486 machine->regP = setBit(machine->regP,CARRY_FL,0);
487 if (bitOn(machine->regP, OVERFLOW_FL) && tmp < 0x80)
488 machine->regP = setBit(machine->regP, OVERFLOW_FL, 0);
492 tmp = machine->regA + ptr.value + c;
494 machine->regP = setBit(machine->regP,CARRY_FL,1);
495 if (bitOn(machine->regP, OVERFLOW_FL) && tmp >= 0x180)
496 machine->regP =setBit(machine->regP, OVERFLOW_FL, 0);
499 machine->regP = setBit(machine->regP,CARRY_FL,0);
500 if (bitOn(machine->regP, OVERFLOW_FL) && tmp < 0x80)
501 machine->regP =setBit(machine->regP, OVERFLOW_FL, 0);
506 manZeroNeg(machine,machine->regA);
509 static void jmpAND(machine_6502 *machine, AddrMode adm){
511 BOOL isValue = getValue(machine, adm, &ptr);
513 machine->regA &= ptr.value;
514 manZeroNeg(machine,machine->regA);
517 static void jmpASL(machine_6502 *machine, AddrMode adm){
519 BOOL isValue = getValue(machine, adm, &ptr);
521 machine->regP = setBit(machine->regP, CARRY_FL, bitOn(ptr.value, NEGATIVE_FL));
522 ptr.value = ptr.value << 1;
523 ptr.value = setBit(ptr.value, CARRY_FL, 0);
524 memStoreByte(machine, ptr.addr, ptr.value);
525 manZeroNeg(machine,ptr.value);
527 else { /* Accumulator */
528 machine->regP = setBit(machine->regP, CARRY_FL, bitOn(machine->regA, NEGATIVE_FL));
529 machine->regA = machine->regA << 1;
530 machine->regA = setBit(machine->regA, CARRY_FL, 0);
531 manZeroNeg(machine,machine->regA);
536 static void jmpBIT(machine_6502 *machine, AddrMode adm){
538 BOOL isValue = getValue(machine, adm, &ptr);
540 machine->regP = setBit(machine->regP, ZERO_FL, (ptr.value & machine->regA));
541 machine->regP = setBit(machine->regP, OVERFLOW_FL, bitOn(ptr.value, OVERFLOW_FL));
542 machine->regP = setBit(machine->regP, NEGATIVE_FL, bitOn(ptr.value, NEGATIVE_FL));
546 static void jumpBranch(machine_6502 *machine, Bit16 offset){
548 machine->regPC = machine->regPC - (0x100 - offset);
550 machine->regPC = machine->regPC + offset;
553 static void jmpBPL(machine_6502 *machine, AddrMode adm){
555 BOOL isValue = getValue(machine, adm, &ptr);
557 if (bitOff(machine->regP,NEGATIVE_FL))
558 jumpBranch(machine, ptr.addr);
562 static void jmpBMI(machine_6502 *machine, AddrMode adm){
564 BOOL isValue = getValue(machine, adm, &ptr);
566 if (bitOn(machine->regP,NEGATIVE_FL))
567 jumpBranch(machine, ptr.addr);
571 static void jmpBVC(machine_6502 *machine, AddrMode adm){
573 BOOL isValue = getValue(machine, adm, &ptr);
575 if (bitOff(machine->regP,OVERFLOW_FL))
576 jumpBranch(machine, ptr.addr);
579 static void jmpBVS(machine_6502 *machine, AddrMode adm){
581 BOOL isValue = getValue(machine, adm, &ptr);
583 if (bitOn(machine->regP,OVERFLOW_FL))
584 jumpBranch(machine, ptr.addr);
587 static void jmpBCC(machine_6502 *machine, AddrMode adm){
589 BOOL isValue = getValue(machine, adm, &ptr);
591 if (bitOff(machine->regP,CARRY_FL))
592 jumpBranch(machine, ptr.addr);
595 static void jmpBCS(machine_6502 *machine, AddrMode adm){
597 BOOL isValue = getValue(machine, adm, &ptr);
599 if (bitOn(machine->regP,CARRY_FL))
600 jumpBranch(machine, ptr.addr);
603 static void jmpBNE(machine_6502 *machine, AddrMode adm){
605 BOOL isValue = getValue(machine, adm, &ptr);
607 if (bitOff(machine->regP, ZERO_FL))
608 jumpBranch(machine, ptr.addr);
611 static void jmpBEQ(machine_6502 *machine, AddrMode adm){
613 BOOL isValue = getValue(machine, adm, &ptr);
615 if (bitOn(machine->regP, ZERO_FL))
616 jumpBranch(machine, ptr.addr);
619 static void doCompare(machine_6502 *machine, Bit16 reg, Pointer *ptr){
620 machine->regP = setBit(machine->regP,CARRY_FL, ((reg + ptr->value) > 0xff));
621 manZeroNeg(machine,(reg - ptr->value));
624 static void jmpCMP(machine_6502 *machine, AddrMode adm){
626 BOOL isValue = getValue(machine, adm, &ptr);
628 doCompare(machine,machine->regA,&ptr);
631 static void jmpCPX(machine_6502 *machine, AddrMode adm){
633 BOOL isValue = getValue(machine, adm, &ptr);
635 doCompare(machine,machine->regX,&ptr);
638 static void jmpCPY(machine_6502 *machine, AddrMode adm){
640 BOOL isValue = getValue(machine, adm, &ptr);
642 doCompare(machine,machine->regY,&ptr);
645 static void jmpDEC(machine_6502 *machine, AddrMode adm){
647 BOOL isValue = getValue(machine, adm, &ptr);
653 memStoreByte(machine, ptr.addr, ptr.value);
654 manZeroNeg(machine,ptr.value);
657 static void jmpEOR(machine_6502 *machine, AddrMode adm){
659 BOOL isValue = getValue(machine, adm, &ptr);
661 machine->regA ^= ptr.value;
662 manZeroNeg(machine, machine->regA);
665 static void jmpCLC(machine_6502 *machine, AddrMode adm){
666 machine->regP = setBit(machine->regP, CARRY_FL, 0);
669 static void jmpSEC(machine_6502 *machine, AddrMode adm){
670 machine->regP = setBit(machine->regP, CARRY_FL, 1);
673 static void jmpCLI(machine_6502 *machine, AddrMode adm){
674 machine->regP = setBit(machine->regP, INTERRUPT_FL, 0);
677 static void jmpSEI(machine_6502 *machine, AddrMode adm){
678 machine->regP = setBit(machine->regP, INTERRUPT_FL, 1);
681 static void jmpCLV(machine_6502 *machine, AddrMode adm){
682 machine->regP = setBit(machine->regP, OVERFLOW_FL, 0);
685 static void jmpCLD(machine_6502 *machine, AddrMode adm){
686 machine->regP = setBit(machine->regP, DECIMAL_FL, 0);
689 static void jmpSED(machine_6502 *machine, AddrMode adm){
690 machine->regP = setBit(machine->regP, DECIMAL_FL, 1);
693 static void jmpINC(machine_6502 *machine, AddrMode adm){
695 BOOL isValue = getValue(machine, adm, &ptr);
697 ptr.value = (ptr.value + 1) & 0xFF;
698 memStoreByte(machine, ptr.addr, ptr.value);
699 manZeroNeg(machine,ptr.value);
702 static void jmpJMP(machine_6502 *machine, AddrMode adm){
704 BOOL isValue = getValue(machine, adm, &ptr);
706 machine->regPC = ptr.addr;
709 static void jmpJSR(machine_6502 *machine, AddrMode adm){
711 /* Move past the 2 byte parameter. JSR is always followed by
713 Bit16 currAddr = machine->regPC + 2;
714 BOOL isValue = getValue(machine, adm, &ptr);
716 stackPush(machine, (currAddr >> 8) & 0xff);
717 stackPush(machine, currAddr & 0xff);
718 machine->regPC = ptr.addr;
721 static void jmpLDA(machine_6502 *machine, AddrMode adm){
723 BOOL isValue = getValue(machine, adm, &ptr);
725 machine->regA = ptr.value;
726 manZeroNeg(machine, machine->regA);
729 static void jmpLDX(machine_6502 *machine, AddrMode adm){
731 BOOL isValue = getValue(machine, adm, &ptr);
733 machine->regX = ptr.value;
734 manZeroNeg(machine, machine->regX);
737 static void jmpLDY(machine_6502 *machine, AddrMode adm){
739 BOOL isValue = getValue(machine, adm, &ptr);
741 machine->regY = ptr.value;
742 manZeroNeg(machine, machine->regY);
745 static void jmpLSR(machine_6502 *machine, AddrMode adm){
747 BOOL isValue = getValue(machine, adm, &ptr);
750 setBit(machine->regP, CARRY_FL,
751 bitOn(ptr.value, CARRY_FL));
752 ptr.value = ptr.value >> 1;
753 ptr.value = setBit(ptr.value,NEGATIVE_FL,0);
754 memStoreByte(machine,ptr.addr,ptr.value);
755 manZeroNeg(machine,ptr.value);
757 else { /* Accumulator */
759 setBit(machine->regP, CARRY_FL,
760 bitOn(machine->regA, CARRY_FL));
761 machine->regA = machine->regA >> 1;
762 machine->regA = setBit(machine->regA,NEGATIVE_FL,0);
763 manZeroNeg(machine,ptr.value);
767 static void jmpNOP(machine_6502 *machine, AddrMode adm){
771 static void jmpORA(machine_6502 *machine, AddrMode adm){
773 BOOL isValue = getValue(machine, adm, &ptr);
775 machine->regA |= ptr.value;
776 manZeroNeg(machine,machine->regA);
779 static void jmpTAX(machine_6502 *machine, AddrMode adm){
780 machine->regX = machine->regA;
781 manZeroNeg(machine,machine->regX);
784 static void jmpTXA(machine_6502 *machine, AddrMode adm){
785 machine->regA = machine->regX;
786 manZeroNeg(machine,machine->regA);
789 static void jmpDEX(machine_6502 *machine, AddrMode adm){
790 if (machine->regX > 0)
793 machine->regX = 0xFF;
794 manZeroNeg(machine, machine->regX);
797 static void jmpINX(machine_6502 *machine, AddrMode adm){
798 Bit16 value = machine->regX + 1;
799 machine->regX = value & 0xFF;
800 manZeroNeg(machine, machine->regX);
803 static void jmpTAY(machine_6502 *machine, AddrMode adm){
804 machine->regY = machine->regA;
805 manZeroNeg(machine, machine->regY);
808 static void jmpTYA(machine_6502 *machine, AddrMode adm){
809 machine->regA = machine->regY;
810 manZeroNeg(machine, machine->regA);
813 static void jmpDEY(machine_6502 *machine, AddrMode adm){
814 if (machine->regY > 0)
817 machine->regY = 0xFF;
818 manZeroNeg(machine, machine->regY);
821 static void jmpINY(machine_6502 *machine, AddrMode adm){
822 Bit16 value = machine->regY + 1;
823 machine->regY = value & 0xff;
824 manZeroNeg(machine, machine->regY);
827 static void jmpROR(machine_6502 *machine, AddrMode adm){
830 BOOL isValue = getValue(machine, adm, &ptr);
832 cf = bitOn(machine->regP, CARRY_FL);
834 setBit(machine->regP, CARRY_FL,
835 bitOn(ptr.value, CARRY_FL));
836 ptr.value = ptr.value >> 1;
837 ptr.value = setBit(ptr.value, NEGATIVE_FL, cf);
838 memStoreByte(machine, ptr.addr, ptr.value);
839 manZeroNeg(machine, ptr.value);
842 cf = bitOn(machine->regP, CARRY_FL);
844 setBit(machine->regP, CARRY_FL,
845 bitOn(machine->regA, CARRY_FL));
846 machine->regA = machine->regA >> 1;
847 machine->regA = setBit(machine->regA, NEGATIVE_FL, cf);
848 manZeroNeg(machine, machine->regA);
852 static void jmpROL(machine_6502 *machine, AddrMode adm){
855 BOOL isValue = getValue(machine, adm, &ptr);
857 cf = bitOn(machine->regP, CARRY_FL);
859 setBit(machine->regP, CARRY_FL,
860 bitOn(ptr.value, NEGATIVE_FL));
861 ptr.value = ptr.value << 1;
862 ptr.value = setBit(ptr.value, CARRY_FL, cf);
863 memStoreByte(machine, ptr.addr, ptr.value);
864 manZeroNeg(machine, ptr.value);
867 cf = bitOn(machine->regP, CARRY_FL);
869 setBit(machine->regP, CARRY_FL,
870 bitOn(machine->regA,NEGATIVE_FL));
871 machine->regA = machine->regA << 1;
872 machine->regA = setBit(machine->regA, CARRY_FL, cf);
873 manZeroNeg(machine, machine->regA);
877 static void jmpRTI(machine_6502 *machine, AddrMode adm){
878 machine->regP = stackPop(machine);
879 machine->regPC = stackPop(machine);
882 static void jmpRTS(machine_6502 *machine, AddrMode adm){
884 BOOL isValue = getValue(machine, adm, &ptr);
885 Bit16 nr = stackPop(machine);
886 Bit16 nl = stackPop(machine);
887 warnValue(! isValue);
888 machine->regPC = (nl << 8) | nr;
891 static void jmpSBC(machine_6502 *machine, AddrMode adm){
894 Bit8 c = bitOn(machine->regP, CARRY_FL);
896 BOOL isValue = getValue(machine, adm, &ptr);
898 /*vflag = (bitOn(machine->regA,NEGATIVE_FL) &&
899 bitOn(ptr.value, NEGATIVE_FL));*/
901 if (bitOn(machine->regP, DECIMAL_FL)) {
902 Bit8 ar = nibble(machine->regA, RIGHT);
903 Bit8 br = nibble(ptr.value, RIGHT);
904 Bit8 al = nibble(machine->regA, LEFT);
905 Bit8 bl = nibble(ptr.value, LEFT);
907 tmp = 0xf + ar - br + c;
918 machine->regP = setBit(machine->regP, CARRY_FL, 0);
919 if (bitOn(machine->regP, OVERFLOW_FL) && w < 0x80)
920 machine->regP = setBit(machine->regP, OVERFLOW_FL, 0);
924 machine->regP = setBit(machine->regP, CARRY_FL, 1);
925 if (bitOn(machine->regP, OVERFLOW_FL) && w >= 0x180)
926 machine->regP = setBit(machine->regP, OVERFLOW_FL, 0);
929 } /* end decimal mode */
931 w = 0xff + machine->regA - ptr.value + c;
933 machine->regP = setBit(machine->regP, CARRY_FL, 0);
934 if (bitOn(machine->regP, OVERFLOW_FL) && w < 0x80)
935 machine->regP = setBit(machine->regP, OVERFLOW_FL, 0);
938 machine->regP = setBit(machine->regP, CARRY_FL, 1);
939 if (bitOn(machine->regP, OVERFLOW_FL) && w >= 0x180)
940 machine->regP = setBit(machine->regP, OVERFLOW_FL, 0);
944 manZeroNeg(machine,machine->regA);
947 static void jmpSTA(machine_6502 *machine, AddrMode adm){
949 BOOL isValue = getValue(machine, adm, &ptr);
951 memStoreByte(machine,ptr.addr,machine->regA);
954 static void jmpTXS(machine_6502 *machine, AddrMode adm){
955 stackPush(machine,machine->regX);
958 static void jmpTSX(machine_6502 *machine, AddrMode adm){
959 machine->regX = stackPop(machine);
960 manZeroNeg(machine, machine->regX);
963 static void jmpPHA(machine_6502 *machine, AddrMode adm){
964 stackPush(machine, machine->regA);
967 static void jmpPLA(machine_6502 *machine, AddrMode adm){
968 machine->regA = stackPop(machine);
969 manZeroNeg(machine, machine->regA);
972 static void jmpPHP(machine_6502 *machine, AddrMode adm){
973 stackPush(machine,machine->regP);
976 static void jmpPLP(machine_6502 *machine, AddrMode adm){
977 machine->regP = stackPop(machine);
978 machine->regP = setBit(machine->regP, FUTURE_FL, 1);
981 static void jmpSTX(machine_6502 *machine, AddrMode adm){
983 BOOL isValue = getValue(machine, adm, &ptr);
985 memStoreByte(machine,ptr.addr,machine->regX);
988 static void jmpSTY(machine_6502 *machine, AddrMode adm){
990 BOOL isValue = getValue(machine, adm, &ptr);
992 memStoreByte(machine,ptr.addr,machine->regY);
998 static void assignOpCodes(Opcodes *opcodes){
1000 #define SETOP(num, _name, _Imm, _ZP, _ZPX, _ZPY, _ABS, _ABSX, _ABSY, _INDX, _INDY, _SNGL, _BRA, _func) \
1001 {opcodes[num].name[3] = '\0'; \
1002 strncpy(opcodes[num].name, _name, 3); opcodes[num].Imm = _Imm; opcodes[num].ZP = _ZP; \
1003 opcodes[num].ZPX = _ZPX; opcodes[num].ZPY = _ZPY; opcodes[num].ABS = _ABS; \
1004 opcodes[num].ABSX = _ABSX; opcodes[num].ABSY = _ABSY; opcodes[num].INDX = _INDX; \
1005 opcodes[num].INDY = _INDY; opcodes[num].SNGL = _SNGL; opcodes[num].BRA = _BRA; \
1006 opcodes[num].func = _func;}
1008 /* OPCODE Imm ZP ZPX ZPY ABS ABSX ABSY INDX INDY SGNL BRA Jump Function*/
1009 SETOP( 0, "ADC", 0x69, 0x65, 0x75, 0x00, 0x6d, 0x7d, 0x79, 0x61, 0x71, 0x00, 0x00, jmpADC);
1010 SETOP( 1, "AND", 0x29, 0x25, 0x35, 0x31, 0x2d, 0x3d, 0x39, 0x00, 0x00, 0x00, 0x00, jmpAND);
1011 SETOP( 2, "ASL", 0x00, 0x06, 0x16, 0x00, 0x0e, 0x1e, 0x00, 0x00, 0x00, 0x0a, 0x00, jmpASL);
1012 SETOP( 3, "BIT", 0x00, 0x24, 0x00, 0x00, 0x2c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, jmpBIT);
1013 SETOP( 4, "BPL", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, jmpBPL);
1014 SETOP( 5, "BMI", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, jmpBMI);
1015 SETOP( 6, "BVC", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x50, jmpBVC);
1016 SETOP( 7, "BVS", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, jmpBVS);
1017 SETOP( 8, "BCC", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x90, jmpBCC);
1018 SETOP( 9, "BCS", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xb0, jmpBCS);
1019 SETOP(10, "BNE", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xd0, jmpBNE);
1020 SETOP(11, "BEQ", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, jmpBEQ);
1021 SETOP(12, "CMP", 0xc9, 0xc5, 0xd5, 0x00, 0xcd, 0xdd, 0xd9, 0xc1, 0xd1, 0x00, 0x00, jmpCMP);
1022 SETOP(13, "CPX", 0xe0, 0xe4, 0x00, 0x00, 0xec, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, jmpCPX);
1023 SETOP(14, "CPY", 0xc0, 0xc4, 0x00, 0x00, 0xcc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, jmpCPY);
1024 SETOP(15, "DEC", 0x00, 0xc6, 0xd6, 0x00, 0xce, 0xde, 0x00, 0x00, 0x00, 0x00, 0x00, jmpDEC);
1025 SETOP(16, "EOR", 0x49, 0x45, 0x55, 0x00, 0x4d, 0x5d, 0x59, 0x41, 0x51, 0x00, 0x00, jmpEOR);
1026 SETOP(17, "CLC", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x00, jmpCLC);
1027 SETOP(18, "SEC", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x38, 0x00, jmpSEC);
1028 SETOP(19, "CLI", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x58, 0x00, jmpCLI);
1029 SETOP(20, "SEI", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x78, 0x00, jmpSEI);
1030 SETOP(21, "CLV", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xb8, 0x00, jmpCLV);
1031 SETOP(22, "CLD", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xd8, 0x00, jmpCLD);
1032 SETOP(23, "SED", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf8, 0x00, jmpSED);
1033 SETOP(24, "INC", 0x00, 0xe6, 0xf6, 0x00, 0xee, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, jmpINC);
1034 SETOP(25, "JMP", 0x00, 0x00, 0x00, 0x00, 0x4c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, jmpJMP);
1035 SETOP(26, "JSR", 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, jmpJSR);
1036 SETOP(27, "LDA", 0xa9, 0xa5, 0xb5, 0x00, 0xad, 0xbd, 0xb9, 0xa1, 0xb1, 0x00, 0x00, jmpLDA);
1037 SETOP(28, "LDX", 0xa2, 0xa6, 0x00, 0xb6, 0xae, 0x00, 0xbe, 0x00, 0x00, 0x00, 0x00, jmpLDX);
1038 SETOP(29, "LDY", 0xa0, 0xa4, 0xb4, 0x00, 0xac, 0xbc, 0x00, 0x00, 0x00, 0x00, 0x00, jmpLDY);
1039 SETOP(30, "LSR", 0x00, 0x46, 0x56, 0x00, 0x4e, 0x5e, 0x00, 0x00, 0x00, 0x4a, 0x00, jmpLSR);
1040 SETOP(31, "NOP", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xea, 0x00, jmpNOP);
1041 SETOP(32, "ORA", 0x09, 0x05, 0x15, 0x00, 0x0d, 0x1d, 0x19, 0x01, 0x11, 0x00, 0x00, jmpORA);
1042 SETOP(33, "TAX", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xaa, 0x00, jmpTAX);
1043 SETOP(34, "TXA", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x8a, 0x00, jmpTXA);
1044 SETOP(35, "DEX", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xca, 0x00, jmpDEX);
1045 SETOP(36, "INX", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe8, 0x00, jmpINX);
1046 SETOP(37, "TAY", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xa8, 0x00, jmpTAY);
1047 SETOP(38, "TYA", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x98, 0x00, jmpTYA);
1048 SETOP(39, "DEY", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x88, 0x00, jmpDEY);
1049 SETOP(40, "INY", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc8, 0x00, jmpINY);
1050 SETOP(41, "ROR", 0x00, 0x66, 0x76, 0x00, 0x6e, 0x7e, 0x00, 0x00, 0x00, 0x6a, 0x00, jmpROR);
1051 SETOP(42, "ROL", 0x00, 0x26, 0x36, 0x00, 0x2e, 0x3e, 0x00, 0x00, 0x00, 0x2a, 0x00, jmpROL);
1052 SETOP(43, "RTI", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, jmpRTI);
1053 SETOP(44, "RTS", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x00, jmpRTS);
1054 SETOP(45, "SBC", 0xe9, 0xe5, 0xf5, 0x00, 0xed, 0xfd, 0xf9, 0xe1, 0xf1, 0x00, 0x00, jmpSBC);
1055 SETOP(46, "STA", 0x00, 0x85, 0x95, 0x00, 0x8d, 0x9d, 0x99, 0x81, 0x91, 0x00, 0x00, jmpSTA);
1056 SETOP(47, "TXS", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x9a, 0x00, jmpTXS);
1057 SETOP(48, "TSX", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xba, 0x00, jmpTSX);
1058 SETOP(49, "PHA", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x48, 0x00, jmpPHA);
1059 SETOP(50, "PLA", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x68, 0x00, jmpPLA);
1060 SETOP(51, "PHP", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, jmpPHP);
1061 SETOP(52, "PLP", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x28, 0x00, jmpPLP);
1062 SETOP(53, "STX", 0x00, 0x86, 0x00, 0x96, 0x8e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, jmpSTX);
1063 SETOP(54, "STY", 0x00, 0x84, 0x94, 0x00, 0x8c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, jmpSTY);
1064 SETOP(55, "---", 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, NULL);
1067 static void buildIndexCache(machine_6502 *machine){
1069 for (i = 0; i < NUM_OPCODES; i++) {
1070 if (machine->opcodes[i].Imm != 0x00){
1071 machine->opcache[machine->opcodes[i].Imm].adm = IMMEDIATE_VALUE;
1072 machine->opcache[machine->opcodes[i].Imm].index = i;
1074 if (machine->opcodes[i].ZP != 0x00){
1075 machine->opcache[machine->opcodes[i].ZP].adm = ZERO;
1076 machine->opcache[machine->opcodes[i].ZP].index = i;
1078 if (machine->opcodes[i].ZPX != 0x00){
1079 machine->opcache[machine->opcodes[i].ZPX].adm = ZERO_X;
1080 machine->opcache[machine->opcodes[i].ZPX].index = i;;
1082 if (machine->opcodes[i].ZPY != 0x00){
1083 machine->opcache[machine->opcodes[i].ZPY].adm = ZERO_Y;
1084 machine->opcache[machine->opcodes[i].ZPY].index = i;;
1086 if (machine->opcodes[i].ABS != 0x00){
1087 machine->opcache[machine->opcodes[i].ABS].adm = ABS_VALUE;
1088 machine->opcache[machine->opcodes[i].ABS].index = i;;
1090 if (machine->opcodes[i].ABSX != 0x00){
1091 machine->opcache[machine->opcodes[i].ABSX].adm = ABS_X;
1092 machine->opcache[machine->opcodes[i].ABSX].index = i;;
1094 if (machine->opcodes[i].ABSY != 0x00){
1095 machine->opcache[machine->opcodes[i].ABSY].adm = ABS_Y;
1096 machine->opcache[machine->opcodes[i].ABSY].index = i;;
1098 if (machine->opcodes[i].INDX != 0x00){
1099 machine->opcache[machine->opcodes[i].INDX].adm = INDIRECT_X;
1100 machine->opcache[machine->opcodes[i].INDX].index = i;;
1102 if (machine->opcodes[i].INDY != 0x00){
1103 machine->opcache[machine->opcodes[i].INDY].adm = INDIRECT_Y;
1104 machine->opcache[machine->opcodes[i].INDY].index = i;;
1106 if (machine->opcodes[i].SNGL != 0x00){
1107 machine->opcache[machine->opcodes[i].SNGL].adm = SINGLE;
1108 machine->opcache[machine->opcodes[i].SNGL].index = i;
1110 if (machine->opcodes[i].BRA != 0x00){
1111 machine->opcache[machine->opcodes[i].BRA].adm = ABS_OR_BRANCH;
1112 machine->opcache[machine->opcodes[i].BRA].index = i;
1117 /* opIndex() - Search the opcode table for a match. If found return
1118 the index into the optable and the address mode of the opcode. If
1119 the opcode is not found then return -1. */
1120 static int opIndex(machine_6502 *machine, Bit8 opcode, AddrMode *adm){
1121 /* XXX could catch errors by setting a addressmode of error or something */
1122 *adm = machine->opcache[opcode].adm;
1123 return machine->opcache[opcode].index;
1127 /* Assembly parser */
1129 static Param *newParam(void){
1133 newp = (Param *) ecalloc(1, sizeof(Param));
1134 newp->type = SINGLE;
1135 for (i = 0; i < MAX_PARAM_VALUE; i++)
1138 newp->label = ecalloc(MAX_LABEL_LEN,sizeof(char));
1143 /* Copy the fields from p2 to p1 */
1144 static void copyParam(Param *p1, Param *p2){
1146 strncpy(p1->label,p2->label,MAX_LABEL_LEN);
1147 for(i = 0; i < MAX_PARAM_VALUE; i++)
1148 p1->value[i] = p2->value[i];
1150 p1->type = p2->type;
1153 static Label *newLabel(void){
1156 newp = (Label *) ecalloc(1, sizeof(Label));
1158 newp->label = ecalloc(MAX_LABEL_LEN,sizeof(char));
1163 static AsmLine *newAsmLine(char *cmd, char *label, BOOL decl, Param *param, int lc)
1167 newp = (AsmLine *) ecalloc(1, sizeof(AsmLine));
1168 newp->labelDecl = decl;
1169 newp->label = newLabel();
1170 strncpy(newp->label->label,label,MAX_LABEL_LEN);
1171 newp->command = estrdup(cmd);
1172 newp->param = newParam();
1173 copyParam(newp->param, param);
1178 static AsmLine *addend(AsmLine *listp, AsmLine *newp)
1183 for (p =listp; p->next != NULL; p = p->next)
1189 static BOOL apply(AsmLine *listp, BOOL(*fn)(AsmLine*, void*), void *arg)
1194 for (p = listp; p != NULL; p = p->next)
1200 static void freeParam(Param *param){
1205 static void freeLabel(Label *label){
1210 static void freeallAsmLine(AsmLine *listp)
1213 for(; listp != NULL; listp = next){
1215 freeParam(listp->param);
1216 freeLabel(listp->label);
1217 free(listp->command);
1222 static BOOL addvalue(Param *param,Bit32 value){
1223 if (0 <= param->vp && param->vp < MAX_PARAM_VALUE) {
1224 param->value[param->vp++] = value;
1228 fprintf(stderr,"Wrong number of parameters: %d. The limit is %d\n",param->vp+1, MAX_PARAM_VALUE);
1233 static void parseError(char *s){
1234 fprintf(stderr,"6502 Syntax Error: %s\n", s);
1237 /* stoupper() - Destructivley modifies the string making all letters upper case*/
1238 static void stoupper(char **s){
1240 while((*s)[i] != '\0'){
1241 (*s)[i] = toupper((*s)[i]);
1246 static BOOL isWhite(char c){
1247 return (c == '\r' || c == '\t' || c == ' ');
1250 static void skipSpace(char **s){
1251 for(; isWhite(**s); (*s)++)
1255 /* nullify() - fills a string with upto sourceLength null characters. */
1256 static void nullify(char *token, unsigned int sourceLength){
1258 while (i < sourceLength)
1262 static BOOL isBlank(const char *token){
1263 return (token[0] == '\0');
1266 static BOOL isCommand(machine_6502 *machine, const char *token){
1269 while (i < NUM_OPCODES) {
1270 if (strcmp(machine->opcodes[i].name,token) == 0)
1275 if (strcmp(token, "DCB") == 0) return TRUE;
1279 /* hasChar() - Check to see if the current line has a certain
1281 static BOOL hasChar(char *s, char c){
1282 for(; *s != '\0' && *s != '\n'; s++) {
1289 static BOOL ishexdigit(char c){
1293 char c1 = toupper(c);
1294 return ('A' <= c1 && c1 <= 'F');
1298 /* isCmdChar() - Is this a valid character for a command. All of the
1299 command are alpha except for the entry point code that is "*=" */
1300 static BOOL isCmdChar(char c){
1301 return (isalpha(c) || c == '*' || c == '=');
1305 /* command() - parse a command from the source code. We pass along a
1306 machine so the opcode can be validated. */
1307 static BOOL command(machine_6502 *machine, char **s, char **cmd){
1310 for(;isCmdChar(**s) && i < MAX_CMD_LEN; (*s)++)
1313 return TRUE; /* Could be a blank line. */
1314 else if (strcmp(*cmd,"*=") == 0)
1315 return TRUE; /* This is an entry point. */
1317 return isCommand(machine,*cmd);
1320 static BOOL declareLabel(char **s, char **label){
1323 for(;**s != ':' && **s != '\n' && **s != '\0'; (*s)++){
1326 (*label)[i++] = **s;
1329 return FALSE; /* Current line has to have a label */
1330 else if (**s == ':'){
1331 (*s)++; /* Skip colon */
1338 static BOOL parseHex(char **s, Bit32 *value){
1339 enum { MAX_HEX_LEN = 5 };
1341 char *hex = ecalloc(MAX_HEX_LEN, sizeof(char));
1344 (*s)++; /* move pass $ */
1345 for(; ishexdigit(**s) && i < MAX_HEX_LEN; (*s)++)
1348 *value = strtol(hex,NULL,16);
1356 static BOOL parseDec(char **s, Bit32 *value){
1357 enum { MAX_DEC_LEN = 4 };
1358 char *dec = ecalloc(MAX_DEC_LEN, sizeof(char));
1360 for(i = 0; isdigit(**s) && i < MAX_DEC_LEN; (*s)++)
1372 static BOOL parseValue(char **s, Bit32 *value){
1375 return parseHex(s, value);
1377 return parseDec(s, value);
1380 static BOOL paramLabel(char **s, char **label){
1382 for(i = 0; (isalnum(**s) || **s == '_') && i < MAX_LABEL_LEN; (*s)++)
1383 (*label)[i++] = **s;
1391 static BOOL immediate(char **s, Param *param){
1395 (*s)++; /*Move past hash */
1396 if (**s == '<' || **s == '>'){
1397 char *label = ecalloc(MAX_LABEL_LEN, sizeof(char));
1398 param->type = (**s == '<') ? IMMEDIATE_LESS : IMMEDIATE_GREAT;
1399 (*s)++; /* move past < or > */
1400 if (paramLabel(s, &label)){
1401 int ln = strlen(label) + 1;
1402 strncpy(param->label, label, ln);
1410 if (parseValue(s, &value)){
1412 parseError("Immediate value is too large.");
1415 param->type = IMMEDIATE_VALUE;
1416 return addvalue(param, value);
1422 static BOOL isDirection(char c){
1423 return (c == 'X' || c == 'Y');
1426 static BOOL getDirection(char **s, char *direction){
1431 if (isDirection(**s)){
1440 static BOOL indirect(char **s, Param *param){
1448 if (! parseHex(s,&value))
1451 parseError("Indirect value is too large.");
1454 if (!addvalue(param, value))
1459 if (getDirection(s,&c)) {
1461 param->type = INDIRECT_Y;
1466 else if (getDirection(s, &c)){
1471 param->type = INDIRECT_X;
1479 static BOOL dcbValue(char **s, Param *param){
1481 if (! parseValue(s,&val))
1487 if (!addvalue(param,val))
1490 param->type = DCB_PARAM;
1495 return dcbValue(s, param);
1501 static BOOL value(char **s, Param *param){
1506 if (! parseValue(s,&val))
1510 dir = getDirection(s,&c);
1511 if (!addvalue(param,val))
1516 param->type = ABS_X;
1518 param->type = ABS_Y;
1523 param->type = ABS_VALUE;
1526 param->type = ZERO_X;
1528 param->type = ZERO_Y;
1538 static BOOL label(char **s, Param *param){
1539 char *label = ecalloc(MAX_LABEL_LEN, sizeof(char));
1541 BOOL labelOk = FALSE;
1542 if (paramLabel(s, &label)){
1544 param->type = ABS_OR_BRANCH;
1545 if (getDirection(s, &c)){
1547 param->type = ABS_LABEL_X;
1549 param->type = ABS_LABEL_Y;
1553 strncpy(param->label,label,MAX_LABEL_LEN);
1559 static BOOL parameter(const char *cmd, char **s, Param *param){
1561 if (**s == '\0' || **s == '\n')
1563 else if (**s == '#')
1564 return immediate(s,param);
1565 else if (**s == '(')
1566 return indirect(s,param);
1567 else if (**s == '$' || isdigit(**s)){
1568 if (strcmp(cmd, "DCB") == 0)
1569 return dcbValue(s,param);
1571 return value(s,param);
1573 else if (isalpha(**s))
1574 return label(s ,param);
1576 return FALSE; /* Invalid Parameter */
1579 static void comment(char **s){
1582 for(;**s != '\n' && **s != '\0'; (*s)++)
1586 static void initParam(Param *param){
1588 param->type = SINGLE;
1589 for(i = 0; i < MAX_PARAM_VALUE; i++)
1590 param->value[i] = 0;
1592 nullify(param->label,MAX_LABEL_LEN);
1596 static AsmLine *parseAssembly(machine_6502 *machine, BOOL *codeOk, const char *code){
1598 char *cmd = ecalloc(MAX_CMD_LEN, sizeof(char));
1599 char *label = ecalloc(MAX_LABEL_LEN, sizeof(char));
1600 char *start; /*pointer to the start of the code.*/
1601 unsigned int lc = 1;
1604 AsmLine *listp = NULL;
1612 while(*s != '\0' && *codeOk){
1614 nullify(cmd, MAX_CMD_LEN);
1615 nullify(label, MAX_LABEL_LEN);
1622 continue; /* blank line */
1624 else if (*s == '\0')
1625 continue; /* no newline at the end of the code */
1626 else if (hasChar(s,':')){
1628 if(! declareLabel(&s,&label)){
1634 if(!command(machine, &s, &cmd)){
1640 if(!parameter(cmd, &s, param)){
1646 if (*s == '\n' || *s == '\0'){
1648 asmm = newAsmLine(cmd,label,decl,param,lc);
1649 listp = addend(listp,asmm);
1657 fprintf(stderr,"Syntax error at line %u\n", lc);
1665 /* fileToBuffer() - Allocates a buffer and loads all of the file into memory. */
1666 static char *fileToBuffer(const char *filename){
1667 const int defaultSize = 1024;
1670 int size = defaultSize;
1672 char *buffer = ecalloc(defaultSize,sizeof(char));
1674 if (!buffer) abort();
1676 ifp = fopen(filename, "rb");
1679 while((c = getc(ifp)) != EOF){
1682 size += defaultSize;
1683 buffer = realloc(buffer, size);
1684 if (buffer == NULL) {
1690 buffer = realloc(buffer, i+2);
1691 if (!buffer) abort();
1692 /* Make sure we have a line feed at the end */
1701 /* reset() - Reset CPU and memory. */
1702 static void reset(machine_6502 *machine){
1704 for ( y = 0; y < 32; y++ ){
1705 for (x = 0; x < 32; x++){
1706 machine->screen[x][y] = 0;
1710 for(x=0; x < MEM_64K; x++)
1711 machine->memory[x] = 0;
1713 machine->codeCompiledOK = FALSE;
1717 machine->regP = setBit(machine->regP, FUTURE_FL, 1);
1718 machine->defaultCodePC = machine->regPC = PROG_START;
1719 machine->regSP = STACK_TOP;
1720 machine->runForever = FALSE;
1721 machine->labelPtr = 0;
1722 machine->codeRunning = FALSE;
1725 /* hexDump() - Dump the memory to output */
1726 void hexDump(machine_6502 *machine, Bit16 start, Bit16 numbytes, FILE *output){
1729 for( i = 0; i < numbytes; i++){
1730 address = start + i;
1731 if ( (i&15) == 0 ) {
1732 fprintf(output,"\n%.4x: ", address);
1734 fprintf(output,"%.2x%s",machine->memory[address], (i & 1) ? " ":"");
1736 fprintf(output,"%s\n",(i&1)?"--":"");
1740 /* void save_program(machine_6502 *machine, char *filename){ */
1742 /* Bit16 pc = PROG_START; */
1743 /* Bit16 end = pc + machine->codeLen; */
1745 /* ofp = fopen(filename, "w"); */
1746 /* if (!ofp) abort(); */
1748 /* fprintf(ofp,"Bit8 prog[%d] =\n{",machine->codeLen); */
1750 /* while(pc < end) */
1751 /* fprintf(ofp,"0x%.2x,%s",machine->memory[pc++],n++%10?" ":"\n"); */
1752 /* fseek(ofp,-2,SEEK_CUR); */
1753 /* fprintf(ofp,"};\n"); */
1758 static BOOL translate(Opcodes *op,Param *param, machine_6502 *machine){
1759 switch(param->type){
1762 pushByte(machine, op->SNGL);
1764 fprintf(stderr,"%s needs a parameter.\n",op->name);
1768 case IMMEDIATE_VALUE:
1770 pushByte(machine, op->Imm);
1771 pushByte(machine, param->value[0]);
1775 fprintf(stderr,"%s does not take IMMEDIATE_VALUE parameters.\n",op->name);
1778 case IMMEDIATE_GREAT:
1780 pushByte(machine, op->Imm);
1781 pushByte(machine, param->lbladdr >> 8);
1785 fprintf(stderr,"%s does not take IMMEDIATE_GREAT parameters.\n",op->name);
1788 case IMMEDIATE_LESS:
1790 pushByte(machine, op->Imm);
1791 pushByte(machine, param->lbladdr & 0xFF);
1795 fprintf(stderr,"%s does not take IMMEDIATE_LESS parameters.\n",op->name);
1800 pushByte(machine, op->INDX);
1801 pushByte(machine, param->value[0]);
1805 fprintf(stderr,"%s does not take INDIRECT_X parameters.\n",op->name);
1810 pushByte(machine, op->INDY);
1811 pushByte(machine, param->value[0]);
1815 fprintf(stderr,"%s does not take INDIRECT_Y parameters.\n",op->name);
1820 pushByte(machine, op->ZP);
1821 pushByte(machine, param->value[0]);
1825 fprintf(stderr,"%s does not take ZERO parameters.\n",op->name);
1830 pushByte(machine, op->ZPX);
1831 pushByte(machine, param->value[0]);
1835 fprintf(stderr,"%s does not take ZERO_X parameters.\n",op->name);
1840 pushByte(machine, op->ZPY);
1841 pushByte(machine, param->value[0]);
1845 fprintf(stderr,"%s does not take ZERO_Y parameters.\n",op->name);
1850 pushByte(machine, op->ABS);
1851 pushWord(machine, param->value[0]);
1855 fprintf(stderr,"%s does not take ABS_VALUE parameters.\n",op->name);
1860 pushByte(machine, op->ABS);
1861 pushWord(machine, param->lbladdr);
1865 pushByte(machine, op->BRA);
1867 int diff = abs(param->lbladdr - machine->defaultCodePC);
1868 int backward = (param->lbladdr < machine->defaultCodePC);
1869 pushByte(machine, (backward) ? 0xff - diff : diff - 1);
1873 fprintf(stderr,"%s does not take BRANCH parameters.\n",op->name);
1880 pushByte(machine, op->ABSX);
1881 pushWord(machine, param->value[0]);
1885 fprintf(stderr,"%s does not take ABS_X parameters.\n",op->name);
1890 pushByte(machine, op->ABSY);
1891 pushWord(machine, param->value[0]);
1895 fprintf(stderr,"%s does not take ABS_Y parameters.\n",op->name);
1900 pushByte(machine, op->ABSX);
1901 pushWord(machine, param->lbladdr);
1905 fprintf(stderr,"%s does not take ABS_LABEL_X parameters.\n",op->name);
1910 pushByte(machine, op->ABSY);
1911 pushWord(machine, param->lbladdr);
1915 fprintf(stderr,"%s does not take ABS_LABEL_Y parameters.\n",op->name);
1919 /* Handled elsewhere */
1925 /* compileLine() - Compile one line of code. Returns
1926 TRUE if it compile successfully. */
1927 static BOOL compileLine(AsmLine *asmline, void *args){
1928 machine_6502 *machine;
1930 if (isBlank(asmline->command)) return TRUE;
1931 if (strcmp("*=",asmline->command) == 0){
1932 machine->defaultCodePC = asmline->param->value[0];
1934 else if (strcmp("DCB",asmline->command) == 0){
1936 for(i = 0; i < asmline->param->vp; i++)
1937 pushByte(machine, asmline->param->value[i]);
1941 char *command = asmline->command;
1943 for(i = 0; i < NUM_OPCODES; i++){
1944 if (strcmp(machine->opcodes[i].name, command) == 0){
1945 op = machine->opcodes[i];
1949 if (i == NUM_OPCODES)
1950 return FALSE; /* unknow upcode */
1952 return translate(&op,asmline->param,machine);
1957 /* indexLabels() - Get the address for each label */
1958 static BOOL indexLabels(AsmLine *asmline, void *arg){
1959 machine_6502 *machine;
1963 oldDefault = machine->defaultCodePC;
1964 thisPC = machine->regPC;
1965 /* Figure out how many bytes this instruction takes */
1966 machine->codeLen = 0;
1968 if ( ! compileLine(asmline, machine) ){
1972 /* If the machine's defaultCodePC has changed then we encountered a
1973 *= which changes the load address. We need to initials our code
1974 *counter with the current default. */
1975 if (oldDefault == machine->defaultCodePC){
1976 machine->regPC += machine->codeLen;
1979 machine->regPC = machine->defaultCodePC;
1980 /*oldDefault = machine->defaultCodePC;*/
1983 if (asmline->labelDecl) {
1984 asmline->label->addr = thisPC;
1989 static BOOL changeParamLabelAddr(AsmLine *asmline, void *label){
1991 if (strcmp(asmline->param->label, la->label) == 0)
1992 asmline->param->lbladdr = la->addr;
1996 static BOOL linkit(AsmLine *asmline, void *asmlist){
1997 apply(asmlist,changeParamLabelAddr,asmline->label);
2001 /* linkLabels - Make sure all of the references to the labels contain
2003 static void linkLabels(AsmLine *asmlist){
2004 apply(asmlist,linkit,asmlist);
2007 /* compileCode() - Compile the current assembly code for the machine */
2008 static BOOL compileCode(machine_6502 *machine, const char *code){
2013 machine->defaultCodePC = machine->regPC = PROG_START;
2014 asmlist = parseAssembly(machine, &codeOk, code);
2017 /* First pass: Find the addresses for the labels */
2018 if (!apply(asmlist, indexLabels, machine))
2020 /* update label references */
2021 linkLabels(asmlist);
2023 #if 0 /* prints out some debugging information */
2026 if(asmlist != NULL){
2027 for (p = asmlist; p != NULL; p = p->next)
2028 fprintf(stderr,"%s lbl: %s addr: %d ParamLbl: %s ParamAddr: %d\n",
2029 p->command, p->label->label, p->label->addr,
2030 p->param->label, p->param->lbladdr);
2036 /* Second pass: translate the instructions */
2037 machine->codeLen = 0;
2038 /* Link label call push_byte which increments defaultCodePC.
2039 We need to reset it so the compiled code goes in the
2041 machine->defaultCodePC = PROG_START;
2042 if (!apply(asmlist, compileLine, machine))
2045 if (machine->defaultCodePC > PROG_START ){
2046 machine->memory[machine->defaultCodePC] = 0x00;
2050 fprintf(stderr,"No Code to run.\n");
2055 fprintf(stderr,"An error occured while parsing the file.\n");
2058 freeallAsmLine(asmlist);
2064 * execute() - Executes one instruction.
2065 * This is the main part of the CPU emulator.
2069 static void execute(machine_6502 *machine){
2074 if(!machine->codeRunning) return;
2076 opcode = popByte(machine);
2078 machine->codeRunning = FALSE;
2080 opidx = opIndex(machine,opcode,&adm);
2082 machine->opcodes[opidx].func(machine, adm);
2084 fprintf(stderr,"Invalid opcode!\n");
2086 if( (machine->regPC == 0) ||
2087 (!machine->codeRunning) ) {
2088 machine->codeRunning = FALSE;
2092 machine_6502 *build6502(){
2093 machine_6502 *machine;
2094 machine = ecalloc(1, sizeof(machine_6502));
2095 assignOpCodes(machine->opcodes);
2096 buildIndexCache(machine);
2101 void destroy6502(machine_6502 *machine){
2106 void trace(machine_6502 *machine, FILE *output){
2107 Bit8 opcode = memReadByte(machine,machine->regPC);
2110 int opidx = opIndex(machine,opcode,&adm);
2111 int stacksz = STACK_TOP - machine->regSP;
2113 fprintf(output,"\n NVFBDIZC\nP: %d%d%d%d%d%d%d%d ",
2114 bitOn(machine->regP,NEGATIVE_FL),
2115 bitOn(machine->regP,OVERFLOW_FL),
2116 bitOn(machine->regP,FUTURE_FL),
2117 bitOn(machine->regP,BREAK_FL),
2118 bitOn(machine->regP,DECIMAL_FL),
2119 bitOn(machine->regP,INTERRUPT_FL),
2120 bitOn(machine->regP,ZERO_FL),
2121 bitOn(machine->regP,CARRY_FL));
2122 fprintf(output,"A: %.2x X: %.2x Y: %.2x SP: %.4x PC: %.4x\n",
2123 machine->regA, machine->regX, machine->regY, machine->regSP, machine->regPC);
2125 Bit16 pc = machine->regPC;
2126 fprintf(output,"\n%.4x:\t%s",machine->regPC, machine->opcodes[opidx].name);
2127 if (peekValue(machine, adm, &ptr, pc+1))
2128 fprintf(output,"\tAddress:%.4x\tValue:%.4x\n",
2129 ptr.addr,ptr.value);
2131 fprintf(output,"\n");
2133 fprintf(output,"STACK:");
2134 hexDump(machine,(STACK_TOP - stacksz) + 1, stacksz, output);
2137 void disassemble(machine_6502 *machine, FILE *output){
2139 increment the program counter
2141 loop until end of program. */
2148 Bit16 opc = machine->regPC;
2149 mem = calloc(20,sizeof(char));
2150 machine->regPC = PROG_START;
2152 addr = machine->regPC;
2153 opcode = popByte(machine);
2154 opidx = opIndex(machine,opcode,&adm);
2155 for (i = 0; i < 20; i++) mem[i] = '\0';
2156 dismem(machine, adm, mem);
2157 fprintf(output,"%x\t%s\t%s\n",
2158 addr,machine->opcodes[opidx].name,mem);
2159 }while((machine->regPC - PROG_START) < machine->codeLen); /*XXX - may need to change since defaultCodePC */
2161 machine->regPC = opc;
2165 void eval_file(machine_6502 *machine, const char *filename, Plotter plot, void *plotterState){
2168 machine->plot = plot;
2169 machine->plotterState = plotterState;
2171 code = fileToBuffer(filename);
2173 if (! compileCode(machine, code) ) abort();
2177 machine->defaultCodePC = machine->regPC = PROG_START;
2178 machine->codeRunning = TRUE;
2185 }while(machine->codeRunning);
2188 void start_eval_file(machine_6502 *machine, const char *filename, Plotter plot, void *plotterState){
2192 machine->plot = plot;
2193 machine->plotterState = plotterState;
2195 code = fileToBuffer(filename);
2197 if (! compileCode(machine, code) ) abort();
2201 machine->defaultCodePC = machine->regPC = PROG_START;
2202 machine->codeRunning = TRUE;
2206 void start_eval_string(machine_6502 *machine, const char *code,
2207 Plotter plot, void *plotterState){
2210 machine->plot = plot;
2211 machine->plotterState = plotterState;
2213 if (! compileCode(machine, code) ){
2214 fprintf(stderr,"Could not compile code.\n");
2217 machine->defaultCodePC = machine->regPC = PROG_START;
2218 machine->codeRunning = TRUE;
2222 /* void start_eval_binary(machine_6502 *machine, Bit8 *program, */
2223 /* unsigned int proglen, */
2224 /* Plotter plot, void *plotterState){ */
2225 /* unsigned int pc, n; */
2226 /* reset(machine); */
2227 /* machine->plot = plot; */
2228 /* machine->plotterState = plotterState; */
2230 /* machine->regPC = PROG_START; */
2231 /* pc = machine->regPC; */
2232 /* machine->codeLen = proglen; */
2234 /* while (n < proglen){ */
2235 /* machine->memory[pc++] = program[n++]; */
2237 /* machine->codeRunning = TRUE; */
2238 /* execute(machine); */
2241 void next_eval(machine_6502 *machine, int insno){
2243 for (i = 1; i < insno; i++){
2244 if (machine->codeRunning){
2246 trace(machine, stdout);