diff --git a/Makefile b/Makefile index 9cb7b1a..b58f699 100644 --- a/Makefile +++ b/Makefile @@ -6,6 +6,7 @@ CFILES = src/main.c \ src/bus.c \ src/cpu.c \ src/framebuffer.c \ + src/mouse.c \ src/screen.c $(TARGET): $(CFILES) diff --git a/src/bus.c b/src/bus.c index 208b3ec..3a3cac7 100644 --- a/src/bus.c +++ b/src/bus.c @@ -11,14 +11,16 @@ #include "bus.h" #include "cpu.h" #include "framebuffer.h" +#include "mouse.h" extern fox32_vm_t vm; +extern mouse_t mouse; int bus_io_read(void *user, uint32_t *value, uint32_t port) { (void) user; switch (port) { case 0x80000000 ... 0x8000031F: { // overlay port - uint8_t overlay_number = (port & 0x000000FF); + uint8_t overlay_number = port & 0x000000FF; uint8_t setting = (port & 0x0000FF00) >> 8; switch (setting) { case 0x00: { @@ -49,6 +51,26 @@ int bus_io_read(void *user, uint32_t *value, uint32_t port) { break; }; + + case 0x80000400 ... 0x80000401: { // mouse port + uint8_t setting = port & 0x000000FF; + switch (setting) { + case 0x00: { + // button states + if (mouse.clicked) *value |= 0b001; + if (mouse.released) *value |= 0b010; + if (mouse.held) *value |= 0b100; else *value &= !0b100; + break; + }; + case 0x01: { + // position + *value = (mouse.y << 16) | mouse.x; + break; + }; + } + + break; + }; } return 0; @@ -97,6 +119,27 @@ int bus_io_write(void *user, uint32_t value, uint32_t port) { break; }; + + case 0x80000400 ... 0x80000401: { // mouse port + uint8_t setting = port & 0x000000FF; + switch (setting) { + case 0x00: { + // button states + mouse.clicked = value & 0b001; + mouse.released = value & 0b010; + mouse.held = value & 0b100; + break; + }; + case 0x01: { + // position + mouse.x = value & 0x0000FFFF; + mouse.y = (value & 0xFFFF0000) >> 16; + break; + }; + } + + break; + }; } return 0; diff --git a/src/main.c b/src/main.c index 245189a..f223494 100644 --- a/src/main.c +++ b/src/main.c @@ -10,6 +10,7 @@ #include "bus.h" #include "cpu.h" #include "framebuffer.h" +#include "mouse.h" #include "screen.h" #include "../fox32rom.h" @@ -46,9 +47,9 @@ int main(int argc, char *argv[]) { draw_framebuffer, 0, 0, - 0, - 0, - 0 + mouse_pressed, + mouse_released, + mouse_moved ); ScreenInit(); diff --git a/src/mouse.c b/src/mouse.c new file mode 100644 index 0000000..7f22c79 --- /dev/null +++ b/src/mouse.c @@ -0,0 +1,36 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "mouse.h" +#include "screen.h" + +mouse_t mouse; + +void mouse_moved(int dx, int dy) { + mouse.x += dx; + mouse.y += dy; + + if (mouse.x > 0x8000) mouse.x = 0; + if (mouse.x > 640) mouse.x = 640; + if (mouse.y > 0x8000) mouse.y = 0; + if (mouse.y > 480) mouse.y = 480; +} + +void mouse_pressed(int button) { + (void) button; // TODO: check which button was clicked + mouse.clicked = true; + mouse.held = true; +} + +void mouse_released(int button) { + (void) button; // TODO: check which button was released + mouse.released = true; + mouse.held = false; +} diff --git a/src/mouse.h b/src/mouse.h new file mode 100644 index 0000000..36fcd7d --- /dev/null +++ b/src/mouse.h @@ -0,0 +1,12 @@ +#pragma once + +typedef struct { + uint16_t x, y; + bool clicked; + bool released; + bool held; +} mouse_t; + +void mouse_moved(int dx, int dy); +void mouse_pressed(int button); +void mouse_released(int button); diff --git a/src/screen.c b/src/screen.c index 24372ec..3a300e1 100644 --- a/src/screen.c +++ b/src/screen.c @@ -111,7 +111,7 @@ int ScreenProcessEvents() { case SDL_MOUSEMOTION: { if (ScreenMouseGrabbed) { if (MainScreen.MouseMoved) - MainScreen.MouseMoved(&MainScreen, event.motion.xrel, event.motion.yrel); + MainScreen.MouseMoved(event.motion.xrel, event.motion.yrel); } break; } @@ -127,14 +127,14 @@ int ScreenProcessEvents() { } if (MainScreen.MousePressed) - MainScreen.MousePressed(&MainScreen, event.button.button); + MainScreen.MousePressed(event.button.button); break; } case SDL_MOUSEBUTTONUP: { if (MainScreen.MouseReleased) - MainScreen.MouseReleased(&MainScreen, event.button.button); + MainScreen.MouseReleased(event.button.button); break; } @@ -149,12 +149,12 @@ int ScreenProcessEvents() { } if (MainScreen.KeyPressed) - MainScreen.KeyPressed(&MainScreen, event.key.keysym.scancode); + MainScreen.KeyPressed(event.key.keysym.scancode); break; case SDL_KEYUP: if (MainScreen.KeyReleased) - MainScreen.KeyReleased(&MainScreen, event.key.keysym.scancode); + MainScreen.KeyReleased(event.key.keysym.scancode); break; } } diff --git a/src/screen.h b/src/screen.h index 2eb4328..70d3cdc 100644 --- a/src/screen.h +++ b/src/screen.h @@ -5,11 +5,11 @@ struct Screen; typedef void (*ScreenDrawF)(struct Screen *screen); -typedef void (*ScreenKeyPressedF)(struct Screen *screen, int sdlscancode); -typedef void (*ScreenKeyReleasedF)(struct Screen *screen, int sdlscancode); -typedef void (*ScreenMousePressedF)(struct Screen *screen, int button); -typedef void (*ScreenMouseReleasedF)(struct Screen *screen, int button); -typedef void (*ScreenMouseMovedF)(struct Screen *screen, int dx, int dy); +typedef void (*ScreenKeyPressedF)(int sdl_scancode); +typedef void (*ScreenKeyReleasedF)(int sdl_scancode); +typedef void (*ScreenMousePressedF)(int button); +typedef void (*ScreenMouseReleasedF)(int button); +typedef void (*ScreenMouseMovedF)(int dx, int dy); struct Screen { int Width;