Shop Learn P1 Docs P2 Docs Events
A Study: "APropOs" a Multitasking Operating System for P2 with Local C-Compiler — Parallax Forums

A Study: "APropOs" a Multitasking Operating System for P2 with Local C-Compiler

Christof Eb.Christof Eb. Posts: 825
edited 2022-12-15 15:19 in Customer Projects

Perhaps this is of some interest for others. So I post the result of this study here. "Study" means, that I did invest some days of work to achieve some results. This self hosted system is based on 3 ingredients:
1. The C- Compiler with it's bytecode interpreter taken from Robert Swierczek's "SWIEROS" https://github.com/rswier/swieros/blob/master/root/bin/c.c
2. Eric's shell, which is provided in the examples of FlexProp.
3. Some ingredients from my side including enhancements of the shell: A menue system for the shell and parameter substitution for one parameter in shell scripts.

About the bytecode interpreter:
This is a virtual processor, which is written in C and emulated by P2. It has one stack for parameters, local variables and return addresses. All addressing is done relative either to the stack pointer or the program counter. This leads to fully relocatable programs. The bytecode interpreter can handle "trap" instructions for extensions. This makes it possible to extend the language without the need to recompile the compiler. Only the interpreter must be recompiled. This method can be used to link to hardware related subroutines. Bytecode is held in files with the name convention file.b . The shell can load the bytecode into ram and execute it in the shells cog or as background job in one of the other cogs. Memory management was one of the challenges. The allocated memory is freed after a file.b finishes.

About the compiler:
The original SWIEROS is intended to run under Linux. So there is nearly unlimited Ram available. The compilers needed/available amount of Ram was reduced for P2. This version is named ct.c compiled in SWIEROS to ct.b . So if you want to modify it, you have to do this with SWIEROS. The compiler has a great number of features including floating point and include statements. In SWIEROS it can compile itself, so it is a good idea to have a look at this source to get an idea what is possible with ct.c .

About the shell/"ApropOs":
The terminal program of FlexProp is used as console.
"APropos" provides access to a file system on a SD-card and via the terminal program of FlexProp on the PC to the filesystem at the host, which is convenient for many things including editing. Be sure to copy the structure of /host/P2Root to /sd/root . ct.b should be present in /sd/root/cmds . The filesystem seems not to to be able to erase files, which have been written under windows, so sometimes you might need to delete them inserting the SD card into a PC. So do not copy files to SD, which you want to alter. The source code is prepared for the Kiss board running at 200MHz.

Shell scripts
reside at the active directory which is /host after start and are started by typing their file name for example: cdir.sh . They can substitute one parameter. See cc.sh . Try cdir.sh, which displays the directory of /sd/root/cmds
There is auto.sh which will be executed at startup.

Execution:
If a file.b is typed this is searched in /sd/root/cmds , loaded and executed. If you want to execute as a background task, type bg /path/file.b
Try: bg /sd/root/cmds/blinkbg.b
If you want to execute from a different directory than sd/root/cmds use go .
You can pass parameters to a program.

To get an overview of running processes use: ps
It will also give information about the last foreground process.
Use mfree to learn about the largest chunk of free memory.

Menue:
As there is no mouse and no command line editing for convenience a menue feature is provided.
men will read a file menue.txt and display it. This file contains lines, which begin with a single character and a space. If you type this single leading character, the rest of the line is executed.

Compiling:
As the PC or a editor there cannot access the SD card, a source is edited in a shadow file structure on the PC side in /host/p2root/source. A shell script cc.sh, which takes "file" without extension as parameter, copies the file.c to /sd/root/source and then starts the compiler. Libraries are in /sd/root/lib. The compiler outputs to /sd/root/cmds .
Try: cc.sh mandela
To get some information about the few implemented special functions of P2 study libct.h

Scratchpad Area:
To provide some supersimple inter-process communication there is a global scratchpad Hub Ram area, which can be written and read by any process.
To display it, type: dumpSc.b
If blinkbg.b is running in background you can see something.

Of course this study of an operating system is limited. A local editor program is given by SWIEROS as source code but does not run. It would be nice to have standard library functions for ADC, DAC and PWM. In principle it should be doable to make a debugger, because the bytecode interpreter can be modified.
Christof

Comments

Sign In or Register to comment.