diff --git a/compiler/res/prog8lib/cx16/psg.p8 b/compiler/res/prog8lib/cx16/psg.p8 index c376ae7cd..1dab5c99d 100644 --- a/compiler/res/prog8lib/cx16/psg.p8 +++ b/compiler/res/prog8lib/cx16/psg.p8 @@ -53,10 +53,10 @@ psg { } sub silent() { - for cx16.r9L in 0 to 15 { - volume(cx16.r9L, 0) - envelope_volumes[cx16.r9L] = 0 - envelope_states[cx16.r9L] = 1 + for cx16.r15L in 0 to 15 { + volume(cx16.r15L, 0) + envelope_volumes[cx16.r15L] = 0 + envelope_states[cx16.r15L] = 255 } } @@ -71,25 +71,28 @@ psg { cx16.r14H = cx16.VERA_ADDR_H for cx16.r15L in 0 to 15 { - if envelope_states[cx16.r15L] { - ; release - cx16.r0 = envelope_volumes[cx16.r15L] - envelope_releases[cx16.r15L] - if msb(cx16.r0) & %11000000 { - cx16.r0 = 0 - envelope_releases[cx16.r15L] = 0 + when envelope_states[cx16.r15L] { + 0 -> { + ; attack + cx16.r0 = envelope_volumes[cx16.r15L] + envelope_attacks[cx16.r15L] + if msb(cx16.r0) & %11000000 or envelope_attacks[cx16.r15L]==0 { + cx16.r0 = mkword(63, 0) + envelope_attacks[cx16.r15L] = 0 + envelope_states[cx16.r15L] = 1 ; start release + } + envelope_volumes[cx16.r15L] = cx16.r0 + volume(cx16.r15L, msb(cx16.r0)) } - envelope_volumes[cx16.r15L] = cx16.r0 - volume(cx16.r15L, msb(cx16.r0)) - } else { - ; attack - cx16.r0 = envelope_volumes[cx16.r15L] + envelope_attacks[cx16.r15L] - if msb(cx16.r0) & %11000000 or envelope_attacks[cx16.r15L]==0 { - cx16.r0 = mkword(63, 0) - envelope_attacks[cx16.r15L] = 0 - envelope_states[cx16.r15L] = 1 ; start release + 1 -> { + ; release + cx16.r0 = envelope_volumes[cx16.r15L] - envelope_releases[cx16.r15L] + if msb(cx16.r0) & %11000000 { + cx16.r0 = 0 + envelope_releases[cx16.r15L] = 0 + } + envelope_volumes[cx16.r15L] = cx16.r0 + volume(cx16.r15L, msb(cx16.r0)) } - envelope_volumes[cx16.r15L] = cx16.r0 - volume(cx16.r15L, msb(cx16.r0)) } } cx16.VERA_CTRL = cx16.r13L diff --git a/docs/source/todo.rst b/docs/source/todo.rst index 461d67e42..0c5f06584 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -4,6 +4,7 @@ TODO For next release ^^^^^^^^^^^^^^^^ - optimize msb(cx16.r0) -> cx16.r0H, lsb(cx16.r0) -> cx16.r0L +- if passing a subroutine or label name as an uword argument, without &, add the addressof automatically - convert the sounds in cx16 tehtriz to use the psg module instead - notify petaxian that it could use the psg module too? diff --git a/examples/cx16/bdmusic.p8 b/examples/cx16/bdmusic.p8 index 3be13b74b..752dd84b1 100644 --- a/examples/cx16/bdmusic.p8 +++ b/examples/cx16/bdmusic.p8 @@ -4,12 +4,59 @@ main { - sub start() { + sub explosion() { + ; this subroutine is not used but it is an example of how to make a sound effect using the psg library! + psg.silent() + cx16.set_irq(&psg.envelopes_irq, true) + psg.voice(0, psg.LEFT, 0, psg.NOISE, 0) + psg.voice(1, psg.RIGHT, 0, psg.NOISE, 0) + psg.freq(0, 1000) + psg.freq(1, 2000) + psg.envelope(0, 50, 5) + psg.envelope(1, 80, 6) + sys.wait(100) + psg.silent() + cx16.restore_irq() + } + sub sweeping() { + ; this subroutine is not used but it is an example of how to make a sound effect using the psg library! + psg.silent() + psg.voice(0, psg.LEFT, 63, psg.PULSE, 0) + psg.voice(1, psg.LEFT, 63, psg.PULSE, 1) + psg.voice(2, psg.RIGHT, 63, psg.PULSE, 2) + psg.voice(3, psg.RIGHT, 63, psg.PULSE, 3) + psg.freq(0, 160) + psg.freq(1, 161) + psg.freq(2, 162) + psg.freq(3, 163) + repeat { + ubyte pw + for pw in 0 to 63 { + psg.pulse_width(0, pw) + psg.pulse_width(1, pw) + psg.pulse_width(2, pw) + psg.pulse_width(3, pw) + sys.wait(2) + } + for pw in 62 downto 1 { + psg.pulse_width(0, pw) + psg.pulse_width(1, pw) + psg.pulse_width(2, pw) + psg.pulse_width(3, pw) + sys.wait(2) + } + } + psg.silent() + } + + + sub start() { txt.print("will play the music from boulderdash,\nmade in 1984 by peter liepa.\npress enter to start: ") void c64.CHRIN() txt.clear_screen() + psg.silent() psg.voice(0, psg.LEFT, 63, psg.TRIANGLE, 0) psg.voice(1, psg.RIGHT, 63, psg.TRIANGLE, 0) cx16.set_irq(&psg.envelopes_irq, false)