15 Commits

34 changed files with 1813 additions and 1 deletions

View File

@ -1,3 +1,44 @@
# rivet-typst
Register / Instruction Visualizer & Explainer Tool with Typst, using CeTZ
RIVET _(Register / Instruction Visualizer & Explainer Tool)_ is a [Typst](https://typst.app) package for visualizing binary instructions or describing the contents of a register, using the [CeTZ](https://typst.app/universe/package/cetz) package.
It is based on the [homonymous Python script](https://git.kb28.ch/HEL/rivet/)
## Examples
<table>
<tr>
<td>
<a href="./gallery/example1.typ">
<img src="./gallery/example1.png" width="1000px">
</a>
</td>
</tr>
<tr>
<td>A bit of eveything</td>
</tr>
<tr>
<td>
<a href="./gallery/example2.typ">
<img src="./gallery/example2.png" width="1000px">
</a>
</td>
</tr>
<tr>
<td>RISC-V memory instructions (blueprint)</td>
</tr>
</table>
*Click on the example image to jump to the code.*
## Usage
For information, see the [manual](manual.pdf)
To use this package, simply import `schema` [src/lib.typ](src/lib.typ) and call `schema.load` to parse a schema description. Then use `schema.render` to render it, et voilà !
```typ
#import "src/lib.typ": schema
#let doc = schema.load("path/to/schema.yaml")
#schema.render(doc)
```
## Installing
> TODO

61
docs/config.typ Normal file
View File

@ -0,0 +1,61 @@
/// Creates a dictionary of all configuration parameters
///
/// - default-font-family (str): The default font family
/// - default font-size (length): The absolute default font size
/// - italic-font-family (str): The italic font family (for value descriptions)
/// - italic-font-size (length): The absolute italic font size
/// - background (color): The diagram background color
/// - text-color (color): The default color used to display text
/// - link-color (color): The color used to display links and arrows
/// - bit-i-color (color): The color used to display bit indices
/// - border-color (color): The color used to display borders
/// - bit-width (float): The width of a bit
/// - bit-height (float): The height of a bit
/// - description-margin (float): The margin between descriptions
/// - dash-length (float): The length of individual dashes (for dashed lines)
/// - dash-space (float): The space between two dashes (for dashed lines)
/// - arrow-size (float): The size of arrow heads
/// - margins (tuple[float]): TODO -> remove
/// - arrow-margin (float): The margin between arrows and the structures they link
/// - values-gap (float): The gap between individual values
/// - arrow-label-distance (float): The distance between arrows and their labels
/// - force-descs-on-side (bool): If true, descriptions are placed on the side of the structure, otherwise, they are placed as close as possible to the bit
/// - left-labels (bool): If true, descriptions are put on the left, otherwise, they default to the right hand side
/// - width (float): TODO -> remove
/// - height (float): TODO -> remove
/// - full-page (bool): If true, the page will be resized to fit the diagram and take the background color
/// -> dictionary
#let config(
default-font-family: "Ubuntu Mono",
default-font-size: 15pt,
italic-font-family: "Ubuntu Mono",
italic-font-size: 12pt,
background: white,
text-color: black,
link-color: black,
bit-i-color: black,
border-color: black,
bit-width: 30,
bit-height: 30,
description-margin: 10,
dash-length: 6,
dash-space: 4,
arrow-size: 10,
margins: (20, 20, 20, 20),
arrow-margin: 4,
values-gap: 5,
arrow-label-distance: 5,
force-descs-on-side: false,
left-labels: false,
width: 1200,
height: 800,
full-page: false
) = {}
/// Dark theme config
/// - ..args (any): see #doc-ref("config.config")
#let dark(..args) = {}
/// Blueprint theme config
/// - ..args (any): see #doc-ref("config.config")
#let blueprint(..args) = {}

73
docs/examples.typ Normal file
View File

@ -0,0 +1,73 @@
#import "@preview/cetz:0.2.2": draw
#import "../src/lib.typ": schema
#import "../src/util.typ"
#let example-preamble = "import \"../src/lib.typ\": *;"
#let example(src, show-src: true, vertical: false, fill: true) = {
src = src.text.trim()
let full-src = example-preamble + src
let body = eval(full-src)
block(width: 100%,
align(center,
box(
stroke: black + 1pt,
radius: .5em,
fill: if fill {yellow.lighten(80%)} else {none},
if show-src {
let src-block = align(left, raw(src, lang: "typc"))
table(
columns: if vertical {1} else {2},
inset: 1em,
align: horizon + center,
stroke: none,
body,
if vertical {table.hline()} else {table.vline()}, src-block
)
} else {
table(
inset: 1em,
body
)
}
)
)
)
}
#let config-config = example(raw("
let ex = schema.load(```yaml
structures:
main:
bits: 4
ranges:
3-0:
name: default
```)
schema.render(ex, config: config.config())
"))
#let config-dark = example(raw("
let ex = schema.load(```yaml
structures:
main:
bits: 4
ranges:
3-0:
name: dark
```)
schema.render(ex, config: config.dark())
"))
#let config-blueprint = example(raw("
let ex = schema.load(```yaml
structures:
main:
bits: 4
ranges:
3-0:
name: blueprint
```)
schema.render(ex, config: config.blueprint())
"))

14
docs/schema.typ Normal file
View File

@ -0,0 +1,14 @@
/// Loads a schema from a file or a raw block.
/// This function returns a dictionary of structures
///
/// Supported formats: #schema.valid-extensions.map(e => raw("." + e)).join(", ")
/// - path-or-schema (str, raw): If it is a string, defines the path to load. \
/// If it is a raw block, its content is directly parsed (the block's language will define the format to use)
/// -> dictionary
#let load(path-or-schema) = {}
/// Renders the given schema
/// This functions
/// - structures (dictionary): A schema dictionary, as returned by #doc-ref("schema.load")
/// - config (auto, dictionary): The configuration parameters, as returned by #doc-ref("config.config")
#let render(structures, config: auto)

BIN
gallery/example1.pdf Normal file

Binary file not shown.

BIN
gallery/example1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 378 KiB

6
gallery/example1.typ Normal file
View File

@ -0,0 +1,6 @@
#import "../src/lib.typ": schema, config
#let example = schema.load("/gallery/example1.yaml")
#schema.render(example, config: config.config(
full-page: true
))

73
gallery/example1.yaml Normal file
View File

@ -0,0 +1,73 @@
structures:
main:
bits: 32
ranges:
31-28:
name: cond
27:
name: 0
26:
name: 1
25:
name: I
24:
name: P
description: pre / post indexing bit
values:
0: post, add offset after transfer
1: pre, add offset before transfer
23:
name: U
description: up / down bit
values:
0: down, subtract offset from base
1: up, addition offset to base
22:
name: B
description: byte / word bit
values:
0: transfer word quantity
1: transfer byte quantity
21:
name: W
description: write-back bit
values:
0: no write-back
1: write address into base
20:
name: L
description: load / store bit
values:
0: store to memory
1: load from memory
19-16:
name: Rn
description: base register
15-12:
name: Rd
description: source / destination register
11-0:
name: offset
depends-on: 25
values:
0:
description: offset is an immediate value
structure: immediateOffset
1:
description: offset is a register
structure: registerOffset
immediateOffset:
bits: 12
ranges:
11-0:
name: 12-bit immediate offset
description: unsigned number
registerOffset:
bits: 12
ranges:
11-4:
name: shift
description: shift applied to Rm
3-0:
name: Rm
description: offset register

BIN
gallery/example2.pdf Normal file

Binary file not shown.

BIN
gallery/example2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 303 KiB

7
gallery/example2.typ Normal file
View File

@ -0,0 +1,7 @@
#import "../src/lib.typ": schema, config
#let example = schema.load("/gallery/example2.yaml")
#schema.render(example, config: config.blueprint(
full-page: true,
left-labels: true
))

78
gallery/example2.yaml Normal file
View File

@ -0,0 +1,78 @@
structures:
main:
bits: 32
ranges:
31-20:
name: src
depends-on: 5
values:
0:
description:
structure: srcImmediate
1:
description:
structure: srcRegister
19-15:
name: rs1
14-12:
name: funct3
description: function modifier
values:
000: byte
001: half-word
"010": word
100: upper byte (load only)
101: upper half (load only)
11-7:
name: dst
depends-on: 5
values:
0:
description:
structure: dstRegister
1:
description:
structure: dstImmediate
6:
name: 0
5:
name: I
4:
name: 0
3:
name: 0
2:
name: 0
1:
name: 1
0:
name: 1
srcImmediate:
bits: 12
ranges:
11-0:
name: src
description: source memory address
srcRegister:
bits: 12
ranges:
11-5:
name: dstU
description: destination address upper bits
4-0:
name: rs2
description: source register
dstImmediate:
bits: 5
ranges:
4-0:
name: destL
description: destination address lower bits
dstRegister:
bits: 5
ranges:
4-0:
name: rd
description: destination register

View File

@ -0,0 +1,60 @@
structures:
main:
bits: 32
ranges:
31-20:
name: op2
depends-on: 5
values:
0:
description: second operand is an immediate value
structure: immediateOp
1:
description: second operand is a register
structure: registerOp
19-15:
name: rs1
14-12:
name: funct3
description: operation
values:
000: add / sub
100: xor
110: or
111: and
001: sl
101: sr
11-7:
name: rd
6:
name: 0
5:
name: I
4:
name: 1
3:
name: 0
2:
name: 0
1:
name: 1
0:
name: 1
immediateOp:
bits: 12
ranges:
11-0:
name: 12-bit immediate value
description: signed number
registerOp:
bits: 12
ranges:
11-5:
name: funct7
description: function modifier
values:
0000000: default (add, srl)
"0100000": sub, sra
4-0:
name: rs2
description: second register operand

View File

@ -0,0 +1,34 @@
structures:
main:
bits: 32
ranges:
31-25:
name: imm
24-20:
name: rs2
19-15:
name: rs1
14-12:
name: funct3
description: function modifier
values:
000: if equal
001: if not equal
100: if less
101: if greater or equal
11-7:
name: imm
6:
name: 1
5:
name: 1
4:
name: 0
3:
name: 0
2:
name: 0
1:
name: 1
0:
name: 1

View File

@ -0,0 +1,78 @@
structures:
main:
bits: 32
ranges:
31-20:
name: src
depends-on: 5
values:
0:
description:
structure: srcImmediate
1:
description:
structure: srcRegister
19-15:
name: rs1
14-12:
name: funct3
description: function modifier
values:
000: byte
001: half-word
"010": word
100: upper byte (load only)
101: upper half (load only)
11-7:
name: dst
depends-on: 5
values:
0:
description:
structure: dstRegister
1:
description:
structure: dstImmediate
6:
name: 0
5:
name: I
4:
name: 0
3:
name: 0
2:
name: 0
1:
name: 1
0:
name: 1
srcImmediate:
bits: 12
ranges:
11-0:
name: src
description: source memory address
srcRegister:
bits: 12
ranges:
11-5:
name: dstU
description: destination address upper bits
4-0:
name: rs2
description: source register
dstImmediate:
bits: 5
ranges:
4-0:
name: destL
description: destination address lower bits
dstRegister:
bits: 5
ranges:
4-0:
name: rd
description: destination register

BIN
gallery/riscv/riscv.pdf Normal file

Binary file not shown.

14
gallery/riscv/riscv.typ Normal file
View File

@ -0,0 +1,14 @@
#import "../../src/lib.typ": *
#let conf = config.config(
full-page: true,
left-labels: true
)
#let alu = schema.load("/gallery/riscv/alu_instr.yaml")
#schema.render(alu, config: conf)
#let branch = schema.load("/gallery/riscv/branch_instr.yaml")
#schema.render(branch, config: conf)
#let mem = schema.load("/gallery/riscv/mem_instr.yaml")
#schema.render(mem, config: conf)

105
gallery/test.json Normal file
View File

@ -0,0 +1,105 @@
{
"structures": {
"main": {
"bits": 32,
"ranges": {
"31-28": {
"name": "cond"
},
"27": {
"name": "0"
},
"26": {
"name": "1"
},
"25": {
"name": "I"
},
"24": {
"name": "P",
"description": "pre / post indexing bit",
"values": {
"0": "post, add offset after transfer",
"1": "pre, add offset before transfer"
}
},
"23": {
"name": "U",
"description": "up / down bit",
"values": {
"0": "down, subtract offset from base",
"1": "up, addition offset to base"
}
},
"22": {
"name": "B",
"description": "byte / word bit",
"values": {
"0": "transfer word quantity",
"1": "transfer byte quantity"
}
},
"21": {
"name": "W",
"description": "write-back bit",
"values": {
"0": "no write-back",
"1": "write address into base"
}
},
"20": {
"name": "L",
"description": "load / store bit",
"values": {
"0": "store to memory",
"1": "load from memory"
}
},
"19-16": {
"name": "Rn",
"description": "base register"
},
"15-12": {
"name": "Rd",
"description": "source / destination register"
},
"11-0": {
"name": "offset",
"depends-on": "25",
"values": {
"0": {
"description": "offset is an immediate value",
"structure": "immediateOffset"
},
"1": {
"description": "offset is a register",
"structure": "registerOffset"
}
}
}
}
},
"immediateOffset": {
"bits": 12,
"ranges": {
"11-0": {
"name": "12-bit immediate offset",
"description": "unsigned number"
}
}
},
"registerOffset": {
"bits": 12,
"ranges": {
"11-4": {
"name": "shift",
"description": "shift applied to Rm"
},
"3-0": {
"name": "Rm",
"description": "offset register"
}
}
}
}
}

BIN
gallery/test.pdf Normal file

Binary file not shown.

28
gallery/test.typ Normal file
View File

@ -0,0 +1,28 @@
#import "../src/lib.typ": *
#let test-yaml = schema.load("/gallery/test.yaml")
#schema.render(test-yaml, config: config.config(
full-page: true
))
#let test-json = schema.load("/gallery/test.json")
#schema.render(test-json, config: config.blueprint(
full-page: true
))
#let test-xml = schema.load("/gallery/test.xml")
#schema.render(test-xml, config: config.dark(
full-page: true
))
#let test-raw = schema.load(```yaml
structures:
main:
bits: 4
ranges:
3-0:
name: test
```)
#schema.render(test-raw, config: config.config(
full-page: true
))

68
gallery/test.xml Normal file
View File

@ -0,0 +1,68 @@
<schema>
<structure id="main" bits="32">
<range start="28" end="31" name="cond"></range>
<range start="27" end="27" name="0"></range>
<range start="26" end="26" name="1"></range>
<range start="25" end="25" name="I"></range>
<range start="24" end="24" name="P">
<description>pre / post indexing bit</description>
<values>
<case value="0">post, add offset after transfer</case>
<case value="1">pre, add offset before transfer</case>
</values>
</range>
<range start="23" end="23" name="U">
<description>up / down bit</description>
<values>
<case value="0">down, subtract offset from base</case>
<case value="1">up, addition offset to base</case>
</values>
</range>
<range start="22" end="22" name="B">
<description>byte / word bit</description>
<values>
<case value="0">transfer word quantity</case>
<case value="1">transfer byte quantity</case>
</values>
</range>
<range start="21" end="21" name="W">
<description>write-back bit</description>
<values>
<case value="0">no write-back</case>
<case value="1">write address into base</case>
</values>
</range>
<range start="20" end="20" name="L">
<description>load / store bit</description>
<values>
<case value="0">store to memory</case>
<case value="1">load from memory</case>
</values>
</range>
<range start="16" end="19" name="Rn">
<description>base register</description>
</range>
<range start="12" end="15" name="Rd">
<description>source / destination register</description>
</range>
<range start="0" end="11" name="offset" depends-on="25">
<values>
<case value="0" structure="immediateOffset">offset is an immediate value</case>
<case value="1" structure="registerOffset">offset is a register</case>
</values>
</range>
</structure>
<structure id="immediateOffset" bits="12">
<range start="0" end="11" name="12-bit immediate offset">
<description>unsigned number</description>
</range>
</structure>
<structure id="registerOffset" bits="12">
<range start="4" end="11" name="shift">
<description>shift applied to Rm</description>
</range>
<range start="0" end="3" name="Rm">
<description>offset register</description>
</range>
</structure>
</schema>

73
gallery/test.yaml Normal file
View File

@ -0,0 +1,73 @@
structures:
main:
bits: 32
ranges:
31-28:
name: cond
27:
name: 0
26:
name: 1
25:
name: I
24:
name: P
description: pre / post indexing bit
values:
0: post, add offset after transfer
1: pre, add offset before transfer
23:
name: U
description: up / down bit
values:
0: down, subtract offset from base
1: up, addition offset to base
22:
name: B
description: byte / word bit
values:
0: transfer word quantity
1: transfer byte quantity
21:
name: W
description: write-back bit
values:
0: no write-back
1: write address into base
20:
name: L
description: load / store bit
values:
0: store to memory
1: load from memory
19-16:
name: Rn
description: base register
15-12:
name: Rd
description: source / destination register
11-0:
name: offset
depends-on: 25
values:
0:
description: offset is an immediate value
structure: immediateOffset
1:
description: offset is a register
structure: registerOffset
immediateOffset:
bits: 12
ranges:
11-0:
name: 12-bit immediate offset
description: unsigned number
registerOffset:
bits: 12
ranges:
11-4:
name: shift
description: shift applied to Rm
3-0:
name: Rm
description: offset register

BIN
manual.pdf Normal file

Binary file not shown.

79
manual.typ Normal file
View File

@ -0,0 +1,79 @@
#import "@preview/tidy:0.3.0"
#import "src/lib.typ"
#import "src/schema.typ"
#import "docs/examples.typ"
#set heading(numbering: (..num) => if num.pos().len() < 4 {
numbering("1.1", ..num)
})
#{
outline(indent: true, depth: 3)
}
#set page(numbering: "1/1", header: align(right)[rivet #sym.dash.em v#lib.version])
#let doc-ref(target, full: false, var: false) = {
let (module, func) = target.split(".")
let label-name = module + func
let display-name = func
if full {
display-name = target
}
if not var {
label-name += "()"
display-name += "()"
}
link(label(label-name))[#display-name]
}
#show link: set text(blue)
= Introduction
This package provides a way to make beautiful register diagrams using the CeTZ package. It can be used to document Assembly instructions or binary registers
This is a port of the #link("https://git.kb28.ch/HEL/rivet")[homonymous Python script] for Typst. For more information on the schema format, please check out the original project's #link("https://git.kb28.ch/HEL/rivet/src/branch/main/format.md")[format.md]
= Usage
Simply import `schema` from #link("src/lib.typ") and call `schema.load` to parse a schema description. Then use `schema.render` to render it, et voilà !
#pad(left: 1em)[```typ
#import "src/lib.typ": schema
#let doc = schema.load("path/to/schema.yaml")
#schema.render(doc)
```]
= Config presets
Aside from the default config, some example presets are also provided:
- #doc-ref("config.config", full: true): the default theme, black on white
#examples.config-config
- #doc-ref("config.dark", full: true): a dark theme, with white text and lines on a black background
#examples.config-dark
- #doc-ref("config.blueprint", full: true): a blueprint theme, with white text and lines on a blue background
#examples.config-blueprint
#pagebreak(weak: true)
= Reference
#let doc-config = tidy.parse-module(
read("docs/config.typ"),
name: "config",
scope: (
doc-ref: doc-ref
)
)
#tidy.show-module(doc-config, sort-functions: false)
#pagebreak()
#let doc-schema = tidy.parse-module(
read("docs/schema.typ"),
name: "schema",
scope: (
schema: schema,
doc-ref: doc-ref
)
)
#tidy.show-module(doc-schema, sort-functions: false)

78
src/config.typ Normal file
View File

@ -0,0 +1,78 @@
#let config(
default-font-family: "Ubuntu Mono",
default-font-size: 15pt,
italic-font-family: "Ubuntu Mono",
italic-font-size: 12pt,
background: white,
text-color: black,
link-color: black,
bit-i-color: black,
border-color: black,
bit-width: 30,
bit-height: 30,
description-margin: 10,
dash-length: 6,
dash-space: 4,
arrow-size: 10,
margins: (20, 20, 20, 20),
arrow-margin: 4,
values-gap: 5,
arrow-label-distance: 5,
force-descs-on-side: false,
left-labels: false,
width: 1200,
height: 800,
full-page: false
) = {
return (
default-font-family: default-font-family,
default-font-size: default-font-size,
italic-font-family: italic-font-family,
italic-font-size: italic-font-size,
background: background,
text-color: text-color,
link-color: link-color,
bit-i-color: bit-i-color,
border-color: border-color,
bit-width: bit-width,
bit-height: bit-height,
description-margin: description-margin,
dash-length: dash-length,
dash-space: dash-space,
arrow-size: arrow-size,
margins: margins,
arrow-margin: arrow-margin,
values-gap: values-gap,
arrow-label-distance: arrow-label-distance,
force-descs-on-side: force-descs-on-side,
left-labels: left-labels,
width: width,
height: height,
full-page: full-page
)
}
#let dark = config.with(
background: rgb(24, 24, 24),
text-color: rgb(216, 216, 216),
link-color: rgb(150, 150, 150),
bit-i-color: rgb(180, 180, 180),
border-color: rgb(180, 180, 180)
)
#let blueprint = config.with(
background: rgb(53, 77, 158),
text-color: rgb(231, 236, 249),
link-color: rgb(169, 193, 228),
bit-i-color: rgb(214, 223, 244),
border-color: rgb(214, 223, 244)
)
#let transparent = config.with(
background: rgb(0, 0, 0, 0),
text-color: rgb(128, 128, 128),
link-color: rgb(128, 128, 128),
bit-i-color: rgb(128, 128, 128),
border-color: rgb(128, 128, 128)
)

4
src/lib.typ Normal file
View File

@ -0,0 +1,4 @@
#let version = version((0,0,1))
#import "config.typ"
#import "schema.typ"

65
src/range.typ Normal file
View File

@ -0,0 +1,65 @@
#import "util.typ"
#let key(start, end) = {
return str(start) + "->" + str(end)
}
#let bits(range) = {
return range.end - range.start + 1
}
#let parse-span(span) = {
let start-end = span.split("-")
if start-end.len() == 1 {
start-end.push(start-end.first())
}
let start = int(start-end.last())
let end = int(start-end.first())
return (start, end)
}
#let make(
start,
end,
name,
description: "",
values: none,
depends-on: none
) = {
return (
start: start,
end: end,
name: name,
description: description,
values: values,
depends-on: depends-on,
last-value-y: -1
)
}
#let load(start, end, data) = {
let values = none
let bits = end - start + 1
if "values" in data {
values = (:)
for (val, desc) in data.values {
val = util.z-fill(val, bits)
values.insert(val, desc)
}
}
let depends-on = data.at("depends-on", default: none)
if depends-on != none {
depends-on = parse-span(str(depends-on))
}
return make(
start,
end,
str(data.name),
description: data.at("description", default: ""),
values: values,
depends-on: depends-on
)
}

485
src/renderer.typ Normal file
View File

@ -0,0 +1,485 @@
#import "@preview/cetz:0.2.2": canvas, draw
#import "range.typ" as rng
#import "structure.typ"
#import "vec.typ"
#let draw-rect(color, x, y, width, height, thickness: 0) = {
let fill = none
let stroke = color + thickness * 1pt
if thickness == 0 {
fill = color
stroke = none
}
draw.rect((x, -y), (x + width, -y - height), fill: fill, stroke: stroke)
}
#let draw-text(
txt,
color,
x,
y,
anchor: "center",
font: none,
italic: false,
size: 1em,
fill: none
) = {
let text-params = (:)
if font != none {
text-params.insert("font", font)
}
if italic {
text-params.insert("style", "italic")
}
let content-params = (:)
if fill != none {
content-params.insert("fill", fill)
content-params.insert("frame", "rect")
content-params.insert("padding", (4pt, 0pt))
}
draw.content(
(x, -y),
text(txt, fill: color, size: size, ..text-params),
anchor: anchor,
stroke: none,
..content-params
)
}
#let draw-line(color, a, b) = {
let (x0, y0) = a
let (x1, y1) = b
draw.line((x0, -y0), (x1, -y1), stroke: color)
}
#let draw-lines(color, ..pts) = {
let pts = pts.pos().map(pt => (pt.at(0), -pt.at(1)))
draw.line(..pts, stroke: color)
}
#let draw-poly(color, ..pts, thickness: 0) = {
let pts = pts.pos().map(pt => (pt.at(0), -pt.at(1)))
let params = (
stroke: (paint: color, thickness: thickness),
fill: none
)
if thickness == 0 {
params = (
stroke: none,
fill: color
)
}
draw.line(..pts, ..params)
}
#let draw-underbracket(config, start, end, bits-y) = {
let bit-w = config.bit-width
let bit-h = config.bit-height
let x0 = start + bit-w / 2
let x1 = end - bit-w / 2
let y0 = bits-y + bit-h * 1.25
let y1 = bits-y + bit-h * 1.5
let col = config.link-color
draw-lines(col, (x0, y0), (x0, y1), (x1, y1), (x1, y0))
}
#let draw-link(
config,
start-x,
start-y,
end-x,
end-y
) = {
let bit-h = config.bit-height
let arrow-margin = config.arrow-margin
if end-x > start-x {
end-x -= arrow-margin
} else {
end-x += arrow-margin
}
draw-lines(
config.link-color,
(start-x, start-y + bit-h * 1.5),
(start-x, end-y + bit-h / 2),
(end-x, end-y + bit-h / 2),
)
}
#let draw-values(config, values, desc-x, desc-y) = {
let shapes = ()
let txt-col = config.text-color
let bit-w = config.bit-height // Why ? I don't remember
let gap = config.values-gap
for (val, desc) in values.pairs().sorted(key: p => p.first()) {
desc-y += gap
let txt = val + " = " + desc
shapes += draw-text(
txt, txt-col, desc-x + bit-w / 2, desc-y,
anchor: "north-west",
font: config.italic-font-family,
italic: true,
size: config.italic-font-size
)
desc-y += config.italic-font-size / 1.2pt
}
return (shapes, desc-x, desc-y)
}
#let draw-description(
config,
range_,
start-x,
start-y,
width,
desc-x,
desc-y
) = {
let shapes = ()
let bit-w = config.bit-width
let bit-h = config.bit-height
if config.left-labels {
desc-x = calc.min(desc-x, start-x + width / 2 - bit-w)
} else {
desc-x = calc.max(desc-x, start-x + width / 2 + bit-w)
}
shapes += draw-underbracket(config, start-x, start-x + width, start-y)
let mid-x = start-x + width / 2
shapes += draw-link(config, mid-x, start-y, desc-x, desc-y)
let txt-x = desc-x
if config.left-labels {
txt-x -= range_.description.len() * config.default-font-size / 2pt
}
shapes += draw-text(
range_.description,
config.text-color,
txt-x, desc-y + bit-h / 2,
anchor: "west"
)
desc-y += config.default-font-size / 0.75pt
if range_.values != none and range_.depends-on == none {
let shapes_
(shapes_, _, desc-y) = draw-values(config, range_.values, txt-x, desc-y)
shapes += shapes_
}
desc-y += config.description-margin
return (shapes, desc-x, desc-y)
}
#let draw-arrow(config, start-x, start-y, end-x, end-y, label: "") = {
let shapes = ()
let dash-len = config.dash-length
let dash-space = config.dash-space
let arrow-size = config.arrow-size
let link-col = config.link-color
let txt-col = config.text-color
let arrow-label-dist = config.arrow-label-distance
let start = vec.vec(start-x, start-y)
let end = vec.vec(end-x, end-y)
let start-end = vec.sub(end, start)
let d = vec.normalize(start-end)
let dashes = int(vec.mag(start-end) / (dash-len + dash-space))
for i in range(dashes) {
let a = vec.add(
start,
vec.mul(d, i * (dash-len + dash-space))
)
let b = vec.add(
a,
vec.mul(d, dash-len)
)
shapes += draw-line(link-col, (a.x, a.y), (b.x, b.y))
}
let n = vec.vec(d.y, -d.x)
let width = arrow-size / 1.5
let p1 = vec.sub(
end,
vec.sub(
vec.mul(d, arrow-size),
vec.mul(n, width)
)
)
let p2 = vec.sub(
end,
vec.add(
vec.mul(d, arrow-size),
vec.mul(n, width)
)
)
shapes += draw-poly(
link-col,
(end.x, end.y),
(p1.x, p1.y),
(p2.x, p2.y)
)
if label != "" {
shapes += draw-text(
label,
txt-col,
(start.x + end.x) / 2,
(start.y + end.y) / 2 + arrow-label-dist,
anchor: "north"
)
}
return shapes
}
#let draw-dependency(
draw-struct, config,
struct, structures, bits-x, bits-y, range_, desc-x, desc-y
) = {
let shapes = ()
let bit-w = config.bit-width
let bit-h = config.bit-height
let arrow-margin = config.arrow-margin
let start-i = struct.bits - range_.end - 1
let start-x = bits-x + start-i * bit-w
let width = rng.bits(range_) * bit-w
shapes += draw-underbracket(config, start-x, start-x + width, bits-y)
let depend-key = rng.key(..range_.depends-on)
let depend-range = struct.ranges.at(depend-key)
let prev-range-y = bits-y + bit-h * 1.5
let prev-depend-y = if depend-range.last-value-y == -1 {
bits-y + bit-h * 1.5
} else {
depend-range.last-value-y
}
let depend-start-i = struct.bits - depend-range.end - 1
let depend-start-x = bits-x + depend-start-i * bit-w
let depend-width = rng.bits(depend-range) * bit-w
let depend-mid = depend-start-x + depend-width / 2
shapes += draw-underbracket(config, depend-start-x, depend-start-x + depend-width, bits-y)
for (val, data) in range_.values.pairs().sorted(key: p => p.first()) {
shapes += draw-arrow(config, depend-mid, prev-depend-y, depend-mid, desc-y - arrow-margin)
let val-ranges = (:)
for i in range(rng.bits(depend-range)) {
val-ranges.insert(
str(depend-range.end - i),
(name: val.at(i))
)
}
let val-struct = (
bits: rng.bits(depend-range),
start: depend-range.start,
ranges: val-ranges
)
val-struct = structure.load("", val-struct)
let shapes_
(shapes_, ..) = draw-struct(config, val-struct, structures, ox: depend-start-x, oy: desc-y)
shapes += shapes_
let y = desc-y + bit-h * 1.5
let x1
let x2
// Arrow from left to right
if depend-range.end > range_.start {
x1 = depend-start-x + depend-width + arrow-margin
x2 = start-x - arrow-margin
// Arrow from right to left
} else {
x1 = depend-start-x - arrow-margin
x2 = start-x + width + arrow-margin
}
shapes += draw-arrow(config, x1, y, x2, y, label: data.description)
shapes += draw-arrow(config,
start-x + width - bit-w,
prev-range-y,
start-x + width - bit-w,
desc-y + bit-h - arrow-margin
)
prev-depend-y = desc-y + bit-h * 2 + arrow-margin
prev-range-y = prev-depend-y
depend-range.last-value-y = prev-depend-y
(shapes_, desc-y) = draw-struct(config, structures.at(data.structure), structures, ox: start-x, oy: desc-y)
shapes += shapes_
}
struct.ranges.at(depend-key) = depend-range
return (shapes, desc-x, desc-y, struct)
}
#let draw-structure(config, struct, structures, ox: 0, oy: 0) = {
let shapes
let bg-col = config.background
let txt-col = config.text-color
let border-col = config.border-color
let bit-w = config.bit-width
let bit-h = config.bit-height
let (bits-x, bits-y) = (ox, oy + bit-h)
let bits-width = struct.bits * bit-w
let start-bit = struct.start
// Draw rectangle around structure
shapes += draw-rect(border-col, bits-x, bits-y, bits-width, bit-h, thickness: 2)
for i in range(struct.bits) {
let bit-x = ox + i * bit-w
shapes += draw-text(
str(struct.bits - i - 1 + start-bit),
txt-col,
bit-x + bit-w / 2,
oy + bit-h / 2
)
// Draw separator
if i != 0 {
shapes += draw-line(border-col, (bit-x, bits-y), (bit-x, bits-y + bit-h))
}
}
let ranges = structure.get-sorted-ranges(struct)
if config.left-labels {
ranges = ranges.rev()
}
let desc-x
if config.force-descs-on-side {
desc-x = config.margins.at(3) + structures.main.bits * bit-w
if config.left-labels {
desc-x = config.width - desc-x
}
} else {
desc-x = ox
if config.left-labels {
desc-x += struct.bits * bit-w
}
}
let desc-y = bits-y + bit-h * 2
// Names + simple descriptions
for range_ in ranges {
let start-i = struct.bits - range_.end + start-bit - 1
let start-x = bits-x + start-i * bit-w
let width = rng.bits(range_) * bit-w
let name-x = start-x + width / 2
let name-y = bits-y + bit-h / 2
shapes += draw-rect(
bg-col,
start-x + bit-w / 2,
name-y - bit-h * 0.3,
width - bit-w,
bit-h * 0.6
)
shapes += draw-text(range_.name, txt-col, name-x, name-y, fill: bg-col)
if range_.description != "" {
let shapes_
(shapes_, desc-x, desc-y) = draw-description(
config, range_, start-x, bits-y, width, desc-x, desc-y
)
shapes += shapes_
}
}
// Dependencies
for range_ in ranges {
if range_.values() != none and range_.depends-on != none {
let shapes_
(shapes_, desc-x, desc-y, struct) = draw-dependency(
draw-structure, config,
struct, structures, bits-x, bits-y, range_, desc-x, desc-y
)
shapes += shapes_
}
}
return (shapes, desc-y)
}
#let render(config, structures) = {
set text(
font: config.default-font-family,
size: config.default-font-size
)
let main = structures.main
let ox = config.margins.at(3)
if config.left-labels {
ox = config.width - ox - main.bits * config.bit-width
}
let params = if config.full-page {
(
width: auto,
height: auto,
fill: config.background,
margin: 0cm
)
} else {
(:)
}
set page(..params)
canvas(length: 1pt, background: config.background, {
let (shapes, _) = draw-structure(
config, main, structures,
ox: ox,
oy: config.margins.at(0)
)
// Workaround for margins
draw.group(name: "g", padding: config.margins, shapes)
draw.line(
"g.north-west",
"g.north-east",
"g.south-east",
"g.south-west",
stroke: none,
fill: none
)
})
}
#let make(config) = {
return (
config: config,
render: render.with(config)
)
}

65
src/schema.typ Normal file
View File

@ -0,0 +1,65 @@
#import "config.typ" as conf
#import "renderer.typ"
#import "structure.typ"
#import "xml-loader.typ"
#let valid-extensions = ("yaml", "json", "xml")
#let parse-file(path) = {
let ext = path.split(".").last()
if not ext in valid-extensions {
let fmts = valid-extensions.map(fmt => "." + fmt).join(", ")
fmts = "(" + fmts + ")"
panic("." + ext + " files are not supported. Valid formats: " + fmts)
}
if ext == "yaml" {
return yaml(path)
} else if ext == "json" {
return json(path)
} else if ext == "xml" {
return xml-loader.load(path)
}
}
#let parse-raw(schema) = {
let lang = schema.lang
let content = schema.text
if not lang in valid-extensions {
let fmts = valid-extensions.join(", ")
fmts = "(" + fmts + ")"
panic("Unsupported format '" + lang + "'. Valid formats: " + fmts)
}
if lang == "yaml" {
return yaml.decode(content)
} else if lang == "json" {
return json.decode(content)
} else if lang == "xml" {
return xml-loader.parse(xml.decode(content).first())
}
}
#let load(path-or-schema) = {
let schema = if type(path-or-schema) == str {
parse-file(path-or-schema)
} else {
parse-raw(path-or-schema)
}
let structures = (:)
for (id, data) in schema.structures {
id = str(id)
structures.insert(id, structure.load(id, data))
}
return structures
}
#let render(structures, config: auto) = {
if config == auto {
config = conf.config()
}
let renderer_ = renderer.make(config)
(renderer_.render)(structures)
}

57
src/structure.typ Normal file
View File

@ -0,0 +1,57 @@
#import "range.typ" as rng
#import "util.typ"
#let make(
name,
bits,
ranges,
start: 0
) = {
return (
name: name,
bits: bits,
ranges: ranges,
start: start
)
}
#let load(id, data) = {
let struct = (id: id)
let ranges = (:)
for (range-span, range-data) in data.ranges {
let (start, end) = rng.parse-span(str(range-span))
ranges.insert(
rng.key(start, end),
rng.load(start, end, range-data)
)
}
let ranges2 = (:)
for (k, range_) in ranges {
if range_.values != none and range_.depends-on != none {
let depends-key = rng.key(..range_.depends-on)
let depends-range = ranges.at(depends-key)
let bits = rng.bits(depends-range)
let values = (:)
for (v, d) in range_.values {
v = util.z-fill(str(int(v)), bits)
values.insert(v, d)
}
range_.values = values
}
ranges2.insert(k, range_)
}
return make(
id,
int(data.bits),
ranges2,
start: data.at("start", default: 0)
)
}
#let get-sorted-ranges(struct) = {
let ranges = struct.ranges.values()
return ranges.sorted(key: r => r.end)
}

4
src/util.typ Normal file
View File

@ -0,0 +1,4 @@
#let z-fill(string, length) = {
let filled = "0" * length + string
return filled.slice(-length)
}

44
src/vec.typ Normal file
View File

@ -0,0 +1,44 @@
#let vec(x, y) = {
return (x: x, y: y)
}
#let add(v1, v2) = {
return vec(
v1.x + v2.x,
v1.y + v2.y
)
}
#let sub(v1, v2) = {
return vec(
v1.x - v2.x,
v1.y - v2.y
)
}
#let mul(v, f) = {
return vec(
v.x * f,
v.y * f
)
}
#let div(v, f) = {
return vec(
v.x / f,
v.y / f
)
}
#let mag(v) = {
return calc.sqrt(v.x * v.x + v.y * v.y)
}
#let normalize(v) = {
let m = mag(v)
if m == 0 {
return (x: 0, y: 0)
}
return div(v, m)
}

104
src/xml-loader.typ Normal file
View File

@ -0,0 +1,104 @@
#let find(elmt, tag) = {
if not "children" in elmt {
return none
}
return elmt.children.find(e => "tag" in e and e.tag == tag)
}
#let find-all(elmt, tag) = {
if not "children" in elmt {
return ()
}
return elmt.children.filter(e => "tag" in e and e.tag == tag)
}
#let parse-values(elmt) = {
let values = (:)
let case-elmts = find-all(elmt, "case")
for case-elmt in case-elmts {
let val = case-elmt.attrs.value
let desc = case-elmt.children.first()
let struct = none
if "structure" in case-elmt.attrs {
struct = case-elmt.attrs.structure
}
values.insert(val,
if struct != none {
(
description: desc,
structure: struct
)
} else {
desc
}
)
}
return values
}
#let parse-range(elmt) = {
let range_ = (
name: elmt.attrs.name
)
let desc = none
if "children" in elmt {
desc = find(elmt, "description")
}
if desc != none {
range_.insert("description", desc.children.first())
}
let values-elmt = find(elmt, "values")
if values-elmt != none {
range_.insert("values", parse-values(values-elmt))
}
if "depends-on" in elmt.attrs {
range_.insert("depends-on", elmt.attrs.depends-on)
}
return range_
}
#let parse-structure(elmt) = {
let ranges = (:)
let range-elmts = elmt.children.filter(e => "tag" in e and e.tag == "range")
for range-elmt in range-elmts {
let span = range-elmt.attrs.end + "-" + range-elmt.attrs.start
ranges.insert(span, parse-range(range-elmt))
}
return (
bits: elmt.attrs.bits,
ranges: ranges
)
}
#let parse(content) = {
let struct-elmts = content.children.filter(e => "tag" in e and e.tag == "structure")
let structures = (:)
for struct-elmt in struct-elmts {
structures.insert(
struct-elmt.attrs.id,
parse-structure(struct-elmt)
)
}
return (
structures: structures
)
}
#let load(path) = {
let content = xml(path).first()
return parse(content)
}

14
typst.toml Normal file
View File

@ -0,0 +1,14 @@
[package]
name = "rivet"
version = "0.0.1"
compiler = "0.11.0"
repository = "https://git.kb28.ch/HEL/rivet-typst"
entrypoint = "src/lib.typ"
authors = [
"Louis Heredero <https://git.kb28.ch/HEL>"
]
categories = [ "visualization" ]
license = "Apache-2.0"
description = "Register / Instruction Visualizer & Explainer Tool with Typst, using CeTZ"
keywords = [ "assembly", "instruction", "binary" ]
exclude = [ "/gallery/*" ]