-
Notifications
You must be signed in to change notification settings - Fork 39
/
user_io.c
2727 lines (2354 loc) · 70 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 <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 "pcecd.h"
#include "neocd.h"
#include "psx.h"
#include "hdd.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 "cue_parser.h"
#include "utils.h"
#include "settings.h"
#include "usb/joymapping.h"
#include "usb/joystick.h"
#include "FatFs/diskio.h"
#include "menu.h"
#ifdef HAVE_HDMI
#include "it6613/HDMI_TX.h"
#endif
// up to 16 key can be remapped
#define MAX_REMAP 16
unsigned char key_remap_table[MAX_REMAP][2];
#define BREAK 0x8000
static char umounted; // 1st image is file or direct SD?
static char cache_buffer[1024];
static uint8_t buffer_drive_index = 0;
static uint32_t buffer_lba = 0xffffffff;
extern char s[FF_LFN_BUF + 1];
// 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;
// extra features in the firmware requested by the core
static uint32_t core_features = 0;
// core variant (mostly for arcades)
static char core_mod = 0;
// 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;
static unsigned char modifier = 0, pressed[6] = { 0,0,0,0,0,0 };
static unsigned char ps2_typematic_rate = 0x80;
static unsigned long ps2_typematic_timer;
typedef enum { PS2_KBD_IDLE, PS2_KBD_SCAN_GETSET, PS2_KBD_TYPEMATIC_SET, PS2_KBD_LED_SET } ps2_kbd_state_t;
static ps2_kbd_state_t ps2_kbd_state;
static char ps2_kbd_scan_set = 2;
typedef enum { PS2_MOUSE_IDLE, PS2_MOUSE_SETRESOLUTION, PS2_MOUSE_SETSAMPLERATE } ps2_mouse_state_t;
static ps2_mouse_state_t ps2_mouse_state;
static unsigned char ps2_mouse_status;
static unsigned char ps2_mouse_resolution;
static unsigned char ps2_mouse_samplerate;
// 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;
static char autofire;
static unsigned long autofire_timer;
static uint32_t autofire_map;
static uint32_t autofire_mask;
static char autofire_joy;
// ATA drives
static hardfileTYPE hardfiles[4];
static uint8_t i2c_flags;
#ifdef HAVE_HDMI
static unsigned long hdmi_timer;
static bool hdmi_detected = 0;
static uint8_t hdmi_hiclk = 0;
#define HDMI_FREQ 1000
#endif
#define CONF_TBL_MAX 64
static uint16_t conf_idx[CONF_TBL_MAX];
static int conf_items = 0;
char user_io_osd_is_visible() {
return osd_is_visible;
}
void user_io_reset() {
// no sd card image selected, SD card accesses will go directly
// to the card (first slot, and only until the first unmount)
umounted = 0;
toc.valid = 0;
sd_image[0].valid = 0;
sd_image[1].valid = 0;
sd_image[2].valid = 0;
sd_image[3].valid = 0;
for (int i=0; i<HARDFILES; i++) {
hardfiles[i].enabled = HDF_DISABLED;
hardfiles[i].present = 0;
}
core_mod = 0;
core_features = 0;
ps2_kbd_state = PS2_KBD_IDLE;
ps2_kbd_scan_set = 2;
ps2_mouse_state = PS2_MOUSE_IDLE;
ps2_mouse_status = 0;
ps2_mouse_resolution = 0;
ps2_mouse_samplerate = 0;
ps2_typematic_rate = 0x80;
autofire = 0;
autofire_joy = -1;
conf_items = 0;
conf_idx[0] = 0;
}
void user_io_init() {
user_io_reset();
if(VIDEO_KEEP_VAR != VIDEO_KEEP_VALUE) VIDEO_ALTERED_VAR = 0;
VIDEO_KEEP_VAR = 0;
// mark remap table as unused
memset(key_remap_table, 0, sizeof(key_remap_table));
if(MenuButton()) 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, const char *ext, char flags) {
char *p = 0;
if (flags & CONFIG_VHD) p = arc_get_vhdname();
if (!p || !*p) p = user_io_get_core_name();
if(p[0]) {
if (flags & CONFIG_ROOT) strcpy(s,"/"); else s[0] = 0;
strcat(s, p);
if (ext) {
strcat(s,".");
strcat(s,ext);
}
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]) strncpy(core_name, p, sizeof(core_name));
core_name[sizeof(core_name)-1] = 0;
}
iprintf("Core name from FPGA is \"%s\"\n", core_name);
}
void user_io_set_core_mod(char mod) {
core_mod = mod;
}
static 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 (GetRTC((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();
}
}
uint32_t user_io_get_core_features() {
return core_features;
}
static void user_io_read_core_features() {
core_features = 0;
spi_uio_cmd_cont(UIO_GET_FEATS);
if (spi_in() == 0x80) {
core_features = spi_in();
core_features = (core_features<<8) | spi_in();
core_features = (core_features<<8) | spi_in();
core_features = (core_features<<8) | spi_in();
}
DisableIO();
if (core_features & FEAT_PS2REP) ps2_typematic_rate = 0x08;
}
void user_io_detect_core_type() {
core_name[0] = 0;
EnableIO();
core_type = SPI(0xff);
DisableIO();
#ifdef SD_NO_DIRECT_MODE
rom_direct_upload = 0;
#else
rom_direct_upload = (core_type & 0x10) >> 4; // bit 4 - direct upload support
#endif
core_type &= 0xef;
switch(core_type) {
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();
// get requested features
user_io_read_core_features();
break;
default:
iprintf("Unable to identify core (%x)!\n", core_type);
core_type = CORE_TYPE_UNKNOWN;
}
}
void user_io_init_core() {
if(core_type == CORE_TYPE_8BIT) {
// send a reset
user_io_8bit_set_status(UIO_STATUS_RESET, ~0);
FIL file;
UINT br;
// try to load config
if(!user_io_create_config_name(s, "CFG", CONFIG_ROOT)) {
iprintf("Loading config %s\n", s);
if (f_open(&file, s, FA_READ) == FR_OK) {
iprintf("Found config\n");
if(f_size(&file) <= 8) {
((unsigned long long*)sector_buffer)[0] = 0;
f_read(&file, sector_buffer, f_size(&file), &br);
user_io_8bit_set_status(((unsigned long long*)sector_buffer)[0], ~1);
} else {
settings_load(false);
}
f_close(&file);
} else {
user_io_8bit_set_status(arc_get_default(), ~1);
}
}
// check if there's a <core>.rom or <core>.r0[1-6] present, send it via index 0-6
for (int i = 0; i < 7; i++) {
char ext[4];
if (!i) {
strcpy(ext, "ROM");
} else {
strcpy(ext, "R01");
ext[2] = '0'+i;
}
for (char root = 0; root <= 1; root++) {
if (!user_io_create_config_name(s, ext, root)) {
iprintf("Looking for %s\n", s);
if (f_open(&file, s, FA_READ) == FR_OK) {
data_io_file_tx(&file, i, ext);
f_close(&file);
break;
}
}
}
}
if(!user_io_create_config_name(s, "RAM", CONFIG_ROOT)) {
iprintf("Looking for %s\n", s);
// check if there's a <core>.ram present, send it via index -1
if (f_open(&file, s, FA_READ) == FR_OK) {
data_io_file_tx(&file, -1, "RAM");
f_close(&file);
}
}
for (int i = 0; i < SD_IMAGES; i++) {
hardfile[i] = &hardfiles[i];
if ((core_features & (FEAT_IDE0 << (2*i))) == (FEAT_IDE0_CDROM << (2*i))) {
iprintf("IDE %d: ATAPI CDROM\n", i);
hardfiles[i].enabled = HDF_CDROM;
OpenHardfile(i, false);
}
}
// check if there's a <core>.vhd present
if(!user_io_create_config_name(s, "VHD", CONFIG_ROOT | CONFIG_VHD)) {
iprintf("Looking for %s\n", s);
if (!(core_features & FEAT_IDE0))
user_io_file_mount(s, 0);
if (!user_io_is_mounted(0)) {
// check for <core>.HD0/1 files
if(!user_io_create_config_name(s, "HD0", CONFIG_ROOT | CONFIG_VHD)) {
for (int i = 0; i < SD_IMAGES; i++) {
s[strlen(s)-1] = '0'+i;
iprintf("Looking for %s\n", s);
if ((core_features & (FEAT_IDE0 << (2*i))) == (FEAT_IDE0_ATA << (2*i))) {
iprintf("IDE %d: ATA Hard Disk\n", i);
hardfiles[i].enabled = HDF_FILE;
strncpy(hardfiles[i].name, s, sizeof(hardfiles[0].name));
hardfiles[i].name[sizeof(hardfiles[0].name)-1] = 0;
OpenHardfile(i, false);
} else {
user_io_file_mount(s, i);
}
}
}
}
}
if (core_features & FEAT_IDE_MASK)
SendHDFCfg();
// release reset
user_io_8bit_set_status(0, UIO_STATUS_RESET);
}
#ifdef HAVE_HDMI
hdmi_detected = false;
hdmi_hiclk = 0;
if ((core_type == CORE_TYPE_8BIT && core_features & FEAT_HDMI) || core_type == CORE_TYPE_MIST2 || core_type == CORE_TYPE_MINIMIG2 || core_type == CORE_TYPE_ARCHIE) {
hdmi_detected = HDMITX_Init();
if (hdmi_detected) HDMITX_ChangeVideoTiming(1);
}
#endif
}
static 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];
}
static 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 (ps2_kbd_scan_set == 1) ? usb2ps2_set1[k] : usb2ps2[k];
}
void user_io_analog_joystick(unsigned char joystick, char valueX, char valueY, char valueX2, char valueY2) {
if(osd_is_visible) return;
if(core_type == CORE_TYPE_8BIT || core_type == CORE_TYPE_MINIMIG2) {
int16_t valueXX = valueX*mist_cfg.joystick_analog_mult/128 + mist_cfg.joystick_analog_offset;
int16_t valueYY = valueY*mist_cfg.joystick_analog_mult/128 + mist_cfg.joystick_analog_offset;
int16_t valueXX2 = valueX2*mist_cfg.joystick_analog_mult/128 + mist_cfg.joystick_analog_offset;
int16_t valueYY2 = valueY2*mist_cfg.joystick_analog_mult/128 + mist_cfg.joystick_analog_offset;
//iprintf("analog: x=%d, y=%d, xx=%d, yy=%d, mult=%d, offs=%d\n", valueX, valueY, valueXX, valueYY, mist_cfg.joystick_analog_mult, mist_cfg.joystick_analog_offset);
spi_uio_cmd8_cont(UIO_ASTICK, joystick);
spi8(valueXX);
spi8(valueYY);
spi8(valueXX2);
spi8(valueYY2);
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 && map)
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, uint32_t map) {
// "only" 6 joysticks are supported
if(joystick > 5) return;
if(osd_is_visible && map) return;
//iprintf("ext j%d: %x\n", joystick, map);
spi_uio_cmd32(UIO_JOYSTICK0_EXT + joystick, 0x000fffff & map);
if (autofire && (map & 0x30)) {
autofire_mask = map & 0x30;
autofire_map = (autofire_map & autofire_mask) | (map & ~autofire_mask);
if (autofire_joy != joystick) {
autofire_joy = joystick;
autofire_timer = GetTimer(autofire*50);
}
} else {
autofire_joy = -1;
}
}
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, uint16_t 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),
0 ,0);
}
// 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
if (fat_uses_mmc()) {
MMC_GetCID(data);
MMC_GetCSD(data+16);
// byte 32 is a generic config byte
data[32] = MMC_IsSDHC()?1:0;
} else {
// synthetic CSD for non-MMC storage
uint32_t capacity;
disk_ioctl(fs.pdrv, GET_SECTOR_COUNT, &capacity);
memset(data, sizeof(data), 0);
data[16+0] = 0x40;
data[16+1] = 0x0e;
data[16+3] = 0x32;
data[16+4] = 0x5b;
data[16+5] = 0x59;
data[16+6] = 0x90;
data[16+7] = (capacity >> 26) & 0xff;
data[16+8] = (capacity >> 18) & 0xff;
data[16+9] = (capacity >> 10) & 0xff;
data[16+10] = 0x5f;
data[16+11] = 0xc0;
data[32] = 1; // SDHC
}
// and forward it to the FPGA
spi_uio_cmd_cont(UIO_SET_SDCONF);
spi_write(data, sizeof(data));
DisableIO();
// hexdump(data, sizeof(data), 0);
}
void user_io_sd_ack(char drive_index) {
spi_uio_cmd_cont(UIO_SD_ACK);
spi8(drive_index);
DisableIO();
}
// read 8+32 bit sd card status word from FPGA
uint8_t user_io_sd_get_status(uint32_t *lba, uint8_t *drive_index, uint8_t *blksz) {
uint32_t s;
uint8_t c;
*drive_index = 0;
*blksz = 0;
spi_uio_cmd_cont(UIO_GET_SDSTAT);
c = spi_in();
if ((c & 0xf0) == 0x60) {
uint8_t tmp = spi_in();
*drive_index = tmp & 0x03;
*blksz = (tmp >> 4) & 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
static 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);
while(len--) SPI(*s++);
//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 = joystick_count();
// 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 = mist_cfg.joystick_disable_swap? 1 : 0;
else j = usb_sticks;
}
return j;
}
static 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 running?
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);
}
char user_io_is_cue_mounted() {
return toc.valid;
}
char user_io_cue_mount(const unsigned char *name, unsigned char index) {
char res = CUE_RES_OK;
toc.valid = 0;
if (name) {
res = cue_parse(name, &sd_image[index]);
}
#ifdef HAVE_PSX
if (core_features & FEAT_PSX) psx_mount_cd(name);
#endif
// 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(toc.valid ? f_size(&toc.file->file) : 0);
spi32le(toc.valid ? f_size(&toc.file->file) >> 32 : 0);
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, 1);
return res;
}
static inline char sd_index(unsigned char index) {
unsigned char retval = index;
if (core_type == CORE_TYPE_ARCHIE)
return (index + 2);
else
return index;
}
char user_io_is_mounted(unsigned char index) {
return sd_image[sd_index(index)].valid;
}
void user_io_file_mount(const unsigned char *name, unsigned char index) {
FRESULT res;
buffer_lba = 0xffffffff; // invalidate cache
if (name) {
if (sd_image[sd_index(index)].valid)
f_close(&sd_image[sd_index(index)].file);
res = IDXOpen(&sd_image[sd_index(index)], name, FA_READ | FA_WRITE);
if (res != FR_OK) res = IDXOpen(&sd_image[sd_index(index)], name, FA_READ);
if (res == FR_OK) {
iprintf("selected %llu bytes to slot %d\n", f_size(&sd_image[sd_index(index)].file), index);
sd_image[sd_index(index)].valid = 1;
// build index for fast random access
IDXIndex(&sd_image[sd_index(index)]);
} else {
iprintf("error mounting %s (%d)\n", name, res);
return;
}
} else {
iprintf("unmounting file in slot %d\n", index);
if (sd_image[sd_index(index)].valid) f_close(&sd_image[sd_index(index)].file);
sd_image[sd_index(index)].valid = 0;
if (!index) umounted = 1;
}
// 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(sd_image[sd_index(index)].valid ? f_size(&sd_image[sd_index(index)].file) : 0);
spi32le(sd_image[sd_index(index)].valid ? f_size(&sd_image[sd_index(index)].file) >> 32: 0);
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(unsigned 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
uint16_t start_chr;
// clear buffer
buffer[0] = 0;
// use the config index table to get where to start
// conf_idx stores the starting position of every 4th item
// if the index is in a DIP setting, it has 0
uint16_t pos = 0, lastpos = 0;
i = index>>2;
while (i > 0 && (i > conf_items || conf_idx[i] == 0)) i--;
pos = lastpos = conf_idx[i];
lidx = i<<2;
//iprintf("index=%d cached pos=%d lidx=%d\n", index, pos, lidx);
spi_uio_cmd_cont(UIO_GET_STR_EXT);
i = SPI(pos & 0xff);
if (i == 0xaa) {
SPI(pos >> 8);
i = spi_in(); // dummy byte to prepare to apply the offset in the core
i = spi_in();
} else {
DisableIO();
lidx = 0;
pos = lastpos = 0;
//iprintf("UIO_GET_STRING_EXT not supported\n");
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((lidx & 0x03) == 0 && (lidx >> 2) < CONF_TBL_MAX) {
conf_idx[lidx >> 2] = arc ? 0 : lastpos;
if (conf_items < (lidx >> 2)) conf_items = (lidx >> 2);
}
lastpos = pos+1;
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;
break;
}
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();
pos++;
}
}
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;