Piano example

This commit is contained in:
neauoire 2021-04-25 13:47:32 -07:00
parent de53a6c95f
commit 7034a0cbfb
3 changed files with 530 additions and 1 deletions

View File

@ -57,6 +57,8 @@ RTN
- Implement Uxambly REPL - Implement Uxambly REPL
- Load disks at a different place than 0x0000. - Load disks at a different place than 0x0000.
- Curl device? 8-bit web browser? - Curl device? 8-bit web browser?
- Replace LTS/GTS? `LTS = #80 ADD SWP #80 ADD SWP LTH`
- `#80 ADD SWP #80 ADD GTH`
## Palettes ## Palettes

View File

@ -32,7 +32,7 @@ else
fi fi
echo "Assembling.." echo "Assembling.."
./bin/assembler projects/software/nasu.usm bin/boot.rom ./bin/assembler projects/demos/piano.usm bin/boot.rom
echo "Running.." echo "Running.."
if [ "${2}" = '--cli' ]; if [ "${2}" = '--cli' ];

527
projects/demos/piano.usm Normal file
View File

@ -0,0 +1,527 @@
( piano )
%RTN { JMP2r }
%8+ { #0008 ADD2 } %8- { #0008 SUB2 }
%8* { #0008 MUL2 } %8/ { #0008 DIV2 }
%2/ { #0002 DIV2 }
%++ { #0001 ADD2 }
%MOD { DUP2 DIV MUL SUB }
%TOS { #00 SWP }
%PRINT-SHORT { .Console/short DEO2 #0d .Console/char DEO }
( devices )
|00 @System [ &vector $2 &pad $6 &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 @Audio [ &pad $8 &adsr $2 &length $2 &addr $2 &volume $1 &pitch $1 ]
|40 @Controller [ &vector $2 &button $1 &key $1 ]
|60 @Mouse [ &vector $2 &x $2 &y $2 &state $1 &chord $1 ]
|70 @File [ &vector $2 &success $2 &offset $2 &pad $2 &name $2 &length $2 &load $2 &save $2 ]
|a0 @DateTime [ &year $2 &month $1 &day $1 &hour $1 &minute $1 &second $1 &dotw $1 &doty $2 &isdst $1 ]
( variables )
|0000
@last $1
@octave $1
@addr $1
@pointer [ &x $2 &y $2 ]
@color $1
@rect [ &x1 $2 &y1 $2 &x2 $2 &y2 $2 ]
@adsr-view [ &x1 $2 &y1 $2 &x2 $2 &y2 $2 ]
@wave-view [ &x1 $2 &y1 $2 &x2 $2 &y2 $2 ]
@octave-view [ &x1 $2 &y1 $2 &x2 $2 &y2 $2 ]
@center [ &x $2 &y $2 ]
@knob [ &x $2 &y $2 &value $1 ]
( program )
|0100 ( -> )
( theme )
#0fe5 .System/r DEO2
#0fc5 .System/g DEO2
#0f25 .System/b DEO2
( vectors )
;on-control .Controller/vector DEO2
;on-mouse .Mouse/vector DEO2
( find center )
.Screen/width DEI2 2/ .center/x POK2
.Screen/height DEI2 2/ .center/y POK2
( place octave )
.center/x PEK2 #0050 SUB2 .octave-view/x1 POK2
.center/y PEK2 #0008 ADD2 .octave-view/y1 POK2
.octave-view/x1 PEK2 #0048 ADD2 .octave-view/x2 POK2
.octave-view/y1 PEK2 #0018 ADD2 .octave-view/y2 POK2
( place adsr )
.center/x PEK2 .adsr-view/x1 POK2
.center/y PEK2 #0008 ADD2 .adsr-view/y1 POK2
.adsr-view/x1 PEK2 #0058 ADD2 .adsr-view/x2 POK2
.adsr-view/y1 PEK2 #0018 ADD2 .adsr-view/y2 POK2
( place waveform )
.center/x PEK2 #0050 SUB2 .wave-view/x1 POK2
.center/y PEK2 #0020 SUB2 .wave-view/y1 POK2
.wave-view/x1 PEK2 #00a8 ADD2 .wave-view/x2 POK2
.wave-view/y1 PEK2 #0020 ADD2 .wave-view/y2 POK2
( default settings )
#ff .last POK
#048c .Audio/adsr DEO2
#88 .Audio/volume DEO
;sine-wave .Audio/addr DEO2
;sine-wave/end ;sine-wave SUB2 #0001 SFT2 .Audio/length DEO2
( background )
;redraw JSR2
BRK
@on-mouse ( -> )
;draw-cursor JSR2
.Mouse/state DEI #00 NEQ ,&no-touch JNZ BRK &no-touch
( wave-view )
.Mouse/x DEI2 DUP2 .wave-view/x1 PEK2 GTH2 ROT ROT .wave-view/x2 PEK2 LTH2 #0101 EQU2
.Mouse/y DEI2 DUP2 .wave-view/y1 PEK2 GTH2 ROT ROT .wave-view/y2 PEK2 LTH2 #0101 EQU2
#0101 EQU2 ;on-touch-wave-view JNZ2
( adsr-view )
.Mouse/x DEI2 DUP2 .adsr-view/x1 PEK2 GTH2 ROT ROT .adsr-view/x2 PEK2 LTH2 #0101 EQU2
.Mouse/y DEI2 DUP2 .adsr-view/y1 PEK2 GTH2 ROT ROT .adsr-view/y2 PEK2 LTH2 #0101 EQU2
#0101 EQU2 ;on-touch-adsr-view JNZ2
( octave-view )
.Mouse/x DEI2 DUP2 .octave-view/x1 PEK2 GTH2 ROT ROT .octave-view/x2 PEK2 LTH2 #0101 EQU2
.Mouse/y DEI2 DUP2 .octave-view/y1 PEK2 GTH2 ROT ROT .octave-view/y2 PEK2 LTH2 #0101 EQU2
#0101 EQU2 ;on-touch-octave-view JNZ2
BRK
@on-touch-wave-view ( -> )
.Mouse/x DEI2 .wave-view/x1 PEK2 SUB2 .Audio/length DEO2
;draw-wave JSR2
;draw-cursor JSR2
BRK
@on-touch-octave-view ( -> )
.Mouse/x DEI2 .octave-view/x1 PEK2 SUB2 #0008 DIV2 SWP POP #08 NEQ ,&no-mod JNZ
.Mouse/y DEI2 .octave-view/y1 PEK2 SUB2 #0008 DIV2 SWP POP
DUP #00 NEQ ,&no-incr JNZ
.octave PEK #01 ADD .octave POK
&no-incr
DUP #02 NEQ ,&no-decr JNZ
.octave PEK #01 SUB .octave POK
&no-decr
POP
( release ) #00 .Mouse/state DEO
;draw-octave JSR2
BRK
&no-mod
.Mouse/x DEI2 .octave-view/x1 PEK2 SUB2 #0008 DIV2 DUP2 SWP POP .last POK ;notes ADD2 GET ;play JSR2
( release ) #00 .Mouse/state DEO
;draw-octave JSR2
BRK
@on-touch-adsr-view ( -> )
.Mouse/x DEI2 .adsr-view/x1 PEK2 SUB2 8/ SWP POP #03 DIV
DUP #00 NEQ ,&no-a JNZ
.Audio/adsr DEI
#10 .Mouse/state DEI #10 EQU #e0 MUL ADD ADD
.Audio/adsr DEO &no-a
DUP #01 NEQ ,&no-d JNZ
.Audio/adsr DEI
DUP #f0 AND STH #01 .Mouse/state DEI #10 EQU #0e MUL ADD ADD #0f AND STHr ADD
.Audio/adsr DEO &no-d
DUP #02 NEQ ,&no-s JNZ
.Audio/adsr #01 ADD DEI
#10 .Mouse/state DEI #10 EQU #e0 MUL ADD ADD
.Audio/adsr #01 ADD DEO &no-s
DUP #03 NEQ ,&no-r JNZ
.Audio/adsr #01 ADD DEI
DUP #f0 AND STH #01 .Mouse/state DEI #10 EQU #0e MUL ADD ADD #0f AND STHr ADD
.Audio/adsr #01 ADD DEO &no-r
POP
( release ) #00 .Mouse/state DEO
;draw-adsr JSR2
;draw-cursor JSR2
BRK
@on-control ( -> )
( clear last cursor )
;clear .Screen/addr DEO2
.pointer/x PEK2 .Screen/x DEO2
.pointer/y PEK2 .Screen/y DEO2
#30 .Screen/color DEO
.Controller/key DEI
DUP #61 NEQ ,&no-c JNZ
#00 .last POK ;notes GET ;play JSR2 &no-c
DUP #73 NEQ ,&no-d JNZ
#01 .last POK ;notes #0001 ADD2 GET ;play JSR2 &no-d
DUP #64 NEQ ,&no-e JNZ
#02 .last POK ;notes #0002 ADD2 GET ;play JSR2 &no-e
DUP #66 NEQ ,&no-f JNZ
#03 .last POK ;notes #0003 ADD2 GET ;play JSR2 &no-f
DUP #67 NEQ ,&no-g JNZ
#04 .last POK ;notes #0004 ADD2 GET ;play JSR2 &no-g
DUP #68 NEQ ,&no-a JNZ
#05 .last POK ;notes #0005 ADD2 GET ;play JSR2 &no-a
DUP #6a NEQ ,&no-b JNZ
#06 .last POK ;notes #0006 ADD2 GET ;play JSR2 &no-b
DUP #6b NEQ ,&no-c2 JNZ
#07 .last POK ;notes #0007 ADD2 GET ;play JSR2 &no-c2
POP
.Controller/button DEI #f0 AND
DUP #04 SFT #01 AND #01 NEQ ,&no-up JNZ
( move ) .Audio/addr DEI2 #0010 SUB2 .Audio/addr DEO2 &no-up
DUP #05 SFT #01 AND #01 NEQ ,&no-down JNZ
( move ) .Audio/addr DEI2 #0010 ADD2 .Audio/addr DEO2 &no-down
DUP #06 SFT #01 AND #01 NEQ ,&no-left JNZ
( move ) .Audio/addr DEI2 #0001 SUB2 .Audio/addr DEO2 &no-left
DUP #07 SFT #01 AND #01 NEQ ,&no-right JNZ
( move ) .Audio/addr DEI2 #0001 ADD2 .Audio/addr DEO2 &no-right
POP
;redraw JSR2
BRK
@play ( pitch -- )
.octave PEK #0c MUL ADD .Audio/pitch DEO
RTN
@redraw ( -- )
;draw-octave JSR2
;draw-adsr JSR2
;draw-wave JSR2
RTN
@draw-cursor ( -- )
( clear last cursor )
;clear .Screen/addr DEO2
.pointer/x PEK2 .Screen/x DEO2
.pointer/y PEK2 .Screen/y DEO2
#30 .Screen/color DEO
( record pointer positions )
.Mouse/x DEI2 .pointer/x POK2
.Mouse/y DEI2 .pointer/y POK2
( draw new cursor )
;cursor .Screen/addr DEO2
.pointer/x PEK2 .Screen/x DEO2
.pointer/y PEK2 .Screen/y DEO2
( colorize on state )
#31 [ .Mouse/state DEI #00 NEQ ] ADD .Screen/color DEO
RTN
@draw-octave ( -- )
.octave-view/x1 PEK2 .octave-view/y1 PEK2
OVR2 OVR2 ;keys-left-icns #21 .last PEK #00 EQU ADD ;draw-key JSR2
OVR2 #0008 ADD2 OVR2 ;keys-middle-icns #21 .last PEK #01 EQU ADD ;draw-key JSR2
OVR2 #0010 ADD2 OVR2 ;keys-right-icns #21 .last PEK #02 EQU ADD ;draw-key JSR2
OVR2 #0018 ADD2 OVR2 ;keys-left-icns #21 .last PEK #03 EQU ADD ;draw-key JSR2
OVR2 #0020 ADD2 OVR2 ;keys-middle-icns #21 .last PEK #04 EQU ADD ;draw-key JSR2
OVR2 #0028 ADD2 OVR2 ;keys-middle-icns #21 .last PEK #05 EQU ADD ;draw-key JSR2
SWP2 #0030 ADD2 SWP2 ;keys-right-icns #21 .last PEK #06 EQU ADD ;draw-key JSR2
;arrow-icns .Screen/addr DEO2
.octave-view/x1 PEK2 #0040 ADD2 .Screen/x DEO2
.octave-view/y1 PEK2 .Screen/y DEO2
#21 .Screen/color DEO
;arrow-icns #0008 ADD2 .Screen/addr DEO2
.octave-view/y1 PEK2 #0010 ADD2 .Screen/y DEO2
#21 .Screen/color DEO
;font-hex .octave PEK #03 ADD #00 SWP #0008 MUL2 ADD2 .Screen/addr DEO2
.octave-view/x2 PEK2 #0008 SUB2 .Screen/x DEO2
.octave-view/y1 PEK2 #0008 ADD2 .Screen/y DEO2
#23 .Screen/color DEO
RTN
@draw-key ( x* y* addr* color -- )
STH
.Screen/addr DEO2
SWP2 .Screen/x DEO2
DUP2 #0018 ADD2
&loop
( move ) OVR2 .Screen/y DEO2
( draw ) DUPr STHr .Screen/color DEO
( incr ) .Screen/addr DEI2 8+ .Screen/addr DEO2
( incr ) SWP2 8+ SWP2
OVR2 OVR2 LTH2 ,&loop JNZ
POP2 POP2
POPr
RTN
@draw-adsr ( -- )
.adsr-view/x1 PEK2 .adsr-view/y1 PEK2
.Audio/adsr DEI #04 SFT
;draw-knob JSR2
.adsr-view/x1 PEK2 #0018 ADD2 .adsr-view/y1 PEK2
.Audio/adsr DEI #0f AND
;draw-knob JSR2
.adsr-view/x1 PEK2 #0030 ADD2 .adsr-view/y1 PEK2
.Audio/adsr #01 ADD DEI #04 SFT
;draw-knob JSR2
.adsr-view/x1 PEK2 #0048 ADD2 .adsr-view/y1 PEK2
.Audio/adsr #01 ADD DEI #0f AND
;draw-knob JSR2
RTN
@draw-wave ( -- )
.wave-view/x1 PEK2
.wave-view/y1 PEK2
.wave-view/x2 PEK2 #0001 ADD2
.wave-view/y2 PEK2
#00 ;fill-rect JSR2
#01 ;draw-wave-length JSR2
.wave-view/x1 PEK2 .Screen/x DEO2
( waveform )
#00 #a8
&loop
( dotted line )
OVR #01 AND ,&no-dot JNZ
.wave-view/y1 PEK2 #0010 ADD2 .Screen/y DEO2
#03 .Screen/color DEO
&no-dot
OVR TOS .Audio/addr DEI2 ADD2 GET
#02 DIV #40 ADD DUP #07 SFT #80 MUL SUB
TOS #0004 DIV2 .wave-view/y1 PEK2 ADD2 .Screen/y DEO2
.Screen/x DEI2 #0001 ADD2 .Screen/x DEO2
( draw ) OVR .Audio/length DEI2 SWP POP GTH #02 MUL #01 ADD .Screen/color DEO
( incr ) SWP #01 ADD SWP
DUP2 LTH ,&loop JNZ
POP2
( range )
.wave-view/x1 PEK2 .Screen/x DEO2
.wave-view/y1 PEK2 #0010 SUB2 .Screen/y DEO2
.Audio/addr DEI2 ;draw-short JSR2
.wave-view/x2 PEK2 #0020 SUB2 .Screen/x DEO2
.Audio/length DEI2 ;draw-short JSR2
RTN
@draw-wave-length ( color -- )
STH
.wave-view/x1 PEK2 .Audio/length DEI2 ADD2 .Screen/x DEO2
.wave-view/y1 PEK2 DUP2 #0020 ADD2
&loop
OVR2 .Screen/y DEO2
( draw ) DUPr STHr .Screen/color DEO
( incr ) SWP2 #0001 ADD2 SWP2
OVR2 OVR2 LTH2 ,&loop JNZ
POP2 POP2
POPr
RTN
@draw-knob ( x* y* value -- )
( load ) .knob/value POK .knob/y POK2 .knob/x POK2
.knob/x PEK2 .Screen/x DEO2
.knob/y PEK2 .Screen/y DEO2 ;knob_icns .Screen/addr DEO2 #21 .Screen/color DEO
.knob/x PEK2 8+ .Screen/x DEO2 ;knob_icns 8+ .Screen/addr DEO2 #21 .Screen/color DEO
.knob/y PEK2 8+ .Screen/y DEO2 ;knob_icns #0018 ADD2 .Screen/addr DEO2 #21 .Screen/color DEO
.knob/x PEK2 .Screen/x DEO2 ;knob_icns #0010 ADD2 .Screen/addr DEO2 #21 .Screen/color DEO
.knob/x PEK2 #00 #00 .knob/value PEK ;knob-offsetx ADD2 GET ADD2 .Screen/x DEO2
.knob/y PEK2 #00 #00 .knob/value PEK ;knob-offsety ADD2 GET ADD2 .Screen/y DEO2
;knob_icns #0020 ADD2 .Screen/addr DEO2
#25 .Screen/color DEO
.knob/x PEK2 #0004 ADD2 .Screen/x DEO2
.knob/y PEK2 #0010 ADD2 .Screen/y DEO2
;font-hex #00 .knob/value PEK #08 MUL ADD2 .Screen/addr DEO2
#21 .Screen/color DEO
RTN
@draw-short ( short -- )
.addr POK2
;font-hex #00 ;addr GET #f0 AND #04 SFT #08 MUL ADD2 .Screen/addr DEO2
( draw ) #22 .Screen/color DEO
.Screen/x DEI2 8+ .Screen/x DEO2
;font-hex #00 ;addr GET #0f AND #08 MUL ADD2 .Screen/addr DEO2
( draw ) #22 .Screen/color DEO
.Screen/x DEI2 8+ .Screen/x DEO2
;font-hex #00 ;addr ++ GET #f0 AND #04 SFT #08 MUL ADD2 .Screen/addr DEO2
( draw ) #22 .Screen/color DEO
.Screen/x DEI2 8+ .Screen/x DEO2
;font-hex #00 ;addr ++ GET #0f AND #08 MUL ADD2 .Screen/addr DEO2
( draw ) #22 .Screen/color DEO
RTN
@fill-rect ( x1 y1 x2 y2 color -- )
.color POK
( x1 x2 y1 y2 ) ROT2 SWP2
&ver
( save ) OVR2 .Screen/y DEO2
STH2 STH2 OVR2 OVR2
&hor
( save ) OVR2 .Screen/x DEO2
( draw ) .color PEK .Screen/color DEO
( incr ) SWP2 ++ SWP2
OVR2 OVR2 LTS2 ,&hor JNZ
POP2 POP2 STH2r STH2r
( incr ) SWP2 ++ SWP2
OVR2 OVR2 LTS2 ,&ver JNZ
POP2 POP2 POP2 POP2
RTN
@line-rect ( x1 y1 x2 y2 color -- )
( load ) .color POK DUP2 STH2 .rect/y2 POK2 .rect/x2 POK2 DUP2 STH2 .rect/y1 POK2 .rect/x1 POK2
STH2r STH2r
&ver
( save ) OVR2 .Screen/y DEO2
( draw ) .rect/x1 PEK2 .Screen/x DEO2 .color PEK DUP .Screen/color DEO
( draw ) .rect/x2 PEK2 .Screen/x DEO2 .Screen/color DEO
( incr ) SWP2 ++ SWP2
OVR2 OVR2 LTS2 ,&ver JNZ
POP2 POP2
.rect/x1 PEK2 .rect/x2 PEK2
&hor
( save ) OVR2 .Screen/x DEO2
( draw ) .rect/y1 PEK2 .Screen/y DEO2 .color PEK DUP .Screen/color DEO
( draw ) .rect/y2 PEK2 .Screen/y DEO2 .Screen/color DEO
( incr ) SWP2 ++ SWP2
OVR2 OVR2 ++ LTS2 ,&hor JNZ
POP2 POP2
RTN
@clear [
0000 0000 0000 0000 ]
@cursor [
80c0 e0f0 f8e0 1000 ]
@arrow-icns [
0010 387c fe10 1000
0010 1010 fe7c 3810 ]
@notes [
3c 3e 40 41 43 45 47
48 4a 4c 4d 4f 51 53 ]
@keys-left-icns [
7c7c 7c7c 7c7c 7c7c
7c7c 7c7c 7c7c 7e7f
7f7f 7f7f 7f7f 3e00 ]
@keys-middle-icns [
1c1c 1c1c 1c1c 1c1c
1c1c 1c1c 1c1c 3e7f
7f7f 7f7f 7f7f 3e00 ]
@keys-right-icns [
1f1f 1f1f 1f1f 1f1f
1f1f 1f1f 1f1f 3f7f
7f7f 7f7f 7f7f 3e00 ]
@knob_icns [
0003 0c10 2020 4040
00c0 3008 0404 0202
4040 2020 100c 0300
0202 0404 0830 c000
0000 183c 3c18 0000 ]
@knob-offsetx [
01 00 00 00 00 01 02 03
05 06 07 08 08 08 08 07 ]
@knob-offsety [
07 06 05 03 02 01 00 00
00 00 01 02 03 05 06 07 ]
@font-hex ( 0-F )
[
007c 8282 8282 827c 0030 1010 1010 1010
007c 8202 7c80 80fe 007c 8202 1c02 827c
000c 1424 4484 fe04 00fe 8080 7c02 827c
007c 8280 fc82 827c 007c 8202 1e02 0202
007c 8282 7c82 827c 007c 8282 7e02 827c
007c 8202 7e82 827e 00fc 8282 fc82 82fc
007c 8280 8080 827c 00fc 8282 8282 82fc
007c 8280 f080 827c 007c 8280 f080 8080 ]
@sine-wave [
0004 090e 1217 1c20 2529 2e32 363b 3f43
474b 4e52 5659 5c60 6365 686b 6d70 7274
7677 797a 7b7c 7d7e 7e7e 7e7e 7e7e 7d7c
7b7a 7978 7674 7270 6e6b 6966 6360 5d5a
5753 4f4c 4844 403c 3833 2f2a 2621 1d18
140f 0a05 01fc f7f2 eee9 e4e0 dbd7 d2ce
cac5 c1bd b9b5 b1ae aaa7 a3a0 9d9a 9795
9290 8e8c 8a88 8785 8483 8282 8181 8181
8181 8282 8384 8687 898a 8c8e 9193 9598
9b9e a1a4 a8ab afb2 b6ba bec2 c6cb cfd3
d8dc e1e6 eaef f4f8 ] &end
@tri-wave [
0002 0406 080a 0c0e 1012 1416 181a 1c1e
2022 2426 282a 2c2e 3032 3436 383a 3c3e
4041 4345 4749 4b4d 4f51 5355 5759 5b5d
5f61 6365 6769 6b6d 6f71 7375 7779 7b7d
7f7d 7b79 7775 7371 6f6d 6b69 6765 6361
5f5d 5b59 5755 5351 4f4d 4b49 4745 4341
403e 3c3a 3836 3432 302e 2c2a 2826 2422
201e 1c1a 1816 1412 100e 0c0a 0806 0402
00fe fcfa f8f6 f4f2 f0ee ecea e8e6 e4e2
e0de dcda d8d6 d4d2 d0ce ccca c8c6 c4c2
c1bf bdbb b9b7 b5b3 ]
@arc [
8000 8d00 9a02 a706 b40b c011 cb18 d520
df2a e734 ee40 f44b f958 fd65 ff72 ff80
ff8d fd9a f9a7 f4b4 eec0 e7cb dfd5 d5df
cbe7 c0ee b4f4 a7f9 9afd 8dff 80ff 72ff
65fd 58f9 4bf4 40ee 34e7 2adf 20d5 18cb
11c0 0bb4 06a7 029a 008d 0080 0072 0265
0658 0b4b 113f 1834 202a 2a20 3418 3f11
4b0b 5806 6502 7200 ]