目錄
1位加法器
8位加法器
8位補碼加減法器?
32位補碼加減法器?
1位加法器
?文章來源:http://www.zghlxwxcb.cn/news/detail-514583.html
?
//
//創(chuàng)建日期:2022/09/21 19:05:50
//設計名稱:一位加法器
//課程名稱:adder_1
//說明:輸入operand1,operand2和進位信號cin
// 輸出當前位result和進位cout
//依賴項:
//
//版次:
//版本0.01-文件已創(chuàng)建
//其他注釋:
//
//
module adder_1(operand1,operand2,cin,result,cout);
input operand1; // 加數(shù)1
input operand2; // 加數(shù)2
input cin; // 進位輸入
output result; // 當前位輸出
output cout; // 進位
wire c[3:0]; // 接線
// 當前位為三個輸入的異或,三個輸入為1的個數(shù)為奇數(shù)時,當前位為1
xor x1(c[0],operand1,operand2),
x2(result,c[0],cin);
// 進位信號為 與非(與非(輸入1,輸入2),與非(異或(輸入1,輸入2),進位))
nand na1(c[1],cin,c[0]),
na2(c[2],operand1,operand2),
na3(cout,c[1],c[2]);
endmodule
8位加法器
//
//創(chuàng)建日期:2022/09/21 19:05:50
//設計名稱:8位加法器
//課程名稱:adder_8
//說明:輸入8位operand1,8位operand2和進位信號cin
// 輸出8位result和進位cout
//依賴項:
// adder_1.v
//版次:
//版本0.01-文件已創(chuàng)建
//其他注釋:
//
//
module adder_8(operand1,operand2,cin,result,cout);
input [7:0]operand1; // 加數(shù)1
input [7:0]operand2; // 加數(shù)2
input cin; // 進位輸入
output [7:0]result; // 當前位輸出
output cout; // 進位
wire [6:0] c; // 第 i 位的進位輸入
adder_1 a0(operand1[0],operand2[0],cin,result[0],c[0]);
adder_1 a1(operand1[1],operand2[1],c[0],result[1],c[1]);
adder_1 a2(operand1[2],operand2[2],c[1],result[2],c[2]);
adder_1 a3(operand1[3],operand2[3],c[2],result[3],c[3]);
adder_1 a4(operand1[4],operand2[4],c[3],result[4],c[4]);
adder_1 a5(operand1[5],operand2[5],c[4],result[5],c[5]);
adder_1 a6(operand1[6],operand2[6],c[5],result[6],c[6]);
adder_1 a7(operand1[7],operand2[7],c[6],result[7],cout);
endmodule
8位補碼加減法器?
?
//
//創(chuàng)建日期:2022/09/21 19:05:50
//設計名稱:8位補碼加減法器
//課程名稱:adder_subtract_8
//說明:輸入8位operand1,8位operand2和加減信號add_or_sub(0+,1-)
// 輸出8位result和溢出提示overflow
//依賴項:
// adder_1.v
//版次:
//版本0.01-文件已創(chuàng)建
//其他注釋:
//
//
module adder_subtract_8(operand1,operand2,add_or_sub,result,overflow);
input [7:0]operand1; // 操作數(shù)1
input [7:0]operand2; // 操作數(shù)2
input add_or_sub; // 加減信號(0+,1-)
output [7:0]result; // 當前位輸出
output overflow; // 溢出提示
wire [7:0] c; // 第 i 位的進位輸入
wire [7:0] op2; // 操作數(shù)2的負數(shù)補碼
// 由add_or_sub控制輸入的是正數(shù)還是負數(shù)
xor x0(op2[0],add_or_sub,operand2[0]),
x1(op2[1],add_or_sub,operand2[1]),
x2(op2[2],add_or_sub,operand2[2]),
x3(op2[3],add_or_sub,operand2[3]),
x4(op2[4],add_or_sub,operand2[4]),
x5(op2[5],add_or_sub,operand2[5]),
x6(op2[6],add_or_sub,operand2[6]),
x7(op2[7],add_or_sub,operand2[7]);
// 加法器
adder_1 a0(operand1[0],op2[0],add_or_sub,result[0],c[0]);
adder_1 a1(operand1[1],op2[1],c[0],result[1],c[1]);
adder_1 a2(operand1[2],op2[2],c[1],result[2],c[2]);
adder_1 a3(operand1[3],op2[3],c[2],result[3],c[3]);
adder_1 a4(operand1[4],op2[4],c[3],result[4],c[4]);
adder_1 a5(operand1[5],op2[5],c[4],result[5],c[5]);
adder_1 a6(operand1[6],op2[6],c[5],result[6],c[6]);
adder_1 a7(operand1[7],op2[7],c[6],result[7],c[7]);
xor x8(overflow,c[6],c[7]);
endmodule
32位補碼加減法器?
//
//創(chuàng)建日期:2022/09/26 13:24:05
//設計名稱:32位補碼加減法器
//課程名稱:adder_subtract_32
//說明:輸入32位operand1,32位operand2和加減信號add_or_sub(0+,1-)
// 輸出32位result和溢出提示overflow
//依賴項:
// adder_1.v
//版次:
//版本0.01-文件已創(chuàng)建
//其他注釋:
//
//
module adder_subtract_32(operand1,operand2,add_or_sub,result,overflow);
input [31:0]operand1; // 操作數(shù)1
input [31:0]operand2; // 操作數(shù)2
input add_or_sub; // 加減信號(0+,1-)
output [31:0]result; // 當前位輸出
output overflow; // 溢出提示
wire [31:0] c; // 第 i 位的進位輸入
wire [31:0] op2; // 操作數(shù)2的負數(shù)補碼
// 由add_or_sub控制輸入的是正數(shù)還是負數(shù)
xor x0(op2[0],add_or_sub,operand2[0]),
x1(op2[1],add_or_sub,operand2[1]),
x2(op2[2],add_or_sub,operand2[2]),
x3(op2[3],add_or_sub,operand2[3]),
x4(op2[4],add_or_sub,operand2[4]),
x5(op2[5],add_or_sub,operand2[5]),
x6(op2[6],add_or_sub,operand2[6]),
x7(op2[7],add_or_sub,operand2[7]),
x8(op2[8],add_or_sub,operand2[8]),
x9(op2[9],add_or_sub,operand2[9]),
x10(op2[10],add_or_sub,operand2[10]),
x11(op2[11],add_or_sub,operand2[11]),
x12(op2[12],add_or_sub,operand2[12]),
x13(op2[13],add_or_sub,operand2[13]),
x14(op2[14],add_or_sub,operand2[14]),
x15(op2[15],add_or_sub,operand2[15]),
x16(op2[16],add_or_sub,operand2[16]),
x17(op2[17],add_or_sub,operand2[17]),
x18(op2[18],add_or_sub,operand2[18]),
x19(op2[19],add_or_sub,operand2[19]),
x20(op2[20],add_or_sub,operand2[20]),
x21(op2[21],add_or_sub,operand2[21]),
x22(op2[22],add_or_sub,operand2[22]),
x23(op2[23],add_or_sub,operand2[23]),
x24(op2[24],add_or_sub,operand2[24]),
x25(op2[25],add_or_sub,operand2[25]),
x26(op2[26],add_or_sub,operand2[26]),
x27(op2[27],add_or_sub,operand2[27]),
x28(op2[28],add_or_sub,operand2[28]),
x29(op2[29],add_or_sub,operand2[29]),
x30(op2[30],add_or_sub,operand2[30]),
x31(op2[31],add_or_sub,operand2[31]);
// 加法器
adder_1 a0(operand1[0],op2[0],add_or_sub,result[0],c[0]);
adder_1 a1(operand1[1],op2[1],c[0],result[1],c[1]);
adder_1 a2(operand1[2],op2[2],c[1],result[2],c[2]);
adder_1 a3(operand1[3],op2[3],c[2],result[3],c[3]);
adder_1 a4(operand1[4],op2[4],c[3],result[4],c[4]);
adder_1 a5(operand1[5],op2[5],c[4],result[5],c[5]);
adder_1 a6(operand1[6],op2[6],c[5],result[6],c[6]);
adder_1 a7(operand1[7],op2[7],c[6],result[7],c[7]);
adder_1 a8(operand1[8],op2[8],c[7],result[8],c[8]);
adder_1 a9(operand1[9],op2[9],c[8],result[9],c[9]);
adder_1 a10(operand1[10],op2[10],c[9],result[10],c[10]);
adder_1 a11(operand1[11],op2[11],c[10],result[11],c[11]);
adder_1 a12(operand1[12],op2[12],c[11],result[12],c[12]);
adder_1 a13(operand1[13],op2[13],c[12],result[13],c[13]);
adder_1 a14(operand1[14],op2[14],c[13],result[14],c[14]);
adder_1 a15(operand1[15],op2[15],c[14],result[15],c[15]);
adder_1 a16(operand1[16],op2[16],c[15],result[16],c[16]);
adder_1 a17(operand1[17],op2[17],c[16],result[17],c[17]);
adder_1 a18(operand1[18],op2[18],c[17],result[18],c[18]);
adder_1 a19(operand1[19],op2[19],c[18],result[19],c[19]);
adder_1 a20(operand1[20],op2[20],c[19],result[20],c[20]);
adder_1 a21(operand1[21],op2[21],c[20],result[21],c[21]);
adder_1 a22(operand1[22],op2[22],c[21],result[22],c[22]);
adder_1 a23(operand1[23],op2[23],c[22],result[23],c[23]);
adder_1 a24(operand1[24],op2[24],c[23],result[24],c[24]);
adder_1 a25(operand1[25],op2[25],c[24],result[25],c[25]);
adder_1 a26(operand1[26],op2[26],c[25],result[26],c[26]);
adder_1 a27(operand1[27],op2[27],c[26],result[27],c[27]);
adder_1 a28(operand1[28],op2[28],c[27],result[28],c[28]);
adder_1 a29(operand1[29],op2[29],c[28],result[29],c[29]);
adder_1 a30(operand1[30],op2[30],c[29],result[30],c[30]);
adder_1 a31(operand1[31],op2[31],c[30],result[31],c[31]);
xor x32(overflow,c[31],c[30]);
endmodule
8位加減法器仿真
`timescale 1ns / 1ps
//
//創(chuàng)建日期:2022/09/21 20:19:20
//設計名稱:8位補碼加減法器測試
//課程名稱:testbench
//說明:輸入8位operand1,8位operand2和加減信號add_or_sub(0+,1-)
// 輸出8位result和溢出提示overflow
//依賴項:
// adder_1.v
// adder_subtract_8
//版次:
//版本0.01-文件已創(chuàng)建
//其他注釋:
//
//
module testbench;
reg [7:0] input_1; // 加數(shù) 1
reg [7:0] input_2; // 加數(shù) 2
reg add_or_sub; // 加減信號(0+,1-)
wire [7:0] output_1; // 輸出
wire overflow; // 溢出提示
//調(diào)用adder_subtract_8模塊
adder_subtract_8 uut(
.operand1(input_1), // 加數(shù)1
.operand2(input_2), // 加數(shù)2
.add_or_sub(add_or_sub), // 加減信號(0+,1-)
.result(output_1), // 當前位輸出
.overflow(overflow) //溢出提示
);
//初始化仿真
initial begin
// Initialize Inputs
input_1 = 0;
input_2 = 0;
add_or_sub = 0;
// Wait 100 ns for global reset to finish
#100;
// Add stimulus here
end
always #10 input_1 = $random; //$random為系統(tǒng)任務,產(chǎn)生一個隨機的8位數(shù)
always #10 input_2 = $random; //#10 表示等待10個單位時間(10ns),即每過10ns,賦值一個隨機的32位數(shù)
always #10 add_or_sub = {$random} % 2; //加了拼接符,{$random}產(chǎn)生一個非負數(shù),除2取余得到0或1
endmodule
結(jié)果示例,32位太多了,8位好算
文章來源地址http://www.zghlxwxcb.cn/news/detail-514583.html
到了這里,關(guān)于Verilog 加法器/減法器的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!