Skip to content

A from scratch compiler, assembler, and linker for the SNES SuperFX (GSU), written in modern C++.

License

Notifications You must be signed in to change notification settings

DiscoLabOfficial/DiscoC

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

DiscoC: A C++23 Toolchain for SuperFX (GSU)

CMake

DiscoC is a custom systems programming language and complete toolchain (Compiler, Assembler, and Linker) targeting the Super Nintendo's SuperFX (GSU) RISC co-processor.

Written in Modern C++ (C++23), this project aims to bring modern development practices to legacy hardware constraints. It features a custom Relocatable Object File format, a dedicated Linker for memory mapping resolution, and hardware-specific optimizations.

Status: Pre-Alpha / Experimental This project is currently a work-in-progress. While fully functional (compiles and links binaries), the generated assembly focuses on correctness over speed. Advanced register allocation is on the roadmap. Also, it does not produce buidable ROMs, you must binclude the binary.

Features

Language Features

  • C-like syntax (if, for, while, variables, functions)
  • word (16-bit) and byte (8-bit) integer types
  • Full 24-bit pointer support with or without far keywords
  • struct definitions with member access
  • rom const for placing data directly into the ROM
  • Direct hardware access via special keywords (plot, set_color, etc.)

Compiler & Toolchain

  • Two-Stage Build Process: A separate compiler (discc) and linker (discld) for multi-file projects.
  • Assembly Export: Inspect the compiler's output with the --emit-asm flag.
  • Hardware-Specific Optimizations:
    • Recognizes for loops and converts them to the ultra-fast LOOP instruction.
    • Optimizes small integer arithmetic into single-byte immediate instructions.
  • Custom Object File Format for relocatable code.

Architecture & Engineering Highlights

This is not just a parser; it is a full-stack compiler implementation including:

  • Custom Object File Format: Designed a binary format supporting code/data sections, symbol tables, and relocation entries (patching JAL targets and global variable addresses at link-time).
  • The discld Linker: A custom linker built from scratch to handle symbol resolution, memory placement, and binary patching across multiple compilation units.
  • Peephole Optimization: The compiler analyzes the AST to detect high-level patterns (like decrementing while loops) and emits specialized hardware instructions (LOOP opcode) for zero-overhead looping.
  • Manual ABI Management: Implements a full stack frame convention (Frame Pointer R9, Stack Pointer R10) to support recursion and local scope management.
  • Software-Defined Arithmetic: Implementation of BF16 (Brain Float 16) and FP16 logic purely in software to enable modern numerics on a 16-bit integer chip.

"Hello, World!" (The "Add" Example)

Here is a simple example of a DiscoC program with two functions.

// main.dc

// Forward declare the function from another file.
word add(word a, word b);

void main() {
    word result = add(30, 12);
    // After this, R0 would hold the value 42.
}
// math.dc

word add(word a, word b) {
    return a + b;
}

Building from Source

This project uses C++23 and CMake.

# 1. Clone the repository
git clone https://github.com/DiscoManOfficial/DiscoC.git
cd DiscoC

# 2. Configure the build using CMake
mkdir build
cd build
cmake ..

# 3. Build the executables (discc and discld)
cmake --build .

After a successful build, the discc and discld executables will be located in the build/Debug or build directory.

Usage

The DiscoC toolchain uses a two-step compile-and-link process.

1. Compile: Use discc to compile each .dc source file into an intermediate .o object file.

# Compile main.dc into main.o
./discc ../main.dc -o main.o

# Compile math.dc into math.o
./discc ../math.dc -o math.o

2. Link: Use discld to link all your .o files into a final .bin ROM.

# Link main.o and math.o into the final ROM
./discld main.o math.o -o my_game.bin

You now have a complete, runnable my_game.bin file!

Project Roadmap

This project is not as actively developed. You can view our grand roadmap and upcoming features in the ROADMAP.md file.

License

DiscoC is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be a useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see https://www.gnu.org/licenses/.

About

A from scratch compiler, assembler, and linker for the SNES SuperFX (GSU), written in modern C++.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published