; ****************************************************************************
;
; SHELL0.ASM
; ----------------------------------------------------------------------------
;
; RETRO UNIX 8086 (Retro Unix == Turkish Rational Unix)
; Operating System Project (v0.1) by ERDOGAN TAN (Beginning: 11/07/2012) 
; Retro UNIX 8086 v1 Kernel - /etc/init and /bin/sh file executing test
;
; [ Last Modification: 23/10/2013 ]
;
; ****************************************************************************

.8086

UNIX   	SEGMENT PUBLIC 'CODE'
        assume cs:UNIX,ds:UNIX,es:UNIX,ss:UNIX

START_CODE:
	mov si, offset msg_unix_sh
	call print_msg
wtty1:
        mov bx, 1 		; standard output file (tty)
	mov cx, offset prompt 	; buffer
	mov dx, sizeofprompt	; byte count
	mov ax, 4		; syswrite
	int 20h	
        jc error

	or ax, ax
        jz error 
rtty1:
	mov al, byte ptr [typedchar]
	mov byte ptr [prevtypedchar], al	
   	mov bx, 0 		 ; standard input file (tty)
	mov cx, offset typedchar ; buffer
	mov dx, 1		 ; byte count
	mov ax, 3		 ; sysread
	int 20h	
	jc short error

	or ax, ax
	jz short error				

	mov al, byte ptr [typedchar]
	
	cmp al, 1Bh
	je short sh_ok	
	

	cmp al, 20h
	jnb short wtty2

	mov byte ptr [typedchar], 7 ; beep

	cmp al, 0Dh
	je short @f
wtty2:
	mov bx, 1 		 ; standard output (tty)
	mov cx, offset typedchar ; buffer
	mov dx, 1		 ; byte count
	mov ax, 4		 ; syswrite
	int 20h	
	jc short error

	or ax, ax
	jz short error	
				
	jmp short rtty1
@@:
	cmp byte ptr [prevtypedchar], 20h
	jna short sh_ok
wtty3:
	mov bx, 1 	            ; standard output (tty) 
	mov cx, offset msg_not_found ; buffer
	mov dx, sizeof_msg_nf	    ; byte count
	mov ax, 4		    ; syswrite
	int 20h	
	jc short error

	or ax, ax
	jz short error	

sh_ok:
        mov bx, 1               ; standard output (tty
        mov cx, offset msg_ok   ; buffer
        mov dx, msg_ok_size     ; byte count
        mov ax, 4               ; syswrite
	int 20h		
@@:    
	mov bx, 0 		 ; standard input file (tty)
	mov cx, offset typedchar ; buffer
	mov dx, 1		 ; byte count
	mov ax, 3		 ; sysread
	int 20h	
	   
	mov ax, 1 	; 'sysexit'
	int 20h   	; UNIX system call 
		      	; (Terminate process for DOS!)
	hlt

	jmp start_code

;here:	
;	hlt
;	jmp short here

error:
	mov si, offset msg_err
	call print_msg
        jmp short @b
 
print_msg:
	push ds
	xor bx, bx
	mov ds, bx
	mov bx, 462h
	mov bh, byte ptr [BX] ; active display page
	pop ds
	mov ah, 0Eh                  
        mov bl, 07h  
@@:
        lodsb                     ; Load byte at DS:SI to AL
        and al,al            
        jz short @f      
            
        int 10h                   ; BIOS Service func ( ah ) = 0Eh
                                  ; Write char as TTY
                                  ;AL-char BH-page BL-color
	jmp short @b     
@@:
        retn

;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
;  messages
;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

msg_unix_sh:	db 0Dh, 0Ah
		db 'Retro Unix 8086 v1 - shell'
		db 0
		
prompt:		db 0Dh, 0Ah
		db '# '
sizeofprompt equ $ - offset prompt

msg_err:
		db 0Dh, 0Ah 
                db 'Error ! '
		db 0Dh, 0Ah, 0
msg_not_found:
		db 0Dh, 0Ah
		db 'not found !'
		db 0Dh, 0Ah
sizeof_msg_nf 	equ $ - msg_not_found	
		db 0
typedchar:	
		db 0
prevtypedchar:
		db 0
msg_ok:	
		db 0Dh, 0Ah
		db 'OK. '
		db 0Dh, 0Ah
msg_ok_size 	equ $ - offset msg_ok 
		db 0

UNIX     	ends

                end     START_CODE
