-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathwiring.h
173 lines (142 loc) · 3.41 KB
/
wiring.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
//
//
// wiring.h
//
// (C) R.P.Bellis 2021
//
//
#pragma once
#include <functional>
//---------------------------------------------------------------------
//
// helper functions for InputPin and Input Port
//
static inline constexpr bool default_true() {
return true;
}
static inline constexpr uint8_t default_high() {
return 0xff;
}
//---------------------------------------------------------------------
//
// OutputPin:
//
// An object providing read-only access to a reference
// to a bool, representing the state of an IO pin.
//
// If the `invert` flag is set then it indicates a pin
// that outputs the opposite of the referenced variable.
//
class OutputPin {
protected:
bool& val;
const bool invert;
public:
OutputPin(bool& val, bool invert = false)
: val(val), invert(invert)
{ }
operator bool() const { return val ^ invert; }
};
//---------------------------------------------------------------------
//
// OutputPinReg:
//
// An object providing read-only access to a single bit
// within uint8_t, representing the state of an IO pin.
//
// Use this to handle cases like the 6522 VIA, where the
// ~IRQ pin always represents the value of IFR bit 7.
//
// If the `invert` flag is set then it indicates a pin
// that outputs the opposite of the referenced bit.
//
class OutputPinReg {
protected:
uint8_t& val;
const uint8_t mask;
const bool invert;
public:
OutputPinReg(uint8_t& val, uint8_t bit, bool invert = false)
: val(val), mask(1 << bit), invert(invert)
{ }
operator bool() const {
return (bool)(val & mask) ^ invert;
}
};
//---------------------------------------------------------------------
//
// OutputPort:
//
// An object providing read-only access to the N least
// significant bits of a uint8_t
//
template<size_t N = 8>
class OutputPort {
static_assert(N <= 8, "OutputPort too large");
protected:
uint8_t& data;
const uint8_t mask = (1 << N) - 1;
public:
OutputPort(uint8_t& data)
: data(data)
{ }
operator uint8_t() const {
return data & mask;
}
};
//---------------------------------------------------------------------
//
// InputPin:
//
// An object that represents a single input pin, where
// the current value of that pin is determined by calling
// a developer-supplied function that typically will
// poll the state of one or more Output* objects
//
class InputPin {
protected:
using Function = std::function<bool()>;
protected:
Function f = default_true;
public:
void bind(const Function& _f) {
f = _f;
}
bool get() const {
return f();
}
operator bool() const {
return get();
}
};
//---------------------------------------------------------------------
//
// InputPort<N>
//
// An object that represents an N-bit input port, where
// the current value of the port is determined by calling
// a developer-supplied function that typically will
// poll the state of one or more Output* objects
//
// The value may be read by casting to (uint8_t), and will
// be masked to be within the expected legal range.
//
template<size_t N>
class InputPort {
static_assert(N <= 8, "InputPort too large");
using Function = std::function<uint8_t()>;
protected:
Function f = default_high;
protected:
const uint8_t mask = (1 << N) - 1;
public:
void bind(const Function& _f) {
f = _f;
}
uint8_t get() const {
return f() & mask;
}
operator uint8_t() const {
return get();
}
};