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

Subversion Repositories xulalx25soc

[/] [xulalx25soc/] [trunk/] [sw/] [ttybus.cpp] - Blame information for rev 113

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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