Interfacing with C Part-I

Print

Introduction to C Interface

There are times when one wants to interface with C code, Like if you had modeled a algo in C, later if you want same code in e for checking if your DUT is working as per the algo. e language provides mechanism to interface with c language, it is called c interface.

 It provides following features

Mapping e Types to C Types
Declaration of e types in C is done with the following
SN_TYPE
Declares an e type in C. Use this macro whenever an e enum or struct must be declared in C.

The following types are mapped directly to C types and can be used without the SN_TYPE macro:

·         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 }
Creating New Objects in C
You can create e objects in C with the SN_STRUCT_NEW, SN_LIST_NEW and SN_STRING_NEW. 

There are many ways to import C functions into e as listed below.

.

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

.

Operation

Commands

Create c object file

gcc -c -o c_interface1.o c_interface1.c

Link c to e

sn_compile.sh -l c_interface1.o c_interface1.e -o c_interface1.run

Simulate

c_interface1.run -c "test"

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úng

Hãy Để Chúng Tôi Hỗ Trợ Cho Bạn. SEMICON  

 

Last Updated ( Sunday, 08 May 2022 23:20 )