Skip to content

Commit

Permalink
Merge pull request #59 from justinmeiners/duplicate-windows-code
Browse files Browse the repository at this point in the history
Remove duplicate windows code
  • Loading branch information
justinmeiners authored Jul 15, 2022
2 parents 485d3b1 + a19407d commit 540ea01
Show file tree
Hide file tree
Showing 6 changed files with 355 additions and 416 deletions.
482 changes: 220 additions & 262 deletions docs/index.html

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions docs/src/lc3-alt-win.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
#include <stdint.h> // uint16_t
#include <stdio.h> // FILE
#include <signal.h> // SIGINT
#include <stdio.h>
#include <stdint.h>
#include <signal.h>
/* windows only */
#include <Windows.h>
#include <conio.h> // _kbhit

HANDLE hStdin = INVALID_HANDLE_VALUE;

enum
{
R_R0 = 0,
Expand Down Expand Up @@ -119,10 +117,6 @@ int read_image(const char* image_path)
fclose(file);
return 1;
}
uint16_t check_key()
{
return WaitForSingleObject(hStdin, 1000) == WAIT_OBJECT_0 && _kbhit();
}
void mem_write(uint16_t address, uint16_t val)
{
memory[address] = val;
Expand All @@ -144,6 +138,7 @@ uint16_t mem_read(uint16_t address)
}
return memory[address];
}
HANDLE hStdin = INVALID_HANDLE_VALUE;
DWORD fdwMode, fdwOldMode;

void disable_input_buffering()
Expand All @@ -162,6 +157,11 @@ void restore_input_buffering()
{
SetConsoleMode(hStdin, fdwOldMode);
}

uint16_t check_key()
{
return WaitForSingleObject(hStdin, 1000) == WAIT_OBJECT_0 && _kbhit();
}
void handle_interrupt(int signal)
{
restore_input_buffering();
Expand Down
29 changes: 14 additions & 15 deletions docs/src/lc3-alt.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <signal.h>
/* unix */
/* unix only */
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>

#include <sys/time.h>
#include <sys/types.h>
#include <sys/termios.h>
Expand Down Expand Up @@ -124,17 +122,6 @@ int read_image(const char* image_path)
fclose(file);
return 1;
}
uint16_t check_key()
{
fd_set readfds;
FD_ZERO(&readfds);
FD_SET(STDIN_FILENO, &readfds);

struct timeval timeout;
timeout.tv_sec = 0;
timeout.tv_usec = 0;
return select(1, &readfds, NULL, NULL, &timeout) != 0;
}
void mem_write(uint16_t address, uint16_t val)
{
memory[address] = val;
Expand Down Expand Up @@ -170,6 +157,18 @@ void restore_input_buffering()
{
tcsetattr(STDIN_FILENO, TCSANOW, &original_tio);
}

uint16_t check_key()
{
fd_set readfds;
FD_ZERO(&readfds);
FD_SET(STDIN_FILENO, &readfds);

struct timeval timeout;
timeout.tv_sec = 0;
timeout.tv_usec = 0;
return select(1, &readfds, NULL, NULL, &timeout) != 0;
}
void handle_interrupt(int signal)
{
restore_input_buffering();
Expand Down
18 changes: 9 additions & 9 deletions docs/src/lc3-win.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
#include <stdint.h> // uint16_t
#include <stdio.h> // FILE
#include <signal.h> // SIGINT
#include <stdio.h>
#include <stdint.h>
#include <signal.h>
/* windows only */
#include <Windows.h>
#include <conio.h> // _kbhit

HANDLE hStdin = INVALID_HANDLE_VALUE;

enum
{
R_R0 = 0,
Expand Down Expand Up @@ -119,10 +117,6 @@ int read_image(const char* image_path)
fclose(file);
return 1;
}
uint16_t check_key()
{
return WaitForSingleObject(hStdin, 1000) == WAIT_OBJECT_0 && _kbhit();
}
void mem_write(uint16_t address, uint16_t val)
{
memory[address] = val;
Expand All @@ -144,6 +138,7 @@ uint16_t mem_read(uint16_t address)
}
return memory[address];
}
HANDLE hStdin = INVALID_HANDLE_VALUE;
DWORD fdwMode, fdwOldMode;

void disable_input_buffering()
Expand All @@ -162,6 +157,11 @@ void restore_input_buffering()
{
SetConsoleMode(hStdin, fdwOldMode);
}

uint16_t check_key()
{
return WaitForSingleObject(hStdin, 1000) == WAIT_OBJECT_0 && _kbhit();
}
void handle_interrupt(int signal)
{
restore_input_buffering();
Expand Down
29 changes: 14 additions & 15 deletions docs/src/lc3.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <signal.h>
/* unix */
/* unix only */
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>

#include <sys/time.h>
#include <sys/types.h>
#include <sys/termios.h>
Expand Down Expand Up @@ -123,17 +121,6 @@ int read_image(const char* image_path)
fclose(file);
return 1;
}
uint16_t check_key()
{
fd_set readfds;
FD_ZERO(&readfds);
FD_SET(STDIN_FILENO, &readfds);

struct timeval timeout;
timeout.tv_sec = 0;
timeout.tv_usec = 0;
return select(1, &readfds, NULL, NULL, &timeout) != 0;
}
void mem_write(uint16_t address, uint16_t val)
{
memory[address] = val;
Expand Down Expand Up @@ -169,6 +156,18 @@ void restore_input_buffering()
{
tcsetattr(STDIN_FILENO, TCSANOW, &original_tio);
}

uint16_t check_key()
{
fd_set readfds;
FD_ZERO(&readfds);
FD_SET(STDIN_FILENO, &readfds);

struct timeval timeout;
timeout.tv_sec = 0;
timeout.tv_usec = 0;
return select(1, &readfds, NULL, NULL, &timeout) != 0;
}
void handle_interrupt(int signal)
{
restore_input_buffering();
Expand Down
Loading

0 comments on commit 540ea01

Please sign in to comment.