Add PS2 keyboard and base device support.
[qemupp.git] / ps2 / ps2.cpp
blobbebc42617590b354a7e099a09ece5353d169f0dc
1 #include "ps2.hpp"
3 PS2Device::~PS2Device(void)
7 uint32_t PS2Device::read(void)
9 int val, index;
11 if (this->count == 0) {
12 /* NOTE: if no data left, we return the last keyboard one
13 (needed for EMM386) */
14 /* XXX: need a timer to do things correctly */
15 index = this->rptr - 1;
16 if (index < 0) {
17 index = PS2_QUEUE_SIZE - 1;
19 val = this->data[index];
20 } else {
21 val = this->data[this->rptr];
22 if (++this->rptr == PS2_QUEUE_SIZE) {
23 this->rptr = 0;
25 this->count--;
26 /* reading deasserts IRQ */
27 this->irq.lower();
28 if (this->count != 0) {
29 /* reassert IRQs if data left */
30 this->irq.raise();
33 return val;
36 void PS2Device::queue(int b)
38 if (this->count >= PS2_QUEUE_SIZE) {
39 return;
41 this->data[this->wptr] = b;
42 if (++this->wptr == PS2_QUEUE_SIZE) {
43 this->wptr = 0;
45 this->count++;
46 this->irq.raise();
49 void PS2Device::marshal(Marshaller *m, const char *name)
51 m->start_struct(name, "PS2Device");
52 ::marshal(m, "irq", &this->irq);
53 ::marshal(m, "write_cmd", &this->write_cmd);
54 ::marshal_array(m, "data", this->data, PS2_QUEUE_SIZE);
55 ::marshal(m, "rptr", &this->rptr);
56 ::marshal(m, "wptr", &this->wptr);
57 ::marshal(m, "count", &this->count);
58 m->end_struct();