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

Subversion Repositories wbuart32

[/] [wbuart32/] [trunk/] [bench/] [cpp/] [uartsim.h] - Blame information for rev 26

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 dgisselq
////////////////////////////////////////////////////////////////////////////////
2
//
3
// Filename:    uartsim.h
4
//
5
// Project:     wbuart32, a full featured UART with simulator
6
//
7
// Purpose:     To forward a Verilator simulated UART link over a TCP/IP pipe.
8
//
9
//      This file provides the description of the interface between the UARTSIM
10
//      and the rest of the world.  See below for more detailed descriptions.
11
//
12
// Creator:     Dan Gisselquist, Ph.D.
13
//              Gisselquist Technology, LLC
14
//
15
////////////////////////////////////////////////////////////////////////////////
16
//
17 26 dgisselq
// Copyright (C) 2015-2019, Gisselquist Technology, LLC
18 2 dgisselq
//
19
// This program is free software (firmware): you can redistribute it and/or
20
// modify it under the terms of  the GNU General Public License as published
21
// by the Free Software Foundation, either version 3 of the License, or (at
22
// your option) any later version.
23
//
24
// This program is distributed in the hope that it will be useful, but WITHOUT
25
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
26
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
27
// for more details.
28
//
29
// You should have received a copy of the GNU General Public License along
30 11 dgisselq
// with this program.  (It's in the $(ROOT)/doc directory.  Run make with no
31 2 dgisselq
// target there if the PDF file isn't present.)  If not, see
32
// <http://www.gnu.org/licenses/> for a copy.
33
//
34
// License:     GPL, v3, as defined and found on www.gnu.org,
35
//              http://www.gnu.org/licenses/gpl.html
36
//
37
//
38
////////////////////////////////////////////////////////////////////////////////
39
//
40
//
41
#ifndef UARTSIM_H
42
#define UARTSIM_H
43
 
44
#include <stdio.h>
45
#include <stdlib.h>
46
#include <string.h>
47
#include <sys/types.h>
48
#include <sys/socket.h>
49
#include <poll.h>
50
#include <unistd.h>
51
#include <arpa/inet.h>
52
#include <signal.h>
53
 
54
#define TXIDLE  0
55
#define TXDATA  1
56
#define RXIDLE  0
57
#define RXDATA  1
58
 
59
class   UARTSIM {
60
        // The file descriptors:
61
        //      m_skt   is the socket/port we are listening on
62
        //      m_conrd is the file descriptor to read from
63
        //      m_conwr is the file descriptor to write to
64
        int     m_skt, m_conrd, m_conwr;
65
        //
66
        // The m_setup register is the 29'bit control register used within
67
        // the core.
68
        unsigned m_setup;
69
        // And the pieces of the setup register broken out.
70
        int     m_nparity, m_fixdp, m_evenp, m_nbits, m_nstop, m_baud_counts;
71
 
72
        // UART state
73
        int     m_rx_baudcounter, m_rx_state, m_rx_busy,
74
                m_rx_changectr, m_last_tx;
75
        int     m_tx_baudcounter, m_tx_state, m_tx_busy;
76
        unsigned        m_rx_data, m_tx_data;
77
 
78
        // setup_listener is an attempt to encapsulate all of the network
79
        // related setup stuff.
80
        void    setup_listener(const int port);
81
 
82 18 dgisselq
        // Call check_for_new_connections() to see if we can accept a new
83
        // network socket connection to our device
84
        void    check_for_new_connections(void);
85
 
86 2 dgisselq
        // nettick() gets called if we are connected to a network, and
87
        int     nettick(const int i_tx);
88
        int     fdtick(const int i_tx);
89 23 dgisselq
        int     rawtick(const int i_tx, const bool network);
90 2 dgisselq
 
91
        // We'll use the file descriptor for the listener socket to determine
92
        // whether we are connected to the network or not.  If not connected
93
        // to the network, then we assume m_conrd and m_conwr refer to 
94
        // your more traditional file descriptors, and use them as such.
95
        int     tick(const int i_tx) {
96 23 dgisselq
                return rawtick(i_tx, (m_skt >= 0));
97 2 dgisselq
        }
98
 
99
public:
100
        //
101
        // The UARTSIM constructor takes one argument: the port on the
102
        // localhost to listen in on.  Once started, connections may be made
103
        // to this port to get the output from the port.
104
        UARTSIM(const int port);
105
 
106
        // kill() closes any active connection and the socket.  Once killed,
107
        // no further output will be sent to the port.
108
        void    kill(void);
109
 
110
        // setup() busts out the bits from isetup to the various internal
111
        // parameters.  It is ideally only called between bits at appropriate
112
        // transition intervals. 
113
        void    setup(unsigned isetup);
114
 
115
        // The operator() function is called on every tick.  The input is the
116
        // the output txuart transmit wire from the device.  The output is to
117
        // be connected to the the rxuart receive wire into the device.  This
118
        // makes hookup and operation very simple.
119
        //
120
        // This is the most appropriate simulation entry function if the 
121
        // setup register will never change.
122
        //
123
        int     operator()(int i_tx) {
124
                return tick(i_tx); }
125
 
126
        // If there is a possibility that the core might change the UART setup,
127
        // then it makes sense to include that current setup when calling the
128
        // tick operator.
129
        int     operator()(int i_tx, unsigned isetup) {
130
                setup(isetup); return tick(i_tx); }
131
};
132
 
133
#endif

powered by: WebSVN 2.1.0

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