欢迎来到虫虫下载站 | 资源下载 资源专辑 关于我们
虫虫下载站

clock.c~

参照MINIX3写的操作系统 用GCC+NASM+BOCHS开发
C~
字号:
#include "../include/toyos.h"#include"clock.h"

PRIVATE long time_to_second(struct t_bios_time * time)
{
	long ret;
	int year;

	/* this century is 1 less than what we offen think of */
	year = time->year + time->century * 100 - 1970;

	/* 1972 % 4 == 0, 1973 - 1970 == 3, 3 + 1 == 4 */
	ret = year * YEAR + DAY * ((year + 1) / 4);	

	ret += month[time->month - 1];
	/* if it's not leap year */
	if(time->month > 2 && ((year + 2) % 4))
		ret -= DAY;
	ret += DAY * (time->day - 1);
	ret += HOUR * time->hour;
	ret += MINUTE * time->minute;
	ret += time->second;

	return ret;
}

PUBLIC void InitClock()
{
/*	OutByte(TIMER_MODE,RATE_GENERATOR);
	OutByte(TIMER0,(t_8)((TIMER_FREQ/HZ)&0xFF));	
	OutByte(TIMER0,(t_8)((TIMER_FREQ/HZ>>8)&0xFF00));*/
    	printk("************Init Clock*************\n");
	bios_time.second = CMOS_READ(RTC_SECOND);
	bios_time.minute = CMOS_READ(RTC_MINUTE);
	bios_time.hour = CMOS_READ(RTC_HOUR);
	bios_time.day = CMOS_READ(RTC_DAY_OF_MONTH);
	bios_time.month = CMOS_READ(RTC_MONTH);
	bios_time.year = CMOS_READ(RTC_YEAR);
	bios_time.century = CMOS_READ(RTC_CENTURY);
	BCD_TO_BIN(bios_time.second);	BCD_TO_BIN(bios_time.minute);
	BCD_TO_BIN(bios_time.hour);	BCD_TO_BIN(bios_time.day);
	BCD_TO_BIN(bios_time.month);	BCD_TO_BIN(bios_time.year);
	BCD_TO_BIN(bios_time.century);

	boot_time = time_to_second(&bios_time);

	printk("Boot time: %u\n", boot_time);
	printk("Century %d Year %d Month %d Day %d Time: %d : %d : %d\n", 
			bios_time.century,
			bios_time.year, bios_time.month,
			bios_time.day, bios_time.hour, 
			bios_time.minute, bios_time.second);

	timer_count = 0;		/* initilize */

  	/* set timer rate */
	/*OutByte(0x34, TIMER_MODE);	 binary, mode 2, LSB/MSB, ch 0 
	outb(LATCH & 0xff, TIMER0);	
	outb(LATCH >> 8, TIMER0);*/	
	/*OutByte(TIMER_MODE,RATE_GENERATOR);	
	OutByte(TIMER0,(t_8)LATCH & 0xff);	 LSB 
	OutByte(TIMER0,(t_8)LATCH >> 8);*//* MSB */
}

PRIVATE void ClockInterrupt()
{ 
	RealTime+=LostTicks;
	LostTicks=0;
	//ProcessSchudler();
	/*DisplayInt(RealTime);*/
}
PRIVATE void SetTime()
//设置时间
{
}

void OutCommand(t_8 Command)
{
	t_8 c;
	int i=0;
	for(i=0;i<100000;i++)
	{
		c=inb_p(0x3f4);
		if(c==0x80)
			break;
	}
	if(i>100000)
		printk("Floppy driver is not ready.\n");
	else
		outb_p(Command,0x3f5);
}

int InResult()
{
	t_8 Status=0;
	int Bytes=0;
	int i=0;
	for(i=0;i<100000;i++)
	{
		Status=inb_p(0x3f4)&(DIR|READY|BUSY);
		if(Status==READY)
			return Bytes;
		if(Status==(DIR|READY|BUSY))
		{
			printk("You do a good job1\n");
		}
	}
	if(i>=100000)
		printk("Floppy driver is not ready %x.\n",Status);
	//else
	//	outb_p(Command,0x3f5);
}
void detect_floppy_drives()
{
	char drive_type[6][50];  
	unsigned char c;
    unsigned char a, b;
	int i=0;

	strcpy(drive_type[0],"no floppy drive");	
	strcpy(drive_type[1],"360kb 5.25in floppy drive");
	strcpy(drive_type[2],"1.2mb 5.25in floppy drive");	
	strcpy(drive_type[3], "720kb 3.5in");
	strcpy(drive_type[4],"1.44mb 3.5in");	
	strcpy(drive_type[5],"2.88mb 3.5in");

   c=CMOS_READ(0x10);

    a = c >> 4;		// get the high nibble
    b = c & 0xF;	// get the low nibble by ANDing out the high nibble

    printk("Floppy drive A is an:\n");
    printk("%s",drive_type[a]);
    printk("\nFloppy drive B is an:\n");
    printk("%s",drive_type[b]);
    printk("\n"); 
    OutCommand(0x07);
    OutCommand(0x0);
    OutCommand(0x08);
    printk("%x\n",InResult());
}
PRIVATE void GetTime()
//获得时间
{
}

PUBLIC void initClock()
{
	InitClock();
	detect_floppy_drives();
}
/*void ClockTask()
{
 	Message *pMess;
	InitClock();
	detect_floppy_drives();
	 while(1)
		{
		
			pMess=ReceiveMessage(CLOCK,NULL);
			switch(pMess->Type)
			{
			case GETTIME: GetTime();break;
			case SETTIME: SetTime();break;
			case INTRRUPT: ClockInterrupt();break;
			default:   break;
			}
			
		}

}*/

⌨️ 快捷键说明

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