HDLBits(7)——Multiplexer & Arithmetic Circuits

  • ----- 61. 2-to-1 multiplexer -----
    • Problem Statement
    • Answer
  • ----- 62. 2-to-1 bus multiplexer -----
    • Problem Statement
    • Answer1
  • ----- 63. 9-to-1 multiplexer -----
    • Problem Statement
    • Answer
  • ----- 64. 256-to-1 multiplexer -----
    • Problem Statement
    • Answer
  • ----- 65. 256-to-1 4-bit multiplexer -----
    • Problem Statement
    • Answer
    • Note: An unfamiliar syntax
  • ----- 66. Half adder -----
    • Problem Statement
    • Answer1
    • Answer2
  • ----- 67. Full adder -----
    • Problem Statement
    • Answer1
    • Answer2
  • ----- 68. 3-bit binary adder -----
    • Problem Statement
    • Answer
  • ----- 69. Adder -----
    • Problem Statement
    • Answer
  • ----- 70. Signed addition overflow -----
    • Problem Statement
    • Answer1
    • Answer2
  • ----- 71. 100-bit binary adder -----
    • Problem Statement
    • Answer1
    • Answer2
  • ----- 72. 4-digit BCD adder -----
    • Problem Statement
    • Answer
    • Warning

----- 61. 2-to-1 multiplexer -----

Problem Statement

Create a one-bit wide, 2-to-1 multiplexer. When sel=0, choose a. When sel=1, choose b.

Expected solution length: Around 1 line.

Answer

module top_module( input a, b, sel,output out ); assign out = (~sel & a) | (sel & b);//other way: assign out = sel ? b : a;endmodule

----- 62. 2-to-1 bus multiplexer -----

Problem Statement

Create a 100-bit wide, 2-to-1 multiplexer. When sel=0, choose a. When sel=1, choose b.

Expected solution length: Around 1 line.

Answer1

module top_module( input [99:0] a, b,input sel,output [99:0] out );assign out = ( ~{100{sel}} & a) | ( {100{sel}} & b);//other way: assign out = sel ? b : a;endmodule

----- 63. 9-to-1 multiplexer -----

Problem Statement

Create a 16-bit wide, 9-to-1 multiplexer. sel=0 chooses a, sel=1 chooses b, etc. For the unused cases (sel=9 to 15), set all output bits to ‘1’.

Expected solution length: Around 15 lines.

Answer

module top_module( input [15:0] a, b, c, d, e, f, g, h, i,input [3:0] sel,output reg [15:0] out );always @(*) begincase(sel)4'd0: out = a;4'd1: out = b;4'd2: out = c;4'd3: out = d;4'd4: out = e;4'd5: out = f;4'd6: out = g;4'd7: out = h;4'd8: out = i;default: out = {16{1'b1}};endcaseendendmodule

----- 64. 256-to-1 multiplexer -----

Problem Statement

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

Expected solution length: Around 1 line.

Answer

module top_module( input [255:0] in,input [7:0] sel,output out );assign out = in[sel];endmodule

----- 65. 256-to-1 4-bit multiplexer -----

Problem Statement

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.

Expected solution length: Around 1–5 lines.

Answer

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

Note: An unfamiliar syntax

Alternatively, “indexed vector part select” works better, but has an unfamiliar syntax:

  • assign out = in[sel*4 +: 4]; // Select starting at index “sel*4”, then select a total width of 4 bits with increasing ( +: ) index number.
  • assign out = in[sel*4+3 -: 4]; // Select starting at index “sel*4+3”, then select a total width of 4 bits with decreasing ( -: ) index number.
  • Note: The width (4 in this case) must be constant. So assign out = in[4*sel+3 : 4*sel] is the wrong way.

----- 66. Half adder -----

Problem Statement

Create a half adder. A half adder adds two bits (with no carry-in) and produces a sum and carry-out.

Expected solution length: Around 2 lines.

Answer1

module top_module( input a, b,output cout, sum );assign sum = a ^ b;assign cout = a & b;endmodule

Answer2

module top_module( input a, b,output cout, sum );assign {cout, sum} = a + b;endmodule

----- 67. Full adder -----

Problem Statement

Create a full adder. A full adder adds three bits (including carry-in) and produces a sum and carry-out.

Expected solution length: Around 2 lines.

Answer1

module top_module( input a, b, cin,output cout, sum );wire s, co1, co2;assign s = a ^ b;assign co1 = a & b;assign sum = cin ^ s;assign co2 = cin & s;assign cout = co1 | co2;endmodule

Answer2

module top_module( input a, b, cin,output cout, sum );assign {cout, sum} = a + b + cin;endmodule

----- 68. 3-bit binary adder -----

Problem Statement

Now that you know how to build a full adder, make 3 instances of it to create a 3-bit binary ripple-carry adder. The adder adds two 3-bit numbers and a carry-in to produce a 3-bit sum and carry out. To encourage you to actually instantiate full adders, also output the carry-out from each full adder in the ripple-carry adder. cout[2] is the final carry-out from the last full adder, and is the carry-out you usually see.

Answer

module top_module( input [2:0] a, b,input cin,output [2:0] cout,output [2:0] sum );adder adder1(.a(a[0]), .b(b[0]), .cin(cin), .cout(cout[0]), .sum(sum[0])); // low-bitadder adder2(.a(a[1]), .b(b[1]), .cin(cout[0]), .cout(cout[1]), .sum(sum[1]));adder adder3(.a(a[2]), .b(b[2]), .cin(cout[1]), .cout(cout[2]), .sum(sum[2])); // high-bitendmodulemodule adder( input a, b, cin,output cout, sum );assign {cout, sum} = a + b + cin;endmodule

----- 69. Adder -----

Problem Statement

Implement the following circuit:

(“FA” is a full adder)

Answer

module top_module (input [3:0] x,input [3:0] y, output [4:0] sum);wire [2:0] io;adder adder1(.a(x[0]), .b(y[0]), .cin(0), .cout(io[0]), .sum(sum[0])); // low-bitadder adder2(.a(x[1]), .b(y[1]), .cin(io[0]), .cout(io[1]), .sum(sum[1]));adder adder3(.a(x[2]), .b(y[2]), .cin(io[1]), .cout(io[2]), .sum(sum[2]));adder adder4(.a(x[3]), .b(y[3]), .cin(io[2]), .cout(sum[4]), .sum(sum[3])); // high-bitendmodulemodule adder( input a, b, cin,output cout, sum );assign {cout, sum} = a + b + cin;endmodule

----- 70. Signed addition overflow -----

Problem Statement

Assume that you have two 8-bit 2’s complement numbers(二补码,一个更为熟悉的翻译是二进制补码), a[7:0] and b[7:0]. These numbers are added to produce s[7:0]. Also compute whether a (signed) overflow has occurred.

Answer1

检测补码加法中的溢出(深入理解计算机系统,CS:APP,P65):令s = x + y,当 x > 0, y > 0, s < 0 时,s发生正溢出(即 x 和 y的最高位为 0,s 最高位为 1 时发生正溢出);当 x < 0, y < 0, s > 0 时,s发生负溢出(即 x 和 y的最高位为 1,s 最高位为 0 时发生负溢出)。

module top_module (input [7:0] a,input [7:0] b,output [7:0] s,output overflow
); wire [7:0] io;adder adder0(.a(a[0]), .b(b[0]), .cin(0), .cout(io[0]), .sum(s[0])); // low-bitadder adder1(.a(a[1]), .b(b[1]), .cin(io[0]), .cout(io[1]), .sum(s[1]));adder adder2(.a(a[2]), .b(b[2]), .cin(io[1]), .cout(io[2]), .sum(s[2]));adder adder3(.a(a[3]), .b(b[3]), .cin(io[2]), .cout(io[3]), .sum(s[3])); adder adder4(.a(a[4]), .b(b[4]), .cin(io[3]), .cout(io[4]), .sum(s[4]));adder adder5(.a(a[5]), .b(b[5]), .cin(io[4]), .cout(io[5]), .sum(s[5]));adder adder6(.a(a[6]), .b(b[6]), .cin(io[5]), .cout(io[6]), .sum(s[6]));adder adder7(.a(a[7]), .b(b[7]), .cin(io[6]), .cout(io[7]), .sum(s[7])); // high-bitassign overflow = (a[7] & b[7] & ~s[7]) | (~a[7] & ~b[7] & s[7]); // 检测补码加法中的溢出(CS:APP,P65)endmodulemodule adder( input a, b, cin,output cout, sum );assign {cout, sum} = a + b + cin;endmodule

Answer2

module top_module (input [7:0] a,input [7:0] b,output [7:0] s,output overflow
); assign s = a + b;assign overflow = (a[7] & b[7] & ~s[7]) | (~a[7] & ~b[7] & s[7]);endmodule

----- 71. 100-bit binary adder -----

Problem Statement

Create a 100-bit binary adder. The adder adds two 100-bit numbers and a carry-in to produce a 100-bit sum and carry out.

Expected solution length: Around 1 line.

Answer1

module top_module( input [99:0] a, b,input cin,output cout,output [99:0] sum );genvar i;wire [99:0] io;generatefor(i = 0; i <= 99; i = i + 1)begin: add100if(i == 0)assign {io[i], sum[i]} = a[i] + b[i] + cin;elseassign {io[i], sum[i]} = a[i] + b[i] + io[i-1];endassign cout = io[99];endgenerateendmodule

Answer2

module top_module (input [99:0] a,input [99:0] b,input cin,output cout,output [99:0] sum
);// The concatenation {cout, sum} is a 101-bit vector.assign {cout, sum} = a+b+cin;endmodule

----- 72. 4-digit BCD adder -----

Problem Statement

You are provided with a BCD (binary-coded decimal) one-digit adder named bcd_fadd that adds two BCD digits and carry-in, and produces a sum and carry-out.

module bcd_fadd (input [3:0] a,input [3:0] b,input     cin,output   cout,output [3:0] sum );

Instantiate 4 copies of bcd_fadd to create a 4-digit BCD ripple-carry adder. Your adder should add two 4-digit BCD numbers (packed into 16-bit vectors) and a carry-in to produce a 4-digit sum and carry out.

Answer

module bcd_fadd (input [3:0] a,input [3:0] b,input     cin,output   cout,output [3:0] sum );//...endmodulemodule top_module( input [15:0] a, b,input cin,output cout,output [15:0] sum );wire io0, io1, io2, io3;bcd_fadd bcd_fadd0(.a(a[3:0]), .b(b[3:0]), .cin(cin), .cout(io0), .sum(sum[3:0]));bcd_fadd bcd_fadd1(.a(a[7:4]), .b(b[7:4]), .cin(io0), .cout(io1), .sum(sum[7:4]));bcd_fadd bcd_fadd2(.a(a[11:8]), .b(b[11:8]), .cin(io1), .cout(io2), .sum(sum[11:8]));bcd_fadd bcd_fadd3(.a(a[15:12]), .b(b[15:12]), .cin(io2), .cout(io3), .sum(sum[15:12]));assign cout = io3;endmodule

Warning

  • Warning (10230): Verilog HDL assignment warning at tb_modules.sv(8): truncated value with size 32 to match size of target (4) File

Truncating values occur when the right side of an assignment is wider than the left side and the upper bits are cut off. This can indicate a bug if there is a truncation you didn’t expect, so check these carefully. The most common case where this isn’t a bug is when you’re using literals without a width (32 bits is implied), e.g., using assign a[1:0] = 1; instead of assign a[1:0] = 2’d1;.

HDLBits(7)——Multiplexer Arithmetic Circuits相关推荐

  1. HDLBits(4) Procedures合集

    Procedures Alwaysblock1:combinational Problem Statement Writing Code Alwaysblock2:clocked 阻塞 VS 非阻塞赋 ...

  2. 【HDLBits 刷题 10】Circuits(6)Finite State Manchines 10-17

    目录 写在前面 Finite State Manchines Lemmings1 Lemmings2 Lemmings3 Lemmings4 Fsm onehot Fsm ps2 Fsm ps2dat ...

  3. 【HDLBits 刷题 11】Circuits(7)Finite State Manchines 18-26

    目录 写在前面 Finite State Manchines Fsm serialdata Fsm serialdp Fsm hdlc Design a Mealy FSM ece241 2014 q ...

  4. 【HDLBits 刷题 12】Circuits(8)Finite State Manchines 27-34

    目录 写在前面 Finite State Manchines 2014 q3c m2014 q6b m2014 q6c m2014 q6 2012 q2fsm 2012 q2b 2013 q2afsm ...

  5. verilog练习:hdlbits网站上的做题笔记(5)

    前言 之前的文章<如何学习verilog,如何快速入门?>中提到了verilog学习,推荐了一个可以练习的网站:hdlbits网站,那自己也玩玩这个网站. 这篇文章,是接着<veri ...

  6. HDLBits刷题合集—9 Arithmetic Circuits

    HDLBits刷题合集-9 Arithmetic Circuits HDLBits-66 Hadd Problem Statement 创建一个半加器.半加器将两个输入(不带低位的进位)相加产生和和向 ...

  7. verilog练习:hdlbits网站上的做题笔记(6)

    前言 之前的文章<如何学习verilog,如何快速入门?>中提到了verilog学习,推荐了一个可以练习的网站:hdlbits网站,那自己也玩玩这个网站. 这篇文章,是接着<veri ...

  8. verilog练习:hdlbits网站上的做题笔记(7)!强烈推荐!

    前言 之前的文章<如何学习verilog,如何快速入门?>中提到了verilog学习,推荐了一个可以练习的网站:hdlbits网站,那自己也玩玩这个网站. 这篇文章,是接着<veri ...

  9. verilog练习:hdlbits网站上的做题笔记(8)

    前言 之前的文章<如何学习verilog,如何快速入门?>中提到了verilog学习,推荐了一个可以练习的网站:hdlbits网站,那自己也玩玩这个网站. 这篇文章,是接着<veri ...

最新文章

  1. RedHat Enterprise Linux 5.0之LVM逻辑卷管理
  2. Linux内核探讨-- 第五章
  3. java joptionpane调整大小_JAVA应用性能监控之JVM层GC调优
  4. 深度学习核心技术精讲100篇(六)-keras 实战系列之知识蒸馏(Knowledge Distilling)
  5. 简单编译安装Apache
  6. 【Leetcode | 235】 235. 二叉搜索树的最近公共祖先
  7. Happy Valentine's Day
  8. 下面介绍几种查看linux版本信息的方法和GCC版本
  9. bootstarp怎么使盒子到最右边_江湖救急!盒子显示“很抱歉Launchercust 已停止运行” ?...
  10. python动态是什么意思_怎么看出自己是Python什么阶段
  11. 华为交换机怎么关闭信息提示_iPhone 关闭支付宝自动扣费服务提示“无法解约”怎么办?...
  12. HTML多行代码搞定微信8.0的炸裂特效!C/C++怎么能输
  13. 华为手机 图标消失_华为手机桌面图标不见了怎么办
  14. java酒店管理系统毕业论文
  15. QQ农场外挂、QQ牧场外挂专偷QQ号
  16. 数据可视化看板怎么搭建,这样做小白能看懂
  17. 负载均衡器之F5和Nginx
  18. 蓝牙 sig base uuid_蓝牙,从系统开机说起
  19. MyBatis-Plus--自动填充的用法
  20. 基于STM32的游戏平台,其二TETRIS

热门文章

  1. 基于小梅哥的Xlinx FPGA开发视频的布置作业--用串口控制一个24小时数字钟
  2. 小米推出物联网软件平台Xiaomi Vela;苹果11月11日再开发布会,自研处理器Mac有望推出;华为:计划在上海建芯片厂...
  3. 全国计算机英语四六级准考证打印准考证号,大学英语四六级准考证打印入口|四六级准考证打印入口2020...
  4. 仅使用CSS提高页面渲染速度
  5. Bambook缺乏制胜市场的杀手锏
  6. 3ds Max的操作快捷键
  7. 基于BSV的存储证明
  8. 如何为MacBook或Mac电脑恢复出厂设置
  9. 多种系统exe打开方式还原的详细方法
  10. Kubernetes 笔记 07 豌豆荚之旅(二)