如何在 Chisel 中使用向量作为输入
How to use a vector as input in Chisel
我正在使用以下代码设计 Chisel 模块:
import chisel3._
import chisel3.util._
class DisplayDriver extends Module {
val io = IO(new Bundle {
val digits = Input(Vec(4, UInt(4.W)))
val leds = Output(UInt(7.W))
val selector = Output(UInt(4.W))
})
// Divisor de frecuencia
val freqDiv = RegInit(0.U(11.W))
val tick = freqDiv === (4000 - 1).U
freqDiv := freqDiv + 1.U;
when (tick) {
freqDiv := 0.U
}
// Multiplexor de dígitos
val digitSel = RegInit(0.U(2.W))
when (tick) {
digitSel := digitSel + 1.U
}
val digit = UInt(4.W)
digit := io.digits(digitSel)
// Decodificador para el selector
io.selector := 0.U
switch (digitSel) {
is (0.U) { io.selector := "b1110".U }
is (1.U) { io.selector := "b1101".U }
is (2.U) { io.selector := "b1011".U }
is (3.U) { io.selector := "b0111".U }
}
// Decodificador para los leds
io.leds := 0.U
switch (digit) {
is (0.U) { io.leds := "b0000001".U }
is (1.U) { io.leds := "b1001111".U }
is (2.U) { io.leds := "b0010010".U }
is (3.U) { io.leds := "b0000110".U }
is (4.U) { io.leds := "b1001100".U }
is (5.U) { io.leds := "b0100100".U }
is (6.U) { io.leds := "b0100000".U }
is (7.U) { io.leds := "b0001111".U }
is (8.U) { io.leds := "b0000000".U }
is (9.U) { io.leds := "b0000100".U }
is (10.U) { io.leds := "b0001000".U }
is (11.U) { io.leds := "b1100000".U }
is (12.U) { io.leds := "b0110001".U }
is (13.U) { io.leds := "b1000010".U }
is (14.U) { io.leds := "b0110000".U }
is (15.U) { io.leds := "b0111000".U }
}
}
class Top extends Module {
val io = IO(new Bundle {
val leds = Output(UInt(7.W))
val selector = Output(UInt(4.W))
})
val displayDriver = Module(new DisplayDriver())
displayDriver.io.digits(3) := 1.U
displayDriver.io.digits(2) := 2.U
displayDriver.io.digits(1) := 3.U
displayDriver.io.digits(0) := 4.U
io.leds := displayDriver.io.leds
io.selector := displayDriver.io.selector
}
但是当 运行 它时我得到以下错误:
[error] (run-main-0) chisel3.package$ExpectedHardwareException: data to be connected 'UInt<4>' must be hardware, not a bare Chisel type. Perhaps you forgot to wrap it in Wire(_) or IO(_)?
[error] chisel3.package$ExpectedHardwareException: data to be connected 'UInt<4>' must be hardware, not a bare Chisel type. Perhaps you forgot to wrap it in Wire(_) or IO(_)?
这显然指向线
digit := io.digits(digitSel)
我在 io.digits 周围尝试了很多与 Wire 的组合,但它仍然无法编译。事实上,在其他示例之后,我不明白为什么会出现此错误,因为向量在 IO 中。发生了什么事?
错误来自连接操作的左侧:digit
应定义为 Wire
。
val digit = Wire(UInt(4.W))
digit := io.digits(digitSel)
我正在使用以下代码设计 Chisel 模块:
import chisel3._
import chisel3.util._
class DisplayDriver extends Module {
val io = IO(new Bundle {
val digits = Input(Vec(4, UInt(4.W)))
val leds = Output(UInt(7.W))
val selector = Output(UInt(4.W))
})
// Divisor de frecuencia
val freqDiv = RegInit(0.U(11.W))
val tick = freqDiv === (4000 - 1).U
freqDiv := freqDiv + 1.U;
when (tick) {
freqDiv := 0.U
}
// Multiplexor de dígitos
val digitSel = RegInit(0.U(2.W))
when (tick) {
digitSel := digitSel + 1.U
}
val digit = UInt(4.W)
digit := io.digits(digitSel)
// Decodificador para el selector
io.selector := 0.U
switch (digitSel) {
is (0.U) { io.selector := "b1110".U }
is (1.U) { io.selector := "b1101".U }
is (2.U) { io.selector := "b1011".U }
is (3.U) { io.selector := "b0111".U }
}
// Decodificador para los leds
io.leds := 0.U
switch (digit) {
is (0.U) { io.leds := "b0000001".U }
is (1.U) { io.leds := "b1001111".U }
is (2.U) { io.leds := "b0010010".U }
is (3.U) { io.leds := "b0000110".U }
is (4.U) { io.leds := "b1001100".U }
is (5.U) { io.leds := "b0100100".U }
is (6.U) { io.leds := "b0100000".U }
is (7.U) { io.leds := "b0001111".U }
is (8.U) { io.leds := "b0000000".U }
is (9.U) { io.leds := "b0000100".U }
is (10.U) { io.leds := "b0001000".U }
is (11.U) { io.leds := "b1100000".U }
is (12.U) { io.leds := "b0110001".U }
is (13.U) { io.leds := "b1000010".U }
is (14.U) { io.leds := "b0110000".U }
is (15.U) { io.leds := "b0111000".U }
}
}
class Top extends Module {
val io = IO(new Bundle {
val leds = Output(UInt(7.W))
val selector = Output(UInt(4.W))
})
val displayDriver = Module(new DisplayDriver())
displayDriver.io.digits(3) := 1.U
displayDriver.io.digits(2) := 2.U
displayDriver.io.digits(1) := 3.U
displayDriver.io.digits(0) := 4.U
io.leds := displayDriver.io.leds
io.selector := displayDriver.io.selector
}
但是当 运行 它时我得到以下错误:
[error] (run-main-0) chisel3.package$ExpectedHardwareException: data to be connected 'UInt<4>' must be hardware, not a bare Chisel type. Perhaps you forgot to wrap it in Wire(_) or IO(_)?
[error] chisel3.package$ExpectedHardwareException: data to be connected 'UInt<4>' must be hardware, not a bare Chisel type. Perhaps you forgot to wrap it in Wire(_) or IO(_)?
这显然指向线
digit := io.digits(digitSel)
我在 io.digits 周围尝试了很多与 Wire 的组合,但它仍然无法编译。事实上,在其他示例之后,我不明白为什么会出现此错误,因为向量在 IO 中。发生了什么事?
错误来自连接操作的左侧:digit
应定义为 Wire
。
val digit = Wire(UInt(4.W))
digit := io.digits(digitSel)