From 741561787cc2df547d5f520a142beaff234c488e Mon Sep 17 00:00:00 2001 From: "Juan J. Martinez" Date: Tue, 9 May 2023 22:43:06 +0100 Subject: Implement controller 1 (keyboard) Exposed via port. --- tr8vm.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) (limited to 'tr8vm.c') diff --git a/tr8vm.c b/tr8vm.c index 107b54b..fdede7c 100644 --- a/tr8vm.c +++ b/tr8vm.c @@ -113,6 +113,50 @@ uint8_t read_m(uint16_t addr) return ram[addr]; } +#define CTL_FIRE1 (1 << 0) +#define CTL_FIRE2 (1 << 1) +#define CTL_UP (1 << 2) +#define CTL_DOWN (1 << 3) +#define CTL_LEFT (1 << 4) +#define CTL_RIGHT (1 << 5) +#define CTL_SELECT (1 << 6) +#define CTL_START (1 << 7) + +uint8_t ctl[2] = { 0 }; + +static void update_ctl(SDL_Event *ev) +{ + switch (ev->key.keysym.sym) + { + case SDLK_z: + ctl[0] = ev->type == SDL_KEYDOWN ? ctl[0] | CTL_FIRE1 : ctl[0] & ~CTL_FIRE1; + break; + case SDLK_x: + ctl[0] = ev->type == SDL_KEYDOWN ? ctl[0] | CTL_FIRE2 : ctl[0] & ~CTL_FIRE2; + break; + case SDLK_UP: + ctl[0] = ev->type == SDL_KEYDOWN ? ctl[0] | CTL_UP : ctl[0] & ~CTL_UP; + break; + case SDLK_DOWN: + ctl[0] = ev->type == SDL_KEYDOWN ? ctl[0] | CTL_DOWN : ctl[0] & ~CTL_DOWN; + break; + case SDLK_LEFT: + ctl[0] = ev->type == SDL_KEYDOWN ? ctl[0] | CTL_LEFT : ctl[0] & ~CTL_LEFT; + break; + case SDLK_RIGHT: + ctl[0] = ev->type == SDL_KEYDOWN ? ctl[0] | CTL_RIGHT : ctl[0] & ~CTL_RIGHT; + break; + case SDLK_s: + ctl[0] = ev->type == SDL_KEYDOWN ? ctl[0] | CTL_START : ctl[0] & ~CTL_START; + break; + case SDLK_d: + ctl[0] = ev->type == SDL_KEYDOWN ? ctl[0] | CTL_SELECT : ctl[0] & ~CTL_SELECT; + break; + default: + break; + } +} + uint8_t blt_set = 0; uint8_t blt_paramc = 0; uint8_t blt_param[6] = { 0 }; @@ -192,6 +236,14 @@ uint8_t port(uint8_t p, uint8_t v) blt_param[blt_paramc++] = v; return p; + /* controller 1 */ + case 0xf0: + return ctl[0]; + + /* controller 2 */ + case 0xf1: + return ctl[1]; + default: return 0xff; } @@ -317,7 +369,11 @@ int main(int argc, char *argv[]) fullscreen ^= 1; resize_full_screen(screen, renderer, fullscreen, &dst); } + update_ctl(&ev); } + + if (ev.type == SDL_KEYUP) + update_ctl(&ev); } pitch = 0; -- cgit v1.2.3