CS318 - Pintos
Pintos source browser for JHU CS318 course
input.c
Go to the documentation of this file.
1 #include "devices/input.h"
2 #include <debug.h>
3 #include "devices/intq.h"
4 #include "devices/serial.h"
5 
6 /** Stores keys from the keyboard and serial port. */
7 static struct intq buffer;
8 
9 /** Initializes the input buffer. */
10 void
11 input_init (void)
12 {
13  intq_init (&buffer);
14 }
15 
16 /** Adds a key to the input buffer.
17  Interrupts must be off and the buffer must not be full. */
18 void
20 {
22  ASSERT (!intq_full (&buffer));
23 
24  intq_putc (&buffer, key);
25  serial_notify ();
26 }
27 
28 /** Retrieves a key from the input buffer.
29  If the buffer is empty, waits for a key to be pressed. */
30 uint8_t
31 input_getc (void)
32 {
33  enum intr_level old_level;
34  uint8_t key;
35 
36  old_level = intr_disable ();
37  key = intq_getc (&buffer);
38  serial_notify ();
39  intr_set_level (old_level);
40 
41  return key;
42 }
43 
44 /** Returns true if the input buffer is full,
45  false otherwise.
46  Interrupts must be off. */
47 bool
48 input_full (void)
49 {
51  return intq_full (&buffer);
52 }
uint8_t
unsigned char uint8_t
Definition: stdint.h:20
intr_level
intr_level
Interrupts on or off?
Definition: interrupt.h:8
intr_set_level
enum intr_level intr_set_level(enum intr_level level)
Enables or disables interrupts as specified by LEVEL and returns the previous interrupt status.
Definition: interrupt.c:81
intq_init
void intq_init(struct intq *q)
Initializes interrupt queue Q.
Definition: intq.c:11
intq_getc
uint8_t intq_getc(struct intq *q)
Removes a byte from Q and returns it.
Definition: intq.c:38
intq.h
input_full
bool input_full(void)
Returns true if the input buffer is full, false otherwise.
Definition: input.c:48
buffer
static struct intq buffer
Stores keys from the keyboard and serial port.
Definition: input.c:7
intr_get_level
enum intr_level intr_get_level(void)
Returns the current interrupt status.
Definition: interrupt.c:65
intq_putc
void intq_putc(struct intq *q, uint8_t byte)
Adds BYTE to the end of Q.
Definition: intq.c:61
serial_notify
void serial_notify(void)
The fullness of the input buffer may have changed.
Definition: serial.c:148
intq_full
bool intq_full(const struct intq *q)
Returns true if Q is full, false otherwise.
Definition: intq.c:28
intr_disable
enum intr_level intr_disable(void)
Disables interrupts and returns the previous interrupt status.
Definition: interrupt.c:104
ASSERT
#define ASSERT(CONDITION)
This is outside the header guard so that debug.h may be included multiple times with different settin...
Definition: debug.h:31
serial.h
intq
A circular queue of bytes.
Definition: intq.h:24
INTR_OFF
Interrupts disabled.
Definition: interrupt.h:10
input_getc
uint8_t input_getc(void)
Retrieves a key from the input buffer.
Definition: input.c:31
input_putc
void input_putc(uint8_t key)
Adds a key to the input buffer.
Definition: input.c:19
input.h
debug.h
input_init
void input_init(void)
Initializes the input buffer.
Definition: input.c:11