visual6502/chipsim.js

197 lines
4.9 KiB
JavaScript
Raw Normal View History

/*
Copyright (c) 2010 Brian Silverman, Barry Silverman
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
var ctrace = false;
var traceTheseNodes = [];
var traceTheseTransistors = [];
2010-09-25 10:29:26 +00:00
var loglevel = 0;
var recalclist = new Array();
var recalcHash = new Array();
var group = new Array();
function recalcNodeList(list){
var n = list[0];
recalclist = new Array();
recalcHash = new Array();
for(var j=0;j<100;j++){ // loop limiter
if(list.length==0) return;
if(ctrace) {
var i;
for(i=0;i<traceTheseNodes.length;i++) {
if(list.indexOf(traceTheseNodes[i])!=-1) break;
}
if((traceTheseNodes.length==0)||(list.indexOf(traceTheseNodes[i])==-1)) {
console.log('recalcNodeList iteration: ', j, list.length, 'nodes');
} else {
console.log('recalcNodeList iteration: ', j, list.length, 'nodes', list);
}
}
2010-10-15 01:15:47 +00:00
list.forEach(recalcNode);
list = recalclist;
recalclist = new Array();
recalcHash = new Array();
}
2010-09-19 07:02:26 +00:00
if(ctrace) console.log(n,'looping...');
}
function recalcNode(node){
if(node==ngnd) return;
if(node==npwr) return;
2010-10-15 12:11:43 +00:00
getNodeGroup(node);
var newState = getNodeValue();
if(ctrace && (traceTheseNodes.indexOf(node)!=-1))
console.log('recalc', node, group);
2010-10-15 12:11:43 +00:00
group.forEach(function(i){
var n = nodes[i];
if(n.state==newState) return;
n.state = newState;
2010-10-15 12:11:43 +00:00
n.gates.forEach(function(t){
if(n.state) turnTransistorOn(t);
else turnTransistorOff(t);});
});
}
function turnTransistorOn(t){
if(t.on) return;
if(ctrace && (traceTheseTransistors.indexOf(t.name)!=-1))
console.log(t.name, 'on', t.gate, t.c1, t.c2);
t.on = true;
addRecalcNode(t.c1);
}
function turnTransistorOff(t){
if(!t.on) return;
if(ctrace && (traceTheseTransistors.indexOf(t.name)!=-1))
console.log(t.name, 'off', t.gate, t.c1, t.c2);
t.on = false;
addRecalcNode(t.c1);
addRecalcNode(t.c2);
}
function addRecalcNode(nn){
2010-10-15 01:00:07 +00:00
if(nn==ngnd) return;
if(nn==npwr) return;
if(recalcHash[nn] == 1)return;
recalclist.push(nn);
recalcHash[nn] = 1;
}
function getNodeGroup(i){
group = new Array();
addNodeToGroup(i);
}
function addNodeToGroup(i){
if(group.indexOf(i) != -1) return;
group.push(i);
if(i==ngnd) return;
if(i==npwr) return;
2010-10-15 12:11:43 +00:00
nodes[i].c1c2s.forEach(
function(t){
2010-10-15 12:11:43 +00:00
if(!t.on) return;
var other;
if(t.c1==i) other=t.c2;
if(t.c2==i) other=t.c1;
addNodeToGroup(other);});
}
function getNodeValue(){
2010-10-15 01:00:07 +00:00
if(arrayContains(group, ngnd)) return false;
if(arrayContains(group, npwr)) return true;
2010-10-15 12:11:43 +00:00
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;
}
2010-10-15 01:00:07 +00:00
return false;
}
function isNodeHigh(nn){
return(nodes[nn].state);
}
function saveString(name, str){
var request = new XMLHttpRequest();
request.onreadystatechange=function(){};
request.open('PUT', 'save.php?name='+name, true);
request.setRequestHeader('Content-Type', 'text/plain');
request.send(str);
}
function allNodes(){
var res = new Array();
for(var i in nodes) if((i!=npwr)&&(i!=ngnd)) res.push(i);
return res;
}
function stateString(){
2010-10-15 01:00:07 +00:00
var codes = ['l','h'];
var res = '';
for(var i=0;i<1725;i++){
var n = nodes[i];
if(n==undefined) res+='x';
else if(i==ngnd) res+='g';
2010-10-15 01:00:07 +00:00
else if(i==npwr) res+='v';
else res+= codes[0+n.state];
}
return res;
}
function showState(str){
2010-10-15 01:00:07 +00:00
var codes = {g: false, h: true, v: true, l: false};
for(var i=0;i<str.length;i++){
if(str[i]=='x') continue;
2010-10-15 01:00:07 +00:00
var state = codes[str[i]];
nodes[i].state = state;
var gates = nodes[i].gates;
2010-10-15 01:00:07 +00:00
gates.forEach(function(t){t.on=state;});
}
refresh();
}
function setPd(name){
var nn = nodenames[name];
nodes[nn].pullup = false;
nodes[nn].pulldown = true;
}
function setHigh(name){
var nn = nodenames[name];
nodes[nn].pullup = true;
nodes[nn].pulldown = false;
recalcNodeList([nn]);
}
function setLow(name){
var nn = nodenames[name];
nodes[nn].pullup = false;
nodes[nn].pulldown = true;
recalcNodeList([nn]);
}
function arrayContains(arr, el){return arr.indexOf(el)!=-1;}