diff --git a/doc/examples.typ b/doc/examples.typ index a38df0d..423a3c9 100644 --- a/doc/examples.typ +++ b/doc/examples.typ @@ -74,3 +74,43 @@ wire.stub("p", "east", name: "east", vertical: true) wire.stub("p", "south", name: "south", length: 15pt) wire.stub("p", "west", name: "west", length: 3em) ```) + +#let gate-and = example(``` +gates.gate-and(x: 0, y: 0, w: 1.5, h: 1.5) +gates.gate-and(x: 3, y: 0, w: 1.5, h: 1.5, inverted: "all") +```, vertical: true) + +#let gate-nand = example(``` +gates.gate-nand(x: 0, y: 0, w: 1.5, h: 1.5) +gates.gate-nand(x: 3, y: 0, w: 1.5, h: 1.5, inverted: "all") +```, vertical: true) + +#let gate-buf = example(``` +gates.gate-buf(x: 0, y: 0, w: 1.5, h: 1.5) +gates.gate-buf(x: 3, y: 0, w: 1.5, h: 1.5, inverted: "all") +```, vertical: true) + +#let gate-not = example(``` +gates.gate-not(x: 0, y: 0, w: 1.5, h: 1.5) +gates.gate-not(x: 3, y: 0, w: 1.5, h: 1.5, inverted: "all") +```, vertical: true) + +#let gate-or = example(``` +gates.gate-or(x: 0, y: 0, w: 1.5, h: 1.5) +gates.gate-or(x: 3, y: 0, w: 1.5, h: 1.5, inverted: "all") +```, vertical: true) + +#let gate-nor = example(``` +gates.gate-nor(x: 0, y: 0, w: 1.5, h: 1.5) +gates.gate-nor(x: 3, y: 0, w: 1.5, h: 1.5, inverted: "all") +```, vertical: true) + +#let gate-xor = example(``` +gates.gate-xor(x: 0, y: 0, w: 1.5, h: 1.5) +gates.gate-xor(x: 3, y: 0, w: 1.5, h: 1.5, inverted: "all") +```, vertical: true) + +#let gate-xnor = example(``` +gates.gate-xnor(x: 0, y: 0, w: 1.5, h: 1.5) +gates.gate-xnor(x: 3, y: 0, w: 1.5, h: 1.5, inverted: "all") +```, vertical: true) \ No newline at end of file diff --git a/gallery/test3.pdf b/gallery/test3.pdf index bed94cd..254a340 100644 Binary files a/gallery/test3.pdf and b/gallery/test3.pdf differ diff --git a/manual.pdf b/manual.pdf index 06521eb..8ba1aa8 100644 Binary files a/manual.pdf and b/manual.pdf differ diff --git a/manual.typ b/manual.typ index 08a5137..4c5fb76 100644 --- a/manual.typ +++ b/manual.typ @@ -4,6 +4,7 @@ #import "doc/examples.typ" #import "src/circuit.typ": circuit #import "src/element.typ" +#import "src/gates.typ" #import "src/util.typ" #import "src/wire.typ" @@ -135,4 +136,24 @@ Simply import #link("src/lib.typ") and call the `circuit` function: #tidy.show-module(element-docs, sort-functions: false) -#(tidy.utilities.get-style-functions(tidy.styles.default).show-reference)(label("wirewire()"), "test") \ No newline at end of file +#let gates-docs = tidy.parse-module( + read("src/elements/logic/gate.typ") + "\n" + + read("src/elements/logic/and.typ") + "\n" + + read("src/elements/logic/buf.typ") + "\n" + + read("src/elements/logic/or.typ") + "\n" + + read("src/elements/logic/xor.typ"), + name: "gates", + require-all-parameters: false, + scope: ( + element: element, + circuit: circuit, + gates: gates, + draw: draw, + wire: wire, + tidy: tidy, + examples: examples, + doc-ref: doc-ref + ) +) + +#tidy.show-module(gates-docs, sort-functions: false) \ No newline at end of file diff --git a/src/element.typ b/src/element.typ index aee6cd1..69bf876 100644 --- a/src/element.typ +++ b/src/element.typ @@ -10,6 +10,4 @@ #import "elements/logic/and.typ": gate-and, gate-nand #import "elements/logic/or.typ": gate-or, gate-nor #import "elements/logic/xor.typ": gate-xor, gate-xnor -#import "elements/logic/buf.typ": gate-buf, gate-not -/* -*/ \ No newline at end of file +#import "elements/logic/buf.typ": gate-buf, gate-not \ No newline at end of file diff --git a/src/elements/logic/and.typ b/src/elements/logic/and.typ index 3df22a4..0865a81 100644 --- a/src/elements/logic/and.typ +++ b/src/elements/logic/and.typ @@ -27,6 +27,10 @@ return (f, tl, tr, br, bl) } +/// Draws an AND gate. This function is also available as `element.gate-and()` +/// +/// For parameters, see #doc-ref("gates.gate") +/// #examples.gate-and #let gate-and( x: none, y: none, @@ -56,6 +60,10 @@ ) } +/// Draws an NAND gate. This function is also available as `element.gate-nand()` +/// +/// For parameters, see #doc-ref("gates.gate") +/// #examples.gate-nand #let gate-nand( x: none, y: none, @@ -79,7 +87,7 @@ fill: fill, stroke: stroke, id: id, - inverted: inverted + ("out",), + inverted: if inverted != "all" {inverted + ("out",)} else {inverted}, debug: debug ) } \ No newline at end of file diff --git a/src/elements/logic/buf.typ b/src/elements/logic/buf.typ index 87d8dc9..f46fcdb 100644 --- a/src/elements/logic/buf.typ +++ b/src/elements/logic/buf.typ @@ -24,6 +24,10 @@ return (f, tl, tr, br, bl) } +/// Draws a buffer gate. This function is also available as `element.gate-buf()` +/// +/// For parameters, see #doc-ref("gates.gate") +/// #examples.gate-buf #let gate-buf( x: none, y: none, @@ -53,6 +57,10 @@ ) } +/// Draws a NOT gate. This function is also available as `element.gate-not()` +/// +/// For parameters, see #doc-ref("gates.gate") +/// #examples.gate-not #let gate-not(x: none, y: none, w: none, @@ -75,7 +83,7 @@ fill: fill, stroke: stroke, id: id, - inverted: inverted + ("out",), + inverted: if inverted != "all" {inverted + ("out",)} else {inverted}, debug: debug ) } \ No newline at end of file diff --git a/src/elements/logic/gate.typ b/src/elements/logic/gate.typ index 5fb282a..b030b4f 100644 --- a/src/elements/logic/gate.typ +++ b/src/elements/logic/gate.typ @@ -7,6 +7,21 @@ return (f, tl, tr, br, bl) } + +/// Draws a logic gate. This function is also available as `element.gate()` +/// +/// - draw-shape (function): see #doc-ref("element.elmt") +/// - x (number, dictionary): see #doc-ref("element.elmt") +/// - y (number, dictionary): see #doc-ref("element.elmt") +/// - w (number): see #doc-ref("element.elmt") +/// - h (number): see #doc-ref("element.elmt") +/// - inputs (int): The number of inputs +/// - fill (none, color): see #doc-ref("element.elmt") +/// - stroke (stroke): see #doc-ref("element.elmt") +/// - id (str): see #doc-ref("element.elmt") +/// - inverted (str, array): Either "all" or an array of port ids to display as inverted +/// - inverted-radius (number): The radius of inverted ports dot +/// - debug (dictionary): see #doc-ref("element.elmt") #let gate( draw-shape: default-draw-shape, x: none, @@ -18,6 +33,7 @@ stroke: black + 1pt, id: "", inverted: (), + inverted-radius: 0.2, debug: ( ports: false ) @@ -65,14 +81,11 @@ // Workaround because CeTZ needs to have all draw functions in the body let func = {} (func, tl, tr, br, bl) = draw-shape(id, tl, tr, br, bl, fill, stroke) - { - //draw.rect(tl, br) - func - } + func - let space = 100% / (inputs + 1) + let space = 100% / inputs for i in range(inputs) { - let pct = (i + 1) * space + let pct = (i + 0.5) * space let a = (tl, pct, bl) let b = (tr, pct, br) let int-name = id + "i" + str(i) @@ -84,8 +97,8 @@ let port-name = "in" + str(i) let port-pos = int-name + ".0" if inverted == "all" or port-name in inverted { - draw.circle(port-pos, radius: .2, anchor: "east", stroke: stroke) - port-pos = (rel: (-.4, 0), to: port-pos) + draw.circle(port-pos, radius: inverted-radius, anchor: "east", stroke: stroke) + port-pos = (rel: (-2 * inverted-radius, 0), to: port-pos) } add-port( id, "west", @@ -96,32 +109,12 @@ let out-pos = id + ".east" if inverted == "all" or "out" in inverted { - draw.circle(out-pos, radius: .2, anchor: "west", stroke: stroke) - out-pos = (rel: (.4, 0), to: out-pos) + draw.circle(out-pos, radius: inverted-radius, anchor: "west", stroke: stroke) + out-pos = (rel: (2 * inverted-radius, 0), to: out-pos) } add-port( id, "east", (id: "out"), out-pos, debug: debug.ports ) -}) -/* { - let ports = (west: (), east: ((id: "out"),)) - for i in range(inputs) { - ports.west.push((id: "in"+str(i))) - } - - element.elmt( - draw-shape: draw-shape, - x: x, - y: y, - w: w, - h: h, - ports: ports, - fill: fill, - stroke: stroke, - id: id, - auto-ports: true, - debug: debug - ) -} \ No newline at end of file +}) \ No newline at end of file diff --git a/src/elements/logic/or.typ b/src/elements/logic/or.typ index 4f44fb7..67bb946 100644 --- a/src/elements/logic/or.typ +++ b/src/elements/logic/or.typ @@ -38,6 +38,10 @@ return (f, tl, tr, br, bl) } +/// Draws an OR gate. This function is also available as `element.gate-or()` +/// +/// For parameters, see #doc-ref("gates.gate") +/// #examples.gate-or #let gate-or( x: none, y: none, @@ -67,6 +71,10 @@ ) } +/// Draws a NOR gate. This function is also available as `element.gate-nor()` +/// +/// For parameters, see #doc-ref("gates.gate") +/// #examples.gate-nor #let gate-nor( x: none, y: none, @@ -90,7 +98,7 @@ fill: fill, stroke: stroke, id: id, - inverted: inverted + ("out",), + inverted: if inverted != "all" {inverted + ("out",)} else {inverted}, debug: debug ) } \ No newline at end of file diff --git a/src/elements/logic/xor.typ b/src/elements/logic/xor.typ index 3b4eb70..42718f6 100644 --- a/src/elements/logic/xor.typ +++ b/src/elements/logic/xor.typ @@ -45,6 +45,10 @@ return (f, tl, tr, br, bl) } +/// Draws a XOR gate. This function is also available as `element.gate-xor()` +/// +/// For parameters, see #doc-ref("gates.gate") +/// #examples.gate-xor #let gate-xor( x: none, y: none, @@ -74,6 +78,10 @@ ) } +/// Draws a XNOR gate. This function is also available as `element.gate-xnor()` +/// +/// For parameters, see #doc-ref("gates.gate") +/// #examples.gate-xnor #let gate-xnor( x: none, y: none, @@ -97,7 +105,7 @@ fill: fill, stroke: stroke, id: id, - inverted: inverted + ("out",), + inverted: if inverted != "all" {inverted + ("out",)} else {inverted}, debug: debug ) } \ No newline at end of file diff --git a/src/gates.typ b/src/gates.typ new file mode 100644 index 0000000..85ba178 --- /dev/null +++ b/src/gates.typ @@ -0,0 +1,5 @@ +#import "elements/logic/gate.typ": gate +#import "elements/logic/and.typ": gate-and, gate-nand +#import "elements/logic/or.typ": gate-or, gate-nor +#import "elements/logic/xor.typ": gate-xor, gate-xnor +#import "elements/logic/buf.typ": gate-buf, gate-not \ No newline at end of file diff --git a/src/lib.typ b/src/lib.typ index d891428..ad137ea 100644 --- a/src/lib.typ +++ b/src/lib.typ @@ -2,5 +2,6 @@ #import "circuit.typ": circuit #import "element.typ" +#import "gates.typ" #import "util.typ" #import "wire.typ" \ No newline at end of file