af0a9341b7b94e312032dde29217260414743e89
[xscreensaver] / hacks / asm6502.c
1 /*-*- indent-tabs-mode:nil -*- */
2 /* Copyright (C) 2007 Jeremy English <jhe@jeremyenglish.org>
3  * 
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 
10  * implied warranty.
11  * 
12  * Created: 12-April-2007 
13  */ 
14
15 /*
16       This is a port of the javascript 6502 assembler, compiler and
17       debugger. The orignal code was copyright 2006 by Stian Soreng -
18       www.6502asm.com
19
20       I changed the structure of the assembler in this version.
21 */
22
23 #define NDEBUG  /* Uncomment when done with debugging */
24
25 #include <stdlib.h>
26 #include <stdio.h>
27 /*#include <malloc.h>*/
28 #include <string.h>
29 #include <errno.h>
30 #include <stdarg.h>
31 #include <assert.h>
32 #include <ctype.h>
33 #include <math.h>
34 #include <stdint.h>
35 #include <unistd.h>
36
37 #include "asm6502.h"
38
39 #ifdef DEBUGGER
40 #  define random rand
41 #endif
42
43 typedef enum{
44   LEFT, RIGHT
45     } Side;
46
47 /* 
48
49 Bit Flags
50    _  _  _  _  _  _  _  _ 
51   |N||V||F||B||D||I||Z||C|
52    -  -  -  -  -  -  -  - 
53    7  6  5  4  3  2  1  0
54
55 */
56
57 typedef enum{
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
61         } Flags;
62         
63
64 typedef BOOL (*CharTest) (char);
65
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);*/
69
70 typedef struct {
71   AddrMode type;
72   Bit32 value[MAX_PARAM_VALUE];
73   unsigned int vp; /*value pointer, index into the value table.*/
74   char *label;
75   Bit32 lbladdr;
76 } Param;
77
78 typedef struct {
79   Bit32 addr; /* Address of the label */  
80   char *label; 
81 } Label;  
82
83 typedef struct AsmLine AsmLine;
84 struct AsmLine {
85   BOOL labelDecl; /* Does the line have a label declaration? */
86   Label *label;
87   char *command;
88   Param *param;
89   AsmLine *next; /* in list */
90 };
91
92 typedef struct {
93   Bit16 addr;
94   Bit16 value;
95 } Pointer;
96
97
98 static void *emalloc(size_t n) {
99   void *p = malloc(n);
100   if (! p) abort();
101   return p;
102 }
103
104 static void *ecalloc(uint32_t nelm, size_t nsize){
105   void *p = calloc(nelm, nsize);
106   if (!p) abort();
107   return p;
108 }
109
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);
115   return s;
116 }
117
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);
122 }
123
124 /*
125  *  stackPush() - Push byte to stack
126  *
127  */
128
129 static void stackPush(machine_6502 *machine, Bit8 value ) {
130   if(machine->regSP >= STACK_BOTTOM){
131     machine->memory[machine->regSP--] = value;
132   }
133   else{
134     fprintf(stderr, "The stack is full: %.4x\n", machine->regSP);
135     machine->codeRunning = FALSE;
136   }
137 }
138
139
140 /*
141  *  stackPop() - Pop byte from stack
142  *
143  */
144
145 static Bit8 stackPop(machine_6502 *machine) {
146   if (machine->regSP < STACK_TOP){
147     Bit8 value =machine->memory[++machine->regSP];
148     return value;
149   }
150   else {
151     /*    fprintf(stderr, "The stack is empty.\n"); xxx */
152     machine->codeRunning = FALSE;
153     return 0;
154   }
155 }
156
157 static void pushByte(machine_6502 *machine, Bit32 value ) {
158   Bit32 address = machine->defaultCodePC;
159   checkAddress(address);
160   machine->memory[address] = value & 0xff;
161   machine->codeLen++;
162   machine->defaultCodePC++;
163 }
164
165 /*
166  * pushWord() - Push a word using pushByte twice
167  *
168  */
169
170 static void pushWord(machine_6502 *machine, Bit16 value ) {
171   pushByte(machine, value & 0xff );
172   pushByte(machine, (value>>8) & 0xff );
173 }
174
175 /*
176  * popByte( machine_6502 *machine,) - Pops a byte
177  *
178  */
179
180 static Bit8 popByte( machine_6502 *machine) {
181   Bit8 value = machine->memory[machine->regPC];
182   machine->regPC++;
183   return value;
184 }
185
186 /*
187  * popWord() - Pops a word using popByte() twice
188  *
189  */
190
191 static int popWord(machine_6502 *machine) {
192   return popByte(machine) + (popByte(machine) << 8);
193 }
194
195
196 /*
197  * memReadByte() - Peek a byte, don't touch any registers
198  *
199  */
200
201 static int memReadByte( machine_6502 *machine, int addr ) {
202   if( addr == 0xfe ) return floor( random()%255 );
203   return machine->memory[addr];
204 }
205
206 static void updateDisplayPixel(machine_6502 *machine, Bit16 addr){
207   Bit8 idx = memReadByte(machine,addr) & 0x0f;
208   Bit8 x,y;
209   addr -= 0x200;
210   x = addr & 0x1f;
211   y = (addr >> 5);
212   if (machine->plot) {
213     machine->plot(x,y,idx,machine->plotterState);
214   }
215 }
216
217 /*
218  * memStoreByte() - Poke a byte, don't touch any registers
219  *
220  */
221
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 );
226 }
227
228
229 \f
230 /* EMULATION CODE */
231
232 static Bit8 bitOn(Bit8 value,Flags bit){
233   Bit8 mask = 1;
234   mask = mask << bit;
235   return ((value & mask) > 0);
236 }
237
238 static Bit8 bitOff(Bit8 value, Flags bit){
239   return (! bitOn(value,bit));
240 }
241
242 static Bit8 setBit(Bit8 value, Flags bit, int on){
243   Bit8 onMask  = 1;
244   Bit8 offMask = 0xff;
245   onMask = onMask << bit;
246   offMask = offMask ^ onMask;
247   return ((on) ? value | onMask : value & offMask);
248 }
249
250 static Bit8 nibble(Bit8 value, Side side){
251   switch(side){
252   case LEFT:  return value & 0xf0;
253   case RIGHT: return value & 0xf;
254   default:
255     fprintf(stderr,"nibble unknown side\n");
256     return 0;
257   }
258 }
259
260
261 /* used for tracing. XXX: combined with function getvalue */
262 static BOOL peekValue(machine_6502 *machine, AddrMode adm, Pointer *pointer, Bit16 PC){
263   Bit8 zp;
264   pointer->value = 0;
265   pointer->addr = 0;
266   switch(adm){
267   case SINGLE:
268     return FALSE;
269   case IMMEDIATE_LESS:
270   case IMMEDIATE_GREAT:
271   case IMMEDIATE_VALUE:
272     pointer->value = memReadByte(machine, PC);
273     return TRUE;
274   case INDIRECT_X:
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);
279     return TRUE;
280   case INDIRECT_Y:
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);
285     return TRUE;
286   case ZERO:
287     pointer->addr = memReadByte(machine, PC);
288     pointer->value = memReadByte(machine, pointer->addr);
289     return TRUE;
290   case ZERO_X:
291     pointer->addr = memReadByte(machine, PC) + machine->regX;
292     pointer->value = memReadByte(machine, pointer->addr);
293     return TRUE;
294   case ZERO_Y:
295     pointer->addr = memReadByte(machine, PC) + machine->regY;
296     pointer->value = memReadByte(machine, pointer->addr);
297     return TRUE;
298   case ABS_OR_BRANCH:
299     pointer->addr = memReadByte(machine, PC);
300     return TRUE;
301   case ABS_VALUE:
302     pointer->addr = memReadByte(machine, PC) + (memReadByte(machine, PC+1) << 8);
303     pointer->value = memReadByte(machine, pointer->addr);
304     return TRUE;
305   case ABS_LABEL_X:
306   case ABS_X:
307     pointer->addr = (memReadByte(machine, PC) + 
308                      (memReadByte(machine, PC+1) << 8)) + machine->regX;
309     pointer->value = memReadByte(machine, pointer->addr);
310     return TRUE;
311   case ABS_LABEL_Y:
312   case ABS_Y:
313     pointer->addr = (memReadByte(machine, PC) + 
314                      (memReadByte(machine, PC+1) << 8)) + machine->regY;
315     pointer->value = memReadByte(machine, pointer->addr);
316     return TRUE;
317   case DCB_PARAM:
318     /* Handled elsewhere */
319     break;
320   }
321   return FALSE;
322
323 }
324
325
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){
328   Bit8 zp;
329   pointer->value = 0;
330   pointer->addr = 0;
331   switch(adm){
332   case SINGLE:
333     return FALSE;
334   case IMMEDIATE_LESS:
335   case IMMEDIATE_GREAT:
336   case IMMEDIATE_VALUE:
337     pointer->value = popByte(machine);
338     return TRUE;
339   case INDIRECT_X:
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);
344     return TRUE;
345   case INDIRECT_Y:
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);
350     return TRUE;
351   case ZERO:
352     pointer->addr = popByte(machine);
353     pointer->value = memReadByte(machine, pointer->addr);
354     return TRUE;
355   case ZERO_X:
356     pointer->addr = popByte(machine) + machine->regX;
357     pointer->value = memReadByte(machine, pointer->addr);
358     return TRUE;
359   case ZERO_Y:
360     pointer->addr = popByte(machine) + machine->regY;
361     pointer->value = memReadByte(machine, pointer->addr);
362     return TRUE;
363   case ABS_OR_BRANCH:
364     pointer->addr = popByte(machine);
365     return TRUE;
366   case ABS_VALUE:
367     pointer->addr = popWord(machine);
368     pointer->value = memReadByte(machine, pointer->addr);
369     return TRUE;
370   case ABS_LABEL_X:
371   case ABS_X:
372     pointer->addr = popWord(machine) + machine->regX;
373     pointer->value = memReadByte(machine, pointer->addr);
374     return TRUE;
375   case ABS_LABEL_Y:
376   case ABS_Y:
377     pointer->addr = popWord(machine) + machine->regY;
378     pointer->value = memReadByte(machine, pointer->addr);
379     return TRUE;
380   case DCB_PARAM:
381     /* Handled elsewhere */
382     break;
383   }
384   return FALSE;
385
386 }
387
388 static void dismem(machine_6502 *machine, AddrMode adm, char *output){
389   Bit8 zp;
390   Bit16 n;
391   switch(adm){
392   case SINGLE:
393     *output = 0;
394     break;
395   case IMMEDIATE_LESS:
396   case IMMEDIATE_GREAT:
397   case IMMEDIATE_VALUE:
398     n = popByte(machine);
399     sprintf(output,"#$%x",n);
400     break;
401   case INDIRECT_X:
402     zp = popByte(machine);
403     n = memReadByte(machine,zp) + 
404       (memReadByte(machine,zp+1)<<8);
405     sprintf(output,"($%x,x)",n);
406     break;
407   case INDIRECT_Y:
408     zp = popByte(machine);
409     n = memReadByte(machine,zp) + 
410       (memReadByte(machine,zp+1)<<8);
411     sprintf(output,"($%x),y",n);
412     break;
413   case ABS_OR_BRANCH:
414   case ZERO:    
415     n = popByte(machine);
416     sprintf(output,"$%x",n);
417     break;
418   case ZERO_X:
419     n = popByte(machine);
420     sprintf(output,"$%x,x",n);
421     break;
422   case ZERO_Y:
423     n = popByte(machine);
424     sprintf(output,"$%x,y",n);
425     break;
426   case ABS_VALUE:
427     n = popWord(machine);
428     sprintf(output,"$%x",n);
429     break;
430   case ABS_LABEL_X:
431   case ABS_X:
432     n = popWord(machine);
433     sprintf(output,"$%x,x",n);
434     break;
435   case ABS_LABEL_Y:
436   case ABS_Y:
437     n = popWord(machine);
438     sprintf(output,"$%x,x",n);
439     break;
440   case DCB_PARAM:
441     *output = 0;
442     break;
443   }
444 }
445
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));
450 }
451
452 static void warnValue(BOOL isValue){
453   if (! isValue){
454     fprintf(stderr,"Invalid Value from getValue.\n");
455   }
456 }
457
458 static void jmpADC(machine_6502 *machine, AddrMode adm){
459   Pointer ptr;
460   Bit16 tmp;
461   Bit8 c = bitOn(machine->regP, CARRY_FL);
462   BOOL isValue = getValue(machine, adm, &ptr);
463
464   warnValue(isValue);
465   
466   if (bitOn(machine->regA, NEGATIVE_FL) &&
467       bitOn(ptr.value, NEGATIVE_FL))
468     machine->regP = setBit(machine->regP, OVERFLOW_FL, 0);
469   else
470     machine->regP = setBit(machine->regP, OVERFLOW_FL, 1);
471
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 */
475     if (tmp >= 10){
476       tmp = 0x10 | ((tmp + 6) & 0xf);
477     }
478     tmp += nibble(machine->regA,LEFT) + nibble(ptr.value,LEFT);
479     if (tmp >= 160){
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);
483       tmp += 0x60;
484     }
485     else {
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);
489     }
490   } /* end decimal */      
491   else {
492     tmp = machine->regA + ptr.value + c;
493     if ( tmp >= 0x100 ){
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);
497     }
498     else {
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);
502     }
503   }
504
505   machine->regA = tmp;
506   manZeroNeg(machine,machine->regA);
507 }
508
509 static void jmpAND(machine_6502 *machine, AddrMode adm){
510   Pointer ptr;
511   BOOL isValue = getValue(machine, adm, &ptr);
512   warnValue(isValue);
513   machine->regA &= ptr.value;
514   manZeroNeg(machine,machine->regA);
515 }
516
517 static void jmpASL(machine_6502 *machine, AddrMode adm){
518   Pointer ptr;
519   BOOL isValue = getValue(machine, adm, &ptr);
520   if (isValue){
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);
526   }
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);
532   }  
533   
534 }
535
536 static void jmpBIT(machine_6502 *machine, AddrMode adm){
537   Pointer ptr;
538   BOOL isValue = getValue(machine, adm, &ptr);
539   warnValue(isValue);
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));
543   
544 }
545
546 static void jumpBranch(machine_6502 *machine, Bit16 offset){
547   if ( offset > 0x7f )
548     machine->regPC = machine->regPC - (0x100 - offset);
549   else
550     machine->regPC = machine->regPC + offset;
551 }
552
553 static void jmpBPL(machine_6502 *machine, AddrMode adm){
554   Pointer ptr;
555   BOOL isValue = getValue(machine, adm, &ptr);
556   warnValue(isValue);
557   if (bitOff(machine->regP,NEGATIVE_FL))
558     jumpBranch(machine, ptr.addr);
559     
560 }
561
562 static void jmpBMI(machine_6502 *machine, AddrMode adm){
563   Pointer ptr;
564   BOOL isValue = getValue(machine, adm, &ptr);
565   warnValue(isValue);
566   if (bitOn(machine->regP,NEGATIVE_FL))
567     jumpBranch(machine, ptr.addr);
568
569 }
570
571 static void jmpBVC(machine_6502 *machine, AddrMode adm){
572   Pointer ptr;
573   BOOL isValue = getValue(machine, adm, &ptr);
574   warnValue(isValue);
575   if (bitOff(machine->regP,OVERFLOW_FL))
576     jumpBranch(machine, ptr.addr);
577 }
578
579 static void jmpBVS(machine_6502 *machine, AddrMode adm){
580   Pointer ptr;
581   BOOL isValue = getValue(machine, adm, &ptr);
582   warnValue(isValue);
583   if (bitOn(machine->regP,OVERFLOW_FL))
584     jumpBranch(machine, ptr.addr);
585 }
586
587 static void jmpBCC(machine_6502 *machine, AddrMode adm){
588   Pointer ptr;
589   BOOL isValue = getValue(machine, adm, &ptr);
590   warnValue(isValue);
591   if (bitOff(machine->regP,CARRY_FL))
592     jumpBranch(machine, ptr.addr);
593 }
594
595 static void jmpBCS(machine_6502 *machine, AddrMode adm){
596   Pointer ptr;
597   BOOL isValue = getValue(machine, adm, &ptr);
598   warnValue(isValue);
599   if (bitOn(machine->regP,CARRY_FL))
600     jumpBranch(machine, ptr.addr);
601 }
602
603 static void jmpBNE(machine_6502 *machine, AddrMode adm){
604   Pointer ptr;
605   BOOL isValue = getValue(machine, adm, &ptr);
606   warnValue(isValue);
607   if (bitOff(machine->regP, ZERO_FL))
608     jumpBranch(machine, ptr.addr);
609 }
610
611 static void jmpBEQ(machine_6502 *machine, AddrMode adm){
612   Pointer ptr;
613   BOOL isValue = getValue(machine, adm, &ptr);
614   warnValue(isValue);
615   if (bitOn(machine->regP, ZERO_FL))
616     jumpBranch(machine, ptr.addr);
617 }
618
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));
622 }
623
624 static void jmpCMP(machine_6502 *machine, AddrMode adm){
625   Pointer ptr;
626   BOOL isValue = getValue(machine, adm, &ptr);
627   warnValue(isValue);
628   doCompare(machine,machine->regA,&ptr);
629 }
630
631 static void jmpCPX(machine_6502 *machine, AddrMode adm){
632   Pointer ptr;
633   BOOL isValue = getValue(machine, adm, &ptr);
634   warnValue(isValue);
635   doCompare(machine,machine->regX,&ptr);
636 }
637
638 static void jmpCPY(machine_6502 *machine, AddrMode adm){
639   Pointer ptr;
640   BOOL isValue = getValue(machine, adm, &ptr);
641   warnValue(isValue);
642   doCompare(machine,machine->regY,&ptr);
643 }
644
645 static void jmpDEC(machine_6502 *machine, AddrMode adm){
646   Pointer ptr;
647   BOOL isValue = getValue(machine, adm, &ptr);
648   warnValue(isValue);
649   if (ptr.value > 0)
650     ptr.value--;
651   else
652     ptr.value = 0xFF;
653   memStoreByte(machine, ptr.addr, ptr.value);
654   manZeroNeg(machine,ptr.value);
655 }
656
657 static void jmpEOR(machine_6502 *machine, AddrMode adm){
658   Pointer ptr;
659   BOOL isValue = getValue(machine, adm, &ptr);
660   warnValue(isValue);
661   machine->regA ^= ptr.value;
662   manZeroNeg(machine, machine->regA);
663 }
664
665 static void jmpCLC(machine_6502 *machine, AddrMode adm){
666   machine->regP = setBit(machine->regP, CARRY_FL, 0);
667 }
668
669 static void jmpSEC(machine_6502 *machine, AddrMode adm){
670   machine->regP = setBit(machine->regP, CARRY_FL, 1);
671 }
672
673 static void jmpCLI(machine_6502 *machine, AddrMode adm){
674   machine->regP = setBit(machine->regP, INTERRUPT_FL, 0);
675 }
676
677 static void jmpSEI(machine_6502 *machine, AddrMode adm){
678   machine->regP = setBit(machine->regP, INTERRUPT_FL, 1);
679 }
680
681 static void jmpCLV(machine_6502 *machine, AddrMode adm){
682   machine->regP = setBit(machine->regP, OVERFLOW_FL, 0);
683 }
684
685 static void jmpCLD(machine_6502 *machine, AddrMode adm){
686   machine->regP = setBit(machine->regP, DECIMAL_FL, 0);
687 }
688
689 static void jmpSED(machine_6502 *machine, AddrMode adm){
690   machine->regP = setBit(machine->regP, DECIMAL_FL, 1);
691 }
692
693 static void jmpINC(machine_6502 *machine, AddrMode adm){
694   Pointer ptr;
695   BOOL isValue = getValue(machine, adm, &ptr);
696   warnValue(isValue);
697   ptr.value = (ptr.value + 1) & 0xFF;
698   memStoreByte(machine, ptr.addr, ptr.value);
699   manZeroNeg(machine,ptr.value);
700 }
701
702 static void jmpJMP(machine_6502 *machine, AddrMode adm){
703   Pointer ptr;
704   BOOL isValue = getValue(machine, adm, &ptr);
705   warnValue(isValue);
706   machine->regPC = ptr.addr;
707 }
708
709 static void jmpJSR(machine_6502 *machine, AddrMode adm){
710   Pointer ptr;
711   /* Move past the 2 byte parameter. JSR is always followed by
712      absolute address. */
713   Bit16 currAddr = machine->regPC + 2;
714   BOOL isValue = getValue(machine, adm, &ptr);
715   warnValue(isValue);
716   stackPush(machine, (currAddr >> 8) & 0xff);
717   stackPush(machine, currAddr & 0xff);
718   machine->regPC = ptr.addr;  
719 }
720
721 static void jmpLDA(machine_6502 *machine, AddrMode adm){
722   Pointer ptr;
723   BOOL isValue = getValue(machine, adm, &ptr);
724   warnValue(isValue);
725   machine->regA = ptr.value;
726   manZeroNeg(machine, machine->regA);
727 }
728
729 static void jmpLDX(machine_6502 *machine, AddrMode adm){
730   Pointer ptr;
731   BOOL isValue = getValue(machine, adm, &ptr);
732   warnValue(isValue);
733   machine->regX = ptr.value;
734   manZeroNeg(machine, machine->regX);
735 }
736
737 static void jmpLDY(machine_6502 *machine, AddrMode adm){
738   Pointer ptr;
739   BOOL isValue = getValue(machine, adm, &ptr);
740   warnValue(isValue);
741   machine->regY = ptr.value;
742   manZeroNeg(machine, machine->regY);
743 }
744
745 static void jmpLSR(machine_6502 *machine, AddrMode adm){
746   Pointer ptr;
747   BOOL isValue = getValue(machine, adm, &ptr);
748   if (isValue){
749     machine->regP = 
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);
756   }
757   else { /* Accumulator */
758     machine->regP = 
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);
764   }
765 }
766
767 static void jmpNOP(machine_6502 *machine, AddrMode adm){
768   /* no operation */
769 }
770
771 static void jmpORA(machine_6502 *machine, AddrMode adm){
772   Pointer ptr;
773   BOOL isValue = getValue(machine, adm, &ptr);
774   warnValue(isValue);
775   machine->regA |= ptr.value;
776   manZeroNeg(machine,machine->regA);
777 }
778
779 static void jmpTAX(machine_6502 *machine, AddrMode adm){
780   machine->regX = machine->regA;
781   manZeroNeg(machine,machine->regX);
782 }
783
784 static void jmpTXA(machine_6502 *machine, AddrMode adm){
785   machine->regA = machine->regX;
786   manZeroNeg(machine,machine->regA);
787 }
788
789 static void jmpDEX(machine_6502 *machine, AddrMode adm){
790   if (machine->regX > 0)
791     machine->regX--;
792   else
793     machine->regX = 0xFF;
794   manZeroNeg(machine, machine->regX);
795 }
796
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);
801 }
802
803 static void jmpTAY(machine_6502 *machine, AddrMode adm){
804   machine->regY = machine->regA;
805   manZeroNeg(machine, machine->regY);
806 }
807
808 static void jmpTYA(machine_6502 *machine, AddrMode adm){
809   machine->regA = machine->regY;
810   manZeroNeg(machine, machine->regA);
811 }
812
813 static void jmpDEY(machine_6502 *machine, AddrMode adm){
814   if (machine->regY > 0)
815     machine->regY--;
816   else
817     machine->regY = 0xFF;
818   manZeroNeg(machine, machine->regY);
819 }
820
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);
825 }
826
827 static void jmpROR(machine_6502 *machine, AddrMode adm){
828   Pointer ptr;
829   Bit8 cf;
830   BOOL isValue = getValue(machine, adm, &ptr);
831   if (isValue) { 
832     cf = bitOn(machine->regP, CARRY_FL);
833     machine->regP = 
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);
840   }
841   else { /* Implied */
842     cf = bitOn(machine->regP, CARRY_FL);
843     machine->regP = 
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);
849   }
850 }
851
852 static void jmpROL(machine_6502 *machine, AddrMode adm){
853   Pointer ptr;
854   Bit8 cf;
855   BOOL isValue = getValue(machine, adm, &ptr);
856   if (isValue) { 
857     cf = bitOn(machine->regP, CARRY_FL);
858     machine->regP = 
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);
865   }
866   else { /* Implied */
867     cf = bitOn(machine->regP, CARRY_FL);
868     machine->regP = 
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);
874   }
875 }
876
877 static void jmpRTI(machine_6502 *machine, AddrMode adm){
878   machine->regP = stackPop(machine);
879   machine->regPC = stackPop(machine);
880 }
881
882 static void jmpRTS(machine_6502 *machine, AddrMode adm){
883   Pointer ptr;
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;
889 }
890
891 static void jmpSBC(machine_6502 *machine, AddrMode adm){
892   Pointer ptr;
893   /*Bit8 vflag;*/
894   Bit8 c = bitOn(machine->regP, CARRY_FL);
895   Bit16 tmp, w;
896   BOOL isValue = getValue(machine, adm, &ptr);
897   warnValue(isValue);
898   /*vflag = (bitOn(machine->regA,NEGATIVE_FL) &&
899            bitOn(ptr.value, NEGATIVE_FL));*/
900
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);
906
907     tmp = 0xf + ar - br + c;
908     if ( tmp < 0x10){
909       w = 0;
910       tmp -= 6;
911     }
912     else {
913       w = 0x10;
914       tmp -= 0x10;
915     }
916     w += 0xf0 + al - bl;
917     if ( w < 0x100) {
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);
921       w -= 0x60;
922     }
923     else {
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);
927     }
928     w += tmp;
929   } /* end decimal mode */
930   else {
931     w = 0xff + machine->regA - ptr.value + c;
932     if ( w < 0x100 ){
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);
936     }
937     else {
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);
941     }
942   }
943   machine->regA = w;
944   manZeroNeg(machine,machine->regA);
945 }
946
947 static void jmpSTA(machine_6502 *machine, AddrMode adm){
948   Pointer ptr;
949   BOOL isValue = getValue(machine, adm, &ptr);
950   warnValue(isValue);
951   memStoreByte(machine,ptr.addr,machine->regA);
952 }
953
954 static void jmpTXS(machine_6502 *machine, AddrMode adm){
955   stackPush(machine,machine->regX);
956 }
957
958 static void jmpTSX(machine_6502 *machine, AddrMode adm){
959   machine->regX = stackPop(machine);
960   manZeroNeg(machine, machine->regX);
961 }
962
963 static void jmpPHA(machine_6502 *machine, AddrMode adm){
964   stackPush(machine, machine->regA);
965 }
966
967 static void jmpPLA(machine_6502 *machine, AddrMode adm){
968   machine->regA = stackPop(machine);
969   manZeroNeg(machine, machine->regA);
970 }
971
972 static void jmpPHP(machine_6502 *machine, AddrMode adm){
973   stackPush(machine,machine->regP);
974 }
975
976 static void jmpPLP(machine_6502 *machine, AddrMode adm){
977   machine->regP = stackPop(machine);
978   machine->regP = setBit(machine->regP, FUTURE_FL, 1);
979 }
980
981 static void jmpSTX(machine_6502 *machine, AddrMode adm){
982   Pointer ptr;
983   BOOL isValue = getValue(machine, adm, &ptr);
984   warnValue(isValue);
985   memStoreByte(machine,ptr.addr,machine->regX);
986 }
987
988 static void jmpSTY(machine_6502 *machine, AddrMode adm){
989   Pointer ptr;
990   BOOL isValue = getValue(machine, adm, &ptr);
991   warnValue(isValue);
992   memStoreByte(machine,ptr.addr,machine->regY);
993 }
994
995 \f
996
997 /* OPCODES */
998 static void assignOpCodes(Opcodes *opcodes){
999
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;}
1007
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);
1065 }
1066
1067 static void buildIndexCache(machine_6502 *machine){
1068   unsigned int i;
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;
1073     }
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;
1077     }
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;;
1081     }
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;;
1085     }
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;;
1089     }
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;;
1093     }
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;;
1097     }
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;;
1101     }
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;;
1105     }
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;
1109     }
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;
1113     }
1114   }   
1115 }
1116
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;
1124 }
1125
1126 \f
1127 /* Assembly parser */
1128
1129 static Param *newParam(void){
1130   Param *newp;
1131   int i = 0;
1132
1133   newp = (Param *) emalloc(sizeof(Param));
1134   newp->type = SINGLE;
1135   for (i = 0; i < MAX_PARAM_VALUE; i++)
1136     newp->value[i] = 0;
1137   newp->vp = 0;
1138   newp->label = ecalloc(MAX_LABEL_LEN,sizeof(char));
1139   newp->lbladdr = 0;
1140   return newp;
1141 }
1142
1143 /* Copy the fields from p2 to p1 */
1144 static void copyParam(Param *p1, Param *p2){
1145   int i = 0;
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];
1149   p1->vp = p2->vp;
1150   p1->type = p2->type;
1151 }
1152
1153 static Label *newLabel(void){
1154   Label *newp; 
1155
1156   newp = (Label *) emalloc(sizeof(Label));
1157   newp->addr = 0;
1158   newp->label = ecalloc(MAX_LABEL_LEN,sizeof(char));
1159   
1160   return newp;
1161 }
1162
1163 static AsmLine *newAsmLine(char *cmd, char *label, BOOL decl, Param *param, int lc)
1164 {
1165     AsmLine *newp;
1166
1167     newp =  (AsmLine *) emalloc(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);
1174     newp->next = NULL;
1175     return newp;
1176 }
1177
1178 static AsmLine *addend(AsmLine *listp, AsmLine *newp)
1179 {
1180     AsmLine *p;
1181     if(listp == NULL)
1182       return newp;
1183     for (p =listp; p->next != NULL; p = p->next)
1184       ;
1185     p->next = newp;
1186     return listp;
1187 }
1188
1189 static BOOL apply(AsmLine *listp, BOOL(*fn)(AsmLine*, void*), void *arg)
1190 {
1191   AsmLine *p;
1192   if(listp == NULL)
1193     return FALSE;
1194   for (p = listp; p != NULL; p = p->next)
1195     if (! fn(p,arg) )
1196       return FALSE;
1197   return TRUE;
1198 }
1199
1200 static void freeParam(Param *param){
1201   free(param->label);
1202   free(param);
1203 }
1204
1205 static void freeLabel(Label *label){
1206   free(label->label);
1207   free(label);
1208 }
1209
1210 static void freeallAsmLine(AsmLine *listp)
1211 {
1212     AsmLine *next;
1213     for(; listp != NULL; listp = next){
1214        next = listp->next;
1215        freeParam(listp->param);
1216        freeLabel(listp->label);
1217        free(listp->command);
1218        free(listp);
1219     }
1220 }
1221
1222 static BOOL addvalue(Param *param,Bit32 value){
1223   if (0 <= param->vp && param->vp < MAX_PARAM_VALUE) {
1224     param->value[param->vp++] = value;
1225     return TRUE;
1226   }
1227   else {
1228     fprintf(stderr,"Wrong number of parameters: %d. The limit is %d\n",param->vp+1, MAX_PARAM_VALUE);
1229     return FALSE;
1230   }
1231 }
1232
1233 static void parseError(char *s){
1234   fprintf(stderr,"6502 Syntax Error: %s\n", s);
1235 }
1236
1237 /* stoupper() - Destructivley modifies the string making all letters upper case*/
1238 static void stoupper(char **s){
1239   int i = 0;
1240   while((*s)[i] != '\0'){
1241     (*s)[i] = toupper((*s)[i]);
1242     i++;
1243   }
1244 }
1245  
1246 static BOOL isWhite(char c){
1247   return (c == '\r' || c == '\t' || c == ' ');
1248 }
1249
1250 static void skipSpace(char **s){
1251   for(; isWhite(**s); (*s)++)
1252     ;
1253 }
1254   
1255 /* nullify() - fills a string with upto sourceLength null characters. */
1256 static void nullify(char *token, unsigned int sourceLength){
1257   unsigned int i = 0;
1258   while (i < sourceLength)
1259     token[i++] = '\0';
1260 }
1261
1262 static BOOL isBlank(const char *token){
1263   return (token[0] == '\0');
1264 }
1265
1266 static BOOL isCommand(machine_6502 *machine, const char *token){
1267   int i = 0;
1268
1269   while (i < NUM_OPCODES) {
1270     if (strcmp(machine->opcodes[i].name,token) == 0) 
1271       return TRUE;
1272     i++;
1273   }
1274   
1275   if (strcmp(token, "DCB") == 0) return TRUE;
1276   return FALSE;
1277 }
1278
1279 /* hasChar() - Check to see if the current line has a certain
1280    charater */
1281 static BOOL hasChar(char *s, char c){
1282   for(; *s != '\0' && *s != '\n'; s++) {
1283     if (*s  == c)
1284       return TRUE;
1285   }
1286   return FALSE;
1287 }
1288
1289 static BOOL ishexdigit(char c){
1290   if (isdigit(c))
1291     return TRUE;
1292   else {
1293     char c1 = toupper(c);
1294     return ('A' <= c1 && c1 <= 'F');
1295   }
1296 }
1297
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 == '=');
1302 }
1303   
1304
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){
1308   int i = 0;
1309   skipSpace(s);
1310   for(;isCmdChar(**s) && i < MAX_CMD_LEN; (*s)++)
1311     (*cmd)[i++] = **s;
1312   if (i == 0)
1313     return TRUE; /* Could be a blank line. */
1314   else if (strcmp(*cmd,"*=") == 0)
1315     return TRUE; /* This is an entry point. */
1316   else
1317     return isCommand(machine,*cmd);
1318 }
1319
1320 static BOOL declareLabel(char **s, char **label){
1321   int i = 0;
1322   skipSpace(s);
1323   for(;**s != ':' && **s != '\n' && **s != '\0'; (*s)++){
1324     if (isWhite(**s)) 
1325       continue;
1326     (*label)[i++] = **s;
1327   }
1328   if (i == 0)
1329     return FALSE; /* Current line has to have a label */
1330   else if (**s == ':'){
1331     (*s)++; /* Skip colon */
1332     return TRUE;
1333   }
1334   else
1335     return FALSE;
1336 }
1337
1338 static BOOL parseHex(char **s, Bit32 *value){
1339   enum { MAX_HEX_LEN = 5 };
1340   if (**s == '$') {    
1341     char *hex = ecalloc(MAX_HEX_LEN, sizeof(char));
1342     int i = 0;
1343
1344     (*s)++; /* move pass $ */
1345     for(; ishexdigit(**s) && i < MAX_HEX_LEN; (*s)++)
1346       hex[i++] = **s;
1347     
1348     *value = strtol(hex,NULL,16);
1349     free(hex);  
1350     return TRUE;
1351   }
1352   else
1353     return FALSE;
1354 }
1355   
1356 static BOOL parseDec(char **s, Bit32 *value){
1357   enum { MAX_DEC_LEN = 4 };
1358   char *dec = ecalloc(MAX_DEC_LEN, sizeof(char));
1359   int i;
1360   for(i = 0; isdigit(**s) && i < MAX_DEC_LEN; (*s)++)
1361     dec[i++] = **s;
1362   
1363   if (i > 0){
1364     *value = atoi(dec);
1365     free(dec);  
1366     return TRUE;
1367   }
1368   else
1369     return FALSE;
1370 }
1371
1372 static BOOL parseValue(char **s, Bit32 *value){
1373   skipSpace(s);
1374   if (**s == '$')
1375     return parseHex(s, value);
1376   else
1377     return parseDec(s, value);
1378 }
1379
1380 static BOOL paramLabel(char **s, char **label){
1381   int i;
1382   for(i = 0; (isalnum(**s) || **s == '_') && i < MAX_LABEL_LEN; (*s)++)
1383     (*label)[i++] = **s;
1384
1385   if (i > 0)
1386     return TRUE;
1387   else
1388     return FALSE;
1389 }
1390
1391 static BOOL immediate(char **s, Param *param){
1392   if (**s != '#') 
1393     return FALSE;
1394
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);
1403       free(label);
1404       return TRUE;
1405     }    
1406     free(label);
1407   }
1408   else {
1409     Bit32 value;
1410     if (parseValue(s, &value)){
1411       if (value > 0xFF){
1412         parseError("Immediate value is too large.");
1413         return FALSE;
1414       }
1415       param->type = IMMEDIATE_VALUE;
1416       return addvalue(param, value);
1417     }
1418   }
1419   return FALSE;
1420 }
1421
1422 static BOOL isDirection(char c){
1423   return (c == 'X' || c == 'Y');
1424 }
1425
1426 static BOOL getDirection(char **s, char *direction){
1427   skipSpace(s);
1428   if (**s == ','){
1429     (*s)++;
1430     skipSpace(s);
1431     if (isDirection(**s)){
1432       *direction = **s;
1433       (*s)++;
1434       return TRUE;
1435     }
1436   }
1437   return FALSE;
1438 }
1439   
1440 static BOOL indirect(char **s, Param *param){
1441   Bit32 value;
1442   char c;
1443   if (**s == '(') 
1444     (*s)++;
1445   else
1446     return FALSE;
1447   
1448   if (! parseHex(s,&value)) 
1449     return FALSE;
1450   if (value > 0xFF) {
1451     parseError("Indirect value is too large.");
1452     return FALSE;
1453   }
1454   if (!addvalue(param, value))
1455     return FALSE;
1456   skipSpace(s);
1457   if (**s == ')'){
1458     (*s)++;
1459     if (getDirection(s,&c)) {
1460       if (c == 'Y'){
1461         param->type = INDIRECT_Y;
1462         return TRUE;
1463       }
1464     }
1465   }
1466   else if (getDirection(s, &c)){
1467     if (c == 'X'){
1468       skipSpace(s);
1469       if (**s == ')'){
1470         (*s)++;
1471         param->type = INDIRECT_X;
1472         return TRUE;
1473       }
1474     }
1475   }
1476   return FALSE;
1477 }
1478
1479 static BOOL dcbValue(char **s, Param *param){
1480   Bit32 val;
1481   if (! parseValue(s,&val))
1482     return FALSE;
1483
1484   if (val > 0xFF) 
1485     return FALSE;
1486                     
1487   if (!addvalue(param,val))
1488     return FALSE;
1489
1490   param->type = DCB_PARAM;
1491
1492   skipSpace(s);
1493   if(**s == ','){
1494     (*s)++;
1495     return dcbValue(s, param);
1496   }
1497   else
1498     return TRUE;
1499
1500
1501 static BOOL value(char **s, Param *param){
1502   Bit32 val;
1503   BOOL abs;
1504   BOOL dir;
1505   char c = '\0';
1506   if (! parseValue(s,&val))
1507     return FALSE;
1508
1509   abs = (val > 0xFF);
1510   dir = getDirection(s,&c);
1511   if (!addvalue(param,val))
1512     return FALSE;
1513
1514   if(abs && dir){
1515     if (c == 'X')
1516       param->type = ABS_X;
1517     else if (c == 'Y')
1518       param->type = ABS_Y;
1519     else
1520       return FALSE;
1521   }
1522   else if (abs)
1523     param->type = ABS_VALUE;
1524   else if (dir){
1525     if (c == 'X')
1526       param->type = ZERO_X;
1527     else if (c == 'Y')
1528       param->type = ZERO_Y;
1529     else
1530       return FALSE;
1531   }
1532   else
1533     param->type = ZERO;
1534
1535   return TRUE;
1536 }
1537
1538 static BOOL label(char **s, Param *param){
1539   char *label = ecalloc(MAX_LABEL_LEN, sizeof(char));
1540   char c;
1541   BOOL labelOk = FALSE;
1542   if (paramLabel(s, &label)){
1543     labelOk = TRUE;
1544     param->type = ABS_OR_BRANCH;
1545     if (getDirection(s, &c)){
1546       if (c == 'X')
1547         param->type = ABS_LABEL_X;
1548       else if (c == 'Y')
1549         param->type = ABS_LABEL_Y;
1550       else
1551         labelOk = FALSE;
1552     }
1553     strncpy(param->label,label,MAX_LABEL_LEN);
1554   }
1555   free(label);
1556   return labelOk;
1557 }
1558
1559 static BOOL parameter(const char *cmd, char **s, Param *param){
1560   skipSpace(s);
1561   if (**s == '\0' || **s == '\n')
1562     return TRUE;
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);
1570     else
1571       return value(s,param);
1572   }
1573   else if (isalpha(**s))
1574     return label(s ,param);
1575   else
1576     return FALSE; /* Invalid Parameter */
1577 }
1578
1579 static void comment(char **s){
1580   skipSpace(s);
1581   if (**s == ';')
1582     for(;**s != '\n' && **s != '\0'; (*s)++)
1583       ;
1584 }
1585
1586 static void initParam(Param *param){
1587   int i;
1588   param->type = SINGLE;
1589   for(i = 0; i < MAX_PARAM_VALUE; i++)
1590     param->value[i] = 0;
1591   param->vp = 0;
1592   nullify(param->label,MAX_LABEL_LEN);
1593 }
1594   
1595
1596 static AsmLine *parseAssembly(machine_6502 *machine, BOOL *codeOk, const char *code){
1597   char *s;
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;
1602   Param *param;
1603   BOOL decl;
1604   AsmLine *listp = NULL;
1605
1606   *codeOk = TRUE;
1607   param = newParam();
1608   s = estrdup(code);
1609   start = s;
1610   stoupper(&s);
1611
1612   while(*s != '\0' && *codeOk){
1613     initParam(param);
1614     nullify(cmd, MAX_CMD_LEN);
1615     nullify(label, MAX_LABEL_LEN);
1616     decl = FALSE;
1617     skipSpace(&s);
1618     comment(&s);
1619     if (*s == '\n'){
1620       lc++;
1621       s++;
1622       continue; /* blank line */
1623     }
1624     else if (*s == '\0')
1625       continue; /* no newline at the end of the code */
1626     else if (hasChar(s,':')){
1627       decl = TRUE;
1628       if(! declareLabel(&s,&label)){
1629         *codeOk = FALSE;
1630         break;
1631       }
1632       skipSpace(&s);
1633     }
1634     if(!command(machine, &s, &cmd)){
1635       *codeOk = FALSE;
1636       break;
1637     }
1638     skipSpace(&s);
1639     comment(&s);
1640     if(!parameter(cmd, &s, param)){
1641       *codeOk = FALSE;
1642       break;
1643     }
1644     skipSpace(&s);
1645     comment(&s);
1646     if (*s == '\n' || *s == '\0'){
1647       AsmLine *asmm;
1648       asmm = newAsmLine(cmd,label,decl,param,lc);
1649       listp = addend(listp,asmm);
1650     }
1651     else {
1652       *codeOk = FALSE;
1653       break;
1654     }
1655   }
1656   if (! *codeOk)
1657     fprintf(stderr,"Syntax error at line %u\n", lc);
1658   free(start);
1659   free(cmd);
1660   free(label);
1661   freeParam(param);
1662   return listp;
1663 }
1664     
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;
1668   FILE *ifp;
1669   int c;
1670   int size = defaultSize;
1671   int i = 0;
1672   char *buffer = ecalloc(defaultSize,sizeof(char));
1673
1674   if (!buffer) abort();
1675
1676   ifp = fopen(filename, "rb");
1677   if (!ifp) return 0;
1678
1679   while((c = getc(ifp)) != EOF){
1680     buffer[i++] = c;
1681     if (i == size){
1682       size += defaultSize;
1683       buffer = realloc(buffer, size);
1684       if (buffer == NULL) {
1685         abort();
1686       }
1687     }
1688   }
1689   fclose(ifp);
1690   buffer = realloc(buffer, i+2);
1691   if (!buffer) abort();
1692   /* Make sure we have a line feed at the end */
1693   buffer[i] = '\n';
1694   buffer[i+1] = '\0';
1695   return buffer;
1696 }
1697
1698 \f
1699 /* Routines */
1700
1701 /* reset() - Reset CPU and memory. */
1702 static void reset(machine_6502 *machine){
1703   int x, y;
1704   for ( y = 0; y < 32; y++ ){
1705     for (x = 0; x < 32; x++){
1706       machine->screen[x][y] = 0;
1707     }
1708   }
1709
1710   for(x=0; x < MEM_64K; x++)
1711     machine->memory[x] = 0;
1712
1713   machine->codeCompiledOK = FALSE;
1714   machine->regA = 0;
1715   machine->regX = 0;
1716   machine->regY = 0;
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;
1723 }
1724
1725 /* hexDump() - Dump the memory to output */
1726 void hexDump(machine_6502 *machine, Bit16 start, Bit16 numbytes, FILE *output){
1727   Bit32 address;
1728   Bit32 i;
1729   for( i = 0; i < numbytes; i++){
1730     address = start + i;
1731     if ( (i&15) == 0 ) {
1732       fprintf(output,"\n%.4x: ", address);
1733     }
1734     fprintf(output,"%.2x%s",machine->memory[address], (i & 1) ? " ":"");
1735   }
1736   fprintf(output,"%s\n",(i&1)?"--":"");
1737 }
1738
1739 /* XXX */
1740 /* void save_program(machine_6502 *machine, char *filename){ */
1741 /*   FILE *ofp; */
1742 /*   Bit16 pc = PROG_START; */
1743 /*   Bit16 end = pc + machine->codeLen; */
1744 /*   Bit16 n; */
1745 /*   ofp = fopen(filename, "w"); */
1746 /*   if (!ofp) abort(); */
1747   
1748 /*   fprintf(ofp,"Bit8 prog[%d] =\n{",machine->codeLen); */
1749 /*   n = 1; */
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"); */
1754   
1755 /*   fclose(ofp); */
1756 /* } */
1757
1758 static BOOL translate(Opcodes *op,Param *param, machine_6502 *machine){
1759    switch(param->type){
1760     case SINGLE:
1761       if (op->SNGL)
1762         pushByte(machine, op->SNGL);
1763       else {
1764         fprintf(stderr,"%s needs a parameter.\n",op->name);
1765         return FALSE;
1766       }
1767       break;
1768     case IMMEDIATE_VALUE:
1769       if (op->Imm) {
1770         pushByte(machine, op->Imm);
1771         pushByte(machine, param->value[0]);
1772         break;
1773       }
1774       else {
1775         fprintf(stderr,"%s does not take IMMEDIATE_VALUE parameters.\n",op->name);
1776         return FALSE;
1777       }
1778     case IMMEDIATE_GREAT:
1779       if (op->Imm) {
1780         pushByte(machine, op->Imm);
1781         pushByte(machine, param->lbladdr >> 8);
1782         break;
1783       }
1784       else {
1785         fprintf(stderr,"%s does not take IMMEDIATE_GREAT parameters.\n",op->name);
1786         return FALSE;
1787       }
1788     case IMMEDIATE_LESS:
1789       if (op->Imm) {
1790         pushByte(machine, op->Imm);
1791         pushByte(machine, param->lbladdr & 0xFF);
1792         break;
1793       }
1794       else {
1795         fprintf(stderr,"%s does not take IMMEDIATE_LESS parameters.\n",op->name);
1796         return FALSE;
1797       }
1798     case INDIRECT_X:
1799       if (op->INDX) {
1800         pushByte(machine, op->INDX);
1801         pushByte(machine, param->value[0]);
1802         break;
1803       }
1804       else {
1805         fprintf(stderr,"%s does not take INDIRECT_X parameters.\n",op->name);
1806         return FALSE;
1807       }
1808     case INDIRECT_Y:
1809       if (op->INDY) {
1810         pushByte(machine, op->INDY);
1811         pushByte(machine, param->value[0]);
1812         break;
1813       }
1814       else {
1815         fprintf(stderr,"%s does not take INDIRECT_Y parameters.\n",op->name);
1816         return FALSE;
1817       }
1818     case ZERO:
1819       if (op->ZP) {
1820         pushByte(machine, op->ZP);
1821         pushByte(machine, param->value[0]);
1822         break;
1823       }
1824       else {
1825         fprintf(stderr,"%s does not take ZERO parameters.\n",op->name);
1826         return FALSE;
1827       }
1828     case ZERO_X:
1829       if (op->ZPX) {
1830         pushByte(machine, op->ZPX);
1831         pushByte(machine, param->value[0]);
1832         break;
1833       }
1834       else {
1835         fprintf(stderr,"%s does not take ZERO_X parameters.\n",op->name);
1836         return FALSE;
1837       }
1838     case ZERO_Y:
1839       if (op->ZPY) {
1840         pushByte(machine, op->ZPY);
1841         pushByte(machine, param->value[0]);
1842         break;
1843       }
1844       else {
1845         fprintf(stderr,"%s does not take ZERO_Y parameters.\n",op->name);
1846         return FALSE;
1847       }
1848     case ABS_VALUE:
1849       if (op->ABS) {
1850         pushByte(machine, op->ABS);
1851         pushWord(machine, param->value[0]);
1852         break;
1853       }
1854       else {
1855         fprintf(stderr,"%s does not take ABS_VALUE parameters.\n",op->name);
1856         return FALSE;
1857       }
1858     case ABS_OR_BRANCH:
1859       if (op->ABS > 0){
1860         pushByte(machine, op->ABS);
1861         pushWord(machine, param->lbladdr);
1862       }
1863       else {
1864         if (op->BRA) {
1865           pushByte(machine, op->BRA);
1866           {
1867             int diff = abs(param->lbladdr - machine->defaultCodePC);
1868             int backward = (param->lbladdr < machine->defaultCodePC);
1869             pushByte(machine, (backward) ? 0xff - diff : diff - 1);
1870           }
1871         }
1872         else {
1873           fprintf(stderr,"%s does not take BRANCH parameters.\n",op->name);
1874           return FALSE;
1875         }
1876       }
1877       break;
1878     case ABS_X:
1879       if (op->ABSX) {
1880         pushByte(machine, op->ABSX);
1881         pushWord(machine, param->value[0]);
1882         break;
1883       }
1884       else {
1885         fprintf(stderr,"%s does not take ABS_X parameters.\n",op->name);
1886         return FALSE;
1887       }
1888     case ABS_Y:
1889       if (op->ABSY) {
1890         pushByte(machine, op->ABSY);
1891         pushWord(machine, param->value[0]);
1892         break;
1893       }
1894       else {
1895         fprintf(stderr,"%s does not take ABS_Y parameters.\n",op->name);
1896         return FALSE;
1897       }
1898     case ABS_LABEL_X:
1899       if (op->ABSX) {
1900         pushByte(machine, op->ABSX);
1901         pushWord(machine, param->lbladdr);
1902         break;
1903       }
1904       else {
1905         fprintf(stderr,"%s does not take ABS_LABEL_X parameters.\n",op->name);
1906         return FALSE;
1907       }
1908     case ABS_LABEL_Y:
1909       if (op->ABSY) {
1910         pushByte(machine, op->ABSY);
1911         pushWord(machine, param->lbladdr);
1912         break;
1913       }
1914       else {
1915         fprintf(stderr,"%s does not take ABS_LABEL_Y parameters.\n",op->name);
1916         return FALSE;
1917       }
1918    case DCB_PARAM:
1919      /* Handled elsewhere */
1920      break;
1921    }
1922    return TRUE;
1923 }
1924
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;
1929   machine = args;
1930   if (isBlank(asmline->command)) return TRUE;
1931   if (strcmp("*=",asmline->command) == 0){
1932     machine->defaultCodePC = asmline->param->value[0];
1933   }
1934   else if (strcmp("DCB",asmline->command) == 0){
1935     int i;
1936     for(i = 0; i < asmline->param->vp; i++)
1937       pushByte(machine, asmline->param->value[i]);
1938   }    
1939   else{
1940     int i;
1941     char *command = asmline->command;
1942     Opcodes op;
1943     for(i = 0; i < NUM_OPCODES; i++){
1944       if (strcmp(machine->opcodes[i].name, command) == 0){
1945         op = machine->opcodes[i];
1946         break;      
1947       }
1948     }
1949     if (i == NUM_OPCODES)
1950       return FALSE; /* unknow upcode */
1951     else
1952       return translate(&op,asmline->param,machine);
1953   }
1954   return TRUE;
1955 }
1956
1957 /* indexLabels() - Get the address for each label */
1958 static BOOL indexLabels(AsmLine *asmline, void *arg){
1959   machine_6502 *machine; 
1960   int thisPC;
1961   Bit16 oldDefault;
1962   machine = arg;
1963   oldDefault = machine->defaultCodePC;
1964   thisPC = machine->regPC;
1965   /* Figure out how many bytes this instruction takes */
1966   machine->codeLen = 0;
1967
1968   if ( ! compileLine(asmline, machine) ){
1969     return FALSE;
1970   }
1971
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;
1977   }
1978   else {
1979     machine->regPC = machine->defaultCodePC;
1980     /*oldDefault = machine->defaultCodePC;*/
1981   }
1982
1983   if (asmline->labelDecl) {
1984     asmline->label->addr = thisPC;
1985   }
1986    return TRUE; 
1987 }
1988
1989 static BOOL changeParamLabelAddr(AsmLine *asmline, void *label){
1990   Label *la = label;
1991   if (strcmp(asmline->param->label, la->label) == 0)
1992     asmline->param->lbladdr = la->addr;
1993   return TRUE;
1994 }
1995
1996 static BOOL linkit(AsmLine *asmline, void *asmlist){
1997   apply(asmlist,changeParamLabelAddr,asmline->label);
1998   return TRUE;
1999 }
2000
2001 /* linkLabels - Make sure all of the references to the labels contain
2002    the right address*/
2003 static void linkLabels(AsmLine *asmlist){
2004   apply(asmlist,linkit,asmlist);
2005 }
2006
2007 /* compileCode() - Compile the current assembly code for the machine */
2008 static BOOL compileCode(machine_6502 *machine, const char *code){
2009   BOOL codeOk;
2010   AsmLine *asmlist;
2011
2012   reset(machine);
2013   machine->defaultCodePC = machine->regPC = PROG_START;
2014   asmlist = parseAssembly(machine, &codeOk, code);
2015
2016   if(codeOk){
2017     /* First pass: Find the addresses for the labels */
2018     if (!apply(asmlist, indexLabels, machine))
2019       return FALSE;
2020     /* update label references */
2021     linkLabels(asmlist);
2022
2023 #if 0 /* prints out some debugging information */
2024     {
2025       AsmLine *p;
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);
2031             }
2032     }
2033
2034 #endif    
2035
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 
2040        correct spot. */
2041     machine->defaultCodePC = PROG_START;
2042     if (!apply(asmlist, compileLine, machine))
2043       return FALSE;
2044
2045     if (machine->defaultCodePC > PROG_START ){
2046       machine->memory[machine->defaultCodePC] = 0x00;
2047       codeOk = TRUE;
2048     }
2049     else{
2050       fprintf(stderr,"No Code to run.\n");
2051       codeOk = FALSE;
2052     }
2053   }
2054   else{
2055     fprintf(stderr,"An error occured while parsing the file.\n");  
2056     codeOk = FALSE;
2057   }
2058   freeallAsmLine(asmlist);
2059   return codeOk;
2060 }
2061
2062
2063 /*
2064  *  execute() - Executes one instruction.
2065  *              This is the main part of the CPU emulator.
2066  *
2067  */
2068
2069 static void execute(machine_6502 *machine){
2070   Bit8 opcode;
2071   AddrMode adm;
2072   int opidx;
2073
2074   if(!machine->codeRunning) return;
2075
2076   opcode = popByte(machine);
2077   if (opcode == 0x00)
2078     machine->codeRunning = FALSE;
2079   else {
2080     opidx = opIndex(machine,opcode,&adm);
2081     if(opidx > -1)
2082       machine->opcodes[opidx].func(machine, adm);
2083     else
2084       fprintf(stderr,"Invalid opcode!\n");
2085   }
2086   if( (machine->regPC == 0) || 
2087       (!machine->codeRunning) ) {
2088     machine->codeRunning = FALSE;
2089   }
2090 }
2091
2092 machine_6502 *build6502(){
2093   machine_6502 *machine;
2094   machine = emalloc(sizeof(machine_6502));
2095   assignOpCodes(machine->opcodes);
2096   buildIndexCache(machine);
2097   reset(machine);
2098   return machine;
2099 }
2100
2101 void destroy6502(machine_6502 *machine){
2102   free(machine);
2103   machine = NULL;
2104 }
2105
2106 void trace(machine_6502 *machine, FILE *output){
2107   Bit8 opcode = memReadByte(machine,machine->regPC);
2108   AddrMode adm;
2109   Pointer ptr;
2110   int opidx = opIndex(machine,opcode,&adm);
2111   int stacksz = STACK_TOP - machine->regSP;
2112
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);
2124   if (opidx > -1){
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);
2130     else
2131       fprintf(output,"\n");
2132   }
2133   fprintf(output,"STACK:");
2134   hexDump(machine,(STACK_TOP - stacksz) + 1, stacksz, output);
2135 }
2136
2137 void disassemble(machine_6502 *machine, FILE *output){
2138   /* Read the opcode
2139      increment the program counter
2140      print the opcode
2141      loop until end of program. */
2142   AddrMode adm;
2143   Bit16 addr;
2144   Bit8 opcode;
2145   int opidx;
2146   char *mem;
2147   int i;
2148   Bit16 opc = machine->regPC;
2149   mem = calloc(20,sizeof(char));
2150   machine->regPC = PROG_START;
2151   do{
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 */
2160   free(mem);
2161   machine->regPC = opc;
2162 }
2163
2164 \f
2165 void eval_file(machine_6502 *machine, const char *filename, Plotter plot, void *plotterState){
2166   char *code = NULL;
2167
2168   machine->plot = plot;
2169   machine->plotterState = plotterState;
2170
2171   code = fileToBuffer(filename);
2172   
2173   if (! compileCode(machine, code) ) abort();
2174
2175   free(code);
2176
2177   machine->defaultCodePC = machine->regPC = PROG_START;
2178   machine->codeRunning = TRUE;
2179   do{
2180     sleep(0); /* XXX */
2181 #if 0
2182     trace(machine);
2183 #endif
2184     execute(machine);
2185   }while(machine->codeRunning);
2186 }
2187
2188 void start_eval_file(machine_6502 *machine, const char *filename, Plotter plot, void *plotterState){
2189   char *code = NULL;
2190   reset(machine);
2191
2192   machine->plot = plot;
2193   machine->plotterState = plotterState;
2194
2195   code = fileToBuffer(filename);
2196   
2197   if (! compileCode(machine, code) ) abort();
2198
2199   free(code);
2200
2201   machine->defaultCodePC = machine->regPC = PROG_START;
2202   machine->codeRunning = TRUE;
2203   execute(machine);
2204 }
2205
2206 void start_eval_string(machine_6502 *machine, const char *code,
2207                        Plotter plot, void *plotterState){
2208   reset(machine);
2209
2210   machine->plot = plot;
2211   machine->plotterState = plotterState;
2212
2213   if (! compileCode(machine, code) ){
2214     fprintf(stderr,"Could not compile code.\n");
2215   }
2216
2217   machine->defaultCodePC = machine->regPC = PROG_START;
2218   machine->codeRunning = TRUE;
2219   execute(machine);
2220 }
2221
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; */
2229
2230 /*   machine->regPC = PROG_START; */
2231 /*   pc = machine->regPC; */
2232 /*   machine->codeLen = proglen; */
2233 /*   n = 0; */
2234 /*   while (n < proglen){ */
2235 /*     machine->memory[pc++] = program[n++]; */
2236 /*   } */
2237 /*   machine->codeRunning = TRUE; */
2238 /*   execute(machine); */
2239 /* } */
2240
2241 void next_eval(machine_6502 *machine, int insno){
2242   int i = 0;
2243   for (i = 1; i < insno; i++){
2244     if (machine->codeRunning){
2245 #if 0
2246       trace(machine, stdout);
2247 #endif
2248       execute(machine);
2249     }
2250     else
2251       break;
2252   }
2253 }
2254