diff --git a/include/emu/lc3.h b/include/emu/lc3.h index 55a7eee..1aa667c 100644 --- a/include/emu/lc3.h +++ b/include/emu/lc3.h @@ -75,8 +75,8 @@ * These values are somewhat arbitrary, so tweak to your liking! Don't go below * 1 because it might cause problems. */ -#define MEM_DELAY 300 -#define DISP_DELAY 5000 +#define MEM_DELAY 1 +#define DISP_DELAY 1 /* * LC-3 data types. diff --git a/include/lc3tools.h b/include/lc3tools.h index 09e068d..291049e 100644 --- a/include/lc3tools.h +++ b/include/lc3tools.h @@ -22,6 +22,16 @@ #ifndef __LC3TOOLS_H #define __LC3TOOLS_H +#ifndef _WIN32 +/* we'll assume POSIX... */ +#include +#include +#include +#else +#include +#include +#endif + /* * Debug build switch. * Uncomment to build lc3tools with debugging info. diff --git a/src/emu/cpu.c b/src/emu/cpu.c index 4e1fd4d..8eee357 100644 --- a/src/emu/cpu.c +++ b/src/emu/cpu.c @@ -23,6 +23,7 @@ #include #include +#include #include #include #include diff --git a/src/emu/disp.c b/src/emu/disp.c index f10cb5c..f04d402 100644 --- a/src/emu/disp.c +++ b/src/emu/disp.c @@ -43,13 +43,19 @@ void disp_reset(void) void disp_tick(void) { + unsigned char c; + if (disp.c > 0) { disp.c--; } if (!RD() && disp.c == 0) { - putc(disp.ddr & 0xFF, stdout); - fflush(stdout); + c = disp.ddr & 0xFF; + if (c != '\0') + { + putc(c, stdout); + fflush(stdout); + } SET_RD(1); } diff --git a/src/emu/kbd.c b/src/emu/kbd.c index be1be1e..6fd9077 100644 --- a/src/emu/kbd.c +++ b/src/emu/kbd.c @@ -22,9 +22,8 @@ #include #include #include -#include -#include +#include #include #include @@ -36,8 +35,8 @@ static struct lc3kbd kbd; -static int kbhit(void); -static int getch(void); +static int kbd_hit(void); +static int read_char(void); void kbd_reset(void) { @@ -51,8 +50,8 @@ void kbd_tick(void) { unsigned char c; - if (kbhit()) { - c = getch(); + if (kbd_hit()) { + c = read_char(); if (c == 3) { printf("CTRL+C pressed!\r\n"); exit(127); @@ -86,8 +85,9 @@ void set_kbdr(lc3word value) kbd.kbdr = value; } -static int kbhit(void) +static int kbd_hit(void) { +#ifndef _WIN32 struct timeval tv = { 0L, 0L }; fd_set fds; @@ -95,10 +95,14 @@ static int kbhit(void) FD_SET(STDIN_FILENO, &fds); return select(1, &fds, NULL, NULL, &tv); +#else + return _kbhit(); +#endif } -static int getch(void) +static int read_char(void) { +#ifndef _WIN32 int r; unsigned char c; @@ -107,4 +111,7 @@ static int getch(void) } return c; +#else + return _getch(); +#endif } diff --git a/src/emu/main.c b/src/emu/main.c index c92f852..2bb51bf 100644 --- a/src/emu/main.c +++ b/src/emu/main.c @@ -23,9 +23,8 @@ #include #include #include -#include -#include +#include #include #include #include @@ -87,8 +86,6 @@ * --version */ -static struct termios orig_termios; - static inline void dev_tick(void); static void write_word(lc3word addr, lc3word data); @@ -96,8 +93,15 @@ static void fill_mem(lc3word addr, const lc3word *data, int n); static void usage(const char *prog_name); -static void set_nonblock(void); -static void reset_terminal(void); +static void enter_raw_mode(void); +static void leave_raw_mode(void); +static void register_hooks(void); + +#ifndef _WIN32 +static struct termios orig_termios; +#else +static DWORD fdwSaveOldMode; +#endif #define OS_ADDR 0x0400 #define DISP_ISR 0x0500 @@ -178,8 +182,8 @@ const lc3word isr4_code[] = int main(int argc, char *argv[]) { - /* Put terminal into raw mode */ - set_nonblock(); + register_hooks(); + enter_raw_mode(); /* Reset machine state */ mem_reset(); @@ -234,20 +238,54 @@ static void usage(const char *prog_name) printf("Run '%s --help' for options.\n", prog_name); } -static void set_nonblock(void) +static void enter_raw_mode(void) { +#ifndef _WIN32 struct termios term; tcgetattr(STDIN_FILENO, &orig_termios); memcpy(&term, &orig_termios, sizeof(struct termios)); - atexit(reset_terminal); cfmakeraw(&term); tcsetattr(STDIN_FILENO, TCSANOW, &term); +#else + HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE); + + if (hStdin == INVALID_HANDLE_VALUE) + { + printf("GetStdHandle (%d)\r\n", GetLastError()); + exit(127); + } + if (!GetConsoleMode(hStdin, &fdwSaveOldMode)) + { + printf("GetConsoleMode (%d)\r\n", GetLastError()); + exit(127); + } + if (!SetConsoleMode(hStdin, 0)) + { + printf("SetConsoleMode (%d)\r\n", GetLastError()); + exit(127); + } +#endif } -static void reset_terminal(void) +static void leave_raw_mode(void) { - cpu_dumpregs(); +#ifndef _WIN32 tcsetattr(STDIN_FILENO, TCSANOW, &orig_termios); +#else + HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE); + + if (!SetConsoleMode(hStdin, fdwSaveOldMode)) + { + printf("SetConsoleMode (%d)\r\n", GetLastError()); + exit(127); + } +#endif +} + +static void register_hooks() +{ + atexit(leave_raw_mode); + atexit(cpu_dumpregs); }