summaryrefslogtreecommitdiffstats
path: root/private/mvdm/wow16/win87em/emwin.asm
blob: adadcb265b1095e3293f935dde43cd8ef57bfb98 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
	page	,132
	subttl	emwin.asm - Initialization and Termination for Windows
;***
;emwin.asm - Initialization and Termination for Windows
;
;	Copyright (c) 1987-89, Microsoft Corporation
;
;Purpose:
;	Initialization and Termination for Windows
;
;	This Module contains Proprietary Information of Microsoft
;	Corporation and should be treated as Confidential.
;
;Revision History:
;	See emulator.hst
;
;*******************************************************************************


comment !

Windows/DOS interfaces to emulator/8087 functions

Certain emulator/8087 functions are performed by calling __fpmath
with an function code and arguments.

__fpmath	general floating point	math  package  interface  used
		by the emulator/8087 and float calls interfaces.  This
		is a far routine and must be far called.

entry:

  bx = 0	initialize floating point math (program startup)
		dx, ax, si input values ignored in WINDOWS emulator
		returns:
		  ax = 0 if successful and using software floating point
		       1 if successful and using 8087
		  just terminates process if error

  bx = 1	reset (FINIT) - finit ok even under WINDOWS;
		typical usage will load control word afterward
		so other tasks won't be hindered (as with _fpreset() call)

  bx = 2	terminate floating point math (program termination)

  bx = 3	set error signal address
		dx:ax = segment:offset of user error handler

  bx = 4	load user control word
		(user should not use FLDCW instruction directly)
		ax = user control word value

  bx = 5	store user control word
		returns:
		  ax = user control word value

  bx = 6	truncate TOS to integer TOS
		ax = user control word (only use round mode)

  bx = 7	truncate TOS to 32-bit integer in DX:AX
		ax = user control word (only use round mode)

  bx = 8	store user status word
		returns:
		  ax = user status word value

  bx = 9	clear exceptions

  bx = 10	return number of stack elements in ax

  bx = 11	returns 1 if using 80x87, 0 if not

  bx = 12	if ax = 0, turn off extended stack. if ax = 1, turn on e.s.

! 


glb	<functab>

functab label	word

	dw	initialization		; 0 - initialize emulator/8087
	dw	reset			; 1 - reset emulator/8087 stack
	dw	termination		; 2 - terminate emulator/8087
	dw	setsignal		; 3 - set error signal address
	dw	loadcontrolword 	; 4 - load user control word
	dw	storecontrolword	; 5 - store user control word
	dw	truncateTOS		; 6 - truncate TOS to integer TOS
	dw	truncateTOSto32int	; 7 - truncate TOS to integer in DX:AX
	dw	storestatusword 	; 8 - store user status word
	dw	clearexceptions 	; 9 - clear execeptions
	dw	NumStack		; 10 - report number of elements in stack
	dw	ReturnHave8087		; 11 - report if using coprocessor
	dw	SetExtendedStack	; 12 - turn on or off extended stack
endfunc label	word

SizeJmpTab  equ    12

sEnd  ecode


sBegin	ecode

assumes cs, ecode
assumes ds, edata



public	__fpmath

__fpmath  proc	far

	cmp	bx, SizeJmpTab
	ja	RetFPErr

	shl	bx, 1
	push	ds			; save DS

	mov	cx, EMULATOR_DATA
	mov	ds, cx

	call	functab[bx]

	pop	ds			; restore DS

EmuRet:
	ret


RetFPErr:
	or	ax, -1
	cwd
	jmp	EmuRet

__fpmath  endp




subttl	emwin.asm - Initialization and Termination
page
;*********************************************************************;
;								      ;
;		      Initialization and Termination		      ;
;								      ;
;*********************************************************************;

wastetime macro
	push	cx
	mov	cx,20			;; wait for a short time
	loop	$
	pop	cx
endm



;	program initialization
;
;	entry	dx:ax = task data area (segment and size) for standalone
;		si = DOS environment segment for NO87 lookup ???
;	DX,AX,SI - ignored in WINDOWS case
;	these register inputs are ignored for Windows app
;		program-time initialization

pub  initialization			; all initialization is done when loaded

ifdef WF
	cmp	[Installed],0
	jnz	@F
	.286p
	push	0
	call	AllocSelector
	mov	[wfSel], ax		; Error checking??
	mov	ax, __WINFLAGS
;	int 3
	test	ax, WF_WIN386
	jz	wfSlow1
	cmp	[Have8087], 0
	je	wfSlow1
	or	[wfGoFast], 1		; We can use fast if Enh Mode & FPU
wfSlow1:
@@:
endif
	inc	[Installed]		; Installed will count number of apps
					; using the emulator.

	cmp	[Have8087], 0		; check for 8087/80287
	je	NoInstall87

	extrn	__FPINSTALL87:near
	call	__FPINSTALL87		; set NMI (int 2) for this instance
NoInstall87:

	call	reset
	xor	ax, ax

	ret




ifdef	standalone
	mov	di,offset BEGstk	; di = base of register stack
	mov	[BASstk],di		; initialize stack base
	mov	cx,Reg87Len		; cx = register length
	xchg	ax,dx			; ax = task data segment size
	sub	ax,di			; ax = number bytes for stack
	cwd				; dx:ax = number of bytes
	div	cx			; ax = number of entries
	mul	cx			; ax = number of bytes
	add	ax,di			; ax = top of stack
	sub	ax,cx			; Leave room for one on overflow
	mov	[LIMstk],ax		; set top of stack
endif	;standalone



; check if floating point emulator/8087 already installed (device driver)



    ; load time initialization

pub  LoadTimeInit

	push	di
	push	si
	push	ds
	mov	ax,EMULATOR_DATA
	mov	ds,ax

	mov	ax, __WINFLAGS
	and	ax, WF_80x87
	cmp	ax, WF_80x87
	jz	WinHave87

ifdef only87
	jmp	loadiniterrorret
endif
	jmp	WinSet87

pub WinHave87
	mov	al,1

pub WinSet87
	mov	[Have8087],al

    ; real mode emulation and fixup on the fly vector setup

pub  initvec
	call	SaveVectors
	call	SetVectors


pub  loadinitfinish


    ; finish initialization

pub  initfinish

	mov	[Installed], 0		; Installed will count number of apps

	call	reset			; reset (0), FINIT if 8087 present and
					; set up default control word

	mov	ax, 1			; return non zero result

pub  loadiniterrorret

	pop	ds
	pop	si
	pop	di
	retf				 ; far return for dynalink lib entry pt.


;*
;*  DLL termination routine.
;*

public	WEP
WEP  label  far

	push	ds
	push	ax

	push	si
	push	di

	mov	ax,EMULATOR_DATA
	mov	ds,ax

	call	reset
	call	RestoreVectors

	pop	di
	pop	si
	pop	ax
	pop	ds

	retf	2	    ; WEP functions are called with a word paramater.


;------ program termination ----------------------------------------------------

pub termination

	call	reset			; reset chip for other apps

	dec	[Installed]		; if Installed is not 0, someone is
	jnz	termrealdone		; still using the emulator.

ifdef WF
	xor	ax, ax
	xchg	ax, [wfSel]
	or	ax, ax
	jz	@F
	push	ax
	call	FreeSelector
@@:
endif

ifndef	only87
	cmp	[Have8087],0		; Non zero if 8087 chip exists
	je	termrealdone
endif	;only87

	extrn	__FPTERMINATE87:near   ; reset NMI (int 2) for this instance
	call	__FPTERMINATE87

pub termrealdone
	ret




subttl	emwin.asm - reset and clearexceptions
page
;*********************************************************************;
;								      ;
;		     Reset and Clearexceptions			      ;
;								      ;
;*********************************************************************;

pub reset

ifndef	only87
	cmp    [Have8087],0		; Nonzero if 8087 chip exists
	je     NoFINIT
endif	;only87
	fninit
	fwait				; Workaround for 80387 bug.
	fninit

pub NoFINIT
	mov	ax, [BASstk]
	mov	[CURstk], ax		; reset stack to bottom

	mov	ax, InitControlWord	; setup initial control word
	call	loadcontrolword


    ; fall into clearexceptions


pub  clearexceptions

	xor	ax, ax
ifndef	only87
	cmp	al, [Have8087]		; Nonzero if 8087 chip exists
	je	NoFCLEX
endif	;only87
	fclex				; clear exceptions

pub NoFCLEX
ifndef	only87
	mov	[StatusWord], ax	; clear status word
endif	;only87
	mov	[UserStatusWord], ax	; clear exception status word

	ret



subttl	emwin.asm - setsignal ---------------------------------
page
;*********************************************************************;
;								      ;
;		     Setsignal					      ;
;								      ;
;*********************************************************************;


pub  setsignal

	push	ds

	mov	ds, dx			; set TSKINT to SignalAddress
	mov	dx, ax
	mov	ax, 25h*256 + TSKINT
	IntDOS

	pop	ds
	ret


pub  SaveVectors

	mov	cx, NUMVEC		; save old vectors under DOS 3
	mov	ax, 35h*256 + BEGINT	; get vector
	mov	di, offset oldvec	; di = old vector table

pub getvecs
	IntDOS
	inc	ax
	mov	[di], bx		; save old vector
	mov	[di+2], es
	add	di, 4
	loop	getvecs

	ret

pub  SetVectors

ifndef	only87
	mov	dx, offset DStrap	; assume emulator
	mov	si, offset SOtrap
	mov	di, offset FWtrap
ifdef  WINDOWS
	mov	ax, __WINFLAGS		  ; if we are in PMODE & win386 increment all of
	and	ax, WF_PMODE or WF_WIN386 ; the handler address past the "sti".
	cmp	ax, WF_PMODE or WF_WIN386
	jne	NotPmode1

	inc	dx
	inc	si
	inc	di
lab NotPmode1
endif	;WINDOWS

	cmp	[Have8087], 0		; are we using 8087 ?
	jz	SetEmVecs		;    no - go ahead and set them
endif	;only87

	mov	dx, offset DSFixUpOnFly ; set up for fixup-on-the-fly
	mov	si, offset SOFixUpOnFly
	mov	di, offset FWFixUpOnFly
ifdef  WINDOWS
	mov	ax, __WINFLAGS		  ; if we are in PMODE & win386 increment all of
	and	ax, WF_PMODE or WF_WIN386 ; the handler address past the "sti".
	cmp	ax, WF_PMODE or WF_WIN386
	jne	NotPmode2

	inc	dx
	inc	si
	inc	di
lab NotPmode2
endif	;WINDOWS

pub  SetEmVecs
	push	ds

	push	cs
	pop	ds
	mov	ax, 25h*256 + BEGINT
	mov	cx, 8			; 8 vectors for DStrap

pub  SetDSLoop
	IntDOS				; set vector
	inc	ax			; bump to next one
	loop	SetDSLoop

	mov	dx, si			; set Segtrap
	IntDOS
	inc	ax
	mov	dx, di			; set FWtrap
	IntDOS

	pop	ds			; restore task data area

	ret


pub  RestoreVectors

	mov	cx, NUMVEC
	mov	ax, 25h*256 + BEGINT	; Dos set vector.
	mov	di, offset oldvec	; di = old vector table

pub  ResetVecLoop
	push	ds
	lds	dx, [di]		; get old vector value
	IntDOS
	pop	ds
	inc	ax
	add	di,4

	loop	ResetVecLoop

	ret



pub  NumStack			; returns the number of stack elements in ax

	xor	dx, dx		; dx will count nonzero elements

ifndef	only87
	cmp	Have8087, 0
	je	CountEmulatorStack
endif	;only87

	sub	sp, 14		; need 14 bytes for fstenv
	mov	bx, sp
	fstenv	ss:[bx]
	fldcw	ss:[bx] 	; reset control word
	mov	ax, ss:[bx+4]	; put tag word in ax
	add	sp, 14		; reset stack

	mov	cx, 8
pub NotEmptyLoop
	mov	bx, ax

	shr	ax, 1
	shr	ax, 1

	and	bx, 3
	cmp	bx, 3
	je	StackEntryEmpty

	inc	dx		; stack element was not empty
pub StackEntryEmpty
	loop	NotEmptyLoop


pub CountEmulatorStack

	mov	ax, CURstk
	sub	ax, BASstk

	mov	bl, Reg87Len

	div	bl

	add	ax, dx		; add elements on 80x87 stack

	ret


ReturnHave8087 proc near

	mov	al, [Have8087]
	cbw

	ret
ReturnHave8087 endp


SetExtendedStack proc near

	mov	[ExtendStack], ax

	ret
SetExtendedStack endp



;***
;int far pascal __Win87EmInfo( WinInfoStruct far * p, int cb );
;
;Purpose:
;   returns information about win87em.exe to CodeView
;
;Entry:
;   WinInfoStruct far * p
;   int cb  - size of WinInfoStruct
;
;Exit:
;   returns non zero if error.
;
;
;Uses:
;
;Exceptions:
;
;*******************************************************************************


cProc	__WIN87EMINFO,<PUBLIC,FAR,PLM>,<ds>

	parmD	p
	parmW	cb

cBegin
	or	ax, -1
	cmp	[cb], size WinInfoStruct
	jb	WIDone

	mov	ax, edataBASE
	mov	es, ax
	assumes es, edata

	lds	bx, [p]
	assumes ds, nothing

	mov	[bx.WI_Version], (major_ver shl 8) +  minor_ver
	mov	[bx.WI_SizeSaveArea], Size80x87Area + edataOFFSET __fptaskdata
	mov	[bx.WI_WinDataSeg], es
	mov	[bx.WI_WinCodeSeg], cs

	mov	al, [Have8087]
	cbw
	mov	[bx.WI_Have80x87], ax
	assumes es, nothing

	xor	ax, ax			    ; return 0 if no error
	mov	[bx.WI_Unused], ax
WIDone:
cEnd


;***
;int far pascal __Win87EmSave( void far * p, int cb );
;
;Purpose:
;   saves win87em.exe info in p
;
;Entry:
;   void far * p    - pointer to save area.
;   int cb	    - size of save area.
;
;Exit:
;   returns non zero if error.
;
;Uses:
;
;Exceptions:
;
;*******************************************************************************


cProc	__WIN87EMSAVE,<PUBLIC,FAR,PLM>,<ds,si,di>

	parmD	p
	parmW	cb

cBegin
	or	ax, -1
	cmp	[cb], Size80x87Area + edataOFFSET __fptaskdata
	jb	WSDone

	mov	ax, edataBASE
	mov	ds, ax
assumes  ds, edata

	les	di, [p]
assumes  es, nothing

	cmp	[Have8087], 0
	je	NoSave80x87

	fsave	es:[di.WSA_Save80x87]
NoSave80x87:

	add	di, (WSA_SaveEm - WSA_Save80x87)
	xor	si, si
	mov	cx, edataOFFSET __fptaskdata
	shr	cx, 1

	rep movsw

	jnc	NoSaveLastByte
	movsb
NoSaveLastByte:

	xor	ax, ax		; return 0 if no error.

WSDone:
cEnd



;***
;int far pascal __Win87EmRestore( void far * p, int cb );
;
;Purpose:
;   retores win87em.exe info from p
;
;Entry:
;   void far * p    - pointer to save area.
;   int cb	    - size of save area.
;
;Exit:
;   returns non zero if error.
;
;Uses:
;
;Exceptions:
;
;*******************************************************************************


cProc	__WIN87EMRESTORE,<PUBLIC,FAR,PLM>,<ds,si,di>

	parmD	p
	parmW	cb

cBegin
	or	ax, -1
	cmp	[cb], Size80x87Area + edataOFFSET __fptaskdata
	jb	WRDone

	mov	ax, edataBASE
	mov	es, ax
assumes  es, edata

	lds	si, [p]
assumes  ds, nothing

	add	si, (WSA_SaveEm - WSA_Save80x87)
	xor	di, di
	mov	cx, edataOFFSET __fptaskdata
	shr	cx, 1

	rep movsw

	jnc	NoRestoreLastByte
	movsb
NoRestoreLastByte:

	mov	si, [OFF_p]	    ; reset source pointer.

	cmp	[Have8087], 0
	je	NoRestore80x87

	frstor	[si.WSA_Save80x87]
NoRestore80x87:

	xor	ax, ax		; return 0 if no error.

WRDone:
cEnd

assumes ds, edata
assumes es, nothing