summaryrefslogtreecommitdiff
path: root/src/libultra_code/osContStartReadData.c
blob: 60e415028a1232fa03728bde69c6640d78f88599 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <ultra64.h>
#include <global.h>

#include <ultra64/controller.h>

s32 osContStartReadData(OSMesgQueue* mq) {
    s32 ret;
    __osSiGetAccess(); // __osSiGetAccess
    if (_osCont_lastPollType != 1) {
        __osPackReadData();
        __osSiRawStartDma(OS_WRITE, &_osPifInternalBuff);
        osRecvMesg(mq, NULL, OS_MESG_BLOCK);
    }
    ret = __osSiRawStartDma(OS_READ, &_osPifInternalBuff);
    _osCont_lastPollType = 1;
    __osSiRelAccess();
    return ret;
}

void osContGetReadData(OSContPad* pad) {
    PIF_IO_slot_t* slot_ptr;
    PIF_IO_slot_t slot;
    s32 i;
    slot_ptr = _osPifInternalBuff.slots;
    for (i = 0; i < _osCont_numControllers; i++, slot_ptr++, pad++) {
        slot = *slot_ptr;
        pad->errno = (slot.hdr.status_hi_bytes_rec_lo & 0xc0) >> 4;
        if (pad->errno == 0) {
            pad->button = slot.input.button;
            pad->stick_x = slot.input.x;
            pad->stick_y = slot.input.y;
        }
    };
}

void __osPackReadData() {
    PIF_IO_slot_t* slot_ptr;
    PIF_IO_slot_t slot;
    s32 i;
    slot_ptr = _osPifInternalBuff.slots;
    for (i = 0; i < 0xF; i++) {
        _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 = 4;
    slot.hdr.command = 1;
    slot.input.button = 0xFFFF;
    slot.input.x = 0xFF;
    slot.input.y = 0xFF;
    for (i = 0; i < _osCont_numControllers; i++) {
        *slot_ptr++ = slot;
    }
    slot_ptr->hdr.slot_type = 0xFE;
}