diff --git a/compiler/res/prog8lib/cx16/psg.p8 b/compiler/res/prog8lib/cx16/psg.p8 index 346908bb6..67742219c 100644 --- a/compiler/res/prog8lib/cx16/psg.p8 +++ b/compiler/res/prog8lib/cx16/psg.p8 @@ -15,8 +15,10 @@ psg { const ubyte RIGHT = %10000000 sub voice(ubyte voice_num, ubyte channel, ubyte volume, ubyte waveform, ubyte pulsewidth) { + envelope_states[voice_num] = 255 cx16.vpoke(1, $f9c2 + voice_num * 4, channel | volume) cx16.vpoke(1, $f9c3 + voice_num * 4, waveform | pulsewidth) + envelope_volumes[voice_num] = mkword(volume, 0) } ; sub freq_hz(ubyte voice_num, float hertz) { @@ -34,6 +36,7 @@ psg { sub volume(ubyte voice_num, ubyte vol) { uword reg = $f9c2 + voice_num * 4 cx16.vpoke(1, reg, cx16.vpeek(1, reg) & %11000000 | vol) + envelope_volumes[voice_num] = mkword(vol, 0) } sub pulse_width(ubyte voice_num, ubyte pw) { @@ -100,7 +103,7 @@ psg { } } - ; set new volumes using vera stride of 4 + ; set new volumes of all 16 voices, using vera stride of 4 cx16.push_vera_context() cx16.VERA_CTRL = 0 cx16.VERA_ADDR_L = $c2 diff --git a/docs/source/todo.rst b/docs/source/todo.rst index 6b8b36cd3..e174b356a 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -3,8 +3,7 @@ TODO For next release ^^^^^^^^^^^^^^^^ -- convert the sounds in cx16 tehtriz to use the psg module instead -- notify petaxian that it could use the psg module too? +... Need help with ^^^^^^^^^^^^^^ diff --git a/examples/cx16/tehtriz.p8 b/examples/cx16/tehtriz.p8 index 6f22e834e..c8377ce4b 100644 --- a/examples/cx16/tehtriz.p8 +++ b/examples/cx16/tehtriz.p8 @@ -5,13 +5,12 @@ ; wall kick rotations ; shows next piece ; staged speed increase -; simplistic sound effects (Vera PSG) - +; simple sound effects (Vera PSG) %import syslib %import textio %import test_stack - +%import psg main { @@ -212,11 +211,14 @@ waitkey: } } if num_lines { - if num_lines>3 + if num_lines>3 { sound.lineclear_big() - else + sys.wait(25) ; slight delay to flash the line + } + else { sound.lineclear() - sys.wait(10) ; slight delay to flash the line + sys.wait(15) ; slight delay to flash the line + } for linepos in complete_lines if linepos and blocklogic.isLineFull(linepos) blocklogic.collapse(linepos) @@ -592,71 +594,52 @@ blocklogic { sound { sub init() { cx16.vpoke(1, $f9c2, %00111111) ; volume max, no channels + psg.silent() + cx16.set_irq(&psg.envelopes_irq, true) } sub blockrotate() { ; soft click/"tschk" sound - uword freq = 15600 - cx16.vpoke(1, $f9c0, lsb(freq)) - cx16.vpoke(1, $f9c1, msb(freq)) - cx16.vpoke(1, $f9c2, %11110000) ; half volume - cx16.vpoke(1, $f9c3, %11000000) ; noise waveform - sys.wait(2) - cx16.vpoke(1, $f9c2, 0) ; shut off + psg.freq(0, 15600) + psg.voice(0, psg.LEFT | psg.RIGHT, 32, psg.NOISE, 0) + psg.envelope(0, 200, 1, 100) } sub blockdrop() { ; swish - uword freq = 4600 - cx16.vpoke(1, $f9c0, lsb(freq)) - cx16.vpoke(1, $f9c1, msb(freq)) - cx16.vpoke(1, $f9c2, %11110000) ; half volume - cx16.vpoke(1, $f9c3, %11000000) ; noise waveform - sys.wait(6) - cx16.vpoke(1, $f9c2, 0) ; shut off + psg.freq(1, 4600) + psg.voice(1, psg.LEFT | psg.RIGHT, 32, psg.NOISE, 0) + psg.envelope(1, 200, 5, 20) } sub swapping() { ; beep - uword freq = 1500 - cx16.vpoke(1, $f9c0, lsb(freq)) - cx16.vpoke(1, $f9c1, msb(freq)) - cx16.vpoke(1, $f9c2, %11110000) ; half volume - cx16.vpoke(1, $f9c3, %10000000) ; triangle waveform - sys.wait(6) - cx16.vpoke(1, $f9c2, 0) ; shut off + psg.freq(2, 1500) + psg.voice(2, psg.LEFT | psg.RIGHT, 32, psg.TRIANGLE, 0) + psg.envelope(2, 100, 6, 10) } sub lineclear() { ; explosion - uword freq = 1400 - cx16.vpoke(1, $f9c0, lsb(freq)) - cx16.vpoke(1, $f9c1, msb(freq)) - cx16.vpoke(1, $f9c2, %11111111) ; max volume - cx16.vpoke(1, $f9c3, %11000000) ; noise waveform - sys.wait(8) - cx16.vpoke(1, $f9c2, 0) ; shut off + psg.freq(3, 1400) + psg.voice(3, psg.LEFT | psg.RIGHT, 63, psg.NOISE, 0) + psg.envelope(3, 100, 8, 10) } sub lineclear_big() { ; big explosion - uword freq = 2500 - cx16.vpoke(1, $f9c0, lsb(freq)) - cx16.vpoke(1, $f9c1, msb(freq)) - cx16.vpoke(1, $f9c2, %11111111) ; max volume - cx16.vpoke(1, $f9c3, %11000000) ; noise waveform - sys.wait(30) - cx16.vpoke(1, $f9c2, 0) ; shut off + psg.freq(4, 2500) + psg.voice(4, psg.LEFT | psg.RIGHT, 63, psg.NOISE, 0) + psg.envelope(4, 100, 20, 10) } sub gameover() { - ; attempt at buzz/boing (but can't get the sawtooth/triangle combined waveform of the sid) - uword freq = 200 - cx16.vpoke(1, $f9c0, lsb(freq)) - cx16.vpoke(1, $f9c1, msb(freq)) - cx16.vpoke(1, $f9c2, %11111111) ; max volume - cx16.vpoke(1, $f9c3, %01000000) ; sawtooth waveform - sys.wait(30) - cx16.vpoke(1, $f9c2, 0) ; shut off + ; attempt at buzz/boing + psg.freq(5, 300) + psg.freq(6, 600) + psg.voice(5, psg.LEFT | psg.RIGHT, 0, psg.SAWTOOTH, 0) + psg.voice(6, psg.LEFT | psg.RIGHT, 0, psg.TRIANGLE, 0) + psg.envelope(5, 100, 30, 10) + psg.envelope(6, 100, 30, 10) } }