Pure C questions Hardware access & usage auf Supexec()

C and PASCAL (or any other high-level languages) in here please

Moderators: simonsunnyboy, Mug UK, Zorro 2, Moderator Team

User avatar
simonsunnyboy
Moderator
Moderator
Posts: 5001
Joined: Wed Oct 23, 2002 4:36 pm
Location: Friedrichshafen, Germany
Contact:

Pure C questions Hardware access & usage auf Supexec()

Postby simonsunnyboy » Thu Aug 03, 2006 1:18 pm

Hiya Pure C users,

I'm currently playing a bit with Pure C and try to access the hardware.

1) I tried he following and it works:

#define COLOR0 ((unsigned short*) 0xffff8240)

Which should give me pointer to a wordwide hw register, color0 of the ST palette in this case.

I can write to it when in supervisor mode *COLOR0=0x00f;
Screen gets blue so it works, but the compiler gives me the following warning for this line:
"Constant is long in functon ...."
Now which constant? The color blue is defined as a 16bit word so it can't be a long. A pointer is always a long on the ST (from my m68k knowledge) so I don't get it as that measn the pointer itself and not the size of the data accessed with it. As far as I understand writing int* means "I want to access data with the size of an int at the pointer address" which shouldn't alter the size of pointer address itself.

2) How do I use the Supexec() call properly?
I tried:

void my_func()
...

Supexec(my_func())

but it didn't work... how is the syntax for this?
I now do a Super(0) as first call but this sucks and I fear I mess things up upon program termination....and I don't want to interfere with Pure C's stack handling by saving its value and restoring it later (as I would do in GFA or m68k)

Thanks for any help!
Simon Sunnyboy/Paradize - http://paradize.atari.org/

Stay cool, stay Atari!

1x2600jr, 1x1040STFm, 1x1040STE 4MB+TOS2.06+SatanDisk, 1xF030 14MB+FPU+NetUS-Bee

User avatar
simonsunnyboy
Moderator
Moderator
Posts: 5001
Joined: Wed Oct 23, 2002 4:36 pm
Location: Friedrichshafen, Germany
Contact:

Postby simonsunnyboy » Thu Aug 03, 2006 1:29 pm

BTW. I checked the output of the compiler with the Pure C debugger and the code for the pointer access looks exactly like what I would do with m68k using move.w. So I wonder at which point the problem occurs? (It gives the line number of the pointer usage, both for reading and writing!)
Simon Sunnyboy/Paradize - http://paradize.atari.org/

Stay cool, stay Atari!

1x2600jr, 1x1040STFm, 1x1040STE 4MB+TOS2.06+SatanDisk, 1xF030 14MB+FPU+NetUS-Bee

User avatar
frost
Captain Atari
Captain Atari
Posts: 352
Joined: Sun Dec 01, 2002 2:50 am
Location: Limoges
Contact:

Re: Pure C questions Hardware access & usage auf Supexec

Postby frost » Thu Aug 03, 2006 1:39 pm

Try Supexec(my_func);

AFAIK, in your code, you call my_func and its return code is given as argument to Supexec.

User avatar
simonsunnyboy
Moderator
Moderator
Posts: 5001
Joined: Wed Oct 23, 2002 4:36 pm
Location: Friedrichshafen, Germany
Contact:

Postby simonsunnyboy » Thu Aug 03, 2006 1:52 pm

That gave me another "type mismatch error" and it refused to compile at all that way.
Simon Sunnyboy/Paradize - http://paradize.atari.org/

Stay cool, stay Atari!

1x2600jr, 1x1040STFm, 1x1040STE 4MB+TOS2.06+SatanDisk, 1xF030 14MB+FPU+NetUS-Bee

User avatar
lp
Fuji Shaped Bastard
Fuji Shaped Bastard
Posts: 2315
Joined: Wed Nov 12, 2003 11:09 pm
Location: GFA Headquarters
Contact:

Postby lp » Thu Aug 03, 2006 7:29 pm

I am not sure how you have defined your function exactly, but an example of Supexec() I found in "C" shows the routine one intends to call defined like this:

static long my_func(void)
{
blah..
}

Supexec(my_func);

I'm not sure if it's a lattice or purec listing, but maybe you can try it.

User avatar
Nyh
Atari God
Atari God
Posts: 1496
Joined: Tue Oct 12, 2004 2:25 pm
Location: Netherlands

Re: Pure C questions Hardware access & usage auf Supexec

Postby Nyh » Mon Aug 21, 2006 8:44 am

simonsunnyboy wrote:I'm currently playing a bit with Pure C and try to access the hardware.

1) I tried he following and it works:

#define COLOR0 ((unsigned short*) 0xffff8240)

Which should give me pointer to a wordwide hw register, color0 of the ST palette in this case.

Screen gets blue so it works, but the compiler gives me the following warning for this line:
"Constant is long in functon ...."
Now which constant? The color blue is defined as a 16bit word so it can't be a long. A pointer is always a long on the ST (from my m68k knowledge) so I don't get it as that measn the pointer itself and not the size of the data accessed with it. As far as I understand writing int* means "I want to access data with the size of an int at the pointer address" which shouldn't alter the size of pointer address itself.

The constant 0xffff8240 is long. Write it as 0xffff8240UL and the warning is gone. As soon as the compiler encounters the number 0xffff8240 it realises it is a long and gives you a warning. It doesn't matter the constant is cast into a 32 bit pointer.
#define COLOR0 ((unsigned short*) 0xffff8240UL)
BTW: if you are doing a lot of colour switching you better use a pointer:
unsigned short* color0 = (unsigned short*) 0xffff8240UL;
Then the compiler will use an adress register.

simonsunnyboy wrote:2) How do I use the Supexec() call properly?
I tried:

void my_func()
...

Supexec(my_func())

The prototype of Supexec is:
void Supexec(long (*func)());
The function call expects a long and not a pointer. (This OS call is not an example of clean C programming.) To use your example:
void my_func()
...
Supexec((long)(my_func))
What you do is cast the adress of my_func() into a long with the (long).

Succes, Hans Wessels

User avatar
simonsunnyboy
Moderator
Moderator
Posts: 5001
Joined: Wed Oct 23, 2002 4:36 pm
Location: Friedrichshafen, Germany
Contact:

Postby simonsunnyboy » Mon Aug 21, 2006 12:12 pm

Thank you for your input, Nyh! I'll try those later.
Simon Sunnyboy/Paradize - http://paradize.atari.org/

Stay cool, stay Atari!

1x2600jr, 1x1040STFm, 1x1040STE 4MB+TOS2.06+SatanDisk, 1xF030 14MB+FPU+NetUS-Bee

User avatar
tobe
Atari God
Atari God
Posts: 1459
Joined: Sat Jan 24, 2004 10:06 am
Location: Lyon, France
Contact:

Postby tobe » Tue Aug 22, 2006 10:00 am

long (*func)()


As lp said, this is a function pointer declaration, not a cast.

typedef long (*FUNC_TYPE)();

long CallThisFunc ( FUNC_TYPE TheFunc )
{
return TheFunc();
}

This is the worst type declaration in C :)
step 1: introduce bug, step 2: fix bug, step 3: goto step 1.

User avatar
tobe
Atari God
Atari God
Posts: 1459
Joined: Sat Jan 24, 2004 10:06 am
Location: Lyon, France
Contact:

Re: Pure C questions Hardware access & usage auf Supexec

Postby tobe » Tue Aug 22, 2006 12:09 pm

Nyh wrote:The constant 0xffff8240 is long. Write it as 0xffff8240UL and the warning is gone.

Just curious, it is possible to declare a short pointer in C like in asm ?
Because 0xFFFF8240 is also a negative signed word, in 68k you can write:
move.w #$0xFFF,$FFFF8240.W
It's faster to execute.
step 1: introduce bug, step 2: fix bug, step 3: goto step 1.

User avatar
daeghnao
Captain Atari
Captain Atari
Posts: 479
Joined: Wed Oct 27, 2004 12:41 pm
Location: York, UK
Contact:

Postby daeghnao » Tue Aug 22, 2006 1:00 pm

If you want to do it without a typedef, you can:

Code: Select all

long CallThisFunc(long (*theFunc)()) {
  return theFunc();
}


If you want to return a pointer to a function from another function, the declaration is something like:

Code: Select all

long (*makeAFunc())(int x) {
  return bigListOfFunctions[x];
}


Of course, declaring bigListOfFunctions is fun too:

Code: Select all

long (*bigListOfFunctions[])() = { func0, func1, func2 };


And finally, if you're into defining function prototypes without names, the prototype for CallThisFunc is:

Code: Select all

long CallThisFunc(long (*)());


Which all goes to show, typedefs are pretty useful :)

User avatar
simonsunnyboy
Moderator
Moderator
Posts: 5001
Joined: Wed Oct 23, 2002 4:36 pm
Location: Friedrichshafen, Germany
Contact:

Postby simonsunnyboy » Tue Aug 22, 2006 3:57 pm

Simply declaring the function to be called via Supexec() as long worked perfectly. Same for the UL trick at the pointer!
Thanks for sorting these problems.
Simon Sunnyboy/Paradize - http://paradize.atari.org/

Stay cool, stay Atari!

1x2600jr, 1x1040STFm, 1x1040STE 4MB+TOS2.06+SatanDisk, 1xF030 14MB+FPU+NetUS-Bee

User avatar
tobe
Atari God
Atari God
Posts: 1459
Joined: Sat Jan 24, 2004 10:06 am
Location: Lyon, France
Contact:

Postby tobe » Tue Aug 22, 2006 4:29 pm

daeghnao wrote:If you want to return a pointer to a function from another function, the declaration is something like:

Code: Select all

long (*makeAFunc())(int x) {
  return bigListOfFunctions[x];
}



I like this one !
It must be possible to re-cast pointers to functions :)
step 1: introduce bug, step 2: fix bug, step 3: goto step 1.


Social Media

     

Return to “C / PASCAL etc.”

Who is online

Users browsing this forum: No registered users and 3 guests