-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwifi.cpp
282 lines (235 loc) · 7.88 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
/*
Copyright 2020 Hydr8gon
This file is part of NooDS.
NooDS 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 3 of the License, or
(at your option) any later version.
NooDS 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 NooDS. If not, see <https://www.gnu.org/licenses/>.
*/
#include "wifi.h"
#include "core.h"
Wifi::Wifi(Core *core): core(core)
{
// Set some default BB register values
bbRegisters[0x00] = 0x6D;
bbRegisters[0x5D] = 0x01;
bbRegisters[0x64] = 0xFF;
}
void Wifi::sendInterrupt(int bit)
{
// Trigger a WiFi interrupt if W_IF & W_IE changes from zero
if (!(wIe & wIrf) && (wIe & BIT(bit)))
core->interpreter[1].sendInterrupt(24);
wIrf |= BIT(bit);
}
void Wifi::writeWModeWep(uint16_t mask, uint16_t value)
{
// Write to the W_MODE_WEP register
wModeWep = (wModeWep & ~mask) | (value & mask);
}
void Wifi::writeWIrf(uint16_t mask, uint16_t value)
{
// Write to the W_IF register
// Setting a bit actually clears it to acknowledge an interrupt
wIrf &= ~(value & mask);
}
void Wifi::writeWIe(uint16_t mask, uint16_t value)
{
// Trigger a WiFi interrupt if W_IF & W_IE changes from zero
if (!(wIe & wIrf) && (value & mask & wIrf))
core->interpreter[1].sendInterrupt(24);
// Write to the W_IE register
mask &= 0xFBFF;
wIe = (wIe & ~mask) | (value & mask);
}
void Wifi::writeWMacaddr(int index, uint16_t mask, uint16_t value)
{
// Write to one of the W_MACADDR registers
wMacaddr[index] = (wMacaddr[index] & ~mask) | (value & mask);
}
void Wifi::writeWBssid(int index, uint16_t mask, uint16_t value)
{
// Write to one of the W_BSSID registers
wBssid[index] = (wBssid[index] & ~mask) | (value & mask);
}
void Wifi::writeWAidFull(uint16_t mask, uint16_t value)
{
// Write to the W_AID_FULL register
mask &= 0x07FF;
wAidFull = (wAidFull & ~mask) | (value & mask);
}
void Wifi::writeWPowerstate(uint16_t mask, uint16_t value)
{
// Write to the W_POWERSTATE register
mask &= 0x0003;
wPowerstate = (wPowerstate & ~mask) | (value & mask);
// Set the power state to enabled if requested
if (wPowerstate & BIT(1))
wPowerstate &= ~BIT(9);
}
void Wifi::writeWPowerforce(uint16_t mask, uint16_t value)
{
// Write to the W_POWERFORCE register
mask &= 0x8001;
wPowerforce = (wPowerforce & ~mask) | (value & mask);
// Force set the power state if requested
if (wPowerforce & BIT(15))
wPowerstate = (wPowerstate & ~BIT(9)) | ((wPowerforce & BIT(0)) << 9);
}
void Wifi::writeWRxbufBegin(uint16_t mask, uint16_t value)
{
// Write to the W_RXBUF_BEGIN register
wRxbufBegin = (wRxbufBegin & ~mask) | (value & mask);
}
void Wifi::writeWRxbufEnd(uint16_t mask, uint16_t value)
{
// Write to the W_RXBUF_END register
wRxbufEnd = (wRxbufEnd & ~mask) | (value & mask);
}
void Wifi::writeWRxbufWrAddr(uint16_t mask, uint16_t value)
{
// Write to the W_RXBUF_WR_ADDR register
mask &= 0x0FFF;
wRxbufWrAddr = (wRxbufWrAddr & ~mask) | (value & mask);
}
void Wifi::writeWRxbufRdAddr(uint16_t mask, uint16_t value)
{
// Write to the W_RXBUF_RD_ADDR register
mask &= 0x1FFE;
wRxbufRdAddr = (wRxbufRdAddr & ~mask) | (value & mask);
}
void Wifi::writeWRxbufReadcsr(uint16_t mask, uint16_t value)
{
// Write to the W_RXBUF_RDCSR register
mask &= 0x0FFF;
wRxbufReadcsr = (wRxbufReadcsr & ~mask) | (value & mask);
}
void Wifi::writeWRxbufGap(uint16_t mask, uint16_t value)
{
// Write to the W_RXBUF_GAP register
mask &= 0x1FFE;
wRxbufGap = (wRxbufGap & ~mask) | (value & mask);
}
void Wifi::writeWRxbufGapdisp(uint16_t mask, uint16_t value)
{
// Write to the W_RXBUF_GAPDISP register
mask &= 0x0FFF;
wRxbufGapdisp = (wRxbufGapdisp & ~mask) | (value & mask);
}
void Wifi::writeWRxbufCount(uint16_t mask, uint16_t value)
{
// Write to the W_RXBUF_COUNT register
mask &= 0x0FFF;
wRxbufCount = (wRxbufCount & ~mask) | (value & mask);
}
void Wifi::writeWTxbufWrAddr(uint16_t mask, uint16_t value)
{
// Write to the W_TXBUF_WR_ADDR register
mask &= 0x1FFE;
wTxbufWrAddr = (wTxbufWrAddr & ~mask) | (value & mask);
}
void Wifi::writeWTxbufCount(uint16_t mask, uint16_t value)
{
// Write to the W_TXBUF_COUNT register
mask &= 0x0FFF;
wTxbufCount = (wTxbufCount & ~mask) | (value & mask);
}
void Wifi::writeWTxbufWrData(uint16_t mask, uint16_t value)
{
// Write a value to WiFi RAM
core->memory.write<uint16_t>(1, 0x4804000 + wTxbufWrAddr, value & mask);
// Increment the write address
wTxbufWrAddr += 2;
if (wTxbufWrAddr == wTxbufGap)
wTxbufWrAddr += wTxbufGapdisp << 1;
wTxbufWrAddr %= 0x2000;
// Decrement the write counter and trigger an interrupt at the end
if (wTxbufCount > 0 && --wTxbufCount == 0)
sendInterrupt(8);
}
void Wifi::writeWTxbufGap(uint16_t mask, uint16_t value)
{
// Write to the W_TXBUF_GAP register
mask &= 0x1FFE;
wTxbufGap = (wTxbufGap & ~mask) | (value & mask);
}
void Wifi::writeWTxbufGapdisp(uint16_t mask, uint16_t value)
{
// Write to the W_TXBUF_GAPDISP register
mask &= 0x0FFF;
wTxbufGapdisp = (wTxbufGapdisp & ~mask) | (value & mask);
}
void Wifi::writeWConfig(int index, uint16_t mask, uint16_t value)
{
const uint16_t masks[] =
{
0x81FF, 0xFFFF, 0xFFFF, 0xFFFF, 0x0FFF,
0x8FFF, 0xFFFF, 0xFFFF, 0x00FF, 0x00FF,
0x00FF, 0x00FF, 0xFFFF, 0xFF3F, 0x7A7F
};
// Write to one of the W_CONFIG registers
mask &= masks[index];
wConfig[index] = (wConfig[index] & ~mask) | (value & mask);
}
void Wifi::writeWBeaconcount2(uint16_t mask, uint16_t value)
{
// Write to the W_BEACONCOUNT2 register
wBeaconcount2 = (wBeaconcount2 & ~mask) | (value & mask);
}
void Wifi::writeWBbCnt(uint16_t mask, uint16_t value)
{
int index = value & 0x00FF;
// Perform a BB register transfer
switch ((value & 0xF000) >> 12)
{
case 5: // Write
{
if ((index >= 0x01 && index <= 0x0C) || (index >= 0x13 && index <= 0x15) || (index >= 0x1B && index <= 0x26) ||
(index >= 0x28 && index <= 0x4C) || (index >= 0x4E && index <= 0x5C) || (index >= 0x62 && index <= 0x63) ||
(index == 0x65) || (index >= 0x67 && index <= 0x68)) // Writable registers
bbRegisters[index] = wBbWrite;
break;
}
case 6: // Read
{
wBbRead = bbRegisters[index];
break;
}
}
}
void Wifi::writeWBbWrite(uint16_t mask, uint16_t value)
{
// Write to the W_BB_WRITE register
wBbWrite = (wBbWrite & ~mask) | (value & mask);
}
void Wifi::writeWIrfSet(uint16_t mask, uint16_t value)
{
// Trigger a WiFi interrupt if W_IF & W_IE changes from zero
if (!(wIe & wIrf) && (wIe & value & mask))
core->interpreter[1].sendInterrupt(24);
// Set bits in the W_IF register
mask &= 0xFBFF;
wIrf |= (value & mask);
}
uint16_t Wifi::readWRxbufRdData()
{
// Read a value from WiFi RAM
uint16_t value = core->memory.read<uint16_t>(1, 0x4804000 + wRxbufRdAddr);
// Increment the read address
wRxbufRdAddr += 2;
if (wRxbufRdAddr == wRxbufGap)
wRxbufRdAddr += wRxbufGapdisp << 1;
if ((wRxbufBegin & 0x1FFE) != (wRxbufEnd & 0x1FFE))
wRxbufRdAddr = (wRxbufBegin & 0x1FFE) + (wRxbufRdAddr - (wRxbufBegin & 0x1FFE)) % ((wRxbufEnd & 0x1FFE) - (wRxbufBegin & 0x1FFE));
wRxbufRdAddr %= 0x2000;
// Decrement the read counter and trigger an interrupt at the end
if (wRxbufCount > 0 && --wRxbufCount == 0)
sendInterrupt(9);
return value;
}