2019-01-10 20:44:16 +00:00
|
|
|
#pragma once
|
|
|
|
|
2019-01-14 02:10:17 +00:00
|
|
|
#include "EventArgs.h"
|
|
|
|
#include "Signal.h"
|
|
|
|
|
2019-01-14 23:17:54 +00:00
|
|
|
#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) \
|
2022-01-17 19:10:15 +00:00
|
|
|
virtual void raise ## name() noexcept;
|
2019-01-14 23:17:54 +00:00
|
|
|
|
|
|
|
#define DECLARE_PIN_LEVEL_LOWER(name) \
|
2022-01-17 19:10:15 +00:00
|
|
|
virtual void lower ## name() noexcept;
|
2019-01-14 23:17:54 +00:00
|
|
|
|
|
|
|
#define DECLARE_PIN_LEVEL_CHANGERS(name) \
|
|
|
|
DECLARE_PIN_LEVEL_RAISE(name) \
|
|
|
|
DECLARE_PIN_LEVEL_LOWER(name)
|
|
|
|
|
|
|
|
#define DEFINE_PIN_LEVEL_RAISE(name, within) \
|
2022-01-17 19:10:15 +00:00
|
|
|
void EightBit:: within ::raise ## name() noexcept { \
|
2019-09-06 22:55:57 +00:00
|
|
|
if (lowered( name ())) { \
|
2021-01-08 09:06:46 +00:00
|
|
|
Raising ## name.fire(); \
|
2019-09-06 22:55:57 +00:00
|
|
|
raise( name ()); \
|
2021-01-08 09:06:46 +00:00
|
|
|
Raised ## name.fire(); \
|
2019-09-06 22:55:57 +00:00
|
|
|
} \
|
2019-01-14 23:17:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#define DEFINE_PIN_LEVEL_LOWER(name, within) \
|
2022-01-17 19:10:15 +00:00
|
|
|
void EightBit:: within ::lower ## name() noexcept { \
|
2019-09-06 22:55:57 +00:00
|
|
|
if (raised( name ())) { \
|
2021-01-08 09:06:46 +00:00
|
|
|
Lowering ## name.fire(); \
|
2019-09-06 22:55:57 +00:00
|
|
|
lower( name ()); \
|
2021-01-08 09:06:46 +00:00
|
|
|
Lowered ## name.fire(); \
|
2019-09-06 22:55:57 +00:00
|
|
|
} \
|
2019-01-14 23:17:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#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;
|
|
|
|
|
2020-05-03 19:45:01 +00:00
|
|
|
#define DEFINE_PIN_ACTIVATOR(name, on, off) \
|
|
|
|
template <class T> class _Activate ## name final { \
|
|
|
|
T& m_parent; \
|
|
|
|
public: \
|
2021-07-18 13:28:40 +00:00
|
|
|
_Activate ## name(T& parent) noexcept \
|
2020-05-03 19:45:01 +00:00
|
|
|
: m_parent(parent) { \
|
|
|
|
m_parent. on ## name(); \
|
|
|
|
} \
|
2021-07-18 13:28:40 +00:00
|
|
|
~_Activate ## name() noexcept { \
|
2020-05-03 19:45:01 +00:00
|
|
|
m_parent. off ## name(); \
|
|
|
|
} \
|
|
|
|
};
|
2019-01-14 23:17:54 +00:00
|
|
|
|
2020-05-03 19:45:01 +00:00
|
|
|
#define DEFINE_PIN_ACTIVATOR_LOW(name) \
|
|
|
|
DEFINE_PIN_ACTIVATOR(name, lower, raise)
|
2019-01-14 23:17:54 +00:00
|
|
|
|
2020-05-03 19:45:01 +00:00
|
|
|
#define DEFINE_PIN_ACTIVATOR_HIGH(name) \
|
|
|
|
DEFINE_PIN_ACTIVATOR(name, raise, lower)
|
|
|
|
|
|
|
|
#define DECLARE_PIN(name, visibility) \
|
|
|
|
public: \
|
|
|
|
DECLARE_PIN_SIGNALS(name) \
|
2021-07-18 13:28:40 +00:00
|
|
|
[[nodiscard]] constexpr auto& name () noexcept { \
|
2020-05-03 19:45:01 +00:00
|
|
|
return m_## name ## _Line; \
|
|
|
|
} \
|
2021-07-18 13:28:40 +00:00
|
|
|
[[nodiscard]] constexpr const auto& name () const noexcept { \
|
2021-01-06 15:30:26 +00:00
|
|
|
return m_## name ## _Line; \
|
|
|
|
} \
|
2020-05-03 19:45:01 +00:00
|
|
|
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)
|
2019-01-14 23:17:54 +00:00
|
|
|
|
2019-01-10 20:44:16 +00:00
|
|
|
namespace EightBit {
|
|
|
|
class Device {
|
|
|
|
public:
|
|
|
|
enum class PinLevel {
|
|
|
|
Low, High
|
|
|
|
};
|
|
|
|
|
2021-07-18 13:28:40 +00:00
|
|
|
DECLARE_PIN_INPUT(POWER)
|
2019-01-10 20:44:16 +00:00
|
|
|
|
2021-07-18 13:28:40 +00:00
|
|
|
public:
|
|
|
|
[[nodiscard]] static constexpr auto raised(const PinLevel line) noexcept { return line == PinLevel::High; }
|
2022-07-01 08:11:28 +00:00
|
|
|
static constexpr void raise(PinLevel& line) noexcept { match(line, PinLevel::High); }
|
2021-07-18 13:28:40 +00:00
|
|
|
[[nodiscard]] static constexpr auto lowered(const PinLevel line) noexcept { return line == PinLevel::Low; }
|
2022-07-01 08:11:28 +00:00
|
|
|
static constexpr void lower(PinLevel& line) noexcept { match(line, PinLevel::Low); }
|
2019-06-02 11:12:04 +00:00
|
|
|
|
2021-07-18 13:28:40 +00:00
|
|
|
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; }
|
2019-01-10 20:44:16 +00:00
|
|
|
|
2022-07-01 08:11:28 +00:00
|
|
|
static constexpr void flip(PinLevel& out) noexcept { match(out, out == PinLevel::Low ? PinLevel::High : PinLevel::Low); }
|
2021-08-23 08:40:54 +00:00
|
|
|
|
2021-07-18 13:28:40 +00:00
|
|
|
virtual ~Device() noexcept {};
|
2019-01-10 20:44:16 +00:00
|
|
|
|
2022-03-26 16:06:29 +00:00
|
|
|
Device(const Device& rhs) noexcept;
|
|
|
|
bool operator==(const Device& rhs) const noexcept;
|
2021-12-27 14:24:38 +00:00
|
|
|
|
2021-07-18 13:28:40 +00:00
|
|
|
[[nodiscard]] constexpr bool powered() const noexcept { return raised(POWER()); }
|
2019-01-10 20:44:16 +00:00
|
|
|
|
|
|
|
protected:
|
2021-07-18 13:28:40 +00:00
|
|
|
Device() noexcept {};
|
2019-01-10 20:44:16 +00:00
|
|
|
};
|
|
|
|
}
|