
/*	kiwisubs.c - frequently used functions for kiwi diagnostics  */


#include "kiwi.h"
#include "diag.h"
#include "rabits.h"
#include "diagdat.h"

#define eMsg(x) xprintf(x)

extern char E2[];

extern short csBuff[];		/* this stuff is the 8x305 microcode */
extern unsigned buffSize;

/*------------------------------------------------------------------*/
/*	load the microcode into the kiwi control store ram */

kload(addr, size)
short	*addr;
unsigned size;
{
	unsigned	cnt;
	char	error;
	short	*csAddr;

	kiwi1->mc = 0x00;	/* reset 8x305 */
	kiwi1->mc |= run | dl | rwe;	/* set up for download */
	cnt = 0;
	csAddr = addr;
	while (cnt < size) {
		kiwi1->dwld.w = *csAddr;
		csAddr++;
		kiwi1->mc |= step;
		cnt++;
	}
	kiwi1->mc = 0x00;
	kiwi1->mc |= run | dl;		/* set up to check ucode */
	cnt = error = 0;
	csAddr = addr;
	while (cnt < size) {
		if (*csAddr != kiwi1->dwld.w) {
			error = 1;
			cnt = size;		/* do this to stop loop */
		}
		csAddr++;
		kiwi1->mc |= step;
		cnt++;
	}
	kiwi1->mc = 0x00;
	if (error) 
		return(FALSE);
	else 
		return(TRUE);
}

/*------------------------------------------------------------------*/
/*	load the microcode into the kiwi control store ram */

kload2(addr, size)
short	*addr;
unsigned size;
{
	unsigned	cnt;
	char	error;
	short	csAddr;
	short *p;

	kiwi1->mc = 0x00;	/* reset 8x305 */
	kiwi1->mc |= run | dl | rwe;	/* set up for download */
	csAddr = 0;	/* address of control store window */
	cnt = 0;	/* counter for loop termination */
	p = addr;	/* pointer for array indexing */
	while (cnt < size) {
		if (*p < csAddr) {
			csAddr = 0;	/* address of control store window */
			kiwi1->mc = 0x00;	/* reset 8x305 */
			kiwi1->mc |= run | dl | rwe;	/* set for download */
			}
		while (*p != csAddr) {	/* loop til addresses equal */
			kiwi1->mc |= step;
			csAddr++;
		}
		p++;
		kiwi1->dwld.w = *p++;
		cnt += 2;
		csAddr++;
		kiwi1->mc |= step;
	}
	kiwi1->mc = 0x00;	/* reset 8x305 */
	kiwi1->mc |= dl | rwe;
	kiwi1->mc |= run; 	/* set up for download */
	csAddr = 0;
	cnt = 0;
	p = addr;
	error = 0;
	while (cnt < size) {
		while (*p != csAddr) {	/* loop til addresses equal */
			kiwi1->mc |= step;
			csAddr++;
		}
		p++;
		if (*p++ != kiwi1->dwld.w) {
			error = 1;
			cnt = size;	 /* do this to stop loop */
		}
		else {	
			cnt += 2;
			csAddr++;
			kiwi1->mc |= step;
		}
	}
	kiwi1->mc = 0x00;
	if (error) 
		return(FALSE);
	else 
		return(TRUE);
}


/*------------------------------------------------------------------*/
/* write to the 8x305's IV bus using the BUG monitor.
*/
wrIV(addr, data, bank)
unsigned char addr, data, bank;
{
	if (rdy8x() == ERROR)
		return ERROR;

	kiwi1->ra.b[3] = addr;
	kiwi1->ra.b[4] = data;
	if (bank == RIGHT)
		kiwi1->ra.b[2] = $WRRB;
	else
		kiwi1->ra.b[2] = $WRLB;

	if (rdy8x() == ERROR)
		return ERROR;
	return PASS;
}
/*------------------------------------------------------------------*/
/* read the 8x305's IV bus using the BUG monitor.
*/
rdIV(addr, bank, pdat)
unsigned char addr, bank;
unsigned char *pdat;
{
	if (rdy8x() == ERROR)
		return ERROR;

	kiwi1->ra.b[3] = addr;
	if (bank == RIGHT)
		kiwi1->ra.b[2] = $RDRB;
	else
		kiwi1->ra.b[2] = $RDRB;
	*pdat = kiwi1->ra.b[9];		/* read data from reg array */
	
	if (rdy8x() == ERROR)
		return ERROR;
	return PASS;
}
/*------------------------------------------------------------------*/
/* poll the ready bit of the BUG monitor, a timeout is included */
rdy8xdel()
{
	unsigned i;

	i = 0;
	while (i < 0x1000) {	/* wait for ready */
		i++;
	}
	return PASS;
}
/*------------------------------------------------------------------*/
/* poll the ready bit of the BUG monitor, a timeout is included */
rdy8x()
{
	unsigned i;

	i = 1;
	while ((kiwi1->ra.b[8] == 0x80) && (i > 0)) {	/* wait for ready */
		i++;
	}
	if (i = 0)
		return ERROR;
	return PASS;
}
/*------------------------------------------------------------------*/
/* write to the valid address RAM - for use with XBUG */
wrVadd(p)
unsigned char * p;
{
	unsigned cnt;
	unsigned char addr;

	addr = cnt = 0;
	while (cnt < 256) {

		if (rdy8x() == ERROR)
			return ERROR;
	
		kiwi1->obus = addr;
		kiwi1->ra.b[3] = *p;
		kiwi1->ra.b[2] = $WRVAL;

		if (rdy8x() == ERROR)
			return ERROR;
		addr++;
		cnt++;
		p++;
	}
	return PASS;
}
/*------------------------------------------------------------------*/
/* read the valid address RAM - for use with XBUG */
rdVadd(p)
unsigned char * p;
{
	unsigned char addr;
	unsigned cnt;

	addr = 0;
	cnt = 0;
	while (cnt < 256) {
		if (rdy8x() == ERROR)
			return ERROR;
	
		kiwi1->obus = addr++;
		kiwi1->ra.b[2] = $RDVAL;

		if (rdy8x() == ERROR)
			return ERROR;

		*p = kiwi1->ra.b[3];
		p++;
		cnt++;
	}
	return PASS;
}
/*------------------------------------------------------------------*/
/* initialize the valid address RAM - table is a list of 256
     subchannel address Valid/Invalid values */
initVaddr(table)
unsigned char * table;
{
	unsigned count;

	count = 0;
	while (count <= 256) {
		kiwi1->obus = count;  	/* write address to off bus */
		if (*table == VALID)
			K$COM |= I$WRVL | I$GO;
		else 
			K$COM |= I$WRIN | I$GO;
		count++;
		table++;
		if (waitr(100) == ERROR)
			return ERROR;
	}
	return PASS;
}

/*------------------------------------------------------------------*/
/* wait for the REDY bit to be set, time out included */
waitr(time)
unsigned time;
{
	unsigned i;

	i = time;
	while (!I$REDY) {
		i--;
		i++;
		i--;
		if (i==0) {
			eMsg(E2);	/* time out */
			return ERROR;
		}
	}
}

/*------------------------------------------------------------------*/
/* write to the UDC using the XBUG monitor.
*/
wrUDC(addr, data)
short addr, data;
{
	if (rdy8x() == ERROR)
		return ERROR;

	kiwi1->ra.b[8] = 0x80;
	kiwi1->ra.b[3] = (unsigned char)addr;
	kiwi1->ra.w[2] = data;
	kiwi1->ra.b[2] = $WRUDC;

	if (rdy8x() == ERROR)
		return ERROR;
	return PASS;
}
/*------------------------------------------------------------------*/
/* read the UDC using the XBUG monitor.
*/
rdUDC(addr, pdat)
short addr;
short *pdat;
{
	if (rdy8x() == ERROR)
		return ERROR;

	kiwi1->ra.b[8] = 0x80;
	kiwi1->ra.b[3] = (unsigned char)addr;
	kiwi1->ra.b[2] = $RDUDC;

	if (rdy8x() == ERROR)
		return ERROR;

	*pdat = kiwi1->ra.w[5];		/* read data from reg array */
	return PASS;
}
/*------------------------------------------------------------------*/

/*  */                           