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

Subversion Repositories xulalx25soc

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

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
// Copyright (C) 2015, Gisselquist Technology, LLC
22
//
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 13 dgisselq
#define DBGPRINTF       null
72 5 dgisselq
// #define      DBGPRINTF       printf
73 13 dgisselq
// #define      DBGPRINTF       filedump
74 5 dgisselq
 
75 13 dgisselq
void    null(...) {}
76 11 dgisselq
#include <stdarg.h>
77
// #include <varargs.h>
78 46 dgisselq
/*
79
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
}
90 46 dgisselq
*/
91 11 dgisselq
 
92 46 dgisselq
char    TTYBUS::charenc(const int sixbitval) const {
93 5 dgisselq
        if (sixbitval < 10)
94
                return '0' + sixbitval;
95
        else if (sixbitval < 10+26)
96
                return 'A' - 10 + sixbitval;
97
        else if (sixbitval < 10+26+26)
98
                return 'a' - 10 - 26 + sixbitval;
99
        else if (sixbitval == 0x3e)
100
                return '@';
101
        else if (sixbitval == 0x3f)
102
                return '%';
103
 
104
        fprintf(stderr, "INTERNAL ERR: SIXBITVAL isn\'t!!!! sixbitval = %08x\n", sixbitval);
105
        assert((sixbitval & (~0x03f))==0);
106
        return 0;
107
}
108
 
109 46 dgisselq
unsigned        TTYBUS::chardec(const char b) const {
110 5 dgisselq
        if ((b >= '0')&&(b <= '9'))
111
                return b-'0';
112
        else if ((b >= 'A')&&(b <= 'Z'))
113
                return b-'A'+10;
114
        else if ((b >= 'a')&&(b <= 'z'))
115
                return b-'a'+36;
116
        else if (b == '@')
117
                return 0x03e;
118
        else if (b == '%')
119
                return 0x03f;
120
        else
121
                return 0x0100; // ERR -- invalid code
122
}
123
 
124
int     TTYBUS::lclreadcode(char *buf, int len) {
125
        char    *sp, *dp;
126
        int     nr, ret;
127
 
128
        nr = m_dev->read(buf, len);
129
        m_total_nread += nr;
130
        ret = nr; sp = buf; dp = buf;
131
        for(int i=0; i<nr; i++) {
132
                if (chardec(*sp)&(~0x3f)) {
133
                        ret--;  // Skip this value, not a valid codeword
134
                        sp++;
135
                } else {
136
                        *sp++ = *dp++;
137
                }
138
        } return ret;
139
}
140
 
141
void    TTYBUS::bufalloc(int len) {
142
        if ((m_buf)&&(m_buflen >= len))
143
                return;
144
        if (m_buf)
145
                delete[] m_buf;
146
        m_buflen = (len&(-0x3f))+0x40;
147
        m_buf = new char[m_buflen];
148
}
149
 
150 46 dgisselq
void    TTYBUS::encode(const int hb, const BUSW val, char *buf) const {
151 5 dgisselq
        buf[0] = charenc( (hb<<2)|((val>>30)&0x03) );
152
        buf[1] = charenc( (val>>24)&0x3f);
153
        buf[2] = charenc( (val>>18)&0x3f);
154
        buf[3] = charenc( (val>>12)&0x3f);
155
        buf[4] = charenc( (val>> 6)&0x3f);
156
        buf[5] = charenc( (val    )&0x3f);
157
}
158
 
159 46 dgisselq
unsigned        TTYBUS::decodestr(const char *buf) const {
160 5 dgisselq
        unsigned        r;
161
 
162
        r = chardec(buf[0]) & 0x03;
163
        r = (r<<6) | (chardec(buf[1]) & 0x03f);
164
        r = (r<<6) | (chardec(buf[2]) & 0x03f);
165
        r = (r<<6) | (chardec(buf[3]) & 0x03f);
166
        r = (r<<6) | (chardec(buf[4]) & 0x03f);
167
        r = (r<<6) | (chardec(buf[5]) & 0x03f);
168
 
169
        return r;
170
}
171
 
172 46 dgisselq
int     TTYBUS::decodehex(const char hx) const {
173 5 dgisselq
        if ((hx >= '0')&&(hx <= '9'))
174
                return hx-'0';
175
        else if ((hx >= 'A')&&(hx <= 'Z'))
176
                return hx-'A'+10;
177
        else if ((hx >= 'a')&&(hx <= 'z'))
178
                return hx-'a'+10;
179
        else
180
                return 0;
181
}
182
 
183
void    TTYBUS::writeio(const BUSW a, const BUSW v) {
184
 
185
        writev(a, 0, 1, &v);
186
        m_lastaddr = a; m_addr_set = true;
187
}
188
 
189
void    TTYBUS::writev(const BUSW a, const int p, const int len, const BUSW *buf) {
190
        char    *ptr;
191
 
192
        // Allocate a buffer of six bytes per word, one for addr, plus
193
        // six more
194
        bufalloc((len+2)*6);
195
 
196
        DBGPRINTF("WRITEV(%08x,%d,#%d,0x%08x ...)\n", a, p, len, buf[0]);
197
        // Encode the address
198
        ptr = encode_address(a);
199
        m_lastaddr = a; m_addr_set = true;
200
 
201
        for(int i=0; i<len; i++) {
202
                BUSW    val = buf[i];
203
 
204
                int     caddr = 0;
205
                // Let's try compression
206
                for(int i=1; i<256; i++) {
207
                        unsigned        tstaddr;
208
                        tstaddr = (m_wraddr - i) & 0x0ff;
209
                        if ((!m_wrloaded)&&(tstaddr > (unsigned)m_wraddr))
210
                                break;
211
                        if (m_writetbl[tstaddr] == val) {
212
                                caddr = ( m_wraddr- tstaddr ) & 0x0ff;
213
                                break;
214
                        }
215
                }
216
 
217
                if (caddr != 0)
218
                        DBGPRINTF("WR[%08x] = %08x (= TBL[%4x] <= %4x)\n", m_lastaddr, val, caddr, m_wraddr);
219
                else
220
                        DBGPRINTF("WR[%08x] = %08x\n", m_lastaddr, val);
221
 
222
                if (caddr != 0) {
223
                        *ptr++ = charenc( (((caddr>>6)&0x03)<<1) + (p?1:0) + 0x010);
224
                        *ptr++ = charenc(    caddr    &0x3f    );
225
 
226
                } else {
227
                        // For testing, let's start just doing this the hard way
228
                        *ptr++ = charenc( (((val>>30)&0x03)<<1) + (p?1:0) + 0x018);
229
                        *ptr++ = charenc( (val>>24)&0x3f);
230
                        *ptr++ = charenc( (val>>18)&0x3f);
231
                        *ptr++ = charenc( (val>>12)&0x3f);
232
                        *ptr++ = charenc( (val>> 6)&0x3f);
233
                        *ptr++ = charenc( (val    )&0x3f);
234
 
235
                        m_writetbl[m_wraddr++] = val;
236
                        m_wraddr &= 0x0ff;
237
                        if (m_wraddr == 0) {
238
                                m_wrloaded = true;
239
                        }
240
                }
241
 
242
                if (p == 1) m_lastaddr++;
243
        }
244
        // *ptr++ = charenc(0x2e);
245
        *ptr++ = '\n'; *ptr = '\0';
246
        m_dev->write(m_buf, ptr-m_buf);
247
 
248
        DBGPRINTF(">> %s\n", m_buf);
249
        DBGPRINTF("WR: LAST ADDRESS LEFT AT %08x\n", m_lastaddr);
250
}
251
 
252
void    TTYBUS::writez(const BUSW a, const int len, const BUSW *buf) {
253
        int     ln = len;
254
        const TTYBUS::BUSW *bptr = buf;
255
        TTYBUS::BUSW addr = a;
256
 
257
        while((unsigned)ln > MAXWRLEN) {
258
                writev(addr, 0, MAXWRLEN, bptr);
259
                bptr += MAXWRLEN;
260
                ln   -= MAXWRLEN;
261
                // addr += MAXWRLEN;
262
        } if ((unsigned)ln > 0)
263
                writev(addr, 0, ln, bptr);
264
}
265
 
266
void    TTYBUS::writei(const BUSW a, const int len, const BUSW *buf) {
267
        int     ln = len;
268
        const TTYBUS::BUSW *bptr = buf;
269
        TTYBUS::BUSW addr = a;
270
 
271
        while((unsigned)ln > MAXWRLEN) {
272
                writev(addr, 1, MAXWRLEN, bptr);
273
                bptr += MAXWRLEN;
274
                ln   -= MAXWRLEN;
275
                addr += MAXWRLEN;
276
        } if ((unsigned)ln > 0)
277
                writev(addr, 1, ln, bptr);
278
}
279
 
280
TTYBUS::BUSW    TTYBUS::readio(const TTYBUS::BUSW a) {
281
        BUSW    v;
282
 
283
        // I/O reads are now the same as vector reads, but with a vector length
284
        // of one.
285 11 dgisselq
        DBGPRINTF("READIO(0x%08x)\n", a);
286 5 dgisselq
        try {
287
                readv(a, 0, 1, &v);
288
        } catch(BUSERR b) {
289
                throw BUSERR(a);
290
        }
291
 
292
        if (m_lastaddr != a) {
293
                DBGPRINTF("LAST-ADDR MIS-MATCH: (RCVD) %08x != %08x (XPECTED)\n", m_lastaddr, a);
294
                m_addr_set = false;
295
 
296
                exit(-3);
297
        }
298
 
299
        return v;
300
}
301
 
302
char    *TTYBUS::encode_address(const TTYBUS::BUSW a) {
303
        TTYBUS::BUSW    addr = a;
304
        char    *ptr = m_buf;
305
 
306 13 dgisselq
// #warning DEBUG_APPROACH
307
        // encode(0, addr, ptr);
308
        // return ptr+6;
309
 
310 5 dgisselq
        if ((m_addr_set)&&(a == m_lastaddr))
311
                return ptr;
312
 
313
        if (m_addr_set) {
314
                // Encode a difference address
315
                int     diffaddr = addr - m_lastaddr;
316
                ptr = m_buf;
317
                if ((diffaddr >= -32)&&(diffaddr < 32)) {
318
                        *ptr++ = charenc(0x09);
319
                        *ptr++ = charenc(diffaddr & 0x03f);
320
                } else if ((diffaddr >= -2048)&&(diffaddr < 2048)) {
321
                        *ptr++ = charenc(0x0b);
322
                        *ptr++ = charenc((diffaddr>>6) & 0x03f);
323
                        *ptr++ = charenc( diffaddr     & 0x03f);
324
                } else if ((diffaddr >= -(1<<17))&&(diffaddr < (1<<17))) {
325
                        *ptr++ = charenc(0x0d);
326
                        *ptr++ = charenc((diffaddr>>12) & 0x03f);
327
                        *ptr++ = charenc((diffaddr>> 6) & 0x03f);
328
                        *ptr++ = charenc( diffaddr      & 0x03f);
329
                } else if ((diffaddr >= -(1<<23))&&(diffaddr < (1<<23))) {
330
                        *ptr++ = charenc(0x0d);
331
                        *ptr++ = charenc((diffaddr>>18) & 0x03f);
332
                        *ptr++ = charenc((diffaddr>>12) & 0x03f);
333
                        *ptr++ = charenc((diffaddr>> 6) & 0x03f);
334
                        *ptr++ = charenc( diffaddr      & 0x03f);
335
                }
336
                *ptr = '\0';
337 11 dgisselq
                DBGPRINTF("DIF-ADDR: (%ld) \'%s\'\n", ptr-m_buf, m_buf);
338 5 dgisselq
        }
339
 
340
        {
341
                // Encode an absolute (low memory) address
342
                // Prefer absolute address encoding over differential encoding,
343
                // when both encodings encode the same address, and when both
344
                // encode the address in the same number of words
345
                if ((addr <= 0x03f)&&((ptr == m_buf)||(ptr >= &m_buf[2]))) {
346
                        ptr = m_buf;
347
                        *ptr++ = charenc(0x08);
348
                        *ptr++ = charenc(addr);
349
                } else if((addr <= 0x0fff)&&((ptr == m_buf)||(ptr >= &m_buf[3]))) {
350 11 dgisselq
                        DBGPRINTF("Setting ADDR.3 to %08x\n", addr);
351 5 dgisselq
                        ptr = m_buf;
352
                        *ptr++ = charenc(0x0a);
353
                        *ptr++ = charenc((addr>> 6) & 0x03f);
354
                        *ptr++ = charenc( addr      & 0x03f);
355
                } else if((addr <= 0x03ffff)&&((ptr == m_buf)||(ptr >= &m_buf[4]))) {
356 11 dgisselq
                        DBGPRINTF("Setting ADDR.4 to %08x\n", addr);
357 5 dgisselq
                        ptr = m_buf;
358
                        *ptr++ = charenc(0x0c);
359
                        *ptr++ = charenc((addr>>12) & 0x03f);
360
                        *ptr++ = charenc((addr>> 6) & 0x03f);
361
                        *ptr++ = charenc( addr      & 0x03f);
362
                } else if((addr <= 0x0ffffff)&&((ptr == m_buf)||(ptr >= &m_buf[5]))) {
363 11 dgisselq
                        DBGPRINTF("Setting ADDR.5 to %08x\n", addr);
364 5 dgisselq
                        ptr = m_buf;
365
                        *ptr++ = charenc(0x0e);
366
                        *ptr++ = charenc((addr>>18) & 0x03f);
367
                        *ptr++ = charenc((addr>>12) & 0x03f);
368
                        *ptr++ = charenc((addr>> 6) & 0x03f);
369
                        *ptr++ = charenc( addr      & 0x03f);
370
                } else if (ptr == m_buf) { // Send our address prior to any read
371 13 dgisselq
                        // ptr = m_buf;
372 5 dgisselq
                        encode(0, addr, ptr);
373
                        ptr+=6;
374
                }
375
        }
376
 
377
        *ptr = '\0';
378 11 dgisselq
        DBGPRINTF("ADDR-CMD: (%ld) \'%s\'\n", ptr-m_buf, m_buf);
379 5 dgisselq
        m_rdaddr = 0;
380
 
381
        return ptr;
382
}
383
 
384
char    *TTYBUS::readcmd(const int inc, const int len, char *buf) {
385
        char    *ptr = buf;
386
 
387 11 dgisselq
        DBGPRINTF("READCMD: LEN = %d\n", len);
388 5 dgisselq
        assert(len < 520);
389
        assert(len > 0);
390
 
391
        if ((len < 8)||((len == 8)&&(inc))) {
392
                *ptr++ = charenc(0x20 + (((len-1)&0x07)<<1) + (inc?1:0));
393
        } else {
394
                *ptr++ = charenc(0x30 + (((len-8)>>5)&0x0e) + (inc?1:0));
395
                *ptr++ = charenc( (len-8) & 0x03f);
396
        }
397
 
398
        return ptr;
399
}
400
 
401
void    TTYBUS::readv(const TTYBUS::BUSW a, const int inc, const int len, TTYBUS::BUSW *buf) {
402
        const   int     READAHEAD = MAXRDLEN/2, READBLOCK=MAXRDLEN/2;
403
        int     cmdrd = 0, nread = 0;
404
        // TTYBUS::BUSW addr = a;
405
        char    *ptr = m_buf;
406
 
407
        if (len <= 0)
408
                return;
409 11 dgisselq
        DBGPRINTF("READV(%08x,%d,#%4d)\n", a, inc, len);
410 5 dgisselq
 
411
        ptr = encode_address(a);
412
        try {
413
            while(cmdrd < len) {
414
                // ptr = m_buf;
415
                do {
416
                        int     nrd = len-cmdrd;
417
                        if (nrd > READBLOCK)
418
                                nrd = READBLOCK;
419
                        if (cmdrd-nread + nrd>READAHEAD+READBLOCK)
420
                                nrd = READAHEAD+READBLOCK-(cmdrd-nread);
421
                        ptr = readcmd(inc, nrd, ptr);
422
                        cmdrd += nrd;
423
                } while((cmdrd-nread < READAHEAD+READBLOCK)&&(cmdrd< len));
424
 
425
                *ptr++ = '\n'; *ptr = '\0';
426
                m_dev->write(m_buf, (ptr-m_buf));
427
 
428
                while(nread<(cmdrd-READAHEAD)) {
429
                        buf[nread++] = readword();
430
                } ptr = m_buf;
431
            } while(nread<len) {
432
                buf[nread++] = readword();
433
            }
434
        } catch(BUSERR b) {
435
                throw BUSERR(a+((inc)?nread:0));
436
        }
437
 
438
        if ((unsigned)m_lastaddr != (a+((inc)?(len):0))) {
439 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);
440 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);
441
                sleep(1);
442
                assert((int)m_lastaddr == (a+(inc)?(len):0));
443
                exit(-3);
444
        }
445
}
446
 
447
void    TTYBUS::readi(const TTYBUS::BUSW a, const int len, TTYBUS::BUSW *buf) {
448
        readv(a, 1, len, buf);
449
}
450
 
451
void    TTYBUS::readz(const TTYBUS::BUSW a, const int len, TTYBUS::BUSW *buf) {
452
        readv(a, 0, len, buf);
453
}
454
 
455
TTYBUS::BUSW    TTYBUS::readword(void) {
456
        TTYBUS::BUSW    val = 0;
457
        int             nr;
458
        unsigned        sixbits;
459
 
460 11 dgisselq
        DBGPRINTF("READ-WORD()\n");
461 5 dgisselq
 
462
        bool    found_start = false;
463
        do {
464
                // Blocking read (for now)
465
                do {
466
                        nr = lclreadcode(&m_buf[0], 1);
467
                } while (nr < 1);
468
 
469
                sixbits = chardec(m_buf[0]);
470
 
471
                if (sixbits&(~0x03f)) {
472
                        // Ignore new lines, unprintables, and characters
473
                        // not a part of our code
474
                        ;
475
                } else if (sixbits < 6) {
476
                        switch(sixbits) {
477
                        case 0:  break; // Idle -- ignore
478
                        case 1: break; // Idle, but the bus is busy
479
                        case 2: break; // Write acknowledgement, ignore it here
480
                        case 3:
481
                                m_bus_err = true;
482
                                throw BUSERR(0);
483
                                break;
484
                        case 4:
485
                                m_interrupt_flag = true;
486
                                break;
487
                        case 5:
488
                                m_bus_err = true;
489
                                throw BUSERR(0);
490
                                break;
491
                        }
492
                } else if (0x08 == (sixbits & 0x3c)) { // Set 32-bit address
493
                        do {
494
                                nr += lclreadcode(&m_buf[nr], 6-nr);
495
                        } while (nr < 6);
496
 
497
                        val = chardec(m_buf[0]) & 0x03;
498
                        val = (val<<6) | (chardec(m_buf[1]) & 0x03f);
499
                        val = (val<<6) | (chardec(m_buf[2]) & 0x03f);
500
                        val = (val<<6) | (chardec(m_buf[3]) & 0x03f);
501
                        val = (val<<6) | (chardec(m_buf[4]) & 0x03f);
502
                        val = (val<<6) | (chardec(m_buf[5]) & 0x03f);
503
 
504
                        m_addr_set = true;
505
                        m_lastaddr = val;
506
 
507 11 dgisselq
                        DBGPRINTF("RCVD ADDR: 0x%08x\n", val);
508 5 dgisselq
                } else if (0x0c == (sixbits & 0x03c)) { // Set 32-bit address,compressed
509
                        int nw = (sixbits & 0x03) + 2;
510
                        do {
511
                                nr += lclreadcode(&m_buf[nr], nw-nr);
512
                        } while (nr < nw);
513
 
514
                        if (nw == 2) {
515
                                val = chardec(m_buf[1]);
516
                        } else if (nw == 3) {
517
                                val = chardec(m_buf[1]);
518
                                val = (val<<6) | (chardec(m_buf[2]) & 0x03f);
519
                        } else if (nw == 4) {
520
                                val = chardec(m_buf[1]);
521
                                val = (val<<6) | (chardec(m_buf[2]) & 0x03f);
522
                                val = (val<<6) | (chardec(m_buf[3]) & 0x03f);
523
                        } else { // if (nw == 5)
524
                                val = chardec(m_buf[1]);
525
                                val = (val<<6) | (chardec(m_buf[2]) & 0x03f);
526
                                val = (val<<6) | (chardec(m_buf[3]) & 0x03f);
527
                                val = (val<<6) | (chardec(m_buf[4]) & 0x03f);
528
                        }
529
 
530
                        m_addr_set = true;
531
                        m_lastaddr = val;
532 11 dgisselq
                        DBGPRINTF("RCVD ADDR: 0x%08x (%d bytes)\n", val, nw+1);
533 5 dgisselq
                } else
534
                        found_start = true;
535
        } while(!found_start);
536
 
537
        int     rdaddr;
538
 
539 46 dgisselq
        DBGPRINTF("READ-WORD() -- sixbits = %02x\n", sixbits);
540 5 dgisselq
        if (0x06 == (sixbits & 0x03e)) { // Tbl read, last value
541
                rdaddr = (m_rdaddr-1)&0x03ff;
542
                val = m_readtbl[rdaddr];
543
                m_lastaddr += (sixbits&1);
544 46 dgisselq
                DBGPRINTF("READ-WORD() -- repeat last value, %08x\n", val);
545 5 dgisselq
        } else if (0x10 == (sixbits & 0x030)) { // Tbl read, up to 521 into past
546
                int     idx;
547
                do {
548
                        nr += lclreadcode(&m_buf[nr], 2-nr);
549
                } while (nr < 2);
550
 
551
                idx = (chardec(m_buf[0])>>1) & 0x07;
552
                idx = ((idx<<6) | (chardec(m_buf[1]) & 0x03f)) + 2 + 8;
553
                rdaddr = (m_rdaddr-idx)&0x03ff;
554
                val = m_readtbl[rdaddr];
555
                m_lastaddr += (sixbits&1);
556 46 dgisselq
                DBGPRINTF("READ-WORD() -- long table value[%3d], %08x\n", idx, val);
557 5 dgisselq
        } else if (0x20 == (sixbits & 0x030)) { // Tbl read, 2-9 into past
558
                rdaddr = (m_rdaddr - (((sixbits>>1)&0x07)+2)) & 0x03ff;
559
                val = m_readtbl[rdaddr];
560
                m_lastaddr += (sixbits&1);
561 46 dgisselq
                DBGPRINTF("READ-WORD() -- short table value[%3d], %08x\n", rdaddr, val);
562 5 dgisselq
        } else if (0x38 == (sixbits & 0x038)) { // Raw read
563 46 dgisselq
                DBGPRINTF("READ-WORD() -- RAW-READ, nr = %d\n", nr);
564 5 dgisselq
                do {
565
                        nr += lclreadcode(&m_buf[nr], 6-nr);
566
                } while (nr < 6);
567
 
568
                val = (chardec(m_buf[0])>>1) & 0x03;
569
                val = (val<<6) | (chardec(m_buf[1]) & 0x03f);
570
                val = (val<<6) | (chardec(m_buf[2]) & 0x03f);
571
                val = (val<<6) | (chardec(m_buf[3]) & 0x03f);
572
                val = (val<<6) | (chardec(m_buf[4]) & 0x03f);
573
                val = (val<<6) | (chardec(m_buf[5]) & 0x03f);
574
 
575
                m_readtbl[m_rdaddr++] = val; m_rdaddr &= 0x03ff;
576
                m_lastaddr += (sixbits&1);
577 46 dgisselq
                DBGPRINTF("READ-WORD() -- RAW-READ %02x:%02x:%02x:%02x:%02x:%02x -- %08x\n",
578
                        m_buf[0], m_buf[1], m_buf[2], m_buf[3],
579
                        m_buf[4], m_buf[5], val);
580
        } else
581
                DBGPRINTF("READ-WORD() -- Unknown character, %02x\n", sixbits);
582 5 dgisselq
 
583
        return val;
584
}
585
 
586
void    TTYBUS::usleep(unsigned ms) {
587
        if (m_dev->poll(ms)) {
588
                int     nr;
589
                nr = m_dev->read(m_buf, 16);
590
                if (nr == 0) {
591
                        // Connection closed, let it drop
592
                        DBGPRINTF("Connection closed!!\n");
593
                        m_dev->close();
594
                        exit(-1);
595
                } for(int i=0; i<nr; i++) {
596
                        if (m_buf[i] == TTYC_INT) {
597
                                m_interrupt_flag = true;
598 11 dgisselq
                                DBGPRINTF("!!!!!!!!!!!!!!!!! ----- INTERRUPT!\n");
599 5 dgisselq
                        } else if (m_buf[i] == TTYC_IDLE) {
600 11 dgisselq
                                DBGPRINTF("Interface is now idle\n");
601 5 dgisselq
                        } else if (m_buf[i] == TTYC_WRITE) {
602
                        } else if (m_buf[i] == TTYC_RESET) {
603 11 dgisselq
                                DBGPRINTF("Bus was RESET!\n");
604 5 dgisselq
                        } else if (m_buf[i] == TTYC_ERR) {
605
                                DBGPRINTF("Bus error\n");
606
                        } else if (m_buf[i] == TTYC_BUSY) {
607 11 dgisselq
                                DBGPRINTF("Interface is ... busy ??\n");
608 5 dgisselq
                        }
609
                        // else if (m_buf[nr] == 'Q')
610
                        // else if (m_buf[nr] == 'W')
611
                        // else if (m_buf[nr] == '\n')
612
                }
613
        }
614
}
615
 
616
void    TTYBUS::wait(void) {
617
        if (m_interrupt_flag)
618
                DBGPRINTF("INTERRUPTED PRIOR TO WAIT()\n");
619
        do {
620
                usleep(200);
621
        } while(!m_interrupt_flag);
622
}
623
 
624
// TTYBUS:  3503421 ~= 3.3 MB, stopwatch = 1:18.5 seconds, vs 53.8 secs
625
//      If you issue two 512 word reads at once, time drops to 41.6 secs.
626
// PORTBUS: 6408320 ~= 6.1 MB, ... 26% improvement, 53 seconds real time
627
 

powered by: WebSVN 2.1.0

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