logo

ibnjs

Unnamed repository; edit this file 'description' to name the repository.
commit: 9e263926d4bdd551028b19345367ffa7b7a30053
Author: Adrian Siekierka <asiekierka@gmail.com>
Date:   Tue, 27 Dec 2011 02:26:20 +0100

First Commit

Diffstat:

ACOPYING14++++++++++++++
AREADME5+++++
Aibniz.html525+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 544 insertions(+), 0 deletions(-)

diff --git a/COPYING b/COPYING @@ -0,0 +1,14 @@ + DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE + Version 2, December 2004 + + Copyright (C) 2004 Sam Hocevar <sam@hocevar.net> + + Everyone is permitted to copy and distribute verbatim or modified + copies of this license document, and changing it is allowed as long + as the name is changed. + + DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. You just DO WHAT THE FUCK YOU WANT TO. + diff --git a/README b/README @@ -0,0 +1,5 @@ +ibnjs +--- + +A little project I made when I was bored and is kind of working. +-- asiekierka diff --git a/ibniz.html b/ibniz.html @@ -0,0 +1,525 @@ +<html> +<head> +</head> +<body> +<canvas id="ibniz" width="256" height="256"> +Why u no canvas +</canvas><br> +<form> + <input type="text" id="code" style="width: 256px"> +</form> +<b>IMPLEMENTED:</b> Arithmetic, stack, memory, if/then/endif<br> +<div id="fps"><b>FPS:</b> [implementing, give me a second]</div> +<b>DEBUG:</b><br> +<script type="text/javascript"> +/* This program is free software. It comes without any warranty, to + * the extent permitted by applicable law. You can redistribute it + * and/or modify it under the terms of the Do What The Fuck You Want + * To Public License, Version 2, as published by Sam Hocevar. See + * http://sam.zoy.org/wtfpl/COPYING for more details. */ + +function Stack (len) +{ + //var buffer = new ArrayBuffer(4096); + //this.stack = new Int32Array(buffer); + this.stack = new Array(len); + this.pos = -1; + this.push = function(v){ + if(this.pos==4095) return -1; + this.pos++; + this.stack[this.pos]=v|0; + return 0; + } + this.pop = function(){ + if(this.pos==-1) return 0; + this.pos--; + return this.stack[this.pos+1]; + } + this.get = function(addr) { + return this.stack[addr&4095]; + } + this.gettop = function(addr) { + return this.stack[(this.pos-addr)&4095]; + } + this.put = function(addr,v) { + this.stack[addr&4095]=v|0; + } + this.puttop = function(addr,v) { + this.stack[(this.pos-addr)&4095]=v|0; + } + this.clear = function(){ + this.pos=-1; + } + this.size = function(){ + return this.pos+1; + } + this.exchange = function(){ + if(this.pos<1) return -1; + var temp = this.stack[this.pos]; + this.stack[this.pos]=this.stack[this.pos-1]; + this.stack[this.pos-1]=temp; + return 0; + } + this.trirot = function(){ + if(this.pos<2) return -1; + var temp = this.stack[this.pos]; + this.stack[this.pos]=this.stack[this.pos-2]; + this.stack[this.pos-2]=this.stack[this.pos-1]; + this.stack[this.pos-1]=temp; + return 0; + } + this.dup = function(){ + return this.push(this.stack[this.pos]); + } + this.debug = function(){ + document.write("Testing Stack...<br>"); + this.push(32); + document.write("[BASIC] 1/6: " + this.pop() + " == 32<br>"); + this.push(64); + this.push(25); + this.exchange(); + document.write("[EXCH] 2/6: " + this.pop() + " == 64<br>"); + document.write("[EXCH] 3/6: " + this.pop() + " == 25<br>"); + this.push(33); + this.push(22); + this.push(11); + this.trirot(); + document.write("[TRIROT] 4/6: " + this.pop() + " == 33<br>"); + this.pop(); + this.pop(); + document.write("[POP] 5/6: " + this.pop() + " == 0<br>"); + document.write("[EXC2] 6/6: " + this.exchange() + " == -1<br>"); + } + this.print = function(){ + var str = ""; + for(var i=0;i<=this.pos;i++) + { + str = str + this.stack[i] + ","; + } + return str; + } +} +function Memory () +{ + this.mem = new Array(1024*1024); + this.get = function(addr){ + if(addr<0xC8000) + return this.mem[addr&1048575]; + else if(addr>=0xE0000) + return this.stack.get(addr&0x1FFFF); + else if(addr>=0xD0000) + return this.stack.get(addr&0xFFFF); + else + return this.retstack.get(addr&0x3FFF); + } + this.put = function(addr,val){ + if(addr<0xC8000) + this.mem[addr&1048575] = val;\ + else if(addr>=0xE0000) + this.stack.put(addr&0x1FFFF,val); + else if(addr>=0xD0000) + this.stack.put(addr&0xFFFF,val); + else + this.retstack.put(addr&0x3FFF,val); + } +} +function ALU () +{ + this.add = function(a,b){ + return a+b; + } + this.sub = function(a,b){ + return b-a; + } + this.mul = function(a,b){ + return (a*b)/65536; + } + this.div = function(a,b){ + return (b*65536)/a; + } + this.mod = function(a,b){ + return b%a; + } + this.sqrt = function(a){ + if(a<0) return 0; + else return Math.sqrt(a/65536.0)*65536.0; + } + this.and = function(a,b){ + return a&b; + } + this.or = function(a,b){ + return a|b; + } + this.xor = function(a,b){ + return a^b; + } + this.not = function(a){ + return ~a; + } + this.sin = function(a){ + return Math.sin(a*(2*Math.PI/65536))*65536; + } + this.atan2 = function(a,b){ + return Math.atan2(a,b)*(65536/(2*Math.PI)); + } + this.isneg = function(a){ + return a<0?a:0; + } + this.ispos = function(a){ + return a>0?a:0; + } + this.iszero = function(a){ + return a==0?1:0; + } + this.shl = function(a){ + var steps = (a>>16)&63; + if(steps<32) return b<<steps; + else return b>>(steps-32); + } + this.ror = function(a,b){ + var steps = (a>>16)&31; + return ((b>>>steps)|(b<<(32-steps))); + } + this.a2f = function(a){ + return (a/65536); + } + this.f2a = function(a){ + return Math.round(a*65536); + } +} +function Parser() +{ + this.code = ""; + this.ip = 0; + this.t = 0; + this.x = 0; + this.y = 0; + this.xy = 0; + this.mode = 0; + this.terminate = 0; + this.stackmode = 0; + this.videoout = 0; + this.audioout = 0; + this.stack = new Stack(0x20000); + this.retstack = new Stack(0x4000); + this.alu = new ALU(); + this.mem = new Memory(); + this.mem.stack = this.stack; + this.mem.retstack = this.retstack; + this.load = function(c) + { + this.code=c; this.configureStackmode(); + } + this.pushmedia = function() + { + if(this.mode==0) + switch(this.stackmode) + { + case 0: + this.stack.push(this.alu.f2a(Math.floor(this.t))); + this.stack.push(this.alu.f2a((this.y/128)-1)); + this.stack.push(this.alu.f2a((this.x/128)-1)); + break; + case 1: + this.stack.push(this.alu.f2a(Math.floor(this.t)) | ((y&255)<<8) | (x&255)); + break; + } + else this.stack.push(this.alu.f2a(this.t)); + } + this.run = function() + { + // reset the machine + this.mode = 0; + this.terminate = 0; + this.stack.clear(); + this.ip = 0; + this.xy = (y<<8)|x; + // push media context + this.pushmedia(); + // loop + while(this.terminate!=1) + { + this.execOne(); + this.ip++; + } + if(this.mode==0) + { + // run audio, too + this.videoout = this.stack.pop(); + /* + this.mode=1; + this.terminate = 0; + this.stack.clear(); + this.ip = 0; + this.pushmedia(); + while(this.terminate!=1) + { + this.execOne(); + this.ip++; + } + */ + } + this.audioout = this.stack.pop(); + } + this.configureStackmode = function() + { + // reset the machine + this.mode = 0; + this.stackmode = 0; + this.terminate = 0; + this.stack.clear(); + this.ip = 0; + // push media context + this.pushmedia(); + // loop + while(this.terminate!=1) + { + this.execOne(); + this.ip++; + } + if(this.stack.pos>0) this.stackmode = 1; + } + this.isLimm = function(ci) + { + // is 0-9? A-F? .? + return ((ci>=48 && ci<=57) || (ci>=65 && ci<=70) || (ci==46)); + } + this.execOne = function() + { + if(this.terminate==1) return; + if(this.ip>=this.code.length) + { + this.terminate=1; + return; + } + var chr = this.code[this.ip]; + var a; + if(this.isLimm(chr.charCodeAt(0))) + { + // Loadimm! + var imm1 = 0; // number + var imm2 = 0; // fraction + var mode = 0; // number/fraction time? + a = this.code[this.ip].charCodeAt(0); // char->int + while(this.isLimm(a) && this.terminate==0) + { + if(a==46) mode=1; // dot, time to switch modes! + else { + if(mode==0) + { + if(a>=48 && a<=57) imm1=(imm1<<4)|(a-48); // number, 0-9 + else imm1=(imm1<<4)|(a-55); // fraction, A-F + } + else + { + if(a>=48 && a<=57) imm2=(imm2>>4)|((a-48)<<12); // fraction, 0-9 + else imm2=(imm2>>4)|((a-55)<<12); // fraction, A-F + } + } + this.ip++; // increment IP + if(this.ip<this.code.length) a = this.code[this.ip].charCodeAt(0); // char->int (checks for ip overrun) + else { a=0; this.terminate=1; } // ip overrun, derpit + } + this.stack.push(((imm1&65535)<<16)|(imm2&65535)); // combine and push + this.ip--; // decrement IP to be incremented by this.run + } + else switch(chr) + { + // Math! + case '+': + a = this.stack.pop(); + this.stack.push(this.alu.add(a,this.stack.pop())); + break; + case '-': + a = this.stack.pop(); + this.stack.push(this.alu.sub(a,this.stack.pop())); + break; + case '*': + a = this.stack.pop(); + this.stack.push(this.alu.mul(a,this.stack.pop())); + break; + case '/': + a = this.stack.pop(); + this.stack.push(this.alu.div(a,this.stack.pop())); + break; + case '%': + a = this.stack.pop(); + this.stack.push(this.alu.mod(a,this.stack.pop())); + break; + case '&': + a = this.stack.pop(); + this.stack.push(this.alu.and(a,this.stack.pop())); + break; + case '|': + a = this.stack.pop(); + this.stack.push(this.alu.or(a,this.stack.pop())); + break; + case '^': + a = this.stack.pop(); + this.stack.push(this.alu.xor(a,this.stack.pop())); + break; + case 'l': + a = this.stack.pop(); + this.stack.push(this.alu.shl(a,this.stack.pop())); + break; + case 'r': + a = this.stack.pop(); + this.stack.push(this.alu.ror(a,this.stack.pop())); + break; + case 'a': + a = this.stack.pop(); + this.stack.push(this.alu.atan2(a,this.stack.pop())); + break; + case 's': + this.stack.push(this.alu.sin(this.stack.pop())); + break; + case 'q': + this.stack.push(this.alu.sqrt(this.stack.pop())); + break; + case '<': + this.stack.push(this.alu.isneg(this.stack.pop())); + break; + case '>': + this.stack.push(this.alu.ispos(this.stack.pop())); + break; + case '=': + this.stack.push(this.alu.iszero(this.stack.pop())); + break; + case '~': + this.stack.push(this.alu.not(this.stack.pop())); + break; + // Exterior! + case 'M': // media context switch + this.mode=1; + this.videoout = this.stack.pop(); + this.stack.clear(); + case 'w': // where am I? well, where are you + this.pushmedia(); + break; + case 'T': + this.terminate = 1; + break; + // Stack! + case 'd': + this.stack.dup(); + break; + case 'x': + this.stack.exchange(); + break; + case 'v': + this.stack.trirot(); + break; + case 'p': + this.stack.pop(); + break; + case ')': + this.stack.push(this.stack.get(this.stack.pop())); + break; + case '(': + a = this.stack.pop(); + this.stack.put(a,this.stack.pop());1 + break; + // Memory! + case '@': + this.stack.push(this.mem.get(this.stack.pop())); + break; + case '!': + a = this.stack.pop(); + this.mem.put(a,this.stack.pop()); + break; + case '?': + a = this.stack.pop(); + if(a==0) + { + while(chr!=":" && this.terminate==0) + { + if(this.ip>=this.code.length) this.terminate=1; + else this.ip++; + chr = this.code[this.ip]; + } + } + break; + case ':': + while(chr!=";" && this.terminate==0) + { + if(this.ip>=this.code.length) this.terminate=1; + else this.ip++; + chr = this.code[this.ip]; + } + break; + // Return stack manipulation + case 'R': + this.stack.push(this.rstack.pop()); + break; + case 'P': + this.rstack.push(this.stack.pop()); + break; + // Loops + case 'i': + this.stack.push(this.rstack.gettop(-1)); + break; + case 'j': + this.stack.push(this.rstack.gettop(-3)); + break; + case 'J': + this.ip = this.stack.pop()-1; + break; + default: + break; + } + } +} +var p = new Parser(); +var oldloop = new Date; +var c = document.getElementById("ibniz").getContext("2d"); +var runningCode = ";"; +p.load(runningCode); +p.t=0; +var codeEdit = document.getElementById("code"); +var fpsField = document.getElementById("fps"); +function clamp(a) +{ + if(a<0) return 0; + else if(a>255) return 255; + else return a; +} +function derp() +{ + var idd = c.createImageData(256,256); + var id = idd.data; + var imgpos = 0; + for(y=0;y<256;y++) + { + p.y=y; + for(x=0;x<256;x++) + { + p.x=x; + p.run(); + var cy = ((p.videoout>>>8)&255); + var cu = (((p.videoout>>>16)&255)^0x80)-128; + var cv = (((p.videoout>>>24)&255)^0x80)-128; + id[imgpos++] = clamp((298*cy + 409*cv + 128)>>8); + id[imgpos++] = clamp((298*cy - 100*cu - 208*cv + 128)>>8); + id[imgpos++] = clamp((298*cy + 516*cu + 128)>>8); + id[imgpos++] = 255; + } + } + c.putImageData(idd,0,0); + var newloop = new Date; + var fps = 1000 / (newloop - oldloop); + p.t+=Math.round(30/fps); + oldloop=newloop; + fpsField.childNodes[1].nodeValue=fps.toFixed(2); + if(runningCode!=codeEdit.value) + { + p.t=0; + runningCode=codeEdit.value; + p.load(runningCode); + console.log("NEW CODE LOADED"); + } + setTimeout("derp()",10); // give the browser time to not lag the whole computer +} +derp(); +</script> +</body> +</html>