-
Notifications
You must be signed in to change notification settings - Fork 2
/
gfx.asm
143 lines (123 loc) · 2.53 KB
/
gfx.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
.segment "CODE"
.export LoadGraphics, LoadPalette, LoadSpritelist
.export EnableDisplayAndNmi, EnableDisplay, EnableNmi, WaitNewFrame
.export DisableDisplay
.importzp ppu_mask_current, ppu_ctrl_current, main_yield
.include "include.mov-macros.asm"
.include "include.sys.asm"
.importzp pointer
;LoadGraphics
; Takes a pointer to nametable and attributes, of size $400, and loads it into
; PPU memory.
; @in x: Low byte of the pointer.
; @in y: Low byte of the pointer.
.proc LoadGraphics
bit PPU_STATUS
stx pointer+0
sty pointer+1
ldx #4
ldy #0
mov PPU_ADDR, #$20
mov PPU_ADDR, #0
Loop:
lda (pointer),y
sta PPU_DATA
iny
bne Loop
inc pointer+1
dex
bne Loop
rts
.endproc
;LoadPalette
; Takes a pointer to a palette, of size $20, and loads it into PPU memory.
; @in x: Low byte of the pointer.
; @in y: Low byte of the pointer.
.proc LoadPalette
bit PPU_STATUS
stx pointer+0
sty pointer+1
lda #$3f
sta PPU_ADDR
lda #$00
sta PPU_ADDR
ldy #0
Loop:
lda (pointer),y
sta PPU_DATA
iny
cpy #$20
bne Loop
ldy #0
lda (pointer),y
sta PPU_DATA
rts
.endproc
;LoadSpritelist
; Takes a pointer to a list of sprites, terminated by $ff, and loads it into
; the shadow OAM.
; @in x: Low byte of the pointer.
; @in y: Low byte of the pointer.
.proc LoadSpritelist
stx pointer+0
sty pointer+1
ldy #0
Loop:
lda (pointer),y
cmp #$ff
beq Done
sta $200,y
iny
bne Loop
Done:
rts
.endproc
;EnableDisplayAndNmi
; Enable PPU rendering and enable the NMI.
.proc EnableDisplayAndNmi
lda #(PPU_CTRL_NMI_ENABLE | PPU_CTRL_SPRITE_1000)
sta PPU_CTRL
sta ppu_ctrl_current
lda #(PPU_MASK_SHOW_SPRITES | PPU_MASK_SHOW_BG | PPU_MASK_NOCLIP_SPRITES | PPU_MASK_NOCLIP_BG)
sta PPU_MASK
sta ppu_mask_current
cli
rts
.endproc
;EnableNmi
; Enable the NMI, and disable everything else.
.proc EnableNmi
lda #(PPU_CTRL_NMI_ENABLE | PPU_CTRL_SPRITE_1000)
sta PPU_CTRL
sta ppu_ctrl_current
cli
rts
.endproc
;WaitNewFrame
; Wait until the next NMI happens and is finished.
.proc WaitNewFrame
mov main_yield, #0
WaitLoop:
bit main_yield
bpl WaitLoop
mov main_yield, #0
rts
.endproc
;EnableDisplay
; Enable PPU rendering, keeping other PPU settings the same.
.proc EnableDisplay
lda ppu_mask_current
ora #(PPU_MASK_SHOW_SPRITES | PPU_MASK_SHOW_BG)
sta PPU_MASK
sta ppu_mask_current
rts
.endproc
;DisableDisplay
; Disable PPU rendering.
.proc DisableDisplay
lda ppu_mask_current
and #($ff & ~PPU_MASK_SHOW_SPRITES & ~PPU_MASK_SHOW_BG)
sta PPU_MASK
sta ppu_mask_current
rts
.endproc