-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathquartz.js
1 lines (1 loc) · 3.9 KB
/
quartz.js
1
"use strict";function rng(){return seed^=seed>>11,seed^=seed<<8,seed^=seed>>19,255&seed}function input(){return inputs[run.input_pos++]}function output(a){run.outputs.push(a)}function quarts(a){return[a>>6&3,a>>4&3,a>>2&3,3&a]}function prettify(a){return void 0===a?"":quarts(a).join("")}function redraw(){$$("#registers td").forEach(function(a,b){a.innerText=prettify(r[b])}),$$("#memory td").forEach(function(a,b){a.innerText=prettify(memory[b]),a.classList.toggle("highlighted",running&&(b==r[3]||b==r[2])),a.classList.toggle("error",1==errors[b])}),$$("#inputs td").forEach(function(a,b){a.innerText=inputs[b],a.classList.toggle("highlighted",running&&b==run.input_pos)}),$$("#outputs td").forEach(function(a,b){void 0!==run.outputs[b]?a.innerText=run.outputs[b].toString():a.innerText=""}),running&&-1!=line_nums.indexOf(r[3])&&ace.gotoLine(line_nums.indexOf(r[3])+1),$$(".ace_line").forEach(function(a,b){})}function compile(){memory.fill(0),errors.fill(0),line_nums=[];for(var a=0,b=0,c=ace.getValue().split("\n");b<c.length;b++){var d=c[b],e=d.replace(/;.*/,"").match(/\S+/g);if(e)if(1==e.length&&e[0].match(/^@\d+$/))line_nums.push(-1),a=+e[0].replace(/@/,"");else if(1==e.length&&e[0].match(/^\d+$/))line_nums.push(a),memory[a++]=+e[0];else{var f=mnemonics.indexOf(e[0]),g=argParser[e[1]],h=argParser[e[2]];(-1==f||void 0===g||void 0===h)&&(errors[a]=1),line_nums.push(a),memory[a++]=f<<4|g<<2|h}else line_nums.push(-1)}redraw()}function reset(a){running=!1,ace.setReadOnly(!1),r.fill(0),run.cycle=0,run.input_pos=0,run.outputs=[],run.breakpoint=-1,compile()}function step(a){if(!running){if(!errors.every(function(a){return!a}))return;return running=!0,ace.setReadOnly(!0),void redraw()}var b=memory[r[3]++],c=b>>4,d=b>>2&3,e=3&b;ops[c](d,e),++run.cycle,a&&redraw()}function fast(a){if(a||running){for(var b=0;64>b;++b)if(step(null),r[3]==run.breakpoint)return void redraw();redraw(),setTimeout(fast,0)}}function keyboard_shortcuts(a){switch(a.key){case"Tab":a.preventDefault(),a.stopPropagation(),step(a);break;case"Escape":a.preventDefault(),a.stopPropagation(),reset(a);break;case"F4":a.preventDefault(),a.stopPropagation(),fast(a)}}function init(){$("#step").addEventListener("click",step),$("#reset").addEventListener("click",reset),$("#fast").addEventListener("click",fast),ace=ace.edit("source",{mode:"ace/mode/assembly_x86",fontSize:14}),ace.setValue(localStorage.code||""),ace.selection.clearSelection(),ace.on("change",function(){localStorage.code=ace.getValue(),compile()}),ace.on("click",function(a){a.domEvent.ctrlKey&&(run.breakpoint=line_nums[a.$pos.row],fast(a))}),document.addEventListener("keydown",keyboard_shortcuts,!0),compile()}var abs=Math.abs,ceil=Math.ceil,floor=Math.floor,log=Math.log,max=Math.max,min=Math.min,pow=Math.pow,round=Math.round,sqrt=Math.sqrt,$=function(a){return document.querySelector(a)},$$=function(a){return[].slice.apply(document.querySelectorAll(a))},seed=42,r=new Uint8Array(4),memory=new Uint8Array(256),errors=new Uint8Array(256),inputs=Array.apply(null,Array(16)).map(rng),running=!1;inputs[5]--,inputs[6]=0,inputs[15]=0;var line_nums=[],run={cycle:0,input_pos:0,outputs:[],breakpoint:-1},ops=[function(a,b){return r[a]=r[b]},function(a,b){return c=[r[b],r[a]],r[a]=c[0],r[b]=c[1],c;var c},function(a,b){return r[a]=+(r[a]==r[b])},function(a,b){return r[a]=+!r[a]},function(a,b){return r[a]+=r[b]},function(a,b){return r[a]=r[a]>r[b]?r[a]-r[b]:0},function(a,b){return r[a]*=r[b]},function(a,b){return r[a]/=r[b]},function(a,b){return r[a]+=1},function(a,b){return r[a]-=1},function(a,b){return r[a]=r[2]>>2*b&3},function(a,b){r[2]&=~(3<<2*b),r[2]|=a<<2*b},function(a,b){return r[a]=memory[r[b]]},function(a,b){return memory[r[b]]=r[a]},function(a,b){return r[a]=input()},function(a,b){return output(r[a])}],mnemonics=["copy","swap","cmp","not","add","sub","mul","div","inc","dec","qget","qset","load","save","recv","send"],argParser={A:0,B:1,Q:2,I:3,0:0,1:1,2:2,3:3,undefined:0};document.addEventListener("DOMContentLoaded",init);