目录
前言
组合逻辑电路的特点是任意时刻的输出仅仅取决于输入信号,输入信号变化,输出立即变化,不依赖于时钟。
4组合逻辑
4.1与门
在 verilog 中以“&”表示按位与,如 c=a&b,在 a 和 b 都等于 1 时结果才为 1。代码与仿真实现、RTL 表示如下:
module top(
input a,
input b,
output c
);
assign c = a & b;
endmodule
//teachbeach代码
`timescale 1 ns/1 ns
module top_tb() ;
reg a ;
reg b ;
wire c ;
initial
begin
a = 0 ;
b = 0 ;
forever
begin
#({$random}%100)
a = ~a ;
#({$random}%100)
b = ~b ;
end
end
top t0
(.a(a),
.b(b),
.c(c)
) ;
endmodule
4.2 或门
在 verilog 中以“|”表示按位或,如 c = a|b ,在 a 和 b 都为 0 时结果才为 0。代码与仿真实现、RTL 表示如下:
module top(
input a,
input b,
output c
);
assign c = a | b;
endmodule
//teachbeach仿真代码
`timescale 1 ns/1 ns
module top_tb() ;
reg a ;
reg b ;
wire c ;
initial
begin
a = 0 ;
b = 0 ;
forever
begin
#({$random}%100)
a = ~a ;
#({$random}%100)
b = ~b ;
end
end
top t0(.a(a), .b(b),.c(c)) ;
endmodule
注意当位宽大于1时,按位或。
4.3 非门
在 verilog 中以“~”表示按位取反,如 b=~a,b 等于 a 的相反数。 代码与仿真实现、RTL 表示如下:
module top(
input a,
output b
);
assign b = ~a;
endmodule
//teachbeach仿真代码
`timescale 1 ns/1 ns
module top_tb() ;
reg a ;
wire b ;
initial
begin
a = 0 ;
forever
begin
#({$random}%100)
a = ~a ;
end
end
top t0(.a(a), .b(b)) ;
endmodule
4.4 比较器
在 verilog 中以大于“>”,等于”==”,小于”<”,大于等于”>=”,小于等于”<=”,不等于”!=”表
示,以大于举例,如 c= a > b ;表示如果 a 大于 b,那么 c 的值就为 1,否则为 0。代码与仿真实现、RTL 表示如下:
module top(
input a,
input b,
output c
);
assign c = a > b;
endmodule
//teachbeach仿真代码
`timescale 1 ns/1 ns
module top_tb() ;
reg a ;
reg b ;
wire c ;
initial
begin
a = 0 ;
b = 0 ;
forever
begin
#({$random}%100)
a = ~a ;
#({$random}%100)
b = ~b ;
end
end
top t0(.a(a), .b(b),.c(c)) ;
endmodule
4.5半加器与全加器
半加器:半加器不考虑从低位来的进位,所以称之为半加器,sum 表示相加结果,count 表示进位代码与仿真实现、RTL 表示如下:
module top(
input a,
input b,
output sum,
output count
);
assign sum = a ^ b;
assign count = a & b;
endmodule
//teachbeach仿真代码
`timescale 1 ns/1 ns
module top_tb() ;
reg a ;
reg b ;
wire sum ;
wire count ;
initial
begin
a = 0 ;
b = 0 ;
forever
begin
#({$random}%100)
a = ~a ;
#({$random}%100)
b = ~b ;
end
end
top t0(.a(a), .b(b),
.sum(sum), .count(count)) ;
endmodule
全加器:全加器需要加上低位来的进位信号cin,代码与仿真实现、RTL 表示如下:
module top(
input a,
input b,
input cin,
output sum,
output count
);
assign {count,sum} = a + b + cin;
endmodule
//teachbeach仿真代码
`timescale 1 ns/1 ns
module top_tb() ;
reg a ;
reg b ;
reg cin ;
wire sum ;
wire count ;
initial
begin
a = 0 ;
b = 0 ;
cin = 0 ;
forever
begin
#({$random}%100)
a = ~a ;
#({$random}%100)
b = ~b ;
#({$random}%100)
cin = ~cin ;
end
end
top t0(.cin(cin),.a(a), .b(b),
.sum(sum), .count(count)) ;
endmodule
4.6数据选择器
在 verilog 中经常会用到数据选择器,通过选择信号,选择不同的输入信号输出到输出端,四选一数据选择器,sel[1:0]为选择信号,a,b,c,d 为输入信号,Mux 为输出信号,代码与仿真实现、RTL 表示如下:
module top(
input a,
input b,
input c,
input d,
input [1:0] sel,
output reg Mux
);
always@(*)
begin
case(sel)
2'b00:Mux = a;
2'b01:Mux = b;
2'b10:Mux = c;
2'b11:Mux = d;
endcase
end
endmodule
//teachbeach仿真代码
`timescale 1 ns/1 ns
module top_tb() ;
reg a ;
reg b ;
reg c ;
reg d ;
reg [1:0] sel ;
wire Mux ;
initial
begin
a = 0 ;
b = 0 ;
c = 0 ;
d = 0 ;
forever
begin
#({$random}%100)
a = {$random}%3 ;
#({$random}%100)
b = {$random}%3 ;
#({$random}%100)
c = {$random}%3 ;
#({$random}%100)
d = {$random}%3 ;
end
end
initial
begin
sel = 2'b00 ;
#2000 sel = 2'b01 ;
#2000 sel = 2'b10 ;
#2000 sel = 2'b11 ;
end
top
t0(.a(a), .b(b),.c(c),.d(d), .sel(sel),
.Mux(Mux)) ;
endmodule
4.7 3-8 译码器
3-8译码器是一个很常用的器件,代码与仿真实现、RTL 表示如下:
module top
(
input [2:0] addr,
output reg [7:0] decoder
);
always @(addr)
begin
case(addr)
3'b000 : decoder = 8'b1111_1110 ;
3'b001 : decoder = 8'b1111_1101 ;
3'b010 : decoder = 8'b1111_1011 ;
3'b011 : decoder = 8'b1111_0111 ;
3'b100 : decoder = 8'b1110_1111 ;
3'b101 : decoder = 8'b1101_1111 ;
3'b110 : decoder = 8'b1011_1111 ;
3'b111 : decoder = 8'b0111_1111 ;
endcase
end
endmodule
//teachbeach仿真代码
`timescale 1 ns/1 ns
module top_tb() ;
reg [2:0] addr ;
wire [7:0] decoder ;
initial
begin
addr = 3'b000 ;
#2000 addr = 3'b001 ;
#2000 addr = 3'b010 ;
#2000 addr = 3'b011 ;
#2000 addr = 3'b100 ;
#2000 addr = 3'b101 ;
#2000 addr = 3'b110 ;
#2000 addr = 3'b111 ;
end
top
t0(.addr(addr),.decoder(decoder)) ;
endmodule
总结
常见的组合逻辑