/
wifi.cpp
5179 lines (4167 loc) · 138 KB
/
wifi.cpp
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (C) 2007 Tim Seidel
Copyright (C) 2008-2018 DeSmuME team
This file is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This file is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with the this software. If not, see <http://www.gnu.org/licenses/>.
*/
#include <assert.h>
#include "wifi.h"
#include "armcpu.h"
#include "NDSSystem.h"
#include "debug.h"
#include "utils/bits.h"
#include "utils/task.h"
#include <driver.h>
#include <registers.h>
#include <rthreads/rthreads.h>
#ifdef HOST_WINDOWS
#include <winsock2.h>
#include <ws2tcpip.h>
#define socket_t SOCKET
#define sockaddr_t SOCKADDR
#ifndef WXPORT
#include "windriver.h"
#endif
#define PCAP_DEVICE_NAME description
#else
#include <stddef.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#define socket_t int
#define sockaddr_t struct sockaddr
#define closesocket close
#define PCAP_DEVICE_NAME name
#endif
#ifndef INVALID_SOCKET
#define INVALID_SOCKET (socket_t)-1
#endif
#define BASEPORT 7000
#define PACKET_SIZE 65535
// Some platforms need HAVE_REMOTE to work with libpcap, but
// Apple platforms are not among them.
#ifndef __APPLE__
#define HAVE_REMOTE
#endif
#ifdef HOST_WINDOWS
#define WPCAP
#endif
#include <pcap.h>
typedef struct pcap pcap_t;
//sometimes this isnt defined
#ifndef PCAP_OPENFLAG_PROMISCUOUS
#define PCAP_OPENFLAG_PROMISCUOUS 1
#endif
// PCAP_ERRBUF_SIZE should 256 bytes according to POSIX libpcap and winpcap.
// Define it if it isn't available.
#ifndef PCAP_ERRBUF_SIZE
#define PCAP_ERRBUF_SIZE 256
#endif
static const u8 BBDefaultData[105] = {
0x6D, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x00 - 0x0F
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x10 - 0x1F
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x20 - 0x2F
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x30 - 0x3F
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x40 - 0x4F
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, // 0x50 - 0x5F
0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00, 0x00 // 0x60 - 0x68
};
static const bool BBIsDataWritable[105] = {
false, true, true, true, true, true, true, true, true, true, true, true, true, false, false, false, // 0x00 - 0x0F
false, false, false, true, true, true, false, false, false, false, false, true, true, true, true, true, // 0x10 - 0x1F
true, true, true, true, true, true, true, false, true, true, true, true, true, true, true, true, // 0x20 - 0x2F
true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, // 0x30 - 0x3F
true, true, true, true, true, true, true, true, true, true, true, true, true, false, true, true, // 0x40 - 0x4F
true, true, true, true, true, true, true, true, true, true, true, true, true, false, false, false, // 0x50 - 0x5F
false, false, true, true, false, true, false, true, true // 0x60 - 0x68
};
static const u16 RFPinsLUT[10] = { 0x04, 0x84, 0, 0x46, 0, 0x84, 0x87, 0, 0x46, 0x04 };
static const WifiLLCSNAPHeader DefaultSNAPHeader = { 0xAA, 0xAA, 0x03, {0x00, 0x00, 0x00}, 0x0800 };
LegacyWifiSFormat legacyWifiSF;
DummyPCapInterface dummyPCapInterface;
WifiHandler* wifiHandler = NULL;
/*******************************************************************************
WIFI TODO
- emulate transmission delays for Beacon and Extra transfers
- emulate delays when receiving as well (may need some queuing system)
- take transfer rate and preamble into account
- figure out RFSTATUS and RFPINS
*******************************************************************************/
/*******************************************************************************
Firmware info needed for if no firmware image is available
see: http://www.akkit.org/info/dswifi.htm#WifiInit
written in bytes, to avoid endianess issues
*******************************************************************************/
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 };
const u8 FW_BBInit[105] = { 0x6D, 0x9E, 0x40, 0x05,
0x1B, 0x6C, 0x48, 0x80,
0x38, 0x00, 0x35, 0x07,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0xb0, 0x00, 0x04, 0x01,
0xd8, 0xff, 0xff, 0xc7,
0xbb, 0x01, 0xb6, 0x7f,
0x5a, 0x01, 0x3f, 0x01,
0x3f, 0x36, 0x36, 0x00,
0x78, 0x28, 0x55, 0x08,
0x28, 0x16, 0x00, 0x01,
0x0e, 0x20, 0x02, 0x98,
0x98, 0x1f, 0x0a, 0x08,
0x04, 0x01, 0x00, 0x00,
0x00, 0xff, 0xff, 0xfe,
0xfe, 0xfe, 0xfe, 0xfc,
0xfa, 0xfa, 0xf8, 0xf8,
0xf6, 0xa5, 0x12, 0x14,
0x12, 0x41, 0x23, 0x03,
0x04, 0x70, 0x35, 0x0E,
0x16, 0x16, 0x00, 0x00,
0x06, 0x01, 0xff, 0xfe,
0xff, 0xff, 0x00, 0x0e,
0x13, 0x00, 0x00, 0x28,
0x1c
};
const u8 FW_RFInit[36] = { 0x07, 0xC0, 0x00,
0x03, 0x9C, 0x12,
0x28, 0x17, 0x14,
0xba, 0xe8, 0x1a,
0x6f, 0x45, 0x1d,
0xfa, 0xff, 0x23,
0x30, 0x1d, 0x24,
0x01, 0x00, 0x28,
0x00, 0x00, 0x2c,
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 */
0x46, 0x17, 0x19,
0x45, 0x17, 0x14, /* Channel 3 */
0xd1, 0x45, 0x1b,
0x54, 0x17, 0x14, /* Channel 4 */
0x5d, 0x74, 0x19,
0x62, 0x17, 0x14, /* Channel 5 */
0xe9, 0xa2, 0x1b,
0x71, 0x17, 0x14, /* Channel 6 */
0x74, 0xd1, 0x19,
0x80, 0x17, 0x14, /* Channel 7 */
0x00, 0x00, 0x18,
0x8e, 0x17, 0x14, /* Channel 8 */
0x8c, 0x2e, 0x1a,
0x9d, 0x17, 0x14, /* Channel 9 */
0x17, 0x5d, 0x18,
0xab, 0x17, 0x14, /* Channel 10 */
0xa3, 0x8b, 0x1a,
0xba, 0x17, 0x14, /* Channel 11 */
0x2f, 0xba, 0x18,
0xc8, 0x17, 0x14, /* Channel 12 */
0xba, 0xe8, 0x1a,
0xd7, 0x17, 0x14, /* Channel 13 */
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 */
};
FW_WFCProfile FW_WFCProfile1 = { "SoftAP",
"",
"",
"",
"",
"",
{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0},
0,
"",
0,
0,
0,
{0, 0, 0, 0, 0, 0, 0},
0,
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0}
};
FW_WFCProfile FW_WFCProfile2 = { "",
"",
"",
"",
"",
"",
{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0},
0,
"",
0,
0,
0xFF,
{0, 0, 0, 0, 0, 0, 0},
0,
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0}
};
FW_WFCProfile FW_WFCProfile3 = { "",
"",
"",
"",
"",
"",
{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0},
0,
"",
0,
0,
0xFF,
{0, 0, 0, 0, 0, 0, 0},
0,
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0}
};
/*******************************************************************************
Logging
*******************************************************************************/
// 0: disable logging
// 1: lowest logging, shows most important messages such as errors
// 2: low logging, shows things such as warnings
// 3: medium logging, for debugging, shows lots of stuff
// 4: high logging, for debugging, shows almost everything, may slow down
// 5: highest logging, for debugging, shows everything, may slow down a lot
#define WIFI_LOGGING_LEVEL 1
#define WIFI_LOG_USE_LOGC 0
#if (WIFI_LOGGING_LEVEL >= 1)
#if WIFI_LOG_USE_LOGC
#define WIFI_LOG(level, ...) if(level <= WIFI_LOGGING_LEVEL) LOGC(8, "WIFI: " __VA_ARGS__);
#else
#define WIFI_LOG(level, ...) if(level <= WIFI_LOGGING_LEVEL) printf("WIFI: " __VA_ARGS__);
#endif
#else
#define WIFI_LOG(level, ...) {}
#endif
// For debugging purposes, the results of libpcap can be written to a file.
// Note that enabling this setting can negatively affect emulation performance.
#define WIFI_SAVE_PCAP_TO_FILE 0
/*******************************************************************************
Helpers
*******************************************************************************/
// Fast MAC compares
INLINE bool WIFI_compareMAC(const u8* a, const u8* b)
{
return ((*(u32*)&a[0]) == (*(u32*)&b[0])) && ((*(u16*)&a[4]) == (*(u16*)&b[4]));
}
INLINE bool WIFI_isBroadcastMAC(const u8* a)
{
return ((*(u32*)&a[0]) == 0xFFFFFFFF) && ((*(u16*)&a[4]) == 0xFFFF);
}
INLINE bool WIFI_IsLLCSNAPHeader(const u8* snapHeader)
{
return ((*(u16*)&snapHeader[0] == *(u16*)&DefaultSNAPHeader.dsap) && (*(u32*)&snapHeader[2] == *(u32*)&DefaultSNAPHeader.control));
}
/*******************************************************************************
CRC32 (http://www.codeproject.com/KB/recipes/crc32_large.aspx)
*******************************************************************************/
u32 WIFI_CRC32Table[256];
static u32 reflect(u32 ref, char ch)
{
u32 value = 0;
for(int i = 1; i < (ch + 1); i++)
{
if(ref & 1)
value |= 1 << (ch - i);
ref >>= 1;
}
return value;
}
static u32 WIFI_calcCRC32(u8* data, int len)
{
u32 crc = 0xFFFFFFFF;
while(len--)
crc = (crc >> 8) ^ WIFI_CRC32Table[(crc & 0xFF) ^ *data++];
return (crc ^ 0xFFFFFFFF);
}
static void WIFI_initCRC32Table()
{
static bool initialized = false;
if(initialized) return;
initialized = true;
u32 polynomial = 0x04C11DB7;
for(int i = 0; i < 0x100; i++)
{
WIFI_CRC32Table[i] = reflect(i, 8) << 24;
for(int j = 0; j < 8; j++)
WIFI_CRC32Table[i] = (WIFI_CRC32Table[i] << 1) ^ (WIFI_CRC32Table[i] & (1 << 31) ? polynomial : 0);
WIFI_CRC32Table[i] = reflect(WIFI_CRC32Table[i], 32);
}
}
/*******************************************************************************
RF-Chip
*******************************************************************************/
static void WIFI_resetRF(RF2958_IOREG_MAP& rf)
{
/* reinitialize RF chip with the default values refer RF2958 docs */
/* CFG1 */
rf.CFG1.IF_VGA_REG_EN = 1;
rf.CFG1.IF_VCO_REG_EN = 1;
rf.CFG1.RF_VCO_REG_EN = 1;
rf.CFG1.HYBERNATE = 0;
rf.CFG1.REF_SEL = 0;
/* IFPLL1 */
rf.IFPLL1.DAC = 3;
rf.IFPLL1.P1 = 0;
rf.IFPLL1.LD_EN1 = 0;
rf.IFPLL1.AUTOCAL_EN1 = 0;
rf.IFPLL1.PDP1 = 1;
rf.IFPLL1.CPL1 = 0;
rf.IFPLL1.LPF1 = 0;
rf.IFPLL1.VTC_EN1 = 1;
rf.IFPLL1.KV_EN1 = 0;
rf.IFPLL1.PLL_EN1 = 0;
/* IFPLL2 */
rf.IFPLL2.IF_N = 0x22;
/* IFPLL3 */
rf.IFPLL3.KV_DEF1 = 8;
rf.IFPLL3.CT_DEF1 = 7;
rf.IFPLL3.DN1 = 0x1FF;
/* RFPLL1 */
rf.RFPLL1.DAC = 3;
rf.RFPLL1.P = 0;
rf.RFPLL1.LD_EN = 0;
rf.RFPLL1.AUTOCAL_EN = 0;
rf.RFPLL1.PDP = 1;
rf.RFPLL1.CPL = 0;
rf.RFPLL1.LPF = 0;
rf.RFPLL1.VTC_EN = 1;
rf.RFPLL1.KV_EN = 0;
rf.RFPLL1.PLL_EN = 0;
/* RFPLL2 */
rf.RFPLL2.NUM2 = 0;
rf.RFPLL2.N2 = 0x5E;
/* RFPLL3 */
rf.RFPLL3.NUM2 = 0;
/* RFPLL4 */
rf.RFPLL4.KV_DEF = 8;
rf.RFPLL4.CT_DEF = 7;
rf.RFPLL4.DN = 0x145;
/* CAL1 */
rf.CAL1.LD_WINDOW = 2;
rf.CAL1.M_CT_VALUE = 8;
rf.CAL1.TLOCK = 7;
rf.CAL1.TVCO = 0x0F;
/* TXRX1 */
rf.TXRX1.TXBYPASS = 0;
rf.TXRX1.INTBIASEN = 0;
rf.TXRX1.TXENMODE = 0;
rf.TXRX1.TXDIFFMODE = 0;
rf.TXRX1.TXLPFBW = 2;
rf.TXRX1.RXLPFBW = 2;
rf.TXRX1.TXVGC = 0;
rf.TXRX1.PCONTROL = 0;
rf.TXRX1.RXDCFBBYPS = 0;
/* PCNT1 */
rf.PCNT1.TX_DELAY = 0;
rf.PCNT1.PC_OFFSET = 0;
rf.PCNT1.P_DESIRED = 0;
rf.PCNT1.MID_BIAS = 0;
/* PCNT2 */
rf.PCNT2.MIN_POWER = 0;
rf.PCNT2.MID_POWER = 0;
rf.PCNT2.MAX_POWER = 0;
/* VCOT1 */
rf.VCOT1.AUX1 = 0;
rf.VCOT1.AUX = 0;
}
/*******************************************************************************
wifimac IO: a lot of the wifi regs are action registers, that are mirrored
without action, so the default IO via MMU.c does not seem to
be very suitable
all registers are 16 bit
*******************************************************************************/
static void WIFI_TXStart(const WifiTXLocIndex txSlotIndex, IOREG_W_TXBUF_LOCATION& txLocation);
static void WIFI_SetIRQ(const WifiIRQ irq)
{
WIFI_IOREG_MAP& io = wifiHandler->GetWifiData().io;
u16 oldFlags = io.IF.value & io.IE.value;
io.IF.value |= ((1 << irq) & 0xFBFF);
u16 newFlags = io.IF.value & io.IE.value;
if((oldFlags == 0) && (newFlags != 0))
{
NDS_makeIrq(ARMCPU_ARM7, IRQ_BIT_ARM7_WIFI); // cascade it via arm7 wifi irq
}
}
static void WIFI_triggerIRQ(const WifiIRQ irq)
{
WIFI_IOREG_MAP& io = wifiHandler->GetWifiData().io;
switch(irq)
{
case WifiIRQ00_RXComplete:
case WifiIRQ01_TXComplete:
case WifiIRQ02_RXEventIncrement:
case WifiIRQ03_TXErrorIncrement:
case WifiIRQ04_RXEventOverflow:
case WifiIRQ05_TXErrorOverflow:
case WifiIRQ08_TXCountExpired:
case WifiIRQ09_RXCountExpired:
case WifiIRQ11_RFWakeup:
WIFI_SetIRQ(irq);
break;
case WifiIRQ06_RXStart:
io.RF_STATUS.RFStatus = WifiRFStatus6_RXEnabled;
io.RF_PINS.value = RFPinsLUT[WifiRFStatus6_RXEnabled];
WIFI_SetIRQ(irq);
break;
case WifiIRQ07_TXStart:
io.TX_SEQNO.Number++;
WIFI_SetIRQ(irq);
break;
case WifiIRQ10_UNUSED:
// Do nothing.
break;
case WifiIRQ12_UNKNOWN:
WIFI_LOG(2, "IRQ 12 triggered.\n");
io.TX_SEQNO.Number++;
WIFI_SetIRQ(irq);
break;
case WifiIRQ13_TimeslotPostBeacon:
{
WIFI_SetIRQ(irq);
if(io.POWER_TX.AutoSleep != 0)
{
io.RF_STATUS.RFStatus = 0x9;
io.RF_PINS.CarrierSense = 0;
io.RF_PINS.TXMain = 1;
io.RF_PINS.UNKNOWN1 = 1;
io.RF_PINS.TX_On = 1;
io.RF_PINS.RX_On = 0;
io.INTERNAL_034 = 0x0002;
io.TXREQ_READ.value &= 0x0010;
io.POWERSTATE.WillPowerOn = 0;
io.POWERSTATE.IsPowerOff = 1;
}
break;
}
case WifiIRQ14_TimeslotBeacon:
{
if(io.US_COMPARECNT.ForceIRQ14 == 0)
{
io.BEACONCOUNT1 = io.BEACONINT.Interval;
}
if(io.US_COMPARECNT.EnableCompare != 0)
{
WIFI_SetIRQ(irq);
io.BEACONCOUNT2 = 0xFFFF;
io.TXREQ_READ.Loc1 = 0;
io.TXREQ_READ.Loc2 = 0;
io.TXREQ_READ.Loc3 = 0;
if(io.TXBUF_BEACON.TransferRequest != 0)
{
io.TXBUSY.Beacon = 1;
io.RF_PINS.value = RFPinsLUT[WifiRFStatus3_TXEnabled];
WIFI_TXStart(WifiTXLocIndex_BEACON, io.TXBUF_BEACON);
}
if(io.LISTENCOUNT.Count == 0)
{
io.LISTENCOUNT.Count = io.LISTENINT.Interval;
}
io.LISTENCOUNT.Count--;
}
break;
}
case WifiIRQ15_TimeslotPreBeacon:
{
WIFI_SetIRQ(irq);
if(io.POWER_TX.AutoWakeup != 0)
{
io.RF_STATUS.RFStatus = 0x1;
io.RF_PINS.RX_On = 1;
}
break;
}
}
}
TXPacketHeader WIFI_GenerateTXHeader(bool isTXRate20, size_t txLength)
{
TXPacketHeader txHeader;
txHeader.txStatus = 1;
txHeader.mpSlaves = 0;
txHeader.seqNumberControl = 0;
txHeader.UNKNOWN1 = 0;
txHeader.UNKNOWN2 = 0;
txHeader.txRate = (isTXRate20) ? 20 : 10;
txHeader.UNKNOWN3 = 0;
txHeader.length = txLength;
return txHeader;
}
RXPacketHeader WIFI_GenerateRXHeader(const u8* packetIEEE80211HeaderPtr, const u16 timeStamp, const bool isTXRate20, const u16 emuPacketSize)
{
const WIFI_IOREG_MAP& io = wifiHandler->GetWifiData().io;
RXPacketHeader rxHeader;
rxHeader.rxFlags.value = 0;
const WifiFrameControl& fc = (WifiFrameControl&)packetIEEE80211HeaderPtr[0];
switch((WifiFrameType)fc.Type)
{
case WifiFrameType_Management:
{
const WifiMgmtFrameHeader& IEEE80211Header = (WifiMgmtFrameHeader&)packetIEEE80211HeaderPtr[0];
rxHeader.rxFlags.MatchingBSSID = (WIFI_compareMAC(IEEE80211Header.BSSID, io.BSSID)) ? 1 : 0;
if(fc.Subtype == WifiFrameManagementSubtype_Beacon)
{
rxHeader.rxFlags.FrameType = 0x1;
}
else
{
rxHeader.rxFlags.FrameType = 0x0;
}
break;
}
case WifiFrameType_Control:
{
rxHeader.rxFlags.FrameType = 0x5;
switch((WifiFrameControlSubtype)fc.Subtype)
{
case WifiFrameControlSubtype_PSPoll:
{
const WifiCtlFrameHeaderPSPoll& IEEE80211Header = (WifiCtlFrameHeaderPSPoll&)packetIEEE80211HeaderPtr[0];
rxHeader.rxFlags.MatchingBSSID = (WIFI_compareMAC(IEEE80211Header.BSSID, io.BSSID)) ? 1 : 0;
break;
}
case WifiFrameControlSubtype_RTS:
case WifiFrameControlSubtype_CTS:
case WifiFrameControlSubtype_ACK:
rxHeader.rxFlags.MatchingBSSID = 1;
break;
case WifiFrameControlSubtype_End:
{
const WifiCtlFrameHeaderEnd& IEEE80211Header = (WifiCtlFrameHeaderEnd&)packetIEEE80211HeaderPtr[0];
rxHeader.rxFlags.MatchingBSSID = (WIFI_compareMAC(IEEE80211Header.BSSID, io.BSSID)) ? 1 : 0;
break;
}
case WifiFrameControlSubtype_EndAck:
{
const WifiCtlFrameHeaderEndAck& IEEE80211Header = (WifiCtlFrameHeaderEndAck&)packetIEEE80211HeaderPtr[0];
rxHeader.rxFlags.MatchingBSSID = (WIFI_compareMAC(IEEE80211Header.BSSID, io.BSSID)) ? 1 : 0;
break;
}
default:
break;
}
break;
}
case WifiFrameType_Data:
{
// By default, set the FrameType flags to 8 for a data frame.
// This can be overridden based on other frame control states.
rxHeader.rxFlags.FrameType = 0x8;
switch((WifiFCFromToState)fc.FromToState)
{
case WifiFCFromToState_STA2STA:
{
const WifiDataFrameHeaderSTA2STA& IEEE80211Header = (WifiDataFrameHeaderSTA2STA&)packetIEEE80211HeaderPtr[0];
rxHeader.rxFlags.MatchingBSSID = (WIFI_compareMAC(IEEE80211Header.BSSID, io.BSSID)) ? 1 : 0;
break;
}
case WifiFCFromToState_STA2DS:
{
const WifiDataFrameHeaderSTA2DS& IEEE80211Header = (WifiDataFrameHeaderSTA2DS&)packetIEEE80211HeaderPtr[0];
rxHeader.rxFlags.MatchingBSSID = (WIFI_compareMAC(IEEE80211Header.BSSID, io.BSSID)) ? 1 : 0;
if(fc.Subtype == WifiFrameDataSubtype_DataAck)
{
rxHeader.rxFlags.FrameType = 0xE;
}
else if(fc.Subtype == WifiFrameDataSubtype_Ack)
{
rxHeader.rxFlags.FrameType = 0xF;
}
break;
}
case WifiFCFromToState_DS2STA:
{
const WifiDataFrameHeaderDS2STA& IEEE80211Header = (WifiDataFrameHeaderDS2STA&)packetIEEE80211HeaderPtr[0];
rxHeader.rxFlags.MatchingBSSID = (WIFI_compareMAC(IEEE80211Header.BSSID, io.BSSID)) ? 1 : 0;
if(fc.Subtype == WifiFrameDataSubtype_DataPoll)
{
rxHeader.rxFlags.FrameType = 0xC;
}
else if(fc.Subtype == WifiFrameDataSubtype_DataAck)
{
rxHeader.rxFlags.FrameType = 0xD;
}
break;
}
case WifiFCFromToState_DS2DS:
{
// We shouldn't be receiving any DS-to-DS packets, but include this case anyways
// to account for all possible code paths.
rxHeader.rxFlags.MatchingBSSID = 0;
break;
}
}
break;
}
default:
break;
}
rxHeader.rxFlags.UNKNOWN1 = 1;
rxHeader.rxFlags.MoreFragments = fc.MoreFragments;
rxHeader.UNKNOWN1 = 0x0040;
rxHeader.timeStamp = timeStamp;
rxHeader.txRate = (isTXRate20) ? 20 : 10;
rxHeader.length = emuPacketSize;
rxHeader.rssiMax = 255;
rxHeader.rssiMin = 240;
return rxHeader;
}
static void WIFI_TXStart(const WifiTXLocIndex txSlotIndex, IOREG_W_TXBUF_LOCATION& txLocation)
{
WifiData& wifi = wifiHandler->GetWifiData();
WIFI_IOREG_MAP& io = wifi.io;
WIFI_LOG(3, "TX slot %i trying to send a packet: TXCnt = %04X, TXBufLoc = %04X\n",
(int)txSlotIndex, io.TXREQ_READ.value, txLocation.value);
if(txLocation.TransferRequest != 0) /* is slot enabled? */
{
//printf("send packet at %08X, lr=%08X\n", NDS_ARM7.instruct_adr, NDS_ARM7.R[14]);
u32 byteAddress = txLocation.HalfwordAddress << 1;
// is there even enough space for the header (6 hwords) in the tx buffer?
if(byteAddress > (0x2000 - sizeof(TXPacketHeader) - sizeof(WifiFrameControl)))
{
WIFI_LOG(1, "TX slot %i trying to send a packet overflowing from the TX buffer (address %04X). Attempt ignored.\n",
(int)txSlotIndex, (int)byteAddress);
return;
}
TXPacketHeader& txHeader = (TXPacketHeader&)wifi.RAM[byteAddress];
const WifiFrameControl& fc = (WifiFrameControl&)wifi.RAM[byteAddress + sizeof(TXPacketHeader)];
//printf("---------- SENDING A PACKET ON SLOT %i, FrameCtl = %04X ----------\n",
// slot, wifi.RAM[byteAddress + sizeof(TXPacketHeader)]);
// 12 byte header TX Header: http://www.akkit.org/info/dswifi.htm#FmtTx
// The minimum possible frame length is 14 bytes.
// - The TX frame length is header + body + FCS.
// - Possible header sizes range from 10 - 30 bytes.
// - It is possible for the frame body to be 0 bytes for certain frame types/subtypes.
// - The frame check sequence (FCS) is always 4 bytes.
if(txHeader.length < 14)
{
WIFI_LOG(1, "TX slot %i trying to send a packet with length field set to zero. Attempt ignored.\n",
(int)txSlotIndex);
return;
}
// Align packet length
txHeader.length = ((txHeader.length + 3) & 0xFFFC);
// Set sequence number if required
if((txSlotIndex == WifiTXLocIndex_BEACON) || (txLocation.IEEESeqCtrl == 0))
{
if(fc.Type == WifiFrameType_Management)
{
WifiMgmtFrameHeader& mgmtHeader = (WifiMgmtFrameHeader&)wifi.RAM[byteAddress + sizeof(TXPacketHeader)];
mgmtHeader.seqCtl.SequenceNumber = io.TX_SEQNO.Number;
mgmtHeader.seqCtl.FragmentNumber = 0;
}
else if(fc.Type == WifiFrameType_Data)
{
// The frame header may not necessarily be a STA-to-STA header, but all of the data frame headers
// place the sequence number value at the same location in memory, so we can just use the
// STA-to-STA header to represent all of the data frame headers.
WifiDataFrameHeaderSTA2STA& dataHeader = (WifiDataFrameHeaderSTA2STA&)wifi.RAM[byteAddress + sizeof(TXPacketHeader)];
dataHeader.seqCtl.SequenceNumber = io.TX_SEQNO.Number;
dataHeader.seqCtl.FragmentNumber = 0;
}
}
// Calculate and set FCS
u32 crc32 = WIFI_calcCRC32(&wifi.RAM[byteAddress + sizeof(TXPacketHeader)], txHeader.length - 4);
*(u32*)&wifi.RAM[byteAddress + sizeof(TXPacketHeader) + txHeader.length - 4] = crc32;
WIFI_triggerIRQ(WifiIRQ07_TXStart);
if((txSlotIndex == WifiTXLocIndex_LOC1) || (txSlotIndex == WifiTXLocIndex_LOC2) || (txSlotIndex == WifiTXLocIndex_LOC3))
{
TXPacketInfo& txPacketInfo = wifiHandler->GetPacketInfoAtSlot(txSlotIndex);
txPacketInfo.emuPacketLength = txHeader.length;
txPacketInfo.remainingBytes = txHeader.length + sizeof(TXPacketHeader);
switch(txSlotIndex)
{
case WifiTXLocIndex_LOC1: io.TXBUSY.Loc1 = 1; break;
//case WifiTXLocIndex_CMD: io.TXBUSY.Cmd = 1; break;
case WifiTXLocIndex_LOC2: io.TXBUSY.Loc2 = 1; break;
case WifiTXLocIndex_LOC3: io.TXBUSY.Loc3 = 1; break;
//case WifiTXLocIndex_BEACON: io.TXBUSY.Beacon = 1; break;
default:
break;
}
if(txSlotIndex == WifiTXLocIndex_LOC3)
{
wifi.txCurrentSlot = WifiTXLocIndex_LOC3;
}
else if((txSlotIndex == WifiTXLocIndex_LOC2) && (wifi.txCurrentSlot == WifiTXLocIndex_LOC1))
{
wifi.txCurrentSlot = WifiTXLocIndex_LOC2;
}
io.RXTX_ADDR.HalfwordAddress = byteAddress >> 1;
io.RF_STATUS.RFStatus = 0x03;
io.RF_PINS.CarrierSense = 0;
io.RF_PINS.TXMain = 1;
io.RF_PINS.UNKNOWN1 = 1;
io.RF_PINS.TX_On = 1;
io.RF_PINS.RX_On = 0;
#if 0
WIFI_SoftAP_RecvPacketFromDS(&wifi.RAM[address + sizeof(TXPacketHeader)], txHeader.length);
WIFI_triggerIRQ(WifiIRQ01_TXComplete);
txHeader.txStatus = 0x0001;
txHeader.UNKNOWN3 = 0;
#endif
}
else if(txSlotIndex == WifiTXLocIndex_CMD)
{
wifiHandler->CommSendPacket(txHeader, &wifi.RAM[byteAddress + sizeof(TXPacketHeader)]);
WIFI_triggerIRQ(WifiIRQ12_UNKNOWN);
// If bit 13 is set, then it has priority over bit 14
if(io.TXSTATCNT.UpdateTXStat_0B01 != 0)
{
WIFI_triggerIRQ(WifiIRQ01_TXComplete);
io.TXSTAT.value = 0x0B01;
}
else if(io.TXSTATCNT.UpdateTXStat_0800 != 0)
{
WIFI_triggerIRQ(WifiIRQ01_TXComplete);
io.TXSTAT.value = 0x0800;
}
txLocation.TransferRequest = 0;
txHeader.txStatus = 0x0001;
txHeader.UNKNOWN3 = 0;
}
else if(txSlotIndex == WifiTXLocIndex_BEACON)
{
// Set timestamp
*(u64*)&wifi.RAM[byteAddress + sizeof(TXPacketHeader) + sizeof(WifiMgmtFrameHeader)] = io.US_COUNT;
wifiHandler->CommSendPacket(txHeader, &wifi.RAM[byteAddress + sizeof(TXPacketHeader)]);
if(io.TXSTATCNT.UpdateTXStatBeacon != 0)
{
WIFI_triggerIRQ(WifiIRQ01_TXComplete);
io.TXSTAT.value = 0x0301;
}
txHeader.txStatus = 0x0001;
txHeader.UNKNOWN3 = 0;
}
}
}
void WIFI_write16(u32 address, u16 val)
{
bool action = false;
if(!nds.power2.wifi) return;
WifiData& wifi = wifiHandler->GetWifiData();
WIFI_IOREG_MAP& io = wifi.io;
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);
// 0x4000 - 0x5FFF: wifi RAM
if((page >= 0x4000) && (page < 0x6000))
{
/* access to the circular buffer */
*(u16*)&wifi.RAM[address & 0x1FFE] = val;
return;
}
// 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_MODE: // 0x004
{
IOREG_W_MODE_RST MODE_RST;
MODE_RST.value = val;
io.MODE_RST.UNKNOWN1 = MODE_RST.UNKNOWN1;
io.MODE_RST.UNKNOWN2 = MODE_RST.UNKNOWN2;
io.MODE_RST.UNKNOWN3 = MODE_RST.UNKNOWN3;
if((io.MODE_RST.TXMasterEnable == 0) && (MODE_RST.TXMasterEnable != 0))
{
io.INTERNAL_034 = 0x0002;
io.RF_STATUS.value = 0x0009;
io.RF_PINS.value = 0x0046;
io.INTERNAL_27C = 0x0005;
// According to GBATEK, the following registers might be reset to some unknown values.
// io.INTERNAL_2A2 = ???;
}
if((io.MODE_RST.TXMasterEnable != 0) && (MODE_RST.TXMasterEnable == 0))
{
io.INTERNAL_27C = 0x000A;
}
io.MODE_RST.TXMasterEnable = MODE_RST.TXMasterEnable;
if(MODE_RST.ResetPortSet1 != 0)
{
io.RXBUF_WR_ADDR.value = 0x0000;
io.CMD_TOTALTIME = 0x0000;
io.CMD_REPLYTIME = 0x0000;
io.X_1A4 = 0x0000;
io.INTERNAL_278 = 0x000F;
// According to GBATEK, the following registers might be reset to some unknown values.
//io.TXREQ_SET.value = ???;
//io.INTERNAL_0BA = ???;
//io.INTERNAL_204 = ???;
//io.INTERNAL_25C = ???;
//io.RXTX_ADDR.value = ???;
//io.INTERNAL_274 = ???;
}
if(MODE_RST.ResetPortSet2 != 0)
{
io.MODE_WEP.value = 0x0000;
io.TXSTATCNT.value = 0x0000;
io.X_00A = 0x0000;
io.MACADDR[0] = 0x00;
io.MACADDR[1] = 0x00;
io.MACADDR[2] = 0x00;
io.MACADDR[3] = 0x00;
io.MACADDR[4] = 0x00;
io.MACADDR[5] = 0x00;
io.BSSID[0] = 0x00;
io.BSSID[1] = 0x00;
io.BSSID[2] = 0x00;
io.BSSID[3] = 0x00;
io.BSSID[4] = 0x00;
io.BSSID[5] = 0x00;
io.AID_LOW.value = 0x0000;
io.AID_FULL.value = 0x0000;
io.TX_RETRYLIMIT.value = 0x0707;
io.INTERNAL_02E = 0x0000;
io.RXBUF_BEGIN = 0x4000;
io.RXBUF_END = 0x4800;
io.TXBUF_TIM.value = 0x0000;