Skip to content

Commit

Permalink
port to libtonc
Browse files Browse the repository at this point in the history
there are a couple remaining problems in audiosync and soundtest
but #55 is coming along
  • Loading branch information
pinobatch committed Mar 22, 2024
1 parent ca14773 commit eee8e94
Show file tree
Hide file tree
Showing 22 changed files with 293 additions and 313 deletions.
5 changes: 3 additions & 2 deletions gba/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,15 @@ LDFLAGS = -g $(ARCH) -Wl,-Map,$(notdir $*.map)
#---------------------------------------------------------------------------------
# any extra libraries we wish to link with the project
#---------------------------------------------------------------------------------
LIBS := -lgba
LIBS := -ltonc


#---------------------------------------------------------------------------------
# list of directories containing libraries, this must be the top level containing
# include and lib
#---------------------------------------------------------------------------------
LIBDIRS := $(LIBGBA)
LIBTONC := $(DEVKITPRO)/libtonc
LIBDIRS := $(LIBTONC)

#---------------------------------------------------------------------------------
# Python executable name depends on operating system.
Expand Down
14 changes: 7 additions & 7 deletions gba/src/4bcanvas.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,19 @@ it freely, subject to the following restrictions:

#include "4bcanvas.h"
#include <sys/types.h>
#include <gba_video.h>
#include <tonc.h>
void dma_memset16(void *dst, unsigned int c16, size_t n);

const TileCanvas screen = {
.left = 0, .top = 0, .width = 32, .height = 20,
.chrBase = (uint32_t *)PATRAM4(0, 0),
.chrBase = tile_mem[0][0].data,
.map = 23,
.core = 0,
.mapTileBase = 0
};

static
void fillcol(uint32_t *dst, unsigned int colStride,
void fillcol(u32 *dst, unsigned int colStride,
unsigned int l, unsigned int t,
unsigned int r, unsigned int b,
unsigned int c)
Expand All @@ -58,13 +58,13 @@ void fillcol(uint32_t *dst, unsigned int colStride,

void canvasRectfill(const TileCanvas *v, int l, int t, int r, int b, int c)
{
uint32_t *dst = v->chrBase;
u32 *dst = v->chrBase;
c &= 0x0000000F;
c *= 0x11111111;

unsigned int x = l;
unsigned int stride = v->height * 8;
uint32_t *tile = dst + stride * (l >> 3);
u32 *tile = dst + stride * (l >> 3);

if (t < 0) {
t = 0;
Expand Down Expand Up @@ -176,9 +176,9 @@ void canvasBlitAligned(const TileCanvas *src, const TileCanvas *dst,

void canvasInit(const TileCanvas *w, unsigned int color) {
#if ARM9
NAMETABLE *dst = w->core ? &(MAP_SUB[w->map]) : &(MAP[w->map]);
SCREENMAT *dst = w->core ? &(se_mat_sub[w->map]) : &(se_mat[w->map]);
#else
NAMETABLE *dst = &(MAP[w->map]);
SCREENMAT *dst = &(se_mat[w->map]);
#endif

int mapTile = w->mapTileBase;
Expand Down
3 changes: 2 additions & 1 deletion gba/src/4bcanvas.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@ it freely, subject to the following restrictions:
#define P8_CANVAS_H

#include <stdint.h>
#include <tonc.h>

typedef struct TileCanvas {
uint8_t left; // in 8 pixel units on nametable
uint8_t top; // in 8 pixel units on nametable
uint8_t width; // in 8 pixel units on nametable
uint8_t height; // in 8 pixel units on nametable
uint32_t *chrBase;
u32 *chrBase;
uint8_t map; // in 2 KiB units on VRAM
uint8_t core; // 0: main; 1: sub
uint16_t mapTileBase;
Expand Down
16 changes: 7 additions & 9 deletions gba/src/audiosync.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
*/
#include "global.h"
#include <gba_video.h>
#include <gba_sound.h>
#include <gba_input.h>
#include <tonc.h>

#define PFMAP 23

Expand Down Expand Up @@ -70,19 +68,19 @@ void activity_audio_sync() {
}

unsigned int y = (progress < 60) ? 128 - progress : 8 + progress;
SOAM[0].attr0 = OBJ_Y(y) | OBJ_16_COLOR | ATTR0_SQUARE;
SOAM[0].attr0 = ATTR0_Y(y) | ATTR0_4BPP | ATTR0_SQUARE;
SOAM[0].attr1 = 119 | ATTR1_SIZE_8;
SOAM[0].attr2 = 0x0023;
ppu_clear_oam(1);

VBlankIntrWait();
REG_DISPCNT = MODE_0 | BG0_ON | OBJ_1D_MAP | OBJ_ON;
BGCTRL[0] = BG_16_COLOR|BG_WID_32|BG_HT_32|CHAR_BASE(0)|SCREEN_BASE(PFMAP);
BG_OFFSET[0].x = BG_OFFSET[0].y = 0;
REG_DISPCNT = DCNT_MODE0 | DCNT_BG0 | DCNT_OBJ_1D | DCNT_OBJ;
REG_BGCNT[0] = BG_4BPP|BG_WID_32|BG_HT_32|BG_CBB(0)|BG_SBB(PFMAP);
REG_BG_OFS[0].x = REG_BG_OFS[0].y = 0;
for (unsigned int i = 0; i < 6; ++i) {
BG_COLORS[i] = progress >= min_progress[i] ? RGB5(31, 31, 31) : RGB5(0, 0, 0);
pal_bg_mem[i] = progress >= min_progress[i] ? RGB5(31, 31, 31) : RGB5(0, 0, 0);
}
OBJ_COLORS[1] = RGB5(31, 31, 31);
pal_obj_mem[1] = RGB5(31, 31, 31);
ppu_copy_oam();

if (progress == 120) {
Expand Down
13 changes: 6 additions & 7 deletions gba/src/backlight.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
*/
#include "global.h"
#include <gba_video.h>
#include <gba_input.h>
#include <tonc.h>

void activity_backlight_zone(void) {
unsigned inverted = 0, hidden = 0, high_gear = 0, held_keys = 0, sz = 1;
Expand Down Expand Up @@ -77,17 +76,17 @@ void activity_backlight_zone(void) {
oam_used = 0;
if (!hidden) {
unsigned int i = oam_used;
SOAM[i].attr0 = OBJ_Y(y) | OBJ_16_COLOR | ATTR0_SQUARE;
SOAM[i].attr1 = OBJ_X(x) | ATTR1_SIZE_8;
SOAM[i].attr0 = ATTR0_Y(y) | ATTR0_4BPP | ATTR0_SQUARE;
SOAM[i].attr1 = ATTR1_X(x) | ATTR1_SIZE_8;
SOAM[i].attr2 = (sz < 3) ? sz + 0x22 : 1;
oam_used = i + 1;
}
ppu_clear_oam(oam_used);

VBlankIntrWait();
BG_COLORS[0] = inverted ? RGB5(31, 31, 31) : RGB5(0, 0, 0);
OBJ_COLORS[1] = inverted ? RGB5(0, 0, 0): RGB5(31, 31, 31);
pal_bg_mem[0] = inverted ? RGB5(31, 31, 31) : RGB5(0, 0, 0);
pal_obj_mem[1] = inverted ? RGB5(0, 0, 0): RGB5(31, 31, 31);
ppu_copy_oam();
REG_DISPCNT = MODE_0 | OBJ_ON;
REG_DISPCNT = DCNT_MODE0 | DCNT_OBJ;
}
}
22 changes: 17 additions & 5 deletions gba/src/global.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#define GLOBAL_H
#include <stdint.h>
#include <sys/types.h>
#include <gba_sprites.h>
#include <gba_systemcalls.h>
#include <tonc.h>
#include "helppages.h"

// Size of a statically sized array
Expand Down Expand Up @@ -105,7 +104,7 @@ unsigned int autorepeat(unsigned int allowed_keys);
typedef unsigned short VBTILE[8];
typedef unsigned char ONEBTILE[8];
extern unsigned char oam_used;
extern OBJATTR SOAM[128];
extern OBJ_ATTR SOAM[128];
void ppu_clear_oam(size_t start);
void ppu_copy_oam(void);
void dma_memset16(void *s, unsigned int c, size_t n);
Expand All @@ -121,13 +120,26 @@ int lcg_rand(void);
// vwfdraw.c
void loadMapRowMajor(unsigned short *dst, unsigned int tilenum,
unsigned int width, unsigned int height);
void vwf8PutTile(uint32_t *dst, unsigned int glyphnum,
void vwf8PutTile(u32 *dst, unsigned int glyphnum,
unsigned int x, unsigned int color);
const char *vwf8Puts(uint32_t *restrict dst, const char *restrict s,
const char *vwf8Puts(u32 *restrict dst, const char *restrict s,
unsigned int x, unsigned int color);
unsigned int vwf8StrWidth(const char *s);

// vwflabels.c
void vwfDrawLabels(const char *labelset, unsigned int sbb, unsigned int tilenum);

// Shims for the libgba to libtonc port
// Note: tonc.h uses u32 (unsigned) instead of standard uint32_t
// (unsigned long) for pointers into video memory
#define MAP se_mat
#define BG_WID_32 BG_SIZE0
#define BG_WID_64 BG_SIZE1
#define BG_HT_32 BG_SIZE0
#define BG_HT_64 BG_SIZE2
// write these as macros instead of static inline to make them constexpr
#define RGB5(r, g, b) (((r)<<0) | ((g)<<5) | ((b)<<10))
#define PATRAM4(cbb, tile) (tile_mem[(cbb)][(tile)].data)
#define SPR_VRAM(tile) (tile_mem_obj[0][(tile)].data)

#endif
55 changes: 25 additions & 30 deletions gba/src/help.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <gba_video.h>
#include <gba_compression.h>
#include <gba_dma.h>
#include <gba_input.h>
#include <gba_sound.h>
#include "global.h"
#include "posprintf.h"
#include "helpsprites_chr.h"
Expand Down Expand Up @@ -105,7 +100,7 @@ static void load_help_bg(void) {
// Clear VWF canvas
dma_memset16(PATRAM4(3, 0), 0x1111 * FG_BGCOLOR, 32*WINDOW_WIDTH*17);

// Load background nametable
// Load background SCREENMAT
dma_memset16(MAP[BGMAP][0], TILE_BACK_WALL, 64*(BACK_WALL_HT - 1));
dma_memset16(MAP[BGMAP][BACK_WALL_HT - 1], TILE_BACK_WALL_BOTTOM, 30*2);
dma_memset16(MAP[BGMAP][BACK_WALL_HT], TILE_FLOOR_TOP, 30*2);
Expand All @@ -118,7 +113,7 @@ static void load_help_bg(void) {
MAP[BGMAP][SHADOW_Y + 1][SHADOW_X + 4 + x] = TILE_FLOOR_SHADOW + 0x0C03 - x;
}

// Clear window nametable
// Clear window SCREENMAT
dma_memset16(MAP[FGMAP], TILE_FG_BLANK, 32*21*2);
dma_memset16(MAP[FGMAP + 1], TILE_FG_XPARENT, 32*21*2);

Expand Down Expand Up @@ -153,22 +148,22 @@ static void help_draw_character(void) {
unsigned int ou = oam_used;

if (++blink_time < 8) {
SOAM[ou].attr0 = 49 | OBJ_16_COLOR | ATTR0_SQUARE;
SOAM[ou].attr0 = 49 | ATTR0_4BPP | ATTR0_SQUARE;
SOAM[ou].attr1 = 48 | ATTR1_SIZE_8;
SOAM[ou].attr2 = BLINK_TILE | ATTR2_PALETTE(0);
SOAM[ou].attr2 = BLINK_TILE | ATTR2_PALBANK(0);
++ou;
SOAM[ou].attr0 = 49 | OBJ_16_COLOR | ATTR0_SQUARE;
SOAM[ou].attr1 = 40 | ATTR1_SIZE_8 | OBJ_HFLIP;
SOAM[ou].attr2 = BLINK_TILE | ATTR2_PALETTE(0);
SOAM[ou].attr0 = 49 | ATTR0_4BPP | ATTR0_SQUARE;
SOAM[ou].attr1 = 40 | ATTR1_SIZE_8 | ATTR1_HFLIP;
SOAM[ou].attr2 = BLINK_TILE | ATTR2_PALBANK(0);
++ou;
}
for (unsigned int i = 0; i < 2; ++i) {
unsigned int a0 = (18 + i * 64) | OBJ_16_COLOR | ATTR0_TALL;
unsigned int a0 = (18 + i * 64) | ATTR0_4BPP | ATTR0_TALL;
unsigned int a1 = 16 | ATTR1_SIZE_64;
unsigned int a2 = (CHARACTER_VRAM_BASE + 32 * i) | ATTR2_PALETTE(0);
unsigned int a2 = (CHARACTER_VRAM_BASE + 32 * i) | ATTR2_PALBANK(0);

SOAM[ou].attr0 = a0;
SOAM[ou].attr1 = a1 | OBJ_HFLIP;
SOAM[ou].attr1 = a1 | ATTR1_HFLIP;
SOAM[ou].attr2 = a2;
++ou;
SOAM[ou].attr0 = a0;
Expand All @@ -183,9 +178,9 @@ static void help_draw_cursor(unsigned int objx) {
if (objx >= 240) return;

unsigned int ou = oam_used;
SOAM[ou].attr0 = (20 + help_cursor_y * 8) | OBJ_16_COLOR | ATTR0_SQUARE;
SOAM[ou].attr0 = (20 + help_cursor_y * 8) | ATTR0_4BPP | ATTR0_SQUARE;
SOAM[ou].attr1 = objx | ATTR1_SIZE_8;
SOAM[ou].attr2 = ARROW_TILE | ATTR2_PALETTE(0);
SOAM[ou].attr2 = ARROW_TILE | ATTR2_PALBANK(0);
oam_used = ou + 1;
}

Expand All @@ -201,7 +196,7 @@ static void help_draw_page(helpdoc_kind doc_num, unsigned int left, unsigned int

// Draw lines of text to the screen
while (y < PAGE_MAX_LINES) {
unsigned long *dst = PATRAM4(3, (y + 1)*WINDOW_WIDTH);
u32 *dst = PATRAM4(3, (y + 1)*WINDOW_WIDTH);
++y;
dma_memset16(dst, FG_BGCOLOR*0x1111, WINDOW_WIDTH * 32);
src = vwf8Puts(dst, src, left, FG_FGCOLOR);
Expand All @@ -213,7 +208,7 @@ static void help_draw_page(helpdoc_kind doc_num, unsigned int left, unsigned int

// Clear unused lines that had been used
for (unsigned int clear_y = y; clear_y < help_height; ++clear_y) {
unsigned long *dst = PATRAM4(3, (clear_y + 1)*WINDOW_WIDTH);
u32 *dst = PATRAM4(3, (clear_y + 1)*WINDOW_WIDTH);
dma_memset16(dst, FG_BGCOLOR*0x1111, WINDOW_WIDTH * 32);
}

Expand All @@ -225,7 +220,7 @@ static void help_draw_page(helpdoc_kind doc_num, unsigned int left, unsigned int

// Draw status line depending on size of document and which
// keys are enabled
unsigned long *dst = PATRAM4(3, (PAGE_MAX_LINES + 1)*WINDOW_WIDTH);
u32 *dst = PATRAM4(3, (PAGE_MAX_LINES + 1)*WINDOW_WIDTH);
dma_memset16(dst, FG_BGCOLOR*0x1111, WINDOW_WIDTH * 32);

if (help_cumul_pages[doc_num + 1] - help_cumul_pages[doc_num] > 1) {
Expand Down Expand Up @@ -297,7 +292,7 @@ unsigned int helpscreen(helpdoc_kind doc_num, unsigned int keymask) {

// If the help VRAM needs to be reloaded, reload its tiles and map
if (!help_bg_loaded) {
REG_DISPCNT = LCDC_OFF;
REG_DISPCNT = DCNT_BLANK;
load_help_bg();
REG_DISPCNT = 0;
} else {
Expand All @@ -313,15 +308,15 @@ unsigned int helpscreen(helpdoc_kind doc_num, unsigned int keymask) {

// Load palette
VBlankIntrWait();
dmaCopy(helpbgtiles_chrPal, BG_COLORS+0x00, sizeof(helpbgtiles_chrPal));
dmaCopy(helpsprites_chrPal, OBJ_COLORS+0x00, sizeof(helpsprites_chrPal));
tonccpy(pal_bg_mem+0x00, helpbgtiles_chrPal, sizeof(helpbgtiles_chrPal));
tonccpy(pal_obj_mem+0x00, helpsprites_chrPal, sizeof(helpsprites_chrPal));

// Set up background regs (except DISPCNT)
BGCTRL[1] = BG_16_COLOR|BG_WID_32|BG_HT_32|CHAR_BASE(3)|SCREEN_BASE(BGMAP);
BGCTRL[0] = BG_16_COLOR|BG_WID_64|BG_HT_32|CHAR_BASE(3)|SCREEN_BASE(FGMAP);
BG_OFFSET[1].x = BG_OFFSET[1].y = 0;
BG_OFFSET[0].x = help_wnd_progress ? WXBASE : 256;
BG_OFFSET[0].y = 4;
REG_BGCNT[1] = BG_4BPP|BG_WID_32|BG_HT_32|BG_CBB(3)|BG_SBB(BGMAP);
REG_BGCNT[0] = BG_4BPP|BG_WID_64|BG_HT_32|BG_CBB(3)|BG_SBB(FGMAP);
REG_BG_OFS[1].x = REG_BG_OFS[1].y = 0;
REG_BG_OFS[0].x = help_wnd_progress ? WXBASE : 256;
REG_BG_OFS[0].y = 4;

// Freeze
while (1) {
Expand Down Expand Up @@ -374,8 +369,8 @@ unsigned int helpscreen(helpdoc_kind doc_num, unsigned int keymask) {
if (help_show_cursor) help_draw_cursor(512 - wx + 6);
ppu_clear_oam(oam_used);
VBlankIntrWait();
REG_DISPCNT = MODE_0 | BG1_ON | BG0_ON | OBJ_1D_MAP | OBJ_ON;
BG_OFFSET[0].x = wx;
REG_DISPCNT = DCNT_MODE0 | DCNT_BG1 | DCNT_BG0 | DCNT_OBJ_1D | DCNT_OBJ;
REG_BG_OFS[0].x = wx;
ppu_copy_oam();
}
}
Expand Down
12 changes: 6 additions & 6 deletions gba/src/helppages.txt
Original file line number Diff line number Diff line change
Expand Up @@ -187,17 +187,17 @@ Like the Game Boy Color,
the Game Boy Advance
has a TFT display with
a decent response time.

All GB LCDs use row
inversion that can look
like interlace, drawing
even or odd lines darker
in alternate frames.

The Game Boy Player
accessory has a "Screen
Filter" effect that applies
a form of motion blur.
accessory's "Screen Filter"
effect applies motion blur
to pixel areas that match
two frames ago.

== Sharpness ==

Expand Down Expand Up @@ -665,10 +665,10 @@ Select: Show or hide
== 160p Test Suite ==

$COMMIT
HDMA is right for you
Welcome to Tonc

© 2011-2016 Artemio Urbina
© 2015-2023 Damian Yerrick
© 2015-2024 Damian Yerrick
This is free software; see
the source for copying
conditions. The program
Expand Down
Loading

0 comments on commit eee8e94

Please sign in to comment.