* COPYRIGHT (C) 1986 BY PETER A. STARK
* STAR-K SOFTWARE SYSTEMS CORP.

* TIME PATCH FOR SK*DOS / 68K FOR THE PT-68K

* THIS PROGRAM IS CALLED EVERY TIME SK*DOS OPENS A FILE
* FOR WRITING, READS THE TIME OF DAY FROM THE 146818 CLOCK
* ON THE PT-68K BOARD, CONVERTS THE TIME INTO A
* ONE-BYTE CODE, AND PUTS IT INTO D5 SO THAT
* SK*DOS CAN PUT IT INTO THE DIRECTORY, NEXT TO THE DATE.
* CAUTION - ALL REGISTERS MUST BE PRESERVED EXCEPT A5-A6,D5-D7.

* READ THE FOLLOWING CAREFULLY BEFORE IMPLEMENTING. THEN
* APPEND THIS CODE TO SK*DOS.SYS

* THIS PROGRAM WILL BE LOADED AT THE EXACT ADDRESS SPECIFIED
* IN THE FOLLOWING ORG STATEMENT (I.E., THE OFFSET WILL NOT
* BE ADDED). HENCE WE HAVE TO BE CAREFUL AS TO WHERE WE PLACE IT.
* IN GENERAL, BEFORE APPENDING THIS TO ANY VERSION OF
* SK*DOS, YOU SHOULD DO A 'LOCATE' ON SK*DOS TO DETERMINE THE
* LOAD ADDRESSES, AND THEN PLACE IT ABOVE THE TOP ADDRESS OF SK*DOS.
* FINALLY, POINT OFFINI ABOVE IT SO IT WILL BE PROTECTED
* FROM USER PROGRAMS ONCE LOADED.

          ORG $5000

* EQUATES

OFFINI    EQU $1018                OFFSET INITIALIZATION ADDRESS
INTIME    EQU $1012                SK*DOS'S GET TIME VECTOR
CLOCKA    EQU $E0041               CLOCK CHIP'S ADDRESS REG
CLOCKD    EQU $E0043               CLOCK CHIP'S DATA REG

* START OF PROGRAM

* CHECK FOR VALID TIME ON CLOCK

* CHECK THAT YEAR IS 85 THRU 99
SETIME    MOVE.B #09,D5            YEAR ADDRESS
          BSR.S GETDAT             GET DATA FROM CHIP
          CMP.B #$85,D5
          BCS.S TIMENG             QUIT IF TIME IS NG
          CMP.B #$99,D5
          BHI.S TIMENG             DITTO

* GET THE TIME
GETTIM    MOVE.B #$0A,D5           CHECK IF OK TO READ
          BSR.S GETDAT             GET UPD IN PROG BIT
          BMI.S GETTIM             WAIT UNTIL IT IS ZERO

          MOVE.B #02,D5
          BSR.S GETDAT             GET MINUTE
          MOVE.L D5,A5             SAVE MINUTES IN A5
          MOVE.B #04,D5
          BSR.S GETDAT             GET HOURS
          CMP.B #$12,D5            12 HOURS AM?
          BNE.S NOT12              NO
          MOVE.L #0,D5             YES, FORCE TO 0 HOURS
NOT12     EXG.L D5,A5              STORE HOURS, GET MINUTES
          BSR.S BCDHEX             CONVERT MINUTES TO HEX
          DIVU #6,D5               DIVIDE MINUTES BY 6
          MOVE.B D5,D7             SAVE MINUTES/6 IN D7
          MOVE.L A5,D5             GET HOURS AGAIN
          AND.B #$7F,D5            REMOVE AM/PM BIT
          BSR.S BCDHEX             CONVERT HOURS TO HEX
          MULU #10,D5              HOURS * 10
          ADD.B D7,D5              HOURS*10 + MINUTES/6 IN D5
          MOVE.L A5,D7             GET HOURS BACK
          TST.B D7                 CHECK IF AM OR PM
          BMI.S ISPM               IF PM
          BNE.S QUIT               EXIT IF NOT 24:00 HOURS
          MOVE.B #120,D5           ELSE MAKE 12 (PLUS 12 LATER = 24)
ISPM      CMP.B #$92,D7            CHECK FOR 12:XX PM
          BEQ.S QUIT               YES, LEAVE AT 12
          ADD.B #120,D5            ELSE CVT TO 24 HR TIME
QUIT      RTS                      RETURN

* IF TIME IS NG, RETURN 0
TIMENG    MOVE.L #0,D5
          BRA.S QUIT

* GIVE ADDRESS TO CLOCK AND GET DATA
GETDAT    MOVE.B D5,CLOCKA         GIVE IT ADDRESS
          MOVE.B CLOCKD,D5         GET DATA
          RTS                      AND RETURN

* CONVERT BCD TO HEX ROUTINE

BCDHEX    CLR.W D6
          MOVE.B D5,D6             SAVE BCD NUMBER
          LSR.B #4,D6              MOVE MSD INTO LSD OF D6
          MULU #10,D6              MULT TENS BY 10
          AND.L #$0F,D5            KEEP ONLY UNITS
          ADD.B D6,D5              ADD TENS AND UNITS
          RTS                      AND RETURN

* NEXT, WE MUST MODIFY OFFINI SO AS TO PROTECT THIS ROUTINE FROM
* USER PROGRAMS.

          EVEN
TIMEND    EQU *                    THIS IS EVEN!

          ORG OFFINI
          DC.L TIMEND              EVEN ADDRESS PAST END

* PATCH SK*DOS TO COME HERE. SK*DOS CURRENTLY HAS THREE RTS
* INSTRUCTIONS AT $1012 (INTIME), WHICH ARE CALLED EACH TIME
* SK*DOS OPENS A FILE FOR WRITING. THE FOLLOWING JMP.L OVERWRITES
* THESE RTS'S AND STEERS SK*DOS TO THIS PROGRAM INSTEAD.

          ORG INTIME
          JMP.L SETIME             GO TO THIS PATCH TO GET TIME

* FINALLY, WE ADD A TRANSFER ADDRESS WHICH JUMPS TO THE BEGINNING
* OF SK*DOS. THIS IS THE ACTUAL TRANSFER ADDRESS WHICH STARTS
* SK*DOS FROM ITS COLD-START ENTRY POINT.

          END $1000
