csim.xul  |  csim.js  |  options.xul  |  options.js  |  about.xul  |  consts.js  |  screen.js  |  platform.js  |  csim.dtd  |  csim.css  |  b2a.c

var
  colors = { bc: new Array, tc: new Array };
  
with (colors) {
  tc[30] = ['black', 'dimgray'];
  tc[31] = ['darkred', 'red'];
  tc[32] = ['green', 'lime'];
  tc[33] = ['gold', 'yellow'];
  tc[34] = ['navy', 'blue'];
  tc[35] = ['purple', 'fuchsia'];
  tc[36] = ['teal', 'aqua'];
  tc[37] = ['ghostwhite', 'white'];
  
  bc[40] = ['black', 'dimgray'];
  bc[41] = ['darkred', 'red'];
  bc[42] = ['green', 'lime'];
  bc[43] = ['gold', 'yellow'];
  bc[44] = ['navy', 'blue'];
  bc[45] = ['purple', 'fuchsia'];
  bc[46] = ['teal', 'aqua'];
  bc[47] = ['ghostwhite', 'white'];
}

var
  scrEmptyLine, scrDefaultLine,
  escape = { status: 'off', param: new Array },
  curr   = { bold: 0, bc: 0, tc: 0 };

function remapAscii(value) {
  var map = new Array;

  map[000] = 0;
  map[176] = 0x2591;
  map[177] = 0x2592;
  map[178] = 0x2593;
  map[179] = 0x2502;
  map[180] = 0x2524;
  map[181] = 0x2561;
  map[182] = 0x2562;
  map[183] = 0x2556;
  map[184] = 0x2555;
  map[185] = 0x2563;
  map[186] = 0x2551;
  map[187] = 0x2557;
  map[188] = 0x255d;
  map[189] = 0x255c;
  map[190] = 0x255b;
  map[191] = 0x2510;
  map[192] = 0x2514;
  map[193] = 0x2534;
  map[194] = 0x252c;
  map[195] = 0x251c;
  map[196] = 0x2500;
  map[197] = 0x253c;
  map[198] = 0x255e;
  map[199] = 0x255f;
  map[200] = 0x255a;
  map[201] = 0x2554;
  map[202] = 0x2569;
  map[203] = 0x2566;
  map[204] = 0x2560;
  map[205] = 0x2550;
  map[206] = 0x256c;
  map[207] = 0x2567;
  map[208] = 0x2568;
  map[209] = 0x2564;
  map[210] = 0x2565;
  map[211] = 0x2559;
  map[212] = 0x2558;
  map[213] = 0x2552;
  map[214] = 0x2553;
  map[215] = 0x256b;
  map[216] = 0x256a;
  map[217] = 0x2518;
  map[218] = 0x250c;
  map[219] = 0x2588;
  map[220] = 0x2585;
  map[221] = 0x258c;
  map[222] = 0x2590;
  map[223] = 0x2580;
  map[000] = 0;
  map[254] = 0x25a0;
  map[000] = 0;

  if (value < 32) alert('value ' + value + ' is smaller 32');
  return value < 176 ? value : map[value];
}

function chrInitScr() {
  var
    vscreen = document.getElementById('vscr'),
    tmpDescr = document.createElement('description'),
    tmpSpan = document.createElement('span');
  
  tmpDescr.setAttribute('style', 'margin-top:1px; margin-bottom:1px;');
  tmpSpan.appendChild(document.createTextNode('.'));
  tmpSpan.setAttribute('style', 'color:' + colors.bc[40][0]);
  
  for (var i = 0; i != 80; i++)
    tmpDescr.appendChild(tmpSpan.cloneNode(true));

  for (var i = 0; i != 26; i++)  // 25 generates render error
    vscreen.appendChild(tmpDescr.cloneNode(true));
}

function chrUpdateScr(value) {
  var vscreen = document.getElementById('vscr');
  
  function nextLine() {
    if (++cursor.y == 25) {
      var scrNewLine = vscreen.lastChild.cloneNode(true);
      for (var i = 0; i != 80; i++)
        scrNewLine.childNodes[i].setAttribute('style', 'background-color:' + curr.bc[0] + '; color:' + curr.bc[0]);
      vscreen.removeChild(vscreen.firstChild);
      vscreen.insertBefore(scrNewLine, vscreen.lastChild);
      cursor.y--;
    }
  }

  with (escape)
    switch (status) {
 
      case 'off':
        if (value == 10) {
          cursor.x = 0;
          nextLine();
          break;
        }
        if (value == 13)
          break;  // two lines of code for the brave m$ people
        if (value == 27) {
          status = 'wait_br';
          break;
        }
        
        // visible char handled down here
        var textCell = vscreen.childNodes[cursor.y].childNodes[cursor.x++];
        if (value == 32) {
          textCell.setAttribute('style', 'background-color:' + curr.bc[0] + '; color:' + curr.bc[0]);
          textCell.firstChild.nodeValue = '.';
        }
        else {
          textCell.setAttribute('style', 'background-color:' + curr.bc[0] + '; color:' + curr.tc[curr.bold]);
          textCell.firstChild.nodeValue = String.fromCharCode(remapAscii(value));
        }
        if (cursor.x == 80) {
          cursor.x = 0;
          nextLine();
        }
        break;

      case 'wait_br':
        if (value == '['.charCodeAt(0)) {
          param.length = 0;
          status = 'wait_param_or_func';
        }
        else
          status = 'off';
        break;
      
      case 'wait_param_or_func':
        if (value >= '0'.charCodeAt(0) && value <= '9'.charCodeAt(0)) {
          param.push(String.fromCharCode(value));
          status = 'rec_param';
          if (value == '2'.charCodeAt(0))
            status += '_cls';
          break
        }
        if (value == 'K'.charCodeAt(0)) {
          for (var k = cursor.x; k != 80; k++)
            with (document.getElementById('vscr').childNodes[cursor.y].childNodes[k])
              setAttribute('style', 'background-color:' + curr.bc[0] + '; color:' + curr.bc[0]);
          with (cursor)
            y = x = 0;
          status = 'off';
          break;
        }
      
      case 'rec_param':
      case 'rec_param_cls':
        if (value >= '0'.charCodeAt(0) && value <= '9'.charCodeAt(0)) {
          param[param.length - 1] += String.fromCharCode(value);
          break;
        }
        if (value == ';'.charCodeAt(0)) {
          status = 'wait_param_or_func';
          break;
        }
        if (value == 'H'.charCodeAt(0) || value == 'f'.charCodeAt(0)) {
          cursor.x = param[1] - 1;  // ask the ansi guys about this substraction
          cursor.y = param[0] - 1;
        }
        if (value == 'A'.charCodeAt(0))
          if ((cursor.y -= param.length ? param[0] - 0 : 1) < 00) cursor.y = 00;
        if (value == 'B'.charCodeAt(0))
          if ((cursor.y += param.length ? param[0] - 0 : 1) > 24) cursor.y = 24;
        if (value == 'C'.charCodeAt(0))
          if ((cursor.x += param.length ? param[0] - 0 : 1) > 79) cursor.x = 79;
        if (value == 'D'.charCodeAt(0))
          if ((cursor.x -= param.length ? param[0] - 0 : 1) < 00) cursor.x = 00;
        if (value == 'J'.charCodeAt(0) && status == 'rec_param_cls') {
          for (var i = 0; i != 25; i++)
            for (var k = 0; k != 80; k++)
              with (document.getElementById('vscr').childNodes[i].childNodes[k])
                setAttribute('style', 'background-color:' + curr.bc[0] + '; color:' + curr.bc[0]);
          with (cursor)
            y = x = 0;
        }
        if (value == 'm'.charCodeAt(0))
          while (param.length) {
            var tmp = param.shift() - 0;
            if (tmp == 00) {
              with (curr) {
                bold = 0;
                tc = colors.tc[37];
                bc = colors.bc[40];
              }
              continue;
            }
            if (tmp == 01) {
              curr.bold = 1;
              continue;
            }
            if (tmp >= 30 && tmp <= 37) {
              curr.tc = colors.tc[tmp];
              continue;
            }
            if (tmp >= 40 && tmp <= 47)
              curr.bc = colors.bc[tmp];
          }
        status = 'off';
        break;
    }
}