; WordStar custom terminal installation program.  Ver. 1.0 by William Meyer
; WordStar is a trademark of MicroPro International.
;
; this program is released into the public domain, and may not be sold or
; licensed.  The author retains all rights, for the sole purpose of preventing
; illegal use of this program, such as resale.
;
; As written, this program will install into the WS.COM file (version 3.0) a
; pair of jumps, some code, a patch to a pointer, and a pair of strings.  The
; purpose of all this is to allow WordStar to program the function keys of
; a modern terminal, and to restore them to CP/M functions on the way out.
;
; My terminal is a Liberty FREEDOM-200, so the strings are designed for
; that unit.
;
; To use this program, first run install on an unmodified copy of WordStar
; such as WSU.COM.  Then use DDT to install this code.  As follows:
;       A>DDT WS.COM	    assuming you called it WS.COM
;	-IWSKEYIN.HEX       this file after assembly
;	-R                  no offset
;	-^C                 get out of DDT
;	A>SAVE XX WS.COM    note the correct number of pages per DDT
;
;       sorry, but I feel if you don't understand these brief instructions,
;       you shouldn't try this, anyway
;
; Revision History:
; 6  Mar 84:    revised: now in 8080 (ugh!) compatible form.  not tested.
; 6  Mar 84:	revised: SCRMES now uses CPM fn 6, fixes loading of scroll keys
; 14 Feb 84:	1st release, version 1.0
;
BDOS:	EQU	5
PUTS:	EQU	9

INISUB:	EQU	2A4H	; WS terminal init hook
UNISUB:	EQU	2A7H    ; WS terminal de-init hook
MORPAT:	EQU	2E0H    ; WS patching area
PBGMEM:	EQU	35CH    ; WS pointer address for start of editing area
WS$TOP:	EQU	3F00H   ; top of WS.COM
MEMORY:	EQU	7849H   ; initial value in PBGMEM *** check this ***

CR:	EQU	0DH
LF:	EQU	0AH
ESC:	EQU	1BH
DEL:	EQU	7FH
;
CNTLA:	EQU     'A'-'@'
CNTLB:  EQU     'B'-'@'
CNTLC:  EQU     'C'-'@'
CNTLD:  EQU     'D'-'@'
CNTLE:  EQU     'E'-'@'
CNTLF:  EQU     'F'-'@'
CNTLG:  EQU     'G'-'@'
CNTLH:  EQU     'H'-'@'
CNTLI:  EQU     'I'-'@'
CNTLJ:  EQU     'J'-'@'
CNTLK:  EQU     'K'-'@'
CNTLL:  EQU     'L'-'@'
CNTLM:  EQU     'M'-'@'
CNTLN:  EQU     'N'-'@'
CNTLO:  EQU     'O'-'@'
CNTLP:  EQU     'P'-'@'
CNTLQ:  EQU     'Q'-'@'
CNTLR:  EQU     'R'-'@'
CNTLS:  EQU     'S'-'@'
CNTLT:  EQU     'T'-'@'
CNTLU:  EQU     'U'-'@'
CNTLV:  EQU     'V'-'@'
CNTLW:  EQU     'W'-'@'
CNTLX:  EQU     'X'-'@'
CNTLY:	EQU	'Y'-'@'
CNTLZ:  EQU     'Z'-'@'
CNTLUAR:  EQU	'^'-'@'

	ORG	INISUB
	JMP	START

	ORG	UNISUB
	JMP	EXIT

	ORG	PBGMEM
	DW	NEWMEM

	ORG	MORPAT
START:
	LXI	H,WS$TOP
        LXI	D,MEMORY
        LXI	B,COD$LGTH
LOOP1:
	MOV	A,(HL)
	STAX	D
	INX	H
	INX	D
	DCX	B
	MOV	A,B
	ORA	C
	JNZ	LOOP1
        JMP	INIT

; in the code that follows, the assembler is duped into generating code
; at address 3F00 which will run properly after being moved to address
; MEMORY.  If you don't understand this trick, don't modify this code!!!
;
	ORG	WS$TOP
INIT:   EQU	MEMORY
        LXI	H,MSG2
        JMP	SNDIT

EXIT:   EQU	MEMORY+($-WS$TOP)
        LXI	H,MSG1

SNDIT:  EQU	MEMORY+($-WS$TOP)
SNDMES:
	MOV	A,M	; get char
        CPI	'$'   	; done?
        RZ       	; yes
        MOV	E,A     ; else send char
        MVI	C,6     ; using direct i/o
	PUSH	H
        CALL	BDOS
	POP	H
        INX	H      ; next char
        JMP	SNDMES

; modify the DB statements only!! this is the string which is used to de-init
; your terminal before exitting to CP/M
;
MSG1:   EQU	MEMORY+($-WS$TOP)
        DB      ESC,'|11','A:',CR,CNTLY ; fn 1
	DB	ESC,'|21','B:',CR,CNTLY ; fn 2
	DB	ESC,'|31','C:',CR,CNTLY ; fn 3
	DB	ESC,'|41','D:',CR,CNTLY ; fn 4
	DB	ESC,'|<1','DFDRV A:',CR,CNTLY ; fn 1 shifted
	DB	ESC,'|=1','DFDRV B:',CR,CNTLY ; fn 2 shifted
	DB	ESC,'|>1','DFDRV C:',CR,CNTLY ; fn 3 shifted
	DB	ESC,'|?1','DFDRV D:',CR,CNTLY ; fn 4 shifted
	DB	ESC,'|:1','A:SYSLOAD A:BNKICPM',CR,CNTLY ; fn 10
	DB	ESC,'|;1',cntly		; ins mode
        DB	ESC,'|',0,'1',CNTLY	; ins char
        DB	ESC,'|',1,'1',CNTLY	; del char
        DB	ESC,'|',2,'1',CNTLY	; ins line
        DB	ESC,'|',3,'1',CNTLY	; del line
        DB	ESC,'|',4,'1',CNTLY	; era eol
        DB	ESC,'|',5,'1',CNTLY	; era eop
        DB	ESC,'|',6,'1',CNTLY	; send
        DB	ESC,'|',7,'1',CNTLY	; print
        DB	ESC,'|',8,'1',CNTLY	; scroll up
        DB	ESC,'|',9,'1',CNTLY	; scroll down
        DB	ESC,'|',10,'1',CNTLA,CNTLY ; word left
        DB	ESC,'|',11,'1',CNTLF,CNTLY ; word right
	DB	ESC,'|',CNTLN,'1',CNTLK,CNTLY   ; cursor up
        DB	ESC,'|',CNTLO,'1',CNTLV,CNTLY   ; cursor down
        DB	ESC,'| 1',CNTLH,CNTLY		; cursor left
        DB	ESC,'|!1',CNTLL,CNTLY		; cursor right
        DB	ESC,'|',CNTLL,'1',CNTLUAR,CNTLY	; cursor home
	DB	'$'      

; modify the DB statements only!!  this is the string which is used to init
; your terminal on the way into WordStar
;
MSG2:   EQU	MEMORY+($-WS$TOP)
        DB      ESC,'|11',CNTLK,CNTLB,CNTLY ; fn 1	mark begin
	DB	ESC,'|21',CNTLK,CNTLK,CNTLY ; fn 2      mark end
	DB	ESC,'|31',CNTLK,CNTLH,CNTLY ; fn 3      hide marks
	DB	ESC,'|41',CNTLY ; fn 4
	DB	ESC,'|51',CNTLY         ; fn 5
	DB	ESC,'|61',CNTLQ,CNTLC,CNTLY ; fn 6      home bottom
	DB	ESC,'|71',CNTLY         ; fn 7
	DB	ESC,'|81',CNTLY         ; fn 8
	DB	ESC,'|91',CNTLR,CNTLY   ; fn 9          page up
	DB	ESC,'|:1',CNTLC,CNTLY   ; fn 10         page down
	DB	ESC,'|;1',cntlv,cntly	; ins mode      insert on/off
        DB	ESC,'|',0,'1',CNTLY	; ins char
        DB	ESC,'|',1,'1',CNTLY	; del char
        DB	ESC,'|',2,'1',CNTLY	; ins line
        DB	ESC,'|',3,'1',CNTLY	; del line
        DB	ESC,'|',4,'1',CNTLY	; era eol
        DB	ESC,'|',5,'1',CNTLY	; era eop
        DB	ESC,'|',6,'1',CNTLY	; send
        DB	ESC,'|',7,'1',CNTLY	; print
        DB	ESC,'|',8,'1',CNTLW,CNTLY ; scroll up
        DB	ESC,'|',9,'1',CNTLZ,CNTLY ; scroll down
        DB	ESC,'|',10,'1',CNTLA,CNTLY ; word left
        DB	ESC,'|',11,'1',CNTLF,CNTLY ; word right
	DB	ESC,'|',CNTLN,'1',CNTLE,CNTLY   ; cursor up
        DB	ESC,'|',CNTLO,'1',CNTLX,CNTLY   ; cursor down
        DB	ESC,'| 1',CNTLS,CNTLY		; cursor left
        DB	ESC,'|!1',CNTLD,CNTLY		; cursor right
        DB	ESC,'|',CNTLL,'1',CNTLQ,CNTLR,CNTLY	; cursor home
	DB	ESC,'|',CNTLG,'1',CNTLK,CNTLX,CNTLY ; print
	DB	'$'      

COD$LGTH:	EQU	$-WS$TOP

NEWMEM:	EQU	MEMORY+COD$LGTH
