Skip to content

Commit

Permalink
Merge pull request #822 from Daft-Freak/blit-header-extend
Browse files Browse the repository at this point in the history
Extend .blit header with api/device info
  • Loading branch information
Daft-Freak committed Jun 1, 2024
2 parents fc3f57d + c97b8a8 commit b43a2e3
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 16 deletions.
2 changes: 1 addition & 1 deletion 32blit-stm32/executable.cmake
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
set(MCU_LINKER_SCRIPT "${CMAKE_CURRENT_LIST_DIR}/${MCU_LINKER_SCRIPT}")
set(HAL_DIR ${CMAKE_CURRENT_LIST_DIR})

set(USER_STARTUP ${CMAKE_CURRENT_LIST_DIR}/startup_user.s ${CMAKE_CURRENT_LIST_DIR}/startup_user.cpp)
set(USER_STARTUP ${CMAKE_CURRENT_LIST_DIR}/startup_user.S ${CMAKE_CURRENT_LIST_DIR}/startup_user.cpp)

function(blit_executable_common NAME)
target_link_libraries(${NAME} BlitEngine)
Expand Down
6 changes: 5 additions & 1 deletion 32blit-stm32/startup_user.s → 32blit-stm32/startup_user.S
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
******************************************************************************
*/

#include "engine/api_version.h"

.syntax unified
.cpu cortex-m7
.fpu softvfp
Expand Down Expand Up @@ -132,7 +134,9 @@ g_pfnVectors:
.word _ZN4blit4tickEm
.word do_init
.word _flash_end
.word flash_start // temp
.word 1 // device_id = 1 + padding
.hword BLIT_API_VERSION_MAJOR
.hword BLIT_API_VERSION_MINOR

/*
.weak render
Expand Down
3 changes: 2 additions & 1 deletion 32blit/engine/api_private.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <cstdint>
#include <vector>

#include "api_version.h"
#include "engine.hpp"
#include "file.hpp"
#include "../audio/audio.hpp"
Expand All @@ -17,7 +18,7 @@ namespace blit {

using AllocateCallback = uint8_t *(*)(size_t);

constexpr uint16_t api_version_major = 0, api_version_minor = 2;
constexpr uint16_t api_version_major = BLIT_API_VERSION_MAJOR, api_version_minor = BLIT_API_VERSION_MINOR;

// template for screen modes
struct SurfaceTemplate {
Expand Down
3 changes: 3 additions & 0 deletions 32blit/engine/api_version.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma once
#define BLIT_API_VERSION_MAJOR 0
#define BLIT_API_VERSION_MINOR 2
21 changes: 13 additions & 8 deletions firmware/firmware.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ static bool parse_file_header(FIL &fh, BlitGameHeader &header, uint32_t &header_
// read header
f_read(&fh, &header, sizeof(header), &bytes_read);

if(header.magic != blit_game_magic)
if(header.magic != blit_game_magic || (header.device_id != BlitDevice::STM32H7_32BlitOld && header.device_id != BlitDevice::STM32H7_32Blit))
return false;

return true;
Expand Down Expand Up @@ -196,7 +196,7 @@ static bool read_flash_game_header(uint32_t offset, BlitGameHeader &header) {
if(qspi_read_buffer(offset, reinterpret_cast<uint8_t *>(&header), sizeof(header)) != QSPI_OK)
return false;

if(header.magic != blit_game_magic)
if(header.magic != blit_game_magic || (header.device_id != BlitDevice::STM32H7_32BlitOld && header.device_id != BlitDevice::STM32H7_32Blit))
return false;

// make sure end/size is sensible
Expand Down Expand Up @@ -589,13 +589,18 @@ static CanLaunchResult can_launch(const char *path) {
if(res != FR_OK)
return CanLaunchResult::InvalidFile;

if(parse_file_header(file, header, header_offset)) {
f_close(&file);
return CanLaunchResult::Success;
}

bool parsed_header = parse_file_header(file, header, header_offset);
f_close(&file);
return CanLaunchResult::IncompatibleBlit;

// failed to parse, or wrong device id
if(!parsed_header || (header.device_id != BlitDevice::STM32H7_32BlitOld && header.device_id != BlitDevice::STM32H7_32Blit))
return CanLaunchResult::IncompatibleBlit;

// check API version (unless old header)
if(header.device_id == BlitDevice::STM32H7_32Blit && (header.api_version_major != BLIT_API_VERSION_MAJOR || header.api_version_minor > BLIT_API_VERSION_MINOR))
return CanLaunchResult::IncompatibleBlit;

return CanLaunchResult::Success;
}

// not a blit file, so we need to check for handlers
Expand Down
14 changes: 13 additions & 1 deletion launcher-shared/executable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ using BlitTickFunction = uint32_t;
using BlitInitFunction = uint32_t;
#endif

enum class BlitDevice : uint8_t {
STM32H7_32BlitOld = 0, // 32blit hw, old header
STM32H7_32Blit = 1, // 32blit hw
RP2040 = 2, // any RP2040-based device
};

// should match the layout in startup_user.s
struct BlitGameHeader {
uint32_t magic;
Expand All @@ -22,7 +28,13 @@ struct BlitGameHeader {
BlitInitFunction init;

uint32_t end;
uint32_t start;

BlitDevice device_id;
uint8_t unused[3];

// if device_id != 0
uint16_t api_version_major;
uint16_t api_version_minor;
};

// missing the "BLITMETA" header and size
Expand Down
17 changes: 13 additions & 4 deletions launcher/launcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,8 @@ static void load_file_list(const std::string &directory) {
return true;
}

return res == CanLaunchResult::Success;
// will filter incompatible later
return res == CanLaunchResult::Success || res == CanLaunchResult::IncompatibleBlit;
});

game_list.reserve(files.size()); // worst case
Expand All @@ -189,6 +190,7 @@ static void load_file_list(const std::string &directory) {

if(ext == "blit") {
game.type = GameType::game;
game.can_launch = api.can_launch(game.filename.c_str()) == CanLaunchResult::Success;

// check for metadata
BlitGameMetadata meta;
Expand Down Expand Up @@ -546,8 +548,10 @@ static void render_screenshot() {

static void render_game_info() {
// run game / launch file
screen.sprite(1, Point(game_actions_offset.x, game_actions_offset.y + 12));
screen.sprite(0, Point(game_actions_offset.x + 10, game_actions_offset.y + 12), SpriteTransform::R90);
if(selected_game.can_launch) {
screen.sprite(1, Point(game_actions_offset.x, game_actions_offset.y + 12));
screen.sprite(0, Point(game_actions_offset.x + 10, game_actions_offset.y + 12), SpriteTransform::R90);
}

// game info
if(selected_game_metadata.splash)
Expand All @@ -572,6 +576,11 @@ static void render_game_info() {
char buf[20];
snprintf(buf, 20, "%i block%s", num_blocks, num_blocks == 1 ? "" : "s");
screen.text(buf, minimal_font, Point(game_info_offset.x, screen.bounds.h - 16));

if(!selected_game.can_launch) {
screen.pen = {255, 0, 0};
screen.text("INCOMPATIBLE", minimal_font, Point(screen.bounds.w - 10, screen.bounds.h - 16), true, TextAlign::top_right);
}
}

void render(uint32_t time) {
Expand Down Expand Up @@ -767,7 +776,7 @@ void update(uint32_t time) {
if(button_a) {
if(selected_game.type == GameType::screenshot && !selected_game.can_launch) {
current_screen = Screen::screenshot;
} else {
} else if(selected_game.can_launch) {
if(!launch_current_game())
dialog.show("Error!", "Failed to launch " + selected_game.filename, [](bool){}, false);
}
Expand Down

0 comments on commit b43a2e3

Please sign in to comment.