add console

This commit is contained in:
Michael Shaw 2023-03-15 03:33:03 -04:00
commit 3215605b2d
5 changed files with 254 additions and 0 deletions

79
dev/console.c Normal file
View file

@ -0,0 +1,79 @@
#include <kernel/common.h>
#include <kernel/pio.h>
#include <dev/console.h>
static struct {
int rows;
int cols;
int cursorpos;
char line[512];
int resizemode;
char resize_rows[8];
char resize_cols[8];
int l_resize_rows;
int l_resize_cols;
} console;
void console_init()
{
memset(console.line, 0, 512);
console.resizemode = false;
}
bool console_ready()
{
return (console.resizemode == 0);
}
void console_resize()
{
console.resizemode = 1;
memset(console.resize_rows, 0, 8);
memset(console.resize_cols, 0, 8);
console.l_resize_rows = 0;
console.l_resize_cols = 0;
printf("\e[s"); /* save cursor position */
printf("\e[999;999H"); /* move cursor FAR away */
printf("\e[6n"); /* cursor position request */
printf("\e[u"); /* restore cursor position */
memset(console.line, 0, 512);
}
void console_input(char c)
{
if (!console.resizemode) {
printf("%c", c);
} else {
if (c == 'R') {
/* detect end of escape sequence */
printf(LOG_SUCCESS "Console resize: Rows=%s, Cols=%s\n", console.resize_rows, console.resize_cols);
console.resizemode = 0;
return;
} else if (c == ';') {
/* switch to cols when semicolon detected */
console.resizemode += 1;
return;
} else if (c == '\e' || c == '[') {
/* ignore the escape sequence */
return;
}
char *out = console.resize_rows;
int *l = &console.l_resize_rows;
if (console.resizemode == 2) {
out = console.resize_cols;
l = &console.l_resize_cols;
}
out[*l] = c;
*l = (*l + 1);
}
}
void console_write(char c)
{
console.cursorpos += 1;
}

77
dev/isr.S Normal file
View file

@ -0,0 +1,77 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#define __ASM__
#include <dev/serial.h>
.macro exception nr
.global _exception_\nr
_exception_\nr:
movq $\nr, %rdi
movq $0, %rsi
call exception
iretq
.endm
.macro exception_err nr
.global _exception_\nr
_exception_\nr:
popq %rsi
movq $\nr, %rdi
call exception
iretq
.endm
.global isr_serial_input
isr_serial_input:
call serial_trap
movb $4, %dil
call pic_eoi
iretq
.global isr_sched
isr_sched:
call sched_test
call pic_eoi
iretq
.global sti
sti:
sti
ret
.global cli
cli:
cli
ret
exception 0x00
exception 0x01
exception 0x02
exception 0x03
exception 0x04
exception 0x05
exception 0x06
exception 0x07
exception_err 0x08
exception 0x09
exception_err 0x0A
exception_err 0x0B
exception_err 0x0C
exception_err 0x0D
exception_err 0x0E
exception 0x0F
exception 0x10
exception_err 0x11
exception 0x12
exception 0x13
exception 0x14
exception_err 0x15
exception 0x16
exception 0x17
exception 0x18
exception 0x19
exception 0x1A
exception 0x1B
exception 0x1C
exception_err 0x1D
exception_err 0x1E
exception 0x1F

67
dev/pic.c Normal file
View file

@ -0,0 +1,67 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#include <kernel/common.h>
#include <kernel/pio.h>
#include <dev/pic.h>
void pic_mask(uint8_t irq, uint8_t i)
{
uint16_t port;
uint8_t val;
i = !(!i);
if (irq < 8) {
port = PIC1_DATA;
} else {
port = PIC2_DATA;
irq -= 8;
}
val = inb(port) & ~(1 << irq);
val |= (i << irq);
outb(port, val);
}
void pic_eoi(uint8_t irq)
{
if (irq > 8)
outb(PIC2_CMD, 0x20);
outb(PIC1_CMD, 0x20);
}
void exception(int vector, int error)
{
printf(LOG_WARN "EXCEPTION: %x, error code: %d\n", vector, error);
}
void pic_init()
{
uint8_t mask1 = inb(PIC1_CMD + 1);
uint8_t mask2 = inb(PIC2_CMD + 1);
outb(PIC1_CMD, 0x11);
iowait();
outb(PIC2_CMD, 0x11);
iowait();
outb(PIC1_DATA, 0x20);
iowait();
outb(PIC2_DATA, 0x28);
iowait();
outb(PIC1_DATA, 4);
iowait();
outb(PIC2_DATA, 2);
iowait();
outb(PIC1_DATA, 0x01);
iowait();
outb(PIC2_DATA, 0x01);
iowait();
outb(PIC1_DATA, mask1);
outb(PIC2_DATA, mask2);
pic_eoi(15);
printf(LOG_SUCCESS "8259 PIC initialized\n");
}

12
include/dev/console.h Normal file
View file

@ -0,0 +1,12 @@
#ifndef _CONSOLE_H_
#define _CONSOLE_H_
#ifndef __ASM__
void console_init();
void console_resize();
void console_input(char c);
bool console_ready();
#endif /* __ASM__ */
#endif /* _CONSOLE_H_ */

19
include/dev/pic.h Normal file
View file

@ -0,0 +1,19 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#ifndef _PIC_H_
#define _PIC_H_
#include <kernel/common.h>
#define PIC1_CMD 0x20
#define PIC1_DATA 0x21
#define PIC2_CMD 0xA0
#define PIC2_DATA 0xA1
#ifndef __ASM__
void pic_mask(uint8_t irq, uint8_t i);
void pic_eoi(uint8_t irq);
void pic_init();
#endif /* __ASM__ */
#endif /* _PIC_H_ */