Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

100daysof RTL: Part-1 (Verilog)

Download as pdf or txt
Download as pdf or txt
You are on page 1of 54

100daysof

RTL
Part-1(Verilog)

Ummidi Chandrika
WELCOME TO MY 100DAYSOFRTL
HERE IS THE LIST OF DAY WISE RTL CODES:
DAY 1 : CLOCK DIVIDER
DAY 2 : JOHNSON COUNTER
DAY 3: RING COUNTER
DAY 4: 5 INPUT MAJORITY CIRCUIT
DAY 5: PARITY GENERATOR
DAY 6: BINARY TO ONE HOT ENCODER
DAY 7: 4-BIT BCD SYNCHRONOUS COUNTER
DAY 8: 4-BIT CARRY LOOKAHEAD ADDER
DAY 9: N-BIT COMPARATOR
DAY 10: SERIAL IN SERIAL OUT SHIFT REGISTER
DAY 11: SERIAL IN PARALLEL OUT SHIFT REGISTER
DAY 12: PARALLEL IN PARALLEL OUT REGISTER
DAY 13: PARALLEL IN SERIAL OUT REGISTER
DAY 14: BIDIRECTION SHIFT REGISTER
DAY 15: PRBS SEQUENCE GENERATOR
DAY 16: 8-BIT SUBTRACTOR
DAY 17: 8-BIT ADDER/SUBTRACTOR
DAY 18: 4-BIT MULTIPLIER
DAY 19: FIXED POINT DIVISION
DAY 20: MASTER SLAVE JK FLIP FLOP
DAY 21: POSITIVE EDGE DETECTOR
DAY 22: BCD ADDER
DAY 23: 4-BIT CARRY SELECT ADDER
DAY 24: MOORE FSM 1010 SEQUENCE DETECTOR
DAY 25: N:1 MUX
DAY 26: BCD TIMECOUNT
DAY 27: 3-1 MUX
DAY 28: BCD TO SEVEN SEGMENT DISPLAY
DAY 29: D LATCH USING 2:1 MUX
DAY 30: 8-BIT BARREL SHIFTER
DAY 31: 1-BIT COMPARATOR USING 4X1 MUX
DAY 32: LOGICAL, ALGEBRAIC, AND ROTATE
SHIFT OPERATIONS
DAY 33: ALU
DAY 34: 4-BIT ASYNCHRONOUS DOWN
COUNTER
DAY 35: MOD-N UPDOWN COUNTER
DAY 36: UNIVERSAL BINARY COUNTER
DAY 37: UNIVERSAL SHIFT REGISTER
DAY 38: CN( CHANGE-NO CHANGE FLIPFLOP)
USING 2:1 MUX
DAY 39: FREQUENCY DIVIDER BY ODD
NUMBERS
DAY 40: GREATEST COMMON DIVISOR USING
BEHAVIOURAL MODELLING
DAY 41: GREATEST COMMON DIVISOR VIA FSM
DAY 42: SINGLE PORT RAM
DAY 43: DUAL PORT RAM
DAY 44: CLOCK BUFFER
DAY 45: SYNCHRONOUS FIFO
DAY 46: PRIORITY ENCODER
DAY 47: SEVEN SEGMENT DISPLAY USING ROM
DAY 48: SERIAL ADDER
DAY 49: FIXED PRIORITY ARBITER
DAY 50: ROUND ROBIN ARBITER
Ummidi Chandrika

100 Days of RTL

Part-1 (Verilog)

Day-1: Clock Divider


A clock divider circuit creates lower frequency clock signals from an input clock source.
The divider circuit counts input clock cycles, and drives the output clock low and then
high for some number of input clock cycles. where fout = fin / n and ''n'' is an integer.
Frequency dividers are used for both analog and digital applications.
Approach:
The code is simple to understand. All you need to do is to set the output clock to 0 at the
time of reset(0). In this example the reset was required to be synchronous. The code is
simple as all we need to do is invert the output clock at each of its rising edge.
Take a temporary 4-bit register as count[3:0].The count is made to increment by 1 for
every positive edge of the clock. Then it will acts as a counter from 0-15
The count[0] acts as a Divideby2 circuit,
count[1] acts as a Divideby4 circuit,
count[2] acts as a Divideby8 circuit,
count[3] acts as a Divideby16 circuit
RTL Code:

1
Ummidi Chandrika

Day-2 Johnson Counter


A Johnson counter is a digital circuit with a series of flip flops connected in a feedback
manner.
The circuit is a special type of shift register where the last flip flop's complement output
is fed back to the first flip flop's input. This is almost similar to the ring counter with a few
extra advantages.
The Johnson counter's main advantage is that it only needs half the number of flip-
flops compared to the standard ring counter, and then it's modulo number is halved. So
an n-stage Johnson counter will circulate a single data bit, giving a sequence of 2n
different states and can therefore be considered a mod-2n counter.
RTL Code:

Day-3 Ring Counter


A ring counter is a special type of application of the Serial IN Serial OUT Shift register.
The only difference between the shift register and the ring counter is that the last flip
flop outcome is taken as the output in the shift register. But in the ring counter, this
outcome is passed to the first flip flop as an input. All of the remaining things in the ring
counter are the same as the shift register.In the Ring counter, No. of states in Ring
counter = No. of flip-flop used.

2
Ummidi Chandrika

RTL Code:

Day-4 : 5 Input Majority Circuit


The design of a majority circuit using built-in primitives. The output of a majority circuit
is a logic 1 if the majority of the inputs is a logic 1; otherwise, the output is a logic 0.
Therefore, a majority circuit must have an odd number of inputs in order to have a
majority of the inputs at the same indicates that the majority of the inputs is a logic 1.

3
Ummidi Chandrika

RTL Code:

Day-5 Parity Generator


A Parity Generator designed is Odd Parity Generator circuit, it maintains the binary data
in an odd number of 1’s, for example, the data taken is in even number of 1’s, this odd
parity generator is going to maintain the data as an odd number of 1’s by adding the
extra 1 to the even number of 1’s. This is the combinational circuit whose output is
always dependent upon the given input data.

4
Ummidi Chandrika

RTL Code:

Day 6- Binary to One Hot Encoder


Binary To One-Hot Converter generates an output bit vector of up to 2^N bits with one
bit set representing the N-bit input binary value. One-hot encoding is particularly
advantageous for FPGA implementations. The width of the output vector is limited by
the Verilog implementation to about a million bits, which means a maximum input
binary width of 20 bits.
RTL Code:

5
Ummidi Chandrika

Day 7- 4-Bit Synchronous BCD Counter:


A BCD counter is one of the 4-bit binary counters, which counts from 0 to a pre-

determined count with an applied clock signal. When the count reaches the

predetermined count value, it resets all the flip-flops and starts to count again from 0.

This type of counter is designed by using 4 JK flip flops and counts from 0 to 9, and the

result is represented in digital form. After reaching the count of 9 (1001), it resets and

starts again.
RTL Code:

Day 8 : 4-Bit Carry Look Ahead Adder:


A carry look-ahead adder reduces the propagation delay by introducing more complex
hardware. In this design, the ripple carry design is suitably transformed such that the

6
Ummidi Chandrika

carry logic over fixed groups of bits of the adder is reduced to two-level logic.

Day 9: N-bit Comparator:


A magnitude digital Comparator is a combinational circuit that compares two digital or
binary numbers in order to find out whether one binary number is equal, less than, or
greater than the other binary number.
RTL Code:

7
Ummidi Chandrika

Day 10: Serial In Serial Out Shift Register


SISO shift register circuit accepts serial data on its input pin and shifts it out serially on
its output pin. The number of bits that can be shifted out before the next bit arrives
depends on the speed of the clock signal that controls the operation of the shift register.
This type of shift register can be used as a buffer between two asynchronous devices
that communicate with each other using signals with different frequencies or phases.
RTL Code:

Day 11- Serial in parallel Out Shift Register


A serial-in, parallel-out shift register is similar to the serial-in, serial-out shift register in
that it shifts data into internal storage elements and shifts data out at the serial-out,
data-out, pin.
It is different in that it makes all the internal stages available as outputs. Therefore, a
serial-in, parallel-out shift register converts data from serial format to parallel format.If
four data bits are shifted in by four clock pulses via a single wire at data-in, below, the
data becomes available simultaneously on the four Outputs QA to QD after the fourth
clock pulse.
• For 'n' bit serial input data which need to be stored, the number of clock pulse
required are equal to 'n'.
• For 'n' bit parallel output data which need to be stored, the number of clock pulse
required is zero. As no clock pulse are required for this operation.

8
Ummidi Chandrika

RTL Code:

Day 12- Parallel In Parallel Out Shift Register


In "Parallel IN Parallel OUT", the inputs and the outputs come in a parallel way in the
register. The inputs A0, A1, A2, and A3, are directly passed to the data inputs D0, D1, D2,
and D3 of the respective flip flop. The bits of the binary input is loaded to the flip flops
when the negative clock edge is applied. The clock pulse is required for loading all the
bits. At the output side, the loaded bits appear.
• For parallel In data, Number of clock pulse needed are equal to 1.
• For parallel Out data, Number of Clock pulse needed are equal to 0.
RTL Code:

9
Ummidi Chandrika

Day-13: Parallel In Serial Out Shift Register


A PISO shift register is a digital circuit that can accept parallel data and output serial

data. It is made up of a succession of flip-flops, with each flip-flop capable of storing

one bit of data. Unlike PIPO shift registers, which offer parallel input and output, a

PISO shift register accepts data in parallel and outputs it sequentially, or serially. It is

parallel data storage register. To store 'n' bit; number of clock pulse required is equal to
1.
• To provide serial data as output, number of clock pulse needed are equal to '(n-1)'.

Day- 14 : Bidirectional Shift Register


Bidirectional shift registers are the storage devices capable of shifting the data either

right or left, depending on the mode selected.Dir control line is made either low or high

to opt for either left-shift or right-shift of the data bits, respectively.

10
Ummidi Chandrika

RTL Code:

Day 15 : Psuedo Bit Random Sequence Generator


A PRBS is a periodic, deterministic signal consisting of a series of digital ones and
zeros.A pseudo-random bit sequence (PRBS) is a pattern test that appears to be random,
but is actually a predictable and repeatable sequence with a very long interval (i.e.
billions of repeating bits), depending upon the pattern. A PRBS bit stream can be
generated by using a linear feedback shift register (LFSR).

11
Ummidi Chandrika

RTL Code:

Day 16: 8-Bit Subtractor

The advantage of 2’s complement over 1’s complement is that if any carry remains after
addition then there is no any need to add that carry in the end results.whereas in the
case of 1’s comement whenever carry remains after addition then it means that the
result is positive and the final result will be obtained by adding 1 in the result.

12
Ummidi Chandrika

RTL Code:

Day 17: 8-Bit Adder Subtractor


When the mode input (M) is at a low logic, i.e. '0', the circuit act as an adder and when
the mode input is at a high logic, i.e. '1', the circuit act as a subtractor.

13
Ummidi Chandrika

Day -18 : 4-Bit Multiplier

Day 19: Fixed Point Restoring Division


Restoring division examines the state of the carry-out when the dividend is subtracted

from the partial remainder. This determines the relative magnitudes of the divisor and
partial remainder. If the carry-out = 0, then the partial remainder is restored to its

previous value by adding the divisor to the partial remainder. If the carry-out = 1, then

there is no restore operation. The partial remainder (high-order half of the dividend)

and the low-order half of the dividend are then shifted left one bit position and the

process repeats for each bit in the divisor.

14
Ummidi Chandrika

RTL Code:

15
Ummidi Chandrika

Day- 20: Master Slave JK Flip Flop


Master slave configuration removes the possibility of race around condition from JK flip
flop when we put both J and K as 1.
In normal JK flip flop when we put both J and K as 1 and as clock level becomes high the
output changes continuously from 0 to 1 & 1 to 0 till the clock is high.However in master
slave configuration when we put both J & K as 1 the present output will be compliment
of past output i.e the race around is changes to toggling. The output in master slave
changes when negative edge of clock reaches i.e negative triggers at negative edge which
also removes problem due to propagation delay. This toggling effect helps in
designing counters.
RTL Code:

Day 21- Positive Edge Detector


A positive edge Detector will send out a pulse. whenever the signal it is monitoring

changes from 0 to 1. The idea behind a Positive Edge Detector is to delay original clock

signal by one clock cycle, take its inverse and perform a logical AND with the original

signal.

16
Ummidi Chandrika

Day 22- BCD Adder

The BCD-Adder is used in the computers and the calculators that perform arithmetic
operation directly in the decimal number system. The BCD-Adder accepts the binary-
coded form of decimal numbers. The output will vary from 0 to 18 if we are not
considering the carry from the previous sum. But if we are considering the carry, then
the maximum value of output will be 19 (i.e. 9+9+1 = 19). When we are simply adding

17
Ummidi Chandrika

A and B, then we get the binary sum. Here, to get the output in BCD form, we will use
BCD Adder.

RTL Code:

18
Ummidi Chandrika

Day 23- 4-Bit Carry Select Adder


A carry select adder is an arithmetic combinational logic circuit which adds two N-bit

binary numbers and outputs their N-bit binary sum and a 1-bit carry.The idea behind a

N-bit carry select adder is to avoid propagating the the carry from bit to bit in sequence.

If we have two adders in parallel: one with a carry input of 0, the other with a carry

input of 1, then we could use the actual carry input generated to select between the

outputs of the two parallel adders. This means all adders could be performing their

calculations in parallel. Having two adders for each result bit is quite wasteful so we

could configure the N-bit adder to use 2*N/M-1 M-bit ripple carry adders in parallel.

Note that the adder for the least significant bits will always have a carry input of 0 so no

parallel addition is needed in this case.


RTL Code:

19
Ummidi Chandrika

Day 24-Moore FSM which detects 1010 Sequence


Moore machine is a finite-state machine whose current output values are determined
only by its current state. This is in contrast to a Mealy machine, whose output values are
determined both by its current state and by the values of its inputs. Like other finite
state machines, in Moore machines, the input typically influences the next state.
Why Moore is better than Mealy?
A Mealy Machine changes its output on the basis of its present state and current input. A
Moore Machine's output depends only on the current state. It does not depend on the
current input.Mealy machines react faster to inputs. They generally react in the same
clock cycle.

20
Ummidi Chandrika

21
Ummidi Chandrika

Day 25- N:1 Multiplexer


Multiplexers are also known as “Data n selector, parallel to serial convertor, many to one
circuit, universal logic circuit”. Multiplexers are mainly used to increase amount of the

data that can be sent over the network within certain amount of time and bandwidth.
RTL Code:

Day 26: Write RTL code for a BCD counter that displays time in a 24hr
format as shown here HH:MM:SS with the following specification
details:
I. Input Clock Frequency is 1Hz
II. Inputs: clock (posedge ), reset(Active high synchronous)
III. Outputs: ms_hr<3:0>, ls_hr<3:0>, ms_min<3:0>, ls_min<3:0>,
ms_sec<3:0>, ls_sec<3:0>

22
Ummidi Chandrika

Day 27 : 3-to-1 1-bit MUX with a 1-bit latch


A 3:1 mux has 2 select lines and 3 inputs. As a mux with 2 select lines can represent at
max 4 inputs, a 3:1 mux repeats some inputs for 2 combinations.
RTL Code:

23
Ummidi Chandrika

Day 28: BCD to Seven Segment Display


A Seven-Segment Display is an indicator commonly used by FPGA designers to show

information to the user. Code to convert from binary to seven-segment display

compatible can be done easily in Verilog. There are many applications that can require

the use of one or more seven-segment displays such as:


-->Alarm Clock
-->Stop Watch
-->Button Count Indicator
-->Voltage Measurements (from Analog to Digital Converter)
RTL Code:

24
Ummidi Chandrika

Day 29: D Latch using 2:1 Mux


To build a positive level sensitive latch from a multiplexer, short the output with IN0 pin
of the multiplexer and connect data input to IN1 and Clock input to SEL pin of
multiplexer. A negative level latch can also be built similarly.
RTL Code:

25
Ummidi Chandrika

Day 30: 8-Bit Barrel Shifter


A barrel shifter is a logic circuit for shifting a word by a varying amount. Its has a control

input that specifies the number of bit positions that it shifts by. A barrel shifter is

implemented with a sequence of shift multiplexers, each shifting a word by 2k bit

positions for different values of k .A barrel shifter is able to complete the shift in a single

clock cycle, giving it a great advantage over a simple shifter which can shift n bits in n

clock cycles. It is used in conjunction with a processor's arithmetic logic unit (ALU) or

otherwise embedded in the ALU itself.


RTL Code:

26
Ummidi Chandrika

Day 31: 1-Bit Comparator using 4X1 Mux


IMPLEMENTATION:

27
Ummidi Chandrika

RTL Code:

Day 32- Logical, Algebraic, and Rotate Shift Operations


Shift registers that perform the operations of shift left logical (SLL), shift left algebraic
(SLA), shift right logical (SRL), shift right algebraic (SRA), rotate left (ROL), and rotate
right (ROR) will be Coded.

28
Ummidi Chandrika

RTL Code:

Day 33: ALU


Arithmetic and logic units (ALUs) perform the arithmetic operations and logical
operations. Here Opcode decides the type of operation to be performed on the
operands.If the opocde is
0-Addition

29
Ummidi Chandrika

1- Subtraction

2-Multiplication

3-And operation

4-Or operation

5-Not operation

6-Xor operation

7-Xnor operation
RTL Code:

30
Ummidi Chandrika

Day-34: 4-Bit Asynchronous Down Counter


In the asynchronous counter, an external clock pulse is provided for only the first flip
flop, thereafter the output of the 1st FF acts as a clock pulse for the second FF and so on.
In the case of synchronous FFs, all the flip flops are triggered simultaneously by an
external clock pulse.
RTL code:

Day 35 : Mod-N UpDown Counter


A bidirectional counter is a synchronous up/down binary counter that has the ability to
count in both directions either to or from some preset value as well as zero.
As well as counting “up” from zero and increasing or incrementing to some preset value,
it is sometimes necessary to count “down” from a predetermined value to zero allowing
us to produce an output that activates when the zero count or some other pre-set value

31
Ummidi Chandrika

is reached.The upordown signal is considered and if it is 0, then it will act as down


counter and if upordown is 1 it will act as up counter.
RTL Code:

Day 36: Universal Binary Counter


A universal binary counter is more versatile. It can count up or down, pause, be loaded

32
Ummidi Chandrika

with a specific value, or be synchronously cleared. Its functions are summarized in


below Table

RTL Code:

33
Ummidi Chandrika

Day 37: Universal Shift Register


A Universal Shift Register is a register which can shift its data in both direction i.e. left
and right directions. In other Words , a universal shift register is a bidirectional shift
register .It is combination of design of bidirectional shift register and a unidirectional
shift register with the parallel load provisions. It can perform parallel to serial operation
( first loading parallel input and then shifting.It can also perform serial to parallel
operation ( first shifting and then retrieving parallel output .The desired operation is
then specified by a 2 bit control signal as shown in the Below table.

RTL Code:

34
Ummidi Chandrika

Day 38: CN- Flipflop (Change -No Change Flip Flop) using DFF and 2:1 Mux
In C-N (Change – No change) flip-flop, there won’t be any change in output as long as
N is 0, irrespective of C. If N=1, then if C=0 output will change to 0 else if C=1 output

will be the compliment of previous output. Design C-N flip-flop using D flip flop and

minimum number of 2 x 1 multiplexer. The characteristic table and design of the above

flip-flop is shown below.

35
Ummidi Chandrika

RTL Code:

Day 39: Frequency Divider by any Odd Number (Here I Used N=5)
Frequency or clock dividers are among the most common circuits used in digital
systems. Things get a little more complicated when we try to divide the frequency by an
odd number, since we can't simply divide the number of input clock cycles by 2. If we
observe the timing diagram describing a frequency divider by 5 in below figure , it give
the output with 50% Duty Cycle.

36
Ummidi Chandrika

RTL Code:

37
Ummidi Chandrika

Day 40: Greatest Common Divisor Using Behavioural Modelling


In this GCD calculation , I have used simple algorithm using repeated subtraction. It is
used to calculate the Greatest Common Divisor of any two numbers and the operation of
the circuit can be explained by flowchart below. But it is not synthesizeable as we are
using While loop.

Let's overcome the limitation of this Non-Synthesizeable circuit in the next code.
RTL Code:

38
Ummidi Chandrika

Day 41: Greatest Common Divisor via Data Path and Controller

In previous code of Greatest Common Divisor using Behavioral Modelling , the main
drawback is Non-Synthesizeable circuit , hence we implemented it using via Data Path
and Controller circuit so that the circuit can be synthesized.

The design of the GCD calculator should be divided into 2 parts - a controller and a
datapath. The controller is an FSM which issues commands to the datapath based on the
current state and the external inputs. This can be a behavioral description. The datapath
contains a netlist of functional units like multiplexers, registers, subtractors and a
comparator. The Datapath does the actual GCD computation.

39
Ummidi Chandrika

Day 42: Single Port RAM


The Single Port RAM block models RAM that supports sequential read and write
operations. If you want to model RAM that supports simultaneous read and write
operations, use the Dual Port RAM block.

40
Ummidi Chandrika

RTL Code:

Day 43: Dual Port RAM


Dual port memory provides a common memory accessible to both processors that can
be used to share and transmit data and system status between the two processors

41
Ummidi Chandrika

On the positive edge of the clock, when the write operation takes place, if port enable 1
is high , the the data is written into port 1 and if port 0 enable is high, data written into
port 0.But the read operation happens asynchronously without waiting for clock.
RTL Code:

42
Ummidi Chandrika

Day 44: Clock Buffer


A Buffer is an element which produces an output signal which is of the same value of the
input signal.
What is clock buffer vs normal buffer?
Clock buffers have equal rise and falltime. This prevents duty cycle of clock signal from
changing when it passes through a chain of clock buffers. Normal buffers are designed
with W/L ratio such that sum of rise time and fall time is minimum. They too are

designed for higher drive strength


RTL Code

43
Ummidi Chandrika

Day 45: Synchronous FIFO


The name FIFO stands for first in first out and means that the data written into the

buffer first comes out of it first.FIFOs are used in designs to safely pass multi-bit data

words from one clock domain to another, or to control the flow of data between source

and destination sides sitting in the same clock domain. If read and write clock domains

are governed by the same clock signal, FIFO is said to be synchronous FIFO
Why is synchronous FIFO needed?
Synchronous FIFOs are the ideal choice for high-performance systems due to high

operating speed. Synchronous FIFOs also offer many other advantages that improve

system performance and reduce complexity. These include status flags: synchronous
flags, half-full, programmable almost-empty and almost-full flags.
RTL Code:

44
Ummidi Chandrika

Day 46: Priority Encoder

The priority encoder is a circuit that executes the priority function. The logic of the
priority encoder is such that two or more inputs appear at an equal time, the input
having the largest priority will take precedence. The truth table of a 8*3 priority
encoder is given below.

45
Ummidi Chandrika

RTL Code:

Day 47: Seven segment display pattern using ROM


ROMs are the devices which are used to store information permanently.ROM is
implemented on verilog to store the display-pattern for seven-segment device

46
Ummidi Chandrika

RTL Code:

47
Ummidi Chandrika

Day 48: Serial Adder

RTL Code:

48
Ummidi Chandrika

Day 49: Fixed Priority Arbiter


Arbitration means deciding who gets the bus when two or more devices request it.
Priority is a means of specifying the relative importance of messages to be sent on the
MCB. In this MCB implementation, the priority will be based on the priority of the
calling task, and on whether this is a control or monitor request.
In Priority arbiter the priority of requesters are fixed not varying as in case of Round-
Robin arbiter

49
Ummidi Chandrika

RTL Code:

Day 50: Round Robin Arbiter


The Round-robin arbiter mechanism is useful when no starvation of grants is allowed.
The arbiter quantizes time shares each requestor is allowed to have. A minimal fairness

50
Ummidi Chandrika

is guaranteed by granting requestors in Round-robin manner. The requestors


can prioritize their time shares by the weight.
RTL Code:

51

You might also like