summaryrefslogtreecommitdiff
path: root/src/nes/nes.cc
blob: 84f330968e469a4a5299fac68c14fc277b8d99c5 (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
#include "nes/nes.hh"
#include "nes/mapper.hh"
#include "nes/display.hh"
#include "nes/util/rgb.hh"
#include <iostream>

namespace nes
{
	nes::nes(cartridge cartridge, display& display)
		: cartridge_{ std::move(cartridge) }
		, display_{ display }
		, mapper_{ mapper::select(cartridge_) }
		, ppu_{ cpu_, mapper_ }
		, cpu_{ ppu_, mapper_, controller_1_, controller_2_ }
	{
		if (!cartridge_.is_valid())
		{
			std::cerr << "Cartridge not valid!" << std::endl;
			std::abort();
		}

		display_.clear(rgb{ animation_progress_, animation_progress_, animation_progress_ });
		display_.switch_buffers();
	}

	auto nes::step(std::chrono::microseconds const delta) -> void
	{
		step(cycle_count::from_duration(delta));
	}

	auto nes::step(cycle_count const delta) -> void
	{
		current_cycles_ += delta;
		cpu_.step_to(current_cycles_);
		ppu_.step_to(current_cycles_);

		animation_progress_ -= 2;
		display_.clear(rgb{ animation_progress_, animation_progress_, animation_progress_ });
		display_.switch_buffers();
	}

	auto nes::snapshot() -> test::status
	{
		auto res = test::status{};
		res.sram = std::vector(cartridge_.get_ram(), cartridge_.get_ram() + cartridge_.get_ram_length());
		cpu_.snapshot(res);
		return res;
	}
} // namespace nes