4: Toán tử toán học

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

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

4.8.4: Toán tử toán học

Verilog cho phép 5 toán tử toán học khác nhau, bao gồm : Toán tử cộng ( Addition Operator)

Toán tử trừ ( Subtraction Operator) Toán tử nhân ( Multiplication Operator) Toán tử chia ( Division Operator) Toán tử lấy dư ( Modulus Operator)

Khi dùng những toán tử này người thiết kế cần cẩn thận về mạch logic ựược tạo ra trong quá trình tổng hợp có thể khác nếu những ràng buộc về thiết kế ựược dùng.

4.8.4.1 : Toán tử cộng Như tên chỉ ra, toán tử này cho phép thực hiện phép toán

cộng và ựược mã trong Verilog dùng kắ hiệu "+". module addition ( inputA, inputB, outputA); input inputA, inputB;

output [1:0] outputA; wire outputA;

assign outputA = inputA + inputB; endmodule

Vắ dụ 4.26 là mã Testbench dùng ựể mô phỏng hoạt ựộng của mạch cộng vừa thiết kế ở trên :

module addition_tb();

reg inputA_reg, inputB_reg; wire [1:0] outputA_wire; integer i,j; initial begin for ( i = 0; i < 2; i = i +1) begin inputA_reg = i; for ( j =0; j <2; j = j+1) begin inputB_reg = i;

#10; // delay for 10 time units end

end end

addition DUT (.inputA ( inputA_reg), .inputB(inputB_reg), .outputA(outputA_wire));

initial begin

$monitor ( "inputA %b inputB %b outputA %b%b", inputA_reg, inputB_reg,

outputA_wire[1],outputA_wire[0]); end

endmodule

Kết quả mô phỏng

4.8.4.2 : Toán tử trừ Như tên chỉ ra, toán tử này cho phép thực hiện phép toán trừ

và ựược mã trong Verilog dùng kắ hiệu "-". module subtraction ( inputA, inputB, outputA); input inputA, inputB;

output [1:0] outputA; wire outputA;

assign outputA = inputA - inputB;

endmodule

Vắ dụ 4.26 là mã Testbench dùng ựể mô phỏng hoạt ựộng của mạch trừ vừa thiết kế ở trên :

module subtraction_tb(); reg inputA_reg, inputB_reg; wire [1:0] outputA_wire; integer i,j; initial begin for ( i = 0; i < 2; i = i +1) begin inputA_reg = i; for ( j =0; j <2; j = j+1) begin inputB_reg = i;

#10; // delay for 10 time units end

end end

// This is where you instantiated your design under test

subtraction DUT (.inputA ( inputA_reg), .inputB(inputB_reg), .outputA(outputA_wire));

begin

$monitor ( "inputA %b inputB %b outputA %b%b", inputA_reg, inputB_reg, (adsbygoogle = window.adsbygoogle || []).push({});

outputA_wire[1],outputA_wire[0]); end

endmodule

Kết quả mô phỏng

4.8.4.2 : Toán tử nhân

Như tên chỉ ra, toán tử này cho phép thực hiện phép toán nhân và ựược mã trong Verilog dùng kắ hiệu "*".

module multiplication ( inputA, inputB, outputA); input [1:0] inputA, inputB;

output [3:0] outputA; wire outputA;

assign outputA = inputA * inputB;

endmodule

Vắ dụ 4.32 là mã Testbench dùng ựể mô phỏng hoạt ựộng của mạch trừ vừa thiết kế ở trên :

module multiplication_tb(); reg [1:0] inputA_reg, inputB_reg; wire [3:0] outputA_wire; integer i,j; initial begin for ( i = 0; i < 4; i = i +1) begin inputA_reg = i; for ( j =0; j <4; j = j+1) begin inputB_reg = i;

end end end

// This is where you instantiated your design under test

multiplication DUT (.inputA ( inputA_reg), .inputB(inputB_reg), .outputA(outputA_wire));

initial begin

$monitor ( "inputA %h inputB %h outputA %h", inputA_reg, inputB_reg,

outputA_wire); end

endmodule

Kết quả mô phỏng

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