Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add documentation for values #468

Merged
merged 1 commit into from
Oct 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw, is this a <= issue or a more general wire() issue?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's a wire issue (in the way the logic handles anonymous 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")