HDLBits(7)——Multiplexer & Arithmetic Circuits
----- 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
1 | module top_module( |
----- 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
1 | module top_module( |
----- 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
1 | module top_module( |
----- 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
1 | module top_module( |
----- 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
1 | module top_module( |
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
1 | module top_module( |
Answer2
1 | module top_module( |
----- 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
1 | module top_module( |
Answer2
1 | module top_module( |
----- 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
1 | module top_module( |
----- 69. Adder -----
Problem Statement
Implement the following circuit:
(“FA” is a full adder)
Answer
1 | module top_module ( |
----- 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 时发生负溢出)。
1 | module top_module ( |
Answer2
1 | module top_module ( |
----- 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
1 | module top_module( |
Answer2
1 | module top_module ( |
----- 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.
1 | module bcd_fadd ( |
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
1 | module bcd_fadd ( |
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;.



