OpenSNES
Modern Open-Source SNES Development SDK
Loading...
Searching...
No Matches
vramqueue.h File Reference

A queue of VRAM uploads, filled during the frame and sent in VBlank. More...

#include <snes/types.h>

Go to the source code of this file.

Macros

#define VRAM_QUEUE_COLUMN   0x81
 Step after each word: 32 words on (a column of a 32-wide tilemap).
#define VRAM_QUEUE_ENTRY_COST   80
 What one entry costs the flush beyond its bytes, in bytes.
#define VRAM_QUEUE_MAX   32
 Transfers the queue holds.
#define VRAM_QUEUE_ROW   0x80
 Step after each word: the next VRAM word (a row, tiles).
#define VRAM_QUEUE_UNTIL_COST   64
 Bytes of the budget vramQueueFlushUntil() spends before its first transfer.
#define vramQueueCost()
 VBlank cost of what is queued, in bytes: each entry's size plus VRAM_QUEUE_ENTRY_COST.
#define vramQueueFree()
 Entries still free.

Functions

void vramQueueFlush (void)
 Send every queued transfer to VRAM and empty the queue.
u16 vramQueueFlushUntil (u16 line)
 Send the queued transfers that end before a scanline; keep the rest.
u16 vramQueuePush (const u8 *src, u16 addr, u16 size, u16 step)
 Note one transfer for the next vramQueueFlush().
u16 vramQueuePushSprite (const u8 *src, u16 addr, u16 size_px)
 Note one frame of a streamed sprite: all its strips in one call.
u16 vramQueuePushSpriteWithin (const u8 *src, u16 addr, u16 size_px, u16 budget)
 vramQueuePushSprite(), if the whole frame fits a budget you give
u16 vramQueuePushWithin (const u8 *src, u16 addr, u16 size, u16 step, u16 budget)
 vramQueuePush(), if it fits a budget you give: the guard written once

Variables

u16 vram_queue_addr [32]
u16 vram_queue_bank [32]
u16 vram_queue_cost
u16 vram_queue_count
u16 vram_queue_cum [32]
u16 vram_queue_refused
 Pushes refused since power-on (queue full): 0 on a healthy run. Counted whether or not the caller reads the return value; a test reads it from WRAM. Wraps at 65536.
u16 vram_queue_size [32]
u16 vram_queue_src [32]
u16 vram_queue_step [32]

Detailed Description

A queue of VRAM uploads, filled during the frame and sent in VBlank.

A game that streams graphics during play — sprite frames, the rows and columns of a scrolling map — makes several small VRAM transfers in each VBlank. One dmaCopyVram() call per transfer costs about as much to set up from C as it does to run for 128 bytes, and the VBlank is short. Here a transfer is noted while the frame is computed, when time is not short, and all of them are sent in VBlank by one assembly routine:

#include <snes/vramqueue.h> // and `vramqueue` in LIB_MODULES
// during the frame, wherever the game decides something must reach VRAM
if (vramQueueFree() >= 4) {
vramQueuePush(frame_tiles, 0x0000, 128, VRAM_QUEUE_ROW);
vramQueuePush(frame_tiles + 512, 0x0100, 128, VRAM_QUEUE_ROW);
// ...
}
vramQueueFlush(); // first thing in VBlank
void WaitForVBlank(void)
Wait for next VBlank period.
A queue of VRAM uploads, filled during the frame and sent in VBlank.
#define VRAM_QUEUE_ROW
Step after each word: the next VRAM word (a row, tiles).
Definition vramqueue.h:61
#define vramQueueFree()
Entries still free.
Definition vramqueue.h:80
u16 vramQueuePush(const u8 *src, u16 addr, u16 size, u16 step)
Note one transfer for the next vramQueueFlush().
void vramQueueFlush(void)
Send every queued transfer to VRAM and empty the queue.

What it buys is VBlank time, not time. Noting an entry is a call too, so in total the queue costs more than the dmaCopyVram() calls it replaces. But six 128-byte transfers called directly put 15,900 master cycles in the VBlank, and queued only 10,600 — 8 per byte plus about 740 per entry, the call to the flush included (devtools/libbench, rows vramc and vramq).

The budget is yours to keep. A VBlank is about 49,000 master cycles and the NMI handler takes the first 5,000 to 8,000 of them. What does not fit lands in active display, where the PPU ignores it: no error, missing tiles. vramQueueCost() says what the entries already queued will take, whichever module queued them.

Request and first user: issue #165.

Macro Definition Documentation

◆ VRAM_QUEUE_COLUMN

#define VRAM_QUEUE_COLUMN   0x81

Step after each word: 32 words on (a column of a 32-wide tilemap).

◆ VRAM_QUEUE_ENTRY_COST

#define VRAM_QUEUE_ENTRY_COST   80

What one entry costs the flush beyond its bytes, in bytes.

The flush spends 8 master cycles per byte and, in its loop, 619 per entry (SlowROM; 560 with USE_FASTROM): 619 / 8 is 77 bytes' worth, rounded up.

◆ VRAM_QUEUE_MAX

#define VRAM_QUEUE_MAX   32

Transfers the queue holds.

32 unless the project's Makefile says otherwise (VRAM_QUEUE_MAX := 64, 1 to 255): the build gives the C files this value and sizes the queue's arrays to it (templates/vramqueue_ram.asm). Each entry takes 12 bytes of the C RAM band ($0000-$1FFF). vramQueueFlush() sends every entry in one call; vramQueueFlushUntil() sends what ends before a given scanline.

◆ VRAM_QUEUE_ROW

#define VRAM_QUEUE_ROW   0x80

Step after each word: the next VRAM word (a row, tiles).

◆ VRAM_QUEUE_UNTIL_COST

#define VRAM_QUEUE_UNTIL_COST   64

Bytes of the budget vramQueueFlushUntil() spends before its first transfer.

◆ vramQueueCost

#define vramQueueCost ( )
Value:
u16 vram_queue_cost

VBlank cost of what is queued, in bytes: each entry's size plus VRAM_QUEUE_ENTRY_COST.

Kept by vramQueuePush() and vramQueuePushSprite(), set to 0 by vramQueueFlush(), to the cost of what is left by vramQueueFlushUntil(); it stops at 0xFFFF rather than wrap. Multiply by 8 for master cycles. A module that streams what is left of the VBlank checks it against its own budget before each push:

// a 32x32 frame: four strips of 128 bytes
while (next < wanted && vramQueueCost() + 512 + 4 * VRAM_QUEUE_ENTRY_COST <= BUDGET) {
vramQueuePushSprite(frame_src[next], frame_dst[next], 32);
next++;
}
#define VRAM_QUEUE_ENTRY_COST
What one entry costs the flush beyond its bytes, in bytes.
Definition vramqueue.h:88
u16 vramQueuePushSprite(const u8 *src, u16 addr, u16 size_px)
Note one frame of a streamed sprite: all its strips in one call.
#define vramQueueCost()
VBlank cost of what is queued, in bytes: each entry's size plus VRAM_QUEUE_ENTRY_COST.
Definition vramqueue.h:111

A count written by hand (vram_queue_count = 0) leaves it as it was until the next flush.

◆ vramQueueFree

#define vramQueueFree ( )
Value:
#define VRAM_QUEUE_MAX
Transfers the queue holds.
Definition vramqueue.h:57
u16 vram_queue_count

Entries still free.

Function Documentation

◆ vramQueueFlush()

void vramQueueFlush ( void )

Send every queued transfer to VRAM and empty the queue.

One DMA per entry, on channel 0. Call it in VBlank — right after WaitForVBlank() — or in force blank: it writes VRAM. With an empty queue it returns at once. VMAIN is left at $80, as the rest of the library leaves it.

Warning
Not from an NMI callback while the main code may be in the middle of a vramQueuePush(): the entry would be half written.

◆ vramQueueFlushUntil()

u16 vramQueueFlushUntil ( u16 line)

Send the queued transfers that end before a scanline; keep the rest.

Reads the vertical counter once, turns the lines left before line into a budget (165 bytes of vramQueueCost() a line: 1324 master cycles, the DRAM refresh taken out), and sends the entries in queue order while their running cost fits. The first one that does not fit stops it: the entries left move to the head of the queue in the same order and go first at the next flush, so a VRAM address pushed twice still receives the second transfer last. Use it to fill every VBlank whatever the NMI took, and push beyond a safe budget.

  • The vertical blank ends after line 261 (NTSC) or 311 (PAL): pass 262 or 312 to use all of it, less to keep a margin. A blank begins at line 225, or 240 with overscan.
  • Outside the vertical blank (HVBJOY bit 7 clear) it sends nothing: a flush called late, in a lag frame, does not write into active display. In force blank use vramQueueFlush().
  • What is left stays queued: its sources are read at a later flush, so a RAM buffer behind a kept entry must not change until then (ROM is safe). vramQueueFree() counts the entries left as taken, and vramQueueCost() is their cost.
  • The routine itself costs VRAM_QUEUE_UNTIL_COST bytes of the budget up to its first transfer, whatever the queue's length (the cut is found by halving). Moving what is left takes time after the blank, not in it.
  • It reads the H/V counter: a Super Scope or light-gun latch read later in the same frame sees this one.
Parameters
lineScanline by whose start the transfers must be over
Returns
Entries left in the queue
Warning
Not from an NMI callback while the main code may be in the middle of a vramQueuePush().

◆ vramQueuePush()

u16 vramQueuePush ( const u8 * src,
u16 addr,
u16 size,
u16 step )

Note one transfer for the next vramQueueFlush().

Parameters
srcSource bytes (ROM, plain RAM or FAR RAM) The queue keeps the POINTER: the bytes are read at the flush, not here. They must not change before vramQueueFlush(); a buffer filled again before the flush sends its last contents for every entry that points to it.
addrVRAM word address
sizeBytes (even; an entry of 0 bytes is skipped by the flush)
stepVRAM_QUEUE_ROW or VRAM_QUEUE_COLUMN
Returns
1, or 0 when the queue is full: nothing was noted

◆ vramQueuePushSprite()

u16 vramQueuePushSprite ( const u8 * src,
u16 addr,
u16 size_px )

Note one frame of a streamed sprite: all its strips in one call.

A sprite frame in OBJ VRAM is size_px / 8 rows of tiles; each row is a strip of size_px * 4 bytes (at 4 bpp), and the rows are 16 tiles apart: 512 bytes in the sheet, 256 words in VRAM. That is the layout opensnes-sprite sheet writes (a 128-pixel-wide raster) and the one OBJ VRAM expects. This notes the 1, 2, 4 or 8 strips of one frame.

Parameters
srcFirst byte of the frame in the sheet (its top-left tile). The frame must not straddle a bank boundary. Read at the flush, like vramQueuePush(): the bytes must not change before vramQueueFlush().
addrVRAM word address of the frame's top-left tile
size_pxWidth and height of the sprite: 8, 16, 32 or 64
Returns
1, or 0 when the strips do not all fit in the queue (none is noted) or size_px is under 8

◆ vramQueuePushSpriteWithin()

u16 vramQueuePushSpriteWithin ( const u8 * src,
u16 addr,
u16 size_px,
u16 budget )

vramQueuePushSprite(), if the whole frame fits a budget you give

Notes every strip of the frame only when vramQueueCost() + (size_px / 8)

and all the strips fit in the queue (288 for a 16x16 frame, 832 for a 32x32 one). Otherwise returns 0 and changes nothing, and does not count a refusal (vram_queue_refused is for a queue that was full, not for a frame that waits for the next one): that is what sets it apart from vramQueuePushSprite(), which counts its refusals. A sum past $FFFF is a refusal, never a wrap. The budget is yours: deciding what each module may spend stays with the game; this only does the test and the push together.

// before: the guard by hand, every time
if (vramQueueCost() + 2 * (64 + VRAM_QUEUE_ENTRY_COST) <= BUDGET
&& vramQueueFree() >= 2)
// after
if (!vramQueuePushSpriteWithin(frame, addr, 16, BUDGET))
retry_next_frame = 1;
u16 frame
Definition main.c:94
u16 vramQueuePushSpriteWithin(const u8 *src, u16 addr, u16 size_px, u16 budget)
vramQueuePushSprite(), if the whole frame fits a budget you give
Parameters
src,addr,size_pxAs vramQueuePushSprite()
budgetMost the queue may cost, in vramQueueCost()'s unit (bytes + VRAM_QUEUE_ENTRY_COST an entry)
Returns
1 if every strip was noted, 0 if the frame did not fit (nothing noted) or size_px is under 8

◆ vramQueuePushWithin()

u16 vramQueuePushWithin ( const u8 * src,
u16 addr,
u16 size,
u16 step,
u16 budget )

vramQueuePush(), if it fits a budget you give: the guard written once

Notes the transfer only when vramQueueCost() + size + VRAM_QUEUE_ENTRY_COST <= budget and the queue has a free entry. Otherwise returns 0 and changes nothing, and does not count a refusal (vram_queue_refused is for a queue that was full, not for a transfer that waits for the next frame). The budget is yours: deciding what each module may spend stays with the game; this only does the test and the push together.

// before: the guard by hand, every time
if (vramQueueFree() && vramQueueCost() + 128 + VRAM_QUEUE_ENTRY_COST <= BUDGET)
vramQueuePush(row, addr, 128, VRAM_QUEUE_ROW);
// after
if (!vramQueuePushWithin(row, addr, 128, VRAM_QUEUE_ROW, BUDGET))
retry_next_frame = 1;
u16 vramQueuePushWithin(const u8 *src, u16 addr, u16 size, u16 step, u16 budget)
vramQueuePush(), if it fits a budget you give: the guard written once
Parameters
src,addr,size,stepAs vramQueuePush()
budgetMost the queue may cost, in vramQueueCost()'s unit (bytes + VRAM_QUEUE_ENTRY_COST an entry)
Returns
1 if noted, 0 if it did not fit

Variable Documentation

◆ vram_queue_addr

u16 vram_queue_addr[32]
extern

◆ vram_queue_bank

u16 vram_queue_bank[32]
extern

◆ vram_queue_cost

u16 vram_queue_cost
extern

◆ vram_queue_count

u16 vram_queue_count
extern

◆ vram_queue_cum

u16 vram_queue_cum[32]
extern

◆ vram_queue_refused

u16 vram_queue_refused
extern

Pushes refused since power-on (queue full): 0 on a healthy run. Counted whether or not the caller reads the return value; a test reads it from WRAM. Wraps at 65536.

◆ vram_queue_size

u16 vram_queue_size[32]
extern

◆ vram_queue_src

u16 vram_queue_src[32]
extern

◆ vram_queue_step

u16 vram_queue_step[32]
extern