3

我对 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;
4

1 回答 1

5

以下是我的评论:

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

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

val load = Bool(INPUT)

虽然这无疑是一种风格观点,但它可以防止以后需要进行 .toBool 演员表。

第三,这条线不符合你的意图:

val bitfield = Reg(init = UInt(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))
于 2014-02-05T21:43:10.513 回答