Проектування комп'ютера
} else if (opcode == xorf) {
state. reg [arg2] = state. reg [arg0] xor state. reg [arg1];
} else if (opcode == cmpge) {
state. reg [arg2] = state. reg [arg0] >= state. reg [arg1];
} else if (opcode == jmae) {
if ( (unsigned) state. reg [arg0] >= (unsigned) state. reg [arg1])
state. pc += addressField;
} else if (opcode == jmnae) {
if ( (unsigned) state. reg [arg0] < (unsigned) state. reg [arg1])
state. pc += addressField;
} else if (opcode == bsf) {
state. ZF=0;
for (i=0; i<31; i++) {
if (state. reg [arg0] %2==1) {
state. ZF=1;
state. reg [arg1] =i;
break;
}
else {
state. reg [arg0] =state. reg [arg0] >>1;
}
}
} else if (opcode == bsr) {
state. ZF=0;
for (i=31; i>0; i--) {
if (state. reg [arg0] /0x80000000==1) {
state. ZF=1;
state. reg [arg1] =i;
break;
}
else {
state. reg [arg0] =state. reg [arg0] <<1;
}
}
} else if (opcode == jne) {
if (state. ZF! =0) state. pc = addressField;
} else if (opcode == push) {
if (state. sStack. size () > STACKDEPTH - 1)
{
printf ("nerror: stack overflow! 0x%xn", opcode);
printf ("total of %d instructions executedn", instructions+1);
printf ("final state of machine: n");
printState (&state);
exit (1);
}
else state. sStack. push_back (state. reg [1]);
} else if (opcode == pop) {
if (state. sStack. empty ())
{
printf ("nerror: stack underflow! 0x%xn", opcode);
printf ("total of %d instructions executedn", instructions+1);
printf ("final state of machine: n");
printState (&state);
exit (1);
}
else
{
state. reg [1] = state. sStack. back ();
state. sStack. pop_back ();
}
} else {
printf ("error: illegal opcode 0x%xn", opcode);
exit (1);
}
state. reg [0] = 0;
}
}
void
printState (stateType *statePtr)
{
int i;
printf ("n@@@nstate: n");
printf ("tpc %dn", statePtr->pc);
printf ("ttZF = %dn",statePtr->ZF);
vector <int>:: iterator stackiter;
printf ("tstack: n");
for (stackiter = statePtr->sStack. begin (), i = 0; stackiter < statePtr->sStack. end (); stackiter++, i++) {
printf ("ttstek [%d] %dn", i, *stackiter);
}
printf ("tmemory: n");
for (i=0; i<statePtr->numMemory; i++) {
printf ("ttmem [%d] %dn", i, statePtr->mem [i]);
}
printf ("tregisters: n");
for (i=0; i<NUMREGS; i++) {
printf ("ttreg [%d] %dn", i, statePtr->reg [i]);
}
printf ("end staten");
}
int convertNum (int num)
{
/* convert a 16-bit number into a 32-bit Sun integer */
if (num & (1<<15)) {
num - = (1<<16);
}
return (num);
}