例1. 四位全加器
module adder_4(
input [3:0] in_a,
input [3:0] in_b,
input in_c,
output [3:0] sum,
output out_c);
assign {out_c,sum} = in_a + in_b + in_c;
endmodule
全加器英语名称为full-adder,是用门电路实现两个二进制数相加并求出和的组合线路,称为一位全加器。一位全加器可以处理低位进位,并输出本位加法进位。多个一位全加器进行级联可以得到多位全加器。常用二进制四位全加器74LS283。一位全加器真值表如下:
例2.四位计数器
module count4(
input clk, //时钟信号
input reset,//复位信号
output [3:0]out
);
always@(posedge clk)begin
if(reset)
out <= 4'b0;
else
out <= out + 4'b0001;
end
endmodule
例3.“与-或-非”门电路
module AOI(
input wire A,
input wire B,
input wire C,
input wire D,
output wire out
);
assign out = ~((A&B)|(C&D));
endmodule
例4.用case实现四选一数据选择器
module mux_4(
input [3:0]in_1,
input [3:0]in_2,
input [3:0]in_3,
input [3:0]in_4,
input [1:0] sel,
output [3:0] out
);
always@(in_1 or in_2 or in_3 or in_4 or sel)begin
case(sel)
2'b00 : out <= in_1;
2'b01 : out <= in_2;
2'b10 : out <= in_3;
2'b11 : out <= in_4;
default:out <= 4'b0;
endcase
end
endmoudule
例5.同步置数、同步清零的计数器
module count{
input clk;
input reset;
input load; //置数使能
input [7:0] data;
output reg [7:0] out;
};
always@(posedge clk)begin //上升沿触发
if(reset) out <= 8'b0; //同步清零
else(load) out <= data; //同步预置
else out <= out + 1'b1;//计数
end
endmodule
例6.使用always语句完成简单的逻辑算数单元
`define add 3'd0
`define minus 3'd1
`define band 3'd2
`define bor 3'd3
`define bnot 3'd4
module alu(
input [7:0]a_in,
input [7:0]b_in,
input [2:0]opt_mode,
output[7:0]out
);
always@(opt_mode or a_in or b_in)begin
case(opt_mode)
`add : out <= a_in + b_in;
`minus : out <= a_in - b_in;
`band : out <= a_in & b_in;
`bor : out <= a_in | b_in;
`bnot : out <= ~a_in;
default : out <= 8'b0;
endcase
end
endmoudule
例8.使用begin-end串行块产生信号波形(与例9并行模块对比)
`timescale 10ns/1ns
module wave1{
output reg wave
};
parameter cycle = 10;
initial
begin
wave = 0;
#(cycle/2) wave = 1;
#(cycle/2) wave = 0;
#(cycle/2) wave = 1;
#(cycle/2) wave = 0;
#(cycle/2) wave = 1;
#(cycle/2) $finish ;
end
initial $monitor($time,,,"wave = %b",wave);
endmodule
例9. 使用fork-join 并行块产生信号波形
`timescale 10ns/1ns
module wave2{
output reg wave
};
parameter cycle = 10;
fork
wave = 0;
#(cycle) wave = 1;
#(2*cycle) wave = 0;
#(3*cycle) wave = 1;
#(4*cycle) wave = 0;
#(5*cycle) wave = 1;
#(6*cycle) $finish ;
join
initial $monitor($time,,,"wave = %b",wave);
endmodule
例10. 连续赋值方式定义2选1多路选择器
module MUX21_1(
input a,
input b,
input sel,
output wire out
);
assign out = (sel == 0)?a:b;//持续赋值,如果sel为0,out = a;否则out = b;
endmodule
例11.阻塞赋值方式定义的2选1多路选择器
module MUX21_2(
input a,
input b,
input sel,
output out
);
always@(a or b or sel)begin
if(sel == 0)
out = a;
else
out = b;
end
endmodule
例12.非阻塞赋值
module non_block{
input clk,
input a,
output reg b,
output reg c
);
always@(posedge clk)begin
b <= a;
c <= b;
end
endmodule
假设 输入 a = 1 则第一个时钟c输出为 b在上一个时刻的值。
例13.阻塞赋值
module block{
input clk,
input a,
output reg b,
output reg c
);
always@(posedge clk)begin
b = a;
c = b;
end
endmodule
假设 输入 a = 1 则第一个时钟c输出为 1。