138 lines
5.1 KiB
TypeScript
138 lines
5.1 KiB
TypeScript
|
|
// What a circuit remembers between visits and saves, like the contents of a
|
||
|
|
// memory chip. Only gates on feedback loops (latches, flip-flops, registers,
|
||
|
|
// RAM built from NANDs) can hold a bit; every other gate follows from its
|
||
|
|
// inputs. So only the loop gates that are 1 are kept, plus the clocks that
|
||
|
|
// are high, written small enough to save with the project.
|
||
|
|
|
||
|
|
import type { Netlist } from './compile'
|
||
|
|
import type { SimCarry, Simulator } from './sim'
|
||
|
|
import type { HeldState } from './types'
|
||
|
|
|
||
|
|
export interface Flow {
|
||
|
|
/** Every gate once, each after the gates that feed it; the gates of a loop after what feeds the loop. */
|
||
|
|
order: Int32Array
|
||
|
|
/** 1 for gates on a feedback loop. */
|
||
|
|
loop: Uint8Array
|
||
|
|
}
|
||
|
|
|
||
|
|
const flows = new WeakMap<Netlist, Flow>()
|
||
|
|
|
||
|
|
/** Which gates sit on a loop, and the order signals flow in. Tarjan's method without recursion, so a CPU can't run out of stack; kept per netlist. */
|
||
|
|
export function flow(nl: Netlist): Flow {
|
||
|
|
const known = flows.get(nl)
|
||
|
|
if (known) return known
|
||
|
|
const { gateA, gateB, gateOut, fanStart, fanList } = nl
|
||
|
|
const n = gateOut.length
|
||
|
|
const index = new Int32Array(n).fill(-1)
|
||
|
|
const low = new Int32Array(n)
|
||
|
|
const onStack = new Uint8Array(n)
|
||
|
|
const stack = new Int32Array(n)
|
||
|
|
const path = new Int32Array(n)
|
||
|
|
const edge = new Int32Array(n)
|
||
|
|
const loop = new Uint8Array(n)
|
||
|
|
// A group of gates is finished after every group it feeds, so the order fills from the back.
|
||
|
|
const order = new Int32Array(n)
|
||
|
|
let done = n
|
||
|
|
let sp = 0
|
||
|
|
let next = 0
|
||
|
|
for (let root = 0; root < n; root++) {
|
||
|
|
if (index[root]! >= 0) continue
|
||
|
|
let depth = 0
|
||
|
|
path[0] = root
|
||
|
|
index[root] = low[root] = next++
|
||
|
|
stack[sp++] = root
|
||
|
|
onStack[root] = 1
|
||
|
|
edge[root] = fanStart[gateOut[root]!]!
|
||
|
|
while (depth >= 0) {
|
||
|
|
const g = path[depth]!
|
||
|
|
if (edge[g]! < fanStart[gateOut[g]! + 1]!) {
|
||
|
|
const r = fanList[edge[g]!++]!
|
||
|
|
if (index[r]! < 0) {
|
||
|
|
index[r] = low[r] = next++
|
||
|
|
stack[sp++] = r
|
||
|
|
onStack[r] = 1
|
||
|
|
edge[r] = fanStart[gateOut[r]!]!
|
||
|
|
path[++depth] = r
|
||
|
|
} else if (onStack[r]) low[g] = Math.min(low[g]!, index[r]!)
|
||
|
|
continue
|
||
|
|
}
|
||
|
|
if (low[g] === index[g]) {
|
||
|
|
const end = done
|
||
|
|
let r: number
|
||
|
|
do {
|
||
|
|
r = stack[--sp]!
|
||
|
|
onStack[r] = 0
|
||
|
|
order[--done] = r
|
||
|
|
} while (r !== g)
|
||
|
|
if (end - done > 1) {
|
||
|
|
// Inside a loop the gates keep the order they were placed in, which decides how a new latch starts (see ex-sr).
|
||
|
|
order.subarray(done, end).sort()
|
||
|
|
for (let i = done; i < end; i++) loop[order[i]!] = 1
|
||
|
|
} else if (gateA[g] === gateOut[g] || gateB[g] === gateOut[g]) {
|
||
|
|
// A gate on its own is a loop when it reads its own output.
|
||
|
|
loop[g] = 1
|
||
|
|
}
|
||
|
|
}
|
||
|
|
depth--
|
||
|
|
if (depth >= 0) low[path[depth]!] = Math.min(low[path[depth]!]!, low[g]!)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
const res = { order, loop }
|
||
|
|
flows.set(nl, res)
|
||
|
|
return res
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* What the circuit holds right now, ready to save; undefined when nothing is 1.
|
||
|
|
* A gate's key is its path of node ids ("chipNode/chipNode/nand"): each id goes
|
||
|
|
* in `names` once, and a gate is written as how many levels of its path it
|
||
|
|
* shares with the gate before it, then the indexes of the rest. Gates come
|
||
|
|
* chip by chip, so most share all but the last level or two.
|
||
|
|
*/
|
||
|
|
export function packState(sim: Simulator): HeldState | undefined {
|
||
|
|
const { nl, values } = sim
|
||
|
|
const { loop } = flow(nl)
|
||
|
|
const names: string[] = []
|
||
|
|
const index = new Map<string, number>()
|
||
|
|
const on: string[] = []
|
||
|
|
let prev: string[] = []
|
||
|
|
for (let g = 0; g < loop.length; g++) {
|
||
|
|
if (!loop[g] || !values[nl.gateOut[g]!]) continue
|
||
|
|
const path = nl.gateKeys[g]!.split('/')
|
||
|
|
let shared = 0
|
||
|
|
while (shared < path.length - 1 && path[shared] === prev[shared]) shared++
|
||
|
|
const rest = path.slice(shared).map(id => {
|
||
|
|
let i = index.get(id)
|
||
|
|
if (i === undefined) {
|
||
|
|
i = names.push(id) - 1
|
||
|
|
index.set(id, i)
|
||
|
|
}
|
||
|
|
return i.toString(36)
|
||
|
|
})
|
||
|
|
on.push(`${shared.toString(36)}:${rest.join('.')}`)
|
||
|
|
prev = path
|
||
|
|
}
|
||
|
|
const clocks = nl.clockKeys.filter((_, i) => values[nl.clocks[i]!])
|
||
|
|
if (!on.length && !clocks.length) return undefined
|
||
|
|
const held: HeldState = { names, on: on.join(' ') }
|
||
|
|
if (clocks.length) held.clocks = clocks
|
||
|
|
return held
|
||
|
|
}
|
||
|
|
|
||
|
|
/** Saved bits as the simulator's carry for this netlist: loop gates not named are 0, and so are clocks. */
|
||
|
|
export function stateCarry(held: HeldState, nl: Netlist): SimCarry {
|
||
|
|
const gates = new Map<string, number>()
|
||
|
|
let prev: string[] = []
|
||
|
|
for (const gate of held.on.split(' ')) {
|
||
|
|
const [shared, rest] = gate.split(':')
|
||
|
|
if (!shared || !rest) continue
|
||
|
|
// A broken entry makes a key no gate has, which is ignored like one that no longer exists.
|
||
|
|
const path = [...prev.slice(0, parseInt(shared, 36)), ...rest.split('.').map(i => held.names[parseInt(i, 36)] ?? '')]
|
||
|
|
gates.set(path.join('/'), 1)
|
||
|
|
prev = path
|
||
|
|
}
|
||
|
|
const high = new Set(held.clocks ?? [])
|
||
|
|
const clocks = new Map(nl.clockKeys.map(k => [k, high.has(k) ? 1 : 0]))
|
||
|
|
return { gates, clock: high.size ? 1 : 0, clocks }
|
||
|
|
}
|