ISR + coroutines = Multitasking in DOS

DOS specific questions.
angros47
Posts: 1670
Joined: Jun 21, 2005 19:04

ISR + coroutines = Multitasking in DOS

Postby angros47 » Apr 25, 2020 14:36

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: 285
Joined: May 09, 2014 21:19
Location: Argentina

Re: ISR + coroutines = Multitasking in DOS

Postby xlucas » Aug 10, 2020 23:19

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: 1670
Joined: Jun 21, 2005 19:04

Re: ISR + coroutines = Multitasking in DOS

Postby angros47 » Aug 13, 2020 15:51

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: 285
Joined: May 09, 2014 21:19
Location: Argentina

Re: ISR + coroutines = Multitasking in DOS

Postby xlucas » Aug 18, 2020 6:30

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.

Return to “DOS”

Who is online

Users browsing this forum: No registered users and 2 guests