libjoybus
Joybus implementation for 32-bit MCUs
Loading...
Searching...
No Matches
commands.h
Go to the documentation of this file.
1
6#pragma once
7
8#include <stdint.h>
9
10// Some commands (EEPROM read/write, RTC read/write) are only used by the internal
11// N64 joybus line, but we are including them here for completeness.
12
13// Joybus command codes
14#define JOYBUS_CMD_RESET 0xFF
15#define JOYBUS_CMD_IDENTIFY 0x00
16#define JOYBUS_CMD_N64_READ 0x01
17#define JOYBUS_CMD_N64_ACCESSORY_READ 0x02
18#define JOYBUS_CMD_N64_ACCESSORY_WRITE 0x03
19#define JOYBUS_CMD_N64_EEPROM_READ 0x04
20#define JOYBUS_CMD_N64_EEPROM_WRITE 0x05
21#define JOYBUS_CMD_N64_RTC_INFO 0x06
22#define JOYBUS_CMD_N64_RTC_READ 0x07
23#define JOYBUS_CMD_N64_RTC_WRITE 0x08
24#define JOYBUS_CMD_N64_KEYBOARD_READ 0x13
25#define JOYBUS_CMD_GBA_READ 0x14
26#define JOYBUS_CMD_GBA_WRITE 0x15
27#define JOYBUS_CMD_PIXELFX_GAMEID 0x1D
28#define JOYBUS_CMD_GCN_READ 0x40
29#define JOYBUS_CMD_GCN_READ_ORIGIN 0x41
30#define JOYBUS_CMD_GCN_CALIBRATE 0x42
31#define JOYBUS_CMD_GCN_READ_LONG 0x43
32#define JOYBUS_CMD_GCN_PROBE_DEVICE 0x4D
33#define JOYBUS_CMD_GCN_FIX_DEVICE 0x4E
34#define JOYBUS_CMD_GCN_KEYBOARD_READ 0x54
35
36// Joybus command transfer lengths
37#define JOYBUS_CMD_RESET_TX 1
38#define JOYBUS_CMD_RESET_RX 3
39#define JOYBUS_CMD_IDENTIFY_TX 1
40#define JOYBUS_CMD_IDENTIFY_RX 3
41#define JOYBUS_CMD_N64_READ_TX 1
42#define JOYBUS_CMD_N64_READ_RX 4
43#define JOYBUS_CMD_N64_ACCESSORY_READ_TX 3
44#define JOYBUS_CMD_N64_ACCESSORY_READ_RX 33
45#define JOYBUS_CMD_N64_ACCESSORY_WRITE_TX 35
46#define JOYBUS_CMD_N64_ACCESSORY_WRITE_RX 1
47#define JOYBUS_CMD_N64_EEPROM_READ_TX 2
48#define JOYBUS_CMD_N64_EEPROM_READ_RX 8
49#define JOYBUS_CMD_N64_EEPROM_WRITE_TX 10
50#define JOYBUS_CMD_N64_EEPROM_WRITE_RX 1
51#define JOYBUS_CMD_N64_RTC_INFO_TX 1
52#define JOYBUS_CMD_N64_RTC_INFO_RX 3
53#define JOYBUS_CMD_N64_RTC_READ_TX 2
54#define JOYBUS_CMD_N64_RTC_READ_RX 9
55#define JOYBUS_CMD_N64_RTC_WRITE_TX 10
56#define JOYBUS_CMD_N64_RTC_WRITE_RX 1
57#define JOYBUS_CMD_N64_KEYBOARD_READ_TX 2
58#define JOYBUS_CMD_N64_KEYBOARD_READ_RX 7
59#define JOYBUS_CMD_GBA_READ_TX 3
60#define JOYBUS_CMD_GBA_READ_RX 33
61#define JOYBUS_CMD_GBA_WRITE_TX 35
62#define JOYBUS_CMD_GBA_WRITE_RX 1
63#define JOYBUS_CMD_PIXELFX_GAMEID_TX 11
64#define JOYBUS_CMD_PIXELFX_GAMEID_RX 0
65#define JOYBUS_CMD_GCN_READ_TX 3
66#define JOYBUS_CMD_GCN_READ_RX 8
67#define JOYBUS_CMD_GCN_READ_ORIGIN_TX 1
68#define JOYBUS_CMD_GCN_READ_ORIGIN_RX 10
69#define JOYBUS_CMD_GCN_CALIBRATE_TX 3
70#define JOYBUS_CMD_GCN_CALIBRATE_RX 10
71#define JOYBUS_CMD_GCN_READ_LONG_TX 3
72#define JOYBUS_CMD_GCN_READ_LONG_RX 10
73#define JOYBUS_CMD_GCN_PROBE_DEVICE_TX 3
74#define JOYBUS_CMD_GCN_PROBE_DEVICE_RX 8
75#define JOYBUS_CMD_GCN_FIX_DEVICE_TX 3
76#define JOYBUS_CMD_GCN_FIX_DEVICE_RX 3
77#define JOYBUS_CMD_GCN_KEYBOARD_READ_TX 3
78#define JOYBUS_CMD_GCN_KEYBOARD_READ_RX 8
79
80// N64 identify type values
81#define JOYBUS_ID_N64_VRU 0x0001
82#define JOYBUS_ID_N64_KEYBOARD 0x0002
83#define JOYBUS_ID_N64_GBA_LINK 0x0004
84#define JOYBUS_ID_N64_MOUSE 0x0200
85#define JOYBUS_ID_N64_CONTROLLER 0x0500
86
87// N64 identify status values (standard controllers)
88#define JOYBUS_ID_N64_ACCESSORY_PRESENT 0x01
89#define JOYBUS_ID_N64_ACCESSORY_ABSENT 0x02
90#define JOYBUS_ID_N64_ACCESSORY_CHANGED 0x03
91
92// N64 identify status values (VRU)
93#define JOYBUS_ID_N64_VRU_UNINITIALIZED 0x00
94#define JOYBUS_ID_N64_VRU_INITIALIZED 0x01
95
96// GCN identify type flags
97#define JOYBUS_ID_GCN_WIRELESS_ID_FIXED 0x0010
98#define JOYBUS_ID_GCN_WIRELESS_ORIGIN 0x0020
99#define JOYBUS_ID_GCN_STANDARD 0x0100
100#define JOYBUS_ID_GCN_WIRELESS_STATE 0x0200
101#define JOYBUS_ID_GCN_DEVICE 0x0800
102#define JOYBUS_ID_GCN_NO_MOTOR 0x2000
103#define JOYBUS_ID_GCN_WIRELESS_RECEIVED 0x4000
104#define JOYBUS_ID_GCN_WIRELESS 0x8000
105
106// GCN identify status flags (non-wireless controllers)
107#define JOYBUS_ID_GCN_ANALOG_MODE_MASK 0x07
108#define JOYBUS_ID_GCN_MOTOR_STATE_MASK 0x18
109#define JOYBUS_ID_GCN_NEED_ORIGIN 0x20
110#define JOYBUS_ID_GCN_ERROR_LATCHED 0x40
111#define JOYBUS_ID_GCN_ERROR 0x80
112#define JOYBUS_ID_GCN_MOTOR_STATE_SHIFT 3
113#define JOYBUS_ID_GCN_ANALOG_MODE_SHIFT 0
114
116#define JOYBUS_GAMECUBE_CONTROLLER (JOYBUS_ID_GCN_DEVICE | JOYBUS_ID_GCN_STANDARD)
117
119#define JOYBUS_WAVEBIRD_RECEIVER (JOYBUS_ID_GCN_DEVICE | JOYBUS_ID_GCN_WIRELESS | JOYBUS_ID_GCN_NO_MOTOR)
120
127static inline uint16_t joybus_id_get_type(const uint8_t *id)
128{
129 return (uint16_t)((id[0] << 8) | id[1]);
130}
131
138static inline void joybus_id_clear_type_flags(uint8_t *id, uint16_t flags)
139{
140 id[0] &= ~(flags >> 8);
141 id[1] &= ~(flags & 0xFF);
142}
143
150static inline void joybus_id_set_type_flags(uint8_t *id, uint16_t flags)
151{
152 id[0] |= (flags >> 8);
153 id[1] |= (flags & 0xFF);
154}
155
162static inline uint8_t joybus_id_get_status(const uint8_t *id)
163{
164 return id[2];
165}
166
173static inline void joybus_id_clear_status_flags(uint8_t *id, uint8_t flags)
174{
175 id[2] &= ~flags;
176}
177
184static inline void joybus_id_set_status_flags(uint8_t *id, uint8_t flags)
185{
186 id[2] |= flags;
187}
188
195static inline uint8_t joybus_id_get_wireless_id(const uint8_t *id)
196{
197 return (uint16_t)((id[1] & 0xC0) << 2 | id[2]);
198}
199
206static inline void joybus_id_set_wireless_id(uint8_t *id, uint16_t wireless_id)
207{
208 id[1] = (id[1] & ~0xC0) | ((wireless_id >> 2) & 0xC0);
209 id[2] = wireless_id & 0xFF;
210}
static void joybus_id_set_type_flags(uint8_t *id, uint16_t flags)
Set type flags in an "identify" buffer.
Definition commands.h:150
static void joybus_id_clear_type_flags(uint8_t *id, uint16_t flags)
Clear type flags in an "identify" buffer.
Definition commands.h:138
static uint8_t joybus_id_get_wireless_id(const uint8_t *id)
Get the 10-bit wireless ID from an "identify" buffer.
Definition commands.h:195
static uint8_t joybus_id_get_status(const uint8_t *id)
Get the status byte from an "identify" buffer.
Definition commands.h:162
static uint16_t joybus_id_get_type(const uint8_t *id)
Get the controller type from an "identify" buffer.
Definition commands.h:127
static void joybus_id_set_status_flags(uint8_t *id, uint8_t flags)
Set status flags in an "identify" buffer.
Definition commands.h:184
static void joybus_id_set_wireless_id(uint8_t *id, uint16_t wireless_id)
Set the 10-bit wireless ID in an "identify" buffer.
Definition commands.h:206
static void joybus_id_clear_status_flags(uint8_t *id, uint8_t flags)
Clear status flags in an "identify" buffer.
Definition commands.h:173