Sign in to follow this  
xyuri

I dont really understand memory ....

Recommended Posts

I have been getting more and more into C++ and have started realising that you need to addess memory locations more and more .... But I just seem to get more and more confused by it :( A classic example is this article on developing for the GBA which got me a little baffled
Quote:
REG_DISPCNT is a 16 bit register at memory address 0x4000000. Each bit within this register controls something of the GBA. Here's a description: Bits 0-2: Control the screen mode and can be any value between 0 and 5 Bit 3: Automatically set on a GBA if a GB or GBC cartridge is put in (ignore it). Bit 4: Used for double buffering in screen modes 4 and 5. Bit 5: Allows OAM to be set during horizontal blank (I'll explain further when we get to sprites). Bit 6: Determines if we are using 1D or 2D mapping for sprites. Again, I'll give more information when that bridge must be crossed. Bit 7: Puts the screen into a forced blank. Bits 8-11: Enable backgrounds 0-4 (more on this when we get to tile maps). Bit 12: Enables hardware rendered sprites. Bits 13-15: Enables window displays (I don't have much info on these). Handy for one 16 bit number, 'eh? Personally I probably would have rather remembered a bunch of different variable names than one variable name and the specifics on every bit in it, but oh well. Now that we know what each bit stands for, we need a way to change the values. This can be done by bit masking (the | operator) a series of numbers into the register. But who wants to remember a bunch of numbers when we can create a header file and use variable names instead? Let's do that. //screenmodes.h #ifndef __SCREENMODES__ #define __SCREENMODES__ #define SCREENMODE0 0x0 //Enable screen mode 0 #define SCREENMODE1 0x1 //Enable screen mode 1 #define SCREENMODE2 0x2 //Enable screen mode 2 #define SCREENMODE3 0x3 //Enable screen mode 3 #define SCREENMODE4 0x4 //Enable screen mode 4 #define SCREENMODE5 0x5 //Enable screen mode 5 #define BACKBUFFER 0x10 //Determine backbuffer #define HBLANKOAM 0x20 //Update OAM during HBlank? #define OBJMAP2D 0x0 //2D object (sprite) mapping #define OBJMAP1D 0x40 //1D object(sprite) mapping #define FORCEBLANK 0x80 //Force a blank #define BG0ENABLE 0x100 //Enable background 0 #define BG1ENABLE 0x200 //Enable background 1 #define BG2ENABLE 0x400 //Enable background 2 #define BG3ENABLE 0x800 //Enable background 3 #define OBJENABLE 0x1000 //Enable sprites #define WIN1ENABLE 0x2000 //Enable window 1 #define WIN2ENABLE 0x4000 //Enable window 2 #define WINOBJENABLE 0x8000 //Enable object window #define SetMode(mode) (REG_DISPCNT = mode) #endif
Does anyone know of a rather extensive document which covers all of this sort of stuff from a complete beginners point of view? Any help is greatly appreciated :) Thank you.

Share this post


Link to post
Share on other sites
This may be totally off subject. Not really sure. By just using C++, I can say that everything gets stored into RAM (which is what memory is usually referred at). A variable is stored in memory aka sizeof(int) = 4 bytes of RAM space. Pointers would access heap memory. Besides that, I think that's all you need to know about memory at a beginners standpoint.

That above seems like a 16-bit register file which is something you don't really have to worry about in C++. That seems more like Assembly aka backbone detail.

Share this post


Link to post
Share on other sites
That quote is pretty much explaining how to use the REG_DISPCNT register, which is important for the display modes on the GBA. What you need to understand is that it's a memory location composed of 16 bits, in which each bit, or otherwise a small segment of bits, composes a flag controlling how things are displayed. The first three bits are used to control the video mode, for example. It might be useful to think of these defines and the register itself just like the flags you'll see used in Windows programming sometimes, if you're familiar with it. Just like you choose the window style by passing it one value composed of many flags ORed together, you determine your video mode here by setting this register to one value composed of many flags ORed together.

Share this post


Link to post
Share on other sites
Quote:
Original post by philvaira
That above seems like a 16-bit register file which is something you don't really have to worry about in C++. That seems more like Assembly aka backbone detail.
That's for GBA development. I've seen the file, and it is something he'll need to understand.

Share this post


Link to post
Share on other sites
The article isn't really talking about memory locations.

It's basically explaining about the REG_DISPCNT register - a register is just a specific memory location. This register has 16 bits - that is... 16 positions which could be either a 0 or a 1...

It goes through explaining what happens when you set one of those bit positions to be on (1) or off(0). Presumably, this causes the GBA to respond since it probably has bits in it which monitor the register to determine what to do.

Since it's hard to remember binary bits, and what not, what he's done is provide symbolic names using #defines to create constants that basically sets the bits on or off.

This is written in hex... so
#define SCREENMODE1 0x2

Is actually refering to 0000000000000010
Since 2 represented in binary is as above... especially if it's a 16 bit register.

You can then use the binary or operator to compose a bit mask... so say you did

SCREENMODE2 | SCREENMODE5

That's really saying 0x2 | 0x5
Which is really

0000000000000010 |
0000000000000101

That's equal to
0000000000000111

Do a google on bit mask... and bitwise operators... i'm sure you'll find lots....

Share this post


Link to post
Share on other sites
I have found the following document:

http://www.vipan.com/htdocs/bitwisehelp.html

It looks to be helpful, and I hope it turns out to be :)

I am starting to sorta understand just a little bit from what you buys have spoken about :)

I assume when I can underatand this easily it will make a lot more things make sense :)

Thanks guys.

EDIT: also found this:

http://www.johnselvia.com/binary/binprime.html

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this