/*------------------------------------------------------*/
/*                   Debug Libarary                     */
/*                                                      */
/*                      DEBUG.C                         */
/*                                                      */
/*                Written by:    Dan Kohn               */
/*                Rev:           2.1                    */
/*                Date:          4/30/00                */
/*                                                      */
/*                                                      */
/* Routines:                                            */
/*     debug()                                          */
/*     show(Address)                                    */
/*     showc(c)                                         */
/*     crlf()                                           */
/*                                                      */
/*______________________________________________________*/




/*------------------------------------------------------*/
/*                   Debug Routine                      */
/*                                                      */
/*                Written by:    Dan Kohn               */
/*                Rev:           1.9                    */
/*                Date:          4/25/00                */
/*                                                      */
/*                                                      */
/* Function: When called, the routine checks for        */
/*           incoming characters on the RS-232 port     */
/*           and then does one of the following:        */
/*                                                      */
/*           If no character present - Return           */
/*                                                      */
/*           IF 'Z' -> Echo back 'Z' (used for re-sync) */
/*                                                      */
/*           A '*' represents the start of a valid      */
/*                     incoming message. These messages */
/*                     have two possible formats:       */
/*                                                      */
/*           If the next four ascii characters are FFFF */
/*                     debug will get four additional   */
/*                     ascii characters that will be    */
/*                     translated as the address in     */
/*                     which to store a following 2     */
/*                     digit hex value.                 */
/*                                                      */
/*           If the next four ascii characters are NOT  */
/*                     FFFF the routine will translate  */
/*                     them into a hex address and      */
/*                     send the ascii representation of */
/*                     the value held in that address.  */
/*                     This response will start with a  */
/*                     '!' and end with a '@' character */
/*                                                      */
/*           This routine can be interfaced with via    */
/*           a dumb terminal program or via a high      */
/*           level language program.                    */
/*______________________________________________________*/


debug()
{
        #asm
           jsr INPUT               * IF 'Z' Echo back and  
           cmpa #'Z'               * exit (used for
           beq respond             * sync.

           cmpa #'*'               * check for valid 
           bne enddebug            * start of input ('*')

           jsr get_add             * Get 4 hex digits
           cmpx #$FFFF             * If NOT $FFFF then 
           bne d_loop5             * jmp to send address value


           jsr get_add             * if FFFF then get 4 hex digit address
           jsr get_hex             * get 2 hex digit value
           staa $0,x               * store value in address
           jmp enddebug            * jmp to end


d_loop5:   tab                     * SEND VALUE IN ADDRESS
           ldaa #'!'               * send '!' (start of msg)
           jsr OUTA
           TBA           
           jsr OUT1BSP             * send requested value
           ldaa #'@'               * send '@' (end of msg)
           jsr OUTA
           jmp enddebug            * jmp to end


respond:   ldaa #'Z'               * RESYNC (echo back 'Z')
           jsr OUTA
           jmp enddebug            * jmp to end

get_add:   nop
           jsr get_hex             * Get 2 hex digits
           psha
           jsr get_hex             * Get 2 more hex digits
           psha
           pulb
           pula
           xgdx                    * Return Address in X
           rts                     * Return


get_hex:   nop                     * GET 2 HEX DIGIT ROUTINE
           jsr getchar
           asla
           asla
           asla
           asla
           psha
           jsr getchar
           pulb
           aba
           rts                     * RETURN


getchar:   jsr INPUT               * WAIT FOR ASCII CHAR AND
           cmpa #$0                * RETURN HEX DIGIT
           beq getchar             * ONLY ACCEPTS 0..9 and 
           JSR OUTA                * A..F
           cmpa #'0'
           blt g_loop1
           cmpa #'9'
           bgt g_loop1
           suba #'0'
           jmp g_loop3
g_loop1:   cmpa #'A'
           blt g_loop2
           cmpa #'F'
           bgt g_loop2
           suba #'A'
           adda #10
           jmp g_loop3
g_loop2:   nop
g_loop3:   rts                      * RETURN


enddebug:  nop                      * End Point

#endasm

}


/*------------------------------------------------------*/
/*                    Show Routine                      */
/*                                                      */
/*                Written by:    Dan Kohn               */
/*                Rev:           1.1                    */
/*                Date:          1/14/00                */
/*                                                      */
/*                                                      */
/* Function: Sends the value stored in ADDRESS to       */
/*           the RS-232 port as 2 hex digits and a      */
/*           space.                                     */
/*______________________________________________________*/



show(address)
unsigned int address;
{
#asm
        ldx ARG1,y
        jsr OUT1BSP

#endasm
}


/*------------------------------------------------------*/
/*                    Showc Routine                     */
/*                                                      */
/*                Written by:    Dan Kohn               */
/*                Rev:           1.1                    */
/*                Date:          1/14/00                */
/*                                                      */
/*                                                      */
/* Function: Sends the character in C to the RS-232     */
/*           port.                                      */
/*______________________________________________________*/

showc(c)
char c;
{
#asm
        ldaa ARG1,y
        tba
        jsr OUTA
#endasm
}

/*------------------------------------------------------*/
/*                     CRLF Routine                     */
/*                                                      */
/*                Written by:    Dan Kohn               */
/*                Rev:           1.1                    */
/*                Date:          1/14/00                */
/*                                                      */
/*                                                      */
/* Function: Sends a carage return and line feed to the */
/*           RS-232 port.                               */
/*______________________________________________________*/


crlf()
{
#asm
        jsr OUTCRLF
#endasm
}


