Verilog切片语法

题目要求如下

  1. 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.

提供的顶层模块如下

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

初次编写的代码如下

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

此时代码执行会产生报错

  1. 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

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

  1. .With this many options, a case statement isn't so useful.
  2. .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.
  3. .Bit slicing ("Indexed vector part select", since Verilog-2001) has an even more compact syntax.

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

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

其中M是指的起始的我们的索引的位置,加减号是指的从索引位置位置向上还是向下切片,而N是指的我们切片的长度。

通过以下的小例子就可以解释这个语法

  1. bit [7:0] PA, PB;
  2. int loc;
  3. initial begin
  4. loc = 3;
  5. PA = PB; // Read/Write
  6. PA[7:4] = 'hA; // Read/Write of a slice
  7. PA[loc -:4] = PA[loc+1 +:4]; // Read/Write of a variable slice equivalent to PA[3:0] = PA[7:4];
  8. end

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

  1. module top_module(
  2. input [1023:0] in,
  3. input [7:0] sel,
  4. output [3:0] out );
  5. assign out = in[(sel*4) +:4];
  6. 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数据类型内置的方法

    数据类型的内置方法 在日常生活中不同类型的数据具有不同的功能 eg:表格数据文件具有处理表格的各项功能(透视表 图形化 公式计算) 视频数据文件具有快进 加速等各项功能 ... 1.整型int # 方 ...

  2. JetBrains Rider C# 学习②

    前言 C#从入门到精通 链接:https://pan.baidu.com/s/1UveJI_f-c5Dul3GLIICRHg 提取码:1314 C#入门课程 刘铁猛 链接:https://pan.ba ...

  3. toString()函数与valueOf()函数

    一.前言 在等于运算符中,如果比较的内容包含对象类型数据,则会涉及隐式转换,那么就会调用toString()函数和valueOf()函数,下面我们将会了解到关于这两个函数的基本概念和使用场景. 二.t ...

  4. brup去除mozilla等无用数据包的方法

    方法一 针对火狐浏览器的解决方法 1.在firefox(火狐浏览器)地址栏中输入: about:config 2.然后出现搜索框,搜索以下内容,双击将它设置成false. network.captiv ...

  5. 2021.11.09 P3426 [POI2005]SZA-Template(KMP+DP)

    2021.11.09 P3426 [POI2005]SZA-Template(KMP+DP) https://www.luogu.com.cn/problem/P3426 题意: 你打算在纸上印一串字 ...

  6. go-micro开发RPC服务的方法及其运行原理

    go-micro是一个知名的golang微服务框架,最新版本是v4,这篇文章将介绍go-micro v4开发RPC服务的方法及其运作原理. 基本概念 go-micro有几个重要的概念,后边开发RPC服 ...

  7. C#语法糖系列 —— 第二篇:聊聊 ref,in 修饰符底层玩法

    自从 C# 7.3 放开 ref 之后,这玩法就太花哨了,也让 C# 这门语言变得越来越多范式,越来越重,这篇我们就来聊聊 ref,本质上来说 ref 的放开就是把 C/C++ 指针的那一套又拿回来了 ...

  8. mybatis混淆概念

    1.resultMap与resultType <mapper namespace="com.dao.FilmMapper"> <resultMap id=&quo ...

  9. [AcWing 29] 删除链表中重复的节点

    点击查看代码 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * L ...

  10. 虚拟机VMware 安装centos、常规配置、共享文件等

    安装centos7[通过vm来安装运行centos7] 一.准备工作 1.centos7 的安装镜像下载链接:http://isoredirect.centos.org/centos/7/isos/x ...