
;	NOTEPAD application for Write-Hand-Man
;
;	Copyright (c) 1985 by Poor Person Software
;	and HiSoft 1987
.8080
	org	0
base	equ	$
bdos	equ	base+5
home	equ	base+010h
fcb	equ	base+05ch
fcbcr	equ	fcb+32
fcbr0	equ	fcb+33
fcbr2	equ	fcb+35
fcbs2	equ	fcb+14
bdos	equ	base+5
buf	equ	base+080h
;
open	equ	15
close	equ	16
ranread equ	33
ranwrit equ	34
pstring equ	9
setdma	equ	26
conio	equ	6

functab equ	base+018h

	org	0100h

	jmp	go
	org	0180h	   ; second page buffer
go:
	xra	a
	sta	fcbs2	   ; clear fcb fields
	sta	fcbr2
	lxi	d,fcb
	mvi	c,open	   ; open DAT file
	call	bdos
	inr	a
	jz	base	   ; return if failed
;
	lxi	h,0
	shld	page	   ; set current page # to 0
	call	getpage 	   ; read in page
	call	dispage 	   ; display page
setcur:
	xra	a
	sta	curcol
	inr	a	
	sta	curlin
loop:
.Z80
       
xbios	macro	adr
	call	0fc5ah
	dw	adr
	endm

KM_KT_GET	equ	0dah
KM_KT_PUT	equ	0ddh

kshift	equ	21
kalt	equ	80
kextra	equ	84
kshlock	equ	70
kright	equ	6
kleft	equ	15
kup	equ	14
kdown	equ	79
kplus	equ	23
kminus	equ	76
kexit	equ	8
kdelback equ	72
kcopy	equ	11
kfind	equ	20
kcut	equ	10
kcr	equ	18
kenter	equ	78
kfind	equ	20
kC	equ	62
kH	equ	44
kJ	equ	45
kV	equ	55
kY	equ	43

repeat	equ	3
shift	equ	5	; bit in the shift status byte
alt	equ	7

loop:
key1:	xbios	KM_KT_GET
	jr	nc,loop
; key number in bc
;ignore shifted stuff on its own
	ld	a,c
	cp	kshift
	jr	z,key1
	cp	kalt
	jr	z,key1
	cp	kextra
	jr	z,key1
	cp	kshlock
	jr	Z,key1

	res	3,b
	dec	b
	inc	b
	jr	nz,Shifted
;the unshifted codes
	cp	kdelback
	jp	z,dodel
	cp	kexit	   ; an esc
	jp	z,exit	   ; quit
	cp	kcr	; cr
	jp	z,doreturn
	cp	kenter
	jp	z,doreturn
	cp	kdown	; down cursor
	jp	z,dodwn
	cp	kleft	; left
	jp	z,doleft
	cp	kup	; up cursor
	jp	z,doup
	cp	kright	; right cursor
	jp	z,dorgt
	cp	kplus
	jp	z,doins	;toggle insert
	cp	kfind
	jp	z,dojump	;goto Page n
	cp	kcopy
	jp	z,doprint
	cp	kcut
	jp	z,docut
;now make it ASCII
normal:	xbios	KM_KT_PUT
	ld	c,conio
loop2:	ld	e,255
	call	bdos
	or	a
	jr	z,loop2
	cp	" "
	jr	c,key1
	jp	update
Shifted: bit	shift,b
	jr	z,Alted
;really shifted
	cp	kright
	jp	z,dofor	;forward a page
	cp	kleft
	jp	z,doback	;back a page
	jr	normal
Alted:	bit	alt,b
	jp	z,normal	;not shift or alt assume ascii
	cp	kY
	jp	z,doers		;erase page
	cp	kV
	jp	z,doins		;toggle insert
	cp	kJ
	jp	z,dojump	;goto page n
	cp	kC
	jp	z,docut		;cut
	cp	kH
	jp	z,dobsp		;destructive backspace
	jr	normal

.8080
exit:
	call	putpage 	   ; flush page if needed
;
	lxi	d,fcb
	mvi	c,close 	   ; close file
	call	bdos
	jmp	base	   ; return
;
getpage:
;
	call	set1	; set lower half
	lhld	page
	dad	h	; times 2
	shld	fcbr0
	call	readit
	call	set2	; set upper half
	lhld	page
	dad	h	; times 2
	inx	h	; plus 1
	shld	fcbr0
	call	readit
	lxi	h,buf+254
	mvi	m,020h	; put in blank
	inx	h
	mvi	m,020h	; put in blank
	ret
;
readit:
	mvi	c,ranread
	lxi	d,fcb
	call	bdos
	ora	a
	pop	h	; rectify stack in case of error
	jnz	blkpag
	pchl		; return
;
set1:
	lxi	d,buf	; first part of buffer
	call	setd
	ret
set2:
	lxi	d,buf+128	; second half
	call	setd
	ret
;
setd:
	mvi	c,26	; set dma
	call	bdos
	ret
;
putpage:
	lda	flush
	ora	a
	rz		; nothing to do
	lxi	h,buf+254
	mvi	m,13	; cr
	inx	h
	mvi	m,10	; lf
	call	set1
	lhld	page
	dad	h	; times 2
	shld	fcbr0
	call	writit
	call	set2
	lhld	page
	dad	h	; times 2
	inx	h	; second half
	shld	fcbr0
	call	writit
	xra	a
	sta	flush
	ret
;
writit:
	mvi	c,ranwrit
	lxi	d,fcb
	call	bdos
	ret
;
blkpag: 		; blank the whole buffer
	mvi	b,0	; 256 characters
	lxi	h,buf
blklp:
	mvi	m,32	; store a blank
	inx	h
	dcr	b
	jnz	blklp
	mvi	a,1	; say changed
	sta	flush
	ret
blkpg1: 		; blank all but first line
	mvi	b,247
	lxi	h,buf+7
	jmp	blklp
;
dispage:
	call	home	; home the cursor
	CALL	TITLE
dispg1:
	lxi	h,buf	; start of buffer
	mvi	b,8	; 8 lines
dloop:
	call	dline	; display line
	dcr	b
	jnz	dloop
.Z80
	ld	de,sline
	ld	c,9
	call	bdos
.8080
	mvi	b,32
dlp1:		; put out bottom line
	mvi	e,08ah
	push	b
	call	putchar
	pop	b
	dcr	b
	jnz	dlp1
.Z80
	ld	de,eline
	ld	c,9
	call	bdos
.8080
	lda	list
	ora	a
	rnz		; for print no home
;
	call	home	; home cursor
.Z80
	LD	DE,DOWN2
	LD	C,9
	CALL	bdos
	ret
;
DOWN2:	DEFB	10,10,085h,"$"
BORD:	DEFB	27,"q",085H,13,10,"$"
lbord:	defb	27,"q",085h,024h
sline:	defb	27,"q",83h,"$"
eline:	defb	89H,27,"q","$"
.8080
dline:
	push	b	; save b
;
	mvi	B,32	; characters per line
.z80
	push	bc
	push	hl
	ld	de,lbord
	ld	c,9
	call	bdos
	pop	hl
	pop	bc
.8080

dlp:
	mov	e,m	; get byte
	push	h
	push	b
	call	putchar 	; display it
	pop	b
	pop	h
	inx	h	; bump pointer
.Z80			; count
	DJNZ	dlp
.8080
;
	push	h	; save buffer pointer
.Z80	
	ld	de,BORD
	ld	c,9	 	; put border
	call	bdos		; next line
.8080
	pop	h
	pop	b
	ret
;
putchar:		; write character to console
	mvi	c,conio
	lda	list
	ora	a
	jz	putch1
	mvi	c,5	; printer
putch1:
	call	bdos
	ret
crlf:		; write CR/LF
.Z80
	LD	DE,NEWLINE
	LD	C,9
	CALL	bdos
	ret
NEWLINE:	13,10,"$"
.8080
movup:
	lda	curcol
	call	getadr
	lxi	d,buf+255
;
	mov	a,e
	sub	l
	mov	c,a
	mov	a,d
	sbb	h
	mov	b,a
;
	lxi	h,buf+254
movlp:
	mov	a,b
	ora	c
	jz	movlpe
	mov	a,m
	stax	d
	dcx	h
	dcx	d
	dcx	b
	jmp	movlp
movlpe:
	call	dispage
	call	dopos
	ret
update: 		; replace character in buffer
	push	psw
	lda	insert
	ora	a
	cnz	movup	; move buffer up one
	pop	psw
	push	psw
	mov	e,a
	call	putchar 	; put onto screen
	lda	curcol	; column number
	call	getadr	; address of character in buffer
	pop	psw	; restore character
	mov	m,a	; to buffer
	mvi	a,1
	sta	flush	; mark changed
;
nxtchar:
	lda	curcol
	inr	a
	sta	curcol	; advance column pointer
	cpi	32 
	jc	loop	; done
	xra	a
	sta	curcol	; next line
.z80
	ld	de,fred2
	ld	c,9
	call	bdos
.8080

;
nxtline:
	lda	curlin
	inr	a
	sta	curlin	; advance line
;
	cpi	8
	jc	loop	; no wrap
	xra	a
	sta	curlin	; wrap
	sta	curcol
	call	home
	mvi	e,10
	call	putchar
	mvi	e,085h
	call	putchar

	jmp	loop
;
fred2:	db	13,10,27,"C$"

getadr:
	push	psw	; column
	lda	curlin	; current line
	rlc		; 2
	rlc		; 4
	rlc		; 8
	rlc		; 16
	rlc		; 32
	mov	c,a
	pop	psw	; get column
	add	c	; now have position
	mov	c,a
	mvi	b,0
	lxi	h,buf	; buffer base
	dad	b	; final address
	ret
;
doins:
	lda	insert
	mov	b,a
	mvi	a,1
	sub	b
	sta	insert	; toggle insert
	jmp	loop
dodwn:		; down
	mvi	e,10
	call	putchar
	jmp	nxtline
doleft:
	lda	curcol
	ora	a
	jz	loop	; dont do anything
	dcr	a
	sta	curcol	; update pointer
	mvi	e,8
	call	putchar 	; move cursor
	jmp	loop
dofor:
	call	putpage 	; flush current page
	lhld	page
	inx	h
	shld	page	; bump page
	call	getpage 	; read it or blank new page
	call	dispage 	; display result
	jmp	setcur
dojump:
	call	putpage 	; flush the current page
	xra	a
	sta	jpage	; jump page address
dojl:
	mvi	c,conio
	mvi	e,255
	call	bdos	; get a character
	ora	a
	jz	dojl	; loop till ready
	cpi	13	; carriage return
	jz	dojmpgo 	; go to page
	cpi	030h
	jc	dojl	; skip non-numeric
	cpi	03ah	
	jnc	dojl	; skip non-numeric
	ani	0fh	; isolate digit
	mov	b,a	; save
	lda	jpage
	rlc		; times 2
	mov	c,a	; save this
	rlc		; times 4
	rlc		; times 8
	add	c	; times 10
	add	b	; plus new one
	sta	jpage
	jmp	dojl	; and loop
dojmpgo:
	mvi	h,0
	lda	jpage
	ora	a	; zero is zero
	jz	dojmpz
	dcr	a	; numbers start at 0 not 1
dojmpz:
	mov	l,a	; set page
	shld	page	; set page
	call	getpage
	call	dispage
	jmp	setcur
doback:
	call	putpage 	; flush current page
	lhld	page
	mov	a,h
	ora	l
	jz	loop	; on page zero
	dcx	h
	shld	page
	call	getpage
	call	dispage
	jmp	setcur
;
doreturn:		; next line
	mvi	a,32
	sta	curcol
	jmp	nxtchar 	; force new line
;
doers:
	call	blkpg1
	call	dispage
	jmp	setcur
;
dorgt:		; go right
	lda	curcol
	call	getadr	; get current character
	mov	e,m
	call	putchar 	; display it thus moving cursor
	jmp	nxtchar
;
dobsp:
	lda	curcol
	ora	a	; are we at beginning
	jz	loop	; do nothing
	dcr	a
	sta	curcol	; update column pointer
	call	getadr	; get address
.Z80
	PUSH	HL
	LD	HL,buf+255
	pop	de
	and	a
	sbc	hl,de
	ex	de,hl
	ld	c,e
	ld	b,d
	ld	e,l
	ld	d,h
	INC	HL
	ldir
	call	dispage
	call	dopos
.8080
	mvi	a,1
	sta	flush	; note change in buffer
	jmp	loop
doup:		; must go home and back
	lda	curlin
	ora	a
	jz	loop
;
	dcr	a
	sta	curlin	; decrement
	call	dopos	
	jmp	loop
;
dopos:
;
;	position to the currow curcol
;
	call	home	; start from here
	MVI	E,10
	CALL	putchar
	mvi	e,085h
	call	putchar
	lda	curlin	; are we at top
	ora	a
	jz	upcol	; skip line do column
uplp1:
	mvi	e,10	; output LF
	push	psw
	call	putchar
	pop	psw
	dcr	a	; loop to on previous line
	jnz	uplp1
upcol:		; now go to correct column
	lda	curcol
	ora	a
	rz		; already there
	mvi	b,0
uplp2:
	push	b
	mov	a,b
	call	getadr	; get address this character
	mov	e,m
	call	putchar 	; put it
	pop	b  
	inr	b	; advance counter
	lda	curcol
	cmp	b	; see if there 
	jnz	uplp2	; no loop some more
	ret		; yep
dodel:
;		     
;    move the buffer down 1 overlaying the current character
;    redisplay and then reposition
;
	lda	curcol
	call	getadr	; set h to character in buffer
	lxi	d,buf+255
; subtract
	mov	a,e
	sub	l	; lower
	mov	c,a	; difference here
	mov	a,d
	sbb	h	; subtract with borrow
	mov	b,a	; count in bc
;
	push	b
	push	h
	lda	curcol
	inr	a
	call	getadr	; source of move
	pop	d
	pop	b	; length  h->d count b
dellp:
.Z80
	LDIR
.8080

dellpe:
	lxi	h,buf+255
	mvi	m,020h	       ; blank last byte
	call	dispage
	mvi	a,1
	sta	flush
	call	dopos
	jmp	loop
doprint:
.z80
	ld	e,13
	ld	c,5
	call	bdos
	ld	e,10
	ld	c,5
	call	bdos
	ld	hl,buf
	ld	b,8
priloop:
	call	printl
	push	bc
	push	hl
	ld	e,13
	ld	c,5
	call	bdos
	ld	e,10
	ld	c,5
	call	bdos
	pop	hl
	pop	bc
	djnz	priloop
	ld	e,13
	ld	c,5
	call	bdos
	ld	e,10
	ld	c,5
	call	bdos
	jp	loop

printl:
	push	bc
	ld	b,32
	ld	c,5
loop99:	push	hl
	push	bc
	ld	e,(hl)
	call	bdos
	pop	bc
	pop	hl
	inc	hl
	djnz	loop99
	pop	bc
	ret



.8080

docut:
	call	getf	;on return a = address of first marker
	push	h	; save
	call	gets	; address in bc of last character
	mov	c,a	; number to move till end
	cpi	0
	dcr	c
	jnz	ok
	pop	d
	jp	loop
ok:
	cpi	96
	jc	cut1
	mvi	c,95	; max  128-32 -1
cut1:
.z80
	ld	de,(functab)
	pop	hl
	inc	hl
cutl:
	ld	b,0
	ld	a,c
	cp	15
	jp	c,shortmove
	push	bc
	ld	bc,15
	ldir
	xor	a
	ld	(de),a
	inc	de
	pop	bc
	ld	a,c
	ld	c,15
	sub	c
	jp	m,resadd
	ld	c,a
	jp	cutl
resadd:
	add	a,c

shortmove:
	ld	b,0
	ldir
	xor	a
	ld	(de),a

	jp	loop

.8080

cutend:
	mvi	m,0
	jmp	loop
.Z80
getf:	ld	hl,buf
	ld	b,0
fredie:	ld	a,(hl)
	cp	0ABH
	jp	z,found
	inc	hl
	djnz	fredie
	ld	hl,buf-1
found:	ret	;hl contains address of first
gets:	;returns a=offset of second marker
	ex	de,hl
	ld	hl,buf+255
	xor	a
	sbc	hl,de
	ld	b,l
	push	bc
	ex	de,hl
fredo:	ld	a,(hl)
	cp	0ACH
	jp	z,founds
	inc	hl
	djnz	fredo
founds:		
	pop	de
	ld	a,D
	sub	b
	ret
;

TITLE:	LD	DE,PAGTIT
	LD	C,9
	CALL	bdos
	RET
PAGTIT:	DEFB	86H,27,"p"
	DEFM	"            Notepad             "
	defb	27,"q",8CH,13,10,"$"
;
.8080
insert: db	0	; insert mode flag
list:	db	0
curlin: db	0	; current line
curcol: db	0	; current col
flush:	db	0
jpage:	db	0
page:	db	0,0


	end

