mirror of
https://github.com/kenshineto/kern.git
synced 2025-04-16 23:47:25 +00:00
serial and tty
This commit is contained in:
parent
d604f8e93f
commit
3c2a519ee9
10 changed files with 215 additions and 21 deletions
|
@ -32,8 +32,10 @@ const kernel_src = &[_][]const u8{
|
|||
"kernel/cpu/pic.c",
|
||||
"kernel/drivers/drivers.c",
|
||||
"kernel/drivers/pci.c",
|
||||
"kernel/io/io.c",
|
||||
"kernel/io/panic.c",
|
||||
"kernel/drivers/tty.c",
|
||||
"kernel/drivers/uart.c",
|
||||
"kernel/lib/fputc.c",
|
||||
"kernel/lib/panic.c",
|
||||
"kernel/mboot/mboot.c",
|
||||
"kernel/mboot/mmap.c",
|
||||
"kernel/memory/memory.c",
|
||||
|
|
|
@ -1,7 +1,11 @@
|
|||
#include <comus/drivers.h>
|
||||
#include <comus/drivers/uart.h>
|
||||
#include <comus/drivers/tty.h>
|
||||
#include <comus/drivers/pci.h>
|
||||
|
||||
void drivers_init(void)
|
||||
{
|
||||
uart_init();
|
||||
tty_init();
|
||||
pci_init();
|
||||
}
|
||||
|
|
|
@ -137,6 +137,8 @@ void pci_init(void)
|
|||
printf("PCI DEVICES\n");
|
||||
for (size_t i = 0; i < pci_table_next; i++) {
|
||||
print_device(&pci_table[i]);
|
||||
print_device(&pci_table[i]);
|
||||
print_device(&pci_table[i]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
|
105
kernel/drivers/tty.c
Normal file
105
kernel/drivers/tty.c
Normal file
|
@ -0,0 +1,105 @@
|
|||
#include <lib.h>
|
||||
#include <comus/drivers/tty.h>
|
||||
#include <comus/asm.h>
|
||||
#include <comus/memory.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#define VGA_ADDR 0xB8000
|
||||
|
||||
static const uint8_t width = 80;
|
||||
static const uint8_t height = 25;
|
||||
static volatile uint16_t *buffer = NULL;
|
||||
|
||||
// position
|
||||
static uint32_t x, y;
|
||||
|
||||
// color
|
||||
static uint8_t fg, bg;
|
||||
|
||||
// blank color
|
||||
const uint16_t blank = (uint16_t) 0 | 0 << 12 | 15 << 8;
|
||||
|
||||
static void term_clear_line(int line) {
|
||||
if (line < 0 || line >= height)
|
||||
return;
|
||||
for (uint8_t x = 0; x < width; x++) {
|
||||
const size_t index = line * width + x;
|
||||
buffer[index] = blank;
|
||||
}
|
||||
}
|
||||
|
||||
static void term_clear(void) {
|
||||
for (uint8_t y = 0; y < height; y++)
|
||||
term_clear_line(y);
|
||||
}
|
||||
|
||||
static void term_scroll(int lines) {
|
||||
cli();
|
||||
y -= lines;
|
||||
if (!lines) return;
|
||||
if (lines >= height || lines <= -height) {
|
||||
term_clear();
|
||||
} else if (lines > 0) {
|
||||
memmovev(buffer, buffer + lines * width, 2 * (height - lines) * width);
|
||||
term_clear_line(height - lines);
|
||||
} else {
|
||||
memmovev(buffer + lines * width, buffer + lines, (height + lines) * width);
|
||||
}
|
||||
sti();
|
||||
}
|
||||
|
||||
void tty_init(void)
|
||||
{
|
||||
buffer = mapaddr((void*)VGA_ADDR, width * height * sizeof(uint16_t));
|
||||
x = 0;
|
||||
y = 0;
|
||||
fg = 15;
|
||||
bg = 0;
|
||||
}
|
||||
|
||||
void tty_out(char c)
|
||||
{
|
||||
if (buffer == NULL)
|
||||
return;
|
||||
|
||||
// handle special chars
|
||||
switch (c) {
|
||||
case '\n':
|
||||
x = 0;
|
||||
y++;
|
||||
break;
|
||||
case '\t':
|
||||
x += 4;
|
||||
break;
|
||||
case '\v':
|
||||
case '\f':
|
||||
y++;
|
||||
break;
|
||||
case '\r':
|
||||
x = 0;
|
||||
break;
|
||||
default: {
|
||||
const size_t index = y * width + x;
|
||||
buffer[index] = c | bg << 12 | fg << 8;
|
||||
x++;
|
||||
}
|
||||
}
|
||||
|
||||
// wrap cursor if needed
|
||||
if (x >= width) {
|
||||
x = 0;
|
||||
y++;
|
||||
}
|
||||
if (y >= height) {
|
||||
term_scroll(y - (height - 1));
|
||||
y = height - 1;
|
||||
}
|
||||
|
||||
// set cursor position on screen
|
||||
const uint16_t pos = y * width + x;
|
||||
outb(0x3D4, 0x0F);
|
||||
outb(0x3D5, (uint8_t) (pos & 0xFF));
|
||||
outb(0x3D4, 0x0E);
|
||||
outb(0x3D5, (uint8_t) ((pos >> 8) & 0xFF));
|
||||
}
|
43
kernel/drivers/uart.c
Normal file
43
kernel/drivers/uart.c
Normal file
|
@ -0,0 +1,43 @@
|
|||
#include <comus/drivers/uart.h>
|
||||
#include <comus/asm.h>
|
||||
|
||||
#define PORT 0x3F8
|
||||
|
||||
int uart_init(void) {
|
||||
outb(PORT + 1, 0x00); // disable interrupts
|
||||
outb(PORT + 3, 0x80); // enable DLAB (divisor latch access bit)
|
||||
outb(PORT + 0, 0x03); // (lo) Set baud divisor to 3 38400 baud
|
||||
outb(PORT + 1, 0x00); // (hi)
|
||||
outb(PORT + 3, 0x03); // disable DLAB, set 8 bits per word, one stop bit, no parity
|
||||
outb(PORT + 2, 0xC7); // enable and clear FIFOs, set to maximum threshold
|
||||
outb(PORT + 4, 0x0B); // ???
|
||||
outb(PORT + 4, 0x1E); // set in loopback mode for test
|
||||
outb(PORT + 0, 0xAE); // test by sending 0xAE
|
||||
|
||||
uint8_t response = inb(PORT + 0);
|
||||
if (response != 0xAE) {
|
||||
// TODO: panic here?
|
||||
return -1;
|
||||
}
|
||||
|
||||
// disable loopback, enable IRQs
|
||||
outb(PORT + 4, 0x0F);
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8_t uart_in(void) {
|
||||
// wait for data to be available
|
||||
while ((inb(PORT + 5) & 0x01) == 0);
|
||||
return inb(PORT);
|
||||
}
|
||||
|
||||
void uart_out(uint8_t ch) {
|
||||
// wait for output to be free
|
||||
while ((inb(PORT + 5) & 0x20) == 0);
|
||||
outb(PORT, ch);
|
||||
}
|
||||
|
||||
void uart_out_str(const char *str) {
|
||||
while (*str)
|
||||
uart_out(*str++);
|
||||
}
|
27
kernel/include/comus/drivers/tty.h
Normal file
27
kernel/include/comus/drivers/tty.h
Normal file
|
@ -0,0 +1,27 @@
|
|||
/**
|
||||
* @file tty.h
|
||||
*
|
||||
* @author Freya Murphy <freya@freyacat.org>
|
||||
*
|
||||
* Terminal output
|
||||
*/
|
||||
|
||||
#ifndef TTY_H_
|
||||
#define TTY_H_
|
||||
|
||||
/**
|
||||
* Initalize the terminal
|
||||
*/
|
||||
void tty_init(void);
|
||||
|
||||
/**
|
||||
* Output a character to the terminal
|
||||
*/
|
||||
void tty_out(char c);
|
||||
|
||||
/**
|
||||
* Output a string to the terminal
|
||||
*/
|
||||
void tty_out_str(char *str);
|
||||
|
||||
#endif /* tty.h */
|
19
kernel/include/comus/drivers/uart.h
Normal file
19
kernel/include/comus/drivers/uart.h
Normal file
|
@ -0,0 +1,19 @@
|
|||
/**
|
||||
* @file uart.h
|
||||
*
|
||||
* @author Freya Murphy <freya@freyacat.org>
|
||||
*
|
||||
* Serial interface
|
||||
*/
|
||||
|
||||
#ifndef UART_H_
|
||||
#define UART_H_
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
int uart_init(void);
|
||||
uint8_t uart_in(void);
|
||||
void uart_out(uint8_t ch);
|
||||
void uart_out_str(const char *str);
|
||||
|
||||
#endif /* uart.h */
|
|
@ -1,19 +0,0 @@
|
|||
#include <lib.h>
|
||||
#include <stdio.h>
|
||||
#include <comus/asm.h>
|
||||
|
||||
#define PORT 0x3F8
|
||||
static void serial_out(uint8_t ch)
|
||||
{
|
||||
// wait for output to be free
|
||||
while ((inb(PORT + 5) & 0x20) == 0)
|
||||
;
|
||||
outb(PORT, ch);
|
||||
}
|
||||
|
||||
void fputc(FILE *stream, char c)
|
||||
{
|
||||
(void)stream;
|
||||
|
||||
serial_out(c);
|
||||
}
|
11
kernel/lib/fputc.c
Normal file
11
kernel/lib/fputc.c
Normal file
|
@ -0,0 +1,11 @@
|
|||
#include <lib.h>
|
||||
#include <comus/drivers/tty.h>
|
||||
#include <comus/drivers/uart.h>
|
||||
|
||||
void fputc(FILE *stream, char c)
|
||||
{
|
||||
(void)stream; // TODO: manage stream
|
||||
uart_out(c);
|
||||
tty_out(c);
|
||||
}
|
||||
|
Loading…
Add table
Reference in a new issue