Cleanup libultra controller routines (#1312)

* Cleanup libultra controller routines

* Correct structure name

* Fixes + Suggestions

* Move READFORMAT macro to controller.h
This commit is contained in:
Tharo
2022-07-30 20:41:32 +01:00
committed by GitHub
parent 2f5eac6b76
commit 97e795fc0e
13 changed files with 302 additions and 255 deletions
+47 -12
View File
@@ -3,6 +3,21 @@
#include "message.h"
/**
* Controller channel
* Each game controller channel has 4 error bits that are defined in bit 6-7 of
* the Rx and Tx data size area bytes. Programmers need to clear these bits
* when setting the Tx/Rx size area values for a channel
*/
#define CHNL_ERR_NORESP 0x80 /* Bit 7 (Rx): No response error */
#define CHNL_ERR_OVERRUN 0x40 /* Bit 6 (Rx): Overrun error */
#define CHNL_ERR_FRAME 0x80 /* Bit 7 (Tx): Frame error */
#define CHNL_ERR_COLLISION 0x40 /* Bit 6 (Tx): Collision error */
#define CHNL_ERR_MASK 0xC0 /* Bit 6-7: channel errors */
#define CHNL_ERR(readFormat) (((readFormat).rxsize & CHNL_ERR_MASK) >> 4)
#define BLOCKSIZE 32
#define MAXCONTROLLERS 4
#define PFS_ONE_PAGE 8
@@ -36,6 +51,7 @@
#define CONT_CMD_NOP 0xFF
#define CONT_CMD_END 0xFE // Indicates end of a command
#define CONT_CMD_EXE 1 // Set pif ram status byte to this to do a command
#define CONT_CMD_SKIP_CHNL 0 // Skip channel
#define CONT_ERR_NO_CONTROLLER PFS_ERR_NOPACK /* 1 */
#define CONT_ERR_CONTRFAIL CONT_OVERRUN_ERROR /* 4 */
@@ -79,6 +95,27 @@
#define CONT_ADDR_CRC_ER 0x04
#define CONT_EEPROM_BUSY 0x80
/* Accessory detection */
#define CONT_ADDR_DETECT 0x8000
// Rumble
#define CONT_ADDR_RUMBLE 0xC000
// Controller Pak / Transfer Pak
#define CONT_ADDR_GB_POWER 0x8000 // Same as the detection address, but semantically different
#define CONT_ADDR_GB_BANK 0xA000
#define CONT_ADDR_GB_STATUS 0xB000
// Addresses sent to controller accessories are in blocks, not bytes
#define CONT_BLOCKS(x) ((x) / BLOCKSIZE)
// Block addresses of the above
#define CONT_BLOCK_DETECT CONT_BLOCKS(CONT_ADDR_DETECT)
#define CONT_BLOCK_RUMBLE CONT_BLOCKS(CONT_ADDR_RUMBLE)
#define CONT_BLOCK_GB_POWER CONT_BLOCKS(CONT_ADDR_GB_POWER)
#define CONT_BLOCK_GB_BANK CONT_BLOCKS(CONT_ADDR_GB_BANK)
#define CONT_BLOCK_GB_STATUS CONT_BLOCKS(CONT_ADDR_GB_STATUS)
/* Buttons */
#define BTN_CRIGHT 0x0001
#define BTN_CLEFT 0x0002
@@ -124,49 +161,47 @@ typedef struct {
/* 0x26 */ u8 errno;
} OSContRamIo; // size = 0x28
// Original name: __OSContRequesFormat
typedef struct {
/* 0x00 */ u8 align;
/* 0x01 */ u8 txsize;
/* 0x02 */ u8 rxsize;
/* 0x03 */ u8 poll;
/* 0x03 */ u8 cmd;
/* 0x04 */ u8 typeh;
/* 0x05 */ u8 typel;
/* 0x06 */ u8 status;
/* 0x07 */ u8 align1;
} __OSContRequestHeader; // size = 0x8
} __OSContRequesFormat; // size = 0x8
// Original name: __OSContRequesHeaderFormatShort
typedef struct {
/* 0x00 */ u8 txsize;
/* 0x01 */ u8 rxsize;
/* 0x02 */ u8 poll;
/* 0x02 */ u8 cmd;
/* 0x03 */ u8 typeh;
/* 0x04 */ u8 typel;
/* 0x05 */ u8 status;
} __OSContRequestHeaderAligned; // size = 0x6
} __OSContRequesFormatShort; // size = 0x6
// Original Name: __OSContRamReadFormat
typedef struct {
/* 0x00 */ u8 unk_00;
/* 0x01 */ u8 txsize;
/* 0x02 */ u8 rxsize;
/* 0x03 */ u8 poll;
/* 0x03 */ u8 cmd;
/* 0x04 */ u8 hi;
/* 0x05 */ u8 lo;
/* 0x06 */ u8 data[BLOCKSIZE];
/* 0x26 */ u8 datacrc;
} __OSContRamHeader; // size = 0x27
} __OSContRamReadFormat; // size = 0x27
#define READFORMAT(ptr) ((__OSContRamReadFormat*)(ptr))
// Original name: __OSContReadFormat
typedef struct {
/* 0x00 */ u8 align;
/* 0x01 */ u8 txsize;
/* 0x02 */ u8 rxsize;
/* 0x03 */ u8 poll;
/* 0x03 */ u8 cmd;
/* 0x04 */ u16 button;
/* 0x06 */ s8 joyX;
/* 0x07 */ s8 joyY;
} __OSContReadHeader; // size = 0x8
} __OSContReadFormat; // size = 0x8
#endif
+5 -5
View File
@@ -77,16 +77,16 @@ typedef struct {
/* 0x2C */ u8 label[32];
/* 0x4C */ s32 version;
/* 0x50 */ s32 dir_size;
/* 0x54 */ s32 inode_table; /* block location */
/* 0x58 */ s32 minode_table; /* mirrioring inode_table */
/* 0x5C */ s32 dir_table; /* block location */
/* 0x60 */ s32 inodeStartPage; /* page # */
/* 0x54 */ s32 inode_table; /* block location */
/* 0x58 */ s32 minode_table; /* mirroring inode_table */
/* 0x5C */ s32 dir_table; /* block location */
/* 0x60 */ s32 inodeStartPage; /* page # */
/* 0x64 */ u8 banks;
/* 0x65 */ u8 activebank;
} OSPfs; // size = 0x68
typedef struct {
/* 0x00 */ u32 file_size; /* bytes */
/* 0x00 */ u32 file_size; /* bytes */
/* 0x04 */ u32 game_code;
/* 0x08 */ u16 company_code;
/* 0x0C */ char ext_name[4];
+3 -3
View File
@@ -226,11 +226,11 @@ extern void(*D_801755D0)(void);
extern u32 __osMalloc_FreeBlockTest_Enable;
extern Arena gSystemArena;
extern OSPifRam __osPifInternalBuff;
extern u8 __osContLastPoll;
extern OSPifRam __osContPifRam;
extern u8 __osContLastCmd;
extern u8 __osMaxControllers;
extern __OSInode __osPfsInodeCache;
extern OSPifRam gPifMempakBuf;
extern OSPifRam __osPfsPifRam;
extern u16 gZBuffer[SCREEN_HEIGHT][SCREEN_WIDTH]; // 0x25800 bytes
extern u64 gGfxSPTaskOutputBuffer[0x3000]; // 0x18000 bytes
extern u64 gGfxSPTaskYieldBuffer[OS_YIELD_DATA_SIZE / sizeof(u64)]; // 0xC00 bytes