tinyACORN in action

Memory management in tinyACORN.
ACORN kernel from the very beginning was planned to work within a small  RAM Memory environment but 128 bytes of RAM is kind of too small even for a kernel written entirely in assembler.
What is needed is to optimize the memory usage within the kernel so that more space is available for
each task. What follows is a brief review  of all possible targets of optimization.

  • Stack pointer is single byte which implies that task’s stack pointer in TCB structure could be reduced to 1 byte for SPL only.
  • TCB is reduced to 3 bytes with small modification to keep task’s id number.
  • Remove any barrier to further reduce memory usage.
  • Reduce each task context saved on the stack - instead of 32 general purpose registers use only 18.
  • Use the remaining general registers that are not part of task’s context anymore as global memory.
  • Optimize code in kernel macros so that less registers are used thus less task’s stack usage
  • Get rid of the idle task.(IMPORTANT- make sure at least one task is free of suspend task related macros, otherwise the scheduler may never leave the interrupt and block the system forever)

RAM space fragmentation. 
Let us see how we can pack 3 tasks in a tiny device with 128 bytes of RAM having in mind the constraints from above.




It is evident that there is no place for global RAM variables and the MUTEX state byte! The whole RAM space is tightly packed. In order to enable to MUTEX synchronization ,register r1 is used.
Register r2 through r14 could be used as global RAM area.
TCB structure.
Task Control Block is system RAM structure that identifies and describes the execution of a single task.It consists of 3 bytes.Tasks are static -TCB structure per task is created during the CPU reset and never deleted off the RAM.



{1}LSB byte address of the stack position where the task left off its execution due to a time quantum expiration  or task voluntarily relinquishing it.
{2}  task control bits
Task ID - task’s id number
S bit - task’s schedule ability bit.{0 - task is schedule able(default);1 - task is not schedule able}
P bit - task’s priority bit.{0 - NORMAL priority;1 - DEVICE priority}
{3} timeout units for the sleep macro.

Synchronization structure.
As mentioned above MUTEX status byte is moved to general register r1.It is not possible to use the extended event system and barrier for lack of free RAM.

When using stack related instructions PUSH,RCALL.....be careful not to exceed tasks stack quota.
When using interrupt handlers keep in mind that they happen in an arbitrary task context and add more stack entries(at least 2 in _PRE_INTERRUPT macro) so calculate the worst scenario for each task and make sure it does not exceed the stack quota. The idle task in megaAcorn kernel prevents the scheduler from looping forever if all tasks are suspended at the same time, therefore make sure at least one task does not use the suspend task related macros. Again: there should always be at least one task running in PASSIVE level!

Total kernel code is 448 bytes
Total RAM usage 128 bytes

I can not make it smaller!
A few demo applications are being prepared for the new tinyACORN kernel.