Skip to content

Commit

Permalink
修改文本模式设置光标位置函数
Browse files Browse the repository at this point in the history
  • Loading branch information
copi143 committed Dec 18, 2024
1 parent a419bae commit 627dff4
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 42 deletions.
1 change: 0 additions & 1 deletion include/kernel/screen.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ typedef enum {
MODE_m = 'm'
} vt100_mode_t;

void move_cursor_by_idx(int idx);
void print(const char *str);
void screen_clear();
void putchar(int ch);
Expand Down
1 change: 0 additions & 1 deletion include/loader/screen.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@ int klogf(const char *format, ...);
void screen_clear();
void print(const char *str);
void screen_ne();
void move_cursor_by_idx(int idx);
void putchar(char ch);
35 changes: 1 addition & 34 deletions src/kernel/cpu/x86.asm
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
[bits 32]
global move_cursor_by_idx
global memtest_sub, start_app
global memtest_sub
section .text
Expand Down Expand Up @@ -52,38 +51,6 @@ memtest_sub: ; u32 memtest_sub(u32 start, u32 end)
POP EDI
RET
move_cursor_by_idx: ;移动光标
mov dx, 03d4h ;03d4h是索引端口
mov al, 0eh ;内部的0eh位置存放着光标位置的高八位
out dx, al
inc dx ;03d5h是数据端口用于读写数据
in al, dx ;读取光标的高八位并且放入bh
mov bh, al
dec dx ;这儿开始读取光标位置的低八位放入bl
mov al, 0fh ;0fh位置存放着光标位置的低八位
out dx, al
inc dx
in al, dx
mov bl, al
mov word bx, [esp + 4] ;获取参数中的光标位置
mov dx, 03d4h ;这段代码将改变后的光标位置写入端口内相应的地方以便下次访问
mov al, 0eh ;写入光标位置高八位
out dx, al
inc dx
mov al, bh
out dx, al
dec dx
mov al, 0fh ;写入光标位置低八位
out dx, al
inc dx
mov al, bl
out dx, al
ret
extern task_current
global asm_task_switch, asm_task_start
; 注意进入函数时必须 cli
Expand Down
17 changes: 16 additions & 1 deletion src/kernel/io/textmode.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
#include <kernel.h>

static int get_cursor_idx() {
asm_out8(0x03d4, 0x0e);
byte hi = asm_in8(0x03d5);
asm_out8(0x03d4, 0x0f);
byte lo = asm_in8(0x03d5);
return (hi << 8) + lo;
}

static void set_cursor_idx(int idx) {
asm_out8(0x03d4, 0x0e);
asm_out8(0x03d5, idx >> 8);
asm_out8(0x03d4, 0x0f);
asm_out8(0x03d5, idx & 0xff);
}

void MoveCursor_TextMode(struct tty *res, int x, int y) {
res->x = x;
res->y = y;
if (!res->cur_moving) return;
int i = y * res->xsize + x;
if (res->vram == (void *)0xb8000) { // TODO 移除特判
move_cursor_by_idx(i);
set_cursor_idx(i);
}
}

Expand Down
4 changes: 0 additions & 4 deletions src/loader/base/asmfunc.asm
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[BITS 32]
EXTERN flint ; 在 fdc.c 中定义
GLOBAL memtest_sub
GLOBAL move_cursor_by_idx
GLOBAL floppy_int
[SECTION .text]
Expand Down Expand Up @@ -64,8 +63,5 @@ mts_nomore:
POP EDI
RET
move_cursor_by_idx: ;移动光标
ret
[SECTION .data]
testsize: dd 0
17 changes: 16 additions & 1 deletion src/loader/driver/BasicVideo.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
#include <loader.h>

static int get_cursor_idx() {
asm_out8(0x03d4, 0x0e);
byte hi = asm_in8(0x03d5);
asm_out8(0x03d4, 0x0f);
byte lo = asm_in8(0x03d5);
return (hi << 8) + lo;
}

static void set_cursor_idx(int idx) {
asm_out8(0x03d4, 0x0e);
asm_out8(0x03d5, idx >> 8);
asm_out8(0x03d4, 0x0f);
asm_out8(0x03d5, idx & 0xff);
}

static int x, y;
static int cons_x, cons_y;

Expand Down Expand Up @@ -89,7 +104,7 @@ void putchar(char ch) {
}
void Move_Cursor(i16 x, i16 y) {
int res = y * 80 + x;
move_cursor_by_idx(res);
set_cursor_idx(res);
}
void print(cstr str) {
putstr(str, getlength(str));
Expand Down

0 comments on commit 627dff4

Please sign in to comment.