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>
22 #include <backends/cxxrtl/cxxrtl_vcd.h>
29 /* nothing sophisticated: set up a listening socket
34 struct sockaddr_in serv_addr
;
36 listenfd
= socket(AF_INET
, SOCK_STREAM
, 0);
37 memset(&serv_addr
, '0', sizeof(serv_addr
));
39 serv_addr
.sin_family
= AF_INET
;
40 serv_addr
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
41 serv_addr
.sin_port
= htons(44853);
43 bind(listenfd
, (struct sockaddr
*)&serv_addr
, sizeof(serv_addr
));
49 /* sees if there is an incoming connection (non-blocking). if so,
50 accept it and return it
52 int get_connection(int listenfd
)
64 /* Initialize the set of active sockets. */
65 FD_ZERO (&active_fd_set
);
66 FD_SET (listenfd
, &active_fd_set
);
68 /* Block until input arrives on one or more active sockets. */
69 status
= select (FD_SETSIZE
, &active_fd_set
, NULL
, NULL
, &tv
);
73 printf("Error listening on socket\n");
74 exit(status
); // error
77 return -1; // timeout (nothing read)
79 // return accepted socket
80 connfd
= accept(listenfd
, (struct sockaddr
*)NULL
, NULL
);
81 setsockopt(connfd
, IPPROTO_TCP
, TCP_NODELAY
, (char *) &flag
,
87 /* reads from a socket if it is ready (a single byte) and returns 1 if success
89 int read_handler(int fdread
, char *buffer
)
94 FD_SET(fdread
, &read_fds
);
101 status
= select(fdread
+1, &read_fds
, NULL
, NULL
, &tv
);
105 printf("Error reading on socket\n");
106 exit(status
); // error
109 return 0; // timeout (nothing read)
111 status
= read(fdread
, buffer
, 1);
113 printf("Error reading on socket\n");
122 /* main function which polls the socket and talks openocd jtagremote protocol.
123 dead simple: incoming number 0-7 sets TCK, TMS and TDK. request "R"
124 indicates that receiver wants to know the status of TDO.
125 "Q" means "quit socket".
127 int read_openocd_jtagremote(cxxrtl_design::p_ls180
&top
, int sock
)
130 if (read_handler(sock
, &c
) != 1) {
133 printf ("read %c\n", c
);
134 if ((c
>= '0') && (c
<= '7'))
136 top
.p_jtag__tck
.set
<bool>(((c
- '0') >> 2) & 1);
137 top
.p_jtag__tms
.set
<bool>(((c
- '0') >> 1) & 1);
138 top
.p_jtag__tdi
.set
<bool>((c
- '0') & 1);
142 uint8_t val
= top
.p_jtag__tdo
.get
<uint8_t>() + '0';
143 if(-1 == write(sock
, &val
, 1))
145 printf("Error writing on socket\n");
151 printf("disconnect request\n");
160 cxxrtl_design::p_ls180 top
;
164 // Load the debug items of the top down the whole design hierarchy
165 cxxrtl::debug_items all_debug_items
;
166 top
.debug_info(all_debug_items
);
168 // set up vcdwriter with 1ns resu
169 cxxrtl::vcd_writer vcd
;
170 vcd
.timescale(1, "us");
173 int listenfd
= setup_socket();
177 //vcd.add_without_memories(all_debug_items);
178 vcd
.add(all_debug_items
);
179 std::ofstream
waves("waves.vcd");
188 top
.p_sys__clk
.set
<bool>(false);
191 vcd
.sample(steps
*2 + 0);
193 top
.p_sys__clk
.set
<bool>(true);
196 vcd
.sample(steps
*2 + 1);
200 // if no current connection see if there is one
202 sock
= get_connection(listenfd
);
204 /* read and process incoming jtag. sock set to -1 if disconnected */
205 sock
= read_openocd_jtagremote(top
, sock
);