From df6aaa392ccfc335d4368c6fd0acaa2bab2a0dde Mon Sep 17 00:00:00 2001 From: Barry Silverman Date: Fri, 15 Oct 2010 08:11:43 -0400 Subject: [PATCH] More simplifications from Brian --- chipsim.js | 57 +++++++++++++++++++++--------------------------------- wires.js | 2 ++ 2 files changed, 24 insertions(+), 35 deletions(-) diff --git a/chipsim.js b/chipsim.js index 857ff39..59f630c 100644 --- a/chipsim.js +++ b/chipsim.js @@ -56,20 +56,18 @@ function recalcNodeList(list){ function recalcNode(node){ if(node==ngnd) return; if(node==npwr) return; - group = getNodeGroup(node); - var newState = getNodeValue(group); + getNodeGroup(node); + var newState = getNodeValue(); if(ctrace && (traceTheseNodes.indexOf(node)!=-1)) console.log('recalc', node, group); - for(var i in group){ - var n = nodes[group[i]]; - if(n.state==newState)continue; /******Performance********/ + group.forEach(function(i){ + var n = nodes[i]; + if(n.state==newState) return; n.state = newState; - if(n.state){ - n.gates.forEach(turnTransistorOn); - } else { - n.gates.forEach(turnTransistorOff); - } - } + n.gates.forEach(function(t){ + if(n.state) turnTransistorOn(t); + else turnTransistorOff(t);}); + }); } function turnTransistorOn(t){ @@ -78,7 +76,6 @@ function turnTransistorOn(t){ console.log(t.name, 'on', t.gate, t.c1, t.c2); t.on = true; addRecalcNode(t.c1); - addRecalcNode(t.c2); } function turnTransistorOff(t){ @@ -101,7 +98,6 @@ function addRecalcNode(nn){ function getNodeGroup(i){ group = new Array(); addNodeToGroup(i); - return group; } function addNodeToGroup(i){ @@ -109,35 +105,26 @@ function addNodeToGroup(i){ group.push(i); if(i==ngnd) return; if(i==npwr) return; - addNodeToGroup1(i); -} - -function addNodeToGroup1(i){ - var output=nodes[i].c1c2s; - output.forEach( + nodes[i].c1c2s.forEach( function(t){ - if(t.on)addNodeTransistor(i,t); - }); -} - -function addNodeTransistor(node, tr){ - var other; - if(tr.c1==node) other=tr.c2; - if(tr.c2==node) other=tr.c1; - addNodeToGroup(other); + if(!t.on) return; + var other; + if(t.c1==i) other=t.c2; + if(t.c2==i) other=t.c1; + addNodeToGroup(other);}); } function getNodeValue(){ if(arrayContains(group, ngnd)) return false; if(arrayContains(group, npwr)) return true; - for(var i in group){ - var nn = group[i]; - var n = nodes[nn]; - if(n.pullup) return true; - if(n.pulldown) return false; - if(n.state) return true; - } + for(var i in group){ + var nn = group[i]; + var n = nodes[nn]; + if(n.pullup) return true; + if(n.pulldown) return false; + if(n.state) return true; + } return false; } diff --git a/wires.js b/wires.js index a1b0d98..5685bec 100644 --- a/wires.js +++ b/wires.js @@ -50,6 +50,8 @@ function setupTransistors(){ var gate = tdef[1]; var c1 = tdef[2]; var c2 = tdef[3]; + if(c1==ngnd) {c1=c2;c2=ngnd;} + if(c1==npwr) {c1=c2;c2=npwr;} var trans = {name: name, on: false, gate: gate, c1: c1, c2: c2}; nodes[gate].gates.push(trans); nodes[c1].c1c2s.push(trans);