Example:
- module Full_Adder(x, y, ci, sum, co);
- input x, y, ci;
- output sum, co;
- assign {co, sum} = x + y + ci;
- endmodule
- module Full_Adder2(x, y, ci, sum, co);
- input [1:0] x, y;
- input ci;
- output [1:0] sum;
- output co;
- wire co_t;
- Full_Adder fa1(x[0], y[0], ci, sum[0], co_t);
- Full_Adder fa2(x[1], y[1], co_t, sum[1], co);
- endmodule
- `timescale 1ns / 1ns
- module Adder2TB;
- reg [1:0] A_t;
- reg [1:0] B_t;
- wire [1:0] R_t;
- reg CI_t;
- wire CO_t;
- // module Full_Adder2(x, y, ci, sum, co);
- Full_Adder2 fa2(.x(A_t),
- .y(B_t),
- .ci(CI_t),
- .sum(R_t),
- .co(CO_t));
- initial
- begin
- CI_t = 0;
- //case 0
- A_t <= 0; B_t <= 0;
- #10 $display("A=%b; B=%b; CI=%b; Result=%b; CO=%b", A_t, B_t, CI_t, R_t, CO_t);
- //case 1
- A_t <= 1; B_t <= 0;
- #10 $display("A=%b; B=%b; CI=%b; Result=%b; CO=%b", A_t, B_t, CI_t, R_t, CO_t);
- //case 2
- A_t <= 1; B_t <= 1;
- #10 $display("A=%b; B=%b; CI=%b; Result=%b; CO=%b", A_t, B_t, CI_t, R_t, CO_t);
- //case 3
- A_t <= 2; B_t <= 1;
- #10 $display("A=%b; B=%b; CI=%b; Result=%b; CO=%b", A_t, B_t, CI_t, R_t, CO_t);
- CI_t = 1;
- //case 4
- A_t <= 0; B_t <= 0;
- #10 $display("A=%b; B=%b; CI=%b; Result=%b; CO=%b", A_t, B_t, CI_t, R_t, CO_t);
- //case 5
- A_t <= 1; B_t <= 0;
- #10 $display("A=%b; B=%b; CI=%b; Result=%b; CO=%b", A_t, B_t, CI_t, R_t, CO_t);
- //case 6
- A_t <= 1; B_t <= 1;
- #10 $display("A=%b; B=%b; CI=%b; Result=%b; CO=%b", A_t, B_t, CI_t, R_t, CO_t);
- //case 7
- A_t <= 2; B_t <= 1;
- #10 $display("A=%b; B=%b; CI=%b; Result=%b; CO=%b", A_t, B_t, CI_t, R_t, CO_t);
- end
- endmodule
Console 輸出:
Supplement:
* ModelSim教學
* Verilog By Example
沒有留言:
張貼留言