From b48c1665271e08afbb923acff1eca1cb7acbb34d Mon Sep 17 00:00:00 2001 From: empathicqubit Date: Sat, 27 Nov 2021 03:04:48 +0100 Subject: [PATCH] Add joyport command for controller manipulation --- .gitignore | 11 +++++++++++ commands.lua | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ server.lua | 8 ++++++++ 3 files changed, 67 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0f43807 --- /dev/null +++ b/.gitignore @@ -0,0 +1,11 @@ +.DS_Store +*.s?? +*~ +*.log +*.zip +*.env + +*.orig +*.rej + +*.sf-* diff --git a/commands.lua b/commands.lua index 40dc67a..1a06904 100644 --- a/commands.lua +++ b/commands.lua @@ -340,6 +340,51 @@ return function(server) server.response(server.responseType.DISPLAY_GET, server.errorType.OK, command.requestId, table.concat(r)); end + local JOYPORT_BITS = { + UP = 0, + DOWN = 1, + LEFT = 2, + RIGHT = 3, + A = 4, + B = 5, + SELECT = 6, + START = 7, + } + + local function processJoyportSet(command) + if command.length < 4 then + server.errorResponse(server.errorType.CMD_INVALID_LENGTH, command.requestId) + return + end + + local port = server.readUint16(command.body, 1) + local value = server.readUint16(command.body, 3) + + if port > 3 then + server.errorResponse(server.errorType.OBJECT_MISSING, command.requestId) + return + end + + if value > 0xff then + server.errorResponse(server.errorType.INVALID_PARAMETER, command.requestId) + return + end + + local state = { + a = ((value & JOYPORT_BITS.A) ~= 0), + b = ((value & JOYPORT_BITS.B) ~= 0), + select = ((value & JOYPORT_BITS.SELECT) ~= 0), + start = ((value & JOYPORT_BITS.START) ~= 0), + up = ((value & JOYPORT_BITS.UP) ~= 0), + down = ((value & JOYPORT_BITS.DOWN) ~= 0), + left = ((value & JOYPORT_BITS.LEFT) ~= 0), + right = ((value & JOYPORT_BITS.RIGHT) ~= 0), + } + emu.setInput(port, state) + + server.response(server.responseType.JOYPORT_SET, server.errorType.OK, command.requestId, nil); + end + local function processExit(command) server.response(server.responseType.EXIT, server.errorType.OK, command.requestId, nil) @@ -869,6 +914,9 @@ return function(server) elseif ct == server.commandType.DISPLAY_GET then processDisplayGet(command) + elseif ct == server.commandType.JOYPORT_SET then + processJoyportSet(command) + elseif ct == server.commandType.EXIT then processExit(command) elseif ct == server.commandType.QUIT then diff --git a/server.lua b/server.lua index 927ce88..8681ad6 100644 --- a/server.lua +++ b/server.lua @@ -44,6 +44,10 @@ me.commandType = { PALETTE_GET = 0x91, + JOYPORT_SET = 0xa2, + + USERPORT_SET = 0xb2, + EXIT = 0xaa, QUIT = 0xbb, RESET = 0xcc, @@ -86,6 +90,10 @@ me.responseType = { PALETTE_GET = 0x91, + JOYPORT_SET = 0xa2, + + USERPORT_SET = 0xb2, + EXIT = 0xaa, QUIT = 0xbb, RESET = 0xcc,