diff --git a/desmume/src/saves.cpp b/desmume/src/saves.cpp index 37ac18620..c9b1559e1 100644 --- a/desmume/src/saves.cpp +++ b/desmume/src/saves.cpp @@ -291,8 +291,8 @@ SFORMAT SF_WIFI[]={ { "W020", 2, 1, &wifiMac.rfStatus}, { "W030", 2, 1, &wifiMac.rfPins}, - { "W040", 2, 1, &wifiMac.IE.val}, - { "W050", 2, 1, &wifiMac.IF.val}, + { "W040", 2, 1, &wifiMac.IE}, + { "W050", 2, 1, &wifiMac.IF}, { "W060", 2, 1, &wifiMac.macMode}, { "W070", 2, 1, &wifiMac.wepMode}, @@ -349,14 +349,10 @@ SFORMAT SF_WIFI[]={ { "W370", 4, 1, &wifiMac.rfIOData.val}, { "W380", 2, 1, &wifiMac.bbIOCnt.val}, - { "W390", 1, 1, &wifiMac.bbDataToWrite}, - - { "W400", 2, 0x1000, &wifiMac.circularBuffer[0]}, + { "W400", 2, 0x1000, &wifiMac.RAM[0]}, { "W410", 2, 1, &wifiMac.RXRangeBegin}, { "W420", 2, 1, &wifiMac.RXRangeEnd}, - { "W430", 2, 1, &wifiMac.RXHWWriteCursor}, - { "W440", 2, 1, &wifiMac.RXHWWriteCursorReg}, - { "W450", 2, 1, &wifiMac.RXHWWriteCursorLatched}, + { "W430", 2, 1, &wifiMac.RXWriteCursor}, { "W460", 2, 1, &wifiMac.RXReadCursor}, { "W470", 2, 1, &wifiMac.RXUnits}, { "W480", 2, 1, &wifiMac.RXBufCount}, @@ -371,14 +367,14 @@ SFORMAT SF_WIFI[]={ { "W560", 4, 1, &wifiMac.curPacketPos[0]}, { "W570", 4, 1, &wifiMac.curPacketSending[0]}, - { "W580", 2, 0x800, &wifiMac.ioMem[0]}, + { "W580", 2, 0x800, &wifiMac.IOPorts[0]}, { "W590", 2, 1, &wifiMac.randomSeed}, - { "WX00", 8, 1, &wifiMac.SoftAP.usecCounter}, - { "WX10", 1, 4096, &wifiMac.SoftAP.curPacket[0]}, - { "WX20", 4, 1, &wifiMac.SoftAP.curPacketSize}, - { "WX30", 4, 1, &wifiMac.SoftAP.curPacketPos}, - { "WX40", 4, 1, &wifiMac.SoftAP.curPacketSending}, + { "WX00", 8, 1, &SoftAP.usecCounter}, + { "WX10", 1, 4096, &SoftAP.curPacket[0]}, + { "WX20", 4, 1, &SoftAP.curPacketSize}, + { "WX30", 4, 1, &SoftAP.curPacketPos}, + { "WX40", 4, 1, &SoftAP.curPacketSending}, { 0 } }; diff --git a/desmume/src/wifi.cpp b/desmume/src/wifi.cpp index d8a1173bf..989a45c02 100644 --- a/desmume/src/wifi.cpp +++ b/desmume/src/wifi.cpp @@ -63,6 +63,8 @@ pcap_t *wifi_bridge = NULL; #endif wifimac_t wifiMac; +Adhoc_t Adhoc; +SoftAP_t SoftAP; /******************************************************************************* @@ -73,12 +75,12 @@ wifimac_t wifiMac; *******************************************************************************/ -u8 FW_Mac[6] = { 0x00, 0x09, 0xBF, 0x12, 0x34, 0x56 } ; +u8 FW_Mac[6] = { 0x00, 0x09, 0xBF, 0x12, 0x34, 0x56 }; const u8 FW_WIFIInit[32] = { 0x02,0x00, 0x17,0x00, 0x26,0x00, 0x18,0x18, 0x48,0x00, 0x40,0x48, 0x58,0x00, 0x42,0x00, 0x40,0x01, 0x64,0x80, 0xE0,0xE0, 0x43,0x24, - 0x0E,0x00, 0x32,0x00, 0xF4,0x01, 0x01,0x01 } ; + 0x0E,0x00, 0x32,0x00, 0xF4,0x01, 0x01,0x01 }; const u8 FW_BBInit[105] = { 0x6D, 0x9E, 0x40, 0x05, 0x1B, 0x6C, 0x48, 0x80, 0x38, 0x00, 0x35, 0x07, @@ -105,7 +107,7 @@ const u8 FW_BBInit[105] = { 0x6D, 0x9E, 0x40, 0x05, 0xff, 0xff, 0x00, 0x0e, 0x13, 0x00, 0x00, 0x28, 0x1c - } ; + }; const u8 FW_RFInit[36] = { 0x07, 0xC0, 0x00, 0x03, 0x9C, 0x12, 0x28, 0x17, 0x14, @@ -118,7 +120,7 @@ const u8 FW_RFInit[36] = { 0x07, 0xC0, 0x00, 0x03, 0x9c, 0x06, 0x22, 0x00, 0x08, 0x6f, 0xff, 0x0d - } ; + }; const u8 FW_RFChannel[6*14] = { 0x28, 0x17, 0x14, /* Channel 1 */ 0xba, 0xe8, 0x1a, 0x37, 0x17, 0x14, /* Channel 2 */ @@ -147,14 +149,12 @@ const u8 FW_RFChannel[6*14] = { 0x28, 0x17, 0x14, /* Channel 1 */ 0x46, 0x17, 0x19, 0xfa, 0x17, 0x14, /* Channel 14 */ 0x2f, 0xba, 0x18 - } ; + }; const u8 FW_BBChannel[14] = { 0xb3, 0xb3, 0xb3, 0xb3, 0xb3, /* channel 1- 6 */ 0xb4, 0xb4, 0xb4, 0xb4, 0xb4, /* channel 7-10 */ 0xb5, 0xb5, /* channel 11-12 */ 0xb6, 0xb6 /* channel 13-14 */ - } ; - -/* Note : the values are inspired from what I found in a firmware image from my DS */ + }; FW_WFCProfile FW_WFCProfile1 = {"SoftAP", "", @@ -175,7 +175,7 @@ FW_WFCProfile FW_WFCProfile1 = {"SoftAP", 0, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0} - } ; + }; FW_WFCProfile FW_WFCProfile2 = {"", "", @@ -196,7 +196,7 @@ FW_WFCProfile FW_WFCProfile2 = {"", 0, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0} - } ; + }; FW_WFCProfile FW_WFCProfile3 = {"", "", @@ -217,7 +217,7 @@ FW_WFCProfile FW_WFCProfile3 = {"", 0, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0} - } ; + }; /******************************************************************************* @@ -241,7 +241,7 @@ void SoftAP_Reset(); void SoftAP_SendPacket(u8 *packet, u32 len); void SoftAP_usTrigger(); -WifiComInterface SoftAP = { +WifiComInterface CI_SoftAP = { SoftAP_Init, SoftAP_DeInit, SoftAP_Reset, @@ -255,7 +255,7 @@ void Adhoc_Reset(); void Adhoc_SendPacket(u8* packet, u32 len); void Adhoc_usTrigger(); -WifiComInterface Adhoc = { +WifiComInterface CI_Adhoc = { Adhoc_Init, Adhoc_DeInit, Adhoc_Reset, @@ -266,8 +266,8 @@ WifiComInterface Adhoc = { WifiComInterface* wifiComs[] = { #ifdef EXPERIMENTAL_WIFI_COMM - &Adhoc, - &SoftAP, + &CI_Adhoc, + &CI_SoftAP, #endif NULL }; @@ -377,91 +377,90 @@ static void WIFI_initCRC32Table() *******************************************************************************/ -static void WIFI_resetRF(rffilter_t *rf) { +static void WIFI_resetRF(rffilter_t *rf) +{ /* reinitialize RF chip with the default values refer RF2958 docs */ /* CFG1 */ - rf->CFG1.bits.IF_VGA_REG_EN = 1 ; - rf->CFG1.bits.IF_VCO_REG_EN = 1 ; - rf->CFG1.bits.RF_VCO_REG_EN = 1 ; - rf->CFG1.bits.HYBERNATE = 0 ; - rf->CFG1.bits.REF_SEL = 0 ; + rf->CFG1.bits.IF_VGA_REG_EN = 1; + rf->CFG1.bits.IF_VCO_REG_EN = 1; + rf->CFG1.bits.RF_VCO_REG_EN = 1; + rf->CFG1.bits.HYBERNATE = 0; + rf->CFG1.bits.REF_SEL = 0; /* IFPLL1 */ - rf->IFPLL1.bits.DAC = 3 ; - rf->IFPLL1.bits.P1 = 0 ; - rf->IFPLL1.bits.LD_EN1 = 0 ; - rf->IFPLL1.bits.AUTOCAL_EN1 = 0 ; - rf->IFPLL1.bits.PDP1 = 1 ; - rf->IFPLL1.bits.CPL1 = 0 ; - rf->IFPLL1.bits.LPF1 = 0 ; - rf->IFPLL1.bits.VTC_EN1 = 1 ; - rf->IFPLL1.bits.KV_EN1 = 0 ; - rf->IFPLL1.bits.PLL_EN1 = 0 ; + rf->IFPLL1.bits.DAC = 3; + rf->IFPLL1.bits.P1 = 0; + rf->IFPLL1.bits.LD_EN1 = 0; + rf->IFPLL1.bits.AUTOCAL_EN1 = 0; + rf->IFPLL1.bits.PDP1 = 1; + rf->IFPLL1.bits.CPL1 = 0; + rf->IFPLL1.bits.LPF1 = 0; + rf->IFPLL1.bits.VTC_EN1 = 1; + rf->IFPLL1.bits.KV_EN1 = 0; + rf->IFPLL1.bits.PLL_EN1 = 0; /* IFPLL2 */ - rf->IFPLL2.bits.IF_N = 0x22 ; + rf->IFPLL2.bits.IF_N = 0x22; /* IFPLL3 */ - rf->IFPLL3.bits.KV_DEF1 = 8 ; - rf->IFPLL3.bits.CT_DEF1 = 7 ; - rf->IFPLL3.bits.DN1 = 0x1FF ; + rf->IFPLL3.bits.KV_DEF1 = 8; + rf->IFPLL3.bits.CT_DEF1 = 7; + rf->IFPLL3.bits.DN1 = 0x1FF; /* RFPLL1 */ - rf->RFPLL1.bits.DAC = 3 ; - rf->RFPLL1.bits.P = 0 ; - rf->RFPLL1.bits.LD_EN = 0 ; - rf->RFPLL1.bits.AUTOCAL_EN = 0 ; - rf->RFPLL1.bits.PDP = 1 ; - rf->RFPLL1.bits.CPL = 0 ; - rf->RFPLL1.bits.LPF = 0 ; - rf->RFPLL1.bits.VTC_EN = 0 ; - rf->RFPLL1.bits.KV_EN = 0 ; - rf->RFPLL1.bits.PLL_EN = 0 ; + rf->RFPLL1.bits.DAC = 3; + rf->RFPLL1.bits.P = 0; + rf->RFPLL1.bits.LD_EN = 0; + rf->RFPLL1.bits.AUTOCAL_EN = 0; + rf->RFPLL1.bits.PDP = 1; + rf->RFPLL1.bits.CPL = 0; + rf->RFPLL1.bits.LPF = 0; + rf->RFPLL1.bits.VTC_EN = 0; + rf->RFPLL1.bits.KV_EN = 0; + rf->RFPLL1.bits.PLL_EN = 0; /* RFPLL2 */ - rf->RFPLL2.bits.NUM2 = 0 ; - rf->RFPLL2.bits.N2 = 0x5E ; + rf->RFPLL2.bits.NUM2 = 0; + rf->RFPLL2.bits.N2 = 0x5E; /* RFPLL3 */ - rf->RFPLL3.bits.NUM2 = 0 ; + rf->RFPLL3.bits.NUM2 = 0; /* RFPLL4 */ - rf->RFPLL4.bits.KV_DEF = 8 ; - rf->RFPLL4.bits.CT_DEF = 7 ; - rf->RFPLL4.bits.DN = 0x145 ; + rf->RFPLL4.bits.KV_DEF = 8; + rf->RFPLL4.bits.CT_DEF = 7; + rf->RFPLL4.bits.DN = 0x145; /* CAL1 */ - rf->CAL1.bits.LD_WINDOW = 2 ; - rf->CAL1.bits.M_CT_VALUE = 8 ; - rf->CAL1.bits.TLOCK = 7 ; - rf->CAL1.bits.TVCO = 0x0F ; + rf->CAL1.bits.LD_WINDOW = 2; + rf->CAL1.bits.M_CT_VALUE = 8; + rf->CAL1.bits.TLOCK = 7; + rf->CAL1.bits.TVCO = 0x0F; /* TXRX1 */ - rf->TXRX1.bits.TXBYPASS = 0 ; - rf->TXRX1.bits.INTBIASEN = 0 ; - rf->TXRX1.bits.TXENMODE = 0 ; - rf->TXRX1.bits.TXDIFFMODE = 0 ; - rf->TXRX1.bits.TXLPFBW = 2 ; - rf->TXRX1.bits.RXLPFBW = 2 ; - rf->TXRX1.bits.TXVGC = 0 ; - rf->TXRX1.bits.PCONTROL = 0 ; - rf->TXRX1.bits.RXDCFBBYPS = 0 ; + rf->TXRX1.bits.TXBYPASS = 0; + rf->TXRX1.bits.INTBIASEN = 0; + rf->TXRX1.bits.TXENMODE = 0; + rf->TXRX1.bits.TXDIFFMODE = 0; + rf->TXRX1.bits.TXLPFBW = 2; + rf->TXRX1.bits.RXLPFBW = 2; + rf->TXRX1.bits.TXVGC = 0; + rf->TXRX1.bits.PCONTROL = 0; + rf->TXRX1.bits.RXDCFBBYPS = 0; /* PCNT1 */ - rf->PCNT1.bits.TX_DELAY = 0 ; - rf->PCNT1.bits.PC_OFFSET = 0 ; - rf->PCNT1.bits.P_DESIRED = 0 ; - rf->PCNT1.bits.MID_BIAS = 0 ; + rf->PCNT1.bits.TX_DELAY = 0; + rf->PCNT1.bits.PC_OFFSET = 0; + rf->PCNT1.bits.P_DESIRED = 0; + rf->PCNT1.bits.MID_BIAS = 0; /* PCNT2 */ - rf->PCNT2.bits.MIN_POWER = 0 ; - rf->PCNT2.bits.MID_POWER = 0 ; - rf->PCNT2.bits.MAX_POWER = 0 ; + rf->PCNT2.bits.MIN_POWER = 0; + rf->PCNT2.bits.MID_POWER = 0; + rf->PCNT2.bits.MAX_POWER = 0; /* VCOT1 */ - rf->VCOT1.bits.AUX1 = 0 ; - rf->VCOT1.bits.AUX = 0 ; -} ; + rf->VCOT1.bits.AUX1 = 0; + rf->VCOT1.bits.AUX = 0; +} void WIFI_setRF_CNT(u16 val) { -// printf("write rf cnt %04X\n", val); if (!wifiMac.rfIOStatus.bits.busy) - wifiMac.rfIOCnt.val = val ; + wifiMac.rfIOCnt.val = val; } void WIFI_setRF_DATA(u16 val, u8 part) { -// printf("write rf data %04X %s part\n", val, (part?"high":"low")); if (!wifiMac.rfIOStatus.bits.busy) { rfIOData_t *rfreg = (rfIOData_t *)&wifiMac.RF; @@ -472,15 +471,15 @@ void WIFI_setRF_DATA(u16 val, u8 part) /* on high part, the address is read, and the data at this is written back */ if (part==1) { - wifiMac.rfIOData.array16[part] = val ; - if (wifiMac.rfIOData.bits.address > (sizeof(wifiMac.RF) / 4)) return ; /* out of bound */ + wifiMac.rfIOData.array16[part] = val; + if (wifiMac.rfIOData.bits.address > (sizeof(wifiMac.RF) / 4)) return; /* out of bound */ /* get content of the addressed register */ - wifiMac.rfIOData.bits.content = rfreg[wifiMac.rfIOData.bits.address].bits.content ; + wifiMac.rfIOData.bits.content = rfreg[wifiMac.rfIOData.bits.address].bits.content; } - break ; + break; case 0: /* write to RF chip */ - wifiMac.rfIOData.array16[part] = val ; - if (wifiMac.rfIOData.bits.address > (sizeof(wifiMac.RF) / 4)) return ; /* out of bound */ + wifiMac.rfIOData.array16[part] = val; + if (wifiMac.rfIOData.bits.address > (sizeof(wifiMac.RF) / 4)) return; /* out of bound */ /* the actual transfer is done on high part write */ if (part==1) { @@ -489,45 +488,41 @@ void WIFI_setRF_DATA(u16 val, u8 part) case 5: /* write to upper part of the frequency filter */ case 6: /* write to lower part of the frequency filter */ { - u32 channelFreqN ; - rfreg[wifiMac.rfIOData.bits.address].bits.content = wifiMac.rfIOData.bits.content ; + u32 channelFreqN; + rfreg[wifiMac.rfIOData.bits.address].bits.content = wifiMac.rfIOData.bits.content; /* get the complete rfpll.n */ - channelFreqN = (u32)wifiMac.RF.RFPLL3.bits.NUM2 + ((u32)wifiMac.RF.RFPLL2.bits.NUM2 << 18) + ((u32)wifiMac.RF.RFPLL2.bits.N2 << 24) ; + channelFreqN = (u32)wifiMac.RF.RFPLL3.bits.NUM2 + ((u32)wifiMac.RF.RFPLL2.bits.NUM2 << 18) + ((u32)wifiMac.RF.RFPLL2.bits.N2 << 24); /* frequency setting is out of range */ - if (channelFreqN<0x00A2E8BA) return ; + if (channelFreqN<0x00A2E8BA) return; /* substract base frequency (channel 1) */ - channelFreqN -= 0x00A2E8BA ; + channelFreqN -= 0x00A2E8BA; } - return ; + return; case 13: /* special purpose register: TEST1, on write, the RF chip resets */ - WIFI_resetRF(&wifiMac.RF) ; - return ; + WIFI_resetRF(&wifiMac.RF); + return; } /* set content of the addressed register */ - rfreg[wifiMac.rfIOData.bits.address].bits.content = wifiMac.rfIOData.bits.content ; + rfreg[wifiMac.rfIOData.bits.address].bits.content = wifiMac.rfIOData.bits.content; } - break ; + break; } } } u16 WIFI_getRF_DATA(u8 part) { -// printf("read rf data %s part\n", (part?"high":"low")); if (!wifiMac.rfIOStatus.bits.busy) - { - return wifiMac.rfIOData.array16[part] ; - } else - { /* data is not (yet) available */ - return 0 ; - } + return wifiMac.rfIOData.array16[part]; + else + /* data is not (yet) available */ + return 0; } u16 WIFI_getRF_STATUS() { -// printf("read rf status\n"); - return wifiMac.rfIOStatus.val ; + return wifiMac.rfIOStatus.val; } /******************************************************************************* @@ -541,7 +536,7 @@ void WIFI_setBB_CNT(u16 val) wifiMac.bbIOCnt.val = val; if(wifiMac.bbIOCnt.bits.mode == 1) - wifiMac.BB.data[wifiMac.bbIOCnt.bits.address] = wifiMac.bbDataToWrite; + wifiMac.BB.data[wifiMac.bbIOCnt.bits.address] = WIFI_IOREG(REG_WIFI_BBWRITE); } u8 WIFI_getBB_DATA() @@ -552,11 +547,6 @@ u8 WIFI_getBB_DATA() return wifiMac.BB.data[wifiMac.bbIOCnt.bits.address]; } -void WIFI_setBB_DATA(u8 val) -{ - wifiMac.bbDataToWrite = val; -} - /******************************************************************************* wifimac IO: a lot of the wifi regs are action registers, that are mirrored @@ -571,14 +561,14 @@ static void WIFI_BeaconTXStart(); static void WIFI_triggerIRQMask(u16 mask) { - u16 oResult,nResult ; - oResult = wifiMac.IE.val & wifiMac.IF.val ; - wifiMac.IF.val = wifiMac.IF.val | (mask & ~0x0400) ; - nResult = wifiMac.IE.val & wifiMac.IF.val ; + u16 oResult,nResult; + oResult = wifiMac.IE & wifiMac.IF; + wifiMac.IF = wifiMac.IF | (mask & ~0x0400); + nResult = wifiMac.IE & wifiMac.IF; if (!oResult && nResult) { - NDS_makeARM7Int(24) ; /* cascade it via arm7 wifi irq */ + NDS_makeARM7Int(24); /* cascade it via arm7 wifi irq */ } } @@ -623,7 +613,7 @@ static void WIFI_triggerIRQ(u8 irq) break; } - WIFI_triggerIRQMask(1<= wifiMac.RXReadCursor) { printf("WIFI: write cursor (%04X) above READCSR (%04X). Cannot write received packet.\n", @@ -745,19 +735,16 @@ static void WIFI_RXPutWord(u16 val) return; }*/ /* write the data to cursor position */ - wifiMac.circularBuffer[wifiMac.RXHWWriteCursor & 0xFFF] = val; + wifiMac.RAM[wifiMac.RXWriteCursor & 0xFFF] = val; // printf("wifi: written word %04X to circbuf addr %04X\n", val, (wifiMac.RXHWWriteCursor << 1)); /* move cursor by one */ //printf("written one word to %04X (start %04X, end %04X), ", wifiMac.RXHWWriteCursor, wifiMac.RXRangeBegin, wifiMac.RXRangeEnd); - wifiMac.RXHWWriteCursor++ ; + wifiMac.RXWriteCursor++; /* wrap around */ -// wifiMac.RXHWWriteCursor %= (wifiMac.RXRangeEnd - wifiMac.RXRangeBegin) >> 1 ; +// wifiMac.RXHWWriteCursor %= (wifiMac.RXRangeEnd - wifiMac.RXRangeBegin) >> 1; // printf("new addr=%04X\n", wifiMac.RXHWWriteCursor); - if(wifiMac.RXHWWriteCursor >= ((wifiMac.RXRangeEnd & 0x1FFE) >> 1)) - { - //printf("hehe! mario warp!\n"); - wifiMac.RXHWWriteCursor = ((wifiMac.RXRangeBegin & 0x1FFE) >> 1); - } + if(wifiMac.RXWriteCursor >= ((wifiMac.RXRangeEnd & 0x1FFE) >> 1)) + wifiMac.RXWriteCursor = ((wifiMac.RXRangeBegin & 0x1FFE) >> 1); } #endif @@ -781,10 +768,10 @@ static void WIFI_TXStart(u8 slot) } //printf("---------- SENDING A PACKET ON SLOT %i, FrameCtl = %04X ----------\n", - // slot, wifiMac.circularBuffer[address+6]); + // slot, wifiMac.RAM[address+6]); // 12 byte header TX Header: http://www.akkit.org/info/dswifi.htm#FmtTx - txLen = wifiMac.circularBuffer[address+5]; + txLen = wifiMac.RAM[address+5]; // zero length if (txLen == 0) { @@ -797,30 +784,30 @@ static void WIFI_TXStart(u8 slot) txLen = WIFI_alignedLen(txLen); // unsupported txRate - switch (wifiMac.circularBuffer[address+4] & 0xFF) + switch (wifiMac.RAM[address+4] & 0xFF) { case 10: // 1 mbit case 20: // 2 mbit break; default: // other rates WIFI_LOG(1, "TX slot %i trying to send a packet with transfer rate field set to an invalid value of %i. Attempt ignored.\n", - slot, wifiMac.circularBuffer[address+4] & 0xFF); + slot, wifiMac.RAM[address+4] & 0xFF); return; } // Set sequence number if required if (!BIT13(wifiMac.TXSlot[slot])) { - // u16 seqctl = wifiMac.circularBuffer[address + 6 + 22]; - // wifiMac.circularBuffer[address + 6 + 11] = (seqctl & 0x000F) | (wifiMac.TXSeqNo << 4); - wifiMac.circularBuffer[address + 6 + 11] = wifiMac.TXSeqNo << 4; + // u16 seqctl = wifiMac.RAM[address + 6 + 22]; + // wifiMac.RAM[address + 6 + 11] = (seqctl & 0x000F) | (wifiMac.TXSeqNo << 4); + wifiMac.RAM[address + 6 + 11] = wifiMac.TXSeqNo << 4; } // Calculate and set FCS - u32 crc32 = WIFI_calcCRC32((u8*)&wifiMac.circularBuffer[address + 6], txLen - 4); - *(u32*)&wifiMac.circularBuffer[address + 6 + ((txLen-4) >> 1)] = crc32; + u32 crc32 = WIFI_calcCRC32((u8*)&wifiMac.RAM[address + 6], txLen - 4); + *(u32*)&wifiMac.RAM[address + 6 + ((txLen-4) >> 1)] = crc32; - WIFI_triggerIRQ(WIFI_IRQ_TXSTART) ; + WIFI_triggerIRQ(WIFI_IRQ_TXSTART); if(slot > wifiMac.txCurSlot) wifiMac.txCurSlot = slot; @@ -836,11 +823,11 @@ static void WIFI_TXStart(u8 slot) wifiMac.rfPins = 0x0046; #if 0 - WIFI_SoftAP_RecvPacketFromDS((u8*)&wifiMac.circularBuffer[address+6], txLen); - WIFI_triggerIRQ(WIFI_IRQ_TXEND) ; + WIFI_SoftAP_RecvPacketFromDS((u8*)&wifiMac.RAM[address+6], txLen); + WIFI_triggerIRQ(WIFI_IRQ_TXEND); - wifiMac.circularBuffer[address] = 0x0001; - wifiMac.circularBuffer[address+4] &= 0x00FF; + wifiMac.RAM[address] = 0x0001; + wifiMac.RAM[address+4] &= 0x00FF; #endif } } @@ -858,10 +845,10 @@ static void WIFI_ExtraTXStart() } //printf("---------- SENDING A PACKET ON EXTRA SLOT, FrameCtl = %04X ----------\n", - // wifiMac.circularBuffer[address+6]); + // wifiMac.RAM[address+6]); // 12 byte header TX Header: http://www.akkit.org/info/dswifi.htm#FmtTx - txLen = wifiMac.circularBuffer[address+5]; + txLen = wifiMac.RAM[address+5]; // zero length if (txLen == 0) { @@ -872,7 +859,7 @@ static void WIFI_ExtraTXStart() txLen = WIFI_alignedLen(txLen); // unsupported txRate - switch (wifiMac.circularBuffer[address+4] & 0xFF) + switch (wifiMac.RAM[address+4] & 0xFF) { case 10: // 1 mbit case 20: // 2 mbit @@ -884,19 +871,19 @@ static void WIFI_ExtraTXStart() // Set sequence number if required if (!BIT13(wifiMac.TXSlotExtra)) { - //u16 seqctl = wifiMac.circularBuffer[address + 6 + 22]; - //wifiMac.circularBuffer[address + 6 + 11] = (seqctl & 0x000F) | (wifiMac.TXSeqNo << 4); - wifiMac.circularBuffer[address + 6 + 11] = wifiMac.TXSeqNo << 4; + //u16 seqctl = wifiMac.RAM[address + 6 + 22]; + //wifiMac.RAM[address + 6 + 11] = (seqctl & 0x000F) | (wifiMac.TXSeqNo << 4); + wifiMac.RAM[address + 6 + 11] = wifiMac.TXSeqNo << 4; } // Calculate and set FCS - u32 crc32 = WIFI_calcCRC32((u8*)&wifiMac.circularBuffer[address + 6], txLen - 4); - *(u32*)&wifiMac.circularBuffer[address + 6 + ((txLen-4) >> 1)] = crc32; + u32 crc32 = WIFI_calcCRC32((u8*)&wifiMac.RAM[address + 6], txLen - 4); + *(u32*)&wifiMac.RAM[address + 6 + ((txLen-4) >> 1)] = crc32; // Note: Extra transfers trigger two TX start interrupts according to GBATek WIFI_triggerIRQ(WIFI_IRQ_TXSTART); if(wifiCom) - wifiCom->SendPacket((u8*)&wifiMac.circularBuffer[address+6], txLen); + wifiCom->SendPacket((u8*)&wifiMac.RAM[address+6], txLen); WIFI_triggerIRQ(WIFI_IRQ_UNK); if (BIT13(wifiMac.TXStatCnt)) @@ -912,8 +899,8 @@ static void WIFI_ExtraTXStart() wifiMac.TXSlotExtra &= 0x7FFF; - wifiMac.circularBuffer[address] = 0x0001; - wifiMac.circularBuffer[address+4] &= 0x00FF; + wifiMac.RAM[address] = 0x0001; + wifiMac.RAM[address+4] &= 0x00FF; } } @@ -930,7 +917,7 @@ static void WIFI_BeaconTXStart() } // 12 byte header TX Header: http://www.akkit.org/info/dswifi.htm#FmtTx - txLen = wifiMac.circularBuffer[address+5]; + txLen = wifiMac.RAM[address+5]; // zero length if (txLen == 0) { @@ -941,7 +928,7 @@ static void WIFI_BeaconTXStart() txLen = WIFI_alignedLen(txLen); // unsupported txRate - switch (wifiMac.circularBuffer[address+4] & 0xFF) + switch (wifiMac.RAM[address+4] & 0xFF) { case 10: // 1 mbit case 20: // 2 mbit @@ -951,20 +938,20 @@ static void WIFI_BeaconTXStart() } // Set sequence number - //u16 seqctl = wifiMac.circularBuffer[address + 6 + 22]; - //wifiMac.circularBuffer[address + 6 + 11] = (seqctl & 0x000F) | (wifiMac.TXSeqNo << 4); - wifiMac.circularBuffer[address + 6 + 11] = wifiMac.TXSeqNo << 4; + //u16 seqctl = wifiMac.RAM[address + 6 + 22]; + //wifiMac.RAM[address + 6 + 11] = (seqctl & 0x000F) | (wifiMac.TXSeqNo << 4); + wifiMac.RAM[address + 6 + 11] = wifiMac.TXSeqNo << 4; // Set timestamp - *(u64*)&wifiMac.circularBuffer[address + 6 + 12] = wifiMac.usec; + *(u64*)&wifiMac.RAM[address + 6 + 12] = wifiMac.usec; // Calculate and set FCS - u32 crc32 = WIFI_calcCRC32((u8*)&wifiMac.circularBuffer[address + 6], txLen - 4); - *(u32*)&wifiMac.circularBuffer[address + 6 + ((txLen-4) >> 1)] = crc32; + u32 crc32 = WIFI_calcCRC32((u8*)&wifiMac.RAM[address + 6], txLen - 4); + *(u32*)&wifiMac.RAM[address + 6 + ((txLen-4) >> 1)] = crc32; WIFI_triggerIRQ(WIFI_IRQ_TXSTART); if(wifiCom) - wifiCom->SendPacket((u8*)&wifiMac.circularBuffer[address+6], txLen); + wifiCom->SendPacket((u8*)&wifiMac.RAM[address+6], txLen); if (BIT15(wifiMac.TXStatCnt)) { @@ -972,132 +959,137 @@ static void WIFI_BeaconTXStart() wifiMac.TXStat = 0x0301; } - wifiMac.circularBuffer[address] = 0x0001; - wifiMac.circularBuffer[address+4] &= 0x00FF; + wifiMac.RAM[address] = 0x0001; + wifiMac.RAM[address+4] &= 0x00FF; } } void WIFI_write16(u32 address, u16 val) { - BOOL action = FALSE ; + BOOL action = FALSE; if (!nds.power2.wifi) return; + u32 page = address & 0x7000; + + // 0x2000 - 0x3FFF: unused + if ((page >= 0x2000) && (page < 0x4000)) + return; + WIFI_LOG(5, "Write at address %08X, %04X\n", address, val); //printf("WIFI: Write at address %08X, %04X, pc=%08X\n", address, val, NDS_ARM7.instruct_adr); - /* the first 0x1000 bytes are mirrored at +0x1000,+0x2000,+0x3000,+06000,+0x7000 */ - /* only the first mirror causes an special action */ - /* the gap at +0x4000 is filled with the circular bufferspace */ - /* the so created 0x8000 byte block is then mirrored till 0x04FFFFFF */ - /* see: http://www.akkit.org/info/dswifi.htm#Wifihwcap */ - if (((address & 0x00007000) >= 0x00004000) && ((address & 0x00007000) < 0x00006000)) + // 0x4000 - 0x5FFF: wifi RAM + if ((page >= 0x4000) && (page < 0x6000)) { /* access to the circular buffer */ - address &= 0x1FFF ; - wifiMac.circularBuffer[address >> 1] = val ; - return ; + address &= 0x1FFF; + wifiMac.RAM[address >> 1] = val; + return; } - if (!(address & 0x00007000)) action = TRUE ; - /* mirrors => register address */ - address &= 0x00000FFF ; + + // anything else: I/O ports + // only the first mirror (0x0000 - 0x0FFF) causes a special action + if (page == 0x0000) action = TRUE; + + address &= 0x0FFF; switch (address) { case REG_WIFI_ID: - break ; + break; case REG_WIFI_MODE: { u16 oldval = wifiMac.macMode; if (!BIT0(oldval) && BIT0(val)) { - wifiMac.ioMem[0x034>>1] = 0x0002; - wifiMac.rfPins = 0x0046; - wifiMac.rfStatus = 0x0009; - wifiMac.ioMem[0x27C>>1] = 0x0005; + WIFI_IOREG(0x034) = 0x0002; + wifiMac.rfPins = 0x0046; + wifiMac.rfStatus = 0x0009; + WIFI_IOREG(0x27C) = 0x0005; } if (BIT0(oldval) && !BIT0(val)) { - wifiMac.ioMem[0x27C>>1] = 0x000A; + WIFI_IOREG(0x27C) = 0x000A; } if (BIT13(val)) { - wifiMac.ioMem[0x056>>1] = 0x0000; - wifiMac.ioMem[0x0C0>>1] = 0x0000; - wifiMac.ioMem[0x0C4>>1] = 0x0000; - wifiMac.ioMem[0x1A4>>1] = 0x0000; - wifiMac.ioMem[0x278>>1] = 0x000F; + WIFI_IOREG(REG_WIFI_WRITECSRLATCH) = 0x0000; + WIFI_IOREG(0x0C0) = 0x0000; + WIFI_IOREG(0x0C4) = 0x0000; + WIFI_IOREG(REG_WIFI_MAYBE_RATE) = 0x0000; + WIFI_IOREG(0x278) = 0x000F; } if (BIT14(val)) { - wifiMac.wepMode = 0x0000; - wifiMac.TXStatCnt = 0x0000; - wifiMac.ioMem[0x00A>>1] = 0x0000; - wifiMac.mac.words[0] = 0x0000; - wifiMac.mac.words[1] = 0x0000; - wifiMac.mac.words[2] = 0x0000; - wifiMac.bss.words[0] = 0x0000; - wifiMac.bss.words[1] = 0x0000; - wifiMac.bss.words[2] = 0x0000; - wifiMac.pid = 0x0000; - wifiMac.aid = 0x0000; - wifiMac.ioMem[0x02C>>1] = 0x0707; - wifiMac.ioMem[0x02E>>1] = 0x0000; - wifiMac.RXRangeBegin = 0x4000; - wifiMac.RXRangeEnd = 0x4800; - wifiMac.ioMem[0x084>>1] = 0x0000; - wifiMac.ioMem[0x0BC>>1] = 0x0001; - wifiMac.ioMem[0x0D0>>1] = 0x0401; - wifiMac.ioMem[0x0D4>>1] = 0x0001; - wifiMac.ioMem[0x0E0>>1] = 0x0008; - wifiMac.ioMem[0x0EC>>1] = 0x3F03; - wifiMac.ioMem[0x194>>1] = 0x0000; - wifiMac.ioMem[0x198>>1] = 0x0000; - wifiMac.ioMem[0x1A2>>1] = 0x0001; - wifiMac.ioMem[0x224>>1] = 0x0003; - wifiMac.ioMem[0x230>>1] = 0x0047; + wifiMac.wepMode = 0x0000; + wifiMac.TXStatCnt = 0x0000; + WIFI_IOREG(REG_WIFI_0A) = 0x0000; + wifiMac.mac.words[0] = 0x0000; + wifiMac.mac.words[1] = 0x0000; + wifiMac.mac.words[2] = 0x0000; + wifiMac.bss.words[0] = 0x0000; + wifiMac.bss.words[1] = 0x0000; + wifiMac.bss.words[2] = 0x0000; + wifiMac.pid = 0x0000; + wifiMac.aid = 0x0000; + WIFI_IOREG(REG_WIFI_RETRYLIMIT) = 0x0707; + WIFI_IOREG(0x02E) = 0x0000; + wifiMac.RXRangeBegin = 0x4000; + wifiMac.RXRangeEnd = 0x4800; + WIFI_IOREG(0x084) = 0x0000; + WIFI_IOREG(REG_WIFI_PREAMBLE) = 0x0001; + WIFI_IOREG(REG_WIFI_RXFILTER) = 0x0401; + WIFI_IOREG(0x0D4) = 0x0001; + WIFI_IOREG(REG_WIFI_RXFILTER2) = 0x0008; + WIFI_IOREG(0x0EC) = 0x3F03; + WIFI_IOREG(0x194) = 0x0000; + WIFI_IOREG(0x198) = 0x0000; + WIFI_IOREG(0x1A2) = 0x0001; + WIFI_IOREG(0x224) = 0x0003; + WIFI_IOREG(0x230) = 0x0047; } wifiMac.macMode = val & 0xAFFF; } - break ; + break; case REG_WIFI_WEP: - wifiMac.wepMode = val ; - break ; + wifiMac.wepMode = val; + break; case REG_WIFI_TXSTATCNT: wifiMac.TXStatCnt = val; //printf("txstatcnt=%04X\n", val); break; case REG_WIFI_IE: - wifiMac.IE.val = val ; + wifiMac.IE = val; //printf("wifi ie write %04X\n", val); - break ; + break; case REG_WIFI_IF: - wifiMac.IF.val &= ~val ; /* clear flagging bits */ - break ; + wifiMac.IF &= ~val; /* clear flagging bits */ + break; case REG_WIFI_MAC0: case REG_WIFI_MAC1: case REG_WIFI_MAC2: - wifiMac.mac.words[(address - REG_WIFI_MAC0) >> 1] = val ; - break ; + wifiMac.mac.words[(address - REG_WIFI_MAC0) >> 1] = val; + break; case REG_WIFI_BSS0: case REG_WIFI_BSS1: case REG_WIFI_BSS2: - wifiMac.bss.words[(address - REG_WIFI_BSS0) >> 1] = val ; - break ; + wifiMac.bss.words[(address - REG_WIFI_BSS0) >> 1] = val; + break; case REG_WIFI_RETRYLIMIT: - wifiMac.retryLimit = val ; - break ; + wifiMac.retryLimit = val; + break; case REG_WIFI_WEPCNT: - wifiMac.WEP_enable = (val & 0x8000) != 0 ; - break ; + wifiMac.WEP_enable = (val & 0x8000) != 0; + break; case REG_WIFI_POWERSTATE: wifiMac.powerOn = ((val & 0x0002)?TRUE:FALSE); if(wifiMac.powerOn) WIFI_triggerIRQ(WIFI_IRQ_RFWAKEUP); break; - case REG_WIFI_FORCEPS: + case REG_WIFI_POWERFORCE: if((val & 0x8000) && (!wifiMac.powerOnPending)) { /* BOOL newPower = ((val & 0x0001)?FALSE:TRUE); @@ -1125,66 +1117,55 @@ void WIFI_write16(u32 address, u16 val) wifiMac.RXCnt = val & 0xFF0E; if(BIT0(val)) { - wifiMac.RXHWWriteCursor = wifiMac.RXHWWriteCursorReg = wifiMac.RXHWWriteCursorLatched; + wifiMac.RXWriteCursor = WIFI_IOREG(REG_WIFI_WRITECSRLATCH); + WIFI_IOREG(REG_WIFI_RXHWWRITECSR) = wifiMac.RXWriteCursor; } - if (BIT7(val)) - printf("latch port 094 into port 098 (rxcnt=%04X)\n", val); break; case REG_WIFI_RXRANGEBEGIN: - //printf("rxrangebegin=%04X\n", val); - wifiMac.RXRangeBegin = val ; - if(wifiMac.RXHWWriteCursor < ((val & 0x1FFE) >> 1)) - wifiMac.RXHWWriteCursor = ((val & 0x1FFE) >> 1); - break ; + wifiMac.RXRangeBegin = val; + if(wifiMac.RXWriteCursor < ((val & 0x1FFE) >> 1)) + wifiMac.RXWriteCursor = ((val & 0x1FFE) >> 1); + break; case REG_WIFI_RXRANGEEND: - // printf("rxrangeend=%04X\n", val); - wifiMac.RXRangeEnd = val ; - if(wifiMac.RXHWWriteCursor >= ((val & 0x1FFE) >> 1)) - wifiMac.RXHWWriteCursor = ((wifiMac.RXRangeBegin & 0x1FFE) >> 1); - break ; - case REG_WIFI_WRITECSRLATCH: - if (action) /* only when action register and CSR change enabled */ - { - wifiMac.RXHWWriteCursorLatched = val ; - } - break ; + wifiMac.RXRangeEnd = val; + if(wifiMac.RXWriteCursor >= ((val & 0x1FFE) >> 1)) + wifiMac.RXWriteCursor = ((wifiMac.RXRangeBegin & 0x1FFE) >> 1); + break; + case REG_WIFI_CIRCBUFRADR: wifiMac.CircBufReadAddress = (val & 0x1FFE); - break ; + break; case REG_WIFI_RXREADCSR: - //printf("advance rxreadcsr from %04X to %04X, pc=%08X\n", wifiMac.RXReadCursor<<1, val<<1, NDS_ARM7.instruct_adr); - //if ((val > wifiMac.RXHWWriteCursor) && ((val - wifiMac.RXHWWriteCursor) < 16)) - // printf("AAAAAAAAAAAAAARRRRRRRRRRRRRRGGGGGGGGGGGGGGGHHHHHHHHHHHH!!!!!!! WRCSR off by %i bytes\n", (val-wifiMac.RXHWWriteCursor)<<1); - wifiMac.RXReadCursor = val ; - break ; + wifiMac.RXReadCursor = val; + break; case REG_WIFI_CIRCBUFWADR: - wifiMac.CircBufWriteAddress = val ; - break ; + wifiMac.CircBufWriteAddress = val; + break; case REG_WIFI_CIRCBUFWRITE: /* set value into the circ buffer, and move cursor to the next hword on action */ //printf("wifi: circbuf fifo write at %04X, %04X (action=%i)\n", (wifiMac.CircBufWriteAddress & 0x1FFF), val, action); - wifiMac.circularBuffer[(wifiMac.CircBufWriteAddress >> 1) & 0xFFF] = val ; + wifiMac.RAM[(wifiMac.CircBufWriteAddress >> 1) & 0xFFF] = val; if (action) { /* move to next hword */ - wifiMac.CircBufWriteAddress+=2 ; + wifiMac.CircBufWriteAddress+=2; if (wifiMac.CircBufWriteAddress == wifiMac.CircBufWrEnd) { /* on end of buffer, add skip hwords to it */ - wifiMac.CircBufWrEnd += wifiMac.CircBufWrSkip * 2 ; + wifiMac.CircBufWrEnd += wifiMac.CircBufWrSkip * 2; } } - break ; + break; case REG_WIFI_CIRCBUFWR_SKIP: - wifiMac.CircBufWrSkip = val ; - break ; + wifiMac.CircBufWrSkip = val; + break; case REG_WIFI_TXLOCBEACON: wifiMac.BeaconAddr = val & 0x0FFF; wifiMac.BeaconEnable = BIT15(val); if (wifiMac.BeaconEnable) WIFI_LOG(3, "Beacon transmission enabled to send the packet at %08X every %i milliseconds.\n", 0x04804000 + (wifiMac.BeaconAddr << 1), wifiMac.BeaconInterval); - break ; + break; case REG_WIFI_TXLOCEXTRA: wifiMac.TXSlotExtra = val; WIFI_LOG(2, "Write to port %03X: %04X\n", address, val); @@ -1208,86 +1189,69 @@ void WIFI_write16(u32 address, u16 val) case REG_WIFI_TXLOC1: case REG_WIFI_TXLOC2: case REG_WIFI_TXLOC3: - wifiMac.TXSlot[(address - REG_WIFI_TXLOC1) >> 2] = val ; + wifiMac.TXSlot[(address - REG_WIFI_TXLOC1) >> 2] = val; WIFI_LOG(2, "Write to port %03X: %04X\n", address, val); - break ; + break; case REG_WIFI_TXRESET: WIFI_LOG(3, "Write to TXRESET: %04X\n", val); //if (val & 0x0001) wifiMac.TXSlot[0] &= 0x7FFF; //if (val & 0x0004) wifiMac.TXSlot[1] &= 0x7FFF; //if (val & 0x0008) wifiMac.TXSlot[2] &= 0x7FFF; break; - case REG_WIFI_TXOPT: + case REG_WIFI_TXREQ_RESET: wifiMac.TXCnt &= ~val; - //if (val) printf("TXReqReset: %04X\n", val); - break ; - case REG_WIFI_TXCNT: + break; + case REG_WIFI_TXREQ_SET: wifiMac.TXCnt |= val; if (BIT0(val)) WIFI_TXStart(0); if (BIT1(val)) WIFI_ExtraTXStart(); if (BIT2(val)) WIFI_TXStart(1); if (BIT3(val)) WIFI_TXStart(2); //if (val) printf("TXReq: %04X\n", val); - break ; - case REG_WIFI_TXSEQNO: - WIFI_LOG(3, "Set TXSeqNo to %04X.\n", val); - //wifiMac.TXSeqNo = val & 0x0FFF; break; - case REG_WIFI_RFIOCNT: - WIFI_setRF_CNT(val) ; - break ; - case REG_WIFI_RFIOBSY: + case REG_WIFI_RFCNT: + WIFI_setRF_CNT(val); + break; + case REG_WIFI_RFBUSY: /* CHECKME: read only? */ - break ; - case REG_WIFI_RFIODATA1: - WIFI_setRF_DATA(val,0) ; - break ; - case REG_WIFI_RFIODATA2: - WIFI_setRF_DATA(val,1) ; - break ; + break; + case REG_WIFI_RFDATA1: + WIFI_setRF_DATA(val,0); + break; + case REG_WIFI_RFDATA2: + WIFI_setRF_DATA(val,1); + break; case REG_WIFI_USCOUNTERCNT: wifiMac.usecEnable = BIT0(val); - break ; + break; case REG_WIFI_USCOMPARECNT: wifiMac.ucmpEnable = BIT0(val); - if (val & 2) printf("----- FORCE IRQ 14 -----\n"); - //printf("usec compare %s\n", wifiMac.usecEnable?"enabled":"disabled"); - break ; + break; case REG_WIFI_USCOUNTER0: - //printf("write uscounter.word0 : %04X\n", val); wifiMac.usec = (wifiMac.usec & 0xFFFFFFFFFFFF0000ULL) | (u64)val; break; case REG_WIFI_USCOUNTER1: - //printf("write uscounter.word1 : %04X\n", val); wifiMac.usec = (wifiMac.usec & 0xFFFFFFFF0000FFFFULL) | (u64)val << 16; break; case REG_WIFI_USCOUNTER2: - //printf("write uscounter.word2 : %04X\n", val); wifiMac.usec = (wifiMac.usec & 0xFFFF0000FFFFFFFFULL) | (u64)val << 32; break; case REG_WIFI_USCOUNTER3: - //printf("write uscounter.word3 : %04X\n", val); wifiMac.usec = (wifiMac.usec & 0x0000FFFFFFFFFFFFULL) | (u64)val << 48; break; case REG_WIFI_USCOMPARE0: - //printf("write uscompare.word0 : %04X (%08X%08X / %08X%08X, diff=%i)\n", - // val, (u32)(wifiMac.SoftAP.usecCounter >> 32), (u32)wifiMac.SoftAP.usecCounter, - // (u32)(wifiMac.usec >> 32), (u32)wifiMac.usec, (int)(wifiMac.ucmp-1-wifiMac.SoftAP.usecCounter)); wifiMac.ucmp = (wifiMac.ucmp & 0xFFFFFFFFFFFF0000ULL) | (u64)(val & 0xFFFE); //if (BIT0(val)) // WIFI_triggerIRQ(14); // wifiMac.usec = wifiMac.ucmp; break; case REG_WIFI_USCOMPARE1: - //printf("write uscompare.word1 : %04X\n", val); wifiMac.ucmp = (wifiMac.ucmp & 0xFFFFFFFF0000FFFFULL) | (u64)val << 16; break; case REG_WIFI_USCOMPARE2: - //printf("write uscompare.word2 : %04X\n", val); wifiMac.ucmp = (wifiMac.ucmp & 0xFFFF0000FFFFFFFFULL) | (u64)val << 32; break; case REG_WIFI_USCOMPARE3: - //printf("write uscompare.word3 : %04X\n", val); wifiMac.ucmp = (wifiMac.ucmp & 0x0000FFFFFFFFFFFFULL) | (u64)val << 48; break; case REG_WIFI_BEACONPERIOD: @@ -1299,21 +1263,18 @@ void WIFI_write16(u32 address, u16 val) case REG_WIFI_BEACONCOUNT2: wifiMac.BeaconCount2 = val; break; - case REG_WIFI_BBSIOCNT: - WIFI_setBB_CNT(val) ; - break ; - case REG_WIFI_BBSIOWRITE: - WIFI_setBB_DATA(val&0xFF) ; - break ; + case REG_WIFI_BBCNT: + WIFI_setBB_CNT(val); + break; case REG_WIFI_RXBUF_COUNT: - wifiMac.RXBufCount = val & 0x0FFF ; - break ; + wifiMac.RXBufCount = val & 0x0FFF; + break; case REG_WIFI_EXTRACOUNTCNT: wifiMac.eCountEnable = BIT0(val); - break ; + break; case REG_WIFI_EXTRACOUNT: wifiMac.eCount = (u32)val * 10; - break ; + break; case REG_WIFI_LISTENINT: wifiMac.ListenInterval = val & 0x00FF; break; @@ -1322,95 +1283,84 @@ void WIFI_write16(u32 address, u16 val) break; case REG_WIFI_POWER_US: wifiMac.crystalEnabled = !BIT0(val); - break ; + break; case REG_WIFI_IF_SET: - WIFI_triggerIRQMask(val) ; - break ; + WIFI_triggerIRQMask(val); + break; case REG_WIFI_CIRCBUFRD_END: - wifiMac.CircBufRdEnd = (val & 0x1FFE) ; - break ; + wifiMac.CircBufRdEnd = (val & 0x1FFE); + break; case REG_WIFI_CIRCBUFRD_SKIP: - wifiMac.CircBufRdSkip = val & 0xFFF ; - break ; + wifiMac.CircBufRdSkip = val & 0xFFF; + break; case REG_WIFI_AID_LOW: - wifiMac.pid = val & 0x0F ; - break ; + wifiMac.pid = val & 0x0F; + break; case REG_WIFI_AID_HIGH: - wifiMac.aid = val & 0x07FF ; - break ; + wifiMac.aid = val & 0x07FF; + break; case 0xD0: //printf("wifi: rxfilter=%04X\n", val); break; + case 0x0E0: + // printf("wifi: rxfilter2=%04X\n", val); + break; + default: - // WIFI_LOG(2, "Write to unhandled port %03X: %04X\n", address, val); - // printf("wifi: write unhandled reg %03X, %04X\n", address, val); - // val = 0 ; /* not handled yet */ - break ; + break; } - wifiMac.ioMem[address >> 1] = val; + WIFI_IOREG(address) = val; } u16 WIFI_read16(u32 address) { - BOOL action = FALSE ; - u16 temp ; + BOOL action = FALSE; if (!nds.power2.wifi) return 0; - //if (address != 0x0480819C) - // printf("WIFI: Read at address %08X, pc=%08X, r1=%08X\n", address, NDS_ARM7.instruct_adr, NDS_ARM7.R[1]); + u32 page = address & 0x7000; + + // 0x2000 - 0x3FFF: unused + if ((page >= 0x2000) && (page < 0x4000)) + return 0xFFFF; WIFI_LOG(5, "Read at address %08X\n", address); - /* the first 0x1000 bytes are mirrored at +0x1000,+0x2000,+0x3000,+06000,+0x7000 */ - /* only the first mirror causes an special action */ - /* the gap at +0x4000 is filled with the circular bufferspace */ - /* the so created 0x8000 byte block is then mirrored till 0x04FFFFFF */ - /* see: http://www.akkit.org/info/dswifi.htm#Wifihwcap */ - if (((address & 0x00007000) >= 0x00004000) && ((address & 0x00007000) < 0x00006000)) + // 0x4000 - 0x5FFF: wifi RAM + if ((page >= 0x4000) && (page < 0x6000)) { - /* access to the circular buffer */ - - //if ((address == 0x04804BFC)) - // printf("read packet rxhdr.word0 at %08X\n", NDS_ARM7.instruct_adr); - //if ((address >= 0x04804BFC) && (address < 0x04804BFC + 12 + 0x70)) - // printf("read inside received beacon at %08X (offset %08X)\n", address, address-0x04804BFC); - - return wifiMac.circularBuffer[(address & 0x1FFF) >> 1] ; + return wifiMac.RAM[(address & 0x1FFF) >> 1]; } - if (!(address & 0x00007000)) action = TRUE ; - /* mirrors => register address */ - address &= 0x00000FFF ; - //if ((address >= 0x1B0) && (address < 0x1C0)) - // printf("read rxstat %03X\n", address); + // anything else: I/O ports + // only the first mirror causes a special action + if (page == 0x0000) action = TRUE; + + address &= 0x0FFF; switch (address) { case REG_WIFI_ID: - return WIFI_CHIPID ; + return WIFI_CHIPID; case REG_WIFI_MODE: - return wifiMac.macMode ; + return wifiMac.macMode; case REG_WIFI_WEP: - return wifiMac.wepMode ; + return wifiMac.wepMode; case REG_WIFI_IE: - return wifiMac.IE.val ; + return wifiMac.IE; case REG_WIFI_IF: - return wifiMac.IF.val ; + return wifiMac.IF; case REG_WIFI_POWERSTATE: return ((wifiMac.powerOn ? 0x0000 : 0x0200) | (wifiMac.powerOnPending ? 0x0102 : 0x0000)); - case REG_WIFI_RFIODATA1: - return WIFI_getRF_DATA(0) ; - case REG_WIFI_RFIODATA2: - return WIFI_getRF_DATA(1) ; - case REG_WIFI_RFIOBSY: - case REG_WIFI_BBSIOBUSY: - return 0 ; /* we are never busy :p */ - case REG_WIFI_BBSIOREAD: - return WIFI_getBB_DATA() ; + case REG_WIFI_RFDATA1: + return WIFI_getRF_DATA(0); + case REG_WIFI_RFDATA2: + return WIFI_getRF_DATA(1); + case REG_WIFI_RFBUSY: + case REG_WIFI_BBBUSY: + return 0; /* we are never busy :p */ + case REG_WIFI_BBREAD: + return WIFI_getBB_DATA(); case REG_WIFI_RANDOM: - /* FIXME: random generator */ -// return (rand() & 0x7FF); // disabled (no wonder there were desyncs...) - // probably not right, but it's better than using the unsaved and shared rand(). // at the very least, rand() shouldn't be used when movieMode is active. { @@ -1419,60 +1369,58 @@ u16 WIFI_read16(u32 address) return returnValue; } - return 0 ; + return 0; case REG_WIFI_MAC0: case REG_WIFI_MAC1: case REG_WIFI_MAC2: //printf("read mac addr: word %i = %02X\n", (address - REG_WIFI_MAC0) >> 1, wifiMac.mac.words[(address - REG_WIFI_MAC0) >> 1]); - return wifiMac.mac.words[(address - REG_WIFI_MAC0) >> 1] ; + return wifiMac.mac.words[(address - REG_WIFI_MAC0) >> 1]; case REG_WIFI_BSS0: case REG_WIFI_BSS1: case REG_WIFI_BSS2: //printf("read bssid addr: word %i = %02X\n", (address - REG_WIFI_BSS0) >> 1, wifiMac.bss.words[(address - REG_WIFI_BSS0) >> 1]); - return wifiMac.bss.words[(address - REG_WIFI_BSS0) >> 1] ; + return wifiMac.bss.words[(address - REG_WIFI_BSS0) >> 1]; case REG_WIFI_RXCNT: return wifiMac.RXCnt; case REG_WIFI_RXRANGEBEGIN: - return wifiMac.RXRangeBegin ; + return wifiMac.RXRangeBegin; case REG_WIFI_CIRCBUFREAD: - temp = wifiMac.circularBuffer[wifiMac.CircBufReadAddress >> 1] ; - if (action) { - wifiMac.CircBufReadAddress += 2 ; - - if (wifiMac.CircBufReadAddress >= wifiMac.RXRangeEnd) - { - wifiMac.CircBufReadAddress = wifiMac.RXRangeBegin; - } - else + u16 val = wifiMac.RAM[wifiMac.CircBufReadAddress >> 1]; + if (action) { - /* skip does not fire after a reset */ - if (wifiMac.CircBufReadAddress == wifiMac.CircBufRdEnd) + wifiMac.CircBufReadAddress += 2; + + if (wifiMac.CircBufReadAddress >= wifiMac.RXRangeEnd) + { + wifiMac.CircBufReadAddress = wifiMac.RXRangeBegin; + } + else { - wifiMac.CircBufReadAddress += wifiMac.CircBufRdSkip * 2 ; - wifiMac.CircBufReadAddress &= 0x1FFE ; - if (wifiMac.CircBufReadAddress + wifiMac.RXRangeBegin == wifiMac.RXRangeEnd) wifiMac.CircBufReadAddress = 0 ; + /* skip does not fire after a reset */ + if (wifiMac.CircBufReadAddress == wifiMac.CircBufRdEnd) + { + wifiMac.CircBufReadAddress += wifiMac.CircBufRdSkip * 2; + wifiMac.CircBufReadAddress &= 0x1FFE; + if (wifiMac.CircBufReadAddress + wifiMac.RXRangeBegin == wifiMac.RXRangeEnd) wifiMac.CircBufReadAddress = 0; + } } + + if (wifiMac.RXBufCount > 0) + { + if (wifiMac.RXBufCount == 1) + { + WIFI_triggerIRQ(WIFI_IRQ_RXCOUNTEXP); + } + wifiMac.RXBufCount--; + } } - - if (wifiMac.RXBufCount > 0) - { - if (wifiMac.RXBufCount == 1) - { - WIFI_triggerIRQ(WIFI_IRQ_RXCOUNTEXP) ; - } - wifiMac.RXBufCount-- ; - } + return val; } - return temp; case REG_WIFI_CIRCBUFRADR: - return wifiMac.CircBufReadAddress ; - case REG_WIFI_RXHWWRITECSR: - return wifiMac.RXHWWriteCursorReg; - case REG_WIFI_RXREADCSR: - return wifiMac.RXReadCursor; + return wifiMac.CircBufReadAddress; case REG_WIFI_RXBUF_COUNT: - return wifiMac.RXBufCount ; + return wifiMac.RXBufCount; case REG_WIFI_TXREQ_READ: //printf("read TX reg %04X\n", address); return wifiMac.TXCnt | 0x10; @@ -1494,7 +1442,7 @@ u16 WIFI_read16(u32 address) //printf("read TX reg %04X\n", address); break; case REG_WIFI_EXTRACOUNTCNT: - return wifiMac.eCountEnable?1:0 ; + return wifiMac.eCountEnable?1:0; case REG_WIFI_EXTRACOUNT: return (u16)((wifiMac.eCount + 9) / 10); case REG_WIFI_USCOUNTER0: @@ -1520,21 +1468,24 @@ u16 WIFI_read16(u32 address) case REG_WIFI_LISTENCOUNT: return wifiMac.ListenCount; case REG_WIFI_POWER_US: - return wifiMac.crystalEnabled?0:1 ; + return wifiMac.crystalEnabled?0:1; case REG_WIFI_CIRCBUFRD_END: - return wifiMac.CircBufRdEnd ; + return wifiMac.CircBufRdEnd; case REG_WIFI_CIRCBUFRD_SKIP: - return wifiMac.CircBufRdSkip ; + return wifiMac.CircBufRdSkip; case REG_WIFI_AID_LOW: - return wifiMac.pid ; + return wifiMac.pid; case REG_WIFI_AID_HIGH: - return wifiMac.aid ; + return wifiMac.aid; case REG_WIFI_RFSTATUS: //WIFI_LOG(3, "Read RF_STATUS: %04X\n", wifiMac.rfStatus); + //printf("-------------------- read RFSTATUS at %08X -----------------------------\n", NDS_ARM7.instruct_adr); return 0x0009; //return wifiMac.rfStatus; case REG_WIFI_RFPINS: //WIFI_LOG(3, "Read RF_PINS: %04X\n", wifiMac.rfPins); + //emu_halt(); + // printf("-------------------- read RFPINS at %08X -----------------------------\n", NDS_ARM7.instruct_adr); //return wifiMac.rfPins; return 0x0004; case 0x210: @@ -1553,11 +1504,13 @@ u16 WIFI_read16(u32 address) // printf("wifi: Read from port %03X\n", address); case 0x268: return wifiMac.RXTXAddr; + default: // printf("wifi: read unhandled reg %03X\n", address); - return wifiMac.ioMem[address >> 1]; + break; } - return wifiMac.ioMem[address >> 1]; + + return WIFI_IOREG(address); } @@ -1567,7 +1520,7 @@ void WIFI_usTrigger() { /* a usec has passed */ if (wifiMac.usecEnable) - wifiMac.usec++ ; + wifiMac.usec++; // Note: the extra counter is decremented every 10 microseconds. // To avoid a modulo every microsecond, we multiply the counter @@ -1588,7 +1541,7 @@ void WIFI_usTrigger() { wifiMac.BeaconCount1--; - if (wifiMac.BeaconCount1 == (wifiMac.ioMem[0x110>>1] >> 10)) + if (wifiMac.BeaconCount1 == (WIFI_IOREG(REG_WIFI_PREBEACONCOUNT) >> 10)) WIFI_triggerIRQ(WIFI_IRQ_TIMEPREBEACON); else if (wifiMac.BeaconCount1 == 0) WIFI_triggerIRQ(WIFI_IRQ_TIMEBEACON); @@ -1605,7 +1558,7 @@ void WIFI_usTrigger() if ((wifiMac.ucmpEnable) && (wifiMac.ucmp == wifiMac.usec)) { //printf("ucmp irq14\n"); - WIFI_triggerIRQ(WIFI_IRQ_TIMEBEACON) ; + WIFI_triggerIRQ(WIFI_IRQ_TIMEBEACON); } if((wifiMac.usec & 3) == 0) @@ -1622,13 +1575,13 @@ void WIFI_usTrigger() wifiMac.TXSlot[slot] &= 0x7FFF; if(wifiCom) - wifiCom->SendPacket((u8*)&wifiMac.circularBuffer[wifiMac.txSlotAddr[slot]+6], wifiMac.txSlotLen[slot]); + wifiCom->SendPacket((u8*)&wifiMac.RAM[wifiMac.txSlotAddr[slot]+6], wifiMac.txSlotLen[slot]); while((wifiMac.txSlotBusy[wifiMac.txCurSlot] == 0) && (wifiMac.txCurSlot > 0)) wifiMac.txCurSlot--; - wifiMac.circularBuffer[wifiMac.txSlotAddr[slot]] = 0x0001; - wifiMac.circularBuffer[wifiMac.txSlotAddr[slot]+4] &= 0x00FF; + wifiMac.RAM[wifiMac.txSlotAddr[slot]] = 0x0001; + wifiMac.RAM[wifiMac.txSlotAddr[slot]+4] &= 0x00FF; wifiMac.TXStat = (0x0001 | (slot << 12)); @@ -1672,7 +1625,7 @@ bool Adhoc_Init() { int res; - wifiMac.Adhoc.usecCounter = 0; + Adhoc.usecCounter = 0; if (!driver->WIFI_SocketsAvailable()) { @@ -1731,7 +1684,7 @@ void Adhoc_DeInit() void Adhoc_Reset() { - wifiMac.Adhoc.usecCounter = 0; + Adhoc.usecCounter = 0; } void Adhoc_SendPacket(u8* packet, u32 len) @@ -1764,13 +1717,13 @@ void Adhoc_SendPacket(u8* packet, u32 len) void Adhoc_usTrigger() { - wifiMac.Adhoc.usecCounter++; + Adhoc.usecCounter++; if (wifi_socket < 0) return; // Check every millisecond if we received a packet - if (!(wifiMac.Adhoc.usecCounter & 1023)) + if (!(Adhoc.usecCounter & 1023)) { fd_set fd; struct timeval tv; @@ -1849,7 +1802,8 @@ void Adhoc_usTrigger() WIFI_RXPutWord(word); } - wifiMac.RXHWWriteCursorReg = ((wifiMac.RXHWWriteCursor + 1) & (~1)); + wifiMac.RXWriteCursor = ((wifiMac.RXWriteCursor + 1) & (~1)); + WIFI_IOREG(REG_WIFI_RXHWWRITECSR) = wifiMac.RXWriteCursor; wifiMac.RXNum++; WIFI_triggerIRQ(WIFI_IRQ_RXEND); } @@ -1864,6 +1818,11 @@ void Adhoc_usTrigger() *******************************************************************************/ +// Note on the CRC32 field in received packets: +// The wifi hardware doesn't store the CRC32 in memory when receiving a packet +// so the RX header length field is indeed header+body +// Hence the CRC32 has been removed from those templates. + const u8 SoftAP_MACAddr[6] = {0x00, 0xF0, 0x1A, 0x2B, 0x3C, 0x4D}; const u8 SoftAP_Beacon[] = { @@ -1877,17 +1836,33 @@ const u8 SoftAP_Beacon[] = { /* Frame body */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Timestamp (modified later) - 0x64, 0x00, // Beacon interval + 0x80, 0x00, // Beacon interval 0x0F, 0x00, // Capablilty information 0x01, 0x02, 0x82, 0x84, // Supported rates 0x03, 0x01, 0x06, // Current channel 0x05, 0x04, 0x00, 0x00, 0x00, 0x00, // TIM 0x00, 0x06, 'S', 'o', 'f', 't', 'A', 'P', // SSID - - /* CRC32 */ - 0x00, 0x00, 0x00, 0x00 }; +const u8 SoftAP_ProbeResponse[] = { + /* 802.11 header */ + 0x50, 0x00, // Frame control + 0x00, 0x00, // Duration ID + 0x00, 0x09, 0xBF, 0x12, 0x34, 0x56, // Receiver + 0x00, 0xF0, 0x1A, 0x2B, 0x3C, 0x4D, // Sender + 0x00, 0xF0, 0x1A, 0x2B, 0x3C, 0x4D, // BSSID + 0x00, 0x00, // Sequence control + + /* Frame body */ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Timestamp (modified later) + 0x80, 0x00, // Beacon interval + 0x0F, 0x00, // Capablilty information + 0x01, 0x02, 0x82, 0x84, // Supported rates + 0x03, 0x01, 0x06, // Current channel + 0x00, 0x06, 'S', 'o', 'f', 't', 'A', 'P', // SSID +}; + + const u8 SoftAP_AuthFrame[] = { /* 802.11 header */ 0xB0, 0x00, // Frame control @@ -1901,9 +1876,6 @@ const u8 SoftAP_AuthFrame[] = { 0x00, 0x00, // Authentication algorithm 0x02, 0x00, // Authentication sequence 0x00, 0x00, // Status - - /* CRC32 */ - 0x00, 0x00, 0x00, 0x00 }; const u8 SoftAP_AssocResponse[] = { @@ -1920,9 +1892,6 @@ const u8 SoftAP_AssocResponse[] = { 0x00, 0x00, // Status 0x01, 0xC0, // Assocation ID 0x01, 0x02, 0x82, 0x84, // Supported rates - - /* CRC32 */ - 0x00, 0x00, 0x00, 0x00 }; //todo - make a class to wrap this @@ -1939,15 +1908,18 @@ static pcap_if_t * WIFI_index_device(pcap_if_t *alldevs, int index) bool SoftAP_Init() { - wifiMac.SoftAP.usecCounter = 0; + SoftAP.usecCounter = 0; - wifiMac.SoftAP.curPacketSize = 0; - wifiMac.SoftAP.curPacketPos = 0; - wifiMac.SoftAP.curPacketSending = FALSE; + SoftAP.curPacketSize = 0; + SoftAP.curPacketPos = 0; + SoftAP.curPacketSending = FALSE; + + SoftAP.status = APStatus_Disconnected; + SoftAP.seqNum = 0; if (!driver->WIFI_PCapAvailable()) { - WIFI_LOG(1, "SoftAP: failed to initialize PCap.\n"); + WIFI_LOG(1, "SoftAP: PCap library not available on your system.\n"); wifi_bridge = NULL; return false; } @@ -1990,18 +1962,21 @@ void SoftAP_DeInit() void SoftAP_Reset() { - wifiMac.SoftAP.usecCounter = 0; + SoftAP.usecCounter = 0; - wifiMac.SoftAP.curPacketSize = 0; - wifiMac.SoftAP.curPacketPos = 0; - wifiMac.SoftAP.curPacketSending = FALSE; + SoftAP.curPacketSize = 0; + SoftAP.curPacketPos = 0; + SoftAP.curPacketSending = FALSE; + + SoftAP.status = APStatus_Disconnected; + SoftAP.seqNum = 0; } void SoftAP_SendPacket(u8 *packet, u32 len) { u16 frameCtl = *(u16*)&packet[0]; - WIFI_LOG(3, "SoftAP: Received a packet of length %i bytes. Frame control = %04X\n", + /*WIFI_LOG(3, */printf("SoftAP: Received a packet of length %i bytes. Frame control = %04X\n", len, frameCtl); switch((frameCtl >> 2) & 0x3) @@ -2010,99 +1985,123 @@ void SoftAP_SendPacket(u8 *packet, u32 len) { switch((frameCtl >> 4) & 0xF) { + case 0x4: // Probe request (WFC) + { + u32 packetLen = sizeof(SoftAP_ProbeResponse); + u32 totalLen = (packetLen + 12); + + // Make the RX header + WIFI_MakeRXHeader(SoftAP.curPacket, 0x0010, 20, packetLen, 0, 0); + + // Copy the probe response template + memcpy(&SoftAP.curPacket[12], SoftAP_ProbeResponse, packetLen); + + // Add the MAC address + memcpy(&SoftAP.curPacket[12 + 4], FW_Mac, 6); + + // Add the timestamp + u64 timestamp = SoftAP.usecCounter; + *(u64*)&SoftAP.curPacket[12 + 24] = timestamp; + + // Let's prepare to send + SoftAP.curPacketSize = totalLen; + SoftAP.curPacketPos = 0; + SoftAP.curPacketSending = TRUE; + } + break; + case 0xB: // Authentication { u32 packetLen = sizeof(SoftAP_AuthFrame); u32 totalLen = (packetLen + 12); // Make the RX header - WIFI_MakeRXHeader(wifiMac.SoftAP.curPacket, 0x0010, 20, packetLen, 0, 0); + WIFI_MakeRXHeader(SoftAP.curPacket, 0x0010, 20, packetLen, 0, 0); // Copy the authentication frame template - memcpy(&wifiMac.SoftAP.curPacket[12], SoftAP_AuthFrame, packetLen); + memcpy(&SoftAP.curPacket[12], SoftAP_AuthFrame, packetLen); // Add the MAC address - memcpy(&wifiMac.SoftAP.curPacket[12 + 4], FW_Mac, 6); - - // And the CRC32 (FCS) - u32 crc32 = WIFI_calcCRC32(&wifiMac.SoftAP.curPacket[12], (packetLen - 4)); - *(u32*)&wifiMac.SoftAP.curPacket[12 + packetLen - 4] = crc32; + memcpy(&SoftAP.curPacket[12 + 4], FW_Mac, 6); // Let's prepare to send - wifiMac.SoftAP.curPacketSize = WIFI_alignedLen(totalLen); - wifiMac.SoftAP.curPacketPos = 0; - wifiMac.SoftAP.curPacketSending = TRUE; + SoftAP.curPacketSize = totalLen; + SoftAP.curPacketPos = 0; + SoftAP.curPacketSending = TRUE; + + SoftAP.status = APStatus_Authenticated; } break; case 0x0: // Association request { + if (SoftAP.status != APStatus_Authenticated) + return; + u32 packetLen = sizeof(SoftAP_AssocResponse); u32 totalLen = (packetLen + 12); // Make the RX header - WIFI_MakeRXHeader(wifiMac.SoftAP.curPacket, 0x0010, 20, packetLen, 0, 0); + WIFI_MakeRXHeader(SoftAP.curPacket, 0x0010, 20, packetLen, 0, 0); // Copy the association response template - memcpy(&wifiMac.SoftAP.curPacket[12], SoftAP_AssocResponse, packetLen); + memcpy(&SoftAP.curPacket[12], SoftAP_AssocResponse, packetLen); // Add the MAC address - memcpy(&wifiMac.SoftAP.curPacket[12 + 4], FW_Mac, 6); - - // And the CRC32 (FCS) - u32 crc32 = WIFI_calcCRC32(&wifiMac.SoftAP.curPacket[12], (packetLen - 4)); - *(u32*)&wifiMac.SoftAP.curPacket[12 + packetLen - 4] = crc32; + memcpy(&SoftAP.curPacket[12 + 4], FW_Mac, 6); // Let's prepare to send - wifiMac.SoftAP.curPacketSize = WIFI_alignedLen(totalLen); - wifiMac.SoftAP.curPacketPos = 0; - wifiMac.SoftAP.curPacketSending = TRUE; + SoftAP.curPacketSize = totalLen; + SoftAP.curPacketPos = 0; + SoftAP.curPacketSending = TRUE; + + SoftAP.status = APStatus_Associated; + WIFI_LOG(1, "SoftAP connected!\n"); } break; + + case 0xA: // Disassociation + SoftAP.status = APStatus_Authenticated; + break; + + case 0xC: // Deauthentication + SoftAP.status = APStatus_Disconnected; + WIFI_LOG(1, "SoftAP disconnected\n"); + break; + + default: + WIFI_LOG(2, "SoftAP: unknown management frame type %04X\n", (frameCtl >> 4) & 0xF); + break; } } break; case 0x2: // Data frame { - // We convert the packet into an Ethernet packet + // If it has a LLC/SLIP header, send it over the Ethernet + if (((*(u16*)&packet[24]) == 0xAAAA) && ((*(u16*)&packet[26]) == 0x0003) && ((*(u16*)&packet[28]) == 0x0000)) + { + if (SoftAP.status != APStatus_Associated) + return; - u32 eflen = (len - 4 - 30 + 14); - u8 *ethernetframe = new u8[eflen]; + u32 epacketLen = ((len - 30 - 4) + 14); + u8 epacket[2048]; - printf("----- SENDING DATA FRAME: len=%i, ethertype=%04X -----\n", - len, *(u16*)&packet[30]); + printf("----- SENDING ETHERNET PACKET: len=%i, ethertype=%04X -----\n", + len, *(u16*)&packet[30]); - // Destination address - ethernetframe[0] = packet[16]; - ethernetframe[1] = packet[17]; - ethernetframe[2] = packet[18]; - ethernetframe[3] = packet[19]; - ethernetframe[4] = packet[20]; - ethernetframe[5] = packet[21]; + memcpy(&epacket[0], &packet[16], 6); + memcpy(&epacket[6], &packet[10], 6); + *(u16*)&epacket[12] = *(u16*)&packet[30]; + memcpy(&epacket[14], &packet[32], epacketLen - 14); - // Source address - ethernetframe[6] = packet[10]; - ethernetframe[7] = packet[11]; - ethernetframe[8] = packet[12]; - ethernetframe[9] = packet[13]; - ethernetframe[10] = packet[14]; - ethernetframe[11] = packet[15]; - - // EtherType - ethernetframe[12] = packet[30]; - ethernetframe[13] = packet[31]; - - // Frame body - memcpy((ethernetframe + 14), (packet + 32), (len - 30 - 4)); - - // Checksum - // TODO ? - - if(wifi_bridge != NULL) - driver->PCAP_sendpacket(wifi_bridge, ethernetframe, eflen); - - delete ethernetframe; + if(wifi_bridge != NULL) + driver->PCAP_sendpacket(wifi_bridge, epacket, epacketLen); + } + else + { + WIFI_LOG(1, "SoftAP: received non-Ethernet data frame. wtf?\n"); + } } break; } @@ -2114,23 +2113,19 @@ INLINE void SoftAP_SendBeacon() u32 totalLen = (packetLen + 12); // Make the RX header - WIFI_MakeRXHeader(wifiMac.SoftAP.curPacket, 0x0011, 20, packetLen, 0, 0); + WIFI_MakeRXHeader(SoftAP.curPacket, 0x0011, 20, packetLen, 0, 0); // Copy the beacon template - memcpy(&wifiMac.SoftAP.curPacket[12], SoftAP_Beacon, packetLen); + memcpy(&SoftAP.curPacket[12], SoftAP_Beacon, packetLen); // Add the timestamp - u64 timestamp = wifiMac.SoftAP.usecCounter; // FIXME: is it correct? - *(u64*)&wifiMac.SoftAP.curPacket[12 + 24] = timestamp; - - // And the CRC32 (FCS) - u32 crc32 = WIFI_calcCRC32(&wifiMac.SoftAP.curPacket[12], (packetLen - 4)); - *(u32*)&wifiMac.SoftAP.curPacket[12 + packetLen - 4] = crc32; + u64 timestamp = SoftAP.usecCounter; + *(u64*)&SoftAP.curPacket[12 + 24] = timestamp; // Let's prepare to send - wifiMac.SoftAP.curPacketSize = WIFI_alignedLen(totalLen); - wifiMac.SoftAP.curPacketPos = 0; - wifiMac.SoftAP.curPacketSending = TRUE; + SoftAP.curPacketSize = totalLen; + SoftAP.curPacketPos = 0; + SoftAP.curPacketSending = TRUE; } void SoftAP_RXHandler(u_char* user, const struct pcap_pkthdr* h, const u_char* _data) @@ -2152,7 +2147,7 @@ void SoftAP_RXHandler(u_char* user, const struct pcap_pkthdr* h, const u_char* _ // The packet was for us. Let's process it then. WIFI_triggerIRQ(WIFI_IRQ_RXSTART); - int wpacketLen = WIFI_alignedLen(26 + 6 + 2 + (h->len-14)); + int wpacketLen = WIFI_alignedLen(26 + 6 + (h->len-14)); u8 wpacket[2048]; //printf("RECEIVED DATA FRAME: len=%i/%i, src=%02X:%02X:%02X:%02X:%02X:%02X, dst=%02X:%02X:%02X:%02X:%02X:%02X, ethertype=%04X\n", @@ -2164,9 +2159,9 @@ void SoftAP_RXHandler(u_char* user, const struct pcap_pkthdr* h, const u_char* _ *(u16*)&wpacket[12+0] = 0x0208; *(u16*)&wpacket[12+2] = 0x0000; memcpy(&wpacket[12+4], &data[0], 6); - memcpy(&wpacket[12+10], &SoftAP_MACAddr[0], 6); + memcpy(&wpacket[12+10], SoftAP_MACAddr, 6); memcpy(&wpacket[12+16], &data[6], 6); - *(u16*)&wpacket[12+22] = 0x0000; + *(u16*)&wpacket[12+22] = 0x0000; // Sequence control. Todo? *(u16*)&wpacket[12+24] = 0xAAAA; *(u16*)&wpacket[12+26] = 0x0003; *(u16*)&wpacket[12+28] = 0x0000; @@ -2181,22 +2176,23 @@ void SoftAP_RXHandler(u_char* user, const struct pcap_pkthdr* h, const u_char* _ } // Done! - wifiMac.RXHWWriteCursorReg = ((wifiMac.RXHWWriteCursor + 1) & (~1)); + wifiMac.RXWriteCursor = ((wifiMac.RXWriteCursor + 1) & (~1)); + WIFI_IOREG(REG_WIFI_RXHWWRITECSR) = wifiMac.RXWriteCursor; wifiMac.RXNum++; WIFI_triggerIRQ(WIFI_IRQ_RXEND); } void SoftAP_usTrigger() { - wifiMac.SoftAP.usecCounter++; + SoftAP.usecCounter++; - if(!wifiMac.SoftAP.curPacketSending) + if(!SoftAP.curPacketSending) { //if(wifiMac.ioMem[0xD0 >> 1] & 0x0400) { //zero sez: every 1/10 second? does it have to be precise? this is so costly.. - //if((wifiMac.SoftAP.usecCounter % (100 * 1024)) == 0) - if((wifiMac.SoftAP.usecCounter & 131071) == 0) + // Okay for 128 ms then + if((SoftAP.usecCounter & 131071) == 0) { //printf("send beacon, store to %04X (readcsr=%04X), size=%x\n", // wifiMac.RXHWWriteCursor<<1, wifiMac.RXReadCursor<<1, sizeof(SoftAP_Beacon)+12); @@ -2208,19 +2204,16 @@ void SoftAP_usTrigger() /* Given a connection of 2 megabits per second, */ /* we take ~4 microseconds to transfer a byte, */ /* ie ~8 microseconds to transfer a word. */ - if((wifiMac.SoftAP.curPacketSending) && !(wifiMac.SoftAP.usecCounter & 7)) + if((SoftAP.curPacketSending) && !(SoftAP.usecCounter & 7)) { - if(wifiMac.SoftAP.curPacketPos >= 0) + if(SoftAP.curPacketPos >= 0) { - if(wifiMac.SoftAP.curPacketPos == 0) + if(SoftAP.curPacketPos == 0) { WIFI_triggerIRQ(WIFI_IRQ_RXSTART); wifiMac.rfStatus = 0x0009; wifiMac.rfPins = 0x0004; - - // printf("wifi: softap: started sending packet at %04X\n", wifiMac.RXHWWriteCursor << 1); - // wifiMac.SoftAP.curPacketPos += 2; } else { @@ -2228,29 +2221,32 @@ void SoftAP_usTrigger() wifiMac.rfPins = 0x0084; } - u16 word = (wifiMac.SoftAP.curPacket[wifiMac.SoftAP.curPacketPos] | (wifiMac.SoftAP.curPacket[wifiMac.SoftAP.curPacketPos+1] << 8)); + u16 word = *(u16*)&SoftAP.curPacket[SoftAP.curPacketPos]; WIFI_RXPutWord(word); } - wifiMac.SoftAP.curPacketPos += 2; - if(wifiMac.SoftAP.curPacketPos >= wifiMac.SoftAP.curPacketSize) + SoftAP.curPacketPos += 2; + if(SoftAP.curPacketPos >= SoftAP.curPacketSize) { - wifiMac.SoftAP.curPacketSize = 0; - wifiMac.SoftAP.curPacketPos = 0; - wifiMac.SoftAP.curPacketSending = FALSE; + //printf("SoftAP: packet finished sending, size=%i, startaddr=%04X", SoftAP.curPacketSize, WIFI_IOREG(REG_WIFI_RXHWWRITECSR)); + SoftAP.curPacketSize = 0; + SoftAP.curPacketPos = 0; + SoftAP.curPacketSending = FALSE; - wifiMac.RXHWWriteCursorReg = ((wifiMac.RXHWWriteCursor + 1) & (~1)); + wifiMac.RXWriteCursor = ((wifiMac.RXWriteCursor + 1) & (~1)); + WIFI_IOREG(REG_WIFI_RXHWWRITECSR) = wifiMac.RXWriteCursor; + //printf(", end=%04X\n", wifiMac.RXWriteCursor); WIFI_triggerIRQ(WIFI_IRQ_RXEND); - //printf("wifi: softap: finished sending packet. end at %04X (aligned=%04X)\n", wifiMac.RXHWWriteCursor << 1, wifiMac.RXHWWriteCursorReg << 1); + SoftAP.seqNum += 0x10; } } // EXTREMELY EXPERIMENTAL packet receiving code // Can now receive 64 packets per millisecond. Completely arbitrary limit. Todo: tweak if needed. // But due to using non-blocking mode, this shouldn't be as slow as it used to be. - if ((wifiMac.SoftAP.usecCounter & 1023) == 0) + if ((SoftAP.usecCounter & 1023) == 0) if(wifi_bridge != NULL) driver->PCAP_dispatch(wifi_bridge, 64, SoftAP_RXHandler, NULL); } diff --git a/desmume/src/wifi.h b/desmume/src/wifi.h index c0acada86..d535f01f4 100644 --- a/desmume/src/wifi.h +++ b/desmume/src/wifi.h @@ -32,10 +32,12 @@ #endif + #define REG_WIFI_ID 0x000 #define REG_WIFI_MODE 0x004 #define REG_WIFI_WEP 0x006 #define REG_WIFI_TXSTATCNT 0x008 +#define REG_WIFI_0A 0x00A #define REG_WIFI_IF 0x010 #define REG_WIFI_IE 0x012 #define REG_WIFI_MAC0 0x018 @@ -47,13 +49,18 @@ #define REG_WIFI_AID_LOW 0x028 #define REG_WIFI_AID_HIGH 0x02A #define REG_WIFI_RETRYLIMIT 0x02C +// 02E - internal #define REG_WIFI_RXCNT 0x030 #define REG_WIFI_WEPCNT 0x032 +// 034 - internal + #define REG_WIFI_POWER_US 0x036 #define REG_WIFI_POWER_TX 0x038 #define REG_WIFI_POWERSTATE 0x03C -#define REG_WIFI_FORCEPS 0x040 +#define REG_WIFI_POWERFORCE 0x040 #define REG_WIFI_RANDOM 0x044 +#define REG_WIFI_POWER_UNK 0x048 + #define REG_WIFI_RXRANGEBEGIN 0x050 #define REG_WIFI_RXRANGEEND 0x052 #define REG_WIFI_RXHWWRITECSR 0x054 @@ -65,26 +72,43 @@ #define REG_WIFI_CIRCBUFRD_END 0x062 #define REG_WIFI_CIRCBUFRD_SKIP 0x064 #define REG_WIFI_CIRCBUFWADR 0x068 +#define REG_WIFI_TXBUFCOUNT 0x06C #define REG_WIFI_CIRCBUFWRITE 0x070 #define REG_WIFI_CIRCBUFWR_END 0x074 #define REG_WIFI_CIRCBUFWR_SKIP 0x076 + +// 078 - internal #define REG_WIFI_TXLOCBEACON 0x080 #define REG_WIFI_LISTENCOUNT 0x088 #define REG_WIFI_BEACONPERIOD 0x08C #define REG_WIFI_LISTENINT 0x08E #define REG_WIFI_TXLOCEXTRA 0x090 +// 094 - ? +// 098 - ? +// 09C - internal #define REG_WIFI_TXLOC1 0x0A0 #define REG_WIFI_TXLOC2 0x0A4 #define REG_WIFI_TXLOC3 0x0A8 -#define REG_WIFI_TXOPT 0x0AC -#define REG_WIFI_TXCNT 0x0AE +#define REG_WIFI_TXREQ_RESET 0x0AC +#define REG_WIFI_TXREQ_SET 0x0AE #define REG_WIFI_TXREQ_READ 0x0B0 #define REG_WIFI_TXRESET 0x0B4 #define REG_WIFI_TXBUSY 0x0B6 #define REG_WIFI_TXSTAT 0x0B8 +// 0BA - internal +#define REG_WIFI_PREAMBLE 0x0BC +// 0C0 - ? +// 0C4 - ? +// 0C8 - internal #define REG_WIFI_RXFILTER 0x0D0 +// 0D4 - config +// 0D8 - config +// 0DA - config +#define REG_WIFI_RXFILTER2 0x0E0 + #define REG_WIFI_USCOUNTERCNT 0x0E8 #define REG_WIFI_USCOMPARECNT 0x0EA +// 0EC - config #define REG_WIFI_EXTRACOUNTCNT 0x0EE #define REG_WIFI_USCOMPARE0 0x0F0 #define REG_WIFI_USCOMPARE1 0x0F2 @@ -94,26 +118,60 @@ #define REG_WIFI_USCOUNTER1 0x0FA #define REG_WIFI_USCOUNTER2 0x0FC #define REG_WIFI_USCOUNTER3 0x0FE +// 100 - internal +// 102 - internal +// 104 - internal +// 106 - internal +#define REG_WIFI_CONTENTFREE 0x10C +#define REG_WIFI_PREBEACONCOUNT 0x110 #define REG_WIFI_EXTRACOUNT 0x118 #define REG_WIFI_BEACONCOUNT1 0x11C + +// 120 to 132 - config/internal ports #define REG_WIFI_BEACONCOUNT2 0x134 -#define REG_WIFI_BBSIOCNT 0x158 -#define REG_WIFI_BBSIOWRITE 0x15A -#define REG_WIFI_BBSIOREAD 0x15C -#define REG_WIFI_BBSIOBUSY 0x15E -#define REG_WIFI_RFIODATA2 0x17C -#define REG_WIFI_RFIODATA1 0x17E -#define REG_WIFI_RFIOBSY 0x180 -#define REG_WIFI_RFIOCNT 0x184 +// 140 to 154 - config ports + +#define REG_WIFI_BBCNT 0x158 +#define REG_WIFI_BBWRITE 0x15A +#define REG_WIFI_BBREAD 0x15C +#define REG_WIFI_BBBUSY 0x15E +#define REG_WIFI_BBMODE 0x160 +#define REG_WIFI_BBPOWER 0x168 + +// 16A to 178 - internal + +#define REG_WIFI_RFDATA2 0x17C +#define REG_WIFI_RFDATA1 0x17E +#define REG_WIFI_RFBUSY 0x180 +#define REG_WIFI_RFCNT 0x184 + +// 190 - internal +// 194 - ? +// 198 - internal #define REG_WIFI_RFPINS 0x19C +// 1A0 - internal +// 1A2 - internal +#define REG_WIFI_MAYBE_RATE 0x1A4 + +#define REG_WIFI_RXSTAT_INC_IF 0x1A8 +#define REG_WIFI_RXSTAT_INC_IE 0x1AA +#define REG_WIFI_RXSTAT_OVF_IF 0x1AC +#define REG_WIFI_RXSTAT_OVF_IE 0x1AE +#define REG_WIFI_TXERR_COUNT 0x1C0 +#define REG_WIFI_RX_COUNT 0x1C4 + +// 1D0 to 1DE - related to multiplayer. argh. + #define REG_WIFI_RFSTATUS 0x214 #define REG_WIFI_IF_SET 0x21C #define REG_WIFI_TXSEQNO 0x210 #define REG_WIFI_POWERACK 0x2D0 + +#define WIFI_IOREG(reg) wifiMac.IOPorts[(reg) >> 1] + /* WIFI misc constants */ #define WIFI_CHIPID 0x1440 /* emulates "old" wifi chip, new is 0xC340 */ -#define REG_PWRCNT 0x04000304 /* Referenced as RF_ in dswifi: rffilter_t */ /* based on the documentation for the RF2958 chip of RF Micro Devices */ @@ -131,10 +189,10 @@ typedef struct rffilter_t /* 3*/ unsigned HYBERNATE:1; /* 4*/ unsigned :10; /*14*/ unsigned REF_SEL:2; -/*16*/ unsigned :2 ; - } bits ; - u32 val ; - } CFG1 ; +/*16*/ unsigned :2; + } bits; + u32 val; + } CFG1; union IFPLL1 { struct @@ -150,18 +208,18 @@ typedef struct rffilter_t /*15*/ unsigned VTC_EN1:1; /*16*/ unsigned KV_EN1:1; /*17*/ unsigned PLL_EN1:1; - } bits ; - u32 val ; - } IFPLL1 ; + } bits; + u32 val; + } IFPLL1; union IFPLL2 { struct { /* 0*/ unsigned IF_N:16; /*16*/ unsigned :2; - } bits ; - u32 val ; - } IFPLL2 ; + } bits; + u32 val; + } IFPLL2; union IFPLL3 { struct @@ -170,9 +228,9 @@ typedef struct rffilter_t /* 4*/ unsigned CT_DEF1:4; /* 8*/ unsigned DN1:9; /*17*/ unsigned :1; - } bits ; - u32 val ; - } IFPLL3 ; + } bits; + u32 val; + } IFPLL3; union RFPLL1 { struct @@ -188,26 +246,26 @@ typedef struct rffilter_t /*15*/ unsigned VTC_EN:1; /*16*/ unsigned KV_EN:1; /*17*/ unsigned PLL_EN:1; - } bits ; - u32 val ; - } RFPLL1 ; + } bits; + u32 val; + } RFPLL1; union RFPLL2 { struct { /* 0*/ unsigned NUM2:6; /* 6*/ unsigned N2:12; - } bits ; - u32 val ; - } RFPLL2 ; + } bits; + u32 val; + } RFPLL2; union RFPLL3 { struct { /* 0*/ unsigned NUM2:18; - } bits ; - u32 val ; - } RFPLL3 ; + } bits; + u32 val; + } RFPLL3; union RFPLL4 { struct @@ -216,9 +274,9 @@ typedef struct rffilter_t /* 4*/ unsigned CT_DEF:4; /* 8*/ unsigned DN:9; /*17*/ unsigned :1; - } bits ; - u32 val ; - } RFPLL4 ; + } bits; + u32 val; + } RFPLL4; union CAL1 { struct @@ -227,9 +285,9 @@ typedef struct rffilter_t /* 3*/ unsigned M_CT_VALUE:5; /* 8*/ unsigned TLOCK:5; /*13*/ unsigned TVCO:5; - } bits ; - u32 val ; - } CAL1 ; + } bits; + u32 val; + } CAL1; union TXRX1 { struct @@ -243,9 +301,9 @@ typedef struct rffilter_t /*10*/ unsigned TXVGC:5; /*15*/ unsigned PCONTROL:2; /*17*/ unsigned RXDCFBBYPS:1; - } bits ; - u32 val ; - } TXRX1 ; + } bits; + u32 val; + } TXRX1; union PCNT1 { struct @@ -254,9 +312,9 @@ typedef struct rffilter_t /* 3*/ unsigned PC_OFFSET:6; /* 9*/ unsigned P_DESIRED:6; /*15*/ unsigned MID_BIAS:3; - } bits ; - u32 val ; - } PCNT1 ; + } bits; + u32 val; + } PCNT1; union PCNT2 { struct @@ -264,9 +322,9 @@ typedef struct rffilter_t /* 0*/ unsigned MIN_POWER:6; /* 6*/ unsigned MID_POWER:6; /*12*/ unsigned MAX_POWER:6; - } bits ; - u32 val ; - } PCNT2 ; + } bits; + u32 val; + } PCNT2; union VCOT1 { struct @@ -274,17 +332,17 @@ typedef struct rffilter_t /* 0*/ unsigned :16; /*16*/ unsigned AUX1:1; /*17*/ unsigned AUX:1; - } bits ; - u32 val ; - } VCOT1 ; -} rffilter_t ; + } bits; + u32 val; + } VCOT1; +} rffilter_t; /* baseband chip refered as BB_, dataformat is unknown yet */ /* it has at least 105 bytes of functional data */ typedef struct { - u8 data[105] ; -} bb_t ; + u8 data[105]; +} bb_t; /* communication interface between RF,BB and the mac */ typedef union @@ -294,33 +352,33 @@ typedef union /* 5*/ unsigned :2; /* 7*/ unsigned readOperation:1; /* 8*/ unsigned :8; - } bits ; - u16 val ; -} rfIOCnt_t ; + } bits; + u16 val; +} rfIOCnt_t; typedef union { struct { /* 0*/ unsigned busy:1; /* 1*/ unsigned :15; - } bits ; - u16 val ; -} rfIOStat_t ; + } bits; + u16 val; +} rfIOStat_t; typedef union { struct { -/* 0*/ unsigned content:18 ; +/* 0*/ unsigned content:18; /*18*/ unsigned address:5; /*23*/ unsigned :9; - } bits ; + } bits; struct { -/* 0*/ unsigned low:16 ; -/*16*/ unsigned high:16 ; - } val16 ; - u16 array16[2] ; - u32 val ; -} rfIOData_t ; +/* 0*/ unsigned low:16; +/*16*/ unsigned high:16; + } val16; + u16 array16[2]; + u32 val; +} rfIOData_t; typedef union { @@ -330,9 +388,9 @@ typedef union /*12*/ unsigned mode:2; /*14*/ unsigned enable:1; /*15*/ unsigned :1; - } bits ; - u16 val ; -} bbIOCnt_t ; + } bits; + u16 val; +} bbIOCnt_t; #define WIFI_IRQ_RXEND 0 #define WIFI_IRQ_TXEND 1 @@ -350,28 +408,6 @@ typedef union #define WIFI_IRQ_TIMEBEACON 14 #define WIFI_IRQ_TIMEPREBEACON 15 -/* definition of the irq bitfields for wifi irq's (cascaded at arm7 irq #24) */ -typedef union -{ - struct - { -/* 0*/ unsigned recv_complete:1; -/* 1*/ unsigned send_complete:1; -/* 2*/ unsigned recv_countup:1; -/* 3*/ unsigned send_error:1; -/* 4*/ unsigned stat_countup:1; -/* 5*/ unsigned stat_ackup:1; -/* 6*/ unsigned recv_start:1; -/* 7*/ unsigned send_start:1; -/* 8*/ unsigned :3; -/*11*/ unsigned rf_wakeup:1; -/*12*/ unsigned :2; -/*14*/ unsigned time_beacon:1; -/*15*/ unsigned time_prebeacon:1; - } bits ; - u16 val ; -} wifiirq_t ; - typedef struct { bool enabled; @@ -381,6 +417,13 @@ typedef struct u16 remtime; } Wifi_TXLoc; +enum EAPStatus +{ + APStatus_Disconnected = 0, + APStatus_Authenticated, + APStatus_Associated, +}; + /* wifimac_t: the buildin mac (arm7 addressrange: 0x04800000-0x04FFFFFF )*/ /* http://www.akkit.org/info/dswifi.htm#WifiIOMap */ @@ -395,20 +438,20 @@ typedef struct u16 rfPins; /* wifi interrupt handling */ - wifiirq_t IE ; - wifiirq_t IF ; + u16 IE; + u16 IF; /* modes */ - u16 macMode ; - u16 wepMode ; - BOOL WEP_enable ; + u16 macMode; + u16 wepMode; + BOOL WEP_enable; /* sending */ u16 TXStatCnt; u16 TXPower; - u16 TXSlot[3] ; - u16 TXCnt ; - u16 TXOpt ; + u16 TXSlot[3]; + u16 TXCnt; + u16 TXOpt; u16 TXStat; u16 BeaconAddr; BOOL BeaconEnable; @@ -425,8 +468,8 @@ typedef struct u16 ExtraSlotRemBytes; /* receiving */ - u16 RXCnt ; - u16 RXCheckCounter ; + u16 RXCnt; + u16 RXCheckCounter; u8 RXNum; u16 RXTXAddr; @@ -435,26 +478,26 @@ typedef struct union { //TODO - is this endian safe? don't think so - u16 words[3] ; - u8 bytes[6] ; - } mac ; + u16 words[3]; + u8 bytes[6]; + } mac; union { - u16 words[3] ; - u8 bytes[6] ; - } bss ; - u16 aid ; - u16 pid ; /* player ID or aid_low */ - u16 retryLimit ; + u16 words[3]; + u8 bytes[6]; + } bss; + u16 aid; + u16 pid; /* player ID or aid_low */ + u16 retryLimit; /* timing */ - BOOL crystalEnabled ; - u64 usec ; - BOOL usecEnable ; - u64 ucmp ; - BOOL ucmpEnable ; - u32 eCount ; - BOOL eCountEnable ; + BOOL crystalEnabled; + u64 usec; + BOOL usecEnable; + u64 ucmp; + BOOL ucmpEnable; + u32 eCount; + BOOL eCountEnable; u16 BeaconInterval; u16 BeaconCount1; u16 BeaconCount2; @@ -462,64 +505,63 @@ typedef struct u16 ListenCount; /* subchips */ - rffilter_t RF ; - bb_t BB ; + rffilter_t RF; + bb_t BB; /* subchip communications */ - rfIOCnt_t rfIOCnt ; - rfIOStat_t rfIOStatus ; - rfIOData_t rfIOData ; - bbIOCnt_t bbIOCnt ; - - u8 bbDataToWrite; + rfIOCnt_t rfIOCnt; + rfIOStat_t rfIOStatus; + rfIOData_t rfIOData; + bbIOCnt_t bbIOCnt; /* buffers */ - u16 circularBuffer[0x1000] ; - u16 RXRangeBegin ; - u16 RXRangeEnd ; - u16 RXHWWriteCursor ; - u16 RXHWWriteCursorReg; - u16 RXHWWriteCursorLatched; - u16 RXReadCursor ; - u16 RXUnits ; - u16 RXBufCount ; - u16 CircBufReadAddress ; - u16 CircBufWriteAddress ; - u16 CircBufRdEnd ; - u16 CircBufRdSkip ; - u16 CircBufWrEnd ; - u16 CircBufWrSkip ; + u16 RAM[0x1000]; + u16 RXRangeBegin; + u16 RXRangeEnd; + u16 RXWriteCursor; + u16 RXReadCursor; + u16 RXUnits; + u16 RXBufCount; + u16 CircBufReadAddress; + u16 CircBufWriteAddress; + u16 CircBufRdEnd; + u16 CircBufRdSkip; + u16 CircBufWrEnd; + u16 CircBufWrSkip; /* tx packets */ s32 curPacketSize[3]; s32 curPacketPos[3]; BOOL curPacketSending[3]; - /* i/o mem */ - u16 ioMem[0x800]; + /* I/O ports */ + u16 IOPorts[0x800]; /* others */ - u16 randomSeed ; + u16 randomSeed; - struct _Adhoc - { - u64 usecCounter; +} wifimac_t; - } Adhoc; - /* SoftAP */ - struct _SoftAP - { - u64 usecCounter; +typedef struct +{ + u64 usecCounter; - u8 curPacket[4096]; - s32 curPacketSize; - s32 curPacketPos; - BOOL curPacketSending; +} Adhoc_t; - } SoftAP; +typedef struct +{ + u64 usecCounter; -} wifimac_t ; + u8 curPacket[4096]; + s32 curPacketSize; + s32 curPacketPos; + BOOL curPacketSending; + + EAPStatus status; + u16 seqNum; + +} SoftAP_t; // desmume host communication #ifdef EXPERIMENTAL_WIFI_COMM @@ -528,27 +570,29 @@ extern pcap_t *wifi_bridge; #endif extern wifimac_t wifiMac; +extern Adhoc_t Adhoc; +extern SoftAP_t SoftAP; bool WIFI_Init(); void WIFI_DeInit(); void WIFI_Reset(); /* subchip communication IO functions */ -void WIFI_setRF_CNT(u16 val) ; -void WIFI_setRF_DATA(u16 val, u8 part) ; -u16 WIFI_getRF_DATA(u8 part) ; -u16 WIFI_getRF_STATUS() ; +void WIFI_setRF_CNT(u16 val); +void WIFI_setRF_DATA(u16 val, u8 part); +u16 WIFI_getRF_DATA(u8 part); +u16 WIFI_getRF_STATUS(); -void WIFI_setBB_CNT(u16 val) ; -u8 WIFI_getBB_DATA() ; -void WIFI_setBB_DATA(u8 val) ; +void WIFI_setBB_CNT(u16 val); +u8 WIFI_getBB_DATA(); +void WIFI_setBB_DATA(u8 val); /* wifimac io */ -void WIFI_write16(u32 address, u16 val) ; -u16 WIFI_read16(u32 address) ; +void WIFI_write16(u32 address, u16 val); +u16 WIFI_read16(u32 address); /* wifimac timing */ -void WIFI_usTrigger() ; +void WIFI_usTrigger(); /* DS WFC profile data documented here : */ @@ -580,11 +624,11 @@ typedef struct _FW_WFCProfile /* wifi data to be stored in firmware, when no firmware image was loaded */ extern u8 FW_Mac[6]; -extern const u8 FW_WIFIInit[32] ; -extern const u8 FW_BBInit[105] ; -extern const u8 FW_RFInit[36] ; -extern const u8 FW_RFChannel[6*14] ; -extern const u8 FW_BBChannel[14] ; +extern const u8 FW_WIFIInit[32]; +extern const u8 FW_BBInit[105]; +extern const u8 FW_RFInit[36]; +extern const u8 FW_RFChannel[6*14]; +extern const u8 FW_BBChannel[14]; extern FW_WFCProfile FW_WFCProfile1; extern FW_WFCProfile FW_WFCProfile2; extern FW_WFCProfile FW_WFCProfile3;