6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Fri Nov 22, 2024 3:33 am

All times are UTC




Post new topic Reply to topic  [ 52 posts ]  Go to page Previous  1, 2, 3, 4
Author Message
 Post subject: Re: Multi tasking?
PostPosted: Fri Apr 18, 2014 6:32 pm 
Online
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8543
Location: Southern California
GARTHWILSON wrote:
Maybe it's about time for me to dig out that article again on cooperative multitasking in Forth that showed how efficient it can be. They definitely were not moving stacks around. IIRC, they were just storing the stack-pointer values for the task that was taking a break and replacing them with the stack-pointer values the next task had when it left off the last time. They had their own segments of the stack area, so they did not step on each other's space. Since it was cooperative, a task would only pause when it was at a good stopping point, where it was the least work to put its stuff down to let other tasks run for awhile and then resume the next time its turn came.

I'm not sure this is the article I was thinking of, but I came across one of Brad Rodriguez's, "Forth Multitasking In a Nutshell," on paper in The Computer Journal, issue #58, Nov/Dec '92, and asked him if it was online since I couldn't find it there. He said he would try to find it on an old disc and convert it to html and post it, which he did in record time, at http://bradrodriguez.com/papers/mtasking.html.

_________________
http://WilsonMinesCo.com/ lots of 6502 resources
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?


Top
 Profile  
Reply with quote  
 Post subject: Re: Multi tasking?
PostPosted: Fri Apr 18, 2014 7:37 pm 
Offline
User avatar

Joined: Thu May 28, 2009 9:46 pm
Posts: 8505
Location: Midwestern USA
GARTHWILSON wrote:
I'm not sure this is the article I was thinking of, but I came across one of Brad Rodriguez's, "Forth Multitasking In a Nutshell," on paper in The Computer Journal, issue #58, Nov/Dec '92, and asked him if it was online since I couldn't find it there. He said he would try to find it on an old disc and convert it to html and post it, which he did in record time, at http://bradrodriguez.com/papers/mtasking.html.

Interesting article and I'm pleased to note that he clearly differentiates between "multiuser" and "multitasking." The former is implicitly the latter, but not vice versa.

The Forth multitasking method, while efficient in the use of system resources, has a theoretical risk of deadlock if one of the tasks misbehaves. Avoidance of deadlock is one of the key benefits of using a jiffy IRQ to suspend the current task and restart another. However, an IRQ-driven task scheduler adds to the processing load and doesn't guarantee that any one task will meet required processing deadlines, often a requirement in Forth applications.

An IRQ-driven task scheduler isn't all that difficult to implement with the 65C816, thanks to the relocatable direct page and stack. Reentrancy is also relatively trivial, thanks to stack pointer relative addressing. Something similar could be accomplished with the 65C02 if the glue logic supports memory banking in a way that allows each task to have its own zero page and stack.

I've tinkered with a crude task scheduler on POC, which I had briefly incorporated into the firmware as an alarm function. I later removed it after deciding that I could better use the ROM space for other things. The principles are fairly basic:

  1. Push all registers to the stack, including DB and DP.
  2. Write SP to a "shadow" location that only the current task can access.
  3. Load SP from the new task's "shadow" location.
  4. Pull all registers from the stack in the reverse order of how they were pushed when the task was suspended.
  5. Execute RTI, which will restart the task.

Starting a new task involves setting the initial stack pointer, loading the stack with dummy values for the .C, .X and .Y registers, loading the stack with the proper values for DB, DP, PB and PC, and then continuing at step 4 above.

Obviously, there's more to it, but the above covers the elemental parts.

_________________
x86?  We ain't got no x86.  We don't NEED no stinking x86!


Top
 Profile  
Reply with quote  
 Post subject: Re: Multi tasking?
PostPosted: Fri Apr 18, 2014 8:18 pm 
Online
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8543
Location: Southern California
I have no experience on the insides of multitasking OSs of any kind, but although what Brad is telling about in the article may have more luxuries than needed (that's my perception anyway), it still does not require pushing any more than four registers in a task switch. That's pretty economical in terms of overhead! I've been tending toward advocating for the benefits of cooperative rather than preemptive, as our 65-family software will be our own and not commercial, and we have control of everything to fix our bugs and prevent the deadlocks from misbehaving tasks.

_________________
http://WilsonMinesCo.com/ lots of 6502 resources
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?


Top
 Profile  
Reply with quote  
 Post subject: Re: Multi tasking?
PostPosted: Sat May 17, 2014 1:27 am 
Online
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8543
Location: Southern California
I have a new 6502-oriented article up on my website on simple methods for doing multitasking without a multitasking OS, for small systems which may not have the resources for a multitasking OS, or where hard realtime requirements may rule one out. The article is at http://wilsonminesco.com/multitask/index.html.

_________________
http://WilsonMinesCo.com/ lots of 6502 resources
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?


Top
 Profile  
Reply with quote  
 Post subject: Re: Multi tasking?
PostPosted: Sun May 18, 2014 2:29 am 
Offline
User avatar

Joined: Thu May 28, 2009 9:46 pm
Posts: 8505
Location: Midwestern USA
GARTHWILSON wrote:
I have a new 6502-oriented article up on my website on simple methods for doing multitasking without a multitasking OS, for small systems which may not have the resources for a multitasking OS, or where hard realtime requirements may rule one out. The article is at http://wilsonminesco.com/multitask/index.html.

Multitasking with 65xx hardware is not something on we've seen much discussion, so your page (which I have read) comes at a good time.

As Garth notes in his article, if your system is intended to be a single user system, as most 65xx hobby systems are, you will do fine with simple cooperative multitasking. Such an environment is less demanding to develop and is much better suited to real time applications. The primary caveat is that all applications must behave, since the failure of one may cause system deadlock.

While a cooperative multitasking system that performs with alacrity may be implemented on a 65C02 system without an exorbitant amount of effort or fancy hardware, preemptive multitasking on that same system could potentially be a nightmare of trying to protect page zero and the stack, as well as keeping one application from stepping on another's space. A preemptive multitasking kernel is no trivial thing to develop for the 65C816. However, that MPU has features that can be harnessed with programmable logic to provide the necessary hardware support. Doing so is a long-term goal of mine.

_________________
x86?  We ain't got no x86.  We don't NEED no stinking x86!


Top
 Profile  
Reply with quote  
 Post subject: Re: Multi tasking?
PostPosted: Tue May 20, 2014 4:30 am 
Offline

Joined: Tue Jul 24, 2012 2:27 am
Posts: 679
One view of multitasking that I haven't seen discussed much is semi-preemptive checkpointing. This is used often in larger machine VMs, to interrupt threads cleanly for garbage collection, message passing, or other background processing.

The basic idea is that you write straight-line code, instead of continuation/event based handlers like typically found in cooperative multitasking. But at certain known safe points, outside of atomic actions or when the registers are free, you perform some quick check which may result in a yield. The program can then resume from there when its time comes around again.

In the main Java JIT, these safepoint checks are done by putting an memory read instruction to a fixed location at the end of subroutines and loop bodies, and the usage of all registers at these specific locations are known from the debug info for safe introspection & GC. When the VM wants to interrupt a thread, it sets that memory location to unreadable, causing a software fault at that some expected safe location. For the 6502, the simplest and fastest checkpoint test I can think of is to work with interrupts disabled, then run a CLI, SEI as the test. Just 2 bytes and 4 cycles would allow interrupts to be asynchronously raised and interrupt only at safe points of the program. This does require code injection, but again it does allow you to write straight line code, more suitable for desktop & application style programming.

In AcheronVM, the ISR uses self-modification to adjust the main VM dispatch to call out to a service handler instead of the next instruction processor. I believe the "zero overhead" Forth ISR does something similar. These can also be considered checkpoints, where the VM is in a known "safe" state outside of instruction processing that allows the whole VM state to be migrated much easier than if it were fully preemptive.

_________________
WFDis Interactive 6502 Disassembler
AcheronVM: A Reconfigurable 16-bit Virtual CPU for the 6502 Microprocessor


Top
 Profile  
Reply with quote  
 Post subject: Re: Multi tasking?
PostPosted: Fri Oct 02, 2015 6:42 am 
Online
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8543
Location: Southern California
whartung wrote:
Garth, I'd love to see a write up on this, both for the '02 and '816, and from the Forth perspective.

It's up, at http://wilsonminesco.com/stacks/ . The '816 and Forth are mentioned plenty, but it's really about 6502 stacks.

_________________
http://WilsonMinesCo.com/ lots of 6502 resources
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 52 posts ]  Go to page Previous  1, 2, 3, 4

All times are UTC


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: