// Screens you write to instead of wiring every pixel: the terminal (text), // the monitor (one bit a pixel, like an OLED) and the RGB monitor (eight // colours). Each has a size you set (characters, or pixels), and its pins // follow it: a monitor's X and Y address bits are as many as its width and // height need. A rising WR writes, a rising CLR clears. import type { CircuitNode, NodeKind } from './types' export type DisplayKind = 'terminal' | 'monitor' | 'rgbmonitor' export const DISPLAY_KINDS: NodeKind[] = ['terminal', 'monitor', 'rgbmonitor'] export const isDisplay = (k: NodeKind): k is DisplayKind => DISPLAY_KINDS.includes(k) /** Sizes: characters for the terminal, pixels for the monitors. */ export const DISPLAY_SIZES: Record = { terminal: { min: [4, 1], max: [120, 60], start: [32, 10], unit: 'characters' }, monitor: { min: [2, 2], max: [128, 128], start: [32, 32], unit: 'pixels' }, rgbmonitor: { min: [2, 2], max: [128, 128], start: [32, 24], unit: 'pixels' }, } /** Grid units a character (terminal) or a pixel (monitors) takes on the box. */ export const TERM_CHAR = 6.6 / 16 export const TERM_LINE = 1 export const PIXEL = 0.5 export function clampSize(kind: DisplayKind, w: number, h: number): [number, number] { const { min, max } = DISPLAY_SIZES[kind] const c = (v: number, i: 0 | 1) => Math.max(min[i], Math.min(max[i], Math.round(v) || min[i])) return [c(w, 0), c(h, 1)] } export function displaySize(n: CircuitNode): [number, number] { const kind = n.kind as DisplayKind return n.dims ? clampSize(kind, n.dims[0], n.dims[1]) : DISPLAY_SIZES[kind].start } /** Address bits for a count of columns or rows. */ export const addrBits = (n: number) => Math.max(1, Math.ceil(Math.log2(n))) /** The input pins, top to bottom. */ export function displayPins(n: CircuitNode): { key: string; label: string }[] { const bits = (p: string, k: number) => Array.from({ length: k }, (_, i) => ({ key: `${p}${i}`, label: `${p.toUpperCase()}${i}` })) if (n.kind === 'terminal') return [...bits('d', 8), { key: 'wr', label: 'WR' }, { key: 'clr', label: 'CLR' }] const [w, h] = displaySize(n) const data = n.kind === 'rgbmonitor' ? ['r', 'g', 'b'].map(k => ({ key: k, label: k.toUpperCase() })) : [{ key: 'd', label: 'D' }] return [...bits('x', addrBits(w)), ...bits('y', addrBits(h)), ...data, { key: 'wr', label: 'WR' }, { key: 'clr', label: 'CLR' }] } export interface DisplayLayout { w: number h: number /** The screen, in grid units from the node. */ sx: number sy: number sw: number sh: number /** One character or pixel. */ cw: number ch: number } export function displayLayout(n: CircuitNode): DisplayLayout { const [cols, lines] = displaySize(n) const pins = displayPins(n).length const cw = n.kind === 'terminal' ? TERM_CHAR : PIXEL const ch = n.kind === 'terminal' ? TERM_LINE : PIXEL const sx = 2 const sy = -0.5 + 0.2 const sw = cols * cw + (n.kind === 'terminal' ? 0.3 : 0) const sh = lines * ch return { w: Math.ceil(sx + sw + 0.3), h: Math.max(pins, Math.ceil(sh + 0.4)), sx, sy, sw, sh, cw, ch } } /** Width to height, in small numbers when there are some (16:9), else to two places (1.78:1). */ export function ratioText(w: number, h: number): string { const gcd = (a: number, b: number): number => (b ? gcd(b, a % b) : a) const g = gcd(w, h) const a = w / g const b = h / g return a <= 32 && b <= 32 ? `${a}:${b}` : `${(w / h).toFixed(2)}:1` } /** A pixel's colour on an RGB monitor (value r + 2g + 4b); black is 0. */ export const RGB_PIXEL = ['#000000', '#ff4d4d', '#3ddc84', '#ffe04d', '#4d7dff', '#ff5cf0', '#4de8ff', '#f2f6fa'] export const MONO_PIXEL = '#e8f0f8'