2010-09-18 16:56:48 +00:00
|
|
|
/*
|
|
|
|
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 frame, chipbg, overlay, hilite, hitbuffer, ctx;
|
|
|
|
var nodes = new Array();
|
|
|
|
var transistors = {};
|
2010-09-27 13:16:59 +00:00
|
|
|
var nodenamelist=[];
|
2010-09-18 16:56:48 +00:00
|
|
|
|
|
|
|
var ngnd = nodenames['vss'];
|
|
|
|
var npwr = nodenames['vcc'];
|
|
|
|
|
2010-10-09 13:43:47 +00:00
|
|
|
var chipLayoutIsVisible = true; // only modified in expert mode
|
|
|
|
|
2010-09-18 16:56:48 +00:00
|
|
|
function setupNodes(){
|
|
|
|
for(var i in segdefs){
|
|
|
|
var seg = segdefs[i];
|
|
|
|
var w = seg[0];
|
|
|
|
if(nodes[w]==undefined)
|
|
|
|
nodes[w] = {segs: new Array(), num: w, pullup: seg[1]=='+',
|
2010-10-15 01:00:07 +00:00
|
|
|
state: false, gates: new Array(), c1c2s: new Array()};
|
2010-09-18 16:56:48 +00:00
|
|
|
if(w==ngnd) continue;
|
|
|
|
if(w==npwr) continue;
|
|
|
|
nodes[w].segs.push(seg.slice(3));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function setupTransistors(){
|
|
|
|
for(i in transdefs){
|
|
|
|
var tdef = transdefs[i];
|
|
|
|
var name = tdef[0];
|
|
|
|
var gate = tdef[1];
|
|
|
|
var c1 = tdef[2];
|
|
|
|
var c2 = tdef[3];
|
2010-10-15 12:11:43 +00:00
|
|
|
if(c1==ngnd) {c1=c2;c2=ngnd;}
|
|
|
|
if(c1==npwr) {c1=c2;c2=npwr;}
|
2010-09-18 16:56:48 +00:00
|
|
|
var trans = {name: name, on: false, gate: gate, c1: c1, c2: c2};
|
2010-10-15 00:16:43 +00:00
|
|
|
nodes[gate].gates.push(trans);
|
|
|
|
nodes[c1].c1c2s.push(trans);
|
|
|
|
nodes[c2].c1c2s.push(trans);
|
2010-09-18 16:56:48 +00:00
|
|
|
transistors[name] = trans;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-18 17:46:24 +00:00
|
|
|
function setupLayerVisibility(){
|
|
|
|
var x=document.getElementById('updateShow');
|
|
|
|
for (var i=0;i<x.childNodes.length;i++) {
|
|
|
|
if(x.childNodes[i].type='checkbox'){
|
|
|
|
x.childNodes[i].checked=drawlayers[x.childNodes[i].name];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-09-18 16:56:48 +00:00
|
|
|
|
|
|
|
function setupBackground(){
|
|
|
|
chipbg = document.getElementById('chipbg');
|
2010-10-04 11:04:35 +00:00
|
|
|
chipbg.width = grCanvasSize;
|
|
|
|
chipbg.height = grCanvasSize;
|
2010-09-18 16:56:48 +00:00
|
|
|
var ctx = chipbg.getContext('2d');
|
|
|
|
ctx.fillStyle = '#000000';
|
|
|
|
ctx.strokeStyle = 'rgba(255,255,255,0.5)';
|
2010-10-04 11:04:35 +00:00
|
|
|
ctx.lineWidth = grLineWidth;
|
|
|
|
ctx.fillRect(0,0,grCanvasSize,grCanvasSize);
|
2010-09-18 16:56:48 +00:00
|
|
|
for(var i in segdefs){
|
|
|
|
var seg = segdefs[i];
|
|
|
|
var c = seg[2];
|
2010-09-18 17:46:24 +00:00
|
|
|
if (drawlayers[c]) {
|
|
|
|
ctx.fillStyle = colors[c];
|
|
|
|
drawSeg(ctx, segdefs[i].slice(3));
|
|
|
|
ctx.fill();
|
|
|
|
if((c==0)||(c==6)) ctx.stroke();
|
|
|
|
}
|
2010-09-18 16:56:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function setupOverlay(){
|
|
|
|
overlay = document.getElementById('overlay');
|
2010-10-04 11:04:35 +00:00
|
|
|
overlay.width = grCanvasSize;
|
|
|
|
overlay.height = grCanvasSize;
|
2010-09-18 16:56:48 +00:00
|
|
|
ctx = overlay.getContext('2d');
|
|
|
|
}
|
|
|
|
|
|
|
|
function setupHilite(){
|
|
|
|
hilite = document.getElementById('hilite');
|
2010-10-04 11:04:35 +00:00
|
|
|
hilite.width = grCanvasSize;
|
|
|
|
hilite.height = grCanvasSize;
|
2010-09-18 16:56:48 +00:00
|
|
|
var ctx = hilite.getContext('2d');
|
|
|
|
}
|
|
|
|
|
|
|
|
function setupHitBuffer(){
|
|
|
|
hitbuffer = document.getElementById('hitbuffer');
|
2010-10-04 11:04:35 +00:00
|
|
|
hitbuffer.width = grCanvasSize;
|
|
|
|
hitbuffer.height = grCanvasSize;
|
2010-09-18 16:56:48 +00:00
|
|
|
hitbuffer.style.visibility = 'hidden';
|
|
|
|
var ctx = hitbuffer.getContext('2d');
|
|
|
|
for(i in nodes) hitBufferNode(ctx, i, nodes[i].segs);
|
|
|
|
}
|
|
|
|
|
|
|
|
function hitBufferNode(ctx, i, w){
|
|
|
|
var low = hexdigit(i&0xf);
|
|
|
|
var mid = hexdigit((i>>4)&0xf);
|
|
|
|
var high = hexdigit((i>>8)&0xf);
|
|
|
|
ctx.fillStyle = '#'+high+'F'+mid+'F'+low+'F';
|
|
|
|
for(i in w) {
|
|
|
|
drawSeg(ctx, w[i]);
|
|
|
|
ctx.fill();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function hexdigit(n){return '0123456789ABCDEF'.charAt(n);}
|
|
|
|
|
|
|
|
|
|
|
|
/////////////////////////
|
|
|
|
//
|
|
|
|
// Drawing Runtime
|
|
|
|
//
|
|
|
|
/////////////////////////
|
|
|
|
|
|
|
|
function refresh(){
|
2010-10-09 13:43:47 +00:00
|
|
|
if(!chipLayoutIsVisible) return;
|
2010-10-04 11:04:35 +00:00
|
|
|
ctx.clearRect(0,0,grCanvasSize,grCanvasSize);
|
2010-09-18 16:56:48 +00:00
|
|
|
for(i in nodes){
|
|
|
|
if(isNodeHigh(i)) overlayNode(nodes[i].segs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function overlayNode(w){
|
|
|
|
ctx.fillStyle = 'rgba(255,0,64,0.4)';
|
|
|
|
for(i in w) {
|
|
|
|
drawSeg(ctx, w[i]);
|
|
|
|
ctx.fill();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function hiliteNode(n){
|
|
|
|
var ctx = hilite.getContext('2d');
|
2010-10-04 11:04:35 +00:00
|
|
|
ctx.clearRect(0,0,grCanvasSize,grCanvasSize);
|
2010-09-18 16:56:48 +00:00
|
|
|
ctx.fillStyle = 'rgba(255,255,255,0.7)';
|
|
|
|
if(n==-1) return;
|
|
|
|
if(isNodeHigh(n[0]))
|
|
|
|
ctx.fillStyle = 'rgba(255,0,0,0.7)';
|
|
|
|
|
|
|
|
for(var i in n){
|
|
|
|
var segs = nodes[n[i]].segs;
|
|
|
|
for(var s in segs){drawSeg(ctx, segs[s]); ctx.fill();}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function drawSeg(ctx, seg){
|
|
|
|
var dx = 400;
|
|
|
|
ctx.beginPath();
|
2010-10-04 11:04:35 +00:00
|
|
|
ctx.moveTo(grScale(seg[0]+dx), grScale(grChipSize-seg[1]));
|
|
|
|
for(var i=2;i<seg.length;i+=2) ctx.lineTo(grScale(seg[i]+dx), grScale(grChipSize-seg[i+1]));
|
|
|
|
ctx.lineTo(grScale(seg[0]+dx), grScale(grChipSize-seg[1]));
|
2010-09-18 16:56:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function findNodeNumber(x,y){
|
|
|
|
var ctx = hitbuffer.getContext('2d');
|
2010-10-04 11:04:35 +00:00
|
|
|
var pixels = ctx.getImageData(x*grCanvasSize/600, y*grCanvasSize/600, 2, 2).data;
|
2010-09-18 16:56:48 +00:00
|
|
|
if(pixels[0]==0) return -1;
|
|
|
|
var high = pixels[0]>>4;
|
|
|
|
var mid = pixels[1]>>4;
|
|
|
|
var low = pixels[2]>>4;
|
|
|
|
return (high<<8)+(mid<<4)+low;
|
|
|
|
}
|
|
|
|
|
2010-09-27 17:25:14 +00:00
|
|
|
function clearHighlight(){
|
|
|
|
// remove red/white overlay according to logic value
|
|
|
|
// for easier layout navigation
|
2010-10-04 11:04:35 +00:00
|
|
|
ctx.clearRect(0,0,grCanvasSize,grCanvasSize);
|
2010-09-27 17:25:14 +00:00
|
|
|
}
|
|
|
|
|
2010-09-18 17:46:24 +00:00
|
|
|
function updateShow(layer, on){
|
|
|
|
drawlayers[layer]=on;
|
|
|
|
setupBackground();
|
|
|
|
}
|
|
|
|
|
2010-10-04 11:04:35 +00:00
|
|
|
// we draw the chip data scaled down to the canvas
|
|
|
|
// and so avoid scaling a large canvas
|
|
|
|
function grScale(x){
|
|
|
|
return Math.round(x*grCanvasSize/grChipSize);
|
|
|
|
}
|
2010-09-18 16:56:48 +00:00
|
|
|
|
|
|
|
function localx(el, gx){
|
2010-09-19 18:27:48 +00:00
|
|
|
return gx-el.getBoundingClientRect().left;
|
2010-09-18 16:56:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function localy(el, gy){
|
2010-09-19 18:27:48 +00:00
|
|
|
return gy-el.getBoundingClientRect().top;
|
2010-09-18 16:56:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function setStatus(){
|
|
|
|
var res = '';
|
2010-10-02 16:36:02 +00:00
|
|
|
// pad the arguments to make this a three-line display
|
|
|
|
// there must be a clean way to do this
|
|
|
|
if(arguments[1]==undefined)arguments[1]="";
|
|
|
|
if(arguments[2]==undefined)arguments[2]="";
|
|
|
|
arguments.length=3;
|
|
|
|
for(var i=0;i<arguments.length;i++) res=res+arguments[i]+'<br>';
|
2010-09-18 16:56:48 +00:00
|
|
|
statbox.innerHTML = res;
|
|
|
|
}
|
|
|
|
|
2010-09-27 13:16:59 +00:00
|
|
|
function setupNodeNameList(){
|
|
|
|
for(var i in nodenames)
|
|
|
|
nodenamelist.push(i);
|
|
|
|
}
|
|
|
|
|
2010-09-18 16:56:48 +00:00
|
|
|
function nodeName(n) {
|
|
|
|
for(var i in nodenames){
|
|
|
|
if(nodenames[i]==n) return i;
|
|
|
|
}
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
function now(){return new Date().getTime();}
|