;/// WORDFIND.Z80 --- Wordfinder (SAT Dictionary) version 1.8
;/// By Chris Hiszpanski, 11/10/1998 ///////////////////

	.nolist
	#include 	"ion.inc"
	.list
#ifdef TI83P
	.org 		progstart-2
	.db		$BB,$6D

#else
	.org		progstart
#endif
	ret
	jr		nc,prog_start
	.db		"Word Finder v1.8",0

;/// VARIBLES //////////////////////////////////////////

#define curvat saferam1			;location in vat
#define temp   saferam1+1		;used to track first 8 words
#define word   saferam1+2		;current word
#define stop   saferam1+3		;area in mem where vat search paused (two bytes)
#define sector saferam1+5		;area in memory of current volume (two bytes)

#define kUp    003h			;key equates
#define kDown  004h			;	
#define kEnter 005h			;
#define kClear 009h			;

;/// INTRO SETUP ///////////////////////////////////////

prog_start:
     bcall(_clrlcdf)			;
     bcall(_cleargbuf)			;
     bcall(_homeup)			;
     set textInverse,(iy+textflags)	;Reverse video
     ld hl,title			;Displays title
     bcall(_puts)		        ;
     res textInverse,(iy+textflags)	;
     ld hl,9*256+1			;Displays my name
     ld (pencol),hl			;
     ld hl,name				;
     bcall(_vputs)			;
     ld hl,17*256+1			;aka (Man in the Moon)
     ld (pencol),hl			;
     ld hl,aka				;
     bcall(_vputs)			;
     ld hl,35*256+1			;directions
     ld (pencol),hl			;
     ld hl,dir_one			;
     bcall(_vputs)			;
     ld hl,45*256+1			;
     ld (pencol),hl			;
     ld hl,dir_two			;
     bcall(_vputs)			;
     ld hl,55*256+35			;press mode
     ld (pencol),hl			;
     ld hl,press_mode			;
     bcall(_vputs)			;

pause:     
     bcall(_getk)			;bugs were reported with getkey and
     bcall(_op2toop1)			;
     bcall(_convop1)			;
     cp 21				;direct input is too fast
     jr z, vatsearch			;
     cp 23				;
     jp z, progquit			;
     jr pause				;


;/// VAT SEARCH ////////////////////////////////////////

vatsearch:
     bcall(_clrlcdf)			;
     bcall(_clrscrf)			;
     set textInverse, (iy+textflags)	;     

     ld hl,12*256+20			;Displays the title of prog select
     ld (pencol),hl			;
     ld hl,menu_bar			;
     bcall(_vputs)			;
   					;
     res textInverse, (iy+textflags)	;
   					;
	ld	hl,(progptr)
	ld	ix, progname
	call	detect
	jp	nz, noprog

find_prog:
	ld	 hl,(progptr)
	ld	(stop),hl
	jp	paste_loop

          				          
;/// MAIN //////////////////////////////////////////////

main:
     bcall(_clrscrf)			;
     bcall(_homeup)			;
     ld a,2				;first word is in third string
     ld (word),a			;word counter
     ld hl,(sector)			;area where external program is located at
     call getstring			;
     set textInverse, (iy+textflags)	;show that this is where the cursor is
     bcall(_puts)			;
     res textInverse, (iy+textflags)	;
     ld a,1				;first word displayed; move on to 2nd
     ld (temp),a			;

loop:
     call disp_next			;display first 8 words
     ld a,(temp)			;track which word has been displayed
     inc a				;
     ld (temp),a			;
     cp 8				;has it been the 8th
     jr nz,loop				;no, keep displaying
     ld hl,0				;set cursor to row one
     ld (currow),hl			;
     ld a,2				;
     ld (word),a			;				
     
downup:
     bcall(_getk)			;
     bcall(_op2toop1)			;
     bcall(_convop1)			;
     cp 21				;
     jp z, disp_word			;
     cp 23				;
     jp z, progquit			;
     cp 25				;
     jp z, previous			;
     cp 34				;
     jp z, next				;
     jp nz, downup			;

     
;/// ROUTINES //////////////////////////////////////////

previous:
     ld a,(word)		;check if at first word
     cp 2			;
     jp z,downup		;
     ld hl,0			;refresh curcol
     ld (curcol),hl		;
     ld a,(currow)		;check if cursor is on first row
     cp 0			;
     jp z,prevscroll		;if so scroll screen down
     ld a,(word)		;
     ld hl,(sector)		;area in mem
     call getstring		;paste word with no inverse
     bcall(_puts)		;
     ld a,(word)		;
     dec a			;previous word
     dec a              	;skip definition
     ld (word),a		;
     ld hl,(currow)		;update cursor position
     dec hl			;
     ld (currow),hl		;
     ld hl,0			;refresh curcol
     ld (curcol),hl		;
     ld hl, blank		;clear current line
     bcall(_puts)		;
     ld hl,0			;refresh column
     ld (curcol),hl		;
     ld a,(word)		;
     ld hl,(sector)		;area in mem
     call getstring		;output in inverse indicating cursor
     set textInverse,(iy+textflags)
     bcall(_puts)		;
     res textInverse,(iy+textflags)
     jp downup			;

next:
     ld hl,0			;
     ld (curcol),hl		;
     ld a,(word)		;paste word with no inverse
     ld hl,(sector)		;area in mem
     call getstring		;
     bcall(_puts)		;
     ld a,(word)		;
     inc a			;select next word
     inc a			;skip definition
     ld (word),a		;
     ld a,(currow)		;if cursor on last line scroll up
     cp 7			;
     jp z, nextscroll		;
     inc a			;else next row
     ld (currow),a		;
     ld hl,0			;refresh curcol
     ld (curcol),hl		;
     ld hl,blank		;clear last word
     bcall(_puts)		;
     ld hl,0			;refresh curcol
     ld (curcol),hl		;
     ld a,(word)		;
     ld hl,(sector)		;area in mem
     call getstring		;output in inverse
     set textInverse, (iy+textflags)
     bcall(_puts)		;
     res textInverse, (iy+textflags)
     jp downup			;

disp_word:
     ld a,(word)		;
     ld b,a			;
     push bc			;
     bcall(_clrscrf)		;
     bcall(_homeup)		;
     ld a,(word)		;output word
     ld hl,(sector)		;area in mem
     call getstring		;
     bcall(_puts)     		;
     bcall(_homeup)		;setup display for definition
     ld a,(currow)		;
     inc a			;
     inc a			;3rd line
     ld (currow),a		;
     ld a,(word)		;select definition
     inc a			;
     ld hl,(sector)		;area in mem of current external volume
     call getstring		;display definition
     bcall(_puts)		;

pause_two:
     bcall(_getk)		; I used getk since bugs were reported with
     bcall(_op2toop1)		;
     bcall(_convop1)		;
     cp 23			; getkey and direct input
     jp z, progquit		;
     cp 21			;return
     jp z, resume		;
     jp pause_two		;

resume:
     bcall(_homeup)
     bcall(_clrscrf)
     pop bc
     push bc
     ld a,b
     ld (word),a
     ld hl,(sector)			;area where external program is located at
     call getstring			;
     set textInverse, (iy+textflags)	;show that this is where the cursor is
     bcall(_puts)			;
     res textInverse, (iy+textflags)	;
     ld a,1				;first word displayed; move on to 2nd
     ld (temp),a			;

loop2:
     call disp_next			;display first 8 words
     ld a,(temp)			;track which word has been displayed
     inc a				;
     ld (temp),a			;
     cp 8				;has it been the 8th
     jr nz,loop2			;no, keep displaying
     ld hl,0				;set cursor to row one
     ld (currow),hl			;
     pop bc
     ld a,b
     ld (word),a
     jp downup

prevscroll:
     bcall(_homeup)		;set display position to top row
     ld a,(word)		;copy word in normal video to get rid of cursor
     ld hl,(sector)		;area in mem of current external volume
     call getstring		;
     bcall(_puts)		;
     bcall(_scrollDown)		;scroll screen down (hardware scroll method)
     bcall(_indicatorOff)	;turns back on because of hardware scrolling
     ld a,(word)		;
     dec a			;get previous word
     dec a			;skip definiton
     ld (word),a		;
     bcall(_homeup)		;refresh displaying position
     ld hl,(sector)		;area in mem of current external volume
     call getstring		;copy new word in reverse video
     set textInverse,(iy+textflags)
     bcall(_puts)		;
     res textInverse,(iy+textflags)
     jp downup			;

nextscroll:
     bcall(_newline)		;copy next word with cursor to very bottom
     ld a,(word)		;
     ld hl,(sector)		;area in mem
     call getstring		;
     set textInverse, (iy+textflags)
     bcall(_puts)		;
     res textInverse, (iy+textflags)
     jp downup			;

disp_next:
     bcall(_newline)		;
     ld a,(word)		;which word
     inc a			;skip definition
     inc a			;
     ld (word),a		;store in counter
     ld hl,(sector)		;area in mem of current program
     call getstring		;
     bcall(_puts)		;
     ret			;
     
getkey:
     bcall(_getk)			; I used getk since bugs were reported with
     bcall(_op2toop1)		;
     bcall(_convop1)		;
     cp 23			; getkey and direct input
     jp z, progquit		;
     cp 21			;
     jp z, main			;
     cp 25			;
     jp z, find_prog		;
     cp 34			;
     jp z, paste_loop		;
     jp getkey			;

noprog:
     bcall(_clrlcdf)		;
     ld hl,20*256+20		;
     ld (pencol),hl		;
     ld hl, noprogtext		;if no program was found
     bcall(_vputs)		;
noprog1:
     bcall(_getk)		;
     bcall(_op2toop1)		;
     bcall(_convop1)		;
     cp 21
     jp z,progquit		;now quit
     jr noprog1     
  	
paste_loop:
     bcall(_clrlcdf)			;
     set textInverse, (iy+textflags)	;     
     ld hl,12*256+20			;Displays the title of prog select
     ld (pencol),hl			;
     ld hl,menu_bar			;
     bcall(_vputs)			;
     res textInverse, (iy+textflags)	;

     ld hl,(stop)		;continue search for volumes
     ld ix, progname		;		
     call detect		;
     jp nz, find_prog		;if no more volumes are present restart search
     ld (stop),de		;
     ld (sector),hl		;store position of current volume

     ld hl,20*256+20		;
     ld (pencol),hl		;

     ld hl,(sector)		;recall position of current volume
     ld a,1			;
     call getstring		;display name of volume
     bcall(_vputs)		;
     jp getkey			;

getstring:	or	a	;check end of line
	ret	z		;return if so
	ld	b,a		;back up a
	ld	a,0		;0 -> a
				

getStringL1:			;
	push	bc		;store bc
	cpir			;
	pop	bc		;
	djnz	getStringL1	;
	ret			;

detect = ionDetect

;/// OTHER DATA ////////////////////////////////////////

progname:
     .db "WORD",0

menu_bar:
     .db "Which Volume?",0

noprogtext:
     .db "No Volumes!",0

title:
     .db "Word Finder v1.8",0

name:
     .db "By Chris Hiszpanski",0

aka:
     .db "aka: Apparatus",0

dir_one:
     .db "Scorll Up/Down",0

dir_two:
     .db "Enter to select",0

press_mode:
     .db "Press 2nd",0

blank:
     .db "               ",0

clearline:
     .db "                              ",0

;/// END ///////////////////////////////////////////

progquit:
     ret
.end
END