HALT is terminal - stepCPU returns at once when the Halt Flag is up, so a halted machine does not execute, service devices, or take an interrupt - and that has to stay true, because every test ends with a halt and "halted" is how a program says it has finished. The consequence was that SplitBit had no way to wait at all. Every wait was a spin, and a spin is bus traffic: 11.5% of Type over a 14K file on a disk of ten thousand cycles, after read-ahead had already hidden three quarters of the latency. WAIT is 0xFE, one byte, no operands, sitting under HALT where the instruction that almost stops the machine belongs. Three decisions in it: - A line already standing means there is nothing to wait for, so WAIT does nothing. That is what makes test-then-wait race-free. - Any line ends the wait, masked or not, so a program can sleep on a device it has no handler for and read its status afterwards. Masking says who answers a request, not whether it happened. - A line that wakes the CPU without being dispatched is taken down by the WAIT. Left standing it would be found by the next WAIT, which would return at once - the program would spin exactly as before while looking as though it slept. Waiting is NOT a Status bit, and that is the trap avoided rather than a gap: Status rides into the interrupt frame and comes back out, so a machine interrupted mid-wait would return from its handler still waiting, and wait again for what it had already been given. An internal field instead. Idle cycles are counted apart from bus cycles and the halt line says so when there are any, which is what makes the difference observable at all - with the line-clearing removed the total moves by ONE cycle, 20,100 against 20,099, and only the idle half changes, halving to 9,976. A test on totals could never have seen it. Tests/terminal.sh asks that question, being the file for things a recorded output cannot see, and fails with the clear removed while "both reads finished" still passes. Three collisions, all found by building it: - 0xFE was the assembler's "not an instruction" sentinel. getOpcode now answers a negative NOT_AN_OPCODE, which is outside the range of every possible answer instead of inside the unused part of it. - 0xFE was also what faultTest and faultResumeTest executed to provoke a fault. They now use 0xFD and say why, because they did not fail when it became an instruction - they HUNG, having started sleeping instead. - Keys.asm has had a label called "wait" for a year, and mnemonics are matched uppercased. What that reported was "Branch without label" at the BRQ thirty lines away. The assembler now refuses a label that is already an instruction, at the label, by name; every instruction added takes a word out of the space of label names, so this will happen again.
590 lines
31 KiB
Bash
Executable File
590 lines
31 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# Checks the manuals against the code, and the repository against its own rules.
|
|
#
|
|
# Documentation goes stale quietly. An instruction added without a table row, or a count
|
|
# in a heading that nobody updated, is wrong in a way nothing notices until somebody
|
|
# trusts it. Everything here is a claim the manuals make that can be settled by looking
|
|
# at the source, so it is settled every time the tests run.
|
|
#
|
|
# Written by Anachronaut
|
|
|
|
set -u
|
|
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
|
|
cd "$ROOT" || exit 1
|
|
|
|
python3 - <<'PY'
|
|
import re
|
|
import sys
|
|
|
|
problems = []
|
|
|
|
|
|
def read(path):
|
|
return open(path).read()
|
|
|
|
|
|
pm = read("SplitBit Programming Manual.md")
|
|
am = read("SplitBit Assembler Manual.md")
|
|
# The third manual. CosmOS is a system that runs ON SplitBit rather than part of it, so
|
|
# what it offers a program is documented with it and checked here alongside the other two.
|
|
cr = read("Programs/CosmOS/README.md")
|
|
asmc = read("Source/Assembler/assembly.c")
|
|
util = read("Source/Assembler/Assm-util.c")
|
|
|
|
# ---- Every tracked file is plain ASCII ----
|
|
#
|
|
# A standing rule of this repository, and nothing enforced it, so it drifted: 39 em dashes
|
|
# and an ellipsis had collected in the two manuals, all of them typed by something that
|
|
# helpfully substituted a nicer character.
|
|
#
|
|
# GIT LS-FILES IS READ NUL SEPARATED, and that is not fussiness. The obvious shell version
|
|
# of this check - looping over $(git ls-files) - splits on whitespace, so it looked for a
|
|
# file called "SplitBit" and reported the repository clean while both manuals had drifted.
|
|
# A check that cannot see the files with spaces in their names is worse than no check.
|
|
import subprocess
|
|
|
|
tracked = subprocess.run(["git", "ls-files", "-z"], capture_output=True).stdout
|
|
for name in tracked.split(b"\0"):
|
|
if not name:
|
|
continue
|
|
path = name.decode()
|
|
try:
|
|
text = open(path, encoding="utf-8").read()
|
|
except (UnicodeDecodeError, OSError):
|
|
continue
|
|
for number, line in enumerate(text.split("\n"), 1):
|
|
odd = sorted({c for c in line if ord(c) > 127})
|
|
if odd:
|
|
problems.append("%s line %d is not plain ASCII: %s"
|
|
% (path, number, ", ".join("%r (U+%04X)" % (c, ord(c)) for c in odd)))
|
|
break
|
|
|
|
# ---- Every link in the documents goes somewhere ----
|
|
#
|
|
# A link that does not resolve is the same kind of wrong as a stale claim: it looks like
|
|
# information and is not, and nobody notices until a stranger clicks it. The manuals have
|
|
# SPACES IN THEIR NAMES, so a link to one carries %20 and has to be unquoted before it can
|
|
# be looked for - which is the sort of thing that would otherwise be got wrong once and
|
|
# then reported as fine.
|
|
import os
|
|
import urllib.parse
|
|
|
|
for name in tracked.split(b"\0"):
|
|
if not name or not name.endswith(b".md"):
|
|
continue
|
|
path = name.decode()
|
|
here = os.path.dirname(path)
|
|
for match in re.finditer(r"\[[^\]]*\]\(([^)]+)\)", read(path)):
|
|
target = match.group(1)
|
|
if target.startswith(("http://", "https://", "#", "mailto:")):
|
|
continue
|
|
# A raw space ends the link early in most renderers, so the file existing is not
|
|
# enough - the manuals have spaces in their names and must carry %20.
|
|
if " " in target:
|
|
problems.append("%s links to \"%s\", which has a space in it: most renderers"
|
|
" stop at the space. Write it as %%20."
|
|
% (path, target))
|
|
continue
|
|
wanted = urllib.parse.unquote(target.split("#")[0])
|
|
if not os.path.exists(os.path.normpath(os.path.join(here, wanted))):
|
|
problems.append("%s links to %s, and there is nothing there" % (path, target))
|
|
|
|
# ---- Every instruction has a row, and every row is an instruction ----
|
|
#
|
|
# A mnemonic begins with a letter, which is what keeps the offset and size columns of the
|
|
# other tables in these manuals out of it.
|
|
documented = {(int(m.group(1), 16), m.group(2))
|
|
for m in re.finditer(r'^\|\s*([0-9A-F]{2})\s*\|\s*([A-Z][A-Z0-9]*)\s*\|', pm, re.M)}
|
|
implemented = {(int(m.group(1), 16), m.group(2))
|
|
for m in re.finditer(r'\{0x([0-9A-Fa-f]{2}),\s*"([A-Z0-9]+)"\}', asmc)}
|
|
for opcode, name in sorted(implemented - documented):
|
|
problems.append("%s (0x%02X) is implemented and not in the manual" % (name, opcode))
|
|
for opcode, name in sorted(documented - implemented):
|
|
problems.append("%s (0x%02X) is in the manual and not implemented" % (name, opcode))
|
|
|
|
# ---- The counts in the group headings ----
|
|
body = asmc[asmc.index("Instruction instruction_set[]"):asmc.index("int num_instructions")]
|
|
actual = {}
|
|
group = None
|
|
for line in body.split("\n"):
|
|
heading = re.match(r'\s*// (.+?) Operations:', line)
|
|
if heading:
|
|
group = heading.group(1)
|
|
actual.setdefault(group, 0)
|
|
if re.search(r'\{0x[0-9A-Fa-f]{2},', line) and group:
|
|
actual[group] += 1
|
|
|
|
for m in re.finditer(r'^### (.+?) Operations: (\d+) Instructions?$', pm, re.M):
|
|
name, claimed = m.group(1), int(m.group(2))
|
|
# The manual's headings are wordier than the source's comments, so match on the start.
|
|
match = [v for k, v in actual.items() if name.startswith(k)]
|
|
if not match:
|
|
problems.append("the manual has a group called \"%s\" that the source does not" % name)
|
|
elif match[0] != claimed:
|
|
problems.append("the manual says %s has %d instructions, and it has %d"
|
|
% (name, claimed, match[0]))
|
|
|
|
# ---- How many instructions carry a Data Pointer selector ----
|
|
#
|
|
# The manual says this as a word rather than a figure, and it is the sort of number that
|
|
# goes stale quietly: adding an instruction that takes a selector leaves the sentence
|
|
# looking perfectly reasonable and wrong. dataPointerOperands is the list, so it is the
|
|
# one to believe.
|
|
# Past twenty the number is two words, the way this manual writes every other one, so the
|
|
# pattern has to allow a second - and the count going past twenty is exactly the sort of
|
|
# thing that would otherwise turn "the manual is wrong" into "the manual has stopped
|
|
# saying it", which reads as a different kind of problem.
|
|
words = {12: "Twelve", 13: "Thirteen", 14: "Fourteen", 15: "Fifteen", 16: "Sixteen",
|
|
17: "Seventeen", 18: "Eighteen", 19: "Nineteen", 20: "Twenty",
|
|
21: "Twenty one", 22: "Twenty two", 23: "Twenty three", 24: "Twenty four",
|
|
25: "Twenty five", 26: "Twenty six"}
|
|
selectors = asmc[asmc.index("int dataPointerOperands"):asmc.index("int getOpcode")]
|
|
taking = len(re.findall(r'^\s*case 0x[0-9A-Fa-f]{2}:', selectors, re.M))
|
|
said = re.search(r'^([A-Z][a-z]+(?: [a-z]+)?) instructions work through a Data Pointer\.',
|
|
pm, re.M)
|
|
if not said:
|
|
problems.append("the manual no longer says how many instructions take a Data Pointer")
|
|
elif said.group(1) != words.get(taking):
|
|
problems.append("the manual says %s instructions work through a Data Pointer, and %d do"
|
|
% (said.group(1).lower(), taking))
|
|
|
|
# ---- Every device class in the header has a row in the Devices table ----
|
|
#
|
|
# The table says which ports a device answers on and what class it reports. Adding a
|
|
# device, or widening one from a single port to a block, leaves the table looking perfectly
|
|
# reasonable and describing a machine that no longer exists. The classes are the part that
|
|
# can be checked against the source without teaching this script how ports are laid out:
|
|
# every class the header defines except DEVICE_NONE is something a program can find on the
|
|
# bus, so every one of them has to be findable in the manual too.
|
|
ioh = read("Source/Emulator/io.h")
|
|
classes = {name: int(value, 16)
|
|
for name, value in re.findall(r'^#define (DEVICE_[A-Z_]+)\s+(0x[0-9A-Fa-f]{2})$',
|
|
ioh, re.M)
|
|
if name not in ("DEVICE_NONE",)}
|
|
if "## Devices:" not in pm:
|
|
problems.append("the Programming Manual has lost its Devices table")
|
|
else:
|
|
table = pm.split("## Devices:")[1].split("\n## ")[0]
|
|
listed = {int(m, 16) for m in re.findall(r'\|\s*(0x[0-9A-Fa-f]{2})\s*\|\s*$', table, re.M)}
|
|
for name, value in sorted(classes.items(), key=lambda pair: pair[1]):
|
|
if value not in listed:
|
|
problems.append("%s (0x%02X) is a device class and has no row in the Devices"
|
|
" table" % (name, value))
|
|
|
|
# ---- The vector ranges the manuals quote are the ones the assembler uses ----
|
|
#
|
|
# Both manuals print the boundary between numbers a program may pin and numbers the
|
|
# assembler hands out. Those are two constants in one header, and moving them without
|
|
# touching the manuals would leave every programmer reading a range that no longer exists
|
|
# and being refused a number the manual said was theirs.
|
|
header = read("Source/Assembler/assembly.h")
|
|
ranges = {name: int(value)
|
|
for name, value in re.findall(r'^#define (VECTOR_FIRST_[A-Z]+)\s+(\d+)$',
|
|
header, re.M)}
|
|
if set(ranges) != {"VECTOR_FIRST_PINNED", "VECTOR_FIRST_AUTO"}:
|
|
problems.append("the vector range constants are not the two this check knows about: %s"
|
|
% ", ".join(sorted(ranges)) if ranges else "none found")
|
|
else:
|
|
pinnedFrom = ranges["VECTOR_FIRST_PINNED"]
|
|
autoFrom = ranges["VECTOR_FIRST_AUTO"]
|
|
said = "%d to %d" % (pinnedFrom, autoFrom - 1)
|
|
for manual, text in [("Programming Manual", pm), ("Assembler Manual", am)]:
|
|
if said not in text:
|
|
problems.append("the %s does not say the pinned vectors are %s"
|
|
% (manual, said))
|
|
if "%d and up" % autoFrom not in pm:
|
|
problems.append("the Programming Manual does not say the automatic vectors start"
|
|
" at %d" % autoFrom)
|
|
if "from vector %d upwards" % autoFrom not in am:
|
|
problems.append("the Assembler Manual does not say the automatic vectors start"
|
|
" at %d" % autoFrom)
|
|
|
|
# ---- The loadable header table matches the offsets the assembler writes ----
|
|
#
|
|
# The Programming Manual prints the header field by field, which is the description two
|
|
# implementations work from. sbex.h is where the offsets actually are, so a field moved
|
|
# there and not here would leave the manual describing a format nobody writes.
|
|
sbex = read("Source/Assembler/sbex.h")
|
|
offsets = {name: int(value)
|
|
for name, value in re.findall(r'^#define (SBEX_[A-Z_]+_AT)\s+(\d+)$', sbex, re.M)}
|
|
if "## Loading A Program From A Disk:" not in am:
|
|
problems.append("the Assembler Manual has lost its loadable program section")
|
|
else:
|
|
loading = am.split("## Loading A Program From A Disk:")[1].split("\n## ")[0]
|
|
listed = [int(m) for m in re.findall(r'^\| (\d+) \| \d* \|', loading, re.M)]
|
|
for name, offset in sorted(offsets.items(), key=lambda pair: pair[1]):
|
|
if offset not in listed:
|
|
problems.append("%s is at offset %d and the header table has no row for it"
|
|
% (name, offset))
|
|
# Spelled as a word, the way these manuals write small numbers in prose.
|
|
asWord = {1: "one", 2: "two", 3: "three", 4: "four", 5: "five"}
|
|
for version in ("SBEX_VERSION", "SBEX_VERSION_VECTORS"):
|
|
number = re.search(r'^#define %s\s+(\d+)$' % version, sbex, re.M)
|
|
if not number:
|
|
problems.append("%s is gone from sbex.h" % version)
|
|
continue
|
|
said = asWord.get(int(number.group(1)))
|
|
if said is None or said not in loading.lower():
|
|
problems.append("the loadable program section does not mention version %s (%s)"
|
|
% (number.group(1), said))
|
|
|
|
# ---- Every console status bit is described ----
|
|
#
|
|
# The status port is read by writing a mask and testing it, so a program can only use a bit
|
|
# it has been told the number of. Adding one and forgetting to write it down leaves a bit
|
|
# that works and that nobody can discover. The section names them as "bit N", so that is
|
|
# what is looked for.
|
|
status = {name: int(value, 16)
|
|
for name, value in re.findall(r'^#define (CONSOLE_STATUS_[A-Z]+)\s+(0x[0-9A-Fa-f]{2})$',
|
|
ioh, re.M)}
|
|
if "## The Console:" not in pm:
|
|
problems.append("the Programming Manual has lost its \"The Console\" section")
|
|
else:
|
|
console = pm.split("## The Console:")[1].split("\n## ")[0]
|
|
for name, value in sorted(status.items(), key=lambda pair: pair[1]):
|
|
bit = value.bit_length() - 1
|
|
if "bit %d" % bit not in console:
|
|
problems.append("%s is bit %d of the console status port and The Console does"
|
|
" not mention it" % (name, bit))
|
|
|
|
# ---- Every service the system offers has a row ----
|
|
#
|
|
# services.asm is the one place the numbers are written, and both the system and every
|
|
# program include it. A service added there and not here is one nothing can find out about
|
|
# except by reading the source of the operating system.
|
|
# DECLARING A SERVICE AND IMPLEMENTING ONE ARE DIFFERENT THINGS, and the manual should
|
|
# describe the second. services.asm names them and fixes their numbers, which is what lets a
|
|
# number be pinned before anything answers to it; cosmos.asm is where a name gets a handler.
|
|
# A row for a service nothing implements would be describing a call that faults, and a
|
|
# missing row for one that works is a service nobody can find out about.
|
|
services = read("Programs/CosmOS/Source/services.asm")
|
|
named = set(re.findall(r'^\s{2}(os[A-Za-z]+)\s+0d\d+', services, re.M))
|
|
system = read("Programs/CosmOS/Source/cosmos.asm")
|
|
vectors = system.split("#Vectors")[-1] if "#Vectors" in system else ""
|
|
implemented = {name for name in re.findall(r'^\s{2}(os[A-Za-z]+)\s+[a-zA-Z]', vectors, re.M)
|
|
if name in named}
|
|
if not named:
|
|
problems.append("no services could be found in services.asm")
|
|
elif "## What A Program May Ask The System For:" not in cr:
|
|
problems.append("the CosmOS README has lost its services section")
|
|
else:
|
|
section = cr.split("## What A Program May Ask The System For:")[1].split("\n## ")[0]
|
|
documented = set(re.findall(r'^\| (os[A-Za-z]+) \|', section, re.M))
|
|
for name in sorted(implemented - documented):
|
|
problems.append("%s is a service the system implements and has no row in the"
|
|
" services table" % name)
|
|
for name in sorted(documented - implemented):
|
|
problems.append("the services table describes %s, which nothing implements: calling"
|
|
" it would dispatch through an empty vector and fault" % name)
|
|
|
|
# ---- Every program the manual describes is really there ----
|
|
#
|
|
# The table names what the shell can load. A program renamed or removed leaves a row
|
|
# describing something nobody can run, which is the same kind of quiet wrongness as a
|
|
# routine that no longer exists. The other direction is deliberately not checked: the ported
|
|
# programs are covered in the prose rather than given a row each.
|
|
import os
|
|
if "## Included Applications:" not in cr:
|
|
problems.append("the CosmOS README has lost its list of applications")
|
|
else:
|
|
listed = cr.split("## Included Applications:")[1].split("\n### ")[0]
|
|
# After the separator, so the table's own heading row is not mistaken for a program.
|
|
listed = listed.split("| --- |")[-1]
|
|
for name in re.findall(r'^\| ([A-Z][A-Za-z0-9-]*) \|', listed, re.M):
|
|
if not os.path.exists("Programs/CosmOS/Apps/%s.asm" % name):
|
|
problems.append("the CosmOS README describes an application called %s, and"
|
|
" there is no Programs/CosmOS/Apps/%s.asm" % (name, name))
|
|
|
|
# ---- The monitor's instruction table is the assembler's ----
|
|
#
|
|
# The monitor disassembles, so it needs the same 64 instructions with the same names and the
|
|
# same lengths. A disassembler that disagreed about a length would not print one line wrong,
|
|
# it would lose its place and print everything after it wrong, which is the worst way for a
|
|
# tool like that to fail: confidently. So the table is generated from assembly.c by
|
|
# Tests/instructiontable.py, and what is in the monitor is checked against it here.
|
|
import subprocess
|
|
generated = subprocess.run([sys.executable, "Tests/instructiontable.py"],
|
|
capture_output=True, text=True)
|
|
if generated.returncode != 0:
|
|
problems.append("the instruction table generator would not run")
|
|
else:
|
|
wanted = [line.rstrip() for line in generated.stdout.splitlines() if line.strip()]
|
|
# TWO copies now, and both are checked. The monitor has one and the assembler that
|
|
# runs on the machine has another, because they are separate programs and there is no
|
|
# linker to let them share: the monitor's lives in the system's data at an address
|
|
# that moves every rebuild. Duplication is the cost of having no libraries, and a
|
|
# check on every copy is what keeps the cost to bytes rather than to correctness.
|
|
copies = [("the system", "Programs/CosmOS/Source/cosmos.asm", "\nInstructions:\n"),
|
|
("the native assembler", "Programs/CosmOS/Assembler/table.asm",
|
|
"\nAsmInstructions:\n")]
|
|
for who, path, marker in copies:
|
|
text = read(path)
|
|
if marker not in text:
|
|
problems.append("%s has lost its instruction table" % who)
|
|
continue
|
|
block = text.split(marker)[1]
|
|
have = []
|
|
for line in block.splitlines():
|
|
if not line.strip() or not line.startswith(" 0x"):
|
|
break
|
|
have.append(line.rstrip())
|
|
if have != wanted:
|
|
problems.append("%s's instruction table is not what the assembler's"
|
|
" instruction set generates: %d entries against %d, first"
|
|
" difference at %s"
|
|
% (who, len(have), len(wanted),
|
|
next((a or b for a, b in zip(have + [None] * len(wanted),
|
|
wanted + [None] * len(have))
|
|
if a != b), "the end")))
|
|
|
|
# ---- And the lengths that table implies are the ones the manual prints ----
|
|
#
|
|
# The generator works out how long each instruction is from rules written in it; the manual
|
|
# says so in a column somebody typed. They are independent accounts of the same fact, which
|
|
# is exactly the pair worth checking against each other.
|
|
sys.path.insert(0, "Tests")
|
|
import instructiontable
|
|
lengthOf = {0: 1, 1: 3, 2: 2, 3: 2, 4: 3, 5: 4, 6: 3}
|
|
printed = {}
|
|
for m in re.finditer(r'^\|\s*[0-9A-F]{2}\s*\|\s*([A-Z][A-Z0-9]*)\s*\|\s*(\d+)\s*\|', pm, re.M):
|
|
printed[m.group(1)] = int(m.group(2))
|
|
for opcode, name in instructiontable.table():
|
|
implied = lengthOf[instructiontable.shapeOf(opcode)]
|
|
if name in printed and printed[name] != implied:
|
|
problems.append("the manual says %s is %d bytes and the disassembler will read it"
|
|
" as %d" % (name, printed[name], implied))
|
|
|
|
# ---- Every directive the assembler knows is written down ----
|
|
for directive in sorted(set(re.findall(r'"(#[A-Za-z]+)"', util))):
|
|
if directive not in am:
|
|
problems.append("%s is a directive and is not in the Assembler Manual" % directive)
|
|
|
|
# ---- Every routine the manual promises exists ----
|
|
#
|
|
# The first column of the table in each of these sections names something the library has
|
|
# to define. A routine renamed in the source and not in the manual is caught here, which
|
|
# is what keeps the tables a description rather than a memory.
|
|
for heading, library in [("## The Filesystem Library:", "Programs/CosmOS/Source/sbfs.asm"),
|
|
("## The Console Library:", "Programs/CosmOS/Source/console.asm")]:
|
|
if heading not in cr:
|
|
problems.append("the CosmOS README has lost its \"%s\" section"
|
|
% heading.strip("# :"))
|
|
continue
|
|
section = cr.split(heading)[1].split("\n## ")[0]
|
|
defined = set(re.findall(r'^([a-zA-Z][A-Za-z0-9]*):', read(library), re.M))
|
|
for name in re.findall(r'^\| ([a-z][A-Za-z0-9]*) \|', section, re.M):
|
|
if name not in defined:
|
|
problems.append("the manual lists %s, which %s does not define" % (name, library))
|
|
|
|
# ---- The worked example still assembles to the bytes the manual prints ----
|
|
#
|
|
# The hello world program and the hex dump beside it are two claims about the same thing,
|
|
# and nothing but this keeps them agreeing.
|
|
import os
|
|
import subprocess
|
|
import tempfile
|
|
|
|
# BOTH ANCHORS ARE CHECKED BEFORE THEY ARE USED. Every other heading this script splits on
|
|
# says what it could not find; these two were the exception, and a rename here produced an
|
|
# IndexError and a traceback instead of a sentence. That is a worse answer than a stale
|
|
# manual, because whoever reads it learns nothing about which heading moved.
|
|
# THE TWO HALVES ARE IN DIFFERENT MANUALS NOW, and that makes this a better check than it
|
|
# was. The program belongs with the machine, where it arrives just after the instruction
|
|
# list; the hex dump belongs with the boot image format it is an example of, which is the
|
|
# assembler's business. So this settles three things against each other at once: what the
|
|
# Programming Manual prints, what the Assembler Manual prints, and what the assembler does.
|
|
exampleAnchor = "### Example Program: Hello World"
|
|
dumpAnchor = "assembled and dumped as hex:"
|
|
if exampleAnchor not in pm:
|
|
problems.append("the Programming Manual has lost its \"Example Program: Hello World\""
|
|
" heading, so the worked example cannot be found")
|
|
elif dumpAnchor not in am:
|
|
problems.append("the Assembler Manual no longer says \"%s\" before the hex dump, so"
|
|
" there is nothing to compare the worked example against" % dumpAnchor)
|
|
else:
|
|
source = pm.split(exampleAnchor)[1].split("```")[1]
|
|
claimed = am.split(dumpAnchor)[1].split("```")[1].split()
|
|
with tempfile.TemporaryDirectory() as work:
|
|
asm = os.path.join(work, "hello.asm")
|
|
binary = os.path.join(work, "hello.bin")
|
|
open(asm, "w").write(source)
|
|
built = subprocess.run(["./Assembler", asm, "-o", binary], capture_output=True)
|
|
if built.returncode != 0:
|
|
problems.append("the hello world program in the manual no longer assembles")
|
|
else:
|
|
actual = ["%02x" % b for b in open(binary, "rb").read()]
|
|
if [c.lower() for c in claimed] != actual:
|
|
problems.append("the hex dump in the manual is not what that program"
|
|
" assembles to now: it prints %d bytes and the assembler"
|
|
" makes %d" % (len(claimed), len(actual)))
|
|
|
|
# ---- The Assembler Manual's worked programs still assemble ----
|
|
for heading in ["## An Example SplitBit Assembly Program:",
|
|
"## An Example Using More Than One Data Pointer:",
|
|
"## An Example Using Interrupts:"]:
|
|
if heading not in am:
|
|
problems.append("the Assembler Manual has lost its \"%s\" section" % heading.strip("# :"))
|
|
continue
|
|
example = am.split(heading)[1].split("```")[1]
|
|
with tempfile.TemporaryDirectory() as work:
|
|
asm = os.path.join(work, "example.asm")
|
|
open(asm, "w").write(example)
|
|
built = subprocess.run(["./Assembler", "-I", "Programs/Libraries",
|
|
"-I", "Programs/CosmOS/Source", asm,
|
|
"-o", os.path.join(work, "example.bin")],
|
|
capture_output=True)
|
|
if built.returncode != 0:
|
|
problems.append("the example under \"%s\" no longer assembles"
|
|
% heading.strip("# :"))
|
|
|
|
# ---- No manual still says the filesystem is flat ----
|
|
#
|
|
# It was flat, and every manual said so in passing, and those sentences went on being true
|
|
# for as long as nobody looked. SBFS grew directories over five separate pieces of work and
|
|
# the last thing left claiming otherwise was one line in the Assembler Manual explaining
|
|
# why an include had nowhere else to look.
|
|
#
|
|
# A phrase rather than a section, because that is the shape this kind of staleness takes:
|
|
# not a chapter that is wrong, one clause inside a paragraph that is otherwise right.
|
|
for name, text in (("the Programming Manual", pm), ("the Assembler Manual", am),
|
|
("the README", open("README.md").read()),
|
|
("the CosmOS README", open("Programs/CosmOS/README.md").read())):
|
|
for claim in ("SBFS is flat", "the filesystem is flat", "flat filesystem",
|
|
"SBFS has no directories"):
|
|
if claim.lower() in text.lower():
|
|
problems.append("%s still says \"%s\", and it has not been true since"
|
|
" directories arrived" % (name, claim))
|
|
|
|
# ---- CosmOS fits in the half of the machine it says it does ----
|
|
#
|
|
# The memory map in the CosmOS README is a CONVENTION. Nothing in the assembler, the
|
|
# loader or the machine enforces it: an application says where it goes with #Base, and
|
|
# CosmOS puts it there. So when CosmOS grew past the address applications are based at,
|
|
# nothing said so - the next program loaded simply landed on top of the shell's own code,
|
|
# and what broke was whichever part of the shell that program happened to cover, at
|
|
# whatever later moment somebody used it.
|
|
#
|
|
# That is why this reads the numbers out of the table rather than being told them: the
|
|
# table is the specification, and a table nothing checks is the thing that goes stale.
|
|
readme = open("Programs/CosmOS/README.md").read()
|
|
row = re.compile(r"\|\s*(Program|Data) Memory\s*\|\s*`0x0000` through `0x([0-9A-Fa-f]{4})`"
|
|
r"\s*\|\s*`0x([0-9A-Fa-f]{4})` and above\s*\|")
|
|
table = {kind: (int(top, 16) + 1, int(base, 16)) for kind, top, base in row.findall(readme)}
|
|
limits = {kind: room for kind, (room, base) in table.items()}
|
|
bases = {kind: base for kind, (room, base) in table.items()}
|
|
if len(table) != 2:
|
|
problems.append("the CosmOS README no longer states a memory map this can check")
|
|
else:
|
|
built = subprocess.run(["./Assembler", "-I", "Programs/CosmOS/Source",
|
|
"Programs/CosmOS/Source/cosmos.asm", "-o", os.devnull],
|
|
capture_output=True, text=True)
|
|
sizes = dict(re.findall(r"(Program|Data) Segment size: (\d+)", built.stdout))
|
|
if len(sizes) != 2:
|
|
problems.append("could not measure the CosmOS segments")
|
|
else:
|
|
for kind in ("Program", "Data"):
|
|
used, room = int(sizes[kind]), limits[kind]
|
|
if used > room:
|
|
problems.append(
|
|
"CosmOS's %s Segment is %d bytes and the map gives it %d - it now"
|
|
" reaches into the %d bytes an application is loaded at, and loading"
|
|
" one will overwrite it" % (kind, used, room, used - room))
|
|
|
|
# ---- And the table against itself ----
|
|
#
|
|
# The check above reads the CosmOS column and never the one beside it, so it passed a
|
|
# Data row that gave the system 0x0000-0x3FFF and an application 0x2000 and above -
|
|
# two halves of one sentence contradicting each other in print. A number checked
|
|
# against the code and not against the number next to it is still unchecked.
|
|
for kind in ("Program", "Data"):
|
|
room, base = table[kind]
|
|
if base != room:
|
|
problems.append(
|
|
"the CosmOS README gives the system %s Memory up to 0x%04X and puts an"
|
|
" application at 0x%04X - the two columns of that row disagree"
|
|
% (kind, room - 1, base))
|
|
|
|
# ---- And the example under it ----
|
|
#
|
|
# The minimal application in the same section is what somebody copies, so it is the
|
|
# part of the map most worth being right. It went stale across the doubling while the
|
|
# table above it was corrected.
|
|
example = re.search(r"```asm\n(.*?)```", readme, re.S)
|
|
if not example:
|
|
problems.append("the CosmOS README no longer shows a minimal application")
|
|
else:
|
|
shown = dict(zip(("Program", "Data"),
|
|
re.findall(r"#Base 0x([0-9A-Fa-f]{4})", example.group(1))))
|
|
for kind in ("Program", "Data"):
|
|
if kind not in shown:
|
|
problems.append("the minimal CosmOS application shows no %s #Base" % kind)
|
|
elif int(shown[kind], 16) != bases[kind]:
|
|
problems.append(
|
|
"the minimal CosmOS application is based at 0x%s in %s Memory and the"
|
|
" map above it says 0x%04X" % (shown[kind].upper(), kind, bases[kind]))
|
|
|
|
# ---- And the copy in the source ----
|
|
#
|
|
# cosmos.asm opens with the same map in its own words, because somebody reading the
|
|
# system reads that before they read the README. Three copies of one fact, so all
|
|
# three are compared.
|
|
header = open("Programs/CosmOS/Source/cosmos.asm").read()[:4096]
|
|
stated = dict(re.findall(
|
|
r"(Program|Data) Memory\s+0x0000 - 0x([0-9A-Fa-f]{4})\s+the system", header))
|
|
for kind in ("Program", "Data"):
|
|
if kind not in stated:
|
|
problems.append("cosmos.asm no longer opens with a %s Memory map" % kind)
|
|
elif int(stated[kind], 16) + 1 != limits[kind]:
|
|
problems.append(
|
|
"cosmos.asm says the system keeps below 0x%s in %s Memory and the CosmOS"
|
|
" README says 0x%04X" % (stated[kind].upper(), kind, limits[kind] - 1))
|
|
|
|
# ---- The assembler's scratch map sits above what it says it sits above ----
|
|
#
|
|
# scratch.asm is a MAP rather than a set of declarations: the buffers are not reserved,
|
|
# they are addresses written in a comment, because reserving them would put 22K of zeroes
|
|
# in the file and the assembler could not load itself. Nothing enforces a word of it.
|
|
#
|
|
# So it carries two claims about the machine around it, and both have gone stale once. It
|
|
# said the system keeps below 0x1000 for a while after the system's half of Data Memory
|
|
# was doubled - twelve lines above the paragraph explaining the doubling. And the map
|
|
# starts at 0x4000 on the grounds that the assembler's own data ends well before there,
|
|
# which was measured on the day and is not measured again by anything.
|
|
scratch = open("Programs/CosmOS/Assembler/scratch.asm").read()
|
|
floor = re.search(r"the system keeps\s*;?\s*below 0x([0-9A-Fa-f]{4})", scratch)
|
|
if not floor:
|
|
problems.append("scratch.asm no longer says what the system keeps below")
|
|
elif "Data" in limits and int(floor.group(1), 16) + 1 != limits["Data"]:
|
|
problems.append(
|
|
"the assembler's scratch map says the system keeps below 0x%s and the CosmOS"
|
|
" README says 0x%04X" % (floor.group(1).upper(), limits["Data"] - 1))
|
|
|
|
first = re.search(r"^;\s+0x([0-9A-Fa-f]{4})\s+\d+\s+the label index", scratch, re.M)
|
|
if not first:
|
|
problems.append("scratch.asm no longer states where its buffers begin")
|
|
else:
|
|
built = subprocess.run(["./Assembler", "-I", "Programs/CosmOS/Source",
|
|
"-I", "Programs/CosmOS/Assembler",
|
|
"Programs/CosmOS/Assembler/Asm.asm", "-o", os.devnull],
|
|
capture_output=True, text=True)
|
|
# A loadable program reports "Data: N bytes at 0xAAAA"; a boot image says it another
|
|
# way. The assembler is the former, and the address matters as much as the size.
|
|
said = re.search(r"Data:\s*(\d+) bytes at 0x([0-9A-Fa-f]{4})", built.stdout)
|
|
if not said:
|
|
problems.append("could not measure the native assembler's data")
|
|
else:
|
|
ends = int(said.group(2), 16) + int(said.group(1))
|
|
if ends > int(first.group(1), 16):
|
|
problems.append(
|
|
"the native assembler's data reaches 0x%04X and its scratch map begins at"
|
|
" 0x%s - the buffers are on top of the variables"
|
|
% (ends - 1, first.group(1).upper()))
|
|
|
|
if problems:
|
|
print("The manuals and the code disagree:")
|
|
for p in problems:
|
|
print(" " + p)
|
|
sys.exit(1)
|
|
print("The manuals agree with the code.")
|
|
PY
|