diff --git a/app/assets/css/main.css b/app/assets/css/main.css
index 99125fd..f670aee 100644
--- a/app/assets/css/main.css
+++ b/app/assets/css/main.css
@@ -6314,3 +6314,40 @@ button.tile:hover {
.scope-chan .scope-bits {
width: 52px;
}
+
+/* Screens: size by the corner */
+.node .frame {
+ image-rendering: pixelated;
+ pointer-events: none;
+}
+
+.node .resize-grip {
+ fill: var(--accent);
+ stroke: var(--bg);
+ stroke-width: 1.5;
+ cursor: nwse-resize;
+}
+
+.node .size-badge {
+ fill: var(--accent);
+ font-size: 10px;
+ font-weight: 600;
+ pointer-events: none;
+}
+
+.size-row {
+ display: flex;
+ align-items: center;
+ gap: 6px;
+}
+
+.size-row .field-input {
+ width: 70px;
+}
+
+.size-presets {
+ display: flex;
+ flex-wrap: wrap;
+ gap: 4px;
+ margin-top: 6px;
+}
diff --git a/app/components/editor/Canvas.vue b/app/components/editor/Canvas.vue
index 0d500d7..690b0fc 100644
--- a/app/components/editor/Canvas.vue
+++ b/app/components/editor/Canvas.vue
@@ -11,6 +11,7 @@ import type { CanvasOps, MenuEntry } from '~/composables/useMenu'
import { cableInUse, LANE, plugLayout, ribbon, RIBBON_MAX } from '~/lib/logic/cables'
import { latchLayout, latchSeam } from '~/lib/logic/latch'
import { keyCode } from '~/lib/logic/devices'
+import { displayLayout, displaySize } from '~/lib/logic/displays'
import { moveParts, moveStart, type MoveStart } from '~/lib/logic/move'
import { wirePath, type HopSpan } from '~/lib/draw'
import { downloadSheet, printSheet, sheetSvg } from '~/lib/sheet'
@@ -639,6 +640,14 @@ function onDrop(e: DragEvent) {
// ── Pointer interaction ─────────────────────────────────────────────
type Drag =
+ | {
+ /** A screen's corner: its size in characters or pixels follows. */
+ kind: 'resize'
+ node: CircuitNode
+ start: Pt
+ size: [number, number]
+ cell: [number, number]
+ }
| {
kind: 'pan'
sx: number
@@ -758,6 +767,13 @@ function onPointerDown(e: PointerEvent) {
}
const target = e.target as Element
+ const resizeEl = target.closest('[data-resize]')
+ const resizing = resizeEl ? nodeMap.value.get(resizeEl.closest('[data-node]')?.getAttribute('data-node') ?? '') : undefined
+ if (resizing) {
+ const l = displayLayout(resizing)
+ drag.value = { kind: 'resize', node: resizing, start: w, size: displaySize(resizing), cell: [l.cw, l.ch] }
+ return
+ }
const actionEl = target.closest('[data-action]')
if (actionEl) return actionButtons.value.find(b => b.key === actionEl.getAttribute('data-action'))?.run(e)
const widenEl = target.closest('[data-inputs]')
@@ -902,6 +918,9 @@ function onPointerMove(e: PointerEvent) {
const d = drag.value
if (!d) return trackHover(e.target as Element)
switch (d.kind) {
+ case 'resize':
+ ed.setDims(d.node.id, d.size[0] + Math.round((w[0] - d.start[0]) / d.cell[0]), d.size[1] + Math.round((w[1] - d.start[1]) / d.cell[1]), false)
+ break
case 'pan':
ed.views[def.value.id] = { ...view.value, x: d.vx + e.clientX - d.sx, y: d.vy + e.clientY - d.sy }
break
@@ -1152,6 +1171,9 @@ function onPointerUp(e: PointerEvent) {
drag.value = null
if (!d) return
switch (d.kind) {
+ case 'resize':
+ ed.commit()
+ break
case 'pan':
if (d.menu && Math.abs(e.clientX - d.sx) + Math.abs(e.clientY - d.sy) < 5) openMenuAt(d.menu.target, d.menu.at, e.clientX, e.clientY)
break
diff --git a/app/components/editor/NodeSettings.vue b/app/components/editor/NodeSettings.vue
index 610af55..4cc3c2a 100644
--- a/app/components/editor/NodeSettings.vue
+++ b/app/components/editor/NodeSettings.vue
@@ -6,6 +6,7 @@ import { cableOf } from '~/lib/logic/cables'
import { INVERTIBLE } from '~/lib/logic/compile'
import { cableBits, cableOffsets, chipPins, MAX_BITS, panelOffsets, portName, rowLabel } from '~/lib/logic/geometry'
import { SINK_KINDS, SOURCE_KINDS, uid } from '~/lib/logic/ops'
+import { DISPLAY_SIZES, displaySize, isDisplay, ratioText, type DisplayKind } from '~/lib/logic/displays'
import { KIND_NAMES } from '~/lib/logic/progress'
import { baseOf, gateFamily, inputCount, MAX_GATE_INPUTS } from '~/lib/logic/wide'
import { LAMP_COLORS, WIRE_COLORS, type CircuitNode, type NodeKind, type PanelControl, type PanelRow } from '~/lib/logic/types'
@@ -345,6 +346,9 @@ function setRowHz(row: PanelRow, e: Event) {
ed.commit()
}
+/** Monitor sizes a click away. */
+const SIZE_PRESETS: [number, number][] = [[16, 16], [32, 32], [64, 32], [64, 64], [128, 64], [128, 128]]
+
/** Logic analyzer channels. */
function addChannel(bits: number) {
const used = new Set((n.value.scope?.cols ?? []).map(c => c.name))
@@ -712,6 +716,19 @@ function setChannelBits(i: number, v: string) {
+
Channels: wire to their pins on the left
diff --git a/app/components/editor/NodeView.vue b/app/components/editor/NodeView.vue
index 0b044a9..4726b8d 100644
--- a/app/components/editor/NodeView.vue
+++ b/app/components/editor/NodeView.vue
@@ -5,7 +5,8 @@ import { baseOf, gateFamily, inputCount, MAX_GATE_INPUTS } from '~/lib/logic/wid
import { faceTransform, GRID as G, noteLines, noteScale, PANEL_CONTROL_W, portLines, rgbHex, shapeTransform, type Shape } from '~/lib/logic/geometry'
import { benchLayout } from '~/lib/logic/benchLayout'
import { SCOPE_CELL, SCOPE_SAMPLES, scopeLayout } from '~/lib/logic/scopeLayout'
-import { keyLabel, TERMINAL_COLS, TERMINAL_LINES } from '~/lib/logic/devices'
+import { keyLabel } from '~/lib/logic/devices'
+import { displayLayout, displaySize, isDisplay, MONO_PIXEL, ratioText, RGB_PIXEL, TERM_CHAR } from '~/lib/logic/displays'
import type { CircuitNode } from '~/lib/logic/types'
const props = defineProps<{
@@ -257,16 +258,44 @@ const bench = computed(() => {
return { state: r?.fail || (r?.error && !r.pass) ? 'fail' : r?.pass ? 'pass' : 'idle', line, cols, rows, right }
})
-// Terminal and keyboard: what the store keeps for this part.
-const TERM_CHAR = 6.6
+// Terminal and monitors: what the store keeps for this part, and its size.
+const display = computed(() => isDisplay(props.node.kind))
+const dl = computed(() => (display.value ? displayLayout(props.node) : { w: 0, h: 0, sx: 0, sy: 0, sw: 0, sh: 0, cw: 0, ch: 0 }))
+const TERM_PX = TERM_CHAR * G
+const sizeText = computed(() => {
+ if (!display.value) return ''
+ const [w, h] = displaySize(props.node)
+ return props.node.kind === 'terminal' ? `${w} × ${h} characters` : `${w} × ${h} · ${ratioText(w, h)}`
+})
const termLines = computed(() => {
if (props.node.kind !== 'terminal' || props.ghost) return []
void ed.devVersion.value
- return (ed.screens.value.get(props.node.id) ?? ['']).slice(-TERMINAL_LINES)
+ return (ed.screens.value.get(props.node.id) ?? ['']).slice(-displaySize(props.node)[1])
})
const termCursor = computed(() => {
const lines = termLines.value.length ? termLines.value : ['']
- return { x: Math.min(TERMINAL_COLS - 1, lines[lines.length - 1]!.length), y: lines.length - 1 }
+ return { x: Math.min(displaySize(props.node)[0] - 1, lines[lines.length - 1]!.length), y: lines.length - 1 }
+})
+/** A monitor's pixels as an image, made again when they change. */
+const frameUrl = computed(() => {
+ const n = props.node
+ if ((n.kind !== 'monitor' && n.kind !== 'rgbmonitor') || props.ghost || typeof document === 'undefined') return ''
+ void ed.devVersion.value
+ const f = ed.frames.value.get(n.id)
+ if (!f) return ''
+ const c = document.createElement('canvas')
+ c.width = f.w
+ c.height = f.h
+ const ctx = c.getContext('2d')
+ if (!ctx) return ''
+ const img = ctx.createImageData(f.w, f.h)
+ const palette = (n.kind === 'rgbmonitor' ? RGB_PIXEL : ['#000000', MONO_PIXEL]).map(h => [parseInt(h.slice(1, 3), 16), parseInt(h.slice(3, 5), 16), parseInt(h.slice(5, 7), 16)])
+ for (let i = 0; i < f.px.length; i++) {
+ const [r, g, bl] = palette[f.px[i]!] ?? palette[0]!
+ img.data.set([r!, g!, bl!, f.px[i] ? 255 : 0], i * 4)
+ }
+ ctx.putImageData(img, 0, 0)
+ return c.toDataURL()
})
const kbTyping = computed(() => ed.typingInto.value === props.node.id)
const kbWaiting = computed(() => {
@@ -550,14 +579,21 @@ const title = computed(() => {
{{ p.label }}
-
-
+
+
-
- {{ line }}
-
+
+
+ {{ line }}
+
+
+
{{ p.label }}
{{ node.label }}
+
+ {{ sizeText }}
+ Drag to resize
+
diff --git a/app/composables/useEditor.ts b/app/composables/useEditor.ts
index 7e554f0..9ed9d7d 100644
--- a/app/composables/useEditor.ts
+++ b/app/composables/useEditor.ts
@@ -26,6 +26,7 @@ import { apiId, makePuzzle, scoreSolution, weekKey, weeklyId } from '~/lib/logic
import { baseOf, MAX_GATE_INPUTS, NAND_BASE, wideGate } from '~/lib/logic/wide'
import { CHALLENGES, challengeSteps, type Challenge } from '~/lib/logic/challenges'
import { bitsCode, codeBits, DATA_KEYS, KEY_QUEUE, putChar } from '~/lib/logic/devices'
+import { addrBits, clampSize, displaySize, isDisplay } from '~/lib/logic/displays'
import { AUTO_TICKS, benchFor, colKeys, groupPins, HAND_TICKS, parseCell, runBench, upgradeBenches, type BenchResult } from '~/lib/logic/bench'
import { EXAMPLE as ASM_EXAMPLE, loadIntoPanels } from '~/lib/asm'
import { latchedTo, latchInto, latchTarget, syncLatches } from '~/lib/logic/latch'
@@ -432,6 +433,9 @@ function create() {
const typingInto = ref(null)
let devTerms: string[] = []
let devKbs: string[] = []
+ let devMons: string[] = []
+ /** What each monitor shows: a value a pixel, row by row (one bit, or r + 2g + 4b). Not saved. */
+ const frames = shallowRef(new Map())
/** Each strobe pin's level when last looked at, to act on a rise. */
const levels = new Map()
@@ -464,12 +468,15 @@ function create() {
for (const n of d.nodes) if (n.kind === 'bench' && benchLive.value.has(n.id)) feedBench(n.id, s)
devTerms = d.nodes.filter(n => n.kind === 'terminal').map(n => n.id)
devKbs = d.nodes.filter(n => n.kind === 'keyboard').map(n => n.id)
+ devMons = d.nodes.filter(n => n.kind === 'monitor' || n.kind === 'rgbmonitor').map(n => n.id)
+ for (const n of d.nodes) if (n.kind === 'monitor' || n.kind === 'rgbmonitor') frameOf(n)
if (!devKbs.includes(typingInto.value ?? '')) typingInto.value = null
for (const id of devKbs) feedKeyboard(id, s)
s.run(BUDGET)
// Where the strobes start: only a rise from here on acts.
for (const id of devTerms) for (const pin of ['wr', 'clr']) levels.set(`${id}:${pin}`, s.pin(id, pin))
for (const id of devKbs) levels.set(`${id}:next`, s.pin(id, 'next'))
+ for (const id of devMons) for (const pin of ['wr', 'clr']) levels.set(`${id}:${pin}`, s.pin(id, pin))
if (simRoot !== key) cycles.value = 0
sim.value = s
simRoot = key
@@ -593,7 +600,7 @@ function create() {
/** Acts on the strobes of terminals and keyboards; called whenever the circuit has settled. */
function devices() {
const s = sim.value
- if (!s || (!devTerms.length && !devKbs.length)) return
+ if (!s || (!devTerms.length && !devKbs.length && !devMons.length)) return
let changed = false
for (const id of devTerms) {
if (rose(id, 'clr', s.pin(id, 'clr'))) {
@@ -601,10 +608,28 @@ function create() {
changed = true
}
if (rose(id, 'wr', s.pin(id, 'wr'))) {
- screens.value.set(id, putChar(screens.value.get(id) ?? [], bitsCode(DATA_KEYS.map(k => s.pin(id, k)))))
+ const n = def.value.nodes.find(x => x.id === id)
+ screens.value.set(id, putChar(screens.value.get(id) ?? [], bitsCode(DATA_KEYS.map(k => s.pin(id, k))), n ? displaySize(n)[0] : undefined))
changed = true
}
}
+ for (const id of devMons) {
+ const clr = rose(id, 'clr', s.pin(id, 'clr'))
+ const wr = rose(id, 'wr', s.pin(id, 'wr'))
+ if (!clr && !wr) continue
+ const n = def.value.nodes.find(x => x.id === id)
+ if (!n) continue
+ const f = frameOf(n)
+ if (clr) f.px.fill(0)
+ if (wr) {
+ const num = (p: string, k: number) => Array.from({ length: k }, (_, i) => s.pin(id, `${p}${i}`) << i).reduce((a, b) => a + b, 0)
+ const x = num('x', addrBits(f.w))
+ const y = num('y', addrBits(f.h))
+ const v = n.kind === 'rgbmonitor' ? s.pin(id, 'r') + 2 * s.pin(id, 'g') + 4 * s.pin(id, 'b') : s.pin(id, 'd')
+ if (x < f.w && y < f.h) f.px[y * f.w + x] = v
+ }
+ changed = true
+ }
for (const id of devKbs) {
if (rose(id, 'next', s.pin(id, 'next'))) {
const q = keyQueues.value.get(id)
@@ -618,6 +643,45 @@ function create() {
if (changed) devVersion.value++
}
+ /** A monitor's pixels, made (or remade, keeping what fits) for its size. */
+ function frameOf(n: CircuitNode) {
+ const [w, h] = displaySize(n)
+ const old = frames.value.get(n.id)
+ if (old && old.w === w && old.h === h) return old
+ const px = new Uint8Array(w * h)
+ if (old) for (let y = 0; y < Math.min(h, old.h); y++) for (let x = 0; x < Math.min(w, old.w); x++) px[y * w + x] = old.px[y * old.w + x]!
+ const f = { w, h, px }
+ frames.value.set(n.id, f)
+ return f
+ }
+
+ /** A screen's size (characters or pixels); its pins follow. */
+ function setDims(id: string, w: number, h: number, undoable = true) {
+ const n = def.value.nodes.find(x => x.id === id)
+ if (!n || !isDisplay(n.kind)) return
+ const [a, b] = clampSize(n.kind, w, h)
+ const [ow, oh] = displaySize(n)
+ if (a === ow && b === oh) return
+ n.dims = [a, b]
+ if (n.kind !== 'terminal') frameOf(n)
+ if (undoable) commit()
+ else devVersion.value++
+ }
+
+ /** Turns a screen a quarter: width and height swap. */
+ function turnDisplay(id: string) {
+ const n = def.value.nodes.find(x => x.id === id)
+ if (!n || !isDisplay(n.kind)) return
+ const [w, h] = displaySize(n)
+ setDims(id, h, w)
+ }
+
+ function clearFrame(id: string) {
+ const f = frames.value.get(id)
+ if (f) f.px.fill(0)
+ devVersion.value++
+ }
+
/** Starts (or stops) typing into a keyboard. */
function typeInto(id: string | null) {
typingInto.value = id && typingInto.value !== id ? id : null
@@ -3162,6 +3226,10 @@ function create() {
typeKey,
clearScreen,
clearKeys,
+ frames,
+ setDims,
+ turnDisplay,
+ clearFrame,
asmDoc,
saveAsm,
loadProgram,
diff --git a/app/composables/useMenu.ts b/app/composables/useMenu.ts
index 679296b..c49faaf 100644
--- a/app/composables/useMenu.ts
+++ b/app/composables/useMenu.ts
@@ -154,6 +154,8 @@ export function useMenu() {
)
}
if (n.kind === 'terminal') out.push({ label: 'Clear the screen', icon: 'lucide:eraser', run: () => ed.clearScreen(n.id) })
+ if (n.kind === 'monitor' || n.kind === 'rgbmonitor') out.push({ label: 'Clear the screen', icon: 'lucide:eraser', run: () => ed.clearFrame(n.id) })
+ if (n.kind === 'terminal' || n.kind === 'monitor' || n.kind === 'rgbmonitor') out.push({ label: 'Turn (width and height swap)', icon: 'lucide:rotate-cw-square', run: () => ed.turnDisplay(n.id) })
if (n.kind === 'keyboard') {
out.push({ label: ed.typingInto.value === n.id ? 'Stop typing' : 'Type into it', icon: 'lucide:keyboard', keys: 'Click', run: () => ed.typeInto(n.id) })
if (ed.keyQueues.value.get(n.id)?.length) out.push({ label: 'Forget the waiting keys', icon: 'lucide:eraser', run: () => ed.clearKeys(n.id) })
diff --git a/app/composables/useParts.ts b/app/composables/useParts.ts
index 35f85a7..4f331bf 100644
--- a/app/composables/useParts.ts
+++ b/app/composables/useParts.ts
@@ -43,6 +43,8 @@ export const BASIC_GROUPS: { name: string; parts: BasicPart[] }[] = [
{ kind: 'outpanel', name: 'Output panel', icon: 'lucide:list-checks', hint: 'A column of named lamps. Inside a component its rows are output pins.', aka: 'lamps leds' },
{ kind: 'hex', name: 'Hex display', icon: 'lucide:binary', key: 'H', hint: 'Shows 4 bits as 0–F, and passes them on.', aka: 'seven segment 7seg digit' },
{ kind: 'terminal', name: 'Terminal', icon: 'lucide:square-terminal', hint: 'A text screen: a rising WR writes the character whose code is on D0–D7 (10 starts a new line, 8 goes back); a rising CLR clears it.', aka: 'text output ascii console screen print' },
+ { kind: 'monitor', name: 'Monitor', icon: 'lucide:monitor', hint: 'A screen of pixels, one bit each (like an OLED): a rising WR sets the pixel at X, Y to D, a rising CLR clears it. Its size in pixels is yours to set.', aka: 'oled lcd screen display framebuffer graphics mono binary' },
+ { kind: 'rgbmonitor', name: 'RGB monitor', icon: 'lucide:monitor-play', hint: 'A screen of pixels in eight colours: a rising WR sets the pixel at X, Y to R G B, a rising CLR clears it. Its size in pixels is yours to set.', aka: 'colour color screen display framebuffer graphics' },
{ kind: 'pixel', name: 'Pixel', icon: 'lucide:square', key: 'X', hint: 'One square of a screen, lit at 1, or in colour with red, green and blue inputs. Pixels inside a component show on its box as screens.', aka: 'screen lcd rgb colour color monitor' },
],
},
diff --git a/app/lib/logic/compile.ts b/app/lib/logic/compile.ts
index c531b0b..de9f64e 100644
--- a/app/lib/logic/compile.ts
+++ b/app/lib/logic/compile.ts
@@ -5,6 +5,7 @@
import { cableBits, chipPins, isPinRow, portLines, rowPin, splitLines, type ChipPins, type DefLookup } from './geometry'
import { colKeys } from './benchLayout'
import { DATA_KEYS } from './devices'
+import { displayPins } from './displays'
import type { CircuitNode, ComponentDef, Wire } from './types'
export const LOW = 0
@@ -56,7 +57,9 @@ export function inputKeys(node: CircuitNode, pins?: ChipPins, get?: DefLookup):
case 'hex':
return ['b0', 'b1', 'b2', 'b3']
case 'terminal':
- return [...DATA_KEYS, 'wr', 'clr']
+ case 'monitor':
+ case 'rgbmonitor':
+ return displayPins(node).map(p => p.key)
case 'keyboard':
return ['next']
case 'bench':
diff --git a/app/lib/logic/displays.ts b/app/lib/logic/displays.ts
new file mode 100644
index 0000000..6f50898
--- /dev/null
+++ b/app/lib/logic/displays.ts
@@ -0,0 +1,84 @@
+// 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'
diff --git a/app/lib/logic/geometry.ts b/app/lib/logic/geometry.ts
index 51a8a77..f667d3b 100644
--- a/app/lib/logic/geometry.ts
+++ b/app/lib/logic/geometry.ts
@@ -5,6 +5,7 @@ import type { CircuitNode, ComponentDef, ConnectorType, FaceShape, LampColor, Pa
import { benchLayout } from './benchLayout'
import { scopeLayout } from './scopeLayout'
import { DATA_KEYS, DEVICE_PIN_NAMES } from './devices'
+import { displayLayout, displayPins } from './displays'
export const GRID = 16
@@ -723,10 +724,9 @@ const PIXEL_RGB_SHAPE: Shape = {
outputs: [],
}
-const TERMINAL_SHAPE: Shape = {
- body: { x: 0, y: -0.5, w: 15, h: 10 },
- inputs: [...DATA_KEYS, 'wr', 'clr'].map((key, i) => ({ key, label: DEVICE_PIN_NAMES[key]!, dx: 0, dy: i })),
- outputs: [],
+function displayShape(node: CircuitNode): Shape {
+ const l = displayLayout(node)
+ return { body: { x: 0, y: -0.5, w: l.w, h: l.h }, inputs: displayPins(node).map((p, i) => ({ ...p, dx: 0, dy: i })), outputs: [] }
}
function benchShape(node: CircuitNode): Shape {
@@ -841,7 +841,9 @@ export function shapeOf(node: CircuitNode, getDef: DefLookup): Shape {
case 'pixel':
return node.rgb ? PIXEL_RGB_SHAPE : PIXEL_SHAPE
case 'terminal':
- return TERMINAL_SHAPE
+ case 'monitor':
+ case 'rgbmonitor':
+ return displayShape(node)
case 'bench':
return benchShape(node)
case 'scope': {
diff --git a/app/lib/logic/labels.ts b/app/lib/logic/labels.ts
index 9db15aa..f03a86b 100644
--- a/app/lib/logic/labels.ts
+++ b/app/lib/logic/labels.ts
@@ -5,6 +5,7 @@
import { chipPins, splitLines, type DefLookup } from './geometry'
import { colPinNames } from './benchLayout'
import { DEVICE_PIN_NAMES } from './devices'
+import { displayPins } from './displays'
import type { CircuitNode, ComponentDef, NodeKind } from './types'
const SERIES = /^(.*?)(\d+)$/
@@ -85,6 +86,7 @@ export function pinName(node: CircuitNode, pin: string, get: DefLookup): string
// A wire of a cable carries the name of its signal (the cable pin itself has none).
if (node.kind === 'bundle' || node.kind === 'split') return /^b\d+$/.test(pin) ? node.names?.[Number(pin.slice(1))]?.trim() || undefined : undefined
if (node.kind === 'terminal' || node.kind === 'keyboard') return DEVICE_PIN_NAMES[pin]
+ if (node.kind === 'monitor' || node.kind === 'rgbmonitor') return displayPins(node).find(p => p.key === pin)?.label
if (node.kind === 'bench' || node.kind === 'scope') {
const [id, bit] = pin.split('.')
const c = (node.kind === 'bench' ? node.bench?.cols : node.scope?.cols)?.find(x => x.id === id)
diff --git a/app/lib/logic/progress.ts b/app/lib/logic/progress.ts
index 4708d24..4ba5ab7 100644
--- a/app/lib/logic/progress.ts
+++ b/app/lib/logic/progress.ts
@@ -30,6 +30,8 @@ export const KIND_NAMES: Partial> = {
ref: 'Use signal',
terminal: 'Terminal',
keyboard: 'Keyboard',
+ monitor: 'Monitor',
+ rgbmonitor: 'RGB monitor',
bench: 'Test bench',
scope: 'Logic analyzer',
}
@@ -65,6 +67,8 @@ export const KIND_UNLOCK: Partial> = {
pixel: 'dec2',
terminal: 'dec2',
keyboard: 'dec2',
+ monitor: 'dec2',
+ rgbmonitor: 'dec2',
bundle: 'add2',
split: 'add2',
portin: 'add2',
diff --git a/app/lib/logic/project.ts b/app/lib/logic/project.ts
index f2113d4..e3c9a1c 100644
--- a/app/lib/logic/project.ts
+++ b/app/lib/logic/project.ts
@@ -49,6 +49,7 @@ function cleanNode(n: CircuitNode) {
if (n.inner !== undefined && n.inner !== true) delete n.inner
if (n.screen !== undefined && typeof n.screen !== 'string') delete n.screen
if (n.rgb !== undefined && n.rgb !== true) delete n.rgb
+ if (n.dims !== undefined && !(Array.isArray(n.dims) && n.dims.length === 2 && n.dims.every(v => Number.isInteger(v) && v >= 1 && v <= 128))) delete n.dims
if (n.scope !== undefined) {
const cols = Array.isArray(n.scope?.cols) ? n.scope.cols : []
n.scope = {
diff --git a/app/lib/logic/types.ts b/app/lib/logic/types.ts
index b96ed7e..b77ba0a 100644
--- a/app/lib/logic/types.ts
+++ b/app/lib/logic/types.ts
@@ -23,6 +23,8 @@ export type BuiltinKind =
| 'tag' // gives the signal wired into it a name (its label)
| 'ref' // carries the signal of the tag with the same name in this circuit, without a wire
| 'terminal' // a text screen: writes the character on D0–D7 at a rising WR, clears at a rising CLR
+ | 'monitor' // a screen of pixels, one bit each: a rising WR sets the pixel at X, Y to D; a rising CLR clears it
+ | 'rgbmonitor' // the same in eight colours: R, G, B instead of D
| 'keyboard' // the keys you type: the first waiting one on D0–D7, RDY while one waits, a rising NEXT takes it
| 'bench' // a table of tests: drives the circuit from its outputs, checks what comes back on its inputs
| 'scope' // logic analyzer: the signals on its inputs over time, drawn on its box
@@ -78,6 +80,8 @@ export interface CircuitNode {
inner?: boolean
/** Pixel: the screen it belongs to on the component's box; pixels without one make the main screen. */
screen?: string
+ /** Terminal: characters wide and lines; monitors: pixels wide and high. Empty: the part's starting size. */
+ dims?: [number, number]
/** Pixel with three inputs, red, green and blue: eight colours. */
rgb?: boolean
/** Test bench: its columns and tests. */
diff --git a/tests/devices.test.ts b/tests/devices.test.ts
index 5c34fc8..4454282 100644
--- a/tests/devices.test.ts
+++ b/tests/devices.test.ts
@@ -5,6 +5,7 @@ import * as vue from 'vue'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { compile } from '../app/lib/logic/compile'
import { DATA_KEYS, keyCode, putChar } from '../app/lib/logic/devices'
+import { clampSize, displayPins, displaySize, ratioText } from '../app/lib/logic/displays'
import { createProject, lookup } from '../app/lib/logic/ops'
import { Simulator } from '../app/lib/logic/sim'
import { saveProject, setCurrentId } from '../app/lib/logic/storage'
@@ -124,3 +125,52 @@ describe('keyboard and terminal in the editor', () => {
expect(s.nl.sources.size).toBe(0)
})
})
+
+describe('monitors', () => {
+ it('pins follow the size: as many X and Y bits as it needs; turning swaps them', () => {
+ const n = { id: 'm', kind: 'monitor' as const, x: 0, y: 0, dims: [64, 32] as [number, number] }
+ expect(displayPins(n).map(p => p.label)).toEqual(['X0', 'X1', 'X2', 'X3', 'X4', 'X5', 'Y0', 'Y1', 'Y2', 'Y3', 'Y4', 'D', 'WR', 'CLR'])
+ expect(displayPins({ ...n, kind: 'rgbmonitor', dims: [5, 3] }).map(p => p.label)).toEqual(['X0', 'X1', 'X2', 'Y0', 'Y1', 'R', 'G', 'B', 'WR', 'CLR'])
+ expect(ratioText(64, 32)).toBe('2:1')
+ expect(ratioText(1920, 1080)).toBe('16:9')
+ expect(ratioText(127, 3)).toBe('42.33:1')
+ expect(clampSize('monitor', 500, 0)).toEqual([128, 2])
+ // An old terminal (no size) keeps its 32 × 10 and its pins.
+ expect(displaySize({ id: 't', kind: 'terminal', x: 0, y: 0 })).toEqual([32, 10])
+ })
+
+ it('a rising WR sets the pixel at X, Y to D; CLR clears; resizing keeps what fits', async () => {
+ const p = createProject({ examples: false })
+ const sw = (id: string, y: number) => ({ id, kind: 'input' as const, x: 0, y, label: id.toUpperCase() })
+ p.components.push({
+ id: 'mon',
+ name: 'Mon',
+ symbol: 'box',
+ nodes: [sw('x0', 0), sw('x1', 2), sw('y0', 4), sw('y1', 6), sw('d', 8), { id: 'w', kind: 'button', x: 0, y: 10, label: 'W' }, { id: 'c', kind: 'button', x: 0, y: 12, label: 'C' }, { id: 'm', kind: 'monitor', x: 10, y: 0, dims: [4, 4] }],
+ wires: [...['x0', 'x1', 'y0', 'y1', 'd'].map(k => ({ id: `w${k}`, from: { node: k, pin: 'q' }, to: { node: 'm', pin: k } })), { id: 'ww', from: { node: 'w', pin: 'q' }, to: { node: 'm', pin: 'wr' } }, { id: 'wc', from: { node: 'c', pin: 'q' }, to: { node: 'm', pin: 'clr' } }],
+ })
+ p.openId = 'mon'
+ const ed = await openEditor(p)
+ vi.advanceTimersByTime(100)
+ const node = (id: string) => ed.def.value.nodes.find(n => n.id === id)!
+ const click = (id: string) => {
+ ed.press(node(id), true)
+ ed.step()
+ ed.press(node(id), false)
+ ed.step()
+ }
+ // x = 3, y = 2, d = 1
+ for (const k of ['x0', 'x1', 'y1', 'd']) ed.toggleInput(node(k))
+ click('w')
+ const px = () => [...ed.frames.value.get('m')!.px]
+ expect(px().indexOf(1)).toBe(2 * 4 + 3)
+ expect(px().filter(v => v).length).toBe(1)
+ ed.setDims('m', 8, 3)
+ expect(ed.frames.value.get('m')!.px.length).toBe(24)
+ expect(px().indexOf(1)).toBe(2 * 8 + 3)
+ click('c')
+ expect(px().some(v => v)).toBe(false)
+ ed.turnDisplay('m')
+ expect(node('m').dims).toEqual([3, 8])
+ })
+})