-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmegayume_upper.spin2
1393 lines (1175 loc) · 37.1 KB
/
megayume_upper.spin2
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
CON ' Header
'' _______ _______ _______ _______ _______ _______
'' ( | ____ ( ____ ( ___ )\ /|\ /( | ____ \
'' | () () | ( \/ ( \/ ( ) ( \ / ) ) ( | () () | ( \/
'' | || || | (__ | | | (___) |\ (_) /| | | | || || | (__
'' | |(_)| | __) | | ____| ___ | \ / | | | | |(_)| | __)
'' | | | | ( | | \_ ) ( ) | ) ( | | | | | | | (
'' | ) ( | (____/\ (___) | ) ( | | | | (___) | ) ( | (____/\
'' |/ \(_______(_______)/ \| \_/ (_______)/ \(_______/
''
'' -= SEGA MEGA DRIVE EMULATOR =-
''
VERSION_MAJOR = 1
VERSION_MINOR = 4
VERSION_RC = 0
'' Note: this sucks. not anymore.
'' Perhaps more important note: use the included build.sh.
#include "config.spin2"
CON
MAX_PATH = 255
DIRENT_MAX = 4000
EXRAM_MAX = 8*1024*1024
HEAPSIZE = 1024*8
OBJ
video : "MegaVGA"
'' A = BT3, B = BT2, C = BT1, X = BT4, Y = BT0, Z = BT5
usb : "usbnew" | ACTIVITY_LED = -1, ERROR_LED = -1, USB_BASE_PIN = USB0_BASEPIN, USB_ENABLE_OFFSET = ENABLE_PIN, USB_DMINUS_OFFSET = DM_PIN, ...
OVERLAP_MEMORY = true, ...
EMUPAD_MAX_PLAYER = 4, EMUPAD_BUILTIN_RULES = true, ...
EMUPAD_TYPE_KBD = 1, EMUPAD_TYPE_PAD = 1, EMUPAD_TYPE_PAD_3BT = 2, ...
EMUPAD_UP_BIT = 0,EMUPAD_DOWN_BIT = 1,EMUPAD_LEFT_BIT = 2,EMUPAD_RIGHT_BIT = 3, ...
EMUPAD_BT0_BIT = 9, EMUPAD_BT1_BIT = 5, EMUPAD_BT2_BIT = 4, EMUPAD_BT3_BIT = 6, ...
EMUPAD_BT4_BIT = 10, EMUPAD_BT5_BIT = 8, EMUPAD_BT6_BIT = -1, EMUPAD_BT7_BIT = -1, ...
EMUPAD_BT8_BIT = 11, EMUPAD_BT9_BIT = 7, ...
EMUPAD_KB_BT0 = $07, EMUPAD_KB_BT1 = $19, EMUPAD_KB_BT2 = $06, EMUPAD_KB_BT3 = $1B, ...
EMUPAD_KB_BT4 = $16, EMUPAD_KB_BT5 = $09, EMUPAD_KB_BT6 = 0, EMUPAD_KB_BT7 = 0, ...
EMUPAD_KB_BT8 = $2A, EMUPAD_KB_BT9 = $28
padmap: "padmap_parser"
c : "libc"
a : "fsadapter.c"
#ifdef USE_PSRAM4
exmem : "psram4drv-dualCE.spin2"
#endif
#ifdef USE_PSRAM8
exmem : "psram8drv.spin2"
#endif
#ifdef USE_PSRAM16
exmem : "psram16drv.spin2"
#endif
#ifdef USE_HYPER
exmem : "hyperdrv.spin2"
#endif
VAR
long repeatkey,repeatnext
long romsize,romfile,srmfile
long selection
long exmem_mailbox[8*3]
long exmem_struct
byte cpu_cog,z80_cog,exmem_cog
byte smdrom
byte got_parameter
byte header_buf[$100]
alignl
byte curdir[MAX_PATH+1],tmppath[MAX_PATH+1]
#ifdef INPUT_USE_PINS
alignl
long pinput_stack[32]
#endif
PUB main() | tmp, x, mirrors, tmp2
org
' allocate all the locks like some sort of maniac
mov pa,#0
rep @.lockmadness,#16
locknew pb wc
if_nc bith pa,pb
.lockmadness
' deallocate all the ones we don't want
andn pa,##STATIC_LOCKS
mov pb,#0
rep @.lockmadness2,#16
shr pa,#1 wc
if_c lockret pb
add pb,#1
.lockmadness2
end
' setup video driver
video.start($F8,$70+4,long[$20],VIDEO_MODE,VGA_BASEPIN,VGA_VSYNC,VIDEO_SUBMODE) '
' setup OPN2/PSG
coginit(HUBEXEC_NEW,long[$30],0)
'setup VDP
coginit(COGEXEC_NEW,long[$24],0)
coginit(COGEXEC_NEW,long[$28],0)
menu_vdp_init()
msgbox_simple(@"Mounting storage...")
_mount(@"/sd",c._vfs_open_sdcardx(SDCARD_CLK,SDCARD_SELECT,SDCARD_DI,SDCARD_DO))
#ifdef INPUT_USE_USB
usb.set_emupad_ptr($40)
tmp := c.fopen(@"/sd/PADMAP.TXT",@"r")
if tmp
padmap.parse(usb.get_emupad_rule_buffer(),usb.EMUPAD_MAX_RULES,@c.fgetc,tmp,@"megayume")
c.fclose(tmp)
usb.start()
#elseifdef INPUT_USE_PINS
cogspin(NEWCOG,pinput_cog(),@pinput_stack)
#endif
#ifndef DIRECT_BOOT
if USE_ARGV && long[$FC000] == ("A" + "R"<<8 + "G"<<16 + "v"<<24) && word[$FC004] <> 0
got_parameter := true
long[$FC000] := 0 ' Destroy signature
else
do_opening()
#endif
'clearplane(MENU_PLANEA)
if a.get_type_for_path(@"/sd/MEGAYUME")==1
c.strncpy(@curdir,@"/sd/MEGAYUME/",MAX_PATH)
else
c.strncpy(@curdir,@"/sd/",MAX_PATH)
'msgbox_simple(@"OK!")
'waitkey(MENUKEY_CONFIRM)
repeat
repeat
#ifdef DIRECT_BOOT
c.strncpy(@tmppath,@DIRECT_BOOT,MAX_PATH)
#else
if got_parameter
c.strncpy(@tmppath,$FC004,MAX_PATH)
got_parameter := false
elseifnot do_filechooser()
next
#endif
'msgbox_simple(@tmppath)
'waitkey(MENUKEY_CONFIRM)
case a.get_type_for_path(@tmppath)
0:
msgbox_simple(@"Path not found?")
waitkey(MENUKEY_CONFIRM)
next
1:
c.strncpy(@curdir,@tmppath,MAX_PATH)
c.strncat(@curdir,@"/",MAX_PATH)
selection := 0
next ' go for another round
2:
'msgbox_simple(@"ROM ok?")
'waitkey(MENUKEY_CONFIRM)
if open_romfile(@tmppath) < 0
msgbox_simple(@"open error")
waitkey(MENUKEY_CONFIRM)
next
'c.sprintf(@tmppath,@"Rom size: %d",romsize)
'msgbox_simple(@tmppath)
'waitkey(MENUKEY_CONFIRM)
'settile(MENU_PLANEA,0,0,"X"+ATTR_PAL2<<11)
exmem_start()
'settile(MENU_PLANEA,0,0,"L"+ATTR_PAL2<<11)
do_load_rom()
byte[$E1].[6 addbits 1] := detect_region()
byte[$50] := detect_iomode()
' remove extension from tmppath
tmp := strsize(@tmppath)
repeat while --tmp >= 0
case tmppath[tmp]
".":
tmppath[tmp] := 0
quit
"/": quit
' add SRM extnesion
c.strncat(@tmppath,@".SRM",MAX_PATH)
init_sram(@tmppath)
settile(MENU_PLANEA,0,0,"2"+ATTR_PAL2<<11)
quit
run_emulator()
if srmfile
flush_sram()
c.fclose(srmfile\0)
menu_vdp_init()
PRI run_emulator() | tmp,did_reset,did_pause,should_pause,inband_time,inband_which,inband_trig
did_pause := did_reset := should_pause := 0
exmem_stop()
bytefill(MKRAM_BASE,0,$1_0000)
bytefill(VRAM_BASE,0,$1_0000)
' setup Z80
z80_cog := coginit(HUBEXEC_NEW,long[$2C],0)
' setup 68000
cpu_cog := coginit(HUBEXEC_NEW,long[$1C],0)
inband_which := 0
repeat
if INBAND_RESET
tmp := long[$40]
if tmp.[0] and tmp.[7] ' Up+Start (Reset)
if inband_which <> 1
inband_which:=1
inband_time := getct()
#ifndef DIRECT_BOOT
elseif tmp.[1] and tmp.[7] ' Down+Start (Quit)
if inband_which <> 2
inband_which:=2
inband_time := getct()
#endif
else
inband_which:=0
if inband_which<>0 and (getct()-inband_time) > (3*clkfreq)
inband_trig := inband_which\0
else
inband_trig := 0
tmp:=((usb.keystate(usb.KEY_LCTRL)||usb.keystate(usb.KEY_RCTRL)) and usb.keystate(usb.KEY_R)) or inband_trig == 1
if tmp and not did_reset
byte[$E2] := 255
cogatn(decod cpu_cog)
did_reset:=tmp
tmp := usb.keystate(usb.KEY_PAUSE)
if tmp and not did_pause
should_pause := true
did_pause:=tmp
if should_pause and long[$F8] +< 224 ' only toggle 86k pause during active scan to avoid garbage
if byte[$E2] ^= 1
cogatn(decod cpu_cog)
should_pause := false
if ((usb.keystate(usb.KEY_LCTRL)||usb.keystate(usb.KEY_RCTRL)) and usb.keystate(usb.KEY_Esc)) or inband_trig == 2
quit
' Autosave SRAM if last write was 3 seconds ago
if srmfile and long[$E4] and (getct() - long[$E4]) >= clkfreq*3
long[$E4] := 0 ' clear first, so if we get another write while we're busy, it will still be autosaved later
flush_sram()
repeat until locktry(ROM_LOCK)
cogstop(cpu_cog)
cogstop(z80_cog)
tmp:=coginit(HUBEXEC_NEW,long[$2C],0) ' Start Z80 again so it can reset audio
waitms(1)
cogstop(tmp)
lockrel(ROM_LOCK)
PRI poll_menukeys() : keys | pad
pad := long[$40]
if {usb.keystate(usb.KEY_UP) or} pad.[0]
keys |= MENUKEY_UP
if {usb.keystate(usb.KEY_DOWN) or} pad.[1]
keys |= MENUKEY_DOWN
if {usb.keystate(usb.KEY_LEFT) or} pad.[2]
keys |= MENUKEY_LEFT
if {usb.keystate(usb.KEY_RIGHT) or} pad.[3]
keys |= MENUKEY_RIGHT
if usb.keystate(usb.KEY_PAGEUP)
keys |= MENUKEY_PGUP
if usb.keystate(usb.KEY_PAGEDOWN)
keys |= MENUKEY_PGDOWN
if {usb.keystate(usb.KEY_Enter) or} pad.[7] or pad.[5] or pad.[6]
keys |= MENUKEY_CONFIRM
if usb.keystate(usb.KEY_Esc) or {usb.keystate(usb.KEY_Bkspace) or} pad.[4]
keys |= MENUKEY_BACK
PRI open_romfile(name) : r | tmp
tmp := __builtin_alloca(512)
if romfile
c.fclose(romfile)
romfile := c.fopen(name,@"rb")
ifnot romfile
return -1
smdrom := 0
if c.fread(tmp,512,1,romfile) == 512
if long[tmp+$100] <> SEGA_FOURCC and word[tmp+8] == $BBAA
smdrom := 1
c.fseek(romfile,0,c.SEEK_END)
romsize := c.ftell(romfile)
if smdrom
romsize -= 512
c.fseek(romfile,smdrom?512:0,c.SEEK_SET)
return smdrom
PRI init_sram(srmpath) | tmp,sram_is_nvram,open_existing
' init to disabled state
bytefill(SRAM_BASE,$FF,$10000)
long[$EC] := long[$E8] := -1
long[$E4] := 0
byte[$E3] := %10
sram_is_nvram := 0
if srmfile
msgbox_simple(@"srmfile leak?")
waitkey(MENUKEY_CONFIRM)
c.fclose(srmfile\0)
tmp := @header_buf
if long[tmp+$00] == SEGA_FOURCC and word[tmp+$B0] == ("R"+"A"<<8) and byte[tmp+$B3] == $20
sram_is_nvram := byte[tmp+$B2].[6]
long[$E8] := __builtin_bswap32(long[tmp+$B4]) zerox 23 ' sram start
long[$EC] := __builtin_bswap32(long[tmp+$B8]) zerox 23 ' sram end
'' Sanity checks and hacks
if long[$EC]-long[$E8] +> $FFFF ' end before start or larger than 64k
long[$EC] := long[$E8]+$FFFF
if long[$E8] +>= $80_0000 ' header mistake (or weird thing we don't support?)
sram_is_nvram := false
long[$EC] := long[$E8] := -1
'c.sprintf(tmp,@"RAM: %08X..%08X",long[$E8],long[$EC])
'msgbox_simple(tmp)
'waitkey(MENUKEY_CONFIRM)
if long[$E8] >= romsize
byte[$E3] := %01 ' initialize to enabled state
'' open srmfile if need be
if sram_is_nvram
open_existing := a.exists(srmpath)
if open_existing
srmfile := c.fopen(srmpath,@"r+b")
ifnot srmfile
msgbox_simple(@"Failed to open save file!")
waitkey(MENUKEY_CONFIRM)
else
c.fread(SRAM_BASE,1,$1_0000,srmfile)
msgbox_simple(@"Save open OK!")
waitkey(MENUKEY_CONFIRM)
else
srmfile := c.fopen(srmpath,@"w+b")
ifnot srmfile
msgbox_simple(@"Failed to create save file!")
waitkey(MENUKEY_CONFIRM)
else
c.fwrite(SRAM_BASE,1,$1_0000,srmfile)
msgbox_simple(@"Save create OK!")
waitkey(MENUKEY_CONFIRM)
PRI flush_sram()
c.fseek(srmfile,0,c.SEEK_SET)
c.fwrite(SRAM_BASE,1,$1_0000,srmfile)
c.fflush(srmfile)
PRI detect_region() : region | chr,tmp, got, i, valid
tmp := @header_buf
if long[tmp+$00] <> SEGA_FOURCC
' No SEGA header, assume the worst
return REGION_PREFERRED
' Try parsing old style first
got := 0
valid := true
repeat i from 0 to 2
case chr:=byte[tmp][$F0+i]
"J": got.[REGION_DOMESTIC_60HZ] := true
"U": got.[REGION_OVERSEAS_60HZ] := true
"E": got.[REGION_OVERSEAS_50HZ] := true
" ": ' nothing burger
other: valid:= false
' Try parsing new style
ifnot valid
got:=-1
repeat i from 0 to 2
case chr:=byte[tmp][$F0+i]
"0".."9":
if got < 0
got := chr-"0"
valid := true
else
valid := false
"A".."F":
if got < 0
got := chr-"A"+$A
valid := true
else
valid := false
" ": ' nothing burger
other: valid:= false
if got < 0
valid := false
' got preferred 60Hz region?
if valid == 0 or got == 0 or got.[REGION_PREFERRED]
return REGION_PREFERRED
' got the other 60Hz region?
if got.[REGION_NOT_PREFERRED]
return REGION_NOT_PREFERRED
' Try PAL
if got.[REGION_OVERSEAS_50HZ]
return REGION_OVERSEAS_50HZ
return REGION_DOMESTIC_50HZ ' The nameless land named OHA...
PRI detect_iomode() : mode | ptr,serial
' detect games that don't work with 6 button pads
' or don't properly declare multitap compatibility
' by comparing serial numbers
ptr := @iomode_special_serials
repeat (@iomode_special_serials_end-@iomode_special_serials)/14
ifnot c.memcmp(@header_buf+$80,ptr,14)
return byte[ptr+14]
ptr+=15
' detect multitap support using header
if long[@header_buf+$00] == SEGA_FOURCC
ptr := @header_buf+$90
ifnot c.memcmp(ptr,@"OJKRPTBVFCA ",16)
return IOMODE_MULTITAP_3BUTTON ' Is it allowed to murder Codemasters employees?
repeat 16
if byte[ptr++] == "4"
return IOMODE_MULTITAP
return IOMODE_6BUTTON
DAT
'' Oddly, not all of the games that are listed online as being 6-button-incompatible
'' actually misbehave in emulation. Perhaps my 6 button state machine is more robust?
iomode_special_serials
byte "GM T-48036 -00",IOMODE_3BUTTON ' Ms. Pacman
byte "GM T-95026-00 ",IOMODE_3BUTTON ' Sunset Riders
byte "GM MK-1210 -00",IOMODE_3BUTTON ' Mario Lemieux Hockey
byte "GM T-48216 -00",IOMODE_MULTITAP ' Gauntlet 4
byte "GM T-48213 -00",IOMODE_MULTITAP ' Gauntlet 4 (alt)
byte "GM T-125016-00",IOMODE_MULTITAP ' The Lost Vikings
byte "GM BMAP-001-00",IOMODE_6BUTTON ' Xeno Crisis (bad multitap support)
byte "GM T-23056 -00",IOMODE_6BUTTON ' Columns 3 (bad multitap support)
byte "GM T-120096-50",IOMODE_MULTITAP_3BUTTON ' Micro Machines 2
iomode_special_serials_end
PRI do_load_rom() | tmp,x
msgbox_simple(@"Loading ROM...")
byte[$E0] := encod(romsize-1) ' Set rom size limit for lower code
exmem_fill(0,0,EXRAM_MAX,true) ' Fill entire memory with zeroes
tmp:=0
repeat
x := (tmp*25)/romsize
settiles(MENU_PLANEA,7,7+x,14,14,258+ATTR_PAL2<<11)
settiles(MENU_PLANEA,7+x+1,7+25,14,14," "+ATTR_PAL2<<11)
rom_read_block(MKRAM_BASE+tmp&$4000)
exmem_write(tmp,MKRAM_BASE+tmp&$4000,$4000,true)
if tmp==0
' Buffer ROM Header
longmove(@header_buf,MKRAM_BASE+$100,$100/4)
while (tmp+=$4000)<romsize
exmem_sync()
PRI rom_read_block(dest) | i,j,tmp1,tmp2
'' Read a 16K block of ROM data
if tmp1 := $4000 - c.fread(dest,1,$4000,romfile)
bytefill(dest+$4000-tmp1,0,tmp1)
' stupid super magic drive interleave
' adapted from: https://stackoverflow.com/a/55112294
if smdrom
i := $3FFF
org
.lp
mov j,i
.adj
shr j,#1 wc
bitnc j,#13
cmp i,j wc
if_b jmp #.adj
add i,dest
add j,dest
rdbyte tmp1,i
rdbyte tmp2,j
wrbyte tmp2,i
wrbyte tmp1,j
sub i,dest
djnf i,#.lp
end
CON
#ifdef USE_PSRAM_SLOW
PSRAM_TIMEOFFSET = 11 ' I guess???
#else
PSRAM_TIMEOFFSET = 8
#endif
PRI exmem_start() | tmp,cogs,banks
ifnot exmem_struct
exmem_struct := c.malloc(8*4+8*4+32*4)
tmp := exmem_struct
cogs := tmp+8*4
banks := cogs+8*4
if exmem_cog
return
long[tmp][0] := clkfreq
#ifdef USE_HYPER
long[tmp][1] := (HYPER_SYNC_CLOCK?0:1)<<exmem.UNREGCLK_BIT
long[tmp][2] := (HYPER_RESET < 32 && HYPER_RESET > 0) ? 1<<HYPER_RESET : 0
long[tmp][3] := HYPER_RESET >= 32 ? 1<<(HYPER_RESET-32) : 0
#elseifdef USE_PSRAM_EITHER
long[tmp][1] := (PSRAM_SYNC_CLOCK?0:1)<<exmem.UNREGCLK_BIT
#ifdef USE_PSRAM_SLOW
long[tmp][1] |= 1<<exmem.SLOWCLK_BIT | 1<<exmem.CLKSEL_BIT
#endif
long[tmp][2] := 0
long[tmp][3] := 0
if PSRAM_BANKS > 1
pinh((PSRAM_SELECT+1) addpins (PSRAM_BANKS-2))
#endif
#ifdef USE_PSRAM_EITHER
long[tmp][4] := PSRAM_BASE
#elseifdef USE_HYPER
long[tmp][4] := HYPER_BASE
#endif
long[tmp][5] := banks
long[tmp][6] := cogs
long[tmp][7] := @exmem_mailbox[0]
long[cogs][0]:=-1<<16 + %1_111<<12
longfill(cogs+4,-1<<16,7)
longfill(banks,negx,32)
#ifdef USE_PSRAM16
long[banks][0] := 512<<16 + (PSRAM_DELAY-PSRAM_TIMEOFFSET)<<13 + (PSRAM_SYNC_DATA?0:1)<<12 + 22
long[banks][16] := PSRAM_SELECT + PSRAM_CLK<<8
#elseifdef USE_PSRAM8
long[banks][0] := 256<<16 + (PSRAM_DELAY-PSRAM_TIMEOFFSET)<<13 + (PSRAM_SYNC_DATA?0:1)<<12 + 22
long[banks][16] := PSRAM_SELECT + PSRAM_CLK<<8
#elseifdef USE_PSRAM4
long[banks][0] := 128<<16 + (PSRAM_DELAY-PSRAM_TIMEOFFSET)<<13 + (PSRAM_SYNC_DATA?0:1)<<12 + 22
long[banks][16] := PSRAM_SELECT + PSRAM_CLK<<8 + PSRAM_SELECT<<16
#elseifdef USE_HYPER
long[banks][0] := 128<<16 + (HYPER_DELAY-7)<<13 + (HYPER_SYNC_DATA?0:1)<<12 + 22
long[banks][16] := HYPER_SELECT + HYPER_CLK<<8 + HYPER_RWDS<<16 + HYPER_LATENCY<<25 ' Latency????
#endif
exmem_mailbox[0] := -1
cogs := exmem.getDriverAddr()
exmem_cog := coginit(COGEXEC_NEW,cogs,tmp)+1
repeat while exmem_mailbox[0] ' wait for init so data structures can go dead
PRI exmem_stop()
if exmem_cog
exmem_sync()
cogstop((exmem_cog\0)-1)
PRI exmem_sync()
repeat while exmem_mailbox[0]
PRI exmem_write(dst,src,length,async)
exmem_sync()
exmem_mailbox[2] := length
exmem_mailbox[1] := src
exmem_mailbox[0] := exmem.R_WRITEBURST + (dst & $fffffff)
ifnot async
exmem_sync()
PRI exmem_fill(dst,val,length,async)
exmem_sync()
exmem_mailbox[2] := length
exmem_mailbox[1] := val
exmem_mailbox[0] := exmem.R_WRITEBYTE + (dst & $fffffff)
ifnot async
exmem_sync()
PRI exmem_read(dst,src,length,async)
exmem_sync()
exmem_mailbox[2] := length
exmem_mailbox[1] := dst
exmem_mailbox[0] := exmem.R_READBURST + (src & $fffffff)
ifnot async
exmem_sync()
CON
SEGA_FOURCC = ("S"+"E"<<8+"G"<<16+"A"<<24)
MENU_PLANEA = $C000
MENU_PLANEB = $E000
ATTR_PRIO = 1<<(15-11)
ATTR_PAL0 = 0<<(13-11)
ATTR_PAL1 = 1<<(13-11)
ATTR_PAL2 = 2<<(13-11)
ATTR_PAL3 = 3<<(13-11)
ATTR_FLIP = 1<<(12-11)
ATTR_MIRR = 1<<(11-11)
' These have to match lower code
VRAM_BASE = $3_0000
MKRAM_BASE = $1_0000
SRAM_BASE = $4_0000
ROM_LOCK = 7
HINT_LOCK = 8
VINT_LOCK = 9
ZINT_LOCK = 10 ' Z80 VBlank IRQ is sorta independent
STATIC_LOCKS = decod ROM_LOCK | decod HINT_LOCK | decod VINT_LOCK | decod ZINT_LOCK
#0,IOMODE_3BUTTON,IOMODE_6BUTTON,IOMODE_MULTITAP,IOMODE_MULTITAP_3BUTTON
#0,PAD_NONE,PAD_6BUTTON,PAD_3BUTTON
REGION_OVERSEAS_60HZ = %10 '' Americas
REGION_DOMESTIC_60HZ = %00 '' Japan
REGION_OVERSEAS_50HZ = %11 '' Europe, Australia
REGION_DOMESTIC_50HZ = %01 '' Narnia, Gensokyo, Oz, Bielefeld
REGION_PREFERRED = PREFER_OVERSEAS ? REGION_OVERSEAS_60HZ : REGION_DOMESTIC_60HZ
REGION_NOT_PREFERRED = PREFER_OVERSEAS ? REGION_DOMESTIC_60HZ : REGION_OVERSEAS_60HZ
MENUKEY_UP = %0000_0001
MENUKEY_DOWN = %0000_0010
MENUKEY_LEFT = %0000_0100
MENUKEY_RIGHT = %0000_1000
MENUKEYS_DIRECTIONS = %1111
MENUKEY_CONFIRM = %0001_0000
MENUKEY_BACK = %0010_0000
MENUKEY_PGUP = %0100_0000
MENUKEY_PGDOWN = %1000_0000
PRI do_opening() | y,ptr,vram
screen_reset()
ptr := @info_text_block
vram := $2800
y:=4
repeat while byte[ptr]<>127
longfill(VRAM_BASE+vram,$1111_1111,32*8)
draw_shiftjis_string(ptr,VRAM_BASE+vram,1,2,32*2)
show_shiftjis_string(vram+/32 + ATTR_PAL2<<11,32,4,y++,MENU_PLANEA)
ptr += strsize(ptr)+1
vram += 32*8*4
'repeat until poll_menukeys() & MENUKEY_CONFIRM
' repeat y from 0 to 31
' word[VRAM_BASE+MENU_PLANEA][64*24+4+y] := long[$40].[y] ? "O" : "_"
waitkey(MENUKEY_CONFIRM)
DAT ' credits/info text
info_text_block
byte " -- M e g a Y u m e --",0
byte " SEGA Megadrive Emulator",0
byte " for Parallax P2",0
byte "",0
'byte " ",0
'' There's a joke in here somewhere about neither ASCII nor katakana
'' being able to fully render my name, but it got lost in encoding :/
byte " Programmed by Ada Gottenstraeter / アダ・ゴテンスツレテル",0
byte " of IRQsome Software non-incorporated",0
byte "",0
byte " Additional programming:",0
byte " Johannes Ahlebrand - original PSG code",0
byte " Eric Smith - flexspin compiler",0
byte " Roger Loh - external memory drivers",0
byte " Garry Jordan & Marco Maccaferri - USB driver",0
byte "",0
byte " UI fonts used: 「Misaki Gothic 2nd」 and 「funscii」",0
byte "",0
byte " This program is free software under the MIT license.",0
byte " See https://github.com/IRQsome/MegaYume for info.",0
byte " THE SOFTWARE IS PROVIDED 「AS IS」, WITHOUT WARRANTY OF ANY KIND.",0
byte "",0
byte " Hit CONFIRM key to continue...",0
byte "",0
byte 127
CON
INFO_X = 21
INFO_Y = 6
INFO_HEIGHT = 19
DIRLIST_X = 3
DIRLIST_Y = 4
DIRLIST_HEIGHT = 21
PRI do_filechooser() | keys,x,y,tmp,gotfiles,tmp2,attr,position,dirty
msgbox_simple(@"Accessing...")
gotfiles := scandir()
if gotfiles < 0
msgbox_simple(@"Directory error!!!")
waitkey(MENUKEY_CONFIRM)
return
if c.strncmp(@curdir,@"/",MAX_PATH) ' don't add updir into root dir
fileenter(@"<go up>",0,gotfiles++)
sortFiles(gotfiles)
screen_reset()
longfill(VRAM_BASE+$2800,0,32*8)
draw_shiftjis_string(@curdir,VRAM_BASE+$2800,1,2,32*2)
show_shiftjis_string($2800/32,32,DIRLIST_X,DIRLIST_Y-1,MENU_PLANEA)
if selection +>= gotfiles
selection := 0
position := 0
else
position := (selection - 2) <# (gotfiles - DIRLIST_HEIGHT) #> 0
dirty := true
repeatkey := 0
repeat while poll_menukeys() & !MENUKEYS_DIRECTIONS
repeat
if dirty\false
settiles(MENU_PLANEA,17,39,4,4," "+(ATTR_PAL0)<<11)
longfill(VRAM_BASE+$3000,$1111_1111,INFO_HEIGHT*16*8)
repeat y from 0 to INFO_HEIGHT-1
show_shiftjis_string($3000/32+y*16 + ATTR_PAL2<<11,16,INFO_X,INFO_Y+y,MENU_PLANEA)
if selection <= position
position := (selection-1) #> 0
if selection >= position+DIRLIST_HEIGHT-1
position := (selection-DIRLIST_HEIGHT+2) #> 0 <# gotfiles-DIRLIST_HEIGHT
repeat y from DIRLIST_Y to DIRLIST_Y+DIRLIST_HEIGHT-1
tmp := y-DIRLIST_Y+position
attr := tmp == selection ? ATTR_PAL1 : ATTR_PAL2
if tmp < gotfiles
tmp2:=MKRAM_BASE+tmp*12
settile(MENU_PLANEA,DIRLIST_X,y," "+attr<<11)
settile(MENU_PLANEA,DIRLIST_X+1,y,byte[tmp2++]+256+attr<<11)
repeat x from 2 to 9
settile(MENU_PLANEA,DIRLIST_X+x,y,byte[tmp2++]+attr<<11)
settile(MENU_PLANEA,DIRLIST_X+10,y,(byte[tmp2] <> " "?".":" ")+attr<<11)
repeat x from 11 to 13
settile(MENU_PLANEA,DIRLIST_X+x,y,byte[tmp2++]+attr<<11)
settile(MENU_PLANEA,DIRLIST_X+14,y," "+attr<<11)
else
settiles(MENU_PLANEA,DIRLIST_X,DIRLIST_X+14,y,y," "+(ATTR_PAL2)<<11)
ifnot gotfiles
' Does this case ever actually happen anymore?
putstring(@"No Files...",18,4,MENU_PLANEA,ATTR_PAL0)
else
case byte[MKRAM_BASE+12*selection]
0:putstring(@"Go one folder up.",INFO_X,4,MENU_PLANEA,ATTR_PAL0)
1:putstring(@"Folder",INFO_X,4,MENU_PLANEA,ATTR_PAL0)
2:
putstring(@"ROM info:",INFO_X,4,MENU_PLANEA,ATTR_PAL0)
c.strncpy(@tmppath,@curdir,MAX_PATH-12)
filegetname(@tmppath+strsize(@tmppath),selection)
if open_romfile(@tmppath) < 0
draw_shiftjis_string(@"OPEN ERROR???",VRAM_BASE+$3000+0*16*32,1,2,32)
else
rom_read_block(MKRAM_BASE+$C000)
if long[MKRAM_BASE+$C100] <> SEGA_FOURCC
draw_shiftjis_string(@"NO SEGA HEADER!!!",VRAM_BASE+$3000+0*16*32,1,2,32)
else
draw_shiftjis_string(@"Machine type:",VRAM_BASE+$3000+0*16*32,1,2,16)
draw_shiftjis_string(MKRAM_BASE+$C100,VRAM_BASE+$3000+(1*16+1)*32,1,2,16)
draw_shiftjis_string(@"Copyright:",VRAM_BASE+$3000+2*16*32,1,2,16)
draw_shiftjis_string(MKRAM_BASE+$C110,VRAM_BASE+$3000+(3*16+1)*32,1,2,16)
draw_shiftjis_string(@"Domestic name:",VRAM_BASE+$3000+4*16*32,1,2,16)
draw_shiftjis_string(MKRAM_BASE+$C120,VRAM_BASE+$3000+(5*16+1)*32,1,2,16)
draw_shiftjis_string(MKRAM_BASE+$C130,VRAM_BASE+$3000+(6*16+1)*32,1,2,16)
draw_shiftjis_string(MKRAM_BASE+$C140,VRAM_BASE+$3000+(7*16+1)*32,1,2,16)
draw_shiftjis_string(@"Overseas name:",VRAM_BASE+$3000+8*16*32,1,2,16)
draw_shiftjis_string(MKRAM_BASE+$C150,VRAM_BASE+$3000+(9*16+1)*32,1,2,16)
draw_shiftjis_string(MKRAM_BASE+$C160,VRAM_BASE+$3000+(10*16+1)*32,1,2,16)
draw_shiftjis_string(MKRAM_BASE+$C170,VRAM_BASE+$3000+(11*16+1)*32,1,2,16)
draw_shiftjis_string(@"ROM Serial:",VRAM_BASE+$3000+12*16*32,1,2,16)
draw_shiftjis_string(MKRAM_BASE+$C180,VRAM_BASE+$3000+(13*16+1)*32,1,2,16-2)
draw_shiftjis_string(@"Devices:",VRAM_BASE+$3000+14*16*32,1,2,16)
draw_shiftjis_string(MKRAM_BASE+$C190,VRAM_BASE+$3000+(15*16+1)*32,1,2,16)
draw_shiftjis_string(smdrom?@"Super Magic Drive dump":@"Raw ROM image",VRAM_BASE+$3000+16*16*32,1,2,32)
c.sprintf(@tmppath,@"ROM Size: %d",romsize)
draw_shiftjis_string(@tmppath,VRAM_BASE+$3000+(17*16+0)*32,1,2,32)
'settile(MENU_PLANEA,0,0,"0"+ATTR_PAL2<<11)
keys := poll_menukeys()
if keys & MENUKEY_BACK
repeat while poll_menukeys() & MENUKEY_BACK
tmppath[0] := 0
return false
elseif keys & MENUKEY_CONFIRM and gotfiles
repeat while poll_menukeys() & MENUKEY_CONFIRM
c.strncpy(@tmppath,@curdir,MAX_PATH-12)
if byte[MKRAM_BASE+selection*12]==0 ' got directory up?
tmp := @tmppath+strsize(@tmppath)
tmp2 := 2
repeat while tmp >= @tmppath and tmp2
if byte[tmp] == "/"
tmp2--
byte[tmp--] := 0
else
filegetname(@tmppath+strsize(@tmppath),selection)
return true
else
tmp := selection
selection := handle_menu_move(keys,selection,gotfiles-1,DIRLIST_HEIGHT)
if tmp <> selection
dirty := 1
PRI handle_menu_move(keys,current,max,pagelen) : r | tmp
tmp := 0
if ones(keys) <> 1
repeatkey := 0
return current
if repeatkey <> keys
repeatkey := keys
repeatnext := clkfreq+/2 + getct()
tmp := 1
elseif (getct()-repeatnext) >= 0
tmp := 1
repeatnext := clkfreq+/10 + getct()
ifnot tmp
return current
case encod keys
encod MENUKEY_UP:
return (current-1) #> 0
encod MENUKEY_DOWN:
return (current+1) <# max
encod MENUKEY_PGUP:
return (current-pagelen) #> 0
encod MENUKEY_PGDOWN:
return (current+pagelen) <# max
other:
return current
PRI scandir() : got | dir,tmp,type
c.strncpy(@tmppath,@curdir,MAX_PATH)
'if c.strncmp(@curdir,@"/sd/",MAX_PATH) ' if not root, remove slash for opening directory (WTF??)
' tmppath[strsize(@tmppath)-1] := 0
dir := c.opendir(@tmppath)
ifnot dir
return -1
repeat
tmp:= c.readdir(dir)
ifnot tmp
quit
ifnot type := a.get_type_for_dirent(tmp)
next
fileenter(a.get_name_for_dirent(tmp),type,got)
ifnot type == 1 or valid_rom_extension(got)
next
if ++got == DIRENT_MAX
quit
c.closedir(dir)
PRI fileenter(str,type,num) | pt
pt := MKRAM_BASE+12*num
byte[pt++] := type
repeat 8
if byte[str] == 0 || byte[str] == "."
byte[pt++] := " "
else
byte[pt++] := byte[str++]
if byte[str] == "."
str++
repeat 3
if byte[str] == 0
byte[pt++] := " "
else
byte[pt++] := byte[str++]
PRI valid_rom_extension(num) : r
if check_extension(num,"B"+"I"<<8+"N"<<16)
return true
if check_extension(num,"G"+"E"<<8+"N"<<16)
return true
if check_extension(num,"M"+"D"<<8+" "<<16)
return true
if check_extension(num,"S"+"M"<<8+"D"<<16)
return true
return false
PRI check_extension(num,ext) : r | tmp
tmp := long[MKRAM_BASE+12*num+9]&$FFFFFF
return tmp==ext || tmp==(ext|$202020)
PRI filegetname(dst,num) | pt
pt := MKRAM_BASE+12*num+1
repeat 8
if byte[pt] <> " "
byte[dst++] := byte[pt]
pt++
if byte[pt] <> " "
byte[dst++] := "."
repeat 3
if byte[pt] <> " "
byte[dst++] := byte[pt]
pt++
byte[dst] := 0
PRI sortFiles(length) : i | ip,jp,i2p,zonep,k,realsize,subsize,sizze,temp
'' Sort the Files...
temp := __builtin_alloca(12)
if length < 2 ' no need to sort if < 2 entries
return
'' Non-recursive in-place sorting algorithm
'' As relayed to me by a friendly Discord user
'' (ported to spin and optimized by me)
subsize := 12
length *= 12
repeat while subsize < length
sizze := subsize<<1
repeat i from 0 to length-12 step sizze
jp := (ip:=MKRAM_BASE+i)+subsize
realsize := sizze <# (length-i)
i2p := ip
repeat k from 0 to realsize-12 step 12
if jp => (ip+realsize) or i2p => jp
'pass
elseif compareNames(i2p,jp) =< 0
i2p += 12
else
zonep := jp
repeat
if (zonep+12) == (ip+realsize)
longmove(temp, i2p,3)
longmove(i2p,jp,3)
longmove(jp,jp+12,(zonep-jp)>>2)
longmove(zonep,temp,3)
if jp == zonep
i2p += 12
else
k -= 12
quit