script async='async' crossorigin='anonymous' src='https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-6016566166623052'/> Verilog coding: Verilog
Showing posts with label Verilog. Show all posts
Showing posts with label Verilog. Show all posts

Monday, 23 October 2023

What is an FPGA and How Does It Work?

What is an FPGA and How Does It Work?

Field-Programmable Gate Arrays (FPGAs) are versatile integrated circuits that have revolutionized the world of digital electronics. They offer a unique blend of flexibility and high-performance processing capabilities, making them a crucial component in various industries, from telecommunications to automotive to data centers. But what exactly is an FPGA, and how does it work? In this blog post, we'll unravel the mysteries of FPGAs, shedding light on their architecture, functioning, and the myriad of applications they serve.

Understanding FPGA Basics


At its core, an FPGA is a reconfigurable semiconductor device that can be programmed to perform specific tasks. Unlike Application-Specific Integrated Circuits (ASICs), which are hardwired for specific functions, FPGAs are programmable to execute a wide range of tasks. The heart of an FPGA is a sea of logic gates and interconnects, which can be configured to create custom digital circuits tailored to a particular application.\

FPGA Architecture


Look-Up Tables (LUTs)

One of the key components of an FPGA is the Look-Up Table (LUT). LUTs act as digital logic blocks and are the building blocks of the FPGA's reprogrammability. A LUT takes a set of input signals and produces an output based on a predefined truth table. For example, a 2-input LUT can represent any logic function with two inputs.

Configurable Interconnects

The interconnects within an FPGA are just as critical as the LUTs. These interconnections are programmable, allowing you to connect different LUTs in a multitude of ways. This flexibility is what makes FPGAs adaptable to different applications.


Programmable Routing


FPGAs have programmable routing resources that enable the establishment of connections between LUTs and other logic elements. The routing is programmed to create custom data paths and connect specific components as needed for the given application.


 How FPGAs Work


When an FPGA is programmed, a Hardware Description Language (HDL) like VHDL or Verilog is used to specify the desired functionality. The HDL code is synthesized into a netlist of digital logic components and connections. This netlist is then mapped to the FPGA's LUTs and interconnects, configuring the device to perform the desired functions.


Once programmed, FPGAs can execute complex operations in parallel, making them exceptionally fast and well-suited for tasks like signal processing, image recognition, and real-time control systems.


 Applications of FPGAs


FPGAs have found their way into numerous applications. They are commonly used in telecommunications equipment, where their reprogrammability allows for easy upgrades and adaptability to new standards. They are also employed in aerospace and automotive industries for control systems, safety features, and data processing. In data centers, FPGAs are used for acceleration of specific tasks, such as encryption and deep learning.


Conclusion


Field-Programmable Gate Arrays are remarkable devices that offer the best of both worlds: the flexibility of software and the performance of dedicated hardware. Their programmable nature makes them invaluable in today's ever-changing technological landscape. Whether you're a hardware engineer, a software developer, or simply curious about the world of electronics, FPGAs are fascinating tools to explore. As technology continues to evolve, the role of FPGAs in shaping our digital world is only set to expand, and understanding their fundamental operation is key to appreciating their potential.






Keywords,

FPGA, Field-Programmable Gate Array, FPGA architecture, LUT (Look-Up Table), Configurable interconnects, Programmable routing, HDL (Hardware Description Language),  FPGA programming, FPGA functionality,  FPGA applications,  FPGA in telecommunications,  FPGA in aerospace,  FPGA in automotive, Data center acceleration, Digital logic components, Reconfigurable semiconductor, FPGA basics, VHDL, Verilog, parallel processing


Wednesday, 14 June 2023

Cascading Muxes for Larger Multiplexing: 4x1MUX, 8x1 MUX

Cascading Muxes for Larger Multiplexing: FPGA 

Introduction to Cascading Muxes for Larger Multiplexing

Multiplexers (Muxes) are fundamental digital components used to select one out of multiple input signals based on control signals. When dealing with larger multiplexing requirements, cascading multiple smaller Muxes can be an effective approach. This technique allows us to build larger Muxes with a higher number of inputs.

Cascading Muxes involves connecting the output of one Mux to the input of another Mux, effectively creating a hierarchical structure. Each stage of Muxes reduces the selection size by half, leading to a significant reduction in the number of required control signals.

To illustrate this concept, consider an 8x1 Mux, which selects one out of eight input signals. By cascading two 4x1 Muxes, we can create an 8x1 Mux. The first stage Mux selects between four input signals, and the second stage Mux selects between the outputs of the first stage Mux.

This cascading technique can be extended to create larger Muxes by adding more stages, each with smaller Muxes. The final output is obtained by selecting the appropriate signals based on the control signals at each stage.

Implementing cascading Muxes can be achieved using hardware description languages such as Verilog or VHDL. By properly designing and connecting the inputs, outputs, and control signals, the cascaded Muxes can be synthesized and implemented on an FPGA board.

Cascading Muxes offers a scalable and efficient solution for handling larger multiplexing requirements in digital logic designs. It provides flexibility and modularity by leveraging smaller Muxes to create larger ones, reducing the complexity and improving the overall efficiency of the design.

In the next section, we will explore a sample Verilog code that demonstrates the implementation of an 8x1 Mux using two cascaded 4x1 Muxes.

Example of Verilog code for cascading 4x1 multiplexers to create a larger multiplexer, such as an 8x1 Mux:


Verilog Code:

module CascadedMux(

  input wire [2:0] select,

  input wire [7:0] data,

  output wire out

);

wire [3:0] mux1_out;

wire [3:0] mux2_out;

wire [1:0] select_mux2;

assign select_mux2 = select[1:0];

mux4to1 mux1(

  .select(select[2]),

  .data({data[7], data[6], data[5], data[4]}),

  .out(mux1_out)

);

mux4to1 mux2(

  .select(select_mux2),

  .data({data[3], data[2], data[1], data[0]}),

  .out(mux2_out)

);

mux2to1 mux3(

  .select(select[0]),

  .data({mux2_out[3], mux2_out[2]}),

  .out(out)

);

endmodule


module mux4to1(

  input wire select,

  input wire [3:0] data,

  output wire out

);

assign out = select ? data[3] : 

             select ? data[2] :

             select ? data[1] :

                      data[0];

endmodule


module mux2to1(

  input wire select,

  input wire [1:0] data,

  output wire out

);

assign out = select ? data[1] : data[0];

endmodule


Explanation of the Code:

1. The `CascadedMux` module takes a 3-bit `select` signal and an 8-bit `data` input and provides a single-bit `out` output.

2. The `mux4to1` module is defined to implement a 4x1 multiplexer. It takes a `select` signal and a 4-bit `data` input, and provides a single-bit `out` output based on the select signal.

3. The `mux2to1` module is defined to implement a 2x1 multiplexer. It takes a `select` signal and a 2-bit `data` input, and provides a single-bit `out` output based on the select signal.

4. In the `CascadedMux` module, two instances of `mux4to1` are instantiated to implement the first and second stages of the cascaded multiplexer.

5. The `mux2to1` module is instantiated to implement the final stage of the cascaded multiplexer.

6. The `select` signal is appropriately split to select the desired data inputs for each stage, and the final output is assigned to the `out` signal.


Please note that this code assumes the availability of `mux4to1` and `mux2to1` modules. You can replace these modules with your own implementation or use built-in modules provided by the FPGA board's library.


Feel free to modify the code according to your specific requirements and make any necessary connections to the FPGA board.

Sunday, 26 April 2020

Ripple Counter

Verilog HDL for Ripple Counter:

Here is an example of Verilog code for a ripple counter, which is a type of counter that uses a single clock input to increment the count value: 

fpga verilog code example

Example#1

module ripple_counter(input clk, input rst, output reg [3:0] count);

always @(posedge clk) begin
    if (rst)
        count <= 4'b0000;
    else
        count <= count + 1'b1;
end

endmodule


This code defines a module called "ripple_counter" that has a 4-bit output (count) and two inputs: a clock input (clk) and a reset input (rst). The always block is triggered on the rising edge of the clock input and increments the count value by 1 on each rising edge of the clock. The if-else statement checks the reset input, if it is high the count is reset to zero.

The output will be the count value of the ripple counter. The count value will increment every clock cycle until it reach the maximum value of the count register. It's important to note that ripple counters are not recommended for high-frequency or high-speed applications as the propagation delay of the output stage causes the output to change at different times for each bit, resulting in glitches and inaccuracies.



Example# 2

!timescale 1ns/100 ps
module Ripple-Counter_4bit (A3, A2, Al, A0, Count, Reset);
output  A3, A2, Al, A0;
input    Count, R~set;
// Instantiate complementing flip-flop
Comp_D_flip_flop F0 (A0, Count, Reset);
Comp_D_flip_flop Fl (A1, A0, Reset);
Comp_D_flip_flop F2 (A2, A1 , Reset);
Comp_D_flip_flop F3 (A3, A2, Reset);
endmodule
// Complementing flip-flop with delay
// lnput to D flip-flop = Q'
module Comp_D_flip_flop (Q, CLK, Reset);
output   Q;
Input    CLK, Reset;
reg        Q;
always @  (negedge CLK, posedge Reset)
If (Reset) Q <= 1 'b0;
else Q <= #2 ~Q;                       //Intra-assignment delay
endmodule
// Stimulus for testing ripple counter
module t_Ripple_Counter_4bit;
reg Count;
reg Reset;
wire A0, Al , A2 A3;
// Instantiate ripple oounter
Rippte_Counter_4bit M0 (A3, A2, At, A0, Count, Reset);
always
#5 Count = ~Count;
lnitial
    begin
       Count = 1'b0;
       Reset = l'bl;
      #4 Reset = l'b0;
end
initial#170 $finish;

endmodule

Four-bit binary counter with parallel load







Graphical Symbol of Four-bit binary counter with parallel load

Four-bit binary counter with parallel load

Functional Table of Four-bit binary counter with parallel load


fpga verilog code example
Verilog HDL of Four-bit binary counter with parallel load
module Bin-Counter-4bit-Par_Load (
output reg   [3: 0]  A-count,                    // Data output
output          C-out,                                  //Output any
input            [3: 0] Data-in,                      // Data input
input            Count,                                  //Active high to count
                      Load,                                   //Active high to load
                      CLK,                                   // Positive-edge sensitive
                      Clear                                   //Active low  
);
assign      C-out = Count & (~Load) & (A_count == 4'b111l);
always @ (posedge CLK, negedge Clear)
If (~Clear)                          A_count <= 4'b0000;
else If (Load)                      A_count <= data-in;
else If (Count)                    A_count  <= A_count + l'bl;
else                                     A_count <= A_count;            //redundant statement
endmodule


D- Flip Flop Verilog HDL

A D-  Flipflop is the simplest example of a sequential machine. Verilog HDL describes 2-positive edge D- flip-flops in two different modules. The first responds only to the clock signal Clk, the second includes an asynchronous signal reset input rst. Output Q must be declared as a register reg data type in
addition to being listed as an output. Because in a procedural assignment statement, it is a target output. The keyword posedge endures that the transfer of input D into Q is synchronized by the positive-edge transition of Clk. A change in D input at any other time does not change in Output Q.
Positive and Negative Edge D flipflop
The 2nd module includes an Asynchronous reset input, in addition to the synchronous
clk. A specific form of an if statement is used to describe such a flip-flop so that the model
can be synthesized by a software tool. The event expression in the always statement after the @ symbol may have any number of edge events, either posedge or negedge. For modelling hardware,
one event must be a clock event. The other events specify conditions under
which asynchronous logic is to be executed. The flip flop designer knows which signal is the clock signal, but the clock is not an identifier that software tools automatically recognize, as the synchronizing signal of a flip flop circuit. The software tool must be able to conclude which signal is the clock signal, so you need to write the description in a way that enables the software tool to conclude the clock signal correctly, The rules are simple to follow:
( 1) Each if or else if statement in the procedural assignment statements is to correspond to an asynchronous event, (2) the last else statement cornponds to the clock event, and (3) the
asynchronous events are tested first. Thm am two edge events in the second duk of HDL D flip-flop. The negedge rst (reset) event is asynchronous, since it matches the if (-rst) statement. As long as reset signal rst is 0, output Q is cleared to 0. If Clock signal Clk has a positive transition, its effect is
blocked only if reset signal rst is logic 1 can the posedge clock event synchrounously transfer Input D into Output Q.

D- Positive edge triggered flip-flop

Master-Slave D flip-flop



fpga verilog code example

HDL Verilog of D flipflop
// D flip-flop without reset
module D-Flip (Q, D, Clk);
output Q;
input D, Clk;
reg Q;
always @ (posedge Clk)
Q <= D;
endmodule

// D flipflop with asynchronous reset

module DFlip (output reg Q, input D, Clk, rst);
always @ (posedge Clk, negedge rst)
If (-rst) Q <= I'b0;          // Same as: if (rst == 0)
else Q <= D;
endmodule

D-Latch Verilog code:

The D-latch is a transparent and responds to a change in data input with a change in output, as long as
the enable input is high. It has one output Q and  two inputs, D and en. Since Q is evaluated in a procedural statement, it must be declared as register type. D-latch responds to input signal levels, so the two inputs are listed without edge qualifiers in the event enable expression following the @ symbol in the always statement. it has only one blocking procedural assignment statement and it also specifies the transfer of D-input to Q-output if logic 1 i.e when enable is true. This statement is executed every time when there is a change in input D if enable is  logic1.
D-Latch Gate level and truth Table.
D-Latch Graphical Symbol.
fpga verilog code example
Verilog of D latch

module D-latch (Q, 0, en); // en is enable
output Q;
Input D, en;
reg Q;
always @ (en or D)
If (en) Q <= D;   //Same as: If (enable == 1)
endmodule


// Alternative syntax 
module D-latch (output reg Q, input enable, D);
always @ (enable, D)
If (enable) Q <= D;   // No action if enable not asserted
endmodule


Here is an example of VHDL code for a D-latch:


library IEEE; use IEEE.STD_LOGIC_1164.ALL; entity D_latch is Port ( D : in STD_LOGIC; clk : in STD_LOGIC; Q : out STD_LOGIC); end D_latch; architecture Behavioral of D_latch is begin process(clk) begin if (clk'event and clk = '1') then Q <= D; end if; end process; end Behavioral;

And here is an example of Verilog code for a D-latch:


module D_latch (input D, clk, output reg Q); always @(posedge clk) begin Q <= D; end endmodule


Both codes define a D-latch module with inputs D and clk, and an output Q. The D-latch is a level sensitive device and uses the input D as the data input, and the input clk as the enable input.

In the VHDL code, the process is sensitive to the clock input and the output Q is assigned the value of the input D when there is a rising edge on the clock input.

In the Verilog code, the always block is triggered on the rising edge of the clock input and assigns the value of the input D to the output Q.

Both codes implements the same functionality and are equivalent. Please note that this is a basic example and the actual code may depend on the specific requirements of your design.

Thursday, 14 December 2017

Parallel Multiplier Verilog HDL code

Verilog Code for Parallel Multiplier:

A parallel multiplier is a type of digital circuit that performs multiplication on two numbers in parallel, using multiple arithmetic units in parallel. This allows the circuit to perform multiplication faster than a serial multiplier, which performs multiplication using a single arithmetic unit.

fpga verilog code example

Code For Parallel Multiplier.

//parallel multiplier (n = 8)
module parallel_Multiplier (Product, multiplicand, Multiplier);
input [7: 0] multiplicand, Multiplier;
output reg [15: 0] Product;
always @ (multiplicand, Multiplier)
Product1 = multiplicand*Multiplier;
endmodule

module Algorithmic-Binary-Multiplier #(parameter dp-width =5) (
output [2*dp_width -1 : 0] Product, Input [dp-width -1 : 0] multiplicand, Multiplier);
reg [dp-width -1: 0] A, B, Q; //Sized for datapath
reg C;
Integer k;
assign Product1 = {C, A, Q};
always @ (multiplicand, Multiplier) begin
Q = Multiplier;
B = multiplicand;
C=0;
A=0;
for(k= 0; k <= dp-width-1; k= k+ 1)
begin
if (Q[0]) {(C, A) = A + B;
{C, A, Q) = {C, A, Q} >> 1;
end
end
endmodule

module t-Algorithmic-Binary-Multiplier;
parameter dp-width = 5; // Width of data path
wire [2* dp-width -1: 0] Prdct;
reg [dp-width -1: 0]multiplicand, Multiplier;
integer Exp-Value;
reg Error;
Algorithmic-Binary-Multiplier M0 (Product, multiplicand, Multiplier);
// Error detection
initial# 1030000 finish:
always @ (Product) begin
Exp-Value =multiplicand, Multiplier;
Error = Exp-Value ^ Product;
end
initial begin
#5 multiplicand= 0;
 Multiplier = 0;
repeat (32)# 10 begin  Multiplier =  Multiplier + 1;
repeat (32) #5 multiplicand= multiplicand + 1 :
end
end
endmodule

Here is an example of VHDL code for a parallel multiplier:

library IEEE; use IEEE.STD_LOGIC_1164.ALL; entity parallel_multiplier is Port ( a : in STD_LOGIC_VECTOR (3 downto 0); b : in STD_LOGIC_VECTOR (3 downto 0); p : out STD_LOGIC_VECTOR (7 downto 0)); end parallel_multiplier; architecture Behavioral of parallel_multiplier is begin p <= a * b; end Behavioral;

This code defines a parallel multiplier module called "parallel_multiplier" with two inputs "a" and "b" and an output "p". The inputs are 4-bit vectors and the output is 8-bit vector. The architecture uses the built-in multiplication operator to perform the multiplication of the inputs in parallel.

Here is an example of Verilog code for a parallel multiplier:


module parallel_multiplier(input [3:0] a, input [3:0] b, output [7:0] p); assign p = a * b; endmodule

This code defines a parallel multiplier module called "parallel_multiplier" with two inputs "a" and "b" and an output "p". The inputs are 4-bit vectors and the output is 8-bit vector. The assign statement is used to perform the multiplication of the inputs in parallel.

Please note that this is a basic example and the actual code will depend on the specific requirements of your design, the width of the inputs, and the width of the output. Some designs may require more complex implementation and also this is a combinational multiplier and not a pipelined multiplier.


Transmission Gate Verilog code

Transmission Gate:

In VHDL the transmission gate is represented with the keyword Cmos. Transmission Gate has one output, one input and two control signals. It is referred to as a Cmos switch. The code for Transmission gate is as follows:
cmos (output, Input, contorl1, control2); // general description
cmos (Y, X, A, B); // transmission gate
Normally, control signal 1 and control signal 2 are the complement of each others. The Cmos switch doesn't need any power source, However VDD and ground are connected to the substrates of the MOS transistors. Tansmission gates are useful for building Multiplexer (MUX) and flip-flops with CMOS circuits.

                                                   Transmission Gate

                                                      Block diagram


                                                Behavior of the switch

fpga verilog code example

Verilog Code for Transmission gate : 

module Cmos-XOR (A, B, Y);
Input A, B;
output Y:
wire A1, B1;
// represented inverter
inverter in1 {A1, A);
inverter in2 (B1, B);
// instantiate Cmos switch
cmos (Y, B, A1, A);                 // cmos (output, Input, control1, control2);
cmos (Y, B1, A, A1);
endmodule

//Cmos Inverter
module inverter (Y, A);
input A;
output Y;
supply1 PWR;
supply0 GND;
pmos (Y, PWR, A);  //(Drain, source. gate)
nmos (Y, GND, A);
endmodule

// Stimulus to test Cmos-XOR
module test-Cmos-XOR;
reg A,B;
wire Y;
//instantiate Cmos-XOR
Cmos-XOR XI (A, B, Y);
// Apply truth table
initial
begin
A = I'b0; B = 1'b0;
#5 A = I'b0; B = l'bl;
#5A = I'bl; B = l'b0;
#5A = l'bl; B = l'bl;
end

// Display results
initial
$monitor ("A =%b B= %b Y =%b", A, B, Y);
endmodule

Verilog Code for Carry Look-ahead Adder

Carry Lookahead Adder:

A Carry lookahead adder is a very complex hardwareadder. In carry lookahead adder the ripple carry transformed in such that the carry logic is reduced to two-level logic. Following is the Verilog code for Carry LookAhead adder: 
fpga verilog code example

VHDL for Carry Lookahead Adder:

module carry_lookahead_addre(a,b, cin, s, cout , pg, gg);
input [3:0]a,b;
input cin;
output [3:0]s;
output cout,pg,gg;
wire [3:0]p,g,c;
assign g=a&b;
assign p=a^b;
assign c[0]=cin;
assign c[1]=g[0]|(p[0]&c[0]);
assign c[2]=g[1]|(p[1]&g[0])|(p[1]&p[0]&c[0]);
assign c[3]=g[2]|(p[2]&g[1])|(p[2]&p[1]&g[0])|(p[1]&p[2]&c[0]);
assign cout=g[3]|(p[3]&g[2])|(p[3]&p[2]&g[1])|(p[3]&p[2]&p[1]&g[0])|(p[3]&p[2]&p[1]&p[0]&c[0]);
assign sum=p^c;
assign pg=p[3]&p[2]&p[1]&p[0];
assign gg=g[3]|(p[3]&g[2])|(p[3]&p[2]&g[1])|(p[3]&p[2]&p[1]&g[0]);
endmodule


 TestBench for Carry Lookahead Adder;

module carry_v;
// Inputs
reg [3:0] a;
reg [3:0] b;
reg cin;
// Outputs
wire [3:0] s;
wire cout;
wire pg;
wire gg;
// Instantiate the Unit Under Test (UUT)
carrylookahead uut (
.a(a),
.b(b),
.cin(cin),
.s(s),
.cout(cout),
.pg(pg),
.gg(gg)
);
initial begin
// Initialize Inputs
a = 0;
b = 0;
cin = 0;
// Wait 100 nano seconds  for global rst to finish
#100;
a = 1;
b = 0;
cin = 1;
// Wait 100 nano seconds  for global rst to finish
#100;
a = 2;
b = 2;
cin = 0;
// Wait 100 nano seconds  for global rst to finish
#100;
a = 3;
b = 2;
cin = 1;
// Wait 100 nano seconds  for global rst to finish
#100;
a = 6;
b = 3;
cin = 0;
// Wait 100 nano seconds  for global rst to finish
#100;
a = 1;
b = 6;
cin = 0;
// Wait 100 nano seconds  for global rst to finish
#100;
// Add stimulus here
end
endmodule


Verilog Code for 8-bit Booth’s Multiplier

This is a Multiplication algorithm which multiplies two binary numbers's in 2's Compliment.

Following is the 8-bits Booth's Multiplier verilog code:
fpga verilog code example

VHDL of 8-bit Booth’s Multiplier

module 8_bit_booth_mult(prd, busy, mc, mp, clk, start);
output [15:0] prd;
output busy;
input [7:0] mc, mp;
input clk, start;
reg [7:0] A, Q, B;
reg Q_1;
reg [3:0] count;
wire [7:0] sum, difference;
always @(posedge clk)
begin
if (start) begin
A <= 8'b0;
B <= mc;
Q <= mp;
Q_1 <= 1'b0;
count <= 4'b0;
end
else begin
case ({Q[0], Q_1})
2'b0_1 : {A, Q, Q_1} <= {sum[7], sum, Q};
2'b1_0 : {A, Q, Q_1} <= {difference[7], difference, Q};
default: {A, Q, Q_1} <= {A[7], A, Q};
endcase
count <= count + 1'b1;
end
end
alu adder (sum, A, B, 1'b0);
alu subtracter (difference, A, ~B, 1'b1);
assign prd = {A, Q};
assign busy = (count < 8);
endmodule
//For alu.
//It is an adder, but capable of subtraction:
//Recall that subtraction means adding the two's complement
//a - b = a + (-b) = a + (inverted b + 1)
//The 1 will be coming in as Cin (carry-in)
module alu(out, a, b, cin);
output [7:0] out;
input [7:0] a;
input [7:0] b;
input cin;
assign out = a + b + cin;
endmodule



Testbench for Booth’s Multiplier


module testbench;
reg clk, start;
reg [7:0] a, b;
wire [15:0] ab;
wire busy;
booth_multiplier multiplier1(ab, busy, a, b, clk, start);
initial begin
clk = 0;
$display("first example: a = 3 b = 17");
a = 3; b = 17; start = 1; #50 start = 0;
#80 $display("first example done");
$display("second example: a = 7 b = 7");
a = 7; b = 7; start = 1; #50 start = 0;
#80 $display("second example done");
$finish;
end
always #5 clk = !clk;
always @(posedge clk) $strobe("ab: %d busy: %d at time=%t", ab, busy, $stime);
endmodule






Verilog code for 4-Bit universal shift register:

 4-Bit universal shift register:

                         You are looking for 4 bit Universal shift register? The Shift Register is one of sequential logic circuit that is used for the storage or the transfer of binary data.  A simple register can shift only in one direction that is called uni-directional shift register ' and ' A register that shifts in both direction (left or right ) is called bi-directional shift registers.  So, if a register shifts in both directions and ('Left to right' or 'right to left ') and has parallel load capabilities then it is called ' Universal shift register'.  Following is the Functional table and Block Diagram For "4 bit shift register".



Function table for the register

       

                           Fig.1: 4 Bit universal shift register

fpga verilog code example

Verilog code of D-Flip Flop:

module 4bit_universal_shift_reg(d, clr, clk, Q);
input d, clr, clk;
output Q;
reg Q;
always @ (negedge clr or posedge clk)
begin
if (!clr)
Q<= 1’b0;
else
Q<= d;
end
endmodule

Friday, 14 April 2017

Verilog code for 2-to-4 line Decoder:

How to program 2-to-4 line Decoder in verilog?
A Decoder is a simple logic gates diagram that changes a code into a set of different signals. Decoder is the reverse process of Encoders.
A simple/common  decoder is the line decoder which takes an n-digit binary number and decodes it into 2n data lines. The simplest decoder is the 1-to-2 line decoder. Following is the truth table of 1-to-2 line decoder,

Following is the truth table of 2-to-4 line decoder ;


when it developed into a logic circuit it looks like





Verilog code of 2 to 4 line decoder:

module decoder(d,A0,A1);
output [3:0] d;
input x,y;
assign d[0] = ~A0 & ~A1 ;
assign d[1] = A0 & ~A1;
assign d[2] = ~A0 & A1;
assign d[3] = A0 & A1 ;
endmodule





fpga verilog code example