home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_08_10 / 8n10122a < prev    next >
Text File  |  1990-08-29  |  18KB  |  552 lines

  1. #include         <alloc.h>
  2. #include         <bios.h>
  3. #include         <dos.h>
  4. #include         <stdio.h>
  5. #include         <stdlib.h>
  6. #include         <string.h>
  7.  
  8. #include         "csimint.h"
  9.  
  10.  
  11. int               start_kb_process(void(*task)(void),int stacksize)
  12.    {
  13.       int   proc_id;
  14.       set_kb_process = 1;
  15.       proc_id = start_process(task,stacksize);
  16.       set_kb_process = 0;
  17.       return(proc_id);
  18.    }
  19.  
  20.  
  21. int               start_process(void(*task)(void),int stacksize)
  22.    {
  23.       char                    *stack;
  24.       struct   _simprocessT   *process;
  25.       int                     *off_seg=(int *)&stack;
  26.       unsigned   int           stackbytes;
  27.       int                      oldinit;
  28.  
  29.       /*  Check to see if tack handler has been installed*/
  30.  
  31.          if(getvect(_INTRPT_)!=task_handler)
  32.             /* if it has not been installed, attempt to */
  33.  
  34.             if(install_handler())
  35.                /* if it doesn't install, report error and exit */
  36.                {
  37.                   printf("error installing task handler \n");
  38.                   exit_processing(1);
  39.                }
  40.  
  41.          if(stacksize > 0x0fdf)
  42.             /* Max stack size in paragraphs (SEGMENT counts)*/
  43.             {
  44.                printf("stack size error: proc id : %d",totalprocs);
  45.                exit_processing(2);
  46.             }
  47.  
  48.          stackbytes = stacksize * 16;
  49.  
  50.       /* Setup Process header and stack */
  51.  
  52.          process =
  53.                (struct _simprocessT *)
  54.                calloc(sizeof(struct _simprocessT) + stackbytes,1);
  55.  
  56.          /* make sure there was enough memory */
  57.          if(process==NULL)
  58.             return(2);
  59.  
  60.          stack = (char *)(process+1);
  61.  
  62.          /* 'Normalize' the bottom of stack pointer, that is
  63.             make sure the offset is less then 16, as would be
  64.             the case if   the program where compiled in the huge
  65.             memory model */
  66.  
  67.          if(off_seg[0]>16)
  68.             for(;off_seg[0]>16;off_seg[1]++,off_seg[0]-=16);
  69.  
  70.          /* remember, the stack actually starts high and
  71.             builds its way down, so put the pointer at the top.*/
  72.  
  73.          stack += stackbytes - 1;
  74.  
  75.          /* finish setup of stack and base pointers */
  76.  
  77.          process->reg_sp = FP_OFF(stack);
  78.          process->reg_ss = FP_SEG(stack);
  79.  
  80.          process->reg_bp = FP_OFF(stack);
  81.  
  82.          process->proc_id = totalprocs++;
  83.          process->init = 1;
  84.  
  85.       /* Setup start jump */
  86.          process->reg_cs = FP_SEG(task);
  87.          process->reg_ip = FP_OFF(task);
  88.  
  89.       /* link into current processes */
  90.          if(processlist==NULL)
  91.             {
  92.                processlist =         process;
  93.                processlist->next =   process;
  94.                processlist->prev =   process;
  95.             }
  96.          else
  97.             {
  98.                process->prev =       processlist;
  99.                process->next =       processlist->next;
  100.                process->next->prev = process;
  101.                processlist->next =   process;
  102.                processlist = process;
  103.             }
  104.          if(set_kb_process)
  105.             kbprocess = processlist;
  106.  
  107.          /* make sure we can get back to where we were... */
  108.          glbl_ss = _SS;
  109.          glbl_sp = _SP;
  110.          glbl_bp = _BP;
  111.          oldinit = _sim_init_val;
  112.  
  113.          /*   tell the task handler what routine the process uses */
  114.  
  115.          glbl_fc = task;
  116.  
  117.          /* tell the task handler that this proc is initing */
  118.  
  119.          _sim_init_val=1;
  120.  
  121.          /* set up the new stak and base pointers and ... */
  122.          _SS = process->reg_ss;
  123.          _SP = process->reg_sp;
  124.          _BP = process->reg_bp;
  125.  
  126.          /* have the task_handle do the rest */
  127.          geninterrupt(_INTRPT_);
  128.  
  129.          _SS = glbl_ss;
  130.          _SP = glbl_sp;
  131.          _BP = glbl_bp;
  132.          glbl_fc = NULL;
  133.          _sim_init_val = oldinit;
  134.          processlist = processlist->prev;
  135.          return(0);
  136.  
  137.    }
  138.  
  139.  
  140. void               stop_process(int   process_id)
  141.    {
  142.       struct   _simprocessT   *process;
  143.       int                      go;
  144.  
  145.       go = 1;
  146.       process = processlist;
  147.  
  148.       while(go)
  149.          {
  150.             if(process_id == process->proc_id)
  151.                {
  152.                   process->status.kill_flag = 1;
  153.                   process->start_time = _sim_system_time;
  154.                   go = 0;
  155.                }
  156.             if(process->next == processlist)
  157.                {
  158.                   if(go)
  159.                      printf("\nattempted to stop non-existant proc\n");
  160.                   go = 0;
  161.                }
  162.             else
  163.                {
  164.                   process = process->next;
  165.                }
  166.          }
  167.    }
  168.  
  169.  
  170.  
  171. void               sim_start(void)
  172.    {
  173.       _sim_init_val=0;
  174.       _last_update_time = *systimer;
  175.       geninterrupt(_INTRPT_);
  176.    }
  177.  
  178.  
  179. void               exit_processing(int condition)
  180.    {
  181.       struct   _simprocessT   *pholder;
  182.       setvect(_INTRPT_,old_vector);
  183.       printf("Exit processing, code : %d\n",condition);
  184.       processlist->prev->next = NULL;
  185.       do
  186.          {
  187.             pholder = processlist;
  188.             processlist = processlist->next;
  189.             free(pholder);
  190.          }   while(processlist!=NULL);
  191.       exit(condition);
  192.    }
  193.  
  194.  
  195. int               install_handler(void)
  196.    {
  197.       void               *new_vector;
  198.  
  199.       _sim_init_val=2;
  200.       old_vector = getvect(_INTRPT_);
  201.       setvect(_INTRPT_,task_handler);
  202.       new_vector = getvect(_INTRPT_);
  203.       if(new_vector==old_vector)
  204.          return(1);
  205.       return(0);
  206.    }
  207.  
  208.  
  209. void   interrupt   task_handler( unsigned bp,unsigned di,unsigned si,
  210.                                  unsigned ds,unsigned es,unsigned dx,
  211.                                  unsigned cx,unsigned bx,unsigned ax,
  212.                                  unsigned ip,unsigned cs,unsigned flags)
  213.    {
  214.       int                    notfound;
  215.       struct   _simprocessT *procpntr;
  216.  
  217.       if(_sim_init_val==2)
  218.          /* if this ever happens, it means that the task handler
  219.             was installed before it should have been, so de-install
  220.             it and stop the program */
  221.          {
  222.             exit_processing(10);
  223.          }
  224.       if(_sim_init_val==0) /* sim_start has been run */
  225.          {
  226.             processlist->reg_bp      = _BP;
  227.             processlist->reg_sp      = _SP;
  228.             processlist->reg_ss      = _SS;
  229.  
  230.             if(processlist->status.kill_flag)
  231.                {
  232.                   if(processlist == kbprocess)
  233.                      /* You can,t kill the kbproc */
  234.                      processlist->status.kill_flag = 0;
  235.                   else
  236.                      {
  237.                         if(processlist->next == processlist)
  238.                            /* no more procs */
  239.                            exit_processing(20);
  240.  
  241.                         /* break list */
  242.                         processlist->prev->next = processlist->next;
  243.  
  244.                         /* move in front of break */
  245.                         processlist = processlist->prev;
  246.  
  247.                         /* free dead proc */
  248.                         free(processlist->next->prev);
  249.  
  250.                         /* fix break */
  251.                         processlist->next->prev = processlist;
  252.                      }
  253.                }
  254.  
  255.             if((kbprocess!= NULL)&&(bioskey(1)))
  256.                processlist = kbprocess;
  257.             else
  258.                {
  259.                   if(_sim_time_ratio != 0.0)
  260.                      {
  261.                         for(notfound = 1;notfound;)
  262.                            {
  263.                               if((kbprocess != NULL)&&(bioskey(1)))
  264.                                  {
  265.                                     processlist = kbprocess;
  266.                                     notfound = 0;
  267.                                  }
  268.                               else
  269.                                  {
  270.                                     processlist = processlist->next;
  271.                                     if((*systimer - _last_update_time)>
  272.                                        (1/_sim_time_ratio))
  273.                                        {
  274.                                           _sim_system_time +=
  275.                                              (*systimer -
  276.                                               _last_update_time) *
  277.                                              _sim_time_ratio;
  278.                                           _last_update_time = *systimer;
  279.                                        }
  280.                                     if((processlist->start_time <=
  281.                                         _sim_system_time)&&
  282.                                        (processlist->status.wait_flag==0))
  283.                                        notfound = 0;
  284.                                  }
  285.                            }
  286.                         _sim_system_time = processlist->start_time;
  287.                      }
  288.                   else
  289.                      {
  290.                         if(processlist->status.wait_flag)
  291.                            {
  292.                               for(procpntr=processlist->next;
  293.                                   procpntr&&
  294.                                   (procpntr->status.wait_flag)&&
  295.                                   (procpntr->status.kill_flag);)
  296.                                  {
  297.                                     procpntr = procpntr->next;
  298.                                     if(procpntr == processlist)
  299.                                        procpntr = NULL;
  300.                                  }
  301.                               if(procpntr == NULL)
  302.                                  {
  303.                                     fprintf(stderr,"waitlock\n");
  304.                                     exit_processing(1);
  305.                                  }
  306.                               _sim_system_time = procpntr->start_time;
  307.                            }
  308.                         else
  309.                            _sim_system_time = processlist->start_time;
  310.                         procpntr = processlist->next;
  311.                         while(procpntr!= processlist)
  312.                            {
  313.                               if((!procpntr->status.wait_flag)&&
  314.                                  (_sim_system_time > procpntr->start_time))
  315.                                  _sim_system_time = procpntr->start_time;
  316.                               procpntr = procpntr->next;
  317.                            }
  318.                         processlist = processlist->next;
  319.                         while((processlist->status.wait_flag||
  320.                                processlist->status.kill_flag)||
  321.                               (processlist->start_time > _sim_system_time))
  322.                            processlist = processlist->next;
  323.                         _last_update_time = *systimer;
  324.                      }
  325.                }
  326.  
  327.             CurrentTime = _sim_system_time/18.2;
  328.             disable();
  329.             _SP     = processlist->reg_sp;
  330.             _SS     = processlist->reg_ss;
  331.             _BP     = processlist->reg_bp;
  332.             enable();
  333.  
  334.             if(processlist->init==1)
  335.                /* process is in its first step, so initialize it */
  336.                {
  337.                   ax       = processlist->reg_ax;
  338.                   bx       = processlist->reg_bx;
  339.                   cx       = processlist->reg_cx;
  340.                   dx       = processlist->reg_dx;
  341.                   es       = processlist->reg_es;
  342.                   ds       = processlist->reg_ds;
  343.                   si       = processlist->reg_si;
  344.                   di       = processlist->reg_di;
  345.                   bp       = processlist->reg_bp;
  346.                   ip       = processlist->reg_ip;
  347.                   cs       = processlist->reg_cs;
  348.                   flags    = processlist->reg_flag;
  349.                   processlist->init = 0;
  350.                }
  351.          }
  352.       else  /* sim_start has not been run */
  353.          {
  354.             if(_sim_init_val==1)
  355.                {
  356.                   processlist->reg_ax      = ax;
  357.                   processlist->reg_bx      = bx;
  358.                   processlist->reg_cx      = cx;
  359.                   processlist->reg_dx      = dx;
  360.                   processlist->reg_es      = es;
  361.                   processlist->reg_ds      = ds;
  362.                   processlist->reg_si      = si;
  363.                   processlist->reg_di      = di;
  364.                   processlist->reg_ip      = FP_OFF(glbl_fc);
  365.                   processlist->reg_cs      = FP_SEG(glbl_fc);
  366.                   processlist->reg_flag    = flags;
  367.                   processlist->reg_bp      = _BP;
  368.                   processlist->reg_sp      = _SP;
  369.                   processlist->reg_ss      = _SS;
  370.                   processlist->init        = 1;
  371.                   cs = cs;
  372.                   ip = ip;
  373.                   bp = bp;
  374.                }
  375.          }
  376.    }
  377.  
  378.  
  379. int               set_time_ratio      (float   ratio)
  380.    {
  381.       _sim_time_ratio = ratio;
  382.       return(0);
  383.    }
  384.  
  385.  
  386. int               my_process_id(void)
  387.    {
  388.       return(processlist->proc_id);
  389.    }
  390.  
  391.  
  392. int               wait_until_time   (long unsigned   starttime)
  393.    {
  394.       if(starttime < (_sim_system_time/18.2))
  395.          {
  396.             printf("Waiting for a time that is past in %d",
  397.                     processlist->proc_id);
  398.             exit_processing(-1);
  399.          }
  400.       wait_for_time((float)(starttime*18.2 - _sim_system_time)/18.2);
  401.       return(0);
  402.    }
  403.  
  404.  
  405. int               wait_for_time      (float   delaytime)
  406.    {
  407.       if(_sim_time_ratio == 0)
  408.          {
  409.             processlist->start_time =
  410.                _sim_system_time + delaytime*18.2;
  411.          }
  412.       else
  413.          processlist->start_time =
  414.             _sim_system_time + (long)(18.2 * delaytime);
  415.  
  416.       geninterrupt(_INTRPT_);
  417.       return(0);
  418.    }
  419.  
  420.  
  421. int               init_post            (char   *postname)
  422.    {
  423.       struct   _postT      *post;
  424.  
  425.       for(post = postlist;post != NULL;post = post->next)
  426.          {
  427.             if(strcmp((char *)&post->name,postname)==0)
  428.                return(post->handle);
  429.          }
  430.       post = (struct _postT *)calloc(sizeof(struct _postT),1);
  431.       if(post==NULL)
  432.          return(-1);
  433.  
  434.       post->handle = totalposts++;
  435.       post->value = NULL;
  436.       strcpy((char *)&post->name,postname);
  437.  
  438.       post->waiting = calloc(1,sizeof(void *));
  439.       post->waiting[0] = NULL;
  440.  
  441.       post->next = postlist;
  442.       post->prev = NULL;
  443.  
  444.       postlist->prev = post;
  445.       postlist = post;
  446.       return(post->handle);
  447.    }
  448.  
  449.  
  450. int               set_post           (int    posthandle,void   *pointer)
  451.    {
  452.       struct   _postT       *post;
  453.       struct   _simprocessT *procpntr;
  454.       int                   loop;
  455.       for(post = postlist;post != NULL;post = post->next)
  456.          {
  457.             if(post->handle==posthandle)
  458.                {
  459.                   if(post->value != NULL)
  460.                      {
  461.                         wait_for_time(0);
  462.                         if(post->value != NULL)
  463.                            return(post->handle);
  464.                      }
  465.                   if(post->waiting[0] != NULL)
  466.                      {
  467.                         post->waiting[0]->status.wait_flag = 0;
  468.                         post->waiting[0]->start_time = _sim_system_time;
  469.                         loop = 0;
  470.                         procpntr = processlist->next;
  471.                         while(procpntr != processlist)
  472.                            {
  473.                               if((procpntr->status.wait_flag)&&
  474.                                  (procpntr->waitpost == post))
  475.                                  {
  476.                                     post->waiting[loop] = procpntr;
  477.                                     loop++;
  478.                                  }
  479.                               procpntr = procpntr->next;
  480.                            }
  481.                         post->waiting[loop] = NULL;
  482.                      }
  483.                   post->value = pointer;
  484.                   wait_for_time(0);
  485.                   return(0);
  486.                }
  487.          }
  488.       return(-posthandle);
  489.    }
  490.  
  491.  
  492. void              *get_post            (int    posthandle)
  493.    {
  494.       struct   _postT      *post;
  495.       void                 *ret;
  496.  
  497.       for(post = postlist;post != NULL;post = post->next)
  498.          {
  499.             if(post->handle==posthandle)
  500.                {
  501.                   ret = post->value;
  502.  
  503.                   if(ret==NULL)
  504.                      return(ret);
  505.  
  506.                   post->value = NULL;
  507.                   return(ret);
  508.                }
  509.          }
  510.       return(NULL);
  511.    }
  512.  
  513.  
  514. void              *wait_post         (int    posthandle)
  515.    {
  516.       struct   _postT      *post;
  517.       void                 *ret;
  518.       int                  loop;
  519.  
  520.       for(post = postlist;post != NULL;post = post->next)
  521.          {
  522.             if(post->handle==posthandle)
  523.                {
  524.                   ret = post->value;
  525.  
  526.                   if(ret==NULL)
  527.                      {
  528.                         for(loop=0;post->waiting[loop];)
  529.                            loop++;
  530.                         post->waiting =
  531.                            realloc(post->waiting,(loop+2)*sizeof(void*));
  532.                         post->waiting[loop] = processlist;
  533.                         post->waiting[loop+1] = NULL;
  534.                         while(ret == NULL)
  535.                            {
  536.                               processlist->status.wait_flag = 1;
  537.                               processlist->waitpost = postlist;
  538.                               wait_for_time(0);
  539.                               ret = post->value;
  540.                               post->value = NULL;
  541.                            }
  542.                         processlist->waitpost = NULL;
  543.                         return(ret);
  544.                      }
  545.  
  546.                   post->value = NULL;
  547.                   return(ret);
  548.                }
  549.          }
  550.       return(NULL);
  551.    }
  552.