-
Notifications
You must be signed in to change notification settings - Fork 1
/
5717emu.ino
339 lines (268 loc) · 8.55 KB
/
5717emu.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
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
/* CSG 5717 emulator
Emulates the CSG 5717 controller (inside a Commodore 1351 mouse)
on an Arduino Nano
(c) 2019 Martin Emrich <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <avr/io.h>
#include <avr/interrupt.h>
//#include <math.h>
/* run calibration at startup */
//#define CALIBRATE
/* number of samples to take to calibrate quad sensors */
#define CALIBRATION_ROUNDS 1000
/* Calibration values (maximum voltage for each sensor) for YOUR mouse sensors */
#define MAX_X0 739
#define MAX_X1 656
#define MAX_Y0 546
#define MAX_Y1 973
/* Hysteresis: plus/minus this from the middle between low and high range for sensors */
#define QUAD_HYSTERESIS 20
/*
Pin connections
*/
// Analog inputs: Quadrature Encoder wheels (photosensors)
#define QUAD_X0 A0
#define QUAD_X1 A1
#define QUAD_Y0 A2
#define QUAD_Y1 A3
#define RIGHT_BUTTON 4
//#define SCHMITT_TRIGGER_ON_2
#ifdef SCHMITT_TRIGGER_ON_2
// sync input (POT_Y from C64)
#define SYNC_INPUT 2
#define SYNC_MODE LOW
#else
#define SYNC_INPUT 3
#define SYNC_MODE LOW
#endif
// Joystick directions (Up is sent for Right button!)
#define JOY_UP 5
#define JOY_DOWN 6
#define JOY_RIGHT 7
#define JOY_LEFT 8
// Outputs
#define POT_X 9
#define POT_Y 10
// Quad raw (analog values)
uint16_t raw_x0, raw_x1, raw_y0, raw_y1;
uint8_t x0 = 0;
uint8_t x1 = 0;
uint8_t y0 = 0;
uint8_t y1 = 0;
uint8_t old_x = 0;
uint8_t old_y = 0;
volatile uint8_t x_pos = 0;
volatile uint8_t y_pos = 0;
//#define raw2bit(Q,R,MAX_R) { if (Q == 0) { if (R > (MAX_R/2+QUAD_HYSTERESIS)) Q = 1; } else { if (R < (MAX_R/2-QUAD_HYSTERESIS)) Q = 0; } }
#define raw2bit(Q,R,MAX_R) { if (R > (MAX_R/2+QUAD_HYSTERESIS)) Q = 1; if (R < (MAX_R/2-QUAD_HYSTERESIS)) Q = 0; }
/* Unused alternative approach: calculate phase angle of quad signal */
#define norm_x0 ((raw_x0 - (MAX_X0/2.0f))/(MAX_X0/2.0f))
#define norm_x1 ((raw_x1 - (MAX_X1/2.0f))/(MAX_X1/2.0f))
#define norm_y0 ((raw_y0 - (MAX_Y0/2.0f))/(MAX_Y0/2.0f))
#define norm_y1 ((raw_y1 - (MAX_Y1/2.0f))/(MAX_Y1/2.0f))
#define angle_y() (int16_t)(atanf(norm_y0/norm_y1)*100)
#define angle_x() (int16_t)(atanf(norm_x0/norm_x1)*100)
void update_pos_y()
{
raw_y0 = analogRead(QUAD_Y0);
raw_y1 = analogRead(QUAD_Y1);
raw2bit(y0, raw_y0, MAX_Y0);
raw2bit(y1, raw_y1, MAX_Y1);
/* Gray code to binary */
y0 = (y1 ? y0 : 1 - y0);
/* to single two-bit-number */
uint8_t y = y1 * 2 + y0;
int8_t y_dir = 0;
switch (old_y) {
case 0b00: if (y == 0b01) y_dir = +1; else if (y == 0b11) y_dir = -1 ; break;
case 0b01: if (y == 0b10) y_dir = +1; else if (y == 0b00) y_dir = -1 ; break;
case 0b10: if (y == 0b11) y_dir = +1; else if (y == 0b01) y_dir = -1 ; break;
case 0b11: if (y == 0b00) y_dir = +1; else if (y == 0b10) y_dir = -1 ; break;
default: break;
}
y_pos = (y_pos + y_dir) & 0x3F;
old_y = y;
}
void update_pos_x()
{
raw_x0 = analogRead(QUAD_X0);
raw_x1 = analogRead(QUAD_X1);
raw2bit(x0, raw_x0, MAX_X0);
raw2bit(x1, raw_x1, MAX_X1);
/* Gray code to binary */
x0 = (x1 ? x0 : 1 - x0);
/* to single two-bit-number */
uint8_t x = x1 * 2 + x0;
int8_t x_dir = 0;
switch (old_x) {
case 0b00: if (x == 0b01) x_dir = +1; else if (x == 0b11) x_dir = -1 ; break;
case 0b01: if (x == 0b10) x_dir = +1; else if (x == 0b00) x_dir = -1 ; break;
case 0b10: if (x == 0b11) x_dir = +1; else if (x == 0b01) x_dir = -1 ; break;
case 0b11: if (x == 0b00) x_dir = +1; else if (x == 0b10) x_dir = -1 ; break;
default: break;
}
x_pos = (x_pos + x_dir) % 64;
old_x = x;
}
#ifdef CALIBRATE
// Quad Maximums for calibration
uint16_t max_x0 = 0;
uint16_t max_x1 = 0;
uint16_t max_y0 = 0;
uint16_t max_y1 = 0;
void calibrate()
{
Serial.print("Calibrating...Move around both axes NOW for a good result...\n");
for (uint16_t counter = 0; counter < CALIBRATION_ROUNDS; counter++)
{
update_pos_x();
update_pos_y();
if (raw_x0 > max_x0) max_x0 = raw_x0;
if (raw_x1 > max_x1) max_x1 = raw_x1;
if (raw_y0 > max_y0) max_y0 = raw_y0;
if (raw_y1 > max_y1) max_y1 = raw_y1;
delay(30);
}
char quad_nums[64];
snprintf(quad_nums, 64, "Calibrated: Max: X0 %04u, X1 %04u, Y0 %04u, Y1 %04u\n", max_x0, max_x1, max_y0, max_y1);
Serial.print(quad_nums);
delay(5000);
}
#endif // CALIBRATE
uint8_t current_dir = 0;
/* Update one alternating direction, takes ca. 240uS */
void update_one_direction()
{
if (current_dir == 0) {
update_pos_x();
current_dir = 1;
} else {
update_pos_y();
current_dir = 0;
}
}
/* start high-precision timer at 0 "jiffies" (0.5µS) */
inline void hpTimerStart()
{
TCCR1A = 0;
TCCR1B = 0;
TCNT1 = 0; // reset counter
TIMSK1 = 0; // no interrupts, no compare stuff.
TCCR1B |= (0 << CS12) | (1 << CS11) | (0 >> CS10); // Prescaler: 8 (1count == 0.5µS), start clock
}
/* wait for timer to pass us Microseconds */
inline void hpTimerWaituS(uint16_t us)
{
while (TCNT1 < (us * 2)) {
asm volatile ("NOP");
}
}
/* Delay-encoded value in Host cycles */
uint16_t x_delay;
uint16_t y_delay;
#define triStatePin(PIN) pinMode(PIN, INPUT);
#define pullUpPin(PIN) digitalWrite(PIN, HIGH);pinMode(PIN, OUTPUT);
/* One cycle of
* * Measuring one axis
* * Transmitting both axes to C64
*/
void measureTransmitCycle() {
uint16_t t = TCNT1;
hpTimerStart();
// pinMode(SYNC_INPUT, INPUT);
// Diagnose: LED is on while the interrupt procedure runs
digitalWrite(LED_BUILTIN, HIGH);
// Toggle pin 12 if the interrupt pin missed a sync event
if (t > 1060) {
// we skipped a beat!
digitalWrite(12, ! digitalRead(12) );
}
/* tristate pins */
// triStatePin(POT_X);
// triStatePin(POT_Y);
update_one_direction(); // takes approx. 240us
/* Position to delay (2uS per value) */
x_delay = (x_pos * 2);
y_delay = (y_pos * 2);
/* wait 256µS (half of the SID measuring cycle) plus another 64 (ca. 65µS) */
#define TX_START_OFFSET 320
/* wait until 480µS elapsed before tristating the POT pins again */
#define TX_END_OFFSET 480
// Start 5µs "early", because switching from tristate to high takes some time
uint16_t xtime = (TX_START_OFFSET + x_delay - 5 ) * 2;
uint16_t ytime = (TX_START_OFFSET + y_delay - 5 ) * 2;
uint8_t xset = 0;
uint8_t yset = 0;
while (TCNT1 <= TX_END_OFFSET * 2) {
if (!xset && TCNT1 >= xtime) {
pullUpPin(POT_X);
xset = 1;
}
if (!yset && TCNT1 >= ytime) {
pullUpPin(POT_Y);
yset = 1;
}
}
/* tristate pins */
triStatePin(POT_X);
triStatePin(POT_Y);
digitalWrite(LED_BUILTIN, LOW);
}
void setup() {
// Inputs
pinMode(QUAD_X0, INPUT);
pinMode(QUAD_X1, INPUT);
pinMode(QUAD_Y0, INPUT);
pinMode(QUAD_Y1, INPUT);
pinMode(RIGHT_BUTTON, INPUT);
pinMode(2, INPUT);
pinMode(3, INPUT);
pinMode(SYNC_INPUT, INPUT);
/* Caution: they are pulled high inside CIA, and are intended to be shorted to ground to trigger.
* I guess it's a bad idea to pull them high here (see https://www.c64-wiki.de/wiki/Controlport)
*
* We leave them as input for now.
*/
pinMode(JOY_UP, INPUT);
pinMode(JOY_DOWN, INPUT);
pinMode(JOY_RIGHT, INPUT);
pinMode(JOY_LEFT, INPUT);
// Outputs
pinMode(12, OUTPUT);
digitalWrite(12, LOW);
triStatePin( POT_X);
triStatePin( POT_Y);
#ifdef CALIBRATE
Serial.begin(115200);
calibrate();
#endif
attachInterrupt(digitalPinToInterrupt(SYNC_INPUT), measureTransmitCycle, SYNC_MODE);
}
void loop()
{
/* do nothing here, all work is done in the interrupt routine */
while (true) {
asm ("NOP");
/* if (TCNT1 > 1040) {
digitalWrite(12, ! digitalRead(12) );
noInterrupts();
measureTransmitCycle();
interrupts();
} */
}
}