make our code a little more standards compliant
[gem5.git] / src / dev / simconsole.hh
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 Console Interface
34 */
35
36 #ifndef __CONSOLE_HH__
37 #define __CONSOLE_HH__
38
39 #include <iostream>
40
41 #include "base/circlebuf.hh"
42 #include "cpu/intr_control.hh"
43 #include "base/pollevent.hh"
44 #include "base/socket.hh"
45 #include "sim/sim_object.hh"
46
47 class ConsoleListener;
48 class Uart;
49
50 class SimConsole : public SimObject
51 {
52 public:
53 Uart *uart;
54
55 protected:
56 class Event : public PollEvent
57 {
58 protected:
59 SimConsole *cons;
60
61 public:
62 Event(SimConsole *c, int fd, int e);
63 void process(int revent);
64 };
65
66 friend class Event;
67 Event *event;
68
69 protected:
70 int number;
71 int in_fd;
72 int out_fd;
73 ConsoleListener *listener;
74
75 public:
76 SimConsole(const std::string &name, std::ostream *os, int num);
77 ~SimConsole();
78
79 protected:
80 CircleBuf txbuf;
81 CircleBuf rxbuf;
82 std::ostream *outfile;
83 #if TRACING_ON == 1
84 CircleBuf linebuf;
85 #endif
86
87 public:
88 ///////////////////////
89 // Terminal Interface
90
91 void attach(int fd, ConsoleListener *l = NULL) { attach(fd, fd, l); }
92 void attach(int in, int out, ConsoleListener *l = NULL);
93 void detach();
94
95 void data();
96
97 void close();
98 void read(uint8_t &c) { read(&c, 1); }
99 size_t read(uint8_t *buf, size_t len);
100 void write(uint8_t c) { write(&c, 1); }
101 size_t write(const uint8_t *buf, size_t len);
102
103 public:
104 /////////////////
105 // OS interface
106
107 // Get a character from the console.
108 uint8_t in();
109
110 // get a character from the console in the console specific format
111 // corresponds to GETC:
112 // retval<63:61>
113 // 000: success: character received
114 // 001: success: character received, more pending
115 // 100: failure: no character ready
116 // 110: failure: character received with error
117 // 111: failure: character received with error, more pending
118 // retval<31:0>
119 // character read from console
120 //
121 // Interrupts are cleared when the buffer is empty.
122 uint64_t console_in();
123
124 // Send a character to the console
125 void out(char c);
126
127 //Ask the console if data is available
128 bool dataAvailable() { return !rxbuf.empty(); }
129
130 virtual void serialize(std::ostream &os);
131 virtual void unserialize(Checkpoint *cp, const std::string &section);
132 };
133
134 class ConsoleListener : public SimObject
135 {
136 protected:
137 class Event : public PollEvent
138 {
139 protected:
140 ConsoleListener *listener;
141
142 public:
143 Event(ConsoleListener *l, int fd, int e)
144 : PollEvent(fd, e), listener(l) {}
145 void process(int revent);
146 };
147
148 friend class Event;
149 Event *event;
150
151 typedef std::list<SimConsole *> list_t;
152 typedef list_t::iterator iter_t;
153 list_t ConsoleList;
154
155 protected:
156 ListenSocket listener;
157
158 public:
159 ConsoleListener(const std::string &name);
160 ~ConsoleListener();
161
162 void add(SimConsole *cons);
163
164 void accept();
165 void listen(int port);
166 };
167
168 #endif // __CONSOLE_HH__