Fix option parsing, add -i, fix debugger
diff --git a/CMakeLists.txt b/CMakeLists.txt
index a913e68..c672846 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -12,4 +12,5 @@
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})
endif()
-add_executable(emu-6502 main.c cpu.c cpu.h dbg.c dbg.h instructions.h)
+add_executable(6502 main.c cpu.c cpu.h dbg.c dbg.h instructions.h)
+target_link_libraries(6502 readline)
\ No newline at end of file
diff --git a/dbg.c b/dbg.c
index bdc2b31..f5ca437 100644
--- a/dbg.c
+++ b/dbg.c
@@ -11,10 +11,13 @@
while (true)
{
char *input = readline("\033[33m> \033[0m");
- if (!input)
+ if (!input || !*input)
continue;
char *tok = strtok(input, " \t\r\n\v");
+
+ if (!tok || !*tok)
+ continue;
if (!strcmp(tok, "step") || !strcmp(tok, "s"))
{
@@ -29,22 +32,34 @@
{
uint16_t addr = strtol(tok + 1, &ok, 16);
- if (ok == 0)
- {
- printf("Memory:\n");
- printf("\t$%x %x\n", addr, cpu->mem[addr]);
+ printf("Memory:\n");
+ printf("\t$%x %x\n", addr, cpu->mem[addr]);
+ if (addr < 0xFFFF)
printf("\t$%x %x\n", addr + 1, cpu->mem[addr + 1]);
- }
}
else
{
- printf("Registers:\n");
-
- #define R(r) printf("\t" #r ": %x\n", cpu->regs[r]);
- REGISTERS
- #undef R
+ printf("Expected an address as an argument in the form "
+ "$1234, not %s\n", tok);
}
}
+ else
+ {
+ printf("Registers:\n");
+
+ #define R(r) printf("\t" #r ":\t%x\n", cpu->regs[r]);
+ REGISTERS
+ #undef R
+ }
+ }
+ else if (!strcmp(tok, "quit") || !strcmp(tok, "exit"))
+ {
+ printf("Bye\n");
+ return;
+ }
+ else
+ {
+ printf("Unknown command %s\n", tok);
}
add_history(input);
diff --git a/main.c b/main.c
index 8e6ec52..5ab918d 100644
--- a/main.c
+++ b/main.c
@@ -1,5 +1,7 @@
#include "cpu.h"
+#include "dbg.h"
+#include <bits/getopt_core.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
@@ -7,44 +9,71 @@
int main(int argc, char **argv)
{
- printf("6502 Emulator\n");
-
- uint8_t disflag = 0,
+ bool disflag = 0,
runflag = 0,
- helpflag = 0;
+ helpflag = 0,
+ debugflag = 0,
+ should_read = 0;
+
+ FILE *input = stdin;
char c;
- while ((c = getopt(argc, argv, "drh")) != -1)
+ while ((c = getopt(argc, argv, "Ddrhi:")) != -1)
{
switch (c)
{
- case 'd':
- disflag = 1;
- break;
- case 'r':
- runflag = 1;
- break;
- case 'h':
- case '?':
- helpflag = 1;
- break;
+ case 'd':
+ disflag = true;
+ should_read = true;
+ break;
+ case 'r':
+ runflag = true;
+ should_read = true;
+ break;
+ case 'D':
+ debugflag = true;
+ should_read = true;
+ break;
+ case 'i':
+ input = fopen(optarg, "r");
+ break;
+ case 'h':
+ case '?':
+ helpflag = 1;
+ break;
}
}
if (helpflag)
{
- printf("-r to run, -d to disassemble");
+ printf("6502 emulator, disassembler and debugger\n"
+ "Usage:\n"
+ " -d disassemble input\n"
+ " -r run input\n"
+ " -D debug input (open debug prompt)\n"
+ " -i <input> set input file, defaults to standard input\n"
+ " -h, -? show this help page\n");
return 0;
}
- cpu_t cpu = new_cpu();
- fread(cpu.mem, 0xFFFF, 1, stdin);
+ cpu_t cpu;
+
+ if (should_read)
+ {
+ cpu = new_cpu();
+ fread(cpu.mem, 0xFFFF, 1, input);
+ }
if (disflag)
{
disas(&cpu);
}
-
- free_cpu(&cpu);
+ else if (debugflag)
+ {
+ debug(&cpu);
+ }
+
+ if (should_read)
+ free_cpu(&cpu);
}