function like Delay(ms); in Pascal
function like Delay(ms); in Pascal
I am really beginer in OSDev.
Can you help me with function like Delay(ms); in Pascal or Sleep(ms); in Win API ? I have timer, but i don't know how to make "slowing function". Can you help me please?
Can you help me with function like Delay(ms); in Pascal or Sleep(ms); in Win API ? I have timer, but i don't know how to make "slowing function". Can you help me please?
-
- Member
- Posts: 62
- Joined: Tue Feb 13, 2007 10:46 am
For this I programmed the timer (IRQ 0) to fire ~1000 times a second.
Delay(unsigned int milliseconds) is called from C code
hope this helps
timer dd 0
irqM0:
push ax
inc dword [timer]
;manual EOI before the interrupt has ended
mov al, 0x20
out 0x20, al
pop ax
iret
[global Delay]
Delay: ;delays for some number of irq0 firings (each of which are about 1 millisecond)
push eax
push ebx
mov eax, [timer]
add eax, [esp + 12] ;eax = delay + time
.wait
mov ebx, [timer]
cmp eax, ebx
jg .wait
pop ebx
pop eax
ret
Delay(unsigned int milliseconds) is called from C code
hope this helps
timer dd 0
irqM0:
push ax
inc dword [timer]
;manual EOI before the interrupt has ended
mov al, 0x20
out 0x20, al
pop ax
iret
[global Delay]
Delay: ;delays for some number of irq0 firings (each of which are about 1 millisecond)
push eax
push ebx
mov eax, [timer]
add eax, [esp + 12] ;eax = delay + time
.wait
mov ebx, [timer]
cmp eax, ebx
jg .wait
pop ebx
pop eax
ret
I have an 80386SX 20MHz 2MB RAM.
It is my testbed platform. Only has the 3.5" and 5.25" floppy drives.
It is my testbed platform. Only has the 3.5" and 5.25" floppy drives.
-
- Member
- Posts: 62
- Joined: Tue Feb 13, 2007 10:46 am
-
- Member
- Posts: 62
- Joined: Tue Feb 13, 2007 10:46 am
No i mean.. what does the timing for a delay function if it cant get access to a PIT? For example in user mode code where the kernel cant provide the service because the kernel uses the PIT for sceduling...uglyoldbob wrote:BogoMips? Take a quick spin around google for it. It is a calculation of how fast your computer can do "nothing" and can ppossibly be used for timing loops.
-
- Member
- Posts: 62
- Joined: Tue Feb 13, 2007 10:46 am
You would probably have to use the RTC clock for that (which isn't supported on all hardware, can't seem to remember what doesn't support it, probably old stuff). Or you could introduce a method into your scheduler that delays a process/thread for a minimum of x timer ticks.
//add an item for each process that says (earliest time to execute)
//pretend earlyTime is the variable for the process/thread
earlyTime dd 0
Delay
mov eax, [ticks]
mov [earlyTime], eax
(switch to another task)
ret
EXAMPLE
ticks dd 0
irq0:
inc dword [ticks]
//bla bla scheduler code
//found a process/thread to switch to maybe?
mov eax, earlyTime
cmp eax, [ticks]
if (eax < [ticks])
DONT do this thread/process
This might be the easiest, but you'll probably have to use software task switching instead of hardware switching becuase of the added information for each task.
//add an item for each process that says (earliest time to execute)
//pretend earlyTime is the variable for the process/thread
earlyTime dd 0
Delay
mov eax, [ticks]
mov [earlyTime], eax
(switch to another task)
ret
EXAMPLE
ticks dd 0
irq0:
inc dword [ticks]
//bla bla scheduler code
//found a process/thread to switch to maybe?
mov eax, earlyTime
cmp eax, [ticks]
if (eax < [ticks])
DONT do this thread/process
This might be the easiest, but you'll probably have to use software task switching instead of hardware switching becuase of the added information for each task.
I have an 80386SX 20MHz 2MB RAM.
It is my testbed platform. Only has the 3.5" and 5.25" floppy drives.
It is my testbed platform. Only has the 3.5" and 5.25" floppy drives.
hm...
Now, the linker giving this:
main.o(.text+0x1ff):main.c: undefined reference to `_Delay'
i wrote "extern void Delay(unsigned int cas);" into system.h included from all .C files of my OS.
i wrote "Delay(2000);" into main.c (for wait 2 seconds)
i wrote your code for Delay: into start.asm when i setting up all IRQ's.
i wrote your code for irqM0: into start.asm into setting of IRQ0
main.o(.text+0x1ff):main.c: undefined reference to `_Delay'
i wrote "extern void Delay(unsigned int cas);" into system.h included from all .C files of my OS.
i wrote "Delay(2000);" into main.c (for wait 2 seconds)
i wrote your code for Delay: into start.asm when i setting up all IRQ's.
i wrote your code for irqM0: into start.asm into setting of IRQ0
Sorry for my bad English...
Hi - a couple of things:
1) If you want to access your assembly routine from C, you need to prefix the assembly label with an underscore (ie _Delay, rather than Delay).
2) Can the linker see all of your object files and are they all in a supported format?
3) Have you put a 'global _Delay' somewhere in your assembly code?
HTH,
Adam
1) If you want to access your assembly routine from C, you need to prefix the assembly label with an underscore (ie _Delay, rather than Delay).
2) Can the linker see all of your object files and are they all in a supported format?
3) Have you put a 'global _Delay' somewhere in your assembly code?
HTH,
Adam
If you have GDT.INC (or something) accessible by your OS kernel, you can switch to real mode -
and use INT15,AH=86h function Wait.
CX*65535 and DX is the number of microseconds to wait.
The precision for this function is 977 microseconds.
Then you can switch to protected mode by loading GDT, IDT and other necessary data to stack and setting the first bit of CR0 to 1.![Smile :)](./images/smilies/icon_smile.gif)
But beware - if this function is used very frequently in your OS - your minimal system requirements may be higher than a regular 80386 - switching to real mode from PMode just for delay function and hopping back to PM can be on older machines slow. But it is a delay procedure, so who cares?![Very Happy :D](./images/smilies/icon_biggrin.gif)
Code: Select all
XOR EAX,EAX
MOV CR0,EAX
CX*65535 and DX is the number of microseconds to wait.
The precision for this function is 977 microseconds.
Then you can switch to protected mode by loading GDT, IDT and other necessary data to stack and setting the first bit of CR0 to 1.
![Smile :)](./images/smilies/icon_smile.gif)
But beware - if this function is used very frequently in your OS - your minimal system requirements may be higher than a regular 80386 - switching to real mode from PMode just for delay function and hopping back to PM can be on older machines slow. But it is a delay procedure, so who cares?
![Very Happy :D](./images/smilies/icon_biggrin.gif)
My web site: http://inflater.wz.cz (Slovak)
Derrick operating system: http://derrick.xf.cz (Slovak and English
)
Derrick operating system: http://derrick.xf.cz (Slovak and English
![Razz :P](./images/smilies/icon_razz.gif)
Hi,
![Wink ;)](./images/smilies/icon_wink.gif)
Cheers,
Brendan
I'd guess anyone trying to write protected mode IRQ handlers would be deeply disturbed by this - worst case IRQ latency measured in days....inflater wrote:But beware - if this function is used very frequently in your OS - your minimal system requirements may be higher than a regular 80386 - switching to real mode from PMode just for delay function and hopping back to PM can be on older machines slow. But it is a delay procedure, so who cares?
![Wink ;)](./images/smilies/icon_wink.gif)
Cheers,
Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
Hey
I recently implemented a delay function in my native library... for the sake of it. But it causes serious problems the way i am doing it. Does anyone know how it is done by an operatin system like linux? Do they use the PIT and time the system with APIC or do they simply check the RTC during kernel mode access?
I recently implemented a delay function in my native library... for the sake of it. But it causes serious problems the way i am doing it. Does anyone know how it is done by an operatin system like linux? Do they use the PIT and time the system with APIC or do they simply check the RTC during kernel mode access?
I don't know how linux does it however, I can tell you how my delay function works. When you call the delay function it passes on the delay count to the scheduler and tells the scheduler not to wake the task up before this delay has passed. Of course that is how I do it, and I'm sure that this is not the only way.Tyler wrote:Hey
I recently implemented a delay function in my native library... for the sake of it. But it causes serious problems the way i am doing it. Does anyone know how it is done by an operatin system like linux? Do they use the PIT and time the system with APIC or do they simply check the RTC during kernel mode access?