English Amiga Board

English Amiga Board (https://eab.abime.net/index.php)
-   Coders. C/C++ (https://eab.abime.net/forumdisplay.php?f=118)
-   -   Decipher C vs ASM (https://eab.abime.net/showthread.php?t=104444)

kamelito 24 October 2020 11:49

Decipher C vs ASM
 
Hi
Is someone able to explain all those cryptic C lines for me to understand?
In ASM it is easy and clear.
waitmouse:
btst #6, $bfe001
bne waitmouse
clr.l d0
rts
Now please explain all those cryptic C code please and why is it so complicated.

#include <exec/types.h>

volatile UBYTE *ciaa_pra = (volatile UBYTE *) 0xbfe001;
#define PRA_FIR0_BIT (1 << 6)

void waitmouse(void)
{
while ((*ciaa_pra & PRA_FIR0_BIT) != 0) ;
}

int main(int argc, char **argv)
{
waitmouse();
return 0;
}

Samurai_Crow 24 October 2020 12:04

C was designed to save keystrokes. That's why it's so cryptic.

RedskullDC 24 October 2020 12:12

Hi Kamelito, et al.

Quote:

Originally Posted by kamelito (Post 1436736)
Now please explain all those cryptic C code please and why is it so complicated.

> #include <exec/types.h>

types.h has the definition for UBYTE (unsigned byte value)

> volatile UBYTE *ciaa_pra = (volatile UBYTE *) 0xbfe001;

Volatile keyword means that the value can change at any time.
The program should not try to cache the value it reads from 0xbfe001

> #define PRA_FIR0_BIT (1 << 6)

Set PRA_FIR0_BIT to $40

> void waitmouse(void)
{

Function takes no arguments, and returns nothing.

>while ((*ciaa_pra & PRA_FIR0_BIT) != 0) ;

Wait for Bit#6 to be ZERO, then return.


> int main(int argc, char **argv)

Main function is called by the program startup code, and can have an array of string arguments passed from the command line, or through intuition.
**argv points at the string argument array, and there are argc number of arguments

{
> waitmouse();

Call the waitmouse function which will not return until bit#6 of 0xbfe001 becomes ZERO.

> return 0;

Tell the operating system we had no errors.


Cheers,
Red

kamelito 24 October 2020 12:45

Thanks I’ll have to digest that.
I suppose you’ve to do this for all custom chips registers.
Is there a more readable way of doing this?

meynaf 24 October 2020 12:49

C is like many other languages, once you've learnt it it ceases to be cryptic (well, mostly).


Quote:

Originally Posted by Samurai_Crow (Post 1436740)
C was designed to save keystrokes. That's why it's so cryptic.

This isn't obvious in the above example, where asm is much shorter :D

Samurai_Crow 24 October 2020 12:50

First off, #include <hardware/custom.h> will define all the custom registers as one big volitile structure so you can refer to them as custom.regname rather than having to define them individually.

pink^abyss 24 October 2020 15:29

Quote:

Originally Posted by kamelito (Post 1436747)
Thanks I’ll have to digest that.
I suppose you’ve to do this for all custom chips registers.
Is there a more readable way of doing this?

I don't know if this is more readable, but if you like to access registers directly like in ASM then you can do it like this:

while (!((*(volatile char*)0xbfe001)&64));



Volatile means the read must not be cached, but must done in every iteration of the loop. Otherwise the C compiler could optimize this code fully away.
Usually you would put such code into an inline function like this:

inline char mouseLeft(){return !((*(volatile char*)0xbfe001)&64);}

kamelito 24 October 2020 15:31

Thanks, is this the way you handle it on your games?

Thorham 24 October 2020 15:44

Quote:

Originally Posted by meynaf (Post 1436750)
This isn't obvious in the above example, where asm is much shorter :D

Try implementing a dictionary.

kamelito 24 October 2020 15:53

So if I wanna change the background color is this correct?

#include "exec/types.h"
#include "hardware/custom.h"

struct Custom custom;

main()
{
custom.color[0] = 0x0F00;
}

Ernst Blofeld 24 October 2020 16:04

Quote:

Originally Posted by kamelito (Post 1436789)
So if I wanna change the background color is this correct?

#include "exec/types.h"
#include "hardware/custom.h"

struct Custom custom;

main()
{
custom.color[0] = 0x0F00;
}

No, but nearly. You don't want to create your own Custom struct, you want to reference the one that will be provided when you link your project (extern).

(besides all other things like the copper list that's running will probably override your change, so maybe this isn't the best first example)

(also, this will depend on you linking against something that provides that Custom struct)

meynaf 24 October 2020 16:24

Quote:

Originally Posted by Thorham (Post 1436787)
Try implementing a dictionary.

Already done.

alkis 24 October 2020 16:50

Quote:

Originally Posted by meynaf (Post 1436750)
This isn't obvious in the above example, where asm is much shorter :D

Maybe now with one-liner? :P

void mousewait() {while(*(volatile unsigned char *) 0xbfe001 & (1<<6));}

http://franke.ms/cex/z/cvoPbE

Ami 24 October 2020 17:15

Quote:

Originally Posted by kamelito (Post 1436747)
Is there a more readable way of doing this?

Mix C and asm?

meynaf 24 October 2020 17:27

Quote:

Originally Posted by alkis (Post 1436808)
Maybe now with one-liner? :P

void mousewait() {while(*(volatile unsigned char *) 0xbfe001 & (1<<6));}

Maybe. Yet the asm was the whole program, where here the main is missing. :p

kamelito 24 October 2020 18:10

Ok with a bit of help from rkm, I manage to make it work, but why it has to be extern ?

#include "exec/types.h"
#include "hardware/custom.h"

extern struct Custom far custom; /* far bc I had distance for data reloc16 > 32768 under sasc */

main()
{
long i;
for (i = 0; i < 10000000 ; i++)
custom.color[0] = 0x0F00; /* change bg color */
}

Samurai_Crow 24 October 2020 18:46

Extern because the linker resolves the addresses.

kamelito 24 October 2020 21:37

Quote:

Originally Posted by Samurai_Crow (Post 1436846)
Extern because the linker resolves the addresses.

How does it know that custom is dff000 ? I can't see that in the included files.

Samurai_Crow 24 October 2020 21:42

Probably Amiga.lib linker library.

kamelito 24 October 2020 21:53

I should have thought of that, thanks.


All times are GMT +2. The time now is 12:17.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.

Page generated in 0.04828 seconds with 11 queries