Skip to content

Commit

Permalink
Add documentation for values
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardt committed Oct 28, 2019
1 parent ab1594a commit 5d7698d
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 1 deletion.
52 changes: 52 additions & 0 deletions docs/cheat_sheet.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
**NOTE** This is a work in progress, please let us know via issue/gitter/email
if you'd like to see anything added to this.

This is inspired by https://github.com/freechipsproject/chisel-cheatsheet and
will be rendered in a similar single page layout soon.

# Basic Magma Constructs
Magma Values
```
class Test(Circuit):
IO = ["I", In(Bits[5]), "O", Out(Bits[5])]
@classmethod
def definition(io):
# Allocate `x` as a value of type `Bits`
x = Bits[5]()
# Wire io.I to x
x <= io.I
# Wire x to io.O
io.O <= x
```

**NOTE** Currently magma only supports wiring two intermediate temporary values
if the driver already has a driver. The following example will work, because `y` is
driven by `io.I` before wiring to the temporary `x`.

```python
class Test(Circuit):
IO = ["I", In(Bits[5]), "O", Out(Bits[5])]

@classmethod
def definition(io):
x = Bits[5]()
y = Bits[5]()
y <= io.I
x <= y
io.O <= x
```
while this example will not work, because `y` has no driver when being wired to
`x`. A fix for this issue is forthcoming.
```python
class Test(Circuit):
IO = ["I", In(Bits[5]), "O", Out(Bits[5])]

@classmethod
def definition(io):
x = Bits[5]()
y = Bits[5]()
y <= io.I
x <= y
io.O <= x
```
2 changes: 1 addition & 1 deletion magma/t.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def debug_name(self):
return f"{defn_str}{inst_str}{str(self)}"

def __le__(self, other):
if self.isinput():
if not self.isoutput():
self.wire(other)
else:
raise TypeError(f"Cannot use <= to assign to output: {self.debug_name} (trying to assign {other.debug_name})")
Expand Down
4 changes: 4 additions & 0 deletions tests/test_type/gold/test_anon_bits.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module Test (input [4:0] I, output [4:0] O);
assign O = I;
endmodule

17 changes: 17 additions & 0 deletions tests/test_type/test_anon.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from magma import *
from magma.testing import check_files_equal

# test anonymous bits

Expand All @@ -18,3 +19,19 @@ def test():
assert len(wires.outputs) == 1
print('inputs:', [str(p) for p in wires.inputs])
print('outputs:', [str(p) for p in wires.outputs])


def test_anon_bits():
class Test(Circuit):
IO = ["I", In(Bits[5]), "O", Out(Bits[5])]

@classmethod
def definition(io):
x = Bits[5]()
y = Bits[5]()
y <= io.I
x <= y
io.O <= x
compile("build/test_anon_bits", Test, output="coreir-verilog")
assert check_files_equal(__file__, f"build/test_anon_bits.v",
f"gold/test_anon_bits.v")

0 comments on commit 5d7698d

Please sign in to comment.