From http://www.jwz.org/xscreensaver/xscreensaver-5.35.tar.gz
[xscreensaver] / hacks / asm6502.c
index af0a9341b7b94e312032dde29217260414743e89..2228a38e4052b93e31d28a7f3077b2a0c7bed65e 100644 (file)
@@ -20,8 +20,6 @@
       I changed the structure of the assembler in this version.
 */
 
-#define NDEBUG  /* Uncomment when done with debugging */
-
 #include <stdlib.h>
 #include <stdio.h>
 /*#include <malloc.h>*/
 #include <assert.h>
 #include <ctype.h>
 #include <math.h>
-#include <stdint.h>
+#include <inttypes.h>
 #include <unistd.h>
 
+#include "yarandom.h"
 #include "asm6502.h"
 
-#ifdef DEBUGGER
+/*#ifdef DEBUGGER
 #  define random rand
+#endif*/
+
+#ifndef HAVE_MOBILE
+# define READ_FILES
 #endif
 
 typedef enum{
@@ -68,7 +71,7 @@ typedef BOOL (*CharTest) (char);
 /*typedef void (*JumpFunc) (machine_6502* AddrMode);*/
 
 typedef struct {
-  AddrMode type;
+  m6502_AddrMode type;
   Bit32 value[MAX_PARAM_VALUE];
   unsigned int vp; /*value pointer, index into the value table.*/
   char *label;
@@ -95,11 +98,11 @@ typedef struct {
 } Pointer;
 
 
-static void *emalloc(size_t n) {
+/*static void *emalloc(size_t n) {
   void *p = malloc(n);
   if (! p) abort();
   return p;
-}
+}*/
 
 static void *ecalloc(uint32_t nelm, size_t nsize){
   void *p = calloc(nelm, nsize);
@@ -259,7 +262,7 @@ static Bit8 nibble(Bit8 value, Side side){
 
 
 /* used for tracing. XXX: combined with function getvalue */
-static BOOL peekValue(machine_6502 *machine, AddrMode adm, Pointer *pointer, Bit16 PC){
+static BOOL peekValue(machine_6502 *machine, m6502_AddrMode adm, Pointer *pointer, Bit16 PC){
   Bit8 zp;
   pointer->value = 0;
   pointer->addr = 0;
@@ -324,7 +327,7 @@ static BOOL peekValue(machine_6502 *machine, AddrMode adm, Pointer *pointer, Bit
 
 
 /* Figure out how to get the value from the addrmode and get it.*/
-static BOOL getValue(machine_6502 *machine, AddrMode adm, Pointer *pointer){
+static BOOL getValue(machine_6502 *machine, m6502_AddrMode adm, Pointer *pointer){
   Bit8 zp;
   pointer->value = 0;
   pointer->addr = 0;
@@ -385,7 +388,8 @@ static BOOL getValue(machine_6502 *machine, AddrMode adm, Pointer *pointer){
 
 }
 
-static void dismem(machine_6502 *machine, AddrMode adm, char *output){
+#if 0
+static void dismem(machine_6502 *machine, m6502_AddrMode adm, char *output){
   Bit8 zp;
   Bit16 n;
   switch(adm){
@@ -442,6 +446,7 @@ static void dismem(machine_6502 *machine, AddrMode adm, char *output){
     break;
   }
 }
+#endif
 
 /* manZeroNeg - Manage the negative and zero flags */
 static void manZeroNeg(machine_6502 *machine, Bit8 value){
@@ -455,7 +460,7 @@ static void warnValue(BOOL isValue){
   }
 }
 
-static void jmpADC(machine_6502 *machine, AddrMode adm){
+static void jmpADC(machine_6502 *machine, m6502_AddrMode adm){
   Pointer ptr;
   Bit16 tmp;
   Bit8 c = bitOn(machine->regP, CARRY_FL);
@@ -506,7 +511,7 @@ static void jmpADC(machine_6502 *machine, AddrMode adm){
   manZeroNeg(machine,machine->regA);
 }
 
-static void jmpAND(machine_6502 *machine, AddrMode adm){
+static void jmpAND(machine_6502 *machine, m6502_AddrMode adm){
   Pointer ptr;
   BOOL isValue = getValue(machine, adm, &ptr);
   warnValue(isValue);
@@ -514,7 +519,7 @@ static void jmpAND(machine_6502 *machine, AddrMode adm){
   manZeroNeg(machine,machine->regA);
 }
 
-static void jmpASL(machine_6502 *machine, AddrMode adm){
+static void jmpASL(machine_6502 *machine, m6502_AddrMode adm){
   Pointer ptr;
   BOOL isValue = getValue(machine, adm, &ptr);
   if (isValue){
@@ -533,7 +538,7 @@ static void jmpASL(machine_6502 *machine, AddrMode adm){
   
 }
 
-static void jmpBIT(machine_6502 *machine, AddrMode adm){
+static void jmpBIT(machine_6502 *machine, m6502_AddrMode adm){
   Pointer ptr;
   BOOL isValue = getValue(machine, adm, &ptr);
   warnValue(isValue);
@@ -550,7 +555,7 @@ static void jumpBranch(machine_6502 *machine, Bit16 offset){
     machine->regPC = machine->regPC + offset;
 }
 
-static void jmpBPL(machine_6502 *machine, AddrMode adm){
+static void jmpBPL(machine_6502 *machine, m6502_AddrMode adm){
   Pointer ptr;
   BOOL isValue = getValue(machine, adm, &ptr);
   warnValue(isValue);
@@ -559,7 +564,7 @@ static void jmpBPL(machine_6502 *machine, AddrMode adm){
     
 }
 
-static void jmpBMI(machine_6502 *machine, AddrMode adm){
+static void jmpBMI(machine_6502 *machine, m6502_AddrMode adm){
   Pointer ptr;
   BOOL isValue = getValue(machine, adm, &ptr);
   warnValue(isValue);
@@ -568,7 +573,7 @@ static void jmpBMI(machine_6502 *machine, AddrMode adm){
 
 }
 
-static void jmpBVC(machine_6502 *machine, AddrMode adm){
+static void jmpBVC(machine_6502 *machine, m6502_AddrMode adm){
   Pointer ptr;
   BOOL isValue = getValue(machine, adm, &ptr);
   warnValue(isValue);
@@ -576,7 +581,7 @@ static void jmpBVC(machine_6502 *machine, AddrMode adm){
     jumpBranch(machine, ptr.addr);
 }
 
-static void jmpBVS(machine_6502 *machine, AddrMode adm){
+static void jmpBVS(machine_6502 *machine, m6502_AddrMode adm){
   Pointer ptr;
   BOOL isValue = getValue(machine, adm, &ptr);
   warnValue(isValue);
@@ -584,7 +589,7 @@ static void jmpBVS(machine_6502 *machine, AddrMode adm){
     jumpBranch(machine, ptr.addr);
 }
 
-static void jmpBCC(machine_6502 *machine, AddrMode adm){
+static void jmpBCC(machine_6502 *machine, m6502_AddrMode adm){
   Pointer ptr;
   BOOL isValue = getValue(machine, adm, &ptr);
   warnValue(isValue);
@@ -592,7 +597,7 @@ static void jmpBCC(machine_6502 *machine, AddrMode adm){
     jumpBranch(machine, ptr.addr);
 }
 
-static void jmpBCS(machine_6502 *machine, AddrMode adm){
+static void jmpBCS(machine_6502 *machine, m6502_AddrMode adm){
   Pointer ptr;
   BOOL isValue = getValue(machine, adm, &ptr);
   warnValue(isValue);
@@ -600,7 +605,7 @@ static void jmpBCS(machine_6502 *machine, AddrMode adm){
     jumpBranch(machine, ptr.addr);
 }
 
-static void jmpBNE(machine_6502 *machine, AddrMode adm){
+static void jmpBNE(machine_6502 *machine, m6502_AddrMode adm){
   Pointer ptr;
   BOOL isValue = getValue(machine, adm, &ptr);
   warnValue(isValue);
@@ -608,7 +613,7 @@ static void jmpBNE(machine_6502 *machine, AddrMode adm){
     jumpBranch(machine, ptr.addr);
 }
 
-static void jmpBEQ(machine_6502 *machine, AddrMode adm){
+static void jmpBEQ(machine_6502 *machine, m6502_AddrMode adm){
   Pointer ptr;
   BOOL isValue = getValue(machine, adm, &ptr);
   warnValue(isValue);
@@ -621,28 +626,28 @@ static void doCompare(machine_6502 *machine, Bit16 reg, Pointer *ptr){
   manZeroNeg(machine,(reg - ptr->value));
 }
 
-static void jmpCMP(machine_6502 *machine, AddrMode adm){
+static void jmpCMP(machine_6502 *machine, m6502_AddrMode adm){
   Pointer ptr;
   BOOL isValue = getValue(machine, adm, &ptr);
   warnValue(isValue);
   doCompare(machine,machine->regA,&ptr);
 }
 
-static void jmpCPX(machine_6502 *machine, AddrMode adm){
+static void jmpCPX(machine_6502 *machine, m6502_AddrMode adm){
   Pointer ptr;
   BOOL isValue = getValue(machine, adm, &ptr);
   warnValue(isValue);
   doCompare(machine,machine->regX,&ptr);
 }
 
-static void jmpCPY(machine_6502 *machine, AddrMode adm){
+static void jmpCPY(machine_6502 *machine, m6502_AddrMode adm){
   Pointer ptr;
   BOOL isValue = getValue(machine, adm, &ptr);
   warnValue(isValue);
   doCompare(machine,machine->regY,&ptr);
 }
 
-static void jmpDEC(machine_6502 *machine, AddrMode adm){
+static void jmpDEC(machine_6502 *machine, m6502_AddrMode adm){
   Pointer ptr;
   BOOL isValue = getValue(machine, adm, &ptr);
   warnValue(isValue);
@@ -654,7 +659,7 @@ static void jmpDEC(machine_6502 *machine, AddrMode adm){
   manZeroNeg(machine,ptr.value);
 }
 
-static void jmpEOR(machine_6502 *machine, AddrMode adm){
+static void jmpEOR(machine_6502 *machine, m6502_AddrMode adm){
   Pointer ptr;
   BOOL isValue = getValue(machine, adm, &ptr);
   warnValue(isValue);
@@ -662,35 +667,35 @@ static void jmpEOR(machine_6502 *machine, AddrMode adm){
   manZeroNeg(machine, machine->regA);
 }
 
-static void jmpCLC(machine_6502 *machine, AddrMode adm){
+static void jmpCLC(machine_6502 *machine, m6502_AddrMode adm){
   machine->regP = setBit(machine->regP, CARRY_FL, 0);
 }
 
-static void jmpSEC(machine_6502 *machine, AddrMode adm){
+static void jmpSEC(machine_6502 *machine, m6502_AddrMode adm){
   machine->regP = setBit(machine->regP, CARRY_FL, 1);
 }
 
-static void jmpCLI(machine_6502 *machine, AddrMode adm){
+static void jmpCLI(machine_6502 *machine, m6502_AddrMode adm){
   machine->regP = setBit(machine->regP, INTERRUPT_FL, 0);
 }
 
-static void jmpSEI(machine_6502 *machine, AddrMode adm){
+static void jmpSEI(machine_6502 *machine, m6502_AddrMode adm){
   machine->regP = setBit(machine->regP, INTERRUPT_FL, 1);
 }
 
-static void jmpCLV(machine_6502 *machine, AddrMode adm){
+static void jmpCLV(machine_6502 *machine, m6502_AddrMode adm){
   machine->regP = setBit(machine->regP, OVERFLOW_FL, 0);
 }
 
-static void jmpCLD(machine_6502 *machine, AddrMode adm){
+static void jmpCLD(machine_6502 *machine, m6502_AddrMode adm){
   machine->regP = setBit(machine->regP, DECIMAL_FL, 0);
 }
 
-static void jmpSED(machine_6502 *machine, AddrMode adm){
+static void jmpSED(machine_6502 *machine, m6502_AddrMode adm){
   machine->regP = setBit(machine->regP, DECIMAL_FL, 1);
 }
 
-static void jmpINC(machine_6502 *machine, AddrMode adm){
+static void jmpINC(machine_6502 *machine, m6502_AddrMode adm){
   Pointer ptr;
   BOOL isValue = getValue(machine, adm, &ptr);
   warnValue(isValue);
@@ -699,14 +704,14 @@ static void jmpINC(machine_6502 *machine, AddrMode adm){
   manZeroNeg(machine,ptr.value);
 }
 
-static void jmpJMP(machine_6502 *machine, AddrMode adm){
+static void jmpJMP(machine_6502 *machine, m6502_AddrMode adm){
   Pointer ptr;
   BOOL isValue = getValue(machine, adm, &ptr);
   warnValue(isValue);
   machine->regPC = ptr.addr;
 }
 
-static void jmpJSR(machine_6502 *machine, AddrMode adm){
+static void jmpJSR(machine_6502 *machine, m6502_AddrMode adm){
   Pointer ptr;
   /* Move past the 2 byte parameter. JSR is always followed by
      absolute address. */
@@ -718,7 +723,7 @@ static void jmpJSR(machine_6502 *machine, AddrMode adm){
   machine->regPC = ptr.addr;  
 }
 
-static void jmpLDA(machine_6502 *machine, AddrMode adm){
+static void jmpLDA(machine_6502 *machine, m6502_AddrMode adm){
   Pointer ptr;
   BOOL isValue = getValue(machine, adm, &ptr);
   warnValue(isValue);
@@ -726,7 +731,7 @@ static void jmpLDA(machine_6502 *machine, AddrMode adm){
   manZeroNeg(machine, machine->regA);
 }
 
-static void jmpLDX(machine_6502 *machine, AddrMode adm){
+static void jmpLDX(machine_6502 *machine, m6502_AddrMode adm){
   Pointer ptr;
   BOOL isValue = getValue(machine, adm, &ptr);
   warnValue(isValue);
@@ -734,7 +739,7 @@ static void jmpLDX(machine_6502 *machine, AddrMode adm){
   manZeroNeg(machine, machine->regX);
 }
 
-static void jmpLDY(machine_6502 *machine, AddrMode adm){
+static void jmpLDY(machine_6502 *machine, m6502_AddrMode adm){
   Pointer ptr;
   BOOL isValue = getValue(machine, adm, &ptr);
   warnValue(isValue);
@@ -742,7 +747,7 @@ static void jmpLDY(machine_6502 *machine, AddrMode adm){
   manZeroNeg(machine, machine->regY);
 }
 
-static void jmpLSR(machine_6502 *machine, AddrMode adm){
+static void jmpLSR(machine_6502 *machine, m6502_AddrMode adm){
   Pointer ptr;
   BOOL isValue = getValue(machine, adm, &ptr);
   if (isValue){
@@ -764,11 +769,11 @@ static void jmpLSR(machine_6502 *machine, AddrMode adm){
   }
 }
 
-static void jmpNOP(machine_6502 *machine, AddrMode adm){
+static void jmpNOP(machine_6502 *machine, m6502_AddrMode adm){
   /* no operation */
 }
 
-static void jmpORA(machine_6502 *machine, AddrMode adm){
+static void jmpORA(machine_6502 *machine, m6502_AddrMode adm){
   Pointer ptr;
   BOOL isValue = getValue(machine, adm, &ptr);
   warnValue(isValue);
@@ -776,17 +781,17 @@ static void jmpORA(machine_6502 *machine, AddrMode adm){
   manZeroNeg(machine,machine->regA);
 }
 
-static void jmpTAX(machine_6502 *machine, AddrMode adm){
+static void jmpTAX(machine_6502 *machine, m6502_AddrMode adm){
   machine->regX = machine->regA;
   manZeroNeg(machine,machine->regX);
 }
 
-static void jmpTXA(machine_6502 *machine, AddrMode adm){
+static void jmpTXA(machine_6502 *machine, m6502_AddrMode adm){
   machine->regA = machine->regX;
   manZeroNeg(machine,machine->regA);
 }
 
-static void jmpDEX(machine_6502 *machine, AddrMode adm){
+static void jmpDEX(machine_6502 *machine, m6502_AddrMode adm){
   if (machine->regX > 0)
     machine->regX--;
   else
@@ -794,23 +799,23 @@ static void jmpDEX(machine_6502 *machine, AddrMode adm){
   manZeroNeg(machine, machine->regX);
 }
 
-static void jmpINX(machine_6502 *machine, AddrMode adm){
+static void jmpINX(machine_6502 *machine, m6502_AddrMode adm){
   Bit16 value = machine->regX + 1;
   machine->regX = value & 0xFF;
   manZeroNeg(machine, machine->regX);
 }
 
-static void jmpTAY(machine_6502 *machine, AddrMode adm){
+static void jmpTAY(machine_6502 *machine, m6502_AddrMode adm){
   machine->regY = machine->regA;
   manZeroNeg(machine, machine->regY);
 }
 
-static void jmpTYA(machine_6502 *machine, AddrMode adm){
+static void jmpTYA(machine_6502 *machine, m6502_AddrMode adm){
   machine->regA = machine->regY;
   manZeroNeg(machine, machine->regA);
 }
 
-static void jmpDEY(machine_6502 *machine, AddrMode adm){
+static void jmpDEY(machine_6502 *machine, m6502_AddrMode adm){
   if (machine->regY > 0)
     machine->regY--;
   else
@@ -818,13 +823,13 @@ static void jmpDEY(machine_6502 *machine, AddrMode adm){
   manZeroNeg(machine, machine->regY);
 }
 
-static void jmpINY(machine_6502 *machine, AddrMode adm){
+static void jmpINY(machine_6502 *machine, m6502_AddrMode adm){
   Bit16 value = machine->regY + 1;
   machine->regY = value & 0xff;
   manZeroNeg(machine, machine->regY);
 }
 
-static void jmpROR(machine_6502 *machine, AddrMode adm){
+static void jmpROR(machine_6502 *machine, m6502_AddrMode adm){
   Pointer ptr;
   Bit8 cf;
   BOOL isValue = getValue(machine, adm, &ptr);
@@ -849,7 +854,7 @@ static void jmpROR(machine_6502 *machine, AddrMode adm){
   }
 }
 
-static void jmpROL(machine_6502 *machine, AddrMode adm){
+static void jmpROL(machine_6502 *machine, m6502_AddrMode adm){
   Pointer ptr;
   Bit8 cf;
   BOOL isValue = getValue(machine, adm, &ptr);
@@ -874,12 +879,12 @@ static void jmpROL(machine_6502 *machine, AddrMode adm){
   }
 }
 
-static void jmpRTI(machine_6502 *machine, AddrMode adm){
+static void jmpRTI(machine_6502 *machine, m6502_AddrMode adm){
   machine->regP = stackPop(machine);
   machine->regPC = stackPop(machine);
 }
 
-static void jmpRTS(machine_6502 *machine, AddrMode adm){
+static void jmpRTS(machine_6502 *machine, m6502_AddrMode adm){
   Pointer ptr;
   BOOL isValue = getValue(machine, adm, &ptr);
   Bit16 nr = stackPop(machine);
@@ -888,7 +893,7 @@ static void jmpRTS(machine_6502 *machine, AddrMode adm){
   machine->regPC = (nl << 8) | nr;
 }
 
-static void jmpSBC(machine_6502 *machine, AddrMode adm){
+static void jmpSBC(machine_6502 *machine, m6502_AddrMode adm){
   Pointer ptr;
   /*Bit8 vflag;*/
   Bit8 c = bitOn(machine->regP, CARRY_FL);
@@ -944,48 +949,48 @@ static void jmpSBC(machine_6502 *machine, AddrMode adm){
   manZeroNeg(machine,machine->regA);
 }
 
-static void jmpSTA(machine_6502 *machine, AddrMode adm){
+static void jmpSTA(machine_6502 *machine, m6502_AddrMode adm){
   Pointer ptr;
   BOOL isValue = getValue(machine, adm, &ptr);
   warnValue(isValue);
   memStoreByte(machine,ptr.addr,machine->regA);
 }
 
-static void jmpTXS(machine_6502 *machine, AddrMode adm){
+static void jmpTXS(machine_6502 *machine, m6502_AddrMode adm){
   stackPush(machine,machine->regX);
 }
 
-static void jmpTSX(machine_6502 *machine, AddrMode adm){
+static void jmpTSX(machine_6502 *machine, m6502_AddrMode adm){
   machine->regX = stackPop(machine);
   manZeroNeg(machine, machine->regX);
 }
 
-static void jmpPHA(machine_6502 *machine, AddrMode adm){
+static void jmpPHA(machine_6502 *machine, m6502_AddrMode adm){
   stackPush(machine, machine->regA);
 }
 
-static void jmpPLA(machine_6502 *machine, AddrMode adm){
+static void jmpPLA(machine_6502 *machine, m6502_AddrMode adm){
   machine->regA = stackPop(machine);
   manZeroNeg(machine, machine->regA);
 }
 
-static void jmpPHP(machine_6502 *machine, AddrMode adm){
+static void jmpPHP(machine_6502 *machine, m6502_AddrMode adm){
   stackPush(machine,machine->regP);
 }
 
-static void jmpPLP(machine_6502 *machine, AddrMode adm){
+static void jmpPLP(machine_6502 *machine, m6502_AddrMode adm){
   machine->regP = stackPop(machine);
   machine->regP = setBit(machine->regP, FUTURE_FL, 1);
 }
 
-static void jmpSTX(machine_6502 *machine, AddrMode adm){
+static void jmpSTX(machine_6502 *machine, m6502_AddrMode adm){
   Pointer ptr;
   BOOL isValue = getValue(machine, adm, &ptr);
   warnValue(isValue);
   memStoreByte(machine,ptr.addr,machine->regX);
 }
 
-static void jmpSTY(machine_6502 *machine, AddrMode adm){
+static void jmpSTY(machine_6502 *machine, m6502_AddrMode adm){
   Pointer ptr;
   BOOL isValue = getValue(machine, adm, &ptr);
   warnValue(isValue);
@@ -995,7 +1000,7 @@ static void jmpSTY(machine_6502 *machine, AddrMode adm){
 \f
 
 /* OPCODES */
-static void assignOpCodes(Opcodes *opcodes){
+static void assignOpCodes(m6502_Opcodes *opcodes){
 
  #define SETOP(num, _name, _Imm, _ZP, _ZPX, _ZPY, _ABS, _ABSX, _ABSY, _INDX, _INDY, _SNGL, _BRA, _func) \
 {opcodes[num].name[3] = '\0'; \
@@ -1117,7 +1122,7 @@ static void buildIndexCache(machine_6502 *machine){
 /* opIndex() - Search the opcode table for a match. If found return
    the index into the optable and the address mode of the opcode. If
    the opcode is not found then return -1. */
-static int opIndex(machine_6502 *machine, Bit8 opcode, AddrMode *adm){ 
+static int opIndex(machine_6502 *machine, Bit8 opcode, m6502_AddrMode *adm){ 
   /* XXX could catch errors by setting a addressmode of error or something */
   *adm = machine->opcache[opcode].adm;
   return machine->opcache[opcode].index;
@@ -1130,7 +1135,7 @@ static Param *newParam(void){
   Param *newp;
   int i = 0;
 
-  newp = (Param *) emalloc(sizeof(Param));
+  newp = (Param *) ecalloc(1, sizeof(Param));
   newp->type = SINGLE;
   for (i = 0; i < MAX_PARAM_VALUE; i++)
     newp->value[i] = 0;
@@ -1153,7 +1158,7 @@ static void copyParam(Param *p1, Param *p2){
 static Label *newLabel(void){
   Label *newp; 
 
-  newp = (Label *) emalloc(sizeof(Label));
+  newp = (Label *) ecalloc(1, sizeof(Label));
   newp->addr = 0;
   newp->label = ecalloc(MAX_LABEL_LEN,sizeof(char));
   
@@ -1164,7 +1169,7 @@ static AsmLine *newAsmLine(char *cmd, char *label, BOOL decl, Param *param, int
 {
     AsmLine *newp;
 
-    newp =  (AsmLine *) emalloc(sizeof(AsmLine));
+    newp =  (AsmLine *) ecalloc(1, sizeof(AsmLine));
     newp->labelDecl = decl;
     newp->label = newLabel();
     strncpy(newp->label->label,label,MAX_LABEL_LEN);
@@ -1220,7 +1225,8 @@ static void freeallAsmLine(AsmLine *listp)
 }
 
 static BOOL addvalue(Param *param,Bit32 value){
-  if (0 <= param->vp && param->vp < MAX_PARAM_VALUE) {
+  /* jwz: suppress "0 <= unsigned" warning */
+  if (/*0 <= param->vp &&*/ param->vp < MAX_PARAM_VALUE) {
     param->value[param->vp++] = value;
     return TRUE;
   }
@@ -1365,8 +1371,10 @@ static BOOL parseDec(char **s, Bit32 *value){
     free(dec);  
     return TRUE;
   }
-  else
+  else{
+    free(dec);
     return FALSE;
+  }
 }
 
 static BOOL parseValue(char **s, Bit32 *value){
@@ -1662,6 +1670,7 @@ static AsmLine *parseAssembly(machine_6502 *machine, BOOL *codeOk, const char *c
   return listp;
 }
     
+#ifdef READ_FILES
 /* fileToBuffer() - Allocates a buffer and loads all of the file into memory. */
 static char *fileToBuffer(const char *filename){
   const int defaultSize = 1024;
@@ -1674,7 +1683,7 @@ static char *fileToBuffer(const char *filename){
   if (!buffer) abort();
 
   ifp = fopen(filename, "rb");
-  if (!ifp) return 0;
+  if (!ifp) abort();
 
   while((c = getc(ifp)) != EOF){
     buffer[i++] = c;
@@ -1694,6 +1703,7 @@ static char *fileToBuffer(const char *filename){
   buffer[i+1] = '\0';
   return buffer;
 }
+#endif
 
 \f
 /* Routines */
@@ -1723,7 +1733,7 @@ static void reset(machine_6502 *machine){
 }
 
 /* hexDump() - Dump the memory to output */
-void hexDump(machine_6502 *machine, Bit16 start, Bit16 numbytes, FILE *output){
+void m6502_hexDump(machine_6502 *machine, Bit16 start, Bit16 numbytes, FILE *output){
   Bit32 address;
   Bit32 i;
   for( i = 0; i < numbytes; i++){
@@ -1755,7 +1765,7 @@ void hexDump(machine_6502 *machine, Bit16 start, Bit16 numbytes, FILE *output){
 /*   fclose(ofp); */
 /* } */
 
-static BOOL translate(Opcodes *op,Param *param, machine_6502 *machine){
+static BOOL translate(m6502_Opcodes *op,Param *param, machine_6502 *machine){
    switch(param->type){
     case SINGLE:
       if (op->SNGL)
@@ -1864,7 +1874,7 @@ static BOOL translate(Opcodes *op,Param *param, machine_6502 *machine){
        if (op->BRA) {
          pushByte(machine, op->BRA);
           {
-            int diff = abs(param->lbladdr - machine->defaultCodePC);
+            int diff = (param->lbladdr - machine->defaultCodePC);
             int backward = (param->lbladdr < machine->defaultCodePC);
             pushByte(machine, (backward) ? 0xff - diff : diff - 1);
           }
@@ -1939,7 +1949,7 @@ static BOOL compileLine(AsmLine *asmline, void *args){
   else{
     int i;
     char *command = asmline->command;
-    Opcodes op;
+    m6502_Opcodes op;
     for(i = 0; i < NUM_OPCODES; i++){
       if (strcmp(machine->opcodes[i].name, command) == 0){
        op = machine->opcodes[i];
@@ -2068,7 +2078,7 @@ static BOOL compileCode(machine_6502 *machine, const char *code){
 
 static void execute(machine_6502 *machine){
   Bit8 opcode;
-  AddrMode adm;
+  m6502_AddrMode adm;
   int opidx;
 
   if(!machine->codeRunning) return;
@@ -2089,23 +2099,23 @@ static void execute(machine_6502 *machine){
   }
 }
 
-machine_6502 *build6502(){
+machine_6502 *m6502_build(void){
   machine_6502 *machine;
-  machine = emalloc(sizeof(machine_6502));
+  machine = ecalloc(1, sizeof(machine_6502));
   assignOpCodes(machine->opcodes);
   buildIndexCache(machine);
   reset(machine);
   return machine;
 }
 
-void destroy6502(machine_6502 *machine){
+void m6502_destroy6502(machine_6502 *machine){
   free(machine);
   machine = NULL;
 }
 
-void trace(machine_6502 *machine, FILE *output){
+void m6502_trace(machine_6502 *machine, FILE *output){
   Bit8 opcode = memReadByte(machine,machine->regPC);
-  AddrMode adm;
+  m6502_AddrMode adm;
   Pointer ptr;
   int opidx = opIndex(machine,opcode,&adm);
   int stacksz = STACK_TOP - machine->regSP;
@@ -2131,15 +2141,16 @@ void trace(machine_6502 *machine, FILE *output){
       fprintf(output,"\n");
   }
   fprintf(output,"STACK:");
-  hexDump(machine,(STACK_TOP - stacksz) + 1, stacksz, output);
+  m6502_hexDump(machine,(STACK_TOP - stacksz) + 1, stacksz, output);
 }
 
+#if 0
 void disassemble(machine_6502 *machine, FILE *output){
   /* Read the opcode
      increment the program counter
      print the opcode
      loop until end of program. */
-  AddrMode adm;
+  m6502_AddrMode adm;
   Bit16 addr;
   Bit8 opcode;
   int opidx;
@@ -2160,9 +2171,11 @@ void disassemble(machine_6502 *machine, FILE *output){
   free(mem);
   machine->regPC = opc;
 }
+#endif
 
 \f
-void eval_file(machine_6502 *machine, const char *filename, Plotter plot, void *plotterState){
+#ifdef READ_FILES
+void m6502_eval_file(machine_6502 *machine, const char *filename, m6502_Plotter plot, void *plotterState){
   char *code = NULL;
 
   machine->plot = plot;
@@ -2185,7 +2198,7 @@ void eval_file(machine_6502 *machine, const char *filename, Plotter plot, void *
   }while(machine->codeRunning);
 }
 
-void start_eval_file(machine_6502 *machine, const char *filename, Plotter plot, void *plotterState){
+void m6502_start_eval_file(machine_6502 *machine, const char *filename, m6502_Plotter plot, void *plotterState){
   char *code = NULL;
   reset(machine);
 
@@ -2202,9 +2215,10 @@ void start_eval_file(machine_6502 *machine, const char *filename, Plotter plot,
   machine->codeRunning = TRUE;
   execute(machine);
 }
+#endif /* READ_FILES */
 
-void start_eval_string(machine_6502 *machine, const char *code,
-                      Plotter plot, void *plotterState){
+void m6502_start_eval_string(machine_6502 *machine, const char *code,
+                      m6502_Plotter plot, void *plotterState){
   reset(machine);
 
   machine->plot = plot;
@@ -2238,7 +2252,7 @@ void start_eval_string(machine_6502 *machine, const char *code,
 /*   execute(machine); */
 /* } */
 
-void next_eval(machine_6502 *machine, int insno){
+void m6502_next_eval(machine_6502 *machine, int insno){
   int i = 0;
   for (i = 1; i < insno; i++){
     if (machine->codeRunning){