-
Notifications
You must be signed in to change notification settings - Fork 39
/
archie.c
593 lines (502 loc) · 14.5 KB
/
archie.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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
#include "stdio.h"
#include "string.h"
#include "hardware.h"
#include "menu.h"
#include "osd.h"
#include "archie.h"
#include "hdd.h"
#include "user_io.h"
#include "data_io.h"
#include "debug.h"
#define CONFIG_FILENAME "ARCHIE CFG"
#define MAX_FLOPPY 2
typedef struct {
unsigned long system_ctrl; // system control word
char rom_img[64]; // rom image file name
char cmos_img[64]; // cmos image file name
hardfileTYPE hardfile[2];
} archie_config_t;
static archie_config_t config;
static char floppy_name[MAX_FLOPPY][64];
extern char s[FF_LFN_BUF + 1];
enum state { STATE_HRST, STATE_RAK1, STATE_RAK2, STATE_IDLE,
STATE_WAIT4ACK1, STATE_WAIT4ACK2, STATE_HOLD_OFF } kbd_state;
// archie keyboard controller commands
#define HRST 0xff
#define RAK1 0xfe
#define RAK2 0xfd
#define RQPD 0x40 // mask 0xf0
#define PDAT 0xe0 // mask 0xf0
#define RQID 0x20
#define KBID 0x80 // mask 0xc0
#define KDDA 0xc0 // new key down data, mask 0xf0
#define KUDA 0xd0 // new key up data, mask 0xf0
#define RQMP 0x22 // request mouse data
#define MDAT 0x00 // mouse data, mask 0x80
#define BACK 0x3f
#define NACK 0x30 // disable kbd scan, disable mouse
#define SACK 0x31 // enable kbd scan, disable mouse
#define MACK 0x32 // disable kbd scan, enable mouse
#define SMAK 0x33 // enable kbd scan, enable mouse
#define LEDS 0x00 // mask 0xf8
#define PRST 0x21 // nop
#define QUEUE_LEN 8
static unsigned char tx_queue[QUEUE_LEN][2];
static unsigned char tx_queue_rptr, tx_queue_wptr;
#define QUEUE_NEXT(a) ((a+1)&(QUEUE_LEN-1))
static unsigned long ack_timeout;
static short mouse_x, mouse_y;
#define FLAG_SCAN_ENABLED 0x01
#define FLAG_MOUSE_ENABLED 0x02
static unsigned char flags;
// #define HOLD_OFF_TIME 2
#ifdef HOLD_OFF_TIME
static unsigned long hold_off_timer;
#endif
static char buffer[17]; // local buffer to assemble file name (8+.+3+\0)
char *archie_get_rom_name(void) {
return config.rom_img;
}
char *archie_get_cmos_name(void) {
return config.cmos_img;
}
char *archie_get_floppy_name(char i) {
if(!floppy_name[i][0]) {
strcpy(buffer, "* no disk *");
return buffer;
} else
return (floppy_name[i]);
}
void archie_save_config(void) {
FIL file;
UINT bw;
// save configuration data
if (FileOpenCompat(&file, CONFIG_FILENAME, FA_WRITE | FA_CREATE_ALWAYS) == FR_OK) {
f_write(&file, &config, sizeof(archie_config_t), &bw);
f_close(&file);
}
}
void archie_set_floppy(char i, const unsigned char *name) {
user_io_file_mount(name, i);
if (user_io_is_mounted(i)) {
strncpy(floppy_name[i], name, sizeof(floppy_name[i]));
floppy_name[i][sizeof(floppy_name[i])-1] = 0;
} else {
floppy_name[i][0] = 0;
}
}
void archie_save_cmos() {
FIL file;
archie_debugf("Saving CMOS file");
strcpy(s, "/");
strcat(s, config.cmos_img);
if (f_open(&file, s, FA_WRITE | FA_CREATE_ALWAYS) == FR_OK) {
data_io_file_rx(&file, 0x03, 256);
f_close(&file);
}
}
void archie_set_cmos(const unsigned char *name) {
FIL file;
if (!name) return;
if(f_open(&file, name, FA_READ) == FR_OK) {
archie_debugf("CMOS file %s with %llu bytes to send", name, f_size(&file));
// save file name
strncpy(config.cmos_img, name, sizeof(config.cmos_img));
config.cmos_img[sizeof(config.cmos_img)-1] = 0;
data_io_file_tx(&file, 0x03, 0);
f_close(&file);
} else
archie_debugf("CMOS %.s no found", name);
}
void archie_set_rom(const unsigned char *name) {
FIL file;
if (!name) return;
if(f_open(&file, name, FA_READ) == FR_OK) {
archie_debugf("ROM file %s with %llu bytes to send", name, f_size(&file));
// save file name
strncpy(config.rom_img, name, sizeof(config.rom_img));
config.rom_img[sizeof(config.rom_img)-1] = 0;
data_io_file_tx(&file, 0x01, 0);
f_close(&file);
} else
archie_debugf("ROM %.11s no found", name);
}
static void archie_kbd_enqueue(unsigned char state, unsigned char byte) {
if(QUEUE_NEXT(tx_queue_wptr) == tx_queue_rptr) {
archie_debugf("KBD tx queue overflow");
return;
}
archie_debugf("KBD ENQUEUE %x (%x)", byte, state);
tx_queue[tx_queue_wptr][0] = state;
tx_queue[tx_queue_wptr][1] = byte;
tx_queue_wptr = QUEUE_NEXT(tx_queue_wptr);
}
static void archie_kbd_tx(unsigned char state, unsigned char byte) {
archie_debugf("KBD TX %x (%x)", byte, state);
spi_uio_cmd_cont(0x05);
spi8(byte);
DisableIO();
kbd_state = state;
ack_timeout = GetTimer(10); // 10ms timeout
}
static void archie_kbd_send(unsigned char state, unsigned char byte) {
// don't send if we are waiting for an ack
if((kbd_state != STATE_WAIT4ACK1)&&(kbd_state != STATE_WAIT4ACK2))
archie_kbd_tx(state, byte);
else
archie_kbd_enqueue(state, byte);
}
static void archie_kbd_reset(void) {
archie_debugf("KBD reset");
tx_queue_rptr = tx_queue_wptr = 0;
kbd_state = STATE_HRST;
mouse_x = mouse_y = 0;
flags = 0;
}
void archie_init(void) {
FIL file;
UINT br;
char i;
archie_debugf("init");
ResetMenu();
ChangeDirectoryName("/");
// set config defaults
config.system_ctrl = 0;
strcpy(config.rom_img, "RISCOS.ROM");
strcpy(config.cmos_img, "CMOS.RAM");
config.hardfile[0].enabled = HDF_FILE;
strcpy(config.hardfile[0].name, "ARCHIE1.HDF");
config.hardfile[1].enabled = HDF_FILE;
strcpy(config.hardfile[1].name, "ARCHIE2.HDF");
// try to load config from card
if(FileOpenCompat(&file, CONFIG_FILENAME, FA_READ) == FR_OK) {
if(f_size(&file) == sizeof(archie_config_t))
f_read(&file, &config, sizeof(archie_config_t), &br);
else
archie_debugf("Unexpected config size %llu != %u", f_size(&file), sizeof(archie_config_t));
f_close(&file);
} else
archie_debugf("No %.11s config found", CONFIG_FILENAME);
// upload rom file
archie_set_rom(config.rom_img);
// upload ext file
if(FileOpenCompat(&file, "RISCOS EXT", FA_READ) == FR_OK) {
archie_debugf("Found RISCOS.EXT, uploading it");
data_io_file_tx(&file, 0x02, 0);
f_close(&file);
} else
archie_debugf("RISCOS.EXT no found");
// upload cmos file
archie_set_cmos(config.cmos_img);
// try to open default floppies
for(i=0;i<MAX_FLOPPY;i++) {
char fdc_name[] = "FLOPPY0.ADF";
fdc_name[6] = '0'+i;
user_io_file_mount(fdc_name, i);
if (user_io_is_mounted(i)) {
strcpy(floppy_name[i], fdc_name);
archie_debugf("Inserted floppy %d", i);
} else
floppy_name[i][0] = 0;
}
// open hdd image(s)
hardfile[0] = &config.hardfile[0];
hardfile[1] = &config.hardfile[1];
OpenHardfile(0, true);
OpenHardfile(1, true);
archie_kbd_send(STATE_RAK1, HRST);
ack_timeout = GetTimer(20); // give archie 20ms to reply
}
void archie_kbd(unsigned short code) {
archie_debugf("KBD key code %x", code);
// don't send anything yet if we are still in reset state
if(kbd_state <= STATE_RAK2) {
archie_debugf("KBD still in reset");
return;
}
// ignore any key event if key scanning is disabled
if(!(flags & FLAG_SCAN_ENABLED)) {
archie_debugf("KBD keyboard scan is disabled!");
return;
}
// select prefix for up or down event
unsigned char prefix = (code&0x8000)?KUDA:KDDA;
archie_kbd_send(STATE_WAIT4ACK1, prefix | (code>>4));
archie_kbd_send(STATE_WAIT4ACK2, prefix | (code&0x0f));
}
void archie_mouse(unsigned char b, char x, char y) {
archie_debugf("KBD MOUSE X:%d Y:%d B:%d", x, y, b);
// max values -64 .. 63
mouse_x += x;
if(mouse_x > 63) mouse_x = 63;
if(mouse_x < -64) mouse_x = -64;
mouse_y -= y;
if(mouse_y > 63) mouse_y = 63;
if(mouse_y < -64) mouse_y = -64;
// don't send anything yet if we are still in reset state
if(kbd_state <= STATE_RAK2) {
archie_debugf("KBD still in reset");
return;
}
// ignore any mouse movement if mouse is disabled or if nothing to report
if((flags & FLAG_MOUSE_ENABLED) && (mouse_x || mouse_y)) {
// send asap if no pending byte
if(kbd_state == STATE_IDLE) {
archie_kbd_send(STATE_WAIT4ACK1, mouse_x & 0x7f);
archie_kbd_send(STATE_WAIT4ACK2, mouse_y & 0x7f);
mouse_x = mouse_y = 0;
}
}
// ignore mouse buttons if key scanning is disabled
if(flags & FLAG_SCAN_ENABLED) {
static const uint8_t remap[] = { 0, 2, 1 };
static unsigned char buts = 0;
uint8_t s;
// map all three buttons
for(s=0;s<3;s++) {
uint8_t mask = (1<<s);
if((b&mask) != (buts&mask)) {
unsigned char prefix = (b&mask)?KDDA:KUDA;
archie_kbd_send(STATE_WAIT4ACK1, prefix | 0x07);
archie_kbd_send(STATE_WAIT4ACK2, prefix | remap[s]);
}
}
buts = b;
}
}
static void archie_check_queue(void) {
if(tx_queue_rptr == tx_queue_wptr)
return;
archie_kbd_tx(tx_queue[tx_queue_rptr][0], tx_queue[tx_queue_rptr][1]);
tx_queue_rptr = QUEUE_NEXT(tx_queue_rptr);
}
void archie_handle_kbd(void) {
#ifdef HOLD_OFF_TIME
if((kbd_state == STATE_HOLD_OFF) && CheckTimer(hold_off_timer)) {
archie_debugf("KBD resume after hold off");
kbd_state = STATE_IDLE;
archie_check_queue();
}
#endif
// timeout waiting for ack?
if((kbd_state == STATE_WAIT4ACK1) || (kbd_state == STATE_WAIT4ACK2)) {
if(CheckTimer(ack_timeout)) {
if(kbd_state == STATE_WAIT4ACK1)
archie_debugf(">>>> KBD ACK TIMEOUT 1ST BYTE <<<<");
if(kbd_state == STATE_WAIT4ACK2)
archie_debugf(">>>> KBD ACK TIMEOUT 2ND BYTE <<<<");
kbd_state = STATE_IDLE;
}
}
// timeout in reset sequence?
if(kbd_state <= STATE_RAK2) {
if(CheckTimer(ack_timeout)) {
archie_debugf("KBD timeout in reset state");
archie_kbd_send(STATE_RAK1, HRST);
ack_timeout = GetTimer(20); // 20ms timeout
}
}
spi_uio_cmd_cont(0x04);
if(spi_in() == 0xa1) {
unsigned char data = spi_in();
DisableIO();
archie_debugf("KBD RX %x", data);
switch(data) {
// arm requests reset
case HRST:
archie_kbd_reset();
archie_kbd_send(STATE_RAK1, HRST);
ack_timeout = GetTimer(20); // 20ms timeout
break;
// arm sends reset ack 1
case RAK1:
if(kbd_state == STATE_RAK1) {
archie_kbd_send(STATE_RAK2, RAK1);
ack_timeout = GetTimer(20); // 20ms timeout
} else
kbd_state = STATE_HRST;
break;
// arm sends reset ack 2
case RAK2:
if(kbd_state == STATE_RAK2) {
archie_kbd_send(STATE_IDLE, RAK2);
ack_timeout = GetTimer(20); // 20ms timeout
} else
kbd_state = STATE_HRST;
break;
// arm request keyboard id
case RQID:
archie_kbd_send(STATE_IDLE, KBID | 1);
break;
// arm acks first byte
case BACK:
if(kbd_state != STATE_WAIT4ACK1) {
archie_debugf("KBD unexpected BACK, resetting KBD");
kbd_state = STATE_HRST;
} else {
#ifdef HOLD_OFF_TIME
// wait some time before sending next byte
archie_debugf("KBD starting hold off");
kbd_state = STATE_HOLD_OFF;
hold_off_timer = GetTimer(10);
#else
kbd_state = STATE_IDLE;
archie_check_queue();
#endif
}
break;
// arm acks second byte
case NACK:
case SACK:
case MACK:
case SMAK:
if(((data == SACK) || (data == SMAK)) && !(flags & FLAG_SCAN_ENABLED)) {
archie_debugf("KBD Enabling key scanning");
flags |= FLAG_SCAN_ENABLED;
}
if(((data == NACK) || (data == MACK)) && (flags & FLAG_SCAN_ENABLED)) {
archie_debugf("KBD Disabling key scanning");
flags &= ~FLAG_SCAN_ENABLED;
}
if(((data == MACK) || (data == SMAK)) && !(flags & FLAG_MOUSE_ENABLED)) {
archie_debugf("KBD Enabling mouse");
flags |= FLAG_MOUSE_ENABLED;
}
if(((data == NACK) || (data == SACK)) && (flags & FLAG_MOUSE_ENABLED)) {
archie_debugf("KBD Disabling mouse");
flags &= ~FLAG_MOUSE_ENABLED;
}
// wait another 10ms before sending next byte
#ifdef HOLD_OFF_TIME
archie_debugf("KBD starting hold off");
kbd_state = STATE_HOLD_OFF;
hold_off_timer = GetTimer(10);
#else
kbd_state = STATE_IDLE;
archie_check_queue();
#endif
break;
}
} else
DisableIO();
}
void archie_handle_hdd(void) {
unsigned char c1;
EnableFpga();
c1 = SPI(0); // cmd request
SPI(0);
SPI(0);
SPI(0);
SPI(0);
SPI(0);
DisableFpga();
HandleHDD(c1, 0, 0);
}
void archie_poll(void) {
archie_handle_kbd();
archie_handle_hdd();
}
//////////////////////////
////// Archie menu ///////
//////////////////////////
static char archie_file_selected(uint8_t idx, const char *SelectedName) {
switch(idx) {
case 0:
case 1:
archie_set_floppy(idx, SelectedName);
break;
case 2:
archie_set_rom(SelectedName);
break;
case 3:
archie_set_cmos(SelectedName);
break;
}
return 0;
}
static char archie_getmenupage(uint8_t idx, char action, menu_page_t *page) {
if (action == MENU_PAGE_EXIT) return 0;
page->title = "Archie";
page->flags = OSD_ARROW_RIGHT;
page->timer = 0;
page->stdexit = MENU_STD_EXIT;
return 0;
}
static char archie_getmenuitem(uint8_t idx, char action, menu_item_t *item) {
item->stipple = 0;
item->active = 1;
item->page = 0;
item->newpage = 0;
item->newsub = 0;
if (action == MENU_ACT_RIGHT) {
SetupSystemMenu();
return 0;
}
switch (action) {
case MENU_ACT_GET:
switch(idx) {
case 0:
strcpy(s, " Floppy 0: ");
strcat(s, archie_get_floppy_name(0));
item->item = s;
break;
case 1:
strcpy(s, " Floppy 1: ");
strcat(s, archie_get_floppy_name(1));
item->item = s;
break;
case 2:
strcpy(s, " OS ROM: ");
strcat(s, archie_get_rom_name());
item->item = s;
break;
case 3:
strcpy(s, " CMOS RAM: ");
strcat(s, archie_get_cmos_name());
item->item = s;
break;
case 4:
item->item = " Save CMOS RAM";
break;
case 5:
item->item = " Save config";
break;
default:
item->active = 0;
return 0;
}
break;
case MENU_ACT_SEL:
switch(idx) {
case 0: // Floppy 0
case 1: // Floppy 1
if(user_io_is_mounted(idx)) {
archie_set_floppy(idx, NULL);
} else
SelectFileNG("ADF", SCAN_DIR | SCAN_LFN, archie_file_selected, 1);
break;
case 2: // Load ROM
SelectFileNG("ROM", SCAN_LFN, archie_file_selected, 0);
break;
case 3: // Load CMOS
SelectFileNG("RAM", SCAN_LFN, archie_file_selected, 0);
break;
case 4: // Save CMOS
archie_save_cmos();
break;
case 5: // Save config
archie_save_config();
break;
default:
return 0;
}
break;
default:
return 0;
}
return 1;
}
void archie_setup_menu()
{
SetupMenu(archie_getmenupage, archie_getmenuitem, NULL);
}