summaryrefslogtreecommitdiff
path: root/src/libultra/voice/voicecontread2.c
blob: a9e6660bbbbb87c94951acd35024a0b8085c6d6d (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/**
 * File: voicecontread2.c
 */

#include "ultra64.h"
#include "PR/controller_voice.h"
#include "PR/voiceinternal.h"
#include "PR/controller.h"
#include "macros.h"

#define READ2FORMAT(ptr) ((__OSVoiceRead2Format*)(ptr))

s32 __osVoiceContRead2(OSMesgQueue* mq, s32 channel, u16 address, u8 dst[2]) {
    s32 errorCode;
    u8 status;
    u8* ptr;
    s32 retryCount = 2;
    s32 i;

    __osSiGetAccess();

    do {
        ptr = (u8*)&__osPfsPifRam.ramarray;

        if ((__osContLastPoll != CONT_CMD_READ2_VOICE) || (channel != __osPfsLastChannel)) {
            __osContLastPoll = CONT_CMD_READ2_VOICE;
            __osPfsLastChannel = channel;

            for (i = 0; i < channel; i++, *ptr++ = 0) {}

            __osPfsPifRam.status = CONT_CMD_EXE;

            READ2FORMAT(ptr)->dummy = CONT_CMD_NOP;
            READ2FORMAT(ptr)->txsize = CONT_CMD_READ2_VOICE_TX;
            READ2FORMAT(ptr)->rxsize = CONT_CMD_READ2_VOICE_RX;
            READ2FORMAT(ptr)->cmd = CONT_CMD_READ2_VOICE;
            READ2FORMAT(ptr)->datacrc = 0xFF;

            ptr[sizeof(__OSVoiceRead2Format)] = CONT_CMD_END;
        } else {
            ptr += channel;
        }

        READ2FORMAT(ptr)->addrh = address >> 3;
        READ2FORMAT(ptr)->addrl = (address << 5) | __osContAddressCrc(address);

        __osSiRawStartDma(OS_WRITE, &__osPfsPifRam);
        osRecvMesg(mq, NULL, OS_MESG_BLOCK);
        __osSiRawStartDma(OS_READ, &__osPfsPifRam);
        osRecvMesg(mq, NULL, OS_MESG_BLOCK);

        errorCode = CHNL_ERR(*READ2FORMAT(ptr));

        if (errorCode == 0) {
            if (__osVoiceContDataCrc(READ2FORMAT(ptr)->data, ARRAY_COUNT(READ2FORMAT(ptr)->data)) !=
                READ2FORMAT(ptr)->datacrc) {
                errorCode = __osVoiceGetStatus(mq, channel, &status);
                if (errorCode != 0) {
                    break;
                }

                errorCode = CONT_ERR_CONTRFAIL;
            } else {
                bcopy(READ2FORMAT(ptr)->data, dst, ARRAY_COUNT(READ2FORMAT(ptr)->data));
            }
        } else {
            errorCode = CONT_ERR_NO_CONTROLLER;
        }

    } while ((errorCode == CONT_ERR_CONTRFAIL) && (retryCount-- >= 0));

    __osSiRelAccess();

    return errorCode;
}