Put CosmOS on the screen without changing a line of it
The console is now a display controller as well as a port: it owns a font, keeps a cursor, handles newline, carriage return, backspace and wrapping, and scrolls. That is an ordinary kind of chip - it is what a video terminal's character generator did - and it is the reason this rung needed no changes to CosmOS at all. CosmOS already writes bytes to port 0x00. It writes to BOTH the screen and standard output, which is deliberate. A machine with a screen and a serial line is an ordinary machine, the emulator's standard output is that serial line, and one console drives both. It is also what keeps all 165 recorded results passing under Voyager, and what makes --screen work on the plain SplitBit: there is one console and it drives everything it has. Scrolling moves the video device's origin and no memory. The row arriving at the bottom is cleared because the map is a ring and it holds what was there 128 rows ago; the rows going off the top are not, and that is a hundred rows of scrollback nothing had to keep. The test reads the register back rather than looking at the screen, because a console blitting rows instead would look identical and cost twelve percent of a frame for every line printed. The font is vendored from Hatchet-GPU with a note saying where it came from, since that repository is not part of this one. 135 glyphs in ASCII order, which is the thing that makes it worth keeping - PETSCII's whole inconvenience was that its order was not ASCII's, so a machine using it needed a translation table in front of every string. Here the machine subtracts 32. It is stored one bit a pixel and expanded into tile memory at reset: 1,088 bytes against 16 kilobytes. Voyager gets a keyboard. A window has no standard input, and a machine blocking on it inside a frame would stop drawing and stop answering, so a front end with a window installs a hook that the console calls while it has nothing: it keeps the window alive and hands back a key. The hook has to tell "nobody has typed yet", which happens sixty times a second, apart from "the window has gone", which is the end of input - one value for both would have made the first keystroke look like a closed machine. In line mode the console echoes what it is given, because there is no terminal behind a window to do it and that was always the terminal's job. Tests/video.sh grew from 14 checks to 26, half of them about the console rather than the device: those programs ask the video device for nothing and write bytes to port 0x00 like every SplitBit program always has. Verified by breaking two things - removing the scroll failed exactly the two checks about scrolling, and removing the cursor advance failed exactly the three that depend on it. Two video checks had quietly depended on palette entry 0 being black, which stopped being true the moment a machine woke up able to show text. They now set what they are about to look at, and a new check pins the waking state itself. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01E2JrLzFvuFX9fgi1LDRjrW
This commit is contained in:
co-authored by
Claude Opus 5
parent
83623a3df3
commit
773b0f8add
@@ -0,0 +1,157 @@
|
||||
// font.c
|
||||
// GENERATED ONCE from the Hatchet-GPU sprite sheet, and vendored here on purpose.
|
||||
//
|
||||
// Hatchet was an earlier attempt at a graphics system for this machine and is not part of
|
||||
// this repository. What survives of it is this font: an 8x8 sheet in ASCII order, which is
|
||||
// the thing that makes it worth keeping. PETSCII's whole inconvenience was that its order
|
||||
// was not ASCII's, so a machine using it needed a translation table in front of every
|
||||
// string. Here the machine subtracts 32 and is done.
|
||||
//
|
||||
// One bit a pixel, most significant bit leftmost, eight bytes a glyph. The screen wants
|
||||
// eight bits a pixel, so the machine expands this into tile memory at reset rather than
|
||||
// storing it expanded: 1,088 bytes here against 16 kilobytes there.
|
||||
//
|
||||
// Index 0 is ASCII 32, the space, and is blank. Everything through ASCII 126 is where
|
||||
// ASCII says it is; after that come the drawn extras - box corners and junctions, arrows,
|
||||
// the card suits - which have no ASCII to be in order with.
|
||||
|
||||
#include "font.h"
|
||||
|
||||
const unsigned char consoleFont[CONSOLE_FONT_GLYPHS * CONSOLE_FONT_BYTES] = {
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 32 space
|
||||
0x00, 0x30, 0x30, 0x30, 0x30, 0x30, 0x00, 0x30, // 33 '!'
|
||||
0x00, 0x36, 0x36, 0x12, 0x00, 0x00, 0x00, 0x00, // 34 '"'
|
||||
0x00, 0x24, 0x7E, 0x24, 0x24, 0x7E, 0x24, 0x00, // 35 '#'
|
||||
0x00, 0x18, 0x3E, 0x58, 0x3C, 0x1A, 0x7C, 0x18, // 36 '$'
|
||||
0x00, 0x62, 0x66, 0x0C, 0x18, 0x30, 0x66, 0x06, // 37 '%'
|
||||
0x00, 0x38, 0x44, 0x48, 0x30, 0x4A, 0x44, 0x3A, // 38 '&'
|
||||
0x00, 0x18, 0x18, 0x08, 0x00, 0x00, 0x00, 0x00, // 39 '''
|
||||
0x00, 0x1E, 0x38, 0x70, 0x70, 0x70, 0x38, 0x1E, // 40 '('
|
||||
0x00, 0x78, 0x1C, 0x0E, 0x0E, 0x0E, 0x1C, 0x78, // 41 ')'
|
||||
0x00, 0x00, 0x5A, 0x3C, 0x7E, 0x3C, 0x5A, 0x00, // 42 '*'
|
||||
0x00, 0x00, 0x18, 0x18, 0x7E, 0x7E, 0x18, 0x18, // 43 '+'
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x30, 0x10, // 44 ','
|
||||
0x00, 0x00, 0x00, 0x00, 0x7F, 0x7F, 0x00, 0x00, // 45 '-'
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x30, 0x00, // 46 '.'
|
||||
0x00, 0x03, 0x07, 0x0E, 0x1C, 0x38, 0x70, 0x60, // 47 '/'
|
||||
0x00, 0x3C, 0x66, 0x4E, 0x5A, 0x72, 0x66, 0x3C, // 48 '0'
|
||||
0x00, 0x18, 0x38, 0x18, 0x18, 0x18, 0x18, 0x7E, // 49 '1'
|
||||
0x00, 0x3C, 0x66, 0x46, 0x0C, 0x18, 0x30, 0x7E, // 50 '2'
|
||||
0x00, 0x3C, 0x66, 0x06, 0x0C, 0x06, 0x66, 0x3C, // 51 '3'
|
||||
0x00, 0x3C, 0x6C, 0x6C, 0x7E, 0x7E, 0x0C, 0x0C, // 52 '4'
|
||||
0x00, 0x7E, 0x60, 0x7C, 0x0E, 0x66, 0x6E, 0x3C, // 53 '5'
|
||||
0x00, 0x3C, 0x66, 0x60, 0x7C, 0x66, 0x66, 0x3C, // 54 '6'
|
||||
0x00, 0x7E, 0x66, 0x66, 0x0E, 0x1C, 0x38, 0x30, // 55 '7'
|
||||
0x00, 0x3C, 0x66, 0x66, 0x3C, 0x66, 0x66, 0x3C, // 56 '8'
|
||||
0x00, 0x3E, 0x66, 0x66, 0x3E, 0x06, 0x06, 0x06, // 57 '9'
|
||||
0x00, 0x00, 0x30, 0x30, 0x00, 0x30, 0x30, 0x00, // 58 ':'
|
||||
0x00, 0x00, 0x30, 0x30, 0x00, 0x30, 0x30, 0x10, // 59 ';'
|
||||
0x00, 0x0E, 0x1C, 0x38, 0x70, 0x38, 0x1C, 0x0E, // 60 '<'
|
||||
0x00, 0x00, 0x3E, 0x3E, 0x00, 0x3E, 0x3E, 0x00, // 61 '='
|
||||
0x00, 0x70, 0x38, 0x1C, 0x0E, 0x1C, 0x38, 0x70, // 62 '>'
|
||||
0x00, 0x3C, 0x66, 0x06, 0x1C, 0x18, 0x00, 0x18, // 63 '?'
|
||||
0x00, 0x3C, 0x66, 0x4E, 0x4E, 0x40, 0x60, 0x3C, // 64 '@'
|
||||
0x00, 0x3E, 0x36, 0x63, 0x63, 0x7F, 0x63, 0x63, // 65 'A'
|
||||
0x00, 0x7C, 0x66, 0x66, 0x7C, 0x66, 0x66, 0x7C, // 66 'B'
|
||||
0x00, 0x3E, 0x73, 0x60, 0x60, 0x60, 0x73, 0x3E, // 67 'C'
|
||||
0x00, 0x7C, 0x66, 0x63, 0x63, 0x63, 0x66, 0x7C, // 68 'D'
|
||||
0x00, 0x7E, 0x7E, 0x60, 0x78, 0x60, 0x7E, 0x7E, // 69 'E'
|
||||
0x00, 0x7E, 0x7E, 0x60, 0x7C, 0x7C, 0x60, 0x60, // 70 'F'
|
||||
0x00, 0x3E, 0x73, 0x60, 0x67, 0x63, 0x73, 0x3E, // 71 'G'
|
||||
0x00, 0x63, 0x63, 0x63, 0x7F, 0x63, 0x63, 0x63, // 72 'H'
|
||||
0x00, 0x7E, 0x18, 0x18, 0x18, 0x18, 0x18, 0x7E, // 73 'I'
|
||||
0x00, 0x7F, 0x6C, 0x0C, 0x0C, 0x6C, 0x6C, 0x38, // 74 'J'
|
||||
0x00, 0x67, 0x6E, 0x7C, 0x78, 0x7E, 0x66, 0x67, // 75 'K'
|
||||
0x00, 0x60, 0x60, 0x60, 0x60, 0x60, 0x7E, 0x7E, // 76 'L'
|
||||
0x00, 0x63, 0x77, 0x7F, 0x7F, 0x6B, 0x63, 0x63, // 77 'M'
|
||||
0x00, 0x63, 0x73, 0x7B, 0x7B, 0x6F, 0x67, 0x67, // 78 'N'
|
||||
0x00, 0x3C, 0x66, 0x66, 0x66, 0x66, 0x66, 0x3C, // 79 'O'
|
||||
0x00, 0x7E, 0x67, 0x63, 0x67, 0x7E, 0x60, 0x60, // 80 'P'
|
||||
0x00, 0x3E, 0x77, 0x63, 0x63, 0x77, 0x3E, 0x07, // 81 'Q'
|
||||
0x00, 0x7E, 0x67, 0x63, 0x66, 0x7C, 0x6E, 0x67, // 82 'R'
|
||||
0x00, 0x3E, 0x77, 0x70, 0x3E, 0x07, 0x77, 0x3E, // 83 'S'
|
||||
0x00, 0x7F, 0x6C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, // 84 'T'
|
||||
0x00, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x3E, // 85 'U'
|
||||
0x00, 0x63, 0x63, 0x63, 0x77, 0x3E, 0x1C, 0x08, // 86 'V'
|
||||
0x00, 0x63, 0x63, 0x63, 0x6B, 0x6B, 0x7F, 0x36, // 87 'W'
|
||||
0x00, 0x66, 0x66, 0x3C, 0x18, 0x3C, 0x66, 0x66, // 88 'X'
|
||||
0x00, 0x63, 0x77, 0x3E, 0x1C, 0x38, 0x70, 0x60, // 89 'Y'
|
||||
0x00, 0x7F, 0x7F, 0x0E, 0x1C, 0x38, 0x7F, 0x7F, // 90 'Z'
|
||||
0x00, 0x3E, 0x30, 0x30, 0x30, 0x30, 0x30, 0x3E, // 91 '['
|
||||
0x00, 0x60, 0x70, 0x38, 0x1C, 0x0E, 0x07, 0x03, // 92 '\'
|
||||
0x00, 0x7C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x7C, // 93 ']'
|
||||
0x00, 0x08, 0x1C, 0x3E, 0x77, 0x63, 0x00, 0x00, // 94 '^'
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7F, 0x7F, // 95 '_'
|
||||
0x00, 0x30, 0x38, 0x18, 0x00, 0x00, 0x00, 0x00, // 96 '`'
|
||||
0x00, 0x00, 0x38, 0x06, 0x3E, 0x66, 0x66, 0x3B, // 97 'a'
|
||||
0x00, 0x60, 0x60, 0x7C, 0x66, 0x66, 0x66, 0x7C, // 98 'b'
|
||||
0x00, 0x00, 0x3C, 0x66, 0x60, 0x60, 0x66, 0x3C, // 99 'c'
|
||||
0x00, 0x06, 0x06, 0x3E, 0x66, 0x66, 0x66, 0x3E, // 100 'd'
|
||||
0x00, 0x00, 0x3C, 0x66, 0x7C, 0x60, 0x62, 0x3C, // 101 'e'
|
||||
0x00, 0x1E, 0x30, 0x60, 0x7C, 0x60, 0x60, 0x60, // 102 'f'
|
||||
0x00, 0x00, 0x3C, 0x66, 0x66, 0x3E, 0x06, 0x7C, // 103 'g'
|
||||
0x00, 0x60, 0x60, 0x7C, 0x66, 0x66, 0x66, 0x66, // 104 'h'
|
||||
0x00, 0x18, 0x00, 0x18, 0x18, 0x18, 0x18, 0x18, // 105 'i'
|
||||
0x00, 0x06, 0x00, 0x06, 0x06, 0x66, 0x66, 0x3C, // 106 'j'
|
||||
0x00, 0x60, 0x66, 0x6C, 0x78, 0x7C, 0x64, 0x66, // 107 'k'
|
||||
0x00, 0x38, 0x38, 0x18, 0x18, 0x18, 0x18, 0x18, // 108 'l'
|
||||
0x00, 0x00, 0xC2, 0x66, 0x7E, 0x7E, 0x66, 0x66, // 109 'm'
|
||||
0x00, 0x00, 0xEC, 0x76, 0x66, 0x66, 0x66, 0x66, // 110 'n'
|
||||
0x00, 0x00, 0x3C, 0x66, 0x66, 0x66, 0x66, 0x3C, // 111 'o'
|
||||
0x00, 0x00, 0x7C, 0x66, 0x66, 0x7C, 0x60, 0x60, // 112 'p'
|
||||
0x00, 0x00, 0x3C, 0x66, 0x3C, 0x04, 0x19, 0x3E, // 113 'q'
|
||||
0x00, 0x00, 0x60, 0x7C, 0x6C, 0x60, 0x60, 0x60, // 114 'r'
|
||||
0x00, 0x00, 0x3C, 0x66, 0x30, 0x0C, 0x66, 0x3C, // 115 's'
|
||||
0x00, 0x18, 0x18, 0x7E, 0x58, 0x18, 0x18, 0x18, // 116 't'
|
||||
0x00, 0x00, 0x66, 0x66, 0x66, 0x66, 0x7E, 0x3D, // 117 'u'
|
||||
0x00, 0x00, 0x42, 0x66, 0x66, 0x76, 0x3C, 0x18, // 118 'v'
|
||||
0x00, 0x00, 0x42, 0x66, 0x66, 0x7E, 0x7E, 0x24, // 119 'w'
|
||||
0x00, 0x00, 0x66, 0x76, 0x38, 0x1C, 0x6E, 0x66, // 120 'x'
|
||||
0x00, 0x00, 0x66, 0x66, 0x76, 0x3E, 0x06, 0x7C, // 121 'y'
|
||||
0x00, 0x00, 0x7E, 0x66, 0x0C, 0x18, 0x32, 0x7E, // 122 'z'
|
||||
0x00, 0x0E, 0x38, 0x30, 0x18, 0x30, 0x38, 0x0E, // 123 '{'
|
||||
0x00, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, // 124 '|'
|
||||
0x00, 0x70, 0x1C, 0x0C, 0x18, 0x0C, 0x1C, 0x78, // 125 '}'
|
||||
0x00, 0x00, 0x03, 0x3B, 0x6E, 0x60, 0x00, 0x00, // 126 '~'
|
||||
0x00, 0x40, 0x60, 0x70, 0x78, 0x7C, 0x10, 0x00, // 127
|
||||
0x00, 0x00, 0x00, 0x0F, 0x1F, 0x1C, 0x18, 0x18, // 128
|
||||
0x00, 0x00, 0x00, 0xF0, 0xF8, 0x38, 0x18, 0x18, // 129
|
||||
0x00, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0x00, // 130
|
||||
0x00, 0x00, 0x00, 0xFF, 0xFF, 0x3C, 0x18, 0x18, // 131
|
||||
0x18, 0x18, 0x38, 0xF8, 0xF8, 0x38, 0x18, 0x18, // 132
|
||||
0xFF, 0xFF, 0x3C, 0x18, 0x18, 0x18, 0x18, 0x18, // 133
|
||||
0x03, 0x03, 0x07, 0xFF, 0xFF, 0x07, 0x03, 0x03, // 134
|
||||
0x01, 0x03, 0x06, 0x0C, 0x18, 0x30, 0x60, 0xC0, // 135
|
||||
0x80, 0xC0, 0x60, 0x30, 0x18, 0x0C, 0x06, 0x03, // 136
|
||||
0xFF, 0xFF, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, // 137
|
||||
0xFF, 0xFF, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, // 138
|
||||
0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 139
|
||||
0x18, 0x18, 0x1C, 0x1F, 0x0F, 0x00, 0x00, 0x00, // 140
|
||||
0x18, 0x18, 0x38, 0xF8, 0xF0, 0x00, 0x00, 0x00, // 141
|
||||
0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, // 142
|
||||
0x18, 0x18, 0x1C, 0x1F, 0x1F, 0x1C, 0x18, 0x18, // 143
|
||||
0x18, 0x18, 0x3C, 0xFF, 0xFF, 0x00, 0x00, 0x00, // 144
|
||||
0xC0, 0xC0, 0xE0, 0xFF, 0xFF, 0xE0, 0xC0, 0xC0, // 145
|
||||
0x18, 0x18, 0x18, 0x18, 0x18, 0x3C, 0xFF, 0xFF, // 146
|
||||
0xC0, 0x60, 0x30, 0x18, 0x0C, 0x06, 0x03, 0x01, // 147
|
||||
0x03, 0x06, 0x0C, 0x18, 0x30, 0x60, 0xC0, 0x80, // 148
|
||||
0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xFF, 0xFF, // 149
|
||||
0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0xFF, 0xFF, // 150
|
||||
0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, // 151
|
||||
0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, // 152
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, // 153
|
||||
0x00, 0x36, 0x7F, 0x7F, 0x7F, 0x3E, 0x1C, 0x08, // 154
|
||||
0x00, 0x08, 0x1C, 0x3E, 0x7F, 0x3E, 0x08, 0x3E, // 155
|
||||
0x00, 0x08, 0x1C, 0x3E, 0x7F, 0x3E, 0x1C, 0x08, // 156
|
||||
0x00, 0x1C, 0x1C, 0x7F, 0x7F, 0x7F, 0x08, 0x3E, // 157
|
||||
0x00, 0x18, 0x24, 0x24, 0x42, 0x42, 0x7E, 0x00, // 158
|
||||
0x00, 0x3C, 0x66, 0x42, 0x42, 0x66, 0x3C, 0x00, // 159
|
||||
0x00, 0x7E, 0x42, 0x42, 0x42, 0x42, 0x7E, 0x00, // 160
|
||||
0x00, 0x42, 0x24, 0x18, 0x18, 0x24, 0x42, 0x00, // 161
|
||||
0x00, 0x02, 0x04, 0x44, 0x28, 0x28, 0x10, 0x00, // 162
|
||||
0x08, 0x0C, 0x0E, 0xFF, 0xFF, 0x0E, 0x0C, 0x08, // 163
|
||||
0x18, 0x18, 0x18, 0x18, 0xFF, 0x7E, 0x3C, 0x18, // 164
|
||||
0x10, 0x30, 0x70, 0xFF, 0xFF, 0x70, 0x30, 0x10, // 165
|
||||
0x18, 0x3C, 0x7E, 0xFF, 0x18, 0x18, 0x18, 0x18, // 166
|
||||
};
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
// font.h
|
||||
// The console's character generator.
|
||||
// Written by Anachronaut
|
||||
|
||||
#ifndef FONT_H
|
||||
#define FONT_H
|
||||
|
||||
// Eight bytes a glyph, one bit a pixel. See font.c for where it came from.
|
||||
#define CONSOLE_FONT_BYTES 8
|
||||
#define CONSOLE_FONT_GLYPHS 135
|
||||
|
||||
// The first character the font has, so a byte maps to a glyph by subtracting this.
|
||||
#define CONSOLE_FONT_FIRST 32
|
||||
|
||||
extern const unsigned char consoleFont[CONSOLE_FONT_GLYPHS * CONSOLE_FONT_BYTES];
|
||||
|
||||
#endif // FONT_H
|
||||
+101
-3
@@ -7,6 +7,7 @@
|
||||
#include "../Assembler/assembly.h" // For the fault vector numbers.
|
||||
#include "controller.h"
|
||||
#include "video.h"
|
||||
#include "font.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
@@ -199,6 +200,78 @@ static void consoleShowWhatIsWritten(void) {
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
// ---- The console draws as well as it speaks ----
|
||||
//
|
||||
// THE VOYAGER'S CONSOLE IS A DISPLAY CONTROLLER: it takes a byte stream and puts glyphs on
|
||||
// a screen, keeps a cursor, and scrolls. That is an ordinary kind of chip - it is what a
|
||||
// video terminal's character generator did - and it is why CosmOS needs no changes at all
|
||||
// to run in a window. It already writes bytes to the console.
|
||||
//
|
||||
// It also keeps writing to standard output, and that is deliberate rather than an
|
||||
// oversight. A machine with a screen AND a serial line is completely ordinary, the emulator's
|
||||
// standard output is that serial line, and having both is what lets the whole test suite
|
||||
// hold Voyager to the same recorded results as SplitBit. It is also what makes --screen
|
||||
// work on the plain machine: there is one console, and it drives everything it has.
|
||||
|
||||
static int cursorColumn = 0;
|
||||
static int cursorRow = 0;
|
||||
|
||||
void consoleHome(void) {
|
||||
cursorColumn = 0;
|
||||
cursorRow = 0;
|
||||
}
|
||||
|
||||
static void consoleNewLine(void) {
|
||||
cursorColumn = 0;
|
||||
if (cursorRow + 1 < videoRows()) {
|
||||
cursorRow++;
|
||||
} else {
|
||||
// At the bottom, the screen moves under the cursor rather than the cursor moving
|
||||
// off the screen. One port write in the device, and no memory moves at all.
|
||||
videoScrollUp();
|
||||
}
|
||||
}
|
||||
|
||||
static void consoleDraw(uint8_t byte) {
|
||||
switch (byte) {
|
||||
case '\n':
|
||||
consoleNewLine();
|
||||
return;
|
||||
case '\r':
|
||||
cursorColumn = 0;
|
||||
return;
|
||||
case 0x08: // Backspace, which CosmOS sends when a line is being edited.
|
||||
if (cursorColumn > 0) {
|
||||
cursorColumn--;
|
||||
videoPutCell(cursorRow, cursorColumn, 0, 0);
|
||||
}
|
||||
return;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
// Anything below the font's first character has no glyph and no agreed meaning here.
|
||||
// Drawing a box for it would put marks on the screen that nothing asked for.
|
||||
if (byte < CONSOLE_FONT_FIRST) {
|
||||
return;
|
||||
}
|
||||
videoPutCell(cursorRow, cursorColumn, (uint8_t)(byte - CONSOLE_FONT_FIRST), 0);
|
||||
if (++cursorColumn >= videoColumns()) {
|
||||
consoleNewLine();
|
||||
}
|
||||
}
|
||||
|
||||
// ---- Waiting for a key when there is no terminal to wait on ----
|
||||
//
|
||||
// A window has no standard input, and a machine that blocked on it inside a frame would
|
||||
// stop drawing and stop answering. So a front end with a window installs a hook: called
|
||||
// while the console has nothing, it gets to keep the window alive and hands back a byte
|
||||
// when one is typed, or -1 to say the window has gone.
|
||||
static int (*inputHook)(void) = NULL;
|
||||
|
||||
void consoleSetInputHook(int (*hook)(void)) {
|
||||
inputHook = hook;
|
||||
}
|
||||
|
||||
uint8_t consoleReadByte(void) {
|
||||
// Taking the byte answers whatever the console was asking about, so the line comes
|
||||
// down here as well as when the CPU acknowledges it. Otherwise a program that reads
|
||||
@@ -211,6 +284,31 @@ uint8_t consoleReadByte(void) {
|
||||
return byte;
|
||||
}
|
||||
consoleShowWhatIsWritten();
|
||||
if (inputHook != NULL) {
|
||||
for (;;) {
|
||||
int got = inputHook();
|
||||
if (got >= 0) {
|
||||
// ---- The screen is the terminal now ----
|
||||
//
|
||||
// In line mode a terminal echoes what is typed and rubs out a backspace,
|
||||
// and CosmOS has always relied on that. There is no terminal behind a
|
||||
// window, so the display controller does it - which is exactly the job a
|
||||
// video terminal's character generator had. In key mode nothing echoes,
|
||||
// because a program in key mode is drawing its own screen.
|
||||
if (!consoleKeyMode) {
|
||||
consoleDraw((uint8_t)got);
|
||||
}
|
||||
return (uint8_t)got;
|
||||
}
|
||||
if (got == CONSOLE_GONE) {
|
||||
// The window has closed, which is this machine's end of input the way a
|
||||
// closed pipe is the other one's.
|
||||
consoleEnded = 1;
|
||||
return 0xFF;
|
||||
}
|
||||
// Nothing typed yet. The hook kept the window alive; ask it again.
|
||||
}
|
||||
}
|
||||
unsigned char byte;
|
||||
for (;;) {
|
||||
ssize_t got = read(STDIN_FILENO, &byte, 1);
|
||||
@@ -658,9 +756,9 @@ uint8_t OutputHandler(uint8_t DataByte, uint8_t Address) {
|
||||
// This function sends the DataByte to the appropriate place based on the Port Address.
|
||||
switch(Address) {
|
||||
case CONSOLE_DATA:
|
||||
// If data is sent here, it should be written to STDOUT.
|
||||
// For now, I'll implement this so it simply writes each byte out as it comes in.
|
||||
// Later, I'll want to use a buffer for this for performance, probably.
|
||||
// Both, always. The screen because this machine has one, and standard output
|
||||
// because the serial line is how everything that is not a person reads it.
|
||||
consoleDraw(DataByte);
|
||||
putchar(DataByte);
|
||||
break;
|
||||
case CONSOLE_CONTROL: consoleSetControl(DataByte); break;
|
||||
|
||||
@@ -132,6 +132,23 @@ void consoleRestore(void);
|
||||
// of its own behind that would make the status port lie about what is waiting.
|
||||
uint8_t consoleReadByte(void);
|
||||
|
||||
// Puts the cursor back in the corner. Called when the machine starts, since the screen is
|
||||
// cleared then too and a cursor left where the last program stopped would be a cursor
|
||||
// pointing into something that is gone.
|
||||
void consoleHome(void);
|
||||
|
||||
// ---- How a front end with a window feeds the console ----
|
||||
//
|
||||
// Called while the console has nothing to give. It returns a byte, or one of the two
|
||||
// answers below. They have to be told apart: a window with nobody typing yet is the normal
|
||||
// case and happens sixty times a second, while a window that has gone is the end of input.
|
||||
// One value for both would have made the first keystroke look like a closed machine.
|
||||
//
|
||||
// Without a hook the console reads standard input, which is what it has always done.
|
||||
#define CONSOLE_NOTHING_YET (-1)
|
||||
#define CONSOLE_GONE (-2)
|
||||
void consoleSetInputHook(int (*hook)(void));
|
||||
|
||||
// ---- Device classes ----
|
||||
//
|
||||
// What kind of thing is plugged into a port. Class 0 is not a device: reading an
|
||||
|
||||
@@ -107,6 +107,7 @@ uint8_t machineStart(Machine *m, const EmulatorOptions *options, const char *pro
|
||||
// the device's, and a reset that left last program's screen up would be a reset that
|
||||
// did not happen.
|
||||
videoReset();
|
||||
consoleHome();
|
||||
// The controller has to know where the memories are before anything can reach
|
||||
// them through it. Banks 0 and 1 are those two arrays.
|
||||
initializeController(Program, Data);
|
||||
@@ -190,6 +191,7 @@ void machineRunSlice(Machine *m) {
|
||||
return;
|
||||
}
|
||||
videoReset();
|
||||
consoleHome();
|
||||
initializeCPU(&m->cpu, Program, Data);
|
||||
break; // Out of this batch; the loop above carries on with a new CPU.
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
// Written by Anachronaut
|
||||
|
||||
#include "video.h"
|
||||
#include "font.h"
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
@@ -29,12 +30,66 @@ static int renderedHeight = 0;
|
||||
static int columnsFor(uint8_t m) { return m == VIDEO_MODE_80x50 ? 80 : 40; }
|
||||
static int rowsFor(uint8_t m) { return m == VIDEO_MODE_80x50 ? 50 : 25; }
|
||||
|
||||
int videoColumns(void) { return columnsFor(mode); }
|
||||
int videoRows(void) { return rowsFor(mode); }
|
||||
|
||||
// ---- The two colours a machine wakes up with ----
|
||||
//
|
||||
// Only two, and the rest of the palette left at zero. A program that wants colour sets it,
|
||||
// and a machine that guessed sixteen entries on its behalf would be sixteen entries it had
|
||||
// to overwrite. What it must not do is wake up unable to show text at all.
|
||||
static const uint8_t defaultInk[3] = { 0xDC, 0xE6, 0xDC };
|
||||
static const uint8_t defaultPaper[3] = { 0x10, 0x14, 0x12 };
|
||||
|
||||
void videoLoadFont(void) {
|
||||
// One bit a pixel becomes one byte a pixel: index 1 where the font has a dot and 0
|
||||
// where it does not, which is what makes the two palette entries below mean ink and
|
||||
// paper. Glyphs the font does not have are left blank rather than left as whatever was
|
||||
// in tile memory.
|
||||
memset(videoRAM + VIDEO_TILE_BASE, 0, (size_t)VIDEO_TILE_COUNT * VIDEO_TILE_BYTES);
|
||||
for (int glyph = 0; glyph < CONSOLE_FONT_GLYPHS && glyph < VIDEO_TILE_COUNT; glyph++) {
|
||||
uint8_t *tile = videoRAM + VIDEO_TILE_BASE + glyph * VIDEO_TILE_BYTES;
|
||||
for (int y = 0; y < CONSOLE_FONT_BYTES; y++) {
|
||||
const unsigned char row = consoleFont[glyph * CONSOLE_FONT_BYTES + y];
|
||||
for (int x = 0; x < VIDEO_CELL_PIXELS; x++) {
|
||||
tile[y * VIDEO_CELL_PIXELS + x] = (row & (0x80u >> x)) ? 1 : 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
uint8_t *palette = videoRAM + VIDEO_PALETTE_BASE;
|
||||
memcpy(palette + 0 * VIDEO_PALETTE_BYTES, defaultPaper, 3);
|
||||
memcpy(palette + 1 * VIDEO_PALETTE_BYTES, defaultInk, 3);
|
||||
}
|
||||
|
||||
void videoPutCell(int screenRow, int column, uint8_t tile, uint8_t attribute) {
|
||||
if (screenRow < 0 || screenRow >= rowsFor(mode)) return;
|
||||
if (column < 0 || column >= columnsFor(mode)) return;
|
||||
const int mapRow = (scroll + screenRow) % VIDEO_MAP_ROWS;
|
||||
uint8_t *cell = videoRAM + VIDEO_MAP_BASE + mapRow * VIDEO_MAP_STRIDE
|
||||
+ column * VIDEO_CELL_BYTES;
|
||||
cell[0] = tile;
|
||||
cell[1] = attribute;
|
||||
}
|
||||
|
||||
void videoScrollUp(void) {
|
||||
scroll = (uint8_t)((scroll + 1) % VIDEO_MAP_ROWS);
|
||||
// The row now at the bottom held whatever was there a ring ago, so it is cleared. The
|
||||
// rows that went off the top are NOT cleared, which is the whole of the scrollback: a
|
||||
// hundred rows of what has already been said, still sitting in the map.
|
||||
const int bottom = rowsFor(mode) - 1;
|
||||
const int mapRow = (scroll + bottom) % VIDEO_MAP_ROWS;
|
||||
memset(videoRAM + VIDEO_MAP_BASE + mapRow * VIDEO_MAP_STRIDE, 0, VIDEO_MAP_STRIDE);
|
||||
}
|
||||
|
||||
void videoReset(void) {
|
||||
memset(videoRAM, 0, sizeof(videoRAM));
|
||||
mode = VIDEO_MODE_40x25;
|
||||
scroll = 0;
|
||||
renderedWidth = 0;
|
||||
renderedHeight = 0;
|
||||
// A machine wakes up able to show text. Everything here is ordinary video memory that a
|
||||
// program may overwrite the moment it wants the screen for something else.
|
||||
videoLoadFont();
|
||||
}
|
||||
|
||||
uint8_t *videoMemory(uint32_t *capacity) {
|
||||
|
||||
@@ -85,6 +85,28 @@
|
||||
|
||||
void videoReset(void);
|
||||
|
||||
// ---- What the console needs to draw with ----
|
||||
//
|
||||
// The Voyager's console is a display controller: it takes a byte stream and puts glyphs on
|
||||
// the screen, the way a video terminal's character generator does. That is a real kind of
|
||||
// chip rather than an emulator convenience - but it does mean the console and a program
|
||||
// drawing graphics are writing one screen, because a machine has one screen.
|
||||
//
|
||||
// The font is expanded into tile memory at reset rather than stored expanded: 1,088 bytes
|
||||
// of one-bit rows against 16 kilobytes of tiles.
|
||||
void videoLoadFont(void);
|
||||
|
||||
int videoColumns(void);
|
||||
int videoRows(void);
|
||||
|
||||
// Screen coordinates, not map coordinates. The ring is the device's business, and a caller
|
||||
// that had to know where the origin was would have to be told every time it moved.
|
||||
void videoPutCell(int screenRow, int column, uint8_t tile, uint8_t attribute);
|
||||
|
||||
// Moves the origin on by a row and clears the one that has just come into view at the
|
||||
// bottom - which is holding whatever was there 128 rows ago, since the map is a ring.
|
||||
void videoScrollUp(void);
|
||||
|
||||
uint8_t *videoMemory(uint32_t *capacity);
|
||||
|
||||
uint8_t videoWrite(uint8_t value, uint8_t port);
|
||||
|
||||
+74
-30
@@ -22,6 +22,7 @@
|
||||
|
||||
#include "machine.h"
|
||||
#include "video.h"
|
||||
#include "io.h"
|
||||
#include "utility.h"
|
||||
#include "raylib.h"
|
||||
#include <stdio.h>
|
||||
@@ -58,6 +59,65 @@ static int takeHeadless(int *argc, char *argv[]) {
|
||||
return headless;
|
||||
}
|
||||
|
||||
// ---- The window, kept in one place ----
|
||||
//
|
||||
// Both the frame loop and the input hook have to be able to present, because a machine
|
||||
// waiting for a key is still a machine somebody is looking at. A window that froze while a
|
||||
// program asked a question would look broken every time it asked one.
|
||||
static Texture2D screenTexture;
|
||||
static int windowOpen = 0;
|
||||
|
||||
static void presentFrame(void) {
|
||||
// The device turns video memory into pixels; this puts them on the glass. Everything
|
||||
// that decides what the screen looks like is in the machine, where the suite can
|
||||
// reach it.
|
||||
videoRender();
|
||||
int width, height;
|
||||
const uint8_t *frame = videoPixels(&width, &height);
|
||||
if (width > 0 && height > 0) {
|
||||
UpdateTextureRec(screenTexture, (Rectangle){ 0, 0, (float)width, (float)height },
|
||||
frame);
|
||||
}
|
||||
BeginDrawing();
|
||||
// Not black. The border around a smaller mode should look like a machine with a screen
|
||||
// on, rather than like a window that failed to open.
|
||||
ClearBackground((Color){ 18, 22, 20, 255 });
|
||||
if (width > 0 && height > 0) {
|
||||
// Centred, so changing mode moves the picture rather than the window.
|
||||
Rectangle from = { 0, 0, (float)width, (float)height };
|
||||
Rectangle to = {
|
||||
(float)((VIDEO_MAX_WIDTH * SCREEN_SCALE - width * SCREEN_SCALE) / 2),
|
||||
(float)((VIDEO_MAX_HEIGHT * SCREEN_SCALE - height * SCREEN_SCALE) / 2),
|
||||
(float)(width * SCREEN_SCALE), (float)(height * SCREEN_SCALE)
|
||||
};
|
||||
DrawTexturePro(screenTexture, from, to, (Vector2){ 0, 0 }, 0.0f, WHITE);
|
||||
}
|
||||
EndDrawing();
|
||||
}
|
||||
|
||||
// What the console asks while it is waiting. Presenting from in here is what keeps the
|
||||
// window answering, and EndDrawing paces it, so waiting for a key costs a frame rather
|
||||
// than a spin.
|
||||
static int voyagerKey(void) {
|
||||
if (!windowOpen || WindowShouldClose()) {
|
||||
windowOpen = 0;
|
||||
return CONSOLE_GONE;
|
||||
}
|
||||
presentFrame();
|
||||
int character = GetCharPressed();
|
||||
if (character > 0 && character < 128) {
|
||||
return character;
|
||||
}
|
||||
switch (GetKeyPressed()) {
|
||||
// The keys a character queue does not carry, because they are not characters.
|
||||
case KEY_ENTER: case KEY_KP_ENTER: return '\n';
|
||||
case KEY_BACKSPACE: return 0x08;
|
||||
case KEY_TAB: return '\t';
|
||||
case KEY_ESCAPE: return 0x1B;
|
||||
default: return CONSOLE_NOTHING_YET;
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
int headless = takeHeadless(&argc, argv);
|
||||
|
||||
@@ -103,47 +163,31 @@ int main(int argc, char *argv[]) {
|
||||
// allocation sixty times a second for a picture that is the same size every time.
|
||||
Image blank = GenImageColor(VIDEO_MAX_WIDTH, VIDEO_MAX_HEIGHT, BLACK);
|
||||
ImageFormat(&blank, PIXELFORMAT_UNCOMPRESSED_R8G8B8);
|
||||
Texture2D screen = LoadTextureFromImage(blank);
|
||||
screenTexture = LoadTextureFromImage(blank);
|
||||
UnloadImage(blank);
|
||||
windowOpen = 1;
|
||||
// The keyboard becomes the console's input, in place of a standard input the window
|
||||
// does not have.
|
||||
consoleSetInputHook(voyagerKey);
|
||||
// ---- A slice a frame ----
|
||||
//
|
||||
// The machine gets its turn, then the window gets its turn. Closing the window
|
||||
// stops the machine, and the machine halting leaves the window up so that whatever
|
||||
// it drew is still there to look at - a program that ends should not take its
|
||||
// output off the screen with it.
|
||||
while (!WindowShouldClose()) {
|
||||
// A slice, then a frame. The machine halting leaves the window up so that whatever
|
||||
// it drew is still there to look at - a program that ends should not take its output
|
||||
// off the screen with it.
|
||||
while (windowOpen && !WindowShouldClose()) {
|
||||
if (machineRunning(&machine)) {
|
||||
machineRunSlice(&machine);
|
||||
}
|
||||
// ---- Presenting, not deciding ----
|
||||
//
|
||||
// The device turns video memory into pixels; this puts them on the glass. The
|
||||
// split is the whole design: everything that decides what the screen looks like
|
||||
// is in the machine, where the suite can reach it.
|
||||
videoRender();
|
||||
int width, height;
|
||||
const uint8_t *frame = videoPixels(&width, &height);
|
||||
if (width > 0 && height > 0) {
|
||||
UpdateTextureRec(screen, (Rectangle){ 0, 0, (float)width, (float)height },
|
||||
frame);
|
||||
}
|
||||
BeginDrawing();
|
||||
// Not black. The border around a smaller mode should look like a machine with
|
||||
// a screen on, rather than like a window that failed to open.
|
||||
ClearBackground((Color){ 18, 22, 20, 255 });
|
||||
if (width > 0 && height > 0) {
|
||||
// Centred, so changing mode moves the picture rather than the window.
|
||||
Rectangle from = { 0, 0, (float)width, (float)height };
|
||||
Rectangle to = {
|
||||
(float)((VIDEO_MAX_WIDTH * SCREEN_SCALE - width * SCREEN_SCALE) / 2),
|
||||
(float)((VIDEO_MAX_HEIGHT * SCREEN_SCALE - height * SCREEN_SCALE) / 2),
|
||||
(float)(width * SCREEN_SCALE), (float)(height * SCREEN_SCALE)
|
||||
};
|
||||
DrawTexturePro(screen, from, to, (Vector2){ 0, 0 }, 0.0f, WHITE);
|
||||
}
|
||||
EndDrawing();
|
||||
presentFrame();
|
||||
}
|
||||
UnloadTexture(screen);
|
||||
windowOpen = 0;
|
||||
// Taken back before the machine stops, so nothing can ask a window that has gone.
|
||||
consoleSetInputHook(NULL);
|
||||
UnloadTexture(screenTexture);
|
||||
CloseWindow();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user