diff --git a/src/main.c b/src/main.c index 5910172..3f84ccd 100644 --- a/src/main.c +++ b/src/main.c @@ -1,3 +1,4 @@ +#include #include #include #include @@ -25,19 +26,23 @@ uint8_t halt = 0; uint8_t showHelp = 0; uint8_t showDebug = 0; uint8_t fpsCount = 0; +uint8_t displayMmap = 0; +uint8_t documentReload = 0; -uint8_t fpsStore; +uint8_t fpsStore = 60; +uint8_t FPS = 30; uint8_t cpuHealth; unsigned long cpuTicks = 0; unsigned long tickTrigger = 0; -unsigned long renderTrigger = 0; -char debugString[255]; +char debugString[512]; +char snapshotFile[32]; +char *inputFile; -int openFile(const char *inputFile){ +int openFile(){ FILE *file = fopen(inputFile, "rb"); int retCode = 0; @@ -60,32 +65,23 @@ void resetSystem(){ reset6502(); } -void writeHelp(int type){ - if (!type) printf(HELP); - else printf(HELPKEYS); - - exit(EXIT_SUCCESS); -} - -void writePreamble() { - printf(PREAMBLE); -} - void fetchArgs(int argc, char *argv[]){ - writePreamble(); + printf(PREAMBLE); for (int i = 1; i < argc; i++) { - if (!strcmp(argv[i], "--help")) writeHelp(0); - else if (!strcmp(argv[i], "--help-keys")) writeHelp(1); + if (!strcmp(argv[i], "--help")) printf(HELP); + else if (!strcmp(argv[i], "--help-keys")) printf(HELPKEYS); else if (!strcmp(argv[i], "--cpuspeed")) cpuSpeed = atoi(argv[++i]); else if (!strcmp(argv[i], "--scale")) displayScale = atof(argv[++i]); else if (!strcmp(argv[i], "--singlestep")) singleStep = 1; else if (!strcmp(argv[i], "--clocksteps")) clockSteps = atoi(argv[++i]); //else if (!strcmp(argv[i], "--snapshot")) snapshotFile = &argv[i]; - + else if (!strcmp(argv[i], "--fps")) FPS = atoi(argv[++i]); + else if (!strcmp(argv[i], "--enable-reload")) documentReload = 1; else { - if (openFile(argv[i])){ - printf("Unknown parameter or file '%s'\nTry '--help' for help\n", argv[i]); + inputFile = argv[i]; + if (openFile()){ + printf("%s: Unknown parameter or file '%s'\nTry '--help' for help\n", argv[0], argv[i]); exit(EXIT_FAILURE); } } @@ -100,48 +96,80 @@ int main(int argc, char *argv[]){ resetSystem(); while (1){ - SDL_Delay(32); + SDL_Delay(1000.f / FPS); pollEvents(); - fpsCount++; - + fpsCount++; updateVideo(); - + if (!singleStep && !halt){ - for (int i = 0; i < (cpuSpeed / FPS) * 2; i++) { + for (int i = 0; i < ((float)cpuSpeed / (float)fpsStore); i++) { step6502(); cpuTicks++; } irq6502(); } - + if ((unsigned)time(NULL) != tickTrigger){ tickTrigger = (unsigned)time(NULL); cpuHealth = (uint8_t)((cpuTicks / (float)cpuSpeed) * 100.); cpuTicks = 0; - fpsStore = fpsCount; - fpsCount = 0; + fpsStore = fpsCount; fpsCount = 0; + + /* Refresh entire video buffer */ + videoModified = 1; } - /* Redraw entire screen every X seconds */ - //if ((unsigned)time(NULL) > renderTrigger + 0){ - renderTrigger = (unsigned)time(NULL); + + uint16_t vStack = 16; + + if (halt) { + drawString("!CPU Haltet!", renderMemory, + SDL_X_SIZE, 0x000000FF, time(NULL) % 2 ? 0xAAAAAAFF : 0xFFFFFFFF, 16, vStack, 2); - if (SDL_UpdateTexture(texture, NULL, renderMemory, (sizeof(uint32_t) * SDL_X_SIZE))) { - fprintf(stderr, "Could not update SDL texture: %s.\n", SDL_GetError()); - exit(EXIT_FAILURE); - } - if (SDL_RenderCopy(renderer, texture, NULL, NULL)) { - fprintf(stderr, "Could not display SDL texture: %s.\n", SDL_GetError()); - exit(EXIT_FAILURE); - } - SDL_RenderPresent(renderer); - //} + vStack += (FONT_HEIGHT * 2 * 2); + } if (showDebug){ + sprintf(debugString, "LOADED BINARY: \"%s\"\nSCREENSIZE: %dx%d\nFPS: %d\nSCALE: %d", inputFile, SDL_X_SIZE, SDL_Y_SIZE, fpsStore, (int)displayScale); + drawString(debugString, renderMemory, + SDL_X_SIZE, 0x000000FF, 0xAAAAAAFF, 16, vStack, 2); + + vStack += (FONT_HEIGHT * 4 * 2); + + sprintf(debugString, "CPU: %d%%", cpuHealth); + drawString(debugString, renderMemory, + SDL_X_SIZE, 0x000000FF, time(NULL) % 2 ? 0xAAAAAAFF : cpuHealth < 10 ? + 0xFF0000FF : cpuHealth < 50 ? 0xFFFF00FF : 0xAAAAAAFF, 16, vStack, 2); + + vStack += (FONT_HEIGHT * 2 * 2); + } if (displayMmap){ + sprintf(debugString, "RAM: $%04X - $%04X\nROM: $%04X - $%04X\nVIAADDRS: $%04X\nKBDADDRS: $%04X\nVIDADDRS: $%04X", RAMLOC, RAMLOC + RAMSIZE, ROMLOC, ROMLOC + ROMSIZE, VIAADDRS, KBDADDRS, VIDADDRS); + drawString(debugString, renderMemory, SDL_X_SIZE, 0x000000FF, 0xAAAAAAFF, 16, vStack, 2); + + vStack += (FONT_HEIGHT * 6 * 2); + } if (showHelp) { + drawString(HELPKEYS, renderMemory, SDL_X_SIZE, 0x000000FF, 0xAAAAAAFF, 16, vStack, 2); + + vStack += (FONT_HEIGHT * 2 * 2); + } + + + + if (SDL_UpdateTexture(texture, NULL, renderMemory, (sizeof(uint32_t) * SDL_X_SIZE))) { + fprintf(stderr, "Could not update SDL texture: %s.\n", SDL_GetError()); + exit(EXIT_FAILURE); + } + if (SDL_RenderCopy(renderer, texture, NULL, NULL)) { + fprintf(stderr, "Could not display SDL texture: %s.\n", SDL_GetError()); + exit(EXIT_FAILURE); + } + + SDL_RenderPresent(renderer); } return EXIT_SUCCESS; } +