MediaWiki:Common.js

From NeoGeo Development Wiki
Jump to navigation Jump to search

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
/* Any JavaScript here will be loaded for all users on every page load. */

(function () {

  // NeoGeo Color converter
  var cconvdiv = document.getElementById('color-converter');
  if (cconvdiv != null) {
    cconvdiv.innerHTML = '<form name="cconvform" action="">' +
                         'Red:<input type="text" maxlength="3" size="4" id="color-red">/255<br>' +
                         'Green:<input type="text" maxlength="3" size="4" id="color-green">/255<br>' +
                         'Blue:<input type="text" maxlength="3" size="4" id="color-blue">/255<br>' +
                         '<input type="button" value="Convert" onClick="cconv();"></form>';
  }

  // NeoGeo Mirror guesser
  var mguessdiv = document.getElementById('mirror-guesser');
  if (mguessdiv != null) {
    mguessdiv.innerHTML = '<form name="mguessform" action="">' +
                         'Address (hex):<input type="text" maxlength="6" size="6" id="address">' +
                         '<input type="button" value="Guess" onClick="mguess();"></form>';
  }
}());

function mguess(frm) {

  var address = parseInt(document.forms["mguessform"]["address"].value, 16);

  var reg = '';

  if ((address & 0xFE0001) == 0x300000) reg = 'REG_P1CNT';
  if ((address & 0xFE0081) == 0x300001) reg = 'REG_DIPSW';
  if ((address & 0xFE0081) == 0x300081) reg = 'REG_SYSTYPE';
  if ((address & 0xFE0001) == 0x320000) reg = 'REG_SOUND';
  if ((address & 0xFE0001) == 0x320001) reg = 'REG_STATUS_A';
  if ((address & 0xFE0001) == 0x340000) reg = 'REG_P2CNT';
  if ((address & 0xFE0001) == 0x380000) reg = 'REG_STATUS_B';
  if ((address & 0xFE0071) == 0x380001) reg = 'REG_POUTPUT';
  if ((address & 0xFE0071) == 0x380011) reg = 'REG_CRDBANK';
  if ((address & 0xFE00F1) == 0x380021) reg = 'REG_SLOT';
  if ((address & 0xFE00F1) == 0x380031) reg = 'REG_LEDLATCHES';
  if ((address & 0xFE00F1) == 0x380041) reg = 'REG_LEDDATA';
  if ((address & 0xFE00F1) == 0x380051) reg = 'REG_RTCCTRL';
  if ((address & 0xFE00F1) == 0x380061) reg = 'REG_RESETCC1';
  if ((address & 0xFE00F1) == 0x380063) reg = 'REG_RESETCC2';
  if ((address & 0xFE00F1) == 0x380065) reg = 'REG_RESETCL1';
  if ((address & 0xFE00F1) == 0x380067) reg = 'REG_RESETCL2';
  if ((address & 0xFE00F1) == 0x3800D1) reg = 'REG_RTCOUT';
  if ((address & 0xFE00F1) == 0x3800E1) reg = 'REG_SETCC1';
  if ((address & 0xFE00F1) == 0x3800E3) reg = 'REG_SETCC2';
  if ((address & 0xFE00F1) == 0x3800E5) reg = 'REG_SETCL1';
  if ((address & 0xFE00F1) == 0x3800E7) reg = 'REG_SETCL2';

  if ((address & 0xFE0001) == 0x3A0001) reg = 'REG_NOSHADOW';
  if ((address & 0xFE0001) == 0x3A0021) reg = 'REG_SHADOW';
  if ((address & 0xFE0001) == 0x3A0003) reg = 'REG_SWPBIOS';
  if ((address & 0xFE0001) == 0x3A0013) reg = 'REG_SWPROM';
  if ((address & 0xFE0001) == 0x3A0005) reg = 'REG_CRDUNLOCK1';
  if ((address & 0xFE0001) == 0x3A0015) reg = 'REG_CRDLOCK1';
  if ((address & 0xFE0001) == 0x3A0007) reg = 'REG_CRDLOCK2';
  if ((address & 0xFE0001) == 0x3A0017) reg = 'REG_CRDUNLOCK2';
  if ((address & 0xFE0001) == 0x3A0009) reg = 'REG_CRDREGSEL';
  if ((address & 0xFE0001) == 0x3A0019) reg = 'REG_CRDNORMAL';
  if ((address & 0xFE0001) == 0x3A000B) reg = 'REG_BRDFIX';
  if ((address & 0xFE0001) == 0x3A001B) reg = 'REG_CRTFIX';
  if ((address & 0xFE0001) == 0x3A000D) reg = 'REG_SRAMLOCK';
  if ((address & 0xFE0001) == 0x3A001D) reg = 'REG_SRAMUNLOCK';
  if ((address & 0xFE0001) == 0x3A000F) reg = 'REG_PALBANK1';
  if ((address & 0xFE0001) == 0x3A001F) reg = 'REG_PALBANK0';

  if ((address & 0xFE0001) == 0x3C0000) reg = 'REG_VRAMADDR';
  if ((address & 0xFE0001) == 0x3C0002) reg = 'REG_VRAMRW';
  if ((address & 0xFE0001) == 0x3C0004) reg = 'REG_VRAMMOD';
  if ((address & 0xFE0001) == 0x3C0006) reg = 'REG_LSPCMODE';
  if ((address & 0xFE0001) == 0x3C0008) reg = 'REG_TIMERHIGH';
  if ((address & 0xFE0001) == 0x3C000A) reg = 'REG_TIMERLOW';
  if ((address & 0xFE0001) == 0x3C000C) reg = 'REG_IRQACK';
  if ((address & 0xFE0001) == 0x3C000E) reg = 'REG_TIMERSTOP';

  if ((address & 0xFF0000) == 0xFF0000) reg = 'CD';


  if (reg == 'CD') {
    text = 'Sorry, NeoGeo CD register mirrors can\'t be guessed for now :(';
  } else if (reg == '') {
    text = 'Can\'t find a match for $' + address.toString(16).toUpperCase() + ' :(';
  } else {
    text = '$' + address.toString(16).toUpperCase() + ' should be a mirror of ' + reg + '.';
  }

  document.getElementById('mguessresult').innerHTML = text;

}

function cconv(frm) {

  var red = Math.min(255, document.forms["cconvform"]["color-red"].value);
  var green = Math.min(255, document.forms["cconvform"]["color-green"].value);
  var blue = Math.min(255, document.forms["cconvform"]["color-blue"].value);

  var icolor = red + "," + green + "," + blue;

  var luma = Math.floor((54.213*red) + (182.376*green) + (18.411*blue)) & 1;

  red = Math.floor(red / 8);
  green = Math.floor(green / 8);
  blue = Math.floor(blue / 8);

  var ocolor = (((red<<1)+luma)<<2) + "," + (((green<<1)+luma)<<2) + "," + (((blue<<1)+luma)<<2);

  var hex = (((luma^1)<<15)|((red&1)<<14)|((green&1)<<13)|((blue&1)<<12)|((red&0x1E)<<7)|((green&0x1E)<<3)|(blue>>1)).toString(16).toUpperCase();
  while (hex.length < 4) {
    hex = "0" + hex;
  }

  document.getElementById('cconvresult').innerHTML = 'NeoGeo color: $' + hex + '<br>In: <div style="width:64px; height:32px; background:rgb(' + icolor + ');"></div>  Out:<div style="width:64px; height:32px; background:rgb(' + ocolor + ');"></div> ';

}