Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/backend/esp32/joybus.c
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ static void IRAM_ATTR handle_command_response(const uint8_t *response, uint8_t l
}

// Frequency-independent bit decode: '1' is short low + long high, '0' the reverse
static inline uint8_t decode_bit(rmt_symbol_word_t sym)
static inline IRAM_ATTR uint8_t decode_bit(rmt_symbol_word_t sym)
{
return sym.duration0 < sym.duration1;
}
Expand Down
5 changes: 4 additions & 1 deletion src/n64_pak_fs.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#include <stddef.h>
#include <string.h>

#include <joybus/attributes.h>
#include <joybus/errors.h>
#include <joybus/common/n64_pak_fs.h>

// Offsets of the four ID copies in page 0
#define ID_COPIES 4
static const uint16_t id_offsets[ID_COPIES] = {0x20, 0x60, 0x80, 0xC0};
static JOYBUS_RAM_DATA const uint16_t id_offsets[ID_COPIES] = {0x20, 0x60, 0x80, 0xC0};

// ID block layout, 32 bytes with the checksums covering the first 28
#define ID_SIZE 32
Expand Down Expand Up @@ -106,6 +107,7 @@ bool joybus_n64_pak_fs_valid(const uint8_t *bank0, uint8_t banks)
return false;
}

JOYBUS_RAM_FUNC
bool joybus_n64_pak_fs_is_id_block(uint16_t addr)
{
for (int i = 0; i < ID_COPIES; i++) {
Expand All @@ -116,6 +118,7 @@ bool joybus_n64_pak_fs_is_id_block(uint16_t addr)
return false;
}

JOYBUS_RAM_FUNC
uint8_t joybus_n64_pak_fs_id_banks(const uint8_t block[JOYBUS_N64_PAK_BLOCK_SIZE])
{
return block[ID_BANKS];
Expand Down
59 changes: 33 additions & 26 deletions src/target/gcn_controller.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
* Pack a "full" input state into a "short" 8-byte input state, depending on the
* analog mode. See enum joybus_gcn_analog_mode for more details.
*/
static inline uint8_t *pack_input_state(uint8_t *dest, const struct joybus_gcn_controller_state *src,
enum joybus_gcn_analog_mode analog_mode)
JOYBUS_RAM_FUNC
static uint8_t *pack_input_state(uint8_t *dest, const struct joybus_gcn_controller_state *src,
enum joybus_gcn_analog_mode analog_mode)
{
// Copy button and stick data
memcpy(dest, src, 4);
Expand Down Expand Up @@ -53,7 +54,8 @@ static inline uint8_t *pack_input_state(uint8_t *dest, const struct joybus_gcn_c
}

// Set or clear the "need origin" flag in the input state and device ID
static inline void set_need_origin(struct joybus_target_gcn_controller *controller, bool need_origin)
JOYBUS_RAM_FUNC
static void set_need_origin(struct joybus_target_gcn_controller *controller, bool need_origin)
{
// Always set the need_origin flag in the input state
if (need_origin) {
Expand All @@ -79,8 +81,8 @@ static inline void set_need_origin(struct joybus_target_gcn_controller *controll
* Response: A 3-byte controller ID
*/
JOYBUS_RAM_FUNC
static inline int handle_reset(struct joybus_target_gcn_controller *controller, const uint8_t *command,
uint8_t bytes_read, joybus_target_response_cb send_response, void *user_data)
static int handle_reset(struct joybus_target_gcn_controller *controller, const uint8_t *command, uint8_t bytes_read,
joybus_target_response_cb send_response, void *user_data)
{
// Respond with the controller ID
send_response((const uint8_t *)&controller->id, JOYBUS_CMD_RESET_RX, user_data);
Expand All @@ -103,8 +105,8 @@ static inline int handle_reset(struct joybus_target_gcn_controller *controller,
* Response: A 3-byte controller ID
*/
JOYBUS_RAM_FUNC
static inline int handle_identify(struct joybus_target_gcn_controller *controller, const uint8_t *command,
uint8_t bytes_read, joybus_target_response_cb send_response, void *user_data)
static int handle_identify(struct joybus_target_gcn_controller *controller, const uint8_t *command, uint8_t bytes_read,
joybus_target_response_cb send_response, void *user_data)
{
// Respond with the controller ID
send_response((const uint8_t *)&controller->id, JOYBUS_CMD_IDENTIFY_RX, user_data);
Expand All @@ -119,8 +121,8 @@ static inline int handle_identify(struct joybus_target_gcn_controller *controlle
* Response: An 8-byte packed input state, see `pack_input_state` for details
*/
JOYBUS_RAM_FUNC
static inline int handle_read(struct joybus_target_gcn_controller *controller, const uint8_t *command,
uint8_t bytes_read, joybus_target_response_cb send_response, void *user_data)
static int handle_read(struct joybus_target_gcn_controller *controller, const uint8_t *command, uint8_t bytes_read,
joybus_target_response_cb send_response, void *user_data)
{
// We can respond after the first two bytes
if (bytes_read == 2) {
Expand All @@ -145,8 +147,8 @@ static inline int handle_read(struct joybus_target_gcn_controller *controller, c
controller->input.buttons |= JOYBUS_GCN_USE_ORIGIN;

// Get the previous motor state
uint8_t last_motor_state = (controller->id.status & JOYBUS_STATUS_GCN_MOTOR_STATE_MASK) >>
JOYBUS_STATUS_GCN_MOTOR_STATE_SHIFT;
uint8_t last_motor_state =
(controller->id.status & JOYBUS_STATUS_GCN_MOTOR_STATE_MASK) >> JOYBUS_STATUS_GCN_MOTOR_STATE_SHIFT;

// Save the analog mode and motor state
joybus_id_clear_status_flags(&controller->id,
Expand All @@ -168,8 +170,9 @@ static inline int handle_read(struct joybus_target_gcn_controller *controller, c
* Command: {0x41}
* Response: A 10-byte input state representing the current origin.
*/
static inline int handle_read_origin(struct joybus_target_gcn_controller *controller, const uint8_t *command,
uint8_t bytes_read, joybus_target_response_cb send_response, void *user_data)
JOYBUS_RAM_FUNC
static int handle_read_origin(struct joybus_target_gcn_controller *controller, const uint8_t *command,
uint8_t bytes_read, joybus_target_response_cb send_response, void *user_data)
{
// Respond with the controller origin
send_response((uint8_t *)&controller->origin, JOYBUS_CMD_GCN_READ_ORIGIN_RX, user_data);
Expand All @@ -186,8 +189,9 @@ static inline int handle_read_origin(struct joybus_target_gcn_controller *contro
* Command: {0x42, 0x00, 0x00}
* Response: A 10-byte input state representing the current origin.
*/
static inline int handle_calibrate(struct joybus_target_gcn_controller *controller, const uint8_t *command,
uint8_t bytes_read, joybus_target_response_cb send_response, void *user_data)
JOYBUS_RAM_FUNC
static int handle_calibrate(struct joybus_target_gcn_controller *controller, const uint8_t *command, uint8_t bytes_read,
joybus_target_response_cb send_response, void *user_data)
{
// We can respond after the first byte
if (bytes_read == 1) {
Expand All @@ -212,8 +216,9 @@ static inline int handle_calibrate(struct joybus_target_gcn_controller *controll
*
* NOTE: This command is not used by any games, but is included for completeness.
*/
static inline int handle_read_long(struct joybus_target_gcn_controller *controller, const uint8_t *command,
uint8_t bytes_read, joybus_target_response_cb send_response, void *user_data)
JOYBUS_RAM_FUNC
static int handle_read_long(struct joybus_target_gcn_controller *controller, const uint8_t *command, uint8_t bytes_read,
joybus_target_response_cb send_response, void *user_data)
{
// We can respond after the second byte is read
if (bytes_read == 2) {
Expand All @@ -233,8 +238,8 @@ static inline int handle_read_long(struct joybus_target_gcn_controller *controll
controller->input.buttons |= JOYBUS_GCN_USE_ORIGIN;

// Get the previous motor state
uint8_t last_motor_state = (controller->id.status & JOYBUS_STATUS_GCN_MOTOR_STATE_MASK) >>
JOYBUS_STATUS_GCN_MOTOR_STATE_SHIFT;
uint8_t last_motor_state =
(controller->id.status & JOYBUS_STATUS_GCN_MOTOR_STATE_MASK) >> JOYBUS_STATUS_GCN_MOTOR_STATE_SHIFT;

// Save the analog mode and motor state
joybus_id_clear_status_flags(&controller->id,
Expand Down Expand Up @@ -264,17 +269,18 @@ static inline int handle_read_long(struct joybus_target_gcn_controller *controll
* Command: {0x4D, 0x??, 0x??} - 2nd and 3rd bytes seem to differ every time
* Response: 8 bytes of zeroes.
*/
static inline int handle_probe_device(struct joybus_target_gcn_controller *controller, const uint8_t *command,
uint8_t bytes_read, joybus_target_response_cb send_response, void *user_data)
JOYBUS_RAM_FUNC
static int handle_probe_device(struct joybus_target_gcn_controller *controller, const uint8_t *command,
uint8_t bytes_read, joybus_target_response_cb send_response, void *user_data)
{
if (bytes_read == 1) {
// Don't respond to probe commands if we already received data from a controller
if (controller->id.type & JOYBUS_TYPE_GCN_WIRELESS_RECEIVED)
return -JOYBUS_ERR_NOT_SUPPORTED;

// Respond with 8 bytes of zeroes
static const uint8_t zeroes[8] = {0};
send_response(zeroes, JOYBUS_CMD_GCN_PROBE_DEVICE_RX, user_data);
memset(controller->packed_input, 0, JOYBUS_CMD_GCN_PROBE_DEVICE_RX);
send_response(controller->packed_input, JOYBUS_CMD_GCN_PROBE_DEVICE_RX, user_data);
}

return JOYBUS_CMD_GCN_PROBE_DEVICE_TX - bytes_read;
Expand All @@ -288,8 +294,9 @@ static inline int handle_probe_device(struct joybus_target_gcn_controller *contr
* Command: {0x4E, wireless_id_h | 0x10, wireless_id_l}
* Response: A 3-byte controller ID
*/
static inline int handle_fix_device(struct joybus_target_gcn_controller *controller, const uint8_t *command,
uint8_t bytes_read, joybus_target_response_cb send_response, void *user_data)
JOYBUS_RAM_FUNC
static int handle_fix_device(struct joybus_target_gcn_controller *controller, const uint8_t *command,
uint8_t bytes_read, joybus_target_response_cb send_response, void *user_data)
{
if (bytes_read == JOYBUS_CMD_GCN_FIX_DEVICE_TX) {
// Extract the wireless ID from the command
Expand Down Expand Up @@ -336,7 +343,7 @@ static int gcn_controller_byte_received(struct joybus_target *target, const uint
return -JOYBUS_ERR_NOT_SUPPORTED;
}

static const struct joybus_target_api gcn_controller_api = {
static JOYBUS_RAM_DATA const struct joybus_target_api gcn_controller_api = {
.byte_received = gcn_controller_byte_received,
};

Expand Down
7 changes: 4 additions & 3 deletions src/target/n64_controller.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// Helper to check if an pak is currently read for pak read/write commands
// An pak is "ready" when it is present and pak changed flag has been cleared
JOYBUS_RAM_FUNC
static inline bool pak_ready(struct joybus_target_n64_controller *controller)
static bool pak_ready(struct joybus_target_n64_controller *controller)
{
return controller->pak && !joybus_id_n64_pak_changed(&controller->id);
}
Expand Down Expand Up @@ -159,7 +159,8 @@ static int handle_pak_read(struct joybus_target_n64_controller *controller, cons

if (ready) {
// Calculate and append the data checksum
controller->response[JOYBUS_N64_PAK_BLOCK_SIZE] = joybus_data_checksum(controller->response, JOYBUS_N64_PAK_BLOCK_SIZE);
controller->response[JOYBUS_N64_PAK_BLOCK_SIZE] =
joybus_data_checksum(controller->response, JOYBUS_N64_PAK_BLOCK_SIZE);
} else {
// Prepare a zero response with the "no pak" CRC, which the host treats as a transfer error
memset(controller->response, 0, JOYBUS_CMD_N64_PAK_READ_RX);
Expand Down Expand Up @@ -249,7 +250,7 @@ static int n64_controller_byte_received(struct joybus_target *target, const uint
return -JOYBUS_ERR_NOT_SUPPORTED;
}

static const struct joybus_target_api n64_controller_api = {
static JOYBUS_RAM_DATA const struct joybus_target_api n64_controller_api = {
.byte_received = n64_controller_byte_received,
};

Expand Down
2 changes: 1 addition & 1 deletion src/target/n64_pak_controller.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ static int memory_write_block(struct joybus_target_n64_pak_controller *pak_contr
return 0;
}

static const struct joybus_target_n64_pak_api pak_controller_api = {
static JOYBUS_RAM_DATA const struct joybus_target_n64_pak_api pak_controller_api = {
.read_block = pak_controller_read_block,
.write_block = pak_controller_write_block,
};
Expand Down
2 changes: 1 addition & 1 deletion src/target/n64_pak_rumble.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ static int pak_rumble_write_block(struct joybus_target_n64_pak *pak, uint16_t ad
return 0;
}

static const struct joybus_target_n64_pak_api pak_rumble_api = {
static JOYBUS_RAM_DATA const struct joybus_target_n64_pak_api pak_rumble_api = {
.read_block = pak_rumble_read_block,
.write_block = pak_rumble_write_block,
};
Expand Down
2 changes: 1 addition & 1 deletion src/target/pixelfx_gameid.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ static int pixelfx_gameid_byte_received(struct joybus_target *target, const uint
return 0;
}

static const struct joybus_target_api pixelfx_gameid_api = {
static JOYBUS_RAM_DATA const struct joybus_target_api pixelfx_gameid_api = {
.byte_received = pixelfx_gameid_byte_received,
};

Expand Down
Loading