1
0
Fork 0
mirror of https://github.com/zeldaret/oot.git synced 2025-02-27 19:44:40 +00:00
oot/src/libultra_code/osContInit.c

91 lines
2.6 KiB
C
Raw Normal View History

2020-03-17 00:31:30 -04:00
#include <ultra64.h>
#include <global.h>
#include <ultra64/controller.h>
pif_data_buffer_t _osPifInternalBuff;
u8 _osCont_lastPollType;
2020-03-22 22:19:43 +01:00
u8 _osCont_numControllers; // always 4
2020-03-17 00:31:30 -04:00
2020-03-22 22:19:43 +01:00
// Not sure if the following is a struct together with the last two variables
2020-03-17 00:31:30 -04:00
u16 unk_80175812;
u32 unk_80175814;
2020-03-22 22:19:43 +01:00
u8 unk_80175818[0x20];
2020-03-17 00:31:30 -04:00
u32 gOsContInitialized = 0;
OSMesgQueue _osContMesgQueue;
OSMesg _osContMesgBuff[4];
#define HALF_SECOND OS_USEC_TO_CYCLES(500000)
2020-03-22 22:19:43 +01:00
s32 osContInit(OSMesgQueue* mq, u8* ctl_present_bitfield, OSContStatus* status) {
2020-03-17 00:31:30 -04:00
OSMesg mesg;
s32 ret = 0;
OSTime currentTime;
OSTimer timer;
OSMesgQueue timerqueue;
if (gOsContInitialized) {
return 0;
}
gOsContInitialized = 1;
currentTime = osGetTime();
if (HALF_SECOND > currentTime) {
osCreateMesgQueue(&timerqueue, &mesg, 1);
osSetTimer(&timer, HALF_SECOND - currentTime, 0, &timerqueue, &mesg);
osRecvMesg(&timerqueue, &mesg, OS_MESG_BLOCK);
}
_osCont_numControllers = 4;
__osPackRequestData(0);
ret = __osSiRawStartDma(OS_WRITE, &_osPifInternalBuff);
osRecvMesg(mq, &mesg, OS_MESG_BLOCK);
ret = __osSiRawStartDma(OS_READ, &_osPifInternalBuff);
osRecvMesg(mq, &mesg, OS_MESG_BLOCK);
__osContGetInitData(ctl_present_bitfield, status);
_osCont_lastPollType = 0;
__osSiCreateAccessQueue();
osCreateMesgQueue(&_osContMesgQueue, _osContMesgBuff, 1);
return ret;
}
2020-03-22 22:19:43 +01:00
void __osContGetInitData(u8* ctl_present_bitfield, OSContStatus* status) {
PIF_IO_slot_t* slot_ptr;
2020-03-17 00:31:30 -04:00
PIF_IO_slot_t slot;
s32 i;
u8 bitfield_temp;
bitfield_temp = 0;
slot_ptr = _osPifInternalBuff.slots;
for (i = 0; i < _osCont_numControllers; i++, slot_ptr++, status++) {
slot = *slot_ptr;
status->errno = (slot.hdr.status_hi_bytes_rec_lo & 0xc0) >> 4;
if (status->errno == 0) {
status->type = slot.ctl_type_hi << 8 | slot.ctl_type_lo;
status->status = slot.ctl_status;
bitfield_temp |= 1 << i;
}
}
*ctl_present_bitfield = bitfield_temp;
}
2020-03-22 22:19:43 +01:00
void __osPackRequestData(u8 command) {
PIF_IO_slot_t* slot_ptr;
2020-03-17 00:31:30 -04:00
PIF_IO_slot_t slot;
s32 i;
2020-03-22 22:19:43 +01:00
for (i = 0; i < 0xF; i++) {
2020-03-17 00:31:30 -04:00
_osPifInternalBuff.words[i] = 0;
}
_osPifInternalBuff.status_control = 1;
slot.hdr.slot_type = 0xFF;
slot.hdr.bytes_send = 1;
slot.hdr.status_hi_bytes_rec_lo = 3;
slot.hdr.command = command;
slot.ctl_type_lo = 0xFF;
slot.ctl_type_hi = 0xFF;
slot.ctl_status = 0xFF;
slot.dummy = 0xFF;
slot_ptr = _osPifInternalBuff.slots;
for (i = 0; i < _osCont_numControllers; i++) {
*slot_ptr++ = slot;
}
slot_ptr->hdr.slot_type = 254;
}