Give the Voyager a screen
A tile engine on ports 0x30 to 0x3F, bringing one bank of video memory registered the way the disk's buffer is. The CPU writes cell indices and the device turns them into pixels, which is the whole reason a screen is affordable at a megahertz: a frame is 16,667 cycles, a full 320 by 200 picture is 64,000 bytes, and a 40 by 25 map is 2,000. A program that changes two cells writes four bytes. The cost of a screen becomes the number of cells that changed rather than the number of pixels on it. Which makes colour depth free, so the tiles are eight bits: an 8 by 8 cell is 64 pixels and each picks independently out of 256 colours, with no per-cell limit of the kind that made a Spectrum two and C64 multicolour four. The low nibble of a cell's attribute is ADDED to every index in its tile, sixteen at a time, so a tile drawn in 0 to 15 appears in any of sixteen schemes without a second copy in tile memory - and a tile wanting all 256 leaves the nibble at zero and gets them. Neither use costs the other anything. Two decisions are arithmetic rather than taste, and both come from the machine having no multiply. A map row is a page whether the mode fills it or not, so a cell address is the row number as the high byte and the doubled column as the low byte with no arithmetic at all; otherwise every cursor move on a 40 column screen would cost a row-times-40 in software. And a palette entry is four bytes rather than three, so entry n is at n times four, a shift. THE MAP IS A RING and the Scroll register says which of its 128 rows is on top. Scrolling moves a register and no memory: blitting a 40 by 25 screen up one line is 1,920 bytes inside one bank, which is twelve percent of a frame even with the controller widened, and a program printing one page would spend six frames shuffling memory. It is now one port write - and the rows that scrolled off are still there, which is where a terminal gets scrollback it never had. The device is part of the machine rather than part of the window. It renders into a buffer that is a pure function of video memory, so the same program draws the same picture with nobody watching; Voyager puts that buffer on the glass and decides nothing. Both binaries take --screen, which saves a PPM when the machine stops, and that is what makes a screen checkable on a host with no display at all. Tests/video.sh checks fourteen named behaviours rather than comparing a recorded image, because a recorded image would say "something changed" and leave which of the palette, the tile, the attribute, the map or the scroll register broke to be found by hand. Verified by breaking three things in turn: the additive nibble failed exactly one check, the scroll origin exactly two, and moving every cell one pixel sideways exactly the four about placement. Tests/docs.sh could not count past nine, which is how a suite of ten scripts reported itself as wrong for the wrong reason. 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
e3ef25e3b3
commit
83623a3df3
@@ -6,6 +6,7 @@
|
||||
#include "io.h"
|
||||
#include "../Assembler/assembly.h" // For the fault vector numbers.
|
||||
#include "controller.h"
|
||||
#include "video.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
@@ -555,6 +556,12 @@ uint8_t *deviceMemory(uint8_t port, uint32_t *capacity) {
|
||||
*capacity = DISK_BLOCK_BYTES;
|
||||
return diskBuffer;
|
||||
}
|
||||
if (port == PORT_VIDEO) {
|
||||
// Tiles, the map and the palette, in one bank. A program blits the part that
|
||||
// changed and the rest stays as it was, which is the whole reason the screen is a
|
||||
// bank rather than a window onto a port.
|
||||
return videoMemory(capacity);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -582,6 +589,7 @@ static const DeviceRecord deviceTable[] = {
|
||||
{ PORT_MACHINE, DEVICE_MACHINE, 0 },
|
||||
{ PORT_MEMORY, DEVICE_MEMORY, DEVICE_FLAG_HAS_MEMORY },
|
||||
{ PORT_DISK, DEVICE_DISK, DEVICE_FLAG_HAS_MEMORY },
|
||||
{ PORT_VIDEO, DEVICE_VIDEO, DEVICE_FLAG_HAS_MEMORY },
|
||||
{ PORT_REGISTRY, DEVICE_REGISTRY, 0 },
|
||||
};
|
||||
static const int deviceCount = (int)(sizeof(deviceTable) / sizeof(deviceTable[0]));
|
||||
@@ -610,6 +618,10 @@ static const DeviceRecord *deviceOnPort(uint8_t port) {
|
||||
// memory and raises the line. The rest of the block reports the same device.
|
||||
return deviceOnPort(PORT_DISK);
|
||||
}
|
||||
if (port > PORT_VIDEO && port <= PORT_VIDEO_TOP) {
|
||||
// Sixteen ports, one device, and the same rule again.
|
||||
return deviceOnPort(PORT_VIDEO);
|
||||
}
|
||||
for (int i = 0; i < deviceCount; i++) {
|
||||
if (deviceTable[i].port == port) {
|
||||
return &deviceTable[i];
|
||||
@@ -639,6 +651,10 @@ uint8_t OutputHandler(uint8_t DataByte, uint8_t Address) {
|
||||
if (Address >= CONTROLLER_PORT_BASE && Address <= CONTROLLER_PORT_TOP) {
|
||||
return controllerWrite(DataByte, Address);
|
||||
}
|
||||
// And so does the screen.
|
||||
if (Address >= PORT_VIDEO && Address <= PORT_VIDEO_TOP) {
|
||||
return videoWrite(DataByte, Address);
|
||||
}
|
||||
// This function sends the DataByte to the appropriate place based on the Port Address.
|
||||
switch(Address) {
|
||||
case CONSOLE_DATA:
|
||||
@@ -704,6 +720,9 @@ uint8_t InputHandler(uint8_t Address) {
|
||||
if (Address >= CONTROLLER_PORT_BASE && Address <= CONTROLLER_PORT_TOP) {
|
||||
return controllerRead(Address);
|
||||
}
|
||||
if (Address >= PORT_VIDEO && Address <= PORT_VIDEO_TOP) {
|
||||
return videoRead(Address);
|
||||
}
|
||||
switch(Address) {
|
||||
case CONSOLE_DATA:
|
||||
// If data is sent here, it should be read from STDIN.
|
||||
|
||||
@@ -51,6 +51,14 @@
|
||||
#define DISK_BLOCK_LOW 0x21
|
||||
#define DISK_COMMAND 0x22
|
||||
#define DISK_STATUS 0x23
|
||||
// ---- The screen ----
|
||||
//
|
||||
// Sixteen ports, like the controller, and it interrupts on its base the way the disk
|
||||
// established for a device that spans more than one. The registers themselves are in
|
||||
// video.h, with the memory layout they describe.
|
||||
#define PORT_VIDEO 0x30
|
||||
#define PORT_VIDEO_TOP 0x3F
|
||||
|
||||
#define PORT_REGISTRY 0xFF
|
||||
|
||||
// ---- The console ----
|
||||
@@ -144,6 +152,7 @@ uint8_t consoleReadByte(void);
|
||||
#define DEVICE_REFUSE 0x11
|
||||
#define DEVICE_MEMORY 0x12
|
||||
#define DEVICE_DISK 0x13
|
||||
#define DEVICE_VIDEO 0x14
|
||||
|
||||
// What a device brings besides itself. This means memory that somebody has to register
|
||||
// with the controller, so the controller's own bank 2 does not count: it is already there.
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
#include "cpu.h"
|
||||
#include "controller.h"
|
||||
#include "io.h"
|
||||
#include "video.h"
|
||||
#include "utility.h"
|
||||
#include "../Assembler/assembly.h"
|
||||
#include <stdio.h>
|
||||
@@ -102,6 +103,10 @@ uint8_t machineStart(Machine *m, const EmulatorOptions *options, const char *pro
|
||||
if (options->disk != NULL && attachDisk(options->disk, options->writeProtect)) {
|
||||
return MACHINE_ERROR;
|
||||
}
|
||||
// The screen starts blank, and starts blank again on a warm restart: video memory is
|
||||
// the device's, and a reset that left last program's screen up would be a reset that
|
||||
// did not happen.
|
||||
videoReset();
|
||||
// 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);
|
||||
@@ -184,6 +189,7 @@ void machineRunSlice(Machine *m) {
|
||||
m->restartFailed = 1;
|
||||
return;
|
||||
}
|
||||
videoReset();
|
||||
initializeCPU(&m->cpu, Program, Data);
|
||||
break; // Out of this batch; the loop above carries on with a new CPU.
|
||||
}
|
||||
@@ -203,7 +209,15 @@ void machineRunSlice(Machine *m) {
|
||||
}
|
||||
|
||||
void machineStop(Machine *m) {
|
||||
(void)m;
|
||||
// ---- Saving the screen ----
|
||||
//
|
||||
// Written when the machine stops, and it is what makes the screen testable at all: a
|
||||
// suite has no display, so the only way to check what was drawn is to be handed it. A
|
||||
// picture out of a headless run is also the quickest way for a person to see what a
|
||||
// program actually put on the screen without sitting and watching it happen.
|
||||
if (m->options.screen != NULL) {
|
||||
videoWriteImage(m->options.screen);
|
||||
}
|
||||
detachDisk();
|
||||
}
|
||||
|
||||
|
||||
@@ -27,6 +27,9 @@ void printHelp(const char *programName) {
|
||||
printf(" default, finishes before the next instruction starts.\n");
|
||||
printf(" -W, --write-protect Attach the disk read only. A disk the host will not let\n");
|
||||
printf(" you write is read only whether you ask for this or not.\n");
|
||||
printf(" -S, --screen FILE Save a picture of the screen, as a PPM, when the machine\n");
|
||||
printf(" stops. Works with or without a window, which is how the\n");
|
||||
printf(" tests look at a screen on a host that has no display.\n");
|
||||
printf(" -h, --help Display this help message.\n");
|
||||
}
|
||||
|
||||
@@ -38,6 +41,7 @@ uint8_t parseOptions(int argc, char *argv[], EmulatorOptions *options) {
|
||||
{"disk", required_argument, 0, 'D'},
|
||||
{"write-protect", no_argument, 0, 'W'},
|
||||
{"disk-cycles", required_argument, 0, 'L'},
|
||||
{"screen", required_argument, 0, 'S'},
|
||||
{"help", no_argument, 0, 'h'},
|
||||
{0, 0, 0, 0 }
|
||||
};
|
||||
@@ -50,9 +54,10 @@ uint8_t parseOptions(int argc, char *argv[], EmulatorOptions *options) {
|
||||
options->disk = NULL;
|
||||
options->writeProtect = 0;
|
||||
options->diskCycles = 0;
|
||||
options->screen = NULL;
|
||||
|
||||
// Parse options
|
||||
while ((opt = getopt_long(argc, argv, "dc:fhD:WL:", long_options, &option_index)) != -1) {
|
||||
while ((opt = getopt_long(argc, argv, "dc:fhD:WL:S:", long_options, &option_index)) != -1) {
|
||||
switch (opt) {
|
||||
case 'd':
|
||||
options->debug = 1;
|
||||
@@ -82,6 +87,9 @@ uint8_t parseOptions(int argc, char *argv[], EmulatorOptions *options) {
|
||||
case 'L':
|
||||
options->diskCycles = strtoul(optarg, NULL, 0);
|
||||
break;
|
||||
case 'S':
|
||||
options->screen = optarg;
|
||||
break;
|
||||
case 'h':
|
||||
printHelp(argv[0]);
|
||||
return OPTIONS_HELP;
|
||||
|
||||
@@ -22,6 +22,7 @@ typedef struct {
|
||||
unsigned long diskCycles; // How long a block move takes. Zero is instant, and the default.
|
||||
const char *disk; // Disk image to attach, or NULL for a machine with no disk.
|
||||
uint8_t writeProtect; // Attach the disk read only, the way a tab on a floppy would.
|
||||
const char *screen; // Where to save a picture of the screen when the machine stops.
|
||||
} EmulatorOptions;
|
||||
|
||||
uint8_t parseOptions(int argc, char *argv[], EmulatorOptions *options);
|
||||
|
||||
@@ -0,0 +1,149 @@
|
||||
// video.c
|
||||
// The Voyager's video device.
|
||||
// Written by Anachronaut
|
||||
|
||||
#include "video.h"
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
// The bank the device brings. Registered by whoever enumerates the hardware, reached only
|
||||
// through the memory controller, and never by the CPU directly - the same arrangement the
|
||||
// disk's buffer has always had.
|
||||
static uint8_t videoRAM[VIDEO_MEMORY_BYTES];
|
||||
|
||||
static uint8_t mode;
|
||||
// Which map row is drawn at the top. THE MAP IS A RING: rendering row r reads map row
|
||||
// (scroll + r) wrapped, so scrolling a screen moves this byte and moves no memory at all.
|
||||
//
|
||||
// That is worth more than it looks. Blitting a 40 by 25 screen up one line is 1,920 bytes
|
||||
// inside one bank, which is 1,920 cycles even with the controller widened - twelve percent
|
||||
// of a frame, every line. A program printing one page would spend six frames shuffling
|
||||
// memory. Here it costs one port write, and the rows that scrolled off are still there,
|
||||
// which is where the console gets scrollback it never had.
|
||||
static uint8_t scroll;
|
||||
|
||||
static uint8_t pixels[VIDEO_MAX_WIDTH * VIDEO_MAX_HEIGHT * 3];
|
||||
static int renderedWidth = 0;
|
||||
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; }
|
||||
|
||||
void videoReset(void) {
|
||||
memset(videoRAM, 0, sizeof(videoRAM));
|
||||
mode = VIDEO_MODE_40x25;
|
||||
scroll = 0;
|
||||
renderedWidth = 0;
|
||||
renderedHeight = 0;
|
||||
}
|
||||
|
||||
uint8_t *videoMemory(uint32_t *capacity) {
|
||||
*capacity = VIDEO_MEMORY_BYTES;
|
||||
return videoRAM;
|
||||
}
|
||||
|
||||
uint8_t videoWrite(uint8_t value, uint8_t port) {
|
||||
switch (port) {
|
||||
case VIDEO_MODE:
|
||||
// A mode that does not exist is not taken. Refusing outright would be the other
|
||||
// choice, but a screen is not the place to stop the machine: a program that
|
||||
// asked for something impossible still has the screen it had.
|
||||
if (value < VIDEO_MODE_COUNT) {
|
||||
mode = value;
|
||||
}
|
||||
break;
|
||||
case VIDEO_SCROLL:
|
||||
// Wrapped rather than clipped, because the map is a ring and every byte names a
|
||||
// row that exists.
|
||||
scroll = (uint8_t)(value % VIDEO_MAP_ROWS);
|
||||
break;
|
||||
default:
|
||||
// Everything else is read only or not there yet. Writing does nothing rather
|
||||
// than refusing: a port block reserved for later should be quiet, not fatal.
|
||||
break;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8_t videoRead(uint8_t port) {
|
||||
switch (port) {
|
||||
// Reserved for the frame interrupt, which is the next rung. Zero until then.
|
||||
case VIDEO_STATUS: return 0;
|
||||
case VIDEO_MODE: return mode;
|
||||
// Asked rather than assumed. A program that wants to know how wide the screen is
|
||||
// should be able to find out, the same way it asks the console what mode it is in.
|
||||
case VIDEO_COLUMNS: return (uint8_t)columnsFor(mode);
|
||||
case VIDEO_ROWS: return (uint8_t)rowsFor(mode);
|
||||
case VIDEO_SCROLL: return scroll;
|
||||
default: return 0;
|
||||
}
|
||||
}
|
||||
|
||||
void videoRender(void) {
|
||||
const int columns = columnsFor(mode);
|
||||
const int rows = rowsFor(mode);
|
||||
const int width = columns * VIDEO_CELL_PIXELS;
|
||||
|
||||
for (int row = 0; row < rows; row++) {
|
||||
// The ring. Rows that scrolled off the top are still in the map, which is what
|
||||
// makes scrollback free rather than something the console has to keep itself.
|
||||
const int mapRow = (scroll + row) % VIDEO_MAP_ROWS;
|
||||
const uint8_t *cells = videoRAM + VIDEO_MAP_BASE + mapRow * VIDEO_MAP_STRIDE;
|
||||
for (int column = 0; column < columns; column++) {
|
||||
const uint8_t tile = cells[column * VIDEO_CELL_BYTES];
|
||||
const uint8_t attribute = cells[column * VIDEO_CELL_BYTES + 1];
|
||||
// ---- The additive nibble ----
|
||||
//
|
||||
// The low nibble of the attribute is added to every palette index in the tile,
|
||||
// sixteen at a time. A tile drawn in indices 0 to 15 therefore appears in any
|
||||
// of sixteen colour schemes without a second copy of it in tile memory, and a
|
||||
// tile that wants all 256 colours simply leaves the nibble at zero and gets
|
||||
// them. One adder in hardware, and neither use costs the other anything.
|
||||
const uint8_t bank = (uint8_t)((attribute & 0x0F) << 4);
|
||||
const uint8_t *art = videoRAM + VIDEO_TILE_BASE + tile * VIDEO_TILE_BYTES;
|
||||
for (int y = 0; y < VIDEO_CELL_PIXELS; y++) {
|
||||
uint8_t *out = pixels + ((row * VIDEO_CELL_PIXELS + y) * width
|
||||
+ column * VIDEO_CELL_PIXELS) * 3;
|
||||
for (int x = 0; x < VIDEO_CELL_PIXELS; x++) {
|
||||
// Wrapping, because a byte plus a byte is a byte. A tile using the
|
||||
// high end of the palette with a nibble set comes round the bottom,
|
||||
// which is what an adder does and what the manual says it does.
|
||||
const uint8_t index = (uint8_t)(art[y * VIDEO_CELL_PIXELS + x] + bank);
|
||||
const uint8_t *entry = videoRAM + VIDEO_PALETTE_BASE
|
||||
+ index * VIDEO_PALETTE_BYTES;
|
||||
*out++ = entry[0];
|
||||
*out++ = entry[1];
|
||||
*out++ = entry[2];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
renderedWidth = width;
|
||||
renderedHeight = rows * VIDEO_CELL_PIXELS;
|
||||
}
|
||||
|
||||
const uint8_t *videoPixels(int *width, int *height) {
|
||||
*width = renderedWidth;
|
||||
*height = renderedHeight;
|
||||
return pixels;
|
||||
}
|
||||
|
||||
// A binary PPM, because it is the smallest format that needs no library to write and no
|
||||
// library to read - which matters when the thing reading it is a test script.
|
||||
int videoWriteImage(const char *path) {
|
||||
videoRender();
|
||||
FILE *file = fopen(path, "wb");
|
||||
if (file == NULL) {
|
||||
fprintf(stderr, "Error: Couldn't write the screen to: %s\n", path);
|
||||
return 1;
|
||||
}
|
||||
fprintf(file, "P6\n%d %d\n255\n", renderedWidth, renderedHeight);
|
||||
size_t bytes = (size_t)renderedWidth * (size_t)renderedHeight * 3;
|
||||
size_t written = fwrite(pixels, 1, bytes, file);
|
||||
fclose(file);
|
||||
if (written != bytes) {
|
||||
fprintf(stderr, "Error: The screen was not written whole to: %s\n", path);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
// video.h
|
||||
// The Voyager's video device.
|
||||
// Written by Anachronaut
|
||||
|
||||
#ifndef VIDEO_H
|
||||
#define VIDEO_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
// ---- What this is ----
|
||||
//
|
||||
// A tile engine. The CPU writes cell indices and the device expands them into pixels, which
|
||||
// is the difference between a screen costing 2,000 bytes a frame and 64,000 - and at a
|
||||
// megahertz that is the difference between a screen and no screen at all.
|
||||
//
|
||||
// It follows that COLOUR DEPTH IS FREE AT FRAME TIME. The map is the same size whether the
|
||||
// tiles behind it are one bit deep or eight, because the depth lives in tile memory, which
|
||||
// is written once when a program loads and not sixty times a second. So the tiles are eight
|
||||
// bits: an 8x8 cell is 64 pixels and each one picks independently out of 256 colours, with
|
||||
// no per-cell limit of the kind that made a Spectrum two and C64 multicolour four.
|
||||
//
|
||||
// ---- The device brings memory ----
|
||||
//
|
||||
// One bank, registered the way the disk's buffer is, so it costs a program nothing in Data
|
||||
// Memory and keeps what is in it between frames. A program blits the region that changed
|
||||
// and the rest stays as it was, which is the whole reason this is a bank rather than a
|
||||
// window onto a port.
|
||||
|
||||
#define VIDEO_MEMORY_BYTES 0x10000
|
||||
|
||||
// Tile memory: 256 tiles of 8x8, one byte a pixel.
|
||||
#define VIDEO_TILE_BASE 0x0000
|
||||
#define VIDEO_TILE_BYTES 64
|
||||
#define VIDEO_TILE_COUNT 256
|
||||
|
||||
// ---- The map, one page a row ----
|
||||
//
|
||||
// A row is padded to exactly 256 bytes whether the mode uses all of it or not, and that is
|
||||
// not waste, it is arithmetic. THE MACHINE HAS NO MULTIPLY. On a 40 column screen every
|
||||
// cursor move would otherwise need row times 40 in software, which is a tax on the most
|
||||
// common operation in the whole system. At a page a row the address needs no arithmetic at
|
||||
// all: the row number IS the high byte and the doubled column IS the low byte.
|
||||
//
|
||||
// It also frees the geometry from having to be a power of two, which is what lets the
|
||||
// pixel resolution be whatever looks right.
|
||||
#define VIDEO_MAP_BASE 0x4000
|
||||
#define VIDEO_MAP_STRIDE 256
|
||||
#define VIDEO_MAP_ROWS 128
|
||||
#define VIDEO_MAP_COLUMNS (VIDEO_MAP_STRIDE / 2)
|
||||
|
||||
// Two bytes to a cell: which tile, and how to colour it.
|
||||
#define VIDEO_CELL_BYTES 2
|
||||
|
||||
// ---- The palette ----
|
||||
//
|
||||
// Four bytes an entry rather than three, for the same reason a map row is a page: entry n
|
||||
// begins at n times four, which is a shift. Three would need a multiply the machine does
|
||||
// not have. The fourth byte is unused and reads as whatever was put there.
|
||||
#define VIDEO_PALETTE_BASE 0xC000
|
||||
#define VIDEO_PALETTE_BYTES 4
|
||||
#define VIDEO_PALETTE_SIZE 256
|
||||
|
||||
// ---- Modes ----
|
||||
//
|
||||
// Both are 8x8 cells over the same engine; only how many of them differ. The pixel count
|
||||
// costs the CPU nothing, because it only ever writes the map - which is why the larger mode
|
||||
// is affordable at all.
|
||||
#define VIDEO_MODE_40x25 0
|
||||
#define VIDEO_MODE_80x50 1
|
||||
#define VIDEO_MODE_COUNT 2
|
||||
|
||||
#define VIDEO_CELL_PIXELS 8
|
||||
#define VIDEO_MAX_WIDTH (80 * VIDEO_CELL_PIXELS)
|
||||
#define VIDEO_MAX_HEIGHT (50 * VIDEO_CELL_PIXELS)
|
||||
|
||||
// ---- Ports ----
|
||||
//
|
||||
// Sixteen, like the controller, and it interrupts on its base the way the disk established.
|
||||
// Nothing interrupts yet; the frame interrupt is the next rung.
|
||||
#define VIDEO_STATUS 0x30
|
||||
#define VIDEO_MODE 0x31
|
||||
#define VIDEO_COLUMNS 0x32
|
||||
#define VIDEO_ROWS 0x33
|
||||
#define VIDEO_SCROLL 0x34
|
||||
|
||||
void videoReset(void);
|
||||
|
||||
uint8_t *videoMemory(uint32_t *capacity);
|
||||
|
||||
uint8_t videoWrite(uint8_t value, uint8_t port);
|
||||
uint8_t videoRead(uint8_t port);
|
||||
|
||||
// Turns what is in video memory into pixels. A pure function of that memory, so the same
|
||||
// contents give the same picture with nobody watching - which is what lets the suite check
|
||||
// a screen on a machine that has no display.
|
||||
void videoRender(void);
|
||||
|
||||
// The pixels the last render produced, three bytes each, red then green then blue.
|
||||
const uint8_t *videoPixels(int *width, int *height);
|
||||
|
||||
// Renders and writes a binary PPM. Returns 0 if it worked.
|
||||
int videoWriteImage(const char *path);
|
||||
|
||||
#endif // VIDEO_H
|
||||
+40
-11
@@ -16,22 +16,22 @@
|
||||
// lets a test suite with no display hold this binary to the same behaviour as the other
|
||||
// one.
|
||||
//
|
||||
// At this stage the window is empty. There is no video device yet, and inventing a
|
||||
// temporary way to draw would mean building something to throw away.
|
||||
// The window shows what the video device produced and decides nothing about it. Render is a
|
||||
// pure function of video memory, so the same program draws the same picture whether or not
|
||||
// anybody is watching - which is what lets a suite with no display check a screen.
|
||||
|
||||
#include "machine.h"
|
||||
#include "video.h"
|
||||
#include "utility.h"
|
||||
#include "raylib.h"
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <getopt.h>
|
||||
|
||||
// The screen the Voyager will have, scaled up because a 320 by 200 window is a postage
|
||||
// stamp on a modern display. Both numbers are provisional until the video device decides
|
||||
// them for real.
|
||||
#define SCREEN_WIDTH 320
|
||||
#define SCREEN_HEIGHT 200
|
||||
#define SCREEN_SCALE 3
|
||||
// The window is the largest screen the video device can produce, scaled up because a 320 by
|
||||
// 200 window is a postage stamp on a modern display. A smaller mode is drawn into the middle
|
||||
// of it rather than resizing the window out from under whoever is looking at it.
|
||||
#define SCREEN_SCALE 2
|
||||
|
||||
// ---- Running without a window ----
|
||||
//
|
||||
@@ -96,9 +96,15 @@ int main(int argc, char *argv[]) {
|
||||
machineRunSlice(&machine);
|
||||
}
|
||||
} else {
|
||||
InitWindow(SCREEN_WIDTH * SCREEN_SCALE, SCREEN_HEIGHT * SCREEN_SCALE,
|
||||
InitWindow(VIDEO_MAX_WIDTH * SCREEN_SCALE, VIDEO_MAX_HEIGHT * SCREEN_SCALE,
|
||||
"Segan Voyager");
|
||||
SetTargetFPS(60);
|
||||
// One texture, updated in place. Making a new one every frame would be a new
|
||||
// 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);
|
||||
UnloadImage(blank);
|
||||
// ---- A slice a frame ----
|
||||
//
|
||||
// The machine gets its turn, then the window gets its turn. Closing the window
|
||||
@@ -109,12 +115,35 @@ int main(int argc, char *argv[]) {
|
||||
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. A screen with nothing driving it should look like a screen that
|
||||
// is on, rather than like a window that failed to open.
|
||||
// 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();
|
||||
}
|
||||
UnloadTexture(screen);
|
||||
CloseWindow();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user