VHDL in Alliance – A Different Start!


Welcome back! :) Its time to start off with Digital Design in VHDL using the Alliance package available in Fedora Electronic Lab. Now most tutorials would start of with the simulator itself. I have however, chosen a different start – The stimulus. The reason for this it that we need a stimulus for every simulation that we run in future tutorials and Alliance uses a separate file format for this.

Now you could write it yourself and it looks something like this:

--port list
--inputs
in I3 B;;;
in I2 B;;;
in I1 B;;;
in I0 B;;;
in En B;;;
in Sel (1 downto 0) B;;;
--output
out Y B;;;

begin

<   0ns> : 0 0 0 0 0 00 ?* ;
< +50ns> : 0 0 0 1 1 00 ?* ;
< +50ns> : 1 1 1 0 1 00 ?* ;
< +50ns> : 0 0 1 0 1 01 ?* ;
< +50ns> : 1 1 0 1 1 01 ?* ;
< +50ns> : 0 1 0 0 1 10 ?* ;
< +50ns> : 1 0 1 1 1 10 ?* ;
< +50ns> : 1 0 0 0 1 11 ?* ;
< +50ns> : 0 1 1 1 1 11 ?* ;
< +50ns> : 0 0 0 0 0 00 ?* ;

end;

Simple? Well the format is actually simple if you look through the man page (run ‘man genpat’ (without quotes) from the terminal). This was the stimulus I wrote for a 4×1 multiplexer. The list of zeros and ones are the values of inputs. Since this was a simple circuit, it needed just a few sets of inputs for verification. Now imagine a larger design. Writing this table is not only hard but impractical. Well Alliance has a program that will generate the stimulus for you. This tutorial will teach you how to start using it. The program is called genpat. Do read the man page once before proceeding further by running ‘man genpat’. genpat lets you write a simple procedural program to generate those patterns you just saw (Now that I mentioned it, the file is called a ‘patterns file’). If you read through the man page, you would find separate manuals for each of the functions available in genpat. I will explain them a bit here so that we can get started with genpat quickly and we’ll cover new stuff as and when we come across them in later tutorials. Now a genpat program has a structure like this:

/*You will always need this one                     */
#include "genpat.h"

/*Other headers such as stdio.h                     */ 

/*You will almost always need this function as well */
/*Taken from genpat man page                        */
/*Functions look like the original K&R C functions  */
char *inttostr(entier)
int entier;
{
  char *str;
  str = (char *) mbkalloc (32 * sizeof (char));
  sprintf (str, "%d",entier);
  return(str);
}

/*Other functions you may need*/

/*The main function*/
main()
{
  /*Define the variables you may need. loop counters*/
  /*for example                                     */

  /*Name your pattern using DEF_GENPAT (check out   */
  /*man DEF_GENPAT)                                 */

  /*Define the interface, i.e., the pins of the     */
  /*entity in your VHDL code (check out man DECLAR) */

  /*Specify the values you want for each of the     */
  /*input pins using some procedural statements and */
  /*AFFECT (check out man AFFECT)                   */

  /*Finally save the pattern                        */
  SAV_GENPAT();

}

I would suggest saving this for later reference. Next we will look at a simple genpat program. After this we will break each section down and understand what it does. Lets create a stimulus to test a JK Flip Flop. First we need to know what the interface will be:

entity jk_ff is
(
  clk  : in  std_logic;
  J    : in  std_logic;
  K    : in  std_logic;
  Q    : out std_logic;
  Qbar : out std_logic;
)
end jk_ff;

And here is our genpat program (pretty long one!):

#include <stdio.h>
#include "genpat.h"

char *inttostr(entier)
int entier;
{
  char *str;
  str = (char*) mbkalloc(32 * sizeof (char));
  sprintf(str, "%d", entier);
  return(str);
}

main()
{
  int temp_clk, temp_J, temp_K, time_tick, counter;
  int i;

  DEF_GENPAT("jk_ff");

  DECLAR("clk", ":2", "B", IN, "", "");
  DECLAR("J", ":2", "B", IN, "", "");
  DECLAR("K", ":2", "B", IN, "", "");
  DECLAR("Q", ":2", "B", OUT, "", "");
  DECLAR("Qbar", ":2", "B", OUT, "", "");

  temp_clk = 0;
  time_tick = 0;
  counter = 1;
  temp_J = 1;
  temp_K = 0;

  SETTUNIT("ps");
  for(i = 0; i < 16; i++)
  {
    AFFECT(inttostr(time_tick), "clk", inttostr(temp_clk));
    AFFECT(inttostr(time_tick), "J", inttostr(temp_J));
    AFFECT(inttostr(time_tick), "K", inttostr(temp_K));

    if(temp_clk == 0)
      temp_clk = 1;
    else
      temp_clk = 0;
    if(counter%2 == 0)
    {
      if(temp_J == 0)
        temp_J = 1;
      else
        temp_J = 0;
    }
    if(counter%4 == 0)
    {
      if(temp_K == 0)
        temp_K = 1;
      else
        temp_K = 0;
    }

    time_tick = time_tick + 10;
    counter++;
  }
  SAV_GENPAT();

}

Now lets take this code bit by bit (not literally! ;)). The #include section and the inttostr function should simple enough to understand. Lets look at the main function. It begins with a few variables declared. The names explain their purpose. Right after that is our first genpat function:

DEF_GENPAT(“jk_ff”);

This functions is required to specify the name of the output file. Note that there is no extension added. “jk_ff” is all that genpat needs. It will add the extension to the filename when it generates the patterns (All of the programs in the Alliance package which I have seen so far, use only the filenames. No extension.). We will look at this later.

After that we have several DECLAR statements. Here is the format for a DECLAR:

Most of the explanation should be simple to understand. Ignore “:nb_space” and option for now. We’ll look at spaces and option in a later tutorial. Also, format refers to how the signals will be shown on the waveform viewer. Now looking back at the program, here are our signals.

clk, J and K: have format binary and mode IN.
Q and Qbar: have format binary and mode OUT.

Since all these are scalars, we have left the size field blank.

After this there is the SETTUNIT function. Clearly this sets the time unit and we have selected picoseconds.

The rest of the program is a for loop which should be simple enough to follow. It basically toggles the clock and the J and K inputs, How you do this is up to you. However, the clock must obviously toggle in each iteration. What is new here are the AFFECT statements. Here is its format:

The time unit for pattern_date is set using SETTUNIT. Finally, after we have made sure we get all permutations we need, we save the pattern using the SAV_GENPAT() function. Save this to jk_ff.c and run the following command:

genpat jk_ff

Notice that no file extension has been specified. genpat will generate a file called jk_ff.pat for you. Open it. This is what it should contain:

-- description generated by Pat driver

--			date     : Sat Feb  5 00:37:59 2011
--			revision : v109

--			sequence : jk_ff

-- input / output list :
in       clk B;;;
in       j B;;;
in       k B;;;
out      q B;;;
out      qbar B;;;

begin

-- Pattern description :

--                        c  j  k   q   q
--                        l             b
--                        k             a
--                                      r   

-- Beware : unprocessed patterns

<          0 ps>        : 0  0  0  ?*  ?*  ;
<         10 ps>        : 1  0  0  ?*  ?*  ;
<         20 ps>        : 0  0  0  ?*  ?*  ;
<         30 ps>        : 1  0  0  ?*  ?*  ;
<         40 ps>        : 0  1  1  ?*  ?*  ;
<         50 ps>        : 1  1  1  ?*  ?*  ;
<         60 ps>        : 0  0  1  ?*  ?*  ;
<         70 ps>        : 1  0  1  ?*  ?*  ;
<         80 ps>        : 0  1  0  ?*  ?*  ;
<         90 ps>        : 1  1  0  ?*  ?*  ;
<        100 ps>        : 0  0  0  ?*  ?*  ;
<        110 ps>        : 1  0  0  ?*  ?*  ;
<        120 ps>        : 0  1  1  ?*  ?*  ;
<        130 ps>        : 1  1  1  ?*  ?*  ;
<        140 ps>        : 0  0  1  ?*  ?*  ;
<        150 ps>        : 1  0  1  ?*  ?*  ;

end;

Pretty small for the amount of code we wrote! :( Well the use of genpat will become apparent when we look at better examples. But I’m sure you’ll agree that it isn’t all that simple to write it all so neatly. Finally lets have a look at the waveforms. In the terminal, run xpat. xpat is Alliance’s patterns viewer. In xpat, go to file -> open. Select jk_ff.pat and click OK. This is what you should see:

Notice Q and Qbar are just pink blocks? This is because they don’t have any values yet. Once we run a simulation, Q and Qbar will get their values from the results and then we will see them correctly. We will look at simulations in the next tutorial.

So thats basically how we generate a stimulus in Alliance. I am however more comfortable in writing a testbench like we do in Verilog. What do you think? Please do tell me through a comment. Also let me know if I have been unclear or incorrect anywhere. Stay tuned for my next tutorial on compiling and simulating VHDL code. Thanks for reading! :)

About these ads

One Response to VHDL in Alliance – A Different Start!

  1. Pingback: Tweets that mention VHDL in Alliance – A Different Start! « Ashwith -- Topsy.com

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 71 other followers

%d bloggers like this: