1 //
2 // Copyright (c) 2010-2024 Antmicro
3 //
4 // This file is licensed under the MIT License.
5 // Full license text is available in 'licenses/MIT.txt'.
6 //
7 
8 #include "renode_dpi.h"
9 #include "communication/socket_channel.h"
10 
11 static SocketCommunicationChannel *socketChannel;
12 
renodeDPIReceive(uint32_t * actionId,uint64_t * address,uint64_t * value,int32_t * peripheralIndex)13 bool renodeDPIReceive(uint32_t* actionId, uint64_t* address, uint64_t* value, int32_t* peripheralIndex)
14 {
15     if(!renodeDPIIsConnected())
16     {
17         return false;
18     }
19     Protocol *message = socketChannel->receive();
20     *actionId = message->actionId;
21     *address = message->addr;
22     *value = message->value;
23     *peripheralIndex = message->peripheralIndex;
24 
25     delete message;
26     return true;
27 }
28 
renodeDPIConnect(int receiverPort,int senderPort,const char * address)29 void renodeDPIConnect(int receiverPort, int senderPort, const char* address)
30 {
31     socketChannel = new SocketCommunicationChannel();
32     socketChannel->connect(receiverPort, senderPort, address);
33 }
34 
renodeDPIDisconnect()35 void renodeDPIDisconnect()
36 {
37     if(socketChannel != NULL)
38     {
39         socketChannel->disconnect();
40     }
41 }
42 
renodeDPIIsConnected()43 bool renodeDPIIsConnected()
44 {
45     return socketChannel != NULL && socketChannel->isConnected();
46 }
47 
renodeDPISend(uint32_t actionId,uint64_t address,uint64_t value,int32_t peripheralIndex)48 bool renodeDPISend(uint32_t actionId, uint64_t address, uint64_t value, int32_t peripheralIndex)
49 {
50     if(!renodeDPIIsConnected())
51     {
52         return false;
53     }
54     socketChannel->sendMain(Protocol(actionId, address, value, peripheralIndex));
55     return true;
56 }
57 
renodeDPISendToAsync(uint32_t actionId,uint64_t address,uint64_t value,int32_t peripheralIndex)58 bool renodeDPISendToAsync(uint32_t actionId, uint64_t address, uint64_t value, int32_t peripheralIndex)
59 {
60     if(!renodeDPIIsConnected())
61     {
62         return false;
63     }
64     socketChannel->sendSender(Protocol(actionId, address, value, peripheralIndex));
65     return true;
66 }
67 
renodeDPILog(int logLevel,const char * data)68 bool renodeDPILog(int logLevel, const char* data)
69 {
70     if(!renodeDPIIsConnected())
71     {
72         return false;
73     }
74     socketChannel->log(logLevel, data);
75     return true;
76 }
77