-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathspi.cpp
176 lines (154 loc) · 5.63 KB
/
spi.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
/*
Copyright 2019-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 "spi.h"
#include "core.h"
#include "settings.h"
void Spi::loadFirmware()
{
// Attempt to load the firmware
FILE *firmwareFile = fopen(Settings::getFirmwarePath().c_str(), "rb");
if (!firmwareFile) return;
fread(firmware, sizeof(uint8_t), 0x40000, firmwareFile);
fclose(firmwareFile);
}
void Spi::directBoot()
{
// Load the user settings into memory
for (uint32_t i = 0; i < 0x70; i++)
core->memory.write<uint8_t>(0, 0x27FFC80 + i, firmware[0x3FF00 + i]);
}
void Spi::setTouch(int x, int y)
{
// Read calibration points from the firmware
uint16_t adcX1 = U8TO16(firmware, 0x3FF58);
uint16_t adcY1 = U8TO16(firmware, 0x3FF5A);
uint8_t scrX1 = firmware[0x3FF5C];
uint8_t scrY1 = firmware[0x3FF5D];
uint16_t adcX2 = U8TO16(firmware, 0x3FF5E);
uint16_t adcY2 = U8TO16(firmware, 0x3FF60);
uint8_t scrX2 = firmware[0x3FF62];
uint8_t scrY2 = firmware[0x3FF63];
// Ensure the coordinates are within bounds
// A one pixel border around the screen is ignored to avoid potential underflow/overflow
// This should be fine; GBATEK says that pressing near the screen borders may be impossible anyways
if (x < 1) x = 1; else if (x > 254) x = 254;
if (y < 1) y = 1; else if (y > 190) y = 190;
// Convert the coordinates to ADC values
if (scrX2 - scrX1 != 0) touchX = (x - (scrX1 - 1)) * (adcX2 - adcX1) / (scrX2 - scrX1) + adcX1;
if (scrY2 - scrY1 != 0) touchY = (y - (scrY1 - 1)) * (adcY2 - adcY1) / (scrY2 - scrY1) + adcY1;
}
void Spi::clearTouch()
{
// Set the ADC values to their default state
touchX = 0x000;
touchY = 0xFFF;
}
void Spi::writeSpiCnt(uint16_t mask, uint16_t value)
{
// Write to the SPICNT register
mask &= 0xCF03;
spiCnt = (spiCnt & ~mask) | (value & mask);
}
void Spi::writeSpiData(uint8_t value)
{
// Don't do anything if the SPI isn't enabled
if (!(spiCnt & BIT(15)))
{
spiData = 0;
return;
}
if (writeCount == 0)
{
// On the first write, set the command byte
command = value;
address = 0;
spiData = 0;
}
else
{
switch ((spiCnt & 0x0300) >> 8) // Device
{
case 1: // Firmware
{
switch (command)
{
case 0x03: // Read data bytes
{
if (writeCount < 4)
{
// On writes 2-4, set the 3 byte address to read from
address |= value << ((3 - writeCount) * 8);
}
else
{
// On writes 5+, read data from the firmware and send it back
spiData = (address < 0x40000) ? firmware[address] : 0;
// Increment the address
// 16-bit mode is bugged; the address is incremented accordingly, but only the lower 8 bits are sent
address += (spiCnt & BIT(10)) ? 2 : 1;
}
break;
}
default:
{
printf("Write to SPI with unknown firmware command: 0x%X\n", command);
spiData = 0;
break;
}
}
break;
}
case 2: // Touchscreen
{
switch ((command & 0x70) >> 4) // Channel
{
case 1: // Y-coordinate
{
// Send the ADC Y coordinate MSB first, with 3 dummy bits in front
spiData = ((touchY << 11) & 0xFF00) | ((touchY >> 5) & 0x00FF) >> ((writeCount - 1) % 2);
break;
}
case 5: // X-coordinate
{
// Send the ADC X coordinate MSB first, with 3 dummy bits in front
spiData = ((touchX << 11) & 0xFF00) | ((touchX >> 5) & 0x00FF) >> ((writeCount - 1) % 2);
break;
}
default:
{
printf("Write to SPI with unknown touchscreen channel: %d\n", (command & 0x70) >> 4);
spiData = 0;
break;
}
}
break;
}
default:
{
printf("Write to SPI with unknown device: %d\n", (spiCnt & 0x0300) >> 8);
spiData = 0;
break;
}
}
}
// Keep track of the write count
if (spiCnt & BIT(11)) // Keep chip selected
writeCount++;
else // Deselect chip
writeCount = 0;
// Trigger a transfer finished IRQ if enabled
if (spiCnt & BIT(14))
core->interpreter[1].sendInterrupt(23);
}