;
; B5C-MORE.INS		Wayne Masters
; 02/09/86		Potpourri, 408-378-7474
;
; BYE5 insert for MORE-CLOCK running on CP/M-2 or CP/M-3 systems
;
; To install this insert you must set the following equates in BYE5:
;	CLOCK	EQU	YES
;	TIMEON	EQU	YES
;	BCDBIN	EQU	YES
;	BINBCD	EQU	NO
;
; You must set the PIAPORT equate below to match your system
;
; You should use your clock initialization utility to set the date/time
; prior to executing BYE.  Make sure you initialize the clock as a 24
; hour clock if you want it to work correctly with BYE5.
;
; Locate the area near the beginning of BYE5 +++ Install your clock insert+++
; and remove the existing code inside the equates.  Replace it with this
; insert.
;
;************************************************************************
;*		MM58321 port library & 8255 PIA ports			*
;*				  for					*
;*	      MORE-CLOCK  ver 2.1     with version 2 PC board		*
;************************************************************************
;
; Port addresses and register assignments in the clock system.
;	   clock chip used : MM58321 OKI
;	   PIA chip used   : D8255AC-5 NEC
;
; This is the port addresses and equates for the MORE-CLOCK using version 2
; PC board ( with wire jumper to select the page lines ).
; The primary difference in the software are the 4 control lines -
;  stop, read, write, and address write. If the page number is changed,
;  the only thing that will have to be altered in the software is the
;  PIAPORT equate, which can have values of 00h, 10h, 20h, and 30h.
;
;
CENTURY	EQU	19H		; Change this in year 2000 to 20
PIAPORT	EQU	00H		; Hardware port for the PIA
				; ( jumpered on pc board )
CDAPORT	EQU	PIAPORT+2	; Location of clock data port ( in PIA )
CTLPORT	EQU	PIAPORT+3	; Location of PIA control port

				; PIA configuration words:
WMODE	EQU	10000000B	; PC 0-3 being outputs ( clock writes )
RMODE	EQU	10000001B	; PC 0-3 being inputs ( clock reads )
				; ( PC 4-7 are always outputs )
;
				; PC 4-7 are the control bits for the clock
CSBIT	EQU	10000000B	; Clock chip stop bit
CABIT	EQU	01000000B	; Clock chip register address bit
CRBIT	EQU	00100000B	; Clock chip read bit
CWBIT	EQU	00010000B	; Clock chip write bit
;
;----------------------------------
;  CLOCK CHIP DATA REGISTERS
;----------------------------------
;
USEC	EQU	00H		; Units of seconds
TSEC	EQU	01H		; Tens of seconds
;
UMIN	EQU	02H		; Units of minutes
TMIN	EQU	03H		; Tens of minutes
;
UHOU	EQU	04H		; Units of hours
THOU	EQU	05H		; Tens of hours
MO12	EQU	00000000B	; 12 hour mode ( D3 clear )
MO24	EQU	00001000B	; 24 hour mode ( D3 set )
AMTM	EQU	00000000B	; AM time ( D2 clear )
PMTM	EQU	00000100B	; PM time ( D2 set )
;
DOWK	EQU	06H		; Day of the week
UDAY	EQU	07H		; Units of days
TDAY	EQU	08H		; Tens of days
LPYR	EQU	00000000B	; D2 and D3 select the leap year operation
;
UMON	EQU	09H		; Units of months
TMON	EQU	0AH		; Tens of months
;
UYEA	EQU	0BH		; Units of years
TYEA	EQU	0CH		; Tens of years
;
; Machine code interface to get the time and date information from the
;  MORE-CLOCK and store it for BYE5.
;
TIME:	DI			; Disable the interrupts while we do this
	MVI	A,CENTURY
	STA	RTCBUF+3	; Store century
	MVI	H,TMON		; Load the tens month register number
	CALL	CLKRD		; Get the clock data
	STA	BCDMSB		; Save MSB
	MVI	H,UMON		; Load the units month register number
	CALL	CLKRD		; Get the clock data
	STA	BCDLSB		; Save LSB
	CALL	PACKBCD		; Make it packed bcd
	STA	RTCBUF+5	; Store month
;
	MVI	H,TDAY		; Load in the tens day register number
	CALL	CLKRD		; Get the clock data
	STA	BCDMSB
	MVI	H,UDAY		; Load in the units day register number
	CALL	CLKRD		; Get the clock data
	STA	BCDLSB
	CALL	PACKBCD
	STA	RTCBUF+6	; Store day
;
	MVI	H,TYEA		; Load in the tens year number
	CALL	CLKRD		; Get the clock data
	STA	BCDMSB
	MVI	H,UYEA		; Load in the units of years number
	CALL	CLKRD		; Get the clock data
	STA	BCDLSB
	CALL	PACKBCD
	STA	RTCBUF+4	; Store year
;
	MVI	H,THOU		; Load in the tens hour
	CALL	CLKRD		; Get the clock data
	ANI	00000011B	; Strip off the 24 mode and AM PM bits
	STA	BCDMSB
	MVI	H,UHOU		; Load in the units of hours reg. number
	CALL	CLKRD		; Get the clock data
	STA	BCDLSB
	CALL	PACKBCD
	STA	RTCBUF		; Store hour
	CALL	BCDBIN
	STA	CCHOUR		; For BYE
;
	MVI	H,TMIN		; Load in the tens of minutes reg.
	CALL	CLKRD		; Get the clock data
	STA	BCDMSB
	MVI	H,UMIN		; Load in the units of minutes
	CALL	CLKRD		; Get the clock data
	STA	BCDLSB
	CALL	PACKBCD
	STA	RTCBUF+1	; Store minutes
	CALL	BCDBIN		; Convert to binary
	STA	CCMIN		; For BYE
;
	MVI	H,TSEC		; Load in the tens of seconds reg.
	CALL	CLKRD		; Get the clock data
	STA	BCDMSB
	MVI	H,USEC		; Load in the units of seconds number
	CALL	CLKRD		; Get the clock data
	STA	BCDLSB
	CALL	PACKBCD
	STA	RTCBUF+2	; Store seconds
;
	EI			; Enable the interrupts again
	RET
;
;*********************************************************************
;
; Modified to automatically handle the 24 hour mode flag in the tens
;   of hours register.
;
;	CLKRD - reads a referenced clock register
;
; Entrance parameters :
;	register address in H, data will be returned in A
;
;----------------------------------------------------------------------
;
; The trick to getting to the clock chip is the D8255 PIA which will be
; necessary to program for a read and a write operation. The PIA was
; used for two reasons, the speed of most clock chips are not sufficient
; for use directly attached to a computer's buss and the polarity of the
; control lines were wrong. The C port of the PIA is attached to the
; clock chip, 4 lines as the data buss and 4 lines as control. The PIA's
; C port can be configured as two 4 bit ports which is the situation
; here. Only the two modes are used to 'talk' to the clock, RMODE and
; WMODE.
;
;
; The port numbers that must be passed are:
;		0	units of seconds
;		1	tens of seconds
;		2	units of minutes
;		3	tens of minutes
;		4	units of hours
;		5	tens of hours
;		6	day of week
;		7	units of days
;		8	tens of days
;		9	units of months
;		10	tens of months
;		11	units of years
;		12	tens of years
;
; Special bits used :
;		5	tens of hours	D3 used to indicate 24 hour mode
;					D2 indicates PM if 12 hour mode
;		8	tens of days	D3 & D2 used to indicate leap year
;
;--------------------------------------------------------------------
;
CLKRD:				; Read a clock register
	MVI	A,WMODE		; Control word for PIA write
	OUT	CTLPORT		; Set the control lines to the clock
;
	MVI	A,CABIT		; Get the clock register address bit
	ORA	H		; Add in the register address
	ORI	CSBIT		; Stop line of the clock must be high
	OUT	CDAPORT		; Output the data
;
	MVI	A,RMODE		; PIA control word for chip read
	OUT	CTLPORT		; Output the data
;
	MVI	A,CRBIT		; Set the read bit
	ORI	CSBIT		; Set the stop bit
	OUT	CDAPORT		; Init. the clock chip for a read
	NOP			; Delay for the clock chip to stabilize
	NOP
	NOP
	NOP
	NOP
	IN	CDAPORT		; The clock data will now be on the low nib
	MOV	B,A		; Temp. store data
	XRA	A		; Reset all of the control lines to
	OUT	CDAPORT		; The clock chip
	MVI	A,THOU		; Get the real tens register address
	CMP	H		; See if this is it
	MOV	A,B		; Get the data back
	JNZ	CX1		; If not tens reg. then jump around
	ANI	00000011B	; Make sure only the low two bits are sent
CX1:	ANI	0FH		; Mask of the high nibble
	RET
;
;----------------------------------------------------------------------------
;
; BCDMSB= Most significent byte, BCDLSB= Least signficent byte
;
PACKBCD:LDA	BCDLSB
	ANI	0FH
	MOV	B,A		; Save lsb in B
	LDA	BCDMSB
	ANI	0FH
	RLC!RLC!RLC!RLC		; Shift it to high nibble
	ORA	B		; Combine with lsb
	RET
;
BCDMSB:	DB	0
BCDLSB:	DB	0
;
;  That's the end of B5C-MORE.INS
;
