diff --git a/.gitignore b/.gitignore index 3a1ca78..adcd2ed 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ *.o 6502-emu +memdump diff --git a/6502-emu.c b/6502-emu.c new file mode 100644 index 0000000..c8a9c51 --- /dev/null +++ b/6502-emu.c @@ -0,0 +1,71 @@ +#include +#include +#include +#include + +#include "6502.h" +#include "6850.h" + +struct termios oldTermios; + +void step_delay() +{ + struct timespec req, rem; + + req.tv_sec = 0; + req.tv_nsec = STEP_DURATION; + + nanosleep(&req, &rem); +} + +void run_cpu() +{ + for (;;) { + // CPU timing is currently very far from being cycle-accurate + for (int i = 0; i < (CPU_FREQ / (ONE_SECOND / STEP_DURATION)); i++) { + step_cpu(); + step_uart(); + } + step_delay(); // remove this for more speed + } +} + +void restore_stdin() +{ + tcsetattr(0, TCSANOW, &oldTermios); +} + +void raw_stdin() +{ + struct termios newTermios; + + tcgetattr(0, &oldTermios); + newTermios = oldTermios; + cfmakeraw(&newTermios); + tcsetattr(0, TCSANOW, &newTermios); + atexit(restore_stdin); +} + +int main(int argc, char *argv[]) +{ + if (argc != 2) { + printf("Usage: %s file.rom\n", argv[0]); + printf("The first 16k of \"file.rom\" is loaded into the last 16k of memory.\n"); + return EXIT_FAILURE; + } + + if (load_rom(argv[1]) != 0) { + printf("Error loading \"%s\".\n", argv[1]); + return EXIT_FAILURE; + } + + raw_stdin(); // allow individual keystrokes to be detected + + init_tables(); + init_uart(); + + reset_cpu(); + run_cpu(); + + return EXIT_SUCCESS; +} diff --git a/Makefile b/Makefile index 84c4f12..d1b5061 100644 --- a/Makefile +++ b/Makefile @@ -1,22 +1,17 @@ -CC=gcc -CFLAGS=-Wall -Ofast -std=gnu99 +CFLAGS = -Wall -Ofast -std=gnu99 +LDFLAGS = -Ofast + +OBJ := 6502-emu.o 6502.o 6850.o all: 6502-emu -6502-emu: main.o 6502.o 6850.o - $(CC) -o 6502-emu main.c 6502.o 6850.o +debug: CFLAGS += -DDEBUG +debug: 6502-emu -main.o: main.c - $(CC) $(CFLAGS) -c main.c - -6502.o: 6502.c - $(CC) $(CFLAGS) -c 6502.c - -6850.o: 6850.c - $(CC) $(CFLAGS) -c 6850.c +6502-emu: $(OBJ) clean: - rm *.o 6502-emu + $(RM) 6502-emu $(OBJ) test: 6502-emu ./6502-emu examples/ehbasic.rom