-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmapper0.c
More file actions
92 lines (85 loc) · 3.15 KB
/
Copy pathmapper0.c
File metadata and controls
92 lines (85 loc) · 3.15 KB
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
#include "mapper0.h"
#include <stdint.h>
static nes_header header;
static uint8_t *prg_rom;
static uint8_t *chr_rom;
static uint8_t nametables[2][0x400] ; // 2KiB of internal ram
//implements mapper0 ( NROM )
void init_mapper0 ( nes_header header0 , reader *mapper_cpu_read , reader *mapper_ppu_read , writer *mapper_cpu_write,
writer *mapper_ppu_write )
{
header = header0 ;
prg_rom = header0.prg_mem ;
chr_rom = header0.chr_mem ;
// assign the mapper0 function to the used mapping function ( set mapper0 )
*mapper_cpu_read = mapper0_cpu_read;
*mapper_cpu_write = mapper0_cpu_write ;
*mapper_ppu_read = mapper0_ppu_read ;
*mapper_ppu_write = mapper0_ppu_write ;
}
uint8_t mapper0_ppu_read ( uint16_t address )
{
if ( address >= 0 && address <= 0x1FFF ) // pattern tables
{
return chr_rom[address];
}
else if ( address >= 0x2000 && address <= 0x3EFF ) // nametables and mirrors
{
uint16_t temp = address & 0x0FFF ;
if ( header.mirroring ) // if vertical
{
if ( temp >= 0x800 && temp <= 0xFFF ) // access from 2 or 3
temp -= 0x800 ; // mirror to 0 and 1
return nametables[(temp & 0x0400) >> 10][temp & 0x03FF ] ; // TODO : check here
}
else // horizontal
{
if ( temp >= 0x000 && temp <= 0x7FF ) // access from 0 or 1
return nametables[0][temp & 0x3FF] ; // mirror 0 and 1
else
return nametables[1][ temp & 0xBFF -0x800 ] ; // mirror 2 and 3
}
}
}
void mapper0_ppu_write( uint16_t address , uint8_t data )
{
if ( address >= 0 && address <= 0x1FFF ) // pattern tables
chr_rom[address] = data;
else if ( address >= 0x2000 && address <= 0x3EFF ) // nametables and mirrors
{
uint16_t temp = address & 0x0FFF ;
if ( header.mirroring ) // if vertical
{
if ( temp >= 0x800 && temp <= 0xFFF ) // access from 2 or 3
temp -= 0x800 ; // mirror to 0 and 1
nametables[(temp & 0x0400) >> 10][temp & 0x03FF ] = data ; // TODO : check here
}
else // horizontal
{
if ( temp >= 0x000 && temp <= 0x7FF ) // access from 0 or 1
nametables[0][temp & 0x3FF] = data; // mirror 0 and 1
else
nametables[1][ (temp & 0xBFF) -0x800] = data; // mirror 2 and 3
}
}
}
void mapper0_cpu_write( uint16_t address , uint8_t data )
{
return ; // this is all ROM
}
uint8_t mapper0_cpu_read( uint16_t address )
{
// we are in 0x4020 to 0xFFFF which is Cartridge space
// 1 prg rom means NROM-128 , 2 for NROM-256 || 1: $8000-$BFFF 2:$C000-$FFFF or mirror of 1
if ( address >= 0x8000 && address <= 0xBFFF )
return prg_rom[address - 0x8000 ] ;
else if ( address >= 0xC000 && address <= 0xFFFF )
{ // see if we need to mirror
if ( header.prg_rom_count == 2 ) // NROM-256
{
return prg_rom[address - 0x8000] ;
}
return prg_rom[address -0xC000 ] ; //mirror
}
return 0 ;
}