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 return read(fdread
, buffer
, 1);
109 /* main function which polls the socket and talks openocd jtagremote protocol.
110 dead simple: incoming number 0-7 sets TCK, TMS and TDK. request "R"
111 indicates that receiver wants to know the status of TDO.
112 "Q" means "quit socket".
114 int read_openocd_jtagremote(cxxrtl_design::p_add
&top
, int sock
)
117 if (read_handler(sock
, &c
) != 1) {
120 printf ("read %c\n", c
);
121 if ((c
>= '0') && (c
<= '7'))
123 top
.p_tck
.set
<bool>(((c
- '0') >> 2) & 1);
124 top
.p_tms
.set
<bool>(((c
- '0') >> 1) & 1);
125 top
.p_tdi
.set
<bool>((c
- '0') & 1);
129 uint8_t val
= top
.p_tdo
.get
<uint8_t>() + '0';
130 if(-1 == write(sock
, &val
, 1))
132 printf("Error writing on socket\n");
138 printf("disconnect request\n");
147 cxxrtl_design::p_add top
;
149 int listenfd
= setup_socket();
155 top
.p_clk
.set
<bool>(false);
157 top
.p_clk
.set
<bool>(true);
160 // if no current connection see if there is one
162 sock
= get_connection(listenfd
);
164 /* read and process incoming jtag. sock set to -1 if disconnected */
165 sock
= read_openocd_jtagremote(top
, sock
);
167 // quick check that the output is correct (it's an adder: go figure)
169 top.p_a.set<uint8_t>(5);
170 top.p_b.set<uint8_t>(3);
171 uint32_t f = top.p_f.get<uint32_t>();
173 cout << "f " << f << endl;