%begin
!DAK APM Alarm clock booting program
!DAK (C) David A Kerr, CS4, December 1984
!DAK preloads the background job and starts clock.
!DAK prompts for an alarm time and then returns to command level.
!DAK the background job is 68000 assembler in USEFUL:TIMEINT.ASM and .MOB
  %option  "-low-nocheck-nodiag-notrace-noline-nostack"
  %include "INC:UTIL.IMP"
  %include "INC:DICT.IMP"
  %include "CLOCK:MSFTIME.INC"

  @16_1078 %integer lintvec
  @16_107A %short sintvec
  @16_365E %short %integer lend
  %label gettime
  %conststring(25) str = "CLOCK:TIMEINT.MOB"
  %conststring(25) thisfile = "CLOCK:ALARM"
  %ownbytearray mess(1:7)=16_72,0,0,0,0,0,6
  %record%format f1 (%half feoo,hours,minutes,seconds,
                           alhrs,almins,alsecs,tsec,onoff)
  %record(f1)%name ft
  %record(msf fm) mtime
  %string(255) fred, s
  %string(31) now
  %integer end, start, update, return, i, deadline
  
  %routine ptime(%integer h,m,s)
    printsymbol('0') %if h < 10
    write(h,0); printsymbol(':')
    printsymbol('0') %if m < 10
    write(m,0); printsymbol(':')
    printsymbol('0') %if s < 10
    write(s,0)
  %end

  start = refname(str,fildict)
  %if start > 0 %start
    !** background job object already preloaded
    ft == record(integer(start))
    end = integer(start+4)
    update = end-2+shortinteger(end-2)
    return = end-6+shortinteger(end-6)

    %if ft_feoo # 16_FE00 %start
      !** clock program already going, so disable
!     i = shortinteger(return+2)
!     lintvec = i
!     ft_feoo = 16_FE00
!**   with comments removed the interrupt vector would be returned to system.
      %if ft_alhrs # 25 %start
        ft_alhrs = 25
        ft_onoff = 0
        printstring("Alarm disabled")
        newline
        %stop
      %finish
    %finish

    %if ft_feoo = 16_FE00 %start
      !** clock program not already running *!
      !** first try VAX for the time
      printstring("Clock not running, asking MSF clock for time.".snl)
      mtime = msftime(16_15)
      %if mtime_state#0 %start
        printstring ("Error")
        write(mtime_state,1)
        printstring (" - ".mtime_full time.snl)
        printstring ("MSF failed, trying VAX for time.".snl)
        deadline = cputime+4000
        etherwrite(0,mess(1),7)
        i = 0
        %cycle
          %if cputime>deadline %start
            printstring("VAX failed, trying filestore for time".snl)
            now = time
            i = -1
          %finish
        %repeatuntil dtx&1<<0#0 %or i # 0
        %if i = 0 %start
          length(s) = etherread(0,charno(s,1),255)
          now = substring(s,20,length(s))
          now = now."00000000" %if length(now) < 8
        %finish %else now = now.":00"
        ft_hours = stoi(substring(now,1,2))
        ft_minutes = stoi(substring(now,4,5))
        ft_seconds = stoi(substring(now,7,8))
      %finish %else %start
        ft_hours = mtime_current time_hour
        ft_minutes = mtime_current time_minute
        ft_seconds = mtime_current time_second
      %finish
      ft_feoo = 16_00FE
      ft_tsec = 0
      ft_onoff = 0
      ft_alhrs = 25
      !** above impossible times so alarm will never sound
      shortinteger(return+2) = sintvec
      lintvec = update
      !** the background job is now running
    %finish
    %if ft_alhrs = 25 %start
      printstring("The time now is ")
      ptime(ft_hours,ft_minutes,ft_seconds)
      printstring(" clock running.")
      newline
      printstring("When would you like the alarm to sound?   (HH MM SS)")
gettime:
      read(ft_alhrs)
      read(ft_almins)
      read(ft_alsecs)
      %if %not(0<=ft_alhrs<=23 %and 0<=ft_almins<=59 %and 0<=ft_alsecs<=59) %start
        printstring("Invalid 24 hour notation, have another go (HH MM SS)")
        -> gettime
      %finish
      printstring("Alarm will sound at ")
      ptime(ft_alhrs,ft_almins,ft_alsecs)
      printstring(" Time now is ")
      ptime(ft_hours,ft_minutes,ft_seconds)
      newline
      printstring("To stop alarm sounding re-run this program")
      newline
    %finish
  %finish %else %start
    !** background job object not preloaded, preload then re-execute this
    fred = "preload ".str.snl." ".thisfile.snl
    %for i=1,1,length(fred) %cycle
      byte integer(lend+i-1)=charno(fred,i)
    %repeat
    lend = lend + length(fred)
  %finish

%endofprogram
