/*
yasep/tools/opcode_map.js created on 2009-07-24 from
yasep/tools/opcode_map.html created by whygee at f-cpu.org
2006-09-06 : moved the form explanations to assembly.html
   (this also simplifies the handling of the anchors)
2007-04-11
2007-11-05 : rebranding to YASEP
2008-11-17
2009-01-06 : split the table into 2 parts
2009-01-18 : style changes
2009-01-23 : moved from /docs/ to /tools/
2009-07-11 : js_list2.js
2009-07-28 : Y
2009-08-09
*/

function init() {

// OPCODE TABLE

  var m='<tr> <td colspan="2"></td> <td colspan="8" class="haut">Function</td> </tr> <td></td> <td></td>';
  var index, instr, t;

  for (var col=0; col<8; col++)
    m+=' <td class="haut">'+int2hex(col<<5,1,1)+'</td>\n';
  m+='</tr><tr><td class="haut" rowspan="16">Group</td>\n';

  for (var line=0; line<32; line+=4) { // group number

    m+='<td class="side">'+int2hex(line,1,1);
    if  (typeof Y.GroupNames[line]=="string")
      m+='<br>'+Y.GroupNames[line].replace(/GROUP_/,'');
    m+='</td>\n';

    for (var col=0; col<8; col++) {
      index=line|(col<<5);
      instr=Y.opcode_table[index];
      if (typeof instr == 'object') {
        t=Y.decorate_opcode(index);
        m+='<td title="'+instr.description+'" id="'+instr.name+'"><a name="'+instr.name+'">'
         +t+' <span style="float:right">'+int2hex(index,1,1)+'</span><br>'+Y.form2html(instr.forms,1)
         +'<br>'+Y.flags2html(instr.flags)+'</a></td>\n';
      }
      else
        m+='<td></td>\n';
    }
    m+='</tr><tr>\n';
  }
  document.getElementById("optab").innerHTML=m;

// ALIASES
  m='<tr><td>Alias</td><td>Forms</td><td>Real opcode</td></tr>';
  var j=0;
  for (var i in Y.opcode_aliases) {
    m+='<tr';
    if (j^=1)
      m+=' bgcolor="#EEEEFF"';
    m+='><td class="f"><a name="'+i+'" href="'+prfx+'ISM/'+i+'.html">'+i+'</a></td>'
     +'<td class="f">'+Y.form2html(Y.table_opcode[i].forms)+'</td>'
     +'<td class="f"><a href="#'+Y.opcode_aliases[i]+'">'+Y.opcode_aliases[i]+'</a></td>'
     +'</tr>\n';
  }
  document.getElementById("aliases").innerHTML=m;

/// FORMS
  m='<tr><td>Form</td><td>Count</td><td>Opcodes</td></tr>';

  // gathering the stats :
  var i, t;
  var FormUsage=new Array();
  var FlagUsage=new Array();
  for (i=1; i<Y.MAX_FORM; i+=i)
    FormUsage[i]=new Array();
  for (i=1; i<Y.MAX_FLAG; i+=i)
    FlagUsage[i]=new Array();

  // scan the list of opcodes
  for (var name in Y.table_opcode) {
    if (Y.table_opcode[name].forms>0) {
      instr=Y.table_opcode[name];
      t='<a href="#'+name+'">'+name+'</a>'  ;
      i=1;
      while (i<=instr.forms) {
        if (i & instr.forms)
          FormUsage[i][FormUsage[i].length]=t;
        i+=i;
      }
      i=1;
      while (i<=instr.flags) {
        if (i & instr.flags)
          FlagUsage[i][FlagUsage[i].length]=t;
        i+=i;
      }
    }
  }
  var j=0;
  for (var i in Y.TableForm) {
    m+=('<tr');
    if (j^=1)
      m+=' bgcolor="#EEEEFF"';
    m+='><td class="f"><a href="'+prfx+'docs/instructions.html#'
     +Y.TableForm[i].replace(/FORM_/,'')+'">'+Y.TableForm[i]+"</a></td>"
     +'<td class="f">'+FormUsage[i].length+"</td>"
     +'<td class="f">\n'+FormUsage[i].sort().join(" ")+"</td></tr>\n";
  }
  document.getElementById("forms").innerHTML=m;

// FLAGS
  m='<tr><td>Flag</td><td>Count</td><td>Opcodes</td></tr>';
  j=0;
  for (var i in Y.TableFlags) {
    m+='<tr';
    if (j^=1)
      m+=' bgcolor="#EEEEFF"';
    m+='><td class="f"><a href="'+prfx+'docs/flags.html#'
     +Y.TableFlags[i].replace(/FLAG_/,'')+'">'+Y.TableFlags[i]+"</a></td>"
     +'<td class="f">'+FlagUsage[i].length+"</td>"
     +'<td class="f">\n'+FlagUsage[i].sort().join(" ")+"</td></tr>\n";
  }

  document.getElementById("flags").innerHTML=m;

  setTimeout(init2,10);
}

function init2() {
  // highlight of the searched item :
  var t=document.location.hash.replace(/[#]/,'');
  if (t=='')
    return;

  if ((t=='forms')||(t=='flags')||(t=='aliases')) {
    // it must be focused again because the page height changed after the JS code and the images are loaded.
    document.getElementById(t).focus();
    return;
  }

  t=t.toUpperCase();
  if (Y.table_opcode[t]!=undefined) {
    /* aliases
    if (opcode_aliases[t])
      t=opcode_aliases[t]; */
    var i=document.getElementById(t);
    if (i) {
      i.style.backgroundColor="#FFFFA0";
      i.getElementsByTagName("a")[2].focus(); // the focus is somewhat... not as expected.
    }
  }
  else {
    document.getElementById('comment').innerHTML='Beware : '+t+' is not a valid opcode<br>';
  }
}

addLoadEvent(init);
