Work in progress

This commit is contained in:
Kevin Hamacher 2025-01-15 13:17:41 +01:00
parent 59f3c95a18
commit 0b49e33473
3 changed files with 204 additions and 108 deletions

View File

@ -1,26 +1,41 @@
From c0bc79d64a1e11d8008f24f063e65b144f3a6067 Mon Sep 17 00:00:00 2001
From 456a9dbf5e31297a638475afb1b3b8fef5f12969 Mon Sep 17 00:00:00 2001
From: User <user@example.com>
Date: Tue, 14 Jan 2025 08:29:08 +0000
Subject: [PATCH] Patches to run with gcc
Date: Wed, 15 Jan 2025 11:46:50 +0000
Subject: [PATCH] Make project compile with GCC
---
Cargo.toml | 3 +-
examples/megapong/entry.S | 154 ++++++++++++++---------------
examples/megapong/stdlib.S | 21 ++++
examples/megapong/hack.c | 27 +++++
libs/megadrive-graphics/src/lib.rs | 2 +-
libs/megadrive-util/src/panic.rs | 5 +-
share/ldscripts/megadrive.x | 8 +-
6 files changed, 119 insertions(+), 85 deletions(-)
create mode 100644 examples/megapong/compile.sh
create mode 100644 examples/megapong/stdlib.S
share/ldscripts/megadrive.x | 11 ++-
6 files changed, 116 insertions(+), 86 deletions(-)
create mode 100644 examples/megapong/hack.c
diff --git a/Cargo.toml b/Cargo.toml
index 03b1a1c..b0624fb 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -11,8 +11,9 @@ members = [
]
[profile.release]
-lto = true
+lto = "fat"
panic = "abort"
[profile.dev]
+lto = "fat"
panic = "abort"
diff --git a/examples/megapong/entry.S b/examples/megapong/entry.S
index e5343ed..2543eb0 100644
index e5343ed..0dde62b 100644
--- a/examples/megapong/entry.S
+++ b/examples/megapong/entry.S
@@ -6,91 +6,91 @@
.set HBLANK, _int
.set VBLANK, _vblank
-.long 0x0 ; Initial Stack Address
-.long START ; Start of program Code
-.long INT ; Bus error
@ -149,7 +164,7 @@ index e5343ed..2543eb0 100644
+.long INT
+.long INT
+.long INT
-.ascii "SEGA MEGADRIVE " ; SEGA must be the first four chars for TMSS
+.ascii "SEGA MEGADRIVE "
.ascii "(C)2021.JAN "
@ -179,7 +194,7 @@ index e5343ed..2543eb0 100644
.ascii " "
-.ascii "F " ; enable any hardware configuration
+.ascii "F "
.global vblank
_vblank:
@@ -100,14 +100,14 @@ _vblank:
@ -193,94 +208,112 @@ index e5343ed..2543eb0 100644
jsr _init_runtime
- bra #main
+ bra main
.global abort
abort:
- bra #abort
+ bra abort
_int:
rte
diff --git a/examples/megapong/stdlib.S b/examples/megapong/stdlib.S
diff --git a/examples/megapong/hack.c b/examples/megapong/hack.c
new file mode 100644
index 0000000..b614ed5
index 0000000..f853c8b
--- /dev/null
+++ b/examples/megapong/stdlib.S
@@ -0,0 +1,25 @@
+.text
+++ b/examples/megapong/hack.c
@@ -0,0 +1,27 @@
+#if 1
+void* memcpy(void *dst, const void *src, long unsigned int n) {
+ char *dst_ = dst;
+ const char *src_ = src;
+ while (--n) *dst_++ = *src_++;
+}
+#endif
+
+.global memcpy
+memcpy:
+ rts
+#if 0
+void *memset(char *s, int c, long unsigned int n) {
+ while (--n) *s++ = c;
+}
+#endif
+
+.global memcmp
+memcmp:
+ rts
+
+.global memset
+memset:
+ rts
+#if 0
+int memcmp(char *a, char *b, long unsigned n) {
+ while (--n) {
+ if (*a < *b) return -1;
+ if (*a > *b) return 1;
+ ++a;
+ ++b;
+ }
+
+.global _Unwind_Resume
+_Unwind_Resume:
+ rts
+
+.global rust_eh_personality
+rust_eh_personality:
+ rts
+
+.global __mulsi3
+__mulsi3:
+ rts
+ return (*a == *b) ? 0 : 1;
+}
+#endif
diff --git a/libs/megadrive-graphics/src/lib.rs b/libs/megadrive-graphics/src/lib.rs
index d7aa284..5e7f929 100644
--- a/libs/megadrive-graphics/src/lib.rs
+++ b/libs/megadrive-graphics/src/lib.rs
@@ -5,7 +5,7 @@ use core::mem::MaybeUninit;
use megadrive_sys::vdp::{Sprite, VDP};
pub mod default_ascii {
- include!(concat!(env!("OUT_DIR"), "/default_ascii.rs"));
+ // include!(concat!(env!("OUT_DIR"), "/default_ascii.rs"));
}
mod font;
diff --git a/libs/megadrive-util/src/panic.rs b/libs/megadrive-util/src/panic.rs
index 8564bbd..edb1663 100644
--- a/libs/megadrive-util/src/panic.rs
+++ b/libs/megadrive-util/src/panic.rs
@@ -3,7 +3,6 @@ use core::ptr::read_volatile;
use megadrive_sys::vdp::VDP;
use megadrive_graphics::Renderer;
-use megadrive_graphics::default_ascii::DEFAULT_FONT_1X1;
static mut NEW_FRAME: u16 = 0;
@@ -19,7 +18,7 @@ fn panic(_info: &PanicInfo) -> ! {
vdp.enable_display(true);
// Initialize the default font tiles
- DEFAULT_FONT_1X1.load(&mut vdp);
+ // DEFAULT_FONT_1X1.load(&mut vdp);
let resolution = vdp.resolution();
let half_screen_width = (resolution.0 >> 1) as i16;
@@ -36,7 +35,7 @@ fn panic(_info: &PanicInfo) -> ! {
// let mut panic_text: &str = &_info.payload().downcast_ref::<&str>().unwrap();
let panic_text= "Panic!";
- DEFAULT_FONT_1X1.blit_text(&mut renderer, panic_text, x_off as u16, y_off as u16);
+ // DEFAULT_FONT_1X1.blit_text(&mut renderer, panic_text, x_off as u16, y_off as u16);
renderer.render(&mut vdp);
// vsync
wait_for_vblank();
diff --git a/share/ldscripts/megadrive.x b/share/ldscripts/megadrive.x
index 95e1cf3..c6d91ae 100644
index 95e1cf3..dd61c42 100644
--- a/share/ldscripts/megadrive.x
+++ b/share/ldscripts/megadrive.x
@@ -22,19 +22,19 @@ SECTIONS
@@ -4,6 +4,8 @@ MEMORY
RAM (rwx) : ORIGIN = 0xFF0000, LENGTH = 0x10000
}
+ENTRY(_start)
+
SECTIONS
{
_stack_top = 0x1000000;
@@ -16,25 +18,26 @@ SECTIONS
.rodata :
{
+ . = ALIGN(4);
*(.rodata .rodata.*);
_data_src = .;
} > ROM
.data :
{
- ALIGN(4);
@ -291,7 +324,7 @@ index 95e1cf3..c6d91ae 100644
+ . = ALIGN(4);
_data_end = .;
} > RAM AT > ROM
.bss (NOLOAD) :
{
- ALIGN(4);
@ -302,7 +335,7 @@ index 95e1cf3..c6d91ae 100644
+ . = ALIGN(4);
_bss_end = .;
} > RAM AT > ROM
--
--
2.34.1

View File

@ -1,57 +1,69 @@
From e2b431ff117b9e58c4a487f6021da5aa54cbbd77 Mon Sep 17 00:00:00 2001
From 0c4bd1d8bb2863f16f13faad58eb3cbbcf7044cc Mon Sep 17 00:00:00 2001
From: User <user@example.com>
Date: Tue, 14 Jan 2025 17:23:55 +0000
Subject: [PATCH] Hackery
Date: Wed, 15 Jan 2025 11:49:31 +0000
Subject: [PATCH] codegen: add support for megadrive
---
build_system/build_sysroot/Cargo.toml | 11 +++++++----
src/base.rs | 1 +
build_system/build_sysroot/Cargo.toml | 8 +++-----
src/base.rs | 17 +++++++++++++++++
src/gcc_util.rs | 3 +--
src/lib.rs | 1 +
target_specs/m68k-unknown-linux-gnu.json | 10 +++++-----
5 files changed, 15 insertions(+), 11 deletions(-)
target_specs/m68k-unknown-linux-gnu.json | 12 ++++++------
5 files changed, 28 insertions(+), 13 deletions(-)
diff --git a/build_system/build_sysroot/Cargo.toml b/build_system/build_sysroot/Cargo.toml
index 2415207..8076c81 100644
index 2415207..8f928c5 100644
--- a/build_system/build_sysroot/Cargo.toml
+++ b/build_system/build_sysroot/Cargo.toml
@@ -5,12 +5,14 @@ version = "0.0.0"
resolver = "2"
@@ -6,11 +6,8 @@ resolver = "2"
[dependencies]
+# rustc-dep-of-std?
core = { path = "./sysroot_src/library/core" }
-compiler_builtins = "0.1"
+compiler_builtins = { version = "0.1" }
+#compiler_builtins = { version = "0.1", features = ['rustc-dep-of-std'] }
+compiler_builtins = { version = "0.1", features = ["c"] }
alloc = { path = "./sysroot_src/library/alloc" }
-std = { path = "./sysroot_src/library/std", features = ["panic_unwind", "backtrace"] }
-test = { path = "./sysroot_src/library/test" }
-proc_macro = { path = "./sysroot_src/library/proc_macro" }
+# std = { path = "./sysroot_src/library/std", features = ["panic_unwind", "backtrace"] }
+# test = { path = "./sysroot_src/library/test" }
+# proc_macro = { path = "./sysroot_src/library/proc_macro" }
[patch.crates-io]
rustc-std-workspace-core = { path = "./sysroot_src/library/rustc-std-workspace-core" }
@@ -35,4 +37,5 @@ codegen-units = 10000
@@ -35,4 +32,5 @@ codegen-units = 10000
[profile.release]
debug = "limited"
-#lto = "fat" # TODO(antoyo): re-enable when the failing LTO tests regarding proc-macros are fixed.
+panic = "abort"
#lto = "fat" # TODO(antoyo): re-enable when the failing LTO tests regarding proc-macros are fixed.
+lto = "fat"
diff --git a/src/base.rs b/src/base.rs
index c9701fb..97c28f1 100644
index c9701fb..989f8aa 100644
--- a/src/base.rs
+++ b/src/base.rs
@@ -156,6 +156,7 @@ pub fn compile_codegen_unit(
@@ -156,7 +156,24 @@ pub fn compile_codegen_unit(
let target_cpu = gcc_util::target_cpu(tcx.sess);
if target_cpu != "generic" {
context.add_command_line_option(format!("-march={}", target_cpu));
+ context.add_command_line_option("-mcpu=68000");
}
+ context.add_command_line_option("-flto");
+ context.add_driver_option("-flto");
+ //context.add_command_line_option("-fno-rtti");
+ context.add_driver_option("-fno-rtti");
+ context.add_command_line_option("-fno-exceptions");
+ context.add_driver_option("-fno-exceptions");
+
+ // Improve linker garbage collection
+ context.add_driver_option("-ffunction-sections");
+ context.add_command_line_option("-ffunction-sections");
+ context.add_driver_option("-fdata-sections");
+ context.add_command_line_option("-fdata-sections");
+
+ // ?
+ context.add_driver_option("-mxgot");
+ context.add_command_line_option("-mxgot");
if tcx
.sess
diff --git a/src/gcc_util.rs b/src/gcc_util.rs
index 560aff4..39b1889 100644
--- a/src/gcc_util.rs
@ -79,7 +91,7 @@ index f6ad0c7..e19dbcf 100644
**self.target_info.info.lock().expect("lock") = context.get_target_info();
diff --git a/target_specs/m68k-unknown-linux-gnu.json b/target_specs/m68k-unknown-linux-gnu.json
index 95ea061..026dc98 100644
index 95ea061..d1ff9a2 100644
--- a/target_specs/m68k-unknown-linux-gnu.json
+++ b/target_specs/m68k-unknown-linux-gnu.json
@@ -1,17 +1,17 @@
@ -94,8 +106,9 @@ index 95ea061..026dc98 100644
"env": "gnu",
"has-rpath": true,
- "has-thread-local": true,
- "llvm-target": "m68k-unknown-linux-gnu",
+ "has-thread-local": false,
"llvm-target": "m68k-unknown-linux-gnu",
+ "llvm-target": "m68k-elf",
"max-atomic-width": 32,
"os": "linux",
- "position-independent-executables": true,

View File

@ -1,64 +1,114 @@
FROM ubuntu:22.04
RUN apt-get -y update && apt-get install -y --no-install-recommends git curl ca-certificates build-essential sudo
RUN apt-get -y update && apt-get install -y --no-install-recommends \
git curl ca-certificates build-essential sudo python3 file \
flex libmpfr-dev libgmp-dev libmpc3 libmpc-dev texinfo
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | bash -s -- -y
ENV PATH="/root/.cargo/bin:${PATH}"
# Not sure why/if this is needed
ENV LIBRARY_PATH=/usr/lib
# Make sure we compile to the right target
ENV OVERWRITE_TARGET_TRIPLE=m68k-unknown-linux-gnu
ENV OVERWRITE_TARGET_TRIPLE=m68k-elf
RUN mkdir /build
WORKDIR /build
# Install binutils
RUN curl -L https://sourceware.org/pub/binutils/releases/binutils-2.43.tar.xz | tar xJ
RUN mkdir binutils-build && cd binutils-build && \
../binutils-2.43/configure --prefix=/usr/local --target=m68k-elf --disable-multilib && \
make -j $(nproc) && \
make install
# Install GCC
RUN git clone https://github.com/antoyo/gcc
RUN mkdir gcc-build && cd gcc-build && \
../gcc/configure \
--enable-host-shared \
--enable-languages=jit \
--enable-checking=release \
--disable-bootstrap \
--disable-multilib \
--prefix=/usr/local \
--with-cpu=m68000 \
--with-arch=m68k \
--disable-libmudflap --disable-libgomp --disable-libssp --disable-libquadmath \
--disable-libquadmath-support --disable-libsanitizer --disable-libmpx --disable-libstdcxx-verbose \
--without-zstd --disable-threads --disable-tls --disable-cet \
--without-headers \
--target=m68k-elf && \
make -j $(nproc) && \
make install
# --enable-cxx-flags='-fomit-frame-pointer -ffunction-sections -fno-exceptions' -fno-rtti?
# --with-gnu-as --with-gnu-ld?
# Clone source repo
RUN git clone https://github.com/rust-lang/rustc_codegen_gcc.git
# Set gcc path in the rustc_codegen project
RUN echo 'gcc-path = "/usr/local/lib"' > rustc_codegen_gcc/config.toml
# Setup rustup
RUN cd rustc_codegen_gcc && rustup show
# Install gcc
RUN curl -LO https://github.com/cross-cg-gcc-tools/cross-gcc/releases/latest/download/gcc-m68k-13.deb
RUN dpkg -i gcc-m68k-13.deb
# Set gcc path in the rustc_codegen project
RUN echo 'gcc-path = "/usr/lib"' > rustc_codegen_gcc/config.toml
# Git config is needed for ./y.sh prepare
RUN git config --global user.email "user@example.com"
RUN git config --global user.name "User"
# Apply hacky patch
COPY 0001-Hackery.patch /tmp/0001-Hackery.patch
RUN cd rustc_codegen_gcc && patch -p1 < /tmp/0001-Hackery.patch && rm /tmp/0001-Hackery.patch
COPY 0001-codegen-add-support-for-megadrive.patch /tmp/0001-codegen-add-support-for-megadrive.patch
RUN cd rustc_codegen_gcc && patch -p1 < /tmp/0001-codegen-add-support-for-megadrive.patch && rm /tmp/0001-codegen-add-support-for-megadrive.patch
# Provide compiler-rt source so we can get intrinsics to work. (XXX: currently doesn't)
RUN apt-get install -y unzip # <- move up.
RUN curl -LO https://github.com/llvm/llvm-project/archive/refs/heads/main.zip && unzip main.zip && rm main.zip
ENV RUST_COMPILER_RT_ROOT=/build/llvm-project-main/compiler-rt/
ENV CHANNEL=release
# Build project
# See https://github.com/rust-lang/rustc_codegen_gcc/blob/master/doc/tips.md#how-to-build-a-cross-compiling-libgccjit
RUN cd rustc_codegen_gcc && ./y.sh prepare --cross
RUN cd rustc_codegen_gcc && ./y.sh build --sysroot --features compiler_builtins/no-f16-f128 --target-triple m68k-unknown-linux-gnu --target /rustc_codegen_gcc/target_specs/m68k-unknown-linux-gnu.json
RUN CG_RUSTFLAGS="-Cpanic=abort -Copt-level=3" cd rustc_codegen_gcc && ./y.sh build --sysroot --features compiler_builtins/no-f16-f128 --target-triple m68k-unknown-linux-gnu --target /build/rustc_codegen_gcc/target_specs/m68k-unknown-linux-gnu.json --release
ENV CG_RUSTFLAGS="-Cpanic=abort -Crelocation-model=static -Copt-level=s"
# Build "megapong" PoC
RUN git clone https://github.com/ricky26/rust-mega-drive.git
COPY 0001-Patches-to-run-with-gcc.patch /tmp/rust-mega-drive.patch
RUN cd rust-mega-drive && patch -p1 </tmp/rust-mega-drive.patch && rm /tmp/rust-mega-drive.patch
# rust-mega-drive requires python3 as part of the build script.
RUN apt-get install -y python3
COPY 0001-Make-project-compile-with-GCC.patch /tmp/rust-mega-drive.patch
RUN cd rust-mega-drive && patch -p1 < /tmp/rust-mega-drive.patch && rm /tmp/rust-mega-drive.patch
# Compile rust code
RUN cd rustc_codegen_gcc && ./y.sh cargo build --target ./target_specs/m68k-unknown-linux-gnu.json --manifest-path /rust-mega-drive/examples/megapong/Cargo.toml --release
RUN cd rustc_codegen_gcc && ./y.sh cargo build --target /build/rustc_codegen_gcc/target_specs/m68k-unknown-linux-gnu.json --manifest-path /build/rust-mega-drive/examples/megapong/Cargo.toml --release
# Compile entrypoint + stdlib stub (should not be strictly necessary to reproduce)
RUN cd rust-mega-drive/examples/megapong && m68k-unknown-linux-gnu-gcc -c entry.S -o entry.o -fno-exceptions -mcpu=68000
RUN cd rust-mega-drive/examples/megapong && m68k-unknown-linux-gnu-gcc -c stdlib.S -o stdlib.o -fno-exceptions -mcpu=68000
# Link everything
RUN cd rust-mega-drive/examples/megapong && m68k-unknown-linux-gnu-ld -o out.elf -T /rust-mega-drive/share/ldscripts/megadrive.x entry.o stdlib.o ../../target/m68k-unknown-linux-gnu/release/libmegapong.a -static -z nocopyreloc --no-eh-frame-hdr -nostdlib
# Create cartridge
RUN cd rust-mega-drive/examples/megapong && m68k-unknown-linux-gnu-objcopy -O binary out.elf out.md
RUN cd rust-mega-drive/examples/megapong && m68k-elf-gcc -fno-exceptions -mcpu=68000 -T /build/rust-mega-drive/share/ldscripts/megadrive.x entry.S hack.c /build/rust-mega-drive/target/m68k-unknown-linux-gnu/release/libmegapong.a -o out.elf -O2 -nostartfiles -nostdlib -static -nolibc -s -flto -Wl,-gc-sections -ffunction-sections -fdata-sections
#RUN cd rust-mega-drive/examples/megapong && /usr/local/bin/m68k-elf-gcc -c entry.S -o entry.o -fno-exceptions -mcpu=68000
#RUN cd rust-mega-drive/examples/megapong && /usr/local/bin/m68k-elf-gcc -c hack.c -o entry.o -fno-exceptions -mcpu=68000
# Installing utility file
RUN apt install -y file
# XXX: Artifacts have been created, the following commands can be used to verify if things are okay or not.
# 1) This shows that the object files were created for 68020 "Motorola m68k, 68020".
# cd /tmp
# ar x /rust-mega-drive/target/m68k-unknown-linux-gnu/release/libmegapong.a
# file *.o
#
# 2) The offending instruction will be inserted at the beginning of _init_runtime, if the following command finds something, the code is invalid for 68000
# cd rust-mega-drive/examples/megapong && m68k-unknown-linux-gnu-objdump -d out.elf | grep -A3 "_init_runtime" | grep "4bfb 0170"
RUN false
# Link everything
#RUN cd rust-mega-drive/examples/megapong && /usr/local/bin/m68k-elf-ld -o out.elf -T /build/rust-mega-drive/share/ldscripts/megadrive.x entry.o stdlib.o /build/rust-mega-drive/target/m68k-unknown-linux-gnu/debug/libmegapong.a -static -z nocopyreloc --no-eh-frame-hdr -nostdlib
# RUN cd rust-mega-drive/examples/megapong && /usr/local/bin/m68k-elf-gcc -fno-exceptions -mcpu=68000 -T /build/rust-mega-drive/share/ldscripts/megadrive.x entry.o stdlib.o /build/rust-mega-drive/target/m68k-unknown-linux-gnu/release/libmegapong.a -static -s -o out.elf -nostdlib -nodefaultlibs
# Create cartridge
#RUN cd rust-mega-drive/examples/megapong && /usr/local/bin/m68k-elf-objcopy -O binary out.elf out.md
#apt install texinfo
#../binutils-2.43/configure --prefix=/gcc/binutils-install --target=m68k-elf --disable-multilib
#make -j $(nproc)
#make install
#set path...
#set gcc-path in config.toml /gcc/gcc-install/lib
# -> set target stuff to m68k-elf
# install unzip
# https://github.com/llvm/llvm-project/archive/refs/heads/main.zip
# -> llvm-project-main/
# enable feature [c] for compiler-builtins thingy
# set RUST_COMPILER_RT_ROOT (RUST_COMPILER_RT_ROOT=/build/llvm-project-main/compiler-rt)