From 618be86f31f62c396ba6afee1db1f16b47811ea9 Mon Sep 17 00:00:00 2001 From: xiaoxiao <1010386862@qq.com> Date: Sat, 24 Dec 2022 16:58:23 +0800 Subject: [PATCH] add CSI JKST --- kernel/ktest.c | 87 ++++++++++---------- kernel/vga.c | 209 +++++++++++++++++++++++++++++++++++++------------ user/test.c | 5 +- 3 files changed, 211 insertions(+), 90 deletions(-) diff --git a/kernel/ktest.c b/kernel/ktest.c index 37c9ad6..4a5df2e 100644 --- a/kernel/ktest.c +++ b/kernel/ktest.c @@ -1,5 +1,5 @@ /* - * To test if new kernel features work normally, and if old features still + * To test if new kernel features work normally, and if old features still * work normally with new features added. * added by xw, 18/4/27 */ @@ -18,9 +18,9 @@ * @struct posix_tar_header * Borrowed from GNU `tar' */ -//added by mingxuan 2019-5-18 +// added by mingxuan 2019-5-18 struct posix_tar_header -{ /* byte offset */ +{ /* byte offset */ char name[100]; /* 0 */ char mode[8]; /* 100 */ char uid[8]; /* 108 */ @@ -29,7 +29,7 @@ struct posix_tar_header char mtime[12]; /* 136 */ char chksum[8]; /* 148 */ char typeflag; /* 156 */ - char linkname[100]; /* 157 */ + char linkname[100]; /* 157 */ char magic[6]; /* 257 */ char version[2]; /* 263 */ char uname[32]; /* 265 */ @@ -37,7 +37,7 @@ struct posix_tar_header char devmajor[8]; /* 329 */ char devminor[8]; /* 337 */ char prefix[155]; /* 345 */ - /* 500 */ + /* 500 */ }; /***************************************************************************** @@ -46,88 +46,95 @@ struct posix_tar_header *****************************************************************************/ /** * Extract the tar file and store them. - * + * * @param filename The tar file. *****************************************************************************/ -static void untar(const char * filename) +static void untar(const char *filename) { printf("[extract %s \n", filename); - int fd = do_vopen(filename, O_RDWR);//modified by mingxuan 2019-5-20 + int fd = do_vopen(filename, O_RDWR); // modified by mingxuan 2019-5-20 char buf[512 * 16]; int chunk = sizeof(buf); int i = 0; - while (1) { - do_vread(fd, buf, 512); //modified by mingxuan 2019-5-21 - if (buf[0] == 0) { + while (1) + { + do_vread(fd, buf, 512); // modified by mingxuan 2019-5-21 + if (buf[0] == 0) + { if (i == 0) printf(" need not unpack the file.\n"); break; } i++; - struct posix_tar_header * phdr = (struct posix_tar_header *)buf; + struct posix_tar_header *phdr = (struct posix_tar_header *)buf; /* calculate the file size */ - char * p = phdr->size; + char *p = phdr->size; int f_len = 0; while (*p) f_len = (f_len * 8) + (*p++ - '0'); /* octal */ int bytes_left = f_len; char full_name[30] = "orange/"; - strcat(full_name,phdr->name); - int fdout = do_vopen(full_name, O_CREAT | O_RDWR ); //modified by mingxuan 2019-5-20 - if (fdout == -1) { + strcat(full_name, phdr->name); + int fdout = do_vopen(full_name, O_CREAT | O_RDWR); // modified by mingxuan 2019-5-20 + if (fdout == -1) + { printf(" failed to extract file: %s\n", phdr->name); printf(" aborted]\n"); - do_vclose(fd); //modified by mingxuan 2019-5-20 + do_vclose(fd); // modified by mingxuan 2019-5-20 return; } - printf(" %s \n", phdr->name); //deleted by mingxuan 2019-5-22 - while (bytes_left) { + printf(" %s \n", phdr->name); // deleted by mingxuan 2019-5-22 + while (bytes_left) + { int iobytes = min(chunk, bytes_left); - - do_vread(fd, buf, ((iobytes - 1) / 512 + 1) * 512); //modified by mingxuan 2019-5-21 - - do_vwrite(fdout, buf, iobytes); //modified by mingxuan 2019-5-21 + + do_vread(fd, buf, ((iobytes - 1) / 512 + 1) * 512); // modified by mingxuan 2019-5-21 + + do_vwrite(fdout, buf, iobytes); // modified by mingxuan 2019-5-21 bytes_left -= iobytes; } - do_vclose(fdout); //modified by mingxuan 2019-5-20 + do_vclose(fdout); // modified by mingxuan 2019-5-20 } - if (i) { - do_vlseek(fd, 0, SEEK_SET); //modified by mingxuan 2019-5-20 + if (i) + { + do_vlseek(fd, 0, SEEK_SET); // modified by mingxuan 2019-5-20 buf[0] = 0; - do_vwrite(fd, buf, 1); //modified by mingxuan 2019-5-20 + do_vwrite(fd, buf, 1); // modified by mingxuan 2019-5-20 } - do_vclose(fd); //modified by mingxuan 2019-5-21 + do_vclose(fd); // modified by mingxuan 2019-5-21 printf(" done, %d files extracted]\n", i); } - void initial() { - - int stdin = do_vopen("dev_tty0",O_RDWR); - int stdout= do_vopen("dev_tty0",O_RDWR); - int stderr= do_vopen("dev_tty0",O_RDWR); - //untar(INSTALL_FILENAME); - //modified by mingxuan 2019-5-21 - char full_name[30]="orange/";; - printf("untar:%s\n",full_name); - strcat(full_name,INSTALL_FILENAME); + int stdin = do_vopen("dev_tty0", O_RDWR); + int stdout = do_vopen("dev_tty0", O_RDWR); + int stderr = do_vopen("dev_tty0", O_RDWR); + + // untar(INSTALL_FILENAME); + // modified by mingxuan 2019-5-21 + char full_name[30] = "orange/"; + ; + printf("untar:%s\n", full_name); + strcat(full_name, INSTALL_FILENAME); untar(full_name); do_vclose(stdin); do_vclose(stdout); do_vclose(stderr); - exec("orange/shell_0.bin"); + // exec("orange/shell_0.bin"); + exec("orange/test.bin"); - while(1); + while (1) + ; } \ No newline at end of file diff --git a/kernel/vga.c b/kernel/vga.c index 5edf2bb..224e84a 100644 --- a/kernel/vga.c +++ b/kernel/vga.c @@ -121,13 +121,14 @@ static inline void vga_flush_blankline(int line_no) *****************************************************************************/ static u16 pagebuf[3][SCR_BUFSIZE]; -void vga_tty_init(NTTY* tty) { +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 ++]; + 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; @@ -144,6 +145,37 @@ void vga_tty_init(NTTY* tty) { #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) +#define ADDLINE(row, add_num) (((row) + add_num) % SCR_MAXLINE) + +// called by csi +void vga_tty_scroll(vga_buf *vgabuf, i16 scroll_num) +{ + u16 *buf = vgabuf->buf; + while (scroll_num > 0) + { + // down + scroll_num--; + if (vgabuf->scr_top_line == vgabuf->scr_cur_line) + return; + vgabuf->scr_top_line = NEXTLINE(vgabuf->scr_top_line); + } + while (scroll_num < 0) + { + scroll_num++; + if (vgabuf->scr_top_line == vgabuf->head_line) + return; + vgabuf->scr_top_line = LASTLINE(vgabuf->scr_top_line); + } + vgabuf->cur_row = abs(vgabuf->scr_cur_line - vgabuf->scr_top_line); + if (vgabuf->cur_row >= SCR_HEIGHT) + { + vga_disable_cursor(); + } + else + { + vga_enable_cursor(0, 15); + } +} static void newline(vga_buf *vgabuf) { @@ -180,12 +212,40 @@ static void nextcol(vga_buf *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; - + while (move_row > 0) // down + { + move_col--; + vgabuf->scr_cur_line = NEXTLINE(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 + } + } + } + } + while (move_row < 0) // up + { + move_col++; + // 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->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; @@ -203,6 +263,25 @@ param12vga_color(i16 *param) *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) @@ -334,12 +413,41 @@ static void CSI_handler(u8 terminator, vga_buf *vgabuf) cursor_move(vgabuf->param1 - vgabuf->scr_cur_line, vgabuf->param1 - vgabuf->cur_col, 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) + { + clear_screen(vgabuf, vgabuf->scr_top_line, 0, ADDLINE(vgabuf->scr_top_line, SCR_HEIGHT - 1), SCR_WIDTH - 1); + vgabuf->cur_col = 0; + vgabuf->scr_cur_line = vgabuf->scr_top_line; + } + else if (vgabuf->param1 == 3) + { + clear_screen(vgabuf, 0, 0, SCR_MAXLINE - 1, SCR_WIDTH - 1); + vgabuf->cur_col = vgabuf->cur_row = 0; + vgabuf->scr_top_line = vgabuf->scr_cur_line = 0; + vgabuf->head_line = 0; + } 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': + case 'S': // Scroll Up + if (vgabuf->param1 == 0) + vgabuf->param1 = 1; + vga_tty_scroll(vgabuf, -vgabuf->param1); break; - case 'T': + case 'T': // Scroll Down + if (vgabuf->param1 == 0) + vgabuf->param1 = 1; + vga_tty_scroll(vgabuf, +vgabuf->param1); break; case 'm': set_color(vgabuf); @@ -446,25 +554,30 @@ void vga_tty_write(NTTY *tty, char ch) // 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) { +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 --; + } + 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; +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) + if (vga->cur_row == SCR_HEIGHT - 1) ///////////???????????????? { vga_flush_screen(&buf[INDEX(vga->scr_top_line, 0)]); } @@ -476,7 +589,7 @@ void vga_tty_flush(NTTY* tty) { vga_flush_line(&buf[INDEX(cur_line, 0)], i); cur_line = NEXTLINE(cur_line); } - for (; i < SCR_WIDTH; ++i) + for (; i < SCR_HEIGHT; ++i) { vga_flush_blankline(i); } @@ -484,33 +597,33 @@ void vga_tty_flush(NTTY* tty) { // 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_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) { diff --git a/user/test.c b/user/test.c index 90d1883..b13f0ea 100644 --- a/user/test.c +++ b/user/test.c @@ -30,8 +30,9 @@ int main(int arg, char *argv[]) printf("5555555555"); printf("6666666666"); printf("7777777777"); - printf("8888888888"); - printf("9999999999\r\b\b\n"); + printf("\x1b[5F"); + // printf("8888888888"); + // printf("9999999999\r\b\b\n"); } while (1)