<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://dreamcast.wiki/wiki/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Darc</id>
	<title>dreamcast.wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://dreamcast.wiki/wiki/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Darc"/>
	<link rel="alternate" type="text/html" href="https://dreamcast.wiki/Special:Contributions/Darc"/>
	<updated>2026-04-29T11:51:39Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.39.3</generator>
	<entry>
		<id>https://dreamcast.wiki/wiki/index.php?title=Programming_language_support&amp;diff=3849</id>
		<title>Programming language support</title>
		<link rel="alternate" type="text/html" href="https://dreamcast.wiki/wiki/index.php?title=Programming_language_support&amp;diff=3849"/>
		<updated>2026-01-10T21:50:25Z</updated>

		<summary type="html">&lt;p&gt;Darc: /* Compiled languages */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The following languages can be used in Sega Dreamcast programming:&lt;br /&gt;
&lt;br /&gt;
=Assembly languages=&lt;br /&gt;
* &#039;&#039;&#039;SH4 assembly&#039;&#039;&#039;, as the native machine code for the Dreamcast&#039;s SuperH processor&lt;br /&gt;
* &#039;&#039;&#039;ARM assembly&#039;&#039;&#039;, as the native machine code for the Dreamcast&#039;s AICA ARM7DI sound processor&lt;br /&gt;
&lt;br /&gt;
=Compiled languages=&lt;br /&gt;
* &#039;&#039;&#039;C&#039;&#039;&#039;, the predominant language for Sega Dreamcast development, via the GCC C compiler.&lt;br /&gt;
* &#039;&#039;&#039;C++&#039;&#039;&#039;, commonly used and very well-supported, via the GCC C++ compiler.&lt;br /&gt;
* &#039;&#039;&#039;Objective-C&#039;&#039;&#039;, via the GCC Objective-C compiler. Preliminary support for Foundation framework via [https://gnustep.github.io/ GNUstep] is being developed. See [https://twitter.com/dinobj_c/status/1732399498977276154 Andrew Apperley&#039;s X account]. See [[Objective-C on Dreamcast]] for more information.&lt;br /&gt;
** The GNU &#039;&#039;&#039;Objective-C++&#039;&#039;&#039; extension is available as well.&lt;br /&gt;
* &#039;&#039;&#039;D&#039;&#039;&#039;, via the GCC D compiler. Early support, see [https://dreamcast.wiki/D_on_Dreamcast D on Dreamcast] for more information.&lt;br /&gt;
* &#039;&#039;&#039;Go&#039;&#039;&#039;, via gccgo with libgodc runtime. Full runtime support including garbage collection, goroutines, and channels. See [https://github.com/drpaneas/libgodc libgodc on GitHub] for more information.&lt;br /&gt;
* &#039;&#039;&#039;Rust&#039;&#039;&#039;, via rustc_codegen_gcc and libgccjit, or via gccrs. When using rustc_codegen_gcc, the standard library is available, with bindings to KallistiOS being developed. See [[Rust on Dreamcast]] for more information.&lt;br /&gt;
* &#039;&#039;&#039;Ada&#039;&#039;&#039;, via GNAT. [https://github.com/dkm/ada-dreamcast-helloworld Ada 3D cube demo] by [https://poulhies.fr/ Marc Poulhiès].&lt;br /&gt;
* &#039;&#039;&#039;Fortran&#039;&#039;&#039;, via GFortran. Requires some work to create a cross-compiler, but does work.&lt;br /&gt;
&lt;br /&gt;
=Scripting/Interpreted languages=&lt;br /&gt;
* Lua (available in [[kos-ports]])&lt;br /&gt;
* MicroPython (available in [[kos-ports]])&lt;br /&gt;
* Tcl (available in [[kos-ports]])&lt;br /&gt;
* mRuby (available in [[kos-ports]])&lt;br /&gt;
* AngelScript&lt;br /&gt;
* QuickJS&lt;/div&gt;</summary>
		<author><name>Darc</name></author>
	</entry>
	<entry>
		<id>https://dreamcast.wiki/wiki/index.php?title=GLdc&amp;diff=3845</id>
		<title>GLdc</title>
		<link rel="alternate" type="text/html" href="https://dreamcast.wiki/wiki/index.php?title=GLdc&amp;diff=3845"/>
		<updated>2025-12-26T14:37:20Z</updated>

		<summary type="html">&lt;p&gt;Darc: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[https://github.com/Kazade/GLdc GLdc] is an OpenGL implementation for the Dreamcast that is actively developed by Kazade. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Contributors==&lt;br /&gt;
People who also contributed to the testing and optimisation of GLdc (in no particular order): &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Hayden Kowalchuk (MrNeo) &amp;lt;br&amp;gt;&lt;br /&gt;
Unknown Shadow &amp;lt;br&amp;gt;&lt;br /&gt;
T_Chan &amp;lt;br&amp;gt;&lt;br /&gt;
Troy Davis &amp;lt;br&amp;gt;&lt;br /&gt;
Falco Girgis &amp;lt;br&amp;gt;&lt;br /&gt;
darcagn &amp;lt;br&amp;gt;&lt;br /&gt;
Bkacjios &amp;lt;br&amp;gt;&lt;br /&gt;
David Reichelt &amp;lt;br&amp;gt;&lt;br /&gt;
Spencer Elliott &amp;lt;br&amp;gt;&lt;br /&gt;
Andress Antonio Barajas &amp;lt;br&amp;gt;&lt;br /&gt;
Ben Baron &amp;lt;br&amp;gt;&lt;br /&gt;
cepawiel &amp;lt;br&amp;gt;&lt;br /&gt;
lerabot &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Features==&lt;br /&gt;
&lt;br /&gt;
Supports most of the OpenGL 1.2 implementation.&lt;br /&gt;
&lt;br /&gt;
==Projects built with GLdc==&lt;br /&gt;
*[[Simulant|Simulant engine]]&lt;br /&gt;
*[[Summoning Signals]]&lt;br /&gt;
*[[nuQuake]]&lt;br /&gt;
&lt;br /&gt;
==Building GLdc==&lt;br /&gt;
&lt;br /&gt;
GLdc can be installed through the KallistiOS [https://github.com/KallistiOS/kos-ports kos-ports] system. It is provided as the &amp;lt;code&amp;gt;libGL&amp;lt;/code&amp;gt; package.&lt;/div&gt;</summary>
		<author><name>Darc</name></author>
	</entry>
	<entry>
		<id>https://dreamcast.wiki/wiki/index.php?title=Getting_Started_with_Dreamcast_development&amp;diff=3843</id>
		<title>Getting Started with Dreamcast development</title>
		<link rel="alternate" type="text/html" href="https://dreamcast.wiki/wiki/index.php?title=Getting_Started_with_Dreamcast_development&amp;diff=3843"/>
		<updated>2025-12-10T09:04:52Z</updated>

		<summary type="html">&lt;p&gt;Darc: /* Setting up and compiling the toolchain with the dc-chain script */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;div style=&amp;quot;float:right;&amp;quot;&amp;gt;__TOC__&amp;lt;/div&amp;gt;&lt;br /&gt;
=Introduction=&lt;br /&gt;
This article will cover the entire beginning process: starting from zero to having a working dev environment with debug link (serial or IP) and self-booting CD-R. This guide will cover the process for the following platforms:&lt;br /&gt;
* &#039;&#039;&#039;Microsoft Windows 10&#039;&#039;&#039; via [https://learn.microsoft.com/en-us/windows/wsl/about Windows Subsystem for Linux]&lt;br /&gt;
** Users desiring a native Windows approach, see [[DreamSDK]] instead&lt;br /&gt;
* &#039;&#039;&#039;macOS&#039;&#039;&#039; on Intel or Apple Silicon systems with the [https://brew.sh/ Homebrew] package manager installed&lt;br /&gt;
* &#039;&#039;&#039;Linux&#039;&#039;&#039;-based systems&lt;br /&gt;
** &#039;&#039;&#039;Debian&#039;&#039;&#039;- and &#039;&#039;&#039;Ubuntu&#039;&#039;&#039;-based distributions using the default &#039;&#039;&#039;apt&#039;&#039;&#039; package manger&lt;br /&gt;
** &#039;&#039;&#039;Fedora&#039;&#039;&#039;-based distributions using the default &#039;&#039;&#039;dnf&#039;&#039;&#039; package manager&lt;br /&gt;
** &#039;&#039;&#039;Arch&#039;&#039;&#039;-based distributions using the default &#039;&#039;&#039;pacman&#039;&#039;&#039; package manager&lt;br /&gt;
** &#039;&#039;&#039;Alpine&#039;&#039;&#039;-based distributions using the default &#039;&#039;&#039;apk&#039;&#039;&#039; package manager&lt;br /&gt;
&lt;br /&gt;
===Need help?===&lt;br /&gt;
Important note: &#039;&#039;This guide aims to remain up to date and work on all of the above platforms, but keeping instructions for such a variety of platforms up-to-date can be difficult. If you run into any errors or other challenges while following this tutorial, or simply need clarification on any of the steps, feel free to ask for assistance on the [https://dcemulation.org/phpBB/viewforum.php?f=29 message board] or [https://discord.gg/wDEfkUh63h Discord chat] and we would be happy to aid you and update the guide for the benefit of future readers and others in the community.&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
===Terms===&lt;br /&gt;
Before we get started, let&#039;s define several terms:&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;&#039;toolchain&#039;&#039;&#039; is a set of programs which turns your code into an executable file for your Dreamcast console. The toolchain includes:&lt;br /&gt;
* &#039;&#039;&#039;GCC&#039;&#039;&#039;, a compiler which supports C, C++, Objective-C, and more (see also [[Rust on Dreamcast]] and [[Programming language support]])&lt;br /&gt;
* &#039;&#039;&#039;binutils&#039;&#039;&#039;, an assembler and linker&lt;br /&gt;
* &#039;&#039;&#039;newlib&#039;&#039;&#039;, a C library&lt;br /&gt;
* &#039;&#039;&#039;gdb&#039;&#039;&#039;, a debugger&lt;br /&gt;
The toolchain includes a compiler for the Dreamcast&#039;s main SH4 CPU, and optionally a compiler for the ARM-based AICA sound processor. Your operating system may already have versions of these programs installed to compile code for your computer, but we will need to build a &amp;quot;cross-compiler&amp;quot; for compiling specifically for the Dreamcast. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;KallistiOS&#039;&#039;&#039; or &#039;&#039;KOS&#039;&#039; is an open source development library and pseudo-operating system for the Dreamcast console. It is the best documented and most widely used development kit in the homebrew community. KallistiOS&#039;s very flexible license allows both homebrew and commercial use with no restrictions other than a requirement to include credit for its use in your project, and indeed almost all commercially sold indie Dreamcast titles use it. There are others in existence, like [[libronin]] and [[libdream]], as well as the older development kits [[Katana]] and [[Windows CE]] created by Sega and Microsoft for use in retail games, but this guide will only cover the setup and use of KallistiOS. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;kos-ports&#039;&#039;&#039; is a repository including various libraries which integrate with KallistiOS. We will download and compile these libraries as well. &lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;&#039;debug link&#039;&#039;&#039; is a generic term referring to a hardware accessory to facilitate quickly running and debugging your programs. IP-based links include the Dreamcast&#039;s &#039;&#039;&#039;[[Broadband adapter]]&#039;&#039;&#039; and &#039;&#039;&#039;[[LAN adapter]]&#039;&#039;&#039; accessories, and serial-based links include the [[Coder&#039;s cable]], which is a cable that can connect the Dreamcast&#039;s serial port to your computer via USB or serial. This guide includes instructions for setting up and using the the Broadband adapter and a USB-based coder&#039;s cable.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;dc-tool&#039;&#039;&#039; and &#039;&#039;&#039;dcload&#039;&#039;&#039; are a pair of programs to facilitate using a debug link. &#039;&#039;dc-tool&#039;&#039; runs on your computer and links to a Dreamcast running &#039;&#039;dcload-ip&#039;&#039; or &#039;&#039;dcload-serial&#039;&#039;. With this setup, you can quickly load programs, read console feedback, load assets, transfer data, redirect I/O, handle exceptions, debug problems, and so forth.&lt;br /&gt;
&lt;br /&gt;
=Choosing a debug link solution=&lt;br /&gt;
If you are building the toolchain for the purpose of building existing programs from source with little to no modifications, then a debug link setup might not be necessary for you. You may simply build programs to burn directly to CD-R. However, if you are planning to actively develop for the Dreamcast, then a debug link is a critical component. While Dreamcast emulators are mature and accurate enough to play the vast majority of the system&#039;s games library without issue, many critical bugs may show up on a real Dreamcast system, but not on a Dreamcast emulator. Therefore, it is highly recommended to test on a real system as much as possible. It&#039;s also possible to load software off of a [[Serial SD card adapter]], but without an active link to a computer, debugging and stepping through programs as they execute is significantly more challenging.&lt;br /&gt;
&lt;br /&gt;
Presented below is a table comparing the different options available for a debug link. Due to the cost, potential buyers may want to factor in the ability to play multiplayer games with their purchase. Thus, for comparison, we have included information about the [[Modem]] with [[DreamPi]] as well, but understand that the Modem with DreamPi cannot be used as a debug link.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
!colspan=&amp;quot;6&amp;quot; |Comparison of various Dreamcast connectivity options&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#c0c0c0;&amp;quot; width=&amp;quot;150&amp;quot; | Device: &lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; width=&amp;quot;400&amp;quot; | [[Broadband adapter]] (HIT-400 or HIT-401) &amp;lt;br /&amp;gt;Realtek RTL8139C chipset&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; width=&amp;quot;400&amp;quot; | [[LAN adapter]] (HIT-300) &amp;lt;br /&amp;gt;Fujitsu MB86967 chipset&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; width=&amp;quot;400&amp;quot; | [[Modem]] with [[DreamPi]]&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; width=&amp;quot;400&amp;quot; | USB [[Coder&#039;s cable]]&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; width=&amp;quot;400&amp;quot; | Serial [[Coder&#039;s cable]]&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Useful for dev? || Yes, supports dcload-ip || Yes, supports dcload-ip,&amp;lt;br/&amp;gt;but BBA is superior and cheaper || No, only useful for online multiplayer gaming || Yes, supports dcload-serial || Yes, supports dcload-serial&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Cost || $100 - $200 and up on used markets || $200 and up on used markets,&amp;lt;br/&amp;gt;due to extreme rarity || Kit prices vary, around $100 || See [[Coder&#039;s cable]] for vendors&amp;lt;br /&amp;gt;darc sells for $45, RetroOnyx sells for $85 || Varies on used markets, uncommonly sold&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Can make DIY? || No || No || Yes || Yes || Yes&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Performance || Up to 100 megabits/s || Up to 10 megabits/s || Up to 56 kilobits/s || Up to 1500 kilobits/s || Up to 120 kilobits/s&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Games support || Some games: Phantasy Star Online, Quake III Arena, Toy Racer, POD SpeedZone, Propellor Arena, Unreal Tournament&amp;lt;br /&amp;gt;Some browsers: Broadband Passport, PlanetWeb 3.0 || No games&amp;lt;br /&amp;gt;One browser: Dream Passport for LAN || All multiplayer games with network support&amp;lt;br /&amp;gt;All web browsers || NO multiplayer games support || NO multiplayer games support &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Homebrew support || Homebrew utilities like dcload-ip || Homebrew utilities like dcload-ip || Homebrew utilities don&#039;t support, only multiplayer games || Homebrew utilities like dcload-serial || Homebrew utilities like dcload-serial &lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=Setting up and compiling the toolchain with the dc-chain script=&lt;br /&gt;
===Dependencies===&lt;br /&gt;
First, we&#039;ll need to install dependencies before building the toolchain. Below we have provided commands to install these dependencies on various systems. Many of the packages will likely already be installed on your system, but we have provided an exhaustive list for good measure.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;mw-collapsible mw-collapsed&amp;quot;&amp;gt;&lt;br /&gt;
====macOS on an Intel or Apple Silicon processor====&lt;br /&gt;
&amp;lt;div class=&amp;quot;mw-collapsible-content&amp;quot;&amp;gt;&lt;br /&gt;
First, make sure you install Apple Xcode, including the Command Line tools. You will also need to install several other packages for which we&#039;ll include instructions assuming you have installed the [https://brew.sh/ Homebrew] package manager on your system.&lt;br /&gt;
 brew install make wget gettext texinfo gmp mpfr libmpc libelf jpeg-turbo libpng cmake meson libisofs&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;Important Note for Apple Silicon users&#039;&#039;: On Apple Silicon, Homebrew installs libraries to a path not included by default by the compiler. If you haven&#039;t added these to your &#039;&#039;&#039;~/.zprofile&#039;&#039;&#039;, then add the following lines now and reload your session (or run them in your Terminal session whenever you compile KOS):&lt;br /&gt;
 export CPATH=/opt/homebrew/include&lt;br /&gt;
 export LIBRARY_PATH=/opt/homebrew/lib&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;mw-collapsible mw-collapsed&amp;quot;&amp;gt;&lt;br /&gt;
====Debian/Ubuntu-based Linux====&lt;br /&gt;
&amp;lt;div class=&amp;quot;mw-collapsible-content&amp;quot;&amp;gt;&lt;br /&gt;
 sudo apt-get update&lt;br /&gt;
 sudo apt install gawk patch bzip2 tar make libgmp-dev libmpfr-dev libmpc-dev gettext wget libelf-dev texinfo bison flex sed git build-essential diffutils curl libjpeg-dev libpng-dev python3 pkg-config cmake libisofs-dev meson ninja-build rake cmake&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;mw-collapsible mw-collapsed&amp;quot;&amp;gt;&lt;br /&gt;
====Fedora-based Linux====&lt;br /&gt;
&amp;lt;div class=&amp;quot;mw-collapsible-content&amp;quot;&amp;gt;&lt;br /&gt;
 sudo dnf install gawk patch bzip2 tar make gmp-devel mpfr-devel libmpc-devel gettext wget elfutils-libelf-devel texinfo bison flex sed git diffutils curl libjpeg-turbo-devel libpng-devel gcc-c++ python3 rubygem-rake meson ninja-build cmake&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;mw-collapsible mw-collapsed&amp;quot;&amp;gt;&lt;br /&gt;
====Arch-based Linux====&lt;br /&gt;
&amp;lt;div class=&amp;quot;mw-collapsible-content&amp;quot;&amp;gt;&lt;br /&gt;
 sudo pacman -S --needed gawk patch bzip2 tar make gmp mpfr libmpc gettext wget libelf texinfo bison flex sed git diffutils curl libjpeg-turbo libpng python3 meson ruby-rake cmake&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;mw-collapsible mw-collapsed&amp;quot;&amp;gt;&lt;br /&gt;
====Alpine-based Linux====&lt;br /&gt;
&amp;lt;div class=&amp;quot;mw-collapsible-content&amp;quot;&amp;gt;&lt;br /&gt;
 sudo apk --update add build-base patch bash texinfo gmp-dev mpfr-dev libjpeg-turbo-dev libpng-dev elfutils-dev curl wget python3 git ruby-rake cmake fmt&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Other Linux distributions====&lt;br /&gt;
If you&#039;re using a different Linux- or Unix-based system besides the one above, you may need to reference your distribution&#039;s package database and package manager documentation for the equivalent package names and commands necessary for your system.&lt;br /&gt;
&lt;br /&gt;
===Creating a space for your toolchain installation===&lt;br /&gt;
Create the path where we&#039;ll install the toolchain and KOS, and grant it the proper permissions:&lt;br /&gt;
 sudo mkdir -p /opt/toolchains/dc&lt;br /&gt;
 sudo chmod -R 755 /opt/toolchains/dc&lt;br /&gt;
 sudo chown -R $(id -u):$(id -g) /opt/toolchains/dc&lt;br /&gt;
===Cloning the KallistiOS git repository===&lt;br /&gt;
Next, clone the git repository for the latest &#039;&#039;&#039;2.2.x&#039;&#039;&#039; stable version of KallistiOS: &lt;br /&gt;
 git clone https://github.com/KallistiOS/KallistiOS.git -b v2.2.x /opt/toolchains/dc/kos&lt;br /&gt;
&lt;br /&gt;
Alternatively, you may clone the latest unstable version, which contains the latest changes, but may also contain bugs or issues. Proceed at your own risk using unstable master!&lt;br /&gt;
 git clone https://github.com/KallistiOS/KallistiOS.git -b master /opt/toolchains/dc/kos&lt;br /&gt;
&lt;br /&gt;
===Configuring the dc-chain script===&lt;br /&gt;
Enter the dc-chain directory:&lt;br /&gt;
 cd /opt/toolchains/dc/kos/utils/dc-chain&lt;br /&gt;
&lt;br /&gt;
You may now configure the options to your liking by creating a &amp;lt;code&amp;gt;Makefile.cfg&amp;lt;/code&amp;gt; file in this directory. Copy or rename the &amp;lt;code&amp;gt;Makefile.default.cfg&amp;lt;/code&amp;gt; (KOS 2.2.0 stable) or &amp;lt;code&amp;gt;Makefile.dreamcast.cfg&amp;lt;/code&amp;gt; (KOS master unstable) file to &amp;lt;code&amp;gt;Makefile.cfg&amp;lt;/code&amp;gt; and then use a text editor to edit this file&#039;s settings to your liking. For example, you may like to alter the &amp;lt;code&amp;gt;makejobs&amp;lt;/code&amp;gt; option to the number of threads available on your CPU to speed up the compilation. However, if you run into errors during compilation, you may want to set &amp;lt;code&amp;gt;makejobs=1&amp;lt;/code&amp;gt;, as on some operating systems the toolchain may fail to build with a higher setting.&lt;br /&gt;
&lt;br /&gt;
We will leave the default &#039;&#039;&#039;stable&#039;&#039;&#039; profile for the toolchain, which currently uses GCC 13.2. For advanced users, other profiles are available to you; read the &amp;lt;code&amp;gt;README.md&amp;lt;/code&amp;gt; file in the dc-chain directory for more information if you are interested. If you are unsure about options in the &amp;lt;code&amp;gt;Makefile.cfg&amp;lt;/code&amp;gt;, just leave the defaults alone.&lt;br /&gt;
&lt;br /&gt;
===Downloading and compiling the toolchain===&lt;br /&gt;
Now we will run a script to download files and compile the toolchain. Simply run:&lt;br /&gt;
 make&lt;br /&gt;
&lt;br /&gt;
This will download and unpack the relevant necessary files and then begin the compilation process. The compilation can take anywhere from minutes to a few hours depending on your CPU and number of threads available. When successfully finished, the toolchains will be ready. If you used the command above, the &#039;&#039;&#039;gdb&#039;&#039;&#039; debugger will have been built as well.&lt;br /&gt;
&lt;br /&gt;
===Cleaning up temporary files===&lt;br /&gt;
After building everything, you can clean up the extraneous files in your dc-chain directory by entering:&lt;br /&gt;
 make clean distclean&lt;br /&gt;
&lt;br /&gt;
=Configuring and compiling KOS and kos-ports=&lt;br /&gt;
===Setting up the environment settings===&lt;br /&gt;
Enter the KOS directory:&lt;br /&gt;
 cd /opt/toolchains/dc/kos&lt;br /&gt;
Copy the pre-made environment script into place:&lt;br /&gt;
 cp doc/environ.sh.sample environ.sh &lt;br /&gt;
For most users, the default settings will suffice. However, advanced users may edit the environ.sh to your liking if you&#039;d like to change compile flags or alter paths. If you&#039;d like to have multiple KOS versions installed or multiple toolchain versions installed, you can set up different environ.sh files corresponding to these different configurations by altering the paths.&lt;br /&gt;
&lt;br /&gt;
You will need to run the following command to apply the KOS environment settings to your currently running shell. Run the following command now, &#039;&#039;&#039;and&#039;&#039;&#039; &#039;&#039;whenever&#039;&#039; you open a new shell to work on Dreamcast projects:&lt;br /&gt;
 . /opt/toolchains/dc/kos/environ.sh&lt;br /&gt;
&lt;br /&gt;
===Building KOS===&lt;br /&gt;
Build KOS:&lt;br /&gt;
 make&lt;br /&gt;
KOS is now built.&lt;br /&gt;
===Building kos-ports===&lt;br /&gt;
Clone the kos-ports repository to your system:&lt;br /&gt;
 git clone --recursive https://github.com/KallistiOS/kos-ports /opt/toolchains/dc/kos-ports&lt;br /&gt;
Run the script to build all of the included ports:&lt;br /&gt;
 /opt/toolchains/dc/kos-ports/utils/build-all.sh&lt;br /&gt;
kos-ports is now built.&lt;br /&gt;
&lt;br /&gt;
===Building the KOS examples===&lt;br /&gt;
Enter the KOS examples directory:&lt;br /&gt;
 cd /opt/toolchains/dc/kos/examples&lt;br /&gt;
Build the examples:&lt;br /&gt;
 make&lt;br /&gt;
All of the example programs provided with KallistiOS are now built.&lt;br /&gt;
&lt;br /&gt;
=Further=&lt;br /&gt;
At this point, you will have a cross-compiler built so that you can generate programs for your Dreamcast from code. KallistiOS, the Dreamcast development library and operating system, is built, as is its associated packages within kos-ports, and all of its example programs.&lt;br /&gt;
&lt;br /&gt;
At this point, you will likely want to view some of the following articles:&lt;br /&gt;
* Setting up an IDE for Dreamcast development&lt;br /&gt;
** [[CLion Debugging]]&lt;br /&gt;
** [[Visual Studio Code]]&lt;br /&gt;
* [[Creating a first project with KallistiOS]] (&#039;&#039;&#039;TODO&#039;&#039;&#039;: Explain how to create a new DC project folder with Makefile, adding an external library, create a basic program, and compile it)&lt;br /&gt;
* [[Using a debug link]]&lt;br /&gt;
* [[Burning an example to CD-R]]&lt;br /&gt;
&lt;br /&gt;
=Running an example program through a debug link=&lt;br /&gt;
&#039;&#039;&#039;TODO&#039;&#039;&#039;: &#039;&#039;Give a tutorial on compiling dcload/dc-tool, setting up a serial, USB, or IP debug link, and running 2ndmix demo on real hardware.&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
On your development machine, setup [[dcload-ip]]:&lt;br /&gt;
* [[Using dcload-ip with Linux]]&lt;br /&gt;
* [[Using dcload-ip with Windows Subsystem for Linux]]&lt;br /&gt;
&lt;br /&gt;
For the Dreamcast, download and burn the [[:File:Dcload-2023-06-22.zip|latest version of dcload-ip or dcload-serial]] to a CD-R -- the IP version includes improved DHCP support, so there is no longer a need to configure things beforehand. &lt;br /&gt;
 &lt;br /&gt;
Run one of the examples from the &amp;lt;code&amp;gt;kos/examples/dreamcast&amp;lt;/code&amp;gt; directory with one of the following commands:&lt;br /&gt;
 dc-tool-ip -t &amp;lt;dreamcast IP address&amp;gt; -x example.elf&lt;br /&gt;
&lt;br /&gt;
 dc-tool-ser -t &amp;lt;serial device path&amp;gt; -x example.elf&lt;br /&gt;
&lt;br /&gt;
Run &amp;lt;code&amp;gt;dc-tool-ip&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;dc-tool-ser&amp;lt;/code&amp;gt; without any parameters to get additional options.&lt;br /&gt;
&lt;br /&gt;
=Burning an example program to CD-R=&lt;br /&gt;
&#039;&#039;&#039;TODO&#039;&#039;&#039;: &#039;&#039;Explain how to build mkdcdisc and write 2ndmix demo to CD-R and run on a Dreamcast console&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[https://gitlab.com/simulant/mkdcdisc mkdcdisc] can be used to easily generate a burnable self-boot CDI image.&lt;br /&gt;
Dependencies required:&lt;br /&gt;
* Homebrew: &amp;lt;code&amp;gt;meson libisofs pkg-config&amp;lt;/code&amp;gt;&lt;br /&gt;
* Debian/Ubuntu: &amp;lt;code&amp;gt;libisofs-dev meson ninja-build&amp;lt;/code&amp;gt;&lt;br /&gt;
* Fedora: &amp;lt;code&amp;gt;meson ninja-build&amp;lt;/code&amp;gt;&lt;br /&gt;
* Arch: &amp;lt;code&amp;gt;meson&amp;lt;/code&amp;gt;&lt;br /&gt;
* Alpine: &amp;lt;code&amp;gt;meson libisofs-dev&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Build &amp;lt;code&amp;gt;mkdcdisc&amp;lt;/code&amp;gt;:&lt;br /&gt;
 git clone https://gitlab.com/simulant/mkdcdisc.git&lt;br /&gt;
 cd mkdcdisc&lt;br /&gt;
 meson setup builddir&lt;br /&gt;
 meson compile -C builddir&lt;br /&gt;
 ./builddir/mkdcdisc -h&lt;br /&gt;
and create a CDI image from your compiled ELF like so:&lt;br /&gt;
 mkdcdisc -e MyProgram.elf -o MyProgram.cdi&lt;br /&gt;
Then you can burn the CDI file using DiscJuggler (Windows-only, but also works through [https://www.winehq.org/ WINE]), ImgBurn with the CDI plugin, or the cdiburn *nix script floating around out there. (document this better)&lt;/div&gt;</summary>
		<author><name>Darc</name></author>
	</entry>
	<entry>
		<id>https://dreamcast.wiki/wiki/index.php?title=Getting_Started_with_Dreamcast_development&amp;diff=3834</id>
		<title>Getting Started with Dreamcast development</title>
		<link rel="alternate" type="text/html" href="https://dreamcast.wiki/wiki/index.php?title=Getting_Started_with_Dreamcast_development&amp;diff=3834"/>
		<updated>2025-12-01T23:51:25Z</updated>

		<summary type="html">&lt;p&gt;Darc: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;div style=&amp;quot;float:right;&amp;quot;&amp;gt;__TOC__&amp;lt;/div&amp;gt;&lt;br /&gt;
=Introduction=&lt;br /&gt;
This article will cover the entire beginning process: starting from zero to having a working dev environment with debug link (serial or IP) and self-booting CD-R. This guide will cover the process for the following platforms:&lt;br /&gt;
* &#039;&#039;&#039;Microsoft Windows 10&#039;&#039;&#039; via [https://learn.microsoft.com/en-us/windows/wsl/about Windows Subsystem for Linux]&lt;br /&gt;
** Users desiring a native Windows approach, see [[DreamSDK]] instead&lt;br /&gt;
* &#039;&#039;&#039;macOS&#039;&#039;&#039; on Intel or Apple Silicon systems with the [https://brew.sh/ Homebrew] package manager installed&lt;br /&gt;
* &#039;&#039;&#039;Linux&#039;&#039;&#039;-based systems&lt;br /&gt;
** &#039;&#039;&#039;Debian&#039;&#039;&#039;- and &#039;&#039;&#039;Ubuntu&#039;&#039;&#039;-based distributions using the default &#039;&#039;&#039;apt&#039;&#039;&#039; package manger&lt;br /&gt;
** &#039;&#039;&#039;Fedora&#039;&#039;&#039;-based distributions using the default &#039;&#039;&#039;dnf&#039;&#039;&#039; package manager&lt;br /&gt;
** &#039;&#039;&#039;Arch&#039;&#039;&#039;-based distributions using the default &#039;&#039;&#039;pacman&#039;&#039;&#039; package manager&lt;br /&gt;
** &#039;&#039;&#039;Alpine&#039;&#039;&#039;-based distributions using the default &#039;&#039;&#039;apk&#039;&#039;&#039; package manager&lt;br /&gt;
&lt;br /&gt;
===Need help?===&lt;br /&gt;
Important note: &#039;&#039;This guide aims to remain up to date and work on all of the above platforms, but keeping instructions for such a variety of platforms up-to-date can be difficult. If you run into any errors or other challenges while following this tutorial, or simply need clarification on any of the steps, feel free to ask for assistance on the [https://dcemulation.org/phpBB/viewforum.php?f=29 message board] or [https://discord.gg/wDEfkUh63h Discord chat] and we would be happy to aid you and update the guide for the benefit of future readers and others in the community.&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
===Terms===&lt;br /&gt;
Before we get started, let&#039;s define several terms:&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;&#039;toolchain&#039;&#039;&#039; is a set of programs which turns your code into an executable file for your Dreamcast console. The toolchain includes:&lt;br /&gt;
* &#039;&#039;&#039;GCC&#039;&#039;&#039;, a compiler which supports C, C++, Objective-C, and more (see also [[Rust on Dreamcast]] and [[Programming language support]])&lt;br /&gt;
* &#039;&#039;&#039;binutils&#039;&#039;&#039;, an assembler and linker&lt;br /&gt;
* &#039;&#039;&#039;newlib&#039;&#039;&#039;, a C library&lt;br /&gt;
* &#039;&#039;&#039;gdb&#039;&#039;&#039;, a debugger&lt;br /&gt;
The toolchain includes a compiler for the Dreamcast&#039;s main SH4 CPU, and optionally a compiler for the ARM-based AICA sound processor. Your operating system may already have versions of these programs installed to compile code for your computer, but we will need to build a &amp;quot;cross-compiler&amp;quot; for compiling specifically for the Dreamcast. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;KallistiOS&#039;&#039;&#039; or &#039;&#039;KOS&#039;&#039; is an open source development library and pseudo-operating system for the Dreamcast console. It is the best documented and most widely used development kit in the homebrew community. KallistiOS&#039;s very flexible license allows both homebrew and commercial use with no restrictions other than a requirement to include credit for its use in your project, and indeed almost all commercially sold indie Dreamcast titles use it. There are others in existence, like [[libronin]] and [[libdream]], as well as the older development kits [[Katana]] and [[Windows CE]] created by Sega and Microsoft for use in retail games, but this guide will only cover the setup and use of KallistiOS. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;kos-ports&#039;&#039;&#039; is a repository including various libraries which integrate with KallistiOS. We will download and compile these libraries as well. &lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;&#039;debug link&#039;&#039;&#039; is a generic term referring to a hardware accessory to facilitate quickly running and debugging your programs. IP-based links include the Dreamcast&#039;s &#039;&#039;&#039;[[Broadband adapter]]&#039;&#039;&#039; and &#039;&#039;&#039;[[LAN adapter]]&#039;&#039;&#039; accessories, and serial-based links include the [[Coder&#039;s cable]], which is a cable that can connect the Dreamcast&#039;s serial port to your computer via USB or serial. This guide includes instructions for setting up and using the the Broadband adapter and a USB-based coder&#039;s cable.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;dc-tool&#039;&#039;&#039; and &#039;&#039;&#039;dcload&#039;&#039;&#039; are a pair of programs to facilitate using a debug link. &#039;&#039;dc-tool&#039;&#039; runs on your computer and links to a Dreamcast running &#039;&#039;dcload-ip&#039;&#039; or &#039;&#039;dcload-serial&#039;&#039;. With this setup, you can quickly load programs, read console feedback, load assets, transfer data, redirect I/O, handle exceptions, debug problems, and so forth.&lt;br /&gt;
&lt;br /&gt;
=Choosing a debug link solution=&lt;br /&gt;
If you are building the toolchain for the purpose of building existing programs from source with little to no modifications, then a debug link setup might not be necessary for you. You may simply build programs to burn directly to CD-R. However, if you are planning to actively develop for the Dreamcast, then a debug link is a critical component. While Dreamcast emulators are mature and accurate enough to play the vast majority of the system&#039;s games library without issue, many critical bugs may show up on a real Dreamcast system, but not on a Dreamcast emulator. Therefore, it is highly recommended to test on a real system as much as possible. It&#039;s also possible to load software off of a [[Serial SD card adapter]], but without an active link to a computer, debugging and stepping through programs as they execute is significantly more challenging.&lt;br /&gt;
&lt;br /&gt;
Presented below is a table comparing the different options available for a debug link. Due to the cost, potential buyers may want to factor in the ability to play multiplayer games with their purchase. Thus, for comparison, we have included information about the [[Modem]] with [[DreamPi]] as well, but understand that the Modem with DreamPi cannot be used as a debug link.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
!colspan=&amp;quot;6&amp;quot; |Comparison of various Dreamcast connectivity options&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#c0c0c0;&amp;quot; width=&amp;quot;150&amp;quot; | Device: &lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; width=&amp;quot;400&amp;quot; | [[Broadband adapter]] (HIT-400 or HIT-401) &amp;lt;br /&amp;gt;Realtek RTL8139C chipset&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; width=&amp;quot;400&amp;quot; | [[LAN adapter]] (HIT-300) &amp;lt;br /&amp;gt;Fujitsu MB86967 chipset&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; width=&amp;quot;400&amp;quot; | [[Modem]] with [[DreamPi]]&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; width=&amp;quot;400&amp;quot; | USB [[Coder&#039;s cable]]&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; width=&amp;quot;400&amp;quot; | Serial [[Coder&#039;s cable]]&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Useful for dev? || Yes, supports dcload-ip || Yes, supports dcload-ip,&amp;lt;br/&amp;gt;but BBA is superior and cheaper || No, only useful for online multiplayer gaming || Yes, supports dcload-serial || Yes, supports dcload-serial&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Cost || $100 - $200 and up on used markets || $200 and up on used markets,&amp;lt;br/&amp;gt;due to extreme rarity || Kit prices vary, around $100 || See [[Coder&#039;s cable]] for vendors&amp;lt;br /&amp;gt;darc sells for $45, RetroOnyx sells for $85 || Varies on used markets, uncommonly sold&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Can make DIY? || No || No || Yes || Yes || Yes&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Performance || Up to 100 megabits/s || Up to 10 megabits/s || Up to 56 kilobits/s || Up to 1500 kilobits/s || Up to 120 kilobits/s&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Games support || Some games: Phantasy Star Online, Quake III Arena, Toy Racer, POD SpeedZone, Propellor Arena, Unreal Tournament&amp;lt;br /&amp;gt;Some browsers: Broadband Passport, PlanetWeb 3.0 || No games&amp;lt;br /&amp;gt;One browser: Dream Passport for LAN || All multiplayer games with network support&amp;lt;br /&amp;gt;All web browsers || NO multiplayer games support || NO multiplayer games support &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Homebrew support || Homebrew utilities like dcload-ip || Homebrew utilities like dcload-ip || Homebrew utilities don&#039;t support, only multiplayer games || Homebrew utilities like dcload-serial || Homebrew utilities like dcload-serial &lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=Setting up and compiling the toolchain with the dc-chain script=&lt;br /&gt;
===Dependencies===&lt;br /&gt;
First, we&#039;ll need to install dependencies before building the toolchain. Below we have provided commands to install these dependencies on various systems. Many of the packages will likely already be installed on your system, but we have provided an exhaustive list for good measure.&lt;br /&gt;
====macOS 13 Ventura on an Intel or Apple Silicon processor====&lt;br /&gt;
First, make sure you install Apple Xcode, including the Command Line tools. You will also need to install several other packages for which we&#039;ll include instructions assuming you have installed the [https://brew.sh/ Homebrew] package manager on your system.&lt;br /&gt;
 brew install make wget gettext texinfo gmp mpfr libmpc libelf jpeg-turbo libpng cmake meson libisofs&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;Important Note for Apple Silicon users&#039;&#039;: On Apple Silicon, Homebrew installs libraries to a path not included by default by the compiler. If you haven&#039;t added these to your &#039;&#039;&#039;~/.zprofile&#039;&#039;&#039;, then add the following lines now and reload your session (or run them in your Terminal session whenever you compile KOS):&lt;br /&gt;
 export CPATH=/opt/homebrew/include&lt;br /&gt;
 export LIBRARY_PATH=/opt/homebrew/lib&lt;br /&gt;
&lt;br /&gt;
====Debian/Ubuntu-based Linux====&lt;br /&gt;
 sudo apt-get update&lt;br /&gt;
 sudo apt install gawk patch bzip2 tar make libgmp-dev libmpfr-dev libmpc-dev gettext wget libelf-dev texinfo bison flex sed git build-essential diffutils curl libjpeg-dev libpng-dev python3 pkg-config cmake libisofs-dev meson ninja-build rake cmake&lt;br /&gt;
&lt;br /&gt;
====Fedora-based Linux====&lt;br /&gt;
 sudo dnf install gawk patch bzip2 tar make gmp-devel mpfr-devel libmpc-devel gettext wget elfutils-libelf-devel texinfo bison flex sed git diffutils curl libjpeg-turbo-devel libpng-devel gcc-c++ python3 rubygem-rake meson ninja-build cmake&lt;br /&gt;
&lt;br /&gt;
====Arch-based Linux====&lt;br /&gt;
 sudo pacman -S --needed gawk patch bzip2 tar make gmp mpfr libmpc gettext wget libelf texinfo bison flex sed git diffutils curl libjpeg-turbo libpng python3 meson ruby-rake cmake&lt;br /&gt;
&lt;br /&gt;
====Alpine-based Linux====&lt;br /&gt;
 sudo apk --update add build-base patch bash texinfo gmp-dev mpfr-dev libjpeg-turbo-dev libpng-dev elfutils-dev curl wget python3 git ruby-rake cmake fmt&lt;br /&gt;
&lt;br /&gt;
====Other Linux distributions====&lt;br /&gt;
If you&#039;re using a different Linux- or Unix-based system besides the one above, you may need to reference your distribution&#039;s package database and package manager documentation for the equivalent package names and commands necessary for your system.&lt;br /&gt;
&lt;br /&gt;
===Creating a space for your toolchain installation===&lt;br /&gt;
Create the path where we&#039;ll install the toolchain and KOS, and grant it the proper permissions:&lt;br /&gt;
 sudo mkdir -p /opt/toolchains/dc&lt;br /&gt;
 sudo chmod -R 755 /opt/toolchains/dc&lt;br /&gt;
 sudo chown -R $(id -u):$(id -g) /opt/toolchains/dc&lt;br /&gt;
===Cloning the KallistiOS git repository===&lt;br /&gt;
Next, clone the git repository for the latest &#039;&#039;&#039;2.2.x&#039;&#039;&#039; stable version of KallistiOS: &lt;br /&gt;
 git clone https://github.com/KallistiOS/KallistiOS.git -b v2.2.x /opt/toolchains/dc/kos&lt;br /&gt;
&lt;br /&gt;
Alternatively, you may clone the latest unstable version, which contains the latest changes, but may also contain bugs or issues. Proceed at your own risk using unstable master!&lt;br /&gt;
 git clone https://github.com/KallistiOS/KallistiOS.git -b master /opt/toolchains/dc/kos&lt;br /&gt;
&lt;br /&gt;
===Configuring the dc-chain script===&lt;br /&gt;
Enter the dc-chain directory:&lt;br /&gt;
 cd /opt/toolchains/dc/kos/utils/dc-chain&lt;br /&gt;
&lt;br /&gt;
You may now configure the options to your liking by creating a &amp;lt;code&amp;gt;Makefile.cfg&amp;lt;/code&amp;gt; file in this directory. Copy or rename the &amp;lt;code&amp;gt;Makefile.default.cfg&amp;lt;/code&amp;gt; (KOS 2.2.0 stable) or &amp;lt;code&amp;gt;Makefile.dreamcast.cfg&amp;lt;/code&amp;gt; (KOS master unstable) file to &amp;lt;code&amp;gt;Makefile.cfg&amp;lt;/code&amp;gt; and then use a text editor to edit this file&#039;s settings to your liking. For example, you may like to alter the &amp;lt;code&amp;gt;makejobs&amp;lt;/code&amp;gt; option to the number of threads available on your CPU to speed up the compilation. However, if you run into errors during compilation, you may want to set &amp;lt;code&amp;gt;makejobs=1&amp;lt;/code&amp;gt;, as on some operating systems the toolchain may fail to build with a higher setting.&lt;br /&gt;
&lt;br /&gt;
We will leave the default &#039;&#039;&#039;stable&#039;&#039;&#039; profile for the toolchain, which currently uses GCC 13.2. For advanced users, other profiles are available to you; read the &amp;lt;code&amp;gt;README.md&amp;lt;/code&amp;gt; file in the dc-chain directory for more information if you are interested. If you are unsure about options in the &amp;lt;code&amp;gt;Makefile.cfg&amp;lt;/code&amp;gt;, just leave the defaults alone.&lt;br /&gt;
&lt;br /&gt;
===Downloading and compiling the toolchain===&lt;br /&gt;
Now we will run a script to download files and compile the toolchain. Simply run:&lt;br /&gt;
 make&lt;br /&gt;
&lt;br /&gt;
This will download and unpack the relevant necessary files and then begin the compilation process. The compilation can take anywhere from minutes to a few hours depending on your CPU and number of threads available. When successfully finished, the toolchains will be ready. If you used the command above, the &#039;&#039;&#039;gdb&#039;&#039;&#039; debugger will have been built as well.&lt;br /&gt;
&lt;br /&gt;
===Cleaning up temporary files===&lt;br /&gt;
After building everything, you can clean up the extraneous files in your dc-chain directory by entering:&lt;br /&gt;
 make clean distclean&lt;br /&gt;
&lt;br /&gt;
=Configuring and compiling KOS and kos-ports=&lt;br /&gt;
===Setting up the environment settings===&lt;br /&gt;
Enter the KOS directory:&lt;br /&gt;
 cd /opt/toolchains/dc/kos&lt;br /&gt;
Copy the pre-made environment script into place:&lt;br /&gt;
 cp doc/environ.sh.sample environ.sh &lt;br /&gt;
For most users, the default settings will suffice. However, advanced users may edit the environ.sh to your liking if you&#039;d like to change compile flags or alter paths. If you&#039;d like to have multiple KOS versions installed or multiple toolchain versions installed, you can set up different environ.sh files corresponding to these different configurations by altering the paths.&lt;br /&gt;
&lt;br /&gt;
You will need to run the following command to apply the KOS environment settings to your currently running shell. Run the following command now, &#039;&#039;&#039;and&#039;&#039;&#039; &#039;&#039;whenever&#039;&#039; you open a new shell to work on Dreamcast projects:&lt;br /&gt;
 . /opt/toolchains/dc/kos/environ.sh&lt;br /&gt;
&lt;br /&gt;
===Building KOS===&lt;br /&gt;
Build KOS:&lt;br /&gt;
 make&lt;br /&gt;
KOS is now built.&lt;br /&gt;
===Building kos-ports===&lt;br /&gt;
Clone the kos-ports repository to your system:&lt;br /&gt;
 git clone --recursive https://github.com/KallistiOS/kos-ports /opt/toolchains/dc/kos-ports&lt;br /&gt;
Run the script to build all of the included ports:&lt;br /&gt;
 /opt/toolchains/dc/kos-ports/utils/build-all.sh&lt;br /&gt;
kos-ports is now built.&lt;br /&gt;
&lt;br /&gt;
===Building the KOS examples===&lt;br /&gt;
Enter the KOS examples directory:&lt;br /&gt;
 cd /opt/toolchains/dc/kos/examples&lt;br /&gt;
Build the examples:&lt;br /&gt;
 make&lt;br /&gt;
All of the example programs provided with KallistiOS are now built.&lt;br /&gt;
&lt;br /&gt;
=Further=&lt;br /&gt;
At this point, you will have a cross-compiler built so that you can generate programs for your Dreamcast from code. KallistiOS, the Dreamcast development library and operating system, is built, as is its associated packages within kos-ports, and all of its example programs.&lt;br /&gt;
&lt;br /&gt;
At this point, you will likely want to view some of the following articles:&lt;br /&gt;
* Setting up an IDE for Dreamcast development&lt;br /&gt;
** [[CLion Debugging]]&lt;br /&gt;
** [[Visual Studio Code]]&lt;br /&gt;
* [[Creating a first project with KallistiOS]] (&#039;&#039;&#039;TODO&#039;&#039;&#039;: Explain how to create a new DC project folder with Makefile, adding an external library, create a basic program, and compile it)&lt;br /&gt;
* [[Using a debug link]]&lt;br /&gt;
* [[Burning an example to CD-R]]&lt;br /&gt;
&lt;br /&gt;
=Running an example program through a debug link=&lt;br /&gt;
&#039;&#039;&#039;TODO&#039;&#039;&#039;: &#039;&#039;Give a tutorial on compiling dcload/dc-tool, setting up a serial, USB, or IP debug link, and running 2ndmix demo on real hardware.&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
On your development machine, setup [[dcload-ip]]:&lt;br /&gt;
* [[Using dcload-ip with Linux]]&lt;br /&gt;
* [[Using dcload-ip with Windows Subsystem for Linux]]&lt;br /&gt;
&lt;br /&gt;
For the Dreamcast, download and burn the [[:File:Dcload-2023-06-22.zip|latest version of dcload-ip or dcload-serial]] to a CD-R -- the IP version includes improved DHCP support, so there is no longer a need to configure things beforehand. &lt;br /&gt;
 &lt;br /&gt;
Run one of the examples from the &amp;lt;code&amp;gt;kos/examples/dreamcast&amp;lt;/code&amp;gt; directory with one of the following commands:&lt;br /&gt;
 dc-tool-ip -t &amp;lt;dreamcast IP address&amp;gt; -x example.elf&lt;br /&gt;
&lt;br /&gt;
 dc-tool-ser -t &amp;lt;serial device path&amp;gt; -x example.elf&lt;br /&gt;
&lt;br /&gt;
Run &amp;lt;code&amp;gt;dc-tool-ip&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;dc-tool-ser&amp;lt;/code&amp;gt; without any parameters to get additional options.&lt;br /&gt;
&lt;br /&gt;
=Burning an example program to CD-R=&lt;br /&gt;
&#039;&#039;&#039;TODO&#039;&#039;&#039;: &#039;&#039;Explain how to build mkdcdisc and write 2ndmix demo to CD-R and run on a Dreamcast console&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[https://gitlab.com/simulant/mkdcdisc mkdcdisc] can be used to easily generate a burnable self-boot CDI image.&lt;br /&gt;
Dependencies required:&lt;br /&gt;
* Homebrew: &amp;lt;code&amp;gt;meson libisofs pkg-config&amp;lt;/code&amp;gt;&lt;br /&gt;
* Debian/Ubuntu: &amp;lt;code&amp;gt;libisofs-dev meson ninja-build&amp;lt;/code&amp;gt;&lt;br /&gt;
* Fedora: &amp;lt;code&amp;gt;meson ninja-build&amp;lt;/code&amp;gt;&lt;br /&gt;
* Arch: &amp;lt;code&amp;gt;meson&amp;lt;/code&amp;gt;&lt;br /&gt;
* Alpine: &amp;lt;code&amp;gt;meson libisofs-dev&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Build &amp;lt;code&amp;gt;mkdcdisc&amp;lt;/code&amp;gt;:&lt;br /&gt;
 git clone https://gitlab.com/simulant/mkdcdisc.git&lt;br /&gt;
 cd mkdcdisc&lt;br /&gt;
 meson setup builddir&lt;br /&gt;
 meson compile -C builddir&lt;br /&gt;
 ./builddir/mkdcdisc -h&lt;br /&gt;
and create a CDI image from your compiled ELF like so:&lt;br /&gt;
 mkdcdisc -e MyProgram.elf -o MyProgram.cdi&lt;br /&gt;
Then you can burn the CDI file using DiscJuggler (Windows-only, but also works through [https://www.winehq.org/ WINE]), ImgBurn with the CDI plugin, or the cdiburn *nix script floating around out there. (document this better)&lt;/div&gt;</summary>
		<author><name>Darc</name></author>
	</entry>
	<entry>
		<id>https://dreamcast.wiki/wiki/index.php?title=SaveFileConverter&amp;diff=3827</id>
		<title>SaveFileConverter</title>
		<link rel="alternate" type="text/html" href="https://dreamcast.wiki/wiki/index.php?title=SaveFileConverter&amp;diff=3827"/>
		<updated>2025-10-29T04:04:09Z</updated>

		<summary type="html">&lt;p&gt;Darc: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[https://savefileconverter.com/#/dreamcast SaveFileConverter.com] is a website enabling the conversion of save files from many consoles.&lt;br /&gt;
&lt;br /&gt;
[https://github.com/euan-forrester Euan Forrester] created this tool, and the source code can be found [https://github.com/euan-forrester/save-file-converter on Github]. He hosts this website so you don&#039;t need to run it yourself.&lt;br /&gt;
&lt;br /&gt;
In the case of Dreamcast, the tool disaggregates &#039;&#039;.bin&#039;&#039;, &#039;&#039;.vmu&#039;&#039;, or &#039;&#039;.flash&#039;&#039; files, into individual &#039;&#039;.dci&#039;&#039; files or a set of &#039;&#039;.vmi&#039;&#039; and &#039;&#039;.vms&#039;&#039; files, per game save. So basically you can get a single save file off of a whole memory card file.&lt;br /&gt;
&lt;br /&gt;
Conversely, it can build or aggregate a VMU bin file out of multiple individual saves. Using Save File Converter, you can put combine the save files from multiple different games into a single VMU file.&lt;/div&gt;</summary>
		<author><name>Darc</name></author>
	</entry>
	<entry>
		<id>https://dreamcast.wiki/wiki/index.php?title=Redump2CDI&amp;diff=3817</id>
		<title>Redump2CDI</title>
		<link rel="alternate" type="text/html" href="https://dreamcast.wiki/wiki/index.php?title=Redump2CDI&amp;diff=3817"/>
		<updated>2025-09-11T02:40:07Z</updated>

		<summary type="html">&lt;p&gt;Darc: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;Redump2CDI&#039;&#039;&#039; is a utility created by [[darc]] to convert Redump Dreamcast CD-ROM images to [[DiscJuggler]] format. The Redump image format is a non-standard form of BIN+CUE image that cannot be burned or mounted with much existing software, so conversion is convenient for non-archival purposes.&lt;br /&gt;
&lt;br /&gt;
It can also convert Redump bin/cue images for other systems as well, although this is not its primary intended use case.&lt;br /&gt;
&lt;br /&gt;
Redump2CDI is intended only for Redump Dreamcast CD-ROM images. It is not intended to convert GD-ROM images or old school non-selfboot BIN+CUE Dreamcast files.&lt;br /&gt;
&lt;br /&gt;
* [[:File:Redump2cdi-1.00-2025-09-10.zip|Redump2CDI 1.00 2025-09-10]] (Windows/macOS/Linux/FreeBSD binaries provided)&lt;br /&gt;
* [[:File:Redump2cdi-0.9.0-2024-03-30.zip|Redump2CDI TEST/BETA 0.9.0 2024-03-30]]&lt;br /&gt;
* [[:File:Redump2cdi-0.1.0-2023-09-23.zip|Redump2CDI TEST/BETA 0.1.0 2023-09-23]]&lt;br /&gt;
&lt;br /&gt;
To use, run like so:&lt;br /&gt;
 redump2cdi -i &amp;quot;My Favorite Game (Unl).cue&amp;quot; -o myfavgame.cdi&lt;br /&gt;
&lt;br /&gt;
==Changelog==&lt;br /&gt;
* 1.00: As requested by several users, added ability to ignore unsupported FLAGS, CDTEXTFILE, PERFORMER, SONGWRITER, and TITLE cuesheet commands with a warning about inaccuracy, though images using these flags should work fine. These aren&#039;t used by any Redump Dreamcast images, but this allows redump2cdi to be used for Redump images for other consoles. Added `-u` flag to skip other unsupported, unrecognized commands at user&#039;s own risk.&lt;br /&gt;
* 0.9.0: Added support for processing MODE1/2352 tracks. These aren&#039;t used in Dreamcast images, but support for these tracks was requested as it helps this utility be useful for converting other systems&#039; images, too.&lt;br /&gt;
* 0.8.0: Largely refactored code. Not publicly released.&lt;br /&gt;
* 0.1.0: Initial release.&lt;br /&gt;
&lt;br /&gt;
==Windows GUI application==&lt;br /&gt;
A Windows GUI frontend for Redump2CDI has kindly been provided by teddyrogers. Make sure to download the latest version of Redump2CDI above and place the Windows executable in the same folder as the GUI application.&lt;br /&gt;
&lt;br /&gt;
* [[:File:Redump2CDI-GUI v1.0.zip|Redump2CDI GUI v1.0]]&lt;/div&gt;</summary>
		<author><name>Darc</name></author>
	</entry>
	<entry>
		<id>https://dreamcast.wiki/wiki/index.php?title=Redump2CDI&amp;diff=3816</id>
		<title>Redump2CDI</title>
		<link rel="alternate" type="text/html" href="https://dreamcast.wiki/wiki/index.php?title=Redump2CDI&amp;diff=3816"/>
		<updated>2025-09-11T02:40:01Z</updated>

		<summary type="html">&lt;p&gt;Darc: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;Redump2CDI&#039;&#039;&#039; is a utility created by [[darc]] to convert Redump Dreamcast CD-ROM images to [[DiscJuggler]] format. The Redump image format is a non-standard form of BIN+CUE image that cannot be burned or mounted with much existing software, so conversion is convenient for non-archival purposes.&lt;br /&gt;
&lt;br /&gt;
It can also convert Redump bin/cue images for other systems as well, although this is not its primary intended use case.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Redump2CDI is intended only for Redump Dreamcast CD-ROM images. It is not intended to convert GD-ROM images or old school non-selfboot BIN+CUE Dreamcast files.&lt;br /&gt;
&lt;br /&gt;
* [[:File:Redump2cdi-1.00-2025-09-10.zip|Redump2CDI 1.00 2025-09-10]] (Windows/macOS/Linux/FreeBSD binaries provided)&lt;br /&gt;
* [[:File:Redump2cdi-0.9.0-2024-03-30.zip|Redump2CDI TEST/BETA 0.9.0 2024-03-30]]&lt;br /&gt;
* [[:File:Redump2cdi-0.1.0-2023-09-23.zip|Redump2CDI TEST/BETA 0.1.0 2023-09-23]]&lt;br /&gt;
&lt;br /&gt;
To use, run like so:&lt;br /&gt;
 redump2cdi -i &amp;quot;My Favorite Game (Unl).cue&amp;quot; -o myfavgame.cdi&lt;br /&gt;
&lt;br /&gt;
==Changelog==&lt;br /&gt;
* 1.00: As requested by several users, added ability to ignore unsupported FLAGS, CDTEXTFILE, PERFORMER, SONGWRITER, and TITLE cuesheet commands with a warning about inaccuracy, though images using these flags should work fine. These aren&#039;t used by any Redump Dreamcast images, but this allows redump2cdi to be used for Redump images for other consoles. Added `-u` flag to skip other unsupported, unrecognized commands at user&#039;s own risk.&lt;br /&gt;
* 0.9.0: Added support for processing MODE1/2352 tracks. These aren&#039;t used in Dreamcast images, but support for these tracks was requested as it helps this utility be useful for converting other systems&#039; images, too.&lt;br /&gt;
* 0.8.0: Largely refactored code. Not publicly released.&lt;br /&gt;
* 0.1.0: Initial release.&lt;br /&gt;
&lt;br /&gt;
==Windows GUI application==&lt;br /&gt;
A Windows GUI frontend for Redump2CDI has kindly been provided by teddyrogers. Make sure to download the latest version of Redump2CDI above and place the Windows executable in the same folder as the GUI application.&lt;br /&gt;
&lt;br /&gt;
* [[:File:Redump2CDI-GUI v1.0.zip|Redump2CDI GUI v1.0]]&lt;/div&gt;</summary>
		<author><name>Darc</name></author>
	</entry>
	<entry>
		<id>https://dreamcast.wiki/wiki/index.php?title=Redump2CDI&amp;diff=3815</id>
		<title>Redump2CDI</title>
		<link rel="alternate" type="text/html" href="https://dreamcast.wiki/wiki/index.php?title=Redump2CDI&amp;diff=3815"/>
		<updated>2025-09-11T02:39:33Z</updated>

		<summary type="html">&lt;p&gt;Darc: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;Redump2CDI&#039;&#039;&#039; is a utility created by [[darc]] to convert Redump Dreamcast CD-ROM images to [[DiscJuggler]] format. The Redump image format is a non-standard form of BIN+CUE image that cannot be burned or mounted with much existing software, so conversion is convenient for non-archival purposes.&lt;br /&gt;
&lt;br /&gt;
It can also convert Redump bin/cue images for other systems as well, although this is not its primary intended use case.&lt;br /&gt;
&lt;br /&gt;
Redump2CDI is intended only for Redump Dreamcast CD-ROM images. It is not intended to convert GD-ROM images or old school non-selfboot BIN+CUE Dreamcast files.&lt;br /&gt;
&lt;br /&gt;
This is a beta version.&lt;br /&gt;
&lt;br /&gt;
* [[:File:Redump2cdi-0.9.0-2024-03-30.zip|Redump2CDI TEST/BETA 0.9.0 2024-03-30]] (Windows/macOS/Linux/FreeBSD binaries provided)&lt;br /&gt;
* [[:File:Redump2cdi-0.1.0-2023-09-23.zip|Redump2CDI TEST/BETA 0.1.0 2023-09-23]]&lt;br /&gt;
&lt;br /&gt;
To use, run like so:&lt;br /&gt;
 redump2cdi -i &amp;quot;My Favorite Game (Unl).cue&amp;quot; -o myfavgame.cdi&lt;br /&gt;
&lt;br /&gt;
==Changelog==&lt;br /&gt;
* 1.00: As requested by several users, added ability to ignore unsupported FLAGS, CDTEXTFILE, PERFORMER, SONGWRITER, and TITLE cuesheet commands with a warning about inaccuracy, though images using these flags should work fine. These aren&#039;t used by any Redump Dreamcast images, but this allows redump2cdi to be used for Redump images for other consoles. Added `-u` flag to skip other unsupported, unrecognized commands at user&#039;s own risk.&lt;br /&gt;
* 0.9.0: Added support for processing MODE1/2352 tracks. These aren&#039;t used in Dreamcast images, but support for these tracks was requested as it helps this utility be useful for converting other systems&#039; images, too.&lt;br /&gt;
* 0.8.0: Largely refactored code. Not publicly released.&lt;br /&gt;
* 0.1.0: Initial release.&lt;br /&gt;
&lt;br /&gt;
==Windows GUI application==&lt;br /&gt;
A Windows GUI frontend for Redump2CDI has kindly been provided by teddyrogers. Make sure to download the latest version of Redump2CDI above and place the Windows executable in the same folder as the GUI application.&lt;br /&gt;
&lt;br /&gt;
* [[:File:Redump2CDI-GUI v1.0.zip|Redump2CDI GUI v1.0]]&lt;/div&gt;</summary>
		<author><name>Darc</name></author>
	</entry>
	<entry>
		<id>https://dreamcast.wiki/wiki/index.php?title=File:Redump2cdi-1.00-2025-09-10.zip&amp;diff=3814</id>
		<title>File:Redump2cdi-1.00-2025-09-10.zip</title>
		<link rel="alternate" type="text/html" href="https://dreamcast.wiki/wiki/index.php?title=File:Redump2cdi-1.00-2025-09-10.zip&amp;diff=3814"/>
		<updated>2025-09-11T02:26:50Z</updated>

		<summary type="html">&lt;p&gt;Darc: Uploaded own work with UploadWizard&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=={{int:filedesc}}==&lt;br /&gt;
{{Information&lt;br /&gt;
|description={{no|1=Redump2cdi-1.00-2025-09-10}}&lt;br /&gt;
|date=2025-09-10&lt;br /&gt;
|source={{own}}&lt;br /&gt;
|author=[[User:Darc|Darc]]&lt;br /&gt;
|permission=&lt;br /&gt;
|other versions=&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
=={{int:license-header}}==&lt;br /&gt;
{{licensing|generic}}&lt;/div&gt;</summary>
		<author><name>Darc</name></author>
	</entry>
	<entry>
		<id>https://dreamcast.wiki/wiki/index.php?title=Redump2CDI&amp;diff=3813</id>
		<title>Redump2CDI</title>
		<link rel="alternate" type="text/html" href="https://dreamcast.wiki/wiki/index.php?title=Redump2CDI&amp;diff=3813"/>
		<updated>2025-09-11T02:18:42Z</updated>

		<summary type="html">&lt;p&gt;Darc: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;Redump2CDI&#039;&#039;&#039; is a utility created by [[darc]] to convert Redump Dreamcast CD-ROM images to [[DiscJuggler]] format. The Redump image format is a non-standard form of BIN+CUE image that cannot be burned or mounted with much existing software, so conversion is convenient for non-archival purposes.&lt;br /&gt;
&lt;br /&gt;
Redump2CDI is intended only for Redump Dreamcast CD-ROM images. It is not intended to convert GD-ROM images or old school non-selfboot BIN+CUE Dreamcast files.&lt;br /&gt;
&lt;br /&gt;
This is a beta version.&lt;br /&gt;
&lt;br /&gt;
* [[:File:Redump2cdi-0.9.0-2024-03-30.zip|Redump2CDI TEST/BETA 0.9.0 2024-03-30]] (Windows/macOS/Linux/FreeBSD binaries provided)&lt;br /&gt;
* [[:File:Redump2cdi-0.1.0-2023-09-23.zip|Redump2CDI TEST/BETA 0.1.0 2023-09-23]]&lt;br /&gt;
&lt;br /&gt;
To use, run like so:&lt;br /&gt;
 redump2cdi -i &amp;quot;My Favorite Game (Unl).cue&amp;quot; -o myfavgame.cdi&lt;br /&gt;
&lt;br /&gt;
==Changelog==&lt;br /&gt;
* 1.00: As requested by several users, added ability to ignore unsupported FLAGS, CDTEXTFILE, PERFORMER, SONGWRITER, and TITLE cuesheet commands with a warning about inaccuracy, though images using these flags should work fine. These aren&#039;t used by any Redump Dreamcast images, but this allows redump2cdi to be used for Redump images for other consoles. Added `-u` flag to skip other unsupported, unrecognized commands at user&#039;s own risk.&lt;br /&gt;
* 0.9.0: Added support for processing MODE1/2352 tracks. These aren&#039;t used in Dreamcast images, but support for these tracks was requested as it helps this utility be useful for converting other systems&#039; images, too.&lt;br /&gt;
* 0.8.0: Largely refactored code. Not publicly released.&lt;br /&gt;
* 0.1.0: Initial release.&lt;br /&gt;
&lt;br /&gt;
==Windows GUI application==&lt;br /&gt;
A Windows GUI frontend for Redump2CDI has kindly been provided by teddyrogers. Make sure to download the latest version of Redump2CDI above and place the Windows executable in the same folder as the GUI application.&lt;br /&gt;
&lt;br /&gt;
* [[:File:Redump2CDI-GUI v1.0.zip|Redump2CDI GUI v1.0]]&lt;/div&gt;</summary>
		<author><name>Darc</name></author>
	</entry>
	<entry>
		<id>https://dreamcast.wiki/wiki/index.php?title=Getting_Started_with_Dreamcast_development&amp;diff=3808</id>
		<title>Getting Started with Dreamcast development</title>
		<link rel="alternate" type="text/html" href="https://dreamcast.wiki/wiki/index.php?title=Getting_Started_with_Dreamcast_development&amp;diff=3808"/>
		<updated>2025-08-20T17:06:56Z</updated>

		<summary type="html">&lt;p&gt;Darc: /* Cloning the KallistiOS git repository */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;div style=&amp;quot;float:right;&amp;quot;&amp;gt;__TOC__&amp;lt;/div&amp;gt;&lt;br /&gt;
=Introduction=&lt;br /&gt;
This article will cover the entire beginning process: starting from zero to having a working dev environment with debug link (serial or IP) and self-booting CD-R. This guide will cover the process for the following platforms:&lt;br /&gt;
* &#039;&#039;&#039;Microsoft Windows 10&#039;&#039;&#039; via [https://learn.microsoft.com/en-us/windows/wsl/about Windows Subsystem for Linux]&lt;br /&gt;
** Users desiring a native Windows approach, see [[DreamSDK]] instead&lt;br /&gt;
* &#039;&#039;&#039;macOS&#039;&#039;&#039; on Intel or Apple Silicon systems with the [https://brew.sh/ Homebrew] package manager installed&lt;br /&gt;
* &#039;&#039;&#039;Linux&#039;&#039;&#039;-based systems&lt;br /&gt;
** &#039;&#039;&#039;Debian&#039;&#039;&#039;- and &#039;&#039;&#039;Ubuntu&#039;&#039;&#039;-based distributions using the default &#039;&#039;&#039;apt&#039;&#039;&#039; package manger&lt;br /&gt;
** &#039;&#039;&#039;Fedora&#039;&#039;&#039;-based distributions using the default &#039;&#039;&#039;dnf&#039;&#039;&#039; package manager&lt;br /&gt;
** &#039;&#039;&#039;Arch&#039;&#039;&#039;-based distributions using the default &#039;&#039;&#039;pacman&#039;&#039;&#039; package manager&lt;br /&gt;
** &#039;&#039;&#039;Alpine&#039;&#039;&#039;-based distributions using the default &#039;&#039;&#039;apk&#039;&#039;&#039; package manager&lt;br /&gt;
&lt;br /&gt;
===Need help?===&lt;br /&gt;
Important note: &#039;&#039;This guide aims to remain up to date and work on all of the above platforms, but keeping instructions for such a variety of platforms up-to-date can be difficult. If you run into any errors or other challenges while following this tutorial, or simply need clarification on any of the steps, feel free to ask for assistance on the [https://dcemulation.org/phpBB/viewforum.php?f=29 message board] or [https://discord.gg/wDEfkUh63h Discord chat] and we would be happy to aid you and update the guide for the benefit of future readers and others in the community.&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
===Terms===&lt;br /&gt;
Before we get started, let&#039;s define several terms:&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;&#039;toolchain&#039;&#039;&#039; is a set of programs which turns your code into an executable file for your Dreamcast console. The toolchain includes:&lt;br /&gt;
* &#039;&#039;&#039;GCC&#039;&#039;&#039;, a compiler which supports C, C++, Objective-C, and more (see also [[Rust on Dreamcast]] and [[Programming language support]])&lt;br /&gt;
* &#039;&#039;&#039;binutils&#039;&#039;&#039;, an assembler and linker&lt;br /&gt;
* &#039;&#039;&#039;newlib&#039;&#039;&#039;, a C library&lt;br /&gt;
* &#039;&#039;&#039;gdb&#039;&#039;&#039;, a debugger&lt;br /&gt;
The toolchain includes a compiler for the Dreamcast&#039;s main SH4 CPU, and optionally a compiler for the ARM-based AICA sound processor. Your operating system may already have versions of these programs installed to compile code for your computer, but we will need to build a &amp;quot;cross-compiler&amp;quot; for compiling specifically for the Dreamcast. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;KallistiOS&#039;&#039;&#039; or &#039;&#039;KOS&#039;&#039; is an open source development library and pseudo-operating system for the Dreamcast console. It is the best documented and most widely used development kit in the homebrew community. KallistiOS&#039;s very flexible license allows both homebrew and commercial use with no restrictions other than a requirement to include credit for its use in your project, and indeed almost all commercially sold indie Dreamcast titles use it. There are others in existence, like [[libronin]] and [[libdream]], as well as the older development kits [[Katana]] and [[Windows CE]] created by Sega and Microsoft for use in retail games, but this guide will only cover the setup and use of KallistiOS. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;kos-ports&#039;&#039;&#039; is a repository including various libraries which integrate with KallistiOS. We will download and compile these libraries as well. &lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;&#039;debug link&#039;&#039;&#039; is a generic term referring to a hardware accessory to facilitate quickly running and debugging your programs. IP-based links include the Dreamcast&#039;s &#039;&#039;&#039;[[Broadband adapter]]&#039;&#039;&#039; and &#039;&#039;&#039;[[LAN adapter]]&#039;&#039;&#039; accessories, and serial-based links include the [[Coder&#039;s cable]], which is a cable that can connect the Dreamcast&#039;s serial port to your computer via USB or serial. This guide includes instructions for setting up and using the the Broadband adapter and a USB-based coder&#039;s cable.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;dc-tool&#039;&#039;&#039; and &#039;&#039;&#039;dcload&#039;&#039;&#039; are a pair of programs to facilitate using a debug link. &#039;&#039;dc-tool&#039;&#039; runs on your computer and links to a Dreamcast running &#039;&#039;dcload-ip&#039;&#039; or &#039;&#039;dcload-serial&#039;&#039;. With this setup, you can quickly load programs, read console feedback, load assets, transfer data, redirect I/O, handle exceptions, debug problems, and so forth.&lt;br /&gt;
&lt;br /&gt;
=Choosing a debug link solution=&lt;br /&gt;
If you are building the toolchain for the purpose of building existing programs from source with little to no modifications, then a debug link setup might not be necessary for you. You may simply build programs to burn directly to CD-R. However, if you are planning to actively develop for the Dreamcast, then a debug link is a critical component. While Dreamcast emulators are mature and accurate enough to play the vast majority of the system&#039;s games library without issue, many critical bugs may show up on a real Dreamcast system, but not on a Dreamcast emulator. Therefore, it is highly recommended to test on a real system as much as possible. It&#039;s also possible to load software off of a [[Serial SD card adapter]], but without an active link to a computer, debugging and stepping through programs as they execute is significantly more challenging.&lt;br /&gt;
&lt;br /&gt;
Presented below is a table comparing the different options available for a debug link. Due to the cost, potential buyers may want to factor in the ability to play multiplayer games with their purchase. Thus, for comparison, we have included information about the [[Modem]] with [[DreamPi]] as well, but understand that the Modem with DreamPi cannot be used as a debug link.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
!colspan=&amp;quot;6&amp;quot; |Comparison of various Dreamcast connectivity options&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#c0c0c0;&amp;quot; width=&amp;quot;150&amp;quot; | Device: &lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; width=&amp;quot;400&amp;quot; | [[Broadband adapter]] (HIT-400 or HIT-401) &amp;lt;br /&amp;gt;Realtek RTL8139C chipset&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; width=&amp;quot;400&amp;quot; | [[LAN adapter]] (HIT-300) &amp;lt;br /&amp;gt;Fujitsu MB86967 chipset&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; width=&amp;quot;400&amp;quot; | [[Modem]] with [[DreamPi]]&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; width=&amp;quot;400&amp;quot; | USB [[Coder&#039;s cable]]&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; width=&amp;quot;400&amp;quot; | Serial [[Coder&#039;s cable]]&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Useful for dev? || Yes, supports dcload-ip || Yes, supports dcload-ip,&amp;lt;br/&amp;gt;but BBA is superior and cheaper || No, only useful for online multiplayer gaming || Yes, supports dcload-serial || Yes, supports dcload-serial&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Cost || $100 - $200 and up on used markets || $200 and up on used markets,&amp;lt;br/&amp;gt;due to extreme rarity || Kit prices vary, around $100 || See [[Coder&#039;s cable]] for vendors&amp;lt;br /&amp;gt;darc sells for $45, RetroOnyx sells for $85 || Varies on used markets, uncommonly sold&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Can make DIY? || No || No || Yes || Yes || Yes&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Performance || Up to 100 megabits/s || Up to 10 megabits/s || Up to 56 kilobits/s || Up to 1500 kilobits/s || Up to 120 kilobits/s&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Games support || Some games: Phantasy Star Online, Quake III Arena, Toy Racer, POD SpeedZone, Propellor Arena, Unreal Tournament&amp;lt;br /&amp;gt;Some browsers: Broadband Passport, PlanetWeb 3.0 || No games&amp;lt;br /&amp;gt;One browser: Dream Passport for LAN || All multiplayer games with network support&amp;lt;br /&amp;gt;All web browsers || NO multiplayer games support || NO multiplayer games support &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Homebrew support || Homebrew utilities like dcload-ip || Homebrew utilities like dcload-ip || Homebrew utilities don&#039;t support, only multiplayer games || Homebrew utilities like dcload-serial || Homebrew utilities like dcload-serial &lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=Setting up and compiling the toolchain with the dc-chain script=&lt;br /&gt;
===Dependencies===&lt;br /&gt;
First, we&#039;ll need to install dependencies before building the toolchain. Below we have provided commands to install these dependencies on various systems. Many of the packages will likely already be installed on your system, but we have provided an exhaustive list for good measure.&lt;br /&gt;
====macOS 13 Ventura on an Intel or Apple Silicon processor====&lt;br /&gt;
First, make sure you install Apple Xcode, including the Command Line tools. You will also need to install several other packages for which we&#039;ll include instructions assuming you have installed the [https://brew.sh/ Homebrew] package manager on your system.&lt;br /&gt;
 brew install wget gettext texinfo gmp mpfr libmpc libelf jpeg-turbo libpng cmake meson libisofs&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;Important Note for Apple Silicon users&#039;&#039;: On Apple Silicon, Homebrew installs libraries to a path not included by default by the compiler. If you haven&#039;t added these to your &#039;&#039;&#039;~/.zprofile&#039;&#039;&#039;, then add the following lines now and reload your session (or run them in your Terminal session whenever you compile KOS):&lt;br /&gt;
 export CPATH=/opt/homebrew/include&lt;br /&gt;
 export LIBRARY_PATH=/opt/homebrew/lib&lt;br /&gt;
&lt;br /&gt;
====Debian/Ubuntu-based Linux====&lt;br /&gt;
 sudo apt-get update&lt;br /&gt;
 sudo apt install gawk patch bzip2 tar make libgmp-dev libmpfr-dev libmpc-dev gettext wget libelf-dev texinfo bison flex sed git build-essential diffutils curl libjpeg-dev libpng-dev python3 pkg-config cmake libisofs-dev meson ninja-build rake cmake&lt;br /&gt;
&lt;br /&gt;
====Fedora-based Linux====&lt;br /&gt;
 sudo dnf install gawk patch bzip2 tar make gmp-devel mpfr-devel libmpc-devel gettext wget elfutils-libelf-devel texinfo bison flex sed git diffutils curl libjpeg-turbo-devel libpng-devel gcc-c++ python3 rubygem-rake meson ninja-build cmake&lt;br /&gt;
&lt;br /&gt;
====Arch-based Linux====&lt;br /&gt;
 sudo pacman -S --needed gawk patch bzip2 tar make gmp mpfr libmpc gettext wget libelf texinfo bison flex sed git diffutils curl libjpeg-turbo libpng python3 meson ruby-rake cmake&lt;br /&gt;
&lt;br /&gt;
====Alpine-based Linux====&lt;br /&gt;
 sudo apk --update add build-base patch bash texinfo gmp-dev mpfr-dev libjpeg-turbo-dev libpng-dev elfutils-dev curl wget python3 git ruby-rake cmake fmt&lt;br /&gt;
&lt;br /&gt;
====Other Linux distributions====&lt;br /&gt;
If you&#039;re using a different Linux- or Unix-based system besides the one above, you may need to reference your distribution&#039;s package database and package manager documentation for the equivalent package names and commands necessary for your system.&lt;br /&gt;
&lt;br /&gt;
===Creating a space for your toolchain installation===&lt;br /&gt;
Create the path where we&#039;ll install the toolchain and KOS, and grant it the proper permissions:&lt;br /&gt;
 sudo mkdir -p /opt/toolchains/dc&lt;br /&gt;
 sudo chmod -R 755 /opt/toolchains/dc&lt;br /&gt;
 sudo chown -R $(id -u):$(id -g) /opt/toolchains/dc&lt;br /&gt;
===Cloning the KallistiOS git repository===&lt;br /&gt;
Next, clone the git repository for the latest &#039;&#039;&#039;2.2.x&#039;&#039;&#039; stable version of KallistiOS: &lt;br /&gt;
 git clone https://github.com/KallistiOS/KallistiOS.git -b v2.2.x /opt/toolchains/dc/kos&lt;br /&gt;
&lt;br /&gt;
Alternatively, you may clone the latest unstable version, which contains the latest changes, but may also contain bugs or issues. Proceed at your own risk using unstable master!&lt;br /&gt;
 git clone https://github.com/KallistiOS/KallistiOS.git -b master /opt/toolchains/dc/kos&lt;br /&gt;
&lt;br /&gt;
===Configuring the dc-chain script===&lt;br /&gt;
Enter the dc-chain directory:&lt;br /&gt;
 cd /opt/toolchains/dc/kos/utils/dc-chain&lt;br /&gt;
&lt;br /&gt;
You may now configure the options to your liking by creating a &amp;lt;code&amp;gt;Makefile.cfg&amp;lt;/code&amp;gt; file in this directory. Copy or rename the &amp;lt;code&amp;gt;Makefile.default.cfg&amp;lt;/code&amp;gt; (KOS 2.2.0 stable) or &amp;lt;code&amp;gt;Makefile.dreamcast.cfg&amp;lt;/code&amp;gt; (KOS master unstable) file to &amp;lt;code&amp;gt;Makefile.cfg&amp;lt;/code&amp;gt; and then use a text editor to edit this file&#039;s settings to your liking. For example, you may like to alter the &amp;lt;code&amp;gt;makejobs&amp;lt;/code&amp;gt; option to the number of threads available on your CPU to speed up the compilation. However, if you run into errors during compilation, you may want to set &amp;lt;code&amp;gt;makejobs=1&amp;lt;/code&amp;gt;, as on some operating systems the toolchain may fail to build with a higher setting.&lt;br /&gt;
&lt;br /&gt;
We will leave the default &#039;&#039;&#039;stable&#039;&#039;&#039; profile for the toolchain, which currently uses GCC 13.2. For advanced users, other profiles are available to you; read the &amp;lt;code&amp;gt;README.md&amp;lt;/code&amp;gt; file in the dc-chain directory for more information if you are interested. If you are unsure about options in the &amp;lt;code&amp;gt;Makefile.cfg&amp;lt;/code&amp;gt;, just leave the defaults alone.&lt;br /&gt;
&lt;br /&gt;
===Downloading and compiling the toolchain===&lt;br /&gt;
Now we will run a script to download files and compile the toolchain. Simply run:&lt;br /&gt;
 make&lt;br /&gt;
&lt;br /&gt;
This will download and unpack the relevant necessary files and then begin the compilation process. The compilation can take anywhere from minutes to a few hours depending on your CPU and number of threads available. When successfully finished, the toolchains will be ready. If you used the command above, the &#039;&#039;&#039;gdb&#039;&#039;&#039; debugger will have been built as well.&lt;br /&gt;
&lt;br /&gt;
===Cleaning up temporary files===&lt;br /&gt;
After building everything, you can clean up the extraneous files in your dc-chain directory by entering:&lt;br /&gt;
 make clean distclean&lt;br /&gt;
&lt;br /&gt;
=Configuring and compiling KOS and kos-ports=&lt;br /&gt;
===Setting up the environment settings===&lt;br /&gt;
Enter the KOS directory:&lt;br /&gt;
 cd /opt/toolchains/dc/kos&lt;br /&gt;
Copy the pre-made environment script into place:&lt;br /&gt;
 cp doc/environ.sh.sample environ.sh &lt;br /&gt;
For most users, the default settings will suffice. However, advanced users may edit the environ.sh to your liking if you&#039;d like to change compile flags or alter paths. If you&#039;d like to have multiple KOS versions installed or multiple toolchain versions installed, you can set up different environ.sh files corresponding to these different configurations by altering the paths.&lt;br /&gt;
&lt;br /&gt;
You will need to run the following command to apply the KOS environment settings to your currently running shell. Run the following command now, &#039;&#039;&#039;and&#039;&#039;&#039; &#039;&#039;whenever&#039;&#039; you open a new shell to work on Dreamcast projects:&lt;br /&gt;
 . /opt/toolchains/dc/kos/environ.sh&lt;br /&gt;
&lt;br /&gt;
===Building KOS===&lt;br /&gt;
Build KOS:&lt;br /&gt;
 make&lt;br /&gt;
KOS is now built.&lt;br /&gt;
===Building kos-ports===&lt;br /&gt;
Clone the kos-ports repository to your system:&lt;br /&gt;
 git clone --recursive https://github.com/KallistiOS/kos-ports /opt/toolchains/dc/kos-ports&lt;br /&gt;
Run the script to build all of the included ports:&lt;br /&gt;
 /opt/toolchains/dc/kos-ports/utils/build-all.sh&lt;br /&gt;
kos-ports is now built.&lt;br /&gt;
&lt;br /&gt;
===Building the KOS examples===&lt;br /&gt;
Enter the KOS examples directory:&lt;br /&gt;
 cd /opt/toolchains/dc/kos/examples&lt;br /&gt;
Build the examples:&lt;br /&gt;
 make&lt;br /&gt;
All of the example programs provided with KallistiOS are now built.&lt;br /&gt;
&lt;br /&gt;
=Further=&lt;br /&gt;
At this point, you will have a cross-compiler built so that you can generate programs for your Dreamcast from code. KallistiOS, the Dreamcast development library and operating system, is built, as is its associated packages within kos-ports, and all of its example programs.&lt;br /&gt;
&lt;br /&gt;
At this point, you will likely want to view some of the following articles:&lt;br /&gt;
* Setting up an IDE for Dreamcast development&lt;br /&gt;
** [[CLion Debugging]]&lt;br /&gt;
** [[Visual Studio Code]]&lt;br /&gt;
* [[Creating a first project with KallistiOS]] (&#039;&#039;&#039;TODO&#039;&#039;&#039;: Explain how to create a new DC project folder with Makefile, adding an external library, create a basic program, and compile it)&lt;br /&gt;
* [[Using a debug link]]&lt;br /&gt;
* [[Burning an example to CD-R]]&lt;br /&gt;
&lt;br /&gt;
=Running an example program through a debug link=&lt;br /&gt;
&#039;&#039;&#039;TODO&#039;&#039;&#039;: &#039;&#039;Give a tutorial on compiling dcload/dc-tool, setting up a serial, USB, or IP debug link, and running 2ndmix demo on real hardware.&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
On your development machine, setup [[dcload-ip]]:&lt;br /&gt;
* [[Using dcload-ip with Linux]]&lt;br /&gt;
* [[Using dcload-ip with Windows Subsystem for Linux]]&lt;br /&gt;
&lt;br /&gt;
For the Dreamcast, download and burn the [[:File:Dcload-2023-06-22.zip|latest version of dcload-ip or dcload-serial]] to a CD-R -- the IP version includes improved DHCP support, so there is no longer a need to configure things beforehand. &lt;br /&gt;
 &lt;br /&gt;
Run one of the examples from the &amp;lt;code&amp;gt;kos/examples/dreamcast&amp;lt;/code&amp;gt; directory with one of the following commands:&lt;br /&gt;
 dc-tool-ip -t &amp;lt;dreamcast IP address&amp;gt; -x example.elf&lt;br /&gt;
&lt;br /&gt;
 dc-tool-ser -t &amp;lt;serial device path&amp;gt; -x example.elf&lt;br /&gt;
&lt;br /&gt;
Run &amp;lt;code&amp;gt;dc-tool-ip&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;dc-tool-ser&amp;lt;/code&amp;gt; without any parameters to get additional options.&lt;br /&gt;
&lt;br /&gt;
=Burning an example program to CD-R=&lt;br /&gt;
&#039;&#039;&#039;TODO&#039;&#039;&#039;: &#039;&#039;Explain how to build mkdcdisc and write 2ndmix demo to CD-R and run on a Dreamcast console&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[https://gitlab.com/simulant/mkdcdisc mkdcdisc] can be used to easily generate a burnable self-boot CDI image.&lt;br /&gt;
Dependencies required:&lt;br /&gt;
* Homebrew: &amp;lt;code&amp;gt;meson libisofs pkg-config&amp;lt;/code&amp;gt;&lt;br /&gt;
* Debian/Ubuntu: &amp;lt;code&amp;gt;libisofs-dev meson ninja-build&amp;lt;/code&amp;gt;&lt;br /&gt;
* Fedora: &amp;lt;code&amp;gt;meson ninja-build&amp;lt;/code&amp;gt;&lt;br /&gt;
* Arch: &amp;lt;code&amp;gt;meson&amp;lt;/code&amp;gt;&lt;br /&gt;
* Alpine: &amp;lt;code&amp;gt;meson libisofs-dev&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Build &amp;lt;code&amp;gt;mkdcdisc&amp;lt;/code&amp;gt;:&lt;br /&gt;
 git clone https://gitlab.com/simulant/mkdcdisc.git&lt;br /&gt;
 cd mkdcdisc&lt;br /&gt;
 meson setup builddir&lt;br /&gt;
 meson compile -C builddir&lt;br /&gt;
 ./builddir/mkdcdisc -h&lt;br /&gt;
and create a CDI image from your compiled ELF like so:&lt;br /&gt;
 mkdcdisc -e MyProgram.elf -o MyProgram.cdi&lt;br /&gt;
Then you can burn the CDI file using DiscJuggler (Windows-only, but also works through [https://www.winehq.org/ WINE]), ImgBurn with the CDI plugin, or the cdiburn *nix script floating around out there. (document this better)&lt;/div&gt;</summary>
		<author><name>Darc</name></author>
	</entry>
	<entry>
		<id>https://dreamcast.wiki/wiki/index.php?title=Getting_Started_with_Dreamcast_development&amp;diff=3796</id>
		<title>Getting Started with Dreamcast development</title>
		<link rel="alternate" type="text/html" href="https://dreamcast.wiki/wiki/index.php?title=Getting_Started_with_Dreamcast_development&amp;diff=3796"/>
		<updated>2025-08-04T19:17:37Z</updated>

		<summary type="html">&lt;p&gt;Darc: /* Cleaning up temporary files */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;div style=&amp;quot;float:right;&amp;quot;&amp;gt;__TOC__&amp;lt;/div&amp;gt;&lt;br /&gt;
=Introduction=&lt;br /&gt;
This article will cover the entire beginning process: starting from zero to having a working dev environment with debug link (serial or IP) and self-booting CD-R. This guide will cover the process for the following platforms:&lt;br /&gt;
* &#039;&#039;&#039;Microsoft Windows 10&#039;&#039;&#039; via [https://learn.microsoft.com/en-us/windows/wsl/about Windows Subsystem for Linux]&lt;br /&gt;
** Users desiring a native Windows approach, see [[DreamSDK]] instead&lt;br /&gt;
* &#039;&#039;&#039;macOS&#039;&#039;&#039; on Intel or Apple Silicon systems with the [https://brew.sh/ Homebrew] package manager installed&lt;br /&gt;
* &#039;&#039;&#039;Linux&#039;&#039;&#039;-based systems&lt;br /&gt;
** &#039;&#039;&#039;Debian&#039;&#039;&#039;- and &#039;&#039;&#039;Ubuntu&#039;&#039;&#039;-based distributions using the default &#039;&#039;&#039;apt&#039;&#039;&#039; package manger&lt;br /&gt;
** &#039;&#039;&#039;Fedora&#039;&#039;&#039;-based distributions using the default &#039;&#039;&#039;dnf&#039;&#039;&#039; package manager&lt;br /&gt;
** &#039;&#039;&#039;Arch&#039;&#039;&#039;-based distributions using the default &#039;&#039;&#039;pacman&#039;&#039;&#039; package manager&lt;br /&gt;
** &#039;&#039;&#039;Alpine&#039;&#039;&#039;-based distributions using the default &#039;&#039;&#039;apk&#039;&#039;&#039; package manager&lt;br /&gt;
&lt;br /&gt;
===Need help?===&lt;br /&gt;
Important note: &#039;&#039;This guide aims to remain up to date and work on all of the above platforms, but keeping instructions for such a variety of platforms up-to-date can be difficult. If you run into any errors or other challenges while following this tutorial, or simply need clarification on any of the steps, feel free to ask for assistance on the [https://dcemulation.org/phpBB/viewforum.php?f=29 message board] or [https://discord.gg/wDEfkUh63h Discord chat] and we would be happy to aid you and update the guide for the benefit of future readers and others in the community.&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
===Terms===&lt;br /&gt;
Before we get started, let&#039;s define several terms:&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;&#039;toolchain&#039;&#039;&#039; is a set of programs which turns your code into an executable file for your Dreamcast console. The toolchain includes:&lt;br /&gt;
* &#039;&#039;&#039;GCC&#039;&#039;&#039;, a compiler which supports C, C++, Objective-C, and more (see also [[Rust on Dreamcast]] and [[Programming language support]])&lt;br /&gt;
* &#039;&#039;&#039;binutils&#039;&#039;&#039;, an assembler and linker&lt;br /&gt;
* &#039;&#039;&#039;newlib&#039;&#039;&#039;, a C library&lt;br /&gt;
* &#039;&#039;&#039;gdb&#039;&#039;&#039;, a debugger&lt;br /&gt;
The toolchain includes a compiler for the Dreamcast&#039;s main SH4 CPU, and optionally a compiler for the ARM-based AICA sound processor. Your operating system may already have versions of these programs installed to compile code for your computer, but we will need to build a &amp;quot;cross-compiler&amp;quot; for compiling specifically for the Dreamcast. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;KallistiOS&#039;&#039;&#039; or &#039;&#039;KOS&#039;&#039; is an open source development library and pseudo-operating system for the Dreamcast console. It is the best documented and most widely used development kit in the homebrew community. KallistiOS&#039;s very flexible license allows both homebrew and commercial use with no restrictions other than a requirement to include credit for its use in your project, and indeed almost all commercially sold indie Dreamcast titles use it. There are others in existence, like [[libronin]] and [[libdream]], as well as the older development kits [[Katana]] and [[Windows CE]] created by Sega and Microsoft for use in retail games, but this guide will only cover the setup and use of KallistiOS. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;kos-ports&#039;&#039;&#039; is a repository including various libraries which integrate with KallistiOS. We will download and compile these libraries as well. &lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;&#039;debug link&#039;&#039;&#039; is a generic term referring to a hardware accessory to facilitate quickly running and debugging your programs. IP-based links include the Dreamcast&#039;s &#039;&#039;&#039;[[Broadband adapter]]&#039;&#039;&#039; and &#039;&#039;&#039;[[LAN adapter]]&#039;&#039;&#039; accessories, and serial-based links include the [[Coder&#039;s cable]], which is a cable that can connect the Dreamcast&#039;s serial port to your computer via USB or serial. This guide includes instructions for setting up and using the the Broadband adapter and a USB-based coder&#039;s cable.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;dc-tool&#039;&#039;&#039; and &#039;&#039;&#039;dcload&#039;&#039;&#039; are a pair of programs to facilitate using a debug link. &#039;&#039;dc-tool&#039;&#039; runs on your computer and links to a Dreamcast running &#039;&#039;dcload-ip&#039;&#039; or &#039;&#039;dcload-serial&#039;&#039;. With this setup, you can quickly load programs, read console feedback, load assets, transfer data, redirect I/O, handle exceptions, debug problems, and so forth.&lt;br /&gt;
&lt;br /&gt;
=Choosing a debug link solution=&lt;br /&gt;
If you are building the toolchain for the purpose of building existing programs from source with little to no modifications, then a debug link setup might not be necessary for you. You may simply build programs to burn directly to CD-R. However, if you are planning to actively develop for the Dreamcast, then a debug link is a critical component. While Dreamcast emulators are mature and accurate enough to play the vast majority of the system&#039;s games library without issue, many critical bugs may show up on a real Dreamcast system, but not on a Dreamcast emulator. Therefore, it is highly recommended to test on a real system as much as possible. It&#039;s also possible to load software off of a [[Serial SD card adapter]], but without an active link to a computer, debugging and stepping through programs as they execute is significantly more challenging.&lt;br /&gt;
&lt;br /&gt;
Presented below is a table comparing the different options available for a debug link. Due to the cost, potential buyers may want to factor in the ability to play multiplayer games with their purchase. Thus, for comparison, we have included information about the [[Modem]] with [[DreamPi]] as well, but understand that the Modem with DreamPi cannot be used as a debug link.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
!colspan=&amp;quot;6&amp;quot; |Comparison of various Dreamcast connectivity options&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#c0c0c0;&amp;quot; width=&amp;quot;150&amp;quot; | Device: &lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; width=&amp;quot;400&amp;quot; | [[Broadband adapter]] (HIT-400 or HIT-401) &amp;lt;br /&amp;gt;Realtek RTL8139C chipset&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; width=&amp;quot;400&amp;quot; | [[LAN adapter]] (HIT-300) &amp;lt;br /&amp;gt;Fujitsu MB86967 chipset&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; width=&amp;quot;400&amp;quot; | [[Modem]] with [[DreamPi]]&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; width=&amp;quot;400&amp;quot; | USB [[Coder&#039;s cable]]&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; width=&amp;quot;400&amp;quot; | Serial [[Coder&#039;s cable]]&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Useful for dev? || Yes, supports dcload-ip || Yes, supports dcload-ip,&amp;lt;br/&amp;gt;but BBA is superior and cheaper || No, only useful for online multiplayer gaming || Yes, supports dcload-serial || Yes, supports dcload-serial&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Cost || $100 - $200 and up on used markets || $200 and up on used markets,&amp;lt;br/&amp;gt;due to extreme rarity || Kit prices vary, around $100 || See [[Coder&#039;s cable]] for vendors&amp;lt;br /&amp;gt;darc sells for $45, RetroOnyx sells for $85 || Varies on used markets, uncommonly sold&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Can make DIY? || No || No || Yes || Yes || Yes&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Performance || Up to 100 megabits/s || Up to 10 megabits/s || Up to 56 kilobits/s || Up to 1500 kilobits/s || Up to 120 kilobits/s&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Games support || Some games: Phantasy Star Online, Quake III Arena, Toy Racer, POD SpeedZone, Propellor Arena, Unreal Tournament&amp;lt;br /&amp;gt;Some browsers: Broadband Passport, PlanetWeb 3.0 || No games&amp;lt;br /&amp;gt;One browser: Dream Passport for LAN || All multiplayer games with network support&amp;lt;br /&amp;gt;All web browsers || NO multiplayer games support || NO multiplayer games support &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Homebrew support || Homebrew utilities like dcload-ip || Homebrew utilities like dcload-ip || Homebrew utilities don&#039;t support, only multiplayer games || Homebrew utilities like dcload-serial || Homebrew utilities like dcload-serial &lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=Setting up and compiling the toolchain with the dc-chain script=&lt;br /&gt;
===Dependencies===&lt;br /&gt;
First, we&#039;ll need to install dependencies before building the toolchain. Below we have provided commands to install these dependencies on various systems. Many of the packages will likely already be installed on your system, but we have provided an exhaustive list for good measure.&lt;br /&gt;
====macOS 13 Ventura on an Intel or Apple Silicon processor====&lt;br /&gt;
First, make sure you install Apple Xcode, including the Command Line tools. You will also need to install several other packages for which we&#039;ll include instructions assuming you have installed the [https://brew.sh/ Homebrew] package manager on your system.&lt;br /&gt;
 brew install wget gettext texinfo gmp mpfr libmpc libelf jpeg-turbo libpng cmake meson libisofs&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;Important Note for Apple Silicon users&#039;&#039;: On Apple Silicon, Homebrew installs libraries to a path not included by default by the compiler. If you haven&#039;t added these to your &#039;&#039;&#039;~/.zprofile&#039;&#039;&#039;, then add the following lines now and reload your session (or run them in your Terminal session whenever you compile KOS):&lt;br /&gt;
 export CPATH=/opt/homebrew/include&lt;br /&gt;
 export LIBRARY_PATH=/opt/homebrew/lib&lt;br /&gt;
&lt;br /&gt;
====Debian/Ubuntu-based Linux====&lt;br /&gt;
 sudo apt-get update&lt;br /&gt;
 sudo apt install gawk patch bzip2 tar make libgmp-dev libmpfr-dev libmpc-dev gettext wget libelf-dev texinfo bison flex sed git build-essential diffutils curl libjpeg-dev libpng-dev python3 pkg-config cmake libisofs-dev meson ninja-build rake cmake&lt;br /&gt;
&lt;br /&gt;
====Fedora-based Linux====&lt;br /&gt;
 sudo dnf install gawk patch bzip2 tar make gmp-devel mpfr-devel libmpc-devel gettext wget elfutils-libelf-devel texinfo bison flex sed git diffutils curl libjpeg-turbo-devel libpng-devel gcc-c++ python3 rubygem-rake meson ninja-build cmake&lt;br /&gt;
&lt;br /&gt;
====Arch-based Linux====&lt;br /&gt;
 sudo pacman -S --needed gawk patch bzip2 tar make gmp mpfr libmpc gettext wget libelf texinfo bison flex sed git diffutils curl libjpeg-turbo libpng python3 meson ruby-rake cmake&lt;br /&gt;
&lt;br /&gt;
====Alpine-based Linux====&lt;br /&gt;
 sudo apk --update add build-base patch bash texinfo gmp-dev mpfr-dev libjpeg-turbo-dev libpng-dev elfutils-dev curl wget python3 git ruby-rake cmake fmt&lt;br /&gt;
&lt;br /&gt;
====Other Linux distributions====&lt;br /&gt;
If you&#039;re using a different Linux- or Unix-based system besides the one above, you may need to reference your distribution&#039;s package database and package manager documentation for the equivalent package names and commands necessary for your system.&lt;br /&gt;
&lt;br /&gt;
===Creating a space for your toolchain installation===&lt;br /&gt;
Create the path where we&#039;ll install the toolchain and KOS, and grant it the proper permissions:&lt;br /&gt;
 sudo mkdir -p /opt/toolchains/dc&lt;br /&gt;
 sudo chmod -R 755 /opt/toolchains/dc&lt;br /&gt;
 sudo chown -R $(id -u):$(id -g) /opt/toolchains/dc&lt;br /&gt;
===Cloning the KallistiOS git repository===&lt;br /&gt;
Next, clone the git repository for the latest &#039;&#039;&#039;2.2.0&#039;&#039;&#039; stable version of KallistiOS: &lt;br /&gt;
 git clone https://github.com/KallistiOS/KallistiOS.git -b v2.2.0 /opt/toolchains/dc/kos&lt;br /&gt;
&lt;br /&gt;
Alternatively, you may clone the latest unstable version, which contains the latest changes, but may also contain bugs or issues:&lt;br /&gt;
 git clone https://github.com/KallistiOS/KallistiOS.git -b master /opt/toolchains/dc/kos&lt;br /&gt;
&lt;br /&gt;
===Configuring the dc-chain script===&lt;br /&gt;
Enter the dc-chain directory:&lt;br /&gt;
 cd /opt/toolchains/dc/kos/utils/dc-chain&lt;br /&gt;
&lt;br /&gt;
You may now configure the options to your liking by creating a &amp;lt;code&amp;gt;Makefile.cfg&amp;lt;/code&amp;gt; file in this directory. Copy or rename the &amp;lt;code&amp;gt;Makefile.default.cfg&amp;lt;/code&amp;gt; (KOS 2.2.0 stable) or &amp;lt;code&amp;gt;Makefile.dreamcast.cfg&amp;lt;/code&amp;gt; (KOS master unstable) file to &amp;lt;code&amp;gt;Makefile.cfg&amp;lt;/code&amp;gt; and then use a text editor to edit this file&#039;s settings to your liking. For example, you may like to alter the &amp;lt;code&amp;gt;makejobs&amp;lt;/code&amp;gt; option to the number of threads available on your CPU to speed up the compilation. However, if you run into errors during compilation, you may want to set &amp;lt;code&amp;gt;makejobs=1&amp;lt;/code&amp;gt;, as on some operating systems the toolchain may fail to build with a higher setting.&lt;br /&gt;
&lt;br /&gt;
We will leave the default &#039;&#039;&#039;stable&#039;&#039;&#039; profile for the toolchain, which currently uses GCC 13.2. For advanced users, other profiles are available to you; read the &amp;lt;code&amp;gt;README.md&amp;lt;/code&amp;gt; file in the dc-chain directory for more information if you are interested. If you are unsure about options in the &amp;lt;code&amp;gt;Makefile.cfg&amp;lt;/code&amp;gt;, just leave the defaults alone.&lt;br /&gt;
&lt;br /&gt;
===Downloading and compiling the toolchain===&lt;br /&gt;
Now we will run a script to download files and compile the toolchain. Simply run:&lt;br /&gt;
 make&lt;br /&gt;
&lt;br /&gt;
This will download and unpack the relevant necessary files and then begin the compilation process. The compilation can take anywhere from minutes to a few hours depending on your CPU and number of threads available. When successfully finished, the toolchains will be ready. If you used the command above, the &#039;&#039;&#039;gdb&#039;&#039;&#039; debugger will have been built as well.&lt;br /&gt;
&lt;br /&gt;
===Cleaning up temporary files===&lt;br /&gt;
After building everything, you can clean up the extraneous files in your dc-chain directory by entering:&lt;br /&gt;
 make clean distclean&lt;br /&gt;
&lt;br /&gt;
=Configuring and compiling KOS and kos-ports=&lt;br /&gt;
===Setting up the environment settings===&lt;br /&gt;
Enter the KOS directory:&lt;br /&gt;
 cd /opt/toolchains/dc/kos&lt;br /&gt;
Copy the pre-made environment script into place:&lt;br /&gt;
 cp doc/environ.sh.sample environ.sh &lt;br /&gt;
For most users, the default settings will suffice. However, advanced users may edit the environ.sh to your liking if you&#039;d like to change compile flags or alter paths. If you&#039;d like to have multiple KOS versions installed or multiple toolchain versions installed, you can set up different environ.sh files corresponding to these different configurations by altering the paths.&lt;br /&gt;
&lt;br /&gt;
You will need to run the following command to apply the KOS environment settings to your currently running shell. Run the following command now, &#039;&#039;&#039;and&#039;&#039;&#039; &#039;&#039;whenever&#039;&#039; you open a new shell to work on Dreamcast projects:&lt;br /&gt;
 . /opt/toolchains/dc/kos/environ.sh&lt;br /&gt;
&lt;br /&gt;
===Building KOS===&lt;br /&gt;
Build KOS:&lt;br /&gt;
 make&lt;br /&gt;
KOS is now built.&lt;br /&gt;
===Building kos-ports===&lt;br /&gt;
Clone the kos-ports repository to your system:&lt;br /&gt;
 git clone --recursive https://github.com/KallistiOS/kos-ports /opt/toolchains/dc/kos-ports&lt;br /&gt;
Run the script to build all of the included ports:&lt;br /&gt;
 /opt/toolchains/dc/kos-ports/utils/build-all.sh&lt;br /&gt;
kos-ports is now built.&lt;br /&gt;
&lt;br /&gt;
===Building the KOS examples===&lt;br /&gt;
Enter the KOS examples directory:&lt;br /&gt;
 cd /opt/toolchains/dc/kos/examples&lt;br /&gt;
Build the examples:&lt;br /&gt;
 make&lt;br /&gt;
All of the example programs provided with KallistiOS are now built.&lt;br /&gt;
&lt;br /&gt;
=Further=&lt;br /&gt;
At this point, you will have a cross-compiler built so that you can generate programs for your Dreamcast from code. KallistiOS, the Dreamcast development library and operating system, is built, as is its associated packages within kos-ports, and all of its example programs.&lt;br /&gt;
&lt;br /&gt;
At this point, you will likely want to view some of the following articles:&lt;br /&gt;
* Setting up an IDE for Dreamcast development&lt;br /&gt;
** [[CLion Debugging]]&lt;br /&gt;
** [[Visual Studio Code]]&lt;br /&gt;
* [[Creating a first project with KallistiOS]] (&#039;&#039;&#039;TODO&#039;&#039;&#039;: Explain how to create a new DC project folder with Makefile, adding an external library, create a basic program, and compile it)&lt;br /&gt;
* [[Using a debug link]]&lt;br /&gt;
* [[Burning an example to CD-R]]&lt;br /&gt;
&lt;br /&gt;
=Running an example program through a debug link=&lt;br /&gt;
&#039;&#039;&#039;TODO&#039;&#039;&#039;: &#039;&#039;Give a tutorial on compiling dcload/dc-tool, setting up a serial, USB, or IP debug link, and running 2ndmix demo on real hardware.&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
On your development machine, setup [[dcload-ip]]:&lt;br /&gt;
* [[Using dcload-ip with Linux]]&lt;br /&gt;
* [[Using dcload-ip with Windows Subsystem for Linux]]&lt;br /&gt;
&lt;br /&gt;
For the Dreamcast, download and burn the [[:File:Dcload-2023-06-22.zip|latest version of dcload-ip or dcload-serial]] to a CD-R -- the IP version includes improved DHCP support, so there is no longer a need to configure things beforehand. &lt;br /&gt;
 &lt;br /&gt;
Run one of the examples from the &amp;lt;code&amp;gt;kos/examples/dreamcast&amp;lt;/code&amp;gt; directory with one of the following commands:&lt;br /&gt;
 dc-tool-ip -t &amp;lt;dreamcast IP address&amp;gt; -x example.elf&lt;br /&gt;
&lt;br /&gt;
 dc-tool-ser -t &amp;lt;serial device path&amp;gt; -x example.elf&lt;br /&gt;
&lt;br /&gt;
Run &amp;lt;code&amp;gt;dc-tool-ip&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;dc-tool-ser&amp;lt;/code&amp;gt; without any parameters to get additional options.&lt;br /&gt;
&lt;br /&gt;
=Burning an example program to CD-R=&lt;br /&gt;
&#039;&#039;&#039;TODO&#039;&#039;&#039;: &#039;&#039;Explain how to build mkdcdisc and write 2ndmix demo to CD-R and run on a Dreamcast console&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[https://gitlab.com/simulant/mkdcdisc mkdcdisc] can be used to easily generate a burnable self-boot CDI image.&lt;br /&gt;
Dependencies required:&lt;br /&gt;
* Homebrew: &amp;lt;code&amp;gt;meson libisofs pkg-config&amp;lt;/code&amp;gt;&lt;br /&gt;
* Debian/Ubuntu: &amp;lt;code&amp;gt;libisofs-dev meson ninja-build&amp;lt;/code&amp;gt;&lt;br /&gt;
* Fedora: &amp;lt;code&amp;gt;meson ninja-build&amp;lt;/code&amp;gt;&lt;br /&gt;
* Arch: &amp;lt;code&amp;gt;meson&amp;lt;/code&amp;gt;&lt;br /&gt;
* Alpine: &amp;lt;code&amp;gt;meson libisofs-dev&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Build &amp;lt;code&amp;gt;mkdcdisc&amp;lt;/code&amp;gt;:&lt;br /&gt;
 git clone https://gitlab.com/simulant/mkdcdisc.git&lt;br /&gt;
 cd mkdcdisc&lt;br /&gt;
 meson setup builddir&lt;br /&gt;
 meson compile -C builddir&lt;br /&gt;
 ./builddir/mkdcdisc -h&lt;br /&gt;
and create a CDI image from your compiled ELF like so:&lt;br /&gt;
 mkdcdisc -e MyProgram.elf -o MyProgram.cdi&lt;br /&gt;
Then you can burn the CDI file using DiscJuggler (Windows-only, but also works through [https://www.winehq.org/ WINE]), ImgBurn with the CDI plugin, or the cdiburn *nix script floating around out there. (document this better)&lt;/div&gt;</summary>
		<author><name>Darc</name></author>
	</entry>
	<entry>
		<id>https://dreamcast.wiki/wiki/index.php?title=Getting_Started_with_Dreamcast_development&amp;diff=3795</id>
		<title>Getting Started with Dreamcast development</title>
		<link rel="alternate" type="text/html" href="https://dreamcast.wiki/wiki/index.php?title=Getting_Started_with_Dreamcast_development&amp;diff=3795"/>
		<updated>2025-08-04T19:17:18Z</updated>

		<summary type="html">&lt;p&gt;Darc: /* Downloading and compiling the toolchain */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;div style=&amp;quot;float:right;&amp;quot;&amp;gt;__TOC__&amp;lt;/div&amp;gt;&lt;br /&gt;
=Introduction=&lt;br /&gt;
This article will cover the entire beginning process: starting from zero to having a working dev environment with debug link (serial or IP) and self-booting CD-R. This guide will cover the process for the following platforms:&lt;br /&gt;
* &#039;&#039;&#039;Microsoft Windows 10&#039;&#039;&#039; via [https://learn.microsoft.com/en-us/windows/wsl/about Windows Subsystem for Linux]&lt;br /&gt;
** Users desiring a native Windows approach, see [[DreamSDK]] instead&lt;br /&gt;
* &#039;&#039;&#039;macOS&#039;&#039;&#039; on Intel or Apple Silicon systems with the [https://brew.sh/ Homebrew] package manager installed&lt;br /&gt;
* &#039;&#039;&#039;Linux&#039;&#039;&#039;-based systems&lt;br /&gt;
** &#039;&#039;&#039;Debian&#039;&#039;&#039;- and &#039;&#039;&#039;Ubuntu&#039;&#039;&#039;-based distributions using the default &#039;&#039;&#039;apt&#039;&#039;&#039; package manger&lt;br /&gt;
** &#039;&#039;&#039;Fedora&#039;&#039;&#039;-based distributions using the default &#039;&#039;&#039;dnf&#039;&#039;&#039; package manager&lt;br /&gt;
** &#039;&#039;&#039;Arch&#039;&#039;&#039;-based distributions using the default &#039;&#039;&#039;pacman&#039;&#039;&#039; package manager&lt;br /&gt;
** &#039;&#039;&#039;Alpine&#039;&#039;&#039;-based distributions using the default &#039;&#039;&#039;apk&#039;&#039;&#039; package manager&lt;br /&gt;
&lt;br /&gt;
===Need help?===&lt;br /&gt;
Important note: &#039;&#039;This guide aims to remain up to date and work on all of the above platforms, but keeping instructions for such a variety of platforms up-to-date can be difficult. If you run into any errors or other challenges while following this tutorial, or simply need clarification on any of the steps, feel free to ask for assistance on the [https://dcemulation.org/phpBB/viewforum.php?f=29 message board] or [https://discord.gg/wDEfkUh63h Discord chat] and we would be happy to aid you and update the guide for the benefit of future readers and others in the community.&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
===Terms===&lt;br /&gt;
Before we get started, let&#039;s define several terms:&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;&#039;toolchain&#039;&#039;&#039; is a set of programs which turns your code into an executable file for your Dreamcast console. The toolchain includes:&lt;br /&gt;
* &#039;&#039;&#039;GCC&#039;&#039;&#039;, a compiler which supports C, C++, Objective-C, and more (see also [[Rust on Dreamcast]] and [[Programming language support]])&lt;br /&gt;
* &#039;&#039;&#039;binutils&#039;&#039;&#039;, an assembler and linker&lt;br /&gt;
* &#039;&#039;&#039;newlib&#039;&#039;&#039;, a C library&lt;br /&gt;
* &#039;&#039;&#039;gdb&#039;&#039;&#039;, a debugger&lt;br /&gt;
The toolchain includes a compiler for the Dreamcast&#039;s main SH4 CPU, and optionally a compiler for the ARM-based AICA sound processor. Your operating system may already have versions of these programs installed to compile code for your computer, but we will need to build a &amp;quot;cross-compiler&amp;quot; for compiling specifically for the Dreamcast. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;KallistiOS&#039;&#039;&#039; or &#039;&#039;KOS&#039;&#039; is an open source development library and pseudo-operating system for the Dreamcast console. It is the best documented and most widely used development kit in the homebrew community. KallistiOS&#039;s very flexible license allows both homebrew and commercial use with no restrictions other than a requirement to include credit for its use in your project, and indeed almost all commercially sold indie Dreamcast titles use it. There are others in existence, like [[libronin]] and [[libdream]], as well as the older development kits [[Katana]] and [[Windows CE]] created by Sega and Microsoft for use in retail games, but this guide will only cover the setup and use of KallistiOS. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;kos-ports&#039;&#039;&#039; is a repository including various libraries which integrate with KallistiOS. We will download and compile these libraries as well. &lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;&#039;debug link&#039;&#039;&#039; is a generic term referring to a hardware accessory to facilitate quickly running and debugging your programs. IP-based links include the Dreamcast&#039;s &#039;&#039;&#039;[[Broadband adapter]]&#039;&#039;&#039; and &#039;&#039;&#039;[[LAN adapter]]&#039;&#039;&#039; accessories, and serial-based links include the [[Coder&#039;s cable]], which is a cable that can connect the Dreamcast&#039;s serial port to your computer via USB or serial. This guide includes instructions for setting up and using the the Broadband adapter and a USB-based coder&#039;s cable.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;dc-tool&#039;&#039;&#039; and &#039;&#039;&#039;dcload&#039;&#039;&#039; are a pair of programs to facilitate using a debug link. &#039;&#039;dc-tool&#039;&#039; runs on your computer and links to a Dreamcast running &#039;&#039;dcload-ip&#039;&#039; or &#039;&#039;dcload-serial&#039;&#039;. With this setup, you can quickly load programs, read console feedback, load assets, transfer data, redirect I/O, handle exceptions, debug problems, and so forth.&lt;br /&gt;
&lt;br /&gt;
=Choosing a debug link solution=&lt;br /&gt;
If you are building the toolchain for the purpose of building existing programs from source with little to no modifications, then a debug link setup might not be necessary for you. You may simply build programs to burn directly to CD-R. However, if you are planning to actively develop for the Dreamcast, then a debug link is a critical component. While Dreamcast emulators are mature and accurate enough to play the vast majority of the system&#039;s games library without issue, many critical bugs may show up on a real Dreamcast system, but not on a Dreamcast emulator. Therefore, it is highly recommended to test on a real system as much as possible. It&#039;s also possible to load software off of a [[Serial SD card adapter]], but without an active link to a computer, debugging and stepping through programs as they execute is significantly more challenging.&lt;br /&gt;
&lt;br /&gt;
Presented below is a table comparing the different options available for a debug link. Due to the cost, potential buyers may want to factor in the ability to play multiplayer games with their purchase. Thus, for comparison, we have included information about the [[Modem]] with [[DreamPi]] as well, but understand that the Modem with DreamPi cannot be used as a debug link.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
!colspan=&amp;quot;6&amp;quot; |Comparison of various Dreamcast connectivity options&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#c0c0c0;&amp;quot; width=&amp;quot;150&amp;quot; | Device: &lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; width=&amp;quot;400&amp;quot; | [[Broadband adapter]] (HIT-400 or HIT-401) &amp;lt;br /&amp;gt;Realtek RTL8139C chipset&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; width=&amp;quot;400&amp;quot; | [[LAN adapter]] (HIT-300) &amp;lt;br /&amp;gt;Fujitsu MB86967 chipset&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; width=&amp;quot;400&amp;quot; | [[Modem]] with [[DreamPi]]&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; width=&amp;quot;400&amp;quot; | USB [[Coder&#039;s cable]]&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; width=&amp;quot;400&amp;quot; | Serial [[Coder&#039;s cable]]&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Useful for dev? || Yes, supports dcload-ip || Yes, supports dcload-ip,&amp;lt;br/&amp;gt;but BBA is superior and cheaper || No, only useful for online multiplayer gaming || Yes, supports dcload-serial || Yes, supports dcload-serial&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Cost || $100 - $200 and up on used markets || $200 and up on used markets,&amp;lt;br/&amp;gt;due to extreme rarity || Kit prices vary, around $100 || See [[Coder&#039;s cable]] for vendors&amp;lt;br /&amp;gt;darc sells for $45, RetroOnyx sells for $85 || Varies on used markets, uncommonly sold&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Can make DIY? || No || No || Yes || Yes || Yes&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Performance || Up to 100 megabits/s || Up to 10 megabits/s || Up to 56 kilobits/s || Up to 1500 kilobits/s || Up to 120 kilobits/s&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Games support || Some games: Phantasy Star Online, Quake III Arena, Toy Racer, POD SpeedZone, Propellor Arena, Unreal Tournament&amp;lt;br /&amp;gt;Some browsers: Broadband Passport, PlanetWeb 3.0 || No games&amp;lt;br /&amp;gt;One browser: Dream Passport for LAN || All multiplayer games with network support&amp;lt;br /&amp;gt;All web browsers || NO multiplayer games support || NO multiplayer games support &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Homebrew support || Homebrew utilities like dcload-ip || Homebrew utilities like dcload-ip || Homebrew utilities don&#039;t support, only multiplayer games || Homebrew utilities like dcload-serial || Homebrew utilities like dcload-serial &lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=Setting up and compiling the toolchain with the dc-chain script=&lt;br /&gt;
===Dependencies===&lt;br /&gt;
First, we&#039;ll need to install dependencies before building the toolchain. Below we have provided commands to install these dependencies on various systems. Many of the packages will likely already be installed on your system, but we have provided an exhaustive list for good measure.&lt;br /&gt;
====macOS 13 Ventura on an Intel or Apple Silicon processor====&lt;br /&gt;
First, make sure you install Apple Xcode, including the Command Line tools. You will also need to install several other packages for which we&#039;ll include instructions assuming you have installed the [https://brew.sh/ Homebrew] package manager on your system.&lt;br /&gt;
 brew install wget gettext texinfo gmp mpfr libmpc libelf jpeg-turbo libpng cmake meson libisofs&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;Important Note for Apple Silicon users&#039;&#039;: On Apple Silicon, Homebrew installs libraries to a path not included by default by the compiler. If you haven&#039;t added these to your &#039;&#039;&#039;~/.zprofile&#039;&#039;&#039;, then add the following lines now and reload your session (or run them in your Terminal session whenever you compile KOS):&lt;br /&gt;
 export CPATH=/opt/homebrew/include&lt;br /&gt;
 export LIBRARY_PATH=/opt/homebrew/lib&lt;br /&gt;
&lt;br /&gt;
====Debian/Ubuntu-based Linux====&lt;br /&gt;
 sudo apt-get update&lt;br /&gt;
 sudo apt install gawk patch bzip2 tar make libgmp-dev libmpfr-dev libmpc-dev gettext wget libelf-dev texinfo bison flex sed git build-essential diffutils curl libjpeg-dev libpng-dev python3 pkg-config cmake libisofs-dev meson ninja-build rake cmake&lt;br /&gt;
&lt;br /&gt;
====Fedora-based Linux====&lt;br /&gt;
 sudo dnf install gawk patch bzip2 tar make gmp-devel mpfr-devel libmpc-devel gettext wget elfutils-libelf-devel texinfo bison flex sed git diffutils curl libjpeg-turbo-devel libpng-devel gcc-c++ python3 rubygem-rake meson ninja-build cmake&lt;br /&gt;
&lt;br /&gt;
====Arch-based Linux====&lt;br /&gt;
 sudo pacman -S --needed gawk patch bzip2 tar make gmp mpfr libmpc gettext wget libelf texinfo bison flex sed git diffutils curl libjpeg-turbo libpng python3 meson ruby-rake cmake&lt;br /&gt;
&lt;br /&gt;
====Alpine-based Linux====&lt;br /&gt;
 sudo apk --update add build-base patch bash texinfo gmp-dev mpfr-dev libjpeg-turbo-dev libpng-dev elfutils-dev curl wget python3 git ruby-rake cmake fmt&lt;br /&gt;
&lt;br /&gt;
====Other Linux distributions====&lt;br /&gt;
If you&#039;re using a different Linux- or Unix-based system besides the one above, you may need to reference your distribution&#039;s package database and package manager documentation for the equivalent package names and commands necessary for your system.&lt;br /&gt;
&lt;br /&gt;
===Creating a space for your toolchain installation===&lt;br /&gt;
Create the path where we&#039;ll install the toolchain and KOS, and grant it the proper permissions:&lt;br /&gt;
 sudo mkdir -p /opt/toolchains/dc&lt;br /&gt;
 sudo chmod -R 755 /opt/toolchains/dc&lt;br /&gt;
 sudo chown -R $(id -u):$(id -g) /opt/toolchains/dc&lt;br /&gt;
===Cloning the KallistiOS git repository===&lt;br /&gt;
Next, clone the git repository for the latest &#039;&#039;&#039;2.2.0&#039;&#039;&#039; stable version of KallistiOS: &lt;br /&gt;
 git clone https://github.com/KallistiOS/KallistiOS.git -b v2.2.0 /opt/toolchains/dc/kos&lt;br /&gt;
&lt;br /&gt;
Alternatively, you may clone the latest unstable version, which contains the latest changes, but may also contain bugs or issues:&lt;br /&gt;
 git clone https://github.com/KallistiOS/KallistiOS.git -b master /opt/toolchains/dc/kos&lt;br /&gt;
&lt;br /&gt;
===Configuring the dc-chain script===&lt;br /&gt;
Enter the dc-chain directory:&lt;br /&gt;
 cd /opt/toolchains/dc/kos/utils/dc-chain&lt;br /&gt;
&lt;br /&gt;
You may now configure the options to your liking by creating a &amp;lt;code&amp;gt;Makefile.cfg&amp;lt;/code&amp;gt; file in this directory. Copy or rename the &amp;lt;code&amp;gt;Makefile.default.cfg&amp;lt;/code&amp;gt; (KOS 2.2.0 stable) or &amp;lt;code&amp;gt;Makefile.dreamcast.cfg&amp;lt;/code&amp;gt; (KOS master unstable) file to &amp;lt;code&amp;gt;Makefile.cfg&amp;lt;/code&amp;gt; and then use a text editor to edit this file&#039;s settings to your liking. For example, you may like to alter the &amp;lt;code&amp;gt;makejobs&amp;lt;/code&amp;gt; option to the number of threads available on your CPU to speed up the compilation. However, if you run into errors during compilation, you may want to set &amp;lt;code&amp;gt;makejobs=1&amp;lt;/code&amp;gt;, as on some operating systems the toolchain may fail to build with a higher setting.&lt;br /&gt;
&lt;br /&gt;
We will leave the default &#039;&#039;&#039;stable&#039;&#039;&#039; profile for the toolchain, which currently uses GCC 13.2. For advanced users, other profiles are available to you; read the &amp;lt;code&amp;gt;README.md&amp;lt;/code&amp;gt; file in the dc-chain directory for more information if you are interested. If you are unsure about options in the &amp;lt;code&amp;gt;Makefile.cfg&amp;lt;/code&amp;gt;, just leave the defaults alone.&lt;br /&gt;
&lt;br /&gt;
===Downloading and compiling the toolchain===&lt;br /&gt;
Now we will run a script to download files and compile the toolchain. Simply run:&lt;br /&gt;
 make&lt;br /&gt;
&lt;br /&gt;
This will download and unpack the relevant necessary files and then begin the compilation process. The compilation can take anywhere from minutes to a few hours depending on your CPU and number of threads available. When successfully finished, the toolchains will be ready. If you used the command above, the &#039;&#039;&#039;gdb&#039;&#039;&#039; debugger will have been built as well.&lt;br /&gt;
&lt;br /&gt;
===Cleaning up temporary files===&lt;br /&gt;
After building everything, you can clean up the extraneous files in your dc-chain directory by entering:&lt;br /&gt;
 make clean&lt;br /&gt;
&lt;br /&gt;
=Configuring and compiling KOS and kos-ports=&lt;br /&gt;
===Setting up the environment settings===&lt;br /&gt;
Enter the KOS directory:&lt;br /&gt;
 cd /opt/toolchains/dc/kos&lt;br /&gt;
Copy the pre-made environment script into place:&lt;br /&gt;
 cp doc/environ.sh.sample environ.sh &lt;br /&gt;
For most users, the default settings will suffice. However, advanced users may edit the environ.sh to your liking if you&#039;d like to change compile flags or alter paths. If you&#039;d like to have multiple KOS versions installed or multiple toolchain versions installed, you can set up different environ.sh files corresponding to these different configurations by altering the paths.&lt;br /&gt;
&lt;br /&gt;
You will need to run the following command to apply the KOS environment settings to your currently running shell. Run the following command now, &#039;&#039;&#039;and&#039;&#039;&#039; &#039;&#039;whenever&#039;&#039; you open a new shell to work on Dreamcast projects:&lt;br /&gt;
 . /opt/toolchains/dc/kos/environ.sh&lt;br /&gt;
&lt;br /&gt;
===Building KOS===&lt;br /&gt;
Build KOS:&lt;br /&gt;
 make&lt;br /&gt;
KOS is now built.&lt;br /&gt;
===Building kos-ports===&lt;br /&gt;
Clone the kos-ports repository to your system:&lt;br /&gt;
 git clone --recursive https://github.com/KallistiOS/kos-ports /opt/toolchains/dc/kos-ports&lt;br /&gt;
Run the script to build all of the included ports:&lt;br /&gt;
 /opt/toolchains/dc/kos-ports/utils/build-all.sh&lt;br /&gt;
kos-ports is now built.&lt;br /&gt;
&lt;br /&gt;
===Building the KOS examples===&lt;br /&gt;
Enter the KOS examples directory:&lt;br /&gt;
 cd /opt/toolchains/dc/kos/examples&lt;br /&gt;
Build the examples:&lt;br /&gt;
 make&lt;br /&gt;
All of the example programs provided with KallistiOS are now built.&lt;br /&gt;
&lt;br /&gt;
=Further=&lt;br /&gt;
At this point, you will have a cross-compiler built so that you can generate programs for your Dreamcast from code. KallistiOS, the Dreamcast development library and operating system, is built, as is its associated packages within kos-ports, and all of its example programs.&lt;br /&gt;
&lt;br /&gt;
At this point, you will likely want to view some of the following articles:&lt;br /&gt;
* Setting up an IDE for Dreamcast development&lt;br /&gt;
** [[CLion Debugging]]&lt;br /&gt;
** [[Visual Studio Code]]&lt;br /&gt;
* [[Creating a first project with KallistiOS]] (&#039;&#039;&#039;TODO&#039;&#039;&#039;: Explain how to create a new DC project folder with Makefile, adding an external library, create a basic program, and compile it)&lt;br /&gt;
* [[Using a debug link]]&lt;br /&gt;
* [[Burning an example to CD-R]]&lt;br /&gt;
&lt;br /&gt;
=Running an example program through a debug link=&lt;br /&gt;
&#039;&#039;&#039;TODO&#039;&#039;&#039;: &#039;&#039;Give a tutorial on compiling dcload/dc-tool, setting up a serial, USB, or IP debug link, and running 2ndmix demo on real hardware.&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
On your development machine, setup [[dcload-ip]]:&lt;br /&gt;
* [[Using dcload-ip with Linux]]&lt;br /&gt;
* [[Using dcload-ip with Windows Subsystem for Linux]]&lt;br /&gt;
&lt;br /&gt;
For the Dreamcast, download and burn the [[:File:Dcload-2023-06-22.zip|latest version of dcload-ip or dcload-serial]] to a CD-R -- the IP version includes improved DHCP support, so there is no longer a need to configure things beforehand. &lt;br /&gt;
 &lt;br /&gt;
Run one of the examples from the &amp;lt;code&amp;gt;kos/examples/dreamcast&amp;lt;/code&amp;gt; directory with one of the following commands:&lt;br /&gt;
 dc-tool-ip -t &amp;lt;dreamcast IP address&amp;gt; -x example.elf&lt;br /&gt;
&lt;br /&gt;
 dc-tool-ser -t &amp;lt;serial device path&amp;gt; -x example.elf&lt;br /&gt;
&lt;br /&gt;
Run &amp;lt;code&amp;gt;dc-tool-ip&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;dc-tool-ser&amp;lt;/code&amp;gt; without any parameters to get additional options.&lt;br /&gt;
&lt;br /&gt;
=Burning an example program to CD-R=&lt;br /&gt;
&#039;&#039;&#039;TODO&#039;&#039;&#039;: &#039;&#039;Explain how to build mkdcdisc and write 2ndmix demo to CD-R and run on a Dreamcast console&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[https://gitlab.com/simulant/mkdcdisc mkdcdisc] can be used to easily generate a burnable self-boot CDI image.&lt;br /&gt;
Dependencies required:&lt;br /&gt;
* Homebrew: &amp;lt;code&amp;gt;meson libisofs pkg-config&amp;lt;/code&amp;gt;&lt;br /&gt;
* Debian/Ubuntu: &amp;lt;code&amp;gt;libisofs-dev meson ninja-build&amp;lt;/code&amp;gt;&lt;br /&gt;
* Fedora: &amp;lt;code&amp;gt;meson ninja-build&amp;lt;/code&amp;gt;&lt;br /&gt;
* Arch: &amp;lt;code&amp;gt;meson&amp;lt;/code&amp;gt;&lt;br /&gt;
* Alpine: &amp;lt;code&amp;gt;meson libisofs-dev&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Build &amp;lt;code&amp;gt;mkdcdisc&amp;lt;/code&amp;gt;:&lt;br /&gt;
 git clone https://gitlab.com/simulant/mkdcdisc.git&lt;br /&gt;
 cd mkdcdisc&lt;br /&gt;
 meson setup builddir&lt;br /&gt;
 meson compile -C builddir&lt;br /&gt;
 ./builddir/mkdcdisc -h&lt;br /&gt;
and create a CDI image from your compiled ELF like so:&lt;br /&gt;
 mkdcdisc -e MyProgram.elf -o MyProgram.cdi&lt;br /&gt;
Then you can burn the CDI file using DiscJuggler (Windows-only, but also works through [https://www.winehq.org/ WINE]), ImgBurn with the CDI plugin, or the cdiburn *nix script floating around out there. (document this better)&lt;/div&gt;</summary>
		<author><name>Darc</name></author>
	</entry>
	<entry>
		<id>https://dreamcast.wiki/wiki/index.php?title=Getting_Started_with_Dreamcast_development&amp;diff=3794</id>
		<title>Getting Started with Dreamcast development</title>
		<link rel="alternate" type="text/html" href="https://dreamcast.wiki/wiki/index.php?title=Getting_Started_with_Dreamcast_development&amp;diff=3794"/>
		<updated>2025-08-04T19:16:19Z</updated>

		<summary type="html">&lt;p&gt;Darc: /* Configuring the dc-chain script */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;div style=&amp;quot;float:right;&amp;quot;&amp;gt;__TOC__&amp;lt;/div&amp;gt;&lt;br /&gt;
=Introduction=&lt;br /&gt;
This article will cover the entire beginning process: starting from zero to having a working dev environment with debug link (serial or IP) and self-booting CD-R. This guide will cover the process for the following platforms:&lt;br /&gt;
* &#039;&#039;&#039;Microsoft Windows 10&#039;&#039;&#039; via [https://learn.microsoft.com/en-us/windows/wsl/about Windows Subsystem for Linux]&lt;br /&gt;
** Users desiring a native Windows approach, see [[DreamSDK]] instead&lt;br /&gt;
* &#039;&#039;&#039;macOS&#039;&#039;&#039; on Intel or Apple Silicon systems with the [https://brew.sh/ Homebrew] package manager installed&lt;br /&gt;
* &#039;&#039;&#039;Linux&#039;&#039;&#039;-based systems&lt;br /&gt;
** &#039;&#039;&#039;Debian&#039;&#039;&#039;- and &#039;&#039;&#039;Ubuntu&#039;&#039;&#039;-based distributions using the default &#039;&#039;&#039;apt&#039;&#039;&#039; package manger&lt;br /&gt;
** &#039;&#039;&#039;Fedora&#039;&#039;&#039;-based distributions using the default &#039;&#039;&#039;dnf&#039;&#039;&#039; package manager&lt;br /&gt;
** &#039;&#039;&#039;Arch&#039;&#039;&#039;-based distributions using the default &#039;&#039;&#039;pacman&#039;&#039;&#039; package manager&lt;br /&gt;
** &#039;&#039;&#039;Alpine&#039;&#039;&#039;-based distributions using the default &#039;&#039;&#039;apk&#039;&#039;&#039; package manager&lt;br /&gt;
&lt;br /&gt;
===Need help?===&lt;br /&gt;
Important note: &#039;&#039;This guide aims to remain up to date and work on all of the above platforms, but keeping instructions for such a variety of platforms up-to-date can be difficult. If you run into any errors or other challenges while following this tutorial, or simply need clarification on any of the steps, feel free to ask for assistance on the [https://dcemulation.org/phpBB/viewforum.php?f=29 message board] or [https://discord.gg/wDEfkUh63h Discord chat] and we would be happy to aid you and update the guide for the benefit of future readers and others in the community.&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
===Terms===&lt;br /&gt;
Before we get started, let&#039;s define several terms:&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;&#039;toolchain&#039;&#039;&#039; is a set of programs which turns your code into an executable file for your Dreamcast console. The toolchain includes:&lt;br /&gt;
* &#039;&#039;&#039;GCC&#039;&#039;&#039;, a compiler which supports C, C++, Objective-C, and more (see also [[Rust on Dreamcast]] and [[Programming language support]])&lt;br /&gt;
* &#039;&#039;&#039;binutils&#039;&#039;&#039;, an assembler and linker&lt;br /&gt;
* &#039;&#039;&#039;newlib&#039;&#039;&#039;, a C library&lt;br /&gt;
* &#039;&#039;&#039;gdb&#039;&#039;&#039;, a debugger&lt;br /&gt;
The toolchain includes a compiler for the Dreamcast&#039;s main SH4 CPU, and optionally a compiler for the ARM-based AICA sound processor. Your operating system may already have versions of these programs installed to compile code for your computer, but we will need to build a &amp;quot;cross-compiler&amp;quot; for compiling specifically for the Dreamcast. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;KallistiOS&#039;&#039;&#039; or &#039;&#039;KOS&#039;&#039; is an open source development library and pseudo-operating system for the Dreamcast console. It is the best documented and most widely used development kit in the homebrew community. KallistiOS&#039;s very flexible license allows both homebrew and commercial use with no restrictions other than a requirement to include credit for its use in your project, and indeed almost all commercially sold indie Dreamcast titles use it. There are others in existence, like [[libronin]] and [[libdream]], as well as the older development kits [[Katana]] and [[Windows CE]] created by Sega and Microsoft for use in retail games, but this guide will only cover the setup and use of KallistiOS. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;kos-ports&#039;&#039;&#039; is a repository including various libraries which integrate with KallistiOS. We will download and compile these libraries as well. &lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;&#039;debug link&#039;&#039;&#039; is a generic term referring to a hardware accessory to facilitate quickly running and debugging your programs. IP-based links include the Dreamcast&#039;s &#039;&#039;&#039;[[Broadband adapter]]&#039;&#039;&#039; and &#039;&#039;&#039;[[LAN adapter]]&#039;&#039;&#039; accessories, and serial-based links include the [[Coder&#039;s cable]], which is a cable that can connect the Dreamcast&#039;s serial port to your computer via USB or serial. This guide includes instructions for setting up and using the the Broadband adapter and a USB-based coder&#039;s cable.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;dc-tool&#039;&#039;&#039; and &#039;&#039;&#039;dcload&#039;&#039;&#039; are a pair of programs to facilitate using a debug link. &#039;&#039;dc-tool&#039;&#039; runs on your computer and links to a Dreamcast running &#039;&#039;dcload-ip&#039;&#039; or &#039;&#039;dcload-serial&#039;&#039;. With this setup, you can quickly load programs, read console feedback, load assets, transfer data, redirect I/O, handle exceptions, debug problems, and so forth.&lt;br /&gt;
&lt;br /&gt;
=Choosing a debug link solution=&lt;br /&gt;
If you are building the toolchain for the purpose of building existing programs from source with little to no modifications, then a debug link setup might not be necessary for you. You may simply build programs to burn directly to CD-R. However, if you are planning to actively develop for the Dreamcast, then a debug link is a critical component. While Dreamcast emulators are mature and accurate enough to play the vast majority of the system&#039;s games library without issue, many critical bugs may show up on a real Dreamcast system, but not on a Dreamcast emulator. Therefore, it is highly recommended to test on a real system as much as possible. It&#039;s also possible to load software off of a [[Serial SD card adapter]], but without an active link to a computer, debugging and stepping through programs as they execute is significantly more challenging.&lt;br /&gt;
&lt;br /&gt;
Presented below is a table comparing the different options available for a debug link. Due to the cost, potential buyers may want to factor in the ability to play multiplayer games with their purchase. Thus, for comparison, we have included information about the [[Modem]] with [[DreamPi]] as well, but understand that the Modem with DreamPi cannot be used as a debug link.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
!colspan=&amp;quot;6&amp;quot; |Comparison of various Dreamcast connectivity options&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#c0c0c0;&amp;quot; width=&amp;quot;150&amp;quot; | Device: &lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; width=&amp;quot;400&amp;quot; | [[Broadband adapter]] (HIT-400 or HIT-401) &amp;lt;br /&amp;gt;Realtek RTL8139C chipset&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; width=&amp;quot;400&amp;quot; | [[LAN adapter]] (HIT-300) &amp;lt;br /&amp;gt;Fujitsu MB86967 chipset&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; width=&amp;quot;400&amp;quot; | [[Modem]] with [[DreamPi]]&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; width=&amp;quot;400&amp;quot; | USB [[Coder&#039;s cable]]&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; width=&amp;quot;400&amp;quot; | Serial [[Coder&#039;s cable]]&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Useful for dev? || Yes, supports dcload-ip || Yes, supports dcload-ip,&amp;lt;br/&amp;gt;but BBA is superior and cheaper || No, only useful for online multiplayer gaming || Yes, supports dcload-serial || Yes, supports dcload-serial&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Cost || $100 - $200 and up on used markets || $200 and up on used markets,&amp;lt;br/&amp;gt;due to extreme rarity || Kit prices vary, around $100 || See [[Coder&#039;s cable]] for vendors&amp;lt;br /&amp;gt;darc sells for $45, RetroOnyx sells for $85 || Varies on used markets, uncommonly sold&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Can make DIY? || No || No || Yes || Yes || Yes&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Performance || Up to 100 megabits/s || Up to 10 megabits/s || Up to 56 kilobits/s || Up to 1500 kilobits/s || Up to 120 kilobits/s&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Games support || Some games: Phantasy Star Online, Quake III Arena, Toy Racer, POD SpeedZone, Propellor Arena, Unreal Tournament&amp;lt;br /&amp;gt;Some browsers: Broadband Passport, PlanetWeb 3.0 || No games&amp;lt;br /&amp;gt;One browser: Dream Passport for LAN || All multiplayer games with network support&amp;lt;br /&amp;gt;All web browsers || NO multiplayer games support || NO multiplayer games support &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Homebrew support || Homebrew utilities like dcload-ip || Homebrew utilities like dcload-ip || Homebrew utilities don&#039;t support, only multiplayer games || Homebrew utilities like dcload-serial || Homebrew utilities like dcload-serial &lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=Setting up and compiling the toolchain with the dc-chain script=&lt;br /&gt;
===Dependencies===&lt;br /&gt;
First, we&#039;ll need to install dependencies before building the toolchain. Below we have provided commands to install these dependencies on various systems. Many of the packages will likely already be installed on your system, but we have provided an exhaustive list for good measure.&lt;br /&gt;
====macOS 13 Ventura on an Intel or Apple Silicon processor====&lt;br /&gt;
First, make sure you install Apple Xcode, including the Command Line tools. You will also need to install several other packages for which we&#039;ll include instructions assuming you have installed the [https://brew.sh/ Homebrew] package manager on your system.&lt;br /&gt;
 brew install wget gettext texinfo gmp mpfr libmpc libelf jpeg-turbo libpng cmake meson libisofs&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;Important Note for Apple Silicon users&#039;&#039;: On Apple Silicon, Homebrew installs libraries to a path not included by default by the compiler. If you haven&#039;t added these to your &#039;&#039;&#039;~/.zprofile&#039;&#039;&#039;, then add the following lines now and reload your session (or run them in your Terminal session whenever you compile KOS):&lt;br /&gt;
 export CPATH=/opt/homebrew/include&lt;br /&gt;
 export LIBRARY_PATH=/opt/homebrew/lib&lt;br /&gt;
&lt;br /&gt;
====Debian/Ubuntu-based Linux====&lt;br /&gt;
 sudo apt-get update&lt;br /&gt;
 sudo apt install gawk patch bzip2 tar make libgmp-dev libmpfr-dev libmpc-dev gettext wget libelf-dev texinfo bison flex sed git build-essential diffutils curl libjpeg-dev libpng-dev python3 pkg-config cmake libisofs-dev meson ninja-build rake cmake&lt;br /&gt;
&lt;br /&gt;
====Fedora-based Linux====&lt;br /&gt;
 sudo dnf install gawk patch bzip2 tar make gmp-devel mpfr-devel libmpc-devel gettext wget elfutils-libelf-devel texinfo bison flex sed git diffutils curl libjpeg-turbo-devel libpng-devel gcc-c++ python3 rubygem-rake meson ninja-build cmake&lt;br /&gt;
&lt;br /&gt;
====Arch-based Linux====&lt;br /&gt;
 sudo pacman -S --needed gawk patch bzip2 tar make gmp mpfr libmpc gettext wget libelf texinfo bison flex sed git diffutils curl libjpeg-turbo libpng python3 meson ruby-rake cmake&lt;br /&gt;
&lt;br /&gt;
====Alpine-based Linux====&lt;br /&gt;
 sudo apk --update add build-base patch bash texinfo gmp-dev mpfr-dev libjpeg-turbo-dev libpng-dev elfutils-dev curl wget python3 git ruby-rake cmake fmt&lt;br /&gt;
&lt;br /&gt;
====Other Linux distributions====&lt;br /&gt;
If you&#039;re using a different Linux- or Unix-based system besides the one above, you may need to reference your distribution&#039;s package database and package manager documentation for the equivalent package names and commands necessary for your system.&lt;br /&gt;
&lt;br /&gt;
===Creating a space for your toolchain installation===&lt;br /&gt;
Create the path where we&#039;ll install the toolchain and KOS, and grant it the proper permissions:&lt;br /&gt;
 sudo mkdir -p /opt/toolchains/dc&lt;br /&gt;
 sudo chmod -R 755 /opt/toolchains/dc&lt;br /&gt;
 sudo chown -R $(id -u):$(id -g) /opt/toolchains/dc&lt;br /&gt;
===Cloning the KallistiOS git repository===&lt;br /&gt;
Next, clone the git repository for the latest &#039;&#039;&#039;2.2.0&#039;&#039;&#039; stable version of KallistiOS: &lt;br /&gt;
 git clone https://github.com/KallistiOS/KallistiOS.git -b v2.2.0 /opt/toolchains/dc/kos&lt;br /&gt;
&lt;br /&gt;
Alternatively, you may clone the latest unstable version, which contains the latest changes, but may also contain bugs or issues:&lt;br /&gt;
 git clone https://github.com/KallistiOS/KallistiOS.git -b master /opt/toolchains/dc/kos&lt;br /&gt;
&lt;br /&gt;
===Configuring the dc-chain script===&lt;br /&gt;
Enter the dc-chain directory:&lt;br /&gt;
 cd /opt/toolchains/dc/kos/utils/dc-chain&lt;br /&gt;
&lt;br /&gt;
You may now configure the options to your liking by creating a &amp;lt;code&amp;gt;Makefile.cfg&amp;lt;/code&amp;gt; file in this directory. Copy or rename the &amp;lt;code&amp;gt;Makefile.default.cfg&amp;lt;/code&amp;gt; (KOS 2.2.0 stable) or &amp;lt;code&amp;gt;Makefile.dreamcast.cfg&amp;lt;/code&amp;gt; (KOS master unstable) file to &amp;lt;code&amp;gt;Makefile.cfg&amp;lt;/code&amp;gt; and then use a text editor to edit this file&#039;s settings to your liking. For example, you may like to alter the &amp;lt;code&amp;gt;makejobs&amp;lt;/code&amp;gt; option to the number of threads available on your CPU to speed up the compilation. However, if you run into errors during compilation, you may want to set &amp;lt;code&amp;gt;makejobs=1&amp;lt;/code&amp;gt;, as on some operating systems the toolchain may fail to build with a higher setting.&lt;br /&gt;
&lt;br /&gt;
We will leave the default &#039;&#039;&#039;stable&#039;&#039;&#039; profile for the toolchain, which currently uses GCC 13.2. For advanced users, other profiles are available to you; read the &amp;lt;code&amp;gt;README.md&amp;lt;/code&amp;gt; file in the dc-chain directory for more information if you are interested. If you are unsure about options in the &amp;lt;code&amp;gt;Makefile.cfg&amp;lt;/code&amp;gt;, just leave the defaults alone.&lt;br /&gt;
&lt;br /&gt;
===Downloading and compiling the toolchain===&lt;br /&gt;
Now we will run a script to download files and compile the toolchain. At this point, we have the option of building both the main CPU SH4 compiler and the AICA sound processor ARM compiler, or we can skip the ARM compiler and just build the SH4 compiler. Thankfully, KallistiOS includes a prebuilt sound driver, so the ARM compiler is only necessary if you&#039;re wanting to make changes to the sound driver or write custom code to run on the sound processor.&lt;br /&gt;
To build &#039;&#039;&#039;only the SH4 compiler&#039;&#039;&#039;:&lt;br /&gt;
 make&lt;br /&gt;
To build &#039;&#039;&#039;both&#039;&#039;&#039; the SH4 and the ARM compilers:&lt;br /&gt;
 make all&lt;br /&gt;
This will download and unpack the relevant necessary files and then begin the compilation process. The compilation can take anywhere from minutes to a few hours depending on your CPU and number of threads available. When successfully finished, the toolchains will be ready. If you used the commands above, the &#039;&#039;&#039;gdb&#039;&#039;&#039; debugger will have been built as well.&lt;br /&gt;
&lt;br /&gt;
===Cleaning up temporary files===&lt;br /&gt;
After building everything, you can clean up the extraneous files in your dc-chain directory by entering:&lt;br /&gt;
 make clean&lt;br /&gt;
&lt;br /&gt;
=Configuring and compiling KOS and kos-ports=&lt;br /&gt;
===Setting up the environment settings===&lt;br /&gt;
Enter the KOS directory:&lt;br /&gt;
 cd /opt/toolchains/dc/kos&lt;br /&gt;
Copy the pre-made environment script into place:&lt;br /&gt;
 cp doc/environ.sh.sample environ.sh &lt;br /&gt;
For most users, the default settings will suffice. However, advanced users may edit the environ.sh to your liking if you&#039;d like to change compile flags or alter paths. If you&#039;d like to have multiple KOS versions installed or multiple toolchain versions installed, you can set up different environ.sh files corresponding to these different configurations by altering the paths.&lt;br /&gt;
&lt;br /&gt;
You will need to run the following command to apply the KOS environment settings to your currently running shell. Run the following command now, &#039;&#039;&#039;and&#039;&#039;&#039; &#039;&#039;whenever&#039;&#039; you open a new shell to work on Dreamcast projects:&lt;br /&gt;
 . /opt/toolchains/dc/kos/environ.sh&lt;br /&gt;
&lt;br /&gt;
===Building KOS===&lt;br /&gt;
Build KOS:&lt;br /&gt;
 make&lt;br /&gt;
KOS is now built.&lt;br /&gt;
===Building kos-ports===&lt;br /&gt;
Clone the kos-ports repository to your system:&lt;br /&gt;
 git clone --recursive https://github.com/KallistiOS/kos-ports /opt/toolchains/dc/kos-ports&lt;br /&gt;
Run the script to build all of the included ports:&lt;br /&gt;
 /opt/toolchains/dc/kos-ports/utils/build-all.sh&lt;br /&gt;
kos-ports is now built.&lt;br /&gt;
&lt;br /&gt;
===Building the KOS examples===&lt;br /&gt;
Enter the KOS examples directory:&lt;br /&gt;
 cd /opt/toolchains/dc/kos/examples&lt;br /&gt;
Build the examples:&lt;br /&gt;
 make&lt;br /&gt;
All of the example programs provided with KallistiOS are now built.&lt;br /&gt;
&lt;br /&gt;
=Further=&lt;br /&gt;
At this point, you will have a cross-compiler built so that you can generate programs for your Dreamcast from code. KallistiOS, the Dreamcast development library and operating system, is built, as is its associated packages within kos-ports, and all of its example programs.&lt;br /&gt;
&lt;br /&gt;
At this point, you will likely want to view some of the following articles:&lt;br /&gt;
* Setting up an IDE for Dreamcast development&lt;br /&gt;
** [[CLion Debugging]]&lt;br /&gt;
** [[Visual Studio Code]]&lt;br /&gt;
* [[Creating a first project with KallistiOS]] (&#039;&#039;&#039;TODO&#039;&#039;&#039;: Explain how to create a new DC project folder with Makefile, adding an external library, create a basic program, and compile it)&lt;br /&gt;
* [[Using a debug link]]&lt;br /&gt;
* [[Burning an example to CD-R]]&lt;br /&gt;
&lt;br /&gt;
=Running an example program through a debug link=&lt;br /&gt;
&#039;&#039;&#039;TODO&#039;&#039;&#039;: &#039;&#039;Give a tutorial on compiling dcload/dc-tool, setting up a serial, USB, or IP debug link, and running 2ndmix demo on real hardware.&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
On your development machine, setup [[dcload-ip]]:&lt;br /&gt;
* [[Using dcload-ip with Linux]]&lt;br /&gt;
* [[Using dcload-ip with Windows Subsystem for Linux]]&lt;br /&gt;
&lt;br /&gt;
For the Dreamcast, download and burn the [[:File:Dcload-2023-06-22.zip|latest version of dcload-ip or dcload-serial]] to a CD-R -- the IP version includes improved DHCP support, so there is no longer a need to configure things beforehand. &lt;br /&gt;
 &lt;br /&gt;
Run one of the examples from the &amp;lt;code&amp;gt;kos/examples/dreamcast&amp;lt;/code&amp;gt; directory with one of the following commands:&lt;br /&gt;
 dc-tool-ip -t &amp;lt;dreamcast IP address&amp;gt; -x example.elf&lt;br /&gt;
&lt;br /&gt;
 dc-tool-ser -t &amp;lt;serial device path&amp;gt; -x example.elf&lt;br /&gt;
&lt;br /&gt;
Run &amp;lt;code&amp;gt;dc-tool-ip&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;dc-tool-ser&amp;lt;/code&amp;gt; without any parameters to get additional options.&lt;br /&gt;
&lt;br /&gt;
=Burning an example program to CD-R=&lt;br /&gt;
&#039;&#039;&#039;TODO&#039;&#039;&#039;: &#039;&#039;Explain how to build mkdcdisc and write 2ndmix demo to CD-R and run on a Dreamcast console&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[https://gitlab.com/simulant/mkdcdisc mkdcdisc] can be used to easily generate a burnable self-boot CDI image.&lt;br /&gt;
Dependencies required:&lt;br /&gt;
* Homebrew: &amp;lt;code&amp;gt;meson libisofs pkg-config&amp;lt;/code&amp;gt;&lt;br /&gt;
* Debian/Ubuntu: &amp;lt;code&amp;gt;libisofs-dev meson ninja-build&amp;lt;/code&amp;gt;&lt;br /&gt;
* Fedora: &amp;lt;code&amp;gt;meson ninja-build&amp;lt;/code&amp;gt;&lt;br /&gt;
* Arch: &amp;lt;code&amp;gt;meson&amp;lt;/code&amp;gt;&lt;br /&gt;
* Alpine: &amp;lt;code&amp;gt;meson libisofs-dev&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Build &amp;lt;code&amp;gt;mkdcdisc&amp;lt;/code&amp;gt;:&lt;br /&gt;
 git clone https://gitlab.com/simulant/mkdcdisc.git&lt;br /&gt;
 cd mkdcdisc&lt;br /&gt;
 meson setup builddir&lt;br /&gt;
 meson compile -C builddir&lt;br /&gt;
 ./builddir/mkdcdisc -h&lt;br /&gt;
and create a CDI image from your compiled ELF like so:&lt;br /&gt;
 mkdcdisc -e MyProgram.elf -o MyProgram.cdi&lt;br /&gt;
Then you can burn the CDI file using DiscJuggler (Windows-only, but also works through [https://www.winehq.org/ WINE]), ImgBurn with the CDI plugin, or the cdiburn *nix script floating around out there. (document this better)&lt;/div&gt;</summary>
		<author><name>Darc</name></author>
	</entry>
	<entry>
		<id>https://dreamcast.wiki/wiki/index.php?title=KallistiOS_GameCube&amp;diff=3793</id>
		<title>KallistiOS GameCube</title>
		<link rel="alternate" type="text/html" href="https://dreamcast.wiki/wiki/index.php?title=KallistiOS_GameCube&amp;diff=3793"/>
		<updated>2025-08-03T20:30:32Z</updated>

		<summary type="html">&lt;p&gt;Darc: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;A KallistiOS port to GameCube is underway, led by [[darc]].&lt;br /&gt;
&lt;br /&gt;
This page exists to serve as a general reference for KallistiOS GameCube related information until a better page can be created.&lt;br /&gt;
&lt;br /&gt;
Wii support is planned as well, but GameCube is the current priority. Wii U support is not planned at this time, as it would likely require significant core KallistiOS changes.&lt;br /&gt;
&lt;br /&gt;
==Purpose/Goals==&lt;br /&gt;
# Born as a personal side project by [[darc]] to play with a non-Dreamcast embedded target and to learn more about low-level hardware programming, kernel programming, and KallistiOS low-level internals&lt;br /&gt;
# Restore KallistiOS to a multi-arch project. KallistiOS 1.x was originally multi-arch (dc/ps2/gba) before all non-Dreamcast targets were removed with the KallistiOS 2.x release due to lack of progress and interest. GameCube/Wii pairs well with the Dreamcast. The additional targets help keep the kernel architecture robust and provide more testing capabilities.&lt;br /&gt;
# Facilitate cross-platform homebrew games and allow easier development of GameCube/Wii ports of Dreamcast homebrew. While console-specific code will still be required, a unified KallistiOS core will reduce porting burden, and higher level, platform-independent KallistiOS APIs can be created for many components. OpenGL or SDL implementations can ease porting burden as well. This can help expand both the Dreamcast and GameCube/Wii homebrew dev scenes.&lt;br /&gt;
# For those concerned about the legality of libogc, KallistiOS serves as an alternative, as it does not use devkitPro/libogc.&lt;br /&gt;
&lt;br /&gt;
==Status==&lt;br /&gt;
* Toolchain builder: [https://github.com/KallistiOS/KallistiOS/pull/1097 Currently in PR] (thanks pcercuei)&lt;br /&gt;
* Changes needed to general KOS: few left, currently pending (thanks quzar)&lt;br /&gt;
* GameCube arch: Not upstreamed yet (in progress by darc)&lt;br /&gt;
** Working: Most of core KOS kernel (interrupts/threads/etc.), USB Gecko for debug output, basic pad reading, external framebuffer for video output&lt;br /&gt;
** Upstreaming of core arch code expected soon(TM)&lt;br /&gt;
&lt;br /&gt;
==News==&lt;br /&gt;
* [https://x.com/darcag3nt/status/1916867037588050326 Announcement] 4/28/2025&lt;br /&gt;
* [https://x.com/darcag3nt/status/1936593167454986745 Doom port demonstration] 6/21/2025&lt;br /&gt;
&lt;br /&gt;
==Running homebrew==&lt;br /&gt;
Most accessible methods for running homebrew (non-exhaustive):&lt;br /&gt;
* FlippyDrive: ODE + real disc drive combo, solderless, USD$50+ kit&lt;br /&gt;
* CubeODE: Replaces disc drive with ODE via SD slot, solderless, about USD$50&lt;br /&gt;
* [https://github.com/webhdx/PicoBoot PicoBoot]: Overrides BIOS with [https://github.com/redolution/gekkoboot gekkoboot] homebrew bootloader, easy soldering required, DIY and very cheap&lt;br /&gt;
* [https://github.com/makeo/PicoLoader PicoLoader]: Emulates a disc drive to boot a main homebrew app then re-enables real disc drive, solderless, DIY and very cheap&lt;br /&gt;
* XenoGC: Allows disc drive to boot homebrew from DVD-R, requires moderate soldering, cheap&lt;br /&gt;
* Action Replay: Requires rare Action Replay disc to load files from SD card reader&lt;br /&gt;
* Save exploits&lt;/div&gt;</summary>
		<author><name>Darc</name></author>
	</entry>
	<entry>
		<id>https://dreamcast.wiki/wiki/index.php?title=KallistiOS_GameCube&amp;diff=3792</id>
		<title>KallistiOS GameCube</title>
		<link rel="alternate" type="text/html" href="https://dreamcast.wiki/wiki/index.php?title=KallistiOS_GameCube&amp;diff=3792"/>
		<updated>2025-08-03T19:54:47Z</updated>

		<summary type="html">&lt;p&gt;Darc: Created page with &amp;quot;A KallistiOS port to GameCube is underway, led by darc.  This page exists to serve as a general reference for KallistiOS GameCube related information until a better page can be created.  ==Status== * Toolchain: Currently in PR (thanks pcercuei) * Changes needed to general KOS: few left, currently pending (thanks quzar) * GameCube arch: Not upstreamed yet (in progress by darc)  ==Running homebrew== Most accessible methods for running homebrew (non-exhaustive): * Flipp...&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;A KallistiOS port to GameCube is underway, led by [[darc]].&lt;br /&gt;
&lt;br /&gt;
This page exists to serve as a general reference for KallistiOS GameCube related information until a better page can be created.&lt;br /&gt;
&lt;br /&gt;
==Status==&lt;br /&gt;
* Toolchain: Currently in PR (thanks pcercuei)&lt;br /&gt;
* Changes needed to general KOS: few left, currently pending (thanks quzar)&lt;br /&gt;
* GameCube arch: Not upstreamed yet (in progress by darc)&lt;br /&gt;
&lt;br /&gt;
==Running homebrew==&lt;br /&gt;
Most accessible methods for running homebrew (non-exhaustive):&lt;br /&gt;
* FlippyDrive: ODE + real disc drive combo, solderless, USD$50+ kit&lt;br /&gt;
* CubeODE: Replaces disc drive with ODE via SD slot, solderless, about USD$50&lt;br /&gt;
* [https://github.com/webhdx/PicoBoot PicoBoot]: Overrides BIOS with [https://github.com/redolution/gekkoboot gekkoboot] homebrew bootloader, easy soldering required, DIY and very cheap&lt;br /&gt;
* [https://github.com/makeo/PicoLoader PicoLoader]: Emulates a disc drive to boot a main homebrew app then re-enables real disc drive, solderless, DIY and very cheap&lt;br /&gt;
* XenoGC: Allows disc drive to boot homebrew from DVD-R, requires moderate soldering, cheap&lt;br /&gt;
* Action Replay: Requires rare Action Replay disc to load files from SD card reader&lt;br /&gt;
* Save exploits&lt;/div&gt;</summary>
		<author><name>Darc</name></author>
	</entry>
	<entry>
		<id>https://dreamcast.wiki/wiki/index.php?title=File:SH7750_Group_User%27s_Manual_-_Hardware.pdf&amp;diff=3786</id>
		<title>File:SH7750 Group User&#039;s Manual - Hardware.pdf</title>
		<link rel="alternate" type="text/html" href="https://dreamcast.wiki/wiki/index.php?title=File:SH7750_Group_User%27s_Manual_-_Hardware.pdf&amp;diff=3786"/>
		<updated>2025-07-24T21:08:39Z</updated>

		<summary type="html">&lt;p&gt;Darc: Uploaded own work with UploadWizard&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=={{int:filedesc}}==&lt;br /&gt;
{{Information&lt;br /&gt;
|description={{no|1=SH7750 Group User&#039;s Manual - Hardware}}&lt;br /&gt;
|date=2025-07-24&lt;br /&gt;
|source={{own}}&lt;br /&gt;
|author=[[User:Darc|Darc]]&lt;br /&gt;
|permission=&lt;br /&gt;
|other versions=&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
=={{int:license-header}}==&lt;br /&gt;
{{licensing|generic}}&lt;/div&gt;</summary>
		<author><name>Darc</name></author>
	</entry>
	<entry>
		<id>https://dreamcast.wiki/wiki/index.php?title=File:SH4_C_ABI.pdf&amp;diff=3785</id>
		<title>File:SH4 C ABI.pdf</title>
		<link rel="alternate" type="text/html" href="https://dreamcast.wiki/wiki/index.php?title=File:SH4_C_ABI.pdf&amp;diff=3785"/>
		<updated>2025-07-24T21:08:39Z</updated>

		<summary type="html">&lt;p&gt;Darc: Uploaded own work with UploadWizard&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=={{int:filedesc}}==&lt;br /&gt;
{{Information&lt;br /&gt;
|description={{no|1=SH4 C ABI}}&lt;br /&gt;
|date=2025-07-24&lt;br /&gt;
|source={{own}}&lt;br /&gt;
|author=[[User:Darc|Darc]]&lt;br /&gt;
|permission=&lt;br /&gt;
|other versions=&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
=={{int:license-header}}==&lt;br /&gt;
{{licensing|generic}}&lt;/div&gt;</summary>
		<author><name>Darc</name></author>
	</entry>
	<entry>
		<id>https://dreamcast.wiki/wiki/index.php?title=File:SH-4_Software_Manual.pdf&amp;diff=3784</id>
		<title>File:SH-4 Software Manual.pdf</title>
		<link rel="alternate" type="text/html" href="https://dreamcast.wiki/wiki/index.php?title=File:SH-4_Software_Manual.pdf&amp;diff=3784"/>
		<updated>2025-07-24T21:08:39Z</updated>

		<summary type="html">&lt;p&gt;Darc: Uploaded own work with UploadWizard&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=={{int:filedesc}}==&lt;br /&gt;
{{Information&lt;br /&gt;
|description={{no|1=SH-4 Software Manual}}&lt;br /&gt;
|date=2025-07-24&lt;br /&gt;
|source={{own}}&lt;br /&gt;
|author=[[User:Darc|Darc]]&lt;br /&gt;
|permission=&lt;br /&gt;
|other versions=&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
=={{int:license-header}}==&lt;br /&gt;
{{licensing|generic}}&lt;/div&gt;</summary>
		<author><name>Darc</name></author>
	</entry>
	<entry>
		<id>https://dreamcast.wiki/wiki/index.php?title=Getting_Started_with_Dreamcast_development&amp;diff=3769</id>
		<title>Getting Started with Dreamcast development</title>
		<link rel="alternate" type="text/html" href="https://dreamcast.wiki/wiki/index.php?title=Getting_Started_with_Dreamcast_development&amp;diff=3769"/>
		<updated>2025-07-11T22:57:10Z</updated>

		<summary type="html">&lt;p&gt;Darc: /* Cloning the KallistiOS git repository */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;div style=&amp;quot;float:right;&amp;quot;&amp;gt;__TOC__&amp;lt;/div&amp;gt;&lt;br /&gt;
=Introduction=&lt;br /&gt;
This article will cover the entire beginning process: starting from zero to having a working dev environment with debug link (serial or IP) and self-booting CD-R. This guide will cover the process for the following platforms:&lt;br /&gt;
* &#039;&#039;&#039;Microsoft Windows 10&#039;&#039;&#039; via [https://learn.microsoft.com/en-us/windows/wsl/about Windows Subsystem for Linux]&lt;br /&gt;
** Users desiring a native Windows approach, see [[DreamSDK]] instead&lt;br /&gt;
* &#039;&#039;&#039;macOS&#039;&#039;&#039; on Intel or Apple Silicon systems with the [https://brew.sh/ Homebrew] package manager installed&lt;br /&gt;
* &#039;&#039;&#039;Linux&#039;&#039;&#039;-based systems&lt;br /&gt;
** &#039;&#039;&#039;Debian&#039;&#039;&#039;- and &#039;&#039;&#039;Ubuntu&#039;&#039;&#039;-based distributions using the default &#039;&#039;&#039;apt&#039;&#039;&#039; package manger&lt;br /&gt;
** &#039;&#039;&#039;Fedora&#039;&#039;&#039;-based distributions using the default &#039;&#039;&#039;dnf&#039;&#039;&#039; package manager&lt;br /&gt;
** &#039;&#039;&#039;Arch&#039;&#039;&#039;-based distributions using the default &#039;&#039;&#039;pacman&#039;&#039;&#039; package manager&lt;br /&gt;
** &#039;&#039;&#039;Alpine&#039;&#039;&#039;-based distributions using the default &#039;&#039;&#039;apk&#039;&#039;&#039; package manager&lt;br /&gt;
&lt;br /&gt;
===Need help?===&lt;br /&gt;
Important note: &#039;&#039;This guide aims to remain up to date and work on all of the above platforms, but keeping instructions for such a variety of platforms up-to-date can be difficult. If you run into any errors or other challenges while following this tutorial, or simply need clarification on any of the steps, feel free to ask for assistance on the [https://dcemulation.org/phpBB/viewforum.php?f=29 message board] or [https://discord.gg/wDEfkUh63h Discord chat] and we would be happy to aid you and update the guide for the benefit of future readers and others in the community.&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
===Terms===&lt;br /&gt;
Before we get started, let&#039;s define several terms:&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;&#039;toolchain&#039;&#039;&#039; is a set of programs which turns your code into an executable file for your Dreamcast console. The toolchain includes:&lt;br /&gt;
* &#039;&#039;&#039;GCC&#039;&#039;&#039;, a compiler which supports C, C++, Objective-C, and more (see also [[Rust on Dreamcast]] and [[Programming language support]])&lt;br /&gt;
* &#039;&#039;&#039;binutils&#039;&#039;&#039;, an assembler and linker&lt;br /&gt;
* &#039;&#039;&#039;newlib&#039;&#039;&#039;, a C library&lt;br /&gt;
* &#039;&#039;&#039;gdb&#039;&#039;&#039;, a debugger&lt;br /&gt;
The toolchain includes a compiler for the Dreamcast&#039;s main SH4 CPU, and optionally a compiler for the ARM-based AICA sound processor. Your operating system may already have versions of these programs installed to compile code for your computer, but we will need to build a &amp;quot;cross-compiler&amp;quot; for compiling specifically for the Dreamcast. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;KallistiOS&#039;&#039;&#039; or &#039;&#039;KOS&#039;&#039; is an open source development library and pseudo-operating system for the Dreamcast console. It is the best documented and most widely used development kit in the homebrew community. KallistiOS&#039;s very flexible license allows both homebrew and commercial use with no restrictions other than a requirement to include credit for its use in your project, and indeed almost all commercially sold indie Dreamcast titles use it. There are others in existence, like [[libronin]] and [[libdream]], as well as the older development kits [[Katana]] and [[Windows CE]] created by Sega and Microsoft for use in retail games, but this guide will only cover the setup and use of KallistiOS. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;kos-ports&#039;&#039;&#039; is a repository including various libraries which integrate with KallistiOS. We will download and compile these libraries as well. &lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;&#039;debug link&#039;&#039;&#039; is a generic term referring to a hardware accessory to facilitate quickly running and debugging your programs. IP-based links include the Dreamcast&#039;s &#039;&#039;&#039;[[Broadband adapter]]&#039;&#039;&#039; and &#039;&#039;&#039;[[LAN adapter]]&#039;&#039;&#039; accessories, and serial-based links include the [[Coder&#039;s cable]], which is a cable that can connect the Dreamcast&#039;s serial port to your computer via USB or serial. This guide includes instructions for setting up and using the the Broadband adapter and a USB-based coder&#039;s cable.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;dc-tool&#039;&#039;&#039; and &#039;&#039;&#039;dcload&#039;&#039;&#039; are a pair of programs to facilitate using a debug link. &#039;&#039;dc-tool&#039;&#039; runs on your computer and links to a Dreamcast running &#039;&#039;dcload-ip&#039;&#039; or &#039;&#039;dcload-serial&#039;&#039;. With this setup, you can quickly load programs, read console feedback, load assets, transfer data, redirect I/O, handle exceptions, debug problems, and so forth.&lt;br /&gt;
&lt;br /&gt;
=Choosing a debug link solution=&lt;br /&gt;
If you are building the toolchain for the purpose of building existing programs from source with little to no modifications, then a debug link setup might not be necessary for you. You may simply build programs to burn directly to CD-R. However, if you are planning to actively develop for the Dreamcast, then a debug link is a critical component. While Dreamcast emulators are mature and accurate enough to play the vast majority of the system&#039;s games library without issue, many critical bugs may show up on a real Dreamcast system, but not on a Dreamcast emulator. Therefore, it is highly recommended to test on a real system as much as possible. It&#039;s also possible to load software off of a [[Serial SD card adapter]], but without an active link to a computer, debugging and stepping through programs as they execute is significantly more challenging.&lt;br /&gt;
&lt;br /&gt;
Presented below is a table comparing the different options available for a debug link. Due to the cost, potential buyers may want to factor in the ability to play multiplayer games with their purchase. Thus, for comparison, we have included information about the [[Modem]] with [[DreamPi]] as well, but understand that the Modem with DreamPi cannot be used as a debug link.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
!colspan=&amp;quot;6&amp;quot; |Comparison of various Dreamcast connectivity options&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#c0c0c0;&amp;quot; width=&amp;quot;150&amp;quot; | Device: &lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; width=&amp;quot;400&amp;quot; | [[Broadband adapter]] (HIT-400 or HIT-401) &amp;lt;br /&amp;gt;Realtek RTL8139C chipset&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; width=&amp;quot;400&amp;quot; | [[LAN adapter]] (HIT-300) &amp;lt;br /&amp;gt;Fujitsu MB86967 chipset&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; width=&amp;quot;400&amp;quot; | [[Modem]] with [[DreamPi]]&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; width=&amp;quot;400&amp;quot; | USB [[Coder&#039;s cable]]&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; width=&amp;quot;400&amp;quot; | Serial [[Coder&#039;s cable]]&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Useful for dev? || Yes, supports dcload-ip || Yes, supports dcload-ip,&amp;lt;br/&amp;gt;but BBA is superior and cheaper || No, only useful for online multiplayer gaming || Yes, supports dcload-serial || Yes, supports dcload-serial&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Cost || $100 - $200 and up on used markets || $200 and up on used markets,&amp;lt;br/&amp;gt;due to extreme rarity || Kit prices vary, around $100 || See [[Coder&#039;s cable]] for vendors&amp;lt;br /&amp;gt;darc sells for $45, RetroOnyx sells for $85 || Varies on used markets, uncommonly sold&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Can make DIY? || No || No || Yes || Yes || Yes&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Performance || Up to 100 megabits/s || Up to 10 megabits/s || Up to 56 kilobits/s || Up to 1500 kilobits/s || Up to 120 kilobits/s&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Games support || Some games: Phantasy Star Online, Quake III Arena, Toy Racer, POD SpeedZone, Propellor Arena, Unreal Tournament&amp;lt;br /&amp;gt;Some browsers: Broadband Passport, PlanetWeb 3.0 || No games&amp;lt;br /&amp;gt;One browser: Dream Passport for LAN || All multiplayer games with network support&amp;lt;br /&amp;gt;All web browsers || NO multiplayer games support || NO multiplayer games support &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Homebrew support || Homebrew utilities like dcload-ip || Homebrew utilities like dcload-ip || Homebrew utilities don&#039;t support, only multiplayer games || Homebrew utilities like dcload-serial || Homebrew utilities like dcload-serial &lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=Setting up and compiling the toolchain with the dc-chain script=&lt;br /&gt;
===Dependencies===&lt;br /&gt;
First, we&#039;ll need to install dependencies before building the toolchain. Below we have provided commands to install these dependencies on various systems. Many of the packages will likely already be installed on your system, but we have provided an exhaustive list for good measure.&lt;br /&gt;
====macOS 13 Ventura on an Intel or Apple Silicon processor====&lt;br /&gt;
First, make sure you install Apple Xcode, including the Command Line tools. You will also need to install several other packages for which we&#039;ll include instructions assuming you have installed the [https://brew.sh/ Homebrew] package manager on your system.&lt;br /&gt;
 brew install wget gettext texinfo gmp mpfr libmpc libelf jpeg-turbo libpng cmake meson libisofs&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;Important Note for Apple Silicon users&#039;&#039;: On Apple Silicon, Homebrew installs libraries to a path not included by default by the compiler. If you haven&#039;t added these to your &#039;&#039;&#039;~/.zprofile&#039;&#039;&#039;, then add the following lines now and reload your session (or run them in your Terminal session whenever you compile KOS):&lt;br /&gt;
 export CPATH=/opt/homebrew/include&lt;br /&gt;
 export LIBRARY_PATH=/opt/homebrew/lib&lt;br /&gt;
&lt;br /&gt;
====Debian/Ubuntu-based Linux====&lt;br /&gt;
 sudo apt-get update&lt;br /&gt;
 sudo apt install gawk patch bzip2 tar make libgmp-dev libmpfr-dev libmpc-dev gettext wget libelf-dev texinfo bison flex sed git build-essential diffutils curl libjpeg-dev libpng-dev python3 pkg-config cmake libisofs-dev meson ninja-build rake cmake&lt;br /&gt;
&lt;br /&gt;
====Fedora-based Linux====&lt;br /&gt;
 sudo dnf install gawk patch bzip2 tar make gmp-devel mpfr-devel libmpc-devel gettext wget elfutils-libelf-devel texinfo bison flex sed git diffutils curl libjpeg-turbo-devel libpng-devel gcc-c++ python3 rubygem-rake meson ninja-build cmake&lt;br /&gt;
&lt;br /&gt;
====Arch-based Linux====&lt;br /&gt;
 sudo pacman -S --needed gawk patch bzip2 tar make gmp mpfr libmpc gettext wget libelf texinfo bison flex sed git diffutils curl libjpeg-turbo libpng python3 meson ruby-rake cmake&lt;br /&gt;
&lt;br /&gt;
====Alpine-based Linux====&lt;br /&gt;
 sudo apk --update add build-base patch bash texinfo gmp-dev mpfr-dev libjpeg-turbo-dev libpng-dev elfutils-dev curl wget python3 git ruby-rake cmake fmt&lt;br /&gt;
&lt;br /&gt;
====Other Linux distributions====&lt;br /&gt;
If you&#039;re using a different Linux- or Unix-based system besides the one above, you may need to reference your distribution&#039;s package database and package manager documentation for the equivalent package names and commands necessary for your system.&lt;br /&gt;
&lt;br /&gt;
===Creating a space for your toolchain installation===&lt;br /&gt;
Create the path where we&#039;ll install the toolchain and KOS, and grant it the proper permissions:&lt;br /&gt;
 sudo mkdir -p /opt/toolchains/dc&lt;br /&gt;
 sudo chmod -R 755 /opt/toolchains/dc&lt;br /&gt;
 sudo chown -R $(id -u):$(id -g) /opt/toolchains/dc&lt;br /&gt;
===Cloning the KallistiOS git repository===&lt;br /&gt;
Next, clone the git repository for the latest &#039;&#039;&#039;2.2.0&#039;&#039;&#039; stable version of KallistiOS: &lt;br /&gt;
 git clone https://github.com/KallistiOS/KallistiOS.git -b v2.2.0 /opt/toolchains/dc/kos&lt;br /&gt;
&lt;br /&gt;
Alternatively, you may clone the latest unstable version, which contains the latest changes, but may also contain bugs or issues:&lt;br /&gt;
 git clone https://github.com/KallistiOS/KallistiOS.git -b master /opt/toolchains/dc/kos&lt;br /&gt;
&lt;br /&gt;
===Configuring the dc-chain script===&lt;br /&gt;
Enter the dc-chain directory:&lt;br /&gt;
 cd /opt/toolchains/dc/kos/utils/dc-chain&lt;br /&gt;
&lt;br /&gt;
You may now configure the options to your liking by creating a &amp;lt;code&amp;gt;Makefile.cfg&amp;lt;/code&amp;gt; file in this directory. Copy or rename the &amp;lt;code&amp;gt;Makefile.default.cfg&amp;lt;/code&amp;gt; file to &amp;lt;code&amp;gt;Makefile.cfg&amp;lt;/code&amp;gt; and then use a text editor to edit this file&#039;s settings to your liking. For example, you may like to alter the &amp;lt;code&amp;gt;makejobs&amp;lt;/code&amp;gt; option to the number of threads available on your CPU to speed up the compilation. However, if you run into errors during compilation, you may want to set &amp;lt;code&amp;gt;makejobs=1&amp;lt;/code&amp;gt;, as on some operating systems the toolchain may fail to build with a higher setting.&lt;br /&gt;
&lt;br /&gt;
We will leave the default &#039;&#039;&#039;stable&#039;&#039;&#039; profile for the toolchain, which currently uses GCC 13.2. For advanced users, other profiles are available to you; read the &amp;lt;code&amp;gt;README.md&amp;lt;/code&amp;gt; file in the dc-chain directory for more information if you are interested. If you are unsure about options in the &amp;lt;code&amp;gt;Makefile.cfg&amp;lt;/code&amp;gt;, just leave the defaults alone.&lt;br /&gt;
&lt;br /&gt;
===Downloading and compiling the toolchain===&lt;br /&gt;
Now we will run a script to download files and compile the toolchain. At this point, we have the option of building both the main CPU SH4 compiler and the AICA sound processor ARM compiler, or we can skip the ARM compiler and just build the SH4 compiler. Thankfully, KallistiOS includes a prebuilt sound driver, so the ARM compiler is only necessary if you&#039;re wanting to make changes to the sound driver or write custom code to run on the sound processor.&lt;br /&gt;
To build &#039;&#039;&#039;only the SH4 compiler&#039;&#039;&#039;:&lt;br /&gt;
 make&lt;br /&gt;
To build &#039;&#039;&#039;both&#039;&#039;&#039; the SH4 and the ARM compilers:&lt;br /&gt;
 make all&lt;br /&gt;
This will download and unpack the relevant necessary files and then begin the compilation process. The compilation can take anywhere from minutes to a few hours depending on your CPU and number of threads available. When successfully finished, the toolchains will be ready. If you used the commands above, the &#039;&#039;&#039;gdb&#039;&#039;&#039; debugger will have been built as well.&lt;br /&gt;
&lt;br /&gt;
===Cleaning up temporary files===&lt;br /&gt;
After building everything, you can clean up the extraneous files in your dc-chain directory by entering:&lt;br /&gt;
 make clean&lt;br /&gt;
&lt;br /&gt;
=Configuring and compiling KOS and kos-ports=&lt;br /&gt;
===Setting up the environment settings===&lt;br /&gt;
Enter the KOS directory:&lt;br /&gt;
 cd /opt/toolchains/dc/kos&lt;br /&gt;
Copy the pre-made environment script into place:&lt;br /&gt;
 cp doc/environ.sh.sample environ.sh &lt;br /&gt;
For most users, the default settings will suffice. However, advanced users may edit the environ.sh to your liking if you&#039;d like to change compile flags or alter paths. If you&#039;d like to have multiple KOS versions installed or multiple toolchain versions installed, you can set up different environ.sh files corresponding to these different configurations by altering the paths.&lt;br /&gt;
&lt;br /&gt;
You will need to run the following command to apply the KOS environment settings to your currently running shell. Run the following command now, &#039;&#039;&#039;and&#039;&#039;&#039; &#039;&#039;whenever&#039;&#039; you open a new shell to work on Dreamcast projects:&lt;br /&gt;
 . /opt/toolchains/dc/kos/environ.sh&lt;br /&gt;
&lt;br /&gt;
===Building KOS===&lt;br /&gt;
Build KOS:&lt;br /&gt;
 make&lt;br /&gt;
KOS is now built.&lt;br /&gt;
===Building kos-ports===&lt;br /&gt;
Clone the kos-ports repository to your system:&lt;br /&gt;
 git clone --recursive https://github.com/KallistiOS/kos-ports /opt/toolchains/dc/kos-ports&lt;br /&gt;
Run the script to build all of the included ports:&lt;br /&gt;
 /opt/toolchains/dc/kos-ports/utils/build-all.sh&lt;br /&gt;
kos-ports is now built.&lt;br /&gt;
&lt;br /&gt;
===Building the KOS examples===&lt;br /&gt;
Enter the KOS examples directory:&lt;br /&gt;
 cd /opt/toolchains/dc/kos/examples&lt;br /&gt;
Build the examples:&lt;br /&gt;
 make&lt;br /&gt;
All of the example programs provided with KallistiOS are now built.&lt;br /&gt;
&lt;br /&gt;
=Further=&lt;br /&gt;
At this point, you will have a cross-compiler built so that you can generate programs for your Dreamcast from code. KallistiOS, the Dreamcast development library and operating system, is built, as is its associated packages within kos-ports, and all of its example programs.&lt;br /&gt;
&lt;br /&gt;
At this point, you will likely want to view some of the following articles:&lt;br /&gt;
* Setting up an IDE for Dreamcast development&lt;br /&gt;
** [[CLion Debugging]]&lt;br /&gt;
** [[Visual Studio Code]]&lt;br /&gt;
* [[Creating a first project with KallistiOS]] (&#039;&#039;&#039;TODO&#039;&#039;&#039;: Explain how to create a new DC project folder with Makefile, adding an external library, create a basic program, and compile it)&lt;br /&gt;
* [[Using a debug link]]&lt;br /&gt;
* [[Burning an example to CD-R]]&lt;br /&gt;
&lt;br /&gt;
=Running an example program through a debug link=&lt;br /&gt;
&#039;&#039;&#039;TODO&#039;&#039;&#039;: &#039;&#039;Give a tutorial on compiling dcload/dc-tool, setting up a serial, USB, or IP debug link, and running 2ndmix demo on real hardware.&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Download and burn the [[:File:Dcload-2023-06-22.zip|latest versions of dcload-ip or dcload-serial]] -- the IP version includes improved DHCP support, so there is no longer a need to configure things beforehand. &lt;br /&gt;
 &lt;br /&gt;
Run one of the examples from the &amp;lt;code&amp;gt;kos/examples/dreamcast&amp;lt;/code&amp;gt; directory with one of the following commands:&lt;br /&gt;
 dc-tool-ip -t &amp;lt;dreamcast IP address&amp;gt; -x example.elf&lt;br /&gt;
&lt;br /&gt;
 dc-tool-ser -t &amp;lt;serial device path&amp;gt; -x example.elf&lt;br /&gt;
&lt;br /&gt;
Run &amp;lt;code&amp;gt;dc-tool-ip&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;dc-tool-ser&amp;lt;/code&amp;gt; without any parameters to get additional options.&lt;br /&gt;
&lt;br /&gt;
=Burning an example program to CD-R=&lt;br /&gt;
&#039;&#039;&#039;TODO&#039;&#039;&#039;: &#039;&#039;Explain how to build mkdcdisc and write 2ndmix demo to CD-R and run on a Dreamcast console&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[https://gitlab.com/simulant/mkdcdisc mkdcdisc] can be used to easily generate a burnable self-boot CDI image.&lt;br /&gt;
Dependencies required:&lt;br /&gt;
* Homebrew: &amp;lt;code&amp;gt;meson libisofs pkg-config&amp;lt;/code&amp;gt;&lt;br /&gt;
* Debian/Ubuntu: &amp;lt;code&amp;gt;libisofs-dev meson ninja-build&amp;lt;/code&amp;gt;&lt;br /&gt;
* Fedora: &amp;lt;code&amp;gt;meson ninja-build&amp;lt;/code&amp;gt;&lt;br /&gt;
* Arch: &amp;lt;code&amp;gt;meson&amp;lt;/code&amp;gt;&lt;br /&gt;
* Alpine: &amp;lt;code&amp;gt;meson libisofs-dev&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Build &amp;lt;code&amp;gt;mkdcdisc&amp;lt;/code&amp;gt;:&lt;br /&gt;
 git clone https://gitlab.com/simulant/mkdcdisc.git&lt;br /&gt;
 cd mkdcdisc&lt;br /&gt;
 meson setup builddir&lt;br /&gt;
 meson compile -C builddir&lt;br /&gt;
 ./builddir/mkdcdisc -h&lt;br /&gt;
and create a CDI image from your compiled ELF like so:&lt;br /&gt;
 mkdcdisc -e MyProgram.elf -o MyProgram.cdi&lt;br /&gt;
Then you can burn the CDI file using DiscJuggler (Windows-only, but also works through [https://www.winehq.org/ WINE]), ImgBurn with the CDI plugin, or the cdiburn *nix script floating around out there. (document this better)&lt;/div&gt;</summary>
		<author><name>Darc</name></author>
	</entry>
	<entry>
		<id>https://dreamcast.wiki/wiki/index.php?title=Getting_Started_with_Dreamcast_development&amp;diff=3761</id>
		<title>Getting Started with Dreamcast development</title>
		<link rel="alternate" type="text/html" href="https://dreamcast.wiki/wiki/index.php?title=Getting_Started_with_Dreamcast_development&amp;diff=3761"/>
		<updated>2025-06-23T03:48:40Z</updated>

		<summary type="html">&lt;p&gt;Darc: /* Setting up the environment settings */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;div style=&amp;quot;float:right;&amp;quot;&amp;gt;__TOC__&amp;lt;/div&amp;gt;&lt;br /&gt;
=Introduction=&lt;br /&gt;
This article will cover the entire beginning process: starting from zero to having a working dev environment with debug link (serial or IP) and self-booting CD-R. This guide will cover the process for the following platforms:&lt;br /&gt;
* &#039;&#039;&#039;Microsoft Windows 10&#039;&#039;&#039; via [https://learn.microsoft.com/en-us/windows/wsl/about Windows Subsystem for Linux]&lt;br /&gt;
** Users desiring a native Windows approach, see [[DreamSDK]] instead&lt;br /&gt;
* &#039;&#039;&#039;macOS&#039;&#039;&#039; on Intel or Apple Silicon systems with the [https://brew.sh/ Homebrew] package manager installed&lt;br /&gt;
* &#039;&#039;&#039;Linux&#039;&#039;&#039;-based systems&lt;br /&gt;
** &#039;&#039;&#039;Debian&#039;&#039;&#039;- and &#039;&#039;&#039;Ubuntu&#039;&#039;&#039;-based distributions using the default &#039;&#039;&#039;apt&#039;&#039;&#039; package manger&lt;br /&gt;
** &#039;&#039;&#039;Fedora&#039;&#039;&#039;-based distributions using the default &#039;&#039;&#039;dnf&#039;&#039;&#039; package manager&lt;br /&gt;
** &#039;&#039;&#039;Arch&#039;&#039;&#039;-based distributions using the default &#039;&#039;&#039;pacman&#039;&#039;&#039; package manager&lt;br /&gt;
** &#039;&#039;&#039;Alpine&#039;&#039;&#039;-based distributions using the default &#039;&#039;&#039;apk&#039;&#039;&#039; package manager&lt;br /&gt;
&lt;br /&gt;
===Need help?===&lt;br /&gt;
Important note: &#039;&#039;This guide aims to remain up to date and work on all of the above platforms, but keeping instructions for such a variety of platforms up-to-date can be difficult. If you run into any errors or other challenges while following this tutorial, or simply need clarification on any of the steps, feel free to ask for assistance on the [https://dcemulation.org/phpBB/viewforum.php?f=29 message board] or [https://discord.gg/wDEfkUh63h Discord chat] and we would be happy to aid you and update the guide for the benefit of future readers and others in the community.&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
===Terms===&lt;br /&gt;
Before we get started, let&#039;s define several terms:&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;&#039;toolchain&#039;&#039;&#039; is a set of programs which turns your code into an executable file for your Dreamcast console. The toolchain includes:&lt;br /&gt;
* &#039;&#039;&#039;GCC&#039;&#039;&#039;, a compiler which supports C, C++, Objective-C, and more (see also [[Rust on Dreamcast]] and [[Programming language support]])&lt;br /&gt;
* &#039;&#039;&#039;binutils&#039;&#039;&#039;, an assembler and linker&lt;br /&gt;
* &#039;&#039;&#039;newlib&#039;&#039;&#039;, a C library&lt;br /&gt;
* &#039;&#039;&#039;gdb&#039;&#039;&#039;, a debugger&lt;br /&gt;
The toolchain includes a compiler for the Dreamcast&#039;s main SH4 CPU, and optionally a compiler for the ARM-based AICA sound processor. Your operating system may already have versions of these programs installed to compile code for your computer, but we will need to build a &amp;quot;cross-compiler&amp;quot; for compiling specifically for the Dreamcast. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;KallistiOS&#039;&#039;&#039; or &#039;&#039;KOS&#039;&#039; is an open source development library and pseudo-operating system for the Dreamcast console. It is the best documented and most widely used development kit in the homebrew community. KallistiOS&#039;s very flexible license allows both homebrew and commercial use with no restrictions other than a requirement to include credit for its use in your project, and indeed almost all commercially sold indie Dreamcast titles use it. There are others in existence, like [[libronin]] and [[libdream]], as well as the older development kits [[Katana]] and [[Windows CE]] created by Sega and Microsoft for use in retail games, but this guide will only cover the setup and use of KallistiOS. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;kos-ports&#039;&#039;&#039; is a repository including various libraries which integrate with KallistiOS. We will download and compile these libraries as well. &lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;&#039;debug link&#039;&#039;&#039; is a generic term referring to a hardware accessory to facilitate quickly running and debugging your programs. IP-based links include the Dreamcast&#039;s &#039;&#039;&#039;[[Broadband adapter]]&#039;&#039;&#039; and &#039;&#039;&#039;[[LAN adapter]]&#039;&#039;&#039; accessories, and serial-based links include the [[Coder&#039;s cable]], which is a cable that can connect the Dreamcast&#039;s serial port to your computer via USB or serial. This guide includes instructions for setting up and using the the Broadband adapter and a USB-based coder&#039;s cable.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;dc-tool&#039;&#039;&#039; and &#039;&#039;&#039;dcload&#039;&#039;&#039; are a pair of programs to facilitate using a debug link. &#039;&#039;dc-tool&#039;&#039; runs on your computer and links to a Dreamcast running &#039;&#039;dcload-ip&#039;&#039; or &#039;&#039;dcload-serial&#039;&#039;. With this setup, you can quickly load programs, read console feedback, load assets, transfer data, redirect I/O, handle exceptions, debug problems, and so forth.&lt;br /&gt;
&lt;br /&gt;
=Choosing a debug link solution=&lt;br /&gt;
If you are building the toolchain for the purpose of building existing programs from source with little to no modifications, then a debug link setup might not be necessary for you. You may simply build programs to burn directly to CD-R. However, if you are planning to actively develop for the Dreamcast, then a debug link is a critical component. While Dreamcast emulators are mature and accurate enough to play the vast majority of the system&#039;s games library without issue, many critical bugs may show up on a real Dreamcast system, but not on a Dreamcast emulator. Therefore, it is highly recommended to test on a real system as much as possible. It&#039;s also possible to load software off of a [[Serial SD card adapter]], but without an active link to a computer, debugging and stepping through programs as they execute is significantly more challenging.&lt;br /&gt;
&lt;br /&gt;
Presented below is a table comparing the different options available for a debug link. Due to the cost, potential buyers may want to factor in the ability to play multiplayer games with their purchase. Thus, for comparison, we have included information about the [[Modem]] with [[DreamPi]] as well, but understand that the Modem with DreamPi cannot be used as a debug link.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
!colspan=&amp;quot;6&amp;quot; |Comparison of various Dreamcast connectivity options&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#c0c0c0;&amp;quot; width=&amp;quot;150&amp;quot; | Device: &lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; width=&amp;quot;400&amp;quot; | [[Broadband adapter]] (HIT-400 or HIT-401) &amp;lt;br /&amp;gt;Realtek RTL8139C chipset&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; width=&amp;quot;400&amp;quot; | [[LAN adapter]] (HIT-300) &amp;lt;br /&amp;gt;Fujitsu MB86967 chipset&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; width=&amp;quot;400&amp;quot; | [[Modem]] with [[DreamPi]]&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; width=&amp;quot;400&amp;quot; | USB [[Coder&#039;s cable]]&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; width=&amp;quot;400&amp;quot; | Serial [[Coder&#039;s cable]]&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Useful for dev? || Yes, supports dcload-ip || Yes, supports dcload-ip,&amp;lt;br/&amp;gt;but BBA is superior and cheaper || No, only useful for online multiplayer gaming || Yes, supports dcload-serial || Yes, supports dcload-serial&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Cost || $100 - $200 and up on used markets || $200 and up on used markets,&amp;lt;br/&amp;gt;due to extreme rarity || Kit prices vary, around $100 || See [[Coder&#039;s cable]] for vendors&amp;lt;br /&amp;gt;darc sells for $45, RetroOnyx sells for $85 || Varies on used markets, uncommonly sold&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Can make DIY? || No || No || Yes || Yes || Yes&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Performance || Up to 100 megabits/s || Up to 10 megabits/s || Up to 56 kilobits/s || Up to 1500 kilobits/s || Up to 120 kilobits/s&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Games support || Some games: Phantasy Star Online, Quake III Arena, Toy Racer, POD SpeedZone, Propellor Arena, Unreal Tournament&amp;lt;br /&amp;gt;Some browsers: Broadband Passport, PlanetWeb 3.0 || No games&amp;lt;br /&amp;gt;One browser: Dream Passport for LAN || All multiplayer games with network support&amp;lt;br /&amp;gt;All web browsers || NO multiplayer games support || NO multiplayer games support &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Homebrew support || Homebrew utilities like dcload-ip || Homebrew utilities like dcload-ip || Homebrew utilities don&#039;t support, only multiplayer games || Homebrew utilities like dcload-serial || Homebrew utilities like dcload-serial &lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=Setting up and compiling the toolchain with the dc-chain script=&lt;br /&gt;
===Dependencies===&lt;br /&gt;
First, we&#039;ll need to install dependencies before building the toolchain. Below we have provided commands to install these dependencies on various systems. Many of the packages will likely already be installed on your system, but we have provided an exhaustive list for good measure.&lt;br /&gt;
====macOS 13 Ventura on an Intel or Apple Silicon processor====&lt;br /&gt;
First, make sure you install Apple Xcode, including the Command Line tools. You will also need to install several other packages for which we&#039;ll include instructions assuming you have installed the [https://brew.sh/ Homebrew] package manager on your system.&lt;br /&gt;
 brew install wget gettext texinfo gmp mpfr libmpc libelf jpeg-turbo libpng cmake meson libisofs&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;Important Note for Apple Silicon users&#039;&#039;: On Apple Silicon, Homebrew installs libraries to a path not included by default by the compiler. If you haven&#039;t added these to your &#039;&#039;&#039;~/.zprofile&#039;&#039;&#039;, then add the following lines now and reload your session (or run them in your Terminal session whenever you compile KOS):&lt;br /&gt;
 export CPATH=/opt/homebrew/include&lt;br /&gt;
 export LIBRARY_PATH=/opt/homebrew/lib&lt;br /&gt;
&lt;br /&gt;
====Debian/Ubuntu-based Linux====&lt;br /&gt;
 sudo apt-get update&lt;br /&gt;
 sudo apt install gawk patch bzip2 tar make libgmp-dev libmpfr-dev libmpc-dev gettext wget libelf-dev texinfo bison flex sed git build-essential diffutils curl libjpeg-dev libpng-dev python3 pkg-config cmake libisofs-dev meson ninja-build rake cmake&lt;br /&gt;
&lt;br /&gt;
====Fedora-based Linux====&lt;br /&gt;
 sudo dnf install gawk patch bzip2 tar make gmp-devel mpfr-devel libmpc-devel gettext wget elfutils-libelf-devel texinfo bison flex sed git diffutils curl libjpeg-turbo-devel libpng-devel gcc-c++ python3 rubygem-rake meson ninja-build cmake&lt;br /&gt;
&lt;br /&gt;
====Arch-based Linux====&lt;br /&gt;
 sudo pacman -S --needed gawk patch bzip2 tar make gmp mpfr libmpc gettext wget libelf texinfo bison flex sed git diffutils curl libjpeg-turbo libpng python3 meson ruby-rake cmake&lt;br /&gt;
&lt;br /&gt;
====Alpine-based Linux====&lt;br /&gt;
 sudo apk --update add build-base patch bash texinfo gmp-dev mpfr-dev libjpeg-turbo-dev libpng-dev elfutils-dev curl wget python3 git ruby-rake cmake fmt&lt;br /&gt;
&lt;br /&gt;
====Other Linux distributions====&lt;br /&gt;
If you&#039;re using a different Linux- or Unix-based system besides the one above, you may need to reference your distribution&#039;s package database and package manager documentation for the equivalent package names and commands necessary for your system.&lt;br /&gt;
&lt;br /&gt;
===Creating a space for your toolchain installation===&lt;br /&gt;
Create the path where we&#039;ll install the toolchain and KOS, and grant it the proper permissions:&lt;br /&gt;
 sudo mkdir -p /opt/toolchains/dc&lt;br /&gt;
 sudo chmod -R 755 /opt/toolchains/dc&lt;br /&gt;
 sudo chown -R $(id -u):$(id -g) /opt/toolchains/dc&lt;br /&gt;
===Cloning the KallistiOS git repository===&lt;br /&gt;
Next, clone the git repository for the latest &#039;&#039;&#039;2.1.1&#039;&#039;&#039; stable version of KallistiOS: &lt;br /&gt;
 git clone https://github.com/KallistiOS/KallistiOS.git -b v2.1.x /opt/toolchains/dc/kos&lt;br /&gt;
&lt;br /&gt;
Alternatively, you may clone the latest unstable version, which contains the latest changes, but may also contain bugs or issues:&lt;br /&gt;
 git clone https://github.com/KallistiOS/KallistiOS.git -b master /opt/toolchains/dc/kos&lt;br /&gt;
&lt;br /&gt;
===Configuring the dc-chain script===&lt;br /&gt;
Enter the dc-chain directory:&lt;br /&gt;
 cd /opt/toolchains/dc/kos/utils/dc-chain&lt;br /&gt;
&lt;br /&gt;
You may now configure the options to your liking by creating a &amp;lt;code&amp;gt;Makefile.cfg&amp;lt;/code&amp;gt; file in this directory. Copy or rename the &amp;lt;code&amp;gt;Makefile.default.cfg&amp;lt;/code&amp;gt; file to &amp;lt;code&amp;gt;Makefile.cfg&amp;lt;/code&amp;gt; and then use a text editor to edit this file&#039;s settings to your liking. For example, you may like to alter the &amp;lt;code&amp;gt;makejobs&amp;lt;/code&amp;gt; option to the number of threads available on your CPU to speed up the compilation. However, if you run into errors during compilation, you may want to set &amp;lt;code&amp;gt;makejobs=1&amp;lt;/code&amp;gt;, as on some operating systems the toolchain may fail to build with a higher setting.&lt;br /&gt;
&lt;br /&gt;
We will leave the default &#039;&#039;&#039;stable&#039;&#039;&#039; profile for the toolchain, which currently uses GCC 13.2. For advanced users, other profiles are available to you; read the &amp;lt;code&amp;gt;README.md&amp;lt;/code&amp;gt; file in the dc-chain directory for more information if you are interested. If you are unsure about options in the &amp;lt;code&amp;gt;Makefile.cfg&amp;lt;/code&amp;gt;, just leave the defaults alone.&lt;br /&gt;
&lt;br /&gt;
===Downloading and compiling the toolchain===&lt;br /&gt;
Now we will run a script to download files and compile the toolchain. At this point, we have the option of building both the main CPU SH4 compiler and the AICA sound processor ARM compiler, or we can skip the ARM compiler and just build the SH4 compiler. Thankfully, KallistiOS includes a prebuilt sound driver, so the ARM compiler is only necessary if you&#039;re wanting to make changes to the sound driver or write custom code to run on the sound processor.&lt;br /&gt;
To build &#039;&#039;&#039;only the SH4 compiler&#039;&#039;&#039;:&lt;br /&gt;
 make&lt;br /&gt;
To build &#039;&#039;&#039;both&#039;&#039;&#039; the SH4 and the ARM compilers:&lt;br /&gt;
 make all&lt;br /&gt;
This will download and unpack the relevant necessary files and then begin the compilation process. The compilation can take anywhere from minutes to a few hours depending on your CPU and number of threads available. When successfully finished, the toolchains will be ready. If you used the commands above, the &#039;&#039;&#039;gdb&#039;&#039;&#039; debugger will have been built as well.&lt;br /&gt;
&lt;br /&gt;
===Cleaning up temporary files===&lt;br /&gt;
After building everything, you can clean up the extraneous files in your dc-chain directory by entering:&lt;br /&gt;
 make clean&lt;br /&gt;
&lt;br /&gt;
=Configuring and compiling KOS and kos-ports=&lt;br /&gt;
===Setting up the environment settings===&lt;br /&gt;
Enter the KOS directory:&lt;br /&gt;
 cd /opt/toolchains/dc/kos&lt;br /&gt;
Copy the pre-made environment script into place:&lt;br /&gt;
 cp doc/environ.sh.sample environ.sh &lt;br /&gt;
For most users, the default settings will suffice. However, advanced users may edit the environ.sh to your liking if you&#039;d like to change compile flags or alter paths. If you&#039;d like to have multiple KOS versions installed or multiple toolchain versions installed, you can set up different environ.sh files corresponding to these different configurations by altering the paths.&lt;br /&gt;
&lt;br /&gt;
You will need to run the following command to apply the KOS environment settings to your currently running shell. Run the following command now, &#039;&#039;&#039;and&#039;&#039;&#039; &#039;&#039;whenever&#039;&#039; you open a new shell to work on Dreamcast projects:&lt;br /&gt;
 . /opt/toolchains/dc/kos/environ.sh&lt;br /&gt;
&lt;br /&gt;
===Building KOS===&lt;br /&gt;
Build KOS:&lt;br /&gt;
 make&lt;br /&gt;
KOS is now built.&lt;br /&gt;
===Building kos-ports===&lt;br /&gt;
Clone the kos-ports repository to your system:&lt;br /&gt;
 git clone --recursive https://github.com/KallistiOS/kos-ports /opt/toolchains/dc/kos-ports&lt;br /&gt;
Run the script to build all of the included ports:&lt;br /&gt;
 /opt/toolchains/dc/kos-ports/utils/build-all.sh&lt;br /&gt;
kos-ports is now built.&lt;br /&gt;
&lt;br /&gt;
===Building the KOS examples===&lt;br /&gt;
Enter the KOS examples directory:&lt;br /&gt;
 cd /opt/toolchains/dc/kos/examples&lt;br /&gt;
Build the examples:&lt;br /&gt;
 make&lt;br /&gt;
All of the example programs provided with KallistiOS are now built.&lt;br /&gt;
&lt;br /&gt;
=Further=&lt;br /&gt;
At this point, you will have a cross-compiler built so that you can generate programs for your Dreamcast from code. KallistiOS, the Dreamcast development library and operating system, is built, as is its associated packages within kos-ports, and all of its example programs.&lt;br /&gt;
&lt;br /&gt;
At this point, you will likely want to view some of the following articles:&lt;br /&gt;
* Setting up an IDE for Dreamcast development&lt;br /&gt;
** [[CLion Debugging]]&lt;br /&gt;
** [[Visual Studio Code]]&lt;br /&gt;
* [[Creating a first project with KallistiOS]] (&#039;&#039;&#039;TODO&#039;&#039;&#039;: Explain how to create a new DC project folder with Makefile, adding an external library, create a basic program, and compile it)&lt;br /&gt;
* [[Using a debug link]]&lt;br /&gt;
* [[Burning an example to CD-R]]&lt;br /&gt;
&lt;br /&gt;
=Running an example program through a debug link=&lt;br /&gt;
&#039;&#039;&#039;TODO&#039;&#039;&#039;: &#039;&#039;Give a tutorial on compiling dcload/dc-tool, setting up a serial, USB, or IP debug link, and running 2ndmix demo on real hardware.&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Download and burn the [[:File:Dcload-2023-06-22.zip|latest versions of dcload-ip or dcload-serial]] -- the IP version includes improved DHCP support, so there is no longer a need to configure things beforehand. &lt;br /&gt;
 &lt;br /&gt;
Run one of the examples from the &amp;lt;code&amp;gt;kos/examples/dreamcast&amp;lt;/code&amp;gt; directory with one of the following commands:&lt;br /&gt;
 dc-tool-ip -t &amp;lt;dreamcast IP address&amp;gt; -x example.elf&lt;br /&gt;
&lt;br /&gt;
 dc-tool-ser -t &amp;lt;serial device path&amp;gt; -x example.elf&lt;br /&gt;
&lt;br /&gt;
Run &amp;lt;code&amp;gt;dc-tool-ip&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;dc-tool-ser&amp;lt;/code&amp;gt; without any parameters to get additional options.&lt;br /&gt;
&lt;br /&gt;
=Burning an example program to CD-R=&lt;br /&gt;
&#039;&#039;&#039;TODO&#039;&#039;&#039;: &#039;&#039;Explain how to build mkdcdisc and write 2ndmix demo to CD-R and run on a Dreamcast console&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[https://gitlab.com/simulant/mkdcdisc mkdcdisc] can be used to easily generate a burnable self-boot CDI image.&lt;br /&gt;
Dependencies required:&lt;br /&gt;
* Homebrew: &amp;lt;code&amp;gt;meson libisofs pkg-config&amp;lt;/code&amp;gt;&lt;br /&gt;
* Debian/Ubuntu: &amp;lt;code&amp;gt;libisofs-dev meson ninja-build&amp;lt;/code&amp;gt;&lt;br /&gt;
* Fedora: &amp;lt;code&amp;gt;meson ninja-build&amp;lt;/code&amp;gt;&lt;br /&gt;
* Arch: &amp;lt;code&amp;gt;meson&amp;lt;/code&amp;gt;&lt;br /&gt;
* Alpine: &amp;lt;code&amp;gt;meson libisofs-dev&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Build &amp;lt;code&amp;gt;mkdcdisc&amp;lt;/code&amp;gt;:&lt;br /&gt;
 git clone https://gitlab.com/simulant/mkdcdisc.git&lt;br /&gt;
 cd mkdcdisc&lt;br /&gt;
 meson setup builddir&lt;br /&gt;
 meson compile -C builddir&lt;br /&gt;
 ./builddir/mkdcdisc -h&lt;br /&gt;
and create a CDI image from your compiled ELF like so:&lt;br /&gt;
 mkdcdisc -e MyProgram.elf -o MyProgram.cdi&lt;br /&gt;
Then you can burn the CDI file using DiscJuggler (Windows-only, but also works through [https://www.winehq.org/ WINE]), ImgBurn with the CDI plugin, or the cdiburn *nix script floating around out there. (document this better)&lt;/div&gt;</summary>
		<author><name>Darc</name></author>
	</entry>
	<entry>
		<id>https://dreamcast.wiki/wiki/index.php?title=Getting_Started_with_Dreamcast_development&amp;diff=3760</id>
		<title>Getting Started with Dreamcast development</title>
		<link rel="alternate" type="text/html" href="https://dreamcast.wiki/wiki/index.php?title=Getting_Started_with_Dreamcast_development&amp;diff=3760"/>
		<updated>2025-06-23T03:48:16Z</updated>

		<summary type="html">&lt;p&gt;Darc: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;div style=&amp;quot;float:right;&amp;quot;&amp;gt;__TOC__&amp;lt;/div&amp;gt;&lt;br /&gt;
=Introduction=&lt;br /&gt;
This article will cover the entire beginning process: starting from zero to having a working dev environment with debug link (serial or IP) and self-booting CD-R. This guide will cover the process for the following platforms:&lt;br /&gt;
* &#039;&#039;&#039;Microsoft Windows 10&#039;&#039;&#039; via [https://learn.microsoft.com/en-us/windows/wsl/about Windows Subsystem for Linux]&lt;br /&gt;
** Users desiring a native Windows approach, see [[DreamSDK]] instead&lt;br /&gt;
* &#039;&#039;&#039;macOS&#039;&#039;&#039; on Intel or Apple Silicon systems with the [https://brew.sh/ Homebrew] package manager installed&lt;br /&gt;
* &#039;&#039;&#039;Linux&#039;&#039;&#039;-based systems&lt;br /&gt;
** &#039;&#039;&#039;Debian&#039;&#039;&#039;- and &#039;&#039;&#039;Ubuntu&#039;&#039;&#039;-based distributions using the default &#039;&#039;&#039;apt&#039;&#039;&#039; package manger&lt;br /&gt;
** &#039;&#039;&#039;Fedora&#039;&#039;&#039;-based distributions using the default &#039;&#039;&#039;dnf&#039;&#039;&#039; package manager&lt;br /&gt;
** &#039;&#039;&#039;Arch&#039;&#039;&#039;-based distributions using the default &#039;&#039;&#039;pacman&#039;&#039;&#039; package manager&lt;br /&gt;
** &#039;&#039;&#039;Alpine&#039;&#039;&#039;-based distributions using the default &#039;&#039;&#039;apk&#039;&#039;&#039; package manager&lt;br /&gt;
&lt;br /&gt;
===Need help?===&lt;br /&gt;
Important note: &#039;&#039;This guide aims to remain up to date and work on all of the above platforms, but keeping instructions for such a variety of platforms up-to-date can be difficult. If you run into any errors or other challenges while following this tutorial, or simply need clarification on any of the steps, feel free to ask for assistance on the [https://dcemulation.org/phpBB/viewforum.php?f=29 message board] or [https://discord.gg/wDEfkUh63h Discord chat] and we would be happy to aid you and update the guide for the benefit of future readers and others in the community.&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
===Terms===&lt;br /&gt;
Before we get started, let&#039;s define several terms:&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;&#039;toolchain&#039;&#039;&#039; is a set of programs which turns your code into an executable file for your Dreamcast console. The toolchain includes:&lt;br /&gt;
* &#039;&#039;&#039;GCC&#039;&#039;&#039;, a compiler which supports C, C++, Objective-C, and more (see also [[Rust on Dreamcast]] and [[Programming language support]])&lt;br /&gt;
* &#039;&#039;&#039;binutils&#039;&#039;&#039;, an assembler and linker&lt;br /&gt;
* &#039;&#039;&#039;newlib&#039;&#039;&#039;, a C library&lt;br /&gt;
* &#039;&#039;&#039;gdb&#039;&#039;&#039;, a debugger&lt;br /&gt;
The toolchain includes a compiler for the Dreamcast&#039;s main SH4 CPU, and optionally a compiler for the ARM-based AICA sound processor. Your operating system may already have versions of these programs installed to compile code for your computer, but we will need to build a &amp;quot;cross-compiler&amp;quot; for compiling specifically for the Dreamcast. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;KallistiOS&#039;&#039;&#039; or &#039;&#039;KOS&#039;&#039; is an open source development library and pseudo-operating system for the Dreamcast console. It is the best documented and most widely used development kit in the homebrew community. KallistiOS&#039;s very flexible license allows both homebrew and commercial use with no restrictions other than a requirement to include credit for its use in your project, and indeed almost all commercially sold indie Dreamcast titles use it. There are others in existence, like [[libronin]] and [[libdream]], as well as the older development kits [[Katana]] and [[Windows CE]] created by Sega and Microsoft for use in retail games, but this guide will only cover the setup and use of KallistiOS. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;kos-ports&#039;&#039;&#039; is a repository including various libraries which integrate with KallistiOS. We will download and compile these libraries as well. &lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;&#039;debug link&#039;&#039;&#039; is a generic term referring to a hardware accessory to facilitate quickly running and debugging your programs. IP-based links include the Dreamcast&#039;s &#039;&#039;&#039;[[Broadband adapter]]&#039;&#039;&#039; and &#039;&#039;&#039;[[LAN adapter]]&#039;&#039;&#039; accessories, and serial-based links include the [[Coder&#039;s cable]], which is a cable that can connect the Dreamcast&#039;s serial port to your computer via USB or serial. This guide includes instructions for setting up and using the the Broadband adapter and a USB-based coder&#039;s cable.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;dc-tool&#039;&#039;&#039; and &#039;&#039;&#039;dcload&#039;&#039;&#039; are a pair of programs to facilitate using a debug link. &#039;&#039;dc-tool&#039;&#039; runs on your computer and links to a Dreamcast running &#039;&#039;dcload-ip&#039;&#039; or &#039;&#039;dcload-serial&#039;&#039;. With this setup, you can quickly load programs, read console feedback, load assets, transfer data, redirect I/O, handle exceptions, debug problems, and so forth.&lt;br /&gt;
&lt;br /&gt;
=Choosing a debug link solution=&lt;br /&gt;
If you are building the toolchain for the purpose of building existing programs from source with little to no modifications, then a debug link setup might not be necessary for you. You may simply build programs to burn directly to CD-R. However, if you are planning to actively develop for the Dreamcast, then a debug link is a critical component. While Dreamcast emulators are mature and accurate enough to play the vast majority of the system&#039;s games library without issue, many critical bugs may show up on a real Dreamcast system, but not on a Dreamcast emulator. Therefore, it is highly recommended to test on a real system as much as possible. It&#039;s also possible to load software off of a [[Serial SD card adapter]], but without an active link to a computer, debugging and stepping through programs as they execute is significantly more challenging.&lt;br /&gt;
&lt;br /&gt;
Presented below is a table comparing the different options available for a debug link. Due to the cost, potential buyers may want to factor in the ability to play multiplayer games with their purchase. Thus, for comparison, we have included information about the [[Modem]] with [[DreamPi]] as well, but understand that the Modem with DreamPi cannot be used as a debug link.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
!colspan=&amp;quot;6&amp;quot; |Comparison of various Dreamcast connectivity options&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#c0c0c0;&amp;quot; width=&amp;quot;150&amp;quot; | Device: &lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; width=&amp;quot;400&amp;quot; | [[Broadband adapter]] (HIT-400 or HIT-401) &amp;lt;br /&amp;gt;Realtek RTL8139C chipset&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; width=&amp;quot;400&amp;quot; | [[LAN adapter]] (HIT-300) &amp;lt;br /&amp;gt;Fujitsu MB86967 chipset&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; width=&amp;quot;400&amp;quot; | [[Modem]] with [[DreamPi]]&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; width=&amp;quot;400&amp;quot; | USB [[Coder&#039;s cable]]&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; width=&amp;quot;400&amp;quot; | Serial [[Coder&#039;s cable]]&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Useful for dev? || Yes, supports dcload-ip || Yes, supports dcload-ip,&amp;lt;br/&amp;gt;but BBA is superior and cheaper || No, only useful for online multiplayer gaming || Yes, supports dcload-serial || Yes, supports dcload-serial&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Cost || $100 - $200 and up on used markets || $200 and up on used markets,&amp;lt;br/&amp;gt;due to extreme rarity || Kit prices vary, around $100 || See [[Coder&#039;s cable]] for vendors&amp;lt;br /&amp;gt;darc sells for $45, RetroOnyx sells for $85 || Varies on used markets, uncommonly sold&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Can make DIY? || No || No || Yes || Yes || Yes&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Performance || Up to 100 megabits/s || Up to 10 megabits/s || Up to 56 kilobits/s || Up to 1500 kilobits/s || Up to 120 kilobits/s&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Games support || Some games: Phantasy Star Online, Quake III Arena, Toy Racer, POD SpeedZone, Propellor Arena, Unreal Tournament&amp;lt;br /&amp;gt;Some browsers: Broadband Passport, PlanetWeb 3.0 || No games&amp;lt;br /&amp;gt;One browser: Dream Passport for LAN || All multiplayer games with network support&amp;lt;br /&amp;gt;All web browsers || NO multiplayer games support || NO multiplayer games support &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Homebrew support || Homebrew utilities like dcload-ip || Homebrew utilities like dcload-ip || Homebrew utilities don&#039;t support, only multiplayer games || Homebrew utilities like dcload-serial || Homebrew utilities like dcload-serial &lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=Setting up and compiling the toolchain with the dc-chain script=&lt;br /&gt;
===Dependencies===&lt;br /&gt;
First, we&#039;ll need to install dependencies before building the toolchain. Below we have provided commands to install these dependencies on various systems. Many of the packages will likely already be installed on your system, but we have provided an exhaustive list for good measure.&lt;br /&gt;
====macOS 13 Ventura on an Intel or Apple Silicon processor====&lt;br /&gt;
First, make sure you install Apple Xcode, including the Command Line tools. You will also need to install several other packages for which we&#039;ll include instructions assuming you have installed the [https://brew.sh/ Homebrew] package manager on your system.&lt;br /&gt;
 brew install wget gettext texinfo gmp mpfr libmpc libelf jpeg-turbo libpng cmake meson libisofs&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;Important Note for Apple Silicon users&#039;&#039;: On Apple Silicon, Homebrew installs libraries to a path not included by default by the compiler. If you haven&#039;t added these to your &#039;&#039;&#039;~/.zprofile&#039;&#039;&#039;, then add the following lines now and reload your session (or run them in your Terminal session whenever you compile KOS):&lt;br /&gt;
 export CPATH=/opt/homebrew/include&lt;br /&gt;
 export LIBRARY_PATH=/opt/homebrew/lib&lt;br /&gt;
&lt;br /&gt;
====Debian/Ubuntu-based Linux====&lt;br /&gt;
 sudo apt-get update&lt;br /&gt;
 sudo apt install gawk patch bzip2 tar make libgmp-dev libmpfr-dev libmpc-dev gettext wget libelf-dev texinfo bison flex sed git build-essential diffutils curl libjpeg-dev libpng-dev python3 pkg-config cmake libisofs-dev meson ninja-build rake cmake&lt;br /&gt;
&lt;br /&gt;
====Fedora-based Linux====&lt;br /&gt;
 sudo dnf install gawk patch bzip2 tar make gmp-devel mpfr-devel libmpc-devel gettext wget elfutils-libelf-devel texinfo bison flex sed git diffutils curl libjpeg-turbo-devel libpng-devel gcc-c++ python3 rubygem-rake meson ninja-build cmake&lt;br /&gt;
&lt;br /&gt;
====Arch-based Linux====&lt;br /&gt;
 sudo pacman -S --needed gawk patch bzip2 tar make gmp mpfr libmpc gettext wget libelf texinfo bison flex sed git diffutils curl libjpeg-turbo libpng python3 meson ruby-rake cmake&lt;br /&gt;
&lt;br /&gt;
====Alpine-based Linux====&lt;br /&gt;
 sudo apk --update add build-base patch bash texinfo gmp-dev mpfr-dev libjpeg-turbo-dev libpng-dev elfutils-dev curl wget python3 git ruby-rake cmake fmt&lt;br /&gt;
&lt;br /&gt;
====Other Linux distributions====&lt;br /&gt;
If you&#039;re using a different Linux- or Unix-based system besides the one above, you may need to reference your distribution&#039;s package database and package manager documentation for the equivalent package names and commands necessary for your system.&lt;br /&gt;
&lt;br /&gt;
===Creating a space for your toolchain installation===&lt;br /&gt;
Create the path where we&#039;ll install the toolchain and KOS, and grant it the proper permissions:&lt;br /&gt;
 sudo mkdir -p /opt/toolchains/dc&lt;br /&gt;
 sudo chmod -R 755 /opt/toolchains/dc&lt;br /&gt;
 sudo chown -R $(id -u):$(id -g) /opt/toolchains/dc&lt;br /&gt;
===Cloning the KallistiOS git repository===&lt;br /&gt;
Next, clone the git repository for the latest &#039;&#039;&#039;2.1.1&#039;&#039;&#039; stable version of KallistiOS: &lt;br /&gt;
 git clone https://github.com/KallistiOS/KallistiOS.git -b v2.1.x /opt/toolchains/dc/kos&lt;br /&gt;
&lt;br /&gt;
Alternatively, you may clone the latest unstable version, which contains the latest changes, but may also contain bugs or issues:&lt;br /&gt;
 git clone https://github.com/KallistiOS/KallistiOS.git -b master /opt/toolchains/dc/kos&lt;br /&gt;
&lt;br /&gt;
===Configuring the dc-chain script===&lt;br /&gt;
Enter the dc-chain directory:&lt;br /&gt;
 cd /opt/toolchains/dc/kos/utils/dc-chain&lt;br /&gt;
&lt;br /&gt;
You may now configure the options to your liking by creating a &amp;lt;code&amp;gt;Makefile.cfg&amp;lt;/code&amp;gt; file in this directory. Copy or rename the &amp;lt;code&amp;gt;Makefile.default.cfg&amp;lt;/code&amp;gt; file to &amp;lt;code&amp;gt;Makefile.cfg&amp;lt;/code&amp;gt; and then use a text editor to edit this file&#039;s settings to your liking. For example, you may like to alter the &amp;lt;code&amp;gt;makejobs&amp;lt;/code&amp;gt; option to the number of threads available on your CPU to speed up the compilation. However, if you run into errors during compilation, you may want to set &amp;lt;code&amp;gt;makejobs=1&amp;lt;/code&amp;gt;, as on some operating systems the toolchain may fail to build with a higher setting.&lt;br /&gt;
&lt;br /&gt;
We will leave the default &#039;&#039;&#039;stable&#039;&#039;&#039; profile for the toolchain, which currently uses GCC 13.2. For advanced users, other profiles are available to you; read the &amp;lt;code&amp;gt;README.md&amp;lt;/code&amp;gt; file in the dc-chain directory for more information if you are interested. If you are unsure about options in the &amp;lt;code&amp;gt;Makefile.cfg&amp;lt;/code&amp;gt;, just leave the defaults alone.&lt;br /&gt;
&lt;br /&gt;
===Downloading and compiling the toolchain===&lt;br /&gt;
Now we will run a script to download files and compile the toolchain. At this point, we have the option of building both the main CPU SH4 compiler and the AICA sound processor ARM compiler, or we can skip the ARM compiler and just build the SH4 compiler. Thankfully, KallistiOS includes a prebuilt sound driver, so the ARM compiler is only necessary if you&#039;re wanting to make changes to the sound driver or write custom code to run on the sound processor.&lt;br /&gt;
To build &#039;&#039;&#039;only the SH4 compiler&#039;&#039;&#039;:&lt;br /&gt;
 make&lt;br /&gt;
To build &#039;&#039;&#039;both&#039;&#039;&#039; the SH4 and the ARM compilers:&lt;br /&gt;
 make all&lt;br /&gt;
This will download and unpack the relevant necessary files and then begin the compilation process. The compilation can take anywhere from minutes to a few hours depending on your CPU and number of threads available. When successfully finished, the toolchains will be ready. If you used the commands above, the &#039;&#039;&#039;gdb&#039;&#039;&#039; debugger will have been built as well.&lt;br /&gt;
&lt;br /&gt;
===Cleaning up temporary files===&lt;br /&gt;
After building everything, you can clean up the extraneous files in your dc-chain directory by entering:&lt;br /&gt;
 make clean&lt;br /&gt;
&lt;br /&gt;
=Configuring and compiling KOS and kos-ports=&lt;br /&gt;
===Setting up the environment settings===&lt;br /&gt;
Enter the KOS directory:&lt;br /&gt;
 cd /opt/toolchains/dc/kos&lt;br /&gt;
Copy the pre-made environment script into place:&lt;br /&gt;
 cp doc/environ.sh.sample environ.sh &lt;br /&gt;
For most users, the default settings will suffice. However, advanced users may edit the environ.sh to your liking if you&#039;d like to change compile flags or alter paths. If you&#039;d like to have multiple KOS versions installed or multiple toolchain versions installed, you can set up different environ.sh files corresponding to these different configurations by altering the paths.&lt;br /&gt;
&lt;br /&gt;
You will need to run the following command to apply the KOS environment settings to your currently running shell. Run the following now, &#039;&#039;&#039;and&#039;&#039;&#039; &#039;&#039;whenever&#039;&#039; you open a new shell to work on Dreamcast projects:&lt;br /&gt;
 . /opt/toolchains/dc/kos/environ.sh&lt;br /&gt;
&lt;br /&gt;
===Building KOS===&lt;br /&gt;
Build KOS:&lt;br /&gt;
 make&lt;br /&gt;
KOS is now built.&lt;br /&gt;
===Building kos-ports===&lt;br /&gt;
Clone the kos-ports repository to your system:&lt;br /&gt;
 git clone --recursive https://github.com/KallistiOS/kos-ports /opt/toolchains/dc/kos-ports&lt;br /&gt;
Run the script to build all of the included ports:&lt;br /&gt;
 /opt/toolchains/dc/kos-ports/utils/build-all.sh&lt;br /&gt;
kos-ports is now built.&lt;br /&gt;
&lt;br /&gt;
===Building the KOS examples===&lt;br /&gt;
Enter the KOS examples directory:&lt;br /&gt;
 cd /opt/toolchains/dc/kos/examples&lt;br /&gt;
Build the examples:&lt;br /&gt;
 make&lt;br /&gt;
All of the example programs provided with KallistiOS are now built.&lt;br /&gt;
&lt;br /&gt;
=Further=&lt;br /&gt;
At this point, you will have a cross-compiler built so that you can generate programs for your Dreamcast from code. KallistiOS, the Dreamcast development library and operating system, is built, as is its associated packages within kos-ports, and all of its example programs.&lt;br /&gt;
&lt;br /&gt;
At this point, you will likely want to view some of the following articles:&lt;br /&gt;
* Setting up an IDE for Dreamcast development&lt;br /&gt;
** [[CLion Debugging]]&lt;br /&gt;
** [[Visual Studio Code]]&lt;br /&gt;
* [[Creating a first project with KallistiOS]] (&#039;&#039;&#039;TODO&#039;&#039;&#039;: Explain how to create a new DC project folder with Makefile, adding an external library, create a basic program, and compile it)&lt;br /&gt;
* [[Using a debug link]]&lt;br /&gt;
* [[Burning an example to CD-R]]&lt;br /&gt;
&lt;br /&gt;
=Running an example program through a debug link=&lt;br /&gt;
&#039;&#039;&#039;TODO&#039;&#039;&#039;: &#039;&#039;Give a tutorial on compiling dcload/dc-tool, setting up a serial, USB, or IP debug link, and running 2ndmix demo on real hardware.&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Download and burn the [[:File:Dcload-2023-06-22.zip|latest versions of dcload-ip or dcload-serial]] -- the IP version includes improved DHCP support, so there is no longer a need to configure things beforehand. &lt;br /&gt;
 &lt;br /&gt;
Run one of the examples from the &amp;lt;code&amp;gt;kos/examples/dreamcast&amp;lt;/code&amp;gt; directory with one of the following commands:&lt;br /&gt;
 dc-tool-ip -t &amp;lt;dreamcast IP address&amp;gt; -x example.elf&lt;br /&gt;
&lt;br /&gt;
 dc-tool-ser -t &amp;lt;serial device path&amp;gt; -x example.elf&lt;br /&gt;
&lt;br /&gt;
Run &amp;lt;code&amp;gt;dc-tool-ip&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;dc-tool-ser&amp;lt;/code&amp;gt; without any parameters to get additional options.&lt;br /&gt;
&lt;br /&gt;
=Burning an example program to CD-R=&lt;br /&gt;
&#039;&#039;&#039;TODO&#039;&#039;&#039;: &#039;&#039;Explain how to build mkdcdisc and write 2ndmix demo to CD-R and run on a Dreamcast console&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[https://gitlab.com/simulant/mkdcdisc mkdcdisc] can be used to easily generate a burnable self-boot CDI image.&lt;br /&gt;
Dependencies required:&lt;br /&gt;
* Homebrew: &amp;lt;code&amp;gt;meson libisofs pkg-config&amp;lt;/code&amp;gt;&lt;br /&gt;
* Debian/Ubuntu: &amp;lt;code&amp;gt;libisofs-dev meson ninja-build&amp;lt;/code&amp;gt;&lt;br /&gt;
* Fedora: &amp;lt;code&amp;gt;meson ninja-build&amp;lt;/code&amp;gt;&lt;br /&gt;
* Arch: &amp;lt;code&amp;gt;meson&amp;lt;/code&amp;gt;&lt;br /&gt;
* Alpine: &amp;lt;code&amp;gt;meson libisofs-dev&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Build &amp;lt;code&amp;gt;mkdcdisc&amp;lt;/code&amp;gt;:&lt;br /&gt;
 git clone https://gitlab.com/simulant/mkdcdisc.git&lt;br /&gt;
 cd mkdcdisc&lt;br /&gt;
 meson setup builddir&lt;br /&gt;
 meson compile -C builddir&lt;br /&gt;
 ./builddir/mkdcdisc -h&lt;br /&gt;
and create a CDI image from your compiled ELF like so:&lt;br /&gt;
 mkdcdisc -e MyProgram.elf -o MyProgram.cdi&lt;br /&gt;
Then you can burn the CDI file using DiscJuggler (Windows-only, but also works through [https://www.winehq.org/ WINE]), ImgBurn with the CDI plugin, or the cdiburn *nix script floating around out there. (document this better)&lt;/div&gt;</summary>
		<author><name>Darc</name></author>
	</entry>
	<entry>
		<id>https://dreamcast.wiki/wiki/index.php?title=Getting_Started_with_Dreamcast_development&amp;diff=3693</id>
		<title>Getting Started with Dreamcast development</title>
		<link rel="alternate" type="text/html" href="https://dreamcast.wiki/wiki/index.php?title=Getting_Started_with_Dreamcast_development&amp;diff=3693"/>
		<updated>2025-03-04T05:00:49Z</updated>

		<summary type="html">&lt;p&gt;Darc: /* Need help? */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;div style=&amp;quot;float:right;&amp;quot;&amp;gt;__TOC__&amp;lt;/div&amp;gt;&lt;br /&gt;
=Introduction=&lt;br /&gt;
This article will cover the entire beginning process: starting from zero to having a working dev environment with debug link (serial or IP) and self-booting CD-R. This guide will cover the process for the following platforms:&lt;br /&gt;
* &#039;&#039;&#039;Microsoft Windows 10&#039;&#039;&#039; via [https://learn.microsoft.com/en-us/windows/wsl/about Windows Subsystem for Linux]&lt;br /&gt;
** Users desiring a native Windows approach, see [[DreamSDK]] instead&lt;br /&gt;
* &#039;&#039;&#039;macOS&#039;&#039;&#039; on Intel or Apple Silicon systems with the [https://brew.sh/ Homebrew] package manager installed&lt;br /&gt;
* &#039;&#039;&#039;Linux&#039;&#039;&#039;-based systems&lt;br /&gt;
** &#039;&#039;&#039;Debian&#039;&#039;&#039;- and &#039;&#039;&#039;Ubuntu&#039;&#039;&#039;-based distributions using the default &#039;&#039;&#039;apt&#039;&#039;&#039; package manger&lt;br /&gt;
** &#039;&#039;&#039;Fedora&#039;&#039;&#039;-based distributions using the default &#039;&#039;&#039;dnf&#039;&#039;&#039; package manager&lt;br /&gt;
** &#039;&#039;&#039;Arch&#039;&#039;&#039;-based distributions using the default &#039;&#039;&#039;pacman&#039;&#039;&#039; package manager&lt;br /&gt;
** &#039;&#039;&#039;Alpine&#039;&#039;&#039;-based distributions using the default &#039;&#039;&#039;apk&#039;&#039;&#039; package manager&lt;br /&gt;
&lt;br /&gt;
===Need help?===&lt;br /&gt;
Important note: &#039;&#039;This guide aims to remain up to date and work on all of the above platforms, but keeping instructions for such a variety of platforms up-to-date can be difficult. If you run into any errors or other challenges while following this tutorial, or simply need clarification on any of the steps, feel free to ask for assistance on the [https://dcemulation.org/phpBB/viewforum.php?f=29 message board] or [https://discord.gg/wDEfkUh63h Discord chat] and we would be happy to aid you and update the guide for the benefit of future readers and others in the community.&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
===Terms===&lt;br /&gt;
Before we get started, let&#039;s define several terms:&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;&#039;toolchain&#039;&#039;&#039; is a set of programs which turns your code into an executable file for your Dreamcast console. The toolchain includes:&lt;br /&gt;
* &#039;&#039;&#039;GCC&#039;&#039;&#039;, a compiler which supports C, C++, Objective-C, and more (see also [[Rust on Dreamcast]] and [[Programming language support]])&lt;br /&gt;
* &#039;&#039;&#039;binutils&#039;&#039;&#039;, an assembler and linker&lt;br /&gt;
* &#039;&#039;&#039;newlib&#039;&#039;&#039;, a C library&lt;br /&gt;
* &#039;&#039;&#039;gdb&#039;&#039;&#039;, a debugger&lt;br /&gt;
The toolchain includes a compiler for the Dreamcast&#039;s main SH4 CPU, and optionally a compiler for the ARM-based AICA sound processor. Your operating system may already have versions of these programs installed to compile code for your computer, but we will need to build a &amp;quot;cross-compiler&amp;quot; for compiling specifically for the Dreamcast. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;KallistiOS&#039;&#039;&#039; or &#039;&#039;KOS&#039;&#039; is an open source development library and pseudo-operating system for the Dreamcast console. It is the best documented and most widely used development kit in the homebrew community. KallistiOS&#039;s very flexible license allows both homebrew and commercial use with no restrictions other than a requirement to include credit for its use in your project, and indeed almost all commercially sold indie Dreamcast titles use it. There are others in existence, like [[libronin]] and [[libdream]], as well as the older development kits [[Katana]] and [[Windows CE]] created by Sega and Microsoft for use in retail games, but this guide will only cover the setup and use of KallistiOS. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;kos-ports&#039;&#039;&#039; is a repository including various libraries which integrate with KallistiOS. We will download and compile these libraries as well. &lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;&#039;debug link&#039;&#039;&#039; is a generic term referring to a hardware accessory to facilitate quickly running and debugging your programs. IP-based links include the Dreamcast&#039;s &#039;&#039;&#039;[[Broadband adapter]]&#039;&#039;&#039; and &#039;&#039;&#039;[[LAN adapter]]&#039;&#039;&#039; accessories, and serial-based links include the [[Coder&#039;s cable]], which is a cable that can connect the Dreamcast&#039;s serial port to your computer via USB or serial. This guide includes instructions for setting up and using the the Broadband adapter and a USB-based coder&#039;s cable.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;dc-tool&#039;&#039;&#039; and &#039;&#039;&#039;dcload&#039;&#039;&#039; are a pair of programs to facilitate using a debug link. &#039;&#039;dc-tool&#039;&#039; runs on your computer and links to a Dreamcast running &#039;&#039;dcload-ip&#039;&#039; or &#039;&#039;dcload-serial&#039;&#039;. With this setup, you can quickly load programs, read console feedback, load assets, transfer data, redirect I/O, handle exceptions, debug problems, and so forth.&lt;br /&gt;
&lt;br /&gt;
=Choosing a debug link solution=&lt;br /&gt;
If you are building the toolchain for the purpose of building existing programs from source with little to no modifications, then a debug link setup might not be necessary for you. You may simply build programs to burn directly to CD-R. However, if you are planning to actively develop for the Dreamcast, then a debug link is a critical component. While Dreamcast emulators are mature and accurate enough to play the vast majority of the system&#039;s games library without issue, many critical bugs may show up on a real Dreamcast system, but not on a Dreamcast emulator. Therefore, it is highly recommended to test on a real system as much as possible. It&#039;s also possible to load software off of a [[Serial SD card adapter]], but without an active link to a computer, debugging and stepping through programs as they execute is significantly more challenging.&lt;br /&gt;
&lt;br /&gt;
Presented below is a table comparing the different options available for a debug link. Due to the cost, potential buyers may want to factor in the ability to play multiplayer games with their purchase. Thus, for comparison, we have included information about the [[Modem]] with [[DreamPi]] as well, but understand that the Modem with DreamPi cannot be used as a debug link.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
!colspan=&amp;quot;6&amp;quot; |Comparison of various Dreamcast connectivity options&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#c0c0c0;&amp;quot; width=&amp;quot;150&amp;quot; | Device: &lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; width=&amp;quot;400&amp;quot; | [[Broadband adapter]] (HIT-400 or HIT-401) &amp;lt;br /&amp;gt;Realtek RTL8139C chipset&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; width=&amp;quot;400&amp;quot; | [[LAN adapter]] (HIT-300) &amp;lt;br /&amp;gt;Fujitsu MB86967 chipset&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; width=&amp;quot;400&amp;quot; | [[Modem]] with [[DreamPi]]&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; width=&amp;quot;400&amp;quot; | USB [[Coder&#039;s cable]]&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; width=&amp;quot;400&amp;quot; | Serial [[Coder&#039;s cable]]&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Useful for dev? || Yes, supports dcload-ip || Yes, supports dcload-ip,&amp;lt;br/&amp;gt;but BBA is superior and cheaper || No, only useful for online multiplayer gaming || Yes, supports dcload-serial || Yes, supports dcload-serial&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Cost || $100 - $200 and up on used markets || $200 and up on used markets,&amp;lt;br/&amp;gt;due to extreme rarity || Kit prices vary, around $100 || See [[Coder&#039;s cable]] for vendors&amp;lt;br /&amp;gt;darc sells for $45, RetroOnyx sells for $85 || Varies on used markets, uncommonly sold&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Can make DIY? || No || No || Yes || Yes || Yes&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Performance || Up to 100 megabits/s || Up to 10 megabits/s || Up to 56 kilobits/s || Up to 1500 kilobits/s || Up to 120 kilobits/s&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Games support || Some games: Phantasy Star Online, Quake III Arena, Toy Racer, POD SpeedZone, Propellor Arena, Unreal Tournament&amp;lt;br /&amp;gt;Some browsers: Broadband Passport, PlanetWeb 3.0 || No games&amp;lt;br /&amp;gt;One browser: Dream Passport for LAN || All multiplayer games with network support&amp;lt;br /&amp;gt;All web browsers || NO multiplayer games support || NO multiplayer games support &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Homebrew support || Homebrew utilities like dcload-ip || Homebrew utilities like dcload-ip || Homebrew utilities don&#039;t support, only multiplayer games || Homebrew utilities like dcload-serial || Homebrew utilities like dcload-serial &lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=Setting up and compiling the toolchain with the dc-chain script=&lt;br /&gt;
===Dependencies===&lt;br /&gt;
First, we&#039;ll need to install dependencies before building the toolchain. Below we have provided commands to install these dependencies on various systems. Many of the packages will likely already be installed on your system, but we have provided an exhaustive list for good measure.&lt;br /&gt;
====macOS 13 Ventura on an Intel or Apple Silicon processor====&lt;br /&gt;
First, make sure you install Apple Xcode, including the Command Line tools. You will also need to install several other packages for which we&#039;ll include instructions assuming you have installed the [https://brew.sh/ Homebrew] package manager on your system.&lt;br /&gt;
 brew install wget gettext texinfo gmp mpfr libmpc libelf jpeg-turbo libpng cmake meson libisofs&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;Important Note for Apple Silicon users&#039;&#039;: On Apple Silicon, Homebrew installs libraries to a path not included by default by the compiler. If you haven&#039;t added these to your &#039;&#039;&#039;~/.zprofile&#039;&#039;&#039;, then add the following lines now and reload your session (or run them in your Terminal session whenever you compile KOS):&lt;br /&gt;
 export CPATH=/opt/homebrew/include&lt;br /&gt;
 export LIBRARY_PATH=/opt/homebrew/lib&lt;br /&gt;
&lt;br /&gt;
====Debian/Ubuntu-based Linux====&lt;br /&gt;
 sudo apt-get update&lt;br /&gt;
 sudo apt install gawk patch bzip2 tar make libgmp-dev libmpfr-dev libmpc-dev gettext wget libelf-dev texinfo bison flex sed git build-essential diffutils curl libjpeg-dev libpng-dev python3 pkg-config cmake libisofs-dev meson ninja-build rake&lt;br /&gt;
&lt;br /&gt;
====Fedora-based Linux====&lt;br /&gt;
 sudo dnf install gawk patch bzip2 tar make gmp-devel mpfr-devel libmpc-devel gettext wget elfutils-libelf-devel texinfo bison flex sed git diffutils curl libjpeg-turbo-devel libpng-devel gcc-c++ python3 rubygem-rake meson ninja-build&lt;br /&gt;
&lt;br /&gt;
====Arch-based Linux====&lt;br /&gt;
 sudo pacman -S --needed gawk patch bzip2 tar make gmp mpfr libmpc gettext wget libelf texinfo bison flex sed git diffutils curl libjpeg-turbo libpng python3 meson ruby-rake&lt;br /&gt;
&lt;br /&gt;
====Alpine-based Linux====&lt;br /&gt;
 sudo apk --update add build-base patch bash texinfo gmp-dev libjpeg-turbo-dev libpng-dev elfutils-dev curl wget python3 git ruby-rake&lt;br /&gt;
&lt;br /&gt;
====Other Linux distributions====&lt;br /&gt;
If you&#039;re using a different Linux- or Unix-based system besides the one above, you may need to reference your distribution&#039;s package database and package manager documentation for the equivalent package names and commands necessary for your system.&lt;br /&gt;
&lt;br /&gt;
===Creating a space for your toolchain installation===&lt;br /&gt;
Create the path where we&#039;ll install the toolchain and KOS, and grant it the proper permissions:&lt;br /&gt;
 sudo mkdir -p /opt/toolchains/dc&lt;br /&gt;
 sudo chmod -R 755 /opt/toolchains/dc&lt;br /&gt;
 sudo chown -R $(id -u):$(id -g) /opt/toolchains/dc&lt;br /&gt;
===Cloning the KallistiOS git repository===&lt;br /&gt;
Next, clone the git repository for the latest &#039;&#039;&#039;2.1.1&#039;&#039;&#039; stable version of KallistiOS: &lt;br /&gt;
 git clone https://github.com/KallistiOS/KallistiOS.git -b v2.1.x /opt/toolchains/dc/kos&lt;br /&gt;
&lt;br /&gt;
Alternatively, you may clone the latest unstable version, which contains the latest changes, but may also contain bugs or issues:&lt;br /&gt;
 git clone https://github.com/KallistiOS/KallistiOS.git -b master /opt/toolchains/dc/kos&lt;br /&gt;
&lt;br /&gt;
===Configuring the dc-chain script===&lt;br /&gt;
Enter the dc-chain directory:&lt;br /&gt;
 cd /opt/toolchains/dc/kos/utils/dc-chain&lt;br /&gt;
&lt;br /&gt;
You may now configure the options to your liking by creating a &amp;lt;code&amp;gt;Makefile.cfg&amp;lt;/code&amp;gt; file in this directory. Copy or rename the &amp;lt;code&amp;gt;Makefile.default.cfg&amp;lt;/code&amp;gt; file to &amp;lt;code&amp;gt;Makefile.cfg&amp;lt;/code&amp;gt; and then use a text editor to edit this file&#039;s settings to your liking. For example, you may like to alter the &amp;lt;code&amp;gt;makejobs&amp;lt;/code&amp;gt; option to the number of threads available on your CPU to speed up the compilation. However, if you run into errors during compilation, you may want to set &amp;lt;code&amp;gt;makejobs=1&amp;lt;/code&amp;gt;, as on some operating systems the toolchain may fail to build with a higher setting.&lt;br /&gt;
&lt;br /&gt;
We will leave the default &#039;&#039;&#039;stable&#039;&#039;&#039; profile for the toolchain, which currently uses GCC 13.2. For advanced users, other profiles are available to you; read the &amp;lt;code&amp;gt;README.md&amp;lt;/code&amp;gt; file in the dc-chain directory for more information if you are interested. If you are unsure about options in the &amp;lt;code&amp;gt;Makefile.cfg&amp;lt;/code&amp;gt;, just leave the defaults alone.&lt;br /&gt;
&lt;br /&gt;
===Downloading and compiling the toolchain===&lt;br /&gt;
Now we will run a script to download files and compile the toolchain. At this point, we have the option of building both the main CPU SH4 compiler and the AICA sound processor ARM compiler, or we can skip the ARM compiler and just build the SH4 compiler. Thankfully, KallistiOS includes a prebuilt sound driver, so the ARM compiler is only necessary if you&#039;re wanting to make changes to the sound driver or write custom code to run on the sound processor.&lt;br /&gt;
To build &#039;&#039;&#039;only the SH4 compiler&#039;&#039;&#039;:&lt;br /&gt;
 make&lt;br /&gt;
To build &#039;&#039;&#039;both&#039;&#039;&#039; the SH4 and the ARM compilers:&lt;br /&gt;
 make all&lt;br /&gt;
This will download and unpack the relevant necessary files and then begin the compilation process. The compilation can take anywhere from minutes to a few hours depending on your CPU and number of threads available. When successfully finished, the toolchains will be ready. If you used the commands above, the &#039;&#039;&#039;gdb&#039;&#039;&#039; debugger will have been built as well.&lt;br /&gt;
&lt;br /&gt;
===Cleaning up temporary files===&lt;br /&gt;
After building everything, you can clean up the extraneous files in your dc-chain directory by entering:&lt;br /&gt;
 make clean&lt;br /&gt;
&lt;br /&gt;
=Configuring and compiling KOS and kos-ports=&lt;br /&gt;
===Setting up the environment settings===&lt;br /&gt;
Enter the KOS directory:&lt;br /&gt;
 cd /opt/toolchains/dc/kos&lt;br /&gt;
Copy the pre-made environment script into place:&lt;br /&gt;
 cp doc/environ.sh.sample environ.sh &lt;br /&gt;
For most users, the default settings will suffice. However, advanced users may edit the environ.sh to your liking if you&#039;d like to change compile flags or alter paths. If you&#039;d like to have multiple KOS versions installed or multiple toolchain versions installed, you can set up different environ.sh files corresponding to these different configurations by altering the paths. Run the source command on the desired environ.sh file to select that configuration prior to compiling your project. &lt;br /&gt;
&lt;br /&gt;
You will need to run the source command to apply the KOS environment settings to your currently running shell. Run the following now, &#039;&#039;&#039;and&#039;&#039;&#039; &#039;&#039;whenever&#039;&#039; you open a new shell to work on Dreamcast projects:&lt;br /&gt;
 source /opt/toolchains/dc/kos/environ.sh&lt;br /&gt;
&lt;br /&gt;
===Building KOS===&lt;br /&gt;
Build KOS:&lt;br /&gt;
 make&lt;br /&gt;
KOS is now built.&lt;br /&gt;
===Building kos-ports===&lt;br /&gt;
Clone the kos-ports repository to your system:&lt;br /&gt;
 git clone --recursive https://github.com/KallistiOS/kos-ports /opt/toolchains/dc/kos-ports&lt;br /&gt;
Run the script to build all of the included ports:&lt;br /&gt;
 /opt/toolchains/dc/kos-ports/utils/build-all.sh&lt;br /&gt;
kos-ports is now built.&lt;br /&gt;
&lt;br /&gt;
===Building the KOS examples===&lt;br /&gt;
Enter the KOS examples directory:&lt;br /&gt;
 cd /opt/toolchains/dc/kos/examples&lt;br /&gt;
Build the examples:&lt;br /&gt;
 make&lt;br /&gt;
All of the example programs provided with KallistiOS are now built.&lt;br /&gt;
&lt;br /&gt;
=Further=&lt;br /&gt;
At this point, you will have a cross-compiler built so that you can generate programs for your Dreamcast from code. KallistiOS, the Dreamcast development library and operating system, is built, as is its associated packages within kos-ports, and all of its example programs.&lt;br /&gt;
&lt;br /&gt;
At this point, you will likely want to view some of the following articles:&lt;br /&gt;
* Setting up an IDE for Dreamcast development&lt;br /&gt;
** [[CLion Debugging]]&lt;br /&gt;
** [[Visual Studio Code]]&lt;br /&gt;
* [[Creating a first project with KallistiOS]] (&#039;&#039;&#039;TODO&#039;&#039;&#039;: Explain how to create a new DC project folder with Makefile, adding an external library, create a basic program, and compile it)&lt;br /&gt;
* [[Using a debug link]]&lt;br /&gt;
* [[Burning an example to CD-R]]&lt;br /&gt;
&lt;br /&gt;
=Running an example program through a debug link=&lt;br /&gt;
&#039;&#039;&#039;TODO&#039;&#039;&#039;: &#039;&#039;Give a tutorial on compiling dcload/dc-tool, setting up a serial, USB, or IP debug link, and running 2ndmix demo on real hardware.&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Download and burn the [[:File:Dcload-2023-06-22.zip|latest versions of dcload-ip or dcload-serial]] -- the IP version includes improved DHCP support, so there is no longer a need to configure things beforehand. &lt;br /&gt;
 &lt;br /&gt;
Run one of the examples from the &amp;lt;code&amp;gt;kos/examples/dreamcast&amp;lt;/code&amp;gt; directory with one of the following commands:&lt;br /&gt;
 dc-tool-ip -t &amp;lt;dreamcast IP address&amp;gt; -x example.elf&lt;br /&gt;
&lt;br /&gt;
 dc-tool-ser -t &amp;lt;serial device path&amp;gt; -x example.elf&lt;br /&gt;
&lt;br /&gt;
Run &amp;lt;code&amp;gt;dc-tool-ip&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;dc-tool-ser&amp;lt;/code&amp;gt; without any parameters to get additional options.&lt;br /&gt;
&lt;br /&gt;
=Burning an example program to CD-R=&lt;br /&gt;
&#039;&#039;&#039;TODO&#039;&#039;&#039;: &#039;&#039;Explain how to build mkdcdisc and write 2ndmix demo to CD-R and run on a Dreamcast console&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[https://gitlab.com/simulant/mkdcdisc mkdcdisc] can be used to easily generate a burnable self-boot CDI image.&lt;br /&gt;
Dependencies required:&lt;br /&gt;
* Homebrew: &amp;lt;code&amp;gt;meson libisofs pkg-config&amp;lt;/code&amp;gt;&lt;br /&gt;
* Debian/Ubuntu:  &amp;lt;code&amp;gt;libisofs-dev meson ninja-build&amp;lt;/code&amp;gt;&lt;br /&gt;
* Fedora: &amp;lt;code&amp;gt;meson ninja-build&amp;lt;/code&amp;gt;&lt;br /&gt;
* Arch: &amp;lt;code&amp;gt;meson&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Build &amp;lt;code&amp;gt;mkdcdisc&amp;lt;/code&amp;gt;:&lt;br /&gt;
 git clone https://gitlab.com/simulant/mkdcdisc.git&lt;br /&gt;
 cd mkdcdisc&lt;br /&gt;
 meson setup builddir&lt;br /&gt;
 meson compile -C builddir&lt;br /&gt;
 ./builddir/mkdcdisc -h&lt;br /&gt;
and create a CDI image from your compiled ELF like so:&lt;br /&gt;
 mkdcdisc -e MyProgram.elf -o MyProgram.cdi&lt;br /&gt;
Then you can burn the CDI file using DiscJuggler (Windows-only, but also works through [https://www.winehq.org/ WINE]), ImgBurn with the CDI plugin, or the cdiburn *nix script floating around out there. (document this better)&lt;/div&gt;</summary>
		<author><name>Darc</name></author>
	</entry>
	<entry>
		<id>https://dreamcast.wiki/wiki/index.php?title=Hardware_variations&amp;diff=3690</id>
		<title>Hardware variations</title>
		<link rel="alternate" type="text/html" href="https://dreamcast.wiki/wiki/index.php?title=Hardware_variations&amp;diff=3690"/>
		<updated>2025-02-24T02:00:12Z</updated>

		<summary type="html">&lt;p&gt;Darc: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The Dreamcast had three major hardware variations, usually referred to as v0, v1, or v2 consoles. U.S. and PAL consoles can be identified easily through the sticker on the bottom of the console, which will have the version number in a circle printed on it. &lt;br /&gt;
&lt;br /&gt;
v0 consoles are known for their heatpipe cooling system, which can be identified by the metal fan shroud from the outside of the console. v0 consoles use 5V levels on the G1 bus, creating a compatibility issue with G1 devices designed for 3.3V levels. v0 units are more common in Japan. These models have a separate GD-ROM daughterboard.&lt;br /&gt;
&lt;br /&gt;
v1 consoles are the most common in the United States and Europe, and are the most compatible with Dreamcast mods. These consoles have plastic fan shrouds, a 3.3V level G1 bus, and a separate GD-ROM daughterboard.&lt;br /&gt;
&lt;br /&gt;
v2 consoles are less common, and have the GD-ROM functionality built directly into the motherboard, so the GD-ROM cannot be replaced with an [[Optical drive replacements|optical drive emulator]] device. They have plastic fan shrouds and 3.3V level G1 bus. Some later v2 consoles came fitted with firmware v1.022, which disables [[MIL-CD]] booting, so CD-based software like homebrew and indie games cannot be used without a [[BIOS modification]].&lt;br /&gt;
&lt;br /&gt;
An unofficial variation of the console is the portable Chinese [[Treamcast]].&lt;/div&gt;</summary>
		<author><name>Darc</name></author>
	</entry>
	<entry>
		<id>https://dreamcast.wiki/wiki/index.php?title=Creating_a_bootable_Dreamcast_disc&amp;diff=3686</id>
		<title>Creating a bootable Dreamcast disc</title>
		<link rel="alternate" type="text/html" href="https://dreamcast.wiki/wiki/index.php?title=Creating_a_bootable_Dreamcast_disc&amp;diff=3686"/>
		<updated>2025-02-15T20:24:32Z</updated>

		<summary type="html">&lt;p&gt;Darc: /* Full Overview of the steps involved */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Full Overview of the steps involved ==&lt;br /&gt;
&lt;br /&gt;
The full list of steps to make self-bootable disc for the [[Dreamcast]] goes like this:&lt;br /&gt;
&lt;br /&gt;
* Build your source (usually using [[KallistiOS]])&lt;br /&gt;
* Transform your .elf executable into binary&lt;br /&gt;
* Scramble the binary&lt;br /&gt;
* Build a .iso image from a directory (representing the &#039;&#039;second&#039;&#039; session of a multisession disc and not bootable)&lt;br /&gt;
* Transform the .iso into a .cdi image (representing a self-booting multisession disc image)&lt;br /&gt;
* Transfer the .cdi image to your GDEMU (or other optical disc drive emulator device), or burn the .cdi image to a CD-R to launch it in a standard Dreamcast.&lt;br /&gt;
&lt;br /&gt;
There are other details that could be taken into account:&lt;br /&gt;
* You can make Audio/Data and Data/Data .cdi files. Both can selfboot, but the files will be organized differently on the physical CD. {{Citation needed}}&lt;br /&gt;
* You&#039;ll need to provide a valid [[IP.BIN]], these can be modified to display a logo during the boot screen, apply patches or execute some code. &lt;br /&gt;
* It is possible to make CDDA compatible images, this process in not currently covered here.&lt;br /&gt;
&lt;br /&gt;
Depending on the method/tools used, some of the above steps are simplified a lot:&lt;br /&gt;
* mkdcdisc: does the heavy lifting for you. Linux only for now.&lt;br /&gt;
* mkisofs + cdi4dc&lt;br /&gt;
* mkisofs + cdrecord&lt;br /&gt;
&lt;br /&gt;
== mkdcdisc (Linux only for now) ==&lt;br /&gt;
&lt;br /&gt;
[https://gitlab.com/simulant/mkdcdisc mkdcdisc] is a recent (2022) tool that pretty much does all the steps for you.&lt;br /&gt;
&lt;br /&gt;
The minimum you need for it, is your executable .elf.&lt;br /&gt;
&lt;br /&gt;
Example to build a bootable DC disc image nehe05.cdi, starting from an executable called nehe05.elf:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
mkdcdisc -e nehe05.elf -o nehe05.cdi&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Example to build a bootable DC disc image mygame.cdi, starting from an executable called mygame.elf, with assets/textures/... files stored in a subfolder called &amp;quot;data&amp;quot;:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
mkdcdisc -e mygame.elf -D data -o mygame.cdi&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There are a lot of commandline options, so be sure to check them all out!&lt;br /&gt;
&lt;br /&gt;
== mkisofs + cdi4dc ==&lt;br /&gt;
&lt;br /&gt;
This is an example for automating the .cdi image process under Linux. Consider this more like a template as you&#039;ll need to edit the paths, names, etc to your own project. &lt;br /&gt;
&lt;br /&gt;
  #! /bin/sh&lt;br /&gt;
  PROJECT_DIR=$PWD/build&lt;br /&gt;
  PROJECT_NAME=&amp;quot;Project_Name&amp;quot;&lt;br /&gt;
  TARGET=&amp;quot;main.elf&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  # Build your program&lt;br /&gt;
  # This assumes that you can properly build your source code. The program main output will be $TARGET&lt;br /&gt;
  make $TARGET&lt;br /&gt;
&lt;br /&gt;
  # Elf transform&lt;br /&gt;
  sh-elf-objcopy -R .stack -O binary $TARGET output.bin&lt;br /&gt;
&lt;br /&gt;
  # Scrambling process&lt;br /&gt;
  $KOS_BASE/utils/scramble/scramble output.bin 1ST_READ.bin&lt;br /&gt;
&lt;br /&gt;
  # Creating a .iso image from a directory &lt;br /&gt;
  # Make sure you have a working [[IP.BIN]] in your current directory. Or change IP.BIN path to wherever you like.&lt;br /&gt;
  # Useful option for mkisofs is *-m* which allow to exclude files from the iso image (useful to remove .git, or some other folder) &lt;br /&gt;
  mkisofs -C 0,11702 -V $PROJECT_NAME -G IP.BIN -r -J -l -o $PROJECT_NAME.iso $DIR&lt;br /&gt;
&lt;br /&gt;
  # Transform your .iso into a .cdi&lt;br /&gt;
  $KOS_BASE/utils/cdi4dc/cdi4dc $PROJECT_NAME.iso $PROJECT_NAME.cdi&lt;br /&gt;
&lt;br /&gt;
== mkisofs + cdrecord  ==&lt;br /&gt;
TODO&lt;/div&gt;</summary>
		<author><name>Darc</name></author>
	</entry>
	<entry>
		<id>https://dreamcast.wiki/wiki/index.php?title=Getting_Started_with_Dreamcast_development&amp;diff=3676</id>
		<title>Getting Started with Dreamcast development</title>
		<link rel="alternate" type="text/html" href="https://dreamcast.wiki/wiki/index.php?title=Getting_Started_with_Dreamcast_development&amp;diff=3676"/>
		<updated>2025-01-12T19:39:01Z</updated>

		<summary type="html">&lt;p&gt;Darc: /* Building kos-ports */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;div style=&amp;quot;float:right;&amp;quot;&amp;gt;__TOC__&amp;lt;/div&amp;gt;&lt;br /&gt;
=Introduction=&lt;br /&gt;
This article will cover the entire beginning process: starting from zero to having a working dev environment with debug link (serial or IP) and self-booting CD-R. This guide will cover the process for the following platforms:&lt;br /&gt;
* &#039;&#039;&#039;Microsoft Windows 10&#039;&#039;&#039; via [https://learn.microsoft.com/en-us/windows/wsl/about Windows Subsystem for Linux]&lt;br /&gt;
** Users desiring a native Windows approach, see [[DreamSDK]] instead&lt;br /&gt;
* &#039;&#039;&#039;macOS&#039;&#039;&#039; on Intel or Apple Silicon systems with the [https://brew.sh/ Homebrew] package manager installed&lt;br /&gt;
* &#039;&#039;&#039;Linux&#039;&#039;&#039;-based systems&lt;br /&gt;
** &#039;&#039;&#039;Debian&#039;&#039;&#039;- and &#039;&#039;&#039;Ubuntu&#039;&#039;&#039;-based distributions using the default &#039;&#039;&#039;apt&#039;&#039;&#039; package manger&lt;br /&gt;
** &#039;&#039;&#039;Fedora&#039;&#039;&#039;-based distributions using the default &#039;&#039;&#039;dnf&#039;&#039;&#039; package manager&lt;br /&gt;
** &#039;&#039;&#039;Arch&#039;&#039;&#039;-based distributions using the default &#039;&#039;&#039;pacman&#039;&#039;&#039; package manager&lt;br /&gt;
** &#039;&#039;&#039;Alpine&#039;&#039;&#039;-based distributions using the default &#039;&#039;&#039;apk&#039;&#039;&#039; package manager&lt;br /&gt;
&lt;br /&gt;
===Need help?===&lt;br /&gt;
Important note: &#039;&#039;This guide aims to remain up to date and work on all of the above platforms, but keeping instructions for such a variety of platforms up-to-date can be difficult. If you run into any errors or other challenges while following this tutorial, or simply need clarification on any of the steps, feel free to ask for assistance on the [https://dcemulation.org/phpBB/viewforum.php?f=29 message board] or [https://discord.gg/RuKmpu7k Discord chat] and we would be happy to aid you and update the guide for the benefit of future readers and others in the community.&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
===Terms===&lt;br /&gt;
Before we get started, let&#039;s define several terms:&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;&#039;toolchain&#039;&#039;&#039; is a set of programs which turns your code into an executable file for your Dreamcast console. The toolchain includes:&lt;br /&gt;
* &#039;&#039;&#039;GCC&#039;&#039;&#039;, a compiler which supports C, C++, Objective-C, and more (see also [[Rust on Dreamcast]] and [[Programming language support]])&lt;br /&gt;
* &#039;&#039;&#039;binutils&#039;&#039;&#039;, an assembler and linker&lt;br /&gt;
* &#039;&#039;&#039;newlib&#039;&#039;&#039;, a C library&lt;br /&gt;
* &#039;&#039;&#039;gdb&#039;&#039;&#039;, a debugger&lt;br /&gt;
The toolchain includes a compiler for the Dreamcast&#039;s main SH4 CPU, and optionally a compiler for the ARM-based AICA sound processor. Your operating system may already have versions of these programs installed to compile code for your computer, but we will need to build a &amp;quot;cross-compiler&amp;quot; for compiling specifically for the Dreamcast. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;KallistiOS&#039;&#039;&#039; or &#039;&#039;KOS&#039;&#039; is an open source development library and pseudo-operating system for the Dreamcast console. It is the best documented and most widely used development kit in the homebrew community. KallistiOS&#039;s very flexible license allows both homebrew and commercial use with no restrictions other than a requirement to include credit for its use in your project, and indeed almost all commercially sold indie Dreamcast titles use it. There are others in existence, like [[libronin]] and [[libdream]], as well as the older development kits [[Katana]] and [[Windows CE]] created by Sega and Microsoft for use in retail games, but this guide will only cover the setup and use of KallistiOS. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;kos-ports&#039;&#039;&#039; is a repository including various libraries which integrate with KallistiOS. We will download and compile these libraries as well. &lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;&#039;debug link&#039;&#039;&#039; is a generic term referring to a hardware accessory to facilitate quickly running and debugging your programs. IP-based links include the Dreamcast&#039;s &#039;&#039;&#039;[[Broadband adapter]]&#039;&#039;&#039; and &#039;&#039;&#039;[[LAN adapter]]&#039;&#039;&#039; accessories, and serial-based links include the [[Coder&#039;s cable]], which is a cable that can connect the Dreamcast&#039;s serial port to your computer via USB or serial. This guide includes instructions for setting up and using the the Broadband adapter and a USB-based coder&#039;s cable.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;dc-tool&#039;&#039;&#039; and &#039;&#039;&#039;dcload&#039;&#039;&#039; are a pair of programs to facilitate using a debug link. &#039;&#039;dc-tool&#039;&#039; runs on your computer and links to a Dreamcast running &#039;&#039;dcload-ip&#039;&#039; or &#039;&#039;dcload-serial&#039;&#039;. With this setup, you can quickly load programs, read console feedback, load assets, transfer data, redirect I/O, handle exceptions, debug problems, and so forth.&lt;br /&gt;
&lt;br /&gt;
=Choosing a debug link solution=&lt;br /&gt;
If you are building the toolchain for the purpose of building existing programs from source with little to no modifications, then a debug link setup might not be necessary for you. You may simply build programs to burn directly to CD-R. However, if you are planning to actively develop for the Dreamcast, then a debug link is a critical component. While Dreamcast emulators are mature and accurate enough to play the vast majority of the system&#039;s games library without issue, many critical bugs may show up on a real Dreamcast system, but not on a Dreamcast emulator. Therefore, it is highly recommended to test on a real system as much as possible. It&#039;s also possible to load software off of a [[Serial SD card adapter]], but without an active link to a computer, debugging and stepping through programs as they execute is significantly more challenging.&lt;br /&gt;
&lt;br /&gt;
Presented below is a table comparing the different options available for a debug link. Due to the cost, potential buyers may want to factor in the ability to play multiplayer games with their purchase. Thus, for comparison, we have included information about the [[Modem]] with [[DreamPi]] as well, but understand that the Modem with DreamPi cannot be used as a debug link.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
!colspan=&amp;quot;6&amp;quot; |Comparison of various Dreamcast connectivity options&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#c0c0c0;&amp;quot; width=&amp;quot;150&amp;quot; | Device: &lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; width=&amp;quot;400&amp;quot; | [[Broadband adapter]] (HIT-400 or HIT-401) &amp;lt;br /&amp;gt;Realtek RTL8139C chipset&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; width=&amp;quot;400&amp;quot; | [[LAN adapter]] (HIT-300) &amp;lt;br /&amp;gt;Fujitsu MB86967 chipset&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; width=&amp;quot;400&amp;quot; | [[Modem]] with [[DreamPi]]&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; width=&amp;quot;400&amp;quot; | USB [[Coder&#039;s cable]]&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; width=&amp;quot;400&amp;quot; | Serial [[Coder&#039;s cable]]&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Useful for dev? || Yes, supports dcload-ip || Yes, supports dcload-ip,&amp;lt;br/&amp;gt;but BBA is superior and cheaper || No, only useful for online multiplayer gaming || Yes, supports dcload-serial || Yes, supports dcload-serial&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Cost || $100 - $200 and up on used markets || $200 and up on used markets,&amp;lt;br/&amp;gt;due to extreme rarity || Kit prices vary, around $100 || See [[Coder&#039;s cable]] for vendors&amp;lt;br /&amp;gt;darc sells for $45, RetroOnyx sells for $85 || Varies on used markets, uncommonly sold&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Can make DIY? || No || No || Yes || Yes || Yes&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Performance || Up to 100 megabits/s || Up to 10 megabits/s || Up to 56 kilobits/s || Up to 1500 kilobits/s || Up to 120 kilobits/s&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Games support || Some games: Phantasy Star Online, Quake III Arena, Toy Racer, POD SpeedZone, Propellor Arena, Unreal Tournament&amp;lt;br /&amp;gt;Some browsers: Broadband Passport, PlanetWeb 3.0 || No games&amp;lt;br /&amp;gt;One browser: Dream Passport for LAN || All multiplayer games with network support&amp;lt;br /&amp;gt;All web browsers || NO multiplayer games support || NO multiplayer games support &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Homebrew support || Homebrew utilities like dcload-ip || Homebrew utilities like dcload-ip || Homebrew utilities don&#039;t support, only multiplayer games || Homebrew utilities like dcload-serial || Homebrew utilities like dcload-serial &lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=Setting up and compiling the toolchain with the dc-chain script=&lt;br /&gt;
===Dependencies===&lt;br /&gt;
First, we&#039;ll need to install dependencies before building the toolchain. Below we have provided commands to install these dependencies on various systems. Many of the packages will likely already be installed on your system, but we have provided an exhaustive list for good measure.&lt;br /&gt;
====macOS 13 Ventura on an Intel or Apple Silicon processor====&lt;br /&gt;
First, make sure you install Apple Xcode, including the Command Line tools. You will also need to install several other packages for which we&#039;ll include instructions assuming you have installed the [https://brew.sh/ Homebrew] package manager on your system.&lt;br /&gt;
 brew install wget gettext texinfo gmp mpfr libmpc libelf jpeg-turbo libpng cmake meson libisofs&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;Important Note for Apple Silicon users&#039;&#039;: On Apple Silicon, Homebrew installs libraries to a path not included by default by the compiler. If you haven&#039;t added these to your &#039;&#039;&#039;~/.zprofile&#039;&#039;&#039;, then add the following lines now and reload your session (or run them in your Terminal session whenever you compile KOS):&lt;br /&gt;
 export CPATH=/opt/homebrew/include&lt;br /&gt;
 export LIBRARY_PATH=/opt/homebrew/lib&lt;br /&gt;
&lt;br /&gt;
====Debian/Ubuntu-based Linux====&lt;br /&gt;
 sudo apt-get update&lt;br /&gt;
 sudo apt install gawk patch bzip2 tar make libgmp-dev libmpfr-dev libmpc-dev gettext wget libelf-dev texinfo bison flex sed git build-essential diffutils curl libjpeg-dev libpng-dev python3 pkg-config cmake libisofs-dev meson ninja-build rake&lt;br /&gt;
&lt;br /&gt;
====Fedora-based Linux====&lt;br /&gt;
 sudo dnf install gawk patch bzip2 tar make gmp-devel mpfr-devel libmpc-devel gettext wget elfutils-libelf-devel texinfo bison flex sed git diffutils curl libjpeg-turbo-devel libpng-devel gcc-c++ python3 rubygem-rake meson ninja-build&lt;br /&gt;
&lt;br /&gt;
====Arch-based Linux====&lt;br /&gt;
 sudo pacman -S --needed gawk patch bzip2 tar make gmp mpfr libmpc gettext wget libelf texinfo bison flex sed git diffutils curl libjpeg-turbo libpng python3 meson ruby-rake&lt;br /&gt;
&lt;br /&gt;
====Alpine-based Linux====&lt;br /&gt;
 sudo apk --update add build-base patch bash texinfo gmp-dev libjpeg-turbo-dev libpng-dev elfutils-dev curl wget python3 git ruby-rake&lt;br /&gt;
&lt;br /&gt;
====Other Linux distributions====&lt;br /&gt;
If you&#039;re using a different Linux- or Unix-based system besides the one above, you may need to reference your distribution&#039;s package database and package manager documentation for the equivalent package names and commands necessary for your system.&lt;br /&gt;
&lt;br /&gt;
===Creating a space for your toolchain installation===&lt;br /&gt;
Create the path where we&#039;ll install the toolchain and KOS, and grant it the proper permissions:&lt;br /&gt;
 sudo mkdir -p /opt/toolchains/dc&lt;br /&gt;
 sudo chmod -R 755 /opt/toolchains/dc&lt;br /&gt;
 sudo chown -R $(id -u):$(id -g) /opt/toolchains/dc&lt;br /&gt;
===Cloning the KallistiOS git repository===&lt;br /&gt;
Next, clone the git repository for the latest &#039;&#039;&#039;2.1.1&#039;&#039;&#039; stable version of KallistiOS: &lt;br /&gt;
 git clone https://github.com/KallistiOS/KallistiOS.git -b v2.1.x /opt/toolchains/dc/kos&lt;br /&gt;
&lt;br /&gt;
Alternatively, you may clone the latest unstable version, which contains the latest changes, but may also contain bugs or issues:&lt;br /&gt;
 git clone https://github.com/KallistiOS/KallistiOS.git -b master /opt/toolchains/dc/kos&lt;br /&gt;
&lt;br /&gt;
===Configuring the dc-chain script===&lt;br /&gt;
Enter the dc-chain directory:&lt;br /&gt;
 cd /opt/toolchains/dc/kos/utils/dc-chain&lt;br /&gt;
&lt;br /&gt;
You may now configure the options to your liking by creating a &amp;lt;code&amp;gt;Makefile.cfg&amp;lt;/code&amp;gt; file in this directory. Copy or rename the &amp;lt;code&amp;gt;Makefile.default.cfg&amp;lt;/code&amp;gt; file to &amp;lt;code&amp;gt;Makefile.cfg&amp;lt;/code&amp;gt; and then use a text editor to edit this file&#039;s settings to your liking. For example, you may like to alter the &amp;lt;code&amp;gt;makejobs&amp;lt;/code&amp;gt; option to the number of threads available on your CPU to speed up the compilation. However, if you run into errors during compilation, you may want to set &amp;lt;code&amp;gt;makejobs=1&amp;lt;/code&amp;gt;, as on some operating systems the toolchain may fail to build with a higher setting.&lt;br /&gt;
&lt;br /&gt;
We will leave the default &#039;&#039;&#039;stable&#039;&#039;&#039; profile for the toolchain, which currently uses GCC 13.2. For advanced users, other profiles are available to you; read the &amp;lt;code&amp;gt;README.md&amp;lt;/code&amp;gt; file in the dc-chain directory for more information if you are interested. If you are unsure about options in the &amp;lt;code&amp;gt;Makefile.cfg&amp;lt;/code&amp;gt;, just leave the defaults alone.&lt;br /&gt;
&lt;br /&gt;
===Downloading and compiling the toolchain===&lt;br /&gt;
Now we will run a script to download files and compile the toolchain. At this point, we have the option of building both the main CPU SH4 compiler and the AICA sound processor ARM compiler, or we can skip the ARM compiler and just build the SH4 compiler. Thankfully, KallistiOS includes a prebuilt sound driver, so the ARM compiler is only necessary if you&#039;re wanting to make changes to the sound driver or write custom code to run on the sound processor.&lt;br /&gt;
To build &#039;&#039;&#039;only the SH4 compiler&#039;&#039;&#039;:&lt;br /&gt;
 make&lt;br /&gt;
To build &#039;&#039;&#039;both&#039;&#039;&#039; the SH4 and the ARM compilers:&lt;br /&gt;
 make all&lt;br /&gt;
This will download and unpack the relevant necessary files and then begin the compilation process. The compilation can take anywhere from minutes to a few hours depending on your CPU and number of threads available. When successfully finished, the toolchains will be ready. If you used the commands above, the &#039;&#039;&#039;gdb&#039;&#039;&#039; debugger will have been built as well.&lt;br /&gt;
&lt;br /&gt;
===Cleaning up temporary files===&lt;br /&gt;
After building everything, you can clean up the extraneous files in your dc-chain directory by entering:&lt;br /&gt;
 make clean&lt;br /&gt;
&lt;br /&gt;
=Configuring and compiling KOS and kos-ports=&lt;br /&gt;
===Setting up the environment settings===&lt;br /&gt;
Enter the KOS directory:&lt;br /&gt;
 cd /opt/toolchains/dc/kos&lt;br /&gt;
Copy the pre-made environment script into place:&lt;br /&gt;
 cp doc/environ.sh.sample environ.sh &lt;br /&gt;
For most users, the default settings will suffice. However, advanced users may edit the environ.sh to your liking if you&#039;d like to change compile flags or alter paths. If you&#039;d like to have multiple KOS versions installed or multiple toolchain versions installed, you can set up different environ.sh files corresponding to these different configurations by altering the paths. Run the source command on the desired environ.sh file to select that configuration prior to compiling your project. &lt;br /&gt;
&lt;br /&gt;
You will need to run the source command to apply the KOS environment settings to your currently running shell. Run the following now, &#039;&#039;&#039;and&#039;&#039;&#039; &#039;&#039;whenever&#039;&#039; you open a new shell to work on Dreamcast projects:&lt;br /&gt;
 source /opt/toolchains/dc/kos/environ.sh&lt;br /&gt;
&lt;br /&gt;
===Building KOS===&lt;br /&gt;
Build KOS:&lt;br /&gt;
 make&lt;br /&gt;
KOS is now built.&lt;br /&gt;
===Building kos-ports===&lt;br /&gt;
Clone the kos-ports repository to your system:&lt;br /&gt;
 git clone --recursive https://github.com/KallistiOS/kos-ports /opt/toolchains/dc/kos-ports&lt;br /&gt;
Run the script to build all of the included ports:&lt;br /&gt;
 /opt/toolchains/dc/kos-ports/utils/build-all.sh&lt;br /&gt;
kos-ports is now built.&lt;br /&gt;
&lt;br /&gt;
===Building the KOS examples===&lt;br /&gt;
Enter the KOS examples directory:&lt;br /&gt;
 cd /opt/toolchains/dc/kos/examples&lt;br /&gt;
Build the examples:&lt;br /&gt;
 make&lt;br /&gt;
All of the example programs provided with KallistiOS are now built.&lt;br /&gt;
&lt;br /&gt;
=Further=&lt;br /&gt;
At this point, you will have a cross-compiler built so that you can generate programs for your Dreamcast from code. KallistiOS, the Dreamcast development library and operating system, is built, as is its associated packages within kos-ports, and all of its example programs.&lt;br /&gt;
&lt;br /&gt;
At this point, you will likely want to view some of the following articles:&lt;br /&gt;
* Setting up an IDE for Dreamcast development&lt;br /&gt;
** [[CLion Debugging]]&lt;br /&gt;
** [[Visual Studio Code]]&lt;br /&gt;
* [[Creating a first project with KallistiOS]] (&#039;&#039;&#039;TODO&#039;&#039;&#039;: Explain how to create a new DC project folder with Makefile, adding an external library, create a basic program, and compile it)&lt;br /&gt;
* [[Using a debug link]]&lt;br /&gt;
* [[Burning an example to CD-R]]&lt;br /&gt;
&lt;br /&gt;
=Running an example program through a debug link=&lt;br /&gt;
&#039;&#039;&#039;TODO&#039;&#039;&#039;: &#039;&#039;Give a tutorial on compiling dcload/dc-tool, setting up a serial, USB, or IP debug link, and running 2ndmix demo on real hardware.&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Download and burn the [[:File:Dcload-2023-06-22.zip|latest versions of dcload-ip or dcload-serial]] -- the IP version includes improved DHCP support, so there is no longer a need to configure things beforehand. &lt;br /&gt;
 &lt;br /&gt;
Run one of the examples from the &amp;lt;code&amp;gt;kos/examples/dreamcast&amp;lt;/code&amp;gt; directory with one of the following commands:&lt;br /&gt;
 dc-tool-ip -t &amp;lt;dreamcast IP address&amp;gt; -x example.elf&lt;br /&gt;
&lt;br /&gt;
 dc-tool-ser -t &amp;lt;serial device path&amp;gt; -x example.elf&lt;br /&gt;
&lt;br /&gt;
Run &amp;lt;code&amp;gt;dc-tool-ip&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;dc-tool-ser&amp;lt;/code&amp;gt; without any parameters to get additional options.&lt;br /&gt;
&lt;br /&gt;
=Burning an example program to CD-R=&lt;br /&gt;
&#039;&#039;&#039;TODO&#039;&#039;&#039;: &#039;&#039;Explain how to build mkdcdisc and write 2ndmix demo to CD-R and run on a Dreamcast console&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[https://gitlab.com/simulant/mkdcdisc mkdcdisc] can be used to easily generate a burnable self-boot CDI image.&lt;br /&gt;
Dependencies required:&lt;br /&gt;
* Homebrew: &amp;lt;code&amp;gt;meson libisofs pkg-config&amp;lt;/code&amp;gt;&lt;br /&gt;
* Debian/Ubuntu:  &amp;lt;code&amp;gt;libisofs-dev meson ninja-build&amp;lt;/code&amp;gt;&lt;br /&gt;
* Fedora: &amp;lt;code&amp;gt;meson ninja-build&amp;lt;/code&amp;gt;&lt;br /&gt;
* Arch: &amp;lt;code&amp;gt;meson&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Build &amp;lt;code&amp;gt;mkdcdisc&amp;lt;/code&amp;gt;:&lt;br /&gt;
 git clone https://gitlab.com/simulant/mkdcdisc.git&lt;br /&gt;
 cd mkdcdisc&lt;br /&gt;
 meson setup builddir&lt;br /&gt;
 meson compile -C builddir&lt;br /&gt;
 ./builddir/mkdcdisc -h&lt;br /&gt;
and create a CDI image from your compiled ELF like so:&lt;br /&gt;
 mkdcdisc -e MyProgram.elf -o MyProgram.cdi&lt;br /&gt;
Then you can burn the CDI file using DiscJuggler (Windows-only, but also works through [https://www.winehq.org/ WINE]), ImgBurn with the CDI plugin, or the cdiburn *nix script floating around out there. (document this better)&lt;/div&gt;</summary>
		<author><name>Darc</name></author>
	</entry>
	<entry>
		<id>https://dreamcast.wiki/wiki/index.php?title=Dcload-ip&amp;diff=3675</id>
		<title>Dcload-ip</title>
		<link rel="alternate" type="text/html" href="https://dreamcast.wiki/wiki/index.php?title=Dcload-ip&amp;diff=3675"/>
		<updated>2025-01-11T18:59:11Z</updated>

		<summary type="html">&lt;p&gt;Darc: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;dcload-ip is a homebrew program originally developed by Andrew Kieschnick, now maintained by [[KallistiOS]] developers and by [[SiZiOUS]] at the following repositories:&lt;br /&gt;
&lt;br /&gt;
* [https://github.com/KallistiOS/dcload-serial KallistiOS dcload-ip repository]&lt;br /&gt;
* [https://github.com/sizious/dcload-ip SiZiOUS dcload-ip repository with enhancements]&lt;br /&gt;
&lt;br /&gt;
It is the sister application to [[dcload-serial]].&lt;br /&gt;
&lt;br /&gt;
It allows a developer to use a [[Broadband adapter]] or [[LAN adapter]] as a link to send, execute, and debug code on a Sega Dreamcast console. It is typically loaded via CD-R, but can also be loaded directly through the BIOS (for example, using [https://github.com/darcagn/DreamDash DreamDash]).&lt;br /&gt;
&lt;br /&gt;
It is able to transfer .bin and .elf files (and srec files, if specially configured for it) over the BBA. It also provides chainloaded programs with a standard syscall interface for communicating with a networked PC. The program is mostly intended for homebrew development, but can also be used with special programs designed to make use of it like [https://github.com/zeldin/dc-virtcd Marcus Comstedt&#039;s dc-virtcd].&lt;br /&gt;
&lt;br /&gt;
DiscJuggler images of the latest DC side software as of June 22, 2023 are available to download: [[:File:Dcload-2023-06-22.zip|DiscJuggler images of dcload-ip 2.0.1 and dcload-serial 1.0.6]]&lt;br /&gt;
&lt;br /&gt;
{{DISPLAYTITLE:dcload-ip}}&lt;/div&gt;</summary>
		<author><name>Darc</name></author>
	</entry>
	<entry>
		<id>https://dreamcast.wiki/wiki/index.php?title=Dcload-serial&amp;diff=3674</id>
		<title>Dcload-serial</title>
		<link rel="alternate" type="text/html" href="https://dreamcast.wiki/wiki/index.php?title=Dcload-serial&amp;diff=3674"/>
		<updated>2025-01-11T18:55:15Z</updated>

		<summary type="html">&lt;p&gt;Darc: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;dcload-serial is a homebrew program originally developed by Andrew Kieschnick, now maintained by [[KallistiOS]] developers at the [https://github.com/KallistiOS/dcload-serial dcload-serial repository]. It is the sister application to [[dcload-ip]].&lt;br /&gt;
&lt;br /&gt;
It allows a developer to use a serial cable (also referred to as a &amp;quot;[[coder&#039;s cable]]&amp;quot;) as a link to send, execute, and debug code on a Sega Dreamcast console. It is typically loaded via CD-R, but can also be loaded directly through the BIOS (for example, using [https://github.com/darcagn/DreamDash DreamDash]).&lt;br /&gt;
&lt;br /&gt;
It is able to transfer .bin and .elf files (and srec files, if specially configured for it) over the serial port. It also provides chainloaded programs with a standard syscall interface for communicating with a connected PC. The program is mostly intended for homebrew development, but can also be used with special programs.&lt;br /&gt;
&lt;br /&gt;
DiscJuggler images of the latest DC side software as of June 22, 2023 are available to download: [[:File:Dcload-2023-06-22.zip|DiscJuggler images of dcload-ip 2.0.1 and dcload-serial 1.0.6]]&lt;br /&gt;
&lt;br /&gt;
{{DISPLAYTITLE:dcload-serial}}&lt;/div&gt;</summary>
		<author><name>Darc</name></author>
	</entry>
	<entry>
		<id>https://dreamcast.wiki/wiki/index.php?title=Dcload-serial&amp;diff=3673</id>
		<title>Dcload-serial</title>
		<link rel="alternate" type="text/html" href="https://dreamcast.wiki/wiki/index.php?title=Dcload-serial&amp;diff=3673"/>
		<updated>2025-01-11T18:54:34Z</updated>

		<summary type="html">&lt;p&gt;Darc: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;dcload-serial is a homebrew program originally developped by Andrew Kieschnick, now maintained by [[KallistiOS]] developers at the [https://github.com/KallistiOS/dcload-serial dcload-serial repository]. It is the sister application to [[dcload-ip]].&lt;br /&gt;
&lt;br /&gt;
It allows a developer to use a serial cable (also referred to as a &amp;quot;[[coder&#039;s cable]]&amp;quot;) as a link to send, execute, and debug code on a Sega Dreamcast console. It is typically loaded via CD-R, but can also be loaded directly through the BIOS (for example, using [https://github.com/darcagn/DreamDash DreamDash]).&lt;br /&gt;
&lt;br /&gt;
It is able to transfer .bin and .elf files (and srec files, if specially configured for it) over the serial port. It also provides chainloaded programs with a standard syscall interface for communicating with a connected PC. The program is mostly intended for homebrew development, but can also be used with special programs.&lt;br /&gt;
&lt;br /&gt;
DiscJuggler images of the latest DC side software as of June 22, 2023 are available to download: [[:File:Dcload-2023-06-22.zip|DiscJuggler images of dcload-ip 2.0.1 and dcload-serial 1.0.6]]&lt;br /&gt;
&lt;br /&gt;
{{DISPLAYTITLE:dcload-serial}}&lt;/div&gt;</summary>
		<author><name>Darc</name></author>
	</entry>
	<entry>
		<id>https://dreamcast.wiki/wiki/index.php?title=Rust_on_Dreamcast&amp;diff=3670</id>
		<title>Rust on Dreamcast</title>
		<link rel="alternate" type="text/html" href="https://dreamcast.wiki/wiki/index.php?title=Rust_on_Dreamcast&amp;diff=3670"/>
		<updated>2025-01-02T01:19:15Z</updated>

		<summary type="html">&lt;p&gt;Darc: Replaced content with &amp;quot;Ferris holding his Dreamcast controller  &amp;#039;&amp;#039;&amp;#039;Please visit [https://dreamcast.rs/ dreamcast.rs]!&amp;#039;&amp;#039;&amp;#039;  The Rust for Dreamcast documentation has now [https://dreamcast.rs been migrated into an mdBook]!&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[File:Rust-dc-logo.png|thumb|Ferris holding his Dreamcast controller]]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Please visit [https://dreamcast.rs/ dreamcast.rs]!&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The Rust for Dreamcast documentation has now [https://dreamcast.rs been migrated into an mdBook]!&lt;/div&gt;</summary>
		<author><name>Darc</name></author>
	</entry>
	<entry>
		<id>https://dreamcast.wiki/wiki/index.php?title=Getting_Started_with_Dreamcast_development&amp;diff=3669</id>
		<title>Getting Started with Dreamcast development</title>
		<link rel="alternate" type="text/html" href="https://dreamcast.wiki/wiki/index.php?title=Getting_Started_with_Dreamcast_development&amp;diff=3669"/>
		<updated>2025-01-01T21:46:50Z</updated>

		<summary type="html">&lt;p&gt;Darc: /* Cloning the KallistiOS git repository */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;div style=&amp;quot;float:right;&amp;quot;&amp;gt;__TOC__&amp;lt;/div&amp;gt;&lt;br /&gt;
=Introduction=&lt;br /&gt;
This article will cover the entire beginning process: starting from zero to having a working dev environment with debug link (serial or IP) and self-booting CD-R. This guide will cover the process for the following platforms:&lt;br /&gt;
* &#039;&#039;&#039;Microsoft Windows 10&#039;&#039;&#039; via [https://learn.microsoft.com/en-us/windows/wsl/about Windows Subsystem for Linux]&lt;br /&gt;
** Users desiring a native Windows approach, see [[DreamSDK]] instead&lt;br /&gt;
* &#039;&#039;&#039;macOS&#039;&#039;&#039; on Intel or Apple Silicon systems with the [https://brew.sh/ Homebrew] package manager installed&lt;br /&gt;
* &#039;&#039;&#039;Linux&#039;&#039;&#039;-based systems&lt;br /&gt;
** &#039;&#039;&#039;Debian&#039;&#039;&#039;- and &#039;&#039;&#039;Ubuntu&#039;&#039;&#039;-based distributions using the default &#039;&#039;&#039;apt&#039;&#039;&#039; package manger&lt;br /&gt;
** &#039;&#039;&#039;Fedora&#039;&#039;&#039;-based distributions using the default &#039;&#039;&#039;dnf&#039;&#039;&#039; package manager&lt;br /&gt;
** &#039;&#039;&#039;Arch&#039;&#039;&#039;-based distributions using the default &#039;&#039;&#039;pacman&#039;&#039;&#039; package manager&lt;br /&gt;
** &#039;&#039;&#039;Alpine&#039;&#039;&#039;-based distributions using the default &#039;&#039;&#039;apk&#039;&#039;&#039; package manager&lt;br /&gt;
&lt;br /&gt;
===Need help?===&lt;br /&gt;
Important note: &#039;&#039;This guide aims to remain up to date and work on all of the above platforms, but keeping instructions for such a variety of platforms up-to-date can be difficult. If you run into any errors or other challenges while following this tutorial, or simply need clarification on any of the steps, feel free to ask for assistance on the [https://dcemulation.org/phpBB/viewforum.php?f=29 message board] or [https://discord.gg/RuKmpu7k Discord chat] and we would be happy to aid you and update the guide for the benefit of future readers and others in the community.&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
===Terms===&lt;br /&gt;
Before we get started, let&#039;s define several terms:&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;&#039;toolchain&#039;&#039;&#039; is a set of programs which turns your code into an executable file for your Dreamcast console. The toolchain includes:&lt;br /&gt;
* &#039;&#039;&#039;GCC&#039;&#039;&#039;, a compiler which supports C, C++, Objective-C, and more (see also [[Rust on Dreamcast]] and [[Programming language support]])&lt;br /&gt;
* &#039;&#039;&#039;binutils&#039;&#039;&#039;, an assembler and linker&lt;br /&gt;
* &#039;&#039;&#039;newlib&#039;&#039;&#039;, a C library&lt;br /&gt;
* &#039;&#039;&#039;gdb&#039;&#039;&#039;, a debugger&lt;br /&gt;
The toolchain includes a compiler for the Dreamcast&#039;s main SH4 CPU, and optionally a compiler for the ARM-based AICA sound processor. Your operating system may already have versions of these programs installed to compile code for your computer, but we will need to build a &amp;quot;cross-compiler&amp;quot; for compiling specifically for the Dreamcast. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;KallistiOS&#039;&#039;&#039; or &#039;&#039;KOS&#039;&#039; is an open source development library and pseudo-operating system for the Dreamcast console. It is the best documented and most widely used development kit in the homebrew community. KallistiOS&#039;s very flexible license allows both homebrew and commercial use with no restrictions other than a requirement to include credit for its use in your project, and indeed almost all commercially sold indie Dreamcast titles use it. There are others in existence, like [[libronin]] and [[libdream]], as well as the older development kits [[Katana]] and [[Windows CE]] created by Sega and Microsoft for use in retail games, but this guide will only cover the setup and use of KallistiOS. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;kos-ports&#039;&#039;&#039; is a repository including various libraries which integrate with KallistiOS. We will download and compile these libraries as well. &lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;&#039;debug link&#039;&#039;&#039; is a generic term referring to a hardware accessory to facilitate quickly running and debugging your programs. IP-based links include the Dreamcast&#039;s &#039;&#039;&#039;[[Broadband adapter]]&#039;&#039;&#039; and &#039;&#039;&#039;[[LAN adapter]]&#039;&#039;&#039; accessories, and serial-based links include the [[Coder&#039;s cable]], which is a cable that can connect the Dreamcast&#039;s serial port to your computer via USB or serial. This guide includes instructions for setting up and using the the Broadband adapter and a USB-based coder&#039;s cable.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;dc-tool&#039;&#039;&#039; and &#039;&#039;&#039;dcload&#039;&#039;&#039; are a pair of programs to facilitate using a debug link. &#039;&#039;dc-tool&#039;&#039; runs on your computer and links to a Dreamcast running &#039;&#039;dcload-ip&#039;&#039; or &#039;&#039;dcload-serial&#039;&#039;. With this setup, you can quickly load programs, read console feedback, load assets, transfer data, redirect I/O, handle exceptions, debug problems, and so forth.&lt;br /&gt;
&lt;br /&gt;
=Choosing a debug link solution=&lt;br /&gt;
If you are building the toolchain for the purpose of building existing programs from source with little to no modifications, then a debug link setup might not be necessary for you. You may simply build programs to burn directly to CD-R. However, if you are planning to actively develop for the Dreamcast, then a debug link is a critical component. While Dreamcast emulators are mature and accurate enough to play the vast majority of the system&#039;s games library without issue, many critical bugs may show up on a real Dreamcast system, but not on a Dreamcast emulator. Therefore, it is highly recommended to test on a real system as much as possible. It&#039;s also possible to load software off of a [[Serial SD card adapter]], but without an active link to a computer, debugging and stepping through programs as they execute is significantly more challenging.&lt;br /&gt;
&lt;br /&gt;
Presented below is a table comparing the different options available for a debug link. Due to the cost, potential buyers may want to factor in the ability to play multiplayer games with their purchase. Thus, for comparison, we have included information about the [[Modem]] with [[DreamPi]] as well, but understand that the Modem with DreamPi cannot be used as a debug link.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
!colspan=&amp;quot;6&amp;quot; |Comparison of various Dreamcast connectivity options&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#c0c0c0;&amp;quot; width=&amp;quot;150&amp;quot; | Device: &lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; width=&amp;quot;400&amp;quot; | [[Broadband adapter]] (HIT-400 or HIT-401) &amp;lt;br /&amp;gt;Realtek RTL8139C chipset&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; width=&amp;quot;400&amp;quot; | [[LAN adapter]] (HIT-300) &amp;lt;br /&amp;gt;Fujitsu MB86967 chipset&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; width=&amp;quot;400&amp;quot; | [[Modem]] with [[DreamPi]]&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; width=&amp;quot;400&amp;quot; | USB [[Coder&#039;s cable]]&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; width=&amp;quot;400&amp;quot; | Serial [[Coder&#039;s cable]]&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Useful for dev? || Yes, supports dcload-ip || Yes, supports dcload-ip,&amp;lt;br/&amp;gt;but BBA is superior and cheaper || No, only useful for online multiplayer gaming || Yes, supports dcload-serial || Yes, supports dcload-serial&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Cost || $100 - $200 and up on used markets || $200 and up on used markets,&amp;lt;br/&amp;gt;due to extreme rarity || Kit prices vary, around $100 || See [[Coder&#039;s cable]] for vendors&amp;lt;br /&amp;gt;darc sells for $45, RetroOnyx sells for $85 || Varies on used markets, uncommonly sold&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Can make DIY? || No || No || Yes || Yes || Yes&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Performance || Up to 100 megabits/s || Up to 10 megabits/s || Up to 56 kilobits/s || Up to 1500 kilobits/s || Up to 120 kilobits/s&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Games support || Some games: Phantasy Star Online, Quake III Arena, Toy Racer, POD SpeedZone, Propellor Arena, Unreal Tournament&amp;lt;br /&amp;gt;Some browsers: Broadband Passport, PlanetWeb 3.0 || No games&amp;lt;br /&amp;gt;One browser: Dream Passport for LAN || All multiplayer games with network support&amp;lt;br /&amp;gt;All web browsers || NO multiplayer games support || NO multiplayer games support &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Homebrew support || Homebrew utilities like dcload-ip || Homebrew utilities like dcload-ip || Homebrew utilities don&#039;t support, only multiplayer games || Homebrew utilities like dcload-serial || Homebrew utilities like dcload-serial &lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=Setting up and compiling the toolchain with the dc-chain script=&lt;br /&gt;
===Dependencies===&lt;br /&gt;
First, we&#039;ll need to install dependencies before building the toolchain. Below we have provided commands to install these dependencies on various systems. Many of the packages will likely already be installed on your system, but we have provided an exhaustive list for good measure.&lt;br /&gt;
====macOS 13 Ventura on an Intel or Apple Silicon processor====&lt;br /&gt;
First, make sure you install Apple Xcode, including the Command Line tools. You will also need to install several other packages for which we&#039;ll include instructions assuming you have installed the [https://brew.sh/ Homebrew] package manager on your system.&lt;br /&gt;
 brew install wget gettext texinfo gmp mpfr libmpc libelf jpeg-turbo libpng cmake meson libisofs&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;Important Note for Apple Silicon users&#039;&#039;: On Apple Silicon, Homebrew installs libraries to a path not included by default by the compiler. If you haven&#039;t added these to your &#039;&#039;&#039;~/.zprofile&#039;&#039;&#039;, then add the following lines now and reload your session (or run them in your Terminal session whenever you compile KOS):&lt;br /&gt;
 export CPATH=/opt/homebrew/include&lt;br /&gt;
 export LIBRARY_PATH=/opt/homebrew/lib&lt;br /&gt;
&lt;br /&gt;
====Debian/Ubuntu-based Linux====&lt;br /&gt;
 sudo apt-get update&lt;br /&gt;
 sudo apt install gawk patch bzip2 tar make libgmp-dev libmpfr-dev libmpc-dev gettext wget libelf-dev texinfo bison flex sed git build-essential diffutils curl libjpeg-dev libpng-dev python3 pkg-config cmake libisofs-dev meson ninja-build rake&lt;br /&gt;
&lt;br /&gt;
====Fedora-based Linux====&lt;br /&gt;
 sudo dnf install gawk patch bzip2 tar make gmp-devel mpfr-devel libmpc-devel gettext wget elfutils-libelf-devel texinfo bison flex sed git diffutils curl libjpeg-turbo-devel libpng-devel gcc-c++ python3 rubygem-rake meson ninja-build&lt;br /&gt;
&lt;br /&gt;
====Arch-based Linux====&lt;br /&gt;
 sudo pacman -S --needed gawk patch bzip2 tar make gmp mpfr libmpc gettext wget libelf texinfo bison flex sed git diffutils curl libjpeg-turbo libpng python3 meson ruby-rake&lt;br /&gt;
&lt;br /&gt;
====Alpine-based Linux====&lt;br /&gt;
 sudo apk --update add build-base patch bash texinfo gmp-dev libjpeg-turbo-dev libpng-dev elfutils-dev curl wget python3 git ruby-rake&lt;br /&gt;
&lt;br /&gt;
====Other Linux distributions====&lt;br /&gt;
If you&#039;re using a different Linux- or Unix-based system besides the one above, you may need to reference your distribution&#039;s package database and package manager documentation for the equivalent package names and commands necessary for your system.&lt;br /&gt;
&lt;br /&gt;
===Creating a space for your toolchain installation===&lt;br /&gt;
Create the path where we&#039;ll install the toolchain and KOS, and grant it the proper permissions:&lt;br /&gt;
 sudo mkdir -p /opt/toolchains/dc&lt;br /&gt;
 sudo chmod -R 755 /opt/toolchains/dc&lt;br /&gt;
 sudo chown -R $(id -u):$(id -g) /opt/toolchains/dc&lt;br /&gt;
===Cloning the KallistiOS git repository===&lt;br /&gt;
Next, clone the git repository for the latest &#039;&#039;&#039;2.1.1&#039;&#039;&#039; stable version of KallistiOS: &lt;br /&gt;
 git clone https://github.com/KallistiOS/KallistiOS.git -b v2.1.x /opt/toolchains/dc/kos&lt;br /&gt;
&lt;br /&gt;
Alternatively, you may clone the latest unstable version, which contains the latest changes, but may also contain bugs or issues:&lt;br /&gt;
 git clone https://github.com/KallistiOS/KallistiOS.git -b master /opt/toolchains/dc/kos&lt;br /&gt;
&lt;br /&gt;
===Configuring the dc-chain script===&lt;br /&gt;
Enter the dc-chain directory:&lt;br /&gt;
 cd /opt/toolchains/dc/kos/utils/dc-chain&lt;br /&gt;
&lt;br /&gt;
You may now configure the options to your liking by creating a &amp;lt;code&amp;gt;Makefile.cfg&amp;lt;/code&amp;gt; file in this directory. Copy or rename the &amp;lt;code&amp;gt;Makefile.default.cfg&amp;lt;/code&amp;gt; file to &amp;lt;code&amp;gt;Makefile.cfg&amp;lt;/code&amp;gt; and then use a text editor to edit this file&#039;s settings to your liking. For example, you may like to alter the &amp;lt;code&amp;gt;makejobs&amp;lt;/code&amp;gt; option to the number of threads available on your CPU to speed up the compilation. However, if you run into errors during compilation, you may want to set &amp;lt;code&amp;gt;makejobs=1&amp;lt;/code&amp;gt;, as on some operating systems the toolchain may fail to build with a higher setting.&lt;br /&gt;
&lt;br /&gt;
We will leave the default &#039;&#039;&#039;stable&#039;&#039;&#039; profile for the toolchain, which currently uses GCC 13.2. For advanced users, other profiles are available to you; read the &amp;lt;code&amp;gt;README.md&amp;lt;/code&amp;gt; file in the dc-chain directory for more information if you are interested. If you are unsure about options in the &amp;lt;code&amp;gt;Makefile.cfg&amp;lt;/code&amp;gt;, just leave the defaults alone.&lt;br /&gt;
&lt;br /&gt;
===Downloading and compiling the toolchain===&lt;br /&gt;
Now we will run a script to download files and compile the toolchain. At this point, we have the option of building both the main CPU SH4 compiler and the AICA sound processor ARM compiler, or we can skip the ARM compiler and just build the SH4 compiler. Thankfully, KallistiOS includes a prebuilt sound driver, so the ARM compiler is only necessary if you&#039;re wanting to make changes to the sound driver or write custom code to run on the sound processor.&lt;br /&gt;
To build &#039;&#039;&#039;only the SH4 compiler&#039;&#039;&#039;:&lt;br /&gt;
 make&lt;br /&gt;
To build &#039;&#039;&#039;both&#039;&#039;&#039; the SH4 and the ARM compilers:&lt;br /&gt;
 make all&lt;br /&gt;
This will download and unpack the relevant necessary files and then begin the compilation process. The compilation can take anywhere from minutes to a few hours depending on your CPU and number of threads available. When successfully finished, the toolchains will be ready. If you used the commands above, the &#039;&#039;&#039;gdb&#039;&#039;&#039; debugger will have been built as well.&lt;br /&gt;
&lt;br /&gt;
===Cleaning up temporary files===&lt;br /&gt;
After building everything, you can clean up the extraneous files in your dc-chain directory by entering:&lt;br /&gt;
 make clean&lt;br /&gt;
&lt;br /&gt;
=Configuring and compiling KOS and kos-ports=&lt;br /&gt;
===Setting up the environment settings===&lt;br /&gt;
Enter the KOS directory:&lt;br /&gt;
 cd /opt/toolchains/dc/kos&lt;br /&gt;
Copy the pre-made environment script into place:&lt;br /&gt;
 cp doc/environ.sh.sample environ.sh &lt;br /&gt;
For most users, the default settings will suffice. However, advanced users may edit the environ.sh to your liking if you&#039;d like to change compile flags or alter paths. If you&#039;d like to have multiple KOS versions installed or multiple toolchain versions installed, you can set up different environ.sh files corresponding to these different configurations by altering the paths. Run the source command on the desired environ.sh file to select that configuration prior to compiling your project. &lt;br /&gt;
&lt;br /&gt;
You will need to run the source command to apply the KOS environment settings to your currently running shell. Run the following now, &#039;&#039;&#039;and&#039;&#039;&#039; &#039;&#039;whenever&#039;&#039; you open a new shell to work on Dreamcast projects:&lt;br /&gt;
 source /opt/toolchains/dc/kos/environ.sh&lt;br /&gt;
&lt;br /&gt;
===Building KOS===&lt;br /&gt;
Build KOS:&lt;br /&gt;
 make&lt;br /&gt;
KOS is now built.&lt;br /&gt;
===Building kos-ports===&lt;br /&gt;
Clone the kos-ports repository to your system:&lt;br /&gt;
 git clone --recursive https://github.com/KallistiOS/kos-ports /opt/toolchains/dc/kos-ports&lt;br /&gt;
Run the script to build all of the included ports:&lt;br /&gt;
 /opt/toolchains/dc/kos-ports/utils/build-all.sh&lt;br /&gt;
kos-ports is now built.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;NOTE&#039;&#039;&#039;: If you see the following error:&lt;br /&gt;
 1 error(s) occurred during the build process:&lt;br /&gt;
 /opt/toolchains/dc/kos/../kos-ports/libjimtcl: Build failed with return code 2&lt;br /&gt;
The error is safe to ignore as the &amp;lt;code&amp;gt;libjimtcl&amp;lt;/code&amp;gt; kos-port is broken at this time. The rest should compile without issue with the default KOS settings.&lt;br /&gt;
&lt;br /&gt;
===Building the KOS examples===&lt;br /&gt;
Enter the KOS examples directory:&lt;br /&gt;
 cd /opt/toolchains/dc/kos/examples&lt;br /&gt;
Build the examples:&lt;br /&gt;
 make&lt;br /&gt;
All of the example programs provided with KallistiOS are now built.&lt;br /&gt;
&lt;br /&gt;
=Further=&lt;br /&gt;
At this point, you will have a cross-compiler built so that you can generate programs for your Dreamcast from code. KallistiOS, the Dreamcast development library and operating system, is built, as is its associated packages within kos-ports, and all of its example programs.&lt;br /&gt;
&lt;br /&gt;
At this point, you will likely want to view some of the following articles:&lt;br /&gt;
* Setting up an IDE for Dreamcast development&lt;br /&gt;
** [[CLion Debugging]]&lt;br /&gt;
** [[Visual Studio Code]]&lt;br /&gt;
* [[Creating a first project with KallistiOS]] (&#039;&#039;&#039;TODO&#039;&#039;&#039;: Explain how to create a new DC project folder with Makefile, adding an external library, create a basic program, and compile it)&lt;br /&gt;
* [[Using a debug link]]&lt;br /&gt;
* [[Burning an example to CD-R]]&lt;br /&gt;
&lt;br /&gt;
=Running an example program through a debug link=&lt;br /&gt;
&#039;&#039;&#039;TODO&#039;&#039;&#039;: &#039;&#039;Give a tutorial on compiling dcload/dc-tool, setting up a serial, USB, or IP debug link, and running 2ndmix demo on real hardware.&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Download and burn the [[:File:Dcload-2023-06-22.zip|latest versions of dcload-ip or dcload-serial]] -- the IP version includes improved DHCP support, so there is no longer a need to configure things beforehand. &lt;br /&gt;
 &lt;br /&gt;
Run one of the examples from the &amp;lt;code&amp;gt;kos/examples/dreamcast&amp;lt;/code&amp;gt; directory with one of the following commands:&lt;br /&gt;
 dc-tool-ip -t &amp;lt;dreamcast IP address&amp;gt; -x example.elf&lt;br /&gt;
&lt;br /&gt;
 dc-tool-ser -t &amp;lt;serial device path&amp;gt; -x example.elf&lt;br /&gt;
&lt;br /&gt;
Run &amp;lt;code&amp;gt;dc-tool-ip&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;dc-tool-ser&amp;lt;/code&amp;gt; without any parameters to get additional options.&lt;br /&gt;
&lt;br /&gt;
=Burning an example program to CD-R=&lt;br /&gt;
&#039;&#039;&#039;TODO&#039;&#039;&#039;: &#039;&#039;Explain how to build mkdcdisc and write 2ndmix demo to CD-R and run on a Dreamcast console&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[https://gitlab.com/simulant/mkdcdisc mkdcdisc] can be used to easily generate a burnable self-boot CDI image.&lt;br /&gt;
Dependencies required:&lt;br /&gt;
* Homebrew: &amp;lt;code&amp;gt;meson libisofs pkg-config&amp;lt;/code&amp;gt;&lt;br /&gt;
* Debian/Ubuntu:  &amp;lt;code&amp;gt;libisofs-dev meson ninja-build&amp;lt;/code&amp;gt;&lt;br /&gt;
* Fedora: &amp;lt;code&amp;gt;meson ninja-build&amp;lt;/code&amp;gt;&lt;br /&gt;
* Arch: &amp;lt;code&amp;gt;meson&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Build &amp;lt;code&amp;gt;mkdcdisc&amp;lt;/code&amp;gt;:&lt;br /&gt;
 git clone https://gitlab.com/simulant/mkdcdisc.git&lt;br /&gt;
 cd mkdcdisc&lt;br /&gt;
 meson setup builddir&lt;br /&gt;
 meson compile -C builddir&lt;br /&gt;
 ./builddir/mkdcdisc -h&lt;br /&gt;
and create a CDI image from your compiled ELF like so:&lt;br /&gt;
 mkdcdisc -e MyProgram.elf -o MyProgram.cdi&lt;br /&gt;
Then you can burn the CDI file using DiscJuggler (Windows-only, but also works through [https://www.winehq.org/ WINE]), ImgBurn with the CDI plugin, or the cdiburn *nix script floating around out there. (document this better)&lt;/div&gt;</summary>
		<author><name>Darc</name></author>
	</entry>
	<entry>
		<id>https://dreamcast.wiki/wiki/index.php?title=Getting_Started_with_Dreamcast_development&amp;diff=3668</id>
		<title>Getting Started with Dreamcast development</title>
		<link rel="alternate" type="text/html" href="https://dreamcast.wiki/wiki/index.php?title=Getting_Started_with_Dreamcast_development&amp;diff=3668"/>
		<updated>2024-12-31T05:43:27Z</updated>

		<summary type="html">&lt;p&gt;Darc: /* Building kos-ports */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;div style=&amp;quot;float:right;&amp;quot;&amp;gt;__TOC__&amp;lt;/div&amp;gt;&lt;br /&gt;
=Introduction=&lt;br /&gt;
This article will cover the entire beginning process: starting from zero to having a working dev environment with debug link (serial or IP) and self-booting CD-R. This guide will cover the process for the following platforms:&lt;br /&gt;
* &#039;&#039;&#039;Microsoft Windows 10&#039;&#039;&#039; via [https://learn.microsoft.com/en-us/windows/wsl/about Windows Subsystem for Linux]&lt;br /&gt;
** Users desiring a native Windows approach, see [[DreamSDK]] instead&lt;br /&gt;
* &#039;&#039;&#039;macOS&#039;&#039;&#039; on Intel or Apple Silicon systems with the [https://brew.sh/ Homebrew] package manager installed&lt;br /&gt;
* &#039;&#039;&#039;Linux&#039;&#039;&#039;-based systems&lt;br /&gt;
** &#039;&#039;&#039;Debian&#039;&#039;&#039;- and &#039;&#039;&#039;Ubuntu&#039;&#039;&#039;-based distributions using the default &#039;&#039;&#039;apt&#039;&#039;&#039; package manger&lt;br /&gt;
** &#039;&#039;&#039;Fedora&#039;&#039;&#039;-based distributions using the default &#039;&#039;&#039;dnf&#039;&#039;&#039; package manager&lt;br /&gt;
** &#039;&#039;&#039;Arch&#039;&#039;&#039;-based distributions using the default &#039;&#039;&#039;pacman&#039;&#039;&#039; package manager&lt;br /&gt;
** &#039;&#039;&#039;Alpine&#039;&#039;&#039;-based distributions using the default &#039;&#039;&#039;apk&#039;&#039;&#039; package manager&lt;br /&gt;
&lt;br /&gt;
===Need help?===&lt;br /&gt;
Important note: &#039;&#039;This guide aims to remain up to date and work on all of the above platforms, but keeping instructions for such a variety of platforms up-to-date can be difficult. If you run into any errors or other challenges while following this tutorial, or simply need clarification on any of the steps, feel free to ask for assistance on the [https://dcemulation.org/phpBB/viewforum.php?f=29 message board] or [https://discord.gg/RuKmpu7k Discord chat] and we would be happy to aid you and update the guide for the benefit of future readers and others in the community.&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
===Terms===&lt;br /&gt;
Before we get started, let&#039;s define several terms:&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;&#039;toolchain&#039;&#039;&#039; is a set of programs which turns your code into an executable file for your Dreamcast console. The toolchain includes:&lt;br /&gt;
* &#039;&#039;&#039;GCC&#039;&#039;&#039;, a compiler which supports C, C++, Objective-C, and more (see also [[Rust on Dreamcast]] and [[Programming language support]])&lt;br /&gt;
* &#039;&#039;&#039;binutils&#039;&#039;&#039;, an assembler and linker&lt;br /&gt;
* &#039;&#039;&#039;newlib&#039;&#039;&#039;, a C library&lt;br /&gt;
* &#039;&#039;&#039;gdb&#039;&#039;&#039;, a debugger&lt;br /&gt;
The toolchain includes a compiler for the Dreamcast&#039;s main SH4 CPU, and optionally a compiler for the ARM-based AICA sound processor. Your operating system may already have versions of these programs installed to compile code for your computer, but we will need to build a &amp;quot;cross-compiler&amp;quot; for compiling specifically for the Dreamcast. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;KallistiOS&#039;&#039;&#039; or &#039;&#039;KOS&#039;&#039; is an open source development library and pseudo-operating system for the Dreamcast console. It is the best documented and most widely used development kit in the homebrew community. KallistiOS&#039;s very flexible license allows both homebrew and commercial use with no restrictions other than a requirement to include credit for its use in your project, and indeed almost all commercially sold indie Dreamcast titles use it. There are others in existence, like [[libronin]] and [[libdream]], as well as the older development kits [[Katana]] and [[Windows CE]] created by Sega and Microsoft for use in retail games, but this guide will only cover the setup and use of KallistiOS. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;kos-ports&#039;&#039;&#039; is a repository including various libraries which integrate with KallistiOS. We will download and compile these libraries as well. &lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;&#039;debug link&#039;&#039;&#039; is a generic term referring to a hardware accessory to facilitate quickly running and debugging your programs. IP-based links include the Dreamcast&#039;s &#039;&#039;&#039;[[Broadband adapter]]&#039;&#039;&#039; and &#039;&#039;&#039;[[LAN adapter]]&#039;&#039;&#039; accessories, and serial-based links include the [[Coder&#039;s cable]], which is a cable that can connect the Dreamcast&#039;s serial port to your computer via USB or serial. This guide includes instructions for setting up and using the the Broadband adapter and a USB-based coder&#039;s cable.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;dc-tool&#039;&#039;&#039; and &#039;&#039;&#039;dcload&#039;&#039;&#039; are a pair of programs to facilitate using a debug link. &#039;&#039;dc-tool&#039;&#039; runs on your computer and links to a Dreamcast running &#039;&#039;dcload-ip&#039;&#039; or &#039;&#039;dcload-serial&#039;&#039;. With this setup, you can quickly load programs, read console feedback, load assets, transfer data, redirect I/O, handle exceptions, debug problems, and so forth.&lt;br /&gt;
&lt;br /&gt;
=Choosing a debug link solution=&lt;br /&gt;
If you are building the toolchain for the purpose of building existing programs from source with little to no modifications, then a debug link setup might not be necessary for you. You may simply build programs to burn directly to CD-R. However, if you are planning to actively develop for the Dreamcast, then a debug link is a critical component. While Dreamcast emulators are mature and accurate enough to play the vast majority of the system&#039;s games library without issue, many critical bugs may show up on a real Dreamcast system, but not on a Dreamcast emulator. Therefore, it is highly recommended to test on a real system as much as possible. It&#039;s also possible to load software off of a [[Serial SD card adapter]], but without an active link to a computer, debugging and stepping through programs as they execute is significantly more challenging.&lt;br /&gt;
&lt;br /&gt;
Presented below is a table comparing the different options available for a debug link. Due to the cost, potential buyers may want to factor in the ability to play multiplayer games with their purchase. Thus, for comparison, we have included information about the [[Modem]] with [[DreamPi]] as well, but understand that the Modem with DreamPi cannot be used as a debug link.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
!colspan=&amp;quot;6&amp;quot; |Comparison of various Dreamcast connectivity options&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#c0c0c0;&amp;quot; width=&amp;quot;150&amp;quot; | Device: &lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; width=&amp;quot;400&amp;quot; | [[Broadband adapter]] (HIT-400 or HIT-401) &amp;lt;br /&amp;gt;Realtek RTL8139C chipset&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; width=&amp;quot;400&amp;quot; | [[LAN adapter]] (HIT-300) &amp;lt;br /&amp;gt;Fujitsu MB86967 chipset&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; width=&amp;quot;400&amp;quot; | [[Modem]] with [[DreamPi]]&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; width=&amp;quot;400&amp;quot; | USB [[Coder&#039;s cable]]&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; width=&amp;quot;400&amp;quot; | Serial [[Coder&#039;s cable]]&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Useful for dev? || Yes, supports dcload-ip || Yes, supports dcload-ip,&amp;lt;br/&amp;gt;but BBA is superior and cheaper || No, only useful for online multiplayer gaming || Yes, supports dcload-serial || Yes, supports dcload-serial&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Cost || $100 - $200 and up on used markets || $200 and up on used markets,&amp;lt;br/&amp;gt;due to extreme rarity || Kit prices vary, around $100 || See [[Coder&#039;s cable]] for vendors&amp;lt;br /&amp;gt;darc sells for $45, RetroOnyx sells for $85 || Varies on used markets, uncommonly sold&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Can make DIY? || No || No || Yes || Yes || Yes&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Performance || Up to 100 megabits/s || Up to 10 megabits/s || Up to 56 kilobits/s || Up to 1500 kilobits/s || Up to 120 kilobits/s&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Games support || Some games: Phantasy Star Online, Quake III Arena, Toy Racer, POD SpeedZone, Propellor Arena, Unreal Tournament&amp;lt;br /&amp;gt;Some browsers: Broadband Passport, PlanetWeb 3.0 || No games&amp;lt;br /&amp;gt;One browser: Dream Passport for LAN || All multiplayer games with network support&amp;lt;br /&amp;gt;All web browsers || NO multiplayer games support || NO multiplayer games support &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Homebrew support || Homebrew utilities like dcload-ip || Homebrew utilities like dcload-ip || Homebrew utilities don&#039;t support, only multiplayer games || Homebrew utilities like dcload-serial || Homebrew utilities like dcload-serial &lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=Setting up and compiling the toolchain with the dc-chain script=&lt;br /&gt;
===Dependencies===&lt;br /&gt;
First, we&#039;ll need to install dependencies before building the toolchain. Below we have provided commands to install these dependencies on various systems. Many of the packages will likely already be installed on your system, but we have provided an exhaustive list for good measure.&lt;br /&gt;
====macOS 13 Ventura on an Intel or Apple Silicon processor====&lt;br /&gt;
First, make sure you install Apple Xcode, including the Command Line tools. You will also need to install several other packages for which we&#039;ll include instructions assuming you have installed the [https://brew.sh/ Homebrew] package manager on your system.&lt;br /&gt;
 brew install wget gettext texinfo gmp mpfr libmpc libelf jpeg-turbo libpng cmake meson libisofs&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;Important Note for Apple Silicon users&#039;&#039;: On Apple Silicon, Homebrew installs libraries to a path not included by default by the compiler. If you haven&#039;t added these to your &#039;&#039;&#039;~/.zprofile&#039;&#039;&#039;, then add the following lines now and reload your session (or run them in your Terminal session whenever you compile KOS):&lt;br /&gt;
 export CPATH=/opt/homebrew/include&lt;br /&gt;
 export LIBRARY_PATH=/opt/homebrew/lib&lt;br /&gt;
&lt;br /&gt;
====Debian/Ubuntu-based Linux====&lt;br /&gt;
 sudo apt-get update&lt;br /&gt;
 sudo apt install gawk patch bzip2 tar make libgmp-dev libmpfr-dev libmpc-dev gettext wget libelf-dev texinfo bison flex sed git build-essential diffutils curl libjpeg-dev libpng-dev python3 pkg-config cmake libisofs-dev meson ninja-build rake&lt;br /&gt;
&lt;br /&gt;
====Fedora-based Linux====&lt;br /&gt;
 sudo dnf install gawk patch bzip2 tar make gmp-devel mpfr-devel libmpc-devel gettext wget elfutils-libelf-devel texinfo bison flex sed git diffutils curl libjpeg-turbo-devel libpng-devel gcc-c++ python3 rubygem-rake meson ninja-build&lt;br /&gt;
&lt;br /&gt;
====Arch-based Linux====&lt;br /&gt;
 sudo pacman -S --needed gawk patch bzip2 tar make gmp mpfr libmpc gettext wget libelf texinfo bison flex sed git diffutils curl libjpeg-turbo libpng python3 meson ruby-rake&lt;br /&gt;
&lt;br /&gt;
====Alpine-based Linux====&lt;br /&gt;
 sudo apk --update add build-base patch bash texinfo gmp-dev libjpeg-turbo-dev libpng-dev elfutils-dev curl wget python3 git ruby-rake&lt;br /&gt;
&lt;br /&gt;
====Other Linux distributions====&lt;br /&gt;
If you&#039;re using a different Linux- or Unix-based system besides the one above, you may need to reference your distribution&#039;s package database and package manager documentation for the equivalent package names and commands necessary for your system.&lt;br /&gt;
&lt;br /&gt;
===Creating a space for your toolchain installation===&lt;br /&gt;
Create the path where we&#039;ll install the toolchain and KOS, and grant it the proper permissions:&lt;br /&gt;
 sudo mkdir -p /opt/toolchains/dc&lt;br /&gt;
 sudo chmod -R 755 /opt/toolchains/dc&lt;br /&gt;
 sudo chown -R $(id -u):$(id -g) /opt/toolchains/dc&lt;br /&gt;
===Cloning the KallistiOS git repository===&lt;br /&gt;
Next, clone the git repository for the latest &#039;&#039;&#039;2.1.0&#039;&#039;&#039; stable version of KallistiOS: &lt;br /&gt;
 git clone https://github.com/KallistiOS/KallistiOS.git -b v2.1.x /opt/toolchains/dc/kos&lt;br /&gt;
&lt;br /&gt;
Alternatively, you may clone the latest unstable version, which contains the latest changes, but may also contain bugs or issues:&lt;br /&gt;
 git clone https://github.com/KallistiOS/KallistiOS.git -b master /opt/toolchains/dc/kos&lt;br /&gt;
&lt;br /&gt;
===Configuring the dc-chain script===&lt;br /&gt;
Enter the dc-chain directory:&lt;br /&gt;
 cd /opt/toolchains/dc/kos/utils/dc-chain&lt;br /&gt;
&lt;br /&gt;
You may now configure the options to your liking by creating a &amp;lt;code&amp;gt;Makefile.cfg&amp;lt;/code&amp;gt; file in this directory. Copy or rename the &amp;lt;code&amp;gt;Makefile.default.cfg&amp;lt;/code&amp;gt; file to &amp;lt;code&amp;gt;Makefile.cfg&amp;lt;/code&amp;gt; and then use a text editor to edit this file&#039;s settings to your liking. For example, you may like to alter the &amp;lt;code&amp;gt;makejobs&amp;lt;/code&amp;gt; option to the number of threads available on your CPU to speed up the compilation. However, if you run into errors during compilation, you may want to set &amp;lt;code&amp;gt;makejobs=1&amp;lt;/code&amp;gt;, as on some operating systems the toolchain may fail to build with a higher setting.&lt;br /&gt;
&lt;br /&gt;
We will leave the default &#039;&#039;&#039;stable&#039;&#039;&#039; profile for the toolchain, which currently uses GCC 13.2. For advanced users, other profiles are available to you; read the &amp;lt;code&amp;gt;README.md&amp;lt;/code&amp;gt; file in the dc-chain directory for more information if you are interested. If you are unsure about options in the &amp;lt;code&amp;gt;Makefile.cfg&amp;lt;/code&amp;gt;, just leave the defaults alone.&lt;br /&gt;
&lt;br /&gt;
===Downloading and compiling the toolchain===&lt;br /&gt;
Now we will run a script to download files and compile the toolchain. At this point, we have the option of building both the main CPU SH4 compiler and the AICA sound processor ARM compiler, or we can skip the ARM compiler and just build the SH4 compiler. Thankfully, KallistiOS includes a prebuilt sound driver, so the ARM compiler is only necessary if you&#039;re wanting to make changes to the sound driver or write custom code to run on the sound processor.&lt;br /&gt;
To build &#039;&#039;&#039;only the SH4 compiler&#039;&#039;&#039;:&lt;br /&gt;
 make&lt;br /&gt;
To build &#039;&#039;&#039;both&#039;&#039;&#039; the SH4 and the ARM compilers:&lt;br /&gt;
 make all&lt;br /&gt;
This will download and unpack the relevant necessary files and then begin the compilation process. The compilation can take anywhere from minutes to a few hours depending on your CPU and number of threads available. When successfully finished, the toolchains will be ready. If you used the commands above, the &#039;&#039;&#039;gdb&#039;&#039;&#039; debugger will have been built as well.&lt;br /&gt;
&lt;br /&gt;
===Cleaning up temporary files===&lt;br /&gt;
After building everything, you can clean up the extraneous files in your dc-chain directory by entering:&lt;br /&gt;
 make clean&lt;br /&gt;
&lt;br /&gt;
=Configuring and compiling KOS and kos-ports=&lt;br /&gt;
===Setting up the environment settings===&lt;br /&gt;
Enter the KOS directory:&lt;br /&gt;
 cd /opt/toolchains/dc/kos&lt;br /&gt;
Copy the pre-made environment script into place:&lt;br /&gt;
 cp doc/environ.sh.sample environ.sh &lt;br /&gt;
For most users, the default settings will suffice. However, advanced users may edit the environ.sh to your liking if you&#039;d like to change compile flags or alter paths. If you&#039;d like to have multiple KOS versions installed or multiple toolchain versions installed, you can set up different environ.sh files corresponding to these different configurations by altering the paths. Run the source command on the desired environ.sh file to select that configuration prior to compiling your project. &lt;br /&gt;
&lt;br /&gt;
You will need to run the source command to apply the KOS environment settings to your currently running shell. Run the following now, &#039;&#039;&#039;and&#039;&#039;&#039; &#039;&#039;whenever&#039;&#039; you open a new shell to work on Dreamcast projects:&lt;br /&gt;
 source /opt/toolchains/dc/kos/environ.sh&lt;br /&gt;
&lt;br /&gt;
===Building KOS===&lt;br /&gt;
Build KOS:&lt;br /&gt;
 make&lt;br /&gt;
KOS is now built.&lt;br /&gt;
===Building kos-ports===&lt;br /&gt;
Clone the kos-ports repository to your system:&lt;br /&gt;
 git clone --recursive https://github.com/KallistiOS/kos-ports /opt/toolchains/dc/kos-ports&lt;br /&gt;
Run the script to build all of the included ports:&lt;br /&gt;
 /opt/toolchains/dc/kos-ports/utils/build-all.sh&lt;br /&gt;
kos-ports is now built.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;NOTE&#039;&#039;&#039;: If you see the following error:&lt;br /&gt;
 1 error(s) occurred during the build process:&lt;br /&gt;
 /opt/toolchains/dc/kos/../kos-ports/libjimtcl: Build failed with return code 2&lt;br /&gt;
The error is safe to ignore as the &amp;lt;code&amp;gt;libjimtcl&amp;lt;/code&amp;gt; kos-port is broken at this time. The rest should compile without issue with the default KOS settings.&lt;br /&gt;
&lt;br /&gt;
===Building the KOS examples===&lt;br /&gt;
Enter the KOS examples directory:&lt;br /&gt;
 cd /opt/toolchains/dc/kos/examples&lt;br /&gt;
Build the examples:&lt;br /&gt;
 make&lt;br /&gt;
All of the example programs provided with KallistiOS are now built.&lt;br /&gt;
&lt;br /&gt;
=Further=&lt;br /&gt;
At this point, you will have a cross-compiler built so that you can generate programs for your Dreamcast from code. KallistiOS, the Dreamcast development library and operating system, is built, as is its associated packages within kos-ports, and all of its example programs.&lt;br /&gt;
&lt;br /&gt;
At this point, you will likely want to view some of the following articles:&lt;br /&gt;
* Setting up an IDE for Dreamcast development&lt;br /&gt;
** [[CLion Debugging]]&lt;br /&gt;
** [[Visual Studio Code]]&lt;br /&gt;
* [[Creating a first project with KallistiOS]] (&#039;&#039;&#039;TODO&#039;&#039;&#039;: Explain how to create a new DC project folder with Makefile, adding an external library, create a basic program, and compile it)&lt;br /&gt;
* [[Using a debug link]]&lt;br /&gt;
* [[Burning an example to CD-R]]&lt;br /&gt;
&lt;br /&gt;
=Running an example program through a debug link=&lt;br /&gt;
&#039;&#039;&#039;TODO&#039;&#039;&#039;: &#039;&#039;Give a tutorial on compiling dcload/dc-tool, setting up a serial, USB, or IP debug link, and running 2ndmix demo on real hardware.&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Download and burn the [[:File:Dcload-2023-06-22.zip|latest versions of dcload-ip or dcload-serial]] -- the IP version includes improved DHCP support, so there is no longer a need to configure things beforehand. &lt;br /&gt;
 &lt;br /&gt;
Run one of the examples from the &amp;lt;code&amp;gt;kos/examples/dreamcast&amp;lt;/code&amp;gt; directory with one of the following commands:&lt;br /&gt;
 dc-tool-ip -t &amp;lt;dreamcast IP address&amp;gt; -x example.elf&lt;br /&gt;
&lt;br /&gt;
 dc-tool-ser -t &amp;lt;serial device path&amp;gt; -x example.elf&lt;br /&gt;
&lt;br /&gt;
Run &amp;lt;code&amp;gt;dc-tool-ip&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;dc-tool-ser&amp;lt;/code&amp;gt; without any parameters to get additional options.&lt;br /&gt;
&lt;br /&gt;
=Burning an example program to CD-R=&lt;br /&gt;
&#039;&#039;&#039;TODO&#039;&#039;&#039;: &#039;&#039;Explain how to build mkdcdisc and write 2ndmix demo to CD-R and run on a Dreamcast console&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[https://gitlab.com/simulant/mkdcdisc mkdcdisc] can be used to easily generate a burnable self-boot CDI image.&lt;br /&gt;
Dependencies required:&lt;br /&gt;
* Homebrew: &amp;lt;code&amp;gt;meson libisofs pkg-config&amp;lt;/code&amp;gt;&lt;br /&gt;
* Debian/Ubuntu:  &amp;lt;code&amp;gt;libisofs-dev meson ninja-build&amp;lt;/code&amp;gt;&lt;br /&gt;
* Fedora: &amp;lt;code&amp;gt;meson ninja-build&amp;lt;/code&amp;gt;&lt;br /&gt;
* Arch: &amp;lt;code&amp;gt;meson&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Build &amp;lt;code&amp;gt;mkdcdisc&amp;lt;/code&amp;gt;:&lt;br /&gt;
 git clone https://gitlab.com/simulant/mkdcdisc.git&lt;br /&gt;
 cd mkdcdisc&lt;br /&gt;
 meson setup builddir&lt;br /&gt;
 meson compile -C builddir&lt;br /&gt;
 ./builddir/mkdcdisc -h&lt;br /&gt;
and create a CDI image from your compiled ELF like so:&lt;br /&gt;
 mkdcdisc -e MyProgram.elf -o MyProgram.cdi&lt;br /&gt;
Then you can burn the CDI file using DiscJuggler (Windows-only, but also works through [https://www.winehq.org/ WINE]), ImgBurn with the CDI plugin, or the cdiburn *nix script floating around out there. (document this better)&lt;/div&gt;</summary>
		<author><name>Darc</name></author>
	</entry>
	<entry>
		<id>https://dreamcast.wiki/wiki/index.php?title=Rust_on_Dreamcast&amp;diff=3667</id>
		<title>Rust on Dreamcast</title>
		<link rel="alternate" type="text/html" href="https://dreamcast.wiki/wiki/index.php?title=Rust_on_Dreamcast&amp;diff=3667"/>
		<updated>2024-12-11T23:20:55Z</updated>

		<summary type="html">&lt;p&gt;Darc: /* Examples */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[File:Rust-dc-logo.png|thumb|Ferris holding his Dreamcast controller]]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Rust&#039;&#039;&#039; is a popular modern programming language focusing on performance, memory safety, and program correctness. As a &amp;quot;blazingly fast&amp;quot; low-level language, it is viable for running on a Dreamcast console. Doing so presents a bit of a challenge, however, as the official Rust compiler is based on the [https://llvm.org/ LLVM] toolchain infrastructure, which does not support the Dreamcast CPU&#039;s SuperH architecture. Dreamcast programming is instead typically done with [https://gcc.gnu.org/ GCC], the GNU Compiler Collection. There are currently two viable solutions to this challenge:&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;rustc_codegen_gcc&#039;&#039;&#039;, the current preferred method: An experimental codegen backend for the official Rust &#039;&#039;&#039;rustc&#039;&#039;&#039; compiler which interfaces with GCC&#039;s libgccjit API&lt;br /&gt;
* &#039;&#039;&#039;gccrs&#039;&#039;&#039;: an experimental Rust frontend for GCC&lt;br /&gt;
&lt;br /&gt;
Neither solution is complete at this time, but &#039;&#039;&#039;rustc_codegen_gcc&#039;&#039;&#039; is much further along and is quite usable with some patience with its current limitations. On the other hand, &#039;&#039;&#039;gccrs&#039;&#039;&#039; can compile for Dreamcast, but is in an earlier state. Below we will focus on using &#039;&#039;&#039;rustc_codegen_gcc&#039;&#039;&#039;. For more information on using gccrs, see the [[gccrs]] page.&lt;br /&gt;
&lt;br /&gt;
=Current Status=&lt;br /&gt;
* The full &#039;&#039;&#039;std&#039;&#039;&#039; library is supported.&lt;br /&gt;
* A [https://github.com/dreamcast-rs/KallistiOS custom version of KallistiOS] v2.1.0 (latest stable release) is used.&lt;br /&gt;
** This version includes minor changes for Rust and a beta libpthread addon layer to interface with the Rust standard library.&lt;br /&gt;
** Wrappers are provided to invoke &#039;&#039;&#039;cargo&#039;&#039;&#039; and &#039;&#039;&#039;rustc&#039;&#039;&#039; in a familiar manner to compile projects.&lt;br /&gt;
* A [https://github.com/dreamcast-rs/kos-sys &#039;&#039;&#039;kos-sys&#039;&#039;&#039;] raw/unsafe bindings crate is provided for easily linking KallistiOS and its unsafe C API into Rust crates. This crate is in active development, but with a large portion of KallistiOS functionality available now via C FFI.&lt;br /&gt;
* A [https://github.com/dreamcast-rs/kos-rs &#039;&#039;&#039;kos-rs&#039;&#039;&#039;] Rust crate with safe idiomatic interfaces is available but is in very early stages.&lt;br /&gt;
** Documentation for the bindings is available at [https://kos-rs.dreamcast.wiki/ kos-rs.dreamcast.wiki].&lt;br /&gt;
** If bindings do not yet exist for desired functionality, wrapping of KallistiOS C functions is required to get proper use of KallistiOS at this time. See the [https://doc.rust-lang.org/nomicon/ffi.html Foreign Function Interface page] in the Rustonomicon for further understanding. &lt;br /&gt;
** A custom [https://github.com/dreamcast-rs/libc/tree/libc-0.2-kos &#039;&#039;&#039;libc&#039;&#039;&#039; crate with KallistiOS support] is provided.&lt;br /&gt;
* Code generation uses &#039;&#039;&#039;rustc_codegen_gcc&#039;&#039;&#039;, so its limitations apply here. See the [https://blog.antoyo.xyz/ development blog] for more information on its progress.&lt;br /&gt;
** rustc_codegen_gcc is pinned to a particular Rust nightly version, but is synced regularly. The current version in use is the &#039;&#039;&#039;2024-08-10&#039;&#039;&#039; nightly.&lt;br /&gt;
** A [https://github.com/dreamcast-rs/rust/tree/kos-2024-08-10 KallistiOS-patched version of the latest nightly] is maintained and updated regularly. &lt;br /&gt;
** Panic unwinding, debug info, LTO, etc. are not currently available.&lt;br /&gt;
** Architectures dependent on rustc_codegen_gcc are not yet able to be added to the rustc frontend, so a workaround is necessary: MIPS is falsely selected for KallistiOS, causing rustc to emit objects with a MIPS header and a provided wrapper is used when linking to rewrite these MIPS headers to SuperH before passing the compiled objects to the linker.&lt;br /&gt;
* A separate [https://github.com/rust-lang/gcc development version of the GCC toolchain] must be used.&lt;br /&gt;
** This version of GCC contains the latest libgccjit patches necessary to interface with rustc_codegen_gcc, and will be built to work with the custom KallistiOS&#039;s libpthread layer.&lt;br /&gt;
** New updates to rustc_codegen_gcc often require new updates to GCC, so recompiling GCC regularly will be necessary to keep up with the latest updates.&lt;br /&gt;
** The development version of GCC does not always compile well on every platform. Using a modern Linux platform is recommended for maximum compatibility.&lt;br /&gt;
** The custom KallistiOS and GCC environment can still be used to compile code or projects written in C, C++, etc.&lt;br /&gt;
* KallistiOS and all related libraries and tools must be compiled using the &amp;lt;code&amp;gt;-m4-single&amp;lt;/code&amp;gt; ABI. This is already set up for you in the guide below, but if external libraries are being linked to Rust projects, make sure that code is compiled using the proper ABI.&lt;br /&gt;
&lt;br /&gt;
=Setting up a Rust environment for Dreamcast development=&lt;br /&gt;
The following guide will provide instructions for setting up a Rust development environment for Dreamcast. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;If you run into any errors or other challenges while following this tutorial, or simply need clarification on any of the steps, feel free to ask for assistance on the [https://dcemulation.org/phpBB/viewforum.php?f=29 message board] or [https://discord.gg/wDEfkUh63h our Discord server] and we would be happy to aid you and update the guide for the benefit of future readers and others in the community.&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
==Prerequisites==&lt;br /&gt;
First, we will need to set up the prerequisites.&lt;br /&gt;
&lt;br /&gt;
* You must have the [https://rustup.rs/ rustup] tool installed for your operating system to manage Rust toolchain installations. It will automatically manage the latest Rust nightly installations for you. You cannot use a Rust toolchain that may be provided by your operating system vendor.&lt;br /&gt;
* Install the dependency packages for your operating system. These are listed in the &#039;&#039;&#039;Dependencies&#039;&#039;&#039; section on the [[Getting Started with Dreamcast development#Dependencies]] page, with lists provided for most common operating systems.&lt;br /&gt;
* We will install our custom Rust toolchain environment within &amp;lt;code&amp;gt;/opt/toolchains/dc/rust&amp;lt;/code&amp;gt;. If it does not already exist, create the &amp;lt;code&amp;gt;/opt/toolchains/dc&amp;lt;/code&amp;gt; directory and grant it proper permissions using the following commands. &lt;br /&gt;
 sudo mkdir -p /opt/toolchains/dc&lt;br /&gt;
 sudo chmod -R 755 /opt/toolchains/dc&lt;br /&gt;
 sudo chown -R $(id -u):$(id -g) /opt/toolchains/dc&lt;br /&gt;
* Clone the [https://github.com/dreamcast-rs/rust-for-dreamcast/ Rust for Dreamcast] repo containing necessary support files to &amp;lt;code&amp;gt;/opt/toolchains/dc/rust&amp;lt;/code&amp;gt;:&lt;br /&gt;
 git clone https://github.com/dreamcast-rs/rust-for-dreamcast.git /opt/toolchains/dc/rust&lt;br /&gt;
&lt;br /&gt;
==Installing our custom KallistiOS and GCC toolchain==&lt;br /&gt;
Clone the git repository for our custom Rust-patched version of KallistiOS v2.1.0 stable:&lt;br /&gt;
 git clone https://github.com/dreamcast-rs/KallistiOS /opt/toolchains/dc/rust/kos&lt;br /&gt;
&lt;br /&gt;
Enter the dc-chain directory:&lt;br /&gt;
 cd /opt/toolchains/dc/rust/kos/utils/dc-chain&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;Makefile.default.cfg&amp;lt;/code&amp;gt; file in this directory has been customized for you, selecting the proper toolchain profile, paths, and build options necessary for building a Rust toolchain using this guide. It is not recommended to adjust the defaults.&lt;br /&gt;
&lt;br /&gt;
To build the GCC toolchain using 2 CPU cores, simply type:&lt;br /&gt;
 make makejobs=2&lt;br /&gt;
Adjust the &amp;lt;code&amp;gt;makejobs=...&amp;lt;/code&amp;gt; value to use the number of CPU cores available to you on your system in order to speed up compilation. This may take a while to build, so be patient.&lt;br /&gt;
&lt;br /&gt;
Once the GCC toolchain is built, source the &amp;lt;code&amp;gt;environ.sh&amp;lt;/code&amp;gt; KallistiOS environment settings. A custom &amp;lt;code&amp;gt;environ.sh&amp;lt;/code&amp;gt; file has been provided for you, containing settings pre-adjusted for Rust development using this guide.&lt;br /&gt;
&lt;br /&gt;
You will need to run the source command to apply the KallistiOS environment settings to your currently running shell. Run the following now, &#039;&#039;&#039;and&#039;&#039;&#039; &#039;&#039;whenever&#039;&#039; you open a new shell to work on Dreamcast projects:&lt;br /&gt;
 source /opt/toolchains/dc/rust/misc/environ.sh&lt;br /&gt;
&lt;br /&gt;
Enter the KallistiOS directory:&lt;br /&gt;
 cd /opt/toolchains/dc/rust/kos&lt;br /&gt;
&lt;br /&gt;
Build KallistiOS:&lt;br /&gt;
 make&lt;br /&gt;
&lt;br /&gt;
==Building rustc_codegen_gcc and Rust sysroot==&lt;br /&gt;
Now that we have a working KallistiOS environment suitable for Rust development set up, we can build the Rust compiler compiler and sysroot components. A script is provided which will download the necessary components from the respective repositories and compile them for you. Run the installer script:&lt;br /&gt;
&lt;br /&gt;
 /opt/toolchains/dc/rust/misc/install-rust.sh&lt;br /&gt;
&lt;br /&gt;
If all goes well, you&#039;ll see &#039;&#039;&#039;Rust for KallistiOS/Dreamcast installed!&#039;&#039;&#039; message!&lt;br /&gt;
&lt;br /&gt;
==Building KOS ports libraries==&lt;br /&gt;
If desired, you may also built the KOS ports collection of libraries for use with your Rust environment. It is recommended to maintain a separate KOS ports installation for linking with Rust projects in &amp;lt;code&amp;gt;/opt/toolchains/dc/rust/kos-ports&amp;lt;/code&amp;gt;. This is because the Rust environment is using the &amp;lt;code&amp;gt;-m4-single&amp;lt;/code&amp;gt; ABI, but the KallistiOS default installation will be using the &amp;lt;code&amp;gt;-m4-single-only&amp;lt;/code&amp;gt;, which cannot be mixed and matched.&lt;br /&gt;
&lt;br /&gt;
Clone the kos-ports repository to your system:&lt;br /&gt;
 git clone https://github.com/KallistiOS/kos-ports /opt/toolchains/dc/rust/kos-ports&lt;br /&gt;
An individual port can be built and installed by entering its directory and running the proper command. For example, installing GLdc:&lt;br /&gt;
 cd /opt/toolchains/dc/rust/kos-ports/libGL&lt;br /&gt;
 make install&lt;br /&gt;
Or, instead, you may run the script to build all of the included ports:&lt;br /&gt;
 /opt/toolchains/dc/rust/kos-ports/utils/build-all.sh&lt;br /&gt;
&lt;br /&gt;
=Using Rust for Dreamcast=&lt;br /&gt;
If all went well, you will now have a working Rust for Dreamcast environment!&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;cargo&#039;&#039;&#039; can be invoked to target Dreamcast using the &amp;lt;code&amp;gt;kos-cargo&amp;lt;/code&amp;gt; command.&lt;br /&gt;
** &amp;lt;code&amp;gt;kos-cargo run&amp;lt;/code&amp;gt; can be used to build and run code on a Dreamcast console using [[dcload-ip]] or [[dcload-serial]]. You will need to set up the &amp;lt;code&amp;gt;KOS_LOADER&amp;lt;/code&amp;gt; variable with the necessary command in the &amp;lt;code&amp;gt;/opt/toolchains/dc/rust/misc/environ.sh&amp;lt;/code&amp;gt; file (and then re-source the file for the change to take effect).&lt;br /&gt;
* &#039;&#039;&#039;rustc&#039;&#039;&#039; can be invoked to target Dreamcast using the &amp;lt;code&amp;gt;kos-rustc&amp;lt;/code&amp;gt; command.&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
Examples are included with the Rust for Dreamcast repo to help you get started.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/hello &amp;lt;code&amp;gt;hello&amp;lt;/code&amp;gt;] demonstrates how to create a simple &amp;quot;Hello, world!&amp;quot; application using cargo.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/gldc-cube &amp;lt;code&amp;gt;gldc-cube&amp;lt;/code&amp;gt;] demonstrates a Rust project using the GLdc KOS port.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/romdisk &amp;lt;code&amp;gt;romdisk&amp;lt;/code&amp;gt;] demonstrates how to generate and mount a read-only ROMdisk with a KallistiOS project.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/addlib &amp;lt;code&amp;gt;addlib&amp;lt;/code&amp;gt;] demonstrates how to create a Rust library that can be included with a KallistiOS project.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/atomics &amp;lt;code&amp;gt;atomics&amp;lt;/code&amp;gt;] demonstrates the use of atomic functions.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/filesystem-io &amp;lt;code&amp;gt;filesystem-io&amp;lt;/code&amp;gt;] demonstrates listing directory contents and opening files.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/network-time &amp;lt;code&amp;gt;network-time&amp;lt;/code&amp;gt;] demonstrates displaying the system time and retrieving the current UTC time from an NTP server using the [[Broadband adapter]] or [[LAN adapter]].&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/pvr/pvrmark_strips_direct &amp;lt;code&amp;gt;pvrmark_strips_direct&amp;lt;/code&amp;gt;] demonstrates the use of PVR direct rendering&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/threads &amp;lt;code&amp;gt;threads&amp;lt;/code&amp;gt;] demonstrates the use of threads.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/tokio-async &amp;lt;code&amp;gt;tokio-async&amp;lt;/code&amp;gt;] demonstrates the use of the &#039;&#039;&#039;tokio&#039;&#039;&#039; async runtime.&lt;br /&gt;
&lt;br /&gt;
==Creating a new Rust project with Cargo==&lt;br /&gt;
First, we&#039;ll demonstrate creating a new &amp;quot;Hello, world!&amp;quot; project with &amp;lt;code&amp;gt;kos-cargo&amp;lt;/code&amp;gt;. This will follow the [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/hello-cargo &amp;lt;code&amp;gt;cargo-hello&amp;lt;/code&amp;gt;] example included in the Rust-for-Dreamcast repo. &lt;br /&gt;
&lt;br /&gt;
In a directory of your choosing, let&#039;s invoke &amp;lt;code&amp;gt;kos-cargo&amp;lt;/code&amp;gt; to create a new project and then enter the directory:&lt;br /&gt;
 kos-cargo new hello&lt;br /&gt;
 cd hello&lt;br /&gt;
&lt;br /&gt;
Let&#039;s add our kos-rs crate to gain access to current KallistiOS bindings. Open &amp;lt;code&amp;gt;Cargo.toml&amp;lt;/code&amp;gt; in your text editor and add:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;toml&amp;quot;&amp;gt;&lt;br /&gt;
[dependencies]&lt;br /&gt;
kos = { package = &amp;quot;kos-rs&amp;quot;, git = &amp;quot;https://github.com/dreamcast-rs/kos-rs&amp;quot; }&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we can open up &amp;lt;code&amp;gt;src/main.rs&amp;lt;/code&amp;gt; and write our &amp;quot;Hello, world!&amp;quot; example code:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; line&amp;gt;&lt;br /&gt;
fn main() {&lt;br /&gt;
    println!(&amp;quot;Hello, world from Rust!&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we can use &amp;lt;code&amp;gt;kos-cargo build&amp;lt;/code&amp;gt; to build our project. If all goes well, there will be a &amp;lt;code&amp;gt;target/sh-elf/debug/hello.elf&amp;lt;/code&amp;gt; file that can be sent to the Dreamcast with &amp;lt;code&amp;gt;dc-tool&amp;lt;/code&amp;gt;.&lt;br /&gt;
If you have &amp;lt;code&amp;gt;KOS_LOADER&amp;lt;/code&amp;gt; set in the &amp;lt;code&amp;gt;environ.sh&amp;lt;/code&amp;gt; file, you can invoke it directly with &amp;lt;code&amp;gt;kos-cargo run&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Creating a Rust project using kos-ports libraries==&lt;br /&gt;
[[File:Rust-Cube rustc codegen-gcc demo.gif|thumb|cube example in action]]&lt;br /&gt;
The [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/gldc-cube &amp;lt;code&amp;gt;gldc-cube&amp;lt;/code&amp;gt;] example demonstrates creating a rotating 3D cube using Rust as the primary language, while calling C functions provided by the &#039;&#039;&#039;GLdc&#039;&#039;&#039; library available in kos-ports. This project&#039;s initial setup is done the same as the above &amp;lt;code&amp;gt;hello&amp;lt;/code&amp;gt; example.&lt;br /&gt;
&lt;br /&gt;
For this example, we&#039;ll need to convert JPEG textures to VQ-encoded textures at compile time using the &amp;lt;code&amp;gt;vqenc&amp;lt;/code&amp;gt; utility includes with KallistiOS. To do this, we&#039;ll add a &amp;lt;code&amp;gt;build.rs&amp;lt;/code&amp;gt; file to the root of the crate with the following code (abridged to show the conversion of only one texture):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot;&amp;gt;&lt;br /&gt;
fn main() {&lt;br /&gt;
    let kos_base = std::env::var(&amp;quot;KOS_BASE&amp;quot;).expect(&amp;quot;Missing $KOS_BASE -- KallistiOS environment not sourced!&amp;quot;);&lt;br /&gt;
    let vqenc_cmd = kos_base + &amp;quot;/utils/vqenc/vqenc&amp;quot;;&lt;br /&gt;
    Command::new(&amp;amp;vqenc_cmd)&lt;br /&gt;
        .arg(&amp;quot;-t&amp;quot;)&lt;br /&gt;
        .arg(&amp;quot;-v&amp;quot;)&lt;br /&gt;
        .arg(&amp;quot;rsrc/tex_claw.jpg&amp;quot;)&lt;br /&gt;
        .output()&lt;br /&gt;
        .expect(&amp;quot;vqenc on tex_claw.jpg failed!&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
The texture files are then included in our project using the &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;include_bytes!&amp;lt;/syntaxhighlight&amp;gt; macro.&lt;br /&gt;
&lt;br /&gt;
We will need to link the GLdc library in this example, but we don&#039;t need to do anything special to add the paths to this library, because adding the common paths to KallistiOS libraries is already done for us in the kos-rs crate. However, if in your project you need to include a separate unique library path, you can add it in your &amp;lt;code&amp;gt;build.rs&amp;lt;/code&amp;gt; like so:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot;&amp;gt;&lt;br /&gt;
println!(&amp;quot;cargo:rustc-link-search=native={}&amp;quot;, lib_path);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The workings of this example&#039;s source code are too great to detail here line-by-line, but the example demonstrates how to use GLdc via the [https://github.com/dreamcast-rs/gldc-sys gldc-sys] crate, which contains unsafe/raw bindings to GLdc. Check out this crate&#039;s source code for an example of declaring and binding external C functions, constants, and structures so they can be used in Rust code. Since the entirety of the gldc-cube example uses C FFI functions without safe wrappers, the &amp;lt;code&amp;gt;main()&amp;lt;/code&amp;gt; source is wrapped in &amp;lt;code&amp;gt;unsafe {}&amp;lt;/code&amp;gt;. In the future, this would be much less necessary as higher level safe Rust bindings to KallistiOS and other libraries become mature.&lt;br /&gt;
&lt;br /&gt;
==Creating a Rust library==&lt;br /&gt;
Next, we&#039;ll demonstrate creating a Rust library with &amp;lt;code&amp;gt;kos-cargo&amp;lt;/code&amp;gt; that can be included in other Dreamcast code. This will follow the [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/addlib &amp;lt;code&amp;gt;addlib&amp;lt;/code&amp;gt;] example. Once again, this project&#039;s initial setup is done the same as the above &amp;lt;code&amp;gt;hello&amp;lt;/code&amp;gt; example, but you&#039;ll create the new project using &amp;lt;code&amp;gt;kos-cargo new --lib addlib&amp;lt;/code&amp;gt; to specify that we&#039;re creating a library named &amp;lt;code&amp;gt;addlib&amp;lt;/code&amp;gt;. You&#039;ll also need to add the following text to this project&#039;s &amp;lt;code&amp;gt;Cargo.toml&amp;lt;/code&amp;gt; file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;toml&amp;quot;&amp;gt;&lt;br /&gt;
[lib]&lt;br /&gt;
crate-type = [&amp;quot;staticlib&amp;quot;]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This tells Rust to build a static &amp;lt;code&amp;gt;.a&amp;lt;/code&amp;gt; library archive file from our code, which is located in &amp;lt;code&amp;gt;src/lib.rs&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; line&amp;gt;&lt;br /&gt;
#[no_mangle]&lt;br /&gt;
pub extern &amp;quot;C&amp;quot; fn print_added(a: isize, b: isize) {&lt;br /&gt;
    print!(&amp;quot;{}&amp;quot;, a + b);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
#[no_mangle]&lt;br /&gt;
pub extern &amp;quot;C&amp;quot; fn add_integers(a: isize, b: isize) -&amp;gt; isize {&lt;br /&gt;
    a + b&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Two simple functions are provided: one for adding two integers and returning the result, and another for adding two integers and printing the result as text. Because these functions use &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;#[no_mangle]&amp;lt;/syntaxhighlight&amp;gt; and are declared &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;extern &amp;quot;C&amp;quot;&amp;lt;/syntaxhighlight&amp;gt;, they can be called by name in C code that links this library.&lt;br /&gt;
&lt;br /&gt;
When built using &amp;lt;code&amp;gt;kos-cargo build&amp;lt;/code&amp;gt;, a &amp;lt;code&amp;gt;target/sh-elf/debug/libaddlib.a&amp;lt;/code&amp;gt; file will be generated. This can be linked into other projects to gain the use of these functions.&lt;br /&gt;
&lt;br /&gt;
For example, this can be added to a standard &amp;lt;code&amp;gt;Makefile&amp;lt;/code&amp;gt;-based KallistiOS project by editing the &amp;lt;code&amp;gt;Makefile&amp;lt;/code&amp;gt;:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;make&amp;quot;&amp;gt;&lt;br /&gt;
$(TARGET): $(OBJS)&lt;br /&gt;
	kos-cc -o $(TARGET) $(OBJS) -L/opt/toolchains/dc/rust/examples/cargo-addlib/target/sh-elf/debug -laddlib&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then, we can use the code in our C source:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/* Declare the external function from the Rust library */&lt;br /&gt;
int add_integers(int a, int b);&lt;br /&gt;
&lt;br /&gt;
/* Use the function */&lt;br /&gt;
printf(&amp;quot;Five plus six is %d\n&amp;quot;, add_integers(5, 6));&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Using the libc crate with KallistiOS==&lt;br /&gt;
If any of the crates in your project&#039;s dependency tree requires the &#039;&#039;&#039;libc&#039;&#039;&#039; crate, you will need to override fetching the default crate with our custom libc crate with KallistiOS/Dreamcast definitions. This custom version was copied to &amp;lt;code&amp;gt;/opt/toolchains/dc/rust/libc&amp;lt;/code&amp;gt; when building the Rust sysroot earlier. Add the following text to your &amp;lt;code&amp;gt;Cargo.toml&amp;lt;/code&amp;gt;:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;toml&amp;quot;&amp;gt;&lt;br /&gt;
[patch.crates-io]&lt;br /&gt;
libc = { path = &amp;quot;/opt/toolchains/dc/rust/libc&amp;quot; }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Adjusting build flags==&lt;br /&gt;
Build settings can be adjusted through the &amp;lt;code&amp;gt;KOS_RCG_RUSTFLAGS&amp;lt;/code&amp;gt; variable in the &amp;lt;code&amp;gt;environ.sh&amp;lt;/code&amp;gt; file under the &amp;lt;code&amp;gt;### Rust Flags&amp;lt;/code&amp;gt; section. &lt;br /&gt;
* To pass arguments to the &#039;&#039;&#039;rustc&#039;&#039;&#039; frontend, e.g. to disable debug assertions:&lt;br /&gt;
** &amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot; inline&amp;gt;export KOS_RCG_RUSTFLAGS=&amp;quot;${KOS_RCG_RUSTFLAGS} -Cdebug-assertions=no&amp;quot;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* To pass arguments to the GCC backend, e.g. to enable the &amp;lt;code&amp;gt;-freorder-blocks-algorithm=simple&amp;lt;/code&amp;gt; optimization for Rust code:&lt;br /&gt;
** &amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot; inline&amp;gt;export KOS_RCG_RUSTFLAGS=&amp;quot;${KOS_RCG_RUSTFLAGS} -Cllvm-args=-freorder-blocks-algorithm=simple&amp;quot;&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Darc</name></author>
	</entry>
	<entry>
		<id>https://dreamcast.wiki/wiki/index.php?title=Rust_on_Dreamcast&amp;diff=3666</id>
		<title>Rust on Dreamcast</title>
		<link rel="alternate" type="text/html" href="https://dreamcast.wiki/wiki/index.php?title=Rust_on_Dreamcast&amp;diff=3666"/>
		<updated>2024-12-11T23:04:27Z</updated>

		<summary type="html">&lt;p&gt;Darc: /* Compiling individual modules into object files with rustc */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[File:Rust-dc-logo.png|thumb|Ferris holding his Dreamcast controller]]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Rust&#039;&#039;&#039; is a popular modern programming language focusing on performance, memory safety, and program correctness. As a &amp;quot;blazingly fast&amp;quot; low-level language, it is viable for running on a Dreamcast console. Doing so presents a bit of a challenge, however, as the official Rust compiler is based on the [https://llvm.org/ LLVM] toolchain infrastructure, which does not support the Dreamcast CPU&#039;s SuperH architecture. Dreamcast programming is instead typically done with [https://gcc.gnu.org/ GCC], the GNU Compiler Collection. There are currently two viable solutions to this challenge:&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;rustc_codegen_gcc&#039;&#039;&#039;, the current preferred method: An experimental codegen backend for the official Rust &#039;&#039;&#039;rustc&#039;&#039;&#039; compiler which interfaces with GCC&#039;s libgccjit API&lt;br /&gt;
* &#039;&#039;&#039;gccrs&#039;&#039;&#039;: an experimental Rust frontend for GCC&lt;br /&gt;
&lt;br /&gt;
Neither solution is complete at this time, but &#039;&#039;&#039;rustc_codegen_gcc&#039;&#039;&#039; is much further along and is quite usable with some patience with its current limitations. On the other hand, &#039;&#039;&#039;gccrs&#039;&#039;&#039; can compile for Dreamcast, but is in an earlier state. Below we will focus on using &#039;&#039;&#039;rustc_codegen_gcc&#039;&#039;&#039;. For more information on using gccrs, see the [[gccrs]] page.&lt;br /&gt;
&lt;br /&gt;
=Current Status=&lt;br /&gt;
* The full &#039;&#039;&#039;std&#039;&#039;&#039; library is supported.&lt;br /&gt;
* A [https://github.com/dreamcast-rs/KallistiOS custom version of KallistiOS] v2.1.0 (latest stable release) is used.&lt;br /&gt;
** This version includes minor changes for Rust and a beta libpthread addon layer to interface with the Rust standard library.&lt;br /&gt;
** Wrappers are provided to invoke &#039;&#039;&#039;cargo&#039;&#039;&#039; and &#039;&#039;&#039;rustc&#039;&#039;&#039; in a familiar manner to compile projects.&lt;br /&gt;
* A [https://github.com/dreamcast-rs/kos-sys &#039;&#039;&#039;kos-sys&#039;&#039;&#039;] raw/unsafe bindings crate is provided for easily linking KallistiOS and its unsafe C API into Rust crates. This crate is in active development, but with a large portion of KallistiOS functionality available now via C FFI.&lt;br /&gt;
* A [https://github.com/dreamcast-rs/kos-rs &#039;&#039;&#039;kos-rs&#039;&#039;&#039;] Rust crate with safe idiomatic interfaces is available but is in very early stages.&lt;br /&gt;
** Documentation for the bindings is available at [https://kos-rs.dreamcast.wiki/ kos-rs.dreamcast.wiki].&lt;br /&gt;
** If bindings do not yet exist for desired functionality, wrapping of KallistiOS C functions is required to get proper use of KallistiOS at this time. See the [https://doc.rust-lang.org/nomicon/ffi.html Foreign Function Interface page] in the Rustonomicon for further understanding. &lt;br /&gt;
** A custom [https://github.com/dreamcast-rs/libc/tree/libc-0.2-kos &#039;&#039;&#039;libc&#039;&#039;&#039; crate with KallistiOS support] is provided.&lt;br /&gt;
* Code generation uses &#039;&#039;&#039;rustc_codegen_gcc&#039;&#039;&#039;, so its limitations apply here. See the [https://blog.antoyo.xyz/ development blog] for more information on its progress.&lt;br /&gt;
** rustc_codegen_gcc is pinned to a particular Rust nightly version, but is synced regularly. The current version in use is the &#039;&#039;&#039;2024-08-10&#039;&#039;&#039; nightly.&lt;br /&gt;
** A [https://github.com/dreamcast-rs/rust/tree/kos-2024-08-10 KallistiOS-patched version of the latest nightly] is maintained and updated regularly. &lt;br /&gt;
** Panic unwinding, debug info, LTO, etc. are not currently available.&lt;br /&gt;
** Architectures dependent on rustc_codegen_gcc are not yet able to be added to the rustc frontend, so a workaround is necessary: MIPS is falsely selected for KallistiOS, causing rustc to emit objects with a MIPS header and a provided wrapper is used when linking to rewrite these MIPS headers to SuperH before passing the compiled objects to the linker.&lt;br /&gt;
* A separate [https://github.com/rust-lang/gcc development version of the GCC toolchain] must be used.&lt;br /&gt;
** This version of GCC contains the latest libgccjit patches necessary to interface with rustc_codegen_gcc, and will be built to work with the custom KallistiOS&#039;s libpthread layer.&lt;br /&gt;
** New updates to rustc_codegen_gcc often require new updates to GCC, so recompiling GCC regularly will be necessary to keep up with the latest updates.&lt;br /&gt;
** The development version of GCC does not always compile well on every platform. Using a modern Linux platform is recommended for maximum compatibility.&lt;br /&gt;
** The custom KallistiOS and GCC environment can still be used to compile code or projects written in C, C++, etc.&lt;br /&gt;
* KallistiOS and all related libraries and tools must be compiled using the &amp;lt;code&amp;gt;-m4-single&amp;lt;/code&amp;gt; ABI. This is already set up for you in the guide below, but if external libraries are being linked to Rust projects, make sure that code is compiled using the proper ABI.&lt;br /&gt;
&lt;br /&gt;
=Setting up a Rust environment for Dreamcast development=&lt;br /&gt;
The following guide will provide instructions for setting up a Rust development environment for Dreamcast. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;If you run into any errors or other challenges while following this tutorial, or simply need clarification on any of the steps, feel free to ask for assistance on the [https://dcemulation.org/phpBB/viewforum.php?f=29 message board] or [https://discord.gg/wDEfkUh63h our Discord server] and we would be happy to aid you and update the guide for the benefit of future readers and others in the community.&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
==Prerequisites==&lt;br /&gt;
First, we will need to set up the prerequisites.&lt;br /&gt;
&lt;br /&gt;
* You must have the [https://rustup.rs/ rustup] tool installed for your operating system to manage Rust toolchain installations. It will automatically manage the latest Rust nightly installations for you. You cannot use a Rust toolchain that may be provided by your operating system vendor.&lt;br /&gt;
* Install the dependency packages for your operating system. These are listed in the &#039;&#039;&#039;Dependencies&#039;&#039;&#039; section on the [[Getting Started with Dreamcast development#Dependencies]] page, with lists provided for most common operating systems.&lt;br /&gt;
* We will install our custom Rust toolchain environment within &amp;lt;code&amp;gt;/opt/toolchains/dc/rust&amp;lt;/code&amp;gt;. If it does not already exist, create the &amp;lt;code&amp;gt;/opt/toolchains/dc&amp;lt;/code&amp;gt; directory and grant it proper permissions using the following commands. &lt;br /&gt;
 sudo mkdir -p /opt/toolchains/dc&lt;br /&gt;
 sudo chmod -R 755 /opt/toolchains/dc&lt;br /&gt;
 sudo chown -R $(id -u):$(id -g) /opt/toolchains/dc&lt;br /&gt;
* Clone the [https://github.com/dreamcast-rs/rust-for-dreamcast/ Rust for Dreamcast] repo containing necessary support files to &amp;lt;code&amp;gt;/opt/toolchains/dc/rust&amp;lt;/code&amp;gt;:&lt;br /&gt;
 git clone https://github.com/dreamcast-rs/rust-for-dreamcast.git /opt/toolchains/dc/rust&lt;br /&gt;
&lt;br /&gt;
==Installing our custom KallistiOS and GCC toolchain==&lt;br /&gt;
Clone the git repository for our custom Rust-patched version of KallistiOS v2.1.0 stable:&lt;br /&gt;
 git clone https://github.com/dreamcast-rs/KallistiOS /opt/toolchains/dc/rust/kos&lt;br /&gt;
&lt;br /&gt;
Enter the dc-chain directory:&lt;br /&gt;
 cd /opt/toolchains/dc/rust/kos/utils/dc-chain&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;Makefile.default.cfg&amp;lt;/code&amp;gt; file in this directory has been customized for you, selecting the proper toolchain profile, paths, and build options necessary for building a Rust toolchain using this guide. It is not recommended to adjust the defaults.&lt;br /&gt;
&lt;br /&gt;
To build the GCC toolchain using 2 CPU cores, simply type:&lt;br /&gt;
 make makejobs=2&lt;br /&gt;
Adjust the &amp;lt;code&amp;gt;makejobs=...&amp;lt;/code&amp;gt; value to use the number of CPU cores available to you on your system in order to speed up compilation. This may take a while to build, so be patient.&lt;br /&gt;
&lt;br /&gt;
Once the GCC toolchain is built, source the &amp;lt;code&amp;gt;environ.sh&amp;lt;/code&amp;gt; KallistiOS environment settings. A custom &amp;lt;code&amp;gt;environ.sh&amp;lt;/code&amp;gt; file has been provided for you, containing settings pre-adjusted for Rust development using this guide.&lt;br /&gt;
&lt;br /&gt;
You will need to run the source command to apply the KallistiOS environment settings to your currently running shell. Run the following now, &#039;&#039;&#039;and&#039;&#039;&#039; &#039;&#039;whenever&#039;&#039; you open a new shell to work on Dreamcast projects:&lt;br /&gt;
 source /opt/toolchains/dc/rust/misc/environ.sh&lt;br /&gt;
&lt;br /&gt;
Enter the KallistiOS directory:&lt;br /&gt;
 cd /opt/toolchains/dc/rust/kos&lt;br /&gt;
&lt;br /&gt;
Build KallistiOS:&lt;br /&gt;
 make&lt;br /&gt;
&lt;br /&gt;
==Building rustc_codegen_gcc and Rust sysroot==&lt;br /&gt;
Now that we have a working KallistiOS environment suitable for Rust development set up, we can build the Rust compiler compiler and sysroot components. A script is provided which will download the necessary components from the respective repositories and compile them for you. Run the installer script:&lt;br /&gt;
&lt;br /&gt;
 /opt/toolchains/dc/rust/misc/install-rust.sh&lt;br /&gt;
&lt;br /&gt;
If all goes well, you&#039;ll see &#039;&#039;&#039;Rust for KallistiOS/Dreamcast installed!&#039;&#039;&#039; message!&lt;br /&gt;
&lt;br /&gt;
==Building KOS ports libraries==&lt;br /&gt;
If desired, you may also built the KOS ports collection of libraries for use with your Rust environment. It is recommended to maintain a separate KOS ports installation for linking with Rust projects in &amp;lt;code&amp;gt;/opt/toolchains/dc/rust/kos-ports&amp;lt;/code&amp;gt;. This is because the Rust environment is using the &amp;lt;code&amp;gt;-m4-single&amp;lt;/code&amp;gt; ABI, but the KallistiOS default installation will be using the &amp;lt;code&amp;gt;-m4-single-only&amp;lt;/code&amp;gt;, which cannot be mixed and matched.&lt;br /&gt;
&lt;br /&gt;
Clone the kos-ports repository to your system:&lt;br /&gt;
 git clone https://github.com/KallistiOS/kos-ports /opt/toolchains/dc/rust/kos-ports&lt;br /&gt;
An individual port can be built and installed by entering its directory and running the proper command. For example, installing GLdc:&lt;br /&gt;
 cd /opt/toolchains/dc/rust/kos-ports/libGL&lt;br /&gt;
 make install&lt;br /&gt;
Or, instead, you may run the script to build all of the included ports:&lt;br /&gt;
 /opt/toolchains/dc/rust/kos-ports/utils/build-all.sh&lt;br /&gt;
&lt;br /&gt;
=Using Rust for Dreamcast=&lt;br /&gt;
If all went well, you will now have a working Rust for Dreamcast environment!&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;cargo&#039;&#039;&#039; can be invoked to target Dreamcast using the &amp;lt;code&amp;gt;kos-cargo&amp;lt;/code&amp;gt; command.&lt;br /&gt;
** &amp;lt;code&amp;gt;kos-cargo run&amp;lt;/code&amp;gt; can be used to build and run code on a Dreamcast console using [[dcload-ip]] or [[dcload-serial]]. You will need to set up the &amp;lt;code&amp;gt;KOS_LOADER&amp;lt;/code&amp;gt; variable with the necessary command in the &amp;lt;code&amp;gt;/opt/toolchains/dc/rust/misc/environ.sh&amp;lt;/code&amp;gt; file (and then re-source the file for the change to take effect).&lt;br /&gt;
* &#039;&#039;&#039;rustc&#039;&#039;&#039; can be invoked to target Dreamcast using the &amp;lt;code&amp;gt;kos-rustc&amp;lt;/code&amp;gt; command.&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
Examples are included with the Rust for Dreamcast repo to help you get started.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/hello-cargo &amp;lt;code&amp;gt;hello-cargo&amp;lt;/code&amp;gt;] demonstrates how to create a simple &amp;quot;Hello, world!&amp;quot; application using cargo.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/hello-make &amp;lt;code&amp;gt;hello-make&amp;lt;/code&amp;gt;] demonstrates how to create a simple &amp;quot;Hello, world!&amp;quot; application using standard Makefiles.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/gldc-cube &amp;lt;code&amp;gt;gldc-cube&amp;lt;/code&amp;gt;] demonstrates a Rust project using the GLdc KOS port.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/addlib &amp;lt;code&amp;gt;addlib&amp;lt;/code&amp;gt;] demonstrates how to create a Rust library that can be included with a KallistiOS project.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/atomics &amp;lt;code&amp;gt;atomics&amp;lt;/code&amp;gt;] demonstrates the use of atomic functions.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/filesystem-io &amp;lt;code&amp;gt;filesystem-io&amp;lt;/code&amp;gt;] demonstrates listing directory contents and opening files.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/network-time &amp;lt;code&amp;gt;network-time&amp;lt;/code&amp;gt;] demonstrates displaying the system time and retrieving the current UTC time from an NTP server using the [[Broadband adapter]] or [[LAN adapter]].&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/pvr_strips_direct &amp;lt;code&amp;gt;pvr_strips_direct&amp;lt;/code&amp;gt;] demonstrates the use of PVR direct rendering&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/threads &amp;lt;code&amp;gt;threads&amp;lt;/code&amp;gt;] demonstrates the use of threads.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/tokio-async &amp;lt;code&amp;gt;tokio-async&amp;lt;/code&amp;gt;] demonstrates the use of the &#039;&#039;&#039;tokio&#039;&#039;&#039; async runtime.&lt;br /&gt;
&lt;br /&gt;
==Creating a new Rust project with Cargo==&lt;br /&gt;
First, we&#039;ll demonstrate creating a new &amp;quot;Hello, world!&amp;quot; project with &amp;lt;code&amp;gt;kos-cargo&amp;lt;/code&amp;gt;. This will follow the [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/hello-cargo &amp;lt;code&amp;gt;cargo-hello&amp;lt;/code&amp;gt;] example included in the Rust-for-Dreamcast repo. &lt;br /&gt;
&lt;br /&gt;
In a directory of your choosing, let&#039;s invoke &amp;lt;code&amp;gt;kos-cargo&amp;lt;/code&amp;gt; to create a new project and then enter the directory:&lt;br /&gt;
 kos-cargo new hello&lt;br /&gt;
 cd hello&lt;br /&gt;
&lt;br /&gt;
Let&#039;s add our kos-rs crate to gain access to current KallistiOS bindings. Open &amp;lt;code&amp;gt;Cargo.toml&amp;lt;/code&amp;gt; in your text editor and add:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;toml&amp;quot;&amp;gt;&lt;br /&gt;
[dependencies]&lt;br /&gt;
kos = { package = &amp;quot;kos-rs&amp;quot;, git = &amp;quot;https://github.com/dreamcast-rs/kos-rs&amp;quot; }&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we can open up &amp;lt;code&amp;gt;src/main.rs&amp;lt;/code&amp;gt; and write our &amp;quot;Hello, world!&amp;quot; example code:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; line&amp;gt;&lt;br /&gt;
fn main() {&lt;br /&gt;
    println!(&amp;quot;Hello, world from Rust!&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we can use &amp;lt;code&amp;gt;kos-cargo build&amp;lt;/code&amp;gt; to build our project. If all goes well, there will be a &amp;lt;code&amp;gt;target/sh-elf/debug/hello.elf&amp;lt;/code&amp;gt; file that can be sent to the Dreamcast with &amp;lt;code&amp;gt;dc-tool&amp;lt;/code&amp;gt;.&lt;br /&gt;
If you have &amp;lt;code&amp;gt;KOS_LOADER&amp;lt;/code&amp;gt; set in the &amp;lt;code&amp;gt;environ.sh&amp;lt;/code&amp;gt; file, you can invoke it directly with &amp;lt;code&amp;gt;kos-cargo run&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Creating a Rust project using kos-ports libraries==&lt;br /&gt;
[[File:Rust-Cube rustc codegen-gcc demo.gif|thumb|cube example in action]]&lt;br /&gt;
The [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/gldc-cube &amp;lt;code&amp;gt;gldc-cube&amp;lt;/code&amp;gt;] example demonstrates creating a rotating 3D cube using Rust as the primary language, while calling C functions provided by the &#039;&#039;&#039;GLdc&#039;&#039;&#039; library available in kos-ports. This project&#039;s initial setup is done the same as the above &amp;lt;code&amp;gt;hello&amp;lt;/code&amp;gt; example.&lt;br /&gt;
&lt;br /&gt;
For this example, we&#039;ll need to convert JPEG textures to VQ-encoded textures at compile time using the &amp;lt;code&amp;gt;vqenc&amp;lt;/code&amp;gt; utility includes with KallistiOS. To do this, we&#039;ll add a &amp;lt;code&amp;gt;build.rs&amp;lt;/code&amp;gt; file to the root of the crate with the following code (abridged to show the conversion of only one texture):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot;&amp;gt;&lt;br /&gt;
fn main() {&lt;br /&gt;
    let kos_base = std::env::var(&amp;quot;KOS_BASE&amp;quot;).expect(&amp;quot;Missing $KOS_BASE -- KallistiOS environment not sourced!&amp;quot;);&lt;br /&gt;
    let vqenc_cmd = kos_base + &amp;quot;/utils/vqenc/vqenc&amp;quot;;&lt;br /&gt;
    Command::new(&amp;amp;vqenc_cmd)&lt;br /&gt;
        .arg(&amp;quot;-t&amp;quot;)&lt;br /&gt;
        .arg(&amp;quot;-v&amp;quot;)&lt;br /&gt;
        .arg(&amp;quot;rsrc/tex_claw.jpg&amp;quot;)&lt;br /&gt;
        .output()&lt;br /&gt;
        .expect(&amp;quot;vqenc on tex_claw.jpg failed!&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
The texture files are then included in our project using the &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;include_bytes!&amp;lt;/syntaxhighlight&amp;gt; macro.&lt;br /&gt;
&lt;br /&gt;
We will need to link the GLdc library in this example, but we don&#039;t need to do anything special to add the paths to this library, because adding the common paths to KallistiOS libraries is already done for us in the kos-rs crate. However, if in your project you need to include a separate unique library path, you can add it in your &amp;lt;code&amp;gt;build.rs&amp;lt;/code&amp;gt; like so:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot;&amp;gt;&lt;br /&gt;
println!(&amp;quot;cargo:rustc-link-search=native={}&amp;quot;, lib_path);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The workings of this example&#039;s source code are too great to detail here line-by-line, but the example demonstrates how to use GLdc via the [https://github.com/dreamcast-rs/gldc-sys gldc-sys] crate, which contains unsafe/raw bindings to GLdc. Check out this crate&#039;s source code for an example of declaring and binding external C functions, constants, and structures so they can be used in Rust code. Since the entirety of the gldc-cube example uses C FFI functions without safe wrappers, the &amp;lt;code&amp;gt;main()&amp;lt;/code&amp;gt; source is wrapped in &amp;lt;code&amp;gt;unsafe {}&amp;lt;/code&amp;gt;. In the future, this would be much less necessary as higher level safe Rust bindings to KallistiOS and other libraries become mature.&lt;br /&gt;
&lt;br /&gt;
==Creating a Rust library==&lt;br /&gt;
Next, we&#039;ll demonstrate creating a Rust library with &amp;lt;code&amp;gt;kos-cargo&amp;lt;/code&amp;gt; that can be included in other Dreamcast code. This will follow the [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/addlib &amp;lt;code&amp;gt;addlib&amp;lt;/code&amp;gt;] example. Once again, this project&#039;s initial setup is done the same as the above &amp;lt;code&amp;gt;hello&amp;lt;/code&amp;gt; example, but you&#039;ll create the new project using &amp;lt;code&amp;gt;kos-cargo new --lib addlib&amp;lt;/code&amp;gt; to specify that we&#039;re creating a library named &amp;lt;code&amp;gt;addlib&amp;lt;/code&amp;gt;. You&#039;ll also need to add the following text to this project&#039;s &amp;lt;code&amp;gt;Cargo.toml&amp;lt;/code&amp;gt; file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;toml&amp;quot;&amp;gt;&lt;br /&gt;
[lib]&lt;br /&gt;
crate-type = [&amp;quot;staticlib&amp;quot;]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This tells Rust to build a static &amp;lt;code&amp;gt;.a&amp;lt;/code&amp;gt; library archive file from our code, which is located in &amp;lt;code&amp;gt;src/lib.rs&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; line&amp;gt;&lt;br /&gt;
#[no_mangle]&lt;br /&gt;
pub extern &amp;quot;C&amp;quot; fn print_added(a: isize, b: isize) {&lt;br /&gt;
    print!(&amp;quot;{}&amp;quot;, a + b);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
#[no_mangle]&lt;br /&gt;
pub extern &amp;quot;C&amp;quot; fn add_integers(a: isize, b: isize) -&amp;gt; isize {&lt;br /&gt;
    a + b&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Two simple functions are provided: one for adding two integers and returning the result, and another for adding two integers and printing the result as text. Because these functions use &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;#[no_mangle]&amp;lt;/syntaxhighlight&amp;gt; and are declared &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;extern &amp;quot;C&amp;quot;&amp;lt;/syntaxhighlight&amp;gt;, they can be called by name in C code that links this library.&lt;br /&gt;
&lt;br /&gt;
When built using &amp;lt;code&amp;gt;kos-cargo build&amp;lt;/code&amp;gt;, a &amp;lt;code&amp;gt;target/sh-elf/debug/libaddlib.a&amp;lt;/code&amp;gt; file will be generated. This can be linked into other projects to gain the use of these functions.&lt;br /&gt;
&lt;br /&gt;
For example, this can be added to a standard &amp;lt;code&amp;gt;Makefile&amp;lt;/code&amp;gt;-based KallistiOS project by editing the &amp;lt;code&amp;gt;Makefile&amp;lt;/code&amp;gt;:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;make&amp;quot;&amp;gt;&lt;br /&gt;
$(TARGET): $(OBJS)&lt;br /&gt;
	kos-cc -o $(TARGET) $(OBJS) -L/opt/toolchains/dc/rust/examples/cargo-addlib/target/sh-elf/debug -laddlib&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then, we can use the code in our C source:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/* Declare the external function from the Rust library */&lt;br /&gt;
int add_integers(int a, int b);&lt;br /&gt;
&lt;br /&gt;
/* Use the function */&lt;br /&gt;
printf(&amp;quot;Five plus six is %d\n&amp;quot;, add_integers(5, 6));&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Using the libc crate with KallistiOS==&lt;br /&gt;
If any of the crates in your project&#039;s dependency tree requires the &#039;&#039;&#039;libc&#039;&#039;&#039; crate, you will need to override fetching the default crate with our custom libc crate with KallistiOS/Dreamcast definitions. This custom version was copied to &amp;lt;code&amp;gt;/opt/toolchains/dc/rust/libc&amp;lt;/code&amp;gt; when building the Rust sysroot earlier. Add the following text to your &amp;lt;code&amp;gt;Cargo.toml&amp;lt;/code&amp;gt;:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;toml&amp;quot;&amp;gt;&lt;br /&gt;
[patch.crates-io]&lt;br /&gt;
libc = { path = &amp;quot;/opt/toolchains/dc/rust/libc&amp;quot; }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Adjusting build flags==&lt;br /&gt;
Build settings can be adjusted through the &amp;lt;code&amp;gt;KOS_RCG_RUSTFLAGS&amp;lt;/code&amp;gt; variable in the &amp;lt;code&amp;gt;environ.sh&amp;lt;/code&amp;gt; file under the &amp;lt;code&amp;gt;### Rust Flags&amp;lt;/code&amp;gt; section. &lt;br /&gt;
* To pass arguments to the &#039;&#039;&#039;rustc&#039;&#039;&#039; frontend, e.g. to disable debug assertions:&lt;br /&gt;
** &amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot; inline&amp;gt;export KOS_RCG_RUSTFLAGS=&amp;quot;${KOS_RCG_RUSTFLAGS} -Cdebug-assertions=no&amp;quot;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* To pass arguments to the GCC backend, e.g. to enable the &amp;lt;code&amp;gt;-freorder-blocks-algorithm=simple&amp;lt;/code&amp;gt; optimization for Rust code:&lt;br /&gt;
** &amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot; inline&amp;gt;export KOS_RCG_RUSTFLAGS=&amp;quot;${KOS_RCG_RUSTFLAGS} -Cllvm-args=-freorder-blocks-algorithm=simple&amp;quot;&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Darc</name></author>
	</entry>
	<entry>
		<id>https://dreamcast.wiki/wiki/index.php?title=Rust_on_Dreamcast&amp;diff=3649</id>
		<title>Rust on Dreamcast</title>
		<link rel="alternate" type="text/html" href="https://dreamcast.wiki/wiki/index.php?title=Rust_on_Dreamcast&amp;diff=3649"/>
		<updated>2024-11-27T01:19:01Z</updated>

		<summary type="html">&lt;p&gt;Darc: /* Creating a Rust library */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[File:Rust-dc-logo.png|thumb|Ferris holding his Dreamcast controller]]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Rust&#039;&#039;&#039; is a popular modern programming language focusing on performance, memory safety, and program correctness. As a &amp;quot;blazingly fast&amp;quot; low-level language, it is viable for running on a Dreamcast console. Doing so presents a bit of a challenge, however, as the official Rust compiler is based on the [https://llvm.org/ LLVM] toolchain infrastructure, which does not support the Dreamcast CPU&#039;s SuperH architecture. Dreamcast programming is instead typically done with [https://gcc.gnu.org/ GCC], the GNU Compiler Collection. There are currently two viable solutions to this challenge:&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;rustc_codegen_gcc&#039;&#039;&#039;, the current preferred method: An experimental codegen backend for the official Rust &#039;&#039;&#039;rustc&#039;&#039;&#039; compiler which interfaces with GCC&#039;s libgccjit API&lt;br /&gt;
* &#039;&#039;&#039;gccrs&#039;&#039;&#039;: an experimental Rust frontend for GCC&lt;br /&gt;
&lt;br /&gt;
Neither solution is complete at this time, but &#039;&#039;&#039;rustc_codegen_gcc&#039;&#039;&#039; is much further along and is quite usable with some patience with its current limitations. On the other hand, &#039;&#039;&#039;gccrs&#039;&#039;&#039; can compile for Dreamcast, but is in an earlier state. Below we will focus on using &#039;&#039;&#039;rustc_codegen_gcc&#039;&#039;&#039;. For more information on using gccrs, see the [[gccrs]] page.&lt;br /&gt;
&lt;br /&gt;
=Current Status=&lt;br /&gt;
* The full &#039;&#039;&#039;std&#039;&#039;&#039; library is supported.&lt;br /&gt;
* A [https://github.com/dreamcast-rs/KallistiOS custom version of KallistiOS] v2.1.0 (latest stable release) is used.&lt;br /&gt;
** This version includes minor changes for Rust and a beta libpthread addon layer to interface with the Rust standard library.&lt;br /&gt;
** Wrappers are provided to invoke &#039;&#039;&#039;cargo&#039;&#039;&#039; and &#039;&#039;&#039;rustc&#039;&#039;&#039; in a familiar manner to compile projects.&lt;br /&gt;
* A [https://github.com/dreamcast-rs/kos-sys &#039;&#039;&#039;kos-sys&#039;&#039;&#039;] raw/unsafe bindings crate is provided for easily linking KallistiOS and its unsafe C API into Rust crates. This crate is in active development, but with a large portion of KallistiOS functionality available now via C FFI.&lt;br /&gt;
* A [https://github.com/dreamcast-rs/kos-rs &#039;&#039;&#039;kos-rs&#039;&#039;&#039;] Rust crate with safe idiomatic interfaces is available but is in very early stages.&lt;br /&gt;
** Documentation for the bindings is available at [https://kos-rs.dreamcast.wiki/ kos-rs.dreamcast.wiki].&lt;br /&gt;
** If bindings do not yet exist for desired functionality, wrapping of KallistiOS C functions is required to get proper use of KallistiOS at this time. See the [https://doc.rust-lang.org/nomicon/ffi.html Foreign Function Interface page] in the Rustonomicon for further understanding. &lt;br /&gt;
** A custom [https://github.com/dreamcast-rs/libc/tree/libc-0.2-kos &#039;&#039;&#039;libc&#039;&#039;&#039; crate with KallistiOS support] is provided.&lt;br /&gt;
* Code generation uses &#039;&#039;&#039;rustc_codegen_gcc&#039;&#039;&#039;, so its limitations apply here. See the [https://blog.antoyo.xyz/ development blog] for more information on its progress.&lt;br /&gt;
** rustc_codegen_gcc is pinned to a particular Rust nightly version, but is synced regularly. The current version in use is the &#039;&#039;&#039;2024-08-10&#039;&#039;&#039; nightly.&lt;br /&gt;
** A [https://github.com/dreamcast-rs/rust/tree/kos-2024-08-10 KallistiOS-patched version of the latest nightly] is maintained and updated regularly. &lt;br /&gt;
** Panic unwinding, debug info, LTO, etc. are not currently available.&lt;br /&gt;
** Architectures dependent on rustc_codegen_gcc are not yet able to be added to the rustc frontend, so a workaround is necessary: MIPS is falsely selected for KallistiOS, causing rustc to emit objects with a MIPS header and a provided wrapper is used when linking to rewrite these MIPS headers to SuperH before passing the compiled objects to the linker.&lt;br /&gt;
* A separate [https://github.com/rust-lang/gcc development version of the GCC toolchain] must be used.&lt;br /&gt;
** This version of GCC contains the latest libgccjit patches necessary to interface with rustc_codegen_gcc, and will be built to work with the custom KallistiOS&#039;s libpthread layer.&lt;br /&gt;
** New updates to rustc_codegen_gcc often require new updates to GCC, so recompiling GCC regularly will be necessary to keep up with the latest updates.&lt;br /&gt;
** The development version of GCC does not always compile well on every platform. Using a modern Linux platform is recommended for maximum compatibility.&lt;br /&gt;
** The custom KallistiOS and GCC environment can still be used to compile code or projects written in C, C++, etc.&lt;br /&gt;
* KallistiOS and all related libraries and tools must be compiled using the &amp;lt;code&amp;gt;-m4-single&amp;lt;/code&amp;gt; ABI. This is already set up for you in the guide below, but if external libraries are being linked to Rust projects, make sure that code is compiled using the proper ABI.&lt;br /&gt;
&lt;br /&gt;
=Setting up a Rust environment for Dreamcast development=&lt;br /&gt;
The following guide will provide instructions for setting up a Rust development environment for Dreamcast. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;If you run into any errors or other challenges while following this tutorial, or simply need clarification on any of the steps, feel free to ask for assistance on the [https://dcemulation.org/phpBB/viewforum.php?f=29 message board] or [https://discord.gg/wDEfkUh63h our Discord server] and we would be happy to aid you and update the guide for the benefit of future readers and others in the community.&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
==Prerequisites==&lt;br /&gt;
First, we will need to set up the prerequisites.&lt;br /&gt;
&lt;br /&gt;
* You must have the [https://rustup.rs/ rustup] tool installed for your operating system to manage Rust toolchain installations. It will automatically manage the latest Rust nightly installations for you. You cannot use a Rust toolchain that may be provided by your operating system vendor.&lt;br /&gt;
* Install the dependency packages for your operating system. These are listed in the &#039;&#039;&#039;Dependencies&#039;&#039;&#039; section on the [[Getting Started with Dreamcast development#Dependencies]] page, with lists provided for most common operating systems.&lt;br /&gt;
* We will install our custom Rust toolchain environment within &amp;lt;code&amp;gt;/opt/toolchains/dc/rust&amp;lt;/code&amp;gt;. If it does not already exist, create the &amp;lt;code&amp;gt;/opt/toolchains/dc&amp;lt;/code&amp;gt; directory and grant it proper permissions using the following commands. &lt;br /&gt;
 sudo mkdir -p /opt/toolchains/dc&lt;br /&gt;
 sudo chmod -R 755 /opt/toolchains/dc&lt;br /&gt;
 sudo chown -R $(id -u):$(id -g) /opt/toolchains/dc&lt;br /&gt;
* Clone the [https://github.com/dreamcast-rs/rust-for-dreamcast/ Rust for Dreamcast] repo containing necessary support files to &amp;lt;code&amp;gt;/opt/toolchains/dc/rust&amp;lt;/code&amp;gt;:&lt;br /&gt;
 git clone https://github.com/dreamcast-rs/rust-for-dreamcast.git /opt/toolchains/dc/rust&lt;br /&gt;
&lt;br /&gt;
==Installing our custom KallistiOS and GCC toolchain==&lt;br /&gt;
Clone the git repository for our custom Rust-patched version of KallistiOS v2.1.0 stable:&lt;br /&gt;
 git clone https://github.com/dreamcast-rs/KallistiOS /opt/toolchains/dc/rust/kos&lt;br /&gt;
&lt;br /&gt;
Enter the dc-chain directory:&lt;br /&gt;
 cd /opt/toolchains/dc/rust/kos/utils/dc-chain&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;Makefile.default.cfg&amp;lt;/code&amp;gt; file in this directory has been customized for you, selecting the proper toolchain profile, paths, and build options necessary for building a Rust toolchain using this guide. It is not recommended to adjust the defaults.&lt;br /&gt;
&lt;br /&gt;
To build the GCC toolchain using 2 CPU cores, simply type:&lt;br /&gt;
 make makejobs=2&lt;br /&gt;
Adjust the &amp;lt;code&amp;gt;makejobs=...&amp;lt;/code&amp;gt; value to use the number of CPU cores available to you on your system in order to speed up compilation. This may take a while to build, so be patient.&lt;br /&gt;
&lt;br /&gt;
Once the GCC toolchain is built, source the &amp;lt;code&amp;gt;environ.sh&amp;lt;/code&amp;gt; KallistiOS environment settings. A custom &amp;lt;code&amp;gt;environ.sh&amp;lt;/code&amp;gt; file has been provided for you, containing settings pre-adjusted for Rust development using this guide.&lt;br /&gt;
&lt;br /&gt;
You will need to run the source command to apply the KallistiOS environment settings to your currently running shell. Run the following now, &#039;&#039;&#039;and&#039;&#039;&#039; &#039;&#039;whenever&#039;&#039; you open a new shell to work on Dreamcast projects:&lt;br /&gt;
 source /opt/toolchains/dc/rust/misc/environ.sh&lt;br /&gt;
&lt;br /&gt;
Enter the KallistiOS directory:&lt;br /&gt;
 cd /opt/toolchains/dc/rust/kos&lt;br /&gt;
&lt;br /&gt;
Build KallistiOS:&lt;br /&gt;
 make&lt;br /&gt;
&lt;br /&gt;
==Building rustc_codegen_gcc and Rust sysroot==&lt;br /&gt;
Now that we have a working KallistiOS environment suitable for Rust development set up, we can build the Rust compiler compiler and sysroot components. A script is provided which will download the necessary components from the respective repositories and compile them for you. Run the installer script:&lt;br /&gt;
&lt;br /&gt;
 /opt/toolchains/dc/rust/misc/install-rust.sh&lt;br /&gt;
&lt;br /&gt;
If all goes well, you&#039;ll see &#039;&#039;&#039;Rust for KallistiOS/Dreamcast installed!&#039;&#039;&#039; message!&lt;br /&gt;
&lt;br /&gt;
==Building KOS ports libraries==&lt;br /&gt;
If desired, you may also built the KOS ports collection of libraries for use with your Rust environment. It is recommended to maintain a separate KOS ports installation for linking with Rust projects in &amp;lt;code&amp;gt;/opt/toolchains/dc/rust/kos-ports&amp;lt;/code&amp;gt;. This is because the Rust environment is using the &amp;lt;code&amp;gt;-m4-single&amp;lt;/code&amp;gt; ABI, but the KallistiOS default installation will be using the &amp;lt;code&amp;gt;-m4-single-only&amp;lt;/code&amp;gt;, which cannot be mixed and matched.&lt;br /&gt;
&lt;br /&gt;
Clone the kos-ports repository to your system:&lt;br /&gt;
 git clone https://github.com/KallistiOS/kos-ports /opt/toolchains/dc/rust/kos-ports&lt;br /&gt;
An individual port can be built and installed by entering its directory and running the proper command. For example, installing GLdc:&lt;br /&gt;
 cd /opt/toolchains/dc/rust/kos-ports/libGL&lt;br /&gt;
 make install&lt;br /&gt;
Or, instead, you may run the script to build all of the included ports:&lt;br /&gt;
 /opt/toolchains/dc/rust/kos-ports/utils/build-all.sh&lt;br /&gt;
&lt;br /&gt;
=Using Rust for Dreamcast=&lt;br /&gt;
If all went well, you will now have a working Rust for Dreamcast environment!&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;cargo&#039;&#039;&#039; can be invoked to target Dreamcast using the &amp;lt;code&amp;gt;kos-cargo&amp;lt;/code&amp;gt; command.&lt;br /&gt;
** &amp;lt;code&amp;gt;kos-cargo run&amp;lt;/code&amp;gt; can be used to build and run code on a Dreamcast console using [[dcload-ip]] or [[dcload-serial]]. You will need to set up the &amp;lt;code&amp;gt;KOS_LOADER&amp;lt;/code&amp;gt; variable with the necessary command in the &amp;lt;code&amp;gt;/opt/toolchains/dc/rust/misc/environ.sh&amp;lt;/code&amp;gt; file (and then re-source the file for the change to take effect).&lt;br /&gt;
* &#039;&#039;&#039;rustc&#039;&#039;&#039; can be invoked to target Dreamcast using the &amp;lt;code&amp;gt;kos-rustc&amp;lt;/code&amp;gt; command.&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
Examples are included with the Rust for Dreamcast repo to help you get started.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/hello-cargo &amp;lt;code&amp;gt;hello-cargo&amp;lt;/code&amp;gt;] demonstrates how to create a simple &amp;quot;Hello, world!&amp;quot; application using cargo.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/hello-make &amp;lt;code&amp;gt;hello-make&amp;lt;/code&amp;gt;] demonstrates how to create a simple &amp;quot;Hello, world!&amp;quot; application using standard Makefiles.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/gldc-cube &amp;lt;code&amp;gt;gldc-cube&amp;lt;/code&amp;gt;] demonstrates a Rust project using the GLdc KOS port.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/addlib &amp;lt;code&amp;gt;addlib&amp;lt;/code&amp;gt;] demonstrates how to create a Rust library that can be included with a KallistiOS project.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/atomics &amp;lt;code&amp;gt;atomics&amp;lt;/code&amp;gt;] demonstrates the use of atomic functions.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/filesystem-io &amp;lt;code&amp;gt;filesystem-io&amp;lt;/code&amp;gt;] demonstrates listing directory contents and opening files.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/network-time &amp;lt;code&amp;gt;network-time&amp;lt;/code&amp;gt;] demonstrates displaying the system time and retrieving the current UTC time from an NTP server using the [[Broadband adapter]] or [[LAN adapter]].&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/pvr_strips_direct &amp;lt;code&amp;gt;pvr_strips_direct&amp;lt;/code&amp;gt;] demonstrates the use of PVR direct rendering&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/threads &amp;lt;code&amp;gt;threads&amp;lt;/code&amp;gt;] demonstrates the use of threads.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/tokio-async &amp;lt;code&amp;gt;tokio-async&amp;lt;/code&amp;gt;] demonstrates the use of the &#039;&#039;&#039;tokio&#039;&#039;&#039; async runtime.&lt;br /&gt;
&lt;br /&gt;
==Creating a new Rust project with Cargo==&lt;br /&gt;
First, we&#039;ll demonstrate creating a new &amp;quot;Hello, world!&amp;quot; project with &amp;lt;code&amp;gt;kos-cargo&amp;lt;/code&amp;gt;. This will follow the [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/hello-cargo &amp;lt;code&amp;gt;cargo-hello&amp;lt;/code&amp;gt;] example included in the Rust-for-Dreamcast repo. &lt;br /&gt;
&lt;br /&gt;
In a directory of your choosing, let&#039;s invoke &amp;lt;code&amp;gt;kos-cargo&amp;lt;/code&amp;gt; to create a new project and then enter the directory:&lt;br /&gt;
 kos-cargo new hello&lt;br /&gt;
 cd hello&lt;br /&gt;
&lt;br /&gt;
Let&#039;s add our kos-rs crate to gain access to current KallistiOS bindings. Open &amp;lt;code&amp;gt;Cargo.toml&amp;lt;/code&amp;gt; in your text editor and add:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;toml&amp;quot;&amp;gt;&lt;br /&gt;
[dependencies]&lt;br /&gt;
kos = { package = &amp;quot;kos-rs&amp;quot;, git = &amp;quot;https://github.com/dreamcast-rs/kos-rs&amp;quot; }&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we can open up &amp;lt;code&amp;gt;src/main.rs&amp;lt;/code&amp;gt; and write our &amp;quot;Hello, world!&amp;quot; example code:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; line&amp;gt;&lt;br /&gt;
fn main() {&lt;br /&gt;
    println!(&amp;quot;Hello, world from Rust!&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we can use &amp;lt;code&amp;gt;kos-cargo build&amp;lt;/code&amp;gt; to build our project. If all goes well, there will be a &amp;lt;code&amp;gt;target/sh-elf/debug/hello.elf&amp;lt;/code&amp;gt; file that can be sent to the Dreamcast with &amp;lt;code&amp;gt;dc-tool&amp;lt;/code&amp;gt;.&lt;br /&gt;
If you have &amp;lt;code&amp;gt;KOS_LOADER&amp;lt;/code&amp;gt; set in the &amp;lt;code&amp;gt;environ.sh&amp;lt;/code&amp;gt; file, you can invoke it directly with &amp;lt;code&amp;gt;kos-cargo run&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Creating a Rust project using kos-ports libraries==&lt;br /&gt;
[[File:Rust-Cube rustc codegen-gcc demo.gif|thumb|cube example in action]]&lt;br /&gt;
The [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/gldc-cube &amp;lt;code&amp;gt;gldc-cube&amp;lt;/code&amp;gt;] example demonstrates creating a rotating 3D cube using Rust as the primary language, while calling C functions provided by the &#039;&#039;&#039;GLdc&#039;&#039;&#039; library available in kos-ports. This project&#039;s initial setup is done the same as the above &amp;lt;code&amp;gt;hello&amp;lt;/code&amp;gt; example.&lt;br /&gt;
&lt;br /&gt;
For this example, we&#039;ll need to convert JPEG textures to VQ-encoded textures at compile time using the &amp;lt;code&amp;gt;vqenc&amp;lt;/code&amp;gt; utility includes with KallistiOS. To do this, we&#039;ll add a &amp;lt;code&amp;gt;build.rs&amp;lt;/code&amp;gt; file to the root of the crate with the following code (abridged to show the conversion of only one texture):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot;&amp;gt;&lt;br /&gt;
fn main() {&lt;br /&gt;
    let kos_base = std::env::var(&amp;quot;KOS_BASE&amp;quot;).expect(&amp;quot;Missing $KOS_BASE -- KallistiOS environment not sourced!&amp;quot;);&lt;br /&gt;
    let vqenc_cmd = kos_base + &amp;quot;/utils/vqenc/vqenc&amp;quot;;&lt;br /&gt;
    Command::new(&amp;amp;vqenc_cmd)&lt;br /&gt;
        .arg(&amp;quot;-t&amp;quot;)&lt;br /&gt;
        .arg(&amp;quot;-v&amp;quot;)&lt;br /&gt;
        .arg(&amp;quot;rsrc/tex_claw.jpg&amp;quot;)&lt;br /&gt;
        .output()&lt;br /&gt;
        .expect(&amp;quot;vqenc on tex_claw.jpg failed!&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
The texture files are then included in our project using the &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;include_bytes!&amp;lt;/syntaxhighlight&amp;gt; macro.&lt;br /&gt;
&lt;br /&gt;
We will need to link the GLdc library in this example, but we don&#039;t need to do anything special to add the paths to this library, because adding the common paths to KallistiOS libraries is already done for us in the kos-rs crate. However, if in your project you need to include a separate unique library path, you can add it in your &amp;lt;code&amp;gt;build.rs&amp;lt;/code&amp;gt; like so:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot;&amp;gt;&lt;br /&gt;
println!(&amp;quot;cargo:rustc-link-search=native={}&amp;quot;, lib_path);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The workings of this example&#039;s source code are too great to detail here line-by-line, but the example demonstrates how to use GLdc via the [https://github.com/dreamcast-rs/gldc-sys gldc-sys] crate, which contains unsafe/raw bindings to GLdc. Check out this crate&#039;s source code for an example of declaring and binding external C functions, constants, and structures so they can be used in Rust code. Since the entirety of the gldc-cube example uses C FFI functions without safe wrappers, the &amp;lt;code&amp;gt;main()&amp;lt;/code&amp;gt; source is wrapped in &amp;lt;code&amp;gt;unsafe {}&amp;lt;/code&amp;gt;. In the future, this would be much less necessary as higher level safe Rust bindings to KallistiOS and other libraries become mature.&lt;br /&gt;
&lt;br /&gt;
==Creating a Rust library==&lt;br /&gt;
Next, we&#039;ll demonstrate creating a Rust library with &amp;lt;code&amp;gt;kos-cargo&amp;lt;/code&amp;gt; that can be included in other Dreamcast code. This will follow the [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/addlib &amp;lt;code&amp;gt;addlib&amp;lt;/code&amp;gt;] example. Once again, this project&#039;s initial setup is done the same as the above &amp;lt;code&amp;gt;hello&amp;lt;/code&amp;gt; example, but you&#039;ll create the new project using &amp;lt;code&amp;gt;kos-cargo new --lib addlib&amp;lt;/code&amp;gt; to specify that we&#039;re creating a library named &amp;lt;code&amp;gt;addlib&amp;lt;/code&amp;gt;. You&#039;ll also need to add the following text to this project&#039;s &amp;lt;code&amp;gt;Cargo.toml&amp;lt;/code&amp;gt; file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;toml&amp;quot;&amp;gt;&lt;br /&gt;
[lib]&lt;br /&gt;
crate-type = [&amp;quot;staticlib&amp;quot;]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This tells Rust to build a static &amp;lt;code&amp;gt;.a&amp;lt;/code&amp;gt; library archive file from our code, which is located in &amp;lt;code&amp;gt;src/lib.rs&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; line&amp;gt;&lt;br /&gt;
#[no_mangle]&lt;br /&gt;
pub extern &amp;quot;C&amp;quot; fn print_added(a: isize, b: isize) {&lt;br /&gt;
    print!(&amp;quot;{}&amp;quot;, a + b);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
#[no_mangle]&lt;br /&gt;
pub extern &amp;quot;C&amp;quot; fn add_integers(a: isize, b: isize) -&amp;gt; isize {&lt;br /&gt;
    a + b&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Two simple functions are provided: one for adding two integers and returning the result, and another for adding two integers and printing the result as text. Because these functions use &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;#[no_mangle]&amp;lt;/syntaxhighlight&amp;gt; and are declared &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;extern &amp;quot;C&amp;quot;&amp;lt;/syntaxhighlight&amp;gt;, they can be called by name in C code that links this library.&lt;br /&gt;
&lt;br /&gt;
When built using &amp;lt;code&amp;gt;kos-cargo build&amp;lt;/code&amp;gt;, a &amp;lt;code&amp;gt;target/sh-elf/debug/libaddlib.a&amp;lt;/code&amp;gt; file will be generated. This can be linked into other projects to gain the use of these functions.&lt;br /&gt;
&lt;br /&gt;
For example, this can be added to a standard &amp;lt;code&amp;gt;Makefile&amp;lt;/code&amp;gt;-based KallistiOS project by editing the &amp;lt;code&amp;gt;Makefile&amp;lt;/code&amp;gt;:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;make&amp;quot;&amp;gt;&lt;br /&gt;
$(TARGET): $(OBJS)&lt;br /&gt;
	kos-cc -o $(TARGET) $(OBJS) -L/opt/toolchains/dc/rust/examples/cargo-addlib/target/sh-elf/debug -laddlib&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then, we can use the code in our C source:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/* Declare the external function from the Rust library */&lt;br /&gt;
int add_integers(int a, int b);&lt;br /&gt;
&lt;br /&gt;
/* Use the function */&lt;br /&gt;
printf(&amp;quot;Five plus six is %d\n&amp;quot;, add_integers(5, 6));&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Compiling individual modules into object files with rustc==&lt;br /&gt;
If we&#039;d like to mix C and Rust code in the same &amp;lt;code&amp;gt;Makefile&amp;lt;/code&amp;gt;-based KallistiOS project without building an entirely separate library, we can do that as well. This is demonstrated in the [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/no_std/hello-make &amp;lt;code&amp;gt;hello-make&amp;lt;/code&amp;gt;] example.&lt;br /&gt;
&lt;br /&gt;
Instead of using &amp;lt;code&amp;gt;kos-cargo&amp;lt;/code&amp;gt;, we can invoke the &amp;lt;code&amp;gt;kos-rustc&amp;lt;/code&amp;gt; via a &amp;lt;code&amp;gt;Makefile&amp;lt;/code&amp;gt; to build Rust modules. If we assume the Rust file is named &amp;lt;code&amp;gt;example.rs&amp;lt;/code&amp;gt;, you&#039;ll need to add &amp;lt;code&amp;gt;example.o&amp;lt;/code&amp;gt; as an object file in your &amp;lt;code&amp;gt;Makefile&amp;lt;/code&amp;gt;&#039;s &amp;lt;code&amp;gt;OBJS =&amp;lt;/code&amp;gt; declaration. For example, if the project has two source files &amp;lt;code&amp;gt;hello_c.c&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;hello_rust.rs&amp;lt;/code&amp;gt;, our &amp;lt;code&amp;gt;Makefile&amp;lt;/code&amp;gt; would have a line like this:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;make&amp;quot;&amp;gt;&lt;br /&gt;
OBJS = hello_c.o hello_rust.o&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The example code demonstrates starting a C &amp;lt;code&amp;gt;main()&amp;lt;/code&amp;gt; function to call a Rust function which builds a &amp;lt;code&amp;gt;String&amp;lt;/code&amp;gt; containing the &amp;quot;Hello, world!&amp;quot; text which is passed back to a C function which prints &amp;lt;code&amp;gt;String&amp;lt;/code&amp;gt;s.&lt;br /&gt;
&lt;br /&gt;
==Using the libc crate with KallistiOS==&lt;br /&gt;
If any of the crates in your project&#039;s dependency tree requires the &#039;&#039;&#039;libc&#039;&#039;&#039; crate, you will need to override fetching the default crate with our custom libc crate with KallistiOS/Dreamcast definitions. This custom version was copied to &amp;lt;code&amp;gt;/opt/toolchains/dc/rust/libc&amp;lt;/code&amp;gt; when building the Rust sysroot earlier. Add the following text to your &amp;lt;code&amp;gt;Cargo.toml&amp;lt;/code&amp;gt;:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;toml&amp;quot;&amp;gt;&lt;br /&gt;
[patch.crates-io]&lt;br /&gt;
libc = { path = &amp;quot;/opt/toolchains/dc/rust/libc&amp;quot; }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Adjusting build flags==&lt;br /&gt;
Build settings can be adjusted through the &amp;lt;code&amp;gt;KOS_RCG_RUSTFLAGS&amp;lt;/code&amp;gt; variable in the &amp;lt;code&amp;gt;environ.sh&amp;lt;/code&amp;gt; file under the &amp;lt;code&amp;gt;### Rust Flags&amp;lt;/code&amp;gt; section. &lt;br /&gt;
* To pass arguments to the &#039;&#039;&#039;rustc&#039;&#039;&#039; frontend, e.g. to disable debug assertions:&lt;br /&gt;
** &amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot; inline&amp;gt;export KOS_RCG_RUSTFLAGS=&amp;quot;${KOS_RCG_RUSTFLAGS} -Cdebug-assertions=no&amp;quot;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* To pass arguments to the GCC backend, e.g. to enable the &amp;lt;code&amp;gt;-freorder-blocks-algorithm=simple&amp;lt;/code&amp;gt; optimization for Rust code:&lt;br /&gt;
** &amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot; inline&amp;gt;export KOS_RCG_RUSTFLAGS=&amp;quot;${KOS_RCG_RUSTFLAGS} -Cllvm-args=-freorder-blocks-algorithm=simple&amp;quot;&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Darc</name></author>
	</entry>
	<entry>
		<id>https://dreamcast.wiki/wiki/index.php?title=Rust_on_Dreamcast&amp;diff=3648</id>
		<title>Rust on Dreamcast</title>
		<link rel="alternate" type="text/html" href="https://dreamcast.wiki/wiki/index.php?title=Rust_on_Dreamcast&amp;diff=3648"/>
		<updated>2024-11-26T22:46:27Z</updated>

		<summary type="html">&lt;p&gt;Darc: /* Installing our custom KallistiOS and GCC toolchain */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[File:Rust-dc-logo.png|thumb|Ferris holding his Dreamcast controller]]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Rust&#039;&#039;&#039; is a popular modern programming language focusing on performance, memory safety, and program correctness. As a &amp;quot;blazingly fast&amp;quot; low-level language, it is viable for running on a Dreamcast console. Doing so presents a bit of a challenge, however, as the official Rust compiler is based on the [https://llvm.org/ LLVM] toolchain infrastructure, which does not support the Dreamcast CPU&#039;s SuperH architecture. Dreamcast programming is instead typically done with [https://gcc.gnu.org/ GCC], the GNU Compiler Collection. There are currently two viable solutions to this challenge:&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;rustc_codegen_gcc&#039;&#039;&#039;, the current preferred method: An experimental codegen backend for the official Rust &#039;&#039;&#039;rustc&#039;&#039;&#039; compiler which interfaces with GCC&#039;s libgccjit API&lt;br /&gt;
* &#039;&#039;&#039;gccrs&#039;&#039;&#039;: an experimental Rust frontend for GCC&lt;br /&gt;
&lt;br /&gt;
Neither solution is complete at this time, but &#039;&#039;&#039;rustc_codegen_gcc&#039;&#039;&#039; is much further along and is quite usable with some patience with its current limitations. On the other hand, &#039;&#039;&#039;gccrs&#039;&#039;&#039; can compile for Dreamcast, but is in an earlier state. Below we will focus on using &#039;&#039;&#039;rustc_codegen_gcc&#039;&#039;&#039;. For more information on using gccrs, see the [[gccrs]] page.&lt;br /&gt;
&lt;br /&gt;
=Current Status=&lt;br /&gt;
* The full &#039;&#039;&#039;std&#039;&#039;&#039; library is supported.&lt;br /&gt;
* A [https://github.com/dreamcast-rs/KallistiOS custom version of KallistiOS] v2.1.0 (latest stable release) is used.&lt;br /&gt;
** This version includes minor changes for Rust and a beta libpthread addon layer to interface with the Rust standard library.&lt;br /&gt;
** Wrappers are provided to invoke &#039;&#039;&#039;cargo&#039;&#039;&#039; and &#039;&#039;&#039;rustc&#039;&#039;&#039; in a familiar manner to compile projects.&lt;br /&gt;
* A [https://github.com/dreamcast-rs/kos-sys &#039;&#039;&#039;kos-sys&#039;&#039;&#039;] raw/unsafe bindings crate is provided for easily linking KallistiOS and its unsafe C API into Rust crates. This crate is in active development, but with a large portion of KallistiOS functionality available now via C FFI.&lt;br /&gt;
* A [https://github.com/dreamcast-rs/kos-rs &#039;&#039;&#039;kos-rs&#039;&#039;&#039;] Rust crate with safe idiomatic interfaces is available but is in very early stages.&lt;br /&gt;
** Documentation for the bindings is available at [https://kos-rs.dreamcast.wiki/ kos-rs.dreamcast.wiki].&lt;br /&gt;
** If bindings do not yet exist for desired functionality, wrapping of KallistiOS C functions is required to get proper use of KallistiOS at this time. See the [https://doc.rust-lang.org/nomicon/ffi.html Foreign Function Interface page] in the Rustonomicon for further understanding. &lt;br /&gt;
** A custom [https://github.com/dreamcast-rs/libc/tree/libc-0.2-kos &#039;&#039;&#039;libc&#039;&#039;&#039; crate with KallistiOS support] is provided.&lt;br /&gt;
* Code generation uses &#039;&#039;&#039;rustc_codegen_gcc&#039;&#039;&#039;, so its limitations apply here. See the [https://blog.antoyo.xyz/ development blog] for more information on its progress.&lt;br /&gt;
** rustc_codegen_gcc is pinned to a particular Rust nightly version, but is synced regularly. The current version in use is the &#039;&#039;&#039;2024-08-10&#039;&#039;&#039; nightly.&lt;br /&gt;
** A [https://github.com/dreamcast-rs/rust/tree/kos-2024-08-10 KallistiOS-patched version of the latest nightly] is maintained and updated regularly. &lt;br /&gt;
** Panic unwinding, debug info, LTO, etc. are not currently available.&lt;br /&gt;
** Architectures dependent on rustc_codegen_gcc are not yet able to be added to the rustc frontend, so a workaround is necessary: MIPS is falsely selected for KallistiOS, causing rustc to emit objects with a MIPS header and a provided wrapper is used when linking to rewrite these MIPS headers to SuperH before passing the compiled objects to the linker.&lt;br /&gt;
* A separate [https://github.com/rust-lang/gcc development version of the GCC toolchain] must be used.&lt;br /&gt;
** This version of GCC contains the latest libgccjit patches necessary to interface with rustc_codegen_gcc, and will be built to work with the custom KallistiOS&#039;s libpthread layer.&lt;br /&gt;
** New updates to rustc_codegen_gcc often require new updates to GCC, so recompiling GCC regularly will be necessary to keep up with the latest updates.&lt;br /&gt;
** The development version of GCC does not always compile well on every platform. Using a modern Linux platform is recommended for maximum compatibility.&lt;br /&gt;
** The custom KallistiOS and GCC environment can still be used to compile code or projects written in C, C++, etc.&lt;br /&gt;
* KallistiOS and all related libraries and tools must be compiled using the &amp;lt;code&amp;gt;-m4-single&amp;lt;/code&amp;gt; ABI. This is already set up for you in the guide below, but if external libraries are being linked to Rust projects, make sure that code is compiled using the proper ABI.&lt;br /&gt;
&lt;br /&gt;
=Setting up a Rust environment for Dreamcast development=&lt;br /&gt;
The following guide will provide instructions for setting up a Rust development environment for Dreamcast. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;If you run into any errors or other challenges while following this tutorial, or simply need clarification on any of the steps, feel free to ask for assistance on the [https://dcemulation.org/phpBB/viewforum.php?f=29 message board] or [https://discord.gg/wDEfkUh63h our Discord server] and we would be happy to aid you and update the guide for the benefit of future readers and others in the community.&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
==Prerequisites==&lt;br /&gt;
First, we will need to set up the prerequisites.&lt;br /&gt;
&lt;br /&gt;
* You must have the [https://rustup.rs/ rustup] tool installed for your operating system to manage Rust toolchain installations. It will automatically manage the latest Rust nightly installations for you. You cannot use a Rust toolchain that may be provided by your operating system vendor.&lt;br /&gt;
* Install the dependency packages for your operating system. These are listed in the &#039;&#039;&#039;Dependencies&#039;&#039;&#039; section on the [[Getting Started with Dreamcast development#Dependencies]] page, with lists provided for most common operating systems.&lt;br /&gt;
* We will install our custom Rust toolchain environment within &amp;lt;code&amp;gt;/opt/toolchains/dc/rust&amp;lt;/code&amp;gt;. If it does not already exist, create the &amp;lt;code&amp;gt;/opt/toolchains/dc&amp;lt;/code&amp;gt; directory and grant it proper permissions using the following commands. &lt;br /&gt;
 sudo mkdir -p /opt/toolchains/dc&lt;br /&gt;
 sudo chmod -R 755 /opt/toolchains/dc&lt;br /&gt;
 sudo chown -R $(id -u):$(id -g) /opt/toolchains/dc&lt;br /&gt;
* Clone the [https://github.com/dreamcast-rs/rust-for-dreamcast/ Rust for Dreamcast] repo containing necessary support files to &amp;lt;code&amp;gt;/opt/toolchains/dc/rust&amp;lt;/code&amp;gt;:&lt;br /&gt;
 git clone https://github.com/dreamcast-rs/rust-for-dreamcast.git /opt/toolchains/dc/rust&lt;br /&gt;
&lt;br /&gt;
==Installing our custom KallistiOS and GCC toolchain==&lt;br /&gt;
Clone the git repository for our custom Rust-patched version of KallistiOS v2.1.0 stable:&lt;br /&gt;
 git clone https://github.com/dreamcast-rs/KallistiOS /opt/toolchains/dc/rust/kos&lt;br /&gt;
&lt;br /&gt;
Enter the dc-chain directory:&lt;br /&gt;
 cd /opt/toolchains/dc/rust/kos/utils/dc-chain&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;Makefile.default.cfg&amp;lt;/code&amp;gt; file in this directory has been customized for you, selecting the proper toolchain profile, paths, and build options necessary for building a Rust toolchain using this guide. It is not recommended to adjust the defaults.&lt;br /&gt;
&lt;br /&gt;
To build the GCC toolchain using 2 CPU cores, simply type:&lt;br /&gt;
 make makejobs=2&lt;br /&gt;
Adjust the &amp;lt;code&amp;gt;makejobs=...&amp;lt;/code&amp;gt; value to use the number of CPU cores available to you on your system in order to speed up compilation. This may take a while to build, so be patient.&lt;br /&gt;
&lt;br /&gt;
Once the GCC toolchain is built, source the &amp;lt;code&amp;gt;environ.sh&amp;lt;/code&amp;gt; KallistiOS environment settings. A custom &amp;lt;code&amp;gt;environ.sh&amp;lt;/code&amp;gt; file has been provided for you, containing settings pre-adjusted for Rust development using this guide.&lt;br /&gt;
&lt;br /&gt;
You will need to run the source command to apply the KallistiOS environment settings to your currently running shell. Run the following now, &#039;&#039;&#039;and&#039;&#039;&#039; &#039;&#039;whenever&#039;&#039; you open a new shell to work on Dreamcast projects:&lt;br /&gt;
 source /opt/toolchains/dc/rust/misc/environ.sh&lt;br /&gt;
&lt;br /&gt;
Enter the KallistiOS directory:&lt;br /&gt;
 cd /opt/toolchains/dc/rust/kos&lt;br /&gt;
&lt;br /&gt;
Build KallistiOS:&lt;br /&gt;
 make&lt;br /&gt;
&lt;br /&gt;
==Building rustc_codegen_gcc and Rust sysroot==&lt;br /&gt;
Now that we have a working KallistiOS environment suitable for Rust development set up, we can build the Rust compiler compiler and sysroot components. A script is provided which will download the necessary components from the respective repositories and compile them for you. Run the installer script:&lt;br /&gt;
&lt;br /&gt;
 /opt/toolchains/dc/rust/misc/install-rust.sh&lt;br /&gt;
&lt;br /&gt;
If all goes well, you&#039;ll see &#039;&#039;&#039;Rust for KallistiOS/Dreamcast installed!&#039;&#039;&#039; message!&lt;br /&gt;
&lt;br /&gt;
==Building KOS ports libraries==&lt;br /&gt;
If desired, you may also built the KOS ports collection of libraries for use with your Rust environment. It is recommended to maintain a separate KOS ports installation for linking with Rust projects in &amp;lt;code&amp;gt;/opt/toolchains/dc/rust/kos-ports&amp;lt;/code&amp;gt;. This is because the Rust environment is using the &amp;lt;code&amp;gt;-m4-single&amp;lt;/code&amp;gt; ABI, but the KallistiOS default installation will be using the &amp;lt;code&amp;gt;-m4-single-only&amp;lt;/code&amp;gt;, which cannot be mixed and matched.&lt;br /&gt;
&lt;br /&gt;
Clone the kos-ports repository to your system:&lt;br /&gt;
 git clone https://github.com/KallistiOS/kos-ports /opt/toolchains/dc/rust/kos-ports&lt;br /&gt;
An individual port can be built and installed by entering its directory and running the proper command. For example, installing GLdc:&lt;br /&gt;
 cd /opt/toolchains/dc/rust/kos-ports/libGL&lt;br /&gt;
 make install&lt;br /&gt;
Or, instead, you may run the script to build all of the included ports:&lt;br /&gt;
 /opt/toolchains/dc/rust/kos-ports/utils/build-all.sh&lt;br /&gt;
&lt;br /&gt;
=Using Rust for Dreamcast=&lt;br /&gt;
If all went well, you will now have a working Rust for Dreamcast environment!&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;cargo&#039;&#039;&#039; can be invoked to target Dreamcast using the &amp;lt;code&amp;gt;kos-cargo&amp;lt;/code&amp;gt; command.&lt;br /&gt;
** &amp;lt;code&amp;gt;kos-cargo run&amp;lt;/code&amp;gt; can be used to build and run code on a Dreamcast console using [[dcload-ip]] or [[dcload-serial]]. You will need to set up the &amp;lt;code&amp;gt;KOS_LOADER&amp;lt;/code&amp;gt; variable with the necessary command in the &amp;lt;code&amp;gt;/opt/toolchains/dc/rust/misc/environ.sh&amp;lt;/code&amp;gt; file (and then re-source the file for the change to take effect).&lt;br /&gt;
* &#039;&#039;&#039;rustc&#039;&#039;&#039; can be invoked to target Dreamcast using the &amp;lt;code&amp;gt;kos-rustc&amp;lt;/code&amp;gt; command.&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
Examples are included with the Rust for Dreamcast repo to help you get started.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/hello-cargo &amp;lt;code&amp;gt;hello-cargo&amp;lt;/code&amp;gt;] demonstrates how to create a simple &amp;quot;Hello, world!&amp;quot; application using cargo.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/hello-make &amp;lt;code&amp;gt;hello-make&amp;lt;/code&amp;gt;] demonstrates how to create a simple &amp;quot;Hello, world!&amp;quot; application using standard Makefiles.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/gldc-cube &amp;lt;code&amp;gt;gldc-cube&amp;lt;/code&amp;gt;] demonstrates a Rust project using the GLdc KOS port.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/addlib &amp;lt;code&amp;gt;addlib&amp;lt;/code&amp;gt;] demonstrates how to create a Rust library that can be included with a KallistiOS project.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/atomics &amp;lt;code&amp;gt;atomics&amp;lt;/code&amp;gt;] demonstrates the use of atomic functions.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/filesystem-io &amp;lt;code&amp;gt;filesystem-io&amp;lt;/code&amp;gt;] demonstrates listing directory contents and opening files.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/network-time &amp;lt;code&amp;gt;network-time&amp;lt;/code&amp;gt;] demonstrates displaying the system time and retrieving the current UTC time from an NTP server using the [[Broadband adapter]] or [[LAN adapter]].&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/pvr_strips_direct &amp;lt;code&amp;gt;pvr_strips_direct&amp;lt;/code&amp;gt;] demonstrates the use of PVR direct rendering&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/threads &amp;lt;code&amp;gt;threads&amp;lt;/code&amp;gt;] demonstrates the use of threads.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/tokio-async &amp;lt;code&amp;gt;tokio-async&amp;lt;/code&amp;gt;] demonstrates the use of the &#039;&#039;&#039;tokio&#039;&#039;&#039; async runtime.&lt;br /&gt;
&lt;br /&gt;
==Creating a new Rust project with Cargo==&lt;br /&gt;
First, we&#039;ll demonstrate creating a new &amp;quot;Hello, world!&amp;quot; project with &amp;lt;code&amp;gt;kos-cargo&amp;lt;/code&amp;gt;. This will follow the [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/hello-cargo &amp;lt;code&amp;gt;cargo-hello&amp;lt;/code&amp;gt;] example included in the Rust-for-Dreamcast repo. &lt;br /&gt;
&lt;br /&gt;
In a directory of your choosing, let&#039;s invoke &amp;lt;code&amp;gt;kos-cargo&amp;lt;/code&amp;gt; to create a new project and then enter the directory:&lt;br /&gt;
 kos-cargo new hello&lt;br /&gt;
 cd hello&lt;br /&gt;
&lt;br /&gt;
Let&#039;s add our kos-rs crate to gain access to current KallistiOS bindings. Open &amp;lt;code&amp;gt;Cargo.toml&amp;lt;/code&amp;gt; in your text editor and add:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;toml&amp;quot;&amp;gt;&lt;br /&gt;
[dependencies]&lt;br /&gt;
kos = { package = &amp;quot;kos-rs&amp;quot;, git = &amp;quot;https://github.com/dreamcast-rs/kos-rs&amp;quot; }&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we can open up &amp;lt;code&amp;gt;src/main.rs&amp;lt;/code&amp;gt; and write our &amp;quot;Hello, world!&amp;quot; example code:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; line&amp;gt;&lt;br /&gt;
fn main() {&lt;br /&gt;
    println!(&amp;quot;Hello, world from Rust!&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we can use &amp;lt;code&amp;gt;kos-cargo build&amp;lt;/code&amp;gt; to build our project. If all goes well, there will be a &amp;lt;code&amp;gt;target/sh-elf/debug/hello.elf&amp;lt;/code&amp;gt; file that can be sent to the Dreamcast with &amp;lt;code&amp;gt;dc-tool&amp;lt;/code&amp;gt;.&lt;br /&gt;
If you have &amp;lt;code&amp;gt;KOS_LOADER&amp;lt;/code&amp;gt; set in the &amp;lt;code&amp;gt;environ.sh&amp;lt;/code&amp;gt; file, you can invoke it directly with &amp;lt;code&amp;gt;kos-cargo run&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Creating a Rust project using kos-ports libraries==&lt;br /&gt;
[[File:Rust-Cube rustc codegen-gcc demo.gif|thumb|cube example in action]]&lt;br /&gt;
The [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/gldc-cube &amp;lt;code&amp;gt;gldc-cube&amp;lt;/code&amp;gt;] example demonstrates creating a rotating 3D cube using Rust as the primary language, while calling C functions provided by the &#039;&#039;&#039;GLdc&#039;&#039;&#039; library available in kos-ports. This project&#039;s initial setup is done the same as the above &amp;lt;code&amp;gt;hello&amp;lt;/code&amp;gt; example.&lt;br /&gt;
&lt;br /&gt;
For this example, we&#039;ll need to convert JPEG textures to VQ-encoded textures at compile time using the &amp;lt;code&amp;gt;vqenc&amp;lt;/code&amp;gt; utility includes with KallistiOS. To do this, we&#039;ll add a &amp;lt;code&amp;gt;build.rs&amp;lt;/code&amp;gt; file to the root of the crate with the following code (abridged to show the conversion of only one texture):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot;&amp;gt;&lt;br /&gt;
fn main() {&lt;br /&gt;
    let kos_base = std::env::var(&amp;quot;KOS_BASE&amp;quot;).expect(&amp;quot;Missing $KOS_BASE -- KallistiOS environment not sourced!&amp;quot;);&lt;br /&gt;
    let vqenc_cmd = kos_base + &amp;quot;/utils/vqenc/vqenc&amp;quot;;&lt;br /&gt;
    Command::new(&amp;amp;vqenc_cmd)&lt;br /&gt;
        .arg(&amp;quot;-t&amp;quot;)&lt;br /&gt;
        .arg(&amp;quot;-v&amp;quot;)&lt;br /&gt;
        .arg(&amp;quot;rsrc/tex_claw.jpg&amp;quot;)&lt;br /&gt;
        .output()&lt;br /&gt;
        .expect(&amp;quot;vqenc on tex_claw.jpg failed!&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
The texture files are then included in our project using the &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;include_bytes!&amp;lt;/syntaxhighlight&amp;gt; macro.&lt;br /&gt;
&lt;br /&gt;
We will need to link the GLdc library in this example, but we don&#039;t need to do anything special to add the paths to this library, because adding the common paths to KallistiOS libraries is already done for us in the kos-rs crate. However, if in your project you need to include a separate unique library path, you can add it in your &amp;lt;code&amp;gt;build.rs&amp;lt;/code&amp;gt; like so:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot;&amp;gt;&lt;br /&gt;
println!(&amp;quot;cargo:rustc-link-search=native={}&amp;quot;, lib_path);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The workings of this example&#039;s source code are too great to detail here line-by-line, but the example demonstrates how to use GLdc via the [https://github.com/dreamcast-rs/gldc-sys gldc-sys] crate, which contains unsafe/raw bindings to GLdc. Check out this crate&#039;s source code for an example of declaring and binding external C functions, constants, and structures so they can be used in Rust code. Since the entirety of the gldc-cube example uses C FFI functions without safe wrappers, the &amp;lt;code&amp;gt;main()&amp;lt;/code&amp;gt; source is wrapped in &amp;lt;code&amp;gt;unsafe {}&amp;lt;/code&amp;gt;. In the future, this would be much less necessary as higher level safe Rust bindings to KallistiOS and other libraries become mature.&lt;br /&gt;
&lt;br /&gt;
==Creating a Rust library==&lt;br /&gt;
Next, we&#039;ll demonstrate creating a Rust library with &amp;lt;code&amp;gt;kos-cargo&amp;lt;/code&amp;gt; that can be included in other Dreamcast code. This will follow the [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/addlib &amp;lt;code&amp;gt;addlib&amp;lt;/code&amp;gt;] example. Once again, this project&#039;s initial setup is done the same as the above &amp;lt;code&amp;gt;hello&amp;lt;/code&amp;gt; example, but you&#039;ll create the new project using &amp;lt;code&amp;gt;kos-cargo new --lib addlib&amp;lt;/code&amp;gt; to specify that we&#039;re creating a library named &amp;lt;code&amp;gt;addlib&amp;lt;/code&amp;gt;. You&#039;ll also need to add the following text to this project&#039;s &amp;lt;code&amp;gt;Cargo.toml&amp;lt;/code&amp;gt; file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;toml&amp;quot;&amp;gt;&lt;br /&gt;
[lib]&lt;br /&gt;
crate-type = [&amp;quot;staticlib&amp;quot;]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This tells Rust to build a static &amp;lt;code&amp;gt;.a&amp;lt;/code&amp;gt; library archive file from our code, which is located in &amp;lt;code&amp;gt;src/lib.rs&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; line&amp;gt;&lt;br /&gt;
#[no_mangle]&lt;br /&gt;
pub extern &amp;quot;C&amp;quot; fn print_added(a: isize, b: isize) {&lt;br /&gt;
    print!(&amp;quot;{}&amp;quot;, a + b);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
#[no_mangle]&lt;br /&gt;
pub extern &amp;quot;C&amp;quot; fn add_integers(a: isize, b: isize) -&amp;gt; isize {&lt;br /&gt;
    a + b&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The source code here starts similarly to the &amp;quot;Hello, world!&amp;quot; example, except we don&#039;t need to specify &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;#![no_main]&amp;lt;/syntaxhighlight&amp;gt; as this is a library which wouldn&#039;t have a &amp;lt;code&amp;gt;main()&amp;lt;/code&amp;gt; function anyway.&lt;br /&gt;
&lt;br /&gt;
Two simple functions are provided: one for adding two integers and returning the result, and another for adding two integers and printing the result as text. Because these functions use &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;#[no_mangle]&amp;lt;/syntaxhighlight&amp;gt; and are declared &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;extern &amp;quot;C&amp;quot;&amp;lt;/syntaxhighlight&amp;gt;, they can be called by name in C code that links this library.&lt;br /&gt;
&lt;br /&gt;
When built using &amp;lt;code&amp;gt;kos-cargo build&amp;lt;/code&amp;gt;, a &amp;lt;code&amp;gt;target/sh-elf/debug/libaddlib.a&amp;lt;/code&amp;gt; file will be generated. This can be linked into other projects to gain the use of these functions.&lt;br /&gt;
&lt;br /&gt;
For example, this can be added to a standard &amp;lt;code&amp;gt;Makefile&amp;lt;/code&amp;gt;-based KallistiOS project by editing the &amp;lt;code&amp;gt;Makefile&amp;lt;/code&amp;gt;:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;make&amp;quot;&amp;gt;&lt;br /&gt;
$(TARGET): $(OBJS)&lt;br /&gt;
	kos-cc -o $(TARGET) $(OBJS) -L/opt/toolchains/dc/rust/examples/cargo-addlib/target/sh-elf/debug -laddlib&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then, we can use the code in our C source:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/* Declare the external function from the Rust library */&lt;br /&gt;
int add_integers(int a, int b);&lt;br /&gt;
&lt;br /&gt;
/* Use the function */&lt;br /&gt;
printf(&amp;quot;Five plus six is %d\n&amp;quot;, add_integers(5, 6));&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Compiling individual modules into object files with rustc==&lt;br /&gt;
If we&#039;d like to mix C and Rust code in the same &amp;lt;code&amp;gt;Makefile&amp;lt;/code&amp;gt;-based KallistiOS project without building an entirely separate library, we can do that as well. This is demonstrated in the [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/no_std/hello-make &amp;lt;code&amp;gt;hello-make&amp;lt;/code&amp;gt;] example.&lt;br /&gt;
&lt;br /&gt;
Instead of using &amp;lt;code&amp;gt;kos-cargo&amp;lt;/code&amp;gt;, we can invoke the &amp;lt;code&amp;gt;kos-rustc&amp;lt;/code&amp;gt; via a &amp;lt;code&amp;gt;Makefile&amp;lt;/code&amp;gt; to build Rust modules. If we assume the Rust file is named &amp;lt;code&amp;gt;example.rs&amp;lt;/code&amp;gt;, you&#039;ll need to add &amp;lt;code&amp;gt;example.o&amp;lt;/code&amp;gt; as an object file in your &amp;lt;code&amp;gt;Makefile&amp;lt;/code&amp;gt;&#039;s &amp;lt;code&amp;gt;OBJS =&amp;lt;/code&amp;gt; declaration. For example, if the project has two source files &amp;lt;code&amp;gt;hello_c.c&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;hello_rust.rs&amp;lt;/code&amp;gt;, our &amp;lt;code&amp;gt;Makefile&amp;lt;/code&amp;gt; would have a line like this:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;make&amp;quot;&amp;gt;&lt;br /&gt;
OBJS = hello_c.o hello_rust.o&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The example code demonstrates starting a C &amp;lt;code&amp;gt;main()&amp;lt;/code&amp;gt; function to call a Rust function which builds a &amp;lt;code&amp;gt;String&amp;lt;/code&amp;gt; containing the &amp;quot;Hello, world!&amp;quot; text which is passed back to a C function which prints &amp;lt;code&amp;gt;String&amp;lt;/code&amp;gt;s.&lt;br /&gt;
&lt;br /&gt;
==Using the libc crate with KallistiOS==&lt;br /&gt;
If any of the crates in your project&#039;s dependency tree requires the &#039;&#039;&#039;libc&#039;&#039;&#039; crate, you will need to override fetching the default crate with our custom libc crate with KallistiOS/Dreamcast definitions. This custom version was copied to &amp;lt;code&amp;gt;/opt/toolchains/dc/rust/libc&amp;lt;/code&amp;gt; when building the Rust sysroot earlier. Add the following text to your &amp;lt;code&amp;gt;Cargo.toml&amp;lt;/code&amp;gt;:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;toml&amp;quot;&amp;gt;&lt;br /&gt;
[patch.crates-io]&lt;br /&gt;
libc = { path = &amp;quot;/opt/toolchains/dc/rust/libc&amp;quot; }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Adjusting build flags==&lt;br /&gt;
Build settings can be adjusted through the &amp;lt;code&amp;gt;KOS_RCG_RUSTFLAGS&amp;lt;/code&amp;gt; variable in the &amp;lt;code&amp;gt;environ.sh&amp;lt;/code&amp;gt; file under the &amp;lt;code&amp;gt;### Rust Flags&amp;lt;/code&amp;gt; section. &lt;br /&gt;
* To pass arguments to the &#039;&#039;&#039;rustc&#039;&#039;&#039; frontend, e.g. to disable debug assertions:&lt;br /&gt;
** &amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot; inline&amp;gt;export KOS_RCG_RUSTFLAGS=&amp;quot;${KOS_RCG_RUSTFLAGS} -Cdebug-assertions=no&amp;quot;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* To pass arguments to the GCC backend, e.g. to enable the &amp;lt;code&amp;gt;-freorder-blocks-algorithm=simple&amp;lt;/code&amp;gt; optimization for Rust code:&lt;br /&gt;
** &amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot; inline&amp;gt;export KOS_RCG_RUSTFLAGS=&amp;quot;${KOS_RCG_RUSTFLAGS} -Cllvm-args=-freorder-blocks-algorithm=simple&amp;quot;&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Darc</name></author>
	</entry>
	<entry>
		<id>https://dreamcast.wiki/wiki/index.php?title=Rust_on_Dreamcast&amp;diff=3644</id>
		<title>Rust on Dreamcast</title>
		<link rel="alternate" type="text/html" href="https://dreamcast.wiki/wiki/index.php?title=Rust_on_Dreamcast&amp;diff=3644"/>
		<updated>2024-11-25T18:00:06Z</updated>

		<summary type="html">&lt;p&gt;Darc: /* Installing our custom KallistiOS and GCC toolchain */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[File:Rust-dc-logo.png|thumb|Ferris holding his Dreamcast controller]]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Rust&#039;&#039;&#039; is a popular modern programming language focusing on performance, memory safety, and program correctness. As a &amp;quot;blazingly fast&amp;quot; low-level language, it is viable for running on a Dreamcast console. Doing so presents a bit of a challenge, however, as the official Rust compiler is based on the [https://llvm.org/ LLVM] toolchain infrastructure, which does not support the Dreamcast CPU&#039;s SuperH architecture. Dreamcast programming is instead typically done with [https://gcc.gnu.org/ GCC], the GNU Compiler Collection. There are currently two viable solutions to this challenge:&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;rustc_codegen_gcc&#039;&#039;&#039;, the current preferred method: An experimental codegen backend for the official Rust &#039;&#039;&#039;rustc&#039;&#039;&#039; compiler which interfaces with GCC&#039;s libgccjit API&lt;br /&gt;
* &#039;&#039;&#039;gccrs&#039;&#039;&#039;: an experimental Rust frontend for GCC&lt;br /&gt;
&lt;br /&gt;
Neither solution is complete at this time, but &#039;&#039;&#039;rustc_codegen_gcc&#039;&#039;&#039; is much further along and is quite usable with some patience with its current limitations. On the other hand, &#039;&#039;&#039;gccrs&#039;&#039;&#039; can compile for Dreamcast, but is in an earlier state. Below we will focus on using &#039;&#039;&#039;rustc_codegen_gcc&#039;&#039;&#039;. For more information on using gccrs, see the [[gccrs]] page.&lt;br /&gt;
&lt;br /&gt;
=Current Status=&lt;br /&gt;
* The full &#039;&#039;&#039;std&#039;&#039;&#039; library is supported.&lt;br /&gt;
* A [https://github.com/dreamcast-rs/KallistiOS custom version of KallistiOS] v2.1.0 (latest stable release) is used.&lt;br /&gt;
** This version includes minor changes for Rust and a beta libpthread addon layer to interface with the Rust standard library.&lt;br /&gt;
** Wrappers are provided to invoke &#039;&#039;&#039;cargo&#039;&#039;&#039; and &#039;&#039;&#039;rustc&#039;&#039;&#039; in a familiar manner to compile projects.&lt;br /&gt;
* A [https://github.com/dreamcast-rs/kos-sys &#039;&#039;&#039;kos-sys&#039;&#039;&#039;] raw/unsafe bindings crate is provided for easily linking KallistiOS and its unsafe C API into Rust crates. This crate is in active development, but with a large portion of KallistiOS functionality available now via C FFI.&lt;br /&gt;
* A [https://github.com/dreamcast-rs/kos-rs &#039;&#039;&#039;kos-rs&#039;&#039;&#039;] Rust crate with safe idiomatic interfaces is available but is in very early stages.&lt;br /&gt;
** Documentation for the bindings is available at [https://kos-rs.dreamcast.wiki/ kos-rs.dreamcast.wiki].&lt;br /&gt;
** If bindings do not yet exist for desired functionality, wrapping of KallistiOS C functions is required to get proper use of KallistiOS at this time. See the [https://doc.rust-lang.org/nomicon/ffi.html Foreign Function Interface page] in the Rustonomicon for further understanding. &lt;br /&gt;
** A custom [https://github.com/dreamcast-rs/libc/tree/libc-0.2-kos &#039;&#039;&#039;libc&#039;&#039;&#039; crate with KallistiOS support] is provided.&lt;br /&gt;
* Code generation uses &#039;&#039;&#039;rustc_codegen_gcc&#039;&#039;&#039;, so its limitations apply here. See the [https://blog.antoyo.xyz/ development blog] for more information on its progress.&lt;br /&gt;
** rustc_codegen_gcc is pinned to a particular Rust nightly version, but is synced regularly. The current version in use is the &#039;&#039;&#039;2024-08-10&#039;&#039;&#039; nightly.&lt;br /&gt;
** A [https://github.com/dreamcast-rs/rust/tree/kos-2024-08-10 KallistiOS-patched version of the latest nightly] is maintained and updated regularly. &lt;br /&gt;
** Panic unwinding, debug info, LTO, etc. are not currently available.&lt;br /&gt;
** Architectures dependent on rustc_codegen_gcc are not yet able to be added to the rustc frontend, so a workaround is necessary: MIPS is falsely selected for KallistiOS, causing rustc to emit objects with a MIPS header and a provided wrapper is used when linking to rewrite these MIPS headers to SuperH before passing the compiled objects to the linker.&lt;br /&gt;
* A separate [https://github.com/rust-lang/gcc development version of the GCC toolchain] must be used.&lt;br /&gt;
** This version of GCC contains the latest libgccjit patches necessary to interface with rustc_codegen_gcc, and will be built to work with the custom KallistiOS&#039;s libpthread layer.&lt;br /&gt;
** New updates to rustc_codegen_gcc often require new updates to GCC, so recompiling GCC regularly will be necessary to keep up with the latest updates.&lt;br /&gt;
** The development version of GCC does not always compile well on every platform. Using a modern Linux platform is recommended for maximum compatibility.&lt;br /&gt;
** The custom KallistiOS and GCC environment can still be used to compile code or projects written in C, C++, etc.&lt;br /&gt;
* KallistiOS and all related libraries and tools must be compiled using the &amp;lt;code&amp;gt;-m4-single&amp;lt;/code&amp;gt; ABI. This is already set up for you in the guide below, but if external libraries are being linked to Rust projects, make sure that code is compiled using the proper ABI.&lt;br /&gt;
&lt;br /&gt;
=Setting up a Rust environment for Dreamcast development=&lt;br /&gt;
The following guide will provide instructions for setting up a Rust development environment for Dreamcast. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;If you run into any errors or other challenges while following this tutorial, or simply need clarification on any of the steps, feel free to ask for assistance on the [https://dcemulation.org/phpBB/viewforum.php?f=29 message board] or [https://discord.gg/wDEfkUh63h our Discord server] and we would be happy to aid you and update the guide for the benefit of future readers and others in the community.&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
==Prerequisites==&lt;br /&gt;
First, we will need to set up the prerequisites.&lt;br /&gt;
&lt;br /&gt;
* You must have the [https://rustup.rs/ rustup] tool installed for your operating system to manage Rust toolchain installations. It will automatically manage the latest Rust nightly installations for you. You cannot use a Rust toolchain that may be provided by your operating system vendor.&lt;br /&gt;
* Install the dependency packages for your operating system. These are listed in the &#039;&#039;&#039;Dependencies&#039;&#039;&#039; section on the [[Getting Started with Dreamcast development#Dependencies]] page, with lists provided for most common operating systems.&lt;br /&gt;
* We will install our custom Rust toolchain environment within &amp;lt;code&amp;gt;/opt/toolchains/dc/rust&amp;lt;/code&amp;gt;. If it does not already exist, create the &amp;lt;code&amp;gt;/opt/toolchains/dc&amp;lt;/code&amp;gt; directory and grant it proper permissions using the following commands. &lt;br /&gt;
 sudo mkdir -p /opt/toolchains/dc&lt;br /&gt;
 sudo chmod -R 755 /opt/toolchains/dc&lt;br /&gt;
 sudo chown -R $(id -u):$(id -g) /opt/toolchains/dc&lt;br /&gt;
* Clone the [https://github.com/dreamcast-rs/rust-for-dreamcast/ Rust for Dreamcast] repo containing necessary support files to &amp;lt;code&amp;gt;/opt/toolchains/dc/rust&amp;lt;/code&amp;gt;:&lt;br /&gt;
 git clone https://github.com/dreamcast-rs/rust-for-dreamcast.git /opt/toolchains/dc/rust&lt;br /&gt;
&lt;br /&gt;
==Installing our custom KallistiOS and GCC toolchain==&lt;br /&gt;
Clone the git repository for our custom Rust-patched version of KallistiOS v2.1.0 stable:&lt;br /&gt;
 git clone https://github.com/dreamcast-rs/KallistiOS /opt/toolchains/dc/rust/kos&lt;br /&gt;
&lt;br /&gt;
Enter the dc-chain directory:&lt;br /&gt;
 cd /opt/toolchains/dc/rust/kos/utils/dc-chain&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;Makefile.default.cfg&amp;lt;/code&amp;gt; file in this directory has been customized for you, selecting the proper toolchain profile, paths, and build options necessary for building a Rust toolchain using this guide. It is not recommended to adjust the defaults.&lt;br /&gt;
&lt;br /&gt;
To build the GCC toolchain using 2 CPU cores, simply type:&lt;br /&gt;
 make makejobs=2 sh_gcc_git_branch=master-88b2da3c175d34d65415f69961420f35ac1a1c56&lt;br /&gt;
Adjust the &amp;lt;code&amp;gt;makejobs=...&amp;lt;/code&amp;gt; value to use the number of CPU cores available to you on your system in order to speed up compilation. This may take a while to build, so be patient.&lt;br /&gt;
&lt;br /&gt;
Once the GCC toolchain is built, source the &amp;lt;code&amp;gt;environ.sh&amp;lt;/code&amp;gt; KallistiOS environment settings. A custom &amp;lt;code&amp;gt;environ.sh&amp;lt;/code&amp;gt; file has been provided for you, containing settings pre-adjusted for Rust development using this guide.&lt;br /&gt;
&lt;br /&gt;
You will need to run the source command to apply the KallistiOS environment settings to your currently running shell. Run the following now, &#039;&#039;&#039;and&#039;&#039;&#039; &#039;&#039;whenever&#039;&#039; you open a new shell to work on Dreamcast projects:&lt;br /&gt;
 source /opt/toolchains/dc/rust/misc/environ.sh&lt;br /&gt;
&lt;br /&gt;
Enter the KallistiOS directory:&lt;br /&gt;
 cd /opt/toolchains/dc/rust/kos&lt;br /&gt;
&lt;br /&gt;
Build KallistiOS:&lt;br /&gt;
 make&lt;br /&gt;
&lt;br /&gt;
==Building rustc_codegen_gcc and Rust sysroot==&lt;br /&gt;
Now that we have a working KallistiOS environment suitable for Rust development set up, we can build the Rust compiler compiler and sysroot components. A script is provided which will download the necessary components from the respective repositories and compile them for you. Run the installer script:&lt;br /&gt;
&lt;br /&gt;
 /opt/toolchains/dc/rust/misc/install-rust.sh&lt;br /&gt;
&lt;br /&gt;
If all goes well, you&#039;ll see &#039;&#039;&#039;Rust for KallistiOS/Dreamcast installed!&#039;&#039;&#039; message!&lt;br /&gt;
&lt;br /&gt;
==Building KOS ports libraries==&lt;br /&gt;
If desired, you may also built the KOS ports collection of libraries for use with your Rust environment. It is recommended to maintain a separate KOS ports installation for linking with Rust projects in &amp;lt;code&amp;gt;/opt/toolchains/dc/rust/kos-ports&amp;lt;/code&amp;gt;. This is because the Rust environment is using the &amp;lt;code&amp;gt;-m4-single&amp;lt;/code&amp;gt; ABI, but the KallistiOS default installation will be using the &amp;lt;code&amp;gt;-m4-single-only&amp;lt;/code&amp;gt;, which cannot be mixed and matched.&lt;br /&gt;
&lt;br /&gt;
Clone the kos-ports repository to your system:&lt;br /&gt;
 git clone https://github.com/KallistiOS/kos-ports /opt/toolchains/dc/rust/kos-ports&lt;br /&gt;
An individual port can be built and installed by entering its directory and running the proper command. For example, installing GLdc:&lt;br /&gt;
 cd /opt/toolchains/dc/rust/kos-ports/libGL&lt;br /&gt;
 make install&lt;br /&gt;
Or, instead, you may run the script to build all of the included ports:&lt;br /&gt;
 /opt/toolchains/dc/rust/kos-ports/utils/build-all.sh&lt;br /&gt;
&lt;br /&gt;
=Using Rust for Dreamcast=&lt;br /&gt;
If all went well, you will now have a working Rust for Dreamcast environment!&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;cargo&#039;&#039;&#039; can be invoked to target Dreamcast using the &amp;lt;code&amp;gt;kos-cargo&amp;lt;/code&amp;gt; command.&lt;br /&gt;
** &amp;lt;code&amp;gt;kos-cargo run&amp;lt;/code&amp;gt; can be used to build and run code on a Dreamcast console using [[dcload-ip]] or [[dcload-serial]]. You will need to set up the &amp;lt;code&amp;gt;KOS_LOADER&amp;lt;/code&amp;gt; variable with the necessary command in the &amp;lt;code&amp;gt;/opt/toolchains/dc/rust/misc/environ.sh&amp;lt;/code&amp;gt; file (and then re-source the file for the change to take effect).&lt;br /&gt;
* &#039;&#039;&#039;rustc&#039;&#039;&#039; can be invoked to target Dreamcast using the &amp;lt;code&amp;gt;kos-rustc&amp;lt;/code&amp;gt; command.&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
Examples are included with the Rust for Dreamcast repo to help you get started.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/hello-cargo &amp;lt;code&amp;gt;hello-cargo&amp;lt;/code&amp;gt;] demonstrates how to create a simple &amp;quot;Hello, world!&amp;quot; application using cargo.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/hello-make &amp;lt;code&amp;gt;hello-make&amp;lt;/code&amp;gt;] demonstrates how to create a simple &amp;quot;Hello, world!&amp;quot; application using standard Makefiles.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/gldc-cube &amp;lt;code&amp;gt;gldc-cube&amp;lt;/code&amp;gt;] demonstrates a Rust project using the GLdc KOS port.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/addlib &amp;lt;code&amp;gt;addlib&amp;lt;/code&amp;gt;] demonstrates how to create a Rust library that can be included with a KallistiOS project.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/atomics &amp;lt;code&amp;gt;atomics&amp;lt;/code&amp;gt;] demonstrates the use of atomic functions.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/filesystem-io &amp;lt;code&amp;gt;filesystem-io&amp;lt;/code&amp;gt;] demonstrates listing directory contents and opening files.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/network-time &amp;lt;code&amp;gt;network-time&amp;lt;/code&amp;gt;] demonstrates displaying the system time and retrieving the current UTC time from an NTP server using the [[Broadband adapter]] or [[LAN adapter]].&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/pvr_strips_direct &amp;lt;code&amp;gt;pvr_strips_direct&amp;lt;/code&amp;gt;] demonstrates the use of PVR direct rendering&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/threads &amp;lt;code&amp;gt;threads&amp;lt;/code&amp;gt;] demonstrates the use of threads.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/tokio-async &amp;lt;code&amp;gt;tokio-async&amp;lt;/code&amp;gt;] demonstrates the use of the &#039;&#039;&#039;tokio&#039;&#039;&#039; async runtime.&lt;br /&gt;
&lt;br /&gt;
==Creating a new Rust project with Cargo==&lt;br /&gt;
First, we&#039;ll demonstrate creating a new &amp;quot;Hello, world!&amp;quot; project with &amp;lt;code&amp;gt;kos-cargo&amp;lt;/code&amp;gt;. This will follow the [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/hello-cargo &amp;lt;code&amp;gt;cargo-hello&amp;lt;/code&amp;gt;] example included in the Rust-for-Dreamcast repo. &lt;br /&gt;
&lt;br /&gt;
In a directory of your choosing, let&#039;s invoke &amp;lt;code&amp;gt;kos-cargo&amp;lt;/code&amp;gt; to create a new project and then enter the directory:&lt;br /&gt;
 kos-cargo new hello&lt;br /&gt;
 cd hello&lt;br /&gt;
&lt;br /&gt;
Let&#039;s add our kos-rs crate to gain access to current KallistiOS bindings. Open &amp;lt;code&amp;gt;Cargo.toml&amp;lt;/code&amp;gt; in your text editor and add:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;toml&amp;quot;&amp;gt;&lt;br /&gt;
[dependencies]&lt;br /&gt;
kos = { package = &amp;quot;kos-rs&amp;quot;, git = &amp;quot;https://github.com/dreamcast-rs/kos-rs&amp;quot; }&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we can open up &amp;lt;code&amp;gt;src/main.rs&amp;lt;/code&amp;gt; and write our &amp;quot;Hello, world!&amp;quot; example code:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; line&amp;gt;&lt;br /&gt;
fn main() {&lt;br /&gt;
    println!(&amp;quot;Hello, world from Rust!&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we can use &amp;lt;code&amp;gt;kos-cargo build&amp;lt;/code&amp;gt; to build our project. If all goes well, there will be a &amp;lt;code&amp;gt;target/sh-elf/debug/hello.elf&amp;lt;/code&amp;gt; file that can be sent to the Dreamcast with &amp;lt;code&amp;gt;dc-tool&amp;lt;/code&amp;gt;.&lt;br /&gt;
If you have &amp;lt;code&amp;gt;KOS_LOADER&amp;lt;/code&amp;gt; set in the &amp;lt;code&amp;gt;environ.sh&amp;lt;/code&amp;gt; file, you can invoke it directly with &amp;lt;code&amp;gt;kos-cargo run&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Creating a Rust project using kos-ports libraries==&lt;br /&gt;
[[File:Rust-Cube rustc codegen-gcc demo.gif|thumb|cube example in action]]&lt;br /&gt;
The [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/gldc-cube &amp;lt;code&amp;gt;gldc-cube&amp;lt;/code&amp;gt;] example demonstrates creating a rotating 3D cube using Rust as the primary language, while calling C functions provided by the &#039;&#039;&#039;GLdc&#039;&#039;&#039; library available in kos-ports. This project&#039;s initial setup is done the same as the above &amp;lt;code&amp;gt;hello&amp;lt;/code&amp;gt; example.&lt;br /&gt;
&lt;br /&gt;
For this example, we&#039;ll need to convert JPEG textures to VQ-encoded textures at compile time using the &amp;lt;code&amp;gt;vqenc&amp;lt;/code&amp;gt; utility includes with KallistiOS. To do this, we&#039;ll add a &amp;lt;code&amp;gt;build.rs&amp;lt;/code&amp;gt; file to the root of the crate with the following code (abridged to show the conversion of only one texture):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot;&amp;gt;&lt;br /&gt;
fn main() {&lt;br /&gt;
    let kos_base = std::env::var(&amp;quot;KOS_BASE&amp;quot;).expect(&amp;quot;Missing $KOS_BASE -- KallistiOS environment not sourced!&amp;quot;);&lt;br /&gt;
    let vqenc_cmd = kos_base + &amp;quot;/utils/vqenc/vqenc&amp;quot;;&lt;br /&gt;
    Command::new(&amp;amp;vqenc_cmd)&lt;br /&gt;
        .arg(&amp;quot;-t&amp;quot;)&lt;br /&gt;
        .arg(&amp;quot;-v&amp;quot;)&lt;br /&gt;
        .arg(&amp;quot;rsrc/tex_claw.jpg&amp;quot;)&lt;br /&gt;
        .output()&lt;br /&gt;
        .expect(&amp;quot;vqenc on tex_claw.jpg failed!&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
The texture files are then included in our project using the &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;include_bytes!&amp;lt;/syntaxhighlight&amp;gt; macro.&lt;br /&gt;
&lt;br /&gt;
We will need to link the GLdc library in this example, but we don&#039;t need to do anything special to add the paths to this library, because adding the common paths to KallistiOS libraries is already done for us in the kos-rs crate. However, if in your project you need to include a separate unique library path, you can add it in your &amp;lt;code&amp;gt;build.rs&amp;lt;/code&amp;gt; like so:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot;&amp;gt;&lt;br /&gt;
println!(&amp;quot;cargo:rustc-link-search=native={}&amp;quot;, lib_path);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The workings of this example&#039;s source code are too great to detail here line-by-line, but the example demonstrates how to use GLdc via the [https://github.com/dreamcast-rs/gldc-sys gldc-sys] crate, which contains unsafe/raw bindings to GLdc. Check out this crate&#039;s source code for an example of declaring and binding external C functions, constants, and structures so they can be used in Rust code. Since the entirety of the gldc-cube example uses C FFI functions without safe wrappers, the &amp;lt;code&amp;gt;main()&amp;lt;/code&amp;gt; source is wrapped in &amp;lt;code&amp;gt;unsafe {}&amp;lt;/code&amp;gt;. In the future, this would be much less necessary as higher level safe Rust bindings to KallistiOS and other libraries become mature.&lt;br /&gt;
&lt;br /&gt;
==Creating a Rust library==&lt;br /&gt;
Next, we&#039;ll demonstrate creating a Rust library with &amp;lt;code&amp;gt;kos-cargo&amp;lt;/code&amp;gt; that can be included in other Dreamcast code. This will follow the [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/addlib &amp;lt;code&amp;gt;addlib&amp;lt;/code&amp;gt;] example. Once again, this project&#039;s initial setup is done the same as the above &amp;lt;code&amp;gt;hello&amp;lt;/code&amp;gt; example, but you&#039;ll create the new project using &amp;lt;code&amp;gt;kos-cargo new --lib addlib&amp;lt;/code&amp;gt; to specify that we&#039;re creating a library named &amp;lt;code&amp;gt;addlib&amp;lt;/code&amp;gt;. You&#039;ll also need to add the following text to this project&#039;s &amp;lt;code&amp;gt;Cargo.toml&amp;lt;/code&amp;gt; file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;toml&amp;quot;&amp;gt;&lt;br /&gt;
[lib]&lt;br /&gt;
crate-type = [&amp;quot;staticlib&amp;quot;]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This tells Rust to build a static &amp;lt;code&amp;gt;.a&amp;lt;/code&amp;gt; library archive file from our code, which is located in &amp;lt;code&amp;gt;src/lib.rs&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; line&amp;gt;&lt;br /&gt;
#[no_mangle]&lt;br /&gt;
pub extern &amp;quot;C&amp;quot; fn print_added(a: isize, b: isize) {&lt;br /&gt;
    print!(&amp;quot;{}&amp;quot;, a + b);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
#[no_mangle]&lt;br /&gt;
pub extern &amp;quot;C&amp;quot; fn add_integers(a: isize, b: isize) -&amp;gt; isize {&lt;br /&gt;
    a + b&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The source code here starts similarly to the &amp;quot;Hello, world!&amp;quot; example, except we don&#039;t need to specify &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;#![no_main]&amp;lt;/syntaxhighlight&amp;gt; as this is a library which wouldn&#039;t have a &amp;lt;code&amp;gt;main()&amp;lt;/code&amp;gt; function anyway.&lt;br /&gt;
&lt;br /&gt;
Two simple functions are provided: one for adding two integers and returning the result, and another for adding two integers and printing the result as text. Because these functions use &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;#[no_mangle]&amp;lt;/syntaxhighlight&amp;gt; and are declared &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;extern &amp;quot;C&amp;quot;&amp;lt;/syntaxhighlight&amp;gt;, they can be called by name in C code that links this library.&lt;br /&gt;
&lt;br /&gt;
When built using &amp;lt;code&amp;gt;kos-cargo build&amp;lt;/code&amp;gt;, a &amp;lt;code&amp;gt;target/sh-elf/debug/libaddlib.a&amp;lt;/code&amp;gt; file will be generated. This can be linked into other projects to gain the use of these functions.&lt;br /&gt;
&lt;br /&gt;
For example, this can be added to a standard &amp;lt;code&amp;gt;Makefile&amp;lt;/code&amp;gt;-based KallistiOS project by editing the &amp;lt;code&amp;gt;Makefile&amp;lt;/code&amp;gt;:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;make&amp;quot;&amp;gt;&lt;br /&gt;
$(TARGET): $(OBJS)&lt;br /&gt;
	kos-cc -o $(TARGET) $(OBJS) -L/opt/toolchains/dc/rust/examples/cargo-addlib/target/sh-elf/debug -laddlib&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then, we can use the code in our C source:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/* Declare the external function from the Rust library */&lt;br /&gt;
int add_integers(int a, int b);&lt;br /&gt;
&lt;br /&gt;
/* Use the function */&lt;br /&gt;
printf(&amp;quot;Five plus six is %d\n&amp;quot;, add_integers(5, 6));&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Compiling individual modules into object files with rustc==&lt;br /&gt;
If we&#039;d like to mix C and Rust code in the same &amp;lt;code&amp;gt;Makefile&amp;lt;/code&amp;gt;-based KallistiOS project without building an entirely separate library, we can do that as well. This is demonstrated in the [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/no_std/hello-make &amp;lt;code&amp;gt;hello-make&amp;lt;/code&amp;gt;] example.&lt;br /&gt;
&lt;br /&gt;
Instead of using &amp;lt;code&amp;gt;kos-cargo&amp;lt;/code&amp;gt;, we can invoke the &amp;lt;code&amp;gt;kos-rustc&amp;lt;/code&amp;gt; via a &amp;lt;code&amp;gt;Makefile&amp;lt;/code&amp;gt; to build Rust modules. If we assume the Rust file is named &amp;lt;code&amp;gt;example.rs&amp;lt;/code&amp;gt;, you&#039;ll need to add &amp;lt;code&amp;gt;example.o&amp;lt;/code&amp;gt; as an object file in your &amp;lt;code&amp;gt;Makefile&amp;lt;/code&amp;gt;&#039;s &amp;lt;code&amp;gt;OBJS =&amp;lt;/code&amp;gt; declaration. For example, if the project has two source files &amp;lt;code&amp;gt;hello_c.c&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;hello_rust.rs&amp;lt;/code&amp;gt;, our &amp;lt;code&amp;gt;Makefile&amp;lt;/code&amp;gt; would have a line like this:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;make&amp;quot;&amp;gt;&lt;br /&gt;
OBJS = hello_c.o hello_rust.o&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The example code demonstrates starting a C &amp;lt;code&amp;gt;main()&amp;lt;/code&amp;gt; function to call a Rust function which builds a &amp;lt;code&amp;gt;String&amp;lt;/code&amp;gt; containing the &amp;quot;Hello, world!&amp;quot; text which is passed back to a C function which prints &amp;lt;code&amp;gt;String&amp;lt;/code&amp;gt;s.&lt;br /&gt;
&lt;br /&gt;
==Using the libc crate with KallistiOS==&lt;br /&gt;
If any of the crates in your project&#039;s dependency tree requires the &#039;&#039;&#039;libc&#039;&#039;&#039; crate, you will need to override fetching the default crate with our custom libc crate with KallistiOS/Dreamcast definitions. This custom version was copied to &amp;lt;code&amp;gt;/opt/toolchains/dc/rust/libc&amp;lt;/code&amp;gt; when building the Rust sysroot earlier. Add the following text to your &amp;lt;code&amp;gt;Cargo.toml&amp;lt;/code&amp;gt;:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;toml&amp;quot;&amp;gt;&lt;br /&gt;
[patch.crates-io]&lt;br /&gt;
libc = { path = &amp;quot;/opt/toolchains/dc/rust/libc&amp;quot; }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Adjusting build flags==&lt;br /&gt;
Build settings can be adjusted through the &amp;lt;code&amp;gt;KOS_RCG_RUSTFLAGS&amp;lt;/code&amp;gt; variable in the &amp;lt;code&amp;gt;environ.sh&amp;lt;/code&amp;gt; file under the &amp;lt;code&amp;gt;### Rust Flags&amp;lt;/code&amp;gt; section. &lt;br /&gt;
* To pass arguments to the &#039;&#039;&#039;rustc&#039;&#039;&#039; frontend, e.g. to disable debug assertions:&lt;br /&gt;
** &amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot; inline&amp;gt;export KOS_RCG_RUSTFLAGS=&amp;quot;${KOS_RCG_RUSTFLAGS} -Cdebug-assertions=no&amp;quot;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* To pass arguments to the GCC backend, e.g. to enable the &amp;lt;code&amp;gt;-freorder-blocks-algorithm=simple&amp;lt;/code&amp;gt; optimization for Rust code:&lt;br /&gt;
** &amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot; inline&amp;gt;export KOS_RCG_RUSTFLAGS=&amp;quot;${KOS_RCG_RUSTFLAGS} -Cllvm-args=-freorder-blocks-algorithm=simple&amp;quot;&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Darc</name></author>
	</entry>
	<entry>
		<id>https://dreamcast.wiki/wiki/index.php?title=Rust_on_Dreamcast&amp;diff=3643</id>
		<title>Rust on Dreamcast</title>
		<link rel="alternate" type="text/html" href="https://dreamcast.wiki/wiki/index.php?title=Rust_on_Dreamcast&amp;diff=3643"/>
		<updated>2024-11-22T00:28:19Z</updated>

		<summary type="html">&lt;p&gt;Darc: /* Creating a new Rust project with Cargo */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[File:Rust-dc-logo.png|thumb|Ferris holding his Dreamcast controller]]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Rust&#039;&#039;&#039; is a popular modern programming language focusing on performance, memory safety, and program correctness. As a &amp;quot;blazingly fast&amp;quot; low-level language, it is viable for running on a Dreamcast console. Doing so presents a bit of a challenge, however, as the official Rust compiler is based on the [https://llvm.org/ LLVM] toolchain infrastructure, which does not support the Dreamcast CPU&#039;s SuperH architecture. Dreamcast programming is instead typically done with [https://gcc.gnu.org/ GCC], the GNU Compiler Collection. There are currently two viable solutions to this challenge:&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;rustc_codegen_gcc&#039;&#039;&#039;, the current preferred method: An experimental codegen backend for the official Rust &#039;&#039;&#039;rustc&#039;&#039;&#039; compiler which interfaces with GCC&#039;s libgccjit API&lt;br /&gt;
* &#039;&#039;&#039;gccrs&#039;&#039;&#039;: an experimental Rust frontend for GCC&lt;br /&gt;
&lt;br /&gt;
Neither solution is complete at this time, but &#039;&#039;&#039;rustc_codegen_gcc&#039;&#039;&#039; is much further along and is quite usable with some patience with its current limitations. On the other hand, &#039;&#039;&#039;gccrs&#039;&#039;&#039; can compile for Dreamcast, but is in an earlier state. Below we will focus on using &#039;&#039;&#039;rustc_codegen_gcc&#039;&#039;&#039;. For more information on using gccrs, see the [[gccrs]] page.&lt;br /&gt;
&lt;br /&gt;
=Current Status=&lt;br /&gt;
* The full &#039;&#039;&#039;std&#039;&#039;&#039; library is supported.&lt;br /&gt;
* A [https://github.com/dreamcast-rs/KallistiOS custom version of KallistiOS] v2.1.0 (latest stable release) is used.&lt;br /&gt;
** This version includes minor changes for Rust and a beta libpthread addon layer to interface with the Rust standard library.&lt;br /&gt;
** Wrappers are provided to invoke &#039;&#039;&#039;cargo&#039;&#039;&#039; and &#039;&#039;&#039;rustc&#039;&#039;&#039; in a familiar manner to compile projects.&lt;br /&gt;
* A [https://github.com/dreamcast-rs/kos-sys &#039;&#039;&#039;kos-sys&#039;&#039;&#039;] raw/unsafe bindings crate is provided for easily linking KallistiOS and its unsafe C API into Rust crates. This crate is in active development, but with a large portion of KallistiOS functionality available now via C FFI.&lt;br /&gt;
* A [https://github.com/dreamcast-rs/kos-rs &#039;&#039;&#039;kos-rs&#039;&#039;&#039;] Rust crate with safe idiomatic interfaces is available but is in very early stages.&lt;br /&gt;
** Documentation for the bindings is available at [https://kos-rs.dreamcast.wiki/ kos-rs.dreamcast.wiki].&lt;br /&gt;
** If bindings do not yet exist for desired functionality, wrapping of KallistiOS C functions is required to get proper use of KallistiOS at this time. See the [https://doc.rust-lang.org/nomicon/ffi.html Foreign Function Interface page] in the Rustonomicon for further understanding. &lt;br /&gt;
** A custom [https://github.com/dreamcast-rs/libc/tree/libc-0.2-kos &#039;&#039;&#039;libc&#039;&#039;&#039; crate with KallistiOS support] is provided.&lt;br /&gt;
* Code generation uses &#039;&#039;&#039;rustc_codegen_gcc&#039;&#039;&#039;, so its limitations apply here. See the [https://blog.antoyo.xyz/ development blog] for more information on its progress.&lt;br /&gt;
** rustc_codegen_gcc is pinned to a particular Rust nightly version, but is synced regularly. The current version in use is the &#039;&#039;&#039;2024-08-10&#039;&#039;&#039; nightly.&lt;br /&gt;
** A [https://github.com/dreamcast-rs/rust/tree/kos-2024-08-10 KallistiOS-patched version of the latest nightly] is maintained and updated regularly. &lt;br /&gt;
** Panic unwinding, debug info, LTO, etc. are not currently available.&lt;br /&gt;
** Architectures dependent on rustc_codegen_gcc are not yet able to be added to the rustc frontend, so a workaround is necessary: MIPS is falsely selected for KallistiOS, causing rustc to emit objects with a MIPS header and a provided wrapper is used when linking to rewrite these MIPS headers to SuperH before passing the compiled objects to the linker.&lt;br /&gt;
* A separate [https://github.com/rust-lang/gcc development version of the GCC toolchain] must be used.&lt;br /&gt;
** This version of GCC contains the latest libgccjit patches necessary to interface with rustc_codegen_gcc, and will be built to work with the custom KallistiOS&#039;s libpthread layer.&lt;br /&gt;
** New updates to rustc_codegen_gcc often require new updates to GCC, so recompiling GCC regularly will be necessary to keep up with the latest updates.&lt;br /&gt;
** The development version of GCC does not always compile well on every platform. Using a modern Linux platform is recommended for maximum compatibility.&lt;br /&gt;
** The custom KallistiOS and GCC environment can still be used to compile code or projects written in C, C++, etc.&lt;br /&gt;
* KallistiOS and all related libraries and tools must be compiled using the &amp;lt;code&amp;gt;-m4-single&amp;lt;/code&amp;gt; ABI. This is already set up for you in the guide below, but if external libraries are being linked to Rust projects, make sure that code is compiled using the proper ABI.&lt;br /&gt;
&lt;br /&gt;
=Setting up a Rust environment for Dreamcast development=&lt;br /&gt;
The following guide will provide instructions for setting up a Rust development environment for Dreamcast. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;If you run into any errors or other challenges while following this tutorial, or simply need clarification on any of the steps, feel free to ask for assistance on the [https://dcemulation.org/phpBB/viewforum.php?f=29 message board] or [https://discord.gg/wDEfkUh63h our Discord server] and we would be happy to aid you and update the guide for the benefit of future readers and others in the community.&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
==Prerequisites==&lt;br /&gt;
First, we will need to set up the prerequisites.&lt;br /&gt;
&lt;br /&gt;
* You must have the [https://rustup.rs/ rustup] tool installed for your operating system to manage Rust toolchain installations. It will automatically manage the latest Rust nightly installations for you. You cannot use a Rust toolchain that may be provided by your operating system vendor.&lt;br /&gt;
* Install the dependency packages for your operating system. These are listed in the &#039;&#039;&#039;Dependencies&#039;&#039;&#039; section on the [[Getting Started with Dreamcast development#Dependencies]] page, with lists provided for most common operating systems.&lt;br /&gt;
* We will install our custom Rust toolchain environment within &amp;lt;code&amp;gt;/opt/toolchains/dc/rust&amp;lt;/code&amp;gt;. If it does not already exist, create the &amp;lt;code&amp;gt;/opt/toolchains/dc&amp;lt;/code&amp;gt; directory and grant it proper permissions using the following commands. &lt;br /&gt;
 sudo mkdir -p /opt/toolchains/dc&lt;br /&gt;
 sudo chmod -R 755 /opt/toolchains/dc&lt;br /&gt;
 sudo chown -R $(id -u):$(id -g) /opt/toolchains/dc&lt;br /&gt;
* Clone the [https://github.com/dreamcast-rs/rust-for-dreamcast/ Rust for Dreamcast] repo containing necessary support files to &amp;lt;code&amp;gt;/opt/toolchains/dc/rust&amp;lt;/code&amp;gt;:&lt;br /&gt;
 git clone https://github.com/dreamcast-rs/rust-for-dreamcast.git /opt/toolchains/dc/rust&lt;br /&gt;
&lt;br /&gt;
==Installing our custom KallistiOS and GCC toolchain==&lt;br /&gt;
Clone the git repository for our custom Rust-patched version of KallistiOS v2.1.0 stable:&lt;br /&gt;
 git clone https://github.com/dreamcast-rs/KallistiOS /opt/toolchains/dc/rust/kos&lt;br /&gt;
&lt;br /&gt;
Enter the dc-chain directory:&lt;br /&gt;
 cd /opt/toolchains/dc/rust/kos/utils/dc-chain&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;Makefile.default.cfg&amp;lt;/code&amp;gt; file in this directory has been customized for you, selecting the proper toolchain profile, paths, and build options necessary for building a Rust toolchain using this guide. It is not recommended to adjust the defaults.&lt;br /&gt;
&lt;br /&gt;
To build the GCC toolchain using 2 CPU cores, simply type:&lt;br /&gt;
 make makejobs=2&lt;br /&gt;
Adjust the &amp;lt;code&amp;gt;makejobs=...&amp;lt;/code&amp;gt; value to use the number of CPU cores available to you on your system in order to speed up compilation. This may take a while to build, so be patient.&lt;br /&gt;
&lt;br /&gt;
Once the GCC toolchain is built, source the &amp;lt;code&amp;gt;environ.sh&amp;lt;/code&amp;gt; KallistiOS environment settings. A custom &amp;lt;code&amp;gt;environ.sh&amp;lt;/code&amp;gt; file has been provided for you, containing settings pre-adjusted for Rust development using this guide.&lt;br /&gt;
&lt;br /&gt;
You will need to run the source command to apply the KallistiOS environment settings to your currently running shell. Run the following now, &#039;&#039;&#039;and&#039;&#039;&#039; &#039;&#039;whenever&#039;&#039; you open a new shell to work on Dreamcast projects:&lt;br /&gt;
 source /opt/toolchains/dc/rust/misc/environ.sh&lt;br /&gt;
&lt;br /&gt;
Enter the KallistiOS directory:&lt;br /&gt;
 cd /opt/toolchains/dc/rust/kos&lt;br /&gt;
&lt;br /&gt;
Build KallistiOS:&lt;br /&gt;
 make&lt;br /&gt;
&lt;br /&gt;
==Building rustc_codegen_gcc and Rust sysroot==&lt;br /&gt;
Now that we have a working KallistiOS environment suitable for Rust development set up, we can build the Rust compiler compiler and sysroot components. A script is provided which will download the necessary components from the respective repositories and compile them for you. Run the installer script:&lt;br /&gt;
&lt;br /&gt;
 /opt/toolchains/dc/rust/misc/install-rust.sh&lt;br /&gt;
&lt;br /&gt;
If all goes well, you&#039;ll see &#039;&#039;&#039;Rust for KallistiOS/Dreamcast installed!&#039;&#039;&#039; message!&lt;br /&gt;
&lt;br /&gt;
==Building KOS ports libraries==&lt;br /&gt;
If desired, you may also built the KOS ports collection of libraries for use with your Rust environment. It is recommended to maintain a separate KOS ports installation for linking with Rust projects in &amp;lt;code&amp;gt;/opt/toolchains/dc/rust/kos-ports&amp;lt;/code&amp;gt;. This is because the Rust environment is using the &amp;lt;code&amp;gt;-m4-single&amp;lt;/code&amp;gt; ABI, but the KallistiOS default installation will be using the &amp;lt;code&amp;gt;-m4-single-only&amp;lt;/code&amp;gt;, which cannot be mixed and matched.&lt;br /&gt;
&lt;br /&gt;
Clone the kos-ports repository to your system:&lt;br /&gt;
 git clone https://github.com/KallistiOS/kos-ports /opt/toolchains/dc/rust/kos-ports&lt;br /&gt;
An individual port can be built and installed by entering its directory and running the proper command. For example, installing GLdc:&lt;br /&gt;
 cd /opt/toolchains/dc/rust/kos-ports/libGL&lt;br /&gt;
 make install&lt;br /&gt;
Or, instead, you may run the script to build all of the included ports:&lt;br /&gt;
 /opt/toolchains/dc/rust/kos-ports/utils/build-all.sh&lt;br /&gt;
&lt;br /&gt;
=Using Rust for Dreamcast=&lt;br /&gt;
If all went well, you will now have a working Rust for Dreamcast environment!&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;cargo&#039;&#039;&#039; can be invoked to target Dreamcast using the &amp;lt;code&amp;gt;kos-cargo&amp;lt;/code&amp;gt; command.&lt;br /&gt;
** &amp;lt;code&amp;gt;kos-cargo run&amp;lt;/code&amp;gt; can be used to build and run code on a Dreamcast console using [[dcload-ip]] or [[dcload-serial]]. You will need to set up the &amp;lt;code&amp;gt;KOS_LOADER&amp;lt;/code&amp;gt; variable with the necessary command in the &amp;lt;code&amp;gt;/opt/toolchains/dc/rust/misc/environ.sh&amp;lt;/code&amp;gt; file (and then re-source the file for the change to take effect).&lt;br /&gt;
* &#039;&#039;&#039;rustc&#039;&#039;&#039; can be invoked to target Dreamcast using the &amp;lt;code&amp;gt;kos-rustc&amp;lt;/code&amp;gt; command.&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
Examples are included with the Rust for Dreamcast repo to help you get started.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/hello-cargo &amp;lt;code&amp;gt;hello-cargo&amp;lt;/code&amp;gt;] demonstrates how to create a simple &amp;quot;Hello, world!&amp;quot; application using cargo.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/hello-make &amp;lt;code&amp;gt;hello-make&amp;lt;/code&amp;gt;] demonstrates how to create a simple &amp;quot;Hello, world!&amp;quot; application using standard Makefiles.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/gldc-cube &amp;lt;code&amp;gt;gldc-cube&amp;lt;/code&amp;gt;] demonstrates a Rust project using the GLdc KOS port.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/addlib &amp;lt;code&amp;gt;addlib&amp;lt;/code&amp;gt;] demonstrates how to create a Rust library that can be included with a KallistiOS project.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/atomics &amp;lt;code&amp;gt;atomics&amp;lt;/code&amp;gt;] demonstrates the use of atomic functions.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/filesystem-io &amp;lt;code&amp;gt;filesystem-io&amp;lt;/code&amp;gt;] demonstrates listing directory contents and opening files.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/network-time &amp;lt;code&amp;gt;network-time&amp;lt;/code&amp;gt;] demonstrates displaying the system time and retrieving the current UTC time from an NTP server using the [[Broadband adapter]] or [[LAN adapter]].&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/pvr_strips_direct &amp;lt;code&amp;gt;pvr_strips_direct&amp;lt;/code&amp;gt;] demonstrates the use of PVR direct rendering&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/threads &amp;lt;code&amp;gt;threads&amp;lt;/code&amp;gt;] demonstrates the use of threads.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/tokio-async &amp;lt;code&amp;gt;tokio-async&amp;lt;/code&amp;gt;] demonstrates the use of the &#039;&#039;&#039;tokio&#039;&#039;&#039; async runtime.&lt;br /&gt;
&lt;br /&gt;
==Creating a new Rust project with Cargo==&lt;br /&gt;
First, we&#039;ll demonstrate creating a new &amp;quot;Hello, world!&amp;quot; project with &amp;lt;code&amp;gt;kos-cargo&amp;lt;/code&amp;gt;. This will follow the [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/hello-cargo &amp;lt;code&amp;gt;cargo-hello&amp;lt;/code&amp;gt;] example included in the Rust-for-Dreamcast repo. &lt;br /&gt;
&lt;br /&gt;
In a directory of your choosing, let&#039;s invoke &amp;lt;code&amp;gt;kos-cargo&amp;lt;/code&amp;gt; to create a new project and then enter the directory:&lt;br /&gt;
 kos-cargo new hello&lt;br /&gt;
 cd hello&lt;br /&gt;
&lt;br /&gt;
Let&#039;s add our kos-rs crate to gain access to current KallistiOS bindings. Open &amp;lt;code&amp;gt;Cargo.toml&amp;lt;/code&amp;gt; in your text editor and add:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;toml&amp;quot;&amp;gt;&lt;br /&gt;
[dependencies]&lt;br /&gt;
kos = { package = &amp;quot;kos-rs&amp;quot;, git = &amp;quot;https://github.com/dreamcast-rs/kos-rs&amp;quot; }&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we can open up &amp;lt;code&amp;gt;src/main.rs&amp;lt;/code&amp;gt; and write our &amp;quot;Hello, world!&amp;quot; example code:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; line&amp;gt;&lt;br /&gt;
fn main() {&lt;br /&gt;
    println!(&amp;quot;Hello, world from Rust!&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we can use &amp;lt;code&amp;gt;kos-cargo build&amp;lt;/code&amp;gt; to build our project. If all goes well, there will be a &amp;lt;code&amp;gt;target/sh-elf/debug/hello.elf&amp;lt;/code&amp;gt; file that can be sent to the Dreamcast with &amp;lt;code&amp;gt;dc-tool&amp;lt;/code&amp;gt;.&lt;br /&gt;
If you have &amp;lt;code&amp;gt;KOS_LOADER&amp;lt;/code&amp;gt; set in the &amp;lt;code&amp;gt;environ.sh&amp;lt;/code&amp;gt; file, you can invoke it directly with &amp;lt;code&amp;gt;kos-cargo run&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Creating a Rust project using kos-ports libraries==&lt;br /&gt;
[[File:Rust-Cube rustc codegen-gcc demo.gif|thumb|cube example in action]]&lt;br /&gt;
The [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/gldc-cube &amp;lt;code&amp;gt;gldc-cube&amp;lt;/code&amp;gt;] example demonstrates creating a rotating 3D cube using Rust as the primary language, while calling C functions provided by the &#039;&#039;&#039;GLdc&#039;&#039;&#039; library available in kos-ports. This project&#039;s initial setup is done the same as the above &amp;lt;code&amp;gt;hello&amp;lt;/code&amp;gt; example.&lt;br /&gt;
&lt;br /&gt;
For this example, we&#039;ll need to convert JPEG textures to VQ-encoded textures at compile time using the &amp;lt;code&amp;gt;vqenc&amp;lt;/code&amp;gt; utility includes with KallistiOS. To do this, we&#039;ll add a &amp;lt;code&amp;gt;build.rs&amp;lt;/code&amp;gt; file to the root of the crate with the following code (abridged to show the conversion of only one texture):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot;&amp;gt;&lt;br /&gt;
fn main() {&lt;br /&gt;
    let kos_base = std::env::var(&amp;quot;KOS_BASE&amp;quot;).expect(&amp;quot;Missing $KOS_BASE -- KallistiOS environment not sourced!&amp;quot;);&lt;br /&gt;
    let vqenc_cmd = kos_base + &amp;quot;/utils/vqenc/vqenc&amp;quot;;&lt;br /&gt;
    Command::new(&amp;amp;vqenc_cmd)&lt;br /&gt;
        .arg(&amp;quot;-t&amp;quot;)&lt;br /&gt;
        .arg(&amp;quot;-v&amp;quot;)&lt;br /&gt;
        .arg(&amp;quot;rsrc/tex_claw.jpg&amp;quot;)&lt;br /&gt;
        .output()&lt;br /&gt;
        .expect(&amp;quot;vqenc on tex_claw.jpg failed!&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
The texture files are then included in our project using the &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;include_bytes!&amp;lt;/syntaxhighlight&amp;gt; macro.&lt;br /&gt;
&lt;br /&gt;
We will need to link the GLdc library in this example, but we don&#039;t need to do anything special to add the paths to this library, because adding the common paths to KallistiOS libraries is already done for us in the kos-rs crate. However, if in your project you need to include a separate unique library path, you can add it in your &amp;lt;code&amp;gt;build.rs&amp;lt;/code&amp;gt; like so:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot;&amp;gt;&lt;br /&gt;
println!(&amp;quot;cargo:rustc-link-search=native={}&amp;quot;, lib_path);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The workings of this example&#039;s source code are too great to detail here line-by-line, but the example demonstrates how to use GLdc via the [https://github.com/dreamcast-rs/gldc-sys gldc-sys] crate, which contains unsafe/raw bindings to GLdc. Check out this crate&#039;s source code for an example of declaring and binding external C functions, constants, and structures so they can be used in Rust code. Since the entirety of the gldc-cube example uses C FFI functions without safe wrappers, the &amp;lt;code&amp;gt;main()&amp;lt;/code&amp;gt; source is wrapped in &amp;lt;code&amp;gt;unsafe {}&amp;lt;/code&amp;gt;. In the future, this would be much less necessary as higher level safe Rust bindings to KallistiOS and other libraries become mature.&lt;br /&gt;
&lt;br /&gt;
==Creating a Rust library==&lt;br /&gt;
Next, we&#039;ll demonstrate creating a Rust library with &amp;lt;code&amp;gt;kos-cargo&amp;lt;/code&amp;gt; that can be included in other Dreamcast code. This will follow the [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/addlib &amp;lt;code&amp;gt;addlib&amp;lt;/code&amp;gt;] example. Once again, this project&#039;s initial setup is done the same as the above &amp;lt;code&amp;gt;hello&amp;lt;/code&amp;gt; example, but you&#039;ll create the new project using &amp;lt;code&amp;gt;kos-cargo new --lib addlib&amp;lt;/code&amp;gt; to specify that we&#039;re creating a library named &amp;lt;code&amp;gt;addlib&amp;lt;/code&amp;gt;. You&#039;ll also need to add the following text to this project&#039;s &amp;lt;code&amp;gt;Cargo.toml&amp;lt;/code&amp;gt; file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;toml&amp;quot;&amp;gt;&lt;br /&gt;
[lib]&lt;br /&gt;
crate-type = [&amp;quot;staticlib&amp;quot;]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This tells Rust to build a static &amp;lt;code&amp;gt;.a&amp;lt;/code&amp;gt; library archive file from our code, which is located in &amp;lt;code&amp;gt;src/lib.rs&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; line&amp;gt;&lt;br /&gt;
#[no_mangle]&lt;br /&gt;
pub extern &amp;quot;C&amp;quot; fn print_added(a: isize, b: isize) {&lt;br /&gt;
    print!(&amp;quot;{}&amp;quot;, a + b);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
#[no_mangle]&lt;br /&gt;
pub extern &amp;quot;C&amp;quot; fn add_integers(a: isize, b: isize) -&amp;gt; isize {&lt;br /&gt;
    a + b&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The source code here starts similarly to the &amp;quot;Hello, world!&amp;quot; example, except we don&#039;t need to specify &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;#![no_main]&amp;lt;/syntaxhighlight&amp;gt; as this is a library which wouldn&#039;t have a &amp;lt;code&amp;gt;main()&amp;lt;/code&amp;gt; function anyway.&lt;br /&gt;
&lt;br /&gt;
Two simple functions are provided: one for adding two integers and returning the result, and another for adding two integers and printing the result as text. Because these functions use &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;#[no_mangle]&amp;lt;/syntaxhighlight&amp;gt; and are declared &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;extern &amp;quot;C&amp;quot;&amp;lt;/syntaxhighlight&amp;gt;, they can be called by name in C code that links this library.&lt;br /&gt;
&lt;br /&gt;
When built using &amp;lt;code&amp;gt;kos-cargo build&amp;lt;/code&amp;gt;, a &amp;lt;code&amp;gt;target/sh-elf/debug/libaddlib.a&amp;lt;/code&amp;gt; file will be generated. This can be linked into other projects to gain the use of these functions.&lt;br /&gt;
&lt;br /&gt;
For example, this can be added to a standard &amp;lt;code&amp;gt;Makefile&amp;lt;/code&amp;gt;-based KallistiOS project by editing the &amp;lt;code&amp;gt;Makefile&amp;lt;/code&amp;gt;:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;make&amp;quot;&amp;gt;&lt;br /&gt;
$(TARGET): $(OBJS)&lt;br /&gt;
	kos-cc -o $(TARGET) $(OBJS) -L/opt/toolchains/dc/rust/examples/cargo-addlib/target/sh-elf/debug -laddlib&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then, we can use the code in our C source:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/* Declare the external function from the Rust library */&lt;br /&gt;
int add_integers(int a, int b);&lt;br /&gt;
&lt;br /&gt;
/* Use the function */&lt;br /&gt;
printf(&amp;quot;Five plus six is %d\n&amp;quot;, add_integers(5, 6));&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Compiling individual modules into object files with rustc==&lt;br /&gt;
If we&#039;d like to mix C and Rust code in the same &amp;lt;code&amp;gt;Makefile&amp;lt;/code&amp;gt;-based KallistiOS project without building an entirely separate library, we can do that as well. This is demonstrated in the [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/no_std/hello-make &amp;lt;code&amp;gt;hello-make&amp;lt;/code&amp;gt;] example.&lt;br /&gt;
&lt;br /&gt;
Instead of using &amp;lt;code&amp;gt;kos-cargo&amp;lt;/code&amp;gt;, we can invoke the &amp;lt;code&amp;gt;kos-rustc&amp;lt;/code&amp;gt; via a &amp;lt;code&amp;gt;Makefile&amp;lt;/code&amp;gt; to build Rust modules. If we assume the Rust file is named &amp;lt;code&amp;gt;example.rs&amp;lt;/code&amp;gt;, you&#039;ll need to add &amp;lt;code&amp;gt;example.o&amp;lt;/code&amp;gt; as an object file in your &amp;lt;code&amp;gt;Makefile&amp;lt;/code&amp;gt;&#039;s &amp;lt;code&amp;gt;OBJS =&amp;lt;/code&amp;gt; declaration. For example, if the project has two source files &amp;lt;code&amp;gt;hello_c.c&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;hello_rust.rs&amp;lt;/code&amp;gt;, our &amp;lt;code&amp;gt;Makefile&amp;lt;/code&amp;gt; would have a line like this:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;make&amp;quot;&amp;gt;&lt;br /&gt;
OBJS = hello_c.o hello_rust.o&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The example code demonstrates starting a C &amp;lt;code&amp;gt;main()&amp;lt;/code&amp;gt; function to call a Rust function which builds a &amp;lt;code&amp;gt;String&amp;lt;/code&amp;gt; containing the &amp;quot;Hello, world!&amp;quot; text which is passed back to a C function which prints &amp;lt;code&amp;gt;String&amp;lt;/code&amp;gt;s.&lt;br /&gt;
&lt;br /&gt;
==Using the libc crate with KallistiOS==&lt;br /&gt;
If any of the crates in your project&#039;s dependency tree requires the &#039;&#039;&#039;libc&#039;&#039;&#039; crate, you will need to override fetching the default crate with our custom libc crate with KallistiOS/Dreamcast definitions. This custom version was copied to &amp;lt;code&amp;gt;/opt/toolchains/dc/rust/libc&amp;lt;/code&amp;gt; when building the Rust sysroot earlier. Add the following text to your &amp;lt;code&amp;gt;Cargo.toml&amp;lt;/code&amp;gt;:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;toml&amp;quot;&amp;gt;&lt;br /&gt;
[patch.crates-io]&lt;br /&gt;
libc = { path = &amp;quot;/opt/toolchains/dc/rust/libc&amp;quot; }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Adjusting build flags==&lt;br /&gt;
Build settings can be adjusted through the &amp;lt;code&amp;gt;KOS_RCG_RUSTFLAGS&amp;lt;/code&amp;gt; variable in the &amp;lt;code&amp;gt;environ.sh&amp;lt;/code&amp;gt; file under the &amp;lt;code&amp;gt;### Rust Flags&amp;lt;/code&amp;gt; section. &lt;br /&gt;
* To pass arguments to the &#039;&#039;&#039;rustc&#039;&#039;&#039; frontend, e.g. to disable debug assertions:&lt;br /&gt;
** &amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot; inline&amp;gt;export KOS_RCG_RUSTFLAGS=&amp;quot;${KOS_RCG_RUSTFLAGS} -Cdebug-assertions=no&amp;quot;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* To pass arguments to the GCC backend, e.g. to enable the &amp;lt;code&amp;gt;-freorder-blocks-algorithm=simple&amp;lt;/code&amp;gt; optimization for Rust code:&lt;br /&gt;
** &amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot; inline&amp;gt;export KOS_RCG_RUSTFLAGS=&amp;quot;${KOS_RCG_RUSTFLAGS} -Cllvm-args=-freorder-blocks-algorithm=simple&amp;quot;&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Darc</name></author>
	</entry>
	<entry>
		<id>https://dreamcast.wiki/wiki/index.php?title=Rust_on_Dreamcast&amp;diff=3642</id>
		<title>Rust on Dreamcast</title>
		<link rel="alternate" type="text/html" href="https://dreamcast.wiki/wiki/index.php?title=Rust_on_Dreamcast&amp;diff=3642"/>
		<updated>2024-11-14T14:04:10Z</updated>

		<summary type="html">&lt;p&gt;Darc: /* Examples */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[File:Rust-dc-logo.png|thumb|Ferris holding his Dreamcast controller]]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Rust&#039;&#039;&#039; is a popular modern programming language focusing on performance, memory safety, and program correctness. As a &amp;quot;blazingly fast&amp;quot; low-level language, it is viable for running on a Dreamcast console. Doing so presents a bit of a challenge, however, as the official Rust compiler is based on the [https://llvm.org/ LLVM] toolchain infrastructure, which does not support the Dreamcast CPU&#039;s SuperH architecture. Dreamcast programming is instead typically done with [https://gcc.gnu.org/ GCC], the GNU Compiler Collection. There are currently two viable solutions to this challenge:&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;rustc_codegen_gcc&#039;&#039;&#039;, the current preferred method: An experimental codegen backend for the official Rust &#039;&#039;&#039;rustc&#039;&#039;&#039; compiler which interfaces with GCC&#039;s libgccjit API&lt;br /&gt;
* &#039;&#039;&#039;gccrs&#039;&#039;&#039;: an experimental Rust frontend for GCC&lt;br /&gt;
&lt;br /&gt;
Neither solution is complete at this time, but &#039;&#039;&#039;rustc_codegen_gcc&#039;&#039;&#039; is much further along and is quite usable with some patience with its current limitations. On the other hand, &#039;&#039;&#039;gccrs&#039;&#039;&#039; can compile for Dreamcast, but is in an earlier state. Below we will focus on using &#039;&#039;&#039;rustc_codegen_gcc&#039;&#039;&#039;. For more information on using gccrs, see the [[gccrs]] page.&lt;br /&gt;
&lt;br /&gt;
=Current Status=&lt;br /&gt;
* The full &#039;&#039;&#039;std&#039;&#039;&#039; library is supported.&lt;br /&gt;
* A [https://github.com/dreamcast-rs/KallistiOS custom version of KallistiOS] v2.1.0 (latest stable release) is used.&lt;br /&gt;
** This version includes minor changes for Rust and a beta libpthread addon layer to interface with the Rust standard library.&lt;br /&gt;
** Wrappers are provided to invoke &#039;&#039;&#039;cargo&#039;&#039;&#039; and &#039;&#039;&#039;rustc&#039;&#039;&#039; in a familiar manner to compile projects.&lt;br /&gt;
* A [https://github.com/dreamcast-rs/kos-sys &#039;&#039;&#039;kos-sys&#039;&#039;&#039;] raw/unsafe bindings crate is provided for easily linking KallistiOS and its unsafe C API into Rust crates. This crate is in active development, but with a large portion of KallistiOS functionality available now via C FFI.&lt;br /&gt;
* A [https://github.com/dreamcast-rs/kos-rs &#039;&#039;&#039;kos-rs&#039;&#039;&#039;] Rust crate with safe idiomatic interfaces is available but is in very early stages.&lt;br /&gt;
** Documentation for the bindings is available at [https://kos-rs.dreamcast.wiki/ kos-rs.dreamcast.wiki].&lt;br /&gt;
** If bindings do not yet exist for desired functionality, wrapping of KallistiOS C functions is required to get proper use of KallistiOS at this time. See the [https://doc.rust-lang.org/nomicon/ffi.html Foreign Function Interface page] in the Rustonomicon for further understanding. &lt;br /&gt;
** A custom [https://github.com/dreamcast-rs/libc/tree/libc-0.2-kos &#039;&#039;&#039;libc&#039;&#039;&#039; crate with KallistiOS support] is provided.&lt;br /&gt;
* Code generation uses &#039;&#039;&#039;rustc_codegen_gcc&#039;&#039;&#039;, so its limitations apply here. See the [https://blog.antoyo.xyz/ development blog] for more information on its progress.&lt;br /&gt;
** rustc_codegen_gcc is pinned to a particular Rust nightly version, but is synced regularly. The current version in use is the &#039;&#039;&#039;2024-08-10&#039;&#039;&#039; nightly.&lt;br /&gt;
** A [https://github.com/dreamcast-rs/rust/tree/kos-2024-08-10 KallistiOS-patched version of the latest nightly] is maintained and updated regularly. &lt;br /&gt;
** Panic unwinding, debug info, LTO, etc. are not currently available.&lt;br /&gt;
** Architectures dependent on rustc_codegen_gcc are not yet able to be added to the rustc frontend, so a workaround is necessary: MIPS is falsely selected for KallistiOS, causing rustc to emit objects with a MIPS header and a provided wrapper is used when linking to rewrite these MIPS headers to SuperH before passing the compiled objects to the linker.&lt;br /&gt;
* A separate [https://github.com/rust-lang/gcc development version of the GCC toolchain] must be used.&lt;br /&gt;
** This version of GCC contains the latest libgccjit patches necessary to interface with rustc_codegen_gcc, and will be built to work with the custom KallistiOS&#039;s libpthread layer.&lt;br /&gt;
** New updates to rustc_codegen_gcc often require new updates to GCC, so recompiling GCC regularly will be necessary to keep up with the latest updates.&lt;br /&gt;
** The development version of GCC does not always compile well on every platform. Using a modern Linux platform is recommended for maximum compatibility.&lt;br /&gt;
** The custom KallistiOS and GCC environment can still be used to compile code or projects written in C, C++, etc.&lt;br /&gt;
* KallistiOS and all related libraries and tools must be compiled using the &amp;lt;code&amp;gt;-m4-single&amp;lt;/code&amp;gt; ABI. This is already set up for you in the guide below, but if external libraries are being linked to Rust projects, make sure that code is compiled using the proper ABI.&lt;br /&gt;
&lt;br /&gt;
=Setting up a Rust environment for Dreamcast development=&lt;br /&gt;
The following guide will provide instructions for setting up a Rust development environment for Dreamcast. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;If you run into any errors or other challenges while following this tutorial, or simply need clarification on any of the steps, feel free to ask for assistance on the [https://dcemulation.org/phpBB/viewforum.php?f=29 message board] or [https://discord.gg/wDEfkUh63h our Discord server] and we would be happy to aid you and update the guide for the benefit of future readers and others in the community.&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
==Prerequisites==&lt;br /&gt;
First, we will need to set up the prerequisites.&lt;br /&gt;
&lt;br /&gt;
* You must have the [https://rustup.rs/ rustup] tool installed for your operating system to manage Rust toolchain installations. It will automatically manage the latest Rust nightly installations for you. You cannot use a Rust toolchain that may be provided by your operating system vendor.&lt;br /&gt;
* Install the dependency packages for your operating system. These are listed in the &#039;&#039;&#039;Dependencies&#039;&#039;&#039; section on the [[Getting Started with Dreamcast development#Dependencies]] page, with lists provided for most common operating systems.&lt;br /&gt;
* We will install our custom Rust toolchain environment within &amp;lt;code&amp;gt;/opt/toolchains/dc/rust&amp;lt;/code&amp;gt;. If it does not already exist, create the &amp;lt;code&amp;gt;/opt/toolchains/dc&amp;lt;/code&amp;gt; directory and grant it proper permissions using the following commands. &lt;br /&gt;
 sudo mkdir -p /opt/toolchains/dc&lt;br /&gt;
 sudo chmod -R 755 /opt/toolchains/dc&lt;br /&gt;
 sudo chown -R $(id -u):$(id -g) /opt/toolchains/dc&lt;br /&gt;
* Clone the [https://github.com/dreamcast-rs/rust-for-dreamcast/ Rust for Dreamcast] repo containing necessary support files to &amp;lt;code&amp;gt;/opt/toolchains/dc/rust&amp;lt;/code&amp;gt;:&lt;br /&gt;
 git clone https://github.com/dreamcast-rs/rust-for-dreamcast.git /opt/toolchains/dc/rust&lt;br /&gt;
&lt;br /&gt;
==Installing our custom KallistiOS and GCC toolchain==&lt;br /&gt;
Clone the git repository for our custom Rust-patched version of KallistiOS v2.1.0 stable:&lt;br /&gt;
 git clone https://github.com/dreamcast-rs/KallistiOS /opt/toolchains/dc/rust/kos&lt;br /&gt;
&lt;br /&gt;
Enter the dc-chain directory:&lt;br /&gt;
 cd /opt/toolchains/dc/rust/kos/utils/dc-chain&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;Makefile.default.cfg&amp;lt;/code&amp;gt; file in this directory has been customized for you, selecting the proper toolchain profile, paths, and build options necessary for building a Rust toolchain using this guide. It is not recommended to adjust the defaults.&lt;br /&gt;
&lt;br /&gt;
To build the GCC toolchain using 2 CPU cores, simply type:&lt;br /&gt;
 make makejobs=2&lt;br /&gt;
Adjust the &amp;lt;code&amp;gt;makejobs=...&amp;lt;/code&amp;gt; value to use the number of CPU cores available to you on your system in order to speed up compilation. This may take a while to build, so be patient.&lt;br /&gt;
&lt;br /&gt;
Once the GCC toolchain is built, source the &amp;lt;code&amp;gt;environ.sh&amp;lt;/code&amp;gt; KallistiOS environment settings. A custom &amp;lt;code&amp;gt;environ.sh&amp;lt;/code&amp;gt; file has been provided for you, containing settings pre-adjusted for Rust development using this guide.&lt;br /&gt;
&lt;br /&gt;
You will need to run the source command to apply the KallistiOS environment settings to your currently running shell. Run the following now, &#039;&#039;&#039;and&#039;&#039;&#039; &#039;&#039;whenever&#039;&#039; you open a new shell to work on Dreamcast projects:&lt;br /&gt;
 source /opt/toolchains/dc/rust/misc/environ.sh&lt;br /&gt;
&lt;br /&gt;
Enter the KallistiOS directory:&lt;br /&gt;
 cd /opt/toolchains/dc/rust/kos&lt;br /&gt;
&lt;br /&gt;
Build KallistiOS:&lt;br /&gt;
 make&lt;br /&gt;
&lt;br /&gt;
==Building rustc_codegen_gcc and Rust sysroot==&lt;br /&gt;
Now that we have a working KallistiOS environment suitable for Rust development set up, we can build the Rust compiler compiler and sysroot components. A script is provided which will download the necessary components from the respective repositories and compile them for you. Run the installer script:&lt;br /&gt;
&lt;br /&gt;
 /opt/toolchains/dc/rust/misc/install-rust.sh&lt;br /&gt;
&lt;br /&gt;
If all goes well, you&#039;ll see &#039;&#039;&#039;Rust for KallistiOS/Dreamcast installed!&#039;&#039;&#039; message!&lt;br /&gt;
&lt;br /&gt;
==Building KOS ports libraries==&lt;br /&gt;
If desired, you may also built the KOS ports collection of libraries for use with your Rust environment. It is recommended to maintain a separate KOS ports installation for linking with Rust projects in &amp;lt;code&amp;gt;/opt/toolchains/dc/rust/kos-ports&amp;lt;/code&amp;gt;. This is because the Rust environment is using the &amp;lt;code&amp;gt;-m4-single&amp;lt;/code&amp;gt; ABI, but the KallistiOS default installation will be using the &amp;lt;code&amp;gt;-m4-single-only&amp;lt;/code&amp;gt;, which cannot be mixed and matched.&lt;br /&gt;
&lt;br /&gt;
Clone the kos-ports repository to your system:&lt;br /&gt;
 git clone https://github.com/KallistiOS/kos-ports /opt/toolchains/dc/rust/kos-ports&lt;br /&gt;
An individual port can be built and installed by entering its directory and running the proper command. For example, installing GLdc:&lt;br /&gt;
 cd /opt/toolchains/dc/rust/kos-ports/libGL&lt;br /&gt;
 make install&lt;br /&gt;
Or, instead, you may run the script to build all of the included ports:&lt;br /&gt;
 /opt/toolchains/dc/rust/kos-ports/utils/build-all.sh&lt;br /&gt;
&lt;br /&gt;
=Using Rust for Dreamcast=&lt;br /&gt;
If all went well, you will now have a working Rust for Dreamcast environment!&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;cargo&#039;&#039;&#039; can be invoked to target Dreamcast using the &amp;lt;code&amp;gt;kos-cargo&amp;lt;/code&amp;gt; command.&lt;br /&gt;
** &amp;lt;code&amp;gt;kos-cargo run&amp;lt;/code&amp;gt; can be used to build and run code on a Dreamcast console using [[dcload-ip]] or [[dcload-serial]]. You will need to set up the &amp;lt;code&amp;gt;KOS_LOADER&amp;lt;/code&amp;gt; variable with the necessary command in the &amp;lt;code&amp;gt;/opt/toolchains/dc/rust/misc/environ.sh&amp;lt;/code&amp;gt; file (and then re-source the file for the change to take effect).&lt;br /&gt;
* &#039;&#039;&#039;rustc&#039;&#039;&#039; can be invoked to target Dreamcast using the &amp;lt;code&amp;gt;kos-rustc&amp;lt;/code&amp;gt; command.&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
Examples are included with the Rust for Dreamcast repo to help you get started.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/hello-cargo &amp;lt;code&amp;gt;hello-cargo&amp;lt;/code&amp;gt;] demonstrates how to create a simple &amp;quot;Hello, world!&amp;quot; application using cargo.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/hello-make &amp;lt;code&amp;gt;hello-make&amp;lt;/code&amp;gt;] demonstrates how to create a simple &amp;quot;Hello, world!&amp;quot; application using standard Makefiles.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/gldc-cube &amp;lt;code&amp;gt;gldc-cube&amp;lt;/code&amp;gt;] demonstrates a Rust project using the GLdc KOS port.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/addlib &amp;lt;code&amp;gt;addlib&amp;lt;/code&amp;gt;] demonstrates how to create a Rust library that can be included with a KallistiOS project.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/atomics &amp;lt;code&amp;gt;atomics&amp;lt;/code&amp;gt;] demonstrates the use of atomic functions.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/filesystem-io &amp;lt;code&amp;gt;filesystem-io&amp;lt;/code&amp;gt;] demonstrates listing directory contents and opening files.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/network-time &amp;lt;code&amp;gt;network-time&amp;lt;/code&amp;gt;] demonstrates displaying the system time and retrieving the current UTC time from an NTP server using the [[Broadband adapter]] or [[LAN adapter]].&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/pvr_strips_direct &amp;lt;code&amp;gt;pvr_strips_direct&amp;lt;/code&amp;gt;] demonstrates the use of PVR direct rendering&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/threads &amp;lt;code&amp;gt;threads&amp;lt;/code&amp;gt;] demonstrates the use of threads.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/tokio-async &amp;lt;code&amp;gt;tokio-async&amp;lt;/code&amp;gt;] demonstrates the use of the &#039;&#039;&#039;tokio&#039;&#039;&#039; async runtime.&lt;br /&gt;
&lt;br /&gt;
==Creating a new Rust project with Cargo==&lt;br /&gt;
First, we&#039;ll demonstrate creating a new &amp;quot;Hello, world!&amp;quot; project with &amp;lt;code&amp;gt;kos-cargo&amp;lt;/code&amp;gt;. This will follow the [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/hello-cargo &amp;lt;code&amp;gt;cargo-hello&amp;lt;/code&amp;gt;] example included in the Rust-for-Dreamcast repo. &lt;br /&gt;
&lt;br /&gt;
In a directory of your choosing, let&#039;s invoke &amp;lt;code&amp;gt;kos-cargo&amp;lt;/code&amp;gt; to create a new project and then enter the directory:&lt;br /&gt;
 kos-cargo new hello&lt;br /&gt;
 cd hello&lt;br /&gt;
&lt;br /&gt;
Let&#039;s add our kos-rs crate to gain access to current KallistiOS bindings. Open &amp;lt;code&amp;gt;Cargo.toml&amp;lt;/code&amp;gt; in your text editor and add:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;toml&amp;quot;&amp;gt;&lt;br /&gt;
[dependencies]&lt;br /&gt;
kos = { package = &amp;quot;kos-rs&amp;quot;, git = &amp;quot;https://github.com/dreamcast-rs/kos-rs&amp;quot; }&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we can open up &amp;lt;code&amp;gt;src/main.rs&amp;lt;/code&amp;gt; and write our &amp;quot;Hello, world!&amp;quot; example code:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; line&amp;gt;&lt;br /&gt;
#![no_main]&lt;br /&gt;
&lt;br /&gt;
#[no_mangle]&lt;br /&gt;
extern &amp;quot;C&amp;quot; fn main(_argc: isize, _argv: *const *const u8) -&amp;gt; isize {&lt;br /&gt;
    println!(&amp;quot;Hello, world from Rust!&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
    0&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;#![no_main]&amp;lt;/syntaxhighlight&amp;gt; tells Rust that we will not have Rust use a &amp;lt;code&amp;gt;main&amp;lt;/code&amp;gt; function as an entry point -- this will be handled by KallistiOS.&lt;br /&gt;
* &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;#[no_mangle]&amp;lt;/syntaxhighlight&amp;gt; tells Rust to disable name mangling so that the &amp;lt;code&amp;gt;main&amp;lt;/code&amp;gt; function can be used by KallistiOS&#039;s C code.&lt;br /&gt;
* Finally, we have a &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;fn main&amp;lt;/syntaxhighlight&amp;gt; with the function signature of a typical C &amp;lt;code&amp;gt;main&amp;lt;/code&amp;gt; function, containing a basic &amp;quot;Hello, world!&amp;quot; exclamation.&lt;br /&gt;
&lt;br /&gt;
Now we can use &amp;lt;code&amp;gt;kos-cargo build&amp;lt;/code&amp;gt; to build our project. If all goes well, there will be a &amp;lt;code&amp;gt;target/sh-elf/debug/hello.elf&amp;lt;/code&amp;gt; file that can be sent to the Dreamcast with &amp;lt;code&amp;gt;dc-tool&amp;lt;/code&amp;gt;.&lt;br /&gt;
If you have &amp;lt;code&amp;gt;KOS_LOADER&amp;lt;/code&amp;gt; set in the &amp;lt;code&amp;gt;environ.sh&amp;lt;/code&amp;gt; file, you can invoke it directly with &amp;lt;code&amp;gt;kos-cargo run&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Creating a Rust project using kos-ports libraries==&lt;br /&gt;
[[File:Rust-Cube rustc codegen-gcc demo.gif|thumb|cube example in action]]&lt;br /&gt;
The [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/gldc-cube &amp;lt;code&amp;gt;gldc-cube&amp;lt;/code&amp;gt;] example demonstrates creating a rotating 3D cube using Rust as the primary language, while calling C functions provided by the &#039;&#039;&#039;GLdc&#039;&#039;&#039; library available in kos-ports. This project&#039;s initial setup is done the same as the above &amp;lt;code&amp;gt;hello&amp;lt;/code&amp;gt; example.&lt;br /&gt;
&lt;br /&gt;
For this example, we&#039;ll need to convert JPEG textures to VQ-encoded textures at compile time using the &amp;lt;code&amp;gt;vqenc&amp;lt;/code&amp;gt; utility includes with KallistiOS. To do this, we&#039;ll add a &amp;lt;code&amp;gt;build.rs&amp;lt;/code&amp;gt; file to the root of the crate with the following code (abridged to show the conversion of only one texture):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot;&amp;gt;&lt;br /&gt;
fn main() {&lt;br /&gt;
    let kos_base = std::env::var(&amp;quot;KOS_BASE&amp;quot;).expect(&amp;quot;Missing $KOS_BASE -- KallistiOS environment not sourced!&amp;quot;);&lt;br /&gt;
    let vqenc_cmd = kos_base + &amp;quot;/utils/vqenc/vqenc&amp;quot;;&lt;br /&gt;
    Command::new(&amp;amp;vqenc_cmd)&lt;br /&gt;
        .arg(&amp;quot;-t&amp;quot;)&lt;br /&gt;
        .arg(&amp;quot;-v&amp;quot;)&lt;br /&gt;
        .arg(&amp;quot;rsrc/tex_claw.jpg&amp;quot;)&lt;br /&gt;
        .output()&lt;br /&gt;
        .expect(&amp;quot;vqenc on tex_claw.jpg failed!&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
The texture files are then included in our project using the &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;include_bytes!&amp;lt;/syntaxhighlight&amp;gt; macro.&lt;br /&gt;
&lt;br /&gt;
We will need to link the GLdc library in this example, but we don&#039;t need to do anything special to add the paths to this library, because adding the common paths to KallistiOS libraries is already done for us in the kos-rs crate. However, if in your project you need to include a separate unique library path, you can add it in your &amp;lt;code&amp;gt;build.rs&amp;lt;/code&amp;gt; like so:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot;&amp;gt;&lt;br /&gt;
println!(&amp;quot;cargo:rustc-link-search=native={}&amp;quot;, lib_path);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The workings of this example&#039;s source code are too great to detail here line-by-line, but the example demonstrates how to use GLdc via the [https://github.com/dreamcast-rs/gldc-sys gldc-sys] crate, which contains unsafe/raw bindings to GLdc. Check out this crate&#039;s source code for an example of declaring and binding external C functions, constants, and structures so they can be used in Rust code. Since the entirety of the gldc-cube example uses C FFI functions without safe wrappers, the &amp;lt;code&amp;gt;main()&amp;lt;/code&amp;gt; source is wrapped in &amp;lt;code&amp;gt;unsafe {}&amp;lt;/code&amp;gt;. In the future, this would be much less necessary as higher level safe Rust bindings to KallistiOS and other libraries become mature.&lt;br /&gt;
&lt;br /&gt;
==Creating a Rust library==&lt;br /&gt;
Next, we&#039;ll demonstrate creating a Rust library with &amp;lt;code&amp;gt;kos-cargo&amp;lt;/code&amp;gt; that can be included in other Dreamcast code. This will follow the [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/addlib &amp;lt;code&amp;gt;addlib&amp;lt;/code&amp;gt;] example. Once again, this project&#039;s initial setup is done the same as the above &amp;lt;code&amp;gt;hello&amp;lt;/code&amp;gt; example, but you&#039;ll create the new project using &amp;lt;code&amp;gt;kos-cargo new --lib addlib&amp;lt;/code&amp;gt; to specify that we&#039;re creating a library named &amp;lt;code&amp;gt;addlib&amp;lt;/code&amp;gt;. You&#039;ll also need to add the following text to this project&#039;s &amp;lt;code&amp;gt;Cargo.toml&amp;lt;/code&amp;gt; file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;toml&amp;quot;&amp;gt;&lt;br /&gt;
[lib]&lt;br /&gt;
crate-type = [&amp;quot;staticlib&amp;quot;]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This tells Rust to build a static &amp;lt;code&amp;gt;.a&amp;lt;/code&amp;gt; library archive file from our code, which is located in &amp;lt;code&amp;gt;src/lib.rs&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; line&amp;gt;&lt;br /&gt;
#[no_mangle]&lt;br /&gt;
pub extern &amp;quot;C&amp;quot; fn print_added(a: isize, b: isize) {&lt;br /&gt;
    print!(&amp;quot;{}&amp;quot;, a + b);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
#[no_mangle]&lt;br /&gt;
pub extern &amp;quot;C&amp;quot; fn add_integers(a: isize, b: isize) -&amp;gt; isize {&lt;br /&gt;
    a + b&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The source code here starts similarly to the &amp;quot;Hello, world!&amp;quot; example, except we don&#039;t need to specify &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;#![no_main]&amp;lt;/syntaxhighlight&amp;gt; as this is a library which wouldn&#039;t have a &amp;lt;code&amp;gt;main()&amp;lt;/code&amp;gt; function anyway.&lt;br /&gt;
&lt;br /&gt;
Two simple functions are provided: one for adding two integers and returning the result, and another for adding two integers and printing the result as text. Because these functions use &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;#[no_mangle]&amp;lt;/syntaxhighlight&amp;gt; and are declared &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;extern &amp;quot;C&amp;quot;&amp;lt;/syntaxhighlight&amp;gt;, they can be called by name in C code that links this library.&lt;br /&gt;
&lt;br /&gt;
When built using &amp;lt;code&amp;gt;kos-cargo build&amp;lt;/code&amp;gt;, a &amp;lt;code&amp;gt;target/sh-elf/debug/libaddlib.a&amp;lt;/code&amp;gt; file will be generated. This can be linked into other projects to gain the use of these functions.&lt;br /&gt;
&lt;br /&gt;
For example, this can be added to a standard &amp;lt;code&amp;gt;Makefile&amp;lt;/code&amp;gt;-based KallistiOS project by editing the &amp;lt;code&amp;gt;Makefile&amp;lt;/code&amp;gt;:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;make&amp;quot;&amp;gt;&lt;br /&gt;
$(TARGET): $(OBJS)&lt;br /&gt;
	kos-cc -o $(TARGET) $(OBJS) -L/opt/toolchains/dc/rust/examples/cargo-addlib/target/sh-elf/debug -laddlib&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then, we can use the code in our C source:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/* Declare the external function from the Rust library */&lt;br /&gt;
int add_integers(int a, int b);&lt;br /&gt;
&lt;br /&gt;
/* Use the function */&lt;br /&gt;
printf(&amp;quot;Five plus six is %d\n&amp;quot;, add_integers(5, 6));&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Compiling individual modules into object files with rustc==&lt;br /&gt;
If we&#039;d like to mix C and Rust code in the same &amp;lt;code&amp;gt;Makefile&amp;lt;/code&amp;gt;-based KallistiOS project without building an entirely separate library, we can do that as well. This is demonstrated in the [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/no_std/hello-make &amp;lt;code&amp;gt;hello-make&amp;lt;/code&amp;gt;] example.&lt;br /&gt;
&lt;br /&gt;
Instead of using &amp;lt;code&amp;gt;kos-cargo&amp;lt;/code&amp;gt;, we can invoke the &amp;lt;code&amp;gt;kos-rustc&amp;lt;/code&amp;gt; via a &amp;lt;code&amp;gt;Makefile&amp;lt;/code&amp;gt; to build Rust modules. If we assume the Rust file is named &amp;lt;code&amp;gt;example.rs&amp;lt;/code&amp;gt;, you&#039;ll need to add &amp;lt;code&amp;gt;example.o&amp;lt;/code&amp;gt; as an object file in your &amp;lt;code&amp;gt;Makefile&amp;lt;/code&amp;gt;&#039;s &amp;lt;code&amp;gt;OBJS =&amp;lt;/code&amp;gt; declaration. For example, if the project has two source files &amp;lt;code&amp;gt;hello_c.c&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;hello_rust.rs&amp;lt;/code&amp;gt;, our &amp;lt;code&amp;gt;Makefile&amp;lt;/code&amp;gt; would have a line like this:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;make&amp;quot;&amp;gt;&lt;br /&gt;
OBJS = hello_c.o hello_rust.o&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The example code demonstrates starting a C &amp;lt;code&amp;gt;main()&amp;lt;/code&amp;gt; function to call a Rust function which builds a &amp;lt;code&amp;gt;String&amp;lt;/code&amp;gt; containing the &amp;quot;Hello, world!&amp;quot; text which is passed back to a C function which prints &amp;lt;code&amp;gt;String&amp;lt;/code&amp;gt;s.&lt;br /&gt;
&lt;br /&gt;
==Using the libc crate with KallistiOS==&lt;br /&gt;
If any of the crates in your project&#039;s dependency tree requires the &#039;&#039;&#039;libc&#039;&#039;&#039; crate, you will need to override fetching the default crate with our custom libc crate with KallistiOS/Dreamcast definitions. This custom version was copied to &amp;lt;code&amp;gt;/opt/toolchains/dc/rust/libc&amp;lt;/code&amp;gt; when building the Rust sysroot earlier. Add the following text to your &amp;lt;code&amp;gt;Cargo.toml&amp;lt;/code&amp;gt;:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;toml&amp;quot;&amp;gt;&lt;br /&gt;
[patch.crates-io]&lt;br /&gt;
libc = { path = &amp;quot;/opt/toolchains/dc/rust/libc&amp;quot; }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Adjusting build flags==&lt;br /&gt;
Build settings can be adjusted through the &amp;lt;code&amp;gt;KOS_RCG_RUSTFLAGS&amp;lt;/code&amp;gt; variable in the &amp;lt;code&amp;gt;environ.sh&amp;lt;/code&amp;gt; file under the &amp;lt;code&amp;gt;### Rust Flags&amp;lt;/code&amp;gt; section. &lt;br /&gt;
* To pass arguments to the &#039;&#039;&#039;rustc&#039;&#039;&#039; frontend, e.g. to disable debug assertions:&lt;br /&gt;
** &amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot; inline&amp;gt;export KOS_RCG_RUSTFLAGS=&amp;quot;${KOS_RCG_RUSTFLAGS} -Cdebug-assertions=no&amp;quot;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* To pass arguments to the GCC backend, e.g. to enable the &amp;lt;code&amp;gt;-freorder-blocks-algorithm=simple&amp;lt;/code&amp;gt; optimization for Rust code:&lt;br /&gt;
** &amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot; inline&amp;gt;export KOS_RCG_RUSTFLAGS=&amp;quot;${KOS_RCG_RUSTFLAGS} -Cllvm-args=-freorder-blocks-algorithm=simple&amp;quot;&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Darc</name></author>
	</entry>
	<entry>
		<id>https://dreamcast.wiki/wiki/index.php?title=Programming_language_support&amp;diff=3641</id>
		<title>Programming language support</title>
		<link rel="alternate" type="text/html" href="https://dreamcast.wiki/wiki/index.php?title=Programming_language_support&amp;diff=3641"/>
		<updated>2024-11-14T03:59:57Z</updated>

		<summary type="html">&lt;p&gt;Darc: /* Compiled languages */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The following languages can be used in Sega Dreamcast programming:&lt;br /&gt;
&lt;br /&gt;
=Assembly languages=&lt;br /&gt;
* &#039;&#039;&#039;SH4 assembly&#039;&#039;&#039;, as the native machine code for the Dreamcast&#039;s SuperH processor&lt;br /&gt;
* &#039;&#039;&#039;ARM assembly&#039;&#039;&#039;, as the native machine code for the Dreamcast&#039;s AICA ARM7DI sound processor&lt;br /&gt;
&lt;br /&gt;
=Compiled languages=&lt;br /&gt;
* &#039;&#039;&#039;C&#039;&#039;&#039;, the predominant language for Sega Dreamcast development, via the GCC C compiler.&lt;br /&gt;
* &#039;&#039;&#039;C++&#039;&#039;&#039;, commonly used and very well-supported, via the GCC C++ compiler.&lt;br /&gt;
* &#039;&#039;&#039;Objective-C&#039;&#039;&#039;, via the GCC Objective-C compiler. Preliminary support for Foundation framework via [https://gnustep.github.io/ GNUstep] is being developed. See [https://twitter.com/dinobj_c/status/1732399498977276154 Andrew Apperley&#039;s X account]. See [[Objective-C on Dreamcast]] for more information.&lt;br /&gt;
** The GNU &#039;&#039;&#039;Objective-C++&#039;&#039;&#039; extension is available as well.&lt;br /&gt;
* &#039;&#039;&#039;Rust&#039;&#039;&#039;, via rustc_codegen_gcc and libgccjit, or via gccrs. When using rustc_codegen_gcc, the standard library is available, with bindings to KallistiOS being developed. See [[Rust on Dreamcast]] for more information.&lt;br /&gt;
* &#039;&#039;&#039;D&#039;&#039;&#039;, via the GCC D compiler. Early support, see [https://dreamcast.wiki/D_on_Dreamcast D on Dreamcast] for more information.&lt;br /&gt;
* &#039;&#039;&#039;Ada&#039;&#039;&#039;, via GNAT. [https://github.com/dkm/ada-dreamcast-helloworld Ada 3D cube demo] by [https://poulhies.fr/ Marc Poulhiès].&lt;br /&gt;
* &#039;&#039;&#039;Fortran&#039;&#039;&#039;, via GFortran. Requires some work to create a cross-compiler, but does work.&lt;br /&gt;
&lt;br /&gt;
=Scripting/Interpreted languages=&lt;br /&gt;
* Lua (available in [[kos-ports]])&lt;br /&gt;
* MicroPython (available in [[kos-ports]])&lt;br /&gt;
* Tcl (available in [[kos-ports]])&lt;br /&gt;
* mRuby (available in [[kos-ports]])&lt;br /&gt;
* AngelScript&lt;br /&gt;
* QuickJS&lt;/div&gt;</summary>
		<author><name>Darc</name></author>
	</entry>
	<entry>
		<id>https://dreamcast.wiki/wiki/index.php?title=Rust_on_Dreamcast&amp;diff=3640</id>
		<title>Rust on Dreamcast</title>
		<link rel="alternate" type="text/html" href="https://dreamcast.wiki/wiki/index.php?title=Rust_on_Dreamcast&amp;diff=3640"/>
		<updated>2024-11-14T01:07:38Z</updated>

		<summary type="html">&lt;p&gt;Darc: /* Current Status */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[File:Rust-dc-logo.png|thumb|Ferris holding his Dreamcast controller]]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Rust&#039;&#039;&#039; is a popular modern programming language focusing on performance, memory safety, and program correctness. As a &amp;quot;blazingly fast&amp;quot; low-level language, it is viable for running on a Dreamcast console. Doing so presents a bit of a challenge, however, as the official Rust compiler is based on the [https://llvm.org/ LLVM] toolchain infrastructure, which does not support the Dreamcast CPU&#039;s SuperH architecture. Dreamcast programming is instead typically done with [https://gcc.gnu.org/ GCC], the GNU Compiler Collection. There are currently two viable solutions to this challenge:&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;rustc_codegen_gcc&#039;&#039;&#039;, the current preferred method: An experimental codegen backend for the official Rust &#039;&#039;&#039;rustc&#039;&#039;&#039; compiler which interfaces with GCC&#039;s libgccjit API&lt;br /&gt;
* &#039;&#039;&#039;gccrs&#039;&#039;&#039;: an experimental Rust frontend for GCC&lt;br /&gt;
&lt;br /&gt;
Neither solution is complete at this time, but &#039;&#039;&#039;rustc_codegen_gcc&#039;&#039;&#039; is much further along and is quite usable with some patience with its current limitations. On the other hand, &#039;&#039;&#039;gccrs&#039;&#039;&#039; can compile for Dreamcast, but is in an earlier state. Below we will focus on using &#039;&#039;&#039;rustc_codegen_gcc&#039;&#039;&#039;. For more information on using gccrs, see the [[gccrs]] page.&lt;br /&gt;
&lt;br /&gt;
=Current Status=&lt;br /&gt;
* The full &#039;&#039;&#039;std&#039;&#039;&#039; library is supported.&lt;br /&gt;
* A [https://github.com/dreamcast-rs/KallistiOS custom version of KallistiOS] v2.1.0 (latest stable release) is used.&lt;br /&gt;
** This version includes minor changes for Rust and a beta libpthread addon layer to interface with the Rust standard library.&lt;br /&gt;
** Wrappers are provided to invoke &#039;&#039;&#039;cargo&#039;&#039;&#039; and &#039;&#039;&#039;rustc&#039;&#039;&#039; in a familiar manner to compile projects.&lt;br /&gt;
* A [https://github.com/dreamcast-rs/kos-sys &#039;&#039;&#039;kos-sys&#039;&#039;&#039;] raw/unsafe bindings crate is provided for easily linking KallistiOS and its unsafe C API into Rust crates. This crate is in active development, but with a large portion of KallistiOS functionality available now via C FFI.&lt;br /&gt;
* A [https://github.com/dreamcast-rs/kos-rs &#039;&#039;&#039;kos-rs&#039;&#039;&#039;] Rust crate with safe idiomatic interfaces is available but is in very early stages.&lt;br /&gt;
** Documentation for the bindings is available at [https://kos-rs.dreamcast.wiki/ kos-rs.dreamcast.wiki].&lt;br /&gt;
** If bindings do not yet exist for desired functionality, wrapping of KallistiOS C functions is required to get proper use of KallistiOS at this time. See the [https://doc.rust-lang.org/nomicon/ffi.html Foreign Function Interface page] in the Rustonomicon for further understanding. &lt;br /&gt;
** A custom [https://github.com/dreamcast-rs/libc/tree/libc-0.2-kos &#039;&#039;&#039;libc&#039;&#039;&#039; crate with KallistiOS support] is provided.&lt;br /&gt;
* Code generation uses &#039;&#039;&#039;rustc_codegen_gcc&#039;&#039;&#039;, so its limitations apply here. See the [https://blog.antoyo.xyz/ development blog] for more information on its progress.&lt;br /&gt;
** rustc_codegen_gcc is pinned to a particular Rust nightly version, but is synced regularly. The current version in use is the &#039;&#039;&#039;2024-08-10&#039;&#039;&#039; nightly.&lt;br /&gt;
** A [https://github.com/dreamcast-rs/rust/tree/kos-2024-08-10 KallistiOS-patched version of the latest nightly] is maintained and updated regularly. &lt;br /&gt;
** Panic unwinding, debug info, LTO, etc. are not currently available.&lt;br /&gt;
** Architectures dependent on rustc_codegen_gcc are not yet able to be added to the rustc frontend, so a workaround is necessary: MIPS is falsely selected for KallistiOS, causing rustc to emit objects with a MIPS header and a provided wrapper is used when linking to rewrite these MIPS headers to SuperH before passing the compiled objects to the linker.&lt;br /&gt;
* A separate [https://github.com/rust-lang/gcc development version of the GCC toolchain] must be used.&lt;br /&gt;
** This version of GCC contains the latest libgccjit patches necessary to interface with rustc_codegen_gcc, and will be built to work with the custom KallistiOS&#039;s libpthread layer.&lt;br /&gt;
** New updates to rustc_codegen_gcc often require new updates to GCC, so recompiling GCC regularly will be necessary to keep up with the latest updates.&lt;br /&gt;
** The development version of GCC does not always compile well on every platform. Using a modern Linux platform is recommended for maximum compatibility.&lt;br /&gt;
** The custom KallistiOS and GCC environment can still be used to compile code or projects written in C, C++, etc.&lt;br /&gt;
* KallistiOS and all related libraries and tools must be compiled using the &amp;lt;code&amp;gt;-m4-single&amp;lt;/code&amp;gt; ABI. This is already set up for you in the guide below, but if external libraries are being linked to Rust projects, make sure that code is compiled using the proper ABI.&lt;br /&gt;
&lt;br /&gt;
=Setting up a Rust environment for Dreamcast development=&lt;br /&gt;
The following guide will provide instructions for setting up a Rust development environment for Dreamcast. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;If you run into any errors or other challenges while following this tutorial, or simply need clarification on any of the steps, feel free to ask for assistance on the [https://dcemulation.org/phpBB/viewforum.php?f=29 message board] or [https://discord.gg/wDEfkUh63h our Discord server] and we would be happy to aid you and update the guide for the benefit of future readers and others in the community.&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
==Prerequisites==&lt;br /&gt;
First, we will need to set up the prerequisites.&lt;br /&gt;
&lt;br /&gt;
* You must have the [https://rustup.rs/ rustup] tool installed for your operating system to manage Rust toolchain installations. It will automatically manage the latest Rust nightly installations for you. You cannot use a Rust toolchain that may be provided by your operating system vendor.&lt;br /&gt;
* Install the dependency packages for your operating system. These are listed in the &#039;&#039;&#039;Dependencies&#039;&#039;&#039; section on the [[Getting Started with Dreamcast development#Dependencies]] page, with lists provided for most common operating systems.&lt;br /&gt;
* We will install our custom Rust toolchain environment within &amp;lt;code&amp;gt;/opt/toolchains/dc/rust&amp;lt;/code&amp;gt;. If it does not already exist, create the &amp;lt;code&amp;gt;/opt/toolchains/dc&amp;lt;/code&amp;gt; directory and grant it proper permissions using the following commands. &lt;br /&gt;
 sudo mkdir -p /opt/toolchains/dc&lt;br /&gt;
 sudo chmod -R 755 /opt/toolchains/dc&lt;br /&gt;
 sudo chown -R $(id -u):$(id -g) /opt/toolchains/dc&lt;br /&gt;
* Clone the [https://github.com/dreamcast-rs/rust-for-dreamcast/ Rust for Dreamcast] repo containing necessary support files to &amp;lt;code&amp;gt;/opt/toolchains/dc/rust&amp;lt;/code&amp;gt;:&lt;br /&gt;
 git clone https://github.com/dreamcast-rs/rust-for-dreamcast.git /opt/toolchains/dc/rust&lt;br /&gt;
&lt;br /&gt;
==Installing our custom KallistiOS and GCC toolchain==&lt;br /&gt;
Clone the git repository for our custom Rust-patched version of KallistiOS v2.1.0 stable:&lt;br /&gt;
 git clone https://github.com/dreamcast-rs/KallistiOS /opt/toolchains/dc/rust/kos&lt;br /&gt;
&lt;br /&gt;
Enter the dc-chain directory:&lt;br /&gt;
 cd /opt/toolchains/dc/rust/kos/utils/dc-chain&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;Makefile.default.cfg&amp;lt;/code&amp;gt; file in this directory has been customized for you, selecting the proper toolchain profile, paths, and build options necessary for building a Rust toolchain using this guide. It is not recommended to adjust the defaults.&lt;br /&gt;
&lt;br /&gt;
To build the GCC toolchain using 2 CPU cores, simply type:&lt;br /&gt;
 make makejobs=2&lt;br /&gt;
Adjust the &amp;lt;code&amp;gt;makejobs=...&amp;lt;/code&amp;gt; value to use the number of CPU cores available to you on your system in order to speed up compilation. This may take a while to build, so be patient.&lt;br /&gt;
&lt;br /&gt;
Once the GCC toolchain is built, source the &amp;lt;code&amp;gt;environ.sh&amp;lt;/code&amp;gt; KallistiOS environment settings. A custom &amp;lt;code&amp;gt;environ.sh&amp;lt;/code&amp;gt; file has been provided for you, containing settings pre-adjusted for Rust development using this guide.&lt;br /&gt;
&lt;br /&gt;
You will need to run the source command to apply the KallistiOS environment settings to your currently running shell. Run the following now, &#039;&#039;&#039;and&#039;&#039;&#039; &#039;&#039;whenever&#039;&#039; you open a new shell to work on Dreamcast projects:&lt;br /&gt;
 source /opt/toolchains/dc/rust/misc/environ.sh&lt;br /&gt;
&lt;br /&gt;
Enter the KallistiOS directory:&lt;br /&gt;
 cd /opt/toolchains/dc/rust/kos&lt;br /&gt;
&lt;br /&gt;
Build KallistiOS:&lt;br /&gt;
 make&lt;br /&gt;
&lt;br /&gt;
==Building rustc_codegen_gcc and Rust sysroot==&lt;br /&gt;
Now that we have a working KallistiOS environment suitable for Rust development set up, we can build the Rust compiler compiler and sysroot components. A script is provided which will download the necessary components from the respective repositories and compile them for you. Run the installer script:&lt;br /&gt;
&lt;br /&gt;
 /opt/toolchains/dc/rust/misc/install-rust.sh&lt;br /&gt;
&lt;br /&gt;
If all goes well, you&#039;ll see &#039;&#039;&#039;Rust for KallistiOS/Dreamcast installed!&#039;&#039;&#039; message!&lt;br /&gt;
&lt;br /&gt;
==Building KOS ports libraries==&lt;br /&gt;
If desired, you may also built the KOS ports collection of libraries for use with your Rust environment. It is recommended to maintain a separate KOS ports installation for linking with Rust projects in &amp;lt;code&amp;gt;/opt/toolchains/dc/rust/kos-ports&amp;lt;/code&amp;gt;. This is because the Rust environment is using the &amp;lt;code&amp;gt;-m4-single&amp;lt;/code&amp;gt; ABI, but the KallistiOS default installation will be using the &amp;lt;code&amp;gt;-m4-single-only&amp;lt;/code&amp;gt;, which cannot be mixed and matched.&lt;br /&gt;
&lt;br /&gt;
Clone the kos-ports repository to your system:&lt;br /&gt;
 git clone https://github.com/KallistiOS/kos-ports /opt/toolchains/dc/rust/kos-ports&lt;br /&gt;
An individual port can be built and installed by entering its directory and running the proper command. For example, installing GLdc:&lt;br /&gt;
 cd /opt/toolchains/dc/rust/kos-ports/libGL&lt;br /&gt;
 make install&lt;br /&gt;
Or, instead, you may run the script to build all of the included ports:&lt;br /&gt;
 /opt/toolchains/dc/rust/kos-ports/utils/build-all.sh&lt;br /&gt;
&lt;br /&gt;
=Using Rust for Dreamcast=&lt;br /&gt;
If all went well, you will now have a working Rust for Dreamcast environment!&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;cargo&#039;&#039;&#039; can be invoked to target Dreamcast using the &amp;lt;code&amp;gt;kos-cargo&amp;lt;/code&amp;gt; command.&lt;br /&gt;
** &amp;lt;code&amp;gt;kos-cargo run&amp;lt;/code&amp;gt; can be used to build and run code on a Dreamcast console using [[dcload-ip]] or [[dcload-serial]]. You will need to set up the &amp;lt;code&amp;gt;KOS_LOADER&amp;lt;/code&amp;gt; variable with the necessary command in the &amp;lt;code&amp;gt;/opt/toolchains/dc/rust/misc/environ.sh&amp;lt;/code&amp;gt; file (and then re-source the file for the change to take effect).&lt;br /&gt;
* &#039;&#039;&#039;rustc&#039;&#039;&#039; can be invoked to target Dreamcast using the &amp;lt;code&amp;gt;kos-rustc&amp;lt;/code&amp;gt; command.&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
Examples are included with the Rust for Dreamcast repo to help you get started.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/hello-cargo &amp;lt;code&amp;gt;hello-cargo&amp;lt;/code&amp;gt;] demonstrates how to create a simple &amp;quot;Hello, world!&amp;quot; application using cargo.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/hello-make &amp;lt;code&amp;gt;hello-make&amp;lt;/code&amp;gt;] demonstrates how to create a simple &amp;quot;Hello, world!&amp;quot; application using standard Makefiles.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/gldc-cube &amp;lt;code&amp;gt;gldc-cube&amp;lt;/code&amp;gt;] demonstrates a Rust project using the GLdc KOS port.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/addlib &amp;lt;code&amp;gt;addlib&amp;lt;/code&amp;gt;] demonstrates how to create a Rust library that can be included with a KallistiOS project.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/atomics &amp;lt;code&amp;gt;atomics&amp;lt;/code&amp;gt;] demonstrates the use of atomic functions.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/filesystem-io &amp;lt;code&amp;gt;filesystem-io&amp;lt;/code&amp;gt;] demonstrates listing directory contents and opening files.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/network-time &amp;lt;code&amp;gt;network-time&amp;lt;/code&amp;gt;] demonstrates displaying the system time and retrieving the current UTC time from an NTP server using the [[Broadband adapter]] or [[LAN adapter]].&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/threads &amp;lt;code&amp;gt;threads&amp;lt;/code&amp;gt;] demonstrates the use of threads.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/tokio-async &amp;lt;code&amp;gt;tokio-async&amp;lt;/code&amp;gt;] demonstrates the use of the &#039;&#039;&#039;tokio&#039;&#039;&#039; async runtime.&lt;br /&gt;
&lt;br /&gt;
==Creating a new Rust project with Cargo==&lt;br /&gt;
First, we&#039;ll demonstrate creating a new &amp;quot;Hello, world!&amp;quot; project with &amp;lt;code&amp;gt;kos-cargo&amp;lt;/code&amp;gt;. This will follow the [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/hello-cargo &amp;lt;code&amp;gt;cargo-hello&amp;lt;/code&amp;gt;] example included in the Rust-for-Dreamcast repo. &lt;br /&gt;
&lt;br /&gt;
In a directory of your choosing, let&#039;s invoke &amp;lt;code&amp;gt;kos-cargo&amp;lt;/code&amp;gt; to create a new project and then enter the directory:&lt;br /&gt;
 kos-cargo new hello&lt;br /&gt;
 cd hello&lt;br /&gt;
&lt;br /&gt;
Let&#039;s add our kos-rs crate to gain access to current KallistiOS bindings. Open &amp;lt;code&amp;gt;Cargo.toml&amp;lt;/code&amp;gt; in your text editor and add:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;toml&amp;quot;&amp;gt;&lt;br /&gt;
[dependencies]&lt;br /&gt;
kos = { package = &amp;quot;kos-rs&amp;quot;, git = &amp;quot;https://github.com/dreamcast-rs/kos-rs&amp;quot; }&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we can open up &amp;lt;code&amp;gt;src/main.rs&amp;lt;/code&amp;gt; and write our &amp;quot;Hello, world!&amp;quot; example code:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; line&amp;gt;&lt;br /&gt;
#![no_main]&lt;br /&gt;
&lt;br /&gt;
#[no_mangle]&lt;br /&gt;
extern &amp;quot;C&amp;quot; fn main(_argc: isize, _argv: *const *const u8) -&amp;gt; isize {&lt;br /&gt;
    println!(&amp;quot;Hello, world from Rust!&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
    0&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;#![no_main]&amp;lt;/syntaxhighlight&amp;gt; tells Rust that we will not have Rust use a &amp;lt;code&amp;gt;main&amp;lt;/code&amp;gt; function as an entry point -- this will be handled by KallistiOS.&lt;br /&gt;
* &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;#[no_mangle]&amp;lt;/syntaxhighlight&amp;gt; tells Rust to disable name mangling so that the &amp;lt;code&amp;gt;main&amp;lt;/code&amp;gt; function can be used by KallistiOS&#039;s C code.&lt;br /&gt;
* Finally, we have a &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;fn main&amp;lt;/syntaxhighlight&amp;gt; with the function signature of a typical C &amp;lt;code&amp;gt;main&amp;lt;/code&amp;gt; function, containing a basic &amp;quot;Hello, world!&amp;quot; exclamation.&lt;br /&gt;
&lt;br /&gt;
Now we can use &amp;lt;code&amp;gt;kos-cargo build&amp;lt;/code&amp;gt; to build our project. If all goes well, there will be a &amp;lt;code&amp;gt;target/sh-elf/debug/hello.elf&amp;lt;/code&amp;gt; file that can be sent to the Dreamcast with &amp;lt;code&amp;gt;dc-tool&amp;lt;/code&amp;gt;.&lt;br /&gt;
If you have &amp;lt;code&amp;gt;KOS_LOADER&amp;lt;/code&amp;gt; set in the &amp;lt;code&amp;gt;environ.sh&amp;lt;/code&amp;gt; file, you can invoke it directly with &amp;lt;code&amp;gt;kos-cargo run&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Creating a Rust project using kos-ports libraries==&lt;br /&gt;
[[File:Rust-Cube rustc codegen-gcc demo.gif|thumb|cube example in action]]&lt;br /&gt;
The [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/gldc-cube &amp;lt;code&amp;gt;gldc-cube&amp;lt;/code&amp;gt;] example demonstrates creating a rotating 3D cube using Rust as the primary language, while calling C functions provided by the &#039;&#039;&#039;GLdc&#039;&#039;&#039; library available in kos-ports. This project&#039;s initial setup is done the same as the above &amp;lt;code&amp;gt;hello&amp;lt;/code&amp;gt; example.&lt;br /&gt;
&lt;br /&gt;
For this example, we&#039;ll need to convert JPEG textures to VQ-encoded textures at compile time using the &amp;lt;code&amp;gt;vqenc&amp;lt;/code&amp;gt; utility includes with KallistiOS. To do this, we&#039;ll add a &amp;lt;code&amp;gt;build.rs&amp;lt;/code&amp;gt; file to the root of the crate with the following code (abridged to show the conversion of only one texture):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot;&amp;gt;&lt;br /&gt;
fn main() {&lt;br /&gt;
    let kos_base = std::env::var(&amp;quot;KOS_BASE&amp;quot;).expect(&amp;quot;Missing $KOS_BASE -- KallistiOS environment not sourced!&amp;quot;);&lt;br /&gt;
    let vqenc_cmd = kos_base + &amp;quot;/utils/vqenc/vqenc&amp;quot;;&lt;br /&gt;
    Command::new(&amp;amp;vqenc_cmd)&lt;br /&gt;
        .arg(&amp;quot;-t&amp;quot;)&lt;br /&gt;
        .arg(&amp;quot;-v&amp;quot;)&lt;br /&gt;
        .arg(&amp;quot;rsrc/tex_claw.jpg&amp;quot;)&lt;br /&gt;
        .output()&lt;br /&gt;
        .expect(&amp;quot;vqenc on tex_claw.jpg failed!&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
The texture files are then included in our project using the &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;include_bytes!&amp;lt;/syntaxhighlight&amp;gt; macro.&lt;br /&gt;
&lt;br /&gt;
We will need to link the GLdc library in this example, but we don&#039;t need to do anything special to add the paths to this library, because adding the common paths to KallistiOS libraries is already done for us in the kos-rs crate. However, if in your project you need to include a separate unique library path, you can add it in your &amp;lt;code&amp;gt;build.rs&amp;lt;/code&amp;gt; like so:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot;&amp;gt;&lt;br /&gt;
println!(&amp;quot;cargo:rustc-link-search=native={}&amp;quot;, lib_path);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The workings of this example&#039;s source code are too great to detail here line-by-line, but the example demonstrates how to use GLdc via the [https://github.com/dreamcast-rs/gldc-sys gldc-sys] crate, which contains unsafe/raw bindings to GLdc. Check out this crate&#039;s source code for an example of declaring and binding external C functions, constants, and structures so they can be used in Rust code. Since the entirety of the gldc-cube example uses C FFI functions without safe wrappers, the &amp;lt;code&amp;gt;main()&amp;lt;/code&amp;gt; source is wrapped in &amp;lt;code&amp;gt;unsafe {}&amp;lt;/code&amp;gt;. In the future, this would be much less necessary as higher level safe Rust bindings to KallistiOS and other libraries become mature.&lt;br /&gt;
&lt;br /&gt;
==Creating a Rust library==&lt;br /&gt;
Next, we&#039;ll demonstrate creating a Rust library with &amp;lt;code&amp;gt;kos-cargo&amp;lt;/code&amp;gt; that can be included in other Dreamcast code. This will follow the [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/addlib &amp;lt;code&amp;gt;addlib&amp;lt;/code&amp;gt;] example. Once again, this project&#039;s initial setup is done the same as the above &amp;lt;code&amp;gt;hello&amp;lt;/code&amp;gt; example, but you&#039;ll create the new project using &amp;lt;code&amp;gt;kos-cargo new --lib addlib&amp;lt;/code&amp;gt; to specify that we&#039;re creating a library named &amp;lt;code&amp;gt;addlib&amp;lt;/code&amp;gt;. You&#039;ll also need to add the following text to this project&#039;s &amp;lt;code&amp;gt;Cargo.toml&amp;lt;/code&amp;gt; file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;toml&amp;quot;&amp;gt;&lt;br /&gt;
[lib]&lt;br /&gt;
crate-type = [&amp;quot;staticlib&amp;quot;]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This tells Rust to build a static &amp;lt;code&amp;gt;.a&amp;lt;/code&amp;gt; library archive file from our code, which is located in &amp;lt;code&amp;gt;src/lib.rs&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; line&amp;gt;&lt;br /&gt;
#[no_mangle]&lt;br /&gt;
pub extern &amp;quot;C&amp;quot; fn print_added(a: isize, b: isize) {&lt;br /&gt;
    print!(&amp;quot;{}&amp;quot;, a + b);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
#[no_mangle]&lt;br /&gt;
pub extern &amp;quot;C&amp;quot; fn add_integers(a: isize, b: isize) -&amp;gt; isize {&lt;br /&gt;
    a + b&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The source code here starts similarly to the &amp;quot;Hello, world!&amp;quot; example, except we don&#039;t need to specify &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;#![no_main]&amp;lt;/syntaxhighlight&amp;gt; as this is a library which wouldn&#039;t have a &amp;lt;code&amp;gt;main()&amp;lt;/code&amp;gt; function anyway.&lt;br /&gt;
&lt;br /&gt;
Two simple functions are provided: one for adding two integers and returning the result, and another for adding two integers and printing the result as text. Because these functions use &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;#[no_mangle]&amp;lt;/syntaxhighlight&amp;gt; and are declared &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;extern &amp;quot;C&amp;quot;&amp;lt;/syntaxhighlight&amp;gt;, they can be called by name in C code that links this library.&lt;br /&gt;
&lt;br /&gt;
When built using &amp;lt;code&amp;gt;kos-cargo build&amp;lt;/code&amp;gt;, a &amp;lt;code&amp;gt;target/sh-elf/debug/libaddlib.a&amp;lt;/code&amp;gt; file will be generated. This can be linked into other projects to gain the use of these functions.&lt;br /&gt;
&lt;br /&gt;
For example, this can be added to a standard &amp;lt;code&amp;gt;Makefile&amp;lt;/code&amp;gt;-based KallistiOS project by editing the &amp;lt;code&amp;gt;Makefile&amp;lt;/code&amp;gt;:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;make&amp;quot;&amp;gt;&lt;br /&gt;
$(TARGET): $(OBJS)&lt;br /&gt;
	kos-cc -o $(TARGET) $(OBJS) -L/opt/toolchains/dc/rust/examples/cargo-addlib/target/sh-elf/debug -laddlib&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then, we can use the code in our C source:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/* Declare the external function from the Rust library */&lt;br /&gt;
int add_integers(int a, int b);&lt;br /&gt;
&lt;br /&gt;
/* Use the function */&lt;br /&gt;
printf(&amp;quot;Five plus six is %d\n&amp;quot;, add_integers(5, 6));&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Compiling individual modules into object files with rustc==&lt;br /&gt;
If we&#039;d like to mix C and Rust code in the same &amp;lt;code&amp;gt;Makefile&amp;lt;/code&amp;gt;-based KallistiOS project without building an entirely separate library, we can do that as well. This is demonstrated in the [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/no_std/hello-make &amp;lt;code&amp;gt;hello-make&amp;lt;/code&amp;gt;] example.&lt;br /&gt;
&lt;br /&gt;
Instead of using &amp;lt;code&amp;gt;kos-cargo&amp;lt;/code&amp;gt;, we can invoke the &amp;lt;code&amp;gt;kos-rustc&amp;lt;/code&amp;gt; via a &amp;lt;code&amp;gt;Makefile&amp;lt;/code&amp;gt; to build Rust modules. If we assume the Rust file is named &amp;lt;code&amp;gt;example.rs&amp;lt;/code&amp;gt;, you&#039;ll need to add &amp;lt;code&amp;gt;example.o&amp;lt;/code&amp;gt; as an object file in your &amp;lt;code&amp;gt;Makefile&amp;lt;/code&amp;gt;&#039;s &amp;lt;code&amp;gt;OBJS =&amp;lt;/code&amp;gt; declaration. For example, if the project has two source files &amp;lt;code&amp;gt;hello_c.c&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;hello_rust.rs&amp;lt;/code&amp;gt;, our &amp;lt;code&amp;gt;Makefile&amp;lt;/code&amp;gt; would have a line like this:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;make&amp;quot;&amp;gt;&lt;br /&gt;
OBJS = hello_c.o hello_rust.o&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The example code demonstrates starting a C &amp;lt;code&amp;gt;main()&amp;lt;/code&amp;gt; function to call a Rust function which builds a &amp;lt;code&amp;gt;String&amp;lt;/code&amp;gt; containing the &amp;quot;Hello, world!&amp;quot; text which is passed back to a C function which prints &amp;lt;code&amp;gt;String&amp;lt;/code&amp;gt;s.&lt;br /&gt;
&lt;br /&gt;
==Using the libc crate with KallistiOS==&lt;br /&gt;
If any of the crates in your project&#039;s dependency tree requires the &#039;&#039;&#039;libc&#039;&#039;&#039; crate, you will need to override fetching the default crate with our custom libc crate with KallistiOS/Dreamcast definitions. This custom version was copied to &amp;lt;code&amp;gt;/opt/toolchains/dc/rust/libc&amp;lt;/code&amp;gt; when building the Rust sysroot earlier. Add the following text to your &amp;lt;code&amp;gt;Cargo.toml&amp;lt;/code&amp;gt;:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;toml&amp;quot;&amp;gt;&lt;br /&gt;
[patch.crates-io]&lt;br /&gt;
libc = { path = &amp;quot;/opt/toolchains/dc/rust/libc&amp;quot; }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Adjusting build flags==&lt;br /&gt;
Build settings can be adjusted through the &amp;lt;code&amp;gt;KOS_RCG_RUSTFLAGS&amp;lt;/code&amp;gt; variable in the &amp;lt;code&amp;gt;environ.sh&amp;lt;/code&amp;gt; file under the &amp;lt;code&amp;gt;### Rust Flags&amp;lt;/code&amp;gt; section. &lt;br /&gt;
* To pass arguments to the &#039;&#039;&#039;rustc&#039;&#039;&#039; frontend, e.g. to disable debug assertions:&lt;br /&gt;
** &amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot; inline&amp;gt;export KOS_RCG_RUSTFLAGS=&amp;quot;${KOS_RCG_RUSTFLAGS} -Cdebug-assertions=no&amp;quot;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* To pass arguments to the GCC backend, e.g. to enable the &amp;lt;code&amp;gt;-freorder-blocks-algorithm=simple&amp;lt;/code&amp;gt; optimization for Rust code:&lt;br /&gt;
** &amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot; inline&amp;gt;export KOS_RCG_RUSTFLAGS=&amp;quot;${KOS_RCG_RUSTFLAGS} -Cllvm-args=-freorder-blocks-algorithm=simple&amp;quot;&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Darc</name></author>
	</entry>
	<entry>
		<id>https://dreamcast.wiki/wiki/index.php?title=Rust_on_Dreamcast&amp;diff=3639</id>
		<title>Rust on Dreamcast</title>
		<link rel="alternate" type="text/html" href="https://dreamcast.wiki/wiki/index.php?title=Rust_on_Dreamcast&amp;diff=3639"/>
		<updated>2024-11-14T01:06:44Z</updated>

		<summary type="html">&lt;p&gt;Darc: /* Current Status */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[File:Rust-dc-logo.png|thumb|Ferris holding his Dreamcast controller]]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Rust&#039;&#039;&#039; is a popular modern programming language focusing on performance, memory safety, and program correctness. As a &amp;quot;blazingly fast&amp;quot; low-level language, it is viable for running on a Dreamcast console. Doing so presents a bit of a challenge, however, as the official Rust compiler is based on the [https://llvm.org/ LLVM] toolchain infrastructure, which does not support the Dreamcast CPU&#039;s SuperH architecture. Dreamcast programming is instead typically done with [https://gcc.gnu.org/ GCC], the GNU Compiler Collection. There are currently two viable solutions to this challenge:&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;rustc_codegen_gcc&#039;&#039;&#039;, the current preferred method: An experimental codegen backend for the official Rust &#039;&#039;&#039;rustc&#039;&#039;&#039; compiler which interfaces with GCC&#039;s libgccjit API&lt;br /&gt;
* &#039;&#039;&#039;gccrs&#039;&#039;&#039;: an experimental Rust frontend for GCC&lt;br /&gt;
&lt;br /&gt;
Neither solution is complete at this time, but &#039;&#039;&#039;rustc_codegen_gcc&#039;&#039;&#039; is much further along and is quite usable with some patience with its current limitations. On the other hand, &#039;&#039;&#039;gccrs&#039;&#039;&#039; can compile for Dreamcast, but is in an earlier state. Below we will focus on using &#039;&#039;&#039;rustc_codegen_gcc&#039;&#039;&#039;. For more information on using gccrs, see the [[gccrs]] page.&lt;br /&gt;
&lt;br /&gt;
=Current Status=&lt;br /&gt;
* The full &#039;&#039;&#039;std&#039;&#039;&#039; library is supported.&lt;br /&gt;
* A [https://github.com/dreamcast-rs/KallistiOS custom version of KallistiOS] v2.1.0 (latest stable release) is used.&lt;br /&gt;
** This version includes minor changes for Rust and a beta libpthread addon layer to interface with the Rust standard library.&lt;br /&gt;
** Wrappers are provided to invoke &#039;&#039;&#039;cargo&#039;&#039;&#039; and &#039;&#039;&#039;rustc&#039;&#039;&#039; in a familiar manner to compile projects.&lt;br /&gt;
* A [https://github.com/dreamcast-rs/kos-sys &#039;&#039;&#039;kos-sys&#039;&#039;&#039;] raw/unsafe bindings crate is provided for easily linking KallistiOS and its unsafe C API into Rust crates. This crate is in active development, but with a large portion of KallistiOS functionality available now via C FFI.&lt;br /&gt;
* A [https://github.com/dreamcast-rs/kos-rs &#039;&#039;&#039;kos-rs&#039;&#039;&#039;] Rust crate is available but is also in early stages.&lt;br /&gt;
** Documentation for the bindings is available at [https://kos-rs.dreamcast.wiki/ kos-rs.dreamcast.wiki].&lt;br /&gt;
** If bindings do not yet exist for desired functionality, wrapping of KallistiOS C functions is required to get proper use of KallistiOS at this time. See the [https://doc.rust-lang.org/nomicon/ffi.html Foreign Function Interface page] in the Rustonomicon for further understanding. &lt;br /&gt;
** A custom [https://github.com/dreamcast-rs/libc/tree/libc-0.2-kos &#039;&#039;&#039;libc&#039;&#039;&#039; crate with KallistiOS support] is provided.&lt;br /&gt;
* Code generation uses &#039;&#039;&#039;rustc_codegen_gcc&#039;&#039;&#039;, so its limitations apply here. See the [https://blog.antoyo.xyz/ development blog] for more information on its progress.&lt;br /&gt;
** rustc_codegen_gcc is pinned to a particular Rust nightly version, but is synced regularly. The current version in use is the &#039;&#039;&#039;2024-08-10&#039;&#039;&#039; nightly.&lt;br /&gt;
** A [https://github.com/dreamcast-rs/rust/tree/kos-2024-08-10 KallistiOS-patched version of the latest nightly] is maintained and updated regularly. &lt;br /&gt;
** Panic unwinding, debug info, LTO, etc. are not currently available.&lt;br /&gt;
** Architectures dependent on rustc_codegen_gcc are not yet able to be added to the rustc frontend, so a workaround is necessary: MIPS is falsely selected for KallistiOS, causing rustc to emit objects with a MIPS header and a provided wrapper is used when linking to rewrite these MIPS headers to SuperH before passing the compiled objects to the linker.&lt;br /&gt;
* A separate [https://github.com/rust-lang/gcc development version of the GCC toolchain] must be used.&lt;br /&gt;
** This version of GCC contains the latest libgccjit patches necessary to interface with rustc_codegen_gcc, and will be built to work with the custom KallistiOS&#039;s libpthread layer.&lt;br /&gt;
** New updates to rustc_codegen_gcc often require new updates to GCC, so recompiling GCC regularly will be necessary to keep up with the latest updates.&lt;br /&gt;
** The development version of GCC does not always compile well on every platform. Using a modern Linux platform is recommended for maximum compatibility.&lt;br /&gt;
** The custom KallistiOS and GCC environment can still be used to compile code or projects written in C, C++, etc.&lt;br /&gt;
* KallistiOS and all related libraries and tools must be compiled using the &amp;lt;code&amp;gt;-m4-single&amp;lt;/code&amp;gt; ABI. This is already set up for you in the guide below, but if external libraries are being linked to Rust projects, make sure that code is compiled using the proper ABI.&lt;br /&gt;
&lt;br /&gt;
=Setting up a Rust environment for Dreamcast development=&lt;br /&gt;
The following guide will provide instructions for setting up a Rust development environment for Dreamcast. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;If you run into any errors or other challenges while following this tutorial, or simply need clarification on any of the steps, feel free to ask for assistance on the [https://dcemulation.org/phpBB/viewforum.php?f=29 message board] or [https://discord.gg/wDEfkUh63h our Discord server] and we would be happy to aid you and update the guide for the benefit of future readers and others in the community.&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
==Prerequisites==&lt;br /&gt;
First, we will need to set up the prerequisites.&lt;br /&gt;
&lt;br /&gt;
* You must have the [https://rustup.rs/ rustup] tool installed for your operating system to manage Rust toolchain installations. It will automatically manage the latest Rust nightly installations for you. You cannot use a Rust toolchain that may be provided by your operating system vendor.&lt;br /&gt;
* Install the dependency packages for your operating system. These are listed in the &#039;&#039;&#039;Dependencies&#039;&#039;&#039; section on the [[Getting Started with Dreamcast development#Dependencies]] page, with lists provided for most common operating systems.&lt;br /&gt;
* We will install our custom Rust toolchain environment within &amp;lt;code&amp;gt;/opt/toolchains/dc/rust&amp;lt;/code&amp;gt;. If it does not already exist, create the &amp;lt;code&amp;gt;/opt/toolchains/dc&amp;lt;/code&amp;gt; directory and grant it proper permissions using the following commands. &lt;br /&gt;
 sudo mkdir -p /opt/toolchains/dc&lt;br /&gt;
 sudo chmod -R 755 /opt/toolchains/dc&lt;br /&gt;
 sudo chown -R $(id -u):$(id -g) /opt/toolchains/dc&lt;br /&gt;
* Clone the [https://github.com/dreamcast-rs/rust-for-dreamcast/ Rust for Dreamcast] repo containing necessary support files to &amp;lt;code&amp;gt;/opt/toolchains/dc/rust&amp;lt;/code&amp;gt;:&lt;br /&gt;
 git clone https://github.com/dreamcast-rs/rust-for-dreamcast.git /opt/toolchains/dc/rust&lt;br /&gt;
&lt;br /&gt;
==Installing our custom KallistiOS and GCC toolchain==&lt;br /&gt;
Clone the git repository for our custom Rust-patched version of KallistiOS v2.1.0 stable:&lt;br /&gt;
 git clone https://github.com/dreamcast-rs/KallistiOS /opt/toolchains/dc/rust/kos&lt;br /&gt;
&lt;br /&gt;
Enter the dc-chain directory:&lt;br /&gt;
 cd /opt/toolchains/dc/rust/kos/utils/dc-chain&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;Makefile.default.cfg&amp;lt;/code&amp;gt; file in this directory has been customized for you, selecting the proper toolchain profile, paths, and build options necessary for building a Rust toolchain using this guide. It is not recommended to adjust the defaults.&lt;br /&gt;
&lt;br /&gt;
To build the GCC toolchain using 2 CPU cores, simply type:&lt;br /&gt;
 make makejobs=2&lt;br /&gt;
Adjust the &amp;lt;code&amp;gt;makejobs=...&amp;lt;/code&amp;gt; value to use the number of CPU cores available to you on your system in order to speed up compilation. This may take a while to build, so be patient.&lt;br /&gt;
&lt;br /&gt;
Once the GCC toolchain is built, source the &amp;lt;code&amp;gt;environ.sh&amp;lt;/code&amp;gt; KallistiOS environment settings. A custom &amp;lt;code&amp;gt;environ.sh&amp;lt;/code&amp;gt; file has been provided for you, containing settings pre-adjusted for Rust development using this guide.&lt;br /&gt;
&lt;br /&gt;
You will need to run the source command to apply the KallistiOS environment settings to your currently running shell. Run the following now, &#039;&#039;&#039;and&#039;&#039;&#039; &#039;&#039;whenever&#039;&#039; you open a new shell to work on Dreamcast projects:&lt;br /&gt;
 source /opt/toolchains/dc/rust/misc/environ.sh&lt;br /&gt;
&lt;br /&gt;
Enter the KallistiOS directory:&lt;br /&gt;
 cd /opt/toolchains/dc/rust/kos&lt;br /&gt;
&lt;br /&gt;
Build KallistiOS:&lt;br /&gt;
 make&lt;br /&gt;
&lt;br /&gt;
==Building rustc_codegen_gcc and Rust sysroot==&lt;br /&gt;
Now that we have a working KallistiOS environment suitable for Rust development set up, we can build the Rust compiler compiler and sysroot components. A script is provided which will download the necessary components from the respective repositories and compile them for you. Run the installer script:&lt;br /&gt;
&lt;br /&gt;
 /opt/toolchains/dc/rust/misc/install-rust.sh&lt;br /&gt;
&lt;br /&gt;
If all goes well, you&#039;ll see &#039;&#039;&#039;Rust for KallistiOS/Dreamcast installed!&#039;&#039;&#039; message!&lt;br /&gt;
&lt;br /&gt;
==Building KOS ports libraries==&lt;br /&gt;
If desired, you may also built the KOS ports collection of libraries for use with your Rust environment. It is recommended to maintain a separate KOS ports installation for linking with Rust projects in &amp;lt;code&amp;gt;/opt/toolchains/dc/rust/kos-ports&amp;lt;/code&amp;gt;. This is because the Rust environment is using the &amp;lt;code&amp;gt;-m4-single&amp;lt;/code&amp;gt; ABI, but the KallistiOS default installation will be using the &amp;lt;code&amp;gt;-m4-single-only&amp;lt;/code&amp;gt;, which cannot be mixed and matched.&lt;br /&gt;
&lt;br /&gt;
Clone the kos-ports repository to your system:&lt;br /&gt;
 git clone https://github.com/KallistiOS/kos-ports /opt/toolchains/dc/rust/kos-ports&lt;br /&gt;
An individual port can be built and installed by entering its directory and running the proper command. For example, installing GLdc:&lt;br /&gt;
 cd /opt/toolchains/dc/rust/kos-ports/libGL&lt;br /&gt;
 make install&lt;br /&gt;
Or, instead, you may run the script to build all of the included ports:&lt;br /&gt;
 /opt/toolchains/dc/rust/kos-ports/utils/build-all.sh&lt;br /&gt;
&lt;br /&gt;
=Using Rust for Dreamcast=&lt;br /&gt;
If all went well, you will now have a working Rust for Dreamcast environment!&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;cargo&#039;&#039;&#039; can be invoked to target Dreamcast using the &amp;lt;code&amp;gt;kos-cargo&amp;lt;/code&amp;gt; command.&lt;br /&gt;
** &amp;lt;code&amp;gt;kos-cargo run&amp;lt;/code&amp;gt; can be used to build and run code on a Dreamcast console using [[dcload-ip]] or [[dcload-serial]]. You will need to set up the &amp;lt;code&amp;gt;KOS_LOADER&amp;lt;/code&amp;gt; variable with the necessary command in the &amp;lt;code&amp;gt;/opt/toolchains/dc/rust/misc/environ.sh&amp;lt;/code&amp;gt; file (and then re-source the file for the change to take effect).&lt;br /&gt;
* &#039;&#039;&#039;rustc&#039;&#039;&#039; can be invoked to target Dreamcast using the &amp;lt;code&amp;gt;kos-rustc&amp;lt;/code&amp;gt; command.&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
Examples are included with the Rust for Dreamcast repo to help you get started.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/hello-cargo &amp;lt;code&amp;gt;hello-cargo&amp;lt;/code&amp;gt;] demonstrates how to create a simple &amp;quot;Hello, world!&amp;quot; application using cargo.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/hello-make &amp;lt;code&amp;gt;hello-make&amp;lt;/code&amp;gt;] demonstrates how to create a simple &amp;quot;Hello, world!&amp;quot; application using standard Makefiles.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/gldc-cube &amp;lt;code&amp;gt;gldc-cube&amp;lt;/code&amp;gt;] demonstrates a Rust project using the GLdc KOS port.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/addlib &amp;lt;code&amp;gt;addlib&amp;lt;/code&amp;gt;] demonstrates how to create a Rust library that can be included with a KallistiOS project.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/atomics &amp;lt;code&amp;gt;atomics&amp;lt;/code&amp;gt;] demonstrates the use of atomic functions.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/filesystem-io &amp;lt;code&amp;gt;filesystem-io&amp;lt;/code&amp;gt;] demonstrates listing directory contents and opening files.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/network-time &amp;lt;code&amp;gt;network-time&amp;lt;/code&amp;gt;] demonstrates displaying the system time and retrieving the current UTC time from an NTP server using the [[Broadband adapter]] or [[LAN adapter]].&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/threads &amp;lt;code&amp;gt;threads&amp;lt;/code&amp;gt;] demonstrates the use of threads.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/tokio-async &amp;lt;code&amp;gt;tokio-async&amp;lt;/code&amp;gt;] demonstrates the use of the &#039;&#039;&#039;tokio&#039;&#039;&#039; async runtime.&lt;br /&gt;
&lt;br /&gt;
==Creating a new Rust project with Cargo==&lt;br /&gt;
First, we&#039;ll demonstrate creating a new &amp;quot;Hello, world!&amp;quot; project with &amp;lt;code&amp;gt;kos-cargo&amp;lt;/code&amp;gt;. This will follow the [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/hello-cargo &amp;lt;code&amp;gt;cargo-hello&amp;lt;/code&amp;gt;] example included in the Rust-for-Dreamcast repo. &lt;br /&gt;
&lt;br /&gt;
In a directory of your choosing, let&#039;s invoke &amp;lt;code&amp;gt;kos-cargo&amp;lt;/code&amp;gt; to create a new project and then enter the directory:&lt;br /&gt;
 kos-cargo new hello&lt;br /&gt;
 cd hello&lt;br /&gt;
&lt;br /&gt;
Let&#039;s add our kos-rs crate to gain access to current KallistiOS bindings. Open &amp;lt;code&amp;gt;Cargo.toml&amp;lt;/code&amp;gt; in your text editor and add:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;toml&amp;quot;&amp;gt;&lt;br /&gt;
[dependencies]&lt;br /&gt;
kos = { package = &amp;quot;kos-rs&amp;quot;, git = &amp;quot;https://github.com/dreamcast-rs/kos-rs&amp;quot; }&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we can open up &amp;lt;code&amp;gt;src/main.rs&amp;lt;/code&amp;gt; and write our &amp;quot;Hello, world!&amp;quot; example code:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; line&amp;gt;&lt;br /&gt;
#![no_main]&lt;br /&gt;
&lt;br /&gt;
#[no_mangle]&lt;br /&gt;
extern &amp;quot;C&amp;quot; fn main(_argc: isize, _argv: *const *const u8) -&amp;gt; isize {&lt;br /&gt;
    println!(&amp;quot;Hello, world from Rust!&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
    0&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;#![no_main]&amp;lt;/syntaxhighlight&amp;gt; tells Rust that we will not have Rust use a &amp;lt;code&amp;gt;main&amp;lt;/code&amp;gt; function as an entry point -- this will be handled by KallistiOS.&lt;br /&gt;
* &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;#[no_mangle]&amp;lt;/syntaxhighlight&amp;gt; tells Rust to disable name mangling so that the &amp;lt;code&amp;gt;main&amp;lt;/code&amp;gt; function can be used by KallistiOS&#039;s C code.&lt;br /&gt;
* Finally, we have a &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;fn main&amp;lt;/syntaxhighlight&amp;gt; with the function signature of a typical C &amp;lt;code&amp;gt;main&amp;lt;/code&amp;gt; function, containing a basic &amp;quot;Hello, world!&amp;quot; exclamation.&lt;br /&gt;
&lt;br /&gt;
Now we can use &amp;lt;code&amp;gt;kos-cargo build&amp;lt;/code&amp;gt; to build our project. If all goes well, there will be a &amp;lt;code&amp;gt;target/sh-elf/debug/hello.elf&amp;lt;/code&amp;gt; file that can be sent to the Dreamcast with &amp;lt;code&amp;gt;dc-tool&amp;lt;/code&amp;gt;.&lt;br /&gt;
If you have &amp;lt;code&amp;gt;KOS_LOADER&amp;lt;/code&amp;gt; set in the &amp;lt;code&amp;gt;environ.sh&amp;lt;/code&amp;gt; file, you can invoke it directly with &amp;lt;code&amp;gt;kos-cargo run&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Creating a Rust project using kos-ports libraries==&lt;br /&gt;
[[File:Rust-Cube rustc codegen-gcc demo.gif|thumb|cube example in action]]&lt;br /&gt;
The [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/gldc-cube &amp;lt;code&amp;gt;gldc-cube&amp;lt;/code&amp;gt;] example demonstrates creating a rotating 3D cube using Rust as the primary language, while calling C functions provided by the &#039;&#039;&#039;GLdc&#039;&#039;&#039; library available in kos-ports. This project&#039;s initial setup is done the same as the above &amp;lt;code&amp;gt;hello&amp;lt;/code&amp;gt; example.&lt;br /&gt;
&lt;br /&gt;
For this example, we&#039;ll need to convert JPEG textures to VQ-encoded textures at compile time using the &amp;lt;code&amp;gt;vqenc&amp;lt;/code&amp;gt; utility includes with KallistiOS. To do this, we&#039;ll add a &amp;lt;code&amp;gt;build.rs&amp;lt;/code&amp;gt; file to the root of the crate with the following code (abridged to show the conversion of only one texture):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot;&amp;gt;&lt;br /&gt;
fn main() {&lt;br /&gt;
    let kos_base = std::env::var(&amp;quot;KOS_BASE&amp;quot;).expect(&amp;quot;Missing $KOS_BASE -- KallistiOS environment not sourced!&amp;quot;);&lt;br /&gt;
    let vqenc_cmd = kos_base + &amp;quot;/utils/vqenc/vqenc&amp;quot;;&lt;br /&gt;
    Command::new(&amp;amp;vqenc_cmd)&lt;br /&gt;
        .arg(&amp;quot;-t&amp;quot;)&lt;br /&gt;
        .arg(&amp;quot;-v&amp;quot;)&lt;br /&gt;
        .arg(&amp;quot;rsrc/tex_claw.jpg&amp;quot;)&lt;br /&gt;
        .output()&lt;br /&gt;
        .expect(&amp;quot;vqenc on tex_claw.jpg failed!&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
The texture files are then included in our project using the &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;include_bytes!&amp;lt;/syntaxhighlight&amp;gt; macro.&lt;br /&gt;
&lt;br /&gt;
We will need to link the GLdc library in this example, but we don&#039;t need to do anything special to add the paths to this library, because adding the common paths to KallistiOS libraries is already done for us in the kos-rs crate. However, if in your project you need to include a separate unique library path, you can add it in your &amp;lt;code&amp;gt;build.rs&amp;lt;/code&amp;gt; like so:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot;&amp;gt;&lt;br /&gt;
println!(&amp;quot;cargo:rustc-link-search=native={}&amp;quot;, lib_path);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The workings of this example&#039;s source code are too great to detail here line-by-line, but the example demonstrates how to use GLdc via the [https://github.com/dreamcast-rs/gldc-sys gldc-sys] crate, which contains unsafe/raw bindings to GLdc. Check out this crate&#039;s source code for an example of declaring and binding external C functions, constants, and structures so they can be used in Rust code. Since the entirety of the gldc-cube example uses C FFI functions without safe wrappers, the &amp;lt;code&amp;gt;main()&amp;lt;/code&amp;gt; source is wrapped in &amp;lt;code&amp;gt;unsafe {}&amp;lt;/code&amp;gt;. In the future, this would be much less necessary as higher level safe Rust bindings to KallistiOS and other libraries become mature.&lt;br /&gt;
&lt;br /&gt;
==Creating a Rust library==&lt;br /&gt;
Next, we&#039;ll demonstrate creating a Rust library with &amp;lt;code&amp;gt;kos-cargo&amp;lt;/code&amp;gt; that can be included in other Dreamcast code. This will follow the [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/addlib &amp;lt;code&amp;gt;addlib&amp;lt;/code&amp;gt;] example. Once again, this project&#039;s initial setup is done the same as the above &amp;lt;code&amp;gt;hello&amp;lt;/code&amp;gt; example, but you&#039;ll create the new project using &amp;lt;code&amp;gt;kos-cargo new --lib addlib&amp;lt;/code&amp;gt; to specify that we&#039;re creating a library named &amp;lt;code&amp;gt;addlib&amp;lt;/code&amp;gt;. You&#039;ll also need to add the following text to this project&#039;s &amp;lt;code&amp;gt;Cargo.toml&amp;lt;/code&amp;gt; file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;toml&amp;quot;&amp;gt;&lt;br /&gt;
[lib]&lt;br /&gt;
crate-type = [&amp;quot;staticlib&amp;quot;]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This tells Rust to build a static &amp;lt;code&amp;gt;.a&amp;lt;/code&amp;gt; library archive file from our code, which is located in &amp;lt;code&amp;gt;src/lib.rs&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; line&amp;gt;&lt;br /&gt;
#[no_mangle]&lt;br /&gt;
pub extern &amp;quot;C&amp;quot; fn print_added(a: isize, b: isize) {&lt;br /&gt;
    print!(&amp;quot;{}&amp;quot;, a + b);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
#[no_mangle]&lt;br /&gt;
pub extern &amp;quot;C&amp;quot; fn add_integers(a: isize, b: isize) -&amp;gt; isize {&lt;br /&gt;
    a + b&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The source code here starts similarly to the &amp;quot;Hello, world!&amp;quot; example, except we don&#039;t need to specify &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;#![no_main]&amp;lt;/syntaxhighlight&amp;gt; as this is a library which wouldn&#039;t have a &amp;lt;code&amp;gt;main()&amp;lt;/code&amp;gt; function anyway.&lt;br /&gt;
&lt;br /&gt;
Two simple functions are provided: one for adding two integers and returning the result, and another for adding two integers and printing the result as text. Because these functions use &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;#[no_mangle]&amp;lt;/syntaxhighlight&amp;gt; and are declared &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;extern &amp;quot;C&amp;quot;&amp;lt;/syntaxhighlight&amp;gt;, they can be called by name in C code that links this library.&lt;br /&gt;
&lt;br /&gt;
When built using &amp;lt;code&amp;gt;kos-cargo build&amp;lt;/code&amp;gt;, a &amp;lt;code&amp;gt;target/sh-elf/debug/libaddlib.a&amp;lt;/code&amp;gt; file will be generated. This can be linked into other projects to gain the use of these functions.&lt;br /&gt;
&lt;br /&gt;
For example, this can be added to a standard &amp;lt;code&amp;gt;Makefile&amp;lt;/code&amp;gt;-based KallistiOS project by editing the &amp;lt;code&amp;gt;Makefile&amp;lt;/code&amp;gt;:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;make&amp;quot;&amp;gt;&lt;br /&gt;
$(TARGET): $(OBJS)&lt;br /&gt;
	kos-cc -o $(TARGET) $(OBJS) -L/opt/toolchains/dc/rust/examples/cargo-addlib/target/sh-elf/debug -laddlib&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then, we can use the code in our C source:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/* Declare the external function from the Rust library */&lt;br /&gt;
int add_integers(int a, int b);&lt;br /&gt;
&lt;br /&gt;
/* Use the function */&lt;br /&gt;
printf(&amp;quot;Five plus six is %d\n&amp;quot;, add_integers(5, 6));&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Compiling individual modules into object files with rustc==&lt;br /&gt;
If we&#039;d like to mix C and Rust code in the same &amp;lt;code&amp;gt;Makefile&amp;lt;/code&amp;gt;-based KallistiOS project without building an entirely separate library, we can do that as well. This is demonstrated in the [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/no_std/hello-make &amp;lt;code&amp;gt;hello-make&amp;lt;/code&amp;gt;] example.&lt;br /&gt;
&lt;br /&gt;
Instead of using &amp;lt;code&amp;gt;kos-cargo&amp;lt;/code&amp;gt;, we can invoke the &amp;lt;code&amp;gt;kos-rustc&amp;lt;/code&amp;gt; via a &amp;lt;code&amp;gt;Makefile&amp;lt;/code&amp;gt; to build Rust modules. If we assume the Rust file is named &amp;lt;code&amp;gt;example.rs&amp;lt;/code&amp;gt;, you&#039;ll need to add &amp;lt;code&amp;gt;example.o&amp;lt;/code&amp;gt; as an object file in your &amp;lt;code&amp;gt;Makefile&amp;lt;/code&amp;gt;&#039;s &amp;lt;code&amp;gt;OBJS =&amp;lt;/code&amp;gt; declaration. For example, if the project has two source files &amp;lt;code&amp;gt;hello_c.c&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;hello_rust.rs&amp;lt;/code&amp;gt;, our &amp;lt;code&amp;gt;Makefile&amp;lt;/code&amp;gt; would have a line like this:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;make&amp;quot;&amp;gt;&lt;br /&gt;
OBJS = hello_c.o hello_rust.o&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The example code demonstrates starting a C &amp;lt;code&amp;gt;main()&amp;lt;/code&amp;gt; function to call a Rust function which builds a &amp;lt;code&amp;gt;String&amp;lt;/code&amp;gt; containing the &amp;quot;Hello, world!&amp;quot; text which is passed back to a C function which prints &amp;lt;code&amp;gt;String&amp;lt;/code&amp;gt;s.&lt;br /&gt;
&lt;br /&gt;
==Using the libc crate with KallistiOS==&lt;br /&gt;
If any of the crates in your project&#039;s dependency tree requires the &#039;&#039;&#039;libc&#039;&#039;&#039; crate, you will need to override fetching the default crate with our custom libc crate with KallistiOS/Dreamcast definitions. This custom version was copied to &amp;lt;code&amp;gt;/opt/toolchains/dc/rust/libc&amp;lt;/code&amp;gt; when building the Rust sysroot earlier. Add the following text to your &amp;lt;code&amp;gt;Cargo.toml&amp;lt;/code&amp;gt;:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;toml&amp;quot;&amp;gt;&lt;br /&gt;
[patch.crates-io]&lt;br /&gt;
libc = { path = &amp;quot;/opt/toolchains/dc/rust/libc&amp;quot; }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Adjusting build flags==&lt;br /&gt;
Build settings can be adjusted through the &amp;lt;code&amp;gt;KOS_RCG_RUSTFLAGS&amp;lt;/code&amp;gt; variable in the &amp;lt;code&amp;gt;environ.sh&amp;lt;/code&amp;gt; file under the &amp;lt;code&amp;gt;### Rust Flags&amp;lt;/code&amp;gt; section. &lt;br /&gt;
* To pass arguments to the &#039;&#039;&#039;rustc&#039;&#039;&#039; frontend, e.g. to disable debug assertions:&lt;br /&gt;
** &amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot; inline&amp;gt;export KOS_RCG_RUSTFLAGS=&amp;quot;${KOS_RCG_RUSTFLAGS} -Cdebug-assertions=no&amp;quot;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* To pass arguments to the GCC backend, e.g. to enable the &amp;lt;code&amp;gt;-freorder-blocks-algorithm=simple&amp;lt;/code&amp;gt; optimization for Rust code:&lt;br /&gt;
** &amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot; inline&amp;gt;export KOS_RCG_RUSTFLAGS=&amp;quot;${KOS_RCG_RUSTFLAGS} -Cllvm-args=-freorder-blocks-algorithm=simple&amp;quot;&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Darc</name></author>
	</entry>
	<entry>
		<id>https://dreamcast.wiki/wiki/index.php?title=Getting_Started_with_Dreamcast_development&amp;diff=3638</id>
		<title>Getting Started with Dreamcast development</title>
		<link rel="alternate" type="text/html" href="https://dreamcast.wiki/wiki/index.php?title=Getting_Started_with_Dreamcast_development&amp;diff=3638"/>
		<updated>2024-11-10T19:40:53Z</updated>

		<summary type="html">&lt;p&gt;Darc: /* Cloning the KallistiOS git repository */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;div style=&amp;quot;float:right;&amp;quot;&amp;gt;__TOC__&amp;lt;/div&amp;gt;&lt;br /&gt;
=Introduction=&lt;br /&gt;
This article will cover the entire beginning process: starting from zero to having a working dev environment with debug link (serial or IP) and self-booting CD-R. This guide will cover the process for the following platforms:&lt;br /&gt;
* &#039;&#039;&#039;Microsoft Windows 10&#039;&#039;&#039; via [https://learn.microsoft.com/en-us/windows/wsl/about Windows Subsystem for Linux]&lt;br /&gt;
** Users desiring a native Windows approach, see [[DreamSDK]] instead&lt;br /&gt;
* &#039;&#039;&#039;macOS&#039;&#039;&#039; on Intel or Apple Silicon systems with the [https://brew.sh/ Homebrew] package manager installed&lt;br /&gt;
* &#039;&#039;&#039;Linux&#039;&#039;&#039;-based systems&lt;br /&gt;
** &#039;&#039;&#039;Debian&#039;&#039;&#039;- and &#039;&#039;&#039;Ubuntu&#039;&#039;&#039;-based distributions using the default &#039;&#039;&#039;apt&#039;&#039;&#039; package manger&lt;br /&gt;
** &#039;&#039;&#039;Fedora&#039;&#039;&#039;-based distributions using the default &#039;&#039;&#039;dnf&#039;&#039;&#039; package manager&lt;br /&gt;
** &#039;&#039;&#039;Arch&#039;&#039;&#039;-based distributions using the default &#039;&#039;&#039;pacman&#039;&#039;&#039; package manager&lt;br /&gt;
** &#039;&#039;&#039;Alpine&#039;&#039;&#039;-based distributions using the default &#039;&#039;&#039;apk&#039;&#039;&#039; package manager&lt;br /&gt;
&lt;br /&gt;
===Need help?===&lt;br /&gt;
Important note: &#039;&#039;This guide aims to remain up to date and work on all of the above platforms, but keeping instructions for such a variety of platforms up-to-date can be difficult. If you run into any errors or other challenges while following this tutorial, or simply need clarification on any of the steps, feel free to ask for assistance on the [https://dcemulation.org/phpBB/viewforum.php?f=29 message board] or [https://discord.gg/RuKmpu7k Discord chat] and we would be happy to aid you and update the guide for the benefit of future readers and others in the community.&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
===Terms===&lt;br /&gt;
Before we get started, let&#039;s define several terms:&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;&#039;toolchain&#039;&#039;&#039; is a set of programs which turns your code into an executable file for your Dreamcast console. The toolchain includes:&lt;br /&gt;
* &#039;&#039;&#039;GCC&#039;&#039;&#039;, a compiler which supports C, C++, Objective-C, and more (see also [[Rust on Dreamcast]] and [[Programming language support]])&lt;br /&gt;
* &#039;&#039;&#039;binutils&#039;&#039;&#039;, an assembler and linker&lt;br /&gt;
* &#039;&#039;&#039;newlib&#039;&#039;&#039;, a C library&lt;br /&gt;
* &#039;&#039;&#039;gdb&#039;&#039;&#039;, a debugger&lt;br /&gt;
The toolchain includes a compiler for the Dreamcast&#039;s main SH4 CPU, and optionally a compiler for the ARM-based AICA sound processor. Your operating system may already have versions of these programs installed to compile code for your computer, but we will need to build a &amp;quot;cross-compiler&amp;quot; for compiling specifically for the Dreamcast. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;KallistiOS&#039;&#039;&#039; or &#039;&#039;KOS&#039;&#039; is an open source development library and pseudo-operating system for the Dreamcast console. It is the best documented and most widely used development kit in the homebrew community. KallistiOS&#039;s very flexible license allows both homebrew and commercial use with no restrictions other than a requirement to include credit for its use in your project, and indeed almost all commercially sold indie Dreamcast titles use it. There are others in existence, like [[libronin]] and [[libdream]], as well as the older development kits [[Katana]] and [[Windows CE]] created by Sega and Microsoft for use in retail games, but this guide will only cover the setup and use of KallistiOS. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;kos-ports&#039;&#039;&#039; is a repository including various libraries which integrate with KallistiOS. We will download and compile these libraries as well. &lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;&#039;debug link&#039;&#039;&#039; is a generic term referring to a hardware accessory to facilitate quickly running and debugging your programs. IP-based links include the Dreamcast&#039;s &#039;&#039;&#039;[[Broadband adapter]]&#039;&#039;&#039; and &#039;&#039;&#039;[[LAN adapter]]&#039;&#039;&#039; accessories, and serial-based links include the [[Coder&#039;s cable]], which is a cable that can connect the Dreamcast&#039;s serial port to your computer via USB or serial. This guide includes instructions for setting up and using the the Broadband adapter and a USB-based coder&#039;s cable.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;dc-tool&#039;&#039;&#039; and &#039;&#039;&#039;dcload&#039;&#039;&#039; are a pair of programs to facilitate using a debug link. &#039;&#039;dc-tool&#039;&#039; runs on your computer and links to a Dreamcast running &#039;&#039;dcload-ip&#039;&#039; or &#039;&#039;dcload-serial&#039;&#039;. With this setup, you can quickly load programs, read console feedback, load assets, transfer data, redirect I/O, handle exceptions, debug problems, and so forth.&lt;br /&gt;
&lt;br /&gt;
=Choosing a debug link solution=&lt;br /&gt;
If you are building the toolchain for the purpose of building existing programs from source with little to no modifications, then a debug link setup might not be necessary for you. You may simply build programs to burn directly to CD-R. However, if you are planning to actively develop for the Dreamcast, then a debug link is a critical component. While Dreamcast emulators are mature and accurate enough to play the vast majority of the system&#039;s games library without issue, many critical bugs may show up on a real Dreamcast system, but not on a Dreamcast emulator. Therefore, it is highly recommended to test on a real system as much as possible. It&#039;s also possible to load software off of a [[Serial SD card adapter]], but without an active link to a computer, debugging and stepping through programs as they execute is significantly more challenging.&lt;br /&gt;
&lt;br /&gt;
Presented below is a table comparing the different options available for a debug link. Due to the cost, potential buyers may want to factor in the ability to play multiplayer games with their purchase. Thus, for comparison, we have included information about the [[Modem]] with [[DreamPi]] as well, but understand that the Modem with DreamPi cannot be used as a debug link.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
!colspan=&amp;quot;6&amp;quot; |Comparison of various Dreamcast connectivity options&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#c0c0c0;&amp;quot; width=&amp;quot;150&amp;quot; | Device: &lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; width=&amp;quot;400&amp;quot; | [[Broadband adapter]] (HIT-400 or HIT-401) &amp;lt;br /&amp;gt;Realtek RTL8139C chipset&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; width=&amp;quot;400&amp;quot; | [[LAN adapter]] (HIT-300) &amp;lt;br /&amp;gt;Fujitsu MB86967 chipset&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; width=&amp;quot;400&amp;quot; | [[Modem]] with [[DreamPi]]&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; width=&amp;quot;400&amp;quot; | USB [[Coder&#039;s cable]]&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; width=&amp;quot;400&amp;quot; | Serial [[Coder&#039;s cable]]&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Useful for dev? || Yes, supports dcload-ip || Yes, supports dcload-ip,&amp;lt;br/&amp;gt;but BBA is superior and cheaper || No, only useful for online multiplayer gaming || Yes, supports dcload-serial || Yes, supports dcload-serial&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Cost || $100 - $200 and up on used markets || $200 and up on used markets,&amp;lt;br/&amp;gt;due to extreme rarity || Kit prices vary, around $100 || See [[Coder&#039;s cable]] for vendors&amp;lt;br /&amp;gt;darc sells for $45, RetroOnyx sells for $85 || Varies on used markets, uncommonly sold&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Can make DIY? || No || No || Yes || Yes || Yes&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Performance || Up to 100 megabits/s || Up to 10 megabits/s || Up to 56 kilobits/s || Up to 1500 kilobits/s || Up to 120 kilobits/s&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Games support || Some games: Phantasy Star Online, Quake III Arena, Toy Racer, POD SpeedZone, Propellor Arena, Unreal Tournament&amp;lt;br /&amp;gt;Some browsers: Broadband Passport, PlanetWeb 3.0 || No games&amp;lt;br /&amp;gt;One browser: Dream Passport for LAN || All multiplayer games with network support&amp;lt;br /&amp;gt;All web browsers || NO multiplayer games support || NO multiplayer games support &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Homebrew support || Homebrew utilities like dcload-ip || Homebrew utilities like dcload-ip || Homebrew utilities don&#039;t support, only multiplayer games || Homebrew utilities like dcload-serial || Homebrew utilities like dcload-serial &lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=Setting up and compiling the toolchain with the dc-chain script=&lt;br /&gt;
===Dependencies===&lt;br /&gt;
First, we&#039;ll need to install dependencies before building the toolchain. Below we have provided commands to install these dependencies on various systems. Many of the packages will likely already be installed on your system, but we have provided an exhaustive list for good measure.&lt;br /&gt;
====macOS 13 Ventura on an Intel or Apple Silicon processor====&lt;br /&gt;
First, make sure you install Apple Xcode, including the Command Line tools. You will also need to install several other packages for which we&#039;ll include instructions assuming you have installed the [https://brew.sh/ Homebrew] package manager on your system.&lt;br /&gt;
 brew install wget gettext texinfo gmp mpfr libmpc libelf jpeg-turbo libpng cmake meson libisofs&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;Important Note for Apple Silicon users&#039;&#039;: On Apple Silicon, Homebrew installs libraries to a path not included by default by the compiler. If you haven&#039;t added these to your &#039;&#039;&#039;~/.zprofile&#039;&#039;&#039;, then add the following lines now and reload your session (or run them in your Terminal session whenever you compile KOS):&lt;br /&gt;
 export CPATH=/opt/homebrew/include&lt;br /&gt;
 export LIBRARY_PATH=/opt/homebrew/lib&lt;br /&gt;
&lt;br /&gt;
====Debian/Ubuntu-based Linux====&lt;br /&gt;
 sudo apt-get update&lt;br /&gt;
 sudo apt install gawk patch bzip2 tar make libgmp-dev libmpfr-dev libmpc-dev gettext wget libelf-dev texinfo bison flex sed git build-essential diffutils curl libjpeg-dev libpng-dev python3 pkg-config cmake libisofs-dev meson ninja-build rake&lt;br /&gt;
&lt;br /&gt;
====Fedora-based Linux====&lt;br /&gt;
 sudo dnf install gawk patch bzip2 tar make gmp-devel mpfr-devel libmpc-devel gettext wget elfutils-libelf-devel texinfo bison flex sed git diffutils curl libjpeg-turbo-devel libpng-devel gcc-c++ python3 rubygem-rake meson ninja-build&lt;br /&gt;
&lt;br /&gt;
====Arch-based Linux====&lt;br /&gt;
 sudo pacman -S --needed gawk patch bzip2 tar make gmp mpfr libmpc gettext wget libelf texinfo bison flex sed git diffutils curl libjpeg-turbo libpng python3 meson ruby-rake&lt;br /&gt;
&lt;br /&gt;
====Alpine-based Linux====&lt;br /&gt;
 sudo apk --update add build-base patch bash texinfo gmp-dev libjpeg-turbo-dev libpng-dev elfutils-dev curl wget python3 git ruby-rake&lt;br /&gt;
&lt;br /&gt;
====Other Linux distributions====&lt;br /&gt;
If you&#039;re using a different Linux- or Unix-based system besides the one above, you may need to reference your distribution&#039;s package database and package manager documentation for the equivalent package names and commands necessary for your system.&lt;br /&gt;
&lt;br /&gt;
===Creating a space for your toolchain installation===&lt;br /&gt;
Create the path where we&#039;ll install the toolchain and KOS, and grant it the proper permissions:&lt;br /&gt;
 sudo mkdir -p /opt/toolchains/dc&lt;br /&gt;
 sudo chmod -R 755 /opt/toolchains/dc&lt;br /&gt;
 sudo chown -R $(id -u):$(id -g) /opt/toolchains/dc&lt;br /&gt;
===Cloning the KallistiOS git repository===&lt;br /&gt;
Next, clone the git repository for the latest &#039;&#039;&#039;2.1.0&#039;&#039;&#039; stable version of KallistiOS: &lt;br /&gt;
 git clone https://github.com/KallistiOS/KallistiOS.git -b v2.1.x /opt/toolchains/dc/kos&lt;br /&gt;
&lt;br /&gt;
Alternatively, you may clone the latest unstable version, which contains the latest changes, but may also contain bugs or issues:&lt;br /&gt;
 git clone https://github.com/KallistiOS/KallistiOS.git -b master /opt/toolchains/dc/kos&lt;br /&gt;
&lt;br /&gt;
===Configuring the dc-chain script===&lt;br /&gt;
Enter the dc-chain directory:&lt;br /&gt;
 cd /opt/toolchains/dc/kos/utils/dc-chain&lt;br /&gt;
&lt;br /&gt;
You may now configure the options to your liking by creating a &amp;lt;code&amp;gt;Makefile.cfg&amp;lt;/code&amp;gt; file in this directory. Copy or rename the &amp;lt;code&amp;gt;Makefile.default.cfg&amp;lt;/code&amp;gt; file to &amp;lt;code&amp;gt;Makefile.cfg&amp;lt;/code&amp;gt; and then use a text editor to edit this file&#039;s settings to your liking. For example, you may like to alter the &amp;lt;code&amp;gt;makejobs&amp;lt;/code&amp;gt; option to the number of threads available on your CPU to speed up the compilation. However, if you run into errors during compilation, you may want to set &amp;lt;code&amp;gt;makejobs=1&amp;lt;/code&amp;gt;, as on some operating systems the toolchain may fail to build with a higher setting.&lt;br /&gt;
&lt;br /&gt;
We will leave the default &#039;&#039;&#039;stable&#039;&#039;&#039; profile for the toolchain, which currently uses GCC 13.2. For advanced users, other profiles are available to you; read the &amp;lt;code&amp;gt;README.md&amp;lt;/code&amp;gt; file in the dc-chain directory for more information if you are interested. If you are unsure about options in the &amp;lt;code&amp;gt;Makefile.cfg&amp;lt;/code&amp;gt;, just leave the defaults alone.&lt;br /&gt;
&lt;br /&gt;
===Downloading and compiling the toolchain===&lt;br /&gt;
Now we will run a script to download files and compile the toolchain. At this point, we have the option of building both the main CPU SH4 compiler and the AICA sound processor ARM compiler, or we can skip the ARM compiler and just build the SH4 compiler. Thankfully, KallistiOS includes a prebuilt sound driver, so the ARM compiler is only necessary if you&#039;re wanting to make changes to the sound driver or write custom code to run on the sound processor.&lt;br /&gt;
To build &#039;&#039;&#039;only the SH4 compiler&#039;&#039;&#039;:&lt;br /&gt;
 make&lt;br /&gt;
To build &#039;&#039;&#039;both&#039;&#039;&#039; the SH4 and the ARM compilers:&lt;br /&gt;
 make all&lt;br /&gt;
This will download and unpack the relevant necessary files and then begin the compilation process. The compilation can take anywhere from minutes to a few hours depending on your CPU and number of threads available. When successfully finished, the toolchains will be ready. If you used the commands above, the &#039;&#039;&#039;gdb&#039;&#039;&#039; debugger will have been built as well.&lt;br /&gt;
&lt;br /&gt;
===Cleaning up temporary files===&lt;br /&gt;
After building everything, you can clean up the extraneous files in your dc-chain directory by entering:&lt;br /&gt;
 make clean&lt;br /&gt;
&lt;br /&gt;
=Configuring and compiling KOS and kos-ports=&lt;br /&gt;
===Setting up the environment settings===&lt;br /&gt;
Enter the KOS directory:&lt;br /&gt;
 cd /opt/toolchains/dc/kos&lt;br /&gt;
Copy the pre-made environment script into place:&lt;br /&gt;
 cp doc/environ.sh.sample environ.sh &lt;br /&gt;
For most users, the default settings will suffice. However, advanced users may edit the environ.sh to your liking if you&#039;d like to change compile flags or alter paths. If you&#039;d like to have multiple KOS versions installed or multiple toolchain versions installed, you can set up different environ.sh files corresponding to these different configurations by altering the paths. Run the source command on the desired environ.sh file to select that configuration prior to compiling your project. &lt;br /&gt;
&lt;br /&gt;
You will need to run the source command to apply the KOS environment settings to your currently running shell. Run the following now, &#039;&#039;&#039;and&#039;&#039;&#039; &#039;&#039;whenever&#039;&#039; you open a new shell to work on Dreamcast projects:&lt;br /&gt;
 source /opt/toolchains/dc/kos/environ.sh&lt;br /&gt;
&lt;br /&gt;
===Building KOS===&lt;br /&gt;
Build KOS:&lt;br /&gt;
 make&lt;br /&gt;
KOS is now built.&lt;br /&gt;
===Building kos-ports===&lt;br /&gt;
Clone the kos-ports repository to your system:&lt;br /&gt;
 git clone --recursive https://github.com/KallistiOS/kos-ports /opt/toolchains/dc/kos-ports&lt;br /&gt;
Run the script to build all of the included ports:&lt;br /&gt;
 /opt/toolchains/dc/kos-ports/utils/build-all.sh&lt;br /&gt;
kos-ports is now built.&lt;br /&gt;
&lt;br /&gt;
===Building the KOS examples===&lt;br /&gt;
Enter the KOS examples directory:&lt;br /&gt;
 cd /opt/toolchains/dc/kos/examples&lt;br /&gt;
Build the examples:&lt;br /&gt;
 make&lt;br /&gt;
All of the example programs provided with KallistiOS are now built.&lt;br /&gt;
&lt;br /&gt;
=Further=&lt;br /&gt;
At this point, you will have a cross-compiler built so that you can generate programs for your Dreamcast from code. KallistiOS, the Dreamcast development library and operating system, is built, as is its associated packages within kos-ports, and all of its example programs.&lt;br /&gt;
&lt;br /&gt;
At this point, you will likely want to view some of the following articles:&lt;br /&gt;
* Setting up an IDE for Dreamcast development&lt;br /&gt;
** [[CLion Debugging]]&lt;br /&gt;
** [[Visual Studio Code]]&lt;br /&gt;
* [[Creating a first project with KallistiOS]] (&#039;&#039;&#039;TODO&#039;&#039;&#039;: Explain how to create a new DC project folder with Makefile, adding an external library, create a basic program, and compile it)&lt;br /&gt;
* [[Using a debug link]]&lt;br /&gt;
* [[Burning an example to CD-R]]&lt;br /&gt;
&lt;br /&gt;
=Running an example program through a debug link=&lt;br /&gt;
&#039;&#039;&#039;TODO&#039;&#039;&#039;: &#039;&#039;Give a tutorial on compiling dcload/dc-tool, setting up a serial, USB, or IP debug link, and running 2ndmix demo on real hardware.&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Download and burn the [[:File:Dcload-2023-06-22.zip|latest versions of dcload-ip or dcload-serial]] -- the IP version includes improved DHCP support, so there is no longer a need to configure things beforehand. &lt;br /&gt;
 &lt;br /&gt;
Run one of the examples from the &amp;lt;code&amp;gt;kos/examples/dreamcast&amp;lt;/code&amp;gt; directory with one of the following commands:&lt;br /&gt;
 dc-tool-ip -t &amp;lt;dreamcast IP address&amp;gt; -x example.elf&lt;br /&gt;
&lt;br /&gt;
 dc-tool-ser -t &amp;lt;serial device path&amp;gt; -x example.elf&lt;br /&gt;
&lt;br /&gt;
Run &amp;lt;code&amp;gt;dc-tool-ip&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;dc-tool-ser&amp;lt;/code&amp;gt; without any parameters to get additional options.&lt;br /&gt;
&lt;br /&gt;
=Burning an example program to CD-R=&lt;br /&gt;
&#039;&#039;&#039;TODO&#039;&#039;&#039;: &#039;&#039;Explain how to build mkdcdisc and write 2ndmix demo to CD-R and run on a Dreamcast console&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[https://gitlab.com/simulant/mkdcdisc mkdcdisc] can be used to easily generate a burnable self-boot CDI image.&lt;br /&gt;
Dependencies required:&lt;br /&gt;
* Homebrew: &amp;lt;code&amp;gt;meson libisofs pkg-config&amp;lt;/code&amp;gt;&lt;br /&gt;
* Debian/Ubuntu:  &amp;lt;code&amp;gt;libisofs-dev meson ninja-build&amp;lt;/code&amp;gt;&lt;br /&gt;
* Fedora: &amp;lt;code&amp;gt;meson ninja-build&amp;lt;/code&amp;gt;&lt;br /&gt;
* Arch: &amp;lt;code&amp;gt;meson&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Build &amp;lt;code&amp;gt;mkdcdisc&amp;lt;/code&amp;gt;:&lt;br /&gt;
 git clone https://gitlab.com/simulant/mkdcdisc.git&lt;br /&gt;
 cd mkdcdisc&lt;br /&gt;
 meson setup builddir&lt;br /&gt;
 meson compile -C builddir&lt;br /&gt;
 ./builddir/mkdcdisc -h&lt;br /&gt;
and create a CDI image from your compiled ELF like so:&lt;br /&gt;
 mkdcdisc -e MyProgram.elf -o MyProgram.cdi&lt;br /&gt;
Then you can burn the CDI file using DiscJuggler (Windows-only, but also works through [https://www.winehq.org/ WINE]), ImgBurn with the CDI plugin, or the cdiburn *nix script floating around out there. (document this better)&lt;/div&gt;</summary>
		<author><name>Darc</name></author>
	</entry>
	<entry>
		<id>https://dreamcast.wiki/wiki/index.php?title=Rust_on_Dreamcast&amp;diff=3637</id>
		<title>Rust on Dreamcast</title>
		<link rel="alternate" type="text/html" href="https://dreamcast.wiki/wiki/index.php?title=Rust_on_Dreamcast&amp;diff=3637"/>
		<updated>2024-11-10T03:21:19Z</updated>

		<summary type="html">&lt;p&gt;Darc: /* Current Status */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[File:Rust-dc-logo.png|thumb|Ferris holding his Dreamcast controller]]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Rust&#039;&#039;&#039; is a popular modern programming language focusing on performance, memory safety, and program correctness. As a &amp;quot;blazingly fast&amp;quot; low-level language, it is viable for running on a Dreamcast console. Doing so presents a bit of a challenge, however, as the official Rust compiler is based on the [https://llvm.org/ LLVM] toolchain infrastructure, which does not support the Dreamcast CPU&#039;s SuperH architecture. Dreamcast programming is instead typically done with [https://gcc.gnu.org/ GCC], the GNU Compiler Collection. There are currently two viable solutions to this challenge:&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;rustc_codegen_gcc&#039;&#039;&#039;, the current preferred method: An experimental codegen backend for the official Rust &#039;&#039;&#039;rustc&#039;&#039;&#039; compiler which interfaces with GCC&#039;s libgccjit API&lt;br /&gt;
* &#039;&#039;&#039;gccrs&#039;&#039;&#039;: an experimental Rust frontend for GCC&lt;br /&gt;
&lt;br /&gt;
Neither solution is complete at this time, but &#039;&#039;&#039;rustc_codegen_gcc&#039;&#039;&#039; is much further along and is quite usable with some patience with its current limitations. On the other hand, &#039;&#039;&#039;gccrs&#039;&#039;&#039; can compile for Dreamcast, but is in an earlier state. Below we will focus on using &#039;&#039;&#039;rustc_codegen_gcc&#039;&#039;&#039;. For more information on using gccrs, see the [[gccrs]] page.&lt;br /&gt;
&lt;br /&gt;
=Current Status=&lt;br /&gt;
* The full &#039;&#039;&#039;std&#039;&#039;&#039; library is supported.&lt;br /&gt;
* A [https://github.com/dreamcast-rs/KallistiOS custom version of KallistiOS] v2.1.0 (latest stable release) is used.&lt;br /&gt;
** This version includes minor changes for Rust and a beta libpthread addon layer to interface with the Rust standard library.&lt;br /&gt;
** Wrappers are provided to invoke &#039;&#039;&#039;cargo&#039;&#039;&#039; and &#039;&#039;&#039;rustc&#039;&#039;&#039; in a familiar manner to compile projects.&lt;br /&gt;
* A [https://github.com/dreamcast-rs/kos-sys &#039;&#039;&#039;kos-sys&#039;&#039;&#039;] raw/unsafe bindings crate is provided for easily linking KallistiOS and its unsafe C API into Rust crates. This crate is in early stages and in active development.&lt;br /&gt;
* A [https://github.com/dreamcast-rs/kos-rs &#039;&#039;&#039;kos-rs&#039;&#039;&#039;] Rust crate is available but is also in early stages.&lt;br /&gt;
** Documentation for the bindings is available at [https://kos-rs.dreamcast.wiki/ kos-rs.dreamcast.wiki].&lt;br /&gt;
** If bindings do not yet exist for desired functionality, wrapping of KallistiOS C functions is required to get proper use of KallistiOS at this time. See the [https://doc.rust-lang.org/nomicon/ffi.html Foreign Function Interface page] in the Rustonomicon for further understanding. &lt;br /&gt;
** A custom [https://github.com/dreamcast-rs/libc/tree/libc-0.2-kos &#039;&#039;&#039;libc&#039;&#039;&#039; crate with KallistiOS support] is provided.&lt;br /&gt;
* Code generation uses &#039;&#039;&#039;rustc_codegen_gcc&#039;&#039;&#039;, so its limitations apply here. See the [https://blog.antoyo.xyz/ development blog] for more information on its progress.&lt;br /&gt;
** rustc_codegen_gcc is pinned to a particular Rust nightly version, but is synced regularly. The current version in use is the &#039;&#039;&#039;2024-08-10&#039;&#039;&#039; nightly.&lt;br /&gt;
** A [https://github.com/dreamcast-rs/rust/tree/kos-2024-08-10 KallistiOS-patched version of the latest nightly] is maintained and updated regularly. &lt;br /&gt;
** Panic unwinding, debug info, LTO, etc. are not currently available.&lt;br /&gt;
** Architectures dependent on rustc_codegen_gcc are not yet able to be added to the rustc frontend, so a workaround is necessary: MIPS is falsely selected for KallistiOS, causing rustc to emit objects with a MIPS header and a provided wrapper is used when linking to rewrite these MIPS headers to SuperH before passing the compiled objects to the linker.&lt;br /&gt;
* A separate [https://github.com/rust-lang/gcc development version of the GCC toolchain] must be used.&lt;br /&gt;
** This version of GCC contains the latest libgccjit patches necessary to interface with rustc_codegen_gcc, and will be built to work with the custom KallistiOS&#039;s libpthread layer.&lt;br /&gt;
** New updates to rustc_codegen_gcc often require new updates to GCC, so recompiling GCC regularly will be necessary to keep up with the latest updates.&lt;br /&gt;
** The development version of GCC does not always compile well on every platform. Using a modern Linux platform is recommended for maximum compatibility.&lt;br /&gt;
** The custom KallistiOS and GCC environment can still be used to compile code or projects written in C, C++, etc.&lt;br /&gt;
* KallistiOS and all related libraries and tools must be compiled using the &amp;lt;code&amp;gt;-m4-single&amp;lt;/code&amp;gt; ABI. This is already set up for you in the guide below, but if external libraries are being linked to Rust projects, make sure that code is compiled using the proper ABI.&lt;br /&gt;
&lt;br /&gt;
=Setting up a Rust environment for Dreamcast development=&lt;br /&gt;
The following guide will provide instructions for setting up a Rust development environment for Dreamcast. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;If you run into any errors or other challenges while following this tutorial, or simply need clarification on any of the steps, feel free to ask for assistance on the [https://dcemulation.org/phpBB/viewforum.php?f=29 message board] or [https://discord.gg/wDEfkUh63h our Discord server] and we would be happy to aid you and update the guide for the benefit of future readers and others in the community.&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
==Prerequisites==&lt;br /&gt;
First, we will need to set up the prerequisites.&lt;br /&gt;
&lt;br /&gt;
* You must have the [https://rustup.rs/ rustup] tool installed for your operating system to manage Rust toolchain installations. It will automatically manage the latest Rust nightly installations for you. You cannot use a Rust toolchain that may be provided by your operating system vendor.&lt;br /&gt;
* Install the dependency packages for your operating system. These are listed in the &#039;&#039;&#039;Dependencies&#039;&#039;&#039; section on the [[Getting Started with Dreamcast development#Dependencies]] page, with lists provided for most common operating systems.&lt;br /&gt;
* We will install our custom Rust toolchain environment within &amp;lt;code&amp;gt;/opt/toolchains/dc/rust&amp;lt;/code&amp;gt;. If it does not already exist, create the &amp;lt;code&amp;gt;/opt/toolchains/dc&amp;lt;/code&amp;gt; directory and grant it proper permissions using the following commands. &lt;br /&gt;
 sudo mkdir -p /opt/toolchains/dc&lt;br /&gt;
 sudo chmod -R 755 /opt/toolchains/dc&lt;br /&gt;
 sudo chown -R $(id -u):$(id -g) /opt/toolchains/dc&lt;br /&gt;
* Clone the [https://github.com/dreamcast-rs/rust-for-dreamcast/ Rust for Dreamcast] repo containing necessary support files to &amp;lt;code&amp;gt;/opt/toolchains/dc/rust&amp;lt;/code&amp;gt;:&lt;br /&gt;
 git clone https://github.com/dreamcast-rs/rust-for-dreamcast.git /opt/toolchains/dc/rust&lt;br /&gt;
&lt;br /&gt;
==Installing our custom KallistiOS and GCC toolchain==&lt;br /&gt;
Clone the git repository for our custom Rust-patched version of KallistiOS v2.1.0 stable:&lt;br /&gt;
 git clone https://github.com/dreamcast-rs/KallistiOS /opt/toolchains/dc/rust/kos&lt;br /&gt;
&lt;br /&gt;
Enter the dc-chain directory:&lt;br /&gt;
 cd /opt/toolchains/dc/rust/kos/utils/dc-chain&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;Makefile.default.cfg&amp;lt;/code&amp;gt; file in this directory has been customized for you, selecting the proper toolchain profile, paths, and build options necessary for building a Rust toolchain using this guide. It is not recommended to adjust the defaults.&lt;br /&gt;
&lt;br /&gt;
To build the GCC toolchain using 2 CPU cores, simply type:&lt;br /&gt;
 make makejobs=2&lt;br /&gt;
Adjust the &amp;lt;code&amp;gt;makejobs=...&amp;lt;/code&amp;gt; value to use the number of CPU cores available to you on your system in order to speed up compilation. This may take a while to build, so be patient.&lt;br /&gt;
&lt;br /&gt;
Once the GCC toolchain is built, source the &amp;lt;code&amp;gt;environ.sh&amp;lt;/code&amp;gt; KallistiOS environment settings. A custom &amp;lt;code&amp;gt;environ.sh&amp;lt;/code&amp;gt; file has been provided for you, containing settings pre-adjusted for Rust development using this guide.&lt;br /&gt;
&lt;br /&gt;
You will need to run the source command to apply the KallistiOS environment settings to your currently running shell. Run the following now, &#039;&#039;&#039;and&#039;&#039;&#039; &#039;&#039;whenever&#039;&#039; you open a new shell to work on Dreamcast projects:&lt;br /&gt;
 source /opt/toolchains/dc/rust/misc/environ.sh&lt;br /&gt;
&lt;br /&gt;
Enter the KallistiOS directory:&lt;br /&gt;
 cd /opt/toolchains/dc/rust/kos&lt;br /&gt;
&lt;br /&gt;
Build KallistiOS:&lt;br /&gt;
 make&lt;br /&gt;
&lt;br /&gt;
==Building rustc_codegen_gcc and Rust sysroot==&lt;br /&gt;
Now that we have a working KallistiOS environment suitable for Rust development set up, we can build the Rust compiler compiler and sysroot components. A script is provided which will download the necessary components from the respective repositories and compile them for you. Run the installer script:&lt;br /&gt;
&lt;br /&gt;
 /opt/toolchains/dc/rust/misc/install-rust.sh&lt;br /&gt;
&lt;br /&gt;
If all goes well, you&#039;ll see &#039;&#039;&#039;Rust for KallistiOS/Dreamcast installed!&#039;&#039;&#039; message!&lt;br /&gt;
&lt;br /&gt;
==Building KOS ports libraries==&lt;br /&gt;
If desired, you may also built the KOS ports collection of libraries for use with your Rust environment. It is recommended to maintain a separate KOS ports installation for linking with Rust projects in &amp;lt;code&amp;gt;/opt/toolchains/dc/rust/kos-ports&amp;lt;/code&amp;gt;. This is because the Rust environment is using the &amp;lt;code&amp;gt;-m4-single&amp;lt;/code&amp;gt; ABI, but the KallistiOS default installation will be using the &amp;lt;code&amp;gt;-m4-single-only&amp;lt;/code&amp;gt;, which cannot be mixed and matched.&lt;br /&gt;
&lt;br /&gt;
Clone the kos-ports repository to your system:&lt;br /&gt;
 git clone https://github.com/KallistiOS/kos-ports /opt/toolchains/dc/rust/kos-ports&lt;br /&gt;
An individual port can be built and installed by entering its directory and running the proper command. For example, installing GLdc:&lt;br /&gt;
 cd /opt/toolchains/dc/rust/kos-ports/libGL&lt;br /&gt;
 make install&lt;br /&gt;
Or, instead, you may run the script to build all of the included ports:&lt;br /&gt;
 /opt/toolchains/dc/rust/kos-ports/utils/build-all.sh&lt;br /&gt;
&lt;br /&gt;
=Using Rust for Dreamcast=&lt;br /&gt;
If all went well, you will now have a working Rust for Dreamcast environment!&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;cargo&#039;&#039;&#039; can be invoked to target Dreamcast using the &amp;lt;code&amp;gt;kos-cargo&amp;lt;/code&amp;gt; command.&lt;br /&gt;
** &amp;lt;code&amp;gt;kos-cargo run&amp;lt;/code&amp;gt; can be used to build and run code on a Dreamcast console using [[dcload-ip]] or [[dcload-serial]]. You will need to set up the &amp;lt;code&amp;gt;KOS_LOADER&amp;lt;/code&amp;gt; variable with the necessary command in the &amp;lt;code&amp;gt;/opt/toolchains/dc/rust/misc/environ.sh&amp;lt;/code&amp;gt; file (and then re-source the file for the change to take effect).&lt;br /&gt;
* &#039;&#039;&#039;rustc&#039;&#039;&#039; can be invoked to target Dreamcast using the &amp;lt;code&amp;gt;kos-rustc&amp;lt;/code&amp;gt; command.&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
Examples are included with the Rust for Dreamcast repo to help you get started.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/hello-cargo &amp;lt;code&amp;gt;hello-cargo&amp;lt;/code&amp;gt;] demonstrates how to create a simple &amp;quot;Hello, world!&amp;quot; application using cargo.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/hello-make &amp;lt;code&amp;gt;hello-make&amp;lt;/code&amp;gt;] demonstrates how to create a simple &amp;quot;Hello, world!&amp;quot; application using standard Makefiles.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/gldc-cube &amp;lt;code&amp;gt;gldc-cube&amp;lt;/code&amp;gt;] demonstrates a Rust project using the GLdc KOS port.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/addlib &amp;lt;code&amp;gt;addlib&amp;lt;/code&amp;gt;] demonstrates how to create a Rust library that can be included with a KallistiOS project.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/atomics &amp;lt;code&amp;gt;atomics&amp;lt;/code&amp;gt;] demonstrates the use of atomic functions.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/filesystem-io &amp;lt;code&amp;gt;filesystem-io&amp;lt;/code&amp;gt;] demonstrates listing directory contents and opening files.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/network-time &amp;lt;code&amp;gt;network-time&amp;lt;/code&amp;gt;] demonstrates displaying the system time and retrieving the current UTC time from an NTP server using the [[Broadband adapter]] or [[LAN adapter]].&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/threads &amp;lt;code&amp;gt;threads&amp;lt;/code&amp;gt;] demonstrates the use of threads.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/tokio-async &amp;lt;code&amp;gt;tokio-async&amp;lt;/code&amp;gt;] demonstrates the use of the &#039;&#039;&#039;tokio&#039;&#039;&#039; async runtime.&lt;br /&gt;
&lt;br /&gt;
==Creating a new Rust project with Cargo==&lt;br /&gt;
First, we&#039;ll demonstrate creating a new &amp;quot;Hello, world!&amp;quot; project with &amp;lt;code&amp;gt;kos-cargo&amp;lt;/code&amp;gt;. This will follow the [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/hello-cargo &amp;lt;code&amp;gt;cargo-hello&amp;lt;/code&amp;gt;] example included in the Rust-for-Dreamcast repo. &lt;br /&gt;
&lt;br /&gt;
In a directory of your choosing, let&#039;s invoke &amp;lt;code&amp;gt;kos-cargo&amp;lt;/code&amp;gt; to create a new project and then enter the directory:&lt;br /&gt;
 kos-cargo new hello&lt;br /&gt;
 cd hello&lt;br /&gt;
&lt;br /&gt;
Let&#039;s add our kos-rs crate to gain access to current KallistiOS bindings. Open &amp;lt;code&amp;gt;Cargo.toml&amp;lt;/code&amp;gt; in your text editor and add:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;toml&amp;quot;&amp;gt;&lt;br /&gt;
[dependencies]&lt;br /&gt;
kos = { package = &amp;quot;kos-rs&amp;quot;, git = &amp;quot;https://github.com/dreamcast-rs/kos-rs&amp;quot; }&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we can open up &amp;lt;code&amp;gt;src/main.rs&amp;lt;/code&amp;gt; and write our &amp;quot;Hello, world!&amp;quot; example code:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; line&amp;gt;&lt;br /&gt;
#![no_main]&lt;br /&gt;
&lt;br /&gt;
#[no_mangle]&lt;br /&gt;
extern &amp;quot;C&amp;quot; fn main(_argc: isize, _argv: *const *const u8) -&amp;gt; isize {&lt;br /&gt;
    println!(&amp;quot;Hello, world from Rust!&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
    0&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;#![no_main]&amp;lt;/syntaxhighlight&amp;gt; tells Rust that we will not have Rust use a &amp;lt;code&amp;gt;main&amp;lt;/code&amp;gt; function as an entry point -- this will be handled by KallistiOS.&lt;br /&gt;
* &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;#[no_mangle]&amp;lt;/syntaxhighlight&amp;gt; tells Rust to disable name mangling so that the &amp;lt;code&amp;gt;main&amp;lt;/code&amp;gt; function can be used by KallistiOS&#039;s C code.&lt;br /&gt;
* Finally, we have a &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;fn main&amp;lt;/syntaxhighlight&amp;gt; with the function signature of a typical C &amp;lt;code&amp;gt;main&amp;lt;/code&amp;gt; function, containing a basic &amp;quot;Hello, world!&amp;quot; exclamation.&lt;br /&gt;
&lt;br /&gt;
Now we can use &amp;lt;code&amp;gt;kos-cargo build&amp;lt;/code&amp;gt; to build our project. If all goes well, there will be a &amp;lt;code&amp;gt;target/sh-elf/debug/hello.elf&amp;lt;/code&amp;gt; file that can be sent to the Dreamcast with &amp;lt;code&amp;gt;dc-tool&amp;lt;/code&amp;gt;.&lt;br /&gt;
If you have &amp;lt;code&amp;gt;KOS_LOADER&amp;lt;/code&amp;gt; set in the &amp;lt;code&amp;gt;environ.sh&amp;lt;/code&amp;gt; file, you can invoke it directly with &amp;lt;code&amp;gt;kos-cargo run&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Creating a Rust project using kos-ports libraries==&lt;br /&gt;
[[File:Rust-Cube rustc codegen-gcc demo.gif|thumb|cube example in action]]&lt;br /&gt;
The [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/gldc-cube &amp;lt;code&amp;gt;gldc-cube&amp;lt;/code&amp;gt;] example demonstrates creating a rotating 3D cube using Rust as the primary language, while calling C functions provided by the &#039;&#039;&#039;GLdc&#039;&#039;&#039; library available in kos-ports. This project&#039;s initial setup is done the same as the above &amp;lt;code&amp;gt;hello&amp;lt;/code&amp;gt; example.&lt;br /&gt;
&lt;br /&gt;
For this example, we&#039;ll need to convert JPEG textures to VQ-encoded textures at compile time using the &amp;lt;code&amp;gt;vqenc&amp;lt;/code&amp;gt; utility includes with KallistiOS. To do this, we&#039;ll add a &amp;lt;code&amp;gt;build.rs&amp;lt;/code&amp;gt; file to the root of the crate with the following code (abridged to show the conversion of only one texture):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot;&amp;gt;&lt;br /&gt;
fn main() {&lt;br /&gt;
    let kos_base = std::env::var(&amp;quot;KOS_BASE&amp;quot;).expect(&amp;quot;Missing $KOS_BASE -- KallistiOS environment not sourced!&amp;quot;);&lt;br /&gt;
    let vqenc_cmd = kos_base + &amp;quot;/utils/vqenc/vqenc&amp;quot;;&lt;br /&gt;
    Command::new(&amp;amp;vqenc_cmd)&lt;br /&gt;
        .arg(&amp;quot;-t&amp;quot;)&lt;br /&gt;
        .arg(&amp;quot;-v&amp;quot;)&lt;br /&gt;
        .arg(&amp;quot;rsrc/tex_claw.jpg&amp;quot;)&lt;br /&gt;
        .output()&lt;br /&gt;
        .expect(&amp;quot;vqenc on tex_claw.jpg failed!&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
The texture files are then included in our project using the &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;include_bytes!&amp;lt;/syntaxhighlight&amp;gt; macro.&lt;br /&gt;
&lt;br /&gt;
We will need to link the GLdc library in this example, but we don&#039;t need to do anything special to add the paths to this library, because adding the common paths to KallistiOS libraries is already done for us in the kos-rs crate. However, if in your project you need to include a separate unique library path, you can add it in your &amp;lt;code&amp;gt;build.rs&amp;lt;/code&amp;gt; like so:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot;&amp;gt;&lt;br /&gt;
println!(&amp;quot;cargo:rustc-link-search=native={}&amp;quot;, lib_path);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The workings of this example&#039;s source code are too great to detail here line-by-line, but the example demonstrates how to use GLdc via the [https://github.com/dreamcast-rs/gldc-sys gldc-sys] crate, which contains unsafe/raw bindings to GLdc. Check out this crate&#039;s source code for an example of declaring and binding external C functions, constants, and structures so they can be used in Rust code. Since the entirety of the gldc-cube example uses C FFI functions without safe wrappers, the &amp;lt;code&amp;gt;main()&amp;lt;/code&amp;gt; source is wrapped in &amp;lt;code&amp;gt;unsafe {}&amp;lt;/code&amp;gt;. In the future, this would be much less necessary as higher level safe Rust bindings to KallistiOS and other libraries become mature.&lt;br /&gt;
&lt;br /&gt;
==Creating a Rust library==&lt;br /&gt;
Next, we&#039;ll demonstrate creating a Rust library with &amp;lt;code&amp;gt;kos-cargo&amp;lt;/code&amp;gt; that can be included in other Dreamcast code. This will follow the [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/addlib &amp;lt;code&amp;gt;addlib&amp;lt;/code&amp;gt;] example. Once again, this project&#039;s initial setup is done the same as the above &amp;lt;code&amp;gt;hello&amp;lt;/code&amp;gt; example, but you&#039;ll create the new project using &amp;lt;code&amp;gt;kos-cargo new --lib addlib&amp;lt;/code&amp;gt; to specify that we&#039;re creating a library named &amp;lt;code&amp;gt;addlib&amp;lt;/code&amp;gt;. You&#039;ll also need to add the following text to this project&#039;s &amp;lt;code&amp;gt;Cargo.toml&amp;lt;/code&amp;gt; file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;toml&amp;quot;&amp;gt;&lt;br /&gt;
[lib]&lt;br /&gt;
crate-type = [&amp;quot;staticlib&amp;quot;]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This tells Rust to build a static &amp;lt;code&amp;gt;.a&amp;lt;/code&amp;gt; library archive file from our code, which is located in &amp;lt;code&amp;gt;src/lib.rs&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; line&amp;gt;&lt;br /&gt;
#[no_mangle]&lt;br /&gt;
pub extern &amp;quot;C&amp;quot; fn print_added(a: isize, b: isize) {&lt;br /&gt;
    print!(&amp;quot;{}&amp;quot;, a + b);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
#[no_mangle]&lt;br /&gt;
pub extern &amp;quot;C&amp;quot; fn add_integers(a: isize, b: isize) -&amp;gt; isize {&lt;br /&gt;
    a + b&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The source code here starts similarly to the &amp;quot;Hello, world!&amp;quot; example, except we don&#039;t need to specify &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;#![no_main]&amp;lt;/syntaxhighlight&amp;gt; as this is a library which wouldn&#039;t have a &amp;lt;code&amp;gt;main()&amp;lt;/code&amp;gt; function anyway.&lt;br /&gt;
&lt;br /&gt;
Two simple functions are provided: one for adding two integers and returning the result, and another for adding two integers and printing the result as text. Because these functions use &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;#[no_mangle]&amp;lt;/syntaxhighlight&amp;gt; and are declared &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;extern &amp;quot;C&amp;quot;&amp;lt;/syntaxhighlight&amp;gt;, they can be called by name in C code that links this library.&lt;br /&gt;
&lt;br /&gt;
When built using &amp;lt;code&amp;gt;kos-cargo build&amp;lt;/code&amp;gt;, a &amp;lt;code&amp;gt;target/sh-elf/debug/libaddlib.a&amp;lt;/code&amp;gt; file will be generated. This can be linked into other projects to gain the use of these functions.&lt;br /&gt;
&lt;br /&gt;
For example, this can be added to a standard &amp;lt;code&amp;gt;Makefile&amp;lt;/code&amp;gt;-based KallistiOS project by editing the &amp;lt;code&amp;gt;Makefile&amp;lt;/code&amp;gt;:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;make&amp;quot;&amp;gt;&lt;br /&gt;
$(TARGET): $(OBJS)&lt;br /&gt;
	kos-cc -o $(TARGET) $(OBJS) -L/opt/toolchains/dc/rust/examples/cargo-addlib/target/sh-elf/debug -laddlib&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then, we can use the code in our C source:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/* Declare the external function from the Rust library */&lt;br /&gt;
int add_integers(int a, int b);&lt;br /&gt;
&lt;br /&gt;
/* Use the function */&lt;br /&gt;
printf(&amp;quot;Five plus six is %d\n&amp;quot;, add_integers(5, 6));&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Compiling individual modules into object files with rustc==&lt;br /&gt;
If we&#039;d like to mix C and Rust code in the same &amp;lt;code&amp;gt;Makefile&amp;lt;/code&amp;gt;-based KallistiOS project without building an entirely separate library, we can do that as well. This is demonstrated in the [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/no_std/hello-make &amp;lt;code&amp;gt;hello-make&amp;lt;/code&amp;gt;] example.&lt;br /&gt;
&lt;br /&gt;
Instead of using &amp;lt;code&amp;gt;kos-cargo&amp;lt;/code&amp;gt;, we can invoke the &amp;lt;code&amp;gt;kos-rustc&amp;lt;/code&amp;gt; via a &amp;lt;code&amp;gt;Makefile&amp;lt;/code&amp;gt; to build Rust modules. If we assume the Rust file is named &amp;lt;code&amp;gt;example.rs&amp;lt;/code&amp;gt;, you&#039;ll need to add &amp;lt;code&amp;gt;example.o&amp;lt;/code&amp;gt; as an object file in your &amp;lt;code&amp;gt;Makefile&amp;lt;/code&amp;gt;&#039;s &amp;lt;code&amp;gt;OBJS =&amp;lt;/code&amp;gt; declaration. For example, if the project has two source files &amp;lt;code&amp;gt;hello_c.c&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;hello_rust.rs&amp;lt;/code&amp;gt;, our &amp;lt;code&amp;gt;Makefile&amp;lt;/code&amp;gt; would have a line like this:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;make&amp;quot;&amp;gt;&lt;br /&gt;
OBJS = hello_c.o hello_rust.o&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The example code demonstrates starting a C &amp;lt;code&amp;gt;main()&amp;lt;/code&amp;gt; function to call a Rust function which builds a &amp;lt;code&amp;gt;String&amp;lt;/code&amp;gt; containing the &amp;quot;Hello, world!&amp;quot; text which is passed back to a C function which prints &amp;lt;code&amp;gt;String&amp;lt;/code&amp;gt;s.&lt;br /&gt;
&lt;br /&gt;
==Using the libc crate with KallistiOS==&lt;br /&gt;
If any of the crates in your project&#039;s dependency tree requires the &#039;&#039;&#039;libc&#039;&#039;&#039; crate, you will need to override fetching the default crate with our custom libc crate with KallistiOS/Dreamcast definitions. This custom version was copied to &amp;lt;code&amp;gt;/opt/toolchains/dc/rust/libc&amp;lt;/code&amp;gt; when building the Rust sysroot earlier. Add the following text to your &amp;lt;code&amp;gt;Cargo.toml&amp;lt;/code&amp;gt;:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;toml&amp;quot;&amp;gt;&lt;br /&gt;
[patch.crates-io]&lt;br /&gt;
libc = { path = &amp;quot;/opt/toolchains/dc/rust/libc&amp;quot; }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Adjusting build flags==&lt;br /&gt;
Build settings can be adjusted through the &amp;lt;code&amp;gt;KOS_RCG_RUSTFLAGS&amp;lt;/code&amp;gt; variable in the &amp;lt;code&amp;gt;environ.sh&amp;lt;/code&amp;gt; file under the &amp;lt;code&amp;gt;### Rust Flags&amp;lt;/code&amp;gt; section. &lt;br /&gt;
* To pass arguments to the &#039;&#039;&#039;rustc&#039;&#039;&#039; frontend, e.g. to disable debug assertions:&lt;br /&gt;
** &amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot; inline&amp;gt;export KOS_RCG_RUSTFLAGS=&amp;quot;${KOS_RCG_RUSTFLAGS} -Cdebug-assertions=no&amp;quot;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* To pass arguments to the GCC backend, e.g. to enable the &amp;lt;code&amp;gt;-freorder-blocks-algorithm=simple&amp;lt;/code&amp;gt; optimization for Rust code:&lt;br /&gt;
** &amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot; inline&amp;gt;export KOS_RCG_RUSTFLAGS=&amp;quot;${KOS_RCG_RUSTFLAGS} -Cllvm-args=-freorder-blocks-algorithm=simple&amp;quot;&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Darc</name></author>
	</entry>
	<entry>
		<id>https://dreamcast.wiki/wiki/index.php?title=Getting_Started_with_Dreamcast_development&amp;diff=3636</id>
		<title>Getting Started with Dreamcast development</title>
		<link rel="alternate" type="text/html" href="https://dreamcast.wiki/wiki/index.php?title=Getting_Started_with_Dreamcast_development&amp;diff=3636"/>
		<updated>2024-11-09T13:03:05Z</updated>

		<summary type="html">&lt;p&gt;Darc: /* Building the KOS examples */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;div style=&amp;quot;float:right;&amp;quot;&amp;gt;__TOC__&amp;lt;/div&amp;gt;&lt;br /&gt;
=Introduction=&lt;br /&gt;
This article will cover the entire beginning process: starting from zero to having a working dev environment with debug link (serial or IP) and self-booting CD-R. This guide will cover the process for the following platforms:&lt;br /&gt;
* &#039;&#039;&#039;Microsoft Windows 10&#039;&#039;&#039; via [https://learn.microsoft.com/en-us/windows/wsl/about Windows Subsystem for Linux]&lt;br /&gt;
** Users desiring a native Windows approach, see [[DreamSDK]] instead&lt;br /&gt;
* &#039;&#039;&#039;macOS&#039;&#039;&#039; on Intel or Apple Silicon systems with the [https://brew.sh/ Homebrew] package manager installed&lt;br /&gt;
* &#039;&#039;&#039;Linux&#039;&#039;&#039;-based systems&lt;br /&gt;
** &#039;&#039;&#039;Debian&#039;&#039;&#039;- and &#039;&#039;&#039;Ubuntu&#039;&#039;&#039;-based distributions using the default &#039;&#039;&#039;apt&#039;&#039;&#039; package manger&lt;br /&gt;
** &#039;&#039;&#039;Fedora&#039;&#039;&#039;-based distributions using the default &#039;&#039;&#039;dnf&#039;&#039;&#039; package manager&lt;br /&gt;
** &#039;&#039;&#039;Arch&#039;&#039;&#039;-based distributions using the default &#039;&#039;&#039;pacman&#039;&#039;&#039; package manager&lt;br /&gt;
** &#039;&#039;&#039;Alpine&#039;&#039;&#039;-based distributions using the default &#039;&#039;&#039;apk&#039;&#039;&#039; package manager&lt;br /&gt;
&lt;br /&gt;
===Need help?===&lt;br /&gt;
Important note: &#039;&#039;This guide aims to remain up to date and work on all of the above platforms, but keeping instructions for such a variety of platforms up-to-date can be difficult. If you run into any errors or other challenges while following this tutorial, or simply need clarification on any of the steps, feel free to ask for assistance on the [https://dcemulation.org/phpBB/viewforum.php?f=29 message board] or [https://discord.gg/RuKmpu7k Discord chat] and we would be happy to aid you and update the guide for the benefit of future readers and others in the community.&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
===Terms===&lt;br /&gt;
Before we get started, let&#039;s define several terms:&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;&#039;toolchain&#039;&#039;&#039; is a set of programs which turns your code into an executable file for your Dreamcast console. The toolchain includes:&lt;br /&gt;
* &#039;&#039;&#039;GCC&#039;&#039;&#039;, a compiler which supports C, C++, Objective-C, and more (see also [[Rust on Dreamcast]] and [[Programming language support]])&lt;br /&gt;
* &#039;&#039;&#039;binutils&#039;&#039;&#039;, an assembler and linker&lt;br /&gt;
* &#039;&#039;&#039;newlib&#039;&#039;&#039;, a C library&lt;br /&gt;
* &#039;&#039;&#039;gdb&#039;&#039;&#039;, a debugger&lt;br /&gt;
The toolchain includes a compiler for the Dreamcast&#039;s main SH4 CPU, and optionally a compiler for the ARM-based AICA sound processor. Your operating system may already have versions of these programs installed to compile code for your computer, but we will need to build a &amp;quot;cross-compiler&amp;quot; for compiling specifically for the Dreamcast. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;KallistiOS&#039;&#039;&#039; or &#039;&#039;KOS&#039;&#039; is an open source development library and pseudo-operating system for the Dreamcast console. It is the best documented and most widely used development kit in the homebrew community. KallistiOS&#039;s very flexible license allows both homebrew and commercial use with no restrictions other than a requirement to include credit for its use in your project, and indeed almost all commercially sold indie Dreamcast titles use it. There are others in existence, like [[libronin]] and [[libdream]], as well as the older development kits [[Katana]] and [[Windows CE]] created by Sega and Microsoft for use in retail games, but this guide will only cover the setup and use of KallistiOS. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;kos-ports&#039;&#039;&#039; is a repository including various libraries which integrate with KallistiOS. We will download and compile these libraries as well. &lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;&#039;debug link&#039;&#039;&#039; is a generic term referring to a hardware accessory to facilitate quickly running and debugging your programs. IP-based links include the Dreamcast&#039;s &#039;&#039;&#039;[[Broadband adapter]]&#039;&#039;&#039; and &#039;&#039;&#039;[[LAN adapter]]&#039;&#039;&#039; accessories, and serial-based links include the [[Coder&#039;s cable]], which is a cable that can connect the Dreamcast&#039;s serial port to your computer via USB or serial. This guide includes instructions for setting up and using the the Broadband adapter and a USB-based coder&#039;s cable.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;dc-tool&#039;&#039;&#039; and &#039;&#039;&#039;dcload&#039;&#039;&#039; are a pair of programs to facilitate using a debug link. &#039;&#039;dc-tool&#039;&#039; runs on your computer and links to a Dreamcast running &#039;&#039;dcload-ip&#039;&#039; or &#039;&#039;dcload-serial&#039;&#039;. With this setup, you can quickly load programs, read console feedback, load assets, transfer data, redirect I/O, handle exceptions, debug problems, and so forth.&lt;br /&gt;
&lt;br /&gt;
=Choosing a debug link solution=&lt;br /&gt;
If you are building the toolchain for the purpose of building existing programs from source with little to no modifications, then a debug link setup might not be necessary for you. You may simply build programs to burn directly to CD-R. However, if you are planning to actively develop for the Dreamcast, then a debug link is a critical component. While Dreamcast emulators are mature and accurate enough to play the vast majority of the system&#039;s games library without issue, many critical bugs may show up on a real Dreamcast system, but not on a Dreamcast emulator. Therefore, it is highly recommended to test on a real system as much as possible. It&#039;s also possible to load software off of a [[Serial SD card adapter]], but without an active link to a computer, debugging and stepping through programs as they execute is significantly more challenging.&lt;br /&gt;
&lt;br /&gt;
Presented below is a table comparing the different options available for a debug link. Due to the cost, potential buyers may want to factor in the ability to play multiplayer games with their purchase. Thus, for comparison, we have included information about the [[Modem]] with [[DreamPi]] as well, but understand that the Modem with DreamPi cannot be used as a debug link.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
!colspan=&amp;quot;6&amp;quot; |Comparison of various Dreamcast connectivity options&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#c0c0c0;&amp;quot; width=&amp;quot;150&amp;quot; | Device: &lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; width=&amp;quot;400&amp;quot; | [[Broadband adapter]] (HIT-400 or HIT-401) &amp;lt;br /&amp;gt;Realtek RTL8139C chipset&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; width=&amp;quot;400&amp;quot; | [[LAN adapter]] (HIT-300) &amp;lt;br /&amp;gt;Fujitsu MB86967 chipset&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; width=&amp;quot;400&amp;quot; | [[Modem]] with [[DreamPi]]&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; width=&amp;quot;400&amp;quot; | USB [[Coder&#039;s cable]]&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; width=&amp;quot;400&amp;quot; | Serial [[Coder&#039;s cable]]&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Useful for dev? || Yes, supports dcload-ip || Yes, supports dcload-ip,&amp;lt;br/&amp;gt;but BBA is superior and cheaper || No, only useful for online multiplayer gaming || Yes, supports dcload-serial || Yes, supports dcload-serial&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Cost || $100 - $200 and up on used markets || $200 and up on used markets,&amp;lt;br/&amp;gt;due to extreme rarity || Kit prices vary, around $100 || See [[Coder&#039;s cable]] for vendors&amp;lt;br /&amp;gt;darc sells for $45, RetroOnyx sells for $85 || Varies on used markets, uncommonly sold&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Can make DIY? || No || No || Yes || Yes || Yes&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Performance || Up to 100 megabits/s || Up to 10 megabits/s || Up to 56 kilobits/s || Up to 1500 kilobits/s || Up to 120 kilobits/s&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Games support || Some games: Phantasy Star Online, Quake III Arena, Toy Racer, POD SpeedZone, Propellor Arena, Unreal Tournament&amp;lt;br /&amp;gt;Some browsers: Broadband Passport, PlanetWeb 3.0 || No games&amp;lt;br /&amp;gt;One browser: Dream Passport for LAN || All multiplayer games with network support&amp;lt;br /&amp;gt;All web browsers || NO multiplayer games support || NO multiplayer games support &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;background-color:#d0d0d0;&amp;quot; | Homebrew support || Homebrew utilities like dcload-ip || Homebrew utilities like dcload-ip || Homebrew utilities don&#039;t support, only multiplayer games || Homebrew utilities like dcload-serial || Homebrew utilities like dcload-serial &lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=Setting up and compiling the toolchain with the dc-chain script=&lt;br /&gt;
===Dependencies===&lt;br /&gt;
First, we&#039;ll need to install dependencies before building the toolchain. Below we have provided commands to install these dependencies on various systems. Many of the packages will likely already be installed on your system, but we have provided an exhaustive list for good measure.&lt;br /&gt;
====macOS 13 Ventura on an Intel or Apple Silicon processor====&lt;br /&gt;
First, make sure you install Apple Xcode, including the Command Line tools. You will also need to install several other packages for which we&#039;ll include instructions assuming you have installed the [https://brew.sh/ Homebrew] package manager on your system.&lt;br /&gt;
 brew install wget gettext texinfo gmp mpfr libmpc libelf jpeg-turbo libpng cmake meson libisofs&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;Important Note for Apple Silicon users&#039;&#039;: On Apple Silicon, Homebrew installs libraries to a path not included by default by the compiler. If you haven&#039;t added these to your &#039;&#039;&#039;~/.zprofile&#039;&#039;&#039;, then add the following lines now and reload your session (or run them in your Terminal session whenever you compile KOS):&lt;br /&gt;
 export CPATH=/opt/homebrew/include&lt;br /&gt;
 export LIBRARY_PATH=/opt/homebrew/lib&lt;br /&gt;
&lt;br /&gt;
====Debian/Ubuntu-based Linux====&lt;br /&gt;
 sudo apt-get update&lt;br /&gt;
 sudo apt install gawk patch bzip2 tar make libgmp-dev libmpfr-dev libmpc-dev gettext wget libelf-dev texinfo bison flex sed git build-essential diffutils curl libjpeg-dev libpng-dev python3 pkg-config cmake libisofs-dev meson ninja-build rake&lt;br /&gt;
&lt;br /&gt;
====Fedora-based Linux====&lt;br /&gt;
 sudo dnf install gawk patch bzip2 tar make gmp-devel mpfr-devel libmpc-devel gettext wget elfutils-libelf-devel texinfo bison flex sed git diffutils curl libjpeg-turbo-devel libpng-devel gcc-c++ python3 rubygem-rake meson ninja-build&lt;br /&gt;
&lt;br /&gt;
====Arch-based Linux====&lt;br /&gt;
 sudo pacman -S --needed gawk patch bzip2 tar make gmp mpfr libmpc gettext wget libelf texinfo bison flex sed git diffutils curl libjpeg-turbo libpng python3 meson ruby-rake&lt;br /&gt;
&lt;br /&gt;
====Alpine-based Linux====&lt;br /&gt;
 sudo apk --update add build-base patch bash texinfo gmp-dev libjpeg-turbo-dev libpng-dev elfutils-dev curl wget python3 git ruby-rake&lt;br /&gt;
&lt;br /&gt;
====Other Linux distributions====&lt;br /&gt;
If you&#039;re using a different Linux- or Unix-based system besides the one above, you may need to reference your distribution&#039;s package database and package manager documentation for the equivalent package names and commands necessary for your system.&lt;br /&gt;
&lt;br /&gt;
===Creating a space for your toolchain installation===&lt;br /&gt;
Create the path where we&#039;ll install the toolchain and KOS, and grant it the proper permissions:&lt;br /&gt;
 sudo mkdir -p /opt/toolchains/dc&lt;br /&gt;
 sudo chmod -R 755 /opt/toolchains/dc&lt;br /&gt;
 sudo chown -R $(id -u):$(id -g) /opt/toolchains/dc&lt;br /&gt;
===Cloning the KallistiOS git repository===&lt;br /&gt;
Next, clone the git repository for the latest &#039;&#039;&#039;2.1.0&#039;&#039;&#039; stable version of KallistiOS: &lt;br /&gt;
 git clone https://github.com/KallistiOS/KallistiOS.git -b v2.1.0 /opt/toolchains/dc/kos&lt;br /&gt;
&lt;br /&gt;
Alternatively, you may clone the latest unstable version, which contains the latest changes, but may also contain bugs or issues:&lt;br /&gt;
 git clone https://github.com/KallistiOS/KallistiOS.git -b master /opt/toolchains/dc/kos&lt;br /&gt;
&lt;br /&gt;
===Configuring the dc-chain script===&lt;br /&gt;
Enter the dc-chain directory:&lt;br /&gt;
 cd /opt/toolchains/dc/kos/utils/dc-chain&lt;br /&gt;
&lt;br /&gt;
You may now configure the options to your liking by creating a &amp;lt;code&amp;gt;Makefile.cfg&amp;lt;/code&amp;gt; file in this directory. Copy or rename the &amp;lt;code&amp;gt;Makefile.default.cfg&amp;lt;/code&amp;gt; file to &amp;lt;code&amp;gt;Makefile.cfg&amp;lt;/code&amp;gt; and then use a text editor to edit this file&#039;s settings to your liking. For example, you may like to alter the &amp;lt;code&amp;gt;makejobs&amp;lt;/code&amp;gt; option to the number of threads available on your CPU to speed up the compilation. However, if you run into errors during compilation, you may want to set &amp;lt;code&amp;gt;makejobs=1&amp;lt;/code&amp;gt;, as on some operating systems the toolchain may fail to build with a higher setting.&lt;br /&gt;
&lt;br /&gt;
We will leave the default &#039;&#039;&#039;stable&#039;&#039;&#039; profile for the toolchain, which currently uses GCC 13.2. For advanced users, other profiles are available to you; read the &amp;lt;code&amp;gt;README.md&amp;lt;/code&amp;gt; file in the dc-chain directory for more information if you are interested. If you are unsure about options in the &amp;lt;code&amp;gt;Makefile.cfg&amp;lt;/code&amp;gt;, just leave the defaults alone.&lt;br /&gt;
&lt;br /&gt;
===Downloading and compiling the toolchain===&lt;br /&gt;
Now we will run a script to download files and compile the toolchain. At this point, we have the option of building both the main CPU SH4 compiler and the AICA sound processor ARM compiler, or we can skip the ARM compiler and just build the SH4 compiler. Thankfully, KallistiOS includes a prebuilt sound driver, so the ARM compiler is only necessary if you&#039;re wanting to make changes to the sound driver or write custom code to run on the sound processor.&lt;br /&gt;
To build &#039;&#039;&#039;only the SH4 compiler&#039;&#039;&#039;:&lt;br /&gt;
 make&lt;br /&gt;
To build &#039;&#039;&#039;both&#039;&#039;&#039; the SH4 and the ARM compilers:&lt;br /&gt;
 make all&lt;br /&gt;
This will download and unpack the relevant necessary files and then begin the compilation process. The compilation can take anywhere from minutes to a few hours depending on your CPU and number of threads available. When successfully finished, the toolchains will be ready. If you used the commands above, the &#039;&#039;&#039;gdb&#039;&#039;&#039; debugger will have been built as well.&lt;br /&gt;
&lt;br /&gt;
===Cleaning up temporary files===&lt;br /&gt;
After building everything, you can clean up the extraneous files in your dc-chain directory by entering:&lt;br /&gt;
 make clean&lt;br /&gt;
&lt;br /&gt;
=Configuring and compiling KOS and kos-ports=&lt;br /&gt;
===Setting up the environment settings===&lt;br /&gt;
Enter the KOS directory:&lt;br /&gt;
 cd /opt/toolchains/dc/kos&lt;br /&gt;
Copy the pre-made environment script into place:&lt;br /&gt;
 cp doc/environ.sh.sample environ.sh &lt;br /&gt;
For most users, the default settings will suffice. However, advanced users may edit the environ.sh to your liking if you&#039;d like to change compile flags or alter paths. If you&#039;d like to have multiple KOS versions installed or multiple toolchain versions installed, you can set up different environ.sh files corresponding to these different configurations by altering the paths. Run the source command on the desired environ.sh file to select that configuration prior to compiling your project. &lt;br /&gt;
&lt;br /&gt;
You will need to run the source command to apply the KOS environment settings to your currently running shell. Run the following now, &#039;&#039;&#039;and&#039;&#039;&#039; &#039;&#039;whenever&#039;&#039; you open a new shell to work on Dreamcast projects:&lt;br /&gt;
 source /opt/toolchains/dc/kos/environ.sh&lt;br /&gt;
&lt;br /&gt;
===Building KOS===&lt;br /&gt;
Build KOS:&lt;br /&gt;
 make&lt;br /&gt;
KOS is now built.&lt;br /&gt;
===Building kos-ports===&lt;br /&gt;
Clone the kos-ports repository to your system:&lt;br /&gt;
 git clone --recursive https://github.com/KallistiOS/kos-ports /opt/toolchains/dc/kos-ports&lt;br /&gt;
Run the script to build all of the included ports:&lt;br /&gt;
 /opt/toolchains/dc/kos-ports/utils/build-all.sh&lt;br /&gt;
kos-ports is now built.&lt;br /&gt;
&lt;br /&gt;
===Building the KOS examples===&lt;br /&gt;
Enter the KOS examples directory:&lt;br /&gt;
 cd /opt/toolchains/dc/kos/examples&lt;br /&gt;
Build the examples:&lt;br /&gt;
 make&lt;br /&gt;
All of the example programs provided with KallistiOS are now built.&lt;br /&gt;
&lt;br /&gt;
=Further=&lt;br /&gt;
At this point, you will have a cross-compiler built so that you can generate programs for your Dreamcast from code. KallistiOS, the Dreamcast development library and operating system, is built, as is its associated packages within kos-ports, and all of its example programs.&lt;br /&gt;
&lt;br /&gt;
At this point, you will likely want to view some of the following articles:&lt;br /&gt;
* Setting up an IDE for Dreamcast development&lt;br /&gt;
** [[CLion Debugging]]&lt;br /&gt;
** [[Visual Studio Code]]&lt;br /&gt;
* [[Creating a first project with KallistiOS]] (&#039;&#039;&#039;TODO&#039;&#039;&#039;: Explain how to create a new DC project folder with Makefile, adding an external library, create a basic program, and compile it)&lt;br /&gt;
* [[Using a debug link]]&lt;br /&gt;
* [[Burning an example to CD-R]]&lt;br /&gt;
&lt;br /&gt;
=Running an example program through a debug link=&lt;br /&gt;
&#039;&#039;&#039;TODO&#039;&#039;&#039;: &#039;&#039;Give a tutorial on compiling dcload/dc-tool, setting up a serial, USB, or IP debug link, and running 2ndmix demo on real hardware.&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Download and burn the [[:File:Dcload-2023-06-22.zip|latest versions of dcload-ip or dcload-serial]] -- the IP version includes improved DHCP support, so there is no longer a need to configure things beforehand. &lt;br /&gt;
 &lt;br /&gt;
Run one of the examples from the &amp;lt;code&amp;gt;kos/examples/dreamcast&amp;lt;/code&amp;gt; directory with one of the following commands:&lt;br /&gt;
 dc-tool-ip -t &amp;lt;dreamcast IP address&amp;gt; -x example.elf&lt;br /&gt;
&lt;br /&gt;
 dc-tool-ser -t &amp;lt;serial device path&amp;gt; -x example.elf&lt;br /&gt;
&lt;br /&gt;
Run &amp;lt;code&amp;gt;dc-tool-ip&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;dc-tool-ser&amp;lt;/code&amp;gt; without any parameters to get additional options.&lt;br /&gt;
&lt;br /&gt;
=Burning an example program to CD-R=&lt;br /&gt;
&#039;&#039;&#039;TODO&#039;&#039;&#039;: &#039;&#039;Explain how to build mkdcdisc and write 2ndmix demo to CD-R and run on a Dreamcast console&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[https://gitlab.com/simulant/mkdcdisc mkdcdisc] can be used to easily generate a burnable self-boot CDI image.&lt;br /&gt;
Dependencies required:&lt;br /&gt;
* Homebrew: &amp;lt;code&amp;gt;meson libisofs pkg-config&amp;lt;/code&amp;gt;&lt;br /&gt;
* Debian/Ubuntu:  &amp;lt;code&amp;gt;libisofs-dev meson ninja-build&amp;lt;/code&amp;gt;&lt;br /&gt;
* Fedora: &amp;lt;code&amp;gt;meson ninja-build&amp;lt;/code&amp;gt;&lt;br /&gt;
* Arch: &amp;lt;code&amp;gt;meson&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Build &amp;lt;code&amp;gt;mkdcdisc&amp;lt;/code&amp;gt;:&lt;br /&gt;
 git clone https://gitlab.com/simulant/mkdcdisc.git&lt;br /&gt;
 cd mkdcdisc&lt;br /&gt;
 meson setup builddir&lt;br /&gt;
 meson compile -C builddir&lt;br /&gt;
 ./builddir/mkdcdisc -h&lt;br /&gt;
and create a CDI image from your compiled ELF like so:&lt;br /&gt;
 mkdcdisc -e MyProgram.elf -o MyProgram.cdi&lt;br /&gt;
Then you can burn the CDI file using DiscJuggler (Windows-only, but also works through [https://www.winehq.org/ WINE]), ImgBurn with the CDI plugin, or the cdiburn *nix script floating around out there. (document this better)&lt;/div&gt;</summary>
		<author><name>Darc</name></author>
	</entry>
	<entry>
		<id>https://dreamcast.wiki/wiki/index.php?title=Rust_on_Dreamcast&amp;diff=3635</id>
		<title>Rust on Dreamcast</title>
		<link rel="alternate" type="text/html" href="https://dreamcast.wiki/wiki/index.php?title=Rust_on_Dreamcast&amp;diff=3635"/>
		<updated>2024-11-09T03:36:04Z</updated>

		<summary type="html">&lt;p&gt;Darc: /* Examples */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[File:Rust-dc-logo.png|thumb|Ferris holding his Dreamcast controller]]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Rust&#039;&#039;&#039; is a popular modern programming language focusing on performance, memory safety, and program correctness. As a &amp;quot;blazingly fast&amp;quot; low-level language, it is viable for running on a Dreamcast console. Doing so presents a bit of a challenge, however, as the official Rust compiler is based on the [https://llvm.org/ LLVM] toolchain infrastructure, which does not support the Dreamcast CPU&#039;s SuperH architecture. Dreamcast programming is instead typically done with [https://gcc.gnu.org/ GCC], the GNU Compiler Collection. There are currently two viable solutions to this challenge:&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;rustc_codegen_gcc&#039;&#039;&#039;, the current preferred method: An experimental codegen backend for the official Rust &#039;&#039;&#039;rustc&#039;&#039;&#039; compiler which interfaces with GCC&#039;s libgccjit API&lt;br /&gt;
* &#039;&#039;&#039;gccrs&#039;&#039;&#039;: an experimental Rust frontend for GCC&lt;br /&gt;
&lt;br /&gt;
Neither solution is complete at this time, but &#039;&#039;&#039;rustc_codegen_gcc&#039;&#039;&#039; is much further along and is quite usable with some patience with its current limitations. On the other hand, &#039;&#039;&#039;gccrs&#039;&#039;&#039; can compile for Dreamcast, but is in an earlier state. Below we will focus on using &#039;&#039;&#039;rustc_codegen_gcc&#039;&#039;&#039;. For more information on using gccrs, see the [[gccrs]] page.&lt;br /&gt;
&lt;br /&gt;
=Current Status=&lt;br /&gt;
* The full &#039;&#039;&#039;std&#039;&#039;&#039; library is supported.&lt;br /&gt;
* A [https://github.com/dreamcast-rs/KallistiOS custom version of KallistiOS] v2.1.0 (latest stable release) is used.&lt;br /&gt;
** This version includes minor changes for Rust and a beta libpthread addon layer to interface with the Rust standard library.&lt;br /&gt;
** Wrappers are provided to invoke &#039;&#039;&#039;cargo&#039;&#039;&#039; and &#039;&#039;&#039;rustc&#039;&#039;&#039; in a familiar manner to compile projects.&lt;br /&gt;
* A [https://github.com/dreamcast-rs/kos-sys &#039;&#039;&#039;kos-sys&#039;&#039;&#039;] raw/unsafe bindings crate is provided for easily linking KallistiOS and its unsafe C API into Rust crates. This crate is in early stages and in active development.&lt;br /&gt;
&amp;amp; A [https://github.com/dreamcast-rs/kos-rs &#039;&#039;&#039;kos-rs&#039;&#039;&#039;] Rust crate is available but is also in early stages.&lt;br /&gt;
** Documentation for the bindings is available at [https://kos-rs.dreamcast.wiki/ kos-rs.dreamcast.wiki].&lt;br /&gt;
** If bindings do not yet exist for desired functionality, wrapping of KallistiOS C functions is required to get proper use of KallistiOS at this time. See the [https://doc.rust-lang.org/nomicon/ffi.html Foreign Function Interface page] in the Rustonomicon for further understanding. &lt;br /&gt;
** A custom [https://github.com/dreamcast-rs/libc/tree/libc-0.2-kos &#039;&#039;&#039;libc&#039;&#039;&#039; crate with KallistiOS support] is provided.&lt;br /&gt;
* Code generation uses &#039;&#039;&#039;rustc_codegen_gcc&#039;&#039;&#039;, so its limitations apply here. See the [https://blog.antoyo.xyz/ development blog] for more information on its progress.&lt;br /&gt;
** rustc_codegen_gcc is pinned to a particular Rust nightly version, but is synced regularly. The current version in use is the &#039;&#039;&#039;2024-08-10&#039;&#039;&#039; nightly.&lt;br /&gt;
** A [https://github.com/dreamcast-rs/rust/tree/kos-2024-08-10 KallistiOS-patched version of the latest nightly] is maintained and updated regularly. &lt;br /&gt;
** Panic unwinding, debug info, LTO, etc. are not currently available.&lt;br /&gt;
** Architectures dependent on rustc_codegen_gcc are not yet able to be added to the rustc frontend, so a workaround is necessary: MIPS is falsely selected for KallistiOS, causing rustc to emit objects with a MIPS header and a provided wrapper is used when linking to rewrite these MIPS headers to SuperH before passing the compiled objects to the linker.&lt;br /&gt;
* A separate [https://github.com/rust-lang/gcc development version of the GCC toolchain] must be used.&lt;br /&gt;
** This version of GCC contains the latest libgccjit patches necessary to interface with rustc_codegen_gcc, and will be built to work with the custom KallistiOS&#039;s libpthread layer.&lt;br /&gt;
** New updates to rustc_codegen_gcc often require new updates to GCC, so recompiling GCC regularly will be necessary to keep up with the latest updates.&lt;br /&gt;
** The development version of GCC does not always compile well on every platform. Using a modern Linux platform is recommended for maximum compatibility.&lt;br /&gt;
** The custom KallistiOS and GCC environment can still be used to compile code or projects written in C, C++, etc.&lt;br /&gt;
* KallistiOS and all related libraries and tools must be compiled using the &amp;lt;code&amp;gt;-m4-single&amp;lt;/code&amp;gt; ABI. This is already set up for you in the guide below, but if external libraries are being linked to Rust projects, make sure that code is compiled using the proper ABI.&lt;br /&gt;
&lt;br /&gt;
=Setting up a Rust environment for Dreamcast development=&lt;br /&gt;
The following guide will provide instructions for setting up a Rust development environment for Dreamcast. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;If you run into any errors or other challenges while following this tutorial, or simply need clarification on any of the steps, feel free to ask for assistance on the [https://dcemulation.org/phpBB/viewforum.php?f=29 message board] or [https://discord.gg/wDEfkUh63h our Discord server] and we would be happy to aid you and update the guide for the benefit of future readers and others in the community.&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
==Prerequisites==&lt;br /&gt;
First, we will need to set up the prerequisites.&lt;br /&gt;
&lt;br /&gt;
* You must have the [https://rustup.rs/ rustup] tool installed for your operating system to manage Rust toolchain installations. It will automatically manage the latest Rust nightly installations for you. You cannot use a Rust toolchain that may be provided by your operating system vendor.&lt;br /&gt;
* Install the dependency packages for your operating system. These are listed in the &#039;&#039;&#039;Dependencies&#039;&#039;&#039; section on the [[Getting Started with Dreamcast development#Dependencies]] page, with lists provided for most common operating systems.&lt;br /&gt;
* We will install our custom Rust toolchain environment within &amp;lt;code&amp;gt;/opt/toolchains/dc/rust&amp;lt;/code&amp;gt;. If it does not already exist, create the &amp;lt;code&amp;gt;/opt/toolchains/dc&amp;lt;/code&amp;gt; directory and grant it proper permissions using the following commands. &lt;br /&gt;
 sudo mkdir -p /opt/toolchains/dc&lt;br /&gt;
 sudo chmod -R 755 /opt/toolchains/dc&lt;br /&gt;
 sudo chown -R $(id -u):$(id -g) /opt/toolchains/dc&lt;br /&gt;
* Clone the [https://github.com/dreamcast-rs/rust-for-dreamcast/ Rust for Dreamcast] repo containing necessary support files to &amp;lt;code&amp;gt;/opt/toolchains/dc/rust&amp;lt;/code&amp;gt;:&lt;br /&gt;
 git clone https://github.com/dreamcast-rs/rust-for-dreamcast.git /opt/toolchains/dc/rust&lt;br /&gt;
&lt;br /&gt;
==Installing our custom KallistiOS and GCC toolchain==&lt;br /&gt;
Clone the git repository for our custom Rust-patched version of KallistiOS v2.1.0 stable:&lt;br /&gt;
 git clone https://github.com/dreamcast-rs/KallistiOS /opt/toolchains/dc/rust/kos&lt;br /&gt;
&lt;br /&gt;
Enter the dc-chain directory:&lt;br /&gt;
 cd /opt/toolchains/dc/rust/kos/utils/dc-chain&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;Makefile.default.cfg&amp;lt;/code&amp;gt; file in this directory has been customized for you, selecting the proper toolchain profile, paths, and build options necessary for building a Rust toolchain using this guide. It is not recommended to adjust the defaults.&lt;br /&gt;
&lt;br /&gt;
To build the GCC toolchain using 2 CPU cores, simply type:&lt;br /&gt;
 make makejobs=2&lt;br /&gt;
Adjust the &amp;lt;code&amp;gt;makejobs=...&amp;lt;/code&amp;gt; value to use the number of CPU cores available to you on your system in order to speed up compilation. This may take a while to build, so be patient.&lt;br /&gt;
&lt;br /&gt;
Once the GCC toolchain is built, source the &amp;lt;code&amp;gt;environ.sh&amp;lt;/code&amp;gt; KallistiOS environment settings. A custom &amp;lt;code&amp;gt;environ.sh&amp;lt;/code&amp;gt; file has been provided for you, containing settings pre-adjusted for Rust development using this guide.&lt;br /&gt;
&lt;br /&gt;
You will need to run the source command to apply the KallistiOS environment settings to your currently running shell. Run the following now, &#039;&#039;&#039;and&#039;&#039;&#039; &#039;&#039;whenever&#039;&#039; you open a new shell to work on Dreamcast projects:&lt;br /&gt;
 source /opt/toolchains/dc/rust/misc/environ.sh&lt;br /&gt;
&lt;br /&gt;
Enter the KallistiOS directory:&lt;br /&gt;
 cd /opt/toolchains/dc/rust/kos&lt;br /&gt;
&lt;br /&gt;
Build KallistiOS:&lt;br /&gt;
 make&lt;br /&gt;
&lt;br /&gt;
==Building rustc_codegen_gcc and Rust sysroot==&lt;br /&gt;
Now that we have a working KallistiOS environment suitable for Rust development set up, we can build the Rust compiler compiler and sysroot components. A script is provided which will download the necessary components from the respective repositories and compile them for you. Run the installer script:&lt;br /&gt;
&lt;br /&gt;
 /opt/toolchains/dc/rust/misc/install-rust.sh&lt;br /&gt;
&lt;br /&gt;
If all goes well, you&#039;ll see &#039;&#039;&#039;Rust for KallistiOS/Dreamcast installed!&#039;&#039;&#039; message!&lt;br /&gt;
&lt;br /&gt;
==Building KOS ports libraries==&lt;br /&gt;
If desired, you may also built the KOS ports collection of libraries for use with your Rust environment. It is recommended to maintain a separate KOS ports installation for linking with Rust projects in &amp;lt;code&amp;gt;/opt/toolchains/dc/rust/kos-ports&amp;lt;/code&amp;gt;. This is because the Rust environment is using the &amp;lt;code&amp;gt;-m4-single&amp;lt;/code&amp;gt; ABI, but the KallistiOS default installation will be using the &amp;lt;code&amp;gt;-m4-single-only&amp;lt;/code&amp;gt;, which cannot be mixed and matched.&lt;br /&gt;
&lt;br /&gt;
Clone the kos-ports repository to your system:&lt;br /&gt;
 git clone https://github.com/KallistiOS/kos-ports /opt/toolchains/dc/rust/kos-ports&lt;br /&gt;
An individual port can be built and installed by entering its directory and running the proper command. For example, installing GLdc:&lt;br /&gt;
 cd /opt/toolchains/dc/rust/kos-ports/libGL&lt;br /&gt;
 make install&lt;br /&gt;
Or, instead, you may run the script to build all of the included ports:&lt;br /&gt;
 /opt/toolchains/dc/rust/kos-ports/utils/build-all.sh&lt;br /&gt;
&lt;br /&gt;
=Using Rust for Dreamcast=&lt;br /&gt;
If all went well, you will now have a working Rust for Dreamcast environment!&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;cargo&#039;&#039;&#039; can be invoked to target Dreamcast using the &amp;lt;code&amp;gt;kos-cargo&amp;lt;/code&amp;gt; command.&lt;br /&gt;
** &amp;lt;code&amp;gt;kos-cargo run&amp;lt;/code&amp;gt; can be used to build and run code on a Dreamcast console using [[dcload-ip]] or [[dcload-serial]]. You will need to set up the &amp;lt;code&amp;gt;KOS_LOADER&amp;lt;/code&amp;gt; variable with the necessary command in the &amp;lt;code&amp;gt;/opt/toolchains/dc/rust/misc/environ.sh&amp;lt;/code&amp;gt; file (and then re-source the file for the change to take effect).&lt;br /&gt;
* &#039;&#039;&#039;rustc&#039;&#039;&#039; can be invoked to target Dreamcast using the &amp;lt;code&amp;gt;kos-rustc&amp;lt;/code&amp;gt; command.&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
Examples are included with the Rust for Dreamcast repo to help you get started.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/hello-cargo &amp;lt;code&amp;gt;hello-cargo&amp;lt;/code&amp;gt;] demonstrates how to create a simple &amp;quot;Hello, world!&amp;quot; application using cargo.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/hello-make &amp;lt;code&amp;gt;hello-make&amp;lt;/code&amp;gt;] demonstrates how to create a simple &amp;quot;Hello, world!&amp;quot; application using standard Makefiles.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/gldc-cube &amp;lt;code&amp;gt;gldc-cube&amp;lt;/code&amp;gt;] demonstrates a Rust project using the GLdc KOS port.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/addlib &amp;lt;code&amp;gt;addlib&amp;lt;/code&amp;gt;] demonstrates how to create a Rust library that can be included with a KallistiOS project.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/atomics &amp;lt;code&amp;gt;atomics&amp;lt;/code&amp;gt;] demonstrates the use of atomic functions.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/filesystem-io &amp;lt;code&amp;gt;filesystem-io&amp;lt;/code&amp;gt;] demonstrates listing directory contents and opening files.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/network-time &amp;lt;code&amp;gt;network-time&amp;lt;/code&amp;gt;] demonstrates displaying the system time and retrieving the current UTC time from an NTP server using the [[Broadband adapter]] or [[LAN adapter]].&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/threads &amp;lt;code&amp;gt;threads&amp;lt;/code&amp;gt;] demonstrates the use of threads.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/tokio-async &amp;lt;code&amp;gt;tokio-async&amp;lt;/code&amp;gt;] demonstrates the use of the &#039;&#039;&#039;tokio&#039;&#039;&#039; async runtime.&lt;br /&gt;
&lt;br /&gt;
==Creating a new Rust project with Cargo==&lt;br /&gt;
First, we&#039;ll demonstrate creating a new &amp;quot;Hello, world!&amp;quot; project with &amp;lt;code&amp;gt;kos-cargo&amp;lt;/code&amp;gt;. This will follow the [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/hello-cargo &amp;lt;code&amp;gt;cargo-hello&amp;lt;/code&amp;gt;] example included in the Rust-for-Dreamcast repo. &lt;br /&gt;
&lt;br /&gt;
In a directory of your choosing, let&#039;s invoke &amp;lt;code&amp;gt;kos-cargo&amp;lt;/code&amp;gt; to create a new project and then enter the directory:&lt;br /&gt;
 kos-cargo new hello&lt;br /&gt;
 cd hello&lt;br /&gt;
&lt;br /&gt;
Let&#039;s add our kos-rs crate to gain access to current KallistiOS bindings. Open &amp;lt;code&amp;gt;Cargo.toml&amp;lt;/code&amp;gt; in your text editor and add:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;toml&amp;quot;&amp;gt;&lt;br /&gt;
[dependencies]&lt;br /&gt;
kos = { package = &amp;quot;kos-rs&amp;quot;, git = &amp;quot;https://github.com/dreamcast-rs/kos-rs&amp;quot; }&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we can open up &amp;lt;code&amp;gt;src/main.rs&amp;lt;/code&amp;gt; and write our &amp;quot;Hello, world!&amp;quot; example code:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; line&amp;gt;&lt;br /&gt;
#![no_main]&lt;br /&gt;
&lt;br /&gt;
#[no_mangle]&lt;br /&gt;
extern &amp;quot;C&amp;quot; fn main(_argc: isize, _argv: *const *const u8) -&amp;gt; isize {&lt;br /&gt;
    println!(&amp;quot;Hello, world from Rust!&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
    0&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;#![no_main]&amp;lt;/syntaxhighlight&amp;gt; tells Rust that we will not have Rust use a &amp;lt;code&amp;gt;main&amp;lt;/code&amp;gt; function as an entry point -- this will be handled by KallistiOS.&lt;br /&gt;
* &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;#[no_mangle]&amp;lt;/syntaxhighlight&amp;gt; tells Rust to disable name mangling so that the &amp;lt;code&amp;gt;main&amp;lt;/code&amp;gt; function can be used by KallistiOS&#039;s C code.&lt;br /&gt;
* Finally, we have a &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;fn main&amp;lt;/syntaxhighlight&amp;gt; with the function signature of a typical C &amp;lt;code&amp;gt;main&amp;lt;/code&amp;gt; function, containing a basic &amp;quot;Hello, world!&amp;quot; exclamation.&lt;br /&gt;
&lt;br /&gt;
Now we can use &amp;lt;code&amp;gt;kos-cargo build&amp;lt;/code&amp;gt; to build our project. If all goes well, there will be a &amp;lt;code&amp;gt;target/sh-elf/debug/hello.elf&amp;lt;/code&amp;gt; file that can be sent to the Dreamcast with &amp;lt;code&amp;gt;dc-tool&amp;lt;/code&amp;gt;.&lt;br /&gt;
If you have &amp;lt;code&amp;gt;KOS_LOADER&amp;lt;/code&amp;gt; set in the &amp;lt;code&amp;gt;environ.sh&amp;lt;/code&amp;gt; file, you can invoke it directly with &amp;lt;code&amp;gt;kos-cargo run&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Creating a Rust project using kos-ports libraries==&lt;br /&gt;
[[File:Rust-Cube rustc codegen-gcc demo.gif|thumb|cube example in action]]&lt;br /&gt;
The [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/gldc-cube &amp;lt;code&amp;gt;gldc-cube&amp;lt;/code&amp;gt;] example demonstrates creating a rotating 3D cube using Rust as the primary language, while calling C functions provided by the &#039;&#039;&#039;GLdc&#039;&#039;&#039; library available in kos-ports. This project&#039;s initial setup is done the same as the above &amp;lt;code&amp;gt;hello&amp;lt;/code&amp;gt; example.&lt;br /&gt;
&lt;br /&gt;
For this example, we&#039;ll need to convert JPEG textures to VQ-encoded textures at compile time using the &amp;lt;code&amp;gt;vqenc&amp;lt;/code&amp;gt; utility includes with KallistiOS. To do this, we&#039;ll add a &amp;lt;code&amp;gt;build.rs&amp;lt;/code&amp;gt; file to the root of the crate with the following code (abridged to show the conversion of only one texture):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot;&amp;gt;&lt;br /&gt;
fn main() {&lt;br /&gt;
    let kos_base = std::env::var(&amp;quot;KOS_BASE&amp;quot;).expect(&amp;quot;Missing $KOS_BASE -- KallistiOS environment not sourced!&amp;quot;);&lt;br /&gt;
    let vqenc_cmd = kos_base + &amp;quot;/utils/vqenc/vqenc&amp;quot;;&lt;br /&gt;
    Command::new(&amp;amp;vqenc_cmd)&lt;br /&gt;
        .arg(&amp;quot;-t&amp;quot;)&lt;br /&gt;
        .arg(&amp;quot;-v&amp;quot;)&lt;br /&gt;
        .arg(&amp;quot;rsrc/tex_claw.jpg&amp;quot;)&lt;br /&gt;
        .output()&lt;br /&gt;
        .expect(&amp;quot;vqenc on tex_claw.jpg failed!&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
The texture files are then included in our project using the &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;include_bytes!&amp;lt;/syntaxhighlight&amp;gt; macro.&lt;br /&gt;
&lt;br /&gt;
We will need to link the GLdc library in this example, but we don&#039;t need to do anything special to add the paths to this library, because adding the common paths to KallistiOS libraries is already done for us in the kos-rs crate. However, if in your project you need to include a separate unique library path, you can add it in your &amp;lt;code&amp;gt;build.rs&amp;lt;/code&amp;gt; like so:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot;&amp;gt;&lt;br /&gt;
println!(&amp;quot;cargo:rustc-link-search=native={}&amp;quot;, lib_path);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The workings of this example&#039;s source code are too great to detail here line-by-line, but the example demonstrates how to use GLdc via the [https://github.com/dreamcast-rs/gldc-sys gldc-sys] crate, which contains unsafe/raw bindings to GLdc. Check out this crate&#039;s source code for an example of declaring and binding external C functions, constants, and structures so they can be used in Rust code. Since the entirety of the gldc-cube example uses C FFI functions without safe wrappers, the &amp;lt;code&amp;gt;main()&amp;lt;/code&amp;gt; source is wrapped in &amp;lt;code&amp;gt;unsafe {}&amp;lt;/code&amp;gt;. In the future, this would be much less necessary as higher level safe Rust bindings to KallistiOS and other libraries become mature.&lt;br /&gt;
&lt;br /&gt;
==Creating a Rust library==&lt;br /&gt;
Next, we&#039;ll demonstrate creating a Rust library with &amp;lt;code&amp;gt;kos-cargo&amp;lt;/code&amp;gt; that can be included in other Dreamcast code. This will follow the [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/addlib &amp;lt;code&amp;gt;addlib&amp;lt;/code&amp;gt;] example. Once again, this project&#039;s initial setup is done the same as the above &amp;lt;code&amp;gt;hello&amp;lt;/code&amp;gt; example, but you&#039;ll create the new project using &amp;lt;code&amp;gt;kos-cargo new --lib addlib&amp;lt;/code&amp;gt; to specify that we&#039;re creating a library named &amp;lt;code&amp;gt;addlib&amp;lt;/code&amp;gt;. You&#039;ll also need to add the following text to this project&#039;s &amp;lt;code&amp;gt;Cargo.toml&amp;lt;/code&amp;gt; file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;toml&amp;quot;&amp;gt;&lt;br /&gt;
[lib]&lt;br /&gt;
crate-type = [&amp;quot;staticlib&amp;quot;]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This tells Rust to build a static &amp;lt;code&amp;gt;.a&amp;lt;/code&amp;gt; library archive file from our code, which is located in &amp;lt;code&amp;gt;src/lib.rs&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; line&amp;gt;&lt;br /&gt;
#[no_mangle]&lt;br /&gt;
pub extern &amp;quot;C&amp;quot; fn print_added(a: isize, b: isize) {&lt;br /&gt;
    print!(&amp;quot;{}&amp;quot;, a + b);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
#[no_mangle]&lt;br /&gt;
pub extern &amp;quot;C&amp;quot; fn add_integers(a: isize, b: isize) -&amp;gt; isize {&lt;br /&gt;
    a + b&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The source code here starts similarly to the &amp;quot;Hello, world!&amp;quot; example, except we don&#039;t need to specify &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;#![no_main]&amp;lt;/syntaxhighlight&amp;gt; as this is a library which wouldn&#039;t have a &amp;lt;code&amp;gt;main()&amp;lt;/code&amp;gt; function anyway.&lt;br /&gt;
&lt;br /&gt;
Two simple functions are provided: one for adding two integers and returning the result, and another for adding two integers and printing the result as text. Because these functions use &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;#[no_mangle]&amp;lt;/syntaxhighlight&amp;gt; and are declared &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;extern &amp;quot;C&amp;quot;&amp;lt;/syntaxhighlight&amp;gt;, they can be called by name in C code that links this library.&lt;br /&gt;
&lt;br /&gt;
When built using &amp;lt;code&amp;gt;kos-cargo build&amp;lt;/code&amp;gt;, a &amp;lt;code&amp;gt;target/sh-elf/debug/libaddlib.a&amp;lt;/code&amp;gt; file will be generated. This can be linked into other projects to gain the use of these functions.&lt;br /&gt;
&lt;br /&gt;
For example, this can be added to a standard &amp;lt;code&amp;gt;Makefile&amp;lt;/code&amp;gt;-based KallistiOS project by editing the &amp;lt;code&amp;gt;Makefile&amp;lt;/code&amp;gt;:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;make&amp;quot;&amp;gt;&lt;br /&gt;
$(TARGET): $(OBJS)&lt;br /&gt;
	kos-cc -o $(TARGET) $(OBJS) -L/opt/toolchains/dc/rust/examples/cargo-addlib/target/sh-elf/debug -laddlib&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then, we can use the code in our C source:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/* Declare the external function from the Rust library */&lt;br /&gt;
int add_integers(int a, int b);&lt;br /&gt;
&lt;br /&gt;
/* Use the function */&lt;br /&gt;
printf(&amp;quot;Five plus six is %d\n&amp;quot;, add_integers(5, 6));&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Compiling individual modules into object files with rustc==&lt;br /&gt;
If we&#039;d like to mix C and Rust code in the same &amp;lt;code&amp;gt;Makefile&amp;lt;/code&amp;gt;-based KallistiOS project without building an entirely separate library, we can do that as well. This is demonstrated in the [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/no_std/hello-make &amp;lt;code&amp;gt;hello-make&amp;lt;/code&amp;gt;] example.&lt;br /&gt;
&lt;br /&gt;
Instead of using &amp;lt;code&amp;gt;kos-cargo&amp;lt;/code&amp;gt;, we can invoke the &amp;lt;code&amp;gt;kos-rustc&amp;lt;/code&amp;gt; via a &amp;lt;code&amp;gt;Makefile&amp;lt;/code&amp;gt; to build Rust modules. If we assume the Rust file is named &amp;lt;code&amp;gt;example.rs&amp;lt;/code&amp;gt;, you&#039;ll need to add &amp;lt;code&amp;gt;example.o&amp;lt;/code&amp;gt; as an object file in your &amp;lt;code&amp;gt;Makefile&amp;lt;/code&amp;gt;&#039;s &amp;lt;code&amp;gt;OBJS =&amp;lt;/code&amp;gt; declaration. For example, if the project has two source files &amp;lt;code&amp;gt;hello_c.c&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;hello_rust.rs&amp;lt;/code&amp;gt;, our &amp;lt;code&amp;gt;Makefile&amp;lt;/code&amp;gt; would have a line like this:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;make&amp;quot;&amp;gt;&lt;br /&gt;
OBJS = hello_c.o hello_rust.o&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The example code demonstrates starting a C &amp;lt;code&amp;gt;main()&amp;lt;/code&amp;gt; function to call a Rust function which builds a &amp;lt;code&amp;gt;String&amp;lt;/code&amp;gt; containing the &amp;quot;Hello, world!&amp;quot; text which is passed back to a C function which prints &amp;lt;code&amp;gt;String&amp;lt;/code&amp;gt;s.&lt;br /&gt;
&lt;br /&gt;
==Using the libc crate with KallistiOS==&lt;br /&gt;
If any of the crates in your project&#039;s dependency tree requires the &#039;&#039;&#039;libc&#039;&#039;&#039; crate, you will need to override fetching the default crate with our custom libc crate with KallistiOS/Dreamcast definitions. This custom version was copied to &amp;lt;code&amp;gt;/opt/toolchains/dc/rust/libc&amp;lt;/code&amp;gt; when building the Rust sysroot earlier. Add the following text to your &amp;lt;code&amp;gt;Cargo.toml&amp;lt;/code&amp;gt;:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;toml&amp;quot;&amp;gt;&lt;br /&gt;
[patch.crates-io]&lt;br /&gt;
libc = { path = &amp;quot;/opt/toolchains/dc/rust/libc&amp;quot; }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Adjusting build flags==&lt;br /&gt;
Build settings can be adjusted through the &amp;lt;code&amp;gt;KOS_RCG_RUSTFLAGS&amp;lt;/code&amp;gt; variable in the &amp;lt;code&amp;gt;environ.sh&amp;lt;/code&amp;gt; file under the &amp;lt;code&amp;gt;### Rust Flags&amp;lt;/code&amp;gt; section. &lt;br /&gt;
* To pass arguments to the &#039;&#039;&#039;rustc&#039;&#039;&#039; frontend, e.g. to disable debug assertions:&lt;br /&gt;
** &amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot; inline&amp;gt;export KOS_RCG_RUSTFLAGS=&amp;quot;${KOS_RCG_RUSTFLAGS} -Cdebug-assertions=no&amp;quot;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* To pass arguments to the GCC backend, e.g. to enable the &amp;lt;code&amp;gt;-freorder-blocks-algorithm=simple&amp;lt;/code&amp;gt; optimization for Rust code:&lt;br /&gt;
** &amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot; inline&amp;gt;export KOS_RCG_RUSTFLAGS=&amp;quot;${KOS_RCG_RUSTFLAGS} -Cllvm-args=-freorder-blocks-algorithm=simple&amp;quot;&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Darc</name></author>
	</entry>
	<entry>
		<id>https://dreamcast.wiki/wiki/index.php?title=Rust_on_Dreamcast&amp;diff=3634</id>
		<title>Rust on Dreamcast</title>
		<link rel="alternate" type="text/html" href="https://dreamcast.wiki/wiki/index.php?title=Rust_on_Dreamcast&amp;diff=3634"/>
		<updated>2024-11-09T03:35:42Z</updated>

		<summary type="html">&lt;p&gt;Darc: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[File:Rust-dc-logo.png|thumb|Ferris holding his Dreamcast controller]]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Rust&#039;&#039;&#039; is a popular modern programming language focusing on performance, memory safety, and program correctness. As a &amp;quot;blazingly fast&amp;quot; low-level language, it is viable for running on a Dreamcast console. Doing so presents a bit of a challenge, however, as the official Rust compiler is based on the [https://llvm.org/ LLVM] toolchain infrastructure, which does not support the Dreamcast CPU&#039;s SuperH architecture. Dreamcast programming is instead typically done with [https://gcc.gnu.org/ GCC], the GNU Compiler Collection. There are currently two viable solutions to this challenge:&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;rustc_codegen_gcc&#039;&#039;&#039;, the current preferred method: An experimental codegen backend for the official Rust &#039;&#039;&#039;rustc&#039;&#039;&#039; compiler which interfaces with GCC&#039;s libgccjit API&lt;br /&gt;
* &#039;&#039;&#039;gccrs&#039;&#039;&#039;: an experimental Rust frontend for GCC&lt;br /&gt;
&lt;br /&gt;
Neither solution is complete at this time, but &#039;&#039;&#039;rustc_codegen_gcc&#039;&#039;&#039; is much further along and is quite usable with some patience with its current limitations. On the other hand, &#039;&#039;&#039;gccrs&#039;&#039;&#039; can compile for Dreamcast, but is in an earlier state. Below we will focus on using &#039;&#039;&#039;rustc_codegen_gcc&#039;&#039;&#039;. For more information on using gccrs, see the [[gccrs]] page.&lt;br /&gt;
&lt;br /&gt;
=Current Status=&lt;br /&gt;
* The full &#039;&#039;&#039;std&#039;&#039;&#039; library is supported.&lt;br /&gt;
* A [https://github.com/dreamcast-rs/KallistiOS custom version of KallistiOS] v2.1.0 (latest stable release) is used.&lt;br /&gt;
** This version includes minor changes for Rust and a beta libpthread addon layer to interface with the Rust standard library.&lt;br /&gt;
** Wrappers are provided to invoke &#039;&#039;&#039;cargo&#039;&#039;&#039; and &#039;&#039;&#039;rustc&#039;&#039;&#039; in a familiar manner to compile projects.&lt;br /&gt;
* A [https://github.com/dreamcast-rs/kos-sys &#039;&#039;&#039;kos-sys&#039;&#039;&#039;] raw/unsafe bindings crate is provided for easily linking KallistiOS and its unsafe C API into Rust crates. This crate is in early stages and in active development.&lt;br /&gt;
&amp;amp; A [https://github.com/dreamcast-rs/kos-rs &#039;&#039;&#039;kos-rs&#039;&#039;&#039;] Rust crate is available but is also in early stages.&lt;br /&gt;
** Documentation for the bindings is available at [https://kos-rs.dreamcast.wiki/ kos-rs.dreamcast.wiki].&lt;br /&gt;
** If bindings do not yet exist for desired functionality, wrapping of KallistiOS C functions is required to get proper use of KallistiOS at this time. See the [https://doc.rust-lang.org/nomicon/ffi.html Foreign Function Interface page] in the Rustonomicon for further understanding. &lt;br /&gt;
** A custom [https://github.com/dreamcast-rs/libc/tree/libc-0.2-kos &#039;&#039;&#039;libc&#039;&#039;&#039; crate with KallistiOS support] is provided.&lt;br /&gt;
* Code generation uses &#039;&#039;&#039;rustc_codegen_gcc&#039;&#039;&#039;, so its limitations apply here. See the [https://blog.antoyo.xyz/ development blog] for more information on its progress.&lt;br /&gt;
** rustc_codegen_gcc is pinned to a particular Rust nightly version, but is synced regularly. The current version in use is the &#039;&#039;&#039;2024-08-10&#039;&#039;&#039; nightly.&lt;br /&gt;
** A [https://github.com/dreamcast-rs/rust/tree/kos-2024-08-10 KallistiOS-patched version of the latest nightly] is maintained and updated regularly. &lt;br /&gt;
** Panic unwinding, debug info, LTO, etc. are not currently available.&lt;br /&gt;
** Architectures dependent on rustc_codegen_gcc are not yet able to be added to the rustc frontend, so a workaround is necessary: MIPS is falsely selected for KallistiOS, causing rustc to emit objects with a MIPS header and a provided wrapper is used when linking to rewrite these MIPS headers to SuperH before passing the compiled objects to the linker.&lt;br /&gt;
* A separate [https://github.com/rust-lang/gcc development version of the GCC toolchain] must be used.&lt;br /&gt;
** This version of GCC contains the latest libgccjit patches necessary to interface with rustc_codegen_gcc, and will be built to work with the custom KallistiOS&#039;s libpthread layer.&lt;br /&gt;
** New updates to rustc_codegen_gcc often require new updates to GCC, so recompiling GCC regularly will be necessary to keep up with the latest updates.&lt;br /&gt;
** The development version of GCC does not always compile well on every platform. Using a modern Linux platform is recommended for maximum compatibility.&lt;br /&gt;
** The custom KallistiOS and GCC environment can still be used to compile code or projects written in C, C++, etc.&lt;br /&gt;
* KallistiOS and all related libraries and tools must be compiled using the &amp;lt;code&amp;gt;-m4-single&amp;lt;/code&amp;gt; ABI. This is already set up for you in the guide below, but if external libraries are being linked to Rust projects, make sure that code is compiled using the proper ABI.&lt;br /&gt;
&lt;br /&gt;
=Setting up a Rust environment for Dreamcast development=&lt;br /&gt;
The following guide will provide instructions for setting up a Rust development environment for Dreamcast. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;If you run into any errors or other challenges while following this tutorial, or simply need clarification on any of the steps, feel free to ask for assistance on the [https://dcemulation.org/phpBB/viewforum.php?f=29 message board] or [https://discord.gg/wDEfkUh63h our Discord server] and we would be happy to aid you and update the guide for the benefit of future readers and others in the community.&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
==Prerequisites==&lt;br /&gt;
First, we will need to set up the prerequisites.&lt;br /&gt;
&lt;br /&gt;
* You must have the [https://rustup.rs/ rustup] tool installed for your operating system to manage Rust toolchain installations. It will automatically manage the latest Rust nightly installations for you. You cannot use a Rust toolchain that may be provided by your operating system vendor.&lt;br /&gt;
* Install the dependency packages for your operating system. These are listed in the &#039;&#039;&#039;Dependencies&#039;&#039;&#039; section on the [[Getting Started with Dreamcast development#Dependencies]] page, with lists provided for most common operating systems.&lt;br /&gt;
* We will install our custom Rust toolchain environment within &amp;lt;code&amp;gt;/opt/toolchains/dc/rust&amp;lt;/code&amp;gt;. If it does not already exist, create the &amp;lt;code&amp;gt;/opt/toolchains/dc&amp;lt;/code&amp;gt; directory and grant it proper permissions using the following commands. &lt;br /&gt;
 sudo mkdir -p /opt/toolchains/dc&lt;br /&gt;
 sudo chmod -R 755 /opt/toolchains/dc&lt;br /&gt;
 sudo chown -R $(id -u):$(id -g) /opt/toolchains/dc&lt;br /&gt;
* Clone the [https://github.com/dreamcast-rs/rust-for-dreamcast/ Rust for Dreamcast] repo containing necessary support files to &amp;lt;code&amp;gt;/opt/toolchains/dc/rust&amp;lt;/code&amp;gt;:&lt;br /&gt;
 git clone https://github.com/dreamcast-rs/rust-for-dreamcast.git /opt/toolchains/dc/rust&lt;br /&gt;
&lt;br /&gt;
==Installing our custom KallistiOS and GCC toolchain==&lt;br /&gt;
Clone the git repository for our custom Rust-patched version of KallistiOS v2.1.0 stable:&lt;br /&gt;
 git clone https://github.com/dreamcast-rs/KallistiOS /opt/toolchains/dc/rust/kos&lt;br /&gt;
&lt;br /&gt;
Enter the dc-chain directory:&lt;br /&gt;
 cd /opt/toolchains/dc/rust/kos/utils/dc-chain&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;Makefile.default.cfg&amp;lt;/code&amp;gt; file in this directory has been customized for you, selecting the proper toolchain profile, paths, and build options necessary for building a Rust toolchain using this guide. It is not recommended to adjust the defaults.&lt;br /&gt;
&lt;br /&gt;
To build the GCC toolchain using 2 CPU cores, simply type:&lt;br /&gt;
 make makejobs=2&lt;br /&gt;
Adjust the &amp;lt;code&amp;gt;makejobs=...&amp;lt;/code&amp;gt; value to use the number of CPU cores available to you on your system in order to speed up compilation. This may take a while to build, so be patient.&lt;br /&gt;
&lt;br /&gt;
Once the GCC toolchain is built, source the &amp;lt;code&amp;gt;environ.sh&amp;lt;/code&amp;gt; KallistiOS environment settings. A custom &amp;lt;code&amp;gt;environ.sh&amp;lt;/code&amp;gt; file has been provided for you, containing settings pre-adjusted for Rust development using this guide.&lt;br /&gt;
&lt;br /&gt;
You will need to run the source command to apply the KallistiOS environment settings to your currently running shell. Run the following now, &#039;&#039;&#039;and&#039;&#039;&#039; &#039;&#039;whenever&#039;&#039; you open a new shell to work on Dreamcast projects:&lt;br /&gt;
 source /opt/toolchains/dc/rust/misc/environ.sh&lt;br /&gt;
&lt;br /&gt;
Enter the KallistiOS directory:&lt;br /&gt;
 cd /opt/toolchains/dc/rust/kos&lt;br /&gt;
&lt;br /&gt;
Build KallistiOS:&lt;br /&gt;
 make&lt;br /&gt;
&lt;br /&gt;
==Building rustc_codegen_gcc and Rust sysroot==&lt;br /&gt;
Now that we have a working KallistiOS environment suitable for Rust development set up, we can build the Rust compiler compiler and sysroot components. A script is provided which will download the necessary components from the respective repositories and compile them for you. Run the installer script:&lt;br /&gt;
&lt;br /&gt;
 /opt/toolchains/dc/rust/misc/install-rust.sh&lt;br /&gt;
&lt;br /&gt;
If all goes well, you&#039;ll see &#039;&#039;&#039;Rust for KallistiOS/Dreamcast installed!&#039;&#039;&#039; message!&lt;br /&gt;
&lt;br /&gt;
==Building KOS ports libraries==&lt;br /&gt;
If desired, you may also built the KOS ports collection of libraries for use with your Rust environment. It is recommended to maintain a separate KOS ports installation for linking with Rust projects in &amp;lt;code&amp;gt;/opt/toolchains/dc/rust/kos-ports&amp;lt;/code&amp;gt;. This is because the Rust environment is using the &amp;lt;code&amp;gt;-m4-single&amp;lt;/code&amp;gt; ABI, but the KallistiOS default installation will be using the &amp;lt;code&amp;gt;-m4-single-only&amp;lt;/code&amp;gt;, which cannot be mixed and matched.&lt;br /&gt;
&lt;br /&gt;
Clone the kos-ports repository to your system:&lt;br /&gt;
 git clone https://github.com/KallistiOS/kos-ports /opt/toolchains/dc/rust/kos-ports&lt;br /&gt;
An individual port can be built and installed by entering its directory and running the proper command. For example, installing GLdc:&lt;br /&gt;
 cd /opt/toolchains/dc/rust/kos-ports/libGL&lt;br /&gt;
 make install&lt;br /&gt;
Or, instead, you may run the script to build all of the included ports:&lt;br /&gt;
 /opt/toolchains/dc/rust/kos-ports/utils/build-all.sh&lt;br /&gt;
&lt;br /&gt;
=Using Rust for Dreamcast=&lt;br /&gt;
If all went well, you will now have a working Rust for Dreamcast environment!&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;cargo&#039;&#039;&#039; can be invoked to target Dreamcast using the &amp;lt;code&amp;gt;kos-cargo&amp;lt;/code&amp;gt; command.&lt;br /&gt;
** &amp;lt;code&amp;gt;kos-cargo run&amp;lt;/code&amp;gt; can be used to build and run code on a Dreamcast console using [[dcload-ip]] or [[dcload-serial]]. You will need to set up the &amp;lt;code&amp;gt;KOS_LOADER&amp;lt;/code&amp;gt; variable with the necessary command in the &amp;lt;code&amp;gt;/opt/toolchains/dc/rust/misc/environ.sh&amp;lt;/code&amp;gt; file (and then re-source the file for the change to take effect).&lt;br /&gt;
* &#039;&#039;&#039;rustc&#039;&#039;&#039; can be invoked to target Dreamcast using the &amp;lt;code&amp;gt;kos-rustc&amp;lt;/code&amp;gt; command.&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
Examples are included with the Rust for Dreamcast repo to help you get started.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/hello-cargo &amp;lt;code&amp;gt;hello-cargo&amp;lt;/code&amp;gt;] demonstrates how to create a simple &amp;quot;Hello, world!&amp;quot; application using cargo.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/hello-make &amp;lt;code&amp;gt;hello-make&amp;lt;/code&amp;gt;] demonstrates how to create a simple &amp;quot;Hello, world!&amp;quot; application using standard Makefiles.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/gldc-cube &amp;lt;code&amp;gt;gldc-cube&amp;lt;/code&amp;gt;] demonstrates a Rust project using the GLdc KOS port.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/addlib &amp;lt;code&amp;gt;addlib&amp;lt;/code&amp;gt;] demonstrates how to create a Rust library that can be included with a KallistiOS project.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/atomics &amp;lt;code&amp;gt;atomics&amp;lt;/code&amp;gt;] demonstrates the use of atomic functions.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/filesystem-io &amp;lt;code&amp;gt;filesystem-io&amp;lt;/code&amp;gt;] demonstrates listing directory contents and opening files.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/network-time &amp;lt;code&amp;gt;network-time&amp;lt;/code&amp;gt;] demonstrates displaying the system time and retrieving the current UTC time from an NTP server using the [[Broadband adapter]] or [[LAN adapter]].&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/std/threads &amp;lt;code&amp;gt;threads&amp;lt;/code&amp;gt;] demonstrates the use of threads.&lt;br /&gt;
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/tokio-async &amp;lt;code&amp;gt;tokio-async&amp;lt;/code&amp;gt;] demonstrates the use of the &#039;&#039;&#039;tokio&#039;&#039;&#039; async runtime.&lt;br /&gt;
&lt;br /&gt;
==Creating a new Rust project with Cargo==&lt;br /&gt;
First, we&#039;ll demonstrate creating a new &amp;quot;Hello, world!&amp;quot; project with &amp;lt;code&amp;gt;kos-cargo&amp;lt;/code&amp;gt;. This will follow the [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/hello-cargo &amp;lt;code&amp;gt;cargo-hello&amp;lt;/code&amp;gt;] example included in the Rust-for-Dreamcast repo. &lt;br /&gt;
&lt;br /&gt;
In a directory of your choosing, let&#039;s invoke &amp;lt;code&amp;gt;kos-cargo&amp;lt;/code&amp;gt; to create a new project and then enter the directory:&lt;br /&gt;
 kos-cargo new hello&lt;br /&gt;
 cd hello&lt;br /&gt;
&lt;br /&gt;
Let&#039;s add our kos-rs crate to gain access to current KallistiOS bindings. Open &amp;lt;code&amp;gt;Cargo.toml&amp;lt;/code&amp;gt; in your text editor and add:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;toml&amp;quot;&amp;gt;&lt;br /&gt;
[dependencies]&lt;br /&gt;
kos = { package = &amp;quot;kos-rs&amp;quot;, git = &amp;quot;https://github.com/dreamcast-rs/kos-rs&amp;quot; }&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we can open up &amp;lt;code&amp;gt;src/main.rs&amp;lt;/code&amp;gt; and write our &amp;quot;Hello, world!&amp;quot; example code:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; line&amp;gt;&lt;br /&gt;
#![no_main]&lt;br /&gt;
&lt;br /&gt;
#[no_mangle]&lt;br /&gt;
extern &amp;quot;C&amp;quot; fn main(_argc: isize, _argv: *const *const u8) -&amp;gt; isize {&lt;br /&gt;
    println!(&amp;quot;Hello, world from Rust!&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
    0&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;#![no_main]&amp;lt;/syntaxhighlight&amp;gt; tells Rust that we will not have Rust use a &amp;lt;code&amp;gt;main&amp;lt;/code&amp;gt; function as an entry point -- this will be handled by KallistiOS.&lt;br /&gt;
* &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;#[no_mangle]&amp;lt;/syntaxhighlight&amp;gt; tells Rust to disable name mangling so that the &amp;lt;code&amp;gt;main&amp;lt;/code&amp;gt; function can be used by KallistiOS&#039;s C code.&lt;br /&gt;
* Finally, we have a &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;fn main&amp;lt;/syntaxhighlight&amp;gt; with the function signature of a typical C &amp;lt;code&amp;gt;main&amp;lt;/code&amp;gt; function, containing a basic &amp;quot;Hello, world!&amp;quot; exclamation.&lt;br /&gt;
&lt;br /&gt;
Now we can use &amp;lt;code&amp;gt;kos-cargo build&amp;lt;/code&amp;gt; to build our project. If all goes well, there will be a &amp;lt;code&amp;gt;target/sh-elf/debug/hello.elf&amp;lt;/code&amp;gt; file that can be sent to the Dreamcast with &amp;lt;code&amp;gt;dc-tool&amp;lt;/code&amp;gt;.&lt;br /&gt;
If you have &amp;lt;code&amp;gt;KOS_LOADER&amp;lt;/code&amp;gt; set in the &amp;lt;code&amp;gt;environ.sh&amp;lt;/code&amp;gt; file, you can invoke it directly with &amp;lt;code&amp;gt;kos-cargo run&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Creating a Rust project using kos-ports libraries==&lt;br /&gt;
[[File:Rust-Cube rustc codegen-gcc demo.gif|thumb|cube example in action]]&lt;br /&gt;
The [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/gldc-cube &amp;lt;code&amp;gt;gldc-cube&amp;lt;/code&amp;gt;] example demonstrates creating a rotating 3D cube using Rust as the primary language, while calling C functions provided by the &#039;&#039;&#039;GLdc&#039;&#039;&#039; library available in kos-ports. This project&#039;s initial setup is done the same as the above &amp;lt;code&amp;gt;hello&amp;lt;/code&amp;gt; example.&lt;br /&gt;
&lt;br /&gt;
For this example, we&#039;ll need to convert JPEG textures to VQ-encoded textures at compile time using the &amp;lt;code&amp;gt;vqenc&amp;lt;/code&amp;gt; utility includes with KallistiOS. To do this, we&#039;ll add a &amp;lt;code&amp;gt;build.rs&amp;lt;/code&amp;gt; file to the root of the crate with the following code (abridged to show the conversion of only one texture):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot;&amp;gt;&lt;br /&gt;
fn main() {&lt;br /&gt;
    let kos_base = std::env::var(&amp;quot;KOS_BASE&amp;quot;).expect(&amp;quot;Missing $KOS_BASE -- KallistiOS environment not sourced!&amp;quot;);&lt;br /&gt;
    let vqenc_cmd = kos_base + &amp;quot;/utils/vqenc/vqenc&amp;quot;;&lt;br /&gt;
    Command::new(&amp;amp;vqenc_cmd)&lt;br /&gt;
        .arg(&amp;quot;-t&amp;quot;)&lt;br /&gt;
        .arg(&amp;quot;-v&amp;quot;)&lt;br /&gt;
        .arg(&amp;quot;rsrc/tex_claw.jpg&amp;quot;)&lt;br /&gt;
        .output()&lt;br /&gt;
        .expect(&amp;quot;vqenc on tex_claw.jpg failed!&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
The texture files are then included in our project using the &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;include_bytes!&amp;lt;/syntaxhighlight&amp;gt; macro.&lt;br /&gt;
&lt;br /&gt;
We will need to link the GLdc library in this example, but we don&#039;t need to do anything special to add the paths to this library, because adding the common paths to KallistiOS libraries is already done for us in the kos-rs crate. However, if in your project you need to include a separate unique library path, you can add it in your &amp;lt;code&amp;gt;build.rs&amp;lt;/code&amp;gt; like so:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot;&amp;gt;&lt;br /&gt;
println!(&amp;quot;cargo:rustc-link-search=native={}&amp;quot;, lib_path);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The workings of this example&#039;s source code are too great to detail here line-by-line, but the example demonstrates how to use GLdc via the [https://github.com/dreamcast-rs/gldc-sys gldc-sys] crate, which contains unsafe/raw bindings to GLdc. Check out this crate&#039;s source code for an example of declaring and binding external C functions, constants, and structures so they can be used in Rust code. Since the entirety of the gldc-cube example uses C FFI functions without safe wrappers, the &amp;lt;code&amp;gt;main()&amp;lt;/code&amp;gt; source is wrapped in &amp;lt;code&amp;gt;unsafe {}&amp;lt;/code&amp;gt;. In the future, this would be much less necessary as higher level safe Rust bindings to KallistiOS and other libraries become mature.&lt;br /&gt;
&lt;br /&gt;
==Creating a Rust library==&lt;br /&gt;
Next, we&#039;ll demonstrate creating a Rust library with &amp;lt;code&amp;gt;kos-cargo&amp;lt;/code&amp;gt; that can be included in other Dreamcast code. This will follow the [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/addlib &amp;lt;code&amp;gt;addlib&amp;lt;/code&amp;gt;] example. Once again, this project&#039;s initial setup is done the same as the above &amp;lt;code&amp;gt;hello&amp;lt;/code&amp;gt; example, but you&#039;ll create the new project using &amp;lt;code&amp;gt;kos-cargo new --lib addlib&amp;lt;/code&amp;gt; to specify that we&#039;re creating a library named &amp;lt;code&amp;gt;addlib&amp;lt;/code&amp;gt;. You&#039;ll also need to add the following text to this project&#039;s &amp;lt;code&amp;gt;Cargo.toml&amp;lt;/code&amp;gt; file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;toml&amp;quot;&amp;gt;&lt;br /&gt;
[lib]&lt;br /&gt;
crate-type = [&amp;quot;staticlib&amp;quot;]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This tells Rust to build a static &amp;lt;code&amp;gt;.a&amp;lt;/code&amp;gt; library archive file from our code, which is located in &amp;lt;code&amp;gt;src/lib.rs&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; line&amp;gt;&lt;br /&gt;
#[no_mangle]&lt;br /&gt;
pub extern &amp;quot;C&amp;quot; fn print_added(a: isize, b: isize) {&lt;br /&gt;
    print!(&amp;quot;{}&amp;quot;, a + b);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
#[no_mangle]&lt;br /&gt;
pub extern &amp;quot;C&amp;quot; fn add_integers(a: isize, b: isize) -&amp;gt; isize {&lt;br /&gt;
    a + b&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The source code here starts similarly to the &amp;quot;Hello, world!&amp;quot; example, except we don&#039;t need to specify &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;#![no_main]&amp;lt;/syntaxhighlight&amp;gt; as this is a library which wouldn&#039;t have a &amp;lt;code&amp;gt;main()&amp;lt;/code&amp;gt; function anyway.&lt;br /&gt;
&lt;br /&gt;
Two simple functions are provided: one for adding two integers and returning the result, and another for adding two integers and printing the result as text. Because these functions use &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;#[no_mangle]&amp;lt;/syntaxhighlight&amp;gt; and are declared &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;extern &amp;quot;C&amp;quot;&amp;lt;/syntaxhighlight&amp;gt;, they can be called by name in C code that links this library.&lt;br /&gt;
&lt;br /&gt;
When built using &amp;lt;code&amp;gt;kos-cargo build&amp;lt;/code&amp;gt;, a &amp;lt;code&amp;gt;target/sh-elf/debug/libaddlib.a&amp;lt;/code&amp;gt; file will be generated. This can be linked into other projects to gain the use of these functions.&lt;br /&gt;
&lt;br /&gt;
For example, this can be added to a standard &amp;lt;code&amp;gt;Makefile&amp;lt;/code&amp;gt;-based KallistiOS project by editing the &amp;lt;code&amp;gt;Makefile&amp;lt;/code&amp;gt;:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;make&amp;quot;&amp;gt;&lt;br /&gt;
$(TARGET): $(OBJS)&lt;br /&gt;
	kos-cc -o $(TARGET) $(OBJS) -L/opt/toolchains/dc/rust/examples/cargo-addlib/target/sh-elf/debug -laddlib&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then, we can use the code in our C source:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/* Declare the external function from the Rust library */&lt;br /&gt;
int add_integers(int a, int b);&lt;br /&gt;
&lt;br /&gt;
/* Use the function */&lt;br /&gt;
printf(&amp;quot;Five plus six is %d\n&amp;quot;, add_integers(5, 6));&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Compiling individual modules into object files with rustc==&lt;br /&gt;
If we&#039;d like to mix C and Rust code in the same &amp;lt;code&amp;gt;Makefile&amp;lt;/code&amp;gt;-based KallistiOS project without building an entirely separate library, we can do that as well. This is demonstrated in the [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/no_std/hello-make &amp;lt;code&amp;gt;hello-make&amp;lt;/code&amp;gt;] example.&lt;br /&gt;
&lt;br /&gt;
Instead of using &amp;lt;code&amp;gt;kos-cargo&amp;lt;/code&amp;gt;, we can invoke the &amp;lt;code&amp;gt;kos-rustc&amp;lt;/code&amp;gt; via a &amp;lt;code&amp;gt;Makefile&amp;lt;/code&amp;gt; to build Rust modules. If we assume the Rust file is named &amp;lt;code&amp;gt;example.rs&amp;lt;/code&amp;gt;, you&#039;ll need to add &amp;lt;code&amp;gt;example.o&amp;lt;/code&amp;gt; as an object file in your &amp;lt;code&amp;gt;Makefile&amp;lt;/code&amp;gt;&#039;s &amp;lt;code&amp;gt;OBJS =&amp;lt;/code&amp;gt; declaration. For example, if the project has two source files &amp;lt;code&amp;gt;hello_c.c&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;hello_rust.rs&amp;lt;/code&amp;gt;, our &amp;lt;code&amp;gt;Makefile&amp;lt;/code&amp;gt; would have a line like this:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;make&amp;quot;&amp;gt;&lt;br /&gt;
OBJS = hello_c.o hello_rust.o&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The example code demonstrates starting a C &amp;lt;code&amp;gt;main()&amp;lt;/code&amp;gt; function to call a Rust function which builds a &amp;lt;code&amp;gt;String&amp;lt;/code&amp;gt; containing the &amp;quot;Hello, world!&amp;quot; text which is passed back to a C function which prints &amp;lt;code&amp;gt;String&amp;lt;/code&amp;gt;s.&lt;br /&gt;
&lt;br /&gt;
==Using the libc crate with KallistiOS==&lt;br /&gt;
If any of the crates in your project&#039;s dependency tree requires the &#039;&#039;&#039;libc&#039;&#039;&#039; crate, you will need to override fetching the default crate with our custom libc crate with KallistiOS/Dreamcast definitions. This custom version was copied to &amp;lt;code&amp;gt;/opt/toolchains/dc/rust/libc&amp;lt;/code&amp;gt; when building the Rust sysroot earlier. Add the following text to your &amp;lt;code&amp;gt;Cargo.toml&amp;lt;/code&amp;gt;:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;toml&amp;quot;&amp;gt;&lt;br /&gt;
[patch.crates-io]&lt;br /&gt;
libc = { path = &amp;quot;/opt/toolchains/dc/rust/libc&amp;quot; }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Adjusting build flags==&lt;br /&gt;
Build settings can be adjusted through the &amp;lt;code&amp;gt;KOS_RCG_RUSTFLAGS&amp;lt;/code&amp;gt; variable in the &amp;lt;code&amp;gt;environ.sh&amp;lt;/code&amp;gt; file under the &amp;lt;code&amp;gt;### Rust Flags&amp;lt;/code&amp;gt; section. &lt;br /&gt;
* To pass arguments to the &#039;&#039;&#039;rustc&#039;&#039;&#039; frontend, e.g. to disable debug assertions:&lt;br /&gt;
** &amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot; inline&amp;gt;export KOS_RCG_RUSTFLAGS=&amp;quot;${KOS_RCG_RUSTFLAGS} -Cdebug-assertions=no&amp;quot;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* To pass arguments to the GCC backend, e.g. to enable the &amp;lt;code&amp;gt;-freorder-blocks-algorithm=simple&amp;lt;/code&amp;gt; optimization for Rust code:&lt;br /&gt;
** &amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot; inline&amp;gt;export KOS_RCG_RUSTFLAGS=&amp;quot;${KOS_RCG_RUSTFLAGS} -Cllvm-args=-freorder-blocks-algorithm=simple&amp;quot;&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Darc</name></author>
	</entry>
	<entry>
		<id>https://dreamcast.wiki/wiki/index.php?title=Rust_on_Dreamcast&amp;diff=3633</id>
		<title>Rust on Dreamcast</title>
		<link rel="alternate" type="text/html" href="https://dreamcast.wiki/wiki/index.php?title=Rust_on_Dreamcast&amp;diff=3633"/>
		<updated>2024-10-29T22:29:56Z</updated>

		<summary type="html">&lt;p&gt;Darc: /* Creating a Rust project using kos-ports libraries */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[File:Rust-dc-logo.png|thumb|Ferris holding his Dreamcast controller]]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Rust&#039;&#039;&#039; is a popular modern programming language focusing on performance, memory safety, and program correctness. As a &amp;quot;blazingly fast&amp;quot; low-level language, it is viable for running on a Dreamcast console. Doing so presents a bit of a challenge, however, as the official Rust compiler is based on the [https://llvm.org/ LLVM] toolchain infrastructure, which does not support the Dreamcast CPU&#039;s SuperH architecture. Dreamcast programming is instead typically done with [https://gcc.gnu.org/ GCC], the GNU Compiler Collection. There are currently two viable solutions to this challenge:&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;rustc_codegen_gcc&#039;&#039;&#039;, the current preferred method: An experimental codegen backend for the official Rust &#039;&#039;&#039;rustc&#039;&#039;&#039; compiler which interfaces with GCC&#039;s libgccjit API&lt;br /&gt;
* &#039;&#039;&#039;gccrs&#039;&#039;&#039;: an experimental Rust frontend for GCC&lt;br /&gt;
&lt;br /&gt;
Neither solution is complete at this time, but &#039;&#039;&#039;rustc_codegen_gcc&#039;&#039;&#039; is much further along and is quite usable with some patience with its current limitations. On the other hand, &#039;&#039;&#039;gccrs&#039;&#039;&#039; can compile for Dreamcast, but is in an earlier state. Below we will focus on using &#039;&#039;&#039;rustc_codegen_gcc&#039;&#039;&#039;. For more information on using gccrs, see the [[gccrs]] page.&lt;br /&gt;
&lt;br /&gt;
=Current Status=&lt;br /&gt;
* Programs can be written using the full &#039;&#039;&#039;std&#039;&#039;&#039; library or &#039;&#039;&#039;no_std&#039;&#039;&#039;. In the case of &#039;&#039;&#039;no_std&#039;&#039;&#039; programs, a memory allocator is provided so &#039;&#039;&#039;alloc&#039;&#039;&#039; types can be used (Vec, String, Box, etc.).&lt;br /&gt;
* A [https://github.com/dreamcast-rs/KallistiOS custom version of KallistiOS] v2.1.0 (latest stable release) is used.&lt;br /&gt;
** This version includes minor changes for Rust and a beta libpthread addon layer to interface with the Rust standard library.&lt;br /&gt;
** Wrappers are provided to invoke &#039;&#039;&#039;cargo&#039;&#039;&#039; and &#039;&#039;&#039;rustc&#039;&#039;&#039; in a familiar manner to compile projects.&lt;br /&gt;
* A [https://github.com/dreamcast-rs/kos-rs &#039;&#039;&#039;kos-rs&#039;&#039;&#039;] crate is provided for easily linking KallistiOS into Rust crates.&lt;br /&gt;
** Some very minimal bindings to KallistiOS functions are provided, with expansion of these bindings in progress.&lt;br /&gt;
** Documentation for the bindings is available at [https://kos-rs.dreamcast.wiki/ kos-rs.dreamcast.wiki].&lt;br /&gt;
** If bindings do not yet exist for desired functionality, wrapping of KallistiOS C functions is required to get proper use of KallistiOS at this time. See the [https://doc.rust-lang.org/nomicon/ffi.html Foreign Function Interface page] in the Rustonomicon for further understanding. &lt;br /&gt;
** A custom [https://github.com/dreamcast-rs/libc/tree/libc-0.2-kos &#039;&#039;&#039;libc&#039;&#039;&#039; crate with KallistiOS support] is provided.&lt;br /&gt;
* Code generation uses &#039;&#039;&#039;rustc_codegen_gcc&#039;&#039;&#039;, so its limitations apply here. See the [https://blog.antoyo.xyz/ development blog] for more information on its progress.&lt;br /&gt;
** rustc_codegen_gcc is pinned to a particular Rust nightly version, but is synced regularly. The current version in use is the &#039;&#039;&#039;2024-08-10&#039;&#039;&#039; nightly.&lt;br /&gt;
** A [https://github.com/dreamcast-rs/rust/tree/kos-2024-08-10 KallistiOS-patched version of the latest nightly] is maintained and updated regularly. &lt;br /&gt;
** Panic unwinding, debug info, LTO, etc. are not currently available.&lt;br /&gt;
** Architectures dependent on rustc_codegen_gcc are not yet able to be added to the rustc frontend, so a workaround is necessary: MIPS is falsely selected for KallistiOS, causing rustc to emit objects with a MIPS header and a provided wrapper is used when linking to rewrite these MIPS headers to SuperH before passing the compiled objects to the linker.&lt;br /&gt;
* A separate [https://github.com/rust-lang/gcc development version of the GCC toolchain] must be used.&lt;br /&gt;
** This version of GCC contains the latest libgccjit patches necessary to interface with rustc_codegen_gcc, and will be built to work with the custom KallistiOS&#039;s libpthread layer.&lt;br /&gt;
** New updates to rustc_codegen_gcc often require new updates to GCC, so recompiling GCC regularly will be necessary to keep up with the latest updates.&lt;br /&gt;
** The development version of GCC does not always compile well on every platform. Using a modern Linux platform is recommended for maximum compatibility.&lt;br /&gt;
** The custom KallistiOS and GCC environment can still be used to compile code or projects written in C, C++, etc.&lt;br /&gt;
* KallistiOS and all related libraries and tools must be compiled using the &amp;lt;code&amp;gt;-m4-single&amp;lt;/code&amp;gt; ABI. This is already set up for you in the guide below, but if external libraries are being linked to Rust projects, make sure that code is compiled using the proper ABI.&lt;br /&gt;
&lt;br /&gt;
=Setting up a Rust environment for Dreamcast development=&lt;br /&gt;
The following guide will provide instructions for setting up a Rust development environment for Dreamcast. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;If you run into any errors or other challenges while following this tutorial, or simply need clarification on any of the steps, feel free to ask for assistance on the [https://dcemulation.org/phpBB/viewforum.php?f=29 message board] or [https://discord.gg/wDEfkUh63h our Discord server] and we would be happy to aid you and update the guide for the benefit of future readers and others in the community.&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
==Prerequisites==&lt;br /&gt;
First, we will need to set up the prerequisites.&lt;br /&gt;
&lt;br /&gt;
* You must have the [https://rustup.rs/ rustup] tool installed for your operating system to manage Rust toolchain installations. It will automatically manage the latest Rust nightly installations for you. You cannot use a Rust toolchain that may be provided by your operating system vendor.&lt;br /&gt;
* Install the dependency packages for your operating system. These are listed in the &#039;&#039;&#039;Dependencies&#039;&#039;&#039; section on the [[Getting Started with Dreamcast development#Dependencies]] page, with lists provided for most common operating systems.&lt;br /&gt;
* We will install our custom Rust toolchain environment within &amp;lt;code&amp;gt;/opt/toolchains/dc/rust&amp;lt;/code&amp;gt;. If it does not already exist, create the &amp;lt;code&amp;gt;/opt/toolchains/dc&amp;lt;/code&amp;gt; directory and grant it proper permissions using the following commands. &lt;br /&gt;
 sudo mkdir -p /opt/toolchains/dc&lt;br /&gt;
 sudo chmod -R 755 /opt/toolchains/dc&lt;br /&gt;
 sudo chown -R $(id -u):$(id -g) /opt/toolchains/dc&lt;br /&gt;
* Clone the [https://github.com/dreamcast-rs/rust-for-dreamcast/ Rust for Dreamcast] repo containing necessary support files to &amp;lt;code&amp;gt;/opt/toolchains/dc/rust&amp;lt;/code&amp;gt;:&lt;br /&gt;
 git clone https://github.com/dreamcast-rs/rust-for-dreamcast.git /opt/toolchains/dc/rust&lt;br /&gt;
&lt;br /&gt;
==Installing our custom KallistiOS and GCC toolchain==&lt;br /&gt;
Clone the git repository for our custom Rust-patched version of KallistiOS v2.1.0 stable:&lt;br /&gt;
 git clone https://github.com/dreamcast-rs/KallistiOS /opt/toolchains/dc/rust/kos&lt;br /&gt;
&lt;br /&gt;
Enter the dc-chain directory:&lt;br /&gt;
 cd /opt/toolchains/dc/rust/kos/utils/dc-chain&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;Makefile.default.cfg&amp;lt;/code&amp;gt; file in this directory has been customized for you, selecting the proper toolchain profile, paths, and build options necessary for building a Rust toolchain using this guide. It is not recommended to adjust the defaults.&lt;br /&gt;
&lt;br /&gt;
To build the GCC toolchain using 2 CPU cores, simply type:&lt;br /&gt;
 make makejobs=2&lt;br /&gt;
Adjust the &amp;lt;code&amp;gt;makejobs=...&amp;lt;/code&amp;gt; value to use the number of CPU cores available to you on your system in order to speed up compilation. This may take a while to build, so be patient.&lt;br /&gt;
&lt;br /&gt;
Once the GCC toolchain is built, source the &amp;lt;code&amp;gt;environ.sh&amp;lt;/code&amp;gt; KallistiOS environment settings. A custom &amp;lt;code&amp;gt;environ.sh&amp;lt;/code&amp;gt; file has been provided for you, containing settings pre-adjusted for Rust development using this guide.&lt;br /&gt;
&lt;br /&gt;
You will need to run the source command to apply the KallistiOS environment settings to your currently running shell. Run the following now, &#039;&#039;&#039;and&#039;&#039;&#039; &#039;&#039;whenever&#039;&#039; you open a new shell to work on Dreamcast projects:&lt;br /&gt;
 source /opt/toolchains/dc/rust/misc/environ.sh&lt;br /&gt;
&lt;br /&gt;
Enter the KallistiOS directory:&lt;br /&gt;
 cd /opt/toolchains/dc/rust/kos&lt;br /&gt;
&lt;br /&gt;
Build KallistiOS:&lt;br /&gt;
 make&lt;br /&gt;
&lt;br /&gt;
==Building rustc_codegen_gcc and Rust sysroot==&lt;br /&gt;
Now that we have a working KallistiOS environment suitable for Rust development set up, we can build the Rust compiler compiler and sysroot components. A script is provided which will download the necessary components from the respective repositories and compile them for you. Run the installer script:&lt;br /&gt;
&lt;br /&gt;
 /opt/toolchains/dc/rust/misc/install-rust.sh&lt;br /&gt;
&lt;br /&gt;
If all goes well, you&#039;ll see &#039;&#039;&#039;Rust for KallistiOS/Dreamcast installed!&#039;&#039;&#039; message!&lt;br /&gt;
&lt;br /&gt;
==Building KOS ports libraries==&lt;br /&gt;
If desired, you may also built the KOS ports collection of libraries for use with your Rust environment. It is recommended to maintain a separate KOS ports installation for linking with Rust projects in &amp;lt;code&amp;gt;/opt/toolchains/dc/rust/kos-ports&amp;lt;/code&amp;gt;. This is because the Rust environment is using the &amp;lt;code&amp;gt;-m4-single&amp;lt;/code&amp;gt; ABI, but the KallistiOS default installation will be using the &amp;lt;code&amp;gt;-m4-single-only&amp;lt;/code&amp;gt;, which cannot be mixed and matched.&lt;br /&gt;
&lt;br /&gt;
Clone the kos-ports repository to your system:&lt;br /&gt;
 git clone https://github.com/KallistiOS/kos-ports /opt/toolchains/dc/rust/kos-ports&lt;br /&gt;
An individual port can be built and installed by entering its directory and running the proper command. For example, installing GLdc:&lt;br /&gt;
 cd /opt/toolchains/dc/rust/kos-ports/libGL&lt;br /&gt;
 make install&lt;br /&gt;
Or, instead, you may run the script to build all of the included ports:&lt;br /&gt;
 /opt/toolchains/dc/rust/kos-ports/utils/build-all.sh&lt;br /&gt;
&lt;br /&gt;
=Using Rust for Dreamcast=&lt;br /&gt;
If all went well, you will now have a working Rust for Dreamcast environment!&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;cargo&#039;&#039;&#039; can be invoked to target Dreamcast using the &amp;lt;code&amp;gt;kos-cargo&amp;lt;/code&amp;gt; command.&lt;br /&gt;
** &amp;lt;code&amp;gt;kos-cargo run&amp;lt;/code&amp;gt; can be used to build and run code on a Dreamcast console using [[dcload-ip]] or [[dcload-serial]]. You will need to set up the &amp;lt;code&amp;gt;KOS_LOADER&amp;lt;/code&amp;gt; variable with the necessary command in the &amp;lt;code&amp;gt;/opt/toolchains/dc/rust/misc/environ.sh&amp;lt;/code&amp;gt; file (and then re-source the file for the change to take effect).&lt;br /&gt;
* &#039;&#039;&#039;rustc&#039;&#039;&#039; can be invoked to target Dreamcast using the &amp;lt;code&amp;gt;kos-rustc&amp;lt;/code&amp;gt; command.&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
Examples are included with the Rust for Dreamcast repo to help you get started.&lt;br /&gt;
* &#039;&#039;&#039;no_std&#039;&#039;&#039; examples:&lt;br /&gt;
** [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/no_std/hello-cargo &amp;lt;code&amp;gt;no_std/hello-cargo&amp;lt;/code&amp;gt;] demonstrates how to create a simple &amp;quot;Hello, world!&amp;quot; application using cargo.&lt;br /&gt;
** [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/no_std/hello-make &amp;lt;code&amp;gt;no_std/hello-make&amp;lt;/code&amp;gt;] demonstrates how to create a simple &amp;quot;Hello, world!&amp;quot; application using standard Makefiles.&lt;br /&gt;
** [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/no_std/gldc-cube &amp;lt;code&amp;gt;no_std/gldc-cube&amp;lt;/code&amp;gt;] demonstrates a Rust project using the GLdc KOS port.&lt;br /&gt;
** [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/no_std/addlib &amp;lt;code&amp;gt;no_std/addlib&amp;lt;/code&amp;gt;] demonstrates how to create a Rust library that can be included with a KallistiOS project.&lt;br /&gt;
* &#039;&#039;&#039;std&#039;&#039;&#039; examples:&lt;br /&gt;
** [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/std/atomics &amp;lt;code&amp;gt;std/atomics&amp;lt;/code&amp;gt;] demonstrates the use of atomic functions.&lt;br /&gt;
** [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/std/filesystem-io &amp;lt;code&amp;gt;std/filesystem-io&amp;lt;/code&amp;gt;] demonstrates listing directory contents and opening files.&lt;br /&gt;
** [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/std/network-time &amp;lt;code&amp;gt;std/network-time&amp;lt;/code&amp;gt;] demonstrates displaying the system time and retrieving the current UTC time from an NTP server using the [[Broadband adapter]] or [[LAN adapter]].&lt;br /&gt;
** [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/std/threads &amp;lt;code&amp;gt;std/threads&amp;lt;/code&amp;gt;] demonstrates the use of threads.&lt;br /&gt;
** [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/std/tokio-async &amp;lt;code&amp;gt;std/tokio-async&amp;lt;/code&amp;gt;] demonstrates the use of the &#039;&#039;&#039;tokio&#039;&#039;&#039; async runtime.&lt;br /&gt;
&lt;br /&gt;
==Creating a new Rust project with Cargo==&lt;br /&gt;
First, we&#039;ll demonstrate creating a new no_std &amp;quot;Hello, world!&amp;quot; project with &amp;lt;code&amp;gt;kos-cargo&amp;lt;/code&amp;gt;. This will follow the [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/no_std/hello-cargo &amp;lt;code&amp;gt;no_std/cargo-hello&amp;lt;/code&amp;gt;] example included in the Rust-for-Dreamcast repo. &lt;br /&gt;
&lt;br /&gt;
In a directory of your choosing, let&#039;s invoke &amp;lt;code&amp;gt;kos-cargo&amp;lt;/code&amp;gt; to create a new project and then enter the directory:&lt;br /&gt;
 kos-cargo new hello&lt;br /&gt;
 cd hello&lt;br /&gt;
&lt;br /&gt;
Let&#039;s add our kos-rs crate to gain access to current KallistiOS bindings. This will be a &amp;lt;code&amp;gt;no_std&amp;lt;/code&amp;gt; project, so we will disable the use of the &amp;lt;code&amp;gt;std&amp;lt;/code&amp;gt; library by using &amp;lt;syntaxhighlight lang=&amp;quot;toml&amp;quot; inline&amp;gt;default-features = false&amp;lt;/syntaxhighlight&amp;gt;. Open &amp;lt;code&amp;gt;Cargo.toml&amp;lt;/code&amp;gt; in your text editor and add:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;toml&amp;quot;&amp;gt;&lt;br /&gt;
[dependencies]&lt;br /&gt;
kos = { package = &amp;quot;kos-rs&amp;quot;, git = &amp;quot;https://github.com/dreamcast-rs/kos-rs&amp;quot;, default-features = false }&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we can open up &amp;lt;code&amp;gt;src/main.rs&amp;lt;/code&amp;gt; and write our &amp;quot;Hello, world!&amp;quot; example code:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; line&amp;gt;&lt;br /&gt;
#![no_std]&lt;br /&gt;
#![no_main]&lt;br /&gt;
extern crate alloc;&lt;br /&gt;
use kos::println;&lt;br /&gt;
&lt;br /&gt;
#[no_mangle]&lt;br /&gt;
extern &amp;quot;C&amp;quot; fn main(_argc: isize, _argv: *const *const u8) -&amp;gt; isize {&lt;br /&gt;
    println!(&amp;quot;Hello, world from Rust!&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
    0&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;#![no_std]&amp;lt;/syntaxhighlight&amp;gt; and &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;#![no_main]&amp;lt;/syntaxhighlight&amp;gt; tell Rust that our project does not use the standard library and we will not have Rust use a &amp;lt;code&amp;gt;main&amp;lt;/code&amp;gt; function as an entry point -- this will be handled by KallistiOS.&lt;br /&gt;
* &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;extern crate alloc;&amp;lt;/syntaxhighlight&amp;gt; tells Rust to use the alloc crate to gain access to heap-allocated types (in our case, &amp;lt;code&amp;gt;String&amp;lt;/code&amp;gt;).&lt;br /&gt;
* &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;use kos::println!;&amp;lt;/syntaxhighlight&amp;gt; tells Rust to use the &amp;lt;code&amp;gt;println!&amp;lt;/code&amp;gt; macro defined in the kos-rs crate. With this, we can print output to our &amp;lt;code&amp;gt;dc-tool&amp;lt;/code&amp;gt; console.&lt;br /&gt;
* &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;#[no_mangle]&amp;lt;/syntaxhighlight&amp;gt; tells Rust to disable name mangling so that the &amp;lt;code&amp;gt;main&amp;lt;/code&amp;gt; function can be used by KallistiOS.&lt;br /&gt;
* Finally, we have a &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;fn main&amp;lt;/syntaxhighlight&amp;gt; with the function signature of a typical C &amp;lt;code&amp;gt;main&amp;lt;/code&amp;gt; function, containing a basic &amp;quot;Hello, world!&amp;quot; exclamation.&lt;br /&gt;
&lt;br /&gt;
Now we can use &amp;lt;code&amp;gt;kos-cargo build&amp;lt;/code&amp;gt; to build our project. If all goes well, there will be a &amp;lt;code&amp;gt;target/sh-elf/debug/hello.elf&amp;lt;/code&amp;gt; file that can be sent to the Dreamcast with &amp;lt;code&amp;gt;dc-tool&amp;lt;/code&amp;gt;.&lt;br /&gt;
If you have &amp;lt;code&amp;gt;KOS_LOADER&amp;lt;/code&amp;gt; set in the &amp;lt;code&amp;gt;environ.sh&amp;lt;/code&amp;gt; file, you can invoke it directly with &amp;lt;code&amp;gt;kos-cargo run&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Creating a Rust project using kos-ports libraries==&lt;br /&gt;
[[File:Rust-Cube rustc codegen-gcc demo.gif|thumb|cube example in action]]&lt;br /&gt;
The [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/no_std/gldc-cube &amp;lt;code&amp;gt;gldc-cube&amp;lt;/code&amp;gt;] example demonstrates creating a rotating 3D cube using Rust as the primary language, while calling C functions provided by the &#039;&#039;&#039;GLdc&#039;&#039;&#039; library available in kos-ports. This project&#039;s initial setup is done the same as the above &amp;lt;code&amp;gt;hello&amp;lt;/code&amp;gt; example.&lt;br /&gt;
&lt;br /&gt;
For this example, we&#039;ll need to convert JPEG textures to VQ-encoded textures at compile time using the &amp;lt;code&amp;gt;vqenc&amp;lt;/code&amp;gt; utility includes with KallistiOS. To do this, we&#039;ll add a &amp;lt;code&amp;gt;build.rs&amp;lt;/code&amp;gt; file to the root of the crate with the following code (abridged to show the conversion of only one texture):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot;&amp;gt;&lt;br /&gt;
fn main() {&lt;br /&gt;
    let kos_base = std::env::var(&amp;quot;KOS_BASE&amp;quot;).expect(&amp;quot;Missing $KOS_BASE -- KallistiOS environment not sourced!&amp;quot;);&lt;br /&gt;
    let vqenc_cmd = kos_base + &amp;quot;/utils/vqenc/vqenc&amp;quot;;&lt;br /&gt;
    Command::new(&amp;amp;vqenc_cmd)&lt;br /&gt;
        .arg(&amp;quot;-t&amp;quot;)&lt;br /&gt;
        .arg(&amp;quot;-v&amp;quot;)&lt;br /&gt;
        .arg(&amp;quot;rsrc/tex_claw.jpg&amp;quot;)&lt;br /&gt;
        .output()&lt;br /&gt;
        .expect(&amp;quot;vqenc on tex_claw.jpg failed!&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
The texture files are then included in our project using the &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;include_bytes!&amp;lt;/syntaxhighlight&amp;gt; macro.&lt;br /&gt;
&lt;br /&gt;
We will need to link the GLdc library in this example, but we don&#039;t need to do anything special to add the paths to this library, because adding the common paths to KallistiOS libraries is already done for us in the kos-rs crate. However, if in your project you need to include a separate unique library path, you can add it in your &amp;lt;code&amp;gt;build.rs&amp;lt;/code&amp;gt; like so:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot;&amp;gt;&lt;br /&gt;
println!(&amp;quot;cargo:rustc-link-search=native={}&amp;quot;, lib_path);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The workings of this example&#039;s source code are too great to detail here line-by-line, but the example demonstrates how to use GLdc via the [https://github.com/dreamcast-rs/gldc-sys gldc-sys] crate, which contains unsafe/raw bindings to GLdc. Check out this crate&#039;s source code for an example of declaring and binding external C functions, constants, and structures so they can be used in Rust code. Since the entirety of the gldc-cube example uses C FFI functions without safe wrappers, the &amp;lt;code&amp;gt;main()&amp;lt;/code&amp;gt; source is wrapped in &amp;lt;code&amp;gt;unsafe {}&amp;lt;/code&amp;gt;. In the future, this would be much less necessary as higher level safe Rust bindings to KallistiOS and other libraries become mature.&lt;br /&gt;
&lt;br /&gt;
==Creating a Rust library==&lt;br /&gt;
Next, we&#039;ll demonstrate creating a Rust library with &amp;lt;code&amp;gt;kos-cargo&amp;lt;/code&amp;gt; that can be included in other Dreamcast code. This will follow the [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/no_std/addlib &amp;lt;code&amp;gt;addlib&amp;lt;/code&amp;gt;] example. Once again, this project&#039;s initial setup is done the same as the above &amp;lt;code&amp;gt;hello&amp;lt;/code&amp;gt; example, but you&#039;ll create the new project using &amp;lt;code&amp;gt;kos-cargo new --lib addlib&amp;lt;/code&amp;gt; to specify that we&#039;re creating a library named &amp;lt;code&amp;gt;addlib&amp;lt;/code&amp;gt;. You&#039;ll also need to add the following text to this project&#039;s &amp;lt;code&amp;gt;Cargo.toml&amp;lt;/code&amp;gt; file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;toml&amp;quot;&amp;gt;&lt;br /&gt;
[lib]&lt;br /&gt;
crate-type = [&amp;quot;staticlib&amp;quot;]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This tells Rust to build a static &amp;lt;code&amp;gt;.a&amp;lt;/code&amp;gt; library archive file from our code, which is located in &amp;lt;code&amp;gt;src/lib.rs&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; line&amp;gt;&lt;br /&gt;
#![no_std]&lt;br /&gt;
extern crate alloc;&lt;br /&gt;
use kos::print;&lt;br /&gt;
&lt;br /&gt;
#[no_mangle]&lt;br /&gt;
pub extern &amp;quot;C&amp;quot; fn print_added(a: isize, b: isize) {&lt;br /&gt;
    print!(&amp;quot;{}&amp;quot;, a + b);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
#[no_mangle]&lt;br /&gt;
pub extern &amp;quot;C&amp;quot; fn add_integers(a: isize, b: isize) -&amp;gt; isize {&lt;br /&gt;
    a + b&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The source code here starts similarly to the &amp;quot;Hello, world!&amp;quot; example, except we don&#039;t need to specify &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;#![no_main]&amp;lt;/syntaxhighlight&amp;gt; as this is a library which wouldn&#039;t have a &amp;lt;code&amp;gt;main()&amp;lt;/code&amp;gt; function anyway.&lt;br /&gt;
&lt;br /&gt;
Two simple functions are provided: one for adding two integers and returning the result, and another for adding two integers and printing the result as text. Because these functions use &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;#[no_mangle]&amp;lt;/syntaxhighlight&amp;gt; and are declared &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;extern &amp;quot;C&amp;quot;&amp;lt;/syntaxhighlight&amp;gt;, they can be called by name in C code that links this library.&lt;br /&gt;
&lt;br /&gt;
When built using &amp;lt;code&amp;gt;kos-cargo build&amp;lt;/code&amp;gt;, a &amp;lt;code&amp;gt;target/sh-elf/debug/libaddlib.a&amp;lt;/code&amp;gt; file will be generated. This can be linked into other projects to gain the use of these functions.&lt;br /&gt;
&lt;br /&gt;
For example, this can be added to a standard &amp;lt;code&amp;gt;Makefile&amp;lt;/code&amp;gt;-based KallistiOS project by editing the &amp;lt;code&amp;gt;Makefile&amp;lt;/code&amp;gt;:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;make&amp;quot;&amp;gt;&lt;br /&gt;
$(TARGET): $(OBJS)&lt;br /&gt;
	kos-cc -o $(TARGET) $(OBJS) -L/opt/toolchains/dc/rust/examples/cargo-addlib/target/sh-elf/debug -laddlib&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then, we can use the code in our C source:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/* Declare the external function from the Rust library */&lt;br /&gt;
int add_integers(int a, int b);&lt;br /&gt;
&lt;br /&gt;
/* Use the function */&lt;br /&gt;
printf(&amp;quot;Five plus six is %d\n&amp;quot;, add_integers(5, 6));&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Compiling individual modules into object files with rustc==&lt;br /&gt;
If we&#039;d like to mix C and Rust code in the same &amp;lt;code&amp;gt;Makefile&amp;lt;/code&amp;gt;-based KallistiOS project without building an entirely separate library, we can do that as well. This is demonstrated in the [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/no_std/hello-make &amp;lt;code&amp;gt;hello-make&amp;lt;/code&amp;gt;] example.&lt;br /&gt;
&lt;br /&gt;
Instead of using &amp;lt;code&amp;gt;kos-cargo&amp;lt;/code&amp;gt;, we can invoke the &amp;lt;code&amp;gt;kos-rustc&amp;lt;/code&amp;gt; via a &amp;lt;code&amp;gt;Makefile&amp;lt;/code&amp;gt; to build Rust modules. If we assume the Rust file is named &amp;lt;code&amp;gt;example.rs&amp;lt;/code&amp;gt;, you&#039;ll need to add &amp;lt;code&amp;gt;example.o&amp;lt;/code&amp;gt; as an object file in your &amp;lt;code&amp;gt;Makefile&amp;lt;/code&amp;gt;&#039;s &amp;lt;code&amp;gt;OBJS =&amp;lt;/code&amp;gt; declaration. For example, if the project has two source files &amp;lt;code&amp;gt;hello_c.c&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;hello_rust.rs&amp;lt;/code&amp;gt;, our &amp;lt;code&amp;gt;Makefile&amp;lt;/code&amp;gt; would have a line like this:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;make&amp;quot;&amp;gt;&lt;br /&gt;
OBJS = hello_c.o hello_rust.o&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The example code demonstrates starting a C &amp;lt;code&amp;gt;main()&amp;lt;/code&amp;gt; function to call a Rust function which builds a &amp;lt;code&amp;gt;String&amp;lt;/code&amp;gt; containing the &amp;quot;Hello, world!&amp;quot; text which is passed back to a C function which prints &amp;lt;code&amp;gt;String&amp;lt;/code&amp;gt;s.&lt;br /&gt;
&lt;br /&gt;
==Using the libc crate with KallistiOS==&lt;br /&gt;
If any of the crates in your project&#039;s dependency tree requires the &#039;&#039;&#039;libc&#039;&#039;&#039; crate, you will need to override fetching the default crate with our custom libc crate with KallistiOS/Dreamcast definitions. This custom version was copied to &amp;lt;code&amp;gt;/opt/toolchains/dc/rust/libc&amp;lt;/code&amp;gt; when building the Rust sysroot earlier. Add the following text to your &amp;lt;code&amp;gt;Cargo.toml&amp;lt;/code&amp;gt;:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;toml&amp;quot;&amp;gt;&lt;br /&gt;
[patch.crates-io]&lt;br /&gt;
libc = { path = &amp;quot;/opt/toolchains/dc/rust/libc&amp;quot; }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Adjusting build flags==&lt;br /&gt;
Build settings can be adjusted through the &amp;lt;code&amp;gt;KOS_RCG_RUSTFLAGS&amp;lt;/code&amp;gt; variable in the &amp;lt;code&amp;gt;environ.sh&amp;lt;/code&amp;gt; file under the &amp;lt;code&amp;gt;### Rust Flags&amp;lt;/code&amp;gt; section. &lt;br /&gt;
* To pass arguments to the &#039;&#039;&#039;rustc&#039;&#039;&#039; frontend, e.g. to disable debug assertions:&lt;br /&gt;
** &amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot; inline&amp;gt;export KOS_RCG_RUSTFLAGS=&amp;quot;${KOS_RCG_RUSTFLAGS} -Cdebug-assertions=no&amp;quot;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* To pass arguments to the GCC backend, e.g. to enable the &amp;lt;code&amp;gt;-freorder-blocks-algorithm=simple&amp;lt;/code&amp;gt; optimization for Rust code:&lt;br /&gt;
** &amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot; inline&amp;gt;export KOS_RCG_RUSTFLAGS=&amp;quot;${KOS_RCG_RUSTFLAGS} -Cllvm-args=-freorder-blocks-algorithm=simple&amp;quot;&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Darc</name></author>
	</entry>
	<entry>
		<id>https://dreamcast.wiki/wiki/index.php?title=Rust_on_Dreamcast&amp;diff=3632</id>
		<title>Rust on Dreamcast</title>
		<link rel="alternate" type="text/html" href="https://dreamcast.wiki/wiki/index.php?title=Rust_on_Dreamcast&amp;diff=3632"/>
		<updated>2024-10-25T16:23:26Z</updated>

		<summary type="html">&lt;p&gt;Darc: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[File:Rust-dc-logo.png|thumb|Ferris holding his Dreamcast controller]]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Rust&#039;&#039;&#039; is a popular modern programming language focusing on performance, memory safety, and program correctness. As a &amp;quot;blazingly fast&amp;quot; low-level language, it is viable for running on a Dreamcast console. Doing so presents a bit of a challenge, however, as the official Rust compiler is based on the [https://llvm.org/ LLVM] toolchain infrastructure, which does not support the Dreamcast CPU&#039;s SuperH architecture. Dreamcast programming is instead typically done with [https://gcc.gnu.org/ GCC], the GNU Compiler Collection. There are currently two viable solutions to this challenge:&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;rustc_codegen_gcc&#039;&#039;&#039;, the current preferred method: An experimental codegen backend for the official Rust &#039;&#039;&#039;rustc&#039;&#039;&#039; compiler which interfaces with GCC&#039;s libgccjit API&lt;br /&gt;
* &#039;&#039;&#039;gccrs&#039;&#039;&#039;: an experimental Rust frontend for GCC&lt;br /&gt;
&lt;br /&gt;
Neither solution is complete at this time, but &#039;&#039;&#039;rustc_codegen_gcc&#039;&#039;&#039; is much further along and is quite usable with some patience with its current limitations. On the other hand, &#039;&#039;&#039;gccrs&#039;&#039;&#039; can compile for Dreamcast, but is in an earlier state. Below we will focus on using &#039;&#039;&#039;rustc_codegen_gcc&#039;&#039;&#039;. For more information on using gccrs, see the [[gccrs]] page.&lt;br /&gt;
&lt;br /&gt;
=Current Status=&lt;br /&gt;
* Programs can be written using the full &#039;&#039;&#039;std&#039;&#039;&#039; library or &#039;&#039;&#039;no_std&#039;&#039;&#039;. In the case of &#039;&#039;&#039;no_std&#039;&#039;&#039; programs, a memory allocator is provided so &#039;&#039;&#039;alloc&#039;&#039;&#039; types can be used (Vec, String, Box, etc.).&lt;br /&gt;
* A [https://github.com/dreamcast-rs/KallistiOS custom version of KallistiOS] v2.1.0 (latest stable release) is used.&lt;br /&gt;
** This version includes minor changes for Rust and a beta libpthread addon layer to interface with the Rust standard library.&lt;br /&gt;
** Wrappers are provided to invoke &#039;&#039;&#039;cargo&#039;&#039;&#039; and &#039;&#039;&#039;rustc&#039;&#039;&#039; in a familiar manner to compile projects.&lt;br /&gt;
* A [https://github.com/dreamcast-rs/kos-rs &#039;&#039;&#039;kos-rs&#039;&#039;&#039;] crate is provided for easily linking KallistiOS into Rust crates.&lt;br /&gt;
** Some very minimal bindings to KallistiOS functions are provided, with expansion of these bindings in progress.&lt;br /&gt;
** Documentation for the bindings is available at [https://kos-rs.dreamcast.wiki/ kos-rs.dreamcast.wiki].&lt;br /&gt;
** If bindings do not yet exist for desired functionality, wrapping of KallistiOS C functions is required to get proper use of KallistiOS at this time. See the [https://doc.rust-lang.org/nomicon/ffi.html Foreign Function Interface page] in the Rustonomicon for further understanding. &lt;br /&gt;
** A custom [https://github.com/dreamcast-rs/libc/tree/libc-0.2-kos &#039;&#039;&#039;libc&#039;&#039;&#039; crate with KallistiOS support] is provided.&lt;br /&gt;
* Code generation uses &#039;&#039;&#039;rustc_codegen_gcc&#039;&#039;&#039;, so its limitations apply here. See the [https://blog.antoyo.xyz/ development blog] for more information on its progress.&lt;br /&gt;
** rustc_codegen_gcc is pinned to a particular Rust nightly version, but is synced regularly. The current version in use is the &#039;&#039;&#039;2024-08-10&#039;&#039;&#039; nightly.&lt;br /&gt;
** A [https://github.com/dreamcast-rs/rust/tree/kos-2024-08-10 KallistiOS-patched version of the latest nightly] is maintained and updated regularly. &lt;br /&gt;
** Panic unwinding, debug info, LTO, etc. are not currently available.&lt;br /&gt;
** Architectures dependent on rustc_codegen_gcc are not yet able to be added to the rustc frontend, so a workaround is necessary: MIPS is falsely selected for KallistiOS, causing rustc to emit objects with a MIPS header and a provided wrapper is used when linking to rewrite these MIPS headers to SuperH before passing the compiled objects to the linker.&lt;br /&gt;
* A separate [https://github.com/rust-lang/gcc development version of the GCC toolchain] must be used.&lt;br /&gt;
** This version of GCC contains the latest libgccjit patches necessary to interface with rustc_codegen_gcc, and will be built to work with the custom KallistiOS&#039;s libpthread layer.&lt;br /&gt;
** New updates to rustc_codegen_gcc often require new updates to GCC, so recompiling GCC regularly will be necessary to keep up with the latest updates.&lt;br /&gt;
** The development version of GCC does not always compile well on every platform. Using a modern Linux platform is recommended for maximum compatibility.&lt;br /&gt;
** The custom KallistiOS and GCC environment can still be used to compile code or projects written in C, C++, etc.&lt;br /&gt;
* KallistiOS and all related libraries and tools must be compiled using the &amp;lt;code&amp;gt;-m4-single&amp;lt;/code&amp;gt; ABI. This is already set up for you in the guide below, but if external libraries are being linked to Rust projects, make sure that code is compiled using the proper ABI.&lt;br /&gt;
&lt;br /&gt;
=Setting up a Rust environment for Dreamcast development=&lt;br /&gt;
The following guide will provide instructions for setting up a Rust development environment for Dreamcast. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;If you run into any errors or other challenges while following this tutorial, or simply need clarification on any of the steps, feel free to ask for assistance on the [https://dcemulation.org/phpBB/viewforum.php?f=29 message board] or [https://discord.gg/wDEfkUh63h our Discord server] and we would be happy to aid you and update the guide for the benefit of future readers and others in the community.&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
==Prerequisites==&lt;br /&gt;
First, we will need to set up the prerequisites.&lt;br /&gt;
&lt;br /&gt;
* You must have the [https://rustup.rs/ rustup] tool installed for your operating system to manage Rust toolchain installations. It will automatically manage the latest Rust nightly installations for you. You cannot use a Rust toolchain that may be provided by your operating system vendor.&lt;br /&gt;
* Install the dependency packages for your operating system. These are listed in the &#039;&#039;&#039;Dependencies&#039;&#039;&#039; section on the [[Getting Started with Dreamcast development#Dependencies]] page, with lists provided for most common operating systems.&lt;br /&gt;
* We will install our custom Rust toolchain environment within &amp;lt;code&amp;gt;/opt/toolchains/dc/rust&amp;lt;/code&amp;gt;. If it does not already exist, create the &amp;lt;code&amp;gt;/opt/toolchains/dc&amp;lt;/code&amp;gt; directory and grant it proper permissions using the following commands. &lt;br /&gt;
 sudo mkdir -p /opt/toolchains/dc&lt;br /&gt;
 sudo chmod -R 755 /opt/toolchains/dc&lt;br /&gt;
 sudo chown -R $(id -u):$(id -g) /opt/toolchains/dc&lt;br /&gt;
* Clone the [https://github.com/dreamcast-rs/rust-for-dreamcast/ Rust for Dreamcast] repo containing necessary support files to &amp;lt;code&amp;gt;/opt/toolchains/dc/rust&amp;lt;/code&amp;gt;:&lt;br /&gt;
 git clone https://github.com/dreamcast-rs/rust-for-dreamcast.git /opt/toolchains/dc/rust&lt;br /&gt;
&lt;br /&gt;
==Installing our custom KallistiOS and GCC toolchain==&lt;br /&gt;
Clone the git repository for our custom Rust-patched version of KallistiOS v2.1.0 stable:&lt;br /&gt;
 git clone https://github.com/dreamcast-rs/KallistiOS /opt/toolchains/dc/rust/kos&lt;br /&gt;
&lt;br /&gt;
Enter the dc-chain directory:&lt;br /&gt;
 cd /opt/toolchains/dc/rust/kos/utils/dc-chain&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;Makefile.default.cfg&amp;lt;/code&amp;gt; file in this directory has been customized for you, selecting the proper toolchain profile, paths, and build options necessary for building a Rust toolchain using this guide. It is not recommended to adjust the defaults.&lt;br /&gt;
&lt;br /&gt;
To build the GCC toolchain using 2 CPU cores, simply type:&lt;br /&gt;
 make makejobs=2&lt;br /&gt;
Adjust the &amp;lt;code&amp;gt;makejobs=...&amp;lt;/code&amp;gt; value to use the number of CPU cores available to you on your system in order to speed up compilation. This may take a while to build, so be patient.&lt;br /&gt;
&lt;br /&gt;
Once the GCC toolchain is built, source the &amp;lt;code&amp;gt;environ.sh&amp;lt;/code&amp;gt; KallistiOS environment settings. A custom &amp;lt;code&amp;gt;environ.sh&amp;lt;/code&amp;gt; file has been provided for you, containing settings pre-adjusted for Rust development using this guide.&lt;br /&gt;
&lt;br /&gt;
You will need to run the source command to apply the KallistiOS environment settings to your currently running shell. Run the following now, &#039;&#039;&#039;and&#039;&#039;&#039; &#039;&#039;whenever&#039;&#039; you open a new shell to work on Dreamcast projects:&lt;br /&gt;
 source /opt/toolchains/dc/rust/misc/environ.sh&lt;br /&gt;
&lt;br /&gt;
Enter the KallistiOS directory:&lt;br /&gt;
 cd /opt/toolchains/dc/rust/kos&lt;br /&gt;
&lt;br /&gt;
Build KallistiOS:&lt;br /&gt;
 make&lt;br /&gt;
&lt;br /&gt;
==Building rustc_codegen_gcc and Rust sysroot==&lt;br /&gt;
Now that we have a working KallistiOS environment suitable for Rust development set up, we can build the Rust compiler compiler and sysroot components. A script is provided which will download the necessary components from the respective repositories and compile them for you. Run the installer script:&lt;br /&gt;
&lt;br /&gt;
 /opt/toolchains/dc/rust/misc/install-rust.sh&lt;br /&gt;
&lt;br /&gt;
If all goes well, you&#039;ll see &#039;&#039;&#039;Rust for KallistiOS/Dreamcast installed!&#039;&#039;&#039; message!&lt;br /&gt;
&lt;br /&gt;
==Building KOS ports libraries==&lt;br /&gt;
If desired, you may also built the KOS ports collection of libraries for use with your Rust environment. It is recommended to maintain a separate KOS ports installation for linking with Rust projects in &amp;lt;code&amp;gt;/opt/toolchains/dc/rust/kos-ports&amp;lt;/code&amp;gt;. This is because the Rust environment is using the &amp;lt;code&amp;gt;-m4-single&amp;lt;/code&amp;gt; ABI, but the KallistiOS default installation will be using the &amp;lt;code&amp;gt;-m4-single-only&amp;lt;/code&amp;gt;, which cannot be mixed and matched.&lt;br /&gt;
&lt;br /&gt;
Clone the kos-ports repository to your system:&lt;br /&gt;
 git clone https://github.com/KallistiOS/kos-ports /opt/toolchains/dc/rust/kos-ports&lt;br /&gt;
An individual port can be built and installed by entering its directory and running the proper command. For example, installing GLdc:&lt;br /&gt;
 cd /opt/toolchains/dc/rust/kos-ports/libGL&lt;br /&gt;
 make install&lt;br /&gt;
Or, instead, you may run the script to build all of the included ports:&lt;br /&gt;
 /opt/toolchains/dc/rust/kos-ports/utils/build-all.sh&lt;br /&gt;
&lt;br /&gt;
=Using Rust for Dreamcast=&lt;br /&gt;
If all went well, you will now have a working Rust for Dreamcast environment!&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;cargo&#039;&#039;&#039; can be invoked to target Dreamcast using the &amp;lt;code&amp;gt;kos-cargo&amp;lt;/code&amp;gt; command.&lt;br /&gt;
** &amp;lt;code&amp;gt;kos-cargo run&amp;lt;/code&amp;gt; can be used to build and run code on a Dreamcast console using [[dcload-ip]] or [[dcload-serial]]. You will need to set up the &amp;lt;code&amp;gt;KOS_LOADER&amp;lt;/code&amp;gt; variable with the necessary command in the &amp;lt;code&amp;gt;/opt/toolchains/dc/rust/misc/environ.sh&amp;lt;/code&amp;gt; file (and then re-source the file for the change to take effect).&lt;br /&gt;
* &#039;&#039;&#039;rustc&#039;&#039;&#039; can be invoked to target Dreamcast using the &amp;lt;code&amp;gt;kos-rustc&amp;lt;/code&amp;gt; command.&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
Examples are included with the Rust for Dreamcast repo to help you get started.&lt;br /&gt;
* &#039;&#039;&#039;no_std&#039;&#039;&#039; examples:&lt;br /&gt;
** [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/no_std/hello-cargo &amp;lt;code&amp;gt;no_std/hello-cargo&amp;lt;/code&amp;gt;] demonstrates how to create a simple &amp;quot;Hello, world!&amp;quot; application using cargo.&lt;br /&gt;
** [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/no_std/hello-make &amp;lt;code&amp;gt;no_std/hello-make&amp;lt;/code&amp;gt;] demonstrates how to create a simple &amp;quot;Hello, world!&amp;quot; application using standard Makefiles.&lt;br /&gt;
** [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/no_std/gldc-cube &amp;lt;code&amp;gt;no_std/gldc-cube&amp;lt;/code&amp;gt;] demonstrates a Rust project using the GLdc KOS port.&lt;br /&gt;
** [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/no_std/addlib &amp;lt;code&amp;gt;no_std/addlib&amp;lt;/code&amp;gt;] demonstrates how to create a Rust library that can be included with a KallistiOS project.&lt;br /&gt;
* &#039;&#039;&#039;std&#039;&#039;&#039; examples:&lt;br /&gt;
** [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/std/atomics &amp;lt;code&amp;gt;std/atomics&amp;lt;/code&amp;gt;] demonstrates the use of atomic functions.&lt;br /&gt;
** [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/std/filesystem-io &amp;lt;code&amp;gt;std/filesystem-io&amp;lt;/code&amp;gt;] demonstrates listing directory contents and opening files.&lt;br /&gt;
** [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/std/network-time &amp;lt;code&amp;gt;std/network-time&amp;lt;/code&amp;gt;] demonstrates displaying the system time and retrieving the current UTC time from an NTP server using the [[Broadband adapter]] or [[LAN adapter]].&lt;br /&gt;
** [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/std/threads &amp;lt;code&amp;gt;std/threads&amp;lt;/code&amp;gt;] demonstrates the use of threads.&lt;br /&gt;
** [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/std/tokio-async &amp;lt;code&amp;gt;std/tokio-async&amp;lt;/code&amp;gt;] demonstrates the use of the &#039;&#039;&#039;tokio&#039;&#039;&#039; async runtime.&lt;br /&gt;
&lt;br /&gt;
==Creating a new Rust project with Cargo==&lt;br /&gt;
First, we&#039;ll demonstrate creating a new no_std &amp;quot;Hello, world!&amp;quot; project with &amp;lt;code&amp;gt;kos-cargo&amp;lt;/code&amp;gt;. This will follow the [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/no_std/hello-cargo &amp;lt;code&amp;gt;no_std/cargo-hello&amp;lt;/code&amp;gt;] example included in the Rust-for-Dreamcast repo. &lt;br /&gt;
&lt;br /&gt;
In a directory of your choosing, let&#039;s invoke &amp;lt;code&amp;gt;kos-cargo&amp;lt;/code&amp;gt; to create a new project and then enter the directory:&lt;br /&gt;
 kos-cargo new hello&lt;br /&gt;
 cd hello&lt;br /&gt;
&lt;br /&gt;
Let&#039;s add our kos-rs crate to gain access to current KallistiOS bindings. This will be a &amp;lt;code&amp;gt;no_std&amp;lt;/code&amp;gt; project, so we will disable the use of the &amp;lt;code&amp;gt;std&amp;lt;/code&amp;gt; library by using &amp;lt;syntaxhighlight lang=&amp;quot;toml&amp;quot; inline&amp;gt;default-features = false&amp;lt;/syntaxhighlight&amp;gt;. Open &amp;lt;code&amp;gt;Cargo.toml&amp;lt;/code&amp;gt; in your text editor and add:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;toml&amp;quot;&amp;gt;&lt;br /&gt;
[dependencies]&lt;br /&gt;
kos = { package = &amp;quot;kos-rs&amp;quot;, git = &amp;quot;https://github.com/dreamcast-rs/kos-rs&amp;quot;, default-features = false }&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we can open up &amp;lt;code&amp;gt;src/main.rs&amp;lt;/code&amp;gt; and write our &amp;quot;Hello, world!&amp;quot; example code:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; line&amp;gt;&lt;br /&gt;
#![no_std]&lt;br /&gt;
#![no_main]&lt;br /&gt;
extern crate alloc;&lt;br /&gt;
use kos::println;&lt;br /&gt;
&lt;br /&gt;
#[no_mangle]&lt;br /&gt;
extern &amp;quot;C&amp;quot; fn main(_argc: isize, _argv: *const *const u8) -&amp;gt; isize {&lt;br /&gt;
    println!(&amp;quot;Hello, world from Rust!&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
    0&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;#![no_std]&amp;lt;/syntaxhighlight&amp;gt; and &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;#![no_main]&amp;lt;/syntaxhighlight&amp;gt; tell Rust that our project does not use the standard library and we will not have Rust use a &amp;lt;code&amp;gt;main&amp;lt;/code&amp;gt; function as an entry point -- this will be handled by KallistiOS.&lt;br /&gt;
* &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;extern crate alloc;&amp;lt;/syntaxhighlight&amp;gt; tells Rust to use the alloc crate to gain access to heap-allocated types (in our case, &amp;lt;code&amp;gt;String&amp;lt;/code&amp;gt;).&lt;br /&gt;
* &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;use kos::println!;&amp;lt;/syntaxhighlight&amp;gt; tells Rust to use the &amp;lt;code&amp;gt;println!&amp;lt;/code&amp;gt; macro defined in the kos-rs crate. With this, we can print output to our &amp;lt;code&amp;gt;dc-tool&amp;lt;/code&amp;gt; console.&lt;br /&gt;
* &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;#[no_mangle]&amp;lt;/syntaxhighlight&amp;gt; tells Rust to disable name mangling so that the &amp;lt;code&amp;gt;main&amp;lt;/code&amp;gt; function can be used by KallistiOS.&lt;br /&gt;
* Finally, we have a &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;fn main&amp;lt;/syntaxhighlight&amp;gt; with the function signature of a typical C &amp;lt;code&amp;gt;main&amp;lt;/code&amp;gt; function, containing a basic &amp;quot;Hello, world!&amp;quot; exclamation.&lt;br /&gt;
&lt;br /&gt;
Now we can use &amp;lt;code&amp;gt;kos-cargo build&amp;lt;/code&amp;gt; to build our project. If all goes well, there will be a &amp;lt;code&amp;gt;target/sh-elf/debug/hello.elf&amp;lt;/code&amp;gt; file that can be sent to the Dreamcast with &amp;lt;code&amp;gt;dc-tool&amp;lt;/code&amp;gt;.&lt;br /&gt;
If you have &amp;lt;code&amp;gt;KOS_LOADER&amp;lt;/code&amp;gt; set in the &amp;lt;code&amp;gt;environ.sh&amp;lt;/code&amp;gt; file, you can invoke it directly with &amp;lt;code&amp;gt;kos-cargo run&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Creating a Rust project using kos-ports libraries==&lt;br /&gt;
[[File:Rust-Cube rustc codegen-gcc demo.gif|thumb|cube example in action]]&lt;br /&gt;
The [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/no_std/gldc-cube &amp;lt;code&amp;gt;gldc-cube&amp;lt;/code&amp;gt;] example demonstrates creating a rotating 3D cube using Rust as the primary language, while calling C functions provided by the &#039;&#039;&#039;GLdc&#039;&#039;&#039; library available in kos-ports. This project&#039;s initial setup is done the same as the above &amp;lt;code&amp;gt;hello&amp;lt;/code&amp;gt; example.&lt;br /&gt;
&lt;br /&gt;
For this example, we&#039;ll need to convert JPEG textures to VQ-encoded textures at compile time using the &amp;lt;code&amp;gt;vqenc&amp;lt;/code&amp;gt; utility includes with KallistiOS. To do this, we&#039;ll add a &amp;lt;code&amp;gt;build.rs&amp;lt;/code&amp;gt; file to the root of the crate with the following code (abridged to show the conversion of only one texture):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot;&amp;gt;&lt;br /&gt;
fn main() {&lt;br /&gt;
    let kos_base = std::env::var(&amp;quot;KOS_BASE&amp;quot;).expect(&amp;quot;Missing $KOS_BASE -- KallistiOS environment not sourced!&amp;quot;);&lt;br /&gt;
    let vqenc_cmd = kos_base + &amp;quot;/utils/vqenc/vqenc&amp;quot;;&lt;br /&gt;
    Command::new(&amp;amp;vqenc_cmd)&lt;br /&gt;
        .arg(&amp;quot;-t&amp;quot;)&lt;br /&gt;
        .arg(&amp;quot;-v&amp;quot;)&lt;br /&gt;
        .arg(&amp;quot;rsrc/tex_claw.jpg&amp;quot;)&lt;br /&gt;
        .output()&lt;br /&gt;
        .expect(&amp;quot;vqenc on tex_claw.jpg failed!&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
The texture files are then included in our project using the &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;include_bytes!&amp;lt;/syntaxhighlight&amp;gt; macro.&lt;br /&gt;
&lt;br /&gt;
We will need to link the GLdc library in this example, but we don&#039;t need to do anything special to add the paths to this library, because adding the common paths to KallistiOS libraries is already done for us in the kos-rs crate. However, if in your project you need to include a separate unique library path, you can add it in your &amp;lt;code&amp;gt;build.rs&amp;lt;/code&amp;gt; like so:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot;&amp;gt;&lt;br /&gt;
println!(&amp;quot;cargo:rustc-link-search=native={}&amp;quot;, lib_path);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The workings of this example&#039;s source code are too great to detail here line-by-line, but the example demonstrates declaring and binding external C functions, constants, and structures and then using them in Rust code. The declarations are preceded by &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;#[link(name = &amp;quot;GL&amp;quot;)]&amp;lt;/syntaxhighlight&amp;gt; to tell Rust to link in &#039;&#039;&#039;libGL&#039;&#039;&#039; (GLdc). Since the entirety of the example uses C FFI functions without safe wrappers, the &amp;lt;code&amp;gt;main()&amp;lt;/code&amp;gt; source is wrapped in &amp;lt;code&amp;gt;unsafe {}&amp;lt;/code&amp;gt;. In the future, this would be much less necessary as higher level safe Rust bindings to KallistiOS and other libraries become mature.&lt;br /&gt;
&lt;br /&gt;
==Creating a Rust library==&lt;br /&gt;
Next, we&#039;ll demonstrate creating a Rust library with &amp;lt;code&amp;gt;kos-cargo&amp;lt;/code&amp;gt; that can be included in other Dreamcast code. This will follow the [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/no_std/addlib &amp;lt;code&amp;gt;addlib&amp;lt;/code&amp;gt;] example. Once again, this project&#039;s initial setup is done the same as the above &amp;lt;code&amp;gt;hello&amp;lt;/code&amp;gt; example, but you&#039;ll create the new project using &amp;lt;code&amp;gt;kos-cargo new --lib addlib&amp;lt;/code&amp;gt; to specify that we&#039;re creating a library named &amp;lt;code&amp;gt;addlib&amp;lt;/code&amp;gt;. You&#039;ll also need to add the following text to this project&#039;s &amp;lt;code&amp;gt;Cargo.toml&amp;lt;/code&amp;gt; file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;toml&amp;quot;&amp;gt;&lt;br /&gt;
[lib]&lt;br /&gt;
crate-type = [&amp;quot;staticlib&amp;quot;]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This tells Rust to build a static &amp;lt;code&amp;gt;.a&amp;lt;/code&amp;gt; library archive file from our code, which is located in &amp;lt;code&amp;gt;src/lib.rs&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; line&amp;gt;&lt;br /&gt;
#![no_std]&lt;br /&gt;
extern crate alloc;&lt;br /&gt;
use kos::print;&lt;br /&gt;
&lt;br /&gt;
#[no_mangle]&lt;br /&gt;
pub extern &amp;quot;C&amp;quot; fn print_added(a: isize, b: isize) {&lt;br /&gt;
    print!(&amp;quot;{}&amp;quot;, a + b);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
#[no_mangle]&lt;br /&gt;
pub extern &amp;quot;C&amp;quot; fn add_integers(a: isize, b: isize) -&amp;gt; isize {&lt;br /&gt;
    a + b&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The source code here starts similarly to the &amp;quot;Hello, world!&amp;quot; example, except we don&#039;t need to specify &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;#![no_main]&amp;lt;/syntaxhighlight&amp;gt; as this is a library which wouldn&#039;t have a &amp;lt;code&amp;gt;main()&amp;lt;/code&amp;gt; function anyway.&lt;br /&gt;
&lt;br /&gt;
Two simple functions are provided: one for adding two integers and returning the result, and another for adding two integers and printing the result as text. Because these functions use &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;#[no_mangle]&amp;lt;/syntaxhighlight&amp;gt; and are declared &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;extern &amp;quot;C&amp;quot;&amp;lt;/syntaxhighlight&amp;gt;, they can be called by name in C code that links this library.&lt;br /&gt;
&lt;br /&gt;
When built using &amp;lt;code&amp;gt;kos-cargo build&amp;lt;/code&amp;gt;, a &amp;lt;code&amp;gt;target/sh-elf/debug/libaddlib.a&amp;lt;/code&amp;gt; file will be generated. This can be linked into other projects to gain the use of these functions.&lt;br /&gt;
&lt;br /&gt;
For example, this can be added to a standard &amp;lt;code&amp;gt;Makefile&amp;lt;/code&amp;gt;-based KallistiOS project by editing the &amp;lt;code&amp;gt;Makefile&amp;lt;/code&amp;gt;:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;make&amp;quot;&amp;gt;&lt;br /&gt;
$(TARGET): $(OBJS)&lt;br /&gt;
	kos-cc -o $(TARGET) $(OBJS) -L/opt/toolchains/dc/rust/examples/cargo-addlib/target/sh-elf/debug -laddlib&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then, we can use the code in our C source:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/* Declare the external function from the Rust library */&lt;br /&gt;
int add_integers(int a, int b);&lt;br /&gt;
&lt;br /&gt;
/* Use the function */&lt;br /&gt;
printf(&amp;quot;Five plus six is %d\n&amp;quot;, add_integers(5, 6));&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Compiling individual modules into object files with rustc==&lt;br /&gt;
If we&#039;d like to mix C and Rust code in the same &amp;lt;code&amp;gt;Makefile&amp;lt;/code&amp;gt;-based KallistiOS project without building an entirely separate library, we can do that as well. This is demonstrated in the [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/no_std/hello-make &amp;lt;code&amp;gt;hello-make&amp;lt;/code&amp;gt;] example.&lt;br /&gt;
&lt;br /&gt;
Instead of using &amp;lt;code&amp;gt;kos-cargo&amp;lt;/code&amp;gt;, we can invoke the &amp;lt;code&amp;gt;kos-rustc&amp;lt;/code&amp;gt; via a &amp;lt;code&amp;gt;Makefile&amp;lt;/code&amp;gt; to build Rust modules. If we assume the Rust file is named &amp;lt;code&amp;gt;example.rs&amp;lt;/code&amp;gt;, you&#039;ll need to add &amp;lt;code&amp;gt;example.o&amp;lt;/code&amp;gt; as an object file in your &amp;lt;code&amp;gt;Makefile&amp;lt;/code&amp;gt;&#039;s &amp;lt;code&amp;gt;OBJS =&amp;lt;/code&amp;gt; declaration. For example, if the project has two source files &amp;lt;code&amp;gt;hello_c.c&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;hello_rust.rs&amp;lt;/code&amp;gt;, our &amp;lt;code&amp;gt;Makefile&amp;lt;/code&amp;gt; would have a line like this:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;make&amp;quot;&amp;gt;&lt;br /&gt;
OBJS = hello_c.o hello_rust.o&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The example code demonstrates starting a C &amp;lt;code&amp;gt;main()&amp;lt;/code&amp;gt; function to call a Rust function which builds a &amp;lt;code&amp;gt;String&amp;lt;/code&amp;gt; containing the &amp;quot;Hello, world!&amp;quot; text which is passed back to a C function which prints &amp;lt;code&amp;gt;String&amp;lt;/code&amp;gt;s.&lt;br /&gt;
&lt;br /&gt;
==Using the libc crate with KallistiOS==&lt;br /&gt;
If any of the crates in your project&#039;s dependency tree requires the &#039;&#039;&#039;libc&#039;&#039;&#039; crate, you will need to override fetching the default crate with our custom libc crate with KallistiOS/Dreamcast definitions. This custom version was copied to &amp;lt;code&amp;gt;/opt/toolchains/dc/rust/libc&amp;lt;/code&amp;gt; when building the Rust sysroot earlier. Add the following text to your &amp;lt;code&amp;gt;Cargo.toml&amp;lt;/code&amp;gt;:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;toml&amp;quot;&amp;gt;&lt;br /&gt;
[patch.crates-io]&lt;br /&gt;
libc = { path = &amp;quot;/opt/toolchains/dc/rust/libc&amp;quot; }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Adjusting build flags==&lt;br /&gt;
Build settings can be adjusted through the &amp;lt;code&amp;gt;KOS_RCG_RUSTFLAGS&amp;lt;/code&amp;gt; variable in the &amp;lt;code&amp;gt;environ.sh&amp;lt;/code&amp;gt; file under the &amp;lt;code&amp;gt;### Rust Flags&amp;lt;/code&amp;gt; section. &lt;br /&gt;
* To pass arguments to the &#039;&#039;&#039;rustc&#039;&#039;&#039; frontend, e.g. to disable debug assertions:&lt;br /&gt;
** &amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot; inline&amp;gt;export KOS_RCG_RUSTFLAGS=&amp;quot;${KOS_RCG_RUSTFLAGS} -Cdebug-assertions=no&amp;quot;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* To pass arguments to the GCC backend, e.g. to enable the &amp;lt;code&amp;gt;-freorder-blocks-algorithm=simple&amp;lt;/code&amp;gt; optimization for Rust code:&lt;br /&gt;
** &amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot; inline&amp;gt;export KOS_RCG_RUSTFLAGS=&amp;quot;${KOS_RCG_RUSTFLAGS} -Cllvm-args=-freorder-blocks-algorithm=simple&amp;quot;&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Darc</name></author>
	</entry>
	<entry>
		<id>https://dreamcast.wiki/wiki/index.php?title=Rust_on_Dreamcast&amp;diff=3631</id>
		<title>Rust on Dreamcast</title>
		<link rel="alternate" type="text/html" href="https://dreamcast.wiki/wiki/index.php?title=Rust_on_Dreamcast&amp;diff=3631"/>
		<updated>2024-10-25T15:18:43Z</updated>

		<summary type="html">&lt;p&gt;Darc: /* Current Status */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[File:Rust-dc-logo.png|thumb|Ferris holding his Dreamcast controller]]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Rust&#039;&#039;&#039; is a popular modern programming language focusing on performance, memory safety, and program correctness. As a &amp;quot;blazingly fast&amp;quot; low-level language, it is viable for running on a Dreamcast console. Doing so presents a bit of a challenge, however, as the official Rust compiler is based on the [https://llvm.org/ LLVM] toolchain infrastructure, which does not support the Dreamcast CPU&#039;s SuperH architecture. Dreamcast programming is instead typically done with [https://gcc.gnu.org/ GCC], the GNU Compiler Collection. There are currently two viable solutions to this challenge:&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;rustc_codegen_gcc&#039;&#039;&#039;, the current preferred method: An experimental codegen backend for the official Rust &#039;&#039;&#039;rustc&#039;&#039;&#039; compiler which interfaces with GCC&#039;s libgccjit API&lt;br /&gt;
* &#039;&#039;&#039;gccrs&#039;&#039;&#039;: an experimental Rust frontend for GCC&lt;br /&gt;
&lt;br /&gt;
Neither solution is complete at this time, but &#039;&#039;&#039;rustc_codegen_gcc&#039;&#039;&#039; is much further along and is quite usable with some patience with its current limitations. On the other hand, &#039;&#039;&#039;gccrs&#039;&#039;&#039; can compile for Dreamcast, but is in an earlier state. Below we will focus on using &#039;&#039;&#039;rustc_codegen_gcc&#039;&#039;&#039;. For more information on using gccrs, see the [[gccrs]] page.&lt;br /&gt;
&lt;br /&gt;
=Current Status=&lt;br /&gt;
* Programs can be written using the full &#039;&#039;&#039;std&#039;&#039;&#039; library or &#039;&#039;&#039;no_std&#039;&#039;&#039;. In the case of &#039;&#039;&#039;no_std&#039;&#039;&#039; programs, a memory allocator is provided so &#039;&#039;&#039;alloc&#039;&#039;&#039; types can be used (Vec, String, Box, etc.).&lt;br /&gt;
* A [https://github.com/dreamcast-rs/KallistiOS custom version of KallistiOS] v2.1.0 (latest stable release) is used.&lt;br /&gt;
** This version includes minor changes for Rust and a beta libpthread addon layer to interface with the Rust standard library.&lt;br /&gt;
** Wrappers are provided to invoke &#039;&#039;&#039;cargo&#039;&#039;&#039; and &#039;&#039;&#039;rustc&#039;&#039;&#039; in a familiar manner to compile projects.&lt;br /&gt;
* A [https://github.com/dreamcast-rs/kos-rs &#039;&#039;&#039;kos-rs&#039;&#039;&#039;] crate is provided for easily linking KallistiOS into Rust crates.&lt;br /&gt;
** Some very minimal bindings to KallistiOS functions are provided, with expansion of these bindings in progress.&lt;br /&gt;
** Documentation for the bindings is available at [https://kos-rs.dreamcast.wiki/ kos-rs.dreamcast.wiki].&lt;br /&gt;
** If bindings do not yet exist for desired functionality, wrapping of KallistiOS C functions is required to get proper use of KallistiOS at this time. See the [https://doc.rust-lang.org/nomicon/ffi.html Foreign Function Interface page] in the Rustonomicon for further understanding. &lt;br /&gt;
** A custom [https://github.com/dreamcast-rs/libc/tree/libc-0.2-kos &#039;&#039;&#039;libc&#039;&#039;&#039; crate with KallistiOS support] is provided.&lt;br /&gt;
* Code generation uses &#039;&#039;&#039;rustc_codegen_gcc&#039;&#039;&#039;, so its limitations apply here. See the [https://blog.antoyo.xyz/ development blog] for more information on its progress.&lt;br /&gt;
** rustc_codegen_gcc is pinned to a particular Rust nightly version, but is synced regularly. The current version in use is the &#039;&#039;&#039;2024-08-10&#039;&#039;&#039; nightly.&lt;br /&gt;
** A [https://github.com/dreamcast-rs/rust/tree/kos-2024-08-10 KallistiOS-patched version of the latest nightly] is maintained and updated regularly. &lt;br /&gt;
** Panic unwinding, debug info, LTO, etc. are not currently available.&lt;br /&gt;
** Architectures dependent on rustc_codegen_gcc are not yet able to be added to the rustc frontend, so a workaround is necessary: MIPS is falsely selected for KallistiOS, causing rustc to emit objects with a MIPS header and a provided wrapper is used when linking to rewrite these MIPS headers to SuperH before passing the compiled objects to the linker.&lt;br /&gt;
* A separate [https://github.com/rust-lang/gcc development version of the GCC toolchain] must be used.&lt;br /&gt;
** This version of GCC contains the latest libgccjit patches necessary to interface with rustc_codegen_gcc, and will be built to work with the custom KallistiOS&#039;s libpthread layer.&lt;br /&gt;
** New updates to rustc_codegen_gcc often require new updates to GCC, so recompiling GCC regularly will be necessary to keep up with the latest updates.&lt;br /&gt;
** The development version of GCC does not always compile well on every platform. Using a modern Linux platform is recommended for maximum compatibility.&lt;br /&gt;
** The custom KallistiOS and GCC environment can still be used to compile code or projects written in C, C++, etc.&lt;br /&gt;
* KallistiOS and all related libraries and tools must be compiled using the &amp;lt;code&amp;gt;-m4-single&amp;lt;/code&amp;gt; ABI. This is already set up for you in the guide below, but if external libraries are being linked to Rust projects, make sure that code is compiled using the proper ABI.&lt;br /&gt;
&lt;br /&gt;
=Setting up a Rust environment for Dreamcast development=&lt;br /&gt;
The following guide will provide instructions for setting up a Rust development environment for Dreamcast. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;If you run into any errors or other challenges while following this tutorial, or simply need clarification on any of the steps, feel free to ask for assistance on the [https://dcemulation.org/phpBB/viewforum.php?f=29 message board] or [https://discord.gg/wDEfkUh63h our Discord server] and we would be happy to aid you and update the guide for the benefit of future readers and others in the community.&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
==Prerequisites==&lt;br /&gt;
First, we will need to set up the prerequisites.&lt;br /&gt;
&lt;br /&gt;
* You must have the [https://rustup.rs/ rustup] tool installed for your operating system to manage Rust toolchain installations. It will automatically manage the latest Rust nightly installations for you. You cannot use a Rust toolchain that may be provided by your operating system vendor.&lt;br /&gt;
* Install the dependency packages for your operating system. These are listed in the &#039;&#039;&#039;Dependencies&#039;&#039;&#039; section on the [[Getting Started with Dreamcast development#Dependencies]] page, with lists provided for most common operating systems.&lt;br /&gt;
* We will install our custom Rust toolchain environment within &amp;lt;code&amp;gt;/opt/toolchains/dc/rust&amp;lt;/code&amp;gt;. If it does not already exist, create the &amp;lt;code&amp;gt;/opt/toolchains/dc&amp;lt;/code&amp;gt; directory and grant it proper permissions using the following commands. &lt;br /&gt;
 sudo mkdir -p /opt/toolchains/dc&lt;br /&gt;
 sudo chmod -R 755 /opt/toolchains/dc&lt;br /&gt;
 sudo chown -R $(id -u):$(id -g) /opt/toolchains/dc&lt;br /&gt;
* Clone the [https://github.com/dreamcast-rs/rust-for-dreamcast/ Rust for Dreamcast] repo containing necessary support files to &amp;lt;code&amp;gt;/opt/toolchains/dc/rust&amp;lt;/code&amp;gt;:&lt;br /&gt;
 git clone https://github.com/dreamcast-rs/rust-for-dreamcast.git /opt/toolchains/dc/rust&lt;br /&gt;
&lt;br /&gt;
==Installing our custom KallistiOS and GCC toolchain==&lt;br /&gt;
Clone the git repository for our custom Rust-patched version of KallistiOS v2.1.0 stable:&lt;br /&gt;
 git clone https://github.com/dreamcast-rs/KallistiOS /opt/toolchains/dc/rust/kos&lt;br /&gt;
&lt;br /&gt;
Enter the dc-chain directory:&lt;br /&gt;
 cd /opt/toolchains/dc/rust/kos/utils/dc-chain&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;Makefile.default.cfg&amp;lt;/code&amp;gt; file in this directory has been customized for you, selecting the proper toolchain profile, paths, and build options necessary for building a Rust toolchain using this guide. It is not recommended to adjust the defaults.&lt;br /&gt;
&lt;br /&gt;
To build the GCC toolchain using 2 CPU cores, simply type:&lt;br /&gt;
 make makejobs=2&lt;br /&gt;
Adjust the &amp;lt;code&amp;gt;makejobs=...&amp;lt;/code&amp;gt; value to use the number of CPU cores available to you on your system in order to speed up compilation. This may take a while to build, so be patient.&lt;br /&gt;
&lt;br /&gt;
Once the GCC toolchain is built, source the &amp;lt;code&amp;gt;environ.sh&amp;lt;/code&amp;gt; KallistiOS environment settings. A custom &amp;lt;code&amp;gt;environ.sh&amp;lt;/code&amp;gt; file has been provided for you, containing settings pre-adjusted for Rust development using this guide.&lt;br /&gt;
&lt;br /&gt;
You will need to run the source command to apply the KallistiOS environment settings to your currently running shell. Run the following now, &#039;&#039;&#039;and&#039;&#039;&#039; &#039;&#039;whenever&#039;&#039; you open a new shell to work on Dreamcast projects:&lt;br /&gt;
 source /opt/toolchains/dc/rust/misc/environ.sh&lt;br /&gt;
&lt;br /&gt;
Enter the KallistiOS directory:&lt;br /&gt;
 cd /opt/toolchains/dc/rust/kos&lt;br /&gt;
&lt;br /&gt;
Build KallistiOS:&lt;br /&gt;
 make&lt;br /&gt;
&lt;br /&gt;
==Building rustc_codegen_gcc and Rust sysroot==&lt;br /&gt;
Now that we have a working KallistiOS environment suitable for Rust development set up, we can build the Rust compiler compiler and sysroot components. A script is provided which will download the necessary components from the respective repositories and compile them for you. Run the installer script:&lt;br /&gt;
&lt;br /&gt;
 /opt/toolchains/dc/rust/misc/install-rust.sh&lt;br /&gt;
&lt;br /&gt;
If all goes well, you&#039;ll see &#039;&#039;&#039;Rust for KallistiOS/Dreamcast installed!&#039;&#039;&#039; message!&lt;br /&gt;
&lt;br /&gt;
==Building KOS ports libraries==&lt;br /&gt;
If desired, you may also built the KOS ports collection of libraries for use with your Rust environment. It is recommended to maintain a separate KOS ports installation for linking with Rust projects in &amp;lt;code&amp;gt;/opt/toolchains/dc/rust/kos-ports&amp;lt;/code&amp;gt;. This is because the Rust environment is using the &amp;lt;code&amp;gt;-m4-single&amp;lt;/code&amp;gt; ABI, but the KallistiOS default installation will be using the &amp;lt;code&amp;gt;-m4-single-only&amp;lt;/code&amp;gt;, which cannot be mixed and matched.&lt;br /&gt;
&lt;br /&gt;
Clone the kos-ports repository to your system:&lt;br /&gt;
 git clone https://github.com/KallistiOS/kos-ports /opt/toolchains/dc/rust/kos-ports&lt;br /&gt;
An individual port can be built and installed by entering its directory and running the proper command. For example, installing GLdc:&lt;br /&gt;
 cd /opt/toolchains/dc/rust/kos-ports/libGL&lt;br /&gt;
 make install&lt;br /&gt;
Or, instead, you may run the script to build all of the included ports:&lt;br /&gt;
 /opt/toolchains/dc/rust/kos-ports/utils/build-all.sh&lt;br /&gt;
&lt;br /&gt;
=Using Rust for Dreamcast=&lt;br /&gt;
If all went well, you will now have a working Rust for Dreamcast environment!&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;cargo&#039;&#039;&#039; can be invoked to target Dreamcast using the &amp;lt;code&amp;gt;kos-cargo&amp;lt;/code&amp;gt; command.&lt;br /&gt;
** &amp;lt;code&amp;gt;kos-cargo run&amp;lt;/code&amp;gt; can be used to build and run code on a Dreamcast console using [[dcload-ip]] or [[dcload-serial]]. You will need to set up the &amp;lt;code&amp;gt;KOS_LOADER&amp;lt;/code&amp;gt; variable with the necessary command in the &amp;lt;code&amp;gt;/opt/toolchains/dc/rust/misc/environ.sh&amp;lt;/code&amp;gt; file (and then re-source the file for the change to take effect).&lt;br /&gt;
* &#039;&#039;&#039;rustc&#039;&#039;&#039; can be invoked to target Dreamcast using the &amp;lt;code&amp;gt;kos-rustc&amp;lt;/code&amp;gt; command.&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
Examples are included with the Rust for Dreamcast repo to help you get started.&lt;br /&gt;
* &#039;&#039;&#039;no_std&#039;&#039;&#039; examples:&lt;br /&gt;
** [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/no_std/hello-cargo &amp;lt;code&amp;gt;no_std/hello-cargo&amp;lt;/code&amp;gt;] demonstrates how to create a simple &amp;quot;Hello, world!&amp;quot; application using cargo.&lt;br /&gt;
** [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/no_std/hello-make &amp;lt;code&amp;gt;no_std/hello-make&amp;lt;/code&amp;gt;] demonstrates how to create a simple &amp;quot;Hello, world!&amp;quot; application using standard Makefiles.&lt;br /&gt;
** [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/no_std/gldc-cube &amp;lt;code&amp;gt;no_std/gldc-cube&amp;lt;/code&amp;gt;] demonstrates a Rust project using the GLdc KOS port.&lt;br /&gt;
** [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/no_std/addlib &amp;lt;code&amp;gt;no_std/addlib&amp;lt;/code&amp;gt;] demonstrates how to create a Rust library that can be included with a KallistiOS project.&lt;br /&gt;
* &#039;&#039;&#039;std&#039;&#039;&#039; examples:&lt;br /&gt;
** [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/std/atomics &amp;lt;code&amp;gt;std/atomics&amp;lt;/code&amp;gt;] demonstrates the use of atomic functions.&lt;br /&gt;
** [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/std/filesystem-io &amp;lt;code&amp;gt;std/filesystem-io&amp;lt;/code&amp;gt;] demonstrates listing directory contents and opening files.&lt;br /&gt;
** [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/std/network-time &amp;lt;code&amp;gt;std/network-time&amp;lt;/code&amp;gt;] demonstrates displaying the system time and retrieving the current UTC time from an NTP server using the [[Broadband adapter]] or [[LAN adapter]].&lt;br /&gt;
** [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/std/threads &amp;lt;code&amp;gt;std/threads&amp;lt;/code&amp;gt;] demonstrates the use of threads.&lt;br /&gt;
** [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/std/tokio-async &amp;lt;code&amp;gt;std/tokio-async&amp;lt;/code&amp;gt;] demonstrates the use of the &#039;&#039;&#039;tokio&#039;&#039;&#039; async runtime.&lt;br /&gt;
&lt;br /&gt;
==Creating a new Rust project with Cargo==&lt;br /&gt;
First, we&#039;ll demonstrate creating a new no_std &amp;quot;Hello, world!&amp;quot; project with &amp;lt;code&amp;gt;kos-cargo&amp;lt;/code&amp;gt;. This will follow the [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/no_std/hello-cargo &amp;lt;code&amp;gt;no_std/cargo-hello&amp;lt;/code&amp;gt;] example included in the Rust-for-Dreamcast repo. &lt;br /&gt;
&lt;br /&gt;
In a directory of your choosing, let&#039;s invoke &amp;lt;code&amp;gt;kos-cargo&amp;lt;/code&amp;gt; to create a new project and then enter the directory:&lt;br /&gt;
 kos-cargo new hello&lt;br /&gt;
 cd hello&lt;br /&gt;
&lt;br /&gt;
Let&#039;s add our kos-rs crate to gain access to current KallistiOS bindings. Open &amp;lt;code&amp;gt;Cargo.toml&amp;lt;/code&amp;gt; in your text editor and add:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;toml&amp;quot;&amp;gt;&lt;br /&gt;
[dependencies]&lt;br /&gt;
kos = { package = &amp;quot;kos-rs&amp;quot;, git = &amp;quot;https://github.com/dreamcast-rs/kos-rs&amp;quot; }&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we can open up &amp;lt;code&amp;gt;src/main.rs&amp;lt;/code&amp;gt; and write our &amp;quot;Hello, world!&amp;quot; example code:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; line&amp;gt;&lt;br /&gt;
#![no_std]&lt;br /&gt;
#![no_main]&lt;br /&gt;
extern crate alloc;&lt;br /&gt;
use kos::println;&lt;br /&gt;
&lt;br /&gt;
#[no_mangle]&lt;br /&gt;
extern &amp;quot;C&amp;quot; fn main(_argc: isize, _argv: *const *const u8) -&amp;gt; isize {&lt;br /&gt;
    println!(&amp;quot;Hello, world from Rust!&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
    0&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;#![no_std]&amp;lt;/syntaxhighlight&amp;gt; and &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;#![no_main]&amp;lt;/syntaxhighlight&amp;gt; tell Rust that our project does not use the standard library and we will not have Rust use a &amp;lt;code&amp;gt;main&amp;lt;/code&amp;gt; function as an entry point -- this will be handled by KallistiOS.&lt;br /&gt;
* &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;extern crate alloc;&amp;lt;/syntaxhighlight&amp;gt; tells Rust to use the alloc crate to gain access to heap-allocated types (in our case, &amp;lt;code&amp;gt;String&amp;lt;/code&amp;gt;).&lt;br /&gt;
* &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;use kos::println!;&amp;lt;/syntaxhighlight&amp;gt; tells Rust to use the &amp;lt;code&amp;gt;println!&amp;lt;/code&amp;gt; macro defined in the kos-rs crate. With this, we can print output to our &amp;lt;code&amp;gt;dc-tool&amp;lt;/code&amp;gt; console.&lt;br /&gt;
* &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;#[no_mangle]&amp;lt;/syntaxhighlight&amp;gt; tells Rust to disable name mangling so that the &amp;lt;code&amp;gt;main&amp;lt;/code&amp;gt; function can be used by KallistiOS.&lt;br /&gt;
* Finally, we have a &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;fn main&amp;lt;/syntaxhighlight&amp;gt; with the function signature of a typical C &amp;lt;code&amp;gt;main&amp;lt;/code&amp;gt; function, containing a basic &amp;quot;Hello, world!&amp;quot; exclamation.&lt;br /&gt;
&lt;br /&gt;
Now we can use &amp;lt;code&amp;gt;kos-cargo build&amp;lt;/code&amp;gt; to build our project. If all goes well, there will be a &amp;lt;code&amp;gt;target/sh-elf/debug/hello.elf&amp;lt;/code&amp;gt; file that can be sent to the Dreamcast with &amp;lt;code&amp;gt;dc-tool&amp;lt;/code&amp;gt;.&lt;br /&gt;
If you have &amp;lt;code&amp;gt;KOS_LOADER&amp;lt;/code&amp;gt; set in the &amp;lt;code&amp;gt;environ.sh&amp;lt;/code&amp;gt; file, you can invoke it directly with &amp;lt;code&amp;gt;kos-cargo run&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Creating a Rust project using kos-ports libraries==&lt;br /&gt;
[[File:Rust-Cube rustc codegen-gcc demo.gif|thumb|cube example in action]]&lt;br /&gt;
The [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/no_std/gldc-cube &amp;lt;code&amp;gt;gldc-cube&amp;lt;/code&amp;gt;] example demonstrates creating a rotating 3D cube using Rust as the primary language, while calling C functions provided by the &#039;&#039;&#039;GLdc&#039;&#039;&#039; library available in kos-ports. This project&#039;s initial setup is done the same as the above &amp;lt;code&amp;gt;hello&amp;lt;/code&amp;gt; example.&lt;br /&gt;
&lt;br /&gt;
For this example, we&#039;ll need to convert JPEG textures to VQ-encoded textures at compile time using the &amp;lt;code&amp;gt;vqenc&amp;lt;/code&amp;gt; utility includes with KallistiOS. To do this, we&#039;ll add a &amp;lt;code&amp;gt;build.rs&amp;lt;/code&amp;gt; file to the root of the crate with the following code (abridged to show the conversion of only one texture):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot;&amp;gt;&lt;br /&gt;
fn main() {&lt;br /&gt;
    let kos_base = std::env::var(&amp;quot;KOS_BASE&amp;quot;).expect(&amp;quot;Missing $KOS_BASE -- KallistiOS environment not sourced!&amp;quot;);&lt;br /&gt;
    let vqenc_cmd = kos_base + &amp;quot;/utils/vqenc/vqenc&amp;quot;;&lt;br /&gt;
    Command::new(&amp;amp;vqenc_cmd)&lt;br /&gt;
        .arg(&amp;quot;-t&amp;quot;)&lt;br /&gt;
        .arg(&amp;quot;-v&amp;quot;)&lt;br /&gt;
        .arg(&amp;quot;rsrc/tex_claw.jpg&amp;quot;)&lt;br /&gt;
        .output()&lt;br /&gt;
        .expect(&amp;quot;vqenc on tex_claw.jpg failed!&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
The texture files are then included in our project using the &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;include_bytes!&amp;lt;/syntaxhighlight&amp;gt; macro.&lt;br /&gt;
&lt;br /&gt;
We will need to link the GLdc library in this example, but we don&#039;t need to do anything special to add the paths to this library, because adding the common paths to KallistiOS libraries is already done for us in the kos-rs crate. However, if in your project you need to include a separate unique library path, you can add it in your &amp;lt;code&amp;gt;build.rs&amp;lt;/code&amp;gt; like so:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot;&amp;gt;&lt;br /&gt;
println!(&amp;quot;cargo:rustc-link-search=native={}&amp;quot;, lib_path);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The workings of this example&#039;s source code are too great to detail here line-by-line, but the example demonstrates declaring and binding external C functions, constants, and structures and then using them in Rust code. The declarations are preceded by &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;#[link(name = &amp;quot;GL&amp;quot;)]&amp;lt;/syntaxhighlight&amp;gt; to tell Rust to link in &#039;&#039;&#039;libGL&#039;&#039;&#039; (GLdc). Since the entirety of the example uses C FFI functions without safe wrappers, the &amp;lt;code&amp;gt;main()&amp;lt;/code&amp;gt; source is wrapped in &amp;lt;code&amp;gt;unsafe {}&amp;lt;/code&amp;gt;. In the future, this would be much less necessary as higher level safe Rust bindings to KallistiOS and other libraries become mature.&lt;br /&gt;
&lt;br /&gt;
==Creating a Rust library==&lt;br /&gt;
Next, we&#039;ll demonstrate creating a Rust library with &amp;lt;code&amp;gt;kos-cargo&amp;lt;/code&amp;gt; that can be included in other Dreamcast code. This will follow the [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/no_std/addlib &amp;lt;code&amp;gt;addlib&amp;lt;/code&amp;gt;] example. Once again, this project&#039;s initial setup is done the same as the above &amp;lt;code&amp;gt;hello&amp;lt;/code&amp;gt; example, but you&#039;ll create the new project using &amp;lt;code&amp;gt;kos-cargo new --lib addlib&amp;lt;/code&amp;gt; to specify that we&#039;re creating a library named &amp;lt;code&amp;gt;addlib&amp;lt;/code&amp;gt;. You&#039;ll also need to add the following text to this project&#039;s &amp;lt;code&amp;gt;Cargo.toml&amp;lt;/code&amp;gt; file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;toml&amp;quot;&amp;gt;&lt;br /&gt;
[lib]&lt;br /&gt;
crate-type = [&amp;quot;staticlib&amp;quot;]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This tells Rust to build a static &amp;lt;code&amp;gt;.a&amp;lt;/code&amp;gt; library archive file from our code, which is located in &amp;lt;code&amp;gt;src/lib.rs&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; line&amp;gt;&lt;br /&gt;
#![no_std]&lt;br /&gt;
extern crate alloc;&lt;br /&gt;
use kos::print;&lt;br /&gt;
&lt;br /&gt;
#[no_mangle]&lt;br /&gt;
pub extern &amp;quot;C&amp;quot; fn print_added(a: isize, b: isize) {&lt;br /&gt;
    print!(&amp;quot;{}&amp;quot;, a + b);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
#[no_mangle]&lt;br /&gt;
pub extern &amp;quot;C&amp;quot; fn add_integers(a: isize, b: isize) -&amp;gt; isize {&lt;br /&gt;
    a + b&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The source code here starts similarly to the &amp;quot;Hello, world!&amp;quot; example, except we don&#039;t need to specify &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;#![no_main]&amp;lt;/syntaxhighlight&amp;gt; as this is a library which wouldn&#039;t have a &amp;lt;code&amp;gt;main()&amp;lt;/code&amp;gt; function anyway.&lt;br /&gt;
&lt;br /&gt;
Two simple functions are provided: one for adding two integers and returning the result, and another for adding two integers and printing the result as text. Because these functions use &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;#[no_mangle]&amp;lt;/syntaxhighlight&amp;gt; and are declared &amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot; inline&amp;gt;extern &amp;quot;C&amp;quot;&amp;lt;/syntaxhighlight&amp;gt;, they can be called by name in C code that links this library.&lt;br /&gt;
&lt;br /&gt;
When built using &amp;lt;code&amp;gt;kos-cargo build&amp;lt;/code&amp;gt;, a &amp;lt;code&amp;gt;target/sh-elf/debug/libaddlib.a&amp;lt;/code&amp;gt; file will be generated. This can be linked into other projects to gain the use of these functions.&lt;br /&gt;
&lt;br /&gt;
For example, this can be added to a standard &amp;lt;code&amp;gt;Makefile&amp;lt;/code&amp;gt;-based KallistiOS project by editing the &amp;lt;code&amp;gt;Makefile&amp;lt;/code&amp;gt;:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;make&amp;quot;&amp;gt;&lt;br /&gt;
$(TARGET): $(OBJS)&lt;br /&gt;
	kos-cc -o $(TARGET) $(OBJS) -L/opt/toolchains/dc/rust/examples/cargo-addlib/target/sh-elf/debug -laddlib&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then, we can use the code in our C source:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/* Declare the external function from the Rust library */&lt;br /&gt;
int add_integers(int a, int b);&lt;br /&gt;
&lt;br /&gt;
/* Use the function */&lt;br /&gt;
printf(&amp;quot;Five plus six is %d\n&amp;quot;, add_integers(5, 6));&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Compiling individual modules into object files with rustc==&lt;br /&gt;
If we&#039;d like to mix C and Rust code in the same &amp;lt;code&amp;gt;Makefile&amp;lt;/code&amp;gt;-based KallistiOS project without building an entirely separate library, we can do that as well. This is demonstrated in the [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/no_std/hello-make &amp;lt;code&amp;gt;hello-make&amp;lt;/code&amp;gt;] example.&lt;br /&gt;
&lt;br /&gt;
Instead of using &amp;lt;code&amp;gt;kos-cargo&amp;lt;/code&amp;gt;, we can invoke the &amp;lt;code&amp;gt;kos-rustc&amp;lt;/code&amp;gt; via a &amp;lt;code&amp;gt;Makefile&amp;lt;/code&amp;gt; to build Rust modules. If we assume the Rust file is named &amp;lt;code&amp;gt;example.rs&amp;lt;/code&amp;gt;, you&#039;ll need to add &amp;lt;code&amp;gt;example.o&amp;lt;/code&amp;gt; as an object file in your &amp;lt;code&amp;gt;Makefile&amp;lt;/code&amp;gt;&#039;s &amp;lt;code&amp;gt;OBJS =&amp;lt;/code&amp;gt; declaration. For example, if the project has two source files &amp;lt;code&amp;gt;hello_c.c&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;hello_rust.rs&amp;lt;/code&amp;gt;, our &amp;lt;code&amp;gt;Makefile&amp;lt;/code&amp;gt; would have a line like this:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;make&amp;quot;&amp;gt;&lt;br /&gt;
OBJS = hello_c.o hello_rust.o&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The example code demonstrates starting a C &amp;lt;code&amp;gt;main()&amp;lt;/code&amp;gt; function to call a Rust function which builds a &amp;lt;code&amp;gt;String&amp;lt;/code&amp;gt; containing the &amp;quot;Hello, world!&amp;quot; text which is passed back to a C function which prints &amp;lt;code&amp;gt;String&amp;lt;/code&amp;gt;s.&lt;br /&gt;
&lt;br /&gt;
==Using the std library with KallistiOS==&lt;br /&gt;
When using the Rust &#039;&#039;&#039;std&#039;&#039;&#039; library in your project, be sure to enable the &amp;lt;code&amp;gt;std&amp;lt;/code&amp;gt; feature in &#039;&#039;&#039;kos-rs&#039;&#039;&#039; in your &amp;lt;code&amp;gt;Cargo.toml&amp;lt;/code&amp;gt;:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;toml&amp;quot;&amp;gt;&lt;br /&gt;
[dependencies]&lt;br /&gt;
kos = { package = &amp;quot;kos-rs&amp;quot;, features = [&amp;quot;std&amp;quot;], git = &amp;quot;https://github.com/dreamcast-rs/kos-rs&amp;quot; }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
If the &amp;lt;code&amp;gt;std&amp;lt;/code&amp;gt; feature is not enabled for the &#039;&#039;&#039;kos-rs&#039;&#039;&#039; crate, there will be errors with conflicting definitions.&lt;br /&gt;
&lt;br /&gt;
==Using the libc crate with KallistiOS==&lt;br /&gt;
If any of the crates in your project&#039;s dependency tree requires the &#039;&#039;&#039;libc&#039;&#039;&#039; crate, you will need to override fetching the default crate with our custom libc crate with KallistiOS/Dreamcast definitions. This custom version was copied to &amp;lt;code&amp;gt;/opt/toolchains/dc/rust/libc&amp;lt;/code&amp;gt; when building the Rust sysroot earlier. Add the following text to your &amp;lt;code&amp;gt;Cargo.toml&amp;lt;/code&amp;gt;:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;toml&amp;quot;&amp;gt;&lt;br /&gt;
[patch.crates-io]&lt;br /&gt;
libc = { path = &amp;quot;/opt/toolchains/dc/rust/libc&amp;quot; }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Adjusting build flags==&lt;br /&gt;
Build settings can be adjusted through the &amp;lt;code&amp;gt;KOS_RCG_RUSTFLAGS&amp;lt;/code&amp;gt; variable in the &amp;lt;code&amp;gt;environ.sh&amp;lt;/code&amp;gt; file under the &amp;lt;code&amp;gt;### Rust Flags&amp;lt;/code&amp;gt; section. &lt;br /&gt;
* To pass arguments to the &#039;&#039;&#039;rustc&#039;&#039;&#039; frontend, e.g. to disable debug assertions:&lt;br /&gt;
** &amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot; inline&amp;gt;export KOS_RCG_RUSTFLAGS=&amp;quot;${KOS_RCG_RUSTFLAGS} -Cdebug-assertions=no&amp;quot;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* To pass arguments to the GCC backend, e.g. to enable the &amp;lt;code&amp;gt;-freorder-blocks-algorithm=simple&amp;lt;/code&amp;gt; optimization for Rust code:&lt;br /&gt;
** &amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot; inline&amp;gt;export KOS_RCG_RUSTFLAGS=&amp;quot;${KOS_RCG_RUSTFLAGS} -Cllvm-args=-freorder-blocks-algorithm=simple&amp;quot;&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Darc</name></author>
	</entry>
</feed>