/****************************************************************************/
/*****************************************************************************
 *                      
 *      Project:        16x training
 *      Name:           demomod 
 *      Filename:       demomod.c
 *      System:         IBM PC/AT
 *      Compiler:       c166 V3.11
 *      Date:           02/98
 *      Author:         Siegbert Schaufelberger
 *      Rights:         hitex-systementwicklung GmbH        
 *                      Greschbachstr. 12
 *                      76229  Karlsruhe
 *                                                         
 *****************************************************************************
 *
 *      Implementation description:
 *
 *
 *****************************************************************************
 *
 *      Update:
 *        Date /Sgn.       Version  Changes made because of ...
 *      __-___-____/
 *
 *
 ****************************************************************************/
/****************************************************************************/


/****************************************************************************/
/***					System includes			                          ***/
/****************************************************************************/
#pragma MOD167 FIX167
#include <reg167.h>
#include <intrins.h>

/****************************************************************************/
/***					Defines & Constants                               ***/
/****************************************************************************/

#define STRLEN  50
#define DIMX    10
#define L_MAX    4
#define M_MAX   14
#define ERR_LED P3.6



/****************************************************************************/
/***					Externals					                      ***/
/****************************************************************************/

extern unsigned int millisec;
extern long     SystemTick;          	/* Counts occurence of interrupt */
										/* at system clock */
extern long     CaptureCompare [];		/* Counter for CAPCOM register */

extern struct time 	{					/* reference in module isr */
		unsigned char millisec;
		unsigned char second;
		unsigned char minute;
		unsigned char hour;
					} clock;

extern void  init_t0 (void);
extern void  init_t1 (void);
extern void  init_t6 (void);
extern void  init_port(void);
extern void  led_on(unsigned int);
extern unsigned int  random(void);
 
/****************************************************************************/
/***                    MODULCONSTANTS IN CODE SEGMENT         		      ***/
/****************************************************************************/

char    text1[STRLEN] = "HiTEX DEVELOPMENT TOOLS for the C166/167-family\0";
char    text2[STRLEN] = "HiSCHOOL - Training for T32/AX166\0";
char    text3[STRLEN] = "Achtung! Drehzahl zu hoch\0";
char    text4[STRLEN] = "Drehzahl kleiner  7000\0";


/****************************************************************************/
/***        			 MODULVARIABLES                                   ***/
/****************************************************************************/

unsigned int    i,k,l,m,r,counter,led_cnt,i_array[DIMX];
unsigned int idata val_int;
char    *ptr;   
float   val_float;
long    val_long;
bit     marker;

/* Example of a complex Datastructure   */

typedef enum
{
  RED=0,YELLOW=1,GREEN=2
}color;

color state_c;

typedef struct c_s
{
  char c_array[STRLEN];
  char *text_ptr;
  unsigned int  text_ind;
  struct simple
  {
    unsigned char  state;
    unsigned int  number;
  } page;		  
} complex;

complex message;

/* example of a union */

union uni_1{
   int val_int_u;
   char *ptr_u;
} union_expl;

/* example of a bif-field structur */

struct bf {
   unsigned led:1;
   unsigned b_array:8;
} bit_field;

/****************************************************************************/
/***					Functions					                      ***/
/****************************************************************************/

/*----------------------------------------------------------------------------
 *      
 *  Name:           init_clock
 *      
 *  Description:    initializing of the clock values
 *                  
 *      
 *  Parameters:     
 *                  
 *  Return Value:   void
 *                  
 *--------------------------------------------------------------------------*/

void init_clock () 
{
	clock.millisec = 0;
	clock.second = 0;
	clock.minute = 0;
	clock.hour = 0;
	_nop_ ();

} /* init_clock*/


/*----------------------------------------------------------------------------
 *      
 *  Name:  			memcopy
 *      
 *  Description:	
 *      
 *  Parameters:		char * dest, char * source
 *                  
 *  Return Value:   
 *                  
 *--------------------------------------------------------------------------*/

void memcopy (char *dest, char *source){
unsigned char state;

	state = 0;
	while (*source)
	  *(dest++) = *(source++);
	state = 1;

} /* memcopy*/


/*----------------------------------------------------------------------------
 *      
 *  Name:  			func3
 *      
 *  Description:	
 *                  
 *  Parameters:		unsigned int count1    
 *                  
 *  Return Value:   
 *                  
 *--------------------------------------------------------------------------*/

void func3 (unsigned int count1){
unsigned int loc_count3;

	loc_count3 = 0;
	while (count1){
	  count1--;
	  loc_count3++;}
	state_c = 3;

} /*func3*/

/*----------------------------------------------------------------------------
 *      
 *  Name:  			func2
 *      
 *  Description:	
 *                  
 *  Parameters:		unsigned int count1    
 *                  
 *  Return Value:   
 *                  
 *--------------------------------------------------------------------------*/

void func2 (unsigned int count1){
unsigned int loc_count2;

	loc_count2 = 0;
	while (count1){
	  func3(count1);
	  count1--;
	  loc_count2++;}
	state_c = 2;
	 _nop_ (); 

} /*func2*/

/*----------------------------------------------------------------------------
 *      
 *  Name:  			func1
 *      
 *  Description:	
 *      
 *  Parameters:		unsigned int count1    
 *                  
 *  Return Value:   
 *                  
 *--------------------------------------------------------------------------*/

void func1 (unsigned int count1){
unsigned int loc_count1;

	loc_count1 = 0;
	while (count1){
	  func2(count1);
	  count1--;
	  loc_count1++;}
	state_c = 1;
    	_nop_ (); 
} /*func1*/


/*----------------------------------------------------------------------------
 *      
 *  Name:  			wait_m
 *      
 *  Description:	this function waits to return for a given period
 *                  
 *  Parameters:		unsigned int time    
 *                  
 *  Return Value:   
 *                  
 *--------------------------------------------------------------------------*/

void    wait_m (unsigned int w_time){

while (w_time)
  		_nop_ (); 
 		w_time--;
} /* wait_m */


/*----------------------------------------------------------------------------
 *      
 *  Name:  			wait
 *      
 *  Description:	this function waits for a given period from func random      
 *  Parameters:		unsigned int delay    
 *                  
 *  Return Value:   
 *                  
 *--------------------------------------------------------------------------*/

void    wait (unsigned int delay){
unsigned int state;



while (delay)
  	delay--;
	func1 (1);  
    	_nop_ (); 	    	/*Dummy */
} /* wait */


/*----------------------------------------------------------------------------
 *      
 *  Name:  			increment
 *      
 *  Description:	this function increments a variable passed to by name
 *                  
 *  Parameters:		unsigned int *value    
 *                  
 *  Return Value:   
 *                  
 *--------------------------------------------------------------------------*/

void increment (unsigned int *value ){
unsigned int loc_val;
char c_array[STRLEN];
    loc_val = (*value)++;
    if (loc_val >= 7000)
	memcopy (c_array, text3);
      else
	memcopy (c_array, text4);
	marker = 1;

}/* increment */

/*----------------------------------------------------------------------------
 *      
 *  Name:  			error
 *      
 *  Description:	error loop, toogle variable state
 *                  
 *  Parameters:	     
 *                  
 *  Return Value:   
 *                  
 *--------------------------------------------------------------------------*/

void error (void){
unsigned char value, state;

value = 'E';
while (1)
  state != state;

} /* error */


/*----------------------------------------------------------------------------
 *      
 *  Name:  			main
 *      
 *  Description:	mainfunction
 *                        
 *  Parameters:	     
 *                  
 *  Return Value:   
 *                  
 *--------------------------------------------------------------------------*/

void main (void){

  val_int = 0;
  led_cnt = 7;
  counter = 0;
  SystemTick = 0;
  IEN = 1;

  for (l=0;l<16;l++)
    CaptureCompare [l] = 0;

/*  _nop_ (); */
  init_t0 ();
  init_t1 ();
  init_t6 ();
  init_port();
  init_clock();

  while (1)
  {
    for(l=0; l<=L_MAX; ++l)
    {
       for(m=10; m<=M_MAX; ++m)
       {
	 for (i=0;i<DIMX;i++)
	    i_array[i] = counter;
	  message.text_ptr = text1;
	  led_on (led_cnt);
	  ptr = (char*)&message;

	   if (!ptr)
	  { /* error */
	    counter = 0;
	    val_int = 0;
	    millisec = 0;
	    error();
	  } /* if !ptr */

	 ptr = text1;      
	  memcopy (&message.c_array[0], message.text_ptr);
	  message.page.state = 1;

	  r = random();
	  wait (r*100);

	  func1 (l);
	  wait (m);
	  increment (&counter);
	  val_int++;
	  message.page.number =  counter;

 	  message.text_ptr = text2;
	  ptr = text2;
	  memcopy (&message.c_array[0], message.text_ptr);
	  message.page.state = 2;
	  val_float = (float) counter * 2.54;
	  val_long  = (long) counter;
	  marker = 0;
	  _nop_ ();
      } /* for m... */
    } /*for l... */
  } /* while...*/
} /* main */
