/* testruntime.c   June 11, 1982  2:42 PM   */

char leftover;
int args[10];
int ta[10];
int mlerr[3];
int any;

main()
  {
  char c;
  int i, result;
  wf("\rRuntimeTest.\r");
  result = ssenable(mlerr);
  if (result) wf(" ##\r");
  for (;;) {
      wf("% ");
      c = getchar() & 0177;
      switch (c) {
        case 'q': {
          wf("\rQuit!\r");
          return;
          };
        case '1': { getarg(0); break; };
        case '2': { getarg(1); break; };
        case '3': { getarg(2); break; };
        case '4': { getarg(3); break; };
        case 'p': { printargs(); break; };
	case 'c': { testcompare(); break; };
        case 'a': { testarith(); break; };
        default: wf("\r unknown command\r");
        };
 };  /* end of forever loop */
 };

copyargs()
  {
  int i;
  for (i = 0; i < 10; i +=1 ) { ta[i] = args[i]; };
  };

getarg(an)
  int an;
  {
  wf1("arg word %d: ", an);
  args[an] = gethex();
  wfcr();
  printarg(an);
  };

printargs()
  {
  int i;
  for (i = 0; i<4; i+=1) printarg(i);
  };

printarg(i)
  int i;
  {
  wf3("arg %d = %xH, %d\r", i, args[i], args[i]);
  };

printta(i)
  int i;
  {
  wf3("arg %d = %xH, %d\r", i, ta[i], ta[i]);
  };

printtapair(i)
  int i;
  {
  printta(i);
  printta(i+1);
  };

testcompare()
  {
  copyargs();
  ta[4] = Min(ta[0], ta[2]);  /* compare low order words */
  ta[5] = Max(ta[0], ta[2]);
  ta[7] = UMax(ta[0], ta[2]);
  ta[8] = Usc(ta[0], ta[2]);
  wf("Min = ");
  printta(4);
  wf("Max = ");
  printta(5);
  wf("UMax = ");
  printta(7);
  wf("Usc = ");
  printta(8);
  };

testarith()
  {
  copyargs();
  ta[4] = DoubleIncrement(&ta[0], ta[2]);
  wf("DoubleIncrement returns ");
  printta(4);
  wf(" newval = ");
  printtapair(0);
  copyargs();
  ta[4] = DoubleDifference(&ta[0], &ta[2]);
  wf("DoubleDifference returns ");
  printta(4);
  wf(" newval = ");
  printtapair(0);
  copyargs();
  ta[4] = DoubleInc(&ta[0], ta[2]);
  wf("DoubleInc returns ");
  printta(4);
  wf(" newval = ");
  printtapair(0);
  copyargs();
  ta[4] = DoubleDiff(&ta[0], &ta[2]);
  wf("DoubleDiff returns ");
  printta(4);
  wf(" newval = ");
  printtapair(0);
  copyargs();
  ta[4] = DoubleEq(&ta[0], &ta[2]);
  wf("DoubleEq returns ");
  printta(4);
  copyargs();
  ta[4] = MultEq(&ta[0], &ta[2], 2);
  wf("MultEq returns ");
  printta(4);
  ta[4] = MultEq(&ta[0], &ta[0], 4);
  wf("MultEq (4) returns ");
  printta(4);
  };

mlabort()
  {
  ssreturn(mlerr, 1);
  };

int egetchar()  /* getchar with echo */
  {
  leftover = getchar() & 0177;
  putcrchar(leftover);
  if (leftover == 0177) mlabort();
  return(leftover);
  };

int gethex()
  {
  int v;
  v = 0;
  any = 0;
  for (;;) {
    egetchar();
    if (isdigit(leftover)) {
      v = (v<<4);
      v += (leftover-'0');
      any = 1;
      continue;
      };
    leftover = lc(leftover);
    if (leftover>='a' && leftover<='f') {
      v = (v<<4);
      v += (leftover-'a'+10);
      any = 1;
      continue;
      };
    break;
    };
  return(v);
  };