summaryrefslogtreecommitdiff
path: root/app/test/main.cc
blob: 3bb785f63b1bd305c21875d58555c76f4883cf65 (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
#include "nes/display.hh"
#include "nes/nes.hh"
#include "nes/util/rgb.hh"
#include "nes/util/snapshot.hh"
#include <iostream>

namespace
{
	class dummy_display final : public nes::display
	{
		std::uint8_t buffer_a_[width * height * 3]{};
		std::uint8_t buffer_b_[width * height * 3]{};
		std::uint8_t* front_buffer_{ buffer_a_ };
		std::uint8_t* back_buffer_{ buffer_b_ };

	public:
		explicit dummy_display() = default;

		auto get(unsigned const x, unsigned const y) const -> nes::rgb override
		{
			auto const offset = ((y * width) + x) * 3;
			return nes::rgb{ back_buffer_[offset + 0], back_buffer_[offset + 1], back_buffer_[offset + 2] };
		}

		auto set(unsigned const x, unsigned const y, nes::rgb const value) -> void override
		{
			auto const offset = ((y * width) + x) * 3;
			back_buffer_[offset + 0] = value.r;
			back_buffer_[offset + 1] = value.g;
			back_buffer_[offset + 2] = value.b;
		}

		auto switch_buffers() -> void override
		{
			std::swap(front_buffer_, back_buffer_);
		}
	};
} // namespace

int main()
{
	auto const path = "/Users/hannes/Documents/temp/supermario.nes";

	auto cartridge = nes::cartridge::from_file(path);
	if (!cartridge.is_valid())
	{
		std::cerr << "Could not load cartridge!" << std::endl;
		std::abort();
	}

	// Initialize the system to be tested.
	auto display_a = std::make_unique<dummy_display>();
	auto sys_a = nes::nes{ cartridge, *display_a };

	// Initialize the (assumed to be working) reference implementation.
	auto display_b = std::make_unique<dummy_display>();
	auto sys_b = nes::nes{ cartridge, *display_b };

	// Run.
	while (true)
	{
		auto const status_a = sys_a.get_snapshot();
		auto const status_b = sys_b.get_snapshot();

		if (status_a != status_b)
		{
			std::cout << "Different snapshot!\n";
			return 1;
		}

		std::cout << "Cycle: " << status_a.cpu_cycle.to_cpu() << ", " << status_b.cpu_cycle.to_cpu() << '\n';

		sys_a.step_to_nmi();
		sys_b.step_to_nmi();
	}
}