|
- ; ============================================================
- ; bitmap-demo.asm - 320x200 hi-res bitmap on the C64 (ca65)
- ;
- ; Switches to hi-res bitmap mode, draws a diagonal line from (0,0) to
- ; (199,199), waits for a key, and restores the text screen.
- ;
- ; c64 run examples/bitmap-demo.asm
- ; c64 test examples/bitmap-demo.asm -Until wait_key (see build/test/screenshot.png)
- ;
- ; Memory (VIC bank 2, $8000-$BFFF):
- ; $8400-$87E7 screen matrix = bitmap colors (high nybble = ink, low = paper)
- ; $A000-$BF3F bitmap (8000 bytes). The CPU writes go to the RAM under BASIC
- ; ROM; the VIC-II always reads RAM there.
- ; The program itself must end below $8400 (it is far smaller).
- ; ============================================================
-
- ptr = $FB ; 2-byte work pointer (free zero page)
- bptr = $FD ; 2-byte bitmap pointer
-
- BITMAP = $A000
- COLORS = $8400
-
- CIA2_PRA = $DD00 ; bits 0-1: VIC bank (inverted)
- VIC_CTRL1 = $D011 ; bit 5: bitmap mode
- VIC_MEM = $D018 ; screen matrix and bitmap location in the bank
- VIC_CTRL2 = $D016 ; bit 4: multicolor
- VIC_BORDER = $D020
- VIC_BG = $D021
- GETIN = $FFE4
- CHROUT = $FFD2
-
- .segment "CODE"
- start:
- jsr init_bitmap
- jsr draw_diagonal
- wait_key: ; label for `c64 test -Until`
- jsr GETIN
- beq wait_key
- jsr restore_text
- rts
-
- ; ============================================================
- ; init_bitmap: VIC bank 2, bitmap at $A000, colors at $8400
- ; ============================================================
- .proc init_bitmap
- lda CIA2_PRA
- and #%11111100
- ora #%00000001 ; %01 = bank 2 ($8000-$BFFF)
- sta CIA2_PRA
-
- ; $D018: bits 7-4 = screen matrix / $0400 (1 -> $8400),
- ; bit 3 = bitmap at bank + $2000 ($A000)
- lda #%00011000
- sta VIC_MEM
-
- lda VIC_CTRL1
- ora #%00100000 ; bitmap mode on
- sta VIC_CTRL1
- lda VIC_CTRL2
- and #%11101111 ; multicolor off (hi-res)
- sta VIC_CTRL2
-
- lda #0 ; black border; the background register is
- sta VIC_BORDER ; not used in hi-res bitmap mode
- sta VIC_BG
-
- ; Clear the bitmap: 32 pages ($A000-$BFFF) covers the 8000 bytes.
- lda #<BITMAP
- sta ptr
- lda #>BITMAP
- sta ptr+1
- lda #0
- tay
- ldx #32
- clear: sta (ptr),y
- iny
- bne clear
- inc ptr+1
- dex
- bne clear
-
- ; Colors: white ink ($1x) on black paper ($x0) in all 1000 cells.
- ; Four pages from $8400 cover $8400-$87FF (24 bytes past the matrix, harmless).
- lda #<COLORS
- sta ptr
- lda #>COLORS
- sta ptr+1
- lda #$10
- ldy #0
- ldx #4
- fill: sta (ptr),y
- iny
- bne fill
- inc ptr+1
- dex
- bne fill
- rts
- .endproc
-
- ; ============================================================
- ; draw_diagonal: pixels (n, n) for n = 0..199
- ; ============================================================
- .proc draw_diagonal
- ; set_pixel reads the bitmap (ORA) before writing it. With BASIC ROM
- ; banked in, reads at $A000-$BFFF return ROM bytes, which would then be
- ; written into the bitmap. Bank BASIC out while drawing; the Kernal and
- ; I/O stay visible, so IRQs keep working.
- lda $01
- pha
- and #%11111110 ; LORAM = 0: RAM at $A000-$BFFF ($37 -> $36)
- sta $01
-
- ldx #0
- loop: txa ; y = x
- jsr set_pixel ; preserves X
- inx
- cpx #200
- bne loop
-
- pla
- sta $01 ; BASIC back in before returning to it
- rts
- .endproc
-
- ; ============================================================
- ; set_pixel: X = x (0-255), A = y (0-199). Preserves X.
- ; address = BITMAP + (y / 8) * 320 + (x / 8) * 8 + (y & 7)
- ; bit = 7 - (x & 7)
- ; ============================================================
- .proc set_pixel
- stx xcoord
- sta ycoord
-
- lsr ; y / 8 = character row
- lsr
- lsr
- tay
- lda rowlo,y ; row * 320
- sta bptr
- lda rowhi,y
- clc
- adc #>BITMAP
- sta bptr+1
-
- lda xcoord
- and #%11111000 ; (x / 8) * 8
- clc
- adc bptr
- sta bptr
- bcc :+
- inc bptr+1
- :
- lda ycoord
- and #%00000111 ; line within the character cell
- tay
-
- lda xcoord
- and #%00000111
- tax
- lda bitmask,x
- ora (bptr),y
- sta (bptr),y
-
- ldx xcoord ; restore X for the caller
- rts
- .endproc
-
- ; ============================================================
- ; restore_text: bank 0, screen $0400, charset ROM, text mode
- ; ============================================================
- .proc restore_text
- lda CIA2_PRA
- ora #%00000011 ; bank 0
- sta CIA2_PRA
- lda #$15 ; screen $0400, upper-case charset
- sta VIC_MEM
- lda VIC_CTRL1
- and #%11011111 ; bitmap mode off
- sta VIC_CTRL1
- lda #14 ; default light blue border, blue background
- sta VIC_BORDER
- lda #6
- sta VIC_BG
- lda #147 ; clear screen
- jmp CHROUT
- .endproc
-
- .segment "RODATA"
- bitmask: .byte $80, $40, $20, $10, $08, $04, $02, $01
-
- rowlo:
- .repeat 25, row
- .byte <(row * 320)
- .endrepeat
- rowhi:
- .repeat 25, row
- .byte >(row * 320)
- .endrepeat
-
- .segment "BSS"
- xcoord: .res 1
- ycoord: .res 1
|