home *** CD-ROM | disk | FTP | other *** search
/ Crawly Crypt Collection 1 / crawlyvol1.bin / telecomm / caltim31 / calltime.s < prev    next >
Text File  |  1994-01-15  |  23KB  |  512 lines

  1.  ILABEL E:\ASSEMBLE\SOURCE\TOS\TOS.L
  2.  
  3. ; This program sets GEMDOS clock after receiving time from Naval
  4. ; Observatory in Washington D.C.  The D.C. Observatory spits out the
  5. ; julian date and that must be converted to the _wonderful_ system the
  6. ; ST uses to store the time and date (yea, right!) with the following
  7. ; formula (please don't ask me how this formula works, I downloaded it
  8. ; from the Observatory):
  9.  
  10. ; L=Julian date + 2468570
  11. ; N=4 * L/146097
  12. ; L = L - (146097 * N + 3) / 4
  13. ; Y = 4000 * (L + 1) / 1461001
  14. ; L = L - (1461 * Y / 4) + 31
  15. ; M = 80 * L / 2447
  16. ; DAY = L - (2447 * M / 80)          DAY = DAY OF MONTH
  17. ; L = M / 11
  18. ; MONTH = M + 2 - (12 * L)         MONTH = CURRENT MONTH
  19. ; YEAR = 100 * (N - 49) + Y + L     YEAR = CURRENT YEAR
  20.  
  21. ; PLEASE DO NOT TRY TO FIGURE THIS FORMULA OUT AT HOME.  YOU MAY INJURE
  22. ; YOURSELF
  23.  
  24.  TEXT
  25.                  dc.w    $a00a          ;hide mouse
  26.                  bsr     clearrs232     ;clear serial port of garbage
  27.                  bsr     getdat         ;load the DAT file
  28.                  bsr     dial           ;output string to serial
  29.                  bsr     getinput       ;get input from Observatory
  30.                  bsr     asc2dec        ;convert text into decimal format
  31.                  bsr     jd2greg        ;convert julian date to gregorian
  32.                  bsr     setclock       ;convert greg to GEMDOS time and set
  33.                  bsr     curandreal     ;print current and actual time
  34.                  printline finished
  35. quit:
  36.                  supexec hangup
  37.                  conin_we               ;wait for keypress (without echo)
  38.                  term                   ; MILLER TIME!
  39.  
  40. ;***************************** CLEARRS232 *****************************
  41. clearrs232:
  42.                  printline intro
  43.                  rsconf  #-1,#-1,#-1,#-1,#0,#7   ;set port to 1200 baud
  44. clear:
  45.                  auxistat               ;check for junk characters
  46.                  tst     d0             ;any there?
  47.                  beq     rsdone         ;NO!
  48.                  auxin                  ;get and trash unwanted char
  49.                  bra     clear          ;do until no more characters
  50. rsdone:
  51.                  rts                    ;finí
  52.  
  53. ;******************************* GETDAT ******************************
  54. getdat:
  55.                  open    #0,fname       ;open file
  56.                  tst.l   d0             ;error?   (file not found)
  57.                  bmi     createfile     ;yes, create
  58.  
  59.                  move.w  d0,handle      ;save handle
  60.  
  61.                  read    timez,#1,handle         ;read timezone data
  62.                  read    initstring,#80,handle   ;read initialization
  63.                  move.l  d0,length               ;save length
  64.                  close   handle                  ;close file
  65.                  rts
  66. ;******************************* CREATEFILE **************************
  67. createfile:
  68.                  printline  filenotfound     ;print error message
  69.                  create  #0,fname            ;create file
  70.                  move.w  d0,handle           ;save handle
  71.  
  72.                  conin_we                    ;get timezone input
  73.                  sub.b   #$31,d0             ;convert to decimal-1
  74.                  move.b  d0,timez            ;save timezone input
  75.                  write   timez,#1,handle     ;write to file
  76.  
  77.                  printline initst            ;print init message
  78.  
  79.                  move.l  #initstring,a3      ;address of buffer
  80.                  clr.l   d3                  ;clear garbage
  81. inputloop:
  82.                  conin                  ;get keyboard input
  83.                  cmp.b   #26,d0         ;ctrl Z??
  84.                  beq     loopdone       ;if yes, we're done inputting
  85.                  cmp.b   #$8,d0         ;backspace?
  86.                  beq     bs             ;if yes, handle backspace
  87.                  cmp.b   #$d,d0         ;RETURN?
  88.                  beq     ret            ;yes, handle it
  89.                  move.b  d0,(a3)+       ;save input in buffer
  90.                  add.l   #1,d3          ;keep track of length of input
  91.                  bra     inputloop      ;keep inputting
  92.  
  93. ret:
  94.                  move.b  #$d,(a3)+
  95.                  move.b  #$a,(a3)+
  96.                  add.l   #2,d3
  97.                  conout  #$a
  98.                  bra     inputloop
  99.  
  100. bs:
  101.                  conout  #32            ;erase letter
  102.                  conout  #8             ;move back
  103.                  sub.l   #1,d3          ;subtract from length of input
  104.                  tst.l   d3             ;did we hit backspace too much?
  105.                  bmi     toofar         ;yes, start from ZERO
  106.                  move.b  #0,-(a3)       ;we're ok, so delete last input
  107.                  bra     inputloop      ;keeping inputting
  108. toofar:
  109.                  clr.l   d3             ;0 length
  110.                  bra     inputloop      ;keep inputting
  111.  
  112. loopdone:
  113.                  write   initstring,d3,handle     ;save init file
  114.                  move.l  d3,length                ;save length
  115.                  close   handle                   ;close file
  116.                  printline intro
  117.                  rts                              ;done
  118.  
  119. ;******************************* DIAL ********************************
  120. dial:
  121.                  printline dialing                  ;dialing message
  122.                  write   initstring,length,#2       ;output to modem
  123.                  write   phnum,#9,#2               ;output ph #
  124.                  rts
  125.  
  126. ;***************************** GETINPUT *******************************
  127. getinput:
  128.                  move.l  #0,d3          ;wait 0 seconds
  129.                  supexec tloop0         ;current time will be returned in d4
  130.                  move.l  curtime,starttime   ;save beginning time...
  131. gi2:
  132.                  auxistat               ;is a character waiting?
  133.                  tst.l   d0             ;if d0=0, no char waiting...
  134.                  beq     timer          ;no char, check time
  135.                  auxin                  ;get character from serial
  136.                  cmp.b   #"*",d0        ;is it header character
  137.                  beq     receive        ;yes, get time...
  138. timer:
  139.                  constat                ;keypress?
  140.                  tst.l   d0             ;if d0=-1, char waiting
  141.                  bmi     interrupt      ;see if it's a space
  142. tcont:
  143.                  move.l  #0,d3          ;wait 0 again...
  144.                  supexec tloop0         ;get current time
  145.                  move.l  starttime,d4   ;get starting time
  146.                  sub.l   d4,curtime     ;get time difference
  147.                  cmp.l   #6000,curtime  ;less than 10 seconds
  148.                  blt     gi2            ;ok, less than 10 seconds
  149.  
  150.                  printline timeout      ;TIMEOUT ERROR
  151.                  bra     quit           ;term prg
  152.  
  153. interrupt:
  154.                  conin_we               ;get keypress
  155.                  cmp.b   #32,d0         ;spacebar
  156.                  bne     tcont          ;no
  157.                  printline abort        ;user aborted (IDIOT!)
  158.                  printline fin2
  159.                  bra     quit
  160.  
  161. receive:
  162.                  printline receiving    ;yes, tell em we're receiving
  163.                  auxin                  ;get CR, and trash it
  164.                  auxin                  ;get LF, and trash it too
  165.  
  166.                  move.l  #inputbuf,a3   ;address of buffer
  167.  
  168. stringin:
  169.                  auxin                  ;get character from serial
  170.                  cmp.b   #"*",d0        ;are we done?
  171.                  beq     stringdone     ;yes
  172.                  sub.b   #$30,d0        ;convert to decimal
  173.                  move.b  d0,(a3)+       ;save it in buffer
  174.                  bra     stringin       ;keep going
  175. stringdone:
  176.                  rts                    ;ta da!
  177.  
  178. ;******************************** HANGUP *****************************
  179. hangup:
  180.                  move.l  #400,d3        ; at least 1 second wait
  181.                  supexec tloop0         ; wait
  182.                  write   esccode,#3,#2  ; Send Escape Code
  183.                  move.l  #400,d3        ; at least 1 second pause
  184.                  supexec tloop0         ; pause
  185.                  write   mreset,#5,#2   ; "ATH"
  186.  
  187.                  rts                    ;alacazam!
  188.  
  189. ;****************************** ASC2DEC *****************************
  190. asc2dec:
  191.                  printline setting      ;print message
  192.                  move.l  #inputbuf,a3   ;address of input
  193.                  clr.l   d0             ;clear garbage
  194.                  move.b  (a3)+,d0       ;get first character
  195.                  mulu    #10000,d0      ;10000's place
  196.                  add.l   d0,julian      ;add to julian date
  197.                  clr.l   d0             ;clear junk
  198.                  move.b  (a3)+,d0       ;get second char
  199.                  mulu    #1000,d0       ;1000's place
  200.                  add.l   d0,julian      ;add to julian date
  201.                  clr.l   d0             ;you know by now...
  202.                  move.b  (a3)+,d0       ;get third
  203.                  mulu    #100,d0        ;100's place
  204.                  add.l   d0,julian      ;add to julian date
  205.                  clr.l   d0             ;and so on...
  206.                  move.b  (a3)+,d0       ;get fourth
  207.                  mulu    #10,d0         ;10's place
  208.                  add.l   d0,julian      ;add to julian date
  209.                  clr.l   d0             ; etc. etc.
  210.                  move.b  (a3)+,d0       ;finally
  211.                  add.l   d0,julian      ;1's place, add to julian
  212.  
  213.                  move.b  (a3)+,d0       ;junk next space
  214.  
  215. chkspc:
  216.                  cmp.b   #$f0,(a3)+     ;check for space? (space-$30)
  217.                  bne     chkspc         ;no
  218.  
  219.                  clr.l   d0
  220.                  move.b  (a3)+,d0       ;get first digit for hours
  221.                  mulu    #10,d0         ;10's place
  222.                  add.l   d0,hour        ;add to hour
  223.                  move.b  (a3)+,d0       ;one's place
  224.                  add.l   d0,hour        ;add to hour
  225.                  move.b  (a3)+,d0       ;first digit of minutes
  226.                  mulu    #10,d0         ;10s place
  227.                  add.l   d0,minute      ;save it
  228.                  move.b  (a3)+,d0       ;ones place
  229.                  add.l   d0,minute      ;save it
  230.                  move.b  (a3)+,d0       ;first digit of seconds
  231.                  mulu    #10,d0         ;10's place
  232.                  add.b   (a3)+,d0       ;add one's place
  233.                  lsr.l   #1,d0          ;GEMDOS like seconds/2
  234.                  move.l  d0,second      ;save it
  235.  
  236.                  clr.l   d0             ;just in case...
  237.                  clr.l   d1             ;and this too...
  238.                  move.b  timez,d0       ;get timezone data
  239.                  move.l  #timedif,a0    ;address of timezone change
  240.                  move.b  0(a0,d0.w),d1  ;get correct change in timezone
  241.                  sub.l   d1,hour        ;correct for timezone
  242.                  tst.l   hour           ;are we into previous day?
  243.                  bge     noch           ;NO
  244.                  sub.l   #1,julian      ;yes, subtract a day
  245.                  add.l   #24,hour       ;but make hour positive
  246. noch:
  247.                  rts                    ;whew!
  248.  
  249. ;******************************* JD2GREG *****************************
  250. jd2greg:
  251. ;----------------------------- CALCULATE N ----------------------------
  252.                  clr.l   d0
  253.                  clr.l   d1
  254.                  move.l  julian,l       ;save julian
  255.                  add.l   #2468570,l     ;L= Julian + 2468570
  256.                  move.w  l+2,d0         ;low word of L into d0
  257.                  move.w  l,d1           ;high word of L into d1
  258.                  mulu    #4,d0          ;product #1
  259.                  mulu    #4,d1          ;product #2
  260.                  swap    d1             ;shift to upper word
  261.                  add.l   d1,d0          ;d0 = L * 4
  262.                  move.l  #146097,d3     ;divide d0 by #146097, answer in d2
  263.  
  264.                  bsr     divide         ;go do long division
  265.  
  266.                  move.l  d2,n           ;N is final answer
  267.  
  268. ;---------------------------- CALCULATE Y ------------------------------
  269.                  move.l  #2,d1          ;high word in value $23ab1
  270.                  move.l  #$3ab1,d0      ;low word in value $23ab1
  271.  
  272.                  mulu    d2,d0          ;product #1
  273.                  mulu    d2,d1          ;product #2
  274.                  swap    d1             ;shift to upper word
  275.                  add.l   d1,d0          ;d0 = N * 146097 ($23ab1)
  276.                  add.l   #3,d0          ;add 3
  277.  
  278.                  asr.l   #2,d0          ;quickie divide by 4
  279.                  sub.l   d0,l
  280.  
  281.                  move.l  l,d0           ;L ...
  282.                  add.l   #1,d0          ; Plus 1
  283.                  mulu    #4000,d0       ; Times 4000
  284.                  move.l  #1461001,d3    ;divided by 1461001
  285.                  bsr     divide         ;long division
  286.                  move.l  d2,year        ;SAVE Y in year
  287.  
  288. ;---------------------------- CALCULATE M & DAY -----------------------
  289.                  mulu    #1461,d2       ;mutiply Y by 1461
  290.                  move.l  d2,d0          ;d0 is dividend
  291.                  
  292.                  
  293.                  asr.l   #2,d0      ;quickie divide by 4
  294.                  add.l   #31,l
  295.                  sub.l   d0,l
  296.                  
  297.                  move.l  l,d0           ;L...
  298.                  mulu    #80,d0         ;times 80
  299.                  divu    #2447,d0       ;divided by 2447
  300.                  and.l   #$ffff,d0      ;mask off remainder
  301.                  move.l  d0,month       ;equals M (save in month)
  302.  
  303.                  mulu    #2447,d0       ;times 2447
  304.                  divu    #80,d0         ;divided by 80
  305.                  and.l   #$ffff,d0      ;mask off remainder
  306.                  move.l  l,day          ; day = L - d0
  307.                  sub.l   d0,day         ;ACTUAL DAY
  308.  
  309. ;-------------------------- CALCULATE MONTH -------------------------
  310.                  move.l  month,d0       ;month in d0...
  311.                  divu    #11,d0         ;divide by 11 (why???)
  312.                  and.l   #$ffff,d0      ;no remainder
  313.                  move.l  d0,l           ;save it
  314.                  add.l   #2,month       ;2 + month
  315.                  mulu    #12,d0         ; L times 12
  316.                  sub.l   d0,month       ;difference = ACTUAL MONTH
  317.  
  318. ;------------------------- CALUCULATE YEAR ------------------------
  319.                  move.l  n,d0           ;N...
  320.                  sub.l   #49,d0         ;minus 49...
  321.                  mulu    #100,d0        ;times 100
  322.                  add.l   year,d0        ;plus Y
  323.                  add.l   l,d0           ;plus L
  324.                  move.l  d0,year        ;equals ACTUAL YEAR
  325.                  
  326.  
  327.                  rts                    ;yippie taye oh kiy aie
  328.  
  329. ;****************************** SETCLOCK ******************************
  330. setclock:
  331.                  gettime                ;get current time and date
  332.                  move.l  d0,curtime     ;save it for later use...
  333.                  move.l  year,d5        ;get year
  334.                  sub.l   #1980,d5       ;convert to system format (year-1980)
  335.                  move.l  d5,year        ;save for later use!
  336.                  asl.l   #4,d5          ;give 4 places for month
  337.                  add.l   month,d5       ;add in month
  338.                  asl.l   #5,d5          ;make room for day
  339.                  add.l   day,d5         ;add in day
  340.                  set_date d5            ;set the GEMDOS date
  341.  
  342.                  move.l  hour,d5        ;add in hour
  343.                  asl.l   #6,d5          ;make room for minute
  344.                  add.l   minute,d5      ;add in minute
  345.                  asl.l   #5,d5          ;make room for seconds
  346.                  add.l   second,d5      ;add in seconds
  347.  
  348.                  set_time d5            ;set the system time.
  349.                  rts                    ;easy, ain't it?
  350.  
  351. ;********************* PRINT CURRENT AND REAL TIME *****************
  352. curandreal:
  353.                  printline adate        ;print 'actual date'
  354.                  bsr     pdate          ;print date
  355.                  printline atime        ;print 'actual time
  356.                  bsr     ptime          ;print time
  357.  
  358.                  printline cdate        ;print 'current date'
  359.  
  360.                  move.l  curtime,d0          ;get time and date
  361.                  move.l  d0,second           ;save time
  362.                  and.l   #%11111,second      ;seconds only
  363.                  asr.l   #5,d0               ;shift for minutes
  364.                  move.l  d0,minute           ;save time
  365.                  and.l   #%111111,minute     ;min only
  366.                  asr.l   #6,d0               ;shift for hours
  367.                  move.l  d0,hour             ;hours
  368.                  and.l   #%11111,hour        ;hours only
  369.                  asr.l   #5,d0               ;shift for day
  370.                  move.l  d0,day              ;save it
  371.                  and.l   #%11111,day         ;day only
  372.                  asr.l   #5,d0               ;shift for month
  373.                  move.l  d0,month            ;save it
  374.                  and.l   #%11111,month       ;month only
  375.                  asr.l   #4,d0               ;shift for year (minus 1980)
  376.                  move.l  d0,year             ;save year
  377.  
  378.                  bsr     pdate               ;print current date
  379.                  printline ctime             ;print 'current time'
  380.                  bsr     ptime               ;print time
  381.                  rts
  382.  
  383. pdate:
  384.                  move.l  month,d3       ;print month
  385.                  bsr     pnum           ;print number... (d3)
  386.                  conout  #'/'           ;output /
  387.                  move.l  day,d3         ;print day
  388.                  bsr     pnum           ;print number
  389.                  conout  #'/'           ;output /
  390.                  move.l  year,d3        ;output year
  391.                  add.l   #80,d3         ;plus 80...
  392.                  bsr     pnum           ;print number
  393.                  rts
  394.  
  395. ptime:
  396.                  move.l  hour,d3        ;print hour
  397.                  bsr     pnum
  398.                  conout  #':'           ;output :
  399.                  move.l  minute,d3      ;print minute
  400.                  bsr     pnum
  401.                  conout  #':'           ;output :
  402.                  move.l  second,d3      ;print second
  403.                  asl     #1,d3          ;times 2
  404.                  bsr     pnum
  405.                  rts
  406.  
  407. pnum:
  408.                  divu    #10,d3         ;uw contain 1's pl, lw=10's
  409.                  add.w   #$30,d3        ;d3 contains ascii 10's place
  410.                  conout  d3             ;output 10's place
  411.                  swap    d3             ;1's place in low word
  412.                  add.w   #$30,d3        ;d3 contains ascii 1's placer
  413.                  conout  d3
  414.                  rts
  415.  
  416.  
  417.  
  418.  
  419. ;******************************** DIVIDE ***************************
  420. divide:
  421.                  clr.l   d2             ;counter for 32-bit division
  422. dloop:
  423.                  sub.l   d3,d0          ;long division
  424.                  bmi     divdone        ;did we subtract too much?  yes
  425.                  addq.l  #1,d2          ;no, count 1
  426.                  bra     dloop          ;keep subtracting
  427. divdone:
  428.                  rts
  429.  
  430. ;******************************* TLOOP0 ******************************
  431. tloop0:
  432.                  move.l  $4ba,d4        ; Get value in 200hz counter
  433.                  move.l  d4,curtime     ; save current time
  434. tloop1:
  435.                  cmp.l   $4ba,d4        ; Has the clock ticked yet?
  436.                  beq     tloop1         ; If not, loop until it does
  437.  
  438.                  dbeq    d3,tloop0      ; Decrement ticks, loop til zero
  439.                  rts
  440.  
  441.  
  442. ;****************************** THE DATA ****************************
  443.  DATA
  444. julian           dc.l    0
  445. phnum            dc.b    "6530351",$d,$a,0
  446.                  align
  447. esccode          dc.b    "+++",0
  448.                  align
  449. mreset           dc.b    "ATH",$d,$a,0
  450.                  align
  451. fname            dc.b    "CALLTIME.DAT",0
  452.                  align
  453. dialing          dc.b    $d,$a,$a,"Dialing Naval Observatory...",$d,$a,$0
  454.                  align
  455. receiving        dc.b    "Receiving time and date...",$d,$a,$0
  456.                  align
  457. setting          dc.b    "Setting system clock...",$d,$a,$a,$0
  458.                  align
  459. finished         dc.b    $d,$a,$a,"Done!  System clock now accurate to 1 second...",$a
  460. fin2             dc.b    $d,"Thank you for using Bob's CallTime  V. 3.0",$a
  461.                  dc.b    $d,"If you find this program useful, please consider",$a
  462.                  dc.b    $d,"making a donation of $10 U.S. currency.",$d,$a,$a,$a
  463.                  dc.b    "Bob Areddy",$d,$a,"2607 Clawson Avenue",$d,$a
  464.                  dc.b    "Royal Oak, Mi  48073",$d,$a,$a,$a
  465.                  dc.b    $0
  466.                  align
  467. filenotfound:    dc.b    "Data file not found...",$d,$a,$a,$a
  468.                  dc.b    "                          Please Select Time Zone",$d,$a,$a
  469.                  dc.b    "                Standard Time              Daylight Savings Time",$d,$a
  470.                  dc.b    "                 1)  Pacific                   2)  Pacific",$d,$a
  471.                  dc.b    "                 3)  Mountain                  4)  Mountain",$d,$a
  472.                  dc.b    "                 5)  Central                   6)  Central",$d,$a
  473.                  dc.b    "                 7)  Eastern                   8)  Eastern",$d,$a
  474.                  dc.b    "                                   9)  Zulu",$d,$a,$a,$a,$0
  475.                  align
  476. initst           dc.b    $d,$a,$a,"Please enter initialization string... (press control-z when done)",$d,$a,$a
  477.                  align
  478. timedif          dc.b    8,7,7,6,6,5,5,4,0
  479. timeout          dc.b    "Timeout error... unable to receive data.",$d,$a,$a
  480.                  dc.b    "Press any key to exit...",$1b,$66,$0
  481. intro            dc.b    $1b,$45,$1b,"e                        Bob's Calltime   v. 3.1",$d,$a
  482.                  dc.b    "                       Copyright (c)  1990,1994",$d,$a,$a,$0
  483. abort            dc.b    $d,"User interrupted program....",$d,$a,$a,$0
  484. cdate            dc.b    $d,$a,"            GEMDOS date  ",0
  485. ctime            dc.b    "                  GEMDOS time  ",0
  486. adate            dc.b    $d,$a," Naval Observatory date  ",0
  487. atime            dc.b    "       Naval Observatory time  ",0
  488.  
  489.  
  490.  BSS
  491.  ALIGN
  492. handle           ds.w    1
  493. initstring       ds.b    128,0
  494. l                ds.l    1
  495. n                ds.l    1
  496. year             ds.l    1
  497. month            ds.l    1
  498. day              ds.l    1
  499. hour             ds.l    1
  500. minute           ds.l    1
  501. second           ds.l    1
  502. dummy1           ds.l    1
  503. dummy2           ds.l    1
  504. length           ds.l    1
  505. inputbuf         ds.b    64,0
  506. timez            ds.b    1
  507.                  align
  508. starttime        ds.l    1
  509. curtime          ds.l    1
  510.                  end
  511.