home *** CD-ROM | disk | FTP | other *** search
/ Large Pack of OldSkool DOS MOD Trackers / goattracker_2.72.zip / src / altplayer.s next >
Text File  |  2008-08-15  |  55KB  |  1,823 lines

  1. ;-------------------------------------------------------------------------------
  2. ; GoatTracker V2.68 alternative SID write order 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_loadregswave             ;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 ((BUFFEREDWRITES == 0) && (NOFIRSTWAVECMD != 0))
  840.                 sta SIDBASE+$04,x
  841.               .ENDIF
  842.               .IF ((NUMLEGATOINSTR > 0) || (NOFIRSTWAVECMD == 0))
  843.                 lda #$ff
  844. mt_skipwave2:
  845.                 sta mt_chngate,x                ;Reset gateflag
  846.               .ELSE
  847.                 inc mt_chngate,x
  848.               .ENDIF
  849. mt_skipwave:
  850.  
  851.               .IF ((BUFFEREDWRITES == 0) && (NOFIRSTWAVECMD == 0))
  852.                 lda mt_chnwave,x
  853.                 and mt_chngate,x
  854.                 sta SIDBASE+$04,x
  855.               .ENDIF
  856.  
  857.                 lda mt_inswaveptr-1,y           ;Load waveptr
  858.                 sta mt_chnwaveptr,x
  859.               .IF (NOPULSE == 0)
  860.                 lda mt_inspulseptr-1,y          ;Load pulseptr (if nonzero)
  861.                 beq mt_skippulse
  862.                 sta mt_chnpulseptr,x
  863.               .IF (NOPULSEMOD == 0)
  864.                 lda #$00                        ;Reset pulse step duration
  865.                 sta mt_chnpulsetime,x
  866.               .ENDIF
  867.               .ENDIF
  868. mt_skippulse:
  869.               .IF (NOFILTER == 0)
  870.                 lda mt_insfiltptr-1,y         ;Load filtptr (if nonzero)
  871.                 beq mt_skipfilt
  872.                 sta mt_filtstep+1
  873.               .IF (NOFILTERMOD == 0)
  874.                 lda #$00
  875.                 sta mt_filttime+1
  876.               .ENDIF
  877.               .ENDIF
  878. mt_skipfilt:
  879.  
  880.                 lda mt_insad-1,y                ;Load Attack/Decay
  881.               .IF (BUFFEREDWRITES == 0)
  882.                 sta SIDBASE+$05,x
  883.               .ELSE
  884.               .IF (ZPGHOSTREGS == 0)
  885.                 sta mt_chnad,x
  886.               .ELSE
  887.                 sta <ghostad,x
  888.               .ENDIF
  889.               .ENDIF
  890.                 lda mt_inssr-1,y                ;Load Sustain/Release
  891.               .IF (BUFFEREDWRITES == 0)
  892.                 sta SIDBASE+$06,x
  893.               .ELSE
  894.               .IF (ZPGHOSTREGS == 0)
  895.                 sta mt_chnsr,x
  896.               .ELSE
  897.                 sta <ghostsr,x
  898.               .ENDIF
  899.               .ENDIF
  900.  
  901.               .IF (NOEFFECTS == 0)
  902.                 lda mt_chnnewparam,x            ;Execute tick 0 FX after
  903. mt_tick0jump1:                                  ;newnote init
  904.               .IF (BUFFEREDWRITES == 0)         
  905.                 jmp mt_tick0_0                  
  906.               .ELSE
  907.                 jsr mt_tick0_0
  908.                 jmp mt_loadregs                               
  909.               .ENDIF
  910.               .ELSE
  911.               .IF (BUFFEREDWRITES == 0)
  912.                 rts
  913.               .ELSE                            
  914.                 jmp mt_loadregs
  915.               .ENDIF
  916.               .ENDIF
  917.  
  918.               .IF (NOWAVECMD == 0)
  919. mt_wavecmd:
  920.                 jmp mt_execwavecmd
  921.               .ENDIF
  922.  
  923.         ;Tick 0 effect execution
  924.  
  925. mt_nonewnoteinit:
  926.               .IF (NOEFFECTS == 0)
  927.                 lda mt_chnnewparam,x            ;No new note init: exec tick 0
  928. mt_tick0jump2:
  929.                 jsr mt_tick0_0                  ;FX, and wavetable afterwards
  930.               .ENDIF
  931.  
  932.         ;Wavetable execution
  933.  
  934. mt_waveexec:
  935.                 ldy mt_chnwaveptr,y
  936.                 beq mt_wavedone
  937.                 lda mt_wavetbl-1,y
  938.               .IF (NOWAVEDELAY == 0)
  939.                 cmp #$10                        ;0-15 used as delay
  940.                 bcs mt_nowavedelay              ;+ no wave change
  941.                 cmp mt_chnwavetime,x
  942.                 beq mt_nowavechange
  943.                 inc mt_chnwavetime,x
  944.                 bne mt_wavedone
  945. mt_nowavedelay:
  946.                 sbc #$10
  947.               .ELSE
  948.                 beq mt_nowavechange
  949.               .ENDIF
  950.               .IF (NOWAVECMD == 0)
  951.                 cmp #$e0
  952.                 bcs mt_nowavechange
  953.               .ENDIF
  954.                 sta mt_chnwave,x
  955. mt_nowavechange:
  956.                 lda mt_wavetbl,y
  957.                 cmp #LOOPWAVE                  ;Check for wavetable jump
  958.                 iny
  959.                 tya
  960.                 bcc mt_nowavejump
  961.               .IF (NOWAVECMD != 0)
  962.                 clc
  963.               .ENDIF
  964.                 lda mt_notetbl-1,y
  965. mt_nowavejump:
  966.                 sta mt_chnwaveptr,x
  967.               .IF (NOWAVEDELAY == 0)
  968.                 lda #$00
  969.                 sta mt_chnwavetime,x
  970.               .ENDIF
  971.  
  972.               .IF (NOWAVECMD == 0)
  973.                 lda mt_wavetbl-2,y
  974.                 cmp #$e0
  975.                 bcs mt_wavecmd
  976.               .ENDIF
  977.  
  978.                 lda mt_notetbl-2,y
  979.  
  980.               .IF ((NOTONEPORTA == 0) || (NOPORTAMENTO == 0) || (NOVIB == 0))
  981.                 bne mt_wavefreq                 ;No frequency-change?
  982.  
  983.         ;No frequency-change / continuous effect execution
  984.  
  985. mt_wavedone:
  986.               .IF (REALTIMEOPTIMIZATION != 0)
  987.                 lda mt_chncounter,x             ;No continuous effects on tick0
  988.               .IF (PULSEOPTIMIZATION != 0)
  989.                 beq mt_gatetimer
  990.               .ELSE
  991.                 beq mt_done
  992.               .ENDIF
  993.               .ENDIF
  994.               .IF (NOEFFECTS == 0)
  995.                 ldy mt_chnfx,y
  996.               .IF (NOWAVECMD == 0)
  997.               .IF (.DEFINED(mt_effectnum))
  998.                 sty mt_effectnum+1
  999.               .ENDIF
  1000.               .ENDIF
  1001.                 lda mt_effectjumptbl,y
  1002.                 sta mt_effectjump+1
  1003.                 ldy mt_chnparam,y
  1004.               .ELSE
  1005.                 ldy mt_chninstr,y
  1006.                 lda mt_insvibparam-1,y
  1007.                 tay
  1008.               .ENDIF
  1009. mt_setspeedparam:
  1010.               .IF (NOCALCULATEDSPEED != 0)
  1011.                 lda mt_speedrighttbl-1,y
  1012.                 sta <mt_temp1
  1013.               .ELSE
  1014.               .IF (NONORMALSPEED == 0)
  1015.                 lda mt_speedlefttbl-1,y
  1016.                 bmi mt_calculatedspeed
  1017. mt_normalspeed:
  1018.                 sta <mt_temp2
  1019.                 lda mt_speedrighttbl-1,y
  1020.                 sta <mt_temp1
  1021.                 jmp mt_effectjump
  1022.               .ELSE
  1023.               .IF (NOZEROSPEED == 0)
  1024.                 bne mt_calculatedspeed
  1025. mt_zerospeed:
  1026.                 sty <mt_temp1
  1027.                 sty <mt_temp2
  1028.                 beq mt_effectjump
  1029.               .ENDIF
  1030.               .ENDIF
  1031. mt_calculatedspeed:
  1032.                 lda mt_speedrighttbl-1,y
  1033.                 sta mt_cscount+1
  1034.                 sty mt_csresty+1
  1035.                 ldy mt_chnlastnote,y
  1036.                 lda mt_freqtbllo+1-FIRSTNOTE,y
  1037.                 sec
  1038.                 sbc mt_freqtbllo-FIRSTNOTE,y
  1039.                 sta <mt_temp1
  1040.                 lda mt_freqtblhi+1-FIRSTNOTE,y
  1041.                 sbc mt_freqtblhi-FIRSTNOTE,y
  1042. mt_cscount:     ldy #$00
  1043.                 beq mt_csresty
  1044. mt_csloop:      lsr
  1045.                 ror <mt_temp1
  1046.                 dey
  1047.                 bne mt_csloop
  1048. mt_csresty:     ldy #$00
  1049.                 sta <mt_temp2
  1050.               .ENDIF
  1051. mt_effectjump:
  1052.                 jmp mt_effect_0
  1053.               .ELSE
  1054.                 beq mt_wavedone
  1055.               .ENDIF
  1056.  
  1057.         ;Setting note frequency
  1058.  
  1059. mt_wavefreq:
  1060.                 bpl mt_wavenoteabs
  1061.                 adc mt_chnnote,x
  1062.                 and #$7f
  1063. mt_wavenoteabs:
  1064.               .IF (NOCALCULATEDSPEED == 0)
  1065.                 sta mt_chnlastnote,x
  1066.               .ENDIF
  1067.                 tay
  1068. mt_wavenote:
  1069.               .IF (NOVIB == 0)
  1070.                 lda #$00                        ;Reset vibrato phase
  1071.                 sta mt_chnvibtime,x
  1072.               .ENDIF
  1073.                 lda mt_freqtbllo-FIRSTNOTE,y
  1074.               .IF (ZPGHOSTREGS == 0)
  1075.                 sta mt_chnfreqlo,x
  1076.                 lda mt_freqtblhi-FIRSTNOTE,y
  1077. mt_storefreqhi:
  1078.                 sta mt_chnfreqhi,x
  1079.               .ELSE
  1080.                 sta <ghostfreqlo,x
  1081.                 lda mt_freqtblhi-FIRSTNOTE,y
  1082. mt_storefreqhi:
  1083.                 sta <ghostfreqhi,x
  1084.               .ENDIF
  1085.  
  1086.         ;Check for new note fetch
  1087.  
  1088.               .IF ((NOTONEPORTA != 0) && (NOPORTAMENTO != 0) && (NOVIB != 0))
  1089. mt_wavedone:
  1090.               .ENDIF
  1091. mt_done:
  1092.               .IF (PULSEOPTIMIZATION != 0)
  1093.                 lda mt_chncounter,x             ;Check for gateoff timer
  1094. mt_gatetimer:
  1095.               .IF (FIXEDPARAMS == 0)
  1096.                 cmp mt_chngatetimer,x
  1097.               .ELSE
  1098.                 cmp #GATETIMERPARAM
  1099.               .ENDIF
  1100.  
  1101.                 beq mt_getnewnote               ;Fetch new notes if equal
  1102.               .ENDIF
  1103.  
  1104.         ;Pulse execution
  1105.               .IF (NOPULSE == 0)
  1106. mt_pulseexec:
  1107.                 ldy mt_chnpulseptr,y            ;See if pulse stopped
  1108.                 beq mt_pulseskip
  1109.               .IF (PULSEOPTIMIZATION != 0)
  1110.                 ora mt_chnpattptr,x             ;Skip when sequencer executed
  1111.                 beq mt_pulseskip
  1112.               .ENDIF
  1113.               .IF (NOPULSEMOD == 0)
  1114.                 lda mt_chnpulsetime,x           ;Pulse step counter time left?
  1115.                 bne mt_pulsemod
  1116.               .ENDIF
  1117. mt_newpulsestep:
  1118.                 lda mt_pulsetimetbl-1,y         ;Set pulse, or new modulation
  1119.               .IF (NOPULSEMOD == 0)
  1120.                 bpl mt_newpulsemod              ;step?
  1121.               .ENDIF
  1122. mt_setpulse:
  1123.               .IF (SIMPLEPULSE == 0)
  1124.               .IF (ZPGHOSTREGS == 0)
  1125.                 sta mt_chnpulsehi,x             ;Highbyte
  1126.               .ELSE
  1127.                 sta <ghostpulsehi,x
  1128.               .ENDIF
  1129.               .ENDIF
  1130.                 lda mt_pulsespdtbl-1,y          ;Lowbyte
  1131.               .IF (ZPGHOSTREGS == 0)
  1132.                 sta mt_chnpulselo,x
  1133.               .ELSE
  1134.                 sta <ghostpulselo,x
  1135.               .IF (SIMPLEPULSE != 0)
  1136.                 sta <ghostpulsehi,x
  1137.               .ENDIF
  1138.               .ENDIF
  1139.               .IF (NOPULSEMOD == 0)
  1140.                 jmp mt_nextpulsestep
  1141. mt_newpulsemod:
  1142.                 sta mt_chnpulsetime,x
  1143. mt_pulsemod:
  1144.               .IF (SIMPLEPULSE == 0)
  1145.                 lda mt_pulsespdtbl-1,y          ;Take pulse speed
  1146.                 clc
  1147.                 bpl mt_pulseup
  1148.               .IF (ZPGHOSTREGS == 0)
  1149.                 dec mt_chnpulsehi,x
  1150. mt_pulseup:
  1151.                 adc mt_chnpulselo,x             ;Add pulse lowbyte
  1152.                 sta mt_chnpulselo,x
  1153.                 bcc mt_pulsenotover
  1154.                 inc mt_chnpulsehi,x
  1155.               .ELSE
  1156.                 dec <ghostpulsehi,x
  1157. mt_pulseup:
  1158.                 adc <ghostpulselo,x             ;Add pulse lowbyte
  1159.                 sta <ghostpulselo,x
  1160.                 bcc mt_pulsenotover
  1161.                 inc <ghostpulsehi,x
  1162.               .ENDIF
  1163. mt_pulsenotover:
  1164.               .ELSE
  1165.               .IF (ZPGHOSTREGS == 0)
  1166.                 lda mt_chnpulselo,x
  1167.                 clc
  1168.                 adc mt_pulsespdtbl-1,y
  1169.                 adc #$00
  1170.                 sta mt_chnpulselo,x
  1171.               .ELSE
  1172.                 lda <ghostpulselo,x
  1173.                 clc
  1174.                 adc mt_pulsespdtbl-1,y
  1175.                 adc #$00
  1176.                 sta <ghostpulselo,x
  1177.                 sta <ghostpulsehi,x
  1178.               .ENDIF
  1179.               .ENDIF
  1180.                 dec mt_chnpulsetime,x
  1181.                 bne mt_pulsedone2
  1182.               .ENDIF
  1183.  
  1184. mt_nextpulsestep:
  1185.                 lda mt_pulsetimetbl,y           ;Jump in pulsetable?
  1186.                 cmp #LOOPPULSE
  1187.                 iny
  1188.                 tya
  1189.                 bcc mt_nopulsejump
  1190.                 lda mt_pulsespdtbl-1,y          ;Take jump point
  1191. mt_nopulsejump:
  1192.                 sta mt_chnpulseptr,x
  1193. mt_pulsedone:
  1194.               .IF (BUFFEREDWRITES == 0)
  1195.                 lda mt_chnpulselo,x
  1196.               .ENDIF
  1197. mt_pulsedone2:
  1198.               .IF (BUFFEREDWRITES == 0)
  1199.                 sta SIDBASE+$02,x
  1200.               .IF (SIMPLEPULSE == 0)
  1201.                 lda mt_chnpulsehi,x
  1202.               .ENDIF
  1203.                 sta SIDBASE+$03,x
  1204.               .ENDIF
  1205. mt_pulseskip:
  1206.               .ENDIF
  1207.  
  1208.               .IF (PULSEOPTIMIZATION == 0)
  1209.                 lda mt_chncounter,x             ;Check for gateoff timer
  1210. mt_gatetimer:
  1211.               .IF (FIXEDPARAMS == 0)
  1212.                 cmp mt_chngatetimer,x
  1213.               .ELSE
  1214.                 cmp #GATETIMERPARAM
  1215.               .ENDIF
  1216.  
  1217.                 beq mt_getnewnote               ;Fetch new notes if equal
  1218.               .ENDIF
  1219.  
  1220.                 jmp mt_loadregs
  1221.  
  1222.         ;New note fetch
  1223.  
  1224. mt_getnewnote:
  1225.                 ldy mt_chnpattnum,y
  1226.                 lda mt_patttbllo,y
  1227.                 sta <mt_temp1
  1228.                 lda mt_patttblhi,y
  1229.                 sta <mt_temp2
  1230.                 ldy mt_chnpattptr,y
  1231.                 lda (mt_temp1),y
  1232.                 cmp #FX
  1233.                 bcc mt_instr                    ;Instr. change
  1234.               .IF (NOEFFECTS == 0)
  1235.                 cmp #NOTE
  1236.                 bcc mt_fx                       ;FX
  1237.               .ENDIF
  1238.                 cmp #FIRSTPACKEDREST
  1239.                 bcc mt_note                     ;Note only
  1240.  
  1241.         ;Packed rest handling
  1242.  
  1243. mt_packedrest:
  1244.                 lda mt_chnpackedrest,x
  1245.                 bne mt_packedrestnonew
  1246.                 lda (mt_temp1),y
  1247. mt_packedrestnonew:
  1248.                 adc #$00
  1249.                 sta mt_chnpackedrest,x
  1250.                 beq mt_rest
  1251.                 bne mt_loadregs
  1252.  
  1253.         ;Instrument change
  1254.  
  1255. mt_instr:
  1256.                 sta mt_chninstr,x               ;Instrument change, followed
  1257.                 iny
  1258.                 lda (mt_temp1),y                ;by either FX or note
  1259.  
  1260.               .IF (NOEFFECTS == 0)
  1261.                 cmp #NOTE
  1262.                 bcs mt_note
  1263.  
  1264.         ;Effect change
  1265.  
  1266. mt_fx:
  1267.                 cmp #FXONLY                     ;Note follows?
  1268.                 and #$0f
  1269.                 sta mt_chnnewfx,x
  1270.                 beq mt_fx_noparam               ;Effect 0 - no param.
  1271.                 iny
  1272.                 lda (mt_temp1),y
  1273.                 sta mt_chnnewparam,x
  1274. mt_fx_noparam:
  1275.                 bcs mt_rest
  1276. mt_fx_getnote:
  1277.                 iny
  1278.                 lda (mt_temp1),y
  1279.               .ENDIF
  1280.  
  1281.         ;Note handling
  1282.  
  1283. mt_note:
  1284.                 cmp #REST                   ;Rest or gateoff/on?
  1285.               .IF (NOGATE == 0)
  1286.                 bcc mt_normalnote
  1287.               .ENDIF
  1288.                 beq mt_rest
  1289. mt_gate:
  1290.               .IF (NOGATE == 0)
  1291.                 ora #$f0
  1292.                 bne mt_setgate
  1293.               .ENDIF
  1294.  
  1295.         ;Prepare for note start; perform hardrestart
  1296.  
  1297. mt_normalnote:
  1298.               .IF (NOTRANS == 0)
  1299.                 adc mt_chntrans,x
  1300.               .ENDIF
  1301.                 sta mt_chnnewnote,x
  1302.               .IF (NOTONEPORTA == 0)
  1303.                 lda mt_chnnewfx,x           ;If toneportamento, no gateoff
  1304.                 cmp #TONEPORTA
  1305.                 beq mt_rest
  1306.               .ENDIF
  1307.               .IF (((NUMHRINSTR > 0) && (NUMNOHRINSTR > 0)) || (NUMLEGATOINSTR > 0))
  1308.                 lda mt_chninstr,x
  1309.                 cmp #FIRSTNOHRINSTR         ;Instrument order:
  1310.               .IF (NUMLEGATOINSTR > 0)
  1311.                 bcs mt_nohr_legato          ;With HR - no HR - legato
  1312.               .ELSE
  1313.                 bcs mt_skiphr
  1314.               .ENDIF
  1315.               .ENDIF
  1316.               .IF (NUMHRINSTR > 0)
  1317.                 lda #ADPARAM                ;Hard restart
  1318.               .IF (BUFFEREDWRITES == 0)
  1319.                 sta SIDBASE+$05,x
  1320.               .ELSE
  1321.               .IF (ZPGHOSTREGS == 0)
  1322.                 sta mt_chnad,x
  1323.               .ELSE
  1324.                 sta <ghostad,x
  1325.               .ENDIF
  1326.               .ENDIF
  1327.                 lda #SRPARAM
  1328.               .IF (BUFFEREDWRITES == 0)
  1329.                 sta SIDBASE+$06,x
  1330.               .ELSE
  1331.               .IF (ZPGHOSTREGS == 0)
  1332.                 sta mt_chnsr,x
  1333.               .ELSE
  1334.                 sta <ghostsr,x
  1335.               .ENDIF
  1336.               .ENDIF
  1337.               .ENDIF
  1338. mt_skiphr:
  1339.                 lda #$fe
  1340. mt_setgate:
  1341.                 sta mt_chngate,x
  1342.  
  1343.         ;Check for end of pattern
  1344.  
  1345. mt_rest:
  1346.                 iny
  1347.                 lda (mt_temp1),y
  1348.                 beq mt_endpatt
  1349.                 tya
  1350. mt_endpatt:
  1351.                 sta mt_chnpattptr,x
  1352.  
  1353.         ;Load voice registers
  1354.  
  1355. mt_loadregs:
  1356.               .IF (BUFFEREDWRITES == 0)
  1357. mt_loadregswave:lda mt_chnwave,x
  1358.                 and mt_chngate,x
  1359.                 sta SIDBASE+$04,x
  1360.                 lda mt_chnfreqlo,x
  1361.                 sta SIDBASE+$00,x
  1362.                 lda mt_chnfreqhi,x
  1363.                 sta SIDBASE+$01,x
  1364.               .ELSE
  1365.               .IF (SOUNDSUPPORT != 0)
  1366.                 ldy mt_chnsfx,y
  1367.                 bne mt_sfxexec
  1368.               .ENDIF
  1369.               .IF (ZPGHOSTREGS == 0)
  1370. mt_loadregswave:lda mt_chnwave,x
  1371.                 and mt_chngate,x
  1372.                 sta SIDBASE+$04,x
  1373.                 lda mt_chnfreqlo,x
  1374.                 sta SIDBASE+$00,x
  1375.                 lda mt_chnfreqhi,x
  1376.                 sta SIDBASE+$01,x
  1377.                 lda mt_chnpulselo,x
  1378.               .IF (SIMPLEPULSE == 0)
  1379.                 sta SIDBASE+$02,x
  1380.                 lda mt_chnpulsehi,x
  1381.                 sta SIDBASE+$03,x
  1382.               .ELSE
  1383.                 sta SIDBASE+$02,x
  1384.                 sta SIDBASE+$03,x
  1385.               .ENDIF
  1386.               .IF (SOUNDSUPPORT != 0)
  1387.                 lda mt_chnsfx,x
  1388.                 bne mt_loadskipadsr
  1389.               .ENDIF
  1390.                 lda mt_chnad,x
  1391.                 sta SIDBASE+$05,x
  1392.                 lda mt_chnsr,x
  1393.                 sta SIDBASE+$06,x
  1394. mt_loadskipadsr:
  1395.               .ELSE
  1396. mt_loadregswave:lda mt_chnwave,x
  1397.                 and mt_chngate,x
  1398.                 sta <ghostwave,x
  1399.               .ENDIF
  1400.               .ENDIF
  1401.                 rts
  1402.  
  1403.               .IF (NUMLEGATOINSTR > 0)
  1404. mt_nohr_legato:
  1405.                 cmp #FIRSTLEGATOINSTR
  1406.                 bcc mt_skiphr
  1407.                 bcs mt_rest
  1408.               .ENDIF
  1409.  
  1410.         ;Sound FX code
  1411.  
  1412.               .IF (SOUNDSUPPORT != 0)
  1413.               .IF (ZPGHOSTREGS == 0)
  1414.  
  1415.         ;Sound FX code without ghostregs
  1416.  
  1417. mt_sfxexec:     lda mt_chnsfxlo,x
  1418.                 sta <mt_temp1
  1419.                 lda mt_chnsfxhi,x
  1420.                 sta <mt_temp2
  1421.                 lda #$fe
  1422.                 sta mt_chngate,x
  1423.                 lda #$00
  1424.                 sta mt_chnwaveptr,x
  1425.                 inc mt_chnsfx,x
  1426.                 cpy #$02
  1427.                 beq mt_sfxexec_frame0
  1428.                 bcs mt_sfxexec_framen
  1429.                 sta SIDBASE+$05,x                ;Hardrestart before sound FX
  1430.                 sta SIDBASE+$06,x                ;begins
  1431.                 bcc mt_loadregswave
  1432. mt_sfxexec_frame0:
  1433.                 tay
  1434.                 lda (mt_temp1),y           ;Load ADSR
  1435.                 sta SIDBASE+$05,x
  1436.                 iny
  1437.                 lda (mt_temp1),y
  1438.                 sta SIDBASE+$06,x
  1439.                 iny
  1440.                 lda (mt_temp1),y           ;Load pulse
  1441.                 sta SIDBASE+$02,x
  1442.                 sta SIDBASE+$03,x
  1443.                 lda #$09                   ;Testbit
  1444. mt_sfxexec_wavechg:
  1445.                 sta mt_chnwave,x
  1446.                 sta SIDBASE+$04,x
  1447. mt_sfxexec_done:
  1448.                 rts
  1449. mt_sfxexec_framen:
  1450.                 lda (mt_temp1),y
  1451.                 bne mt_sfxexec_noend
  1452. mt_sfxexec_end:
  1453.                 sta mt_chnsfx,x
  1454.                 beq mt_sfxexec_wavechg
  1455. mt_sfxexec_noend:
  1456.                 tay
  1457.                 lda mt_freqtbllo-$80,y        ;Get frequency
  1458.                 sta SIDBASE+$00,x
  1459.                 lda mt_freqtblhi-$80,y
  1460.                 sta SIDBASE+$01,x
  1461.                 ldy mt_chnsfx,y
  1462.                 lda (mt_temp1),y              ;Then take a look at the next
  1463.                 beq mt_sfxexec_done           ;byte
  1464.                 cmp #$82                      ;Is it a waveform or a note?
  1465.                 bcs mt_sfxexec_done
  1466.                 inc mt_chnsfx,x
  1467.                 bcc mt_sfxexec_wavechg
  1468.  
  1469.               .ELSE
  1470.  
  1471.         ;Sound FX code with ghostregs
  1472.  
  1473. mt_sfxexec:
  1474.                 lda mt_chnsfxlo,x
  1475.                 sta <mt_temp1
  1476.                 lda mt_chnsfxhi,x
  1477.                 sta <mt_temp2
  1478.                 lda #$fe
  1479.                 sta mt_chngate,x
  1480.                 lda #$00
  1481.                 sta mt_chnwaveptr,x
  1482.                 inc mt_chnsfx,x
  1483.                 cpy #$02
  1484.                 bcc mt_sfxexec_fr1                  ;Hardrestart frame?
  1485.                 beq mt_sfxexec_fr2                  ;First or nth frame?
  1486. mt_sfxexec_fr3:
  1487.                 lda (mt_temp1),y
  1488.                 bne mt_sfxexec_noend
  1489. mt_sfxexec_end:
  1490.                 sta mt_chnsfx,x
  1491.                 beq mt_sfxexec_wavechg
  1492. mt_sfxexec_noend:
  1493.                 tay
  1494.                 lda mt_freqtbllo-$80,y        ;Get frequency
  1495.                 sta <ghostfreqlo,x
  1496.                 lda mt_freqtblhi-$80,y
  1497.                 sta <ghostfreqhi,x
  1498.                 ldy mt_chnsfx,y
  1499.                 lda (mt_temp1),y              ;Then take a look at the next
  1500.                 beq mt_sfxexec_done           ;byte
  1501.                 cmp #$82                      ;Is it a waveform or a note?
  1502.                 bcs mt_sfxexec_done
  1503.                 inc mt_chnsfx,x
  1504. mt_sfxexec_wavechg:
  1505.                 sta mt_chnwave,x
  1506.                 sta <ghostwave,x
  1507. mt_sfxexec_done:
  1508.                 ldy #$00
  1509.                 lda (mt_temp1),y             ;Load ADSR
  1510.                 sta <ghostad,x
  1511.                 iny
  1512.                 lda (mt_temp1),y
  1513.                 sta <ghostsr,x
  1514.                 iny
  1515.                 lda (mt_temp1),y             ;Load pulse
  1516.                 sta <ghostpulselo,x
  1517.                 sta <ghostpulsehi,x
  1518.                 rts
  1519.  
  1520. mt_sfxexec_fr1:
  1521.                 sta <ghostad,x               ;Hardrestart before sound FX
  1522.                 sta <ghostsr,x               ;begins
  1523.                 bcc mt_loadregswave
  1524.  
  1525. mt_sfxexec_fr2:
  1526.                 lda #$09
  1527.                 bne mt_sfxexec_wavechg
  1528.  
  1529.               .ENDIF
  1530.               .ENDIF
  1531.  
  1532.         ;Wavetable command exec
  1533.  
  1534.               .IF (NOWAVECMD == 0)
  1535. mt_execwavecmd:
  1536.                 and #$0f
  1537.                 sta <mt_temp1
  1538.                 lda mt_notetbl-2,y
  1539.                 sta <mt_temp2
  1540.                 ldy <mt_temp1
  1541.               .IF ((NOTONEPORTA == 0) || (NOPORTAMENTO == 0) || (NOVIB == 0))
  1542.                 cpy #$05
  1543.                 bcs mt_execwavetick0
  1544. mt_execwavetickn:
  1545.               .IF (.DEFINED(mt_effectnum))
  1546.                 sty mt_effectnum+1
  1547.               .ENDIF
  1548.                 lda mt_effectjumptbl,y
  1549.                 sta mt_effectjump+1
  1550.                 ldy <mt_temp2
  1551.                 jmp mt_setspeedparam
  1552.               .ENDIF
  1553. mt_execwavetick0:
  1554.                 lda mt_tick0jumptbl,y
  1555.                 sta mt_execwavetick0jump+1
  1556.                 lda <mt_temp2
  1557. mt_execwavetick0jump:
  1558.                 jsr mt_tick0_0
  1559.                 jmp mt_done
  1560.               .ENDIF
  1561.  
  1562.               .IF (NOEFFECTS == 0)
  1563.               .IF (!.DEFINED(mt_tick0jumptbl))
  1564. mt_tick0jumptbl:
  1565.                 .BYTE (mt_tick0_0 % 256)
  1566.                 .BYTE (mt_tick0_12 % 256)
  1567.                 .BYTE (mt_tick0_12 % 256)
  1568.                 .BYTE (mt_tick0_34 % 256)
  1569.                 .BYTE (mt_tick0_34 % 256)
  1570.                 .BYTE (mt_tick0_5 % 256)
  1571.                 .BYTE (mt_tick0_6 % 256)
  1572.                 .BYTE (mt_tick0_7 % 256)
  1573.                 .BYTE (mt_tick0_8 % 256)
  1574.                 .BYTE (mt_tick0_9 % 256)
  1575.                 .BYTE (mt_tick0_a % 256)
  1576.                 .BYTE (mt_tick0_b % 256)
  1577.                 .BYTE (mt_tick0_c % 256)
  1578.                 .BYTE (mt_tick0_d % 256)
  1579.                 .BYTE (mt_tick0_e % 256)
  1580.                 .BYTE (mt_tick0_f % 256)
  1581.               .ENDIF
  1582.               .ENDIF
  1583.  
  1584.               .IF (NOEFFECTS == 0)
  1585.               .IF (!.DEFINED(mt_effectjumptbl))
  1586.               .IF ((NOTONEPORTA == 0) || (NOPORTAMENTO == 0) || (NOVIB == 0))
  1587. mt_effectjumptbl:
  1588.                 .BYTE (mt_effect_0 % 256)
  1589.                 .BYTE (mt_effect_12 % 256)
  1590.                 .BYTE (mt_effect_12 % 256)
  1591.                 .BYTE (mt_effect_3 % 256)
  1592.                 .BYTE (mt_effect_4 % 256)
  1593.               .ENDIF
  1594.               .ENDIF
  1595.               .ENDIF
  1596.  
  1597.               .IF (!.DEFINED(mt_funktempotbl))
  1598.               .IF (NOFUNKTEMPO == 0)
  1599. mt_funktempotbl:
  1600.                 .BYTE (8,5)
  1601.               .ENDIF
  1602.               .ENDIF
  1603.  
  1604.               .IF ((NOEFFECTS == 0) || (NOWAVEDELAY == 0) || (NOTRANS == 0) || (NOREPEAT == 0) || (FIXEDPARAMS == 0) || (ZPGHOSTREGS != 0) || (BUFFEREDWRITES != 0) || (NOCALCULATEDSPEED == 0))
  1605.  
  1606.               ;Normal channel variables
  1607.  
  1608. mt_chnsongptr:
  1609.                 .BYTE (0)
  1610. mt_chntrans:
  1611.                 .BYTE (0)
  1612. mt_chnrepeat:
  1613.                 .BYTE (0)
  1614. mt_chnpattptr:
  1615.                 .BYTE (0)
  1616. mt_chnpackedrest:
  1617.                 .BYTE (0)
  1618. mt_chnnewfx:
  1619.                 .BYTE (0)
  1620. mt_chnnewparam:
  1621.                 .BYTE (0)
  1622.  
  1623.               .IF (NUMCHANNELS > 1)
  1624.                 .BYTE (0,0,0,0,0,0,0)
  1625.               .ENDIF
  1626.               .IF (NUMCHANNELS > 2)
  1627.                 .BYTE (0,0,0,0,0,0,0)
  1628.               .ENDIF
  1629.  
  1630. mt_chnfx:
  1631.                 .BYTE (0)
  1632. mt_chnparam:
  1633.                 .BYTE (0)
  1634. mt_chnnewnote:
  1635.                 .BYTE (0)
  1636. mt_chnwaveptr:
  1637.                 .BYTE (0)
  1638. mt_chnwave:
  1639.                 .BYTE (0)
  1640. mt_chnpulseptr:
  1641.                 .BYTE (0)
  1642. mt_chnpulsetime:
  1643.                 .BYTE (0)
  1644.  
  1645.               .IF (NUMCHANNELS > 1)
  1646.                 .BYTE (0,0,0,0,0,0,0)
  1647.               .ENDIF
  1648.               .IF (NUMCHANNELS > 2)
  1649.                 .BYTE (0,0,0,0,0,0,0)
  1650.               .ENDIF
  1651.  
  1652. mt_chnsongnum:
  1653.                 .BYTE (0)
  1654. mt_chnpattnum:
  1655.                 .BYTE (0)
  1656. mt_chntempo:
  1657.                 .BYTE (0)
  1658. mt_chncounter:
  1659.                 .BYTE (0)
  1660. mt_chnnote:
  1661.                 .BYTE (0)
  1662. mt_chninstr:
  1663.                 .BYTE (1)
  1664. mt_chngate:
  1665.                 .BYTE ($fe)
  1666.  
  1667.               .IF (NUMCHANNELS > 1)
  1668.                 .BYTE (1,0,0,0,0,1,$fe)
  1669.               .ENDIF
  1670.               .IF (NUMCHANNELS > 2)
  1671.                 .BYTE (2,0,0,0,0,1,$fe)
  1672.               .ENDIF
  1673.  
  1674.               .IF ((ZPGHOSTREGS == 0) || (NOCALCULATEDSPEED == 0))
  1675.  
  1676. mt_chnvibtime:
  1677.                 .BYTE (0)
  1678. mt_chnvibdelay:
  1679.                 .BYTE (0)
  1680. mt_chnwavetime:
  1681.                 .BYTE (0)
  1682. mt_chnfreqlo:
  1683.                 .BYTE (0)
  1684. mt_chnfreqhi:
  1685.                 .BYTE (0)
  1686. mt_chnpulselo:
  1687.                 .BYTE (0)
  1688. mt_chnpulsehi:
  1689.                 .BYTE (0)
  1690.  
  1691.               .IF (NUMCHANNELS > 1)
  1692.                 .BYTE (0,0,0,0,0,0,0)
  1693.               .ENDIF
  1694.               .IF (NUMCHANNELS > 2)
  1695.                 .BYTE (0,0,0,0,0,0,0)
  1696.               .ENDIF
  1697.  
  1698.               .IF ((BUFFEREDWRITES != 0) || (FIXEDPARAMS == 0) || (NOCALCULATEDSPEED == 0))
  1699. mt_chnad:
  1700.                 .BYTE (0)
  1701. mt_chnsr:
  1702.                 .BYTE (0)
  1703. mt_chnsfx:
  1704.                 .BYTE (0)
  1705. mt_chnsfxlo:
  1706.                 .BYTE (0)
  1707. mt_chnsfxhi:
  1708.                 .BYTE (0)
  1709. mt_chngatetimer:
  1710.                 .BYTE (0)
  1711. mt_chnlastnote:
  1712.                 .BYTE (0)
  1713.  
  1714.               .IF (NUMCHANNELS > 1)
  1715.                 .BYTE (0,0,0,0,0,0,0)
  1716.               .ENDIF
  1717.               .IF (NUMCHANNELS > 2)
  1718.                 .BYTE (0,0,0,0,0,0,0)
  1719.               .ENDIF
  1720.  
  1721.               .ENDIF
  1722.  
  1723.               .ELSE
  1724.  
  1725. mt_chnvibtime:
  1726.                 .BYTE (0)
  1727. mt_chnvibdelay:
  1728.                 .BYTE (0)
  1729. mt_chnwavetime:
  1730.                 .BYTE (0)
  1731. mt_chnsfx:
  1732.                 .BYTE (0)
  1733. mt_chnsfxlo:
  1734.                 .BYTE (0)
  1735. mt_chnsfxhi:
  1736.                 .BYTE (0)
  1737. mt_chngatetimer:
  1738.                 .BYTE (0)
  1739.  
  1740.               .IF (NUMCHANNELS > 1)
  1741.                 .BYTE (0,0,0,0,0,0,0)
  1742.               .ENDIF
  1743.               .IF (NUMCHANNELS > 2)
  1744.                 .BYTE (0,0,0,0,0,0,0)
  1745.               .ENDIF
  1746.  
  1747.               .ENDIF
  1748.  
  1749.               .ELSE
  1750.  
  1751.               ;Optimized channel variables
  1752.  
  1753. mt_chnsongptr:
  1754.                 .BYTE (0)
  1755. mt_chnpattptr:
  1756.                 .BYTE (0)
  1757. mt_chnpackedrest:
  1758.                 .BYTE (0)
  1759. mt_chnnewnote:
  1760.                 .BYTE (0)
  1761. mt_chnwaveptr:
  1762.                 .BYTE (0)
  1763. mt_chnwave:
  1764.                 .BYTE (0)
  1765. mt_chnpulseptr:
  1766.                 .BYTE (0)
  1767.  
  1768.               .IF (NUMCHANNELS > 1)
  1769.                 .BYTE (0,0,0,0,0,0,0)
  1770.               .ENDIF
  1771.               .IF (NUMCHANNELS > 2)
  1772.                 .BYTE (0,0,0,0,0,0,0)
  1773.               .ENDIF
  1774.  
  1775. mt_chnpulsetime:
  1776.                 .BYTE (0)
  1777. mt_chnpulselo:
  1778.                 .BYTE (0)
  1779. mt_chnpulsehi:
  1780.                 .BYTE (0)
  1781. mt_chnvibtime:
  1782.                 .BYTE (0)
  1783. mt_chnvibdelay:
  1784.                 .BYTE (0)
  1785. mt_chnfreqlo:
  1786.                 .BYTE (0)
  1787. mt_chnfreqhi:
  1788.                 .BYTE (0)
  1789.  
  1790.               .IF (NUMCHANNELS > 1)
  1791.                 .BYTE (0,0,0,0,0,0,0)
  1792.               .ENDIF
  1793.               .IF (NUMCHANNELS > 2)
  1794.                 .BYTE (0,0,0,0,0,0,0)
  1795.               .ENDIF
  1796.  
  1797. mt_chnsongnum:
  1798.                 .BYTE (0)
  1799. mt_chnpattnum:
  1800.                 .BYTE (0)
  1801. mt_chntempo:
  1802.                 .BYTE (0)
  1803. mt_chncounter:
  1804.                 .BYTE (0)
  1805. mt_chnnote:
  1806.                 .BYTE (0)
  1807. mt_chninstr:
  1808.                 .BYTE (1)
  1809. mt_chngate:
  1810.                 .BYTE ($fe)
  1811.  
  1812.               .IF (NUMCHANNELS > 1)
  1813.                 .BYTE (1,0,0,0,0,1,$fe)
  1814.               .ENDIF
  1815.               .IF (NUMCHANNELS > 2)
  1816.                 .BYTE (2,0,0,0,0,1,$fe)
  1817.               .ENDIF
  1818.  
  1819.               .ENDIF
  1820.  
  1821.         ;Songdata & frequencytable will be inserted by the relocator here
  1822.  
  1823.