Timed functions

Hi!
SDL is fantastic: I’ve ported my SVGALIB emulator to SDL in just
2 hours (I’m developing an GPL spectrum emulator), but I have
some questions…

I want a function to be executed 50 times per second (it is the
function that updates the keyboard array and screen for my
emulator) and I don’t know how to do this under SDL… ?Must I
use a separate thread with a wait like SDL_Delay into it?

Another question is the “putpixel” example given on the SDL docs.
It is supposed to accept an Uint32 colour, but you must decide
before calling it the format of that colour, so you need extra
code. I’ve done the following, but I don’t know if will work on
all platforms (I think BGR cards will show wrong colours):

learScreen( char color )
{
int y, x;
Uint32 color32;

if ( SDL_MUSTLOCK(video) )
{ if ( SDL_LockSurface(video) < 0 ) exit(1); }

switch(video->format->BytesPerPixel)
{
case 1: color32 = color;
break;
case 2: color32 = ((colores[color][0]/4)<<12) |
((colores[color][1]/4)<<6) | colores[color][2];
break;
case 3: /* 24 BPP NOT SUPPORTED YET!!! */
break;
case 4: color32=(colores[color][0]<<16)|(colores[color][1]<<8)|
(colores[color][2]);
break;
}

for( y=0; y<200; y++ )
for( x=0; x<320; x++ )
{
PutPixel(video,x, y,color32);
}

if ( SDL_MUSTLOCK(video) )
{ SDL_UnlockSurface(video); }
SDL_UpdateRect(video, 0, 0, 0, 0);
}

The putpixel routine is a #define of the putpixel example given
on your source code… I pass it x, y, and the Uint32 color to write.

This code works fine in my computer (surely an RGB card), but
as I don’t have an BGR card, I can’t test it… but surely it will
display wrong colors, right?

How can I fix it?

PS: Yes, I know the above is a really shit of code :slight_smile: but I’m
just testing SDL at this moment. And as I’ve said, it’s fantastic :)–
Windows es multitarea real: puede ejecutar 2 bugs simult?neamente.

-----------------------------------------------------

NoP / Compiler – nop @ todolinux.org
POWERED BY - Linux RedHat 6.0 - Reg. User #74.821
http://www.ctv.es/USERS/sromero

~-----------------------------------------------------~

I want a function to be executed 50 times per second (it is the
function that updates the keyboard array and screen for my
emulator) and I don’t know how to do this under SDL… ?Must I
use a separate thread with a wait like SDL_Delay into it?

Don’t you want the refresh interrupt linked to the emulation speed?
A Speccy is so simple that most people prefer to do a cycle-exact emulation,
and I suppose that you could count T-states instead?

In article <slrn8g5lpu.2g6.nop at compiler.linux.es>,
nop at todolinux.org (Santiago Romero) writes:

Hi!
SDL is fantastic: I’ve ported my SVGALIB emulator to SDL in just
2 hours (I’m developing an GPL spectrum emulator), but I have
some questions…

I want a function to be executed 50 times per second (it is the
function that updates the keyboard array and screen for my
emulator) and I don’t know how to do this under SDL… ?Must I
use a separate thread with a wait like SDL_Delay into it?

Your best bet is probably to use a SDL threaded timer (SDL_SetTimer() or
SDL_AddTimer() in SDL 1.1). Actually if you really just want to update
the keyboard array, it would probably be more efficient to do so only when
events are received, so you can use a SDL event filter for that.

I did write a SVGAlib emulator as well in the past :wink:

Another question is the “putpixel” example given on the SDL docs.
It is supposed to accept an Uint32 colour, but you must decide
before calling it the format of that colour, so you need extra
code. I’ve done the following, but I don’t know if will work on
all platforms (I think BGR cards will show wrong colours):

You should probably call SDL_MapRGB() for that, passing the pixel format
structure from the surface you are using… It will do all the work for you !–
Stephane Peter
Programmer
Loki Entertainment Software

“Microsoft has done to computers what McDonald’s has done to gastronomy”

El 23 Apr 2000 11:12:58 -0700, Mattias Engdeg?rd escribi?:

I want a function to be executed 50 times per second (it is the
function that updates the keyboard array and screen for my
emulator) and I don’t know how to do this under SDL… ?Must I
use a separate thread with a wait like SDL_Delay into it?

Don’t you want the refresh interrupt linked to the emulation speed?

yes, that’s the way I want to do it. There is a 50Hz interrupt and
I want to link my emulator speed to the real emulator speed by
executing opcodes up to 224 T-States, and then wait until a 50Hz
interrupt occurs. That’s the way the R80 emulator works, and I
think its a good idea to sync the emulator (I accept any advice,
in the other hand :). The R80 emulator uses a global variable
(changed by the 50Hz function) and after executing 224 t-states
the emulator waits till that variable changes in a while loop
with a delay into it.

A Speccy is so simple that most people prefer to do a cycle-exact
emulation, and I suppose that you could count T-states instead?

I’m counting them, but I must still to wait to sync with the real
spectrum speed (3.59Mhz)…

I don’t want (still) to do a perfect opcode by opcode spectrum
emulation, I’ve seen R80 working (very good) and it syncs on each
scanline, so I’ll try this (at least in my 0.0.1 version :).–

-----------------------------------------------------

NoP / Compiler – nop @ todolinux.org
POWERED BY - Linux RedHat 6.0 - Reg. User #74.821
http://www.ctv.es/USERS/sromero

~-----------------------------------------------------~

(changed by the 50Hz function) and after executing 224 t-states
the emulator waits till that variable changes in a while loop
with a delay into it.

Warning - Tight loops like that eat major CPU :wink: I know, I’ve gotten
complaints about them before. :wink:

-bill!

Den 24 april 2000 18:25:34 +0200 skrev Santiago Romero:

yes, that’s the way I want to do it. There is a 50Hz interrupt and
I want to link my emulator speed to the real emulator speed by
executing opcodes up to 224 T-States, and then wait until a 50Hz
interrupt occurs.

224 T-states if for one scanline, not for a full frame. A frame is
(64 + 192 + 56) * 224 = 69888 T-states (OK, so I cheated and looked it
up). I suppose synching to each frame is all right, but no matter how fast
your machine is, you are never going to have more than 20ms free time
until the next frame so sleeping is out of the question under Unix, unless
you have an Alpha :-).

The hardest part might be to get the sound play cleanly, but that should be
doable without time-locking the entire emulator to the audio device :slight_smile:

I don’t want (still) to do a perfect opcode by opcode spectrum
emulation, I’ve seen R80 working (very good) and it syncs on each
scanline, so I’ll try this (at least in my 0.0.1 version :).

Sync on each scanline is OK, but then you need a high resolution timer
like gettimeofday() - I don’t think milliseconds (from SDL_GetTicks) would
do.

I want a function to be executed 50 times per second (it is the
function that updates the keyboard array and screen for my
emulator) and I don’t know how to do this under SDL… ?Must I
use a separate thread with a wait like SDL_Delay into it?

Your best bet is probably to use a SDL threaded timer (SDL_SetTimer() or
SDL_AddTimer() in SDL 1.1). Actually if you really just want to update

Ok, although the timer is quite limited (10ms), It is enough for me
(f=50Hz -> T=20ms)…

the keyboard array, it would probably be more efficient to do so only
when events are received, so you can use a SDL event filter for that.

do you mean treating the keys just in the main
loop (after detecting a SDL_KEYDOWN message?)…

I did write a SVGAlib emulator as well in the past :wink:

what kind of emulator? :slight_smile:

Another question is the “putpixel” example given on the SDL docs.
It is supposed to accept an Uint32 colour, but you must decide
before calling it the format of that colour, so you need extra
code. I’ve done the following, but I don’t know if will work on
all platforms (I think BGR cards will show wrong colours):

You should probably call SDL_MapRGB() for that, passing the pixel
format structure from the surface you are using… It will do all
the work for you !

I don’t understand what SDL_MapRGB() does… :frowning: You pass it the
pixel format of the dest surface, and … it’s supposed to build
a “color palette” for non-palette modes? :? I see… I’m testing
it… yes, it works :slight_smile: nice, every day SDL surprises me :slight_smile:

Programmer
Loki Entertainment Software

I think the Linux world will have lots of reasons to be very grateful
to Loki E.S. if SDL continues being developed this way… I’ve never
used SDL and I’m learning it in just a few weeks, when it took 2 months
to learn just directdraw… It’s amazing what SDL can do and how has
returned me to my “programming days” :slight_smile:

“Microsoft has done to computers what McDonald’s has done to gastronomy”

X’DDD I’ll add it to my RANDSIG signature generator :slight_smile:

 http://www.ctv.es/USERS/sromero/prog/randsig.html

CU (and thx a lot!).–
ULTIMAS NOTICIAS: Programador encontrado muerto en la ducha con
una botella de champ? donde pon?a: “Lavar, aclarar, repetir”.

-----------------------------------------------------

NoP / Compiler – nop @ todolinux.org
POWERED BY - Linux RedHat 6.0 - Reg. User #74.821
http://www.ctv.es/USERS/sromero

~-----------------------------------------------------~

El 24 Apr 2000 15:49:05 -0700, William Kendrick escribi?:

(changed by the 50Hz function) and after executing 224 t-states
the emulator waits till that variable changes in a while loop
with a delay into it.

Warning - Tight loops like that eat major CPU :wink: I know, I’ve gotten
complaints about them before. :wink:

well… his R80 emulator is MSDOS-based, so is now worried by CPU :)–
Windows 2000 no se cuelg?$%&/# NO CARRIER

-----------------------------------------------------

NoP / Compiler – nop @ todolinux.org
POWERED BY - Linux RedHat 6.0 - Reg. User #74.821
http://www.ctv.es/USERS/sromero

~-----------------------------------------------------~