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

📄 timer.cpp

📁 Jazmyn is a 32-bit, protected mode, multitasking OS which runs on i386 & above CPU`s. Its complete
💻 CPP
字号:
/*
 * Copyright (C) 2004, Thejesh AP. All rights reserved.
 */

#include <sys\types.h>
#include <null.h>
#include <stdlib.h>
#include <string.h>
#include <jazmyn\handlers.h>
#include <jazmyn\kernel.h>
#include <fs\devmgr.h>
#include <drivers\console.h>
#include <drivers\keyboard.h>
#include <drivers\timer.h>

extern device_manager _dev_mgr;


timer::timer(char *name) : driver(name)
{
	int err;
	if((err = _dev_mgr.register_driver(1,this->name,0,timer_main))<0)
	{
		cout<<"register_driver ERROR : "<<this->name<<" "<<err<<endl;
		return;
	}
	if((err = set_handler(IRQ0,timer_handler))<0)
	{
		cout<<"set_handler ERROR : "<<this->name<<" "<<err<<endl;
		return;
	}
	prog_pit(PROG_HZ,TMR_SC0);
	for(int i=0;i<MAX_CALL;i++)
	{
		call_arr[i].end_time = 0;
		call_arr[i].fun = NULL;
	}
        sys_boot_time = get_time();
        curr_time = sys_boot_time;

        enable_irq(IRQ0);
        cout<<"timer driver succesfully registered             [name] = "<<this->name<<endl;
}

timer::~timer()
{

}

void timer::prog_pit(float Hz,byte counter)
{
	uint val=0;
	if(Hz < 18.206759) Hz = 18.206759;
	val = 1193180/Hz;
	outportb(TMR_CTRL, counter | TMR_BOTH | TMR_MD3);
	outportb((0x40+counter),val & 0xFF);
	outportb((0x40+counter),(val>>8) & 0xFF);
}

int timer::call_after(uint millisec,void (*fun)())
{
	ullong end = curr_time + millisec;
	for(int i=0;i<MAX_CALL;i++)
	{
		if(call_arr[i].fun == NULL)
		{
			call_arr[i].fun = fun;
			call_arr[i].end_time = end;
		}
	}
}

ullong timer::get_time()
{
        return 0;
}

timer	_timer_obj("timer");
int     ticks = 0;

extern int sched_ready;

void timer_handler()
{
        _timer_obj.curr_time += (ullong)(1000 / PROG_HZ);
	ullong curr = _timer_obj.curr_time;
	for(int i=0;i<MAX_CALL;i++)
	{
		if(_timer_obj.call_arr[i].fun && curr >= _timer_obj.call_arr[i].end_time)
		{
                        void (*f)() = _timer_obj.call_arr[i].fun;
			_timer_obj.call_arr[i].fun = NULL;
                        f();
		}
        }
        ticks++;
        if(ticks >= 2 && sched_ready)
        {
                ticks = 0;
                schedule();
        }

}	

int timer_main(void *req)
{
	/* 
	 *There is no support to directly program the timer as it may affect the functioning
	 *of scheduler & other pending calls.
	 */
	return -1;
}
	

⌨️ 快捷键说明

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