【问题标题】:Design a decoder with unknown number of inputs using verilog使用verilog设计一个输入数量未知的解码器
【发布时间】:2013-10-18 06:36:01
【问题描述】:

这是我的问题。 我有未知数量的输入(全部为 3 位宽),具体取决于系统配置。我想设计一个解码器来选择具有最大值的输入作为输出。所以我在这里使用嵌入式 ruby​​,以便可以将配置传递给 RTL。 这是我的设计:

代码:

module decoder
(
<%  (1...NUM_INPUT).each do |i| -%>
     input      [2:0]  freq_<%=i%>,  
<% end -%>
     output    [2:0]  decoded_freq        
)
<%  (1...NUM_INPUT-1).each do |i| -%>
     wire      [2:0]  x<%=i%>,
<% end -%>

  integer i;
//decode logic below
  assign x1 = (freq_1 > freq_2)? freq_1:freq_2;  //compare the first two inputs and select the bigger one
  for (i=1; i<NUM_INPUT-1;i++)                   //for-loop to do the rest
       x<%=i+1%> = (x<%=i%> > freq_<%=i+2%>)? x<%=i%>:freq_<%=i+2%>;
  assign decoded_freq = x<%=NUM_INPUT-1%>;
endmodule

这行得通吗?我不确定这里的 for 循环。它会按我的意愿工作吗?还有其他方法吗?

【问题讨论】:

  • 哇这是怎么回事?我不认识 Ruby,但这看起来真的很难看。无论如何,如果您想合成具有“未知”数量输入的东西,则需要在合成之前知道输入的数量。您不能在 FPGA 中动态创建信号,当它被编程时,它都需要在那里。
  • 很明显你还没有尝试过使用ruby来解析文件。最后一个 for 循环不会解开,并且您有 Verilog 拼写错误。您要遵循哪个版本的 IEEE 标准?您可以在没有嵌入 ruby​​ 的情况下实现该功能。 (提示:使用参数)

标签: ruby for-loop verilog


【解决方案1】:

优秀的使用 erb 模板 verilog。 NUM_INPUT 将被定义,然后生成 verilog,我认为这增加了可伸缩性和重用代码。

新工具可以支持多维端口,但我发现某些工具不可靠,例如input [31:0] data [9:0]

有一个 ruby​​ gem 专门用于解析像 RubyIt 这样的文件;

只是提到您使用1...NUM ...NUM 表示直到NUM-1,您通常会使用0...NUM 来暗示NUM 迭代或1..NUM 从1 开始编号。

您使用的 for 循环是一种 verilog 样式,但您使用嵌入式 ruby​​ 处理某些变量。

这样的事情可能会更好:

<% NUM_INPUT = 3 %>
module decoder
(
  <%  (1..NUM_INPUT).each do |i| -%>
  input      [2:0]  freq_<%=i%>,  
  <% end -%>
  output     [2:0]  decoded_freq        
);
<%  (1..NUM_INPUT).each do |i| -%>
wire      [2:0]  x<%=i%>;
<% end -%>

//decode logic below
assign x1 = (freq_1 > freq_2)? freq_1:freq_2;  //compare the first two inputs and select the bigger one
<%# Ruby comment, for loop to create the others %>
<%  (2..NUM_INPUT).each do |i| -%>
assign x<%=i%> = (x<%=i-1%> > freq_<%=i%>)? x<%=i-1%>:freq_<%=i%>;
<% end %>

我调用了这个 decoder.rv 并运行它创建了 decoder.v

gem install ruby_it

ruby_it -f test.rv 

生成的文件:

module decoder
(
  input      [2:0]  freq_1,  
  input      [2:0]  freq_2,  
  input      [2:0]  freq_3,  
  output     [2:0]  decoded_freq        
);
wire      [2:0]  x1;
wire      [2:0]  x2;
wire      [2:0]  x3;

//decode logic below
assign x1 = (freq_1 > freq_2)? freq_1:freq_2;  //compare the first two inputs and select the bigger one

assign x2 = (x1 > freq_2)? x1:freq_2;
assign x3 = (x2 > freq_3)? x2:freq_3;

【讨论】:

  • 仍有 Verilog 语法错误。在第一个右括号后需要一个分号。连线声明需要以分号而不是逗号结尾。 integer i; 应该被删除,因为它不再被使用。
  • 你正确的@Greg,我正专注于让红宝石进行解析。会更新。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-22
  • 1970-01-01
  • 1970-01-01
  • 2014-04-04
  • 1970-01-01
相关资源
最近更新 更多