Trung tâm đào tạo thiết kế vi mạch Semicon


  • ĐĂNG KÝ TÀI KHOẢN ĐỂ TRUY CẬP NHIỀU TÀI LIỆU HƠN!
  • Create an account
    *
    *
    *
    *
    *
    Fields marked with an asterisk (*) are required.
semicon_lab.jpg

Interfacing with C Part-I

E-mail Print PDF

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

  • 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.
Mapping e Types to C Types
Declaration of e types in C is done with the following
  • SN_TYPE
  • SN_LIST
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:

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

.

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 )  

Related Articles

Chat Zalo