					.setcpu "65C02"

					ACIA_DATA = $8200
					ACIA_STATUS = $8201
					ACIA_COMMAND = $8202
					ACIA_CONTROL = $8203
	
.segment "VECTORS"

          .word   nmi
          .word   reset
          .word   irq

          .code

reset:				jmp main

nmi:					rti

irq:					rti

main:
init_acia:			lda #%00001011				;No parity, no echo, no interrupt
					sta ACIA_COMMAND
					lda #%00011111				;1 stop bit, 8 data bits, 19200 baud
					sta ACIA_CONTROL

write:				ldx #0	;for x = 0 to length of message, send character to acia
next_char:
wait_txd_empty:		lda ACIA_STATUS
					;and #$10
					;beq wait_txd_empty
					lda text,x
					beq write
					sta ACIA_DATA
					inx
                                        jsr DELAY_6551
					jmp next_char

read:
wait_rxd_full:		lda ACIA_STATUS
					and #$08
					beq wait_rxd_full
					lda ACIA_DATA
					jmp write

text:				.byte "Hello World!", $0d, $0a, $00

DELAY_6551:   PHY      ;Save Y Reg
         PHX      ;Save X Reg
DELAY_LOOP:   LDY   #1    ;Get delay value (clock rate in MHz 2 clock cycles)
;
MINIDLY:   LDX   #$68      ;Seed X reg
DELAY_1:      DEX         ;Decrement low index
         BNE   DELAY_1   ;Loop back until done
;
         DEY         ;Decrease by one
         BNE   MINIDLY   ;Loop until done
         PLX         ;Restore X Reg
         PLY         ;Restore Y Reg
DELAY_DONE:   RTS     