Advertisement
Guest User

Untitled

a guest
May 18th, 2019
370
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. window.fairchild_bios1 = { size: 0x400, byte1: 0x70 };
  2. window.fairchild_bios2 = { size: 0x400, byte1: 0x04 };
  3. window.fairchild_cart = { byte1: 0x55 };
  4. window.fairchild_transfer_active = false;
  5. // Load one rom from file to javascript
  6. window.fairchild_load_rom = function(input) {
  7.     var target = window[input.id];
  8.    
  9.     var reader = new FileReader();
  10.     reader.addEventListener("loadend", function() {
  11.         var data = new Uint8Array(reader.result);
  12.         if ((target.size && data.length != target.size) ||
  13.             (target.byte1 && data[0] != target.byte1)) {
  14.             if (!confirm("This file doesn't look right, use it anyway?")) {
  15.                 input.value = ''; // resets files
  16.                 return;
  17.             }
  18.         }
  19.  
  20.         target.data = data;
  21.         window.fairchild_update(target);
  22.     });
  23.     reader.readAsArrayBuffer(input.files[0]);
  24. }
  25. // Enable/disable UI & send to pico8 based on state
  26. window.fairchild_update = function(lastUpdated) {
  27.     var cartElem = document.getElementById('fairchild_cart');
  28.     var noCartElem = document.getElementById('fairchild_nocart');
  29.     var hasBios = window.fairchild_bios1.data && window.fairchild_bios2.data;
  30.     var hasCart = hasBios && window.fairchild_cart.data
  31.     cartElem.disabled = !hasBios;
  32.     noCartElem.disabled = !hasBios;
  33.     if (hasCart && lastUpdated == window.fairchild_cart) {
  34.         window.fairchild_send_to_pico8(true);
  35.     }
  36. }
  37. // Send the roms from javascript to pico8 cart
  38. window.fairchild_send_to_pico8 = function(withCart) {
  39.     var elem = document.getElementById(withCart ? 'fairchild_cart' : 'fairchild_nocart');
  40.     elem.insertAdjacentHTML('afterend', "<b id='fairchild_loading'>Loading... (Make sure pico8 is running!)</b>");
  41.    
  42.     var endLoading = function() {
  43.         var loadingELem = document.getElementById('fairchild_loading');
  44.         loadingELem.parentNode.removeChild(loadingELem);
  45.     };
  46.  
  47.     if (window.fairchild_transfer_active) {
  48.         alert("Error - Transfer already active!");
  49.         return;
  50.     }
  51.  
  52.     window.fairchild_send_rom_to_pico8(window.fairchild_bios1.data, 0x0, function () {
  53.         window.fairchild_send_rom_to_pico8(window.fairchild_bios2.data, 0x400, function () {
  54.             if (withCart) {
  55.                 window.fairchild_send_rom_to_pico8(window.fairchild_cart.data, 0x800, endLoading, true);
  56.             } else {
  57.                 window.fairchild_send_rom_to_pico8([0, 0], 0x800, endLoading, true); // empty cart header
  58.             }
  59.         });
  60.     });
  61. }
  62. // Send one rom from javascript to pico8 cart
  63. window.fairchild_send_rom_to_pico8 = function(data, targetAddr, nextCallback, endTransfer) {
  64.     if (!window.fairchild_transfer_active) {
  65.         window.fairchild_transfer_active = true;
  66.         // Init gpio array, for transfering data to pico8
  67.         window.pico8_gpio = Array(128);
  68.         window.pico8_gpio[0] = 0xca; // request transfer
  69.     }
  70.  
  71.     var gpio = window.pico8_gpio;
  72.     var index = 0;
  73.     var interval;
  74.     interval = setInterval(function () {
  75.         if (gpio[0] >= 0xc0) { // 0xca/0xcb
  76.             return; // pico8 cart didn't process the last block (or request) yet, wait for next interval
  77.         }
  78.  
  79.         if (index < data.length) { // more data to transfer
  80.             gpio[1] = Math.min(data.length - index, 0x7c); // block size
  81.             gpio[2] = (targetAddr + index) & 0xff; // target addr (low)
  82.             gpio[3] = ((targetAddr + index) >> 8) & 0xff; // target addr (high)
  83.             for (var i = 0; i < gpio[1]; i++) {
  84.                 gpio[4 + i] = data[index++]; // block data
  85.             }
  86.             gpio[0] = 0xcb; // block is now valid
  87.         } else if (endTransfer && gpio[0] != 1) { // end of transfer
  88.             gpio[1] = 0; // end of transfer
  89.             gpio[0] = 0xcb; // end-of-transfer block is now valid
  90.             // this will be acknoledged by pico8 with gpio[0] == 1, so we won't re-enter this case again
  91.         } else {
  92.             clearInterval(interval)
  93.             if (endTransfer) { window.fairchild_transfer_active = false; }
  94.             if (nextCallback) { nextCallback(); }
  95.         }
  96.     }, 1);
  97. }
  98. document.body.insertAdjacentHTML('beforeend',
  99.     "<div style='position:fixed;left:0;bottom:0'>" +
  100.     "Step 1: Load Bios #1 (SL31253 or SL90025):<br> <input id='fairchild_bios1' type='file' onchange='fairchild_load_rom(this)'> <br>" +
  101.     "Step 2: Load Bios #2 (SL31254):<br> <input id='fairchild_bios2' type='file' onchange='fairchild_load_rom(this)'> <br>" +
  102.     "Step 3: Load Cart:<br> <input id='fairchild_cart' type='file' onchange='fairchild_load_rom(this)' disabled> <br>" +
  103.     "Or just <button id='fairchild_nocart' onclick='fairchild_send_to_pico8(false)' disabled>Run Bios</button>" +
  104.     "</div>"
  105. );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement