/*	Title	SE6440 tests*/
/******************************************************************************/
/*				    DIAG3				      */
/*			      Allpro SE6440 diagnostic v3.0		      */
/*									      */
/* Project .................... Allpro / SE6440 - Universal Programmers       */
/* Module ..................... DIAG3.C 				      */
/* Title ...................... SE6440 tests				      */
/* Developing Engineer ........ Steven T. Harr				      */
/* Company .................... Logical Devices 			      */
/* Revision Level ............. v1.00					      */
/* Date of Last Revision ...... 10-16-89				      */
/* Last Revision by ........... STH					      */
/* Developing System .......... IBM PC or compatible			      */
/* Target System .............. IBM PC or compatible			      */
/* Language ................... C					      */
/* Compiler ................... Manx Aztec C v3.20e			      */
/* Compiler Command(s) ........ cc se6440				      */
/*									      */
/* Interpreter ................ none					      */
/*									      */
/* Synopsis ................... This module contains the SE6440 tests	      */
/*									      */
/* Revision History:							      */
/*  v1.00   10-16-89		Original version - STH			      */
/*									      */
/******************************************************************************/
/*	Subttl	Includes    */
#include <stdio.h>
#include <ctype.h>
#include <time.h>
#include "cmdfile.h"
#include "pindrivr.h"
#include "keybrd.h"
#include "color.h"
#include "diag2.h"
#include "windows.h"
#include "window.h"


#define MAX_ROWS    8
#define MAX_COLS    8
#define MAX_PINS    40
#define MAX_SOCKETS 64

#define SOURCE_TEST	    0
#define WALKING_BIT_TEST    1
#define EXISTENCE_TEST	    2
#define PINDRIVER_TEST	    3
#define SEQUENCER_TEST	    4
#define ENABLE_TEST	    5
#define BURN_IN_TEST	    6
#define MAX_TESTS   6


#define MASK_PD_START	    0x0001
#define MASK_PD_SOURCE	    0x0001
#define MASK_PD_PULLDN	    0x0002
#define MASK_PD_PULLUP	    0x0004
#define MASK_PD_GND	    0x0008
#define MASK_PD_TTLHI	    0x0010
#define MASK_PD_TTLLOW	    0x0020
#define MASK_PD_CURRENT     0x0040
#define MASK_PD_CLOCK	    0x0080
#define MASK_PD_DECOUPLE    0x0100
#define MASK_PD_SIZE	    9

#define MASK_SOURCE_START   0x0200
#define MASK_SOURCE	    0x0200
#define MASK_SOURCE_SIZE    1

#define MASK_WALK_BIT_START 0x0400
#define MASK_WALK_BIT_LOW   0x0400
#define MASK_WALK_BIT_HI    0x0800
#define MASK_WALK_BIT	    0x1000
#define MASK_WALK_BIT_SIZE  3

#define MASK_SEQ_START	    0x2000
#define MASK_SEQ_STATUS     0x2000
#define MASK_SEQ_VERIFY     0x4000
#define MASK_SEQ_NONVER     0x8000
#define MASK_SEQ_SIZE	    3




#define RED_LED_OFF_BIT     GREEN_LED_OFF_BIT
#define GREEN_LED_ON_BIT    0x04

unsigned char bit_mask[MAX_COLS] = {1,2,4,8,16,32,64,128};
#define is_mask_set(m,r,c)  (m[r]&bit_mask[c])
#define set_mask(m,r,c,s)   {if(s)m[r]|=bit_mask[c];else m[r]&=~bit_mask[c];}


extern unsigned int phys_pin_offset;
extern unsigned int pin_base_address[];

extern char revision_level [];
extern char cwd_path [];
extern char command_buffer [];
extern struct window_def_struc status_window_table [];
extern struct window_usage_struc status_display_table [];
extern unsigned char log_masks [];
extern unsigned char current_masks [];
extern unsigned char error_masks [];
extern unsigned char detect_masks [];
extern se6440_flag;
extern long get_time ();
extern int selected_zif_row;
extern int selected_zif_col;

long unsigned error_ctr [MAX_ROWS][MAX_COLS][MAX_TESTS+1];

char *test_name[] =
{
    "Source Driver Test",
    "Walking Bit Test",
    "Existence Test",
    "Pin Driver Test",
    "Sequencer Test",
    "Enable Test",
    "Burn in Test"
};

int pd_test_flags[8][8][41];


int loop_flag;
int accum_flag;
int full_test_flag;
int stop_on_error_flag;
int stop;
static int zr, zc;
static long unsigned start_time;
static long unsigned stop_time;
static long unsigned number_of_cycles;
static unsigned current_test;
int hrs;
int mins;
int secs;
unsigned long fail_ctr;


display_test_name(test)
unsigned test;
{
    if(test < MAX_TESTS)
    {
	clear_window_line(WND_ZIFS_STATUS,20);
	write_window(WND_ZIFS_STATUS,20,'C',test_name[test]);
    }
}


get_elapsed_time()
{
    long unsigned elapsed_time;
    long unsigned total_minutes;

    stop_time = get_time();
    elapsed_time = stop_time - start_time;
    total_minutes = (elapsed_time / 1092); /* total minutes */
    hrs = total_minutes / 60; /* total hours */
    mins = total_minutes % 60;
    secs = (elapsed_time % 1092) * 10 / 182;
}


display_run_time_parms()
{
    write_window (WND_TEST_PROMPT, 19, 'R', "%8lu   ",fail_ctr);
    write_window (WND_TEST_PROMPT, 21, 'R', " %8lu   ",number_of_cycles);
    write_window (WND_TEST_PROMPT, 20, 'R', "%3d:%02d:%02d   ",hrs,mins,secs);
}


func_se6440()
{
    if(se6440_flag)
    {
	select_test();
    }
}


select_test ()
{
    unsigned char exit_flag;
    int update_flag;
    unsigned int key;

    exit_flag = FALSE;

    update_flag = TRUE;
    while (exit_flag == FALSE)
    {
	if(update_flag)
	{
	    setup_window (WND_ZIFS_STATUS);
	    setup_window (WND_ZIFS_PROMPT);
	    draw_sockets ();
	    write_window (WND_ZIFS_PROMPT,  1, 'L', "  Function Key for Testing");
	    write_window (WND_ZIFS_PROMPT,  2, 'L', "  SHIFT FKey to Cycle");
	    write_window (WND_ZIFS_PROMPT,  3, 'L', "  ALT FKey to Stop on Error");

	    write_window (WND_ZIFS_PROMPT,  5, 'L', "   F1 - Full Test (Always Cycles).");
	    write_window (WND_ZIFS_PROMPT,  6, 'L', " * F2 - Walking Bit Test.");
	    write_window (WND_ZIFS_PROMPT,  7, 'L', " * F3 - Source Driver Test.");
	    write_window (WND_ZIFS_PROMPT,  8, 'L', " * F4 - Existence Test.");
	    write_window (WND_ZIFS_PROMPT,  9, 'L', " * F5 - Pin Driver Test.");
	    write_window (WND_ZIFS_PROMPT, 10, 'L', " + F6 - Enable Test.");
	    write_window (WND_ZIFS_PROMPT, 11, 'L', " + F7 - Sequencer Test.");

	    write_window (WND_ZIFS_PROMPT, 13, 'L', " * - Will run with F1.");
	    write_window (WND_ZIFS_PROMPT, 14, 'L', " + - Will only run with S-F1.");
	    write_window (WND_ZIFS_PROMPT, 15, 'L', "   - - - - - - - - - - - - - -");
	    write_window (WND_ZIFS_PROMPT, 17, 'L', "   F8 - Zif Socket Test.");
/*	      write_window (WND_ZIFS_PROMPT, 18, 'L', "   S-F8 - Burn In Test.");*/
	    write_window (WND_ZIFS_PROMPT, 19, 'L', "   F9 - Select Zif Socket.");
	    write_window (WND_ZIFS_PROMPT, 20, 'L', "   F10 - Log Zifs.");
	    write_window (WND_ZIFS_PROMPT, 21, 'L', " ALT F10 - Show error counters.");
	    update_flag = FALSE;
	}
	loop_flag = FALSE;
	accum_flag = FALSE;
	full_test_flag = FALSE;
	stop_on_error_flag = FALSE;
	stop = FALSE;
	key = keybrd_getc ();
	switch (key)
	{
	    case KEY_SHIFT_F1:
		full_test_flag = TRUE;
	    case KEY_F1:
		accum_flag = TRUE;
		update_flag = TRUE;
		full_test();
		break;

	    case KEY_SHIFT_F2:
		loop_flag = TRUE;
		accum_flag = TRUE;
	    case KEY_F2:
		walk_bit_test();
		break;
	    case KEY_ALT_F2:
		stop_on_error_flag = TRUE;
		walk_bit_test();
		break;

	    case KEY_SHIFT_F3:
		loop_flag = TRUE;
		accum_flag = TRUE;
	    case KEY_F3:
		source_driver_test();
		break;
	    case KEY_ALT_F3:
		stop_on_error_flag = TRUE;
		source_driver_test();
		break;

	    case KEY_SHIFT_F4:
		loop_flag = TRUE;
		accum_flag = TRUE;
	    case KEY_F4:
		existence_test();
		break;
	    case KEY_ALT_F4:
		stop_on_error_flag = TRUE;
		existence_test();
		break;


	    case KEY_SHIFT_F5:
		loop_flag = TRUE;
		accum_flag = TRUE;
	    case KEY_F5:
		pin_driver_test();
		break;
	    case KEY_ALT_F5:
		stop_on_error_flag = TRUE;
		pin_driver_test();
		break;


	    case KEY_SHIFT_F6:
		loop_flag = TRUE;
		accum_flag = TRUE;
	    case KEY_F6:
		enable_test();
		break;
	    case KEY_ALT_F6:
		stop_on_error_flag = TRUE;
		enable_test();
		break;


	    case KEY_SHIFT_F7:
		loop_flag = TRUE;
		accum_flag = TRUE;
	    case KEY_F7:
		sequencer_test();
		break;
	    case KEY_ALT_F7:
		stop_on_error_flag = TRUE;
		sequencer_test();
		break;


	    case KEY_F8:
	    {
		zif_test();
		break;
	    }

	    case KEY_F9:
	    {
		select_zif_socket();
		update_flag = TRUE;
		break;
	    }
	    case KEY_F10:
	    {
		log_zifs();
		update_flag = TRUE;
		break;
	    }

	    case KEY_SHIFT_F8:
	    {
		loop_flag = TRUE;
		accum_flag = TRUE;
		heat_test();
		break;
	    }

	    case KEY_ESC:
	    {
		exit_flag = TRUE;
		break;
	    }

	    case KEY_ALT_F10:
	    {
		display_status_screen();
		update_flag = TRUE;
		break;
	    }
	}
	while(keyboard_status() != -1){} /* clear our keyboard buffer */
	if(accum_flag)
	    draw_sockets ();
    }

    return ();
}


full_test()
{
    loop_flag = FALSE;
    accum_flag = TRUE;

    reset_test_results();
    number_of_cycles = 0;
    start_time = get_time();
    fail_ctr = 0;
    hide_cursor();
    setup_window (WND_TEST_PROMPT);

    write_window (WND_TEST_PROMPT,  2, 'C', "RUNNING FULL TEST");
    write_window (WND_TEST_PROMPT,  4, 'L', "  Hit Any key to stop testing.");

    write_window (WND_TEST_PROMPT,  6, 'L', "  List of tests running:");
    write_window (WND_TEST_PROMPT,  8, 'L', "  Walking Bit Test.");
    write_window (WND_TEST_PROMPT,  9, 'L', "  Source Driver Test.");
    write_window (WND_TEST_PROMPT, 10, 'L', "  Existence Test.");
    write_window (WND_TEST_PROMPT, 11, 'L', "  Pin Driver Test.");
    if(full_test_flag)
    {
	write_window (WND_TEST_PROMPT,	12, 'L', "  Enable Test.");
	write_window (WND_TEST_PROMPT,	13, 'L', "  Sequencer Test.");
    }

    write_window (WND_TEST_PROMPT, 19, 'L', " Failures =     ");
    write_window (WND_TEST_PROMPT, 20, 'L', " Elapsed time = ");
    write_window (WND_TEST_PROMPT, 21, 'L', " Number of Cycles = ");
    get_elapsed_time();
    display_run_time_parms();
    do
    {
	display_test_name(WALKING_BIT_TEST);
	walk_bit_test();
	if(key_queue_check() != -1) break;
	display_test_name(SOURCE_TEST);
	source_driver_test();
	if(key_queue_check() != -1) break;
	display_test_name(EXISTENCE_TEST);
	existence_test();
	if(key_queue_check() != -1) break;
	display_test_name(PINDRIVER_TEST);
	pin_driver_test();
	if(key_queue_check() != -1) break;
	if(full_test_flag)
	{
	    display_test_name(ENABLE_TEST);
	    enable_test();
	    if(key_queue_check() != -1) break;
	    display_test_name(SEQUENCER_TEST);
	    sequencer_test();
	    if(key_queue_check() != -1) break;
	}
	number_of_cycles++;
	get_elapsed_time();
	display_run_time_parms();
    }
    while(key_queue_check() == -1);
    stop_time = get_time();


    show_cursor();
}

static int pin_voltage;

se6440_compare_pin_voltage(pin)
{
    int pv = read_voltage_at_pin(pin);
    if(pv > pin_voltage + 40 || pv < pin_voltage - 40)
    {
	log_error(pin);
	return 0;
    }
    return 1;
}


write_window_status(wind,str)
int wind;
char *str;
{
    if(wind == WND_ZIFS_PROMPT)
    {
	clear_window_line (wind,20);
	write_window (wind,20,'C',str);
    }
}

zif_test()
{
    int pin, ps1, ps2, loop_flag, ch, stop;

    update_zifs(log_masks,GREEN);
    copy_masks(current_masks,log_masks);
    setup_supplies (1000, 200, 600, 50);
    udi_write_mem(ENABLE_REG_ALL,0xff);
    for(pin = 1;pin <= MAX_PINS; pin++)
    {
	set_pin_cnf(pin,0);
    }
    udi_write_mem(ANALOG_CONTROL, GREEN_LED_OFF_BIT);
    udi_write_dac(THRESH_HOLD_DAC, 500);
    udi_write_dac(PU_CONTROL_DAC,1000);
    write_enable_regs(log_masks);
    stop = FALSE;
    for(zr=0;!stop && zr < MAX_ROWS;zr++)
    {
	for(zc = 0; !stop && zc < MAX_COLS; zc++)
	{
	    if(!is_mask_set(log_masks,zr,zc)) continue;
	    select_zif(zr,zc);
	    udi_write_mem(ANALOG_CONTROL, DAC_REFERENCE_BIT | GREEN_LED_OFF_BIT);
	    delay_us(500);
	    loop_flag = TRUE;
	    while(!stop && loop_flag)
	    {
		for(pin=1;pin<=MAX_PINS/2;pin++)
		{
		    set_pin_cnf(pin,PU_DRIVER_ON);
		    ps1 = read_zif_pin(MAX_PINS+1-pin);
		    set_pin_cnf(pin,GD_DRIVER_ON);
		    ps1 ^= read_zif_pin(MAX_PINS+1-pin);
		    set_pin_cnf(pin,0);
		    set_pin_cnf(MAX_PINS+1-pin,PU_DRIVER_ON);
		    ps2 = read_zif_pin(pin);
		    set_pin_cnf(MAX_PINS+1-pin,GD_DRIVER_ON);
		    ps2 ^= read_zif_pin(pin);
		    set_pin_cnf(MAX_PINS+1-pin,0);
		    if(!ps1 || !ps2)
		    {
			write_zif_led(zr,zc,ON,RED);
			udi_write_mem(ANALOG_CONTROL, DAC_REFERENCE_BIT);
			break;
		    }
		}
		if(ps1 && ps2)
		{
		    write_zif_led(zr,zc,ON,GREEN);
		    udi_write_mem(ANALOG_CONTROL, DAC_REFERENCE_BIT | RED_LED_OFF_BIT);
		}
		if(key_queue_check() != -1)
		{
		    ch = keybrd_getc();
		    if(ch == KEY_ESC) stop = TRUE;
		    if(ch == 0x20) loop_flag = FALSE;
		}
	    }
	}
    }
    udi_write_mem(ENABLE_REG_ALL,0xff);
    udi_write_mem(ANALOG_CONTROL, 0);
    return;
}


heat_test()
{
    int pn, pv, stop;

    pin_voltage = 70;

    current_test = BURN_IN_TEST;
    display_test_name(current_test);

	setup_supplies (2100, 200, 600, 50);
	write_enable_regs(log_masks);
	udi_write_mem(ANALOG_CONTROL, RED_LED_OFF_BIT);
	udi_write_dac (SLEW_RATE_DAC,50);
	update_zifs(log_masks,GREEN);
	copy_masks(current_masks,log_masks);
	clear_masks(error_masks);
	for(pn=1;pn<=MAX_PINS;pn++)
	{
	    set_pin_dac(pn, pin_voltage);
	    set_pin_cnf(pn, SRC_DRIVER_ON | TTL_LOW_DRIVER_ON);
	}
	stop = FALSE;
    do
    {
	for(zr = 0;!stop && zr<MAX_ROWS;zr++)
	{
	    for(zc = 0;!stop && zc<MAX_COLS;zc++)
	    {
		if(is_mask_set(current_masks,zr,zc))
		{
		    select_zif(zr,zc);
		    udi_write_mem(ANALOG_CONTROL, DAC_REFERENCE_BIT);
		    delay_us(500);

		    for(pn=1;pn<=MAX_PINS;pn++)
		    {
			pv = read_voltage_at_pin(pn);
			if(pv < pin_voltage - 30 || pv > pin_voltage + 30)
			{
			    log_error(pn);
			    break;
			}
		    }
		    if(pv >= pin_voltage - 30 && pv <= pin_voltage + 30)
		    {
			udi_write_mem(ANALOG_CONTROL, DAC_REFERENCE_BIT | RED_LED_OFF_BIT);
		    }
		}
		if(key_queue_check() != -1)
		{
		    stop = TRUE;
		}
	    }
	}
    }
    while(loop_flag && !stop);
    udi_write_mem(ENABLE_REG_ALL,0xff);
    udi_write_mem(ANALOG_CONTROL, 0);
    reset_allpro ();
}


sequencer_test()
{
    int pn, pv, error, j, i, k, err_num;

/* sequencer definitions */
#define WRITE_MODE		1
#define READ_MODE		0


#define STATE_SET_0		0
#define STATE_SET_1		2
#define STATE_SET_2		4


/* sequencer read registers */
#define SEQUENCER_STATUS	0x2e4
#define SEQUENCER_VERIFY	0x2e5

    current_test = SEQUENCER_TEST;
    display_test_name(current_test);

    udi_write_mem(SEQUENCER_COUNTER,0);
    for(i=1;i<=MAX_PINS;i++)
    {
	udi_write_mem(SEQUENCER_RAM,pin_base_address[i+phys_pin_offset]>>4);
    }

    do
    {
	setup_supplies (500, 200, 600, 50);
	write_enable_regs(log_masks);
	udi_write_mem(ANALOG_CONTROL, RED_LED_OFF_BIT);
	update_zifs(log_masks,GREEN);
	copy_masks(current_masks,log_masks);
	clear_masks(error_masks);
	for(pn=1;pn<=MAX_PINS;pn++)
	{
	    set_pin_cnf(pn,0);
	    set_pin_dac(pn,50);
	}

	stop = FALSE;
	for(zr = 0;!stop && zr<MAX_ROWS;zr++)
	{
	    for(zc = 0;!stop && zc<MAX_COLS;zc++)
	    {
		if(is_mask_set(current_masks,zr,zc))
		{
		    select_zif(zr,zc);
		    udi_write_mem(ANALOG_CONTROL, DAC_REFERENCE_BIT | RED_LED_OFF_BIT);
		    delay_us(500);

		    error = FALSE;
		    for(i=0;i<MAX_PINS;i+=8)
		    {
			for(j=0x80;j>0;j >>= 1)
			{
			    /* test the status flip flop */
			    udi_write_mem(SEQUENCER_COUNTER,READ_MODE);
			    udi_write_mem(SEQUENCER_SHIFTER,j);
			    delay_us(20);

			    if ((udi_read_mem(SEQUENCER_STATUS) & 0x01) == 1)
			    {
				pd_test_flags[zr][zc][i+1] |= MASK_SEQ_STATUS;
				err_num = 1;
				error = TRUE;
				break;
			    }
			    udi_write_sequencer(i | STATE_SET_0 | WRITE_MODE,j);
			    error = !udi_write_sequencer(i|READ_MODE,j);
			    err_num = 2;
			    if(error)
			    {
				pd_test_flags[zr][zc][i+1] |= MASK_SEQ_VERIFY;
				break;
			    }
			    err_num = 3;
			    for(k=1;k<=0x80;k*=2)
			    {
				error = !udi_write_sequencer(i|READ_MODE,k);
				if(k != j)
				{
				    if(error) error = FALSE;
				    else error = TRUE;
				}
				if(error)
				{
				    pd_test_flags[zr][zc][i+1] |= MASK_SEQ_VERIFY;
				    break;
				}
			    }
			    if(error) break;

    /*
			    err_num = 4;
			    udi_write_dac(THRESH_HOLD_DAC,90);
			    udi_write_sequencer(i|STATE_SET_1|WRITE_MODE,j);
			    error = !udi_write_sequencer(i|READ_MODE,j);
			    if(error) break;

			    err_num = 5;
			    udi_write_dac(THRESH_HOLD_DAC,30);
			    error = !udi_write_sequencer(i|READ_MODE,0xff);

			    udi_write_sequencer(i|STATE_SET_0|WRITE_MODE,0x00);
			    udi_write_dac(THRESH_HOLD_DAC,200);
			    if(error) break;
    */
			}
			if(error) log_error(0,err_num);
			if(error) break;
		    }
		    if(error == FALSE)
		    {
			udi_write_mem(ANALOG_CONTROL, RED_LED_OFF_BIT | GREEN_LED_ON_BIT);
		    }
		}
		if(key_queue_check() != -1)
		{
		    stop = TRUE;
		}
	    }
	}
    }
    while(loop_flag && !stop);

}

enable_test()
{
    int zif_row, zif_col, tst_row, tst_col, pin, tst_pin;
    write_window_status (WND_ZIFS_STATUS,"Enable Test");


    current_test = ENABLE_TEST;
    display_test_name(current_test);
    do
    {
	update_zifs(log_masks,GREEN);
	copy_masks(current_masks,log_masks);
	clear_masks(error_masks);

	setup_supplies (500, 200, 600, 50);
	udi_write_mem(ENABLE_REG_ALL,0xff);
	for(pin = 1;pin <= MAX_PINS; pin++)
	{
	    set_pin_cnf(pin,GD_DRIVER_ON);
	}
	udi_write_mem(ANALOG_CONTROL, GREEN_LED_OFF_BIT);
	udi_write_dac(THRESH_HOLD_DAC, 200);
	delay_us(500);
	for(zif_row = 0;!stop && zif_row < MAX_ROWS;zif_row++)
	{
	    for(zif_col = 0;!stop && zif_col < MAX_COLS;zif_col++)
	    {
		if(is_mask_set(current_masks,zif_row,zif_col))
		{
		    write_enable_regs(current_masks);
		    udi_write_mem(ANALOG_CONTROL, GREEN_LED_OFF_BIT);
		    for(pin = 1;!stop && pin <= MAX_PINS; pin++)
		    {
			set_pin_cnf(pin,GD_DRIVER_ON);
		    }
		    select_zif(zif_row,zif_col);
		    udi_write_mem(ANALOG_CONTROL, DAC_REFERENCE_BIT);
		    delay_us(500);
		    for(pin = 1;!stop && pin <= MAX_PINS; pin++)
		    {
			set_pin_cnf(pin, TTL_HI_DRIVER_ON);
			if(!read_zif_pin(pin))
			{
			    log_error(pin);
			    goto main_loop;
			}
		    }
		    for(tst_row = 0;!stop && tst_row < MAX_ROWS;tst_row++)
		    {
			for(tst_col = 0;!stop && tst_col < MAX_COLS;tst_col++)
			{
			    if(is_mask_set(current_masks,tst_row,tst_col))
			    {
				select_zif(tst_row,tst_col);
				udi_write_mem(ANALOG_CONTROL, DAC_REFERENCE_BIT | GREEN_LED_OFF_BIT);
				delay_us(500);
				if(tst_row != zif_row || tst_col != zif_col)
				{
				    for(pin = 1;pin <= MAX_PINS; pin++)
				    {
					if(read_zif_pin(pin))
					{
					    log_error(pin);
					    break;
					}
				    }
				}
			    }
			}
		    }
		}
    main_loop:;
	    }
	}
    }
    while(loop_flag && key_queue_check() == -1);

}


pin_driver_test()
{
    int pin, pv;
    int status;
    int pd_flags;
    int ac_flag = accum_flag;

    current_test = PINDRIVER_TEST;
    display_test_name(current_test);

    do
    {
	setup_supplies (2500, 200, 600, 50);
    /*	  write_enable_regs(log_masks); */
	udi_write_mem(ENABLE_REG_ALL,0xff);
	udi_write_mem(ANALOG_CONTROL, GREEN_LED_OFF_BIT);
	for(pin=1;pin<=MAX_PINS;pin++)
	{
	    set_pin_cnf(pin,0);
	}
	update_zifs(log_masks,GREEN);
	copy_masks(current_masks,log_masks);
	clear_masks(error_masks);



	for(zr = 0;!stop && zr<MAX_ROWS;zr++)
	{
	    for(zc = 0;!stop && zc<MAX_COLS;zc++)
	    {
		if(is_mask_set(current_masks,zr,zc))
		{
		    select_zif(zr,zc);
		    udi_write_mem(ANALOG_CONTROL, DAC_REFERENCE_BIT | GREEN_LED_OFF_BIT);
		    udi_write_dac (SLEW_RATE_DAC,30);
		    udi_write_dac (PU_CONTROL_DAC, 600 );
		    delay_us (500);
		    status = TRUE;
		    for(pin = 1;status && pin <= MAX_PINS;pin++)
		    {
			pd_flags = 0;
			set_pin_cap(pin, OFF);
    /* source driver */
			set_pin_dac (pin, 2200);
			delay_us(50);
			set_pin_cnf (pin,SRC_DRIVER_ON);
			pv = read_voltage_at_pin(pin);
			if(pv < 2140 || pv > 2270)
			{
			    log_error(pin);
			    pd_flags |= 0x01;
			    break;
			}
    /* pull down driver */
			set_pin_cnf (pin,PD_DRIVER_ON);
			pv = read_voltage_at_pin(pin);
			if(pv > 50) {log_error(pin); pd_flags |= 0x02; break;}
    /* pull up driver */
			set_pin_cnf (pin,PU_DRIVER_ON | PD_DRIVER_ON);
			pv = read_voltage_at_pin(pin);
			if(pv < 500 || pv >  650) {log_error(pin); pd_flags |= 0x04; break;}
    /* ground driver */
			set_pin_cnf (pin,GD_DRIVER_ON | PU_DRIVER_ON);
			pv = read_voltage_at_pin(pin);
			if(pv > 50) {log_error(pin); pd_flags |= 0x08; break;}
    /* ttl high driver */
			set_pin_cnf (pin,TTL_HI_DRIVER_ON | TTL_LOW_DRIVER_ON);
			pv = read_voltage_at_pin(pin);
			if(pv > 500 || pv < 270) {log_error(pin); pd_flags |= 0x10; break;}
    /* ttl low driver */
			set_pin_cnf (pin,TTL_LOW_DRIVER_ON | PU_DRIVER_ON);
			pv = read_voltage_at_pin(pin);
			if(pv > 70) {log_error(pin); pd_flags |= 0x20; break;}
    /* current driver */
			udi_write_mem(VIPI_DAC,0);
			udi_write_mem(VIPV_DAC,100);
			set_pin_cnf(pin, TTL_LOW_DRIVER_ON | CUR_DRIVER_ON);
			udi_write_mem(VIPI_DAC,40 / 2 + 10);
			pv = read_voltage_at_pin(pin);
			if(pv > 300 || pv < 150) {log_error(pin); pd_flags |= 0x40; break;}
			udi_write_mem(VIPV_DAC,0);
			udi_write_mem(VIPI_DAC,0);
    /* clock driver */
			udi_write_mem(PCLOCK,0x01);
			set_pin_cnf(pin, CLK0_DRIVER_ON);
			pv = read_voltage_at_pin(pin);
			if(pv > 70) {log_error(pin); pd_flags |= 0x80; break;}
			udi_write_mem(PCLOCK,0x81);
			pv = read_voltage_at_pin(pin);
			if(pv < 250 || pv > 500) {log_error(pin); pd_flags |= 0x80; break;}
			set_pin_cnf(pin, CLK1_DRIVER_ON);
			pv = read_voltage_at_pin(pin);
			if(pv > 70) {log_error(pin); pd_flags |= 0x80; break;}
			udi_write_mem(PCLOCK,0x01);
			pv = read_voltage_at_pin(pin);
			if(pv < 250 || pv > 500) {log_error(pin); pd_flags |= 0x80; break;}
			set_pin_cnf (pin, TTL_LOW_DRIVER_ON);
    /* decouple driver */
			delay_us(400);
			udi_write_dac(THRESH_HOLD_DAC,300);
			set_pin_cap(pin, ON);
			set_pin_dac (pin, 0);
			set_pin_cnf(pin, SRC_DRIVER_ON);
			set_pin_dac (pin, 1000);
			delay_us(100);
#asm
    cli
#endasm
			set_pin_cnf(pin, PD_DRIVER_ON);
			delay_us(100);
			pv = read_zif_pin(pin);
#asm
    sti
#endasm
			set_pin_cnf(pin, GD_DRIVER_ON);
			if(!pv) {log_error(pin); pd_flags |= 0x100; break;}
			set_pin_dac (pin, 0);
			delay_us(400);
    /* disconnect driver */
/*
			set_pin_cnf (pin, GD_DRIVER_ON);
			set_pin_cap(pin, OFF);
			set_pin_cnf(pin, PU_DRIVER_ON);

#asm
    cli
#endasm
			set_pin_cnf(pin, 0);
			pv = read_zif_pin(pin);
#asm
    sti
#endasm
			delay_us(400);
			set_pin_cnf(pin, GD_DRIVER_ON);
			if(!pv) {log_error(pin); break;}
			delay_ms(1);

*/
    /* end of test */
			set_pin_dac (pin, 0);
			set_pin_cnf (pin, GD_DRIVER_ON);
			if(!is_mask_set(current_masks,zr,zc)) status = FALSE;
		    }
		    if(accum_flag && pin <= 40)
		    {
			pd_test_flags[zr][zc][pin] |= pd_flags;
		    }
		}
		if(key_queue_check() != -1) stop = TRUE;
	    }
	}
    }
    while(loop_flag && key_queue_check() == -1);

}


source_driver_test()
{
    int pn, pv;


    current_test = SOURCE_TEST;
    display_test_name(current_test);

    pin_voltage = 1920;

    do
    {
	setup_supplies (2500, 200, 600, 50);
	write_enable_regs(log_masks);
	udi_write_mem(ANALOG_CONTROL, RED_LED_OFF_BIT);
	udi_write_dac (SLEW_RATE_DAC,100);
	udi_write_dac (PU_CONTROL_DAC, 1000 );
	update_zifs(log_masks,GREEN);
	copy_masks(current_masks,log_masks);
	clear_masks(error_masks);
	for(pn=1;pn<=MAX_PINS;pn++)
	{
	    set_pin_cnf(pn,0);
	}

	stop = FALSE;
	for(zr = 0;!stop && zr<MAX_ROWS;zr++)
	{
	    for(zc = 0;!stop && zc<MAX_COLS;zc++)
	    {
		if(is_mask_set(current_masks,zr,zc))
		{
		    select_zif(zr,zc);
		    udi_write_mem(ANALOG_CONTROL, DAC_REFERENCE_BIT | RED_LED_OFF_BIT);
		    delay_us(500);

		    for(pn=1;pn<=MAX_PINS;pn++)
		    {
			set_pin_dac(pn,pin_voltage);
			set_pin_cnf(pn,SRC_DRIVER_ON | PD_DRIVER_ON);
			delay_us(50);
			pv = read_voltage_at_pin(pn);
			set_pin_dac(pn,0);
			set_pin_cnf(pn,0);
			if(pv < pin_voltage - 40 || pv > pin_voltage + 40)
			{
			    pd_test_flags[zr][zc][pn] |= MASK_SOURCE;
			    log_error(pn);
			    break;
			}
		    }
		    if(pv < pin_voltage - 40 || pv > pin_voltage + 40)
		    {
			udi_write_mem(ANALOG_CONTROL, RED_LED_OFF_BIT | GREEN_LED_ON_BIT);
		    }
		}
		if(key_queue_check() != -1)
		{
		    stop = TRUE;
		}
	    }
	}
    }
    while(loop_flag && !stop);
}

existence_test()
{
    int i;

    current_test = EXISTENCE_TEST;
    display_test_name(current_test);

    write_enable_regs(log_masks);
    do
    {
	update_zifs(log_masks,GREEN);
	copy_masks(current_masks,log_masks);
	clear_masks(error_masks);

	get_existing_zifs(error_masks);
	xor_masks(error_masks,log_masks);
	write_enable_regs(error_masks);
	udi_write_mem(ANALOG_CONTROL,0);
	update_zifs(error_masks,RED);

	for(zr=0;!stop && zr<MAX_ROWS;zr++)
	{
	    for(zc = 0;!stop &&  zc < MAX_COLS; zc++)
	    {
		if(error_masks[zr] & (1 << zc))
		{
		    if (accum_flag) draw_zif(zr,zc,0x1c);
		    if(current_test < MAX_TESTS) (error_ctr[zr][zc][current_test])++;
		}
	    }
	}

    /*	  log_errors();*/
    }
    while(loop_flag && key_queue_check() == -1);
}

get_existing_zifs(msk)
unsigned char msk[];
{
    int i;
    udi_write_mem(ENABLE_REG_ALL,0xff); /* enable all sockets */
    udi_write_mem(ANALOG_CONTROL,RED_LED_OFF_BIT|GREEN_LED_ON_BIT);
    for(i=0;i<MAX_ROWS;i++)
    {
	msk[i] = (char) udi_read_mem(0x340 + i);
    }
    udi_write_mem(ANALOG_CONTROL,0x80|RED_LED_OFF_BIT|GREEN_LED_ON_BIT);
    for(i=0;i<MAX_ROWS;i++)
    {
	msk[i] ^= (char) udi_read_mem(0x340 + i);
    }
    udi_write_mem(ENABLE_REG_ALL,0x00); /* enable all sockets */
    for(i=0;i<MAX_ROWS;i++)
    {
	msk[i] &= (char) ~udi_read_mem(0x340 + i);
    }
    write_enable_regs(log_masks);
    and_masks(msk,log_masks);
    udi_write_mem(ANALOG_CONTROL,RED_LED_OFF_BIT|GREEN_LED_ON_BIT);
}


walk_bit_test()
{
    int zif_row, zif_col, tst_row, tst_col, pin, tst_pin;
    write_window_status (WND_ZIFS_STATUS,"Walking Bit Test");

    current_test = WALKING_BIT_TEST;
    display_test_name(current_test);

    do
    {
	update_zifs(log_masks,GREEN);
	copy_masks(current_masks,log_masks);
	clear_masks(error_masks);

	setup_supplies (500, 200, 600, 50);
	udi_write_mem(ENABLE_REG_ALL,0xff);
	for(pin = 1;pin <= MAX_PINS; pin++)
	{
	    set_pin_cnf(pin,0);
	}
	udi_write_mem(ANALOG_CONTROL, GREEN_LED_OFF_BIT);
	udi_write_dac(THRESH_HOLD_DAC, 200);

	for(zif_row = 0;!stop && zif_row < MAX_ROWS;zif_row++)
	{
	    for(zif_col = 0;!stop && zif_col < MAX_COLS;zif_col++)
	    {
		if(is_mask_set(current_masks,zif_row,zif_col))
		{
		    select_zif(zif_row,zif_col);
		    udi_write_mem(ANALOG_CONTROL, DAC_REFERENCE_BIT | GREEN_LED_OFF_BIT);
		    delay_us(500);
		    for(pin=1;!stop && pin <= MAX_PINS;pin++)
		    {
			select_zif(zif_row,zif_col);
			set_pin_cnf(pin,GD_DRIVER_ON);
			if(!read_zif_pin(pin))
			{
			    set_pin_cnf(pin,TTL_HI_DRIVER_ON);
			    if(read_zif_pin(pin))
			    {
				for(tst_pin=1;!stop && tst_pin <= MAX_PINS;tst_pin++)
				{
				    if(tst_pin != pin)
				    {
					set_pin_cnf(tst_pin,TTL_HI_DRIVER_ON);
					set_pin_cnf(tst_pin,GD_DRIVER_ON);
					if(!read_zif_pin(pin))
					{
					    pd_test_flags[zr][zc][pin] |= MASK_WALK_BIT;
					    log_error(tst_pin);
					    break;
					}
				    }
				}
			    }
			    else
			    {
				pd_test_flags[zr][zc][pin] |= MASK_WALK_BIT_HI;
				log_error(pin);
				break;
			    }
			}
			else
			{
			    pd_test_flags[zr][zc][pin] |= MASK_WALK_BIT_LOW;
			    log_error(pin);
			    break;
			}
			select_zif(zif_row,zif_col);
			set_pin_cnf(pin,GD_DRIVER_ON);
			set_pin_cnf(pin,0);
		    }
		}
	    }
	}
    }
    while(loop_flag && key_queue_check() == -1);
}


read_zif_pin(pin)
{
    return udi_read_mem(pin_base_address[phys_pin_offset+pin]+zr) & bit_mask[zc]?1:0;
}


select_zif(row,col)
{
    if(accum_flag && (zc == zr)) display_moving_star();
    zr = row;
    zc = col;
    udi_write_mem(ENABLE_REG_ALL,0);
    udi_write_mem(ENABLE_REG_0+zr,bit_mask[zc]);
}

log_error(pin,err_type)
{
    int i;

    fail_ctr++;
    clear_masks(error_masks);
    set_mask(error_masks, zr, zc, ON);

    if(current_test < MAX_TESTS) error_ctr[zr][zc][current_test]++;

    update_zifs(error_masks,RED);

    if(stop_on_error_flag)
    {
	write_window_border(WND_ZIFS_STATUS,'L'," Socket %1d%c ",zr+1,zc+'A');
	if(pin)
	{
	    write_window_border(WND_ZIFS_STATUS,'R'," Pin %d ",pin);
	}
	else
	{
	    write_window_border(WND_ZIFS_STATUS,'R'," Error %d ",err_type);
	}
	if(keybrd_getc() == KEY_ESC) stop = TRUE;
	write_window_border(WND_ZIFS_STATUS,'L',"");
	write_window_border(WND_ZIFS_STATUS,'R',"");
    }

    write_enable_regs(error_masks);
    udi_write_mem(ANALOG_CONTROL,0);
    for(i=1;i<=MAX_PINS;i++)
    {
	set_pin_cnf(i,0);
    }
    set_mask(current_masks,zr,zc,OFF);
    set_mask(error_masks, zr, zc, OFF);
    write_enable_regs(error_masks);
    if (accum_flag) draw_zif(zr,zc,0x1c);
}

log_errors()
{
    int i;
    update_zifs(error_masks,RED);
    write_enable_regs(error_masks);
    udi_write_mem(ANALOG_CONTROL,0);
    for(i=1;i<=MAX_PINS;i++)
    {
	set_pin_cnf(i,0);
    }
    for(i=0;i<MAX_ROWS;i++)
    {
	current_masks[i] &= ~error_masks[i];
    }
    write_enable_regs(current_masks);
    clear_masks(error_masks);
/*    if (accum_flag) draw_zif(zr,zc,0x1c);*/
}


copy_masks(to,from)
unsigned char to[], from[];
{
    int i;
    for(i=0;i<MAX_ROWS;i++)
    {
	to[i]=from[i];
    }
}

cpl_masks(masks)
unsigned char masks[];
{
    int i;
    for(i=0;i<MAX_ROWS;i++)
    {
	masks[i]=~masks[i];
    }
}

xor_masks(to,from)
unsigned char to[], from[];
{
    int i;
    for(i=0;i<MAX_ROWS;i++)
    {
	to[i]^=from[i];
    }
}

and_masks(to,from)
unsigned char to[], from[];
{
    int i;
    for(i=0;i<MAX_ROWS;i++)
    {
	to[i] &= from[i];
    }
}

clear_masks(masks)
unsigned char masks[];
{
    int i;
    for(i=0;i<MAX_ROWS;i++) masks[i] = 0;
}


read_voltage_at_pin (pin)
unsigned int pin;
{
    unsigned int approx;
    unsigned int guess;

    approx = 0; 						/* initial approximation			*/

    for (guess = 0x80; guess > 0; guess >>= 1)			/* addend and control				*/
    {
	udi_write_mem (THRESH_HOLD_DAC, approx + guess);	/* set comp drive				*/

	if (read_zif_pin (pin)) 				/* was it too low?				*/
	{
	    approx = approx + guess;				/* yes - restore AND add			*/
	}
    }

    return (approx * 10);					/* return pinvalue				*/
}


draw_disp_stat_screen()
{
    setup_window (WND_TEST_RESULT);
    setup_window (WND_TEST_PROMPT);

    write_window (WND_TEST_PROMPT,  2, 'L', "  FKey = Show Fault Counters");
    write_window (WND_TEST_PROMPT,  3, 'L', "  ALT-FKey = Show Statistics");

    write_window (WND_TEST_PROMPT,  6, 'L', "  F2  > Walking Bit Test");
    write_window (WND_TEST_PROMPT,  7, 'L', "  F3  > Source Driver Test");
    write_window (WND_TEST_PROMPT,  8, 'L', "  F4  - Existence Test");
    write_window (WND_TEST_PROMPT,  9, 'L', "  F5  > Pin Driver Test");
    write_window (WND_TEST_PROMPT, 10, 'L', "  F6  - Enable Test");
    write_window (WND_TEST_PROMPT, 11, 'L', "  F7  > Sequencer Test");

    write_window (WND_ZIFS_PROMPT, 13, 'L', "   >  = Statistics Available");

    write_window (WND_ZIFS_PROMPT, 15, 'L', "  F9  - Select Zif Socket");
    write_window (WND_ZIFS_PROMPT, 16, 'L', "  ESC - Return to main Menu");

    write_window (WND_TEST_PROMPT, 18, 'L', " Elapsed time = %3d:%02d:%02d ",hrs,mins,secs);
    write_window (WND_TEST_PROMPT, 19, 'L', " Number of Cycles = %8lu     ",number_of_cycles);

    display_test_results(MAX_TESTS);


}


display_status_screen()
{
    int i,j,k;
    int row, col;
    int vars;
    int key;

    hide_cursor();
    draw_disp_stat_screen();
    while(1)
    {
	key = keybrd_getc ();
	if(key == KEY_ESC) break;
	switch(key)
	{
	    case KEY_F2:
		display_test_results(WALKING_BIT_TEST);
		break;
	    case KEY_F3:
		display_test_results(SOURCE_TEST);
		break;
	    case KEY_F4:
		display_test_results(EXISTENCE_TEST);
		break;
	    case KEY_F5:
		display_test_results(PINDRIVER_TEST);
		break;
	    case KEY_F6:
		display_test_results(ENABLE_TEST);
		break;
	    case KEY_F7:
		display_test_results(SEQUENCER_TEST);
		break;

	    case KEY_ALT_F2:
		show_pin_driver_info(WALKING_BIT_TEST);
		draw_disp_stat_screen();
		break;
	    case KEY_ALT_F3:
		show_pin_driver_info(SOURCE_TEST);
		draw_disp_stat_screen();
		break;
	    case KEY_ALT_F5:
		show_pin_driver_info(PINDRIVER_TEST);
		draw_disp_stat_screen();
		break;
	    case KEY_ALT_F7:
		show_pin_driver_info(SEQUENCER_TEST);
		draw_disp_stat_screen();
		break;

	    case KEY_F9:
		select_zif_socket();
		draw_disp_stat_screen();
		break;
	    case KEY_F10:
		log_zifs();
		draw_disp_stat_screen();
		break;
	}
    }
    show_cursor();
}

reset_test_results()
{
    int i, j, k;
    for(i=0;i<MAX_ROWS;i++)
    {
	for(j=0;j<MAX_COLS;j++)
	{
	    for(k=0;k<MAX_TESTS;k++)
	    {
		error_ctr[i][j][k] = 0;
	    }
	    for(k=1;k<=40;k++)
	    {
		pd_test_flags[i][j][k] = 0;
	    }
	}
    }
}



display_test_results(test)
unsigned int test;
{
    unsigned int i, j, err_val;

    setup_window (WND_TEST_RESULT);
    clear_window(WND_TEST_RESULT);
    for(i=0;i<8;i++)
    {
	write_col_number(i);
	write_row_number(i);
    }
    display_test_name(test);
    for(i=0;i<MAX_ROWS;i++)
    {
	for(j=0;j<MAX_COLS;j++)
	{
	    if(test >= MAX_TESTS)
	    {
		pos_write_window (WND_TEST_RESULT, 16-i*2, 4*j+6, "---");
	    }
	    else
	    {
		err_val = error_ctr[i][j][test];
		if(err_val > 999) err_val = 999;
		pos_write_window (WND_TEST_RESULT, 16-i*2, 4*j+6, "%03u", err_val);
	    }
	}
    }
}



show_pin_driver_info(test)
int test;
{
    int i, pn;
    unsigned int mask;

    while(1)
    {
	setup_window (WND_TEST_STATISTICS);
	setup_window (WND_TEST_PROMPT);
	clear_window(WND_TEST_STATISTICS);
	clear_window(WND_TEST_PROMPT);

	switch(test)
	{
	    case PINDRIVER_TEST:
		write_window (WND_TEST_PROMPT,	4, 'L', "  A = Source test");
		write_window (WND_TEST_PROMPT,	5, 'L', "  B = Pull-down test");
		write_window (WND_TEST_PROMPT,	6, 'L', "  C = Pull-up test");
		write_window (WND_TEST_PROMPT,	7, 'L', "  D = Ground test");
		write_window (WND_TEST_PROMPT,	8, 'L', "  E = Logic high test");
		write_window (WND_TEST_PROMPT,	9, 'L', "  F = Logic low test");
		write_window (WND_TEST_PROMPT, 10, 'L', "  G = Current test");
		write_window (WND_TEST_PROMPT, 11, 'L', "  H = Clock test");
		write_window (WND_TEST_PROMPT, 12, 'L', "  I = Decouple test");
	    break;
	    case SOURCE_TEST:
		write_window (WND_TEST_PROMPT,	4, 'L', "  A = Out of window test");
	    break;
	    case WALKING_BIT_TEST:
		write_window (WND_TEST_PROMPT,	4, 'L', "  A = Write Low test");
		write_window (WND_TEST_PROMPT,	5, 'L', "  B = Write High test");
		write_window (WND_TEST_PROMPT,	6, 'L', "  C = Overwrite test");
	    break;
	    case SEQUENCER_TEST:
		write_window (WND_TEST_PROMPT,	4, 'L', "  A = Busy test");
		write_window (WND_TEST_PROMPT,	5, 'L', "  B = Verify test");
		write_window (WND_TEST_PROMPT,	6, 'L', "  C = Non Verify test");
	    break;
	}

	write_window (WND_TEST_PROMPT, 14, 'L', "  - = Passed test");
	write_window (WND_TEST_PROMPT, 15, 'L', "  X = Failed test");

	write_window (WND_TEST_PROMPT, 19, 'L', "  F9 = Select Zif");
	write_window (WND_TEST_PROMPT, 20, 'L', "  Any other key returns");


	pos_write_window(WND_TEST_STATISTICS, 1, 11,"ABCDEFGHI           ABCDEFGHI");
	write_window_border(WND_TEST_STATISTICS,'C'," %s, Socket %1d%c ",test_name[test],selected_zif_row+1,'A'+selected_zif_col);
	for(pn=1;pn<41;pn++)
	{
	    if(pn < 21)
		pos_write_window(WND_TEST_STATISTICS, 22-pn, 22,"");
	    else
		pos_write_window(WND_TEST_STATISTICS, pn-19, 2,"");
	    printf("Pin %02d = ",pn);
	    switch(test)
	    {
		case SOURCE_TEST:
		    i = MASK_SOURCE_SIZE;
		    mask = MASK_SOURCE_START;
		break;
		case WALKING_BIT_TEST:
		    i = MASK_WALK_BIT_SIZE;
		    mask = MASK_WALK_BIT_START;
		break;
		case PINDRIVER_TEST:
		    i = MASK_PD_SIZE;
		    mask = MASK_PD_START;
		break;
		case SEQUENCER_TEST:
		    i = MASK_SEQ_SIZE;
		    mask = MASK_SEQ_START;
		break;
	    }
	    for(;i>0;i--)
	    {
		if(pd_test_flags[selected_zif_row][selected_zif_col][pn] & mask)
		{
		    printf("X");
		}
		else
		{
		    printf("-");
		}
		mask <<= 1;
	    }
	}
	if(keybrd_getc() == KEY_F9)
	{
	    select_zif_socket();
	}
	else break;
    }
}



display_moving_star()
{
    char moving_star[18];
    static int position;
    static int direction;

    position += direction;
    strcpy(moving_star,"[        ]");
    if(position == 1) direction = 1;
    else if(position <= 0)
    {
	direction = 1;
	position = 1;
    }
    else if(position == 8)
    {
	direction = -1;
    }

    moving_star[position] = '*';
    write_window(WND_ZIFS_STATUS,21,'C',moving_star);
}


