-
Notifications
You must be signed in to change notification settings - Fork 1
/
hardware.ino
237 lines (212 loc) · 7.43 KB
/
hardware.ino
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
/*=====================================================================
* Open Vega+ Emulator. Handheld ESP32 based hardware with
* ZX Spectrum emulation
*
* (C) 2019 Alvaro Alea Fernandez
*
* This program is free software; redistributable under the terms of
* the GNU General Public License Version 2
*
* Based on the Aspectrum emulator Copyright (c) 2000 Santiago Romero Iglesias
*======================================================================
*
* This file contains the Real Hardware to Emulate Hardware part
*
*======================================================================
*/
#include "hardware.h"
#include <Wire.h>
//FIXME part of struct of speccy???
uint8_t border=0;
uint8_t kempston_port=0xFF;
uint8_t ulaport_FF=0xFF;
// Con estas variables se controla el mapeado de los botones hardware reales a los virtuales del spectrum
uint8_t mappingkey[4][12]={
{SPECKEY_Z, SPECKEY_M,SPECKEY_SPACE, SPECKEY_ENTER, SPECKEY_Q, SPECKEY_A, SPECKEY_O, SPECKEY_P, VEGAKEY_MENU,SPECKEY_SHIFT,SPECKEY_J,SPECKEY_H},
{SPECKEY_P, JOYK_FIRE,SPECKEY_SPACE, SPECKEY_ENTER, JOYK_UP, JOYK_DOWN, JOYK_LEFT, JOYK_RIGHT, VEGAKEY_MENU,SPECKEY_SHIFT,SPECKEY_J,SPECKEY_H},
{SPECKEY_P, SPECKEY_0,SPECKEY_SPACE, SPECKEY_ENTER, SPECKEY_9, SPECKEY_8, SPECKEY_6, SPECKEY_7, VEGAKEY_MENU,SPECKEY_SHIFT,SPECKEY_J,SPECKEY_H},
{SPECKEY_5, SPECKEY_M,SPECKEY_SPACE, SPECKEY_ENTER, SPECKEY_P, SPECKEY_L, SPECKEY_Z, SPECKEY_X, VEGAKEY_MENU,SPECKEY_SHIFT,SPECKEY_J,SPECKEY_H},
};
// Con estas variables se controla el mapeado de las teclas virtuales del spectrum a I/O port
const int key2specy[2][41]={
{ 0, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4,
2, 2, 2, 2, 2, 5, 5, 5, 5, 5,
1, 1, 1, 1, 1, 6, 6, 6, 6, 6,
0, 0, 0, 0, 0, 7, 7, 7, 7, 7 },
{ 0, 0xFE, 0xFD, 0xFB, 0xF7, 0xEF, 0xEF, 0xF7, 0xFB, 0xFD, 0xFE,
0xFE, 0xFD, 0xFB, 0xF7, 0xEF, 0xEF, 0xF7, 0xFB, 0xFD, 0xFE,
0xFE, 0xFD, 0xFB, 0xF7, 0xEF, 0xEF, 0xF7, 0xFB, 0xFD, 0xFE,
0xFE, 0xFD, 0xFB, 0xF7, 0xEF, 0xEF, 0xF7, 0xFB, 0xFD, 0xFE }
};
uint8_t speckey[8]={0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
int keys[16]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int oldkeys[16]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
// MAPA conlos 8 INS + KEMPSTON
// MAPA con 8 botones a >teclas
// mapeamos hardware a teclas
// mapeamos teclas a INS
void leebotones(void){
int c;
keypad_i2c_read();
for(c=0;c<12;c++){
if (keys[c]!=oldkeys[c]) {
AS_printf("B="); AS_print(c);
AS_printf("\tM="); AS_print(mappingkey[emuopt.mappingindex][c]);
AS_printf("\n");
updatekey(mappingkey[emuopt.mappingindex][c], keys[c]);
// paint_tecla (mappingkey[emuopt.mappingindex][c],keys[c]);
// paint_button(c,keys[c]);
}
oldkeys[c]=keys[c];
}
}
int mirabotones(void){
int c;
int r=-1;
static int tmp_oldkeys[16]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
keypad_i2c_read();
for(c=0;c<12;c++){
if (keys[c]!=tmp_oldkeys[c]) {
if (keys[c]==0) r=c;
}
tmp_oldkeys[c]=keys[c];
}
return r;
}
void updatekey(uint8_t key, uint8_t state){
uint8_t n;
switch (key) {
case SPECKEY_NONE :
break;
case VEGAKEY_MENU :
if (state==1) gui_Main_Menu();
break;
case JOYK_LEFT :
if (state==1) kempston_port &=!0x01;
else kempston_port |= 0x01;
break;
case JOYK_RIGHT :
if (state==1) kempston_port &=!0x02;
else kempston_port |= 0x02;
break;
case JOYK_UP :
if (state==1) kempston_port &=!0x04;
else kempston_port |= 0x04;
break;
case JOYK_DOWN :
if (state==1) kempston_port &=!0x08;
else kempston_port |= 0x08;
break;
case JOYK_FIRE :
if (state==1) kempston_port &=!0x10;
else kempston_port |= 0x10;
break;
default:
if (state==1) n= key2specy[1][key] ;
else n= (key2specy[1][key])^0xFF ;
if (state==1) speckey[ key2specy[0][key] ] &= key2specy[1][key] ;
else speckey[ key2specy[0][key] ] |= ((key2specy[1][key])^0xFF) ;
// AS_printf("Fila %i, state %i, es:",key2specy[0][key],state); AS_print(speckey[ key2specy[0][key] ],BIN);
// AS_printf(" sacado de:"); AS_print(n,BIN);
// AS_print("\n");
break;
}
}
void paint_tecla(int b, int state){
const uint8_t k[5][48]= {
{0, 255,255,255,255,255, 255,255,255,255,255,
255,255,255,255,255, 255,255,255,255,255,
255,255,255,255,255, 255,255,255,255,255,
255,255,255,255,255, 255,255,255,255,255,
0, 0, 0, 0, 0, 255 },
{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, 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,
255,255,255,255,255, 255 },
{0, 0, 5, 10, 15, 20, 25, 30, 35, 40, 45,
0, 5, 10, 15, 20, 25, 30, 35, 40, 45,
0, 5, 10, 15, 20, 25, 30, 35, 40, 45,
0, 5, 10, 15, 20, 25, 30, 35, 40, 45,
65, 65, 60, 70, 75, 50 },
{0, 220,220,220,220,220, 220,220,220,220,220,
225,225,225,225,225, 225,225,225,225,225,
230,230,230,230,230, 230,230,230,230,230,
235,235,235,235,235, 235,235,235,235,235,
220,230,225,225,235, 235 }
};
if (b!=SPECKEY_NONE) {
if (state==0) {
tft.fillRect(k[3][b],k[4][b],4,3,tft.color565(0,0,0));
} else {
tft.fillRect(k[3][b],k[4][b],4,3,tft.color565(k[0][b],k[1][b],k[2][b]));
}
}
}
void paint_button(int b, int state){
const uint8_t k[5][12]= {
{ 255,255, 0, 0, 255,255,255,255, 0,255,128,128},
{ 0,255,255, 0, 255,255,255,255, 255, 0,128, 0},
{ 0, 0, 0,255, 255,255,255,255, 255,255,128, 0},
{ 30,35,30,25, 10, 10, 5, 15, 35,35,30,25},
{ 9,12,15,12, 9,15,12,12, 3,19,19,19}
};
if (state==0) {
tft.fillRect(k[3][b],k[4][b],4,3,tft.color565(0,0,0));
} else {
tft.fillRect(k[3][b],k[4][b],4,3,tft.color565(k[0][b],k[1][b],k[2][b]));
}
}
// Function for writing two Bytes to the I2C expander device
void pf575_write(uint16_t dato)
{
Wire.beginTransmission(KEYPADADRESS);
Wire.write(lowByte(dato));
Wire.write(highByte(dato));
Wire.endTransmission();
}
uint16_t pf575_read(void){
uint8_t a,b;
Wire.requestFrom(KEYPADADRESS, 2); //request two bytes of data
if (Wire.available()) { //really this do anything??
a = Wire.read(); //read byte 1
b = Wire.read(); //read byte 2
}
return (uint16_t)b + (uint16_t)a<<8 ;
}
void keypad_i2c_read(void){
uint8_t dataReceived[3]; //a two byte array to hold our data
int c;
pf575_write(0xFFFE);
// dataReceived[1]=lowByte(pf575_read());
Wire.requestFrom(KEYPADADRESS, 2); //request two bytes of data
if (Wire.available()) {
dataReceived[0] = Wire.read(); //read byte 1
dataReceived[1] = Wire.read(); //read byte 2
}
pf575_write(0xFFFD);
// dataReceived[2]=lowByte(pf575_read());
Wire.requestFrom(KEYPADADRESS, 2); //request two bytes of data
if (Wire.available()) {
dataReceived[0] = Wire.read(); //read byte 1
dataReceived[2] = Wire.read(); //read byte 2
}
pf575_write(0xFFFF);
for(c=0;c<8;c++){
keys[c]=!(dataReceived[1] & 1);
dataReceived[1]=dataReceived[1]>>1;
}
for(c=8;c<12;c++){
keys[c]=!(dataReceived[2] & 1);
dataReceived[2]=dataReceived[2]>>1;
}
}
void keypad_i2c_init(void){
Wire.begin();
pf575_write(0xFFFF);
}