Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Launcher refactoring #804

Merged
merged 19 commits into from
May 17, 2023
Merged

Conversation

Daft-Freak
Copy link
Collaborator

Started off trying to run the launcher on pico things. Then got lost in the launcher code so this happened.

A few of these are bug fixes (most importantly 2b84bd2). First four are entirely for squishing the launcher onto a picosystem.

Now I need to make scan_flash go away... (Shouldn't be too hard, firmware already has a list of where things are for other reasons)

Mostly in the dir list. This caused some invalid clip rects
Fits on a picosystem now, a bit small though. Someone should make a launcher for small screens...
This puts all the *current_game* funcs together and also load_file/directory_list
Can result in a crash if flash is empty and the last launched file was missing
This has been broken since the launcher split... but it's not possible to hit since the launcher itself will always be in the list
It was never set, so it did nothing. load_current_game_metadata is only called on init or if the selected file changes anyway
@Gadgetoid
Copy link
Contributor

👀

Lots of very interesting (and much needed) tweaks!

@Daft-Freak
Copy link
Collaborator Author

Now I need to make scan_flash go away... (Shouldn't be too hard, firmware already has a list of where things are for other reasons)

Huh, really not that bad:

diff --git a/32blit/engine/api_private.hpp b/32blit/engine/api_private.hpp
index fa6dd034..88599e9c 100644
--- a/32blit/engine/api_private.hpp
+++ b/32blit/engine/api_private.hpp
@@ -128,6 +128,9 @@ namespace blit {
     bool (*set_raw_cdc_enabled)(bool enabled);
     void (*cdc_write)(const uint8_t *data, uint16_t len);
     uint16_t (*cdc_read)(uint8_t *data, uint16_t len);
+
+    // another launcher API
+    void (*list_installed_games)(std::function<void(const uint8_t *, uint32_t, uint32_t)> callback);
   };
   #pragma pack(pop)
 
diff --git a/firmware/firmware.cpp b/firmware/firmware.cpp
index 2bcd9570..87af67ec 100644
--- a/firmware/firmware.cpp
+++ b/firmware/firmware.cpp
@@ -545,6 +545,11 @@ static void *get_type_handler_metadata(const char *filetype) {
   return nullptr;
 }
 
+static void list_installed_games(std::function<void(const uint8_t *, uint32_t, uint32_t)> callback) {
+  for(auto &game : game_list)
+    callback((const uint8_t *)(qspi_flash_address + game.offset), game.offset / qspi_flash_sector_size, game.size);
+}
+
 static const uint8_t *flash_to_tmp(const std::string &filename, uint32_t &size) {
   // one file at a time
   // TODO: this could be improved
@@ -630,6 +635,8 @@ void init() {
   api.flash_to_tmp = flash_to_tmp;
   api.tmp_file_closed = tmp_file_closed;
 
+  api.list_installed_games = list_installed_games;
+
   scan_flash();
   flash_scanned = true;
 
diff --git a/launcher/launcher.cpp b/launcher/launcher.cpp
index 4fb50a1b..0de7656f 100644
--- a/launcher/launcher.cpp
+++ b/launcher/launcher.cpp
@@ -342,31 +342,11 @@ void init_lists() {
 }
 
 void scan_flash() {
-#ifdef TARGET_32BLIT_HW
-  const uint32_t qspi_flash_sector_size = 64 * 1024;
-  const uint32_t qspi_flash_size = 32768 * 1024;
-  const uint32_t qspi_flash_address = 0x90000000;
-
-  for(uint32_t offset = 0; offset < qspi_flash_size;) {
-    auto header = *(BlitGameHeader *)(qspi_flash_address + offset);
-
-    if(header.magic != blit_game_magic) {
-      offset += qspi_flash_sector_size;
-      continue;
-    }
-
-    uint32_t size = header.end - qspi_flash_address;
-
-    // tiny bit of metadata parsing just to get the size
-    auto buf = (char *)(qspi_flash_address + offset + size);
-    if(memcmp(buf, "BLITMETA", 8) == 0)
-      size += *(uint16_t *)(buf + 8) + 10;
-
-    File::add_buffer_file("flash:/" + std::to_string(offset / qspi_flash_sector_size) + ".blit", (uint8_t *)(qspi_flash_address + offset), size);
-
-    offset += calc_num_blocks(size) * qspi_flash_sector_size;
+  if(api.list_installed_games) {
+    api.list_installed_games([](const uint8_t *ptr, uint32_t block, uint32_t size){
+      File::add_buffer_file("flash:/" + std::to_string(block) + ".blit", ptr, size);
+    });
   }
-#endif
 }
 
 void delete_current_game() {

(wrote it on the wrong branch though)

This should replace the hacky code in the launcher. The firmware already has the info for this.

Returns block index as well as a pointer/size for the flash:/[block index] support in the launch API
Chunk of device specific code gone
@Daft-Freak
Copy link
Collaborator Author

Okay device-specific code gone, but... api.erase_game takes an offset in bytes, api.launch has a special case for flash:/[offset in blocks] sigh

The new list_installed_games API returns the offset in blocks, existing code in the launcher multiplies it by the block size for erase. Should bump the API minor version here... but if we don't the worst that happens is that new launchers have an empty installed list on old firmware. Also I forgot to bump it for the previous changes, whoops.

@Daft-Freak
Copy link
Collaborator Author

I think this just needs a minor API version bump and maybe some testing.

Can try to sort out the existing API offset inconsistency later...

@Gadgetoid
Copy link
Contributor

I think it's reasonable to merge this and go from there. I did a cursory firmware/launcher update and things seem to still work as expected. Thank you!!

@Gadgetoid Gadgetoid merged commit 619413a into 32blit:master May 17, 2023
@Daft-Freak Daft-Freak deleted the launcher-refactoring branch May 17, 2023 13:01
@Daft-Freak
Copy link
Collaborator Author

... and I didn't do the version bump again, whoops.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants