-
Notifications
You must be signed in to change notification settings - Fork 9
/
sim.cpp
143 lines (119 loc) · 3.52 KB
/
sim.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include "sim.h"
#include <unistd.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <errno.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <string.h>
#include <sys/select.h>
#include <list>
SimIF::SimIF(const char* mem_server, int port) {
struct sockaddr_in addr;
struct hostent *he;
m_port = port;
m_server = mem_server;
if((m_socket = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
fprintf(stderr, "Unable to create socket (%s)\n", strerror(errno));
return;
}
if((he = gethostbyname(m_server)) == NULL) {
perror("gethostbyname");
return;
}
addr.sin_family = AF_INET;
addr.sin_port = htons(m_port);
addr.sin_addr = *((struct in_addr *)he->h_addr_list[0]);
memset(addr.sin_zero, '\0', sizeof(addr.sin_zero));
if(connect(m_socket, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
fprintf(stderr, "Unable to connect to %s port %d (%s)\n", m_server, m_port,
strerror(errno));
return;
}
printf("Mem connected!\n");
}
bool SimIF::access_raw(bool write, unsigned int addr, int size, char* buffer) {
int ret;
char data[1024];
// packet header looks like this:
// Write (in LSB)
// ADDR[31:0]
// SIZE[31:0]
//
// after that follows the data in the buffer (for a write), starting at the lowest byte
// for a read, the packet is finished with SIZE
memset(data, 0, sizeof(data));
data[0] = write ? 1 : 0;
data[1] = (addr >> 0) & 0xFF;
data[2] = (addr >> 8) & 0xFF;
data[3] = (addr >> 16) & 0xFF;
data[4] = (addr >> 24) & 0xFF;
data[5] = (size >> 0) & 0xFF;
data[6] = (size >> 8) & 0xFF;
data[7] = (size >> 16) & 0xFF;
data[8] = (size >> 24) & 0xFF;
ret = send(m_socket, data, 9, 0);
if (ret != 9) {
fprintf(stderr, "Unable to send header to simulator: %s\n", strerror(errno));
return false;
}
if (write) {
// write
ret = send(m_socket, buffer, size, 0);
if (ret != size) {
fprintf(stderr, "Unable to send buffer to simulator: %s\n", strerror(errno));
return false;
}
// check response
ret = recv(m_socket, data, 5, 0);
if (ret == -1 || ret == 0) {
fprintf(stderr, "Unable to get a response from simulator: %s\n", strerror(errno));
return false;
}
if (ret != 5) {
fprintf(stderr, "Unable to get all write response, only get %d from %d\n", ret, 5);
return false;
}
if (data[0] == -1) {
fprintf(stderr, "Write failed on simulator\n");
return false;
}
} else {
// read
ret = recv(m_socket, data, 5, 0);
if (ret == -1 || ret == 0) {
fprintf(stderr, "Unable to get a response from simulator: %s\n", strerror(errno));
return false;
}
if (data[0] == -1) {
fprintf(stderr, "Read failed on simulator\n");
return false;
}
uint32_t size = (data[1] << 0) | (data[2] << 8) | (data[3] << 16) | (data[4] << 24);
ret = recv(m_socket, buffer, size, 0);
if (ret == -1 || ret == 0) {
fprintf(stderr, "Unable to get a response from simulator: %s\n", strerror(errno));
return false;
}
if ((uint32_t)ret != size) {
fprintf(stderr, "Unable to get all data only get %d from %d\n", ret, size);
return false;
}
}
return true;
}
bool
SimIF::access(bool write, unsigned int addr, int size, char* buffer) {
bool retval = true;
// break into 1024 byte chunks
while (size > 0) {
retval = retval && this->access_raw(write, addr, size, buffer);
addr += 1024;
size -= 1024;
buffer += 1024;
}
return retval;
}