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) \
|
|
|
|
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() { \
|
2019-09-06 22:55:57 +00:00
|
|
|
if (lowered( name ())) { \
|
|
|
|
Raising ## name.fire(EventArgs::empty()); \
|
|
|
|
raise( name ()); \
|
|
|
|
Raised ## name.fire(EventArgs::empty()); \
|
|
|
|
} \
|
2019-01-14 23:17:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#define DEFINE_PIN_LEVEL_LOWER(name, within) \
|
|
|
|
void EightBit:: within ::lower ## name() { \
|
2019-09-06 22:55:57 +00:00
|
|
|
if (raised( name ())) { \
|
|
|
|
Lowering ## name.fire(EventArgs::empty()); \
|
|
|
|
lower( name ()); \
|
|
|
|
Lowered ## name.fire(EventArgs::empty()); \
|
|
|
|
} \
|
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;
|
|
|
|
|
|
|
|
#define DECLARE_PIN(name, visibility) \
|
|
|
|
public: DECLARE_PIN_SIGNALS(name) \
|
2019-07-05 20:42:19 +00:00
|
|
|
[[nodiscard]] PinLevel& name () noexcept { return m_## name ## _Line; } \
|
2019-01-14 23:17:54 +00:00
|
|
|
visibility : DECLARE_PIN_LEVEL_CHANGERS(name) \
|
|
|
|
private: DECLARE_PIN_MEMBER(name)
|
|
|
|
|
|
|
|
// Input pins have a degree of external control
|
|
|
|
#define DECLARE_PIN_INPUT(name) DECLARE_PIN(name, public)
|
|
|
|
|
|
|
|
// Output pins may only be internally controlled
|
|
|
|
#define DECLARE_PIN_OUTPUT(name) DECLARE_PIN(name, protected)
|
|
|
|
|
2019-01-10 20:44:16 +00:00
|
|
|
namespace EightBit {
|
|
|
|
class Device {
|
|
|
|
public:
|
|
|
|
enum class PinLevel {
|
|
|
|
Low, High
|
|
|
|
};
|
|
|
|
|
|
|
|
static constexpr auto raised(const PinLevel line) { return line == PinLevel::High; }
|
|
|
|
static void raise(PinLevel& line) noexcept { line = PinLevel::High; }
|
|
|
|
static constexpr auto lowered(const PinLevel line) { return line == PinLevel::Low; }
|
|
|
|
static void lower(PinLevel& line) noexcept { line = PinLevel::Low; }
|
|
|
|
|
2019-06-02 11:12:04 +00:00
|
|
|
static void match(PinLevel& line, int condition) {
|
|
|
|
match(line, condition != 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void match(PinLevel& line, bool condition) {
|
|
|
|
condition ? raise(line) : lower(line);
|
|
|
|
}
|
|
|
|
|
2019-01-10 20:44:16 +00:00
|
|
|
virtual ~Device() {};
|
|
|
|
|
2019-01-14 23:17:54 +00:00
|
|
|
[[nodiscard]] bool powered() noexcept { return raised(POWER()); }
|
2019-01-10 20:44:16 +00:00
|
|
|
|
2019-01-14 23:17:54 +00:00
|
|
|
DECLARE_PIN_INPUT(POWER)
|
2019-01-10 20:44:16 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
Device() {};
|
|
|
|
};
|
|
|
|
}
|