Difference between revisions of "6502 Emulator Example Code"

From CDOT Wiki
Jump to: navigation, search
(Type on the Screen)
(Type on the Screen)
Line 72: Line 72:
 
           iny
 
           iny
 
           jmp next
 
           jmp next
 +
 +
== Place a Graphic on the Screen ==
 +
 +
  define WIDTH 4 ; width of graphic
 +
  define HEIGHT 4 ; height of graphic
 +
 +
 +
  lda #$25 ; create a pointer at $10
 +
  sta $10 ;  which points to where
 +
  lda #$02 ;  the graphic should be drawn
 +
  sta $11
 +
 
 +
  lda #$00 ; number of rows we've drawn
 +
  sta $12 ;  is stored in $12
 +
 
 +
  ldx #$00 ; index for data
 +
  ldy #$00 ; index for screen column
 +
 
 +
  draw: lda data,x
 +
  sta ($10),y
 +
  inx
 +
  iny
 +
  cpy #WIDTH
 +
bne draw
 +
 +
  inc $12 ; increment row counter
 +
 +
  lda #HEIGHT ; are we done yet?
 +
  cmp $12
 +
  beq done ; ...exit if we are
 +
 
 +
  lda $10 ; load pointer
 +
  clc
 +
  adc #$20 ; add 32 to drop one row
 +
  sta $10
 +
  lda $11 ; carry to high byte if needed
 +
  adc #$00
 +
  sta $11
 +
 
 +
  ldy #$00
 +
  beq draw
 +
 
 +
  done: brk ; stop when finished
 +
 
 +
  data:
 +
  dcb 00,03,03,00
 +
  dcb 07,00,00,07
 +
  dcb 07,00,00,07
 +
  dcb 00,03,03,00

Revision as of 13:50, 8 January 2020

This is a collection of simple examples of 6502 code which will run in the emulator.

Fill the Bitmapped Display

      lda #$00     ; set pointer at $10 to $0200
      sta $10
      lda #$02
      sta $11
      
      ldx #$06     ; max value for $11
      
      ldy #$00     ; index

loop: sta ($10),y  ; store colour
      iny          ; increment index
      bne loop     ; branch until overflow
      
      inc $11      ; increment hi byte of pointer
      lda $11      ; load page number as colour
      cpx $11      ; compare with max value
      bne loop     ; continue if not done 
      
      rts          ; return

Place a Message on the Character Display

 define SCREEN $f000
           ldy #$00
 
 char:     lda text,y
           beq done
           sta SCREEN,y
           iny
           bne char
 
 con e:    brk
 
 text:
 dcb "6","5","0","2",32,"w","a","s",32,"h","e","r","e",".",00

Type on the Screen

; let the user type on the first page of character screen
; has blinking cursor!
; backspace works (non-destructive), arrows/ENTER don't
 
next:     ldx #$00
idle:     inx
          cpx #$10
          bne check
          lda $f000,y
          eor #$80
          sta $f000,y

check:    lda $ff
          beq idle

          ldx #$00
          stx $ff

          cmp #$08 ; bs
          bne print

          lda $f000,y
          and #$7f
          sta $f000,y

          dey
          jmp next

print:    sta $f000,y
          iny
          jmp next

Place a Graphic on the Screen

 define WIDTH 	4 ; width of graphic
 define HEIGHT 	4 ; height of graphic


 	lda #$25	; create a pointer at $10
 	sta $10		;   which points to where
 	lda #$02	;   the graphic should be drawn
 	sta $11
 
 	lda #$00	; number of rows we've drawn
 	sta $12		;   is stored in $12
 
 	ldx #$00	; index for data
 	ldy #$00	; index for screen column
 
 draw:	lda data,x
 	sta ($10),y
 	inx
 	iny
 	cpy #WIDTH

bne draw

 	inc $12		; increment row counter
 	lda #HEIGHT	; are we done yet?
 	cmp $12
 	beq done	; ...exit if we are
 
 	lda $10		; load pointer
 	clc
 	adc #$20	; add 32 to drop one row
 	sta $10
 	lda $11	; carry to high byte if needed
 	adc #$00
 	sta $11
 
 	ldy #$00
 	beq draw
 
 done:	brk		; stop when finished
 
 data:
 dcb 00,03,03,00
 dcb 07,00,00,07
 dcb 07,00,00,07
 dcb 00,03,03,00