This repository has been archived on 2022-04-18. You can view files and clone it, but cannot push or open issues or pull requests.
kalama-sin/player.tal

234 lines
5.6 KiB
Tal

( a blank file )
%DEBUG { ;print-hex/byte JSR2 #0a18 DEO }
%DEBUG2 { ;print-hex JSR2 #0a18 DEO }
%RTN { JMP2r }
%10/ { #04 SFT }
( devices )
|00 @System &vector $2 &wst $1 &rst $1 &eaddr $2 &ecode $1 &pad $1 &r $2 &g $2 &b $2 &debug $1 &halt $1
|10 @Console &vector $2 &read $1 &pad $5 &write $1 &error $1
|20 @Screen &vector $2 &width $2 &height $2 &auto $1 &pad $1 &x $2 &y $2 &addr $2 &pixel $1 &sprite $1
|30 @Audio0 &vector $2 &position $2 &output $1 &pad $3 &adsr $2 &length $2 &addr $2 &volume $1 &pitch $1
|40 @Audio1 &vector $2 &position $2 &output $1 &pad $3 &adsr $2 &length $2 &addr $2 &volume $1 &pitch $1
|50 @Audio2 &vector $2 &position $2 &output $1 &pad $3 &adsr $2 &length $2 &addr $2 &volume $1 &pitch $1
|60 @Audio3 &vector $2 &position $2 &output $1 &pad $3 &adsr $2 &length $2 &addr $2 &volume $1 &pitch $1
|80 @Controller &vector $2 &button $1 &key $1 &func $1
|90 @Mouse &vector $2 &x $2 &y $2 &state $1 &pad $3 &scrollx $2 &scrolly $2
|a0 @File &vector $2 &success $2 &stat $2 &delete $1 &append $1 &name $2 &length $2 &read $2 &write $2
|b0 @DateTime &year $2 &month $1 &day $1 &hour $1 &minute $1 &second $1 &dotw $1 &doty $2 &isdst $1
( variables )
|0000
@kalama
&module $2 ( pointer to start of module file )
&loopflags $1 ( if different channels are set to loop )
&instruments $2 ( address that instruments start at )
&tick $1
&line $1
&speed $1
( program )
|0100 ( -> )
( theme )
#0fe5 .System/r DEO2
#0fc5 .System/g DEO2
#0f25 .System/b DEO2
;module ;k-init-module JSR2
#00 #00 ;k-load-instrument JSR2
;on-frame/run .Screen/vector DEO2
BRK
@on-frame ( -> )
&run
;k-tick/run JSR2
BRK
@k-tick
&run
.kalama/tick LDZ #01 ADD DUP
.kalama/speed LDZ EQU ,&play JCN
.kalama/tick STZ
RTN
&play
#ff .kalama/tick STZ
.kalama/line LDZ #01 ADD DUP
DUP #00 SWP #0003 MUL2
#00 ;k-get-pattern/run JSR2 ADD2 ;k-play-line/run JSR2
#0f EQU ,&next JCN
.kalama/line STZ
RTN
&next
( TODO next pattern )
#ff .kalama/line STZ
RTN
( initialises kalama with module data )
@k-init-module ( addr* -- )
&run
DUP2 .kalama/module STZ2 ( store module address )
DUP2 LDA #0f AND .kalama/speed STZ ( set speed )
#0001 ADD2 LDA ( get pattern count byte )
;k-get-pattern/run JSR2 ( get the "pattern" that is 1 over the pattern index numbers. This is the first byte of the instruments )
.kalama/instruments STZ2
#ff .kalama/tick STZ
#ff .kalama/line STZ
RTN
( gets the memory address of the given pattern in loaded module )
@k-get-pattern ( number -- addr* )
&run
#00 SWP #0030 MUL2
.kalama/module LDZ2
#0003 ADD2 ( skip header )
DUP2 LDA2 ADD2 ( skip song table body )
#0002 ADD2 ( skip song table size short )
ADD2
RTN
( gets the memory address of the given instrument in loaded module )
@k-get-instrument ( number -- addr* )
&run
STH ( store instrument number for later )
.kalama/instruments LDZ2
( we are now at the size byte of instrument 00, the start of instrument memory )
&loop
STHrk #00 EQU ,&end JCN
STHr #01 SUB STH ( decrement counter )
DUP2 LDA2 ( load length byte and convert to short )
#0006 ADD2 ( jump over length short, vol, flags, ADSR )
ADD2 ( jump to next pattern )
,&loop JMP
&end
POPr
RTN
@k-play-line ( *l -- )
&run
LDA DUP
#00 EQU ,&rest JCN
.Audio0/pitch DEO ( TODO use correct channel, effects )
RTN
&rest
POP
RTN
( loads instrument with number into specified audio channel, 0-indexed )
@k-load-instrument ( number channel -- )
&run
#10 MUL STH ( stash channel * 0x10, offset for that channel's data relative to Audio0 )
;k-get-instrument JSR2 ( get instrument location )
DUP2 LDA2 STHrk .Audio0/length ADD DEO2 ( copy ADSR value )
#0002 ADD2 ( move to volume value )
DUP2 LDA STHrk .Audio0/volume ADD DEO ( copy volume value )
#0001 ADD2 DUP2 ( move to flags value )
LDA #07 SFT ( MSB is set for looping sample, turn it into 0/1 )
DUP ( set or clear loop bit )
#00 EQU ,&clear JCN
STHrk SFT .kalama/loopflags LDZ ORA .kalama/loopflags STZ ( set bit on )
,&end JMP
&clear
STHrk SFT #ff EOR .kalama/loopflags LDZ AND .kalama/loopflags STZ ( set bit off )
&end
#0001 ADD2 LDA2 STHrk .Audio0/adsr DEO2 ( copy ADSR value )
POPr
RTN
@print-hex ( value* -- )
SWP ,&byte JSR
&byte ( byte -- )
STHk #04 SFT ,&parse JSR #18 DEO
STHr #0f AND ,&parse JSR #18 DEO
RTN
&parse ( byte -- char ) DUP #09 GTH ,&above JCN #30 ADD RTN
&above #57 ADD RTN
RTN
@print-dec ( value* -- )
#2710 DIV2k DUP #30 ADD #18 DEO MUL2 SUB2
#03e8 DIV2k DUP #30 ADD #18 DEO MUL2 SUB2
#0064 DIV2k DUP #30 ADD #18 DEO MUL2 SUB2
#000a DIV2k DUP #30 ADD #18 DEO MUL2 SUB2
#30 ADD #18 DEO POP
RTN
@print-str ( string* -- )
#0001 SUB2
&while
INC2 LDAk DUP #18 DEO ,&while JCN
POP2
RTN
@module
88 02 02 ( file header - speed 8, loop on, 2 patterns, 2 instruments )
0008 ( size )
00 00 ff ff
00 01 ff ff ( song table, 2 rows )
( pattern 00 )
3c 02 00 ( C-3 I00 )
00 00 00
ff 00 00
00 00 00
3c 00 00
00 00 00
00 00 00
00 00 00
3c 00 00
00 00 00
00 00 00
00 00 00
3c 00 00
00 00 00
00 00 00
00 00 00
( pattern 01 )
ff 00 00
3c 02 00 ( C-3 I00 )
ff 00 00
3c 00 00
ff 00 00
3c 00 00
ff 00 00
3c 00 00
ff 00 00
3c 00 00
ff 00 00
3c 00 00
ff 00 00
3c 00 00
ff 00 00
3c 00 00
( instrument 00 )
0002 ( sample length )
ff ( volume )
80 ( flags - loop sample )
00f0 ( ADSR )
ff 00 ( square wave sample data )
( instrument 01, unused )
0008 ( sample length )
60 ( volume )
00 ( flags - no loop )
10f0 ( ADSR )
ff de 21 32 fa 43 aa 04 ( 8 random bytes )