blob: ce38cf22749f419f215a093835171cace7594152 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
---
layout: docs
title: "Ports"
section: "chisel3"
---
# Ports
Ports are used as interfaces to hardware components. A port is simply
any `Data` object that has directions assigned to its members.
Chisel provides port constructors to allow a direction to be added
(input or output) to an object at construction time. Primitive port
constructors wrap the type of the port in `Input` or `Output`.
An example port declaration is as follows:
```scala mdoc:invisible
import chisel3._
```
```scala mdoc
class Decoupled extends Bundle {
val ready = Output(Bool())
val data = Input(UInt(32.W))
val valid = Input(Bool())
}
```
After defining ```Decoupled```, it becomes a new type that can be
used as needed for module interfaces or for named collections of
wires.
By folding directions into the object declarations, Chisel is able to
provide powerful wiring constructs described later.
## Inspecting Module ports
(Chisel 3.2+)
Chisel 3.2 introduced `DataMirror.modulePorts` which can be used to inspect the IOs of any Chisel module (this includes modules in both `import chisel3._` and `import Chisel._`, as well as BlackBoxes from each package).
Here is an example of how to use this API:
```scala mdoc
import chisel3.experimental.DataMirror
import chisel3.stage.{ChiselGeneratorAnnotation, ChiselStage}
class Adder extends Module {
val a = IO(Input(UInt(8.W)))
val b = IO(Input(UInt(8.W)))
val c = IO(Output(UInt(8.W)))
c := a +& b
}
class Test extends Module {
val adder = Module(new Adder)
// for debug only
adder.a := DontCare
adder.b := DontCare
// Inspect ports of adder
// See the result below.
DataMirror.modulePorts(adder).foreach { case (name, port) => {
println(s"Found port $name: $port")
}}
}
(new ChiselStage).execute(Array.empty, Seq(ChiselGeneratorAnnotation(() => new Test)))
```
|