summaryrefslogtreecommitdiff
path: root/src/nes/ppu.hh
blob: 1d086142b3b4c62990f1623ec0fa64a9b17ccf3f (plain)
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
#pragma once

#include "nes/util/cycle_count.hh"

namespace nes
{
	class address;
	class cpu;
	class mapper;
	class rgb;

	class ppu
	{
		static constexpr auto vram_size = std::size_t{ 0x800 };
		static constexpr auto oam_size = std::size_t{ 0x100 };
		static constexpr auto palette_buffer_size = std::size_t{ 0x20 };
		static constexpr auto status_mask = std::uint8_t{ 0b11100000 };
		// Writes to some registers are ignored until this clock cycle.
		static constexpr auto boot_up_cycles = cycle_count::from_ppu(29658);

		enum class vram_increment
		{
			add_1_across = 0,
			add_32_down = 1,
		};

		enum class sprite_size
		{
			_8x8 = 0,
			_8x16 = 1,
		};

		enum class role
		{
			background = 0,
			sprite = 1,
		};

		enum class color : std::uint8_t
		{
		};

		union color_index
		{
			struct
			{
				unsigned color   : 2;
				unsigned palette : 2;
				role     role    : 1;
			};
			unsigned value;

			explicit color_index(unsigned const value)
				: value{ value }
			{
			}
		};

		cycle_count current_cycles_;
		cpu& cpu_;
		mapper& mapper_;
		std::uint8_t vram_[vram_size]{};
		std::uint8_t oam_[oam_size]{};
		color palette_buffer_[palette_buffer_size]
		{
			color{ 0x09 }, color{ 0x01 }, color{ 0x00 }, color{ 0x01 },
			color{ 0x00 }, color{ 0x02 }, color{ 0x02 }, color{ 0x0D },
			color{ 0x08 }, color{ 0x10 }, color{ 0x08 }, color{ 0x24 },
			color{ 0x00 }, color{ 0x00 }, color{ 0x04 }, color{ 0x2C },
			color{ 0x09 }, color{ 0x01 }, color{ 0x34 }, color{ 0x03 },
			color{ 0x00 }, color{ 0x04 }, color{ 0x00 }, color{ 0x14 },
			color{ 0x08 }, color{ 0x3A }, color{ 0x00 }, color{ 0x02 },
			color{ 0x00 }, color{ 0x20 }, color{ 0x2C }, color{ 0x08 },
		};
		std::uint8_t latch_{}; // The last read/written IO register value, returned when reading write-only registers.
		union
		{
			struct
			{
				bool                 : 1;
				bool                 : 1;
				bool                 : 1;
				bool                 : 1;
				bool                 : 1;
				bool sprite_overflow : 1;
				bool sprite_0_hit    : 1;
				bool vblank          : 1;
			};
			std::uint8_t value{ 0b00000000 };
		} status_{};
		union
		{
			struct
			{
				std::uint8_t   base_name_table          : 2; // Base nametable address.
				vram_increment vram_increment           : 1; // VRAM increment per CPU read/write of PPUDATA.
				std::uint8_t   sprite_pattern_table     : 1; // Sprite pattern table address for 8x8 sprites.
				std::uint8_t   background_pattern_table : 1; // Background pattern table address.
				sprite_size    sprite_size              : 1; // Sprite size.
				bool           enable_ext_pin           : 1; // PPU master/slave select.
				bool           vblank_nmi               : 1; // Enable/disable Vblank NMI
			};
			std::uint8_t value{ 0b00000000 };
		} control_{};
		union
		{
			struct
			{
				bool grayscale             : 1; // Enable/disable grayscale.
				bool show_background_start : 1; // Show background in leftmost 8 pixels of screen.
				bool show_sprites_start    : 1; // Show sprites in leftmost 8 pixels of screen.
				bool enable_background     : 1; // Enable background rendering.
				bool enable_sprites        : 1; // Enable sprite rendering.
				bool emphasize_red         : 1; // Emphasize red.
				bool emphasize_green       : 1; // Emphasize green.
				bool emphasize_blue        : 1; // Emphasize blue.
			};
			std::uint8_t value{ 0b00000000 };
		} mask_{};
		struct
		{
			// See https://www.nesdev.org/wiki/PPU_scrolling#PPU_internal_registers
			std::uint16_t v{}; // Current VRAM address (15 bits)
			std::uint16_t t{}; // Temporary VRAM address (15 bits)
			std::uint8_t  x{}; // Fine X scroll (3 bits)
			bool          w{}; // Second write toggle (1 bit)
		} internal_{};
		std::uint8_t oamaddr_{}; // Address accessed by IO registers
		std::uint8_t ppudata_read_buffer_{}; // Delays PPUDATA reads by one.

	public:
		explicit ppu(cpu& cpu, mapper& mapper);

		ppu(ppu const&) = delete;
		ppu(ppu&&) = delete;
		auto operator=(ppu const&) -> ppu& = delete;
		auto operator=(ppu&&) -> ppu& = delete;

		auto step_to(cycle_count) -> void;

		// Memory access

		auto read8(address) -> std::uint8_t;
		auto write8(address, std::uint8_t) -> void;

		// IO registers

		auto read_latch() -> std::uint8_t;
		auto read_ppustatus() -> std::uint8_t;
		auto read_oamdata() -> std::uint8_t;
		auto read_ppudata() -> std::uint8_t;

		auto write_latch(std::uint8_t) -> void;
		auto write_ppuctrl(std::uint8_t) -> void;
		auto write_ppuscroll(std::uint8_t) -> void;
		auto write_ppumask(std::uint8_t) -> void;
		auto write_ppuaddr(std::uint8_t) -> void;
		auto write_ppudata(std::uint8_t) -> void;
		auto write_oamaddr(std::uint8_t) -> void;
		auto write_oamdata(std::uint8_t) -> void;
		auto write_oamdma(std::uint8_t) -> void;

	private:
		auto step() -> void;

		// Helpers

		auto increment_vram() -> void;
		auto get_color(color_index) -> color&;
		auto resolve_color(color) -> rgb;
	};
} // namespace nes