一、反相门
'timescale 1ns/10ps /*时间单位*/
module inv(
A, /*A,Y是端口,INV是module的名字*/
Y
);
input A;
ouput Y; /*定义端口的属性*/
assign Y=~A;
endmodule
//----testbench of inv-----
//反相器例化,异名例化
module inv_tb;
reg aa;
wire yy;
inv inv(
.A(aa),
.Y(yy)
);
initial begin
aa<=0;
#10 aa<=1;
#10 aa<=0;
#10 aa<=1; /*过10个时间单位变换一次*/
#10 $stop;
end
endmodule
二、与非门
//----testbench of inv-----
module nand_gate_tb;
reg aa,bb;
wire yy;
nand_gate nand_gate(
.A(aa),
.B(bb),
.Y(yy)
);
initial begin
aa<=0;bb<=0; /*aa为0,bb为0*/
#10 aa<=0;bb<=1;
#10 aa<=1;bb<=0;
#10 aa<=1;bb<=1; /*过10个时间单位变换一次*/
#10 $stop;
end
endmodule
三、二选一逻辑(用问号冒号语句实现二选一)
always语句实现(always语句块写组合逻辑)
//----testbench of inv-----
module fn_sw_tb;
reg a,b,sel;
wire y;
fn_sw fn_sw(
.a(a),
.b(b),
.sel(sel),
.y(y)
);
initial begin
a<=0;b<=0;sel<=0;
#10 a<=0;b<=0;sel<=0;
#10 a<=0;b<=0;sel<=1;
#10 a<=0;b<=1;sel<=0; /*过10个时间单位变换一次*/
#10 a<=0;b<=1;sel<=1;
#10 a<=1;b<=0;sel<=0;
#10 a<=1;b<=0;sel<=1;
#10 a<=1;b<=1;sel<=0;
#10 a<=1;b<=1;sel<=1;
#10 $stop;
end
endmodule
四、四选一逻辑选择器(用case语句实现多路选择;用always #语句遍历逻辑值)
//----testbench of inv-----
module fn_sw_tb;
reg[3:0] absel;
wire y;
fn_sw_4 fn_sw_4(
.a(absel[0]),
.b(absel[1]),
.sel(absel[3:2]),
.y(y)
);
initial begin
absel<=0;
#200 $stop; /*变换20次结束*/
end
always#10absel<=absel+1; /*每过10ns加1*/
endmodule
五、补码转换
module comp_conv(
a,
a_comp
);
input[7:0] a;
output[7:0] a_comp;
wire[6:0] b; //按位取反的幅度位
wire[7:0] y; //负数的补码
assign b=~a[6:0];
assign y[6:0]=b+1; //按位取反加1
assign y[7]=a[7]; //符号位不变
assign a_comp=a[7]?y:a;
//----testbench of inv-----
module comp_conv_tb;
reg[7:0] a_in;
wire[7:0] y_out;
comp_conv comp_conv(
.a(a_in),
.a_comp(y_out
);
initial begin
a_in<=0;
#3000 //8位数一共256个,每个10ns
end
always#10 a_in<=a_in+1;
endmodule
六、7段数码管