Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 14:55 28 Nov 2026 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : PicoMite Firware V8.00.00b4 onwards: MMbasic multi-tasking

Author Message
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 12301
Posted: 07:11pm 26 Nov 2026
Copy link to clipboard 
Print this post

PicoMite V8.00.00b4: the fourth beta of V8

Beta 4 brings threads to every PicoMite build: SUBs that run alongside the main program, each with its own loops, local variables and PAUSEs, with the same commands as the Colour Maximite 2. It also changes how listings behave inside a program and fixes the RTC, WEB TRANSMIT FILE, a TLS connection that the server closed, LIBRARY LOAD and empty RAM and flash slots. This is a beta for testing: V7.00.00 stays the current release. The firmware for all 18 builds, the manuals, the release notes and the apps are on GitHub: https://github.com/UKTailwind/PicoMite/releases/tag/PicoMite-V8.00.00b4

Every build resets at its first boot of b4: every build's flash region has moved up 16 KB to make room for threads. Save your options first with OPTION DISK SAVE "B:/myoptions.txt" and load them again afterwards with OPTION DISK LOAD "B:/myoptions.txt". The flash slots and program memory are cleared, and on a board with 2 or 4 MB of flash the A: drive too. A PicoComputer 3 keeps its A: drive, and so does the PicoMiteRP2040MIN, whose program memory is 4 KB smaller instead (108 KB). Programs saved by b3 do not need loading again.


Threads
A thread is an ordinary SUB started with THREAD START. It runs for as long as it likes while the main program carries on, and the firmware shares the processor between them a millisecond at a time:

Dim Integer beats

Sub Heart
 Do
   Inc beats
   Pause 500                     ' the rest of the program runs meanwhile
 Loop
End Sub

Thread Start 1, Heart
Do
 Print "Beats: "; beats
 Pause 1000
Loop Until Inkey$ <> ""
End                               ' the threads stop with the program

Threads suit work that goes on by itself: a clock in the corner of the screen, a tune played note by note, a sensor read every second, a game's aliens each moving on its own, a long calculation while the screen stays alive, a network request while the user types.

• Up to eight threads, numbered 1 to 8. THREAD EXIT, STOP, YIELD, PAUSE, RESUME, SLICE, PRIORITY and SIZE manage them, THREAD LIST shows where each one is and how much stack it has used, and MM.INFO(THREAD) reports on them.
• A thread that waits uses no processor time. PAUSE, INPUT, SYNC, TEMPR(, the frame waits on the VGA and HDMI builds, and the network's and Bluetooth's commands all let the other threads run, so a program can sit in INPUT while its threads keep the screen moving.
• Sharing safely. Sixteen locks (THREAD LOCK and UNLOCK, a mutex), THREAD HOLD and RELEASE for a few statements that must not be split, and THREAD WAIT for a thread to end or a FLAG( to be set. An interrupt runs in the thread that set it up.
• The same commands as the Colour Maximite 2, so a program's threads work the same on both, within the PicoMite's limits: eight threads, and an 8 KB stack each by default. A thread of the defaults takes about 10.5 KB of the heap, and a program that starts no thread runs as fast as before.
• An error in any thread stops the whole program and shows where it was, as [#3:40] (thread 3, line 40) before the statement.

The User Manual has the commands in its Commands listing and an overview in its new Appendix N. The new MMBasic Threads User Manual (MMBasic_Threads_User_Manual.pdf, in the supplementary manuals zip) covers threads in detail, with worked programs, for the Colour Maximite 2 and the PicoMite together; notes beginning PicoMite give the PicoMite's differences.


How many threads, realistically
Each thread has its own stack, 8 KB unless SIZE says otherwise, and a record, so a thread of the defaults takes about 10.5 KB of the heap (measured on a PicoComputer 3; a little less on the RP2040). Each also reserves local variable slots for its LOCAL variables and SUB parameters: 24 on the RP2350 and 16 on the RP2040. Memory and those slots are the two limits, besides the eight threads. All the threads share one processor core, so threads give a program structure, not speed. They suit work that spends most of its time waiting (in PAUSE, INPUT or the network); eight threads that all calculate flat out each get a ninth of the processor.

• PicoMiteVGA (RP2040): 118 KB of heap at the prompt, shared with the program's variables, arrays and strings, and with any FRAMEBUFFER or LAYER it creates. Eight threads of the defaults take about 80 KB, so they fit beside a small program. With a few arrays, four to six is realistic, and SIZE 6 LOCALS 8 NEST 4 makes a thread of under 8 KB for a simple loop. The default stack allows about two FUNCTION calls inside each other (SUB calls cost almost nothing), so keep deep or recursive work in the main program, or give that thread a larger SIZE. Local slots are no problem: eight default threads take 128 of the 240. Good uses are a clock on the screen, a sensor read every second, a tune played note by note, a few moving sprites, or a display that keeps moving while the program waits for keys.
• PicoMite (RP2350): over 300 KB of heap, so memory is no limit. All eight threads fit with plenty to spare, and a thread doing deep work can have a much larger stack: SIZE 32 allows about twenty FUNCTION calls inside each other. The limit here is the local slots. The region has 192, and each default thread reserves 24, so seven default threads leave the main program 24 for its own SUBs. For eight, start them with LOCALS 16, or put OPTION LOCAL VARIABLES 256 at the top of the program (512 global variables remain).
• PicoMitePC3: 104 KB of SRAM for the threads, since a thread's stack and record are always in SRAM (the PSRAM cannot be read while the flash is written). Eight threads of the defaults take about 84 KB of it. A program's large arrays (over 52 KB) go straight to the 8 MB PSRAM, but its smaller variables and strings fill the SRAM first, so start the threads early in the program. The local region has 256 slots here, so eight default threads leave the main program 64. With OPTION COMPILE ON a FUNCTION call takes half the stack, about six levels in the default 8 KB. A thread can own the network or Bluetooth, so a web request, an NTP update or an MQTT client can run in a thread while the screen and keyboard stay with the main program.


Try it
demo_input.bas runs four bouncing balls and a clock as five threads, and they keep going while the main program waits in INPUT for you to type a line. It needs an RP2350 with a VGA or HDMI build (it uses MODE 3). Type Q to quit.

https://github.com/UKTailwind/PicoMite/blob/PicoMite-V8.00.00b4/Bas/threads/demo_input.bas


What else has changed
• Listings in a program. A listing that stops with PRESS ANY KEY at every screen full now belongs to the command prompt: in a running program LIST, LIST file$, FLASH LIST n, RAM LIST n, LIBRARY LIST, LIST VARIABLES, LIST COMMANDS, LIST FUNCTIONS and LIST PINS give "Invalid in a program". Each has an ALL form that lists without stopping, and LIST VARIABLES, LIST COMMANDS, LIST FUNCTIONS and LIST PINS get theirs new in this beta. At the command prompt everything pages as before.
• The MMBasic heap is 4 KB smaller on the PicoMiteRP2350USB, PicoMiteHDMI, PicoMiteHDMIUSB, PicoMiteHDMIWEB and PicoMitePC3, to leave the processor's stack the room it needs. The largest program is unchanged.


What is fixed
• The RTC (#164): an RTC whose backup battery has failed restarts in 2000, and with OPTION RTC AUTO ENABLE that time was taken at every boot and every day, over a good time from WEB NTP. A lost time is no longer taken: the boot says "RTC time not set".
• WEB TRANSMIT FILE (#165) sent a large file at about half a second a chunk; it now goes as fast as the connection takes it.
• A TLS connection that the server closed before sending anything (#166) left freed memory in use, and the next close could crash the board. A request on it now gives "No connection".
• LIBRARY LOAD as a program's first statement emptied MM.CMDLINE$, and in a program started by CHAIN it wiped the variables the chain had kept (it is now an error there).
• RAM RUN, RAM CHAIN, FLASH RUN, FLASH CHAIN and FLASH LIST n of an empty slot left the empty slot as the program in memory. The program you had loaded now stays.
• LOAD PNG uses 5.4 KB less stack.

This beta has been tested on the PicoComputer 3, where all 40 thread tests pass, with and without OPTION COMPILE ON, and on the PicoMiteRP2040MIN, PicoMiteRP2040VGA, PicoMiteRP2040USB and PicoMiteRP2040VGAUSB builds. The other builds compile cleanly, so reports from other boards are welcome.
 
ville56
Guru

Joined: 08/06/2022
Location: Austria
Posts: 685
Posted: 03:16pm 27 Nov 2026
Copy link to clipboard 
Print this post

Great job Peter, incredible what you have implemented. Thank you.

Minor finding:

EXECUTE "files" in a thread seems to corrupt the system with

*** FAULT PC=00000000 LR=2002F000 SP=2002CB98 CFSR=00020000 HFSR=40000000
*** FAULT R0=20014F00 R1=00000000 R2=2002F000 R3=2002F000 R12=00000000 XPSR=68100000 FAR=FFFFFFFF EXC=FFFFFFED

Other commands like 'EXECUTE "list pins all"' are rejected as described.

Environment:
PicoMitePC3 MMBasic USB RP2350B Edition V8.00.00b4

Gerald
                                                                 
73 de OE1HGA, Gerald
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 12301
Posted: 04:54pm 27 Nov 2026
Copy link to clipboard 
Print this post

PicoMite V8.00.00b5: the fifth beta of V8
https://github.com/UKTailwind/PicoMite/releases/tag/PicoMite-V8.00.00b5
Coming from b4, every build keeps everything: its options, its programs, its flash slots and its A: drive. Coming from an earlier version, see the boxes at the top of the release notes: every build reset at its first boot of b4.


A CSUB that lets the threads run
A CSUB is a single statement, and threads take turns between statements, so until now every other thread waited until a CSUB returned. A CSUB that runs for a long time, such as a chess program's search, stopped the program's threads for as long as it ran.

A CSUB can now call CSubYield, at offset 0x1C0 in the call table (PicoCFunctions.h v2.4.0), to give the other threads their turns. It returns when this thread's turn comes round again, and does nothing in a program without threads. mmb2csub calls it for a THREAD YIELD in a routine that it converts, so the BASIC can say where:

nodes = nodes + 1
If (nodes And 127) = 0 Then
 If Timer > stop_time Then stop_search = 1
 Thread Yield                  ' the other threads have their turns here
EndIf

Each of the other threads' turns is one time slice, a millisecond by default. A thread with a burst of work to do while a CSUB runs, such as redrawing a clock, should put THREAD HOLD and THREAD RELEASE round it, to finish it in one turn.


Two threads demos
• Chess (Bas/threads/chess): TSCP, Tom Kerrigan's Simple Chess Program, as converted to MMBasic by Ceptimus and others, with the search in a CSUB. A chess clock below the moves list runs as a thread. The search yields every 128 nodes, so the clock never stops for more than about a quarter of a second while the computer thinks, and the search is only about 4% slower for it. The board is now redrawn after a capture of a like piece (pawn takes pawn), which used to leave the captured piece on show. It needs b5 and a VGA or HDMI build, a PicoMitePC3, or an LCD panel with FRAMEBUFFER CREATE. Note that the first run puts chesslib.bas in the library with LIBRARY LOAD "chesslib.bas", O, which replaces whatever library the board had without asking. https://github.com/UKTailwind/PicoMite/tree/PicoMite-V8.00.00b5/Bas/threads/chess
• Pico Frog (Bas/threads/picofrog): Martin Herhaus' game, with later work by Thomas Williams, Volhout and harm, with its sound as two threads in place of its SETTICK routines. One plays the music, with its own place in the DATA, so loading a level no longer stops it; the other sleeps until the game asks for a jump or a death sound. The original is beside it, for comparison. It needs b4 or later and the same displays as the chess. https://github.com/UKTailwind/PicoMite/tree/PicoMite-V8.00.00b5/Bas/threads/picofrog

Each folder's README describes what was changed and how to run it.


What is fixed
• Threads in interrupt routines (#167): THREAD EXIT, or THREAD STOP of its own thread, in an interrupt routine stopped every thread switch and interrupt for the rest of the run. Both are now "Invalid in an interrupt routine": a routine ends its thread by setting a flag that the thread's loop tests. And with a fast SETTICK in one thread beside busy ones, the main program's PAUSE could never end; the turns now go round in order, whichever thread an interrupt picks out of turn.
• FILES in a thread (EXECUTE "files", for example) crashed the board. It is now "Not allowed in a thread", as LIST is; the main program can still use FILES while threads run. RAM FILE LOAD ended every thread; it is now "Not allowed while a thread is running", as SAVE CONTEXT is.
• LGETSTR$ of 255 characters wrote past the end of its string (#162).
• WEB CONNECT with no arguments now rejoins the network whenever the board is not joined, not only after a failed join (#163).

The User Manual, the mmb2csub guide, the CallTable appendix of armcfgen.pdf and the MMBasic Threads User Manual describe CSubYield. The CallTable appendix also listed ExtCurrentConfig at 0x1C0, where no released firmware had it: it is at 0x48.

This beta has been tested on the PicoComputer 3, where all 43 thread tests pass and the chess and Pico Frog run. The other builds compile cleanly, so reports from other boards are welcome.
 
ville56
Guru

Joined: 08/06/2022
Location: Austria
Posts: 685
Posted: 05:18pm 27 Nov 2026
Copy link to clipboard 
Print this post

Peter,

have there been changes to the IP stack in b4 or b5? Cackground is that the connection to my Fritzbox now seems to work on every startup of the pico. Up and including b3 the connection "reliably failed" every 2nd time e.g. with "cpu restart". And there were no changes on my fritzbox firmware or config in between.

Gerald
                                                                 
73 de OE1HGA, Gerald
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 12301
Posted: 05:40pm 27 Nov 2026
Copy link to clipboard 
Print this post

No change, every build puts code in a slightly different place which affects caching and timing very slightly so no guarantees it won't change again
 
flip
Senior Member

Joined: 18/07/2016
Location: Australia
Posts: 142
Posted: 10:06pm 27 Nov 2026
Copy link to clipboard 
Print this post

Hi Peter,
Getting carried away with these amazing threads, I've noticed I'm often running out of memory.
is there a way to easily at run time identify the free memory available for a thread prior to attempting to create a thread?

In creating them I know I should know how much each thread should take up - and that's absolutely fine, it's just the available space for a thread (so program can avoid a crash in advance, or choose alternative procedure to 'skin the cat').

I have an example program (Multi-tasking frames) that I'm playing with as follows: (it's by no means an exemplar of programming skill with threads)..which has a limit of 5 (MAXTHREADS) if I set to 6 it crashes when 6 are started (sometimes 5?)

I will scour the manuals and answer myself if something found.

'PC3 Multi-tasking frames 8.00.00b4
Dim f%
const HRES=MM.HRES\MM.FONTWIDTH
Const VRES=MM.VRES\MM.FONTHEIGHT
Const MAXTHREADS=5
Dim Integer nthreads
Dim Integer RunStatus(8)
CONST sINITIAL=0
CONST sFIRSTRUN=1
CONST sSTOPPING=2
CONST sRERUN=3
CONST sSTOPPED=4

FRAME CREATE
FRAME BOX 0,0,HRES-1,VRES-1,,,RGB(CYAN)

Sub ChildFrame(id As Integer)
 'Static used%=0
 Local i%
 If RunStatus(id)=sRERUN THEN FRAME CLS id+1
 If RunStatus(id)=sFIRSTRUN Then Frame Overlay id+1,25,20,RGB(YELLOW)
 Frame Show id+1,(1+((id-1)) mod 4)*HRES\5,(1+(VRES-3)\2*((id-1)\4))
 Do
   Inc i%
   Frame Print id+1,Str$(id)+":"+Str$(i%)+"...",,WRAP
   FRAME WRITE
   Pause 500+id*100
 Loop Until RunStatus(id)=sSTOPPING
 FRAME HIDE id+1
 Frame Print 1, "("+Str$(nthreads-1)+")"+Time$+": Stopped "+Str$(id)+CHR$(10),rgb(red)
 FRAME WRITE
 RunStatus(id)=sSTOPPED
 INC nthreads,-1
End Sub

Sub Start(fr%)
 INC nthreads
 Thread Start fr% Size 8 Locals 8 Nest 4,ChildFrame,fr%
 Frame Print 1, "("+Str$(nthreads)+")"+Time$+": Started "+Str$(fr%)+chr$(10),rgb(green)
 FRAME WRITE
End Sub

Sub Stop(fr%)
End Sub

'************ MAIN LOOP **************
FRAME PRINT 1,"PRESS ANY KEY TO END"+Chr$(10),RGB(YELLOW)
'randomly start/stop tasks (limited to the number of MAXTHREADS)
Do
 t%=1+rnd*7.5
 If RunStatus(t%) MOD 2=1 Then 'running
   RunStatus(t%)=sSTOPPING 'Stop it
 Else
   If nthreads<MAXTHREADS Then
     If RunStatus(t%)=sINITIAL Then RunStatus(t%)=sFIRSTRUN Else RunStatus(t%)=sRERUN
     Start t%
   EndIf
 EndIf
 Pause 1000
Loop While Inkey$=""

'************ Close all frames/tasks
For f%=1 to 8
 If RunStatus(f%) MOD 2=1 Then
   RunStatus(f%)=sSTOPPING
   Pause 400
 EndIf
Next f%
'Wait for tasks to finish

do:loop while nthreads
Frame Print 1,"("+Str$(nthreads)+")"+Time$+": ALL TASKS ENDED"+CHR$(10),rgb(BLUE)
FRAME WRITE
pause 2000
END


This is just too much fun, every time I think I can start work on a serious project, a new feature blows me away.
Edit: Just fixed HRES & VRES

UPDATE: Tried checking the stack MM.INFO(STACK) - &H 2003f800 (as implied by manual) this gives -262k free (that's negative) so either am running in the super red-zone of stack or the manual may need an update (or I'm missing something)
Edited 2026-11-28 10:09 by flip
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2026