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;
}
Bit16 n;
switch(adm){
case SINGLE:
- output = "";
+ *output = 0;
break;
case IMMEDIATE_LESS:
case IMMEDIATE_GREAT:
sprintf(output,"$%x,x",n);
break;
case DCB_PARAM:
- output = "";
+ *output = 0;
break;
}
}
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);
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; \
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;
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));
{
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);
}
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;
}
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;
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';
/* 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; */
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);
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;
}
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.
machine_6502 *build6502(){
machine_6502 *machine;
- machine = emalloc(sizeof(machine_6502));
+ machine = ecalloc(1, sizeof(machine_6502));
assignOpCodes(machine->opcodes);
buildIndexCache(machine);
reset(machine);
code = fileToBuffer(filename);
- if (! compileCode(machine, code) ){
- eprintf("Could not compile code.\n");
- }
+ if (! compileCode(machine, code) ) abort();
free(code);
code = fileToBuffer(filename);
- if (! compileCode(machine, code) ){
- eprintf("Could not compile code.\n");
- }
+ if (! compileCode(machine, code) ) abort();
free(code);