improved wire arguments

This commit is contained in:
Louis Heredero 2025-04-19 15:56:20 +02:00
parent 4930b53c84
commit 3e9272255c
Signed by: HEL
GPG Key ID: 8D83DE470F8544E7
7 changed files with 90 additions and 50 deletions

Binary file not shown.

View File

@ -1,5 +1,8 @@
#import "../src/lib.typ": * #import "../src/lib.typ": *
#import "@preview/cetz:0.3.4": draw #import "@preview/cetz:0.3.4": draw
#set text(font: "Source Sans 3")
#circuit({ #circuit({
element.block( element.block(
size: (1.5, 2.2), size: (1.5, 2.2),
@ -10,14 +13,12 @@
north: (id: "CLK", clock: true), north: (id: "CLK", clock: true),
east: "PC", east: "PC",
south: (("EN", "EN"),) south: (("EN", "EN"),)
), )
debug: (ports: true)
) )
wire.stub("PCBuf.CLK", name: "CLK") wire.stub("PCBuf.CLK", name: "CLK")
wire.stub("PCBuf.EN", name: "PCWrite") wire.stub("PCBuf.EN", name: "PCWrite")
element.multiplexer( element.multiplexer(
pos: ( pos: (
3, (align: "in0", with: "PCBuf.PC") 3, (align: "in0", with: "PCBuf.PC")
@ -25,18 +26,18 @@
size: (1, 2), size: (1, 2),
id: "AdrSrc-MP", id: "AdrSrc-MP",
fill: util.colors.orange, fill: util.colors.orange,
entries: 2, entries: 2
debug: (ports: true)
) )
/*wire.wire( wire.wire(
"PCBuf.PC", "PCBuf.PC",
"AdrSrc-MP.in0", "AdrSrc-MP.in0",
id: "wPCBuf-InstDataMgr", id: "wPCBuf-InstDataMgr",
name: "PC", name: "PC",
bus: true bus: true
) )
wire.stub("AdrSrc-MP.north", name: "AdrSrc") wire.stub("AdrSrc-MP.north", side: "north", name: "AdrSrc")
/*
element.block( element.block(
pos: ( pos: (
6, (align: "A", with: "AdrSrc-MP.out") 6, (align: "A", with: "AdrSrc-MP.out")

View File

@ -116,22 +116,29 @@
return coord return coord
} }
#let make-bounds(elmt, x, y, w, h) = { #let complete-bounds(elmt, bounds) = {
let w2 = w / 2 let b = bounds
let h2 = h / 2
let bounds = (
bl: (x, y),
tl: (x, y + h),
tr: (x + w, y + h),
br: (x + w, y),
center: (x + w2, y + h2),
b: (x + w2, y),
t: (x + w2, y + h),
l: (x, y + h2),
r: (x + w, y + h2),
)
bounds += ( bounds += (
center: (
(b.br.at(0) + b.tl.at(0))/2,
(b.br.at(1) + b.tl.at(1))/2
),
b: (
(b.br.at(0) + b.bl.at(0))/2,
(b.br.at(1) + b.bl.at(1))/2
),
t: (
(b.tr.at(0) + b.tl.at(0))/2,
(b.tr.at(1) + b.tl.at(1))/2
),
l: (
(b.bl.at(0) + b.tl.at(0))/2,
(b.bl.at(1) + b.tl.at(1))/2
),
r: (
(b.br.at(0) + b.tr.at(0))/2,
(b.br.at(1) + b.tr.at(1))/2
),
sides: ( sides: (
north: (bounds.tl, bounds.tr), north: (bounds.tl, bounds.tr),
south: (bounds.bl, bounds.br), south: (bounds.bl, bounds.br),
@ -160,6 +167,19 @@
return bounds return bounds
} }
#let make-bounds(elmt, x, y, w, h) = {
let w2 = w / 2
let h2 = h / 2
let bounds = (
bl: (x, y),
tl: (x, y + h),
tr: (x + w, y + h),
br: (x + w, y),
)
return complete-bounds(elmt, bounds)
}
#let render(draw-shape, elmt) = draw.group(name: elmt.id, ctx => { #let render(draw-shape, elmt) = draw.group(name: elmt.id, ctx => {
let width = elmt.size.first() let width = elmt.size.first()
let height = elmt.size.last() let height = elmt.size.last()

View File

@ -5,27 +5,33 @@
#let draw-shape(elmt, bounds) = { #let draw-shape(elmt, bounds) = {
let margin = (100% - elmt.l-ratio) / 2 let margin = (100% - elmt.l-ratio) / 2
let tr2 = (bounds.tr, margin, bounds.br) let tr2 = util.lerp(bounds.tr, margin, bounds.br)
let br2 = (bounds.br, margin, bounds.tr) let br2 = util.lerp(bounds.br, margin, bounds.tr)
let f = draw.group(name: elmt.id, { let bounds2 = element.complete-bounds(elmt, (
tl: bounds.tl,
bl: bounds.bl,
tr: tr2,
br: br2,
))
let f = {
draw.merge-path( draw.merge-path(
inset: 0.5em, inset: 0.5em,
fill: elmt.fill, fill: elmt.fill,
stroke: elmt.stroke, stroke: elmt.stroke,
close: true, close: true,
draw.line(bounds.tl, tr2, br2, bounds.bl) draw.line(bounds2.tl, bounds2.tr, bounds2.br, bounds2.bl)
) )
draw.anchor("north", (bounds.tl, 50%, tr2)) draw.anchor("north", bounds2.t)
draw.anchor("south", (bounds.bl, 50%, br2)) draw.anchor("south", bounds2.b)
draw.anchor("west", (bounds.tl, 50%, bounds.bl)) draw.anchor("west", bounds2.l)
draw.anchor("east", (tr2, 50%, br2)) draw.anchor("east", bounds2.r)
draw.anchor("north-west", bounds.tl) draw.anchor("north-west", bounds2.tl)
draw.anchor("north-east", tr2) draw.anchor("north-east", bounds2.tr)
draw.anchor("south-east", br2) draw.anchor("south-east", bounds2.br)
draw.anchor("south-west", bounds.bl) draw.anchor("south-west", bounds2.bl)
}) }
return (f, bounds) return (f, bounds2)
} }
/// Draws a multiplexer /// Draws a multiplexer

View File

@ -9,7 +9,7 @@
} }
} }
} }
panic("Unknown port " + port + " on element " + element.id) panic("Unknown port " + port + " on element " + elmt.id)
} }
#let get-port-idx(elmt, port, side: auto) = { #let get-port-idx(elmt, port, side: auto) = {

View File

@ -74,3 +74,10 @@
"center", "north", "east", "west", "south", "center", "north", "east", "west", "south",
"north-east", "north-west", "south-east", "south-west" "north-east", "north-west", "south-east", "south-west"
) )
#let lerp(pt0, ratio, pt1) = {
return (
(pt1.at(0) - pt0.at(0)) * ratio / 100% + pt0.at(0),
(pt1.at(1) - pt0.at(1)) * ratio / 100% + pt0.at(1)
)
}

View File

@ -130,10 +130,11 @@
/// - dodge-sides (array): The start and end sides (going out of the connected element) of the wire (only with style "dodge") /// - dodge-sides (array): The start and end sides (going out of the connected element) of the wire (only with style "dodge")
/// - dodge-margins (array): The start and end margins (i.e. space before dodging) of the wire (only with style "dodge") /// - dodge-margins (array): The start and end margins (i.e. space before dodging) of the wire (only with style "dodge")
#let wire( #let wire(
id, pts, pt0,
pt1,
id: none,
bus: false, bus: false,
name: none, name: none,
name-pos: "middle",
slice: none, slice: none,
color: black, color: black,
dashed: false, dashed: false,
@ -145,15 +146,14 @@
zigzag-dir: "vertical", zigzag-dir: "vertical",
dodge-y: 0, dodge-y: 0,
dodge-sides: ("east", "west"), dodge-sides: ("east", "west"),
dodge-margins: (5%, 5%) dodge-margins: (5%, 5%),
..args
) = draw.get-ctx(ctx => { ) = draw.get-ctx(ctx => {
if not style in wire-styles { if not style in wire-styles {
panic("Invalid wire style '" + style + "'") panic("Invalid wire style '" + style + "'")
} }
if pts.len() != 2 { let pts = (pt0, pt1)
panic("Wrong number of points (got " + str(pts.len()) + " instead of 2)")
}
let stroke = ( let stroke = (
paint: color, paint: color,
@ -218,7 +218,7 @@
let names = () let names = ()
if type(name) == str { if type(name) == str {
names = ((name, name-pos),) names = ((name, "middle"),)
} else if type(name) == array { } else if type(name) == array {
names = ( names = (
@ -228,6 +228,9 @@
} }
for (name, pos) in names { for (name, pos) in names {
if name == none {
continue
}
let point let point
let anchor let anchor
@ -269,7 +272,7 @@
/// - vertical (bool): Whether the name should be displayed vertically /// - vertical (bool): Whether the name should be displayed vertically
/// - length (number): The length of the stub /// - length (number): The length of the stub
/// - name-offset (number): The name offset, perpendicular to the stub /// - name-offset (number): The name offset, perpendicular to the stub
#let stub(anchor, name: none, vertical: false, length: 1em, name-offset: 0) = { #let stub(anchor, side: auto, name: none, vertical: false, length: 1em, name-offset: 0) = {
if "." not in anchor { if "." not in anchor {
panic("`anchor` must be a valid anchor of an element") panic("`anchor` must be a valid anchor of an element")
} }
@ -280,11 +283,14 @@
let pre-process = (elements, elmt) => { let pre-process = (elements, elmt) => {
let eid = elmt.id let eid = elmt.id
if port-elmt-id not in elements { let side = side
panic("Unknown element " + port-elmt-id) if side == auto {
if port-elmt-id not in elements {
panic("Unknown element " + port-elmt-id)
}
let port-elmt = elements.at(port-elmt-id)
side = get-port-side(port-elmt, port-id)
} }
let port-elmt = elements.at(port-elmt-id)
let side = get-port-side(port-elmt, port-id)
elements.at(eid).insert("side", side) elements.at(eid).insert("side", side)
return elements return elements
} }