Verilog Assignment 1
Verilog Assignment 1
Soln. -
A.
module basic_gates_n_input(
input [n-1:0] A, // n-bit input
output Y_and, // AND output
output Y_or, // OR output
output Y_nand, // NAND output
output Y_nor, // NOR output
output Y_xor, // XOR output
output Y_xnor // XNOR output
);
// AND Gate with n inputs
assign Y_and = &A; // AND operation on all bits
endmodule
B.
module full_adder_1bit(
input A, B, Cin, // 1-bit inputs
output Sum, Cout // Sum and Carry output
);
assign Sum = A ^ B ^ Cin; // Sum = A XOR B XOR Cin
assign Cout = (A & B) | (B & Cin) | (A & Cin); // Carry = (A AND B) OR (B AND Cin) OR (A
AND Cin)
endmodule
module full_adder_4bit(
input [3:0] A, B, // 4-bit inputs
input Cin, // Carry input
output [3:0] Sum, // 4-bit Sum output
output Cout // Carry output
);
wire c1, c2, c3; // Intermediate carry bits
full_adder_1bit FA1 (
.A(A[1]), .B(B[1]), .Cin(c1), .Sum(Sum[1]), .Cout(c2)
);
full_adder_1bit FA2 (
.A(A[2]), .B(B[2]), .Cin(c2), .Sum(Sum[2]), .Cout(c3)
);
full_adder_1bit FA3 (
.A(A[3]), .B(B[3]), .Cin(c3), .Sum(Sum[3]), .Cout(Cout)
);
endmodule
C.
module subtractor_4bit(
input [3:0] A, B, // 4-bit inputs A and B
output [3:0] Diff, // 4-bit difference (A - B)
output Borrow // Borrow output
);
wire [3:0] B_complement; // 1's complement of B
wire carry_in; // Carry-in for the addition
endmodule
D.
module cla_4bit_adder(
input [3:0] A, B, // 4-bit inputs A and B
input Cin, // Carry input
output [3:0] Sum, // 4-bit Sum output
output Cout // Carry output
);
wire [3:0] G, P; // Generate and Propagate signals
wire [4:0] C; // Carry signals
endmodule
E.
module parity_generator(
input [3:0] data, // 4-bit data input
output parity_bit // Parity bit output
);
assign parity_bit = data[0] ^ data[1] ^ data[2] ^ data[3]; // XOR all bits to generate parity
endmodule
(odd)
module odd_parity_generator(
input [3:0] data, // 4-bit data input
output parity_bit // Parity bit output
);
assign parity_bit = ~(data[0] ^ data[1] ^ data[2] ^ data[3]); // Invert XOR result for odd
parity
endmodule
module parity_checker(
input [3:0] data, // 4-bit data input
input parity_bit, // Parity bit input
output parity_ok // Output: 1 if parity is correct, 0 if error detected
);
wire parity_check;
assign parity_check = data[0] ^ data[1] ^ data[2] ^ data[3] ^ parity_bit; // XOR all bits
including parity bit
(odd)
module odd_parity_checker(
input [3:0] data, // 4-bit data input
input parity_bit, // Parity bit input
output parity_ok // Output: 1 if parity is correct, 0 if error detected
);
wire parity_check;
assign parity_check = data[0] ^ data[1] ^ data[2] ^ data[3] ^ parity_bit; // XOR all bits
including parity bit
assign parity_ok = (parity_check == 1); // For odd parity, the result should be 1 for correct
parity
endmodule
F.
module magnitude_comparator(
input [3:0] A, // 4-bit input A
input [3:0] B, // 4-bit input B
output A_eq_B, // Output: 1 if A == B
output A_gt_B, // Output: 1 if A > B
output A_lt_B // Output: 1 if A < B
);
// Check if A is equal to B
assign A_eq_B = (A == B);
endmodule
Q2.
Write the code of MSI logic like ( choose appropriate programming process)
A. 4 bit , 8 bit , 16 bit MUX
B. 4 bit , 8 bit , 16 bit Demux
C. 16 bit MUX using MUX tree
D. 64 bit MUX using MUX tree
E. Full adder using MUX
F. 3-8 Decoder
G. BCD encoder
H. BCD to Excess three converter using Decoder
I. BCD to Grey code converter using Decoder
J. Priority Encoder
Soln-
A.
4 bit Mux
module mux4to1 (
input [3:0] data_in, // 4-bit input data
input [1:0] select, // 2-bit select line
output reg out // MUX output
);
always @ (data_in or select) begin
case(select)
2'b00: out = data_in[0]; // Select input 0
2'b01: out = data_in[1]; // Select input 1
2'b10: out = data_in[2]; // Select input 2
2'b11: out = data_in[3]; // Select input 3
default: out = 0;
endcase
end
endmodule
8 bit mux
module mux4to1 (
input [3:0] data_in [3:0], // 4 inputs, each 4-bit wide
input [1:0] select, // 2-bit select line
output reg [3:0] out // 4-bit output
);
endmodule
module mux8to1_using_4bit_mux (
input [7:0] data_in [7:0], // 8 inputs, each 8-bit wide
input [2:0] select, // 3-bit select line
output [7:0] out // 8-bit output
);
endmodule
16 bit mux
module mux4to1 (
input [3:0] data_in [3:0], // 4 inputs, each 4-bit wide
input [1:0] select, // 2-bit select line
output reg [3:0] out // 4-bit output
);
endmodule
module mux16to1_using_4bit_mux (
input [15:0] data_in [15:0], // 16 inputs, each 16-bit wide
input [3:0] select, // 4-bit select line
output [15:0] out // 16-bit output
);
endmodule
B.
4 bit dmux
module demux_4bit(
input [3:0] data, // 4-bit data input
input [3:0] sel, // 4-bit select signal
output reg [3:0] out0, out1, out2, out3, out4, out5, out6, out7,
out8, out9, out10, out11, out12, out13, out14, out15 // 16 outputs
);
endmodule
8 bit demux
module demux_8bit(
input [7:0] data, // 8-bit data input
input [3:0] sel, // 4-bit select signal
output [3:0] out0, out1, out2, out3, out4, out5, out6, out7,
out8, out9, out10, out11, out12, out13, out14, out15 // 16 outputs
);
// Use two 4-bit demuxes for the upper and lower 4 bits
demux_4bit demux_lower (
.data(data0),
.sel(sel),
.out0(out0), .out1(out1), .out2(out2), .out3(out3),
.out4(out4), .out5(out5), .out6(out6), .out7(out7),
.out8(out8), .out9(out9), .out10(out10), .out11(out11),
.out12(out12), .out13(out13), .out14(out14), .out15(out15)
);
demux_4bit demux_upper (
.data(data1),
.sel(sel),
.out0(out0), .out1(out1), .out2(out2), .out3(out3),
.out4(out4), .out5(out5), .out6(out6), .out7(out7),
.out8(out8), .out9(out9), .out10(out10), .out11(out11),
.out12(out12), .out13(out13), .out14(out14), .out15(out15)
);
endmodule
16 bit demux
module demux_16bit(
input [15:0] data, // 16-bit data input
input [3:0] sel, // 4-bit select signal
output [3:0] out0, out1, out2, out3, out4, out5, out6, out7,
out8, out9, out10, out11, out12, out13, out14, out15 // 16 outputs
);
// Use two 8-bit demuxes for the lower and upper 8 bits
demux_8bit demux_lower (
.data(data0),
.sel(sel),
.out0(out0), .out1(out1), .out2(out2), .out3(out3),
.out4(out4), .out5(out5), .out6(out6), .out7(out7),
.out8(out8), .out9(out9), .out10(out10), .out11(out11),
.out12(out12), .out13(out13), .out14(out14), .out15(out15)
);
demux_8bit demux_upper (
.data(data1),
.sel(sel),
.out0(out0), .out1(out1), .out2(out2), .out3(out3),
.out4(out4), .out5(out5), .out6(out6), .out7(out7),
.out8(out8), .out9(out9), .out10(out10), .out11(out11),
.out12(out12), .out13(out13), .out14(out14), .out15(out15)
);
endmodule
C.
module mux_16bit (
input [15:0] data, // 16-bit input data
input [3:0] sel, // 4-bit select signal
output reg [15:0] out // 16-bit output
);
endmodule
endmodule
D.
module mux_64bit (
input [63:0] data, // 64-bit input data
input [5:0] sel, // 6-bit select signal
output reg [63:0] out // 64-bit output
);
endmodule
endmodule
E.
module full_adder_mux (
input A, // Input A
input B, // Input B
input Cin, // Carry-in
output Sum, // Sum output
output Cout // Carry-out output
);
endmodule
endmodule
F.
module decoder3to8 (
input [2:0] A, // 3-bit input
output reg [7:0] Y // 8-bit output
);
endmodule
G.
module bcd_encoder (
input [9:0] bcd_in, // 10-bit input for BCD digits (from 0 to 9)
output reg [3:0] bcd_out // 4-bit output BCD representation
);
H.
module bcd_to_excess3_using_decoder (
input [3:0] bcd, // 4-bit BCD input (0 to 9)
output reg [3:0] excess3 // 4-bit Excess-3 output
);
// 4-to-10 Decoder logic for BCD inputs
always @(*) begin
case (bcd)
4'b0000: excess3 = 4'b0011; // BCD 0 -> Excess-3 3
4'b0001: excess3 = 4'b0100; // BCD 1 -> Excess-3 4
4'b0010: excess3 = 4'b0101; // BCD 2 -> Excess-3 5
4'b0011: excess3 = 4'b0110; // BCD 3 -> Excess-3 6
4'b0100: excess3 = 4'b0111; // BCD 4 -> Excess-3 7
4'b0101: excess3 = 4'b1000; // BCD 5 -> Excess-3 8
4'b0110: excess3 = 4'b1001; // BCD 6 -> Excess-3 9
4'b0111: excess3 = 4'b1010; // BCD 7 -> Excess-3 10
4'b1000: excess3 = 4'b1011; // BCD 8 -> Excess-3 11
4'b1001: excess3 = 4'b1100; // BCD 9 -> Excess-3 12
default: excess3 = 4'b0000; // Default case (for invalid BCD)
endcase
end
endmodule
I.
module bcd_to_gray_using_decoder (
input [3:0] bcd, // 4-bit BCD input (0 to 9)
output reg [3:0] gray // 4-bit Gray code output
);
// 4-to-10 Decoder logic for BCD inputs to Gray code
always @(*) begin
case (bcd)
4'b0000: gray = 4'b0000; // BCD 0 -> Gray Code 0000
4'b0001: gray = 4'b0001; // BCD 1 -> Gray Code 0001
4'b0010: gray = 4'b0011; // BCD 2 -> Gray Code 0011
4'b0011: gray = 4'b0010; // BCD 3 -> Gray Code 0010
4'b0100: gray = 4'b0110; // BCD 4 -> Gray Code 0110
4'b0101: gray = 4'b0111; // BCD 5 -> Gray Code 0111
4'b0110: gray = 4'b0101; // BCD 6 -> Gray Code 0101
4'b0111: gray = 4'b0100; // BCD 7 -> Gray Code 0100
4'b1000: gray = 4'b1100; // BCD 8 -> Gray Code 1100
4'b1001: gray = 4'b1101; // BCD 9 -> Gray Code 1101
default: gray = 4'b0000; // Default case (invalid BCD)
endcase
end
endmodule
J.
module priority_encoder (
input [3:0] in, // 4-bit input (I0 to I3)
output reg [1:0] out, // 2-bit output (Y1, Y0)
output reg valid // Valid signal to indicate any input is active
);
Q3.
Write the code for following sequential logic
A. J-K , D and T flipflop
B. Pulse generator
C. 3 bit up down ripple counter
D. 3 bit ring counter
E. 3 bit Shift right-left register ( choose clock as per req)
F. Mod 2, Mod 5, Mod 10 counter
G. 4-bit Unsigned Up Counter with Asynchronous Clear
Soln-
A.
J-K FF
module jk_flip_flop (
input J, // J input
input K, // K input
input CLK, // Clock input
input RESET, // Asynchronous Reset
output reg Q // Output Q
);
endmodule
D FF
module d_flip_flop (
input D, // Data input
input CLK, // Clock input
input RESET, // Asynchronous reset
output reg Q // Output Q
);
endmodule
T FF
module t_flip_flop (
input T, // Toggle input
input CLK, // Clock input
input RESET, // Asynchronous reset
output reg Q // Output Q
);
endmodule
B.
module pulse_generator (
input CLK, // Input clock
input RESET, // Asynchronous reset
output reg PULSE // Output pulse signal
);
endmodule
C.
module up_down_ripple_counter (
input CLK, // Clock input
input RESET, // Asynchronous reset
input UP_DOWN, // Up/Down control (1 for up, 0 for down)
output reg [2:0] Q // 3-bit output (Q2, Q1, Q0)
);
endmodule
D.
module ring_counter (
input CLK, // Clock input
input RESET, // Asynchronous reset
output reg [2:0] Q // 3-bit output (Q2, Q1, Q0)
);
endmodule
E.
module shift_register (
input CLK, // Clock input
input RESET, // Asynchronous reset
input SHIFT_LEFT, // Shift left control (1 for shift left)
input SHIFT_RIGHT, // Shift right control (1 for shift right)
input [2:0] D, // 3-bit input data to load
output reg [2:0] Q // 3-bit output register value
);
endmodule
F.
MOD 2
module mod2_counter (
input CLK, // Clock input
input RESET, // Asynchronous reset
output reg [0:0] Q // 1-bit output (Q0)
);
MOD 5
module mod5_counter (
input CLK, // Clock input
input RESET, // Asynchronous reset
output reg [2:0] Q // 3-bit output (Q2, Q1, Q0)
);
MOD 10
module mod10_counter (
input CLK, // Clock input
input RESET, // Asynchronous reset
output reg [3:0] Q // 4-bit output (Q3, Q2, Q1, Q0)
);
G.
module up_counter (
input wire clk, // Clock signal
input wire clear, // Asynchronous clear signal
output reg [3:0] count // 4-bit counter output
);
endmodule
Q4.
Write code for 16 word 8 bit RAM
Soln-
module ram_16x8 (
input wire clk, // Clock signal
input wire we, // Write enable signal
input wire [3:0] addr, // 4-bit address (16 words = 2^4 address bits)
input wire [7:0] din, // 8-bit data input (to be written into RAM)
output reg [7:0] dout // 8-bit data output (data read from RAM)
);
endmodule
Soln-
module mealy_machine (
input wire clk, // Clock signal
input wire reset, // Asynchronous reset
input wire x, // Input signal
output reg y // Output signal
);
endmodule
Soln-
module moore_machine (
input wire clk, // Clock signal
input wire reset, // Asynchronous reset
input wire x, // Input signal
output reg y // Output signal
);
// Output logic (Moore machine: output depends only on the current state)
always @(current_state) begin
case (current_state)
S0: y = 0; // Output 0 in state S0
S1: y = 1; // Output 1 in state S1
default: y = 0;
endcase
end
endmodule
Soln-
A.
module tb_mealy_machine;
reg clk;
reg reset;
reg x;
wire y;
// Test Sequence
initial begin
// Initialize signals
clk = 0;
reset = 1;
x = 0;
endmodule
B.
module tb_moore_machine;
reg clk;
reg reset;
reg x;
wire y;
// Clock generation
always #5 clk = ~clk;
// Test Sequence
initial begin
// Initialize signals
clk = 0;
reset = 1;
x = 0;
endmodule
C.
module tb_mux_tree;
// Test Sequence
initial begin
// Initialize signals
data_in = 16'b1010101010101010;
select = 4'b0000;
endmodule
D.
module tb_ring_counter;
reg clk;
reg reset;
wire [3:0] out;
// Clock generation
always #5 clk = ~clk;
// Test Sequence
initial begin
// Initialize signals
clk = 0;
reset = 1;
// Apply reset
#10 reset = 0;
endmodule
E.
module tb_parallel_adder;
reg [3:0] A, B;
reg Cin;
wire [3:0] Sum;
wire Cout;
// Test Sequence
initial begin
// Initialize signals
A = 4'b0000;
B = 4'b0000;
Cin = 0;
endmodule
F.
module tb_nbit_xor;
// Test Sequence
initial begin
// Initialize signals
A = 8'b00000000;
B = 8'b00000000;
endmodule
Q8.
Write a code for Dual-Port RAM with Asynchronous Read
Soln-
module dual_port_ram (
input wire clk, // Clock signal (only for write operations)
input wire we, // Write enable signal (active high)
input wire [3:0] addr_write, // 4-bit write address (16 locations)
input wire [3:0] addr_read, // 4-bit read address (16 locations)
input wire [7:0] data_in, // 8-bit data input (for writing)
output reg [7:0] data_out // 8-bit data output (for reading)
);
endmodule