Previous 15994 Revisions Next

r7089 Wednesday 20th January, 2010 at 20:24:10 UTC by Stefano Priore
More obsolete files deleted
[src/osd/sdl]keybled.c osd_cpu.h scale2x.c scale2x_core.c sdlmisc.c sdlsync.c sdltime.c

src/osd/sdl/osd_cpu.h
r7088r7089
1//============================================================
2//
3//  osd_cpu.h - SDL CPU-specific data types
4//
5//  Copyright (c) 1996-2007, Nicola Salmoria and the MAME Team.
6//  Visit http://mamedev.org for licensing and usage restrictions.
7//
8//============================================================
9
10/*******************************************************************************
11*                                                                              *
12*   Define size independent data types and operations.                         *
13*                                                                              *
14*   The following types must be supported by all platforms:                    *
15*                                                                              *
16*   UINT8  - Unsigned 8-bit Integer     INT8  - Signed 8-bit integer           *
17*   UINT16 - Unsigned 16-bit Integer    INT16 - Signed 16-bit integer          *
18*   UINT32 - Unsigned 32-bit Integer    INT32 - Signed 32-bit integer          *
19*   UINT64 - Unsigned 64-bit Integer    INT64 - Signed 64-bit integer          *
20*                                                                              *
21*                                                                              *
22*   The macro names for the artithmatic operations are composed as follows:    *
23*                                                                              *
24*   XXX_R_A_B, where XXX - 3 letter operation code (ADD, SUB, etc.)            *
25*                    R   - The type of the result                              *
26*                    A   - The type of operand 1                               *
27*                    B   - The type of operand 2 (if binary operation)         *
28*                                                                              *
29*                    Each type is one of: U8,8,U16,16,U32,32,U64,64            *
30*                                                                              *
31*******************************************************************************/
32#pragma once
33
34#ifndef OSD_CPU_H
35#define OSD_CPU_H
36
37/* Combine two 32-bit integers into a 64-bit integer */
38#define COMBINE_64_32_32(A,B)     ((((UINT64)(A))<<32) | (UINT32)(B))
39#define COMBINE_U64_U32_U32(A,B)  COMBINE_64_32_32(A,B)
40
41/* Return upper 32 bits of a 64-bit integer */
42#define HI32_32_64(A)        (((UINT64)(A)) >> 32)
43#define HI32_U32_U64(A)        HI32_32_64(A)
44
45/* Return lower 32 bits of a 64-bit integer */
46#define LO32_32_64(A)        ((A) & 0xffffffff)
47#define LO32_U32_U64(A)        LO32_32_64(A)
48
49#define DIV_64_64_32(A,B)     ((A)/(B))
50#define DIV_U64_U64_U32(A,B)  ((A)/(UINT32)(B))
51
52#define MOD_32_64_32(A,B)     ((A)%(B))
53#define MOD_U32_U64_U32(A,B)  ((A)%(UINT32)(B))
54
55#define MUL_64_32_32(A,B)     ((A)*(INT64)(B))
56#define MUL_U64_U32_U32(A,B)  ((A)*(UINT64)(UINT32)(B))
57
58#endif   /* defined OSD_CPU_H */
src/osd/sdl/scale2x_core.c
r7088r7089
1/* scale2x algorithm (Andrea Mazzoleni, http://advancemame.sourceforge.net):
2 *
3 * A 9-pixel rectangle is taken from the source bitmap:
4 *
5 *  a b c
6 *  d e f
7 *  g h i
8 *
9 * The central pixel e is expanded into four new pixels,
10 *
11 *  e0 e1
12 *  e2 e3
13 *
14 * where
15 *
16 *  e0 = (d == b && b != f && d != h) ? d : e;
17 *  e1 = (b == f && b != d && f != h) ? f : e;
18 *  e2 = (d == h && d != b && h != f) ? d : e;
19 *  e3 = (h == f && d != h && b != f) ? f : e;
20 *
21 * Copyright stuff:
22 *
23 * 1st According to the copyright header in the original C file this code is
24 * DUAL licensed under:
25 * A) GPL (with an extra clause)(see file COPYING)
26 * B) you are allowed to use this code in your program with these conditions:
27 *    - the program is not used in commercial activities.
28 *    - the whole source code of the program is released with the binary.
29 *    - derivative works of the program are allowed.
30 * IANAL but IMHO xmame matches the demands for B.
31 *
32 * 2nd the version of the GPL used contains this extra clause:
33 *
34 ****************************************************************************
35 *
36 * The AdvanceMAME/MESS sources are released under the
37 * GNU General Public License (GPL) with this special exception
38 * added to every source file :
39 *
40 *     "In addition, as a special exception, Andrea Mazzoleni
41 *     gives permission to link the code of this program with
42 *     the MAME library (or with modified versions of MAME that use the
43 *     same license as MAME), and distribute linked combinations including
44 *     the two.  You must obey the GNU General Public License in all
45 *     respects for all of the code used other than MAME.  If you modify
46 *     this file, you may extend this exception to your version of the
47 *     file, but you are not obligated to do so.  If you do not wish to
48 *     do so, delete this exception statement from your version."
49 *
50 * This imply that if you distribute a binary version of AdvanceMAME/MESS
51 * linked with the MAME and MESS sources you must also follow the MAME
52 * License.
53 *
54 ****************************************************************************
55 *
56 * So basicly this code may be distributed under the MAME license, as is
57 * xmame
58 *
59 * 3th The person who originaly incorperated this effect into
60 * xmame, got explicited permission, see permission.txt.
61 *
62 * J.W.R. de Goede, Rotterdam the Netherlands, 17 december 2004.
63 */
64
65INLINE void FUNC_NAME(scale2x_border)(DEST_TYPE *dst, DEST_TYPE *src0,
66   DEST_TYPE *src1, DEST_TYPE *src2, int count,
67   int borderpix)
68{
69   /* first pixel */
70   if (borderpix)
71      *dst++ = 0;
72   
73   if (src0[0] != src2[0] && src1[0] != src1[1]) {
74      *dst++ = src1[0] == src0[0] ? src0[0] : src1[0];
75      *dst++ = src1[1] == src0[0] ? src0[0] : src1[0];
76   } else {
77      *dst++ = src1[0];
78      *dst++ = src1[0];
79   }
80   ++src0;
81   ++src1;
82   ++src2;
83
84   /* central pixels */
85   count -= 2;
86   while (count) {
87      if (src1[-1] == src0[0] && src2[0] != src0[0] && src1[ 1] != src0[0])
88         *dst++ = src0[0];
89      else *dst++ = src1[0];
90
91      if (src1[ 1] == src0[0] && src2[0] != src0[0] && src1[-1] != src0[0])
92         *dst++ = src0[0];
93      else *dst++ = src1[0];
94
95      src0++;
96      src1++;
97      src2++;
98      count--;
99   }
100
101   /* last pixel */
102   if (src0[0] != src2[0] && src1[-1] != src1[0]) {
103      *dst++ = src1[-1] == src0[0] ? src0[0] : src1[0];
104      *dst++ = src1[0] == src0[0] ? src0[0] : src1[0];
105   } else {
106      *dst++ = src1[0];
107      *dst++ = src1[0];
108   }
109   if (borderpix)
110      *dst++ = 0;
111}
112
113INLINE void FUNC_NAME(scale2x_center)(DEST_TYPE *dst, DEST_TYPE *src0,
114   DEST_TYPE *src1, DEST_TYPE *src2, int count,
115   int borderpix)
116{
117   /* first pixel */
118   if (borderpix)
119      *dst++ = 0;
120   
121   if (src0[0] != src2[0] && src1[0] != src1[1]) {
122      *dst++ = src1[0];
123      *dst++ = (src1[1] == src0[0] && src1[0] != src2[1]) || (src1[1] == src2[0] && src1[0] != src0[1]) ? src1[1] : src1[0];
124   } else {
125      *dst++ = src1[0];
126      *dst++ = src1[0];
127   }
128   ++src0;
129   ++src1;
130   ++src2;
131
132   /* central pixels */
133   count -= 2;
134   while (count) {
135      if (src0[0] != src2[0] && src1[-1] != src1[1]) {
136         *dst++ = (src1[-1] == src0[0] && src1[0] != src2[-1]) || (src1[-1] == src2[0] && src1[0] != src0[-1]) ? src1[-1] : src1[0];
137         *dst++ = (src1[1] == src0[0] && src1[0] != src2[1]) || (src1[1] == src2[0] && src1[0] != src0[1]) ? src1[1] : src1[0];
138      } else {
139         *dst++ = src1[0];
140         *dst++ = src1[0];
141      }
142
143      ++src0;
144      ++src1;
145      ++src2;
146      --count;
147   }
148
149   /* last pixel */
150   if (src0[0] != src2[0] && src1[-1] != src1[0]) {
151      *dst++ = (src1[-1] == src0[0] && src1[0] != src2[-1]) || (src1[-1] == src2[0] && src1[0] != src0[-1]) ? src1[-1] : src1[0];
152      *dst++ = src1[0];
153   } else {
154      *dst++ = src1[0];
155      *dst++ = src1[0];
156   }
157   if (borderpix)
158      *dst++ = 0;
159}
160
161INLINE void FUNC_NAME(scale3x_border)(DEST_TYPE *dst, DEST_TYPE *src0,
162   DEST_TYPE *src1, DEST_TYPE *src2, int count,
163   int borderpix)
164{
165   /* first pixel */
166   if (borderpix)
167      *dst++ = 0;
168   
169   if (src0[0] != src2[0] && src1[0] != src1[1]) {
170      *dst++ = src1[0];
171      *dst++ = (src1[0] == src0[0] && src1[0] != src0[1]) || (src1[1] == src0[0] && src1[0] != src0[0]) ? src0[0] : src1[0];
172      *dst++ = src1[1] == src0[0] ? src1[1] : src1[0];
173   } else {
174      *dst++ = src1[0];
175      *dst++ = src1[0];
176      *dst++ = src1[0];
177   }
178   ++src0;
179   ++src1;
180   ++src2;
181
182   /* central pixels */
183   count -= 2;
184   while (count) {
185      if (src0[0] != src2[0] && src1[-1] != src1[1]) {
186         *dst++ = src1[-1] == src0[0] ? src1[-1] : src1[0];
187         *dst++ = (src1[-1] == src0[0] && src1[0] != src0[1]) || (src1[1] == src0[0] && src1[0] != src0[-1]) ? src0[0] : src1[0];
188         *dst++ = src1[1] == src0[0] ? src1[1] : src1[0];
189      } else {
190         *dst++ = src1[0];
191         *dst++ = src1[0];
192         *dst++ = src1[0];
193      }
194
195      ++src0;
196      ++src1;
197      ++src2;
198      --count;
199   }
200
201   /* last pixel */
202   if (src0[0] != src2[0] && src1[-1] != src1[0]) {
203      *dst++ = src1[-1] == src0[0] ? src1[-1] : src1[0];
204      *dst++ = (src1[-1] == src0[0] && src1[0] != src0[0]) || (src1[0] == src0[0] && src1[0] != src0[-1]) ? src0[0] : src1[0];
205      *dst++ = src1[0];
206   } else {
207      *dst++ = src1[0];
208      *dst++ = src1[0];
209      *dst++ = src1[0];
210   }
211   if (borderpix)
212      *dst++ = 0;
213}
214
215INLINE void FUNC_NAME(scale3x_center)(DEST_TYPE *dst, DEST_TYPE *src0,
216   DEST_TYPE *src1, DEST_TYPE *src2, int count,
217   int borderpix)
218{
219   /* first pixel */
220   if (borderpix)
221      *dst++ = 0;
222   
223   if (src0[0] != src2[0] && src1[0] != src1[1]) {
224      *dst++ = (src1[0] == src0[0] && src1[0] != src2[0]) || (src1[0] == src2[0] && src1[0] != src0[0]) ? src1[0] : src1[0];
225      *dst++ = src1[0];
226      *dst++ = (src1[1] == src0[0] && src1[0] != src2[1]) || (src1[1] == src2[0] && src1[0] != src0[1]) ? src1[1] : src1[0];
227   } else {
228      *dst++ = src1[0];
229      *dst++ = src1[0];
230      *dst++ = src1[0];
231   }
232   ++src0;
233   ++src1;
234   ++src2;
235
236   /* central pixels */
237   count -= 2;
238   while (count) {
239      if (src0[0] != src2[0] && src1[-1] != src1[1]) {
240         *dst++ = (src1[-1] == src0[0] && src1[0] != src2[-1]) || (src1[-1] == src2[0] && src1[0] != src0[-1]) ? src1[-1] : src1[0];
241         *dst++ = src1[0];
242         *dst++ = (src1[1] == src0[0] && src1[0] != src2[1]) || (src1[1] == src2[0] && src1[0] != src0[1]) ? src1[1] : src1[0];
243      } else {
244         *dst++ = src1[0];
245         *dst++ = src1[0];
246         *dst++ = src1[0];
247      }
248
249      ++src0;
250      ++src1;
251      ++src2;
252      --count;
253   }
254
255   /* last pixel */
256   if (src0[0] != src2[0] && src1[-1] != src1[0]) {
257      *dst++ = (src1[-1] == src0[0] && src1[0] != src2[-1]) || (src1[-1] == src2[0] && src1[0] != src0[-1]) ? src1[-1] : src1[0];
258      *dst++ = src1[0];
259      *dst++ = (src1[0] == src0[0] && src1[0] != src2[0]) || (src1[0] == src2[0] && src1[0] != src0[0]) ? src1[0] : src1[0];
260   } else {
261      *dst++ = src1[0];
262      *dst++ = src1[0];
263      *dst++ = src1[0];
264   }
265   if (borderpix)
266      *dst++ = 0;
267}
268
269INLINE void FUNC_NAME(scale2x2)(DEST_TYPE **dst, DEST_TYPE *src0,
270   DEST_TYPE *src1, DEST_TYPE *src2, texture_info *texture,
271   const render_texinfo *texsource)
272{
273   FUNC_NAME(scale2x_border)(*dst, src0, src1, src2, texsource->width,
274      texture->borderpix);
275   FUNC_NAME(scale2x_border)(*dst += texture->rawwidth, src2,
276      src1, src0, texsource->width, texture->borderpix);
277}
278
279INLINE void FUNC_NAME(scale2x3)(DEST_TYPE **dst, DEST_TYPE *src0,
280   DEST_TYPE *src1, DEST_TYPE *src2, texture_info *texture,
281   const render_texinfo *texsource)
282{
283   FUNC_NAME(scale2x_border)(*dst, src0, src1, src2, texsource->width,
284      texture->borderpix);
285   FUNC_NAME(scale2x_center)(*dst += texture->rawwidth, src0,
286      src1, src2, texsource->width, texture->borderpix);
287   FUNC_NAME(scale2x_border)(*dst += texture->rawwidth, src2,
288      src1, src0, texsource->width, texture->borderpix);
289}
290
291INLINE void FUNC_NAME(scale3x2)(DEST_TYPE **dst, DEST_TYPE *src0,
292   DEST_TYPE *src1, DEST_TYPE *src2, texture_info *texture,
293   const render_texinfo *texsource)
294{
295   FUNC_NAME(scale3x_border)(*dst, src0, src1, src2, texsource->width,
296      texture->borderpix);
297   FUNC_NAME(scale3x_border)(*dst += texture->rawwidth, src2,
298      src1, src0, texsource->width, texture->borderpix);
299}
300
301INLINE void FUNC_NAME(scale3x3)(DEST_TYPE **dst, DEST_TYPE *src0,
302   DEST_TYPE *src1, DEST_TYPE *src2, texture_info *texture,
303   const render_texinfo *texsource)
304{
305   FUNC_NAME(scale3x_border)(*dst, src0, src1, src2, texsource->width,
306      texture->borderpix);
307   FUNC_NAME(scale3x_center)(*dst += texture->rawwidth, src0,
308      src1, src2, texsource->width, texture->borderpix);
309   FUNC_NAME(scale3x_border)(*dst += texture->rawwidth, src2,
310      src1, src0, texsource->width, texture->borderpix);
311}
312
313INLINE void FUNC_NAME(scaleXxX)(DEST_TYPE **dst, DEST_TYPE *src0,
314   DEST_TYPE *src1, DEST_TYPE *src2, texture_info *texture,
315   const render_texinfo *texsource)
316{
317   switch ((texture->xprescale << 8) | texture->yprescale)
318   {
319      case 0x0202:
320         FUNC_NAME(scale2x2)(dst, src0, src1, src2, texture,
321            texsource);
322         break;
323      case 0x0203:
324         FUNC_NAME(scale2x3)(dst, src0, src1, src2, texture,
325            texsource);
326         break;
327      case 0x0302:
328         FUNC_NAME(scale3x2)(dst, src0, src1, src2, texture,
329            texsource);
330         break;
331      case 0x0303:
332         FUNC_NAME(scale3x3)(dst, src0, src1, src2, texture,
333            texsource);
334         break;
335   }
336}
337
338#undef DEST_TYPE
339#undef FUNC_NAME
src/osd/sdl/scale2x.c
r7088r7089
1
2#ifndef SDL_TEXFORMAT
3
4// 16 and 32 bpp versions of the core scale2x functions
5// NOTE, advancemame has mmx asm versions of these, these could be easily
6// integrated by someone interested, except for borderpix support.
7
8#ifdef FUNC_NAME
9#undef FUNC_NAME
10#endif
11
12#define DEST_TYPE UINT16
13#define FUNC_NAME(name) name ## _16bpp
14#include "scale2x_core.c"
15
16#define DEST_TYPE UINT32
17#define FUNC_NAME(name) name ## _32bpp
18#include "scale2x_core.c"
19
20
21// and versions for all texformats of the wrapper function called by drawsdl
22#define EFFECT_FUNC_NAME FUNC_NAME(scale2x)
23#define EFFECT_DEST_NAME DEST_NAME(scaleXxX)
24
25#define SDL_TEXFORMAT SDL_TEXFORMAT_ARGB32
26#include "scale2x.c"
27#undef SDL_TEXFORMAT
28
29#define SDL_TEXFORMAT SDL_TEXFORMAT_RGB32
30#include "scale2x.c"
31#undef SDL_TEXFORMAT
32
33#define SDL_TEXFORMAT SDL_TEXFORMAT_RGB32_PALETTED
34#include "scale2x.c"
35#undef SDL_TEXFORMAT
36
37#define SDL_TEXFORMAT SDL_TEXFORMAT_PALETTE16
38#include "scale2x.c"
39#undef SDL_TEXFORMAT
40
41#define SDL_TEXFORMAT SDL_TEXFORMAT_PALETTE16A
42#include "scale2x.c"
43#undef SDL_TEXFORMAT
44
45#define SDL_TEXFORMAT SDL_TEXFORMAT_RGB15
46#include "scale2x.c"
47#undef SDL_TEXFORMAT
48
49#define SDL_TEXFORMAT SDL_TEXFORMAT_RGB15_PALETTED
50#include "scale2x.c"
51#undef SDL_TEXFORMAT
52
53#define SDL_TEXFORMAT SDL_TEXFORMAT_PALETTE16_ARGB1555
54#include "scale2x.c"
55#undef SDL_TEXFORMAT
56
57#define SDL_TEXFORMAT SDL_TEXFORMAT_RGB15_ARGB1555
58#include "scale2x.c"
59#undef SDL_TEXFORMAT
60
61#define SDL_TEXFORMAT SDL_TEXFORMAT_RGB15_PALETTED_ARGB1555
62#include "scale2x.c"
63#undef SDL_TEXFORMAT
64
65#undef EFFECT_FUNC_NAME
66#undef EFFECT_DEST_NAME
67
68//============================================================
69//  MANUAL TEXCOPY FUNCS
70//  (YUY format is weird and doesn't fit the assumptions of the
71//   standard macros so we handle it here
72//     
73//============================================================
74
75static void scale2x_yuv16(texture_info *texture, const render_texinfo *texsource)
76{
77   fatalerror("ERROR: Scale2X not supported for YUV textures\n");
78}
79
80static void scale2x_yuv16_paletted(texture_info *texture, const render_texinfo *texsource)
81{
82   fatalerror("ERROR: Scale2X not supported for YUV textures\n");
83}
84
85#else // recursive include
86
87#include "texsrc.h"
88
89// texcopy function prototype
90//void FUNC_NAME(texcopy)(texture_info *texture, const render_texinfo *texsource);
91
92static void EFFECT_FUNC_NAME(texture_info *texture, const render_texinfo *texsource)
93{
94   int y;
95   DEST_TYPE *dst = (DEST_TYPE *)texture->data + texture->borderpix * texture->rawwidth;
96   TEXSRC_TYPE *src = (TEXSRC_TYPE *)texsource->base;
97#ifndef SRC_EQUALS_DEST
98   int x;
99   DEST_TYPE *tmp, *effectbuf0, *effectbuf1, *effectbuf2;
100#endif
101
102   switch ((texture->xprescale << 8) | texture->yprescale)
103   {
104      case 0x0101:
105      case 0x0102:
106      case 0x0103:
107      case 0x0201:
108      case 0x0301:
109         FUNC_NAME(texcopy)(texture, texsource);
110         return;
111   }
112
113#ifndef SRC_EQUALS_DEST
114   // First convert to a temporary buffer so that we do not convert each
115   // pixel 4 times
116   effectbuf1 = (DEST_TYPE *)texture->effectbuf;
117   effectbuf2 = (DEST_TYPE *)texture->effectbuf + texsource->width;
118   effectbuf0 = (DEST_TYPE *)texture->effectbuf + 2 * texsource->width;
119   
120   tmp = effectbuf1;
121   for (x = 0; x < texsource->width; x++)
122   {
123      *tmp++ = TEXSRC_TO_DEST(*src);
124      src++;
125   }
126
127   src = (TEXSRC_TYPE *)texsource->base + texsource->rowpixels;
128   tmp = effectbuf2;
129   for (x = 0; x < texsource->width; x++)
130   {
131      *tmp++ = TEXSRC_TO_DEST(*src);
132      src++;
133   }
134   EFFECT_DEST_NAME(&dst, effectbuf1, effectbuf1, effectbuf2, texture,
135      texsource);
136#else
137   EFFECT_DEST_NAME(&dst, src, src, src + texsource->rowpixels,
138      texture, texsource);
139#endif   
140
141   // loop over Y
142   for (y = 1; y < (texsource->height-1); y++)
143   {
144      dst += texture->rawwidth;
145#ifndef SRC_EQUALS_DEST
146      // First convert to a temporary buffer so that we do not
147      // convert each pixel 4 times
148      src = (TEXSRC_TYPE *)texsource->base +
149         (y + 1) * texsource->rowpixels;
150      tmp = effectbuf0;
151      effectbuf0 = effectbuf1;
152      effectbuf1 = effectbuf2;
153      effectbuf2 = tmp;
154      for (x = 0; x < texsource->width; x++)
155      {
156         *tmp++ = TEXSRC_TO_DEST(*src);
157         src++;
158      }
159      EFFECT_DEST_NAME(&dst, effectbuf0, effectbuf1, effectbuf2, texture, texsource);
160#else
161      src += texsource->rowpixels;
162      EFFECT_DEST_NAME(&dst, src - texsource->rowpixels, src,
163         src + texsource->rowpixels, texture, texsource);
164#endif
165   }
166
167   // last line
168   dst += texture->rawwidth;
169#ifndef SRC_EQUALS_DEST
170   EFFECT_DEST_NAME(&dst, effectbuf1, effectbuf2, effectbuf2, texture, texsource);
171#else
172   EFFECT_DEST_NAME(&dst, src, src + texsource->rowpixels, src + texsource->rowpixels, texture, texsource);
173#endif
174}
175
176#undef SDL_TEXFORMAT
177
178
179#endif
src/osd/sdl/sdlsync.c
r7088r7089
1//============================================================
2//
3//  sdlsync.c - SDL core synchronization functions
4//
5//  Copyright (c) 1996-2007, Nicola Salmoria and the MAME Team.
6//  Visit http://mamedev.org for licensing and usage restrictions.
7//
8//  SDLMAME by Olivier Galibert and R. Belmont
9//
10//============================================================
11
12#ifndef SDLMAME_WIN32
13#ifndef _GNU_SOURCE
14#define _GNU_SOURCE    // for PTHREAD_MUTEX_RECURSIVE; needs to be here before other glibc headers are included
15#endif
16
17#include "SDL/SDL.h"
18
19#ifdef SDLMAME_MACOSX
20#include <mach/mach.h>
21#endif
22
23// standard C headers
24#include <math.h>
25#include <unistd.h>
26
27// MAME headers
28#include "osdcore.h"
29#include "osinline.h"
30#include "sdlsync.h"
31
32#include "eminline.h"
33
34#ifndef SDLMAME_OS2
35#include <pthread.h>
36#include <errno.h>
37#include <sys/time.h>
38#else
39#define INCL_DOS
40#include <os2.h>
41
42#include <stdlib.h>
43#define pthread_t       int
44#define pthread_self    _gettid
45#endif
46
47#ifndef NO_THREAD_COOPERATIVE
48typedef struct _hidden_mutex_t hidden_mutex_t;
49struct _hidden_mutex_t {
50   pthread_mutex_t id;
51};
52#else
53struct _osd_lock {
54    volatile pthread_t   holder;
55   INT32            count;
56#ifdef PTR64
57   INT8            padding[52];   // Fill a 64-byte cache line
58#else
59   INT8            padding[56];   // A bit more padding
60#endif
61};
62#endif
63 
64#ifndef SDLMAME_OS2
65struct _osd_event {
66   pthread_mutex_t    mutex;
67   pthread_cond_t       cond;
68   volatile INT32      autoreset;
69   volatile INT32      signalled;
70#ifdef PTR64
71   INT8            padding[40];   // Fill a 64-byte cache line
72#else
73   INT8            padding[48];   // A bit more padding
74#endif
75};
76#else
77struct _osd_event {
78    HMTX                hmtx;
79    HEV                 hev;
80    volatile INT32      autoreset;
81    INT8                padding[52];    // Fill a 64-byte cache line
82};
83#endif
84
85struct _osd_thread {
86   pthread_t         thread;
87};
88
89
90#ifndef NO_THREAD_COOPERATIVE
91
92//============================================================
93//  osd_lock_alloc
94//============================================================
95
96osd_lock *osd_lock_alloc(void)
97{
98   hidden_mutex_t *mutex;
99   pthread_mutexattr_t mtxattr;
100
101   mutex = (hidden_mutex_t *)calloc(1, sizeof(hidden_mutex_t));
102
103   pthread_mutexattr_init(&mtxattr);
104   pthread_mutexattr_settype(&mtxattr, PTHREAD_MUTEX_RECURSIVE);
105   pthread_mutex_init(&mutex->id, &mtxattr);
106
107   return (osd_lock *)mutex;
108}
109
110//============================================================
111//  osd_lock_acquire
112//============================================================
113
114void osd_lock_acquire(osd_lock *lock)
115{
116   hidden_mutex_t *mutex = (hidden_mutex_t *) lock;
117   int r;
118   
119   r =   pthread_mutex_lock(&mutex->id);
120   if (r==0)
121      return;
122   //mame_printf_error("Error on lock: %d: %s\n", r, strerror(r));
123}
124
125//============================================================
126//  osd_lock_try
127//============================================================
128
129int osd_lock_try(osd_lock *lock)
130{
131   hidden_mutex_t *mutex = (hidden_mutex_t *) lock;
132   int r;
133   
134   r = pthread_mutex_trylock(&mutex->id);
135   if (r==0)
136      return 1;
137   //if (r!=EBUSY)
138    //   mame_printf_error("Error on trylock: %d: %s\n", r, strerror(r));
139   return 0;
140}
141
142//============================================================
143//  osd_lock_release
144//============================================================
145
146void osd_lock_release(osd_lock *lock)
147{
148   hidden_mutex_t *mutex = (hidden_mutex_t *) lock;
149
150   pthread_mutex_unlock(&mutex->id);
151}
152
153//============================================================
154//  osd_lock_free
155//============================================================
156
157void osd_lock_free(osd_lock *lock)
158{
159   hidden_mutex_t *mutex = (hidden_mutex_t *) lock;
160
161   pthread_mutex_unlock(&mutex->id);
162   pthread_mutex_destroy(&mutex->id);
163   free(mutex);
164}
165#else
166 
167INLINE pthread_t osd_compare_exchange_pthread_t(pthread_t volatile *ptr, pthread_t compare, pthread_t exchange)
168{
169#ifdef PTR64
170   INT64 result = compare_exchange64((INT64 volatile *)ptr, (INT64)compare, (INT64)exchange);
171#else
172   INT32 result = compare_exchange32((INT32 volatile *)ptr, (INT32)compare, (INT32)exchange);
173#endif
174   return (pthread_t)result;
175}
176
177INLINE pthread_t osd_exchange_pthread_t(pthread_t volatile *ptr, pthread_t exchange)
178{
179#ifdef PTR64
180   INT64 result = osd_exchange64((INT64 volatile *)ptr, (INT64)exchange);
181#else
182   INT32 result = atomic_exchange32((INT32 volatile *)ptr, (INT32)exchange);
183#endif
184   return (pthread_t)result;
185}
186
187
188//============================================================
189//  osd_lock_alloc
190//============================================================
191
192osd_lock *osd_lock_alloc(void)
193{
194   osd_lock *lock;
195
196   lock = (osd_lock *)calloc(1, sizeof(osd_lock));
197
198   lock->holder = 0;
199   lock->count = 0;
200
201   return lock;
202}
203
204//============================================================
205//  osd_lock_acquire
206//============================================================
207
208void osd_lock_acquire(osd_lock *lock)
209{
210   pthread_t current, prev;
211
212   current = pthread_self();
213   prev = osd_compare_exchange_pthread_t(&lock->holder, 0, current);
214   if (prev != (size_t)NULL && prev != current)
215   {
216      do {
217         register INT32 spin = 10000; // Convenient spin count
218         register pthread_t tmp;
219#if defined(__i386__) || defined(__x86_64__)
220         __asm__ __volatile__ (
221            "1: pause                    ;"
222            "   mov    %[holder], %[tmp] ;"
223            "   test   %[tmp], %[tmp]    ;"
224            "   loopne 1b                ;"
225            : [spin]   "+c"  (spin)
226            , [tmp]    "=&r" (tmp)
227            : [holder] "m"   (lock->holder)
228            : "%cc"
229         );
230#elif defined(__ppc__) || defined(__PPC__)
231         __asm__ __volatile__ (
232            "1: nop                        \n"
233            "   nop                        \n"
234            "   lwzx  %[tmp], 0, %[holder] \n"
235            "   cmpwi %[tmp], 0            \n"
236            "   bdnzt eq, 1b               \n"
237            : [spin]   "+c"  (spin)
238            , [tmp]    "=&r" (tmp)
239            : [holder] "r"   (&lock->holder)
240            : "cr0"
241         );
242#elif defined(__ppc64__) || defined(__PPC64__)
243         __asm__ __volatile__ (
244            "1: nop                        \n"
245            "   nop                        \n"
246            "   ldx   %[tmp], 0, %[holder] \n"
247            "   cmpdi %[tmp], 0            \n"
248            "   bdnzt eq, 1b               \n"
249            : [spin]   "+c"  (spin)
250            , [tmp]    "=&r" (tmp)
251            : [holder] "r"   (&lock->holder)
252            : "cr0"
253         );
254#else
255         while (--spin > 0 && lock->holder != NULL)
256            osd_yield_processor();
257#endif
258#if 0
259         /* If you mean to use locks as a blocking mechanism for extended
260          * periods of time, you should do something like this.  However,
261          * it kills the performance of gaelco3d.
262          */
263         if (spin == 0)
264         {
265            struct timespec sleep = { 0, 100000 }, remaining;
266            nanosleep(&sleep, &remaining); // sleep for 100us
267         }
268#endif
269      } while (osd_compare_exchange_pthread_t(&lock->holder, 0, current) != (size_t)NULL);
270   }
271   lock->count++;
272}
273
274//============================================================
275//  osd_lock_try
276//============================================================
277
278int osd_lock_try(osd_lock *lock)
279{
280   pthread_t current, prev;
281
282   current = pthread_self();
283   prev = osd_compare_exchange_pthread_t(&lock->holder, 0, current);
284   if (prev == (size_t)NULL || prev == current)
285   {
286      lock->count++;
287      return 1;
288   }
289   return 0;
290}
291
292//============================================================
293//  osd_lock_release
294//============================================================
295
296void osd_lock_release(osd_lock *lock)
297{
298   pthread_t current;
299
300   current = pthread_self();
301   if (lock->holder == current)
302   {
303      if (--lock->count == 0)
304#if defined(__ppc__) || defined(__PPC__) || defined(__ppc64__) || defined(__PPC64__)
305      lock->holder = 0;
306      __asm__ __volatile__( " eieio " : : );
307#else
308      osd_exchange_pthread_t(&lock->holder, 0);
309#endif
310      return;
311   }
312
313   // trying to release a lock you don't hold is bad!
314//   assert(lock->holder == pthread_self());
315}
316
317//============================================================
318//  osd_lock_free
319//============================================================
320
321void osd_lock_free(osd_lock *lock)
322{
323   free(lock);
324}
325#endif
326
327#ifndef SDLMAME_OS2
328//============================================================
329//  osd_num_processors
330//============================================================
331
332int osd_num_processors(void)
333{
334   int processors = 1;
335
336#ifdef SDLMAME_MACOSX
337   {
338      struct host_basic_info host_basic_info;
339      unsigned int count;
340      kern_return_t r;
341      mach_port_t my_mach_host_self;
342     
343      count = HOST_BASIC_INFO_COUNT;
344      my_mach_host_self = mach_host_self();
345      if ( ( r = host_info(my_mach_host_self, HOST_BASIC_INFO, (host_info_t)(&host_basic_info), &count)) == KERN_SUCCESS )
346      {
347         processors = host_basic_info.avail_cpus;
348      }
349      mach_port_deallocate(mach_task_self(), my_mach_host_self);
350   }
351#elif defined(_SC_NPROCESSORS_ONLN)
352   processors = sysconf(_SC_NPROCESSORS_ONLN);
353#endif
354   return processors;
355}
356
357//============================================================
358//  osd_event_alloc
359//============================================================
360
361osd_event *osd_event_alloc(int manualreset, int initialstate)
362{
363   osd_event *ev;
364   pthread_mutexattr_t mtxattr;
365
366   ev = (osd_event *)calloc(1, sizeof(osd_event));
367
368   pthread_mutexattr_init(&mtxattr);
369   pthread_mutex_init(&ev->mutex, &mtxattr);
370   pthread_cond_init(&ev->cond, NULL);
371   ev->signalled = initialstate;
372   ev->autoreset = !manualreset;
373     
374   return ev;
375}
376
377//============================================================
378//  osd_event_free
379//============================================================
380
381void osd_event_free(osd_event *event)
382{
383   pthread_mutex_destroy(&event->mutex);
384   pthread_cond_destroy(&event->cond);
385   free(event);
386}
387
388//============================================================
389//  osd_event_set
390//============================================================
391
392#if 1
393void osd_event_set(osd_event *event)
394{
395   pthread_mutex_lock(&event->mutex);
396   if (event->signalled == FALSE)
397   {
398      event->signalled = TRUE;
399      if (event->autoreset)
400         pthread_cond_signal(&event->cond);
401      else
402         pthread_cond_broadcast(&event->cond);
403   }
404   pthread_mutex_unlock(&event->mutex);
405}
406
407//============================================================
408//  osd_event_reset
409//============================================================
410
411void osd_event_reset(osd_event *event)
412{
413   pthread_mutex_lock(&event->mutex);
414   event->signalled = FALSE;
415   pthread_mutex_unlock(&event->mutex);
416}
417
418//============================================================
419//  osd_event_wait
420//============================================================
421
422int osd_event_wait(osd_event *event, osd_ticks_t timeout)
423{
424   pthread_mutex_lock(&event->mutex);
425   if (!timeout)
426   {
427      if (!event->signalled)
428      {
429            pthread_mutex_unlock(&event->mutex);
430            return FALSE;
431      }
432   }
433   else
434   {
435      if (!event->signalled)
436      {
437         struct timespec   ts;
438         struct timeval    tp;
439         UINT64 msec = timeout * 1000 / osd_ticks_per_second();
440         UINT64 nsec;
441         
442         gettimeofday(&tp, NULL);
443   
444         ts.tv_sec  = tp.tv_sec;
445         nsec = (UINT64) tp.tv_usec * (UINT64) 1000 + (msec * (UINT64) 1000000);
446         ts.tv_nsec = nsec % (UINT64) 1000000000;
447         ts.tv_sec += nsec / (UINT64) 1000000000;
448
449         do {
450            int ret = pthread_cond_timedwait(&event->cond, &event->mutex, &ts);
451            if ( ret == ETIMEDOUT )
452            {
453               if (!event->signalled)
454               {
455                  pthread_mutex_unlock(&event->mutex);
456                  return FALSE;
457               }
458               else
459                  break;
460            }
461            if (ret == 0)
462               break;
463            if ( ret != EINTR)
464            {
465               printf("Error %d while waiting for pthread_cond_timedwait:  %s\n", ret, strerror(ret));
466            }
467           
468         } while (TRUE);
469      }
470   }
471
472   if (event->autoreset)
473      event->signalled = 0;
474
475   pthread_mutex_unlock(&event->mutex);
476
477   return TRUE;
478}
479#else
480
481/*
482 * The following code has been added
483 * for demonstration purposes only.
484 * It implements events without the need
485 * for pthreads. However, it horribly fails, if
486 * threads > num processors as is the case if you
487 * enable "-mt"
488 */
489
490void osd_event_set(osd_event *event)
491{
492//   while (compare_exchange32(&event->signalled, 0, 1) == 1)
493//      osd_yield_processor();
494   atomic_exchange32(&event->signalled, 1);
495}
496
497void osd_event_reset(osd_event *event)
498{
499   compare_exchange32(&event->signalled, 1, 0);
500}
501
502int osd_event_wait(osd_event *event, osd_ticks_t timeout)
503{
504   osd_ticks_t stopspin;
505   osd_ticks_t gotosleep;
506   //struct timespec sleep = { 0, 100000 }, remaining;
507   
508   if (!timeout && !event->signalled)
509      return FALSE;
510
511   stopspin = osd_ticks() + timeout;
512   gotosleep = osd_ticks() + osd_ticks_per_second()/1000;
513
514   if (event->autoreset)
515   {
516      while (compare_exchange32(&event->signalled, 1, 0) == 1)
517      {
518         if (osd_ticks()>=stopspin)
519            return FALSE;
520         osd_yield_processor();
521         //if (osd_ticks()>=gotosleep)
522            //usleep(100);
523            //nanosleep(&sleep, &remaining); // sleep for 100us
524      }
525   }
526   else
527   {
528      while (!event->signalled)
529      {
530         if (osd_ticks()>=stopspin)
531            return FALSE;
532         osd_yield_processor();
533         //if (osd_ticks()>=gotosleep)
534            //usleep(100);
535            //nanosleep(&sleep, &remaining); // sleep for 100us
536      }
537   }
538   return TRUE;
539}
540#endif
541
542//============================================================
543//  osd_thread_create
544//============================================================
545
546osd_thread *osd_thread_create(osd_thread_callback callback, void *cbparam)
547{
548   osd_thread *thread;
549   pthread_attr_t   attr;
550
551   thread = (osd_thread *)calloc(1, sizeof(osd_thread));
552   pthread_attr_init(&attr);
553   pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED);
554   if ( pthread_create(&thread->thread, &attr, callback, cbparam) != 0 )
555   {
556      free(thread);
557      return NULL;
558   }
559   return thread;
560}
561
562//============================================================
563//  osd_thread_adjust_priority
564//============================================================
565
566int osd_thread_adjust_priority(osd_thread *thread, int adjust)
567{
568   struct sched_param   sched;
569   int               policy;
570         
571   if ( pthread_getschedparam( thread->thread, &policy, &sched ) == 0 )
572   {
573      sched.sched_priority += adjust;
574      if ( pthread_setschedparam(thread->thread, policy, &sched ) == 0)
575         return TRUE;
576      else
577         return FALSE;
578   }
579   else
580      return FALSE;
581}
582
583//============================================================
584//  osd_thread_cpu_affinity
585//============================================================
586
587int osd_thread_cpu_affinity(osd_thread *thread, UINT32 mask)
588{
589#if !defined(NO_THREAD_COOPERATIVE) && !defined(NO_AFFINITY_NP)
590   cpu_set_t   cmask;
591   pthread_t   lthread;
592   int         bitnum;
593
594   CPU_ZERO(&cmask);
595   for (bitnum=0; bitnum<32; bitnum++)
596      if (mask & (1<<bitnum))
597         CPU_SET(bitnum, &cmask);
598   
599   if (thread == NULL)
600      lthread = pthread_self();
601   else
602      lthread = thread->thread;
603   
604   if (pthread_setaffinity_np(lthread, sizeof(cmask), &cmask) <0)
605   {
606      /* Not available during link in all targets */
607      fprintf(stderr, "error %d setting cpu affinity to mask %08x", errno, mask);
608      return FALSE;
609   }
610   else
611      return TRUE;
612#else
613   return TRUE;
614#endif
615}
616
617//============================================================
618//  osd_thread_wait_free
619//============================================================
620
621void osd_thread_wait_free(osd_thread *thread)
622{
623   pthread_join(thread->thread, NULL);
624   free(thread);
625}
626
627#else
628
629//============================================================
630//  osd_num_processors
631//============================================================
632
633int osd_num_processors(void)
634{
635    ULONG numprocs = 1;
636
637    DosQuerySysInfo(QSV_NUMPROCESSORS, QSV_NUMPROCESSORS, &numprocs, sizeof(numprocs));
638
639    return numprocs;
640}
641
642//============================================================
643//  osd_event_alloc
644//============================================================
645
646osd_event *osd_event_alloc(int manualreset, int initialstate)
647{
648    osd_event *ev;
649
650    ev = (osd_event *)calloc(1, sizeof(osd_event));
651
652    DosCreateMutexSem(NULL, &ev->hmtx, 0, FALSE);
653    DosCreateEventSem(NULL, &ev->hev, 0, initialstate);
654    ev->autoreset = !manualreset;
655
656    return ev;
657}
658
659//============================================================
660//  osd_event_free
661//============================================================
662
663void osd_event_free(osd_event *event)
664{
665    DosCloseMutexSem(event->hmtx);
666    DosCloseEventSem(event->hev);
667    free(event);
668}
669
670//============================================================
671//  osd_event_set
672//============================================================
673
674void osd_event_set(osd_event *event)
675{
676    DosPostEventSem(event->hev);
677}
678
679//============================================================
680//  osd_event_reset
681//============================================================
682
683void osd_event_reset(osd_event *event)
684{
685    ULONG ulCount;
686
687    DosResetEventSem(event->hev, &ulCount);
688}
689
690//============================================================
691//  osd_event_wait
692//============================================================
693
694int osd_event_wait(osd_event *event, osd_ticks_t timeout)
695{
696    ULONG rc;
697
698    if(event->autoreset)
699        DosRequestMutexSem(event->hmtx, -1);
700
701    rc = DosWaitEventSem(event->hev, timeout * 1000 / osd_ticks_per_second());
702
703    if(event->autoreset)
704    {
705        ULONG ulCount;
706
707        DosResetEventSem(event->hev, &ulCount);
708        DosReleaseMutexSem(event->hmtx);
709    }
710
711    return (rc == 0);
712}
713
714//============================================================
715//  osd_thread_create
716//============================================================
717
718osd_thread *osd_thread_create(osd_thread_callback callback, void *cbparam)
719{
720    osd_thread *thread;
721
722    thread = (osd_thread *)calloc(1, sizeof(osd_thread));
723    thread->thread = _beginthread(callback, NULL, 65535, cbparam);
724    if ( thread->thread == -1 )
725    {
726        free(thread);
727        return NULL;
728    }
729    return thread;
730}
731
732//============================================================
733//  osd_thread_adjust_priority
734//============================================================
735
736int osd_thread_adjust_priority(osd_thread *thread, int adjust)
737{
738    PTIB ptib;
739
740    DosGetInfoBlocks(&ptib, NULL);
741
742    if ( DosSetPriority(PRTYS_THREAD, PRTYC_NOCHANGE,
743                        ((BYTE)ptib->tib_ptib2->tib2_ulpri) + adjust, thread->thread ))
744        return FALSE;
745
746
747    return TRUE;
748}
749
750//============================================================
751//  osd_thread_cpu_affinity
752//============================================================
753
754int osd_thread_cpu_affinity(osd_thread *thread, UINT32 mask)
755{
756    return TRUE;
757}
758
759//============================================================
760//  osd_thread_wait_free
761//============================================================
762
763void osd_thread_wait_free(osd_thread *thread)
764{
765    TID tid = thread->thread;
766
767    DosWaitThread(&tid, 0);
768    free(thread);
769}
770
771#endif  /* SDLMAME_OS2 */
772#else   /* SDLMAME_WIN32 */
773#include "../windows/winsync.c"
774#endif
775
src/osd/sdl/sdlmisc.c
r7088r7089
1//============================================================
2//
3//  sdlmisc.c - SDL utility functions
4//
5//  Copyright (c) 1996-2007, Nicola Salmoria and the MAME Team.
6//  Visit http://mamedev.org for licensing and usage restrictions.
7//
8//  SDLMAME by Olivier Galibert and R. Belmont
9//
10//  Note: previous versions of this file were from XMAME and marked as GPL.
11//        all of that code is now gone/rewritten so that attribution has been removed.
12//
13//============================================================
14
15#include <stdio.h>
16#include <stdlib.h>
17#include <string.h>
18#include <unistd.h>
19#include <ctype.h>
20#include <stdlib.h>
21
22#include "osdcore.h"
23
24#if defined(SDLMAME_UNIX) && !defined(SDLMAME_DARWIN)
25#include <sys/mman.h>
26#endif
27#ifdef SDLMAME_WIN32
28#define WIN32_LEAN_AND_MEAN
29#include <windows.h>
30#endif
31
32#if defined(SDLMAME_UNIX)
33#include <signal.h>
34#include <unistd.h>
35#endif
36
37#ifdef SDLMAME_OS2
38#define INCL_DOS
39#include <os2.h>
40#endif
41
42//============================================================
43//  osd_alloc_executable
44//
45//  allocates "size" bytes of executable memory.  this must take
46//  things like NX support into account.
47//============================================================
48
49void *osd_alloc_executable(size_t size)
50{
51#if defined(SDLMAME_DARWIN)
52   return (void *)malloc(size);
53#elif defined(SDLMAME_BSD)
54   return (void *)mmap(0, size, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_ANON|MAP_SHARED, -1, 0);
55#elif defined(SDLMAME_UNIX)
56   return (void *)mmap(0, size, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_ANON|MAP_SHARED, 0, 0);
57#elif defined(SDLMAME_WIN32)
58   return VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
59#elif defined(SDLMAME_OS2)
60   void *p;
61   
62   DosAllocMem( &p, size, fALLOC );
63   return p;
64#else
65#error Undefined SDLMAME SUBARCH!
66#endif
67}
68
69//============================================================
70//  osd_free_executable
71//
72//  frees memory allocated with osd_alloc_executable
73//============================================================
74
75void osd_free_executable(void *ptr, size_t size)
76{
77#ifdef SDLMAME_DARWIN
78   free(ptr);
79#elif defined(SDLMAME_UNIX)
80   munmap(ptr, size);
81#elif defined(SDLMAME_WIN32)
82   VirtualFree(ptr, 0, MEM_RELEASE);
83#elif defined(SDLMAME_OS2)
84   DosFreeMem( ptr );
85#else
86#error Undefined SDLMAME SUBARCH!
87#endif
88}
89
90//============================================================
91//  osd_break_into_debugger
92//============================================================
93
94void osd_break_into_debugger(const char *message)
95{
96#ifdef SDLMAME_WIN32
97   if (IsDebuggerPresent())
98   {
99      OutputDebugString(message);
100      DebugBreak();
101   }
102#elif defined(SDLMAME_UNIX)
103   #ifdef MAME_DEBUG
104   printf("MAME exception: %s\n", message);
105   printf("Attempting to fall into debugger\n");
106   kill(getpid(), SIGTRAP);
107   #else
108   printf("Ignoring MAME exception: %s\n", message);
109   #endif
110#endif
111}
112
src/osd/sdl/sdltime.c
r7088r7089
1//============================================================
2//
3//  sdltime.c - POSIX, SDL, and Win32 timing code
4//
5//  Copyright (c) 1996-2007, Nicola Salmoria and the MAME Team.
6//  Visit http://mamedev.org for licensing and usage restrictions.
7//
8//  SDLMAME by Olivier Galibert and R. Belmont
9//
10//============================================================
11
12// standard sdl header
13#include <SDL/SDL.h>
14#include <unistd.h>
15
16#ifdef SDLMAME_UNIX
17#ifndef SDLMAME_DARWIN
18#include <time.h>
19#include <sys/time.h>
20#endif
21#endif
22
23#ifdef SDLMAME_WIN32
24#define WIN32_LEAN_AND_MEAN
25#include <windows.h>
26#include <mmsystem.h>
27#endif
28
29#ifdef SDLMAME_DARWIN
30#include <mach/mach.h>
31#include <mach/mach_time.h>
32#endif
33
34#ifdef SDLMAME_OS2
35#define INCL_DOS
36#include <os2.h>
37#endif
38
39// MAME headers
40#include "osdepend.h"
41
42
43//============================================================
44//  PROTOTYPES
45//============================================================
46
47static osd_ticks_t init_cycle_counter(void);
48#if defined(SDLMAME_UNIX) && !defined(SDLMAME_DARWIN)
49static osd_ticks_t time_cycle_counter(void);
50#endif
51#if defined(SDLMAME_WIN32) || defined(SDLMAME_OS2)
52static osd_ticks_t performance_cycle_counter(void);
53#endif
54#ifdef SDLMAME_DARWIN
55static osd_ticks_t mach_cycle_counter(void);
56#endif
57
58//============================================================
59//  GLOBAL VARIABLES
60//============================================================
61
62
63
64//============================================================
65//  STATIC VARIABLES
66//============================================================
67
68static osd_ticks_t suspend_adjustment;
69static osd_ticks_t suspend_time;
70
71// global cycle_counter function and divider
72static osd_ticks_t      (*cycle_counter)(void) = init_cycle_counter;
73static osd_ticks_t      (*ticks_counter)(void) = init_cycle_counter;
74static osd_ticks_t      ticks_per_second;
75
76
77//============================================================
78//  init_cycle_counter
79//
80//  to avoid total grossness, this function is split by subarch
81//============================================================
82
83#ifdef SDLMAME_WIN32
84static osd_ticks_t init_cycle_counter(void)
85{
86   osd_ticks_t start, end;
87   osd_ticks_t a, b;
88   int priority = GetThreadPriority(GetCurrentThread());
89   LARGE_INTEGER frequency;
90
91   suspend_adjustment = 0;
92   suspend_time = 0;
93
94   if (QueryPerformanceFrequency( &frequency ))
95   {
96      // use performance counter if available as it is constant
97      cycle_counter = performance_cycle_counter;
98      ticks_counter = performance_cycle_counter;
99
100      ticks_per_second = frequency.QuadPart;
101
102      // return the current cycle count
103      return (*cycle_counter)();
104   }
105   else
106   {
107      fprintf(stderr, "Error!  Unable to QueryPerformanceFrequency!\n");
108      exit(-1);
109   }
110
111   // temporarily set our priority higher
112   SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
113
114   // wait for an edge on the timeGetTime call
115   a = SDL_GetTicks();
116   do
117   {
118      b = SDL_GetTicks();
119   } while (a == b);
120
121   // get the starting cycle count
122   start = (*cycle_counter)();
123
124   // now wait for 1/4 second total
125   do
126   {
127      a = SDL_GetTicks();
128   } while (a - b < 250);
129
130   // get the ending cycle count
131   end = (*cycle_counter)();
132
133   // compute ticks_per_sec
134   ticks_per_second = (end - start) * 4;
135
136   // restore our priority
137   SetThreadPriority(GetCurrentThread(), priority);
138
139   // return the current cycle count
140   return (*cycle_counter)();
141}
142#endif
143
144#ifdef SDLMAME_DARWIN
145static osd_ticks_t init_cycle_counter(void)
146{
147   osd_ticks_t start, end;
148   osd_ticks_t a, b;
149
150   suspend_adjustment = 0;
151   suspend_time = 0;
152
153   cycle_counter = mach_cycle_counter;
154   ticks_counter = mach_cycle_counter;
155
156   // wait for an edge on the timeGetTime call
157   a = SDL_GetTicks();
158   do
159   {
160      b = SDL_GetTicks();
161   } while (a == b);
162
163   // get the starting cycle count
164   start = (*cycle_counter)();
165
166   // now wait for 1/4 second total
167   do
168   {
169      a = SDL_GetTicks();
170   } while (a - b < 250);
171
172   // get the ending cycle count
173   end = (*cycle_counter)();
174
175   // compute ticks_per_sec
176   ticks_per_second = (end - start) * 4;
177
178   // return the current cycle count
179   return (*cycle_counter)();
180}
181#endif
182
183#if defined(SDLMAME_UNIX) && !defined(SDLMAME_DARWIN)
184static osd_ticks_t init_cycle_counter(void)
185{
186   osd_ticks_t start, end;
187   osd_ticks_t a, b;
188
189   suspend_adjustment = 0;
190   suspend_time = 0;
191
192   cycle_counter = time_cycle_counter;
193   ticks_counter = time_cycle_counter;
194
195   // wait for an edge on the timeGetTime call
196   a = SDL_GetTicks();
197   do
198   {
199      b = SDL_GetTicks();
200   } while (a == b);
201
202   // get the starting cycle count
203   start = (*cycle_counter)();
204
205   // now wait for 1/4 second total
206   do
207   {
208      a = SDL_GetTicks();
209   } while (a - b < 250);
210
211   // get the ending cycle count
212   end = (*cycle_counter)();
213
214   // compute ticks_per_sec
215   ticks_per_second = (end - start) * 4;
216
217   // return the current cycle count
218   return (*cycle_counter)();
219}
220#endif
221
222#ifdef SDLMAME_OS2
223static osd_ticks_t init_cycle_counter(void)
224{
225   osd_ticks_t start, end;
226   osd_ticks_t a, b;
227
228   ULONG  frequency;
229   PTIB   ptib;
230   ULONG  ulClass;
231   ULONG  ulDelta;
232
233   DosGetInfoBlocks( &ptib, NULL );
234   ulClass = HIBYTE( ptib->tib_ptib2->tib2_ulpri );
235   ulDelta = LOBYTE( ptib->tib_ptib2->tib2_ulpri );
236
237   suspend_adjustment = 0;
238   suspend_time = 0;
239
240   if ( DosTmrQueryFreq( &frequency ) == 0 )
241   {
242      // use performance counter if available as it is constant
243      cycle_counter = performance_cycle_counter;
244      ticks_counter = performance_cycle_counter;
245
246      ticks_per_second = frequency;
247
248      // return the current cycle count
249      return (*cycle_counter)();
250   }
251   else
252   {
253      fprintf(stderr, "No Timer available!\n");
254      exit(-1);
255   }
256
257   // temporarily set our priority higher
258   DosSetPriority( PRTYS_THREAD, PRTYC_TIMECRITICAL, PRTYD_MAXIMUM, 0 );
259
260   // wait for an edge on the timeGetTime call
261   a = SDL_GetTicks();
262   do
263   {
264      b = SDL_GetTicks();
265   } while (a == b);
266
267   // get the starting cycle count
268   start = (*cycle_counter)();
269
270   // now wait for 1/4 second total
271   do
272   {
273      a = SDL_GetTicks();
274   } while (a - b < 250);
275
276   // get the ending cycle count
277   end = (*cycle_counter)();
278
279   // compute ticks_per_sec
280   ticks_per_second = (end - start) * 4;
281
282   // restore our priority
283   DosSetPriority( PRTYS_THREAD, ulClass, ulDelta, 0 );
284
285   // return the current cycle count
286   return (*cycle_counter)();
287}
288#endif
289
290//============================================================
291//  performance_cycle_counter
292//============================================================
293#ifdef SDLMAME_WIN32
294static osd_ticks_t performance_cycle_counter(void)
295{
296   LARGE_INTEGER performance_count;
297   QueryPerformanceCounter(&performance_count);
298   return (osd_ticks_t)performance_count.QuadPart;
299}
300#endif
301
302#ifdef SDLMAME_OS2
303static osd_ticks_t performance_cycle_counter(void)
304{
305    QWORD qwTime;
306
307    DosTmrQueryTime( &qwTime );
308    return (osd_ticks_t)qwTime.ulLo;
309}
310#endif
311
312//============================================================
313//  mach_cycle_counter
314//============================================================
315#ifdef SDLMAME_DARWIN
316static osd_ticks_t mach_cycle_counter(void)
317{
318   return mach_absolute_time();
319}
320#endif
321
322//============================================================
323//  time_cycle_counter
324//============================================================
325#if defined(SDLMAME_UNIX) && !defined(SDLMAME_DARWIN)
326
327static osd_ticks_t unix_ticks(void)
328{
329      struct timeval    tp;
330      static osd_ticks_t start_sec = 0;
331     
332      gettimeofday(&tp, NULL);
333      if (start_sec==0)
334         start_sec = tp.tv_sec;
335      return (tp.tv_sec - start_sec) * (osd_ticks_t) 1000000 + tp.tv_usec;
336}
337
338
339static osd_ticks_t time_cycle_counter(void)
340{
341   return unix_ticks();
342}
343#endif
344
345//============================================================
346//  osd_cycles
347//============================================================
348
349osd_ticks_t osd_ticks(void)
350{
351   return suspend_time ? suspend_time : (*cycle_counter)() - suspend_adjustment;
352}
353
354
355
356//============================================================
357//  osd_ticks_per_second
358//============================================================
359
360osd_ticks_t osd_ticks_per_second(void)
361{
362   if (ticks_per_second == 0)
363   {
364      return 1;   // this isn't correct, but it prevents the crash
365   }
366   return ticks_per_second;
367}
368
369
370
371//============================================================
372//  osd_profiling_ticks
373//============================================================
374
375#if defined(__i386__) || defined(__x86_64__)
376static UINT64 last_result = 0;
377static UINT64 current = 0;
378
379static UINT64 rdtsc(void)
380{
381    UINT64 result;
382
383    // use RDTSC
384    __asm__ __volatile__ (
385        "rdtsc"
386        : "=A" (result)
387    );
388    return result;
389}
390#endif
391
392osd_ticks_t osd_profiling_ticks(void)
393{
394#if defined(__i386__) || defined(__x86_64__)
395    UINT64 result;
396    UINT64 diff;
397
398    result = rdtsc();
399    if (result > last_result)
400    {
401        diff = result - last_result;
402        current += diff;
403    } else
404        printf("less @ %lld\n", current);
405    last_result = result;
406
407    /* osd_ticks_t is defined as INT64 */
408    current &= U64(0x7FFFFFFFFFFFFFFF);
409
410    return current;
411#else
412    return (*ticks_counter)();
413#endif
414}
415
416//============================================================
417//  osd_sleep
418//============================================================
419
420void osd_sleep(osd_ticks_t duration)
421{
422   UINT32 msec;
423
424   // make sure we've computed ticks_per_second
425   if (ticks_per_second == 0)
426      (void)osd_ticks();
427
428   // convert to milliseconds, rounding down
429   msec = (UINT32)(duration * 1000 / ticks_per_second);
430
431   // only sleep if at least 2 full milliseconds
432   if (msec >= 2)
433   {
434      // take a couple of msecs off the top for good measure
435      msec -= 2;
436      #ifdef SDLMAME_WIN32
437      Sleep(msec);
438      #else
439      usleep(msec*1000);
440      #endif
441   }
442}
443
src/osd/sdl/keybled.c
r7088r7089
1#include <SDL/SDL.h>
2#include <SDL/SDL_syswm.h>
3
4#include "output.h"
5#include "osdsdl.h"
6
7// FIXME: LED support for SDL >= 1.3
8#define LED_SUPPORTED   (defined(SDLMAME_X11) && (!SDL_VERSION_ATLEAST(1,3,0)))   
9
10#if LED_SUPPORTED
11static void led_change_notify(const char *outname, INT32 value, void *param);
12#endif
13
14void sdlled_init(void)
15{
16   #if LED_SUPPORTED
17   SDL_SysWMinfo info;
18   SDL_VERSION(&info.version);
19     
20   if ( SDL_GetWMInfo(&info) && (info.subsystem == SDL_SYSWM_X11) )
21   {
22      /* GRR leds 1 and 2 are swapped in X */
23      output_set_notifier(SDLMAME_LED(0), led_change_notify, (void *)2);
24      output_set_notifier(SDLMAME_LED(1), led_change_notify, (void *)1);
25      output_set_notifier(SDLMAME_LED(2), led_change_notify, (void *)3);
26   }
27   #endif
28}
29
30#if LED_SUPPORTED
31static void led_change_notify(const char *outname, INT32 value, void *param)
32{
33   XKeyboardControl values;
34   SDL_SysWMinfo info;
35   
36   values.led = (long)param;
37   if (value)
38      values.led_mode = LedModeOn;
39   else
40      values.led_mode = LedModeOff;
41   
42   SDL_VERSION(&info.version);
43   SDL_GetWMInfo(&info);
44   info.info.x11.lock_func();     
45   XChangeKeyboardControl(info.info.x11.display, KBLed|KBLedMode, &values);
46   info.info.x11.unlock_func();
47}
48#endif

Previous 15994 Revisions Next


© 1998-2018 The MESS Team