mirror of
https://github.com/MoleskiCoder/EightBit.git
synced 2024-11-15 21:10:07 +00:00
759b4a9fa8
Signed-off-by: Adrian Conlon <adrian.conlon@gmail.com>
111 lines
3.0 KiB
C++
111 lines
3.0 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();
|
|
|
|
#define DECLARE_PIN_LEVEL_LOWER(name) \
|
|
virtual void lower ## name();
|
|
|
|
#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() { \
|
|
if (lowered( name ())) { \
|
|
Raising ## name.fire(EventArgs::empty()); \
|
|
raise( name ()); \
|
|
Raised ## name.fire(EventArgs::empty()); \
|
|
} \
|
|
}
|
|
|
|
#define DEFINE_PIN_LEVEL_LOWER(name, within) \
|
|
void EightBit:: within ::lower ## name() { \
|
|
if (raised( name ())) { \
|
|
Lowering ## name.fire(EventArgs::empty()); \
|
|
lower( name ()); \
|
|
Lowered ## name.fire(EventArgs::empty()); \
|
|
} \
|
|
}
|
|
|
|
#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 DEFINE_PIN_ACTIVATOR(name, on, off) \
|
|
template <class T> class _Activate ## name final { \
|
|
T& m_parent; \
|
|
public: \
|
|
_Activate ## name(T& parent) \
|
|
: m_parent(parent) { \
|
|
m_parent. on ## name(); \
|
|
} \
|
|
~_Activate ## name() { \
|
|
m_parent. off ## name(); \
|
|
} \
|
|
};
|
|
|
|
#define DEFINE_PIN_ACTIVATOR_LOW(name) \
|
|
DEFINE_PIN_ACTIVATOR(name, lower, raise)
|
|
|
|
#define DEFINE_PIN_ACTIVATOR_HIGH(name) \
|
|
DEFINE_PIN_ACTIVATOR(name, raise, lower)
|
|
|
|
#define DECLARE_PIN(name, visibility) \
|
|
public: \
|
|
DECLARE_PIN_SIGNALS(name) \
|
|
[[nodiscard]] PinLevel& name () 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
|
|
};
|
|
|
|
[[nodiscard]] static constexpr auto raised(const PinLevel line) { return line == PinLevel::High; }
|
|
static void raise(PinLevel& line) noexcept { line = PinLevel::High; }
|
|
[[nodiscard]] static constexpr auto lowered(const PinLevel line) { return line == PinLevel::Low; }
|
|
static void lower(PinLevel& line) noexcept { line = PinLevel::Low; }
|
|
|
|
static void match(PinLevel& line, int condition) noexcept { match(line, condition != 0); }
|
|
static void match(PinLevel& line, bool condition) noexcept { condition ? raise(line) : lower(line); }
|
|
static void match(PinLevel& out, PinLevel in) noexcept { out = in; }
|
|
|
|
virtual ~Device() = default;
|
|
|
|
[[nodiscard]] bool powered() noexcept { return raised(POWER()); }
|
|
|
|
DECLARE_PIN_INPUT(POWER)
|
|
|
|
protected:
|
|
Device() noexcept = default;
|
|
};
|
|
}
|