Verilog Program
Verilog Program
Verilog Program:
Gate Level Modelling:
module half_adder_structural (
input a, // Input 'a'
input b, // Input 'b'
output s, // Output 's' (Sum)
output c // Output 'c' (Carry)
);
xor gate_xor (s, a, b); // XOR gate for sum
and gate_and (c, a, b); // AND gate for carry
endmodule
Dataflow Modelling:
module half_adder_dataflow (
input a, // Input 'a'
input b, // Input 'b'
output s, // Output 's' (Sum)
output c // Output 'c' (Carry)
);
Behavioural Modelling:
module half_adder_behavioural (
input a, // Input 'a'
input b, // Input 'b'
output s, // Output 's' (Sum)
output c // Output 'c' (Carry)
);
// Combinational logic equations for sum and carry
always @(a,b)
s = a ^ b; // XOR operation for sum
c = a & b; // AND operation for carry
endmodule
// Test case 1
d = 1'b1; // Assign input 'a' as 1
$display("a=%b", d); // Display value of input 'a'
e = 1'b1; // Assign input 'b' as 1
$display("b=%b", e); // Display value of input 'b'
#10; // Wait for 10 time units
$display("s=%b", f); // Display value of output 's' (Sum)
$display("c=%b", g); // Display value of output 'c' (Carry)
// Test case 2
d = 1'b0; // Assign input 'a' as 0
$display("a=%b", d); // Display value of input 'a'
e = 1'b1; // Assign input 'b' as 1
$display("b=%b", e); // Display value of input 'b'
#10; // Wait for 10 time units
$display("s=%b", f); // Display value of output 's' (Sum)
$display("c=%b", g); // Display value of output 'c' (Carry)
// Test case 3
d = 1'b1; // Assign input 'a' as 1
$display("a=%b", d); // Display value of input 'a'
e = 1'b0; // Assign input 'b' as 0
$display("b=%b", e); // Display value of input 'b'
#10; // Wait for 10 time units
$display("s=%b", f); // Display value of output 's' (Sum)
$display("c=%b", g); // Display value of output 'c' (Carry)
// Test case 4
d = 1'b0; // Assign input 'a' as 0
$display("a=%b", d); // Display value of input 'a'
e = 1'b0; // Assign input 'b' as 0
$display("b=%b", e); // Display value of input 'b'
#10; // Wait for 10 time units
$display("s=%b", f); // Display value of output 's' (Sum)
$display("c=%b", g); // Display value of output 'c' (Carry)
end
endmodule
Full Adder
Verilog Program:
Gate Level Modelling:
module full_adder_s (
input a,b, Cin,
output sum,carry
);
wire w1,w2,w3,w4; //Internal connections
xor(w1,a,b);
xor(sum,w1, Cin); //Sum output
and(w2,a,b);
and(w3,b, Cin);
and(w4, Cin,a);
or(carry,w2,w3,w4); //carry output
endmodule
Dataflow Modelling:
module full_adder_d (
input a,b, Cin,
output sum,carry
);
assign sum = a ^ b ^ Cin;
assign carry = (a & b) | (b & Cin) | (Cin & a) ;
endmodule
Behavioural Modelling:
module full_adder_d (
input a,b, Cin,
output sum,carry
);
always @(a, b, Cin)
sum = a ^ b ^ Cin;
carry = (a & b) | (b & Cin) | (Cin & a) ;
endmodule