Files
6502PC/src/main.c
T
2026-08-31 04:43:52 +02:00

88 lines
2.1 KiB
C

#include "cpu.h"
#include "ram.h"
#include "rom.h"
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <time.h>
#include <unistd.h>
#define MACHINE_SPEED_HZ 1e6 // 1 MHz;
static struct termios orig_termios;
static struct timespec sleepTime;
void restore_terminal(void) { tcsetattr(STDIN_FILENO, TCSANOW, &orig_termios); }
void set_nonblocking_input(void) {
struct termios raw;
tcgetattr(STDIN_FILENO, &orig_termios);
atexit(restore_terminal);
raw = orig_termios;
raw.c_lflag &= ~(ICANON | ECHO); // disable line buffering and echo
raw.c_cc[VMIN] = 0; // don't block waiting for input
raw.c_cc[VTIME] = 0;
tcsetattr(STDIN_FILENO, TCSANOW, &raw);
// make stdin reads non-blocking too
int flags = fcntl(STDIN_FILENO, F_GETFL, 0);
fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK);
}
int main(void) {
uint8_t *ram = NULL;
CPU cpu;
set_nonblocking_input();
initRom(rom);
if (createRAM(&ram) != 0) {
printf("Could not create RAM\n");
return 1;
}
/* DEBUG */
//FILE *fp = fopen("/Users/Kili2/Documents/Projekte/6502pc/6502_65C02_functional_tests/6502_functional_test.bin", "rb");
//FILE *fp = fopen("/Users/Kili2/Documents/Projekte/6502pc/6502_65C02_functional_tests/6502_decimal_test.bin", "rb");
FILE *fp = fopen("/mnt/c/Users/llego/source/repos/6502PC/6502_65C02_functional_tests/6502_functional_test.bin", "rb");
//FILE *fp = fopen("/mnt/c/Users/llego/source/repos/6502PC/6502_65C02_functional_tests/6502_decimal_test.bin", "rb");
if (!fp) {
perror("fopen");
return 1;
}
fread(ram, 1, RAM_SIZE, fp);
if (ferror(fp)) {
perror("fread");
fclose(fp);
return 1;
}
fclose(fp);
/* DEBUG END*/
initCPU(&cpu);
resetCPU(&cpu, rom);
while (1) {
uint8_t sleep = runCycle(&cpu, ram, rom);
if (sleep == 1) {
printf("opcode missing: 0x%X\n", ram[cpu.ProgrammeCounter - 1]);
return 1;
}
sleepTime.tv_sec = 0;
sleepTime.tv_nsec = sleep * (1000 / MACHINE_SPEED_HZ);
nanosleep(&sleepTime, NULL);
}
destroyRAM(ram);
return 0;
}