Verilog 编程巩固
一、Verilog 简单电路例题实验
-
在线 Verilog 编程网站使用
进入以下连接网页后即可点击其中例题进行练习
🔗https://hdlbits.01xz.net/wiki/Problem_sets#Verilog_Language
进入网站后点击其中的例题进入编程界面
在代码框中输入代码后点击 Submit 提交代码,网站将会运行代码并给出结果
-
门电路
2.1 与门(AND gate)
与门真值表
Verilog 代码:
module top_module(
input a,
input b,
output out );
assign out = a & b;
endmodule
结果如下
2.2 或非门(NOR gate)
或非门真值表
结果如下
2.3 异或非门(XNOR gate)
异或非门真值表
Verilog 代码:
module top_module(
input a,
input b,
output out );
assign out= !((!a & b) | (a & !b));
endmodule
结果如下
2. 组合逻辑电路
2.1 半加器(Half adder)
半加器真值表
Verilog 代码:
module top_module(
input a, b,
output cout, sum );
assign sum = a ^ b;
assign cout = a & b;
endmodule
2.2 全加器(Full adder)
全加器真值表
Verilog 代码:
module top_module(
input a, b, cin,
output cout, sum );
assign sum = a ^ b ^ cin;
assign cout = (a & b) | (a & cin) | (b & cin);
endmodule
结果如下
2.3 选择器(2-to-1 multiplexer)
当 SEL = 0 时,选择 B 的值
当 SEL = 1 时,选择 A 的值
选择器真值表
Verilog 代码:
module top_module(
input a, b, sel,
output out );
assign out=(sel) ? b : a;
endmodule
结果如下
3. 时序逻辑电路
3.1 D 触发器(D flip-flop)
D 触发器真值表
Verilog 代码:
module top_module (
input clk, // Clocks are used in sequential circuits
input d,
output reg q );//
// Use a clocked always block
// copy d to q at every positive edge of clk
// Clocked always blocks should use non-blocking assignments
always@(posedge clk) begin
q <= d;
end
endmodule
结果如下
3.2 可复位的 D 触发器(DFF with reset)
Verilog 代码:
module top_module (
input clk,
input reset, // Synchronous reset
input [7:0] d,
output [7:0] q
);
always@(posedge clk) begin
if(reset)
q <= 8'b0;
else
q <= d;
end
endmodule
结果如下
3.3 8位 D 触发器
Verilog 代码:
module top_module (
input clk,
input [7:0] d,
output [7:0] q
);
always@(posedge clk) begin
q <= d;
end
endmodule
结果如下