Skip to content
Snippets Groups Projects
Commit 9a1b5d0f authored by Dor Laor's avatar Dor Laor
Browse files

Adding read line support to the console

Now our Isa serial device can read characters.
Using it by calling readln(char *msg, size_t len);
Note that the calls block until the len input chars or a newline.
parent 450e2851
No related branches found
No related tags found
No related merge requests found
......@@ -153,6 +153,11 @@ extern "C" {
console::write(msg, strlen(msg), true);
}
void readln(char *msg, size_t size)
{
console::read(msg, size);
}
void debug_write(const char *msg, size_t len)
{
console::write(msg, len, false);
......
......@@ -18,6 +18,16 @@ void write(const char *msg, size_t len, bool lf)
console.newline();
}
void read(char *msg, size_t len)
{
for (size_t i = 0; i < len; i++) {
msg[i] = console.readch();
write(&msg[i],1,false);
if (msg[i] == '\r')
break;
}
}
}
static int
......
......@@ -7,12 +7,14 @@ class Console {
public:
virtual ~Console() {}
virtual void write(const char *str, size_t len) = 0;
virtual char readch() = 0;
virtual void newline() = 0;
};
namespace console {
void write(const char *msg, size_t len, bool lf);
void read(char *msg, size_t len);
}
......
......@@ -14,3 +14,8 @@ void debug_console::newline()
{
with_lock(_lock, [=] { _impl.newline(); });
}
char debug_console::readch()
{
return with_lock(_lock, [=] { return _impl.readch(); });
}
......@@ -12,6 +12,7 @@ public:
explicit debug_console(Console& impl);
virtual void write(const char *str, size_t len);
virtual void newline();
virtual char readch();
private:
Console& _impl;
spinlock _lock;
......
#include "isa-serial.hh"
#include <string.h>
IsaSerialConsole::IsaSerialConsole() {
reset();
......@@ -12,6 +13,20 @@ void IsaSerialConsole::write(const char *str, size_t len)
}
}
char IsaSerialConsole::readch()
{
u8 lsr;
char letter;
do {
lsr = pci::inb(ioport + LSR_ADDRESS);
} while (!(lsr & (LSR_RECEIVE_DATA_READY | LSR_OVERRUN | LSR_PARITY_ERROR | LSR_FRAME_ERROR)));
letter = pci::inb(ioport);
return letter;
}
void IsaSerialConsole::writeByte(const char letter)
{
u8 lsr;
......
......@@ -10,6 +10,7 @@ public:
IsaSerialConsole();
virtual void write(const char *str, size_t len);
virtual void newline();
virtual char readch();
private:
static const u16 ioport = 0x3f8;
u8 lcr;
......@@ -21,7 +22,14 @@ private:
BAUD_GEN0_ADDRESS = 0x0,
BAUD_GEN1_ADDRESS = 0x1,
LSR_ADDRESS = 0x5,
LSR_RECEIVE_DATA_READY = 0x1,
LSR_OVERRUN = 0x2,
LSR_PARITY_ERROR = 0x4,
LSR_FRAME_ERROR = 0x8,
LSR_BREAK_INTERRUPT = 0x10,
LSR_TRANSMIT_HOLD_EMPTY = 0x20,
LSR_TRANSMIT_EMPTY = 0x40,
LSR_FIFO_ERROR = 0x80,
};
void reset();
......
......@@ -55,4 +55,7 @@ extern "C" {void debug(const char *msg); }
void debug(const boost::format& fmt, bool lf=true);
void debug(std::string str, bool lf=true);
extern "C" {void readln(char *msg, size_t size); }
#endif // DEBUG_H
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment