ISR + coroutines = Multitasking in DOS

DOS specific questions.
Post Reply
angros47
Posts: 2321
Joined: Jun 21, 2005 19:04

ISR + coroutines = Multitasking in DOS

Post by angros47 »

Yes, multitasking, not just multithreading. To compile it, you also need the coroutine subroutines that you can find here: https://freebasic.net/forum/viewtopic.php?f=17&t=28417

It will run a normal dos console, but at the same time the freebasic program (the DO...LOOP cycle in the subroutine "task") will keep running (a normal ISR would not allow to use a cycle). So, I guess this can be called multitasking (2 tasks)

Code: Select all

#include "dos/dpmi.bi"
#include "dos/go32.bi"
#include "dos/sys/farptr.bi"
#include "libco.bi"

dim shared as any ptr t0,t1
dim shared as _go32_dpmi_seginfo old_handler_0x08, new_handler_0x08




sub task
	dim z as integer
	do
		z+=1
		dim as integer x,y
		x=csrlin:y=pos
		locate 1,1:print "this is the thread, iteration ";z
		locate x,y
		co_switch(t0)
	loop

end sub



sub Int_0x08 cdecl()
  static D as ubyte
  if d=0 then
    d=255
    co_switch(t1)
    d=4
  elseif d<255 then
    d-=1
  end if
end sub
private sub Int_0x08_end cdecl()
end sub





sub InstallInt0x08()
   dim as byte ptr ptr_end = cast( byte ptr, @Int_0x08_end )
   dim as byte ptr ptr_start = cast( byte ptr, @Int_0x08 )
   _go32_dpmi_lock_code(@Int_0x08, ptr_end-ptr_start)

   _go32_dpmi_get_protected_mode_interrupt_vector( &h08, @old_handler_0x08 )

   new_handler_0x08.pm_offset = cast(integer,@Int_0x08)
   new_handler_0x08.pm_selector = _go32_my_cs()
   _go32_dpmi_chain_protected_mode_interrupt_vector( &h08, @new_handler_0x08 )
end sub

sub RemoveInt0x08()
   _go32_dpmi_set_protected_mode_interrupt_vector( &h08, @old_handler_0x08 )
end sub


t0=co_active()
t1=co_create(65536,@task)



InstallInt0x08
shell
RemoveInt0x08

xlucas
Posts: 334
Joined: May 09, 2014 21:19
Location: Argentina

Re: ISR + coroutines = Multitasking in DOS

Post by xlucas »

Wow! I've done this in real mode many times in the past, but never over DPMI. I used to make my QB programs include an assembly routine that would be loaded to memory and then pointed to with Call Absolute. Then the whole thing could go resident. Or also, I'd make a small TSR in assembly and then interface it with interrupts from QB. But never thought I could do it in a simple way with FreeBasic in 32bit.

Hey, on a sidenote... I came across Pete's QB site and forum and I saw you were registered in that forum. I tried to register there, but there doesn't seem to be any sign up option. However, I see people have posted messages very recently. Is there a way to join?
angros47
Posts: 2321
Joined: Jun 21, 2005 19:04

Re: ISR + coroutines = Multitasking in DOS

Post by angros47 »

A couple of years ago I also figured a way to do actual TSR programs with FreeBasic (https://freebasic.net/forum/viewtopic.php?f=4&t=26883), although I don't recommend it to anyone, a minimal mistake would result in a program that works fine 99 times and crashes the pc on the 100th time, so it would be very hard to debug.

About Pete's QB site: as far as I know, registration might have been disabled due to spammers. I registered many, many years ago (September 2008), and at the time, the site was still maintained. Today, it is almost abandoned, and the owner comes to check it sometimes, so he has no time to moderate it. Also, who would still want to join there nowadays? Most nostalgic people either were already members, or they have moved on years ago. Anyway, you aren't missing much, honestly. Even when I still posted there, that forum was able only to provide the most basic clues, the stuff that you could have figured yourself by reading the manual. If you hoped to find clever tricks, like how to use sound blaster, or extended memory, or VESA graphic modes, no one would have helped, the only reply you would have got would have been "Just use QB64". Those people wanted in fact to support both languages, so they focused only on code that worked on both compilers (so, all QBasic extended libraries and tricks were left out). Also, the people who were able to actually write those extended libraries had been the first ones to leave QBasic when FreeBasic became available. In QB, for example, a very good library (for its times) to manage advanced features was DirectQB. Its author was Angelo Mottola. When Freebasic became available, he stopped working on his library, to write a new one that would have worked on FreeBasic, so DirectQB was abandoned. And the new library made by Angelo Mottola for FreeBasic worked so well that it was integrated in the language itself, and became the official gfx library
xlucas
Posts: 334
Joined: May 09, 2014 21:19
Location: Argentina

Re: ISR + coroutines = Multitasking in DOS

Post by xlucas »

That is really amazing! I didn't know all that! I wish I had been online aware of those sites earlier to have been able to contribute more. And I'm a fan of FreeBasic's graphics library. Most graphics libraries nowadays are horribly bloated for simple tasks.
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: ISR + coroutines = Multitasking in DOS

Post by D.J.Peters »

@angros47 can you share the ready to use compiled lib for DOS here ?
I have no DOS C compiler installed ATM is there a gcc for the FBDOS /bin and it's runtime for the /lib folder ?

Joshy
angros47
Posts: 2321
Joined: Jun 21, 2005 19:04

Re: ISR + coroutines = Multitasking in DOS

Post by angros47 »

D.J.Peters wrote: Jun 18, 2022 8:39 @angros47 can you share the ready to use compiled lib for DOS here ?
Here it is:
https://www.mediafire.com/file/0apraqvl ... bco.o/file
Post Reply