#include <a:printf.c>

#define sec 2 /* clock functions */
#define min 3
#define hour 4
#define day 6
#define mon 7
#define year 9
#define func 32  /* function definition port */
#define clock 36 /* clock port */
#define init 34  /* pio init port */
#define initch 15 /* pio init char */
#define initval 29
#define clkstatus 12
#define military 0
#define civilian 1
#define am 0
#define pm 1
#define tme 0
#define dte 1



/* variables */
int  bcdtoi(), hours, minutes, seconds, months, days, 
     years, n, i, inp(), outp();

main() /* read the real time clock & display time & date */
{
  
  /* initialize and check the clock */

  outp(init, initch);
  outp(func, clkstatus);
  if (bcdtoi( inp(clock)) != 29) {
    printf("The clock is not intialized !\n");
    exit();
  }
 
  	
  outp(func, hour);
  hours = bcdtoi(inp(clock)); /* then get the value of that function */

  outp(func,min);
  minutes = bcdtoi(inp(clock));

  outp(func,sec);
  seconds = bcdtoi(inp(clock));

  outp(func,mon);
  months = bcdtoi(inp(clock));

  outp(func,day);
  days = bcdtoi(inp(clock));

  outp(func,year);
  years = bcdtoi(inp(clock));

  printf("\n");
  printf("The time and date are: ");
  Write_time();
  printf("  ");
  Write_date();
  printf("\n");
} /* end main */

char Bcd(intval)
/* convert an integer value (intval) to byte in packed BCD format
   before sending it to clock.  Not used in this program.
*/
  int intval;
{
  char hinybble; /* hi nybble of packed bcd byte */

  hinybble = intval / 10;
  intval = intval - 10 * hinybble;
  hinybble = hinybble * 16;
  return (hinybble + intval);
} /* end bcd */

int bcdtoi(bcdbyte)
/* convert packed BCD byte read from clock to integer value that
   can be displayed as an ASCII string.
*/
  int bcdbyte;
{
  int lonybble; /* low nybble of bcdbyte */

  lonybble = bcdbyte & 15;
  bcdbyte = 10 * (bcdbyte / 16);
  return (bcdbyte + lonybble);
} /* end ascii */

Set_time() /* just for example, not used in this program */
{
  outp(func,hour);
  outp(clock,Bcd(hours));

  outp(func,min);
  outp(clock,Bcd(minutes));

  outp(func,sec);
  outp(clock,Bcd(seconds));
} /* end set time */

Set_date() /* just for example, not used in this program */
{
  outp(func,mon);
  outp(clock,Bcd(months));

  outp(func,day);
  outp(clock,Bcd(days));

  outp(func,year);
  outp(clock,Bcd(years));
} /* end set date */

Write_time() /* display the time */
{
  printf("%02d:",hours);
  printf("%02d:",minutes);
  printf("%02d",seconds);
} /* end write time */

Write_date() /* display the date */
{
  printf("%02d/",months);
  printf("%02d/",days);
  printf("%02d",years);
} /* end printf date */

int outp(value,port)

  int value, port;

{
#asm
;
;	function to output a byte to a data port
;
	POP	B		;get return address
	POP	H		;get data for port (variable value)
	POP	D		;get port number   (variable port)
	PUSH	D		;restore machine stack
	PUSH	H
	PUSH	B
	MOV	A,E		;get port number
	STA	outp1+1		;save port number
	MOV	A,L		;get data to send
outp1:
	OUT	0		;send data to port

#endasm
}


int inp(port)
  
  int port;
{
#asm
;	function: to input a byte from data port
;
	MOV	A,L		;get port number
	STA	inp1+1		;save port number for input
inp1:
	IN	0		;input data from port
	MVI	H,0		;zero high byte of number
	MOV	L,A		;put data in reg for calling function
#endasm
}

/* int inp(device)

  int device;

{
#asm
	POP	D
	POP	B
	PUSH	B
	PUSH	D
	LXI	H,0
	DB	0EDH,068H
#endasm
}

int outp(value, port)

  int value, port;

{
#asm
	POP	D
	POP	H
	POP	B
	PUSH	B
	PUSH	H
	PUSH	D
	DB	0EDH, 069H
#endasm
}	*/