_TEXT	segment	byte public 'CODE'
DGROUP	group	_TEXT,_DATA,_BSS
	assume	cs:DGROUP,ds:DGROUP,ss:DGROUP
_TEXT	ends

_DATA	segment word public 'DATA'
_DATA	ends

_BSS	segment word public 'BSS'
_BSS	ends

_TEXT	segment	byte public 'CODE'

_htot	proc	near

	push	ds
	mov ax,0c000h
	mov ds,ax
	mov ah,ds:[0ff83h]
	mov byte ptr ds:[0ff81h],01h
	call pro0
	mov byte ptr ds:[0ff81h],42h
	call pro0
	mov byte ptr ds:[0ff81h],84h
	call pro0
	mov byte ptr ds:[0ff81h],0c8h
	call pro0
	mov ds:[0ff83h],ah
	pop ds
	ret

pro0:	mov bx,7ff8h
	mov ds:[0ff83h],al	; bank 0 set
loop0:  mov al,10h
	mov cx,[bx]		; read
	mov ds:[0ff83h],al	; bank 1 set
	xor al,al
	mov [bx],cx		; write
	mov ds:[0ff83h],al	; bank 0 set
	dec bx			; GRAM addr decliment
	dec bx
	jge loop0
	ret
_htot	endp


_ttoh	proc	near

	push	ds
	mov ax,0c000h
	mov ds,ax
	add al,10h
	mov ah,ds:[0ff83h]
	mov byte ptr ds:[0ff81h],01h
	call pro1
	mov byte ptr ds:[0ff81h],42h
	call pro1
	mov byte ptr ds:[0ff81h],84h
	call pro1
	mov byte ptr ds:[0ff81h],0c8h
	call pro1
	mov ds:[0ff83h],ah
	pop ds
	ret

pro1:	mov bx,7ff8h
	mov ds:[0ff83h],al	; bank 1 set
loop1:  xor al,al
	mov cx,[bx]		; read
	mov ds:[0ff83h],al	; bank 0 set
	mov al,10h
	mov [bx],cx		; write
	mov ds:[0ff83h],al	; bank 1 set
	dec bx			; GRAM addr decliment
	dec bx
	jge loop1
	ret

_ttoh	endp


_thchg	proc	near

	push	bp
	push	ds
	push	di
	push	si

	mov ax,0c000h
	mov ds,ax
	mov al,ds:[0ff83h]
	push	ax
	mov byte ptr ds:[0ff81h],01h
	call pro2
	mov byte ptr ds:[0ff81h],42h
	call pro2
	mov byte ptr ds:[0ff81h],84h
	call pro2
	mov byte ptr ds:[0ff81h],0c8h
	call pro2
	pop ax
	mov ds:[0ff83h],al
	pop si
	pop di
	pop ds
	pop bp
	ret

pro2:	mov bx,7ff8h
	xor al,al
	mov ds:[0ff83h],al	; set bank 0
loop2:  mov al,10h
	mov cx,[bx]
	mov di,ds:[bx+2]
	mov ds:[0ff83h],al	; set bank 1
	xor al,al
	mov si,[bx]		; si and bp are buffers of bank 1
	mov bp,ds:[bx+2]	; cx and di are buffers of bank 0
	mov [bx],cx
	mov ds:[bx+2],di
	mov ds:[0ff83h],al	; set bank 0
	mov [bx],si
	mov ds:[bx+2],bp
	mov al,10h
	mov cx,[bx+4]
	mov di,ds:[bx+6]
	mov ds:[0ff83h],al	; set bank 1
	xor al,al
	mov si,[bx+4]
	mov bp,ds:[bx+6]
	mov [bx+4],cx
	mov ds:[bx+6],di
	mov ds:[0ff83h],al	; set bank 0
	mov [bx+4],si
	mov ds:[bx+6],bp
	sub bx,8		; decliment
	jge loop2

	ret	

_thchg	endp


_TEXT	ends

_DATA	segment word public 'DATA'
_DATA	ends

_TEXT	segment	byte public 'CODE'
_TEXT	ends

	public	_htot
	public	_thchg
	public	_ttoh
	end
	