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

Subversion Repositories xulalx25soc

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

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

powered by: WebSVN 2.1.0

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