aboutsummaryrefslogtreecommitdiffstats
path: root/dummy/chip.h
blob: cc5013df458c6373202c82ac4870c40d1f7f2c2b (plain)
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
/*
 *  nextpnr -- Next Generation Place and Route
 *
 *  Copyright (C) 2018  Clifford Wolf <clifford@clifford.at>
 *
 *  Permission to use, copy, modify, and/or distribute this software for any
 *  purpose with or without fee is hereby granted, provided that the above
 *  copyright notice and this permission notice appear in all copies.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 *  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 *  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 *  ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 *  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 *  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 *  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 */

#include "design.h"

#ifndef CHIP_H
#define CHIP_H

struct DelayInfo
{
	float delay = 0;

	float raiseDelay() { return delay; }
	float fallDelay() { return delay; }
};

typedef IdString BelType;
typedef IdString PortPin;

static inline IdString belTypeToId(BelType type) { return type; }
static inline IdString portPinToId(PortPin type) { return type; }

static inline BelType belTypeFromId(IdString id) { return id; }
static inline PortPin portPinFromId(IdString id) { return id; }

struct BelId
{
	uint8_t tile_x = 0, tile_y = 0;
	uint16_t index = 0;

	bool nil() const {
		return !tile_x && !tile_y && !index;
	}
} __attribute__((packed));

struct WireId
{
	uint8_t tile_x = 0, tile_y = 0;
	uint16_t index = 0;

	bool nil() const {
		return !tile_x && !tile_y && !index;
	}
} __attribute__((packed));

namespace std
{
	template<> struct hash<BelId>
        {
		std::size_t operator()(const BelId &bel) const noexcept
		{
			std::size_t result = std::hash<int>{}(bel.index);
			result ^= std::hash<int>{}(bel.tile_x) + 0x9e3779b9 + (result << 6) + (result >> 2);
			result ^= std::hash<int>{}(bel.tile_y) + 0x9e3779b9 + (result << 6) + (result >> 2);
			return result;
		}
	};

	template<> struct hash<WireId>
        {
		std::size_t operator()(const WireId &wire) const noexcept
		{
			std::size_t result = std::hash<int>{}(wire.index);
			result ^= std::hash<int>{}(wire.tile_x) + 0x9e3779b9 + (result << 6) + (result >> 2);
			result ^= std::hash<int>{}(wire.tile_y) + 0x9e3779b9 + (result << 6) + (result >> 2);
			return result;
		}
	};
}

struct BelIterator
{
	BelId *ptr = nullptr;

	void operator++() { ptr++; }
	bool operator!=(const BelIterator &other) const { return ptr != other.ptr; }
	BelId operator*() const { return *ptr; }
};

struct BelRange
{
	BelIterator b, e;
	BelIterator begin() const { return b; }
	BelIterator end() const { return e; }
};

struct WireIterator
{
	WireId *ptr = nullptr;

	void operator++() { ptr++; }
	bool operator!=(const WireIterator &other) const { return ptr != other.ptr; }
	WireId operator*() const { return *ptr; }
};

struct WireRange
{
	WireIterator b, e;
	WireIterator begin() const { return b; }
	WireIterator end() const { return e; }
};

struct WireDelay
{
	WireId wire;
	DelayInfo delay;
};

struct WireDelayIterator
{
	WireDelay *ptr = nullptr;

	void operator++() { ptr++; }
	bool operator!=(const WireDelayIterator &other) const { return ptr != other.ptr; }
	WireDelay operator*() const { return *ptr; }
};

struct WireDelayRange
{
	WireDelayIterator b, e;
	WireDelayIterator begin() const { return b; }
	WireDelayIterator end() const { return e; }
};

struct BelPin
{
	BelId bel;
	PortPin pin;
};

struct BelPinIterator
{
	BelPin *ptr = nullptr;

	void operator++() { ptr++; }
	bool operator!=(const BelPinIterator &other) const { return ptr != other.ptr; }
	BelPin operator*() const { return *ptr; }
};

struct BelPinRange
{
	BelPinIterator b, e;
	BelPinIterator begin() const { return b; }
	BelPinIterator end() const { return e; }
};

struct GuiLine
{
	float x1, y1, x2, y2;
};

struct ChipArgs
{
	// ...
};

struct Chip
{
	// ...

	Chip(ChipArgs args);

	void setBelActive(BelId bel, bool active);
	bool getBelActive(BelId bel);

	BelId getBelByName(IdString name) const;
	WireId getWireByName(IdString name) const;
	IdString getBelName(BelId bel) const;
	IdString getWireName(WireId wire) const;

	BelRange getBels() const;
	BelRange getBelsByType(BelType type) const;
	BelType getBelType(BelId bel) const;

	void getBelPosition(BelId bel, float &x, float &y) const;
	void getWirePosition(WireId wire, float &x, float &y) const;
	vector<GuiLine> getBelGuiLines(BelId bel) const;
	vector<GuiLine> getWireGuiLines(WireId wire) const;

	WireRange getWires() const;
	WireDelayRange getWiresUphill(WireId wire) const;
	WireDelayRange getWiresDownhill(WireId wire) const;
	WireDelayRange getWiresBidir(WireId wire) const;
	WireDelayRange getWireAliases(WireId wire) const;

	// the following will only operate on / return "active" BELs
	// multiple active uphill BELs for a wire will cause a runtime error
	WireId getWireBelPin(BelId bel, PortPin pin) const;
	BelPin getBelPinUphill(WireId wire) const;
	BelPinRange getBelPinsDownhill(WireId wire) const;
};

#endif