;	     32BIT  DOS  EXTENDER
;
; Written by Adam Seychell 	Wed 8-25-1993
;
.386


CODE32	SEGMENT PARA PUBLIC  USE32
assume	DS:CODE32 , CS:CODE32, ES:CODE32

extrn	CODE32_sel	:word		; Selector Values
extrn	DATA_sel	:word
extrn	VIDEO_sel	:word
extrn	FLAT_CODE32_sel	:word
extrn	FLAT_DATA_sel	:word
extrn	XMS_sel		:word
extrn	BASE_sel	:word
extrn	PSP_sel		:word
extrn	ENVIRONMENT_sel	:word


xms_usage	dd 1000000h	; Requested size of XMS block in bytes.
xms_base	dd ?		; Linear base address of the XMS block.
Base_Segment	dw ?		; Real mode segment value for base memory.
PSP_segment	dw ?		; Real mode segment value for PSP segment.

align 4
count		dd 0
v86_mesg	db ' Message printed in v86 mode !!!!',10,13,36
file_name	db 'C:\Command.COM',0
parmBlock	db 10h dup (0)
welcom_mesg	db 'Now in V86 mode',10,13,36
exit_mesg	db 'back to borring real mode',10,13,36
cunt		db 0

include		stdio.asm
include		macros.386
include		dos32.inc

start32:		; 32 bit code entry point.    DS -> data descripor

        mov	ds,cs:[data_sel]       ;Must load these with valid selectors
	mov	es,[xms_sel]
	mov	fs,[FLAT_DATA_sel]
	mov	gs,[video_sel]
        mov	ss,[data_sel]		; Must setup stack area
	mov	esp,offset pmode_stack
        sti

	mov real_ds,CODE32
        mov	ds,cs:data_sel

        mov ah,7
        call clrscr

        writeln 'Got into protected mode ',1

	write <10,13,' A '>

	mov	edx,xms_usage
        mov ah,0eh
        call hex_dword


        write <' byte extended memory block with base address '>
        mov ah,0eh
        mov	edx,xms_Base
        call hex_dword

	writeln ' was allocated'

	write <10,13,'Start of free base memory '>
        xor	edx,edx


	mov	dx,Base_Segment
        shl	edx,4
        mov ah,0eh
        call hex_dword

        writeln
	write <10,13,'PSP memory address '>
        xor	edx,edx
	mov	dx,PSP_Segment
        shl	edx,4
        mov ah,0eh
        call hex_dword


	writeln
	writeln
	writeln ' Press any key to continue '

        mov ah,0
       dosint 16h



        mov es,xms_sel
        cld

mov	cunt,10
tsm3:


        writeln ' Filling memory ',5fh

        mov es,xms_sel
        mov ecx,xms_usage
        and ecx,ecx
        jz NOT_fill_XMS
        shr ecx,2
        mov edi,0
        cld
	rep stosd
NOT_fill_XMS:
        mov es,base_sel
        mov ecx,0A000h
        sub cx,Base_segment
        shl ecx,2
        mov edi,0
        cld
;	rep stosd

        dec cunt
        jnz tsm3

        open 1,'example2.exe'
        jnc opok
        writeln ' error openning file '
        jmp exit
opok:
        writeln ' reading file '
        blockread 1,0B800h,00,1300
        close 1

	mov	word ptr [real_DS],CODE32
dorr:
skipo:

	sti
        mov	ax,psp_segment		; Resize memory block
	mov	word ptr [real_ES],ax
        mov	bx,base_segment
        sub	bx,ax
        mov	ah,4ah
	dosint	21h
	jc exit

        mov dx,offset welcom_mesg
        mov ah,9
	dosint 21h

        mov	es,PSP_sel

        mov	ax,es:[2Ch]		
        mov	word ptr parmBlock,ax   	; set environment segment
        mov	bx,psp_segment

        mov	word ptr parmBlock+2,80h	; set command line
        mov	word ptr parmBlock+4,bx


        mov	word ptr parmBlock+6,5Ch	; Set FCB 1
        mov	word ptr parmBlock+8,bx
        mov	word ptr parmBlock+0Ah,6Ch	; Set FCB 2
        mov	word ptr parmBlock+0Ch,bx

        mov	[real_ES],CODE32
        mov	bx,offset parmBlock	; ES:BX > parameter block
        mov	ah,4bh
        mov	al,0
       mov	dx,offset file_name
	dosint	21h


        mov	[real_DS],CODE32

        mov	ah,9
	mov	dx,offset envir_mesg
        dosint	21h
        ;---- print environment -----
        mov	gs,ENVIRONMENT_sel
        xor	edi,edi
	mov	ah,2


print_env:
        mov	dl,gs:[edi]
        inc	edi
        cmp	dl,0
        jnz @er5
	mov dl,10
        dosint	21h
	mov dl,13
        dosint	21h
@er5:
        dosint	21h
        cmp	edi,100h
        jb print_env

	mov dl,13
        dosint	21h
	mov dl,10
        dosint	21h



        mov dx,offset exit_mesg
        mov ah,9

        dosint 21h
exit:


	mov ah,4ch		; Termiate the program
        int 21h
envir_mesg db 10,10,10,10,13,'                     PROGRAM  ENVIRONMENT  ',10,13,10,10,36

; The 16 hardware inetrrupts.
; If an IRQ accours in V86 mode the segment registers will contain same
; selector values as they were before the last V86 call. ( i.e dosint xx ).
IRQ0:
	push ds
        mov	ds,cs:[flat_data_sel]
        inc	word ptr ds:[0b8000h]
	dosint 08h                      ;  8253  Timer 0
        pop ds
	iretd
IRQ1:	dosint 09h			;  8242 Keyboard
	iretd
IRQ2:	dosint 0Ah                      ; LPT 2 or  Vert Retrate (6845)
	iretd
IRQ3:	dosint 0Bh			; Serial communications ports 2 & 4
	iretd
IRQ4:	dosint 0Ch			; Serial communications ports 1 & 3
	iretd
IRQ5:	dosint 0Dh			; Reserved
	iretd
IRQ6:	dosint 0Eh			; NEC æPD765 Floppy Disk Controler
	iretd
IRQ7:	dosint 0Fh                      ; LPT 1
	iretd
IRQ8:	dosint 70h			; 6818 CMOS CLOCK
	iretd
IRQ9:	dosint 71h			; Reserved
	iretd
IRQ10:	dosint 72h			; Reserved
	iretd
IRQ11:	dosint 73h			; Reserved
	iretd
IRQ12:	dosint 74h			; Reserved
	iretd
IRQ13:	dosint 75h			; 80x87 Math Co-Pro exception.
	iretd
IRQ14:	dosint 76h			; Hard Drive Cotroller.
	iretd
IRQ15:	dosint 77h			; Reserved
	iretd
align 4
db 100h dup (?)
pmode_stack:


CODE32 ENDS

END