using namespace std;
+extern "C" {
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/select.h>
+
+/* reads from a socket if it is ready (a single byte) and returns 1 if success
+ */
+int read_handler(int fdread, char *buffer)
+{
+ ssize_t read_len;
+ fd_set read_fds;
+ FD_ZERO(&read_fds);
+ struct timeval tv;
+ FD_SET(fdread, &read_fds);
+ int status;
+
+ tv.tv_sec = 0;
+ tv.tv_usec = 1;
+
+ status = select(fdread+1, &read_fds, NULL, NULL, &tv);
+ switch (status)
+ {
+ case -1:
+ exit(status); // error
+ return 0;
+ case 0:
+ return 0; // timeout (nothing read)
+ default:
+ return read(fdread, buffer, 1);
+ }
+}
+
+/*
+static void event_handler(int fd, short event, void *arg)
+{
+ if (event & EV_READ)
+ read_handler(fd, event, arg);
+}
+
+static void accept_conn_cb(struct evconnlistener *listener, evutil_socket_t fd, struct sockaddr *address, int socklen, void *ctx)
+{
+ struct session_s *s = (struct session_s*)ctx;
+ struct timeval tv = {1, 0};
+
+ s->fd = fd;
+ s->ev = event_new(base, fd, EV_READ | EV_PERSIST , event_handler, s);
+ event_add(s->ev, &tv);
+}
+
+static int jtagremote_new(void **sess, char *args)
+{
+ int ret = RC_OK;
+ struct session_s *s = NULL;
+ char *cport = NULL;
+ int port;
+ struct evconnlistener *listener;
+ struct sockaddr_in sin;
+
+ if(!sess) {
+ ret = RC_INVARG;
+ goto out;
+ }
+
+ ret = litex_sim_module_get_args(args, "port", &cport);
+ if(RC_OK != ret)
+ goto out;
+
+ printf("Found port %s\n", cport);
+ sscanf(cport, "%d", &port);
+ free(cport);
+ if(!port) {
+ ret = RC_ERROR;
+ fprintf(stderr, "Invalid port selected!\n");
+ goto out;
+ }
+
+ s=(struct session_s*)malloc(sizeof(struct session_s));
+ if(!s) {
+ ret = RC_NOENMEM;
+ goto out;
+ }
+ memset(s, 0, sizeof(struct session_s));
+
+ memset(&sin, 0, sizeof(sin));
+ sin.sin_family = AF_INET;
+ sin.sin_addr.s_addr = htonl(0);
+ sin.sin_port = htons(port);
+ listener = evconnlistener_new_bind(base, accept_conn_cb, s, LEV_OPT_CLOSE_ON_FREE|LEV_OPT_REUSEABLE, -1, (struct sockaddr*)&sin, sizeof(sin));
+ if (!listener) {
+ ret=RC_ERROR;
+ eprintf("Can't bind port %d\n!\n", port);
+ goto out;
+ }
+ evconnlistener_set_error_cb(listener, accept_error_cb);
+
+out:
+ *sess=(void*)s;
+ return ret;
+}
+
+ if(s->datalen)
+ {
+ c = s->databuf[s->data_start];
+
+ if((c >= '0') && (c <= '7')){
+ *s->tck = ((c - '0') >> 2) & 1;
+ *s->tms = ((c - '0') >> 1) & 1;
+ *s->tdi = (c - '0') & 1;
+ }
+ if(c == 'R'){
+ val = *s->tdo + '0';
+ if(-1 == write(s->fd, &val, 1)) {
+ eprintf("Error writing on socket\n");
+ ret = RC_ERROR;
+ goto out;
+ }
+ }
+ s->data_start = (s->data_start + 1) % 2048;
+ s->datalen--;
+ }
+
+out:
+ return ret;
+*/
+
+} // extern "C"
+
int main()
{
cxxrtl_design::p_add top;