;[]-------------------------------------------------------------------------[]
;|	Program name: 3_2_5.ASM                                              |
;|	Image   file: None                                                   |
;|	Note        : Load sector data from file.                            |
;|                    List the subroutines only.                             |
;[]-------------------------------------------------------------------------[]


;------- Data area ---------------------

dummy_read	db	0
load_count	dw	0
count		dw	0
reg_buf		dw	800H dup (0FFFFH)
reg_file	db	'REGISTER.DAT',0
sec_file	db	'A_SECTOR.DAT',0
fake_file	db	'A_SECTOR.DAT',0
handle		dw	0

;------- INT 13H -----------------------

;---------------------------------------
; Transfer data by using Old INT 13H
;---------------------------------------

transfer_data	proc	near

	cmp	cs:dummy_read,1		; If dummy_read is set to 1
	je	t1			; then read sector from file

	cli				; else read sector from disk
	pushf
	call	cs:old_int13		; Call old_int13 to access disk
	sti
	jmp	transfer_done

;
; In following routine, you can not use <pushall> and <popall> macro.
; Register AX, BX, CX, DX, and FLAG must be set by the values of reg_buf.
;
t1:
	pushr	<di,ds>			; Save DI, DS value

	mov	ax,cs			; Adjust DS
	mov	ds,ax

	mov	di,offset reg_buf	; DI get the reg_buf start address
	add	di,load_count		; Adjust DI with load_count
	mov	ax,AX_1			; Get the requesting AX value
	cmp	ah,02H			; Is it a read request ?
	jne	t2			; If not, then branch
	call	load_sector		; If yes, then load data from file
t2:
	add	load_count,BLOCK * 2	; Adjust load_count with Block size
	mov	ax,load_count		; Get the adjusted load_count value
	cmp	ax,count		; Does it larger then count ?
	jb	t3			; If not, then branch
	mov	load_count,0		; If yes, reset load_count to zero
t3:
	mov	ax,FLAG_2		; Get new FLAG value from reg_buf
	push	ax			; Push it to stack
	popf				; Pop it from stack to FLAG
	mov	ax,AX_2			; Get new AX value from reg_buf
	mov	bx,BX_2			; Get new BX value from reg_buf
	mov	cx,CX_2			; Get new CX value from reg_buf
	mov	dx,DX_2			; Get new DX value from reg_buf

	popr	<ds,di>			; Restore DS, DI value

transfer_done:

	ret				; Return

transfer_data	endp

;------- INT 13H Subroutine ------------

;---------------------------------------
; Load sector data from file.
;---------------------------------------
; In this routine, ds:di must point to current block in reg_buf.

load_sector	proc	near

	mov	ax,NAMES		; Get loading file name
	mov	word ptr fake_file,ax	; Put it to fake_file

	h_open	fake_file,RDWR		; Open fake_file

	push	ds			; Save DS value
	mov	dx,BX_1			; Let DX get buffer offset
	mov	cx,BYTES		; Let CX get loading bytes number
	mov	bx,handle		; Let BX get handle
	mov	ax,es			; Get buffer segment
	mov	ds,ax			; Put it to DS
	mov	ah,3FH			; Read data
	int	21H			; Execute
	pop	ds			; Restore DS value

	h_close	handle			; Close fake_file

	ret				; Return

load_sector	endp

;---------------------------------------
