; ****************************************************************************
; CDIR.ASM (by Erdogan Tan)
;
; TRDOS Kernel Test program for
; INT 21h Function 47h, Get Current Directory
;
; 27/03/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_get_cdir_int21h  ; jump if not zero

                mov dl, 0
                jmp short loc_get_cdir_int21h_fcall

loc_get_cdir_int21h:
                inc si
                mov byte ptr [SI][BX], bh     ; 0 
loc_get_cdir_int21h_next:
                inc si
                mov dl, byte ptr [SI]
                cmp dl, 20h                   ; is it SPACE ?
                jb short loc_get_cdir_int21h_no_arg
                je short loc_get_cdir_int21h_next_dec_bl
                mov al, byte ptr [SI]+1
                cmp al, 20h
                jna short loc_get_cdir_int21h_check_drv_name
                cmp al, ':'
                jne short loc_get_cdir_int21h_fcall_error
                cmp byte ptr [SI]+2, 20h
                ja short short loc_get_cdir_int21h_fcall_error
loc_get_cdir_int21h_check_drv_name:
                and dl, 0DFh
                cmp dl, 'Z'
                ja short loc_get_cdir_int21h_fcall_error
                cmp dl, 'A'
                jb short loc_get_cdir_int21h_fcall_error
                sub dl, 'A'
                inc dl
                jmp short loc_get_cdir_int21h_fcall 

loc_get_cdir_int21h_next_dec_bl:
                dec bl                                  
                jnz short loc_get_cdir_int21h_next

loc_get_cdir_int21h_no_arg:
                mov dl, 0

loc_get_cdir_int21h_fcall:
                mov si, offset Current_Directory
                
                mov ah, 47h ; Get Current Directory
                int 21h
                jnc short loc_get_cdir_int21h_fcall_no_error

loc_get_cdir_int21h_fcall_error:
                mov si, offset Error
                call PROC_PRINTMSG
                int 20h

loc_get_cdir_int21h_fcall_no_error:
                or dl, dl
                jz loc_get_cdir_int21h_fcall_get_dos_version

loc_get_cdir_int21h_fcall_save_drive_name:               
                dec dl 
                add dl, 'A'
                mov dh, ':'
                mov word ptr [Drive], dx
                xor al, al
                mov byte ptr [Drive]+2, al   

loc_get_cdir_int21h_fcall_get_dos_version:  
                mov ah, 30h ; Get MSDOS (TRDOS) version
                int 21h
               ; 27/03/2011 TRDOS 1.0 check 
                cmp ax, 10  ; AX = 10 -> Trdos 1.0
                je short loc_get_cdir_int21h_fcall_trdos_ok

                mov si, offset CDIR_Root_Slash
                mov al, '\' ; Msdos directory separator sign
                mov byte ptr [SI], al   

loc_get_cdir_int21h_fcall_trdos_ok:
                mov si, offset Drive_Header
                call PROC_PRINTMSG

                mov si, offset CDir_Header
                call PROC_PRINTMSG

                mov si, offset Next_Line
                call PROC_PRINTMSG

                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


;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
;  data
;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Error:          db 'Drive not ready error !', 0Dh, 0Ah, 0 
        

Drive_Header:
                db "Drive : "
Drive:
		db 'Current Drive'
                db 0
CDir_Header:
		db  0Dh, 0Ah
                db 'Current Directory : '
CDIR_Root_Slash: db '/'
Current_Directory:
                db 92 dup(0) 
Next_Line:
		db  0Dh, 0Ah, 0

           
SINGLIXBOOT     ends

                end     START_CODE
