element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
FPGA
  • Technologies
  • More
FPGA
Blog The Art of FPGA Design - Post 7
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join FPGA to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: fpgaguru
  • Date Created: 21 Aug 2018 3:57 PM Date Created
  • Views 1806 views
  • Likes 6 likes
  • Comments 4 comments
  • vhdl
  • fpga_art
Related
Recommended

The Art of FPGA Design - Post 7

fpgaguru
fpgaguru
21 Aug 2018

We're Not in Kansas Anymore

 

In this post we will try to do things you probably never tried to do using VHDL, assuming they were not even possible in such a low level language.

 

VHDL and Verilog/SystemVerilog are considered low level hardware design languages, compared with C/C++ based HLS flows for example. Only schematic capture is lower on the totem pole and hopefully nobody is still doing FPGA designs using schematics anymore. But the low level label attached to HDLs is only partially correct. They are low level only if you use them that way. It is possible to do very high level things in VHDL if you really want.

 

There is a natural tendency for people to try to solve the problem at hand using the most direct path to the solution. If you only had to solve one engineering problem in your entire life that approach would be the correct one, however, one can expect to have to solve hundreds of somewhat similar problems during a career that can span decades. The shortest possible path to a solution is then rarely the best one in the long term. Rather than trying to solve the current particular problem in the simplest possible way, it might make sense to try to write more general code, which solves a class of problems rather than just a single one. Designs are modular and a good design practice is to try to build generic and reusable modules that can be instantiated multiple times in the same design or used in different designs, even by different designers. This post is the first one in a series that will focus on this idea of writing higher level HDL code. Here we start with the foundation, generic reusable data types.

 

We have seen in the previous post that it is possible to create our own user defined arbitrary size fixed point precision data type, even within the constraints of the VHDL-93 language standard. The next step up would be a complex arbitrary precision fixed point type - let's call it CFIXED - and then arbitrary size vectors and matrices of SFIXED and CFIXED numbers. In VHDL you can create more complicated types out of simpler ones using arrays and records. There is no limit to the complexity of composite types, you can have arrays of records of arrays and so on. However, to create generic and reusable code we want unconstrained arrays and this is where VHDL-93 (and any Verilog and SystemVerilog flavor for that matter) has a major limitation - only the top level array in a composite type can be unconstrained, lower level array types cannot be unconstrained and records of unconstrained arrays are also illegal. So if we were to try to define a complex fixed point type CFIXED based on the SFIXED type we introduced earlier we run into a problem:

  type SFIXED is array(INTEGER range <>) of STD_LOGIC;     – arbitrary precision fixed point signed number, like SIGNED but lower bound can be negative
 
type CFIXED is record RE,IM:SFIXED; end record;          – arbitrary precision fixed point complex signed number - not valid in VHDL-93!
 
signal R:SFIXED(1 downto -4);
 
signal I:SFIXED(R'range); – another SFIXED signal with the same range as R
 
signal A,B,C:CFIXED(RE(R'range),IM(I'range));
begin
  R<=TO_SFIXED(0.125,R);    – 00.0010
  I<=TO_SFIXED(-0.375,I);   – 11.1010
  A<=TO_CFIXED(R,I);        – (00.0010,11.1010)
  B<=TO_CFIXED(1.0,-0.5,B); – (01.0000,11.1000)
  C<=RESIZE(A+B,C);         – (01.0010,11.0010)

This CFIXED type definition is not valid in VHDL-93 and we will get an error. The good news is that this limitation has been removed in VHDL-2008, so the code snippet above is perfectly legal now in that context. The bad news is that support for the new standard is still quite spotty - some tools do not support VHDL-2008 at all, others do but only in some limited way. Just to give an example, the VHDL-2008 feature I used above is fully supported by Vivado Synthesis but not by the Vivado Simulator, although it does implement some other VHDL-2008 aspects. This means that while you can synthesize and implement VHDL designs that use the CFIXED type defined above, you cannot simulate them with Vivado, you have to use a different simulator like QuestaSim, which unfortunately is not free. With the hope that this will be remedied sooner rather than later in Vivado let's move on to introducing some even more complicated types:

  type SFIXED_VECTOR is array(INTEGER range <>) of SFIXED; – unconstrained array of SFIXED
 
type CFIXED_VECTOR is array(INTEGER range <>) of CFIXED; – unconstrained array of CFIXED
 
type CFIXED_MATRIX is array(INTEGER range <>) of CFIXED_VECTOR; – unconstrained array of CFIXED_VECTOR

Similarly, SFIXED_VECTOR, CFIXED_VECTOR and CFIXED_MATRIX are not valid VHDL-93 type definitions but they work without any issue in VHDL-2008. CFIXED_MATRIX for example is a very generic type, it can be used to represent matrices of any size MxN, with complex elements with the real and imaginary parts being arbitrary precision fixed point numbers of any range. We can use these types now to do things like these:

  signal SV:SFIXED_VECTOR(0 to 3)(1 downto -4);
 
signal CV:CFIXED_VECTOR(0 to 3)(RE(1 downto -4),IM(1 downto -4));
 
signal CM:CFIXED_MATRIX(0 to 3)(0 to 3)(RE(1 downto -4),IM(1 downto -4));
begin
  SV(1)<=TO_SFIXED(1.25,1,-4);
  CV(
0).RE<=SV(1);
  CM(
2)<=CV;  – CM(2)(0).RE is now 1.25
  CM(0)(0)<=RESIZE(CM(0)(0)+SV(1),CM(0)(0));

Like we did with SFIXED, we can now create our own overloaded operators and functions to convert and resize signals of these new types. We are free to define whatever operators and functions we need. It is possible for example to overload '+' and '*' so that we can add and multiply CFIXED_MATRIX signals directly in a single step. We can define Galois field arithmetic on vectors and matrices where '+' is actually XOR and '*' is AND and express any CRC like algorithm very elegantly and in a compact way in terms of vector and matrix add and multiply operations.

These are just a few examples but the sky is the limit. This is where thinking about what we actually want to achieve a bit will pay off in the long run. We do not want to have to reinvent the wheel every time we need these types and functions in a new design. The right way to do this is to create a user defined package, collect all these type and function definitions in that package and include it in every new module and design that requires them. Such a package is a design in itself, it is worthwhile coding and especially testing it properly. You do not want to have to debug it years from now or discover later on you wish you had done things differently.

 

Leaving Kansas is not as easy as it seems, you cannot do it in one big jump, it is a long journey of many steps and we might encounter some delays and roadblocks on the way. The shortest route is not necessarily the easiest or fastest one in the long run. Complex generic data types are just the first small step in the right direction. In future posts we will revisit this idea of writing generic reusable VHDL code again and again.

 

Back to the top: The Art of FPGA Design

  • Sign in to reply

Top Comments

  • DAB
    DAB over 7 years ago +1
    I think it would help the reader is you included some pictures of the overall circuit that you are implementing here. I understand the various data types, but being able to see the circuit would help me…
  • fpgaguru
    fpgaguru over 7 years ago in reply to DAB +1
    Well, there is no circuit yet, I started by just defining the basic building blocks, generic and parameterizable types in this case. In the next posts I will get into actual design examples and I will…
  • DAB
    DAB over 7 years ago in reply to fpgaguru +1
    Agreed, but people can get a better idea of WHY this is important if you walk them through a small example showing them how easy it is to avoid potential problems with a real circuit. Pictures can tell…
Parents
  • DAB
    DAB over 7 years ago

    I think it would help the reader is you included some pictures of the overall circuit that you are implementing here.

    I understand the various data types, but being able to see the circuit would help me understand why there is an issue here with respect to VHDL.

     

    DAB

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • fpgaguru
    fpgaguru over 7 years ago in reply to DAB

    Well, there is no circuit yet, I started by just defining the basic building blocks, generic and parameterizable types in this case. In the next posts I will get into actual design examples and I will try to include both full VHDL code, not just snippets, as well as circuit diagrams where appropriate. The point I am trying to make with this post is that most VHDL designers tend to stick to STD_LOGIC and STD_LOGIC_VECTOR types in their designs and while there is nothing fundamentally wrong with that, it limits your coding style to a very low level, making it harder to achieve higher levels of abstraction and generality.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
Comment
  • fpgaguru
    fpgaguru over 7 years ago in reply to DAB

    Well, there is no circuit yet, I started by just defining the basic building blocks, generic and parameterizable types in this case. In the next posts I will get into actual design examples and I will try to include both full VHDL code, not just snippets, as well as circuit diagrams where appropriate. The point I am trying to make with this post is that most VHDL designers tend to stick to STD_LOGIC and STD_LOGIC_VECTOR types in their designs and while there is nothing fundamentally wrong with that, it limits your coding style to a very low level, making it harder to achieve higher levels of abstraction and generality.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
Children
  • DAB
    DAB over 7 years ago in reply to fpgaguru

    Agreed, but people can get a better idea of WHY this is important if you walk them through a small example showing them how easy it is to avoid potential problems with a real circuit.

    Pictures can tell a story of a thousand words. Just make sure the reader gets the right thousand words you want them to read. image

     

    DAB

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube