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
|
/*
Copyright 2016-2022 melonDS team
This file is part of melonDS.
melonDS 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.
melonDS 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 melonDS. If not, see http://www.gnu.org/licenses/.
*/
#include <stdio.h>
#include <string.h>
#include "DSi.h"
#include "SPI.h"
#include "DSi_SPI_TSC.h"
#include "Platform.h"
using Platform::Log;
using Platform::LogLevel;
namespace DSi_SPI_TSC
{
u32 DataPos;
u8 Index;
u8 Bank;
u8 Data;
u8 Bank3Regs[0x80];
u8 TSCMode;
u16 TouchX, TouchY;
bool Init()
{
return true;
}
void DeInit()
{
}
void Reset()
{
DataPos = 0;
Bank = 0;
Index = 0;
Data = 0;
memset(Bank3Regs, 0, 0x80);
Bank3Regs[0x02] = 0x18;
Bank3Regs[0x03] = 0x87;
Bank3Regs[0x04] = 0x22;
Bank3Regs[0x05] = 0x04;
Bank3Regs[0x06] = 0x20;
Bank3Regs[0x09] = 0x40;
Bank3Regs[0x0E] = 0xAD;
Bank3Regs[0x0F] = 0xA0;
Bank3Regs[0x10] = 0x88;
Bank3Regs[0x11] = 0x81;
TSCMode = 0x01; // DSi mode
}
void DoSavestate(Savestate* file)
{
file->Section("SPTi");
file->Var32(&DataPos);
file->Var8(&Index);
file->Var8(&Bank);
file->Var8(&Data);
file->VarArray(Bank3Regs, 0x80);
file->Var8(&TSCMode);
}
void SetMode(u8 mode)
{
TSCMode = mode;
}
void SetTouchCoords(u16 x, u16 y)
{
if (TSCMode == 0x00)
{
if (y == 0xFFF) NDS::KeyInput |= (1 << (16+6));
else NDS::KeyInput &= ~(1 << (16+6));
return SPI_TSC::SetTouchCoords(x, y);
}
TouchX = x;
TouchY = y;
u8 oldpress = Bank3Regs[0x0E] & 0x01;
if (y == 0xFFF)
{
// released
// TODO: GBAtek says it can also be 1000 or 3000??
TouchX = 0x7000;
TouchY = 0x7000;
Bank3Regs[0x09] = 0x40;
//Bank3Regs[0x09] &= ~0x80;
Bank3Regs[0x0E] |= 0x01;
}
else
{
// pressed
TouchX <<= 4;
TouchY <<= 4;
Bank3Regs[0x09] = 0x80;
//Bank3Regs[0x09] |= 0x80;
Bank3Regs[0x0E] &= ~0x01;
}
if (oldpress ^ (Bank3Regs[0x0E] & 0x01))
{
TouchX |= 0x8000;
TouchY |= 0x8000;
}
}
void MicInputFrame(s16* data, int samples)
{
if (TSCMode == 0x00) return SPI_TSC::MicInputFrame(data, samples);
// otherwise we don't handle mic input
// TODO: handle it where it needs to be
}
u8 Read()
{
if (TSCMode == 0x00) return SPI_TSC::Read();
return Data;
}
void Write(u8 val, u32 hold)
{
if (TSCMode == 0x00) return SPI_TSC::Write(val, hold);
#define READWRITE(var) { if (Index & 0x01) Data = var; else var = val; }
if (DataPos == 0)
{
Index = val;
}
else
{
u8 id = Index >> 1;
if (id == 0)
{
READWRITE(Bank);
}
else if (Bank == 0x03)
{
if (Index & 0x01) Data = Bank3Regs[id];
else
{
if (id == 0x0D || id == 0x0E)
Bank3Regs[id] = (Bank3Regs[id] & 0x03) | (val & 0xFC);
}
}
else if ((Bank == 0xFC) && (Index & 0x01))
{
if (id < 0x0B)
{
// X coordinates
if (id & 0x01) Data = TouchX >> 8;
else Data = TouchX & 0xFF;
TouchX &= 0x7FFF;
}
else if (id < 0x15)
{
// Y coordinates
if (id & 0x01) Data = TouchY >> 8;
else Data = TouchY & 0xFF;
TouchY &= 0x7FFF; // checkme
}
else
{
// whatever (TODO)
Data = 0;
}
}
else if (Bank == 0xFF)
{
if (id == 0x05)
{
// TSC mode register
// 01: normal (DSi) mode
// 00: compatibility (DS) mode
if (Index & 0x01) Data = TSCMode;
else
{
TSCMode = val;
if (TSCMode == 0x00)
{
Log(LogLevel::Debug, "DSi_SPI_TSC: DS-compatibility mode\n");
DataPos = 0;
NDS::KeyInput |= (1 << (16+6));
return;
}
}
}
}
else
{
Log(LogLevel::Debug, "DSi_SPI_TSC: unknown IO, bank=%02X, index=%02X (%02X %s)\n", Bank, Index, Index>>1, (Index&1)?"read":"write");
}
Index += (1<<1); // increment index
}
if (hold) DataPos++;
else DataPos = 0;
}
}
|