/*
  benches/test_eu.js
  created 2009-07-28 from benches/test_eu16.html
  2009-08-10
*/

function inc_ptr1() {
  document.update_form.value_ptr1.value=
     int2hex((str2int(document.update_form.value_ptr1.value)+1)& mask_ptr);
}

function dec_ptr1() {
  document.update_form.value_ptr1.value=
     int2hex((str2int(document.update_form.value_ptr1.value)-1)& mask_ptr);
}

function inc_snd() {
  document.update_form.value_snd.value=
     int2hex((str2int(document.update_form.value_snd.value)+1)&mask_data);
}

function dec_snd() {
  document.update_form.value_snd.value=
     int2hex((str2int(document.update_form.value_snd.value)-1)&mask_data);
}

function inc_si4() {
  document.update_form.value_si4.value=
     int2hex((str2int(document.update_form.value_si4.value)+1)&mask_data);
}

function dec_si4() {
  document.update_form.value_si4.value=
     int2hex((str2int(document.update_form.value_si4.value)-1)&mask_data);
}

function update_page(){
  var k=0;
  S.ptr_in= mask_ptr & str2int(document.update_form.value_ptr1.value);
  S.snd=str2int(document.update_form.value_snd.value);
  S.si4=str2int(document.update_form.value_si4.value);
  document.update_form.value_ptr1.value=int2hex(S.ptr_in);
  document.update_form.value_snd.value=int2hex(S.snd);
  document.update_form.value_si4.value=int2hex(S.si4);

  for (var a in ops) { // scan the 2nd list of opcodes
    S.carry=null;
    S.result=null;
    if (test16)
      Y.opcode_table[a].action16(S);
    else
      Y.opcode_table[a].action32(S);
    ops[a].id.innerHTML = int2hex(S.result);
    // Carry
    if (S.carry!=null) {
      if (S.carry==1)
        ops[a].id.style.background="#FF4444";
      else
        ops[a].id.style.background="#44FF44";
    }
  }
}

function init_page() {
  // define global variables :
  eus=new Array(); // array of the group names
  ops=new Array();
  result_ids=new Array(),
  test16=(document.location.pathname.indexOf("test_eu16.html")>=0),
  mask_ptr=3,
  mask_data=F8,
  welcome='<b>test_eu32.html</b>';
  if (test16) {
    mask_ptr=1;
    mask_data=F4;
    welcome='<b>test_eu16.html</b>';
  }

  /* create the 2-level list of the desired instructions : */

  for (var index in Y.opcode_table) {
    if ((test16 && (typeof(Y.opcode_table[index].action16) == "function"))
    || (!test16 && (typeof(Y.opcode_table[index].action32) == "function"))) {
      var group = index & Y.FIELD_GROUP; // extract the group number
      if (!eus[group]) // if the group is not yet registered
        eus[group]=new Array(); // then add this group to the list
      eus[group][index]=0; // add the opcode to the group's list
      ops[index]=new Object();
      ops[index].name=Y.opcode_table[index].name;
    }
  }

  // Scan the 2-list in another more natural order
  var m="";
  for (var a in eus) {
    m+='<td><table class="regs" cellspacing="0">'
       +'<tr><td colspan="2" class="head">'+Y.GroupNames[a]+'</td></tr>'
       +'<tr><td>&nbsp;&nbsp;OP:&nbsp;&nbsp;</td><td>&nbsp;Result:&nbsp;</td></tr>';
    var c=eus[a];

    // scans through the 2nd level list
    for (var b in c) { // b is the opcode index
      m+='<tr><td class="CQ"><a href="'+prfx+'ISM/'+Y.opcode_table[b].name+'.html">'+Y.opcode_table[b].name+'</a>:</td>'
        +'<td class="CQ"><tt id="res_'+b+'">x</tt></td></tr>';
    }
    m+='</table></td>';
  }

  document.getElementById("opcode_dump").innerHTML=m;
  messages = document.getElementById('messages');

  for (var a in ops) // gather the area IDs
    ops[a].id = document.getElementById('res_'+a);

  S=Yasep_Sim(); // the instance of the simulator

  update_page();
  add_message('blue', welcome+' version 2009-08-08');
}
addLoadEvent(init_page);
