Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- window.fairchild_bios1 = { size: 0x400, byte1: 0x70 };
- window.fairchild_bios2 = { size: 0x400, byte1: 0x04 };
- window.fairchild_cart = { byte1: 0x55 };
- window.fairchild_transfer_active = false;
- // Load one rom from file to javascript
- window.fairchild_load_rom = function(input) {
- var target = window[input.id];
- var reader = new FileReader();
- reader.addEventListener("loadend", function() {
- var data = new Uint8Array(reader.result);
- if ((target.size && data.length != target.size) ||
- (target.byte1 && data[0] != target.byte1)) {
- if (!confirm("This file doesn't look right, use it anyway?")) {
- input.value = ''; // resets files
- return;
- }
- }
- target.data = data;
- window.fairchild_update(target);
- });
- reader.readAsArrayBuffer(input.files[0]);
- }
- // Enable/disable UI & send to pico8 based on state
- window.fairchild_update = function(lastUpdated) {
- var cartElem = document.getElementById('fairchild_cart');
- var noCartElem = document.getElementById('fairchild_nocart');
- var hasBios = window.fairchild_bios1.data && window.fairchild_bios2.data;
- var hasCart = hasBios && window.fairchild_cart.data
- cartElem.disabled = !hasBios;
- noCartElem.disabled = !hasBios;
- if (hasCart && lastUpdated == window.fairchild_cart) {
- window.fairchild_send_to_pico8(true);
- }
- }
- // Send the roms from javascript to pico8 cart
- window.fairchild_send_to_pico8 = function(withCart) {
- var elem = document.getElementById(withCart ? 'fairchild_cart' : 'fairchild_nocart');
- elem.insertAdjacentHTML('afterend', "<b id='fairchild_loading'>Loading... (Make sure pico8 is running!)</b>");
- var endLoading = function() {
- var loadingELem = document.getElementById('fairchild_loading');
- loadingELem.parentNode.removeChild(loadingELem);
- };
- if (window.fairchild_transfer_active) {
- alert("Error - Transfer already active!");
- return;
- }
- window.fairchild_send_rom_to_pico8(window.fairchild_bios1.data, 0x0, function () {
- window.fairchild_send_rom_to_pico8(window.fairchild_bios2.data, 0x400, function () {
- if (withCart) {
- window.fairchild_send_rom_to_pico8(window.fairchild_cart.data, 0x800, endLoading, true);
- } else {
- window.fairchild_send_rom_to_pico8([0, 0], 0x800, endLoading, true); // empty cart header
- }
- });
- });
- }
- // Send one rom from javascript to pico8 cart
- window.fairchild_send_rom_to_pico8 = function(data, targetAddr, nextCallback, endTransfer) {
- if (!window.fairchild_transfer_active) {
- window.fairchild_transfer_active = true;
- // Init gpio array, for transfering data to pico8
- window.pico8_gpio = Array(128);
- window.pico8_gpio[0] = 0xca; // request transfer
- }
- var gpio = window.pico8_gpio;
- var index = 0;
- var interval;
- interval = setInterval(function () {
- if (gpio[0] >= 0xc0) { // 0xca/0xcb
- return; // pico8 cart didn't process the last block (or request) yet, wait for next interval
- }
- if (index < data.length) { // more data to transfer
- gpio[1] = Math.min(data.length - index, 0x7c); // block size
- gpio[2] = (targetAddr + index) & 0xff; // target addr (low)
- gpio[3] = ((targetAddr + index) >> 8) & 0xff; // target addr (high)
- for (var i = 0; i < gpio[1]; i++) {
- gpio[4 + i] = data[index++]; // block data
- }
- gpio[0] = 0xcb; // block is now valid
- } else if (endTransfer && gpio[0] != 1) { // end of transfer
- gpio[1] = 0; // end of transfer
- gpio[0] = 0xcb; // end-of-transfer block is now valid
- // this will be acknoledged by pico8 with gpio[0] == 1, so we won't re-enter this case again
- } else {
- clearInterval(interval)
- if (endTransfer) { window.fairchild_transfer_active = false; }
- if (nextCallback) { nextCallback(); }
- }
- }, 1);
- }
- document.body.insertAdjacentHTML('beforeend',
- "<div style='position:fixed;left:0;bottom:0'>" +
- "Step 1: Load Bios #1 (SL31253 or SL90025):<br> <input id='fairchild_bios1' type='file' onchange='fairchild_load_rom(this)'> <br>" +
- "Step 2: Load Bios #2 (SL31254):<br> <input id='fairchild_bios2' type='file' onchange='fairchild_load_rom(this)'> <br>" +
- "Step 3: Load Cart:<br> <input id='fairchild_cart' type='file' onchange='fairchild_load_rom(this)' disabled> <br>" +
- "Or just <button id='fairchild_nocart' onclick='fairchild_send_to_pico8(false)' disabled>Run Bios</button>" +
- "</div>"
- );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement