Ece Verilog Code
Ece Verilog Code
Parity encoder :
1. Dataflow modelling:
module parity_encoder (
input [7:0] data_in,
output parity_out
);
endmodule
2. Behavioural modelling:
module parity_encoder(
input [N-1:0] data_in, // N-bit input data
output reg parity_bit // Output parity bit
);
// Initialize count to 0
count = 0;
// Count the number of '1's in the input data
for (i = 0; i < N; i = i + 1) begin
if (data_in[i] == 1'b1) begin
count = count + 1;
end
end
endmodule
3. Gate-Level modelling:
module parity_encoder(input [7:0] data, output parity);
assign xor_result = data[0] ^ data[1] ^ data[2] ^ data[3] ^ data[4] ^ data[5] ^ data[6] ^ data[7];
assign xor_out = xor_result[0] ^ xor_result[1] ^ xor_result[2] ^ xor_result[3];
Parity Decoder :
1. Structural Modeling:
module ParityDecoderStructural (
input [3:0] in_data,
output out
);
wire xor_output;
endmodule
2. Behavioural modelling:
module ParityDecoderBehavioral (
input [3:0] in_data,
output out
);
reg out;
wire xor_output;
endmodule
1. Structural Modeling:
module cl_adder_structural (
input [3:0] A,
input [3:0] B,
output [4:0] SUM,
output C_OUT
);
wire [3:0] G, P;
wire [4:0] C;
// Generate and Propagate signals
genvar i, j;
generate
for (i = 0; i < 4; i = i + 1) begin : gen_block
assign G[i] = A[i] & B[i];
assign P[i] = A[i] | B[i];
end
endgenerate
// Carry calculation
assign C[0] = 1'b0;
genvar k;
generate
for (k = 1; k < 5; k = k + 1) begin : gen_block2
assign C[k] = G[k-1] | (P[k-1] & C[k-1]);
end
endgenerate
endmodule
2. Behavioural modelling:
module cl_adder_behavioral (
input [3:0] A,
input [3:0] B,
output [4:0] SUM,
output C_OUT
);
// Carry bit
reg C_OUT;
// Internal signals for intermediate calculations
reg [4:0] sum_temp;
// Behavioral implementation
always @* begin
sum_temp = A + B;
C_OUT = sum_temp[4];
end
endmodule
3.Data flow modelling:
module cl_adder_dataflow (
input [3:0] A,
input [3:0] B,
output [4:0] SUM,
output C_OUT
);
endmodule
2. Behavioural modelling:
module magnitude_comparator_behavioral (
input [3:0] A,
input [3:0] B,
output A_greater, // A > B
output B_greater, // B > A
output equal // A = B
);
// Behavioral implementation
always @* begin
if (A > B) begin
A_greater = 1'b1;
B_greater = 1'b0;
equal = 1'b0;
end else if (A < B) begin
A_greater = 1'b0;
B_greater = 1'b1;
equal = 1'b0;
end else begin
A_greater = 1'b0;
B_greater = 1'b0;
equal = 1'b1;
end
end
endmodule
3. Structural Modeling:
module magnitude_comparator_structural (
input [3:0] A,
input [3:0] B,
output A_greater, // A > B
output B_greater, // B > A
output equal // A = B
);
endmodule
Parallel Adder/Subtractor
1. Structural Modeling:
module parallel_adder_subtractor_structural (
input [3:0] A,
input [3:0] B,
input subtract, // 0 for add, 1 for subtract
output [4:0] SUM
);
endmodule
genvar i;
generate
for (i = 1; i < 4; i = i + 1) begin : gen_block
assign SUM[i] = A[i] ^ B[i] ^ C_out[i-1];
assign C_out[i] = (A[i] & B[i]) | (A[i] & C_out[i-1]) | (B[i] & C_out[i-1]);
end
endgenerate
endmodule
module parallel_adder_subtractor_dataflow (
input [3:0] A,
input [3:0] B,
input subtract, // 0 for add, 1 for subtract
output [4:0] SUM
);
assign SUM = subtract ? A - B : A + B;
endmodule
3. Behavioral Modeling:
module parallel_adder_subtractor_behavioral (
input [3:0] A,
input [3:0] B,
input subtract, // 0 for add, 1 for subtract
output [4:0] SUM
);
// Behavioral implementation
always @* begin
if (subtract == 1'b1)
SUM = A - B;
else
SUM = A + B;
end
endmodule
1. Structural Modeling:
module magnitude_comparator_structural (
input [3:0] A,
input [3:0] B,
output equal // A = B
);
wire [3:0] A_xor_B;
assign A_xor_B = A ^ B;
endmodule
module magnitude_comparator_dataflow (
input [3:0] A,
input [3:0] B,
output equal // A = B
);
endmodule
3. Behavioral Modeling:
module magnitude_comparator_behavioral (
input [3:0] A,
input [3:0] B,
output equal // A = B
);
// Behavioral implementation
always @* begin
if (A > B) begin
A_greater = 1'b1;
B_greater = 1'b0;
equal = 1'b0;
A_greater = 1'b0;
B_greater = 1'b1;
equal = 1'b0;
A_greater = 1'b0;
B_greater = 1'b0;
equal = 1'b1;
end
end
endmodule
module siso_shift_register (
);
if (reset)
else
end
endmodule
module sipo_shift_register (
);
if (reset)
else
end
endmodule
module piso_shift_register (
);
if (reset)
else
if (counter < 4)
end
endmodule
module pipo_shift_register (
input clk, // Clock input
input reset, // Reset input
input [3:0] parallel_in, // Parallel input
output reg [3:0] parallel_out // Parallel output
);
endmod
Counters
1. 1. Structural Modeling:
module counter_structural (
input clk, // Clock input
input reset, // Reset input
output reg [3:0] count // Counter output
);
wire clk_inv;
endmodule
// D Flip-Flop module
module D_FF (
input clk, // Clock input
input reset, // Reset input
input D, // Data input
output reg Q // Output
);
endmodule
module counter_dataflow (
input clk, // Clock input
input reset, // Reset input
output reg [3:0] count // Counter output
);
endmodule
3. 3. Behavioral Modeling:
module counter_behavioral (
input clk, // Clock input
input reset, // Reset input
output reg [3:0] count // Counter output
);
// Behavioral implementation
always @(posedge clk or posedge reset) begin
if (reset)
count <= 4'b0000; // Reset count to 0
else
count <= count + 1; // Increment count on clock edge
end
endmodule