From cccfd1feffba5cab4510ed1d0597a9a7961854ef Mon Sep 17 00:00:00 2001 From: kdx Date: Fri, 14 Apr 2023 03:35:46 +0200 Subject: receive input --- samples/helloworld.orgaasm | 18 +++++++++--------- samples/input.orgaasm | 4 ++++ samples/minimal.orgaasm | 26 +++++++++++++------------- samples/shfthelloworld.orgaasm | 24 ++++++++++++------------ src/orgaemu.c | 31 +++++++++++++++++++++++++++++-- 5 files changed, 67 insertions(+), 36 deletions(-) create mode 100644 samples/input.orgaasm diff --git a/samples/helloworld.orgaasm b/samples/helloworld.orgaasm index b78de23..abc99bc 100644 --- a/samples/helloworld.orgaasm +++ b/samples/helloworld.orgaasm @@ -1,14 +1,14 @@ -LIT ,hellostr -JRT ,putstr -RET + LIT ,hellostr + JRT ,putstr + RET @putstr ( str -- ) - DUP LDA WRT ( write ) - INC ( increment pointer ) - DUP LDA ( loop until end of string ) - JNZ ,putstr - POP -RET + DUP LDA WRT ( write ) + INC ( increment pointer ) + DUP LDA ( loop until end of string ) + JNZ ,putstr + POP + RET ( "hello world" ) @hellostr 0068 0065 006c 006c 006f 0020 0077 006f 0072 006c 0064 000a 0000 diff --git a/samples/input.orgaasm b/samples/input.orgaasm new file mode 100644 index 0000000..a5da548 --- /dev/null +++ b/samples/input.orgaasm @@ -0,0 +1,4 @@ +@loop + SLP + LIT ,Input LDA DBG POP + JMP ,loop diff --git a/samples/minimal.orgaasm b/samples/minimal.orgaasm index 5ae1d7b..c9196ee 100644 --- a/samples/minimal.orgaasm +++ b/samples/minimal.orgaasm @@ -1,13 +1,13 @@ -LIT 0068 WRT ( h ) -LIT 0065 WRT ( e ) -LIT 006c WRT ( l ) -LIT 006c WRT ( l ) -LIT 006f WRT ( o ) -LIT 0020 WRT ( ) -LIT 0077 WRT ( w ) -LIT 006f WRT ( o ) -LIT 0072 WRT ( r ) -LIT 006c WRT ( l ) -LIT 0064 WRT ( d ) -LIT 000a WRT ( \n ) -RET + LIT 0068 WRT ( h ) + LIT 0065 WRT ( e ) + LIT 006c WRT ( l ) + LIT 006c WRT ( l ) + LIT 006f WRT ( o ) + LIT 0020 WRT ( ) + LIT 0077 WRT ( w ) + LIT 006f WRT ( o ) + LIT 0072 WRT ( r ) + LIT 006c WRT ( l ) + LIT 0064 WRT ( d ) + LIT 000a WRT ( \n ) + RET diff --git a/samples/shfthelloworld.orgaasm b/samples/shfthelloworld.orgaasm index 896b165..da77f20 100644 --- a/samples/shfthelloworld.orgaasm +++ b/samples/shfthelloworld.orgaasm @@ -1,17 +1,17 @@ -LIT ,hellostr -JRT ,putstr -RET + LIT ,hellostr + JRT ,putstr + RET @putstr ( str -- ) - DUP LDA ( read memory ) - DUP #00ff AND ( split the short in two ) - SWP #0008 RSF - WRT WRT ( write ) - INC ( increment pointer ) - DUP LDA ( loop until end of string ) - JNZ ,putstr - POP -RET + DUP LDA ( read memory ) + DUP #00ff AND ( split the short in two ) + SWP #0008 RSF + WRT WRT ( write ) + INC ( increment pointer ) + DUP LDA ( loop until end of string ) + JNZ ,putstr + POP + kRET ( "hello world" ) @hellostr 6865 6c6c 6f20 776f 726c 640a 0000 diff --git a/src/orgaemu.c b/src/orgaemu.c index 7de0334..c1c898f 100644 --- a/src/orgaemu.c +++ b/src/orgaemu.c @@ -6,6 +6,25 @@ #define MEM_SIZE 0xffff +static const uint16_t buttons[SDL_NUM_SCANCODES] = { + [SDL_SCANCODE_BACKSPACE] = 1 << 2, + [SDL_SCANCODE_RETURN] = 1 << 3, + /* WASD JK */ + [SDL_SCANCODE_J] = 1 << 0, + [SDL_SCANCODE_K] = 1 << 1, + [SDL_SCANCODE_W] = 1 << 4, + [SDL_SCANCODE_S] = 1 << 5, + [SDL_SCANCODE_A] = 1 << 6, + [SDL_SCANCODE_D] = 1 << 7, + /* arrows XC */ + [SDL_SCANCODE_X] = 1 << 0, + [SDL_SCANCODE_C] = 1 << 1, + [SDL_SCANCODE_UP] = 1 << 4, + [SDL_SCANCODE_DOWN] = 1 << 5, + [SDL_SCANCODE_LEFT] = 1 << 6, + [SDL_SCANCODE_RIGHT] = 1 << 7, +}; + static uint16_t *mem = NULL, *stack = NULL, *rstack = NULL; static size_t stack_ptr = 0, rstack_ptr = 0; static SDL_Window *window = NULL; @@ -38,13 +57,21 @@ get_short(const unsigned char *s) } static int -cycle_events(void) +cycle_events(uint16_t *input) { SDL_Event e; while (SDL_PollEvent(&e)) { switch (e.type) { case SDL_QUIT: return 1; + case SDL_KEYDOWN: + if (e.key.repeat) + break; + *input |= buttons[e.key.keysym.scancode]; + break; + case SDL_KEYUP: + *input &= ~(buttons[e.key.keysym.scancode]); + break; default: break; } @@ -272,7 +299,7 @@ exec_op(uint16_t *mem, long pc) return pc + 1; } case OP_SLP: - if (cycle_events()) + if (cycle_events(mem + 0xbffe)) return MEM_SIZE; render(mem + 0xbfff); return pc + 1; -- cgit v1.2.3