Page 1 of 1

Multitasking

Posted: 17 Feb 2024, 13:11
by lb3361
In an attempt to improve the virtual interrupt system (the one that diverts the vCPU whenever frameCount wraps over), I wrote native instructions to save and restore the full vCPU context and arranged a way to have this done automatically when a virtual interrupt occurs. The rationale is explained in https://forum.gigatron.io/viewtopic.php?p=4169#p4169 and the final details are documented in https://github.com/lb3361/gigatron-rom/ ... interrupts. It took several rewrites to have a setup that is good enough to hit one of Marcel's long-term ideas : task switching.

There is now a proof of concept in https://github.com/lb3361/gigatron-lcc/ ... ff/threads.

The C program ’threads_test.c’ spawns two threads, one that computes prime numbers, another one that draws a maze in the bottom of the screen. Then it goes in a loop that prints the elapsed time until the prime numbers thread terminates. This program creates and manages threads using a small library with prototypes in ’threads.h’ and implementation in ’threads.s’. To make this work, I had to make changes in both the dev7 rom and in the compiler. It was also necessary to use a mutex to print things because the glcc library is not reentrant at this point.

Code: Select all

int main(void)
{
	gt_start(&th1, th1s, sizeof(th1s), run_primes, 0);
	gt_start(&th2, th2s, sizeof(th1s), run_maze, 0);
	run_elapsed(0);
	safe_cputs(1, 9, LIGHTMAGENTA, "Finished!");
	return 0;
}
To run the attached gt1 file, you need a very recent version of the dev7 rom, or you can also try it in www.gigatron128k.com using the "Load GT1" button.

threads_test.png
threads_test.png (53.35 KiB) Viewed 1401 times

Re: Multitasking

Posted: 18 Feb 2024, 10:09
by petersieg
That is VERY interesting stuff!
That could open complete new opportunities of gt1 programs.
Future will tell..

Many thanks, Peter

Re: Multitasking

Posted: 04 Mar 2024, 15:52
by lb3361
Updated code with correct stack alignment.
This matters because glcc-2.4 (about to be released) is more fussy about stack alignment.