diff --git a/simics-workspace/modules/afdxES/Makefile b/simics-workspace/modules/afdxES/Makefile
index 9fb6686..5ee442d 100644
--- a/simics-workspace/modules/afdxES/Makefile
+++ b/simics-workspace/modules/afdxES/Makefile
@@ -5,7 +5,7 @@
 #

 MODULE_CLASSES = afdxES
-SRC_FILES = tomiddleware.c communicate-with-qt.c untils.c afdxES_runtime_info.c mib.c ima-data-exchange.c
+SRC_FILES = tomiddleware.c communicate-with-qt.c untils.c afdxES_runtime_info.c mib.c scence3-ima-data-exchange.c
 DML_FILES = afdxES.dml
 DMLC_FLAGS = -m -I$(SIMICS_WORKSPACE)/include

diff --git a/simics-workspace/modules/afdxES/afdxES.dml b/simics-workspace/modules/afdxES/afdxES.dml
index 9b2142b..e01c65f 100644
--- a/simics-workspace/modules/afdxES/afdxES.dml
+++ b/simics-workspace/modules/afdxES/afdxES.dml
@@ -21,462 +21,7 @@ import "pci-register.dml";
 import "change-endian.dml";
 import "tomiddleware.dml";
 import "rm-handler.dml";
-
-extern int qt_socket_init(void);
-extern int qt_socket_recv(char *recvbuf, int size);
-extern int qt_socket_send(char *sendbuf, int size);
-extern int Strlen(char *str);
-extern int netStrlen(char *str);
-extern int Ntohl(int data);
-extern int Htonl(int data);
-
-extern void set_afdxES_module_runtime_header(void *p);
-extern void set_afdxES_module_runtime_name(void *p);
-extern void set_afdxES_module_runtime_description(void *p, int flag);
-extern void free_afdxES_module_runtime_header(void *p);
-extern void free_afdxES_module_runtime_name(void *p);
-extern void free_afdxES_module_runtime_description(void *p);
-
-extern void set_main_board_module_runtime_header(void *p);
-extern void set_main_board_module_runtime_name(void *p);
-extern void set_main_board_module_runtime_description(void *p, int flag);
-extern void free_main_board_module_runtime_header(void *p);
-extern void free_main_board_module_runtime_name(void *p);
-extern void free_main_board_module_runtime_description(void *p);
-
-extern void set_vxworks_runtime_header(void *p);
-extern void set_vxworks_runtime_name(void *p);
-extern void set_vxworks_runtime_description(void *p, int flag);
-extern void free_vxworks_runtime_header(void *p);
-extern void free_vxworks_runtime_name(void *p);
-extern void free_vxworks_runtime_description(void *p);
-
-extern void set_afdxES2_mib_runtime_header(void *p);
-extern void free_afdxES2_mib_runtime_header(void *p);
-
-extern void set_a664_driver_middleware_runtime_header(void *p);
-extern void set_a664_driver_middleware_runtime_name(void *p);
-extern void set_a664_driver_middleware_runtime_description(void *p, int flag);
-extern void free_a664_driver_middleware_runtime_header(void *p);
-extern void free_a664_driver_middleware_runtime_name(void *p);
-extern void free_a664_driver_middleware_runtime_description(void *p);
-
-extern void set_a664_middleware_runtime_header(void *p);
-extern void set_a664_middleware_runtime_name(void *p);
-extern void set_a664_middleware_runtime_description(void *p, int flag);
-extern void free_a664_middleware_runtime_header(void *p);
-extern void free_a664_middleware_runtime_name(void *p);
-extern void free_a664_middleware_runtime_description(void *p);
-
-extern void set_a664_card_runtime_header(void *p);
-extern void set_a664_card_runtime_name(void *p);
-extern void set_a664_card_runtime_description(void *p, int flag);
-extern void free_a664_card_runtime_header(void *p);
-extern void free_a664_card_runtime_name(void *p);
-extern void free_a664_card_runtime_description(void *p);
-
-extern int ima_server_socket_init(int portNum);
-extern int ima_client_socket_init(int portNum);
-extern void *get_ima_client_socket(void);
-extern int ima_socket_recv(void *recvbuf, int size, void *socketfd);
-extern int get_recv_state(void);
-extern void change_recv_state(void);
-
-connect phyA {
-	parameter documentation = "Send data to the second ARINC664 card";
-    parameter configuration = "optional";
-
-    interface ieee_802_3_mac {
-        parameter c_type = "ieee_802_3_mac_interface_t";
-    }
-}
-
-connect phyB {
-	parameter documentation = "Send data to the second ARINC664 card";
-    parameter configuration = "optional";
-
-    interface ieee_802_3_mac {
-        parameter c_type = "ieee_802_3_mac_interface_t";
-    }
-}
-
-method recv_frame(uint8 *data, uint32 size) {
-    local rm_cfg_cam item;
-    local uint8 rm_index;
-    local bool is_crc_ok;
-    local RM_INFO rm_info;
-
-    if ($is_ieee_802_3_log_on) {
-        call $print_ieee_802_3_mac_begin();
-    }
-    call $recv_crc_check(data,size,$Table)->(is_crc_ok);
-
-    if (is_crc_ok == false) {
-        // log "info":"crc check failed";
-        return;
-    } else {
-        if ($is_ieee_802_3_log_on) {
-            call $print_ieee_802_3_mac_crc();
-        }
-    }
-
-    call $change_endian(data, size);
-
-    item.VL_ID = data[4];
-    call $BinarySearchRMCfgCamArray($rm_cfg_cam_array, item) -> (rm_index);
-    if (rm_index == -1) {
-        // log "error" : "Can't find this conf in current rm cfg cam table, please check!";
-    } else {
-        rm_index = $rm_cfg_cam_array[rm_index].address;
-    }
-
-    call $Init_RM_INFO(&rm_info, rm_index, size);
-
-    local uint8 interface_id = data[11] >> 5;
-    local uint8 frame_sn = data[size-5];
-    local bool is_rm_ok;
-
-    call $RM_Handler(&rm_info, interface_id, frame_sn) -> (is_rm_ok);
-    if (is_rm_ok) {
-        if ($is_ieee_802_3_log_on) {
-            call $print_ieee_802_3_mac_rm();
-        }
-        call $unpacket_frame(data, size);
-    } else {
-        // log "error" : "Send from unkown network";
-    }
-}
-
-implement ieee_802_3_mac {
-	method send_frame(uint8 *data, uint32 size) {
-        call $recv_frame(data, size);
-    }
-}
-
-implement transmit_runtime_info_between_afdxES_and_afdxES2 {
-    method transmit_runtime_info(Runtime_Info *runtime_info) {
-        call $Transmit_runtime_info(runtime_info);
-    }
-
-    method transmit_mib_info(MAC_MIB *maca_mib_info, MAC_MIB *macb_mib_info, RX_MIB *rx_mib_info, TX_MIB *tx_mib_info) {
-        call $Transmit_mib_info(maca_mib_info, macb_mib_info, rx_mib_info, tx_mib_info);
-    }
-}
-
-method Transmit_runtime_info(Runtime_Info *runtime_info) {
-    if ($ui_mode == QT) {
-        local int buf_len = runtime_info->header_length + Strlen(runtime_info->header) + sizeof(runtime_info->len) + runtime_info->len;
-        local char *buf = new char [buf_len];
-
-        local int header_length = Htonl(runtime_info->header_length);
-        local int name_length = Htonl(runtime_info->name_length);
-        local int description_length = Htonl(runtime_info->description_length);
-        local int len = Htonl(runtime_info->len);
-        local int type = Htonl(runtime_info->type);
-        local int flag = Htonl(runtime_info->flag);
-
-        memcpy(buf,
-               &header_length,
-               sizeof(runtime_info->header_length));
-        memcpy(buf + sizeof(runtime_info->header_length),
-               runtime_info->header,
-               runtime_info->header_length);
-        memcpy(buf + sizeof(runtime_info->header_length) + runtime_info->header_length,
-               &len,
-               sizeof(runtime_info->len));
-        memcpy(buf + sizeof(runtime_info->header_length) + runtime_info->header_length + sizeof(runtime_info->len),
-               &type,
-               sizeof(runtime_info->type));
-        memcpy(buf + sizeof(runtime_info->header_length) + runtime_info->header_length + sizeof(runtime_info->len) + sizeof(runtime_info->type),
-               &name_length,
-           sizeof(runtime_info->name_length));
-        memcpy(buf + sizeof(runtime_info->header_length) + runtime_info->header_length + sizeof(runtime_info->len) + sizeof(runtime_info->type) + sizeof(runtime_info->name_length),
-               runtime_info->name,
-               runtime_info->name_length);
-        memcpy(buf + sizeof(runtime_info->header_length) + runtime_info->header_length + sizeof(runtime_info->len) + sizeof(runtime_info->type) + sizeof(runtime_info->name_length) +
-               runtime_info->name_length,
-               &flag,
-               sizeof(runtime_info->flag));
-        memcpy(buf + sizeof(runtime_info->header_length) + runtime_info->header_length + sizeof(runtime_info->len) + sizeof(runtime_info->type) + sizeof(runtime_info->name_length) +
-               runtime_info->name_length + sizeof(runtime_info->flag),
-               &description_length,
-               sizeof(runtime_info->description_length));
-        memcpy(buf + sizeof(runtime_info->header_length) + runtime_info->header_length + sizeof(runtime_info->len) + sizeof(runtime_info->type) + sizeof(runtime_info->name_length) +
-               runtime_info->name_length + sizeof(runtime_info->flag) + sizeof(runtime_info->description_length),
-               runtime_info->description,
-               runtime_info->description_length);
-
-        qt_socket_send(buf, buf_len);
-
-        delete buf;
-    }
-}
-
-method Transmit_mib_info(MAC_MIB *maca_mib_info, MAC_MIB *macb_mib_info, RX_MIB *rx_mib_info, TX_MIB *tx_mib_info) {
-    if ($ui_mode == QT) {
-        local Runtime_Info runtime_info;
-        local int buf_len;
-        local char *buf;
-        local int header_length;
-        local int len;
-        local int type;
-
-        set_afdxES2_mib_runtime_header(&runtime_info.header);
-        runtime_info.header_length = Strlen(runtime_info.header);
-        header_length = Htonl(runtime_info.header_length);
-        runtime_info.len = sizeof(runtime_info.type) + sizeof(*maca_mib_info) + sizeof(*macb_mib_info) + sizeof(*rx_mib_info) + sizeof(*tx_mib_info);
-        len = Htonl(runtime_info.len);
-        runtime_info.type = 1;
-        type = Htonl(runtime_info.type);
-
-        buf_len = runtime_info.header_length + Strlen(runtime_info.header) + sizeof(runtime_info.len) + runtime_info.len;
-
-        buf = new char [buf_len];
-
-        memcpy(buf,
-               &header_length,
-               sizeof(runtime_info.header_length));
-        memcpy(buf + sizeof(runtime_info.header_length),
-               runtime_info.header,
-               runtime_info.header_length);
-        memcpy(buf + sizeof(runtime_info.header_length) + runtime_info.header_length,
-               &len,
-           sizeof(runtime_info.len));
-        memcpy(buf + sizeof(runtime_info.header_length) + runtime_info.header_length + sizeof(runtime_info.len),
-           &type,
-               sizeof(runtime_info.type));
-
-        local uint8 i;
-        local uint32 tmp;
-        for (i = 0; i < sizeof(*maca_mib_info) / sizeoftype(uint32); ++i) {
-            tmp = Htonl(*(cast(maca_mib_info, uint32*) +i));
-            memcpy(buf + sizeof(runtime_info.header_length) + runtime_info.header_length +
-                   sizeof(runtime_info.len) + sizeof(runtime_info.type) + i*4,
-                   &tmp,
-                   sizeoftype(uint32));
-        }
-
-        for (i = 0; i < sizeof(*macb_mib_info) / sizeoftype(uint32); ++i) {
-            tmp = Htonl(*(cast(macb_mib_info, uint32*) +i));
-            memcpy(buf + sizeof(runtime_info.header_length) + runtime_info.header_length +
-                   sizeof(runtime_info.len) + sizeof(runtime_info.type) + sizeof(*maca_mib_info) + i*4,
-                   &tmp,
-                   sizeoftype(uint32));
-        }
-
-        for (i = 0; i < sizeof(*rx_mib_info) / sizeoftype(uint32); ++i) {
-            tmp = Htonl(*(cast(rx_mib_info, uint32*) +i));
-            memcpy(buf + sizeof(runtime_info.header_length) + runtime_info.header_length +
-                   sizeof(runtime_info.len) + sizeof(runtime_info.type) + sizeof(*maca_mib_info) + sizeof(*macb_mib_info) + i*4,
-                   &tmp,
-                   sizeoftype(uint32));
-        }
-
-        for (i = 0; i < sizeof(*tx_mib_info) / sizeoftype(uint32); ++i) {
-            tmp = Htonl(*(cast(tx_mib_info, uint32*) +i));
-            memcpy(buf + sizeof(runtime_info.header_length) + runtime_info.header_length +
-                   sizeof(runtime_info.len) + sizeof(runtime_info.type) + sizeof(*maca_mib_info) + sizeof(*macb_mib_info) + sizeof(*rx_mib_info) + i*4,
-                   &tmp,
-                   sizeoftype(uint32));
-        }
-
-        qt_socket_send(buf, buf_len);
-
-        delete buf;
-    }
-}
-
-method ui_mode_init() {
-    switch($ui_mode) {
-    case QT:
-        qt_socket_init();
-        break;
-    case CLI:
-        break;
-    default:
-        break;
-    }
-}
-
-method init_main_board_module_ok() {
-    if ($ui_mode == QT) {
-        local Runtime_Info runtime_info;
-
-        set_main_board_module_runtime_header(&runtime_info.header);
-        runtime_info.header_length = Strlen(runtime_info.header);
-        runtime_info.type = 0;
-        set_main_board_module_runtime_name(&runtime_info.name);
-        runtime_info.name_length = Strlen(runtime_info.name);
-        runtime_info.flag = 0;
-        set_main_board_module_runtime_description(&runtime_info.description, runtime_info.flag);
-        runtime_info.description_length = Strlen(runtime_info.description);
-        runtime_info.len = sizeof(runtime_info.type) + sizeof(runtime_info.name_length) + Strlen(runtime_info.name) +
-            sizeof(runtime_info.flag) + sizeof(runtime_info.description_length) + Strlen(runtime_info.description);
-
-        call $Transmit_runtime_info(&runtime_info);
-
-        free_main_board_module_runtime_header(&runtime_info.header);
-        free_main_board_module_runtime_name(&runtime_info.name);
-        free_main_board_module_runtime_description(&runtime_info.description);
-    }
-}
-
-method init_A664_module_ok() {
-    if ($ui_mode == QT) {
-        local Runtime_Info runtime_info;
-
-        set_afdxES_module_runtime_header(&runtime_info.header);
-        runtime_info.header_length = Strlen(runtime_info.header);
-        runtime_info.type = 0;
-        set_afdxES_module_runtime_name(&runtime_info.name);
-        runtime_info.name_length = Strlen(runtime_info.name);
-        runtime_info.flag = 0;
-        set_afdxES_module_runtime_description(&runtime_info.description, runtime_info.flag);
-        runtime_info.description_length = Strlen(runtime_info.description);
-        runtime_info.len = sizeof(runtime_info.type) + sizeof(runtime_info.name_length) + Strlen(runtime_info.name) +
-            sizeof(runtime_info.flag) + sizeof(runtime_info.description_length) + Strlen(runtime_info.description);
-
-        call $Transmit_runtime_info(&runtime_info);
-
-        free_afdxES_module_runtime_header(&runtime_info.header);
-        free_afdxES_module_runtime_name(&runtime_info.name);
-        free_afdxES_module_runtime_description(&runtime_info.description);
-    }
-}
-
-method init_vxworks_ok() {
-    if ($ui_mode == QT) {
-        local Runtime_Info runtime_info;
-
-        set_vxworks_runtime_header(&runtime_info.header);
-        runtime_info.header_length = Strlen(runtime_info.header);
-        runtime_info.type = 0;
-        set_vxworks_runtime_name(&runtime_info.name);
-        runtime_info.name_length = Strlen(runtime_info.name);
-        runtime_info.flag = 0;
-        set_vxworks_runtime_description(&runtime_info.description, runtime_info.flag);
-        runtime_info.description_length = Strlen(runtime_info.description);
-        runtime_info.len = sizeof(runtime_info.type) + sizeof(runtime_info.name_length) + Strlen(runtime_info.name) +
-            sizeof(runtime_info.flag) + sizeof(runtime_info.description_length) + Strlen(runtime_info.description);
-
-        call $Transmit_runtime_info(&runtime_info);
-
-        free_vxworks_runtime_header(&runtime_info.header);
-        free_vxworks_runtime_name(&runtime_info.name);
-        free_vxworks_runtime_description(&runtime_info.description);
-    }
-}
-
-method init_a664_driver_middleware_ok() {
-    if ($ui_mode == QT) {
-        local Runtime_Info runtime_info;
-
-        set_a664_driver_middleware_runtime_header(&runtime_info.header);
-        runtime_info.header_length = Strlen(runtime_info.header);
-        runtime_info.type = 0;
-        set_a664_driver_middleware_runtime_name(&runtime_info.name);
-        runtime_info.name_length = Strlen(runtime_info.name);
-        runtime_info.flag = 0;
-        set_a664_driver_middleware_runtime_description(&runtime_info.description, runtime_info.flag);
-        runtime_info.description_length = Strlen(runtime_info.description);
-        runtime_info.len = sizeof(runtime_info.type) + sizeof(runtime_info.name_length) + Strlen(runtime_info.name) +
-            sizeof(runtime_info.flag) + sizeof(runtime_info.description_length) + Strlen(runtime_info.description);
-
-        call $Transmit_runtime_info(&runtime_info);
-
-        free_a664_driver_middleware_runtime_header(&runtime_info.header);
-        free_a664_driver_middleware_runtime_name(&runtime_info.name);
-        free_a664_driver_middleware_runtime_description(&runtime_info.description);
-    }
-}
-
-method init_a664_middleware_ok() {
-    if ($ui_mode == QT) {
-        local Runtime_Info runtime_info;
-
-        set_a664_middleware_runtime_header(&runtime_info.header);
-        runtime_info.header_length = Strlen(runtime_info.header);
-        runtime_info.type = 0;
-        set_a664_middleware_runtime_name(&runtime_info.name);
-        runtime_info.name_length = Strlen(runtime_info.name);
-        runtime_info.flag = 0;
-        set_a664_middleware_runtime_description(&runtime_info.description, runtime_info.flag);
-        runtime_info.description_length = Strlen(runtime_info.description);
-        runtime_info.len = sizeof(runtime_info.type) + sizeof(runtime_info.name_length) + Strlen(runtime_info.name) +
-            sizeof(runtime_info.flag) + sizeof(runtime_info.description_length) + Strlen(runtime_info.description);
-
-        call $Transmit_runtime_info(&runtime_info);
-
-        free_a664_middleware_runtime_header(&runtime_info.header);
-        free_a664_middleware_runtime_name(&runtime_info.name);
-        free_a664_middleware_runtime_description(&runtime_info.description);
-    }
-}
-
-method init_a664_card_ok() {
-    if ($ui_mode == QT) {
-        local Runtime_Info runtime_info;
-
-        set_a664_card_runtime_header(&runtime_info.header);
-        runtime_info.header_length = Strlen(runtime_info.header);
-        runtime_info.type = 0;
-        set_a664_card_runtime_name(&runtime_info.name);
-        runtime_info.name_length = Strlen(runtime_info.name);
-        runtime_info.flag = 0;
-        set_a664_card_runtime_description(&runtime_info.description, runtime_info.flag);
-        runtime_info.description_length = Strlen(runtime_info.description);
-        runtime_info.len = sizeof(runtime_info.type) + sizeof(runtime_info.name_length) + Strlen(runtime_info.name) +
-            sizeof(runtime_info.flag) + sizeof(runtime_info.description_length) + Strlen(runtime_info.description);
-
-        call $Transmit_runtime_info(&runtime_info);
-
-        free_a664_card_runtime_header(&runtime_info.header);
-        free_a664_card_runtime_name(&runtime_info.name);
-        free_a664_card_runtime_description(&runtime_info.description);
-    }
-}
-
-method init_ima_send() {
-    if ($middleware_mode == IMA) {
-        local int serverPortNum;
-        local int clientPortNum;
-
-        switch ($CS_mode) {         // notice
-        case CLIENT:
-            clientPortNum = 6789;
-            serverPortNum = 9876;
-            ima_client_socket_init(clientPortNum);
-            ima_server_socket_init(serverPortNum);
-            break;
-        case SERVER:
-            serverPortNum = 6789;
-            clientPortNum = 9876;
-            ima_server_socket_init(serverPortNum);
-            ima_client_socket_init(clientPortNum);
-            break;
-        defaut:
-            break;
-        }
-    }
-}
-
-data int size;
-
-method init_ima_recv() {
-    if ($middleware_mode == IMA) {
-        if ($is_cfg_done) {
-            local uint8 data[MAX_FRAME_SIZE];
-            $size = ima_socket_recv(&data, $size, get_ima_client_socket());
-            if (($size != 0) && (get_recv_state() == 2)) {
-                change_recv_state();
-                call $recv_frame(data, $size);
-            }
-        }
-
-        after (0.001)
-            call $init_ima_recv();
-    }
-}
+import "ui-data-exchange.dml";

 attribute middleware_mode "Used to switch middleware mode" {
     parameter documentation = "Used to switch work mode (to664middleware/to429middleware)";
@@ -514,6 +59,4 @@ method post_init() {
     call $init_udp_flag();
     call $init_vl_cfg();
     call $init_A664_module_ok();
-    call $init_ima_send();
-    call $init_ima_recv();
 }
diff --git a/simics-workspace/modules/afdxES/constant.dml b/simics-workspace/modules/afdxES/constant.dml
index 08ef68b..839b34b 100644
--- a/simics-workspace/modules/afdxES/constant.dml
+++ b/simics-workspace/modules/afdxES/constant.dml
@@ -79,7 +79,7 @@ constant RECV_VL_NUM = 256;
 // for work mode
 constant To429Middleware = 1;
 constant To664Middleware = 2;
-constant IMA = 3;
+constant ToIMAMiddleware = 3;

 // for port cache
 constant CACHE_ITEM_MAXSIZE = 10 * 1024;
diff --git a/simics-workspace/modules/afdxES/ima-data-exchange.c b/simics-workspace/modules/afdxES/ima-data-exchange.c
deleted file mode 100644
index 7f3d8c9..0000000
--- a/simics-workspace/modules/afdxES/ima-data-exchange.c
+++ /dev/null
@@ -1,220 +0,0 @@
-#include <stdio.h>
-#include <string.h>
-#include <stdlib.h>
-#include <errno.h>
-#include <unistd.h>
-#include <WINSOCK2.H>
-
-SOCKET ima_client_socket = INVALID_SOCKET;
-SOCKET ima_server_socket = INVALID_SOCKET;
-
-/*
-#define BUFFER_SIZE          4
-#define BUFFER_MAXLENGTH      1518 * 2
-
-static char frame_buffer[BUFFER_SIZE + BUFFER_MAXLENGTH];
-static int pbegin = 0;
-static int pend = 0;
-*/
-
-static int recv_state;
-static char frame[1518];
-
-typedef enum {
-    WAIT_FOR_FRAME_SIZE,
-    READY_TO_RECV_FRAME,
-    BEGIN_TO_HANDLE_FRAME
-} RECV_STATE;
-
-int ima_server_socket_init(int portNum) {
-    WSADATA wsa;
-
-    if (WSAStartup(MAKEWORD(2,2), &wsa)) {
-        fprintf(stderr, "WSAStartup failed, error code is %d.\n", WSAGetLastError());
-        exit(1);
-    }
-
-    SOCKET serSocket = socket(AF_INET, SOCK_STREAM, 0);
-
-    if (serSocket == INVALID_SOCKET) {
-        fprintf(stderr, "Create server socket failed, error code is %d.\n", WSAGetLastError());
-        exit(1);
-    }
-
-    SOCKADDR_IN addr;
-    addr.sin_family = AF_INET;
-    addr.sin_addr.s_addr = htonl(INADDR_ANY);
-    addr.sin_port = htons(portNum);
-
-    int opt = 1;
-    setsockopt(serSocket, SOL_SOCKET, SO_REUSEADDR, (const char*)(&opt), sizeof(opt));
-
-    if (bind(serSocket, (SOCKADDR*)&addr, sizeof(SOCKADDR_IN)) == SOCKET_ERROR) {
-        fprintf(stderr, "Bind failed, error code is %d\n", WSAGetLastError());
-        exit(1);
-    }
-
-    if (listen(serSocket, 1) == SOCKET_ERROR) {
-        fprintf(stderr, "Listen failed, error code is %d\n", WSAGetLastError());
-        exit(1);
-    }
-
-    SOCKADDR_IN clientsocket;
-    int len = sizeof(SOCKADDR_IN);
-
-    if ((ima_server_socket = accept(serSocket, (SOCKADDR*)&clientsocket, &len)) == SOCKET_ERROR) {
-        fprintf(stderr, "Accept failed, error code is %d.\n", WSAGetLastError());
-        exit(1);
-    }
-
-    fprintf(stdout, "Socket_server_init ok\n");
-
-    return 0;
-}
-
-int ima_client_socket_init(int portNum) {
-    WSADATA wsa;
-
-    if (WSAStartup(MAKEWORD(2,2), &wsa)) {
-        fprintf(stderr, "WSAStartup failed, error code is %d.\n", WSAGetLastError());
-        exit(1);
-    }
-
-    ima_client_socket = socket(AF_INET, SOCK_STREAM, 0);
-
-    if (ima_client_socket  == INVALID_SOCKET) {
-        fprintf(stderr, "Create client socket failed, error code is %d.\n", WSAGetLastError());
-        exit(1);
-    }
-
-    SOCKADDR_IN serveraddr;
-    memset(&serveraddr, 0, sizeof(serveraddr));
-    serveraddr.sin_family = AF_INET;
-    serveraddr.sin_port = htons(portNum);
-    serveraddr.sin_addr.s_addr = inet_addr("127.0.0.1");
-
-    if (connect(ima_client_socket, (struct sockaddr *) &serveraddr, sizeof(serveraddr)) == SOCKET_ERROR) {
-        fprintf(stderr, "Connect failed, error code is %d.\n", WSAGetLastError());
-        exit(1);
-    }
-
-    unsigned long ul = 1;
-    if (ioctlsocket(ima_client_socket, FIONBIO, (unsigned long*)&ul) == SOCKET_ERROR) {
-        fprintf(stderr, "ioctlsocket failed, error code is %d\n", WSAGetLastError());
-        exit(1);
-    }
-
-    fprintf(stdout, "Socket_client_init ok\n");
-
-    return 0;
-}
-
-void ima_socket_send(void *sendbuf, int size, void *socketfd) {
-    if (send(*(SOCKET*)socketfd, (char *)&size, sizeof(size), 0) != sizeof(size)) {
-        fprintf(stderr, "Socket send failed, error code is %d\n", WSAGetLastError());
-    }
-
-    if (send(*(SOCKET*)socketfd, (char *)sendbuf, size, 0) != size) {
-        fprintf(stderr, "Socket send failed, error code is %d\n", WSAGetLastError());
-    }
-}
-
-static int chars_to_int(char *array) {
-    int ret = (array[3] << 24) |
-              (array[2] << 16) |
-              (array[1] << 8) |
-              (array[0]);
-
-    return ret;
-}
-
-int ima_socket_recv(void *recvbuf, int size, void *socketfd) {
-    if (recv_state == WAIT_FOR_FRAME_SIZE) {
-        char frame_size[4];
-        int ret = recv(*(SOCKET*)socketfd, &frame_size, 4, 0);
-        if (ret > 0) {
-            if (ret != 4) {
-                while (4 - ret) {
-                    int ret1 = recv(*(SOCKET*)socketfd, frame_size + ret, 4 - ret, 0);
-                    if (ret1 > 0) {
-                        ret += ret1;
-                    } else {
-                        switch (WSAGetLastError()) {
-                            case WSAEWOULDBLOCK:
-                                break;
-                            default:
-                                fprintf(stderr, "Socket recv failed, error code is %d\n", WSAGetLastError());
-                                exit(1);
-                        }
-                    }
-                }
-            }
-
-            recv_state = READY_TO_RECV_FRAME;
-
-            return ima_socket_recv(recvbuf, chars_to_int(&frame_size), socketfd);
-        } else {
-            switch (WSAGetLastError()) {
-                case WSAEWOULDBLOCK:
-                    return 0;
-                default:
-                    fprintf(stderr, "Socket recv failed, error code is %d\n", WSAGetLastError());
-                    exit(1);
-            }
-        }
-    } else if (recv_state == READY_TO_RECV_FRAME) {
-        int ret = recv(*(SOCKET*)socketfd, &frame, size, 0);
-        if (ret > 0) {
-            if (ret != size) {
-                while (size - ret) {
-                    int ret1 = recv(*(SOCKET*)socketfd, frame + ret, size - ret, 0);
-                    if (ret1 > 0) {
-                        ret += ret1;
-                    } else {
-                        switch (WSAGetLastError()) {
-                            case WSAEWOULDBLOCK:
-                                break;
-                            default:
-                                fprintf(stderr, "Socket recv failed, error code is %d\n", WSAGetLastError());
-                                exit(1);
-                        }
-                    }
-                }
-            }
-
-            recv_state = BEGIN_TO_HANDLE_FRAME;
-            return ima_socket_recv(recvbuf, size, NULL);
-        } else {
-            switch (WSAGetLastError()) {
-                case WSAEWOULDBLOCK:
-                    return size;
-                default:
-                    fprintf(stderr, "Socket recv failed, error code is %d\n", WSAGetLastError());
-                    exit(1);
-            }
-        }
-    } else if (recv_state == BEGIN_TO_HANDLE_FRAME) {
-        memcpy(recvbuf, frame, size);
-        memset(frame, 0, 1518);
-        return size;
-    } else {
-        fprintf(stderr, "Fatal error, unsupported recv state!\n");
-        exit(1);
-    }
-}
-
-void *get_ima_client_socket(void) {
-    return &ima_client_socket;
-}
-
-void *get_ima_server_socket(void) {
-    return &ima_server_socket;
-}
-
-int get_recv_state(void) {
-    return recv_state;
-}
-
-void change_recv_state(void) {
-    recv_state = WAIT_FOR_FRAME_SIZE;
-}
diff --git a/simics-workspace/modules/afdxES/ima-data-exchange.h b/simics-workspace/modules/afdxES/ima-data-exchange.h
deleted file mode 100644
index ed7a4e7..0000000
--- a/simics-workspace/modules/afdxES/ima-data-exchange.h
+++ /dev/null
@@ -1,16 +0,0 @@
-#include <stdio.h>
-#include <string.h>
-#include <stdlib.h>
-#include <errno.h>
-#include <unistd.h>
-#include <WINSOCK2.H>
-
-extern SOCKET ima_client_socket;
-extern SOCKET ima_server_socket;
-
-extern int ima_server_socket_init(int portNum);
-extern int ima_client_socket_init(int portNum);
-extern int ima_socket_recv(unsigned char *recvbuf, int size, void *socketfd);
-extern int ima_socket_send(unsigned char *sendbuf, int size, void *socketfd);
-extern void *get_ima_client_socket(void);
-extern void *get_ima_server_socket(void);
diff --git a/simics-workspace/modules/afdxES/mib-info.dml b/simics-workspace/modules/afdxES/mib-info.dml
index 1b8a1fd..bde2b8c 100644
--- a/simics-workspace/modules/afdxES/mib-info.dml
+++ b/simics-workspace/modules/afdxES/mib-info.dml
@@ -133,3 +133,37 @@ method  Set_Rx_Mib_Info(uint8 *buffer) {
 method  Set_Tx_Mib_Info(uint8 *buffer) {
 	memcpy(&($tx_mib), buffer, sizeoftype(TX_MIB));
 }
+
+method mib_out_sum(MAC_MIB *mib,int size) {
+	mib->output_frame_count++;
+	mib->output_frame_byte_count+=size;
+	if(size <128)
+		mib->output_64_127_count++;
+	else if(size<256)
+		mib->output_128_255_count++;
+	else if(size<512)
+		mib->output_256_511_count++;
+	else if(size < 1024)
+		mib->output_512_1023_count++;
+	else if(size <=1518)
+		mib->output_1024_1518_count++;
+}
+
+method mib_in_sum(MAC_MIB *mib,int size) {
+	mib->input_frame_byte_count+=size;
+	mib->input_frame_count++;
+	if(size < 64)
+		mib->delete_little_64++;
+	else if(size < 128)
+		mib->input_64_127_count++;
+	else if (size < 256)
+		mib->input_128_255_count++;
+	else if(size < 512)
+		mib->input_256_511_count++;
+	else if(size < 1024)
+		mib->input_512_1023_count++;
+	else if(size <=1518)
+		mib->input_1024_1518_count++;
+	else
+		mib->delete_big_1518++;
+}
diff --git a/simics-workspace/modules/afdxES/packet-frame.dml b/simics-workspace/modules/afdxES/packet-frame.dml
index 60d1a6c..0ef2928 100644
--- a/simics-workspace/modules/afdxES/packet-frame.dml
+++ b/simics-workspace/modules/afdxES/packet-frame.dml
@@ -357,7 +357,7 @@ event send_phy {
                     }

                     switch ($middleware_mode) {
-                    case IMA:
+                    case ToIMAMiddleware:
                         ima_socket_send(&item.frame, item.size, get_ima_server_socket());
                         break;
                     case To429Middleware:
@@ -388,7 +388,7 @@ event send_phy {
                     }

                     switch ($middleware_mode) {
-                    case IMA:
+                    case ToIMAMiddleware:
                         ima_socket_send(&item.frame, item.size, get_ima_server_socket());
                         break;
                     case To429Middleware:
@@ -425,7 +425,7 @@ event send_phy {
                     }

                     switch ($middleware_mode) {
-                    case IMA:
+                    case ToIMAMiddleware:
                         ima_socket_send(&item.frame, item.size, get_ima_server_socket());
                         break;
                     case To429Middleware:
@@ -444,7 +444,7 @@ event send_phy {
                     }

                     switch ($middleware_mode) {
-                    case IMA:
+                    case ToIMAMiddleware:
                         ima_socket_send(&item2.frame, item2.size, get_ima_server_socket());
                         break;
                     case To429Middleware:
@@ -476,36 +476,3 @@ event send_phy {
         inline $send_phy.post($send_phy_delay, NULL);
     }
 }
-method mib_out_sum(MAC_MIB *mib,int size) {
-	mib->output_frame_count++;
-	mib->output_frame_byte_count+=size;
-	if(size <128)
-		mib->output_64_127_count++;
-	else if(size<256)
-		mib->output_128_255_count++;
-	else if(size<512)
-		mib->output_256_511_count++;
-	else if(size < 1024)
-		mib->output_512_1023_count++;
-	else if(size <=1518)
-		mib->output_1024_1518_count++;
-}
-
-method mib_in_sum(MAC_MIB *mib,int size) {
-	mib->input_frame_byte_count+=size;
-	mib->input_frame_count++;
-	if(size < 64)
-		mib->delete_little_64++;
-	else if(size < 128)
-		mib->input_64_127_count++;
-	else if (size < 256)
-		mib->input_128_255_count++;
-	else if(size < 512)
-		mib->input_256_511_count++;
-	else if(size < 1024)
-		mib->input_512_1023_count++;
-	else if(size <=1518)
-		mib->input_1024_1518_count++;
-	else
-		mib->delete_big_1518++;
-}
diff --git a/simics-workspace/modules/afdxES/pci-action.dml b/simics-workspace/modules/afdxES/pci-action.dml
index fe76750..37f626d 100644
--- a/simics-workspace/modules/afdxES/pci-action.dml
+++ b/simics-workspace/modules/afdxES/pci-action.dml
@@ -10,58 +10,42 @@ import "packet-frame.dml";
 //start DMA from PCI memory space to local space
 method transmit() {
     if ($pci9056.dmacsr1.dma1e == 1 && $pci9056.dmadpr1.dmad1 == 0 && $pci9056.dmasiz1.siz1 != 0) {
-        // Yes to transmit
-        // read from pci memeory space
         local exception_type_t ex;
         local exception_type_t exn;
-        local dbuffer_t *dbuf = new_dbuffer();  // new a buf
-        log "info",2,0: "tr new a dbuf";
+        local dbuffer_t *dbuf = new_dbuffer();
         local uint8 *buf;
 		local uint32 i;
-        log "info",2,0: "tr new a buf";
         local physical_address_t length;
-        log "info",2,0: "tr new a length";
         length = $pci9056.dmasiz1.siz1;  // required by the method of access_simple
-        log "info",2,0: "tr set length";
         buf = dbuffer_append(dbuf, length); // buf is the address of dbuf
-        log "info",2,0: "tr set buf";
         call $pci_data_from_memory(Sim_Addr_Space_Memory,
                                     buf,
                                     $pci9056.dmapadr1.padr1,
                                     $pci9056.dmasiz1.siz1) -> (ex);
-        log "info",2,0: " tr read from pci memory space";
         if (ex != Sim_PE_No_Exception) {
             throw;
             log "error": "tr Read data from memory fail";
         }

         switch ($middleware_mode) { // fix me
-        case To429Middleware: case IMA:
+        case To429Middleware: case ToIMAMiddleware:
             call $wrapped_frame(buf, length);
             break;
         case To664Middleware:
 			Send_To_A664(0, $pciport.tx_port_id.id, buf, $pciport.tx_frame_len.frame_len);
-            //call $Send_Payload_To_664_Middleware(buf, $pciport.tx_port_id.id, $pciport.tx_frame_len.frame_len);
             break;
         default:
             log "error" : "Unsupported work mode";
             break;
         }

-        log "info",2,0: "tr set DMA done status";
-        // set interrupt pin and raise pci interrupt
         if ($pci9056.dmamode1.die == 1 && $pci9056.intcsr.ie == 1 && $pci9056.intcsr.dma1ie == 1){
-            log "info",2,0: "tr ready to raise pci interrupt";
-           // call $pci_config.pci_raise_interrupt();  //interrrupt mode
 	        $pci9056.loopdmadone = 1;
 	        $pciport.tx_buf_overflow.del_frame_id_valid_bit = 1;
-            log "info",2,0: "tr raise pci interrupt";
-        }
-        else {
+        } else {
             log "error" : "tr Unable to raise PCI interrupt!";
         }
-    }
-    else{
+    } else{
         log "error": "tr Unable to start DMA transmit!";
     }
 }
@@ -69,30 +53,19 @@ method transmit() {
 // start DMA from local space to PCI memory space
 method receive() {
     if ($pci9056.dmacsr0.dma0e == 1 && $pci9056.dmadpr0.dmad0 == 1 && $pci9056.dmasiz0.siz0 != 0) {
-        // Yes to receive
-        // read from local space
         local exception_type_t ex;
         local exception_type_t exn;
         local dbuffer_t *dbuf = new_dbuffer();  // new a buf
-        log "info",2,0: "new a dbuf";
         local uint8 *buf;
-        log "info",2,0: "new a buf";
         local physical_address_t length;
-        log "info",2,0: "new a length";
         length = $pci9056.dmasiz0.siz0;   // required by the method of access_simple
-        log "info",2,0: "set length";
 		buf = dbuffer_append(dbuf, length);  // buf is the address of dbuf
-        log "info",2,0: "set buf";
-
-

-		// the name of connector line 90 add ".memory_space.asscee_simple()"
         exn = $local_space.memory_space.access_simple($dev.obj,
                                                 $pci9056.dmaladr0.ladr0,
                                                 buf,
                                                 length,
                                                 Sim_RW_Read,Sim_Endian_Target);
-        log "info",2,0: "read from local space";
         if (exn != Sim_PE_No_Exception) {
             log "error": "unknown exception (%d) on local space read", cast (exn, int);
         }
@@ -100,22 +73,18 @@ method receive() {
                                     buf,
                                     $pci9056.dmapadr0.padr0,
                                     $pci9056.dmasiz0.siz0) -> (ex);
-        log "info",2,0: "write to pci memory space";
         dbuffer_free(dbuf);//free dbuf
         if (ex != Sim_PE_No_Exception) {
             throw;
             log "error": "write data to memory fail";
         }
-        log "info",2,0: "set DMA done status";
-        // set interrupt pin and raise pci interrupt
+
         if ($pci9056.dmamode0.die == 1 && $pci9056.intcsr.ie == 1 && $pci9056.intcsr.dma0ie == 1) {
-            log "info",2,0: "ready to raise pci interrupt";
             call $pci_config.pci_raise_interrupt();
-            log "info",2,0: "raise pci interrupt";
         } else {
             log "error" : "Unable to raise PCI interrupt!";
         }
 	} else{
         log "error": "Unable to start DMA receive!";
-  }
+    }
 }
diff --git a/simics-workspace/modules/afdxES/pci-register.dml b/simics-workspace/modules/afdxES/pci-register.dml
index c5fc312..8464662 100644
--- a/simics-workspace/modules/afdxES/pci-register.dml
+++ b/simics-workspace/modules/afdxES/pci-register.dml
@@ -271,7 +271,7 @@ bank pciport{
                 case To429Middleware:
                     $phyB.ieee_802_3_mac.config_bandwidth($phy_max_bandwidth);
                     break;
-                case To664Middleware: case IMA:
+                case To664Middleware: case ToIMAMiddleware:
                     break;
                 default:
                     break;
@@ -292,7 +292,7 @@ bank pciport{
                 case To429Middleware:
                     $phyA.ieee_802_3_mac.config_bandwidth($phy_max_bandwidth);
                     break;
-                case To664Middleware: case IMA:
+                case To664Middleware: case ToIMAMiddleware:
                     break;
                 default:
                     break;
@@ -549,7 +549,7 @@ bank pciport{
             $is_cfg_done = true;
             call $Print_Cfg_Table();
             switch ($middleware_mode) { // fix me
-            case To429Middleware: case IMA:
+            case To429Middleware: case ToIMAMiddleware:
                 call $QuickSortAnalyseCamArray($analyse_cfg_cam_array);
                 call $QuickSortRMCamArray($rm_cfg_cam_array);
                 call $InitVLEnableInfo();
@@ -927,7 +927,7 @@ bank pciport{
             case 9:
 				if ($middleware_mode == To664Middleware)
 					$MACA_MIB_data = $maca_mib.input_frame_byte_count+$maca_mib.input_frame_count*4;
-				else if (($middleware_mode == To429Middleware) || ($middleware_mode == IMA))
+				else if (($middleware_mode == To429Middleware) || ($middleware_mode == ToIMAMiddleware))
 					$MACA_MIB_data = $maca_mib.input_frame_byte_count;
                 break;
             case 10:
@@ -951,7 +951,7 @@ bank pciport{
             case 16:
 				if ($middleware_mode == To664Middleware)
 					$MACA_MIB_data = $maca_mib.output_frame_byte_count+$maca_mib.output_frame_count*4;
-                else if (($middleware_mode == To429Middleware) || ($middleware_mode == IMA))
+                else if (($middleware_mode == To429Middleware) || ($middleware_mode == ToIMAMiddleware))
 					$MACA_MIB_data = $maca_mib.output_frame_byte_count;
                 break;
             case 17:
@@ -1003,7 +1003,7 @@ bank pciport{
             case 9:
 				if ($middleware_mode == To664Middleware)
 					$MACB_MIB_data = $macb_mib.input_frame_byte_count+$macb_mib.input_frame_count*4;
-				else if (($middleware_mode == To429Middleware) || ($middleware_mode == IMA))
+				else if (($middleware_mode == To429Middleware) || ($middleware_mode == ToIMAMiddleware))
 					$MACB_MIB_data = $macb_mib.input_frame_byte_count;
                 break;
             case 10:
@@ -1027,7 +1027,7 @@ bank pciport{
             case 16:
 				if ($middleware_mode == To664Middleware)
 					$MACB_MIB_data = $macb_mib.output_frame_byte_count+$macb_mib.output_frame_count*4;
-                else if (($middleware_mode == To429Middleware) || ($middleware_mode == IMA))
+                else if (($middleware_mode == To429Middleware) || ($middleware_mode == ToIMAMiddleware))
 					$MACB_MIB_data = $macb_mib.output_frame_byte_count;
                 break;
             case 17:
@@ -1321,7 +1321,7 @@ bank pciport{
             if (port_id >= 0 && port_id < 4096) {
                 switch (port_type) {
                 case 0: case 2:
-                    if (($middleware_mode == To429Middleware) || ($middleware_mode == IMA)) { // fix me
+                    if (($middleware_mode == To429Middleware) || ($middleware_mode == ToIMAMiddleware)) { // fix me
                         inline $IsPortQueueEmpty($port[port_id].queue) ->(is_empty);
                     } else if ($middleware_mode == To664Middleware) {
                         inline $IsPortCacheEmpty($PortCache[port_id]) ->(is_empty);
@@ -1330,7 +1330,7 @@ bank pciport{
                     }
                     break;
                 case 1:
-					if (($middleware_mode == To429Middleware) || ($middleware_mode == IMA)) { // fix me
+					if (($middleware_mode == To429Middleware) || ($middleware_mode == ToIMAMiddleware)) { // fix me
 						inline $IsPortSampleEmpty(&($port[port_id])) -> (is_empty);
 					} else if ($middleware_mode == To664Middleware) {
 						inline $IsCacheSampleEmpty(port_id) -> (is_empty);
@@ -1369,7 +1369,7 @@ bank pciport{
 					switch (port_type) {
                     case 0: case 2:
                         switch ($middleware_mode) {
-                        case To429Middleware: case IMA:
+                        case To429Middleware: case ToIMAMiddleware:
                             inline $DePortQueue($port[port_id].queue, &item)-> (is_empty);
                             if (is_empty == false) {
                                 return;
@@ -1389,7 +1389,7 @@ bank pciport{

                     case 1:
                         switch ($middleware_mode) {
-                        case To429Middleware: case IMA:
+                        case To429Middleware: case ToIMAMiddleware:
                             inline $IsPortSampleEmpty(&($port[port_id])) -> (is_empty);
                             if(is_empty == false) {
                                 local int i;
@@ -1428,7 +1428,7 @@ bank pciport{
                 }

                 local exception_type_t ex;
-                if (($middleware_mode == To429Middleware) || ($middleware_mode == IMA)) {
+                if (($middleware_mode == To429Middleware) || ($middleware_mode == ToIMAMiddleware)) {
                     item.size -= 8;

                     call $pci_data_to_memory(Sim_Addr_Space_Memory,
diff --git a/simics-workspace/modules/afdxES/rm-handler.dml b/simics-workspace/modules/afdxES/rm-handler.dml
index 44bab3d..09e674a 100644
--- a/simics-workspace/modules/afdxES/rm-handler.dml
+++ b/simics-workspace/modules/afdxES/rm-handler.dml
@@ -13,6 +13,77 @@ struct RM_INFO {
     uint32 frame_size;
 }

+connect phyA {
+	parameter documentation = "Send data to the second ARINC664 card";
+    parameter configuration = "optional";
+
+    interface ieee_802_3_mac {
+        parameter c_type = "ieee_802_3_mac_interface_t";
+    }
+}
+
+connect phyB {
+	parameter documentation = "Send data to the second ARINC664 card";
+    parameter configuration = "optional";
+
+    interface ieee_802_3_mac {
+        parameter c_type = "ieee_802_3_mac_interface_t";
+    }
+}
+
+implement ieee_802_3_mac {
+	method send_frame(uint8 *data, uint32 size) {
+        call $recv_frame(data, size);
+    }
+}
+
+method recv_frame(uint8 *data, uint32 size) {
+    local rm_cfg_cam item;
+    local uint8 rm_index;
+    local bool is_crc_ok;
+    local RM_INFO rm_info;
+
+    if ($is_ieee_802_3_log_on) {
+        call $print_ieee_802_3_mac_begin();
+    }
+    call $recv_crc_check(data,size,$Table)->(is_crc_ok);
+
+    if (is_crc_ok == false) {
+        // log "info":"crc check failed";
+        return;
+    } else {
+        if ($is_ieee_802_3_log_on) {
+            call $print_ieee_802_3_mac_crc();
+        }
+    }
+
+    call $change_endian(data, size);
+
+    item.VL_ID = data[4];
+    call $BinarySearchRMCfgCamArray($rm_cfg_cam_array, item) -> (rm_index);
+    if (rm_index == -1) {
+        // log "error" : "Can't find this conf in current rm cfg cam table, please check!";
+    } else {
+        rm_index = $rm_cfg_cam_array[rm_index].address;
+    }
+
+    call $Init_RM_INFO(&rm_info, rm_index, size);
+
+    local uint8 interface_id = data[11] >> 5;
+    local uint8 frame_sn = data[size-5];
+    local bool is_rm_ok;
+
+    call $RM_Handler(&rm_info, interface_id, frame_sn) -> (is_rm_ok);
+    if (is_rm_ok) {
+        if ($is_ieee_802_3_log_on) {
+            call $print_ieee_802_3_mac_rm();
+        }
+        call $unpacket_frame(data, size);
+    } else {
+        // log "error" : "Send from unkown network";
+    }
+}
+
 method Init_RM_INFO(RM_INFO *rm_info, uint8 rm_index, uint32 frame_size) {
     rm_info->VL_ENABLE = $rm_cfg_ram_array[rm_index].ram_data.VL_ENABLE;
     rm_info->RM_ENABLE = $rm_cfg_ram_array[rm_index].ram_data.RM_ENABLE;
diff --git a/simics-workspace/modules/afdxES/tomiddleware.dml b/simics-workspace/modules/afdxES/tomiddleware.dml
index b3cdb09..aeb5a60 100644
--- a/simics-workspace/modules/afdxES/tomiddleware.dml
+++ b/simics-workspace/modules/afdxES/tomiddleware.dml
@@ -1,189 +1,21 @@
 dml 1.2;

-import "port-cache.dml";
-
-extern int Init_664_Work_Mode(void);
-extern int Init_664_Card(void);
-extern int Send_To_A664(int card_nr, int pord_id, char *buf, int len);
-extern int Recv_From_A664(int card_nr, int port_id, char *buf,uint32 *network,uint32 *valid);
-extern int Send_To_664_Middleware(char *recvbuf, int size);
-extern int Recv_Data_From_664_Middleware(char *sendbuf, int size);
-extern int Recv_Mib_Info_From_664_Middleware(char *recvbuf, int size);
-extern void write_cfg_table(int type ,char *buf, int size);
-extern int get_recv_port_num();
-
-constant ANALYSE_CFG_CAM = 0;
-constant ANALYSE_CFG_RAM = 1;
-constant TRANSMIT_CFG_RAM = 2;
-constant SCHEDULING_CFG_RAM = 3;
-constant RM_CFG_CAM = 4;
-constant RM_CFG_RAM = 5;
-constant FRAGMENT_CFG_RAM = 6;
-constant PACKET_SEND = 7;
-constant PACKET_RECV = 8;
-constant DO_LOAD_CONF = 9;
-constant PACKET_MAC_MIB = 10;
-constant PACKET_RX_MIB = 11;
-constant PACKET_TX_MIB = 12;
-constant CFG_TYPE_SIZE = 4;
-constant CFG_NUM = 128;
-
-data int recv_num;
-
-method Handle_664_Data(uint32 port_id,uint8 *data, int length) {
-    local bool is_cached_ok;
-    local Cache_Item item;
-	local int port_type;
-
-    memcpy(item.buffer, data, length);
-    item.size = length ;
-
-	$recv_num++;
-
-	port_type = $analyse_cfg_ram_array[port_id].ram_data.PORT_TYPE;
-	switch (port_type) {
-		case 0: case 2:
-        call $EnPortCache($PortCache[port_id], item) -> (is_cached_ok);
-        if (!is_cached_ok) {
-            log "error" : "Port: %d's cache has full!\n", port_id;
-            ++$rx_buf_overflow_number[port_id];
-			break;
-		case 1:
-			if ($recv_port_valid[port_id] == 1)
-				call  $RefreshCacheData(port_id, item);
-			break;
-		default:
-			log "error" : "Unsuported port type";
-			break;
-        }
-	}
-}
-
-data int recv_port_num;
-data int recv_port_valid[4096];
-method Recv_Data_From_664_Middleware_Loop_Event() {
-    local int length;
-    local uint8 *buf = new uint8 [PORT_DATA_SIZE];
-	local int i;
-	local int j;
-	local uint32 network;
-	local uint32 valid;
-	for (i=0; i < $recv_port_num; i++)
-	{
-		length = Recv_From_A664(0, i, buf, &network,&valid);
-		$recv_port_valid[i] = valid;
-		if (length > 0) {
-
-			call $Handle_664_Data(i, buf, length);
-		}
-    }
-
-    delete buf;
-	//if($load_config_done == 1)
-		after (0.001) call $Recv_Data_From_664_Middleware_Loop_Event();
-	//else
-	//	after (0.001) call $Recv_Data_From_664_Middleware_Loop_Event();
-}
+import "scence1-664-data-exchange.dml";
+import "scence3-ima-data-exchange.dml";

 method init_middleware_mode() {
-	local int ret;
-	ret = -1;
-    if ($middleware_mode == To664Middleware) {
-     //   Init_664_Work_Mode();
+    switch ($middleware_mode) {
+    case To664Middleware:
         call $init_a664_middleware_ok();
         call $Init_Port_Cache();
-    }
-}
-data int load_config_done;
-
-method change_tran_cfg(transmit_cfg_ram *transmit_cfg_ram_array, transmit_cfg_ram_send *transmit_cfg_ram_send_array) {
-		transmit_cfg_ram_send_array->dst_partition_id=transmit_cfg_ram_array->ram_data_part4.PARTITION_ID ;
-		transmit_cfg_ram_send_array->user_defined_id_l=transmit_cfg_ram_array->ram_data_part4.USER_DEFINED_ID_L ;
-		transmit_cfg_ram_send_array->user_defined_id_h=transmit_cfg_ram_array->ram_data_part4.USER_DEFINED_ID_H ;
-		transmit_cfg_ram_send_array->private_ip=transmit_cfg_ram_array->ram_data_part4. PRIVATE_IP;
-		transmit_cfg_ram_send_array->vl_id=transmit_cfg_ram_array->ram_data_part1.VL_ID;
-		transmit_cfg_ram_send_array->vl_index=transmit_cfg_ram_array->ram_data_part1.VL_INDEX ;
-		transmit_cfg_ram_send_array->ip_multicast=transmit_cfg_ram_array->ram_data_part1.IP_MULTICAST;
-		transmit_cfg_ram_send_array->min_frame_length=transmit_cfg_ram_array->ram_data_part1.MIN_FRAME_LENGTH ;
-		transmit_cfg_ram_send_array->port_type=transmit_cfg_ram_array->ram_data_part1.PORT_TYPE ;
-		transmit_cfg_ram_send_array->partition_id=transmit_cfg_ram_array->ram_data_part2.PARTITION_ID ;
-		transmit_cfg_ram_send_array->interface_id=transmit_cfg_ram_array->ram_data_part2.INTERFACE_ID ;
-		transmit_cfg_ram_send_array->protocol_id=transmit_cfg_ram_array->ram_data_part2.PROTOCAL_ID ;
-		transmit_cfg_ram_send_array->user_defined_id=transmit_cfg_ram_array->ram_data_part2.USER_DEFINED_ID;
-		transmit_cfg_ram_send_array->sub_vl_id=transmit_cfg_ram_array->ram_data_part3.SUB_VL_ID ;
-		transmit_cfg_ram_send_array->dest_udp_port=transmit_cfg_ram_array->ram_data_part3.DEST_UDP_PORT ;
-		transmit_cfg_ram_send_array->source_udp_port=transmit_cfg_ram_array->ram_data_part3.SOURCE_UDP_PORT ;
-        /*
-		log "info": "--------------------------------------------";
-			log "info":"dst_partition_id=%x\n",transmit_cfg_ram_send_array->dst_partition_id;
-			log "info":"user_defined_id_l=%x\n",transmit_cfg_ram_send_array->user_defined_id_l;
-			log "info":"user_defined_id_h=%x\n",transmit_cfg_ram_send_array->user_defined_id_h;
-			log "info":"private_ip=%x\n",transmit_cfg_ram_send_array->private_ip;
-			log "info":"vl_id=%x\n",transmit_cfg_ram_send_array->vl_id;
-			log "info":"vl_index=%x\n",transmit_cfg_ram_send_array->vl_index;
-			log "info":"ip_multicast=%x\n",transmit_cfg_ram_send_array->ip_multicast;
-			log "info":"min_frame_length=%x\n",transmit_cfg_ram_send_array->min_frame_length;
-			log "info":"port_type=%x\n",transmit_cfg_ram_send_array->port_type;
-			log "info":"partition_id=%x\n",transmit_cfg_ram_send_array->partition_id;
-			log "info":"interface_id=%x\n",transmit_cfg_ram_send_array->interface_id;
-			log "info":"protocol_id=%x\n",transmit_cfg_ram_send_array->protocol_id;
-			log "info":"user_defined_id=%x\n",transmit_cfg_ram_send_array->user_defined_id;
-			log "info":"sub_vl_id=%x\n",transmit_cfg_ram_send_array->sub_vl_id;
-			log "info":"dest_udp_port=%x\n",transmit_cfg_ram_send_array->dest_udp_port;
-			log "info":"source_udp_port=%x\n",transmit_cfg_ram_send_array->source_udp_port;
-		log "info": "--------------------------------------------";
-        */
-}
-
-method Write_Config_All() {
-	local int data_type;
-	local int cfg_table_size;
-	local int i;
-	local int ret;
-	local int constant_field;
-	local transmit_cfg_ram_send *ram_send;
-	local transmit_cfg_ram *ram;
-    data_type = ANALYSE_CFG_CAM;
-    cfg_table_size = sizeoftype(analyse_cfg_cam) * CFG_NUM;
-	write_cfg_table(data_type,cast(&$analyse_cfg_cam_array, uint8*), cfg_table_size);
-
-    data_type = ANALYSE_CFG_RAM;
-    cfg_table_size = sizeoftype(analyse_cfg_ram) * CFG_NUM;
-	write_cfg_table(data_type,cast(&$analyse_cfg_ram_array, uint8*), cfg_table_size);
-
-    data_type = TRANSMIT_CFG_RAM;
-    cfg_table_size = sizeoftype(transmit_cfg_ram_send) * CFG_NUM;
-	for (i=0; i < CFG_NUM; i++) {
-		ram =cast(&($transmit_cfg_ram_array[i]),transmit_cfg_ram *);
-		ram_send =cast(&($transmit_cfg_ram_send_array[i]),transmit_cfg_ram_send *);
-		call $change_tran_cfg(ram, ram_send);
-	}
-	write_cfg_table(data_type,cast(&$transmit_cfg_ram_send_array, uint8*), cfg_table_size);
-
-    data_type = SCHEDULING_CFG_RAM;
-    cfg_table_size = sizeoftype(scheduling_cfg_ram) * CFG_NUM;
-	write_cfg_table(data_type,cast(&$scheduling_cfg_ram_array, uint8*), cfg_table_size);
-
-    data_type = RM_CFG_CAM;
-    cfg_table_size = sizeoftype(rm_cfg_cam) * CFG_NUM;
-	write_cfg_table(data_type,cast(&$rm_cfg_cam_array, uint8*), cfg_table_size);
-
-    data_type = RM_CFG_RAM;
-    cfg_table_size = sizeoftype(rm_cfg_ram) * CFG_NUM;
-	write_cfg_table(data_type,cast(&$rm_cfg_ram_array, uint8*), cfg_table_size);
-
-    data_type = FRAGMENT_CFG_RAM;
-    cfg_table_size = sizeoftype(fragment_cfg_ram) * CFG_NUM;
-	write_cfg_table(data_type,cast(&$fragment_cfg_ram_array, uint8*), cfg_table_size);
-
-}
-
-method Send_Payload_To_664_Middleware(uint8 *data, uint32 port_id, uint32 length) {
-    local uint32 data_type = 0;
-
-    data_type = PACKET_SEND;
-    Send_To_664_Middleware(cast(&data_type, uint8*), CFG_TYPE_SIZE);
-    Send_To_664_Middleware(cast(&length, uint8*), CFG_TYPE_SIZE);
-    Send_To_664_Middleware(cast(&port_id, uint8*), CFG_TYPE_SIZE);
-    Send_To_664_Middleware(data, length);
+        break;
+    case To429Middleware:
+        break;
+    case ToIMAMiddleware:
+        call $init_ima_send();
+        call $init_ima_recv();
+        break;
+    default:
+        log "error" : "Unsuported middleware mode!";
+    }
 }
diff --git a/simics-workspace/modules/afdxES/Makefile b/simics-workspace/modules/afdxES/Makefile
index 5ee442d..0d40b31 100644
--- a/simics-workspace/modules/afdxES/Makefile
+++ b/simics-workspace/modules/afdxES/Makefile
@@ -5,13 +5,13 @@
 #

 MODULE_CLASSES = afdxES
-SRC_FILES = tomiddleware.c communicate-with-qt.c untils.c afdxES_runtime_info.c mib.c scence3-ima-data-exchange.c
+SRC_FILES = communicate-with-qt.c untils.c afdxES_runtime_info.c scence3-ima-data-exchange.c scence1-664-data-exchange.c # tomiddleware.c mib.c
 DML_FILES = afdxES.dml
 DMLC_FLAGS = -m -I$(SIMICS_WORKSPACE)/include

 MODULE_CFLAGS = -I$(SIMICS_WORKSPACE)/modules/afdxES/inc/
 MODULE_LDFLAGS = $(SIMICS_WORKSPACE)\modules\afdxES\lib\AFDX_Card.lib -lws2_32
-
+
 SIMICS_API := 4.6
 THREAD_SAFE := yes

diff --git a/simics-workspace/modules/afdxES/afdxES_runtime_info.c b/simics-workspace/modules/afdxES/afdxES_runtime_info.c
index 2ada80a..4ff5f3d 100644
--- a/simics-workspace/modules/afdxES/afdxES_runtime_info.c
+++ b/simics-workspace/modules/afdxES/afdxES_runtime_info.c
@@ -1,8 +1,9 @@
 #include "afdxES_runtime_info.h"
 #include <stdlib.h>
+#include <string.h>

 void set_afdxES_module_runtime_header(void *p) {
-    char *header = "Hwa";
+    const char *header = "Hwa";
     int header_length = strlen(header) + 1;

     *(char **)p = (char *)malloc(header_length);
@@ -10,7 +11,7 @@ void set_afdxES_module_runtime_header(void *p) {
 }

 void set_afdxES_module_runtime_name(void *p) {
-    char *name = "A664Model";
+    const char *name = "A664Model";
     int name_length = strlen(name) + 1;

     *(char **)p = (char *)malloc(name_length);
@@ -19,12 +20,12 @@ void set_afdxES_module_runtime_name(void *p) {

 void set_afdxES_module_runtime_description(void *p, int flag) {
     if (flag == 0) {            /* success */
-        char *description = "A664模型初始化成功";
+        const char *description = "A664模型初始化成功";
         int description_length = strlen(description) + 1;
         *(char **)p = (char *)malloc(description_length);
         memcpy(*(char **)p, description, description_length);
     } else {
-        char *description = "A664模型初始化失败";
+        const char *description = "A664模型初始化失败";
         int description_length = strlen(description) + 1;
         *(char **)p = (char *)malloc(description_length);
         memcpy(*(char **)p, description, description_length);
@@ -47,7 +48,7 @@ void free_afdxES_module_runtime_description(void *p) {
 }

 void set_main_board_module_runtime_header(void *p) {
-    char *header = "Hwa";
+    const char *header = "Hwa";
     int header_length = strlen(header) + 1;

     *(char **)p = (char *)malloc(header_length);
@@ -55,7 +56,7 @@ void set_main_board_module_runtime_header(void *p) {
 }

 void set_main_board_module_runtime_name(void *p) {
-    char *name = "MainboardModel";
+    const char *name = "MainboardModel";
     int name_length = strlen(name) + 1;

     *(char **)p = (char *)malloc(name_length);
@@ -64,12 +65,12 @@ void set_main_board_module_runtime_name(void *p) {

 void set_main_board_module_runtime_description(void *p, int flag) {
     if (flag == 0) {            /* success */
-        char *description = "主机板模型初始化成功";
+        const char *description = "主机板模型初始化成功";
         int description_length = strlen(description) + 1;
         *(char **)p = (char *)malloc(description_length);
         memcpy(*(char **)p, description, description_length);
     } else {
-        char *description = "主机板模型初始化失败";
+        const char *description = "主机板模型初始化失败";
         int description_length = strlen(description) + 1;
         *(char **)p = (char *)malloc(description_length);
         memcpy(*(char **)p, description, description_length);
@@ -92,7 +93,7 @@ void free_main_board_module_runtime_description(void *p) {
 }

 void set_vxworks_runtime_header(void *p) {
-    char *header = "Hwa";
+    const char *header = "Hwa";
     int header_length = strlen(header) + 1;

     *(char **)p = (char *)malloc(header_length);
@@ -100,7 +101,7 @@ void set_vxworks_runtime_header(void *p) {
 }

 void set_vxworks_runtime_name(void *p) {
-    char *name = "VxWorks";
+    const char *name = "VxWorks";
     int name_length = strlen(name) + 1;

     *(char **)p = (char *)malloc(name_length);
@@ -109,12 +110,12 @@ void set_vxworks_runtime_name(void *p) {

 void set_vxworks_runtime_description(void *p, int flag) {
     if (flag == 0) {            /* success */
-        char *description = "VxWorks操作系统加载初始化成功";
+        const char *description = "VxWorks操作系统加载初始化成功";
         int description_length = strlen(description) + 1;
         *(char **)p = (char *)malloc(description_length);
         memcpy(*(char **)p, description, description_length);
     } else {
-        char *description = "VxWorks操作系统加载初始化失败";
+        const char *description = "VxWorks操作系统加载初始化失败";
         int description_length = strlen(description) + 1;
         *(char **)p = (char *)malloc(description_length);
         memcpy(*(char **)p, description, description_length);
@@ -137,7 +138,7 @@ void free_vxworks_runtime_description(void *p) {
 }

 void set_a664_driver_middleware_runtime_header(void *p) {
-    char *header = "Hwa";
+    const char *header = "Hwa";
     int header_length = strlen(header) + 1;

     *(char **)p = (char *)malloc(header_length);
@@ -145,7 +146,7 @@ void set_a664_driver_middleware_runtime_header(void *p) {
 }

 void set_a664_driver_middleware_runtime_name(void *p) {
-    char *name = "A664DriveMiddleware";
+    const char *name = "A664DriveMiddleware";
     int name_length = strlen(name) + 1;

     *(char **)p = (char *)malloc(name_length);
@@ -154,12 +155,12 @@ void set_a664_driver_middleware_runtime_name(void *p) {

 void set_a664_driver_middleware_runtime_description(void *p, int flag) {
     if (flag == 0) {            /* success */
-        char *description = "A664驱动中间件初始化成功";
+        const char *description = "A664驱动中间件初始化成功";
         int description_length = strlen(description) + 1;
         *(char **)p = (char *)malloc(description_length);
         memcpy(*(char **)p, description, description_length);
     } else {
-        char *description = "A664驱动中间件初始化失败";
+        const char *description = "A664驱动中间件初始化失败";
         int description_length = strlen(description) + 1;
         *(char **)p = (char *)malloc(description_length);
         memcpy(*(char **)p, description, description_length);
@@ -182,7 +183,7 @@ void free_a664_driver_middleware_runtime_description(void *p) {
 }

 void set_afdxES2_mib_runtime_header(void *p) {
-    char *header = "Hwa";
+    const char *header = "Hwa";
     int header_length = strlen(header) + 1;

     *(char **)p = (char *)malloc(header_length);
@@ -195,7 +196,7 @@ void free_afdxES2_mib_runtime_header(void *p) {
 }

 void set_a664_middleware_runtime_header(void *p) {
-    char *header = "Hwa";
+    const char *header = "Hwa";
     int header_length = strlen(header) + 1;

     *(char **)p = (char *)malloc(header_length);
@@ -203,7 +204,7 @@ void set_a664_middleware_runtime_header(void *p) {
 }

 void set_a664_middleware_runtime_name(void *p) {
-    char *name = "A664Middleware";
+    const char *name = "A664Middleware";
     int name_length = strlen(name) + 1;

     *(char **)p = (char *)malloc(name_length);
@@ -212,12 +213,12 @@ void set_a664_middleware_runtime_name(void *p) {

 void set_a664_middleware_runtime_description(void *p, int flag) {
     if (flag == 0) {            /* success */
-        char *description = "A664中间件初始化成功";
+        const char *description = "A664中间件初始化成功";
         int description_length = strlen(description) + 1;
         *(char **)p = (char *)malloc(description_length);
         memcpy(*(char **)p, description, description_length);
     } else {
-        char *description = "A664中间件初始化失败";
+        const char *description = "A664中间件初始化失败";
         int description_length = strlen(description) + 1;
         *(char **)p = (char *)malloc(description_length);
         memcpy(*(char **)p, description, description_length);
@@ -240,7 +241,7 @@ void free_a664_middleware_runtime_description(void *p) {
 }

 void set_a664_card_runtime_header(void *p) {
-    char *header = "Hwa";
+    const char *header = "Hwa";
     int header_length = strlen(header) + 1;

     *(char **)p = (char *)malloc(header_length);
@@ -248,7 +249,7 @@ void set_a664_card_runtime_header(void *p) {
 }

 void set_a664_card_runtime_name(void *p) {
-    char *name = "A664Card";
+    const char *name = "A664Card";
     int name_length = strlen(name) + 1;

     *(char **)p = (char *)malloc(name_length);
@@ -257,12 +258,12 @@ void set_a664_card_runtime_name(void *p) {

 void set_a664_card_runtime_description(void *p, int flag) {
     if (flag == 0) {            /* success */
-        char *description = "A664板卡初始化成功";
+        const char *description = "A664板卡初始化成功";
         int description_length = strlen(description) + 1;
         *(char **)p = (char *)malloc(description_length);
         memcpy(*(char **)p, description, description_length);
     } else {
-        char *description = "A664板卡初始化失败";
+        const char *description = "A664板卡初始化失败";
         int description_length = strlen(description) + 1;
         *(char **)p = (char *)malloc(description_length);
         memcpy(*(char **)p, description, description_length);
diff --git a/simics-workspace/modules/afdxES/checksum.dml b/simics-workspace/modules/afdxES/checksum.dml
index df8c3d3..717804a 100644
--- a/simics-workspace/modules/afdxES/checksum.dml
+++ b/simics-workspace/modules/afdxES/checksum.dml
@@ -70,8 +70,8 @@ method receive_udp_check_sum(PortQueue* queue)->(bool result) {
     call $part_udp_check_sum(checksum_data,
                              port_item.size,sum)
         ->(new_udp_checksum);
-  //  log "info" : "new_udp_checksum =%x", new_udp_checksum;
-  //  log "info" : "new_udp_checksum =%x", old_udp_checksum;
+    //  log "info" : "new_udp_checksum =%x", new_udp_checksum;
+    //  log "info" : "new_udp_checksum =%x", old_udp_checksum;
     if(new_udp_checksum != old_udp_checksum) result = false;
     else result = true;
 }
@@ -137,7 +137,7 @@ method unpacket_ip_checksum(uint8 *buffer)->(uint8 correct) {
 	//log "info" : "2	new_ip_checksum =%x", new_ip_checksum;
 	//log "info" : "2	old_ip_checksum =%x", old_ip_checksum;
 	if(new_ip_checksum != old_ip_checksum)
-		correct = INCORRECT;
+		correct = CORRECT;      // IN
 	else
 		correct = CORRECT;
 }
diff --git a/simics-workspace/modules/afdxES/inc/hwa_aes_klib.h b/simics-workspace/modules/afdxES/inc/hwa_aes_klib.h
index cc3cc62..55282f7 100644
--- a/simics-workspace/modules/afdxES/inc/hwa_aes_klib.h
+++ b/simics-workspace/modules/afdxES/inc/hwa_aes_klib.h
@@ -1,30 +1,34 @@
 #ifndef _HWA_AES_KLIB_H_
 #define _HWA_AES_KLIB_H_

-#ifdef WIN32  /* For Windows */
+#ifdef WIN32_NORMAL  /* For Windows */

 #ifdef _AFDX_EXPORT
-	#ifdef __cplusplus
-		#define _AFDX_DLL_EXPORT extern "C" __declspec(dllexport)
-	#else
-		#define _AFDX_DLL_EXPORT __declspec(dllexport)
-	#endif
+#ifdef __cplusplus
+#define _AFDX_DLL_EXPORT extern "C" __declspec(dllexport)
 #else
-	#ifdef __cplusplus
-		#define _AFDX_DLL_EXPORT extern "C" __declspec(dllimport)
-	#else
-		#define _AFDX_DLL_EXPORT __declspec(dllimport)
-	#endif
+#define _AFDX_DLL_EXPORT __declspec(dllexport)
+#endif
+#else
+#ifdef __cplusplus
+#define _AFDX_DLL_EXPORT extern "C" __declspec(dllimport)
+#else
+#define _AFDX_DLL_EXPORT __declspec(dllimport)
+#endif
 #endif

 #else  /* For VxWorks */

-#define _AFDX_DLL_EXPORT
+#ifdef __cplusplus
+#define _AFDX_DLL_EXPORT extern "C" __declspec(dllimport)
+#else
+#define _AFDX_DLL_EXPORT __declspec(dllimport)
+#endif

 #endif

 /* Error Code */
-#define HWA_AES_OK							0
+#define HWA_AES_OK			        0
 #define HWA_AES_CARD_NOT_FOUND				-1
 #define HWA_AES_CARD_NOT_OPEN				-2
 #define HWA_AES_CARD_INVALID				-3
@@ -43,6 +47,7 @@
 #define HWA_AES_UNKNOWN_ERR					-16
 #define HWA_AES_PORT_EXIST                  -17
 #define HWA_AES_MSGQ_ERR                    -18
+#define HWA_AES_MSGQ_TIMEOUT         -18
 #define HWA_AES_TASK_ERR        			-19
 #define HWA_AES_PHY_ERR          			-20
 #define HWA_AES_PHY_NO_LINK  				-21
@@ -63,51 +68,18 @@
 #define HWA_AES_PORT_NOTFOUND 					-36
 #define HWA_AES_PORT_TOO_MANY					-37
 #define HWA_AES_PORT_DUPLICATE  				-38
-
+#define HWA_AES_PACKET_LENGTH_ERR				-39

 /*Monitor Mode*/
 #define HWA_AES_MONITOR_START_EXIST -130
 #define HWA_AES_FINISHED_FIND -131
 #define HWA_AES_ONLY_MONITOR_MODE -132

-#if 0
-/* MIB statistic : network statistic type */
-#define MIB_CRC_ERR_NUM					0
-#define MIB_SHORT_FRAME_ERR_NUM			1
-#define MIB_LONG_1518_FRAME_ERR_NUM			2
-#define MIB_RECV_FRAME_NUM				3
-#define MIB_RECV_FRAME_64TO127_NUM		4
-#define MIB_RECV_FRAME_128TO255_NUM		5
-#define MIB_RECV_FRAME_256TO511_NUM		6
-#define MIB_RECV_FRAME_512TO1023_NUM	7
-#define MIB_RECV_FRAME_1024TO1518_NUM	8
-#define MIB_RECV_FRAME_BYTES			9
-#define MIB_SEND_FRAME_NUM				10
-#define MIB_SEND_FRAME_64TO127_NUM		11
-#define MIB_SEND_FRAME_128TO255_NUM		12
-#define MIB_SEND_FRAME_256TO511_NUM		13
-#define MIB_SEND_FRAME_512TO1023_NUM	14
-#define MIB_SEND_FRAME_1024TO1518_NUM	15
-#define MIB_SEND_FRAME_BYTES			16
-
-#define MIB_RECV_FRAME_PRE_ERR_NUM 17
-#define MIB_RECV_FRAME_IPCHECKSUM_ERR_NUM 18
-#define MIB_LONG_2048_FRAME_ERR_NUM 19
-
-/*MIB statistic : rx type */
-#define MIB_RX_A_DISABLE_TO_FILTER_FRAME_NUM 0
-#define MIB_RX_B_DISABLE_TO_FILTER_FRAME_NUM 1
-#define MIB_RX_A_CRC_ERR_TO_FILTER_FRAME_NUM 2
-#define MIB_RX_B_CRC_ERR_TO_FILTER_FRAME_NUM 3
-#define MIB_RX_VL_ERR_TO_FILTER_FRAME_NUM 4
-#define MIB_RX_REDUND_ERR_TO_FILTER_FRAME_NUM 5
-#define MIB_RX_DSTUDP_ERR_TO_FILTER_FRAME_NUM 6
-#define MIB_RX_IP_CHECKSUM_ERR_TO_FILTER_FRAME_NUM 7
-#define MIB_RX_IP_LEN_ERR_TO_FILTER_FRAME_NUM 8
-#define MIB_RX_UDP_LEN_ERR_TO_FILTER_FRAME_NUM 9
-#define MIB_RX_IP_FRAGMENT_ERR_TO_FILTER_FRAME_NUM 10

-#else
+/*Inject Mode*/
+#define HWA_AES_INJECT_START_EXIST  -200
+#define HWA_AES_ONLY_INJECT_MODE   -201
+
 typedef enum{
 	MIB_TX_SEND_IP_TOTAL_NUM=0,
 	MIB_TX_SEND_UDP_TOTAL_NUM,
@@ -163,19 +135,21 @@ typedef enum{
 	MIB_LONG_2048_FRAME_ERR_NUM,
 	MIB_NET_NR
 }HwaNetMibType;
-#endif
+
+
 /* data struct */
 typedef enum  /* Afdx Card Work Mode */
 {
 	mode_normal = 0,
 	mode_test,
-	mode_monitor
+	mode_monitor,
+	mode_inject
 }HwaWorkMode;

 typedef enum	/* VL Direction*/
 {
 	vl_send	= 0,
-	vl_recv
+	vl_recv
 }HwaVlDirection;

 typedef enum /* Port Type */
@@ -199,38 +173,38 @@ typedef enum /* Net Type */
 	net_g,
 	net_h,
 #endif
-	net_ab
+	net_ab
 }HwaNetwork;

 typedef enum   /* Recv Mode (POLL or Interrupt)*/
 {
-	mode_poll =0,
-	mode_int
+	mode_poll =0,
+	mode_int
 }HwaRecvMode;

 typedef enum /* IRIGB Mode*/
 {
-	mode_slave =0 ,
-	mode_master
+	mode_slave =0 ,
+	mode_master
 }HwaIrigbMode;

 typedef enum /* OutPut Select (Rj45 or PMC J4)*/
 {
-	mode_j4=0,
-	mode_rj45
+	mode_j4=0,
+	mode_rj45
 }HwaOutputMode;

 typedef enum  /* Sampling Port Mode (Capture or No Capture)*/
-{
+{
 	mode_nocapure=0,
-	mode_capture
-
+	mode_capture
+
 }HwaCaptureMode;

 typedef enum   /* Ip muliticast mode */
 {
-	mode_singlecast=0,
-	mode_multicast
+	mode_singlecast=0,
+	mode_multicast
 }HwaMulticastMode;

 typedef struct 		/* Card Info*/
@@ -242,7 +216,7 @@ typedef struct 		/* Card Info*/
 	unsigned int func_num;
 	unsigned int hw_ver;
 	unsigned int work_mode;
-	char serial[16];
+	char serial[16];
 }HwaCardInfo;

 typedef struct  /* packet stamp*/
@@ -264,12 +238,12 @@ typedef struct    /* recv time*/
 typedef struct /* Monitor Mode ,resolved packet info*/
 {
 	unsigned char net;
-	int packet_len;
-	HwaRecvTime time;
-	unsigned int pre_error;
-	unsigned int ip_checksum_error;
-	unsigned int crc_error;
-	unsigned short startbit_pos;
+	int packet_len;
+	HwaRecvTime time;
+	unsigned int pre_error;
+	unsigned int ip_checksum_error;
+	unsigned int crc_error;
+	unsigned short startbit_pos;
 }HwaPackInfo;

 #define HWA_AES_IP_LEN 32
@@ -278,7 +252,7 @@ typedef struct/*sap port information*/
 	unsigned int local_udp;
 	unsigned int remote_udp;
 	char remote_ip[HWA_AES_IP_LEN];
-	HwaRecvTime time;
+	HwaRecvTime time;
 	unsigned int network;
 	unsigned int valid;
 }HwaPktSrc;
@@ -297,7 +271,7 @@ _AFDX_DLL_EXPORT int hwa_aes_get_card_info(int cardnum,HwaCardInfo *card_info);
 _AFDX_DLL_EXPORT int hwa_aes_set_addr_map(unsigned int (*func)());

 /* set card mode */
-_AFDX_DLL_EXPORT int hwa_aes_set_mode(int cardnum, unsigned int work_mode);
+_AFDX_DLL_EXPORT int hwa_aes_set_mode(int cardnum, unsigned int work_mode);

 /* open card */
 _AFDX_DLL_EXPORT int hwa_aes_open_card (int cardnum);
@@ -414,12 +388,27 @@ _AFDX_DLL_EXPORT unsigned int hwa_aes_reg_read(int cardnum,unsigned int addr,int
 /*write FPGA register*/
 _AFDX_DLL_EXPORT unsigned int hwa_aes_reg_write(int cardnum,unsigned int addr,unsigned int value,int dev);

-_AFDX_DLL_EXPORT  int hwa_aes_set_local_time(int cardnum,HwaTime *localtime);
-
-
+/*get local time*/
 _AFDX_DLL_EXPORT  int hwa_aes_get_local_time(int cardnum,HwaTime *localtime);

+/*get soft version*/
 _AFDX_DLL_EXPORT void hwa_aes_get_soft_build_version(char *version);

+/*inject mode*/
+
+/*start send and set time*/
+ _AFDX_DLL_EXPORT  int hwa_aes_inject_start_send(int cardnum,unsigned int time);
+
+ /*stop send*/
+_AFDX_DLL_EXPORT  int hwa_aes_inject_stop_send(int cardnum);
+
+/* set ifg */
+_AFDX_DLL_EXPORT  int hwa_aes_inject_set_ifg(int cardnum, unsigned int num);
+
+_AFDX_DLL_EXPORT  char* hwa_afdx_get_error();
+
+_AFDX_DLL_EXPORT int  hwa_aes_port_check_data(unsigned int cardnum,unsigned int portId);
+_AFDX_DLL_EXPORT int hwa_aes_get_rtc(int cardnum,HwaRecvTime *time);
+_AFDX_DLL_EXPORT int hwa_aes_set_rtc(int cardnum,HwaTime *localtime);
 #endif
diff --git a/simics-workspace/modules/afdxES/mib.c b/simics-workspace/modules/afdxES/mib.c
index 68d6b4e..e95cbef 100755
--- a/simics-workspace/modules/afdxES/mib.c
+++ b/simics-workspace/modules/afdxES/mib.c
@@ -5,8 +5,6 @@ typedef unsigned int u32;

 #define __DEBUG__

-static card_nr = 0;
-
 #ifndef __DEBUG__
 static void get_mac_mib_value(HwaNetMibType type, int a_or_b, u32 *value) {
 	hwa_aes_mib_net_state(card_nr, a_or_b,type, value);
diff --git a/simics-workspace/modules/afdxES/packet-frame.dml b/simics-workspace/modules/afdxES/packet-frame.dml
index 0ef2928..86b19f5 100644
--- a/simics-workspace/modules/afdxES/packet-frame.dml
+++ b/simics-workspace/modules/afdxES/packet-frame.dml
@@ -10,6 +10,7 @@ import "log.dml";
 import "mib-info.dml";
 import "sample-frame.dml";

+extern void a664module_send_to_a664card(void *buf, int len);
 extern void *get_ima_server_socket(void);
 extern void ima_socket_send(void *sendbuf, int size, void *socketfd);

@@ -357,12 +358,15 @@ event send_phy {
                     }

                     switch ($middleware_mode) {
-                    case ToIMAMiddleware:
-                        ima_socket_send(&item.frame, item.size, get_ima_server_socket());
+                    case To664Middleware:
+                        a664module_send_to_a664card(&item.frame, item.size);
                         break;
                     case To429Middleware:
                         $phyA.ieee_802_3_mac.send_frame(cast(&item.frame, uint8*), item.size);
                         break;
+                    case ToIMAMiddleware:
+                        ima_socket_send(&item.frame, item.size, get_ima_server_socket());
+                        break;
                     default:
                         log "error" : "Danger!!! Please check your work mode!";
                         break;
@@ -388,11 +392,14 @@ event send_phy {
                     }

                     switch ($middleware_mode) {
-                    case ToIMAMiddleware:
-                        ima_socket_send(&item.frame, item.size, get_ima_server_socket());
+                    case To664Middleware:
+                        a664module_send_to_a664card(&item.frame, item.size);
                         break;
                     case To429Middleware:
                         $phyB.ieee_802_3_mac.send_frame(cast(&item.frame, uint8*), item.size);
+                        break;
+                    case ToIMAMiddleware:
+                        ima_socket_send(&item.frame, item.size, get_ima_server_socket());
                         break;
                     default:
                         log "error" : "Danger!!! Please check your work mode!";
@@ -425,11 +432,14 @@ event send_phy {
                     }

                     switch ($middleware_mode) {
-                    case ToIMAMiddleware:
-                        ima_socket_send(&item.frame, item.size, get_ima_server_socket());
+                    case To664Middleware:
+                        a664module_send_to_a664card(&item.frame, item.size);
                         break;
                     case To429Middleware:
                         $phyA.ieee_802_3_mac.send_frame(cast(&item.frame, uint8*), item.size);
+                        break;
+                    case ToIMAMiddleware:
+                        ima_socket_send(&item.frame, item.size, get_ima_server_socket());
                         break;
                     default:
                         log "error" : "Danger!!! Please check your work mode!";
@@ -444,11 +454,14 @@ event send_phy {
                     }

                     switch ($middleware_mode) {
-                    case ToIMAMiddleware:
-                        ima_socket_send(&item2.frame, item2.size, get_ima_server_socket());
+                    case To664Middleware:
+                        a664module_send_to_a664card(&item.frame, item.size);
                         break;
                     case To429Middleware:
                         $phyB.ieee_802_3_mac.send_frame(cast(&item2.frame, uint8*), item2.size);
+                        break;
+                    case ToIMAMiddleware:
+                        ima_socket_send(&item2.frame, item2.size, get_ima_server_socket());
                         break;
                     default:
                         log "error" : "Danger!!! Please check your work mode!";
diff --git a/simics-workspace/modules/afdxES/pci-action.dml b/simics-workspace/modules/afdxES/pci-action.dml
index 37f626d..e41befd 100644
--- a/simics-workspace/modules/afdxES/pci-action.dml
+++ b/simics-workspace/modules/afdxES/pci-action.dml
@@ -27,12 +27,11 @@ method transmit() {
             log "error": "tr Read data from memory fail";
         }

-        switch ($middleware_mode) { // fix me
-        case To429Middleware: case ToIMAMiddleware:
-            call $wrapped_frame(buf, length);
-            break;
+        switch ($middleware_mode) {
+        case To429Middleware:
+        case ToIMAMiddleware:
         case To664Middleware:
-			Send_To_A664(0, $pciport.tx_port_id.id, buf, $pciport.tx_frame_len.frame_len);
+            call $wrapped_frame(buf, length);
             break;
         default:
             log "error" : "Unsupported work mode";
diff --git a/simics-workspace/modules/afdxES/pci-register.dml b/simics-workspace/modules/afdxES/pci-register.dml
index 8464662..2244bd8 100644
--- a/simics-workspace/modules/afdxES/pci-register.dml
+++ b/simics-workspace/modules/afdxES/pci-register.dml
@@ -4,9 +4,9 @@ import "pci-device.dml";          // Generic PCI device
 import "binary-search.dml";
 import "quick-sort.dml";
 import "log.dml";
+import "tomiddleware.dml";

-extern void LoadCfgTo664Middleware();
-
+// extern void LoadCfgTo664Middleware();
 // Basic bank declaration
 bank pci_config {
     // List of base address registers used by the device
@@ -548,23 +548,24 @@ bank pciport{
 			local int ret;
             $is_cfg_done = true;
             call $Print_Cfg_Table();
-            switch ($middleware_mode) { // fix me
+            call $QuickSortAnalyseCamArray($analyse_cfg_cam_array);
+            call $QuickSortRMCamArray($rm_cfg_cam_array);
+            call $InitVLEnableInfo();
+            switch ($middleware_mode) {
             case To429Middleware: case ToIMAMiddleware:
-                call $QuickSortAnalyseCamArray($analyse_cfg_cam_array);
-                call $QuickSortRMCamArray($rm_cfg_cam_array);
-                call $InitVLEnableInfo();
                 break;
             case To664Middleware:
-				call $ Write_Config_All();
-                LoadCfgTo664Middleware();
-				ret = Init_664_Card();
-                call $init_a664_card_ok();
-				$recv_port_num = get_recv_port_num();
-				if(ret == 0)
-					log "info":"init middle succesee";
-				else
-					log "info":"init middle failed! ret =%d",ret;
-				call $Recv_Data_From_664_Middleware_Loop_Event();
+				// call $ Write_Config_All();
+                // LoadCfgTo664Middleware();
+				init_664_card();
+                call $init_a664_card_ok();
+                call $init_664_recv();
+				// $recv_port_num = get_recv_port_num();
+				// if(ret == 0)
+				//	log "info":"init middle succesee";
+				// else
+				//	log "info":"init middle failed! ret =%d",ret;
+				// call $Recv_Data_From_664_Middleware_Loop_Event();
                 break;
             default:
                 log "error" : "Unsupported work mode!";
@@ -893,9 +894,10 @@ bank pciport{

     register MACA_MIB_addr {
         method after_write(memop) {
+            /*
 			if ($middleware_mode == To664Middleware)
 				call $Recv_Mac_Mib_From_664_Middleware(MACA_MIB_CONFIG);
-
+            */
             switch($this) {
             case 0:
                 $MACA_MIB_data = $maca_mib.crc_error;
@@ -925,9 +927,11 @@ bank pciport{
                 $MACA_MIB_data = $maca_mib.input_1024_1518_count;
                 break;
             case 9:
+                /*
 				if ($middleware_mode == To664Middleware)
 					$MACA_MIB_data = $maca_mib.input_frame_byte_count+$maca_mib.input_frame_count*4;
 				else if (($middleware_mode == To429Middleware) || ($middleware_mode == ToIMAMiddleware))
+                */
 					$MACA_MIB_data = $maca_mib.input_frame_byte_count;
                 break;
             case 10:
@@ -949,9 +953,11 @@ bank pciport{
                 $MACA_MIB_data = $maca_mib.output_1024_1518_count;
                 break;
             case 16:
+                /*
 				if ($middleware_mode == To664Middleware)
 					$MACA_MIB_data = $maca_mib.output_frame_byte_count+$maca_mib.output_frame_count*4;
                 else if (($middleware_mode == To429Middleware) || ($middleware_mode == ToIMAMiddleware))
+                */
 					$MACA_MIB_data = $maca_mib.output_frame_byte_count;
                 break;
             case 17:
@@ -969,9 +975,10 @@ bank pciport{

     register MACB_MIB_addr {
         method after_write(memop) {
+            /*
 			if ($middleware_mode == To664Middleware)
 				call $Recv_Mac_Mib_From_664_Middleware(MACB_MIB_CONFIG);
-
+            */
             switch($this) {
             case 0:
                 $MACB_MIB_data = $macb_mib.crc_error;
@@ -1001,9 +1008,11 @@ bank pciport{
                 $MACB_MIB_data = $macb_mib.input_1024_1518_count;
                 break;
             case 9:
+                /*
 				if ($middleware_mode == To664Middleware)
 					$MACB_MIB_data = $macb_mib.input_frame_byte_count+$macb_mib.input_frame_count*4;
 				else if (($middleware_mode == To429Middleware) || ($middleware_mode == ToIMAMiddleware))
+                */
 					$MACB_MIB_data = $macb_mib.input_frame_byte_count;
                 break;
             case 10:
@@ -1025,9 +1034,11 @@ bank pciport{
                 $MACB_MIB_data = $macb_mib.output_1024_1518_count;
                 break;
             case 16:
+                /*
 				if ($middleware_mode == To664Middleware)
 					$MACB_MIB_data = $macb_mib.output_frame_byte_count+$macb_mib.output_frame_count*4;
                 else if (($middleware_mode == To429Middleware) || ($middleware_mode == ToIMAMiddleware))
+                */
 					$MACB_MIB_data = $macb_mib.output_frame_byte_count;
                 break;
             case 17:
@@ -1045,8 +1056,10 @@ bank pciport{

     register rx_MIB_addr {
         method after_write(memop) {
+            /*
 			if ($middleware_mode == To664Middleware)
 				call $Recv_Rx_Mib_Info_From_664_Middleware();
+            */
             switch($this) {
             case 0:
                 $rx_MIB_data = $rx_mib.disable_A_filt_count;
@@ -1112,8 +1125,10 @@ bank pciport{

     register tx_MIB_addr {
         method after_write(memop) {
+            /*
 			if ($middleware_mode == To664Middleware)
 				call $Recv_Tx_Mib_Info_From_664_Middleware();
+            */
             switch($this) {
             case 0:
                 $tx_MIB_data = $tx_mib.tx_ip_count;
@@ -1321,23 +1336,30 @@ bank pciport{
             if (port_id >= 0 && port_id < 4096) {
                 switch (port_type) {
                 case 0: case 2:
+                    /*
                     if (($middleware_mode == To429Middleware) || ($middleware_mode == ToIMAMiddleware)) { // fix me
-                        inline $IsPortQueueEmpty($port[port_id].queue) ->(is_empty);
+                    */
+                    inline $IsPortQueueEmpty($port[port_id].queue) ->(is_empty);
+                    /*
                     } else if ($middleware_mode == To664Middleware) {
                         inline $IsPortCacheEmpty($PortCache[port_id]) ->(is_empty);
 					} else {
                         log "info" : "Unsupported work mode!\n";
                     }
+                    */
                     break;
                 case 1:
+                    /*
 					if (($middleware_mode == To429Middleware) || ($middleware_mode == ToIMAMiddleware)) { // fix me
-						inline $IsPortSampleEmpty(&($port[port_id])) -> (is_empty);
+                    */
+                    inline $IsPortSampleEmpty(&($port[port_id])) -> (is_empty);
+                    /*
 					} else if ($middleware_mode == To664Middleware) {
-						inline $IsCacheSampleEmpty(port_id) -> (is_empty);
+                        inline $IsCacheSampleEmpty(port_id) -> (is_empty);
 					} else {
 						log "info" : "Unsupported work mode!\n";
 				    }
-
+                    */
                     break;
                 default:
                     log "error" : "Unsuported port type";
@@ -1362,25 +1384,29 @@ bank pciport{
                 local uint32 port_id = $pciport.rx_port_id;
                 local bool is_empty;
                 local PortItem item;
-                local Cache_Item item1;
+                //local Cache_Item item1;
 				local int port_type;
 				port_type = $analyse_cfg_ram_array[port_id].ram_data.PORT_TYPE;
                 if (port_id >= 0 && port_id < 4096) {
 					switch (port_type) {
                     case 0: case 2:
                         switch ($middleware_mode) {
-                        case To429Middleware: case ToIMAMiddleware:
+                        case To429Middleware:
+                        case ToIMAMiddleware:
+                        case To664Middleware:
                             inline $DePortQueue($port[port_id].queue, &item)-> (is_empty);
                             if (is_empty == false) {
                                 return;
-                            }
+                            }
                             break;
+                            /*
                         case To664Middleware:
                             inline $DePortCache($PortCache[port_id], &item1) -> (is_empty);
                             if (is_empty == false) {
                                 return;
                             }
                             break;
+                            */
                         default:
                             log "info" : "Unsupported work mode!\n";
                             break;
@@ -1389,21 +1415,17 @@ bank pciport{

                     case 1:
                         switch ($middleware_mode) {
-                        case To429Middleware: case ToIMAMiddleware:
+                        case To429Middleware:
+                        case ToIMAMiddleware:
+                        case To664Middleware:
                             inline $IsPortSampleEmpty(&($port[port_id])) -> (is_empty);
-                            if(is_empty == false) {
+                            if(!is_empty) {
                                 local int i;
-                                //memcpy(item.frame, ($port[port_id]).frame, ($port[port_id]).size);
                                 item.frame =  ($port[port_id]).frame;
                                 item.size = ($port[port_id]).size;
-                                /*	log "info": "afdx 1 sample data";
-                                    log "info" : "sample data size is %d",item.size;
-                                    for (i=8; i < item.size; i++) log "info" : "data[%d] = %x", i, item.frame[i];
-                                */
-                            } else {
-                                return ;
-                            }
+                            }
                             break;
+                            /*
                         case To664Middleware:
                             inline $IsCacheSampleEmpty(port_id) -> (is_empty);
                             if(is_empty == false ) {
@@ -1413,6 +1435,7 @@ bank pciport{
                                 return;
                             }
                             break;
+                            */
                         default:
                             log "info" : "Unsupported work mode!\n";
                             break;
@@ -1428,14 +1451,17 @@ bank pciport{
                 }

                 local exception_type_t ex;
+                /*
                 if (($middleware_mode == To429Middleware) || ($middleware_mode == ToIMAMiddleware)) {
-                    item.size -= 8;
-
-                    call $pci_data_to_memory(Sim_Addr_Space_Memory,
-                                             &item.frame[8],
-                                             $pci9056.dmapadr0.padr0,
-                                             item.size) -> (ex);
-                    $pciport.rx_frame_len = item.size;
+                */
+                item.size -= 8;
+
+                call $pci_data_to_memory(Sim_Addr_Space_Memory,
+                                         &item.frame[8],
+                                         $pci9056.dmapadr0.padr0,
+                                         item.size) -> (ex);
+                $pciport.rx_frame_len = item.size;
+                /*
                 } else if ($middleware_mode == To664Middleware) {
                     call $pci_data_to_memory(Sim_Addr_Space_Memory,
                                              item1.buffer,
@@ -1443,7 +1469,7 @@ bank pciport{
                                              item1.size) -> (ex);
                     $pciport.rx_frame_len = item1.size;
                 }
-
+                    */
                 if (ex != Sim_PE_No_Exception) {
                     throw;
                     log "error": "write data to memory fail";
diff --git a/simics-workspace/modules/afdxES/rm-handler.dml b/simics-workspace/modules/afdxES/rm-handler.dml
index 09e674a..72eb325 100644
--- a/simics-workspace/modules/afdxES/rm-handler.dml
+++ b/simics-workspace/modules/afdxES/rm-handler.dml
@@ -97,7 +97,7 @@ method Init_RM_INFO(RM_INFO *rm_info, uint8 rm_index, uint32 frame_size) {

 method RM_Handler(RM_INFO *rm_info, uint8 interface_id, uint8 frame_sn) -> (bool result) {
     if (rm_info->VL_ENABLE == 0) {
-        result = false;
+        result = true;
         return;
     }

@@ -127,6 +127,12 @@ method RM_Handler(RM_INFO *rm_info, uint8 interface_id, uint8 frame_sn) -> (bool
         $recv_vl[rm_info->rm_index].expect_sn++;
         $recv_vl[rm_info->rm_index].expect_sn %= 256;

+        if ($middleware_mode == To664Middleware) {
+            if ($recv_vl[rm_info->rm_index].expect_sn == 0) {
+                $recv_vl[rm_info->rm_index].expect_sn++;
+            }
+        }
+
         result = true;
     } else {
 		if(interface_id == rm_info->A_Interface_ID) {
diff --git a/simics-workspace/modules/afdxES/scence1-664-data-exchange.dml b/simics-workspace/modules/afdxES/scence1-664-data-exchange.dml
index cf15e1a..b5741da 100644
--- a/simics-workspace/modules/afdxES/scence1-664-data-exchange.dml
+++ b/simics-workspace/modules/afdxES/scence1-664-data-exchange.dml
@@ -1,181 +1,23 @@
 dml 1.2;

-import "port-cache.dml";
+extern void init_664_card(void);
+extern int a664module_recv_from_a664card(void *_buf);

-extern int Init_664_Work_Mode(void);
-extern int Init_664_Card(void);
-extern int Send_To_A664(int card_nr, int pord_id, char *buf, int len);
-extern int Recv_From_A664(int card_nr, int port_id, char *buf,uint32 *network,uint32 *valid);
-extern int Send_To_664_Middleware(char *recvbuf, int size);
-extern int Recv_Data_From_664_Middleware(char *sendbuf, int size);
-extern int Recv_Mib_Info_From_664_Middleware(char *recvbuf, int size);
-extern void write_cfg_table(int type ,char *buf, int size);
-extern int get_recv_port_num();
+method init_664_recv() {
+    if ($middleware_mode == To664Middleware) {
+        if ($is_cfg_done) {
+            local uint8 *data;
+            local int frame_size = a664module_recv_from_a664card(&data);

-constant ANALYSE_CFG_CAM = 0;
-constant ANALYSE_CFG_RAM = 1;
-constant TRANSMIT_CFG_RAM = 2;
-constant SCHEDULING_CFG_RAM = 3;
-constant RM_CFG_CAM = 4;
-constant RM_CFG_RAM = 5;
-constant FRAGMENT_CFG_RAM = 6;
-constant PACKET_SEND = 7;
-constant PACKET_RECV = 8;
-constant DO_LOAD_CONF = 9;
-constant PACKET_MAC_MIB = 10;
-constant PACKET_RX_MIB = 11;
-constant PACKET_TX_MIB = 12;
-constant CFG_TYPE_SIZE = 4;
-constant CFG_NUM = 128;
+            data += 8;
+            frame_size -= 8;

-data int recv_num;
-
-method Handle_664_Data(uint32 port_id,uint8 *data, int length) {
-    local bool is_cached_ok;
-    local Cache_Item item;
-	local int port_type;
-
-    memcpy(item.buffer, data, length);
-    item.size = length ;
-
-	$recv_num++;
-
-	port_type = $analyse_cfg_ram_array[port_id].ram_data.PORT_TYPE;
-	switch (port_type) {
-		case 0: case 2:
-        call $EnPortCache($PortCache[port_id], item) -> (is_cached_ok);
-        if (!is_cached_ok) {
-            log "error" : "Port: %d's cache has full!\n", port_id;
-            ++$rx_buf_overflow_number[port_id];
-			break;
-		case 1:
-			if ($recv_port_valid[port_id] == 1)
-				call  $RefreshCacheData(port_id, item);
-			break;
-		default:
-			log "error" : "Unsuported port type";
-			break;
+            if (frame_size > 0) {
+                call $recv_frame(data, frame_size);
+            }
         }
-	}
-}

-data int recv_port_num;
-data int recv_port_valid[4096];
-method Recv_Data_From_664_Middleware_Loop_Event() {
-    local int length;
-    local uint8 *buf = new uint8 [PORT_DATA_SIZE];
-	local int i;
-	local int j;
-	local uint32 network;
-	local uint32 valid;
-	for (i=0; i < $recv_port_num; i++)
-	{
-		length = Recv_From_A664(0, i, buf, &network,&valid);
-		$recv_port_valid[i] = valid;
-		if (length > 0) {
-
-			call $Handle_664_Data(i, buf, length);
-		}
+        after(0.007)
+            call $init_664_recv();
     }
-
-    delete buf;
-	//if($load_config_done == 1)
-		after (0.001) call $Recv_Data_From_664_Middleware_Loop_Event();
-	//else
-	//	after (0.001) call $Recv_Data_From_664_Middleware_Loop_Event();
-}
-
-
-data int load_config_done;
-
-method change_tran_cfg(transmit_cfg_ram *transmit_cfg_ram_array, transmit_cfg_ram_send *transmit_cfg_ram_send_array) {
-		transmit_cfg_ram_send_array->dst_partition_id=transmit_cfg_ram_array->ram_data_part4.PARTITION_ID ;
-		transmit_cfg_ram_send_array->user_defined_id_l=transmit_cfg_ram_array->ram_data_part4.USER_DEFINED_ID_L ;
-		transmit_cfg_ram_send_array->user_defined_id_h=transmit_cfg_ram_array->ram_data_part4.USER_DEFINED_ID_H ;
-		transmit_cfg_ram_send_array->private_ip=transmit_cfg_ram_array->ram_data_part4. PRIVATE_IP;
-		transmit_cfg_ram_send_array->vl_id=transmit_cfg_ram_array->ram_data_part1.VL_ID;
-		transmit_cfg_ram_send_array->vl_index=transmit_cfg_ram_array->ram_data_part1.VL_INDEX ;
-		transmit_cfg_ram_send_array->ip_multicast=transmit_cfg_ram_array->ram_data_part1.IP_MULTICAST;
-		transmit_cfg_ram_send_array->min_frame_length=transmit_cfg_ram_array->ram_data_part1.MIN_FRAME_LENGTH ;
-		transmit_cfg_ram_send_array->port_type=transmit_cfg_ram_array->ram_data_part1.PORT_TYPE ;
-		transmit_cfg_ram_send_array->partition_id=transmit_cfg_ram_array->ram_data_part2.PARTITION_ID ;
-		transmit_cfg_ram_send_array->interface_id=transmit_cfg_ram_array->ram_data_part2.INTERFACE_ID ;
-		transmit_cfg_ram_send_array->protocol_id=transmit_cfg_ram_array->ram_data_part2.PROTOCAL_ID ;
-		transmit_cfg_ram_send_array->user_defined_id=transmit_cfg_ram_array->ram_data_part2.USER_DEFINED_ID;
-		transmit_cfg_ram_send_array->sub_vl_id=transmit_cfg_ram_array->ram_data_part3.SUB_VL_ID ;
-		transmit_cfg_ram_send_array->dest_udp_port=transmit_cfg_ram_array->ram_data_part3.DEST_UDP_PORT ;
-		transmit_cfg_ram_send_array->source_udp_port=transmit_cfg_ram_array->ram_data_part3.SOURCE_UDP_PORT ;
-        /*
-		log "info": "--------------------------------------------";
-			log "info":"dst_partition_id=%x\n",transmit_cfg_ram_send_array->dst_partition_id;
-			log "info":"user_defined_id_l=%x\n",transmit_cfg_ram_send_array->user_defined_id_l;
-			log "info":"user_defined_id_h=%x\n",transmit_cfg_ram_send_array->user_defined_id_h;
-			log "info":"private_ip=%x\n",transmit_cfg_ram_send_array->private_ip;
-			log "info":"vl_id=%x\n",transmit_cfg_ram_send_array->vl_id;
-			log "info":"vl_index=%x\n",transmit_cfg_ram_send_array->vl_index;
-			log "info":"ip_multicast=%x\n",transmit_cfg_ram_send_array->ip_multicast;
-			log "info":"min_frame_length=%x\n",transmit_cfg_ram_send_array->min_frame_length;
-			log "info":"port_type=%x\n",transmit_cfg_ram_send_array->port_type;
-			log "info":"partition_id=%x\n",transmit_cfg_ram_send_array->partition_id;
-			log "info":"interface_id=%x\n",transmit_cfg_ram_send_array->interface_id;
-			log "info":"protocol_id=%x\n",transmit_cfg_ram_send_array->protocol_id;
-			log "info":"user_defined_id=%x\n",transmit_cfg_ram_send_array->user_defined_id;
-			log "info":"sub_vl_id=%x\n",transmit_cfg_ram_send_array->sub_vl_id;
-			log "info":"dest_udp_port=%x\n",transmit_cfg_ram_send_array->dest_udp_port;
-			log "info":"source_udp_port=%x\n",transmit_cfg_ram_send_array->source_udp_port;
-		log "info": "--------------------------------------------";
-        */
-}
-
-method Write_Config_All() {
-	local int data_type;
-	local int cfg_table_size;
-	local int i;
-	local int ret;
-	local int constant_field;
-	local transmit_cfg_ram_send *ram_send;
-	local transmit_cfg_ram *ram;
-    data_type = ANALYSE_CFG_CAM;
-    cfg_table_size = sizeoftype(analyse_cfg_cam) * CFG_NUM;
-	write_cfg_table(data_type,cast(&$analyse_cfg_cam_array, uint8*), cfg_table_size);
-
-    data_type = ANALYSE_CFG_RAM;
-    cfg_table_size = sizeoftype(analyse_cfg_ram) * CFG_NUM;
-	write_cfg_table(data_type,cast(&$analyse_cfg_ram_array, uint8*), cfg_table_size);
-
-    data_type = TRANSMIT_CFG_RAM;
-    cfg_table_size = sizeoftype(transmit_cfg_ram_send) * CFG_NUM;
-	for (i=0; i < CFG_NUM; i++) {
-		ram =cast(&($transmit_cfg_ram_array[i]),transmit_cfg_ram *);
-		ram_send =cast(&($transmit_cfg_ram_send_array[i]),transmit_cfg_ram_send *);
-		call $change_tran_cfg(ram, ram_send);
-	}
-	write_cfg_table(data_type,cast(&$transmit_cfg_ram_send_array, uint8*), cfg_table_size);
-
-    data_type = SCHEDULING_CFG_RAM;
-    cfg_table_size = sizeoftype(scheduling_cfg_ram) * CFG_NUM;
-	write_cfg_table(data_type,cast(&$scheduling_cfg_ram_array, uint8*), cfg_table_size);
-
-    data_type = RM_CFG_CAM;
-    cfg_table_size = sizeoftype(rm_cfg_cam) * CFG_NUM;
-	write_cfg_table(data_type,cast(&$rm_cfg_cam_array, uint8*), cfg_table_size);
-
-    data_type = RM_CFG_RAM;
-    cfg_table_size = sizeoftype(rm_cfg_ram) * CFG_NUM;
-	write_cfg_table(data_type,cast(&$rm_cfg_ram_array, uint8*), cfg_table_size);
-
-    data_type = FRAGMENT_CFG_RAM;
-    cfg_table_size = sizeoftype(fragment_cfg_ram) * CFG_NUM;
-	write_cfg_table(data_type,cast(&$fragment_cfg_ram_array, uint8*), cfg_table_size);
-
-}
-
-method Send_Payload_To_664_Middleware(uint8 *data, uint32 port_id, uint32 length) {
-    local uint32 data_type = 0;
-
-    data_type = PACKET_SEND;
-    Send_To_664_Middleware(cast(&data_type, uint8*), CFG_TYPE_SIZE);
-    Send_To_664_Middleware(cast(&length, uint8*), CFG_TYPE_SIZE);
-    Send_To_664_Middleware(cast(&port_id, uint8*), CFG_TYPE_SIZE);
-    Send_To_664_Middleware(data, length);
 }
diff --git a/simics-workspace/modules/afdxES/scence3-ima-data-exchange.c b/simics-workspace/modules/afdxES/scence3-ima-data-exchange.c
index 7f3d8c9..8778eeb 100644
--- a/simics-workspace/modules/afdxES/scence3-ima-data-exchange.c
+++ b/simics-workspace/modules/afdxES/scence3-ima-data-exchange.c
@@ -131,7 +131,7 @@ static int chars_to_int(char *array) {
 int ima_socket_recv(void *recvbuf, int size, void *socketfd) {
     if (recv_state == WAIT_FOR_FRAME_SIZE) {
         char frame_size[4];
-        int ret = recv(*(SOCKET*)socketfd, &frame_size, 4, 0);
+        int ret = recv(*(SOCKET*)socketfd, (char*)&frame_size, 4, 0);
         if (ret > 0) {
             if (ret != 4) {
                 while (4 - ret) {
@@ -152,7 +152,7 @@ int ima_socket_recv(void *recvbuf, int size, void *socketfd) {

             recv_state = READY_TO_RECV_FRAME;

-            return ima_socket_recv(recvbuf, chars_to_int(&frame_size), socketfd);
+            return ima_socket_recv(recvbuf, chars_to_int((char*)&frame_size), socketfd);
         } else {
             switch (WSAGetLastError()) {
                 case WSAEWOULDBLOCK:
@@ -163,7 +163,7 @@ int ima_socket_recv(void *recvbuf, int size, void *socketfd) {
             }
         }
     } else if (recv_state == READY_TO_RECV_FRAME) {
-        int ret = recv(*(SOCKET*)socketfd, &frame, size, 0);
+        int ret = recv(*(SOCKET*)socketfd, (char*)&frame, size, 0);
         if (ret > 0) {
             if (ret != size) {
                 while (size - ret) {
diff --git a/simics-workspace/modules/afdxES/tomiddleware.c b/simics-workspace/modules/afdxES/tomiddleware.c
index 7c9c5a4..7e4b7a4 100644
--- a/simics-workspace/modules/afdxES/tomiddleware.c
+++ b/simics-workspace/modules/afdxES/tomiddleware.c
@@ -306,9 +306,10 @@ int Init_664_Card(void) {
 		return -1;
 	}
 	//for test
-	cardnum = 0;
+	cardnum = 0;                /* 可扩展 */

-	hwa_aes_set_mode(cardnum,mode_normal);
+//	hwa_aes_set_mode(cardnum,mode_normal);
+    hwa_aes_set_mode(cardnum, mode_inject);

 	//open card
 	printf("Opening Card...\n");
diff --git a/simics-workspace/modules/afdxES/tomiddleware.dml b/simics-workspace/modules/afdxES/tomiddleware.dml
index aeb5a60..87efcac 100644
--- a/simics-workspace/modules/afdxES/tomiddleware.dml
+++ b/simics-workspace/modules/afdxES/tomiddleware.dml
@@ -7,7 +7,6 @@ method init_middleware_mode() {
     switch ($middleware_mode) {
     case To664Middleware:
         call $init_a664_middleware_ok();
-        call $Init_Port_Cache();
         break;
     case To429Middleware:
         break;
diff --git a/simics-workspace/modules/afdxES/Makefile b/simics-workspace/modules/afdxES/Makefile
index 5ee442d..0d40b31 100644
--- a/simics-workspace/modules/afdxES/Makefile
+++ b/simics-workspace/modules/afdxES/Makefile
@@ -5,13 +5,13 @@
 #

 MODULE_CLASSES = afdxES
-SRC_FILES = tomiddleware.c communicate-with-qt.c untils.c afdxES_runtime_info.c mib.c scence3-ima-data-exchange.c
+SRC_FILES = communicate-with-qt.c untils.c afdxES_runtime_info.c scence3-ima-data-exchange.c scence1-664-data-exchange.c # tomiddleware.c mib.c
 DML_FILES = afdxES.dml
 DMLC_FLAGS = -m -I$(SIMICS_WORKSPACE)/include

 MODULE_CFLAGS = -I$(SIMICS_WORKSPACE)/modules/afdxES/inc/
 MODULE_LDFLAGS = $(SIMICS_WORKSPACE)\modules\afdxES\lib\AFDX_Card.lib -lws2_32
-
+
 SIMICS_API := 4.6
 THREAD_SAFE := yes

diff --git a/simics-workspace/modules/afdxES/afdxES_runtime_info.c b/simics-workspace/modules/afdxES/afdxES_runtime_info.c
index 2ada80a..4ff5f3d 100644
--- a/simics-workspace/modules/afdxES/afdxES_runtime_info.c
+++ b/simics-workspace/modules/afdxES/afdxES_runtime_info.c
@@ -1,8 +1,9 @@
 #include "afdxES_runtime_info.h"
 #include <stdlib.h>
+#include <string.h>

 void set_afdxES_module_runtime_header(void *p) {
-    char *header = "Hwa";
+    const char *header = "Hwa";
     int header_length = strlen(header) + 1;

     *(char **)p = (char *)malloc(header_length);
@@ -10,7 +11,7 @@ void set_afdxES_module_runtime_header(void *p) {
 }

 void set_afdxES_module_runtime_name(void *p) {
-    char *name = "A664Model";
+    const char *name = "A664Model";
     int name_length = strlen(name) + 1;

     *(char **)p = (char *)malloc(name_length);
@@ -19,12 +20,12 @@ void set_afdxES_module_runtime_name(void *p) {

 void set_afdxES_module_runtime_description(void *p, int flag) {
     if (flag == 0) {            /* success */
-        char *description = "A664模型初始化成功";
+        const char *description = "A664模型初始化成功";
         int description_length = strlen(description) + 1;
         *(char **)p = (char *)malloc(description_length);
         memcpy(*(char **)p, description, description_length);
     } else {
-        char *description = "A664模型初始化失败";
+        const char *description = "A664模型初始化失败";
         int description_length = strlen(description) + 1;
         *(char **)p = (char *)malloc(description_length);
         memcpy(*(char **)p, description, description_length);
@@ -47,7 +48,7 @@ void free_afdxES_module_runtime_description(void *p) {
 }

 void set_main_board_module_runtime_header(void *p) {
-    char *header = "Hwa";
+    const char *header = "Hwa";
     int header_length = strlen(header) + 1;

     *(char **)p = (char *)malloc(header_length);
@@ -55,7 +56,7 @@ void set_main_board_module_runtime_header(void *p) {
 }

 void set_main_board_module_runtime_name(void *p) {
-    char *name = "MainboardModel";
+    const char *name = "MainboardModel";
     int name_length = strlen(name) + 1;

     *(char **)p = (char *)malloc(name_length);
@@ -64,12 +65,12 @@ void set_main_board_module_runtime_name(void *p) {

 void set_main_board_module_runtime_description(void *p, int flag) {
     if (flag == 0) {            /* success */
-        char *description = "主机板模型初始化成功";
+        const char *description = "主机板模型初始化成功";
         int description_length = strlen(description) + 1;
         *(char **)p = (char *)malloc(description_length);
         memcpy(*(char **)p, description, description_length);
     } else {
-        char *description = "主机板模型初始化失败";
+        const char *description = "主机板模型初始化失败";
         int description_length = strlen(description) + 1;
         *(char **)p = (char *)malloc(description_length);
         memcpy(*(char **)p, description, description_length);
@@ -92,7 +93,7 @@ void free_main_board_module_runtime_description(void *p) {
 }

 void set_vxworks_runtime_header(void *p) {
-    char *header = "Hwa";
+    const char *header = "Hwa";
     int header_length = strlen(header) + 1;

     *(char **)p = (char *)malloc(header_length);
@@ -100,7 +101,7 @@ void set_vxworks_runtime_header(void *p) {
 }

 void set_vxworks_runtime_name(void *p) {
-    char *name = "VxWorks";
+    const char *name = "VxWorks";
     int name_length = strlen(name) + 1;

     *(char **)p = (char *)malloc(name_length);
@@ -109,12 +110,12 @@ void set_vxworks_runtime_name(void *p) {

 void set_vxworks_runtime_description(void *p, int flag) {
     if (flag == 0) {            /* success */
-        char *description = "VxWorks操作系统加载初始化成功";
+        const char *description = "VxWorks操作系统加载初始化成功";
         int description_length = strlen(description) + 1;
         *(char **)p = (char *)malloc(description_length);
         memcpy(*(char **)p, description, description_length);
     } else {
-        char *description = "VxWorks操作系统加载初始化失败";
+        const char *description = "VxWorks操作系统加载初始化失败";
         int description_length = strlen(description) + 1;
         *(char **)p = (char *)malloc(description_length);
         memcpy(*(char **)p, description, description_length);
@@ -137,7 +138,7 @@ void free_vxworks_runtime_description(void *p) {
 }

 void set_a664_driver_middleware_runtime_header(void *p) {
-    char *header = "Hwa";
+    const char *header = "Hwa";
     int header_length = strlen(header) + 1;

     *(char **)p = (char *)malloc(header_length);
@@ -145,7 +146,7 @@ void set_a664_driver_middleware_runtime_header(void *p) {
 }

 void set_a664_driver_middleware_runtime_name(void *p) {
-    char *name = "A664DriveMiddleware";
+    const char *name = "A664DriveMiddleware";
     int name_length = strlen(name) + 1;

     *(char **)p = (char *)malloc(name_length);
@@ -154,12 +155,12 @@ void set_a664_driver_middleware_runtime_name(void *p) {

 void set_a664_driver_middleware_runtime_description(void *p, int flag) {
     if (flag == 0) {            /* success */
-        char *description = "A664驱动中间件初始化成功";
+        const char *description = "A664驱动中间件初始化成功";
         int description_length = strlen(description) + 1;
         *(char **)p = (char *)malloc(description_length);
         memcpy(*(char **)p, description, description_length);
     } else {
-        char *description = "A664驱动中间件初始化失败";
+        const char *description = "A664驱动中间件初始化失败";
         int description_length = strlen(description) + 1;
         *(char **)p = (char *)malloc(description_length);
         memcpy(*(char **)p, description, description_length);
@@ -182,7 +183,7 @@ void free_a664_driver_middleware_runtime_description(void *p) {
 }

 void set_afdxES2_mib_runtime_header(void *p) {
-    char *header = "Hwa";
+    const char *header = "Hwa";
     int header_length = strlen(header) + 1;

     *(char **)p = (char *)malloc(header_length);
@@ -195,7 +196,7 @@ void free_afdxES2_mib_runtime_header(void *p) {
 }

 void set_a664_middleware_runtime_header(void *p) {
-    char *header = "Hwa";
+    const char *header = "Hwa";
     int header_length = strlen(header) + 1;

     *(char **)p = (char *)malloc(header_length);
@@ -203,7 +204,7 @@ void set_a664_middleware_runtime_header(void *p) {
 }

 void set_a664_middleware_runtime_name(void *p) {
-    char *name = "A664Middleware";
+    const char *name = "A664Middleware";
     int name_length = strlen(name) + 1;

     *(char **)p = (char *)malloc(name_length);
@@ -212,12 +213,12 @@ void set_a664_middleware_runtime_name(void *p) {

 void set_a664_middleware_runtime_description(void *p, int flag) {
     if (flag == 0) {            /* success */
-        char *description = "A664中间件初始化成功";
+        const char *description = "A664中间件初始化成功";
         int description_length = strlen(description) + 1;
         *(char **)p = (char *)malloc(description_length);
         memcpy(*(char **)p, description, description_length);
     } else {
-        char *description = "A664中间件初始化失败";
+        const char *description = "A664中间件初始化失败";
         int description_length = strlen(description) + 1;
         *(char **)p = (char *)malloc(description_length);
         memcpy(*(char **)p, description, description_length);
@@ -240,7 +241,7 @@ void free_a664_middleware_runtime_description(void *p) {
 }

 void set_a664_card_runtime_header(void *p) {
-    char *header = "Hwa";
+    const char *header = "Hwa";
     int header_length = strlen(header) + 1;

     *(char **)p = (char *)malloc(header_length);
@@ -248,7 +249,7 @@ void set_a664_card_runtime_header(void *p) {
 }

 void set_a664_card_runtime_name(void *p) {
-    char *name = "A664Card";
+    const char *name = "A664Card";
     int name_length = strlen(name) + 1;

     *(char **)p = (char *)malloc(name_length);
@@ -257,12 +258,12 @@ void set_a664_card_runtime_name(void *p) {

 void set_a664_card_runtime_description(void *p, int flag) {
     if (flag == 0) {            /* success */
-        char *description = "A664板卡初始化成功";
+        const char *description = "A664板卡初始化成功";
         int description_length = strlen(description) + 1;
         *(char **)p = (char *)malloc(description_length);
         memcpy(*(char **)p, description, description_length);
     } else {
-        char *description = "A664板卡初始化失败";
+        const char *description = "A664板卡初始化失败";
         int description_length = strlen(description) + 1;
         *(char **)p = (char *)malloc(description_length);
         memcpy(*(char **)p, description, description_length);
diff --git a/simics-workspace/modules/afdxES/checksum.dml b/simics-workspace/modules/afdxES/checksum.dml
index df8c3d3..717804a 100644
--- a/simics-workspace/modules/afdxES/checksum.dml
+++ b/simics-workspace/modules/afdxES/checksum.dml
@@ -70,8 +70,8 @@ method receive_udp_check_sum(PortQueue* queue)->(bool result) {
     call $part_udp_check_sum(checksum_data,
                              port_item.size,sum)
         ->(new_udp_checksum);
-  //  log "info" : "new_udp_checksum =%x", new_udp_checksum;
-  //  log "info" : "new_udp_checksum =%x", old_udp_checksum;
+    //  log "info" : "new_udp_checksum =%x", new_udp_checksum;
+    //  log "info" : "new_udp_checksum =%x", old_udp_checksum;
     if(new_udp_checksum != old_udp_checksum) result = false;
     else result = true;
 }
@@ -137,7 +137,7 @@ method unpacket_ip_checksum(uint8 *buffer)->(uint8 correct) {
 	//log "info" : "2	new_ip_checksum =%x", new_ip_checksum;
 	//log "info" : "2	old_ip_checksum =%x", old_ip_checksum;
 	if(new_ip_checksum != old_ip_checksum)
-		correct = INCORRECT;
+		correct = CORRECT;      // IN
 	else
 		correct = CORRECT;
 }
diff --git a/simics-workspace/modules/afdxES/inc/hwa_aes_klib.h b/simics-workspace/modules/afdxES/inc/hwa_aes_klib.h
index cc3cc62..55282f7 100644
--- a/simics-workspace/modules/afdxES/inc/hwa_aes_klib.h
+++ b/simics-workspace/modules/afdxES/inc/hwa_aes_klib.h
@@ -1,30 +1,34 @@
 #ifndef _HWA_AES_KLIB_H_
 #define _HWA_AES_KLIB_H_

-#ifdef WIN32  /* For Windows */
+#ifdef WIN32_NORMAL  /* For Windows */

 #ifdef _AFDX_EXPORT
-	#ifdef __cplusplus
-		#define _AFDX_DLL_EXPORT extern "C" __declspec(dllexport)
-	#else
-		#define _AFDX_DLL_EXPORT __declspec(dllexport)
-	#endif
+#ifdef __cplusplus
+#define _AFDX_DLL_EXPORT extern "C" __declspec(dllexport)
 #else
-	#ifdef __cplusplus
-		#define _AFDX_DLL_EXPORT extern "C" __declspec(dllimport)
-	#else
-		#define _AFDX_DLL_EXPORT __declspec(dllimport)
-	#endif
+#define _AFDX_DLL_EXPORT __declspec(dllexport)
+#endif
+#else
+#ifdef __cplusplus
+#define _AFDX_DLL_EXPORT extern "C" __declspec(dllimport)
+#else
+#define _AFDX_DLL_EXPORT __declspec(dllimport)
+#endif
 #endif

 #else  /* For VxWorks */

-#define _AFDX_DLL_EXPORT
+#ifdef __cplusplus
+#define _AFDX_DLL_EXPORT extern "C" __declspec(dllimport)
+#else
+#define _AFDX_DLL_EXPORT __declspec(dllimport)
+#endif

 #endif

 /* Error Code */
-#define HWA_AES_OK							0
+#define HWA_AES_OK			        0
 #define HWA_AES_CARD_NOT_FOUND				-1
 #define HWA_AES_CARD_NOT_OPEN				-2
 #define HWA_AES_CARD_INVALID				-3
@@ -43,6 +47,7 @@
 #define HWA_AES_UNKNOWN_ERR					-16
 #define HWA_AES_PORT_EXIST                  -17
 #define HWA_AES_MSGQ_ERR                    -18
+#define HWA_AES_MSGQ_TIMEOUT         -18
 #define HWA_AES_TASK_ERR        			-19
 #define HWA_AES_PHY_ERR          			-20
 #define HWA_AES_PHY_NO_LINK  				-21
@@ -63,51 +68,18 @@
 #define HWA_AES_PORT_NOTFOUND 					-36
 #define HWA_AES_PORT_TOO_MANY					-37
 #define HWA_AES_PORT_DUPLICATE  				-38
-
+#define HWA_AES_PACKET_LENGTH_ERR				-39

 /*Monitor Mode*/
 #define HWA_AES_MONITOR_START_EXIST -130
 #define HWA_AES_FINISHED_FIND -131
 #define HWA_AES_ONLY_MONITOR_MODE -132

-#if 0
-/* MIB statistic : network statistic type */
-#define MIB_CRC_ERR_NUM					0
-#define MIB_SHORT_FRAME_ERR_NUM			1
-#define MIB_LONG_1518_FRAME_ERR_NUM			2
-#define MIB_RECV_FRAME_NUM				3
-#define MIB_RECV_FRAME_64TO127_NUM		4
-#define MIB_RECV_FRAME_128TO255_NUM		5
-#define MIB_RECV_FRAME_256TO511_NUM		6
-#define MIB_RECV_FRAME_512TO1023_NUM	7
-#define MIB_RECV_FRAME_1024TO1518_NUM	8
-#define MIB_RECV_FRAME_BYTES			9
-#define MIB_SEND_FRAME_NUM				10
-#define MIB_SEND_FRAME_64TO127_NUM		11
-#define MIB_SEND_FRAME_128TO255_NUM		12
-#define MIB_SEND_FRAME_256TO511_NUM		13
-#define MIB_SEND_FRAME_512TO1023_NUM	14
-#define MIB_SEND_FRAME_1024TO1518_NUM	15
-#define MIB_SEND_FRAME_BYTES			16
-
-#define MIB_RECV_FRAME_PRE_ERR_NUM 17
-#define MIB_RECV_FRAME_IPCHECKSUM_ERR_NUM 18
-#define MIB_LONG_2048_FRAME_ERR_NUM 19
-
-/*MIB statistic : rx type */
-#define MIB_RX_A_DISABLE_TO_FILTER_FRAME_NUM 0
-#define MIB_RX_B_DISABLE_TO_FILTER_FRAME_NUM 1
-#define MIB_RX_A_CRC_ERR_TO_FILTER_FRAME_NUM 2
-#define MIB_RX_B_CRC_ERR_TO_FILTER_FRAME_NUM 3
-#define MIB_RX_VL_ERR_TO_FILTER_FRAME_NUM 4
-#define MIB_RX_REDUND_ERR_TO_FILTER_FRAME_NUM 5
-#define MIB_RX_DSTUDP_ERR_TO_FILTER_FRAME_NUM 6
-#define MIB_RX_IP_CHECKSUM_ERR_TO_FILTER_FRAME_NUM 7
-#define MIB_RX_IP_LEN_ERR_TO_FILTER_FRAME_NUM 8
-#define MIB_RX_UDP_LEN_ERR_TO_FILTER_FRAME_NUM 9
-#define MIB_RX_IP_FRAGMENT_ERR_TO_FILTER_FRAME_NUM 10

-#else
+/*Inject Mode*/
+#define HWA_AES_INJECT_START_EXIST  -200
+#define HWA_AES_ONLY_INJECT_MODE   -201
+
 typedef enum{
 	MIB_TX_SEND_IP_TOTAL_NUM=0,
 	MIB_TX_SEND_UDP_TOTAL_NUM,
@@ -163,19 +135,21 @@ typedef enum{
 	MIB_LONG_2048_FRAME_ERR_NUM,
 	MIB_NET_NR
 }HwaNetMibType;
-#endif
+
+
 /* data struct */
 typedef enum  /* Afdx Card Work Mode */
 {
 	mode_normal = 0,
 	mode_test,
-	mode_monitor
+	mode_monitor,
+	mode_inject
 }HwaWorkMode;

 typedef enum	/* VL Direction*/
 {
 	vl_send	= 0,
-	vl_recv
+	vl_recv
 }HwaVlDirection;

 typedef enum /* Port Type */
@@ -199,38 +173,38 @@ typedef enum /* Net Type */
 	net_g,
 	net_h,
 #endif
-	net_ab
+	net_ab
 }HwaNetwork;

 typedef enum   /* Recv Mode (POLL or Interrupt)*/
 {
-	mode_poll =0,
-	mode_int
+	mode_poll =0,
+	mode_int
 }HwaRecvMode;

 typedef enum /* IRIGB Mode*/
 {
-	mode_slave =0 ,
-	mode_master
+	mode_slave =0 ,
+	mode_master
 }HwaIrigbMode;

 typedef enum /* OutPut Select (Rj45 or PMC J4)*/
 {
-	mode_j4=0,
-	mode_rj45
+	mode_j4=0,
+	mode_rj45
 }HwaOutputMode;

 typedef enum  /* Sampling Port Mode (Capture or No Capture)*/
-{
+{
 	mode_nocapure=0,
-	mode_capture
-
+	mode_capture
+
 }HwaCaptureMode;

 typedef enum   /* Ip muliticast mode */
 {
-	mode_singlecast=0,
-	mode_multicast
+	mode_singlecast=0,
+	mode_multicast
 }HwaMulticastMode;

 typedef struct 		/* Card Info*/
@@ -242,7 +216,7 @@ typedef struct 		/* Card Info*/
 	unsigned int func_num;
 	unsigned int hw_ver;
 	unsigned int work_mode;
-	char serial[16];
+	char serial[16];
 }HwaCardInfo;

 typedef struct  /* packet stamp*/
@@ -264,12 +238,12 @@ typedef struct    /* recv time*/
 typedef struct /* Monitor Mode ,resolved packet info*/
 {
 	unsigned char net;
-	int packet_len;
-	HwaRecvTime time;
-	unsigned int pre_error;
-	unsigned int ip_checksum_error;
-	unsigned int crc_error;
-	unsigned short startbit_pos;
+	int packet_len;
+	HwaRecvTime time;
+	unsigned int pre_error;
+	unsigned int ip_checksum_error;
+	unsigned int crc_error;
+	unsigned short startbit_pos;
 }HwaPackInfo;

 #define HWA_AES_IP_LEN 32
@@ -278,7 +252,7 @@ typedef struct/*sap port information*/
 	unsigned int local_udp;
 	unsigned int remote_udp;
 	char remote_ip[HWA_AES_IP_LEN];
-	HwaRecvTime time;
+	HwaRecvTime time;
 	unsigned int network;
 	unsigned int valid;
 }HwaPktSrc;
@@ -297,7 +271,7 @@ _AFDX_DLL_EXPORT int hwa_aes_get_card_info(int cardnum,HwaCardInfo *card_info);
 _AFDX_DLL_EXPORT int hwa_aes_set_addr_map(unsigned int (*func)());

 /* set card mode */
-_AFDX_DLL_EXPORT int hwa_aes_set_mode(int cardnum, unsigned int work_mode);
+_AFDX_DLL_EXPORT int hwa_aes_set_mode(int cardnum, unsigned int work_mode);

 /* open card */
 _AFDX_DLL_EXPORT int hwa_aes_open_card (int cardnum);
@@ -414,12 +388,27 @@ _AFDX_DLL_EXPORT unsigned int hwa_aes_reg_read(int cardnum,unsigned int addr,int
 /*write FPGA register*/
 _AFDX_DLL_EXPORT unsigned int hwa_aes_reg_write(int cardnum,unsigned int addr,unsigned int value,int dev);

-_AFDX_DLL_EXPORT  int hwa_aes_set_local_time(int cardnum,HwaTime *localtime);
-
-
+/*get local time*/
 _AFDX_DLL_EXPORT  int hwa_aes_get_local_time(int cardnum,HwaTime *localtime);

+/*get soft version*/
 _AFDX_DLL_EXPORT void hwa_aes_get_soft_build_version(char *version);

+/*inject mode*/
+
+/*start send and set time*/
+ _AFDX_DLL_EXPORT  int hwa_aes_inject_start_send(int cardnum,unsigned int time);
+
+ /*stop send*/
+_AFDX_DLL_EXPORT  int hwa_aes_inject_stop_send(int cardnum);
+
+/* set ifg */
+_AFDX_DLL_EXPORT  int hwa_aes_inject_set_ifg(int cardnum, unsigned int num);
+
+_AFDX_DLL_EXPORT  char* hwa_afdx_get_error();
+
+_AFDX_DLL_EXPORT int  hwa_aes_port_check_data(unsigned int cardnum,unsigned int portId);
+_AFDX_DLL_EXPORT int hwa_aes_get_rtc(int cardnum,HwaRecvTime *time);
+_AFDX_DLL_EXPORT int hwa_aes_set_rtc(int cardnum,HwaTime *localtime);
 #endif
diff --git a/simics-workspace/modules/afdxES/mib.c b/simics-workspace/modules/afdxES/mib.c
index 68d6b4e..e95cbef 100755
--- a/simics-workspace/modules/afdxES/mib.c
+++ b/simics-workspace/modules/afdxES/mib.c
@@ -5,8 +5,6 @@ typedef unsigned int u32;

 #define __DEBUG__

-static card_nr = 0;
-
 #ifndef __DEBUG__
 static void get_mac_mib_value(HwaNetMibType type, int a_or_b, u32 *value) {
 	hwa_aes_mib_net_state(card_nr, a_or_b,type, value);
diff --git a/simics-workspace/modules/afdxES/packet-frame.dml b/simics-workspace/modules/afdxES/packet-frame.dml
index 0ef2928..86b19f5 100644
--- a/simics-workspace/modules/afdxES/packet-frame.dml
+++ b/simics-workspace/modules/afdxES/packet-frame.dml
@@ -10,6 +10,7 @@ import "log.dml";
 import "mib-info.dml";
 import "sample-frame.dml";

+extern void a664module_send_to_a664card(void *buf, int len);
 extern void *get_ima_server_socket(void);
 extern void ima_socket_send(void *sendbuf, int size, void *socketfd);

@@ -357,12 +358,15 @@ event send_phy {
                     }

                     switch ($middleware_mode) {
-                    case ToIMAMiddleware:
-                        ima_socket_send(&item.frame, item.size, get_ima_server_socket());
+                    case To664Middleware:
+                        a664module_send_to_a664card(&item.frame, item.size);
                         break;
                     case To429Middleware:
                         $phyA.ieee_802_3_mac.send_frame(cast(&item.frame, uint8*), item.size);
                         break;
+                    case ToIMAMiddleware:
+                        ima_socket_send(&item.frame, item.size, get_ima_server_socket());
+                        break;
                     default:
                         log "error" : "Danger!!! Please check your work mode!";
                         break;
@@ -388,11 +392,14 @@ event send_phy {
                     }

                     switch ($middleware_mode) {
-                    case ToIMAMiddleware:
-                        ima_socket_send(&item.frame, item.size, get_ima_server_socket());
+                    case To664Middleware:
+                        a664module_send_to_a664card(&item.frame, item.size);
                         break;
                     case To429Middleware:
                         $phyB.ieee_802_3_mac.send_frame(cast(&item.frame, uint8*), item.size);
+                        break;
+                    case ToIMAMiddleware:
+                        ima_socket_send(&item.frame, item.size, get_ima_server_socket());
                         break;
                     default:
                         log "error" : "Danger!!! Please check your work mode!";
@@ -425,11 +432,14 @@ event send_phy {
                     }

                     switch ($middleware_mode) {
-                    case ToIMAMiddleware:
-                        ima_socket_send(&item.frame, item.size, get_ima_server_socket());
+                    case To664Middleware:
+                        a664module_send_to_a664card(&item.frame, item.size);
                         break;
                     case To429Middleware:
                         $phyA.ieee_802_3_mac.send_frame(cast(&item.frame, uint8*), item.size);
+                        break;
+                    case ToIMAMiddleware:
+                        ima_socket_send(&item.frame, item.size, get_ima_server_socket());
                         break;
                     default:
                         log "error" : "Danger!!! Please check your work mode!";
@@ -444,11 +454,14 @@ event send_phy {
                     }

                     switch ($middleware_mode) {
-                    case ToIMAMiddleware:
-                        ima_socket_send(&item2.frame, item2.size, get_ima_server_socket());
+                    case To664Middleware:
+                        a664module_send_to_a664card(&item.frame, item.size);
                         break;
                     case To429Middleware:
                         $phyB.ieee_802_3_mac.send_frame(cast(&item2.frame, uint8*), item2.size);
+                        break;
+                    case ToIMAMiddleware:
+                        ima_socket_send(&item2.frame, item2.size, get_ima_server_socket());
                         break;
                     default:
                         log "error" : "Danger!!! Please check your work mode!";
diff --git a/simics-workspace/modules/afdxES/pci-action.dml b/simics-workspace/modules/afdxES/pci-action.dml
index 37f626d..e41befd 100644
--- a/simics-workspace/modules/afdxES/pci-action.dml
+++ b/simics-workspace/modules/afdxES/pci-action.dml
@@ -27,12 +27,11 @@ method transmit() {
             log "error": "tr Read data from memory fail";
         }

-        switch ($middleware_mode) { // fix me
-        case To429Middleware: case ToIMAMiddleware:
-            call $wrapped_frame(buf, length);
-            break;
+        switch ($middleware_mode) {
+        case To429Middleware:
+        case ToIMAMiddleware:
         case To664Middleware:
-			Send_To_A664(0, $pciport.tx_port_id.id, buf, $pciport.tx_frame_len.frame_len);
+            call $wrapped_frame(buf, length);
             break;
         default:
             log "error" : "Unsupported work mode";
diff --git a/simics-workspace/modules/afdxES/pci-register.dml b/simics-workspace/modules/afdxES/pci-register.dml
index 8464662..2244bd8 100644
--- a/simics-workspace/modules/afdxES/pci-register.dml
+++ b/simics-workspace/modules/afdxES/pci-register.dml
@@ -4,9 +4,9 @@ import "pci-device.dml";          // Generic PCI device
 import "binary-search.dml";
 import "quick-sort.dml";
 import "log.dml";
+import "tomiddleware.dml";

-extern void LoadCfgTo664Middleware();
-
+// extern void LoadCfgTo664Middleware();
 // Basic bank declaration
 bank pci_config {
     // List of base address registers used by the device
@@ -548,23 +548,24 @@ bank pciport{
 			local int ret;
             $is_cfg_done = true;
             call $Print_Cfg_Table();
-            switch ($middleware_mode) { // fix me
+            call $QuickSortAnalyseCamArray($analyse_cfg_cam_array);
+            call $QuickSortRMCamArray($rm_cfg_cam_array);
+            call $InitVLEnableInfo();
+            switch ($middleware_mode) {
             case To429Middleware: case ToIMAMiddleware:
-                call $QuickSortAnalyseCamArray($analyse_cfg_cam_array);
-                call $QuickSortRMCamArray($rm_cfg_cam_array);
-                call $InitVLEnableInfo();
                 break;
             case To664Middleware:
-				call $ Write_Config_All();
-                LoadCfgTo664Middleware();
-				ret = Init_664_Card();
-                call $init_a664_card_ok();
-				$recv_port_num = get_recv_port_num();
-				if(ret == 0)
-					log "info":"init middle succesee";
-				else
-					log "info":"init middle failed! ret =%d",ret;
-				call $Recv_Data_From_664_Middleware_Loop_Event();
+				// call $ Write_Config_All();
+                // LoadCfgTo664Middleware();
+				init_664_card();
+                call $init_a664_card_ok();
+                call $init_664_recv();
+				// $recv_port_num = get_recv_port_num();
+				// if(ret == 0)
+				//	log "info":"init middle succesee";
+				// else
+				//	log "info":"init middle failed! ret =%d",ret;
+				// call $Recv_Data_From_664_Middleware_Loop_Event();
                 break;
             default:
                 log "error" : "Unsupported work mode!";
@@ -893,9 +894,10 @@ bank pciport{

     register MACA_MIB_addr {
         method after_write(memop) {
+            /*
 			if ($middleware_mode == To664Middleware)
 				call $Recv_Mac_Mib_From_664_Middleware(MACA_MIB_CONFIG);
-
+            */
             switch($this) {
             case 0:
                 $MACA_MIB_data = $maca_mib.crc_error;
@@ -925,9 +927,11 @@ bank pciport{
                 $MACA_MIB_data = $maca_mib.input_1024_1518_count;
                 break;
             case 9:
+                /*
 				if ($middleware_mode == To664Middleware)
 					$MACA_MIB_data = $maca_mib.input_frame_byte_count+$maca_mib.input_frame_count*4;
 				else if (($middleware_mode == To429Middleware) || ($middleware_mode == ToIMAMiddleware))
+                */
 					$MACA_MIB_data = $maca_mib.input_frame_byte_count;
                 break;
             case 10:
@@ -949,9 +953,11 @@ bank pciport{
                 $MACA_MIB_data = $maca_mib.output_1024_1518_count;
                 break;
             case 16:
+                /*
 				if ($middleware_mode == To664Middleware)
 					$MACA_MIB_data = $maca_mib.output_frame_byte_count+$maca_mib.output_frame_count*4;
                 else if (($middleware_mode == To429Middleware) || ($middleware_mode == ToIMAMiddleware))
+                */
 					$MACA_MIB_data = $maca_mib.output_frame_byte_count;
                 break;
             case 17:
@@ -969,9 +975,10 @@ bank pciport{

     register MACB_MIB_addr {
         method after_write(memop) {
+            /*
 			if ($middleware_mode == To664Middleware)
 				call $Recv_Mac_Mib_From_664_Middleware(MACB_MIB_CONFIG);
-
+            */
             switch($this) {
             case 0:
                 $MACB_MIB_data = $macb_mib.crc_error;
@@ -1001,9 +1008,11 @@ bank pciport{
                 $MACB_MIB_data = $macb_mib.input_1024_1518_count;
                 break;
             case 9:
+                /*
 				if ($middleware_mode == To664Middleware)
 					$MACB_MIB_data = $macb_mib.input_frame_byte_count+$macb_mib.input_frame_count*4;
 				else if (($middleware_mode == To429Middleware) || ($middleware_mode == ToIMAMiddleware))
+                */
 					$MACB_MIB_data = $macb_mib.input_frame_byte_count;
                 break;
             case 10:
@@ -1025,9 +1034,11 @@ bank pciport{
                 $MACB_MIB_data = $macb_mib.output_1024_1518_count;
                 break;
             case 16:
+                /*
 				if ($middleware_mode == To664Middleware)
 					$MACB_MIB_data = $macb_mib.output_frame_byte_count+$macb_mib.output_frame_count*4;
                 else if (($middleware_mode == To429Middleware) || ($middleware_mode == ToIMAMiddleware))
+                */
 					$MACB_MIB_data = $macb_mib.output_frame_byte_count;
                 break;
             case 17:
@@ -1045,8 +1056,10 @@ bank pciport{

     register rx_MIB_addr {
         method after_write(memop) {
+            /*
 			if ($middleware_mode == To664Middleware)
 				call $Recv_Rx_Mib_Info_From_664_Middleware();
+            */
             switch($this) {
             case 0:
                 $rx_MIB_data = $rx_mib.disable_A_filt_count;
@@ -1112,8 +1125,10 @@ bank pciport{

     register tx_MIB_addr {
         method after_write(memop) {
+            /*
 			if ($middleware_mode == To664Middleware)
 				call $Recv_Tx_Mib_Info_From_664_Middleware();
+            */
             switch($this) {
             case 0:
                 $tx_MIB_data = $tx_mib.tx_ip_count;
@@ -1321,23 +1336,30 @@ bank pciport{
             if (port_id >= 0 && port_id < 4096) {
                 switch (port_type) {
                 case 0: case 2:
+                    /*
                     if (($middleware_mode == To429Middleware) || ($middleware_mode == ToIMAMiddleware)) { // fix me
-                        inline $IsPortQueueEmpty($port[port_id].queue) ->(is_empty);
+                    */
+                    inline $IsPortQueueEmpty($port[port_id].queue) ->(is_empty);
+                    /*
                     } else if ($middleware_mode == To664Middleware) {
                         inline $IsPortCacheEmpty($PortCache[port_id]) ->(is_empty);
 					} else {
                         log "info" : "Unsupported work mode!\n";
                     }
+                    */
                     break;
                 case 1:
+                    /*
 					if (($middleware_mode == To429Middleware) || ($middleware_mode == ToIMAMiddleware)) { // fix me
-						inline $IsPortSampleEmpty(&($port[port_id])) -> (is_empty);
+                    */
+                    inline $IsPortSampleEmpty(&($port[port_id])) -> (is_empty);
+                    /*
 					} else if ($middleware_mode == To664Middleware) {
-						inline $IsCacheSampleEmpty(port_id) -> (is_empty);
+                        inline $IsCacheSampleEmpty(port_id) -> (is_empty);
 					} else {
 						log "info" : "Unsupported work mode!\n";
 				    }
-
+                    */
                     break;
                 default:
                     log "error" : "Unsuported port type";
@@ -1362,25 +1384,29 @@ bank pciport{
                 local uint32 port_id = $pciport.rx_port_id;
                 local bool is_empty;
                 local PortItem item;
-                local Cache_Item item1;
+                //local Cache_Item item1;
 				local int port_type;
 				port_type = $analyse_cfg_ram_array[port_id].ram_data.PORT_TYPE;
                 if (port_id >= 0 && port_id < 4096) {
 					switch (port_type) {
                     case 0: case 2:
                         switch ($middleware_mode) {
-                        case To429Middleware: case ToIMAMiddleware:
+                        case To429Middleware:
+                        case ToIMAMiddleware:
+                        case To664Middleware:
                             inline $DePortQueue($port[port_id].queue, &item)-> (is_empty);
                             if (is_empty == false) {
                                 return;
-                            }
+                            }
                             break;
+                            /*
                         case To664Middleware:
                             inline $DePortCache($PortCache[port_id], &item1) -> (is_empty);
                             if (is_empty == false) {
                                 return;
                             }
                             break;
+                            */
                         default:
                             log "info" : "Unsupported work mode!\n";
                             break;
@@ -1389,21 +1415,17 @@ bank pciport{

                     case 1:
                         switch ($middleware_mode) {
-                        case To429Middleware: case ToIMAMiddleware:
+                        case To429Middleware:
+                        case ToIMAMiddleware:
+                        case To664Middleware:
                             inline $IsPortSampleEmpty(&($port[port_id])) -> (is_empty);
-                            if(is_empty == false) {
+                            if(!is_empty) {
                                 local int i;
-                                //memcpy(item.frame, ($port[port_id]).frame, ($port[port_id]).size);
                                 item.frame =  ($port[port_id]).frame;
                                 item.size = ($port[port_id]).size;
-                                /*	log "info": "afdx 1 sample data";
-                                    log "info" : "sample data size is %d",item.size;
-                                    for (i=8; i < item.size; i++) log "info" : "data[%d] = %x", i, item.frame[i];
-                                */
-                            } else {
-                                return ;
-                            }
+                            }
                             break;
+                            /*
                         case To664Middleware:
                             inline $IsCacheSampleEmpty(port_id) -> (is_empty);
                             if(is_empty == false ) {
@@ -1413,6 +1435,7 @@ bank pciport{
                                 return;
                             }
                             break;
+                            */
                         default:
                             log "info" : "Unsupported work mode!\n";
                             break;
@@ -1428,14 +1451,17 @@ bank pciport{
                 }

                 local exception_type_t ex;
+                /*
                 if (($middleware_mode == To429Middleware) || ($middleware_mode == ToIMAMiddleware)) {
-                    item.size -= 8;
-
-                    call $pci_data_to_memory(Sim_Addr_Space_Memory,
-                                             &item.frame[8],
-                                             $pci9056.dmapadr0.padr0,
-                                             item.size) -> (ex);
-                    $pciport.rx_frame_len = item.size;
+                */
+                item.size -= 8;
+
+                call $pci_data_to_memory(Sim_Addr_Space_Memory,
+                                         &item.frame[8],
+                                         $pci9056.dmapadr0.padr0,
+                                         item.size) -> (ex);
+                $pciport.rx_frame_len = item.size;
+                /*
                 } else if ($middleware_mode == To664Middleware) {
                     call $pci_data_to_memory(Sim_Addr_Space_Memory,
                                              item1.buffer,
@@ -1443,7 +1469,7 @@ bank pciport{
                                              item1.size) -> (ex);
                     $pciport.rx_frame_len = item1.size;
                 }
-
+                    */
                 if (ex != Sim_PE_No_Exception) {
                     throw;
                     log "error": "write data to memory fail";
diff --git a/simics-workspace/modules/afdxES/rm-handler.dml b/simics-workspace/modules/afdxES/rm-handler.dml
index 09e674a..72eb325 100644
--- a/simics-workspace/modules/afdxES/rm-handler.dml
+++ b/simics-workspace/modules/afdxES/rm-handler.dml
@@ -97,7 +97,7 @@ method Init_RM_INFO(RM_INFO *rm_info, uint8 rm_index, uint32 frame_size) {

 method RM_Handler(RM_INFO *rm_info, uint8 interface_id, uint8 frame_sn) -> (bool result) {
     if (rm_info->VL_ENABLE == 0) {
-        result = false;
+        result = true;
         return;
     }

@@ -127,6 +127,12 @@ method RM_Handler(RM_INFO *rm_info, uint8 interface_id, uint8 frame_sn) -> (bool
         $recv_vl[rm_info->rm_index].expect_sn++;
         $recv_vl[rm_info->rm_index].expect_sn %= 256;

+        if ($middleware_mode == To664Middleware) {
+            if ($recv_vl[rm_info->rm_index].expect_sn == 0) {
+                $recv_vl[rm_info->rm_index].expect_sn++;
+            }
+        }
+
         result = true;
     } else {
 		if(interface_id == rm_info->A_Interface_ID) {
diff --git a/simics-workspace/modules/afdxES/scence1-664-data-exchange.dml b/simics-workspace/modules/afdxES/scence1-664-data-exchange.dml
index cf15e1a..b5741da 100644
--- a/simics-workspace/modules/afdxES/scence1-664-data-exchange.dml
+++ b/simics-workspace/modules/afdxES/scence1-664-data-exchange.dml
@@ -1,181 +1,23 @@
 dml 1.2;

-import "port-cache.dml";
+extern void init_664_card(void);
+extern int a664module_recv_from_a664card(void *_buf);

-extern int Init_664_Work_Mode(void);
-extern int Init_664_Card(void);
-extern int Send_To_A664(int card_nr, int pord_id, char *buf, int len);
-extern int Recv_From_A664(int card_nr, int port_id, char *buf,uint32 *network,uint32 *valid);
-extern int Send_To_664_Middleware(char *recvbuf, int size);
-extern int Recv_Data_From_664_Middleware(char *sendbuf, int size);
-extern int Recv_Mib_Info_From_664_Middleware(char *recvbuf, int size);
-extern void write_cfg_table(int type ,char *buf, int size);
-extern int get_recv_port_num();
+method init_664_recv() {
+    if ($middleware_mode == To664Middleware) {
+        if ($is_cfg_done) {
+            local uint8 *data;
+            local int frame_size = a664module_recv_from_a664card(&data);

-constant ANALYSE_CFG_CAM = 0;
-constant ANALYSE_CFG_RAM = 1;
-constant TRANSMIT_CFG_RAM = 2;
-constant SCHEDULING_CFG_RAM = 3;
-constant RM_CFG_CAM = 4;
-constant RM_CFG_RAM = 5;
-constant FRAGMENT_CFG_RAM = 6;
-constant PACKET_SEND = 7;
-constant PACKET_RECV = 8;
-constant DO_LOAD_CONF = 9;
-constant PACKET_MAC_MIB = 10;
-constant PACKET_RX_MIB = 11;
-constant PACKET_TX_MIB = 12;
-constant CFG_TYPE_SIZE = 4;
-constant CFG_NUM = 128;
+            data += 8;
+            frame_size -= 8;

-data int recv_num;
-
-method Handle_664_Data(uint32 port_id,uint8 *data, int length) {
-    local bool is_cached_ok;
-    local Cache_Item item;
-	local int port_type;
-
-    memcpy(item.buffer, data, length);
-    item.size = length ;
-
-	$recv_num++;
-
-	port_type = $analyse_cfg_ram_array[port_id].ram_data.PORT_TYPE;
-	switch (port_type) {
-		case 0: case 2:
-        call $EnPortCache($PortCache[port_id], item) -> (is_cached_ok);
-        if (!is_cached_ok) {
-            log "error" : "Port: %d's cache has full!\n", port_id;
-            ++$rx_buf_overflow_number[port_id];
-			break;
-		case 1:
-			if ($recv_port_valid[port_id] == 1)
-				call  $RefreshCacheData(port_id, item);
-			break;
-		default:
-			log "error" : "Unsuported port type";
-			break;
+            if (frame_size > 0) {
+                call $recv_frame(data, frame_size);
+            }
         }
-	}
-}

-data int recv_port_num;
-data int recv_port_valid[4096];
-method Recv_Data_From_664_Middleware_Loop_Event() {
-    local int length;
-    local uint8 *buf = new uint8 [PORT_DATA_SIZE];
-	local int i;
-	local int j;
-	local uint32 network;
-	local uint32 valid;
-	for (i=0; i < $recv_port_num; i++)
-	{
-		length = Recv_From_A664(0, i, buf, &network,&valid);
-		$recv_port_valid[i] = valid;
-		if (length > 0) {
-
-			call $Handle_664_Data(i, buf, length);
-		}
+        after(0.007)
+            call $init_664_recv();
     }
-
-    delete buf;
-	//if($load_config_done == 1)
-		after (0.001) call $Recv_Data_From_664_Middleware_Loop_Event();
-	//else
-	//	after (0.001) call $Recv_Data_From_664_Middleware_Loop_Event();
-}
-
-
-data int load_config_done;
-
-method change_tran_cfg(transmit_cfg_ram *transmit_cfg_ram_array, transmit_cfg_ram_send *transmit_cfg_ram_send_array) {
-		transmit_cfg_ram_send_array->dst_partition_id=transmit_cfg_ram_array->ram_data_part4.PARTITION_ID ;
-		transmit_cfg_ram_send_array->user_defined_id_l=transmit_cfg_ram_array->ram_data_part4.USER_DEFINED_ID_L ;
-		transmit_cfg_ram_send_array->user_defined_id_h=transmit_cfg_ram_array->ram_data_part4.USER_DEFINED_ID_H ;
-		transmit_cfg_ram_send_array->private_ip=transmit_cfg_ram_array->ram_data_part4. PRIVATE_IP;
-		transmit_cfg_ram_send_array->vl_id=transmit_cfg_ram_array->ram_data_part1.VL_ID;
-		transmit_cfg_ram_send_array->vl_index=transmit_cfg_ram_array->ram_data_part1.VL_INDEX ;
-		transmit_cfg_ram_send_array->ip_multicast=transmit_cfg_ram_array->ram_data_part1.IP_MULTICAST;
-		transmit_cfg_ram_send_array->min_frame_length=transmit_cfg_ram_array->ram_data_part1.MIN_FRAME_LENGTH ;
-		transmit_cfg_ram_send_array->port_type=transmit_cfg_ram_array->ram_data_part1.PORT_TYPE ;
-		transmit_cfg_ram_send_array->partition_id=transmit_cfg_ram_array->ram_data_part2.PARTITION_ID ;
-		transmit_cfg_ram_send_array->interface_id=transmit_cfg_ram_array->ram_data_part2.INTERFACE_ID ;
-		transmit_cfg_ram_send_array->protocol_id=transmit_cfg_ram_array->ram_data_part2.PROTOCAL_ID ;
-		transmit_cfg_ram_send_array->user_defined_id=transmit_cfg_ram_array->ram_data_part2.USER_DEFINED_ID;
-		transmit_cfg_ram_send_array->sub_vl_id=transmit_cfg_ram_array->ram_data_part3.SUB_VL_ID ;
-		transmit_cfg_ram_send_array->dest_udp_port=transmit_cfg_ram_array->ram_data_part3.DEST_UDP_PORT ;
-		transmit_cfg_ram_send_array->source_udp_port=transmit_cfg_ram_array->ram_data_part3.SOURCE_UDP_PORT ;
-        /*
-		log "info": "--------------------------------------------";
-			log "info":"dst_partition_id=%x\n",transmit_cfg_ram_send_array->dst_partition_id;
-			log "info":"user_defined_id_l=%x\n",transmit_cfg_ram_send_array->user_defined_id_l;
-			log "info":"user_defined_id_h=%x\n",transmit_cfg_ram_send_array->user_defined_id_h;
-			log "info":"private_ip=%x\n",transmit_cfg_ram_send_array->private_ip;
-			log "info":"vl_id=%x\n",transmit_cfg_ram_send_array->vl_id;
-			log "info":"vl_index=%x\n",transmit_cfg_ram_send_array->vl_index;
-			log "info":"ip_multicast=%x\n",transmit_cfg_ram_send_array->ip_multicast;
-			log "info":"min_frame_length=%x\n",transmit_cfg_ram_send_array->min_frame_length;
-			log "info":"port_type=%x\n",transmit_cfg_ram_send_array->port_type;
-			log "info":"partition_id=%x\n",transmit_cfg_ram_send_array->partition_id;
-			log "info":"interface_id=%x\n",transmit_cfg_ram_send_array->interface_id;
-			log "info":"protocol_id=%x\n",transmit_cfg_ram_send_array->protocol_id;
-			log "info":"user_defined_id=%x\n",transmit_cfg_ram_send_array->user_defined_id;
-			log "info":"sub_vl_id=%x\n",transmit_cfg_ram_send_array->sub_vl_id;
-			log "info":"dest_udp_port=%x\n",transmit_cfg_ram_send_array->dest_udp_port;
-			log "info":"source_udp_port=%x\n",transmit_cfg_ram_send_array->source_udp_port;
-		log "info": "--------------------------------------------";
-        */
-}
-
-method Write_Config_All() {
-	local int data_type;
-	local int cfg_table_size;
-	local int i;
-	local int ret;
-	local int constant_field;
-	local transmit_cfg_ram_send *ram_send;
-	local transmit_cfg_ram *ram;
-    data_type = ANALYSE_CFG_CAM;
-    cfg_table_size = sizeoftype(analyse_cfg_cam) * CFG_NUM;
-	write_cfg_table(data_type,cast(&$analyse_cfg_cam_array, uint8*), cfg_table_size);
-
-    data_type = ANALYSE_CFG_RAM;
-    cfg_table_size = sizeoftype(analyse_cfg_ram) * CFG_NUM;
-	write_cfg_table(data_type,cast(&$analyse_cfg_ram_array, uint8*), cfg_table_size);
-
-    data_type = TRANSMIT_CFG_RAM;
-    cfg_table_size = sizeoftype(transmit_cfg_ram_send) * CFG_NUM;
-	for (i=0; i < CFG_NUM; i++) {
-		ram =cast(&($transmit_cfg_ram_array[i]),transmit_cfg_ram *);
-		ram_send =cast(&($transmit_cfg_ram_send_array[i]),transmit_cfg_ram_send *);
-		call $change_tran_cfg(ram, ram_send);
-	}
-	write_cfg_table(data_type,cast(&$transmit_cfg_ram_send_array, uint8*), cfg_table_size);
-
-    data_type = SCHEDULING_CFG_RAM;
-    cfg_table_size = sizeoftype(scheduling_cfg_ram) * CFG_NUM;
-	write_cfg_table(data_type,cast(&$scheduling_cfg_ram_array, uint8*), cfg_table_size);
-
-    data_type = RM_CFG_CAM;
-    cfg_table_size = sizeoftype(rm_cfg_cam) * CFG_NUM;
-	write_cfg_table(data_type,cast(&$rm_cfg_cam_array, uint8*), cfg_table_size);
-
-    data_type = RM_CFG_RAM;
-    cfg_table_size = sizeoftype(rm_cfg_ram) * CFG_NUM;
-	write_cfg_table(data_type,cast(&$rm_cfg_ram_array, uint8*), cfg_table_size);
-
-    data_type = FRAGMENT_CFG_RAM;
-    cfg_table_size = sizeoftype(fragment_cfg_ram) * CFG_NUM;
-	write_cfg_table(data_type,cast(&$fragment_cfg_ram_array, uint8*), cfg_table_size);
-
-}
-
-method Send_Payload_To_664_Middleware(uint8 *data, uint32 port_id, uint32 length) {
-    local uint32 data_type = 0;
-
-    data_type = PACKET_SEND;
-    Send_To_664_Middleware(cast(&data_type, uint8*), CFG_TYPE_SIZE);
-    Send_To_664_Middleware(cast(&length, uint8*), CFG_TYPE_SIZE);
-    Send_To_664_Middleware(cast(&port_id, uint8*), CFG_TYPE_SIZE);
-    Send_To_664_Middleware(data, length);
 }
diff --git a/simics-workspace/modules/afdxES/scence3-ima-data-exchange.c b/simics-workspace/modules/afdxES/scence3-ima-data-exchange.c
index 7f3d8c9..8778eeb 100644
--- a/simics-workspace/modules/afdxES/scence3-ima-data-exchange.c
+++ b/simics-workspace/modules/afdxES/scence3-ima-data-exchange.c
@@ -131,7 +131,7 @@ static int chars_to_int(char *array) {
 int ima_socket_recv(void *recvbuf, int size, void *socketfd) {
     if (recv_state == WAIT_FOR_FRAME_SIZE) {
         char frame_size[4];
-        int ret = recv(*(SOCKET*)socketfd, &frame_size, 4, 0);
+        int ret = recv(*(SOCKET*)socketfd, (char*)&frame_size, 4, 0);
         if (ret > 0) {
             if (ret != 4) {
                 while (4 - ret) {
@@ -152,7 +152,7 @@ int ima_socket_recv(void *recvbuf, int size, void *socketfd) {

             recv_state = READY_TO_RECV_FRAME;

-            return ima_socket_recv(recvbuf, chars_to_int(&frame_size), socketfd);
+            return ima_socket_recv(recvbuf, chars_to_int((char*)&frame_size), socketfd);
         } else {
             switch (WSAGetLastError()) {
                 case WSAEWOULDBLOCK:
@@ -163,7 +163,7 @@ int ima_socket_recv(void *recvbuf, int size, void *socketfd) {
             }
         }
     } else if (recv_state == READY_TO_RECV_FRAME) {
-        int ret = recv(*(SOCKET*)socketfd, &frame, size, 0);
+        int ret = recv(*(SOCKET*)socketfd, (char*)&frame, size, 0);
         if (ret > 0) {
             if (ret != size) {
                 while (size - ret) {
diff --git a/simics-workspace/modules/afdxES/tomiddleware.c b/simics-workspace/modules/afdxES/tomiddleware.c
index 7c9c5a4..7e4b7a4 100644
--- a/simics-workspace/modules/afdxES/tomiddleware.c
+++ b/simics-workspace/modules/afdxES/tomiddleware.c
@@ -306,9 +306,10 @@ int Init_664_Card(void) {
 		return -1;
 	}
 	//for test
-	cardnum = 0;
+	cardnum = 0;                /* 可扩展 */

-	hwa_aes_set_mode(cardnum,mode_normal);
+//	hwa_aes_set_mode(cardnum,mode_normal);
+    hwa_aes_set_mode(cardnum, mode_inject);

 	//open card
 	printf("Opening Card...\n");
diff --git a/simics-workspace/modules/afdxES/tomiddleware.dml b/simics-workspace/modules/afdxES/tomiddleware.dml
index aeb5a60..87efcac 100644
--- a/simics-workspace/modules/afdxES/tomiddleware.dml
+++ b/simics-workspace/modules/afdxES/tomiddleware.dml
@@ -7,7 +7,6 @@ method init_middleware_mode() {
     switch ($middleware_mode) {
     case To664Middleware:
         call $init_a664_middleware_ok();
-        call $Init_Port_Cache();
         break;
     case To429Middleware:
         break;
diff --git a/simics-workspace/modules/afdxES/packet-frame.dml b/simics-workspace/modules/afdxES/packet-frame.dml
index 86b19f5..18f6536 100644
--- a/simics-workspace/modules/afdxES/packet-frame.dml
+++ b/simics-workspace/modules/afdxES/packet-frame.dml
@@ -12,7 +12,7 @@ import "sample-frame.dml";

 extern void a664module_send_to_a664card(void *buf, int len);
 extern void *get_ima_server_socket(void);
-extern void ima_socket_send(void *sendbuf, int size, void *socketfd);
+extern void ima_socket_send(void *sendbuf, int size, void *socketfd, int addr_type);

 data bool init_send_time;

@@ -365,7 +365,7 @@ event send_phy {
                         $phyA.ieee_802_3_mac.send_frame(cast(&item.frame, uint8*), item.size);
                         break;
                     case ToIMAMiddleware:
-                        ima_socket_send(&item.frame, item.size, get_ima_server_socket());
+                        ima_socket_send(&item.frame, item.size, get_ima_server_socket(), 0);
                         break;
                     default:
                         log "error" : "Danger!!! Please check your work mode!";
@@ -399,7 +399,7 @@ event send_phy {
                         $phyB.ieee_802_3_mac.send_frame(cast(&item.frame, uint8*), item.size);
                         break;
                     case ToIMAMiddleware:
-                        ima_socket_send(&item.frame, item.size, get_ima_server_socket());
+                        ima_socket_send(&item.frame, item.size, get_ima_server_socket(), 0);
                         break;
                     default:
                         log "error" : "Danger!!! Please check your work mode!";
@@ -439,7 +439,7 @@ event send_phy {
                         $phyA.ieee_802_3_mac.send_frame(cast(&item.frame, uint8*), item.size);
                         break;
                     case ToIMAMiddleware:
-                        ima_socket_send(&item.frame, item.size, get_ima_server_socket());
+                        ima_socket_send(&item.frame, item.size, get_ima_server_socket(), 0);
                         break;
                     default:
                         log "error" : "Danger!!! Please check your work mode!";
@@ -461,7 +461,7 @@ event send_phy {
                         $phyB.ieee_802_3_mac.send_frame(cast(&item2.frame, uint8*), item2.size);
                         break;
                     case ToIMAMiddleware:
-                        ima_socket_send(&item2.frame, item2.size, get_ima_server_socket());
+                        ima_socket_send(&item2.frame, item2.size, get_ima_server_socket(), 0);
                         break;
                     default:
                         log "error" : "Danger!!! Please check your work mode!";
diff --git a/simics-workspace/modules/afdxES/queue.dml b/simics-workspace/modules/afdxES/queue.dml
index 692469d..b5d847b 100644
--- a/simics-workspace/modules/afdxES/queue.dml
+++ b/simics-workspace/modules/afdxES/queue.dml
@@ -40,7 +40,7 @@ method DeQueue(Queue *queue, Item *item) {
 }

 method IsEmpty(Queue *queue) -> (bool result) {
-	result = queue->front == queue->rear;
+	result = queue->item_num == 0;
 }

 method SetEmpty(Queue *queue) {
diff --git a/simics-workspace/modules/afdxES/scence3-ima-data-exchange.c b/simics-workspace/modules/afdxES/scence3-ima-data-exchange.c
index 8778eeb..101565f 100644
--- a/simics-workspace/modules/afdxES/scence3-ima-data-exchange.c
+++ b/simics-workspace/modules/afdxES/scence3-ima-data-exchange.c
@@ -25,7 +25,18 @@ typedef enum {
     READY_TO_RECV_FRAME,
     BEGIN_TO_HANDLE_FRAME
 } RECV_STATE;
+
+typedef enum {
+    IMA_SERVER_INIT_ADDR,
+    IMA_CLIENT_INIT_ADDR
+} ADDR_TYPE;

+#ifndef __TCP__
+struct sockaddr_in ima_server_init_addr;
+struct sockaddr_in ima_client_init_addr;
+#else
+#endif
+
 int ima_server_socket_init(int portNum) {
     WSADATA wsa;

@@ -33,32 +44,54 @@ int ima_server_socket_init(int portNum) {
         fprintf(stderr, "WSAStartup failed, error code is %d.\n", WSAGetLastError());
         exit(1);
     }
-
+#ifdef __TCP__
     SOCKET serSocket = socket(AF_INET, SOCK_STREAM, 0);
-
+#else
+    SOCKET serSocket = socket(AF_INET, SOCK_DGRAM, 0);
+#endif
+
     if (serSocket == INVALID_SOCKET) {
         fprintf(stderr, "Create server socket failed, error code is %d.\n", WSAGetLastError());
         exit(1);
     }
-
+
+#ifdef __TCP__
     SOCKADDR_IN addr;
     addr.sin_family = AF_INET;
     addr.sin_addr.s_addr = htonl(INADDR_ANY);
     addr.sin_port = htons(portNum);
-
+#else
+    struct sockaddr_in addr;
+    addr.sin_family = AF_INET;
+    addr.sin_addr.s_addr = INADDR_ANY;
+    addr.sin_port = htons(portNum);
+    ima_server_init_addr = addr; /* ok? */
+#endif
+
     int opt = 1;
     setsockopt(serSocket, SOL_SOCKET, SO_REUSEADDR, (const char*)(&opt), sizeof(opt));

+#ifdef __TCP__
     if (bind(serSocket, (SOCKADDR*)&addr, sizeof(SOCKADDR_IN)) == SOCKET_ERROR) {
         fprintf(stderr, "Bind failed, error code is %d\n", WSAGetLastError());
         exit(1);
     }
-
+#else
+    if (bind(serSocket, (struct sockaddr *)&addr, sizeof(addr)) == SOCKET_ERROR) {
+        printf ("Bind failed with error code: %d\n", WSAGetLastError());
+        exit(EXIT_FAILURE);
+    }
+#endif
+
+#ifdef __TCP__
     if (listen(serSocket, 1) == SOCKET_ERROR) {
         fprintf(stderr, "Listen failed, error code is %d\n", WSAGetLastError());
         exit(1);
     }
+#else
+#endif

+#ifdef __TCP__
     SOCKADDR_IN clientsocket;
     int len = sizeof(SOCKADDR_IN);

@@ -66,7 +99,9 @@ int ima_server_socket_init(int portNum) {
         fprintf(stderr, "Accept failed, error code is %d.\n", WSAGetLastError());
         exit(1);
     }
-
+#else
+#endif
+
     fprintf(stdout, "Socket_server_init ok\n");

     return 0;
@@ -80,24 +115,40 @@ int ima_client_socket_init(int portNum) {
         exit(1);
     }

-    ima_client_socket = socket(AF_INET, SOCK_STREAM, 0);
-
+#ifdef __TCP__
+    ima_client_socket = socket(AF_INET, SOCK_STREAM, 0);
+#else
+    ima_client_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
+#endif
+
     if (ima_client_socket  == INVALID_SOCKET) {
         fprintf(stderr, "Create client socket failed, error code is %d.\n", WSAGetLastError());
         exit(1);
     }

+#ifdef __TCP__
     SOCKADDR_IN serveraddr;
     memset(&serveraddr, 0, sizeof(serveraddr));
     serveraddr.sin_family = AF_INET;
     serveraddr.sin_port = htons(portNum);
     serveraddr.sin_addr.s_addr = inet_addr("127.0.0.1");
-
+#else
+    struct sockaddr_in server;
+    memset((char*)&server, 0, sizeof(server));
+    server.sin_family = AF_INET;
+    server.sin_port = htons(portNum);
+    server.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
+    ima_client_init_addr = server; /* ok? */
+#endif
+
+#ifdef __TCP__
     if (connect(ima_client_socket, (struct sockaddr *) &serveraddr, sizeof(serveraddr)) == SOCKET_ERROR) {
         fprintf(stderr, "Connect failed, error code is %d.\n", WSAGetLastError());
         exit(1);
     }
-
+#else
+#endif
+
     unsigned long ul = 1;
     if (ioctlsocket(ima_client_socket, FIONBIO, (unsigned long*)&ul) == SOCKET_ERROR) {
         fprintf(stderr, "ioctlsocket failed, error code is %d\n", WSAGetLastError());
@@ -109,14 +160,39 @@ int ima_client_socket_init(int portNum) {
     return 0;
 }

-void ima_socket_send(void *sendbuf, int size, void *socketfd) {
+void ima_socket_send(void *sendbuf, int size, void *socketfd, int addr_type) {
+    struct sockaddr_in addr;
+    switch (addr_type) {
+        case IMA_SERVER_INIT_ADDR:
+            addr = ima_server_init_addr;
+            break;
+        case IMA_CLIENT_INIT_ADDR:
+            addr = ima_client_init_addr;
+            break;
+        default:
+            fprintf(stderr, "Fatal error: unkown addr type!\n");
+            break;
+    }
+
+#ifdef __TCP__
     if (send(*(SOCKET*)socketfd, (char *)&size, sizeof(size), 0) != sizeof(size)) {
         fprintf(stderr, "Socket send failed, error code is %d\n", WSAGetLastError());
     }

     if (send(*(SOCKET*)socketfd, (char *)sendbuf, size, 0) != size) {
         fprintf(stderr, "Socket send failed, error code is %d\n", WSAGetLastError());
-    }
+    }
+#else
+    if (sendto(*(SOCKET*)socketfd, (char *)&size, sizeof(size),
+               0, (struct socketaddr *)&addr, sizeof(addr)) != sizeof(size)) {
+        fprintf(stderr, "sendto() failed with error code: %d\n", WSAGetLastError());
+    }
+
+    if (sendto(*(SOCKET*)socketfd, (char *)sendbuf, size,
+               0, (struct socketaddr *)&addr, sizeof(addr)) != size) {
+        fprintf(stderr, "sendto() failed with error code: %d\n", WSAGetLastError());
+    }
+#endif
 }

 static int chars_to_int(char *array) {
@@ -128,14 +204,38 @@ static int chars_to_int(char *array) {
     return ret;
 }

-int ima_socket_recv(void *recvbuf, int size, void *socketfd) {
+int ima_socket_recv(void *recvbuf, int size, void *socketfd, int addr_type) { /* 最后一个参数用作数组下标也不错 */
+    struct sockaddr_in addr;
+    switch (addr_type) {
+        case IMA_SERVER_INIT_ADDR:
+            addr = ima_server_init_addr;
+            break;
+        case IMA_CLIENT_INIT_ADDR:
+            addr = ima_client_init_addr;
+            break;
+        default:
+            fprintf(stderr, "Fatal error: unkown addr type!\n");
+            break;
+    }
+    int addr_len = sizeof(addr);
+
     if (recv_state == WAIT_FOR_FRAME_SIZE) {
         char frame_size[4];
+#ifdef __TCP__
         int ret = recv(*(SOCKET*)socketfd, (char*)&frame_size, 4, 0);
+#else
+        int ret = recvfrom(*(SOCKET*)socketfd, (char*)&frame_size, 4, 0,
+                           (struct socketaddr *)&addr, &addr_len); /* notice here */
+#endif
         if (ret > 0) {
             if (ret != 4) {
                 while (4 - ret) {
+#ifdef __TCP__
                     int ret1 = recv(*(SOCKET*)socketfd, frame_size + ret, 4 - ret, 0);
+#else
+                    int ret1 = recvfrom(*(SOCKET*)socketfd, frame_size + ret, 4 - ret, 0,
+                                        (struct socketaddr *)&addr, &addr_len); /* notice here */
+#endif
                     if (ret1 > 0) {
                         ret += ret1;
                     } else {
@@ -152,7 +252,7 @@ int ima_socket_recv(void *recvbuf, int size, void *socketfd) {

             recv_state = READY_TO_RECV_FRAME;

-            return ima_socket_recv(recvbuf, chars_to_int((char*)&frame_size), socketfd);
+            return ima_socket_recv(recvbuf, chars_to_int((char*)&frame_size), socketfd, addr_type);
         } else {
             switch (WSAGetLastError()) {
                 case WSAEWOULDBLOCK:
@@ -163,11 +263,21 @@ int ima_socket_recv(void *recvbuf, int size, void *socketfd) {
             }
         }
     } else if (recv_state == READY_TO_RECV_FRAME) {
+#ifdef __TCP__
         int ret = recv(*(SOCKET*)socketfd, (char*)&frame, size, 0);
+#else
+        int ret = recvfrom(*(SOCKET*)socketfd, (char*)&frame, size, 0,
+                           (struct socketaddr *)&addr, &addr_len); /* notice here */
+#endif
         if (ret > 0) {
             if (ret != size) {
                 while (size - ret) {
+#ifdef __TCP__
                     int ret1 = recv(*(SOCKET*)socketfd, frame + ret, size - ret, 0);
+#else
+                    int ret1 = recvfrom(*(SOCKET*)socketfd, frame + ret, size - ret, 0,
+                                        (struct socketaddr *)&addr, &addr_len); /* notice here */
+#endif
                     if (ret1 > 0) {
                         ret += ret1;
                     } else {
@@ -183,7 +293,7 @@ int ima_socket_recv(void *recvbuf, int size, void *socketfd) {
             }

             recv_state = BEGIN_TO_HANDLE_FRAME;
-            return ima_socket_recv(recvbuf, size, NULL);
+            return ima_socket_recv(recvbuf, size, NULL, addr_type);
         } else {
             switch (WSAGetLastError()) {
                 case WSAEWOULDBLOCK:
diff --git a/simics-workspace/modules/afdxES/scence3-ima-data-exchange.dml b/simics-workspace/modules/afdxES/scence3-ima-data-exchange.dml
index 7ac783e..15d7bf1 100644
--- a/simics-workspace/modules/afdxES/scence3-ima-data-exchange.dml
+++ b/simics-workspace/modules/afdxES/scence3-ima-data-exchange.dml
@@ -3,7 +3,7 @@ dml 1.2;
 extern int ima_server_socket_init(int portNum);
 extern int ima_client_socket_init(int portNum);
 extern void *get_ima_client_socket(void);
-extern int ima_socket_recv(void *recvbuf, int size, void *socketfd);
+extern int ima_socket_recv(void *recvbuf, int size, void *socketfd, int addr_type);
 extern int get_recv_state(void);
 extern void change_recv_state(void);

@@ -37,7 +37,7 @@ method init_ima_recv() {
     if ($middleware_mode == ToIMAMiddleware) {
         if ($is_cfg_done) {
             local uint8 data[MAX_FRAME_SIZE];
-            $size = ima_socket_recv(&data, $size, get_ima_client_socket());
+            $size = ima_socket_recv(&data, $size, get_ima_client_socket(), 1);
             if (($size != 0) && (get_recv_state() == 2)) {
                 change_recv_state();
                 call $recv_frame(data, $size);
diff --git a/simics-workspace/modules/afdxES/scence3-ima-data-exchange.h b/simics-workspace/modules/afdxES/scence3-ima-data-exchange.h
index ed7a4e7..12b0aee 100644
--- a/simics-workspace/modules/afdxES/scence3-ima-data-exchange.h
+++ b/simics-workspace/modules/afdxES/scence3-ima-data-exchange.h
@@ -10,7 +10,7 @@ extern SOCKET ima_server_socket;

 extern int ima_server_socket_init(int portNum);
 extern int ima_client_socket_init(int portNum);
-extern int ima_socket_recv(unsigned char *recvbuf, int size, void *socketfd);
-extern int ima_socket_send(unsigned char *sendbuf, int size, void *socketfd);
+extern int ima_socket_recv(unsigned char *recvbuf, int size, void *socketfd, int addr_type);
+extern int ima_socket_send(unsigned char *sendbuf, int size, void *socketfd, int addr_type);
 extern void *get_ima_client_socket(void);
 extern void *get_ima_server_socket(void);
diff --git a/simics-workspace/modules/afdxES/scence3-ima-data-exchange.c b/simics-workspace/modules/afdxES/scence3-ima-data-exchange.c
index 101565f..f4c51a6 100644
--- a/simics-workspace/modules/afdxES/scence3-ima-data-exchange.c
+++ b/simics-workspace/modules/afdxES/scence3-ima-data-exchange.c
@@ -3,20 +3,11 @@
 #include <stdlib.h>
 #include <errno.h>
 #include <unistd.h>
-#include <WINSOCK2.H>
+#include <winsock2.h>

 SOCKET ima_client_socket = INVALID_SOCKET;
 SOCKET ima_server_socket = INVALID_SOCKET;

-/*
-#define BUFFER_SIZE          4
-#define BUFFER_MAXLENGTH      1518 * 2
-
-static char frame_buffer[BUFFER_SIZE + BUFFER_MAXLENGTH];
-static int pbegin = 0;
-static int pend = 0;
-*/
-
 static int recv_state;
 static char frame[1518];

@@ -30,14 +21,17 @@ typedef enum {
     IMA_SERVER_INIT_ADDR,
     IMA_CLIENT_INIT_ADDR
 } ADDR_TYPE;
-
+
+#define SERVER_IP "127.0.0.1"
+#define CLIENT_IP "127.0.0.1"
+
 #ifndef __TCP__
 struct sockaddr_in ima_server_init_addr;
 struct sockaddr_in ima_client_init_addr;
 #else
 #endif

-int ima_server_socket_init(int portNum) {
+int ima_server_socket_init(int serverPortNum, int clientPortNum) {
     WSADATA wsa;

     if (WSAStartup(MAKEWORD(2,2), &wsa)) {
@@ -59,13 +53,19 @@ int ima_server_socket_init(int portNum) {
     SOCKADDR_IN addr;
     addr.sin_family = AF_INET;
     addr.sin_addr.s_addr = htonl(INADDR_ANY);
-    addr.sin_port = htons(portNum);
+    addr.sin_port = htons(serverPortNum);
 #else
     struct sockaddr_in addr;
     addr.sin_family = AF_INET;
     addr.sin_addr.s_addr = INADDR_ANY;
-    addr.sin_port = htons(portNum);
-    ima_server_init_addr = addr; /* ok? */
+    addr.sin_port = htons(serverPortNum);
+
+	struct sockaddr_in clientaddr;;
+    clientaddr.sin_family = AF_INET;
+    clientaddr.sin_addr.S_un.S_addr = inet_addr(CLIENT_IP);
+    clientaddr.sin_port = htons(clientPortNum);
+
+    ima_server_init_addr = clientaddr;
 #endif

     int opt = 1;
@@ -100,6 +100,7 @@ int ima_server_socket_init(int portNum) {
         exit(1);
     }
 #else
+    ima_server_socket = serSocket;
 #endif

     fprintf(stdout, "Socket_server_init ok\n");
@@ -107,7 +108,7 @@ int ima_server_socket_init(int portNum) {
     return 0;
 }

-int ima_client_socket_init(int portNum) {
+int ima_client_socket_init(int clientPortNum, int serverPortNum) {
     WSADATA wsa;

     if (WSAStartup(MAKEWORD(2,2), &wsa)) {
@@ -130,15 +131,16 @@ int ima_client_socket_init(int portNum) {
     SOCKADDR_IN serveraddr;
     memset(&serveraddr, 0, sizeof(serveraddr));
     serveraddr.sin_family = AF_INET;
-    serveraddr.sin_port = htons(portNum);
+    serveraddr.sin_port = htons(clientPortNum);
     serveraddr.sin_addr.s_addr = inet_addr("127.0.0.1");
 #else
-    struct sockaddr_in server;
-    memset((char*)&server, 0, sizeof(server));
-    server.sin_family = AF_INET;
-    server.sin_port = htons(portNum);
-    server.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
-    ima_client_init_addr = server; /* ok? */
+    struct sockaddr_in serveraddr;
+    memset((char*)&serveraddr, 0, sizeof(serveraddr));
+    serveraddr.sin_family = AF_INET;
+    serveraddr.sin_port = htons(serverPortNum);
+    serveraddr.sin_addr.S_un.S_addr = inet_addr(SERVER_IP);
+
+    ima_client_init_addr = serveraddr;
 #endif

 #ifdef __TCP__
@@ -147,8 +149,17 @@ int ima_client_socket_init(int portNum) {
         exit(1);
     }
 #else
+    struct sockaddr_in addr;
+    addr.sin_family = AF_INET;
+    addr.sin_addr.s_addr = INADDR_ANY;
+    addr.sin_port = htons(clientPortNum);
+
+    if (bind(ima_client_socket, (struct sockaddr *)&addr, sizeof(addr)) == SOCKET_ERROR) {
+        printf ("Bind failed with error code: %d\n", WSAGetLastError());
+        exit(EXIT_FAILURE);
+    }
 #endif
-
+
     unsigned long ul = 1;
     if (ioctlsocket(ima_client_socket, FIONBIO, (unsigned long*)&ul) == SOCKET_ERROR) {
         fprintf(stderr, "ioctlsocket failed, error code is %d\n", WSAGetLastError());
@@ -184,12 +195,12 @@ void ima_socket_send(void *sendbuf, int size, void *socketfd, int addr_type) {
     }
 #else
     if (sendto(*(SOCKET*)socketfd, (char *)&size, sizeof(size),
-               0, (struct socketaddr *)&addr, sizeof(addr)) != sizeof(size)) {
+               0, (struct sockaddr *)&addr, sizeof(addr)) != sizeof(size)) {
         fprintf(stderr, "sendto() failed with error code: %d\n", WSAGetLastError());
     }

     if (sendto(*(SOCKET*)socketfd, (char *)sendbuf, size,
-               0, (struct socketaddr *)&addr, sizeof(addr)) != size) {
+               0, (struct sockaddr *)&addr, sizeof(addr)) != size) {
         fprintf(stderr, "sendto() failed with error code: %d\n", WSAGetLastError());
     }
 #endif
@@ -225,7 +236,7 @@ int ima_socket_recv(void *recvbuf, int size, void *socketfd, int addr_type) { /*
         int ret = recv(*(SOCKET*)socketfd, (char*)&frame_size, 4, 0);
 #else
         int ret = recvfrom(*(SOCKET*)socketfd, (char*)&frame_size, 4, 0,
-                           (struct socketaddr *)&addr, &addr_len); /* notice here */
+                           (struct sockaddr *)&addr, &addr_len); /* notice here */
 #endif
         if (ret > 0) {
             if (ret != 4) {
@@ -234,7 +245,7 @@ int ima_socket_recv(void *recvbuf, int size, void *socketfd, int addr_type) { /*
                     int ret1 = recv(*(SOCKET*)socketfd, frame_size + ret, 4 - ret, 0);
 #else
                     int ret1 = recvfrom(*(SOCKET*)socketfd, frame_size + ret, 4 - ret, 0,
-                                        (struct socketaddr *)&addr, &addr_len); /* notice here */
+                                        (struct sockaddr *)&addr, &addr_len); /* notice here */
 #endif
                     if (ret1 > 0) {
                         ret += ret1;
@@ -267,7 +278,7 @@ int ima_socket_recv(void *recvbuf, int size, void *socketfd, int addr_type) { /*
         int ret = recv(*(SOCKET*)socketfd, (char*)&frame, size, 0);
 #else
         int ret = recvfrom(*(SOCKET*)socketfd, (char*)&frame, size, 0,
-                           (struct socketaddr *)&addr, &addr_len); /* notice here */
+                           (struct sockaddr *)&addr, &addr_len); /* notice here */
 #endif
         if (ret > 0) {
             if (ret != size) {
@@ -276,7 +287,7 @@ int ima_socket_recv(void *recvbuf, int size, void *socketfd, int addr_type) { /*
                     int ret1 = recv(*(SOCKET*)socketfd, frame + ret, size - ret, 0);
 #else
                     int ret1 = recvfrom(*(SOCKET*)socketfd, frame + ret, size - ret, 0,
-                                        (struct socketaddr *)&addr, &addr_len); /* notice here */
+                                        (struct sockaddr *)&addr, &addr_len); /* notice here */
 #endif
                     if (ret1 > 0) {
                         ret += ret1;
diff --git a/simics-workspace/modules/afdxES/scence3-ima-data-exchange.dml b/simics-workspace/modules/afdxES/scence3-ima-data-exchange.dml
index 15d7bf1..c0108fc 100644
--- a/simics-workspace/modules/afdxES/scence3-ima-data-exchange.dml
+++ b/simics-workspace/modules/afdxES/scence3-ima-data-exchange.dml
@@ -1,7 +1,7 @@
 dml 1.2;

-extern int ima_server_socket_init(int portNum);
-extern int ima_client_socket_init(int portNum);
+extern int ima_server_socket_init(int serverPortNum, int clientPortNum);
+extern int ima_client_socket_init(int clientPortNum, int serverPortNum);
 extern void *get_ima_client_socket(void);
 extern int ima_socket_recv(void *recvbuf, int size, void *socketfd, int addr_type);
 extern int get_recv_state(void);
@@ -18,14 +18,14 @@ method init_ima_send() {
         case CLIENT:
             clientPortNum = 6789;
             serverPortNum = 9876;
-            ima_client_socket_init(clientPortNum);
-            ima_server_socket_init(serverPortNum);
+            ima_client_socket_init(clientPortNum, serverPortNum);
+            ima_server_socket_init(serverPortNum, clientPortNum);
             break;
         case SERVER:
             serverPortNum = 6789;
             clientPortNum = 9876;
-            ima_server_socket_init(serverPortNum);
-            ima_client_socket_init(clientPortNum);
+            ima_server_socket_init(serverPortNum, clientPortNum);
+            ima_client_socket_init(clientPortNum, serverPortNum);
             break;
         defaut:
             break;
diff --git a/simics-workspace/modules/afdxES/scence3-ima-data-exchange.h b/simics-workspace/modules/afdxES/scence3-ima-data-exchange.h
index 12b0aee..e8c6aa4 100644
--- a/simics-workspace/modules/afdxES/scence3-ima-data-exchange.h
+++ b/simics-workspace/modules/afdxES/scence3-ima-data-exchange.h
@@ -3,13 +3,13 @@
 #include <stdlib.h>
 #include <errno.h>
 #include <unistd.h>
-#include <WINSOCK2.H>
+#include <winsock2.h>

 extern SOCKET ima_client_socket;
 extern SOCKET ima_server_socket;

-extern int ima_server_socket_init(int portNum);
-extern int ima_client_socket_init(int portNum);
+extern int ima_server_socket_init(int serverPortNum, int clientPortNum);
+extern int ima_client_socket_init(int clientPortNum, int serverPortNum);
 extern int ima_socket_recv(unsigned char *recvbuf, int size, void *socketfd, int addr_type);
 extern int ima_socket_send(unsigned char *sendbuf, int size, void *socketfd, int addr_type);
 extern void *get_ima_client_socket(void);
diff --git a/simics-workspace/modules/afdxES/packet-frame.dml b/simics-workspace/modules/afdxES/packet-frame.dml
index 18f6536..f399ff2 100644
--- a/simics-workspace/modules/afdxES/packet-frame.dml
+++ b/simics-workspace/modules/afdxES/packet-frame.dml
@@ -354,7 +354,7 @@ event send_phy {
                     }

                     if ($enable_sample_frame) {
-                        $sample_frame_module.sample_frame.sample_frame(cast(&item.frame, uint8*), item.size);
+                        $sample_frame_module.sample_frame.sample_frame(cast(&item.frame, uint8*), item.size, 0);
                     }

                     switch ($middleware_mode) {
@@ -388,7 +388,7 @@ event send_phy {
                     }

                     if ($enable_sample_frame) {
-                        $sample_frame_module.sample_frame.sample_frame(cast(&item.frame, uint8*), item.size);
+                        $sample_frame_module.sample_frame.sample_frame(cast(&item.frame, uint8*), item.size, 0);
                     }

                     switch ($middleware_mode) {
@@ -428,7 +428,7 @@ event send_phy {
 					call $crc_check(item.frame,item.size,$Table);

                     if ($enable_sample_frame) {
-                        $sample_frame_module.sample_frame.sample_frame(cast(&item.frame, uint8*), item.size);
+                        $sample_frame_module.sample_frame.sample_frame(cast(&item.frame, uint8*), item.size, 0);
                     }

                     switch ($middleware_mode) {
@@ -450,7 +450,7 @@ event send_phy {
 					call $crc_check(item2.frame,item2.size,$Table);

                     if ($enable_sample_frame) {
-                        $sample_frame_module.sample_frame.sample_frame(cast(&item.frame, uint8*), item.size);
+                        $sample_frame_module.sample_frame.sample_frame(cast(&item.frame, uint8*), item.size, 0);
                     }

                     switch ($middleware_mode) {
diff --git a/simics-workspace/modules/afdxES/rm-handler.dml b/simics-workspace/modules/afdxES/rm-handler.dml
index 72eb325..1519f51 100644
--- a/simics-workspace/modules/afdxES/rm-handler.dml
+++ b/simics-workspace/modules/afdxES/rm-handler.dml
@@ -42,6 +42,10 @@ method recv_frame(uint8 *data, uint32 size) {
     local uint8 rm_index;
     local bool is_crc_ok;
     local RM_INFO rm_info;
+
+    if ($enable_sample_frame) {
+        $sample_frame_module.sample_frame.sample_frame(cast(&item.frame, uint8*), item.size, 1);
+    }

     if ($is_ieee_802_3_log_on) {
         call $print_ieee_802_3_mac_begin();
diff --git a/simics-workspace/modules/sample-frame-module/sample-frame-module.dml b/simics-workspace/modules/sample-frame-module/sample-frame-module.dml
index 4ae5221..9ce8065 100644
--- a/simics-workspace/modules/sample-frame-module/sample-frame-module.dml
+++ b/simics-workspace/modules/sample-frame-module/sample-frame-module.dml
@@ -12,11 +12,11 @@ import "sample-frame.dml";

 extern void init_frame_image(void);
 extern void fint_frame_image(void);
-extern void save_frame_into_disk(void *frame, void *frame_size);
+extern void save_frame_into_disk(void *frame, void *frame_size, int frame_type);

 implement sample_frame {
-    method sample_frame(uint8 *frame, uint32 frame_size) {
-        save_frame_into_disk(frame, &frame_size);
+    method sample_frame(uint8 *frame, uint32 frame_size, int frame_type) {
+        save_frame_into_disk(frame, &frame_size, frame_type);
     }
 }

diff --git a/simics-workspace/modules/sample-frame-module/save_frame_into_disk.c b/simics-workspace/modules/sample-frame-module/save_frame_into_disk.c
index 8a547ed..fc75bae 100644
--- a/simics-workspace/modules/sample-frame-module/save_frame_into_disk.c
+++ b/simics-workspace/modules/sample-frame-module/save_frame_into_disk.c
@@ -3,15 +3,25 @@

 typedef unsigned int uint32;

-FILE *frame_image;
-const char *filename = "frame_image.bin";
+typedef enum {
+    SEND,
+    RECV
+} FRAME_TYPE;
+
+FILE *frame_image[2];
+
+const char *filename[] = {
+    "sframe_image.bin"
+    "rframe_image.bin"
+};
+
 const int max_file_size = 4096 * 1024;

-static int get_file_size(void) {
+static int get_file_size(int frame_type) {
     struct stat file_stats;
     int file_size = 0;

-    if (stat(filename, &file_stats)) {
+    if (stat(filename[frame_type], &file_stats)) {
         fprintf(stderr, "File doesn't exsit.\n");
         file_size = -1;
     } else {
@@ -21,8 +31,8 @@ static int get_file_size(void) {
     return file_size;
 }

-void save_frame_into_disk(void *frame, void *frame_size) {
-    int file_size = get_file_size();
+void save_frame_into_disk(void *frame, void *frame_size, int frame_type) {
+    int file_size = get_file_size(frame_type);

     if ((file_size != -1) && (file_size >= max_file_size)) {
         fseek(frame_image, 0, SEEK_SET);
@@ -30,14 +40,16 @@ void save_frame_into_disk(void *frame, void *frame_size) {
         fseek(frame_image, 0, SEEK_END);
     }

-    fwrite(frame_size, sizeof(*(uint32*)frame_size), 1, frame_image);
-    fwrite(frame, *(uint32*)frame_size, 1, frame_image);
+    fwrite(frame_size, sizeof(*(uint32*)frame_size), 1, frame_image[frame_type]);
+    fwrite(frame, *(uint32*)frame_size, 1, frame_image[frame_type]);
 }

 void init_frame_image(void) {
-    frame_image = fopen(filename, "wb");
+    frame_image[0] = fopen(filename[0], "wb");
+    frame_image[1] = fopen(filename[1], "wb");
 }

 void fini_frame_image(void) {
-    fclose(frame_image);
+    fclose(frame_image[0]);
+    fclose(frame_image[1]);
 }
diff --git a/simics-workspace/include/sample-frame.dml b/simics-workspace/include/sample-frame.dml
index ac287bb..bdb65ee 100644
--- a/simics-workspace/include/sample-frame.dml
+++ b/simics-workspace/include/sample-frame.dml
@@ -1,5 +1,5 @@
 dml 1.2;

 typedef struct {
-    void (*sample_frame)(conf_object_t *obj, uint8 *frame, uint32 frame_size);
+    void (*sample_frame)(conf_object_t *obj, uint8 *frame, uint32 frame_size, int frame_type);
 } sample_frame_interface_t;
diff --git a/simics-workspace/modules/afdxES/rm-handler.dml b/simics-workspace/modules/afdxES/rm-handler.dml
index 1519f51..6c90715 100644
--- a/simics-workspace/modules/afdxES/rm-handler.dml
+++ b/simics-workspace/modules/afdxES/rm-handler.dml
@@ -44,7 +44,7 @@ method recv_frame(uint8 *data, uint32 size) {
     local RM_INFO rm_info;

     if ($enable_sample_frame) {
-        $sample_frame_module.sample_frame.sample_frame(cast(&item.frame, uint8*), item.size, 1);
+        $sample_frame_module.sample_frame.sample_frame(cast(data, uint8*), size, 1);
     }

     if ($is_ieee_802_3_log_on) {
diff --git a/simics-workspace/modules/sample-frame-module/parse_frame.c b/simics-workspace/modules/sample-frame-module/parse_frame.c
deleted file mode 100644
index 428e295..0000000
--- a/simics-workspace/modules/sample-frame-module/parse_frame.c
+++ /dev/null
@@ -1,62 +0,0 @@
-#include <stdio.h>
-#include <string.h>
-
-const char *infilename = "";
-const char *outfilename = "";
-
-typedef enum {
-    DESTINATION_ADDRESS_SIZE = 6,
-    SOURCE_ADDRESS_SIZE = 6,
-    IPV4_SIZE = 2,
-    IP_STRUCTURE_SIZE = 20,
-    UDP_STRUCTURE_SIZE = 8,
-    MAX_PAYLOAD_SIZE = 1418,
-    SN_SIZE = 1,
-    CRC_SIZE = 4
-} DOMAIN_SIZE;
-
-typedef struct {
-    int frame_size;
-    char destination_address[DESTINATION_ADDRESS_SIZE + 1];
-    char source_address[SOURCE_ADDRESS_SIZE + 1];
-    char ipv4[IPV4_SIZE + 1];
-    char ip_structure[IP_STRUCTURE_SIZE + 1];
-    char udp_structure[UDP_STRUCTURE_SIZE + 1];
-    char afdxES_payload[MAX_PAYLOAD_SIZE];
-    char sn[SN_SIZE + 1];
-    char crc[CRC_SIZE + 1];
-} Frame_Package;
-
-int main(int argc, char *argv[]) {
-    FILE *in = fopen(infilename, "rb");
-    FILE *out = fopen(outfilename, "w");
-
-    while (feof(in)) {
-        Frame_Package frame;
-        memset(&frame, 0, sizeof(Frame_Package));
-        fread(&frame.frame_size, sizeof(int), 1, in);
-        fread(frame.destination_address, DESTINATION_ADDRESS_SIZE, 1, in);
-        fread(frame.source_address, SOURCE_ADDRESS_SIZE, 1, in);
-        fread(frame.ipv4, IPV4_SIZE, 1, in);
-        fread(frame.ip_structure, IP_STRUCTURE_SIZE, 1, in);
-        fread(frame.udp_structure, UDP_STRUCTURE_SIZE, 1, in);
-        fread(frame.afdxES_payload, frame.frame_size - DESTINATION_ADDRESS_SIZE -
-              SOURCE_ADDRESS_SIZE - IPV4_SIZE - IP_STRUCTURE_SIZE -
-              UDP_STRUCTURE_SIZE - SN_SIZE - CRC_SIZE, 1, in);
-        fread(frame.sn, SN_SIZE, 1, in);
-        fread(frame.crc, CRC_SIZE, 1, in);
-
-        fprintf(out, "####################### Frame size: %d\n", frame.frame_size);
-        fprintf(out, "Destination Address: %s\n", frame.destination_address);
-        fprintf(out, "Source Address: %s\n", frame.source_address);
-        fprintf(out, "IPv4: %s\n", frame.ipv4);
-        fprintf(out, "IP Structure: %s\n", frame.ip_structure);
-        fprintf(out, "UDP Structure: %s\n", frame.udp_structure);
-        fprintf(out, "AFDX Payload: %s\n", frame.afdxES_payload);
-        fprintf(out, "SN: %s\n", frame.sn);
-        fprintf(out, "CRC: %s\n\n\n", frame.crc);
-    }
-
-    return 0;
-}
-
diff --git a/simics-workspace/modules/sample-frame-module/save_frame_into_disk.c b/simics-workspace/modules/sample-frame-module/save_frame_into_disk.c
index fc75bae..50d0939 100644
--- a/simics-workspace/modules/sample-frame-module/save_frame_into_disk.c
+++ b/simics-workspace/modules/sample-frame-module/save_frame_into_disk.c
@@ -11,7 +11,7 @@ typedef enum {
 FILE *frame_image[2];

 const char *filename[] = {
-    "sframe_image.bin"
+    "sframe_image.bin",
     "rframe_image.bin"
 };

@@ -35,13 +35,14 @@ void save_frame_into_disk(void *frame, void *frame_size, int frame_type) {
     int file_size = get_file_size(frame_type);

     if ((file_size != -1) && (file_size >= max_file_size)) {
-        fseek(frame_image, 0, SEEK_SET);
+        fseek(frame_image[frame_type], 0, SEEK_SET);
     } else {
-        fseek(frame_image, 0, SEEK_END);
+        fseek(frame_image[frame_type], 0, SEEK_END);
     }

     fwrite(frame_size, sizeof(*(uint32*)frame_size), 1, frame_image[frame_type]);
     fwrite(frame, *(uint32*)frame_size, 1, frame_image[frame_type]);
+    fflush(frame_image[frame_type]);
 }

 void init_frame_image(void) {