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