/* cmtslc.c   September 24, 1982  2:48 PM   */

#define slrcmd		0x0020
#define slrccbhi	0x0021
#define slrccblo	0x0022
#define sltcmd		0x0024
#define sltccbhi	0x0025
#define sltccblo	0x0026
#define stoffset	0x0004
#define ctoffset	0x0003
#define adoffset	0x0001
#define cmoffset	0x0000

#define slcsio		0x000c
#define slchio		0x000d
#define slcrio		0x000f

#define slcnop		0x00f0
#define slctx		0x0050
#define slcrx		0x0000
#define slcindir	0x0001
#define slcchain	0x0004
#define slcsetmode	0x00e0
#define slcsetaddr	0x00c0

struct ccb {
  char cmd;
  char *addr;
  char count;
  char stat;
  };

struct ccb tlccb, rchain[3], tchain[3];
char tsltmode, tslrmode, teneta;
char ibuf[256], obuf[256];
int sendb, receiveb;

tslc()
  {
  char c;
  obuf[0] = 0x55;
  obuf[1] = 0x55;
  wf("slc: ");
  c = egetchar();
  switch (c) {
    case 'c': tslcinit(); break;
    case 'l': receive(); break;
    case 'r': tslrinit(1); break;
    case 's': send(); break;
    case 'S': lsend(); break;
    case 't': tsltinit(1); break;
    case '1': trone(); break;
    default: wf("?\r"); break;
    };
  };

lsend()  /* loop send */
  {
/*
  for (;;) {
    tchain[0].cmd = slctx;
    tchain[0].addr = obuf;
    tchain[0].count = 64;
    tchain[0].stat = 0;
    sltstart(&tchain[0]);
    if (twaitccb(&tchain[0], 20)) {
      wf("#");
      slrinit(0);
      sltinit(0);
      };
    if (chav()) break;
    };
  getchar();
 */
  };

send()
  {
  if (sendb) {
    sendb = 0;
    tsltinit();
    }
  else {
    sendb = 1;
    tchain[0].cmd = slctx + slcchain;
    tchain[0].addr = obuf;
    tchain[0].count = 64;
    tchain[0].stat = 0;
    tchain[1].cmd = slcnop + slcindir;
    tchain[1].addr = &tchain[0];
    tchain[1].count = 0;
    tchain[1].stat = 0;
    sltstart(&tchain[0]);
    SysHalt()
    };
  if (sendb) wf("tx on\r");
  else wf("tx off\r");
  };

trone()
  {
  tchain[0].cmd = slctx;
  tchain[0].addr = obuf;
  tchain[0].count = 64;
  tchain[0].stat = 0;
  sltstart(&tchain[0]);
  };

receive()
  {
/*
  if (receiveb) {
    receiveb = 0;
    tslrinit();
    };
  else {
    receiveb = 1;
    rchain[0].cmd = slcrx + slcchain;
    rchain[0].addr = ibuf;
    rchain[0].count = 0;
    rchain[0].stat = 0;
    rchain[1].cmd = slcnop + slcindir;
    rchain[1].addr = &rchain[0];
    rchain[1].count = 0;
    rchain[1].stat = 0;
    slrstart(&rchain[0]);
    };
  if (receiveb) wf("rx on\r");
  else wf("rx off\r");
*/
  };

tslcinit()
  {
  teneta = 0x55;
  tslrinit(1);
  tsltinit(1);
  };

tslrinit(v)
  int v;
  {
  receiveb = 0;
  if (v) wf("tslrinit,");
  tslrmode = 0;
  outbyte(slrcmd, slcrio);
  tlccb.cmd = slcsetmode;
  tlccb.addr = &tslrmode;
  tlccb.count = 0xff;
  tlccb.stat = 0;
  slrstart(&tlccb);
  if (twaitccb(&tlccb, 50) && v) wf(" t.o.");
  if (v) wf1(" mode status: %02x,", tlccb.stat & 0xff);
  tlccb.cmd = slcsetaddr;
  tlccb.addr = &teneta;
  tlccb.count = 0xff;
  tlccb.stat = 0;
  slrstart(&tlccb);
  if (twaitccb(&tlccb, 50) && v) wf(" t.o.");
  if (v) wf1(" addr status: %02x\r", tlccb.stat & 0xff);
  };
  
tsltinit(v)
  int v;
  {
  sendb = 0;
  tsltmode = 0;
  if (v) wf("tsltinit,");
  outbyte(sltcmd, slcrio);
  tlccb.cmd = slcsetmode;
  tlccb.addr = &tsltmode;
  tlccb.count = 1;
  tlccb.stat = 0;
  sltstart(&tlccb);
  if (twaitccb(&tlccb, 50) && v) wf(" t.o.");
  if (v) wf1(" mode status: %02x\r", tlccb.stat & 0xff);
  };

twaitccb(p, t)
  struct ccb *p;
  int t;
  {
  int tim;
  settmr(t, &tim);
  for (;;) {
    if (p->stat != 0) return (0);
    if (tmrexp(&tim)) return (1);
    };
  };