summaryrefslogtreecommitdiff
path: root/src/nes/util/address.hh
blob: 85a00abda668df760dc108108f1edd293403abaa (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
#pragma once

#include <cstdint>

namespace nes
{
	/// An address on the NES.
	class address
	{
		std::uint16_t absolute_{ 0 };

	public:
		/// Create a zero address.
		constexpr explicit address() = default;

		/// Create an address from its absolute value.
		constexpr explicit address(std::uint16_t const absolute)
			: absolute_{ absolute }
		{
		}

		/// Create an address from its page and offset combination.
		constexpr explicit address(std::uint8_t const page, std::uint8_t const offset)
			: address{ static_cast<std::uint16_t>((page << 8) | (offset << 0)) }
		{
		}

		/// Get the absolute address value.
		constexpr auto get_absolute() const -> std::uint16_t
		{
			return absolute_;
		}

		/// Get the address page (the first 8 bits).
		constexpr auto get_page() const -> std::uint8_t
		{
			return static_cast<std::uint8_t>((get_absolute() & 0xFF00) >> 8);
		}

		/// Get the offset within the page (the last 8 bits).
		constexpr auto get_offset() const -> std::uint8_t
		{
			return static_cast<std::uint8_t>((get_absolute() & 0x00FF) >> 0);
		}
	};

	constexpr auto operator==(address const a, address const b) -> bool { return a.get_absolute() == b.get_absolute(); }
	constexpr auto operator!=(address const a, address const b) -> bool { return a.get_absolute() != b.get_absolute(); }
	constexpr auto operator<(address const a, address const b) -> bool { return a.get_absolute() < b.get_absolute(); }
	constexpr auto operator>(address const a, address const b) -> bool { return a.get_absolute() > b.get_absolute(); }
	constexpr auto operator<=(address const a, address const b) -> bool { return a.get_absolute() <= b.get_absolute(); }
	constexpr auto operator>=(address const a, address const b) -> bool { return a.get_absolute() >= b.get_absolute(); }

	constexpr auto operator+(address const a, std::uint16_t const b) -> address
	{
		return address{ static_cast<std::uint16_t>(a.get_absolute() + b) };
	}

	constexpr auto operator+(std::uint16_t const a, address const b) -> address
	{
		return address{ static_cast<std::uint16_t>(a + b.get_absolute()) };
	}

	constexpr auto operator-(address const a, std::uint16_t const b) -> address
	{
		return address{ static_cast<std::uint16_t>(a.get_absolute() - b) };
	}

	constexpr auto operator%(address const a, std::uint16_t b) -> address
	{
		return address{ static_cast<std::uint16_t>(a.get_absolute() % b) };
	}
} // namespace nes