terminal.hh revision 12239
1/*
2 * Copyright (c) 2001-2005 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Authors: Nathan Binkert
29 *          Ali Saidi
30 */
31
32/* @file
33 * User Terminal Interface
34 */
35
36#ifndef __DEV_TERMINAL_HH__
37#define __DEV_TERMINAL_HH__
38
39#include <iostream>
40
41#include "base/callback.hh"
42#include "base/circlebuf.hh"
43#include "base/pollevent.hh"
44#include "base/socket.hh"
45#include "cpu/intr_control.hh"
46#include "dev/serial/serial.hh"
47#include "params/Terminal.hh"
48#include "sim/sim_object.hh"
49
50class OutputStream;
51class TerminalListener;
52
53class Terminal : public SerialDevice
54{
55  protected:
56    class ListenEvent : public PollEvent
57    {
58      protected:
59        Terminal *term;
60
61      public:
62        ListenEvent(Terminal *t, int fd, int e);
63        void process(int revent);
64    };
65
66    friend class ListenEvent;
67    ListenEvent *listenEvent;
68
69    class DataEvent : public PollEvent
70    {
71      protected:
72        Terminal *term;
73
74      public:
75        DataEvent(Terminal *t, int fd, int e);
76        void process(int revent);
77    };
78
79    friend class DataEvent;
80    DataEvent *dataEvent;
81
82  protected:
83    int number;
84    int data_fd;
85
86  public:
87    typedef TerminalParams Params;
88    Terminal(const Params *p);
89    ~Terminal();
90
91  protected:
92    ListenSocket listener;
93
94    void listen(int port);
95    void accept();
96
97  protected:
98    CircleBuf<char> txbuf;
99    CircleBuf<char> rxbuf;
100    OutputStream *outfile;
101#if TRACING_ON == 1
102    CircleBuf<char> linebuf;
103#endif
104
105  public:
106    ///////////////////////
107    // Terminal Interface
108
109    void data();
110
111    void read(uint8_t &c) { read(&c, 1); }
112    size_t read(uint8_t *buf, size_t len);
113    void write(uint8_t c) { write(&c, 1); }
114    size_t write(const uint8_t *buf, size_t len);
115    void detach();
116
117  public: // SerialDevice interface
118    uint8_t readData() override;
119    void writeData(uint8_t c) override;
120    bool dataAvailable() const override { return !rxbuf.empty(); }
121
122  public:
123    /////////////////
124    // OS interface
125
126    // get a character from the terminal in the console specific format
127    // corresponds to GETC:
128    // retval<63:61>
129    //     000: success: character received
130    //     001: success: character received, more pending
131    //     100: failure: no character ready
132    //     110: failure: character received with error
133    //     111: failure: character received with error, more pending
134    // retval<31:0>
135    //     character read from console
136    //
137    // Interrupts are cleared when the buffer is empty.
138    uint64_t console_in();
139};
140
141#endif // __DEV_TERMINAL_HH__
142