This repository was archived by the owner on Aug 15, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathget-port-position.ts
More file actions
265 lines (240 loc) · 7.4 KB
/
get-port-position.ts
File metadata and controls
265 lines (240 loc) · 7.4 KB
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
import _ from "lodash"
export type VerticalPortSideConfiguration = {
pin_definition_direction?: "top-to-bottom" | "bottom-to-top"
pins: number[]
}
export type HorizontalPortSideConfiguration = {
pin_definition_direction?: "left-to-right" | "right-to-left"
pins: number[]
}
export type ExplicitPinMappingArrangement = {
left_side?: VerticalPortSideConfiguration
right_side?: VerticalPortSideConfiguration
top_side?: HorizontalPortSideConfiguration
bottom_side?: HorizontalPortSideConfiguration
}
export type SideSizes = {
left_size?: number
right_size?: number
top_size?: number
bottom_size?: number
}
export type PortArrangement = SideSizes | ExplicitPinMappingArrangement
export type ExtendedPortArrangement = PortArrangement & {
pin_spacing?: number
schWidth?: number
}
export const hasExplicitPinMapping = (
pa: PortArrangement
): pa is ExplicitPinMappingArrangement => {
for (const side of ["left_side", "right_side", "top_side", "bottom_side"]) {
if (side in pa && typeof pa[side] === "number") {
throw new Error(
`A number was specified for "${side}", try using "${side.replace(
"side",
"size"
)}"`
)
}
}
return (
"left_side" in pa ||
"right_side" in pa ||
"top_side" in pa ||
"bottom_side" in pa
)
}
export const getNormalToExplicitPinMapping = (
pa: ExplicitPinMappingArrangement
): number[] => {
const normal_to_explicit: number[] = [0]
const { left_side, right_side, top_side, bottom_side } = pa
for (const [side, normalDirection] of [
[left_side, "top-to-bottom"],
[bottom_side, "left-to-right"],
[right_side, "bottom-to-top"],
[top_side, "right-to-left"],
] as const) {
if (side) {
const definedOrderNormal =
side.pin_definition_direction === undefined ||
side.pin_definition_direction === normalDirection
const definedPins = [...side.pins]
if (!definedOrderNormal) {
definedPins.reverse()
}
for (let i = 0; i < definedPins.length; i++) {
normal_to_explicit.push(definedPins[i])
}
}
}
return normal_to_explicit
}
export const getExplicitToNormalPinMapping = (
pa: ExplicitPinMappingArrangement
): number[] => {
const normal_to_explicit: number[] = getNormalToExplicitPinMapping(pa)
const explicit_to_normal: number[] = []
for (let i = 0; i < normal_to_explicit.length; i++) {
explicit_to_normal[normal_to_explicit[i]] = i
}
return explicit_to_normal
}
export const getSizeOfSidesFromPortArrangement = (
pa: PortArrangement
): {
left_size: number
right_size: number
top_size: number
bottom_size: number
} => {
if (hasExplicitPinMapping(pa)) {
return {
left_size: pa.left_side?.pins.length ?? 0,
right_size: pa.right_side?.pins.length ?? 0,
top_size: pa.top_side?.pins.length ?? 0,
bottom_size: pa.bottom_side?.pins.length ?? 0,
}
}
const {
left_size = 0,
right_size = 0,
top_size = 0,
bottom_size = 0,
} = pa as any
return { left_size, right_size, top_size, bottom_size }
}
/**
* Minimum distance between two sides, e.g. if there is only a left_size and
* right_size provided (only pins on left and right) then the minimum distance
* is the x distance between the left and right ports.
*/
const MIN_SIDE_DIST = 1.5
/**
* Distance between ports on the same side
*/
export const DEFAULT_PIN_SPACING = 0.5
/**
* These are all the defined port indices, for regular bugs all ports are
* defined but some bugs have a lot of unused pins, so there will be skipped
* ports.
*/
export const getPortIndices = (pa: PortArrangement): number[] => {
if (hasExplicitPinMapping(pa)) {
const port_indices: number[] = []
for (const p of [
...(pa.left_side?.pins ?? []),
...(pa.bottom_side?.pins ?? []),
...(pa.right_side?.pins ?? []),
...(pa.top_side?.pins ?? []),
]) {
port_indices.push(p)
}
return port_indices
}
const { left_size, right_size, top_size, bottom_size } =
getSizeOfSidesFromPortArrangement(pa)
return _.range(1, left_size + right_size + top_size + bottom_size + 1)
}
export const getPortArrangementSize = (
port_arrangement: ExtendedPortArrangement
): { width: number; height: number; total_ports: number } => {
const pinSpacing = port_arrangement.pin_spacing ?? DEFAULT_PIN_SPACING
const {
top_size = 0,
right_size = 0,
bottom_size = 0,
left_size = 0,
} = getSizeOfSidesFromPortArrangement(port_arrangement)
const total_ports = top_size + right_size + bottom_size + left_size
const calculatedWidth = Math.max(
MIN_SIDE_DIST * (pinSpacing / DEFAULT_PIN_SPACING),
(top_size + 1) * pinSpacing,
(bottom_size + 1) * pinSpacing
)
const width =
port_arrangement.schWidth !== undefined
? port_arrangement.schWidth
: calculatedWidth
const height = Math.max(
MIN_SIDE_DIST,
(left_size + 1) * pinSpacing,
(right_size + 1) * pinSpacing
)
return { width, height, total_ports }
}
/**
* Get the position of a port given a port arrangement. The position corresponds
* to the index of the port if you travel counter-clockwise starting from the
* top-left and traveling down the left side. The index begins with 1.
*/
export const getPortPosition = (
port_arrangement: ExtendedPortArrangement,
position: number
): {
x: number
y: number
side: "top" | "bottom" | "left" | "right"
} => {
const pin_spacing = port_arrangement.pin_spacing ?? DEFAULT_PIN_SPACING
const {
top_size = 0,
right_size = 0,
bottom_size = 0,
left_size = 0,
} = getSizeOfSidesFromPortArrangement(port_arrangement)
const total = top_size + right_size + bottom_size + left_size
const port_indices = getPortIndices(port_arrangement)
if (!port_indices.includes(position)) {
throw new Error(
`Invalid position ${position} on port arrangement with available ports: ${port_indices.join(
" "
)}`
)
}
if (hasExplicitPinMapping(port_arrangement)) {
// Map position to equivalent position in a normal port mapping
const og_p = position
position = getExplicitToNormalPinMapping(port_arrangement)[position]
// console.log("original position:", og_p, "mapped to:", position)
}
let side: "top" | "bottom" | "left" | "right"
let index: number
if (position <= left_size) {
side = "left"
index = position - 1
} else if (position <= bottom_size + left_size) {
side = "bottom"
index = position - left_size - 1
} else if (position <= bottom_size + left_size + right_size) {
side = "right"
index = position - left_size - bottom_size - 1
} else {
side = "top"
index = position - left_size - bottom_size - right_size - 1
}
const { width, height } = getPortArrangementSize(port_arrangement)
let x: number
let y: number
if (side === "top") {
const i_dist_center = index - (top_size - 1) / 2
x = i_dist_center * pin_spacing
y = height / 2
} else if (side === "bottom") {
const i_dist_center = index - (bottom_size - 1) / 2
x = i_dist_center * pin_spacing
y = -height / 2
} else if (side === "left") {
const i_dist_center = -index + (left_size - 1) / 2
y = i_dist_center * pin_spacing
x = -width / 2
} else if (side === "right") {
const i_dist_center = index - (right_size - 1) / 2
y = i_dist_center * pin_spacing
x = width / 2
} else {
throw new Error(`Invalid side "${side}", can't set x/y values`)
}
return { x, y, side }
}
export default getPortPosition