fix: iterative evaluation for sequential circuits + debug logs
Replace single-pass recursive evaluation with iterative fixed-point evaluation that runs multiple passes until all gate values stabilize. Crucially, gate values are NO LONGER reset to 0 before evaluation, which preserves latch/flip-flop memory state. Add console logs for toggle, wire, and evaluation stability debugging. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
71
js/gates.js
71
js/gates.js
@@ -66,9 +66,11 @@ export function getOutputPorts(gate) {
|
||||
return ports;
|
||||
}
|
||||
|
||||
export function evaluate(gate, visited = new Set()) {
|
||||
if (visited.has(gate.id)) return gate.value || 0;
|
||||
visited.add(gate.id);
|
||||
/**
|
||||
* Compute the output of a single gate given its current input values.
|
||||
* Does NOT recurse — just reads source gate .value directly.
|
||||
*/
|
||||
function computeGate(gate) {
|
||||
if (gate.type === 'INPUT' || gate.type === 'CLOCK') return gate.value;
|
||||
|
||||
const inputCount = gateInputCount(gate.type);
|
||||
@@ -77,39 +79,64 @@ export function evaluate(gate, visited = new Set()) {
|
||||
const conn = state.connections.find(c => c.to === gate.id && c.toPort === i);
|
||||
if (conn) {
|
||||
const srcGate = state.gates.find(g => g.id === conn.from);
|
||||
inputs.push(srcGate ? evaluate(srcGate, visited) : 0);
|
||||
inputs.push(srcGate ? (srcGate.value || 0) : 0);
|
||||
} else {
|
||||
inputs.push(0);
|
||||
}
|
||||
}
|
||||
|
||||
let result = 0;
|
||||
if (gate.type.startsWith('COMPONENT:')) {
|
||||
const outputs = evaluateComponent(gate, inputs);
|
||||
result = outputs[0] || 0;
|
||||
} else {
|
||||
switch (gate.type) {
|
||||
case 'AND': result = (inputs[0] && inputs[1]) ? 1 : 0; break;
|
||||
case 'OR': result = (inputs[0] || inputs[1]) ? 1 : 0; break;
|
||||
case 'NOT': result = inputs[0] ? 0 : 1; break;
|
||||
case 'NAND': result = (inputs[0] && inputs[1]) ? 0 : 1; break;
|
||||
case 'NOR': result = (inputs[0] || inputs[1]) ? 0 : 1; break;
|
||||
case 'XOR': result = (inputs[0] !== inputs[1]) ? 1 : 0; break;
|
||||
case 'OUTPUT': result = inputs[0] || 0; break;
|
||||
}
|
||||
return outputs[0] || 0;
|
||||
}
|
||||
|
||||
switch (gate.type) {
|
||||
case 'AND': return (inputs[0] && inputs[1]) ? 1 : 0;
|
||||
case 'OR': return (inputs[0] || inputs[1]) ? 1 : 0;
|
||||
case 'NOT': return inputs[0] ? 0 : 1;
|
||||
case 'NAND': return (inputs[0] && inputs[1]) ? 0 : 1;
|
||||
case 'NOR': return (inputs[0] || inputs[1]) ? 0 : 1;
|
||||
case 'XOR': return (inputs[0] !== inputs[1]) ? 1 : 0;
|
||||
case 'OUTPUT': return inputs[0] || 0;
|
||||
default: return 0;
|
||||
}
|
||||
gate.value = result;
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Iterative fixed-point evaluation.
|
||||
* Runs multiple passes over all gates until no values change (stable)
|
||||
* or a max iteration limit is reached. Does NOT reset gate values,
|
||||
* preserving latch/flip-flop state across evaluations.
|
||||
*/
|
||||
const MAX_ITERATIONS = 20;
|
||||
|
||||
export function evaluateAll(recordWave = false) {
|
||||
state.gates.forEach(g => {
|
||||
if (g.type !== 'INPUT' && g.type !== 'CLOCK') g.value = 0;
|
||||
});
|
||||
state.gates.forEach(g => evaluate(g));
|
||||
for (let iter = 0; iter < MAX_ITERATIONS; iter++) {
|
||||
let changed = false;
|
||||
for (const gate of state.gates) {
|
||||
if (gate.type === 'INPUT' || gate.type === 'CLOCK') continue;
|
||||
const newVal = computeGate(gate);
|
||||
if (newVal !== gate.value) {
|
||||
gate.value = newVal;
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
if (!changed) {
|
||||
console.log(`[eval] stable after ${iter + 1} iteration(s)`);
|
||||
break;
|
||||
}
|
||||
if (iter === MAX_ITERATIONS - 1) {
|
||||
console.warn(`[eval] did not stabilize after ${MAX_ITERATIONS} iterations (oscillation?)`);
|
||||
}
|
||||
}
|
||||
if (recordWave && state.recording && state.waveformVisible) recordSample();
|
||||
}
|
||||
|
||||
// Keep legacy export name for components.js internal use
|
||||
export function evaluate(gate) {
|
||||
return computeGate(gate);
|
||||
}
|
||||
|
||||
// Register evaluateAll in waveform to break circular dependency
|
||||
setEvaluateAll(evaluateAll);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user