Repository navigation
Expand file tree
/
Copy patholc2C02.h
More file actions
203 lines (162 loc) · 5.24 KB
/
Copy patholc2C02.h
File metadata and controls
203 lines (162 loc) · 5.24 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
olc::NES - Picture Processing Unit (PPU) 2C02
"Thanks Dad for believing computers were gonna be a big deal..." - javidx9
License (OLC-3)
~~~~~~~~~~~~~~~
Copyright 2018-2019 OneLoneCoder.com
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions or derivations of source code must retain the above
copyright notice, this list of conditions and the following disclaimer.
2. Redistributions or derivative works in binary form must reproduce
the above copyright notice. This list of conditions and the following
disclaimer must be reproduced in the documentation and/or other
materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Relevant Video: https://youtu.be/xdzOvpYPmGE
Links
~~~~~
YouTube: https://www.youtube.com/javidx9
https://www.youtube.com/javidx9extra
Discord: https://discord.gg/WhwHUMV
Twitter: https://www.twitter.com/javidx9
Twitch: https://www.twitch.tv/javidx9
GitHub: https://www.github.com/onelonecoder
Patreon: https://www.patreon.com/javidx9
Homepage: https://www.onelonecoder.com
Author
~~~~~~
David Barr, aka javidx9, �OneLoneCoder 2019
*/
#pragma once
#include <cstdint>
#include <memory>
#include "olcPixelGameEngine.h"
#include "Cartridge.h"
class olc2C02
{
public:
olc2C02();
~olc2C02();
private:
uint8_t tblName[2][1024];
uint8_t tblPattern[2][4096];
uint8_t tblPalette[32];
private:
olc::Pixel palScreen[0x40];
olc::Sprite sprScreen = olc::Sprite(256, 240);
olc::Sprite sprNameTable[2] = { olc::Sprite(256, 240), olc::Sprite(256, 240) };
olc::Sprite sprPatternTable[2] = { olc::Sprite(128, 128), olc::Sprite(128, 128) };
public:
// Debugging Utilities
olc::Sprite& GetScreen();
olc::Sprite& GetNameTable(uint8_t i);
olc::Sprite& GetPatternTable(uint8_t i, uint8_t palette);
olc::Pixel& GetColourFromPaletteRam(uint8_t palette, uint8_t pixel);
bool frame_complete = false;
private:
union
{
struct
{
uint8_t unused : 5;
uint8_t sprite_overflow : 1;
uint8_t sprite_zero_hit : 1;
uint8_t vertical_blank : 1;
};
uint8_t reg;
} status;
union
{
struct
{
uint8_t grayscale : 1;
uint8_t render_background_left : 1;
uint8_t render_sprites_left : 1;
uint8_t render_background : 1;
uint8_t render_sprites : 1;
uint8_t enhance_red : 1;
uint8_t enhance_green : 1;
uint8_t enhance_blue : 1;
};
uint8_t reg;
} mask;
union PPUCTRL
{
struct
{
uint8_t nametable_x : 1;
uint8_t nametable_y : 1;
uint8_t increment_mode : 1;
uint8_t pattern_sprite : 1;
uint8_t pattern_background : 1;
uint8_t sprite_size : 1;
uint8_t slave_mode : 1; // unused
uint8_t enable_nmi : 1;
};
uint8_t reg;
} control;
union loopy_register
{
// Credit to Loopy for working this out :D
struct
{
uint16_t coarse_x : 5;
uint16_t coarse_y : 5;
uint16_t nametable_x : 1;
uint16_t nametable_y : 1;
uint16_t fine_y : 3;
uint16_t unused : 1;
};
uint16_t reg = 0x0000;
};
loopy_register vram_addr; // Active "pointer" address into nametable to extract background tile info
loopy_register tram_addr; // Temporary store of information to be "transferred" into "pointer" at various times
// Pixel offset horizontally
uint8_t fine_x = 0x00;
// Internal communications
uint8_t address_latch = 0x00;
uint8_t ppu_data_buffer = 0x00;
// Pixel "dot" position information
int16_t scanline = 0;
int16_t cycle = 0;
// Background rendering
uint8_t bg_next_tile_id = 0x00;
uint8_t bg_next_tile_attrib = 0x00;
uint8_t bg_next_tile_lsb = 0x00;
uint8_t bg_next_tile_msb = 0x00;
uint16_t bg_shifter_pattern_lo = 0x0000;
uint16_t bg_shifter_pattern_hi = 0x0000;
uint16_t bg_shifter_attrib_lo = 0x0000;
uint16_t bg_shifter_attrib_hi = 0x0000;
public:
// Communications with Main Bus
uint8_t cpuRead(uint16_t addr, bool rdonly = false);
void cpuWrite(uint16_t addr, uint8_t data);
// Communications with PPU Bus
uint8_t ppuRead(uint16_t addr, bool rdonly = false);
void ppuWrite(uint16_t addr, uint8_t data);
private:
// The Cartridge or "GamePak"
std::shared_ptr<Cartridge> cart;
public:
// Interface
void ConnectCartridge(const std::shared_ptr<Cartridge>& cartridge);
void clock();
void reset();
bool nmi = false;
};