126 lines
3.2 KiB
C
126 lines
3.2 KiB
C
#include "type.h"
|
||
#include "const.h"
|
||
#include "protect.h"
|
||
#include "string.h"
|
||
#include "proc.h"
|
||
#include "tty.h"
|
||
#include "console.h"
|
||
#include "global.h"
|
||
#include "proto.h"
|
||
#include "keyboard.h"
|
||
#include "x86.h"
|
||
#include "memman.h"
|
||
#include "stdio.h"
|
||
#include "serialport.h"
|
||
|
||
|
||
int current_console; // 当前显示在屏幕上的console
|
||
int tty_ok = 0;
|
||
void tty_write(NTTY *tty, char *buf, int len);
|
||
int tty_read(NTTY *tty, char *buf, int len);
|
||
|
||
NTTY* cur_ntty;
|
||
NTTY* ntty_table[NR_CONSOLES];
|
||
|
||
static void write_default_tty(char ch) {
|
||
cur_ntty->write(cur_ntty, ch);
|
||
}
|
||
|
||
inline NTTY* get_tty(const int nr_tty) {
|
||
return ntty_table[nr_tty];
|
||
}
|
||
|
||
void init_tty_main()
|
||
{
|
||
NTTY *tty;
|
||
for (int i = 0; i < 2; ++i)
|
||
{
|
||
// tty = &ntty_table[i];
|
||
tty = (NTTY*)K_PHY2LIN(do_kmalloc(sizeof(NTTY)));
|
||
tty->driver_type = 1; // vga
|
||
// tty->input_buf = (void*)do_kmalloc(sizeof(keyboard_buf));
|
||
// tty->output_buf = (void*)do_kmalloc(sizeof(vga_buf));
|
||
tty->input_buf = (keyboard_buf*)K_PHY2LIN(do_kmalloc(sizeof(keyboard_buf)));
|
||
tty->output_buf = (vga_buf*)K_PHY2LIN(do_kmalloc(sizeof(vga_buf)));
|
||
vga_tty_init(tty);
|
||
ps2_tty_init(tty);
|
||
tty->write = vga_tty_write;
|
||
tty->read = ps2_tty_read;
|
||
tty->recvbuf = ps2_tty_recvbuf;
|
||
ntty_table[i] = tty;
|
||
}
|
||
for (int i = 2; i < 3; ++ i) {
|
||
tty = (NTTY*)K_PHY2LIN(do_kmalloc(sizeof(NTTY)));
|
||
tty->driver_type = 2;
|
||
tty->input_buf = (serial_buf*)K_PHY2LIN(do_kmalloc(sizeof(serial_buf)));
|
||
tty->output_buf = NULL;
|
||
serial_tty_init_i(tty);
|
||
serial_tty_init_o(tty);
|
||
tty->write = serial_tty_write;
|
||
tty->read = serial_tty_read;
|
||
tty->recvbuf = serial_tty_recvbuf;
|
||
ntty_table[2] = tty;
|
||
}
|
||
cur_ntty = ntty_table[0];
|
||
tty_ok = 1;
|
||
simpleconsole_transfer(write_default_tty);
|
||
kprintf("TTY initialized\n");
|
||
}
|
||
|
||
|
||
void task_tty()
|
||
{
|
||
// NTTY* p_tty;
|
||
// for (p_tty = ntty_table; p_tty < ntty_table + 3; ++ p_tty) {
|
||
// init_ntty(p_tty);
|
||
// }
|
||
volatile char serial_input;
|
||
while (1)
|
||
{
|
||
yield();
|
||
// vga_tty_flush(cur_ntty);
|
||
// serial_input = read_serial();
|
||
// vga_tty_write(get_tty(2), serial_input);
|
||
// kprintf("%c", serial_input);
|
||
}
|
||
|
||
}
|
||
|
||
|
||
/*****************************************************************************
|
||
* tty_write
|
||
****************************************************************************
|
||
|
||
* 当fd=STD_OUT时,write()系统调用转发到此函数
|
||
*****************************************************************************/
|
||
void tty_write(NTTY *tty, char *buf, int len)
|
||
{
|
||
if (!tty_ok) {
|
||
simpleconsole_write(buf, len);
|
||
// while (--len >= 0) write_serial(*buf++);
|
||
return;
|
||
}
|
||
else if (tty->driver_type == 1) {
|
||
while (--len >= 0)
|
||
{
|
||
vga_tty_write(tty, *buf++);
|
||
}
|
||
if (cur_ntty == tty) vga_tty_flush(tty);
|
||
}
|
||
else if (tty->driver_type == 2) {
|
||
while(--len >= 0) {
|
||
serial_tty_write(tty, *buf++);
|
||
}
|
||
}
|
||
}
|
||
|
||
/*****************************************************************************
|
||
* tty_read
|
||
****************************************************************************
|
||
|
||
* 当fd=STD_IN时,read()系统调用转发到此函数
|
||
*****************************************************************************/
|
||
int tty_read(NTTY *tty, char *buf, int len)
|
||
{
|
||
return tty->read(tty, buf, len);
|
||
} |