Log In  

I've been working on a table to binary converter that can help compress data and store it in cart rom. I know there have been different attempts in the past, but this version aims to fit only the necessary data and leaves the types to a format string. Though it's only really useful if you have some of the following.

  • A lot of data in tables
  • A consistent and known table format
  • A variety of table formats
  • Multiple carts

It works by taking in a table, a writing address, and a format, that generally look like this

tab = {1,2}
form = "[#8,#8]" -- an indexed array of 2 unsigned 8-bit integers
-- form = "[#4,#3]" -- also works with a different number of bits (up to 16)
tab2bin(tab, 0x8000, form) -- compress table to binary at 0x8000
tab2 = bin2tab(0x8000, form) -- decompress table

Though if a table can a varying number of entries, so it instead it would be better to loop through entries.

tab = {1,2,4,8,16,32,64,128}
form = "[#8(#8)]" -- can have up to 255 entries due to looping through (#8) a number of times read from the previous #8

There are also ways to store values that are expected to have decimals or be in a different range

tab = {-10, 1.25, 0x1234.5678}
form = "[#8-128,#8>4,#16>16@dec#16+dec]"
-- "-" subtracts from the read value 
-- ">" shifts the read value right
-- #16>16@dec#16+dec reads the first 16 bits and last 16 bits of a full fixed-point number and stores them in 32 bits

There's also support for strings, booleans, tables with keys(though not mixing them with indexed tables), and custom compression/decompression functions.

The table to binary (tab2bin) compression function is about 913 tokens. While the binary to table (bin2tab) decompression function is about 536 tokens. However, some of the lines of code can be removed to reduce the token count. I would recommend storing your data in a string format initially, only using tab2bin when packing the data for the final carts, and only using bin2tab in the final carts.


The full source and more details are on the github page, since there may be changes in the future. Feel free to send any issues or pull requests to help improve it.

P#139161 2023-12-25 15:54

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-04-18 23:00:44 | 0.005s | Q:6