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

然后答案就正确了。

HDLBits->Circuits->Multiplexers->Mux256to1v的更多相关文章

  1. 乱码电路(Garbled circuits)

    乱码电路(Garbled circuits)是Andrew Yao教授在上世纪80年代发明的一种很聪明的技术.它可以让两个人针对某个算式来计算答案,而不需要知道他们在计算式所输入的数字. 举个例子说, ...

  2. HDU 3157 Crazy Circuits(有源汇上下界最小流)

    HDU 3157 Crazy Circuits 题目链接 题意:一个电路板,上面有N个接线柱(标号1~N),还有两个电源接线柱 + -.给出一些线路,每一个线路有一个下限值求一个能够让全部部件正常工作 ...

  3. hdoj 3157 Crazy Circuits 【有下界最小流】

    题目:hdoj 3157 Crazy Circuits 题意:如今要制造一个电路板.电路板上有 n 个电子元件,各个元件之间有单向的电流流向.然后有一个 + .电流进入, -- 电流汇入,然后推断能不 ...

  4. hdu Crazy Circuits

    Crazy Circuits 题目: 给出一个电路板,从+极出发到负极. 如今给你电路板上的最小电流限制,要你在电流平衡的时候求得从正极出发的最小电流. 算法: 非常裸的有源汇最小流.安有源汇最大流做 ...

  5. specialized English for automation-Lesson 2 Basic Circuits of Operational Amplifiers

    排版有点乱.... ========================================================================= Operational Ampl ...

  6. HDU 3157 Crazy Circuits (有源汇上下界最小流)

    题意:一个电路板,上面有N个接线柱(标号1~N)   还有两个电源接线柱  +  - 然后是 给出M个部件正负极的接线柱和最小电流,求一个可以让所有部件正常工作的总电流. 析:这是一个有源汇有上下界的 ...

  7. HDU 3157 Crazy Circuits

    Crazy Circuits Time Limit: 2000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ...

  8. 香农的伟大论文《A Symbolic Analysis of Relay and Switching Circuits》

    香农在1938年发表的伟大论文A Symbolic Analysis of Relay and Switching Circuits(<对继电器和开关电路中的符号分析>)将开关.继电器.二 ...

  9. HDU 4285 circuits( 插头dp , k回路 )

    circuits Time Limit: 30000/15000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  10. 学会使用Hdlbits网页版Verilog代码仿真验证平台

    给大家推荐一款网页版的 Verilog代码编辑仿真验证平台,这个平台是国外的一家开源FPGA学习网站,通过“https://hdlbits.01xz.net/wiki/Main_Page” 地址链接进 ...

随机推荐

  1. Python学习笔记.md

    Python学习笔记 1.变量类型 x=5 int x="ss" string x='a' string x=True bool #查看变量类型 type(x) 2.字符串常用操作 ...

  2. Java学习day19

    今天学习了窗口监听.和鼠标监听 通过构建自己的输入框监听方法能够实现简单的加法计算器 明天学习Swing,做一个简单的基于鼠标点击操作的画板

  3. angular.js中 路由 用法及概念

    在开讲之前,首先谈谈APP应用.平时我们用的app总是多页面,如果用原生安卓或者苹果,那当然很流畅啦.但是当我们用一般的html页面做移动端,简单时候我们可以用<a href="&qu ...

  4. Sliding Window - 题解【单调队列】

    题面: An array of size n ≤ 106 is given to you. There is a sliding window of size k which is moving fr ...

  5. Apache Doris Oracle ODBC外表使用指南

    1.软件环境 操作系统:ubuntu 18.04 Apache Doris :0.15 Postgresql数据库:oracle 19c UnixODBC:2.3.4 Oracle ODBC :ins ...

  6. Emacs基础使用教程及常见命令整理

    前言:最近使用Emacs,因此整理了官方中文教程中的一些知识点和常用的命令,方便查阅使用. 对于用习惯Windows的人来说,想要入门Emacs不是一件特别容易的事,但好在Emacs提供了官方的教程文 ...

  7. 图文详解:小白也能看懂的 Kubernetes

    Kubernetes 这个单词来自于希腊语,含义是舵手或领航员 .其词根是 governor 和 cybernetic.K8s 是它的缩写,用 8 字替代了"ubernete". ...

  8. Linux 运维工程师面试问答录(推荐阅读)

    一个执着于技术的公众号 本文整理了一些比较常见的 Linux 相关的面试题目,该问答录主要分为基础知识篇和服务器篇.内容主要涉及 Linux 基本原理.常用命令操作.服务器应用等部分的内容. Linu ...

  9. Elasticsearch高级之-集群搭建,数据分片

    目录 Elasticsearch高级之-集群搭建,数据分片 一 广播方式 二 单播方式 三 选取主节点 四 什么是脑裂 五 错误识别 Elasticsearch高级之-集群搭建,数据分片 es使用两种 ...

  10. git rename branch

    git 不能直接重命名远程分支,如果需要重命名则执行以下步骤操作: 重命名本地分支 删除远程分支 推送本地分支(重命名后的)到远程 额外说明: 1. 重命名后的分支也会保留历史 commit(应该是本 ...