Skip to content

Commit

Permalink
recreate day15 folder with bugs fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
Qilin Jane authored and Qilin Jane committed Dec 18, 2020
1 parent a34e365 commit 17b8c30
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 102 deletions.
25 changes: 6 additions & 19 deletions day15/bootpack.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@ void make_textbox8(struct SHEET *sht, int x0, int y0, int sx, int sy, int c);
void HariMain(void)
{
struct BOOTINFO *binfo = (struct BOOTINFO *) ADR_BOOTINFO; // 结构体指针指向储存系统信息的地址
// 内存管理
struct MEMMAN *memman = (struct MEMMAN *) MEMMAN_ADDR;
// 图层管理
struct FIFO32 fifo;
int fifobuf[128];
struct SHTCTL *shtctl;
// 图层背景,鼠标
struct SHEET *sht_bak, *sht_mouse, *sht_win;
// 缓冲区
struct FIFO32 fifo;
// 定义背景缓冲区、鼠标缓冲区
unsigned char *buf_back, buf_mouse[256], *buf_win;
// 键盘按键字符表
Expand All @@ -26,19 +24,16 @@ void HariMain(void)
'2', '3', '0', '.'
};
unsigned int memtotal;
int fifobuf[128];
char s[40];
struct TIMER *timer, *timer2, *timer3;
int i, mx, my, cursor_x, cursor_c;

init_gdtidt(); // 初始化 全局段记录表,中断记录表
init_pic(); // 初始化 PIC
io_sti(); // IDT/PIC 的初始化已经完成,开放 CPU 的中断
init_pit(); // 设定定时器频率
io_out8(PIC0_IMR, 0xf8); // 开放PIC1和键盘中断(11111001)
io_out8(PIC1_IMR, 0xef); // 开放鼠标中断(11101111)
fifo32_init(&fifo, 128, fifobuf);

timer = timer_alloc();
timer2 = timer_alloc();
timer3 = timer_alloc();
Expand All @@ -48,14 +43,12 @@ void HariMain(void)
timer_settime(timer, 1000);
timer_settime(timer2, 300);
timer_settime(timer3, 50);

init_keyboard(&fifo, 256);
enable_mouse(&fifo, 512, &mdec);
memtotal = memtest(0x00400000, 0xbfffffff);
memman_init(memman);
memman_free(memman, 0x00001000, 0x0009e000); // 0x00001000 - 0x0009efff
memman_free(memman, 0x00400000, memtotal - 0x00400000);

init_palette();
// 分配图层、内存
shtctl = shtctl_init(memman, binfo->vram, binfo->scrnx, binfo->scrny);
Expand All @@ -64,14 +57,11 @@ void HariMain(void)
sht_win = sheet_alloc(shtctl);
buf_back = (unsigned char *) memman_alloc_4k(memman, binfo->scrnx * binfo->scrny);
buf_win = (unsigned char *) memman_alloc_4k(memman, 160 * 52);

sheet_setbuf(sht_bak, buf_back, binfo->scrnx, binfo->scrny, -1); // 没有透明色
sheet_setbuf(sht_mouse, buf_mouse, 16, 16, 99); // 透明色号 99
sheet_setbuf(sht_win, buf_win, 160, 52, -1); // 没有透明色

init_screen(buf_back, binfo->scrnx, binfo->scrny);
init_mouse_cursor8(buf_mouse, 99); // 背景色号 99

// 创建窗口
make_window8(buf_win, 160, 52, "window");
// 创建输入盒子
Expand All @@ -80,21 +70,17 @@ void HariMain(void)
cursor_c = COL8_FFFFFF;
mx = (binfo->scrnx - 16) / 2;
my = (binfo->scrny - 28 - 16) / 2;

// 设置在移动图层时进行局部画面刷新
sheet_slide(sht_bak, 0, 0);
sheet_slide(sht_mouse, mx, my);
sheet_slide(sht_win, 80, 72);

// 设置叠加显示优先级
sheet_updown(sht_bak, 0);
sheet_updown(sht_win, 1);
sheet_updown(sht_mouse, 2);

putfont8_pos(buf_back, binfo->scrnx, 0, 30, COL8_FFFFFF, "SPARK.OS");
sprintf(s, "memory %dMB free: %dKB", memtotal / (1024 * 1024), memman_total(memman) / 1024);
putfonts8_str(buf_back, binfo->scrnx, 0, 50, COL8_FFFFFF, s);

sheet_refresh(sht_bak, 0, 0, sht_bak->bxsize, sht_bak->bysize);

for (;;) {
Expand Down Expand Up @@ -131,7 +117,10 @@ void HariMain(void)
} else if (512 <= i && i <= 767) {
if (mouse_decode(&mdec, i - 512) != 0) {
sprintf(s, "[lcr %4d %4d]", mdec.x, mdec.y);
if ((mdec.btn & 0x01) != 0) s[1] = 'L';
if ((mdec.btn & 0x01) != 0) {
s[1] = 'L';
sheet_slide(sht_win, mx - 80, my - 8);
}
if ((mdec.btn & 0x02) != 0) s[3] = 'R';
if ((mdec.btn & 0x04) != 0) s[2] = 'C';
putfonts8_str_sht(sht_bak, 0, 32, COL8_FFFFFF, COL8_008484, s);
Expand All @@ -147,9 +136,7 @@ void HariMain(void)
// 显示坐标
putfonts8_str_sht(sht_bak, 0, 16, COL8_FFFFFF, COL8_008484, s);
// 描绘鼠标
// 包含 sheet_refresh
sheet_slide(sht_mouse, mx, my);
if ((mdec.btn & 0x01) != 0) sheet_slide(sht_win, mx - 80, my - 8);
}
}
switch (i) {
Expand Down
11 changes: 0 additions & 11 deletions day15/bootpack.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,6 @@ void init_gdtidt(void);

/* fifo.c */
#define FLAGS_OVERRUN 0x0001
// FIFO 缓冲区模型
struct FIFO8 {
unsigned char *buf;
int w, r, size, free, flags;
};

void fifo8_init(struct FIFO8 *fifo, int size, unsigned char *buf);
int fifo8_put(struct FIFO8 *fifo, unsigned char data);
int fifo8_get(struct FIFO8 *fifo);
int fifo8_status(struct FIFO8 *fifo);

// FIFO 优化缓冲区
struct FIFO32 {
int *buf;
Expand Down
69 changes: 4 additions & 65 deletions day15/fifo.c
Original file line number Diff line number Diff line change
@@ -1,59 +1,5 @@
#include "bootpack.h"

/** 初始化FIFO缓冲区 */
void fifo8_init(struct FIFO8 *fifo, int size, unsigned char *buf)
{
fifo->size = size;
fifo->buf = buf;
// 缓冲区大小
fifo->free = size;
// 当前索引
fifo->flags = 0;
// 下一个数据写入位置
fifo->w = 0;
// 下一个数据读出位置
fifo->r = 0;
}

/** 向FIFO存放数据 */
int fifo8_put(struct FIFO8 *fifo, unsigned char data)
{
if (fifo->free == 0) {
// 空余没有了,溢出
fifo->flags |= FLAGS_OVERRUN;
return -1;
}
fifo->buf[fifo->w] = data;
fifo->w++;
if (fifo->w == fifo->size) {
fifo->w = 0;
}
fifo->free--;
return 0;
}

/** 向FIFO读取数据 */
int fifo8_get(struct FIFO8 *fifo)
{
int data;
if (fifo->free == fifo->size) {
return -1;
}
data = fifo->buf[fifo->r];
fifo->r++;
if (fifo->r == fifo->size) {
fifo->r = 0;
}
fifo->free++;
return data;
}

/** 报告FIFO状态 */
int fifo8_status(struct FIFO8 *fifo)
{
return fifo->size - fifo->free;
}

/** 初始化FIFO缓冲区 */
void fifo32_init(struct FIFO32 *fifo, int size, int *buf)
{
Expand All @@ -79,25 +25,18 @@ int fifo32_put(struct FIFO32 *fifo, int data)
}
fifo->buf[fifo->w] = data;
fifo->w++;
if (fifo->w == fifo->size) {
fifo->w = 0;
}
if (fifo->w == fifo->size) fifo->w = 0;
fifo->free--;
return 0;
}

/** 向FIFO读取数据 */
int fifo32_get(struct FIFO32 *fifo)
{
int data;
if (fifo->free == fifo->size) {
return -1;
}
data = fifo->buf[fifo->r];
if (fifo->free == fifo->size) return -1;
int data = fifo->buf[fifo->r];
fifo->r++;
if (fifo->r == fifo->size) {
fifo->r = 0;
}
if (fifo->r == fifo->size) fifo->r = 0;
fifo->free++;
return data;
}
Expand Down
4 changes: 3 additions & 1 deletion day15/keyboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ void inthandler21(int *esp)
/** 等待键盘控制电路准备完毕 */
void wait_KBC_sendready(void)
{
for(;;) if ((io_in8(PORT_KEYSTA) & KEYSTA_SEND_NOTREADY) == 0) { break; }
for(;;) {
if ((io_in8(PORT_KEYSTA) & KEYSTA_SEND_NOTREADY) == 0) break;
}
}

/** 初始化键盘控制电路
Expand Down
13 changes: 8 additions & 5 deletions day15/mouse.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,16 @@ void enable_mouse(struct FIFO32 *fifo, int data0, struct MOUSE_DEC *mdec)
int mouse_decode(struct MOUSE_DEC *mdec, unsigned char data)
{
switch (mdec->phase) {
case 0:
// 等待鼠标就绪 (0xfa)
if (data == 0xfa) mdec->phase = 1;
return 0;
/** 等待鼠标发送第一个字节 */
case 1:
mdec->buf[0] = data;
mdec->phase = 2;
if ((data & 0xc8) == 0x08) {
mdec->buf[0] = data;
mdec->phase = 2;
}
return 0;
/** 等待鼠标发送第二个字节 */
case 2:
Expand All @@ -58,9 +64,6 @@ int mouse_decode(struct MOUSE_DEC *mdec, unsigned char data)
// 鼠标的y方向与画面符号相反
mdec->y = - mdec->y;
return 1;
default:
// 等待鼠标就绪 (0xfa)
if (data == 0xfa) mdec->phase = 1;
}
return -1;
}
2 changes: 1 addition & 1 deletion day15/timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ void timer_settime(struct TIMER *timer, unsigned int timeout)
return;
}
// 搜索插入位置
while (1) {
for(;;) {
s = t;
t = t->next;
// 最后面
Expand Down

0 comments on commit 17b8c30

Please sign in to comment.