2018-03-17 16:09:30 +00:00
|
|
|
// Based upon the example code from NES 101 tutorial by Michael Martin
|
|
|
|
// compile with -t nes_mmc4
|
|
|
|
|
2019-06-28 14:28:49 +00:00
|
|
|
import nes_joy
|
|
|
|
|
|
|
|
volatile byte initialized
|
|
|
|
|
2018-03-17 16:09:30 +00:00
|
|
|
void main() {
|
2019-06-28 14:28:49 +00:00
|
|
|
initialized = 0
|
|
|
|
set_prg_bank(0)
|
|
|
|
init_rw_memory()
|
2018-03-17 16:09:30 +00:00
|
|
|
init_graphics()
|
|
|
|
init_sound()
|
|
|
|
ppu_ctrl = %10001000
|
|
|
|
ppu_mask = %00011110
|
2019-06-28 14:28:49 +00:00
|
|
|
initialized = 1
|
2018-03-17 16:09:30 +00:00
|
|
|
while(true){}
|
|
|
|
}
|
|
|
|
|
|
|
|
void nmi() {
|
2019-06-28 14:28:49 +00:00
|
|
|
if initialized != 1 { return }
|
2018-03-17 16:09:30 +00:00
|
|
|
set_prg_bank(1)
|
|
|
|
scroll_screen()
|
|
|
|
set_prg_bank(2)
|
|
|
|
update_sprite()
|
|
|
|
set_prg_bank(3)
|
|
|
|
react_to_input()
|
|
|
|
}
|
|
|
|
|
|
|
|
void irq() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
array oam_buffer [256] @$200
|
2019-06-28 14:28:49 +00:00
|
|
|
byte scroll = 240
|
|
|
|
byte a = 0 // debouncing
|
2018-03-17 16:09:30 +00:00
|
|
|
segment(ram) sbyte dx
|
|
|
|
|
|
|
|
void init_graphics() {
|
2019-06-28 14:28:49 +00:00
|
|
|
set_horizontal_mirroring()
|
2018-03-17 16:09:30 +00:00
|
|
|
set_chr_bank0($06)
|
|
|
|
set_chr_bank1($1A)
|
|
|
|
init_sprites()
|
|
|
|
set_prg_bank(6)
|
|
|
|
load_palette()
|
|
|
|
load_name_tables()
|
|
|
|
}
|
|
|
|
|
|
|
|
void init_sound() {
|
|
|
|
apu_status = 1
|
|
|
|
apu_pulse1_sweep = 0
|
|
|
|
apu_frame_counter = $40
|
|
|
|
}
|
|
|
|
|
|
|
|
void init_sprites() {
|
|
|
|
byte i
|
|
|
|
for i,255,downto,0 {
|
|
|
|
oam_buffer[i] = 0
|
|
|
|
}
|
|
|
|
oam_buffer[0] = $70
|
|
|
|
oam_buffer[1] = 1
|
|
|
|
oam_buffer[3] = 1
|
|
|
|
dx = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
segment(prgrom6)
|
|
|
|
noinline void load_palette() {
|
|
|
|
byte i
|
|
|
|
ppu_set_addr($3f00)
|
|
|
|
for i,0,until,palette.length {
|
|
|
|
ppu_write_data(palette[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
segment(prgrom6)
|
|
|
|
noinline void load_name_tables() {
|
|
|
|
pointer p
|
|
|
|
byte page_count
|
|
|
|
byte b
|
|
|
|
|
|
|
|
read_ppu_status()
|
|
|
|
p = bg.addr
|
|
|
|
ppu_set_addr($2400)
|
|
|
|
for page_count,0,until,4 {
|
2019-06-28 14:28:49 +00:00
|
|
|
for b,0,to,255 {
|
2018-03-17 16:09:30 +00:00
|
|
|
ppu_write_data(p[b])
|
|
|
|
}
|
|
|
|
p.hi += 1
|
|
|
|
}
|
|
|
|
for page_count,0,until,4 {
|
2019-06-28 14:28:49 +00:00
|
|
|
for b,0,to,255 {
|
2018-03-17 16:09:30 +00:00
|
|
|
ppu_write_data(0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
segment(prgrom2)
|
|
|
|
noinline void update_sprite() {
|
|
|
|
ppu_oam_dma_write(oam_buffer.addr.hi)
|
|
|
|
if oam_buffer[3] == 0 {
|
|
|
|
dx = 1
|
|
|
|
high_c()
|
|
|
|
} else if oam_buffer[3] == 255-8 {
|
|
|
|
dx = -1
|
|
|
|
high_c()
|
|
|
|
}
|
|
|
|
oam_buffer[3] += dx
|
|
|
|
}
|
|
|
|
|
|
|
|
segment(prgrom3)
|
|
|
|
noinline void react_to_input() {
|
2019-06-28 14:28:49 +00:00
|
|
|
read_joy1()
|
|
|
|
if input_a != 0 { // A button
|
|
|
|
if a == 0 {
|
|
|
|
reverse_dx()
|
|
|
|
}
|
2018-03-17 16:09:30 +00:00
|
|
|
}
|
2019-06-28 14:28:49 +00:00
|
|
|
a = input_a
|
|
|
|
if input_dy < 0 { // Up button
|
2018-03-17 16:09:30 +00:00
|
|
|
if oam_buffer[0] > 7 { oam_buffer[0] -= 1}
|
|
|
|
}
|
2019-06-28 14:28:49 +00:00
|
|
|
if input_dy > 0 { // Down button
|
2018-03-17 16:09:30 +00:00
|
|
|
if oam_buffer[0] < 223 { oam_buffer[0] += 1}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
segment(prgrom3)
|
|
|
|
void reverse_dx() {
|
|
|
|
dx = 0 - dx
|
|
|
|
low_c()
|
|
|
|
}
|
|
|
|
|
|
|
|
segment(prgrom3)
|
|
|
|
void low_c() {
|
|
|
|
apu_pulse1_ctrl = $84
|
|
|
|
apu_pulse1_period = $9AA
|
|
|
|
}
|
|
|
|
|
|
|
|
segment(prgrom2)
|
|
|
|
void high_c() {
|
|
|
|
apu_pulse1_ctrl = $86
|
|
|
|
apu_pulse1_period = $869
|
|
|
|
}
|
|
|
|
|
|
|
|
segment(prgrom1)
|
|
|
|
noinline void scroll_screen() {
|
|
|
|
ppu_write_data(0)
|
|
|
|
if scroll != 0 {
|
|
|
|
scroll -= 1
|
|
|
|
ppu_set_scroll(0, scroll)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-29 20:57:40 +00:00
|
|
|
const array bg = [
|
2018-03-17 16:09:30 +00:00
|
|
|
" " ascii,
|
|
|
|
"12345678901234567890123456789012" ascii,
|
|
|
|
" " ascii,
|
|
|
|
" " ascii,
|
|
|
|
" PRESENTING NES 101 " ascii,
|
|
|
|
" A GUIDE FOR OTHERWISE " ascii,
|
|
|
|
" EXPERIENCED PROGRAMMERS " ascii,
|
|
|
|
" " ascii,
|
|
|
|
" TUTORIAL FILE BY " ascii,
|
|
|
|
" MICHAEL MARTIN " ascii,
|
|
|
|
" " ascii,
|
|
|
|
" " ascii,
|
|
|
|
" " ascii,
|
|
|
|
" PRESS UP AND DOWN TO SHIFT " ascii,
|
|
|
|
" THE SPRITE " ascii,
|
|
|
|
" " ascii,
|
|
|
|
" PRESS A TO REVERSE DIRECTION" ascii,
|
|
|
|
" " ascii,
|
|
|
|
" " ascii,
|
|
|
|
" " ascii,
|
|
|
|
" " ascii,
|
|
|
|
" " ascii,
|
|
|
|
" " ascii,
|
|
|
|
"CHARACTER SET HIJACKED FROM " ascii,
|
|
|
|
"COMMODORE BUSINESS MACHINES " ascii,
|
|
|
|
" (C64'S CHARACTER ROM)" ascii,
|
|
|
|
" " ascii,
|
|
|
|
"READY. " ascii,
|
|
|
|
" " ascii,
|
|
|
|
" " ascii,
|
|
|
|
$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,
|
|
|
|
$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,
|
|
|
|
$00,$00,$00,$00,$00,$00,$00,$00,$F0,$F0,$F0,$F0,$F0,$F0,$F0,$F0,
|
|
|
|
$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$0F,$0F,$0F,$0F,$0F,$0F,$0F,$0F
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2019-04-29 20:57:40 +00:00
|
|
|
const array palette = [
|
2018-03-17 16:09:30 +00:00
|
|
|
$0E,$00,$0E,$19,$00,$00,$00,$00,$00,$00,$00,$00,$01,$00,$01,$21,
|
|
|
|
$0E,$20,$22,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00
|
|
|
|
]
|
|
|
|
|
|
|
|
segment(chrrom0)
|
2019-04-29 20:57:40 +00:00
|
|
|
const array charset @$6200 = [
|
2018-03-17 16:09:30 +00:00
|
|
|
$00,$00,$00,$00,$00,$00,$00,$00,$FF,$FF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F,$FF,$FF,$FF,$FF,$FF,
|
|
|
|
$00,$18,$3C,$7E,$18,$18,$18,$18,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,
|
|
|
|
$00,$10,$30,$7F,$7F,$30,$10,$00,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF
|
|
|
|
]
|
|
|
|
|
|
|
|
segment(chrrom1)
|
2019-04-29 20:57:40 +00:00
|
|
|
const array sprites @$A000 = [
|
2018-03-17 16:09:30 +00:00
|
|
|
$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,
|
|
|
|
$18,$24,$66,$99,$99,$66,$24,$18,$00,$18,$18,$66,$66,$18,$18,$00
|
|
|
|
]
|