Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <table id="tb_settings">
  <tr>
    <th>Input file:</th>
    <td><input type="file" id="inFile" /></td>
  </tr>
  <tr>
    <th>Input palette:</th>
    <td><input type="file" id="inPal" /></td>
  </tr>
  <tr>
    <th>Designation:</th>
    <td><select id="palset">
      <option value=0>Background</option>
      <option value=128>Object</option>
      <option value=32>Mode 0 - BG2</option>
      <option value=64>Mode 0 - BG3</option>
      <option value=96>Mode 0 - BG4</option>
    </select></td>
  </tr>
  <tr>
    <th>Palette ID:</th>
    <td><input id="outpal" type="number" min=0 max=7 value="0"/></td>
  </tr>
  <tr>
    <th>Bit depth:</th>
    <td><select id="outbpp">
      <option value=2>2bpp</option>
      <option value=4>4bpp</option>
      <option value=8>8bpp</option>
    </select></td>
  </tr>
</table>
<div style="white-space: nowrap">
  <div id="tb_data"></div>
  <canvas id="cv_out" width=1024 height=512></canvas>
  <div id="cv_highlight"></div>
</div>
<div id="bin_data"></div>
              
            
!

CSS

              
                #tb_settings th {
  text-align: right;
}

#tb_data {
  display: inline-block;
  vertical-align: top;
  font-family: monospace;
  height: 512px;
  width: 330px;
  overflow-y: scroll;
  border: 1px solid gray;
}

  #tb_data tr.hl, #bin_data .hl {
    background-color: lightsalmon;
  }

  #tb_data td.hl {
    background-color: darkred;
    color: white;
  }

#bin_data {
  display: inline-block;
  min-height : 1em;
  font-size: 14pt;
  font-family: monospace;
  white-space: nowrap;
}

#cv_out {
  border: 1px solid gray;
  image-rendering: optimizeSpeed;             /* Older versions of FF          */
  image-rendering: -moz-crisp-edges;          /* FF 6.0+                       */
  image-rendering: -webkit-optimize-contrast; /* Safari                        */
  image-rendering: -o-crisp-edges;            /* OS X & Windows Opera (12.02+) */
  image-rendering: pixelated;                 /* Future browsers               */
  -ms-interpolation-mode: nearest-neighbor;   /* IE                            */
}

#cv_highlight {
  position: absolute;
  display: inline-block;
  visibility: hidden;
  z-index: 1000;
  height: 8px;
  width: 8px;
  pointer-events: none;
  background-color: rgba(255, 255, 255, 0.5);
  border: 1px dotted black;
  box-sizing: border-box;
}
              
            
!

JS

              
                var palette;
var graphics;

$(getDefPal());

$("#inPal").on("change", function() {
  var reader = new FileReader();
  reader.onload = function() {
    getPal(new Uint8Array(this.result, 0, 16384));
    if (graphics != null) fillCanvas(graphics, palette);
  }
  reader.readAsArrayBuffer(this.files[0]);
});

$("#inFile").on("change", function() {
	var reader = new FileReader();
  reader.onload = function() {
    graphics = new Uint8Array(this.result);
    fillData(graphics);
    fillCanvas(graphics, palette);
  }
  reader.readAsArrayBuffer(this.files[0]);
});

$("#outbpp").on("change", function() {
  var pal = document.getElementById("outpal");
  if (this.value == 8) {
    pal.disabled = true;
    pal.value = 0;
  } else {
    pal.disabled = false;
  }
  if (graphics != null) fillCanvas(graphics, palette);
});

$("#outpal").on("change", function() {
  if (graphics != null) fillCanvas(graphics, palette);
});

$("#palset").on("change", function() {
  var bdepth = document.getElementById("outbpp");
  document.getElementById("outpal").disabled = false;
  if (this.value == 0) {
    bdepth.disabled = false;
  } else {
    bdepth.disabled = true;
    bdepth.value = (this.value == 128 ? 4 : 2);
  }
  fillCanvas(graphics, palette);
});

function fillData(data) {
  var out = "<table><tr>";
	for (i = 0; i < data.length; i++) {
    if (i % 16 == 0 && i > 0) { out += "</tr><tr>" }
    out += "<td>" + padNum(data[i], 16, 2) + "</td>";
  }
  out += "</tr></table>";
  document.getElementById('tb_data').innerHTML = out;
}

function fillCanvas(graphics, palette) {
  var canvas = document.getElementById('cv_out');
  if (!canvas.getContext) { return; }
  var ctx = canvas.getContext('2d');
  
  var bpp = parseInt(document.getElementById('outbpp').value);
  var pal = parseInt(document.getElementById('outpal').value);
  var palset = parseInt(document.getElementById('palset').value);
  var palind = pal*(1<<bpp) + palset;
  
  var t, r, p, s, c;
  var base, ind, bp0, bp1, bp2, bp3, bp4, bp5, bp6, bp7;
  for (t = 0; t < 128; t++) { // tile loop
    base = t * bpp * 8;
    for (r = 0; r < 8; r++) {
      // fetch each of the bitplane bytes for the row
      ind = base + r*2;
      bp0 =   graphics[ind];
      bp1 =   graphics[ind + 1];
      if (bpp >= 4) {
        bp2 = graphics[ind + 16];
        bp3 = graphics[ind + 17];
      }
      if (bpp == 8) {
        bp4 = graphics[ind + 32];
        bp5 = graphics[ind + 33];
        bp6 = graphics[ind + 48];
        bp7 = graphics[ind + 49];
      }
      
      for (p = 0; p < 8; p++) {
        // fetch each of the bitplanes for the pixel
        s = 1 << p;
        c =    ((bp0 & s) >> p)
            |  ((bp1 & s) >> p) << 1;
        if (bpp >= 4)
          c |= ((bp2 & s) >> p) << 2
            |  ((bp3 & s) >> p) << 3;
        if (bpp == 8)
          c |= ((bp4 & s) >> p) << 4
            |  ((bp5 & s) >> p) << 5
            |  ((bp6 & s) >> p) << 6
            |  ((bp7 & s) >> p) << 7;
            
        // draw color
        ctx.fillStyle = 'rgb(' + palette[palind + c] + ')';
        ctx.fillRect(((t % 16)*8 + (7-p))*8, ((t >> 4)*8 + r)*8, 8, 8);
      }
    }
  }
}

$("#cv_out").mousemove(function(e) {
  if (graphics == null) { return; }
  
  var x = Math.floor((e.pageX - this.offsetLeft - 2)/8);
  var y = Math.floor((e.pageY - this.offsetTop - 2)/8);
  if (x < 0 || y < 0) { return; }
  
  var hl = document.getElementById("cv_highlight");
  hl.style.visibility = "visible";
  hl.style.left = (x*8 + this.offsetLeft + 1) + "px";
  hl.style.top  = (y*8 + this.offsetTop + 1) + "px";
  
  document.getElementById("bin_data").innerHTML = "";
  $("#tb_data .hl").removeClass("hl");
  var bpp = parseInt(document.getElementById('outbpp').value);
  var base  = (Math.floor(y/8)*16 + Math.floor(x/8))*8*bpp + (y % 8)*2;
  var rb = Math.floor(base/16), cb = base % 16;
  
  var tbData = document.getElementById("tb_data");
  var trs = tbData.getElementsByTagName("tr");
  var tds;
  var data = [];
  scrollIntoView(trs[rb], tbData);
  //trs[rb].scrollIntoView({block: "nearest"});
  for (var i = bpp; i > 0; i -= 2, rb++) {
    tds = trs[rb].getElementsByTagName("td");
    data.push(parseInt(tds[cb].innerHTML, 16));
    data.push(parseInt(tds[cb+1].innerHTML, 16));
    trs[rb].className = "hl";
    tds[cb].className = "hl";
    tds[cb+1].className = "hl";
  }
  scrollIntoView(trs[rb-1], tbData);
  //trs[rb-1].scrollIntoView({block: "nearest"});
  writeData(data, x % 8);
  
});

function writeData(data, ind) {
  var out = "$" + data.map(x => padNum(x, 16, 2)).join(" ") + " = %";
  var bout, bin = "";
  for (var i = 0; i < data.length; i++) {
    bout = padNum(data[i], 2, 8);
    bin = bout.charAt(ind) + bin;
    bout = bout.slice(0, ind) + "<span class='hl'>" + bout.charAt(ind) + "</span>" + bout.slice(ind + 1);
    out += bout + " ";
  }
  out += " 🡆 %" + bin + " = $" + padNum(parseInt(bin, 2), 16, 2);
  document.getElementById("bin_data").innerHTML = out;
}


function getPal(data) {
  palette = [];
  var c,r,g,b;
  for (var i = 0; i < 256; i++) {
    if (i > data.length) {
      palette[i] = 0x000000;
    } else {
      var i2 = i*3;
      palette[i] = data[i2] + "," + data[i2+1] + "," + data[i2+2];
    }
  }
}

function scrollIntoView(e, c) {
  if (e.offsetTop < c.scrollTop) {
    c.scrollTop = e.offsetTop;
  } else if (e.offsetTop + e.offsetHeight > c.scrollTop + c.clientHeight) {
    c.scrollTop = e.offsetTop + e.offsetHeight - c.clientHeight;
  }
}
function padNum(val, base, len) { return ("0".repeat(len) + val.toString(base)).substr(-len).toUpperCase(); }

// default palette if no file loaded
function getDefPal() {
   palette = ["0,96,184","232,240,248","16,80,112","64,128,160","112,176,208","144,192,192","168,208,208","192,224,224","152,224,224","0,0,0","216,56,24","88,248,88","152,224,224","0,0,0","232,240,248","248,88,88","0,96,184","232,240,248","16,80,112","64,128,160","112,176,208","16,80,112","64,128,160","112,176,208","152,224,224","0,0,0","88,168,240","248,248,248","152,224,224","0,0,0","216,160,56","248,216,112","0,96,184","232,240,248","0,0,0","120,104,24","200,152,88","0,120,72","224,192,80","0,200,0","0,0,0","248,248,248","0,0,0","0,200,0","176,0,0","248,0,0","248,88,0","248,160,0","0,96,184","232,240,248","0,0,0","88,88,88","120,120,120","152,152,152","192,192,192","224,224,224","0,0,0","248,248,248","0,0,0","0,200,0","232,24,104","240,64,168","248,120,200","248,192,240","0,96,184","232,240,248","112,112,112","0,0,0","192,192,192","160,200,248","168,224,248","192,248,248","0,0,0","248,248,248","0,0,0","0,200,0","0,224,0","136,248,56","200,248,0","248,248,152","0,96,184","232,240,248","0,0,0","184,168,96","216,248,200","0,128,0","0,200,0","0,248,0","0,0,0","0,0,0","0,0,0","0,0,0","0,0,0","0,0,0","0,0,0","0,0,0","0,96,184","232,240,248","0,0,0","136,88,24","248,8,248","216,160,56","248,216,32","248,248,0","0,0,0","0,0,0","0,0,0","0,0,0","0,0,0","0,0,0","0,0,0","0,0,0","0,96,184","232,240,248","0,0,0","184,0,80","248,0,128","72,72,136","104,104,176","128,128,200","0,0,0","0,0,0","0,0,0","0,0,0","0,0,0","0,0,0","0,0,0","0,0,0","0,96,184","248,248,248","0,0,0","136,88,24","216,160,56","248,216,112","248,208,192","232,0,176","80,0,0","248,64,112","32,48,136","64,128,152","128,216,200","176,40,96","248,112,104","248,248,0","0,96,184","248,248,248","0,0,0","112,112,112","160,160,160","192,192,192","224,224,224","248,16,88","0,0,0","248,248,248","0,0,0","0,200,0","176,0,0","248,0,0","248,88,0","248,160,0","0,96,184","248,248,248","0,0,0","248,120,0","248,192,0","248,248,0","184,40,0","248,136,0","0,0,0","248,248,248","0,0,0","0,200,0","232,24,104","240,64,168","248,120,200","248,192,240","0,96,184","248,248,248","0,0,0","64,64,216","104,104,216","136,136,248","184,40,0","248,136,0","0,0,0","248,248,248","0,0,0","0,200,0","0,224,0","136,248,56","200,248,0","248,248,152","0,96,184","248,248,248","0,0,0","136,0,0","184,0,0","248,0,0","184,40,0","248,136,0","0,0,0","0,0,0","0,0,0","0,0,0","0,0,0","0,0,0","0,0,0","0,0,0","0,96,184","248,248,248","0,0,0","0,120,0","0,184,0","0,248,0","184,40,0","248,136,0","0,0,0","0,0,0","0,0,0","0,0,0","0,0,0","0,0,0","0,0,0","0,0,0","0,96,184","248,248,248","0,0,0","40,48,72","72,80,88","104,104,88","152,144,64","192,192,120","0,0,0","0,0,0","0,0,0","0,0,0","0,0,0","0,0,0","0,0,0","0,0,0","0,96,184","248,248,248","24,72,72","32,112,104","40,136,120","48,160,136","56,184,152","248,0,128","0,0,0","0,0,0","0,0,0","0,0,0","0,0,0","0,0,0","0,0,0","0,0,0"];
}
              
            
!
999px

Console