Skip to content

Instantly share code, notes, and snippets.

@ryananguiano
Last active April 20, 2020 08:59
Show Gist options
  • Save ryananguiano/c19424812d00a8b7ae6ac75cc4ab0125 to your computer and use it in GitHub Desktop.
Save ryananguiano/c19424812d00a8b7ae6ac75cc4ab0125 to your computer and use it in GitHub Desktop.
6502 project Hello World code
PORTB = $6000
PORTA = $6001
DDRB = $6002
DDRA = $6003
E = %10000000
RW = %01000000
RS = %00100000
.org $8000
reset:
; Reset stack
ldx #$ff
txs
jsr setup_io
main:
jsr clear_screen
jsr primm
.string "Hello world!"
jsr clear_screen
jsr primm
.string "How are you?"
jsr clear_screen
jsr primm
.string "What are you up to later?"
jmp main
setup_io:
; Set data direction registers
lda #%11111111
sta DDRB
lda #%11100000
sta DDRA
; Setup display
lda #%00111000
jsr lcd_instruction
lda #%00001110
jsr lcd_instruction
lda #%00000110
jsr lcd_instruction
rts
clear_screen:
lda #%00000001
jsr lcd_instruction
rts
lcd_instruction:
sta PORTB
lda #0
sta PORTA
lda #E
sta PORTA
lda #0
sta PORTA
rts
write_letter:
sta PORTB
lda #RS
sta PORTA
lda #(RS | E)
sta PORTA
lda #RS
sta PORTA
rts
; Print immediate - prints the string immediately following jsr
DPL = $fd
DPH = $fe
primm:
pla ; get low part of (string address-1)
sta DPL
pla ; get high part of (string address-1)
sta DPH
ldy #1 ; use +1 for byte offset
primm2:
lda (DPL), y ; load next byte
inc DPL ; increase pointer
bne primm3
inc DPH ; handle page boundary
primm3:
ora #0 ; set flags to accumulator
beq primm4 ; NULL byte
jsr write_letter
jmp primm2 ; loop
primm4:
inc DPL ; increase pointer
bne primm5
inc DPH ; handle page boundary
primm5:
jmp (DPL) ; jump to address after NULL
; Reset vector
.org $fffc
.word reset
.word $0000
PORTB = $6000
PORTA = $6001
DDRB = $6002
DDRA = $6003
STRING_DATA = $20
E = %10000000
RW = %01000000
RS = %00100000
.org $8000
reset:
; Reset stack
ldx #$ff
txs
jsr setup
main:
jsr clear_screen
lda #<HELLO_WORLD
ldy #>HELLO_WORLD
jsr print_string
jsr clear_screen
lda #<HOW_ARE_YOU
ldy #>HOW_ARE_YOU
jsr print_string
jmp main
setup:
; Set data direction registers
lda #%11111111
sta DDRB
lda #%11100000
sta DDRA
; Setup display
lda #%00111000
jsr lcd_instruction
lda #%00001110
jsr lcd_instruction
lda #%00000110
jsr lcd_instruction
rts
clear_screen:
lda #%00000001
jsr lcd_instruction
rts
print_string:
sta STRING_DATA
sty STRING_DATA + 1
ldy #$00
print_loop:
lda (STRING_DATA), y
cmp #$00
beq print_done
jsr write_letter
iny
jmp print_loop
print_done:
rts
lcd_instruction:
sta PORTB
lda #0
sta PORTA
lda #E
sta PORTA
lda #0
sta PORTA
rts
write_letter:
sta PORTB
lda #RS
sta PORTA
lda #(RS | E)
sta PORTA
lda #RS
sta PORTA
rts
HELLO_WORLD .string "Hello world!"
HOW_ARE_YOU .string "How are you?"
.org $fffc
.word reset
.word $0000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment