Skip to content

BMDE host components installation in Linux

1. Objectives

The objective of this doc is to explain how to install and verify installation in the host of the BMDE toolchain components. The BMDE is used in subjects:

  • Computers (primary focus): The whole Computers subject uses all software described in this documentation.
  • Operating Systems Structure (ESO) (secondary focus): Only the practical part of this subject needs all software described in this documentation.
  • Fundamentals of Computers (FC) (tertiary focus): Only debug, build and edit.

2. Deployment Strategy and Docker Considerations

Since Milax is the de-facto Linux environment used by students, and the system image is managed centrally, the recommended approach is to use bare-metal installations, instead of using the Docker based installations already built for the BMDE CLI.

When tools are already deployed directly on an operating system under full administrative control (as milax is), Docker does not provide significant added value. Therefore, a native package-based installation is preferred.

Docker may still be offered as an optional alternative, knowing Docker itself is installed on the milax image. It is not necessary to integrate specific Docker images directly into the Milax distribution.


3. BMDE operations vs software components

A component can be understood as an independent piece of software. One or more components are used together for each operation of the BMDE.

This guide will explain how to:

  • Install each component, either the explanation of each step and the proposed commands to do it.
  • Test the installation of each needed component.
  • Test that each operation is working as expected.

The needed files for everything are in the repository of each component, in the components/ folder of the bmde repository (cloned with git clone --recursive): its download script (scripts/download_assets.sh), its install scripts (src/) and its tests (test/). All proposed commands assume that the current directory is the root of that repository.


4. Installation considerations

scripts/install.sh of the repository of bmde runs every step of this guide (downloads and installation) for the components you choose, in order: see components installation. This guide explains each of those steps, to run them one by one and to test each component.

Each component is installed in two stages: 1. Download: scripts/download_assets.sh of its repository downloads the files that come from outside of the repository (e.g. devkitARM, the sources of DeSmuME) into its assets/ folder. If a file is already there, it only checks it. Every file with a fixed version is checked against its pinned SHA-256, and the script fails if it does not match. Run it with names of assets as arguments to download only those. All these external resources are listed in external resources. It downloads them with curl, which is not installed in every Ubuntu: install it with sudo apt-get install -y curl first. 2. Installation: the scripts in the src/ folder of its repository, which only take files from the repository (src/, config/ and assets/), never from the internet.

The same scripts install the components in the Docker image of each component, and in the image with the full development environment (aleixmt/bmde-full), so the three installations do not drift apart. The scripts are tested on Ubuntu 24.04 and 26.04 by the CI of each component.

All scripts follow the same conventions: * Programs are installed into /URV/bmde/PROGRAM (e.g. /URV/bmde/devkitPro, /URV/bmde/DeSmuME_0.9.11, /URV/bmde/insight). Set the variable BMDE_ROOT to install them into another directory. * The environment of each program (PATH, DEVKITPRO, DESMUME...) is installed into /etc/profile.d/bmde-PROGRAM.sh, which is loaded in new sessions. To load it in the current terminal, run . /etc/profile.d/bmde-PROGRAM.sh (or open a new session). * They install their own dependencies with apt-get, using sudo when they are not run as root. * They can be run again safely: a program that is already installed is not built again. * They need the files downloaded by the download script, and fail asking to run it if they are missing.

Each test of a component ends printing ... OK (e.g. libnds OK) when it passes, and stops with an error when it does not.

Each code snippet assume that previous snippets have been run successfully.

Snippets can be executed:

  • Manually, copying code line by line to a terminal. This only works for the short snippets (e.g. the download commands): the install scripts and the tests find the files of their component (assets/, config/...) from the path of their file, so run them with their file, as explained below.
  • Using the corresponding file for that snippet. The corresponding file for each snippet can be found in the source code of each snippet in this guide. This is because each snippet is embedded in the docs with mkdocs or with the script scripts/render-guide.py. Each script can be run with bash components/COMPONENT/src/SCRIPT.sh (or bash components/COMPONENT/test/SCRIPT.sh for the tests).

  • Using an IDE that supports running markdown snippets or installing extensions for that purpose. In PyCharm you can click in the green button of each snippet to run it in the embedded terminal. We recommend unchecking Close session when it ends, in Settings->Tools->Terminal to see the results after running the script. Also, notice that there are some calls to sudo in the snippets, which usually stops the execution due to the command asking for the password. What we can do to solve this is: Execute the snippet, wait for the password prompt to appear and input your password. After the command with authentication has run (and the snippet execution has stopped in the middle), run the snippet again. Since sudo caches the recent authentications, this time the sudo command will not interrupt the snippet execution.

The tests of graphical programs (DeSmuME, Insight) open their windows for a few seconds; without a display (e.g. through SSH) they use a virtual one, which needs xvfb-run (package xvfb).

Operations

Build Operation

Components:

  • make
  • devkitARM r46:
  • arm-none-eabi-gcc
  • arm-none-eabi-as
  • arm-none-eabi-ld
  • ndstool (not needed for FC subject):
  • libNDS (not needed for FC subject):
  • devkitPro NDS project examples (optional)

Component installation

make

Explanation

Install make using your package manager.

The installed binary is expected to be automatically included in your $PATH by the package manager and because of that available directly through shell commands.

Proposed command
#!/usr/bin/env bash
# Installs make. Standalone: can be run in an Ubuntu / Debian host, and is also used by the BMDE Docker images.
set -Eeuo pipefail

SUDO=""; if [ "$(id -u)" -ne 0 ]; then SUDO=sudo; fi

${SUDO} apt-get update
${SUDO} apt-get install -y --no-install-recommends make
Test component
#!/usr/bin/env bash
# Tests that make is installed.
set -Eeuo pipefail

make --version
echo "make OK"

devkitARM

Explanation

Download devkitARM from here and decompress it into /URV/bmde/devkitPro.

Create 3 permanent environment variables (in /etc/profile.d/bmde-devkitpro.sh), which are used in the Makefiles: * DEVKITPRO with value /URV/bmde/devkitPro. * DEVKITARM with value /URV/bmde/devkitPro/devkitARM. * DESMUME with any value if it is not defined (it just needs to be defined so that it passes the checks of the Makefiles; the installation of DeSmuME gives it its real value).

Modify permanently the PATH variable, prepending (writing at the beginning) the path $DEVKITARM/bin to the variable. Including the directories in the $PATH make the files in the included directories directly available through shell commands. The prepending is done to give more priority to our binaries than the system binaries.

Download assets

Download devkitARM r46 (and the hello world project of the course, used by the test of the operation) into components/devkitarm-nds-docker/assets/.

components/devkitarm-nds-docker/scripts/download_assets.sh
Proposed command
#!/usr/bin/env bash
# Installs devkitARM r46 into /URV/bmde/devkitPro/devkitARM, and its environment (DEVKITPRO, DEVKITARM, PATH) into
# /etc/profile.d/bmde-devkitpro.sh. Can be run in an Ubuntu / Debian host from a checkout of the repository (after
# scripts/download_assets.sh), and is also used by the BMDE Docker images. Set BMDE_ROOT to install into another
# directory.
set -Eeuo pipefail

BMDE_ROOT="${BMDE_ROOT:-/URV/bmde}"
SUDO=""; if [ "$(id -u)" -ne 0 ]; then SUDO=sudo; fi
DEVKITPRO="${BMDE_ROOT}/devkitPro"
DEVKITARM="${DEVKITPRO}/devkitARM"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]:-$0}")" && pwd)"
DEVKITARM_TARBALL="${SCRIPT_DIR}/../assets/devkitARM_r46-x86_64-linux.tar.bz2"

if [ ! -x "${DEVKITARM}/bin/arm-none-eabi-gcc" ]; then
  if [ ! -f "${DEVKITARM_TARBALL}" ]; then
    echo "${DEVKITARM_TARBALL} not found: download it with scripts/download_assets.sh" >&2
    exit 1
  fi
  ${SUDO} apt-get update
  ${SUDO} apt-get install -y --no-install-recommends bzip2
  ${SUDO} mkdir -p "${DEVKITPRO}"
  ${SUDO} rm -rf "${DEVKITARM}"
  ${SUDO} tar -xjf "${DEVKITARM_TARBALL}" -C "${DEVKITPRO}"
fi

# DESMUME is needed by the Makefiles of the course even to build; the DeSmuME install sets its real value
${SUDO} tee /etc/profile.d/bmde-devkitpro.sh > /dev/null <<PROFILE
export DEVKITPRO="${DEVKITPRO}"
export DEVKITARM="${DEVKITARM}"
if [ -z "\${DESMUME:-}" ]; then export DESMUME=/; fi
case ":\${PATH}:" in
  *":${DEVKITARM}/bin:"*) ;;
  *) export PATH="${DEVKITARM}/bin:\${PATH}" ;;
esac
PROFILE

echo "devkitARM installed in ${DEVKITARM}. Open a new session or run: . /etc/profile.d/bmde-devkitpro.sh"
Test component
#!/usr/bin/env bash
# Tests that devkitARM is installed and its environment loaded.
set -Eeuo pipefail

: "${DEVKITPRO:?DEVKITPRO is not set: open a new session or run . /etc/profile.d/bmde-devkitpro.sh}"
: "${DEVKITARM:?DEVKITARM is not set: open a new session or run . /etc/profile.d/bmde-devkitpro.sh}"
arm-none-eabi-gcc --version
arm-none-eabi-as --version
arm-none-eabi-ld --version
ndstool | head -n 1
echo "devkitARM OK"

libnds

Explanation

Download and decompress the file libnds.tar.bz2 into $DEVKITPRO. The file libnds.tar.bz2 has been obtained directly from the original BMDE folder implementation, so in case of update of the original BMDE, it will be needed to update the provided file.

This component is only used in the compilation of projects, so its testing involves the building of a NDS project (operation test). That is why the test will be skipped for this component and the corresponding folder hello-world-nds is provided.

Proposed command
#!/usr/bin/env bash
# Installs the libnds of the course into /URV/bmde/devkitPro/libnds. Needs devkitARM (install_devkitarm.sh).
# Can be run in an Ubuntu / Debian host from a checkout of the repository, and is also used by the BMDE Docker images.
set -Eeuo pipefail

BMDE_ROOT="${BMDE_ROOT:-/URV/bmde}"
SUDO=""; if [ "$(id -u)" -ne 0 ]; then SUDO=sudo; fi
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]:-$0}")" && pwd)"

if [ ! -d "${BMDE_ROOT}/devkitPro/libnds/lib" ]; then
  ${SUDO} mkdir -p "${BMDE_ROOT}/devkitPro"
  ${SUDO} rm -rf "${BMDE_ROOT}/devkitPro/libnds"
  ${SUDO} tar -xjf "${SCRIPT_DIR}/../assets/libnds.tar.bz2" -C "${BMDE_ROOT}/devkitPro"
fi

echo "libnds installed in ${BMDE_ROOT}/devkitPro/libnds"
Test component

The test builds a small program with libnds:

#!/usr/bin/env bash
# Tests that libnds is installed, building one of the project templates of the course with it.
set -Eeuo pipefail

: "${DEVKITPRO:?DEVKITPRO is not set: open a new session or run . /etc/profile.d/bmde-devkitpro.sh}"
[ -f "${DEVKITPRO}/libnds/lib/libnds9.a" ]
cat > /tmp/test_libnds.c <<'C'
#include <nds.h>
int main(void) { consoleDemoInit(); while (1) swiWaitForVBlank(); }
C
arm-none-eabi-gcc -march=armv5te -mtune=arm946e-s -DARM9 -specs=ds_arm9.specs -I"${DEVKITPRO}/libnds/include" \
  /tmp/test_libnds.c -L"${DEVKITPRO}/libnds/lib" -lnds9 -o /tmp/test_libnds.elf
rm -f /tmp/test_libnds.c /tmp/test_libnds.elf
echo "libnds OK"

It may be possible that you need to modify a minor thing in the internal file ds_rules, that provides building rules for NDS projects.

devkitPro NDS project examples (optional)

Explanation

Decompress the file nds-examples.tar.gz (the NDS examples of devkitPro, downloaded into assets/ with the other assets) into $DEVKITPRO/examples/nds.

Proposed command
#!/usr/bin/env bash
# Installs the devkitPro NDS project examples into /URV/bmde/devkitPro/examples/nds (optional). Can be run in an
# Ubuntu / Debian host from a checkout of the repository (after scripts/download_assets.sh), and is used by the BMDE
# full image and by the tests of the examples; the image of the component does not include them. Set BMDE_ROOT to
# install into another directory.
set -Eeuo pipefail

BMDE_ROOT="${BMDE_ROOT:-/URV/bmde}"
SUDO=""; if [ "$(id -u)" -ne 0 ]; then SUDO=sudo; fi
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]:-$0}")" && pwd)"
EXAMPLES_DIR="${BMDE_ROOT}/devkitPro/examples/nds"
EXAMPLES_TARBALL="${SCRIPT_DIR}/../assets/nds-examples.tar.gz"

if [ ! -d "${EXAMPLES_DIR}" ]; then
  if [ ! -f "${EXAMPLES_TARBALL}" ]; then
    echo "${EXAMPLES_TARBALL} not found: download it with scripts/download_assets.sh" >&2
    exit 1
  fi
  ${SUDO} mkdir -p "${EXAMPLES_DIR}"
  ${SUDO} tar -xzf "${EXAMPLES_TARBALL}" -C "${EXAMPLES_DIR}" --strip-components=1
fi

echo "Examples installed in ${EXAMPLES_DIR}"
Test component

The test builds some of the examples:

#!/usr/bin/env bash
# Tests that the examples are installed, building some of them (it needs devkitARM and libnds). If they are not
# installed (e.g. in the image of the component, which does not include them), installs them into a temporary
# directory with install_examples.sh, which tests it.
set -Eeuo pipefail

: "${DEVKITPRO:?DEVKITPRO is not set: open a new session or run . /etc/profile.d/bmde-devkitpro.sh}"
TEST_DIR="$(cd "$(dirname "${BASH_SOURCE[0]:-$0}")" && pwd)"
build_dir="$(mktemp -d)"
examples="${DEVKITPRO}/examples/nds"
if [ ! -d "${examples}" ]; then
  BMDE_ROOT="${build_dir}/root" "${TEST_DIR}/../src/install_examples.sh"
  examples="${build_dir}/root/devkitPro/examples/nds"
fi
cp -r "${examples}/hello_world" "${examples}/time" "${build_dir}"
for example in "${build_dir}"/hello_world/ "${build_dir}"/time/; do
  make -C "${example}"
  find "${example}" -name '*.nds' | grep .
done
rm -rf "${build_dir}"
echo "devkitPro examples OK"

Test operation

The test builds the hello world project of the course:

#!/usr/bin/env bash
# Tests the build operation: builds the hello world project of the course.
set -Eeuo pipefail

work_dir="$(mktemp -d)"
# Hello world project of the course, downloaded by scripts/download_assets.sh
mkdir "${work_dir}/hello-world-nds"
tar -xzf "$(cd "$(dirname "${BASH_SOURCE[0]:-$0}")" && pwd)/../assets/hello-world-nds.tar.gz" \
  -C "${work_dir}/hello-world-nds" --strip-components=1
make -C "${work_dir}/hello-world-nds"
ls "${work_dir}"/hello-world-nds/*.nds
rm -rf "${work_dir}"
echo "build operation OK"

It may be possible that you need to modify a minor thing in the internal file ds_rules, that provides building rules for NDS projects.


Run Operation

Components:

  • DeSmuME 0.9.11
  • $DESMUME/DeSmuME.exe and $DESMUME/DeSmuME_dev.exe wrappers (optional, to be compatible with Makefiles)
  • make, for make run target (make already explained in build section)

Component installation

desmume
Explanation

Install desmume using your package manager. The installed binary is expected to be automatically included in your $PATH by the package manager and because of that available directly through shell commands.

The distributions do not provide DeSmuME 0.9.11 (the version of the course) anymore, nor with the HUD (display of FPS, input...) and the GDB stub that the course needs, so it is built from the sources of its Debian package into /URV/bmde/DeSmuME_0.9.11. The build takes a few minutes.

Create (or modify) 1 permanent environment variable (in /etc/profile.d/bmde-desmume.sh). This is used in the run and debug targets of the Makefiles: * DESMUME with value /URV/bmde/DeSmuME_0.9.11.

It also installs desmume-ini2gtk, which applies the configuration of the Windows DeSmuME (desmume.ini: controls, HUD) to this one. To use the controls of the course, run desmume-ini2gtk /URV/bmde/DeSmuME_0.9.11/share/bmde/desmume.ini.

Download assets

Download the sources of DeSmuME 0.9.11 (and the ROM and project used by the tests) into components/desmume-docker/assets/. Its other asset, filesystem-nds.nds, is only used by the tests of the Docker image, and it can't be built yet: it needs the DLDI patch installed in the patch operation.

components/desmume-docker/scripts/download_assets.sh desmume_0.9.11.orig.tar.gz desmume_0.9.11-3build2.diff.gz \
  hello-world-nds.nds hello-world-nds.tar.gz
Proposed command
#!/usr/bin/env bash
# Installs DeSmuME 0.9.11 (the version of the course) into /URV/bmde/DeSmuME_0.9.11, and its environment (DESMUME,
# PATH) into /etc/profile.d/bmde-desmume.sh. Can be run in an Ubuntu / Debian host from a checkout of the repository
# (after scripts/download_assets.sh), and is also used by the BMDE Docker images. Set BMDE_ROOT to install into another
# directory.
#
# DeSmuME is built from the sources of the Debian / Ubuntu package (in assets/), with HUD and GDB stub
# support, which the distribution packages lack or do not provide for this version anymore. Also installs the tool to
# apply the configuration of the Windows DeSmuME (desmume-ini2gtk) and the default configuration of the course.
set -Eeuo pipefail

BMDE_ROOT="${BMDE_ROOT:-/URV/bmde}"
SUDO=""; if [ "$(id -u)" -ne 0 ]; then SUDO=sudo; fi
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]:-$0}")" && pwd)"
ASSETS_DIR="${SCRIPT_DIR}/../assets"
DESMUME_DIR="${BMDE_ROOT}/DeSmuME_0.9.11"
# Packages needed to run DeSmuME, computed when building it
RUNTIME_PACKAGES="${DESMUME_DIR}/share/bmde/runtime-packages"

# Sources of the Ubuntu package desmume 0.9.11-3build2
DESMUME_ORIG="${ASSETS_DIR}/desmume_0.9.11.orig.tar.gz"
DESMUME_DIFF="${ASSETS_DIR}/desmume_0.9.11-3build2.diff.gz"

build_desmume() {
  local source
  for source in "${DESMUME_ORIG}" "${DESMUME_DIFF}"; do
    if [ ! -f "${source}" ]; then
      echo "${source} not found: download it with scripts/download_assets.sh" >&2
      exit 1
    fi
  done
  ${SUDO} apt-get update
  ${SUDO} apt-get install -y --no-install-recommends \
    build-essential autoconf automake intltool pkg-config patch \
    libgtk2.0-dev libsdl1.2-dev libagg-dev libsoundtouch-dev libopenal-dev libosmesa6-dev libglu1-mesa-dev \
    libtinyxml-dev libpcap-dev zlib1g-dev

  local build_dir
  build_dir="$(mktemp -d)"
  tar -xzf "${DESMUME_ORIG}" -C "${build_dir}"
  (
    cd "${build_dir}/desmume-0.9.11"
    # Patches of the Debian package (build fixes for newer compilers)
    zcat "${DESMUME_DIFF}" | patch -p1 -s
    while read -r p; do
      patch -p1 -s < "debian/patches/${p}"
    done < debian/patches/series
    # libagg >= 2.6 dropped comp_op_minus / comp_op_contrast, which the HUD code of DeSmuME never uses
    sed -i '/agg::comp_op_minus\|agg::comp_op_contrast/d' src/agg2d.h
    autoreconf -fi
    ./configure --prefix="${DESMUME_DIR}" --enable-hud --enable-gdb-stub --enable-osmesa
    make -j"$(nproc)"
    ${SUDO} make install
  )
  rm -rf "${build_dir}"

  # Packages of the shared libraries that DeSmuME uses, to install them without the build dependencies
  ${SUDO} mkdir -p "$(dirname "${RUNTIME_PACKAGES}")"
  ldd "${DESMUME_DIR}/bin/desmume" "${DESMUME_DIR}/bin/desmume-cli" \
    | awk '/=> \// { print $3 }' | sort -u | xargs -r realpath \
    | xargs -r dpkg -S 2>/dev/null | cut -d: -f1 | sort -u \
    | ${SUDO} tee "${RUNTIME_PACKAGES}" > /dev/null
}

if [ ! -x "${DESMUME_DIR}/bin/desmume" ]; then
  build_desmume
fi

# Runtime dependencies (python3 for desmume-ini2gtk)
${SUDO} apt-get update
# shellcheck disable=SC2046
${SUDO} apt-get install -y --no-install-recommends python3 $(cat "${RUNTIME_PACKAGES}")

# Configuration tool and default configuration (controls, HUD) of the course
${SUDO} mkdir -p "${DESMUME_DIR}/share/bmde"
${SUDO} install -m 755 "${SCRIPT_DIR}/desmume-ini2gtk.py" "${DESMUME_DIR}/bin/desmume-ini2gtk"
${SUDO} install -m 644 "${SCRIPT_DIR}/../config/desmume.ini" "${DESMUME_DIR}/share/bmde/desmume.ini"

${SUDO} tee /etc/profile.d/bmde-desmume.sh > /dev/null <<PROFILE
export DESMUME="${DESMUME_DIR}"
case ":\${PATH}:" in
  *":${DESMUME_DIR}/bin:"*) ;;
  *) export PATH="${DESMUME_DIR}/bin:\${PATH}" ;;
esac
PROFILE

echo "DeSmuME installed in ${DESMUME_DIR}. Open a new session or run: . /etc/profile.d/bmde-desmume.sh"
echo "To use the controls of the course: desmume-ini2gtk ${DESMUME_DIR}/share/bmde/desmume.ini"
Test component

The test runs an NDS example for a few seconds:

#!/usr/bin/env bash
# Tests that DeSmuME is installed, running a ROM for a few seconds. Without a display (e.g. in a container) it uses a
# virtual one (xvfb-run).
set -Eeuo pipefail

# Test ROM (hello-world-nds), downloaded by scripts/download_assets.sh
ROM="$(cd "$(dirname "${BASH_SOURCE[0]:-$0}")" && pwd)/../assets/hello-world-nds.nds"

work_dir="$(mktemp -d)"
cp "${ROM}" "${work_dir}/test.nds"
display=(); if [ -z "${DISPLAY:-}" ]; then display=(xvfb-run -a); fi

# DeSmuME runs until it is closed, so it works if it is still running after a few seconds
status=0
timeout 5 "${display[@]}" "${DESMUME_COMMAND:-desmume}" --disable-sound "${work_dir}/test.nds" || status=$?
rm -rf "${work_dir}"
[ "${status}" -eq 124 ]
echo "DeSmuME OK"
You should see something similar to this:

img.png

To test the configuration tool:

#!/usr/bin/env bash
# Tests the tool that applies the configuration of the Windows DeSmuME (desmume.ini), with the default configuration
# of the course.
set -Eeuo pipefail

: "${DESMUME:?DESMUME is not set: open a new session or run . /etc/profile.d/bmde-desmume.sh}"
config_dir="$(mktemp -d)"
desmume-ini2gtk "${DESMUME}/share/bmde/desmume.ini" "${config_dir}"
grep -q "^\[KEYS\]" "${config_dir}/config"
grep -q "^Fps=true" "${config_dir}/config.cfg"
rm -rf "${config_dir}"
echo "desmume-ini2gtk OK"

$DESMUME/DeSmuME.exe and $DESMUME/DeSmuME_dev.exe wrappers
Explanation

Optionally, to make the installation of DeSmuME compatible with the run and debug targets of Makefiles, we need to make available the files $DESMUME/DeSmuME.exe and $DESMUME/DeSmuME_dev.exe, because they are explicitly referenced by the Makefiles.

To do so, we can create wrappers that forward the calls into desmume. We could implement this in another way, such a shell alias.

Proposed command
#!/usr/bin/env bash
# Installs the wrappers $DESMUME/DeSmuME.exe and $DESMUME/DeSmuME_dev.exe, which the Makefiles of the course use to run
# (make run) and debug (make debug) the projects as in the Windows BMDE. Needs DeSmuME (install_desmume.sh).
# Standalone: can be run in an Ubuntu / Debian host, and is also used by the BMDE Docker images.
set -Eeuo pipefail

BMDE_ROOT="${BMDE_ROOT:-/URV/bmde}"
SUDO=""; if [ "$(id -u)" -ne 0 ]; then SUDO=sudo; fi
DESMUME_DIR="${BMDE_ROOT}/DeSmuME_0.9.11"

for wrapper in DeSmuME.exe DeSmuME_dev.exe; do
  ${SUDO} tee "${DESMUME_DIR}/${wrapper}" > /dev/null <<WRAPPER
#!/usr/bin/env sh
exec "${DESMUME_DIR}/bin/desmume" "\$@"
WRAPPER
  ${SUDO} chmod +x "${DESMUME_DIR}/${wrapper}"
done

echo "Wrappers installed in ${DESMUME_DIR}"
Test component
#!/usr/bin/env bash
# Tests the wrappers $DESMUME/DeSmuME.exe and $DESMUME/DeSmuME_dev.exe used by the Makefiles of the course, running a
# ROM with them as test_desmume.sh does.
set -Eeuo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]:-$0}")" && pwd)"
: "${DESMUME:?DESMUME is not set: open a new session or run . /etc/profile.d/bmde-desmume.sh}"
for wrapper in DeSmuME.exe DeSmuME_dev.exe; do
  DESMUME_COMMAND="${DESMUME}/${wrapper}" "${SCRIPT_DIR}/test_desmume.sh"
done
echo "DeSmuME wrappers OK"

You should see appear the window of DeSmuME.

Test operation

The test builds the hello world project of the course and runs it with make run:

#!/usr/bin/env bash
# Tests the run operation: builds the hello world project of the course and runs it with its Makefile (make run), as
# in the Windows BMDE. Needs devkitARM and libnds. Without a display (e.g. in a container) it uses a virtual one.
set -Eeuo pipefail

work_dir="$(mktemp -d)"
# Hello world project of the course, downloaded by scripts/download_assets.sh
mkdir "${work_dir}/hello-world-nds"
tar -xzf "$(cd "$(dirname "${BASH_SOURCE[0]:-$0}")" && pwd)/../assets/hello-world-nds.tar.gz" \
  -C "${work_dir}/hello-world-nds" --strip-components=1
make -C "${work_dir}/hello-world-nds"
display=(); if [ -z "${DISPLAY:-}" ]; then display=(xvfb-run -a); fi

# make run starts DeSmuME in the background: it works if it is still running after a few seconds
"${display[@]}" bash -c "make -C '${work_dir}/hello-world-nds' run && sleep 5 && pgrep -x desmume && pkill -x desmume"
rm -rf "${work_dir}"
echo "make run OK"

You should see the DeSmuME window appearing with the hello-world message.

This test is accumulative with the build operation, because it implies it, so, if a functional .nds binary can be generated from source code and executed using desmume, the environment can be considered operational.


Debug Operation

Components: * DeSmuME 0.9.11 with argument --arm9gdb=$PORT to listen to TCP port $PORT * GDB debugger. Can be one of the following: - insight, the graphical debugger of the Windows BMDE (same version, built from the same source code) - arm-eabi-gdb, the GDB of the same build of Insight, in the terminal - gdb-multiarch, available through package manager * make, for make debug target (make already explained in build operation section)

Component installation

DeSmuME 0.9.11 with argument --arm9gdb=$PORT to listen to TCP port $PORT
Explanation

Using the desmume command obtained in the previous section Run Operation with argument --arm9gdb=$PORT. We recommend PORT to be higher or equal to 1024 to avoid problems, since lower ports are privileged and only root can use them.

Unluckily the Makefiles have the hardcoded argument --arm9gdb=1000 in the make debug target. To use this target as a normal user, allow it to use privileged ports from 1000 with sudo sysctl net.ipv4.ip_unprivileged_port_start=1000.

Test component
#!/usr/bin/env bash
# Tests the GDB stub of DeSmuME, used to debug: runs a ROM with the stub enabled and checks that it accepts connections.
# Without a display (e.g. in a container) it uses a virtual one (xvfb-run).
set -Eeuo pipefail

# Test ROM (hello-world-nds), downloaded by scripts/download_assets.sh
ROM="$(cd "$(dirname "${BASH_SOURCE[0]:-$0}")" && pwd)/../assets/hello-world-nds.nds"
PORT=1024

work_dir="$(mktemp -d)"
cp "${ROM}" "${work_dir}/test.nds"
display=(); if [ -z "${DISPLAY:-}" ]; then display=(xvfb-run -a); fi

timeout 15 "${display[@]}" desmume --disable-sound --arm9gdb="${PORT}" "${work_dir}/test.nds" > /dev/null 2>&1 &
desmume_pid=$!
listening=1
for _ in $(seq 10); do
  if timeout 1 bash -c "</dev/tcp/localhost/${PORT}" 2>/dev/null; then
    listening=0
    break
  fi
  sleep 1
done
kill "${desmume_pid}" 2>/dev/null || true
wait "${desmume_pid}" 2>/dev/null || true
rm -rf "${work_dir}"
[ "${listening}" -eq 0 ]
echo "DeSmuME listening to port ${PORT}"
echo "DeSmuME GDB stub OK"

You should see DeSmuME listening to port 1024 and DeSmuME GDB stub OK in your terminal if everything was OK.

insight and arm-eabi-gdb
Explanation

Insight is the original debugger (and simulator for FC) in the BMDE. The installed version is the same one of the Windows BMDE (GDB/Insight 7.3.50.20110803 for arm-eabi), built from the same source code. It also provides arm-eabi-gdb, the GDB of the same build for the terminal (the arm-none-eabi-gdb of devkitARM r46 needs the library ncurses 5, which current distributions do not provide anymore).

The 2011 source code needs a compiler of its time, so it is built in Ubuntu 14.04 (in the build stage of the Docker image of the component, with build_insight.sh) into a tarball that works in current distributions. The script installs that tarball into /URV/bmde/insight (set INSIGHT_TARBALL to install another file).

Download assets and build the tarball

Download the sources of Insight, reconstructed from the CVS repository of sourceware.org (~900 MB download, it needs rsync and cvs), and the project used by the test of the operation into components/insight-docker/assets/. Then build the tarball of Insight with the build stage of the Docker image (it needs Docker) into components/insight-docker/build/.

sudo apt-get install -y rsync cvs
components/insight-docker/scripts/download_assets.sh
components/insight-docker/scripts/build_tarball.sh
Proposed command
#!/usr/bin/env bash
# Installs Insight 7.3.50.20110803-cvs (the version of the Windows BMDE, with arm-eabi-insight and arm-eabi-gdb) into
# /URV/bmde/insight, and its environment (PATH) into /etc/profile.d/bmde-insight.sh. Can be run in an Ubuntu / Debian
# host from a checkout of the repository (after scripts/download_assets.sh), and is also used by the BMDE Docker
# images. Set BMDE_ROOT to install into another directory.
#
# Insight is installed from the tarball built by build_insight.sh, given in INSIGHT_TARBALL (by default the one built
# by scripts/build_tarball.sh, in build/). Also installs the script that connects the Run button of
# Insight to the GDB stub in GDB_TARGET (used by the Docker image).
set -Eeuo pipefail

BMDE_ROOT="${BMDE_ROOT:-/URV/bmde}"
SUDO=""; if [ "$(id -u)" -ne 0 ]; then SUDO=sudo; fi
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]:-$0}")" && pwd)"
INSIGHT_DIR="${BMDE_ROOT}/insight"
INSIGHT_TARBALL="${INSIGHT_TARBALL:-${SCRIPT_DIR}/../build/insight-7.3.50.20110803-cvs-linux-x86_64.tar.xz}"

${SUDO} apt-get update
${SUDO} apt-get install -y --no-install-recommends xz-utils libx11-6 libexpat1 zlib1g

if [ ! -x "${INSIGHT_DIR}/bin/arm-eabi-insight" ]; then
  if [ ! -f "${INSIGHT_TARBALL}" ]; then
    echo "${INSIGHT_TARBALL} not found: build it with scripts/build_tarball.sh" >&2
    exit 1
  fi
  ${SUDO} mkdir -p "${BMDE_ROOT}"
  ${SUDO} tar -xJf "${INSIGHT_TARBALL}" -C "${BMDE_ROOT}"
fi

# Not a symlink: Tcl finds its library relative to the path of the executable
${SUDO} tee "${INSIGHT_DIR}/bin/insight" > /dev/null <<WRAPPER
#!/usr/bin/env sh
exec "${INSIGHT_DIR}/bin/arm-eabi-insight" "\$@"
WRAPPER
${SUDO} chmod +x "${INSIGHT_DIR}/bin/insight"

${SUDO} mkdir -p "${INSIGHT_DIR}/share/bmde"
${SUDO} install -m 644 "${SCRIPT_DIR}/connect.tcl" "${INSIGHT_DIR}/share/bmde/connect.tcl"

${SUDO} tee /etc/profile.d/bmde-insight.sh > /dev/null <<PROFILE
case ":\${PATH}:" in
  *":${INSIGHT_DIR}/bin:"*) ;;
  *) export PATH="${INSIGHT_DIR}/bin:\${PATH}" ;;
esac
PROFILE

echo "Insight installed in ${INSIGHT_DIR}. Open a new session or run: . /etc/profile.d/bmde-insight.sh"
Test component
#!/usr/bin/env bash
# Tests that Insight is installed, with the version of the Windows BMDE.
set -Eeuo pipefail

insight --version | tee /dev/stderr | grep -q "7.3.50.20110803-cvs"
echo "Insight OK"
#!/usr/bin/env bash
# Tests arm-eabi-gdb, the terminal GDB of the same build of Insight, debugging ARM programs.
set -Eeuo pipefail

arm-eabi-gdb --version | head -n 1
arm-eabi-gdb -batch -ex "show architecture" | grep -q "arm"
echo "arm-eabi-gdb OK"
gdb-multiarch
Explanation

Standard multi-architecture GDB provided via package manager.

Proposed command
#!/usr/bin/env bash
# Installs gdb-multiarch, a terminal debugger alternative to Insight. Standalone: can be run in an Ubuntu / Debian host,
# and is also used by the BMDE Docker images.
set -Eeuo pipefail

SUDO=""; if [ "$(id -u)" -ne 0 ]; then SUDO=sudo; fi

${SUDO} apt-get update
${SUDO} apt-get install -y --no-install-recommends gdb-multiarch
Test component
#!/usr/bin/env bash
# Tests that gdb-multiarch is installed and supports ARM.
set -Eeuo pipefail

gdb-multiarch --version | head -n 1
gdb-multiarch -batch -ex "set architecture armv5te"
echo "gdb-multiarch OK"
$(DEVKITPRO)/insight/bin/arm-eabi-insight wrappers
Explanation

Insight is the original debugger (and simulator for FC) in the BMDE debugger. Optionally, to make the installation of Insight compatible with the debug target of Makefiles, we need to make available the files $(DEVKITPRO)/insight/bin/arm-eabi-insight and arm-eabi-insight.exe, because they are explicitly referenced by the Makefiles.

To do so, we can create wrappers that forward the calls into arm-eabi-insight. We could implement this in another way, such a shell alias.

Proposed command
#!/usr/bin/env bash
# Installs the wrappers $DEVKITPRO/insight/bin/arm-eabi-insight(.exe), which the Makefiles of the course use to debug
# (make debug) the projects as in the Windows BMDE, where Insight is installed in devkitPro. Needs Insight
# (install_insight.sh). Standalone: can be run in an Ubuntu / Debian host, and is also used by the BMDE Docker images.
set -Eeuo pipefail

BMDE_ROOT="${BMDE_ROOT:-/URV/bmde}"
SUDO=""; if [ "$(id -u)" -ne 0 ]; then SUDO=sudo; fi
WRAPPER_DIR="${DEVKITPRO:-${BMDE_ROOT}/devkitPro}/insight/bin"

${SUDO} mkdir -p "${WRAPPER_DIR}"
for wrapper in arm-eabi-insight arm-eabi-insight.exe; do
  ${SUDO} tee "${WRAPPER_DIR}/${wrapper}" > /dev/null <<WRAPPER
#!/usr/bin/env sh
exec "${BMDE_ROOT}/insight/bin/arm-eabi-insight" "\$@"
WRAPPER
  ${SUDO} chmod +x "${WRAPPER_DIR}/${wrapper}"
done

echo "Wrappers installed in ${WRAPPER_DIR}"
Test component
#!/usr/bin/env bash
# Tests the wrappers $DEVKITPRO/insight/bin/arm-eabi-insight(.exe) used by the Makefiles of the course.
set -Eeuo pipefail

for wrapper in arm-eabi-insight arm-eabi-insight.exe; do
  "${DEVKITPRO:-/URV/bmde/devkitPro}/insight/bin/${wrapper}" --version | head -n 1
done
echo "Insight wrappers OK"

Test operation

The test builds the hello world project of the course, runs it in DeSmuME with the GDB stub, and debugs it with arm-eabi-gdb and gdb-multiarch: they connect, stop at main and step:

#!/usr/bin/env bash
# Tests the debug operation: builds the hello world project of the course, runs it in DeSmuME with its GDB stub as
# make debug does, and debugs it with arm-eabi-gdb (the GDB of Insight) and gdb-multiarch: connects, stops at main and
# steps. Needs devkitARM, libnds and DeSmuME. Without a display (e.g. in a container) it uses a virtual one.
set -Eeuo pipefail

work_dir="$(mktemp -d)"
# Hello world project of the course, downloaded by scripts/download_assets.sh
mkdir "${work_dir}/hello-world-nds"
tar -xzf "$(cd "$(dirname "${BASH_SOURCE[0]:-$0}")" && pwd)/../assets/hello-world-nds.tar.gz" \
  -C "${work_dir}/hello-world-nds" --strip-components=1
make -C "${work_dir}/hello-world-nds"
display=(); if [ -z "${DISPLAY:-}" ]; then display=(xvfb-run -a); fi

status=0
# Each debugger with its own DeSmuME and port, so the previous one has not to release it first. Ports under 1024 need
# root out of containers
port=1024
for gdb in arm-eabi-gdb gdb-multiarch; do
  port=$((port + 1))
  "${display[@]}" desmume --disable-sound --arm9gdb="${port}" "${work_dir}"/hello-world-nds/*.nds > /dev/null 2>&1 &
  desmume_pid=$!
  sleep 5
  timeout 60 "${gdb}" -batch -ex "target remote localhost:${port}" -ex "break main" -ex "continue" -ex "next" \
    "${work_dir}"/hello-world-nds/*.elf | tee "${work_dir}/gdb.log" || true
  grep -q "Breakpoint 1, main" "${work_dir}/gdb.log" || { echo "${gdb} did not stop at main"; status=1; }
  kill "${desmume_pid}" 2>/dev/null || true
  pkill -x desmume 2>/dev/null || true
  wait "${desmume_pid}" 2>/dev/null || true
done
rm -rf "${work_dir}"
[ "${status}" -eq 0 ]
echo "debug operation OK"

Test operation through make debug

To test the debug operation with Insight, run make debug in a project of the course (e.g. the hello world project). Make sure that the flags -gdwarf-3 and -O0 are in the CFLAGS (arguments passed to gcc) of the Makefile. This applies to all programs that you want to debug.

You should see both the Insight window and the DeSmuME window. To start the debug session: 1. Click the Run button of Insight. The first time, choose the target Remote/TCP, hostname localhost and port 1000 in the dialog. 2. Insight downloads the program into DeSmuME and stops at the first line of main. 3. You can change the source code mode to see different versions of the source code (ARM, mixed...). 4. Step, set breakpoints and continue with the buttons of Insight. While the program runs, DeSmuME takes the input of the buttons of the emulated NDS.

With bmde debug, Insight is already configured to connect to DeSmuME: just click the Run button.


Patch Operation

Components: * dlditool, with the DLDI patch of the course (mpcf.dldi)

Component installation

dlditool
Explanation

dlditool patches NDS ROMs with a DLDI driver, so they can access the filesystem of the CompactFlash card of the flash cartridge used in the course. It is installed into /URV/bmde/dlditool, with the patch mpcf.dldi in its dldi/ folder, where dlditool finds the patches given by name.

Download assets

Download dlditool and the DLDI driver of the course into components/dlditool-docker/assets/. It also builds the ROM used by the test (the libfatdir example of devkitPro, which it downloads) with the devkitARM installed in the build operation.

components/dlditool-docker/scripts/download_assets.sh
Proposed command
#!/usr/bin/env bash
# Installs dlditool and the DLDI patch of the M3 / G6 / SuperCard CompactFlash (mpcf.dldi) into /URV/bmde/dlditool,
# and its environment (PATH) into /etc/profile.d/bmde-dlditool.sh. dlditool finds the patches given by name in its
# dldi/ directory. Can be run in an Ubuntu / Debian host from a checkout of the repository (after
# scripts/download_assets.sh), and is also used by the BMDE Docker images. Set BMDE_ROOT to install into another
# directory.
set -Eeuo pipefail

BMDE_ROOT="${BMDE_ROOT:-/URV/bmde}"
SUDO=""; if [ "$(id -u)" -ne 0 ]; then SUDO=sudo; fi
DLDITOOL_DIR="${BMDE_ROOT}/dlditool"
ASSETS_DIR="$(cd "$(dirname "${BASH_SOURCE[0]:-$0}")" && pwd)/../assets"

if [ ! -x "${DLDITOOL_DIR}/dlditool" ]; then
  for asset in dlditool-linux-x86_64.zip mpcf.dldi; do
    if [ ! -f "${ASSETS_DIR}/${asset}" ]; then
      echo "${ASSETS_DIR}/${asset} not found: download it with scripts/download_assets.sh" >&2
      exit 1
    fi
  done
  ${SUDO} apt-get update
  ${SUDO} apt-get install -y --no-install-recommends unzip
  ${SUDO} mkdir -p "${DLDITOOL_DIR}/dldi"
  ${SUDO} unzip -o "${ASSETS_DIR}/dlditool-linux-x86_64.zip" dlditool -d "${DLDITOOL_DIR}"
  ${SUDO} chmod +x "${DLDITOOL_DIR}/dlditool"
  ${SUDO} install -m 644 "${ASSETS_DIR}/mpcf.dldi" "${DLDITOOL_DIR}/dldi/mpcf.dldi"
fi

${SUDO} tee /etc/profile.d/bmde-dlditool.sh > /dev/null <<PROFILE
export DLDITOOL="${DLDITOOL_DIR}/dlditool"
case ":\${PATH}:" in
  *":${DLDITOOL_DIR}:"*) ;;
  *) export PATH="${DLDITOOL_DIR}:\${PATH}" ;;
esac
PROFILE

echo "dlditool installed in ${DLDITOOL_DIR}. Open a new session or run: . /etc/profile.d/bmde-dlditool.sh"
Test component

The test patches a ROM that uses the filesystem:

#!/usr/bin/env bash
# Tests that dlditool is installed, patching a ROM that uses libfat (the libfatdir example of devkitPro) with the
# patch used by the course (mpcf.dldi).
set -Eeuo pipefail

# The libfatdir example of devkitPro, built by scripts/download_assets.sh
ROM="$(cd "$(dirname "${BASH_SOURCE[0]:-$0}")" && pwd)/../assets/libfatdir.nds"

work_dir="$(mktemp -d)"
cp "${ROM}" "${work_dir}/test.nds"
(cd "${work_dir}" && dlditool mpcf.dldi test.nds) | tee "${work_dir}/dlditool.log"
grep -q "Patched successfully" "${work_dir}/dlditool.log"
rm -rf "${work_dir}"
echo "dlditool OK"


Edit Operation

Components:

  • Visual Studio Code
  • arm-nds-vscode-extension

The goal is to provide an IDE to edit code and have syntax highlight for:

  • ARM v5 assembly (Nintendo DS)
  • C language
Visual Studio Code
Explanation

Visual Studio Code is a modern IDE.

It is installed into /URV/bmde/vscode from the package of its website, which is also used to install its dependencies.

Download assets

Download the latest Visual Studio Code into components/vscode-arm-syntax-docker/assets/.

components/vscode-arm-syntax-docker/scripts/download_assets.sh vscode.deb
Proposed command
#!/usr/bin/env bash
# Installs Visual Studio Code into /URV/bmde/vscode, and its environment (PATH) into /etc/profile.d/bmde-vscode.sh.
# Can be run in an Ubuntu / Debian host from a checkout of the repository (after scripts/download_assets.sh), and is
# also used by the BMDE Docker images. Set BMDE_ROOT to install into another directory.
set -Eeuo pipefail

BMDE_ROOT="${BMDE_ROOT:-/URV/bmde}"
SUDO=""; if [ "$(id -u)" -ne 0 ]; then SUDO=sudo; fi
VSCODE_DIR="${BMDE_ROOT}/vscode"
VSCODE_DEB="$(cd "$(dirname "${BASH_SOURCE[0]:-$0}")" && pwd)/../assets/vscode.deb"

${SUDO} apt-get update

# VS Code is extracted from the package of Microsoft into VSCODE_DIR. The package declares the dependencies of VS Code
# for each distribution, which are saved to install them when VS Code is already there
DEPENDS="${VSCODE_DIR}/bmde-depends"
if [ ! -x "${VSCODE_DIR}/bin/code" ]; then
  if [ ! -f "${VSCODE_DEB}" ]; then
    echo "${VSCODE_DEB} not found: download it with scripts/download_assets.sh" >&2
    exit 1
  fi
  extract_dir="$(mktemp -d)"
  dpkg-deb --extract "${VSCODE_DEB}" "${extract_dir}"
  ${SUDO} rm -rf "${VSCODE_DIR}"
  ${SUDO} mkdir -p "${BMDE_ROOT}"
  ${SUDO} mv "${extract_dir}/usr/share/code" "${VSCODE_DIR}"
  dpkg-deb --field "${VSCODE_DEB}" Depends | ${SUDO} tee "${DEPENDS}" > /dev/null
  rm -rf "${extract_dir}"
fi
${SUDO} apt-get satisfy -y --no-install-recommends "$(cat "${DEPENDS}")"

# The helper of the sandbox of Chromium must be setuid root, which the package of VS Code does on its installation
# (not on extraction), or code aborts. Also when VS Code was already there (e.g. copied from another image)
${SUDO} chown root:root "${VSCODE_DIR}/chrome-sandbox"
${SUDO} chmod 4755 "${VSCODE_DIR}/chrome-sandbox"

${SUDO} tee /etc/profile.d/bmde-vscode.sh > /dev/null <<PROFILE
case ":\${PATH}:" in
  *":${VSCODE_DIR}/bin:"*) ;;
  *) export PATH="${VSCODE_DIR}/bin:\${PATH}" ;;
esac
PROFILE

echo "VS Code installed in ${VSCODE_DIR}. Open a new session or run: . /etc/profile.d/bmde-vscode.sh"
Test component
#!/usr/bin/env bash
# Tests that VS Code is installed.
set -Eeuo pipefail

code --version

# The helper of the sandbox of Chromium must be setuid root, or code aborts ("The SUID sandbox helper binary was found,
# but is not configured correctly")
vscode_dir="$(dirname "$(dirname "$(readlink -f "$(command -v code)")")")"
sandbox_mode="$(stat -c '%u %a' "${vscode_dir}/chrome-sandbox")"
if [ "${sandbox_mode}" != "0 4755" ]; then
  echo "${vscode_dir}/chrome-sandbox has owner and mode '${sandbox_mode}' instead of '0 4755'" >&2
  exit 1
fi
echo "VS Code OK"
NDS ARM Assembly Syntax Highlight extension
Explanation

Extension for VSCode to enable syntax highlight for ARM v5. Available in the marketplace. The script installs the latest release of its repository for the current user, so run it without sudo.

Download assets

Download the latest release of the extension into components/arm-syntax-vscode-extension/assets/.

components/arm-syntax-vscode-extension/scripts/download_assets.sh
Proposed command

Once we have the command code available we can do the following to install the extension:

#!/usr/bin/env bash
# Installs the extension with the syntax highlighting of the ARM assembly of the course in VS Code, for the current
# user. Needs VS Code (install_vscode.sh of vscode-arm-syntax-docker). Installs the latest release, downloaded into
# assets/ by scripts/download_assets.sh, or the .vsix file given in VSIX. Can be run in an Ubuntu / Debian host from a
# checkout of the repository, and is also used by the BMDE Docker images.
set -Eeuo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]:-$0}")" && pwd)"
vsix="${VSIX:-${SCRIPT_DIR}/../assets/arm-syntax-vscode-extension.vsix}"
if [ ! -f "${vsix}" ]; then
  echo "${vsix} not found: download it with scripts/download_assets.sh" >&2
  exit 1
fi

code --install-extension "${vsix}" --force

Test operation

#!/usr/bin/env bash
# Tests that the extension with the syntax highlighting of the ARM assembly of the course is installed in VS Code.
set -Eeuo pipefail

code --list-extensions | tee /dev/stderr | grep -qi "aleixmt.nds-arm-assembly-syntax-highlight"
echo "ARM syntax extension OK"

Then, open a .s file of a project of the course in the IDE. You should see the syntax highlight for ARM.


VCS Operation

Components:

  • git
  • ssh-client
  • forticlient or openfortivpn

Component installation

git
Explanation

Install git and gitk using your package manager. Version control for course software projects. It also installs the helpers of the BMDE to run git commands through SSH with password (git.exp) into /URV/bmde/git-urv-vpn/bin.

Proposed command
#!/usr/bin/env bash
# Installs git (and gitk, when there is one for that git), and the helpers of the BMDE to run git commands (git.exp,
# git_clone.exp, clone_repos.sh) into /URV/bmde/git-urv-vpn/bin, and its environment (PATH) into
# /etc/profile.d/bmde-git-urv-vpn.sh. Can be run in an Ubuntu / Debian host from a checkout of the repository, and is
# also used by the BMDE Docker images. Set BMDE_ROOT to install into another directory.
set -Eeuo pipefail

BMDE_ROOT="${BMDE_ROOT:-/URV/bmde}"
SUDO=""; if [ "$(id -u)" -ne 0 ]; then SUDO=sudo; fi
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]:-$0}")" && pwd)"
BIN_DIR="${BMDE_ROOT}/git-urv-vpn/bin"

${SUDO} apt-get update
packages=(ca-certificates wget git expect jq)
# gitk is only a history viewer, so it is skipped when the distribution has no gitk for the git installed: e.g. a git
# from the git-core PPA (like in the GitHub runners), newer than the one the gitk of Ubuntu depends on
if apt-get install -s -y --no-install-recommends git gitk > /dev/null 2>&1; then
  packages+=(gitk)
else
  echo "WARNING: gitk is not installed, because there is no version of it for the installed git" >&2
fi
${SUDO} apt-get install -y --no-install-recommends "${packages[@]}"

${SUDO} mkdir -p "${BIN_DIR}"
for helper in git.exp git_clone.exp clone_repos.sh; do
  ${SUDO} install -m 755 "${SCRIPT_DIR}/${helper}" "${BIN_DIR}/${helper}"
done

${SUDO} tee /etc/profile.d/bmde-git-urv-vpn.sh > /dev/null <<PROFILE
case ":\${PATH}:" in
  *":${BIN_DIR}:"*) ;;
  *) export PATH="${BIN_DIR}:\${PATH}" ;;
esac
PROFILE

echo "git installed, with the helpers of the BMDE in ${BIN_DIR}"
Test component
#!/usr/bin/env bash
# Tests that git is installed, with the helpers of the BMDE.
set -Eeuo pipefail

git --version
command -v git.exp git_clone.exp clone_repos.sh expect jq
echo "git OK"
ssh-client
Explanation

SSH authentication is required for accessing the repositories.

Proposed command
#!/usr/bin/env bash
# Installs the SSH client, used by git to access the repositories of the course. Standalone: can be run in an Ubuntu /
# Debian host, and is also used by the BMDE Docker images.
set -Eeuo pipefail

SUDO=""; if [ "$(id -u)" -ne 0 ]; then SUDO=sudo; fi

${SUDO} apt-get update
${SUDO} apt-get install -y --no-install-recommends openssh-client
Test component
#!/usr/bin/env bash
# Tests that the SSH client is installed.
set -Eeuo pipefail

ssh -V
command -v ssh-keyscan
echo "SSH OK"
forticlient or openfortivpn
Explanation

VPN access to the university network. Course Git repositories are only accessible from within the university network. VPN access is therefore required for off-campus work. It also installs the helpers of the BMDE to connect to the VPN (connect_vpn.sh) and to check that it works (vpn_healthcheck.sh) into /URV/bmde/git-urv-vpn/bin. Run connect_vpn.sh as your user: it uses sudo only for openfortivpn, and takes the credentials from the variables VPN_USERNAME, VPN_PASSWORD, VPN_HOST, VPN_PORT, VPN_REALM and VPN_CERT (see the README of git-urv-vpn-docker).

Proposed command

For openfortivpn:

#!/usr/bin/env bash
# Installs openfortivpn, the client of the VPN of the URV, and the helpers of the BMDE to connect to it (connect_vpn.sh)
# and to check that it works (vpn_healthcheck.sh) into /URV/bmde/git-urv-vpn/bin, and its environment (PATH) into
# /etc/profile.d/bmde-git-urv-vpn.sh. Can be run in an Ubuntu / Debian host from a checkout of the repository, and is
# also used by the BMDE Docker images. Set BMDE_ROOT to install into another directory.
set -Eeuo pipefail

BMDE_ROOT="${BMDE_ROOT:-/URV/bmde}"
SUDO=""; if [ "$(id -u)" -ne 0 ]; then SUDO=sudo; fi
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]:-$0}")" && pwd)"
BIN_DIR="${BMDE_ROOT}/git-urv-vpn/bin"

${SUDO} apt-get update
${SUDO} apt-get install -y --no-install-recommends ca-certificates wget openfortivpn

${SUDO} mkdir -p "${BIN_DIR}"
${SUDO} install -m 755 "${SCRIPT_DIR}/connect_vpn.sh" "${BIN_DIR}/connect_vpn.sh"
${SUDO} install -m 755 "${SCRIPT_DIR}/vpn_healthcheck.sh" "${BIN_DIR}/vpn_healthcheck.sh"
${SUDO} install -m 644 "${SCRIPT_DIR}/read_secret.sh" "${BIN_DIR}/read_secret.sh"
# Also in /usr/local/bin, for the shells that do not load /etc/profile.d: the HEALTHCHECK of Docker, and sudo, whose
# secure_path replaces the PATH. The helpers find the files next to them through the links
for helper in connect_vpn.sh vpn_healthcheck.sh; do
  ${SUDO} ln -sf "${BIN_DIR}/${helper}" "/usr/local/bin/${helper}"
done

${SUDO} tee /etc/profile.d/bmde-git-urv-vpn.sh > /dev/null <<PROFILE
case ":\${PATH}:" in
  *":${BIN_DIR}:"*) ;;
  *) export PATH="${BIN_DIR}:\${PATH}" ;;
esac
PROFILE

echo "openfortivpn installed, with the helper of the BMDE in ${BIN_DIR}"

Test component
#!/usr/bin/env bash
# Tests that openfortivpn is installed, with the helpers of the BMDE, and that the health check tells that the VPN does
# not work when it is not connected, as any user (it needs no root).
set -Eeuo pipefail

openfortivpn --version
command -v connect_vpn.sh vpn_healthcheck.sh
# Also for sudo and the HEALTHCHECK of Docker, which do not load the PATH of /etc/profile.d
test -x /usr/local/bin/connect_vpn.sh
test -x /usr/local/bin/vpn_healthcheck.sh

if vpn_healthcheck.sh -v; then
  echo "vpn_healthcheck.sh says that the VPN works, but it is not connected" >&2
  exit 1
fi
echo "openfortivpn OK"

Test operation

When connected to the VPN (or on campus), the following command should work:

#!/usr/bin/env bash
# Tests the git operation: clones a repository of the course through SSH. Manual test: replace USER_ID with your user
# (it asks for your password), and connect to the VPN of the URV first if you are outside its network.
set -Eeuo pipefail

git clone USER_ID@git.deim.urv.cat:comp_25 .
echo "git operation OK"

This requires:

  • The Git server to be active
  • The repository comp_25 to exist
  • Proper access permissions

The Git server is maintained by the course supervisor: Santiago RomanĂ­ (santiago.romani@urv.cat)


Optional Operations

Components: * grit

Component installation

grit
Explanation

Grit is a tool for converting images into GBA/NDS compatible formats. It is useful for graphics development on the NDS. It is installed into /URV/bmde/grit from the devkitARM r46 release (the same binary of the devkitARM of the build operation).

Download assets

Download devkitARM r46, from which grit is taken, into components/grit-docker/assets/. It is not needed if devkitARM is already installed: grit is then copied from it.

components/grit-docker/scripts/download_assets.sh
Proposed command
#!/usr/bin/env bash
# Installs grit (from the devkitARM r46 release) into /URV/bmde/grit, and its environment (PATH) into
# /etc/profile.d/bmde-grit.sh. Can be run in an Ubuntu / Debian host from a checkout of the repository (after
# scripts/download_assets.sh), and is also used by the BMDE Docker images. Set BMDE_ROOT to install into another
# directory.
set -Eeuo pipefail

BMDE_ROOT="${BMDE_ROOT:-/URV/bmde}"
SUDO=""; if [ "$(id -u)" -ne 0 ]; then SUDO=sudo; fi
GRIT_DIR="${BMDE_ROOT}/grit"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]:-$0}")" && pwd)"
DEVKITARM_TARBALL="${SCRIPT_DIR}/../assets/devkitARM_r46-x86_64-linux.tar.bz2"

if [ ! -x "${GRIT_DIR}/grit" ]; then
  ${SUDO} mkdir -p "${GRIT_DIR}"
  if [ -x "${BMDE_ROOT}/devkitPro/devkitARM/bin/grit" ]; then
    # Same binary as the one of the devkitARM already installed
    ${SUDO} cp "${BMDE_ROOT}/devkitPro/devkitARM/bin/grit" "${GRIT_DIR}/grit"
  else
    if [ ! -f "${DEVKITARM_TARBALL}" ]; then
      echo "${DEVKITARM_TARBALL} not found: download it with scripts/download_assets.sh" >&2
      exit 1
    fi
    ${SUDO} apt-get update
    ${SUDO} apt-get install -y --no-install-recommends bzip2
    ${SUDO} tar -xjf "${DEVKITARM_TARBALL}" -C "${GRIT_DIR}" --strip-components=2 devkitARM/bin/grit
  fi
fi

${SUDO} tee /etc/profile.d/bmde-grit.sh > /dev/null <<PROFILE
case ":\${PATH}:" in
  *":${GRIT_DIR}:"*) ;;
  *) export PATH="${GRIT_DIR}:\${PATH}" ;;
esac
PROFILE

echo "grit installed in ${GRIT_DIR}. Open a new session or run: . /etc/profile.d/bmde-grit.sh"
Test component

The test converts an image into GAS assembly, as in the course:

#!/usr/bin/env bash
# Tests that grit is installed, converting an image into GAS assembly (8 bpp tiles, as in the course).
set -Eeuo pipefail

# CRC-32 of the bytes of a hex string, as used by the PNG chunks
crc32() {
  local hex="$1" crc=0xFFFFFFFF i k
  for ((i = 0; i < ${#hex}; i += 2)); do
    crc=$((crc ^ 16#${hex:i:2}))
    for ((k = 0; k < 8; k++)); do
      if ((crc & 1)); then crc=$(((crc >> 1) ^ 0xEDB88320)); else crc=$((crc >> 1)); fi
    done
  done
  printf '%08x' $((crc ^ 0xFFFFFFFF))
}

# Adler-32 of the bytes of a hex string, as used by the zlib stream
adler32() {
  local hex="$1" a=1 b=0 i
  for ((i = 0; i < ${#hex}; i += 2)); do
    a=$(((a + 16#${hex:i:2}) % 65521))
    b=$(((b + a) % 65521))
  done
  printf '%04x%04x' "${b}" "${a}"
}

# PNG chunk of type $1 (hex) with the data $2 (hex)
png_chunk() {
  printf '%08x%s%s%s' $((${#2} / 2)) "$1" "$2" "$(crc32 "$1$2")"
}

# Writes to $1 a 16x16 PNG with a palette of 4 colors (magenta, the transparent color of the course, black, white and
# red) drawn in a checkered pattern. Its pixels are stored without compression, so no tool is needed to build it.
write_test_png() {
  local size=16 x y pixels="" ihdr plte idat
  for ((y = 0; y < size; y++)); do
    pixels+="00"  # Filter type of the row: none
    for ((x = 0; x < size; x++)); do
      pixels+="$(printf '%02x' $(((x / 4 + y / 4) % 4)))"
    done
  done
  ihdr="$(printf '%08x%08x' "${size}" "${size}")0803000000"  # 8 bits per pixel, indexed color
  plte="ff00ff000000ffffffff0000"
  local length=$((${#pixels} / 2))
  # zlib stream with a single stored (uncompressed) deflate block: LEN and NLEN are little-endian
  idat="7801""01$(printf '%02x%02x' $((length & 0xFF)) $((length >> 8)))"
  idat+="$(printf '%02x%02x' $((~length & 0xFF)) $(((~length >> 8) & 0xFF)))${pixels}$(adler32 "${pixels}")"
  printf '%s' "89504e470d0a1a0a$(png_chunk 49484452 "${ihdr}")$(png_chunk 504c5445 "${plte}")" \
    "$(png_chunk 49444154 "${idat}")$(png_chunk 49454e44 "")" | sed 's/../\\x&/g' | xargs -0 printf > "$1"
}

work_dir="$(mktemp -d)"
write_test_png "${work_dir}/test.png"
(cd "${work_dir}" && grit test.png -gB8 -gTFF00FF -o test)
grep -q "testTiles" "${work_dir}/test.s"
rm -rf "${work_dir}"
echo "grit OK"