gpt4 book ai didi

scala - 凿子移位寄存器示例

转载 作者:行者123 更新时间:2023-12-01 11:40:26 25 4
gpt4 key购买 nike

我是 scala 和 Chisel 的新手。我试图创建一个具有动态大小的 Shiftregister 示例,但我不确定以下代码是否正确。如果有人能评论一下就好了:

import Chisel._

class Shiftregister(length: Int) extends Module {
val io = new Bundle {
val clk = UInt(INPUT, 1)
val load = UInt(INPUT, 1) // 1 read from s_data_in, 0 read from p_data_in
val s_data_in = UInt(INPUT, 1)
val s_data_out = UInt(OUTPUT, 1)
val p_data_in = UInt(INPUT, length)
val p_data_out = UInt(OUTPUT, length)
}

val bitfield = Reg(init = UInt(length))

when (io.load.toBool()) {
bitfield := Cat(io.s_data_in, bitfield(length, 1))
}
.otherwise {
bitfield := io.p_data_in
}
io.p_data_out := Reg(next = bitfield)
io.s_data_out := Reg(next = bitfield(0))
}

class ShiftregisterTest(c: Shiftregister) extends Tester(c, Array(c.io)) {
defTests {
true
}
}

object Shiftregister {
def main(args: Array[String]): Unit = {
chiselMainTest(Array[String]("--backend", "c", "--genHarness", "--v"), () => Module(new Shiftregister(16))){c => new ShiftregisterTest(c)}
}
}

我尝试创建与以下 VHDL 代码等效的代码:

LIBRARY ieee;
USE ieee.std_logic_1164.ALL;

ENTITY shiftregister IS
GENERIC(
length: positive
);
PORT(
clk: IN STD_LOGIC;
load: IN STD_LOGIC; -- 1 read from s_data_in, 0 read from p_data_in
s_data_in: IN STD_LOGIC := '0';
s_data_out: OUT STD_LOGIC;
p_data_in: IN STD_LOGIC_VECTOR(length-1 DOWNTO 0) := (others => '0');
p_data_out: OUT STD_LOGIC_VECTOR(length-1 DOWNTO 0)
);
END ENTITY shiftregister;

ARCHITECTURE synthesis OF shiftregister IS
SIGNAL bitfield: STD_LOGIC_VECTOR(length-1 DOWNTO 0);
BEGIN
PROCESS (clk) IS
BEGIN
IF RISING_EDGE(clk) THEN
IF load = '0' THEN
bitfield <= p_data_in;
ELSE
bitfield(length-1 DOWNTO 0) <= s_data_in & bitfield(length-1 DOWNTO 1);
END IF;
END IF;
END PROCESS;
p_data_out <= bitfield;
s_data_out <= bitfield(0);
END ARCHITECTURE synthesis;

最佳答案

这是我的评论:

首先,“clk”是不必要的,因为时钟在 Chisel 中是隐含的。

其次,对于某些信号,您可能应该使用 Bool() 而不是 UInt(width=1)。

val load = Bool(INPUT)

虽然这确实是一种风格观点,但它可以防止以后需要进行 .toBool 转换。

第三,这一行不符合你的意图:

val bitfield = Reg(init = UInt(length))

那是创建一个寄存器,该寄存器在重置时初始化为值为“length”的 UInt()。相反,要创建宽度为“长度”的寄存器,请执行以下操作:

val bitfield = Reg(outType=UInt(width=length))

你也可以直接使用

val bitfield = Reg(UInt(width=length))

Reg() 的默认参数是您要创建的寄存器的“类型”。但是,IMO,这可能有点模棱两可。如果要将寄存器初始化为 0,则执行以下操作:

val bitfield = Reg(init = UInt(0, length))

关于scala - 凿子移位寄存器示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21430175/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com