【FPGA】Verilog 编码实现:与非门 | 或非门 | 异或门 | NAND/NOR/XOR 行为验证

本文介绍了NAND,NOR,XOR逻辑门的基本行为,并通过Verilog代码展示了如何实现4输入的这些门电路。每个门的布尔表达式被用来编写Verilog代码,并通过模拟验证了其正确性。此外,还讨论了4输入的ANDORInverter(AOI)门的Verilog实现和仿真过程。
摘要由CSDN通过智能技术生成

写在前面:本章主要内容为了解和确认 NAND/NOR/XOR 门的行为,并使用Verilog实现,生成输入信号后通过模拟,验证每个门的操作,并使用 FPGA 来验证 Verilog 实现的电路的行为。

本章目录:

Ⅰ. 前置知识

0x00 与非门(NAND)

0x01 或非门(NOR)

0x02 异或门(XOR)

Ⅱ. 练习(Assignment)

0x00  4-input NAND gate

0x01  4-input NOR gate

0x02  4-input XOR gate

0x03  4-input AOI(AND OR Inverter) gate


Ⅰ. 前置知识

0x00 与非门(NAND)

如果所有输入均为High (1),则输出为Low (0),在其他情况下,将产生High (1) 输出。

  • NAND 是 AND 运算符的否定结果

布尔表达式中以 "负乘法" 形式表现:

0x01 或非门(NOR)

如果所有输入均为 Low (0),则输出为 High(1),其中一个输入为高 (1) 则产生低功率 (0)。

  • NOR 是 OR 运算符的否定结果

布尔表达式中以 "否定合" 形式表现:

0x02 异或门(XOR)

如果 两个值不相同,则异或结果为1。如果 两个值相同,异或结果为0。

Ⅱ. 练习(Assignment)

0x00  4-input NAND gate

比较 AB 的布尔表达式,完成 A 和 B 的 Verilog 代码,通过 Simulation 结果进行比较。

💬 Design source:

`timescale 1ns / 1ps

module input_4_NAND(
    // Input the var
    input a, b, c, d,
    // Output the var
    output e, f, g
    );
   
// NAND = NOT + AND     
assign e = ~(a & b);    // a and b then inv   
assign f = ~(e & c);    // e and c then inv
assign g = ~(f & d);    // f and d then inv

endmodule

💬 Simulation:

`timescale 1ns / 1ps

module input_4_NAND_tb;
reg aa, bb, cc, dd;
wire e, f, g;

input_4_NAND u_input_4_NAND (
    .a(aa),
    .b(bb),
    .c(cc),
    .d(dd),
    .e(e),
    .f(f),
    .g(g)
    );

initial aa = 1'b0;
initial bb = 1'b0;
initial cc = 1'b0;
initial dd = 1'b0;

always aa = #100 ~aa;
always bb = #200 ~bb;
always cc = #400 ~cc;
always dd = #800 ~dd;

initial begin
    #1600
    $finish;
end

endmodule

🚩 运行结果如下:

💡 解读:assign 语句中,用取反运算符 ~ 和或运算符 | 实现了 4 个输入取反或运算,并将结果分别赋值给输出变量 e, f, g

0x01  4-input NOR gate

比较 AB 的布尔表达式,完成 A 和 B 的 Verilog 代码,通过 Simulation 结果进行比较。

💬 Design source:

`timescale 1ns / 1ps

module input_4_NOR(
    /* Input the var */
    input a, b, c, d,
    /* Output the var */
    output e, f, g
    );
    
/* NOR = NOT + OR */
assign e = ~(a | b);
assign f = ~(e | c);
assign g = ~(f | d);

endmodule

💬 Testbench:

`timescale 1ns / 1ps

// input_4_NOR_tb

module input_4_NOR_tb;
// input
reg aa, bb, cc, dd;
// output
wire e, f, g;

input_4_NOR u_input_4_NOR (
    .a(aa),
    .b(bb),
    .c(cc),
    .d(dd),
    
    .e(e),
    .f(f),
    .g(g)
    );

initial aa = 1'b0;
initial bb = 1'b0;
initial cc = 1'b0;
initial dd = 1'b0;

always aa = #100 ~aa;
always bb = #200 ~bb;
always cc = #400 ~cc;
always dd = #800 ~dd;

initial begin
    #1600
    $finish;
end

endmodule

🚩 运行结果如下:

0x02  4-input XOR gate

比较 AB 的布尔表达式,完成 A 和 B 的 Verilog 代码,通过 Simulation 结果进行比较。

💬 Design source:

`timescale 1ns / 1ps

module input_4_XOR(
    /* Input the var */
    input a, b, c, d,
    /* Output the var */
    output e, f, g
);

/* XOR */
assign e = a ^ b;
assign f = e ^ c;
assign g = f ^ d;

endmodule

💬 Testbench:

`timescale 1ns / 1ps

module input_4_XOR_tb;
// input
reg aa, bb, cc, dd;
// output
wire e, f, g;

input_4_XOR u_input_4_XOR (
    .a(aa),
    .b(bb),
    .c(cc),
    .d(dd),
    
    .e(e),
    .f(f),
    .g(g)
    );

initial aa = 1'b0;
initial bb = 1'b0;
initial cc = 1'b0;
initial dd = 1'b0;

always aa = #100 ~aa;
always bb = #200 ~bb;
always cc = #400 ~cc;
always dd = #800 ~dd;

initial begin
    #1600
    $finish;
end

endmodule

🚩 运行结果如下:

0x03  4-input AOI(AND OR Inverter) gate

💬 Design source:

`timescale 1ns / 1ps


module inpu_4_AOI (
    /* Input the var */
    input a, b, c, d,
    /* Output the var */
    output e, f, g
);

/* AOI */
assign e = a & b;
assign f = e & c;
assign g = ~(e | f);

endmodule

💬 Testbench:

`timescale 1ns / 1ps

module inpu_4_AOI_tb;
// input
reg aa, bb, cc, dd;
// output
wire e, f, g;

inpu_4_AOI u_inpu_4_AOI (
    .a(aa),
    .b(bb),
    .c(cc),
    .d(dd),
    
    .e(e),
    .f(f),
    .g(g)
    );

initial aa = 1'b0;
initial bb = 1'b0;
initial cc = 1'b0;
initial dd = 1'b0;

always aa = #100 ~aa;
always bb = #200 ~bb;
always cc = #400 ~cc;
always dd = #800 ~dd;

initial begin
    #1600
    $finish;
end

endmodule

🚩 运行结果如下:

📌 [ 笔者 ]   王亦优
📃 [ 更新 ]   2022.9.20
❌ [ 勘误 ]   /* 暂无 */
📜 [ 声明 ]   由于作者水平有限,本文有错误和不准确之处在所难免,
              本人也很想知道这些错误,恳望读者批评指正!

📜 参考资料 

Introduction to Logic and Computer Design, Alan Marcovitz, McGrawHill, 2008

Microsoft. MSDN(Microsoft Developer Network)[EB/OL]. []. .

百度百科[EB/OL]. []. https://baike.baidu.com/.

评论 51
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王平渊

喜欢的话可以支持下我的付费专栏

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值