LS7-Emulator/src/main.c
2024-12-17 13:10:45 +01:00

149 lines
3.9 KiB
C

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <SFML/System.h>
#include <SFML/Graphics.h>
#include <SFML/Window.h>
#include "config.h"
#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;
uint8_t halt = 0;
uint8_t showHelp = 0;
uint8_t showDebug = 0;
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 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);
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);
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);
resetSystem();
while (sfRenderWindow_isOpen(window)){
pollEvents(window);
if (!singlestep && !halt){
for (int i = 0; i < cpuspeed / FPS; i++) step6502();
irq6502();
}
sfRenderWindow_clear(window, BACKCOLOR);
updateVideo();
drawScreen();
sfRenderWindow_display(window);
}
sfRenderWindow_destroy(window);
return EXIT_SUCCESS;
}