// Gate evaluation and port geometry import { GATE_W, GATE_H, COMP_W, PORT_R, gateInputCount as baseGateInputCount, gateOutputCount as baseGateOutputCount } from './constants.js'; import { state } from './state.js'; import { recordSample, setEvaluateAll } from './waveform.js'; import { evaluateComponent } from './components.js'; // Wrappers that handle component types export function gateInputCount(type) { if (type.startsWith('COMPONENT:')) { const componentId = type.substring(10); const component = state.customComponents?.[componentId]; return component ? component.inputCount : 0; } return baseGateInputCount(type); } export function gateOutputCount(type) { if (type.startsWith('COMPONENT:')) { const componentId = type.substring(10); const component = state.customComponents?.[componentId]; return component ? component.outputCount : 0; } return baseGateOutputCount(type); } export function getComponentWidth(gate) { if (gate.type.startsWith('COMPONENT:')) { const count = Math.max(gate.component?.inputCount || 1, gate.component?.outputCount || 1); return Math.max(120, (count + 1) * 25); } return GATE_W; } export function getComponentHeight(gate) { if (gate.type.startsWith('COMPONENT:')) { const count = Math.max(gate.component?.inputCount || 1, gate.component?.outputCount || 1); return Math.max(60, (count + 1) * 25); } return GATE_H; } export function getInputPorts(gate) { const count = gateInputCount(gate.type); const ports = []; const isComponent = gate.type.startsWith('COMPONENT:'); const gateHeight = isComponent ? getComponentHeight(gate) : GATE_H; for (let i = 0; i < count; i++) { const spacing = gateHeight / (count + 1); ports.push({ x: gate.x, y: gate.y + spacing * (i + 1), index: i, type: 'input' }); } return ports; } export function getOutputPorts(gate) { const count = gateOutputCount(gate.type); const ports = []; const isComponent = gate.type.startsWith('COMPONENT:'); const gateWidth = isComponent ? getComponentWidth(gate) : GATE_W; const gateHeight = isComponent ? getComponentHeight(gate) : GATE_H; for (let i = 0; i < count; i++) { const spacing = gateHeight / (count + 1); ports.push({ x: gate.x + gateWidth, y: gate.y + spacing * (i + 1), index: i, type: 'output' }); } return ports; } export function evaluate(gate, visited = new Set()) { if (visited.has(gate.id)) return gate.value || 0; visited.add(gate.id); if (gate.type === 'INPUT' || gate.type === 'CLOCK') return gate.value; const inputCount = gateInputCount(gate.type); const inputs = []; for (let i = 0; i < inputCount; i++) { 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); } 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; } } gate.value = result; return result; } 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)); if (recordWave && state.recording && state.waveformVisible) recordSample(); } // Register evaluateAll in waveform to break circular dependency setEvaluateAll(evaluateAll); export function findGateAt(x, y) { return state.gates.find(g => { const w = g.type.startsWith('COMPONENT:') ? getComponentWidth(g) : GATE_W; const h = g.type.startsWith('COMPONENT:') ? getComponentHeight(g) : GATE_H; return x >= g.x && x <= g.x + w && y >= g.y && y <= g.y + h; }); } export function findPortAt(x, y) { for (const gate of state.gates) { for (const p of getInputPorts(gate)) { if (Math.hypot(x - p.x, y - p.y) < PORT_R + 4) return { gate, index: p.index, type: 'input' }; } for (const p of getOutputPorts(gate)) { if (Math.hypot(x - p.x, y - p.y) < PORT_R + 4) return { gate, index: p.index, type: 'output' }; } } return null; }