|
Forum Index : Microcontroller and PC projects : Threaded frogs, an example of using threads to make life easier
| Author | Message | ||||
| matherp Guru Joined: 11/12/2012 Location: United KingdomPosts: 12301 |
Pico Frog with threads: the music and sound effects as two threads Pico Frog does several things at once: it scrolls ten lanes, moves the frog, plays a tune in two voices with a chorus, and fires off jump and death sounds on top. That makes it a good example for the threads in V8.00.00b4. The attached version runs its music and sound effects as two threads. The game, the graphics, the tunes and the effects are exactly as they were: only the way the sound is driven has changed. The game is Martin Herhaus' Pico Frog (2022), with Thomas Williams' PicoGAME VGA support, Volhout's update for MMBasic 6 and the RP2350, and his work since (b1 to b9). The licence comment at the top of the file is kept. Both programs are on GitHub, in the PicoMite repository's `Bas/threads/picofrog` folder, so you can compare them side by side: - The threads version: https://github.com/UKTailwind/PicoMite/blob/main/Bas/threads/picofrog/picofrog_threads.bas - The original (with only the display fix described below): https://github.com/UKTailwind/PicoMite/blob/main/Bas/threads/picofrog/picofrog6_b9_3.bas - A short note on the changes: https://github.com/UKTailwind/PicoMite/blob/main/Bas/threads/picofrog/README.md How the original drives its sound • The music is a SETTICK interrupt every 23 ms (33 ms on an RP2040). Each tick is one step of a note's volume envelope, and every tenth tick it READs the next pair of notes. A counter (mt%) and four global variables carry its state from one tick to the next. • The songs are chosen by RESTOREs scattered through the game. A program has one DATA pointer, so loadlevel has to stop the music while it READs the level data, then restart it. • The jump and death sounds are SETTICK 1 routines. Each keeps its pitch in JP% and switches its own tick off when it is done. They share the tick, so one cuts the other off. • The LCD's framebuffer merge rides on the music interrupt, and the game loop waits for its next frame in a busy loop. What the threads version does The music is a thread of its own. A note becomes a FOR loop over its envelope with a PAUSE for each step, which is how you would describe it in words: Sub MusicThread Local f1%,f2%,fl%,fr%,st%,nw%,on%,nxt! Do Thread Hold : nw%=want% : want%=0 : Thread Release 'a song asked for If nw% Then SelectSong nw% : on%=1 : nxt!=Timer If on%=0 Then Pause 20 : Continue Do 'silent until then Read f1%,f2% 'this thread's own place in the DATA If f1%=99999 Then Restore Main_song : Read f1%,f2% If f1%=88888 Then on%=0 : Play Stop : Continue Do fl%=f1%-f1%\100 : fr%=f1%+f1%\100 'pseudo chorus For st%=1 To n_adsr% 'one note: the steps of its envelope If f1%>1 Then Play sound 1,"L","q",fl%,adsr%(st%) : Play sound 3,"R","q",fr%,adsr%(st%) If f2%>1 Then Play sound 2,"B","w",f2%,adsr2%(st%) Inc nxt!,beat% : If nxt!>Timer Then Pause nxt!-Timer 'on the beat Next Loop End Sub There is no counter to carry between interrupts and no "is this the first tick of the note?" test, and all its variables are LOCAL. The PAUSE is timed against a running deadline, so the tempo does not drift however long the statements take. Each thread has its own place in the DATA. The music thread's READs and RESTOREs never move the game's, so the game chooses a song with one call, PlaySong S_YANKEE, and the thread starts it at its next note. loadlevel READs the level data while the music plays on. The sound effects are a thread that sleeps until it is needed. It waits in THREAD WAIT FLAG 1, using no processor time, and each effect is a plain FOR loop: Sub SfxThread Local f%,k% Do Thread Wait Flag 1 'asleep until PlaySfx asks for an effect Flag(1)=0 : k%=sfx% Select Case k% Case X_JUMP 'a rising chirp For f%=600 To 1500 Step 100 Play sound 4,"B","Q",f%,25 : Pause 20 If Flag(1) Then Exit For 'a new effect cuts this one short Next Case X_DIE 'a falling wail For f%=220 To 65 Step -5 Play sound 4,"B","Q",f%,25 : Pause 15 If Flag(1) Then Exit For Next End Select If Flag(1)=0 Then Play sound 4,"B","O",f%,0 Loop End Sub The game loop owns the display. It does its own LCD merge, and waits for its frame with a PAUSE rather than a busy loop, and the two threads run in that PAUSE. Why the code is simpler Look at the places where the game asks for a sound. The death went from Restore FKill JP%=220:SetTick 15,DieSnd,1 to PlaySong S_KILL : PlaySfx X_DIE and loadlevel went from stopping the music with SetTick 0,0,4, then Restore intro and SetTick beat%, music,4 again, to a single PlaySong S_INTRO. • No SETTICK is left in the program, so there is nothing to start and stop at the right moments. • The game never touches the music's DATA pointer: it only says which song it wants. • The effects can't step on each other, as they no longer share an interrupt. • The sound code reads top to bottom: a song is a loop of notes, a note is a loop of envelope steps, and an effect is a loop of pitches. • About 135 lines differ from the original, and the three interrupt routines are gone. Two small things are worth knowing. PlaySong sets one variable, and the music thread takes it inside THREAD HOLD and THREAD RELEASE, so that the two statements that take and clear the request can't be split by a thread switch. And game over still RUNs the program again, which ends both threads; they start afresh with it. A fix for the PicoComputer 3 The program used to choose between its VGA/HDMI path and its LCD path by looking for "VGA" or "HDMI" in MM.DEVICE$. The PC3's is "PicoMitePC3", so it took the LCD path and stopped with "Frame buffer 2 not created". Both the threads version and the original now use: const lcd%=not(instr(mm.info(lcdpanel),"MODE")) MM.INFO(LCDPANEL) returns "MODEn" on every VGA or HDMI display and the panel's name on an LCD, so this is the test to use in your own programs. Trying it It needs V8.00.00b4 or later, on a VGA or HDMI build, a PicoComputer 3, or an LCD panel. Each thread is started small (Thread Start 1 Locals 12 Nest 4, MusicThread), so it should be comfortable on an RP2040 too. I have run it on a PicoComputer 3, through to game over and the restart, and the music and effects sound as they always did. Reports from other boards are welcome. The User Manual's Appendix N gives an overview of threads, and the MMBasic Threads User Manual (in the supplementary manuals zip) covers them in detail. |
||||
| Volhout Guru Joined: 05/03/2018 Location: NetherlandsPosts: 6303 |
@Peter, Works nice on 2040 VGA @252MHz. Nice cleanup of the code. A little less messy. How much (RAM) cost for a thread ? (thinking about MaxiTrek). Thanks Volhout Edited 2026-11-27 23:20 by Volhout PicomiteVGA PETSCII ROBOTS |
||||
| matherp Guru Joined: 11/12/2012 Location: United KingdomPosts: 12301 |
|
||||
| PhenixRising Guru Joined: 07/11/2023 Location: United KingdomPosts: 2175 |
Process control Doesn't get better than this |
||||
| Martin H. Guru Joined: 04/06/2022 Location: GermanyPosts: 1595 |
A ‘Play Modpattern’ would be a useful addition here. It would be helpful because it would allow you to define the start pattern, meaning you could store all songs in a single module, as you could set a jump command at the end of a melody within the MOD pattern to take you back to its starting pattern. The Mod Engine should already have this built in; it just needs to be accessible externally as a Basic command. One suggestion would be Play Modpattern patternno Play from pattern patternno Play Modpattern -patternno Plays the selected pattern on a loop Edited 2026-11-28 04:05 by Martin H. 'no comment |
||||
| The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2026 |