Gameboy Development Forum

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.

Ads

#1 2014-06-17 07:21:34

Mills
Member
Registered: 2012-12-21
Posts: 132

Making waves and other asm stuff

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)

Code:

.
.
.
_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

 

#2 2014-06-17 14:48:51

AntonioND
Member
Registered: 2014-06-17
Posts: 134
Website

Re: Making waves and other asm stuff

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:

Code:

int var = 0;

You can use it from asm like:

Code:

ld a,(#_var)

And well, to read from an array you can do something like this:

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)

Offline

 

#3 2014-06-18 07:47:55

Mills
Member
Registered: 2012-12-21
Posts: 132

Re: Making waves and other asm stuff

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

 

#4 2014-06-18 08:26:06

AntonioND
Member
Registered: 2014-06-17
Posts: 134
Website

Re: Making waves and other asm stuff

Yeah, the array name. In C you would do: a = array_base[index]

Offline

 

#5 2014-06-18 17:56:48

Mills
Member
Registered: 2012-12-21
Posts: 132

Re: Making waves and other asm stuff

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?

Code:

_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 smile.

Last edited by Mills (2014-06-18 18:03:56)

Offline

 

#6 2014-06-19 09:50:36

AntonioND
Member
Registered: 2014-06-17
Posts: 134
Website

Re: Making waves and other asm stuff

With that code you are always reading wave[1].

Code:

_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:

Code:

UINT8 wave_index;
UINT8 wave[144];

...

wave_index = REG_LY;
scroll();

In asm:

Code:

_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:

Code:

lcc -S main.c

You could code something in C, compile it, see what is LCC doing, copy it and optimize it.

Offline

 

#7 2014-06-19 10:10:47

Mills
Member
Registered: 2012-12-21
Posts: 132

Re: Making waves and other asm stuff

Thanks, i nearly got it. Now it reads the values correctly smile

Offline

 

#8 2014-06-22 18:44:22

Mills
Member
Registered: 2012-12-21
Posts: 132

Re: Making waves and other asm stuff

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 smile :

https://dl.dropboxusercontent.com/u/757056/GameBoy/bgb00005.bmp

smile

Offline

 

#9 2014-06-23 08:51:48

AntonioND
Member
Registered: 2014-06-17
Posts: 134
Website

Re: Making waves and other asm stuff

It looks pretty good. smile

Offline

 

Board footer

Powered by PunBB
© Copyright 2002–2005 Rickard Andersson