-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdevice.h
342 lines (288 loc) · 11.5 KB
/
device.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#ifndef __DEVICE_H__
#define __DEVICE_H__
#include <cstdlib>
#include <exception>
#include <list>
#include <map>
#include <memory>
#include <set>
#include <string>
#include "bounding_box.h"
#include "patch.h"
#include "png.h"
#include "port.h"
enum LinkResult {
CanTouch,
CanLink,
LinkError
};
class DeviceOperationException : public std::exception {
public:
DeviceOperationException(Device *d, std::string s) {
this->device = d;
this->message = s;
}
Device *device;
std::string message;
};
class Device {
public:
virtual ~Device(void) {};
virtual std::string name(void) = 0;
// Given an image, can a device of this type be parsed, starting from a
// given coordinate? (The device is responsible for remembering which
// pixels it parsed out.) Note that this is separate from "linking" -- we
// do not decide here whether the device can exist in the context of the
// pixels (and devices) surrounding it.
virtual bool parse(AspngSurface *, Coord) = 0;
Patch flood(AspngSurface *, Coord, Rgb);
// Return the list of pixels that were parsed out, during the method above.
virtual std::list<Patch *> all_patches(void) = 0;
std::set<Coord> all_patches_combined(void);
BoundingBox get_bounding_box(void);
Patch *find_patch_containing(Coord);
// `prelink` indicates whether these devices may touch and whether to
// create a port. During linking, if `prelink` returns CanLink for both
// devices, then `link` will be called on both devices, to actually add the
// ports.
virtual std::tuple<LinkResult, PortType, std::string> prelink(Patch *, std::shared_ptr<Device>) = 0;
void add_port(std::shared_ptr<Port>);
std::list<std::shared_ptr<Port> > all_ports(void);
// `link` gives each device a chance to finalize the linking process. This
// is the earliest time that the device knows all ports that are linking
// into it, and it must resolve "ToBeResolved" port types at this time.
virtual bool link(void) = 0;
// `propagate` should return the list of Ports that are immediate neighbors
// of the given Port.
virtual std::list<std::shared_ptr<Port> > propagate(std::shared_ptr<Port>) = 0;
virtual ElectricalValue get_value_at_port(std::shared_ptr<Port>) = 0;
virtual void apply_new_value(std::shared_ptr<Port>, ElectricalValue) = 0;
// If the device needs to know when a new simulation step has started,
// implement this.
virtual void new_step(void) {};
virtual void draw(AspngSurface *);
virtual void draw_debug(AspngSurface *);
virtual void click(Coord);
virtual void unclick(void);
protected:
void draw_helper(AspngSurface *, std::list<Patch *> &);
private:
std::list<std::shared_ptr<Port> > ports;
static void flood_helper(Coord, AspngSurface *, Patch &, std::list<Coord> &);
void maybe_neighbor(Device ***, int32_t, int32_t, int32_t, int32_t, std::set<Device *> *);
virtual Rgb get_draw_color(Patch *);
};
class BackgroundDevice : public Device {
public:
BackgroundDevice(void);
~BackgroundDevice(void);
std::string name(void) override;
static Device *create(void);
bool parse(AspngSurface *, Coord) override;
std::list<Patch *> all_patches(void) override;
std::tuple<LinkResult, PortType, std::string> prelink(Patch *, std::shared_ptr<Device>) override;
bool link(void) override;
std::list<std::shared_ptr<Port> > propagate(std::shared_ptr<Port>) override;
ElectricalValue get_value_at_port(std::shared_ptr<Port>) override;
void apply_new_value(std::shared_ptr<Port>, ElectricalValue) override;
static Rgb color;
private:
Patch patch;
virtual Rgb get_draw_color(Patch *) override;
};
class BaseTemplateDevice : public Device {
public:
bool parse(AspngSurface *, Coord) override;
std::list<Patch *> all_patches(void) override;
std::tuple<LinkResult, PortType, std::string> prelink(Patch *, std::shared_ptr<Device>) override;
static Rgb color;
void draw(AspngSurface *) override;
virtual std::string prefix(void) = 0;
virtual bool sub_parse(AspngSurface *, int32_t, int32_t, int32_t, int32_t, std::string) = 0;
virtual std::list<Patch *> sub_patches(void) = 0;
virtual void sub_draw(AspngSurface *, int32_t, int32_t, int32_t, int32_t) = 0;
private:
Patch patch;
std::string tab;
virtual Rgb get_draw_color(Patch *) override;
};
class BridgeDevice : public Device {
public:
std::string name(void) override;
static Device *create(void);
bool parse(AspngSurface *, Coord) override;
std::list<Patch *> all_patches(void) override;
std::tuple<LinkResult, PortType, std::string> prelink(Patch *, std::shared_ptr<Device>) override;
bool link(void) override;
std::list<std::shared_ptr<Port>> propagate(std::shared_ptr<Port>) override;
ElectricalValue get_value_at_port(std::shared_ptr<Port>) override;
void apply_new_value(std::shared_ptr<Port>, ElectricalValue) override;
static Rgb color;
private:
Patch patch;
virtual Rgb get_draw_color(Patch *) override;
};
class ClockDevice : public BaseTemplateDevice {
public:
ClockDevice(void);
std::string name(void) override;
static Device *create(void);
std::string prefix(void) override;
bool sub_parse(AspngSurface *, int32_t, int32_t, int32_t, int32_t, std::string) override;
bool link(void) override;
std::list<std::shared_ptr<Port>> propagate(std::shared_ptr<Port>) override;
ElectricalValue get_value_at_port(std::shared_ptr<Port>) override;
void apply_new_value(std::shared_ptr<Port>, ElectricalValue) override;
std::list<Patch *> sub_patches(void) override;
void sub_draw(AspngSurface *, int32_t, int32_t, int32_t, int32_t) override;
void new_step(void) override;
private:
ElectricalValue get_state(void);
uint64_t tick;
uint32_t divisor;
Patch sub_patch;
};
class CopperDevice : public Device {
public:
CopperDevice();
std::string name(void) override;
static Device *create(void);
bool parse(AspngSurface *, Coord) override;
std::list<Patch *> all_patches(void) override;
std::tuple<LinkResult, PortType, std::string> prelink(Patch *, std::shared_ptr<Device>) override;
bool link(void) override;
std::list<std::shared_ptr<Port>> propagate(std::shared_ptr<Port>) override;
ElectricalValue get_value_at_port(std::shared_ptr<Port>) override;
void apply_new_value(std::shared_ptr<Port>, ElectricalValue) override;
static Rgb color;
private:
Patch patch;
Rgb color_for_drawing;
virtual Rgb get_draw_color(Patch *) override;
};
class InputDevice : public BaseTemplateDevice {
public:
InputDevice(void);
std::string name(void) override;
static Device *create(void);
std::string prefix(void) override;
bool sub_parse(AspngSurface *, int32_t, int32_t, int32_t, int32_t, std::string) override;
bool link(void) override;
std::list<std::shared_ptr<Port>> propagate(std::shared_ptr<Port>) override;
ElectricalValue get_value_at_port(std::shared_ptr<Port>) override;
void apply_new_value(std::shared_ptr<Port>, ElectricalValue) override;
std::list<Patch *> sub_patches(void) override;
void sub_draw(AspngSurface *, int32_t, int32_t, int32_t, int32_t) override;
void click(Coord) override;
void unclick(void) override;
private:
bool being_clicked;
Patch sub_patch;
};
class LEDDevice : public BaseTemplateDevice {
public:
LEDDevice(void);
std::string name(void) override;
static Device *create(void);
std::string prefix(void) override;
bool sub_parse(AspngSurface *, int32_t, int32_t, int32_t, int32_t, std::string) override;
bool link(void) override;
std::list<std::shared_ptr<Port>> propagate(std::shared_ptr<Port>) override;
ElectricalValue get_value_at_port(std::shared_ptr<Port>) override;
void apply_new_value(std::shared_ptr<Port>, ElectricalValue) override;
std::list<Patch *> sub_patches(void) override;
void sub_draw(AspngSurface *, int32_t, int32_t, int32_t, int32_t) override;
private:
bool active;
Patch sub_patch;
};
class PullDevice : public Device {
public:
std::string name(void) override;
static Device *create(void);
bool parse(AspngSurface *, Coord) override;
std::list<Patch *> all_patches(void) override;
std::tuple<LinkResult, PortType, std::string> prelink(Patch *, std::shared_ptr<Device>) override;
bool link(void) override;
std::list<std::shared_ptr<Port>> propagate(std::shared_ptr<Port>) override;
ElectricalValue get_value_at_port(std::shared_ptr<Port>) override;
void apply_new_value(std::shared_ptr<Port>, ElectricalValue) override;
static Rgb yellow;
private:
Patch patch_source_or_sink;
Patch patch_yellow;
enum {
PullHi,
PullLo
} pull_type;
virtual Rgb get_draw_color(Patch *) override;
};
class SinkDevice : public Device {
public:
std::string name(void) override;
static Device *create(void);
bool parse(AspngSurface *, Coord) override;
std::list<Patch *> all_patches(void) override;
std::tuple<LinkResult, PortType, std::string> prelink(Patch *, std::shared_ptr<Device>) override;
bool link(void) override;
std::list<std::shared_ptr<Port>> propagate(std::shared_ptr<Port>) override;
ElectricalValue get_value_at_port(std::shared_ptr<Port>) override;
void apply_new_value(std::shared_ptr<Port>, ElectricalValue) override;
static Rgb color;
private:
Patch patch;
virtual Rgb get_draw_color(Patch *) override;
};
class SourceDevice : public Device {
public:
std::string name(void) override;
static Device *create(void);
bool parse(AspngSurface *, Coord) override;
std::list<Patch *> all_patches(void) override;
std::tuple<LinkResult, PortType, std::string> prelink(Patch *, std::shared_ptr<Device>) override;
bool link(void) override;
std::list<std::shared_ptr<Port>> propagate(std::shared_ptr<Port>) override;
ElectricalValue get_value_at_port(std::shared_ptr<Port>) override;
void apply_new_value(std::shared_ptr<Port>, ElectricalValue) override;
static Rgb color;
private:
Patch patch;
virtual Rgb get_draw_color(Patch *) override;
};
class SwitchDevice : public BaseTemplateDevice {
public:
std::string name(void) override;
static Device *create(void);
std::string prefix(void) override;
bool sub_parse(AspngSurface *, int32_t, int32_t, int32_t, int32_t, std::string) override;
bool link(void) override;
std::list<std::shared_ptr<Port>> propagate(std::shared_ptr<Port>) override;
ElectricalValue get_value_at_port(std::shared_ptr<Port>) override;
void apply_new_value(std::shared_ptr<Port>, ElectricalValue) override;
std::list<Patch *> sub_patches(void) override;
void sub_draw(AspngSurface *, int32_t, int32_t, int32_t, int32_t) override;
void click(Coord) override;
private:
bool closed;
Patch sub_patch;
};
class TransistorDevice : public Device {
public:
TransistorDevice();
std::string name(void) override;
static Device *create(void);
bool parse(AspngSurface *, Coord) override;
std::list<Patch *> all_patches(void) override;
std::tuple<LinkResult, PortType, std::string> prelink(Patch *, std::shared_ptr<Device>) override;
bool link(void) override;
std::list<std::shared_ptr<Port>> propagate(std::shared_ptr<Port>) override;
ElectricalValue get_value_at_port(std::shared_ptr<Port>) override;
void apply_new_value(std::shared_ptr<Port>, ElectricalValue) override;
static Rgb color;
void draw_debug(AspngSurface *) override;
private:
Patch patch;
bool passing;
virtual Rgb get_draw_color(Patch *) override;
};
#endif