wps wordpress廈門網(wǎng)站seo哪家好
關(guān)注?望森FPGA ?查看更多FPGA資訊
這是望森的第 10 期分享
作者 | 望森
來源 | 望森FPGA
目錄
1 Half adder | 半加器
2 Full adder | 全加器
3 3-bit binary adder | 3位二進(jìn)制加法器
4 Adder | 加法器
5 Signed addition overflow | 有符號(hào)數(shù)的加法溢出
6 100-bit binary adder | 100位二進(jìn)制加法器
7 4-digit BCD adder | 4位BCD加法器
本文中的代碼都能夠正常運(yùn)行,請放心食用😋~
練習(xí)的官方網(wǎng)站是:https://hdlbits.01xz.net/
注:作者將每個(gè)練習(xí)的知識(shí)點(diǎn)都放在了題目和答案之后
1 Half adder | 半加器
題目:
創(chuàng)建一個(gè)半加器。半加器將兩位相加(無進(jìn)位),然后得出和與進(jìn)位。
答案:
module top_module( input a, b,output cout, sum );always@(*) beginsum = a ^ b;cout = a & b;endendmodule
2 Full adder | 全加器
題目:
創(chuàng)建一個(gè)全加器。全加器將三位(包括進(jìn)位)相加,并得出和與進(jìn)位。
答案:
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位二進(jìn)制加法器
題目:
現(xiàn)在您已經(jīng)知道如何構(gòu)建全加器,請創(chuàng)建 3 個(gè)實(shí)例以創(chuàng)建 3 位二進(jìn)制行波進(jìn)位加法器。該加法器將兩個(gè) 3 位數(shù)和一個(gè)進(jìn)位相加,以產(chǎn)生 3 位和并輸出進(jìn)位。為了鼓勵(lì)您實(shí)際實(shí)例化全加器,還要在行波進(jìn)位加法器中輸出每個(gè)全加器的進(jìn)位。cout[2] 是最后一個(gè)全加器的最終進(jìn)位,也是您通??吹降倪M(jìn)位。
答案:
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 | 加法器
題目:
實(shí)現(xiàn)以下電路:
("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 | 有符號(hào)數(shù)的加法溢出
題目:
假設(shè)有兩個(gè) 8 位 2 的補(bǔ)碼數(shù),a[7:0] 和 b[7:0]。這兩個(gè)數(shù)相加得到 s[7:0]。還要計(jì)算是否發(fā)生了(有符號(hào))溢出。
答案:
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
知識(shí)點(diǎn):
提示:當(dāng)兩個(gè)正數(shù)相加產(chǎn)生負(fù)數(shù),或者兩個(gè)負(fù)數(shù)相加產(chǎn)生正數(shù)時(shí),就會(huì)發(fā)生帶符號(hào)溢出。有幾種檢測溢出的方法:可以通過比較輸入和輸出數(shù)字的符號(hào)來計(jì)算溢出,也可以從位n和n-1的進(jìn)位導(dǎo)出溢出。
6 100-bit binary adder | 100位二進(jìn)制加法器
題目:
創(chuàng)建一個(gè) 100 位二進(jìn)制加法器。該加法器將兩個(gè) 100 位數(shù)字和一個(gè)進(jìn)位相加,得到一個(gè) 100 位和及進(jìn)位輸出。
答案:
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加法器
題目:
為您提供了一個(gè)名為 bcd_fadd 的 BCD(二進(jìn)制編碼的十進(jìn)制)一位數(shù)加法器,它將兩個(gè) BCD 數(shù)字和進(jìn)位相加,并產(chǎn)生一個(gè)和及進(jìn)位輸出。
module bcd_fadd (
input [3:0] a,
input [3:0] b,
input cin,
output cout,
output [3:0] sum );
實(shí)例化 bcd_fadd 的 4 個(gè)副本以創(chuàng)建一個(gè) 4 位 BCD 行波進(jìn)位加法器。您的加法器應(yīng)將兩個(gè) 4 位 BCD 數(shù)字(打包成 16 位向量)和一個(gè)進(jìn)位相加,以產(chǎn)生一個(gè) 4 位和及進(jìn)位輸出。
答案:
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號(hào)/CSDN搜索【望森FPGA】,查看更多FPGA資訊~
相關(guān)文章請到我的主頁查看