mirror of
https://github.com/MoleskiCoder/EightBit.git
synced 2026-04-26 21:18:36 +00:00
101 lines
2.9 KiB
C++
101 lines
2.9 KiB
C++
#pragma once
|
|
|
|
#include "EventArgs.h"
|
|
#include "Signal.h"
|
|
|
|
#define DECLARE_PIN_SIGNALS(name) \
|
|
Signal<EventArgs> Raising ## name; \
|
|
Signal<EventArgs> Raised ## name; \
|
|
Signal<EventArgs> Lowering ## name; \
|
|
Signal<EventArgs> Lowered ## name;
|
|
|
|
#define DECLARE_PIN_LEVEL_RAISE(name) \
|
|
virtual void raise ## name() noexcept;
|
|
|
|
#define DECLARE_PIN_LEVEL_LOWER(name) \
|
|
virtual void lower ## name() noexcept;
|
|
|
|
#define DECLARE_PIN_LEVEL_CHANGERS(name) \
|
|
DECLARE_PIN_LEVEL_RAISE(name) \
|
|
DECLARE_PIN_LEVEL_LOWER(name)
|
|
|
|
#define DEFINE_PIN_LEVEL_RAISE(name, within) \
|
|
void EightBit:: within ::raise ## name() noexcept { \
|
|
if (lowered( name ())) { \
|
|
Raising ## name.fire(); \
|
|
raise( name ()); \
|
|
Raised ## name.fire(); \
|
|
} \
|
|
}
|
|
|
|
#define DEFINE_PIN_LEVEL_LOWER(name, within) \
|
|
void EightBit:: within ::lower ## name() noexcept { \
|
|
if (raised( name ())) { \
|
|
Lowering ## name.fire(); \
|
|
lower( name ()); \
|
|
Lowered ## name.fire(); \
|
|
} \
|
|
}
|
|
|
|
#define DEFINE_PIN_LEVEL_CHANGERS(name, within) \
|
|
DEFINE_PIN_LEVEL_RAISE(name, within) \
|
|
DEFINE_PIN_LEVEL_LOWER(name, within)
|
|
|
|
#define DECLARE_PIN_MEMBER(name) \
|
|
PinLevel m_## name ## _Line = PinLevel::Low;
|
|
|
|
#define DECLARE_PIN(name, visibility) \
|
|
public: \
|
|
DECLARE_PIN_SIGNALS(name) \
|
|
[[nodiscard]] constexpr auto& name () noexcept { \
|
|
return m_## name ## _Line; \
|
|
} \
|
|
[[nodiscard]] constexpr const auto& name () const noexcept { \
|
|
return m_## name ## _Line; \
|
|
} \
|
|
visibility : \
|
|
DECLARE_PIN_LEVEL_CHANGERS(name) \
|
|
private: \
|
|
DECLARE_PIN_MEMBER(name)
|
|
|
|
// Input pins may be external controlled
|
|
#define DECLARE_PIN_INPUT(name) \
|
|
DECLARE_PIN(name, public)
|
|
|
|
// Output pins can only be internally controlled
|
|
#define DECLARE_PIN_OUTPUT(name) \
|
|
DECLARE_PIN(name, protected)
|
|
|
|
namespace EightBit {
|
|
class Device {
|
|
public:
|
|
enum class PinLevel {
|
|
Low, High
|
|
};
|
|
|
|
DECLARE_PIN_INPUT(POWER)
|
|
|
|
public:
|
|
[[nodiscard]] static constexpr auto raised(const PinLevel line) noexcept { return line == PinLevel::High; }
|
|
static constexpr void raise(PinLevel& line) noexcept { match(line, PinLevel::High); }
|
|
[[nodiscard]] static constexpr auto lowered(const PinLevel line) noexcept { return line == PinLevel::Low; }
|
|
static constexpr void lower(PinLevel& line) noexcept { match(line, PinLevel::Low); }
|
|
|
|
static constexpr void match(PinLevel& line, int condition) noexcept { match(line, condition != 0); }
|
|
static constexpr void match(PinLevel& line, bool condition) noexcept { condition ? raise(line) : lower(line); }
|
|
static constexpr void match(PinLevel& out, PinLevel in) noexcept { out = in; }
|
|
|
|
static constexpr void flip(PinLevel& out) noexcept { match(out, out == PinLevel::Low ? PinLevel::High : PinLevel::Low); }
|
|
|
|
virtual ~Device() noexcept = default;
|
|
|
|
Device(const Device& rhs) noexcept;
|
|
bool operator==(const Device& rhs) const noexcept;
|
|
|
|
[[nodiscard]] constexpr bool powered() const noexcept { return raised(POWER()); }
|
|
|
|
protected:
|
|
Device() noexcept = default;
|
|
};
|
|
}
|