This repository has been archived on 2021-07-08. You can view files and clone it, but cannot push or open issues or pull requests.
kalama/main.usm

230 lines
7.0 KiB
Plaintext

( a blank file )
( macros )
%RTN { JMP2r } ( return from a subroutine )
%LOB { SWP POP } ( get low byte of short as byte )
%HIB { POP } ( get high byte of short as byte )
( data structure access macros )
%NOTE { NOP } ( get the note of a pattern line addr )
%INST { #0001 ADD2 } ( get the instrument of a pattern line addr )
%VOL { #0002 ADD2 } ( get the volume of a pattern line addr )
( debugging macros )
%LOG { DUP .Console/byte DEO } ( prints a byte to the console non-destructively )
( constants )
%PTN_WIDTH { #0003 } ( pattern width. a short )
%PTN_LEN { #0010 } ( pattern length. a short )
( devices )
|00 @System [ &vector $2 &wst $1 &rst $1 &pad $4 &r $2 &g $2 &b $2 ]
|10 @Console [ &pad $8 &char $1 &byte $1 &short $2 &string $2 ]
|20 @Screen [ &vector $2 &width $2 &height $2 &pad $2 &x $2 &y $2 &addr $2 &color $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 ]
|70 @Midi [ &vector $2 &channel $1 &note $1 &velocity $1 ]
|80 @Controller [ &vector $2 &button $1 &key $1 ]
|90 @Mouse [ &vector $2 &x $2 &y $2 &state $1 &chord $1 ]
|a0 @File [ &vector $2 &success $2 &offset $2 &pad $2 &name $2 &length $2 &load $2 &save $2 ]
|b0 @DateTime [ &year $2 &month $1 &day $1 &hour $1 &minute $1 &second $1 &dotw $1 &doty $2 &isdst $1 ]
( variables )
|0000
@Playback [ &position $2 &pattern-addr $2 &tick $1 &speed $1 ]
@Draw [ &row $1 &col $1
&pattern-x $2 &pattern-y $2
&pattern-vspacing $2 &pattern-hspacing $2
&playhead-x $2 &playhead-y $2 ]
@Edit [ &selection-x $1 &selection-y $1 ]
( program )
|0100 ( -> )
( set a pallete )
#0f00 .System/r DEO2
#0f00 .System/g DEO2
#0fff .System/b DEO2
( speed - how many ticks we play a note every )
#10 .Playback/speed STZ
;pattern .Playback/pattern-addr STZ2
( display/audio init )
;on-frame .Screen/vector DEO2
;on-button .Controller/vector DEO2
#0180 .Audio0/adsr DEO2
;wave .Audio0/addr DEO2
#0002 .Audio0/length DEO2
( drawing properties )
#0010 .Draw/pattern-x STZ2
#0010 .Draw/pattern-y STZ2
#0004 .Draw/pattern-vspacing STZ2
#0007 .Draw/playhead-x STZ2
#0004 .Draw/pattern-hspacing STZ2
#08 .Edit/selection-y STZ
BRK
@on-button ( -> )
.Controller/button DEI ( get button )
DUP #10 AND #00 NEQ ,&up JCN ( check the if the four directions are pressed )
DUP #20 AND ,&down JCN
DUP #40 AND ,&left JCN
#80 AND ,&right JCN
BRK ( if none are pressed, break )
&up
#00 .Edit/selection-y LDZ EQU ,&end JCN
.Edit/selection-y LDZ #01 SUB .Edit/selection-y STZ
BRK
&down
PTN_LEN #01 SUB .Edit/selection-y LDZ EQU ,&end JCN
.Edit/selection-y LDZ #01 ADD .Edit/selection-y STZ
BRK
&left
#00 .Edit/selection-x LDZ EQU ,&end JCN
.Edit/selection-x LDZ #01 SUB .Edit/selection-x STZ
BRK
&right
PTN_WIDTH #01 SUB .Edit/selection-x LDZ EQU ,&end JCN
.Edit/selection-x LDZ #01 ADD .Edit/selection-x STZ
&end
BRK
@on-frame ( -> )
;draw JSR2
.Playback/tick LDZ .Playback/speed LDZ NEQ ,&inc JCN ( if the tick != speed, skip to inc )
;on-step JSR2 ( otherwise, run the on-step routine )
#00 .Playback/tick STZ ( reset the tick )
,&end JMP ( jump to end )
&inc
#01 .Playback/tick LDZ ADD .Playback/tick STZ ( tick up 1 )
&end
BRK
@draw-nibble ( nibble )
#08 MUL #00 SWP ;font_hex ADD2 .Screen/addr DEO2 ( locate nibble in the hex font )
.Edit/selection-y LDZ .Draw/row LDZ NEQ
.Edit/selection-x LDZ .Draw/col LDZ NEQ ADD ( if the row and column are the selected ones, use a different color )
,&else JCN
#22 .Screen/color DEO
,&then JMP
&else
#21 .Screen/color DEO ( display it )
&then
.Screen/x DEI2 #0008 ADD2 .Screen/x DEO2 ( move right a sprite )
RTN
( draw-byte draws a byte as hex on the screen )
@draw-byte ( byte )
DUP #04 SFT ( high digit ) ;draw-nibble JSR2
#0f AND ( low digit ) ;draw-nibble JSR2
RTN
( draw-pattern-line draws a single line in a pattern )
@draw-pattern-line ( addr )
#00 .Draw/col STZ
&loop
DUP2 LDA ;draw-byte JSR2
.Screen/x DEI2 #0004 ADD2 .Screen/x DEO2 ( add spacing between digits )
#0001 ADD2 ( get the next byte in the pattern )
#01 .Draw/col LDZ ADD .Draw/col STZ
.Draw/col LDZ PTN_WIDTH LOB NEQ ,&loop JCN
POP2 ( remove final address from stack )
RTN
( draw-pattern draws a pattern )
@draw-pattern ( addr )
#00 .Draw/row STZ
&loop
.Draw/pattern-y LDZ2 .Screen/x DEO2
DUP2 ;draw-pattern-line JSR2
.Screen/y DEI2 #0008 .Draw/pattern-vspacing LDZ2 ADD2 ADD2 .Screen/y DEO2 ( add spacing between rows )
PTN_WIDTH ADD2 ( get the next byte in the pattern )
#01 .Draw/row LDZ ADD .Draw/row STZ
.Draw/row LDZ PTN_LEN LOB NEQ ,&loop JCN
POP2 ( remove final address from stack )
RTN
@draw-playhead
( clear previous playhead )
.Draw/playhead-y LDZ2 .Screen/y DEO2
.Draw/playhead-x LDZ2 .Screen/x DEO2
#20 .Screen/color DEO
( draw new playhead )
.Draw/pattern-y LDZ2 .Playback/position LDZ2 #0001 SUB2 #0004 MUL2 ADD2 #0008 SUB2 DUP2 .Draw/playhead-y STZ2
.Screen/y DEO2
;playhead_icon .Screen/addr DEO2
#21 .Screen/color DEO
RTN
@draw
;draw-playhead JSR2
.Draw/pattern-x LDZ2 .Screen/x DEO2
.Draw/pattern-y LDZ2 .Screen/y DEO2
.Playback/pattern-addr LDZ2 ;draw-pattern JSR2
RTN
@on-step ( whenever a pattern step should be played )
PTN_WIDTH PTN_LEN MUL2 .Playback/position LDZ2 NEQ2 ,&continue JCN ( if we're at the end of the pattern, reset the counter )
#0000 .Playback/position STZ2
&continue
.Playback/position LDZ2 .Playback/pattern-addr LDZ2 ADD2 ( put pattern line address on the stack )
DUP2 VOL LDA .Audio0/volume DEO ( ln -- ) ( put the address of the pattern line on the stack )
NOTE LDA ( get the current note )
DUP #00 EQU ,&rest JCN ( if the note is 00, aka empty, skip playing it )
.Audio0/pitch DEO ( play the note )
,&end JMP ( jump to the end )
&rest POP ( if the note is a 00, jump here. POP to clean up the stack )
&end
PTN_WIDTH .Playback/position LDZ2 ADD2 .Playback/position STZ2
;draw-playhead JSR2 ( update the playhead position )
RTN
@wave [ ff 00 ]
@font_hex ( 0-F )
[
003c 464a 5262 3c00 0018 0808 0808 1c00
003c 4202 3c40 7e00 003c 421c 0242 3c00
000c 1424 447e 0400 007e 407c 0242 3c00
003c 407c 4242 3c00 007e 0204 0810 1000
003c 423c 4242 3c00 003c 4242 3e02 3c00
003c 4242 7e42 4200 007c 427c 4242 7c00
003c 4240 4042 3c00 007c 4242 4242 7c00
007e 4078 4040 7e00 007e 4078 4040 4000
]
@playhead_icon [ 00 60 78 7e 7e 78 60 00 ]
( pattern format - each note has 3 bytes. Pitch, instrument, volume. patterns are 0x10 long.
an ff in the note column of the pattern will kill the existing note without starting a new one (this is the tracker "box" note) )
@pattern [
3c 00 66
ff 00 66
3e 00 22
00 00 66
3c 00 88
ff 00 66
3e 00 66
00 00 66
3c 00 22
ff 00 66
3e 00 20
00 00 66
3c 00 66
ff 00 66
3e 00 ff
40 00 66
]