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

nes: added attributes example

This commit is contained in:
Steven Hugg 2019-05-16 10:08:01 -04:00
parent e3a3016f9d
commit 3e2a3ddee7
2 changed files with 26 additions and 81 deletions

View File

@ -8,97 +8,41 @@
/*{pal:"nes",layout:"nes"}*/
const char PALETTE[16] = {
0x03, // background color
0x03, // screen color
0x25,0x30,0x27,0x00, // ladders and pickups
0x1C,0x20,0x2C,0x00, // floor blocks
0x00,0x10,0x20,0x00,
0x06,0x16,0x26
0x11,0x30,0x27,0x0, // background palette 0
0x1c,0x20,0x2c,0x0, // background palette 1
0x00,0x10,0x20,0x0, // background palette 2
0x06,0x16,0x26 // background palette 3
};
// convert nametable address to attribute address
unsigned int nt2attraddr(unsigned int a) {
return (a & 0x2c00) | 0x3c0 | (a & 0x0C00) |
((a >> 4) & 0x38) | ((a >> 2) & 0x07);
}
#define ATTRADR_A(x,y) (NAMETABLE_A|0x3c0|((((y)>>2)<<3)|((x)>>2)))
void put_pixel(unsigned char px, unsigned char py, char color) {
int ntaddr, attraddr;
char oldch, newch;
char oldattr, newattr;
// check bounds
if (px >= 32*2 || py >= 30*2)
return;
// compute nametable address
ntaddr = NTADR_A(px>>1, py>>1);
// compute attribute address
attraddr = nt2attraddr(ntaddr);
//attraddr = ATTRADR_A(px>>1, py>>1);
// compute new character mask
newch = 0x80 | (px & 3) | ((py & 3) << 2);
// compute new attribute mask
newattr = color;
if (px & 4) newattr <<= 2;
if (py & 4) newattr <<= 4;
// wait for vsync
ppu_wait_frame();
// read old character
vram_adr(ntaddr);
vram_read(&oldch, 1);
vram_adr(attraddr);
vram_read(&oldattr, 1);
// write new character
vram_adr(ntaddr);
vram_put(oldch | newch);
// write new attribute entry
vram_adr(attraddr);
vram_put(oldattr | newattr);
}
void color_demo(void) {
while (1) {
char x = rand() & 0x3f;
char y = rand() & 0x3f;
char color = rand() & 3;
put_pixel(x, y, color);
// reset scroll position
vram_adr(0);
}
}
// draw letters
void color_demo1(void) {
char ch;
unsigned int ntaddr,attraddr;
while (1) {
ch = rand8();
// use rand() for betterness
ntaddr = 0x2000 | (rand() & 0x1ff);
attraddr = nt2attraddr(ntaddr);
ppu_wait_frame();
// put character into attrib table
vram_adr(attraddr);
vram_put(0x55);
// put character into name table
vram_adr(ntaddr);
vram_inc(rand8() & 1);
vram_put(ch);
vram_put(ch);
// reset scroll position
vram_adr(0);
}
}
// attribute table in PRG ROM
const char ATTRIBUTE_TABLE[0x40] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // rows 0-3
0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, // rows 4-7
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, // rows 8-11
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // rows 12-15
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, // rows 16-19
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, // rows 20-23
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, // rows 24-27
0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f // rows 28-29
};
// main function, run after console reset
void main(void) {
// set palette colors
// set background palette colors
pal_bg(PALETTE);
// fill nametable with diamonds
vram_adr(NAMETABLE_A);
vram_fill(0x16, 32*30);
// copy attribute table from PRG ROM to VRAM
vram_write(ATTRIBUTE_TABLE, sizeof(ATTRIBUTE_TABLE));
// enable PPU rendering (turn on screen)
ppu_on_all();
// infinite loop
color_demo();
while (1) { }
}

View File

@ -11,6 +11,7 @@ declare var jsnes : any;
const JSNES_PRESETS = [
{id:'hello.c', name:'Hello World'},
{id:'scroll.c', name:'Scrolling'},
{id:'attributes.c', name:'Attribute Table'},
{id:'vrambuffer.c', name:'VRAM Buffer'},
{id:'sprites.c', name:'Sprites'},
{id:'metasprites.c', name:'Metasprites'},