Professional Documents
Culture Documents
Verilog
Verilog
endmodule
1module fulladder(a,b,ic,o,oc);
2
3input a,b,ic;
4
5output o,oc;
6
7assign o = (~ic & ((a & ~b) | (~a & b)) ) | (ic & ~((a & ~b) | (~a & b)) );
8
9assign oc = (a & b) | (b & ic) | (ic & a);
10
11endmodule
1. Verilog Code:
module mux (in0, in1, in2, in3,sel, out);
input in0,in1,in2,in3;
input [1:0] sel;
output reg out;
always@(in0 or in1 or in2 or in3 or sel)
begin
if (sel==2’b00)
out = in0;
else if (sel==2’b01)
out =in1;
else if (sel==2’b10)
out =in2;
else
out = in3;
end
endmodule
In this example, the mux module has four inputs (in0,in1,in2,in3), two control inputs
(sel[1:0]), and one output (out). The output is determined by the values of the
control inputs (sel). If sel is 2'b00, then the output is equal to the first input (in0).
If sel is 2'b01, then the output is equal to the second input (in1). If sel is 2'b10,
then the output is equal to the third input (in2). If sel is 2'b11, then the output is
equal to the fourth input (in3).
The if-else statement is a common control flow statement used in Verilog to
implement conditional logic. In this example, the if-else statement is used to compare
the control inputs to the possible selection values and assign the output accordingly.