logo

ibnjs

Unnamed repository; edit this file 'description' to name the repository.

ibniz-old.html (21895B)


  1. <html>
  2. <head>
  3. </head>
  4. <body>
  5. <canvas id="ibniz" width="256" height="256">
  6. Why u no canvas
  7. </canvas><br>
  8. <form>
  9. <input type="text" id="code" style="width: 256px"><br>
  10. <input type="checkbox" id="pause">Pause <input type="checkbox" id="simpleGetPut">Simple memory access<br>
  11. <input type="checkbox" id="useAudio">Audio (experimental, firefox/chrome/safari)<br>
  12. <input type="checkbox" id="recalcAudio">More accurate audio (slightly slower)
  13. </form>
  14. <div id="fps"><b>FPS:</b> [loading...]</div>
  15. <b>IBNIZ-js 0.9b</b><br>
  16. <a href="https://github.com/asiekierka/ibnjs">Github repository</a><br>
  17. <a href="mailto:asiekierka@gmail.com">Contact</a><br>
  18. <script type="text/javascript">
  19. /* This program is free software. It comes without any warranty, to
  20. * the extent permitted by applicable law. You can redistribute it
  21. * and/or modify it under the terms of the Do What The Fuck You Want
  22. * To Public License, Version 2, as published by Sam Hocevar. See
  23. * http://sam.zoy.org/wtfpl/COPYING for more details. */
  24. Array.prototype.clear = function(){
  25. this.length=0;
  26. }
  27. Array.prototype.exchange = function(){
  28. if(this.length<2) return -1;
  29. var temp = this[this.length-1];
  30. this[this.length-1]=this[this.length-2];
  31. this[this.length-2]=temp;
  32. }
  33. Array.prototype.trirot = function(){
  34. if(this.length<3) return -1;
  35. var temp = this[this.length-1];
  36. this[this.length-1]=this[this.length-3];
  37. this[this.length-3]=this[this.length-2];
  38. this[this.length-2]=temp;
  39. }
  40. Array.prototype.trirot2 = function(){
  41. if(this.length<3) return -1;
  42. var temp = this[this.length-2];
  43. this[this.length-2]=this[this.length-3];
  44. this[this.length-3]=this[this.length-1];
  45. this[this.length-1]=temp;
  46. }
  47. Array.prototype.get = function(addr) {
  48. return this[addr];
  49. }
  50. Array.prototype.gettop = function(addr) {
  51. return this[this.length-1+addr];
  52. }
  53. Array.prototype.put = function(addr,v) {
  54. this[addr]=v|0;
  55. }
  56. Array.prototype.puttop = function(addr,v) {
  57. this[this.length-1+addr]=v|0;
  58. }
  59. Array.prototype.dup = function(){
  60. this.push(this[this.length-1]);
  61. }
  62. Array.prototype.dpush = function(v){
  63. this.push(v|0);
  64. }
  65. function Parser(simpleGetPut,useAudio)
  66. {
  67. this.code = "";
  68. this.parsedCode = new Array();
  69. this.ip = 0;
  70. this.t = 0;
  71. this.x = 0;
  72. this.y = 0;
  73. this.xy = 0;
  74. this.mode = 0;
  75. this.terminate = 0;
  76. this.stackmode = 0;
  77. this.videoout = 0;
  78. this.audioout = 0;
  79. this.stacka = new Array();
  80. this.rstacka = new Array();
  81. this.mem = new Array(1048576);
  82. // 0xC8000-0xCFFFF - return stacks
  83. // 0xD0000-0xFFFFF - stacks
  84. this.shl = function(a,b){
  85. var steps = (a>>16)&63;
  86. return steps<32 ? b<<steps : b>>(steps-32);
  87. }
  88. this.rol = function(a,b){
  89. var steps = (a>>16)&31;
  90. return ((b<<steps)|(b>>>(32-steps)));
  91. }
  92. this.ror = function(a,b){
  93. var steps = (a>>16)&31;
  94. return ((b>>>steps)|(b<<(32-steps)));
  95. }
  96. this.config = function(simpleGetPut,useAudio,recalcAudio)
  97. {
  98. this.useAudio=useAudio;
  99. this.recalcAudio=recalcAudio;
  100. if(simpleGetPut)
  101. {
  102. this.get = function(addr) { return this.mem[addr]; }
  103. this.put = function(addr,val) { this.mem[addr]=val; }
  104. }
  105. else
  106. {
  107. this.get = function(addr,val){
  108. switch(addr>>15)
  109. {
  110. case 0:
  111. case 1:
  112. case 2:
  113. case 3:
  114. case 4:
  115. case 5:
  116. case 6:
  117. case 7:
  118. case 8:
  119. case 9:
  120. case 10:
  121. case 11:
  122. case 12:
  123. case 13:
  124. case 14:
  125. case 15:
  126. case 16:
  127. case 17:
  128. case 18:
  129. case 19:
  130. case 20:
  131. case 21:
  132. case 22:
  133. case 23:
  134. case 24:
  135. return this.mem[addr];
  136. break;
  137. case 25:
  138. return this.rstacka[addr&0x3FFF];
  139. break;
  140. case 26:
  141. case 27:
  142. return this.stacka[addr&0xFFFF];
  143. break;
  144. case 28:
  145. case 29:
  146. case 30:
  147. case 31:
  148. return this.stacka[addr&0x1FFFF];
  149. break;
  150. }
  151. }
  152. this.put = function(addr,val){
  153. switch(addr>>15)
  154. {
  155. case 0:
  156. case 1:
  157. case 2:
  158. case 3:
  159. case 4:
  160. case 5:
  161. case 6:
  162. case 7:
  163. case 8:
  164. case 9:
  165. case 10:
  166. case 11:
  167. case 12:
  168. case 13:
  169. case 14:
  170. case 15:
  171. case 16:
  172. case 17:
  173. case 18:
  174. case 19:
  175. case 20:
  176. case 21:
  177. case 22:
  178. case 23:
  179. case 24:
  180. this.mem[addr] = val;
  181. break;
  182. case 25:
  183. this.rstacka.put(addr&0x3FFF,val);
  184. break;
  185. case 26:
  186. case 27:
  187. this.stacka.put(addr&0xFFFF,val);
  188. break;
  189. case 28:
  190. case 29:
  191. case 30:
  192. case 31:
  193. this.stacka.put(addr&0x1FFFF,val);
  194. break;
  195. }
  196. }
  197. }
  198. }
  199. this.config(simpleGetPut,useAudio);
  200. this.load = function(c)
  201. {
  202. this.code=c;
  203. this.parsedCode = new Array();
  204. this.compile();
  205. this.configureStackmode();
  206. }
  207. this.rol16 = function(b){
  208. return ((b<<16)|(b>>>16));
  209. }
  210. this.compiledpm = function(x,y){}
  211. this.pm0 = function() { this.stacka.push(this.t<<16,0,0); };
  212. this.pushmedia = function(x,y)
  213. {
  214. if(this.mode==0) this.compiledpm(x,y);
  215. else this.stacka.push(this.t<<16 | (y<<8) | x);
  216. }
  217. this.pmaudio = function(x,y)
  218. {
  219. this.stacka.push(this.t*65536 + (y<<8) + x);
  220. }
  221. this.compilepushmedia = function()
  222. {
  223. switch(this.stackmode)
  224. {
  225. case 0:
  226. this.compiledpm = function(x,y) { this.stacka.push(this.t<<16,(y<<9)-65536,(x<<9)-65536); };
  227. break;
  228. case 1:
  229. this.compiledpm = function(x,y) { this.stacka.push(this.t<<16 | (y<<8) | x); };
  230. break;
  231. }
  232. }
  233. this.run = function(x,y)
  234. {
  235. // reset the machine
  236. this.mode = 0;
  237. this.terminate = 0;
  238. this.stacka.length=0;
  239. this.ip = 0;
  240. // push media context
  241. this.compiledpm(x,y);
  242. // loop
  243. this.mediaSwitch=true;
  244. this.exec(x,y);
  245. if(this.mode==0)
  246. {
  247. // run audio, too
  248. this.videoout = this.stacka.pop();
  249. if((this.useFFAudio || this.useChromeAudio) && this.useAudio)
  250. {
  251. if(this.stackmode==1 && !this.recalcAudio)
  252. this.audioout=this.videoout; // same stack data, skip the processing bulk
  253. else if((x%128)==0 && this.recalcAudio)
  254. {
  255. this.mode=1;
  256. this.terminate = 0;
  257. this.stacka.length=0;
  258. this.ip = 0;
  259. this.pmaudio(x,y);
  260. this.exec(x,y);
  261. }
  262. }
  263. }
  264. if((this.useFFAudio || this.useChromeAudio) && this.useAudio && this.mode==1) this.audioout = this.stacka.pop();
  265. }
  266. this.isLimm = function(ci)
  267. {
  268. // is 0-9? A-F? .?
  269. return ((ci>=48 && ci<=57) || (ci>=65 && ci<=70) || (ci==46));
  270. }
  271. this.isImmop = function(ci)
  272. {
  273. return ((ci>=112 && ci<=115)||ci==43||ci==45||ci==42||ci==47||ci==37||ci==38||ci==124||ci==94||ci==108||ci==126||ci==97||ci==100||ci==40||ci==41||ci==74||ci==33||ci==64||ci==86||ci==88||ci==80||ci==123);
  274. }
  275. this.isOpcode = function(ci)
  276. {
  277. return (this.isImmop(ci) || this.isLimm(ci) || ci==77||(ci>=118&&ci<=120)||ci==63||ci==58||ci==59||ci==82||ci==84||ci==105||ci==106||ci==91||ci==93||ci==76||ci==125);
  278. }
  279. this.compile = function()
  280. {
  281. var i = 0;
  282. var j = 0;
  283. while(i<this.code.length)
  284. {
  285. var a = this.code[i].charCodeAt(0);
  286. if(this.isLimm(a))
  287. {
  288. // Loadimm!
  289. var imm1 = 0; // number
  290. var imm2 = 0; // fraction
  291. var mode = 0; // number/fraction time?
  292. while(this.isLimm(a) && i<this.code.length)
  293. {
  294. if(a==46) mode=1; // dot, time to switch modes!
  295. else {
  296. if(mode==0)
  297. {
  298. if(a>=48 && a<=57) imm1=(imm1<<4)|(a-48); // number, 0-9
  299. else imm1=(imm1<<4)|(a-55); // fraction, A-F
  300. }
  301. else
  302. {
  303. if(a>=48 && a<=57) imm2=(imm2>>4)|((a-48)<<12); // fraction, 0-9
  304. else imm2=(imm2>>4)|((a-55)<<12); // fraction, A-F
  305. }
  306. }
  307. i++; // increment IP
  308. if(i<this.code.length) a = this.code[i].charCodeAt(0); // char->int (checks for ip overrun)
  309. }
  310. if(i<this.code.length) a = this.code[i].charCodeAt(0);
  311. else a=0;
  312. var out = ((imm1&65535)<<16)|(imm2&65535);
  313. if(this.isImmop(a))
  314. {
  315. switch(a)
  316. {
  317. case 115:
  318. this.parsedCode[j] = new Array(2,Math.sin(out*(Math.PI/32768))*65536);
  319. break;
  320. case 113:
  321. this.parsedCode[j] = new Array(2,0 > out ? 0 : 65536 * Math.sqrt(out / 65536));
  322. break;
  323. case 126:
  324. this.parsedCode[j] = new Array(2,~out);
  325. break;
  326. case 123:
  327. this.parsedCode[j] = new Array(5,this.rol16(out)&1048575,123,0);
  328. break;
  329. case 40:
  330. case 41:
  331. this.parsedCode[j] = new Array(3,0-this.rol16(out),a);
  332. break;
  333. case 74:
  334. case 86:
  335. this.parsedCode[j] = new Array(3,this.rol16(out),a);
  336. break;
  337. case 64:
  338. var addr = this.rol16(out)&1048575;
  339. if(addr<0xC8000) this.parsedCode[j] = new Array(3,addr,128);
  340. else this.parsedCode[j] = new Array(3,addr,a);
  341. break;
  342. case 33:
  343. var addr = this.rol16(out)&1048575;
  344. if(addr<0xC8000) this.parsedCode[j] = new Array(3,addr,129);
  345. else this.parsedCode[j] = new Array(3,addr,a);
  346. break;
  347. default:
  348. this.parsedCode[j] = new Array(3,out,a);
  349. break;
  350. }
  351. i++;
  352. }
  353. else this.parsedCode[j] = new Array(2,out);
  354. j++;
  355. }
  356. else if(this.isOpcode(a))
  357. {
  358. switch(a)
  359. {
  360. case 125:
  361. var t = j;
  362. var chr = 0;
  363. while(chr!=123 && t>=0)
  364. {
  365. t--;
  366. if(this.parsedCode[t] && this.parsedCode[t][0]==1) chr = this.parsedCode[t][1];
  367. else if(this.parsedCode[t] && this.parsedCode[t][0]==5) chr = this.parsedCode[t][2];
  368. }
  369. if(t>=0)
  370. {
  371. if(this.parsedCode[t] && this.parsedCode[t][0]==5)
  372. this.parsedCode[t] = new Array(5,this.parsedCode[t][1],chr,j);
  373. else this.parsedCode[t] = new Array(4,chr,j);
  374. }
  375. this.parsedCode[j] = new Array(1,a);
  376. break;
  377. case 58:
  378. var t = j;
  379. var chr = 0;
  380. while(chr!=63 && t>=0)
  381. {
  382. t--;
  383. if(this.parsedCode[t] && this.parsedCode[t][0]==1) chr = this.parsedCode[t][1];
  384. }
  385. if(t>=0) this.parsedCode[t] = new Array(4,chr,j);
  386. this.parsedCode[j] = new Array(1,a);
  387. break;
  388. case 59:
  389. var t = j;
  390. var chr = 0;
  391. while(chr!=58 && t>=0)
  392. {
  393. t--;
  394. if(this.parsedCode[t] && this.parsedCode[t][0]==1) chr = this.parsedCode[t][1];
  395. }
  396. if(t>=0) this.parsedCode[t] = new Array(4,chr,j);
  397. this.parsedCode[j] = new Array(1,a);
  398. break;
  399. case 118:
  400. this.parsedCode[j] = new Array(1,a);
  401. if(i+1<this.code.length)
  402. {
  403. var b = this.code[i+1].charCodeAt(0);
  404. if(b==118)
  405. {
  406. this.parsedCode[j] = new Array(1,128);
  407. i++;
  408. }
  409. }
  410. break;
  411. default:
  412. this.parsedCode[j] = new Array(1,a);
  413. break;
  414. }
  415. i++;
  416. j++;
  417. }
  418. else i++;
  419. }
  420. }
  421. this.mediaSwitch=true;
  422. this.configureStackmode = function()
  423. {
  424. // reset the machine
  425. this.mode = 0;
  426. this.stackmode = 0;
  427. this.terminate = 0;
  428. this.stacka.length=0;
  429. this.ip = 0;
  430. // push media context
  431. this.pm0();
  432. // loop
  433. this.mediaSwitch=false;
  434. this.exec(0,0);
  435. if(this.stacka.length>1) this.stackmode = 1;
  436. this.compilepushmedia();
  437. }
  438. this.getOp = function(ip)
  439. {
  440. var cmd = this.parsedCode[this.ip];
  441. if(cmd[0]==1 || cmd[0]==4) return cmd[1];
  442. else if(cmd[0]==3) return cmd[2];
  443. else return 0;
  444. }
  445. this.exec = function(x,y)
  446. {
  447. var cmd;
  448. var a;
  449. var stacka = this.stacka;
  450. while(this.terminate!=1)
  451. {
  452. if(this.ip>=this.parsedCode.length)
  453. {
  454. this.terminate=1;
  455. return;
  456. }
  457. cmd = this.parsedCode[this.ip];
  458. switch(cmd[0])
  459. {
  460. case 1: // op
  461. switch(cmd[1])
  462. {
  463. // Math!
  464. case 43:
  465. a = stacka.pop();
  466. stacka.push((a+stacka.pop())|0);
  467. break;
  468. case 45:
  469. a = stacka.pop();
  470. stacka.push((stacka.pop()-a)|0);
  471. break;
  472. case 42:
  473. a = stacka.pop();
  474. stacka.push(((a*stacka.pop())/65536)|0);
  475. break;
  476. case 47:
  477. a = stacka.pop();
  478. stacka.push(((stacka.pop()*65536)/a)|0);
  479. break;
  480. case 37:
  481. a = stacka.pop();
  482. stacka.push(stacka.pop()%a);
  483. break;
  484. case 38:
  485. a = stacka.pop();
  486. stacka.push(a&stacka.pop());
  487. break;
  488. case 124:
  489. a = stacka.pop();
  490. stacka.push(a|stacka.pop());
  491. break;
  492. case 94:
  493. a = stacka.pop();
  494. stacka.push(a^stacka.pop());
  495. break;
  496. case 108:
  497. var steps = (stacka.pop()>>16)&63;
  498. a = stacka.pop();
  499. stacka.push(steps<32 ? a<<steps : a>>(steps-32));
  500. break;
  501. case 114:
  502. var steps = (stacka.pop()>>16)&31;
  503. a = stacka.pop();
  504. stacka.push((a>>>steps)|(a<<(32-steps)));
  505. break;
  506. case 97:
  507. a = stacka.pop();
  508. stacka.push((Math.atan2(a,stacka.pop())*(65536/(2*Math.PI)))|0);
  509. break;
  510. case 115:
  511. stacka.push((Math.sin(stacka.pop()*(2*Math.PI/65536))*65536)|0);
  512. break;
  513. case 113:
  514. a = stacka.pop();
  515. stacka.push(0 > a ? 0 : (65536 * Math.sqrt(a / 65536))|0);
  516. break;
  517. case 60:
  518. a = stacka.pop();
  519. stacka.push(0 > a ? a : 0);
  520. break;
  521. case 62:
  522. a = stacka.pop();
  523. stacka.push(0 < a ? a : 0);
  524. break;
  525. case 61:
  526. stacka.push(stacka.pop()==0);
  527. break;
  528. case 126:
  529. stacka.push(~stacka.pop());
  530. break;
  531. // Exterior!
  532. case 77: // media context switch
  533. if(!this.mediaSwitch || !this.useAudio) this.terminate=1;
  534. else
  535. {
  536. this.mode=1;
  537. this.videoout = stacka.pop();
  538. stacka.length=0;
  539. this.pmaudio(x,y);
  540. }
  541. break;
  542. case 119: // where am I? well, where are you
  543. this.pushmedia(x,y);
  544. break;
  545. case 84:
  546. this.terminate = 1;
  547. break;
  548. // Stack!
  549. case 100:
  550. stacka.dup();
  551. break;
  552. case 120:
  553. stacka.exchange();
  554. break;
  555. case 118:
  556. stacka.trirot();
  557. break;
  558. case 112:
  559. stacka.pop();
  560. break;
  561. case 41:
  562. stacka.push(stacka.gettop(0-this.rol16(stacka.pop())));
  563. break;
  564. case 40:
  565. a = this.rol16(stacka.pop());
  566. stacka.puttop(0-a,stacka.pop());
  567. break;
  568. // Memory!
  569. case 64:
  570. stacka.push(this.get(this.rol16(stacka.pop())&1048575));
  571. break;
  572. case 33:
  573. a = this.rol16(stacka.pop());
  574. this.put(a&1048575,stacka.pop());
  575. break;
  576. // Return stack manipulation
  577. case 82:
  578. stacka.push(this.rstacka.pop());
  579. break;
  580. case 80:
  581. this.rstacka.push(stacka.pop());
  582. break;
  583. // Loops
  584. case 105:
  585. stacka.push(this.rstacka.gettop(-1));
  586. break;
  587. case 106:
  588. stacka.push(this.rstacka.gettop(-3));
  589. break;
  590. case 74:
  591. this.ip = this.rol16(stacka.pop())-1;
  592. break;
  593. case 91:
  594. this.rstacka.push(this.rol16(this.ip+1));
  595. break;
  596. case 93:
  597. if(stacka.pop()!=0) this.ip=this.rol16(this.rstacka.gettop(0))-1;
  598. else this.rstacka.pop();
  599. break;
  600. case 88:
  601. this.rstacka.push(stacka.pop());
  602. this.rstacka.push(this.rol16(this.ip+1));
  603. break;
  604. case 76:
  605. a=this.rstacka.gettop(-1)-(1<<16);
  606. this.rstacka.puttop(-1,a);
  607. if(a!=0) this.ip=this.rol16(this.rstacka.gettop(0))-1;
  608. else { this.rstacka.pop(); this.rstacka.pop(); }
  609. break;
  610. // Subroutines
  611. case 125:
  612. this.ip = this.rol16(this.rstacka.pop())-1;
  613. break;
  614. case 86:
  615. this.rstacka.push(this.rol16(this.ip+1));
  616. this.ip = this.rol16(this.get(this.rol16(stacka.pop())&1048575))-1;
  617. break;
  618. // Special
  619. case 128: // Double trirot
  620. stacka.trirot2();
  621. break;
  622. default:
  623. break;
  624. }
  625. break;
  626. case 5: // imm+op+nextip
  627. switch(cmd[2])
  628. {
  629. case 123:
  630. this.put(cmd[1],this.rol16(this.ip+1));
  631. this.ip = cmd[3];
  632. break;
  633. }
  634. break;
  635. case 4: // op+nextip
  636. switch(cmd[1])
  637. {
  638. case 123:
  639. this.put(this.rol16(stacka.pop())&1048575,this.rol16(this.ip+1));
  640. this.ip = cmd[2];
  641. break;
  642. case 63:
  643. a = stacka.pop();
  644. if(a==0) this.ip = cmd[2];
  645. break;
  646. case 58:
  647. this.ip = cmd[2];
  648. break;
  649. }
  650. break;
  651. case 3: // imm+op
  652. switch(cmd[2])
  653. {
  654. case 43:
  655. stacka.push((cmd[1]+stacka.pop())|0);
  656. break;
  657. case 45:
  658. stacka.push((stacka.pop()-cmd[1])|0);
  659. break;
  660. case 42:
  661. stacka.push(((cmd[1]*stacka.pop())/65536)|0);
  662. break;
  663. case 47:
  664. stacka.push(((stacka.pop()*65536)/cmd[1])|0);
  665. break;
  666. case 37:
  667. stacka.push(stacka.pop()%cmd[1]);
  668. break;
  669. case 38:
  670. stacka.push(cmd[1]&stacka.pop());
  671. break;
  672. case 124:
  673. stacka.push(cmd[1]|stacka.pop());
  674. break;
  675. case 94:
  676. stacka.push(cmd[1]^stacka.pop());
  677. break;
  678. case 108:
  679. stacka.push(this.shl(cmd[1],stacka.pop()));
  680. break;
  681. case 114:
  682. stacka.push(this.ror(cmd[1],stacka.pop()));
  683. break;
  684. case 97:
  685. stacka.push((Math.atan2(cmd[1],stacka.pop())*(65536/(2*Math.PI)))|0);
  686. break;
  687. case 115:
  688. stacka.push((Math.sin(cmd[1]*(Math.PI/32768))*65536)|0);
  689. break;
  690. case 113:
  691. stacka.push(0 > cmd[1] ? 0 : (65536 * Math.sqrt(cmd[1] / 65536))|0);
  692. break;
  693. case 126:
  694. stacka.push(~cmd[1]);
  695. break;
  696. case 100:
  697. stacka.push(cmd[1],cmd[1]);
  698. break;
  699. case 40:
  700. stacka.puttop(cmd[1],stacka.pop());
  701. break;
  702. case 41:
  703. stacka.push(stacka.gettop(cmd[1]));
  704. break;
  705. case 74:
  706. this.ip = cmd[1];
  707. break;
  708. case 64:
  709. stacka.push(this.get(cmd[1]));
  710. break;
  711. case 33:
  712. this.put(cmd[1],stacka.pop());
  713. break;
  714. case 86:
  715. this.rstacka.push(this.rol16(this.ip+1));
  716. this.ip = this.rol16(this.get(cmd[1]))-1;
  717. break;
  718. case 88:
  719. this.rstacka.push(cmd[1]);
  720. this.rstacka.push(this.rol16(this.ip+1));
  721. break;
  722. case 80:
  723. this.rstacka.push(cmd[1]);
  724. break;
  725. case 112:
  726. break;
  727. // special
  728. case 128: // direct load
  729. stacka.push(this.mem[cmd[1]]);
  730. break;
  731. case 129: // direct store
  732. this.mem[cmd[1]]=stacka.pop();
  733. break;
  734. }
  735. break;
  736. case 2: // imm
  737. stacka.push(cmd[1]);
  738. break;
  739. default:
  740. break;
  741. }
  742. this.ip++;
  743. }
  744. }
  745. this.useFFAudio = false;
  746. this.useChromeAudio = false;
  747. this.recalcAudio = false;
  748. this.buffer = new Array(512);
  749. if(typeof Audio !== 'undefined')
  750. {
  751. this.audioOut = new Audio();
  752. if(this.audioOut.mozSetup)
  753. {
  754. this.audioOut.mozSetup(1,512*60);
  755. this.useFFAudio = true;
  756. this.buffer = new Float32Array(512);
  757. }
  758. }
  759. if(typeof webkitAudioContext !== 'undefined')
  760. {
  761. this.audioCtx = new webkitAudioContext();
  762. this.audioSR = this.audioCtx.sampleRate/60;
  763. this.useChromeAudio = true;
  764. this.buffer = new Float32Array(512);
  765. }
  766. this.render = function(c)
  767. {
  768. var idd = c.createImageData(256,256);
  769. var id = idd.data;
  770. var imgpos = 0;
  771. var cy = 0;
  772. var cu = 0;
  773. var cv = 0;
  774. for(var y=0;y<256;y++)
  775. {
  776. for(var x=0;x<256;x++)
  777. {
  778. this.run(x,y);
  779. cy = (p.videoout>>>8)&255;
  780. cu = (((p.videoout>>>16)&255)^0x80)-128;
  781. cv = ((p.videoout>>>24)^0x80)-128;
  782. id[imgpos++] = (298*cy + 409*cv + 128)>>8;
  783. id[imgpos++] = (298*cy - 100*cu - 208*cv + 128)>>8;
  784. id[imgpos++] = (298*cy + 516*cu + 128)>>8;
  785. id[imgpos++] = 255;
  786. this.buffer[(y<<1)] = (((this.audioout&65535)^32768)-32768)/32768;
  787. }
  788. }
  789. c.putImageData(idd,0,0);
  790. if(this.useFFAudio) this.audioOut.mozWriteAudio(this.buffer);
  791. else if(this.useChromeAudio)
  792. {
  793. if(this.audioOut.noteOff) this.audioOut.noteOff(0);
  794. this.fixbuffer = new Float32Array(this.audioSR);
  795. for(var i=0;i<this.audioSR;i++)
  796. {
  797. var t1 = i*512/this.audioSR;
  798. var t2 = t1-Math.floor(t1);
  799. this.fixbuffer[i]=this.buffer[Math.floor(t1)]*(1-t2)+this.buffer[Math.ceil(t1)]*t2;
  800. }
  801. this.aBuffer = this.audioCtx.createBuffer(1,this.audioSR,this.audioSR*60);
  802. this.aBuffer.getChannelData(0).set(this.fixbuffer);
  803. this.audioOut = this.audioCtx.createBufferSource();
  804. this.audioOut.buffer=this.aBuffer;
  805. this.audioOut.connect(this.audioCtx.destination);
  806. this.audioOut.noteOn(0);
  807. this.audioOut.loop = true;
  808. }
  809. }
  810. this.delayAudio = function(a)
  811. {
  812. if(this.useFFAudio) for(var b=0; b<a; b++) this.audioOut.mozWriteAudio(this.buffer);
  813. }
  814. }
  815. var p = new Parser(false,false);
  816. var oldloop = new Date;
  817. var c = document.getElementById("ibniz").getContext("2d");
  818. var runningCode = " ";
  819. p.load(runningCode);
  820. p.t=0;
  821. var codeEdit = document.getElementById("code");
  822. var fpsField = document.getElementById("fps");
  823. var pause = document.getElementById("pause");
  824. var simpleGetPut = document.getElementById("simpleGetPut");
  825. var useAudio = document.getElementById("useAudio");
  826. var recalcAudio = document.getElementById("recalcAudio");
  827. function derp()
  828. {
  829. if(!pause.checked)
  830. {
  831. p.render(c);
  832. var newloop = new Date;
  833. var fps = 1000 / (newloop - oldloop);
  834. oldloop=newloop;
  835. p.t+=60/fps;
  836. p.delayAudio(Math.round(60/fps));
  837. p.configureStackmode();
  838. p.config(simpleGetPut.checked,useAudio.checked,recalcAudio.checked);
  839. fpsField.childNodes[1].nodeValue=fps.toFixed(2);
  840. if(runningCode!=codeEdit.value)
  841. {
  842. p.t=0;
  843. runningCode=codeEdit.value;
  844. p.load(runningCode);
  845. console.log("NEW CODE LOADED");
  846. }
  847. }
  848. setTimeout("derp()",1); // give the browser time to not lag the whole computer
  849. }
  850. function loadFileURL()
  851. {
  852. var url = prompt("Please input an URL","");
  853. var http = createRequestObject();
  854. function createRequestObject() { // http://www.openhosting.co.uk/articles/webdev/5899/
  855. var objAjax;
  856. var browser = navigator.appName;
  857. if(browser == "Microsoft Internet Explorer"){
  858. objAjax = new ActiveXObject("Microsoft.XMLHTTP");
  859. }else{
  860. objAjax = new XMLHttpRequest();
  861. }
  862. return objAjax;
  863. }
  864. function updateFileURL() {
  865. if(http.readyState == 4)
  866. codeEdit.value=http.responseText;
  867. }
  868. http.open('get',url);
  869. http.onreadystatechange = updateFileURL;
  870. http.send(null);
  871. }
  872. derp();
  873. </script>
  874. </body>
  875. </html>0