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

Subversion Repositories openarty

[/] [openarty/] [trunk/] [sw/] [host/] [ttybus.cpp] - Blame information for rev 48

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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