OpenCores
URL https://opencores.org/ocsvn/s6soc/s6soc/trunk

Subversion Repositories s6soc

[/] [s6soc/] [trunk/] [sw/] [host/] [ttybus.cpp] - Diff between revs 11 and 45

Only display areas with differences | Details | Blame | View Log

Rev 11 Rev 45
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
//
//
// Filename:    ttybus.cpp
// Filename:    ttybus.cpp
//
//
// Project:     CMod S6 System on a Chip, ZipCPU demonstration project
// Project:     CMod S6 System on a Chip, ZipCPU demonstration project
//
//
// Purpose:     This is the C++ program on the command side that will interact
// Purpose:     This is the C++ program on the command side that will interact
//              with a serial port (through DEPP) on an FPGA, to command the
//              with a serial port (through DEPP) on an FPGA, to command the
//      WISHBONE on that same FPGA to ... whatever we wish to command it to do.
//      WISHBONE on that same FPGA to ... whatever we wish to command it to do.
//
//
//      This code does not run on an FPGA, is not a test bench, neither is it
//      This code does not run on an FPGA, is not a test bench, neither
//      a simulator.  It is a portion of a command program for commanding an
//      is it a simulator.  It is a portion of a command program
//      FPGA from an attached host computer.
//      for commanding an FPGA from an attached host computer.
//
//
// Creator:     Dan Gisselquist, Ph.D.
// Creator:     Dan Gisselquist, Ph.D.
//              Gisselquist Technology, LLC
//              Gisselquist Technology, LLC
//
//
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
//
//
// Copyright (C) 2015-2016, Gisselquist Technology, LLC
// Copyright (C) 2015-2017, Gisselquist Technology, LLC
//
//
// This program is free software (firmware): you can redistribute it and/or
// This program is free software (firmware): you can redistribute it and/or
// modify it under the terms of  the GNU General Public License as published
// modify it under the terms of  the GNU General Public License as published
// by the Free Software Foundation, either version 3 of the License, or (at
// by the Free Software Foundation, either version 3 of the License, or (at
// your option) any later version.
// your option) any later version.
//
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
// for more details.
// for more details.
//
//
// You should have received a copy of the GNU General Public License along
// You should have received a copy of the GNU General Public License along
// with this program.  (It's in the $(ROOT)/doc directory, run make with no
// with this program.  (It's in the $(ROOT)/doc directory.  Run make with no
// target there if the PDF file isn't present.)  If not, see
// target there if the PDF file isn't present.)  If not, see
// <http://www.gnu.org/licenses/> for a copy.
// <http://www.gnu.org/licenses/> for a copy.
//
//
// License:     GPL, v3, as defined and found on www.gnu.org,
// License:     GPL, v3, as defined and found on www.gnu.org,
//              http://www.gnu.org/licenses/gpl.html
//              http://www.gnu.org/licenses/gpl.html
//
//
//
//
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
//
//
//
//
//
 
#include <sys/socket.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <fcntl.h>
#include <termios.h>
#include <termios.h>
#include <netinet/in.h>
#include <netinet/in.h>
#include <netdb.h>
#include <netdb.h>
#include <stdio.h>
#include <stdio.h>
#include <string.h>
#include <string.h>
#include <stdlib.h>
#include <stdlib.h>
#include <unistd.h>
#include <unistd.h>
#include <errno.h>
#include <errno.h>
#include <arpa/inet.h> 
#include <arpa/inet.h> 
#include <assert.h> 
#include <assert.h> 
#include <strings.h> 
#include <strings.h> 
#include <poll.h> 
#include <poll.h> 
#include <ctype.h> 
#include <ctype.h> 
 
 
#include "ttybus.h"
#include "ttybus.h"
 
 
#define TTYC_IDLE       '0'
#define TTYC_IDLE       '0'
#define TTYC_BUSY       '1'
#define TTYC_BUSY       '1'
#define TTYC_WRITE      '2'
#define TTYC_WRITE      '2'
#define TTYC_RESET      '3'
#define TTYC_RESET      '3'
#define TTYC_INT        '4'
#define TTYC_INT        '4'
#define TTYC_ERR        '5'
#define TTYC_ERR        '5'
 
 
const   unsigned TTYBUS::MAXRDLEN = 1024;
const   unsigned TTYBUS::MAXRDLEN = 64;
const   unsigned TTYBUS::MAXWRLEN = 32;
const   unsigned TTYBUS::MAXWRLEN = 32;
 
 
#define DBGPRINTF       null
 
// #define      DBGPRINTF       printf
// #define      DBGPRINTF       printf
// #define      DBGPRINTF       filedump
// #define      DBGPRINTF       filedump
 
#ifndef DBGPRINTF
 
#define DBGPRINTF       null
 
#else
 
#warning "TTYBUS DEBUG IS TURNED ON"
 
#endif
 
 
void    null(...) {}
void    null(...) {}
#include <stdarg.h>
 
// #include <varargs.h>
#include <stdarg.h> // replaces the (defunct) varargs.h include file
/*
void    filedump(const char *fmt, ...) {
void    filedump(const char *fmt, ...) {
        static  FILE *dbgfp = NULL;
        static  FILE *dbgfp = NULL;
        va_list args;
        va_list args;
 
 
        if (!dbgfp)
        if (!dbgfp)
                dbgfp = fopen("debug.txt", "w");
                dbgfp = fopen("debug.txt", "w");
        va_start(args, fmt);
        va_start(args, fmt);
        vfprintf(dbgfp, fmt, args);
        vfprintf(dbgfp, fmt, args);
        va_end(args);
        va_end(args);
        fflush(dbgfp);
        fflush(dbgfp);
 
}
        // If you want the debug output to go to stderr as well, you can
*/
        // uncomment the next couple of lines
 
        // va_start(args, fmt);
 
        // vfprintf(stderr, fmt, args);
 
        // va_end(args);
 
}
 
 
char    TTYBUS::charenc(const int sixbitval) const {
char    TTYBUS::charenc(const int sixbitval) const {
        if (sixbitval < 10)
        if (sixbitval < 10)
                return '0' + sixbitval;
                return '0' + sixbitval;
        else if (sixbitval < 10+26)
        else if (sixbitval < 10+26)
                return 'A' - 10 + sixbitval;
                return 'A' - 10 + sixbitval;
        else if (sixbitval < 10+26+26)
        else if (sixbitval < 10+26+26)
                return 'a' - 10 - 26 + sixbitval;
                return 'a' - 10 - 26 + sixbitval;
        else if (sixbitval == 0x3e)
        else if (sixbitval == 0x3e)
                return '@';
                return '@';
        else if (sixbitval == 0x3f)
        else if (sixbitval == 0x3f)
                return '%';
                return '%';
 
 
        fprintf(stderr, "INTERNAL ERR: SIXBITVAL isn\'t!!!! sixbitval = %08x\n", sixbitval);
        fprintf(stderr, "INTERNAL ERR: SIXBITVAL isn\'t!!!! sixbitval = %08x\n", sixbitval);
        assert((sixbitval & (~0x03f))==0);
        assert((sixbitval & (~0x03f))==0);
        return 0;
        return 0;
}
}
 
 
unsigned        TTYBUS::chardec(const char b) const {
unsigned        TTYBUS::chardec(const char b) const {
        if ((b >= '0')&&(b <= '9'))
        if ((b >= '0')&&(b <= '9'))
                return b-'0';
                return b-'0';
        else if ((b >= 'A')&&(b <= 'Z'))
        else if ((b >= 'A')&&(b <= 'Z'))
                return b-'A'+10;
                return b-'A'+10;
        else if ((b >= 'a')&&(b <= 'z'))
        else if ((b >= 'a')&&(b <= 'z'))
                return b-'a'+36;
                return b-'a'+36;
        else if (b == '@')
        else if (b == '@')
                return 0x03e;
                return 0x03e;
        else if (b == '%')
        else if (b == '%')
                return 0x03f;
                return 0x03f;
        else
        else
                return 0x0100; // ERR -- invalid code
                return 0x0100; // ERR -- invalid code
}
}
 
 
int     TTYBUS::lclreadcode(char *buf, int len) {
int     TTYBUS::lclreadcode(char *buf, int len) {
        char    *sp, *dp;
        char    *sp, *dp;
        int     nr, ret;
        int     nr, ret;
 
 
        nr = m_dev->read(buf, len);
        nr = m_dev->read(buf, len);
        m_total_nread += nr;
        m_total_nread += nr;
        ret = nr; sp = buf; dp = buf;
        ret = nr; sp = buf; dp = buf;
        for(int i=0; i<nr; i++) {
        for(int i=0; i<nr; i++) {
                if (chardec(*sp)&(~0x3f)) {
                if (chardec(*sp)&(~0x3f)) {
                        ret--;  // Skip this value, not a valid codeword
                        ret--;  // Skip this value, not a valid codeword
                        sp++;
                        sp++;
                } else {
                } else {
                        *sp++ = *dp++;
                        *sp++ = *dp++;
                }
                }
        } return ret;
        } return ret;
}
}
 
 
void    TTYBUS::bufalloc(int len) {
void    TTYBUS::bufalloc(int len) {
        if ((m_buf)&&(m_buflen >= len))
        if ((m_buf)&&(m_buflen >= len))
                return;
                return;
        if (m_buf)
        if (m_buf)
                delete[] m_buf;
                delete[] m_buf;
        m_buflen = (len&(-0x3f))+0x40;
        m_buflen = (len&(-0x3f))+0x40;
        m_buf = new char[m_buflen];
        m_buf = new char[m_buflen];
}
}
 
 
void    TTYBUS::encode(const int hb, const BUSW val, char *buf) const {
void    TTYBUS::encode(const int hb, const BUSW val, char *buf) const {
        buf[0] = charenc( (hb<<2)|((val>>30)&0x03) );
        buf[0] = charenc( (hb<<2)|((val>>30)&0x03) );
        buf[1] = charenc( (val>>24)&0x3f);
        buf[1] = charenc( (val>>24)&0x3f);
        buf[2] = charenc( (val>>18)&0x3f);
        buf[2] = charenc( (val>>18)&0x3f);
        buf[3] = charenc( (val>>12)&0x3f);
        buf[3] = charenc( (val>>12)&0x3f);
        buf[4] = charenc( (val>> 6)&0x3f);
        buf[4] = charenc( (val>> 6)&0x3f);
        buf[5] = charenc( (val    )&0x3f);
        buf[5] = charenc( (val    )&0x3f);
}
}
 
 
unsigned        TTYBUS::decodestr(const char *buf) const {
unsigned        TTYBUS::decodestr(const char *buf) const {
        unsigned        r;
        unsigned        r;
 
 
        r = chardec(buf[0]) & 0x03;
        r = chardec(buf[0]) & 0x03;
        r = (r<<6) | (chardec(buf[1]) & 0x03f);
        r = (r<<6) | (chardec(buf[1]) & 0x03f);
        r = (r<<6) | (chardec(buf[2]) & 0x03f);
        r = (r<<6) | (chardec(buf[2]) & 0x03f);
        r = (r<<6) | (chardec(buf[3]) & 0x03f);
        r = (r<<6) | (chardec(buf[3]) & 0x03f);
        r = (r<<6) | (chardec(buf[4]) & 0x03f);
        r = (r<<6) | (chardec(buf[4]) & 0x03f);
        r = (r<<6) | (chardec(buf[5]) & 0x03f);
        r = (r<<6) | (chardec(buf[5]) & 0x03f);
 
 
        return r;
        return r;
}
}
 
 
int     TTYBUS::decodehex(const char hx) const {
int     TTYBUS::decodehex(const char hx) const {
        if ((hx >= '0')&&(hx <= '9'))
        if ((hx >= '0')&&(hx <= '9'))
                return hx-'0';
                return hx-'0';
        else if ((hx >= 'A')&&(hx <= 'Z'))
        else if ((hx >= 'A')&&(hx <= 'Z'))
                return hx-'A'+10;
                return hx-'A'+10;
        else if ((hx >= 'a')&&(hx <= 'z'))
        else if ((hx >= 'a')&&(hx <= 'z'))
                return hx-'a'+10;
                return hx-'a'+10;
        else
        else
                return 0;
                return 0;
}
}
 
 
void    TTYBUS::writeio(const BUSW a, const BUSW v) {
void    TTYBUS::writeio(const BUSW a, const BUSW v) {
 
 
        writev(a, 0, 1, &v);
        writev(a, 0, 1, &v);
        m_lastaddr = a; m_addr_set = true;
        m_lastaddr = a; m_addr_set = true;
}
}
 
 
void    TTYBUS::writev(const BUSW a, const int p, const int len, const BUSW *buf) {
void    TTYBUS::writev(const BUSW a, const int p, const int len, const BUSW *buf) {
        char    *ptr;
        char    *ptr;
 
        int     nw = 0;
 
 
 
        // We'll never be called with more than MAXWRLEN words to write at once.
 
        // This is a configurable option length, set at the top of this file.
 
        // (currently set at 32, but subject to change ...)  This is important,
 
        // as the return channel *must* be capable of holding at least this many
 
        // acknowledgments in its buffer.
 
        //
 
        // assert(len <= MAXWRLEN);
 
 
        // Allocate a buffer of six bytes per word, one for addr, plus
        // Allocate a buffer of six bytes per word, one for addr, plus
        // six more
        // six more
        bufalloc((len+2)*6);
        bufalloc((len+2)*6);
 
 
        DBGPRINTF("WRITEV(%08x,%d,#%d,0x%08x ...)\n", a, p, len, buf[0]);
        DBGPRINTF("WRITEV(%08x,%d,#%d,0x%08x ...)\n", a, p, len, buf[0]);
        // Encode the address
        // Encode the address
        ptr = encode_address(a);
        ptr = encode_address(a);
        m_lastaddr = a; m_addr_set = true;
        m_lastaddr = a; m_addr_set = true;
 
 
        for(int i=0; i<len; i++) {
        while(nw < len) {
                BUSW    val = buf[i];
                int     ln = len-nw;
 
                if ((unsigned)ln > MAXWRLEN)
 
                        ln = MAXWRLEN;
 
 
 
                DBGPRINTF("WRITEV-SUB(%08x%s,#%d,&buf[%d])\n", a+nw, (p)?"++":"", ln, nw);
 
                for(int i=0; i<ln; i++) {
 
                        BUSW    val = buf[nw+i];
 
 
                int     caddr = 0;
                        int     caddr = 0;
                // Let's try compression
                        // Let's try compression
                for(int i=1; i<256; i++) {
                        for(int i=1; i<256; i++) {
                        unsigned        tstaddr;
                                unsigned        tstaddr;
                        tstaddr = (m_wraddr - i) & 0x0ff;
                                tstaddr = (m_wraddr - i) & 0x0ff;
                        if ((!m_wrloaded)&&(tstaddr > (unsigned)m_wraddr))
                                if ((!m_wrloaded)&&(tstaddr > (unsigned)m_wraddr))
                                break;
                                        break;
                        if (m_writetbl[tstaddr] == val) {
                                if (m_writetbl[tstaddr] == val) {
                                caddr = ( m_wraddr- tstaddr ) & 0x0ff;
                                        caddr = ( m_wraddr- tstaddr ) & 0x0ff;
                                break;
                                        break;
                        }
                                }
                }
                        }
 
 
                if (caddr != 0)
                /*
                        DBGPRINTF("WR[%08x] = %08x (= TBL[%4x] <= %4x)\n", m_lastaddr, val, caddr, m_wraddr);
                if (caddr != 0)
                else
                        DBGPRINTF("WR[%08x] = %08x (= TBL[%4x] <= %4x)\n", m_lastaddr, val, caddr, m_wraddr);
                        DBGPRINTF("WR[%08x] = %08x\n", m_lastaddr, val);
                else
 
                        DBGPRINTF("WR[%08x] = %08x\n", m_lastaddr, val);
 
                */
 
 
                if (caddr != 0) {
                        if (caddr != 0) {
                        *ptr++ = charenc( (((caddr>>6)&0x03)<<1) + (p?1:0) + 0x010);
                                *ptr++ = charenc( (((caddr>>6)&0x03)<<1) + (p?1:0) + 0x010);
                        *ptr++ = charenc(    caddr    &0x3f    );
                                *ptr++ = charenc(    caddr    &0x3f    );
 
 
                } else {
                        } else {
                        // For testing, let's start just doing this the hard way
                                // For testing, let's start just doing this the hard way
                        *ptr++ = charenc( (((val>>30)&0x03)<<1) + (p?1:0) + 0x018);
                                *ptr++ = charenc( (((val>>30)&0x03)<<1) + (p?1:0) + 0x018);
                        *ptr++ = charenc( (val>>24)&0x3f);
                                *ptr++ = charenc( (val>>24)&0x3f);
                        *ptr++ = charenc( (val>>18)&0x3f);
                                *ptr++ = charenc( (val>>18)&0x3f);
                        *ptr++ = charenc( (val>>12)&0x3f);
                                *ptr++ = charenc( (val>>12)&0x3f);
                        *ptr++ = charenc( (val>> 6)&0x3f);
                                *ptr++ = charenc( (val>> 6)&0x3f);
                        *ptr++ = charenc( (val    )&0x3f);
                                *ptr++ = charenc( (val    )&0x3f);
 
 
                        m_writetbl[m_wraddr++] = val;
                                m_writetbl[m_wraddr++] = val;
                        m_wraddr &= 0x0ff;
                                m_wraddr &= 0x0ff;
                        if (m_wraddr == 0) {
                                if (m_wraddr == 0) {
                                m_wrloaded = true;
                                        m_wrloaded = true;
                        }
                                }
                }
                        }
 
 
                if (p == 1) m_lastaddr++;
                        if (p == 1) m_lastaddr+=4;
        }
        }
        // *ptr++ = charenc(0x2e);
        // *ptr++ = charenc(0x2e);
        *ptr++ = '\n'; *ptr = '\0';
                if (ln == len-nw)
 
                        *ptr++ = '\n';
 
                *ptr = '\0';
        m_dev->write(m_buf, ptr-m_buf);
        m_dev->write(m_buf, ptr-m_buf);
 
 
        DBGPRINTF(">> %s\n", m_buf);
        DBGPRINTF(">> %s\n", m_buf);
 
 
 
                readidle();
 
 
 
                nw += ln;
 
                ptr = m_buf;
 
        }
        DBGPRINTF("WR: LAST ADDRESS LEFT AT %08x\n", m_lastaddr);
        DBGPRINTF("WR: LAST ADDRESS LEFT AT %08x\n", m_lastaddr);
 
 
 
        // Need to clear the incoming queue ... if there's anything there.
 
        // We could do a ...
 
        //      readacks(len);
 
        // to clear a known number of acks (up to half the length of our buffer
 
        // which we can let just sit for speed ...), or we could do a ...
 
        //      readtilidle(void);
 
        // Then, upon startup we could also start with a readtilidle(); command.
 
        // This would help to clear out the problems between programs, where
 
        // one program doesn't finish reading, and the next gets a confusing
 
        // message.
 
        readidle();
}
}
 
 
void    TTYBUS::writez(const BUSW a, const int len, const BUSW *buf) {
void    TTYBUS::writez(const BUSW a, const int len, const BUSW *buf) {
        int     ln = len;
        writev(a, 0, len, buf);
        const TTYBUS::BUSW *bptr = buf;
 
        TTYBUS::BUSW addr = a;
 
 
 
        while((unsigned)ln > MAXWRLEN) {
 
                writev(addr, 0, MAXWRLEN, bptr);
 
                bptr += MAXWRLEN;
 
                ln   -= MAXWRLEN;
 
                // addr += MAXWRLEN;
 
        } if ((unsigned)ln > 0)
 
                writev(addr, 0, ln, bptr);
 
}
}
 
 
void    TTYBUS::writei(const BUSW a, const int len, const BUSW *buf) {
void    TTYBUS::writei(const BUSW a, const int len, const BUSW *buf) {
        int     ln = len;
        writev(a, 1, len, buf);
        const TTYBUS::BUSW *bptr = buf;
 
        TTYBUS::BUSW addr = a;
 
 
 
        while((unsigned)ln > MAXWRLEN) {
 
                writev(addr, 1, MAXWRLEN, bptr);
 
                bptr += MAXWRLEN;
 
                ln   -= MAXWRLEN;
 
                addr += MAXWRLEN;
 
        } if ((unsigned)ln > 0)
 
                writev(addr, 1, ln, bptr);
 
}
}
 
 
TTYBUS::BUSW    TTYBUS::readio(const TTYBUS::BUSW a) {
TTYBUS::BUSW    TTYBUS::readio(const TTYBUS::BUSW a) {
        BUSW    v;
        BUSW    v;
 
 
        // I/O reads are now the same as vector reads, but with a vector length
        // I/O reads are now the same as vector reads, but with a vector length
        // of one.
        // of one.
        DBGPRINTF("READIO(0x%08x)\n", a);
        DBGPRINTF("READIO(0x%08x)\n", a);
        try {
        try {
                readv(a, 0, 1, &v);
                readv(a, 0, 1, &v);
        } catch(BUSERR b) {
        } catch(BUSERR b) {
 
                DBGPRINTF("BUSERR trying to read %08x\n", a);
                throw BUSERR(a);
                throw BUSERR(a);
        }
        }
 
 
        if (m_lastaddr != a) {
        if (m_lastaddr != a) {
                DBGPRINTF("LAST-ADDR MIS-MATCH: (RCVD) %08x != %08x (XPECTED)\n", m_lastaddr, a);
                DBGPRINTF("LAST-ADDR MIS-MATCH: (RCVD) %08x != %08x (XPECTED)\n", m_lastaddr, a);
                m_addr_set = false;
                m_addr_set = false;
 
 
                exit(-3);
                exit(-3);
        }
        }
 
 
        return v;
        return v;
}
}
 
 
char    *TTYBUS::encode_address(const TTYBUS::BUSW a) {
char    *TTYBUS::encode_address(const TTYBUS::BUSW a) {
        TTYBUS::BUSW    addr = a;
        TTYBUS::BUSW    addr = a>>2;
        char    *ptr = m_buf;
        char    *ptr = m_buf;
 
 
// #warning DEBUG_APPROACH
        // Double check that we are aligned
        // encode(0, addr, ptr);
        if ((a&3)!=0) {
        // return ptr+6;
                throw BUSERR(a);
 
        }
 
 
        if ((m_addr_set)&&(a == m_lastaddr))
        if ((m_addr_set)&&(a == m_lastaddr))
                return ptr;
                return ptr;
 
 
        if (m_addr_set) {
        if (m_addr_set) {
                // Encode a difference address
                // Encode a difference address
                int     diffaddr = addr - m_lastaddr;
                int     diffaddr = (a - m_lastaddr)>>2;
                ptr = m_buf;
                ptr = m_buf;
                if ((diffaddr >= -32)&&(diffaddr < 32)) {
                if ((diffaddr >= -32)&&(diffaddr < 32)) {
                        *ptr++ = charenc(0x09);
                        *ptr++ = charenc(0x09);
                        *ptr++ = charenc(diffaddr & 0x03f);
                        *ptr++ = charenc(diffaddr & 0x03f);
                } else if ((diffaddr >= -2048)&&(diffaddr < 2048)) {
                } else if ((diffaddr >= -2048)&&(diffaddr < 2048)) {
                        *ptr++ = charenc(0x0b);
                        *ptr++ = charenc(0x0b);
                        *ptr++ = charenc((diffaddr>>6) & 0x03f);
                        *ptr++ = charenc((diffaddr>>6) & 0x03f);
                        *ptr++ = charenc( diffaddr     & 0x03f);
                        *ptr++ = charenc( diffaddr     & 0x03f);
                } else if ((diffaddr >= -(1<<17))&&(diffaddr < (1<<17))) {
                } else if ((diffaddr >= -(1<<17))&&(diffaddr < (1<<17))) {
                        *ptr++ = charenc(0x0d);
                        *ptr++ = charenc(0x0d);
                        *ptr++ = charenc((diffaddr>>12) & 0x03f);
                        *ptr++ = charenc((diffaddr>>12) & 0x03f);
                        *ptr++ = charenc((diffaddr>> 6) & 0x03f);
                        *ptr++ = charenc((diffaddr>> 6) & 0x03f);
                        *ptr++ = charenc( diffaddr      & 0x03f);
                        *ptr++ = charenc( diffaddr      & 0x03f);
                } else if ((diffaddr >= -(1<<23))&&(diffaddr < (1<<23))) {
                } else if ((diffaddr >= -(1<<23))&&(diffaddr < (1<<23))) {
                        *ptr++ = charenc(0x0d);
                        *ptr++ = charenc(0x0d);
                        *ptr++ = charenc((diffaddr>>18) & 0x03f);
                        *ptr++ = charenc((diffaddr>>18) & 0x03f);
                        *ptr++ = charenc((diffaddr>>12) & 0x03f);
                        *ptr++ = charenc((diffaddr>>12) & 0x03f);
                        *ptr++ = charenc((diffaddr>> 6) & 0x03f);
                        *ptr++ = charenc((diffaddr>> 6) & 0x03f);
                        *ptr++ = charenc( diffaddr      & 0x03f);
                        *ptr++ = charenc( diffaddr      & 0x03f);
                }
                }
                *ptr = '\0';
                *ptr = '\0';
                DBGPRINTF("DIF-ADDR: (%ld) \'%s\'\n", ptr-m_buf, m_buf);
                DBGPRINTF("DIF-ADDR: (%ld) \'%s\' encodes last_addr(0x%08x) %c %d(0x%08x)\n",
 
                        ptr-m_buf, m_buf,
 
                        m_lastaddr, (diffaddr<0)?'-':'+',
 
                        diffaddr, diffaddr&0x0ffffffff);
        }
        }
 
 
        {
        {
                // Encode an absolute (low memory) address
                // Encode an absolute (low memory) address
                // Prefer absolute address encoding over differential encoding,
                // Prefer absolute address encoding over differential encoding,
                // when both encodings encode the same address, and when both
                // when both encodings encode the same address, and when both
                // encode the address in the same number of words
                // encode the address in the same number of words
                if ((addr <= 0x03f)&&((ptr == m_buf)||(ptr >= &m_buf[2]))) {
                if ((addr <= 0x03f)&&((ptr == m_buf)||(ptr >= &m_buf[2]))) {
                        ptr = m_buf;
                        ptr = m_buf;
                        *ptr++ = charenc(0x08);
                        *ptr++ = charenc(0x08);
                        *ptr++ = charenc(addr);
                        *ptr++ = charenc(addr);
                } else if((addr <= 0x0fff)&&((ptr == m_buf)||(ptr >= &m_buf[3]))) {
                } else if((addr <= 0x0fff)&&((ptr == m_buf)||(ptr >= &m_buf[3]))) {
                        DBGPRINTF("Setting ADDR.3 to %08x\n", addr);
                        DBGPRINTF("Setting ADDR.3 to %08x\n", addr);
                        ptr = m_buf;
                        ptr = m_buf;
                        *ptr++ = charenc(0x0a);
                        *ptr++ = charenc(0x0a);
                        *ptr++ = charenc((addr>> 6) & 0x03f);
                        *ptr++ = charenc((addr>> 6) & 0x03f);
                        *ptr++ = charenc( addr      & 0x03f);
                        *ptr++ = charenc( addr      & 0x03f);
                } else if((addr <= 0x03ffff)&&((ptr == m_buf)||(ptr >= &m_buf[4]))) {
                } else if((addr <= 0x03ffff)&&((ptr == m_buf)||(ptr >= &m_buf[4]))) {
                        DBGPRINTF("Setting ADDR.4 to %08x\n", addr);
                        DBGPRINTF("Setting ADDR.4 to %08x\n", addr);
                        ptr = m_buf;
                        ptr = m_buf;
                        *ptr++ = charenc(0x0c);
                        *ptr++ = charenc(0x0c);
                        *ptr++ = charenc((addr>>12) & 0x03f);
                        *ptr++ = charenc((addr>>12) & 0x03f);
                        *ptr++ = charenc((addr>> 6) & 0x03f);
                        *ptr++ = charenc((addr>> 6) & 0x03f);
                        *ptr++ = charenc( addr      & 0x03f);
                        *ptr++ = charenc( addr      & 0x03f);
                } else if((addr <= 0x0ffffff)&&((ptr == m_buf)||(ptr >= &m_buf[5]))) {
                } else if((addr <= 0x0ffffff)&&((ptr == m_buf)||(ptr >= &m_buf[5]))) {
                        DBGPRINTF("Setting ADDR.5 to %08x\n", addr);
                        DBGPRINTF("Setting ADDR.5 to %08x\n", addr);
                        ptr = m_buf;
                        ptr = m_buf;
                        *ptr++ = charenc(0x0e);
                        *ptr++ = charenc(0x0e);
                        *ptr++ = charenc((addr>>18) & 0x03f);
                        *ptr++ = charenc((addr>>18) & 0x03f);
                        *ptr++ = charenc((addr>>12) & 0x03f);
                        *ptr++ = charenc((addr>>12) & 0x03f);
                        *ptr++ = charenc((addr>> 6) & 0x03f);
                        *ptr++ = charenc((addr>> 6) & 0x03f);
                        *ptr++ = charenc( addr      & 0x03f);
                        *ptr++ = charenc( addr      & 0x03f);
                } else if (ptr == m_buf) { // Send our address prior to any read
                } else if (ptr == m_buf) { // Send our address prior to any read
                        // ptr = m_buf;
                        // ptr = m_buf;
                        encode(0, addr, ptr);
                        encode(0, addr, ptr);
                        ptr+=6;
                        ptr+=6;
                }
                }
        }
        }
 
 
        *ptr = '\0';
        *ptr = '\0';
        DBGPRINTF("ADDR-CMD: (%ld) \'%s\'\n", ptr-m_buf, m_buf);
        DBGPRINTF("ADDR-CMD: (%ld) \'%s\'\n", ptr-m_buf, m_buf);
        m_rdaddr = 0;
        m_rdaddr = 0;
 
 
        return ptr;
        return ptr;
}
}
 
 
char    *TTYBUS::readcmd(const int inc, const int len, char *buf) {
char    *TTYBUS::readcmd(const int inc, const int len, char *buf) {
        char    *ptr = buf;
        char    *ptr = buf;
 
 
        DBGPRINTF("READCMD: LEN = %d\n", len);
        DBGPRINTF("READCMD: LEN = %d: ", len);
        assert(len < 520);
        assert(len < 520);
        assert(len > 0);
        assert(len > 0);
 
 
        if ((len < 8)||((len == 8)&&(inc))) {
        if (len <= 8) {
                *ptr++ = charenc(0x20 + (((len-1)&0x07)<<1) + (inc?1:0));
                *ptr++ = charenc(0x20 + (((len-1)&0x07)<<1) + (inc?1:0));
 
                DBGPRINTF("%c\n", ptr[-1]);
        } else {
        } else {
                *ptr++ = charenc(0x30 + (((len-8)>>5)&0x0e) + (inc?1:0));
                *ptr++ = charenc(0x30 + (((len-9)>>5)&0x0e) + (inc?1:0));
                *ptr++ = charenc( (len-8) & 0x03f);
                *ptr++ = charenc( (len-9) & 0x03f);
 
                DBGPRINTF("%c%c\n", ptr[-2], ptr[-1]);
        }
        }
 
 
        return ptr;
        return ptr;
}
}
 
 
void    TTYBUS::readv(const TTYBUS::BUSW a, const int inc, const int len, TTYBUS::BUSW *buf) {
void    TTYBUS::readv(const TTYBUS::BUSW a, const int inc, const int len, TTYBUS::BUSW *buf) {
        const   int     READAHEAD = MAXRDLEN/2, READBLOCK=MAXRDLEN/2;
        const   int     READAHEAD = 0, READBLOCK=(MAXRDLEN/2>512)?512:MAXRDLEN/2;
        int     cmdrd = 0, nread = 0;
        int     cmdrd = 0, nread = 0;
        // TTYBUS::BUSW addr = a;
        // TTYBUS::BUSW addr = a;
        char    *ptr = m_buf;
        char    *ptr = m_buf;
 
 
        if (len <= 0)
        if (len <= 0)
                return;
                return;
        DBGPRINTF("READV(%08x,%d,#%4d)\n", a, inc, len);
        DBGPRINTF("READV(%08x,%d,#%4d)\n", a, inc, len);
 
 
        ptr = encode_address(a);
        ptr = encode_address(a);
        try {
        try {
            while(cmdrd < len) {
            while(cmdrd < len) {
                // ptr = m_buf;
                // ptr = m_buf;
                do {
                do {
                        int     nrd = len-cmdrd;
                        int     nrd = len-cmdrd;
                        if (nrd > READBLOCK)
                        if (nrd > READBLOCK)
                                nrd = READBLOCK;
                                nrd = READBLOCK;
                        if (cmdrd-nread + nrd>READAHEAD+READBLOCK)
                        if (cmdrd-nread + nrd>READAHEAD+READBLOCK)
                                nrd = READAHEAD+READBLOCK-(cmdrd-nread);
                                nrd = READAHEAD+READBLOCK-(cmdrd-nread);
                        ptr = readcmd(inc, nrd, ptr);
                        ptr = readcmd(inc, nrd, ptr);
                        cmdrd += nrd;
                        cmdrd += nrd;
                } while((cmdrd-nread < READAHEAD+READBLOCK)&&(cmdrd< len));
                } while((cmdrd-nread < READAHEAD+READBLOCK)&&(cmdrd< len));
 
 
                *ptr++ = '\n'; *ptr = '\0';
                *ptr++ = '\n'; *ptr = '\0';
                m_dev->write(m_buf, (ptr-m_buf));
                m_dev->write(m_buf, (ptr-m_buf));
 
 
 
                // DBGPRINTF("Reading %d words\n", (cmdrd-nread));
                while(nread<(cmdrd-READAHEAD)) {
                while(nread<(cmdrd-READAHEAD)) {
                        buf[nread++] = readword();
                        buf[nread++] = readword();
                } ptr = m_buf;
                } ptr = m_buf;
            } while(nread<len) {
            } // DBGPRINTF("Reading %d words, to end the read\n", len-nread);
 
            while(nread<len) {
                buf[nread++] = readword();
                buf[nread++] = readword();
            }
            }
        } catch(BUSERR b) {
        } catch(BUSERR b) {
                throw BUSERR(a+((inc)?nread:0));
                DBGPRINTF("READV::BUSERR trying to read %08x\n", a+((inc)?nread:0));
 
                throw BUSERR(a+((inc)?(nread<<2):0));
        }
        }
 
 
        if ((unsigned)m_lastaddr != (a+((inc)?(len):0))) {
        if ((unsigned)m_lastaddr != (a+((inc)?(len<<2):0))) {
                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);
                DBGPRINTF("TTYBUS::READV(a=%08x,inc=%d,len=%4x,x) ERR: (Last) %08x != %08x + %08x (Expected)\n", a, inc, len<<2, m_lastaddr, a, (inc)?(len<<2):0);
                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);
                printf("TTYBUS::READV(a=%08x,inc=%d,len=%4x,x) ERR: (Last) %08x != %08x + %08x (Expected)\n", a, inc, len<<2, m_lastaddr, a, (inc)?(len<<2):0);
                sleep(1);
                sleep(1);
                assert((int)m_lastaddr == (a+(inc)?(len):0));
                assert((int)m_lastaddr == (a+(inc)?(len<<2):0));
                exit(-3);
                exit(-3);
        }
        }
 
 
 
        DBGPRINTF("READV::COMPLETE\n");
}
}
 
 
void    TTYBUS::readi(const TTYBUS::BUSW a, const int len, TTYBUS::BUSW *buf) {
void    TTYBUS::readi(const TTYBUS::BUSW a, const int len, TTYBUS::BUSW *buf) {
        readv(a, 1, len, buf);
        readv(a, 1, len, buf);
}
}
 
 
void    TTYBUS::readz(const TTYBUS::BUSW a, const int len, TTYBUS::BUSW *buf) {
void    TTYBUS::readz(const TTYBUS::BUSW a, const int len, TTYBUS::BUSW *buf) {
        readv(a, 0, len, buf);
        readv(a, 0, len, buf);
}
}
 
 
TTYBUS::BUSW    TTYBUS::readword(void) {
TTYBUS::BUSW    TTYBUS::readword(void) {
        TTYBUS::BUSW    val = 0;
        TTYBUS::BUSW    val = 0;
        int             nr;
        int             nr;
        unsigned        sixbits;
        unsigned        sixbits;
 
 
        DBGPRINTF("READ-WORD()\n");
        DBGPRINTF("READ-WORD()\n");
 
 
        bool    found_start = false;
        bool    found_start = false;
        do {
        do {
                // Blocking read (for now)
                // Blocking read (for now)
                do {
                do {
                        nr = lclreadcode(&m_buf[0], 1);
                        nr = lclreadcode(&m_buf[0], 1);
                } while (nr < 1);
                } while (nr < 1);
 
                DBGPRINTF("READWORD: -- lclreadcode, nr = %d, m_buf[0] = %c\n", nr, m_buf[0]);
 
 
                sixbits = chardec(m_buf[0]);
                sixbits = chardec(m_buf[0]);
 
 
                if (sixbits&(~0x03f)) {
                if (sixbits&(~0x03f)) {
                        // Ignore new lines, unprintables, and characters
                        // Ignore new lines, unprintables, and characters
                        // not a part of our code
                        // not a part of our code
                        ;
                        ;
                } else if (sixbits < 6) {
                } else if (sixbits < 6) {
                        switch(sixbits) {
                        switch(sixbits) {
                        case 0:  break; // Idle -- ignore
                        case 0:  break; // Idle -- ignore
                        case 1: break; // Idle, but the bus is busy
                        case 1: break; // Idle, but the bus is busy
                        case 2: break; // Write acknowledgement, ignore it here
                        case 2: break; // Write acknowledgement, ignore it here
                        case 3:
                        case 3:
                                m_bus_err = true;
                                m_bus_err = true;
 
                                DBGPRINTF("READWORD::BUSRESET (unknown addr)\n");
                                throw BUSERR(0);
                                throw BUSERR(0);
                                break;
                                break;
                        case 4:
                        case 4:
                                m_interrupt_flag = true;
                                m_interrupt_flag = true;
                                break;
                                break;
                        case 5:
                        case 5:
 
                                DBGPRINTF("READWORD::BUSERR (unknown addr)\n");
                                m_bus_err = true;
                                m_bus_err = true;
                                throw BUSERR(0);
                                throw BUSERR(0);
                                break;
                                break;
                        }
                        }
                } else if (0x08 == (sixbits & 0x3c)) { // Set 32-bit address
                } else if (0x08 == (sixbits & 0x3c)) { // Set 32-bit address
                        do {
                        do {
                                nr += lclreadcode(&m_buf[nr], 6-nr);
                                nr += lclreadcode(&m_buf[nr], 6-nr);
                        } while (nr < 6);
                        } while (nr < 6);
 
 
                        val = chardec(m_buf[0]) & 0x03;
                        val = chardec(m_buf[0]) & 0x03;
                        val = (val<<6) | (chardec(m_buf[1]) & 0x03f);
                        val = (val<<6) | (chardec(m_buf[1]) & 0x03f);
                        val = (val<<6) | (chardec(m_buf[2]) & 0x03f);
                        val = (val<<6) | (chardec(m_buf[2]) & 0x03f);
                        val = (val<<6) | (chardec(m_buf[3]) & 0x03f);
                        val = (val<<6) | (chardec(m_buf[3]) & 0x03f);
                        val = (val<<6) | (chardec(m_buf[4]) & 0x03f);
                        val = (val<<6) | (chardec(m_buf[4]) & 0x03f);
                        val = (val<<6) | (chardec(m_buf[5]) & 0x03f);
                        val = (val<<6) | (chardec(m_buf[5]) & 0x03f);
 
 
                        m_addr_set = true;
                        m_addr_set = true;
                        m_lastaddr = val;
                        m_lastaddr = val<<2;
 
 
                        DBGPRINTF("RCVD ADDR: 0x%08x\n", val);
                        DBGPRINTF("RCVD ADDR: 0x%08x\n", val<<2);
                } else if (0x0c == (sixbits & 0x03c)) { // Set 32-bit address,compressed
                } else if (0x0c == (sixbits & 0x03c)) { // Set 32-bit address,compressed
                        int nw = (sixbits & 0x03) + 2;
                        int nw = (sixbits & 0x03) + 2;
                        do {
                        do {
                                nr += lclreadcode(&m_buf[nr], nw-nr);
                                nr += lclreadcode(&m_buf[nr], nw-nr);
                        } while (nr < nw);
                        } while (nr < nw);
 
 
                        if (nw == 2) {
                        if (nw == 2) {
                                val = chardec(m_buf[1]);
                                val = chardec(m_buf[1]);
                        } else if (nw == 3) {
                        } else if (nw == 3) {
                                val = chardec(m_buf[1]);
                                val = chardec(m_buf[1]);
                                val = (val<<6) | (chardec(m_buf[2]) & 0x03f);
                                val = (val<<6) | (chardec(m_buf[2]) & 0x03f);
                        } else if (nw == 4) {
                        } else if (nw == 4) {
                                val = chardec(m_buf[1]);
                                val = chardec(m_buf[1]);
                                val = (val<<6) | (chardec(m_buf[2]) & 0x03f);
                                val = (val<<6) | (chardec(m_buf[2]) & 0x03f);
                                val = (val<<6) | (chardec(m_buf[3]) & 0x03f);
                                val = (val<<6) | (chardec(m_buf[3]) & 0x03f);
                        } else { // if (nw == 5)
                        } else { // if (nw == 5)
                                val = chardec(m_buf[1]);
                                val = chardec(m_buf[1]);
                                val = (val<<6) | (chardec(m_buf[2]) & 0x03f);
                                val = (val<<6) | (chardec(m_buf[2]) & 0x03f);
                                val = (val<<6) | (chardec(m_buf[3]) & 0x03f);
                                val = (val<<6) | (chardec(m_buf[3]) & 0x03f);
                                val = (val<<6) | (chardec(m_buf[4]) & 0x03f);
                                val = (val<<6) | (chardec(m_buf[4]) & 0x03f);
                        }
                        }
 
 
                        m_addr_set = true;
                        m_addr_set = true;
                        m_lastaddr = val;
                        m_lastaddr = val<<2;
                        DBGPRINTF("RCVD ADDR: 0x%08x (%d bytes)\n", val, nw+1);
                        DBGPRINTF("RCVD ADDR: 0x%08x (%d bytes)\n", val<<2, nw+1);
                } else
                } else
                        found_start = true;
                        found_start = true;
        } while(!found_start);
        } while(!found_start);
 
 
        int     rdaddr;
        int     rdaddr;
 
 
        DBGPRINTF("READ-WORD() -- sixbits = %02x\n", sixbits);
        DBGPRINTF("READ-WORD() -- sixbits = %02x\n", sixbits);
        if (0x06 == (sixbits & 0x03e)) { // Tbl read, last value
        if (0x06 == (sixbits & 0x03e)) { // Tbl read, last value
                rdaddr = (m_rdaddr-1)&0x03ff;
                rdaddr = (m_rdaddr-1)&0x03ff;
                val = m_readtbl[rdaddr];
                val = m_readtbl[rdaddr];
                m_lastaddr += (sixbits&1);
                m_lastaddr += (sixbits&1)?4:0;
                DBGPRINTF("READ-WORD() -- repeat last value, %08x\n", val);
                DBGPRINTF("READ-WORD() -- repeat last value, %08x, A= %08x\n", val, m_lastaddr);
        } else if (0x10 == (sixbits & 0x030)) { // Tbl read, up to 521 into past
        } else if (0x10 == (sixbits & 0x030)) { // Tbl read, up to 521 into past
                int     idx;
                int     idx;
                do {
                do {
                        nr += lclreadcode(&m_buf[nr], 2-nr);
                        nr += lclreadcode(&m_buf[nr], 2-nr);
                } while (nr < 2);
                } while (nr < 2);
 
 
                idx = (chardec(m_buf[0])>>1) & 0x07;
                idx = (chardec(m_buf[0])>>1) & 0x07;
                idx = ((idx<<6) | (chardec(m_buf[1]) & 0x03f)) + 2 + 8;
                idx = ((idx<<6) | (chardec(m_buf[1]) & 0x03f)) + 2 + 8;
                rdaddr = (m_rdaddr-idx)&0x03ff;
                rdaddr = (m_rdaddr-idx)&0x03ff;
                val = m_readtbl[rdaddr];
                val = m_readtbl[rdaddr];
                m_lastaddr += (sixbits&1);
                m_lastaddr += (sixbits&1)?4:0;
                DBGPRINTF("READ-WORD() -- long table value[%3d], %08x\n", idx, val);
                DBGPRINTF("READ-WORD() -- long table value[%3d], %08x, A=%08x\n", idx, val, m_lastaddr);
        } else if (0x20 == (sixbits & 0x030)) { // Tbl read, 2-9 into past
        } else if (0x20 == (sixbits & 0x030)) { // Tbl read, 2-9 into past
                rdaddr = (m_rdaddr - (((sixbits>>1)&0x07)+2)) & 0x03ff;
                int     idx;
 
                idx = (((sixbits>>1)&0x07)+2);
 
                rdaddr = (m_rdaddr - idx) & 0x03ff;
                val = m_readtbl[rdaddr];
                val = m_readtbl[rdaddr];
                m_lastaddr += (sixbits&1);
                m_lastaddr += (sixbits&1)?4:0;
                DBGPRINTF("READ-WORD() -- short table value[%3d], %08x\n", rdaddr, val);
                DBGPRINTF("READ-WORD() -- short table value[%3d], %08x, A=%08x\n", idx, val, m_lastaddr);
        } else if (0x38 == (sixbits & 0x038)) { // Raw read
        } else if (0x38 == (sixbits & 0x038)) { // Raw read
                DBGPRINTF("READ-WORD() -- RAW-READ, nr = %d\n", nr);
                // DBGPRINTF("READ-WORD() -- RAW-READ, nr = %d\n", nr);
                do {
                do {
                        nr += lclreadcode(&m_buf[nr], 6-nr);
                        nr += lclreadcode(&m_buf[nr], 6-nr);
                } while (nr < 6);
                } while (nr < 6);
 
 
                val = (chardec(m_buf[0])>>1) & 0x03;
                val = (chardec(m_buf[0])>>1) & 0x03;
                val = (val<<6) | (chardec(m_buf[1]) & 0x03f);
                val = (val<<6) | (chardec(m_buf[1]) & 0x03f);
                val = (val<<6) | (chardec(m_buf[2]) & 0x03f);
                val = (val<<6) | (chardec(m_buf[2]) & 0x03f);
                val = (val<<6) | (chardec(m_buf[3]) & 0x03f);
                val = (val<<6) | (chardec(m_buf[3]) & 0x03f);
                val = (val<<6) | (chardec(m_buf[4]) & 0x03f);
                val = (val<<6) | (chardec(m_buf[4]) & 0x03f);
                val = (val<<6) | (chardec(m_buf[5]) & 0x03f);
                val = (val<<6) | (chardec(m_buf[5]) & 0x03f);
 
 
                m_readtbl[m_rdaddr++] = val; m_rdaddr &= 0x03ff;
                m_readtbl[m_rdaddr++] = val; m_rdaddr &= 0x03ff;
                m_lastaddr += (sixbits&1);
                m_lastaddr += (sixbits&1)?4:0;
                DBGPRINTF("READ-WORD() -- RAW-READ %02x:%02x:%02x:%02x:%02x:%02x -- %08x\n",
                DBGPRINTF("READ-WORD() -- RAW-READ %02x:%02x:%02x:%02x:%02x:%02x -- %08x, A=%08x\n",
                        m_buf[0], m_buf[1], m_buf[2], m_buf[3],
                        m_buf[0], m_buf[1], m_buf[2], m_buf[3],
                        m_buf[4], m_buf[5], val);
                        m_buf[4], m_buf[5], val, m_lastaddr);
        } else
        } else
                DBGPRINTF("READ-WORD() -- Unknown character, %02x\n", sixbits);
                DBGPRINTF("READ-WORD() -- Unknown character, %02x\n", sixbits);
 
 
        return val;
        return val;
}
}
 
 
 
void    TTYBUS::readidle(void) {
 
        TTYBUS::BUSW    val = 0;
 
        int             nr;
 
        unsigned        sixbits;
 
        bool            found_start = false;
 
 
 
        DBGPRINTF("READ-IDLE()\n");
 
 
 
        while((!found_start)&&(m_dev->available())
 
                        &&((nr=lclreadcode(&m_buf[0], 1))>0)) {
 
                nr = lclreadcode(&m_buf[0], 1);
 
                sixbits = chardec(m_buf[0]);
 
 
 
                if (sixbits&(~0x03f)) {
 
                        // Ignore new lines, unprintables, and characters
 
                        // not a part of our code
 
                        ;
 
                } else if (sixbits < 6) {
 
                        switch(sixbits) {
 
                        case 0:  break; // Idle -- ignore
 
                        case 1: break; // Idle, but the bus is busy
 
                        case 2:
 
                                // Write acknowledgement, ignore it here
 
                                // This is one of the big reasons why we are
 
                                // doing this.
 
                                break;
 
                        case 3:
 
                                m_bus_err = true;
 
                                DBGPRINTF("READ-IDLE() - BUSERR\n");
 
                                throw BUSERR(0);
 
                                break;
 
                        case 4:
 
                                m_interrupt_flag = true;
 
                                break;
 
                        case 5:
 
                                m_bus_err = true;
 
                                DBGPRINTF("READ-IDLE() - BUS RESET\n");
 
                                throw BUSERR(0);
 
                                break;
 
                        }
 
                } else if (0x08 == (sixbits & 0x3c)) { // Set 32-bit address
 
                        do {
 
                                nr += lclreadcode(&m_buf[nr], 6-nr);
 
                        } while (nr < 6);
 
 
 
                        val = chardec(m_buf[0]) & 0x03;
 
                        val = (val<<6) | (chardec(m_buf[1]) & 0x03f);
 
                        val = (val<<6) | (chardec(m_buf[2]) & 0x03f);
 
                        val = (val<<6) | (chardec(m_buf[3]) & 0x03f);
 
                        val = (val<<6) | (chardec(m_buf[4]) & 0x03f);
 
                        val = (val<<6) | (chardec(m_buf[5]) & 0x03f);
 
 
 
                        /* Ignore the address, as we are in readidle();
 
                        m_addr_set = true;
 
                        m_lastaddr = val;
 
                        */
 
 
 
                        DBGPRINTF("RCVD IDLE-ADDR: 0x%08x\n", val);
 
                } else if (0x0c == (sixbits & 0x03c)) { // Set 32-bit address,compressed
 
                        int nw = (sixbits & 0x03) + 2;
 
                        do {
 
                                nr += lclreadcode(&m_buf[nr], nw-nr);
 
                        } while (nr < nw);
 
 
 
                        if (nw == 2) {
 
                                val = chardec(m_buf[1]);
 
                        } else if (nw == 3) {
 
                                val = chardec(m_buf[1]);
 
                                val = (val<<6) | (chardec(m_buf[2]) & 0x03f);
 
                        } else if (nw == 4) {
 
                                val = chardec(m_buf[1]);
 
                                val = (val<<6) | (chardec(m_buf[2]) & 0x03f);
 
                                val = (val<<6) | (chardec(m_buf[3]) & 0x03f);
 
                        } else { // if (nw == 5)
 
                                val = chardec(m_buf[1]);
 
                                val = (val<<6) | (chardec(m_buf[2]) & 0x03f);
 
                                val = (val<<6) | (chardec(m_buf[3]) & 0x03f);
 
                                val = (val<<6) | (chardec(m_buf[4]) & 0x03f);
 
                        }
 
 
 
                        /* Ignore address, we are in readidle();
 
                        m_addr_set = true;
 
                        m_lastaddr = val;
 
                        */
 
                        DBGPRINTF("RCVD IDLE-ADDR: 0x%08x (%d bytes)\n", val, nw+1);
 
                } else
 
                        found_start = true;
 
        }
 
 
 
        if (found_start) {
 
                // We're in readidle().  We don't expect to find any data.
 
                // But ... we did.  So, just read it off and ignore it.
 
                int     rdaddr;
 
 
 
                DBGPRINTF("READ-IDLE()  PANIC! -- sixbits = %02x\n", sixbits);
 
                if (0x06 == (sixbits & 0x03e)) { // Tbl read, last value
 
                        rdaddr = (m_rdaddr-1)&0x03ff;
 
                        val = m_readtbl[rdaddr];
 
                        m_lastaddr += (sixbits&1)?4:0;
 
                        DBGPRINTF("READ-IDLE() -- repeat last value, %08x\n", val);
 
                } else if (0x10 == (sixbits & 0x030)) { // Tbl read, up to 521 into past
 
                        int     idx;
 
                        do {
 
                                nr += lclreadcode(&m_buf[nr], 2-nr);
 
                        } while (nr < 2);
 
 
 
                        idx = (chardec(m_buf[0])>>1) & 0x07;
 
                        idx = ((idx<<6) | (chardec(m_buf[1]) & 0x03f)) + 2 + 8;
 
                        rdaddr = (m_rdaddr-idx)&0x03ff;
 
                        val = m_readtbl[rdaddr];
 
                        m_lastaddr += (sixbits&1)?4:0;
 
                        DBGPRINTF("READ-IDLE() -- long table value[%3d], %08x\n", idx, val);
 
                } else if (0x20 == (sixbits & 0x030)) { // Tbl read, 2-9 into past
 
                        rdaddr = (m_rdaddr - (((sixbits>>1)&0x07)+2)) & 0x03ff;
 
                        val = m_readtbl[rdaddr];
 
                        m_lastaddr += (sixbits&1)?4:0;
 
                        DBGPRINTF("READ-IDLE() -- short table value[%3d], %08x\n", rdaddr, val);
 
                } else if (0x38 == (sixbits & 0x038)) { // Raw read
 
                        do {
 
                                nr += lclreadcode(&m_buf[nr], 6-nr);
 
                        } while (nr < 6);
 
 
 
                        val = (chardec(m_buf[0])>>1) & 0x03;
 
                        val = (val<<6) | (chardec(m_buf[1]) & 0x03f);
 
                        val = (val<<6) | (chardec(m_buf[2]) & 0x03f);
 
                        val = (val<<6) | (chardec(m_buf[3]) & 0x03f);
 
                        val = (val<<6) | (chardec(m_buf[4]) & 0x03f);
 
                        val = (val<<6) | (chardec(m_buf[5]) & 0x03f);
 
 
 
                        m_readtbl[m_rdaddr++] = val; m_rdaddr &= 0x03ff;
 
                        m_lastaddr += (sixbits&1)?4:0;
 
                        DBGPRINTF("READ-IDLE() -- RAW-READ %02x:%02x:%02x:%02x:%02x:%02x -- %08x\n",
 
                                m_buf[0], m_buf[1], m_buf[2], m_buf[3],
 
                                m_buf[4], m_buf[5], val);
 
                } else
 
                        DBGPRINTF("READ-IDLE() -- Unknown character, %02x\n", sixbits);
 
 
 
        }
 
}
 
 
void    TTYBUS::usleep(unsigned ms) {
void    TTYBUS::usleep(unsigned ms) {
        if (m_dev->poll(ms)) {
        if (m_dev->poll(ms)) {
                int     nr;
                int     nr;
                nr = m_dev->read(m_buf, 16);
                nr = m_dev->read(m_buf, 16);
                if (nr == 0) {
                if (nr == 0) {
                        // Connection closed, let it drop
                        // Connection closed, let it drop
                        DBGPRINTF("Connection closed!!\n");
                        DBGPRINTF("Connection closed!!\n");
                        m_dev->close();
                        m_dev->close();
                        exit(-1);
                        exit(-1);
                } for(int i=0; i<nr; i++) {
                } for(int i=0; i<nr; i++) {
                        if (m_buf[i] == TTYC_INT) {
                        if (m_buf[i] == TTYC_INT) {
                                m_interrupt_flag = true;
                                m_interrupt_flag = true;
                                DBGPRINTF("!!!!!!!!!!!!!!!!! ----- INTERRUPT!\n");
                                DBGPRINTF("!!!!!!!!!!!!!!!!! ----- INTERRUPT!\n");
                        } else if (m_buf[i] == TTYC_IDLE) {
                        } else if (m_buf[i] == TTYC_IDLE) {
                                DBGPRINTF("Interface is now idle\n");
                                DBGPRINTF("Interface is now idle\n");
                        } else if (m_buf[i] == TTYC_WRITE) {
                        } else if (m_buf[i] == TTYC_WRITE) {
                        } else if (m_buf[i] == TTYC_RESET) {
                        } else if (m_buf[i] == TTYC_RESET) {
                                DBGPRINTF("Bus was RESET!\n");
                                DBGPRINTF("Bus was RESET!\n");
                        } else if (m_buf[i] == TTYC_ERR) {
                        } else if (m_buf[i] == TTYC_ERR) {
                                DBGPRINTF("Bus error\n");
                                DBGPRINTF("Bus error\n");
                        } else if (m_buf[i] == TTYC_BUSY) {
                        } else if (m_buf[i] == TTYC_BUSY) {
                                DBGPRINTF("Interface is ... busy ??\n");
                                DBGPRINTF("Interface is ... busy ??\n");
                        }
                        }
                        // else if (m_buf[nr] == 'Q')
                        // else if (m_buf[nr] == 'Q')
                        // else if (m_buf[nr] == 'W')
                        // else if (m_buf[nr] == 'W')
                        // else if (m_buf[nr] == '\n')
                        // else if (m_buf[nr] == '\n')
                }
                }
        }
        }
}
}
 
 
void    TTYBUS::wait(void) {
void    TTYBUS::wait(void) {
        if (m_interrupt_flag)
        if (m_interrupt_flag)
                DBGPRINTF("INTERRUPTED PRIOR TO WAIT()\n");
                DBGPRINTF("INTERRUPTED PRIOR TO WAIT()\n");
        do {
        do {
                usleep(200);
                usleep(200);
        } while(!m_interrupt_flag);
        } while(!m_interrupt_flag);
}
}
 
 
// TTYBUS:  3503421 ~= 3.3 MB, stopwatch = 1:18.5 seconds, vs 53.8 secs
// TTYBUS:  3503421 ~= 3.3 MB, stopwatch = 1:18.5 seconds, vs 53.8 secs
//      If you issue two 512 word reads at once, time drops to 41.6 secs.
//      If you issue two 512 word reads at once, time drops to 41.6 secs.
// PORTBUS: 6408320 ~= 6.1 MB, ... 26% improvement, 53 seconds real time
// PORTBUS: 6408320 ~= 6.1 MB, ... 26% improvement, 53 seconds real time
 
 
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.