You are on page 1of 4

Submitted by:

Abdul MAteen hashmi (roll#54)

Submitted to:

Ma’aM SuMaira

Semester:

7th

Session:

2019-2023

Course title:

System

Programming

Date of submission:

7,april , 2023

University of poonch rawalakot, ajk

Dept. of cs and it
Question:A clock which would continuously display the current time at the top right corner of the
screen which you have came across in number of applications. For this we will have to first
understand how the computer keeps track of time. It has got a timer which keeps ticking. For
XTs the timer stops ticking when the computer is put off, whereas form AT onwards it keeps
ticking even when the computer is put off. The timer ticks at a rate of 18.2 times per second. Or
1092 times per minute and 65520 times per hour. Whenever the timer an interrupt 8 occurs and
the timer routine in ROM-BIOS gets called. This routine increments the tick count which is
placed at location number 0x46C to 0x46F in BIOS data area. At the stroke of midnight this
count is reset to zero.
In AT onwards when the computer is put off though the contents of 0x46C to 0x46F are lost the
time is continuously updated in CMOS RAM since it has a battery backup. When the computer
is rebooted the current time is read back into locations 0x46C to 0x46F.
So to display the current time on the screen our TSR should pick up the current tick count from
locations 0x46C to 0x46C to 0x46F and the convert it to the corresponding hours, minutes and
seconds. Since the time displayed should get continuously updated irrespective of what we are
working with, these calculations must be performed in the TSR

Through linux:
Script
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <time.h>
#include <unistd.h>
#include <sys/io.h>

#define RTC_ADDRESS 0x70


#define RTC_DATA 0x71

void print_time() {
struct tm* timeinfo;
time_t current_time;
unsigned char hour, minute, second;

// read current time from RTC


outb(0x04, RTC_ADDRESS);
hour = inb(RTC_DATA);
outb(0x02, RTC_ADDRESS);
minute = inb(RTC_DATA);
outb(0x00, RTC_ADDRESS);
second = inb(RTC_DATA);

// convert binary-coded decimal to decimal


hour = ((hour & 0x0F) + ((hour / 16) * 10));
minute = ((minute & 0x0F) + ((minute / 16) * 10));
second = ((second & 0x0F) + ((second / 16) * 10));

// print time on screen


printf("\033[s\033[24;60H%02d:%02d:%02d\033[u", hour, minute, second);
fflush(stdout);
}

void handle_signal(int signal) {


// disable cursor blinking and exit program
printf("\033[?25h\033[0m\n");
exit(0);
}

int main() {
// request I/O permissions for accessing RTC
if (ioperm(RTC_ADDRESS, 2, 1) != 0) {
perror("ioperm");
return 1;
}
// disable cursor blinking and set signal handler
printf("\033[?25l");
signal(SIGINT, handle_signal);

// print current time every second


while (1) {
print_time();
sleep(1);
}

return 0;
}
OUT PUT

You might also like