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

Subversion Repositories s6soc

[/] [s6soc/] [trunk/] [sw/] [host/] [ttybus.cpp] - Blame information for rev 45

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 11 dgisselq
////////////////////////////////////////////////////////////////////////////////
2
//
3 45 dgisselq
// Filename:    ttybus.cpp
4 11 dgisselq
//
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 45 dgisselq
//      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 from an attached host computer.
14 11 dgisselq
//
15
// Creator:     Dan Gisselquist, Ph.D.
16
//              Gisselquist Technology, LLC
17
//
18
////////////////////////////////////////////////////////////////////////////////
19
//
20 45 dgisselq
// Copyright (C) 2015-2017, Gisselquist Technology, LLC
21 11 dgisselq
//
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 45 dgisselq
// with this program.  (It's in the $(ROOT)/doc directory.  Run make with no
34 11 dgisselq
// 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
#include <sys/socket.h>
45
#include <sys/types.h>
46
#include <sys/stat.h>
47
#include <fcntl.h>
48
#include <termios.h>
49
#include <netinet/in.h>
50
#include <netdb.h>
51
#include <stdio.h>
52
#include <string.h>
53
#include <stdlib.h>
54
#include <unistd.h>
55
#include <errno.h>
56
#include <arpa/inet.h> 
57
#include <assert.h> 
58
#include <strings.h> 
59
#include <poll.h> 
60
#include <ctype.h> 
61
 
62
#include "ttybus.h"
63
 
64
#define TTYC_IDLE       '0'
65
#define TTYC_BUSY       '1'
66
#define TTYC_WRITE      '2'
67
#define TTYC_RESET      '3'
68
#define TTYC_INT        '4'
69
#define TTYC_ERR        '5'
70
 
71 45 dgisselq
const   unsigned TTYBUS::MAXRDLEN = 64;
72 11 dgisselq
const   unsigned TTYBUS::MAXWRLEN = 32;
73
 
74
// #define      DBGPRINTF       printf
75
// #define      DBGPRINTF       filedump
76 45 dgisselq
#ifndef DBGPRINTF
77
#define DBGPRINTF       null
78
#else
79
#warning "TTYBUS DEBUG IS TURNED ON"
80
#endif
81 11 dgisselq
 
82
void    null(...) {}
83 45 dgisselq
 
84
#include <stdarg.h> // replaces the (defunct) varargs.h include file
85 11 dgisselq
void    filedump(const char *fmt, ...) {
86
        static  FILE *dbgfp = NULL;
87
        va_list args;
88
 
89
        if (!dbgfp)
90
                dbgfp = fopen("debug.txt", "w");
91
        va_start(args, fmt);
92
        vfprintf(dbgfp, fmt, args);
93
        va_end(args);
94
        fflush(dbgfp);
95 45 dgisselq
 
96
        // If you want the debug output to go to stderr as well, you can
97
        // uncomment the next couple of lines
98
        // va_start(args, fmt);
99
        // vfprintf(stderr, fmt, args);
100
        // va_end(args);
101 11 dgisselq
}
102
 
103
char    TTYBUS::charenc(const int sixbitval) const {
104
        if (sixbitval < 10)
105
                return '0' + sixbitval;
106
        else if (sixbitval < 10+26)
107
                return 'A' - 10 + sixbitval;
108
        else if (sixbitval < 10+26+26)
109
                return 'a' - 10 - 26 + sixbitval;
110
        else if (sixbitval == 0x3e)
111
                return '@';
112
        else if (sixbitval == 0x3f)
113
                return '%';
114
 
115
        fprintf(stderr, "INTERNAL ERR: SIXBITVAL isn\'t!!!! sixbitval = %08x\n", sixbitval);
116
        assert((sixbitval & (~0x03f))==0);
117
        return 0;
118
}
119
 
120
unsigned        TTYBUS::chardec(const char b) const {
121
        if ((b >= '0')&&(b <= '9'))
122
                return b-'0';
123
        else if ((b >= 'A')&&(b <= 'Z'))
124
                return b-'A'+10;
125
        else if ((b >= 'a')&&(b <= 'z'))
126
                return b-'a'+36;
127
        else if (b == '@')
128
                return 0x03e;
129
        else if (b == '%')
130
                return 0x03f;
131
        else
132
                return 0x0100; // ERR -- invalid code
133
}
134
 
135
int     TTYBUS::lclreadcode(char *buf, int len) {
136
        char    *sp, *dp;
137
        int     nr, ret;
138
 
139
        nr = m_dev->read(buf, len);
140
        m_total_nread += nr;
141
        ret = nr; sp = buf; dp = buf;
142
        for(int i=0; i<nr; i++) {
143
                if (chardec(*sp)&(~0x3f)) {
144
                        ret--;  // Skip this value, not a valid codeword
145
                        sp++;
146
                } else {
147
                        *sp++ = *dp++;
148
                }
149
        } return ret;
150
}
151
 
152
void    TTYBUS::bufalloc(int len) {
153
        if ((m_buf)&&(m_buflen >= len))
154
                return;
155
        if (m_buf)
156
                delete[] m_buf;
157
        m_buflen = (len&(-0x3f))+0x40;
158
        m_buf = new char[m_buflen];
159
}
160
 
161
void    TTYBUS::encode(const int hb, const BUSW val, char *buf) const {
162
        buf[0] = charenc( (hb<<2)|((val>>30)&0x03) );
163
        buf[1] = charenc( (val>>24)&0x3f);
164
        buf[2] = charenc( (val>>18)&0x3f);
165
        buf[3] = charenc( (val>>12)&0x3f);
166
        buf[4] = charenc( (val>> 6)&0x3f);
167
        buf[5] = charenc( (val    )&0x3f);
168
}
169
 
170
unsigned        TTYBUS::decodestr(const char *buf) const {
171
        unsigned        r;
172
 
173
        r = chardec(buf[0]) & 0x03;
174
        r = (r<<6) | (chardec(buf[1]) & 0x03f);
175
        r = (r<<6) | (chardec(buf[2]) & 0x03f);
176
        r = (r<<6) | (chardec(buf[3]) & 0x03f);
177
        r = (r<<6) | (chardec(buf[4]) & 0x03f);
178
        r = (r<<6) | (chardec(buf[5]) & 0x03f);
179
 
180
        return r;
181
}
182
 
183
int     TTYBUS::decodehex(const char hx) const {
184
        if ((hx >= '0')&&(hx <= '9'))
185
                return hx-'0';
186
        else if ((hx >= 'A')&&(hx <= 'Z'))
187
                return hx-'A'+10;
188
        else if ((hx >= 'a')&&(hx <= 'z'))
189
                return hx-'a'+10;
190
        else
191
                return 0;
192
}
193
 
194
void    TTYBUS::writeio(const BUSW a, const BUSW v) {
195
 
196
        writev(a, 0, 1, &v);
197
        m_lastaddr = a; m_addr_set = true;
198
}
199
 
200
void    TTYBUS::writev(const BUSW a, const int p, const int len, const BUSW *buf) {
201
        char    *ptr;
202 45 dgisselq
        int     nw = 0;
203 11 dgisselq
 
204 45 dgisselq
        // We'll never be called with more than MAXWRLEN words to write at once.
205
        // This is a configurable option length, set at the top of this file.
206
        // (currently set at 32, but subject to change ...)  This is important,
207
        // as the return channel *must* be capable of holding at least this many
208
        // acknowledgments in its buffer.
209
        //
210
        // assert(len <= MAXWRLEN);
211
 
212 11 dgisselq
        // Allocate a buffer of six bytes per word, one for addr, plus
213
        // six more
214
        bufalloc((len+2)*6);
215
 
216
        DBGPRINTF("WRITEV(%08x,%d,#%d,0x%08x ...)\n", a, p, len, buf[0]);
217
        // Encode the address
218
        ptr = encode_address(a);
219
        m_lastaddr = a; m_addr_set = true;
220
 
221 45 dgisselq
        while(nw < len) {
222
                int     ln = len-nw;
223
                if ((unsigned)ln > MAXWRLEN)
224
                        ln = MAXWRLEN;
225 11 dgisselq
 
226 45 dgisselq
                DBGPRINTF("WRITEV-SUB(%08x%s,#%d,&buf[%d])\n", a+nw, (p)?"++":"", ln, nw);
227
                for(int i=0; i<ln; i++) {
228
                        BUSW    val = buf[nw+i];
229
 
230
                        int     caddr = 0;
231
                        // Let's try compression
232
                        for(int i=1; i<256; i++) {
233
                                unsigned        tstaddr;
234
                                tstaddr = (m_wraddr - i) & 0x0ff;
235
                                if ((!m_wrloaded)&&(tstaddr > (unsigned)m_wraddr))
236
                                        break;
237
                                if (m_writetbl[tstaddr] == val) {
238
                                        caddr = ( m_wraddr- tstaddr ) & 0x0ff;
239
                                        break;
240
                                }
241 11 dgisselq
                        }
242
 
243 45 dgisselq
                /*
244 11 dgisselq
                if (caddr != 0)
245
                        DBGPRINTF("WR[%08x] = %08x (= TBL[%4x] <= %4x)\n", m_lastaddr, val, caddr, m_wraddr);
246
                else
247
                        DBGPRINTF("WR[%08x] = %08x\n", m_lastaddr, val);
248 45 dgisselq
                */
249 11 dgisselq
 
250 45 dgisselq
                        if (caddr != 0) {
251
                                *ptr++ = charenc( (((caddr>>6)&0x03)<<1) + (p?1:0) + 0x010);
252
                                *ptr++ = charenc(    caddr    &0x3f    );
253 11 dgisselq
 
254 45 dgisselq
                        } else {
255
                                // For testing, let's start just doing this the hard way
256
                                *ptr++ = charenc( (((val>>30)&0x03)<<1) + (p?1:0) + 0x018);
257
                                *ptr++ = charenc( (val>>24)&0x3f);
258
                                *ptr++ = charenc( (val>>18)&0x3f);
259
                                *ptr++ = charenc( (val>>12)&0x3f);
260
                                *ptr++ = charenc( (val>> 6)&0x3f);
261
                                *ptr++ = charenc( (val    )&0x3f);
262 11 dgisselq
 
263 45 dgisselq
                                m_writetbl[m_wraddr++] = val;
264
                                m_wraddr &= 0x0ff;
265
                                if (m_wraddr == 0) {
266
                                        m_wrloaded = true;
267
                                }
268 11 dgisselq
                        }
269 45 dgisselq
 
270
                        if (p == 1) m_lastaddr+=4;
271 11 dgisselq
                }
272 45 dgisselq
                // *ptr++ = charenc(0x2e);
273
                if (ln == len-nw)
274
                        *ptr++ = '\n';
275
                *ptr = '\0';
276
                m_dev->write(m_buf, ptr-m_buf);
277
                DBGPRINTF(">> %s\n", m_buf);
278 11 dgisselq
 
279 45 dgisselq
                readidle();
280
 
281
                nw += ln;
282
                ptr = m_buf;
283 11 dgisselq
        }
284 45 dgisselq
        DBGPRINTF("WR: LAST ADDRESS LEFT AT %08x\n", m_lastaddr);
285 11 dgisselq
 
286 45 dgisselq
        // Need to clear the incoming queue ... if there's anything there.
287
        // We could do a ...
288
        //      readacks(len);
289
        // to clear a known number of acks (up to half the length of our buffer
290
        // which we can let just sit for speed ...), or we could do a ...
291
        //      readtilidle(void);
292
        // Then, upon startup we could also start with a readtilidle(); command.
293
        // This would help to clear out the problems between programs, where
294
        // one program doesn't finish reading, and the next gets a confusing
295
        // message.
296
        readidle();
297 11 dgisselq
}
298
 
299
void    TTYBUS::writez(const BUSW a, const int len, const BUSW *buf) {
300 45 dgisselq
        writev(a, 0, len, buf);
301 11 dgisselq
}
302
 
303
void    TTYBUS::writei(const BUSW a, const int len, const BUSW *buf) {
304 45 dgisselq
        writev(a, 1, len, buf);
305 11 dgisselq
}
306
 
307
TTYBUS::BUSW    TTYBUS::readio(const TTYBUS::BUSW a) {
308
        BUSW    v;
309
 
310
        // I/O reads are now the same as vector reads, but with a vector length
311
        // of one.
312
        DBGPRINTF("READIO(0x%08x)\n", a);
313
        try {
314
                readv(a, 0, 1, &v);
315
        } catch(BUSERR b) {
316 45 dgisselq
                DBGPRINTF("BUSERR trying to read %08x\n", a);
317 11 dgisselq
                throw BUSERR(a);
318
        }
319
 
320
        if (m_lastaddr != a) {
321
                DBGPRINTF("LAST-ADDR MIS-MATCH: (RCVD) %08x != %08x (XPECTED)\n", m_lastaddr, a);
322
                m_addr_set = false;
323
 
324
                exit(-3);
325
        }
326
 
327
        return v;
328
}
329
 
330
char    *TTYBUS::encode_address(const TTYBUS::BUSW a) {
331 45 dgisselq
        TTYBUS::BUSW    addr = a>>2;
332 11 dgisselq
        char    *ptr = m_buf;
333
 
334 45 dgisselq
        // Double check that we are aligned
335
        if ((a&3)!=0) {
336
                throw BUSERR(a);
337
        }
338 11 dgisselq
 
339
        if ((m_addr_set)&&(a == m_lastaddr))
340
                return ptr;
341
 
342
        if (m_addr_set) {
343
                // Encode a difference address
344 45 dgisselq
                int     diffaddr = (a - m_lastaddr)>>2;
345 11 dgisselq
                ptr = m_buf;
346
                if ((diffaddr >= -32)&&(diffaddr < 32)) {
347
                        *ptr++ = charenc(0x09);
348
                        *ptr++ = charenc(diffaddr & 0x03f);
349
                } else if ((diffaddr >= -2048)&&(diffaddr < 2048)) {
350
                        *ptr++ = charenc(0x0b);
351
                        *ptr++ = charenc((diffaddr>>6) & 0x03f);
352
                        *ptr++ = charenc( diffaddr     & 0x03f);
353
                } else if ((diffaddr >= -(1<<17))&&(diffaddr < (1<<17))) {
354
                        *ptr++ = charenc(0x0d);
355
                        *ptr++ = charenc((diffaddr>>12) & 0x03f);
356
                        *ptr++ = charenc((diffaddr>> 6) & 0x03f);
357
                        *ptr++ = charenc( diffaddr      & 0x03f);
358
                } else if ((diffaddr >= -(1<<23))&&(diffaddr < (1<<23))) {
359
                        *ptr++ = charenc(0x0d);
360
                        *ptr++ = charenc((diffaddr>>18) & 0x03f);
361
                        *ptr++ = charenc((diffaddr>>12) & 0x03f);
362
                        *ptr++ = charenc((diffaddr>> 6) & 0x03f);
363
                        *ptr++ = charenc( diffaddr      & 0x03f);
364
                }
365
                *ptr = '\0';
366 45 dgisselq
                DBGPRINTF("DIF-ADDR: (%ld) \'%s\' encodes last_addr(0x%08x) %c %d(0x%08x)\n",
367
                        ptr-m_buf, m_buf,
368
                        m_lastaddr, (diffaddr<0)?'-':'+',
369
                        diffaddr, diffaddr&0x0ffffffff);
370 11 dgisselq
        }
371
 
372
        {
373
                // Encode an absolute (low memory) address
374
                // Prefer absolute address encoding over differential encoding,
375
                // when both encodings encode the same address, and when both
376
                // encode the address in the same number of words
377
                if ((addr <= 0x03f)&&((ptr == m_buf)||(ptr >= &m_buf[2]))) {
378
                        ptr = m_buf;
379
                        *ptr++ = charenc(0x08);
380
                        *ptr++ = charenc(addr);
381
                } else if((addr <= 0x0fff)&&((ptr == m_buf)||(ptr >= &m_buf[3]))) {
382
                        DBGPRINTF("Setting ADDR.3 to %08x\n", addr);
383
                        ptr = m_buf;
384
                        *ptr++ = charenc(0x0a);
385
                        *ptr++ = charenc((addr>> 6) & 0x03f);
386
                        *ptr++ = charenc( addr      & 0x03f);
387
                } else if((addr <= 0x03ffff)&&((ptr == m_buf)||(ptr >= &m_buf[4]))) {
388
                        DBGPRINTF("Setting ADDR.4 to %08x\n", addr);
389
                        ptr = m_buf;
390
                        *ptr++ = charenc(0x0c);
391
                        *ptr++ = charenc((addr>>12) & 0x03f);
392
                        *ptr++ = charenc((addr>> 6) & 0x03f);
393
                        *ptr++ = charenc( addr      & 0x03f);
394
                } else if((addr <= 0x0ffffff)&&((ptr == m_buf)||(ptr >= &m_buf[5]))) {
395
                        DBGPRINTF("Setting ADDR.5 to %08x\n", addr);
396
                        ptr = m_buf;
397
                        *ptr++ = charenc(0x0e);
398
                        *ptr++ = charenc((addr>>18) & 0x03f);
399
                        *ptr++ = charenc((addr>>12) & 0x03f);
400
                        *ptr++ = charenc((addr>> 6) & 0x03f);
401
                        *ptr++ = charenc( addr      & 0x03f);
402
                } else if (ptr == m_buf) { // Send our address prior to any read
403
                        // ptr = m_buf;
404
                        encode(0, addr, ptr);
405
                        ptr+=6;
406
                }
407
        }
408
 
409
        *ptr = '\0';
410
        DBGPRINTF("ADDR-CMD: (%ld) \'%s\'\n", ptr-m_buf, m_buf);
411
        m_rdaddr = 0;
412
 
413
        return ptr;
414
}
415
 
416
char    *TTYBUS::readcmd(const int inc, const int len, char *buf) {
417
        char    *ptr = buf;
418
 
419 45 dgisselq
        DBGPRINTF("READCMD: LEN = %d: ", len);
420 11 dgisselq
        assert(len < 520);
421
        assert(len > 0);
422
 
423 45 dgisselq
        if (len <= 8) {
424 11 dgisselq
                *ptr++ = charenc(0x20 + (((len-1)&0x07)<<1) + (inc?1:0));
425 45 dgisselq
                DBGPRINTF("%c\n", ptr[-1]);
426 11 dgisselq
        } else {
427 45 dgisselq
                *ptr++ = charenc(0x30 + (((len-9)>>5)&0x0e) + (inc?1:0));
428
                *ptr++ = charenc( (len-9) & 0x03f);
429
                DBGPRINTF("%c%c\n", ptr[-2], ptr[-1]);
430 11 dgisselq
        }
431
 
432
        return ptr;
433
}
434
 
435
void    TTYBUS::readv(const TTYBUS::BUSW a, const int inc, const int len, TTYBUS::BUSW *buf) {
436 45 dgisselq
        const   int     READAHEAD = 0, READBLOCK=(MAXRDLEN/2>512)?512:MAXRDLEN/2;
437 11 dgisselq
        int     cmdrd = 0, nread = 0;
438
        // TTYBUS::BUSW addr = a;
439
        char    *ptr = m_buf;
440
 
441
        if (len <= 0)
442
                return;
443
        DBGPRINTF("READV(%08x,%d,#%4d)\n", a, inc, len);
444
 
445
        ptr = encode_address(a);
446
        try {
447
            while(cmdrd < len) {
448
                // ptr = m_buf;
449
                do {
450
                        int     nrd = len-cmdrd;
451
                        if (nrd > READBLOCK)
452
                                nrd = READBLOCK;
453
                        if (cmdrd-nread + nrd>READAHEAD+READBLOCK)
454
                                nrd = READAHEAD+READBLOCK-(cmdrd-nread);
455
                        ptr = readcmd(inc, nrd, ptr);
456
                        cmdrd += nrd;
457
                } while((cmdrd-nread < READAHEAD+READBLOCK)&&(cmdrd< len));
458
 
459
                *ptr++ = '\n'; *ptr = '\0';
460
                m_dev->write(m_buf, (ptr-m_buf));
461
 
462 45 dgisselq
                // DBGPRINTF("Reading %d words\n", (cmdrd-nread));
463 11 dgisselq
                while(nread<(cmdrd-READAHEAD)) {
464
                        buf[nread++] = readword();
465
                } ptr = m_buf;
466 45 dgisselq
            } // DBGPRINTF("Reading %d words, to end the read\n", len-nread);
467
            while(nread<len) {
468 11 dgisselq
                buf[nread++] = readword();
469
            }
470
        } catch(BUSERR b) {
471 45 dgisselq
                DBGPRINTF("READV::BUSERR trying to read %08x\n", a+((inc)?nread:0));
472
                throw BUSERR(a+((inc)?(nread<<2):0));
473 11 dgisselq
        }
474 45 dgisselq
 
475
        if ((unsigned)m_lastaddr != (a+((inc)?(len<<2):0))) {
476
                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);
477
                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);
478 11 dgisselq
                sleep(1);
479 45 dgisselq
                assert((int)m_lastaddr == (a+(inc)?(len<<2):0));
480 11 dgisselq
                exit(-3);
481
        }
482 45 dgisselq
 
483
        DBGPRINTF("READV::COMPLETE\n");
484 11 dgisselq
}
485
 
486
void    TTYBUS::readi(const TTYBUS::BUSW a, const int len, TTYBUS::BUSW *buf) {
487
        readv(a, 1, len, buf);
488
}
489
 
490
void    TTYBUS::readz(const TTYBUS::BUSW a, const int len, TTYBUS::BUSW *buf) {
491
        readv(a, 0, len, buf);
492
}
493
 
494
TTYBUS::BUSW    TTYBUS::readword(void) {
495
        TTYBUS::BUSW    val = 0;
496
        int             nr;
497
        unsigned        sixbits;
498
 
499
        DBGPRINTF("READ-WORD()\n");
500
 
501
        bool    found_start = false;
502
        do {
503
                // Blocking read (for now)
504
                do {
505
                        nr = lclreadcode(&m_buf[0], 1);
506
                } while (nr < 1);
507 45 dgisselq
                DBGPRINTF("READWORD: -- lclreadcode, nr = %d, m_buf[0] = %c\n", nr, m_buf[0]);
508 11 dgisselq
 
509
                sixbits = chardec(m_buf[0]);
510
 
511
                if (sixbits&(~0x03f)) {
512
                        // Ignore new lines, unprintables, and characters
513
                        // not a part of our code
514
                        ;
515
                } else if (sixbits < 6) {
516
                        switch(sixbits) {
517
                        case 0:  break; // Idle -- ignore
518
                        case 1: break; // Idle, but the bus is busy
519
                        case 2: break; // Write acknowledgement, ignore it here
520
                        case 3:
521
                                m_bus_err = true;
522 45 dgisselq
                                DBGPRINTF("READWORD::BUSRESET (unknown addr)\n");
523 11 dgisselq
                                throw BUSERR(0);
524
                                break;
525
                        case 4:
526
                                m_interrupt_flag = true;
527
                                break;
528
                        case 5:
529 45 dgisselq
                                DBGPRINTF("READWORD::BUSERR (unknown addr)\n");
530 11 dgisselq
                                m_bus_err = true;
531
                                throw BUSERR(0);
532
                                break;
533
                        }
534
                } else if (0x08 == (sixbits & 0x3c)) { // Set 32-bit address
535
                        do {
536
                                nr += lclreadcode(&m_buf[nr], 6-nr);
537
                        } while (nr < 6);
538
 
539
                        val = chardec(m_buf[0]) & 0x03;
540
                        val = (val<<6) | (chardec(m_buf[1]) & 0x03f);
541
                        val = (val<<6) | (chardec(m_buf[2]) & 0x03f);
542
                        val = (val<<6) | (chardec(m_buf[3]) & 0x03f);
543
                        val = (val<<6) | (chardec(m_buf[4]) & 0x03f);
544
                        val = (val<<6) | (chardec(m_buf[5]) & 0x03f);
545
 
546
                        m_addr_set = true;
547 45 dgisselq
                        m_lastaddr = val<<2;
548 11 dgisselq
 
549 45 dgisselq
                        DBGPRINTF("RCVD ADDR: 0x%08x\n", val<<2);
550 11 dgisselq
                } else if (0x0c == (sixbits & 0x03c)) { // Set 32-bit address,compressed
551
                        int nw = (sixbits & 0x03) + 2;
552
                        do {
553
                                nr += lclreadcode(&m_buf[nr], nw-nr);
554
                        } while (nr < nw);
555
 
556
                        if (nw == 2) {
557
                                val = chardec(m_buf[1]);
558
                        } else if (nw == 3) {
559
                                val = chardec(m_buf[1]);
560
                                val = (val<<6) | (chardec(m_buf[2]) & 0x03f);
561
                        } else if (nw == 4) {
562
                                val = chardec(m_buf[1]);
563
                                val = (val<<6) | (chardec(m_buf[2]) & 0x03f);
564
                                val = (val<<6) | (chardec(m_buf[3]) & 0x03f);
565
                        } else { // if (nw == 5)
566
                                val = chardec(m_buf[1]);
567
                                val = (val<<6) | (chardec(m_buf[2]) & 0x03f);
568
                                val = (val<<6) | (chardec(m_buf[3]) & 0x03f);
569
                                val = (val<<6) | (chardec(m_buf[4]) & 0x03f);
570
                        }
571
 
572
                        m_addr_set = true;
573 45 dgisselq
                        m_lastaddr = val<<2;
574
                        DBGPRINTF("RCVD ADDR: 0x%08x (%d bytes)\n", val<<2, nw+1);
575 11 dgisselq
                } else
576
                        found_start = true;
577
        } while(!found_start);
578
 
579
        int     rdaddr;
580
 
581
        DBGPRINTF("READ-WORD() -- sixbits = %02x\n", sixbits);
582
        if (0x06 == (sixbits & 0x03e)) { // Tbl read, last value
583
                rdaddr = (m_rdaddr-1)&0x03ff;
584
                val = m_readtbl[rdaddr];
585 45 dgisselq
                m_lastaddr += (sixbits&1)?4:0;
586
                DBGPRINTF("READ-WORD() -- repeat last value, %08x, A= %08x\n", val, m_lastaddr);
587 11 dgisselq
        } else if (0x10 == (sixbits & 0x030)) { // Tbl read, up to 521 into past
588
                int     idx;
589
                do {
590
                        nr += lclreadcode(&m_buf[nr], 2-nr);
591
                } while (nr < 2);
592
 
593
                idx = (chardec(m_buf[0])>>1) & 0x07;
594
                idx = ((idx<<6) | (chardec(m_buf[1]) & 0x03f)) + 2 + 8;
595
                rdaddr = (m_rdaddr-idx)&0x03ff;
596
                val = m_readtbl[rdaddr];
597 45 dgisselq
                m_lastaddr += (sixbits&1)?4:0;
598
                DBGPRINTF("READ-WORD() -- long table value[%3d], %08x, A=%08x\n", idx, val, m_lastaddr);
599 11 dgisselq
        } else if (0x20 == (sixbits & 0x030)) { // Tbl read, 2-9 into past
600 45 dgisselq
                int     idx;
601
                idx = (((sixbits>>1)&0x07)+2);
602
                rdaddr = (m_rdaddr - idx) & 0x03ff;
603 11 dgisselq
                val = m_readtbl[rdaddr];
604 45 dgisselq
                m_lastaddr += (sixbits&1)?4:0;
605
                DBGPRINTF("READ-WORD() -- short table value[%3d], %08x, A=%08x\n", idx, val, m_lastaddr);
606 11 dgisselq
        } else if (0x38 == (sixbits & 0x038)) { // Raw read
607 45 dgisselq
                // DBGPRINTF("READ-WORD() -- RAW-READ, nr = %d\n", nr);
608 11 dgisselq
                do {
609
                        nr += lclreadcode(&m_buf[nr], 6-nr);
610
                } while (nr < 6);
611
 
612
                val = (chardec(m_buf[0])>>1) & 0x03;
613
                val = (val<<6) | (chardec(m_buf[1]) & 0x03f);
614
                val = (val<<6) | (chardec(m_buf[2]) & 0x03f);
615
                val = (val<<6) | (chardec(m_buf[3]) & 0x03f);
616
                val = (val<<6) | (chardec(m_buf[4]) & 0x03f);
617
                val = (val<<6) | (chardec(m_buf[5]) & 0x03f);
618
 
619
                m_readtbl[m_rdaddr++] = val; m_rdaddr &= 0x03ff;
620 45 dgisselq
                m_lastaddr += (sixbits&1)?4:0;
621
                DBGPRINTF("READ-WORD() -- RAW-READ %02x:%02x:%02x:%02x:%02x:%02x -- %08x, A=%08x\n",
622 11 dgisselq
                        m_buf[0], m_buf[1], m_buf[2], m_buf[3],
623 45 dgisselq
                        m_buf[4], m_buf[5], val, m_lastaddr);
624 11 dgisselq
        } else
625
                DBGPRINTF("READ-WORD() -- Unknown character, %02x\n", sixbits);
626
 
627
        return val;
628
}
629
 
630 45 dgisselq
void    TTYBUS::readidle(void) {
631
        TTYBUS::BUSW    val = 0;
632
        int             nr;
633
        unsigned        sixbits;
634
        bool            found_start = false;
635
 
636
        DBGPRINTF("READ-IDLE()\n");
637
 
638
        while((!found_start)&&(m_dev->available())
639
                        &&((nr=lclreadcode(&m_buf[0], 1))>0)) {
640
                nr = lclreadcode(&m_buf[0], 1);
641
                sixbits = chardec(m_buf[0]);
642
 
643
                if (sixbits&(~0x03f)) {
644
                        // Ignore new lines, unprintables, and characters
645
                        // not a part of our code
646
                        ;
647
                } else if (sixbits < 6) {
648
                        switch(sixbits) {
649
                        case 0:  break; // Idle -- ignore
650
                        case 1: break; // Idle, but the bus is busy
651
                        case 2:
652
                                // Write acknowledgement, ignore it here
653
                                // This is one of the big reasons why we are
654
                                // doing this.
655
                                break;
656
                        case 3:
657
                                m_bus_err = true;
658
                                DBGPRINTF("READ-IDLE() - BUSERR\n");
659
                                throw BUSERR(0);
660
                                break;
661
                        case 4:
662
                                m_interrupt_flag = true;
663
                                break;
664
                        case 5:
665
                                m_bus_err = true;
666
                                DBGPRINTF("READ-IDLE() - BUS RESET\n");
667
                                throw BUSERR(0);
668
                                break;
669
                        }
670
                } else if (0x08 == (sixbits & 0x3c)) { // Set 32-bit address
671
                        do {
672
                                nr += lclreadcode(&m_buf[nr], 6-nr);
673
                        } while (nr < 6);
674
 
675
                        val = chardec(m_buf[0]) & 0x03;
676
                        val = (val<<6) | (chardec(m_buf[1]) & 0x03f);
677
                        val = (val<<6) | (chardec(m_buf[2]) & 0x03f);
678
                        val = (val<<6) | (chardec(m_buf[3]) & 0x03f);
679
                        val = (val<<6) | (chardec(m_buf[4]) & 0x03f);
680
                        val = (val<<6) | (chardec(m_buf[5]) & 0x03f);
681
 
682
                        /* Ignore the address, as we are in readidle();
683
                        m_addr_set = true;
684
                        m_lastaddr = val;
685
                        */
686
 
687
                        DBGPRINTF("RCVD IDLE-ADDR: 0x%08x\n", val);
688
                } else if (0x0c == (sixbits & 0x03c)) { // Set 32-bit address,compressed
689
                        int nw = (sixbits & 0x03) + 2;
690
                        do {
691
                                nr += lclreadcode(&m_buf[nr], nw-nr);
692
                        } while (nr < nw);
693
 
694
                        if (nw == 2) {
695
                                val = chardec(m_buf[1]);
696
                        } else if (nw == 3) {
697
                                val = chardec(m_buf[1]);
698
                                val = (val<<6) | (chardec(m_buf[2]) & 0x03f);
699
                        } else if (nw == 4) {
700
                                val = chardec(m_buf[1]);
701
                                val = (val<<6) | (chardec(m_buf[2]) & 0x03f);
702
                                val = (val<<6) | (chardec(m_buf[3]) & 0x03f);
703
                        } else { // if (nw == 5)
704
                                val = chardec(m_buf[1]);
705
                                val = (val<<6) | (chardec(m_buf[2]) & 0x03f);
706
                                val = (val<<6) | (chardec(m_buf[3]) & 0x03f);
707
                                val = (val<<6) | (chardec(m_buf[4]) & 0x03f);
708
                        }
709
 
710
                        /* Ignore address, we are in readidle();
711
                        m_addr_set = true;
712
                        m_lastaddr = val;
713
                        */
714
                        DBGPRINTF("RCVD IDLE-ADDR: 0x%08x (%d bytes)\n", val, nw+1);
715
                } else
716
                        found_start = true;
717
        }
718
 
719
        if (found_start) {
720
                // We're in readidle().  We don't expect to find any data.
721
                // But ... we did.  So, just read it off and ignore it.
722
                int     rdaddr;
723
 
724
                DBGPRINTF("READ-IDLE()  PANIC! -- sixbits = %02x\n", sixbits);
725
                if (0x06 == (sixbits & 0x03e)) { // Tbl read, last value
726
                        rdaddr = (m_rdaddr-1)&0x03ff;
727
                        val = m_readtbl[rdaddr];
728
                        m_lastaddr += (sixbits&1)?4:0;
729
                        DBGPRINTF("READ-IDLE() -- repeat last value, %08x\n", val);
730
                } else if (0x10 == (sixbits & 0x030)) { // Tbl read, up to 521 into past
731
                        int     idx;
732
                        do {
733
                                nr += lclreadcode(&m_buf[nr], 2-nr);
734
                        } while (nr < 2);
735
 
736
                        idx = (chardec(m_buf[0])>>1) & 0x07;
737
                        idx = ((idx<<6) | (chardec(m_buf[1]) & 0x03f)) + 2 + 8;
738
                        rdaddr = (m_rdaddr-idx)&0x03ff;
739
                        val = m_readtbl[rdaddr];
740
                        m_lastaddr += (sixbits&1)?4:0;
741
                        DBGPRINTF("READ-IDLE() -- long table value[%3d], %08x\n", idx, val);
742
                } else if (0x20 == (sixbits & 0x030)) { // Tbl read, 2-9 into past
743
                        rdaddr = (m_rdaddr - (((sixbits>>1)&0x07)+2)) & 0x03ff;
744
                        val = m_readtbl[rdaddr];
745
                        m_lastaddr += (sixbits&1)?4:0;
746
                        DBGPRINTF("READ-IDLE() -- short table value[%3d], %08x\n", rdaddr, val);
747
                } else if (0x38 == (sixbits & 0x038)) { // Raw read
748
                        do {
749
                                nr += lclreadcode(&m_buf[nr], 6-nr);
750
                        } while (nr < 6);
751
 
752
                        val = (chardec(m_buf[0])>>1) & 0x03;
753
                        val = (val<<6) | (chardec(m_buf[1]) & 0x03f);
754
                        val = (val<<6) | (chardec(m_buf[2]) & 0x03f);
755
                        val = (val<<6) | (chardec(m_buf[3]) & 0x03f);
756
                        val = (val<<6) | (chardec(m_buf[4]) & 0x03f);
757
                        val = (val<<6) | (chardec(m_buf[5]) & 0x03f);
758
 
759
                        m_readtbl[m_rdaddr++] = val; m_rdaddr &= 0x03ff;
760
                        m_lastaddr += (sixbits&1)?4:0;
761
                        DBGPRINTF("READ-IDLE() -- RAW-READ %02x:%02x:%02x:%02x:%02x:%02x -- %08x\n",
762
                                m_buf[0], m_buf[1], m_buf[2], m_buf[3],
763
                                m_buf[4], m_buf[5], val);
764
                } else
765
                        DBGPRINTF("READ-IDLE() -- Unknown character, %02x\n", sixbits);
766
 
767
        }
768
}
769
 
770 11 dgisselq
void    TTYBUS::usleep(unsigned ms) {
771
        if (m_dev->poll(ms)) {
772
                int     nr;
773
                nr = m_dev->read(m_buf, 16);
774
                if (nr == 0) {
775
                        // Connection closed, let it drop
776
                        DBGPRINTF("Connection closed!!\n");
777
                        m_dev->close();
778
                        exit(-1);
779
                } for(int i=0; i<nr; i++) {
780
                        if (m_buf[i] == TTYC_INT) {
781
                                m_interrupt_flag = true;
782
                                DBGPRINTF("!!!!!!!!!!!!!!!!! ----- INTERRUPT!\n");
783
                        } else if (m_buf[i] == TTYC_IDLE) {
784
                                DBGPRINTF("Interface is now idle\n");
785
                        } else if (m_buf[i] == TTYC_WRITE) {
786
                        } else if (m_buf[i] == TTYC_RESET) {
787
                                DBGPRINTF("Bus was RESET!\n");
788
                        } else if (m_buf[i] == TTYC_ERR) {
789
                                DBGPRINTF("Bus error\n");
790
                        } else if (m_buf[i] == TTYC_BUSY) {
791
                                DBGPRINTF("Interface is ... busy ??\n");
792
                        }
793
                        // else if (m_buf[nr] == 'Q')
794
                        // else if (m_buf[nr] == 'W')
795
                        // else if (m_buf[nr] == '\n')
796
                }
797
        }
798
}
799
 
800
void    TTYBUS::wait(void) {
801
        if (m_interrupt_flag)
802
                DBGPRINTF("INTERRUPTED PRIOR TO WAIT()\n");
803
        do {
804
                usleep(200);
805
        } while(!m_interrupt_flag);
806
}
807
 
808
// TTYBUS:  3503421 ~= 3.3 MB, stopwatch = 1:18.5 seconds, vs 53.8 secs
809
//      If you issue two 512 word reads at once, time drops to 41.6 secs.
810
// PORTBUS: 6408320 ~= 6.1 MB, ... 26% improvement, 53 seconds real time
811
 

powered by: WebSVN 2.1.0

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