
Introduction to C Interface
It provides following features
- You can pass data between e modules and C modules.
- You can use e data types in C code.
- You can call e methods and manipulate e data from C code.
- You can access any global data under sys in C code.
- You can call C routines from e code, even if that C routine has no knowledge of e.Working with C language require data to be passed in both direction. i.e. from C to E, and E to C. While passing data types which are complex in nature like struct, byte so on, we need to use special data types. We will see both of this in detail below.
- SN_TYPE
- SN_LIST
The following types are mapped directly to C types and can be used without the SN_TYPE macro: |
- SN_TYPE(int) => int.
- SN_TYPE(uint) => uint.
- SN_TYPE(bool) => bool.
- SN_TYPE(string) => char *.
· 1 #include
· 2 #include "c_interface.h"
· 3
· 4 SN_TYPE(byte) c_interface (
· 5 SN_TYPE(string) name,
· 6 SN_TYPE(packet) packet1
· 7 ) {
· 8 SN_TYPE(byte) data;
· 9 // Do something here
· 10 return data;
· 11 }
SN_LIST
Declares an e list type in C. This macro is used whenever an e list must be declared in C. |
. |
1 #include
2
3 SN_TYPE(bool) c_interface_list (
4 SN_TYPE(string) list_of_names,
5 SN_TYPE(packet) packet1
6 ) {
7 // Do something here
8 }
- SN_STRUCT_NEW : This macro returns a new, initialized struct of that type. All fields of the struct, including lists, are initialized as in e.
- SN_LIST_NEW : The macro returns a new, initialized, empty list of that type.
- SN_STRING_NEW : The macro allocates a new string (according to the size that was specified in the parameter) and returns a pointer to the new string.Importing/Exporting to C
There are many ways to import C functions into e as listed below. |
. |
- You can declare a global e routine to be implemented as a C routine.
- You can declare a local e method to be implemented as a C routine.
- You can declare a local e method to be implemented as a dynamic C routine or foreign dynamic C routine.
routine e-routine-name(param,...) [:result-type] | |
[is C routine c-routine-name] | |
In this case, you can call the C routine directly from anywhere in your e code. The syntax for declaring a C routine as a global routine is: | |
routine e-routine-name(param,...) [:result-type] | |
[is C routine c-routine-name] | |
This syntax is a statement. This statement must be compiled and statically linked with Specman Elite and the compiled C code. (The e code that calls the specified routine can be either loaded or compiled.) | |
Example-Global routine | . |
1 #include
2
3 void c_interface1 () {
4 printf("Hello World\n");
5 }
Below is e code which uses the above c function. |
1 <'
2 extend sys {
3 c_hello() is C routine c_interface1;
4
5 run() is also {
6 c_hello();
7 };
8 };
9 '>
Compiling the example | ||||||||
.
Bạn Có Đam Mê Với Vi Mạch hay Nhúng - Bạn Muốn Trau Dồi Thêm Kĩ Năng
Mong Muốn Có Thêm Cơ Hội Trong Công Việc Và Trở Thành Một Người Có Giá Trị Hơn Bạn Chưa Biết Phương Thức Nào Nhanh Chóng Để Đạt Được ChúngHãy Để Chúng Tôi Hỗ Trợ Cho Bạn. SEMICON |