6502 Assembly: Let's talk multitasking!

Let's talk multitasking!
kveroneau(Game Creator)
Aug. 19, 2016, 11:31 p.m.

I am creating this thread as there has been a lot of interest in beta players wanting to create a multitasking Kernel/Operating System within Hacker's Edge. Firstly, let me provide a link to a good resource on multitasking on the 6502: http://wilsonminesco.com/multitask/

There are really two categories in multitasking here, preemptive multitasking and cooperative multitasking. Preemptive can include interrupts and alarms, most include interrupts for IRQ request from devices.

Cooperative Multitasking

First let's talk about the most simplest to implement. This will be the multitasking which I plan on implementing in the official Kernel. It will still make use of interrupts and Hacker's Edge specific event callback system for promptly responding to devices, however the BRK op will tell the kernel it can work on another task in queue. The operations directly following a BRK will run after the Kernel resumes control to that program. So, to multitask enable a program is as simple as making a basic loop and use the BRK op within this loop until your program is done. When your program is ready to quit, it can set an exit status to A, and JMP to a Kernel API letting the Kernel know that your current program/process has completed.

  ....  Do stuff here ....
  BRA Mainloop
  ; program should JMP here from within Mainloop
  LDA #0 ; exit status code.
  JMP %exitproc

Here you notice we have a main loop, this main loop could say be a command interpreter for example. Within the loop, we constantly call BRK to give other programs a chance to run. Now, if data comes in from the network, or another event occurs, unless you called SEI, your code may be temporarily suspended at any point besides the BRK to service the device's request.

I am choosing this one for the Official Kernel as it's much easier to manage and works well with command-based tools. When a program is in the foreground, you know it's the only program with TTY access. If you've ever run a command-line program in the background on Linux without redirecting the output somewhere, you'll know what I mean on why this makes life easier for command-line programs.

Preemptive Multitasking

This one is vastly more complex than cooperative multitasking, and there is more than one way to do it.

You can also implement this type of multitasking within a process to enable what is called Threads. And this can be implemented inside a cooperative multitasking Kernel! I've created threads in QBasic for good old MS-DOS for example. Here's the jist of it...

  JSR @task1
  JSR @task2
  JSR @task3
  BRA ThreadLoop

This can also be done using a loop and having a JSR jump table to manage all the running tasks. The only downside of this, is that each task needs to keep track of their individual states.

The next form of Preemptive multitasking is the one most beta players in Hacker's Edge are talking about implementing in their own custom Kernels. This is when the Kernel regains control of the CPU after a set period of time. This is done by use of a Timer interrupt, so rather than say calling BRK manual when you want to task switch, a timer interrupt basically does a BRK for you.

Preemptive is most useful in GUI environments when you need to update multiple windows and widgets very often. If you Preemptively multitask two command-line programs at the same time, and both use input and output, your going to run into a lot of problems. Such as, what should be displayed? Which program should that keyboard input go to? These are some extra tasks which a Preemptive Kernel must manage, and the main reason why for the official Kernel, I am choosing Cooperative multitasking.

Complexities of multitasking

When multitasking more than one program, you will need to take the current state of the CPU, Stack, ZeroPage, and Instruction Pointer into consideration. If a task is returned back to with an invalid state and it was in the middle of some calculation, odds are that calculation will fail or at the very least output incorrect results.

Modern OSes which use multitasking have a special section of memory called the PCB or also called the Process Control Block. This is where the state the CPU was in while a given process was running is stored, such as the registers, and the instruction pointer.

Hopefully this starts a good discussion thread on multitasking within Hacker's Edge, happy hacking!

Let's talk multitasking!
bauen1(Game Moderator)
Aug. 20, 2016, 12:41 a.m.

During my hunt for resources on multitasking the 6502 i found a mini kernel: http://6502.org/source/kernels/minikernel.txt (It doesn't copy the zero page when switching tasks and only allows for 4 tasks but its a start)

Let's talk multitasking!
Jan. 20, 2018, 8:56 p.m.

also an prof of concept is the C64's disk os. the old (and most of the last DiskDrives) have an 6502 chip on it and (let my lye) 2k ram, the Disk OS (so i understand it) performs the operations over an "JOB CODE" so if lets say the code $80 comes to the drive it reads the directory. so you can have jobcodes with another prioritys... idk ;D also you can thake the indrive 6502 as an coop to the 6502 in your c64 machine.