5 #include <sys/select.h>
7 #include <sys/socket.h>
8 #include <netinet/in.h>
10 #include <sys/fcntl.h>
11 #include <netinet/in.h>
12 #include <netinet/tcp.h>
21 /* nothing sophisticated: set up a listening socket
26 struct sockaddr_in serv_addr
;
28 listenfd
= socket(AF_INET
, SOCK_STREAM
, 0);
29 memset(&serv_addr
, '0', sizeof(serv_addr
));
31 serv_addr
.sin_family
= AF_INET
;
32 serv_addr
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
33 serv_addr
.sin_port
= htons(44853);
35 bind(listenfd
, (struct sockaddr
*)&serv_addr
, sizeof(serv_addr
));
41 /* sees if there is an incoming connection (non-blocking). if so,
42 accept it and return it
44 int get_connection(int listenfd
)
56 /* Initialize the set of active sockets. */
57 FD_ZERO (&active_fd_set
);
58 FD_SET (listenfd
, &active_fd_set
);
60 /* Block until input arrives on one or more active sockets. */
61 status
= select (FD_SETSIZE
, &active_fd_set
, NULL
, NULL
, &tv
);
65 printf("Error listening on socket\n");
66 exit(status
); // error
69 return -1; // timeout (nothing read)
71 // return accepted socket
72 connfd
= accept(listenfd
, (struct sockaddr
*)NULL
, NULL
);
73 setsockopt(connfd
, IPPROTO_TCP
, TCP_NODELAY
, (char *) &flag
,
79 /* reads from a socket if it is ready (a single byte) and returns 1 if success
81 int read_handler(int fdread
, char *buffer
)
86 FD_SET(fdread
, &read_fds
);
93 status
= select(fdread
+1, &read_fds
, NULL
, NULL
, &tv
);
97 printf("Error reading on socket\n");
98 exit(status
); // error
101 return 0; // timeout (nothing read)
103 status
= read(fdread
, buffer
, 1);
105 printf("Error reading on socket\n");
114 /* main function which polls the socket and talks openocd jtagremote protocol.
115 dead simple: incoming number 0-7 sets TCK, TMS and TDK. request "R"
116 indicates that receiver wants to know the status of TDO.
117 "Q" means "quit socket".
119 int read_openocd_jtagremote(cxxrtl_design::p_add
&top
, int sock
)
122 if (read_handler(sock
, &c
) != 1) {
125 printf ("read %c\n", c
);
126 if ((c
>= '0') && (c
<= '7'))
128 top
.p_tck
.set
<bool>(((c
- '0') >> 2) & 1);
129 top
.p_tms
.set
<bool>(((c
- '0') >> 1) & 1);
130 top
.p_tdi
.set
<bool>((c
- '0') & 1);
134 uint8_t val
= top
.p_tdo
.get
<uint8_t>() + '0';
135 if(-1 == write(sock
, &val
, 1))
137 printf("Error writing on socket\n");
143 printf("disconnect request\n");
152 cxxrtl_design::p_add top
;
154 int listenfd
= setup_socket();
160 top
.p_clk
.set
<bool>(false);
162 top
.p_clk
.set
<bool>(true);
165 // if no current connection see if there is one
167 sock
= get_connection(listenfd
);
169 /* read and process incoming jtag. sock set to -1 if disconnected */
170 sock
= read_openocd_jtagremote(top
, sock
);
172 // quick check that the output is correct (it's an adder: go figure)
174 top.p_a.set<uint8_t>(5);
175 top.p_b.set<uint8_t>(3);
176 uint32_t f = top.p_f.get<uint32_t>();
178 cout << "f " << f << endl;