+ *out_data = data;
+ return true;
+}
+
+enum ZY1000_CMD {
+ ZY1000_CMD_POKE = 0x0,
+ ZY1000_CMD_PEEK = 0x8,
+ ZY1000_CMD_SLEEP = 0x1,
+ ZY1000_CMD_WAITIDLE = 2
+};
+
+#include <sys/socket.h> /* for socket(), connect(), send(), and recv() */
+#include <arpa/inet.h> /* for sockaddr_in and inet_addr() */
+
+/* We initialize this late since we need to know the server address
+ * first.
+ */
+static void tcpip_open(void)
+{
+ if (tcp_ip >= 0)
+ return;
+
+ struct sockaddr_in echoServAddr;/* Echo server address */
+
+ /* Create a reliable, stream socket using TCP */
+ tcp_ip = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
+ if (tcp_ip < 0) {
+ fprintf(stderr, "Failed to connect to zy1000 server\n");
+ exit(-1);
+ }
+
+ /* Construct the server address structure */
+ memset(&echoServAddr, 0, sizeof(echoServAddr)); /* Zero out structure */
+ echoServAddr.sin_family = AF_INET; /* Internet address family */
+ echoServAddr.sin_addr.s_addr = inet_addr(tcp_server); /* Server IP address */
+ echoServAddr.sin_port = htons(7777); /* Server port */
+
+ /* Establish the connection to the echo server */
+ if (connect(tcp_ip, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) < 0) {
+ fprintf(stderr, "Failed to connect to zy1000 server\n");
+ exit(-1);
+ }
+
+ int flag = 1;
+ setsockopt(tcp_ip, /* socket affected */
+ IPPROTO_TCP, /* set option at TCP level */
+ TCP_NODELAY, /* name of option */
+ (char *)&flag, /* the cast is historical cruft */
+ sizeof(int)); /* length of option value */
+
+}
+
+/* send a poke */
+void zy1000_tcpout(uint32_t address, uint32_t data)
+{
+ tcpip_open();
+ if (!writeLong((ZY1000_CMD_POKE << 24) | address) || !writeLong(data)) {
+ fprintf(stderr, "Could not write to zy1000 server\n");
+ exit(-1);
+ }
+}
+
+/* By sending the wait to the server, we avoid a readback
+ * of status. Radically improves performance for this operation
+ * with long ping times.
+ */
+void waitIdle(void)
+{
+ tcpip_open();
+ if (!writeLong((ZY1000_CMD_WAITIDLE << 24))) {
+ fprintf(stderr, "Could not write to zy1000 server\n");
+ exit(-1);
+ }
+}
+
+uint32_t zy1000_tcpin(uint32_t address)
+{
+ tcpip_open();
+
+ zy1000_flush_readqueue();
+
+ uint32_t data;
+ if (!writeLong((ZY1000_CMD_PEEK << 24) | address) || !readLong(&data)) {
+ fprintf(stderr, "Could not read from zy1000 server\n");
+ exit(-1);
+ }
+ return data;
+}
+
+int interface_jtag_add_sleep(uint32_t us)
+{
+ tcpip_open();
+ if (!writeLong((ZY1000_CMD_SLEEP << 24)) || !writeLong(us)) {
+ fprintf(stderr, "Could not read from zy1000 server\n");
+ exit(-1);
+ }
+ return ERROR_OK;
+}
+
+/* queue a readback */
+#define readqueue_size 16384
+static struct {
+ uint8_t *dest;
+ int bits;
+} readqueue[readqueue_size];
+
+static int readqueue_pos;
+
+/* flush the readqueue, this means reading any data that
+ * we're expecting and store them into the final position
+ */
+void zy1000_flush_readqueue(void)
+{
+ if (readqueue_pos == 0) {
+ /* simply debugging by allowing easy breakpoints when there
+ * is something to do. */
+ return;
+ }
+ int i;
+ tcpip_open();
+ for (i = 0; i < readqueue_pos; i++) {
+ uint32_t value;
+ if (!readLong(&value)) {
+ fprintf(stderr, "Could not read from zy1000 server\n");
+ exit(-1);
+ }
+
+ uint8_t *in_value = readqueue[i].dest;
+ int k = readqueue[i].bits;
+
+ /* we're shifting in data to MSB, shift data to be aligned for returning the value */
+ value >>= 32-k;
+
+ for (int l = 0; l < k; l += 8)
+ in_value[l/8] = (value >> l)&0xff;
+ }
+ readqueue_pos = 0;
+}
+
+/* By queuing the callback's we avoid flushing the
+ * read queue until jtag_execute_queue(). This can
+ * reduce latency dramatically for cases where
+ * callbacks are used extensively.
+*/
+#define callbackqueue_size 128
+static struct callbackentry {
+ jtag_callback_t callback;
+ jtag_callback_data_t data0;
+ jtag_callback_data_t data1;
+ jtag_callback_data_t data2;
+ jtag_callback_data_t data3;
+} callbackqueue[callbackqueue_size];
+
+static int callbackqueue_pos;
+
+void zy1000_jtag_add_callback4(jtag_callback_t callback,
+ jtag_callback_data_t data0,
+ jtag_callback_data_t data1,
+ jtag_callback_data_t data2,
+ jtag_callback_data_t data3)
+{
+ if (callbackqueue_pos >= callbackqueue_size)
+ zy1000_flush_callbackqueue();
+
+ callbackqueue[callbackqueue_pos].callback = callback;
+ callbackqueue[callbackqueue_pos].data0 = data0;
+ callbackqueue[callbackqueue_pos].data1 = data1;
+ callbackqueue[callbackqueue_pos].data2 = data2;
+ callbackqueue[callbackqueue_pos].data3 = data3;
+ callbackqueue_pos++;
+
+ /* KLUDGE!
+ * make callbacks synchronous for now as minidriver requires callback
+ * to be synchronous.
+ *
+ * We can get away with making read and writes asynchronous so we
+ * don't completely kill performance.
+ */
+ zy1000_flush_callbackqueue();
+}
+
+static int zy1000_jtag_convert_to_callback4(jtag_callback_data_t data0,
+ jtag_callback_data_t data1,
+ jtag_callback_data_t data2,
+ jtag_callback_data_t data3)
+{
+ ((jtag_callback1_t)data1)(data0);
+ return ERROR_OK;
+}
+
+void zy1000_jtag_add_callback(jtag_callback1_t callback, jtag_callback_data_t data0)
+{
+ zy1000_jtag_add_callback4(zy1000_jtag_convert_to_callback4,
+ data0,
+ (jtag_callback_data_t)callback,
+ 0,
+ 0);
+}
+
+void zy1000_flush_callbackqueue(void)
+{
+ /* we have to flush the read queue so we have access to
+ the data the callbacks will use
+ */
+ zy1000_flush_readqueue();
+ int i;
+ for (i = 0; i < callbackqueue_pos; i++) {
+ struct callbackentry *entry = &callbackqueue[i];
+ jtag_set_error(entry->callback(entry->data0, entry->data1, entry->data2,
+ entry->data3));
+ }
+ callbackqueue_pos = 0;
+}
+
+static void writeShiftValue(uint8_t *data, int bits)
+{
+ waitIdle();
+
+ if (!writeLong((ZY1000_CMD_PEEK << 24) | (ZY1000_JTAG_BASE + 0xc))) {
+ fprintf(stderr, "Could not read from zy1000 server\n");
+ exit(-1);
+ }
+
+ if (readqueue_pos >= readqueue_size)
+ zy1000_flush_readqueue();
+
+ readqueue[readqueue_pos].dest = data;
+ readqueue[readqueue_pos].bits = bits;
+ readqueue_pos++;
+
+ /* KLUDGE!!! minidriver requires readqueue to be synchronous */
+ zy1000_flush_readqueue();
+}
+
+#else
+
+static void writeShiftValue(uint8_t *data, int bits)
+{
+ uint32_t value;
+ waitIdle();
+ ZY1000_PEEK(ZY1000_JTAG_BASE + 0xc, value);
+ VERBOSE(LOG_INFO("getShiftValue %08x", value));
+
+ /* data in, LSB to MSB */
+ /* we're shifting in data to MSB, shift data to be aligned for returning the value */
+ value >>= 32 - bits;
+
+ for (int l = 0; l < bits; l += 8)
+ data[l/8] = (value >> l)&0xff;
+}
+
+#endif
+
+#if BUILD_ZY1000_MASTER
+
+#ifdef WATCHDOG_BASE
+/* If we connect to port 8888 we must send a char every 10s or the board resets itself */
+static void watchdog_server(cyg_addrword_t data)
+{
+ int so_reuseaddr_option = 1;
+
+ int fd = socket(AF_INET, SOCK_STREAM, 0);
+ if (fd == -1) {
+ LOG_ERROR("error creating socket: %s", strerror(errno));
+ exit(-1);
+ }
+
+ setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void *) &so_reuseaddr_option,
+ sizeof(int));
+
+ struct sockaddr_in sin;
+ unsigned int address_size;
+ address_size = sizeof(sin);
+ memset(&sin, 0, sizeof(sin));
+ sin.sin_family = AF_INET;
+ sin.sin_addr.s_addr = INADDR_ANY;
+ sin.sin_port = htons(8888);
+
+ if (bind(fd, (struct sockaddr *) &sin, sizeof(sin)) == -1) {
+ LOG_ERROR("couldn't bind to socket: %s", strerror(errno));
+ exit(-1);
+ }
+
+ if (listen(fd, 1) == -1) {
+ LOG_ERROR("couldn't listen on socket: %s", strerror(errno));
+ exit(-1);
+ }
+
+
+ for (;; ) {
+ int watchdog_ip = accept(fd, (struct sockaddr *) &sin, &address_size);
+
+ /* Start watchdog, must be reset every 10 seconds. */
+ HAL_WRITE_UINT32(WATCHDOG_BASE + 4, 4);
+
+ if (watchdog_ip < 0) {
+ LOG_ERROR("couldn't open watchdog socket: %s", strerror(errno));
+ exit(-1);