home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 2: PC / frozenfish_august_1995.bin / bbs / d09xx / d0904.lha / IdleLED / IDLE_LED.C < prev    next >
C/C++ Source or Header  |  1993-08-26  |  3KB  |  99 lines

  1.  
  2. /*
  3.  
  4. Program:    Idle Led
  5. Version:    1.0a    7/7/93
  6. Author:        Lindsay Meek (meek@fizzy.csu.murdoch.edu.au)
  7.  
  8. Description:    Whenever the CPU is busy, the power led goes on else it is off.
  9.         It operates using timerA on CIAB. This is continuosly reset
  10.         by the idle task running at low priority (-127). When the
  11.         idle task is preempted by anything, the timer is allowed to
  12.         underflow causing the interrupt server to run which turns on
  13.         the power light. Next time the idle task runs, it immediately
  14.         turns off the power light.
  15.         
  16.         Modifying the timer reload value below will change the
  17.         responsiveness of the led to the changes in CPU load. 
  18.  
  19. */
  20.  
  21. /* I think this is about 10 ms */
  22. #define TIMER_DELAY 7000
  23.  
  24. #include <exec/nodes.h>
  25. #include <exec/tasks.h>
  26. #include <exec/interrupts.h>
  27. #include <hardware/cia.h>
  28. #include <libraries/dos.h>
  29. #include <resources/cia.h>
  30.  
  31. struct Task *FindTask(char *);
  32.  
  33. /* CIA resource calls */
  34. struct MiscResource;
  35. long AbleICR(struct MiscResource *resource, long mask);
  36. struct Interrupt *AddICRVector(struct MiscResource *resource, long iCRBit, struct Interrupt *interrupt);
  37. void RemICRVector(struct MiscResource *resource, long iCRBit, struct Interrupt *interrupt);
  38. long SetICR(struct MiscResource *resource, long mask);
  39. struct MiscResource *OpenResource(char *);
  40.  
  41. struct MiscResource *ciabase=NULL;
  42.  
  43. IdleIrq(void)
  44. {
  45.     /* Turn on power light */
  46.     ciaa.ciapra &= -1-CIAF_LED;
  47.  
  48.     return 0;    /* Set Z flag so other servers will execute */
  49. }
  50.  
  51. /* Run this as a process. It aborts when it detects a CTRL_C signal */
  52. main(int argc,char **argv)
  53. {
  54.     struct Task *met;
  55.     BYTE oldpri;
  56.     struct Interrupt mei;
  57.  
  58.     if(NULL==(ciabase=OpenResource(CIABNAME)))
  59.     { printf("Couldn't aquire %s\n",CIABNAME); exit(1); }
  60.  
  61.     mei.is_Node.ln_Type = NT_INTERRUPT;
  62.     mei.is_Node.ln_Pri  = 127;
  63.     mei.is_Node.ln_Name = "Idle-Led-Off";
  64.     mei.is_Data = NULL;
  65.     mei.is_Code = IdleIrq;
  66.  
  67.     AddICRVector(ciabase,CIAICRB_TA,&mei);    /* Install IRQ server */
  68.  
  69.     met=FindTask(NULL);        /* Fetch this tcb */
  70.     oldpri=met->tc_Node.ln_Pri;    /* Save old priority */
  71.     met->tc_Node.ln_Pri=-127;    /* Make priority VERY low for idle */
  72.  
  73.     ciab.ciacra    = 0;                /* Stop timer */
  74.     ciab.ciatalo   = TIMER_DELAY & 0xff;
  75.     ciab.ciatahi   = TIMER_DELAY >> 8;         /* Load up timer delay */
  76.  
  77.     SetICR(ciabase,CIAICRF_TA | CIAICRF_SETCLR);    /* Enable IRQ */
  78.  
  79.     /* Busy loop for eating idle cpu cycles */
  80.     while((met->tc_SigRecvd & SIGBREAKF_CTRL_C)==0)    /* Abort on CTRL_C */
  81.     {
  82.         /* Reset timer. Started with force load and one-shot */
  83.         ciab.ciacra = CIACRAF_START | CIACRAF_LOAD | CIACRAF_RUNMODE;
  84.  
  85.         /* Turn off power light */
  86.         ciaa.ciapra |= CIAF_LED;
  87.     }
  88.  
  89.     ciab.ciacra    = 0;                /* Stop timer */
  90.  
  91.     SetICR(ciabase,CIAICRF_TA);            /* Disable interrupt */
  92.  
  93.     RemICRVector(ciabase,CIAICRB_TA,&mei);        /* Remove IRQ server */
  94.  
  95.     met->tc_Node.ln_Pri=oldpri;            /* Change priority back */
  96.  
  97.     return 0;
  98. }
  99.