Discussion about software development for the old-school Gameboys, ranging from the "Gray brick" to Gameboy Color
(Launched in 2008)
You are not logged in.
Hi.
I'm improving a demo i made, (lots of bugs).
I want to include a wave screen effect, but i don't know how to use arrays in asm.
This code moves the scx register. (syntax may be different as i'm using .s files)
. . . _scroll:: ldh a, (.SCX) inc a ldh (.SCX), a ret _WAVE: .DB 0, 1, 2, 3, 4, 5...
Now i want the SCX to take values from The "WAVE" array and loop them.
Next question, Imagine the same code as above. But WAVE is defined in C array.
How do I acces it from the asm code?
Thanks a lot!
Last edited by Mills (2014-06-17 07:21:57)
Offline
If I remember correctly, to access a C variable from asm in GBDK you have to put an underscore before the name. For example, if your C file is like this:
int var = 0;
You can use it from asm like:
ld a,(#_var)
And well, to read from an array you can do something like this:
ld hl,#array_base ld de,index add hl,de ld a,(hl)
If your index is in a 8-bit variable:
ld hl,#array_base ld a,(#index) ld e,a ld d,#0 add hl,de ld a,(hl)
Offline
AntonioND wrote:
Code:
ld hl,#array_base ld de,index add hl,de ld a,(hl)If your index is in a 8-bit variable:
Code:
ld hl,#array_base ld a,(#index) ld e,a ld d,#0 add hl,de ld a,(hl)
I don't understand it very well. array_base is the name of the array? What does index mean?
Thanks.
Last edited by Mills (2014-06-18 07:48:24)
Offline
AntonioND wrote:
Yeah, the array name. In C you would do: a = array_base[index]
Thanks.
It "works". But it only reads the data if i create the array in the same asm file... not from c.
And it reads just one position of the array. How do i increase the index value?
_scroll:: ld hl,#_wave ;does not complain about "not defined". But it does not read it from c ld de,#1 ;if i use here a number, it just loads that position of the array add hl,de ;shouldn't this line go to the next position of the "wave" array?.. it does not... ld a,(hl) ldh (.SCY), a ret ;returns to c
I'm very very noob at this .
Last edited by Mills (2014-06-18 18:03:56)
Offline
With that code you are always reading wave[1].
_scroll:: ld hl,#_wave ; hl = &wave ld de,#1 ; de = 1 add hl,de ; hl = &wave + 1 => hl = &(wave[1]) ld a,(hl) ; a = *(&(wave[1])) => a = wave[1] ldh (.SCY), a ret
You need to pass the index as an argument (I don't know how to do it correctly, I think they are always saved in the stack but sometimes register A holds one of them too) or save it in a variable and read the variable from asm.
For example, in C:
UINT8 wave_index; UINT8 wave[144]; ... wave_index = REG_LY; scroll();
In asm:
_scroll:: ld hl,#_wave ; hl = &wave ld a,(#_wave_index) ld e,a ld d,#0 ; de = wave_index add hl,de ; hl = &wave + wave_index => hl = &(wave[wave_index]) ld a,(hl) ; a = *(&(wave[wave_index])) => a = wave[wave_index] ldh (.SCY), a ret
You could even change the "ld a,(#_wave_index)" by "ldh a,(#.REG_LY)", but probably the first screen line would be incorrect.
Anyway, you could do this from C. I don't think that code can be optimized because the function is really short and after compiling it I'm sure it is pretty short too. And there's the call function overhead, too. To see what is LCC doing when compiling, execute lcc like this:
lcc -S main.c
You could code something in C, compile it, see what is LCC doing, copy it and optimize it.
Offline
Thanks, i nearly got it. Now it reads the values correctly
Offline
I made that "ugly" second reality port to the game boy, so i was improving it a bit.
At the end I was able to make the waves in C without making the screen to show garbage.
This is the result :
Offline