@@ -148,13 +148,103 @@ static uint64_t _ria816_update_irq(ria816_t* c, uint64_t pins) {
148148
149149static uint8_t HID_dev = 0 ;
150150
151+ #ifdef NDEBUG
152+ static inline void DBG (const char * fmt , ...) {
153+ (void )fmt ;
154+ }
155+ #else
156+ #include "log.h"
157+ #define DBG (...) LOG_INFO(__VA_ARGS__)
158+ #endif
151159typedef uint32_t DWORD ;
152160#include "hid/hid.c"
153161static void kbd_queue_char (char ch ) {}
154162static void kbd_queue_key (uint8_t modifier , uint8_t keycode , bool initial_press ) {}
155163#include "hid/kbd.c"
156164#include "hid/mou.c"
157165#include "hid/pad.c"
166+ #include <SDL3/SDL.h>
167+
168+ static void pad_synth_report (pad_connection_t * conn , void * data , uint16_t event_type , pad_xram_t * report ) {
169+ DBG ("Type: 0x%X - %p, slot: %d" , event_type , data , conn -> slot );
170+
171+ uint8_t dpad = 0 ;
172+ uint8_t button0 = 0 ;
173+ uint8_t button1 = 0 ;
174+
175+ if (event_type >= SDL_EVENT_JOYSTICK_AXIS_MOTION && event_type <= SDL_EVENT_JOYSTICK_UPDATE_COMPLETE ) {
176+ // Joystick event
177+
178+ report -> lx = SDL_GetJoystickAxis (data , 0 ) / 256 ;
179+ report -> ly = SDL_GetJoystickAxis (data , 1 ) / 256 ;
180+ report -> rx = SDL_GetJoystickAxis (data , 2 ) / 256 ;
181+ report -> ry = SDL_GetJoystickAxis (data , 3 ) / 256 ;
182+ report -> lt = SDL_GetJoystickAxis (data , 4 ) / 256 ;
183+ report -> rt = SDL_GetJoystickAxis (data , 5 ) / 256 ;
184+
185+ uint8_t hat = SDL_GetJoystickHat (data , 0 );
186+ if (hat & SDL_HAT_UP ) dpad |= 1 ;
187+ if (hat & SDL_HAT_DOWN ) dpad |= 2 ;
188+ if (hat & SDL_HAT_LEFT ) dpad |= 4 ;
189+ if (hat & SDL_HAT_RIGHT ) dpad |= 8 ;
190+
191+ uint32_t buttons = 0 ;
192+ for (int i = 0 ; i < PAD_MAX_BUTTONS ; i ++ ) {
193+ if (SDL_GetJoystickButton (data , conn -> button_offsets [i ])) {
194+ buttons |= (1 << i );
195+ }
196+ }
197+ button0 = buttons & 0xFF ;
198+ button1 = (buttons >> 8 ) & 0xFF ;
199+ }
200+ else if (event_type >= SDL_EVENT_GAMEPAD_AXIS_MOTION && event_type <= SDL_EVENT_GAMEPAD_UPDATE_COMPLETE ) {
201+ // Gamepad event
202+
203+ report -> lx = SDL_GetGamepadAxis (data , SDL_GAMEPAD_AXIS_LEFTX ) / 256 ;
204+ report -> ly = SDL_GetGamepadAxis (data , SDL_GAMEPAD_AXIS_LEFTY ) / 256 ;
205+ report -> rx = SDL_GetGamepadAxis (data , SDL_GAMEPAD_AXIS_RIGHTX ) / 256 ;
206+ report -> ry = SDL_GetGamepadAxis (data , SDL_GAMEPAD_AXIS_RIGHTY ) / 256 ;
207+ report -> lt = SDL_GetGamepadAxis (data , SDL_GAMEPAD_AXIS_LEFT_TRIGGER ) / 256 ;
208+ report -> rt = SDL_GetGamepadAxis (data , SDL_GAMEPAD_AXIS_RIGHT_TRIGGER ) / 256 ;
209+
210+ if (SDL_GetGamepadButton (data , SDL_GAMEPAD_BUTTON_DPAD_UP )) dpad |= (1 << 0 );
211+ if (SDL_GetGamepadButton (data , SDL_GAMEPAD_BUTTON_DPAD_DOWN )) dpad |= (1 << 1 );
212+ if (SDL_GetGamepadButton (data , SDL_GAMEPAD_BUTTON_DPAD_LEFT )) dpad |= (1 << 2 );
213+ if (SDL_GetGamepadButton (data , SDL_GAMEPAD_BUTTON_DPAD_RIGHT )) dpad |= (1 << 3 );
214+
215+ if (SDL_GetGamepadButton (data , SDL_GAMEPAD_BUTTON_SOUTH )) button0 |= (1 << 0 ); // A
216+ if (SDL_GetGamepadButton (data , SDL_GAMEPAD_BUTTON_EAST )) button0 |= (1 << 1 ); // B
217+ if (SDL_GetGamepadButton (data , SDL_GAMEPAD_BUTTON_RIGHT_PADDLE1 )) button0 |= (1 << 2 ); // C
218+ if (SDL_GetGamepadButton (data , SDL_GAMEPAD_BUTTON_WEST )) button0 |= (1 << 3 ); // X
219+ if (SDL_GetGamepadButton (data , SDL_GAMEPAD_BUTTON_NORTH )) button0 |= (1 << 4 ); // Y
220+ if (SDL_GetGamepadButton (data , SDL_GAMEPAD_BUTTON_LEFT_PADDLE1 )) button0 |= (1 << 5 ); // Z
221+ if (SDL_GetGamepadButton (data , SDL_GAMEPAD_BUTTON_LEFT_SHOULDER )) button0 |= (1 << 6 ); // L1
222+ if (SDL_GetGamepadButton (data , SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER )) button0 |= (1 << 7 ); // R1
223+
224+ if (SDL_GetGamepadButton (data , SDL_GAMEPAD_BUTTON_BACK )) button1 |= (1 << 2 );
225+ if (SDL_GetGamepadButton (data , SDL_GAMEPAD_BUTTON_START )) button1 |= (1 << 3 );
226+ if (SDL_GetGamepadButton (data , SDL_GAMEPAD_BUTTON_GUIDE )) button1 |= (1 << 4 );
227+ if (SDL_GetGamepadButton (data , SDL_GAMEPAD_BUTTON_LEFT_STICK )) button1 |= (1 << 5 ); // L3
228+ if (SDL_GetGamepadButton (data , SDL_GAMEPAD_BUTTON_RIGHT_STICK )) button1 |= (1 << 6 ); // R3
229+ }
230+
231+ report -> dpad |= dpad & 0x0F ; // only lower 4 bits are dpad
232+ report -> button0 = button0 ;
233+ report -> button1 = button1 ;
234+ DBG ("\t%s: 0x%02X, Sticks: 0x%02X, Buttons: 0x%02X 0x%02X, Sticks: L(%d,%d) R(%d,%d), Triggers: L(%d) R(%d)" ,
235+ SDL_IsGamepad (SDL_GetGamepadID (data )) ? "Gamepad" : "Joystick" ,
236+ report -> dpad ,
237+ report -> sticks ,
238+ report -> button0 ,
239+ report -> button1 ,
240+ report -> lx ,
241+ report -> ly ,
242+ report -> rx ,
243+ report -> ry ,
244+ report -> lt ,
245+ report -> rt );
246+ }
247+
158248uint8_t ria816_hid_read (ria816_t * c , uint8_t reg ) {
159249 uint8_t data = 0xFF ; // invalid
160250 switch (HID_dev & 0xF ) {
0 commit comments