-
Notifications
You must be signed in to change notification settings - Fork 0
/
seil.c
executable file
·377 lines (343 loc) · 9.32 KB
/
seil.c
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#include <stdint.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/sfr_defs.h>
#include <avr/cpufunc.h>
#include <avr/pgmspace.h>
#include <stdbool.h>
#define F_CPU 8000000UL
#include <util/delay.h>
#define LOST_THRES 120
#define SYNC_THRES 240
/*
W Ü T E N D E S S E I L
=========================
HYDRA v1 CFW by ikari_01 <[email protected]>
Features:
* MANUAL input selection (press middle button to toggle)
* fast input seeking, no running light
* LEDs are OFF during seeking, only the input found will be illuminated
* LED PWM disabled to eliminate humming noise in the audio output
* 2s input loss tolerance (to avoid seeking on console reset)
// * active input stack
// - remembers the order of inputs used and rewinds them on
// loss of an input (not yet)
* ... (forgot)
*/
/* PORT MAP FOR HYDRA
PB2 out front LED + video buffer LPF (0=on, 1=off)
PB3 out extension option select (input 9-24)
PB4 out extension option select (input 17-24)
PC0 in right button
PC1 in center button
PC2 in left button
PC3 in input extension sense 0
PC4 in input extension sense 1
PD0 out UART SPI master in (set as output to enable UART SPI)
PD1 out UART SPI master out
PD2 in CSync (triggers INT0)
PD3 out LED shift register load
PD4 out UART SPI clock out
PD5 out input enable shift register load
*/
/* BUTTON MAPPING FOR HYDRA CFW:
center button:
- push: toggle manual/auto input select
left+right button:
- push simultaneously: toggle SCART input illumination
left/right button:
- manual mode: select input
- auto mode: force input change
*/
FUSES = {
.low = LFUSE_DEFAULT | ~(FUSE_CKDIV8),
.high = FUSE_SPIEN,
.extended = EFUSE_DEFAULT
};
uint8_t autoselect;
uint8_t last_manual_input;
uint8_t num_inputs;
uint8_t current_input;
volatile uint8_t debounce_buf[8];
uint8_t debounce_count;
uint8_t buttons;
uint8_t phasecount;
uint32_t leds;
uint8_t tick_cnt;
uint8_t enable_front_led;
uint8_t enable_input_leds;
uint8_t input_lost = 1;
volatile uint8_t csync_count;
uint8_t p0, p1, p2, p3;
uint8_t prev_input;
volatile uint8_t input_sample_en = 0;
/* This sets two additional GPIOs low, depending on the
currently selected input. Purpose unknown yet
PB3 = 0 for input >= 8
PB4 = 0 for input >= 16 */
void set_option_outputs() {
if(current_input >= 8) {
PORTB &= ~(_BV(3));
if(current_input >= 16) {
PORTB &= ~(_BV(4));
} else {
PORTB |= _BV(4);
}
} else {
PORTB |= _BV(3) | _BV(4);
}
return;
}
void init_GPIO() {
/* PD2 pullup */
PORTD |= _BV(2);
DDRC &= ~(_BV(0) | _BV(1) | _BV(2) | _BV(3) | _BV(4));
DDRD = _BV(0) | _BV(1) | _BV(3) | _BV(4) | _BV(5);
DDRB |= _BV(2) | _BV(3) | _BV(4);
PORTB &= ~(_BV(3) | _BV(4));
for (volatile uint16_t i = 0; i < 512; i++);
return;
}
void init_SPI() {
/* reset baud rate */
UBRR0H = 0;
UBRR0L = 0;
/* set USART to SPI mode */
UCSR0C = (_BV(UMSEL01) | _BV(UMSEL00));
UCSR0B = _BV(TXEN0);
/* must reset baud rate again after TX enable */
UBRR0H = 0;
UBRR0L = 0;
return;
}
void init_timer_IRQ() {
GTCCR = _BV(PSRSYNC); /* reset prescaler */
TCCR0A = 0; /* no compare/PWM/signal gen */
TCCR0B = 0x02; /* clock select: clk_io / 8 */
TIMSK0 = _BV(TOIE0); /* enable Timer0 overflow interrupt */
return;
}
void init_pin_IRQ() {
EICRA = _BV(ISC01) | _BV(ISC00); /* generate INT0 on rising edge of INT0 pin PD2 */
EIMSK = _BV(INT0); /* enable INT0 */
return;
}
void clear_PD0() {
PORTD &= ~(_BV(0));
return;
}
void set_PD0() {
PORTD |= _BV(0);
return;
}
void spi_send(uint8_t data) {
UDR0 = data;
while(!(UCSR0A & _BV(TXC0)));
UCSR0A = _BV(TXC0);
return;
}
void spi_send24(uint32_t data) {
spi_send((data >> 16) & 0xff);
spi_send((data >> 8) & 0xff);
spi_send((data >> 0) & 0xff);
return;
}
void update_debounce_buf() {
debounce_count = (debounce_count + 1) & 0x07;
debounce_buf[debounce_count] = (~PINC) & 0x1f;
return;
}
const uint8_t masktable[8] PROGMEM = {
0x08, 0x04, 0x02, 0x01, 0x10, 0x20, 0x40, 0x80
};
uint32_t get_input_enable(uint8_t input) {
union {
uint32_t result;
uint8_t ena[4];
} bits;
bits.result = 0;
bits.ena[(input >> 3) & 0x3] = pgm_read_byte(&(masktable[input & 0x7]));
return bits.result;
}
void sr_latch_inputs() {
PORTD &= ~(_BV(5));
_NOP();
PORTD |= _BV(5);
_NOP();
PORTD &= ~(_BV(5));
return;
}
void sr_latch_leds() {
PORTD &= ~(_BV(3));
_NOP();
PORTD |= _BV(3);
_NOP();
PORTD &= ~(_BV(3));
return;
}
bool get_debounced_buttons(uint8_t *changed) {
uint8_t tmp = 0x07;
for(int i=0; i < 8; i++) {
tmp &= debounce_buf[i] & 7;
}
if(tmp == 0) {
// no buttons pressed for at least 8 ticks
buttons = 0;
*changed = 0;
return false;
} else if(buttons != tmp) {
// buttons pressed and state changed since last poll
buttons = tmp;
*changed = 1;
return true;
} else {
// buttons pressed but state unchanged since last poll
*changed = 0;
return true;
}
}
int main(void) {
init_GPIO();
init_SPI();
init_timer_IRQ();
init_pin_IRQ();
PORTD &= ~(_BV(0) | _BV(1) | _BV(4));
sei();
enable_front_led = 1;
PORTB &= ~(_BV(2));
enable_input_leds = 1;
autoselect = 0;
uint8_t buttons_changed;
int8_t search_dir, next_input;
/* Detect input extension (Hydra "HEADS") by pulling up the presence
pins and checking for GNDed inputs
no ext -> 8 inputs
one ext -> 16 inputs
two exts -> 24 inputs */
PORTC = _BV(3) | _BV(4);
_NOP(); _NOP();
uint8_t ext_sense = (~PINC) & (_BV(3) | _BV(4));
if(ext_sense == (_BV(3) | _BV(4))) {
num_inputs = 24;
} else if(ext_sense == _BV(3) || ext_sense == _BV(4)) {
num_inputs = 16;
} else {
num_inputs = 8;
}
search_dir = 1;
uint8_t frames_lost = 0;
uint8_t input_active = 0;
while(1) {
/* wait for input change complete */
while(!input_sample_en);
csync_count = 0;
/* count hsync for a given time (via INT0 handler) */
_delay_ms(20);
bool force_input_change;
bool repeat_button_poll;
do {
/* less than 128 scanlines -> "no sync" */
if(csync_count < SYNC_THRES && autoselect) {
if(input_active) {
/* grace period on sync loss when input was active
-> don't lose input when the console/computer is merely reset */
input_active = 0;
frames_lost = 1;
} else if (frames_lost) {
frames_lost++;
}
if(frames_lost >= LOST_THRES) {
/* No sync for a longer time -> finally switch input */
input_lost = 1;
frames_lost = 0;
}
} else {
input_lost = 0;
frames_lost = 0;
input_active = 1;
}
if(input_lost || force_input_change) {
input_active = 0;
input_lost = 1;
next_input = current_input + search_dir;
if(next_input >= num_inputs) {
next_input = 0;
}
if(next_input < 0) {
next_input = num_inputs - 1;
}
current_input = next_input;
input_sample_en = 0;
}
force_input_change = false;
do {
repeat_button_poll = false;
bool buttons_pressed = get_debounced_buttons(&buttons_changed);
if(buttons_pressed && buttons_changed) {
// get buttons
if(buttons == _BV(1)) {
autoselect ^= 1;
// Toggle autoselect and force immediate scan on inactive input.
// An active input will override this before scan is forced.
input_lost = 1;
} else if(buttons == _BV(2)) {
// l button pushed - direction left
search_dir = 1;
force_input_change = true;
} else if(buttons == _BV(0)) {
// r button pushed - direction right
search_dir = -1;
force_input_change = true;
} else if(buttons == (_BV(0) | _BV(2))) {
// l+r buttons pushed - toggle active input LED on/off
enable_input_leds ^= 1;
repeat_button_poll = true;
}
}
} while (repeat_button_poll);
} while (force_input_change);
}
}
ISR(INT0_vect) {
/* gets triggered on rising edge of CSync */
if(csync_count <= SYNC_THRES && input_sample_en) csync_count++;
}
ISR(TIMER0_OVF_vect, ISR_BLOCK) {
/* gets triggered periodically:
* F_CPU = 8 MHz
* Timer prescaler = 8
* Timer period = 256
* => Frequency: 8/8/256 ~= 3.9kHz */
update_debounce_buf();
tick_cnt = (tick_cnt + 1) & 0x1f;
// every 32 ticks: update LEDs and inputs
if(!tick_cnt) {
/*
* briefly activate both inputs before switching to new input;
* while make-before-break switching should generally be frowned upon,
* with this hardware it helps greatly reduce clicking and popping when
* switching inputs
*/
prev_input = p3;
p3=p2;
p2=p1;
p1=p0;
p0=current_input;
uint32_t input_en;
uint32_t input_en_p;
input_en = get_input_enable(current_input);
input_en_p = get_input_enable(prev_input);
uint32_t input_en_final = input_en | input_en_p;
if(input_en == input_en_p) {
input_sample_en = 1;
} else {
input_sample_en = 0;
}
spi_send24(input_en_final);
sr_latch_inputs();
if(!enable_input_leds || input_lost) {
spi_send24(0);
}
sr_latch_leds();
}
}