Renamed OpClass enum members: they all end in 'Op' now.
[gem5.git] / dev / console.hh
1 /*
2 * Copyright (c) 2003 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
29 /* @file
30 * User Console Interface
31 */
32
33 #ifndef __CONSOLE_HH__
34 #define __CONSOLE_HH__
35
36 #include <iostream>
37
38 #include "base/circlebuf.hh"
39 #include "cpu/intr_control.hh"
40 #include "base/pollevent.hh"
41 #include "base/socket.hh"
42 #include "sim/sim_object.hh"
43
44 class ConsoleListener;
45 class SimConsole : public SimObject
46 {
47 protected:
48 class Event : public PollEvent
49 {
50 protected:
51 SimConsole *cons;
52
53 public:
54 Event(SimConsole *c, int fd, int e);
55 void process(int revent);
56 };
57
58 friend class Event;
59 Event *event;
60
61 protected:
62 int number;
63 int in_fd;
64 int out_fd;
65
66 protected:
67 ConsoleListener *listener;
68
69 public:
70 SimConsole(const std::string &name, const std::string &file, int num);
71 ~SimConsole();
72
73 protected:
74 CircleBuf txbuf;
75 CircleBuf rxbuf;
76 std::ostream *outfile;
77 #if TRACING_ON == 1
78 CircleBuf linebuf;
79 #endif
80
81 public:
82 ///////////////////////
83 // Terminal Interface
84
85 void attach(int fd, ConsoleListener *l = NULL) { attach(fd, fd, l); }
86 void attach(int in, int out, ConsoleListener *l = NULL);
87 void detach();
88
89 void data();
90
91 void close();
92 void read(uint8_t &c) { read(&c, 1); }
93 size_t read(uint8_t *buf, size_t len);
94 void write(uint8_t c) { write(&c, 1); }
95 size_t write(const uint8_t *buf, size_t len);
96
97 void configTerm();
98
99 protected:
100 // interrupt status/enable
101 int _status;
102 int _enable;
103
104 // interrupt handle
105 IntrControl *intr;
106
107 public:
108 /////////////////
109 // OS interface
110
111 // Get a character from the console.
112 bool in(uint8_t &value);
113
114 // get a character from the console in the console specific format
115 // corresponds to GETC:
116 // retval<63:61>
117 // 000: success: character received
118 // 001: success: character received, more pending
119 // 100: failure: no character ready
120 // 110: failure: character received with error
121 // 111: failure: character received with error, more pending
122 // retval<31:0>
123 // character read from console
124 //
125 // Interrupts are cleared when the buffer is empty.
126 uint64_t console_in();
127
128 // Send a character to the console
129 void out(char c, bool raise_int = true);
130
131 enum {
132 TransmitInterrupt = 1,
133 ReceiveInterrupt = 2
134 };
135
136 // Read the current interrupt status of this console.
137 int intStatus() { return _status; }
138
139 // Set the interrupt enable bits.
140 int clearInt(int i);
141 void raiseInt(int i);
142
143 void initInt(IntrControl *i);
144 void setInt(int bits);
145
146 virtual void serialize(std::ostream &os);
147 virtual void unserialize(Checkpoint *cp, const std::string &section);
148 };
149
150 class ConsoleListener : public SimObject
151 {
152 protected:
153 class Event : public PollEvent
154 {
155 protected:
156 ConsoleListener *listener;
157
158 public:
159 Event(ConsoleListener *l, int fd, int e)
160 : PollEvent(fd, e), listener(l) {}
161 void process(int revent);
162 };
163
164 friend class Event;
165 Event *event;
166
167 typedef std::list<SimConsole *> list_t;
168 typedef list_t::iterator iter_t;
169 list_t ConsoleList;
170
171 protected:
172 ListenSocket listener;
173
174 public:
175 ConsoleListener(const std::string &name);
176 ~ConsoleListener();
177
178 void add(SimConsole *cons);
179
180 void accept();
181 void listen(int port);
182 };
183
184 #endif // __CONSOLE_HH__