From fd5beb2adfd2ceb1bfc1f8bdb103a73b17577327 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Fri, 7 Apr 2017 18:49:41 -0500 Subject: [PATCH] Add ability to read media section --- .gitignore | 1 + cmake/Modules/GBA.cmake | 9 +++++-- src/core/CMakeLists.txt | 1 + src/core/gba/addresses.hpp | 48 ++++++++++++++++++++++++++++++++++++++ src/core/gba/gfx.cpp | 28 ++++++++++++++-------- src/core/gba/media.cpp | 36 ++++++++++++++++++++++++++++ src/core/gba/media.hpp | 19 +++++++++++++++ src/core/gba/registers.hpp | 34 --------------------------- src/player/CMakeLists.txt | 6 +++-- src/player/main.cpp | 1 + 10 files changed, 135 insertions(+), 48 deletions(-) create mode 100644 src/core/gba/addresses.hpp create mode 100644 src/core/gba/media.cpp create mode 100644 src/core/gba/media.hpp delete mode 100644 src/core/gba/registers.hpp diff --git a/.gitignore b/.gitignore index b90a9bf8..a676324e 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ build/current build/gba build/*-release build/*-debug +tags diff --git a/cmake/Modules/GBA.cmake b/cmake/Modules/GBA.cmake index 7d112f2d..df290b56 100644 --- a/cmake/Modules/GBA.cmake +++ b/cmake/Modules/GBA.cmake @@ -13,6 +13,8 @@ endif() set(CMAKE_C_COMPILER ${DEVKITARM}/bin/arm-none-eabi-gcc) set(CMAKE_CXX_COMPILER ${DEVKITARM}/bin/arm-none-eabi-g++) set(CMAKE_OBJCOPY ${DEVKITARM}/bin/arm-none-eabi-objcopy) +set(CMAKE_PADBIN ${DEVKITARM}/bin/padbin) +set(CMAKE_GBAFIX ${DEVKITARM}/bin/gbafix) set(CMAKE_FIND_ROOT_PATH ${DEVKITARM}) set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) @@ -38,18 +40,21 @@ macro(OBJCOPY_FILE EXE_NAME) set(FO ${CMAKE_CURRENT_BINARY_DIR}/${EXE_NAME}.bin) set(FI ${CMAKE_CURRENT_BINARY_DIR}/${EXE_NAME}) message(STATUS ${FO}) + + # run objcopy add_custom_command( OUTPUT "${FO}" COMMAND ${CMAKE_OBJCOPY} ARGS -O binary ${FI} ${FO} - DEPENDS ${FI} + DEPENDS "${FI}" ) + get_filename_component(TGT "${EXE_NAME}" NAME) add_custom_target("TargetObjCopy_${TGT}" ALL DEPENDS ${FO} VERBATIM) get_directory_property(extra_clean_files ADDITIONAL_MAKE_CLEAN_FILES) set_directory_properties( PROPERTIES - ADDITIONAL_MAKE_CLEAN_FILES "${extra_clean_files};${FO}" + ADDITIONAL_MAKE_CLEAN_FILES "${extra_clean_files};${FO};" ) set_source_files_properties("${FO}" PROPERTIES GENERATED TRUE) endmacro(OBJCOPY_FILE) diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 93cfb318..99bcb1c5 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -5,6 +5,7 @@ if(WOMBAT_BUILD_TYPE STREQUAL "GBA") set( CPP gba/gfx.cpp + gba/media.cpp gba/dirt.s ) elseif(WOMBAT_BUILD_TYPE STREQUAL "Native") diff --git a/src/core/gba/addresses.hpp b/src/core/gba/addresses.hpp new file mode 100644 index 00000000..d8762ca8 --- /dev/null +++ b/src/core/gba/addresses.hpp @@ -0,0 +1,48 @@ +/* + * Copyright 2016-2017 gtalent2@gmail.com + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ +#pragma once + +#include + +///////////////////////////////////////////////////////////////// +// I/O Registers + +#define REG_DISPCNT *((volatile uint32_t*) 0x04000000) + +///////////////////////////////////////////////////////////////// +// background registers + +// background control registers +#define REG_BG0CNT *((volatile uint32_t*) 0x04000008) +#define REG_BG1CNT *((volatile uint32_t*) 0x0400000a) +#define REG_BG2CNT *((volatile uint32_t*) 0x0400000c) +#define REG_BG3CNT *((volatile uint32_t*) 0x0400000e) + +// background horizontal scrolling registers +#define REG_BG0HOFS *((volatile uint32_t*) 0x04000010) +#define REG_BG1HOFS *((volatile uint32_t*) 0x04000014) +#define REG_BG2HOFS *((volatile uint32_t*) 0x04000018) +#define REG_BG3HOFS *((volatile uint32_t*) 0x0400001c) + +// background vertical scrolling registers +#define REG_BG0VOFS *((volatile uint32_t*) 0x04000012) +#define REG_BG1VOFS *((volatile uint32_t*) 0x04000016) +#define REG_BG2VOFS *((volatile uint32_t*) 0x0400001a) +#define REG_BG3VOFS *((volatile uint32_t*) 0x0400001e) + + +///////////////////////////////////////////////////////////////// +// Memory Addresses + +#define MEM_PALLETE_BG *((unsigned short*) 0x05000000) +#define MEM_PALLETE_SPRITE *((unsigned short*) 0x05000200) + +typedef uint16_t BgMapTile[1024]; +#define MEM_BG_MAP ((BgMapTile*) 0x06000000) + +#define MEM_ROM *((uint8_t*) 0x08000000) diff --git a/src/core/gba/gfx.cpp b/src/core/gba/gfx.cpp index 303decfb..0ed31c25 100644 --- a/src/core/gba/gfx.cpp +++ b/src/core/gba/gfx.cpp @@ -6,13 +6,16 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -#include -#include "registers.hpp" +#include +#include +#include "addresses.hpp" +#include "media.hpp" #include "dirt.h" namespace nostalgia { namespace core { +using namespace ox::fs; using namespace ox::std; typedef struct { uint32_t data[8]; } Tile, Tile4; @@ -26,12 +29,6 @@ typedef Tile8 CharBlock8[256]; #define TILE_ADDR ((CharBlock*) 0x06000000) #define TILE8_ADDR ((CharBlock8*) 0x06000000) -#define PALLETE_BG ((unsigned short*) 0x05000000) -#define PALLETE_SPRITE ((unsigned short*) 0x05000200) - -typedef uint16_t BgMapTile[1024]; -#define BG_MAP ((BgMapTile*) 0x06000000) - ox::std::Error initGfx() { /* Sprite Mode ----\ */ /* ---\| */ @@ -43,11 +40,22 @@ ox::std::Error initGfx() { TILE_ADDR[0][1] = *(Tile*) dirtTiles; for (auto i = 0; i < (dirtPalLen / 2); i++) { - PALLETE_BG[i] = dirtPal[i]; + (&MEM_PALLETE_BG)[i] = dirtPal[i]; } - BG_MAP[28][52] = 1; + auto fs = (FileStore32*) findMedia(); REG_BG0CNT = (28 << 8) | 1; + MEM_BG_MAP[28][106] = 1; + MEM_BG_MAP[28][107] = 1; + if (fs) { + char out[6]; + FileStore32::FsSize_t outSize = 0; + fs->read(3, out, &outSize); + if (outSize == 5 && ox_strcmp(out, "narf") == 0) { + MEM_BG_MAP[28][138] = 1; + MEM_BG_MAP[28][139] = 1; + } + } return 0; } diff --git a/src/core/gba/media.cpp b/src/core/gba/media.cpp new file mode 100644 index 00000000..6e5beeac --- /dev/null +++ b/src/core/gba/media.cpp @@ -0,0 +1,36 @@ +/* + * Copyright 2016-2017 gtalent2@gmail.com + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#include +#include +#include "addresses.hpp" +#include "media.hpp" + +namespace nostalgia { +namespace core { + +uint8_t *findMedia() { + // put the header in the wrong order to prevent mistaking this code for the + // media section + const static auto headerP2 = "_HEADER_________"; + const static auto headerP1 = "NOSTALGIA_MEDIA"; + const static auto headerP1Len = 15; + const static auto headerP2Len = 16; + const static auto headerLen = headerP1Len + headerP2Len + 1; + + for (uint8_t *current = &MEM_ROM; current < ((uint8_t*) 0x0a000000); current += headerLen) { + if (ox_memcmp(current, headerP1, headerP1Len) == 0 || + ox_memcmp(current + headerP1Len, headerP2, headerP2Len) == 0) { + return current + headerLen; + } + } + return 0; +} + +} +} diff --git a/src/core/gba/media.hpp b/src/core/gba/media.hpp new file mode 100644 index 00000000..1acc95d1 --- /dev/null +++ b/src/core/gba/media.hpp @@ -0,0 +1,19 @@ +/* + * Copyright 2016-2017 gtalent2@gmail.com + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#pragma once + +#include + +namespace nostalgia { +namespace core { + +uint8_t *findMedia(); + +} +} diff --git a/src/core/gba/registers.hpp b/src/core/gba/registers.hpp deleted file mode 100644 index f3ef3585..00000000 --- a/src/core/gba/registers.hpp +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright 2016-2017 gtalent2@gmail.com - * - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. - */ -#ifndef NOSTALGIA_CORE_GBA_REGISTERS_HPP -#define NOSTALGIA_CORE_GBA_REGISTERS_HPP - -#define REG_DISPCNT *((volatile unsigned int*) 0x04000000) - -///////////////////////////////////////////////////////////////// -// background registers - -// control registers -#define REG_BG0CNT *((volatile unsigned int*) 0x04000008) -#define REG_BG1CNT *((volatile unsigned int*) 0x0400000a) -#define REG_BG2CNT *((volatile unsigned int*) 0x0400000c) -#define REG_BG3CNT *((volatile unsigned int*) 0x0400000e) - -// horizontal scrolling registers -#define REG_BG0HOFS *((volatile unsigned int*) 0x04000010) -#define REG_BG1HOFS *((volatile unsigned int*) 0x04000014) -#define REG_BG2HOFS *((volatile unsigned int*) 0x04000018) -#define REG_BG3HOFS *((volatile unsigned int*) 0x0400001c) - -// vertical scrolling registers -#define REG_BG0VOFS *((volatile unsigned int*) 0x04000012) -#define REG_BG1VOFS *((volatile unsigned int*) 0x04000016) -#define REG_BG2VOFS *((volatile unsigned int*) 0x0400001a) -#define REG_BG3VOFS *((volatile unsigned int*) 0x0400001e) - -#endif diff --git a/src/player/CMakeLists.txt b/src/player/CMakeLists.txt index 72e20649..45dbdf24 100644 --- a/src/player/CMakeLists.txt +++ b/src/player/CMakeLists.txt @@ -7,18 +7,20 @@ add_executable( main.cpp ) -if(COMMAND objcopy_file) +if(COMMAND OBJCOPY_FILE) set_target_properties(nostalgia PROPERTIES LINK_FLAGS ${LINKER_FLAGS} COMPILER_FLAGS "-mthumb -mthumb-interwork" ) - objcopy_file(nostalgia) + OBJCOPY_FILE(nostalgia) + #PADBIN_FILE(nostalgia) endif() target_link_libraries( nostalgia NostalgiaCore ${OxStd_LIBRARY} + ${OxFS_LIBRARY} ) diff --git a/src/player/main.cpp b/src/player/main.cpp index e3de04af..a63a883e 100644 --- a/src/player/main.cpp +++ b/src/player/main.cpp @@ -12,5 +12,6 @@ using namespace nostalgia; int main() { core::init(); + while (1); return 0; }