Skip to content

Commit

Permalink
Merge branch 'master' into lc3as
Browse files Browse the repository at this point in the history
  • Loading branch information
whampson committed Jun 14, 2019
2 parents 2506ef6 + 624b6b2 commit 824ef5d
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 24 deletions.
4 changes: 2 additions & 2 deletions include/emu/lc3.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
10 changes: 10 additions & 0 deletions include/lc3tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@
#ifndef __LC3TOOLS_H
#define __LC3TOOLS_H

#ifndef _WIN32
/* we'll assume POSIX... */
#include <termios.h>
#include <unistd.h>
#include <sys/select.h>
#else
#include <conio.h>
#include <windows.h>
#endif

/*
* Debug build switch.
* Uncomment to build lc3tools with debugging info.
Expand Down
1 change: 1 addition & 0 deletions src/emu/cpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <stdio.h>
#include <string.h>

#include <lc3.h>
#include <cpu.h>
#include <state.h>
#include <mem.h>
Expand Down
10 changes: 8 additions & 2 deletions src/emu/disp.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
23 changes: 15 additions & 8 deletions src/emu/kbd.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/select.h>

#include <lc3tools.h>
#include <kbd.h>
#include <pic.h>

Expand All @@ -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)
{
Expand All @@ -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);
Expand Down Expand Up @@ -86,19 +85,24 @@ 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;

FD_ZERO(&fds);
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;

Expand All @@ -107,4 +111,7 @@ static int getch(void)
}

return c;
#else
return _getch();
#endif
}
62 changes: 50 additions & 12 deletions src/emu/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>

#include <lc3tools.h>
#include <lc3.h>
#include <cpu.h>
#include <mem.h>
Expand Down Expand Up @@ -87,17 +86,22 @@
* --version
*/

static struct termios orig_termios;

static inline void dev_tick(void);

static void write_word(lc3word addr, lc3word data);
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
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
}

0 comments on commit 824ef5d

Please sign in to comment.