-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathns_lib_spi.c
More file actions
390 lines (305 loc) · 8.65 KB
/
Copy pathns_lib_spi.c
File metadata and controls
390 lines (305 loc) · 8.65 KB
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
/**
* @file ns_lib_spi.c
* @brief Emulated SPI flash reads for factory / pairing regions (NS-LIB-HID).
*
* Copyright (c) 2026 Mitchell Cairns, Hand Held Legend, LLC.
*
* Licensed under the Creative Commons Attribution-NonCommercial 4.0 International License
* (CC BY-NC 4.0). Non-commercial use with attribution; commercial licensing from Hand Held Legend.
* Full terms: https://creativecommons.org/licenses/by-nc/4.0/legalcode — see LICENSING.md in this folder
*
* SPDX-License-Identifier: CC-BY-NC-4.0
*
* TRADEMARK AND AFFILIATION DISCLAIMER:
* This library is not affiliated, associated, authorized, endorsed by, or in any way officially
* connected with Nintendo Co., Ltd., or any of its subsidiaries or its affiliates. Nintendo and
* related marks are trademarks of their respective owners.
*/
#include "ns_lib_spi.h"
#include "ns_lib_analog.h"
#include "ns_lib_types.h"
#include "ns_lib_config.h"
#include <string.h>
uint8_t _ns_spi_getaddressdata(uint8_t offset_address, uint8_t address)
{
/*
* SPI memory map notes (mirrors legacy switch_spi.c docs):
* 0x00xx: patch ROM region (unused here)
* 0x10xx: failsafe region (unused here)
* 0x20xx..0x40xx: pairing block
* 0x50xx: shipment info
* 0x60xx: factory/config/calibration/color/sensor params
* 0x80xx: user calibration region
*/
switch (offset_address)
{
case 0x00:
/* Patch ROM not needed */
return 0x00;
case 0x10:
/* Failsafe mechanism not needed */
return 0x00;
case 0x20 ... 0x40:
/* Pairing data block */
switch (address)
{
case 0x26:
case 0x00:
/* Pairing magic byte: 0x95 means pairing data present */
return 0x95;
case 0x27:
case 0x01:
/* Pairing payload size */
return 0x22;
case 0x28:
case 0x29:
case 0x02:
case 0x03:
/* Pairing checksum (not implemented) */
return 0x00;
case 0x2A ... 0x2F:
/* Host BT address (big-endian mirror area) */
return 0;
case 0x04 ... 0x09:
/* Host BT address mirror */
return 0;
case 0x30 ... 0x3F:
/* Bluetooth LTK area (placeholder) */
return 0x00;
case 0x0A ... 0x19:
/* Bluetooth LTK mirror */
return 0x00;
case 0x4A:
case 0x24:
/* Host capability: 0x68 Switch, 0x08 PC */
return 0x68;
case 0x4B:
case 0x25:
return 0;
default:
return 0x00;
}
case 0x50:
/* Shipment flag: always 0 */
return 0x00;
case 0x60:
{
uint8_t *analog_cal = ns_analog_calibration_data();
/* Factory configuration and calibration */
uint8_t factory_id_hi, factory_id_lo, factory_color_byte, factory_snes_region_byte;
ns_config_get_devtype_bytes(&factory_id_hi, &factory_id_lo, &factory_color_byte,
&factory_snes_region_byte);
ns_device_config_s cfg;
ns_config_get(&cfg);
switch (address)
{
case 0x00 ... 0x0F:
/* Stage 1 (0x6000): serial number block, 0xFF means disabled */
return cfg.device_serial[address];
// ProCon 0x03 0x02
// N64 0x0C 0x11
// SNES 0x0B 0x02
// Famicom 0x07 0x02
// NES 0x09 0x02
// Genesis 0x0D 0x02
case 0x12:
return factory_id_hi;
case 0x13:
return factory_id_lo;
// Returns a bool indicating if color is set.
// 0x02 if the controller is SNES
case 0x1B:
return factory_color_byte;
case 0x20:
return 35;
case 0x21:
return 0;
case 0x22:
return 185;
case 0x23:
return 255;
case 0x24:
return 26;
case 0x25:
return 1;
case 0x26:
return 0;
case 0x27:
return 64;
case 0x28:
return 0;
case 0x29:
return 64;
case 0x2A:
return 0;
case 0x2B:
return 64;
case 0x2C:
return 1;
case 0x2D:
return 0;
case 0x2E:
return 1;
case 0x2F:
return 0;
case 0x30:
return 1;
case 0x31:
return 0;
case 0x32:
return 0x3B;
case 0x33:
return 0x34;
case 0x34:
return 0x3B;
case 0x35:
return 0x34;
case 0x36:
return 0x3B;
case 0x37:
return 0x34;
case 0x3D ... 0x45:
/* Left stick factory calibration */
return analog_cal[address - 0x3D];
case 0x46 ... 0x4E:
/* Right stick factory calibration */
return analog_cal[address - 0x3D];
case 0x4F:
return 0xFF;
case 0x50:
/* Stage 2 (0x6050): body color (RGB) */
return cfg.colors.body.r;
case 0x51:
return cfg.colors.body.g;
case 0x52:
return cfg.colors.body.b;
case 0x53:
/* Stage 2: button color (RGB) */
return cfg.colors.buttons.r;
case 0x54:
return cfg.colors.buttons.g;
case 0x55:
return cfg.colors.buttons.b;
case 0x56:
/* Stage 2: left grip color (RGB) */
return cfg.colors.l_grip.r;
case 0x57:
return cfg.colors.l_grip.g;
case 0x58:
return cfg.colors.l_grip.b;
case 0x59:
/* Stage 2: right grip color (RGB) */
return cfg.colors.r_grip.r;
case 0x5A:
return cfg.colors.r_grip.g;
case 0x5B:
return cfg.colors.r_grip.b;
// This is used for SNES controller color options
// 0x00 - North America (Super Nintendo Purple)
// 0x01 - Japan (Super Famicom)
// 0x02 - Europe (Super Nintendo)
case 0x5C:
return factory_snes_region_byte;
// Stage 3 configuration 0x6080, length 24
// Covers factory sensor and stick device params
// Start accelerometer offsets
case 0x80:
return 80;
case 0x81:
return 253;
case 0x82:
return 0;
case 0x83:
return 0;
case 0x84:
return 198;
case 0x85:
return 15;
case 0x98:
case 0x86:
/* Stage 3/4: stick device parameters (mirrored) */
return 15;
case 0x99:
case 0x87:
return 48;
case 0x9A:
case 0x88:
return 97;
case 0x9B:
case 0x89:
return 174;
case 0x9C:
case 0x8A:
return 144;
case 0x9D:
case 0x8B:
return 217;
case 0x9E:
case 0x8C:
return 212;
case 0x9F:
case 0x8D:
return 20;
case 0xA0:
case 0x8E:
return 84;
case 0xA1:
case 0x8F:
return 65;
case 0xA2:
case 0x90:
return 21;
case 0xA3:
case 0x91:
return 84;
case 0xA4:
case 0x92:
return 199;
case 0xA5:
case 0x93:
return 121;
case 0xA6:
case 0x94:
return 156;
case 0xA7:
case 0x95:
return 51;
case 0xA8:
case 0x96:
return 54;
case 0xA9:
case 0x97:
return 99;
default:
return 0x00;
}
}
case 0x80:
/* User calibration block (unset => 0xFF) */
switch (address)
{
case 0x10 ... 0x1A:
/* User left-stick calibration magic */
return 0xFF;
case 0x1B ... 0x25:
/* User right-stick calibration magic */
return 0xFF;
case 0x26 ... 0x3F:
/* User gyro calibration area */
return 0xFF;
default:
return 0xFF;
}
default:
return 0xFF;
}
}
void ns_spi_get(uint8_t offset_address, uint8_t address, uint8_t length, uint8_t *out)
{
uint8_t read_info[5] = {address, offset_address, 0x00, 0x00, length};
memcpy(&out[NS_SPI_STARTREAD_IDX], read_info, 5);
for (int i = 0; i < length; i++)
{
out[NS_SPI_READ_OUTPUT_IDX + i] = _ns_spi_getaddressdata(offset_address, (uint8_t)(address + i));
}
}