]> 4ch.mooo.com Git - 16.git/blob - src/lib/doslib/hw/cpu/cpurdtsc.asm
added a bunch of things~ and midi stuff~
[16.git] / src / lib / doslib / hw / cpu / cpurdtsc.asm
1
2 section .text class=CODE
3
4 ; NTS: If we code 'push ax' and 'popf' for the 16-bit tests in 32-bit protected mode we will screw up the stack pointer and crash
5 ;      so we avoid duplicate code by defining 'native' pushf/popf functions and 'result' to ax or eax depending on CPU mode
6 %if TARGET_MSDOS == 32
7  %define point_s esi
8  %define result eax
9  %define pushfn pushfd
10  %define popfn popfd
11 use32
12 %else
13  %define point_s si
14  %define result ax
15  %define pushfn pushf
16  %define popfn popf
17 use16
18 %endif
19
20 %if TARGET_MSDOS == 16
21  %ifndef MMODE
22   %error You must specify MMODE variable (memory model) for 16-bit real mode code
23  %endif
24 %endif
25
26 %if TARGET_MSDOS == 16
27  %ifidni MMODE,l
28   %define retnative retf
29   %define cdecl_param_offset 6  ; RETF addr + PUSH BP
30  %else
31   %ifidni MMODE,m
32    %define retnative retf
33    %define cdecl_param_offset 6 ; RETF addr + PUSH BP
34   %else
35    %define retnative ret
36    %define cdecl_param_offset 4 ; RET addr + PUSH BP
37   %endif
38  %endif
39 %else
40  %define retnative ret
41  %define cdecl_param_offset 8   ; RET addr + PUSH EBP
42 %endif
43
44 %if TARGET_MSDOS == 16
45 ; cpu_rdtsc():
46 ; read RDTSC and return.
47 ; places 64-bit value in AX:BX:CX:DX according to Watcom register calling convention
48 global cpu_rdtsc_
49 cpu_rdtsc_:
50         pushf
51         cli
52         rdtsc
53         xchg eax,edx
54         mov ecx,edx
55         mov ebx,eax
56         shr eax,16
57         shr ecx,16
58         popf
59         retnative
60 %endif
61
62 %if TARGET_MSDOS == 16
63 ; WARNING: Do not execute this instruction when you are a Windows application.
64 ;          The Windows VM doesn't take it well.
65 ; void __cdecl cpu_rdtsc_write(uint64_t val):
66 ; write RDTSC and return.
67 global cpu_rdtsc_write_
68 cpu_rdtsc_write_:
69         pushf
70         push    ax
71         push    bx
72         push    cx
73         push    dx
74         cli
75
76         ; assemble EAX = AX:BX
77         shl     eax,16
78         mov     ax,bx
79
80         ; assemble EDX = CX:DX
81         shl     ecx,16
82         and     edx,0xFFFF
83         or      edx,ecx
84
85         ; doit
86         xchg    eax,edx
87         mov     ecx,0x10
88         wrmsr
89
90         pop     dx
91         pop     cx
92         pop     bx
93         pop     ax
94         popf
95         retnative
96 %endif
97