-
Notifications
You must be signed in to change notification settings - Fork 198
/
main.c
3269 lines (2844 loc) · 95.7 KB
/
main.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
/*
* main.c
*
* System initialisation and navigation main loop.
*
* Written & released by Keir Fraser <[email protected]>
*
* This is free and unencumbered software released into the public domain.
* See the file COPYING for more details, or visit <http://unlicense.org>.
*/
int EXC_reset(void) __attribute__((alias("main")));
static const char image_a[] = "IMAGE_A.CFG";
static const char init_image_a[] = "INIT_A.CFG";
static FATFS fatfs;
static struct {
FIL file;
DIR dp;
FILINFO fp;
char buf[512];
} *fs;
struct native_dirent {
uint32_t dir_sect;
uint16_t dir_off;
uint8_t attr;
char name[0];
};
static struct {
uint16_t slot_nr, max_slot_nr;
uint8_t slot_map[1000/8];
struct short_slot autoboot;
struct short_slot hxcsdfe;
struct short_slot imgcfg;
struct slot slot, clipboard;
uint32_t cfg_cdir, cur_cdir;
struct native_dirent **sorted;
struct {
uint32_t cdir;
uint16_t slot;
} stack[20];
uint8_t depth;
bool_t usb_power_fault;
uint8_t dirty_slot_nr:1;
uint8_t dirty_slot_name:1;
uint8_t hxc_mode:1;
uint8_t ejected:1;
uint8_t ima_ej_flag:1; /* "\\EJ" flag in IMAGE_A.CFG? */
/* FF.CFG values which override HXCSDFE.CFG. */
uint8_t ffcfg_has_step_volume:1;
uint8_t ffcfg_has_display_off_secs:1;
uint8_t ffcfg_has_display_scroll_rate:1;
} cfg;
/* If TRUE, reset to start of filename when selecting a new image.
* If FALSE, try to maintain scroll offset when browsing through images. */
#define cfg_scroll_reset TRUE
uint8_t board_id;
#define BUTTON_SCAN_HZ 500
#define BUTTON_SCAN_MS (1000/BUTTON_SCAN_HZ)
static uint32_t display_ticks;
static uint8_t display_state;
enum { BACKLIGHT_OFF, BACKLIGHT_SWITCHING_ON, BACKLIGHT_ON };
enum { LED_NORMAL, LED_TRACK, LED_TRACK_QUIESCENT,
LED_BUTTON_HELD, LED_BUTTON_RELEASED };
static void native_get_slot_map(bool_t sorted_only);
/* Hack inside the guts of FatFS. */
void flashfloppy_fill_fileinfo(FIL *fp);
#ifdef LOGFILE
/* Logfile must be written to config dir. */
#define logfile_flush(_file) do { \
fatfs.cdir = cfg.cfg_cdir; \
logfile_flush(_file); \
fatfs.cdir = cfg.cur_cdir; \
} while(0)
#endif
bool_t lba_within_fat_volume(uint32_t lba)
{
/* Also disallows access to the boot/bpb sector of the mounted volume. */
return (lba > fatfs.volbase) && (lba <= fatfs.volend);
}
static bool_t slot_valid(unsigned int i)
{
if (i > cfg.max_slot_nr)
return FALSE;
if (!cfg.hxc_mode)
return TRUE;
if (i >= (sizeof(cfg.slot_map)*8))
return FALSE;
return !!(cfg.slot_map[i/8] & (0x80>>(i&7)));
}
uint16_t get_slot_nr(void)
{
return cfg.slot_nr;
}
bool_t set_slot_nr(uint16_t slot_nr)
{
if (!slot_valid(slot_nr))
return FALSE;
cfg.slot_nr = slot_nr;
cfg.dirty_slot_nr = TRUE;
return TRUE;
}
void set_slot_name(const char *name)
{
snprintf(cfg.slot.name, sizeof(cfg.slot.name), "%s", name);
cfg.dirty_slot_name = TRUE;
}
/* Turn the LCD backlight on, reset the switch-off handler and ticker. */
static void lcd_on(void)
{
if (display_type != DT_LCD_OLED)
return;
display_ticks = 0;
barrier();
display_state = BACKLIGHT_ON;
barrier();
lcd_backlight(ff_cfg.display_off_secs != 0);
}
static bool_t slot_type(const char *str)
{
char ext[8];
filename_extension(cfg.slot.name, ext, sizeof(ext));
if (!strcmp(ext, str))
return TRUE;
return !strcmp(cfg.slot.type, str);
}
#define wp_column ((lcd_columns > 16) ? 8 : 7)
/* Scroll long filename. */
static struct {
uint16_t off, end, pause, rate;
int32_t ticks;
} lcd_scroll;
static void lcd_scroll_init(uint16_t pause, uint16_t rate)
{
int diff = lcd_scroll.off - lcd_scroll.end;
lcd_scroll.pause = pause;
lcd_scroll.rate = rate;
lcd_scroll.end = max_t(
int, strnlen(cfg.slot.name, sizeof(cfg.slot.name)) - lcd_columns, 0);
if (lcd_scroll.end && !lcd_scroll.pause)
lcd_scroll.end += lcd_columns;
if (lcd_scroll.off > lcd_scroll.end)
lcd_scroll.off = lcd_scroll.pause || !lcd_scroll.end
? 0 : lcd_scroll.end + diff;
}
static void lcd_scroll_name(void)
{
static struct track_info ti;
char msg[lcd_columns+1];
if ((lcd_scroll.ticks > 0) || (lcd_scroll.end == 0))
return;
floppy_get_track(&ti);
if (ti.in_da_mode) {
/* Display controlled by src/image/da.c */
return;
}
lcd_scroll.ticks = time_ms(lcd_scroll.rate);
if (lcd_scroll.pause != 0) {
if (++lcd_scroll.off > lcd_scroll.end)
lcd_scroll.off = 0;
snprintf(msg, sizeof(msg), "%s", cfg.slot.name + lcd_scroll.off);
if ((lcd_scroll.off == 0)
|| (lcd_scroll.off == lcd_scroll.end))
lcd_scroll.ticks = time_ms(lcd_scroll.pause);
} else {
const unsigned int scroll_gap = 4;
lcd_scroll.off++;
if (lcd_scroll.off <= lcd_scroll.end) {
snprintf(msg, sizeof(msg), "%s%*s%s",
cfg.slot.name + lcd_scroll.off,
scroll_gap, "", cfg.slot.name);
} else {
snprintf(msg, sizeof(msg), "%*s%s",
scroll_gap - (lcd_scroll.off - lcd_scroll.end), "",
cfg.slot.name);
if ((lcd_scroll.off - lcd_scroll.end) == scroll_gap)
lcd_scroll.off = 0;
}
}
lcd_write(0, 0, -1, msg);
}
/* Write slot info to display. */
static void display_write_slot(bool_t nav_mode)
{
const struct image_type *type;
char msg[lcd_columns+1], typename[4] = "";
unsigned int i;
if (display_type != DT_LCD_OLED) {
if (display_type == DT_LED_7SEG)
led_7seg_write_decimal(cfg.slot_nr);
return;
}
if (nav_mode && !cfg_scroll_reset) {
lcd_scroll_init(0, ff_cfg.nav_scroll_rate);
if (lcd_scroll.end == 0) {
snprintf(msg, sizeof(msg), "%s", cfg.slot.name);
lcd_write(0, 0, -1, msg);
} else {
lcd_scroll.off--;
lcd_scroll.ticks = 0;
lcd_scroll_name();
}
} else {
snprintf(msg, sizeof(msg), "%s", cfg.slot.name);
lcd_write(0, 0, -1, msg);
}
if (slot_type("v9t9")) {
snprintf(typename, sizeof(typename), "T99");
} else if (!(cfg.slot.attributes & AM_DIR)) {
for (type = &image_type[0]; type->handler != NULL; type++)
if (slot_type(type->ext))
break;
if (type->handler != NULL) {
snprintf(typename, sizeof(typename), "%s", type->ext);
for (i = 0; i < sizeof(typename); i++)
typename[i] = toupper(typename[i]);
}
}
snprintf(msg, sizeof(msg), "%03u/%03u%*s%3s D:%u",
cfg.slot_nr, cfg.max_slot_nr,
(lcd_columns > 16) ? 3 : 1, "",
typename, cfg.depth);
if (cfg.hxc_mode) {
/* HxC mode: Exclude depth from the info message. */
char *p = strrchr(msg, 'D');
if (p)
*p = '\0';
}
lcd_write(0, 1, -1, msg);
lcd_on();
}
/* Write track number to LCD. */
static void lcd_write_track_info(bool_t force)
{
static struct track_info lcd_ti;
struct track_info ti;
char msg[17];
if (display_type != DT_LCD_OLED)
return;
floppy_get_track(&ti);
if (lcd_columns <= 16)
ti.cyl = min_t(uint8_t, ti.cyl, 99);
ASSERT(ti.side <= 1);
if (force || (ti.cyl != lcd_ti.cyl)
|| ((ti.side != lcd_ti.side) && ti.sel)
|| (ti.writing != lcd_ti.writing)) {
snprintf(msg, sizeof(msg), "%c T:%02u.%u",
(cfg.slot.attributes & AM_RDO) ? '*' : ti.writing ? 'W' : ' ',
ti.cyl, ti.side);
lcd_write(wp_column, 1, -1, msg);
if (ff_cfg.display_on_activity != DISPON_no)
lcd_on();
lcd_ti = ti;
} else if ((ff_cfg.display_on_activity == DISPON_sel) && ti.sel) {
lcd_on();
}
}
static void led_7seg_update_track(bool_t force)
{
static struct track_info led_ti;
static bool_t showing_track;
static uint8_t active_countdown;
bool_t changed;
struct track_info ti;
char msg[4];
if (display_type != DT_LED_7SEG)
return;
floppy_get_track(&ti);
changed = (ti.cyl != led_ti.cyl) || ((ti.side != led_ti.side) && ti.sel)
|| (ti.writing != led_ti.writing);
if (force) {
/* First call afer mounting new image: forcibly show track nr. */
display_state = LED_TRACK;
showing_track = FALSE;
changed = TRUE;
}
if (ti.in_da_mode) {
/* Display controlled by src/image/da.c */
display_state = LED_NORMAL;
}
if (changed) {
/* We will show new track nr unless overridden by a button press. */
if (display_state == LED_TRACK_QUIESCENT)
display_state = LED_TRACK;
active_countdown = 50*4;
led_ti = ti;
} else if (active_countdown != 0) {
/* Count down towards reverting to showing image nr. */
active_countdown--;
}
if ((display_state != LED_TRACK) || (active_countdown == 0)) {
if (showing_track)
display_write_slot(FALSE);
showing_track = FALSE;
active_countdown = 0;
if (display_state == LED_TRACK)
display_state = LED_TRACK_QUIESCENT;
return;
}
if (!showing_track || changed) {
const static char status[] = { 'k', 'm', 'v', 'w' };
snprintf(msg, sizeof(msg), "%2u%c", ti.cyl,
status[ti.side|(ti.writing<<1)]);
led_7seg_write_string(msg);
showing_track = TRUE;
}
}
/* Handle switching the LCD backlight. */
static uint8_t lcd_handle_backlight(uint8_t b)
{
if ((ff_cfg.display_off_secs == 0)
|| (ff_cfg.display_off_secs == 0xff))
return b;
switch (display_state) {
case BACKLIGHT_OFF:
if (!b)
break;
/* First button press turns on the backlight. Nothing more. */
b = 0;
display_state = BACKLIGHT_SWITCHING_ON;
lcd_backlight(TRUE);
break;
case BACKLIGHT_SWITCHING_ON:
/* We sit in this state until the button is released. */
if (!b)
display_state = BACKLIGHT_ON;
b = 0;
display_ticks = 0;
break;
case BACKLIGHT_ON:
/* After a period with no button activity we turn the backlight off. */
if (b)
display_ticks = 0;
if (display_ticks++ >= BUTTON_SCAN_HZ*ff_cfg.display_off_secs) {
lcd_backlight(FALSE);
display_state = BACKLIGHT_OFF;
}
break;
}
return b;
}
static uint8_t led_handle_display(uint8_t b)
{
switch (display_state) {
case LED_TRACK:
if (!b)
break;
/* First button press switches to image number. Nothing more. */
b = 0;
display_state = LED_BUTTON_HELD;
break;
case LED_BUTTON_HELD:
/* We sit in this state until the button is released. */
if (!b)
display_state = LED_BUTTON_RELEASED;
b = 0;
display_ticks = 0;
break;
case LED_BUTTON_RELEASED:
/* After a period with no button activity we return to track number. */
if (display_ticks++ >= BUTTON_SCAN_HZ*3)
display_state = LED_TRACK;
break;
}
return b;
}
static uint8_t v2_read_rotary(uint8_t rotary)
{
/* Rotary encoder outputs a Gray code, counting clockwise: 00-01-11-10. */
const uint32_t rotary_transitions[] = {
[ROT_none] = 0x00000000, /* No encoder */
[ROT_full] = 0x20000100, /* 4 transitions (full cycle) per detent */
[ROT_half] = 0x24000018, /* 2 transitions (half cycle) per detent */
[ROT_quarter] = 0x24428118 /* 1 transition (quarter cyc) per detent */
};
return (rotary_transitions[ff_cfg.rotary & 3] >> (rotary << 1)) & 3;
}
static uint8_t read_rotary(uint8_t rotary)
{
/* Rotary encoder outputs a Gray code, counting clockwise: 00-01-11-10. */
const uint32_t rotary_transitions = 0x24428118;
/* Number of back-to-back transitions we see per detent on various
* types of rotary encoder. */
const uint8_t rotary_transitions_per_detent[] = {
[ROT_full] = 4, [ROT_half] = 2, [ROT_quarter] = 1
};
/* p_t(x) returns the previous valid transition in same direction.
* eg. p_t(0b0111) == 0b0001 */
#define p_t(x) (((x)>>2)|((((x)^3)&3)<<2))
/* Bitmask of which valid 4-bit transition codes we have seen in each
* direction (CW and CCW). */
static uint16_t t_seen[2];
uint16_t ts;
uint8_t rb;
/* Check if we have seen a valid CW or CCW state transition. */
rb = (rotary_transitions >> (rotary << 1)) & 3;
if (likely(!rb))
return 0; /* Nope */
/* Have we seen the /previous/ transition in this direction? If not, any
* previously-recorded transitions are not in a contiguous step-wise
* sequence, and should be discarded as switch bounce. */
ts = t_seen[rb-1];
if (!(ts & (1<<p_t(rotary))))
ts = 0; /* Clear any existing bounce transitions. */
/* Record this transition and check if we have seen enough to get
* us from one detent to another. */
ts |= (1<<rotary);
if ((popcount(ts) < rotary_transitions_per_detent[ff_cfg.rotary & 3])
|| (((ff_cfg.rotary & 3) == ROT_full) && ((rotary & 3) != 3))) {
/* Not enough transitions yet: Remember where we are for next time. */
t_seen[rb-1] = ts;
return 0;
}
/* This is a valid movement between detents. Clear transition state
* and return the movement to the caller. */
t_seen[0] = t_seen[1] = 0;
return rb;
#undef p_t
}
static struct timer button_timer;
static volatile uint8_t buttons, velocity;
static uint8_t rotary, rb;
void IRQ_rotary(void)
{
if ((ff_cfg.rotary & ROT_typemask) != ROT_full)
return;
rotary = ((rotary << 2) | board_get_rotary()) & 15;
rb = read_rotary(rotary) ?: rb;
}
static void set_rotary_exti(void)
{
exti->imr &= ~board_rotary_exti_mask;
board_rotary_exti_mask = 0;
if ((ff_cfg.rotary & ROT_typemask) == ROT_full)
board_setup_rotary_exti();
}
static void button_timer_fn(void *unused)
{
const uint8_t rotary_reverse[4] = {
[B_LEFT] = B_RIGHT, [B_RIGHT] = B_LEFT
};
static uint16_t cur_time, prev_time;
static uint32_t _b[3]; /* 0 = left, 1 = right, 2 = select */
uint8_t x, b = osd_buttons_rx;
bool_t twobutton_rotary =
(ff_cfg.twobutton_action & TWOBUTTON_mask) == TWOBUTTON_rotary;
int i, twobutton_reverse = !!(ff_cfg.twobutton_action & TWOBUTTON_reverse);
cur_time++;
if ((uint16_t)(cur_time - prev_time) > 0x7fff)
prev_time = cur_time - 0x7fff;
velocity = 0;
/* Check PA5 (USBFLT, active low). */
if (gotek_enhanced() && !gpio_read_pin(gpioa, 5)) {
/* Latch the error and disable USBENA. */
cfg.usb_power_fault = TRUE;
gpio_write_pin(gpioa, 4, HIGH);
}
/* We debounce the switches by waiting for them to be pressed continuously
* for 32 consecutive sample periods (32 * 2ms == 64ms) */
x = ~board_get_buttons();
for (i = 0; i < 3; i++) {
_b[i] <<= 1;
_b[i] |= x & 1;
x >>= 1;
}
if (_b[twobutton_reverse] == 0)
b |= twobutton_rotary ? B_LEFT|B_RIGHT : B_LEFT;
if (_b[!twobutton_reverse] == 0)
b |= twobutton_rotary ? B_SELECT : B_RIGHT;
if (_b[2] == 0)
b |= B_SELECT;
rotary = ((rotary << 2) | board_get_rotary()) & 15;
switch (ff_cfg.rotary & ROT_typemask) {
case ROT_trackball: {
static uint16_t count, thresh, dir;
rb = rotary_reverse[(rotary ^ (rotary >> 2)) & 3];
if (rb == 0) {
/* Idle: Increase threshold, decay the counter. */
thresh = min_t(int, thresh + BUTTON_SCAN_MS, 360);
count = max_t(int, count - BUTTON_SCAN_MS, 0);
} else if (rb != dir) {
/* Change of direction: Put the brakes on. */
dir = rb;
count = rb = 0;
thresh = 360;
} else {
/* Step in same direction: Increase count, decay the threshold. */
count += 160;
thresh = max_t(int, 0, thresh - 40);
if (count >= thresh) {
/* Count exceeds threshold: register a press. */
count = 0;
} else {
/* Don't register a press yet. */
rb = 0;
}
}
break;
}
case ROT_buttons:
rb = rotary_reverse[rotary & 3];
break;
case ROT_none:
break;
default: /* rotary encoder */ {
rb = (ff_cfg.rotary & ROT_v2) ? v2_read_rotary(rotary)
: (read_rotary(rotary) ?: rb);
if (rb) {
uint16_t delta = cur_time - prev_time;
velocity = (BUTTON_SCAN_HZ/10)/(delta?:1);
velocity = range_t(int, velocity, 0, 20);
prev_time = cur_time;
}
break;
}
}
if (ff_cfg.rotary & ROT_reverse)
rb = rotary_reverse[rb];
b |= rb;
rb = 0;
switch (display_type) {
case DT_LCD_OLED:
b = lcd_handle_backlight(b);
break;
case DT_LED_7SEG:
b = led_handle_display(b);
break;
}
/* Latch final button state and reset the timer. */
buttons = b;
timer_set(&button_timer, button_timer.deadline + time_ms(BUTTON_SCAN_MS));
}
static void canary_init(void)
{
_irq_stackbottom[0] = _thread_stackbottom[0] = 0xdeadbeef;
}
static void canary_check(void)
{
ASSERT(_irq_stackbottom[0] == 0xdeadbeef);
ASSERT(_thread_stackbottom[0] == 0xdeadbeef);
}
static void fix_hxc_short_slot(struct short_slot *short_slot)
{
char *dot;
/* Get rid of trailing file extension. */
short_slot->name[51] = '\0';
if (((dot = strrchr(short_slot->name, '.')) != NULL)
&& !strcmp(dot+1, short_slot->type))
*dot = '\0';
}
static void slot_from_short_slot(
struct slot *slot, const struct short_slot *short_slot)
{
memcpy(slot->name, short_slot->name, sizeof(short_slot->name));
slot->name[sizeof(short_slot->name)] = '\0';
memcpy(slot->type, short_slot->type, sizeof(short_slot->type));
slot->type[sizeof(short_slot->type)] = '\0';
slot->attributes = short_slot->attributes;
slot->firstCluster = short_slot->firstCluster;
slot->size = short_slot->size;
slot->dir_sect = slot->dir_ptr = 0;
}
static void fatfs_to_short_slot(
struct short_slot *slot, FIL *file, const char *name)
{
char *dot;
unsigned int i;
slot->attributes = file->obj.attr;
slot->firstCluster = file->obj.sclust;
slot->size = file->obj.objsize;
snprintf(slot->name, sizeof(slot->name), "%s", name);
if ((dot = strrchr(slot->name, '.')) != NULL) {
memcpy(slot->type, dot+1, sizeof(slot->type));
for (i = 0; i < sizeof(slot->type); i++)
slot->type[i] = tolower(slot->type[i]);
*dot = '\0';
} else {
memset(slot->type, 0, sizeof(slot->type));
}
}
void fatfs_from_slot(FIL *file, const struct slot *slot, BYTE mode)
{
memset(file, 0, sizeof(*file));
file->obj.fs = &fatfs;
file->obj.id = fatfs.id;
file->obj.attr = slot->attributes;
file->obj.sclust = slot->firstCluster;
file->obj.objsize = slot->size;
file->flag = mode;
file->dir_sect = slot->dir_sect;
file->dir_ptr = (void *)slot->dir_ptr;
}
static void fatfs_to_slot(struct slot *slot, FIL *file, const char *name)
{
char *dot;
unsigned int i;
slot->attributes = file->obj.attr;
slot->firstCluster = file->obj.sclust;
slot->size = file->obj.objsize;
slot->dir_sect = file->dir_sect;
slot->dir_ptr = (uint32_t)file->dir_ptr;
snprintf(slot->name, sizeof(slot->name), "%s", name);
if ((dot = strrchr(slot->name, '.')) != NULL) {
snprintf(slot->type, sizeof(slot->type), "%s", dot+1);
for (i = 0; i < sizeof(slot->type); i++)
slot->type[i] = tolower(slot->type[i]);
*dot = '\0';
} else {
memset(slot->type, 0, sizeof(slot->type));
}
}
bool_t get_img_cfg(struct slot *slot)
{
if (!cfg.imgcfg.size)
return FALSE;
slot_from_short_slot(slot, &cfg.imgcfg);
return TRUE;
}
static void dump_file(void)
{
F_lseek(&fs->file, 0);
#ifndef NDEBUG
printk("[");
do {
F_read(&fs->file, fs->buf, sizeof(fs->buf), NULL);
printk("%s", fs->buf);
} while (!f_eof(&fs->file));
printk("]\n");
F_lseek(&fs->file, 0);
#endif
}
static bool_t native_dir_next(void)
{
for (;;) {
F_readdir(&fs->dp, &fs->fp);
if (fs->fp.fname[0] == '\0')
return FALSE;
/* Skip dot files. */
if (fs->fp.fname[0] == '.')
continue;
/* Skip hidden files/folders. */
if (fs->fp.fattrib & AM_HID)
continue;
/* Allow folder navigation when LCD/OLED display is attached. */
if ((fs->fp.fattrib & AM_DIR) && (display_type == DT_LCD_OLED)
/* Skip FF/ in root folder */
&& ((cfg.depth != 0) || strcmp(fs->fp.fname, "FF"))
/* Skip __MACOSX/ zip-file resource-fork folder */
&& strcmp(fs->fp.fname, "__MACOSX"))
break;
/* Allow valid image files. */
if (image_valid(&fs->fp))
break;
}
return TRUE;
}
static inline int __tolower(int c)
{
if ((c >= 'A') && (c <= 'Z'))
c += 'a' - 'A';
return c;
}
int strcmp_lower(const char *s1, const char *s2)
{
for (;;) {
int diff = __tolower(*s1) - __tolower(*s2);
if (diff || !*s1)
return diff;
s1++; s2++;
}
return 0;
}
static int native_dir_cmp(const void *a, const void *b)
{
const struct native_dirent *da = a;
const struct native_dirent *db = b;
if ((da->attr ^ db->attr) & AM_DIR) {
switch (ff_cfg.sort_priority) {
case SORTPRI_folders:
return (da->attr & AM_DIR) ? -1 : 1;
case SORTPRI_files:
return (da->attr & AM_DIR) ? 1 : -1;
}
}
return strcmp_lower(da->name, db->name);
}
/* Returns -1 if not read & sorted. */
static int native_read_and_sort_dir(void)
{
struct native_dirent **p_ent;
struct native_dirent *ent = arena_alloc(0);
char *start = arena_alloc(0);
char *end = start + arena_avail();
int nr;
if (ff_cfg.folder_sort == SORT_never)
return -1;
volume_cache_destroy();
F_opendir(&fs->dp, "");
p_ent = (struct native_dirent **)end;
while (((char *)p_ent - (char *)ent)
> (FF_MAX_LFN + 1 + sizeof(*ent) + sizeof(ent))) {
if (!native_dir_next())
goto complete;
*--p_ent = ent;
ASSERT((unsigned int)(fs->fp.dir_ptr - fatfs.win) < 512u);
ent->dir_sect = fs->fp.dir_sect;
ent->dir_off = fs->fp.dir_ptr - fatfs.win;
ent->attr = fs->fp.fattrib;
strcpy(ent->name, fs->fp.fname);
ent = (struct native_dirent *)(
((uint32_t)ent + sizeof(*ent) + strlen(ent->name) + 1 + 3) & ~3);
}
if (ff_cfg.folder_sort == SORT_always)
goto complete;
volume_cache_init(start, end);
cfg.sorted = NULL;
return -1;
complete:
nr = (struct native_dirent **)end - p_ent;
qsort_p(p_ent, nr, native_dir_cmp);
F_closedir(&fs->dp);
volume_cache_init(ent, p_ent);
cfg.sorted = p_ent;
return nr;
}
static void update_slot_by_name(void)
{
const char *name = cfg.slot.name;
int len = strnlen(name, 256);
int nr = ~0, max;
if (cfg.sorted) {
max = cfg.max_slot_nr;
if (cfg.depth)
max--;
for (nr = 0; nr <= max; nr++)
if (!strncmp(cfg.sorted[nr]->name, name, len))
break;
if (cfg.depth)
nr++;
}
else if (!cfg.hxc_mode) {
nr = cfg.depth ? 1 : 0;
F_opendir(&fs->dp, "");
while (native_dir_next() && strncmp(fs->fp.fname, name, len))
nr++;
F_closedir(&fs->dp);
} else if (ff_cfg.nav_mode != NAVMODE_indexed) {
struct _hxc{
struct hxcsdfe_cfg cfg;
struct v1_slot v1_slot;
struct v2_slot v2_slot;
} *hxc = (struct _hxc *)fs->buf;
struct slot *slot = (struct slot *)hxc;
slot_from_short_slot(slot, &cfg.hxcsdfe);
fatfs_from_slot(&fs->file, slot, FA_READ);
F_read(&fs->file, &hxc->cfg, sizeof(hxc->cfg), NULL);
if (hxc->cfg.index_mode)
goto out;
for (nr = 1; nr <= cfg.max_slot_nr; nr++) {
if (!slot_valid(nr))
continue;
switch (hxc->cfg.signature[9]-'0') {
case 1:
F_lseek(&fs->file, 1024 + nr*128);
F_read(&fs->file, &hxc->v1_slot, sizeof(hxc->v1_slot), NULL);
memcpy(&hxc->v2_slot.type, &hxc->v1_slot.name[8], 3);
memcpy(&hxc->v2_slot.attributes, &hxc->v1_slot.attributes,
1+4+4+17);
hxc->v2_slot.name[17] = '\0';
break;
case 2:
F_lseek(&fs->file, hxc->cfg.slots_position*512
+ nr*64*hxc->cfg.number_of_drive_per_slot);
F_read(&fs->file, &hxc->v2_slot, sizeof(hxc->v2_slot), NULL);
break;
}
fix_hxc_short_slot(&hxc->v2_slot);
if (!strncmp(hxc->v2_slot.name, name,
min_t(int, len, sizeof(hxc->v2_slot.name))))
break;
}
}
set_slot_nr(nr);
out:
cfg.dirty_slot_nr = TRUE;
}
/* Parse pinNN= config value. */
static uint8_t parse_pin_str(const char *s)
{
uint8_t pin = 0;
if (*s == 'n') {
pin = PIN_invert;
s++;
}
pin ^= !strcmp(s, "low") ? PIN_low
: !strcmp(s, "high") ? PIN_high
: !strcmp(s, "c") ? (PIN_invert | PIN_nc)
: !strcmp(s, "rdy") ? PIN_rdy
: !strcmp(s, "dens") ? PIN_dens
: !strcmp(s, "chg") ? PIN_chg
: PIN_auto;
return pin;
}
static uint16_t parse_display_order(const char *p)
{
int sh = 0;
uint16_t order = 0;
if (!strcmp(p, "default"))
return DORD_default;
while (p != NULL) {
order |= ((p[0]-'0')&7) << sh;
if (p[1] == 'd')
order |= DORD_double << sh;
sh += DORD_shift;
if ((p = strchr(p, ',')) == NULL)
break;
p++;
}
if (sh < 16)
order |= 0x7777 << sh;
return order;
}
static void read_ff_cfg(void)
{
enum {
#define x(n,o,v) FFCFG_##o,
#include "ff_cfg_defaults.h"
#undef x
FFCFG_nr
};
const static struct opt ff_cfg_opts[FFCFG_nr+1] = {
#define x(n,o,v) [FFCFG_##o] = { #n },
#include "ff_cfg_defaults.h"
#undef x
};
FRESULT fr;
int option;
struct opts opts = {
.file = &fs->file,
.opts = ff_cfg_opts,
.arg = fs->buf,
.argmax = sizeof(fs->buf)-1
};
fatfs.cdir = cfg.cfg_cdir;
fr = F_try_open(&fs->file, "FF.CFG", FA_READ);
if (fr)
return;
while ((option = get_next_opt(&opts)) != -1) {
switch (option) {
/* DRIVE EMULATION */
case FFCFG_interface:
ff_cfg.interface =
!strcmp(opts.arg, "shugart") ? FINTF_SHUGART
: !strcmp(opts.arg, "ibmpc") ? FINTF_IBMPC
: !strcmp(opts.arg, "ibmpc-hdout") ? FINTF_IBMPC_HDOUT
: !strcmp(opts.arg, "jppc") ? FINTF_JPPC
: !strcmp(opts.arg, "jppc-hdout") ? FINTF_JPPC_HDOUT
: !strcmp(opts.arg, "akai-s950") ? FINTF_JPPC_HDOUT
: !strcmp(opts.arg, "amiga") ? FINTF_AMIGA
: FINTF_JC;
break;
case FFCFG_host:
ff_cfg.host =
!strcmp(opts.arg, "acorn") ? HOST_acorn
: !strcmp(opts.arg, "akai") ? HOST_akai
: !strcmp(opts.arg, "casio") ? HOST_casio
: !strcmp(opts.arg, "dec") ? HOST_dec
: !strcmp(opts.arg, "ensoniq") ? HOST_ensoniq
: !strcmp(opts.arg, "fluke") ? HOST_fluke
: !strcmp(opts.arg, "gem") ? HOST_gem
: !strcmp(opts.arg, "ibm-3174") ? HOST_ibm_3174
: !strcmp(opts.arg, "memotech") ? HOST_memotech
: !strcmp(opts.arg, "msx") ? HOST_msx