home *** CD-ROM | disk | FTP | other *** search
/ Large Pack of OldSkool DOS MOD Trackers / goattracker_2.65.zip / src / player.s < prev    next >
Text File  |  2007-08-08  |  54KB  |  1,797 lines

  1. ;-------------------------------------------------------------------------------
  2. ; GoatTracker V2.63 playroutine
  3. ;
  4. ; NOTE: This playroutine source code does not fall under the GPL license!
  5. ; Use it, or song binaries created from it freely for any purpose, commercial
  6. ; or noncommercial.
  7. ;
  8. ; NOTE 2: This code is in the format of Magnus Lind's assembler from Exomizer.
  9. ; Does not directly compile on DASM etc.
  10. ;-------------------------------------------------------------------------------
  11.  
  12.         ;Defines will be inserted by the relocator here
  13.  
  14.               .IF (ZPGHOSTREGS == 0)
  15. mt_temp1        = zpbase+0
  16. mt_temp2        = zpbase+1
  17.               .ELSE
  18. ghostfreqlo     = zpbase+0
  19. ghostfreqhi     = zpbase+1
  20. ghostpulselo    = zpbase+2
  21. ghostpulsehi    = zpbase+3
  22. ghostwave       = zpbase+4
  23. ghostad         = zpbase+5
  24. ghostsr         = zpbase+6
  25. ghostfiltcutlow = zpbase+21
  26. ghostfiltcutoff = zpbase+22
  27. ghostfiltctrl   = zpbase+23
  28. ghostfilttype   = zpbase+24
  29. mt_temp1        = zpbase+25
  30. mt_temp2        = zpbase+26
  31.               .ENDIF
  32.  
  33.         ;Defines for the music data
  34.         ;Patterndata notes
  35.  
  36. ENDPATT         = $00
  37. INS             = $00
  38. FX              = $40
  39. FXONLY          = $50
  40. NOTE            = $60
  41. REST            = $bd
  42. KEYOFF          = $be
  43. KEYON           = $bf
  44. FIRSTPACKEDREST = $c0
  45. PACKEDREST      = $00
  46.  
  47.         ;Effects
  48.  
  49. DONOTHING       = $00
  50. PORTAUP         = $01
  51. PORTADOWN       = $02
  52. TONEPORTA       = $03
  53. VIBRATO         = $04
  54. SETAD           = $05
  55. SETSR           = $06
  56. SETWAVE         = $07
  57. SETWAVEPTR      = $08
  58. SETPULSEPTR     = $09
  59. SETFILTPTR      = $0a
  60. SETFILTCTRL     = $0b
  61. SETFILTCUTOFF   = $0c
  62. SETMASTERVOL    = $0d
  63. SETFUNKTEMPO    = $0e
  64. SETTEMPO        = $0f
  65.  
  66.         ;Orderlist commands
  67.  
  68. REPEAT          = $d0
  69. TRANSDOWN       = $e0
  70. TRANS           = $f0
  71. TRANSUP         = $f0
  72. LOOPSONG        = $ff
  73.  
  74.         ;Wave,pulse,filttable comands
  75.  
  76. LOOPWAVE        = $ff
  77. LOOPPULSE       = $ff
  78. LOOPFILT        = $ff
  79. SETPULSE        = $80
  80. SETFILTER       = $80
  81. SETCUTOFF       = $00
  82.  
  83.                 .ORG (base)
  84.  
  85.         ;Jump table
  86.  
  87.                 jmp mt_init
  88.                 jmp mt_play
  89.               .IF (SOUNDSUPPORT != 0)
  90.                 jmp mt_playsfx
  91.               .ENDIF
  92.               .IF (VOLSUPPORT != 0)
  93.                 jmp mt_setmastervol
  94.               .ENDIF
  95.  
  96.         ;Author info
  97.  
  98.               .IF (NOAUTHORINFO == 0)
  99.  
  100. authorinfopos   = base + $20
  101. checkpos1:
  102.               .IF ((authorinfopos - checkpos1) > 15)
  103. mt_tick0jumptbl:
  104.                 .BYTE (mt_tick0_0 % 256)
  105.                 .BYTE (mt_tick0_12 % 256)
  106.                 .BYTE (mt_tick0_12 % 256)
  107.                 .BYTE (mt_tick0_34 % 256)
  108.                 .BYTE (mt_tick0_34 % 256)
  109.                 .BYTE (mt_tick0_5 % 256)
  110.                 .BYTE (mt_tick0_6 % 256)
  111.                 .BYTE (mt_tick0_7 % 256)
  112.                 .BYTE (mt_tick0_8 % 256)
  113.                 .BYTE (mt_tick0_9 % 256)
  114.                 .BYTE (mt_tick0_a % 256)
  115.                 .BYTE (mt_tick0_b % 256)
  116.                 .BYTE (mt_tick0_c % 256)
  117.                 .BYTE (mt_tick0_d % 256)
  118.                 .BYTE (mt_tick0_e % 256)
  119.                 .BYTE (mt_tick0_f % 256)
  120.               .ENDIF
  121.  
  122. checkpos2:
  123.               .IF ((authorinfopos - checkpos2) > 4)
  124. mt_effectjumptbl:
  125.                 .BYTE (mt_effect_0 % 256)
  126.                 .BYTE (mt_effect_12 % 256)
  127.                 .BYTE (mt_effect_12 % 256)
  128.                 .BYTE (mt_effect_3 % 256)
  129.                 .BYTE (mt_effect_4 % 256)
  130.               .ENDIF
  131.  
  132. checkpos3:
  133.               .IF ((authorinfopos - checkpos3) > 1)
  134. mt_funktempotbl:
  135.                 .BYTE (8,5)
  136.               .ENDIF
  137.  
  138.         ;This is pretty stupid way of filling left-out space, but .ORG
  139.         ;seemed to bug
  140.  
  141. checkpos4:
  142.               .IF ((authorinfopos - checkpos4) > 0) .BYTE (0) .ENDIF
  143. checkpos5:
  144.               .IF ((authorinfopos - checkpos5) > 0) .BYTE (0) .ENDIF
  145. checkpos6:
  146.               .IF ((authorinfopos - checkpos6) > 0) .BYTE (0) .ENDIF
  147.  
  148. mt_author:
  149.  
  150.                 .BYTE (0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)
  151.                 .BYTE (0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)
  152.               .ENDIF
  153.  
  154.         ;0 Instrument vibrato
  155.  
  156. mt_tick0_0:
  157.               .IF (NOEFFECTS == 0)
  158.               .IF (NOINSTRVIB == 0)
  159.                 lda mt_insvibparam-1,y
  160.                 jmp mt_tick0_34
  161.               .ELSE
  162.               .IF (NOVIB == 0)
  163.                 jmp mt_tick0_34
  164.               .ENDIF
  165.               .ENDIF
  166.               .ENDIF
  167.  
  168.         ;1,2 Portamentos
  169.  
  170.  
  171. mt_tick0_12:
  172.               .IF (NOVIB == 0)
  173.                 tay
  174.                 lda #$00
  175.                 sta mt_chnvibtime,x
  176.                 tya
  177.               .ENDIF
  178.  
  179.         ;3,4 Toneportamento, Vibrato
  180.  
  181. mt_tick0_34:
  182.               .IF (NOEFFECTS == 0)
  183.               .IF ((NOTONEPORTA == 0) || (NOPORTAMENTO == 0) || (NOVIB == 0))
  184.                 sta mt_chnparam,x
  185.                 lda mt_chnnewfx,x
  186.                 sta mt_chnfx,x
  187.               .ENDIF
  188.                 rts
  189.               .ENDIF
  190.  
  191.         ;5 Set AD
  192.  
  193. mt_tick0_5:
  194.               .IF (NOSETAD == 0)
  195.               .IF (BUFFEREDWRITES == 0)
  196.                 sta SIDBASE+$05,x
  197.               .ELSE
  198.               .IF (ZPGHOSTREGS == 0)
  199.                 sta mt_chnad,x
  200.               .ELSE
  201.                 sta <ghostad,x
  202.               .ENDIF
  203.               .ENDIF
  204.                 rts
  205.               .ENDIF
  206.  
  207.         ;6 Set Sustain/Release
  208.  
  209. mt_tick0_6:
  210.               .IF (NOSETSR == 0)
  211.               .IF (BUFFEREDWRITES == 0)
  212.                 sta SIDBASE+$06,x
  213.               .ELSE
  214.               .IF (ZPGHOSTREGS == 0)
  215.                 sta mt_chnsr,x
  216.               .ELSE
  217.                 sta <ghostsr,x
  218.               .ENDIF
  219.               .ENDIF
  220.                 rts
  221.               .ENDIF
  222.  
  223.         ;7 Set waveform
  224.  
  225. mt_tick0_7:
  226.               .IF (NOSETWAVE == 0)
  227.                 sta mt_chnwave,x
  228.                 rts
  229.               .ENDIF
  230.  
  231.         ;8 Set wavepointer
  232.  
  233. mt_tick0_8:
  234.               .IF (NOSETWAVEPTR == 0)
  235.                 sta mt_chnwaveptr,x
  236.               .IF (NOWAVEDELAY == 0)
  237.                 lda #$00                        ;Make sure possible delayed
  238.                 sta mt_chnwavetime,x            ;waveform execution goes
  239.               .ENDIF                            ;correctly
  240.                 rts
  241.               .ENDIF
  242.  
  243.         ;9 Set pulsepointer
  244.  
  245. mt_tick0_9:
  246.               .IF (NOSETPULSEPTR == 0)
  247.                 sta mt_chnpulseptr,x
  248.                 lda #$00                        ;Reset pulse step duration
  249.                 sta mt_chnpulsetime,x
  250.                 rts
  251.               .ENDIF
  252.  
  253.         ;a Set filtpointer
  254.  
  255. mt_tick0_a:
  256.               .IF (NOSETFILTPTR == 0)
  257.               .IF (NOFILTERMOD == 0)
  258.                 ldy #$00
  259.                 sty mt_filttime+1
  260.               .ENDIF
  261. mt_tick0_a_step:
  262.                 sta mt_filtstep+1
  263.                 rts
  264.               .ENDIF
  265.  
  266.         ;b Set filtcontrol (channels & resonance)
  267.  
  268. mt_tick0_b:
  269.               .IF (NOSETFILTCTRL == 0)
  270.                 sta mt_filtctrl+1
  271.               .IF (NOSETFILTPTR == 0)
  272.                 beq mt_tick0_a_step          ;If 0, stop also step-programming
  273.               .ELSE
  274.                 bne mt_tick0_b_noset
  275.                 sta mt_filtstep+1
  276. mt_tick0_b_noset:
  277.               .ENDIF
  278.                 rts
  279.               .ENDIF
  280.  
  281.         ;c Set cutoff
  282.  
  283. mt_tick0_c:
  284.               .IF (NOSETFILTCUTOFF == 0)
  285.                 sta mt_filtcutoff+1
  286.                 rts
  287.               .ENDIF
  288.  
  289.         ;d Set mastervolume / timing mark
  290.  
  291. mt_tick0_d:
  292.               .IF (NOSETMASTERVOL == 0)
  293.               .IF (NOAUTHORINFO == 0)
  294.                 cmp #$10
  295.                 bcs mt_tick0_d_timing
  296.               .ENDIF
  297. mt_setmastervol:
  298.                 sta mt_masterfader+1
  299.                 rts
  300.               .IF (NOAUTHORINFO == 0)
  301. mt_tick0_d_timing:
  302.                 sta mt_author+31
  303.                 rts
  304.               .ENDIF
  305.               .ENDIF
  306.  
  307.         ;e Funktempo
  308.  
  309. mt_tick0_e:
  310.               .IF (NOFUNKTEMPO == 0)
  311.                 tay
  312.                 lda mt_speedlefttbl-1,y
  313.                 sta mt_funktempotbl
  314.                 lda mt_speedrighttbl-1,y
  315.                 sta mt_funktempotbl+1
  316.                 lda #$00
  317.               .IF (NOCHANNELTEMPO == 0)
  318.                 beq mt_tick0_f_setglobaltempo
  319.               .ENDIF
  320.               .ENDIF
  321.  
  322.         ;f Set Tempo
  323.  
  324. mt_tick0_f:
  325.               .IF ((NOCHANNELTEMPO == 0) && (NOGLOBALTEMPO == 0))
  326.                 bmi mt_tick0_f_setchantempo     ;Channel or global tempo?
  327.               .ENDIF
  328. mt_tick0_f_setglobaltempo:
  329.               .IF (NOGLOBALTEMPO == 0)
  330.                 sta mt_chntempo
  331.                 sta mt_chntempo+7
  332.                 sta mt_chntempo+14
  333.                 rts
  334.               .ENDIF
  335. mt_tick0_f_setchantempo:
  336.               .IF (NOCHANNELTEMPO == 0)
  337.                 and #$7f
  338.                 sta mt_chntempo,x
  339.                 rts
  340.               .ENDIF
  341.  
  342.         ;Continuous effect code
  343.  
  344.         ;0 Instrument vibrato
  345.  
  346.               .IF (NOINSTRVIB == 0)
  347. mt_effect_0_delay:
  348.                 dec mt_chnvibdelay,x
  349. mt_effect_0_donothing:
  350.                 jmp mt_done
  351. mt_effect_0:    beq mt_effect_0_donothing         ;Speed 0 = no vibrato at all
  352.                 lda mt_chnvibdelay,x
  353.                 bne mt_effect_0_delay
  354.               .ELSE
  355. mt_effect_0:
  356. mt_effect_0_donothing:
  357.                 jmp mt_done
  358.               .ENDIF
  359.  
  360.         ;4 Vibrato
  361.  
  362. mt_effect_4:
  363.               .IF (NOVIB == 0)
  364.               .IF (NOCALCULATEDSPEED == 0)
  365.                 lda mt_speedlefttbl-1,y
  366.               .IF (NONORMALSPEED == 0)
  367.                 bmi mt_effect_4_nohibyteclear
  368.                 ldy #$00                        ;Clear speed highbyte
  369.                 sty <mt_temp2
  370.               .ENDIF
  371. mt_effect_4_nohibyteclear:
  372.                 and #$7f
  373.                 sta mt_effect_4_speedcmp+1
  374.               .ELSE
  375.                 lda #$00                        ;Clear speed highbyte
  376.                 sta <mt_temp2
  377.               .ENDIF
  378.                 lda mt_chnvibtime,x
  379.                 bmi mt_effect_4_nodir
  380.               .IF (NOCALCULATEDSPEED != 0)
  381.                 cmp mt_speedlefttbl-1,y
  382.               .ELSE
  383. mt_effect_4_speedcmp:
  384.                 cmp #$00
  385.               .ENDIF
  386.                 bcc mt_effect_4_nodir2
  387.                 beq mt_effect_4_nodir
  388.                 eor #$ff
  389. mt_effect_4_nodir:
  390.                 clc
  391. mt_effect_4_nodir2:
  392.                 adc #$02
  393. mt_vibdone:
  394.                 sta mt_chnvibtime,x
  395.                 lsr
  396.                 bcc mt_freqadd
  397.                 bcs mt_freqsub
  398.               .ENDIF
  399.  
  400.         ;1,2,3 Portamentos
  401.  
  402. mt_effect_3:
  403.               .IF (NOTONEPORTA == 0)
  404.                 tya
  405.                 beq mt_effect_3_found           ;Speed $00 = tie note
  406.               .ENDIF
  407. mt_effect_12:
  408.               .IF ((NOTONEPORTA == 0) || (NOPORTAMENTO == 0))
  409.               .IF (NOCALCULATEDSPEED != 0)
  410.                 lda mt_speedlefttbl-1,y
  411.                 sta <mt_temp2
  412.               .ENDIF
  413.               .ENDIF
  414.               .IF (NOPORTAMENTO == 0)
  415.  
  416.               .IF (NOWAVECMD != 0)
  417.                 lda mt_chnfx,x
  418.               .ELSE
  419. mt_effectnum:
  420.                 lda #$00
  421.               .ENDIF
  422.                 cmp #$02
  423.                 bcc mt_freqadd
  424.                 beq mt_freqsub
  425.               .ELSE
  426.               .IF (NOTONEPORTA == 0)
  427.                 sec
  428.               .ENDIF
  429.               .ENDIF
  430.               .IF (NOTONEPORTA == 0)
  431.                 ldy mt_chnnote,y
  432.               .IF (ZPGHOSTREGS == 0)
  433.                 lda mt_chnfreqlo,x              ;Calculate offset to the
  434.                 sbc mt_freqtbllo-FIRSTNOTE,y    ;right frequency
  435.                 pha
  436.                 lda mt_chnfreqhi,x
  437.               .ELSE
  438.                 lda <ghostfreqlo,x              ;Calculate offset to the
  439.                 sbc mt_freqtbllo-FIRSTNOTE,y    ;right frequency
  440.                 pha
  441.                 lda <ghostfreqhi,x
  442.               .ENDIF
  443.                 sbc mt_freqtblhi-FIRSTNOTE,y
  444.                 tay
  445.                 pla
  446.                 bcs mt_effect_3_down            ;If positive, have to go down
  447.  
  448. mt_effect_3_up:
  449.                 adc <mt_temp1                   ;Add speed to offset
  450.                 tya                             ;If changes sign, we're done
  451.                 adc <mt_temp2
  452.                 bpl mt_effect_3_found
  453.               .ENDIF
  454.  
  455.  
  456.               .IF ((NOTONEPORTA == 0) || (NOPORTAMENTO == 0) || (NOVIB == 0))
  457. mt_freqadd:
  458.               .IF (ZPGHOSTREGS == 0)
  459.                 lda mt_chnfreqlo,x
  460.                 adc <mt_temp1
  461.                 sta mt_chnfreqlo,x
  462.                 lda mt_chnfreqhi,x
  463.               .ELSE
  464.                 lda <ghostfreqlo,x
  465.                 adc <mt_temp1
  466.                 sta <ghostfreqlo,x
  467.                 lda <ghostfreqhi,x
  468.               .ENDIF
  469.                 adc <mt_temp2
  470.                 jmp mt_storefreqhi
  471.               .ENDIF
  472.  
  473.               .IF (NOTONEPORTA == 0)
  474. mt_effect_3_down:
  475.                 sbc <mt_temp1                   ;Subtract speed from offset
  476.                 tya                             ;If changes sign, we're done
  477.                 sbc <mt_temp2
  478.                 bmi mt_effect_3_found
  479.               .ENDIF
  480.  
  481.               .IF ((NOTONEPORTA == 0) || (NOPORTAMENTO == 0) || (NOVIB == 0))
  482. mt_freqsub:
  483.               .IF (ZPGHOSTREGS == 0)
  484.                 lda mt_chnfreqlo,x
  485.                 sbc <mt_temp1
  486.                 sta mt_chnfreqlo,x
  487.                 lda mt_chnfreqhi,x
  488.               .ELSE
  489.                 lda <ghostfreqlo,x
  490.                 sbc <mt_temp1
  491.                 sta <ghostfreqlo,x
  492.                 lda <ghostfreqhi,x
  493.               .ENDIF
  494.                 sbc <mt_temp2
  495.                 jmp mt_storefreqhi
  496.               .ENDIF
  497.  
  498. mt_effect_3_found:
  499.               .IF (NOTONEPORTA == 0)
  500.               .IF (NOCALCULATEDSPEED == 0)
  501.                 lda mt_chnnote,x
  502.                 jmp mt_wavenoteabs
  503.               .ELSE
  504.                 ldy mt_chnnote,y
  505.                 jmp mt_wavenote
  506.               .ENDIF
  507.               .ENDIF
  508.  
  509.         ;Init routine
  510.  
  511. mt_init:
  512.               .IF (NUMSONGS > 1)
  513.                 sta mt_init+5
  514.                 asl
  515.                 adc #$00
  516.               .ENDIF
  517.                 sta mt_initsongnum+1
  518.                 rts
  519.  
  520.         ;Play soundeffect -routine
  521.  
  522.               .IF (SOUNDSUPPORT != 0)
  523.         ;Sound FX init routine
  524.  
  525. mt_playsfx:     sta mt_playsfxlo+1
  526.                 sty mt_playsfxhi+1
  527.                 lda mt_chnsfx,x                   ;Need a priority check?
  528.                 beq mt_playsfxok
  529.                 tya                               ;Check address highbyte
  530.                 cmp mt_chnsfxhi,x
  531.                 bcc mt_playsfxskip                ;Lower than current -> skip
  532.                 bne mt_playsfxok                  ;Higher than current -> OK
  533.                 lda mt_playsfxlo+1                ;Check address lowbyte
  534.                 cmp mt_chnsfxlo,x
  535.                 bcc mt_playsfxskip                ;Lower than current -> skip
  536. mt_playsfxok:   lda #$01
  537.                 sta mt_chnsfx,x
  538. mt_playsfxlo:   lda #$00
  539.                 sta mt_chnsfxlo,x
  540. mt_playsfxhi:   lda #$00
  541.                 sta mt_chnsfxhi,x
  542. mt_playsfxskip: rts
  543.               .ENDIF
  544.  
  545.         ;Set mastervolume -routine
  546.  
  547.               .IF ((VOLSUPPORT != 0) && (NOSETMASTERVOL != 0))
  548. mt_setmastervol:
  549.                 sta mt_masterfader+1
  550.                 rts
  551.               .ENDIF
  552.  
  553.         ;Playroutine
  554.  
  555. mt_play:        ldx #$00                        ;Channel index
  556.  
  557.         ;Song initialization
  558.  
  559. mt_initsongnum:
  560.                 ldy #$00
  561.                 bmi mt_filtstep
  562.                 txa
  563.                 ldx #NUMCHANNELS * 14 - 1
  564. mt_resetloop:
  565.                 sta mt_chnsongptr,x             ;Reset sequencer + voice
  566.                 dex                             ;variables on all channels
  567.                 bpl mt_resetloop
  568.               .IF (ZPGHOSTREGS == 0)
  569.               .IF (NUMCHANNELS == 2)
  570.                 sta SIDBASE+$12
  571.               .ENDIF
  572.               .IF (NUMCHANNELS == 1)
  573.                 sta SIDBASE+$0b
  574.                 sta SIDBASE+$12
  575.               .ENDIF
  576.                 sta SIDBASE+$15                       ;Reset filter cutoff lowbyte
  577.               .ELSE
  578.                 sta <ghostfiltcutlow
  579.               .ENDIF
  580.                 sta mt_filtctrl+1             ;Switch filter off & reset
  581.               .IF (NOFILTER == 0)
  582.                 sta mt_filtstep+1             ;step-programming
  583.               .ENDIF
  584.                 stx mt_initsongnum+1          ;Reset initflag
  585.                 tax
  586.               .IF (NUMCHANNELS == 3)
  587.                 jsr mt_initchn
  588.                 ldx #$07
  589.                 jsr mt_initchn
  590.                 ldx #$0e
  591.               .ENDIF
  592.               .IF (NUMCHANNELS == 2)
  593.                 jsr mt_initchn
  594.                 ldx #$07
  595.               .ENDIF
  596. mt_initchn:
  597.               .IF (NUMSONGS > 1)
  598.                 tya
  599.                 iny
  600.                 sta mt_chnsongnum,x             ;Store index to songtable
  601.               .ENDIF
  602. mt_defaulttempo:
  603.                 lda #DEFAULTTEMPO               ;Set default tempo
  604.                 sta mt_chntempo,x
  605.                 lda #$01
  606.                 sta mt_chncounter,x             ;Reset counter
  607.                 sta mt_chninstr,x               ;Reset instrument
  608.                 jmp mt_loadregswaveonly          ;Load waveform
  609.  
  610.         ;Filter execution
  611.  
  612. mt_filtstep:
  613.               .IF (NOFILTER == 0)
  614.                 ldy #$00                        ;See if filter stopped
  615.                 beq mt_filtdone
  616.               .IF (NOFILTERMOD == 0)
  617. mt_filttime:
  618.                 lda #$00                        ;See if time left for mod.
  619.                 bne mt_filtmod                  ;step
  620.               .ENDIF
  621. mt_newfiltstep:
  622.                 lda mt_filttimetbl-1,y          ;$80-> = set filt parameters
  623.                 beq mt_setcutoff                ;$00 = set cutoff
  624.               .IF (NOFILTERMOD == 0)
  625.                 bpl mt_newfiltmod
  626.               .ENDIF
  627. mt_setfilt:
  628.                 asl                             ;Set passband
  629.                 sta mt_filttype+1
  630.                 lda mt_filtspdtbl-1,y           ;Set resonance/channel
  631.                 sta mt_filtctrl+1
  632.                 lda mt_filttimetbl,y            ;Check for cutoff setting
  633.                 bne mt_nextfiltstep2            ;following immediately
  634. mt_setcutoff2:
  635.                 iny
  636. mt_setcutoff:
  637.                 lda mt_filtspdtbl-1,y           ;Take cutoff value
  638.                 sta mt_filtcutoff+1
  639.               .IF (NOFILTERMOD == 0)
  640.                 jmp mt_nextfiltstep
  641. mt_newfiltmod:
  642.                 sta mt_filttime+1               ;$01-$7f = new modulation step
  643. mt_filtmod:   
  644.                 lda mt_filtspdtbl-1,y           ;Take filt speed
  645.                 clc
  646.                 adc mt_filtcutoff+1
  647.                 sta mt_filtcutoff+1
  648.                 dec mt_filttime+1
  649.                 bne mt_storecutoff
  650.               .ENDIF
  651. mt_nextfiltstep:
  652.                 lda mt_filttimetbl,y           ;Jump in filttable?
  653. mt_nextfiltstep2:
  654.                 cmp #LOOPFILT
  655.                 iny
  656.                 tya
  657.                 bcc mt_nofiltjump
  658.                 lda mt_filtspdtbl-1,y          ;Take jump point
  659. mt_nofiltjump:
  660.                 sta mt_filtstep+1
  661. mt_filtdone:
  662. mt_filtcutoff:
  663.                 lda #$00
  664. mt_storecutoff:
  665.               .IF (ZPGHOSTREGS == 0)
  666.                 sta SIDBASE+$16
  667.               .ELSE
  668.                 sta <ghostfiltcutoff
  669.               .ENDIF
  670.               .ENDIF
  671. mt_filtctrl:
  672.                 lda #$00
  673.               .IF (ZPGHOSTREGS == 0)
  674.                 sta SIDBASE+$17
  675.               .ELSE
  676.                 sta <ghostfiltctrl
  677.               .ENDIF
  678. mt_filttype:
  679.                 lda #$00
  680. mt_masterfader:
  681.                 ora #$0f                        ;Master volume fader
  682.               .IF (ZPGHOSTREGS == 0)
  683.                 sta SIDBASE+$18
  684.               .ELSE
  685.                 sta <ghostfilttype
  686.               .ENDIF
  687.  
  688.               .IF (NUMCHANNELS == 3)
  689.                 jsr mt_execchn
  690.                 ldx #$07
  691.                 jsr mt_execchn
  692.                 ldx #$0e
  693.               .ENDIF
  694.               .IF (NUMCHANNELS == 2)
  695.                 jsr mt_execchn
  696.                 ldx #$07
  697.               .ENDIF
  698.  
  699.         ;Channel execution
  700.  
  701. mt_execchn:
  702.                 dec mt_chncounter,x               ;See if tick 0
  703.                 beq mt_tick0
  704.  
  705.         ;Ticks 1-n
  706.  
  707. mt_notick0:
  708.                 bpl mt_effects
  709.                 lda mt_chntempo,x               ;Reload tempo if negative
  710.  
  711.               .IF (NOFUNKTEMPO == 0)
  712.                 cmp #$02
  713.                 bcs mt_nofunktempo              ;Funktempo: bounce between
  714.                 tay                             ;funktable indexes 0,1
  715.                 eor #$01
  716.                 sta mt_chntempo,x
  717.                 lda mt_funktempotbl,y
  718.                 sbc #$00
  719.               .ENDIF
  720.  
  721. mt_nofunktempo:
  722.                 sta mt_chncounter,x
  723. mt_effects:
  724.                 jmp mt_waveexec
  725.  
  726.         ;Sequencer repeat
  727.  
  728. mt_repeat:
  729.               .IF (NOREPEAT == 0)
  730.                 sbc #REPEAT
  731.                 inc mt_chnrepeat,x
  732.                 cmp mt_chnrepeat,x
  733.                 bne mt_nonewpatt
  734. mt_repeatdone:
  735.                 lda #$00
  736.                 sta mt_chnrepeat,x
  737.                 beq mt_repeatdone2
  738.               .ENDIF
  739.  
  740.         ;Tick 0
  741.  
  742. mt_tick0:
  743.               .IF (NOEFFECTS == 0)
  744.                 ldy mt_chnnewfx,y               ;Setup tick 0 FX jumps
  745.                 lda mt_tick0jumptbl,y
  746.                 sta mt_tick0jump1+1
  747.                 sta mt_tick0jump2+1
  748.               .ENDIF
  749.  
  750.         ;Sequencer advance
  751.  
  752. mt_checknewpatt:
  753.                 lda mt_chnpattptr,x             ;Fetch next pattern?
  754.                 bne mt_nonewpatt
  755. mt_sequencer:
  756.                 ldy mt_chnsongnum,y
  757.                 lda mt_songtbllo,y              ;Get address of sequence
  758.                 sta <mt_temp1
  759.                 lda mt_songtblhi,y
  760.                 sta <mt_temp2
  761.                 ldy mt_chnsongptr,y
  762.                 lda (mt_temp1),y                ;Get pattern from sequence
  763.                 cmp #LOOPSONG                   ;Check for loop
  764.                 bcc mt_noloop
  765.                 iny
  766.                 lda (mt_temp1),y
  767.                 tay
  768.                 lda (mt_temp1),y
  769. mt_noloop:
  770.               .IF (NOTRANS == 0)
  771.                 cmp #TRANSDOWN                  ;Check for transpose
  772.                 bcc mt_notrans
  773.                 sbc #TRANS
  774.                 sta mt_chntrans,x
  775.                 iny
  776.                 lda (mt_temp1),y
  777.               .ENDIF
  778. mt_notrans:
  779.               .IF (NOREPEAT == 0)
  780.                 cmp #REPEAT                     ;Check for repeat
  781.                 bcs mt_repeat
  782.               .ENDIF
  783.                 sta mt_chnpattnum,x             ;Store pattern number
  784. mt_repeatdone2:
  785.                 iny
  786.                 tya
  787.                 sta mt_chnsongptr,x             ;Store songposition
  788.  
  789.         ;New note start
  790.  
  791. mt_nonewpatt:
  792.                 ldy mt_chninstr,y
  793.               .IF (FIXEDPARAMS == 0)
  794.                 lda mt_insgatetimer-1,y
  795.                 sta mt_chngatetimer,x
  796.               .ENDIF
  797.                 lda mt_chnnewnote,x             ;Test new note init flag
  798.                 beq mt_nonewnoteinit
  799. mt_newnoteinit:
  800.                 sec
  801.                 sbc #NOTE
  802.                 sta mt_chnnote,x
  803.                 lda #$00
  804.               .IF (NOEFFECTS == 0)
  805.               .IF ((NOTONEPORTA == 0) || (NOPORTAMENTO == 0) || (NOVIB == 0))
  806.                 sta mt_chnfx,x                  ;Reset effect
  807.               .ENDIF
  808.               .ENDIF
  809.                 sta mt_chnnewnote,x             ;Reset newnote action
  810.               .IF (NOINSTRVIB == 0)
  811.                 lda mt_insvibdelay-1,y          ;Load instrument vibrato
  812.                 sta mt_chnvibdelay,x
  813.               .IF (NOEFFECTS == 0)
  814.                 lda mt_insvibparam-1,y
  815.                 sta mt_chnparam,x
  816.               .ENDIF
  817.               .ENDIF
  818.               .IF (NOTONEPORTA == 0)
  819.                 lda mt_chnnewfx,x               ;If toneportamento, skip
  820.                 cmp #TONEPORTA                  ;most of note init
  821.                 beq mt_nonewnoteinit
  822.               .ENDIF
  823.  
  824.                 lda mt_insad-1,y                ;Load Attack/Decay
  825.               .IF (BUFFEREDWRITES == 0)
  826.                 sta SIDBASE+$05,x
  827.               .ELSE
  828.               .IF (ZPGHOSTREGS == 0)
  829.                 sta mt_chnad,x
  830.               .ELSE
  831.                 sta <ghostad,x
  832.               .ENDIF
  833.               .ENDIF
  834.                 lda mt_inssr-1,y                ;Load Sustain/Release
  835.               .IF (BUFFEREDWRITES == 0)
  836.                 sta SIDBASE+$06,x
  837.               .ELSE
  838.               .IF (ZPGHOSTREGS == 0)
  839.                 sta mt_chnsr,x
  840.               .ELSE
  841.                 sta <ghostsr,x
  842.               .ENDIF
  843.               .ENDIF
  844.  
  845.               .IF (NOPULSE == 0)
  846.                 lda mt_inspulseptr-1,y          ;Load pulseptr (if nonzero)
  847.                 beq mt_skippulse
  848.                 sta mt_chnpulseptr,x
  849.               .IF (NOPULSEMOD == 0)
  850.                 lda #$00                        ;Reset pulse step duration
  851.                 sta mt_chnpulsetime,x
  852.               .ENDIF
  853.               .ENDIF
  854. mt_skippulse:
  855.               .IF (NOFILTER == 0)
  856.                 lda mt_insfiltptr-1,y         ;Load filtptr (if nonzero)
  857.                 beq mt_skipfilt
  858.                 sta mt_filtstep+1
  859.               .IF (NOFILTERMOD == 0)
  860.                 lda #$00
  861.                 sta mt_filttime+1
  862.               .ENDIF
  863.               .ENDIF
  864. mt_skipfilt:
  865.               .IF (FIXEDPARAMS == 0)
  866.                 lda mt_insfirstwave-1,y         ;Load first frame waveform
  867.               .IF (NOFIRSTWAVECMD == 0)
  868.                 beq mt_skipwave
  869.                 cmp #$fe
  870.                 bcs mt_skipwave2                ;Skip waveform but load gate
  871.               .ENDIF
  872.               .ELSE
  873.                 lda #FIRSTWAVEPARAM
  874.               .ENDIF
  875.                 sta mt_chnwave,x
  876.               .IF ((NUMLEGATOINSTR > 0) || (NOFIRSTWAVECMD == 0))
  877.                 lda #$ff
  878. mt_skipwave2:
  879.                 sta mt_chngate,x                ;Reset gateflag
  880.               .ELSE
  881.                 inc mt_chngate,x
  882.               .ENDIF
  883. mt_skipwave:
  884.                 lda mt_inswaveptr-1,y           ;Load waveptr
  885.                 sta mt_chnwaveptr,x
  886.               .IF (NOEFFECTS == 0)
  887.                 lda mt_chnnewparam,x            ;Execute tick 0 FX after
  888. mt_tick0jump1:
  889.                 jsr mt_tick0_0                  ;newnote init
  890.               .ENDIF
  891.                 jmp mt_loadregs
  892.  
  893.               .IF (NOWAVECMD == 0)
  894. mt_wavecmd:
  895.                 jmp mt_execwavecmd
  896.               .ENDIF
  897.  
  898.         ;Tick 0 effect execution
  899.  
  900. mt_nonewnoteinit:
  901.               .IF (NOEFFECTS == 0)
  902.                 lda mt_chnnewparam,x            ;No new note init: exec tick 0
  903. mt_tick0jump2:
  904.                 jsr mt_tick0_0                  ;FX, and wavetable afterwards
  905.               .ENDIF
  906.  
  907.         ;Wavetable execution
  908.  
  909. mt_waveexec:
  910.                 ldy mt_chnwaveptr,y
  911.                 beq mt_wavedone
  912.                 lda mt_wavetbl-1,y
  913.               .IF (NOWAVEDELAY == 0)
  914.                 cmp #$10                        ;0-15 used as delay
  915.                 bcs mt_nowavedelay              ;+ no wave change
  916.                 cmp mt_chnwavetime,x
  917.                 beq mt_nowavechange
  918.                 inc mt_chnwavetime,x
  919.                 bne mt_wavedone
  920. mt_nowavedelay:
  921.                 sbc #$10
  922.               .ELSE
  923.                 beq mt_nowavechange
  924.               .ENDIF
  925.               .IF (NOWAVECMD == 0)
  926.                 cmp #$e0
  927.                 bcs mt_nowavechange
  928.               .ENDIF
  929.                 sta mt_chnwave,x
  930. mt_nowavechange:
  931.                 lda mt_wavetbl,y
  932.                 cmp #LOOPWAVE                  ;Check for wavetable jump
  933.                 iny
  934.                 tya
  935.                 bcc mt_nowavejump
  936.               .IF (NOWAVECMD != 0)
  937.                 clc
  938.               .ENDIF
  939.                 lda mt_notetbl-1,y
  940. mt_nowavejump:
  941.                 sta mt_chnwaveptr,x
  942.               .IF (NOWAVEDELAY == 0)
  943.                 lda #$00
  944.                 sta mt_chnwavetime,x
  945.               .ENDIF
  946.  
  947.               .IF (NOWAVECMD == 0)
  948.                 lda mt_wavetbl-2,y
  949.                 cmp #$e0
  950.                 bcs mt_wavecmd
  951.               .ENDIF
  952.  
  953.                 lda mt_notetbl-2,y
  954.  
  955.               .IF ((NOTONEPORTA == 0) || (NOPORTAMENTO == 0) || (NOVIB == 0))
  956.                 bne mt_wavefreq                 ;No frequency-change?
  957.  
  958.         ;No frequency-change / continuous effect execution
  959.  
  960. mt_wavedone:
  961.               .IF (REALTIMEOPTIMIZATION != 0)
  962.                 lda mt_chncounter,x             ;No continuous effects on tick0
  963.               .IF (PULSEOPTIMIZATION != 0)
  964.                 beq mt_gatetimer
  965.               .ELSE
  966.                 beq mt_done
  967.               .ENDIF
  968.               .ENDIF
  969.               .IF (NOEFFECTS == 0)
  970.                 ldy mt_chnfx,y
  971.               .IF (NOWAVECMD == 0)
  972.               .IF (.DEFINED(mt_effectnum))
  973.                 sty mt_effectnum+1
  974.               .ENDIF
  975.               .ENDIF
  976.                 lda mt_effectjumptbl,y
  977.                 sta mt_effectjump+1
  978.                 ldy mt_chnparam,y
  979.               .ELSE
  980.                 ldy mt_chninstr,y
  981.                 lda mt_insvibparam-1,y
  982.                 tay
  983.               .ENDIF
  984. mt_setspeedparam:
  985.               .IF (NOCALCULATEDSPEED != 0)
  986.                 lda mt_speedrighttbl-1,y
  987.                 sta <mt_temp1
  988.               .ELSE
  989.               .IF (NONORMALSPEED == 0)
  990.                 lda mt_speedlefttbl-1,y
  991.                 bmi mt_calculatedspeed
  992. mt_normalspeed:
  993.                 sta <mt_temp2
  994.                 lda mt_speedrighttbl-1,y
  995.                 sta <mt_temp1
  996.                 jmp mt_effectjump
  997.               .ELSE
  998.               .IF (NOZEROSPEED == 0)
  999.                 bne mt_calculatedspeed
  1000. mt_zerospeed:
  1001.                 sty <mt_temp1
  1002.                 sty <mt_temp2
  1003.                 beq mt_effectjump
  1004.               .ENDIF
  1005.               .ENDIF
  1006. mt_calculatedspeed:
  1007.                 lda mt_speedrighttbl-1,y
  1008.                 sta mt_cscount+1
  1009.                 sty mt_csresty+1
  1010.                 ldy mt_chnlastnote,y
  1011.                 lda mt_freqtbllo+1-FIRSTNOTE,y
  1012.                 sec
  1013.                 sbc mt_freqtbllo-FIRSTNOTE,y
  1014.                 sta <mt_temp1
  1015.                 lda mt_freqtblhi+1-FIRSTNOTE,y
  1016.                 sbc mt_freqtblhi-FIRSTNOTE,y
  1017. mt_cscount:     ldy #$00
  1018.                 beq mt_csresty
  1019. mt_csloop:      lsr
  1020.                 ror <mt_temp1
  1021.                 dey
  1022.                 bne mt_csloop
  1023. mt_csresty:     ldy #$00
  1024.                 sta <mt_temp2
  1025.               .ENDIF
  1026. mt_effectjump:
  1027.                 jmp mt_effect_0
  1028.               .ELSE
  1029.                 beq mt_wavedone
  1030.               .ENDIF
  1031.  
  1032.         ;Setting note frequency
  1033.  
  1034. mt_wavefreq:
  1035.                 bpl mt_wavenoteabs
  1036.                 adc mt_chnnote,x
  1037.                 and #$7f
  1038. mt_wavenoteabs:
  1039.               .IF (NOCALCULATEDSPEED == 0)
  1040.                 sta mt_chnlastnote,x
  1041.               .ENDIF
  1042.                 tay
  1043. mt_wavenote:
  1044.               .IF (NOVIB == 0)
  1045.                 lda #$00                        ;Reset vibrato phase
  1046.                 sta mt_chnvibtime,x
  1047.               .ENDIF
  1048.                 lda mt_freqtbllo-FIRSTNOTE,y
  1049.               .IF (ZPGHOSTREGS == 0)
  1050.                 sta mt_chnfreqlo,x
  1051.                 lda mt_freqtblhi-FIRSTNOTE,y
  1052. mt_storefreqhi:
  1053.                 sta mt_chnfreqhi,x
  1054.               .ELSE
  1055.                 sta <ghostfreqlo,x
  1056.                 lda mt_freqtblhi-FIRSTNOTE,y
  1057. mt_storefreqhi:
  1058.                 sta <ghostfreqhi,x
  1059.               .ENDIF
  1060.  
  1061.         ;Check for new note fetch
  1062.  
  1063.               .IF ((NOTONEPORTA != 0) && (NOPORTAMENTO != 0) && (NOVIB != 0))
  1064. mt_wavedone:
  1065.               .ENDIF
  1066. mt_done:
  1067.               .IF (PULSEOPTIMIZATION != 0)
  1068.                 lda mt_chncounter,x             ;Check for gateoff timer
  1069. mt_gatetimer:
  1070.               .IF (FIXEDPARAMS == 0)
  1071.                 cmp mt_chngatetimer,x
  1072.               .ELSE
  1073.                 cmp #GATETIMERPARAM
  1074.               .ENDIF
  1075.  
  1076.                 beq mt_getnewnote               ;Fetch new notes if equal
  1077.               .ENDIF
  1078.  
  1079.         ;Pulse execution
  1080.               .IF (NOPULSE == 0)
  1081. mt_pulseexec:
  1082.                 ldy mt_chnpulseptr,y            ;See if pulse stopped
  1083.                 beq mt_pulseskip
  1084.               .IF (PULSEOPTIMIZATION != 0)
  1085.                 ora mt_chnpattptr,x             ;Skip when sequencer executed
  1086.                 beq mt_pulseskip
  1087.               .ENDIF
  1088.               .IF (NOPULSEMOD == 0)
  1089.                 lda mt_chnpulsetime,x           ;Pulse step counter time left?
  1090.                 bne mt_pulsemod
  1091.               .ENDIF
  1092. mt_newpulsestep:
  1093.                 lda mt_pulsetimetbl-1,y         ;Set pulse, or new modulation
  1094.               .IF (NOPULSEMOD == 0)
  1095.                 bpl mt_newpulsemod              ;step?
  1096.               .ENDIF
  1097. mt_setpulse:
  1098.               .IF (SIMPLEPULSE == 0)
  1099.               .IF (ZPGHOSTREGS == 0)
  1100.                 sta mt_chnpulsehi,x             ;Highbyte
  1101.               .ELSE
  1102.                 sta <ghostpulsehi,x
  1103.               .ENDIF
  1104.               .ENDIF
  1105.                 lda mt_pulsespdtbl-1,y          ;Lowbyte
  1106.               .IF (ZPGHOSTREGS == 0)
  1107.                 sta mt_chnpulselo,x
  1108.               .ELSE
  1109.                 sta <ghostpulselo,x
  1110.               .IF (SIMPLEPULSE != 0)
  1111.                 sta <ghostpulsehi,x
  1112.               .ENDIF
  1113.               .ENDIF
  1114.               .IF (NOPULSEMOD == 0)
  1115.                 jmp mt_nextpulsestep
  1116. mt_newpulsemod:
  1117.                 sta mt_chnpulsetime,x
  1118. mt_pulsemod:
  1119.               .IF (SIMPLEPULSE == 0)
  1120.                 lda mt_pulsespdtbl-1,y          ;Take pulse speed
  1121.                 clc
  1122.                 bpl mt_pulseup
  1123.               .IF (ZPGHOSTREGS == 0)
  1124.                 dec mt_chnpulsehi,x
  1125. mt_pulseup:
  1126.                 adc mt_chnpulselo,x             ;Add pulse lowbyte
  1127.                 sta mt_chnpulselo,x
  1128.                 bcc mt_pulsenotover
  1129.                 inc mt_chnpulsehi,x
  1130.               .ELSE
  1131.                 dec <ghostpulsehi,x
  1132. mt_pulseup:
  1133.                 adc <ghostpulselo,x             ;Add pulse lowbyte
  1134.                 sta <ghostpulselo,x
  1135.                 bcc mt_pulsenotover
  1136.                 inc <ghostpulsehi,x
  1137.               .ENDIF
  1138. mt_pulsenotover:
  1139.               .ELSE
  1140.               .IF (ZPGHOSTREGS == 0)
  1141.                 lda mt_chnpulselo,x
  1142.                 clc
  1143.                 adc mt_pulsespdtbl-1,y
  1144.                 adc #$00
  1145.                 sta mt_chnpulselo,x
  1146.               .ELSE
  1147.                 lda <ghostpulselo,x
  1148.                 clc
  1149.                 adc mt_pulsespdtbl-1,y
  1150.                 adc #$00
  1151.                 sta <ghostpulselo,x
  1152.                 sta <ghostpulsehi,x
  1153.               .ENDIF
  1154.               .ENDIF
  1155.                 dec mt_chnpulsetime,x
  1156.                 bne mt_pulsedone2
  1157.               .ENDIF
  1158.  
  1159. mt_nextpulsestep:
  1160.                 lda mt_pulsetimetbl,y           ;Jump in pulsetable?
  1161.                 cmp #LOOPPULSE
  1162.                 iny
  1163.                 tya
  1164.                 bcc mt_nopulsejump
  1165.                 lda mt_pulsespdtbl-1,y          ;Take jump point
  1166. mt_nopulsejump:
  1167.                 sta mt_chnpulseptr,x
  1168. mt_pulsedone:
  1169.               .IF (BUFFEREDWRITES == 0)
  1170.                 lda mt_chnpulselo,x
  1171.               .ENDIF
  1172. mt_pulsedone2:
  1173.               .IF (BUFFEREDWRITES == 0)
  1174.                 sta SIDBASE+$02,x
  1175.               .IF (SIMPLEPULSE == 0)
  1176.                 lda mt_chnpulsehi,x
  1177.               .ENDIF
  1178.                 sta SIDBASE+$03,x
  1179.               .ENDIF
  1180. mt_pulseskip:
  1181.               .ENDIF
  1182.  
  1183.               .IF (PULSEOPTIMIZATION == 0)
  1184.                 lda mt_chncounter,x             ;Check for gateoff timer
  1185. mt_gatetimer:
  1186.               .IF (FIXEDPARAMS == 0)
  1187.                 cmp mt_chngatetimer,x
  1188.               .ELSE
  1189.                 cmp #GATETIMERPARAM
  1190.               .ENDIF
  1191.  
  1192.                 beq mt_getnewnote               ;Fetch new notes if equal
  1193.               .ENDIF
  1194.  
  1195.                 jmp mt_loadregs
  1196.  
  1197.         ;New note fetch
  1198.  
  1199. mt_getnewnote:
  1200.                 ldy mt_chnpattnum,y
  1201.                 lda mt_patttbllo,y
  1202.                 sta <mt_temp1
  1203.                 lda mt_patttblhi,y
  1204.                 sta <mt_temp2
  1205.                 ldy mt_chnpattptr,y
  1206.                 lda (mt_temp1),y
  1207.                 cmp #FX
  1208.                 bcc mt_instr                    ;Instr. change
  1209.               .IF (NOEFFECTS == 0)
  1210.                 cmp #NOTE
  1211.                 bcc mt_fx                       ;FX
  1212.               .ENDIF
  1213.                 cmp #FIRSTPACKEDREST
  1214.                 bcc mt_note                     ;Note only
  1215.  
  1216.         ;Packed rest handling
  1217.  
  1218. mt_packedrest:
  1219.                 lda mt_chnpackedrest,x
  1220.                 bne mt_packedrestnonew
  1221.                 lda (mt_temp1),y
  1222. mt_packedrestnonew:
  1223.                 adc #$00
  1224.                 sta mt_chnpackedrest,x
  1225.                 beq mt_rest
  1226.                 bne mt_loadregs
  1227.  
  1228.         ;Instrument change
  1229.  
  1230. mt_instr:
  1231.                 sta mt_chninstr,x               ;Instrument change, followed
  1232.                 iny
  1233.                 lda (mt_temp1),y                ;by either FX or note
  1234.  
  1235.               .IF (NOEFFECTS == 0)
  1236.                 cmp #NOTE
  1237.                 bcs mt_note
  1238.  
  1239.         ;Effect change
  1240.  
  1241. mt_fx:
  1242.                 cmp #FXONLY                     ;Note follows?
  1243.                 and #$0f
  1244.                 sta mt_chnnewfx,x
  1245.                 beq mt_fx_noparam               ;Effect 0 - no param.
  1246.                 iny
  1247.                 lda (mt_temp1),y
  1248.                 sta mt_chnnewparam,x
  1249. mt_fx_noparam:
  1250.                 bcs mt_rest
  1251. mt_fx_getnote:
  1252.                 iny
  1253.                 lda (mt_temp1),y
  1254.               .ENDIF
  1255.  
  1256.         ;Note handling
  1257.  
  1258. mt_note:
  1259.                 cmp #REST                   ;Rest or gateoff/on?
  1260.               .IF (NOGATE == 0)
  1261.                 bcc mt_normalnote
  1262.               .ENDIF
  1263.                 beq mt_rest
  1264. mt_gate:
  1265.               .IF (NOGATE == 0)
  1266.                 ora #$f0
  1267.                 bne mt_setgate
  1268.               .ENDIF
  1269.  
  1270.         ;Prepare for note start; perform hardrestart
  1271.  
  1272. mt_normalnote:
  1273.               .IF (NOTRANS == 0)
  1274.                 adc mt_chntrans,x
  1275.               .ENDIF
  1276.                 sta mt_chnnewnote,x
  1277.               .IF (NOTONEPORTA == 0)
  1278.                 lda mt_chnnewfx,x           ;If toneportamento, no gateoff
  1279.                 cmp #TONEPORTA
  1280.                 beq mt_rest
  1281.               .ENDIF
  1282.               .IF (((NUMHRINSTR > 0) && (NUMNOHRINSTR > 0)) || (NUMLEGATOINSTR > 0))
  1283.                 lda mt_chninstr,x
  1284.                 cmp #FIRSTNOHRINSTR         ;Instrument order:
  1285.               .IF (NUMLEGATOINSTR > 0)
  1286.                 bcs mt_nohr_legato          ;With HR - no HR - legato
  1287.               .ELSE
  1288.                 bcs mt_skiphr
  1289.               .ENDIF
  1290.               .ENDIF
  1291.               .IF (NUMHRINSTR > 0)
  1292.                 lda #ADPARAM                ;Hard restart
  1293.               .IF (BUFFEREDWRITES == 0)
  1294.                 sta SIDBASE+$05,x
  1295.               .ELSE
  1296.               .IF (ZPGHOSTREGS == 0)
  1297.                 sta mt_chnad,x
  1298.               .ELSE
  1299.                 sta <ghostad,x
  1300.               .ENDIF
  1301.               .ENDIF
  1302.                 lda #SRPARAM
  1303.               .IF (BUFFEREDWRITES == 0)
  1304.                 sta SIDBASE+$06,x
  1305.               .ELSE
  1306.               .IF (ZPGHOSTREGS == 0)
  1307.                 sta mt_chnsr,x
  1308.               .ELSE
  1309.                 sta <ghostsr,x
  1310.               .ENDIF
  1311.               .ENDIF
  1312.               .ENDIF
  1313. mt_skiphr:
  1314.                 lda #$fe
  1315. mt_setgate:
  1316.                 sta mt_chngate,x
  1317.  
  1318.         ;Check for end of pattern
  1319.  
  1320. mt_rest:
  1321.                 iny
  1322.                 lda (mt_temp1),y
  1323.                 beq mt_endpatt
  1324.                 tya
  1325. mt_endpatt:
  1326.                 sta mt_chnpattptr,x
  1327.  
  1328.         ;Load voice registers
  1329.  
  1330. mt_loadregs:
  1331.               .IF (BUFFEREDWRITES == 0)
  1332.                 lda mt_chnfreqlo,x
  1333.                 sta SIDBASE+$00,x
  1334.                 lda mt_chnfreqhi,x
  1335.                 sta SIDBASE+$01,x
  1336. mt_loadregswaveonly:
  1337.                 lda mt_chnwave,x
  1338.                 and mt_chngate,x
  1339.                 sta SIDBASE+$04,x
  1340.               .ELSE
  1341.               .IF (SOUNDSUPPORT != 0)
  1342.                 ldy mt_chnsfx,y
  1343.                 bne mt_sfxexec
  1344.               .ENDIF
  1345.               .IF (ZPGHOSTREGS == 0)
  1346.                 lda mt_chnad,x
  1347.                 sta SIDBASE+$05,x
  1348.                 lda mt_chnsr,x
  1349.                 sta SIDBASE+$06,x
  1350.                 lda mt_chnpulselo,x
  1351.               .IF (SIMPLEPULSE == 0)
  1352.                 sta SIDBASE+$02,x
  1353.                 lda mt_chnpulsehi,x
  1354.                 sta SIDBASE+$03,x
  1355.               .ELSE
  1356.                 sta SIDBASE+$02,x
  1357.                 sta SIDBASE+$03,x
  1358.               .ENDIF
  1359. mt_loadregswavefreq:
  1360.                 lda mt_chnfreqlo,x
  1361.                 sta SIDBASE+$00,x
  1362.                 lda mt_chnfreqhi,x
  1363.                 sta SIDBASE+$01,x
  1364. mt_loadregswaveonly:
  1365.                 lda mt_chnwave,x
  1366.                 and mt_chngate,x
  1367.                 sta SIDBASE+$04,x
  1368.               .ELSE
  1369. mt_loadregswaveonly:
  1370.                 lda mt_chnwave,x
  1371.                 and mt_chngate,x
  1372.                 sta <ghostwave,x
  1373.               .ENDIF
  1374.               .ENDIF
  1375.                 rts
  1376.  
  1377.               .IF (NUMLEGATOINSTR > 0)
  1378. mt_nohr_legato:
  1379.                 cmp #FIRSTLEGATOINSTR
  1380.                 bcc mt_skiphr
  1381.                 bcs mt_rest
  1382.               .ENDIF
  1383.  
  1384.         ;Sound FX code
  1385.  
  1386.               .IF (SOUNDSUPPORT != 0)
  1387.               .IF (ZPGHOSTREGS == 0)
  1388.  
  1389.         ;Sound FX code without ghostregs
  1390.  
  1391. mt_sfxexec:     lda mt_chnsfxlo,x
  1392.                 sta <mt_temp1
  1393.                 lda mt_chnsfxhi,x
  1394.                 sta <mt_temp2
  1395.                 lda #$fe
  1396.                 sta mt_chngate,x
  1397.                 lda #$00
  1398.                 sta mt_chnwaveptr,x
  1399.                 inc mt_chnsfx,x
  1400.                 cpy #$02
  1401.                 beq mt_sfxexec_frame0
  1402.                 bcs mt_sfxexec_framen
  1403.                 sta SIDBASE+$05,x                ;Hardrestart before sound FX
  1404.                 sta SIDBASE+$06,x                ;begins
  1405.                 bcc mt_loadregswavefreq
  1406. mt_sfxexec_frame0:
  1407.                 tay
  1408.                 lda (mt_temp1),y           ;Load ADSR
  1409.                 sta SIDBASE+$05,x
  1410.                 iny
  1411.                 lda (mt_temp1),y
  1412.                 sta SIDBASE+$06,x
  1413.                 iny
  1414.                 lda (mt_temp1),y           ;Load pulse
  1415.                 sta SIDBASE+$02,x
  1416.                 sta SIDBASE+$03,x
  1417.                 lda #$09                   ;Testbit
  1418. mt_sfxexec_wavechg:
  1419.                 sta mt_chnwave,x
  1420.                 sta SIDBASE+$04,x
  1421. mt_sfxexec_done:
  1422.                 rts
  1423. mt_sfxexec_framen:
  1424.                 lda (mt_temp1),y
  1425.                 bne mt_sfxexec_noend
  1426. mt_sfxexec_end:
  1427.                 sta mt_chnsfx,x
  1428.                 beq mt_sfxexec_wavechg
  1429. mt_sfxexec_noend:
  1430.                 tay
  1431.                 lda mt_freqtbllo-$80,y        ;Get frequency
  1432.                 sta SIDBASE+$00,x
  1433.                 lda mt_freqtblhi-$80,y
  1434.                 sta SIDBASE+$01,x
  1435.                 ldy mt_chnsfx,y
  1436.                 lda (mt_temp1),y              ;Then take a look at the next
  1437.                 beq mt_sfxexec_done           ;byte
  1438.                 cmp #$82                      ;Is it a waveform or a note?
  1439.                 bcs mt_sfxexec_done
  1440.                 inc mt_chnsfx,x
  1441.                 bcc mt_sfxexec_wavechg
  1442.  
  1443.               .ELSE
  1444.  
  1445.         ;Sound FX code with ghostregs
  1446.  
  1447. mt_sfxexec:
  1448.                 lda mt_chnsfxlo,x
  1449.                 sta <mt_temp1
  1450.                 lda mt_chnsfxhi,x
  1451.                 sta <mt_temp2
  1452.                 lda #$fe
  1453.                 sta mt_chngate,x
  1454.                 lda #$00
  1455.                 sta mt_chnwaveptr,x
  1456.                 inc mt_chnsfx,x
  1457.                 cpy #$02
  1458.                 bcc mt_sfxexec_fr1                  ;Hardrestart frame?
  1459.                 beq mt_sfxexec_fr2                  ;First or nth frame?
  1460. mt_sfxexec_fr3:
  1461.                 lda (mt_temp1),y
  1462.                 bne mt_sfxexec_noend
  1463. mt_sfxexec_end:
  1464.                 sta mt_chnsfx,x
  1465.                 beq mt_sfxexec_wavechg
  1466. mt_sfxexec_noend:
  1467.                 tay
  1468.                 lda mt_freqtbllo-$80,y        ;Get frequency
  1469.                 sta <ghostfreqlo,x
  1470.                 lda mt_freqtblhi-$80,y
  1471.                 sta <ghostfreqhi,x
  1472.                 ldy mt_chnsfx,y
  1473.                 lda (mt_temp1),y              ;Then take a look at the next
  1474.                 beq mt_sfxexec_done           ;byte
  1475.                 cmp #$82                      ;Is it a waveform or a note?
  1476.                 bcs mt_sfxexec_done
  1477.                 inc mt_chnsfx,x
  1478. mt_sfxexec_wavechg:
  1479.                 sta mt_chnwave,x
  1480.                 sta <ghostwave,x
  1481. mt_sfxexec_done:
  1482.                 ldy #$00
  1483.                 lda (mt_temp1),y             ;Load ADSR
  1484.                 sta <ghostad,x
  1485.                 iny
  1486.                 lda (mt_temp1),y
  1487.                 sta <ghostsr,x
  1488.                 iny
  1489.                 lda (mt_temp1),y             ;Load pulse
  1490.                 sta <ghostpulselo,x
  1491.                 sta <ghostpulsehi,x
  1492.                 rts
  1493.  
  1494. mt_sfxexec_fr1:
  1495.                 sta <ghostad,x               ;Hardrestart before sound FX
  1496.                 sta <ghostsr,x               ;begins
  1497.                 bcc mt_loadregswaveonly
  1498.  
  1499. mt_sfxexec_fr2:
  1500.                 lda #$09
  1501.                 bne mt_sfxexec_wavechg
  1502.  
  1503.               .ENDIF
  1504.               .ENDIF
  1505.  
  1506.         ;Wavetable command exec
  1507.  
  1508.               .IF (NOWAVECMD == 0)
  1509. mt_execwavecmd:
  1510.                 and #$0f
  1511.                 sta <mt_temp1
  1512.                 lda mt_notetbl-2,y
  1513.                 sta <mt_temp2
  1514.                 ldy <mt_temp1
  1515.               .IF ((NOTONEPORTA == 0) || (NOPORTAMENTO == 0) || (NOVIB == 0))
  1516.                 cpy #$05
  1517.                 bcs mt_execwavetick0
  1518. mt_execwavetickn:
  1519.               .IF (.DEFINED(mt_effectnum))
  1520.                 sty mt_effectnum+1
  1521.               .ENDIF
  1522.                 lda mt_effectjumptbl,y
  1523.                 sta mt_effectjump+1
  1524.                 ldy <mt_temp2
  1525.                 jmp mt_setspeedparam
  1526.               .ENDIF
  1527. mt_execwavetick0:
  1528.                 lda mt_tick0jumptbl,y
  1529.                 sta mt_execwavetick0jump+1
  1530.                 lda <mt_temp2
  1531. mt_execwavetick0jump:
  1532.                 jsr mt_tick0_0
  1533.                 jmp mt_done
  1534.               .ENDIF
  1535.  
  1536.               .IF (NOEFFECTS == 0)
  1537.               .IF (!.DEFINED(mt_tick0jumptbl))
  1538. mt_tick0jumptbl:
  1539.                 .BYTE (mt_tick0_0 % 256)
  1540.                 .BYTE (mt_tick0_12 % 256)
  1541.                 .BYTE (mt_tick0_12 % 256)
  1542.                 .BYTE (mt_tick0_34 % 256)
  1543.                 .BYTE (mt_tick0_34 % 256)
  1544.                 .BYTE (mt_tick0_5 % 256)
  1545.                 .BYTE (mt_tick0_6 % 256)
  1546.                 .BYTE (mt_tick0_7 % 256)
  1547.                 .BYTE (mt_tick0_8 % 256)
  1548.                 .BYTE (mt_tick0_9 % 256)
  1549.                 .BYTE (mt_tick0_a % 256)
  1550.                 .BYTE (mt_tick0_b % 256)
  1551.                 .BYTE (mt_tick0_c % 256)
  1552.                 .BYTE (mt_tick0_d % 256)
  1553.                 .BYTE (mt_tick0_e % 256)
  1554.                 .BYTE (mt_tick0_f % 256)
  1555.               .ENDIF
  1556.               .ENDIF
  1557.  
  1558.               .IF (NOEFFECTS == 0)
  1559.               .IF (!.DEFINED(mt_effectjumptbl))
  1560.               .IF ((NOTONEPORTA == 0) || (NOPORTAMENTO == 0) || (NOVIB == 0))
  1561. mt_effectjumptbl:
  1562.                 .BYTE (mt_effect_0 % 256)
  1563.                 .BYTE (mt_effect_12 % 256)
  1564.                 .BYTE (mt_effect_12 % 256)
  1565.                 .BYTE (mt_effect_3 % 256)
  1566.                 .BYTE (mt_effect_4 % 256)
  1567.               .ENDIF
  1568.               .ENDIF
  1569.               .ENDIF
  1570.  
  1571.               .IF (!.DEFINED(mt_funktempotbl))
  1572.               .IF (NOFUNKTEMPO == 0)
  1573. mt_funktempotbl:
  1574.                 .BYTE (8,5)
  1575.               .ENDIF
  1576.               .ENDIF
  1577.  
  1578.               .IF ((NOEFFECTS == 0) || (NOWAVEDELAY == 0) || (NOTRANS == 0) || (NOREPEAT == 0) || (FIXEDPARAMS == 0) || (ZPGHOSTREGS != 0) || (BUFFEREDWRITES != 0) || (NOCALCULATEDSPEED == 0))
  1579.  
  1580.               ;Normal channel variables
  1581.  
  1582. mt_chnsongptr:
  1583.                 .BYTE (0)
  1584. mt_chntrans:
  1585.                 .BYTE (0)
  1586. mt_chnrepeat:
  1587.                 .BYTE (0)
  1588. mt_chnpattptr:
  1589.                 .BYTE (0)
  1590. mt_chnpackedrest:
  1591.                 .BYTE (0)
  1592. mt_chnnewfx:
  1593.                 .BYTE (0)
  1594. mt_chnnewparam:
  1595.                 .BYTE (0)
  1596.  
  1597.               .IF (NUMCHANNELS > 1)
  1598.                 .BYTE (0,0,0,0,0,0,0)
  1599.               .ENDIF
  1600.               .IF (NUMCHANNELS > 2)
  1601.                 .BYTE (0,0,0,0,0,0,0)
  1602.               .ENDIF
  1603.  
  1604. mt_chnfx:
  1605.                 .BYTE (0)
  1606. mt_chnparam:
  1607.                 .BYTE (0)
  1608. mt_chnnewnote:
  1609.                 .BYTE (0)
  1610. mt_chnwaveptr:
  1611.                 .BYTE (0)
  1612. mt_chnwave:
  1613.                 .BYTE (0)
  1614. mt_chnpulseptr:
  1615.                 .BYTE (0)
  1616. mt_chnpulsetime:
  1617.                 .BYTE (0)
  1618.  
  1619.               .IF (NUMCHANNELS > 1)
  1620.                 .BYTE (0,0,0,0,0,0,0)
  1621.               .ENDIF
  1622.               .IF (NUMCHANNELS > 2)
  1623.                 .BYTE (0,0,0,0,0,0,0)
  1624.               .ENDIF
  1625.  
  1626. mt_chnsongnum:
  1627.                 .BYTE (0)
  1628. mt_chnpattnum:
  1629.                 .BYTE (0)
  1630. mt_chntempo:
  1631.                 .BYTE (0)
  1632. mt_chncounter:
  1633.                 .BYTE (0)
  1634. mt_chnnote:
  1635.                 .BYTE (0)
  1636. mt_chninstr:
  1637.                 .BYTE (1)
  1638. mt_chngate:
  1639.                 .BYTE ($fe)
  1640.  
  1641.               .IF (NUMCHANNELS > 1)
  1642.                 .BYTE (1,0,0,0,0,1,$fe)
  1643.               .ENDIF
  1644.               .IF (NUMCHANNELS > 2)
  1645.                 .BYTE (2,0,0,0,0,1,$fe)
  1646.               .ENDIF
  1647.  
  1648.               .IF ((ZPGHOSTREGS == 0) || (NOCALCULATEDSPEED == 0))
  1649.  
  1650. mt_chnvibtime:
  1651.                 .BYTE (0)
  1652. mt_chnvibdelay:
  1653.                 .BYTE (0)
  1654. mt_chnwavetime:
  1655.                 .BYTE (0)
  1656. mt_chnfreqlo:
  1657.                 .BYTE (0)
  1658. mt_chnfreqhi:
  1659.                 .BYTE (0)
  1660. mt_chnpulselo:
  1661.                 .BYTE (0)
  1662. mt_chnpulsehi:
  1663.                 .BYTE (0)
  1664.  
  1665.               .IF (NUMCHANNELS > 1)
  1666.                 .BYTE (0,0,0,0,0,0,0)
  1667.               .ENDIF
  1668.               .IF (NUMCHANNELS > 2)
  1669.                 .BYTE (0,0,0,0,0,0,0)
  1670.               .ENDIF
  1671.  
  1672.               .IF ((BUFFEREDWRITES != 0) || (FIXEDPARAMS == 0) || (NOCALCULATEDSPEED == 0))
  1673. mt_chnad:
  1674.                 .BYTE (0)
  1675. mt_chnsr:
  1676.                 .BYTE (0)
  1677. mt_chnsfx:
  1678.                 .BYTE (0)
  1679. mt_chnsfxlo:
  1680.                 .BYTE (0)
  1681. mt_chnsfxhi:
  1682.                 .BYTE (0)
  1683. mt_chngatetimer:
  1684.                 .BYTE (0)
  1685. mt_chnlastnote:
  1686.                 .BYTE (0)
  1687.  
  1688.               .IF (NUMCHANNELS > 1)
  1689.                 .BYTE (0,0,0,0,0,0,0)
  1690.               .ENDIF
  1691.               .IF (NUMCHANNELS > 2)
  1692.                 .BYTE (0,0,0,0,0,0,0)
  1693.               .ENDIF
  1694.  
  1695.               .ENDIF
  1696.  
  1697.               .ELSE
  1698.  
  1699. mt_chnvibtime:
  1700.                 .BYTE (0)
  1701. mt_chnvibdelay:
  1702.                 .BYTE (0)
  1703. mt_chnwavetime:
  1704.                 .BYTE (0)
  1705. mt_chnsfx:
  1706.                 .BYTE (0)
  1707. mt_chnsfxlo:
  1708.                 .BYTE (0)
  1709. mt_chnsfxhi:
  1710.                 .BYTE (0)
  1711. mt_chngatetimer:
  1712.                 .BYTE (0)
  1713.  
  1714.               .IF (NUMCHANNELS > 1)
  1715.                 .BYTE (0,0,0,0,0,0,0)
  1716.               .ENDIF
  1717.               .IF (NUMCHANNELS > 2)
  1718.                 .BYTE (0,0,0,0,0,0,0)
  1719.               .ENDIF
  1720.  
  1721.               .ENDIF
  1722.  
  1723.               .ELSE
  1724.  
  1725.               ;Optimized channel variables
  1726.  
  1727. mt_chnsongptr:
  1728.                 .BYTE (0)
  1729. mt_chnpattptr:
  1730.                 .BYTE (0)
  1731. mt_chnpackedrest:
  1732.                 .BYTE (0)
  1733. mt_chnnewnote:
  1734.                 .BYTE (0)
  1735. mt_chnwaveptr:
  1736.                 .BYTE (0)
  1737. mt_chnwave:
  1738.                 .BYTE (0)
  1739. mt_chnpulseptr:
  1740.                 .BYTE (0)
  1741.  
  1742.               .IF (NUMCHANNELS > 1)
  1743.                 .BYTE (0,0,0,0,0,0,0)
  1744.               .ENDIF
  1745.               .IF (NUMCHANNELS > 2)
  1746.                 .BYTE (0,0,0,0,0,0,0)
  1747.               .ENDIF
  1748.  
  1749. mt_chnpulsetime:
  1750.                 .BYTE (0)
  1751. mt_chnpulselo:
  1752.                 .BYTE (0)
  1753. mt_chnpulsehi:
  1754.                 .BYTE (0)
  1755. mt_chnvibtime:
  1756.                 .BYTE (0)
  1757. mt_chnvibdelay:
  1758.                 .BYTE (0)
  1759. mt_chnfreqlo:
  1760.                 .BYTE (0)
  1761. mt_chnfreqhi:
  1762.                 .BYTE (0)
  1763.  
  1764.               .IF (NUMCHANNELS > 1)
  1765.                 .BYTE (0,0,0,0,0,0,0)
  1766.               .ENDIF
  1767.               .IF (NUMCHANNELS > 2)
  1768.                 .BYTE (0,0,0,0,0,0,0)
  1769.               .ENDIF
  1770.  
  1771. mt_chnsongnum:
  1772.                 .BYTE (0)
  1773. mt_chnpattnum:
  1774.                 .BYTE (0)
  1775. mt_chntempo:
  1776.                 .BYTE (0)
  1777. mt_chncounter:
  1778.                 .BYTE (0)
  1779. mt_chnnote:
  1780.                 .BYTE (0)
  1781. mt_chninstr:
  1782.                 .BYTE (1)
  1783. mt_chngate:
  1784.                 .BYTE ($fe)
  1785.  
  1786.               .IF (NUMCHANNELS > 1)
  1787.                 .BYTE (1,0,0,0,0,1,$fe)
  1788.               .ENDIF
  1789.               .IF (NUMCHANNELS > 2)
  1790.                 .BYTE (2,0,0,0,0,1,$fe)
  1791.               .ENDIF
  1792.  
  1793.               .ENDIF
  1794.  
  1795.         ;Songdata & frequencytable will be inserted by the relocator here
  1796.  
  1797.