NCL 303: The Display
We've been using the Display since our very first NCL program.
We've written text with:
D.CHR "Hello, world!"
We've prepared larger updates with:
D.TXT "Hello, world!"
D.BLT
And in the last lesson, we used the Display cursor to echo typed characters and erase them again.
So far, we've mostly treated the Display as somewhere text appears.
Now let's look more closely at where that text goes.
The Display grid
The standard NCS/e Display provides:
- 32 character positions across;
- 12 rows down.
We can describe every position using two coordinates:
(x, y)
x tells us how far across the Display the position is.
y tells us how far down it is.
Both begin at zero.
So the four corners are:
| Position | Coordinate |
|---|---|
| Upper-left | (0, 0) |
| Upper-right | (31, 0) |
| Lower-left | (0, 11) |
| Lower-right | (31, 11) |
The coordinate (0, 0) is therefore the first visible character position.
The coordinate (31, 11) is the last.
The cursor
The Display keeps track of a cursor position.
This is where the next character will be written.
When the Display starts, the cursor position is:
(0, 0)
Writing a character advances the cursor so that the next character can be written after it.
For example:
D.CHR "ABC"
writes three characters in sequence.
Afterward, the cursor is ready to write the next character after C.
We've already relied on this behavior whenever we've written several pieces of text one after another.
Moving the cursor
D.CUR lets us place the cursor at a particular coordinate.
For example:
D.CUR 10 4
moves it to:
(10, 4)
The first value is the horizontal position.
The second is the vertical position.
Anything written afterward begins there:
D.CUR 10 4
D.CHR "HELLO"
Instead of beginning in the upper-left corner, HELLO begins at column 10 on row 4.
D.CUR gives us absolute positioning: we tell the Display exactly where the cursor should be.
We'll shortly see several ways to move it relative to where it already is.
Making the cursor visible
Before the shell starts a program, it hides the visible cursor.
The Display still has a cursor position. You simply cannot see the cursor itself.
While we're experimenting with cursor movement, it will be useful to turn it back on.
D.CUR has an optional third value that sets the cursor mode:
D.CUR 10 4 #D.CUR.BLINK
This moves the cursor to (10, 4) and makes it blink.
The available modes are:
| Constant | Cursor |
|---|---|
#D.CUR.OFF |
Hidden |
#D.CUR.ON |
Visible |
#D.CUR.BLINK |
Visible and blinking |
The cursor mode remains in effect until something changes it.
So once we've made the cursor blink:
D.CUR 0 0 #D.CUR.BLINK
we can move it again without specifying the mode:
D.CUR 10 4
The cursor moves and continues blinking.
If you only want to change the cursor mode without changing its position, use D.CURM:
D.CURM #D.CUR.OFF
For now, we'll mostly use the optional third value of D.CUR.
Walk around the Display
Let's make the cursor visit all four corners.
D.CUR 0 0 #D.CUR.BLINK
SYS.AKEY $000
D.CUR 31 0
SYS.AKEY $000
D.CUR 31 11
SYS.AKEY $000
D.CUR 0 11
SYS.AKEY $000
Run the program.
Each SYS.AKEY waits for keyboard input before the program continues.
Press any key after each cursor movement.
The cursor should move through:
(0, 0)
(31, 0)
(31, 11)
(0, 11)
You've now seen the coordinate system directly.
Try changing the coordinates and running the program again.
The numbers in D.CUR are simply positions in the Display's coordinate system.
Control characters on the grid
In NCL 208, we met several control characters that affect Display output.
Now that we can picture the Display as a grid, we can be more precise about what they do.
| Escape | Operation | Display effect |
|---|---|---|
\r |
Carriage return | Move to column 0 |
\n |
Line feed | Move down one row, taking automatic wrapping into account |
\f |
Form feed | Move to (0, 0) |
\b |
Backspace | Move backward one character position |
\c |
Advance | Move forward one character position |
\d |
Delete | Clear the character at the cursor without moving |
\i |
Index | Move down one row |
\j |
Reverse index | Move up one row |
These aren't a separate positioning system.
They operate on the same cursor position that D.CUR changes.
For example, if the cursor is at:
(12, 4)
then:
D.CHR "\r"
moves it to:
(0, 4)
A Backspace moves in the opposite horizontal direction:
D.CHR "\b"
while Advance moves forward without writing anything:
D.CHR "\c"
This is different from writing a space. A space replaces the character at that position. \c simply moves past it.
Backspace and Delete
Backspace and Delete perform two separate operations.
\b moves the cursor backward.
\d clears the character at the current cursor position without moving it.
That's why the sequence we used in the previous lesson works:
D.CHR "\b\d"
First:
\b
moves onto the previous character.
Then:
\d
clears it.
The cursor is left in the correct position for whatever comes next.
Reaching the right edge
Normally, writing a character advances the cursor one position to the right.
Eventually, we reach the final column:
x = 31
Writing there immediately wraps the cursor to column 0 of the following row.
For example, if the cursor begins at:
(31, 4)
and we write:
D.CHR "A"
the A appears at (31, 4) and the cursor advances to:
(0, 5)
\c follows the same advancing and wrapping rules, except it does not write anything.
Line feed and automatic wrapping
There is one small detail to automatic wrapping.
When the Display wraps from the right edge, it remembers that the wrap has already moved the cursor down one row.
If the next control character is:
\n
the line feed consumes that automatic movement instead of moving down a second time.
Suppose we write exactly 32 characters beginning at column 0.
The final character causes the cursor to wrap:
(31, 4) → (0, 5)
A following line feed leaves it at:
(0, 5)
rather than moving again to (0, 6).
This makes the familiar line ending:
\r\n
behave consistently whether the preceding text ended before the right edge or exactly on it.
\r returns to column 0 while preserving the pending automatic wrap.
Then \n either:
- moves down normally; or
- recognizes that wrapping already supplied that movement.
Either way, the cursor ends at the beginning of the next line.
Line feed and index are different
\n is intended for flowing text, so it takes automatic wrapping into account.
\i is a literal index operation. It always moves the cursor down one row and clears any pending automatic wrap.
\j performs the opposite movement, indexing upward, and also clears any pending automatic wrap.
This gives programs both text-aware line movement and unconditional vertical cursor movement.
The cursor can leave the visible Display
The Display has 12 visible rows, but its logical cursor is not restricted to them.
Consider writing a character at the lower-right corner:
(31, 11)
After the character is written, the cursor advances normally:
(0, 12)
Row 12 is outside the visible Display, but that is a valid logical cursor position.
Simply moving there does not immediately scroll the Display.
If printable text is then written while the cursor is outside the visible area, the Display scrolls as necessary to bring the cursor back into the visible area.
This means reaching the lower-right corner does not cause a premature scroll merely because the cursor advanced.
Character positions and physical tiles
So far, we've described the Display as:
32 × 12 character positions
Underneath that logical grid, the physical Display is actually arranged as:
16 × 12 tiles
Each tile normally contains two character positions beside each other.
So:
32 character positions
fit across:
16 physical tiles
Characters and tiles are different units
Text positioning uses a 32×12 grid of character positions.
The physical Display underneath it contains 16×12 tiles, with two ordinary character positions in each tile.
Some Display operations work with character positions. Others work with physical tiles.
We'll make that distinction explicit whenever it matters.
For ordinary text:
| Text | Physical space |
|---|---|
"A" |
Half a tile |
"AB" |
One tile |
"ABCD" |
Two tiles |
The two character positions in a tile also share some physical Display properties.
That distinction will become increasingly useful as we start constructing complete screens.
Fullwidth characters
Some Unicode characters occupy an entire Display tile.
These are fullwidth characters.
If a fullwidth character is written while the cursor is on the left side of a tile, it occupies that complete tile.
If the cursor is on the right side, the Display first writes a space there and then places the fullwidth character in the next tile.
For example, starting from the left edge:
| Text | Placement |
|---|---|
"AB" |
A and B share one tile |
"中" |
中 occupies one complete tile |
"A中" |
A, then a blank half, then 中 in the next tile |
"AB中" |
A and B share the first tile, then 中 occupies the second |
The cursor also cannot remain in the right half of an existing fullwidth character.
If cursor movement or Backspace would place it there, the cursor moves to the left side of that character instead.
This gives us an important distinction:
S.LENmeasures characters in a string. Display space is a separate concern.
A fullwidth character can have a string length of one while occupying an entire physical tile.
Display state and what you can see
We've used both D.CHR and D.TXT, but there is an important detail behind the difference.
The Display keeps its own internal state.
Writing:
D.TXT "HELLO"
changes that state immediately.
The characters are stored in the Display and the cursor advances.
What D.TXT does not do is refresh the physical Display.
To make the current Display state visible, we use:
D.BLT
D.BLT refreshes the complete physical Display from its current state.
D.CHR also changes the Display state, but immediately refreshes the affected tiles.
| Operation | Changes Display state | Refreshes physical Display |
|---|---|---|
D.TXT |
Yes | No |
D.CHR |
Yes | Yes, affected tiles |
D.BLT |
No new text | Yes, complete Display |
D.TXT still changes the Display
D.TXT does not merely prepare a string somewhere outside the Display.
It changes the Display's internal character data and cursor position immediately.
What it does not do is refresh the physical Display.
D.BLT makes the Display's current internal state visible.
This is the behavior we took advantage of in NCL 302.
While typing, each new character needed to appear immediately:
D.CHR #key
When Enter was pressed, we could prepare a larger update:
D.TXT "\r\nHELLO, "
D.TXT #buffer
D.TXT "!"
D.BLT
The three D.TXT instructions changed the Display state.
Then one D.BLT made the complete result visible.
So our earlier practical rule still holds:
Use
D.CHRfor small changes that should appear immediately.Use
D.TXTto prepare larger changes, then useD.BLTto show them together.
Positioning text deliberately
Now we can combine cursor positioning with normal Display output.
Try:
D.CUR 2 1
D.TXT "SYSTEM READY"
D.CUR 12 5
D.TXT "STATUS"
D.CUR 20 9
D.TXT "PRESS KEY"
D.BLT
Each D.CUR chooses where the next text begins.
Nothing requires one piece of text to begin where the previous one ended.
And because D.TXT does not immediately refresh the physical Display, we can prepare all three pieces before:
D.BLT
presents the finished layout.
Until now, most of our output has flowed from wherever the previous text ended.
Now we can deliberately construct a screen.
Try it
Create a small status screen containing three pieces of text.
For example:
SYSTEM READY
STATUS
PRESS KEY
Use D.CUR to position each message.
Prepare the complete layout using D.TXT, then use one:
D.BLT
to show it.
Once it works, move STATUS somewhere else by changing only its D.CUR.
Then experiment:
- make the cursor visible with
#D.CUR.BLINK; - use
\band\cto move horizontally withoutD.CUR; - use
\iand\jto move vertically; - replace one
D.TXTwithD.CHRand observe when that text becomes visible; - place the cursor at
(31, 4), write one character, and observe where it moves; - try
\r\nafter text that ends exactly at the right edge; - write a fullwidth character beside ordinary text and observe how it aligns to the physical tiles.
The goal isn't to build anything elaborate yet.
It's to become comfortable thinking of the Display as a space that your program can arrange deliberately.
We've used the Display as a stream of text since NCL 101.
Now we know what's underneath that stream: a 32×12 coordinate system, a movable logical cursor, a 16×12 physical tile layout, and Display state that can be changed before it is physically refreshed.
In the next lesson, NCL 304: Drawing a Screen, we'll use that geometry to build a complete screen instead of simply writing one piece of text after another.