; ****************************************************************************
; TRDOS386.ASM (TRDOS 386 Kernel - v2.0.0) - SYS INIT : trdosk1.s
; ----------------------------------------------------------------------------
; Last Update: 30/01/2016
; ----------------------------------------------------------------------------
; Beginning: 04/01/2016
; ----------------------------------------------------------------------------
; Assembler: NASM version 2.11 (trdos386.s)
; ----------------------------------------------------------------------------
; Derived from TRDOS Operating System v1.0 (8086) source code by Erdogan Tan
; TRDOS2.ASM (09/11/2011)
; ****************************************************************************
; TRDOS2.ASM (c) 2004-2011 Erdogan TAN [ 17/01/2004 ] Last Update: 09/11/2011
;

sys_init:
	; 24/01/2016
	; 06/01/2016
	; 04/01/2016

	mov	eax, '?:/'
	mov	[Current_Dir_Drv], eax

	; Logical DRV INIT (only for hard disks)
	call 	ldrv_init  ; trdosk2.s
	
	; When floppy_drv_init call is disabled
	; media changed sign is needed
	; for proper drive initialization
        
	mov 	esi, Logical_DOSDisks
	mov 	al, 1 ; Initialization sign (invalid_fd_parameter)
	add 	esi, LD_MediaChanged ; Media Change Status = 1 (init needed)
	mov 	[esi], al ; A:
	add 	esi, 100h 
	mov 	[esi], al ; B: 
           
_current_drive_bootdisk:
	mov 	dl, [boot_drv] ; physical drive number
	cmp 	dl, 0FFh
	je 	short _last_dos_diskno_check
_boot_drive_check:
	cmp 	dl, 80h
	jb 	short _current_drive_a
	sub 	dl, 7Eh ; C = 2 , D = 3
	jmp 	short _current_drive_a 

_last_dos_diskno_check:
	mov 	dl, [Last_DOS_DiskNo]
	cmp 	dl, 2
	ja 	short _current_drive_c
	je 	short _current_drive_a
	xor 	dl, dl ; A:
	jmp 	short _current_drive_a

_current_drive_c:
	mov 	dl, 2 ; C:

_current_drive_a:
	mov	[drv], dl
        mov     esi, msg_CRLF_temp
	call 	print_msg

	mov	dl, [drv]
	call 	change_current_drive
	jnc 	short _start_mainprog

_drv_not_ready_error: 
	mov 	esi, msgl_drv_not_ready
	call 	print_msg
	jmp 	short _end_of_mainprog

_start_mainprog:
        call    dos_prompt
              
_end_of_mainprog:
        mov     esi, msg_CRLF_temp
	call 	print_msg
	mov 	esi, mainprog_Version
	call 	print_msg
	; 24/01/2016
	sub	ah, ah
	call	int16h ; call getch
	jmp	cpu_reset

infinitiveloop: jmp short infinitiveloop


print_msg:
	; 04/01/2016
	; 01/07/2015
	; 13/03/2015 (Retro UNIX 386 v1)
	; 07/03/2014 (Retro UNIX 8086 v1)
	; (Modified registers: EAX, EBX, ECX, EDX, ESI, EDI)
	;
	lodsb
pmsg1:
	push 	esi
	movzx	ebx, byte [ACTIVE_PAGE] ; 04/01/2016 (ptty)
	mov	ah, 07h ; Black background, light gray forecolor
	call 	WRITE_TTY
	pop	esi
	lodsb
	and 	al, al
	jnz 	short pmsg1
	retn

clear_screen:
	; 30/01/2016
	; 24/01/2016
	; 04/01/2016
	movzx	ebx, byte [ACTIVE_PAGE] ; video page number (0 to 7)
	mov 	ah, [ebx+vmode] ; default = 03h (80x25 text)
	cmp	ah, 4
	jb	short cls1
	cmp	ah, 7
	jne	short vga_clear
cls1:
	cmp	ah, [CRT_MODE] ; current video mode ? 
	;je	short cls2 ; yes (current video mode = 3)
	;;call	set_mode_3 ; set video mode to 3 (& clear screen)
	;;retn
	;jmp	set_mode_3
	jne	set_mode_3
cls2:
	mov	ah, 07h ; attribute to be used on blanked line
	sub 	al, al ; 0 =  entire window
	xor 	cx, cx
	mov 	dx, 184Fh
	call	_scroll_up ; 24/01/2016
	;
	;mov	bl, [ACTIVE_PAGE] ; video page number (0 to 7)
	xor 	dx, dx
	call	_set_cpos ; 24/01/2016 
	;retn
vga_clear:
	retn	

panic:
	; 13/03/2015 (Retro UNIX 386 v1)
	; 07/03/2014 (Retro UNIX 8086 v1)
	mov 	esi, panic_msg
	call 	print_msg
key_to_reboot:
        ; 24/01/2016
        sub     ah, ah
        call    int16h ; call   getch
        ; wait for a character from the current tty
	;
	mov	al, 0Ah
	mov	bl, [ptty] ; [ACTIVE_PAGE]
	mov	ah, 07h ; Black background, 
			; light gray forecolor
	call 	WRITE_TTY
	jmp	cpu_reset 

ctrlbrk:
	; 12/11/2015
	; 13/03/2015 (Retro UNIX 386 v1)
	; 06/12/2013 (Retro UNIX 8086 v1)
	;
	; INT 1Bh (control+break) handler		
	;
      	; Retro Unix 8086 v1 feature only!
      	;
	cmp 	word [u.intr], 0
	jna 	short cbrk4
cbrk0:
	; 12/11/2015
	; 06/12/2013
	cmp 	word [u.quit], 0
	jz	short cbrk4
	;
	; 20/09/2013	
	push 	ax
	mov	al, [ptty]
	;
	; 12/11/2015
	;
	; ctrl+break (EOT, CTRL+D) from serial port
	; or ctrl+break from console (pseudo) tty
	; (!redirection!)
	;
	cmp	al, 8 ; serial port tty nums > 7
        jb      short cbrk1 ; console (pseudo) tty
	;	
	; Serial port interrupt handler sets [ptty]
	; to the port's tty number (as temporary).
	;
	; If active process is using a stdin or 
	; stdout redirection (by the shell),
        ; console tty keyboard must be available
	; to terminate running process,
	; in order to prevent a deadlock. 
	;
	push	edx
	movzx	edx, byte [u.uno]
	cmp     al, [edx+p.ttyc-1] ; console tty (rw)
	pop	edx
	je	short cbrk2
cbrk1:
	inc 	al  ; [u.ttyp] : 1 based tty number
	; 06/12/2013
	cmp	al, [u.ttyp] ; recent open tty (r)
	je	short cbrk2	
        cmp     al, [u.ttyp+1] ; recent open tty (w)
	jne	short cbrk3	
cbrk2:
	;; 06/12/2013
	;mov	ax, [u.quit]
	;and	ax, ax
	;jz	short cbrk3
	;
	xor	ax, ax ; 0
	dec	ax
	; 0FFFFh = 'ctrl+brk' keystroke
	mov	[u.quit], ax
cbrk3:
	pop	ax
cbrk4:
	retn
