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

原创 2013年12月03日 08:41:12

这里我们只给出新增部分和修改部分的源码:

系统运行截图http://blog.csdn.net/ucan23/article/details/17088983点击打开链接

/* filename:    timer.c
 * description: 定时器函数
 * author:      Howard
 * date:        2013-12-02
 * version:     v1.0
 */
 
 #include "bootpack.h"
 
 struct TIMERCTL timerctl;
 
 void init_pit(void)
{
	int i;
	io_out8(PIT_CTRL, 0x34);
	io_out8(PIT_CNT0, 0x9c);
	io_out8(PIT_CNT0, 0x2e);
	timerctl.count = 0;
	timerctl.next = 0xffffffff;
	for (i=0; i<MAX_TIMER; i++){
		timerctl.timers0[i].flags = 0;   /*未使用*/
	}
	return;
}

struct TIMER *timer_alloc(void)
{
	int i;
	for (i=0; i<MAX_TIMER; i++){
		if (timerctl.timers0[i].flags == 0){
			timerctl.timers0[i].flags = TIMER_FLAGS_ALLOC;
			return &timerctl.timers0[i];
		}
	}
	return 0;
}

void timer_free(struct TIMER *timer)
{
	timer->flags = 0;
	return;
}

void timer_init(struct TIMER *timer, struct FIFO8 *fifo, unsigned char data)
{
	timer->fifo = fifo;
	timer->data = data;
	return;
}

void timer_settime(struct TIMER *timer, unsigned int timeout)
{
	int e, i, j; 
	timer->timeout = timeout + timerctl.count;
	timer->flags = TIMER_FLAGS_USING;
	e = io_load_eflags();
	io_cli();
	for (i=0; i<timerctl.using; i++){
		if (timerctl.timers[i]->timeout>timer->timeout){
			break;
		}
	}
	for (j=timerctl.using; j>i; j--){
		timerctl.timers[j] = timerctl.timers[j-1];
	}
	timerctl.using ++;
	timerctl.timers[i] = timer;
	timerctl.next = timerctl.timers[0]->timeout;
	io_store_eflags(e);
	return;
}
void inthandler20(int *esp)
{
	int i, j;
	io_out8(PIC0_OCW2, 0x60);
	timerctl.count ++;
	if (timerctl.next>timerctl.count){
		return;
	}
	
	for (i=0; i<timerctl.using; i++){
		if (timerctl.timers[i]->timeout>timerctl.count){
			break;
		}
		timerctl.timers[i]->flags = TIMER_FLAGS_ALLOC;
		fifo8_put(timerctl.timers[i]->fifo, timerctl.timers[i]->data);
	} 
	timerctl.using -=i;
	for (j=0; j<timerctl.using; j++){
		timerctl.timers[j] = timerctl.timers[i+j];
	}
	if (timerctl.using>0){
		timerctl.next = timerctl.timers[0]->timeout;
	}else{
		timerctl.next = 0xffffffff;
	}

	return;
}
/* 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->map = (unsigned char *) memman_alloc_4k(memman, xsize*ysize);
	if (0==ctl->map) {
		memman_free_4k(memman, (int) ctl, sizeof (struct SHTCTL));
		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; /*标记为未使用*/
		ctl->sheets0[i].ctl = ctl; /*记录所属*/
	}
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_refreshmap(struct SHTCTL *ctl, int vx0, int vy0, int vx1, int vy1, int h0)
{
	int h, bx, by, vx, vy, bx0, by0, bx1, by1;
	unsigned char *buf, sid, *map = ctl->map;
	struct SHEET *sht;
	if (vx0 < 0) { vx0 = 0; }
	if (vy0 < 0) { vy0 = 0; }
	if (vx1 > ctl->xsize) { vx1 = ctl->xsize; }
	if (vy1 > ctl->ysize) { vy1 = ctl->ysize; }
	for (h = h0; h <= ctl->top; h++) {
		sht = ctl->sheets[h];
		sid = sht - ctl->sheets0; 
		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;
				if (buf[by * sht->bxsize + bx] != sht->col_inv) {
					map[vy * ctl->xsize + vx] = sid;
				}
			}
		}
	}
	return;
}

void sheet_refreshsub(struct SHTCTL *ctl, int vx0, int vy0, int vx1, int vy1, int h0, int h1)
{
	int h, bx, by, vx, vy, bx0, by0, bx1, by1;
	unsigned char *buf, *vram = ctl->vram, *map = ctl->map, sid;
	struct SHEET *sht;
	/*如果refresh超出了画面则修正*/
	if (vx0<0){
		vx0 = 0;
	}
	if (vy0<0){
		vy0 = 0;
	}
	if (vx1>ctl->xsize){
		vx1 = ctl->xsize;
	}
	if (vy1>ctl->ysize){
		vy1 = ctl->ysize;
	}
	for (h = h0; h <= h1; h++) {
		sht = ctl->sheets[h];
		buf = sht->buf;
		sid = sht - ctl->sheets0;
		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 (sid == map[vy*ctl->xsize+vx]) {
						vram[vy * ctl->xsize + vx] = buf[by*sht->bxsize+bx];
					}
			}
		}
	}
	return;
}

void sheet_updown(struct SHEET *sht, int height)
{
	struct SHTCTL *ctl = sht->ctl;
	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;
			sheet_refreshmap(ctl, sht->vx0, sht->vy0, sht->vx0 + sht->bxsize, sht->vy0 + sht->bysize, height + 1);
			sheet_refreshsub(ctl, sht->vx0, sht->vy0, sht->vx0 + sht->bxsize, sht->vy0 + sht->bysize, height + 1, old);
		} 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_refreshmap(ctl, sht->vx0, sht->vy0, sht->vx0 + sht->bxsize, sht->vy0 + sht->bysize, 0);
			sheet_refreshsub(ctl, sht->vx0, sht->vy0, sht->vx0 + sht->bxsize, sht->vy0 + sht->bysize, 0, old-1);
		}
		//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);
		sheet_refreshmap(ctl, sht->vx0, sht->vy0, sht->vx0 + sht->bxsize, sht->vy0 + sht->bysize, height);
		sheet_refreshsub(ctl, sht->vx0, sht->vy0, sht->vx0 + sht->bxsize, sht->vy0 + sht->bysize, height, height);
	}
	return;
}

void sheet_refresh(struct SHEET *sht, int bx0, int by0, int bx1, int by1)
{
	if (sht->height >= 0) { 
		sheet_refreshsub(sht->ctl, sht->vx0 + bx0, sht->vy0 + by0, sht->vx0 + bx1, sht->vy0 + by1, sht->height, sht->height);
	}
	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 SHEET *sht, int vx0, int vy0)
{
	struct SHTCTL *ctl = sht->ctl;
	int old_vx0 = sht->vx0, old_vy0 = sht->vy0;
	sht->vx0 = vx0;
	sht->vy0 = vy0;
	if (sht->height>=0){
		//sheet_refresh(ctl);
		//sheet_refreshsub(sht->ctl, old_vx0, old_vy0, old_vx0 + sht->bxsize, old_vy0 + sht->bysize, 0);
		//sheet_refreshsub(sht->ctl, vx0, vy0, vx0 + sht->bxsize, vy0 + sht->bysize, sht->height);
		sheet_refreshmap(ctl, old_vx0, old_vy0, old_vx0 + sht->bxsize, old_vy0 + sht->bysize, 0);
		sheet_refreshmap(ctl, vx0, vy0, vx0 + sht->bxsize, vy0 + sht->bysize, sht->height);
		sheet_refreshsub(ctl, old_vx0, old_vy0, old_vx0 + sht->bxsize, old_vy0 + sht->bysize, 0, sht->height - 1);
		sheet_refreshsub(ctl, vx0, vy0, vx0 + sht->bxsize, vy0 + sht->bysize, sht->height, sht->height);
	}
	return;
}
void sheet_free(struct SHEET *sht)
{
	if (sht->height>=0){
		sheet_updown(sht, -1);
	}
	sht->flags = 0;
	return;
}

dsctbl.nas包含了对中断的处理的函数

[FORMAT "WCOFF"]
[INSTRSET "i486p"]
[OPTIMIZE 1]
[OPTION 1]
[BITS 32]
	EXTERN	_load_gdtr
	EXTERN	_load_idtr
	EXTERN	_asm_inthandler20
	EXTERN	_asm_inthandler21
	EXTERN	_asm_inthandler27
	EXTERN	_asm_inthandler2c
[FILE "dsctbl.c"]
[SECTION .text]
	GLOBAL	_init_gdtidt
_init_gdtidt:
	PUSH	EBP
	MOV	EBP,ESP
	PUSH	ESI
	PUSH	EBX
	MOV	ESI,2555904
	MOV	EBX,8191
L6:
	PUSH	0
	PUSH	0
	PUSH	0
	PUSH	ESI
	ADD	ESI,8
	CALL	_set_segmdesc
	ADD	ESP,16
	DEC	EBX
	JNS	L6
	PUSH	16530
	MOV	ESI,2553856
	PUSH	0
	MOV	EBX,255
	PUSH	-1
	PUSH	2555912
	CALL	_set_segmdesc
	PUSH	16538
	PUSH	2621440
	PUSH	524287
	PUSH	2555920
	CALL	_set_segmdesc
	ADD	ESP,32
	PUSH	2555904
	PUSH	65535
	CALL	_load_gdtr
	POP	EAX
	POP	EDX
L11:
	PUSH	0
	PUSH	0
	PUSH	0
	PUSH	ESI
	ADD	ESI,8
	CALL	_set_gatedesc
	ADD	ESP,16
	DEC	EBX
	JNS	L11
	PUSH	2553856
	PUSH	2047
	CALL	_load_idtr
	PUSH	142
	PUSH	16
	PUSH	_asm_inthandler20
	PUSH	2554112
	CALL	_set_gatedesc
	PUSH	142
	PUSH	16
	PUSH	_asm_inthandler21
	PUSH	2554120
	CALL	_set_gatedesc
	ADD	ESP,40
	PUSH	142
	PUSH	16
	PUSH	_asm_inthandler27
	PUSH	2554168
	CALL	_set_gatedesc
	PUSH	142
	PUSH	16
	PUSH	_asm_inthandler2c
	PUSH	2554208
	CALL	_set_gatedesc
	LEA	ESP,DWORD [-8+EBP]
	POP	EBX
	POP	ESI
	POP	EBP
	RET
	GLOBAL	_set_segmdesc
_set_segmdesc:
	PUSH	EBP
	MOV	EBP,ESP
	PUSH	EBX
	MOV	EDX,DWORD [12+EBP]
	MOV	ECX,DWORD [16+EBP]
	MOV	EBX,DWORD [8+EBP]
	MOV	EAX,DWORD [20+EBP]
	CMP	EDX,1048575
	JBE	L17
	SHR	EDX,12
	OR	EAX,32768
L17:
	MOV	WORD [EBX],DX
	MOV	BYTE [5+EBX],AL
	SHR	EDX,16
	SAR	EAX,8
	AND	EDX,15
	MOV	WORD [2+EBX],CX
	AND	EAX,-16
	SAR	ECX,16
	OR	EDX,EAX
	MOV	BYTE [4+EBX],CL
	MOV	BYTE [6+EBX],DL
	SAR	ECX,8
	MOV	BYTE [7+EBX],CL
	POP	EBX
	POP	EBP
	RET
	GLOBAL	_set_gatedesc
_set_gatedesc:
	PUSH	EBP
	MOV	EBP,ESP
	PUSH	EBX
	MOV	EDX,DWORD [8+EBP]
	MOV	EAX,DWORD [16+EBP]
	MOV	EBX,DWORD [20+EBP]
	MOV	ECX,DWORD [12+EBP]
	MOV	WORD [2+EDX],AX
	MOV	BYTE [5+EDX],BL
	MOV	WORD [EDX],CX
	MOV	EAX,EBX
	SAR	EAX,8
	SAR	ECX,16
	MOV	BYTE [4+EDX],AL
	MOV	WORD [6+EDX],CX
	POP	EBX
	POP	EBP
	RET

dsctbl.c文件增加了这么一行

	set_gatedesc(idt + 0x20, (int) asm_inthandler20, 2 * 8, AR_INTGATE32);

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 \
				timer.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_inthandler20, _asm_inthandler21
		GLOBAL 	_asm_inthandler27, _asm_inthandler2c
		GLOBAL	_memtest_sub
		EXTERN	_inthandler20, _inthandler21
		EXTERN  _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_inthandler20:
		PUSH	ES
		PUSH	DS
		PUSHAD
		MOV		EAX, ESP
		PUSH	EAX
		MOV		AX, SS
		MOV		DS, AX
		MOV 	ES, AX
		CALL	_inthandler20
		POP		EAX
		POPAD
		POP		DS
		POP		ES
		IRETD
		
_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], timerbuf[8], timerbuf2[8], timerbuf3[8];
	int mx, my;//鼠标的(x,y)
	int xsize, ysize;
	int i;
	unsigned int memtotal, count = 0;
	struct MOUSE_DEC mdec; /*鼠标解码缩放的数据*/
	struct SHTCTL *shtctl;
	struct FIFO8 timerfifo, timerfifo2, timerfifo3;
	struct TIMER *timer, *timer2, *timer3;
	struct SHEET *sht_back, *sht_mouse, *sht_win;
	unsigned char *buf_back, buf_mouse[256], *buf_win;
	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);
	fifo8_init(&timerfifo, 8, timerbuf);
	fifo8_init(&timerfifo2, 8, timerbuf2);
	fifo8_init(&timerfifo3, 8, timerbuf3);
	
	init_pit();
	timer = timer_alloc();
	timer2 = timer_alloc();
	timer3 = timer_alloc();
	
	timer_init(timer, &timerfifo, 1);
	timer_init(timer2, &timerfifo2, 1);
	timer_init(timer3, &timerfifo3, 1);
	
	timer_settime(timer, 1000);
	timer_settime(timer2, 300);
	timer_settime(timer3, 50);
	//settimer(1000, &timerfifo, 1);
	io_out8(PIC0_IMR, 0xf8);
	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);
	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_back, buf_back, binfo->scrnx, binfo->scrny, -1);
	sheet_setbuf(sht_mouse, buf_mouse, 16, 16, 99);
	sheet_setbuf(sht_win, buf_win, 160, 52, -1);
	
	//init_screen(binfo->vram, binfo->scrnx, binfo->scrny);
	init_screen(buf_back, binfo->scrnx, binfo->scrny);
	init_mouse_cursor8(buf_mouse, 99);
	make_window8(buf_win, 160, 52, "Counter");
	//putfonts8_asc(buf_win, 160, 24, 28, COL8_000000, "Welcome to");
	//putfonts8_asc(buf_win, 160, 24, 44, COL8_000000, "      Ucan-OS!");
	sheet_slide(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(sht_mouse, mx, my);
	sheet_slide(sht_win, 80, 72);
	sheet_updown(sht_back,  0);
	sheet_updown(sht_win,   1);
	sheet_updown(sht_mouse, 2);
	//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(sht_back, 0, 0, binfo->scrnx, binfo->scrny);
	
	for (;;){
		/*计数器程序*/
		//count ++;
		sprintf(s, "%010d", timerctl.count);
		boxfill8(buf_win, 160, COL8_C6C6C6, 40, 28, 119, 43);
		putfonts8_asc(buf_win, 160,  40, 28, COL8_000000, s);
		sheet_refresh(sht_win, 40, 28, 120, 44);
		
		io_cli();    /*执行nashfunc.nas里的_io_hlt*/
		if (0==(fifo8_status(&keyfifo)+fifo8_status(&mousefifo)+fifo8_status(&timerfifo)+ \
				fifo8_status(&timerfifo2)+fifo8_status(&timerfifo3))){
			io_sti();
		} 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(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(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-1){
						mx = binfo->scrnx-1;
					}
					if (my>binfo->scrny-1){
						my = binfo->scrny-1;
					}
					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(sht_back, binfo->scrnx-100, 120, binfo->scrnx, 136);
					//putblock8_8(binfo->vram,binfo->scrnx, 16, 16, mx, my, mcursor, 16);
					sheet_slide(sht_mouse, mx, my);
				}
			} else if (0!=fifo8_status(&timerfifo))	{
				i = fifo8_get(&timerfifo);
				io_sti();
				//boxfill8(buf_back, binfo->scrnx, COL8_008484,binfo->scrnx-100,0,binfo->scrnx , 16);
				putfonts8_asc(buf_back, binfo->scrnx, binfo->scrnx-100, 0, COL8_FFFFFF, "10[sec]" );
				sheet_refresh(sht_back, binfo->scrnx-100, 0, binfo->scrnx, 16);
				//putfonts8_asc(buf_back, binfo->scrnx, 0, 64, COL8_FFFFFF, "10[sec]" );
				//sheet_refresh(sht_back, 0, 64, 56, 80);
			} else if(0!=fifo8_status(&timerfifo2)) {
				i = fifo8_get(&timerfifo2);
				io_sti();
				putfonts8_asc(buf_back, binfo->scrnx, binfo->scrnx-100, 16, COL8_FFFFFF, "3[sec]" );
				sheet_refresh(sht_back, binfo->scrnx-100, 16, binfo->scrnx, 32);
				
			} else if (0!=fifo8_status(&timerfifo3)){
				i = fifo8_get(&timerfifo3);
				io_sti();
				if (0!=i){
					timer_init(timer3, &timerfifo3, 0);
					putfonts8_asc(buf_back, binfo->scrnx, binfo->scrnx-100, 32, COL8_FFFFFF, "1" );
					//boxfill8(buf_back, binfo->scrnx, COL8_FFFFFF, binfo->scrnx-92, 32, binfo->scrnx-98, 48);
				}else {
					timer_init(timer3, &timerfifo3, 1);
					putfonts8_asc(buf_back, binfo->scrnx, binfo->scrnx-100, 32, COL8_008484, "1" );
					//boxfill8(buf_back, binfo->scrnx, COL8_008484, binfo->scrnx-92, 32, binfo->scrnx-98, 48);
				}
				timer_settime(timer3, 50);
				sheet_refresh(sht_back, binfo->scrnx-100, 32, binfo->scrnx, 48);
			}
		}
	}
}

void make_window8(unsigned char *buf, int xsize, int ysize, char *title)
{
	static char closebtn[14][16] = {
		"OOOOOOOOOOOOOOO@",
		"OQQQQQQQQQQQQQ$@",
		"OQQQQQQQQQQQQQ$@",
		"OQQQ@@QQQQ@@QQ$@",
		"OQQQQ@@QQ@@QQQ$@",
		"OQQQQQ@@@@QQQQ$@",
		"OQQQQQQ@@QQQQQ$@",
		"OQQQQQ@@@@QQQQ$@",
		"OQQQQ@@QQ@@QQQ$@",
		"OQQQ@@QQQQ@@QQ$@",
		"OQQQQQQQQQQQQQ$@",
		"OQQQQQQQQQQQQQ$@",
		"O$$$$$$$$$$$$$$@",
		"@@@@@@@@@@@@@@@@"
	};
	int x, y;
	char c;
	boxfill8(buf, xsize, COL8_C6C6C6, 0,         0,         xsize - 1, 0        );
	boxfill8(buf, xsize, COL8_FFFFFF, 1,         1,         xsize - 2, 1        );
	boxfill8(buf, xsize, COL8_C6C6C6, 0,         0,         0,         ysize - 1);
	boxfill8(buf, xsize, COL8_FFFFFF, 1,         1,         1,         ysize - 2);
	boxfill8(buf, xsize, COL8_848484, xsize - 2, 1,         xsize - 2, ysize - 2);
	boxfill8(buf, xsize, COL8_000000, xsize - 1, 0,         xsize - 1, ysize - 1);
	boxfill8(buf, xsize, COL8_C6C6C6, 2,         2,         xsize - 3, ysize - 3);
	boxfill8(buf, xsize, COL8_000084, 3,         3,         xsize - 4, 20       );
	boxfill8(buf, xsize, COL8_848484, 1,         ysize - 2, xsize - 2, ysize - 2);
	boxfill8(buf, xsize, COL8_000000, 0,         ysize - 1, xsize - 1, ysize - 1);
	putfonts8_asc(buf, xsize, 24, 4, COL8_FFFFFF, title);
	for (y = 0; y < 14; y++) {
		for (x = 0; x < 16; x++) {
			c = closebtn[y][x];
			if (c == '@') {
				c = COL8_000000;
			} else if (c == '$') {
				c = COL8_848484;
			} else if (c == 'Q') {
				c = COL8_C6C6C6;
			} else {
				c = COL8_FFFFFF;
			}
			buf[(5 + y) * xsize + (xsize - 21 + x)] = c;
		}
	}
	return;
}



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

相关文章推荐

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

学完十一、十二天的内容感觉就是在处理层的优化,和显示方面有点难点,至于中断和定时器还是不是很难的: 下面的图是我们上次内容没有优化图层控制的结果,鼠标移出到画面外时会出现问题,感觉跟能穿墙的贪吃蛇很像...
  • cnxxrj
  • cnxxrj
  • 2013-12-03 08:33
  • 1473

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

这里为了节省篇幅和csdn服务器的空间,我们只给出在七八之上改动的或者新建的源码文件的内容: sheet.c /* filename: sheet.c * description: 对图层的管...
  • cnxxrj
  • cnxxrj
  • 2013-12-02 09:33
  • 1960

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

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

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

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

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

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

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