快捷搜索: 王者荣耀 脱发

【Verilog】基本逻辑门代码(二)

与门

//与门设计
module and_gate(
         A,
         B,
         Y
    );
input   A;
input   B;
output  Y;
assign  Y = A & B;
endmodule
module test(
    );
reg     aa;
reg     bb;
wire    yy;
and_gate f(.A(aa),.B(bb),.Y(yy));
initial begin   //  按时间定义变量的值
            aa <= 0;bb<=0;
    #10     aa <= 0;bb<=1;    //时间过10个单位
    #10     aa <= 1;bb<=0;
    #10     aa <= 1;bb<=1;
    #10     $stop;
end
endmodule

或门

/或门设计
module joint_gate(
         A,
         B,
         Y
    );
input   A;
input   B;
output  Y;
assign  Y = A | B;
endmodule
module test(
    );
reg     aa;
reg     bb;
wire    yy;
joint_gate f(.A(aa),.B(bb),.Y(yy));
initial begin   //  按时间定义变量的值
            aa <= 0;bb<=0;
    #10     aa <= 0;bb<=1;    //时间过10个单位
    #10     aa <= 1;bb<=0;
    #10     aa <= 1;bb<=1;
    #10     $stop;
end
endmodule

与非门

//与非门设计
module nand_gate(
         A,
         B,
         Y
    );
input   A;
input   B;
output  Y;
assign  Y = ~(A & B);
endmodule
module test(
    );
reg     aa;
reg     bb;
wire    yy;
nand_gate f(.A(aa),.B(bb),.Y(yy));
initial begin   //  按时间定义变量的值
            aa <= 0;bb<=0;
    #10     aa <= 0;bb<=1;    //时间过10个单位
    #10     aa <= 1;bb<=0;
    #10     aa <= 1;bb<=1;
    #10     $stop;
end
endmodule

或非门

//或非门设计
module nor_gate(
         A,
         B,
         Y
    );
input   A;
input   B;
output  Y;
assign  Y = ~(A | B);
endmodule
module test(
    );
reg     aa;
reg     bb;
wire    yy;
nor_gate f(.A(aa),.B(bb),.Y(yy));
initial begin   //  按时间定义变量的值
            aa <= 0;bb<=0;
    #10     aa <= 0;bb<=1;    //时间过10个单位
    #10     aa <= 1;bb<=0;
    #10     aa <= 1;bb<=1;
    #10     $stop;
end
endmodule

异或门

异或门可以直接用^,我写复杂了

//异或门设计
module exclusive_OR_gate(
         A,
         B,
         Y
    );
input   A;
input   B;
output  Y;
assign  Y = (A & ~B) | (~A & B);
endmodule
module test(
    );
reg     aa;
reg     bb;
wire    yy;
exclusive_OR_gate f(.A(aa),.B(bb),.Y(yy));
initial begin   //  按时间定义变量的值
            aa <= 0;bb<=0;
    #10     aa <= 0;bb<=1;    //时间过10个单位
    #10     aa <= 1;bb<=0;
    #10     aa <= 1;bb<=1;
    #10     $stop;
end
endmodule
module exclusive_OR_gate(
         A,
         B,
         Y
    );
input   A;
input   B;
output  Y;
assign  Y = A ^ B;
endmodule

同或门

module not_exclusive_OR_gate(
         A,
         B,
         Y
    );
input   A;
input   B;
output  Y;
assign  Y = A ~^ B;
endmodule

4位与非门设计

//4位与非门设计
module nand_gate_4bits(
         A,
         B,
         Y
    );
input[3:0]   A;
input[3:0]   B;
output[3:0]  Y;
assign  Y = ~(A & B);
endmodule
module test(
    );
reg[3:0]     aa;
reg[3:0]     bb;
wire[3:0]    yy;
nand_gate_4bits f(.A(aa),.B(bb),.Y(yy));
initial begin   //  按时间定义变量的值
            aa <= 4b0000;bb<= 4b1111;
    #10     aa <= 4b0010;bb<= 4b0110;    //时间过10个单位
    #10     aa <= 4b0111;bb<= 4b0100;
    #10     aa <= 4b0000;bb<= 4b1110;
    #10     $stop;
end
endmodule

步骤

实现功能的模块: 首先确定端口的数量,在模块内部确定端口是输入端口,还是输出端口 确定端口的位宽,确定端口以及信号的数据类型,默认是wire型 实现功能。

Test模块: 确定信号的数据类型、位宽。 调用功能模块, 改变信号值。

两个十六位相加,计算结果

module add(x,y,result);
input[15:0] x,y;
output[15:0] result;

assign result = x + y;
module test(
    );
reg[15:0] x,y;
wire[15:0] result;

add f(.x(x),.y(y),.result(result));
initial begin
    x <= 16d23;y <= 16d45;
#10   x <= 16d12;y <= 16d67;
#10   $stop;
end
endmodule
经验分享 程序员 微信小程序 职场和发展