X-Git-Url: http://git.hungrycats.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=hacks%2Fasm6502.c;h=a893b659d987fe72fece9342e4789ca4c4cb2252;hb=5f9c47ca98dd43d8f59b7c27d3fde6edfde4fe21;hp=101ee5a6a416a98fbeadf7b0abca1b23f91d82a5;hpb=6b1c86cf395f59389e4ece4ea8f4bea2c332745b;p=xscreensaver diff --git a/hacks/asm6502.c b/hacks/asm6502.c index 101ee5a6..a893b659 100644 --- a/hacks/asm6502.c +++ b/hacks/asm6502.c @@ -94,45 +94,16 @@ typedef struct { Bit16 value; } Pointer; -/* eprintf - Taken from "Practice of Programming" by Kernighan and Pike */ -static void eprintf(char *fmt, ...){ - va_list args; - - char *progname = "Assembler"; - - fflush(stdout); - if (progname != NULL) - fprintf(stderr, "%s: ", progname); - - va_start(args, fmt); - vfprintf(stderr, fmt, args); - va_end(args); - - if (fmt[0] != '\0' && fmt[strlen(fmt) -1] == ':') - fprintf(stderr, " %s", strerror(errno)); - fprintf(stderr, "\n"); - exit(2); /* conventional value for failed execution */ -} -/* emalloc - Taken from "Practice of Programming" by Kernighan and - Pike. If memory allocatiion fails the program will print a message - an exit. */ -static void *emalloc(size_t n) { - void *p; - - p = malloc(n); - if (p == NULL) - eprintf("malloc of %u bytes failed:", n); +/*static void *emalloc(size_t n) { + void *p = malloc(n); + if (! p) abort(); return p; -} +}*/ -/* ecalloc - Dose the same thing as emalloc just calls calloc instead. */ static void *ecalloc(uint32_t nelm, size_t nsize){ - void *p; - - p = calloc(nelm, nsize); - if (p == NULL) - eprintf("calloc of %u bytes failed:", nelm * nsize); + void *p = calloc(nelm, nsize); + if (!p) abort(); return p; } @@ -419,7 +390,7 @@ static void dismem(machine_6502 *machine, AddrMode adm, char *output){ Bit16 n; switch(adm){ case SINGLE: - output = ""; + *output = 0; break; case IMMEDIATE_LESS: case IMMEDIATE_GREAT: @@ -467,7 +438,7 @@ static void dismem(machine_6502 *machine, AddrMode adm, char *output){ sprintf(output,"$%x,x",n); break; case DCB_PARAM: - output = ""; + *output = 0; break; } } @@ -919,13 +890,13 @@ static void jmpRTS(machine_6502 *machine, AddrMode adm){ static void jmpSBC(machine_6502 *machine, AddrMode adm){ Pointer ptr; - Bit8 vflag; + /*Bit8 vflag;*/ Bit8 c = bitOn(machine->regP, CARRY_FL); Bit16 tmp, w; BOOL isValue = getValue(machine, adm, &ptr); warnValue(isValue); - vflag = (bitOn(machine->regA,NEGATIVE_FL) && - bitOn(ptr.value, NEGATIVE_FL)); + /*vflag = (bitOn(machine->regA,NEGATIVE_FL) && + bitOn(ptr.value, NEGATIVE_FL));*/ if (bitOn(machine->regP, DECIMAL_FL)) { Bit8 ar = nibble(machine->regA, RIGHT); @@ -1027,7 +998,7 @@ static void jmpSTY(machine_6502 *machine, AddrMode adm){ static void assignOpCodes(Opcodes *opcodes){ #define SETOP(num, _name, _Imm, _ZP, _ZPX, _ZPY, _ABS, _ABSX, _ABSY, _INDX, _INDY, _SNGL, _BRA, _func) \ -{opcodes[num].name[4] = '\0'; \ +{opcodes[num].name[3] = '\0'; \ strncpy(opcodes[num].name, _name, 3); opcodes[num].Imm = _Imm; opcodes[num].ZP = _ZP; \ opcodes[num].ZPX = _ZPX; opcodes[num].ZPY = _ZPY; opcodes[num].ABS = _ABS; \ opcodes[num].ABSX = _ABSX; opcodes[num].ABSY = _ABSY; opcodes[num].INDX = _INDX; \ @@ -1159,7 +1130,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; @@ -1182,7 +1153,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)); @@ -1193,7 +1164,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); @@ -1700,12 +1671,10 @@ static char *fileToBuffer(const char *filename){ int i = 0; char *buffer = ecalloc(defaultSize,sizeof(char)); - if (buffer == NULL) - eprintf("Could not allocate memory for buffer."); + if (!buffer) abort(); ifp = fopen(filename, "rb"); - if (ifp == NULL) - eprintf("Could not open file."); + if (!ifp) return 0; while((c = getc(ifp)) != EOF){ buffer[i++] = c; @@ -1713,15 +1682,13 @@ static char *fileToBuffer(const char *filename){ size += defaultSize; buffer = realloc(buffer, size); if (buffer == NULL) { - fclose(ifp); - eprintf("Could not resize buffer."); + abort(); } } } fclose(ifp); buffer = realloc(buffer, i+2); - if (buffer == NULL) - eprintf("Could not resize buffer."); + if (!buffer) abort(); /* Make sure we have a line feed at the end */ buffer[i] = '\n'; buffer[i+1] = '\0'; @@ -1776,8 +1743,7 @@ void hexDump(machine_6502 *machine, Bit16 start, Bit16 numbytes, FILE *output){ /* Bit16 end = pc + machine->codeLen; */ /* Bit16 n; */ /* ofp = fopen(filename, "w"); */ -/* if (ofp == NULL) */ -/* eprintf("Could not open file."); */ +/* if (!ofp) abort(); */ /* fprintf(ofp,"Bit8 prog[%d] =\n{",machine->codeLen); */ /* n = 1; */ @@ -1897,12 +1863,11 @@ static BOOL translate(Opcodes *op,Param *param, machine_6502 *machine){ else { if (op->BRA) { pushByte(machine, op->BRA); - if (param->lbladdr < (machine->codeLen + PROG_START)) - pushByte(machine, - (0xff - (machine->codeLen-param->lbladdr)) & 0xff); - else - pushByte(machine, - (param->lbladdr - machine->codeLen-1) & 0xff); + { + int diff = abs(param->lbladdr - machine->defaultCodePC); + int backward = (param->lbladdr < machine->defaultCodePC); + pushByte(machine, (backward) ? 0xff - diff : diff - 1); + } } else { fprintf(stderr,"%s does not take BRANCH parameters.\n",op->name); @@ -1993,14 +1958,28 @@ static BOOL compileLine(AsmLine *asmline, void *args){ static BOOL indexLabels(AsmLine *asmline, void *arg){ machine_6502 *machine; int thisPC; + Bit16 oldDefault; machine = arg; + oldDefault = machine->defaultCodePC; thisPC = machine->regPC; /* Figure out how many bytes this instruction takes */ machine->codeLen = 0; + if ( ! compileLine(asmline, machine) ){ return FALSE; } - machine->regPC += machine->codeLen; + + /* If the machine's defaultCodePC has changed then we encountered a + *= which changes the load address. We need to initials our code + *counter with the current default. */ + if (oldDefault == machine->defaultCodePC){ + machine->regPC += machine->codeLen; + } + else { + machine->regPC = machine->defaultCodePC; + /*oldDefault = machine->defaultCodePC;*/ + } + if (asmline->labelDecl) { asmline->label->addr = thisPC; } @@ -2040,6 +2019,20 @@ static BOOL compileCode(machine_6502 *machine, const char *code){ return FALSE; /* update label references */ linkLabels(asmlist); + +#if 0 /* prints out some debugging information */ + { + AsmLine *p; + if(asmlist != NULL){ + for (p = asmlist; p != NULL; p = p->next) + fprintf(stderr,"%s lbl: %s addr: %d ParamLbl: %s ParamAddr: %d\n", + p->command, p->label->label, p->label->addr, + p->param->label, p->param->lbladdr); + } + } + +#endif + /* Second pass: translate the instructions */ machine->codeLen = 0; /* Link label call push_byte which increments defaultCodePC. @@ -2098,7 +2091,7 @@ static void execute(machine_6502 *machine){ machine_6502 *build6502(){ machine_6502 *machine; - machine = emalloc(sizeof(machine_6502)); + machine = ecalloc(1, sizeof(machine_6502)); assignOpCodes(machine->opcodes); buildIndexCache(machine); reset(machine); @@ -2177,9 +2170,7 @@ void eval_file(machine_6502 *machine, const char *filename, Plotter plot, void * code = fileToBuffer(filename); - if (! compileCode(machine, code) ){ - eprintf("Could not compile code.\n"); - } + if (! compileCode(machine, code) ) abort(); free(code); @@ -2203,9 +2194,7 @@ void start_eval_file(machine_6502 *machine, const char *filename, Plotter plot, code = fileToBuffer(filename); - if (! compileCode(machine, code) ){ - eprintf("Could not compile code.\n"); - } + if (! compileCode(machine, code) ) abort(); free(code);