forked from mist-devel/mist-firmware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser_io.c
2189 lines (1837 loc) · 54.4 KB
/
user_io.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
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "AT91SAM7S256.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "hardware.h"
#include "osd.h"
#include "state.h"
#include "state.h"
#include "user_io.h"
#include "data_io.h"
#include "archie.h"
#include "cdc_control.h"
#include "usb.h"
#include "debug.h"
#include "keycodes.h"
#include "ikbd.h"
#include "idxfile.h"
#include "spi.h"
#include "mist_cfg.h"
#include "mmc.h"
#include "tos.h"
#include "errors.h"
#include "arc_file.h"
#include "utils.h"
#include "usb/joymapping.h"
// up to 16 key can be remapped
#define MAX_REMAP 16
unsigned char key_remap_table[MAX_REMAP][2];
#define BREAK 0x8000
static IDXFile sd_image[2];
static char buffer[512];
static uint8_t buffer_drive_index = 0;
static uint32_t buffer_lba = 0xffffffff;
extern fileTYPE file;
extern char s[40];
// mouse and keyboard emulation state
typedef enum { EMU_NONE, EMU_MOUSE, EMU_JOY0, EMU_JOY1 } emu_mode_t;
static emu_mode_t emu_mode = EMU_NONE;
static unsigned char emu_state = 0;
static unsigned long emu_timer = 0;
#define EMU_MOUSE_FREQ 5
// keep state over core type and its capabilities
static unsigned char core_type = CORE_TYPE_UNKNOWN;
static char core_type_8bit_with_config_string = 0;
// core supports direct ROM upload via SS4
extern char rom_direct_upload;
// core variant (mostly for arcades)
static char core_mod = 0;
// permanent state of adc inputs used for dip switches
static unsigned char adc_state = 0;
AT91PS_ADC a_pADC = AT91C_BASE_ADC;
AT91PS_PMC a_pPMC = AT91C_BASE_PMC;
// keep state of caps lock
static char caps_lock_toggle = 0;
// avoid multiple keyboard/controllers to interfere
static uint8_t latest_keyb_priority = 0; // keyboard=0, joypad with key mappings=1
// mouse position storage for ps2 and minimig rate limitation
#define X 0
#define Y 1
#define Z 2
#define MOUSE_FREQ 20 // 20 ms -> 50hz
static int16_t mouse_pos[2][3] = { {0, 0, 0}, {0, 0, 0} };
static uint8_t mouse_flags[2] = { 0, 0 };
static unsigned long mouse_timer;
#define LED_FREQ 100 // 100 ms
static unsigned long led_timer;
char keyboard_leds = 0;
bool caps_status = 0;
bool num_status = 0;
bool scrl_status = 0;
#define RTC_FREQ 1000 // 1 s
static unsigned long rtc_timer;
#define VIDEO_KEEP_VALUE 0x87654321
#define video_keep (*(int*)0x0020FF10)
#define video_altered (*(uint8_t*)0x0020FF14)
#define video_sd_disable (*(uint8_t*)0x0020FF15)
#define video_ypbpr (*(uint8_t*)0x0020FF16)
// set by OSD code to suppress forwarding of those keys to the core which
// may be in use by an active OSD
static char osd_is_visible = false;
char user_io_osd_is_visible() {
return osd_is_visible;
}
static void PollOneAdc() {
static unsigned char adc_cnt = 0xff;
// fetch result from previous run
if(adc_cnt != 0xff) {
unsigned int result;
// wait for end of convertion
while(!(AT91C_BASE_ADC->ADC_SR & (1 << (4+adc_cnt))));
switch (adc_cnt) {
case 0: result = AT91C_BASE_ADC->ADC_CDR4; break;
case 1: result = AT91C_BASE_ADC->ADC_CDR5; break;
case 2: result = AT91C_BASE_ADC->ADC_CDR6; break;
case 3: result = AT91C_BASE_ADC->ADC_CDR7; break;
}
if(result < 128) adc_state |= (1<<adc_cnt);
if(result > 128) adc_state &= ~(1<<adc_cnt);
}
adc_cnt = (adc_cnt + 1)&3;
// Enable desired chanel
AT91C_BASE_ADC->ADC_CHER = 1 << (4+adc_cnt);
// Start conversion
AT91C_BASE_ADC->ADC_CR = AT91C_ADC_START;
}
static void InitADC(void) {
// Enable clock for interface
AT91C_BASE_PMC->PMC_PCER = 1 << AT91C_ID_ADC;
// Reset
AT91C_BASE_ADC->ADC_CR = AT91C_ADC_SWRST;
AT91C_BASE_ADC->ADC_CR = 0x0;
// Set maximum startup time and hold time
AT91C_BASE_ADC->ADC_MR = 0x0F1F0F00 | AT91C_ADC_LOWRES_8_BIT;
// make sure we get the first values immediately
PollOneAdc();
PollOneAdc();
PollOneAdc();
PollOneAdc();
}
// poll one adc channel every 25ms
static void PollAdc() {
static long adc_timer = 0;
if(CheckTimer(adc_timer)) {
adc_timer = GetTimer(25);
PollOneAdc();
}
}
void user_io_init() {
// no sd card image selected, SD card accesses will go directly
// to the card
sd_image[0].file.size = 0;
sd_image[1].file.size = 0;
if(video_keep != VIDEO_KEEP_VALUE) video_altered = 0;
video_keep = 0;
// mark remap table as unused
memset(key_remap_table, 0, sizeof(key_remap_table));
InitADC();
if(user_io_menu_button()) DEBUG_MODE_VAR = DEBUG_MODE ? 0 : DEBUG_MODE_VALUE;
iprintf("debug_mode = %d\n", DEBUG_MODE);
ikbd_init();
}
unsigned char user_io_core_type() {
return core_type;
}
char minimig_v1() {
return(core_type == CORE_TYPE_MINIMIG);
}
char minimig_v2() {
return(core_type == CORE_TYPE_MINIMIG2);
}
char user_io_create_config_name(char *s) {
char *p = user_io_get_core_name();
if(p[0]) {
strcpy(s, p);
while(strlen(s) < 8) strcat(s, " ");
strcat(s, "CFG");
return 0;
}
return 1;
}
char user_io_is_8bit_with_config_string() {
return core_type_8bit_with_config_string;
}
static char core_name[16+1]; // max 16 bytes for core name
char *user_io_get_core_name() {
char *arc_core_name = arc_get_corename();
return *arc_core_name ? arc_core_name : core_name;
}
static void user_io_read_core_name() {
core_name[0] = 0;
if(user_io_is_8bit_with_config_string()) {
char *p = user_io_8bit_get_string(0); // get core name
if(p && p[0]) strcpy(core_name, p);
}
iprintf("Core name from FPGA is \"%s\"\n", core_name);
}
void user_io_set_core_mod(char mod) {
core_mod = mod;
}
void user_io_send_core_mod() {
iprintf("Sending core mod = %d\n", core_mod);
spi_uio_cmd8(UIO_SET_MOD, core_mod);
}
void user_io_send_rtc(void) {
uint8_t date[7]; //year,month,date,hour,min,sec,day
uint8_t i;
if (usb_rtc_get_time((uint8_t*)&date)) {
//iprintf("Sending time of day %u:%02u:%02u %u.%u.%u\n",
// date[3], date[4], date[5], date[2], date[1], 1900 + date[0]);
spi_uio_cmd_cont(UIO_SET_RTC);
spi8(bin2bcd(date[5])); // sec
spi8(bin2bcd(date[4])); // min
spi8(bin2bcd(date[3])); // hour
spi8(bin2bcd(date[2])); // date
spi8(bin2bcd(date[1])); // month
spi8(bin2bcd(date[0]-100)); // year
spi8(bin2bcd(date[6])-1); //day 1-7 -> 0-6
spi8(0x40); // flag
DisableIO();
}
}
extern unsigned long iCurrentDirectory; // cluster number of current directory, 0 for root
void user_io_detect_core_type() {
core_name[0] = 0;
EnableIO();
core_type = SPI(0xff);
DisableIO();
rom_direct_upload = (core_type & 0x10) >> 4; // bit 4 - direct upload support
core_type &= 0xef;
if((core_type != CORE_TYPE_DUMB) &&
(core_type != CORE_TYPE_MINIMIG) &&
(core_type != CORE_TYPE_MINIMIG2) &&
(core_type != CORE_TYPE_PACE) &&
(core_type != CORE_TYPE_MIST) &&
(core_type != CORE_TYPE_MIST2) &&
(core_type != CORE_TYPE_ARCHIE) &&
(core_type != CORE_TYPE_8BIT))
core_type = CORE_TYPE_UNKNOWN;
switch(core_type) {
case CORE_TYPE_UNKNOWN:
iprintf("Unable to identify core (%x)!\n", core_type);
break;
case CORE_TYPE_DUMB:
puts("Identified core without user interface");
break;
case CORE_TYPE_MINIMIG:
strcpy(core_name, "MINIMIG");
puts("Identified Minimig V1 core");
break;
case CORE_TYPE_MINIMIG2:
strcpy(core_name, "MINIMIG");
puts("Identified Minimig V2 core");
break;
case CORE_TYPE_PACE:
puts("Identified PACE core");
break;
case CORE_TYPE_MIST:
case CORE_TYPE_MIST2:
strcpy(core_name, "ST");
puts("Identified MiST core");
break;
case CORE_TYPE_ARCHIE:
puts("Identified Archimedes core");
strcpy(core_name, "ARCHIE");
archie_init();
break;
case CORE_TYPE_8BIT: {
puts("Identified 8BIT core");
// send core variant first to allow the FPGA choosing the config string
user_io_send_core_mod();
// forward SD card config to core in case it uses the local
// SD card implementation
user_io_sd_set_config();
// check if core has a config string
core_type_8bit_with_config_string = (user_io_8bit_get_string(0) != NULL);
// set core name. This currently only sets a name for the 8 bit cores
user_io_read_core_name();
// send a reset
user_io_8bit_set_status(UIO_STATUS_RESET, ~0);
// try to load config
user_io_create_config_name(s);
if(strlen(s) > 0) {
iprintf("Loading config %.11s\n", s);
if (FileOpen(&file, s)) {
iprintf("Found config\n");
if(file.size <= 8) {
((unsigned long long*)sector_buffer)[0] = 0;
FileRead(&file, sector_buffer);
user_io_8bit_set_status(((unsigned long long*)sector_buffer)[0], ~1);
}
} else {
user_io_8bit_set_status(arc_get_default(), ~1);
}
// check if there's a <core>.rom present, send it via index 0
strcpy(s+8, "ROM");
if (FileOpenDir(&file, s, iCurrentDirectory) || FileOpen(&file, s))
data_io_file_tx(&file, 0);
// check if there's a <core>.ram present, send it via index -1
strcpy(s+8, "RAM");
if (FileOpen(&file, s))
data_io_file_tx(&file, -1);
// check if there's a <core>.vhd present
strcpy(s+8, "VHD");
if (FileOpen(&file, s))
user_io_file_mount(&file, 0);
}
// release reset
user_io_8bit_set_status(0, UIO_STATUS_RESET);
} break;
}
}
unsigned short usb2amiga( unsigned char k ) {
// replace MENU key by RGUI to allow using Right Amiga on reduced keyboards
// (it also disables the use of Menu for OSD)
if (mist_cfg.key_menu_as_rgui && k==0x65) {
return 0x67;
}
return usb2ami[k];
}
unsigned short usb2ps2code( unsigned char k) {
// replace MENU key by RGUI e.g. to allow using RGUI on reduced keyboards without physical key
// (it also disables the use of Menu for OSD)
if (mist_cfg.key_menu_as_rgui && k==0x65) {
return EXT | 0x27;
}
return usb2ps2[k];
}
void user_io_analog_joystick(unsigned char joystick, char valueX, char valueY) {
if(core_type == CORE_TYPE_8BIT) {
spi_uio_cmd8_cont(UIO_ASTICK, joystick);
spi8(valueX);
spi8(valueY);
DisableIO();
}
}
void user_io_digital_joystick(unsigned char joystick, unsigned char map) {
uint8_t state = map;
// "only" 6 joysticks are supported
if(joystick > 5)
return;
// if osd is open, control it via joystick
if(osd_is_visible)
return;
//iprintf("j%d: %x\n", joystick, map);
// atari ST handles joystick 0 and 1 through the ikbd emulated by the io controller
// but only for joystick 1 and 2
if((core_type == CORE_TYPE_MIST) && (joystick < 2)) {
ikbd_joystick(joystick, map);
return;
}
// every other core else uses this
// (even MIST, joystick 3 and 4 were introduced later)
spi_uio_cmd8((joystick < 2)?(UIO_JOYSTICK0 + joystick):((UIO_JOYSTICK2 + joystick - 2)), map);
}
void user_io_digital_joystick_ext(unsigned char joystick, uint16_t map) {
// "only" 6 joysticks are supported
if(joystick > 5) return;
//iprintf("ext j%d: %x\n", joystick, map);
spi_uio_cmd32(UIO_JOYSTICK0_EXT + joystick, 0x0000ffff & map);
}
static char dig2ana(char min, char max) {
if(min && !max) return -128;
if(max && !min) return 127;
return 0;
}
void user_io_joystick(unsigned char joystick, unsigned char map) {
// digital joysticks also send analog signals
user_io_digital_joystick(joystick, map);
user_io_digital_joystick_ext(joystick, map);
user_io_analog_joystick(joystick,
dig2ana(map&JOY_LEFT, map&JOY_RIGHT),
dig2ana(map&JOY_UP, map&JOY_DOWN));
}
// transmit serial/rs232 data into core
void user_io_serial_tx(char *chr, uint16_t cnt) {
if (core_type == CORE_TYPE_MIST)
spi_uio_cmd_cont(UIO_SERIAL_OUT);
else
spi_uio_cmd_cont(UIO_SIO_OUT);
while(cnt--) spi8(*chr++);
DisableIO();
}
char user_io_serial_status(serial_status_t *status_in, uint8_t status_out) {
uint8_t i, *p = (uint8_t*)status_in;
spi_uio_cmd_cont(UIO_SERIAL_STAT);
// first byte returned by core must be "magic". otherwise the
// core doesn't support this request
if(SPI(status_out) != 0xa5) {
DisableIO();
return 0;
}
// read the whole structure
for(i=0;i<sizeof(serial_status_t);i++)
*p++ = spi_in();
DisableIO();
return 1;
}
// transmit midi data into core
void user_io_midi_tx(char chr) {
spi_uio_cmd8(UIO_MIDI_OUT, chr);
}
// send ethernet mac address into FPGA
void user_io_eth_send_mac(uint8_t *mac) {
uint8_t i;
spi_uio_cmd_cont(UIO_ETH_MAC);
for(i=0;i<6;i++) spi8(*mac++);
DisableIO();
}
// set SD card info in FPGA (CSD, CID)
void user_io_sd_set_config(void) {
unsigned char data[33];
// get CSD and CID from SD card
MMC_GetCID(data);
MMC_GetCSD(data+16);
// byte 32 is a generic config byte
data[32] = MMC_IsSDHC()?1:0;
// and forward it to the FPGA
spi_uio_cmd_cont(UIO_SET_SDCONF);
spi_write(data, sizeof(data));
DisableIO();
// hexdump(data, sizeof(data), 0);
}
// read 8+32 bit sd card status word from FPGA
uint8_t user_io_sd_get_status(uint32_t *lba, uint8_t *drive_index) {
uint32_t s;
uint8_t c;
*drive_index = 0;
spi_uio_cmd_cont(UIO_GET_SDSTAT);
c = spi_in();
if ((c & 0xf0) == 0x60) *drive_index = spi_in() & 0x01;
s = spi_in();
s = (s<<8) | spi_in();
s = (s<<8) | spi_in();
s = (s<<8) | spi_in();
DisableIO();
if(lba) *lba = s;
return c;
}
// read 8 bit keyboard LEDs status from FPGA
uint8_t user_io_kbdled_get_status(void) {
uint8_t c;
spi_uio_cmd_cont(UIO_GET_KBD_LED);
c = spi_in();
DisableIO();
return c;
}
// read 32 bit ethernet status word from FPGA
uint32_t user_io_eth_get_status(void) {
uint32_t s;
spi_uio_cmd_cont(UIO_ETH_STATUS);
s = spi_in();
s = (s<<8) | spi_in();
s = (s<<8) | spi_in();
s = (s<<8) | spi_in();
DisableIO();
return s;
}
// read ethernet frame from FPGAs ethernet tx buffer
void user_io_eth_receive_tx_frame(uint8_t *d, uint16_t len) {
spi_uio_cmd_cont(UIO_ETH_FRM_IN);
while(len--) *d++=spi_in();
DisableIO();
}
// write ethernet frame to FPGAs rx buffer
void user_io_eth_send_rx_frame(uint8_t *s, uint16_t len) {
spi_uio_cmd_cont(UIO_ETH_FRM_OUT);
spi_write(s, len);
spi8(0); // one additional byte to allow fpga to store the previous one
DisableIO();
}
// the physical joysticks (db9 ports at the right device side)
// as well as the joystick emulation are renumbered if usb joysticks
// are present in the system. The USB joystick(s) replace joystick 1
// and 0 and the physical joysticks are "shifted up".
//
// Since the primary joystick is in port 1 the first usb joystick
// becomes joystick 1 and only the second one becomes joystick 0
// (mouse port)
static uint8_t joystick_renumber(uint8_t j) {
uint8_t usb_sticks = hid_get_joysticks();
// no usb sticks present: no changes are being made
if(!usb_sticks) return j;
// Keep DB9 joysticks as joystick 0 and joystick 1
// USB joysticks will be 2,3,...
if(mist_cfg.joystick_db9_fixed_index) return j;
if(j == 0) {
// if usb joysticks are present, then physical joystick 0 (mouse port)
// becomes becomes 2,3,...
j = mist_cfg.joystick0_prefer_db9 ? 0 : usb_sticks + 1;
} else {
// if one usb joystick is present, then physical joystick 1 (joystick port)
// becomes physical joystick 0 (mouse) port. If more than 1 usb joystick
// is present it becomes 2,3,...
if(usb_sticks == 1) j = 0;
else j = usb_sticks;
}
return j;
}
void user_io_joystick_emu() {
// iprintf("joystick_emu_fixed_index: %d\n", mist_cfg.joystick_emu_fixed_index);
// joystick emulation also follows renumbering if requested (default)
if(emu_mode == EMU_JOY0) user_io_joystick(mist_cfg.joystick_emu_fixed_index ? 0 : joystick_renumber(0), emu_state);
if(emu_mode == EMU_JOY1) user_io_joystick(mist_cfg.joystick_emu_fixed_index ? 1 : joystick_renumber(1), emu_state);
}
// 16 byte fifo for amiga key codes to limit max key rate sent into the core
#define KBD_FIFO_SIZE 16 // must be power of 2
static unsigned short kbd_fifo[KBD_FIFO_SIZE];
static unsigned char kbd_fifo_r=0, kbd_fifo_w=0;
static long kbd_timer = 0;
static void kbd_fifo_minimig_send(unsigned short code) {
spi_uio_cmd8((code&OSD)?UIO_KBD_OSD:UIO_KEYBOARD, code & 0xff);
kbd_timer = GetTimer(10); // next key after 10ms earliest
}
static void kbd_fifo_enqueue(unsigned short code) {
// if fifo full just drop the value. This should never happen
if(((kbd_fifo_w+1)&(KBD_FIFO_SIZE-1)) == kbd_fifo_r)
return;
// store in queue
kbd_fifo[kbd_fifo_w] = code;
kbd_fifo_w = (kbd_fifo_w + 1)&(KBD_FIFO_SIZE-1);
}
// send pending bytes if timer has run up
static void kbd_fifo_poll() {
// timer enabled and runnig?
if(kbd_timer && !CheckTimer(kbd_timer))
return;
kbd_timer = 0; // timer == 0 means timer is not running anymore
if(kbd_fifo_w == kbd_fifo_r)
return;
kbd_fifo_minimig_send(kbd_fifo[kbd_fifo_r]);
kbd_fifo_r = (kbd_fifo_r + 1)&(KBD_FIFO_SIZE-1);
}
void user_io_file_mount(fileTYPE *file, unsigned char index) {
if (file) {
iprintf("selected %.12s with %d bytes to slot %d\n", file->name, file->size, index);
memcpy(&sd_image[index].file, file, sizeof(fileTYPE));
// build index for fast random access
IDXIndex(&sd_image[index]);
} else {
iprintf("unmounting file in slot %d\n", index);
sd_image[index].file.size = 0;
}
buffer_lba = 0xffffffff;
// send mounted image size first then notify about mounting
EnableIO();
SPI(UIO_SET_SDINFO);
// use LE version, so following BYTE(s) may be used for size extension in the future.
spi32le(file ? file->size : 0);
spi32le(0); // reserved for future expansion
spi32le(0); // reserved for future expansion
spi32le(0); // reserved for future expansion
DisableIO();
// notify core of possible sd image change
spi_uio_cmd8(UIO_SET_SDSTAT, index);
}
// 8 bit cores have a config string telling the firmware how
// to treat it
char *user_io_8bit_get_string(char index) {
unsigned char i, lidx = 0, j = 0, d = 0, arc = 0;
int arc_ptr = 0;
char dip[3];
static char buffer[128+1]; // max 128 bytes per config item
// clear buffer
buffer[0] = 0;
spi_uio_cmd_cont(UIO_GET_STRING);
i = spi_in();
// the first char returned will be 0xff if the core doesn't support
// config strings. atari 800 returns 0xa4 which is the status byte
if((i == 0xff) || (i == 0xa4)) {
DisableIO();
return NULL;
}
// iprintf("String: ");
while ((i != 0) && (i!=0xff) && (j<sizeof(buffer))) {
if(i == ';') {
if(!arc && d==3 && !strncmp(dip, "DIP", 3)) {
// found "DIP", continue with config snippet from ARC
if(lidx == index) {
// skip the DIP line
j = 0;
buffer[0] = 0;
}
arc = 1;
} else {
if(lidx == index) buffer[j++] = 0;
lidx++;
}
d = 0;
} else {
if(lidx == index)
buffer[j++] = i;
if (d<3)
dip[d++] = i;
}
//iprintf("%c", i);
if (arc) {
i = arc_get_conf()[arc_ptr++];
if (!i) arc = 0;
}
if (!arc)
i = spi_in();
}
DisableIO();
// iprintf("\n");
// if this was the last string in the config string list, then it still
// needs to be terminated
if(lidx == index)
buffer[j] = 0;
// also return NULL for empty strings
if(!buffer[0])
return NULL;
return buffer;
}
unsigned long long user_io_8bit_set_status(unsigned long long new_status, unsigned long long mask) {
static unsigned long long status = 0;
// if mask is 0 just return the current status
if(mask) {
// keep everything not masked
status &= ~mask;
// updated masked bits
status |= new_status & mask;
spi_uio_cmd8(UIO_SET_STATUS, status);
spi_uio_cmd64(UIO_SET_STATUS2, status);
}
return status;
}
char kbd_reset = 0;
void user_io_send_buttons(char force) {
static unsigned char key_map = 0;
// frequently poll the adc the switches
// and buttons are connected to
PollAdc();
unsigned char map = 0;
if(adc_state & 1) map |= SWITCH2;
if(adc_state & 2) map |= SWITCH1;
if(adc_state & 4) map |= BUTTON1;
else if(adc_state & 8) map |= BUTTON2;
if(kbd_reset) map |= BUTTON2;
if(!mist_cfg.keep_video_mode) video_altered = 0;
if(video_altered & 1)
{
if(video_sd_disable) map |= CONF_SCANDOUBLER_DISABLE;
}
else
{
if(mist_cfg.scandoubler_disable) map |= CONF_SCANDOUBLER_DISABLE;
}
if(video_altered & 2)
{
if(video_ypbpr) map |= CONF_YPBPR;
}
else
{
if(mist_cfg.ypbpr) map |= CONF_YPBPR;
}
if(mist_cfg.csync_disable) map |= CONF_CSYNC_DISABLE;
if((map != key_map) || force) {
key_map = map;
spi_uio_cmd8(UIO_BUT_SW, map);
iprintf("sending keymap\n");
}
}
void set_kbd_led(unsigned char led, bool on)
{
if(led & HID_LED_CAPS_LOCK)
{
if(!(keyboard_leds & KBD_LED_CAPS_CONTROL)) hid_set_kbd_led(led, on);
caps_status = on;
}
if(led & HID_LED_NUM_LOCK)
{
if(!(keyboard_leds & KBD_LED_NUM_CONTROL)) hid_set_kbd_led(led, on);
num_status = on;
}
if(led & HID_LED_SCROLL_LOCK)
{
if(!(keyboard_leds & KBD_LED_SCRL_CONTROL)) hid_set_kbd_led(led, on);
scrl_status = on;
}
}
void user_io_poll() {
// check of core has changed from a good one to a not supported on
// as this likely means that the user is reloading the core via jtag
unsigned char ct;
static unsigned char ct_cnt = 0;
EnableIO();
ct = SPI(0xff);
DisableIO();
SPI(0xff); // needed for old minimig core
if((ct&0xef) == core_type)
ct_cnt = 0; // same core type, everything is fine
else {
// core type has changed
if(++ct_cnt == 255) {
USB_LOAD_VAR = USB_LOAD_VALUE;
// wait for a new valid core id to appear
while((ct & 0xe0) != 0xa0) {
EnableIO();
ct = SPI(0xff);
DisableIO();
SPI(0xff); // needed for old minimig core
}
// reset io controller to cope with new core
*AT91C_RSTC_RCR = 0xA5 << 24 | AT91C_RSTC_PERRST | AT91C_RSTC_PROCRST; // restart
for(;;);
}
}
if((core_type != CORE_TYPE_MINIMIG) &&
(core_type != CORE_TYPE_MINIMIG2) &&
(core_type != CORE_TYPE_PACE) &&
(core_type != CORE_TYPE_MIST) &&
(core_type != CORE_TYPE_MIST2) &&
(core_type != CORE_TYPE_ARCHIE) &&
(core_type != CORE_TYPE_8BIT)) {
return; // no user io for the installed core
}
if((core_type == CORE_TYPE_MIST) ||
(core_type == CORE_TYPE_MIST2)) {
char redirect = tos_get_cdc_control_redirect();
if (core_type == CORE_TYPE_MIST) ikbd_poll();
// check for input data on usart
USART_Poll();
unsigned char c = 0;
// check for incoming serial data. this is directly forwarded to the
// arm rs232 and mixes with debug output. Useful for debugging only of
// e.g. the diagnostic cartridge
if(!pl2303_is_blocked()) {
if (core_type == CORE_TYPE_MIST)
spi_uio_cmd_cont(UIO_SERIAL_IN);
else
spi_uio_cmd_cont(UIO_SIO_IN);
while(spi_in() && !pl2303_is_blocked()) {
c = spi_in();
// if a serial/usb adapter is connected it has precesence over
// any other sink
if(pl2303_present())
pl2303_tx_byte(c);
else {
if(c != 0xff)
putchar(c);
// forward to USB if redirection via USB/CDC enabled
if(redirect == CDC_REDIRECT_RS232)
cdc_control_tx(c);
}
}
DisableIO();
}
// check for incoming parallel/midi data
if((redirect == CDC_REDIRECT_PARALLEL) || (redirect == CDC_REDIRECT_MIDI)) {
spi_uio_cmd_cont((redirect == CDC_REDIRECT_PARALLEL)?UIO_PARALLEL_IN:UIO_MIDI_IN);
// character 0xff is returned if FPGA isn't configured
c = 0;
while(spi_in() && (c!= 0xff)) {
c = spi_in();
cdc_control_tx(c);
}
DisableIO();
// always flush when doing midi to reduce latencies
if(redirect == CDC_REDIRECT_MIDI)
cdc_control_flush();
}
}
// poll db9 joysticks
static int joy0_state = JOY0;
if((*AT91C_PIOA_PDSR & JOY0) != joy0_state) {
joy0_state = *AT91C_PIOA_PDSR & JOY0;
unsigned char joy_map = 0;
if(!(joy0_state & JOY0_UP)) joy_map |= JOY_UP;
if(!(joy0_state & JOY0_DOWN)) joy_map |= JOY_DOWN;
if(!(joy0_state & JOY0_LEFT)) joy_map |= JOY_LEFT;
if(!(joy0_state & JOY0_RIGHT)) joy_map |= JOY_RIGHT;
if(!(joy0_state & JOY0_BTN1)) joy_map |= JOY_BTN1;
if(!(joy0_state & JOY0_BTN2)) joy_map |= JOY_BTN2;
joy_map = virtual_joystick_mapping(0x00db, 0x0000, joy_map);
uint8_t idx = joystick_renumber(0);
user_io_joystick(idx, joy_map);
StateJoySet(joy_map, mist_cfg.joystick_db9_fixed_index ? idx : hid_get_joysticks()); // send to OSD
}
static int joy1_state = JOY1;
if((*AT91C_PIOA_PDSR & JOY1) != joy1_state) {
joy1_state = *AT91C_PIOA_PDSR & JOY1;
unsigned char joy_map = 0;
if(!(joy1_state & JOY1_UP)) joy_map |= JOY_UP;
if(!(joy1_state & JOY1_DOWN)) joy_map |= JOY_DOWN;
if(!(joy1_state & JOY1_LEFT)) joy_map |= JOY_LEFT;
if(!(joy1_state & JOY1_RIGHT)) joy_map |= JOY_RIGHT;
if(!(joy1_state & JOY1_BTN1)) joy_map |= JOY_BTN1;
if(!(joy1_state & JOY1_BTN2)) joy_map |= JOY_BTN2;
joy_map = virtual_joystick_mapping(0x00db, 0x0001, joy_map);
uint8_t idx = joystick_renumber(1);
user_io_joystick(idx, joy_map);
StateJoySet(joy_map, mist_cfg.joystick_db9_fixed_index ? idx : hid_get_joysticks() + 1); // send to OSD
}
user_io_send_buttons(0);
// mouse movement emulation is continous
if(emu_mode == EMU_MOUSE) {
if(CheckTimer(emu_timer)) {
emu_timer = GetTimer(EMU_MOUSE_FREQ);
if(emu_state & JOY_MOVE) {
unsigned char b = 0;
char x = 0, y = 0;
if((emu_state & (JOY_LEFT | JOY_RIGHT)) == JOY_LEFT) x = -1;
if((emu_state & (JOY_LEFT | JOY_RIGHT)) == JOY_RIGHT) x = +1;
if((emu_state & (JOY_UP | JOY_DOWN)) == JOY_UP) y = -1;
if((emu_state & (JOY_UP | JOY_DOWN)) == JOY_DOWN) y = +1;
if(emu_state & JOY_BTN1) b |= 1;
if(emu_state & JOY_BTN2) b |= 2;
user_io_mouse(0, b, x, y, 0);
}
}
}
if((core_type == CORE_TYPE_MINIMIG) ||
(core_type == CORE_TYPE_MINIMIG2)) {
kbd_fifo_poll();
// frequently check mouse for events
if(CheckTimer(mouse_timer)) {
mouse_timer = GetTimer(MOUSE_FREQ);
// has ps2 mouse data been updated in the meantime
for (char idx = 0; idx < 2; idx ++) {
if(mouse_flags[idx] & 0x80) {
char x, y, z;
// ----- X axis -------
if(mouse_pos[idx][X] < -128) {
x = -128;
mouse_pos[idx][X] += 128;
} else if(mouse_pos[idx][X] > 127) {
x = 127;
mouse_pos[idx][X] -= 127;
} else {
x = mouse_pos[idx][X];
mouse_pos[idx][X] = 0;