flipacholas / Architecture-of-consoles

Technical articles about console architecture
https://www.copetti.org/writings/consoles/
Creative Commons Attribution 4.0 International
859 stars 59 forks source link

[GBA] Suggestions and corrections for the article #20

Closed Dwedit closed 3 years ago

Dwedit commented 3 years ago

This is regarding the article https://www.copetti.org/writings/consoles/game-boy-advance/

Diagram

In the Diagram, it shows the GBC cartridge having a mapper, and the GBA cartridge not having a mapper.

This is true for the vast majority of games, but a few games on the GBA were 64MB in size (such as GBA Video cartridges), and needed a mapper to address the whole 64MB cartridge. Flash cartridges also used mappers.

Memory locations

4 locations are listed there. IWRAM, EWRAM, PAK ROM, and CART RAM.

"IWRAM (Internal WRAM) → 32-bit with 32 KB: Useful for storing ARM instructions and data in big chunks."

IWRAM is the fastest memory available on the system. It has a full 32-bit bus, and executes 32-bit ARM instructions at full speed, and reads or writes 32-bit data words at full speed. But it's only 32KB, so "Big Chunks" doesn't sound like the correct term to describe it. I understand that "big chunks" means 4-byte words here, but the term seems a little misleading. You can't hold that much data into the tiny 32K of IWRAM.

Also, Video RAM is missing here. Unlike the NES, SNES, or GB/GBC, the CPU has full access to video memory whenever it wants it, it's not just for the PPU. While it is on a 16-bit bus, it still has an extremely fast connection to the CPU. It only takes 2 cycles to execute a 32-bit ARM instruction from Video RAM, and takes 1 cycle to Read/Write a 16-bit word, or 2 cycles to Read/Write a 32-bit word. This means that Video RAM can be used as "More IWRAM" if you really need the space for running more fast code, or storing more fast data.

The speed comparison is like this:

"Cart RAM -> 16-bit with variable size: This is the place where the cartridge RAM is accessed."

The bus size is incorrect here. It's an 8-bit bus, not a 16-bit bus. Furthermore, it's the only bus where you can't read or write more than 8 bits at a time. Attempting to read more than 8 bits will only give useful data in the low 8 bits of the word read back.

"Critical Sections"

Only critical sections should use the ARM instruction set. (Memory locations section)

Games are mostly written in C with critical sections in assembly (Games section)

The term "critical section" refers to concurrent programming, or multiple threads. When code is placed in a critical section, it can only be executed by one thread at a time, other threads need to wait to get in.

Perhaps change "critical sections" into "High-speed 32-bit code in fast memory".

How do they maintain compatibility?

It turns out that the GBA BIOS does see the cartridge slot switch get pressed, and it reacts by switching the system into GBC mode. The GB Micro can also be swtiched into GBC mode by software, but it is unable to read GBC cartridges.

Constructing the frame

The caption on the background says;

"Affine background layers in use, Layer 3 will be scaled to simulate water effects"

The "Affine" background layers refer to the "Mode-7" like scaling and rotation effects used by games like Mario Kart or F-Zero. The three background layers seen here are plain background layers, not affine-transformed layers.

"Raster effects" (scrolling changes performed at each scanline) can be used to perform some distortions that keep the scanlines whole. This includes effects such as horizontal skewing, simulated multi-plane parallax, wavy scrolling, vertical scaling, or other vertical wavy effects. Raster effects can be performed on plain background layers, the actual Affine layers aren't needed for those.

Tiles are grouped into charblocks. Each block is reserved for a specific type of layer.

It's not quite three 32KB independent blocks.

When you set up a background layer, you can pick one of Four different possibilities for which graphics to use. Each possibility is 32KB large, but they can start at any multiple of 16K. It could be 0K-32K, 16K-48K, 32K-64K, or 56K-64K (can't go past 64K for the background tiles, remaining 32K is for sprites only)

You also pick where the Tile Map goes. You can see some of the junk graphics sitting on "Block 2" (background 32K-64K) that look like vertical lines, those are the tile maps.

Only "Block 3" (Sprite 64K-96K) is reserved for a specific use in that video mode, and that is for sprites.

On the other side, in order to update the frame there are multiple options...

Not quite true anymore, the CPU has full access to Video RAM whenever it wants it, so there is no need to wait for Vblank time to perform DMA like on the other consoles. The only possible consequence is screen tearing due to the graphics changing the middle of the frame.

Cartridge space

SRAM: These need a battery to keep its content and can size up to 64 KB

The address space is 64K large, but Retail GBA cartridges did not exceed 32KB of SRAM. Flash cartridges did offer 64K of SRAM though.

Flashcards

Flashcards looked like ordinary Game Paks but had the addition of a card slot (SD, MiniSD, MicroSD or whatever) which enabled to run game ROMs.

The earlier flash cartridges did not have any memory card slots. Instead, the entire cartridge was NOR Flash memory, plus battery backed SRAM for saves. You booted a small program over the link cable which would rewrite the cartridge, using data received over the link cable.

flipacholas commented 3 years ago

Thanks for all these corrections! I'm going over them just now and I'm only having problems with the backwards compatibility one. I'm reading some disassemblies and docs of the GBA BIOS and I can't find much info about the GBA boot process (specifically, when and how it switches to GBC mode).

It turns out that the GBA BIOS does see the cartridge slot switch get pressed, and it reacts by switching the system into GBC mode.

Could you tell me where that info was found? So far, thanks to this project I can back up how the GBA switches to GBC mode in software, but tcrf lists the GBC boot routine as unused.

Edit: I've just realised you are the author of that program! The statement I'm trying to back up is if during boot, in order to switch into GBC mode with a GBC game inserted, the GBA boot code checks the state of the switch and just changes DISPCNT. Leaving the ARM7 core still working (hence why the display can still be tweaked?). Then I'm guessing the GBC CPU is in charge?

Dwedit commented 3 years ago

I think what happens during GBC boot is that it checks the switch (readable at REG_WAITCNT 0x4000204), does the fade does the fade (a very fast fade, hard to notice), then finally switches to GBC mode (BIOS writes to REG_DISPCNT 0x4000000), stopping the ARM7.

The only missing piece of the puzzle is what would happen if you were to remove a portion of the GBC cartridge shell so the switch isn't pressed anymore, then did a software mode-switch to GBC mode. Multiboot mode could help here. I'm not sure if the switch needs to be pressed down for the GBC cartridge bus to work properly, or if it just works. I'm willing to guess that the switch is necessary for the bus to function, but that's just a guess.

I had gotten some of the facts wrong earlier, so I just deleted the section that I had previously added to TCRF. It's the fade being so quick that threw me off and made me think it didn't happen.

selb commented 3 years ago

More detail on early-ish GBA flash carts:

One well-preserved site with info on the cart I actually had back then: https://web.archive.org/web/20200216121318/http://www.ezfadvance.com/cards/EZ-Flash_2.htm

AntonioND commented 3 years ago

It turns out that the GBA BIOS does see the cartridge slot switch get pressed, and it reacts by switching the system into GBC mode.

Could you tell me where that info was found? So far, thanks to this project I can back up how the GBA switches to GBC mode in software, but tcrf lists the GBC boot routine as unused.

Edit: I've just realised you are the author of that program! The statement I'm trying to back up is if during boot, in order to switch into GBC mode with a GBC game inserted, the GBA boot code checks the state of the switch and just changes DISPCNT. Leaving the ARM7 core still working (hence why the display can still be tweaked?). Then I'm guessing the GBC CPU is in charge?

I'm the owner of the repository you have linked to. Actually, Extrems figured out a few days ago why that code wasn't working, it needed to be placed in IWRAM. It has always been a software switch: https://www.youtube.com/watch?v=pcIQQPOkBGI

flipacholas commented 3 years ago

@AntonioND From what I understood EWRAM is hidden in GBC mode (right?), but what about the shape detector switch? Would gba-switch-to-gbc still work if it weren't there?

flipacholas commented 3 years ago

Btw I don't mind not getting 'the final answer' to this. In fact I was planning in quoting @Dwedit answer (if you are ok with this) and leave it as a 'non-confirmed yet', which is still fine (the same is happening with the original Xbox's DVD protection for what it's worth).

AntonioND commented 3 years ago

As far as I can tell, this confirms that the mode switch is done by software. The shape detector switch can be seen in a bit in register WAITCNT, so I guess that the SoC enables and disables some hardware automatically depending on its value.

For now, the shape switch seems to affect:

Dwedit's first paragraph seems to be exactly what's going on, but I wouldn't say it's not confirmed. When I analyzed the BIOS routine that does the switch I saw the same thing.

flipacholas commented 3 years ago

Thank you everyone, this was a very good insight. I have updated the article now