D on Dreamcast

From dreamcast.wiki
Revision as of 02:46, 19 March 2025 by LunaTheFoxgirl (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Introduction

D is a flexible systems programming language, which allows writing fast software, fast. Getting DLang on the Dreamcast is in of itself simple, using the GCC D compiler (GDC), but as of current the D runtime and standard library are unavailable. As such alternatives need to be used, without the garbage collector you'd find in stock DLang.

Currently, building the compiler on Windows is very difficult and it's recommended to build it on macOS or Linux, or via WSL.

Prerequisites

A few utilities for D are needed to get started, namely dub, the D package manager. You can get this by going to https://dlang.org and downloading the DMD or LDC2 distributions of D. You will not need the compilers or standard libraries that come with either package for dreamcast development, but they may be useful for developing tooling. Dub comes bundled with both DMD and LDC2, there's no precompiled GDC package and it is considerably more difficult to get a full D environment set up the manual route, so it is not recommended.

You will additionally need to get the dc-chain script installed, together with a copy of git. See (the C/C++) getting started guide and follow it until you reach the Downloading and compiling the toolchain section.

Building the D cross compiler

Once you are ready to build the SH toolchain, you can begin the compilation process with D support enabled by running

make enable_d=1

Once this is done you can continue following the instructions in the Getting Started guide.

Patching libphobos sources

A pull-request has been submitted to dc-chain to automate this step.

If you wish to use DLang with classes you will need to prepare the libphobos sources shipping with GDC.

In the dc-chain directory in which you ran the make script, you should have folders called gcc-<version number>, within the gcc folder with the highest version number, find the libphobos folder; within it find the libdruntime folder; and within in a folder called gcc.

Create file called config.d and insert the following as its contents.

// THIS IS AN UGLY HACK TO ACCOUNT FOR THE LACK OF BEING
// ABLE TO COMPILE PHOBOS FOR THIS PLATFORM.
module gcc.config;

// Map from thread model to thread interface.
enum ThreadModel {
    Single,
    Posix,
    Win32,
}

enum GNU_ARM_EABI_Unwinder = false;
enum ThreadModel GNU_Thread_Model = ThreadModel.Posix;
enum OS_Have_Dlpi_Tls_Modid = false;
enum GNU_Have_Atomics = true;
enum GNU_Have_64Bit_Atomics = false;
enum GNU_Have_LibAtomic = false;
enum Have_Qsort_R = false;
enum GNU_Enable_CET = false;

You may now copy the contents of the libdruntime directory to

/opt/toolchains/dc/sh-elf/lib/gcc/sh-elf/<gcc version>/include/d/

After this, you should be set to compile D code with dkos; without betterC.

Post Setup

You can use the dkos package to develop software for the Dreamcast, do note that DKOS is still in development and some features are still missing, contributions are welcome! It is recommended that you clone the dkos repository and either use a path dependency in your dub.sdl like so:

dependency "dkos" path="../dkos"

Or create a version pin by running

dub add-local <dkos directory> "<version>"

Then using the pinned version as your dependency.

Configuring your Project

DKOS has various configuration options which apply D version tags to your final executable, by default the `dreamcast` configuration is used, which targets the dreamcast hardware exclusively. If you wish to target the naomi or VMU as a configuration you can set up subconfigurations in your dub file; for example:

configuration "dreamcast" {
    subConfiguration "dkos" "dreamcast"
    // The version identifier "Dreamcast" will be present in the compilation unit.
}

configuration "naomi" {
    subConfiguration "dkos" "naomi"
    // The version identifier "Naomi" will be present in the compilation unit.
}

// Requires LDC2 to be installed!
configuration "vmu" {
    subConfiguration "dkos" "vmu"
    // The version identifier "VMU" will be present in the compilation unit,
    // though the predefined "ARM" version identifier can also be used.
}

It is recommended that in a multi-target codebase to use the provided version identifiers to handle the differences between the platforms.

Note: The Dreamcast/Naomi/Katana-specific API will not be compiled in with the "VMU" configuration; the vmu subdirectory contains VMU specific APIs.