⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 kernel.c

📁 一个小型的操作系统,采用gcc进行开发,几千行的代码,方便初学者学习
💻 C
📖 第 1 页 / 共 3 页
字号:
/**  Snixos Project version 1.0, 2003.6*  (C) Copyright 2003,2004,2005 Jockeyson,KeqiangGao <Snallie@tom.com>*  All Rights Reserved.*  Distributed under the terms of the GNU General Public License.**  This program is a free and open source software and you can redistribute *  it and/or modify it under the terms of the GNU General Public License as*  published by the Free Software Foundation. As no any liability is assumed *  for any incidental or consequential damages in connection with the *  information or program fragments contained herein,so any exception arised*  is at your own risk. It is ABSOLUTELY WITHOUT ANY WARRANTY.*  Bug report please send to Snallie@tom.com .*//*  kernel.c: kernel of the Snixos OS project  Author  : snallie@tom.com  Time    : 2003.6  built   : $Date: 2005/08/02 01:05:56 $*//* to append a new task:call function: void newTask(unsigned int taskEntrance, char *taskname);*/#include "stdio.h"#include "string.h"#include "ctype.h"#include "mem.h"#include "fatfs.h"#include "asm.h"#include "io.h"#include "cmostime.h"#include "keyboard.h"#include "kernel.h"/* RCS keyword */static char rcsDate[] = "Built: $Date: 2005/08/02 01:05:56 $";#ifdef VIDEO_GRAPH_640X480const int videoMode = VIDGRAPH_640X480;#elseconst int videoMode = VIDTEXT_80X25;#endif#define beepup() asm("pushl $1000\t\n" "call beep\t\n" "add $4,%esp\n")	/* beep for 1sec */void threadTask();void cnverTask();void beep2(int duration, int freq);typedef struct melody {    int freq;    int duration;} music;/* kernel start here */kernelEntrance(){    if (videoMode == VIDTEXT_80X25) {	screen_init();	ver();	printf("\ntype help for available commands\n\n%s", PROMPT);    } else {	VGAScrInit();    }    initInterrupts();    setPITspeed(1000);		/* PIT interrupt frequency 1000 Hz */    initMem();			/* init heap memory */    initFileSystem();		/* RAM disk file system initialization */    mkDemoFile();		/* create sample files in file system */    initMC();			/* init myvm virtual machine */    initTask();			/* init task structure, fill header with dummy data */    /*        create task to be run in system , the threadTask        is task running as thread and remember only less        than 16 tasks can be created here, and the ktask        must be the first to create     */    newTask((unsigned) &ktask, "ktask");	/* !! must be the first task to create !! */    newTask((unsigned) &task1, "task1");    newTask((unsigned) &task2, "task2");    newTask((unsigned) &threadTask, "thread1");    newTask((unsigned) &banner, "banner");    newTask((unsigned) &timeTask, "timetask");    newTask((unsigned) &motion, "motion");    newTask((unsigned) &threadTask, "thread2");    newTask((unsigned) &cnverTask, "verTask");    /* task created ok */    if (videoMode != VIDTEXT_80X25) {	wrfile("hzk16", (char *) 0x20000, HZK16SIZE);	/* file hzk16 created */	cnver();	wrfile("py.mb", (char *) 0x70000, PYMBSIZE);	/* file py.mb created */    }    asm("sti");			/* interrupt enabled */    while (1);			/* waiting for clock interrupt */}// typedef unsigned int word;// typedef unsigned char byte;// #define outb2(port,data) outb((data),(port))// extern void delayMS(int i);// #define delay_msec delayMS// // void sound(const word freq)// {//     word freqdiv;//     byte tmp;//     freqdiv = 1193180 / freq;//     outb2(0x43, 0xB6);          /* counter 2 select, binary 16-bit counter,//                                    first bits 0-7 *///     printf("H %d L %d\n",(unsigned char) (freqdiv >> 8),(unsigned char) freqdiv);//     outb2(0x42, (unsigned char) freqdiv);       /* first bits 0-7 *///     outb2(0x42, (unsigned char) (freqdiv >> 8));        /* then bits 8-15 *///     tmp = inb(0x61);//     if (tmp != (tmp | 3))       /* only output if bits are not correctly set *///         outb2(0x61, tmp | 3);// }void play(){    int i = 0;    music music1[] = {	330, 50,	392, 50,	330, 50,	294, 25,	330, 25,	392, 50,	330, 25,	294, 25,	330, 100,	330, 50,	392, 50,	330, 25,	294, 25,	262, 50,	294, 50,	330, 25,	392, 25,	294, 100,	262, 50,	262, 25,	220, 25,	196, 50,	196, 25,	220, 25,	262, 100,	294, 100,	330, 100,	262, 100,/*two tiger*/	262, 25,	294, 25,	330, 25,	262, 25,	262, 25,	294, 25,	330, 25,	262, 25,	330, 25,	349, 25,	392, 50,	330, 25,	349, 25,	392, 50,	392, 12,	440, 12,	392, 12,	349, 12,	330, 25,	262, 25,	392, 12,	440, 12,	392, 12,	349, 12,	330, 25,	262, 25,	294, 25,	196, 25,	262, 50,	294, 25,	196, 25,	262, 50,	0, 0,    };    while (music1[i].freq > 0) {	printf("%d %d %d: ", i, music1[i].freq, music1[i].duration);	beep2(music1[i].duration * 2, music1[i].freq);	i++;    }}void printHZ(int x, int y, char *s, int mode){				/* mode=0 hznm, mode=1 qwm */    FILE *fp;    unsigned char buffer[32];    int i;    unsigned char qh, wh;    unsigned long location;    if ((fp = fopen("hzk16", "r")) == NULL) {	printf("Can't open hzk16!\n");	return;    }    while (*s) {	if (0 == mode) {	/* via  hz internal code */	    qh = *s - 0xa0;	    wh = *(s + 1) - 0xa0;	} else {		/* via qw code */	    qh = *s;	    wh = *(s + 1);	}	location = (94 * (qh - 1) + (wh - 1)) * 32L;	fseek(fp, location, 0);	for (i = 0; i < 32; i++) {	    buffer[i] = fgetc(fp);	}	s += 2;			/*get the next HZ */	putHz(x, y, buffer);	/* write to screen positioned x,y */	y++;    }    fclose(fp);}void mkDemoFile(){    char tst[] = "  0. What is Snixos Project	Snixos Project is an experimental operating system designed by	Jockeyson,	KeqiangGao < Snallie@tom.com> aiming at multitask / multithread,	Chinese environment including Chinese characters input and ouput	etc ...    Enjoy fun !	snallie@tom.com, jockeyson@tom.com	Mon Jun 28 11:48:03 CST 2004 "; char tp[] =#include "sums.vms"    char *tt =#include "hanoi.vms"    wrfile("sums.vms", tp, strlen(tp));    wrfile("hanoi.vms", tt, strlen(tt));    wrfile("readme", tst, strlen(tst));}/* print 16x16 hz on x,y in 640*480 graph mode , monochrome *//* x range:0~24,y range:0~39 */void putHz(int x, int y, char *font){    int i, j, col, seam;    char *vgamem = (char *) 0xa0000;    /* x = x % 25; */    y = y % 40;    for (i = 0; i < 16; i++) {	for (col = 0; col < 2; col++) {	    /* 2 free line for interval between lines */	    vgamem[(x * (16 + 2)) * 80 + 80 * i + y * 2 + col] =		font[i * 2 + col];	}    }}void div0(){    asm("movl $1,%eax\n" "movl $0,%ebx\n" "div %bx\n");}void ktask(){#define MAX_HIST 20#define HIST_SIZE 30    extern int textCursorX;    int kp = 0;    unsigned int getIdx = 0;    typedef char cmdHist[HIST_SIZE];    cmdHist *cmdHistory;    unsigned int cmdHistIdx = 0;    long a, temp;    int i, j, memcnt;    int cnonoff = 0;    unsigned short int rt = 0;    char *rotation[] = {	"-", "\\", "|", "/"    };    /* require memory for command history */    cmdHistory = (cmdHist *) calloc(1, MAX_HIST * HIST_SIZE);    while (1) {#ifndef VIDEO_GRAPH_640X480	a = temp = tickcount;	/* display tickcount at top line */	for (i = 0; i < 8; i++) {	    a = (a >> (7 - i) * 4) & 0x0000000f;	    *((char *) (0xb8000 + 144 + i * 2)) = (char) a =		(a >= 10) ? a + 0x37 : a + 0x30;	    *((char *) (0xb8000 + 144 + i * 2 + 1)) = 0x29;	    a = temp;	}#else	VGAPrintStr_8x16(25, 60, rotation[rt = (rt++) % 4], 0);	/* cursor rotation at 25,60 */#endif	/* command processing */	while (!keyEmpty()) {	    commands cmds;	    char keyAscii, keyScan;	    if (getLeftCtrl()) {	/* left ctrl pressed */		cnonoff++;		cnonoff = cnonoff & 0x1;	    }	    readKeybd(&keyAscii, &keyScan);	    if (keyAscii != 0x0a) {		/* backspace, remove tail of keyboard buffer in kernel kb */		if (keyAscii == '\b') {		    kp = (kp - 1) % KNLKEYBFLEN;		    printf("%c", keyAscii);		    continue;		}		switch (keyScan) {	/* RETRIEVE HISTORY BUFFER */		case UP:		case LEFT:		case INSERT:		    getIdx = (getIdx - 1 + MAX_HIST) % MAX_HIST;		    goto listHist;		    break;		case DOWN:		case RIGHT:		case DELETE:		    getIdx = (getIdx + 1 + MAX_HIST) % MAX_HIST;		    goto listHist;		    break;		case HOME:		    getIdx = 0;		    goto listHist;		    break;		case END:		    getIdx = MAX_HIST - 1;		    goto listHist;		    break;		case PAGEUP:		    getIdx = (getIdx + 5 + MAX_HIST) % MAX_HIST;		    goto listHist;		    break;		case PAGEDOWN:		    getIdx = (getIdx - 5 + MAX_HIST) % MAX_HIST;		  listHist:		    VGAScrGotoxy(textCursorX, 8);		    printf("                                        ");		    VGAHideCursor();		    VGAScrGotoxy(textCursorX, 8);		    printf("%s", cmdHistory[getIdx]);		    break;		default:		    printf("%c", keyAscii);		    kb[(kp++) % KNLKEYBFLEN] = keyAscii;		}	    } else {		/* keyAscii == 0x0a */		kb[(kp++) % KNLKEYBFLEN] = '\0';	/* fill EOF to keyboard buffer */		kp = 0;		/* reset keyboard buffer pointer */		if (strlen(kb) == 0 && getIdx != cmdHistIdx)		    strncpy(kb, cmdHistory[getIdx], HIST_SIZE);	/* execute history command just selected */		getIdx = cmdHistIdx;		if (strlen(kb) > 1) {		    strncpy(cmdHistory[(cmdHistIdx) % MAX_HIST], kb,			    HIST_SIZE);		    cmdHistIdx = (cmdHistIdx + 1) % MAX_HIST;		    getIdx = cmdHistIdx;		}		printf("\n");		if(cnonoff){ /* chinese input method enabled */                }		cmds = cmdLex(pcmdsRecd);		switch (cmds) {		case PS:		    {			taskData *p;			p = task;			printf			    ("#\tcs\t\t  ip\t\t  flg\t\t sp\t\t  state\t  ksts\tname\n");			do {			    printf				("%d\t%0x\t%0x\t%0x\t%0x\t%s\t%s\t%s\n",				 p->pid, p->cs, p->ip, p->flag, p->sp,				 p->state ? "running" : "suspend",				 p->ksts ? "RUN" : "SLP", p->taskName);			    p = p->nextTaskLink;			}			while (p != task);		    }		    break;		case VER:		    ver();		    break;		case HELP:		    {			int flg = 0;			ver();			printf("snixos help routine:\n");			for (j = 0;			     j < MAXCMDS && strlen(cmdTabs[j].desc) > 0;			     j++) {			    if (strlen(cmdTabs[j].cmdstr) +				strlen(cmdTabs[j].desc) > 30) {				printf("\n%-7s: %-30s ", cmdTabs[j].cmdstr,				       cmdTabs[j].desc);				flg = 1;			    } else {				printf("%s%-7s: %-30s%c",				       (flg == 1) ? "\n" : "",				       cmdTabs[j].cmdstr, cmdTabs[j].desc,				       (j % 2) ? '\n' : ' ');				flg = 0;			    }			}		    }		    break;		case CLEAR:		    if (videoMode == VIDTEXT_80X25)			screen_init();		    else			VGAScrInit();

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -