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

Subversion Repositories xulalx25soc

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

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

powered by: WebSVN 2.1.0

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