7: Toán tử logic

Một phần của tài liệu Verilog coding for logic synthesis ICdesignVN com (Trang 94 - 101)

4. 7: Danh sách nhạ y( Sentivity list)

4.8. 7: Toán tử logic

Toán tử logic tác ựộng lên một nhóm toán hạng và kết quả trả về là giá trị nhị phân 0 hay 1. Toán hạng có thể ựơn bit hay ựa bit nhưng kết quả luôn là ựơn bit. Có ba loại toán tử logic khác nhau ựược dùng trong Verilog.

Ớ && : đây là toán tử logic AND thực hiện chức năng hàm AND và trả về giá trị dơn bit.

Ớ || : Toán tử OR thực hiện phép toán OR và cũng trả về một giá trị ựơn bit. Ớ ! : Thực hiện toán tử logic NOT, nó thực hiện phép ựảo một hàm nào ựó

và trã về giá trị ựơn bit.

module example (inputA, inputB, inputC, inputD, outputA, outputB, outputC, outputD);

input inputA, inputB, inputC; input [2:0] inputD;

output outputA, outputB, outputC; output [2:0] outputD;

// for logical AND

assign outputA = inputA && inputB;

// for logical OR

assign outputB = inputA || inputB;

// for logical NOT

assign outputC = !inputC;

// for vector format

assign outputD = {inputA, inputB, inputC} && inputD;

endmodule

Testbench mô phỏng mạch thiết kế ở trên module logical_tb();

reg inputA_reg, inputB_reg, inputC_reg; reg [2:0] inputD_reg;

wire outputA_wire, outputB_wire, outputC_wire; wire outputD_wire; integer i,j; initial begin for (i=0;i<8;i=i+1) begin

{inputA_reg, inputB_reg, inputC_reg} =i; for(j=0;j<8;j=j+1)

begin

inputD_reg =j; #10;

end end

// Instantiating your design here

example DUT (.inputA(inputA_reg), .inputB(inputB_reg), .inputC(inputC_reg), .inputD(inputD_reg), .outputA(outputA_wire), .outputB(outputB_wire),

.outputC(outputC_wire),.outputD(outputD_wire)); initial

begin

$monitor ("inputA %b, inputB %b, inputC %b, inputD %h, outputA %b, outputB %b, outputC %b, outputD %h",inputA_reg, inputB_reg, inputC_reg, inputD_reg, outputA_wire, outputB_wire, outputC_wire, outputD_wire);

end

endmodule

4.8.8 : Toán tử Bitwise

Toán tử bitwise giống như toán tử logic ngoại trừ toán tử này tác ựộng lên bus và trả về giá trị bus. Vắ dụ, nếu một toán tử bitwise ựược dùng trên hai toán hạng 3 bit kết quả của toán hạng cũng sẽ có 3 bit. Có 4 kiểu toán hạng bitwise :

& đây là toán tử bitwise AND. Nó thực hiện hàm AND và trả về một giá trị tương

ứng với ựộng rộng của toán hạng.

| đây là toán tử bitwise OR. Nó thực hiện hàm OR và trả về một giá trị tương ứng

~ đây là toán tử bitwise NOT. Nó thực hiện hàm NOT và trả về một giá trị tương

ứng với ựộng rộng của toán hạng.

^ đây là toán tử bitwise XOR. Nó thực hiện hàm XOR và trả về một giá trị tương

ứng với ựộng rộng của toán hạng.

Vắ dụ 4.43 chỉ ra mã Verilog dùng cho toán tử bitwise. Giản ựồ hình 4.26 trình bày mạch logic tổng hợp ựược từ mã Verilog.

module bitwise ( inputA, inputB, inputC, inputD, outputA, outputB, outputC, outputD, outputE);

input inputA, inputB, inputC ; input [2:0] inputD;

output outputA, outputB, outputC, outputE; output [2:0] outputD;

wire outputA, ouputB, outputC, outputE; wire [2:0] ouputD;

// for bitwise AND

assign outputA = inputA & inputB;

// for bitwise OR

assign outputB = inputA | inputB;

// for bitwise NOT

assign outputC = ~inputC;

// for bitwise XOR

assign outputE = inputA^inputB;

// for vector format

assign ouputD = {inputA, inputB, inputC} & inputD;

Download bitwise.v

http://www.box.net/shared/00frcb60zv

Mạch tổng hợp ựược của mã trên

Verilog Testbench cho vắ dụ về toán tử bitwise module bitwise_tb ();

reg inputA_reg, inputB_reg, inputC_reg; reg [2:0] inputD;

wire outputA_wire, outputB_wire, outputC_wire, outputD_wire; wire [2:0] outputD_wire;

integer i,j; initial begin

for ( i=0; i <8; i = i+1) begin

{inputA_reg, inputB_reg, inputC_reg} = i; for ( j = 0; j <8; j = j +1) begin inputD_reg = j; #10; end end end

// This is where you instantiate your design under test bitwise DUT (.inputA(inputA_reg), .inputB(inputB_reg),

.inputC(inputC_reg),.inputD(inputD_reg), .outputA(outputA_wire), .ouputB(outputB_wire), .outputC(outputC_wire),

.outputD(outputD_wire),.outputE(outputE_wire)); initial

begin

$monitor ("inputA %b, inputB %b, inputC %b, inputD %h, outputA %b, outputB %b, outputC %b, outputD %h, outputE %b", inputA_reg, inputB_reg, inputC_reg,

inputD_reg, outputA_wire, outputB_wire, outputC_wire, outputD_wire, outputE_wire); end endmodule Download bitwise_tb.v http://www.box.net/shared/hbm45yk9bz

Kết quả mô phỏng bằng ActiveHDL

Một phần của tài liệu Verilog coding for logic synthesis ICdesignVN com (Trang 94 - 101)

Tải bản đầy đủ (PDF)

(122 trang)