LS7-Emulator/src/main.c

149 lines
3.9 KiB
C
Raw Normal View History

2024-12-02 14:06:05 +01:00
#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"
2024-12-05 11:45:05 +01:00
sfVertexArray *renderArray;
sfRenderWindow *window;
2024-12-17 13:10:45 +01:00
sfFont *font;
sfText *renderText;
2024-12-05 11:45:05 +01:00
sfRenderStates renderStates;
2024-12-17 13:10:45 +01:00
sfRectangleShape *haltRect;
2024-12-05 11:45:05 +01:00
2024-12-17 13:10:45 +01:00
uint8_t halt = 0;
uint8_t showHelp = 0;
uint8_t showDebug = 0;
2024-12-02 14:06:05 +01:00
2024-12-17 13:10:45 +01:00
int openFile(const char *inputFile){
2024-12-02 14:06:05 +01:00
FILE *file = fopen(inputFile, "rb");
2024-12-17 13:10:45 +01:00
int retCode = 0;
2024-12-02 14:06:05 +01:00
if (file != NULL){
fseek(file, 0, SEEK_END);
long filelen = ftell(file);
rewind(file);
2024-12-17 13:10:45 +01:00
if (fread(rom, filelen, 1, file) != 1) retCode = 1;
} else retCode = 1;
2024-12-02 14:06:05 +01:00
fclose(file);
2024-12-17 13:10:45 +01:00
return retCode;
2024-12-02 14:06:05 +01:00
}
void resetSystem(){
initVideo();
updateVideo();
reset6502();
}
void updateRenderStates(){
renderStates.transform = sfTransform_Identity;
2024-12-17 13:10:45 +01:00
sfTransform_scale(&renderStates.transform, displayscale, displayscale);
2024-12-02 14:06:05 +01:00
}
void drawScreen(){
sfRenderWindow_drawVertexArray(window, renderArray, &renderStates);
2024-12-17 13:10:45 +01:00
if (halt){
sfRenderWindow_drawRectangleShape(window, haltRect, &renderStates);
}
if (showHelp){
sfText_setString(renderText, HELPKEYS);
sfRenderWindow_drawText(window, renderText, &renderStates);
}
2024-12-02 14:06:05 +01:00
}
2024-12-17 13:10:45 +01:00
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);
}
}
2024-12-02 14:31:43 +01:00
}
2024-12-17 13:10:45 +01:00
}
int main(int argc, char *argv[]){
fetchArgs(argc, argv);
2024-12-02 14:31:43 +01:00
2024-12-02 14:06:05 +01:00
window = sfRenderWindow_create((sfVideoMode){800, 600, 24}, "LS7 Emulator", sfResize | sfClose, NULL);
sfRenderWindow_setVerticalSyncEnabled(window, sfFalse);
2024-12-17 13:10:45 +01:00
sfRenderWindow_setFramerateLimit(window, FPS);
2024-12-02 14:06:05 +01:00
sfRenderWindow_setKeyRepeatEnabled(window, sfFalse);
2024-12-17 13:10:45 +01:00
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 });
2024-12-02 14:06:05 +01:00
renderStates = sfRenderStates_default();
2024-12-17 13:10:45 +01:00
sfTransform_scale(&renderStates.transform, displayscale, displayscale);
2024-12-02 14:06:05 +01:00
resetSystem();
2024-12-05 11:45:05 +01:00
2024-12-02 14:06:05 +01:00
while (sfRenderWindow_isOpen(window)){
pollEvents(window);
2024-12-17 13:10:45 +01:00
if (!singlestep && !halt){
for (int i = 0; i < cpuspeed / FPS; i++) step6502();
irq6502();
}
2024-12-02 14:06:05 +01:00
sfRenderWindow_clear(window, BACKCOLOR);
updateVideo();
drawScreen();
sfRenderWindow_display(window);
}
sfRenderWindow_destroy(window);
2024-12-17 13:10:45 +01:00
return EXIT_SUCCESS;
2024-12-02 14:06:05 +01:00
}