engine_2.asm 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736
  1. ; The second of three duplicated sound engines.
  2. Audio2_UpdateMusic::
  3. ld c, Ch0
  4. .loop
  5. ld b, $0
  6. ld hl, wChannelSoundIDs
  7. add hl, bc
  8. ld a, [hl]
  9. and a
  10. jr z, .nextChannel
  11. ld a, c
  12. cp Ch4
  13. jr nc, .applyAffects ; if sfx channel
  14. ld a, [wMuteAudioAndPauseMusic]
  15. and a
  16. jr z, .applyAffects
  17. bit 7, a
  18. jr nz, .nextChannel
  19. set 7, a
  20. ld [wMuteAudioAndPauseMusic], a
  21. xor a
  22. ld [rNR51], a
  23. ld [rNR30], a
  24. ld a, $80
  25. ld [rNR30], a
  26. jr .nextChannel
  27. .applyAffects
  28. call Audio2_ApplyMusicAffects
  29. .nextChannel
  30. ld a, c
  31. inc c
  32. cp Ch7
  33. jr nz, .loop
  34. ret
  35. ; this routine checks flags for music effects currently applied
  36. ; to the channel and calls certain functions based on flags.
  37. ; known flags for wChannelFlags1:
  38. ; 0: toggleperfectpitch has been used
  39. ; 1: call has been used
  40. ; 3: a toggle used only by this routine for vibrato
  41. ; 4: pitchbend flag
  42. ; 6: dutycycle flag
  43. Audio2_ApplyMusicAffects:
  44. ld b, $0
  45. ld hl, wChannelNoteDelayCounters ; delay until next note
  46. add hl, bc
  47. ld a, [hl]
  48. cp $1 ; if the delay is 1, play next note
  49. jp z, Audio2_PlayNextNote
  50. dec a ; otherwise, decrease the delay timer
  51. ld [hl], a
  52. ld a, c
  53. cp Ch4
  54. jr nc, .startChecks ; if a sfx channel
  55. ld hl, wChannelSoundIDs + Ch4
  56. add hl, bc
  57. ld a, [hl]
  58. and a
  59. jr z, .startChecks
  60. ret
  61. .startChecks
  62. ld hl, wChannelFlags1
  63. add hl, bc
  64. bit 6, [hl] ; dutycycle
  65. jr z, .checkForExecuteMusic
  66. call Audio2_ApplyDutyCycle
  67. .checkForExecuteMusic
  68. ld b, $0
  69. ld hl, wChannelFlags2
  70. add hl, bc
  71. bit 0, [hl]
  72. jr nz, .checkForPitchBend
  73. ld hl, wChannelFlags1
  74. add hl, bc
  75. bit 2, [hl]
  76. jr nz, .disablePitchBendVibrato
  77. .checkForPitchBend
  78. ld hl, wChannelFlags1
  79. add hl, bc
  80. bit 4, [hl] ; pitchbend
  81. jr z, .checkVibratoDelay
  82. jp Audio2_ApplyPitchBend
  83. .checkVibratoDelay
  84. ld hl, wChannelVibratoDelayCounters ; vibrato delay
  85. add hl, bc
  86. ld a, [hl]
  87. and a ; check if delay is over
  88. jr z, .checkForVibrato
  89. dec [hl] ; otherwise, dec delay
  90. .disablePitchBendVibrato
  91. ret
  92. .checkForVibrato
  93. ld hl, wChannelVibratoExtents ; vibrato rate
  94. add hl, bc
  95. ld a, [hl]
  96. and a
  97. jr nz, .vibrato
  98. ret ; no vibrato
  99. .vibrato
  100. ld d, a
  101. ld hl, wChannelVibratoRates
  102. add hl, bc
  103. ld a, [hl]
  104. and $f
  105. and a
  106. jr z, .vibratoAlreadyDone
  107. dec [hl] ; apply vibrato pitch change
  108. ret
  109. .vibratoAlreadyDone
  110. ld a, [hl]
  111. swap [hl]
  112. or [hl]
  113. ld [hl], a ; reset the vibrato value and start again
  114. ld hl, wChannelFrequencyLowBytes
  115. add hl, bc
  116. ld e, [hl] ; get note pitch
  117. ld hl, wChannelFlags1
  118. add hl, bc
  119. bit 3, [hl] ; this is the only code that sets/resets bit three so
  120. jr z, .unset ; it continuously alternates which path it takes
  121. res 3, [hl]
  122. ld a, d
  123. and $f
  124. ld d, a
  125. ld a, e
  126. sub d
  127. jr nc, .noCarry
  128. ld a, $0
  129. .noCarry
  130. jr .done
  131. .unset
  132. set 3, [hl]
  133. ld a, d
  134. and $f0
  135. swap a
  136. add e
  137. jr nc, .done
  138. ld a, $ff
  139. .done
  140. ld d, a
  141. ld b, $3
  142. call Audio2_21ff7
  143. ld [hl], d
  144. ret
  145. ; this routine executes all music commands that take up no time,
  146. ; like tempo changes, duty changes etc. and doesn't return
  147. ; until the first note is reached
  148. Audio2_PlayNextNote:
  149. ld hl, wChannelVibratoDelayCounterReloadValues
  150. add hl, bc
  151. ld a, [hl]
  152. ld hl, wChannelVibratoDelayCounters
  153. add hl, bc
  154. ld [hl], a
  155. ld hl, wChannelFlags1
  156. add hl, bc
  157. res 4, [hl]
  158. res 5, [hl]
  159. ld a, c
  160. cp Ch4
  161. jr nz, .beginChecks
  162. ld a, [wLowHealthAlarm] ;low health alarm enabled?
  163. bit 7, a
  164. ret nz
  165. .beginChecks
  166. call Audio2_endchannel
  167. ret
  168. Audio2_endchannel:
  169. call Audio2_GetNextMusicByte
  170. ld d, a
  171. cp $ff ; is this command an endchannel?
  172. jp nz, Audio2_callchannel ; no
  173. ld b, $0 ; yes
  174. ld hl, wChannelFlags1
  175. add hl, bc
  176. bit 1, [hl]
  177. jr nz, .returnFromCall
  178. ld a, c
  179. cp Ch3
  180. jr nc, .noiseOrSfxChannel
  181. jr .asm_219c0
  182. .noiseOrSfxChannel
  183. res 2, [hl]
  184. ld hl, wChannelFlags2
  185. add hl, bc
  186. res 0, [hl]
  187. cp Ch6
  188. jr nz, .notSfxChannel3
  189. ld a, $0
  190. ld [rNR30], a
  191. ld a, $80
  192. ld [rNR30], a
  193. .notSfxChannel3
  194. jr nz, .asm_219a3
  195. ld a, [wDisableChannelOutputWhenSfxEnds]
  196. and a
  197. jr z, .asm_219a3
  198. xor a
  199. ld [wDisableChannelOutputWhenSfxEnds], a
  200. jr .asm_219c0
  201. .asm_219a3
  202. jr .asm_219c9
  203. .returnFromCall
  204. res 1, [hl]
  205. ld d, $0
  206. ld a, c
  207. add a
  208. ld e, a
  209. ld hl, wChannelCommandPointers
  210. add hl, de
  211. push hl ; store current channel address
  212. ld hl, wChannelReturnAddresses
  213. add hl, de
  214. ld e, l
  215. ld d, h
  216. pop hl
  217. ld a, [de]
  218. ld [hli], a
  219. inc de
  220. ld a, [de]
  221. ld [hl], a ; loads channel address to return to
  222. jp Audio2_endchannel
  223. .asm_219c0
  224. ld hl, Unknown_222de
  225. add hl, bc
  226. ld a, [rNR51]
  227. and [hl]
  228. ld [rNR51], a
  229. .asm_219c9
  230. ld a, [wChannelSoundIDs + Ch4]
  231. cp $14
  232. jr nc, .asm_219d2
  233. jr .asm_219ef
  234. .asm_219d2
  235. ld a, [wChannelSoundIDs + Ch4]
  236. cp $86
  237. jr z, .asm_219ef
  238. jr c, .asm_219dd
  239. jr .asm_219ef
  240. .asm_219dd
  241. ld a, c
  242. cp Ch4
  243. jr z, .asm_219e6
  244. call Audio2_21e6d
  245. ret c
  246. .asm_219e6
  247. ld a, [wSavedVolume]
  248. ld [rNR50], a
  249. xor a
  250. ld [wSavedVolume], a
  251. .asm_219ef
  252. ld hl, wChannelSoundIDs
  253. add hl, bc
  254. ld [hl], b
  255. ret
  256. Audio2_callchannel:
  257. cp $fd ; is this command a callchannel?
  258. jp nz, Audio2_loopchannel ; no
  259. call Audio2_GetNextMusicByte ; yes
  260. push af
  261. call Audio2_GetNextMusicByte
  262. ld d, a
  263. pop af
  264. ld e, a
  265. push de ; store pointer
  266. ld d, $0
  267. ld a, c
  268. add a
  269. ld e, a
  270. ld hl, wChannelCommandPointers
  271. add hl, de
  272. push hl
  273. ld hl, wChannelReturnAddresses
  274. add hl, de
  275. ld e, l
  276. ld d, h
  277. pop hl
  278. ld a, [hli]
  279. ld [de], a
  280. inc de
  281. ld a, [hld]
  282. ld [de], a ; copy current channel address
  283. pop de
  284. ld [hl], e
  285. inc hl
  286. ld [hl], d ; overwrite current address with pointer
  287. ld b, $0
  288. ld hl, wChannelFlags1
  289. add hl, bc
  290. set 1, [hl] ; set the call flag
  291. jp Audio2_endchannel
  292. Audio2_loopchannel:
  293. cp $fe ; is this command a loopchannel?
  294. jp nz, Audio2_notetype ; no
  295. call Audio2_GetNextMusicByte ; yes
  296. ld e, a
  297. and a
  298. jr z, .infiniteLoop
  299. ld b, $0
  300. ld hl, wChannelLoopCounters
  301. add hl, bc
  302. ld a, [hl]
  303. cp e
  304. jr nz, .loopAgain
  305. ld a, $1 ; if no more loops to make,
  306. ld [hl], a
  307. call Audio2_GetNextMusicByte ; skip pointer
  308. call Audio2_GetNextMusicByte
  309. jp Audio2_endchannel
  310. .loopAgain ; inc loop count
  311. inc a
  312. ld [hl], a
  313. ; fall through
  314. .infiniteLoop ; overwrite current address with pointer
  315. call Audio2_GetNextMusicByte
  316. push af
  317. call Audio2_GetNextMusicByte
  318. ld b, a
  319. ld d, $0
  320. ld a, c
  321. add a
  322. ld e, a
  323. ld hl, wChannelCommandPointers
  324. add hl, de
  325. pop af
  326. ld [hli], a
  327. ld [hl], b
  328. jp Audio2_endchannel
  329. Audio2_notetype:
  330. and $f0
  331. cp $d0 ; is this command a notetype?
  332. jp nz, Audio2_toggleperfectpitch ; no
  333. ld a, d ; yes
  334. and $f
  335. ld b, $0
  336. ld hl, wChannelNoteSpeeds
  337. add hl, bc
  338. ld [hl], a ; store low nibble as speed
  339. ld a, c
  340. cp Ch3
  341. jr z, .noiseChannel ; noise channel has 0 params
  342. call Audio2_GetNextMusicByte
  343. ld d, a
  344. ld a, c
  345. cp Ch2
  346. jr z, .musicChannel3
  347. cp Ch6
  348. jr nz, .notChannel3
  349. ld hl, wSfxWaveInstrument
  350. jr .sfxChannel3
  351. .musicChannel3
  352. ld hl, wMusicWaveInstrument
  353. .sfxChannel3
  354. ld a, d
  355. and $f
  356. ld [hl], a ; store low nibble of param as duty
  357. ld a, d
  358. and $30
  359. sla a
  360. ld d, a
  361. ; fall through
  362. ; if channel 3, store high nibble as volume
  363. ; else, store volume (high nibble) and fade (low nibble)
  364. .notChannel3
  365. ld b, $0
  366. ld hl, wChannelVolumes
  367. add hl, bc
  368. ld [hl], d
  369. .noiseChannel
  370. jp Audio2_endchannel
  371. Audio2_toggleperfectpitch:
  372. ld a, d
  373. cp $e8 ; is this command a toggleperfectpitch?
  374. jr nz, Audio2_vibrato ; no
  375. ld b, $0 ; yes
  376. ld hl, wChannelFlags1
  377. add hl, bc
  378. ld a, [hl]
  379. xor $1
  380. ld [hl], a ; flip bit 0 of wChannelFlags1
  381. jp Audio2_endchannel
  382. Audio2_vibrato:
  383. cp $ea ; is this command a vibrato?
  384. jr nz, Audio2_pitchbend ; no
  385. call Audio2_GetNextMusicByte ; yes
  386. ld b, $0
  387. ld hl, wChannelVibratoDelayCounters
  388. add hl, bc
  389. ld [hl], a ; store delay
  390. ld hl, wChannelVibratoDelayCounterReloadValues
  391. add hl, bc
  392. ld [hl], a ; store delay
  393. call Audio2_GetNextMusicByte
  394. ld d, a
  395. and $f0
  396. swap a
  397. ld b, $0
  398. ld hl, wChannelVibratoExtents
  399. add hl, bc
  400. srl a
  401. ld e, a
  402. adc b
  403. swap a
  404. or e
  405. ld [hl], a ; store rate as both high and low nibbles
  406. ld a, d
  407. and $f
  408. ld d, a
  409. ld hl, wChannelVibratoRates
  410. add hl, bc
  411. swap a
  412. or d
  413. ld [hl], a ; store depth as both high and low nibbles
  414. jp Audio2_endchannel
  415. Audio2_pitchbend:
  416. cp $eb ; is this command a pitchbend?
  417. jr nz, Audio2_duty ; no
  418. call Audio2_GetNextMusicByte ; yes
  419. ld b, $0
  420. ld hl, wChannelPitchBendLengthModifiers
  421. add hl, bc
  422. ld [hl], a ; store first param
  423. call Audio2_GetNextMusicByte
  424. ld d, a
  425. and $f0
  426. swap a
  427. ld b, a
  428. ld a, d
  429. and $f
  430. call Audio2_22017
  431. ld b, $0
  432. ld hl, wChannelPitchBendTargetFrequencyHighBytes
  433. add hl, bc
  434. ld [hl], d ; store unknown part of second param
  435. ld hl, wChannelPitchBendTargetFrequencyLowBytes
  436. add hl, bc
  437. ld [hl], e ; store unknown part of second param
  438. ld b, $0
  439. ld hl, wChannelFlags1
  440. add hl, bc
  441. set 4, [hl] ; set pitchbend flag
  442. call Audio2_GetNextMusicByte
  443. ld d, a
  444. jp Audio2_notelength
  445. Audio2_duty:
  446. cp $ec ; is this command a duty?
  447. jr nz, Audio2_tempo ; no
  448. call Audio2_GetNextMusicByte ; yes
  449. rrca
  450. rrca
  451. and $c0
  452. ld b, $0
  453. ld hl, wChannelDuties
  454. add hl, bc
  455. ld [hl], a ; store duty
  456. jp Audio2_endchannel
  457. Audio2_tempo:
  458. cp $ed ; is this command a tempo?
  459. jr nz, Audio2_stereopanning ; no
  460. ld a, c ; yes
  461. cp Ch4
  462. jr nc, .sfxChannel
  463. call Audio2_GetNextMusicByte
  464. ld [wMusicTempo], a ; store first param
  465. call Audio2_GetNextMusicByte
  466. ld [wMusicTempo + 1], a ; store second param
  467. xor a
  468. ld [wChannelNoteDelayCountersFractionalPart], a ; clear RAM
  469. ld [wChannelNoteDelayCountersFractionalPart + 1], a
  470. ld [wChannelNoteDelayCountersFractionalPart + 2], a
  471. ld [wChannelNoteDelayCountersFractionalPart + 3], a
  472. jr .musicChannelDone
  473. .sfxChannel
  474. call Audio2_GetNextMusicByte
  475. ld [wSfxTempo], a ; store first param
  476. call Audio2_GetNextMusicByte
  477. ld [wSfxTempo + 1], a ; store second param
  478. xor a
  479. ld [wChannelNoteDelayCountersFractionalPart + 4], a ; clear RAM
  480. ld [wChannelNoteDelayCountersFractionalPart + 5], a
  481. ld [wChannelNoteDelayCountersFractionalPart + 6], a
  482. ld [wChannelNoteDelayCountersFractionalPart + 7], a
  483. .musicChannelDone
  484. jp Audio2_endchannel
  485. Audio2_stereopanning:
  486. cp $ee ; is this command a stereopanning?
  487. jr nz, Audio2_unknownmusic0xef ; no
  488. call Audio2_GetNextMusicByte ; yes
  489. ld [wStereoPanning], a ; store panning
  490. jp Audio2_endchannel
  491. ; this appears to never be used
  492. Audio2_unknownmusic0xef:
  493. cp $ef ; is this command an unknownmusic0xef?
  494. jr nz, Audio2_dutycycle ; no
  495. call Audio2_GetNextMusicByte ; yes
  496. push bc
  497. call Audio2_PlaySound
  498. pop bc
  499. ld a, [wDisableChannelOutputWhenSfxEnds]
  500. and a
  501. jr nz, .skip
  502. ld a, [wChannelSoundIDs + Ch7]
  503. ld [wDisableChannelOutputWhenSfxEnds], a
  504. xor a
  505. ld [wChannelSoundIDs + Ch7], a
  506. .skip
  507. jp Audio2_endchannel
  508. Audio2_dutycycle:
  509. cp $fc ; is this command a dutycycle?
  510. jr nz, Audio2_volume ; no
  511. call Audio2_GetNextMusicByte ; yes
  512. ld b, $0
  513. ld hl, wChannelDutyCycles
  514. add hl, bc
  515. ld [hl], a ; store full cycle
  516. and $c0
  517. ld hl, wChannelDuties
  518. add hl, bc
  519. ld [hl], a ; store first duty
  520. ld hl, wChannelFlags1
  521. add hl, bc
  522. set 6, [hl] ; set dutycycle flag
  523. jp Audio2_endchannel
  524. Audio2_volume:
  525. cp $f0 ; is this command a volume?
  526. jr nz, Audio2_executemusic ; no
  527. call Audio2_GetNextMusicByte ; yes
  528. ld [rNR50], a ; store volume
  529. jp Audio2_endchannel
  530. Audio2_executemusic:
  531. cp $f8 ; is this command an executemusic?
  532. jr nz, Audio2_octave ; no
  533. ld b, $0 ; yes
  534. ld hl, wChannelFlags2
  535. add hl, bc
  536. set 0, [hl]
  537. jp Audio2_endchannel
  538. Audio2_octave:
  539. and $f0
  540. cp $e0 ; is this command an octave?
  541. jr nz, Audio2_sfxnote ; no
  542. ld hl, wChannelOctaves ; yes
  543. ld b, $0
  544. add hl, bc
  545. ld a, d
  546. and $f
  547. ld [hl], a ; store low nibble as octave
  548. jp Audio2_endchannel
  549. ; sfxnote is either squarenote or noisenote depending on the channel
  550. Audio2_sfxnote:
  551. cp $20 ; is this command an sfxnote?
  552. jr nz, Audio2_pitchenvelope ; no
  553. ld a, c
  554. cp Ch3 ; is this a noise or sfx channel?
  555. jr c, Audio2_pitchenvelope ; no
  556. ld b, $0
  557. ld hl, wChannelFlags2
  558. add hl, bc
  559. bit 0, [hl]
  560. jr nz, Audio2_pitchenvelope ; no
  561. call Audio2_notelength
  562. ld d, a
  563. ld b, $0
  564. ld hl, wChannelDuties
  565. add hl, bc
  566. ld a, [hl]
  567. or d
  568. ld d, a
  569. ld b, $1
  570. call Audio2_21ff7
  571. ld [hl], d
  572. call Audio2_GetNextMusicByte
  573. ld d, a
  574. ld b, $2
  575. call Audio2_21ff7
  576. ld [hl], d
  577. call Audio2_GetNextMusicByte
  578. ld e, a
  579. ld a, c
  580. cp Ch7
  581. ld a, $0
  582. jr z, .sfxNoiseChannel ; only two params for noise channel
  583. push de
  584. call Audio2_GetNextMusicByte
  585. pop de
  586. .sfxNoiseChannel
  587. ld d, a
  588. push de
  589. call Audio2_21daa
  590. call Audio2_21d79
  591. pop de
  592. call Audio2_21dcc
  593. ret
  594. Audio2_pitchenvelope:
  595. ld a, c
  596. cp Ch4
  597. jr c, Audio2_note ; if not a sfx
  598. ld a, d
  599. cp $10 ; is this command a pitchenvelope?
  600. jr nz, Audio2_note ; no
  601. ld b, $0
  602. ld hl, wChannelFlags2
  603. add hl, bc
  604. bit 0, [hl]
  605. jr nz, Audio2_note ; no
  606. call Audio2_GetNextMusicByte ; yes
  607. ld [rNR10], a
  608. jp Audio2_endchannel
  609. Audio2_note:
  610. ld a, c
  611. cp Ch3
  612. jr nz, Audio2_notelength ; if not noise channel
  613. ld a, d
  614. and $f0
  615. cp $b0 ; is this command a dnote?
  616. jr z, Audio2_dnote ; yes
  617. jr nc, Audio2_notelength ; no
  618. swap a
  619. ld b, a
  620. ld a, d
  621. and $f
  622. ld d, a
  623. ld a, b
  624. push de
  625. push bc
  626. jr asm_21c7e
  627. Audio2_dnote:
  628. ld a, d
  629. and $f
  630. push af
  631. push bc
  632. call Audio2_GetNextMusicByte ; get dnote instrument
  633. asm_21c7e
  634. ld d, a
  635. ld a, [wDisableChannelOutputWhenSfxEnds]
  636. and a
  637. jr nz, .asm_21c89
  638. ld a, d
  639. call Audio2_PlaySound
  640. .asm_21c89
  641. pop bc
  642. pop de
  643. Audio2_notelength:
  644. ld a, d
  645. push af
  646. and $f
  647. inc a
  648. ld b, $0
  649. ld e, a ; store note length (in 16ths)
  650. ld d, b
  651. ld hl, wChannelNoteSpeeds
  652. add hl, bc
  653. ld a, [hl]
  654. ld l, b
  655. call Audio2_22006
  656. ld a, c
  657. cp Ch4
  658. jr nc, .sfxChannel
  659. ld a, [wMusicTempo]
  660. ld d, a
  661. ld a, [wMusicTempo + 1]
  662. ld e, a
  663. jr .skip
  664. .sfxChannel
  665. ld d, $1
  666. ld e, $0
  667. cp Ch7
  668. jr z, .skip ; if noise channel
  669. call Audio2_21e2f
  670. ld a, [wSfxTempo]
  671. ld d, a
  672. ld a, [wSfxTempo + 1]
  673. ld e, a
  674. .skip
  675. ld a, l
  676. ld b, $0
  677. ld hl, wChannelNoteDelayCountersFractionalPart
  678. add hl, bc
  679. ld l, [hl]
  680. call Audio2_22006
  681. ld e, l
  682. ld d, h
  683. ld hl, wChannelNoteDelayCountersFractionalPart
  684. add hl, bc
  685. ld [hl], e
  686. ld a, d
  687. ld hl, wChannelNoteDelayCounters
  688. add hl, bc
  689. ld [hl], a
  690. ld hl, wChannelFlags2
  691. add hl, bc
  692. bit 0, [hl]
  693. jr nz, Audio2_notepitch
  694. ld hl, wChannelFlags1
  695. add hl, bc
  696. bit 2, [hl]
  697. jr z, Audio2_notepitch
  698. pop hl
  699. ret
  700. Audio2_notepitch:
  701. pop af
  702. and $f0
  703. cp $c0 ; compare to rest
  704. jr nz, .notRest
  705. ld a, c
  706. cp Ch4
  707. jr nc, .sfxChannel
  708. ld hl, wChannelSoundIDs + Ch4
  709. add hl, bc
  710. ld a, [hl]
  711. and a
  712. jr nz, .done
  713. ; fall through
  714. .sfxChannel
  715. ld a, c
  716. cp Ch2
  717. jr z, .musicChannel3
  718. cp Ch6
  719. jr nz, .notSfxChannel3
  720. .musicChannel3
  721. ld b, $0
  722. ld hl, Unknown_222de
  723. add hl, bc
  724. ld a, [rNR51]
  725. and [hl]
  726. ld [rNR51], a
  727. jr .done
  728. .notSfxChannel3
  729. ld b, $2
  730. call Audio2_21ff7
  731. ld a, $8
  732. ld [hli], a
  733. inc hl
  734. ld a, $80
  735. ld [hl], a
  736. .done
  737. ret
  738. .notRest
  739. swap a
  740. ld b, $0
  741. ld hl, wChannelOctaves
  742. add hl, bc
  743. ld b, [hl]
  744. call Audio2_22017
  745. ld b, $0
  746. ld hl, wChannelFlags1
  747. add hl, bc
  748. bit 4, [hl]
  749. jr z, .asm_21d39
  750. call Audio2_21f4e
  751. .asm_21d39
  752. push de
  753. ld a, c
  754. cp Ch4
  755. jr nc, .skip ; if sfx channel
  756. ld hl, wChannelSoundIDs + Ch4
  757. ld d, $0
  758. ld e, a
  759. add hl, de
  760. ld a, [hl]
  761. and a
  762. jr nz, .asm_21d4c
  763. jr .skip
  764. .asm_21d4c
  765. pop de
  766. ret
  767. .skip
  768. ld b, $0
  769. ld hl, wChannelVolumes
  770. add hl, bc
  771. ld d, [hl]
  772. ld b, $2
  773. call Audio2_21ff7
  774. ld [hl], d
  775. call Audio2_21daa
  776. call Audio2_21d79
  777. pop de
  778. ld b, $0
  779. ld hl, wChannelFlags1
  780. add hl, bc
  781. bit 0, [hl] ; has toggleperfectpitch been used?
  782. jr z, .skip2
  783. inc e ; if yes, increment the pitch by 1
  784. jr nc, .skip2
  785. inc d
  786. .skip2
  787. ld hl, wChannelFrequencyLowBytes
  788. add hl, bc
  789. ld [hl], e
  790. call Audio2_21dcc
  791. ret
  792. Audio2_21d79:
  793. ld b, $0
  794. ld hl, Unknown_222e6
  795. add hl, bc
  796. ld a, [rNR51]
  797. or [hl]
  798. ld d, a
  799. ld a, c
  800. cp Ch7
  801. jr z, .sfxNoiseChannel
  802. cp Ch4
  803. jr nc, .skip ; if sfx channel
  804. ld hl, wChannelSoundIDs + Ch4
  805. add hl, bc
  806. ld a, [hl]
  807. and a
  808. jr nz, .skip
  809. .sfxNoiseChannel
  810. ld a, [wStereoPanning]
  811. ld hl, Unknown_222e6
  812. add hl, bc
  813. and [hl]
  814. ld d, a
  815. ld a, [rNR51]
  816. ld hl, Unknown_222de
  817. add hl, bc
  818. and [hl]
  819. or d
  820. ld d, a
  821. .skip
  822. ld a, d
  823. ld [rNR51], a
  824. ret
  825. Audio2_21daa:
  826. ld b, $0
  827. ld hl, wChannelNoteDelayCounters
  828. add hl, bc
  829. ld d, [hl]
  830. ld a, c
  831. cp Ch2
  832. jr z, .channel3 ; if music channel 3
  833. cp Ch6
  834. jr z, .channel3 ; if sfx channel 3
  835. ld a, d
  836. and $3f
  837. ld d, a
  838. ld hl, wChannelDuties
  839. add hl, bc
  840. ld a, [hl]
  841. or d
  842. ld d, a
  843. .channel3
  844. ld b, $1
  845. call Audio2_21ff7
  846. ld [hl], d
  847. ret
  848. Audio2_21dcc:
  849. ld a, c
  850. cp Ch2
  851. jr z, .channel3
  852. cp Ch6
  853. jr nz, .notSfxChannel3
  854. ; fall through
  855. .channel3
  856. push de
  857. ld de, wMusicWaveInstrument
  858. cp Ch2
  859. jr z, .musicChannel3
  860. ld de, wSfxWaveInstrument
  861. .musicChannel3
  862. ld a, [de]
  863. add a
  864. ld d, $0
  865. ld e, a
  866. ld hl, Audio2_WavePointers
  867. add hl, de
  868. ld e, [hl]
  869. inc hl
  870. ld d, [hl]
  871. ld hl, $ff30
  872. ld b, $f
  873. ld a, $0
  874. ld [rNR30], a
  875. .loop
  876. ld a, [de]
  877. inc de
  878. ld [hli], a
  879. ld a, b
  880. dec b
  881. and a
  882. jr nz, .loop
  883. ld a, $80
  884. ld [rNR30], a
  885. pop de
  886. .notSfxChannel3
  887. ld a, d
  888. or $80
  889. and $c7
  890. ld d, a
  891. ld b, $3
  892. call Audio2_21ff7
  893. ld [hl], e
  894. inc hl
  895. ld [hl], d
  896. ld a, c
  897. cp Ch4
  898. jr c, .musicChannel
  899. call Audio2_21e56
  900. .musicChannel
  901. ret
  902. Audio2_21e19:
  903. ld a, c
  904. cp Ch4
  905. jr nz, .asm_21e2e
  906. ld a, [wLowHealthAlarm]
  907. bit 7, a
  908. jr z, .asm_21e2e
  909. xor a
  910. ld [wFrequencyModifier], a
  911. ld a, $80
  912. ld [wTempoModifier], a
  913. .asm_21e2e
  914. ret
  915. Audio2_21e2f:
  916. call Audio2_21e8b
  917. jr c, .asm_21e39
  918. call Audio2_21e9f
  919. jr nc, .asm_21e4c
  920. .asm_21e39
  921. ld d, $0
  922. ld a, [wTempoModifier]
  923. add $80
  924. jr nc, .asm_21e43
  925. inc d
  926. .asm_21e43
  927. ld [wSfxTempo + 1], a
  928. ld a, d
  929. ld [wSfxTempo], a
  930. jr .asm_21e55
  931. .asm_21e4c
  932. xor a
  933. ld [wSfxTempo + 1], a
  934. ld a, $1
  935. ld [wSfxTempo], a
  936. .asm_21e55
  937. ret
  938. Audio2_21e56:
  939. call Audio2_21e8b
  940. jr c, .asm_21e60
  941. call Audio2_21e9f
  942. jr nc, .asm_21e6c
  943. .asm_21e60
  944. ld a, [wFrequencyModifier]
  945. add e
  946. jr nc, .asm_21e67
  947. inc d
  948. .asm_21e67
  949. dec hl
  950. ld e, a
  951. ld [hl], e
  952. inc hl
  953. ld [hl], d
  954. .asm_21e6c
  955. ret
  956. Audio2_21e6d:
  957. call Audio2_21e8b
  958. jr nc, .asm_21e88
  959. ld hl, wChannelCommandPointers
  960. ld e, c
  961. ld d, $0
  962. sla e
  963. rl d
  964. add hl, de
  965. ld a, [hl]
  966. sub $1
  967. ld [hl], a
  968. inc hl
  969. ld a, [hl]
  970. sbc $0
  971. ld [hl], a
  972. scf
  973. ret
  974. .asm_21e88
  975. scf
  976. ccf
  977. ret
  978. Audio2_21e8b:
  979. ld a, [wChannelSoundIDs + Ch4]
  980. cp $14
  981. jr nc, .asm_21e94
  982. jr .asm_21e9a
  983. .asm_21e94
  984. cp $86
  985. jr z, .asm_21e9a
  986. jr c, .asm_21e9d
  987. .asm_21e9a
  988. scf
  989. ccf
  990. ret
  991. .asm_21e9d
  992. scf
  993. ret
  994. Audio2_21e9f:
  995. ld a, [wChannelSoundIDs + Ch7]
  996. ld b, a
  997. ld a, [wChannelSoundIDs + Ch4]
  998. or b
  999. cp $9d
  1000. jr nc, .asm_21ead
  1001. jr .asm_21eb3
  1002. .asm_21ead
  1003. cp $ea
  1004. jr z, .asm_21eb3
  1005. jr c, .asm_21eb6
  1006. .asm_21eb3
  1007. scf
  1008. ccf
  1009. ret
  1010. .asm_21eb6
  1011. scf
  1012. ret
  1013. Audio2_ApplyPitchBend:
  1014. ld hl, wChannelFlags1
  1015. add hl, bc
  1016. bit 5, [hl]
  1017. jp nz, .asm_21eff
  1018. ld hl, wChannelPitchBendCurrentFrequencyLowBytes
  1019. add hl, bc
  1020. ld e, [hl]
  1021. ld hl, wChannelPitchBendCurrentFrequencyHighBytes
  1022. add hl, bc
  1023. ld d, [hl]
  1024. ld hl, wChannelPitchBendFrequencySteps
  1025. add hl, bc
  1026. ld l, [hl]
  1027. ld h, b
  1028. add hl, de
  1029. ld d, h
  1030. ld e, l
  1031. ld hl, wChannelPitchBendCurrentFrequencyFractionalPart
  1032. add hl, bc
  1033. push hl
  1034. ld hl, wChannelPitchBendFrequencyStepsFractionalPart
  1035. add hl, bc
  1036. ld a, [hl]
  1037. pop hl
  1038. add [hl]
  1039. ld [hl], a
  1040. ld a, $0
  1041. adc e
  1042. ld e, a
  1043. ld a, $0
  1044. adc d
  1045. ld d, a
  1046. ld hl, wChannelPitchBendTargetFrequencyHighBytes
  1047. add hl, bc
  1048. ld a, [hl]
  1049. cp d
  1050. jp c, .asm_21f45
  1051. jr nz, .asm_21f32
  1052. ld hl, wChannelPitchBendTargetFrequencyLowBytes
  1053. add hl, bc
  1054. ld a, [hl]
  1055. cp e
  1056. jp c, .asm_21f45
  1057. jr .asm_21f32
  1058. .asm_21eff
  1059. ld hl, wChannelPitchBendCurrentFrequencyLowBytes
  1060. add hl, bc
  1061. ld a, [hl]
  1062. ld hl, wChannelPitchBendCurrentFrequencyHighBytes
  1063. add hl, bc
  1064. ld d, [hl]
  1065. ld hl, wChannelPitchBendFrequencySteps
  1066. add hl, bc
  1067. ld e, [hl]
  1068. sub e
  1069. ld e, a
  1070. ld a, d
  1071. sbc b
  1072. ld d, a
  1073. ld hl, wChannelPitchBendFrequencyStepsFractionalPart
  1074. add hl, bc
  1075. ld a, [hl]
  1076. add a
  1077. ld [hl], a
  1078. ld a, e
  1079. sbc b
  1080. ld e, a
  1081. ld a, d
  1082. sbc b
  1083. ld d, a
  1084. ld hl, wChannelPitchBendTargetFrequencyHighBytes
  1085. add hl, bc
  1086. ld a, d
  1087. cp [hl]
  1088. jr c, .asm_21f45
  1089. jr nz, .asm_21f32
  1090. ld hl, wChannelPitchBendTargetFrequencyLowBytes
  1091. add hl, bc
  1092. ld a, e
  1093. cp [hl]
  1094. jr c, .asm_21f45
  1095. .asm_21f32
  1096. ld hl, wChannelPitchBendCurrentFrequencyLowBytes
  1097. add hl, bc
  1098. ld [hl], e
  1099. ld hl, wChannelPitchBendCurrentFrequencyHighBytes
  1100. add hl, bc
  1101. ld [hl], d
  1102. ld b, $3
  1103. call Audio2_21ff7
  1104. ld a, e
  1105. ld [hli], a
  1106. ld [hl], d
  1107. ret
  1108. .asm_21f45
  1109. ld hl, wChannelFlags1
  1110. add hl, bc
  1111. res 4, [hl]
  1112. res 5, [hl]
  1113. ret
  1114. Audio2_21f4e:
  1115. ld hl, wChannelPitchBendCurrentFrequencyHighBytes
  1116. add hl, bc
  1117. ld [hl], d
  1118. ld hl, wChannelPitchBendCurrentFrequencyLowBytes
  1119. add hl, bc
  1120. ld [hl], e
  1121. ld hl, wChannelNoteDelayCounters
  1122. add hl, bc
  1123. ld a, [hl]
  1124. ld hl, wChannelPitchBendLengthModifiers
  1125. add hl, bc
  1126. sub [hl]
  1127. jr nc, .asm_21f66
  1128. ld a, $1
  1129. .asm_21f66
  1130. ld [hl], a
  1131. ld hl, wChannelPitchBendTargetFrequencyLowBytes
  1132. add hl, bc
  1133. ld a, e
  1134. sub [hl]
  1135. ld e, a
  1136. ld a, d
  1137. sbc b
  1138. ld hl, wChannelPitchBendTargetFrequencyHighBytes
  1139. add hl, bc
  1140. sub [hl]
  1141. jr c, .asm_21f82
  1142. ld d, a
  1143. ld b, $0
  1144. ld hl, wChannelFlags1
  1145. add hl, bc
  1146. set 5, [hl]
  1147. jr .asm_21fa5
  1148. .asm_21f82
  1149. ld hl, wChannelPitchBendCurrentFrequencyHighBytes
  1150. add hl, bc
  1151. ld d, [hl]
  1152. ld hl, wChannelPitchBendCurrentFrequencyLowBytes
  1153. add hl, bc
  1154. ld e, [hl]
  1155. ld hl, wChannelPitchBendTargetFrequencyLowBytes
  1156. add hl, bc
  1157. ld a, [hl]
  1158. sub e
  1159. ld e, a
  1160. ld a, d
  1161. sbc b
  1162. ld d, a
  1163. ld hl, wChannelPitchBendTargetFrequencyHighBytes
  1164. add hl, bc
  1165. ld a, [hl]
  1166. sub d
  1167. ld d, a
  1168. ld b, $0
  1169. ld hl, wChannelFlags1
  1170. add hl, bc
  1171. res 5, [hl]
  1172. .asm_21fa5
  1173. ld hl, wChannelPitchBendLengthModifiers
  1174. add hl, bc
  1175. .asm_21fa9
  1176. inc b
  1177. ld a, e
  1178. sub [hl]
  1179. ld e, a
  1180. jr nc, .asm_21fa9
  1181. ld a, d
  1182. and a
  1183. jr z, .asm_21fb7
  1184. dec a
  1185. ld d, a
  1186. jr .asm_21fa9
  1187. .asm_21fb7
  1188. ld a, e
  1189. add [hl]
  1190. ld d, b
  1191. ld b, $0
  1192. ld hl, wChannelPitchBendFrequencySteps
  1193. add hl, bc
  1194. ld [hl], d
  1195. ld hl, wChannelPitchBendFrequencyStepsFractionalPart
  1196. add hl, bc
  1197. ld [hl], a
  1198. ld hl, wChannelPitchBendCurrentFrequencyFractionalPart
  1199. add hl, bc
  1200. ld [hl], a
  1201. ret
  1202. Audio2_ApplyDutyCycle:
  1203. ld b, $0
  1204. ld hl, wChannelDutyCycles
  1205. add hl, bc
  1206. ld a, [hl]
  1207. rlca
  1208. rlca
  1209. ld [hl], a
  1210. and $c0
  1211. ld d, a
  1212. ld b, $1
  1213. call Audio2_21ff7
  1214. ld a, [hl]
  1215. and $3f
  1216. or d
  1217. ld [hl], a
  1218. ret
  1219. Audio2_GetNextMusicByte:
  1220. ld d, $0
  1221. ld a, c
  1222. add a
  1223. ld e, a
  1224. ld hl, wChannelCommandPointers
  1225. add hl, de
  1226. ld a, [hli]
  1227. ld e, a
  1228. ld a, [hld]
  1229. ld d, a
  1230. ld a, [de] ; get next music command
  1231. inc de
  1232. ld [hl], e ; store address of next command
  1233. inc hl
  1234. ld [hl], d
  1235. ret
  1236. Audio2_21ff7:
  1237. ld a, c
  1238. ld hl, Unknown_222d6
  1239. add l
  1240. jr nc, .noCarry
  1241. inc h
  1242. .noCarry
  1243. ld l, a
  1244. ld a, [hl]
  1245. add b
  1246. ld l, a
  1247. ld h, $ff
  1248. ret
  1249. Audio2_22006:
  1250. ld h, $0
  1251. .loop
  1252. srl a
  1253. jr nc, .noCarry
  1254. add hl, de
  1255. .noCarry
  1256. sla e
  1257. rl d
  1258. and a
  1259. jr z, .done
  1260. jr .loop
  1261. .done
  1262. ret
  1263. Audio2_22017:
  1264. ld h, $0
  1265. ld l, a
  1266. add hl, hl
  1267. ld d, h
  1268. ld e, l
  1269. ld hl, Audio2_Pitches
  1270. add hl, de
  1271. ld e, [hl]
  1272. inc hl
  1273. ld d, [hl]
  1274. ld a, b
  1275. .loop
  1276. cp Ch7
  1277. jr z, .done
  1278. sra d
  1279. rr e
  1280. inc a
  1281. jr .loop
  1282. .done
  1283. ld a, $8
  1284. add d
  1285. ld d, a
  1286. ret
  1287. Audio2_PlaySound::
  1288. ld [wSoundID], a
  1289. cp $ff
  1290. jp z, Audio2_221f3
  1291. cp $e9
  1292. jp z, Audio2_2210d
  1293. jp c, Audio2_2210d
  1294. cp $fe
  1295. jr z, .asm_2204c
  1296. jp nc, Audio2_2210d
  1297. .asm_2204c
  1298. xor a
  1299. ld [wUnusedC000], a
  1300. ld [wDisableChannelOutputWhenSfxEnds], a
  1301. ld [wMusicTempo + 1], a
  1302. ld [wMusicWaveInstrument], a
  1303. ld [wSfxWaveInstrument], a
  1304. ld d, $8
  1305. ld hl, wChannelReturnAddresses
  1306. call FillAudioRAM2
  1307. ld hl, wChannelCommandPointers
  1308. call FillAudioRAM2
  1309. ld d, $4
  1310. ld hl, wChannelSoundIDs
  1311. call FillAudioRAM2
  1312. ld hl, wChannelFlags1
  1313. call FillAudioRAM2
  1314. ld hl, wChannelDuties
  1315. call FillAudioRAM2
  1316. ld hl, wChannelDutyCycles
  1317. call FillAudioRAM2
  1318. ld hl, wChannelVibratoDelayCounters
  1319. call FillAudioRAM2
  1320. ld hl, wChannelVibratoExtents
  1321. call FillAudioRAM2
  1322. ld hl, wChannelVibratoRates
  1323. call FillAudioRAM2
  1324. ld hl, wChannelFrequencyLowBytes
  1325. call FillAudioRAM2
  1326. ld hl, wChannelVibratoDelayCounterReloadValues
  1327. call FillAudioRAM2
  1328. ld hl, wChannelFlags2
  1329. call FillAudioRAM2
  1330. ld hl, wChannelPitchBendLengthModifiers
  1331. call FillAudioRAM2
  1332. ld hl, wChannelPitchBendFrequencySteps
  1333. call FillAudioRAM2
  1334. ld hl, wChannelPitchBendFrequencyStepsFractionalPart
  1335. call FillAudioRAM2
  1336. ld hl, wChannelPitchBendCurrentFrequencyFractionalPart
  1337. call FillAudioRAM2
  1338. ld hl, wChannelPitchBendCurrentFrequencyHighBytes
  1339. call FillAudioRAM2
  1340. ld hl, wChannelPitchBendCurrentFrequencyLowBytes
  1341. call FillAudioRAM2
  1342. ld hl, wChannelPitchBendTargetFrequencyHighBytes
  1343. call FillAudioRAM2
  1344. ld hl, wChannelPitchBendTargetFrequencyLowBytes
  1345. call FillAudioRAM2
  1346. ld a, $1
  1347. ld hl, wChannelLoopCounters
  1348. call FillAudioRAM2
  1349. ld hl, wChannelNoteDelayCounters
  1350. call FillAudioRAM2
  1351. ld hl, wChannelNoteSpeeds
  1352. call FillAudioRAM2
  1353. ld [wMusicTempo], a
  1354. ld a, $ff
  1355. ld [wStereoPanning], a
  1356. xor a
  1357. ld [rNR50], a
  1358. ld a, $8
  1359. ld [rNR10], a
  1360. ld a, $0
  1361. ld [rNR51], a
  1362. xor a
  1363. ld [rNR30], a
  1364. ld a, $80
  1365. ld [rNR30], a
  1366. ld a, $77
  1367. ld [rNR50], a
  1368. jp Audio2_2224e
  1369. Audio2_2210d:
  1370. ld l, a
  1371. ld e, a
  1372. ld h, $0
  1373. ld d, h
  1374. add hl, hl
  1375. add hl, de
  1376. ld de, SFX_Headers_2
  1377. add hl, de
  1378. ld a, h
  1379. ld [wSfxHeaderPointer], a
  1380. ld a, l
  1381. ld [wSfxHeaderPointer + 1], a
  1382. ld a, [hl]
  1383. and $c0
  1384. rlca
  1385. rlca
  1386. ld c, a
  1387. .asm_22126
  1388. ld d, c
  1389. ld a, c
  1390. add a
  1391. add c
  1392. ld c, a
  1393. ld b, $0
  1394. ld a, [wSfxHeaderPointer]
  1395. ld h, a
  1396. ld a, [wSfxHeaderPointer + 1]
  1397. ld l, a
  1398. add hl, bc
  1399. ld c, d
  1400. ld a, [hl]
  1401. and $f
  1402. ld e, a
  1403. ld d, $0
  1404. ld hl, wChannelSoundIDs
  1405. add hl, de
  1406. ld a, [hl]
  1407. and a
  1408. jr z, .asm_22162
  1409. ld a, e
  1410. cp $7
  1411. jr nz, .asm_22159
  1412. ld a, [wSoundID]
  1413. cp $14
  1414. jr nc, .asm_22152
  1415. ret
  1416. .asm_22152
  1417. ld a, [hl]
  1418. cp $14
  1419. jr z, .asm_22162
  1420. jr c, .asm_22162
  1421. .asm_22159
  1422. ld a, [wSoundID]
  1423. cp [hl]
  1424. jr z, .asm_22162
  1425. jr c, .asm_22162
  1426. ret
  1427. .asm_22162
  1428. xor a
  1429. push de
  1430. ld h, d
  1431. ld l, e
  1432. add hl, hl
  1433. ld d, h
  1434. ld e, l
  1435. ld hl, wChannelReturnAddresses
  1436. add hl, de
  1437. ld [hli], a
  1438. ld [hl], a
  1439. ld hl, wChannelCommandPointers
  1440. add hl, de
  1441. ld [hli], a
  1442. ld [hl], a
  1443. pop de
  1444. ld hl, wChannelSoundIDs
  1445. add hl, de
  1446. ld [hl], a
  1447. ld hl, wChannelFlags1
  1448. add hl, de
  1449. ld [hl], a
  1450. ld hl, wChannelDuties
  1451. add hl, de
  1452. ld [hl], a
  1453. ld hl, wChannelDutyCycles
  1454. add hl, de
  1455. ld [hl], a
  1456. ld hl, wChannelVibratoDelayCounters
  1457. add hl, de
  1458. ld [hl], a
  1459. ld hl, wChannelVibratoExtents
  1460. add hl, de
  1461. ld [hl], a
  1462. ld hl, wChannelVibratoRates
  1463. add hl, de
  1464. ld [hl], a
  1465. ld hl, wChannelFrequencyLowBytes
  1466. add hl, de
  1467. ld [hl], a
  1468. ld hl, wChannelVibratoDelayCounterReloadValues
  1469. add hl, de
  1470. ld [hl], a
  1471. ld hl, wChannelPitchBendLengthModifiers
  1472. add hl, de
  1473. ld [hl], a
  1474. ld hl, wChannelPitchBendFrequencySteps
  1475. add hl, de
  1476. ld [hl], a
  1477. ld hl, wChannelPitchBendFrequencyStepsFractionalPart
  1478. add hl, de
  1479. ld [hl], a
  1480. ld hl, wChannelPitchBendCurrentFrequencyFractionalPart
  1481. add hl, de
  1482. ld [hl], a
  1483. ld hl, wChannelPitchBendCurrentFrequencyHighBytes
  1484. add hl, de
  1485. ld [hl], a
  1486. ld hl, wChannelPitchBendCurrentFrequencyLowBytes
  1487. add hl, de
  1488. ld [hl], a
  1489. ld hl, wChannelPitchBendTargetFrequencyHighBytes
  1490. add hl, de
  1491. ld [hl], a
  1492. ld hl, wChannelPitchBendTargetFrequencyLowBytes
  1493. add hl, de
  1494. ld [hl], a
  1495. ld hl, wChannelFlags2
  1496. add hl, de
  1497. ld [hl], a
  1498. ld a, $1
  1499. ld hl, wChannelLoopCounters
  1500. add hl, de
  1501. ld [hl], a
  1502. ld hl, wChannelNoteDelayCounters
  1503. add hl, de
  1504. ld [hl], a
  1505. ld hl, wChannelNoteSpeeds
  1506. add hl, de
  1507. ld [hl], a
  1508. ld a, e
  1509. cp $4
  1510. jr nz, .asm_221ea
  1511. ld a, $8
  1512. ld [rNR10], a
  1513. .asm_221ea
  1514. ld a, c
  1515. and a
  1516. jp z, Audio2_2224e
  1517. dec c
  1518. jp .asm_22126
  1519. Audio2_221f3:
  1520. ld a, $80
  1521. ld [rNR52], a
  1522. ld [rNR30], a
  1523. xor a
  1524. ld [rNR51], a
  1525. ld [rNR32], a
  1526. ld a, $8
  1527. ld [rNR10], a
  1528. ld [rNR12], a
  1529. ld [rNR22], a
  1530. ld [rNR42], a
  1531. ld a, $40
  1532. ld [rNR14], a
  1533. ld [rNR24], a
  1534. ld [rNR44], a
  1535. ld a, $77
  1536. ld [rNR50], a
  1537. xor a
  1538. ld [wUnusedC000], a
  1539. ld [wDisableChannelOutputWhenSfxEnds], a
  1540. ld [wMuteAudioAndPauseMusic], a
  1541. ld [wMusicTempo + 1], a
  1542. ld [wSfxTempo + 1], a
  1543. ld [wMusicWaveInstrument], a
  1544. ld [wSfxWaveInstrument], a
  1545. ld d, $a0
  1546. ld hl, wChannelCommandPointers
  1547. call FillAudioRAM2
  1548. ld a, $1
  1549. ld d, $18
  1550. ld hl, wChannelNoteDelayCounters
  1551. call FillAudioRAM2
  1552. ld [wMusicTempo], a
  1553. ld [wSfxTempo], a
  1554. ld a, $ff
  1555. ld [wStereoPanning], a
  1556. ret
  1557. ; fills d bytes at hl with a
  1558. FillAudioRAM2:
  1559. ld b, d
  1560. .loop
  1561. ld [hli], a
  1562. dec b
  1563. jr nz, .loop
  1564. ret
  1565. Audio2_2224e:
  1566. ld a, [wSoundID]
  1567. ld l, a
  1568. ld e, a
  1569. ld h, $0
  1570. ld d, h
  1571. add hl, hl
  1572. add hl, de
  1573. ld de, SFX_Headers_2
  1574. add hl, de
  1575. ld e, l
  1576. ld d, h
  1577. ld hl, wChannelCommandPointers
  1578. ld a, [de] ; get channel number
  1579. ld b, a
  1580. rlca
  1581. rlca
  1582. and $3
  1583. ld c, a
  1584. ld a, b
  1585. and $f
  1586. ld b, c
  1587. inc b
  1588. inc de
  1589. ld c, $0
  1590. .asm_22270
  1591. cp c
  1592. jr z, .asm_22278
  1593. inc c
  1594. inc hl
  1595. inc hl
  1596. jr .asm_22270
  1597. .asm_22278
  1598. push hl
  1599. push bc
  1600. push af
  1601. ld b, $0
  1602. ld c, a
  1603. ld hl, wChannelSoundIDs
  1604. add hl, bc
  1605. ld a, [wSoundID]
  1606. ld [hl], a
  1607. pop af
  1608. cp $3
  1609. jr c, .asm_22291
  1610. ld hl, wChannelFlags1
  1611. add hl, bc
  1612. set 2, [hl]
  1613. .asm_22291
  1614. pop bc
  1615. pop hl
  1616. ld a, [de] ; get channel pointer
  1617. ld [hli], a
  1618. inc de
  1619. ld a, [de]
  1620. ld [hli], a
  1621. inc de
  1622. inc c
  1623. dec b
  1624. ld a, b
  1625. and a
  1626. ld a, [de]
  1627. inc de
  1628. jr nz, .asm_22270
  1629. ld a, [wSoundID]
  1630. cp $14
  1631. jr nc, .asm_222aa
  1632. jr .asm_222d4
  1633. .asm_222aa
  1634. ld a, [wSoundID]
  1635. cp $86
  1636. jr z, .asm_222d4
  1637. jr c, .asm_222b5
  1638. jr .asm_222d4
  1639. .asm_222b5
  1640. ld hl, wChannelSoundIDs + Ch4
  1641. ld [hli], a
  1642. ld [hli], a
  1643. ld [hli], a
  1644. ld [hl], a
  1645. ld hl, wChannelCommandPointers + Ch6 * 2 ; sfx noise channel pointer
  1646. ld de, Noise2_endchannel
  1647. ld [hl], e
  1648. inc hl
  1649. ld [hl], d ; overwrite pointer to point to endchannel
  1650. ld a, [wSavedVolume]
  1651. and a
  1652. jr nz, .asm_222d4
  1653. ld a, [rNR50]
  1654. ld [wSavedVolume], a
  1655. ld a, $77
  1656. ld [rNR50], a
  1657. .asm_222d4
  1658. ret
  1659. Noise2_endchannel:
  1660. endchannel
  1661. Unknown_222d6:
  1662. db $10, $15, $1A, $1F ; channels 0-3
  1663. db $10, $15, $1A, $1F ; channels 4-7
  1664. Unknown_222de:
  1665. db $EE, $DD, $BB, $77 ; channels 0-3
  1666. db $EE, $DD, $BB, $77 ; channels 4-7
  1667. Unknown_222e6:
  1668. db $11, $22, $44, $88 ; channels 0-3
  1669. db $11, $22, $44, $88 ; channels 4-7
  1670. Audio2_Pitches:
  1671. dw $F82C ; C_
  1672. dw $F89D ; C#
  1673. dw $F907 ; D_
  1674. dw $F96B ; D#
  1675. dw $F9CA ; E_
  1676. dw $FA23 ; F_
  1677. dw $FA77 ; F#
  1678. dw $FAC7 ; G_
  1679. dw $FB12 ; G#
  1680. dw $FB58 ; A_
  1681. dw $FB9B ; A#
  1682. dw $FBDA ; B_