NCL 605: Non-Blocking Operations
In the previous lesson, we learned that some operations take considerably longer than others.
A simple calculation might finish almost immediately, while a full display refresh can take several seconds.
Normally, the CPU waits for an operation to finish before executing the next instruction.
But what if the CPU has something useful to do while a peripheral is busy?
Non-blocking operations allow the CPU to continue executing instructions before a peripheral has finished its work.
Blocking and Non-Blocking
When the CPU sends a request to a coprocessor or peripheral, it waits for an acknowledgement before continuing.
Normally, that acknowledgement arrives after the operation is complete:
- The CPU sends the request.
- The receiving component performs the operation.
- The component acknowledges completion.
- The CPU resumes execution.
Consider the display instruction:
D.BLT
This refreshes the entire display. Because the operation is blocking, the CPU waits until the refresh is finished.
But the display also provides:
D.BLTN
This performs the same full refresh, but acknowledges the request before doing the work:
- The CPU sends the request.
- The display acknowledges the request.
- The CPU resumes execution.
- The display performs the refresh independently.
This is a non-blocking operation.
Both instructions perform the same rendering work. Neither makes the display itself faster.
The difference is that D.BLTN allows the CPU to do something else while the display refreshes.
Preparing the Next Frame
We've already learned how to construct a complete display buffer using strings in the 300 level.
Now we can see another reason why that technique is useful.
Imagine a program that repeatedly displays the following information:
Name: Alice
Score: 100
Each time the program updates the display, it needs to prepare the next frame.
Let's compare two ways to do that.
Using Individual Display Commands
Our first approach constructs the next frame directly on the display:
-- Display the current frame.
D.BLTN
-- Prepare the next frame.
D.FIL ""; D.CUR 0 0
D.TXT "Name: "
D.TXT #name
D.TXT "\r\nScore: "
D.TXT #score
-- Display the next frame.
D.BLTN
There's a problem.
After the first D.BLTN, the display is still refreshing the previous frame.
The very next display command, D.FIL, encounters a busy peripheral.
The CPU must wait until the display can process that command before continuing.
Even though we used a non-blocking refresh, we've immediately blocked execution again.
Once the display becomes available, the CPU can send the remaining commands.
Constructing the Frame Locally
Now let's produce the same output using string operations:
-- Display the current frame.
D.BLTN
-- Prepare the next frame using the string coprocessor.
S.JOIN #frame "Name: " #name
S.JOIN #frame #frame "\r\nScore: "
S.JOIN #frame #frame #score
-- Send the completed frame.
D.FIL ""; D.CUR 0 0
D.TXT #frame
D.BLTN
This time, the CPU doesn't immediately contact the busy display.
Instead, it asks the string coprocessor to construct the next frame.
Because the string coprocessor operates independently of the display, these instructions can execute while the previous refresh is still underway.
Only after the string is complete do we send the next display commands.
The difference becomes clear when we compare the two approaches:
| Stage | Individual display commands | Local string construction |
|---|---|---|
| Previous frame begins refreshing | D.BLTN |
D.BLTN |
| Begin preparing next frame | First display command blocks | String operations proceed |
| While display is busy | CPU waits | Next frame is constructed |
| Display becomes available | Send individual display commands | Send completed frame |
| Start next refresh | D.BLTN |
D.BLTN |
The second approach doesn't guarantee that the CPU will never wait.
If the next frame is ready before the previous refresh finishes, the first display command will still block until the display becomes available.
But we've already completed the string construction by that point.
The advantage comes from performing useful work before contacting the busy peripheral again.
And there's another benefit: sending one completed string requires fewer display commands than sending each piece separately.
The Busy Peripheral
A non-blocking instruction doesn't make a peripheral capable of processing unlimited requests simultaneously.
If the display is busy, the next display command must wait until the display can handle it.
This applies even if that next command is another D.BLTN.
Non-blocking operations are most useful when the CPU can perform independent work before sending another request to the same peripheral.
When Waiting Is Useful
Not every wait is something we should try to eliminate.
Back in the 300 level, we encountered:
SYS.KEY
SYS.AKEY
SYS.KEY checks for keyboard input without waiting for a new keypress.
The A prefix in SYS.AKEY means await. The instruction deliberately suspends execution until a keypress arrives.
That's useful when the program cannot continue without user input.
There's no reason to keep executing instructions if the program has nothing meaningful to do until the user presses a key.
Non-blocking operations serve a different purpose.
With D.BLTN, the CPU can continue because it has useful work that doesn't depend on the display finishing.
With SYS.AKEY, the program deliberately waits because it needs an external event before continuing.
The goal isn't to eliminate every wait. It's to avoid waiting when useful work could be done instead.
Independent Execution
The NCS/e has only one CPU core, so how can it execute instructions while the display is busy?
The answer is that the display has its own processing capabilities.
In fact, the NCS/e's coprocessors and peripherals all have independent execution contexts.
Normally, they synchronize with the CPU after completing each requested operation. This makes the entire system behave like one sequential machine.
Non-blocking operations are an exception. They temporarily allow the CPU and a peripheral to work independently.
The NCS/e still executes only one stream of NCL instructions. It's the peripheral work that happens concurrently.
More Than One Processor?
The NCS/e is fundamentally a single-core, single-threaded computer.
Although its coprocessors and peripherals have their own independent execution contexts, they normally synchronize with the CPU after each request.
Other computer architectures take a rather more ambitious approach.
Multithreading allows multiple independent sequences of instructions, called threads, to make progress within a program.
Even with a single CPU core, the processor can switch between threads, allowing one to execute while another waits.
Multicore processing goes further, providing multiple CPU cores capable of executing instructions simultaneously.
Multiprocessor computers existed as early as the 1960s, long before multicore processors became commonplace in personal computers.
Of course, such extravagant architectures introduce complications.
What happens when two threads modify the same data? How do they coordinate their work? What happens if one thread needs a result another hasn't calculated yet?
These problems require mechanisms for scheduling, synchronization, and safely sharing resources.
The NCS/e avoids most of these complications by maintaining a single NCL execution stream.
Its non-blocking operations provide a limited form of concurrent execution without requiring multiple NCL threads.
Most peripheral operations don't need non-blocking variants.
Their processing time is often much shorter than the communication overhead, so acknowledging early would provide little practical benefit.
Non-blocking operations are most useful for lengthy tasks, such as refreshing the entire display, where the CPU has time to accomplish something meaningful while the peripheral works.
Non-blocking operations help us use time that would otherwise be spent waiting. But how can we tell whether an optimization actually improves performance?
In the next lesson, NCL 606: Measuring Time, we'll introduce the CPU's time instructions and learn how to measure the execution time of our programs.