It is currently Tue Jul 23, 2019 2:11 am

All times are UTC - 7 hours





Post new topic Reply to topic  [ 14 posts ] 
Author Message
PostPosted: Thu Jun 20, 2019 6:45 pm 
Offline
User avatar

Joined: Sun Jun 11, 2017 5:39 pm
Posts: 66
Hello,

I have an array like this

;Array_1
HEX 01 04 07

Those are address. I would need to be able to define each position of this array with a name. For example,

Mario_Sprite_ID takes the value of 01 (because it refers to HEX 01)
Goomba_Sprite_ID takes the value of 02 (because it refers to HEX 02)

So those values (Mario_Sprite_ID and Goomba_Sprite_ID) are pointers to read from Array_1

I really hope it's clear, not sure how to explain it


Top
 Profile  
 
PostPosted: Fri Jun 21, 2019 12:59 am 
Offline
User avatar

Joined: Sat Sep 07, 2013 2:59 pm
Posts: 1884
Something like this?

Code:
Array_1:
Mario_Sprite_ID:
    HEX 01
Goomba_Sprite_ID:
    HEX 04
Whatever:
    HEX 07

_________________
Available now: My game "City Trouble".
Website: https://megacatstudios.com/products/city-trouble
Trailer: https://youtu.be/IYXpP59qSxA
Gameplay: https://youtu.be/Eee0yurkIW4
German Retro Gamer article: http://i67.tinypic.com/345o108.jpg


Top
 Profile  
 
PostPosted: Fri Jun 21, 2019 5:25 am 
Offline
User avatar

Joined: Fri May 08, 2015 7:17 pm
Posts: 2536
Location: DIGDUG
You can also do an enum if the values are sequential.

And you can define constants with = if they aren't sequential.

Mario_Sprite_ID = 1

_________________
nesdoug.com -- blog/tutorial on programming for the NES


Top
 Profile  
 
PostPosted: Fri Jun 21, 2019 6:25 am 
Offline
User avatar

Joined: Sat Feb 12, 2005 9:43 pm
Posts: 11376
Location: Rio de Janeiro - Brazil
If you use the HEX approach, you'll be using ROM space to define these values. If they are supposed to be used as data in the game, that's fine, but if these are meant to be used as constants in the game's logic, it's better to define these values as assembler symbols, in one of the following ways:

Code:
Mario_Sprite_ID = $01
Goomba_Sprite_ID = $02
Something_Sprite_ID = $03


Code:
Mario_Sprite_ID .equ $01
Goomba_Sprite_ID .equ $02
Something_Sprite_ID .equ $03


Code:
.enum $01
Mario_Sprite_ID .dsb 1
Goomba_Sprite_ID .dsb 1
Something_Sprite_ID .dsb 1
.ende


All of these will create sequential symbols that you can use anywhere in the code, without using ROM space. For example:

Code:
;set the variable to the ID of the goomba sprite
lda #Goomba_Sprite_ID
sta Sprite_ID


Would you mind telling us what this is for? This would possibly help us give more adequate suggestions.


Top
 Profile  
 
PostPosted: Tue Jun 25, 2019 8:40 am 
Offline
User avatar

Joined: Sun Jun 11, 2017 5:39 pm
Posts: 66
Thanks to all for your answer!!

What I want is actually to be able to pick a sprite, let's say mario sprite and do

LDA ArrayTable1 + #MarioSpriteID

I think that with DRW's answer I could do it:

MarioAbsoluteSpriteID = Mario_Sprite_DI - Array_1

where Mario_Sprite_DI and Array_1 are taken from DWR answer.
So MarioAbsoluteSpriteID would be dynamic (which is not in tokumaru's answer). I want it to be dynamic in case I add others pointers in the array.


Top
 Profile  
 
PostPosted: Wed Jun 26, 2019 5:23 pm 
Offline
User avatar

Joined: Sun Jun 11, 2017 5:39 pm
Posts: 66
Ok I was successful in doing what I wanted, but I wondered if there was a better way to do it:

ARRAY__LOW_ASMs:

;here is the low pointer for each ASM location
HEX 00 00

;Following line is used to make calculs later
ASM_PTR_LOCATION__GOOMBA:

DB <ASM__GOOMBA
HEX 00

ARRAY__HIGH_ASMs:

;here is the high pointer for each ASM location
HEX 00 00
DB >ASM__GOOMBA
HEX 00

T=ASM_PTR_LOCATION__GOOMBA - ARRAY__ASMs
DB <T
DB <T

------------------
So here T = #$02, which is what I want, since it is location of high and low byte of RAM address of GOOMBA ASM, which I can now access

LDY T
LDA ARRAY__LOW_ASMs,Y
STA $01
LDA ARRAY__HIGH_ASMs,Y
STA $00

JMP ($00)

----------------------------------

But I fin't it weird the way it's done can I do it better?


Top
 Profile  
 
PostPosted: Wed Jun 26, 2019 5:26 pm 
Offline

Joined: Sun Apr 13, 2008 11:12 am
Posts: 8488
Location: Seattle
We have an article on the wiki about this: nesdevwiki:Jump table


Top
 Profile  
 
PostPosted: Wed Jun 26, 2019 6:04 pm 
Offline
User avatar

Joined: Sun Jun 11, 2017 5:39 pm
Posts: 66
I read all of it but unless I don,t see something on the site, it doesn't answer my question?
What I want is every index in the array to be referenced by a variable, and I see nothing to do the equivalent on the page


Top
 Profile  
 
PostPosted: Wed Jun 26, 2019 6:12 pm 
Offline

Joined: Sun Sep 19, 2004 11:12 pm
Posts: 21511
Location: NE Indiana, USA (NTSC)
Could you express what you want to happen in C or in any other non-assembly programming language in which you are more comfortable, so that others can translate your intent into assembly language?

_________________
Pin Eight | Twitter | GitHub | Patreon


Top
 Profile  
 
PostPosted: Wed Jun 26, 2019 6:19 pm 
Offline

Joined: Sun Apr 13, 2008 11:12 am
Posts: 8488
Location: Seattle
Reading more closely, he already has the jump table set up. He wants an "automated" way of converting index within that jump table to a symbol for use in the assembler.

Since this is asm6, I'm out of my depth.


Top
 Profile  
 
PostPosted: Wed Jun 26, 2019 6:32 pm 
Offline
User avatar

Joined: Sun Jun 11, 2017 5:39 pm
Posts: 66
lidnariq wrote:
Reading more closely, he already has the jump table set up. He wants an "automated" way of converting index within that jump table to a symbol for use in the assembler.

Since this is asm6, I'm out of my depth.


Thanks!! I didn't know how to explain it simply, I did try to explain it twice above but I think it wasn't clear haha


Top
 Profile  
 
PostPosted: Wed Jun 26, 2019 11:14 pm 
Offline
User avatar

Joined: Sat Sep 07, 2013 2:59 pm
Posts: 1884
I don't know how to do this in Asm6, but in C, macros are always quite helpful if you want automated stuff.

For example, if you have enemy arrays with data and then you have one array that stores all the enemy addresses and then you want to use names for the index for this array, you can do this:
Code:
const byte GoombaData[] = { ... };
const byte KoopaData[] = { ... };
const byte BooData[] = { ... };

#define ENEMIES(postfix)\
    Goomba##postfix,
    Koopa##postfix,
    Boo##postfix

/* All enemy IDs that are also the indices for the array below. */
enum
{
    ENEMIES(Id),
    EnemyCount
}

/* Pointers to all enemy arrays. */
const byte *const AllEnemies[] =
{
    ENEMIES(Data)
};

Now you can do enemy = AllEnemies[KoopaId]; or you can do
for (i = 0; i < EnemyCount; ++i)
enemy = AllEnemies[i];


Also, this way, whenever you have a new enemy, just add a value to the macro and the compiler will automatically tell you what array definition you're missing.
And if you shift the enemy order around, the IDs and AllEnemies entries will still match.

If Asm6 supports macros, I assume you can do a similar thing: One macro where all enemy names plus pre- or postfixes are listed. And whenever you need all enemies in order, you simply call this macro with its corresponding pre- and postfix parameters.

_________________
Available now: My game "City Trouble".
Website: https://megacatstudios.com/products/city-trouble
Trailer: https://youtu.be/IYXpP59qSxA
Gameplay: https://youtu.be/Eee0yurkIW4
German Retro Gamer article: http://i67.tinypic.com/345o108.jpg


Top
 Profile  
 
PostPosted: Thu Jun 27, 2019 5:53 pm 
Offline
User avatar

Joined: Sun Jun 11, 2017 5:39 pm
Posts: 66
Looks nice, though I have no idea how to do such things in ASM6


Top
 Profile  
 
PostPosted: Thu Jun 27, 2019 11:56 pm 
Offline
User avatar

Joined: Sat Sep 07, 2013 2:59 pm
Posts: 1884
I know that cc65 can do macros, enums and this stuff where I put together a name.
Although in cc65 it's not done as nicely as C because you have to use .string, .concat, .ident etc. Which makes it much uglier than simply putting the names together as with ##.

Whether ASM6 can do this, I don't know, but someone else here can surely tell you.

_________________
Available now: My game "City Trouble".
Website: https://megacatstudios.com/products/city-trouble
Trailer: https://youtu.be/IYXpP59qSxA
Gameplay: https://youtu.be/Eee0yurkIW4
German Retro Gamer article: http://i67.tinypic.com/345o108.jpg


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 14 posts ] 

All times are UTC - 7 hours


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group