English Amiga Board


Go Back   English Amiga Board > Coders > Coders. Language > Coders. C/C++

 
 
Thread Tools
Old 24 October 2020, 11:49   #1
kamelito
Zone Friend
 
kamelito's Avatar
 
Join Date: May 2006
Location: France
Posts: 1,801
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;
}
kamelito is offline  
Old 24 October 2020, 12:04   #2
Samurai_Crow
Total Chaos forever!
 
Samurai_Crow's Avatar
 
Join Date: Aug 2007
Location: Waterville, MN, USA
Age: 49
Posts: 2,186
C was designed to save keystrokes. That's why it's so cryptic.
Samurai_Crow is online now  
Old 24 October 2020, 12:12   #3
RedskullDC
Digital Corruption
 
RedskullDC's Avatar
 
Join Date: Jan 2007
Location: Dorrigo/Australia
Age: 60
Posts: 355
Hi Kamelito, et al.

Quote:
Originally Posted by kamelito View Post
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
RedskullDC is offline  
Old 24 October 2020, 12:45   #4
kamelito
Zone Friend
 
kamelito's Avatar
 
Join Date: May 2006
Location: France
Posts: 1,801
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?
kamelito is offline  
Old 24 October 2020, 12:49   #5
meynaf
son of 68k
 
meynaf's Avatar
 
Join Date: Nov 2007
Location: Lyon / France
Age: 51
Posts: 5,323
C is like many other languages, once you've learnt it it ceases to be cryptic (well, mostly).


Quote:
Originally Posted by Samurai_Crow View Post
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
meynaf is offline  
Old 24 October 2020, 12:50   #6
Samurai_Crow
Total Chaos forever!
 
Samurai_Crow's Avatar
 
Join Date: Aug 2007
Location: Waterville, MN, USA
Age: 49
Posts: 2,186
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.
Samurai_Crow is online now  
Old 24 October 2020, 15:29   #7
pink^abyss
Registered User
 
Join Date: Aug 2018
Location: Untergrund/Germany
Posts: 408
Quote:
Originally Posted by kamelito View Post
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);}
pink^abyss is offline  
Old 24 October 2020, 15:31   #8
kamelito
Zone Friend
 
kamelito's Avatar
 
Join Date: May 2006
Location: France
Posts: 1,801
Thanks, is this the way you handle it on your games?
kamelito is offline  
Old 24 October 2020, 15:44   #9
Thorham
Computer Nerd
 
Thorham's Avatar
 
Join Date: Sep 2007
Location: Rotterdam/Netherlands
Age: 47
Posts: 3,754
Quote:
Originally Posted by meynaf View Post
This isn't obvious in the above example, where asm is much shorter
Try implementing a dictionary.
Thorham is offline  
Old 24 October 2020, 15:53   #10
kamelito
Zone Friend
 
kamelito's Avatar
 
Join Date: May 2006
Location: France
Posts: 1,801
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;
}
kamelito is offline  
Old 24 October 2020, 16:04   #11
Ernst Blofeld
<optimized out>
 
Ernst Blofeld's Avatar
 
Join Date: Sep 2020
Location: <optimized out>
Posts: 321
Quote:
Originally Posted by kamelito View Post
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)
Ernst Blofeld is offline  
Old 24 October 2020, 16:24   #12
meynaf
son of 68k
 
meynaf's Avatar
 
Join Date: Nov 2007
Location: Lyon / France
Age: 51
Posts: 5,323
Quote:
Originally Posted by Thorham View Post
Try implementing a dictionary.
Already done.
meynaf is offline  
Old 24 October 2020, 16:50   #13
alkis
Registered User
 
Join Date: Dec 2010
Location: Athens/Greece
Age: 53
Posts: 719
Quote:
Originally Posted by meynaf View Post
This isn't obvious in the above example, where asm is much shorter
Maybe now with one-liner? :P

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

http://franke.ms/cex/z/cvoPbE
alkis is offline  
Old 24 October 2020, 17:15   #14
Ami
Registered User
 
Ami's Avatar
 
Join Date: Sep 2014
Location: Poland
Posts: 175
Quote:
Originally Posted by kamelito View Post
Is there a more readable way of doing this?
Mix C and asm?
Ami is offline  
Old 24 October 2020, 17:27   #15
meynaf
son of 68k
 
meynaf's Avatar
 
Join Date: Nov 2007
Location: Lyon / France
Age: 51
Posts: 5,323
Quote:
Originally Posted by alkis View Post
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.
meynaf is offline  
Old 24 October 2020, 18:10   #16
kamelito
Zone Friend
 
kamelito's Avatar
 
Join Date: May 2006
Location: France
Posts: 1,801
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 */
}
kamelito is offline  
Old 24 October 2020, 18:46   #17
Samurai_Crow
Total Chaos forever!
 
Samurai_Crow's Avatar
 
Join Date: Aug 2007
Location: Waterville, MN, USA
Age: 49
Posts: 2,186
Extern because the linker resolves the addresses.
Samurai_Crow is online now  
Old 24 October 2020, 21:37   #18
kamelito
Zone Friend
 
kamelito's Avatar
 
Join Date: May 2006
Location: France
Posts: 1,801
Quote:
Originally Posted by Samurai_Crow View Post
Extern because the linker resolves the addresses.
How does it know that custom is dff000 ? I can't see that in the included files.
kamelito is offline  
Old 24 October 2020, 21:42   #19
Samurai_Crow
Total Chaos forever!
 
Samurai_Crow's Avatar
 
Join Date: Aug 2007
Location: Waterville, MN, USA
Age: 49
Posts: 2,186
Probably Amiga.lib linker library.
Samurai_Crow is online now  
Old 24 October 2020, 21:53   #20
kamelito
Zone Friend
 
kamelito's Avatar
 
Join Date: May 2006
Location: France
Posts: 1,801
I should have thought of that, thanks.
kamelito is offline  
 


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Similar Threads
Thread Thread Starter Forum Replies Last Post
Asm-One Debugger Curbie Coders. General 29 15 May 2021 19:12
Tool to convert asm to gnu asm (gas) Asman Coders. Asm / Hardware 13 30 December 2020 11:57
ASM: Asm-ONE or AsmPro - how to set a Hello amiga coders, I hope it is ok to hijack ? Fireball Coders. Asm / Hardware 2 24 April 2020 21:16
for ASM programmers meynaf Coders. General 29 05 August 2010 10:00

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 20:09.

Top

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Page generated in 0.10135 seconds with 15 queries