diff --git a/src/Platform.h b/src/Platform.h index 91eb9473..3e0b376f 100644 --- a/src/Platform.h +++ b/src/Platform.h @@ -26,8 +26,8 @@ namespace Platform bool MP_Init(); void MP_DeInit(); -int MP_SendPacket(u8* data, int len, int rate); -int MP_RecvPacket(u8* data, bool block, int* rate); +int MP_SendPacket(u8* data, int len); +int MP_RecvPacket(u8* data, bool block); } diff --git a/src/Wifi.cpp b/src/Wifi.cpp index 668af140..fa056ca6 100644 --- a/src/Wifi.cpp +++ b/src/Wifi.cpp @@ -48,6 +48,18 @@ u16 RFData1; u16 RFData2; u32 RFRegs[0x40]; +typedef struct +{ + u16 Addr; + u16 Length; + u8 Rate; + u8 CurPhase; + u32 CurPhaseTime; + +} TXSlot; + +TXSlot TXSlots[6]; + bool MPInited; @@ -131,6 +143,17 @@ void Reset() RFVersion = SPI_Firmware::GetRFVersion(); memset(RFRegs, 0, 4*0x40); + u8 console = SPI_Firmware::GetConsoleType(); + if (console == 0xFF) + IOPORT(0x000) = 0x1440; + else if (console == 0x20) + IOPORT(0x000) = 0xC340; + else + { + printf("wifi: unknown console type %02X\n", console); + IOPORT(0x000) = 0x1440; + } + memset(&IOPORT(0x018), 0xFF, 6); memset(&IOPORT(0x020), 0xFF, 6); } @@ -172,13 +195,9 @@ void SetIRQ14(bool forced) IOPORT(W_BeaconCount2) = 0xFFFF; IOPORT(W_TXReqRead) &= 0xFFF2; // todo, eventually? - // TODO: actually send beacon if (IOPORT(W_TXSlotBeacon) & 0x8000) { - printf("SEND BEACON\n"); - /*FILE* f = fopen("beacon.bin", "wb"); - fwrite(RAM, 0x2000, 1, f); - fclose(f);*/ + //StartTX_Beacon(); } if (IOPORT(W_ListenCount) == 0) @@ -199,6 +218,75 @@ void SetIRQ15() } +// TODO: set RFSTATUS/RFPINS + +int PreambleLen(int rate) +{ + if (rate == 1) return 192; + if (IOPORT(W_Preamble) & 0x0004) return 96; + return 192; +} + +void StartTX_Beacon() +{ + TXSlot* slot = &TXSlots[4]; + + slot->Addr = (IOPORT(W_TXSlotBeacon) & 0x0FFF) << 1; + slot->Length = *(u16*)&RAM[slot->Addr + 0xA] & 0x3FFF; + + u16 rate = *(u16*)&RAM[slot->Addr]; + if (rate == 0x14) slot->Rate = 2; + else slot->Rate = 1; + + slot->CurPhase = 0; + slot->CurPhaseTime = PreambleLen(slot->Rate); + + *(u16*)&RAM[slot->Addr + 0xC + 22] = IOPORT(W_TXSeqNo) << 4; + + u64 oldval = *(u64*)&RAM[slot->Addr + 0xC + 24]; + *(u64*)&RAM[slot->Addr + 0xC + 24] = USCounter; + + Platform::MP_SendPacket(&RAM[slot->Addr], 12 + slot->Length); + + *(u64*)&RAM[slot->Addr + 0xC + 24] = oldval; +} + +void ProcessTX(TXSlot* slot, int busybit) +{ + slot->CurPhaseTime--; + if (slot->CurPhaseTime > 0) return; + + switch (slot->CurPhase) + { + case 0: // preamble done + { + u32 len = slot->Length; + if (slot->Rate == 2) len *= 4; + else len *= 8; + + slot->CurPhase = 1; + slot->CurPhaseTime = len; + + SetIRQ(7); + IOPORT(W_TXSeqNo) = (IOPORT(W_TXSeqNo) + 1) & 0x0FFF; + } + break; + + case 1: // transmit done + { + // checkme + *(u16*)&RAM[slot->Addr] = 0x0001; + RAM[slot->Addr + 5] = 0; + + IOPORT(W_TXBusy) &= ~(1<= (IOPORT(W_RXBufEnd) & 0x1FFE)) rdaddr = rdaddr + (IOPORT(W_RXBufBegin) & 0x1FFE) - (IOPORT(W_RXBufEnd) & 0x1FFE); + + if (IOPORT(0x000) == 0xC340) + IOPORT(W_RXBufGapSize) = 0; } IOPORT(W_RXBufReadAddr) = rdaddr & 0x1FFE; IOPORT(W_RXBufDataRead) = ret; + + if (IOPORT(W_RXBufCount) > 0) + { + IOPORT(W_RXBufCount)--; + if (IOPORT(W_RXBufCount) == 0) + SetIRQ(9); + } } break; } @@ -532,22 +633,67 @@ void Write(u32 addr, u16 val) if (val & 0x7FFF) printf("wifi: unknown RXCNT bits set %04X\n", val); break; - - case W_TXBufDataWrite: + case W_RXBufDataRead: + printf("wifi: writing to RXBUF_DATA_READ. wat\n"); + if (IOPORT(W_RXBufCount) > 0) { - u32 wraddr = IOPORT(W_TXBufWriteAddr) & 0x1FFE; - *(u16*)&RAM[wraddr] = val; - - wraddr += 2; - if (wraddr == (IOPORT(W_TXBufGapAddr) & 0x1FFE)) - wraddr += ((IOPORT(W_TXBufGapSize) & 0x0FFF) << 1); - - IOPORT(W_TXBufWriteAddr) = wraddr & 0x1FFE; + IOPORT(W_RXBufCount)--; + if (IOPORT(W_RXBufCount) == 0) + SetIRQ(9); } return; - case 0x80: - printf("BEACON ADDR %04X\n", val); + case W_RXBufReadAddr: + case W_RXBufGapAddr: + val &= 0x1FFE; + break; + case W_RXBufGapSize: + case W_RXBufCount: + val &= 0x0FFF; + break; + + + case W_TXSlotReset: + if (val & 0x0001) IOPORT(W_TXSlotLoc1) &= 0x7FFF; + if (val & 0x0002) IOPORT(W_TXSlotCmd) &= 0x7FFF; + if (val & 0x0004) IOPORT(W_TXSlotLoc2) &= 0x7FFF; + if (val & 0x0008) IOPORT(W_TXSlotLoc3) &= 0x7FFF; + // checkme: any bits affecting the beacon slot? + if (val & 0x0040) IOPORT(W_TXSlotReply2) &= 0x7FFF; + if (val & 0x0080) IOPORT(W_TXSlotReply1) &= 0x7FFF; + val = 0; // checkme (write-only port) + break; + + case W_TXBufDataWrite: + { + u32 wraddr = IOPORT(W_TXBufWriteAddr); + *(u16*)&RAM[wraddr] = val; + + wraddr += 2; + if (wraddr == IOPORT(W_TXBufGapAddr)) + wraddr += (IOPORT(W_TXBufGapSize) << 1); + + //if (IOPORT(0x000) == 0xC340) + // IOPORT(W_TXBufGapSize) = 0; + + IOPORT(W_TXBufWriteAddr) = wraddr & 0x1FFE; + + if (IOPORT(W_TXBufCount) > 0) + { + IOPORT(W_TXBufCount)--; + if (IOPORT(W_TXBufCount) == 0) + SetIRQ(8); + } + } + return; + + case W_TXBufWriteAddr: + case W_TXBufGapAddr: + val &= 0x1FFE; + break; + case W_TXBufGapSize: + case W_TXBufCount: + val &= 0x0FFF; break; // read-only ports diff --git a/src/Wifi.h b/src/Wifi.h index b5907040..65707f2b 100644 --- a/src/Wifi.h +++ b/src/Wifi.h @@ -144,6 +144,8 @@ bool Init(); void DeInit(); void Reset(); +void StartTX_Beacon(); + void USTimer(u32 param); u16 Read(u32 addr); diff --git a/src/wx/Platform.cpp b/src/wx/Platform.cpp index 71236aff..12145f3b 100644 --- a/src/wx/Platform.cpp +++ b/src/wx/Platform.cpp @@ -117,7 +117,7 @@ void MP_DeInit() #endif // __WXMSW__ } -int MP_SendPacket(u8* data, int len, int rate) +int MP_SendPacket(u8* data, int len) { if (MPSocket < 0) return 0; @@ -130,14 +130,14 @@ int MP_SendPacket(u8* data, int len, int rate) *(u32*)&PacketBuffer[0] = htonl(0x4946494E); // NIFI PacketBuffer[4] = NIFI_VER; - PacketBuffer[5] = rate & 0xFF; + PacketBuffer[5] = 0; *(u16*)&PacketBuffer[6] = htons(len); memcpy(&PacketBuffer[8], data, len); return sendto(MPSocket, (const char*)PacketBuffer, len+8, 0, &MPSendAddr, sizeof(sockaddr_t)); } -int MP_RecvPacket(u8* data, bool block, int* rate) +int MP_RecvPacket(u8* data, bool block) { if (MPSocket < 0) return 0; @@ -181,7 +181,6 @@ int MP_RecvPacket(u8* data, bool block, int* rate) } memcpy(data, &PacketBuffer[8], rlen); - if (rate) *rate = PacketBuffer[5]; return rlen; }