/* Simple Small-C program to test CME11A or CMM11A LCD and keypad 
/* using HC11 port D and E (SS:KEYPAD). Key's pressed are displayed on the LCD.
/* To compile:  type at the dos prompt - sc keylcd-e

/* Define where things should reside... */
#define REG_BASE 0x1000
#data    0x2000
#code    0x2100

#include <68hc11.h>
#include <stdio.h>

/* I/O Port definitions */
#define LCD_DATA 0xB5F1      /* LCD Data Register */
#define LCD_CMD  0xB5F0      /* LCD Command Register */

char keybuf;	/* index variable for key scan */
char keychar;
unsigned char colnum, rownum, ColCnt, keylast, keyflag;

/* main loop */
void main(){

	pokeb(REG_BASE+DDRD,0x3E);	/* set data direction registers */
	pokeb(REG_BASE+DDRA,0xF0);

	/* Initialize the LCD */
    LCD_Command(0x3C);                 /* initialize command */
    LCD_Command(0x0C);                 /* display on, cursor off */
    LCD_Command(0x06);
    LCD_Command(0x01);

	LCDprint("Press any key: ");
	for( ; ;){
		keychar = getkey();
		cprint(keychar);
	}
}


/* returns 1 if a key is pressed.
   the key value/index is stored in the global variable keybuf.
*/
kphit(){
 	/* test rows high while column is high, if none high return */
	for(ColCnt = 0, colnum = 0x04; colnum < 0x40; ColCnt = ColCnt +4){
		pokeb(REG_BASE+PORTD,colnum);	/* set column value */
		#asm		/* delay for lines to settle */
			NOP
			NOP
		#endasm
		rownum = peekb(REG_BASE+PORTE);	/* get row value */
		rownum = rownum & 0x0f;			/* mask all but rows */
		if(rownum){	/* if a row in this column high */
			if(rownum == 0x04) rownum = 3;
			if(rownum == 0x08) rownum = 4;	/* figure row number */
			rownum = rownum - 1;
			keybuf = ColCnt + rownum;	/* calculate key index */
			if(keybuf == keylast)	return 0; /* ignore if same key pressed */
			keylast = keybuf;	/* save this key for repeat */
			return 1;			/* return YES  */
	   }
		/* colnum = colnum << 1; */ /* c code rotates the carry flag, which we don't want */
		#asm
			LSL	_colnum		; shift column left
		#endasm
	}
	keylast = 0xff;	/* initialize key repeat compare value*/
	return 0;	/* no key pressed, return 0 */
}

/* return key pressed value.  this table returns ASCII representations*/
/* variable keybuf contains the index */
keyval(){
	char KeyDATA[16];
	KeyDATA[0] = 	'1';
	KeyDATA[1] = 	'2';
	KeyDATA[2] = 	'3';
	KeyDATA[3] = 	'A';

	KeyDATA[4] = 	'4';
	KeyDATA[5] = 	'5';
	KeyDATA[6] = 	'6';
	KeyDATA[7] = 	'B';

	KeyDATA[8] = 	'7';
	KeyDATA[9] = 	'8';
	KeyDATA[10] = 	'9';
	KeyDATA[11] = 	'C';

	KeyDATA[12] = 	'*';
	KeyDATA[13] = 	'0';
	KeyDATA[14] = 	'#';
	KeyDATA[15] = 	'D';

	return(KeyDATA[keybuf]);
}

/* waits for a key press and returns it */
getkey(){
	while(kphit() == 0)	; /* wait for key press */
	return(keyval());     /* return it */
}


/* Wait for the LCD busy pin to clear                                 */
void LCD_busy(){
    while ((peekb(LCD_CMD) & 0x80)) ;
}

void LCD_Command(cval)
unsigned char cval;
{
    LCD_busy();                         /* wait for busy to clear     */
    pokeb(LCD_CMD,cval);                /* ouptut command             */
}

/* LCD Display Character                                              */
void cprint(dval)
char dval;
{
    LCD_busy();                         /* wait for busy to clear     */
    pokeb(LCD_DATA,dval);               /* ouptut data                */
}

/* LCD Display String                                                 */

void LCDprint(sptr)
unsigned char *sptr;
{
	while( *sptr ){
		cprint(*sptr);
		++sptr;
	}
}


/****************************************/
/*  SMALL C COMPILER                    */
/*  SYSTEM LIBRARY FOR THE 68HC11       */
/****************************************/

#asm
ARG1  EQU   6
ARG2  EQU   8
#endasm

/* NOTE: CCARGC MUST BE PASSED THE NUMBER OF BYTES OF LOCAL (STACK) */
/*       STORAGE FROM THE CALLING ROUTINE                           */
/* EX:   sort_lst(argc)int *argc;{                                  */
/*        char maxlen;                                              */
/*        char day;                                                 */
/*        char numargs;                                             */
/*        int  year;                                                */
/*  numargs=CCARGC(1+1+1+2); /*note the param=number of local bytes */

CCARGC(n)
int n;
{
#asm
      PSHX
      PULA
      PULB
#endasm
}

/* Enable interrupts */
e_int()
{
#asm
      CLI
#endasm
}

/* Disable interrupts */
d_int()
{
#asm
      SEI
#endasm
}

/* Called with 1's in the position to be set */
bit_set(addr,val)
int addr;
char val;
{
#asm
      LDX  ARG2,Y
      LDAB ARG1+1,Y
      ORAB 0,X
      STAB 0,X
#endasm
}

/* Called with 1's in the position to be cleared */
bit_clr(addr,val)
int addr;
char val;
{
#asm
      LDX  ARG2,Y
      LDAB ARG1+1,Y
      COMB
      ANDB 0,X
      STAB 0,X
#endasm
}

pokeb(addr,val)
unsigned int addr;
unsigned char val;
{
#asm
      LDX  ARG2,Y
      LDAB ARG1+1,Y
      STAB 0,X
#endasm
}

poke(addr,val)
unsigned int addr;
int val;
{
#asm
      LDX ARG2,Y
      LDD ARG1,Y
      STD 0,X
#endasm
}

peekb(addr)
unsigned int addr;
{
#asm
      LDX  ARG1,Y
      CLRA
      LDAB 0,X
#endasm
}

peek(addr)
unsigned int addr;
{
#asm
      LDX ARG1,Y
      LDD 0,X
#endasm
}

#asm
*************************************************
***    START OF LOW LEVEL LIBRARY ROUTINES   ****
***    PARTS BORROWED FROM MOTOROLA, OTHERS  ****
***    RE-WRITTEN BY MATT TAYLOR             ****
*************************************************

********
* MUL12
__MUL12
      PSHX
      PSHX
      PSHB
      PSHA
      TSX
      LDAA 3,X
      MUL
      STD  4,X
      LDD  1,X
      MUL
      ADDB 4,X
      STAB 4,X
      LDAB 3,X
      MUL
      ADDB 4,X
      STAB 4,X
      PULX
      PULX
      PULA
      PULB
      RTS

********
* ASR12
__ASR12
      XGDX
__ASRLOOP
      CPX  #0
      BEQ  __ASRDONE
      ASRA
      RORB
      DEX
      BRA  __ASRLOOP
__ASRDONE
      RTS

********
* ASL12
__ASL12
      XGDX
__ASLLOOP
      CPX  #0
      BEQ  __ASLDONE
      ASLD
      DEX
      BRA  __ASLLOOP
__ASLDONE
      RTS

********
*  __GE
__GE
      PSHX
      CPD  0,X
      BLT  __TRUE
      BRA  __FALSE

********
* __UGE
__UGE
      PSHX
      TSX
      CPD  0,X
      BLO  __TRUE
      BRA  __FALSE

********
* __GT
__GT
      PSHX
      TSX
      CPD  0,X
      BLE  __TRUE
      BRA  __FALSE

********
* __UGT
__UGT
      PSHX
      TSX
      CPD  0,X
      BLS  __TRUE
      BRA  __FALSE

********
* __LE
__LE
      PSHX
      TSX
      CPD  0,X
      BGT  __TRUE
      BRA  __FALSE

********
* __ULE
__ULE
      PSHX
      TSX
      CPD  0,X
      BHI  __TRUE
      BRA  __FALSE

********
* __LNEG
__LNEG
      PSHX
      CPD  #0
      BNE  __FALSE
      BRA  __TRUE

********
* __LT
__LT
      PSHX
      TSX
      CPD  0,X
      BGE  __TRUE
      BRA  __FALSE

********
* __ULT
__ULT
      PSHX
      TSX
      CPD  0,X
      BHS  __TRUE
      BRA  __FALSE

********
* __EQ
__EQ
      PSHX
      TSX
      CPD  0,X
      BEQ  __TRUE
      BRA  __FALSE

********
* __NE
__NE
      PSHX
      TSX
      CPD  0,X
      BNE  __TRUE
      BRA  __FALSE

********
*__TRUE
__TRUE
      PULX
      LDD  #1
      RTS

********
*__FALSE
__FALSE
      PULX
      LDD  #0
      RTS

********
* __SWITCH

__SWITCH
      PULX
__XLOOP
      PSHX
      LDX  0,X
      CPX  #0
      PULX
      BEQ  __XDEFAULT
      CPD  2,X
      BEQ  __XMATCH
      INX
      INX
      INX
      INX
      BRA  __XLOOP
__XDEFAULT
      INX
      INX
      JMP  0,X
__XMATCH
      LDX  0,X
      JMP  0,X
#endasm
