6502 Assembly Project

You,assembly

This project involves programming a 6502 microprocessor in assembly language to control an LCD display. The assembly code below initializes the microprocessor and the LCD, displaying the message "Hello, world!".

The initialization block defines memory addresses for port and data direction registers. Constants E, RW, and RS represent specific bits for control signals.

PORTB = $6000
PORTA = $6001
DDRB = $6002
DDRA = $6003

E  = %10000000
RW = %01000000
RS = %00100000

The reset routine initializes the microprocessor, configures port directions, and sets up the LCD for operation.

.org $8000

reset:
  ldx #$ff
  txs

  lda #%11111111 ; Set all pins on port B to output
  sta DDRB
  lda #%11100000 ; Set top 3 pins on port A to output
  sta DDRA

  lda #%00111000 ; Set 8-bit mode; 2-line display; 5x8 font
  jsr lcd_instruction
  lda #%00001110 ; Display on; cursor on; blink off
  jsr lcd_instruction
  lda #%00000110 ; Increment and shift cursor; don't shift display
  jsr lcd_instruction
  lda #$00000001 ; Clear display
  jsr lcd_instruction

The print loop iterates through the message string, calling the print_char subroutine for each character.

  ldx #0
print:
  lda message,x
  beq loop
  jsr print_char
  inx
  jmp print

The lcd_wait subroutine handles waiting for the LCD to be ready before issuing instructions or data.

loop:
  jmp loop

message: .asciiz "Hello, world!"

lcd_wait:
  pha
  lda #%00000000  ; Port B is input
  sta DDRB
lcdbusy:
  lda #RW
  sta PORTA
  lda #(RW | E)
  sta PORTA
  lda PORTB
  and #%10000000
  bne lcdbusy

  lda #RW
  sta PORTA
  lda #%11111111  ; Port B is output
  sta DDRB
  pla
  rts

The lcd_instruction subroutine sends instructions to the LCD, utilizing the lcd_wait routine for synchronization.

lcd_instruction:
  jsr lcd_wait
  sta PORTB
  lda #0         ; Clear RS/RW/E bits
  sta PORTA
  lda #E         ; Set E bit to send instruction
  sta PORTA
  lda #0         ; Clear RS/RW/E bits
  sta PORTA
  rts

The print_char subroutine sends individual characters to the LCD for display.

print_char:
  jsr lcd_wait
  sta PORTB
  lda #RS         ; Set RS; Clear RW/E bits
  sta PORTA
  lda #(RS | E)   ; Set E bit to send instruction
  sta PORTA
  lda #RS         ; Clear E bits
  sta PORTA
  rts
.org $ff
© Kevin Siraki.RSS