          TTL DATEADD
          OPT PAG
          PAG
* COPYRIGHT (C) 1986 BY PETER A. STARK

* 146818 CLOCK PATCH FOR SK*DOS / 68K FOR PT-68K

* THIS PROGRAM READS THE DATE FROM THE PT-68K BOARD'S CLOCK
* CHIP ON BOOTUP OF SK*DOS, SO USER DOES NOT HAVE TO
* TYPE IT IN.

* PROGRAM DOES NOTHING IF THE CLOCK IS NOT SET PROPERLY

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

* EQUATES

          LIB SKEQUATE

GETDAT    EQU $100C                SK*DOS'S GET DATE VECTOR
CLOCKA    EQU $E0041               CLOCK CHIP'S ADDRESS REG
CLOCKD    EQU $E0043               CLOCK CHIP'S DATA REG

* 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.
* THIS VERSION WILL BE PLACED INTO AN UNUSED LOCATION STARTING AT
* $1F00 (WHICH IS ACTUALLY INSIDE AN FCB USED INTERNALLY BY
* SK*DOS.) 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 MAKE SURE THE ORG POINTS TO AN UNUSED
* AREA. IF IN DOUBT, PLACE IT ABOVE THE TOP ADDRESS OF SK*DOS.
* SINCE THIS ROUTINE IS ONLY NEEDED ONCE, AT STARTUP, THERE IS
* NO NEED TO POINT OFFSET OR OFFINI ABOVE IT, AS IT IS OK IF IT
* IS OVERWRITTEN ONCE SK*DOS IS UP AND RUNNING.

          ORG $1F00

* GET THE TIME AND DATE AND CHECK THEM

INPDAT    MOVE.B #2,D0
          BSR.L READIT             GET MINUTES
          CMP.B #$59,D0            CHECK IT
          BHI.S NOGOOD             CLOCK NOT SET IF >59 MIN
          MOVE.B #4,D0
          BSR.L READIT             GET HOURS
          AND.B #$7F,D0            REMOVE AM/PM FLAG
          CMP.B #$12,D0            CHECK IT
          BHI.S NOGOOD             NG IF PAST 12 O'CLOCK
          MOVE.B #6,D0
          BSR.L READIT             GET DAY OF WEEK
          CMP.B #7,D0              CHECK IT
          BHI.S NOGOOD             NO 8-DAY WEEKS ALLOWED
          MOVE.B #7,D0
          BSR.L READIT             GET DAY OF MONTH
          CMP.B #$31,D0            CHECK IT
          BHI.S NOGOOD             MAX 31 DAYS A MONTH
          BSR.L BCDHEX             CONVERT BCD TO HEX
          DC VPOINT
          MOVE.B D0,CDAY(A6)       GIVE IT TO SK*DOS
          MOVE.B #8,D0
          BSR.L READIT             GET MONTH
          CMP.B #$12,D0            CHECK IT
          BHI.S NOGOOD             MAX 12 MONTHS A YEAR
          BSR.L BCDHEX             CONVERT BCD TO HEX
          MOVE.B D0,CMONTH(A6)     GIVE IT DO SK*DOS
          MOVE.B #9,D0
          BSR.L READIT             GET THE YEAR
          CMP.B #$84,D0            CHECK IT
          BCS.S NOGOOD             MUST BE 84 OR LATER
          CMP.B #$99,D0
          BHI.S NOGOOD             ASSUME THIS CENTURY
          BSR.L BCDHEX             CONVERT BCD TO HEX
          MOVE.B D0,CYEAR(A6)      GIVE IT TO SK*DOS
          RTS                      AND THEN CONTINUE SK*DOS

* ON INVALID DATE, GET IT FROM KEYBOARD

NOGOOD    LEA DATERQ(PC),A4
          DC PSTRNG                ASK FOR DATE
          DC INLINE                INPUT DATE, SEPARATED BY COMMAS
          LEA CMONTH(A6),A0        POINT TO MONTH LOCATION
          BSR.S GTDATA GET MONTH
          BSR.S GTDATA GET DAY
          BSR.S GTDATA GET YEAR
          LEA THANKS(PC),A4
          DC PSTRNG                PRINT "THANK YOU"
          DC PCRLF
          RTS
* GTDATA ROUTINE TO INPUT AND STORE DATE
GTDATA    DC DECIN                 GET NEXT DATE ITEM
          BCC.S DATEOK             OK NUMBER RECEIVED
DATENG    ADD.L #4,A7              REMOVE RETURN ADDRESS FROM STACK
          BRA.S NOGOOD             AND ASK FOR DATE AGAIN
DATEOK    CMP.B #99,D5             SHOULD BE LESS THAN 99
          BHI.S DATENG             ELSE IT'S NO GOOD
          MOVE.B D5,(A0)+          SAVE IT
          RTS
DATERQ    DC.B $D,$A
          DC.B "CALENDAR/CLOCK NOT PROPERLY SET -"
          DC.B $D,$A
          DC.B "PLEASE ENTER TODAY'S DATE (MM,DD,YY): "
          DC.B 4
THANKS    DC.B 'THANK YOU.',4

* GET DATA FROM 146818 CLOCK CHIP

READIT    MOVE.B D0,CLOCKA         GIVE ADDRESS TO CLOCK
          MOVE.B CLOCKD,D0         GET THE DATAGAIN
          RTS

* CONVERT BCD TO HEX ROUTINE

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

* PATCH SK*DOS TO COME HERE. SK*DOS CURRENTLY HAS A JMP
* INSTRUCTION AT $100C (GETDAT), WHICH POINTS TO THE ROUTINE WHICH ASKS
* FOR THE DATE FROM THE KEYBOARD. THE FOLLOWING JMP.L OVERWRITES
* THAT INSTRUCTION AND STEERS SK*DOS TO THIS PROGRAM INSTEAD.

          ORG GETDAT
          JMP.L INPDAT             GO TO THIS PATCH TO GET DATE

* 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
