1
2; Clear VBL flag then wait for it to be set
3; Preserved: A, X, Y
4wait_vbl:
5      bit   $2002
6:     bit   $2002
7      bpl   -
8      rts
9      .code
10
11; Set VRAM address to A * $100
12; Preserved: X, Y
13set_vpage:
14      bit   $2002
15      sta   $2006
16      lda   #0
17      sta   $2006
18      rts
19      .code
20
21; Set VRAM address to A * $100 + X
22; Preserved: A, X, Y
23set_vaddr:
24      bit   $2002
25      sta   $2006
26      stx   $2006
27      rts
28      .code
29
30; Set X and Y scroll
31; Preserved: A, X, Y
32set_vscroll:
33      bit   $2002
34      stx   $2005
35      sty   $2005
36      rts
37      .code
38
39; Turn off NMI and disable BG and sprites
40; Preserved: A, X, Y
41disable_ppu:
42      pha
43      lda   #0
44      sta   $2000
45      sta   $2001
46      bit   $2002
47      sta   $2006
48      sta   $2006
49      pla
50      rts
51      .code
52
53; Set sprite memory to $ff
54; Preserved: Y
55clear_sprites:
56      lda   #$ff
57      ldx   #0
58:     sta   $2004
59      dex
60      bne   -
61      rts
62      .code
63
64; Clear/fill nametable with 0/A and clear attributes to 0
65clear_nametable:
66      lda   #0
67fill_nametable:
68      pha
69      lda   #$20
70      jsr   set_vpage
71      pla
72      ldx   #240
73:     sta   $2007
74      sta   $2007
75      sta   $2007
76      sta   $2007
77      dex
78      bne   -
79      lda   #0
80      ldx   #32
81:     sta   $2007
82      dex
83      bne   -
84      rts
85      .code
86
87; Clear/fill VRAM with 0/A
88clear_vram:
89      lda   #0
90fill_vram:
91      ldy   #$24
92      jmp   fill_vram_
93      .code
94
95; Clear/fill CHR with 0/A
96clear_chr:
97      lda   #0
98fill_chr:
99      ldy   #$20
100fill_vram_:
101      tax
102      lda   #0
103      jsr   set_vpage
104      txa
105      ldx   #0
106:     sta   $2007
107      dex
108      bne   -
109      dey
110      bne   -
111      rts
112      .code
113