ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

HDLBits->Circuits->Multiplexers->Mux256to1v

2022-04-01 22:32:19  阅读:459  来源: 互联网

标签:Circuits module Mux256to1v PA input bit sel Multiplexers out


Verilog切片语法

题目要求如下

Create a 4-bit wide, 256-to-1 multiplexer. The 256 4-bit inputs are all packed into a single 1024-bit input vector. sel=0 should select bits in[3:0], sel=1 selects bits in[7:4], sel=2 selects bits in[11:8], etc.

提供的顶层模块如下

module top_module( 
    input [1023:0] in,
    input [7:0] sel,
    output [3:0] out );

初次编写的代码如下

module top_module( 
    input [1023:0] in,
    input [7:0] sel,
    output [3:0] out );
    assign out = in[(sel*4 + 3):in[sel*4]];
endmodule

此时代码执行会产生报错

Error (10734): Verilog HDL error at top_module.v(5): sel is not a constant File: /home/h/work/hdlbits.4558136/top_module.v Line: 5

这条报错我百思不得其解,然后看到题目给出的提示信息

.With this many options, a case statement isn't so useful.
.Vector indices can be variable, as long as the synthesizer can figure out that the width of the bits being selected is constant. It's not always good at this. An error saying "... is not a constant" means it couldn't prove that the select width is constant. In particular, in[ sel*4+3 : sel*4 ] does not work.
.Bit slicing ("Indexed vector part select", since Verilog-2001) has an even more compact syntax.

这条提示的意思就是说警告中的not a constant File意味着它不能够证明选择的宽度是一个常数,Verilog不支持这种语法,然后注意到最后一段说的Bit slicing(位切片)方法。通过网上查找,发现相关语法如下

[M -: N]  // negative offset from bit index M, N bit result 
[M +: N]  // positive offset from bit index M, N bit result

其中M是指的起始的我们的索引的位置,加减号是指的从索引位置位置向上还是向下切片,而N是指的我们切片的长度。
通过以下的小例子就可以解释这个语法

bit [7:0] PA, PB;
int loc;

initial begin
  loc = 3;
  PA = PB;                      // Read/Write
  PA[7:4] = 'hA;                // Read/Write of a slice
  PA[loc -:4] = PA[loc+1 +:4];  // Read/Write of a variable slice equivalent to PA[3:0] = PA[7:4];
end

所以我们将代码做如下修改

module top_module( 
    input [1023:0] in,
    input [7:0] sel,
    output [3:0] out );
    assign out = in[(sel*4) +:4];
endmodule

然后答案就正确了。

标签:Circuits,module,Mux256to1v,PA,input,bit,sel,Multiplexers,out
来源: https://www.cnblogs.com/TwoDogJay/p/16089480.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有