Add tools/ and manual/, move sources to src/
[dpadhero2.git] / src / sound / tonal.asm
blobbfda32625c7b3e7ad15e04da92f8ce889f45f89f
2 ; Copyright (C) 2004, 2005 Kent Hansen.
4 ; This file is part of Neotoxin.
6 ; Neotoxin is free software; you can redistribute it and/or modify
7 ; it under the terms of the GNU General Public License as published by
8 ; the Free Software Foundation; either version 2 of the License, or
9 ; (at your option) any later version.
11 ; Neotoxin is distributed in the hope that it will be useful,
12 ; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ; GNU General Public License for more details.
16 ; You should have received a copy of the GNU General Public License
17 ; along with this program; if not, write to the Free Software
18 ; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 ; Description:
22 ; Handles pattern commands for channels 0,1,2,3.
24 .include <common/tablecall.h>
25 .include "mixer.h"
26 .include "track.h"
28 .dataseg
30 global_transpose .db
32 .public global_transpose
34 .codeseg
36 .public process_tonal_pattern_byte
38 .extrn mixer:mixer_state
39 .extrn table_call:proc
40 .extrn fetch_pattern_byte:proc
41 .extrn set_track_speed:proc
42 .extrn set_all_tracks_speed:proc
43 .extrn period_table_lo:byte
44 .extrn period_table_hi:byte
45 .extrn instrument_table:ptr
46 .extrn tracks:track_state
48 ; Processes one byte received on channel.
49 ; Params: A = byte
50 ; X = offset of channel data
52 .proc process_tonal_pattern_byte
53 ora #0
54 bpl is_note ; if bit 7 clear, it's a note #
56 cmp #$E0 ; is it a command?
57 bcc is_append
58 cmp #$F0
59 and #$0F
60 bcs is_command
61 ; set effect + param
62 sta mixer.tonals.effect.kind,x
63 beq +
64 jsr fetch_pattern_byte
65 sta mixer.tonals.effect.slide.amount,x ; this is a union
66 lda #0
67 lda mixer.tonals.effect.vibrato.delay,x
68 sta mixer.tonals.effect.vibrato.counter,x
69 sta mixer.tonals.effect.vibrato.pos,x
70 + sec
71 rts
73 is_command:
74 jsr go_command
75 sec
76 rts
78 is_append:
79 adc tracks.pattern.transpose,x
80 adc global_transpose
81 and #$7F ; note # in lower 7 bits
82 bpl set_note ; skip the volume & vibrato retrig
84 is_note:
85 clc
86 adc tracks.pattern.transpose,x
87 adc global_transpose
88 pha
89 lda #$80
90 sta mixer.tonals.square.period_save,x ; this is a union
91 lda mixer.envelopes.master,x
92 lsr ; CF=1 if the volume has been overridden by a previous volume command
93 bcs +
94 lda #$78
95 + asl
96 sta mixer.envelopes.master,x
97 lda #ENV_RESET
98 sta mixer.envelopes.phase,x ; volume envelope phase = init
99 lda #0
100 sta mixer.tonals.effect.vibrato.pos,x ; reset vibrato position
101 lda mixer.tonals.effect.vibrato.delay,x
102 sta mixer.tonals.effect.vibrato.counter,x ; reset vibrato delay
105 set_note:
106 ldy mixer.tonals.effect.kind,x
107 cpy #PORTAMENTO_EFFECT ; if slide parameter present...
108 beq init_slide ; ... slide from old to new note
109 ; no slide, set new period immediately
110 sta mixer.tonals.period_index,x
112 lda period_table_lo,y
113 sta mixer.tonals.period.lo,x
114 lda period_table_hi,y
115 sta mixer.tonals.period.hi,x
116 ; channel-specific init
117 ; ### consider making a plain effect
118 lda mixer.tonals.square.duty_ctrl,x
120 and #$C0
121 sta mixer.tonals.square.duty,x
123 and #$0F
124 sta mixer.tonals.square.counter,x
129 init_slide:
130 cmp mixer.tonals.period_index,x ; CF = slide direction (0=down,1=up)
131 sta mixer.tonals.period_index,x
133 lda period_table_lo,y
134 sta mixer.tonals.effect.portamento.target.lo,x
135 lda period_table_hi,y
136 sta mixer.tonals.effect.portamento.target.hi,x
138 lda #$40
139 rol ; bit 0 = slide direction
140 sta mixer.tonals.effect.portamento.ctrl,x
142 .endp
144 ; Processes a channel command.
146 .proc go_command
147 jsr table_call
148 TC_SLOT set_instr
149 TC_SLOT release
150 TC_SLOT set_mastervol
151 TC_SLOT set_speed
152 TC_SLOT end_row
153 .endp
155 ; Sets instrument.
157 .proc set_instr
158 jsr fetch_pattern_byte
159 sta mixer.tonals.instrument,x
162 asl ; each instrument is 8 bytes long
164 lda [instrument_table],y
165 sta mixer.envelopes.ptr.lo,x
167 lda [instrument_table],y
168 sta mixer.envelopes.ptr.hi,x
170 lda [instrument_table],y
171 sta mixer.tonals.effect.vibrato.delay,x
173 lda [instrument_table],y
174 sta mixer.tonals.effect.kind,x
176 lda [instrument_table],y
177 sta mixer.tonals.effect.slide.amount,x
179 lda [instrument_table],y
180 sta mixer.tonals.square.duty_ctrl,x ; this is a union
182 .endp
184 ; Disables volume envelope looping.
186 .proc release
187 lda #1
188 sta mixer.envelopes.hold,x
190 .endp
192 ; Sets the envelope master volume.
194 .proc set_mastervol
195 jsr fetch_pattern_byte
196 ora #1 ; indicates that volume was explicitly set
197 sta mixer.envelopes.master,x
199 .endp
201 .proc set_speed
202 jsr fetch_pattern_byte
203 ; ### this is buggy
204 .if 0
205 jsr set_track_speed
206 .else
207 jsr set_all_tracks_speed
208 .endif
210 .endp
212 ; this command is used when there is no note for the row, only commands
213 .proc end_row
214 lda mixer.envelopes.master,x
215 and #$FE
216 sta mixer.envelopes.master,x
221 .endp
223 .end