2017-09-06 12:22:23 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <cstdint>
|
2018-09-16 11:00:29 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <iomanip>
|
2017-09-06 12:22:23 +00:00
|
|
|
|
|
|
|
#ifdef __BYTE_ORDER__
|
|
|
|
# if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
|
|
|
|
# define HOST_LITTLE_ENDIAN
|
|
|
|
# endif
|
|
|
|
# if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
|
|
|
|
# define HOST_BIG_ENDIAN
|
|
|
|
# endif
|
|
|
|
#else
|
|
|
|
# if defined(_M_X64) || defined(_M_IX86)
|
|
|
|
# define HOST_LITTLE_ENDIAN
|
|
|
|
# else
|
|
|
|
# define HOST_BIG_ENDIAN
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace EightBit {
|
2018-11-25 10:43:51 +00:00
|
|
|
union register16_t final {
|
2017-09-06 12:22:23 +00:00
|
|
|
struct {
|
|
|
|
#ifdef HOST_LITTLE_ENDIAN
|
|
|
|
uint8_t low;
|
|
|
|
uint8_t high;
|
|
|
|
#endif
|
|
|
|
#ifdef HOST_BIG_ENDIAN
|
|
|
|
uint8_t high;
|
|
|
|
uint8_t low;
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
uint16_t word;
|
2018-11-24 10:56:36 +00:00
|
|
|
|
2018-08-07 22:06:15 +00:00
|
|
|
register16_t() noexcept : word(0) {}
|
2018-09-20 23:16:00 +00:00
|
|
|
register16_t(const uint16_t w) noexcept : word(w) {}
|
|
|
|
register16_t(const uint8_t l, const uint8_t h) noexcept : low(l), high(h) {}
|
2018-08-11 20:19:19 +00:00
|
|
|
|
2018-10-27 16:30:23 +00:00
|
|
|
auto& operator++() noexcept {
|
2018-08-11 20:19:19 +00:00
|
|
|
++word;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2018-10-27 16:30:23 +00:00
|
|
|
auto& operator--() noexcept {
|
2018-08-11 20:19:19 +00:00
|
|
|
--word;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2018-10-27 16:30:23 +00:00
|
|
|
auto operator++(int) noexcept {
|
2018-08-11 20:19:19 +00:00
|
|
|
register16_t temporary(*this);
|
|
|
|
operator++();
|
|
|
|
return temporary;
|
|
|
|
}
|
|
|
|
|
2018-10-27 16:30:23 +00:00
|
|
|
auto operator--(int) noexcept {
|
2018-08-11 20:19:19 +00:00
|
|
|
register16_t temporary(*this);
|
|
|
|
operator--();
|
|
|
|
return temporary;
|
|
|
|
}
|
|
|
|
|
2018-10-27 16:30:23 +00:00
|
|
|
auto& operator+=(const register16_t rhs) noexcept {
|
2018-08-11 20:19:19 +00:00
|
|
|
this->word += rhs.word;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2018-10-27 16:30:23 +00:00
|
|
|
auto& operator-=(const register16_t rhs) noexcept {
|
2018-08-11 20:19:19 +00:00
|
|
|
this->word -= rhs.word;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-10-27 16:30:23 +00:00
|
|
|
inline auto operator==(const register16_t lhs, const register16_t rhs) noexcept {
|
2018-09-16 11:00:29 +00:00
|
|
|
return lhs.word == rhs.word;
|
|
|
|
}
|
|
|
|
|
2018-10-27 16:30:23 +00:00
|
|
|
inline auto operator!=(const register16_t lhs, const register16_t rhs) noexcept {
|
2018-09-16 11:00:29 +00:00
|
|
|
return !(lhs == rhs);
|
|
|
|
}
|
|
|
|
|
2018-10-27 16:30:23 +00:00
|
|
|
inline auto operator+(register16_t lhs, const register16_t rhs) noexcept {
|
2018-08-11 20:19:19 +00:00
|
|
|
lhs += rhs;
|
|
|
|
return lhs;
|
|
|
|
}
|
|
|
|
|
2018-10-27 16:30:23 +00:00
|
|
|
inline auto operator-(register16_t lhs, const register16_t rhs) noexcept {
|
2018-08-11 20:19:19 +00:00
|
|
|
lhs -= rhs;
|
|
|
|
return lhs;
|
|
|
|
}
|
2018-09-16 11:00:29 +00:00
|
|
|
|
2018-10-27 16:30:23 +00:00
|
|
|
inline auto& operator<<(std::ostream& output, const register16_t value) {
|
2018-09-16 11:00:29 +00:00
|
|
|
return output << std::hex << std::setw(4) << std::setfill('0') << value.word;
|
|
|
|
}
|
2017-11-30 23:19:17 +00:00
|
|
|
}
|