30天自制操作系统笔记(九十)——源码

原创 2013年12月02日 09:33:45

这里为了节省篇幅和csdn服务器的空间偷笑,我们只给出在七八之上改动的或者新建的源码文件的内容:

运行效果地址http://blog.csdn.net/ucan23/article/details/17066837点击打开链接

sheet.c

/* filename:    sheet.c
 * description: 对图层的管理
 * author:      Howard
 * date:        2013-12-01
 * version:     v1.0
 */
 
#include "bootpack.h"
 
struct SHTCTL *shtctl_init(struct MEMMAN *memman, unsigned char *vram, int xsize, int ysize)
{
	struct SHTCTL *ctl;
	int i;
	ctl = (struct SHTCTL *) memman_alloc_4k(memman, sizeof (struct SHTCTL));
	if (0==ctl){
		goto err;
	}
	ctl->vram = vram;
	ctl->xsize = xsize;
	ctl->ysize = ysize;
	ctl->top = -1; /*一个sheet也没有*/
	for (i=0; i<MAX_SHEETS; i++){
		ctl->sheets0[i].flags = 0; /*标记为未使用*/
	}
err:
	return ctl;
}

struct SHEET *sheet_alloc(struct SHTCTL *ctl)
{
	struct SHEET *sht;
	int i;
	for (i=0; i<MAX_SHEETS; i++){
		if (0==ctl->sheets0[i].flags){
			sht = &ctl->sheets0[i];
			sht->flags = SHEET_USE;
			sht->height = -1;
			return sht;
		}
	}
	return 0;
}

void sheet_setbuf(struct SHEET *sht, unsigned char *buf, int xsize, int ysize, int col_inv)
{
	sht->buf = buf;
	sht->bxsize = xsize;
	sht->bysize = ysize;
	sht->col_inv = col_inv;
	return;
}

void sheet_refreshsub(struct SHTCTL *ctl, int vx0, int vy0, int vx1, int vy1)
{
	int h, bx, by, vx, vy,bx0, by0, bx1, by1;
	unsigned char *buf, c, *vram = ctl->vram;
	struct SHEET *sht;
	for (h = 0; h <= ctl->top; h++) {
		sht = ctl->sheets[h];
		buf = sht->buf;
		bx0 = vx0 - sht->vx0;
		by0 = vy0 - sht->vy0;
		bx1 = vx1 - sht->vx0;
		by1 = vy1 - sht->vy0;
		if (bx0<0){
			bx0 = 0;
		}
		if (by0<0){
			by0 = 0;
		}
		if (bx1>sht->bxsize){
			bx1 = sht->bxsize;
		}
		if (by1>sht->bysize){
			by1 = sht->bysize;
		}
		for (by = by0; by < by1; by++) {
			vy = sht->vy0 + by;
			for (bx = bx0; bx < bx1; bx++) {
				vx = sht->vx0 + bx;
					c = buf[by * sht->bxsize + bx];
					if (c != sht->col_inv) {
						vram[vy * ctl->xsize + vx] = c;
					}
			}
		}
	}
	return;
}

void sheet_updown(struct SHTCTL *ctl, struct SHEET *sht, int height)
{
	int h, old = sht->height;		/*存储设置前的高度信息*/
	/*如果指定的高度过低则进行修正*/
	if (height>ctl->top+1){
		height = ctl->top + 1;
	}
	if (height<-1){
		height = -1;
	}
	sht->height = height; 	/*设置高度*/
	
	/*sheets[]的重新排列*/
	if (old>height){
		if (height>=0){
			for (h = old; h>height;h--){
				ctl->sheets[h] = ctl->sheets[h-1];
				ctl->sheets[h]->height = h;
			}
			ctl->sheets[height] = sht;
		} else {
			if (ctl->top>old){
				for (h=old; h<ctl->top; h++){
					ctl->sheets[h] = ctl->sheets[h+1];
					ctl->sheets[h]->height = h;
				}
			}
			ctl->top --;
		}
		//sheet_refresh(ctl);
		sheet_refreshsub(ctl, sht->vx0, sht->vy0, sht->vx0 + sht->bxsize, sht->vy0 + sht->bysize);
	} else if (old<height){
		if (old>=0){
			for (h=old; h<height; h++){
				ctl->sheets[h] = ctl->sheets[h+1];
				ctl->sheets[h]->height = h;
			}
			ctl->sheets[height] = sht;
		} else {
			for (h=ctl->top; h>=height; h--){
				ctl->sheets[h+1] = ctl->sheets[h];
				ctl->sheets[h+1]->height = h + 1;
			}
			ctl->sheets[height] = sht;
			ctl->top ++;
		}
		//sheet_refresh(ctl);
		sheet_refreshsub(ctl, sht->vx0, sht->vy0, sht->vx0 + sht->bxsize, sht->vy0 + sht->bysize);
	}
	return;
}

void sheet_refresh(struct SHTCTL *ctl, struct SHEET *sht, int bx0, int by0, int bx1, int by1)
{
	if (sht->height >= 0) { 
		sheet_refreshsub(ctl, sht->vx0 + bx0, sht->vy0 + by0, sht->vx0 + bx1, sht->vy0 + by1);
	}
	return;
}
/*
void sheet_refresh(struct SHTCTL *ctl)
{
	int h, bx, by, vx, vy;
	unsigned char *buf, c, *vram = ctl->vram;
	struct SHEET *sht;
	for (h=0; h<=ctl->top; h++){
		sht = ctl->sheets[h];
		buf = sht->buf;
		for (by=0; by<sht->bysize; by++){
			vy = sht->vy0 + by;
			for (bx=0; bx<sht->bxsize; bx++){
				vx = sht->vx0 +bx;
				c = buf[by*sht->bxsize + bx];
				if (c!=sht->col_inv){
					vram[vy*ctl->xsize+vx] = c;
				}
			}
		}
	}
	return;
}
*/

void sheet_slide(struct SHTCTL *ctl, struct SHEET *sht, int vx0, int vy0)
{
	int old_vx0 = sht->vx0, old_vy0 = sht->vy0;
	sht->vx0 = vx0;
	sht->vy0 = vy0;
	if (sht->height>=0){
		//sheet_refresh(ctl);
		sheet_refreshsub(ctl, old_vx0, old_vy0, old_vx0 + sht->bxsize, old_vy0 + sht->bysize);
		sheet_refreshsub(ctl, vx0, vy0, vx0 + sht->bxsize, vy0 + sht->bysize);
	}
	return;
}
void sheet_free(struct SHTCTL *ctl, struct SHEET *sht)
{
	if (sht->height>=0){
		sheet_updown(ctl, sht, -1);
	}
	sht->flags = 0;
	return;
}
memory.c

/* filename:    memory.c
 * description: 包含了有内存操作的相关函数
 * author:      Howard
 * date:        2013-12-01
 * version:     v1.0
 */

#include "bootpack.h"

unsigned int memtest(unsigned int start, unsigned int end)
{
	char flg486 = 0;
	unsigned int eflg, cr0, i;
	eflg = io_load_eflags();
	eflg |= EFLAGS_AC_BIT;
	io_store_eflags(eflg);
	eflg = io_load_eflags();
	if (0!=(eflg & EFLAGS_AC_BIT)){
		flg486 = 1;
	}
	eflg &= -EFLAGS_AC_BIT;
	io_store_eflags(eflg);
	
	if (0!=flg486){
		cr0 = load_cr0();
		cr0 |= CR0_CACHE_DISABLE;
		store_cr0(cr0);
	}
	
	i = memtest_sub(start, end);
	
	if (0!=flg486){
		cr0 = load_cr0();
		cr0 &= ~CR0_CACHE_DISABLE;
		store_cr0(cr0);
	}
	
	return i;
}
/*unsigned int memtest_sub(unsigned int start, unsigned int end)
{
	unsigned int i, *p, old, pat0 = 0xaa55aa55, pat1 = 0x55aa55aa;
	for (i=start; i<=end; i+=0x1000){
		p = (unsigned int *) (i+0xffc);
		old = *p;
		*p = pat0;
		*p ^= 0xffffffff;
		if (*p != pat1){
not_memory:
			*p = old;
			break;
		}
		*p ^= 0xffffffff;
		if (*p != pat0){
			goto not_memory;
		}
		*p = old;
	}
	return i;
}*/

void memman_init(struct MEMMAN *man)
{
	man->frees = 0;
	man->maxfrees = 0;
	man->lostsize = 0;
	man->losts = 0;
	return;
}
unsigned int memman_total(struct MEMMAN *man){
	unsigned int i, t = 0;
	for (i=0; i<man->frees; i++){
		t += man->free[i].size;
	}
	return t;
}
unsigned int memman_alloc(struct MEMMAN *man, unsigned int size)
{
	unsigned int i, a;
	for (i=0; i<man->frees; i++){
		if (man->free[i].size>=size){
			/*找到了足够大的内存*/
			a = man->free[i].addr;
			man->free[i].addr += size;
			man->free[i].size -= size;
			if (0==man->free[i].size){
				man->frees--;
				for (; i<man->frees; i++){
					man->free[i] = man->free[i+1];
				}
			}
			return a;
		}
	}
	return 0; /*没有可用的内存空间(内存分配失败)*/
}

int memman_free(struct MEMMAN *man, unsigned int addr, unsigned int size)
{
	int i,j;
	/*为便于归纳内存,将free[]按照addr的顺序排列*/
	/*所以先决定放在哪里*/
	for (i=0; i<man->frees; i++){
		if (man->free[i].addr > addr){
			break;
		}
	}
	/* free[i-1].addr<addr< free[i].addr*/
	if (i>0){
		if (man->free[i-1].addr + man->free[i-1].size == addr){
			man->free[i-1].size += size;
			if (i<man->frees){
				if (addr+size == man->free[i+1].addr){
					man->free[i-1].size += man->free[i].size;
					man->frees--;
					for (; i<man->frees; i++){
						man->free[i] = man->free[i+1];
					}
				}
			}
			return 0;
		}
	}
	/*不能与前面的可用内存归结在一起*/
	if (i<man->frees){
		if (addr+size == man->free[i].addr){
			/*可以与后面的合并在一起*/
			man->free[i].addr = addr;
			man->free[i].size += size;
			return 0;
		}
	}
	/*既不能跟前面的合并在一起也不能跟后面的合并在一起*/
	if (man->frees<MEMMAN_FREES){
		for (j=man->frees; j>i; j--){
			man->free[j] = man->free[j-1];
		}
		man->frees ++;
		if (man->maxfrees < man->frees){
			man->maxfrees = man->frees;
		}
		man->free[i].addr = addr;
		man->free[i].size = size;
		return 0;
	}
	man->losts ++;
	man->lostsize += size;
	return -1;
}

unsigned int memman_alloc_4k(struct MEMMAN *man, unsigned int size)
{
	unsigned int a;
	size = (size + 0xfff) & (0xfffff000);
	a = memman_alloc(man, size);
	return a;
}

int memman_free_4k(struct MEMMAN *man, unsigned int addr, unsigned int size)
{
	int i;
	size = (size + 0xfff) & (0xfffff000);
	i = memman_free(man, addr, size);
	return i;
}
bootpack.h

/* filename:    bootpack.h
 * description: 函数的声明与变量的定义
 * author:      Howard
 * date:        2013-11-28
 * version:     v1.0
 */


/*ucan23.nas*/
struct BOOTINFO{
	char cyls;
	char leds;
	char vmode;
	char reserve;
	short scrnx;
	short scrny;
	char *vram;
};
#define ADR_BOOTINFO 	 0x00000ff0

/*naskfunc.nas*/
/*告诉C编译器,有函数在别的文件里*/
void io_hlt(void);
void io_cli(void);
void io_sti(void);
void io_stihlt(void);
int io_in8(int port);
void io_out8(int port, int data);
int io_load_eflags(void);
void io_store_eflags(int eflags);
void write_mem8(int addr, int data);
void asm_inthandler21(void);
void asm_inthandler27(void);
void asm_inthandler2c(void);
void init_screen(char *vram, int xsize, int ysize);
void init_palette(void);
void set_palette(int start, int end, unsigned char *rgb); /*调色板函数*/
void boxfill8(unsigned char *vram, int xsize, unsigned char c, int x0, int y0, int x1, int y1);
void putfont8(char *vram, int xsize, int x, int y, char c, char *font);
void putfonts8_asc(char *vram, int xsize, int x, int y, char c, unsigned char *s);
void putblock8_8(char *vram, int vxsize, int pxsize,
	int pysize, int px0, int py0, char *buf, int bxsize);
void init_mouse_cursor8(char *mouse, char bc);
unsigned int memtest_sub(unsigned int start, unsigned int end);

#define COL8_000000		0
#define COL8_FF0000		1
#define COL8_00FF00		2
#define COL8_FFFF00		3
#define COL8_0000FF		4
#define COL8_FF00FF		5
#define COL8_00FFFF		6
#define COL8_FFFFFF		7
#define COL8_C6C6C6		8
#define COL8_840000		9
#define COL8_008400		10
#define COL8_848400		11
#define COL8_000084		12
#define COL8_840084		13
#define COL8_008484		14
#define COL8_848484		15



struct SEGMENT_DESCRIPTOR{
	short limit_low, base_low;
	char base_mid, access_right;
	char limit_high, base_high;
};

struct GATE_DESCRIPTOR{
	short offset_low, selector;
	char dw_count, access_right;
	short offset_high;
};

void init_gdtidt(void);
void set_segmdesc(struct SEGMENT_DESCRIPTOR *sd, unsigned int limit, int base, int ar);
void set_gatedesc(struct GATE_DESCRIPTOR *gd, int offset, int selector, int ar);
void load_gdtr(int limit, int addr);
void load_idtr(int limit, int addr);
#define ADR_IDT			0x0026f800
#define LIMIT_IDT		0x000007ff
#define ADR_GDT			0x00270000
#define LIMIT_GDT		0x0000ffff
#define ADR_BOTPAK		0x00280000
#define LIMIT_BOTPAK	0x0007ffff
#define AR_DATA32_RW	0x4092
#define AR_CODE32_ER	0x409a
#define AR_INTGATE32	0x008e

/*int.c*/

void init_pic(void);
void inthandler21(int *esp);
void inthandler2c(int *esp);
void inthandler27(int *esp);
#define  PIC0_ICW1				0x0020
#define  PIC0_OCW2				0x0020
#define  PIC0_IMR				0x0021
#define  PIC0_ICW2				0x0021
#define  PIC0_ICW3				0x0021
#define  PIC0_ICW4				0x0021
#define  PIC1_ICW1				0x00a0
#define  PIC1_OCW2				0x00a0
#define  PIC1_IMR				0x00a1
#define  PIC1_ICW2				0x00a1
#define  PIC1_ICW3				0x00a1
#define  PIC1_ICW4				0x00a1
#define PORT_KEYDAT				0x0060
#define PORT_KEYSTA				0x0064
#define PORT_KEYCMD				0x0064
#define KEYSTA_SEND_NOTREADY	0x02
#define KEYCMD_WRITE_MODE		0x60
#define KBC_MODE				0x47

struct MOUSE_DEC {
	unsigned char buf[3], phase;
	int x, y, btn;
};/*存放鼠标解码数据的结构体*/

void wait_KBC_sendready(void);
void init_keyboard(void);
#define KEYCMD_SENDTO_MOUSE 	0xd4
#define MOUSECMD_ENABLE			0xf4
void enable_mouse(struct MOUSE_DEC *mdec);
int mouse_decode(struct MOUSE_DEC *mdec, unsigned char dat);
/*struct KEYBUF{
	unsigned char data[32];
	int next_r, next_w, len;
};
*/
/*fifo.c*/

struct FIFO8{
	unsigned char *buf;
	int p, q, 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);

/*memory.c*/
#define EFLAGS_AC_BIT		0x00040000
#define CR0_CACHE_DISABLE	0x60000000
#define MEMMAN_FREES		4090		/*大约是32K*/
#define MEMMAN_ADDR			0x003c0000

struct FREEINFO {
	unsigned int addr, size;
};

struct MEMMAN {
	int frees, maxfrees, lostsize, losts;
	struct FREEINFO free[MEMMAN_FREES];
};
unsigned int memtest(unsigned int start, unsigned int end);
void memman_init(struct MEMMAN *man);
unsigned int memman_total(struct MEMMAN *man);
unsigned int memman_alloc(struct MEMMAN *man, unsigned int size);
int memman_free(struct MEMMAN *man, unsigned int addr, unsigned int size);
unsigned int memman_alloc_4k(struct MEMMAN *man, unsigned int size);
int memman_free_4k(struct MEMMAN *man, unsigned int addr, unsigned int size);


/*sheet.c*/
#define MAX_SHEETS		256
#define SHEET_USE		1
struct SHEET {
	unsigned char *buf;
	int bxsize, bysize, vx0, vy0, col_inv, height, flags;
};

struct SHTCTL{
	unsigned char *vram;
	int xsize, ysize, top;
	struct SHEET *sheets[MAX_SHEETS];
	struct SHEET sheets0[MAX_SHEETS];
};

struct SHTCTL *shtctl_init(struct MEMMAN *memman, unsigned char *vram, int xsize, int ysize);
struct SHEET *sheet_alloc(struct SHTCTL *ctl);
void sheet_setbuf(struct SHEET *sht, unsigned char *buf, int xsize, int ysize, int col_inv);
void sheet_updown(struct SHTCTL *ctl, struct SHEET *sht, int height);
void sheet_refresh(struct SHTCTL *ctl, struct SHEET *sht, int bx0, int by0, int bx1, int by1);
void sheet_slide(struct SHTCTL *ctl, struct SHEET *sht, int vx0, int vy0);
void sheet_free(struct SHTCTL *ctl, struct SHEET *sht);
void sheet_refreshsub(struct SHTCTL *ctl, int vx0, int vy0, int vx1, int vy1);

Makefile只需修改下面的内容即可:

OBJS_BOOTPACK = bootpack.obj naskfunc.obj yezfont.obj graphic.obj dsctbl.obj \
				int.obj fifo.obj keyboard.obj mouse.obj memory.obj sheet.obj

naskfunc.nas

; naskfunc
; TAB=4

[FORMAT "WCOFF"]				 
[INSTRSET "i486p"]				 
[BITS 32]						 
[FILE "naskfunc.nas"]			 

		GLOBAL	_io_hlt,_write_mem8
		GLOBAL	_io_cli, _io_sti, _io_stihlt
		GLOBAL	_io_in8, _io_in16, _io_in32
		GLOBAL	_io_out8, _io_out16, _io_out32
		GLOBAL	_io_load_eflags, _io_store_eflags
		GLOBAL	_load_gdtr, _load_idtr
		GLOBAL  _load_cr0, _store_cr0
		GLOBAL	_asm_inthandler21, _asm_inthandler27, _asm_inthandler2c
		GLOBAL	_memtest_sub
		EXTERN	_inthandler21, _inthandler2c, _inthandler27

[SECTION .text]

_io_hlt:	; void io_hlt(void);
		HLT
		RET

_write_mem8:	; void write_mem8(int addr, int data);
		MOV		ECX,[ESP+4]		;  
		MOV		AL,[ESP+8]		;  
		MOV		[ECX],AL
		RET
 
_io_cli:	;void io_cli(void);
		CLI
		RET
		
_io_sti:	;void io_sti(void);
		STI
		RET
	
_io_stihlt:		;void io_stihlt(void);
		STI
		HLT
		RET
	
_io_in8:			;int io_in8(int port);
		MOV		EDX, [ESP+4]	;port
		MOV		EAX, 0
		IN		AL, DX
		RET
		
_io_in16:		;int io_in16(int port);
		MOV		EDX, [ESP+4]
		MOV		EAX, 0
		IN		AX, DX
		RET
		
_io_in32:		;io_in32(int port);
		MOV		EDX, [ESP+4]
		IN		EAX, DX
		RET
		
_io_out8:		;io_out8(int port, int data);
		MOV		EDX, [ESP+4] 	;PORT
		MOV		AL, [ESP+8]		;DATA
		OUT		DX, AL
		RET
		
_io_out16:		;io_out16(int port, int data);
		MOV		EDX, [ESP+4]
		MOV		EAX, [ESP+8]
		OUT		DX, AX
		
_io_out32:		;io_out32(int port, int data);
		MOV		EDX, [ESP+4]
		MOV		EAX, [ESP+8]
		OUT		DX, EAX
		RET

_io_load_eflags:	;io_load_eflags(int eflags);
		PUSHFD
		POP		EAX
		RET
		
_io_store_eflags:	;io_store_eflags(int eflags);
		MOV		EAX, [ESP+4]
		PUSH	EAX
		POPFD
		RET
		
_load_gdtr:		;void load_gdtr(int limit, int addr);
		MOV		AX,[ESP+4]		;limit
		MOV 	[ESP+6], AX
		LGDT	[ESP+6]
		RET
		
_load_idtr:		;void load_idtr(int limit, int addr);
		MOV		AX, [ESP+4]
		MOV		[ESP+6], AX
		LIDT	[ESP+6]
		RET
		
_load_cr0:		;int load_cr0(void)
		MOV		EAX, CR0
		RET
		
_store_cr0:		;void load_cr0(int cr0)
		MOV		EAX, [ESP+4]
		MOV		CR0, EAX
		RET
		
_asm_inthandler21:
		PUSH	ES
		PUSH	DS
		PUSHAD
		MOV		EAX, ESP
		PUSH 	EAX
		MOV		AX, SS
		MOV		DS, AX
		MOV		ES,	AX
		CALL	_inthandler21
		POP		EAX
		POPAD
		POP		DS
		POP		ES
		IRETD
		
_asm_inthandler27:
		PUSH	ES
		PUSH	DS
		PUSHAD
		MOV		EAX, ESP	
		PUSH	EAX
		MOV		AX, SS
		MOV		DS, AX
		MOV		ES, AX
		CALL	_inthandler27
		POP		EAX
		POPAD
		POP		DS
		POP		ES
		IRETD
		
_asm_inthandler2c:
		PUSH	ES
		PUSH	DS
		PUSHAD
		MOV		EAX, ESP
		PUSH	EAX
		MOV		AX, SS
		MOV		DS, AX
		MOV		ES, AX
		CALL	_inthandler2c
		POP		EAX
		POPAD
		POP		DS
		POP		ES
		IRETD
		
_memtest_sub:		; unsigned int memtest_sub(unsigned int start, unsigned int end);
		PUSH	EDI
		PUSH	ESI
		PUSH	EBX
		MOV		ESI, 0xaa55aa55
		MOV		EDI, 0x55aa55aa
		MOV		EAX, [ESP+12+4]
mts_loop:
		MOV		EBX, EAX
		ADD		EBX, 0xffc
		MOV		EDX, [EBX]
		MOV		[EBX], ESI
		XOR		DWORD [EBX], 0xffffffff
		CMP		EDI, [EBX]
		JNE		mts_fin
		XOR		DWORD [EBX], 0xffffffff
		CMP		ESI, [EBX]
		JNE		mts_fin
		MOV		[EBX], EDX
		ADD		EAX, 0x1000
		CMP		EAX, [ESP+12+8]
		JBE		mts_loop
		POP		EBX
		POP		ESI
		POP		EDI
		RET
mts_fin:
		MOV		[EBX], EDX
		POP		EBX
		POP		ESI
		POP		EDI
		RET

最后是我们的主函数所在的源文件:

/* filename:    bootpack.c
 * description: the UcanMain()file
 * author:      Howard
 * date:        2013-11-28
 * version:     v1.0
 */


#include <stdio.h>
#include "bootpack.h"


extern struct FIFO8 keyfifo, mousefifo;

void UcanMain(void)
{
	
	char *vram;
	char s[50], mcursor[256], keybuf[32], mousebuf[128];
	int mx, my;//鼠标的(x,y)
	int xsize, ysize;
	int i;
	unsigned int memtotal;
	struct MOUSE_DEC mdec; /*鼠标解码缩放的数据*/
	struct SHTCTL *shtctl;
	struct SHEET *sht_back, *sht_mouse;
	unsigned char *buf_back, buf_mouse[256];
	struct MEMMAN *memman = (struct MEMMAN *) MEMMAN_ADDR;
	
	struct BOOTINFO *binfo = (struct BOOTINFO *) 0x0ff0;
			 
	init_gdtidt();
	init_pic();
	io_sti();
	
	fifo8_init(&keyfifo, 32, keybuf);
	fifo8_init(&mousefifo, 128, mousebuf);
	io_out8(PIC0_IMR, 0xf9);
	io_out8(PIC1_IMR, 0xef);
	
	init_keyboard();
	enable_mouse(&mdec);
	
	memtotal = memtest(0x00400000, 0xbfffffff);
	memman_init(memman);
	memman_free(memman, 0x00001000, 0x0009e000);
	memman_free(memman, 0x00400000, memtotal - 0x00400000);
	
	init_palette();
	
	shtctl = shtctl_init(memman, binfo->vram, binfo->scrnx, binfo->scrny);
	sht_back = sheet_alloc(shtctl);
	sht_mouse = sheet_alloc(shtctl);
	buf_back = (unsigned char *)memman_alloc_4k(memman, binfo->scrnx*binfo->scrny);
	sheet_setbuf(sht_back, buf_back, binfo->scrnx, binfo->scrny, -1);
	sheet_setbuf(sht_mouse, buf_mouse, 16, 16, 99);
	
	//init_screen(binfo->vram, binfo->scrnx, binfo->scrny);
	init_screen(buf_back, binfo->scrnx, binfo->scrny);
	init_mouse_cursor8(buf_mouse, 99);
	sheet_slide(shtctl, sht_back, 0, 0);
	//init_mouse_cursor8(mcursor, 99);
	xsize = (*binfo).scrnx;
	ysize = (*binfo).scrny;
	vram = (*binfo).vram;
	
	
	mx = (binfo->scrnx-16) / 2;
	my = (binfo->scrny-28-16) /2;
	sheet_slide(shtctl, sht_mouse, mx, my);
	sheet_updown(shtctl, sht_back,  0);
	sheet_updown(shtctl, sht_mouse, 1);
	//putblock8_8(buf_back, binfo->scrnx, 16, 16, mx, my, buf_mouse, 16);
	putfonts8_asc(buf_back, binfo->scrnx, 8, 8, COL8_FFFFFF, "Hello, world!");
	putfonts8_asc(buf_back, binfo->scrnx, 31, 31, COL8_000000, "Ucan23-OS");
	putfonts8_asc(buf_back, binfo->scrnx, 30, 30, COL8_FFFFFF, "Ucan23-OS");
	
	sprintf(s, "scrnx = %d", binfo->scrnx);
	putfonts8_asc(buf_back, binfo->scrnx, 16, 64, COL8_FFFFFF, s);
	
	sprintf(s, "(%d, %d)", mx, my);
	putfonts8_asc(buf_back, binfo->scrnx, mx+16, my+16, COL8_FFFFFF, s);
	
	//io_out8(PIC0_IMR, 0xf9);
	//io_out8(PIC1_IMR, 0xef);
	
	
	sprintf(s, "Memory %dMB  free: %dKB", memtotal/(1024*1024), memman_total(memman)/1024);
	putfonts8_asc(buf_back, binfo->scrnx, 0, 136, COL8_FFFFFF, s);
	
	sheet_refresh(shtctl, sht_back, 0, 0, binfo->scrnx, binfo->scrny);
	
	for (;;){
		io_cli();    /*执行nashfunc.nas里的_io_hlt*/
		if (0==(fifo8_status(&keyfifo)+fifo8_status(&mousefifo))){
			io_stihlt();
		} else {
			if (0!=fifo8_status(&keyfifo)){
				i = fifo8_get(&keyfifo);
				io_sti();
				sprintf(s, "%02X", i);
				boxfill8(buf_back, binfo->scrnx, COL8_000000, 0, 120, 15, 135);
				putfonts8_asc(buf_back, binfo->scrnx, 0, 120, COL8_FFFFFF, s);
				sheet_refresh(shtctl,sht_back, 0, 120, 15*8, 136);
			} else if (0!=fifo8_status(&mousefifo))
			{
				i = fifo8_get(&mousefifo);
				io_sti();
				if (0 != mouse_decode(&mdec, i)){					
					sprintf(s, "[lcr %4d %4d]", mdec.x, mdec.y);
					if ((mdec.btn & 0x01)!=0){
						s[1] = 'L';
					}
					if ((mdec.btn & 0x02)!=0){
						s[3] = 'R';
					}
					if ((mdec.btn & 0x04)!=0){
						s[2] = 'C';
					}
					boxfill8(buf_back, binfo->scrnx, COL8_008484, 32, 120, 32+15*8-1,135);
					putfonts8_asc(buf_back, binfo->scrnx, 32, 120, COL8_FFFFFF, s);
					sheet_refresh(shtctl, sht_back, 32, 120, 32+15*8, 136);
					/*鼠标指针的移动*/
					//boxfill8(buf_back, binfo->scrnx, COL8_008484, mx, my, mx+15, my+15);/*隐藏鼠标*/
					mx += mdec.x;
					my += mdec.y;
					if (mx<0){
						mx = 0;
					}
					if (my<0){
						my = 0;
					}
					if (mx>binfo->scrnx-16){
						mx = binfo->scrnx-16;
					}
					if (my>binfo->scrny-16){
						my = binfo->scrny-16;
					}
					sprintf(s, "(%3d, %3d)", mx, my);
					boxfill8(buf_back, binfo->scrnx, COL8_008484,binfo->scrnx-100,120,binfo->scrnx , 136);
					putfonts8_asc(buf_back, binfo->scrnx, binfo->scrnx-100, 120, COL8_FFFFFF,s);
					sheet_refresh(shtctl, sht_back, binfo->scrnx-100, 120, binfo->scrnx, 136);
					//putblock8_8(binfo->vram,binfo->scrnx, 16, 16, mx, my, mcursor, 16);
					sheet_slide(shtctl, sht_mouse, mx, my);
				}
			}		
		}
	}
}




版权声明:本文为博主原创文章,未经博主允许不得转载。 举报

相关文章推荐

30天自制操作系统笔记(十一十二)——源码

这里我们只给出新增部分和修改部分的源码: 系统运行截图http://blog.csdn.net/ucan23/article/details/17088983点击打开链接 /* filename:...
  • cnxxrj
  • cnxxrj
  • 2013-12-03 08:41
  • 1512

30天自制操作系统笔记(十三十四)——源码

我们重写了缓冲区,使其能进行键盘和鼠标的读取 /* filename: fifo.c * description: 包含了有关缓冲区的操作 * author: Howard * ...
  • cnxxrj
  • cnxxrj
  • 2013-12-04 08:48
  • 1819

我是如何成为一名python大咖的?

人生苦短,都说必须python,那么我分享下我是如何从小白成为Python资深开发者的吧。2014年我大学刚毕业..

《30天自制操作系统》学习笔记——第1-3天

这本书作者是日本人,虽然书翻译为中文,但是源代码里注释都是日文,对不懂日语的人来说看起来不够方便,我把书本中给出的注释翻译,以及书本介绍的一些知识,加到了注释里,阅读起来能够方便些。前3天主要就是写一...
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)