;*****************************************************************
;**                                                             **
;**         PATCH-IT.ASM  -  VERS. 1.0  -  03/22/84             **
;**         =======================================             **
;**                                                             **
;*****************************************************************
;**                                                             **
;**         PATCH-IT.ASM  is  intended  for use  by  Morrow     **
;**    Micro-Decision Owners with WordStar* Version 3.0 and     **
;**    Correct-It  Version 1.0.   In its  distributed  form     **
;**    this  program  will NOT work with other versions  of     **
;**    these programs.                                          **
;**         This  program "patches" the program  files  for     **
;**    WordStar   &  Correct-It  so  that  the   Correct-It     **
;**    spelling checker may be run from the No-File Menu in     **
;**    essentially the same manner as SpellStar*  would  if     **
;**    it were available on the WordStar diskette.              **
;**         For  more  information  (including  operational     **
;**    data)  please  read the  accompanying  documentation     **
;**    file.                                                    **
;**                                                             **
;**    *(Registered Trademark of MicroPro International)        **
;**                                                             **
;*****************************************************************
;*****************************************************************
;**                                                             **
;**                                                             **
;**                          WARNING!                           **
;**                      THIS PROGRAM IS                        **
;**    						                **
;**         * * *   D E V E L O P M E N T A L   * * *           **
;**                                                             **
;**                            AND                              **
;**                                                             **
;**          * * *   E X P E R I M E N T A L   * * *            **
;**                                                             **
;**         The  Author  places this program in the  public     **
;**    domain  with the stipulation and understanding  that     **
;**    the user accepts all responsibility for its use  and     **
;**    any  consequences thereof.   DO NOT use this program     **
;**    to  make  permanent  changes to the  files  on  your     **
;**    distribution diskettes!                                  **
;**         NOTE:   These  patches will NOT be supported by     **
;**    either MicroPro International (WordStar) or by Aspen     **
;**    Software (Correct-It).   They have been tested under     **
;**    a  variety  of  conditions  and  no  problems   were     **
;**    apparent at the time of distribution.  However, this     **
;**    program    is   distributed   with   absolutely   no     **
;**    guarantees, implied or otherwise.                        **
;**         This source listing,  the assembled .COM  file,     **
;**    and    the   accompanying   documentation   may   be     **
;**    duplicated  or reproduced by whatever means for non-     **
;**    commercial purposes only.                                **
;**         For additional information please read the .DOC     **
;**    file or write to the address provided below.             **
;**                                                             **
;**           USE THIS PROGRAM AT YOUR OWN RISK!!!!             **
;**                                                             **
;*****************************************************************
;
;=================================================================
;**                                                             **
;**                    * * *   NOTE   * * *                     **
;**                                                             **
;**    I have used a version of Wordstar  patched with this     **
;**    prograí foò almosô á  yeaò no÷ oî mù MD-2®   Despitå     **
;**    Robert  Throckmorton's   copious  warnings,  I  have     **
;**    experienced  absolutely  no  problems,  glitches  or     **
;**    bugs  whatsoever.   I highly recommend  this program     **
;**    to all Wordstar/Correct-It users.                        **
;**                   --Marc Norton, 02/20/85                   **
;**                                                             **
;=================================================================
;
;*****************************************************************
;**                                                             **
;**          PATCH-IT.ASM  -  Vers. 1.0  -  03/22/84            **
;**          =======================================            **
;**                                                             **
;**          Written by:  Robert Throckmorton                   **
;**                       7945 Kingswood Drive #240             **
;**                       Citrus Heights, CA 95610              **
;**                       (916) 967-6117                        **
;**                                                             **
;**         Please  address  all  questions,   suggestions,     **
;**    revisions  and  other  such nonsense  to  the  above     **
;**    address.   If  you  write,  please provide  a  Self-     **
;**    Addressed Stamped Envelope for your reply.               **
;**         If  you  are  unable to reach me at  the  above     **
;**    address,  please  write  to the  CP/Morrow  Computer     **
;**    Group in Sacramento, CA.                                 **
;**         Your comments will be genuinely appreciated.        **
;**                                                             **
;*****************************************************************

;*****************************************************************
;**                                                             **
;**                    ASCII CHARACTERS                         **
;**                                                             **
;*****************************************************************

	CR 	EQU	0DH		;CARRIAGE RETURN
	LF	EQU	0AH		;LINE FEED
	CTRLC	EQU	03H		;CONTROL-C (PROGRAM EXIT)
	CTRLZ	EQU	1AH		;END OF FILE INDICATOR
	BELL	EQU	07H		;BELL

;*****************************************************************
;**                                                             **
;**             EQUATES FOR CP/M 2.2                            **
;**                                                             **
;*****************************************************************
;**             CP/M BDOS EQUATES                               **
;*****************************************************************

	RCONF	EQU	1		;CONSOLE INPUT FUNCTION
	WCONF	EQU	2		;CONSOLE OUTPUT FUNCTION
	RESET	EQU	13		;RESET DISK SYSTEM FUNCTION
	OPENF	EQU	15		;OPEN FILE FUNCTION
	CLOSF	EQU	16		;CLOSE FILE FUNCTION
	FINDF	EQU	17		;FIND FILE FUNCTION
	DELETE	EQU	19		;DELETE FILE FUNCTION
	READF	EQU	20		;READ ONE RECORD FUNCTION
	WRITF	EQU	21		;WRITE ONE RECORD FUNCTION
	MAKEF	EQU	22		;CREATE FILE FUNCTION
	RENAME	EQU	23		;RENAME FILE FUNCTION
	SDMAF	EQU	26		;SET DMA FUNCTION
	DFDSKF	EQU	25		;RETURN CURRENT DISK	

;*****************************************************************
;**             CP/M ADDRESSES                                  **
;*****************************************************************

	RBOOT	EQU	0		;RE-BOOT CP/M SYSTEM
	DRIVE	EQU	4		;CURRENT DRIVE SELECTION
	BDOS	EQU	5		;SYSTEM CALL ENTRY;
	MEMAX	EQU	7		;MSB TOP OF MEMORY
	TFCB	EQU	5CH		;TRANSIENT FILE CONTROL BLOCK
	FCBTY	EQU	TFCB+9		;FILE TYPE IN FCB
	FCBEX	EQU	TFCB+12		;FILE EXTENT IN FCB
	FCBS2	EQU	TFCB+14		;SYSTEM USE IN FCB
	FCBRC	EQU	TFCB+15		;RECORD COUNT IN FCB
	FCBCR	EQU	TFCB+32		;CURRENT RECORD IN FCB
	TBUFF	EQU	80H		;TRANSIENT BUFFER
	TPA	EQU	100H		;TRANSIENT PROGRAM AREA	

;*****************************************************************
;**             CP/M FLAGS                                      **
;*****************************************************************

	BDAOK	EQU	0		;BDOS RETURN FOR ALL OK
	BDER1	EQU	1		;BDOS RETURN ONE
	BDER2	EQU	2		;BDOS RETURN TWO
	BDERR	EQU	255		;BDOS RETURN ERROR FLAG

;*****************************************************************
;**                                                             **
;**             MISCELLANEOUS EQUATES                           **
;**                                                             **
;*****************************************************************

	YES	EQU	0FFH
	NO	EQU	00H


;*****************************************************************
;**                                                             **
;**              MAIN PROGRAM AREA                              **
;**                                                             **
;*****************************************************************

	ORG	TPA		;ASSEMBLE PROGRAM FOR TPA

START:	LXI	H,0		;SAVE INITIAL STACK POINTER
	DAD	SP
	SHLD	SPSAVE		
 	LXI	SP,STAK		;SET UP USER'S STACK
	LDA	DRIVE		;AND SAVE INITAL DRIVE SELECTED
	STA	DRSAV		
	MVI	C,RESET		;RESET DISK SYSTEM
	CALL	BDOS
START1:	LXI	H,SINON		;DISPLAY SIGN-ON MESSAGE
	CALL	COMSG
	CALL	CHOOSE		;CHOOSE MD2 OR MD3
	CALL	PATCH		;IMPLEMENT PATCHES
DONE:	LDA	DRSAV		;RESTORE INITIAL DRIVE
	STA	DRIVE	
	JMP	RBOOT		;EXIT TO CP/M

;*****************************************************************
;**                                                             **
;**     CHOOSE MICRO-DECISION MD2 OR MD3 COMPUTER               **
;**                                                             **
;**  	SELECTION AFFECTS CORRECT.COM AND FIXUP.COM PATCHES     **
;**                                                             **
;*****************************************************************

CHOOSE:	CALL	SPMSG
	DB	'Please Choose One of the Following:',CR,LF,LF
	DB	'     1.  Morrow MD2 - (Single-Sided Disk Drives)',CR,LF
	DB	'     2.  Morrow MD3 - (Double-Sided Disk Drives)',CR,LF,CR,LF
CH1:	DB	'Enter "1" or "2":  ',0
CH2:	CALL	CI
	CPI	'1'
	JZ	CH3		;SHOW SELECTION
	CPI	'2'
	JZ	CH4		;SHOW SELECTION
	CPI	CTRLC		;EXIT IF CONTROL-C
	JZ	DONE
	LXI	H,(CH1-2)	;BAD ENTRY, TRY AGAIN
	CALL	COMSG		;CR,LF & MESSAGE
	JMP 	CH2		;GET NEW ENTRY
CH3:	STA	MD2FLAG
	CALL	SPMSG
	DB	CR,LF,LF,'** Morrow MD2 Selected',CR,LF,0
	RET
CH4:	STA	MD2FLAG
	CALL	SPMSG
	DB	CR,LF,LF,'** Morrow MD3 Selected',CR,LF,0
	RET

;*****************************************************************
;**                                                             **
;**                 PROGRAM PATCH AREA                          **
;**                                                             **
;*****************************************************************

PATCH:	LXI	H,WSFCB1	;PREPARE FCB FOR WS.COM
	CALL	SPMSG
	DB	'-- Reading & Revising WS.COM',CR,LF,0
	CALL	GET
	LDA	WS1
	LXI	H,OFFSET+046CH
	MOV	M,A
	LXI	H,WSFCB2
	CALL	PUT
	LXI	D,WSFCB1	;DELETE OLD WS.COM FILE
	CALL	DELF
	LXI	D,WSFCB3	;RENAME WS.$$$ TO WS.COM
	CALL	RENF
PATCH2:	LXI	H,OVRFCB1	;PREPARE FCB FOR WSOVLY1.OVR
	CALL	SPMSG
	DB	'-- Reading & Revising WSOVLY1.OVR',CR,LF,0
	CALL	GET
	MVI	C,11
	LXI	H,WSOVR1
	LXI	D,OFFSET+2814H
	CALL	MOVE
	MVI	C,11
	LXI	H,WSOVR2
	LXI	D,OFFSET+28E4H
	CALL	MOVE
	LXI	H,OVRFCB2
	CALL	PUT
	LXI	D,OVRFCB1
	CALL	DELF
	LXI	D,OVRFCB3
	CALL	RENF
PATCH3:	LXI	H,MSGFCB1
	CALL	SPMSG
	DB	'-- Reading & Revising WSMSGS.OVR',CR,LF,0
	CALL	GET
	MVI	C,17
	LXI	H,WSMSG1
	LXI	D,OFFSET+0496H
	CALL	MOVE
	MVI	C,17
	LXI	H,WSMSG1
	LXI	D,OFFSET+06E8H
	CALL	MOVE
	LDA	WSMSG2
	LXI	H,OFFSET+38E0H
	MOV	M,A
	LXI	H,MSGFCB2
	CALL	PUT
	LXI	D,MSGFCB1
	CALL	DELF
	LXI	D,MSGFCB3
	CALL	RENF
PATCH4:	LXI	H,CRTFCB1
	CALL	SPMSG
	DB	'-- Reading & Revising CORRECT.COM',CR,LF,0
	CALL	GET
	MVI	C,3
	LXI	H,CRCT1
	LXI	D,OFFSET+0100H
	CALL	MOVE
	MVI	C,(CRCT$-CRCT2)
	LXI	H,CRCT2
	LXI	D,OFFSET+01C0H
	CALL	MOVE
	MVI	C,11
	LXI	H,CRCT3
	LXI	D,OFFSET+2102H
	CALL	MOVE
	LDA	MD2FLAG
	CPI	'2'
	JZ	PTC4
	MVI	C,13
	LXI	H,CRCT4
	LXI	D,OFFSET+24D8H
	CALL	MOVE
	MVI	C,11
	LXI	H,CRCT5
	LXI	D,OFFSET+354BH
	CALL	MOVE
	LDA	CRCT6
	LXI	H,OFFSET+436BH
	MOV	M,A
PTC4:	LXI	H,CRTFCB2
	CALL	PUT
	LXI	D,CRTFCB1
	CALL	DELF
	LXI	D,CRTFCB3
	CALL	RENF
PATCH5:	LXI	H,FIXFCB1
	CALL	SPMSG
	DB	'-- Reading & Revising FIXUP.COM',CR,LF,0
	CALL	GET
	MVI	C,3
	LXI	H,FIXUP1
	LXI	D,OFFSET+0100H
	CALL	MOVE
	MVI	C,(FIXUP$-FIXUP2)
	LXI	H,FIXUP2
	LXI	D,OFFSET+01C0H
	CALL	MOVE
	MVI	C,31
	LXI	H,FIXUP3
	LXI	D,OFFSET+03B2H
	CALL	MOVE
	LDA	MD2FLAG
	CPI	'2'
	JZ	PTC5
	MVI	C,10
	LXI	H,FIXUP4
	LXI	D,OFFSET+5487H
	CALL	MOVE
	MVI	C,12
	LXI	H,FIXUP5
	LXI	D,OFFSET+554FH
	CALL	MOVE
PTC5:	LXI	H,FIXFCB2
	CALL	PUT
	LXI	D,FIXFCB1
	CALL	DELF
	LXI	D,FIXFCB3
	CALL	RENF
PATCHX:	CALL	SPMSG	
	DB	'-- Patching Completed',CR,LF,LF,LF,LF,LF,0
	RET

;*****************************************************************
;**                                                             **
;**                 PATCH DATA FOR WS.COM                       **
;**                                                             **
;*****************************************************************

WS1:	DB	03H	;MOVE INTO WS.COM AT 046CH

;*****************************************************************
;**                                                             **
;**            PATCH DATA FOR WSOVLY1.OVR                       **
;**                                                             **
;*****************************************************************

WSOVR1:	DB	'CORRECT COM'	;MOVE INTO WSOVLY1.OVR AT 2814H
WSOVR2:	DB	'CORRECT?  ',0	;MOVE INTO WSOVLY1.OVR AT 28E4H

;*****************************************************************
;**                                                             **
;**             PATCH DATA FOR WSMSGS.OVR                       **
;**                                                             **
;*****************************************************************

WSMSG1:	DB	'C  Run Correct-It '	;USED TWICE AT 0496H AND
					;  06E8H
WSMSG2:	DB	0	;MOVE INTO WSMSGS.OVR AT 38E0H

;*****************************************************************
;**                                                             **
;**             PATCH DATA FOR CORRECT.COM                      **
;**                                                             **
;*****************************************************************

CRCT1:	JMP 	01C0H		;MOVE INTO CORRECT.COM AT 0100H

CRCT2:	LXI 	H,7913H		;MOVE INTO CORRECT.COM AT 01C0H
	MOV 	A,M
	CPI 	01
	JC 	0103H
	CPI 	05    		; NOTE - MAX # PHYSICAL/LOGICAL DRIVES
	JNC 	0103H
	LXI	H,7923H
	MOV	M,A
	MVI 	C,21H
	LXI 	H,7913H
	LXI 	D,005CH           
	CALL 	01F6H
	MVI	C,21H
	LXI	H,7913H
	LXI	D,00DFH
	CALL	01F6H
	MVI 	C,20H
	LXI 	H,752BH
	LXI 	D,0080H
	CALL 	01F6H
	JMP 	0103H
	MOV 	A,M
	STAX 	D
	INX 	D
	INX 	H
	DCR 	C
	JNZ 	01F6H
	RET
CRCT$:	DB	0	;'0' MARKS END OF PATCH

CRCT3:	DB	CTRLZ,'Correct-It'	;CLEAR SCREEN SIGN-ON MESSAGE
					;AS IS 11 BYTES TO 2102

		;FOLLOWING CRCT PATCHES FOR MD2 ONLY

CRCT4:	DB	'C:AUXDICT.TXT'		;13 BYTES TO 24D8H
CRCT5:	DB	'C:FIXUP.COM'		;11 BYTES TO 354BH
CRCT6:	DB	03H			;1 BYTE TO 436BH

;*****************************************************************
;**                                                             **
;**             PATCH DATA FOR FIXUP.COM                        **
;**                                                             **
;*****************************************************************

FIXUP1:	JMP 	01C0H	;MOVE INTO FIXUP.COM AT 0100H

FIXUP2:	MVI 	C,21H	;MOVE INTO FIXUP.COM AT 01C0H
	LXI 	H,00DFH
	LXI 	D,005CH
	MOV 	A,M
	STAX 	D
	INX 	D
	INX 	H
	DCR 	C
	JNZ 	01C8H
	JMP 	0103H
FIXUP$:	DB	0

FIXUP3:	DB	CTRLZ,'Morrow Designs Correct-It(tm) '
			;31 BYTES TO 03B2H
			;CLEAR SCREEN SIGN-ON FOR FIXUP

		;FOLLOWING FIXUP PATCHES FOR MD2 ONLY

FIXUP4:	DB	'C:DICT.BIN'	;10 BYTES TO 5487H
FIXUP5:	DB	'C:DINDEX.BIN'  ;12 BYTES TO 554FH

;*****************************************************************
;**                                                             **
;**     REQUIRED FILE CONTROL BLOCKS FOR READING, WRITING       **
;**     AND RENAMING FILES                                      **
;**								**
;**	ALL FILES READ FROM AND WRITTEN TO 'A' DRIVE            **
;**								**
;**	FCB1's - USED FOR FILE READ				**
;**	FCB2's - USED FOR FILE WRITE				**
;**	FCB3's - USED FOR FILE RENAME				**
;**                                                             **
;*****************************************************************

WSFCB1:	DB	01,'WS      COM',0,0,0,0
	DB	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
WSFCB2:	DB	01,'WS      $$$',0,0,0,0
	DB	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
WSFCB3:	DB	01,'WS      $$$',0,0,0,0
	DB	0,'WS      COM',0,0,0,0,0

OVRFCB1: DB	01,'WSOVLY1 OVR',0,0,0,0
	 DB	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
OVRFCB2: DB	01,'WSOVLY1 $$$',0,0,0,0
	 DB	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
OVRFCB3: DB	01,'WSOVLY1 $$$',0,0,0,0
	 DB	0,'WSOVLY1 OVR',0,0,0,0,0

MSGFCB1: DB	01,'WSMSGS  OVR',0,0,0,0
	 DB	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
MSGFCB2: DB	01,'WSMSGS  $$$',0,0,0,0
	 DB	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
MSGFCB3: DB	01,'WSMSGS  $$$',0,0,0,0
	 DB	0,'WSMSGS  OVR',0,0,0,0,0

CRTFCB1: DB	01,'CORRECT COM',0,0,0,0
	 DB	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
CRTFCB2: DB	01,'CORRECT $$$',0,0,0,0
	 DB	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
CRTFCB3: DB	01,'CORRECT $$$',0,0,0,0
	 DB	0,'CORRECT COM',0,0,0,0,0

FIXFCB1: DB	01,'FIXUP   COM',0,0,0,0	
	 DB	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
FIXFCB2: DB	01,'FIXUP   $$$',0,0,0,0
	 DB	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
FIXFCB3: DB	01,'FIXUP   $$$',0,0,0,0
	 DB	0,'FIXUP   COM',0,0,0,0,0

;*****************************************************************
;**                                                             **
;**     READ FILE FROM DISK INTO MEMORY BUFFER                  **
;**                                                             **
;**     ENTRY CONDITIONS:  REGISTER 'H' - FILE CONTROL BLOCK    **
;**                                       ADDRESS               **
;**                                                             **
;*****************************************************************

GET:	MVI	C,33		;MOVE FILE CONTROL BLOCK INTO 
	LXI	D,TFCB		;  TRANSIENT BUFFER AT 5CH
	CALL	MOVE		;
	LXI	D,TFCB		;
	LXI	H,BUFFR		;GET BUFFER START
	SHLD	NEXT		;  ADDRESS FOR DMA
	MVI	C,OPENF		;AND OPEN FOR READ
	CALL	BDOS		;
	CPI	BDERR		;IS IT THERE?
	JNZ	GET1		;  YES, READ IT IN
	CALL	SPMSG	
	DB	'-- Unable to Locate ',0
	CALL	SHOFN		;SHOW FILE NAME
ERREX:	CALL	SPMSG	
	DB	BELL,CR,LF
	DB	'-- Program Aborting'
	DB	CR,LF
	DB	'-- See .DOC file For More Information',CR,LF,LF,0
	JMP	DONE

GET1:	XRA	A		;ZERO RECORD COUNTERS
	STA	RECCT		
	STA	RECCT2
GET2:	LHLD	NEXT		;SET BUFFER ADDRESS
	XCHG
	MVI	C,SDMAF	
	CALL	BDOS
	LXI	D,TFCB		;READ ONE RECORD INTO
	MVI	C,READF		;BUFFER
	CALL	BDOS		;
	CPI	BDAOK		;READ OK?
	JZ	GET3		;YES, DO MORE
	CPI	BDER1		;MAYBE, END OF FILE?
	JZ	GETEX		;YES, NO PROBLEM
	CALL	REMSG		;NO, SHOW ERROR
	JMP	ERREX		;AND ALL DONE

GET3:	LDA	RECCT		;COUNT THE RECORD
	INR	A	
	CPI	255
	CZ	GET4
	STA	RECCT
	LHLD	NEXT		;INCREMENT BUFFER ADDRESS
	LXI	D,128		;BY RECORD SIZE
	DAD	D
	SHLD	NEXT
	LDA	MEMAX		;ROOM LEFT IN RAM
	DCR	A		;STOP BELOW CCP
	CMP	H		;COMPARE MSB
	JNZ	GET2		;CONTINUE IF NOT EQUAL
	CALL	SPMSG		;  ELSE SHOW OUT OF MEMORY
	DB	'-- Out of Memory',0
	JMP	ERREX		;AND GIVE UP

GET4:	LDA	RECCT2
	INR	A
	STA 	RECCT2
	MVI	A,0
	RET

GETEX:	CALL	CPDMA		;RESTORE CP/M DMA
	RET


;*****************************************************************
;**                                                             **
;**     WRITE FILE TO DISK FROM MEMORY BUFFER                   **
;**                                                             **
;**     ENTRY CONDITIONS:  REGISTER PAIR HL - TEMPORY FILE      **
;**                                  CONTROL BLOCK ADDRESS      **
;**                                                             **
;*****************************************************************

PUT:	MVI	C,33	;MOVE TEMPORY FCB INTO TRANSIENT 
	LXI	D,TFCB	;  AT 5CH
	CALL	MOVE
	LXI	H,BUFFR		;SET UP BUFFER START
	SHLD	NEXT
	LDA	RECCT		;SAVE RECORD COUNT
	STA 	CTSAV
	LDA	TFCB		;LOG-IN SELECTED DISK
	ORA	A		;IS IT LEGAL?
	JNZ	PUT1	
	CALL	WROPN		;NO, SHOW UNABLE TO OPEN
	JMP	PUTEX		;AND TRY AGAIN
	
PUT1:	MVI	C,RESET		;ENABLE WRITE ON ANY DISK
	CALL	BDOS	
	XRA	A		;INITIALIZE FCB
	STA	FCBCR		;CURRENT RECORD
	LXI	H,0	
	SHLD	FCBEX		;EXTENT AND S1
	SHLD	FCBS2		;S2 AND RECORD COUNT
	LXI	D,TFCB		;SEE IF FILE EXISTS
	MVI	C,FINDF		;FIND FUNCTION
	CALL	BDOS
	CPI	BDERR		;IS IT THERE ALREADY?
	JZ	PUT2		;  NO, OPEN FILE & WRITE
	LXI	D,TFCB		;  YES, ERASE IT
	MVI	C,DELETE		;CP/M DELETE FUNCTION
	CALL	BDOS
PUT2:	LXI	D,TFCB		;OPEN A FILE FOR WRITE
	MVI	C,MAKEF		;MAKE A FILE FUNCTION
	CALL	BDOS	
	CPI	BDERR		;GOT IT MADE?
	JNZ	PUT3
	CALL	WROPN		;NO, SHOW UNABLE
	JMP	PUTEX		;AND TRY AGAIN

PUT3:	LHLD	NEXT		;WRITE BUFFER TO DISK
	XCHG			;(FINALLY)
	MVI	C,SDMAF		;SET ADDRESS TO WRITE FROM
	CALL	BDOS	
	LHLD	NEXT		;THEN INCREMENT BY 128
	LXI	D,128		
	DAD	D
	SHLD	NEXT
	LXI	D,TFCB
	MVI	C,WRITF		;WRITE A RECORD FUNCTION
	CALL	BDOS		;
	CPI	BDAOK		;WRITE OK?
	JZ	PUT4
	CALL	WEMSG		;NO, WRITE ERROR MESSAGE
	JMP	PUTEX		; AND TRY AGAIN

PUT4:	LDA	RECCT		;COUNT THE RECORD
	DCR	A
	STA	RECCT		;IF NOT END OF RECORDS
	JNZ	PUT3		;THEN DO ANOTHER
	LDA	RECCT2
	ORA	A
	JZ	PUT5
	DCR	A
	STA	RECCT2
	MVI	A,255
	STA	RECCT
	JMP	PUT3
PUT5:	CALL	CPDMA		;ELSE RESTORE CP/M DMA
	LXI	D,TFCB
	MVI	C,CLOSF		;THEN CLOSE THE FILE
	CALL	BDOS
PUTEX:	CALL	CPDMA		;RESTORE CP/M DMA
	RET			;AND ALL DONE

;*****************************************************************
;**                                                             **
;**            "MOVE" C bytes from HL to DE                     **
;**                                                             **
;*****************************************************************
	
MOVE:	MOV	A,M
	STAX	D
	INX	D
	INX	H
	DCR	C
	JNZ	MOVE
	RET

;*****************************************************************
;**                                                             **
;**     DELETE FILE - ENTRY CONDITIONS:  FCB IN REGISTER DE     **
;**                                                             **
;*****************************************************************

DELF:	MVI	C,DELETE	;CP/M DELETE FILE FUNCTION
	CALL	BDOS
	INR	A
	RNZ
	CALL	SPMSG
	DB	'-- Delete File Error',CR,LF,0
	JMP	DONE

;*****************************************************************
;**                                                             **
;**     RENAME FILE - ENTRY CONDITIONS:  FCB IN REGISTER DE     **
;**                                                             **
;*****************************************************************

RENF:	MVI	C,RENAME	;CP/M RENAME FILE FUNCTION
	CALL	BDOS
	INR	A
	RNZ	
	CALL	SPMSG
	DB	'-- Rename File Error',CR,LF,0
	JMP	DONE

;*****************************************************************
;**                                                             **
;**            MISCELLANEOUS ERROR MESSAGES                     **
;**                                                             **
;*****************************************************************

REMSG:	CALL	SPMSG
	DB	'-- Permanent Read Error',CR,LF,0
	RET

;DISPLAY WRITE ERROR MESSAGE
WEMSG:	CALL	SPMSG
	DB	'-- Permanent Write Error',CR,LF,0
	RET

;DISPLAY WRITE OPEN ERROR MESSAGE
WROPN:	CALL	SPMSG
	DB	'-- Unable to Open File For Write',CR,LF,0
	RET

;*****************************************************************
;**                                                             **
;**     RESTORE CP/M DMA ADDRESS TO THE TRANSIENT BUFFER        **
;**                                                             **
;*****************************************************************

CPDMA:	LXI	D,TBUFF
	MVI	C,SDMAF
	CALL	BDOS
	RET

;*****************************************************************
;**                                                             **
;**     CONSOLE INPUT - CHARACTER IN REGISTER A                 **
;**                                                             **
;*****************************************************************

CI:	PUSH	B		;SAVE REGISTERS
	PUSH	D		
	PUSH	H	
	MVI	C,RCONF		;READ FUNCTION
	CALL	BDOS
	ANI	7FH		;MASK TO 7 BITS
	POP	H		;RESTORE REGISTERS
	POP	D	
	POP	B
	RET

;*****************************************************************
;**                                                             **
;**   CHARACTER IN REGISTER A OUTPUT TO CONSOLE                 **
;**                                                             **
;*****************************************************************

CO:	PUSH	B		;SAVE REGISTERS
	PUSH	D
	PUSH	H
	MVI	C,WCONF		;SELECT FUNCTION
	MOV	E,A		;CHARACTER TO E
	CALL	BDOS		;OUTPUT BY CP/M
	POP	H		;RESTORE REGISTERS
	POP	D
	POP 	B
	RET

;*****************************************************************
;**                                                             **
;**   DISPLAY MESSAGE POINTED TO BY HL REGISTER PAIR            **
;**                                                             **
;*****************************************************************

COMSG:	MOV	A,M		;GET A CHARACTER
	ORA	A		;ZERO IS THE TERMINATOR
	RZ			;RETURN ON ZERO
	CALL	CO		;ELSE OUTPUT THE CHARACTER
	INX	H		;POINT TO THE NEXT ONE
	JMP 	COMSG		;AND CONTINUE

;*****************************************************************
;**                                                             **
;**   DISPLAY MESSAGE POINTED TO BY STACK                       **
;**                                                             **
;*****************************************************************

SPMSG:	XTHL			;GET "RETURN ADDRESS" TO HL
	XRA	A		;CLEAR FLAGS AND ACCUMULATOR
	ADD	M		;GET ONE MESSAGE CHARACTER
	INX	H		;POINT TO NEXT
	XTHL			;RESTORE STACK FOR
	RZ			;RETURN IF DONE
	CALL	CO		;ELSE DISPLAY CHARACHTER 
	JMP	SPMSG		;AND DO ANOTHER

;*****************************************************************
;**                                                             **
;**     DISPLAY FILENAME AND TYPE FROM TRANSIENT FCB            **
;**                                                             **
;*****************************************************************

SHOFN:	PUSH 	B	        ;IT DOESN'T HURT TO STORE
	PUSH	D               ;THE REGISTERS - OUCH!
	PUSH	H	
	PUSH	PSW
SHFN1:	LXI	H,TFCB		;SHOW DISK DRIVE
	MOV	A,M		
	ANI	0FH		;LIMIT TO 4 BITS
	ORI	40H		;CONVERT TO ASCII
 	CPI	'@'		;SEE IF DEFAULT DISK
	JNZ	SHFN2		;  NO, THEN SHOW SELECTION
	PUSH	H
	MVI	C,DFDSKF        ;  ELSE, ASK CPM WHICH DISK IT IS
	CALL	BDOS
	POP	H
	ADI	'A'             ;CONVERT TO ASCII
SHFN2:	CALL	CO              ;SHOW THE DISK DRIVE
	MVI	A,':'		;SHOW THE COLON
	CALL	CO
	INX	H		;AND SHOW THE FILE NAME
	MVI	C,8		;  EIGHT CHARACTERS
	CALL	SHFN3
	LXI	H,FCBTY		;CHECK IF TYPE GIVEN
	MOV	A,M
	CPI	' '		
	JZ	SHFNEX		;  NO, THEN EXIT
	MVI	A,'.'		;ELSE, SHOW SEPARATOR
	CALL	CO		
	MVI	C,3
	CALL	SHFN3		;AND SHOW THE TYPE
SHFNEX:	POP 	PSW		;RESTORE REGISTERS
	POP	H
	POP	D
	POP	B		
	RET			;  AND RETURN

SHFN3:	MOV	A,M		;GET A CHARACTER
	CPI	' '  		;IF IT'S A SPACE
	RZ			;  THEN WE'RE WITH NAME OR TYPE
	CALL	CO		;ELSE OUTPUT THE CHARACTER
	DCR	C		;IF CHARACTER COUNT IS ZERO
	RZ			;  THEN WE'RE ALL DONE WITH NAME OR TYPE
SHFN4:	INX	H		;POINT TO NEXT CHARACTER
	JMP	SHFN3		;AND CONTINUE


;*****************************************************************
;**                                                             **
;**              SIGN-ON MESSAGE                                **
;**                                                             **
;*****************************************************************

SINON:	DB	CTRLZ
	DB	'PATCH-IT.COM  -  Vers. 1.0  - 03/22/84',CR,LF
	DB      '======================================',CR,LF,LF
	DB	0

;*****************************************************************
;**                                                             **
;**     RAM VARIABLES AND BUFFERS                               **
;**                                                             **
;*****************************************************************

DRSAV:	 DS	1		;CURRENT DRIVE AT ENTRY
SPSAVE:	 DW 	0
INBUF:	 DS	83		;LINE INPUT BUFFER
RECCT:	 DS	1		;TOTAL RECORDS READ/TO WRITE
RECCT2:	 DS	1		;  EXTENSION FOR ABOVE COUNTER FOR 32K+ FILES
CTSAV:	 DS	1		;SAVE LOCATION FOR COUNT
NEXT:	 DS	2		;NEXT DMA ADDRESS
TYPEFLAG DS	1		;USED BY SHOW FILE NAME SUBROUTINE
MD2FLAG	 DS	1		;USED TO CHOOSE/OMIT MD2 PATCH SUBROUTINES

;*****************************************************************
;**                                                             **
;**     SET UP STACK AND BUFFER SPACE                           **
;**                                                             **
;*****************************************************************

	DS	64		;40H LOCATIONS
STAK:	DB	0		;TOP OF STACK

BUFFR:	EQU	$
	OFFSET	EQU	(BUFFR-TPA)	;OFFSET ADDED TO NORMAL LOAD ADDRESS
	END
