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; } |
C was designed to save keystrokes. That's why it's so cryptic.
|
Hi Kamelito, et al.
Quote:
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 |
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? |
C is like many other languages, once you've learnt it it ceases to be cryptic (well, mostly).
Quote:
|
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.
|
Quote:
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);} |
Thanks, is this the way you handle it on your games?
|
Quote:
|
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; } |
Quote:
(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) |
Quote:
|
Quote:
void mousewait() {while(*(volatile unsigned char *) 0xbfe001 & (1<<6));} http://franke.ms/cex/z/cvoPbE |
Quote:
|
Quote:
|
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 */ } |
Extern because the linker resolves the addresses.
|
Quote:
|
Probably Amiga.lib linker library.
|
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.