initial commit

This commit is contained in:
nytpu 2021-07-29 12:17:45 -06:00
commit 95218de847
9 changed files with 626 additions and 0 deletions

11
.editorconfig Normal file
View File

@ -0,0 +1,11 @@
# .editorconfig for nytpu's personal C style
# https://editorconfig-specification.readthedocs.io/
root = true
[*]
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
charset = utf-8
indent_style = tab
indent_size = 4

7
.gitignore vendored Normal file
View File

@ -0,0 +1,7 @@
*.gba
*.elf
*.gbfs
*.pal.bin
*.img.bin
*.o
/config.mk

25
LICENSE Normal file
View File

@ -0,0 +1,25 @@
Boost Software License - Version 1.0 - August 17th, 2003
Copyright (c) 2021 nytpu <alex [at] nytpu.com>
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by this
license (the "Software") to use, reproduce, display, distribute, execute, and
transmit the Software, and to prepare derivative works of the Software, and to
permit third-parties to whom the Software is furnished to do so, all subject to
the following:
The copyright notices in the Software and this entire statement, including the
above license grant, this restriction and the following disclaimer, must be
included in all copies of the Software, in whole or in part, and all derivative
works of the Software, unless such copies or derivative works are solely in the
form of machine-executable object code generated by a source language
processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL
THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE FOR ANY
DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

116
Makefile Normal file
View File

@ -0,0 +1,116 @@
#!/usr/bin/make -f
# Pure POSIX Makefile for GBA developemnt
#
#
# Copyright (c) 2021 nytpu <alex [at] nytpu.com>
# SPDX-License-Identifier: BSL-1.0
# The orginal source for this file is available at <https://git.sr.ht/~nytpu/genc>.
#
# Permission is hereby granted, free of charge, to any person or organization
# obtaining a copy of the software and accompanying documentation covered by
# this license (the "Software") to use, reproduce, display, distribute,
# execute, and transmit the Software, and to prepare derivative works of the
# Software, and to permit third-parties to whom the Software is furnished to
# do so, all subject to the following:
#
# The copyright notices in the Software and this entire statement, including
# the above license grant, this restriction and the following disclaimer, must
# be included in all copies of the Software, in whole or in part, and all
# derivative works of the Software, unless such copies or derivative works are
# solely in the form of machine-executable object code generated by a source
# language processor.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
# SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
# FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
.POSIX:
.SILENT:
.SUFFIXES:
## program info
PROGNAME = tonc-template # CHANGE ME
## compilation flags
ARCH = -mthumb -mthumb-interwork
CFLAGS = -g -Wall -Wextra -Wfatal-errors -O3 \
-mcpu=arm7tdmi -mtune=arm7tdmi $(ARCH) -Wno-missing-field-initializers \
-Wno-unused-parameter -Werror=return-type
ASFLAGS = -g $(ARCH)
LDFLAGS = -g $(ARCH)
LDLIBS = -ltonc -lmisc
# configure options to modify flags
include config.mk
## artifacts to build
# C source files to build, with .o extension
OBJS = main.o gbfs.o
# Files to include in the GBFS bin
GBFSFILES =
# Files to convert from .ase to .png
PNGFILES =
## default target
all: $(PROGNAME).gba
## dependencies
main.o: gbfs.h
libgbfs.o: gbfs.h
$(OBJS): config.mk Makefile
## rules
.SUFFIXES: .o .c .s .img.bin .pal.bin .png .ase
.c.o:
printf 'Compiling\t$<\n'
$(CC) $(CFLAGS) -c -o $@ $<
.s.o:
printf 'Assembling\t$<\n'
$(AS) $(ASFLAGS) $< -o $@
.png.pal.bin .png.img.bin:
printf 'Converting\t$< to\t$@\n'
$(GRIT) $< -o $@ -ff
.ase.png:
printf 'Converting\t$< to\t$@\n'
aseprite -b $< --save-as $@
$(PROGNAME).gba: $(PROGNAME).elf $(PROGNAME).gbfs libmisc/libmisc.a
printf 'Finalizing ROM\t$@\n'
$(OBJCOPY) -O binary $< $@
$(GBAFIX) $@
$(PADBIN) 256 $@
cat $(PROGNAME).gbfs >> $@
$(PROGNAME).elf: $(OBJS)
printf 'Linking\t\t$@\n'
$(CC) $(LDFLAGS) -specs=gba.specs $(OBJS) $(LDLIBS) -o $@
$(PROGNAME).gbfs: $(GBFSFILES)
printf 'Archiving GBFS\t$@\n'
$(GBFS) $@ $(GBFSFILES)
libmisc/libmisc.a:
cd libmisc && CC='$(CC)' CFLAGS='$(CFLAGS)' ./configure && make
## phonies
# technically .PHONY isn't POSIX, but targets with a leading period aren't
# reserved so it's still valid, it'd just be useless to actually execute
.PHONY: all clean
clean:
rm -rf $(PROGNAME).gba $(PROGNAME).elf $(PROGNAME).gbfs \
$(OBJS) $(GBFSFILES) $(PNGFILES)
cd libmisc && make clean

83
README.md Normal file
View File

@ -0,0 +1,83 @@
# tonc-template
This template is designed to be used to create gba homebrew using the Tonc
Library. The template is written from scratch and is designed to be
self-contained (other than devkitPro) and easy to understand and modify.
If you want to make homebrew the [tonc
tutorial](http://www.coranac.com/tonc/text/toc.htm) is the best place to learn
to do so, even if you intend to use libgba afterwards.
## Features
- Easy to understand and modify build system
- Very portable, all that's required to build is a POSIX-compatible sh(1),
POSIX-compatible make(1), and a devkitARM installation.
- Includes bundled libgbfs for easily including binary files such as art and
music
- Automatically determines all tool and compiler paths, all you have to do is
set the $`DEVKITPRO` environment variable.
## Usage
### Requirements
- POSIX or POSIX-compatible `sh`.
- POSIX `make`. Most `make`s (including GNU Make and BSD Make) support the
POSIX standard.
- A full devkitARM (`gba-dev`) installation. See [the
devkitPro wiki](https://devkitpro.org/wiki/Getting_Started) for help on
installation.
### Building
Set $`DEVKITPRO` to point to your devkitPro installation (this should be
automatically done most of the time). You can also set $`CFLAGS`, $`ASFLAGS`,
and $`LDFLAGS` to add additional flags to the C compiler, assembler, and
linker, respectively.
./configure
make
### Modifying
Change the $`PROGNAME` macro in the `Makefile` to match your game's name. Then
simply add your code to `main.c` like you normally would. To add more C source
files, add them to the $`OBJS` macro in the `Makefile`. To add files to be
bundled with the ROM with GBFS, add the filename to the $`GBFSFILES` macro in
the `Makefile`.
You can include `<tonc.h>` to get libtonc functions and `"gbfs.h"` to get GBFS
functions. Also included is a modified version of my own
[libmisc](https://git.sr.ht/~nytpu/libmisc) that provides some useful functions
(notably an arena allocator and string utilities).
## Contributing
The upstream URL of this project is <https://git.sr.ht/~nytpu/tonc-template>.
Send suggestions, bugs, and other contributions to
<~nytpu/public-inbox@lists.sr.ht>. For help sending a patch through email, see
<https://git-send-email.io>. You can browse the list archives at
<https://lists.sr.ht/~nytpu/public-inbox>.
## Copyright
Copyright (c) 2021 nytpu <alex [at] nytpu.com>
Licensed under the Boost Software License, version 1.0. You can view a copy of
the Boost Software License in [LICENSE](LICENSE) or at
<https://www.boost.org/LICENSE_1_0.txt>.
This repository contains [libmisc](https://git.sr.ht/~nytpu/libmisc), which is
also licensed under the terms of the Boost Software License, version 1.0.
This repository contains libgbfs. libgbfs is Copyright 2002 Damian Yerrick and
is licensed under the terms of the MIT license. See the header of
[`gbfs.h`](gbfs.h) for a full copy of the license.
All credit for tonclib goes to cearn.

104
configure vendored Executable file
View File

@ -0,0 +1,104 @@
#!/bin/sh
# POSIX shell configure script for GBA development
#
#
# Copyright (c) 2021 nytpu <alex [at] nytpu.com>
# SPDX-License-Identifier: BSL-1.0
# The orginal source for this file is available at <https://git.sr.ht/~nytpu/tonc-template>.
#
# Permission is hereby granted, free of charge, to any person or organization
# obtaining a copy of the software and accompanying documentation covered by
# this license (the "Software") to use, reproduce, display, distribute,
# execute, and transmit the Software, and to prepare derivative works of the
# Software, and to permit third-parties to whom the Software is furnished to
# do so, all subject to the following:
#
# The copyright notices in the Software and this entire statement, including
# the above license grant, this restriction and the following disclaimer, must
# be included in all copies of the Software, in whole or in part, and all
# derivative works of the Software, unless such copies or derivative works are
# solely in the form of machine-executable object code generated by a source
# language processor.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
# SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
# FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
set -eu
# redirect stdout to config.mk beyond this point
exec >config.mk
echo "# Configuration for Makefile"
echo "# This file was generated automatically by configure. Don't edit."
while getopts "h" opt; do
case "${opt}" in
(h)
exec 1>&2
printf "usage: [ENV=VAR] %s\n" "$0"
printf "\nsupported environment variables:\n"
printf "\tDEVKITPRO path to the devkitPro installation\n"
printf "\tCFLAGS additional flags for the C compiler\n"
printf "\tASFLAGS additional flags for the assembler\n"
printf "\tLDFLAGS additional flags for the linker\n"
exit 0
;;
(?)
printf "%s: invalid option '%c'\n" "$0" "${opt}" >&2
exit 1
;;
esac
done
[ -n "${CFLAGS:-}" ] && printf "CFLAGS += %s\n" "${CFLAGS}"
[ -n "${ASFLAGS:-}" ] && printf "ASFLAGS += %s\n" "${ASFLAGS}"
[ -n "${LDFLAGS:-}" ] && printf "LDFLAGS += %s\n" "${LDFLAGS}"
if [ -z "${DEVKITPRO:-}" ] || ! [ -d "${DEVKITPRO:-}" ]; then
exec 1>&2
printf "Couldn't find devkitPro directory.\n"
printf "Please set the \$DEVKITPRO environment variable.\n"
exit 1
fi
dkptools="${DEVKITPRO}/tools/bin"
armtoolchain="${DEVKITPRO}/devkitARM/bin"
# verify all required stuff exists
if ! [ -d "${DEVKITPRO}/libtonc" ]; then
exec 1>&2
printf "Couldn't find libtonc.\n"
printf "Ensure that \$DEVKITPRO/libtonc exists and contains\n"
printf "the \"lib/\" and \"include/\" directories.\n"
exit 1
fi
if ! [ -d "${dkptools}" ]; then
exec 1>&2
printf "Couldn't find devkitPro tools.\n"
printf "Ensure that \$DEVKITPRO/tools/bin exists and contains:\n"
printf "\tgbafix\n\tpadbin\n\tgrit\n\tgbfs\n"
exit 1
fi
if ! [ -d "${armtoolchain}" ]; then
exec 1>&2
printf "Couldn't find the devkitARM toolchain.\n"
printf "Ensure that \$DEVKITPRO/devkitARM/bin exists and contains:\n"
printf "\tarm-none-eabi-gcc\n\tarm-none-eabi-as\n\tarm-none-eabi-objcopy\n"
exit 1
fi
echo "CC = ${armtoolchain}/arm-none-eabi-gcc"
echo "AS = ${armtoolchain}/arm-none-eabi-as"
echo "OBJCOPY = ${armtoolchain}/arm-none-eabi-objcopy"
echo "GBAFIX = ${dkptools}/gbafix"
echo "PADBIN = ${dkptools}/padbin"
echo "GRIT = ${dkptools}/grit"
echo "GBFS = ${dkptools}/gbfs"
echo "LDFLAGS += -L${DEVKITPRO}/libtonc/lib"
echo "CFLAGS += -I${DEVKITPRO}/libtonc/include"
exit 0

84
gbfs.h Normal file
View File

@ -0,0 +1,84 @@
/* gbfs.h
access object in a GBFS file
Copyright 2002 Damian Yerrick
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
*/
/* Dependency on prior include files
Before you #include "gbfs.h", you should define the following types:
typedef (unsigned 16-bit integer) u16;
typedef (unsigned 32-bit integer) u32;
Your gba.h should do this for you.
*/
#ifndef INCLUDE_GBFS_H
#define INCLUDE_GBFS_H
#ifdef __cplusplus
extern "C" {
#endif
/* to make a 300 KB space called samples do GBFS_SPACE(samples, 300) */
#define GBFS_SPACE(filename, kbytes) \
const char filename[(kbytes)*1024] __attribute__ ((aligned (16))) = \
"PinEightGBFSSpace-" #filename "-" #kbytes ;
typedef struct GBFS_FILE
{
char magic[16]; /* "PinEightGBFS\r\n\032\n" */
u32 total_len; /* total length of archive */
u16 dir_off; /* offset in bytes to directory */
u16 dir_nmemb; /* number of files */
char reserved[8]; /* for future use */
} GBFS_FILE;
typedef struct GBFS_ENTRY
{
char name[24]; /* filename, nul-padded */
u32 len; /* length of object in bytes */
u32 data_offset; /* in bytes from beginning of file */
} GBFS_ENTRY;
const GBFS_FILE *find_first_gbfs_file(const void *start);
const void *skip_gbfs_file(const GBFS_FILE *file);
const void *gbfs_get_obj(const GBFS_FILE *file,
const char *name,
u32 *len);
const void *gbfs_get_nth_obj(const GBFS_FILE *file,
size_t n,
char *name,
u32 *len);
void *gbfs_copy_obj(void *dst,
const GBFS_FILE *file,
const char *name);
size_t gbfs_count_objs(const GBFS_FILE *file);
#ifdef __cplusplus
}
#endif
#endif

175
libgbfs.c Normal file
View File

@ -0,0 +1,175 @@
/* libgbfs.c
access object in a GBFS file
Copyright 2002-2004 Damian Yerrick
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
*/
/* This code assumes a LITTLE ENDIAN target. It'll need a boatload
of itohs and itohl calls if converted to run on Sega Genesis. It
also assumes that the target uses 16-bit short and 32-bit longs.
*/
typedef unsigned short u16;
typedef unsigned long u32;
#include <stdlib.h>
#include <string.h>
#include "gbfs.h"
/* change this to the end of your ROM, or to 0x02040000 for multiboot */
#define GBFS_1ST_SEARCH_LIMIT ((const u32 *)0x02040000)
#define GBFS_2ND_SEARCH_START ((const u32 *)0x08000000)
#define GBFS_2ND_SEARCH_LIMIT ((const u32 *)0x0a000000)
/* a power of two, less than or equal to the argument passed to
padbin. Increasing the stride makes find_first_gbfs_file()
faster at the cost of a slightly larger binary. */
#define GBFS_ALIGNMENT 256
const GBFS_FILE *find_first_gbfs_file(const void *start)
{
/* align the pointer */
const u32 *here = (const u32 *)
((unsigned long)start & (-GBFS_ALIGNMENT));
const char rest_of_magic[] = "ightGBFS\r\n\x1a\n";
/* Linear-search first in multiboot space. */
while(here < GBFS_1ST_SEARCH_LIMIT)
{
/* We have to keep the magic code in two pieces; otherwise,
this function may find itself and think it's a GBFS file.
This obviously won't work if your compiler stores this
numeric literal just before the literal string, but Devkit
Advance R4 and R5 seem to keep numeric constant pools
separate enough from string pools for this to work.
*/
if(*here == 0x456e6950) /* ASCII code for little endian "PinE" */
{
/* We've matched the first four bytes.
If the rest of the magic matches, then we've found a file. */
if(!memcmp(here + 1, rest_of_magic, 12))
return (const GBFS_FILE *)here;
}
here += GBFS_ALIGNMENT / sizeof(*here);
}
/* Now search in ROM space. */
if(here < GBFS_2ND_SEARCH_START)
here = GBFS_2ND_SEARCH_START;
while(here < GBFS_2ND_SEARCH_LIMIT)
{
/* Search loop same as above. */
if(*here == 0x456e6950) /* ASCII code for little endian "PinE" */
{
if(!memcmp(here + 1, rest_of_magic, 12))
return (const GBFS_FILE *)here;
}
here += GBFS_ALIGNMENT / sizeof(*here);
}
return 0;
}
const void *skip_gbfs_file(const GBFS_FILE *file)
{
return ((char *)file + file->total_len);
}
static int namecmp(const void *a, const void *b)
{
return memcmp(a, b, 24);
}
const void *gbfs_get_obj(const GBFS_FILE *file,
const char *name,
u32 *len)
{
char key[24] = {0};
const GBFS_ENTRY *dirbase = (const GBFS_ENTRY *)((const char *)file + file->dir_off);
size_t n_entries = file->dir_nmemb;
const GBFS_ENTRY *here;
strncpy(key, name, 23);
here = bsearch(key, dirbase,
n_entries, sizeof(GBFS_ENTRY),
namecmp);
if(!here)
return NULL;
if(len)
*len = here->len;
return (char *)file + here->data_offset;
}
const void *gbfs_get_nth_obj(const GBFS_FILE *file,
size_t n,
char *name,
u32 *len)
{
const GBFS_ENTRY *dirbase = (const GBFS_ENTRY *)((const char *)file + file->dir_off);
size_t n_entries = file->dir_nmemb;
const GBFS_ENTRY *here = dirbase + n;
if(n >= n_entries)
return NULL;
if(name)
{
strncpy(name, here->name, 24);
name[24] = 0;
}
if(len)
*len = here->len;
return (char *)file + here->data_offset;
}
void *gbfs_copy_obj(void *dst,
const GBFS_FILE *file,
const char *name)
{
u32 len;
const void *src = gbfs_get_obj(file, name, &len);
if(!src)
return NULL;
memcpy(dst, src, len);
return dst;
}
size_t gbfs_count_objs(const GBFS_FILE *file)
{
return file ? file->dir_nmemb : 0;
}

21
main.c Normal file
View File

@ -0,0 +1,21 @@
#include <tonc.h>
#include "gbfs.h"
int
main(void)
{
// Wait for vsync
vid_vsync();
// Bitmap mode 3, enable BG2 (bitmap layer)
REG_DISPCNT = DCNT_MODE3 | DCNT_BG2;
// Plot RGB points
m3_plot(119, 80, RGB15(31, 0, 0)); // Red
m3_plot(120, 80, RGB15(0, 31, 0)); // Green
m3_plot(121, 80, RGB15(0, 0, 31)); // Blue
while (1); // Loop so you don't get nasal demons
return 0;
}