关注 望森FPGA 查看更多FPGA资讯
这是望森的第 10 期分享
作者 | 望森
来源 | 望森FPGA
目录
1 Half adder | 半加器
2 Full adder | 全加器
3 3-bit binary adder | 3位二进制加法器
4 Adder | 加法器
5 Signed addition overflow | 有符号数的加法溢出
6 100-bit binary adder | 100位二进制加法器
7 4-digit BCD adder | 4位BCD加法器
本文中的代码都能够正常运行,请放心食用😋~
练习的官方网站是:https://hdlbits.01xz.net/
注:作者将每个练习的知识点都放在了题目和答案之后
1 Half adder | 半加器
题目:
创建一个半加器。半加器将两位相加(无进位),然后得出和与进位。
答案:
module top_module( input a, b,output cout, sum );always@(*) beginsum = a ^ b;cout = a & b;endendmodule
2 Full adder | 全加器
题目:
创建一个全加器。全加器将三位(包括进位)相加,并得出和与进位。
答案:
module top_module( input a, b, cin,output cout, sum );always@(*) beginsum = a ^ b ^ cin;cout = a & b | a & cin | b & cin;endendmodule
3 3-bit binary adder | 3位二进制加法器
题目:
现在您已经知道如何构建全加器,请创建 3 个实例以创建 3 位二进制行波进位加法器。该加法器将两个 3 位数和一个进位相加,以产生 3 位和并输出进位。为了鼓励您实际实例化全加器,还要在行波进位加法器中输出每个全加器的进位。cout[2] 是最后一个全加器的最终进位,也是您通常看到的进位。
答案:
module top_module( input [2:0] a, b,input cin,output [2:0] cout,output [2:0] sum );full_adder adder0(.a(a[0]),.b(b[0]),.cin(cin),.cout(cout[0]),.sum(sum[0]));full_adder adder1(.a(a[1]),.b(b[1]),.cin(cout[0]),.cout(cout[1]),.sum(sum[1]));full_adder adder2(.a(a[2]),.b(b[2]),.cin(cout[1]),.cout(cout[2]),.sum(sum[2]));endmodulemodule full_adder( input a, b, cin,output cout, sum );always@(*) beginsum = a ^ b ^ cin;cout = a & b | a & cin | b & cin;endendmodule
4 Adder | 加法器
题目:
实现以下电路:
("FA" is a full adder)
答案:
module top_module (input [3:0] x,input [3:0] y, output [4:0] sum);wire [2:0] cout;full_adder adder0(.a(x[0]),.b(y[0]),.cin(1'b0),.cout(cout[0]),.sum(sum[0]));full_adder adder1(.a(x[1]),.b(y[1]),.cin(cout[0]),.cout(cout[1]),.sum(sum[1]));full_adder adder2(.a(x[2]),.b(y[2]),.cin(cout[1]),.cout(cout[2]),.sum(sum[2]));full_adder adder3(.a(x[3]),.b(y[3]),.cin(cout[2]),.cout(sum[4]),.sum(sum[3]));endmodulemodule full_adder( input a, b, cin,output cout, sum );always@(*) beginsum = a ^ b ^ cin;cout = a & b | a & cin | b & cin;endendmodule
参考答案:
module top_module (input [3:0] x,input [3:0] y,output [4:0] sum
);// This circuit is a 4-bit ripple-carry adder with carry-out.assign sum = x+y; // Verilog addition automatically produces the carry-out bit.// Verilog quirk: Even though the value of (x+y) includes the carry-out, (x+y) is still considered to be a 4-bit number (The max width of the two operands).// This is correct:// assign sum = (x+y);// But this is incorrect:// assign sum = {x+y}; // Concatenation operator: This discards the carry-out
endmodule
5 Signed addition overflow | 有符号数的加法溢出
题目:
假设有两个 8 位 2 的补码数,a[7:0] 和 b[7:0]。这两个数相加得到 s[7:0]。还要计算是否发生了(有符号)溢出。
答案:
module top_module (input [7:0] a,input [7:0] b,output [7:0] s,output overflow
); //wire [8:0] sum;assign sum = {a[7],a} + {b[7],b};assign s = sum[7:0];assign overflow = sum[8] ^ sum[7];endmodule
知识点:
提示:当两个正数相加产生负数,或者两个负数相加产生正数时,就会发生带符号溢出。有几种检测溢出的方法:可以通过比较输入和输出数字的符号来计算溢出,也可以从位n和n-1的进位导出溢出。
6 100-bit binary adder | 100位二进制加法器
题目:
创建一个 100 位二进制加法器。该加法器将两个 100 位数字和一个进位相加,得到一个 100 位和及进位输出。
答案:
module top_module( input [99:0] a, b,input cin,output cout,output [99:0] sum );wire [100:0] a_t,b_t;assign a_t = {1'b0,a};assign b_t = {1'b0,b};assign {cout,sum} = a_t + b_t + cin;endmodule
参考答案:
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
7 4-digit BCD adder | 4位BCD加法器
题目:
为您提供了一个名为 bcd_fadd 的 BCD(二进制编码的十进制)一位数加法器,它将两个 BCD 数字和进位相加,并产生一个和及进位输出。
module bcd_fadd (
input [3:0] a,
input [3:0] b,
input cin,
output cout,
output [3:0] sum );
实例化 bcd_fadd 的 4 个副本以创建一个 4 位 BCD 行波进位加法器。您的加法器应将两个 4 位 BCD 数字(打包成 16 位向量)和一个进位相加,以产生一个 4 位和及进位输出。
答案:
module top_module ( input [15:0] a, b,input cin,output cout,output [15:0] sum );wire [2:0] cout_t;bcd_fadd addr0(.a(a[3:0]),.b(b[3:0]),.cin(cin),.cout(cout_t[0]),.sum(sum[3:0]));bcd_fadd addr1(.a(a[7:4]),.b(b[7:4]),.cin(cout_t[0]),.cout(cout_t[1]),.sum(sum[7:4]));bcd_fadd addr2(.a(a[11:8]),.b(b[11:8]),.cin(cout_t[1]),.cout(cout_t[2]),.sum(sum[11:8]));bcd_fadd addr3(.a(a[15:12]),.b(b[15:12]),.cin(cout_t[2]),.cout(cout),.sum(sum[15:12]));endmodule
- END -
公z号/CSDN搜索【望森FPGA】,查看更多FPGA资讯~
相关文章请到我的主页查看