OpenSNES
Modern Open-Source SNES Development SDK
Loading...
Searching...
No Matches
main.c File Reference

LZ77 decompression into RAM, screen on: lzssDecodeRam. More...

#include <snes.h>
#include <snes/lzss.h>
#include "res/tiles.inc"
#include "res/tiles_lz.inc"

Macros

#define BANK7F(addr)
 Bank $7F: the tiles at $7F:0000, the hand-built streams from $7F:9000.
#define FILL   0xA5
 What every destination holds before a decode.
#define GUARD   16
 Bytes checked after each output: the decoder must not write past its end.
#define TILES_SIZE   32768
 Size of the tile block, as converted without compression.

Functions

static void bench (u8 which)
 One decode alone, for luna profile (see the README).
static u16 diff (const u8 *d, const u8 *ref, u16 n)
 Bytes of d[0..n) that differ from ref[0..n).
static u16 diff_pattern (const u8 *d, u16 n, u16 period)
 Bytes of d[0..n) that differ from P(k mod period), P(k) = k * 37 + 11.
static void fill (u8 FAR *d, u16 n)
 Fill n bytes at d with FILL.
int main (void)
 Run every test with the screen on, then show the verdicts.
static void show (u8 row, const char *name, u16 len, u16 bad)
 Print one result line: name, bytes returned, verdict.
static u16 test_ovl (u8 FAR *d, u16 *len)
 Decode the overlap stream into d (44 bytes).
static u16 test_ovlong (u8 FAR *d, u16 *len)
 Decode the long overlap stream into d: 127 x 'A', 55 x "BC", 31 x "DEF".
static u16 test_refused (const u8 *s, u8 FAR *d, u16 *len)
 Decode a stream that must be refused: 0 back, nothing written.
static u16 test_tiles (u8 FAR *d, u16 *len)
 Decode the tiles into d; check the bytes and the guard after them.
static u16 touched (const u8 *d, u16 n)
 Bytes of d[0..n) that are not FILL: what a refused decode or an overrun touched.

Variables

u8 bench_sel
 Set by luna (–poke-at main –poke bench_sel=0N) to time one decode alone.
FAR u8 buf7e [32768+16]
 Bank $7E destination: a FAR buffer, outside the 8 KB band.
u8 buf_low [330+16]
 Low-RAM destination (bank $00): the decoder's byte-loop copy.
Hand-built streams (streams.asm)
const u8 big_lz []
const u8 clip_lz []
const u8 lit_lz []
const u8 ovl_expected []
const u8 ovl_lz []
const u8 ovlong_lz []
const u8 ref194_lz []
const u8 ref_lz []
const u8 tag_lz []
const u8 zero_lz []
Results, read by testing/manifests/memory_lzss_ram.toml

For each test: r_X_len = the return value, r_X_bad = bytes that differ from the expectation, guard bytes included (0 = pass).

u16 r_big_bad
u16 r_big_len
u16 r_clip_bad
u16 r_clip_len
u16 r_cross_bad
u16 r_cross_len
u16 r_done
u16 r_edge_bad
u16 r_edge_len
u16 r_failed
u16 r_lit_bad
u16 r_lit_len
u16 r_ovl7e_bad
u16 r_ovl7e_len
u16 r_ovl7f_bad
u16 r_ovl7f_len
u16 r_ovllo_bad
u16 r_ovllo_len
u16 r_ovlong7e_bad
u16 r_ovlong7e_len
u16 r_ovlong7f_bad
u16 r_ovlong7f_len
u16 r_ovlonglo_bad
u16 r_ovlonglo_len
u16 r_r194_bad
u16 r_r194_len
u16 r_ref_bad
u16 r_ref_len
u16 r_t7e_bad
u16 r_t7e_len
u16 r_t7f_bad
u16 r_t7f_len
u16 r_tag_bad
u16 r_tag_len
u16 r_zero_bad
u16 r_zero_len

Detailed Description

LZ77 decompression into RAM, screen on: lzssDecodeRam.

lzssDecodeVram expands a compressed block straight into VRAM, which needs forced blank. lzssDecodeRam expands it into RAM instead, in any bank, with the screen on and the NMI running: the game decompresses its tiles while it plays, then sends them to VRAM by DMA in VBlank.

This example decodes, with the screen on, a 2 bpp tile block that opensnes-sprite compressed (32768 bytes from 17856) into bank $7F and into a FAR buffer of bank $7E, and compares each copy byte for byte with the same tiles converted without compression. Hand-built streams then check the edge cases: literals only, long references (also into the low 8 KB, the byte-loop copy), a reference cut at the end of the output, overlapping references (distance 1, distance shorter than the length) into $7F, $7E and the low 8 KB, a stream that ends exactly at the end of bank $7F, and four refused streams (length 0, length above 65535, a wrong tag, an output that would cross the end of the bank), each of which must return 0 and leave its destination as it was. The verdict of each test is in WRAM, for the luna manifest, and on screen.

SNES Concepts
  • LZ77 (type 0x10) decompression: flag bytes, literals, back references
  • The 128 KB of work RAM: bank $7E (FAR objects) and bank $7F
  • MVN block moves, and why an overlapping move repeats a pattern
  • Decompressing with the screen on: no PPU access, NMI left enabled
What to Observe
  • One line per test: its name, the bytes returned, OK or BAD
  • ALL OK at the bottom when every test passed
Modules Used
console, dma, text, background, sprite, lzss
See also
lzss.h, text.h

Macro Definition Documentation

◆ BANK7F

#define BANK7F ( addr)
Value:
((u8 FAR *)(0x7F0000UL + (addr)))
unsigned char u8
8-bit unsigned integer (0 to 255)
Definition types.h:47
#define FAR
Place a global in bank $7E WRAM, outside the 8 KB bank-0 band.
Definition types.h:130

Bank $7F: the tiles at $7F:0000, the hand-built streams from $7F:9000.

◆ FILL

#define FILL   0xA5

What every destination holds before a decode.

◆ GUARD

#define GUARD   16

Bytes checked after each output: the decoder must not write past its end.

◆ TILES_SIZE

#define TILES_SIZE   32768

Size of the tile block, as converted without compression.

Function Documentation

◆ bench()

void bench ( u8 which)
static

One decode alone, for luna profile (see the README).

◆ diff()

u16 diff ( const u8 * d,
const u8 * ref,
u16 n )
static

Bytes of d[0..n) that differ from ref[0..n).

◆ diff_pattern()

u16 diff_pattern ( const u8 * d,
u16 n,
u16 period )
static

Bytes of d[0..n) that differ from P(k mod period), P(k) = k * 37 + 11.

◆ fill()

void fill ( u8 FAR * d,
u16 n )
static

Fill n bytes at d with FILL.

◆ main()

int main ( void )

Run every test with the screen on, then show the verdicts.

◆ show()

void show ( u8 row,
const char * name,
u16 len,
u16 bad )
static

Print one result line: name, bytes returned, verdict.

◆ test_ovl()

u16 test_ovl ( u8 FAR * d,
u16 * len )
static

Decode the overlap stream into d (44 bytes).

◆ test_ovlong()

u16 test_ovlong ( u8 FAR * d,
u16 * len )
static

Decode the long overlap stream into d: 127 x 'A', 55 x "BC", 31 x "DEF".

◆ test_refused()

u16 test_refused ( const u8 * s,
u8 FAR * d,
u16 * len )
static

Decode a stream that must be refused: 0 back, nothing written.

◆ test_tiles()

u16 test_tiles ( u8 FAR * d,
u16 * len )
static

Decode the tiles into d; check the bytes and the guard after them.

◆ touched()

u16 touched ( const u8 * d,
u16 n )
static

Bytes of d[0..n) that are not FILL: what a refused decode or an overrun touched.

Variable Documentation

◆ bench_sel

u8 bench_sel

Set by luna (–poke-at main –poke bench_sel=0N) to time one decode alone.

◆ big_lz

const u8 big_lz[]
extern

length 65536

◆ buf7e

FAR u8 buf7e[32768+16]

Bank $7E destination: a FAR buffer, outside the 8 KB band.

◆ buf_low

u8 buf_low[330+16]

Low-RAM destination (bank $00): the decoder's byte-loop copy.

◆ clip_lz

const u8 clip_lz[]
extern

a reference that runs past the 5-byte end

◆ lit_lz

const u8 lit_lz[]
extern

4096 literals P(k)

◆ ovl_expected

const u8 ovl_expected[]
extern

what ovl_lz expands to

◆ ovl_lz

const u8 ovl_lz[]
extern

overlapping references, 44 bytes

◆ ovlong_lz

const u8 ovlong_lz[]
extern

overlapping references, 330 bytes (fast loop)

◆ r_big_bad

u16 r_big_bad

◆ r_big_len

u16 r_big_len

◆ r_clip_bad

u16 r_clip_bad

◆ r_clip_len

u16 r_clip_len

◆ r_cross_bad

u16 r_cross_bad

◆ r_cross_len

u16 r_cross_len

◆ r_done

u16 r_done

0xD0E5 once every test ran

◆ r_edge_bad

u16 r_edge_bad

◆ r_edge_len

u16 r_edge_len

◆ r_failed

u16 r_failed

tests that failed

◆ r_lit_bad

u16 r_lit_bad

◆ r_lit_len

u16 r_lit_len

◆ r_ovl7e_bad

u16 r_ovl7e_bad

◆ r_ovl7e_len

u16 r_ovl7e_len

◆ r_ovl7f_bad

u16 r_ovl7f_bad

◆ r_ovl7f_len

u16 r_ovl7f_len

◆ r_ovllo_bad

u16 r_ovllo_bad

◆ r_ovllo_len

u16 r_ovllo_len

◆ r_ovlong7e_bad

u16 r_ovlong7e_bad

◆ r_ovlong7e_len

u16 r_ovlong7e_len

◆ r_ovlong7f_bad

u16 r_ovlong7f_bad

◆ r_ovlong7f_len

u16 r_ovlong7f_len

◆ r_ovlonglo_bad

u16 r_ovlonglo_bad

◆ r_ovlonglo_len

u16 r_ovlonglo_len

◆ r_r194_bad

u16 r_r194_bad

◆ r_r194_len

u16 r_r194_len

◆ r_ref_bad

u16 r_ref_bad

◆ r_ref_len

u16 r_ref_len

◆ r_t7e_bad

u16 r_t7e_bad

◆ r_t7e_len

u16 r_t7e_len

◆ r_t7f_bad

u16 r_t7f_bad

◆ r_t7f_len

u16 r_t7f_len

◆ r_tag_bad

u16 r_tag_bad

◆ r_tag_len

u16 r_tag_len

◆ r_zero_bad

u16 r_zero_bad

◆ r_zero_len

u16 r_zero_len

◆ ref194_lz

const u8 ref194_lz[]
extern

the same, 194 bytes: 32 literals, 9 references

◆ ref_lz

const u8 ref_lz[]
extern

32 literals, then 224 references of 18 bytes at distance 32

◆ tag_lz

const u8 tag_lz[]
extern

tag $11

◆ zero_lz

const u8 zero_lz[]
extern

length 0