D on Dreamcast: Difference between revisions

From dreamcast.wiki
Jump to navigation Jump to search
(Add TOC)
No edit summary
 
(3 intermediate revisions by 2 users not shown)
Line 13: Line 13:
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.
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. [[Getting Started with Dreamcast development#Setting up and compiling the toolchain with the dc-chain script|See (the C/C++) getting started guide]] and follow it till the Configuring the dc-chain script section.
You will additionally need to get the dc-chain script installed, together with a copy of git. [[Getting Started with Dreamcast development#Setting up and compiling the toolchain with the dc-chain script|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 =
= Building the D cross compiler =
Once you have the D utilities, dc-chain and git installed and you have the dc-chain config available, you will need to modify the build script.
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


Put the following text in to config.mk:
Once this is done you can continue following the instructions in the '''Getting Started''' guide.
# Sega Dreamcast Toolchains Maker (dc-chain)
 
# This file is part of KallistiOS.
== Patching libphobos sources ==
  #
A [https://github.com/KallistiOS/KallistiOS/pull/954 pull-request] has been submitted to dc-chain to automate this step.
  # Created by Jim Ursetto (2004)
 
  # Initially adapted from Stalin's build script version 0.3.
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;
   
   
  # Toolchain versions for SH
  // Map from thread model to thread interface.
  sh_binutils_ver=2.41
  enum ThreadModel {
sh_gcc_ver=13.2.0
    Single,
newlib_ver=4.3.0.20230120
    Posix,
  gdb_ver=13.2
    Win32,
  }
   
   
  # Tarball extensions to download for SH
  enum GNU_ARM_EABI_Unwinder = false;
sh_binutils_download_type=xz
  enum ThreadModel GNU_Thread_Model = ThreadModel.Posix;
sh_gcc_download_type=xz
  enum OS_Have_Dlpi_Tls_Modid = false;
newlib_download_type=gz
  enum GNU_Have_Atomics = true;
  gdb_download_type=xz
  enum GNU_Have_64Bit_Atomics = false;
  enum GNU_Have_LibAtomic = false;
# Toolchain for ARM
  enum Have_Qsort_R = false;
# The ARM version of gcc/binutils is separated as support for the ARM7DI core
  enum GNU_Enable_CET = false;
# used in the Dreamcast's AICA is not available in versions of GCC beyond 8.5.0.
 
  arm_binutils_ver=2.41
You may now copy the contents of the libdruntime directory to
  arm_gcc_ver=8.5.0
  /opt/toolchains/dc/sh-elf/lib/gcc/sh-elf/<gcc version>/include/d/
 
# Tarball extensions to download for ARM
After this, you should be set to compile D code with dkos; without betterC.
  arm_binutils_download_type=xz
 
  arm_gcc_download_type=xz
== Post Setup ==
 
# GCC custom dependencies
You can use the [https://github.com/KallistiOS/dkos 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:
# Specify here if you want to use custom GMP, MPFR and MPC libraries when
  dependency "dkos" path="../dkos"
# building GCC. It is recommended that you leave this variable commented, in
 
# which case these dependencies will be automatically downloaded by using the
Or create a version pin by running
# '/contrib/download_prerequisites' shell script provided within the GCC packages.
  dub add-local <dkos directory> "<version>"
# The ISL dependency isn't mandatory; if desired, you may comment the version
# numbers (i.e. 'sh_isl_ver' and 'arm_isl_ver') to disable the ISL library.
  #use_custom_dependencies=1
  # GCC dependencies for SH
sh_gmp_ver=6.2.1
sh_mpfr_ver=4.1.0
sh_mpc_ver=1.2.1
sh_isl_ver=0.24
# Tarball extensions to download for GCC dependencies for SH
sh_gmp_download_type=bz2
sh_mpfr_download_type=bz2
sh_mpc_download_type=gz
sh_isl_download_type=bz2
# GCC dependencies for ARM
arm_gmp_ver=6.1.0
arm_mpfr_ver=3.1.4
arm_mpc_ver=1.0.3
arm_isl_ver=0.18
# Tarball extensions to download for GCC dependencies for ARM
arm_gmp_download_type=bz2
arm_mpfr_download_type=bz2
arm_mpc_download_type=gz
arm_isl_download_type=bz2
# Download protocol (http|https|ftp)
# Specify here the protocol you want to use for downloading the packages.
download_protocol=https
# Force downloader (curl|wget)
# You may specify here 'wget' or 'curl'. If this variable is empty or commented,
# web downloader tool will be auto-detected in the following order: cURL, Wget.
# You must have either Wget or cURL installed to use dc-chain.
#force_downloader=wget
# Specify GNU Mirror override
# The default mirror for GNU sources is `ftpmirror.gnu.org`
# Setting this will allow overriding the default mirror which
# may be desirable if you have a prefered mirror.
#gnu_mirror=mirrors.kernel.org
# Toolchains base
# Indicate the root directory where toolchains will be installed
# This should match your 'environ.sh' configuration
  toolchains_base=/opt/toolchains/dc
# Verbose (1|0)
# Display output to screen as well as log files
verbose=1
# Erase (1|0)
# Erase build directories on the fly to save space
erase=1
# Install mode (install-strip|install)
# Use 'install-strip' mode for removing debugging symbols of the toolchains.
# Use 'install' only if you want to enable debug symbols for the toolchains.
# This may be useful only if you plan to debug the toolchain itself.
install_mode=install-strip
# Make jobs (-jn|<empty>)
# Set this value to -jn where n is the number of jobs you want to run with make.
# If you only want one job, just set this to nothing (i.e, "makejobs=").
# Tracking down problems with multiple make jobs is much more difficult than
# with just one running at a time. So, if you run into trouble, then you should
# clear this variable and try again with just one job running.
# Please note, this value may be overriden in some cases; as issues were
# detected on some OS.
makejobs=-j16
# Languages (c|c++|objc|obj-c++)
# Set the languages to build for pass 2 of building gcc for sh-elf. The default
# here is to build C, C++, Objective C, and Objective C++. You may want to take
# out the latter two if you're not worried about them and/or you're short on
# hard drive space.
pass2_languages=c,c++,objc,obj-c++,d
# GCC threading model (single|kos|posix*)
# With GCC 4.x versions and up, the patches provide a 'kos' thread model, so you
# should use it. If you really don't want threading support for C++ (or
# Objective C/Objective C++), you can set this to 'single'. With GCC 3.x, you
# probably want 'posix' here; but this mode is deprecated as the GCC 3.x branch
# is not anymore supported.
thread_model=kos
# Automatic patching for KOS (1|0)
# Uncomment this if you want to disable applying KOS patches to the toolchain
# source files before building. This will disable usage of the 'kos' thread model.
#use_kos_patches=0
# Automatic fixup SH-4 Newlib (1|0)
# Uncomment this if you want to disable the automatic fixup sh4 newlib needed by
# KallistiOS. This will keep the generated toolchain completely raw. This will
# also disable the 'kos' thread model. Don't mess with that flag unless you know
# exactly what you are doing.
#auto_fixup_sh4_newlib=0
# C99 Format Specifier Support (1|0)
# Define this to build SH4 Newlib with additional support for the C99 format
# specifiers, used by printf and friends. These include support for size_t,
# ptrdiff_t, intmax_t, and sized integral types.
#newlib_c99_formats=1
# Optimize Newlib for Space (1|0)
# Define this to enable optimizing for space when building Newlib. This will
# build Newlib with compiler flags which favor smaller code sizes over faster
# performance.
#newlib_opt_space=1
# MinGW/MSYS
# Standalone binaries (1|0)
# Define this if you want a standalone, no dependency binaries (i.e. static)
# When the binaries are standalone, it can be run outside MinGW/MSYS
# environment. This is NOT recommended. Use it if you know what you are doing.
#standalone_binary=1
# Force installation of BFD for SH (1|0)
# Uncomment this if you want to force the installation of 'libbfd' for the SH
# toolchain. This is required for MinGW/MSYS and can't be disabled in this
  # scenario. This option is here mainly if you want to force the installation of
# 'libbfd' under other environments; but this won't be necessary in most cases,
# as 'libelf' is used almost everywhere. Please note, 'libbfd' couldn't be
# portable if you built it on another environment. Don't mess with that flag
# unless you know exactly what you are doing.
  #sh_force_libbfd_installation=1


Additionally, add <code>--disable-libphobos \</code> to the configure step in <code>scripts/gcc-pass2.mk</code>
Then using the pinned version as your dependency.
Once this is done you can continue building and installing the compiler to <code>/opt/toolchains/dc</code>!


With this done you should have a functional D toolchain for the SEGA Dreamcast installed.
== 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:


== Post Build ==
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.
}


You can use the [https://github.com/LunaTheFoxgirl/dkos dkos] and [https://code.dlang.org/packages/numem numem] packages 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 in a multi-target codebase to use the provided version identifiers to handle the differences between the platforms.


DKOS depends on a custom runtime called tinyd-rt, which automatically should get fetched on build.
'''Note''': The Dreamcast/Naomi/Katana-specific API will not be compiled in with the "VMU" configuration; the vmu subdirectory contains VMU specific APIs.

Latest revision as of 02:46, 19 March 2025

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.