-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.asm
179 lines (148 loc) · 6.19 KB
/
background.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
.include "settings.inc"
.include "constants.inc"
.bank 0, 16, $c000, "NES_PRG0"
.segment "Code"
.org $C000
;;;;;;;;;;;;;;
.segment "ROData"
.org $E000
palette:
.byte $22,$29,$1a,$0f, $22,$36,$17,$0f, $22,$30,$21,$0f, $22,$27,$17,$0f ;;background palette
.byte $0F,$2C,$25,$24, $0F,$02,$38,$3C, $0F,$1C,$15,$14, $0F,$02,$38,$3C ;;sprite palette
end_palette:
s_palette = (end_palette - palette)
rosprites:
;vert tile attr horiz
.byte $80, $32, $00, $80 ;sprite 0
.byte $80, $33, $00, $88 ;sprite 1
.byte $88, $34, $00, $80 ;sprite 2
.byte $88, $35, $00, $88 ;sprite 3
end_rosprites:
s_rosprites = (end_rosprites - rosprites)
background:
.storage $20, $24 ;;row 1, all sky -- This row is discarded
.byte $24, $f4, $f6, $f6, $f6, $f6, $f6, $f6, $f6, $f6, $f7, $24, $24, $24, $24, $24, $24, $24, $24, $24, $24, $24, $24, $24, $24, $24, $24, $24, $24, $24, $24, $24
.byte $24, $f8, $1c, $1e, $19, $24, $1d, $12, $16, $2b, $f8, $24, $24, $24, $24, $24, $24, $24, $24, $24, $24, $24, $24, $24, $24, $24, $24, $24, $24, $24, $24, $24
.byte $24, $f8, $24, $24, $24, $24, $24, $24, $24, $24, $f8, $24, $24, $24, $24, $24, $24, $24, $24, $24, $24, $24, $24, $24, $24, $24, $24, $24, $24, $24, $24, $24
.byte $24, $f8, $24, $24, $24, $24, $24, $24, $fd, $fe, $f8, $24, $24, $24, $24, $24, $24, $24, $24, $24, $24, $24, $24, $24, $24, $24, $24, $24, $24, $24, $24, $24
.byte $24, $f8, $24, $24, $24, $24, $24, $24, $24, $24, $f8, $24, $24, $24, $24, $24, $24, $24, $24, $24, $24, $24, $24, $24, $24, $24, $24, $24, $24, $24, $24, $24
.byte $24, $f5, $f6, $f6, $f6, $f6, $f6, $f6, $f6, $f6, $f9, $24, $24, $24, $24, $24, $24, $24, $24, $24, $24, $24, $24, $24, $24, $24, $24, $24, $24, $24, $24, $24
.storage $20, $24
end_background:
s_background = $FF//(end_background - background)
attribute:
.byte %00000000, %00010000, %01010000, %00010000, %00000000, %00000000, %00000000, %00110000
end_attribute:
s_attribute = (end_attribute - attribute)
;;;;;;;;;;;;;;
.function IRQ
RTI
.endfunction
.function VBLANKWAIT
BIT PPUSTATUS
BPL VBLANKWAIT
.endfunction
.function RESET
SEI ; disable IRQs
CLD ; disable decimal mode
LDX #$40
STX APUIRQ ; disable APU frame IRQ
LDX #$FF
TXS ; Set up stack
INX ; now X = 0
STX PPUCTRL ; disable NMI
STX PPUMASK ; disable rendering
STX DPMCIRQ ; disable DMC IRQs
VBLANKWAIT()
clrmem:
LDA #$00
STA $0000, x
STA $0100, x
STA $0200, x
STA $0400, x
STA $0500, x
STA $0600, x
STA $0700, x
LDA #$FE
STA $0300, x
INX
BNE clrmem
VBLANKWAIT() ; Second wait for vblank, PPU is ready after this
loadpalettes:
LDA PPUSTATUS ; read ppu status to reset the high/low latch
LDA #$3f
STA PPUADDR ; write the high byte of $3f00 address
LDA #$00
STA PPUADDR ; write the low byte of $3f00 address
LDX #$00 ; start out at 0
loadpalettesloop:
LDA palette, x ; load data from address (palette + the value in x)
STA PPUDATA ; write to ppu
INX ; x = x + 1
CPX #s_palette ; compare x to palette size
BNE loadpalettesloop ; repeat until all bytes loaded
loadbackground:
LDA PPUSTATUS ; read ppu status to reset the high/low latch
LDA #$20
STA PPUADDR ; write the high byte of $2000 address
LDA #$00
STA PPUADDR ; write the low byte of $2000 address
LDX #$00 ; start out at 0
loadbackgroundloop:
LDA background, x ; load data from address (background + the value in x)
STA PPUDATA ; write to ppu
INX ; x = x + 1
CPX #s_background ; compare x to background size
BNE loadbackgroundloop; repeat until all bytes copied
loadattribute:
LDA PPUSTATUS ; read ppu status to reset the high/low latch
LDA #$23
STA PPUADDR ; write the high byte of $23c0 address
LDA #$c0
STA PPUADDR ; write the low byte of $23c0 address
LDX #$00 ; start out at 0
loadattributeloop:
LDA attribute, x ; load data from address (attribute + the value in x)
STA PPUDATA ; write to ppu
INX ; x = x + 1
CPX #s_attribute ; compare x to hex $08, decimal 8 - copying 8 bytes
BNE loadattributeloop ; branch to loadattributeloop if compare was not equal to zero
; if compare was equal to 128, keep going down
LDA #%10010000 ; enable nmi, sprites from pattern table 0, background from pattern table 1
STA PPUCTRL
LDA #%00011110 ; enable sprites, enable background, no clipping on left side
STA PPUMASK
LDA #$00 ;;tell the ppu there is no background scrolling
STA PPUSCROLL
sta PPUSCROLL
forever:
JMP forever ;jump back to forever, infinite loop
.endfunction
.function NMI
LDA #$00
STA OAMADDR ; set the low byte (00) of the ram address
LDA #$02
STA OAMDMA ; set the high byte (02) of the ram address, start the transfer
LDA #%10010000 ; enable nmi, sprites from pattern table 0, background from pattern table 1
STA PPUCTRL
LDA #%00011110 ; enable sprites, enable background, no clipping on left side
STA PPUMASK
LDA #$00 ;;tell the ppu there is no background scrolling
STA PPUSCROLL
STA PPUSCROLL
RTI
.endfunction
;;;;;;;;;;;;;;
;.bank 1, 16, $FFFA, "NES_PRG1"
.segment "Vectors"
.org $FFFA ;first of the three vectors starts here
.word NMI ;when an NMI happens (once per frame if enabled) the
;processor will jump to the label NMI:
.word RESET ;when the processor first turns on or is reset, it will jump
;to the label RESET:
.word IRQ ;external interrupt IRQ is not used in this tutorial
;;;;;;;;;;;;;;
.bank 1, 8, $0000, "NES_CHR0"
.segment "Chr"
.org $0000
.incbin "mario.chr" ;includes 8KB graphics file from SMB1