sm64

A Super Mario 64 decompilation
Log | Files | Refs | README | LICENSE

osPfsIsPlug.c (2877B)


      1 #include "PR/os_pi.h"
      2 #include "libultra_internal.h"
      3 #include "controller.h"
      4 #include "macros.h"
      5 
      6 OSPifRam __osPfsPifRam;
      7 
      8 s32 osPfsIsPlug(OSMesgQueue *queue, u8 *pattern) {
      9     s32 ret;
     10     OSMesg dummy;
     11     u8 bitpattern;
     12     OSContStatus data[MAXCONTROLLERS];
     13     int channel;
     14     u8 bits;
     15     int crc_error_cnt;
     16     ret = 0;
     17     bits = 0;
     18     crc_error_cnt = 3;
     19     __osSiGetAccess();
     20     while (TRUE) {
     21         __osPfsRequestData(CONT_CMD_REQUEST_STATUS);
     22         ret = __osSiRawStartDma(OS_WRITE, &__osPfsPifRam);
     23         osRecvMesg(queue, &dummy, OS_MESG_BLOCK);
     24         ret = __osSiRawStartDma(OS_READ, &__osPfsPifRam);
     25         osRecvMesg(queue, &dummy, OS_MESG_BLOCK);
     26         __osPfsGetInitData(&bitpattern, data);
     27         for (channel = 0; channel < __osMaxControllers; channel++) {
     28             if ((data[channel].status & CONT_ADDR_CRC_ER) == 0) {
     29                 crc_error_cnt--;
     30                 break;
     31             }
     32         }
     33         if (__osMaxControllers == channel) {
     34             crc_error_cnt = 0;
     35         }
     36         if (crc_error_cnt < 1) {
     37             for (channel = 0; channel < __osMaxControllers; channel++) {
     38                 if (data[channel].errnum == 0 && (data[channel].status & CONT_CARD_ON) != 0) {
     39                     bits |= 1 << channel;
     40                 }
     41             }
     42             __osSiRelAccess();
     43             *pattern = bits;
     44             return ret;
     45         }
     46     }
     47 }
     48 
     49 void __osPfsRequestData(u8 cmd) {
     50     u8 *ptr;
     51     __OSContRequesFormat requestformat;
     52     int i;
     53 
     54     __osContLastCmd = cmd;
     55 
     56     for (i = 0; i < ARRAY_COUNT(__osPfsPifRam.ramarray) + 1; i++) { // also clear pifstatus
     57         __osPfsPifRam.ramarray[i] = 0;
     58     }
     59 
     60     __osPfsPifRam.pifstatus = CONT_CMD_EXE;
     61 
     62     ptr = (u8 *)&__osPfsPifRam;
     63     requestformat.dummy = CONT_CMD_NOP;
     64     requestformat.txsize = CONT_CMD_REQUEST_STATUS_TX;
     65     requestformat.rxsize = CONT_CMD_REQUEST_STATUS_RX;
     66     requestformat.cmd = cmd;
     67     requestformat.typeh = CONT_CMD_NOP;
     68     requestformat.typel = CONT_CMD_NOP;
     69     requestformat.status = CONT_CMD_NOP;
     70     requestformat.dummy1 = CONT_CMD_NOP;
     71     for (i = 0; i < __osMaxControllers; i++) {
     72         *(__OSContRequesFormat *)ptr = requestformat;
     73         ptr += sizeof(__OSContRequesFormat);
     74     }
     75     *ptr = CONT_CMD_END;
     76 }
     77 
     78 void __osPfsGetInitData(u8 *pattern, OSContStatus *data) {
     79     u8 *ptr;
     80     __OSContRequesFormat requestformat;
     81     int i;
     82     u8 bits;
     83     bits = 0;
     84     ptr = (u8 *)&__osPfsPifRam;
     85     for (i = 0; i < __osMaxControllers; i++, ptr += sizeof(__OSContRequesFormat)) {
     86         requestformat = *(__OSContRequesFormat *)ptr;
     87         data->errnum = CHNL_ERR(requestformat);
     88         if (data->errnum == 0) {
     89             data->type = (requestformat.typel << 8) | (requestformat.typeh);
     90             data->status = requestformat.status;
     91             bits |= 1 << i;
     92         }
     93         data++;
     94     }
     95     *pattern = bits;
     96 }