Title: Simulation 9'Simulation
1Simulation9.Simulation
2Advanced Topics
- To look at a number of more advanced topics in
the application of Handel-C - Interface buses and ports
- User-defined interfaces
- Using FPGA library components
- EDIF bus formats and properties
- Using multiple clocks
- Co-simulation
3Simulation
- Using the "Netlist" Simulator
- If you need to use the old "netlist" simulator,
you need to do two things. - Select the option Use netlist simulator on the
Linker tab of the ProjectSettings dialog. - Change the Simulator DLL command line on the
same tab. - The Simulator DLL command line will depend on
which C or C compiler you are using. The slide
shows the command line for Microsoft Visual C.
The command lines for GCC and Borland C, which
are the other compilers supported by DK1, are
different. Details can be found in the DK1 user
manual or in the online help. (Look for "Default
simulation command lines".)
4Using the "Netlist" Simulator
- Go to "Project Settings", select the "Linker"
tab, and check the box marked "Use netlist
simulator - Change the Simulator DLL command line
- cl /LD /Oitybl /Gs /nologo /I"InstallDir\sim\inclu
de"/Tc"l" /Fe"2" "InstallDir\dkl\sim\lib\numlib
.lib"/link /nodefaultliblibc.lib
5Simulation
- "Fast" and "Netlist" Simulators
- The DK1 Design Suite includes two simulators. The
default one is the "fast" simulator, which was
introduced in DK 1.1. The other one is called the
"netlist" simulator. You would only need to use
the netlist simulator if you were using a
simulator plugin that reads or writes data faster
than the system clock. It is about 100 times
slower than the fast simulator. - The fast simulator supports the ability to call C
and C functions directly from Handel-C
functions and allows a standalone executable to
be created - think of the executable as a
cross-compiled version of the Handel-C code.
6"Fast" and "Netlist" Simulators
- A new "fast" simulator was introduced in DK1.1
- Cycle accurate
- Up to 100 times faster than the "netlist"
simulator - Standalone simulation executables can now be
built - Use of inline C/C can allow user interaction
- The original DK1 "netlist" simulator can still be
used - Required for asynchronous I/O paths between
plugins which produce and read data faster than
the Handel-C clock
7Simulation
- Handel-C File Name Extensions
- This table shows the file extensions for Handel-C
files. The default extension changed with the
introduction of DK 1.1 to support the inclusion
of C and C source code in a Handel-C project.
Previously, Handel-C files used the extension .c. - Backwards compatibility is provided in DK 1.1
because you can mark a .c file as a Handel-C file.
8Handel-C File Name Extensions
- File name extensions changed with the
introduction of DK 1.1
9Simulation
- DK 1.1 File Name Extensions
- This explains the rationale for the file
extension changes in DK 1.1.
10DK 1.1 File Name Extensions
- New file name extensions are required with DK 1.1
- Why? - Because DK 1.1 can include Handel-C, C and
C files - Different file types are recognized by their
extensions - Backward compatibility
- New files will use the new extensions
- Language type can be selected for existing files,
overriding the default extension
11Simulation
- Selecting Source Language Type
- The language of a source file is specified when
the file is created or when an existing file is
added to a project. The DK 1.1 software
automatically adds the proper file extension when
a new file is created. You shouldn't include the
file extension in the dialog, otherwise the file
name may end up as "mycppfile.cpp.cpp" or similar.
12Selecting Source Language Type
- Source language can be specified at file creation
13Simulation
- Selecting Source Language Type
- When adding an existing file, make sure that the
correct language is selected in the dialog box.
For example, the extensions .c and .h might
indicate an ISO-C file or they might indicate a
pre-DK 1.1 Handel-C file.
14Selecting Source Language Type
- Source language is specified when adding an
existing file - Language is selected according to filter in
dialog box
15Simulation
- Changing the Source Language Type
- If you get the source type wrong, all is not
lost. You can change the language using the File
properties dialog. To get this, select the file
in the File View pane. Then select menu
ViewgtProperties (or right-click on the file name
and select Properties from the context menu).
16Changing the Source Language Type
- Source file language can also be changed later
- Select the file in the File View pane
- Select "Properties" from the "View" menu
17Simulation
- Calling C/C Functions from Handel-C
- We have already seen that it is possible to call
C and C library functions from a Handel-C
function by declaring them extern "C"or extern
"C" respectively. It is also possible to call
user-defined C and C functions in the same way.
Conversely, Handel-C functions can be called from
C or C functions. - A call to a C or C function takes no clock
cycles. If several are called in sequence, the
calls should be separated by delay statements. - Handel-C's variable width integer types can be
passed to C functions using the C Wide Number
Library, which is supplied with the DK1 software. - Including user-defined C or C code in a
Handel-C project requires a "custom build" step
for each file - DK1 needs to be told how to
compile these files - this will be discussed in
the following slides. - The ability to mix Handel-C with C and C is
very useful for porting existing C or C code to
Handel-C and when creating tests for Handel-C
models. C and C functions are only meaningful
in simulation - hardware must be described purely
in Handel-C.
18Calling C/C Functions from Calling Calling
C/C Functions from Handel-C
- C/C library and user-defined functions can be
called fromHandel-C and vice versa - Useful when porting C/C to Handel-C
- Useful for creating test benches in C/C
- Only meaningful in simulation
- Take no clock cycles to run
- Supported by C Wide Number Library (hcnum.h)
- User-defined C/C functions require "Custom
build steps"
19Simulation
- Calling User-defined C/C Functions
- Here is an example showing some C functions
being called from a Handel-C model. The C
functions are declared extern "C". Notice the
use of Handel-C data types (e.g. unsigned 12) for
the C functions' arguments.
20Calling User-defined C/C Calling Calling
User-defined C/C Functions
extern "C" void get_data (unsigned 12
data, unsigned 12 data_copy) void
software_algorithm (unsigned 12 data) void
compare_results (unsigned 12 datal, unsigned 12
data2) extern void hardware_algorithm(
unsigned 12 data) set clock external void
main (void) unsigned 12 data 8, data_copy
8 software_algorithm(data)
hardware_algorithm(data_copy)
compare_results (data, data_copy)
21Sequential and Parallel Statements
- Handel-C to C/C Type Conversions
- If a Handel-C data type is to be passed to a C
function, the C Wide Number Library must be
used. This requires the header file hcnum.h,
which includes definitions in the namespace
HCNum. - This header file includes class template
definitions for Into and Ulntltgt These correspond
to Handel-C's int and unsigned types
respectively. The template parameter gives the
size of the Handel-C variable. In the example,
Ulntlt12gt corresponds to Handel-C's unsigned 12. - These class templates include several member
functions. The most useful are probably UlntOff),
IntOfQ and PrintStringQ which convert Ulnt and
Int to the standard C 32-bit unsigned and
signed integer types and null-terminated string
respectively. This is necessary when using
standard library functions, such as printf, as
there is no automatic type conversion in C. An
example using PrintStringQ will be given shortly. - For full details of the wide number library, see
the documentation supplied with DK1.
22Handel-C to C/C Type Conversions
23Simulation
- Handel-C to C/C Type Conversions
- This table shows what type conversions are needed
between C/C and Handel-C. The template classes
Hint and Int are only available in C, so
function arguments with non-standard widths must
be written using C, not C.
24Handel-C to C/C Type Conversions
Â
Â
Â
Â
Â
Â
Â
Â
Â
Â
Â
Â
C only
25Simulation
- Printing Wide Numbers
- Earlier in the course, we saw that the standard C
library function printf could be called directly
from Handel-C, but that it may not work for
integer values greater than 32 bits wide or for
signed numbers less than 32 bits wide. One way to
print these values is to write a function in C
that uses the wide number library to convert the
values to a type that can be written using printf
or a C output stream. - In the example, we want to print the value of a
33-bit wide unsigned integer. We shall write a
C function, print_output to do this. The
Handel-C declaration for the C function
print_output is shown opposite.
26Printing Wide Numbers
- You can't call printf directly from Handel-C for
wide numbers - Use a C function instead
27Simulation
- C Code for Printing Wide Numbers
- Here is the C definition of the function
print_output. The Handel-C declaration is also
repeated here for reference. - The C function uses the template class Ulnt
from the wide number library. Ulntlt33gt
corresponds to unsigned 33 in Handel-C. The class
Ulnt has a member function, PrintString, which
converts the value of a Ulnt to a string and
writes it to a char array as a standard C
zero-terminated string. PrintString has three
parameters the char buffer, the size of the
buffer and the base to be used for conversion.
The base can be 2, 8, 10 or 16 (i.e. the value
can be converted to a binary, octal, decimal or
hex string). PrintString returns the number of
characters written, including the terminating 0.
The buffer can then be printed using printfln the
usual way. - buflen is declared to be of type uint32. This is
a type declared in the wide number library which
is guaranteed to be 32 bits long (i.e. it's a
like a C long, but with a portably defined width).
28C Code for Printing Wide Numbers
29Simulation
- Custom Build Steps
- If a file other than a Handel-C file is to be
included in a project, a "custom build step" is
required for the file. This will be the case when
C/C functions are used in a Handel-C project,
but is not limited to these file types. Any file
type can be included in a DK1 project, as long as
a suitable custom build step is defined. For
example a VHDL or Verilog file could be included,
and compiled to EDIF using a third-party
synthesis tool. (VHDL and Verilog are widely used
hardware description languages.) Custom build
steps can also be used to run place and route
tools and even download the resulting bit file to
a development board. In this case, the project
itself will need a custom build step. - The custom build settings are specified in the
Project Settings dialog, and must be re-specified
for each separate file (except for Handel-C
files).
30Custom Build Steps
- Allows other language source files to be included
and built in aDK1 project - E.g. C/C, VHDL, Verilog
- Usage
- Custom build steps are specified in "Project
Settings" dialog - Build commands and output targets must be
specifiedseparately for each source file - Any custom build command can be issued
- E.g. PAR, download to board, clean up files
31Simulation
- Custom Build Steps
- This shows how to specify the custom build
settings for a C or C file. Remember that the
following instructions must be repeated for each
separate C or C file in the project. - In the Project Settings dialog, make sure that
the C/C file is selected and go to the Build
commands tab. - Select Commands from the View drop-down list and
add the build command. For C or C this will be
the command-line for the C or C compiler you
are using, e.g. Microsoft Visual C. You can use
the variable (lnputName) for the file name. This
variable does not include the file extension. For
Microsoft Visual C the command line is this, - cl /I C\Progral\Celoxica\DKl\Sim\Include /c
- (InputName).cpp /Fo(TargetDir)\(InputName).ob
j - Now select Output from the View drop-down list
and add the name of the output file from the
build command. This should include the directory
(TargetDir). For example, - (TargetDir)\(InputName).obj
- Please refer to the DK1 User Manual or online
help for further details, including the command
lines for GCC and Borland C.
32Custom Build Steps
33Simulation
- Custom Build Steps
- Once custom build commands have been added for
all the non Handel-C files, you must edit the
Linker settings. This is to tell the linker to
link the object files that the custom build steps
have generated. Enter the name(s) of the object
file(s) in the Additional C/C Modules field. If
there is more than one, the names should be
separated by commas.
34Custom Build Steps
35Simulation
- Calling Handel-C from C
- As well as calling C or C functions from
Handel-C, it is also possible to have a C
program that calls Handel-C functions. The
Handel-C functions must be declared extern "C"
as shown. - You can't simulate such a project in DK1, instead
you must create an executable in DK1 and run it
separately. This is described on the next page.
36Calling Handel-C from C
37Simulation
- Building an Executable (.exe)
- To build an executable, it is a good idea to
create a new configuration first. Use menu
BuildgtConfigurations and select the Add... button
from the Configurations dialog. In the Add
Configuration dialog, type a name for the new
configuration, e.g. Executable. Select Copy
settings from Debug from the list of existing
configurations - In the Project Settings for the new configuration
edit the Linker settings so that an executable is
created instead of a DLL. The example shows how
to do this with Microsoft Visual C. This uses
the following command line, - Cl/G2/nologo/I" C\Progral\Celoxica\DKl\Sim\Incl
ude Tpl"/Fe"3".exe"4" - You will need to refer to the online help for
DK1, the DK1 User Guide or the documentation for
your C compiler for more details.
38Building an Executable (.exe)
39Simulation
- Simulator Plugins
- The DK1 simulator allows you to use plugins. A
plugin is simply a Windows DLL that connects to a
Handel-C program via a clock or interface.
Plugins are usually written in C, but can be
written in any language that supports C calling
conventions, provided that a suitable compiler is
available. Some plugins are supplied with DK1 and
you can write your own using the supplied plugin
application programming interface (API).
40Simulator Plugins
- A plugin is a (Windows) program that connects to
a Handel-Cclock or interface - Plugins can be written in any language that
supports C callingconventions - A plugin API is provided with DK1
41Simulation
- Plugins Provided with DK1
- These plugins are supplied with DK1.
42Plugins Provided with DK1
- DK1Share.dll
- Allows a port to be used by more than one plugin
- DK1Sync.dll
- Synchronises DK1 simulations - used to simulate
designswith multiple clocks also used for
waveforms - DK1Connect.dll
- Connects two simulations together - used to
simulate twodesigns together also used for
waveforms - 7segment.dll
- Provides a seven-segment decoder window
duringsimulation (simulates a seven segment
display)
43Simulation
- Example - Using 7segment.dll
- As an example of a simulator plugin we are going
to use the 7segment.dll plugin. This provides a
simulation model of a standard seven segment
display that can be driven from a Handel-C
simulation. - A seven segment display has seven inputs - one
for each display segment. Many commercial
displays have an eighth segment, which is a dot
at the bottom right. To display a 4-bit binary
value as a hexadecimal digit, a seven segment
encoder is needed. This is a combinational
function that "knows" which segments are needed
for each hexadecimal digit. One possible
implementation of this uses an array acting as a
small ROM, with the encoded values being stored
at the sixteen array locations. In Handel-C,
variables must be declared static to be
initialised and declared at the same time, as
here.
44Example - Using 7segment.dll
45Simulation
- Interfacing to 7segment.dll
- Our Handel-C model is connected to the seven
segment display plugin using an interface. The
interface definition includes the name of the
plugin DLL, a user-defined instance name for the
display and the name of the plugin function to
use. This is described in the documentation. - In the simulation, whenever a value is written to
the interface, the plugin responds by displaying
the value.
46Interfacing to 7segment.dll
47Simulation
- Simulating with 7segment.dll
- This shows what the simulation looks like for a
design that has two seven segment displays.
48Simulating with 7segment.dll