metal-cos/sys/dev/console.c
2014-07-21 23:43:01 -07:00

92 lines
1.3 KiB
C

#include <stdint.h>
#include <sys/spinlock.h>
#include "console.h"
#include "x86/vgacons.h"
#include "x86/sercons.h"
#include "x86/debugcons.h"
Spinlock consoleLock;
/*
* Initialize console devices for debugging purposes. At this point interrupts
* and device state is not initialized.
*/
void
Console_Init()
{
VGA_Init();
Serial_Init();
DebugConsole_Init();
Console_Puts("Castor Operating System\n");
Spinlock_Init(&consoleLock, "Console");
}
/*
* Setup interrupts and input devices that may not be ready
*/
void
Console_LateInit()
{
Serial_LateInit();
}
char
Console_Getc()
{
return Serial_Getc();
}
void
Console_Gets(char *str, size_t n)
{
int i;
for (i = 0; i < (n - 1); i++)
{
char ch = Console_Getc();
if (ch == '\b') {
if (i > 0) {
Console_Putc(ch);
i--;
}
i--;
continue;
}
if (ch == '\r') {
Console_Putc('\n');
str[i] = '\0';
return;
}
Console_Putc(ch);
str[i] = ch;
}
str[i+1] = '\0';
}
void
Console_Putc(char ch)
{
Spinlock_Lock(&consoleLock);
VGA_Putc(ch);
Serial_Putc(ch);
DebugConsole_Putc(ch);
Spinlock_Unlock(&consoleLock);
}
void
Console_Puts(const char *str)
{
Spinlock_Lock(&consoleLock);
VGA_Puts(str);
Serial_Puts(str);
DebugConsole_Puts(str);
Spinlock_Unlock(&consoleLock);
}