From c777421c7903fa2128ed052c5ec56f8dc0f968d6 Mon Sep 17 00:00:00 2001 From: 0xmac Date: Tue, 7 Jan 2025 14:35:39 +0100 Subject: [PATCH] Updated main.c for SDL --- src/main.c | 133 ++++++++++++++++++++++++++--------------------------- 1 file changed, 66 insertions(+), 67 deletions(-) diff --git a/src/main.c b/src/main.c index e7a06de..5910172 100644 --- a/src/main.c +++ b/src/main.c @@ -4,28 +4,38 @@ #include #include -#include -#include -#include - +#include #include "config.h" + +/* Preset Values */ +static float displayScale = 2; +static int cpuSpeed = 100000; // 100 kHz +static int singleStep = 0; +static int clockSteps = 1; + +#include "sdlinit.c" #include "events.c" #include "cpu.c" #include "memory.c" #include "video.c" #include "keyboard.c" - - -sfVertexArray *renderArray; -sfRenderWindow *window; -sfFont *font; -sfText *renderText; -sfRenderStates renderStates; -sfRectangleShape *haltRect; +//#include "via.c" uint8_t halt = 0; uint8_t showHelp = 0; uint8_t showDebug = 0; +uint8_t fpsCount = 0; + +uint8_t fpsStore; +uint8_t cpuHealth; + +unsigned long cpuTicks = 0; +unsigned long tickTrigger = 0; +unsigned long renderTrigger = 0; + + + +char debugString[255]; int openFile(const char *inputFile){ FILE *file = fopen(inputFile, "rb"); @@ -50,23 +60,6 @@ void resetSystem(){ reset6502(); } -void updateRenderStates(){ - renderStates.transform = sfTransform_Identity; - sfTransform_scale(&renderStates.transform, displayscale, displayscale); -} - -void drawScreen(){ - sfRenderWindow_drawVertexArray(window, renderArray, &renderStates); - if (halt){ - sfRenderWindow_drawRectangleShape(window, haltRect, &renderStates); - } - if (showHelp){ - sfText_setString(renderText, HELPKEYS); - - sfRenderWindow_drawText(window, renderText, &renderStates); - } -} - void writeHelp(int type){ if (!type) printf(HELP); else printf(HELPKEYS); @@ -84,10 +77,10 @@ void fetchArgs(int argc, char *argv[]){ for (int i = 1; i < argc; i++) { if (!strcmp(argv[i], "--help")) writeHelp(0); else if (!strcmp(argv[i], "--help-keys")) writeHelp(1); - 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], "--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 { @@ -100,47 +93,53 @@ void fetchArgs(int argc, char *argv[]){ } int main(int argc, char *argv[]){ - fetchArgs(argc, argv); - - window = sfRenderWindow_create((sfVideoMode){800, 600, 24}, "LS7 Emulator", sfResize | sfClose, NULL); - sfRenderWindow_setVerticalSyncEnabled(window, sfFalse); - sfRenderWindow_setFramerateLimit(window, FPS); - sfRenderWindow_setKeyRepeatEnabled(window, sfFalse); - - font = sfFont_createFromFile("bin/font.ttf"); - - renderText = sfText_create(); - sfText_setPosition(renderText, (sfVector2f){ 50, 50 }); - sfText_setFont(renderText, font); - sfText_setCharacterSize(renderText, 10); - sfText_setFillColor(renderText, sfWhite); - sfText_setOutlineColor(renderText, sfBlack); - sfText_setOutlineThickness(renderText, 1); - - haltRect = sfRectangleShape_create(); - sfRectangleShape_setFillColor(haltRect, (sfColor){ 0, 0, 0, 128 }); - sfRectangleShape_setSize(haltRect, (sfVector2f){ 384, 256 }); - - renderStates = sfRenderStates_default(); - sfTransform_scale(&renderStates.transform, displayscale, displayscale); + fetchArgs(argc, argv); + initWindow(); + clearScreen(renderMemory, 0x000000FF); resetSystem(); - - while (sfRenderWindow_isOpen(window)){ - pollEvents(window); + + while (1){ + SDL_Delay(32); + pollEvents(); - if (!singlestep && !halt){ - for (int i = 0; i < cpuspeed / FPS; i++) step6502(); + fpsCount++; + + updateVideo(); + + if (!singleStep && !halt){ + for (int i = 0; i < (cpuSpeed / FPS) * 2; i++) { + step6502(); + cpuTicks++; + } irq6502(); } - sfRenderWindow_clear(window, BACKCOLOR); - updateVideo(); - drawScreen(); - sfRenderWindow_display(window); + if ((unsigned)time(NULL) != tickTrigger){ + tickTrigger = (unsigned)time(NULL); + + cpuHealth = (uint8_t)((cpuTicks / (float)cpuSpeed) * 100.); + cpuTicks = 0; + + fpsStore = fpsCount; + fpsCount = 0; + } + + /* Redraw entire screen every X seconds */ + //if ((unsigned)time(NULL) > renderTrigger + 0){ + renderTrigger = (unsigned)time(NULL); + + 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); + //} } - - sfRenderWindow_destroy(window); return EXIT_SUCCESS; }