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

📄 kernel.c,v

📁 一个小型的操作系统,采用gcc进行开发,几千行的代码,方便初学者学习
💻 C,V
📖 第 1 页 / 共 5 页
字号:
/* return PIT tickcount */int get_count(){    return tickcount;}#define SIZE 20/* insert sorting */void insertSort(){//    char *buf;//    int i, j, k;//    buf = (char *) malloc(sizeof(char) * SIZE);//    buf[0] = 0;//    printf("insert sorting for the following\n");//    /* produce random data whose value <100 *///    for (i = 1; i < SIZE; i++) {//      buf[i] = rand() % 100;//      delayMS(100);//      printf("%2d ", buf[i]);//    }//    printf("\nsorting in progress ...");//    for (i = 2; i < SIZE; i++) {//      if (buf[i] < buf[i - 1]) {//          buf[0] = buf[i];//          for (j = i - 1; buf[j] > buf[0]; j--) {//              buf[j + 1] = buf[j];//          }//          buf[j + 1] = buf[0];//      }//      printf("\n");//      for (k = 0; k < SIZE; k++) {//          printf("%s%2d%s ", k == 0 ? "(" : "", buf[k],//                 k == 0 ? ")" : "");//      }//    }//    printf("\nsorting done\n");//    for (i = 1; i < SIZE; i++) {//      printf("%2d ", buf[i]);//    }//    printf("\n");//    free(buf);}/* binary insert sorting, a improvement of the above insort() */void binsort(){//    char *buf;//    int i, j, k, low, high, mid, pos;//    buf = (char *) malloc(sizeof(char) * SIZE);//    buf[0] = 0;////    printf("binary insert sorting for the following\n");//    /* produce random data which value <100 *///    for (i = 1; i < SIZE; i++) {//      buf[i] = rand() % 100;//      delayMS(100);//      printf("%2d ", buf[i]);//    }//    printf("\nsorting in progress ...");//    for (i = 2; i < SIZE; i++) {//      if (buf[i] < buf[i - 1]) {//          buf[0] = buf[i];////          low = 1;//          high = i - 1;//          mid = (low + high) / 2;//          while (low <= high && buf[mid] != buf[0]) {//              if (buf[mid] < buf[0])//                  low = mid + 1;//              else//                  high = mid - 1;//              mid = (low + high) / 2;//          }//          if (buf[mid] == buf[0])//              pos = mid;//          else//              pos = high + 1;//          printf("[%2d] ", pos);////          for (j = i - 1; j >= pos; j--) {//              buf[j + 1] = buf[j];//          }//          buf[j + 1] = buf[0];//      }//      printf("\n");//      for (k = 0; k < SIZE; k++) {//          printf("%s%2d%s ", k == 0 ? "(" : "", buf[k],//                 k == 0 ? ")" : "");//      }//    }//    printf("\nsorting done\n");//    for (i = 1; i < SIZE; i++) {//      printf("%2d ", buf[i]);//    }//    free(buf);//    printf("\n");}long int factors(int i){    if (i == 0)	return 1;    else	return (i * factors(i - 1));}/* hanoi(): recursive prog demo */void hanoi(int n, char from, char via, char to){    if (n == 1)	printf("%d,%c->%c\n", n, from, to);    else {	hanoi(n - 1, from, to, via);	printf("%d,%c->%c\n", n, from, to);	hanoi(n - 1, via, from, to);    }}void time(int *tm){    *tm = (int) tickcount;}char *exceptionMesg[] = {    /* Int 00 */ "Divide By Zero",    /* Int 01 */ "Debug Trap",    /* Int 02 */ "NMI Interrupt",    /* Int 03 */ "Breakpoint",    /* Int 04 */ "Overflow",    /* Int 05 */ "BOUND Range Exceeded",    /* Int 06 */ "Invalid Opcode",    /* Int 07 */ "Device Not Available",    /* Int 08 */ "Double Fault",    /* Int 09 */ "Coprocessor Segment Overrun",    /* Int 0A */ "Invalid TSS",    /* Int 0B */ "Segment Not Present",    /* Int 0C */ "Stack-Segment Fault",    /* Int 0D */ "General Protection Fault",    /* Int 0E */ "Page Fault",    /* Int 0F */ "U/A",    /* Int 10 */    "Floating Point Error (Math Fault)",    /* Int 11 */ "Alignment Check",    /* Int 12 */ "Machine Check",    /* Int 13 */ "Streaming SIMD Extensions",    /* Int 14 */ "U/A",    /* Int 15 */ "U/A",    /* Int 16 */ "U/A",    /* Int 17 */ "U/A",    /* Int 18 */ "U/A",    /* Int 19 */ "U/A",    /* Int 1A */ "U/A",    /* Int 1B */ "U/A",    /* Int 1C */ "U/A",    /* Int 1D */ "U/A",    /* Int 1E */ "U/A",    /* Int 1F */ "U/A"};/* data in stackEFLG,ECS,EIP,||,handleID,cs,ds....esp,cr0,cr2,cr3 *//*void exceptionHandle(int esp, int ebp, int edi, int esi,                   int edx, int ecx, int ebx, int eax, int gs, int fs,                   int ss, int es, int ds, int cs, int handleID, int eip,                   int ics, int eflg)pseudo args eip,ics,eflg, which are pushed by CPU as exception occured */void exceptionHandle(int cr3, int cr2, int cr0, int esp, int ebp, int edi,		     int esi, int edx, int ecx, int ebx, int eax, int gs,		     int fs, int ss, int es, int ds, int cs, int handleID){    unsigned char *expAddr;    unsigned int *a = (unsigned int *) esp;	/* esp's content when exception occured */    int eip = *a;		/* the address exception happened */    int ics = *(a + 1);		/* the cs exception happened */    int eflg = *(a + 2);	/* the eflg exception happened */    int i = 0;    printf	("\nException Int %02x: %s\nEAX=%08X EBX=%08X ECX=%08X EDX=%08X EDI=%08X ESI=%08X\n"	 "ESP=%08X EBP=%08X\n"	 "CS=%08X DS=%08X ES=%08X SS=%08X FS=%08X GS=%08X\n"	 "Stack SS:ESP [CS=%08X EIP=%08X EFLG=%08X(%b)]\n"	 "cr0=%08x(%b) cr2=%08x cr3=%08x\n",	 (char) handleID, exceptionMesg[handleID], eax, ebx, ecx, edx, edi,	 esi, esp, ebp, cs, ds, es, ss, fs, gs, ics, eip, eflg, eflg, cr0,	 cr0, cr2, cr3);    expAddr = (unsigned char *) eip;    for (i = 0, printf("Exp Opcodes %02X:%08X [", ics, eip); i < 16; i++) {	printf("%02x ", *(expAddr + i));    }    printf("\b]\n");    __asm__("hlt\n");		/* system halted while exception occured ! */}/* Exceptions */extern unsigned int int0x0, int0x1, int0x2, int0x3,    int0x4, int0x5, int0x6, int0x7;extern unsigned int int0x8, int0x9, int0xA;extern unsigned int int0xB, int0xC, int0xD, int0xE,    int0x10, int0x11, int0x12, int0x13;/* IRQs */extern unsigned int int0x20, int0x21, int0x22,    int0x23, int0x24, int0x25, int0x26, int0x27;extern unsigned int int0x30, int0x31, int0x32,    int0x33, int0x34, int0x35, int0x36, int0x37;/*   Each entry is a protected mode gate descriptor:   byte in IDT entry    usage    0                    byte 0 (LSB) of handler offset (EIP 7:0)    1                    byte 1 of handler offset (EIP 15:8)    2                    LSB of handler selector (CS 7:0)    3                    MSB of handler selector (CS 15:8)    4                    (not used, set to 0)    5                    access byte (8Eh, 8Fh, 0EEh, or 0EFh)    6                    byte 2 of handler offset (EIP 23:16)    7                    byte 3 (MSB) of handler offset (EIP 31:24)  */#define csHigh 0x00		/* code selector MSB */#define csLow  0x08		/* code selector LSB */#define MAXINTS 35int *intHdlAddr[] = {/* exceptions */    &int0x0, &int0x1, &int0x2, &int0x3, &int0x4, &int0x5, &int0x6, &int0x7,    &int0x8, &int0x9, &int0xA, &int0xB, &int0xC, &int0xD, &int0xE,	/* &int0xF, reserved */    &int0x10, &int0x11, &int0x12, &int0x13,/* IRQs */    &int0x20, &int0x21, &int0x22, &int0x23, &int0x24, &int0x25, &int0x26,    &int0x27, &int0x30, &int0x31, &int0x32, &int0x33, &int0x34, &int0x35,    &int0x36, &int0x37};void doInitInts(){				/* do init interrupts, be called in initInterrupts() */    char *idtAddr = (char *) 0x1000;	/* IDT offset in GDT, GDT=0000h */    int i, tmp, pos = 0;    /* Exception & IRQ handler initialization for Intel X86 CPU */    for (i = 0; i < MAXINTS; i++) {	if (pos == 0x13)	    pos = 0x20;		/* IRQ0~IRQ7 */	if (pos == 0x28)	    pos = 0x30;		/* IRQ8~IRQ15 */	tmp = (int) intHdlAddr[i];	tmp = tmp & 0x0000ff;	/* eip b0~b7 */	*(idtAddr + pos * 8 + 0) = (char) tmp;	tmp = (int) intHdlAddr[i];	tmp = (tmp >> 8) & 0x0000ff;	/* eip b8~b15 */	*(idtAddr + pos * 8 + 1) = (char) tmp;	*(idtAddr + pos * 8 + 2) = csLow;	/* cs =0x08 */	*(idtAddr + pos * 8 + 3) = csHigh;	/* cs =0x08 */	*(idtAddr + pos * 8 + 4) = 0;	/* always reserved as 0 */	*(idtAddr + pos * 8 + 5) = 0x8e;	/* signature 0x8E */	tmp = (int) intHdlAddr[i];	tmp = (tmp >> 16) & 0xff;	/* eip b16~b23 */	*(idtAddr + pos * 8 + 6) = (char) tmp;	tmp = (int) intHdlAddr[i];	tmp = (tmp >> 24) & 0xff;	/* eip b24~b31 */	*(idtAddr + pos * 8 + 7) = (char) tmp;	pos++;    }}/* reboot PC from keyboard command "reboot" */static void reboot(void){    unsigned temp;    beep(1000);    cli();    do {	temp = inb(0x64);	if ((temp & 0x01) != 0) {	    (void) inb(0x60);	    continue;	}    }    while ((temp & 0x02) != 0);	/* flush the keyboard controller */    printf("rebooting...\n");    outb(0xFE, 0x64);		/* pulse the CPU reset line */    cmosDelay(2);    /* ...and if that didn't take effect, just hang */    while (1);}/* Press any key to continue... */char waitAnyKey(){    printf("\nPress any key to continue...\n");    return (getKey());}@1.8log@ hzk16 read to 0x2000:0 while system boot if video graphics mode is set and then write to fatfs, use 'dir' to list directory to see, cnver() read hzk16 to display Chinese version logo and reserved asystem status bar at row 25 of screen, use command 'showhz'to display Chinese character with the syntax "showhz qh wh" where qh stands for "GB qu code" and wh "GB wei code", and newTask() is now void newTask(taskEntrance,taskname)kernel.h created@text@d1 13a13 14/*   Snixos Project version 1.0, 2003.6  (C) Copyright 2003,2004,2005 Jockeyson,KeqianGao <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 liablity 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 .a14 1d19 1a19 3  $Id: kernel.c,v 1.8 2004/06/07 10:24:13 root Exp root $  built   : $Date: 2004/06/07 10:24:13 $  $Revision: 1.8 $d35 1a35 1#include "clock.h"d40 1a40 1static char rcsDate[] = "Built: $Date: 2004/06/07 10:24:13 $";d43 1a43 1int videoMode = VIDGRAPH_640X480;d45 1a45 1int videoMode = VIDTEXT_80X25;d47 12d72 1a72 1    setPITspeed(50 * 1);d74 2a75 1    initFileSystem();a76 1    mkDemoFile();		/* create sample files in file system */d79 7a85 1    newTask((unsigned) &ktask, "ktask");d88 1d92 3d97 1a97 1	wrfile("hzk16", (char *) 0x20000, HZK16SIZE);	// file hzk16 createdd99 1d101 1d106 99a204 2void printHZ(int x, int y, char *s, int mode)	// mode=0 hznm, mode=1 qwm{d216 1a216 1	if (0 == mode) {	// via  hz internal coded219 1a219 1	} else {		// via qw coded229 1a229 1	putHz(x, y, buffer);	// write to screen positioned x,yd237 16a252 12    char tst[] = "write file demo ,using fat WRFILE\n";    char tst2[] =	"Hello OS fan,\nthis is a RAM disk file created by wrfile() in Snixos,\nEnjoy fun! \n";    char tp[] =#include "sums.asm"	char *tt =#include "hanoi.asm"	wrfile("sums.asm", tp, strlen(tp));    wrfile("hanoi.asm", tt, strlen(tt));    wrfile("testfile", tst, strlen(tst));    wrfile("hello", tst2, strlen(tst2));d272 1a272 2memdump()d274 1a274 30    void *b1, *b2, *b3;    dump_heap();    b1 = malloc(42);    dump_heap();    b2 = malloc(23);    dump_heap();    b3 = malloc(7);    dump_heap();    b2 = realloc(b2, 24);    dump_heap();    free(b1);    dump_heap();    b1 = malloc(5);    dump_heap();    free(b2);    dump_heap();    free(b3);    dump_heap();    free(b1);    dump_heap();a276 1d279 3a281 3#define MAX_HIST 10    Date dt;    Time tm;d284 3a286 2    static char cmdHistory[MAX_HIST][30];    static unsigned int cmdHistIdx = 0;d289 3d293 1a293 1	"-", "\\", "|", "/", "-",d295 2d299 1d309 3a311 2	VGAPrintStr_8x16(25, 58, rotation[tickcount % 5], 1);d317 6a323 1d337 1a337 1		    printf("%d:%s\n", getIdx, cmdHistory[getIdx]);d343 1a343 1		    printf("%d:%s\n", getIdx, cmdHistory[getIdx]);d347 1a347 1		    printf("%d:%s\n", getIdx, cmdHistory[getIdx]);d351 1a351 1		    printf("%d:%s\n", getIdx, cmdHistory[getIdx]);d355 1a355 1		    printf("%d:%s\n", getIdx, cmdHistory[getIdx]);d359 6a364 1		    printf("%d:%s\n", getIdx, cmdHistory[getIdx]);d371 1a371 1	    } else {		//keyAscii == 0x0ad376 1a376 1		    strcpy(kb, cmdHistory[getIdx]);d380 2a381 1		    strncpy(cmdHistory[(cmdHistIdx) % MAX_HIST], kb, 30);d386 11d450 1a450 1		    asm("pushl $100\t\n" "call beep\t\n");	/* beep(100); */d472 2a473 2				if (p->pid ==				    pcmdsRecd->attribute.numVala486 1d592 1a592 1		case TESTAS:d596 4a599 2		case D0:		    printf("%d\n", 5 / 0);d604 2a605 1		case BOOT:d608 7a614 5		    getDate(&dt);		    getTime(&tm);		    printf("%4d-%02d-%02d %02d:%02d:%02d\n",			   dt.year + 2000, dt.month, dt.day, tm.hour,			   tm.min, tm.sec);d616 2a617 1		case SHOWHZ:d619 1a619 1			int qh = 0, wh = 0;d633 3a635 1			if (qh > 0 && wh > 0) {d638 11a648 1			    printHZ(20, 38, aa, 1);d669 1a669 1	("\nSnixos Project version 1.0, 2003.6\n(C) Copyright 2003-2004 Snallie Jockeyson <Snallie@@tom.com>    \nAll Rights Reserved.\nDistributed under the terms of the GNU General Public License.\n%s\n",d679 1a679 1    unsigned char aa[] =d684 1d686 8a693 2    VGAPrintStr_8x16(1, 28, "<Sallie@@tom.com>", 1);    printHZ(25, 0, aa, 0);d695 1d752 27a778 1void delay()d780 5a784 3

⌨️ 快捷键说明

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