Menu
CoddyTech

与门、或门和非门

本课属于 Coddy Verilog 学习旅程的“基础知识”单元。第 40 课,共 90 课。

本课介绍三种最基本的逻辑门:AND、OR 和 NOT。这些逻辑门构成了数字逻辑设计的基础。

与门

只有当所有输入都是 1 时,与门才输出 1。

真值表(2 个输入):

about
000
010
100
111

Verilog 门原语:

and(out, a, b);

等效的连续赋值:

assign out = a & b;

或门

当至少一个输入为 1 时,或门输出 1。

真值表(2 个输入):

about
000
011
101
111

Verilog 门原语:

or(out, a, b);

等效的连续赋值:

assign out = a | b;

非门

非门输出其单个输入的相反值。它也称为反相器。

真值表:

aout
01
10

Verilog 门原语:

not(out, a);

连续赋值的等效写法:

assign out = ~a;

多个输入

与门和或门可以有多于 2 个输入:

and(out, a, b, c);     // 3 输入与门(out = a & b & c)
or(out, x, y, z, w);   // 4 输入或门

非门始终恰好有 1 个输入。

代码示例

module and_or_not (
  input a, b,
  output and_out,
  output or_out,
  output not_out
);
  and(and_out, a, b);   // 与门
  or(or_out, a, b);     // 或门
  not(not_out, a);      // 非门(反相器)
endmodule
challenge icon

挑战

根据任务添加缺少的门电路原语。

要做什么:

  1. 创建一个 AND 门,输出为 and_result,输入为 p 和 q
  2. 创建一个 OR 门,输出为 or_result,输入为 p 和 q
  3. 创建一个 NOT 门,输出为 not_result,输入为 p

亲手试试

module gates_challenge (
  input p,
  input q,
  output and_result,
  output or_result,
  output not_result
);
  
  // TODO: 添加与门(and_result = p & q)
  
  // TODO: 添加或门(or_result = p | q)
  
  // TODO: 添加非门(not_result = ~p)

endmodule
quiz icon自我测试

这节课包含一个小测验。开始学习即可作答,并记录你的进度。

基础知识中的所有课

自己练习: Verilog 在线编译器