Skip to content

Commit

Permalink
Baremetal Raspberry PI build support (nesbox#918)
Browse files Browse the repository at this point in the history
* Raspberry PI baremetal support

* boot utils for baremetal RPI

* Building instructions

* fix surf continuously accessing disk
  • Loading branch information
msx80 authored and nesbox committed Sep 2, 2019
1 parent e290a61 commit cfa72df
Show file tree
Hide file tree
Showing 25 changed files with 3,054 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ lib/
*.cmake
CMakeCache.txt
Makefile
!build/baremetalpi/Makefile
!build/baremetalpi/boot/Makefile
build/android/.gradle/
build/android/app/.externalNativeBuild/
build/android/app/build/
Expand Down
55 changes: 55 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ cmake_minimum_required(VERSION 3.3)
project(TIC-80 C CXX)
message("Building for target : ${CMAKE_SYSTEM_NAME}")



if(ANDROID OR EMSCRIPTEN)
set(BUILD_SOKOL_DEFAULT OFF)
set(BUILD_LIBRETRO_DEFAULT OFF)
Expand All @@ -19,6 +21,59 @@ option(BUILD_LIBRETRO "libretro Enabled" ${BUILD_LIBRETRO_DEFAULT})
option(BUILD_DEMO_CARTS "Demo Carts Enabled" ${BUILD_DEMO_CARTS_DEFAULT})
option(BUILD_PRO "Build PRO version" FALSE)

if (BAREMETALPI)

set(BUILD_SDL off)
set(BUILD_DEMO_CARTS OFF)

set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_PROCESSOR ARM)

if(MINGW OR CYGWIN OR WIN32)
set(UTIL_SEARCH_CMD where)
elseif(UNIX OR APPLE)
set(UTIL_SEARCH_CMD which)
endif()

set(TOOLCHAIN_PREFIX arm-none-eabi-)

execute_process(
COMMAND ${UTIL_SEARCH_CMD} ${TOOLCHAIN_PREFIX}gcc
OUTPUT_VARIABLE BINUTILS_PATH
OUTPUT_STRIP_TRAILING_WHITESPACE
)
message("Crosscompiler path is ${BINUTILS_PATH}")
get_filename_component(ARM_TOOLCHAIN_DIR ${BINUTILS_PATH} DIRECTORY)

# Without that flag CMake is not able to pass test compilation check
if (${CMAKE_VERSION} VERSION_EQUAL "3.6.0" OR ${CMAKE_VERSION} VERSION_GREATER "3.6")
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
else()
set(CMAKE_EXE_LINKER_FLAGS_INIT "-Wl,-E")
endif()

# removed squirrel language as it doesn't seem to compile under arm. Needs investigation.
# note: hardcoded flags are for RPI2. Should try to use the ones
# generated in CIRCLE during config
set(CMAKE_C_FLAGS " -DTIC_BUILD_WITH_FENNEL -DTIC_BUILD_WITH_MOON -DTIC_BUILD_WITH_JS -DTIC_BUILD_WITH_WREN -DTIC_BUILD_WITH_LUA -DLUA_32BITS -std=c99 -march=armv7-a+neon-vfpv4 -D AARCH=32 -D __circle__ -D BAREMETALPI --specs=nosys.specs -O3 -mabi=aapcs -marm -mfloat-abi=hard -mfpu=neon-vfpv4 -D__DYNAMIC_REENT__")

set(CMAKE_C_COMPILER ${TOOLCHAIN_PREFIX}gcc)
set(CMAKE_ASM_COMPILER ${CMAKE_C_COMPILER})
set(CMAKE_CXX_COMPILER ${TOOLCHAIN_PREFIX}g++)

set(CMAKE_OBJCOPY ${ARM_TOOLCHAIN_DIR}/${TOOLCHAIN_PREFIX}objcopy CACHE INTERNAL "objcopy tool")
set(CMAKE_SIZE_UTIL ${ARM_TOOLCHAIN_DIR}/${TOOLCHAIN_PREFIX}size CACHE INTERNAL "size tool")

set(CMAKE_SYSROOT ${ARM_TOOLCHAIN_DIR}/../arm-none-eabi)
set(CMAKE_FIND_ROOT_PATH ${BINUTILS_PATH})
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

endif()



if(UNIX AND NOT APPLE AND NOT EMSCRIPTEN AND NOT ANDROID)
set(LINUX TRUE)
endif()
Expand Down
47 changes: 47 additions & 0 deletions build/baremetalpi/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#
# Makefile
#
TIC80HOME = ../..
TIC803RD = $(TIC80HOME)/3rd-party
CIRCLESTDLIB = $(TIC803RD)/circle-stdlib/
CIRCLEHOME = $(CIRCLESTDLIB)/libs/circle
NEWLIBDIR = $(CIRCLESTDLIB)/install/arm-none-circle
TIC80LIB = $(TIC80HOME)/build/lib
TIC80SRC = $(TIC80HOME)/src
BAREMETALSYSTEM = $(TIC80SRC)/system/baremetalpi

OBJS = \
$(BAREMETALSYSTEM)/main.o $(BAREMETALSYSTEM)/kernel.o $(BAREMETALSYSTEM)/customchargenerator.o \
$(BAREMETALSYSTEM)/customscreen.o $(BAREMETALSYSTEM)/utils.o $(BAREMETALSYSTEM)/gamepads.o

include $(CIRCLEHOME)/Rules.mk

CFLAGS += -I "$(NEWLIBDIR)/include" -I $(STDDEF_INCPATH) -I $(CIRCLESTDLIB)/include -I $(TIC80HOME)/include -I $(TIC80SRC) \
-D BAREMETALPI


LIBS := \
$(TIC80LIB)/libtic80lib.a \
$(TIC80LIB)/libtic80core.a \
$(TIC80LIB)/libgiflib.a \
$(TIC80LIB)/liblpeg.a \
$(TIC80LIB)/liblua.a \
$(TIC80LIB)/libwren.a \
$(TIC80LIB)/libzlib.a \
$(NEWLIBDIR)/lib/libc.a \
$(NEWLIBDIR)/lib/libcirclenewlib.a \
$(CIRCLEHOME)/addon/vc4/sound/libvchiqsound.a \
$(CIRCLEHOME)/addon/vc4/vchiq/libvchiq.a \
$(CIRCLEHOME)/addon/linux/liblinuxemu.a \
$(CIRCLEHOME)/addon/SDCard/libsdcard.a \
$(CIRCLEHOME)/addon/fatfs/libfatfs.a \
$(CIRCLEHOME)/lib/usb/libusb.a \
$(CIRCLEHOME)/lib/input/libinput.a \
$(CIRCLEHOME)/lib/fs/fat/libfatfs.a \
$(CIRCLEHOME)/lib/fs/libfs.a \
$(CIRCLEHOME)/lib/net/libnet.a \
$(CIRCLEHOME)/lib/sched/libsched.a \
$(CIRCLEHOME)/lib/libcircle.a \
$(NEWLIBDIR)/lib/libm.a

# $(TIC80LIB)/libsquirrel.a \
93 changes: 93 additions & 0 deletions build/baremetalpi/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@

# Raspberry PI Baremetal build

The following explains how to build TIC-80 for the Raspberry PI boards in baremetal mode, that is, without an operating system (the board boots directly in TIC-80)

# Requirements

You need:

- A Linux machine
- gcc ARM toolchain. You can get it [here](https://developer.arm.com/tools-and-software/open-source-software/developer-tools/gnu-toolchain/gnu-rm/downloads). Successfully tested with version: gcc-arm-none-eabi-8-2018-q4-major
- A standard building environment (make, cmake, gcc, wget, etc)

# Building instructions

Following are all the building steps for building the system.

First, set the path to include the arm toolkit (fix the command with your path):

```
PATH=/home/you/gcc-arm-none-eabi-8-2018-q4-major/bin/:$PATH
```

Get a fresh copy of TIC-80 repository and build demo carts:

```
git clone --recursive https://github.com/nesbox/TIC-80
cd TIC-80
sh build_demos.sh
```

Clone and build circle/circle-stdlib:

```
cd 3rd-party
git clone --recursive https://github.com/smuehlst/circle-stdlib.git
cd circle-stdlib
./configure -r 2
make
```

Make some addon that are not compiled automatically:

```
cd libs/circle/addon/vc4/sound/
make
cd ../vchiq
make
cd ../../linux
make
cd ../fatfs
make
cd ../../../../../..
```

Build `tic80lib` for arm with baremetal customizations:

```
cd build
cmake -DCMAKE_BUILD_TYPE=MinSizeRel -DBAREMETALPI=1 ..
make tic80lib
```

Build the kernel:

```
cd baremetalpi
make
```

This generates the final `kernel7.img` file. Copy it into your SD card root.

Now you have to prepare some bootup files that need to be copied to the SD card root together with your kernel7.img. This only need to be done once:

```
cd boot
make
```

Now copy the following files to your SD card

- `config.txt`
- `LICENCE.broadcom`
- `bootcode.bin`
- `fixup.dat`
- `start.elf`

You need to create a `tic80` folder into your SD card. Your carts go in there.

# Thanks

This project is built on two awesome projects, [circle](https://github.com/rsta2/circle) and [circle-stdlib](https://github.com/smuehlst/circle-stdlib). Without them, this version of TIC-80 would not exists.

14 changes: 14 additions & 0 deletions build/baremetalpi/boot/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#
# Download the firmware files to be required for boot (requires wget)
#
# These files must be copied along with the generated kernel.img
# onto a SD(HC) card with FAT file system.
#

firmware:
wget -q -O LICENCE.broadcom https://github.com/raspberrypi/firmware/blob/master/boot/LICENCE.broadcom?raw=true
wget -q -O bootcode.bin https://github.com/raspberrypi/firmware/blob/master/boot/bootcode.bin?raw=true
wget -q -O fixup.dat https://github.com/raspberrypi/firmware/blob/master/boot/fixup.dat?raw=true
wget -q -O start.elf https://github.com/raspberrypi/firmware/blob/master/boot/start.elf?raw=true

all: firmware
3 changes: 3 additions & 0 deletions build/baremetalpi/boot/config.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
hdmi_group=1
hdmi_mode=16
scaling_kernel=8
8 changes: 8 additions & 0 deletions include/tic80_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,18 @@

typedef uint8_t u8;
typedef uint16_t u16;
#if defined(BAREMETALPI)
typedef unsigned int u32;
#else
typedef uint32_t u32;
#endif
typedef uint64_t u64;

typedef int8_t s8;
typedef int16_t s16;
#if defined(BAREMETALPI)
typedef signed int s32; // TODO understand why it breaks
#else
typedef int32_t s32;
#endif
typedef int64_t s64;
Loading

0 comments on commit cfa72df

Please sign in to comment.