LS7-Emulator/src/main.c
2025-01-07 14:35:39 +01:00

148 lines
3.6 KiB
C

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <SDL2/SDL.h>
#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"
//#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");
int retCode = 0;
if (file != NULL){
fseek(file, 0, SEEK_END);
long filelen = ftell(file);
rewind(file);
if (fread(rom, filelen, 1, file) != 1) retCode = 1;
} else retCode = 1;
fclose(file);
return retCode;
}
void resetSystem(){
initVideo();
updateVideo();
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();
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], "--snapshot")) snapshotFile = &argv[i];
else {
if (openFile(argv[i])){
printf("Unknown parameter or file '%s'\nTry '--help' for help\n", argv[i]);
exit(EXIT_FAILURE);
}
}
}
}
int main(int argc, char *argv[]){
fetchArgs(argc, argv);
initWindow();
clearScreen(renderMemory, 0x000000FF);
resetSystem();
while (1){
SDL_Delay(32);
pollEvents();
fpsCount++;
updateVideo();
if (!singleStep && !halt){
for (int i = 0; i < (cpuSpeed / FPS) * 2; 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;
}
/* 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);
//}
}
return EXIT_SUCCESS;
}