aboutsummaryrefslogtreecommitdiff
path: root/doc/journal.md
blob: 86a427fbf654440fb712800a86fef384bb455122 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# 27.10.2026

Some progress on the `LOAD("#FOO")` support! It turns out that loading these kinds of carts is a 2 step process:

First, PICO-8 will first try to fetch an `.nfo` on the cart which looks like this:

```text
lid:cartid-42
mid:cartid
title:My Cool Cart
author:Me
ts:1970-01-01 00:00:00
catsub:1792
```

> I have absolutely no idea what `catsub` is, but it appears to be the same for the few carts that I tried.

Secondly, it will try to load the cart specified by `lid` from disk, and if that fails additionally use the `wget` machinery to try and download it.

A few things to note:

1. The `.nfo` doesn't have to be complete - a simple `lid:$NAME` would work and case PICO-8 to try and load `$NAME.p8.png` (PNG!) via `open()` syscall.
2. If the file exists (i.e. `open()` succeeds) it will use that instead of trying to `wget` it.

This means that supporting BBS multicarts is possible, and we can do it either:

- automatically by generating `.p8.png` files from ROMs on the fly
- requiring these kinds of multicarts have `.p8.png` files in their `.p8.dir`s
- manually setting up a `.p8.tar` files for the filesystem to include the multicarts, and have PICO-w read them in when the FS is setup (we don't read any Tar file ATM, but that _was_ the plan)

# 26.10.2026

We are able to boot! It was mostly mechanically porting the Emscripten glue code to C+SDL3, and not weird surprises. One issue I _do_ have right now is that there is no "QUIT" entry in the pause menu like there is when you export as `.BIN`; I would _love_ for this to work so that I can use this entry as a mechanism to navigate back to the cart selection menu from a running game.

I would need to look at the Wasm decompilation to see whether or not this is possible; and how to hook into it with the exports to make it happen (I may have just copied over one of the imports from Emscripten which makes it behave like the HTML exports (which also do not have a "QUIT" entry).

There other issue I have is around multicarts that load with BBS ids (so `LOAD("#FOO")`). These do not appear to search in the usual embedded cart data like for regular `LOAD("FOO.P8")` multicarts; and go over the `wget` download mechanism. My hope is that PICO-8 just fetches some BBS metadata and would be able to find the embedded cart via its digest (for context, all of the embedded carts get a `mount_embedded_cart: $NAME.p8 ($SIZE chars) $SHA256SUM` line, and on a BBS load, a `codo_wget_onerror: /user_data/bbs/carts/temp-$NAME.nfo` error after attempting to `wget /bbs/cpost_lister3.php?nfo=1&version=000207e&lid=$NAME` over ASM consts; I was under the impression that PICO-8 would cache BBS downloads, so I hope that this `.nfo` file has some metadata that would allow PICO-8 to reuse the mounted embedded cart).

The final issue is around clipboard support. It appears that POOM in particular makes use of the clipboard (maybe to send data over loaded carts?), so we should probably implement it. I don't think it would be too challenging, and would mostly revolve around keeping some clipboard data loaded in `pw_input` so that we can properly implement the ASM constants.

# 12.10.2026

Looking at the exported Emscripted glue code, it encodes the pixel format as `0x86462004` which is `SDL_PIXELFORMAT_RGBA8888` - an endian-independent pixel format. For example, the PICO-8 baby blue is encoded as:

```
[0x29, 0xad, 0xff, 0xff]
#ffffad29 r=29 g=ad b=ff a=ff
```

However, this contradicts my understanding of SDL pixel formats. From the docs:

> Names with a bit count per component, such as ARGB8888 [...], are "packed" into an appropriately-sized integer in the platform's native endianness. For example, ARGB8888 is a sequence of 32-bit integers; in each integer, the most significant bits are alpha, and the least significant bits are blue. On a little-endian CPU such as x86, the least significant bits of each integer are arranged first in memory, but on a big-endian CPU such as s390x, the most significant bits are arranged first. Use these names if the property of a format that is important to you is the meaning of each bit position within a native-endianness integer.

Which suggests, given the mask and pixel order, should be `ABGR8888` instead. Use the correct value `0x86762004` instead of just copying what the Emscripten glue code does, we can revert it in case it causes issues with the actual PICO-8 engine once we get there.

# 02.10.2026

I figured out that PICO-8 is built with Emscripten using the [SDL 1.3 built-in](https://github.com/emscripten-core/emscripten/tree/main/system/include/SDL). We can use the headers to understand memory layout of the various pieces used by PICO-8. I noticed that:

- AFAICT, it should only care about keyboard and mouse events (as joysticks are handled seprately with the "ASM constants" from JavaScript)
- It has very simple interactions, and most of the work will be stubbing the glue code related to Emscripten itself (like filesystem things) as well as the game controller and audio bridge

# 22.09.2026

We can use `wasm2c` instead of embedding a WASM runtime: it is not only faster, but easier to build and glue together.

# 21.06.2026

Rebooting development of this project. Some things I've decided to change:

- Do not use a PICO-8 boot cart, but just implement a simple game selector in SDL itself.
- Go "SDL-only", don't separate PICO-8 into a separate library; this is adding of lot of cognitive overhead and one of the reasons I shied away from working on the project in the past (I am not a C development, and had a hard time making good architectural decisions). I'm just going to stick it all in a `main.c` and :pray:.
- Update to more recent dependencies (in particular the WAMR version we use is particularly outdated).

# 24.04.2025

It looks like PICO-8 _does_ support labels with the extended colors (as a fun fact, it uses the characters `g` to `v` to encode the colors from the secret palette. This means that, unfortunately, we cannot use a pure PICO-8 boot cart, as we cannot guarantee we will be able to display all the colors necessary for a splash image.

# 21.04.2025

Implementing a cart loader in PICO-8 would be ideal - we can use the existing runtime to control everything from drawing to reading the file system. The current idea is to have special `$/` paths for loading system data (such as file lists and PNG labels for carts). There are some open questions about how this would work with the secret palette, so some additional investigation is needed.

# 20.04.2025

After some painful debugging, I figured out that `invokeNative` implementation in WAMR relied on platform and ABI specific semantics, and that `invokeNative_general.c` implementation was not portable. Instead, there is a `_raw` variant for native functions where the arguments and returns need to be manually setup and extracted, but this is portable and doesn't rely on platform specifics - so I will use this instead. Note that I stubbed out the `invokeNative` implementation so that you get runtime errors if you try and use it.

# 14.04.2025

Nothing too exciting in the way of PICO-8 development, but we did settle on a PNG parsing library (`stb`) and now support extracting PICO-8 32kB roms from `.p8.png` files. It was mostly painless and not very interesting.

# 12.04.2025

Started looking into Wasm runtimes to use, in particular ones that are portable C. The leading candidate right now is WebAssembly Micro Runtime (WAMR). Note that it even has Emscripten library support, so we have way fewer imports to implement.

# 10.04.2025

Beutifying the exported JavaScript file yields some great results. In particular, it wasn't minified and instead just stripped of whitespace, making it legible. I was able to wrap imported and exported functions of the Wasm blob with some basic logging, making the process of reverse engineering what each of the imported functions are supposed to do.

# 8.04.2025

First thing that I tested was exporting different carts and seeing what changed in the output (including exporting multi-carts). What I discovered was:

- There are no changes to the exported HTML, JS, or Wasm (beyond small things like file names and cart data)
- The `_cartdat` is packed cart data, and for multi-carts the various individual carts get packed together
- The exported JS contains a `data.pod` file that is automatically added to the fake file system which contains the PICO-8 boot sequence

Mind you, the carts that I exported weren't very varied, so it is possible that PICO-8 analyzes the cart to determine what it needs to export, but I find that unlikely.