📜 ⬆️ ⬇️

Programming for Nintendo DS. The first steps

This article covers the basics of developing software for Nintendo DS under Linux. However, all the tools used are cross-platform and there should not be big differences for other operating systems.

For a start, let's see what this game console is all about. This is what Wikipedia tells us:
* Processor: ARM946E-S - 67 MHz, ARM7TDMI coprocessor - 33 MHz
* Memory: 4 MB, 656 KB of video memory, 512K of memory for textures
* Screen: two separate LCDs, a diagonal of 77 mm (3 inches), resolution of 256x192 pixels, up to 260 thousand colors. The distance between the screens is approximately 21 mm, which is equivalent to 92 "hidden" lines.
* Video system: 2D and 3D support (T & L, texture coordinate transformation, texture mapping, alpha blending, anti-aliasing, cel-shading and Z-buffering) theoretically allows you to draw 120,000 polygons in s 2048 triangles per frame).
* Sound: Stereo, 16-channel ADPCM / PCM
* Drives: 1 slot for Nintendo DS's own cartridge, 2 slot for Nintendo Gameboy Advance cartridge
* Connectivity: IEEE 802.11 (Wi-Fi), the connection uses Nintendo's own format. The radius of the local network from 10 to 30 meters depending on conditions.
* Control: touch screen, built-in microphone for voice recognition, A / B / X / Y buttons, D-Pad, L / R keys, Start and Select buttons
* Working time: 6-10 hours
* Weight: 275 grams
* Dimensions: 148.7 × 84.7 × 28.9 mm

As we can see, the console has two processors: ARM9 and ARM7. The main thing for us is ARM9, our programs will be executed on it. ARM7 is usually used for touchscreen, keyboard, microphone and other peripherals. The libNDS library includes a standard program for ARM7 (default.arm7), which fully satisfies the need for communication with standard peripherals. So at the initial stage we will not have to think about the work of the second processor.

Next, set up an environment for cross-compilation.
We will need:
development kit for ARM - DevkitARM sourceforge.net/projects/devkitpro/files/devkitARM ;
library for simplifying development for Nintendo DS - libNDS sourceforge.net/projects/devkitpro/files/libnds ;
as well as an emulator, for example, DeSmuME - desmume.org/download .
Create the directory / opt / devkitpro, into which we unpack devkitARM and libNDS.
Configuring environment variables:
export DEVKITPRO=/opt/devkitpro

Here, in general, everything is set up.
Now it's time to look at the graphics subsystem, and at the same time write a simple program, something like “Hello World!”. NDS has two graphic cores: the main one is used by default for the upper screen and the additional (sub) one for the lower one. Each of the cores can work in one of six modes, and the main one has 2 more additional modes for displaying large images.

Graphic modes of the video controller:
Main 2D core
Mode 0Text / 3DTextTextText
Mode 1Text / 3DTextTextRotation
Mode 2Text / 3DTextRotationRotation
Mode 3Text / 3DTextTextExtended
Mode 4Text / 3DTextRotationExtended
Mode 5Text / 3DTextExtendedExtended
Mode 63D-Large Bitmap-
Frame bufferDirect VRAM display as a bitmap
Additional 2D core
Mode 0TextTextTextText
Mode 1TextTextTextRotation
Mode 2TextTextRotationRotation
Mode 3TextTextTextExtended
Mode 4TextTextRotationExtended
Mode 5TextTextExtendedExtended

To use the kernel, we must first enable it, set the mode of operation, and “muzzle” the memory.
Our program will require the output of 2D information, therefore we include the 2D graphics core for the main screen:

Set the video mode for the graphics core:
Framebuffer mode - direct correspondence of video memory to display pixels.

Next, you need to specify the graphics core, what area of ​​memory it needs to use, because both graphics cores share common video memory. Bank A is used as a framebuffer for the top screen:

At this initialization is completed and we can already display information on the screen. For example, fill the screen with pixels of random color.
Here is the entire text of the program:
#include <nds.h>
int main()
powerOn(POWER_2D_A); // 2D
videoSetMode(MODE_FB0); //
vramSetBankA(VRAM_A_LCD); //
uint16* buffer;
while ( true ){
*buffer = RGB15(rand()/(RAND_MAX/31),rand()/(RAND_MAX/31),rand()/(RAND_MAX/31)); //

* This source code was highlighted with Source Code Highlighter .

Here is the program with the makefile: narod.ru/disk/23987391000/example1.tar.gz.html

So, we have just created, albeit a primitive, but well-functioning program for the Nintendo DS. In the next article I plan to consider working with tile graphics, interrupts, keyboard and touch screen on the example of a simple game.

And here is a very good cycle of lessons in English: dev-scene.com/NDS/Tutorials

Source: https://habr.com/ru/post/102666/

All Articles