summaryrefslogtreecommitdiffstats
path: root/private/mvdm/dpmi/dxbug.asm
blob: 83e39c7d2bdd16ccc353dc7390d0e1a5cdfeaa39 (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
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
        PAGE    ,132
        TITLE   DXBUG.ASM -- Dos Extender Debug Services

; Copyright (c) Microsoft Corporation 1989-1991. All Rights Reserved.

;****************************************************************
;*                                                              *
;*      DXBUG.ASM      -   Dos Extender Debug Services          *
;*                                                              *
;****************************************************************
;*                                                              *
;*  Module Description:                                         *
;*                                                              *
;*  This module contains protect mode debug services for the    *
;*  DOS extender.                                               *
;*                                                              *
;****************************************************************
;*  Revision History:                                           *
;*                                                              *
;*  11/17/89 Jimmat     Added TraceBug stuff for debugging on   *
;*                      286 machines.                           *
;*  03/09/89 JimMat     Initial version.                        *
;*                                                              *
;****************************************************************

.286p
.287

ifndef DEBUG
DEBUG   =       0
endif


; -------------------------------------------------------
;           INCLUDE FILE DEFINITIONS
; -------------------------------------------------------

include     segdefs.inc
include     gendefs.inc
include     pmdefs.inc

if DEBUG   ;********** ENTIRE FILE IS DEBUG CODE ***********
; -------------------------------------------------------
;           GENERAL SYMBOL DEFINITIONS
; -------------------------------------------------------

Debug_Serv_Int  equ     41h             ;WDEB386 service codes
DS_Out_Char     equ     0
DS_Out_Symbol   equ     0fh


; -------------------------------------------------------
;           DATA SEGMENT DEFINITIONS
; -------------------------------------------------------

DXDATA  segment

        extrn   fDebug:BYTE
        extrn   fTraceBug:WORD
        extrn   idCpuType:WORD

DXDATA  ends

; -------------------------------------------------------
        page

DXCODE  segment
        assume cs:DXCODE

; -------------------------------------------------------
; PMDebugInt -- Process (display) the pMode debugger interrupt calls
;

        public  PMDebugInt
        assume  ds:NOTHING,es:NOTHING,ss:NOTHING

PMDebugInt      proc    near

        cmp     al,4Fh                  ;debugger installation check?
        jnz     notInsChk
        mov     ax,0F386h               ;say we're here...
        jmp     DebugIntExit

notInsChk:
        cmp     al,50h                  ;load segment call?
        jnz     notLoadSeg

        push    ax
        mov     ax,es
        Trace_Out "Int 41h Load code ->@AX:DI<- ",x
        pop     ax
        test    si,1
        jz      LoadCode
        Trace_Out "data ",x
        jmp     short @f
LoadCode:
        Trace_Out "code ",x
@@:
        Trace_Out "seg #BX  sel #CX  inst #DX"
        jmp     short DebugIntExit
notLoadSeg:

DebugIntExit:

        iret

PMDebugInt      endp


; -------------------------------------------------------
; DXTestDebugIns -- Returns with Z flag set if running
;                   under the debugger, NZ means not
;                   under debugger.
;
;   Input:  none
;   Output: Z - no debugger, NZ - under debugger
;   Uses:   Flags

        public  DXTestDebugIns

DXTestDebugIns  proc    far
        assume  ds:NOTHING,es:NOTHING,ss:NOTHING

        push    ds

        push    SEL_DXDATA              ;address our data seg
        pop     ds
        assume  ds:DGROUP

        cmp     fDebug,0

        pop     ds
        ret

DXTestDebugIns  endp

DXCODE ends


;******************************************************************************
;
;   DXOutputDebugStr
;
;   Basically stolen from Windows/386 code by Ralph Lipe -- hacked up for
;   286 instead of 386.  Here in RalphL's own words is the description:
;
;   DESCRIPTION:
;       The following code is not pretty but it does what it needs to.  It will
;       only be included in DEBUG versions of Win386.  It accepts an ASCIIZ
;       string which it will output to the COM1 serial port.  If the string
;       contains #(Register) (for example #AX) then the value of that register
;       will be output.  It will not work for segment registers.
;
;       If the string contains ?(Register)[:(Register)] (for example ?AX or
;       ?AX:BX) then the value of the register(s) is passed to the debugger
;       to display the label nearest to the given address.  (It, also, will
;       not work with segment registers.  If ?AX is given, then the segment is
;       assumed to be the DX code segment.
;
;   ENTRY:
;       DS:SI -> ASCIIZ string
;
;   EXIT:
;       All registers and flags trashed
;
;   ASSUMES:
;       This procedure was called by the Trace_Out macro.  It assumes that
;       the stack is a pushad followed by a FAR call to this procedure.
;
;------------------------------------------------------------------------------

DXDATA  segment

Reg_Offset_Table LABEL WORD                     ; Order of PUSHA
        dw      "DI"
        dw      "SI"
        dw      "BP"
        dw      "SP"
        dw      "BX"
        dw      "DX"
        dw      "CX"
        dw      "AX"

DXDATA  ends

DXCODE segment
        assume  cs:DXCODE

        public  DXOutDebugStr

DXOutDebugStr   proc    far

        push    bp
        mov     bp, sp                      ; Assumes BP+6 = Pusha
        pushf
        push    es

        push    SEL_DXDATA                  ; Address our own data seg
        pop     es
        assume  ds:NOTHING,es:DGROUP

        cld
        cli

OSC1_Loop:
        lodsb                               ; Get the next character
        test    al, al                      ; Q: End of string?
        jz      short OSC1_Done             ;    Y: Return
        cmp     al, "#"                     ;    N: Q: Special register out?
        je      SHORT OSC1_Hex              ;          Y: Find out which one
        cmp     al, "?"                     ;       Q: special lable out?
        je      short OSC1_Label            ;          Y: find out which one
        cmp     al, "@"                     ;       Q: special string out?
        je      short OSC1_Str
OSC1_out:
        xor     ah, ah                      ;          N: Send char to COM
        call    Out_Debug_Chr
        jmp     OSC1_Loop                   ; Loop until done

OSC1_Hex:
        call    Get_Register
        jnc     short OSC1_not_special

        cmp     bl, 2                       ; Q: Word output?
        jb      SHORT OSC1_Out_Byte         ;    N: display byte
OSC1_Out_Word:
        call    Out_Hex_Word                ; Display AX in hex
        jmp     OSC1_Loop                   ; Output next char

OSC1_Out_Byte:
        xchg    al, ah                      ; swap bytes to print just
        call    Out_Hex_Byte                ; the low one!
        jmp     OSC1_Loop                   ; Output next char

OSC1_Label:
        call    Get_Register
        jc      short show_label
OSC1_not_special:
        lodsb                               ; Get special char again
        jmp     OSC1_out                    ; display it, and continue

show_label:
        mov     cx, ax                      ; save first value
        cmp     byte ptr [si], ':'          ;Q: selector separator?
        jne     short flat_offset           ;  N:
        lodsb                               ;  Y: eat the ':'
        call    Get_Register                ;   and attempt to get the selector
        jc      short sel_offset
flat_offset:
        mov     ax, cs                      ; default selector value
sel_offset:
        call    Show_Near_Label
        jmp     OSC1_Loop

OSC1_Str:
        call    Get_Register
        jnc     short OSC1_not_special
        mov     cx,ax
        cmp     byte ptr [si],':'
        jne     short no_selector
        lodsb
        push    cx
        call    Get_Register
        pop     cx
        xchg    ax,cx
        jc      short got_sel_off
        mov     cx,ax
no_selector:
        mov     ax,ds                       ; default selector for strings
got_sel_off:
        call    Show_String
        jmp     OSC1_Loop

OSC1_Done:                                  ; The end

        pop     es
        npopf
        pop     bp
        ret     10h

DXOutDebugStr   endp


;******************************************************************************
;
;   Get_Register
;
;   DESCRIPTION:
;
;   ENTRY:
;
;   EXIT:           Carry set if register value found
;                       AX = register value
;                       BL = value size   (1, 2, 4)
;
;   USES:
;
;==============================================================================


Get_Register    proc    near

        push    si                          ; Save string pointer
        xor     ax, ax                      ; Zero AX
        mov     bl, 2                       ; BL = 2 (assume word output)
        call    Load_Up_Char                ; Get 1st char
        mov     ah, al                      ; Move 1st char to AH
        call    Load_Up_Char                ; Get second char
        cmp     al, 'L'                     ; Q: "L" (ie AL, BL, etc)?
        jne     short OSC1_WORD             ;    N: word reg
        mov     al, 'X'                     ;    Y: change to X for pos match
        mov     bl, 1                       ;       BL = 1 -- byte output
OSC1_WORD:
        xor     di, di                      ; DI = 0
        mov     cx, 8                       ; Size of a pusha
OSC1_Special_Loop:
        push    di
        shl     di,1
        cmp     ax, Reg_Offset_Table[di]    ; Q: Is this the register?
        pop     di
        je      SHORT OSC1_Out_Reg          ;    Y: Output it
        inc     di                          ;    N: Try the next one
        loop    OSC1_Special_Loop           ;       until CX = 0

OSC1_Ignore_Special:                        ; OOPS!  backup and ignore special
        pop     si                          ; Restore original string ptr
        dec     si                          ; back up to special char
        clc
        jmp     short gr_exit

OSC1_Out_Reg:
        push    di
        shl     di,1
        mov     ax, SS:[bp.6][di]           ; AX = Value to output
        pop     di
        add     sp, 2                       ; Trash pushed SI
        cmp     bl, 2                       ;Q: byte or word value?
        je      short value_fnd             ;      jump if word value
        xor     ah, ah                      ;      clear ah, if byte value
value_fnd:
        stc

gr_exit:
        ret

Get_Register    endp


;******************************************************************************
;
;   Load_Up_Char
;
;   Moves the character at DS:SI into AL and converts it to an upper case
;   character.  SI is incremented.
;
;------------------------------------------------------------------------------

Load_Up_Char    proc    near

        lodsb
        cmp     al, "Z"
        jb      SHORT LUC_Done
        sub     al, "a" - "A"
LUC_Done:
        ret

Load_Up_Char    endp


;******************************************************************************
;
;   Out_Hex_Word
;
;   Outputs the value in AX to the COM port in hexadecimal.
;
;------------------------------------------------------------------------------

Out_Hex_Word    proc    near

        rol     ax, 4
        call    Out_Hex_Char
        rol     ax, 4
        call    Out_Hex_Char
Out_Hex_Byte:
        rol     ax, 4
        call    Out_Hex_Char
        rol     ax, 4
        call    Out_Hex_Char

        ret

Out_Hex_Word    endp


;******************************************************************************
;
;   Out_Hex_Char
;
;   Outputs the low nibble in AL to the COM port.
;
;------------------------------------------------------------------------------

DXDATA  segment

Hex_Char_Tab LABEL BYTE
        db      "0123456789ABCDEF"

DXDATA  ends

Out_Hex_Char    proc    near

        push    ax
        push    bx
        mov     bx, ax
        and     bx, 1111b
        mov     al, Hex_Char_Tab[bx]
        call    Out_Debug_Chr
        pop     bx
        pop     ax
        ret

Out_Hex_Char    endp


;******************************************************************************
;
;   Out_Debug_Chr
;
;   DESCRIPTION:
;
;   ENTRY:
;       AL contains character to output
;
;   EXIT:
;
;   USES:
;       Nothing
;
;==============================================================================

Out_Debug_Chr   proc    near
        assume ds:nothing,es:DGROUP

        push    ax
        mov     ax,cs
        cmp     ax,SEL_DXCODE0          ; Are we in real mode?
        pop     ax
        jne     out_com

        cmp     fDebug,0                ; debugger installed?
        je      out_com                 ; N: go output it ourselves

        push    ax
        push    dx
        mov     dl, al
        mov     ax, DS_Out_Char
        int     Debug_Serv_Int
        pop     dx
        pop     ax
        ret

Out_Debug_Chr   endp


;******************************************************************************
;
;   out_com
;
;   DESCRIPTION:    Reprograms COM and outputs a character (stolen from DEB386).
;
;   ENTRY:          AL character to output
;
;   EXIT:
;
;   USES:
;
;==============================================================================


UR_DAT  equ     0000h           ; Data port
UR_IEN  equ     0001h           ; Interrupt enable
UR_IER  equ     0002h           ; interrupt ID
UR_LCR  equ     0003h           ; line control registers
UR_MCR  equ     0004h           ; modem control register
UR_LSR  equ     0005h           ; line status register
UR_MSR  equ     0006h           ; modem status regiser
UR_DLL  equ     0000h           ; divisor latch least sig
UR_DLM  equ     0001h           ; divisor latch most sig

        public  out_com

out_com proc    near

        push    dx
        push    ax

        push    ds              ;get first com port address from BIOS area
        mov     ax,40h
        mov     ds,ax
        mov     dx,ds:[0]
        pop     ds

        or      dx,dx           ;forget it if no comm port
        jnz     @f

        pop     ax
        pop     dx
        ret
@@:

;
;   Initialize the port on EVERY CHARACTER just to make sure
;
        add     dx, UR_LCR
        mov     al, 80h
        out     dx, al
        IO_Delay
        sub     al, al
        add     dx, UR_DLM - UR_LCR
        out     dx, al
        IO_Delay
        add     dx, UR_DLL - UR_DLM
        mov     al, 12
        out     dx, al
        IO_Delay
        mov     al, 3
        add     dx, UR_LCR - UR_DLL
        out     dx, al
        IO_Delay
        add     dx, UR_LSR - UR_LCR

Wait_Til_Ready:
        in      al, dx
        IO_Delay
        test    al, 020h
        jz      Wait_Til_Ready
;
;   Crank it out!
;
        add     dx, UR_DAT - UR_LSR
        pop     ax
        out     dx, al
        IO_Delay
        pop     dx

        ret

out_com endp


;******************************************************************************
;
;   Show_Near_Label
;
;   DESCRIPTION:    call the debugger to display a label less than or equal
;                   to the given address
;
;   ENTRY:          AX is selector, CX is offset of address to try to find
;                   a symbol for
;                   ES selector to DOSX data segment
;   EXIT:
;
;   USES:
;
;==============================================================================

Show_Near_Label proc    near

        push    ax
        mov     ax,cs
        cmp     ax,SEL_DXCODE0          ; Are we in real mode?
        pop     ax
        jne     Show_Near_Label_ret

        cmp     es:idCpuType,3                  ;use 32 bit regs?
        jae     debug_386

        push    ax                              ;on a 286, use 16 bit regs
        push    bx
        push    cx
        mov     bx,cx
        mov     cx,ax
        mov     ax,DS_Out_Symbol
        int     Debug_Serv_Int
        pop     cx
        pop     bx
        pop     ax
        ret

debug_386:
        .386p
        push    eax
        push    ebx
        push    ecx

        mov     bx, cx

        movzx   ecx, ax                         ;WDEB386 wants a 32 bit offset

        mov     ax, DS_Out_Symbol
        int     Debug_Serv_Int

        pop     ecx
        pop     ebx
        pop     eax
        ret

        .286p

Show_Near_Label_ret:

Show_Near_Label endp


;******************************************************************************
;
;   Show_String
;
;   DESCRIPTION:    Display an asciiz string
;
;   ENTRY:          AX is selector, CX is offset of address to find string
;
;   EXIT:
;
;   USES:
;
;==============================================================================

Show_String     proc    near

        push    ax
        push    ds
        push    si

        mov     ds,ax
        mov     si,cx
        xor     ax,ax
@@:
        lodsb
        or      al,al
        jz      @f
        call    Out_Debug_Chr
        jmp     short @b
@@:
        pop     si
        pop     ds
        pop     ax

        ret

Show_String     endp


DXCODE  ends

endif   ; DEBUG


if NTDEBUG
;******************************************************************************
;
; The following macros and routines implement DOSX's internal trace tables.
;
;==============================================================================

ifdef WOW_x86

SAVE_STATE_PM MACRO
        push    ebp
        mov     ebp, esp
        pushfd
        pushad
        push    ss
        push    es
        push    ds
        push    cs

        mov     ax, ss
        lar     eax,eax
        test    eax,(AB_BIG SHL 8)
        jnz     @f
        movzx   ebp,bp
@@:
ENDM
RESTORE_STATE_PM MACRO
        add     esp, 2
        pop     ds
        pop     es
        pop     ss
        popad
        popfd
        pop     ebp
ENDM

SAVE_STATE_V86 MACRO
        push    ebp
        mov     ebp, esp
        pushfd
        pushad
        push    ss
        push    es
        push    ds
        push    cs
ENDM
RESTORE_STATE_V86 MACRO
        add     esp, 2
        pop     ds
        pop     es
        pop     ss
        popad
        popfd
        pop     ebp
ENDM

TRACEDS_PM MACRO
        mov     ax,SEL_DXDATA or STD_RING
        mov     ds, ax
ENDM

TRACEDS_V86 MACRO
        mov     ds,selDgroup
ENDM

DEBUG_TRACE_PROC        MACRO mode
        local dbgtr_exit

        .386p
        SAVE_STATE_&mode

        TRACEDS_&mode
        assume ds:DGROUP

        test    fDebugTrace, 0ffh
        jnz     short @f
        jmp     dbgtr_exit                      ;just return
@@:
;------------------------------------------------------------------------------
; Update DebugTraceBuffer
;------------------------------------------------------------------------------
        mov     di, DebugTracePointer
        add     di, TRACE_ENTRY_SIZE
        cmp     di, OFFSET DebugTraceBuffer + TRACE_ENTRY_SIZE*(NUM_TRACE_ENTRIES-1)
        jbe     short @f
        mov     di, OFFSET DebugTraceBuffer
@@:
        mov     DebugTracePointer, di
        movzx   edi, di

        push    edi
        push    ds

        push    ds
        pop     es
        push    ss
        pop     ds
        mov     ecx, 8                          ;four parameters on stack
        mov     esi, ebp                        ;point to input parameters
        add     esi, 6
        cld
        rep     movsb                           ;move them to debugtracebuffer
        pop     ds
        pop     edi

        mov     ax, DebugTraceCounter
        mov     word ptr es:[di+8], ax
        inc     DebugTraceCounter

        push    ds
        mov     ax, SEL_VDMTIB or STD_RING
        mov     ds, ax
        mov     ax, word ptr ds:[0]
        mov     word ptr es:[di+10], ax
        pop     ds

        mov     ax, word ptr ss:[ebp+4]
        mov     word ptr es:[di+12], ax

;------------------------------------------------------------------------------
; Update RegisterTraceBuffer
;------------------------------------------------------------------------------
        mov     ax, RegisterTracePointer
        add     ax, REGTRACE_ENTRY_SIZE
        cmp     ax, OFFSET RegisterTraceBuffer + REGTRACE_ENTRY_SIZE*(NUM_TRACE_ENTRIES-1)
        jbe     short @f
        mov     ax, OFFSET RegisterTraceBuffer
@@:
        mov     RegisterTracePointer, ax
        mov     word ptr es:[di+14], ax         ;save pointer to register buff
        mov     di, ax


        push    ds
        pop     es
        push    ss
        pop     ds
        mov     ecx, REGTRACE_ENTRY_SIZE
        mov     esi, ebp
        sub     esi, 44                         ;point to saved register values
        cld
        rep     movsb

dbgtr_exit:
        RESTORE_STATE_&mode
        ENDM

else

SAVE_STATE_PM MACRO
        push    bp
        mov     bp, sp
        pushf
        pusha
        push    ss
        push    es
        push    ds
        push    cs
ENDM
RESTORE_STATE_PM MACRO
        add     sp, 2
        pop     ds
        pop     es
        pop     ss
        popa
        popf
        pop     bp
ENDM

SAVE_STATE_V86 MACRO
        push    bp
        mov     bp, sp
        pushf
        pusha
        push    ss
        push    es
        push    ds
        push    cs
ENDM
RESTORE_STATE_V86 MACRO
        add     sp, 2
        pop     ds
        pop     es
        pop     ss
        popa
        popf
        pop     bp
ENDM

DEBUG_TRACE_PROC        MACRO mode
        local dbgtr_exit

        .286p

        SAVE_STATE_&mode

        mov     ax,SEL_DXDATA or STD_RING
        mov     ds, ax
        assume ds:DXDATA

        test    fDebugTrace, 0ffh
        jnz     short @f
        jmp     dbgtr_exit                      ;just return
@@:
;------------------------------------------------------------------------------
; Update DebugTraceBuffer
;------------------------------------------------------------------------------
        mov     di, DebugTracePointer
        add     di, TRACE_ENTRY_SIZE
        cmp     di, OFFSET DebugTraceBuffer + TRACE_ENTRY_SIZE*(NUM_TRACE_ENTRIES-1)
        jbe     short @f
        mov     di, OFFSET DebugTraceBuffer
@@:
        mov     DebugTracePointer, di

        push    di
        push    ds

        push    ds
        pop     es
        push    ss
        pop     ds
        mov     cx, 8                          ;four parameters on stack
        mov     si, bp                        ;point to input parameters
        add     si, 6
        cld
        rep     movsb                           ;move them to debugtracebuffer
        pop     ds
        pop     di

        mov     ax, DebugTraceCounter
        mov     word ptr es:[di+8], ax
        inc     DebugTraceCounter

        mov     word ptr es:[di+10], 0
        mov     ax, word ptr ss:[bp+4]
        mov     word ptr es:[di+12], ax

;------------------------------------------------------------------------------
; Update RegisterTraceBuffer
;------------------------------------------------------------------------------
        mov     ax, RegisterTracePointer
        add     ax, REGTRACE_ENTRY_SIZE
        cmp     ax, OFFSET RegisterTraceBuffer + REGTRACE_ENTRY_SIZE*(NUM_TRACE_ENTRIES-1)
        jbe     short @f
        mov     ax, OFFSET RegisterTraceBuffer
@@:
        mov     RegisterTracePointer, ax
        mov     word ptr es:[di+14], ax         ;save pointer to register buff
        mov     di, ax


        push    ds
        pop     es
        push    ss
        pop     ds
        mov     cx, REGTRACE_ENTRY_SIZE
        mov     si, bp
        sub     si, 44                         ;point to saved register values
        cld
        rep     movsb

dbgtr_exit:
        RESTORE_STATE_&mode
        ENDM

endif ; WOW_x86

DXDATA  segment

NUM_TRACE_PARAMETERS    equ     4               ;# of pass parameters to proc
NUM_TRACE_ENTRIES       equ     256
TRACE_ENTRY_SIZE        equ     2*8
REGTRACE_ENTRY_SIZE     equ     12*4

        public  DebugTraceBuffer, DebugTracePointer
        public  RegisterTraceBuffer, RegisterTracePointer
        ALIGN 16
DebugTraceBuffer        db     TRACE_ENTRY_SIZE*NUM_TRACE_ENTRIES dup (?)
RegisterTraceBuffer     db     REGTRACE_ENTRY_SIZE*NUM_TRACE_ENTRIES dup (?)

DebugTracePointer       dw    OFFSET DebugTraceBuffer
DebugTraceCounter       dw    0
RegisterTracePointer    dw    OFFSET RegisterTraceBuffer
DXDATA ends

        extrn   pbReflStack:WORD
        extrn   fDebugTrace:BYTE


DXPMCODE  segment
        assume cs:DXPMCODE
;******************************************************************************
;
;   DebugTrace
;
;   DESCRIPTION:
;
;   ENTRY:
;
;   EXIT:
;
;   USES:
;
;==============================================================================

        public  DebugTrace
DebugTrace     proc    near

        DEBUG_TRACE_PROC PM
        ret     2*NUM_TRACE_PARAMETERS

DebugTrace     endp

DXPMCODE  ends

DXCODE segment

IFNDEF  ROM
        extrn   selDgroup:WORD
ENDIF

        assume cs:DXCODE
;******************************************************************************
;
;   Stack_Trace
;
;   DESCRIPTION:
;
;   ENTRY:
;
;   EXIT:
;
;   USES:
;
;==============================================================================

        public  DebugTraceRm
DebugTraceRm     proc    near

        DEBUG_TRACE_PROC V86
        ret     2*NUM_TRACE_PARAMETERS

DebugTraceRm     endp

DXCODE ends

endif ; NTDEBUG
        end