Page 1 of 1
How to define a marco used assembly language in SGDK
Posted: Fri Jun 29, 2012 3:09 pm
by zhengyaxin_8bit
How to write these code in SGDK?
;*****************************************
SetA24IO MACRO BitValue
move.w #7,$a14406
move.w BitValue,$a14404
ENDM
;*****************************************
Posted: Sun Jul 01, 2012 8:35 pm
by sasuke
Depends on if you are using C or assembly:
If in C:
Code: Select all
#include "genesis.h"
#define SetA24IO(bitValue) *(vu16*)0xa14406 = 7;\
*(vu16*)0xa14404 = (bitValue);
This does not always generate the same assembly as the asm macro, but has similar results. Note that genesis.h is required for the vu16 keyword.
For inline assembly (within C code):
Code: Select all
#define SetA24IO(bitValue) asm volatile ("move.w #7, 0xa14406\n\t"\
"move.w %0, 0xa14404\n"\
: :"g"(bitValue) : );
This generates the same assembly instructions as your macro. However, it is less readable to users who do not understand gcc's inline asm syntax. The "g" means that any general source operand can be used where there is a "%0" as the source operand. The volatile keyword here is to tell the compiler that we know what we are doing, and the compiler should not optimize the inline assembly.
And finally, in gas assembler:
Code: Select all
.macro SetA24IO BitValue
move.w #7, 0xa14406
move.w \BitValue, 0xa14404
.endm
Note that this can only be used in asm files, not C files.
P.S. Hi everyone. I have actually been a member here for about 3 years, but haven't found a reason to post yet until now.

Posted: Sun Jul 01, 2012 9:49 pm
by Stef
Seems you know a lot of stuff about GCC, i did not known the asm "volatile" to allow GCC optimization

Posted: Mon Jul 02, 2012 2:36 am
by Chilly Willy
Stef wrote:Seems you know a lot of stuff about GCC, i did not known the asm "volatile" to allow GCC optimization

It's not an optimization, it's an ANTI-optimization. It prevents gcc from changing your assembly thinking that the changes are better. You do this mainly for code that accesses hardware that NEEDS a particular order to the code enforced, like doing VDP commands. Otherwise it might change the order of stores to 0xC00004 and 0xC00000 since they are different locations and CLEARLY not linked.

Posted: Mon Jul 02, 2012 2:45 pm
by Stef
I misread, make more sense actually this way... I did not know that GCC could change your assembly code, i always though we were speaking about bounding C code optimization...
Posted: Mon Jul 02, 2012 5:00 pm
by Chilly Willy
Any inline assembly or assembly files has to be run through gas, and gas is allowed to make optimizations unless otherwise instructed not to. Nearly all gcc assembler backends at least do peephole optimization, and may do more extensive optimizations. The "volatile" keyword, when used with inline assembly, is your way in a C file to limit assembly optimizations that may be dangerous to the proper operation of the code. You also see people insert assembler directives in the code for other purposes. For example, MIPS inline assembly often starts with
Code: Select all
asm volatile (".noat\n\t"
".noreorder\n\t"
which instructs gas to not generate macro instructions that use the at register, and to not change the order of the instructions for delay slots.
Posted: Mon Jul 02, 2012 5:02 pm
by zhengyaxin_8bit
Oh!Thanks stef , you help me again.
I few know the syntax of gcc and other tools in folder "\bin". Where I can find more information about them?
Posted: Mon Jul 02, 2012 5:03 pm
by Chilly Willy
zhengyaxin_8bit wrote:Oh!Thanks stef , you help me again.
I few know the syntax of gcc and other tools in folder "\bin". Where I can find more information about them?
http://sourceware.org/binutils/docs-2.22/
Posted: Mon Jul 02, 2012 5:33 pm
by zhengyaxin_8bit
I have other question.
Before, I use other compiler named "x68k".In this compiler,I can use local
symbol,like this:
;*********************************************
ClearScreenBuffer:
LEA.L ScreenBuffer,A0
LEA.L bg01_PNT_OFFSET,A1
MOVE.W #40*28-1,D0
?LOOP
MOVE.W (a1)+,(A0)+
DBF D0,?LOOP
RTS
;*********************************************
Clear_CRAM: ; CRAM ADDRESS 00000H--0007FH 128 BYTES
MOVE.W #$3F,D0 ;$3F=63
MOVE.L #$C00004,A0 ;A0=$C00004
MOVE.W #$8F02,(A0) ;INCREMENT VALUE=2(REG. #15)
MOVE.W #$C000,(A0)
MOVE.W #0,(A0) ;CRAM WRITE MODE
?LOOP
MOVE.W #0,$C00000
DBF D0,?LOOP
RTS
;******************************************************
There are two place defined "?LOOP"
In sgdk, Do they allow used like above?
Posted: Mon Jul 02, 2012 6:40 pm
by Stef
zhengyaxin_8bit wrote:Oh!Thanks stef , you help me again.
I few know the syntax of gcc and other tools in folder "\bin". Where I can find more information about them?
I guess you meant Chilly, i didn't any help there :p
Posted: Tue Jul 03, 2012 1:46 am
by zhengyaxin_8bit
Stef wrote:zhengyaxin_8bit wrote:Oh!Thanks stef , you help me again.
I few know the syntax of gcc and other tools in folder "\bin". Where I can find more information about them?
I guess you meant Chilly, i didn't any help there :p
Oh! sasuke writed is right
.macro SetA24IO BitValue
move.w #7, 0xa14406
move.w \BitValue, 0xa14404
.endm

Posted: Tue Jul 03, 2012 1:54 am
by Chilly Willy
It's not a problem - we know what you meant.
As to local labels, gas uses a different form from most other assemblers. You'll find that discussed in the docs I linked to, but the most common local label used in gas tends to be of the form <number>: while the code refers to it via <number>f or <number>b depending on if the label <number> is later in the code (f = forward from your current position), or earlier in the code (b = backward from your current position). So you would do something like this
Code: Select all
tst.w d0
beq.b 2f
moveq #10,d1
1:
move.w d0,(a0)+
dbra d1,1b
2:
rts