Delay

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
beyondsociety

Delay

Post by beyondsociety »

How would I create a delay for use in protected mode?
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Delay

Post 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).
PlayOS

Re:Delay

Post by PlayOS »

So how do we program the Programmable Interval Counter chip? Do you have any examples?

Thanks.
PlayOS

Re:Delay

Post by PlayOS »

Thankyou. :)
Tim

Re:Delay

Post 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 */
}
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Delay

Post 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 ...
beyondsociety

Re:Delay

Post by beyondsociety »

Does anybody has any examples of this in assembly language?
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Delay

Post 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 !
Tim

Re:Delay

Post 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.
Tom

Re:Delay

Post by Tom »

beyondsociety, I think you should learn C...
beyondsociety

Re:Delay

Post 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?
beyondsociety

Re:Delay

Post 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.
Tom

Re:Delay

Post 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
Curufir

Re:Delay

Post 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
Post Reply