Index: trunk/src/dsound/OS2SNDBUFFER.CPP
===================================================================
--- trunk/src/dsound/OS2SNDBUFFER.CPP	(revision 2636)
+++ trunk/src/dsound/OS2SNDBUFFER.CPP	(revision 2637)
@@ -1,3 +1,3 @@
-/* $Id: OS2SNDBUFFER.CPP,v 1.5 1999-11-14 22:12:15 hugh Exp $ */
+/* $Id: OS2SNDBUFFER.CPP,v 1.6 2000-02-04 19:29:16 hugh Exp $ */
 
 /*
@@ -100,4 +100,6 @@
           bufferdesc.lpwfxFormat->cbSize + sizeof(WAVEFORMATEX));
 
+  bufferdesc.lpwfxFormat = lpfxFormat;
+
   lpBuffer = (LPSTR)VirtualAlloc( 0, bufferdesc.dwBufferBytes, MEM_COMMIT, PAGE_READWRITE);
   if(lpBuffer == NULL)
@@ -108,4 +110,5 @@
       return;
   }
+
 }
 //******************************************************************************
@@ -210,8 +213,11 @@
     Ticks = CurrentTicks-me->LastTickCnt;
     dprintf(("  Is Playing, Ticks: Last %d Current %d Difference %d",me->LastTickCnt, CurrentTicks,Ticks));
-    me->LastTickCnt = CurrentTicks;
-
-    NewPos = ((me->frequency*Ticks)/1000)*(me->bufferdesc.lpwfxFormat->wBitsPerSample/8);
+    dprintf(("  Frequency: %d Bits per Sample %d",me->frequency,me->lpfxFormat->wBitsPerSample));
+
+    NewPos = ((me->frequency*Ticks)/1000)*(me->lpfxFormat->wBitsPerSample/8);
     dprintf(("  advance pos by %d bytes",NewPos));
+
+    if(NewPos)
+      me->LastTickCnt = CurrentTicks;
 
     if(me->playpos+NewPos >= me->bufferdesc.dwBufferBytes)
@@ -235,5 +241,5 @@
     if(me->writepos >= me->bufferdesc.dwBufferBytes)
       me->writepos = me->writepos % me->bufferdesc.dwBufferBytes;
-
+    dprintf(("  new Pos Write: %d   Play: %d",me->writepos,me->playpos));
   }
   else
@@ -361,12 +367,15 @@
     dwWriteCursor = me->writepos;
   }
+  dprintf(("  Buffer at 0x%08X Size %d",me->lpBuffer,me->bufferdesc.dwBufferBytes));
   if(dwWriteCursor + dwWriteBytes > me->bufferdesc.dwBufferBytes )
   {
     *(DWORD *)lplpvAudioPtr1  = (DWORD)(me->lpBuffer + dwWriteCursor);
     *lpdwAudioBytes1          = me->bufferdesc.dwBufferBytes - dwWriteCursor;
+    dprintf(("  Lock1 at 0x%08X Size %d",*(DWORD *)lplpvAudioPtr1,*lpdwAudioBytes1));
     if(lplpvAudioPtr2 && lpdwAudioBytes2)
     {
-        *(DWORD *)lplpvAudioPtr2   = (DWORD)me->lpBuffer;
-        *lpdwAudioBytes2           = dwWriteBytes - *lpdwAudioBytes1;
+      *(DWORD *)lplpvAudioPtr2   = (DWORD)me->lpBuffer;
+      *lpdwAudioBytes2           = dwWriteBytes - *lpdwAudioBytes1;
+      dprintf(("  Lock2 at 0x%08X Size %d",*(DWORD *)lplpvAudioPtr2,*lpdwAudioBytes2));
     }
   }
@@ -375,8 +384,10 @@
     *(DWORD *)lplpvAudioPtr1  = (DWORD)(me->lpBuffer + dwWriteCursor);
     *lpdwAudioBytes1          = dwWriteBytes;
+    dprintf(("  Lock1 at 0x%08X Size %d",*(DWORD *)lplpvAudioPtr1,*lpdwAudioBytes1));
     if(lplpvAudioPtr2 && lpdwAudioBytes2)
     {
-        *(DWORD *)lplpvAudioPtr2   = 0;
-        *lpdwAudioBytes2           = 0;
+      *(DWORD *)lplpvAudioPtr2   = 0;
+      *lpdwAudioBytes2           = 0;
+      dprintf(("  Lock2 at 0x%08X Size %d",*(DWORD *)lplpvAudioPtr2,*lpdwAudioBytes2));
     }
   }
@@ -493,5 +504,5 @@
     me->LastTickCnt = CurrentTicks;
 
-    NewPos = ((me->frequency*Ticks)/1000)*(me->bufferdesc.lpwfxFormat->wBitsPerSample>>8);
+    NewPos = ((me->frequency*Ticks)/1000)*(me->lpfxFormat->wBitsPerSample>>8);
     if(me->playpos+NewPos >= me->bufferdesc.dwBufferBytes)
     {
@@ -531,4 +542,5 @@
     return DSERR_INVALIDPARAM;
   }
+
   return DS_OK;
 }
Index: trunk/src/dsound/asmutil.asm
===================================================================
--- trunk/src/dsound/asmutil.asm	(revision 2637)
+++ trunk/src/dsound/asmutil.asm	(revision 2637)
@@ -0,0 +1,54 @@
+; asmutil.asm Get Correct MS counter
+;
+; Copyright 1999 Markus Montkowski
+
+                NAME    asmutil
+.586p
+.MMX
+
+CODE32          SEGMENT DWORD PUBLIC USE32 'CODE'
+
+PUBLIC _GetMsCount
+;----------------------------------------------------------------------------
+; ulong GetMSCount (CPU_largeInteger *t1,ULONG freq)
+;----------------------------------------------------------------------------
+; Get the MS value from the High Tmr values
+;----------------------------------------------------------------------------
+_GetMsCount PROC NEAR
+
+    t1 EQU  EBP+8
+    freq  EQU  EBP+12
+
+    push  ebp
+    mov   ebp, esp
+    push  ebx
+    push  edi
+    push  esi
+
+    mov   ecx,[t1]
+    mov   eax,[ecx]   ; EAX := t1.low
+    mov   edx,[ecx+4]   ; ECX := t1.high
+    shld  edx,eax,20
+    shl   eax,20      ; t1 * 0x100000
+    div   DWORD[freq] ; (t1 * 0x100000) / freq
+    mov   ecx,1000000
+    xor   edx,edx
+    mul   ecx       ; ((t1 * 0x100000) / freq) * 1000000)
+    shrd  eax,edx,20    ; ((t1 * 0x100000) / freq) * 1000000) / 0x100000
+
+    pop  edi
+    pop  esi
+    pop  ebx
+    cld
+    mov  esp,ebp
+    pop  ebp
+    ret
+
+_GetMsCount endp
+
+
+
+
+CODE32          ENDS
+
+                END
Index: trunk/src/dsound/asmutil.h
===================================================================
--- trunk/src/dsound/asmutil.h	(revision 2637)
+++ trunk/src/dsound/asmutil.h	(revision 2637)
@@ -0,0 +1,21 @@
+/*
+ * asmutil.asm function definition
+ *
+ * Copyright 1998 Sander van Leeuwen
+ *           1999 Markus Montkowski
+ */
+
+#ifndef __ASMUTIL_H__
+#define __ASMUTIL_H__
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+extern ULONG __cdecl GetMsCount( QWORD* pqwT1, ULONG ulFreq);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
Index: trunk/src/dsound/makefile
===================================================================
--- trunk/src/dsound/makefile	(revision 2636)
+++ trunk/src/dsound/makefile	(revision 2637)
@@ -1,3 +1,3 @@
-# $Id: makefile,v 1.12 2000-01-26 23:19:55 sandervl Exp $
+# $Id: makefile,v 1.13 2000-02-04 19:29:17 hugh Exp $
 
 #
@@ -14,5 +14,5 @@
 
 !include $(PDWIN32_INCLUDE)/pdwin32.mk
-
+ASFLAGS = -Sc -Sv:ALP
 
 CFLAGS = $(CFLAGS) -I$(PDWIN32_INCLUDE)
@@ -22,5 +22,5 @@
 TARGET = dsound
 
-OBJS =  dsound.obj os2dsound.obj initterm.obj os2sndbuffer.obj os2helper.obj
+OBJS =  dsound.obj os2dsound.obj initterm.obj os2sndbuffer.obj os2helper.obj asmutil.obj
 #OBJS =  dsound.obj initterm.obj
 
@@ -43,5 +43,4 @@
 
 
-
 dsound.obj: dsound.cpp \
     $(PDWIN32_INCLUDE)/misc.h \
@@ -61,4 +60,5 @@
     $(PDWIN32_INCLUDE)/os2wrap.h
 
+asmutil.obj: asmutil.asm
 
 initterm.obj: initterm.cpp initterm.h
Index: trunk/src/dsound/os2helper.cpp
===================================================================
--- trunk/src/dsound/os2helper.cpp	(revision 2636)
+++ trunk/src/dsound/os2helper.cpp	(revision 2637)
@@ -3,8 +3,9 @@
 #include <os2wrap.h>
 #include <misc.h>
+#include "asmutil.h"
 
 typedef ULONG DWORD ;
 
-#if 0
+#if 1
 DWORD GetTickCountOS2()
 {
@@ -22,23 +23,21 @@
   QWORD qwTickCount;
   ULONG ulFreq;
-  DWORD dwTick;
+  ULONG ulTicks;
   APIRET rc;
 
   rc = DosTmrQueryFreq( &ulFreq);
+  dprintf(("DSOUND: GetTickCountOS2 Freq: %d(%08X)\n",
+           ulFreq, ulFreq));
+
   if(0==rc)
   {
     rc = DosTmrQueryTime(&qwTickCount);
-    if(0==rc)
-    {
-      ulFreq /=1000;
-      dwTick = qwTickCount.ulLo / ulFreq;
-      //dwTick = ( (qwTickCount.ulHi <<24)+ (qwTickCount.ulLo>>8)) / (ulFreq>>8);
-    }
+    dprintf(("DSOUND: GetTickCountOS2 Time %d(%08X) : %d(%08X)\n",
+             qwTickCount.ulHi, qwTickCount.ulHi,
+             qwTickCount.ulLo,qwTickCount.ulLo, ulTicks));
+    ulTicks = GetMsCount(&qwTickCount,ulFreq);
+    dprintf(("DSOUND: GetTickCountOS2 ms Ticks %d\n"));
   }
-  //dprintf(("DSOUND: GetTickCountOS2 Freq: %d(%08X) Time %d(%08X) : %d(%08X)\n",
-  //         ulFreq, ulFreq,
-  //         qwTickCount.ulHi, qwTickCount.ulHi,
-  //         qwTickCount.ulLo,qwTickCount.ulLo));
-  return 0==rc?dwTick:0x00FFDEAD;
+  return 0==rc?ulTicks:0x00FFDEAD;
 }
 #endif
