我正在尝试创建一个简单的计数器,它计算直到并包括一些 max_count_S 已经过去了多少秒。
为了简化过程,我首先创建了一个“信号”计数器,它使用时钟进行计数,仅在信号输入为高电平时才计数。
import chisel3._
/*
|While signal is active, it counts [start,max_count] clock cycles.
*/
class SignalCounter(start_value: Int = 0, width: Int, max_count: Int) extends Module {
val io = IO(new Bundle {
val signal = Input(Bool())
val out = Output(UInt(width.W))
})
val counter_reg = RegInit(start_value.U(width.W))
when(io.signal)
{
//Whenever we get the signal; Update the counter_reg
counter_reg := Mux(counter_reg === max_count.U(width.W),0.U ,counter_reg + 1.U)
}
io.out := counter_reg
}
我的目标是拥有其中两个; max_count 等于每秒时钟周期数减 1,这样每经过一秒就输出 0。然后我希望有第二个,每当第一个信号为 0 时,它的信号就为高,这样它就可以计算已经过去了多少秒。
我尝试在这里实现:
import chisel3._
import scala.math._
def calculateBitsNeeded(N: Int): Int = {
ceil(log10(N.toDouble + 1) / log10(2)).toInt
}
/*
arg: cc_to_S: Number of clock cycles per second
*/
class TimeCounter(cc_to_S: Int = (100000000-1),timestep_S: Int = 0, width: Int, max_count_S: Int) extends Module {
val io = IO(new Bundle {
val out_s = Output(UInt(width.W))
})
val sig_counter_clock = new SignalCounter(max_count = cc_to_S, width =calculateBitsNeeded(cc_to_S)) //we take the cc_to_S - 1, because we want to it to tick every clock cycle we hit 1 second, not after.
val sig_counter_S = new SignalCounter(max_count = max_count_S, width = calculateBitsNeeded(max_count_S))
sig_counter_clock.io.signal = true.B
}
但是我收到以下错误:
重新分配给val
有什么方法可以分配模块内的 io 引脚吗?
这个问题与我的解决方案的架构无关;简单介绍如何在模块内连接 .io 引脚。
模块实例必须封装在Module()中,如下所示:
val sig_counter_clock = Module(new SignalCounter(max_count = cc_to_S, width =calculateBitsNeeded(cc_to_S))) //we take the cc_to_S - 1, because we want to it to tick every clock cycle we hit 1 second, not after.
val sig_counter_S = Module(new SignalCounter(max_count = max_count_S, width = calculateBitsNeeded(max_count_S)))