/* Copyright (c) 2010 Brian Silverman, Barry Silverman, Ed Spittles, Achim Breidenbach 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 memory = Array(); var cycle = 0; var trace = Array(); var logstream = Array(); var running = false; var logThese=[]; var chipname='6502'; var nodenamereset='res'; var presetLogLists=[ ['cycle'], ['ab','db','rw','Fetch','pc','a','x','y','s','p'], ['Execute','State'], ['ir','tcstate','-pd'], ['adl','adh','sb','alu'], ['alucin','alua','alub','alucout','aluvout','dasb'], ['plaOutputs','DPControl'], ['idb','dor'], ['irq','nmi',nodenamereset], ]; function loadProgram(){ // a moderate size of static testprogram might be loaded if(testprogram.length!=0 && testprogramAddress != undefined) for(var i=0;testprogram[i]!=undefined;i++){ var a=testprogramAddress+i; mWrite(a, testprogram[i]); if(a<0x200) setCellValue(a, testprogram[i]); } // a small test program or patch might be passed in the URL if(userCode.length!=0) for(var i=0;i>1; if(busname=='pc') return busToHex('pch') + busToHex('pcl'); if(busname=='p') return readPstring(); if(busname=='tcstate') return ['clock1','clock2','t2','t3','t4','t5'].map(busToHex).join(""); if(busname=='State') return listActiveTCStates(); if(busname=='TState') return allTCStates( true ); if(busname=='Phi') // Pretty-printed phase indication based on the state of cp1, // the internal Phase 1 node return 'Φ' + (isNodeHigh( nodenames[ 'cp1' ] ) ? '1' : '2'); if(busname=='Execute') return disassemblytoHTML(readBits('ir',8)); if(busname=='Fetch') return isNodeHigh(nodenames['sync'])?disassemblytoHTML(readDataBus()):""; if(busname=='plaOutputs') // PLA outputs are mostly ^op- but some have a prefix too // - we'll allow the x and xx prefix but ignore the # return listActiveSignals('^([x]?x-)?op-'); if(busname=='DPControl') return listActiveSignals('^dpc[-]?[0-9]+_'); if(busname[0]=="-"){ // invert the value of the bus for display var value=busToHex(busname.slice(1)) if(typeof value != "undefined") return value.replace(/./g,function(x){return (15-parseInt(x,16)).toString(16)}); else return undefined;; } else { return busToHex(busname); } } function busToHex(busname){ // may be passed a bus or a signal, so allow multiple signals var width=0; var r=new RegExp('^' + busname + '[0-9]+$'); for(var i in nodenamelist){ if(r.test(nodenamelist[i])) { width++; } } if(width==0) { // not a bus, so could be a signal, a nodenumber or a mistake if(typeof nodenames[busname] != "undefined") return isNodeHigh(nodenames[busname])?"1":"0"; if((parseInt(busname)!=NaN) && (typeof nodes[busname] != "undefined")) return isNodeHigh(busname)?"1":"0"; return undefined; } if(width>16) return undefined; // finally, convert from logic values to hex return (0x10000+readBits(busname,width)).toString(16).slice(-(width-1)/4-1); } function writeDataBus(x){ var recalcs = Array(); for(var i=0;i<8;i++){ var nn = nodenames['db'+i]; var n = nodes[nn]; if((x%2)==0) {n.pulldown=true; n.pullup=false;} else {n.pulldown=false; n.pullup=true;} recalcs.push(nn); x>>=1; } recalcNodeList(recalcs); } function mRead(a){ if(memory[a]==undefined) return 0; else return memory[a]; } function mWrite(a, d){memory[a]=d;} function clkNodes(){ var res = Array(); res.push(943); for(var i in nodes[943].gates){ var t = nodes[943].gates[i]; if(t.c1==npwr) res.push(t.c2); if(t.c2==npwr) res.push(t.c1); } hiliteNode(res); } function runChip(){ var start = document.getElementById('start'); var stop = document.getElementById('stop'); start.style.visibility = 'hidden'; stop.style.visibility = 'visible'; if(typeof running == "undefined") initChip(); running = true; go(); } function stopChip(){ var start = document.getElementById('start'); var stop = document.getElementById('stop'); start.style.visibility = 'visible'; stop.style.visibility = 'hidden'; running = false; } function resetChip(){ stopChip(); setStatus('resetting ' + chipname + '...'); setTimeout(initChip,0); } function stepForward(){ if(typeof running == "undefined") initChip(); stopChip(); step(); } function stepBack(){ if(cycle==0) return; showState(trace[--cycle].chip); setMem(trace[cycle].mem); var clk = isNodeHigh(nodenames['clk0']); if(!clk) writeDataBus(mRead(readAddressBus())); chipStatus(); } function chipStatus(){ var ab = readAddressBus(); var machine1 = ' halfcyc:' + cycle + ' phi0:' + readBit('clk0') + ' AB:' + hexWord(ab) + ' D:' + hexByte(readDataBus()) + ' RnW:' + readBit('rw'); var machine2 = ' PC:' + hexWord(readPC()) + ' A:' + hexByte(readA()) + ' X:' + hexByte(readX()) + ' Y:' + hexByte(readY()) + ' SP:' + hexByte(readSP()) + ' ' + readPstring(); var machine3 = 'Hz: ' + estimatedHz().toFixed(1); if(typeof expertMode != "undefined") { machine3 += ' Exec: ' + busToString('Execute') + '(' + busToString('State') + ')'; if(isNodeHigh(nodenames['sync'])) machine3 += ' (Fetch: ' + busToString('Fetch') + ')'; if(goldenChecksum != undefined) machine3 += " Chk:" + traceChecksum + ((traceChecksum==goldenChecksum)?" OK":" no match"); } setStatus(machine1, machine2, machine3); if (logThese.length>1) { updateLogbox(logThese); } selectCell(ab); } // run for an extended number of cycles, with low overhead, for interactive programs or for benchmarking // note: to run an interactive program, use an URL like // http://visual6502.org/JSSim/expert.html?graphics=f&loglevel=-1&headlesssteps=-500 function goFor(){ var n = headlessSteps; // a negative value is a request to free-run if(headlessSteps<0) n=-n; var start = document.getElementById('start'); var stop = document.getElementById('stop'); start.style.visibility = 'hidden'; stop.style.visibility = 'visible'; if(typeof running == "undefined") { initChip(); } running = true; setTimeout("instantaneousHz(); goForN("+n+")",0); } // helper function: allows us to poll 'running' without resetting it when we're re-scheduled function goForN(n){ var n2=n; // save our parameter so we can re-submit ourselves while(n--){ halfStep(); cycle++; } instantaneousHz(); chipStatus(); if((headlessSteps<0) && running){ setTimeout("goForN("+n2+")",0); // re-submit ourselves if we are meant to free-run return; } running = false; var start = document.getElementById('start'); var stop = document.getElementById('stop'); start.style.visibility = 'visible'; stop.style.visibility = 'hidden'; } var prevHzTimeStamp=0; var prevHzCycleCount=0; var prevHzEstimate1=1; var prevHzEstimate2=1; var HzSamplingRate=10; // return an averaged speed: called periodically during normal running function estimatedHz(){ if(cycle%HzSamplingRate!=3) return prevHzEstimate1; var HzTimeStamp = now(); var HzEstimate = (cycle-prevHzCycleCount+.01)/(HzTimeStamp-prevHzTimeStamp+.01); HzEstimate=HzEstimate*1000/2; // convert from phases per millisecond to Hz if(HzEstimate<5) HzSamplingRate=5; // quicker if(HzEstimate>10) HzSamplingRate=10; // smoother prevHzEstimate2=prevHzEstimate1; prevHzEstimate1=(HzEstimate+prevHzEstimate1+prevHzEstimate2)/3; // wrong way to average speeds prevHzTimeStamp=HzTimeStamp; prevHzCycleCount=cycle; return prevHzEstimate1 } // return instantaneous speed: called twice, before and after a timed run using goFor() function instantaneousHz(){ var HzTimeStamp = now(); var HzEstimate = (cycle-prevHzCycleCount+.01)/(HzTimeStamp-prevHzTimeStamp+.01); HzEstimate=HzEstimate*1000/2; // convert from phases per millisecond to Hz prevHzEstimate1=HzEstimate; prevHzEstimate2=prevHzEstimate1; prevHzTimeStamp=HzTimeStamp; prevHzCycleCount=cycle; return prevHzEstimate1 } var logbox; function initLogbox(names){ logbox=document.getElementById('logstream'); if(logbox==null)return; names=names.map(function(x){return x.replace(/^-/,'')}); logStream = []; logStream.push("" + names.join("") + ""); logbox.innerHTML = ""+logStream.join("")+""; } var logboxAppend=true; // can append or prepend new states to the log table // when we reverse direction we need to reorder the log stream function updateLogDirection(){ var loglines=[]; logboxAppend=!logboxAppend; // the first element is the header so we can't reverse() for (var i=1;i")+""; } // update the table of signal values, by prepending or appending function updateLogbox(names){ var signals=[]; var odd=true; var bg; var row; for(var i in names){ if(cycle % 4 < 2){ bg = odd ? " class=oddcol":""; } else { bg = odd ? " class=oddrow":" class=oddrowcol"; } signals.push("" + busToString(names[i]) + ""); odd =! odd; } row = "" + signals.join("") + ""; if(logboxAppend) logStream.push(row); else logStream.splice(1,0,row); logbox.innerHTML = logStream.join(""); } function getMem(){ var res = Array(); for(var i=0;i<0x200;i++) res.push(mRead(i)); return res; } function setMem(arr){ for(var i=0;i<0x200;i++){mWrite(i, arr[i]); setCellValue(i, arr[i]);} } function hexWord(n){return (0x10000+n).toString(16).substring(1)} function hexByte(n){return (0x100+n).toString(16).substring(1)} function adler32(x){ var a=1; var b=0; for(var i=0;i