Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allocating variables to specific locations in ram. #48

Closed
CalinLeafshade opened this issue Jun 27, 2022 · 3 comments
Closed

Allocating variables to specific locations in ram. #48

CalinLeafshade opened this issue Jun 27, 2022 · 3 comments

Comments

@CalinLeafshade
Copy link

In the NES, OAM (sprite) data can be DMAed into the PPU by setting the OAMDMA register to the most significant byte ($XX) of an address and then the CPU will DMA into the PPU data from $XX00-$XXFF.

This means that if you have this array in your code then it must be aligned with $XX00 in ram.

Is it possible to preallocate this memory somehow at a particular location and then reference it in C? I assume it would be done in the linker with a section but I don't know how to do that.

@CalinLeafshade
Copy link
Author

This is my current solution which seems to work ok but I'm not sure if it's somehow inefficient.

static u8 sprites[256] __attribute__ ((aligned (256)));

(Why do I always raise an issue just before I find a solution?)

@mysterymath
Copy link
Member

Yep, that's the idiomatic pattern for this.

You can also decrease the memory usage incurred by alignment by passing --sort-section=alignment. This will order input sections within each output section by decreasing alignment, which helps decrease the amount of filler emitted in the final object file. It's not a complete binary packing solution though; I'd eventually like to have one, but that sort of thing isn't really in GNU ld or lld, so it'd be greenfield work.

I'll leave this open to track the work of making --sort-section=alignment the default.

@mysterymath
Copy link
Member

Done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants