1 |
5 |
dgisselq |
////////////////////////////////////////////////////////////////////////////////
|
2 |
|
|
//
|
3 |
|
|
// Filename: ttybus.cpp
|
4 |
|
|
//
|
5 |
|
|
// Project: XuLA2 board
|
6 |
|
|
//
|
7 |
|
|
// Purpose: This is the C++ program on the command side that will interact
|
8 |
|
|
// with a UART on an FPGA, to command the WISHBONE on that same
|
9 |
|
|
// FPGA to ... whatever we wish to command it to do.
|
10 |
|
|
//
|
11 |
|
|
// This code does not run on an FPGA, is not a test bench, neither
|
12 |
|
|
// is it a simulator. It is a portion of a command program
|
13 |
|
|
// for commanding an FPGA.
|
14 |
|
|
//
|
15 |
|
|
//
|
16 |
|
|
// Creator: Dan Gisselquist, Ph.D.
|
17 |
|
|
// Gisselquist Technology, LLC
|
18 |
|
|
//
|
19 |
|
|
////////////////////////////////////////////////////////////////////////////////
|
20 |
|
|
//
|
21 |
|
|
// Copyright (C) 2015, Gisselquist Technology, LLC
|
22 |
|
|
//
|
23 |
|
|
// This program is free software (firmware): you can redistribute it and/or
|
24 |
|
|
// modify it under the terms of the GNU General Public License as published
|
25 |
|
|
// by the Free Software Foundation, either version 3 of the License, or (at
|
26 |
|
|
// your option) any later version.
|
27 |
|
|
//
|
28 |
|
|
// This program is distributed in the hope that it will be useful, but WITHOUT
|
29 |
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
|
30 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
31 |
|
|
// for more details.
|
32 |
|
|
//
|
33 |
|
|
// License: GPL, v3, as defined and found on www.gnu.org,
|
34 |
|
|
// http://www.gnu.org/licenses/gpl.html
|
35 |
|
|
//
|
36 |
|
|
//
|
37 |
|
|
////////////////////////////////////////////////////////////////////////////////
|
38 |
|
|
//
|
39 |
|
|
//
|
40 |
|
|
//
|
41 |
|
|
#include <sys/socket.h>
|
42 |
|
|
#include <sys/types.h>
|
43 |
|
|
#include <sys/stat.h>
|
44 |
|
|
#include <fcntl.h>
|
45 |
|
|
#include <termios.h>
|
46 |
|
|
#include <netinet/in.h>
|
47 |
|
|
#include <netdb.h>
|
48 |
|
|
#include <stdio.h>
|
49 |
|
|
#include <string.h>
|
50 |
|
|
#include <stdlib.h>
|
51 |
|
|
#include <unistd.h>
|
52 |
|
|
#include <errno.h>
|
53 |
|
|
#include <arpa/inet.h>
|
54 |
|
|
#include <assert.h>
|
55 |
|
|
#include <strings.h>
|
56 |
|
|
#include <poll.h>
|
57 |
|
|
#include <ctype.h>
|
58 |
|
|
|
59 |
|
|
#include "ttybus.h"
|
60 |
|
|
|
61 |
|
|
#define TTYC_IDLE '0'
|
62 |
|
|
#define TTYC_BUSY '1'
|
63 |
|
|
#define TTYC_WRITE '2'
|
64 |
|
|
#define TTYC_RESET '3'
|
65 |
|
|
#define TTYC_INT '4'
|
66 |
|
|
#define TTYC_ERR '5'
|
67 |
|
|
|
68 |
|
|
const unsigned TTYBUS::MAXRDLEN = 1024;
|
69 |
|
|
const unsigned TTYBUS::MAXWRLEN = 32;
|
70 |
|
|
|
71 |
|
|
#define DBGPRINTF null
|
72 |
|
|
// #define DBGPRINTF printf
|
73 |
|
|
void null(...) {}
|
74 |
|
|
|
75 |
|
|
char TTYBUS::charenc(const int sixbitval) {
|
76 |
|
|
if (sixbitval < 10)
|
77 |
|
|
return '0' + sixbitval;
|
78 |
|
|
else if (sixbitval < 10+26)
|
79 |
|
|
return 'A' - 10 + sixbitval;
|
80 |
|
|
else if (sixbitval < 10+26+26)
|
81 |
|
|
return 'a' - 10 - 26 + sixbitval;
|
82 |
|
|
else if (sixbitval == 0x3e)
|
83 |
|
|
return '@';
|
84 |
|
|
else if (sixbitval == 0x3f)
|
85 |
|
|
return '%';
|
86 |
|
|
|
87 |
|
|
fprintf(stderr, "INTERNAL ERR: SIXBITVAL isn\'t!!!! sixbitval = %08x\n", sixbitval);
|
88 |
|
|
assert((sixbitval & (~0x03f))==0);
|
89 |
|
|
return 0;
|
90 |
|
|
}
|
91 |
|
|
|
92 |
|
|
unsigned TTYBUS::chardec(const char b) {
|
93 |
|
|
if ((b >= '0')&&(b <= '9'))
|
94 |
|
|
return b-'0';
|
95 |
|
|
else if ((b >= 'A')&&(b <= 'Z'))
|
96 |
|
|
return b-'A'+10;
|
97 |
|
|
else if ((b >= 'a')&&(b <= 'z'))
|
98 |
|
|
return b-'a'+36;
|
99 |
|
|
else if (b == '@')
|
100 |
|
|
return 0x03e;
|
101 |
|
|
else if (b == '%')
|
102 |
|
|
return 0x03f;
|
103 |
|
|
else
|
104 |
|
|
return 0x0100; // ERR -- invalid code
|
105 |
|
|
}
|
106 |
|
|
|
107 |
|
|
int TTYBUS::lclreadcode(char *buf, int len) {
|
108 |
|
|
char *sp, *dp;
|
109 |
|
|
int nr, ret;
|
110 |
|
|
|
111 |
|
|
nr = m_dev->read(buf, len);
|
112 |
|
|
m_total_nread += nr;
|
113 |
|
|
ret = nr; sp = buf; dp = buf;
|
114 |
|
|
for(int i=0; i<nr; i++) {
|
115 |
|
|
if (chardec(*sp)&(~0x3f)) {
|
116 |
|
|
ret--; // Skip this value, not a valid codeword
|
117 |
|
|
sp++;
|
118 |
|
|
} else {
|
119 |
|
|
*sp++ = *dp++;
|
120 |
|
|
}
|
121 |
|
|
} return ret;
|
122 |
|
|
}
|
123 |
|
|
|
124 |
|
|
void TTYBUS::bufalloc(int len) {
|
125 |
|
|
if ((m_buf)&&(m_buflen >= len))
|
126 |
|
|
return;
|
127 |
|
|
if (m_buf)
|
128 |
|
|
delete[] m_buf;
|
129 |
|
|
m_buflen = (len&(-0x3f))+0x40;
|
130 |
|
|
m_buf = new char[m_buflen];
|
131 |
|
|
}
|
132 |
|
|
|
133 |
|
|
void TTYBUS::encode(const int hb, const BUSW val, char *buf) {
|
134 |
|
|
buf[0] = charenc( (hb<<2)|((val>>30)&0x03) );
|
135 |
|
|
buf[1] = charenc( (val>>24)&0x3f);
|
136 |
|
|
buf[2] = charenc( (val>>18)&0x3f);
|
137 |
|
|
buf[3] = charenc( (val>>12)&0x3f);
|
138 |
|
|
buf[4] = charenc( (val>> 6)&0x3f);
|
139 |
|
|
buf[5] = charenc( (val )&0x3f);
|
140 |
|
|
}
|
141 |
|
|
|
142 |
|
|
unsigned TTYBUS::decodestr(const char *buf) {
|
143 |
|
|
unsigned r;
|
144 |
|
|
|
145 |
|
|
r = chardec(buf[0]) & 0x03;
|
146 |
|
|
r = (r<<6) | (chardec(buf[1]) & 0x03f);
|
147 |
|
|
r = (r<<6) | (chardec(buf[2]) & 0x03f);
|
148 |
|
|
r = (r<<6) | (chardec(buf[3]) & 0x03f);
|
149 |
|
|
r = (r<<6) | (chardec(buf[4]) & 0x03f);
|
150 |
|
|
r = (r<<6) | (chardec(buf[5]) & 0x03f);
|
151 |
|
|
|
152 |
|
|
return r;
|
153 |
|
|
}
|
154 |
|
|
|
155 |
|
|
int TTYBUS::decodehex(const char hx) {
|
156 |
|
|
if ((hx >= '0')&&(hx <= '9'))
|
157 |
|
|
return hx-'0';
|
158 |
|
|
else if ((hx >= 'A')&&(hx <= 'Z'))
|
159 |
|
|
return hx-'A'+10;
|
160 |
|
|
else if ((hx >= 'a')&&(hx <= 'z'))
|
161 |
|
|
return hx-'a'+10;
|
162 |
|
|
else
|
163 |
|
|
return 0;
|
164 |
|
|
}
|
165 |
|
|
|
166 |
|
|
void TTYBUS::writeio(const BUSW a, const BUSW v) {
|
167 |
|
|
|
168 |
|
|
writev(a, 0, 1, &v);
|
169 |
|
|
m_lastaddr = a; m_addr_set = true;
|
170 |
|
|
}
|
171 |
|
|
|
172 |
|
|
void TTYBUS::writev(const BUSW a, const int p, const int len, const BUSW *buf) {
|
173 |
|
|
char *ptr;
|
174 |
|
|
|
175 |
|
|
// Allocate a buffer of six bytes per word, one for addr, plus
|
176 |
|
|
// six more
|
177 |
|
|
bufalloc((len+2)*6);
|
178 |
|
|
|
179 |
|
|
DBGPRINTF("WRITEV(%08x,%d,#%d,0x%08x ...)\n", a, p, len, buf[0]);
|
180 |
|
|
// Encode the address
|
181 |
|
|
ptr = encode_address(a);
|
182 |
|
|
m_lastaddr = a; m_addr_set = true;
|
183 |
|
|
|
184 |
|
|
for(int i=0; i<len; i++) {
|
185 |
|
|
BUSW val = buf[i];
|
186 |
|
|
|
187 |
|
|
int caddr = 0;
|
188 |
|
|
// Let's try compression
|
189 |
|
|
for(int i=1; i<256; i++) {
|
190 |
|
|
unsigned tstaddr;
|
191 |
|
|
tstaddr = (m_wraddr - i) & 0x0ff;
|
192 |
|
|
if ((!m_wrloaded)&&(tstaddr > (unsigned)m_wraddr))
|
193 |
|
|
break;
|
194 |
|
|
if (m_writetbl[tstaddr] == val) {
|
195 |
|
|
caddr = ( m_wraddr- tstaddr ) & 0x0ff;
|
196 |
|
|
break;
|
197 |
|
|
}
|
198 |
|
|
}
|
199 |
|
|
|
200 |
|
|
if (caddr != 0)
|
201 |
|
|
DBGPRINTF("WR[%08x] = %08x (= TBL[%4x] <= %4x)\n", m_lastaddr, val, caddr, m_wraddr);
|
202 |
|
|
else
|
203 |
|
|
DBGPRINTF("WR[%08x] = %08x\n", m_lastaddr, val);
|
204 |
|
|
|
205 |
|
|
if (caddr != 0) {
|
206 |
|
|
*ptr++ = charenc( (((caddr>>6)&0x03)<<1) + (p?1:0) + 0x010);
|
207 |
|
|
*ptr++ = charenc( caddr &0x3f );
|
208 |
|
|
|
209 |
|
|
} else {
|
210 |
|
|
// For testing, let's start just doing this the hard way
|
211 |
|
|
*ptr++ = charenc( (((val>>30)&0x03)<<1) + (p?1:0) + 0x018);
|
212 |
|
|
*ptr++ = charenc( (val>>24)&0x3f);
|
213 |
|
|
*ptr++ = charenc( (val>>18)&0x3f);
|
214 |
|
|
*ptr++ = charenc( (val>>12)&0x3f);
|
215 |
|
|
*ptr++ = charenc( (val>> 6)&0x3f);
|
216 |
|
|
*ptr++ = charenc( (val )&0x3f);
|
217 |
|
|
|
218 |
|
|
m_writetbl[m_wraddr++] = val;
|
219 |
|
|
m_wraddr &= 0x0ff;
|
220 |
|
|
if (m_wraddr == 0) {
|
221 |
|
|
m_wrloaded = true;
|
222 |
|
|
}
|
223 |
|
|
}
|
224 |
|
|
|
225 |
|
|
if (p == 1) m_lastaddr++;
|
226 |
|
|
}
|
227 |
|
|
// *ptr++ = charenc(0x2e);
|
228 |
|
|
*ptr++ = '\n'; *ptr = '\0';
|
229 |
|
|
m_dev->write(m_buf, ptr-m_buf);
|
230 |
|
|
|
231 |
|
|
DBGPRINTF(">> %s\n", m_buf);
|
232 |
|
|
DBGPRINTF("WR: LAST ADDRESS LEFT AT %08x\n", m_lastaddr);
|
233 |
|
|
}
|
234 |
|
|
|
235 |
|
|
void TTYBUS::writez(const BUSW a, const int len, const BUSW *buf) {
|
236 |
|
|
int ln = len;
|
237 |
|
|
const TTYBUS::BUSW *bptr = buf;
|
238 |
|
|
TTYBUS::BUSW addr = a;
|
239 |
|
|
|
240 |
|
|
while((unsigned)ln > MAXWRLEN) {
|
241 |
|
|
writev(addr, 0, MAXWRLEN, bptr);
|
242 |
|
|
bptr += MAXWRLEN;
|
243 |
|
|
ln -= MAXWRLEN;
|
244 |
|
|
// addr += MAXWRLEN;
|
245 |
|
|
} if ((unsigned)ln > 0)
|
246 |
|
|
writev(addr, 0, ln, bptr);
|
247 |
|
|
}
|
248 |
|
|
|
249 |
|
|
void TTYBUS::writei(const BUSW a, const int len, const BUSW *buf) {
|
250 |
|
|
int ln = len;
|
251 |
|
|
const TTYBUS::BUSW *bptr = buf;
|
252 |
|
|
TTYBUS::BUSW addr = a;
|
253 |
|
|
|
254 |
|
|
while((unsigned)ln > MAXWRLEN) {
|
255 |
|
|
writev(addr, 1, MAXWRLEN, bptr);
|
256 |
|
|
bptr += MAXWRLEN;
|
257 |
|
|
ln -= MAXWRLEN;
|
258 |
|
|
addr += MAXWRLEN;
|
259 |
|
|
} if ((unsigned)ln > 0)
|
260 |
|
|
writev(addr, 1, ln, bptr);
|
261 |
|
|
}
|
262 |
|
|
|
263 |
|
|
TTYBUS::BUSW TTYBUS::readio(const TTYBUS::BUSW a) {
|
264 |
|
|
BUSW v;
|
265 |
|
|
|
266 |
|
|
// I/O reads are now the same as vector reads, but with a vector length
|
267 |
|
|
// of one.
|
268 |
|
|
try {
|
269 |
|
|
readv(a, 0, 1, &v);
|
270 |
|
|
} catch(BUSERR b) {
|
271 |
|
|
throw BUSERR(a);
|
272 |
|
|
}
|
273 |
|
|
|
274 |
|
|
if (m_lastaddr != a) {
|
275 |
|
|
DBGPRINTF("LAST-ADDR MIS-MATCH: (RCVD) %08x != %08x (XPECTED)\n", m_lastaddr, a);
|
276 |
|
|
m_addr_set = false;
|
277 |
|
|
|
278 |
|
|
exit(-3);
|
279 |
|
|
}
|
280 |
|
|
|
281 |
|
|
return v;
|
282 |
|
|
}
|
283 |
|
|
|
284 |
|
|
char *TTYBUS::encode_address(const TTYBUS::BUSW a) {
|
285 |
|
|
TTYBUS::BUSW addr = a;
|
286 |
|
|
char *ptr = m_buf;
|
287 |
|
|
|
288 |
|
|
if ((m_addr_set)&&(a == m_lastaddr))
|
289 |
|
|
return ptr;
|
290 |
|
|
|
291 |
|
|
if (m_addr_set) {
|
292 |
|
|
// Encode a difference address
|
293 |
|
|
int diffaddr = addr - m_lastaddr;
|
294 |
|
|
ptr = m_buf;
|
295 |
|
|
if ((diffaddr >= -32)&&(diffaddr < 32)) {
|
296 |
|
|
*ptr++ = charenc(0x09);
|
297 |
|
|
*ptr++ = charenc(diffaddr & 0x03f);
|
298 |
|
|
} else if ((diffaddr >= -2048)&&(diffaddr < 2048)) {
|
299 |
|
|
*ptr++ = charenc(0x0b);
|
300 |
|
|
*ptr++ = charenc((diffaddr>>6) & 0x03f);
|
301 |
|
|
*ptr++ = charenc( diffaddr & 0x03f);
|
302 |
|
|
} else if ((diffaddr >= -(1<<17))&&(diffaddr < (1<<17))) {
|
303 |
|
|
*ptr++ = charenc(0x0d);
|
304 |
|
|
*ptr++ = charenc((diffaddr>>12) & 0x03f);
|
305 |
|
|
*ptr++ = charenc((diffaddr>> 6) & 0x03f);
|
306 |
|
|
*ptr++ = charenc( diffaddr & 0x03f);
|
307 |
|
|
} else if ((diffaddr >= -(1<<23))&&(diffaddr < (1<<23))) {
|
308 |
|
|
*ptr++ = charenc(0x0d);
|
309 |
|
|
*ptr++ = charenc((diffaddr>>18) & 0x03f);
|
310 |
|
|
*ptr++ = charenc((diffaddr>>12) & 0x03f);
|
311 |
|
|
*ptr++ = charenc((diffaddr>> 6) & 0x03f);
|
312 |
|
|
*ptr++ = charenc( diffaddr & 0x03f);
|
313 |
|
|
}
|
314 |
|
|
*ptr = '\0';
|
315 |
|
|
// DBGPRINTF("DIF-ADDR: (%ld) \'%s\'\n", ptr-m_buf, m_buf);
|
316 |
|
|
}
|
317 |
|
|
|
318 |
|
|
{
|
319 |
|
|
// Encode an absolute (low memory) address
|
320 |
|
|
// Prefer absolute address encoding over differential encoding,
|
321 |
|
|
// when both encodings encode the same address, and when both
|
322 |
|
|
// encode the address in the same number of words
|
323 |
|
|
if ((addr <= 0x03f)&&((ptr == m_buf)||(ptr >= &m_buf[2]))) {
|
324 |
|
|
ptr = m_buf;
|
325 |
|
|
*ptr++ = charenc(0x08);
|
326 |
|
|
*ptr++ = charenc(addr);
|
327 |
|
|
} else if((addr <= 0x0fff)&&((ptr == m_buf)||(ptr >= &m_buf[3]))) {
|
328 |
|
|
// DBGPRINTF("Setting ADDR.3 to %08x\n", addr);
|
329 |
|
|
ptr = m_buf;
|
330 |
|
|
*ptr++ = charenc(0x0a);
|
331 |
|
|
*ptr++ = charenc((addr>> 6) & 0x03f);
|
332 |
|
|
*ptr++ = charenc( addr & 0x03f);
|
333 |
|
|
} else if((addr <= 0x03ffff)&&((ptr == m_buf)||(ptr >= &m_buf[4]))) {
|
334 |
|
|
// DBGPRINTF("Setting ADDR.4 to %08x\n", addr);
|
335 |
|
|
ptr = m_buf;
|
336 |
|
|
*ptr++ = charenc(0x0c);
|
337 |
|
|
*ptr++ = charenc((addr>>12) & 0x03f);
|
338 |
|
|
*ptr++ = charenc((addr>> 6) & 0x03f);
|
339 |
|
|
*ptr++ = charenc( addr & 0x03f);
|
340 |
|
|
} else if((addr <= 0x0ffffff)&&((ptr == m_buf)||(ptr >= &m_buf[5]))) {
|
341 |
|
|
// DBGPRINTF("Setting ADDR.5 to %08x\n", addr);
|
342 |
|
|
ptr = m_buf;
|
343 |
|
|
*ptr++ = charenc(0x0e);
|
344 |
|
|
*ptr++ = charenc((addr>>18) & 0x03f);
|
345 |
|
|
*ptr++ = charenc((addr>>12) & 0x03f);
|
346 |
|
|
*ptr++ = charenc((addr>> 6) & 0x03f);
|
347 |
|
|
*ptr++ = charenc( addr & 0x03f);
|
348 |
|
|
} else if (ptr == m_buf) { // Send our address prior to any read
|
349 |
|
|
ptr = m_buf;
|
350 |
|
|
encode(0, addr, ptr);
|
351 |
|
|
ptr+=6;
|
352 |
|
|
}
|
353 |
|
|
}
|
354 |
|
|
|
355 |
|
|
*ptr = '\0';
|
356 |
|
|
// DBGPRINTF("ADDR-CMD: (%ld) \'%s\'\n", ptr-m_buf, m_buf);
|
357 |
|
|
m_rdaddr = 0;
|
358 |
|
|
|
359 |
|
|
return ptr;
|
360 |
|
|
}
|
361 |
|
|
|
362 |
|
|
char *TTYBUS::readcmd(const int inc, const int len, char *buf) {
|
363 |
|
|
char *ptr = buf;
|
364 |
|
|
|
365 |
|
|
// DBGPRINTF("READCMD: LEN = %d\n", len);
|
366 |
|
|
assert(len < 520);
|
367 |
|
|
assert(len > 0);
|
368 |
|
|
|
369 |
|
|
if ((len < 8)||((len == 8)&&(inc))) {
|
370 |
|
|
*ptr++ = charenc(0x20 + (((len-1)&0x07)<<1) + (inc?1:0));
|
371 |
|
|
} else {
|
372 |
|
|
*ptr++ = charenc(0x30 + (((len-8)>>5)&0x0e) + (inc?1:0));
|
373 |
|
|
*ptr++ = charenc( (len-8) & 0x03f);
|
374 |
|
|
}
|
375 |
|
|
|
376 |
|
|
return ptr;
|
377 |
|
|
}
|
378 |
|
|
|
379 |
|
|
void TTYBUS::readv(const TTYBUS::BUSW a, const int inc, const int len, TTYBUS::BUSW *buf) {
|
380 |
|
|
const int READAHEAD = MAXRDLEN/2, READBLOCK=MAXRDLEN/2;
|
381 |
|
|
int cmdrd = 0, nread = 0;
|
382 |
|
|
// TTYBUS::BUSW addr = a;
|
383 |
|
|
char *ptr = m_buf;
|
384 |
|
|
|
385 |
|
|
if (len <= 0)
|
386 |
|
|
return;
|
387 |
|
|
// DBGPRINTF("READV(%08x,%d,#%4d)\n", a, inc, len);
|
388 |
|
|
|
389 |
|
|
ptr = encode_address(a);
|
390 |
|
|
try {
|
391 |
|
|
while(cmdrd < len) {
|
392 |
|
|
// ptr = m_buf;
|
393 |
|
|
do {
|
394 |
|
|
int nrd = len-cmdrd;
|
395 |
|
|
if (nrd > READBLOCK)
|
396 |
|
|
nrd = READBLOCK;
|
397 |
|
|
if (cmdrd-nread + nrd>READAHEAD+READBLOCK)
|
398 |
|
|
nrd = READAHEAD+READBLOCK-(cmdrd-nread);
|
399 |
|
|
ptr = readcmd(inc, nrd, ptr);
|
400 |
|
|
cmdrd += nrd;
|
401 |
|
|
} while((cmdrd-nread < READAHEAD+READBLOCK)&&(cmdrd< len));
|
402 |
|
|
|
403 |
|
|
*ptr++ = '\n'; *ptr = '\0';
|
404 |
|
|
m_dev->write(m_buf, (ptr-m_buf));
|
405 |
|
|
|
406 |
|
|
while(nread<(cmdrd-READAHEAD)) {
|
407 |
|
|
buf[nread++] = readword();
|
408 |
|
|
} ptr = m_buf;
|
409 |
|
|
} while(nread<len) {
|
410 |
|
|
buf[nread++] = readword();
|
411 |
|
|
}
|
412 |
|
|
} catch(BUSERR b) {
|
413 |
|
|
throw BUSERR(a+((inc)?nread:0));
|
414 |
|
|
}
|
415 |
|
|
|
416 |
|
|
if ((unsigned)m_lastaddr != (a+((inc)?(len):0))) {
|
417 |
|
|
printf("TTYBUS::READV(a=%08x,inc=%d,len=%4x,x) ERR: (Last) %08x != %08x + %08x (Expected)\n", a, inc, len, m_lastaddr, a, (inc)?(len):0);
|
418 |
|
|
sleep(1);
|
419 |
|
|
assert((int)m_lastaddr == (a+(inc)?(len):0));
|
420 |
|
|
exit(-3);
|
421 |
|
|
}
|
422 |
|
|
}
|
423 |
|
|
|
424 |
|
|
void TTYBUS::readi(const TTYBUS::BUSW a, const int len, TTYBUS::BUSW *buf) {
|
425 |
|
|
readv(a, 1, len, buf);
|
426 |
|
|
}
|
427 |
|
|
|
428 |
|
|
void TTYBUS::readz(const TTYBUS::BUSW a, const int len, TTYBUS::BUSW *buf) {
|
429 |
|
|
readv(a, 0, len, buf);
|
430 |
|
|
}
|
431 |
|
|
|
432 |
|
|
TTYBUS::BUSW TTYBUS::readword(void) {
|
433 |
|
|
TTYBUS::BUSW val = 0;
|
434 |
|
|
int nr;
|
435 |
|
|
unsigned sixbits;
|
436 |
|
|
|
437 |
|
|
// DBGPRINTF("READ-WORD()\n");
|
438 |
|
|
|
439 |
|
|
bool found_start = false;
|
440 |
|
|
do {
|
441 |
|
|
// Blocking read (for now)
|
442 |
|
|
do {
|
443 |
|
|
nr = lclreadcode(&m_buf[0], 1);
|
444 |
|
|
} while (nr < 1);
|
445 |
|
|
|
446 |
|
|
sixbits = chardec(m_buf[0]);
|
447 |
|
|
|
448 |
|
|
if (sixbits&(~0x03f)) {
|
449 |
|
|
// Ignore new lines, unprintables, and characters
|
450 |
|
|
// not a part of our code
|
451 |
|
|
;
|
452 |
|
|
} else if (sixbits < 6) {
|
453 |
|
|
switch(sixbits) {
|
454 |
|
|
case 0: break; // Idle -- ignore
|
455 |
|
|
case 1: break; // Idle, but the bus is busy
|
456 |
|
|
case 2: break; // Write acknowledgement, ignore it here
|
457 |
|
|
case 3:
|
458 |
|
|
m_bus_err = true;
|
459 |
|
|
throw BUSERR(0);
|
460 |
|
|
break;
|
461 |
|
|
case 4:
|
462 |
|
|
m_interrupt_flag = true;
|
463 |
|
|
break;
|
464 |
|
|
case 5:
|
465 |
|
|
m_bus_err = true;
|
466 |
|
|
throw BUSERR(0);
|
467 |
|
|
break;
|
468 |
|
|
}
|
469 |
|
|
} else if (0x08 == (sixbits & 0x3c)) { // Set 32-bit address
|
470 |
|
|
do {
|
471 |
|
|
nr += lclreadcode(&m_buf[nr], 6-nr);
|
472 |
|
|
} while (nr < 6);
|
473 |
|
|
|
474 |
|
|
val = chardec(m_buf[0]) & 0x03;
|
475 |
|
|
val = (val<<6) | (chardec(m_buf[1]) & 0x03f);
|
476 |
|
|
val = (val<<6) | (chardec(m_buf[2]) & 0x03f);
|
477 |
|
|
val = (val<<6) | (chardec(m_buf[3]) & 0x03f);
|
478 |
|
|
val = (val<<6) | (chardec(m_buf[4]) & 0x03f);
|
479 |
|
|
val = (val<<6) | (chardec(m_buf[5]) & 0x03f);
|
480 |
|
|
|
481 |
|
|
m_addr_set = true;
|
482 |
|
|
m_lastaddr = val;
|
483 |
|
|
|
484 |
|
|
// DBGPRINTF("RCVD ADDR: 0x%08x\n", val);
|
485 |
|
|
} else if (0x0c == (sixbits & 0x03c)) { // Set 32-bit address,compressed
|
486 |
|
|
int nw = (sixbits & 0x03) + 2;
|
487 |
|
|
do {
|
488 |
|
|
nr += lclreadcode(&m_buf[nr], nw-nr);
|
489 |
|
|
} while (nr < nw);
|
490 |
|
|
|
491 |
|
|
if (nw == 2) {
|
492 |
|
|
val = chardec(m_buf[1]);
|
493 |
|
|
} else if (nw == 3) {
|
494 |
|
|
val = chardec(m_buf[1]);
|
495 |
|
|
val = (val<<6) | (chardec(m_buf[2]) & 0x03f);
|
496 |
|
|
} else if (nw == 4) {
|
497 |
|
|
val = chardec(m_buf[1]);
|
498 |
|
|
val = (val<<6) | (chardec(m_buf[2]) & 0x03f);
|
499 |
|
|
val = (val<<6) | (chardec(m_buf[3]) & 0x03f);
|
500 |
|
|
} else { // if (nw == 5)
|
501 |
|
|
val = chardec(m_buf[1]);
|
502 |
|
|
val = (val<<6) | (chardec(m_buf[2]) & 0x03f);
|
503 |
|
|
val = (val<<6) | (chardec(m_buf[3]) & 0x03f);
|
504 |
|
|
val = (val<<6) | (chardec(m_buf[4]) & 0x03f);
|
505 |
|
|
}
|
506 |
|
|
|
507 |
|
|
m_addr_set = true;
|
508 |
|
|
m_lastaddr = val;
|
509 |
|
|
// DBGPRINTF("RCVD ADDR: 0x%08x (%d bytes)\n", val, nw+1);
|
510 |
|
|
} else
|
511 |
|
|
found_start = true;
|
512 |
|
|
} while(!found_start);
|
513 |
|
|
|
514 |
|
|
int rdaddr;
|
515 |
|
|
|
516 |
|
|
if (0x06 == (sixbits & 0x03e)) { // Tbl read, last value
|
517 |
|
|
rdaddr = (m_rdaddr-1)&0x03ff;
|
518 |
|
|
val = m_readtbl[rdaddr];
|
519 |
|
|
m_lastaddr += (sixbits&1);
|
520 |
|
|
} else if (0x10 == (sixbits & 0x030)) { // Tbl read, up to 521 into past
|
521 |
|
|
int idx;
|
522 |
|
|
do {
|
523 |
|
|
nr += lclreadcode(&m_buf[nr], 2-nr);
|
524 |
|
|
} while (nr < 2);
|
525 |
|
|
|
526 |
|
|
idx = (chardec(m_buf[0])>>1) & 0x07;
|
527 |
|
|
idx = ((idx<<6) | (chardec(m_buf[1]) & 0x03f)) + 2 + 8;
|
528 |
|
|
rdaddr = (m_rdaddr-idx)&0x03ff;
|
529 |
|
|
val = m_readtbl[rdaddr];
|
530 |
|
|
m_lastaddr += (sixbits&1);
|
531 |
|
|
} else if (0x20 == (sixbits & 0x030)) { // Tbl read, 2-9 into past
|
532 |
|
|
rdaddr = (m_rdaddr - (((sixbits>>1)&0x07)+2)) & 0x03ff;
|
533 |
|
|
val = m_readtbl[rdaddr];
|
534 |
|
|
m_lastaddr += (sixbits&1);
|
535 |
|
|
} else if (0x38 == (sixbits & 0x038)) { // Raw read
|
536 |
|
|
do {
|
537 |
|
|
nr += lclreadcode(&m_buf[nr], 6-nr);
|
538 |
|
|
} while (nr < 6);
|
539 |
|
|
|
540 |
|
|
val = (chardec(m_buf[0])>>1) & 0x03;
|
541 |
|
|
val = (val<<6) | (chardec(m_buf[1]) & 0x03f);
|
542 |
|
|
val = (val<<6) | (chardec(m_buf[2]) & 0x03f);
|
543 |
|
|
val = (val<<6) | (chardec(m_buf[3]) & 0x03f);
|
544 |
|
|
val = (val<<6) | (chardec(m_buf[4]) & 0x03f);
|
545 |
|
|
val = (val<<6) | (chardec(m_buf[5]) & 0x03f);
|
546 |
|
|
|
547 |
|
|
m_readtbl[m_rdaddr++] = val; m_rdaddr &= 0x03ff;
|
548 |
|
|
m_lastaddr += (sixbits&1);
|
549 |
|
|
}
|
550 |
|
|
|
551 |
|
|
return val;
|
552 |
|
|
}
|
553 |
|
|
|
554 |
|
|
void TTYBUS::usleep(unsigned ms) {
|
555 |
|
|
if (m_dev->poll(ms)) {
|
556 |
|
|
int nr;
|
557 |
|
|
nr = m_dev->read(m_buf, 16);
|
558 |
|
|
if (nr == 0) {
|
559 |
|
|
// Connection closed, let it drop
|
560 |
|
|
DBGPRINTF("Connection closed!!\n");
|
561 |
|
|
m_dev->close();
|
562 |
|
|
exit(-1);
|
563 |
|
|
} for(int i=0; i<nr; i++) {
|
564 |
|
|
if (m_buf[i] == TTYC_INT) {
|
565 |
|
|
m_interrupt_flag = true;
|
566 |
|
|
// DBGPRINTF("!!!!!!!!!!!!!!!!! ----- INTERRUPT!\n");
|
567 |
|
|
} else if (m_buf[i] == TTYC_IDLE) {
|
568 |
|
|
// DBGPRINTF("Interface is now idle\n");
|
569 |
|
|
} else if (m_buf[i] == TTYC_WRITE) {
|
570 |
|
|
} else if (m_buf[i] == TTYC_RESET) {
|
571 |
|
|
// DBGPRINTF("Bus was RESET!\n");
|
572 |
|
|
} else if (m_buf[i] == TTYC_ERR) {
|
573 |
|
|
DBGPRINTF("Bus error\n");
|
574 |
|
|
} else if (m_buf[i] == TTYC_BUSY) {
|
575 |
|
|
// DBGPRINTF("Interface is ... busy ??\n");
|
576 |
|
|
}
|
577 |
|
|
// else if (m_buf[nr] == 'Q')
|
578 |
|
|
// else if (m_buf[nr] == 'W')
|
579 |
|
|
// else if (m_buf[nr] == '\n')
|
580 |
|
|
}
|
581 |
|
|
}
|
582 |
|
|
}
|
583 |
|
|
|
584 |
|
|
void TTYBUS::wait(void) {
|
585 |
|
|
if (m_interrupt_flag)
|
586 |
|
|
DBGPRINTF("INTERRUPTED PRIOR TO WAIT()\n");
|
587 |
|
|
do {
|
588 |
|
|
usleep(200);
|
589 |
|
|
} while(!m_interrupt_flag);
|
590 |
|
|
}
|
591 |
|
|
|
592 |
|
|
// TTYBUS: 3503421 ~= 3.3 MB, stopwatch = 1:18.5 seconds, vs 53.8 secs
|
593 |
|
|
// If you issue two 512 word reads at once, time drops to 41.6 secs.
|
594 |
|
|
// PORTBUS: 6408320 ~= 6.1 MB, ... 26% improvement, 53 seconds real time
|
595 |
|
|
|