Startup
When you power up the HC11, the first thing that's going to happen is the stackSave array is cleared, meaning that there are no processes running. Additionally, all other internal variables are set to 0. When this is complete, the OS starts the command shell as a new process and sets the real-time interrupt to run. That's it! When the timer interrupt is run next, it automatically runs the next process, which at this point is the command shell since this is the only process that's been started.
When users eventually send the run command (r is the default), the shell starts the user's program as a new process. This means that both the user program and the command shell will run at the same time (and hopefully peacefully). Should the user send the shutdown command (s), this entire process repeats and you're left with the command shell again.
Starting a Process
In kernel.asm is startProcess, a subroutine that's responsible for setting up all internal variables and the process's stack so that a new process can begin running the next time a process switch occurs. Looking at that function (see Listing Three, available at www.ddj.com/code/), you see that it requires two arguments. The first argument (stored in register D) is the actual memory address of the process to start ("Where in memory do we jump to in order to start running this process?"). Second, register Y holds a True/False value specifying whether you need an exact copy of the current process's stack for the new process (used by the fork() command). By creating an exact copy, the new process shares all the current data that the current process is using such as variable data, return addresses, and so on.
When this process is called, it first attempts to find an empty slot in the stackSave array. Once an array element is found, it copies the current process's stack (if requested to do so by the argument in register Y). Once that's complete, it moves on to setting up the new process's stack space by pushing onto it the new process's location in memory (register D), along with some other data that's required by the RTI command of an HC11.
The startProcess procedure finishes by adding 1 to the total number of running processes variable and by returning the process ID in register A. This process ID is simply a unique 8-bit number that's given to each and every process that's started. This ID can be used in the future should you ever want to stop a process.
Stopping a Process
After you start a new process, there may come a time when you want to stop it. This is done with the stopProcess subroutine in kernel.asm. This function requires only one argumentthe process ID stored in register A. Once run, it searches through the list of currently running processes (the stackSave array, Listing Two). When a matching process ID is found, that entry is deleted and the process ceases execution.