532 lines
13 KiB
C
532 lines
13 KiB
C
#include "type.h"
|
|
#include "stdio.h"
|
|
#include "const.h"
|
|
#include "protect.h"
|
|
#include "string.h"
|
|
#include "proc.h"
|
|
#include "tty.h"
|
|
#include "console.h"
|
|
#include "global.h"
|
|
#include "keyboard.h"
|
|
#include "proto.h"
|
|
#include "x86.h"
|
|
#include "memman.h"
|
|
#include "assert.h"
|
|
|
|
/*****************************************************************************
|
|
* Low level vga driver
|
|
*****************************************************************************/
|
|
|
|
/*****************************************************************************
|
|
* Display the cursor by setting CRTC (6845 compatible) registers.
|
|
*
|
|
* @param position Position of the cursor based on the beginning of the video
|
|
* memory. Note that it counts in WORDs, not in BYTEs.
|
|
*****************************************************************************/
|
|
static inline void vga_set_cursor(unsigned int position)
|
|
{
|
|
disable_int();
|
|
outb(CRTC_ADDR_REG, CURSOR_H);
|
|
outb(CRTC_DATA_REG, (position >> 8) & 0xFF);
|
|
outb(CRTC_ADDR_REG, CURSOR_L);
|
|
outb(CRTC_DATA_REG, position & 0xFF);
|
|
enable_int();
|
|
}
|
|
|
|
static inline void vga_disable_cursor()
|
|
{
|
|
outb(CRTC_ADDR_REG, 0x0A);
|
|
outb(CRTC_DATA_REG, 0x20);
|
|
}
|
|
|
|
static inline void vga_enable_cursor(u8 cursor_start, u8 cursor_end)
|
|
{
|
|
outb(0x3D4, 0x0A);
|
|
outb(0x3D5, (inb(0x3D5) & 0xC0) | cursor_start);
|
|
|
|
outb(0x3D4, 0x0B);
|
|
outb(0x3D5, (inb(0x3D5) & 0xE0) | cursor_end);
|
|
}
|
|
|
|
/*****************************************************************************
|
|
* Routine for hardware screen scrolling.
|
|
*
|
|
* @param addr Offset in the video memory.
|
|
*****************************************************************************/
|
|
static inline void vga_set_video_start_addr(u32 addr)
|
|
{
|
|
disable_int();
|
|
outb(CRTC_ADDR_REG, START_ADDR_H);
|
|
outb(CRTC_DATA_REG, (addr >> 8) & 0xFF);
|
|
outb(CRTC_ADDR_REG, START_ADDR_L);
|
|
outb(CRTC_DATA_REG, addr & 0xFF);
|
|
enable_int();
|
|
}
|
|
|
|
/*****************************************************************************
|
|
* Write data directly to Video Memory cell
|
|
*
|
|
* @param pos text mode position(pos*2 yourself)
|
|
* @param dat data to be written, with format [ BG | FG | ASCII ]
|
|
*****************************************************************************/
|
|
static inline void vga_put_raw(u32 pos, u16 dat)
|
|
{
|
|
u16 *pch = (u16 *)K_PHY2LIN(V_MEM_BASE + pos);
|
|
*pch = dat;
|
|
}
|
|
|
|
/*****************************************************************************
|
|
* copy a whole screen of text mode data into video memory, assume that screen
|
|
* width is 80 or 40
|
|
*
|
|
* @param src memory block with the same size as text mode video memory()
|
|
*****************************************************************************/
|
|
static inline void vga_flush_screen(void *src)
|
|
{
|
|
u32 *_src = src;
|
|
u32 *dst = (u32 *)K_PHY2LIN(V_MEM_BASE);
|
|
for (int i = 0; i < SCR_SIZE * sizeof(u16) / sizeof(u32); ++i)
|
|
{
|
|
*dst++ = *_src++;
|
|
}
|
|
}
|
|
|
|
/*****************************************************************************
|
|
* copy a whole screen of text mode data into video memory, assume that screen
|
|
* width is 80 or 40
|
|
*
|
|
* @param src memory block with the same size as text mode video memory()
|
|
*****************************************************************************/
|
|
static inline void vga_flush_line(void *src, int line_no)
|
|
{
|
|
u32 *_src = src;
|
|
u32 *dst = (u32 *)K_PHY2LIN(V_MEM_BASE + line_no * SCR_WIDTH * 2);
|
|
for (int i = 0; i < SCR_WIDTH * sizeof(u16) / sizeof(u32); ++i)
|
|
{
|
|
*dst++ = *_src++;
|
|
}
|
|
}
|
|
|
|
static inline void vga_flush_blankline(int line_no)
|
|
{
|
|
u32 *dst = (u32 *)K_PHY2LIN(V_MEM_BASE + line_no * SCR_WIDTH * 2);
|
|
for (int i = 0; i < SCR_WIDTH * sizeof(u16) / sizeof(u32); ++i)
|
|
{
|
|
*dst++ = (BLANK << 16) | BLANK;
|
|
}
|
|
}
|
|
|
|
/*****************************************************************************
|
|
* tty - vga driver
|
|
*****************************************************************************/
|
|
static u16 pagebuf[3][SCR_BUFSIZE];
|
|
|
|
void vga_tty_init(NTTY* tty) {
|
|
static int _cnt = 0;
|
|
assert(tty->driver_type == 1);
|
|
assert(tty->output_buf);
|
|
vga_buf *vga = tty->output_buf;
|
|
// vga->buf = (void*)do_kmalloc(sizeof(u16) * SCR_BUFSIZE);
|
|
vga->buf = (void*)pagebuf[_cnt ++];
|
|
// kprintf("malloced %p %p %p\n", vga->buf, &vga->buf, &vga->scr_top_line);
|
|
vga->cur_col = vga->cur_row = 0;
|
|
// buf->max_line = SCR_BUFSIZE / SCR_WIDTH;
|
|
vga->scr_top_line = vga->scr_cur_line = 0;
|
|
vga->head_line = 0;
|
|
u32 *ptr_buf = (u32 *)vga->buf;
|
|
for (int i = 0; i < SCR_BUFSIZE * sizeof(u16) / sizeof(u32); ++i)
|
|
{
|
|
ptr_buf[i] = (BLANK << 16) | BLANK; // bg-black, fg-white, ascii-space
|
|
}
|
|
kprintf("%p 0x%x %d\n", vga->buf, ((u32 *)vga->buf)[20], vga->scr_cur_line);
|
|
}
|
|
|
|
#define INDEX(row, col) ((row)*SCR_WIDTH + (col))
|
|
#define NEXTLINE(row) (((row) + 1) % SCR_MAXLINE)
|
|
#define LASTLINE(row) (((row)-1) >= 0 ? ((row)-1) % SCR_MAXLINE : SCR_MAXLINE)
|
|
|
|
static void newline(vga_buf *vgabuf)
|
|
{
|
|
vgabuf->cur_col = 0;
|
|
// kprintf("bf %x\n", vgabuf->scr_cur_line);
|
|
vgabuf->scr_cur_line = NEXTLINE(vgabuf->scr_cur_line);
|
|
// kprintf("af %x\n", vgabuf->scr_cur_line);
|
|
vgabuf->cur_row = abs(vgabuf->scr_cur_line - vgabuf->scr_top_line);
|
|
if (vgabuf->cur_row == SCR_HEIGHT)
|
|
{
|
|
// auto scroll
|
|
vgabuf->scr_top_line = NEXTLINE(vgabuf->scr_top_line);
|
|
if (vgabuf->scr_cur_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->head_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
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
static void nextcol(vga_buf *vgabuf)
|
|
{
|
|
vgabuf->cur_col++;
|
|
if (vgabuf->cur_col == SCR_WIDTH)
|
|
{
|
|
newline(vgabuf);
|
|
}
|
|
}
|
|
|
|
static void cursor_move(i16 move_row, i16 move_col, vga_buf *vgabuf)
|
|
{
|
|
vgabuf->scr_cur_line += move_row;
|
|
if (vgabuf->scr_cur_line < 0)
|
|
vgabuf->scr_cur_line = 0;
|
|
else if (vgabuf->scr_cur_line >= SCR_MAXLINE)
|
|
vgabuf->scr_cur_line = SCR_MAXLINE - 1;
|
|
|
|
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 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->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_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); // nothing
|
|
break;
|
|
case 'D':
|
|
if (vgabuf->param1 == 0)
|
|
vgabuf->param1 == 1;
|
|
cursor_move(0, +vgabuf->param1, vgabuf);
|
|
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
|
|
cursor_move(0, vgabuf->param1 - vgabuf->cur_col, vgabuf);
|
|
break;
|
|
case 'H':
|
|
cursor_move(vgabuf->param1 - vgabuf->scr_cur_line, vgabuf->param1 - vgabuf->cur_col, vgabuf);
|
|
break;
|
|
case 'J':
|
|
break;
|
|
case 'K':
|
|
break;
|
|
case 'S':
|
|
break;
|
|
case 'T':
|
|
break;
|
|
case 'm':
|
|
set_color(vgabuf);
|
|
break;
|
|
}
|
|
}
|
|
|
|
void vga_tty_write(NTTY *tty, char ch)
|
|
{
|
|
// assert(tty->driver_type == 1);
|
|
// assert(tty->output_buf);
|
|
vga_buf *vga = tty->output_buf;
|
|
u16 *buf = vga->buf;
|
|
// kprintf("vga_tty_write %c to %d %d\n", ch, vga->scr_cur_line, vga->cur_col);
|
|
if (vga->CSI == CSI_ESC)
|
|
{
|
|
switch (ch)
|
|
{
|
|
case '\t':
|
|
if (INDEX(vga->scr_cur_line, vga->cur_col) == SCR_BUFSIZE - 1)
|
|
break;
|
|
while (vga->cur_col % 4 != 1)
|
|
{
|
|
nextcol(vga);
|
|
}
|
|
break;
|
|
case '\n':
|
|
newline(vga);
|
|
break;
|
|
case '\r':
|
|
vga->cur_col = 0;
|
|
break;
|
|
case '\b':
|
|
// this implementation is mimic to usual linux shell
|
|
// it moves cursor left but neither crosses line nor delete character
|
|
if (vga->cur_col > 0)
|
|
{
|
|
vga->cur_col--;
|
|
}
|
|
break;
|
|
case '\x1b':
|
|
vga->CSI = CSI_BRACKET;
|
|
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(DEFAULT_CHAR_COLOR, ch);
|
|
nextcol(vga);
|
|
break;
|
|
}
|
|
}
|
|
else if (vga->CSI == CSI_BRACKET)
|
|
{
|
|
switch (ch)
|
|
{
|
|
case '[':
|
|
vga->CSI = CSI_PARAM1;
|
|
vga->param1 = vga->param2 = 0;
|
|
break;
|
|
default:
|
|
vga->CSI = CSI_ESC;
|
|
break;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
switch (ch)
|
|
{
|
|
case '0':
|
|
case '1':
|
|
case '2':
|
|
case '3':
|
|
case '4':
|
|
case '5':
|
|
case '6':
|
|
case '7':
|
|
case '8':
|
|
case '9':
|
|
if (vga->CSI == CSI_PARAM1)
|
|
vga->param1 = vga->param1 * 10 + ch - '0';
|
|
else if (vga->CSI == CSI_PARAM2)
|
|
vga->param2 = vga->param2 * 10 + ch - '0';
|
|
else
|
|
; // do nothing
|
|
break;
|
|
case ';':
|
|
vga->CSI = CSI_PARAM2;
|
|
break;
|
|
default:
|
|
if (!(0x20 <= ch && ch <= 0x7e))
|
|
vga->CSI = CSI_ESC;
|
|
if (0x40 <= ch && ch <= 0x7e)
|
|
CSI_handler(ch, vga);
|
|
break;
|
|
}
|
|
}
|
|
vga->cur_row = CYCLE_SUB(vga->scr_top_line, vga->scr_cur_line, SCR_MAXLINE);
|
|
// kprintf("row: %d; ", vga->cur_row);
|
|
}
|
|
|
|
void vga_tty_backspace(NTTY* tty) {
|
|
vga_buf* vga = tty->output_buf;
|
|
u16* buf = vga->buf;
|
|
if (vga->cur_col == 0) {
|
|
vga->cur_col = SCR_WIDTH - 1;
|
|
vga->scr_cur_line = LASTLINE(vga->scr_cur_line);
|
|
} else {
|
|
vga->cur_col --;
|
|
}
|
|
buf[INDEX(vga->scr_cur_line, vga->cur_col)] = BLANK;
|
|
vga->cur_row = CYCLE_SUB(vga->scr_top_line, vga->scr_cur_line, SCR_MAXLINE);
|
|
}
|
|
|
|
void vga_tty_flush(NTTY* tty) {
|
|
vga_buf* vga = tty->output_buf;
|
|
u16* buf = vga->buf;
|
|
int i, cur_line;
|
|
vga_set_cursor(INDEX(vga->cur_row, vga->cur_col));
|
|
if (vga->cur_row == SCR_WIDTH - 1)
|
|
{
|
|
vga_flush_screen(&buf[INDEX(vga->scr_top_line, 0)]);
|
|
}
|
|
else
|
|
{
|
|
cur_line = vga->scr_top_line;
|
|
for (i = 0; i <= vga->cur_row; ++i)
|
|
{
|
|
vga_flush_line(&buf[INDEX(cur_line, 0)], i);
|
|
cur_line = NEXTLINE(cur_line);
|
|
}
|
|
for (; i < SCR_WIDTH; ++i)
|
|
{
|
|
vga_flush_blankline(i);
|
|
}
|
|
}
|
|
// kprintf("flush: row=%d, top=%d, cur=%d\n", vga->cur_row, vga->scr_top_line, vga->scr_cur_line);
|
|
}
|
|
|
|
void vga_tty_scroll(NTTY *tty, int direction)
|
|
{
|
|
vga_buf *vga = tty->output_buf;
|
|
u16 *buf = vga->buf;
|
|
if (direction > 0)
|
|
{
|
|
// down
|
|
if (vga->scr_top_line == vga->scr_cur_line)
|
|
return;
|
|
vga->scr_top_line = NEXTLINE(vga->scr_top_line);
|
|
}
|
|
else
|
|
{
|
|
if (vga->scr_top_line == vga->head_line)
|
|
return;
|
|
vga->scr_top_line = LASTLINE(vga->scr_top_line);
|
|
}
|
|
vga->cur_row = abs(vga->scr_cur_line - vga->scr_top_line);
|
|
if (vga->cur_row >= SCR_HEIGHT)
|
|
{
|
|
vga_disable_cursor();
|
|
}
|
|
else
|
|
{
|
|
vga_enable_cursor(0, 15);
|
|
}
|
|
}
|
|
|
|
void vga_scroll_to_cur(NTTY *tty)
|
|
{
|
|
vga_buf *vga = tty->output_buf;
|
|
u16 *buf = vga->buf;
|
|
// vga->scr_top_line = vga->scr_cur_line
|
|
}
|
|
|
|
void vga_ttroll_to_cur(NTTY *tty)
|
|
{
|
|
vga_buf *vga = tty->output_buf;
|
|
u16 *buf = vga->buf;
|
|
// vga->scr_top_line = vga->scr_cur_line
|
|
}
|
|
|
|
void vga_tty_select(NTTY *tty)
|
|
{
|
|
//
|
|
} |