home *** CD-ROM | disk | FTP | other *** search
/ ftp.whtech.com / ftp.whtech.com.tar / ftp.whtech.com / club100 / ref / watchdog.doc < prev    next >
Text File  |  2006-10-19  |  11KB  |  88 lines

  1. WATCHDog
  2. A background timer utility...
  3.  
  4. by Tracy Allen
  5. EME Systems, 2018 Parker St., Berkeley CA 94704
  6.  
  7. A "watchdog" is a specialized piece of hardware or software that monitors a computer's activity and can bring it back into operation if anything short of catastrophic goes wrong.  The watchdog itself is simpleminded. All it watches is it's food dish, empty or full, zero or one.  Empty means simply that normal program flow has ceased, and somebody hasn't gotten around to feeding the dog.  It then barks a "reset" or "bootstrap" command.  If the computer is set up right, bingo!  The application program will be restarted in an orderly manner. 
  8.  
  9. Now, it sometimes happens that applications running on the M100/2 become stuck.  Some examples are:  the LPRINT command or the PRINT button when the printer is off line or out of paper; a telephone autologin when the phone at the other end doesn't answer or is garbled; a cat's paw on the PAUSE button; a user who forgets to respond to a prompt; a bug in the program code;  a lightning-induced trip to never-never landI  These are usually no big problem if someone is right there who knows what to do, but it is a different story when the system operates unattended.  The situation might be a remote-site data logger, or an automatic telephone information exchange system, or any system that has to be operated by computer-illiterate personnel.  These situations call for a watchdog to keep things running.  A watchdog can help with everyday programming too, as I'll show in the autologin routine I use as an example below. 
  10.  
  11. WATCHDog is 29 bytes of machine code, tethered to the clock-cursor-keyboard background task.  You should know that the background task is the heartbeat of the M100/2, code executed in response to a hardware interrupt from the uPD1990 clock chip 256 time per second, that is,once every 3.90625 milliseconds.  Rarely, it will skip a beat if there is lots of writing to the LCD screen, printer, etc.  The task resides mostly in ROM, but there is a "hook", a  location in RAM (bytes 62975-62977) that is called right at the beginning of the task.  We will replace the RETurn op code that is initially there with a JuMP to WATCHDog, which we will  have already loaded into protected RAM.  (See listing 1.)  WATCHDog concludes with a RETurn, to the rest of the background task.  So WATCHDog will be executed 256 times per second along with the rest of the background task.  
  12.  
  13. When called upon to do so, WATCHDog acts as a countdown timer.  A POKE from the application program (the one being watched) loads a time interval into WATCHDog.  Normally, the application re-ups the interval periodically, so the time never runs down to zero.  But if something does go wrong and the time runs out, WATCHDog calls the M100 warm boot routine.  The main menu then appears, but just for an instant.  Our application had named itself in an IPL command, so now it restarts automatically.  That's all there is to it!  WATCHDog also raises a "flag", which can subsequently be seen by the restarting program to mean, "dog barked." 
  14.  
  15. Why not simply jam a BREAK character into the keyboard buffer, or use the ON TIME$ facility to do the job of pulling the program back from never-never land?  Unfortunately, the most problematic I/O routines don't use the standard breakcheck callQthey check the keyboard directly.  Nix also the ON TIME$ interrupt, which BASIC checks between execution of statements, but not during them.  Fortunately, the background task ticks on during these operations.  If you think about it, the jump to warm boot covers a lot of possibilities at one stroke and leaves the programmer fewer responsibilities.  Just feed the dog and listen when it barks. 
  16.  
  17. To clarify how it works in practice, here is an example using WATCHDog. LOGIN.BA, listing 2, is a telephone dialer/redialer and autologin routine. It attempts to call and log onto Rick Hanson's Club 100 BBS.  If it doesn't succeed the first time, it will try again, up to five retries. When it does succeed in getting through, it transfers to the TELCOM terminal mode.  You can use this routine exactly as it is written to log on to the BBS.  Rick has set up an account for a user named "Portable Guest" who lives in "Portable Magsville" and uses the password, "PASSWORD".  You will find yourself at the main menu of the BBS and ready to explore.  If you like it, sign on later under your own name.  (You can try the BBS without using this fancy program, too, of course.) 
  18.  
  19. The TELCOM autologin routine called in line 180 of listing 2 is one of those that can stick, due to phone busy, bad data, etc.  Once stuck, it sticks forever until you hit BREAK.  WATCHDog does that for you, and also counts the redials. 
  20.  
  21. Program flow:  Line 20 of LOGIN.BA tests the "hook" byte to determine whether or not WATCHDog is already active.  If not, that is, if the byte=201=RETurn, then the BASIC subroutine starting at line 1000 loads WATCHDog into space CLEARed in high memory and redirects the computer to include WATCHDog in the background task.  In line 30, the WATCHDog flag is checked to see how many redials have occurred, and the program stops if there have already been five.  (WATCHDog increments the flag byte at 92959 each time it executes a restart.)  Line 40 defines the autologin sequence in the standard M102 format.  Lines 50 &60 define the COM: settings and POKE them into memory where the computer will look to find them when it goes to dial the phone.  Line 70 sets the program up for automatic restart via the IPL"LOGIN.BA" instruction.  If this is the first try to dial, the program jumps from line 80 right into the dialing, otherwise lines 90 to 120 provide a 30 second delay between retries.  This delay comes from the WATCHDog timer, but LOGIN.BA doesn't let the dog bark.  It stops short of that.  You can see how the bark can be tamed and used for other purposes. The WATCHDog as such is really activated in line 130, to allow 60 seconds to complete the login. Then down to business:  The CALL  in line 160 passes control to the M100's built-in TELCOM autologin routines.  The autologin either succeeds and the program arrives at line 170, or it sticks, and the WATCHDog timer reboots the system, and LOGIN.BA starts over from the beginning with the next higher retry number.  If it does get to line 170, the program beeps and transfers directly to TELCOM's terminal mode with the CALL 21608 in line 180, after disabling WATCHDog.  When you sign off, you return to the MENU, not to BASIC. 
  22.  
  23. Note that before exiting, the program disables the WATCHDog.  Don't press BREAK to exit from this program!  It will just restart on you after 60 seconds.  Likewise if you press PAUSE or PRINT without a printer on line, the program won't let you stop it Ptry!  That's the idea.  All roads lead back to the program.  You can disable the WATCHDog from BASIC by  typing POKE 62975,201 (no typos, please!), or alternatively, if  LOGIN.BA is loaded, RUN 190. 
  24.  
  25. My own use for WATCHDog is in remote-site data loggers.  There the WATCHDog hardly ever barks, because the main loop of the data-logging program repeatedly feeds it and feeds it.  It is insurance, like a real watchdog.  With it the data-logging program, including printer and phone support, is much simpler and safer than it otherwise could have been. 
  26.  
  27. Listing #1:
  28.  
  29. The assembly code program WATCHDog is executed 256 times per second as part of the CCK background task.  It begins by saving the processor status and the contents of the HL register.  It then checks its seconds-counter variable.  If the variable = zero, that means WATCHDog is disabled, and a JuMP is made to the end of WATCHDog, where the HL register and processor status are restored, and a RETurn is made back to the background task.  If the seconds counter is not already zero, then the routine proceeds to tick off the time.  There are two counters, one for 1/256ths of a second, one for seconds (maximum= 255 seconds=4.25 minutes).  When the seconds counter decrements to zero, WATCHDog puts up a flag (by incrementing the FLG byte) and jumps to the warm boot routine by executing the RST0 op code. 
  30.  
  31.  
  32.         ORG 62931       ;load in high memory starting at 62931
  33. STRT:   PUSH PSW        ;save registers
  34.         PUSH HL ;
  35.         LXI H, CNTS     ;point to seconds counter
  36.         MOV A,M ;test for seconds = zero
  37.         ORA A   ;
  38.         JZ EXIT ;zero means WATCHDog disabled--exit
  39.         DCX H   ;point to 1/256th seconds counter
  40.         DCR M   ;decrement counter
  41.         JNZ EXIT        ;exit if not zero
  42.         INX H   ;point to seconds counter
  43.         DCR M   ;decrement seconds
  44.         JNZ EXIT        ;exit if not zero
  45.         INX H   ;point to time-out flag
  46.         INR M   ;increment flag
  47.         RST 0   ;warm boot
  48. EXIT:   POP H   ;restore registers
  49.         POP PSW ;
  50.         RETURN  ;to cck background task
  51. CNT:    DB 0    ;count 1/256ths second
  52. CNTS:   DB 0    ;count seconds    62958
  53. FLG:    DB 0    ;time-out flag    62959
  54.  
  55. Listing #2: Program "LOGIN.BA"
  56.  
  57. The usual precautions about backing up all your work before trying a new machine language program apply here.  If you have other routines in high memory, you will have to relocate WATCHDog.  Line 40 defines PH$, the autologin sequence, following conventions in the model 100/2 operation manual.  Residents of the 510 area code should delete the initial 1510.  Monitor the call the first time through to be sure you have the phone number correct, so you don't infuriate somebody.  The byte at 62958 is the WATCHDog seconds counter, and the byte at 62959 counts the number of timeouts, and the bytes at 62975-62977 are the "hook" to the background task. 
  58.  
  59. 10 REM__public domain program "LOGIN.BA" by Tracy Allen 510-848-5725
  60. 20 IF PEEK(62975)=201 THEN CLEAR256,62930:GOSUB1000    'load WATCHDog
  61. 30 IF PEEK(62959)>5 THEN BEEP :GOTO190      'already 5 tries->menu
  62. 40 PH$="15109391246<??PORTABLE^M??GUEST^M??Y?:PASSWORD^M?:?:=>" 
  63. 41 REM above line is standard BBS autologin sequence
  64. 50 CP$="M8N1E"                        'modem parameters
  65. 60 FORI=1TO5:POKE63066+I,ASC(MID$(CP$,I,1)):NEXT   'set baud rate etc
  66. 70 CLS:IPL "LOGIN.BA"            'autoboot this program
  67. 80 IFPEEK(62959)=0THEN130ELSEPRINT"Delaying 30 for try:";1+PEEK(62959)
  68. 90 PRINT"Hit ESC key to exit"
  69. 100 POKE62958,32             'set timer for 32 sec.
  70. 110 IFINKEY$=CHR$(27)THEN190       'user pressed ESC->menu
  71. 120 IFPEEK(62958)>2THENPRINT@9,USING"##";PEEK(62958)-1;:GOTO110
  72. 121 REM lines 80->120 allow 30 sec delay between redials or user ESCape
  73. 130 POKE 62958,60:CLS:PRINT"Now dialing...";    'allow 60 seconds
  74. 140 CALL 21200 'connect to phone line
  75. 150 M=VARPTR(PH$):AD=PEEK(M+1)+256*PEEK(M+2) 'find PH$ in memory
  76. 160 CALL 21293,0,AD        'dial & autologin
  77. 170 BEEP            'successful!
  78. 180 POKE62975,201:IPL"":CALL21608  'disable WATCHDog & enter telcom
  79. 190 POKE62975,201:IPL"":MENU       'disable WATCHDog & exit to MENU
  80. 1000 REM__subprogram loads WATCHDog from 62931 to 62959 into RAM
  81. 1010 RESTORE1040:FORI=1TO29:READ D%:POKE 62930+I,D%:NEXT   'load
  82. 1020 POKE 62977,245:POKE 62976,211:POKE 62975,195     'hook
  83. 1030 RETURN
  84. 1040 DATA 245,229,33,238,245,126,183,202,234,245,43,53,194,234
  85. 1050 DATA 245,35,53,194,234,245,35,52,199,225,241,201,0,0,0
  86.  
  87. end...
  88.