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