home *** CD-ROM | disk | FTP | other *** search
/ Large Pack of OldSkool DOS MOD Trackers / goattracker_2.73.zip / src / player.s < prev    next >
Text File  |  2014-07-23  |  55KB  |  1,833 lines

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