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

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