-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathVideo.cpp
More file actions
95 lines (74 loc) · 1.78 KB
/
Video.cpp
File metadata and controls
95 lines (74 loc) · 1.78 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
93
94
95
#include "Video.h"
#include <assert.h>
#include <cstring>
#include "Cartridge.h"
#include "Memory.h"
Video::Video( Cartridge *cartridge )
: cartridge( cartridge )
, ppuCycles( 0u )
, currentScanline( 0u )
{
map = new byte[ 16_KB ];
frameBuffer = new RGB[ NES_VIDEO_RESOLUTION ];
Reset();
}
Video::~Video()
{
delete[] map;
delete[] frameBuffer;
}
void Video::Init( Memory *memorySystem )
{
memory = memorySystem;
}
void Video::Reset()
{
memset( map, 0x00, 16_KB );
for ( u32 i = 0; i < NES_VIDEO_RESOLUTION; ++i )
{
frameBuffer[ i ] = color::PINK;
}
MapCartridgeCHRToPPU();
}
void Video::MapCartridgeCHRToPPU()
{
assert( cartridge != nullptr );
/* TODO(Jonathan): Support more mappers here */
const Cartridge::Header &header = cartridge->GetHeader();
const byte * const cartridgeRom = cartridge->GetRom();
const u64 offset = (header.prgRomSizeKB * 1_KB) + 0x0010;
const u64 dataSize = header.chrRomSizeKB * 1_KB;
memcpy( map, &cartridgeRom[ offset ], dataSize );
}
RGB* Video::GetFrameBuffer() const
{
return frameBuffer;
}
const byte * const Video::GetPPUMemory() const
{
return map;
}
byte Video::Read( word address ) const
{
return map[ address ];
}
void Video::Write( word address, byte data )
{
map[ address ] = data;
}
void Video::Update( u32 cycles )
{
// 1 CPU Cycles = 3 PPU cycle
ppuCycles = cycles * 3;
currentScanline = ppuCycles / CYCLE_DURATION_PER_SCANLINE;
if ( currentScanline == VBLANK_SCANLINE )
{
memory->Write( PPUSTATUS_REGISTER, memory->Read( PPUSTATUS_REGISTER ) & 0b1000'0000 );
}
if ( currentScanline == MAX_SCANLINES_PER_FRAME )
{
// Reset everything
ppuCycles = 0u;
currentScanline = 0u;
}
}