From 13162bfec79f79fd1dc2cfbeede7f81e79d5a669 Mon Sep 17 00:00:00 2001 From: Eggbertx Date: Mon, 25 Mar 2024 15:45:24 -0700 Subject: [PATCH] Make opcode printing set at compile time --- make.py | 32 ++++++++----- src/chip8.c | 126 ++++++++++++++++++++++++++-------------------------- src/chip8.h | 2 +- src/main.c | 15 ++----- 4 files changed, 88 insertions(+), 87 deletions(-) diff --git a/make.py b/make.py index ded8892..9cab015 100755 --- a/make.py +++ b/make.py @@ -132,7 +132,7 @@ def term_type(): return "cmd" return "other" -def build(platform = "native", library = "sdl", debugging = False, embed = "", listing_file = ""): +def build(platform = "native", library = "sdl", debugging = False, print_opcodes=False, embed = "", listing_file = ""): if embed != "": create_embed(embed) term = term_type() @@ -149,7 +149,7 @@ def build(platform = "native", library = "sdl", debugging = False, embed = "", l oc8_out = out_file(platform, "OS" in os.environ and os.environ["OS"] == "Windows_NT") cmd = "" - sources = ["src/util.c", "src/opcode_funcs.c", "src/chip8.c", "src/main.c"] + sources = "src/util.c src/opcode_funcs.c src/chip8.c src/main.c" if msbuild: # Visual Studio development console is being used @@ -174,22 +174,23 @@ def build(platform = "native", library = "sdl", debugging = False, embed = "", l elif library == "sdl": includes_path += "/SDL2" - sources.append("src/io_{}.c".format(library)) + sources += " src/io_{}.c".format(library) cflags = "-pedantic -Wall -std=c89 -D_POSIX_SOURCE -fdiagnostics-color=always " - cmd = "cc -o {oc8_out} -D{io_const}_IO {includes_path} {debug_flag} {cflags} {sources} {lib}".format( + cmd = "cc -o {oc8_out} -D{io_const}_IO {includes_path} {debug_flag} {opcodes} {cflags} {sources} {lib}".format( oc8_out = oc8_out, io_const = library.upper(), includes_path = "-I" + includes_path, debug_flag = "-g" if debugging else "", + opcodes = "-DPRINT_OPCODES" if print_opcodes else "", cflags = cflags, - sources = " ".join(sources), + sources = sources, lib = lib ) elif platform in ("c64", "sim6502"): if not in_pathenv("cl65"): fatal_print("Unable to find the cc65 development kit, required to build for 65xx targets") - sources.append("src/io_{}.c".format(platform)) + sources += " src/io_{}.c".format(platform) cmd = "cl65 -Oi -Or -Os {debug_flag} -o {oc8_out} -t {platform} {listing} -D{io_const}_IO -D__EMBED_ROM__ {sources}".format( debug_flag = "-g -Ln oc8.lbl" if debugging else "", @@ -197,27 +198,27 @@ def build(platform = "native", library = "sdl", debugging = False, embed = "", l platform = platform, listing = ("-l " + listing_file) if listing_file != "" else "", io_const = platform.upper(), - sources = " ".join(sources) + sources = sources ) elif platform in ("gb", "ti83"): if not in_pathenv("zcc"): fatal_print("Unable to find the z88dk development kit, required to build for GameBoy and TI-8x") io_const = platform.upper() - sources.append("src/io_{}.c".format(platform)) + sources += " src/io_{}.c".format(platform) cmd = "zcc +{platform} --opt-code-speed -create-app -o {oc8_out} -D{io_const}_IO -D__EMBED_ROM__ {sources}".format( platform = platform, oc8_out = oc8_out, io_const = io_const, - sources = " ".join(sources) + sources = sources ) elif platform == "emscripten": if not in_pathenv("emcc"): fatal_print("Unable to find the emscripten development kit, required to build browser-compatible JavaScript") - sources.append("src/io_{}.c".format("sdl")) + sources += " src/io_{}.c".format("sdl") cmd = "emcc -o {oc8_out} -s --embed-file games USE_SDL=2 --shell-file shell.html -DSDL_IO -DEMSCRIPTEN_IO {sources}".format( oc8_out = oc8_out, - sources = " ".join(sources) + sources = sources ) else: fatal_print("Unsupported platform " + platform) @@ -274,7 +275,13 @@ def clean(): fatal_print(f"usage: {sys.argv[0]} embed path/to/romfile") create_embed(sys.argv[1]) exit() - elif action in ("sdl","curses"): + elif action == "sdl": + library = action + parser.add_argument( "-p", "--print-opcodes", + help="Print opcodes on the command line as they are executed", + default=False, + action="store_true") + elif action == "curses": library = action else: platform = action @@ -296,6 +303,7 @@ def clean(): create_embed(args.embed) build(platform, library, args.__dict__.get("debug", False), + args.__dict__.get("print_opcodes", False), args.__dict__.get("embed", "games/omnichip8"), args.__dict__.get("listing_file", "")) else: diff --git a/src/chip8.c b/src/chip8.c index 28efa10..ba12d83 100755 --- a/src/chip8.c +++ b/src/chip8.c @@ -107,7 +107,7 @@ void printStatus(struct Chip8* chip8) { } -void _OC8_FASTCALL doCycle(struct Chip8* chip8, uchar printOpcodes) { +void _OC8_FASTCALL doCycle(struct Chip8* chip8) { uchar x = 0; /* -X-- */ uchar y = 0; /* --Y- */ ushort nnn = 0; /* -nnn */ @@ -144,19 +144,19 @@ void _OC8_FASTCALL doCycle(struct Chip8* chip8, uchar printOpcodes) { switch(chip8->opcode & 0xF000) { case 0x0000: if(chip8->opcode == 0x0000) { - if(printOpcodes == 1) { + #ifdef PRINT_OPCODES addrInfo(chip8, "SYS %04x", nnn); /* not used for most "modern" ROMs */ - } + #endif } else if(chip8->opcode == 0x00E0) { - if(printOpcodes == 1) { + #ifdef PRINT_OPCODES addrInfo(chip8, "CLS"); - } + #endif clearDisplay(chip8); chip8->drawFlag = 1; } else if(chip8->opcode == 0x00EE) { - if(printOpcodes == 1) { + #ifdef PRINT_OPCODES addrInfo(chip8, "RET"); - } + #endif chip8->PC = chip8->stack[chip8->stackPointer] + ROM_START_ADDR + 2; chip8->stackPointer--; } else { @@ -164,13 +164,13 @@ void _OC8_FASTCALL doCycle(struct Chip8* chip8, uchar printOpcodes) { } break; case 0x1000: - if(printOpcodes == 1) { + #ifdef PRINT_OPCODES addrInfo(chip8, "JP %#04x", nnn); - } + #endif if(nnn == chip8->PC - 2) { - if(printOpcodes == 1) { + #ifdef PRINT_OPCODES addrInfo(chip8, "Reached infinite loop in CHIP-8 ROM, pausing exeution.\n"); - } + #endif chip8->status = STATUS_PAUSED; break; } @@ -178,69 +178,69 @@ void _OC8_FASTCALL doCycle(struct Chip8* chip8, uchar printOpcodes) { break; case 0x2000: /* increment SP, put PC on top of stack, set to nnn */ - if(printOpcodes == 1) { + #ifdef PRINT_OPCODES addrInfo(chip8, "CALL %#04x", nnn); - } + #endif chip8->stack[chip8->stackPointer] = chip8->PC; chip8->stackPointer++; chip8->PC = nnn; break; case 0x3000: - if(printOpcodes == 1) { + #ifdef PRINT_OPCODES addrInfo(chip8, "SE V%X, #%x", x, nn); - } + #endif if(chip8->V[x] == nn) { chip8->PC += 2; } break; case 0x4000: - if(printOpcodes == 1) { + #ifdef PRINT_OPCODES addrInfo(chip8, "SNE V%X, #%x", x, nn); - } + #endif if(chip8->V[x] != nn) { chip8->PC += 2; } break; case 0x5000: - if(printOpcodes == 1) { + #ifdef PRINT_OPCODES addrInfo(chip8, "SE V%X, V%X", x, y); - } + #endif if(chip8->V[x] == chip8->V[y]) { chip8->PC += 2; } break; case 0x6000: - if(printOpcodes == 1) { + #ifdef PRINT_OPCODES addrInfo(chip8, "LD V%X, #%x", x, nn); - } + #endif chip8->V[x] = nn; break; case 0x7000: - if(printOpcodes == 1) { + #ifdef PRINT_OPCODES addrInfo(chip8, "ADD V%X, #%x", x, nn); - } + #endif chip8->V[x] += nn; break; case 0x8000: { if(n == 0x0000) { - if(printOpcodes == 1) { + #ifdef PRINT_OPCODES addrInfo(chip8, "LD V%X, V%X", x, y); - } + #endif chip8->V[x] = chip8->V[y]; } else if(n == 0x0001) { - if(printOpcodes == 1) { + #ifdef PRINT_OPCODES addrInfo(chip8, "OR V%X, V%X", x, y); - } + #endif chip8->V[x] = chip8->V[x] | chip8->V[y]; } else if(n == 0x0002) { - if(printOpcodes == 1) { + #ifdef PRINT_OPCODES addrInfo(chip8, "AND V%X, V%X", x, y); - } + #endif chip8->V[x] = chip8->V[x] & chip8->V[y]; } else if(n == 0x0003) { - if(printOpcodes == 1) { + #ifdef PRINT_OPCODES addrInfo(chip8, "XOR V%X, V%X", x, y); - } + #endif chip8->V[x] ^= chip8->V[y]; } else if(n == 0x0004) { ushort sum = 0; @@ -248,9 +248,9 @@ void _OC8_FASTCALL doCycle(struct Chip8* chip8, uchar printOpcodes) { * Add V[x] and V[y] (lowest 8 bits) * If sum > 255, VF = 1. V%X = sum & 0xF */ - if(printOpcodes == 1) { + #ifdef PRINT_OPCODES addrInfo(chip8, "ADD V%X, V%X", x, y); - } + #endif sum = chip8->V[x] + chip8->V[y]; chip8->V[0xF] = sum > 255; chip8->V[x] = sum & 0xFF; @@ -260,9 +260,9 @@ void _OC8_FASTCALL doCycle(struct Chip8* chip8, uchar printOpcodes) { * If Vx > Vy, then VF is set to 1, otherwise 0. * Then Vy is subtracted from Vx, and the results stored in Vx. */ - if(printOpcodes == 1) { + #ifdef PRINT_OPCODES addrInfo(chip8, "SUB V%X, V%X", chip8->V[x], chip8->V[y]); - } + #endif chip8->V[0xF] = chip8->V[x] > chip8->V[y]; chip8->V[x] -= chip8->V[y]; } else if(n == 0x0006) { @@ -270,9 +270,9 @@ void _OC8_FASTCALL doCycle(struct Chip8* chip8, uchar printOpcodes) { * If the least-significant bit of Vx is 1, then VF is set to 1, otherwise 0. * Then Vx is divided by 2. */ - if(printOpcodes == 1) { + #ifdef PRINT_OPCODES addrInfo(chip8, "SHR V%X {, V%X}", chip8->V[x], chip8->V[y]); - } + #endif chip8->V[0xF] = (chip8->V[x] & 1); chip8->V[x] /= 2; /* chip8->V[x] = chip8->V[x] >> 1; */ @@ -283,9 +283,9 @@ void _OC8_FASTCALL doCycle(struct Chip8* chip8, uchar printOpcodes) { * Sets V%X to V%X minus V%X. VF is set to 0 when there's a borrow, * and 1 when there isn't */ - if(printOpcodes == 1) { + #ifdef PRINT_OPCODES addrInfo(chip8, "SUBN V%X, V%X", chip8->V[x], chip8->V[y]); - } + #endif chip8->V[0xF] = chip8->V[y] > chip8->V[x]; chip8->V[x] = chip8->V[y] - chip8->V[x]; } else if(n == 0x000E) { @@ -293,10 +293,10 @@ void _OC8_FASTCALL doCycle(struct Chip8* chip8, uchar printOpcodes) { * If the most-significant bit of Vx is 1, then VF is set to 1, otherwise to 0. * Then Vx is multiplied by 2. */ - if(printOpcodes == 1) { + #ifdef PRINT_OPCODES addrInfo(chip8, "SHL V%X {, V%X}", chip8->V[x], chip8->V[y]); /* addrInfo(chip8, "Stores the most significant bit of V%X in VF and then shifts V%X to the left by 1", x, x); */ - } + #endif chip8->V[0xF] = (chip8->V[x] & 128); chip8->V[x] = chip8->V[x] << 1; } else { @@ -305,63 +305,63 @@ void _OC8_FASTCALL doCycle(struct Chip8* chip8, uchar printOpcodes) { } break; case 0x9000: - if(printOpcodes == 1) { + #ifdef PRINT_OPCODES addrInfo(chip8, "SNE V%X, V%X", x, y); - } + #endif if(chip8->V[x] != chip8->V[y]) chip8->PC += 2; break; case 0xA000: - if(printOpcodes == 1) { + #ifdef PRINT_OPCODES addrInfo(chip8, "LD I, %#04x", nnn); - } + #endif chip8->I = nnn; break; case 0xB000: - if(printOpcodes == 1) { + #ifdef PRINT_OPCODES addrInfo(chip8, "JP V0, %#04x", nnn); - } + #endif chip8->PC = nnn + chip8->V[0]; break; case 0xC000: { uchar rnd; - if(printOpcodes == 1) { + #ifdef PRINT_OPCODES addrInfo(chip8, "RND V%X, #%x", x, nn); - } + #endif rnd = (rand() % 255) + 1; chip8->V[x] = rnd & nn; break; } case 0xD000: - if(printOpcodes == 1) { + #ifdef PRINT_OPCODES addrInfo(chip8, "DRW %X, %X, %X", x, y, n); - } + #endif drawSprite(chip8, x, y, n); break; case 0xE000: if(nn == 0x009E) { - if(printOpcodes == 1) { + #ifdef PRINT_OPCODES addrInfo(chip8, "SKP V%X", x); - } + #endif if(isPressed(chip8->V[x])) chip8->PC += 2; } else if(nn == 0x00A1) { - if(printOpcodes == 1) { + #ifdef PRINT_OPCODES addrInfo(chip8, "SKNP V%X", x); - } + #endif if(!isPressed(chip8->V[x])) chip8->PC += 2; } break; case 0xF000: { if(nn == 0x0007) { - if(printOpcodes == 1) { + #ifdef PRINT_OPCODES addrInfo(chip8, "LD V%X, DT", x); - } + #endif chip8->V[x] = chip8->delayTimer; } else if(nn == 0x000A) { int key = 0xFF; - if(printOpcodes == 1) { + #ifdef PRINT_OPCODES addrInfo(chip8, "LD V%X, K", x); - } + #endif do { key = getKey(); } while(key == 0xFF); @@ -373,9 +373,9 @@ void _OC8_FASTCALL doCycle(struct Chip8* chip8, uchar printOpcodes) { } else if(nn == 0x001E) { chip8->I += chip8->V[x]; } else if(nn == 0x0029) { - if(printOpcodes == 1) { + #ifdef PRINT_OPCODES addrInfo(chip8, "LD F, V%X", x); - } + #endif chip8->I = chip8->V[x] * 5; } else if(nn == 0x0033) { chip8->memory[chip8->I+0] = (chip8->V[x]/10) % 10; @@ -383,9 +383,9 @@ void _OC8_FASTCALL doCycle(struct Chip8* chip8, uchar printOpcodes) { chip8->memory[chip8->I+2] = chip8->V[x] % 10; } else if(nn == 0x0055) { ushort i; - if(printOpcodes == 1) { + #ifdef PRINT_OPCODES addrInfo(chip8, "LD [I], V%X", x); - } + #endif for(i = 0; i <= x; i++) { chip8->memory[chip8->I + i] = chip8->V[chip8->I + i]; } diff --git a/src/chip8.h b/src/chip8.h index 60f39fb..d5bd860 100755 --- a/src/chip8.h +++ b/src/chip8.h @@ -81,7 +81,7 @@ void dumpBytes(uchar* bytes, short filesize, char* filename); void printStatus(struct Chip8* chip8); -void _OC8_FASTCALL doCycle(struct Chip8* chip8, uchar printOpcodes); +void _OC8_FASTCALL doCycle(struct Chip8* chip8); void _OC8_FASTCALL drawScreen(struct Chip8* chip8); diff --git a/src/main.c b/src/main.c index d4420ba..6af9e96 100755 --- a/src/main.c +++ b/src/main.c @@ -10,9 +10,8 @@ #include "util.h" struct Chip8 chip8; -uchar printOpcodes; -void runCycles(uchar printOpcodes) { +static void runCycles() { uchar event = EVENT_NULL; while(chip8.status != STATUS_STOPPED) { event = getEvent(); @@ -26,7 +25,7 @@ void runCycles(uchar printOpcodes) { continue; } - doCycle(&chip8, printOpcodes); + doCycle(&chip8); if(chip8.drawFlag == 1) { drawScreen(&chip8); } @@ -35,13 +34,7 @@ void runCycles(uchar printOpcodes) { int main(int argc, char *argv[]) { #ifndef __EMBED_ROM__ - printOpcodes = 0; - if(argc == 3 && argv[1][0] == '-' && argv[1][1] == '-') { - chip8.romPath = argv[2]; - if(strcmp(argv[1], "--print-opcodes") == 0) { - printOpcodes = 1; - } - } else if(argc == 2 && argv[1][0] != '-' && argv[1][0] != '-') { + if(argc == 2) { chip8.romPath = argv[1]; } else { oc8log("usage: %s [--print-opcodes] path/to/rom\n", argv[0]); @@ -66,7 +59,7 @@ int main(int argc, char *argv[]) { #ifdef EMSCRIPTEN_IO emscripten_set_main_loop_arg(runCycles, chip8, 60, 0); #else - runCycles(printOpcodes); + runCycles(); #endif finish: