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 /* reads from a socket if it is ready (a single byte) and returns 1 if success
23 int read_handler(int fdread
, char *buffer
)
29 FD_SET(fdread
, &read_fds
);
35 status
= select(fdread
+1, &read_fds
, NULL
, NULL
, &tv
);
39 printf("Error reading on socket\n");
40 exit(status
); // error
43 return 0; // timeout (nothing read)
45 return read(fdread
, buffer
, 1);
49 /* nothing sophisticated: wait for an incoming connection
53 int listenfd
= 0, connfd
= 0;
55 struct sockaddr_in serv_addr
;
57 listenfd
= socket(AF_INET
, SOCK_STREAM
, 0);
58 memset(&serv_addr
, '0', sizeof(serv_addr
));
60 serv_addr
.sin_family
= AF_INET
;
61 serv_addr
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
62 serv_addr
.sin_port
= htons(5000);
64 bind(listenfd
, (struct sockaddr
*)&serv_addr
, sizeof(serv_addr
));
68 connfd
= accept(listenfd
, (struct sockaddr
*)NULL
, NULL
);
71 setsockopt(connfd
, IPPROTO_TCP
, TCP_NODELAY
, (char *) &flag
, sizeof(int));
77 /* main function which polls the socket and talks openocd jtagremote protocol.
78 dead simple: incoming number 0-7 sets TCK, TMS and TDK. request "R"
79 indicates that receiver wants to know the status of TDO.
80 "Q" means "quit socket".
82 void read_openocd_jtagremote(cxxrtl_design::p_add
&top
, int sock
)
85 if (read_handler(sock
, &c
) != 1) {
88 printf ("read %c\n", c
);
89 if ((c
>= '0') && (c
<= '7'))
91 top
.p_tck
.set
<bool>(((c
- '0') >> 2) & 1);
92 top
.p_tms
.set
<bool>(((c
- '0') >> 1) & 1);
93 top
.p_tdi
.set
<bool>((c
- '0') & 1);
97 uint8_t val
= top
.p_tdo
.get
<uint8_t>() + '0';
98 if(-1 == write(sock
, &val
, 1))
100 printf("Error writing on socket\n");
105 printf("disconnect request\n");
112 cxxrtl_design::p_add top
;
114 int sock
= get_connection();
119 top
.p_clk
.set
<bool>(false);
121 top
.p_clk
.set
<bool>(true);
124 /* read and process incoming jtag */
125 read_openocd_jtagremote(top
, sock
);
127 // quick check that the output is correct (it's an adder: go figure)
129 top.p_a.set<uint8_t>(5);
130 top.p_b.set<uint8_t>(3);
131 uint32_t f = top.p_f.get<uint32_t>();
133 cout << "f " << f << endl;