E23cseu2200 Assignment9
E23cseu2200 Assignment9
TESTBENCH CODE:
module ring_count_tb;
reg CLK, CLR;
wire [3:0] Q;
ring_count a1(Q, CLK, CLR);
initial CLK = 0;
always #1 CLK = ~CLK;
initial begin
CLR = 1'b1;
repeat (2) @(posedge CLK);
CLR = 1'b0;
repeat (10) @(posedge CLK);
$finish;
//ANSHUL
end
initial begin
$dumpfile("dump.vcd");
$dumpvars(1);
end
endmodule
Johnson Counter: A Johnson counter, also known as a twisted ring counter, is a modified
ring counter where the complement output of the last flip-flop is fed back to the input of the
first. It is a counter that counts 2N states if the number of bits is N
Design code:
module johnson(q, clk, clr);
input clk, clr;
//ANSHUL
output reg [3:0] q;
always @(posedge clk)
if (clr == 1) begin
q = 4'b0000;
end
else begin
q[3] <= ~q[0];
q[2] <= q[3];
q[1] <= q[2];
q[0] <= q[1];
end
endmodule
TESTBENCH CODE:
module johnson_tb;
reg CLK, CLR;
wire [3:0] Q;
johnson a1(Q, CLK, CLR);
initial CLK = 0;
always #1 CLK = ~CLK;
initial begin
CLR = 1'b1;
repeat (2) @(posedge CLK);
CLR = 1'b0;
repeat (10) @(posedge CLK);
$finish;
end
initial begin
$dumpfile("dump.vcd");
$dumpvars(1);
end
endmodule
…………………………………………………………………………………………………..
3. What are Shift registers. What are different types of Shift-registers. Discuss about the
various applications of Registers.
Shift Registers: In digital electronics, a shift register is a cascade of flip-flops where the
output pin of one flip-flop is connected to the data input pin of the next. Because all flip-flops
work on the same clock, the bit array stored in the shift register will shift by one position.
Serial In Serial Out (SISO): In this type, data is input serially and taken out serially.
Serial In Parallel Out (SIPO): Here, data is input serially but taken out in parallel.
Parallel In Parallel Out (PIPO): In this type, data is both input and output in parallel.
Parallel In Serial Out (PISO): Here, data is input in parallel but taken out serially.
Bidirectional Shift Registers: These registers can shift data either to the right or left based on
the selected mode.
Applications of Registers:
1. Data Storage: Registers provide temporary storage for data that is being manipulated by a
digital circuit.
2. Data Transfer: Shift registers are often used to convert data from parallel format (used
internally in a microprocessor or other ASIC) to serial format (used for communication
between components on a PCB or between two separate PCBs).
3. Data Manipulation: Shift registers can be used to perform simple data manipulations. For
example, shifting a binary number left by one position multiplies the number by 2, while
shifting it right by one position divides the number by 23.
4. Timing and Control: Registers can be used to introduce deliberate time delays into a
digital circuit.
5. Counting and Division: Shift registers can be used to implement counters and frequency
dividers.
………………………………………………………………………………………………..
4. Write Verilog code to implement 2 bit ripple up/down counter.
Design code:
module up_counter(
input Clk,
input reset,
output reg [1:0] counter
);
always @(posedge Clk or posedge reset) begin
if (reset)
counter <= 2'b00;
else
counter <= counter + 1;
end
endmodule
TESTBENCH CODE:
module tb_counter;
reg CLK, CLR;
wire [1:0] Q;
up_counter uut (.Clk(CLK), .reset(CLR), .counter(Q));
initial CLK = 0;
always #5 CLK = ~CLK;
initial begin
CLR = 1'b1;
repeat (2) @(posedge CLK);
CLR = 1'b0;
repeat (10) @(posedge CLK);
$finish;
end
initial begin
$dumpfile("dump.vcd");
$dumpvars(1);
end
endmodule
************************************************************************