# include "regio.h"
# include "jlib.h"

# define A 22
# define C 8
# define X 10

# define Y (9+X)
# define Z (5+X)
# define B (40+A)
# define D (40+C)

# define BIT(i)		(1L << (i))

struct bcstate {
	int row, col;
	char *name;
};

static struct bcstate rstate [] = {
	X,	B,	"ÔÏË",
	X+1,	B,	"ÔÐÓ",
	X+2,	B,	"ÔÇÉ",
	X+3,	B,	"ÔÎÁÞÐ",
	X+4,	B,	"ÓÔÒÉ(Ð)",
	X+5,	B,	"ÇÉ",
	X+6,	B,	"ÚÁÐÒÏÓÐ(Ð)",
	X+7,	B,	"ÇÏÔÐ",
	Y,	B,	"ÂÞË",
	Y+1,	B,	"ÂÞÍ",
	Y+2,	B,	"ÂÞÓ",
	Y+3,	B,	"ÂÔ12",
	Z,	D,	"ÏÛÁÍ",
	Z+1,	D,	"ÏÛÁÓ",
	Z+2,	D,	"ÏÛÞÍ",
	Z+3,	D,	"ÏÛÞÓ",
	Z+4,	D,	"ÏÛÞË",
	Z+5,	D,	"ËÒÐ",
	Z+6,	D,	"ÓÞÞÍ",
	Z+7,	D,	"ÓÞÞÓ",
};

static struct bcstate tstate [] = {
	X,	A,	0,
	X+1,	A,	"ÔÐÓ",
	X+2,	A,	"ÔÇÐ",
	X+3,	A,	"ÔÎÁÞÉ",
	X+4,	A,	"ÚÁÐÒÏÓÐ(É)",
	X+5,	A,	"ÇÐ",
	X+6,	A,	"ÓÔÒÉ(É)",
	X+7,	A,	"ÇÏÔÉ",
	Y,	A,	0,
	Y+1,	A,	"ÂÇÐÒ",
	Y+2,	A,	"ÉÍ",
	Y+3,	A,	"ÂÞÄ",
	Z,	C,	"ÏÛÁÍ",
	Z+1,	C,	"ÏÛÁÓ",
	Z+2,	C,	"ÓÞÞÍ",
	Z+3,	C,	"ÓÞÞÓ",
	Z+4,	C,	"ÎÏÛ",
	Z+5,	C,	"ËÒÉ",
	Z+6,	C,	0,
	Z+7,	C,	0,
};

extern halting;				/* ÐÒÏÃÅÓÓÏÒ ÓÔÏÉÔ */

extern long ptrecvl ();

notyet ()
{
	win0 ();
	Jmove (22, 30);
	Jrbold (0x4e);
	Jprintf ("åÝÅ ÎÅ ÒÅÁÌÉÚÏ×ÁÎÏ");
	Jnorm (0x1b);
}

bc0 () { bc (0); }
bc1 () { bc (1); }
bc2 () { bc (2); }
bc3 () { bc (3); }
bc4 () { bc (4); }
bc5 () { bc (5); }
bc6 () { bc (6); }
bc7 () { bc (7); }

bc (n)
{
	register i;
	struct bchan b;

	loadbchan (n, &b);
	Jmprintf (2, 30, "âÁÊÔÏ×ÙÊ ËÁÎÁÌ %d", n);
	Jmprintf (4, 10, "éÓÔÏÞÎÉË %d", n);
	Jmprintf (4, 50, "ðÒÉÅÍÎÉË %d", n);

	Jmprintf (6, 5, "ÕÓË  ");
	prbclcm (&b, 0);
	Jmprintf (7, 5, "ÓÓË  ");
	prbclcm (&b, 1);
	Jmprintf (6, 45, "ÕÓË  ");
	prbclcm (&b, 2);
	Jmprintf (7, 45, "ÓÓË  ");
	prbclcm (&b, 3);

	Jmprintf (X, C-1, "%02x  ÄÁÎÉ", b.td);
	Jmprintf (X, D-1, "%02x  ÄÁÎÐ", b.rd);
	Jmprintf (X+1, C, "%x  ÓÓÉ", b.tis);
	Jmprintf (X+1, D, "%x  ÓÓÐ", b.ris);
	Jmprintf (X+2, C, "%x  ËÂÐÓ", b.tbc);
	Jmprintf (X+2, D, "%x  ËÂÐÓ", b.rbc);

	for (i=0; i<20; ++i) {
		prbcstate (&tstate[i], b.ts & BIT (i) ? '1' : '0');
		prbcstate (&rstate[i], b.rs & BIT (i) ? '1' : '0');
	}
}

prbclcm (b, n)
register struct bchan *b;
register n;
{
	prword (&b->lcm[n]);
	Jprintf ("  %02x", b->lcmt [n] & 0xff);
}

static prbcstate (s, c)
register struct bcstate *s;
{
	if (s->name)
		Jmprintf (s->row, s->col, "%c  %s", c, s->name);
}

static loadbchan (n, r)
register n;
register struct bchan *r;
{
	register long h;
	register i;

	/* enable access to cpu registers */
	stop ();
	ptenable ();

	r->rd = ptrecvb (0100|n, 0334) & 0xff;
	r->td = ptrecvb (0100|n, 0324) & 0xff;

	r->ris = ptrecvb (0100|n, 0333) & 0xf;
	r->tis = ptrecvb (0100|n, 0323) & 0xf;

	r->rbc = ptrecvb (0100|n, 0335) >> 4 & 0xf;
	r->tbc = ptrecvb (0100|n, 0325) >> 4 & 0xf;

	h = ptrecvb (0100|n, 0332) & 0xff;
	h |= (long) (ptrecvb (0100|n, 0335) & 0xf) << 8;
	h |= (long) (ptrecvb (0100|n, 0336) & 0xff) << 12;
	r->rs = h;

	h = ptrecvb (0100|n, 0322) & 0xff;
	h |= (long) (ptrecvb (0100|n, 0325) & 0xf) << 8;
	h |= (long) (ptrecvb (0100|n, 0326) & 0xff) << 12;
	r->ts = h;

	for (i=0; i<4; ++i)
		load1lcm (&r->lcm[i], &r->lcmt[i], 32+n*4+i);

	/* disable access to cpu registers */
	if (! halting)
		run ();
}
