Compare commits

...

4 Commits

7 changed files with 121 additions and 176 deletions

Binary file not shown.

View File

@ -2,6 +2,7 @@
#import "@preview/cetz:0.3.4": draw
#set text(font: "Source Sans 3")
#set page(width: auto, height: auto, margin: .5cm)
#circuit({
element.block(
@ -37,7 +38,6 @@
)
wire.stub("AdrSrc-MP.north", side: "north", name: "AdrSrc")
/*
element.block(
pos: (
6, (align: "A", with: "AdrSrc-MP.out")
@ -126,11 +126,12 @@
bus: true
)
let mid = ("InstDataMgr.east", 50%, "RegFile.west")
wire.wire(
"InstDataMgr.Instr",
(vertical: (), horizontal: mid),
id: "wInstDataMgr-Bus"
id: "wInstDataMgr-Bus",
name: ("Instr", none),
bus: true
)
@ -143,7 +144,7 @@
wire.wire(
"RegFile.A1",
(horizontal: mid, vertical: ()),
id: "wBus-RegFile-A1"
id: "wBus-RegFile-A1",
name: (none, "RS1"),
slice: (19, 15),
reverse: true,
@ -152,7 +153,7 @@
wire.wire(
"RegFile.A3",
(horizontal: mid, vertical: ()),
id: "wBus-RegFile-A3"
id: "wBus-RegFile-A3",
name: (none, "RD"),
slice: (11, 7),
reverse: true,
@ -161,12 +162,12 @@
wire.wire(
"Extender.in",
(horizontal: mid, vertical: ()),
id: "wBus-Extender"
id: "wBus-Extender",
slice: (31, 7),
reverse: true,
bus: true
)
element.alu(
pos: (
22, (align: "in1", with: "RegFile.RD1")
@ -178,10 +179,11 @@
wire.wire(
"RegFile.RD1",
"ALU.in1",
id: "wRegFile-ALU"
id: "wRegFile-ALU",
name: ("A", "SrcA"),
bus: true
)
wire.stub("ALU.north", side: "north")
element.block(
pos: (
@ -200,7 +202,7 @@
wire.wire(
"ALU.out",
"OutBuf.in",
id: "wALU-OutBuf"
id: "wALU-OutBuf",
name: "ALUResult",
bus: true
)
@ -214,12 +216,12 @@
fill: util.colors.orange,
entries: 3
)
wire.stub("Res-MP.north", name: "ResultSrc")
wire.stub("Res-MP.north", side: "north", name: "ResultSrc")
wire.stub("Res-MP.in2")
wire.wire(
"OutBuf.out",
"Res-MP.in0",
id: "wOutBuf-ResMP"
id: "wOutBuf-ResMP",
name: "ALUOut",
bus: true
)
@ -227,7 +229,7 @@
wire.wire(
"Extender.out",
"ALU.in2",
id: "wExt-ALU"
id: "wExt-ALU",
name: ("ImmExt", "SrcB"),
bus: true,
style: "zigzag",
@ -237,7 +239,7 @@
wire.wire(
"InstDataMgr.RD",
"Res-MP.in1",
id: "wInstDataMgr-ResMP"
id: "wInstDataMgr-ResMP",
style: "dodge",
dodge-y: -4,
dodge-sides: ("east", "west"),
@ -248,7 +250,7 @@
wire.wire(
"Res-MP.out",
"AdrSrc-MP.in1",
id: "wResMP-AdrSrc"
id: "wResMP-AdrSrc",
style: "dodge",
dodge-y: -5,
dodge-sides: ("east", "west"),
@ -259,7 +261,7 @@
wire.wire(
"Res-MP.out",
"RegFile.WD3",
id: "wResMP-RegFile"
id: "wResMP-RegFile",
style: "dodge",
dodge-y: -5,
dodge-sides: ("east", "west"),
@ -270,7 +272,7 @@
wire.wire(
"Res-MP.out",
"PCBuf.PCNext",
id: "wResMP-PCBuf"
id: "wResMP-PCBuf",
style: "dodge",
dodge-y: -5,
dodge-sides: ("east", "west"),
@ -281,5 +283,4 @@
wire.intersection("wResMP-RegFile.dodge-end", radius: .2)
wire.intersection("wResMP-AdrSrc.dodge-end", radius: .2)
*/
})

View File

@ -1,45 +1,40 @@
#import "@preview/cetz:0.3.2": draw
#import "element.typ"
#import "ports.typ": add-port
#import "../util.typ"
#let draw-shape(elmt, bounds) = {
let tl = bounds.tl
let tr = bounds.tr
let bl = bounds.bl
let br = bounds.br
#let draw-shape(id, tl, tr, br, bl, fill, stroke) = {
let p0 = tl
let p1 = (tr, 10%, br)
let p2 = (tr, 90%, br)
let p1 = util.lerp(tr, 10%, br)
let p2 = util.lerp(tr, 90%, br)
let p3 = bl
let p4 = (tl, 55%, bl)
let p5 = (tl, 50%, br)
let p6 = (tl, 45%, bl)
let p4 = util.lerp(tl, 55%, bl)
let p5 = util.lerp(tl, 50%, br)
let p6 = util.lerp(tl, 45%, bl)
let bounds2 = element.complete-bounds(elmt, (
tl: p0,
tr: p1,
br: p2,
bl: p3
))
let f1 = draw.group(name: id, {
draw.merge-path(
inset: 0.5em,
fill: fill,
stroke: stroke,
close: true,
draw.line(p0, p1, p2, p3, p4, p5, p6)
)
draw.anchor("north", (p0, 50%, p1))
draw.anchor("south", (p2, 50%, p3))
draw.anchor("west", (p0, 50%, p3))
draw.anchor("east", (p1, 50%, p2))
draw.anchor("north-west", p0)
draw.anchor("north-east", p1)
draw.anchor("south-east", p2)
draw.anchor("south-west", p3)
draw.anchor("name", (p5, 50%, (p1, 50%, p2)))
})
let f2 = add-port(id, "west", (id: "in1"), (p0, 50%, p6))
let f3 = add-port(id, "west", (id: "in2"), (p3, 50%, p4))
let f4 = add-port(id, "east", (id: "out"), (p1, 50%, p2))
let f = {
f1; f2; f3; f4
draw.line(
p0, p1, p2, p3, p4, p5, p6,
fill: elmt.fill,
stroke: elmt.stroke,
close: true
)
draw.anchor("name", (p5, 50%, (p1, 50%, p2)))
}
return (f, tl, tr, br, bl)
return (f, bounds2)
}
/// Draws an ALU with two inputs
@ -47,18 +42,7 @@
/// #examples.alu
/// For parameters description, see #doc-ref("element.elmt")
#let alu(
x: none,
y: none,
w: none,
h: none,
name: none,
name-anchor: "center",
fill: none,
stroke: black + 1pt,
id: "",
debug: (
ports: false
)
..args
) = {
let ports = (
west: (
@ -70,24 +54,18 @@
)
)
element.elmt(
return element.elmt(
draw-shape: draw-shape,
x: x,
y: y,
w: w,
h: h,
name: name,
name-anchor: name-anchor,
ports: ports,
fill: fill,
stroke: stroke,
id: id,
auto-ports: false,
ports-y: (
in1: (h) => {h * 0.225},
in2: (h) => {h * 0.775},
out: (h) => {h * 0.5}
ports-pos: (
west: (
in1: l => l * 0.225,
in2: l => l * 0.775,
),
east: (
out: l => l * 0.5
)
),
debug: debug
..args
)
}

View File

@ -10,7 +10,7 @@
}
}
}
panic("Could not find port with id '" + str(id) + "'")
panic("Could not find port with id '" + str(id) + "' in ports " + repr(ports))
}
#let local-to-global(origin, u, v, points) = {
@ -157,9 +157,9 @@
let props2 = props
if side in elmt.ports-margins {
let (pt0, pt1) = props
let margins = ports-margins.at(side)
a = (pt0, margins.at(0), pt1)
b = (pt0, 100% - margins.at(1), pt1)
let margins = elmt.ports-margins.at(side)
let a = util.lerp(pt0, margins.at(0), pt1)
let b = util.lerp(pt0, 100% - margins.at(1), pt1)
props2 = (a, b)
}
bounds.ports.insert(side, props2)
@ -209,6 +209,15 @@
)
func
draw.anchor("north", bounds.t)
draw.anchor("south", bounds.b)
draw.anchor("west", bounds.l)
draw.anchor("east", bounds.r)
draw.anchor("north-west", bounds.tl)
draw.anchor("north-east", bounds.tr)
draw.anchor("south-east", bounds.br)
draw.anchor("south-west", bounds.bl)
if elmt.name != none {
draw.content(
(name: elmt.id, anchor: elmt.name-anchor),
@ -219,6 +228,14 @@
}
add-ports(elmt, bounds)
if elmt.debug.bounds {
draw.line(
bounds.tl, bounds.tr, bounds.br, bounds.bl,
stroke: red,
close: true
)
}
})
/// Draws an element
@ -263,6 +280,7 @@
id: auto,
ports-pos: auto,
debug: (
bounds: false,
ports: false
),
extra: (:)
@ -287,7 +305,6 @@
ports.at(key) = side-ports
}
return ((
cls: cls,
id: id,

View File

@ -1,41 +1,25 @@
#import "@preview/cetz:0.3.2": draw
#import "element.typ"
#import "ports.typ": add-port
#import "../util.typ"
#let draw-shape(id, tl, tr, br, bl, fill, stroke, h-ratio: 75%, align-out: true) = {
let (x, y) = bl
let (width, height) = (tr.at(0) - x, tr.at(1) - y)
#let draw-shape(elmt, bounds) = {
let ratio = elmt.l-ratio / 100%
let tl2 = util.lerp(bounds.bl, elmt.l-ratio, bounds.tl)
let bounds2 = element.complete-bounds(elmt, (
tl: tl2,
tr: bounds.tr,
br: bounds.br,
bl: bounds.bl,
))
let ratio = h-ratio / 100%
tl = (x, y + height * ratio)
let tr2 = (x + width, y + height * ratio)
let br = (x + width, y)
if align-out {
(tr, tr2) = (tr2, tr)
} else {
(tr, tr2) = (tr, tr)
}
let f = draw.group(name: id, {
draw.merge-path(
inset: 0.5em,
fill: fill,
stroke: stroke,
close: true,
draw.line(tl, tr2, br, bl)
)
draw.anchor("north", (tl, 50%, tr2))
draw.anchor("south", (bl, 50%, br))
draw.anchor("west", (tl, 50%, bl))
draw.anchor("east", (tr2, 50%, br))
draw.anchor("north-west", tl)
draw.anchor("north-east", tr2)
draw.anchor("south-east", br)
draw.anchor("south-west", bl)
})
return (f, tl, tr, br, bl)
let f = draw.line(
bounds2.tl, bounds2.tr, bounds2.br, bounds2.bl,
fill: elmt.fill,
stroke: elmt.stroke,
close: true
)
return (f, bounds2)
}
/// Draws a bit extender
@ -45,54 +29,28 @@
/// - h-ratio (ratio): The height ratio of the left side relative to the full height
/// - align-out (bool): If true, the output and input ports are aligned, otherwise, the output port is centered on the right side
#let extender(
x: none,
y: none,
w: none,
h: none,
name: none,
name-anchor: "center",
fill: none,
stroke: black + 1pt,
id: "",
h-ratio: 75%,
l-ratio: 75%,
align-out: true,
debug: (
ports: false
)
..args
) = {
let ports = (
west: (
(id: "in"),
),
east: (
(id: "out"),
)
west: ((id: "in"),),
east: ((id: "out"),)
)
let out-pct = if align-out {h-ratio / 2} else {50%}
let ports-y = (
"in": (h) => {h - h * (h-ratio / 200%)},
"out": (h) => {h - h * (out-pct / 100%)}
let out-pct = if align-out {l-ratio / 2} else {50%}
let ports-pos = (
"east": (l, i) => {l - l * (out-pct / 100%)}
)
element.elmt(
draw-shape: draw-shape.with(h-ratio: h-ratio, align-out: align-out),
x: x,
y: y,
w: w,
h: h,
name: name,
name-anchor: name-anchor,
return element.elmt(
cls: "extender",
draw-shape: draw-shape,
ports: ports,
auto-ports: false,
ports-y: ports-y,
fill: fill,
stroke: stroke,
id: id,
debug: debug
ports-pos: ports-pos,
extra: (
l-ratio: l-ratio,
align-out: align-out
),
..args
)
let in-pos = (rel: (0, h * (h-ratio / 200%)), to: id+".south-west")
let out-pos = (id+".south-east", out-pct, id+".north-east")
add-port(id, "west", ports.west.first(), in-pos)
add-port(id, "east", ports.east.first(), out-pos)
}

View File

@ -13,23 +13,12 @@
tr: tr2,
br: br2,
))
let f = {
draw.merge-path(
inset: 0.5em,
fill: elmt.fill,
stroke: elmt.stroke,
close: true,
draw.line(bounds2.tl, bounds2.tr, bounds2.br, bounds2.bl)
)
draw.anchor("north", bounds2.t)
draw.anchor("south", bounds2.b)
draw.anchor("west", bounds2.l)
draw.anchor("east", bounds2.r)
draw.anchor("north-west", bounds2.tl)
draw.anchor("north-east", bounds2.tr)
draw.anchor("south-east", bounds2.br)
draw.anchor("south-west", bounds2.bl)
}
let f = draw.line(
bounds2.tl, bounds2.tr, bounds2.br, bounds2.bl,
close: true,
fill: elmt.fill,
stroke: elmt.stroke
)
return (f, bounds2)
}

View File

@ -9,7 +9,9 @@
}
}
}
panic("Unknown port " + port + " on element " + elmt.id)
panic(
"Unknown port '" + port + "' on element '" + elmt.id + "', could not automatically determine side"
)
}
#let get-port-idx(elmt, port, side: auto) = {
@ -125,7 +127,7 @@
elmt,
bounds
) = {
let debug = elmt.debug.ports
let debug = elmt.debug.at("ports", default: false)
if type(elmt.ports) != dictionary {
return
@ -142,7 +144,7 @@
pt0.at(1) - pt1.at(1)
}
for (i, port) in side-ports.enumerate() {
let offset = get-port-pos(elmt, bounds, side, port, i)
let offset = get-port-pos(elmt, bounds, side, port.id, i)
let pos = (pt0, offset, pt1)
let offset-prev = if type(offset) == ratio {