-
Notifications
You must be signed in to change notification settings - Fork 2
/
AmstradXTPS2.ino
280 lines (256 loc) · 8.5 KB
/
AmstradXTPS2.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
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
///////
/////// AmstradXTPS2.ino
///////
/////// PS/2 TO AMSTRAD XT (PC1512/PC1640/...) KEYBOARD CONVERTER
///////
/////// Should work with: Amstrad PC1512 / PC1640 / PC2086 / PC3086.
/////// Tesded Platforms: PC1512
///////
/////// Please add https://github.com/techpaul/PS2KeyAdvanced Arduino library to compile.
///////
/////// Code based on PS/2 to XT keyboard converter https://github.com/kesrut/pcxtkbd
/////// http://www.ccgcpu.com/2019/02/14/the-xt-part-4-adapting-a-modernish-keyboard/#How_the_XT_protocol_and_keyboards_work
///////
/////// Amstrad PC1512 Protocol documentation:
/////// https://www.seasip.info/AmstradXT/pc1512kbd.html
/////// http://www.kbdbabel.org/signaling/index.html
///////
/////// Versions:
/////// 0.1 initial working version (30/04/21)
///////
///////
////////////////////////////////////////////////////////////////////
#include <PS2KeyAdvanced.h>
#define ps_clk 2 // PS CLOCK DATA
#define ps_data 4 // PS DATA PIN
uint16_t c;
PS2KeyAdvanced keyboard;
unsigned char translationTable [256];
#define xt_clk 3 // XT CLOCK PIN
#define xt_data 5 // XT DATA PIN
void setupTable () {
for (int i=0;i<256;i++) {
translationTable[i]=0xff;
}
translationTable[PS2_KEY_SCROLL]=0x46;
translationTable[PS2_KEY_CAPS]=0x3A;
translationTable[PS2_KEY_PAUSE]=0x5f;
translationTable[PS2_KEY_L_SHIFT]=0x2A;
translationTable[PS2_KEY_R_SHIFT]=0x36;
translationTable[PS2_KEY_L_ALT] = 0x38 ;
translationTable[PS2_KEY_L_CTRL]=0x1D;
translationTable[PS2_KEY_L_ARROW]=0x4B;
translationTable[PS2_KEY_R_ARROW]=0x4D;
translationTable[PS2_KEY_UP_ARROW]=0x48;
translationTable[PS2_KEY_DN_ARROW]=0x50;
translationTable[PS2_KEY_DELETE]=0x53;
translationTable[PS2_KEY_ESC]=0x01;
translationTable[PS2_KEY_BS]=0x0E;
translationTable[PS2_KEY_TAB]=0x0F;
translationTable[PS2_KEY_ENTER]=0x1C;
translationTable[PS2_KEY_SPACE]=0x39;
translationTable[PS2_KEY_SEMI] = 0x27 ;
translationTable[PS2_KEY_KP0]=0x52;
translationTable[PS2_KEY_KP1]=0x4F;
translationTable[PS2_KEY_KP2]=0x50;
translationTable[PS2_KEY_KP3]=0x51;
translationTable[PS2_KEY_KP4]=0x4B;
translationTable[PS2_KEY_KP5]=0x4C;
translationTable[PS2_KEY_KP6]=0x4D;
translationTable[PS2_KEY_KP7]=0x47;
translationTable[PS2_KEY_KP8]=0x48;
translationTable[PS2_KEY_KP9]=0x49;
translationTable[PS2_KEY_OPEN_SQ]=0x1A;
translationTable[PS2_KEY_CLOSE_SQ]=0x1B;
translationTable[PS2_LOCK_NUM]=0x45;
//translationTable[PS2_KEY_KP_DOT]=0x53;
translationTable[PS2_KEY_KP_PLUS]=0x4E;
translationTable[PS2_KEY_KP_MINUS]=0x4A;
translationTable[PS2_KEY_KP_TIMES]=0x37;
translationTable[PS2_KEY_KP_DIV]=0x35;
translationTable[PS2_KEY_KP_ENTER]=0x01;
translationTable[PS2_KEY_0]=0x0B;
translationTable[PS2_KEY_1]=0x02;
translationTable[PS2_KEY_2]=0x03;
translationTable[PS2_KEY_3]=0x04;
translationTable[PS2_KEY_4]=0x05;
translationTable[PS2_KEY_5]=0x06;
translationTable[PS2_KEY_6]=0x07;
translationTable[PS2_KEY_7]=0x08;
translationTable[PS2_KEY_8]=0x09;
translationTable[PS2_KEY_9]=0x0A;
translationTable[PS2_KEY_APOS]=0x28;
translationTable[PS2_KEY_COMMA]=0x33;
translationTable[PS2_KEY_MINUS]=0x0C;
translationTable[PS2_KEY_SCROLL] = 0x46 ;
translationTable[PS2_KEY_DOT]=0x34;
translationTable[PS2_KEY_BACK] =0x2B ;
translationTable[PS2_KEY_SINGLE] = 0x29 ;
translationTable[PS2_KEY_DIV]=0x35;
translationTable[PS2_KEY_SINGLE]=28;
translationTable[PS2_KEY_A]=0x1E;
translationTable[PS2_KEY_B]=0x30;
translationTable[PS2_KEY_C]=0x2E;
translationTable[PS2_KEY_D]=0x20;
translationTable[PS2_KEY_E]=0x12;
translationTable[PS2_KEY_F]=0x21;
translationTable[PS2_KEY_G]=0x22;
translationTable[PS2_KEY_H]=0x23;
translationTable[PS2_KEY_I]=0x17;
translationTable[PS2_KEY_J]=0x24;
translationTable[PS2_KEY_K]=0x25;
translationTable[PS2_KEY_L]=0x26;
translationTable[PS2_KEY_M]=0x32;
translationTable[PS2_KEY_N]=0x31;
translationTable[PS2_KEY_O]=0x18;
translationTable[PS2_KEY_P]=0x19;
translationTable[PS2_KEY_Q]=0x10;
translationTable[PS2_KEY_R]=0x13;
translationTable[PS2_KEY_S]=0x1F;
translationTable[PS2_KEY_T]=0x14;
translationTable[PS2_KEY_U]=0x16;
translationTable[PS2_KEY_V]=0x2F;
translationTable[PS2_KEY_W]=0x11;
translationTable[PS2_KEY_X]=0x2D;
translationTable[PS2_KEY_Y]=0x15;
translationTable[PS2_KEY_Z]=0x2c;
translationTable[PS2_KEY_EQUAL]=0x0D;
translationTable[PS2_KEY_F1]=0x3B;
translationTable[PS2_KEY_F2]=0x3C;
translationTable[PS2_KEY_F3]=0x3D;
translationTable[PS2_KEY_F4]=0x3E;
translationTable[PS2_KEY_F5]=0x3F;
translationTable[PS2_KEY_F6]=0x40;
translationTable[PS2_KEY_F7]=0x41;
translationTable[PS2_KEY_F8]=0x42;
translationTable[PS2_KEY_F9]=0x43;
translationTable[PS2_KEY_F10]=0x44;
translationTable[PS2_KEY_F11]=0x85;
translationTable[PS2_KEY_F12]=0x86;
}
void _write(unsigned char value)
{
while (digitalRead(xt_data) != HIGH) ; //ACK when xt_data=HIGH to send new keystroke
unsigned char bits[8] ;
byte p = 7 ;
byte i = 0 ;
byte j = 0 ;
for (j=0 ; j < 8; j++)
{
if (value & 1) bits[j] = 1 ;
else bits[j] = 0 ;
value = value >> 1 ;
}
for (i=0; i < 8; i++)
{
digitalWrite(xt_data, bits[p]) ;
delayMicroseconds(1) ;
digitalWrite(xt_clk, LOW) ;
delayMicroseconds(1) ;
digitalWrite(xt_data, HIGH) ;
delayMicroseconds(1) ;
digitalWrite(xt_clk, HIGH) ;
delayMicroseconds(30) ;
p-- ;
}
digitalWrite(xt_clk, HIGH) ;
digitalWrite(xt_data, HIGH) ;
delay(1) ;
}
void setup()
{
setupTable () ;
keyboard.begin( ps_data, ps_clk );
#ifdef DEBUG
Serial.begin(9600) ;
#endif
pinMode(xt_clk, OUTPUT) ;
pinMode(xt_data, OUTPUT) ;
digitalWrite(xt_clk, HIGH) ;
digitalWrite(xt_data, HIGH) ;
// to avoid initial error at BIOS keyboard check
delay(15000) ;
_write(0xAA) ;
delay(5000) ;
_write(0xAA) ;
delay(5000) ;
_write(0xAA) ;
// add more delay _write if you still get stuck on initial BIOS keyboard error
}
byte i = 0 ;
unsigned char ascan;
void loop()
{
if( keyboard.available() )
{
c = keyboard.read();
if( c > 0 )
{
#if defined(ARDUINO_ARCH_AVR)
Serial.print( F( "Value " ) );
Serial.print( c, HEX );
Serial.print( F( " - Status Bits " ) );
Serial.print( c >> 8, HEX );
Serial.print( F( " Code " ) );
#elif defined(ARDUINO_ARCH_SAM)
Serial.print( "Value " );
Serial.print( c, HEX );
Serial.print( " - Status Bits " );
Serial.print( c >> 8, HEX );
Serial.print( " Code " );
#endif
Serial.println( c & 0xFF, HEX );
}
if( !( c & PS2_BREAK ) )
{
switch (c & 0xff)
{
case PS2_KEY_L_CTRL: _write(0xE0); _write(0x1D); break ;
case PS2_KEY_R_ALT: _write(0xE0); _write(0x38); break ;
case PS2_KEY_INSERT: _write(0xE0); _write(0x52); break ;
case PS2_KEY_HOME: _write(0xE0); _write(0x47); break ;
case PS2_KEY_END: _write(0xE0); _write(0x4F); break ;
case PS2_KEY_DN_ARROW: _write(0xE0); _write(0x50); break ;
case PS2_KEY_PGDN: _write(0xE0); _write(0x51); break ;
case PS2_KEY_L_ARROW: _write(0xE0); _write(0x4B); break ;
case PS2_KEY_R_ARROW: _write(0xE0); _write(0x4D); break ;
case PS2_KEY_UP_ARROW: _write(0xE0); _write(0x48); break ;
case PS2_KEY_PGUP: _write(0xE0); _write(0x49); break ;
case PS2_KEY_DELETE: _write(0xE0); _write(0x53); break ;
default:
{
ascan = translationTable[c & 0xff];
_write(ascan) ; break ;
}
}
}
if( ( c & PS2_BREAK ) )
{
switch (c & 0xff)
{
case PS2_KEY_L_CTRL: _write(0xE0); _write(0x1D | 0x80); break ;
case PS2_KEY_R_ALT: _write(0xE0); _write(0x38 | 0x80); break ;
case PS2_KEY_INSERT: _write(0xE0); _write(0x52 | 0x80); break ;
case PS2_KEY_HOME: _write(0xE0); _write(0x47 | 0x80); break ;
case PS2_KEY_END: _write(0xE0); _write(0x4F | 0x80); break ;
case PS2_KEY_DN_ARROW: _write(0xE0); _write(0x50 | 0x80); break ;
case PS2_KEY_PGDN: _write(0xE0); _write(0x51 | 0x80); break ;
case PS2_KEY_L_ARROW: _write(0xE0); _write(0x4B | 0x80); break ;
case PS2_KEY_R_ARROW: _write(0xE0); _write(0x4D | 0x80); break ;
case PS2_KEY_UP_ARROW: _write(0xE0); _write(0x48 | 0x80); break ;
case PS2_KEY_PGUP: _write(0xE0); _write(0x49 | 0x80); break ;
case PS2_KEY_DELETE: _write(0xE0); _write(0x53 | 0x80); break ;
default:
{
ascan = translationTable[c & 0xff];
_write(ascan | 0x80) ;
}
}
}
}
if (digitalRead(xt_clk) == LOW) // power-on self test
{
delay(10) ;
_write(0xAA) ;
}
}