/* cmon2.c
   C monitor program for Lark
   L. Stewart September 24, 1982  9:54 AM
   L. Stewart December 3, 1982  2:37 PM, word mode dumping
 */

extern int gethex();
extern wf();
extern wf1();
extern wf2();
extern wfcr();
extern int innewp();
extern innew();
extern int fetchb();
extern putchar();
extern int chav();
extern int getchar();
extern SetXbar();
extern StartSlave();
extern PIOOn();

extern int audb;

extern char leftover;
extern int any;

/* command: d(startaddr),(stopaddr)(return) */
dump(words)
  int words;
  {
  int start, stop, adr, addr, ba, pv;
  if (words) wf("W ");
  else wf("D ");
  start = gethex();
  if (innewp('\r')) {
    innew(',');
    stop = gethex();
    };
  else any = 0;
  if (!any) { stop = start + 0x7f; any = 1; };
  innew('\r');
  start = start >> 4;
  stop = stop >> 4;
  stop += 1;
  for (addr=start;addr<stop;addr+=1) {
    wf1("%04x:", addr<<4);
    if (words) DW(addr);
    else DB(addr);
    wf("  ");
    for (ba=0;ba<=15;ba+=1) {
      adr = (addr<<4)+ba;
      pv = fetchb(adr) & 0377;
      if ((pv >= 040) && (pv <= 0176)) putchar(pv);
      else putchar('.');
      };
    wfcr();
    if (chav()) {getchar(); break; };  /* give up on character typed */
    };
  };

static DB(addr)
  int addr;
  {
  int ba, adr, pv;
  for (ba=0;ba<=15;ba+=1) {
    adr = (addr<<4)+ba;
    pv = fetchb(adr);
    wf1(" %02x", pv & 0xff);
    };
  };

static DW(addr)
  int addr;
  {
  int ba, adr, pv;
  for (ba=0;ba<=15;ba+=2) {
    adr = (addr<<4)+ba;
    pv = fetchw(adr);
    wf1(" %04x", pv);
    };
  };

togaudio()
  {
  putchar('a');
  if (audb) {
    audb = 0;
    putchar('0');
    PIOOn(0x0020, 0);
    };
  else {
    audb = 1;
    putchar('1');
    SetXbar(0, 3);
    SetXbar(3, 0);
    StartSlave(0, 0);
    };
  };

TraceStack(f)
  int f[];
  {
  int tF[];
  while ((tF=f[0]) && (!chav())) {
    wf2("   BP, IP = (%4xH, %4xH)\r", tF, f[1]);
    f=tF;
    };
  };