User manual

DSM tutorials
Swapping variable values
The swapexample project in the TutorialHCBasics workspace shows how the values of two variables can be
exchanged in a single clock cycle without using an intermediate location to store the contents of one of
them. This is possible because a variable in Handel-C does not take on the value assigned to it until the
end of a clock cycle. By assigning the value of each variable to the other in parallel, the contents are
swapped in a single cycle, without any intermediate storage. The source code shown below assigns
values to the variables and swaps them back and forth, displaying their values on the two 7-segment
displays.
The main point of this demonstration is that it is impossible to achieve this behaviour using C with a
conventional compiler.
/*
* Initialise x and y
*/
par
{
x = 3;
y = 7;
}
while (1)
{
/*
* Swap x and y in a single cycle
*/
par
{
x = y;
y = x;
}
/*
* Write x and y to displays
*/
par
{
PalSevenSegWriteDigit (PalSevenSegCT (0), x, 0);
PalSevenSegWriteDigit (PalSevenSegCT (1), y, 0);
}
}
4.1.2 Channel communications
Channels are used for communication between separate processes. They can be used for
synchronization and/or to pass data. Synchronization is enforced because the write to and read from the
channel must take place in the same clock cycle. This means that a process writing to a channel must
wait until data has been read from the channel before proceeding, and a process reading from a channel
must wait for the data to be written.
The
channelexample project in the TutorialHCBasics workspace uses two processes, one counting a
seven-segment display in hexadecimal, and the other circling a lit segment, as shown below.
www.celoxica.com
Page 47