;*************************************************************************** ; PROGRAM: RS232_String_Echo.asm ; PURPOSE: UART Terminal String and Echo Test ;*************************************************************************** ;These are the eight I/O addresses the 16550 UART uses for its registers. ;Change the UART_BASE to suit your hardware. #define EQU .equ UART_BASE: EQU 00h ;Data in/out UART0: EQU UART_BASE +0h ;Data in/out UART1: EQU UART_BASE +1h ;Check RX UART2: EQU UART_BASE +2h ;Interrupts UART3: EQU UART_BASE +3h ;Line control UART4: EQU UART_BASE +4h ;Modem control UART5: EQU UART_BASE +5h ;Line status UART6: EQU UART_BASE +6h ;Modem status UART7: EQU UART_BASE +7h ;Scratch register EOS: EQU 00h ; End of string. CR: EQU 0dh ; Carriage return. LF: EQU 0ah ; Line feed. space: EQU 20h ; Space. tab: EQU 09h ; Tabulator. .ORG $0000 ;*************************************************************************** ROM_START EQU 00h ROM_END EQU 7FFFh ; 32KB ROM. RAM_START EQU 8000h RAM_END EQU 0FFFFh ; 32KB RAM. ;*************************************************************************** ;Initialize Stack Pointer in RAM LD SP, RAM_END ; Set stack pointer to top of RAM. ;*************************************************************************** ;UART_INIT ;Function: Initialize the UART to BAUD Rate 9600 (1.8432 MHz clock input) ;*************************************************************************** UART_INIT: LD A,80h ;Mask to Set DLAB Flag OUT (UART3),A LD A,0Ch ;0Ch for 9600bps. 60h for 1200bps. Divisor = 12 (0Ch) @ 9600bps w/ 1.8432 Mhz. OUT (UART0),A ;Set BAUD rate to 9600. DLAB DLL = $0C. LD A,00h OUT (UART1),A ;Set BAUD rate to 9600. DLAB DLM = $00. DLAB (DLM + DLL) = $000C. LD A,03h OUT (UART3),A ;Set 8-N-1 byte framing: 8-bit data, no parity, 1 stop bit, reset DLAB Flag. ;*************************************************************************** ;START and ECHO ;Function: Print a string to the terminal screen, then echo every user character keypress. ;*************************************************************************** START: LD A, 00h LD HL, RS232_Echo CALL UART_PRNT_STR ECHO: CALL UART_RX ; Echo user key press. CALL UART_TX ; Read and send user key press. JP ECHO ;*************************************************************************** ;UART_PRNT_STR: ;Function: Print out string starting at MEM location (HL) to 16550 UART. ;*************************************************************************** UART_PRNT_STR: PUSH AF UART_PRNT_STR_LP: LD A,(HL) CP EOS ;Test for end byte of 00h. JP Z,UART_END_PRNT_STR ;Jump if end byte is found. CALL UART_TX INC HL ;Increment pointer to next char. JP UART_PRNT_STR_LP ;Transmit loop. UART_END_PRNT_STR: POP AF RET ;*************************************************************************** ;UART_TX_READY ;Function: Check if UART is ready to transmit. ;*************************************************************************** UART_TX_RDY: PUSH AF UART_TX_RDY_LP: IN A, (UART5) ;Fetch the control register. BIT 5, A ;Bit will be set to 1 if UART is ready to send. JP Z, UART_TX_RDY_LP POP AF RET ;*************************************************************************** ;UART_TX ;Function: Transmit character in A to UART. ;*************************************************************************** UART_TX: CALL UART_TX_RDY ;Ensure UART is ready to receive. OUT (UART0), A ;Transmit character in register A to UART. RET ;*************************************************************************** ;UART_RX_READY ;Function: Check if UART is ready to receive. ;*************************************************************************** UART_RX_RDY: PUSH AF UART_RX_RDY_LP: IN A,(UART5) ;Fetch the control register BIT 0,A ;Bit will be set if UART is ready to receive. JP Z,UART_RX_RDY_LP POP AF RET ;*************************************************************************** ;UART_RX ;Function: Receive character in UART to A. ;*************************************************************************** UART_RX: CALL UART_RX_RDY ;Make sure UART is ready to receive IN A,(UART0) ;Receive character in UART to A RET ;************************************************************************* RS232_Echo: .DB "RS232_String_Echo.asm", 0Dh,0Ah,00h ;************************************************************************* .END