Merge branch 'master' into dev

This commit is contained in:
xiaoxiao 2023-01-13 20:24:21 +08:00
commit 306a3a1eac
11 changed files with 580 additions and 397 deletions

View File

@ -40,6 +40,7 @@ void simpleconsole_setcur(int row, int col);
#define SCR_BUFSIZE (2 * SCR_SIZE)
#define SCR_MAXLINE (SCR_BUFSIZE / SCR_WIDTH)
#define FLASH_CHAR 0x8000
#define DEFAULT_CHAR_COLOR ((MAKE_COLOR(BLACK, WHITE | BRIGHT)) << 8)
#define GRAY_CHAR (MAKE_COLOR(BLACK, BLACK) | BRIGHT)
#define RED_CHAR (MAKE_COLOR(BLUE, RED) | BRIGHT)

View File

@ -166,14 +166,16 @@
/* VGA */
// added by mingxuan 2019-5-19
#define CRTC_ADDR_REG 0x3D4 /* CRT Controller Registers - Addr Register */
#define CRTC_DATA_REG 0x3D5 /* CRT Controller Registers - Data Register */
#define START_ADDR_H 0xC /* reg index of video mem start addr (MSB) */
#define START_ADDR_L 0xD /* reg index of video mem start addr (LSB) */
#define CURSOR_H 0xE /* reg index of cursor position (MSB) */
#define CURSOR_L 0xF /* reg index of cursor position (LSB) */
#define V_MEM_BASE 0xB8000 /* base of color video memory */
#define V_MEM_SIZE 0x8000 /* 32K: B8000H -> BFFFFH */
#define CRTC_ADDR_REG 0x3D4 /* CRT Controller Registers - Addr Register */
#define CRTC_DATA_REG 0x3D5 /* CRT Controller Registers - Data Register */
#define START_ADDR_H 0xC /* reg index of video mem start addr (MSB) */
#define START_ADDR_L 0xD /* reg index of video mem start addr (LSB) */
#define CURSOR_H 0xE /* reg index of cursor position (MSB) */
#define CURSOR_L 0xF /* reg index of cursor position (LSB) */
#define UNDERLINE_REG 0x14 /* reg index of underline */
#define ModeControl_Reg 0x10 /* reg index of Attribute Mode Control Register */
#define V_MEM_BASE 0xB8000 /* base of color video memory */
#define V_MEM_SIZE 0x8000 /* 32K: B8000H -> BFFFFH */
#define STD_IN 0
#define STD_OUT 1

21
include/csi.h Normal file
View File

@ -0,0 +1,21 @@
#ifndef FS_H
#define FS_H
void enable_blink();
void disable_blink();
void set_underline(u32 addr);
void enable_blink();
void csi_scroll(vga_buf *vgabuf, i16 scroll_num);
void cursor_locate(i16 row, i16 col, vga_buf *vgabuf);
void cursor_move(i16 move_row, i16 move_col, vga_buf *vgabuf);
void clear_screen(vga_buf *vgabuf, i16 src_row, i16 src_col, i16 dst_row, i16 dst_col);
void set_color(vga_buf *vgabuf);
void CSI_Erase_handler(vga_buf *vgabuf, int n);
void CSI_handler(u8 terminator, vga_buf *vgabuf);
#endif /* FS_H */

View File

@ -57,10 +57,15 @@ typedef struct vga_buf
int cur_col; // cursor position, on screen
bool Is2param;
bool IsColorReverse;
bool IsHide;
enum CSI_state CSI;
i16 param1; // the first param of CSI
i16 param2; // the second of CSI
u16 color;
u16 last_color; // 为了某些功能关闭后,恢复原属性
} vga_buf;
typedef struct keyboard_buf

View File

@ -32,6 +32,7 @@ KERN_SRCFILES :=kernel/kernel.asm \
kernel/protect.c \
kernel/serialport.c \
kernel/vga.c \
kernel/csi.c \
lib/klib.c \

495
kernel/csi.c Normal file
View File

@ -0,0 +1,495 @@
#include "tty.h"
#include "x86.h"
#include "console.h"
#include "const.h"
#include "assert.h"
#define INDEX(row, col) ((row)*SCR_WIDTH + (col))
#define NEXTLINE(row) NEXT(row, SCR_MAXLINE)
#define LASTLINE(row) LAST(row, SCR_MAXLINE)
#define ADDLINE(row, add_num) (((row) + add_num) % SCR_MAXLINE)
void enable_blink()
{
disable_int();
inb(0x3DA);
outb(0x3C0, 0x30);
u8 temp = inb(0x3C1);
outb(0x3C0, temp | 0x08);
enable_int();
}
void disable_blink()
{
disable_int();
inb(0x3DA);
outb(0x3C0, 0x30);
u8 temp = inb(0x3C1);
outb(0x3C0, temp & 0xF7);
enable_int();
}
void set_underline(u32 addr)
{
disable_int();
inb(0x3DA);
outb(0x3C0, 0x30);
u8 temp = inb(0x3C1);
outb(0x3C0, temp | 0x01);
outb(CRTC_ADDR_REG, UNDERLINE_REG);
outb(CRTC_DATA_REG, addr & 0xFF);
enable_int();
}
// called by csi
void csi_scroll(vga_buf *vgabuf, i16 scroll_num)
{
while (scroll_num > 0) // down
{
scroll_num--;
for (int i = SCR_HEIGHT - 1; i > 0; i--)
{
int line_dst = ADDLINE(vgabuf->scr_top_line, i);
int line_src = ADDLINE(vgabuf->scr_top_line, i - 1);
u32 *ptr_buf_dst = (u32 *)(vgabuf->buf + sizeof(u16) * line_dst * SCR_WIDTH);
u32 *ptr_buf_src = (u32 *)(vgabuf->buf + sizeof(u16) * line_src * SCR_WIDTH);
for (int p = 0; p < SCR_WIDTH * sizeof(u16) / sizeof(u32); ++p)
{
*ptr_buf_dst++ = *ptr_buf_src++;
}
}
u32 *ptr_buf_start = (u32 *)(vgabuf->buf + sizeof(u16) * (vgabuf->scr_top_line) * SCR_WIDTH);
for (int i = 0; i < SCR_WIDTH * sizeof(u16) / sizeof(u32); ++i)
{
*ptr_buf_start++ = (BLANK << 16) | BLANK;
}
}
while (scroll_num < 0) // up
{
scroll_num++;
for (int i = 0; i < SCR_HEIGHT - 1; i++)
{
int line_dst = ADDLINE(vgabuf->scr_top_line, i);
int line_src = ADDLINE(vgabuf->scr_top_line, i + 1);
u32 *ptr_buf_dst = (u32 *)(vgabuf->buf + sizeof(u16) * line_dst * SCR_WIDTH);
u32 *ptr_buf_src = (u32 *)(vgabuf->buf + sizeof(u16) * line_src * SCR_WIDTH);
for (int p = 0; p < SCR_WIDTH * sizeof(u16) / sizeof(u32); ++p)
{
*ptr_buf_dst++ = *ptr_buf_src++;
}
}
int line_end = ADDLINE(vgabuf->scr_top_line, SCR_HEIGHT - 1);
u32 *ptr_buf_end = (u32 *)(vgabuf->buf + sizeof(u16) * line_end * SCR_WIDTH);
for (int i = 0; i < SCR_WIDTH * sizeof(u16) / sizeof(u32); ++i)
{
*ptr_buf_end++ = (BLANK << 16) | BLANK; // bg-black, fg-white, ascii-space
}
}
}
void cursor_locate(i16 row, i16 col, vga_buf *vgabuf)
{
vgabuf->scr_cur_line = vgabuf->scr_top_line;
while (row > 0 && row < SCR_HEIGHT)
{
row--;
vgabuf->scr_cur_line = NEXTLINE(vgabuf->scr_cur_line);
if (vgabuf->scr_cur_line == vgabuf->scr_bot_line)
break;
}
if (col >= 0 && col <= SCR_WIDTH - 1)
{
vgabuf->cur_col = col;
}
}
void cursor_move(i16 move_row, i16 move_col, vga_buf *vgabuf)
{
// kprintf("%d,%d", move_row, move_col);
while (move_row > 0) // down
{
move_row--;
if (vgabuf->scr_cur_line == vgabuf->scr_bot_line)
break;
vgabuf->scr_cur_line = NEXTLINE(vgabuf->scr_cur_line);
vgabuf->cur_row = CYCLE_SUB(vgabuf->scr_top_line, vgabuf->scr_cur_line, SCR_MAXLINE);
if (vgabuf->cur_row == SCR_HEIGHT)
{
// auto scroll
vgabuf->scr_top_line = NEXTLINE(vgabuf->scr_top_line);
}
}
while (move_row < 0) // up
{
move_row++;
vgabuf->scr_cur_line = LASTLINE(vgabuf->scr_cur_line);
if (vgabuf->scr_cur_line == vgabuf->scr_top_line)
{
if (vgabuf->scr_top_line == vgabuf->head_line)
break;
// vgabuf->scr_cur_line = LASTLINE(vgabuf->scr_cur_line);
vgabuf->scr_top_line = LASTLINE(vgabuf->scr_top_line);
vgabuf->scr_bot_line = LASTLINE(vgabuf->scr_bot_line);
}
}
vgabuf->cur_row = CYCLE_SUB(vgabuf->scr_top_line, vgabuf->scr_cur_line, SCR_MAXLINE);
vgabuf->cur_col += move_col;
if (vgabuf->cur_col < 0)
vgabuf->cur_col = 0;
else if (vgabuf->cur_col >= SCR_WIDTH)
vgabuf->cur_col = SCR_WIDTH - 1;
}
void clear_screen(vga_buf *vgabuf, i16 src_row, i16 src_col, i16 dst_row, i16 dst_col)
{
u16 *buf = vgabuf->buf;
int src_index = INDEX(src_row, src_col);
int dst_index = INDEX(dst_row, dst_col);
if (src_index <= dst_index)
for (int i = src_index; i <= dst_index; i++)
{
buf[i] = BLANK;
}
else // scr>dst
{
for (int i = 0; i <= dst_index; i++)
buf[i] = BLANK;
int index_end = INDEX(SCR_MAXLINE - 1, SCR_WIDTH - 1);
for (int i = 0; i <= index_end; i++)
buf[i] = BLANK;
}
}
inline static void
param12vga_color(i16 *param)
{
u8 tmp = *param & 1;
*param &= 0b0110;
*param |= *param >> 2;
*param &= 0b0011;
*param |= tmp << 2;
}
inline static void
enable_reverse(vga_buf *vgabuf)
{
vgabuf->last_color = vgabuf->color;
vgabuf->color = ((vgabuf->color & 0xf000) >> 4) | ((vgabuf->color & 0x0f00) << 4);
vgabuf->IsColorReverse = true;
}
inline static void
disable_reverse(vga_buf *vgabuf)
{
if (vgabuf->IsColorReverse == true)
{
vgabuf->color = vgabuf->last_color;
vgabuf->IsColorReverse = false;
}
}
inline static void
enable_hide(vga_buf *vgabuf)
{
vgabuf->last_color = vgabuf->color;
u16 back_clolor = ((vgabuf->color & 0xF000) >> 4);
vgabuf->color &= 0xf000;
vgabuf->color |= back_clolor;
vgabuf->IsHide = true;
}
inline static void
disable_hide(vga_buf *vgabuf)
{
if (vgabuf->IsHide == true)
{
vgabuf->color = vgabuf->last_color;
vgabuf->IsHide = false;
}
}
void set_color(vga_buf *vgabuf)
{
if (vgabuf->param1 == 0)
{
vgabuf->color = DEFAULT_CHAR_COLOR;
disable_reverse(vgabuf);
disable_hide(vgabuf);
}
// else if (vgabuf->param1 == 5 || vgabuf->param1 == 6)
// {
// enable_blink();
// vgabuf->color |= FLASH_CHAR;
// }
else if (vgabuf->param1 == 7)
{
enable_reverse(vgabuf);
}
else if (vgabuf->param1 == 8)
{
enable_hide(vgabuf);
}
else if (vgabuf->param1 == 25)
{
disable_blink();
vgabuf->color &= (~FLASH_CHAR);
}
else if (vgabuf->param1 == 27)
{
disable_reverse(vgabuf);
}
else if (vgabuf->param1 == 28)
{
disable_hide(vgabuf);
}
else if (30 <= vgabuf->param1 && vgabuf->param1 <= 37)
{
vgabuf->param1 -= 30;
param12vga_color(&(vgabuf->param1));
vgabuf->color = (vgabuf->color & 0xf8ff) | FOREGROUND(vgabuf->param1);
}
else if (vgabuf->param1 == 39)
{
u16 default_foreground = DEFAULT_CHAR_COLOR & 0x0f00;
vgabuf->color = (vgabuf->color & 0xf0ff) | default_foreground;
}
else if (40 <= vgabuf->param1 && vgabuf->param1 <= 47)
{
vgabuf->param1 -= 40;
param12vga_color(&(vgabuf->param1));
vgabuf->color = (vgabuf->color & 0x8fff) | BACKGROUND(vgabuf->param1);
}
else if (vgabuf->param1 == 49)
{
u16 default_background = DEFAULT_CHAR_COLOR & 0xf000;
vgabuf->color = (vgabuf->color & 0x0fff) | default_background;
}
else if (90 <= vgabuf->param1 && vgabuf->param1 <= 97)
{
vgabuf->param1 -= 90;
param12vga_color(&(vgabuf->param1));
vgabuf->param1 |= 0x8;
vgabuf->color = (vgabuf->color & 0xf0ff) | FOREGROUND(vgabuf->param1);
}
else if (100 <= vgabuf->param1 && vgabuf->param1 <= 107)
{
vgabuf->param1 -= 100;
param12vga_color(&(vgabuf->param1));
vgabuf->param1 |= 0x8;
vgabuf->color = (vgabuf->color & 0x0fff) | BACKGROUND(vgabuf->param1);
}
else
{
warn("unsupport CSI: color");
}
if (vgabuf->Is2param == true)
{
if (vgabuf->param2 == 0)
{
vgabuf->color = DEFAULT_CHAR_COLOR;
disable_reverse(vgabuf);
disable_hide(vgabuf);
}
// else if (vgabuf->param2 == 5 || vgabuf->param2 == 6)
// {
// enable_blink();
// vgabuf->color |= FLASH_CHAR;
// }
else if (vgabuf->param2 == 7)
{
enable_reverse(vgabuf);
}
else if (vgabuf->param2 == 8)
{
enable_hide(vgabuf);
}
else if (vgabuf->param2 == 25)
{
disable_blink();
vgabuf->color &= (~FLASH_CHAR);
}
else if (vgabuf->param2 == 27)
{
disable_reverse(vgabuf);
}
else if (vgabuf->param2 == 28)
{
disable_hide(vgabuf);
}
else if (30 <= vgabuf->param2 && vgabuf->param2 <= 37)
{
vgabuf->param2 -= 30;
param12vga_color(&(vgabuf->param2));
vgabuf->color = (vgabuf->color & 0xf8ff) | FOREGROUND(vgabuf->param2);
}
else if (vgabuf->param2 == 39)
{
u16 default_foreground = DEFAULT_CHAR_COLOR & 0x0f00;
vgabuf->color = (vgabuf->color & 0xf0ff) | default_foreground;
}
else if (40 <= vgabuf->param2 && vgabuf->param2 <= 47)
{
vgabuf->param2 -= 40;
param12vga_color(&(vgabuf->param2));
vgabuf->color = (vgabuf->color & 0x8fff) | BACKGROUND(vgabuf->param2);
}
else if (vgabuf->param2 == 49)
{
u16 default_background = DEFAULT_CHAR_COLOR & 0xf000;
vgabuf->color = (vgabuf->color & 0x0fff) | default_background;
}
else if (90 <= vgabuf->param2 && vgabuf->param2 <= 97)
{
vgabuf->param2 -= 90;
param12vga_color(&(vgabuf->param2));
vgabuf->param2 |= 0x8;
vgabuf->color = (vgabuf->color & 0xf0ff) | FOREGROUND(vgabuf->param2);
}
else if (100 <= vgabuf->param2 && vgabuf->param2 <= 107)
{
vgabuf->param2 -= 100;
param12vga_color(&(vgabuf->param2));
vgabuf->param2 |= 0x8;
vgabuf->color = (vgabuf->color & 0x0fff) | BACKGROUND(vgabuf->param2);
}
else
{
warn("unsupport CSI: color");
}
}
}
void CSI_Erase_handler(vga_buf *vgabuf, int n)
{
if (n == 2)
{
vgabuf->scr_bot_line = ADDLINE(vgabuf->scr_top_line, SCR_HEIGHT - 1);
for (int i = 0; i < SCR_HEIGHT; i++)
{
vgabuf->scr_cur_line = NEXTLINE(vgabuf->scr_cur_line);
vgabuf->scr_bot_line = NEXTLINE(vgabuf->scr_bot_line);
// kprintf("af %x\n", vgabuf->scr_cur_line);
vgabuf->cur_row = CYCLE_SUB(vgabuf->scr_top_line, vgabuf->scr_bot_line, SCR_MAXLINE);
if (vgabuf->cur_row == SCR_HEIGHT)
{
// auto scroll
vgabuf->scr_top_line = NEXTLINE(vgabuf->scr_top_line);
}
if (vgabuf->scr_bot_line == vgabuf->head_line)
{
vgabuf->head_line = NEXTLINE(vgabuf->head_line);
// remember to fill blank the old line
u32 *ptr_buf = (u32 *)(vgabuf->buf + sizeof(u16) * vgabuf->scr_bot_line * SCR_WIDTH);
for (int i = 0; i < SCR_WIDTH * sizeof(u16) / sizeof(u32); ++i)
{
*ptr_buf++ = (BLANK << 16) | BLANK; // bg-black, fg-white, ascii-space
}
}
}
}
if (n == 3)
{
int line = vgabuf->head_line;
while (line != vgabuf->scr_top_line)
{
u32 *ptr_buf = (u32 *)(vgabuf->buf + sizeof(u16) * line * SCR_WIDTH);
for (int i = 0; i < SCR_WIDTH * sizeof(u16) / sizeof(u32); ++i)
{
*ptr_buf++ = (BLANK << 16) | BLANK; // bg-black, fg-white, ascii-space
}
line = NEXTLINE(line);
}
vgabuf->head_line = vgabuf->scr_top_line;
}
}
void CSI_handler(u8 terminator, vga_buf *vgabuf)
{
vgabuf->CSI = CSI_ESC;
switch (terminator)
{
case 'A': // Cursor Up
if (vgabuf->param1 == 0)
vgabuf->param1 == 1;
cursor_move(-vgabuf->param1, 0, vgabuf);
break;
case 'B': // Cursor Down
if (vgabuf->param1 == 0)
vgabuf->param1 == 1;
cursor_move(+vgabuf->param1, 0, vgabuf);
break;
case 'C': // Cursor Forward
if (vgabuf->param1 == 0)
vgabuf->param1 == 1;
cursor_move(0, +vgabuf->param1, vgabuf);
break;
case 'D': // Cursor Back
if (vgabuf->param1 == 0)
vgabuf->param1 == 1;
cursor_move(0, -vgabuf->param1, vgabuf); // nothing
break;
case 'E': // Cursor Next Line
if (vgabuf->param1 == 0)
vgabuf->param1 == 1;
cursor_move(+vgabuf->param1, -vgabuf->cur_col, vgabuf);
break;
case 'F': // Cursor Previous Line
if (vgabuf->param1 == 0)
vgabuf->param1 == 1;
cursor_move(-vgabuf->param1, -vgabuf->cur_col, vgabuf);
break;
case 'G': // Cursor Horizontal Absolute
if (vgabuf->param1 == 0)
vgabuf->param1 == 1;
cursor_locate(CYCLE_SUB(vgabuf->scr_top_line, vgabuf->scr_cur_line, SCR_MAXLINE), vgabuf->param1 - 1, vgabuf);
break;
case 'H': // Cursor Position
case 'f':
if (vgabuf->param1 == 0)
vgabuf->param1 == 1;
if (vgabuf->param2 == 0)
vgabuf->param2 == 1;
cursor_locate(vgabuf->param1 - 1, vgabuf->param2 - 1, vgabuf);
break;
case 'J': // Erase in Display
if (vgabuf->param1 == 0)
// from Cursor Position to the end of the screen
clear_screen(vgabuf, vgabuf->scr_cur_line, vgabuf->cur_col, ADDLINE(vgabuf->scr_top_line, SCR_HEIGHT - 1), SCR_WIDTH - 1);
else if (vgabuf->param1 == 1)
// from Cursor Position to the start of the screen
clear_screen(vgabuf, vgabuf->scr_top_line, 0, vgabuf->scr_cur_line, vgabuf->cur_col);
else if (vgabuf->param1 == 2)
// The whole screen
CSI_Erase_handler(vgabuf, 2);
else if (vgabuf->param1 == 3)
// The whole screen and the buffer
CSI_Erase_handler(vgabuf, 3);
break;
case 'K': // Erase in Line
if (vgabuf->param1 == 0)
// from Cursor Position to the end of the line
clear_screen(vgabuf, vgabuf->scr_cur_line, vgabuf->cur_col, vgabuf->scr_cur_line, SCR_WIDTH - 1);
else if (vgabuf->param1 == 1)
// from Cursor Position to the start of the line
clear_screen(vgabuf, vgabuf->scr_cur_line, 0, vgabuf->scr_cur_line, vgabuf->cur_col);
else if (vgabuf->param1 == 2)
// The whole line
clear_screen(vgabuf, vgabuf->scr_cur_line, 0, vgabuf->scr_cur_line, SCR_WIDTH - 1);
break;
case 'S': // Scroll Up
if (vgabuf->param1 == 0)
vgabuf->param1 = 1;
csi_scroll(vgabuf, -vgabuf->param1);
break;
case 'T': // Scroll Down
if (vgabuf->param1 == 0)
vgabuf->param1 = 1;
csi_scroll(vgabuf, +vgabuf->param1);
break;
case 'm': // Select Graphic Rendition
set_color(vgabuf);
break;
}
}

View File

@ -132,7 +132,8 @@ void initial()
do_vclose(stdout);
do_vclose(stderr);
exec("orange/shell_1.bin");
// exec("orange/shell_1.bin");
exec("orange/test.bin");
while (1)
;

View File

@ -32,7 +32,7 @@ _NR_unlink equ 20 ; //added by xw, 18/6/18
_NR_create equ 21 ; //added by mingxuan 2019-5-17
_NR_delete equ 22 ; //added by mingxuan 2019-5-17
_NR_opendir equ 22 ; //added by mingxuan 2019-5-17
_NR_opendir equ 23 ; //added by mingxuan 2019-5-17
_NR_createdir equ 24 ; //added by mingxuan 2019-5-17
_NR_deletedir equ 25 ; //added by mingxuan 2019-5-17

View File

@ -13,24 +13,26 @@
#include "stdio.h"
#include "serialport.h"
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];
NTTY *cur_ntty;
NTTY *ntty_table[NR_CONSOLES];
static void write_default_tty(char ch) {
static void write_default_tty(char ch)
{
cur_ntty->write(cur_ntty, ch);
}
inline NTTY* get_tty(const int nr_tty) {
if (nr_tty >= NR_CONSOLES || nr_tty < 0) return NULL;
inline NTTY *get_tty(const int nr_tty)
{
if (nr_tty >= NR_CONSOLES || nr_tty < 0)
return NULL;
return ntty_table[nr_tty];
}
static int ps2_vga_ioctl(NTTY* tty, u32 cmd, long arg)
static int ps2_vga_ioctl(NTTY *tty, u32 cmd, long arg)
{
int retval = 0;
switch (cmd)
@ -39,11 +41,14 @@ static int ps2_vga_ioctl(NTTY* tty, u32 cmd, long arg)
vga_tty_scroll(tty, arg);
break;
case IOCTL_CMD_TTY_SELECTCON:
if (get_tty(arg) == NULL || get_tty(arg)->driver_type != 1) retval = -1;
else vga_tty_select(get_tty(arg));
if (get_tty(arg) == NULL || get_tty(arg)->driver_type != 1)
retval = -1;
else
vga_tty_select(get_tty(arg));
break;
case IOCTL_CMD_TTY_FLUSH:
if (tty == cur_ntty) vga_tty_flush(tty);
if (tty == cur_ntty)
vga_tty_flush(tty);
break;
default:
break;
@ -51,7 +56,7 @@ static int ps2_vga_ioctl(NTTY* tty, u32 cmd, long arg)
return retval;
}
static int dummy_ioctl(NTTY* tty, u32 cmd, long arg) {return 0;}
static int dummy_ioctl(NTTY *tty, u32 cmd, long arg) { return 0; }
void init_tty_main()
{
@ -59,12 +64,12 @@ void init_tty_main()
for (int i = 0; i < 2; ++i)
{
// tty = &ntty_table[i];
tty = (NTTY*)K_PHY2LIN(do_kmalloc(sizeof(NTTY)));
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)));
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;
@ -73,10 +78,11 @@ void init_tty_main()
tty->ioctl = ps2_vga_ioctl;
ntty_table[i] = tty;
}
for (int i = 2; i < 3; ++ i) {
tty = (NTTY*)K_PHY2LIN(do_kmalloc(sizeof(NTTY)));
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->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);
@ -92,7 +98,6 @@ void init_tty_main()
kprintf("TTY initialized\n");
}
void task_tty()
{
// NTTY* p_tty;
@ -108,10 +113,8 @@ void task_tty()
// vga_tty_write(get_tty(2), serial_input);
// kprintf("%c", serial_input);
}
}
/*****************************************************************************
* tty_write
****************************************************************************
@ -120,12 +123,14 @@ void task_tty()
*****************************************************************************/
void tty_write(NTTY *tty, char *buf, int len)
{
if (!tty_ok) {
if (!tty_ok)
{
simpleconsole_write(buf, len);
// while (--len >= 0) write_serial(*buf++);
return;
}
else {
else
{
while (--len >= 0)
{
tty->write(tty, *buf++);

View File

@ -12,7 +12,7 @@
#include "x86.h"
#include "memman.h"
#include "assert.h"
#include "csi.h"
/*****************************************************************************
* Low level vga driver
*****************************************************************************/
@ -133,6 +133,10 @@ void vga_tty_init(NTTY *tty)
// buf->max_line = SCR_BUFSIZE / SCR_WIDTH;
vga->scr_top_line = vga->scr_cur_line = vga->scr_bot_line = 0;
vga->head_line = 0;
vga->Is2param = false;
vga->IsColorReverse = false;
vga->IsHide = false;
vga->color = DEFAULT_CHAR_COLOR;
u32 *ptr_buf = (u32 *)vga->buf;
for (int i = 0; i < SCR_BUFSIZE * sizeof(u16) / sizeof(u32); ++i)
{
@ -146,60 +150,11 @@ void vga_tty_init(NTTY *tty)
#define LASTLINE(row) LAST(row, SCR_MAXLINE)
#define ADDLINE(row, add_num) (((row) + add_num) % SCR_MAXLINE)
// called by csi
static void csi_scroll(vga_buf *vgabuf, i16 scroll_num)
{
while (scroll_num > 0) // down
{
scroll_num--;
for (int i = SCR_HEIGHT - 1; i > 0; i--)
{
int line_dst = ADDLINE(vgabuf->scr_top_line, i);
int line_src = ADDLINE(vgabuf->scr_top_line, i - 1);
u32 *ptr_buf_dst = (u32 *)(vgabuf->buf + sizeof(u16) * line_dst * SCR_WIDTH);
u32 *ptr_buf_src = (u32 *)(vgabuf->buf + sizeof(u16) * line_src * SCR_WIDTH);
for (int p = 0; p < SCR_WIDTH * sizeof(u16) / sizeof(u32); ++p)
{
*ptr_buf_dst++ = *ptr_buf_src++;
}
}
u32 *ptr_buf_start = (u32 *)(vgabuf->buf + sizeof(u16) * (vgabuf->scr_top_line) * SCR_WIDTH);
for (int i = 0; i < SCR_WIDTH * sizeof(u16) / sizeof(u32); ++i)
{
*ptr_buf_start++ = (BLANK << 16) | BLANK; // bg-black, fg-white, ascii-space
}
}
while (scroll_num < 0) // up
{
scroll_num++;
for (int i = 0; i < SCR_HEIGHT - 1; i++)
{
int line_dst = ADDLINE(vgabuf->scr_top_line, i);
int line_src = ADDLINE(vgabuf->scr_top_line, i + 1);
u32 *ptr_buf_dst = (u32 *)(vgabuf->buf + sizeof(u16) * line_dst * SCR_WIDTH);
u32 *ptr_buf_src = (u32 *)(vgabuf->buf + sizeof(u16) * line_src * SCR_WIDTH);
for (int p = 0; p < SCR_WIDTH * sizeof(u16) / sizeof(u32); ++p)
{
*ptr_buf_dst++ = *ptr_buf_src++;
}
}
int line_end = ADDLINE(vgabuf->scr_top_line, SCR_HEIGHT - 1);
u32 *ptr_buf_end = (u32 *)(vgabuf->buf + sizeof(u16) * line_end * SCR_WIDTH);
for (int i = 0; i < SCR_WIDTH * sizeof(u16) / sizeof(u32); ++i)
{
*ptr_buf_end++ = (BLANK << 16) | BLANK; // bg-black, fg-white, ascii-space
}
}
}
static void newline(vga_buf *vga)
{
u16 *buf = vga->buf;
vga->cur_col = 0;
// kprintf("bf %x\n", vgabuf->scr_cur_line);
vga->scr_cur_line = NEXTLINE(vga->scr_cur_line);
// kprintf("af %x\n", vgabuf->scr_cur_line);
vga->cur_row = CYCLE_SUB(vga->scr_top_line, vga->scr_cur_line, SCR_MAXLINE);
if (vga->cur_row == SCR_HEIGHT)
{
@ -215,9 +170,6 @@ static void newline(vga_buf *vga)
{
*ptr_buf++ = (BLANK << 16) | BLANK; // bg-black, fg-white, ascii-space
}
// for (int i = 0; i < SCR_WIDTH; ++ i) {
// buf[INDEX(vga->scr_cur_line, i)] = BLANK;
// }
}
}
@ -230,304 +182,6 @@ static void nextcol(vga_buf *vgabuf)
}
}
static void cursor_locate(i16 row, i16 col, vga_buf *vgabuf)
{
vgabuf->scr_cur_line = vgabuf->scr_top_line;
while (row > 0 && row < SCR_HEIGHT)
{
row--;
vgabuf->scr_cur_line = NEXTLINE(vgabuf->scr_cur_line);
if (vgabuf->scr_cur_line == vgabuf->scr_bot_line)
break;
}
if (col >= 0 && col <= SCR_WIDTH - 1)
{
vgabuf->cur_col = col;
}
}
static void cursor_move(i16 move_row, i16 move_col, vga_buf *vgabuf)
{
// kprintf("%d,%d", move_row, move_col);
while (move_row > 0) // down
{
move_row--;
if (vgabuf->scr_cur_line == vgabuf->scr_bot_line)
break;
vgabuf->scr_cur_line = NEXTLINE(vgabuf->scr_cur_line);
vgabuf->cur_row = CYCLE_SUB(vgabuf->scr_top_line, vgabuf->scr_cur_line, SCR_MAXLINE);
if (vgabuf->cur_row == SCR_HEIGHT)
{
// auto scroll
vgabuf->scr_top_line = NEXTLINE(vgabuf->scr_top_line);
}
}
while (move_row < 0) // up
{
move_row++;
vgabuf->scr_cur_line = LASTLINE(vgabuf->scr_cur_line);
if (vgabuf->scr_cur_line == vgabuf->scr_top_line)
{
if (vgabuf->scr_top_line == vgabuf->head_line)
break;
// vgabuf->scr_cur_line = LASTLINE(vgabuf->scr_cur_line);
vgabuf->scr_top_line = LASTLINE(vgabuf->scr_top_line);
vgabuf->scr_bot_line = LASTLINE(vgabuf->scr_bot_line);
}
}
vgabuf->cur_row = CYCLE_SUB(vgabuf->scr_top_line, vgabuf->scr_cur_line, SCR_MAXLINE);
vgabuf->cur_col += move_col;
if (vgabuf->cur_col < 0)
vgabuf->cur_col = 0;
else if (vgabuf->cur_col >= SCR_WIDTH)
vgabuf->cur_col = SCR_WIDTH - 1;
}
inline static void
param12vga_color(i16 *param)
{
u8 tmp = *param & 1;
*param &= 0b0110;
*param |= *param >> 2;
*param &= 0b0011;
*param |= tmp << 2;
}
static void clear_screen(vga_buf *vgabuf, i16 src_row, i16 src_col, i16 dst_row, i16 dst_col)
{
u16 *buf = vgabuf->buf;
int src_index = INDEX(src_row, src_col);
int dst_index = INDEX(dst_row, dst_col);
if (src_index <= dst_index)
for (int i = src_index; i <= dst_index; i++)
{
buf[i] = BLANK;
}
else // scr>dst
{
for (int i = 0; i <= dst_index; i++)
buf[i] = BLANK;
int index_end = INDEX(SCR_MAXLINE - 1, SCR_WIDTH - 1);
for (int i = 0; i <= index_end; i++)
buf[i] = BLANK;
}
}
static void set_color(vga_buf *vgabuf)
{
if (vgabuf->param1 == 0)
{
vgabuf->color = DEFAULT_CHAR_COLOR;
}
else if (vgabuf->param1 == 1)
{
vgabuf->color |= 0x8800;
}
else if (vgabuf->param1 == 2)
{
vgabuf->color &= 0x7700;
}
else if (30 <= vgabuf->param1 && vgabuf->param1 <= 37)
{
vgabuf->param1 -= 30;
param12vga_color(&(vgabuf->param1));
vgabuf->color = (vgabuf->color & 0xf8ff) | FOREGROUND(vgabuf->param1);
}
else if (40 <= vgabuf->param1 && vgabuf->param1 <= 47)
{
vgabuf->param1 -= 40;
param12vga_color(&(vgabuf->param1));
vgabuf->color = (vgabuf->color & 0x8fff) | BACKGROUND(vgabuf->param1);
}
else if (90 <= vgabuf->param1 && vgabuf->param1 <= 97)
{
vgabuf->param1 -= 90;
param12vga_color(&(vgabuf->param1));
vgabuf->param1 |= 0x8;
vgabuf->color = (vgabuf->color & 0xf0ff) | FOREGROUND(vgabuf->param1);
}
else if (100 <= vgabuf->param1 && vgabuf->param1 <= 107)
{
vgabuf->param1 -= 100;
param12vga_color(&(vgabuf->param1));
vgabuf->param1 |= 0x8;
vgabuf->color = (vgabuf->color & 0x0fff) | BACKGROUND(vgabuf->param1);
}
else
{
warn("unsupport CSI: color");
}
if (vgabuf->Is2param == true)
{
if (vgabuf->param2 == 0 && vgabuf->param1 == 0)
{
vgabuf->color = DEFAULT_CHAR_COLOR;
}
else if (vgabuf->param2 == 1)
{
vgabuf->color |= 0x8800;
}
else if (vgabuf->param2 == 2)
{
vgabuf->color &= 0x7700;
}
else if (30 <= vgabuf->param2 && vgabuf->param2 <= 37)
{
vgabuf->param2 -= 30;
param12vga_color(&(vgabuf->param2));
vgabuf->color = (vgabuf->color & 0xf8ff) | FOREGROUND(vgabuf->param2);
}
else if (40 <= vgabuf->param2 && vgabuf->param2 <= 47)
{
vgabuf->param2 -= 40;
param12vga_color(&(vgabuf->param2));
vgabuf->color = (vgabuf->color & 0x8fff) | BACKGROUND(vgabuf->param2);
}
else if (90 <= vgabuf->param2 && vgabuf->param2 <= 97)
{
vgabuf->param2 -= 90;
param12vga_color(&(vgabuf->param2));
vgabuf->param2 |= 0x8;
vgabuf->color = (vgabuf->color & 0xf0ff) | FOREGROUND(vgabuf->param2);
}
else if (100 <= vgabuf->param2 && vgabuf->param2 <= 107)
{
vgabuf->param2 -= 100;
param12vga_color(&(vgabuf->param2));
vgabuf->param2 |= 0x8;
vgabuf->color = (vgabuf->color & 0x0fff) | BACKGROUND(vgabuf->param2);
}
else
{
warn("unsupport CSI: color");
}
}
}
static void CSI_Erase_handler(vga_buf *vgabuf, int n)
{
if (n == 2)
{
vgabuf->scr_bot_line = ADDLINE(vgabuf->scr_top_line, SCR_HEIGHT - 1);
for (int i = 0; i < SCR_HEIGHT; i++)
{
vgabuf->scr_cur_line = NEXTLINE(vgabuf->scr_cur_line);
vgabuf->scr_bot_line = NEXTLINE(vgabuf->scr_bot_line);
// kprintf("af %x\n", vgabuf->scr_cur_line);
vgabuf->cur_row = CYCLE_SUB(vgabuf->scr_top_line, vgabuf->scr_bot_line, SCR_MAXLINE);
if (vgabuf->cur_row == SCR_HEIGHT)
{
// auto scroll
vgabuf->scr_top_line = NEXTLINE(vgabuf->scr_top_line);
}
if (vgabuf->scr_bot_line == vgabuf->head_line)
{
vgabuf->head_line = NEXTLINE(vgabuf->head_line);
// remember to fill blank the old line
u32 *ptr_buf = (u32 *)(vgabuf->buf + sizeof(u16) * vgabuf->scr_bot_line * SCR_WIDTH);
for (int i = 0; i < SCR_WIDTH * sizeof(u16) / sizeof(u32); ++i)
{
*ptr_buf++ = (BLANK << 16) | BLANK; // bg-black, fg-white, ascii-space
}
}
}
}
if (n == 3)
{
int line = vgabuf->head_line;
while (line != vgabuf->scr_top_line)
{
u32 *ptr_buf = (u32 *)(vgabuf->buf + sizeof(u16) * line * SCR_WIDTH);
for (int i = 0; i < SCR_WIDTH * sizeof(u16) / sizeof(u32); ++i)
{
*ptr_buf++ = (BLANK << 16) | BLANK; // bg-black, fg-white, ascii-space
}
line = NEXTLINE(line);
}
vgabuf->head_line = vgabuf->scr_top_line;
}
}
static void CSI_handler(u8 terminator, vga_buf *vgabuf)
{
vgabuf->CSI = CSI_ESC;
switch (terminator)
{
case 'A':
if (vgabuf->param1 == 0)
vgabuf->param1 == 1;
cursor_move(-vgabuf->param1, 0, vgabuf);
break;
case 'B':
if (vgabuf->param1 == 0)
vgabuf->param1 == 1;
cursor_move(+vgabuf->param1, 0, vgabuf);
break;
case 'C':
if (vgabuf->param1 == 0)
vgabuf->param1 == 1;
cursor_move(0, +vgabuf->param1, vgabuf);
break;
case 'D':
if (vgabuf->param1 == 0)
vgabuf->param1 == 1;
cursor_move(0, -vgabuf->param1, vgabuf); // nothing
break;
case 'E':
if (vgabuf->param1 == 0)
vgabuf->param1 == 1;
cursor_move(+vgabuf->param1, -vgabuf->cur_col, vgabuf);
break;
case 'F':
if (vgabuf->param1 == 0)
vgabuf->param1 == 1;
cursor_move(-vgabuf->param1, -vgabuf->cur_col, vgabuf);
break;
case 'G': // added
if (vgabuf->param1 == 0)
vgabuf->param1 == 1;
cursor_locate(CYCLE_SUB(vgabuf->scr_top_line, vgabuf->scr_cur_line, SCR_MAXLINE), vgabuf->param1 - 1, vgabuf);
break;
case 'H':
case 'f':
if (vgabuf->param1 == 0)
vgabuf->param1 == 1;
if (vgabuf->param2 == 0)
vgabuf->param2 == 1;
cursor_locate(vgabuf->param1 - 1, vgabuf->param2 - 1, vgabuf);
break;
case 'J':
if (vgabuf->param1 == 0)
clear_screen(vgabuf, vgabuf->scr_cur_line, vgabuf->cur_col, ADDLINE(vgabuf->scr_top_line, SCR_HEIGHT - 1), SCR_WIDTH - 1);
else if (vgabuf->param1 == 1)
clear_screen(vgabuf, vgabuf->scr_top_line, 0, vgabuf->scr_cur_line, vgabuf->cur_col);
else if (vgabuf->param1 == 2)
CSI_Erase_handler(vgabuf, 2);
else if (vgabuf->param1 == 3)
CSI_Erase_handler(vgabuf, 3);
break;
case 'K':
if (vgabuf->param1 == 0)
clear_screen(vgabuf, vgabuf->scr_cur_line, vgabuf->cur_col, vgabuf->scr_cur_line, SCR_WIDTH - 1);
else if (vgabuf->param1 == 1)
clear_screen(vgabuf, vgabuf->scr_cur_line, 0, vgabuf->scr_cur_line, vgabuf->cur_col);
else if (vgabuf->param1 == 2)
clear_screen(vgabuf, vgabuf->scr_cur_line, 0, vgabuf->scr_cur_line, SCR_WIDTH - 1);
break;
case 'S': // Scroll Up
if (vgabuf->param1 == 0)
vgabuf->param1 = 1;
csi_scroll(vgabuf, -vgabuf->param1);
break;
case 'T': // Scroll Down
if (vgabuf->param1 == 0)
vgabuf->param1 = 1;
csi_scroll(vgabuf, +vgabuf->param1);
break;
case 'm':
set_color(vgabuf);
break;
}
}
void vga_tty_write(NTTY *tty, char ch)
{
// assert(tty->driver_type == 1);
@ -566,14 +220,7 @@ void vga_tty_write(NTTY *tty, char ch)
vga->Is2param = false;
break;
default:
if (vga->color == 0)
{
buf[INDEX(vga->scr_cur_line, vga->cur_col)] = MAKE_CELL(DEFAULT_CHAR_COLOR, ch);
}
else
{
buf[INDEX(vga->scr_cur_line, vga->cur_col)] = MAKE_CELL(vga->color, ch);
}
buf[INDEX(vga->scr_cur_line, vga->cur_col)] = MAKE_CELL(vga->color, ch);
// buf[INDEX(vga->scr_cur_line, vga->cur_col)] = MAKE_CELL(DEFAULT_CHAR_COLOR, ch);
nextcol(vga);
break;
@ -614,8 +261,11 @@ void vga_tty_write(NTTY *tty, char ch)
; // do nothing
break;
case ';':
vga->CSI = CSI_PARAM2;
vga->Is2param = true;
if (vga->CSI == CSI_PARAM1)
{
vga->CSI = CSI_PARAM2;
vga->Is2param = true;
}
break;
default:
if (!(0x20 <= ch && ch <= 0x7e))

View File

@ -34,14 +34,16 @@ int main(int arg, char *argv[])
// printf("8888888888");
// printf("9999999999\r\b\b\n");
// }
printf("\x1b[33m");
for (int i = 0; i < 40; i++)
// printf("\x1b[33m");
// printf("\x1b[1m");
for (int i = 0; i < 14; i++)
{
printf("%d", i);
printf("\x1b[m");
printf("11111111111111111\n");
}
printf("\x1b[31;47m555555555555\x1b[m");
printf("\x1b[6T11111111111111111\n");
// Cursor Up
// printf("\x1b[2A");