File: SAMPLE.M65 - Tab length: 1 2 4 8 - Lines: on off - No wrap: on off

001: * sample.m65
002: * started : 97-09-24 (only 3mths till Xmas !!)
003: *
004: * Warning : Just experimental. This code _WILL_ change !
005: *           Also parameters etc.
006: 
007: * functions: void SmpInit(char channel,char timer);
008: *            void SmpStart(char * sample,char speed);
009: *            void SmpStop();
010: *            int  SmpActive();
011: 
012:                 global _SmpInit
013:                 global _SmpStart
014:                 global _SmpStop
015:                 global _SmpActive
016: 
017: * extern labels
018:         xref popax,pushax
019:         xref _InstallIRQ
020: 
021:  bss
022: 
023: SmpCount        ds 2
024: SmpSema         ds 1
025: SmpTemp         ds 1
026: SmpChannel  ds 1
027: SmpChannel8 ds 1
028: SmpTimer    ds 1
029:         bsszp
030: smp_ptr     ds 2
031: 
032:         text
033: mask        dc.b $11,$22,$44,$88
034: 
035: ; void SmpInit(char channel,char timer)
036: ; channel is the channel-number : 0..3
037: ; timer is the timer-number     : 0..7 (where 0,2,4 makes no sense !)
038: 
039: _SmpInit::
040:         jsr popax   ; get timer
041:         and #7
042:         asl A
043:         asl A
044:         pha
045:         sta SmpTimer
046:        
047:         jsr popax   ; get channel
048:         and #3
049:         sta SmpChannel
050:         plx
051:                 stz $fd01,x     ; stop Timer
052:        
053:         tay
054:         asl A
055:         asl A
056:         asl A
057:         sta SmpChannel8
058:         pha
059:        
060:                 lda mask,y
061:                 tsb $fd44       ; voice A balance controled
062:                 tsb $fd50       ; but curr. quite
063:        
064:                 lda #$ff
065:                 sta $fd40,y     ; and full volume left and right
066:                
067:         plx
068:                 stz $fd25,x     ; stop channel counter
069:                 stz $fd22,x     ; channel  quiet please
070:                 rts
071: 
072: ****************
073: * play mono sample
074: ****************
075: SmpIRQ::        lda (smp_ptr)
076:         ldx SmpChannel8
077:                 sta $fd22,x
078:                 inc smp_ptr
079:                 bne SmpIRQ2
080:                   inc smp_ptr+1
081: SmpIRQ2:        inc SmpCount
082:                 bne SmpExit
083:                 inc SmpCount+1
084:                 bne SmpExit
085: _SmpStop::
086:         ldy SmpChannel
087:         lda mask,y
088:                 tsb $fd50
089:                 ldx SmpTimer
090:                 stz $fd01,x
091: SmpExit:
092:                 rts
093: ****************
094: * play packed mono-samples
095: *****************
096: SmpIRQp::
097:                 dec SmpSema
098:                 bmi no_read
099: 
100:             lda (smp_ptr)
101:         inc smp_ptr
102:                 bne SmpIRQp0
103:               inc smp_ptr+1
104: 
105: SmpIRQp0        inc SmpCount
106:                 bne SmpIRQp1
107:                 inc SmpCount+1
108:                 beq _SmpStop
109: 
110: SmpIRQp1        sta SmpIRQp2+1  ; self mod
111:                                
112:                 lsr A
113:                 lsr A
114:                 lsr A
115:                 lsr A
116: *
117: * if there's enough space, could do it with a table
118: *
119:                 bra SmpIRQp3
120: 
121: no_read:        lda #1
122:                 sta SmpSema
123: 
124: SmpIRQp2:       lda #0
125:                 and #$f
126: 
127: SmpIRQp3:       tax
128:                 clc
129:                 lda tabelle,x
130:                 adc SmpTemp
131:                 sta SmpTemp
132:                 ldx SmpChannel8
133:                 bcc __1
134:                 dec $fda0
135: *
136: * Note : with stx $fd22+8 it sounds better but you'll use another channel
137: *
138: __1
139:         asl
140:         bcc __2
141:         lda #255
142: __2
143:                 sta $fd22,x
144:                 rts
145: 
146: tabelle:         DC.B 128,-64,-32,-16,-8,-4,-2,-1,0,1,2,4,8,16,32,64
147: 
148:        
149: ; SmpStart(char * sample,char speed)
150: ;
151: ; sample is a pointer to a structure :
152: ; stereo        ds 1
153: ; length        ds 2 ; big-endian (har,har)
154: ; divider       ds 1 ; 1MHz/sample-freq.
155: ; unpacked      ds 1 : == 0 sample packed
156: ;
157: ; divider may be overridden by speed (!= 0)
158: ;
159: _SmpStart::
160:         ldx SmpTimer
161:                 stz $fd01,x     ; stop current sample
162:                 ldx SmpChannel8
163:                 stz $fd22,x      ; silence
164: 
165:                 stz SmpTemp
166:                 lda #1
167:                 sta SmpSema
168:                 jsr popax
169:                 pha             ; save speed for later use
170:                 jsr popax       ; get data
171:                 sta ptr1
172:                 stx ptr1+1
173:                 clc
174:                 adc #5
175:                 sta smp_ptr
176:                 txa
177:                 adc #0
178:             sta smp_ptr+1
179:                
180: ; no stereo yet, so go to offset 1          
181:                
182:                 ldy #1  
183:                 lda (ptr1),y
184:                 eor #$ff
185:                 sta SmpCount+1
186:                 iny
187:                 lda (ptr1),y
188:                 eor #$ff
189:                 sta SmpCount
190:                 iny
191:                 lda (ptr1),y
192:                 ldx SmpTimer
193:                 sta $fd00,x
194:                 iny
195:                 lda (ptr1),y
196:                 pha             ; save packed-flag
197: 
198:         txa
199:         ldx #0
200:         lsr A
201:         lsr A       ; we want the number not offset
202:                 jsr pushax      ; pushax destroys y and maybe ptr1 !!
203: 
204:                 ldax #SmpIRQ
205:                 ply             ; check packed-flag
206:                 bne _SmpStart0
207:                   ldax #SmpIRQp
208: _SmpStart0:
209:                 jsr pushax
210: ;>                ldy #2
211:                 jsr _InstallIRQ
212:        
213:         ldx SmpTimer
214:                 pla
215:                 beq _SmpStart2
216:                   sta $fd00,x
217: _SmpStart2:     php
218:                 sei
219:                 lda #%10011000
220:                 sta $fd01,x
221:                 ldy SmpChannel
222:                 lda mask,y
223:                 trb $fd50
224:                 plp
225:                 rts
226: ****************
227: * int SmpActive()
228: ****************
229: _SmpActive::    ldx SmpTimer
230:         lda $fd01,x
231:         ldx #0
232:         cmp #0
233:         rts