; ****************************************************************************
; SEEK.ASM (by Erdogan Tan)
;
; TRDOS Kernel Test program for
; INT 21h Function 42h, Move File Pointer,

; 09/04/2011
;
; ****************************************************************************

PSP_CommandTail equ 80h

.8086

SINGLIXBOOT     SEGMENT PUBLIC 'CODE'
                assume cs:SINGLIXBOOT,ds:SINGLIXBOOT,es:SINGLIXBOOT,ss:SINGLIXBOOT

                org 100h
START_CODE:

proc_start      proc near

                xor bh, bh
               
                mov si, PSP_CommandTail
                mov bl, byte ptr [SI]
                or bl, bl                               
                jnz short loc_open_file_int21h  ; jump if not zero

                int 20h

loc_open_file_int21h:
                inc si
                mov byte ptr [SI][BX], bh     ; 0 
loc_open_file_int21h_next:
                inc si
                mov al, byte ptr [SI]
                cmp al, 20h                   ; is it SPACE ?
                ja short loc_open_file_int21h_fcall

                dec bl                                  
                jnz short loc_open_file_int21h_next
                
                int 20h

loc_open_file_int21h_fcall:
                call proc_open_file
                jc short loc_open_file_int21h_err

                mov word ptr [File_Handle], ax
                jmp short loc_open_file_int21h_ok 

loc_open_file_int21h_err:
                push ax
                mov al, ah
                call proc_hex 
                mov word ptr [Error_Number], ax
                pop ax 
                call proc_hex 
                mov word ptr [Error_Number]+2, ax

                mov si, offset Error_Number_Str
                call PROC_PRINTMSG

                int 20h 

loc_open_file_int21h_ok:

               ; mov si, offset Next_Line
	       ; call proc_printmsg

                mov word ptr [Page_Number], 22

loc_show_file_block_next:
                mov bx, word ptr [File_Handle] 
                mov cx, FBuffer_Size
                mov dx, offset File_Buffer               
                call proc_read_file
                jnc short loc_read_file_int21h_next

                push ax
                mov si, offset File_Read_Error_Str  
                call PROC_PRINTMSG
                pop ax
                jmp short loc_close_file_int21h

loc_read_file_int21h_next:
                or ax, ax  ; Read count
                jz short loc_close_file_int21h

loc_show_file_block_int21h: 
                push ax
                push cx

                call proc_show_file 
                
                pop cx ; Requested read count
                pop ax ; Read count  

                jc short loc_close_file_int21h

                cmp ax, cx
                jb short loc_close_file_int21h

                call proc_move_file_pointer

                jmp short loc_show_file_block_next 

loc_close_file_int21h:
                mov bx, word ptr [File_Handle] 
                mov ah, 3Eh ; MS DOS Function = Close File
                int 21h
               ;jc short loc_open_file_int21h_err

                int 20h

proc_start      endp


PROC_PRINTMSG     proc near
 
LOC_PRINTMSG_LOOP:
                lodsb                           ; Load byte at DS:SI to AL
                and     AL,AL            
                jz      short LOC_PRINTMSG_OK       
                mov     AH,0Eh                  
                mov     BX,07h             
                int     10h                     ; BIOS Service func ( ah ) = 0Eh
                                                ; Write char as TTY
                                                ;AL-char BH-page BL-color
                jmp     short LOC_PRINTMSG_LOOP           

LOC_PRINTMSG_OK:
                retn

PROC_PRINTMSG     endp

proc_open_file proc near

                mov     ah,0Eh
                mov     bx,07h
                mov     al,0Dh
                int     10h
                mov     al,0Ah
                int     10h

loc_open_file:
                mov dx, si
                mov ah, 3Dh ; MS DOS Function = Open File
                mov al, 0
                int 21h
                               
                retn

proc_open_file endp


;'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''';
; From binary (byte) to hexadecimal (character) converter    ;
;                                                            ;
; input -> AL = byte (binary number) to be converted         ;
; output -> AH = First character of hexadecimal number       ;
; output -> AL = Second character of hexadecimal number      ;
;                                                            ;
; (c) Erdogan TAN  1998 - 1999                               ;
;............................................................;

; 1998

proc_hex        proc    near

		db 0D4h,10h                     ; Undocumented inst. AAM
						; AH = AL / 10h
						; AL = AL MOD 10h
		or AX,'00'                      ; Make it ZERO (ASCII) based

                xchg AH,AL 

; 1999
		cmp AL,'9'
		jna pass_cc_al
		add AL,7
pass_cc_al:
		cmp AH,'9'
		jna pass_cc_ah
		add AH,7
pass_cc_ah:

; 1998
		retn

proc_hex        endp


proc_read_file proc near
               ;mov bx, ax
                mov ah, 3Fh ; MS DOS Function = Read File
               ;mov cx, FBuffer_Size
               ;mov dx, offset File_Buffer
                int 21h
                retn

proc_read_file endp


proc_show_file proc near

loc_start_show_file:
                mov word ptr [Show_Byte_Count], ax

                mov si, offset File_Buffer
                 
           	mov cx, word ptr [Page_Number]
                jmp short pass_show_wait_for_key

loop_show_file_char:
                or cx, cx
                jnz short pass_show_wait_for_key

		xor ah, ah
		int 16h
		cmp al, 1Bh
		jne short loop_show_file_next

                stc
                jmp short end_of_show_file

loop_show_file_next:
                mov cx, 20
		mov word ptr [Page_Number], cx
pass_show_wait_for_key:
		;mov al, byte ptr [SI]
                ;inc si
                lodsb 
		cmp al, 0Dh
		jne short pass_show_dec_cx
		dec cx
pass_show_dec_cx:
		cmp al, 09h
		jne short pass_put_tab_space
		mov al, 20h
pass_put_tab_space:
                mov ah, 0Eh
                mov bx, 07h
		int 10h

		dec word ptr [Show_Byte_Count]
                jnz short loop_show_file_char    

end_of_show_file:
                mov word ptr [Page_Number], cx
                retn 

proc_show_file  endp

proc_move_file_pointer proc near
                mov ah, 42h
                mov bx, word ptr [File_Handle]
                mov al, byte ptr [MFP]

                cmp al, 0
                jna short pass_mpf_dec_mode_number

loc_mpf_dec_mode_number:
                xor cx, cx
                xor dx, dx
                cmp al, 2
                jnb short pass_mpf_offset_from_cpos
                inc dx
                inc byte ptr [MFP]
                int 21h
                
                retn


pass_mpf_offset_from_cpos:
                dec cx
                dec dx
                sub dx, 2
pass_mpf_offset_from_cpos_2:  
                mov byte ptr [MFP], 0
                int 21h

                retn                  

pass_mpf_dec_mode_number:
                inc byte ptr [MFP]
                inc word ptr [MFPN]
                mov dx, word ptr [MFPN]
                xor cx, cx
                int 21h 
                retn
                  
MFP: db 0
MFPN: dw 0

proc_move_file_pointer  endp

;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
;  data
;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Error_Number_Str:
                db "Error Number: "
Error_Number:
		dd  0
                db "h"
		db  0Dh, 0Ah, 0

File_Read_Error_Str:
                db 'File Read Error !' 
Next_Line:
		db  0Dh, 0Ah, 0

Show_Byte_Count: dw 0

Page_Number:    dw 0

File_Handle:    dw 0

File_Buffer:    db 1 dup (0)

FBuffer_Size    equ $ - offset File_Buffer
                db 0 
                
              
SINGLIXBOOT     ends

                end     START_CODE
