How to define a marco used assembly language in SGDK
Moderator: Stef
-
- Very interested
- Posts: 101
- Joined: Thu Sep 04, 2008 2:57 am
- Location: China
How to define a marco used assembly language in SGDK
How to write these code in SGDK?
;*****************************************
SetA24IO MACRO BitValue
move.w #7,$a14406
move.w BitValue,$a14404
ENDM
;*****************************************
;*****************************************
SetA24IO MACRO BitValue
move.w #7,$a14406
move.w BitValue,$a14404
ENDM
;*****************************************
Depends on if you are using C or assembly:
If in C:
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):
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:
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.
If in C:
Code: Select all
#include "genesis.h"
#define SetA24IO(bitValue) *(vu16*)0xa14406 = 7;\
*(vu16*)0xa14404 = (bitValue);
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) : );
And finally, in gas assembler:
Code: Select all
.macro SetA24IO BitValue
move.w #7, 0xa14406
move.w \BitValue, 0xa14404
.endm
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.
-
- Very interested
- Posts: 2984
- Joined: Fri Aug 17, 2007 9:33 pm
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.Stef wrote:Seems you know a lot of stuff about GCC, i did not known the asm "volatile" to allow GCC optimization
-
- Very interested
- Posts: 2984
- Joined: Fri Aug 17, 2007 9:33 pm
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
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.
Code: Select all
asm volatile (".noat\n\t"
".noreorder\n\t"
Last edited by Chilly Willy on Mon Jul 02, 2012 5:02 pm, edited 2 times in total.
-
- Very interested
- Posts: 101
- Joined: Thu Sep 04, 2008 2:57 am
- Location: China
-
- Very interested
- Posts: 2984
- Joined: Fri Aug 17, 2007 9:33 pm
http://sourceware.org/binutils/docs-2.22/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?
-
- Very interested
- Posts: 101
- Joined: Thu Sep 04, 2008 2:57 am
- Location: China
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?
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?
-
- Very interested
- Posts: 101
- Joined: Thu Sep 04, 2008 2:57 am
- Location: China
Oh! sasuke writed is rightStef wrote:I guess you meant Chilly, i didn't any help there :pzhengyaxin_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?
.macro SetA24IO BitValue
move.w #7, 0xa14406
move.w \BitValue, 0xa14404
.endm
-
- Very interested
- Posts: 2984
- Joined: Fri Aug 17, 2007 9:33 pm
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
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