Page 1 of 2

Delay

Posted: Sun Sep 29, 2002 7:51 pm
by beyondsociety
How would I create a delay for use in protected mode?

Re:Delay

Posted: Mon Sep 30, 2002 5:46 am
by Pype.Clicker
1) install a valid IDT
2) program the Programmable Interval Counter chip to have some useful frequency (default is 18.2Hz, 1Khz is usually better)
3) use the timer interrupt to count milliseconds

then

Code: Select all

int delay;
volatile int done;
wait(int milli) {
   done=false; 
   delay=milli;
   while (!done);
}

timer_interrupt_handler() {
   if (!done && delay) delay--;
   if (delay==0) done=true;
}
nb: this is to be checked by disasembly to see if the compiler doesn't optimized the while(!done); into some while(1); - but it shouldn't

if you have multithreaded support, replace the done variable by some threadResume mechanism (and use a linked list of delay requests, each with its own delay value).

Re:Delay

Posted: Mon Sep 30, 2002 6:02 am
by PlayOS
So how do we program the Programmable Interval Counter chip? Do you have any examples?

Thanks.

Re:Delay

Posted: Mon Sep 30, 2002 6:14 am
by Pype.Clicker

Re:Delay

Posted: Mon Sep 30, 2002 6:18 am
by PlayOS
Thankyou. :)

Re:Delay

Posted: Mon Sep 30, 2002 7:46 am
by Tim
Mobius has two delay routines: one similar to Pype.Clicker's (which uses the clock interrupt) and one high-resolution routine which doesn't use interrupts.

Link to high-resolution timer:
http://cvs.sourceforge.net/cgi-bin/view ... cvs-markup (function: ArchMicroDelay)

Programmable Interval Timer init code:

Code: Select all

/*! \brief Sets up the PC's programmable timer to issue interrupts at HZ 
 *          (100 Hz) instead of the default ~18.2 Hz.
 *    \param        hz    The required clock frequency, in hertz
 */
void i386PitInit(unsigned hz)
{
    unsigned short foo = (3579545L / 3) / hz;

    /* reprogram the 8253 timer chip to run at 'HZ', instead of 18 Hz */
    out(0x43, 0x36);        /* channel 0, LSB/MSB, mode 3, binary */
    out(0x40, foo & 0xFF);    /* LSB */
    out(0x40, foo >> 8);    /* MSB */
}

Re:Delay

Posted: Mon Sep 30, 2002 7:57 am
by Pype.Clicker
hmmm ... if get this right, it launch a "one-shot" timer on an alternative channel of the PIT and polls for end-of-period, right ?
interresting technique ...

Re:Delay

Posted: Mon Sep 30, 2002 10:09 am
by beyondsociety
Does anybody has any examples of this in assembly language?

Re:Delay

Posted: Tue Oct 01, 2002 1:38 am
by Pype.Clicker
come on! this is not *that* hard to translate those little function in ASM, especially Tim's one

out(x,y)

becomes
mov al,y
out x,al

only the division will require you a few more computations ...
once you get your 16-bits value in ax for the frequency (if your frequency is fixed, you can ask your HP48 to do the computation instead of programming it ;) you don't even need to do the &0xff and >>8 stuff: just use al and ah !

Re:Delay

Posted: Tue Oct 01, 2002 10:11 am
by Tim
beyondsociety: if you can't translate C to assembler, then it seems silly to be writing assembly code in the first place.

In any case, a function such as this doesn't need to be written in assembly.

Re:Delay

Posted: Tue Oct 01, 2002 10:18 am
by Tom
beyondsociety, I think you should learn C...

Re:Delay

Posted: Tue Oct 01, 2002 10:58 am
by beyondsociety
Ok, you guys talked me into it. I am going to learn C but only so I can translate the examples people have given me. I am writing my operating system soley in assembly language. So I won't be using any C code in my os.

What compiler do you suggest I use?

At my school they have C books but they are too old and so there is no Cd-rom to go with the book. I can check out the book, but since I dont have the program to run and compile the code, Im stuck. They have alot of C++ books though with the programs I need.

What do you suggest I do?

Re:Delay

Posted: Tue Oct 01, 2002 11:01 am
by beyondsociety
By the way, I am using Windows Me/2000 for writing my os on. I havent had the time to get Linux up and running considering the computer I was going to use has a hard drive problem.

Re:Delay

Posted: Tue Oct 01, 2002 11:11 am
by Tom
get "Learn C++ today!" and learn to program in C++(or C too, in the book it tells you what code is C and C++) and program in DOS until you know more

Re:Delay

Posted: Tue Oct 01, 2002 11:20 am
by Curufir
Try out cc386 for a compiler. It doesn't do all the fancy tricks of gcc, but it has a nice windows ide and (First time I've seen this anywhere) you can get it to produce a nasm syntax text file as an intermediate step. So you can see exactly how cc386 turns your C instructions into something the computer can understand. Very handy if you're more familiar with C than asm.

Homepage is http://members.tripod.com/~ladsoft