mibuild/sim: able to send ethernet frame from sim to server.py
authorFlorent Kermarrec <florent@enjoy-digital.fr>
Fri, 6 Mar 2015 11:49:56 +0000 (12:49 +0100)
committerFlorent Kermarrec <florent@enjoy-digital.fr>
Fri, 6 Mar 2015 11:49:56 +0000 (12:49 +0100)
mibuild/sim/server.py
mibuild/sim/server_tb.cpp

index d21cdc21969d212532f64efb36888f1db2c28c17..ea8bb03ce3d955bedd15479d4fef36ea127e6a61 100644 (file)
@@ -11,7 +11,8 @@ messages= {
        "EXIT":         0,
        "ACK":          1,
        "ERROR":        2,
-       "UART":         3
+       "UART":         3,
+       "ETHERNET":     4
 }
 
 class PacketTooLarge(Exception):
@@ -80,7 +81,11 @@ def read():
                        if packet[0] == messages["UART"]:
                                c = bytes(chr(packet[1]).encode('utf-8'))
                                os.write(server.serial, c)
-
+                       elif packet[0] == messages["ETHERNET"]:
+                               print("received ethernet")
+                               for d in packet[1:]:
+                                       print("{:02X}".format(d), end="")
+                               print("")
                        elif packet[0] == messages["ACK"]:
                                server.ack = True
 
index f93e2962cbb372d4ef1a90917776f53eef871490..5203a7aaab035cd3f5bbe9ee7b87c22a73c0b51b 100644 (file)
@@ -19,6 +19,9 @@
 #include <netdb.h>
 #include <pthread.h>
 
+#define MAX(a,b) (((a)>(b))?(a):(b))
+#define MIN(a,b) (((a)<(b))?(a):(b))
+
 int trace = 0;
 
 vluint64_t main_time = 0;
@@ -38,7 +41,8 @@ enum {
        MESSAGE_EXIT = 0,
        MESSAGE_ACK,
        MESSAGE_ERROR,
-       MESSAGE_UART
+       MESSAGE_UART,
+       MESSAGE_ETH
 };
 
 struct sim {
@@ -58,6 +62,12 @@ struct sim {
        char rx_serial_presented;
 };
 
+unsigned char eth_txbuffer[1532];
+unsigned char eth_rxbuffer[1532];
+int eth_txbuffer_len = 0;
+int eth_rxbuffer_len = 0;
+int eth_rxbuffer_pos = 0;
+
 int sim_connect(struct sim *s, const char *sockaddr)
 {
        struct sockaddr_un addr;
@@ -143,6 +153,25 @@ int console_service(struct sim *s)
        return 0;
 }
 
+int eth_last_source_stb = 0;
+
+int ethernet_service(struct sim *s) {
+       /* fpga --> ethernet tap */
+       ETH_SOURCE_ACK = 1;
+       if(ETH_SOURCE_STB == 1) {
+               eth_txbuffer[eth_txbuffer_len] = ETH_SOURCE_DATA;
+               eth_txbuffer_len++;
+       } else {
+               if (eth_last_source_stb) {
+                       s->txbuffer[0] = MESSAGE_ETH;
+                       memcpy(s->txbuffer+1, eth_txbuffer, eth_txbuffer_len);
+                       sim_send(s, s->txbuffer, eth_txbuffer_len+1);
+                       eth_txbuffer_len = 0;
+               }
+       }
+       eth_last_source_stb = ETH_SOURCE_STB;
+}
+
 void sim_tick(struct sim *s)
 {
        SYS_CLK = s->tick%2;
@@ -190,6 +219,7 @@ int main(int argc, char **argv, char **env)
                if (SYS_CLK) {
                        if (console_service(&s) != 0)
                                s.run = false;
+                       ethernet_service(&s);
                }
        }
        s.end = clock();