1
0
mirror of https://github.com/sehugg/8bitworkshop.git synced 2024-06-06 09:29:33 +00:00

nes: presets

This commit is contained in:
Steven Hugg 2019-02-28 16:08:41 -05:00
parent ccf5824bb4
commit a243044ed5
7 changed files with 106 additions and 104 deletions

View File

@ -19,9 +19,9 @@ Start:
jsr WaitSync ; wait for VSYNC (and PPU warmup)
lda #$3f ; $3F -> A register
ldy #$00 ; $00 -> Y register
sta PPU_ADDR ; write high byte first
sty PPU_ADDR ; $3F00 -> PPU address
lda #$00 ; $00 -> A register
sta PPU_ADDR ; $3F00 -> PPU address
lda #$1c ; $1C = light blue color
sta PPU_DATA ; $1C -> PPU data
lda #CTRL_NMI

View File

@ -33,11 +33,9 @@ Start:
; fill video RAM with "Hello World" msg
HelloVRAM: subroutine
ldy #$20
lda #$21 ; PPU addr = $2021
sty PPU_ADDR
sta PPU_ADDR ; -> PPU_ADDR
ldy #0
; set PPU address to name table A (row 1, col 1)
PPU_SETADDR $2021
ldy #0 ; set Y counter to 0
.loop:
lda HelloMsg,y ; get next character
beq .end ; is 0? exit loop
@ -46,7 +44,7 @@ HelloVRAM: subroutine
bne .loop ; loop
.end
rts ; return to caller
3
; ASCII message to display on screen
HelloMsg:
.byte "Hello, World!"
@ -54,10 +52,8 @@ HelloMsg:
; set palette colors
SetPalette: subroutine
ldy #$00 ; Y = $00 (also palette index)
lda #$3f ; A = $3F
sta PPU_ADDR ; $3F?? -> PPU address
sty PPU_ADDR ; $3F00 -> PPU address
; set PPU address to palette start
PPU_SETADDR $3f00
.loop:
lda Palette,y ; lookup byte in ROM
sta PPU_DATA ; store byte to PPU data
@ -73,17 +69,16 @@ SetPalette: subroutine
;;;;; INTERRUPT HANDLERS
NMIHandler:
rti
rti ; return from interrupt
;;;;; CONSTANT DATA
align $100
Palette:
hex 1f ;background
hex 09092c00 ;bg0
hex 09091900 ;bg1
hex 09091500 ;bg2
hex 09092500 ;bg3
hex 1f ;screen color
hex 09092c00 ;background 0
hex 09091900 ;background 1
hex 09091500 ;background 2
hex 09092500 ;background 3
;;;;; CPU VECTORS

View File

@ -6,11 +6,11 @@
seg.u RAM
org $0
ScrollPos byte ; used during NMI
ScrollPos word ; used during NMI
;;;;; NES CARTRIDGE HEADER
NES_HEADER 0,2,1,0 ; mapper 0, 2 PRGs, 1 CHR, horiz. mirror
NES_HEADER 0,2,1,1 ; mapper 0, 2 PRGs, 1 CHR, horiz. mirror
;;;;; START OF CODE
@ -36,13 +36,12 @@ Start:
; fill video RAM
FillVRAM: subroutine
txa
ldy #$20
sty PPU_ADDR ; $20?? -> PPU address
sta PPU_ADDR ; $2000 -> PPU address
PPU_SETADDR $2000
ldy #$10 ; set $10 pages ($1000 bytes)
.loop:
stx PPU_DATA ; X -> PPU data port
tya ; Y -> A
ora #$40 ; A = A OR $40
sta PPU_DATA ; X -> PPU data port
inx ; X = X + 1
bne .loop ; repeat until 256 bytes
dey ; Y = Y - 1
@ -51,10 +50,7 @@ FillVRAM: subroutine
; set palette colors
SetPalette: subroutine
ldy #$00 ; Y = $00 (also palette index)
lda #$3f ; A = $3F
sta PPU_ADDR ; $3F?? -> PPU address
sty PPU_ADDR ; $3F00 -> PPU address
PPU_SETADDR $3f00
.loop:
lda Palette,y ; lookup byte in ROM
sta PPU_DATA ; store byte to PPU data
@ -72,26 +68,35 @@ SetPalette: subroutine
NMIHandler:
; save registers
pha ; save A
SAVE_REGS
; update scroll position (must be done after VRAM updates)
inc ScrollPos
lda ScrollPos
sta PPU_SCROLL ; horiz byte
lda #0
sta PPU_SCROLL ; vert byte
; reload registers
pla ; reload A
inc ScrollPos ; increment low byte
bne .noinc ; Z flag set if wrapped to 0
inc ScrollPos+1 ; increment high byte
.noinc
; store X and Y scroll position
lda ScrollPos ; A -> low byte
sta PPU_SCROLL ; set horiz scroll
lda #0 ; A -> zero
sta PPU_SCROLL ; set vert scroll
; store 8th bit into name table selector
; name table A or B ($2000 or $2400)
lda ScrollPos+1 ; load high byte
and #1 ; select its low bit
ora #CTRL_NMI ; set rest of bits
sta PPU_CTRL
; restore registers, return from interrupt
RESTORE_REGS
rti
;;;;; CONSTANT DATA
align $100
Palette:
hex 1f ;background
hex 09092c00 ;bg0
hex 09091900 ;bg1
hex 09091500 ;bg2
hex 09092500 ;bg3
hex 1f ;screen color
hex 09092c00 ;background 0
hex 09091900 ;background 1
hex 09091500 ;background 2
hex 09092500 ;background 3
;;;;; CPU VECTORS

View File

@ -6,15 +6,16 @@
seg.u RAM
org $0
ScrollPos byte ; used during NMI
Rand byte
Temp1 byte
ScrollPos word ; used during NMI
Rand byte ; random number
Temp1 byte ; temporary
; OAM sprite buffer
SpriteBuf equ $200
;;;;; NES CARTRIDGE HEADER
NES_HEADER 0,2,1,0 ; mapper 0, 2 PRGs, 1 CHR, horiz. mirror
NES_HEADER 0,2,1,1 ; mapper 0, 2 PRGs, 1 CHR, horiz. mirror
;;;;; START OF CODE
@ -42,10 +43,7 @@ Start:
; fill video RAM
FillVRAM: subroutine
txa
ldy #$20
sty PPU_ADDR
sta PPU_ADDR
PPU_SETADDR $2000
ldy #$10
.loop:
stx PPU_DATA
@ -85,10 +83,7 @@ MoveSprites: subroutine
; set palette colors
SetPalette: subroutine
ldy #$00
lda #$3f
sta PPU_ADDR
sty PPU_ADDR
PPU_SETADDR $3f00
ldx #32
.loop:
lda Palette,y
@ -106,35 +101,45 @@ SetPalette: subroutine
;;;;; INTERRUPT HANDLERS
NMIHandler:
; save registers
SAVE_REGS
; load sprites
lda #$02
sta PPU_OAM_DMA
; update scroll position (must be done after VRAM updates)
inc ScrollPos
lda ScrollPos
sta PPU_SCROLL
lda #0
sta PPU_SCROLL
inc ScrollPos ; increment low byte
bne .noinc ; Z flag set if wrapped to 0
inc ScrollPos+1 ; increment high byte
.noinc
; store X and Y scroll position
lda ScrollPos ; A -> low byte
sta PPU_SCROLL ; set horiz scroll
lda #0 ; A -> zero
sta PPU_SCROLL ; set vert scroll
; store 8th bit into name table selector
; name table A or B ($2000 or $2400)
lda ScrollPos+1 ; load high byte
and #1 ; select its low bit
ora #CTRL_NMI ; set rest of bits
sta PPU_CTRL
; load sprites
lda #$02 ; page 2 ($0200)
sta PPU_OAM_DMA ; start DMA transfer
; move sprites
jsr MoveSprites
; reload registers
jsr MoveSprites ; move sprites
; restore registers, return from interrupt
RESTORE_REGS
rti
;;;;; CONSTANT DATA
align $100
Palette:
hex 1f ;background
hex 09090000 ;bg0
hex 09090c00 ;bg1
hex 09091c00 ;bg2
hex 09092c00 ;bg3
hex 14243400 ;sp0
hex 15253500 ;sp1
hex 16263600 ;sp2
hex 17273700 ;sp3
hex 1f ;screen color
hex 09092c00 ;background 0
hex 09091900 ;background 1
hex 09091500 ;background 2
hex 09092500 ;background 3
hex 14243400 ;sprite 0
hex 15253500 ;sprite 1
hex 16263600 ;sprite 2
hex 17273700 ;sprite 3
;;;;; CPU VECTORS

View File

@ -38,10 +38,7 @@ Start:
; fill video RAM
FillVRAM: subroutine
txa
ldy #$20
sty PPU_ADDR
sta PPU_ADDR
PPU_SETADDR $2000
ldy #$10
.loop:
stx PPU_DATA
@ -54,10 +51,7 @@ FillVRAM: subroutine
; set palette colors
SetPalette: subroutine
ldy #$00
lda #$3f
sta PPU_ADDR
sty PPU_ADDR
PPU_SETADDR $3f00
ldx #32
.loop:
lda Palette,y

View File

@ -119,10 +119,10 @@ NES_MIRR_QUAD = 8
;;;;; PPU_SETADDR <address> - set 16-bit PPU address
MAC PPU_SETADDR
lda #>{1}
ldy #<{1}
lda #>{1} ; upper byte
sta PPU_ADDR
lda #<{1} ; lower byte
sta PPU_ADDR
sty PPU_ADDR
ENDM
;;;;; PPU_SETVALUE <value> - feed 8-bit value to PPU

View File

@ -5,6 +5,9 @@
#include "neslib.h"
#define NES_MAPPER 2 // mapper 2 (UxROM mapper)
#define NES_CHR_BANKS 0 // CHR RAM
// APU (sound) support
#include "apu.h"
//#link "apu.c"
@ -348,20 +351,15 @@ const int SINTBL2[32] = {
-127*2, -125*2, -117*2, -106*2, -90*2, -71*2, -49*2, -25*2,
};
signed char isin(byte dir) {
return SINTBL[dir & 31];
}
signed char icos(byte dir) {
return isin(dir+8);
}
#define ISIN(x) (SINTBL2[(x) & 31])
#define ICOS(x) ISIN(x+8)
// Fast 8-bit table lookup macro
// dest: destination
// ident: table identifier
// index: 8-bit index
#define FASTLUT8(dest,ident,index) \
__AX__ = (index); \
__A__ = (index); \
asm ("tax"); \
asm ("lda %v,x", ident); \
(dest) = __AX__;
@ -371,7 +369,7 @@ signed char icos(byte dir) {
// ident: table identifier
// index: 8-bit index
#define FASTLUT16(dest,ident,index) \
__AX__ = (index); \
__A__ = (index); \
asm ("asl"); \
asm ("tay"); \
asm ("lda %v+1,y", ident); \
@ -431,17 +429,22 @@ void return_attacker(register AttackingEnemy* a) {
}
}
#pragma bss-name (push,"ZEROPAGE")
#pragma data-name (push,"ZEROPAGE")
int sincos;
#pragma data-name(pop)
#pragma bss-name (pop)
void fly_attacker(register AttackingEnemy* a) {
byte dir = a->dir;
#if 1
int sincos;
sincos = FASTLUT16(sincos, SINTBL2, dir&31);
//register int sincos;
sincos = FASTLUT16(sincos, SINTBL2, a->dir&31);
a->x += sincos;
sincos = FASTLUT16(sincos, SINTBL2, (dir+8)&31);
sincos = FASTLUT16(sincos, SINTBL2, (a->dir+8)&31);
a->y += sincos;
#else
a->x += isin(dir) * 2;
a->y += icos(dir) * 2;
a->x += ISIN(a->dir);
a->y += ICOS(a->dir);
#endif
if ((a->y >> 8) == 0) {
a->returning = 1;
@ -689,8 +692,8 @@ void set_sounds() {
APU_ENABLE(enable);
}
static char starx[32];
static byte starofs;
static char starx[32]; // array of star X coords
static byte starofs; // Y offset of all stars
void init_stars() {
byte i;