Declarations

Arrays

It is possible to declare arrays of variables in the same way that arrays are declared in conventional C. For example:

int 6 x[7];
unsigned int 6 x[4][5][6];

When accessing an array, the index must be a compile time constant. If you require random access to an array of values, consider using a ram or rom as described below.

See also: RAMS and ROMS

 

Constants

Constants may be used within expressions where required. Decimal constants are written as the number alone. Hexadecimal constants must be prefixed with 0x or 0X, octal constants with 0 (a zero) and binary constants with 0b or 0B. For example:


w = 1234;       /* Decimal */
x = 0x1234;     /* Hexadecimal */
y = 01234;      /* Octal */
z = 0b00100110; /* Binary */

 

Channels

Handel-C provides channels for communicating between parallel branches of code. One branch writes to a channel and a second branch reads from it. The communication only occurs when both tasks are ready, then one item of data is transferred between the two branches.

chan int 7 x;     // Channel width 7bits called x
chan int 3 y[4];  // Array of channels
chanin int 3 in;  // input channel for simulation
chanout int 3 out;// output channel for simulation

in ? v;           // input from channel in to variable v
y[3] ! 2;         // output 2 to channel y[3]

As with arrays, indexes to channel arrays must be compile time constants.

See also: prialt

 

RAMs and ROMs

RAMs and ROMs may be built from the logic provided in the FPGA using the ram and rom keywords. This example constructs a RAM consisting of 43 entries each of which is 6 bits wide and a ROM consisting of 4 entries each of which is 16 bits wide. The ROM is initialised with the constants given in the following list in the same way as an array isinitialised in C. For example:

ram int 6 a[43];
rom int 16 b[4] = { 23, 46, 69, 92 };

x = b[3]; //x is given the value 92

Note that RAMs differ from arrays in that an array is equivalent to declaring a number of variables. Each entry in an array may be used exactly like an individual variable with as many reads and writes in a clock cycle as required. RAMs, however, are normally more efficient to implement in terms of hardware resources than arrays and also allow a non-constant index. Therefore, you should use an array when you wish to access the elements in parallel and you should use a RAM when you wish to have random access to the elements.

See also: arrays

 

Variables

There is only one fundamental type for variables: int. In addition, the int type may be qualified with the unsigned keyword to indicate that the variable only contains positive integers. For example:

int width wire_name;
int 5 x;
unsigned int 13 y;
signed int 17 x, y, z;
int 15 a = 1234;
int undefined b;
short c;
long d;
char e;

char 8 bits
short 16 bits
long 32 bits
undefined undeclared width, will be infered

 

See also: Type casting