Skip to content

Commit

Permalink
Use printf instead of custom variadic function
Browse files Browse the repository at this point in the history
  • Loading branch information
Eggbertx committed Apr 29, 2024
1 parent b926732 commit 6ef5460
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 46 deletions.
62 changes: 33 additions & 29 deletions src/chip8.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,43 +67,41 @@ void _OC8_FASTCALL drawScreen(struct Chip8* chip8) {

void dumpBytes(uchar* bytes, short filesize, char* filename) {
int i;
#if !defined(__CC65__) && !defined(GB_IO)
FILE* dumpFile;
oc8log("Dumping to %s\n", filename);
#if defined(SDL_IO) || defined(CURSES_IO)
FILE* dumpFile;

printf("Dumping to %s\n", filename);

dumpFile = fopen(filename, "w");
if(dumpFile == NULL) {
oc8log("Error opening %s\n", filename);
return;
}
dumpFile = fopen(filename, "w");
if(dumpFile == NULL) {
printf("Error opening %s\n", filename);
return;
}

for(i = 0; i < filesize; i++) {
fprintf(dumpFile, "%02X ", bytes[i]);
if((i + 1) % 0x10 == 0) fprintf(dumpFile, "\n");
}
fclose(dumpFile);
#endif
for(i = 0; i < filesize; i++) {
fprintf(dumpFile, "%02X ", bytes[i]);
if((i + 1) % 0x10 == 0) fprintf(dumpFile, "\n");
}
fclose(dumpFile);
#endif
}

void printStatus(struct Chip8* chip8) {
#ifdef PRINT_DEBUG
int r = 0;
int s = 0;
oc8log("V registers:\n");
printf("V registers:\n");
for(r = 0; r < 16; r++) {
oc8log("\tV%X: %x\n", r, chip8->V[r]);
printf("\tV%X: %x\n", r, chip8->V[r]);
}
oc8log("Program counter: %d\n", chip8->PC);
oc8log("Address register (I): %d\n", chip8->I);
oc8log("Delay timer: %d\nSound timer: %d\n", chip8->delayTimer, chip8->soundTimer);
oc8log("chip8->drawFlag: %d\n", chip8->drawFlag);
oc8log("Stack:\n");
printf("Program counter: %d\n", chip8->PC);
printf("Address register (I): %d\n", chip8->I);
printf("Delay timer: %d\nSound timer: %d\n", chip8->delayTimer, chip8->soundTimer);
printf("chip8->drawFlag: %d\n", chip8->drawFlag);
printf("Stack:\n");
for(s = 0; s < 16; s++) {
oc8log("\tstack[%d]: %x\n", s, chip8->stack[s]);
printf("\tstack[%d]: %x\n", s, chip8->stack[s]);
}
oc8log("Stack pointer: %d\n", chip8->stackPointer);
#endif
printf("Stack pointer: %d\n", chip8->stackPointer);
}


Expand All @@ -119,13 +117,17 @@ void _OC8_FASTCALL doCycle(struct Chip8* chip8) {
delay(1);
if(chip8->delayTimer > 0) chip8->delayTimer--;
if(chip8->soundTimer > 0) {
oc8log("beep");
#ifdef SDL_IO
printf("beep");
#endif
chip8->soundTimer--;
}


if(chip8->PC > ROM_END_ADDR) {
oc8log("Program counter (%04x) reached end of file\n", chip8->PC);
#ifdef SDL_IO
printf("Program counter (%04x) reached end of file\n", chip8->PC);
#endif
chip8->status = STATUS_PAUSED;
return;
}
Expand Down Expand Up @@ -404,8 +406,10 @@ void _OC8_FASTCALL doCycle(struct Chip8* chip8) {
return;

unrecognized_opcode:
oc8log("Unrecognized opcode: %04x at %04x\n", chip8->opcode, chip8->PC);
dumpBytes(chip8->memory, 4096, "dumps/memorybytes_unrecognized.txt");
#ifdef PRINT_DEBUG
printf("Unrecognized opcode: %04x at %04x\n", chip8->opcode, chip8->PC);
printStatus(chip8);
#endif
chip8->status = STATUS_ERROR;
}
8 changes: 4 additions & 4 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,22 @@ int main(int argc, char *argv[]) {
if(argc == 2) {
chip8.romPath = argv[1];
} else {
oc8log("usage: %s [--print-opcodes] path/to/rom\n", argv[0]);
printf("usage: %s [--print-opcodes] path/to/rom\n", argv[0]);
return 1;
}
#endif
if (initChip8(&chip8) > 0) {
oc8log("ERROR: Something went wrong while loading %s\n", chip8.romPath);
printf("ERROR: Something went wrong while loading %s\n", chip8.romPath);
goto finish;
}

if(initScreen() > 0) {
oc8log("ERROR: Unable to init screen.\n");
printf("ERROR: Unable to init screen.\n");
goto finish;
}

if(initAudio() > 0) {
oc8log("ERROR: Unable to init audio.\n");
printf("ERROR: Unable to init audio.\n");
goto finish;
}

Expand Down
11 changes: 0 additions & 11 deletions src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,3 @@ uchar loadROM(struct Chip8* chip8, char* file) {
}
return 0;
}

int oc8log(const char* format, ...) {
int outSize = 0;
#if !defined(__CC65__)
va_list args;
va_start(args, format);
outSize = vprintf(format, args);
va_end(args);
#endif
return outSize;
}
2 changes: 0 additions & 2 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@

uchar loadROM(struct Chip8* chip8, char* file);

int oc8log(const char* format, ...);

#endif

0 comments on commit 6ef5460

Please sign in to comment.