Skip to content

Commit

Permalink
More updates to CMake bare metal templates
Browse files Browse the repository at this point in the history
  • Loading branch information
spicyjpeg committed Oct 14, 2024
1 parent ba5c080 commit 065c7a1
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,40 +40,8 @@ target_include_directories(
ps1-bare-metal/src/libc
)

# Define a helper function to embed the contents of a file into the executable.
function(addBinaryFile target name path)
set(_file "${PROJECT_BINARY_DIR}/includes/${target}_${name}.s")
cmake_path(ABSOLUTE_PATH path OUTPUT_VARIABLE _path)

# Generate an assembly listing that uses the .incbin directive to embed the
# file and add it to the executable's list of source files. This may look
# hacky, but it works and lets us easily customize the symbol name (i.e. the
# name of the "array" that will contain the file's data).
file(
CONFIGURE
OUTPUT "${_file}"
CONTENT [[
.section .data.${name}, "aw"
.balign 8

.global ${name}
.type ${name}, @object
.size ${name}, (${name}_end - ${name})

${name}:
.incbin "${_path}"
${name}_end:
]]
ESCAPE_QUOTES
NEWLINE_STYLE LF
)

target_sources(${target} PRIVATE "${_file}")
set_source_files_properties("${_file}" PROPERTIES OBJECT_DEPENDS "${_path}")
endfunction()

# Create the main executable. You may add more source files by listing them
# here, or other images or files by adding calls to addBinaryFile().
# Compile the main executable. You may add more source files by listing them
# here.
add_executable(
{{projectName}}
src/font.c
Expand All @@ -83,8 +51,28 @@ add_executable(
)
target_link_libraries({{projectName}} PRIVATE common)

addBinaryFile({{projectName}} fontTexture assets/fontTexture.dat)
addBinaryFile({{projectName}} fontPalette assets/fontPalette.dat)
# Define a CMake macro that invokes convertImage.py in order to generate VRAM
# texture data from an image file.
function(convertImage input bpp)
add_custom_command(
OUTPUT ${ARGN}
DEPENDS "${PROJECT_SOURCE_DIR}/${input}"
COMMAND
"${Python3_EXECUTABLE}"
"${PROJECT_SOURCE_DIR}/ps1-bare-metal/tools/convertImage.py"
-b ${bpp}
"${PROJECT_SOURCE_DIR}/${input}"
${ARGN}
VERBATIM
)
endfunction()

# Convert the font spritesheet to a 4bpp texture and palette, then embed them
# into the executable. The addBinaryFile() macro is defined in setup.cmake; you
# may call it multiple times to embed other data into the binary.
convertImage(assets/font.png 4 fontTexture.dat fontPalette.dat)
addBinaryFile({{projectName}} fontTexture "${PROJECT_BINARY_DIR}/fontTexture.dat")
addBinaryFile({{projectName}} fontPalette "${PROJECT_BINARY_DIR}/fontPalette.dat")

# Add a step to run convertExecutable.py after the executable is compiled in
# order to convert it into a PS1 executable. By default all custom commands run
Expand All @@ -96,6 +84,7 @@ add_custom_command(
COMMAND
"${Python3_EXECUTABLE}"
"${PROJECT_SOURCE_DIR}/ps1-bare-metal/tools/convertExecutable.py"
"$<TARGET_FILE:{{projectName}}>" {{projectName}}.psexe
"$<TARGET_FILE:{{projectName}}>"
{{projectName}}.psexe
VERBATIM
)
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
/*
* This template is based on the following examples:
* This program is a simple demo that displays a 3D cube and some text (using
* the font spritesheet in the assets directory), based on the following
* examples:
* https://github.com/spicyjpeg/ps1-bare-metal/blob/main/src/06_fonts/main.c
* https://github.com/spicyjpeg/ps1-bare-metal/blob/main/src/08_spinningCube/main.c
*
* If you wish to modify the font image in the assets directory or add more
* images, you may use the ps1-bare-metal/tools/convertImage.py script to
* generate the texture and palette files to embed into the executable. Note
* that the script requires additional dependencies to run; see
* ps1-bare-metal/tools/requirements.txt for information on how to install them.
* For further information on the contents of the ps1-bare-metal submodule, see:
* https://github.com/spicyjpeg/ps1-bare-metal
*/

#include <stdbool.h>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
* back of the console or to another interface. Additionally, most emulators can
* log calls to printf() and display the output in their log window.
*
* For a variant of this project that prints directly to the PS1's serial port
* instead of using the kernel, see:
* A variant of this project that prints directly to the PS1's serial port
* instead of using the kernel is available here:
* https://github.com/spicyjpeg/ps1-bare-metal/blob/main/src/00_helloWorld/main.c
*
* For further information on the contents of the ps1-bare-metal submodule, see:
* https://github.com/spicyjpeg/ps1-bare-metal
*/

#define BIOS_API_TABLE ((void **) 0x80000200)
Expand Down

0 comments on commit 065c7a1

Please sign in to comment.