No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

203 líneas
6.0KB

  1. ; ============================================================
  2. ; bitmap-demo.asm - 320x200 hi-res bitmap on the C64 (ca65)
  3. ;
  4. ; Switches to hi-res bitmap mode, draws a diagonal line from (0,0) to
  5. ; (199,199), waits for a key, and restores the text screen.
  6. ;
  7. ; c64 run examples/bitmap-demo.asm
  8. ; c64 test examples/bitmap-demo.asm -Until wait_key (see build/test/screenshot.png)
  9. ;
  10. ; Memory (VIC bank 2, $8000-$BFFF):
  11. ; $8400-$87E7 screen matrix = bitmap colors (high nybble = ink, low = paper)
  12. ; $A000-$BF3F bitmap (8000 bytes). The CPU writes go to the RAM under BASIC
  13. ; ROM; the VIC-II always reads RAM there.
  14. ; The program itself must end below $8400 (it is far smaller).
  15. ; ============================================================
  16. ptr = $FB ; 2-byte work pointer (free zero page)
  17. bptr = $FD ; 2-byte bitmap pointer
  18. BITMAP = $A000
  19. COLORS = $8400
  20. CIA2_PRA = $DD00 ; bits 0-1: VIC bank (inverted)
  21. VIC_CTRL1 = $D011 ; bit 5: bitmap mode
  22. VIC_MEM = $D018 ; screen matrix and bitmap location in the bank
  23. VIC_CTRL2 = $D016 ; bit 4: multicolor
  24. VIC_BORDER = $D020
  25. VIC_BG = $D021
  26. GETIN = $FFE4
  27. CHROUT = $FFD2
  28. .segment "CODE"
  29. start:
  30. jsr init_bitmap
  31. jsr draw_diagonal
  32. wait_key: ; label for `c64 test -Until`
  33. jsr GETIN
  34. beq wait_key
  35. jsr restore_text
  36. rts
  37. ; ============================================================
  38. ; init_bitmap: VIC bank 2, bitmap at $A000, colors at $8400
  39. ; ============================================================
  40. .proc init_bitmap
  41. lda CIA2_PRA
  42. and #%11111100
  43. ora #%00000001 ; %01 = bank 2 ($8000-$BFFF)
  44. sta CIA2_PRA
  45. ; $D018: bits 7-4 = screen matrix / $0400 (1 -> $8400),
  46. ; bit 3 = bitmap at bank + $2000 ($A000)
  47. lda #%00011000
  48. sta VIC_MEM
  49. lda VIC_CTRL1
  50. ora #%00100000 ; bitmap mode on
  51. sta VIC_CTRL1
  52. lda VIC_CTRL2
  53. and #%11101111 ; multicolor off (hi-res)
  54. sta VIC_CTRL2
  55. lda #0 ; black border; the background register is
  56. sta VIC_BORDER ; not used in hi-res bitmap mode
  57. sta VIC_BG
  58. ; Clear the bitmap: 32 pages ($A000-$BFFF) covers the 8000 bytes.
  59. lda #<BITMAP
  60. sta ptr
  61. lda #>BITMAP
  62. sta ptr+1
  63. lda #0
  64. tay
  65. ldx #32
  66. clear: sta (ptr),y
  67. iny
  68. bne clear
  69. inc ptr+1
  70. dex
  71. bne clear
  72. ; Colors: white ink ($1x) on black paper ($x0) in all 1000 cells.
  73. ; Four pages from $8400 cover $8400-$87FF (24 bytes past the matrix, harmless).
  74. lda #<COLORS
  75. sta ptr
  76. lda #>COLORS
  77. sta ptr+1
  78. lda #$10
  79. ldy #0
  80. ldx #4
  81. fill: sta (ptr),y
  82. iny
  83. bne fill
  84. inc ptr+1
  85. dex
  86. bne fill
  87. rts
  88. .endproc
  89. ; ============================================================
  90. ; draw_diagonal: pixels (n, n) for n = 0..199
  91. ; ============================================================
  92. .proc draw_diagonal
  93. ; set_pixel reads the bitmap (ORA) before writing it. With BASIC ROM
  94. ; banked in, reads at $A000-$BFFF return ROM bytes, which would then be
  95. ; written into the bitmap. Bank BASIC out while drawing; the Kernal and
  96. ; I/O stay visible, so IRQs keep working.
  97. lda $01
  98. pha
  99. and #%11111110 ; LORAM = 0: RAM at $A000-$BFFF ($37 -> $36)
  100. sta $01
  101. ldx #0
  102. loop: txa ; y = x
  103. jsr set_pixel ; preserves X
  104. inx
  105. cpx #200
  106. bne loop
  107. pla
  108. sta $01 ; BASIC back in before returning to it
  109. rts
  110. .endproc
  111. ; ============================================================
  112. ; set_pixel: X = x (0-255), A = y (0-199). Preserves X.
  113. ; address = BITMAP + (y / 8) * 320 + (x / 8) * 8 + (y & 7)
  114. ; bit = 7 - (x & 7)
  115. ; ============================================================
  116. .proc set_pixel
  117. stx xcoord
  118. sta ycoord
  119. lsr ; y / 8 = character row
  120. lsr
  121. lsr
  122. tay
  123. lda rowlo,y ; row * 320
  124. sta bptr
  125. lda rowhi,y
  126. clc
  127. adc #>BITMAP
  128. sta bptr+1
  129. lda xcoord
  130. and #%11111000 ; (x / 8) * 8
  131. clc
  132. adc bptr
  133. sta bptr
  134. bcc :+
  135. inc bptr+1
  136. :
  137. lda ycoord
  138. and #%00000111 ; line within the character cell
  139. tay
  140. lda xcoord
  141. and #%00000111
  142. tax
  143. lda bitmask,x
  144. ora (bptr),y
  145. sta (bptr),y
  146. ldx xcoord ; restore X for the caller
  147. rts
  148. .endproc
  149. ; ============================================================
  150. ; restore_text: bank 0, screen $0400, charset ROM, text mode
  151. ; ============================================================
  152. .proc restore_text
  153. lda CIA2_PRA
  154. ora #%00000011 ; bank 0
  155. sta CIA2_PRA
  156. lda #$15 ; screen $0400, upper-case charset
  157. sta VIC_MEM
  158. lda VIC_CTRL1
  159. and #%11011111 ; bitmap mode off
  160. sta VIC_CTRL1
  161. lda #14 ; default light blue border, blue background
  162. sta VIC_BORDER
  163. lda #6
  164. sta VIC_BG
  165. lda #147 ; clear screen
  166. jmp CHROUT
  167. .endproc
  168. .segment "RODATA"
  169. bitmask: .byte $80, $40, $20, $10, $08, $04, $02, $01
  170. rowlo:
  171. .repeat 25, row
  172. .byte <(row * 320)
  173. .endrepeat
  174. rowhi:
  175. .repeat 25, row
  176. .byte >(row * 320)
  177. .endrepeat
  178. .segment "BSS"
  179. xcoord: .res 1
  180. ycoord: .res 1

Powered by TurnKey Linux.