Subversion Repositories NedoOS

Rev

Rev 2421 | Blame | Compare with Previous | Last modification | View Log | Download

  1. ; One-time initialization code, not retained after startup is complete.
  2.  
  3.         include "common/opl4.asm"
  4.         include "common/opn.asm"
  5.         include "common/opm.asm"
  6.         include "common/opna.asm"
  7.  
  8. startup
  9.         OS_GETMAINPAGES ;out: d,e,h,l=pages in 0000,4000,8000,c000, c=flags, b=id
  10.         ld (gpsettings.sharedpages),hl
  11.         ld a,e
  12.         ld (gpsettings.sharedpages+2),a
  13.         ld d,b
  14.         call quitifanotherinstanceisrunning
  15.         call turnturboon
  16.         ld e,7
  17.         OS_CLS
  18.         ld de,COLOR_DEFAULT
  19.         OS_SETCOLOR
  20.         ld de,currentfolder
  21.         OS_GETPATH
  22.         ld hl,(currentfolder+2)
  23.         ld a,l
  24.         xor '/'
  25.         or h
  26.         jr nz,$+5
  27.         ld (currentfolder+2),a
  28.         OS_SETSYSDRV
  29.         call loadandparsesettings
  30.         call checkinifile
  31.         call runsetup
  32.         call detectcpuspeed
  33.         call detectmoonsound
  34.         call detecttfm
  35.         call detectopm
  36.         jp detectopna
  37.  
  38. runsetup
  39.         ld a,(runplayersetup)
  40.         or a
  41.         ret z
  42. ;player setup
  43.         ld hl,(gpsettings.mididevice)
  44.         ld de,midioptions
  45.         ld b,midioptioncount
  46.         ld a,(hl)
  47.         and 15
  48.         call updateradiobuttons
  49.         ld hl,(gpsettings.moddevice)
  50.         ld de,modoptions
  51.         ld b,modoptioncount
  52.         ld a,(hl)
  53.         and 15
  54.         call updateradiobuttons
  55.         ld hl,(bomgemoonsettings)
  56.         ld de,bomgemoonoption
  57.         ld a,(hl)
  58.         and 1
  59.         call updatecheckbox
  60.         ld hl,(gpsettings.slowtfm)
  61.         ld de,slowtfmoption
  62.         ld a,(hl)
  63.         and 1
  64.         call updatecheckbox
  65.         ld hl,(gpsettings.slowmidiuart)
  66.         ld de,slowmidiuartoption
  67.         ld a,(hl)
  68.         and 1
  69.         call updatecheckbox
  70.         call redrawplayersetupui
  71.         ld hl,playersetupmsgtable
  72.         ld (currentmsgtable),hl
  73.         call playloop
  74.         ld de,COLOR_DEFAULT
  75.         OS_SETCOLOR
  76.         ld e,7
  77.         OS_CLS
  78.         ret
  79.  
  80. checkinifile
  81.         ld hl,(inifileversionsettings)
  82.         ld a,l
  83.         or h
  84.         jr z,.isoutdated
  85.         ld a,(hl)
  86.         cp '3'
  87.         ret z
  88. .isoutdated
  89.         ld hl,builtininifile
  90.         ld de,inifilebuffer
  91.         ld bc,builtininifilesize
  92.         ld (inifilesize),bc
  93.         ldir
  94.         xor a
  95.         ld (de),a
  96.         call parsesettings
  97.         ld a,1
  98.         ld (runplayersetup),a
  99.         ret
  100.  
  101. playersetupmsgtable
  102.         db (playersetupmsghandlers_end-playersetupmsghandlers_start)/3
  103. playersetupmsghandlers_start
  104.         db 0             : dw setupidle
  105.         db key_redraw    : dw redrawplayersetupui
  106.         db key_esc       : dw exitplayersetup
  107.         db key_up        : dw goprevoption
  108.         db key_down      : dw gonextoption
  109.         db key_enter     : dw setoption
  110.         db ' '           : dw setoption
  111.         db key_tab       : dw gonextfast
  112. playersetupmsghandlers_end
  113.  
  114. setupidle
  115.         YIELD
  116.         ret
  117.  
  118. playersetupoptions
  119. midioptions
  120.         dw midioption1str : dw midioptionhandler : dw defaultdevicedescstr
  121.         dw midioption2str : dw midioptionhandler : dw moonsounddescstr
  122.         dw midioption3str : dw midioptionhandler : dw midioption3descstr
  123.         dw midioption4str : dw midioptionhandler : dw midioption4descstr
  124.         dw midioption5str : dw midioptionhandler : dw midioption5descstr
  125.         dw midioption6str : dw midioptionhandler : dw midioption6descstr
  126. midioptioncount=($-midioptions)/6
  127. modoptions
  128.         dw modoption1str : dw modoptionhandler : dw defaultdevicedescstr
  129.         dw modoption2str : dw modoptionhandler : dw moonsounddescstr
  130.         dw modoption3str : dw modoptionhandler : dw modoption3descstr
  131. modoptioncount=($-modoptions)/6
  132. bomgemoonoption
  133.         dw bomgemoonoptionstr : dw bomgemoonhandler : dw bomgemoonoptiondescstr
  134. slowtfmoption
  135.         dw slowtfmoptionstr : dw slowtfmhandler : dw slowtfmoptiondescstr
  136. slowmidiuartoption
  137.         dw slowmidiuartoptionstr : dw slowmidiuarthandler : dw slowmidiuartoptiondescstr
  138. playersetupoptioncount=($-playersetupoptions)/6
  139.  
  140. MIDI_DEVICE_WINDOW_X = 4
  141. MIDI_DEVICE_WINDOW_Y = 3
  142. MOD_DEVICE_WINDOW_X = 46
  143. MOD_DEVICE_WINDOW_Y = 4
  144. MISC_OPTION_WINDOW_X = 20
  145. MISC_OPTION_WINDOW_Y = 13
  146.  
  147. playersetupui
  148.         CUSTOMUISETCOLOR ,COLOR_DEFAULT
  149.         CUSTOMUIPRINTTEXT ,30,0,settingsheaderstr
  150.         CUSTOMUIPRINTTEXT ,15,24,setuphotkeysstr
  151.         CUSTOMUISETCOLOR ,COLOR_PANEL
  152.         CUSTOMUIDRAWWINDOW ,MIDI_DEVICE_WINDOW_X,MIDI_DEVICE_WINDOW_Y,30,6
  153.         CUSTOMUIPRINTTEXT ,MIDI_DEVICE_WINDOW_X+2,MIDI_DEVICE_WINDOW_Y,mididevicestr
  154.         CUSTOMUIDRAWWINDOW ,MOD_DEVICE_WINDOW_X,MOD_DEVICE_WINDOW_Y,24,3
  155.         CUSTOMUIPRINTTEXT ,MOD_DEVICE_WINDOW_X+2,MOD_DEVICE_WINDOW_Y,moddevicestr
  156.         CUSTOMUIDRAWWINDOW ,MOD_DEVICE_WINDOW_X,MOD_DEVICE_WINDOW_Y,24,3
  157.         CUSTOMUIPRINTTEXT ,MOD_DEVICE_WINDOW_X+2,MOD_DEVICE_WINDOW_Y,moddevicestr
  158.         CUSTOMUIDRAWWINDOW ,MISC_OPTION_WINDOW_X,MISC_OPTION_WINDOW_Y,34,3
  159.         CUSTOMUIPRINTTEXT ,MISC_OPTION_WINDOW_X+2,MISC_OPTION_WINDOW_Y,miscoptionstr
  160.         CUSTOMUIDRAWEND
  161.  
  162. midioption1str db MIDI_DEVICE_WINDOW_Y+1,MIDI_DEVICE_WINDOW_X+1,"[X] Auto Select Device        ",0
  163. midioption2str db MIDI_DEVICE_WINDOW_Y+2,MIDI_DEVICE_WINDOW_X+1,"[ ] MoonSound (OPL4)          ",0
  164. midioption3str db MIDI_DEVICE_WINDOW_Y+3,MIDI_DEVICE_WINDOW_X+1,"[ ] NeoGS (VS10x3 Synth)      ",0
  165. midioption4str db MIDI_DEVICE_WINDOW_Y+4,MIDI_DEVICE_WINDOW_X+1,"[ ] UART AY1 (Multisound Old) ",0
  166. midioption5str db MIDI_DEVICE_WINDOW_Y+5,MIDI_DEVICE_WINDOW_X+1,"[ ] UART AY2 (Multisound New) ",0
  167. midioption6str db MIDI_DEVICE_WINDOW_Y+6,MIDI_DEVICE_WINDOW_X+1,"[ ] UART YM2608               ",0
  168. modoption1str db MOD_DEVICE_WINDOW_Y+1,MOD_DEVICE_WINDOW_X+1,"[X] Auto Select Device  ",0
  169. modoption2str db MOD_DEVICE_WINDOW_Y+2,MOD_DEVICE_WINDOW_X+1,"[ ] MoonSound (OPL4)    ",0
  170. modoption3str db MOD_DEVICE_WINDOW_Y+3,MOD_DEVICE_WINDOW_X+1,"[ ] GeneralSound        ",0
  171. bomgemoonoptionstr    db MISC_OPTION_WINDOW_Y+1,MISC_OPTION_WINDOW_X+1,"[ ] OPL3-only Device (BomgeMoon)  ",0
  172. slowtfmoptionstr      db MISC_OPTION_WINDOW_Y+2,MISC_OPTION_WINDOW_X+1,"[ ] Slow TurboSound-FM            ",0
  173. slowmidiuartoptionstr db MISC_OPTION_WINDOW_Y+3,MISC_OPTION_WINDOW_X+1,"[ ] MIDI UART at 3.5Mhz CPU       ",0
  174.  
  175. settingsheaderstr db "GP v0.9.0 Settings",0
  176. setuphotkeysstr db "ESC=Save&Continue  Space=Select  Up/Down=Nagivate",0
  177. mididevicestr db "MIDI Device...",0
  178. moddevicestr db "MOD Device...",0
  179. miscoptionstr db "Misc...",0
  180.  
  181. moonsounddescstr db "Проигрывать через MoonSound.",0
  182. defaultdevicedescstr db "Разрешает плееру использовать любое доступное устройство.",0
  183. midioption3descstr db "Используйте если у вас обновлённая версия NeoGS c декодером VLSI VS10x3.",0
  184. midioption4descstr db "Используйте если у вас MultiSound с прошивкой первой версии.",0
  185. midioption5descstr db "Используйте если у вас MultiSound с последней ревизией прошивки.",0
  186. midioption6descstr db "MIDI UART подключен через IOA YM2608.",0
  187. modoption3descstr db "Проигрывать через прошивку GeneralSound/NeoGS.",0
  188. bomgemoonoptiondescstr db "Включите, если у вас нет MoonSound, но есть карта с OPL3 чипом.",0
  189. slowtfmoptiondescstr db "Включите, если ваш TurboSound-FM работает плохо или не работает.",0
  190. slowmidiuartoptiondescstr db "Включите, если MIDI в MultiSound не работает или слышны обрывки нот.",0
  191.  
  192. activeoption db 0
  193. inifileversionsettings dw 0
  194. inifilesize dw 0
  195.  
  196. exitplayersetup
  197.         pop hl
  198.         ld de,settingsfilename
  199.         call openstream_file
  200.         or a
  201.         jr z,.openedfile
  202.         ld de,settingsfilename
  203.         OS_CREATEHANDLE
  204.         or a
  205.         ret nz
  206.         ld a,b
  207.         ld (filehandle),a
  208. .openedfile
  209.         ld a,(filehandle)
  210.         ld b,a
  211.         ld de,inifilebuffer
  212.         ld hl,(inifilesize)
  213.         OS_WRITEHANDLE
  214.         jp closestream_file
  215.  
  216. updatecheckbox
  217. ;hl = ini address
  218. ;de = option struct
  219. ;a = 0-1 value
  220.         dec a
  221.         ld c,a
  222.         ld b,1
  223.         ld a,'X'
  224.         ld (updateradiobuttons.selectionsymbol),a
  225.         ld a,'1'
  226.         jr updateradiobuttons.updateoptions
  227.  
  228. updateradiobuttons
  229. ;hl = ini address
  230. ;de = option structs
  231. ;b = option count
  232. ;a = active option
  233.         ld c,a
  234.         ld a,254
  235.         ld (.selectionsymbol),a
  236.         ld a,'0'
  237. .updateoptions
  238.         ld (.basedigit),a
  239.         ld a,h
  240.         or l
  241.         ret z
  242.         ld a,c
  243. .basedigit=$+1
  244.         add a,'0'
  245.         ld (hl),a
  246.         ex de,hl
  247.         inc c
  248. .loop   ld e,(hl)
  249.         inc hl
  250.         ld d,(hl)
  251.         inc de
  252.         inc de
  253.         inc de
  254.         dec c
  255. .selectionsymbol=$+1
  256.         ld a,254
  257.         jr z,$+4
  258.         ld a,' '
  259.         ld (de),a
  260.         ld de,5
  261.         add hl,de
  262.         djnz .loop
  263.         ret
  264.  
  265. midioptionhandler
  266.         ld hl,(gpsettings.mididevice)
  267.         ld de,midioptions
  268.         ld b,midioptioncount
  269.         ld a,(activeoption)
  270.         sub (midioptions - playersetupoptions)/6
  271.         call updateradiobuttons
  272.         jp drawsetupoptions
  273.  
  274. modoptionhandler
  275.         ld hl,(gpsettings.moddevice)
  276.         ld de,modoptions
  277.         ld b,modoptioncount
  278.         ld a,(activeoption)
  279.         sub (modoptions - playersetupoptions)/6
  280.         call updateradiobuttons
  281.         jp drawsetupoptions
  282.  
  283. bomgemoonhandler
  284.         ld hl,(bomgemoonsettings)
  285.         ld de,bomgemoonoption
  286.         ld a,(hl)
  287.         cpl
  288.         and 1
  289.         call updatecheckbox
  290.         jp drawsetupoptions
  291.  
  292. slowtfmhandler
  293.         ld hl,(gpsettings.slowtfm)
  294.         ld de,slowtfmoption
  295.         ld a,(hl)
  296.         cpl
  297.         and 1
  298.         call updatecheckbox
  299.         jp drawsetupoptions
  300.  
  301. slowmidiuarthandler
  302.         ld hl,(gpsettings.slowmidiuart)
  303.         ld de,slowmidiuartoption
  304.         ld a,(hl)
  305.         cpl
  306.         and 1
  307.         call updatecheckbox
  308.         jp drawsetupoptions
  309.  
  310. getactiveoptionaddr
  311. ;hl=base addr
  312. ;output: hl=active option addr
  313.         ld a,(activeoption)
  314.         add a,a
  315.         ld e,a
  316.         add a,a
  317.         add a,e
  318.         ld e,a
  319.         ld d,0
  320.         add hl,de
  321.         ret
  322.  
  323. setoption
  324.         ld hl,playersetupoptions+2
  325.         call getactiveoptionaddr
  326.         ld e,(hl)
  327.         inc hl
  328.         ld d,(hl)
  329.         ex de,hl
  330.         jp (hl)
  331.  
  332. goprevoption
  333.         ld a,(activeoption)
  334.         dec a
  335.         jp p,$+5
  336.         ld a,playersetupoptioncount-1
  337.         ld (activeoption),a
  338.         jp drawsetupoptions
  339.  
  340. gonextoption
  341.         ld a,(activeoption)
  342.         inc a
  343.         cp playersetupoptioncount
  344.         jr c,$+3
  345.         xor a
  346.         ld (activeoption),a
  347.         jp drawsetupoptions
  348.  
  349. gonextfast
  350.         ld a,(activeoption)
  351.         add a,3
  352.         cp playersetupoptioncount
  353.         jr c,$+3
  354.         xor a
  355.         ld (activeoption),a
  356.         jp drawsetupoptions
  357.  
  358. optiondescbuffer equ playlistpanel
  359.  
  360. redrawplayersetupui
  361.         ld e,7
  362.         OS_CLS
  363.         ld ix,playersetupui
  364.         call drawcustomui
  365. drawsetupoptions
  366.         ld hl,playersetupoptions
  367.         ld b,playersetupoptioncount
  368.         ld c,0
  369. .optionsloop
  370.         push bc
  371.         ld a,(activeoption)
  372.         cp c
  373.         ld de,COLOR_CURSOR
  374.         jr z,$+5
  375.         ld de,COLOR_PANEL_FILE
  376.         push hl
  377.         OS_SETCOLOR
  378.         pop hl
  379.         ld e,(hl)
  380.         inc hl
  381.         ld d,(hl)
  382.         push hl
  383.         ex de,hl
  384.         ld d,(hl)
  385.         inc hl
  386.         ld e,(hl)
  387.         inc hl
  388.         push hl
  389.         OS_SETXY
  390.         pop hl
  391.         call print_hl
  392.         pop hl
  393.         ld de,5
  394.         add hl,de
  395.         pop bc
  396.         inc c
  397.         djnz .optionsloop
  398. ;print desc
  399.         ld de,COLOR_DEFAULT
  400.         OS_SETCOLOR
  401.         ld de,0x1600
  402.         OS_SETXY
  403.         ld hl,playersetupoptions+4
  404.         call getactiveoptionaddr
  405.         ld e,(hl)
  406.         inc hl
  407.         ld d,(hl)
  408.         push de
  409.         ex de,hl
  410.         ld c,0
  411.         call findlastchar
  412.         ld a,l
  413.         sub e
  414.         dec a
  415.         ld c,a
  416.         ld a,80
  417.         sub c
  418.         rra
  419.         ld b,a
  420.         ld de,optiondescbuffer
  421.         ld a,' '
  422. .fillloop1
  423.         ld (de),a
  424.         inc de
  425.         djnz .fillloop1
  426.         pop hl
  427.         ldir
  428.         ld a,(optiondescbuffer+80)%256
  429.         sub e
  430.         ld b,a
  431.         ld a,' '
  432. .fillloop2
  433.         ld (de),a
  434.         inc de
  435.         djnz .fillloop2
  436.         xor a
  437.         ld (de),a
  438.         ld hl,optiondescbuffer
  439.         jp print_hl
  440.  
  441. loadandparsesettings
  442.         ld de,settingsfilename
  443.         call openstream_file
  444.         or a
  445.         ret nz
  446.         ld de,inifilebuffer
  447.         ld hl,0x4000
  448.         call readstream_file
  449.         ld (inifilesize),hl
  450.         ld de,inifilebuffer
  451.         add hl,de
  452.         ld (hl),0
  453.         call closestream_file
  454. parsesettings
  455.         ld de,inifilebuffer
  456. .parseloop
  457.         ld bc,'='*256
  458.         call findnextchar
  459.         or a
  460.         ret z
  461.         cp b
  462.         jr nz,.parseloop
  463.         ld b,settingsvarcount
  464.         ld hl,settingsvars
  465. .varsearchloop
  466.         ld a,(hl)
  467.         inc hl
  468.         cp c
  469.         jr z,.foundvar
  470.         inc hl
  471.         inc hl
  472.         djnz .varsearchloop
  473.         jr .nextvar
  474. .foundvar
  475.         ld a,(hl)
  476.         inc hl
  477.         ld h,(hl)
  478.         ld l,a
  479.         ld (hl),e
  480.         inc hl
  481.         ld (hl),d
  482. .nextvar
  483.         ld b,0
  484.         call findnextchar
  485.         or a
  486.         jr nz,.parseloop
  487.         ret
  488.  
  489. settingsfilename
  490.         db "gp/gp.ini",0
  491.  
  492. settingsvars
  493.         db 0x19 : dw gpsettings.usemp3
  494.         db 0x14 : dw gpsettings.usemwm
  495.         db 0x74 : dw gpsettings.usept3
  496.         db 0x1F : dw gpsettings.usevgm
  497.         db 0x26 : dw gpsettings.usemoonmod
  498.         db 0x7F : dw gpsettings.moonmoddefaultpanning
  499.         db 0x7A : dw gpsettings.midiuartdelayoverride
  500.         db 0x61 : dw bomgemoonsettings
  501.         db 0x20 : dw gpsettings.usemoonmid
  502.         db 0x11 : dw gpsettings.mididevice
  503.         db 0x7E : dw gpsettings.moddevice
  504.         db 0x32 : dw inifileversionsettings
  505.         db 0x78 : dw gpsettings.slowtfm
  506.         db 0x3C : dw gpsettings.slowmidiuart
  507. settingsvarcount=($-settingsvars)/3
  508.  
  509. findnextchar
  510. ;de = ptr
  511. ;b = character to search
  512. ;c = LRC
  513. ;output: de = ptr past character, c = updated LRC
  514.         ld a,(de)
  515.         inc de
  516.         or a
  517.         ret z
  518.         cp "\n"
  519.         ret z
  520.         cp b
  521.         ret z
  522.         xor c
  523.         ld c,a
  524.         jr findnextchar
  525.  
  526. detectcpuspeed
  527.         ld hl,detectingcpustr
  528.         call print_hl
  529.         call swapinterrupthandler ;avoid OS while benchmarking
  530.         halt
  531.         ld hl,0
  532.         ld e,0
  533.         xor a
  534.         ld (.spincount),a
  535.         ld a,33
  536.         halt
  537. ;--> 42 t-states loop start
  538. .loop   inc e
  539.         jp nz,$+4
  540.         inc hl
  541.         nop
  542. .spincount=$+1
  543.         ld bc,0
  544.         cp c
  545.         jp nc,.loop
  546. ;<-- loop end
  547.         push de
  548.         push hl
  549.         call swapinterrupthandler ;restore OS handler
  550.         pop hl
  551.         pop de
  552. ;hl = hle / 32
  553.         sla e : adc hl,hl
  554.         sla e : adc hl,hl
  555.         sla e : adc hl,hl
  556.         ld (gpsettings.framelength),hl
  557.         ex de,hl
  558.         ld hl,-MIN_FRAME_LENGTH_FPGA
  559.         add hl,de
  560.         ld hl,cpufpgastr
  561.         jp c,print_hl
  562.         ld hl,-MIN_FRAME_LENGTH_ZXEVO
  563.         add hl,de
  564.         ld hl,cpuevostr
  565.         jp c,print_hl
  566.         ld hl,cpuatmstr
  567.         jp print_hl
  568.  
  569. swapinterrupthandler
  570.         di
  571.         ld hl,.store
  572.         ld de,0x38
  573.         ld b,3
  574. .loop   ld a,(de)
  575.         ld c,(hl)
  576.         ld (hl),a
  577.         ld a,c
  578.         ld (de),a
  579.         inc hl
  580.         inc de
  581.         djnz .loop
  582.         ei
  583.         ret
  584. .store  jp lightweightinterrupthandler
  585.  
  586. lightweightinterrupthandler
  587.         push af
  588.         ld a,(detectcpuspeed.spincount)
  589.         inc a
  590.         ld (detectcpuspeed.spincount),a
  591.         pop af
  592.         ei
  593.         ret
  594.  
  595. isbomgemoon
  596. ;output: zf=0 is BomgeMoon flag is set
  597.         ld hl,(bomgemoonsettings)
  598.         ld a,l
  599.         or h
  600.         ret z
  601.         ld a,(hl)
  602.         cp '0'
  603.         ret
  604.  
  605. detectmoonsound
  606.         ld hl,detectingmoonsoundstr
  607.         call print_hl
  608.         call ismoonsoundpresent
  609.         ld hl,notfoundstr
  610.         jp nz,print_hl
  611.         call opl4init
  612.         call isbomgemoon
  613.         jr z,.detectwaveports
  614.         ld hl,devicebomgemoon
  615.         ld (devicelist.moonsoundstraddr),hl
  616.         ld a,1
  617.         ld (gpsettings.moonsoundstatus),a
  618.         ld hl,bomgemoonstr
  619.         jp print_hl
  620. .detectwaveports
  621.         ld bc,9
  622.         ld d,0
  623.         ld hl,0x1200
  624.         ld ix,playlistpanel
  625.         call opl4readmemory
  626.         ld b,9
  627.         ld de,rom001200
  628.         ld hl,gpsettings.moonsoundstatus
  629. .cmploop
  630.         ld a,(de)
  631.         cp (ix)
  632.         jr nz,.waveportsfailed
  633.         inc de
  634.         inc ix
  635.         djnz .cmploop
  636.         ld (hl),2
  637.         ld hl,foundstr
  638.         jp print_hl
  639. .waveportsfailed
  640.         ld (hl),1
  641.         ld hl,firmwareerrorstr
  642.         call print_hl
  643.         ld hl,pressanykeystr
  644.         call print_hl
  645.         YIELDGETKEYLOOP
  646.         ret
  647.  
  648. isslowtfm
  649. ;out: zf=0 if slow tfm, zf=1 otherwise
  650.         ld hl,(gpsettings.slowtfm)
  651.         ld a,h
  652.         or l
  653.         ret z
  654.         ld a,(hl)
  655.         cp '0'
  656.         ret
  657.  
  658. detecttfm
  659.         ld hl,detectingtfmstr
  660.         call print_hl
  661.         call isslowtfm
  662.         jr z,.fasttfm
  663.         call turnturbooff
  664. ;TODO: why is enabling FM output needed for NedoPC's TFM?
  665.         ld a,%11111000
  666.         ld (istfmpresent.ymselector),a
  667.         call istfmpresent
  668.         push af
  669.         call turnturboon
  670.         pop af
  671.         ld hl,notfoundstr
  672.         jp nz,print_hl
  673.         ld a,2
  674.         ld (gpsettings.tfmstatus),a
  675.         ld hl,foundslowstr
  676.         jp print_hl
  677. .fasttfm
  678.         call istfmpresent
  679.         ld hl,notfoundstr
  680.         jp nz,print_hl
  681.         ld a,1
  682.         ld (gpsettings.tfmstatus),a
  683.         ld hl,foundstr
  684.         jp print_hl
  685.  
  686. trywritingopm
  687.         dec a
  688.         jr nz,$-1
  689.         ld bc,OPM0_REG
  690.         out (c),e
  691.         ld bc,OPM1_REG
  692.         out (c),e
  693.         dec a
  694.         jr nz,$-1
  695.         ld bc,OPM0_DAT
  696.         out (c),d
  697.         ld bc,OPM1_DAT
  698.         out (c),d
  699.         ret
  700.  
  701. detectopm
  702.         ld hl,detectingopmstr
  703.         call print_hl
  704. ;check for non-zero as an early exit condition
  705.         ld bc,OPM0_DAT
  706.         in a,(c)
  707.         or a
  708.         ld hl,notfoundstr
  709.         jp nz,print_hl
  710. ;start timer
  711.         ld de,0xff12
  712.         call trywritingopm
  713.         ld de,0x2a14
  714.         call trywritingopm
  715. ;wait for the timer to finish
  716.         YIELD
  717.         YIELD
  718. ;check the timer flags
  719.         ld bc,OPM0_DAT
  720.         in a,(c)
  721.         cp 2
  722.         ld hl,notfoundstr
  723.         jp nz,print_hl
  724.         ld bc,OPM1_DAT
  725.         in a,(c)
  726.         cp 2
  727.         ld hl,founddualchipstr
  728.         jr z,.hasdualopm
  729.         call opmdisablechip1
  730.         ld hl,foundstr
  731.         ld a,1
  732. .hasdualopm
  733.         ld (gpsettings.opmstatus),a
  734.         call print_hl
  735.         jp opmstoptimers
  736.  
  737. trywritingopna1
  738.         dec a
  739.         jr nz,$-1
  740.         ld bc,OPNA1_REG
  741.         out (c),e
  742.         dec a
  743.         jr nz,$-1
  744.         ld bc,OPNA1_DAT
  745.         out (c),d
  746.         ret
  747.  
  748. detectopna
  749.         ld hl,detectingopnastr
  750.         call print_hl
  751. ;check for non-zero as an early exit condition
  752.         ld bc,OPNA1_REG
  753.         in a,(c)
  754.         or a
  755.         ld hl,notfoundstr
  756.         jp nz,print_hl
  757.         ld de,0xff26
  758.         call trywritingopna1
  759.         ld de,0x2a27
  760.         call trywritingopna1
  761. ;wait for the timer to finish
  762.         YIELD
  763.         YIELD
  764. ;check the timer flags
  765.         ld bc,OPNA1_REG
  766.         in a,(c)
  767.         cp 2
  768.         ld hl,notfoundstr
  769.         jp nz,print_hl
  770.         ld a,1
  771.         ld (gpsettings.opnastatus),a
  772.         ld de,0x3027
  773.         call trywritingopna1
  774.         ld de,0x0027
  775.         call trywritingopna1
  776.         ld hl,foundstr
  777.         jp print_hl
  778.  
  779. trywritingmoonsoundfm1
  780.         djnz $
  781.         ld a,e
  782.         out (MOON_REG1),a
  783.         djnz $
  784.         ld a,d
  785.         out (MOON_DAT1),a
  786.         ret
  787.  
  788. ismoonsoundpresent
  789. ;out: zf=1 if Moonsound is present, zf=0 if not
  790.         switch_to_pcm_ports_c2_c3
  791. ;check for 255 as an early exit condition
  792.         in a,(MOON_STAT)
  793.         add a,1
  794.         sbc a,a
  795.         ret nz
  796. ;read the status second time, now expect all bits clear
  797.         in a,(MOON_STAT)
  798.         or a
  799.         ret nz
  800. ;start timer
  801.         ld de,0xff03
  802.         call trywritingmoonsoundfm1
  803.         ld de,0x4204
  804.         call trywritingmoonsoundfm1
  805.         ld d,0x80
  806.         call trywritingmoonsoundfm1
  807. ;wait for the timer to finish
  808.         YIELD
  809.         YIELD
  810. ;check the timer flags
  811.         in a,(MOON_STAT)
  812.         cp 0xa0
  813.         ret nz
  814. ;there must be MoonSound in this system
  815.         call opl4stoptimers
  816.         xor a
  817.         ret
  818.  
  819. trywritingtfm1
  820.         dec a
  821.         jr nz,$-1
  822.         ld bc,OPN_REG
  823.         out (c),e
  824.         dec a
  825.         jr nz,$-1
  826.         ld bc,OPN_DAT
  827.         out (c),d
  828.         ret
  829.  
  830. istfmpresent
  831. ;out: zf=1 if tfm is present, zf=0 otherwise
  832.         ld bc,OPN_REG
  833. .ymselector=$+1
  834.         ld a,%11111100
  835.         out (c),a
  836.         ld de,0xff00
  837.         call trywritingtfm1
  838.         YIELD
  839.         YIELD
  840.         ld bc,OPN_REG
  841.         in a,(c)
  842.         bit 7,a
  843.         ret z
  844.         cp 254
  845.         ret
  846.  
  847. quitifanotherinstanceisrunning
  848. ;d = current pid
  849.         ld e,1
  850. .searchloop
  851.         ld a,e
  852.         cp d
  853.         jr z,.nextprocess
  854.         push de
  855.         OS_GETAPPMAINPAGES ;d,e,h,l=pages in 0000,4000,8000,c000
  856.         or a
  857.         ld a,d
  858.         pop de
  859.         jr nz,.nextprocess
  860.         push de
  861.         SETPGC000
  862.         ld hl,0xc000+COMMANDLINE
  863.         ld de,fullpathbuffer
  864.         ld bc,COMMANDLINE_sz
  865.         ldir
  866.         ld hl,fullpathbuffer
  867.         call skipword_hl
  868.         ld (hl),0
  869.         ld hl,fullpathbuffer
  870.         ld c,'/'
  871.         call findlastchar ;out: de = after last slash or start
  872.         call isplayer
  873.         pop de
  874.         jr z,.foundplayer
  875. .nextprocess
  876.         inc e
  877.         ld a,e
  878.         inc a
  879.         jr nz,.searchloop
  880.         ret
  881. .foundplayer
  882.         ld hl,COMMANDLINE
  883.         ld de,0xc000+COMMANDLINE
  884.         ld bc,COMMANDLINE_sz
  885.         ldir
  886.         QUIT
  887.  
  888. isplayer
  889. ;de = command line file name
  890. ;out: zf=1 if gp, zf=0 otherwise
  891.         ld a,(de)
  892.         call tolower
  893.         cp 'g'
  894.         ret nz
  895.         inc de
  896.         ld a,(de)
  897.         call tolower
  898.         cp 'p'
  899.         ret nz
  900.         inc de
  901.         ld a,(de)
  902.         or a
  903.         ret z
  904.         cp '.'
  905.         ret
  906.  
  907. detectingmoonsoundstr
  908.         db "Detecting MoonSound...",0
  909. detectingtfmstr
  910.         db "Detecting TurboSound FM...",0
  911. detectingopmstr
  912.         db "Detecting YM2151...",0
  913. detectingopnastr
  914.         db "Detecting YM2608...",0
  915. notfoundstr
  916.         db "no device!\r\n",0
  917. foundstr
  918.         db "found!\r\n",0
  919. bomgemoonstr
  920.         db "OPL3\r\n",0
  921. founddualchipstr
  922.         db "dual!\r\n",0
  923. foundslowstr
  924.         db "slow!\r\n",0
  925. detectingcpustr
  926.         db "Running on...",0
  927. cpufpgastr
  928.         db "FPGA\r\n",0
  929. cpuevostr
  930.         db "ZX Evolution\r\n",0
  931. cpuatmstr
  932.         db "ATM\r\n",0
  933. rom001200
  934.         db "Copyright"
  935. firmwareerrorstr
  936.         db "firmware problem!\r\nPlease update ZXM-MoonSound firmware to revision 1.01\r\n"
  937.         db "https://www.dropbox.com/s/1e0b2197emrhzos/zxm_moonsound01_frm0101.zip\r\n"
  938.         db "Or enable the BomgeMoon option in the player's settings to skip OPL4 port detection.",0
  939.  
  940. builtininifile
  941.         incbin "gp.ini"
  942. builtininifilesize=$-builtininifile
  943.