Synchronous FIFO Verilog
Synchronous FIFO Verilog
Synchronous FIFO Verilog
//---------------parameter declaration
parameter data_width = 4;
parameter address_width = 4;
parameter ram_depth =16;
(.address_1(wr_pointer),.address_2(rd_pointer),.data_1(data_in),.data_2(data_ram),.wr_en1(wr_en),.
rd_en2(rd_en),.clk(clk));
endmodule // sync_fifo
Design 4-bit Linear Feedback Shift Register (LFSR) using Verilog Coding and Verify with Test
Bench
Share8
Linear Feedback Shift Register is a sequential shift register with combinational feedback logic around
it that causes it to pseudo randomly cycle through a sequence of binary values. Feedback around
LFSR's shift register comes from a selection of points in the register chain and constitute either XORing
or XNORing these points to provide point back into the register. The LFSR basically loops through
repetitive sequences of pseudo random values. The maximum length of sequence is (2^n) - 1. Find out
VHDL Code Here.
wire feedback;
RTL view of LFSR is given below. Above code is synthisized by Xilinx Vivado tool.
initial
begin
clk_tb = 0;
rst_tb = 1;
#15;
rst_tb = 0;
#200;
end
always
begin
#5;
clk_tb = ~ clk_tb;
end
lfsr DUT(out_tb,clk_tb,rst_tb);
endmodule
Above Test Bench code is simulated and waveform result is shown below.
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
use ieee.std_logic_unsigned.all;
entity barrel_shifter is
port (
d_in : in std_logic_vector(7 downto 0); -- input vector
d_out : out std_logic_vector(7 downto 0); -- shifted output
shift_lt_rt : in std_logic; -- 0=>left_operation 1=>right_operation
shift_by : in std_logic_vector(2 downto 0); -- shift amount
clk : in std_logic; -- clock signal
rst_a : in std_logic; -- reset signal
p_load : in std_logic); -- parallel load
end barrel_shifter;
begin -- beh
p1: process (clk,rst_a,shift_by,shift_lt_rt)
variable x,y : std_logic_vector(7 downto 0);
variable ctrl0,ctrl1,ctrl2 : std_logic_vector(1 downto 0);
begin -- process p1
if p_load='1' then
case ctrl0 is
when "00"|"01" =>x:=d_in ;
when "10" =>x:=d_in(6 downto 0) & d_in(7); --shift left by 1 bit
when "11" =>x:=d_in(0) & d_in(7 downto 1); --shift right by 1 bit
when others => null;
end case;
case ctrl1 is
when "00"|"01" =>y:=x;
when "10" =>y:=x(5 downto 0) & x(7 downto 6); --shift left by 2 bits
when "11" =>y:=x(1 downto 0) & x(7 downto 2); --shift right by 2 bits
when others => null;
end case;
case ctrl2 is
when "00"|"01" =>d_out<=y ;
when "10"|"11" =>d_out<= y(3 downto 0) & y(7 downto 4); --shift right/left by 4 bits
when others => null;
end case;
end if;
end if;
end process p1;
end beh;
Design
Share5 Round Robin Arbiter using Verilog FSM Coding with Variable Slice Period
Round-robin (RR) is one of the simplest scheduling algorithms for processes in an operating system.
As the term is generally used, time slices are assigned to each process in equal portions and in circular
order, handling all processes without priority (also known as cyclic executive). Round-robin scheduling
is simple, easy to implement, and starvation-free. Round-robin scheduling can also be applied to other
scheduling problems, such as data packet scheduling in computer networks. In this Arbiter, we have
included one two bit counter. This counter will count no. of clock pulses for grant period of each request
signal. In this if in between of counter, the request signal goes low, then grant will be given to other
respective request signal and we can save that time slice.
module round_robin(clk,rst,req,grant);
always @(state,next_state,count)
begin
case (state)
s0:
begin
if (req[0])
begin
if(count==2'b11)
begin
if (req[1])
begin
count=2'b00;
next_state=s1;
end
else if (req[2])
begin
count=2'b00;
next_state=s2;
end
else if (req[3])
begin
count=2'b00;
next_state=s3;
end
else
begin
count=2'b00;
next_state=s0;
end
end // if (count==2'b11)
else
begin
count=count+2'b01;
next_state=s0;
end // else: !if(count==2'b11)
end // if (req[0])
else if (req[1])
begin
count=2'b00;
next_state=s1;
end
else if (req[2])
begin
count=2'b00;
next_state=s2;
end
else if (req[3])
begin
count=2'b00;
next_state=s3;
end
else
begin
count=2'b00;
next_state=s_ideal;
end
end // case: s0
s1:
begin
if (req[1])
begin
if (count==2'b11)
begin
if (req[2])
begin
count=2'b00;
next_state=s2;
end
else if (req[3])
begin
count=2'b00;
next_state=s3;
end
else if (req[0])
begin
count=2'b00;
next_state=s0;
end
else
begin
count=2'b00;
next_state=s1;
end
end // if (count==2'b11)
else
begin
count=count+2'b01;
next_state=s1;
end // else: !if(count==2'b11)
end // if (req[1])
else if (req[2])
begin
count=2'b00;
next_state=s2;
end
else if (req[3])
begin
count=2'b00;
next_state=s3;
end
else if (req[0])
begin
count=2'b00;
next_state=s0;
end
else
begin
count=2'b00;
next_state=s_ideal;
end
end // case: s1
s2:
begin
if (req[2])
begin
if (count==2'b11)
begin
if (req[3])
begin
count=2'b00;
next_state=s3;
end
else if (req[0])
begin
count=2'b00;
next_state=s0;
end
else if (req[1])
begin
count=2'b00;
next_state=s1;
end
else
begin
count=2'b00;
next_state=s2;
end
end // if (count==2'b11)
else
begin
count=count+2'b01;
next_state=s2;
end // else: !if(count==2'b11)
end // if (req[2])
else if (req[3])
begin
count=2'b00;
next_state=s3;
end
else if (req[0])
begin
count=2'b00;
next_state=s0;
end
else if (req[1])
begin
count=2'b00;
next_state=s1;
end
else
begin
count=2'b00;
next_state=s_ideal;
end
end // case: s2
s3:
begin
if (req[3])
begin
if (count==2'b11)
begin
if (req[0])
begin
count=2'b00;
next_state=s0;
end
else if (req[1])
begin
count=2'b00;
next_state=s1;
end
else if (req[2])
begin
count=2'b00;
next_state=s2;
end
else
begin
count=2'b00;
next_state=s3;
end
end // if (count==2'b11)
else
begin
count=count+2'b01;
next_state=s3;
end // else: !if(count==2'b11)
end // if (req[3])
else if (req[0])
begin
count=2'b00;
next_state=s0;
end
else if (req[1])
begin
count=2'b00;
next_state=s1;
end
else if (req[2])
begin
count=2'b00;
next_state=s2;
end
else
begin
count=2'b00;
next_state=s_ideal;
end
end // case: s3
default:
begin
if (req[0])
begin
count=2'b00;
next_state=s0;
end
else if (req[1])
begin
count=2'b00;
next_state=s1;
end
else if (req[2])
begin
count=2'b00;
next_state=s2;
end
else if (req[3])
begin
count=2'b00;
next_state=s3;
end
else
begin
count=2'b00;
next_state=s_ideal;
end
end // case: default
endcase // case (state)
end // always @ (state,next_state,count)
always @(state,next_state,count)
begin
case (state)
s0:begin grant=4'b0001; end
s1:begin grant=4'b0010; end
s2:begin grant=4'b0100; end
s3:begin grant=4'b1000; end
default:begin grant=4'b0000; end
endcase // case (state)
end
endmodule
Design
Share3 8x3 Priority Encoder in Verilog Coding and Verify with TestBench
Priority Encoder allocates priority to each input. Design and Test Bench code of 8x3 Priority Encoder is
given below. Output are set according to priorities of inputs. So if input with higher priority is present
then inputs with lower priorities are ignored and generates output according to highest priority input.
S. Name Direction Width Remark
No.
1. D_in IN 8 bit Input lines
3. D_out OUT 3 bit Output lines
Design Code
endmodule
Above code is synthesized by Xilinx Vivado and RTL view of Priority Encoder is shown below.
`timescale 1ns/1ps
module prio_enco_8x3_tst;
reg [7:0] d_in;
wire[2:0] d_out;
initial
begin
d_in=8'b11001100;
#10;
d_in=8'b01100110;
#10;
d_in=8'b00110011;
#10;
d_in=8'b00010010;
#10;
d_in=8'b00001001;
#10;
d_in=8'b00000100;
#10;
d_in=8'b00000011;
#10;
d_in=8'b00000001;
#10;
d_in=8'b00000000;
# 10;
$stop;
end // initial begin
endmodule
Above design code is simulated using given Test Bench code by Xilinx Vivado and Simulated waveform
is shown below.
In this wending machine, it accepts only two coins, 5 point and 10 point. Whenever total of coins equal
to 15 points, then nw_pa signal will go high and user will get news paper. It will not return any coin, if
total of points exceeds 15 points.
Design Code
module vending_machine(nw_pa,clk,coin,rst);
always @(state,coin)
begin
case (state)
s0:
begin
if (coin==2'b00)
next_state=s0;
else
if (coin==2'b01)
next_state=s5;
else
if (coin==2'b10)
next_state=s10;
end
s5:
begin
if (coin==2'b00)
next_state=s5;
else
if (coin==2'b01)
next_state=s10;
else
if (coin==2'b10)
next_state=s15;
end
s10:
begin
if (coin==2'b00)
next_state=s10;
else
if (coin==2'b01)
next_state=s15;
else
if (coin==2'b10)
next_state=s15;
end
s15:
begin
next_state=s0;
end
default : next_state=s0;
always @(state)
begin
case (state)
s0 : nw_pa<=1'b0;
s5 : nw_pa<=1'b0;
s10: nw_pa<=1'b0;
s15: nw_pa<=1'b1;
default: nw_pa<=1'b0;
endcase // case (state)
end
endmodule
Design Traffic Light Controller using Verilog FSM Coding and Verify with Test Bench
Given below code is design code for Traffic Light Controller using Finite State Machine(FSM). In this
clk and rst_a are two input signal and n_lights, s_lights, e_lights and w_lights are 3 bit output signal. In
output signal, "001" represents Green light, "010" represents Yellow light and "100" represents Red
light. On the reset signal, design will enter into north state and start giving output after reset will go low.
Design will turn on Green light for eight clock cycles and Yellow light for four clock cycles. Design will
start with north, then goes into south, then east and finally into west and by this it will keep going.
Design Code
module traffic_control(n_lights,s_lights,e_lights,w_lights,clk,rst_a);
north_y :
begin
if (count==3'b011)
begin
count=3'b000;
state=south;
end
else
begin
count=count+3'b001;
state=north_y;
end
end
south :
begin
if (count==3'b111)
begin
count=3'b0;
state=south_y;
end
else
begin
count=count+3'b001;
state=south;
end
end
south_y :
begin
if (count==3'b011)
begin
count=3'b0;
state=east;
end
else
begin
count=count+3'b001;
state=south_y;
end
end
east :
begin
if (count==3'b111)
begin
count=3'b0;
state=east_y;
end
else
begin
count=count+3'b001;
state=east;
end
end
east_y :
begin
if (count==3'b011)
begin
count=3'b0;
state=west;
end
else
begin
count=count+3'b001;
state=east_y;
end
end
west :
begin
if (count==3'b111)
begin
state=west_y;
count=3'b0;
end
else
begin
count=count+3'b001;
state=west;
end
end
west_y :
begin
if (count==3'b011)
begin
state=north;
count=3'b0;
end
else
begin
count=count+3'b001;
state=west_y;
end
end
endcase // case (state)
end // always @ (state)
end
always @(state)
begin
case (state)
north :
begin
n_lights = 3'b001;
s_lights = 3'b100;
e_lights = 3'b100;
w_lights = 3'b100;
end // case: north
north_y :
begin
n_lights = 3'b010;
s_lights = 3'b100;
e_lights = 3'b100;
w_lights = 3'b100;
end // case: north_y
south :
begin
n_lights = 3'b100;
s_lights = 3'b001;
e_lights = 3'b100;
w_lights = 3'b100;
end // case: south
south_y :
begin
n_lights = 3'b100;
s_lights = 3'b010;
e_lights = 3'b100;
w_lights = 3'b100;
end // case: south_y
west :
begin
n_lights = 3'b100;
s_lights = 3'b100;
e_lights = 3'b100;
w_lights = 3'b001;
end // case: west
west_y :
begin
n_lights = 3'b100;
s_lights = 3'b100;
e_lights = 3'b100;
w_lights = 3'b010;
end // case: west_y
east :
begin
n_lights = 3'b100;
s_lights = 3'b100;
e_lights = 3'b001;
w_lights = 3'b100;
end // case: east
east_y :
begin
n_lights = 3'b100;
s_lights = 3'b100;
e_lights = 3'b010;
w_lights = 3'b100;
end // case: east_y
endcase // case (state)
end // always @ (state)
endmodule
Above design code is synthesized by Xilinx Vovado and RTL view of design is shown below.
`timescale 1ns/1ps
module traffic_control_tb;
initial
begin
clk=1'b1;
forever #5 clk=~clk;
end
initial
begin
rst_a=1'b1;
#15;
rst_a=1'b0;
#1000;
$stop;
end
endmodule
Test Bench is simulated using Xilinx Vivado and waveform is shown below