/*
			  CMOS RAM DISPLAY PROGRAM
			    by JAMES D. MCDANIEL
				  10-18-88
*/

#include <stdio.h>
#include <graph.h>
#include <conio.h>

/* program to convert int to binary number */

extern void bitout();
char buffer [255];
main()
{
    int proterm=1;
    while (proterm)
    {
	{

	/* Main Menu Display */

	_clearscreen(_GCLEARSCREEN);
	_settextcolor(15);
	_settextposition(2,21);
	_outtext("AT REAL TIME CLOCK and CMOS RAM");
	_settextcolor(7);
	_settextposition(5,10);
	_outtext("This program will let you look at the Real Time Clock and");
	_settextposition(6,10);
	_outtext("the CMOS RAM used to store an AT's setup info.  See the");
	_settextposition(7,10);
	_outtext("documentation for more information.  If you have any adds");
	_settextposition(8,10);
	_outtext("or changes to the information presented, let me know.");
	_settextposition(10,50);
	_outtext("Thank You,");
	_settextposition(11,50);
	_outtext("James D. McDaniel");
	_settextposition(12,50);
	_outtext("CIS 74365,1716");
	_settextposition(18,7);
	_outtext("Would you like to : view <");
	_settextcolor(15);
	_outtext("A");
	_settextcolor(7);
	_outtext(">ll CMOS memory locations");
	_settextposition(19,27);
	_outtext("view <");
	_settextcolor(15);
	_outtext("S");
	_settextcolor(7);
	_outtext(">etup CMOS memory locations in detail");
	_settextposition(20,29);
	_outtext("or <");
	_settextcolor(15);
	_outtext("Q");
	_settextcolor(7);
	_outtext(">uit and exit to DOS.");
	}
	{
	int true = 1;
	char ch;
	while (true)
	    {
	    ch = getch();
	    ch = tolower(ch);
	    if (ch == 'q')
		{
		proterm=0;
		break;
		}
	    switch (ch)
		{

		/* View All CMOS memory locations */

		case 'a':
		    {
		    int true2=1;
		    _clearscreen(_GCLEARSCREEN);
		    _settextposition(4,25);
		    _settextcolor(15);
		    _outtext("DISPLAY OF ALL CMOS RAM MEMORY");
		    _settextposition(7,15);
		    _outtext("HEX  0  1  2  3  4  5  6  7  8  9 ");
		    _outtext("A  B  C  D  E  F");
			do
			{
			int ltime;
			int i;
			for (i = 0; i <= 3; i++)
			    {
			    int a = i * 16;
			    _settextposition(9+(i*2),15);
			    _settextcolor(15);
			    if (a==0)
				sprintf(buffer,"0%X",a);
			    else
				sprintf(buffer,"%X",a);
			    _settextcolor(15);
			    _outtext(buffer);
			    _outtext("= ");
				{
				int y;
				int d;
				for (y = 0; y <= 15; y++)
				    {
				    int z = ((i * 16)+y);
				    outp(112,z);
				    d=inp(113);
				    if (z==0)
					ltime = d;
				    if (d <= 15)
					sprintf(buffer,"0%X",d);
				    else
				    sprintf(buffer,"%X",d);
				    _settextcolor(7);
				    _outtext(buffer);
				    _outtext(" ");
				    }
				}
			    }
			    _settextposition(18,22);
			    _settextcolor(15);
			    _outtext("Press any key to return to Main Menu");
			    outp(112,0);

			    /* update screen on each sec change */

			    while (1)
			    {
			    if (ltime != inp(113))
				break;
			    if (kbhit())
				{
				true2=0;
				break;
				}
			    }

			/* exit screen update if any key is pressed */

			} while (true2);
		    }
		    true=0;
		    break;
		case 's':
		    {

		    /* View only Setup data memory locations */

		    int x;
		    int y;
		    int a;
		    unsigned int d;
		    _clearscreen(_GCLEARSCREEN);
		    _settextcolor(15);
		    _settextposition(1,25);
		    _outtext("AT CMOS RAM SETUP DATA DISPLAY");
		    _settextposition(3,5);
		    _outtext("OUTPUT ADDRESS # TO I/O PORT 70,");
		    _outtext("INPUT OR OUTPUT DATA FROM I/O PORT 71");
		    _settextposition(5,1);
		    _outtext("HEX  HEX   BINARY");
		    _settextposition(6,1);
		    _outtext("ADD# DATA 7654 3210");
		    _settextposition(5,41);
		    _outtext("HEX  HEX   BINARY");
		    _settextposition(6,41);
		    _outtext("ADD# DATA 7654 3210");

		    /* Display HEX ADD/DATA and BINARY DATA */

		    for ( x = 1; x <= 2; x++)
			{
			for ( y = 0; y <= 15; y++)
			    {
			    a = ((x*16)+y);
			    outp(112,a);
			    d = inp(113);
			    _settextposition(y+8,(1+((x-1)*40)));
			    _outtext(" ");
			    sprintf(buffer,"%X",a);
			    _settextcolor(15);
			    _outtext(buffer);
			    _outtext(" = ");
			    if (d <= 15)
				sprintf(buffer,"0%X",d);
			    else
				sprintf(buffer,"%X",d);
			    _settextcolor(7);
			    _outtext(buffer);
			    _outtext("  ");
			    bitout(&d);
			    if (a==16)
				_outtext(" Floppy Drives");
			    if (a==17)
				_outtext(" Reserved");
			    if (a==18)
				_outtext(" Hard Drives");
			    if (a==19)
				_outtext(" Reserved");
			    if (a==20)
				_outtext(" Equipment List");
			    if (a==21)
				_outtext(" Low Base Memory");
			    if (a==22)
				_outtext(" High Base Memory");
			    if (a==23)
				_outtext(" Low Ext Memory");
			    if (a==24)
				_outtext(" High Ext Memory");
			    if (a==25)
				_outtext(" Drive C Extended");
			    if (a==26)
				_outtext(" Drive D Extended");
			    if (a>=27)
				if (a<=45)
				    _outtext(" Reserved");
			    if (a==46)
				_outtext(" 10-2DH Checksum High");
			    if (a==47)
				_outtext(" 10-2DH Checksum Low");
			    _settextposition(25,22);
			    _settextcolor(15);
			    _outtext("Press any key to return to Main Menu");
			    }
			}
		    }
		    while (!kbhit())
		    { }
		    true=0;
		    break;
		}
	    }
	}
	_clearscreen(_GCLEARSCREEN);
    }

} /* MAIN LOOP */

 /*
    Convert int byte to binary display
    Only the Low int byte is converted,
    to convert High & Low byte use for
    loop with int i = 0 and i = 1
 */

 void bitout(unsigned int num[])

{
    int bytes = 2, i, j;
    int p = 0;
    char buffer [10];

    /* IBM PC STORES INTS LOW/HI. */

    i = 0;
    for (j = 7; j >= 4; --j)
	{
	buffer [p] = ((num[i]&(1<<j))?'1':'0');
	++p;
	}
    buffer [p] = 32;
    ++p;
    for (j = 3; j >=0; --j)
	{
	buffer [p] = ((num[i]&(1<<j))?'1':'0');
	++p;
	}
	buffer [p] = 0;
	_outtext(buffer);
}
