Subversion Repositories NedoOS

Rev

Blame | 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 closeexistingplayer
  15.         ld de,currentfolder
  16.         OS_GETPATH
  17.         ld hl,(currentfolder+2)
  18.         ld a,l
  19.         xor '/'
  20.         or h
  21.         jr nz,$+5
  22.         ld (currentfolder+2),a
  23.         OS_SETSYSDRV
  24.         call loadsettings
  25.         call detectcpuspeed
  26.         call detectmoonsound
  27.         call detecttfm
  28.         call detectopm
  29.         jp detectopna
  30.  
  31. loadsettings
  32.         ld de,settingsfilename
  33.         call openstream_file
  34.         or a
  35.         ret nz
  36.         ld de,browserpanel
  37.         ld hl,0x4000
  38.         call readstream_file
  39.         ld de,browserpanel
  40.         add hl,de
  41.         ld (hl),0
  42.         call closestream_file
  43.         ld de,browserpanel
  44. .parseloop
  45.         ld bc,'='*256
  46.         call findnextchar
  47.         or a
  48.         ret z
  49.         cp b
  50.         jr nz,.parseloop
  51.         ld b,settingsvarcount
  52.         ld hl,settingsvars
  53. .varsearchloop
  54.         ld a,(hl)
  55.         inc hl
  56.         cp c
  57.         jr z,.foundvar
  58.         inc hl
  59.         inc hl
  60.         djnz .varsearchloop
  61.         jr .nextvar
  62. .foundvar
  63.         ld a,(hl)
  64.         inc hl
  65.         ld h,(hl)
  66.         ld l,a
  67.         ld (hl),e
  68.         inc hl
  69.         ld (hl),d
  70. .nextvar
  71.         ld b,0
  72.         call findnextchar
  73.         or a
  74.         jr nz,.parseloop
  75.         ret
  76.  
  77. settingsvars
  78.         db 0x19 : dw gpsettings.usemp3
  79.         db 0x14 : dw gpsettings.usemwm
  80.         db 0x74 : dw gpsettings.usept3
  81.         db 0x1F : dw gpsettings.usevgm
  82.         db 0x26 : dw gpsettings.usemoonmod
  83.         db 0x7F : dw gpsettings.moonmoddefaultpanning
  84.         db 0x7A : dw gpsettings.midiuartdelayoverride
  85.         db 0x61 : dw bomgemoonsettings
  86.         db 0x20 : dw gpsettings.usemoonmid
  87.         db 0x4C : dw gpsettings.forcemididevice
  88. settingsvarcount=($-settingsvars)/3
  89.  
  90. findnextchar
  91. ;de = ptr
  92. ;b = character to search
  93. ;c = LRC
  94. ;output: de = ptr past character, c = updated LRC
  95.         ld a,(de)
  96.         inc de
  97.         or a
  98.         ret z
  99.         cp "\n"
  100.         ret z
  101.         cp b
  102.         ret z
  103.         xor c
  104.         ld c,a
  105.         jr findnextchar
  106.  
  107. detectcpuspeed
  108.         ld hl,detectingcpustr
  109.         call print_hl
  110.         call swapinterrupthandler ;avoid OS while benchmarking
  111.         halt
  112.         ld hl,0
  113.         ld e,0
  114.         xor a
  115.         ld (.spincount),a
  116.         ld a,33
  117.         halt
  118. ;--> 42 t-states loop start
  119. .loop   inc e
  120.         jp nz,$+4
  121.         inc hl
  122.         nop
  123. .spincount=$+1
  124.         ld bc,0
  125.         cp c
  126.         jp nc,.loop
  127. ;<-- loop end
  128.         push de
  129.         push hl
  130.         call swapinterrupthandler ;restore OS handler
  131.         pop hl
  132.         pop de
  133. ;hl = hle / 32
  134.         sla e : adc hl,hl
  135.         sla e : adc hl,hl
  136.         sla e : adc hl,hl
  137.         ld (gpsettings.framelength),hl
  138.         ex de,hl
  139.         ld hl,-MIN_FRAME_LENGTH_FPGA
  140.         add hl,de
  141.         ld hl,cpufpgastr
  142.         jp c,print_hl
  143.         ld hl,-MIN_FRAME_LENGTH_ZXEVO
  144.         add hl,de
  145.         ld hl,cpuevostr
  146.         jp c,print_hl
  147.         ld hl,cpuatmstr
  148.         jp print_hl
  149.  
  150. swapinterrupthandler
  151.         di
  152.         ld hl,.store
  153.         ld de,0x38
  154.         ld b,3
  155. .loop   ld a,(de)
  156.         ld c,(hl)
  157.         ld (hl),a
  158.         ld a,c
  159.         ld (de),a
  160.         inc hl
  161.         inc de
  162.         djnz .loop
  163.         ei
  164.         ret
  165. .store  jp lightweightinterrupthandler
  166.  
  167. lightweightinterrupthandler
  168.         push af
  169.         ld a,(detectcpuspeed.spincount)
  170.         inc a
  171.         ld (detectcpuspeed.spincount),a
  172.         pop af
  173.         ei
  174.         ret
  175.  
  176. isbomgemoon
  177. ;output: zf=0 is BomgeMoon flag is set
  178.         ld hl,(bomgemoonsettings)
  179.         ld a,l
  180.         or h
  181.         ret z
  182.         ld a,(hl)
  183.         cp '0'
  184.         ret
  185.  
  186. detectmoonsound
  187.         ld hl,detectingmoonsoundstr
  188.         call print_hl
  189.         call ismoonsoundpresent
  190.         ld hl,notfoundstr
  191.         jp nz,print_hl
  192.         call opl4init
  193.         call isbomgemoon
  194.         jr z,.detectwaveports
  195.         ld hl,devicebomgemoon
  196.         ld (devicelist.moonsoundstraddr),hl
  197.         ld a,1
  198.         ld (gpsettings.moonsoundstatus),a
  199.         ld hl,bomgemoonstr
  200.         jp print_hl
  201. .detectwaveports
  202.         ld bc,9
  203.         ld d,0
  204.         ld hl,0x1200
  205.         ld ix,browserpanel
  206.         call opl4readmemory
  207.         ld b,9
  208.         ld de,rom001200
  209.         ld hl,gpsettings.moonsoundstatus
  210. .cmploop
  211.         ld a,(de)
  212.         cp (ix)
  213.         jr nz,.waveportsfailed
  214.         inc de
  215.         inc ix
  216.         djnz .cmploop
  217.         ld (hl),2
  218.         ld hl,foundstr
  219.         jp print_hl
  220. .waveportsfailed
  221.         ld (hl),1
  222.         ld hl,firmwareerrorstr
  223.         call print_hl
  224.         ld hl,pressanykeystr
  225.         call print_hl
  226.         YIELDGETKEYLOOP
  227.         ret
  228.  
  229. detecttfm
  230.         ld hl,detectingtfmstr
  231.         call print_hl
  232.         call turnturbooff
  233.         call istfmpresent_notimer
  234.         push af
  235.         call turnturboon
  236.         pop af
  237.         ld hl,notfoundstr
  238.         jp nz,print_hl
  239.         ld a,1
  240.         ld (gpsettings.tfmstatus),a
  241.         ld hl,foundstr
  242.         jp print_hl
  243.  
  244. trywritingopm
  245.         dec a
  246.         jr nz,$-1
  247.         ld bc,OPM0_REG
  248.         out (c),e
  249.         ld bc,OPM1_REG
  250.         out (c),e
  251.         dec a
  252.         jr nz,$-1
  253.         ld bc,OPM0_DAT
  254.         out (c),d
  255.         ld bc,OPM1_DAT
  256.         out (c),d
  257.         ret
  258.  
  259. detectopm
  260.         ld hl,detectingopmstr
  261.         call print_hl
  262. ;check for non-zero as an early exit condition
  263.         ld bc,OPM0_DAT
  264.         in a,(c)
  265.         or a
  266.         ld hl,notfoundstr
  267.         jp nz,print_hl
  268. ;start timer
  269.         ld de,0xff12
  270.         call trywritingopm
  271.         ld de,0x2a14
  272.         call trywritingopm
  273. ;wait for the timer to finish
  274.         YIELD
  275.         YIELD
  276. ;check the timer flags
  277.         ld bc,OPM0_DAT
  278.         in a,(c)
  279.         cp 2
  280.         ld hl,notfoundstr
  281.         jp nz,print_hl
  282.         ld bc,OPM1_DAT
  283.         in a,(c)
  284.         cp 2
  285.         ld hl,founddualchipstr
  286.         jr z,.hasdualopm
  287.         call opmdisablechip1
  288.         ld hl,foundstr
  289.         ld a,1
  290. .hasdualopm
  291.         ld (gpsettings.opmstatus),a
  292.         call print_hl
  293.         jp opmstoptimers
  294.  
  295. trywritingopna1
  296.         dec a
  297.         jr nz,$-1
  298.         ld bc,OPNA1_REG
  299.         out (c),e
  300.         dec a
  301.         jr nz,$-1
  302.         ld bc,OPNA1_DAT
  303.         out (c),d
  304.         ret
  305.  
  306. detectopna
  307.         ld hl,detectingopnastr
  308.         call print_hl
  309. ;check for non-zero as an early exit condition
  310.         ld bc,OPNA1_REG
  311.         in a,(c)
  312.         or a
  313.         ld hl,notfoundstr
  314.         jp nz,print_hl
  315.         ld de,0xff26
  316.         call trywritingopna1
  317.         ld de,0x2a27
  318.         call trywritingopna1
  319. ;wait for the timer to finish
  320.         YIELD
  321.         YIELD
  322. ;check the timer flags
  323.         ld bc,OPNA1_REG
  324.         in a,(c)
  325.         cp 2
  326.         ld hl,notfoundstr
  327.         jp nz,print_hl
  328.         ld a,1
  329.         ld (gpsettings.opnastatus),a
  330.         ld de,0x3027
  331.         call trywritingopna1
  332.         ld de,0x0027
  333.         call trywritingopna1
  334.         ld hl,foundstr
  335.         jp print_hl
  336.  
  337. trywritingmoonsoundfm1
  338.         djnz $
  339.         ld a,e
  340.         out (MOON_REG1),a
  341.         djnz $
  342.         ld a,d
  343.         out (MOON_DAT1),a
  344.         ret
  345.  
  346. ismoonsoundpresent
  347. ;out: zf=1 if Moonsound is present, zf=0 if not
  348.         switch_to_pcm_ports_c2_c3
  349. ;check for 255 as an early exit condition
  350.         in a,(MOON_STAT)
  351.         add a,1
  352.         sbc a,a
  353.         ret nz
  354. ;read the status second time, now expect all bits clear
  355.         in a,(MOON_STAT)
  356.         or a
  357.         ret nz
  358. ;start timer
  359.         ld de,0xff03
  360.         call trywritingmoonsoundfm1
  361.         ld de,0x4204
  362.         call trywritingmoonsoundfm1
  363.         ld d,0x80
  364.         call trywritingmoonsoundfm1
  365. ;wait for the timer to finish
  366.         YIELD
  367.         YIELD
  368. ;check the timer flags
  369.         in a,(MOON_STAT)
  370.         cp 0xa0
  371.         ret nz
  372. ;there must be MoonSound in this system
  373.         call opl4stoptimers
  374.         xor a
  375.         ret
  376.  
  377. trywritingtfm1
  378.         dec a
  379.         jr nz,$-1
  380.         ld bc,OPN_REG
  381.         out (c),e
  382.         dec a
  383.         jr nz,$-1
  384.         ld bc,OPN_DAT
  385.         out (c),d
  386.         ret
  387.  
  388. istfmpresent_notimer
  389.         ld bc,OPN_REG
  390.         ld a,%11111100
  391.         out (c),a
  392.         ld de,0xff00
  393.         call trywritingtfm1
  394.         YIELD
  395.         YIELD
  396.         ld bc,OPN_REG
  397.         in f,(c)
  398.         ret m
  399.         xor a
  400.         ret
  401.  
  402. closeexistingplayer
  403. ;d = current pid
  404.         ld e,1
  405. .searchloop
  406.         ld a,e
  407.         cp d
  408.         jr z,.nextprocess
  409.         push de
  410.         OS_GETAPPMAINPAGES ;d,e,h,l=pages in 0000,4000,8000,c000
  411.         or a
  412.         ld a,d
  413.         pop de
  414.         jr nz,.nextprocess
  415.         push de
  416.         SETPGC000
  417.         ld hl,0xc000+COMMANDLINE
  418.         ld de,fullpathbuffer
  419.         ld bc,COMMANDLINE_sz
  420.         ldir
  421.         ld hl,fullpathbuffer
  422.         call skipword_hl
  423.         ld (hl),0
  424.         ld hl,fullpathbuffer
  425.         ld c,'/'
  426.         call findlastchar ;out: de = after last slash or start
  427.         call isplayer
  428.         pop de
  429.         jr z,.foundplayer
  430. .nextprocess
  431.         inc e
  432.         ld a,e
  433.         inc a
  434.         jr nz,.searchloop
  435.         ret
  436. .foundplayer
  437.         xor a
  438.         ld (0xc000+COMMANDLINE),a
  439.         push de
  440.         ld hl,closingplayerstr
  441.         call print_hl
  442.         pop de
  443. .waitloop
  444.         push de
  445.         YIELD
  446.         YIELD
  447.         YIELD
  448.         YIELD
  449.         OS_GETAPPMAINPAGES
  450.         pop de
  451.         or a
  452.         jr z,.waitloop
  453.         ret
  454.  
  455. isplayer
  456. ;de = command line file name
  457. ;out: zf=1 if gp, zf=0 otherwise
  458.         ld a,(de)
  459.         call tolower
  460.         cp 'g'
  461.         ret nz
  462.         inc de
  463.         ld a,(de)
  464.         call tolower
  465.         cp 'p'
  466.         ret nz
  467.         inc de
  468.         ld a,(de)
  469.         or a
  470.         ret z
  471.         cp '.'
  472.         ret
  473.  
  474. closingplayerstr
  475.         db "Closing old player instance...\r\n",0
  476. detectingmoonsoundstr
  477.         db "Detecting MoonSound...",0
  478. detectingtfmstr
  479.         db "Detecting TurboSound FM...",0
  480. detectingopmstr
  481.         db "Detecting YM2151...",0
  482. detectingopnastr
  483.         db "Detecting YM2608...",0
  484. notfoundstr
  485.         db "no device!\r\n",0
  486. foundstr
  487.         db "found!\r\n",0
  488. bomgemoonstr
  489.         db "OPL3\r\n",0
  490. founddualchipstr
  491.         db "2x\r\n",0
  492. detectingcpustr
  493.         db "Running on...",0
  494. cpufpgastr
  495.         db "FPGA\r\n",0
  496. cpuevostr
  497.         db "ZX Evolution\r\n",0
  498. cpuatmstr
  499.         db "ATM\r\n",0
  500. rom001200
  501.         db "Copyright"
  502. firmwareerrorstr
  503.         db "firmware problem!\r\nPlease update ZXM-MoonSound firmware to revision 1.01\r\n"
  504.         db "https://www.dropbox.com/s/1e0b2197emrhzos/zxm_moonsound01_frm0101.zip\r\n"
  505.         db "Or set BomgeMoon=1 in bin\\gp\\gp.ini to skip OPL4 ports detection.",0
  506.