sd2snes+ Link SDK
A SNES homebrew carries a URL, asks for it, and gets the content: a few KB of text or JSON (news, a message of the day) or megabytes of extra levels, graphics or music. The sd2snes+ Link fetches it over Wi-Fi and hands it to the game through the cartridge. The player allows it once per game, the GamesDB checks every request, and the game keeps what it got in its own memory or in its own folder on the SD card.
The SDK comes with sd2snes+ 3.0 and the Link firmware that ships with it. It was checked on the sd2snes Mk.II and the FXPAK PRO (Mk.III) with the example games. The specification is draft 3.
A minimal example
The library does the mailbox work. Call lk_detect once and lk_open for a
session, then, every frame, lk_poll and lk_recv to take the answers. A
download is an open, reads of pieces until the end (EOF), and a close. The blocks below are copied
unchanged from the SDK's files; each label names the file and the lines.
if (lk_detect() == LK_SDK_OPEN) lk_open();
lk_fetch_open(url, sizeof url - 1, 0, 0, 0, 0);
/* each frame */
while ((op = lk_recv())) {
if (op == (LK_OP_FETCH_OPEN | LK_RSP) && !lk_rsp[3]) lk_fetch_read(LK_RSP_PAYLOAD[0], 0, 16384);
else if (op == (LK_OP_FETCH_READ | LK_RSP) && !lk_rsp[3]) { /* lk_bulk_read(0, dst, len); read again unless EOF */ }
else if (lk_rsp[3]) show(lk_errstr(lk_rsp[3]));
}
Each piece lands in the bulk area of the mailbox. Copy it out
(lk_bulk_dma to WRAM, lk_bulk_vram to VRAM, lk_bulk_read
with the CPU) before you ask for the next piece on the same range.
jsl lk_detect
sta ex_detect
php
AT 3, 10, PAL_WHITE
lda ex_detect
jsr print_hex8
plp
bcc main
jsl lk_open
bcc main
lda #ST_HELLO
sta ex_step
bra main
ca65 · examples/fetch/fetch.s:177-192 · every frame: take the answers
running:
jsl lk_poll
@rx: jsl lk_recv
bcc main
cmp #LK_OP_HELLO | LK_RSP
bne :+
jsr on_hello
bra @rx
: cmp #LK_OP_FETCH_OPEN | LK_RSP
bne :+
jsr on_open
bra @rx
: cmp #LK_OP_FETCH_READ | LK_RSP
bne @rx
jsr on_read
bra @rx
ca65 · examples/fetch/fetch.s:206-222 · open a URL (a plain GET)
; open_url: X = the url's address (bank of the code), A = its length; a plain GET
open_url:
.a8
sta lk_len
stx lk_ptr
lda #^s_news
sta lk_ptr+2
stz lk_vlen ; no etag
stz lk_vlen+1
ldx #0
: stz lk_arg,x ; GET, offset 0
inx
cpx #10
bcc :-
lda #0 ; no flags
jsl lk_fetch_open
rts
ca65 · examples/fetch/fetch.s:277-289 · ask for the next piece
read_more:
.a8
ldx #0 ; the text: one piece at the start of the bulk area
; read_at: X = the bulk offset of the next piece
read_at:
.a8
lda stream
rep #$10
ldy #PIECE
jsl lk_fetch_read
bcc :+ ; the ring is full: cannot happen with these few requests
inc inflight
: rts
The same open and first read with the asar library, from its self-test ROM. At the first line A, X and Y are 16-bit (the routine before it in the file ends a KV write).
asar · lib/asar/selftest.asm:245-270 lda.w #url
sta.w !lk_ptr
stz.w !lk_vlen
stz.w !lk_arg+0
stz.w !lk_arg+2
stz.w !lk_arg+4
stz.w !lk_arg+6
stz.w !lk_arg+8
sep #$20
lda.b #url>>16
sta.w !lk_ptr+2
lda.b #url_end-url
sta.w !lk_len
lda.b #0
jsl lk_fetch_open
rts
on_fopen: ; the stream: read up to 1 KB of it
lda.w !lk_rsp+!LKS_STATUS
sta.w !EX+50
bne +
lda.w !lk_rsp+!LKS_PAYLOAD+0
ldx.w #0
ldy.w #1024
jsl lk_fetch_read
+ rts
asar · lib/asar/selftest.asm:287-289 · the URL the ROM carries
url:
db "https://example.com/sdk/news.txt"
url_end:
The examples carry example.com URLs: put your own. The ROM also needs the two marks of
its header (section 3) and, for the network, a registration in the GamesDB
(section 8).
Requirements
- Firmware: sd2snes+ 3.0 on the cartridge, and an sd2snes+ Link with the Link firmware that ships with 3.0. With an older cartridge firmware, another cartridge or an emulator, the detection says no and the game plays offline.
- Boards: sd2snes Mk.II, FXPAK PRO (Mk.III) and FXPAK PRO STM32.
- Games: a SNES game without an enhancement chip (LoROM or HiROM), or with DSP-n / ST-0010, ST-0011 or ST-0018: the cores that have the mailbox window. Only a LoROM game without a chip has been checked on hardware.
- Not in this version: ExHiROM games; SA-1, Super FX, Cx4, S-DD1, SPC7110 and OBC1 games; Game Boy games; Satellaview games with a memory pack and Sufami Turbo games (they map their own memory where the mailbox goes); a game started in Link Play mode.
- ROM size: while the SDK is open, banks
$E0-$EFare the mailbox's window, not ROM. Otherwise they hold the ROM offsets$300000-$37FFFFin LoROM and$200000-$2FFFFFin HiROM, so a LoROM game of up to 3 MB and a HiROM game of up to 2 MB have nothing there. A LoROM or HiROM game of up to 4 MB finds the same bytes in banks$60-$6Fand may use the SDK only if it reads that part of its ROM there, never through$E0-$EF. ExHiROM is out: the lower halves of its offsets$200000-$2FFFFFexist only in$E0-$EF. - RAM for the library: about 560 bytes of WRAM inside
$0000-$1FFF, used as direct page while a routine runs (ca65: theLKRAMsegment; asar:!LK_RAM). The C library uses two 256-byte arrays and a few bytes. - Battery SRAM is not a mailbox: keep the SDK's state in the mailbox and in WRAM. The firmware autosaves SRAM to the card, and with the Link it is backed up to the cloud. A download may go to SRAM, knowing that.
- Savestates do not include the mailbox: after loading one, the game opens a new
session (
lk_open). The in-game hook (menu, savestates) stays.
Asking for the SDK in the header
A game asks with two marks in the bank of its internal header, at CPU $00:FFA0-$00:FFBC
for LoROM and HiROM alike. File offsets without a copier header: LoROM $7FA0, HiROM
$FFA0.
| Where | Size | Content |
|---|---|---|
$FFB6-$FFBC | 7 | "SD2SNES", ASCII, no terminator: the identification register's mark, the same as on the developer tools page |
$FFA0-$FFA3 | 4 | "LKSD": the SDK descriptor's magic |
$FFA4 | 1 | descriptor version, 1 |
$FFA5 | 1 | request flags: bit 0 = mailbox, must be 1; bits 1-7 are 0 |
$FFA6-$FFA7 | 2 | reserved, 0 |
$FFA8-$FFAF | 8 | app_id, the 8 bytes the GamesDB gives the game when you register it (section 8). All zero = not registered: it works only with a Link in developer mode. |
A descriptor with another magic, another version, bit 0 clear or a reserved bit set does not open
the mailbox. Recompute the header checksum after writing the bytes;
tools/snesrom.py check game.sfc verifies both marks and fix sets the
checksum.
Both marks must be in the ROM file itself. The firmware reads them from the file
before any IPS/BPS patch is applied at the load, so a patch the menu applies is not seen: a patch
that adds the marks leaves the game with no SDK (status 5, NO_ASK, when the file
already had the SD2SNES mark; no identification register at all when it did not). A
romhack or a translation that wants the SDK is distributed, or kept on the card, as a ROM with the
patch already applied. The sd2snes+ menu makes one: Y on the patch in the patch
list › Create patched ROM.
.segment "SDKDESC"
.byte "LKSD", 1, $01, 0, 0
.byte "SDKEXAMP" ; app_id 53444b4558414d50: the examples' (SPEC 12.1)
.segment "HEADER"
.byte "00", "LKFE"
.byte "SD2SNES"
.byte 0, 0, 0
.byte "LINK SDK FETCH "
.byte $20, $00, $05, $00, $01, $33, $00
.word $FFFF, $0000
asar · lib/asar/selftest.asm:342-352 (lorom)
; ---- the SDK descriptor and the header (SPEC 3)
org $00FFA0
db "LKSD", 1, $01, 0, 0
db "SDKEXAMP" ; app_id 53444b4558414d50: the examples' (SPEC 12.1)
org $00FFB0
db "00", "LKAS"
db "SD2SNES"
db 0, 0, 0
db "LINK SDK ASAR TEST "
db $20, $00, $05, $00, $01, $33, $00
dw $FFFF, $0000
53444b4558414d50 (SDKEXAMP) is the app_id of the SDK's examples. Your game
gets its own.
Detection and states
The player's permission
The first time the game is started from the cartridge's menu, the menu asks, before the game runs:
Internet access. <title> wants to use the internet through the sd2snes+ Link. It can
download content for this game. Your answer is remembered: revoke it on the Link's web page.
The answers are Allow and Don't allow; B goes back to the menu.
The answer is kept per game in /sd2snes/sdk/consent.txt, so the question comes once.
A game started without the menu (a usb2snes Boot, autoboot) is never asked: the stored
answer decides, and without one the mailbox stays closed.
What lk_detect reads
The firmware writes the 8-byte identification register at $2BC0-$2BC7 at every game
load (layout on the developer tools page). In firmware 3.0 it is version
3: capability bit $10 at $2BC3 = the mailbox is open,
$2BC5 = the SDK status, $2BC6 = the mailbox's bank ($EF).
lk_detect returns the status, or $FF when there is no identification
register at all.
| Status | Meaning |
|---|---|
1 OPEN | The mailbox is open at $EF:0000: go on with lk_open. |
0 NONE | No valid descriptor, or a firmware without the SDK (register version 2). |
2 DECLINED | The player answered "Don't allow" at this load. |
3 NO_MAP | This core cannot open the mailbox (a chip outside the list above). |
4 DENIED | The stored answer for this game is "Don't allow", or the descriptor asks for something this firmware refuses. |
5 NO_ASK | The question was needed but could not be asked (usb2snes boot, autoboot). |
6 BUSY | The game was started in Link Play mode. |
$FF | Library only: not an sd2snes+ (another cartridge, an emulator, an older firmware). |
The Link's side: host states
Once the mailbox is open, lk_open starts a session and lk_poll (once per
frame) returns the host state, which the Link writes in the mailbox. The Link also beats a heartbeat
at least once a second; lk_poll returns the carry clear when it did not move for 180 frames
(3 s). A Link that comes back (a Wi-Fi drop, a restart) needs a new lk_open.
| State | When | What the game gets |
|---|---|---|
0 ABSENT | No Link, a Link without the SDK, or a Link still starting | Nothing answers and lk_poll returns the carry clear. Give up after your own timeout and play offline. |
1 READY | Serving | Downloads, if the Link is online and the game registered (or in developer mode). Offline: E_OFFLINE on a download, but the game's files on the card still work. |
2 POLICY_OFF | The project switched the SDK off, for everyone or for this game | HELLO and STATUS only; the rest is E_DENIED. |
3 BUSY | A Link Play or Game Boy link cable session runs on the Link | E_BUSY |
4 USER_OFF | The player switched off "Games may use the Link" on the Link's page | HELLO and STATUS only; the rest is E_DENIED. |
The HELLO answer and the mailbox also carry host flags: ONLINE (internet reachable),
PAIRED (the Link has an account), REGISTERED (the game's app_id is known and
enabled), DEVMODE (developer mode) and RA.
Downloading a URL into memory
The mailbox is 64 KB of the cartridge's memory at $EF:0000. Requests and answers go
through two rings of 16 slots; the bulk area, $EF:4000-$EF:FFFF
(48 KB), belongs to the game, which names a range of it for each read.
- Open (
lk_fetch_open): anhttps://URL of up to 235 bytes, port 443, no user name, ASCII only. The answer gives a stream id, the HTTP status, the total length (when the server says it), theETagand the content type. An HTTP error (404, 500) is not an error of the request: the stream opens and the error body can be read. - Read (
lk_fetch_read): up to 32 KB per read into a range of the bulk area. Each answer says the offset of the bytes in the resource, how many were written, EOF, the total so far and the range it used. Fewer bytes than asked is normal; 0 bytes without EOF means "nothing yet, ask again". - Close (
lk_fetch_close). The Link also closes a stream idle for 30 s and every stream of a session that ended.
The Link writes into a range only as the answer to a read the game sent, so the game never gets more than it asked for and never has to keep up with the network.
The fast path: reads in flight and DMA
One read at a time waits a full round trip per piece. Keep 2 to 4 reads of the stream in
flight, each on its own range (three reads of 16 KB fill the 48 KB: a triple buffer); the
Link answers them in the order sent. Copy each piece out with one DMA and ask for
the next piece on the same range. lk_bulk_dma copies to WRAM,
lk_bulk_vram to VRAM (in vblank or forced blank); both use DMA channel 7
(LK_DMA_CH changes it). After EOF or an error, the reads still in flight answer the
same way (0 bytes with EOF, or the same error): count the answers.
; the binary: DEPTH reads at once, on ranges 0, PIECE, 2 * PIECE of the bulk area
rep #$20
.a16
lda nmi_count
sta t0
sep #$20
.a8
stz ended
stz inflight
ldx #0
@more: phx
jsr read_at
rep #$20
.a16
pla
clc
adc #PIECE
tax
sep #$20
.a8
cpx #DEPTH * PIECE
bcc @more
rts
ca65 · examples/fetch/fetch.s:373-400 · each piece: copy, then the same range again
@dlc: ; a piece: copy it out (DMA) and sum it, then the same range again until the end
rep #$20
.a16
lda lk_rsp+LKS_PAYLOAD+4
clc
adc ex_dgot
sta ex_dgot
lda ex_dgot+2
adc #0
sta ex_dgot+2
sep #$20
.a8
jsr take_piece
rep #$20
.a16
ldx #ex_dsum
jsr add_sum
sep #$20
.a8
dec inflight
lda lk_rsp+LKS_PAYLOAD+6
and #LK_FD_EOF
beq :+
sta ended
: lda ended
bne :+
ldx lk_rsp+LKS_PAYLOAD+12 ; the range it came in: free again
jsr read_at
ca65 · examples/fetch/common.inc:246-254 · inside take_piece: one DMA to $7E8000
: lda #.loword(COPY)
sta lk_arg+0
sep #$20
.a8
lda #^COPY
sta lk_arg+2
ldx lk_rsp+LKS_PAYLOAD+12
ldy tmp
jsl lk_bulk_dma
Cache, resume and POST
- Cache: keep the
ETagof the open (and the content). Next time open with theLK_FO_CONDflag and the ETag: an unchanged resource answers 304 with the flagNOT_MODIFIEDand no body. - Resume: after a broken connection (a read answers
E_REMOTEorE_TIMEOUT), open again withLK_FO_RESUME, the offset you already have and the ETag. A server that honours ranges answers 206 (flagPARTIAL) and the data goes on from there; one that does not answers 200 from offset 0. - POST: a body of up to 4 KB from the bulk area, as octet-stream, JSON, plain text
or a form (
lk_fetch_postin C;lk_argfields in ca65 and asar). - At most 2 streams open per session; 10 s to the response headers; at most 3 redirects, each one only to an approved domain.
Errors the game can show
Every answer has a status. lk_errstr(status) in C and lk_errtext in ca65
and asar give the short text in the third column.
| Code | Name | Text | Typical cause |
|---|---|---|---|
$01 | E_UNSUPPORTED | NOT SUPPORTED | An operation this Link does not have (see Status) |
$02 | E_BADREQ | BAD REQUEST | A bad length or field, a file name outside the rules |
$03 | E_DENIED | NOT ALLOWED | Reason byte: 1 not registered, 2 the project's policy, 3 the player's setting, 5 not in this state, 6 the game switched off in the registry |
$04 | E_OFFLINE | NO INTERNET | The Link has no internet |
$05 | E_BUSY | BUSY | Too many requests in flight, 2 streams open, a 5th read, a download to the card running, a Link Play session |
$06 | E_RATE | TOO MANY, WAIT | More than 20 opens in a minute; the answer says how many seconds to wait |
$07 | E_QUOTA | QUOTA USED UP | A download without an announced length passed its byte limit |
$08 | E_NOTFOUND | NOT FOUND | No such stream or file |
$09 | E_TOOBIG | TOO BIG | The announced length is above the limit |
$0A | E_TIMEOUT | TIMED OUT | 10 s without headers, 30 s without data |
$0B | E_REMOTE | NETWORK ERROR | Detail: 1 DNS, 2 connect, 3 TLS, 4 a private address refused, 5 a redirect refused |
$0C | E_DOMAIN | URL NOT ALLOWED | Not https, a host not approved for this game, a port, a user name, non-ASCII |
$0E | E_RANGE | BAD BUFFER | A range outside the bulk area, a read of 0 or more than 32768 bytes |
$0F | E_INTERNAL | INTERNAL ERROR |
; ---- an error: its code and its text, readable
fail:
.a8
sta ex_lasterr
AT 20, 2, PAL_RED
PRINT s_err
lda ex_lasterr
jsr print_hex8
lda #' '
jsr putc
lda ex_lasterr
jsl lk_errtext
rep #$20
.a16
lda lk_ptr
sta sptr
sep #$20
.a8
lda lk_ptr+2
sta sptr+2
jsr print_str
lda #ST_DONE
sta ex_step
rts
Speed and cost per frame
Measured on the bench, on an sd2snes Mk.II, with the example ROMs (SlowROM):
| Path | Measured |
|---|---|
| URL → game's WRAM, 3 reads of 16 KB in flight, DMA | ≈300 KB/s (3 MB in 602 frames) |
| URL → file on the card, through the GamesDB proxy | ≈470 KB/s |
| URL → file on the card, fetched by the Link directly | ≈320 KB/s |
| File on the card → game's WRAM | ≈170 KB/s |
| For comparison: one read of 16 KB at a time, a CPU byte loop | ≈50 KB/s |
- Your server and the player's network decide the real figure. For a download into memory the ceiling is the cartridge taking the Link's writes while a game runs (~400 KB/s).
- The game's cost:
lk_pollandlk_recvonce per frame. Copying a 16 KB piece withlk_bulk_dmastops the CPU for about 0.37 of a frame (8 master clocks a byte); a CPU byte loop in SlowROM takes about 16 frames for the same piece. - The Link's cost: one 8-byte read of the mailbox per video frame over the cartridge's USB; the slots only when the game wrote a request.
- The window answers at the speed of its bank: FastROM with
MEMSELset, SlowROM otherwise. FastROM access has not been confirmed on hardware yet.
Files in the game's own folder
Every game has one folder on the SD card, /sd2snes/sdk/<app_id in 16 lowercase hex>/,
and sees nothing else of the card. The Link writes the download itself, so the file can be larger
than the game's memory, and the game reads it back in pieces whenever it wants.
| C | What it does |
|---|---|
lk_file_fetch | A URL straight into a file. The URL is checked as an open's and counts in the same 20 opens a minute. Progress events (LK_EV_FILE_PROGRESS) at most every 64 KB, then the answer with the HTTP status and the file's size. Only a 200 or 206 writes the card; a 304 or an HTTP error leaves the file as it was. One at a time per game. |
lk_file_list | The files, in name order, with their sizes, a page at a time. |
lk_file_read | Up to 32 KB of a file from an offset into a bulk range. Several may be in flight, as with a download. |
lk_file_delete | Deletes a file (and an unfinished download of that name). |
lk_file_space | The bytes and files in the game's folder. The card's free space comes back as "not known" ($FFFFFFFF) with this Link. |
- Names: 1 to 32 characters of
[A-Za-z0-9._-], not starting with a dot and not ending in.part; no paths. Names compare without case (FAT). - Downloads in progress are written as
<name>.partand renamed when complete, replacing the old file. After an error, or when the session ends, the.partstays: fetch again withLK_FO_RESUMEto continue it. - Size: no cap but the card's free space and a per-file limit in the registry (256 MB by default).
- Offline: listing, reading, deleting and the folder's size use only the card. They
work without internet and without a registration, as long as the player allowed the game. Only
lk_file_fetchneeds the internet and a registered game. - While a download runs the game keeps running: the Link writes the card in pieces of 64 KB and serves the mailbox between them. An MSU-1 track pauses about 0.2 s per piece, so show a "downloading" screen and keep the music on the SPC.
- The folder is an ordinary folder: the player sees and deletes the files in the menu's file browser, and on the Link's page (Online › Games › Delete files).
lk_file_fetch("level9.bin", 10, url, sizeof url - 1, 0, 0, 0);
/* each frame */
while ((op = lk_recv())) {
if (op == LK_EV_FILE_PROGRESS) show_progress(LK_RSP_PAYLOAD + 4); /* u32 bytes, u32 total at +8 */
else if (op == (LK_OP_FILE_FETCH | LK_RSP)) { if (lk_rsp[3]) show(lk_errstr(lk_rsp[3])); else /* on the card */; }
}
ca65 · examples/dlc/dlc.s:238-264 · once the session is open
ldx #.loword(s_name)
stx lk_ptr
lda #^s_name
sta lk_ptr+2
rts
; ---- the session is open: the URL straight into the file
on_hello:
.a8
lda #1
sta ex_hello
AT 3, 13, PAL_GREEN
PRINT s_ready
AT 5, 2, PAL_WHITE
PRINT s_dl_lbl
lda #ST_FETCH
sta ex_step
lda #<(s_url_end - s_url) ; the url, as for lk_fetch_open
sta lk_len
ldx #.loword(s_url)
stx lk_ptr
lda #^s_url
sta lk_ptr+2
stz lk_vlen ; no etag
stz lk_vlen+1
ldx #.loword(s_name) ; the file's name: lk_arg+0..2 pointer, +3 length
stx lk_arg+0
Permissions and limits
Three yeses before a download
- The player, in the cartridge's menu, the first time the game starts (see section 4). Per game, remembered. The player can take it back on the Link's page: Online › Games › Revoke removes the answer and the menu asks again the next time. Revoking and deleting files work with the console in its menu. The same page has the switch Games may use the Link (on by default), which turns the SDK off for every game.
- The registration. Every game that downloads needs an entry in the GamesDB: its name, a contact and, for release builds, their CRC32s (section 8). There is no approval: the game gets its app_id at once. An unregistered game (app_id zero or unknown) gets no download; the files it already has in its folder still work.
- The domain. A registered game may fetch from the GamesDB's public
list of approved domains without asking. A domain outside the list is asked for per game
on the developer page and needs an admin's approval. Only
httpson port 443.
The GamesDB decides on every open: game registered and enabled, URL on an approved domain of that game, opens left this minute. The Link keeps no list of its own, so a game or a domain can be withdrawn at once.
Who fetches the URL
| Domain approved as | Fetched by | Notes |
|---|---|---|
direct (the default) | The Link, straight to the domain | No traffic through the project's server. The Link checks TLS against the URL's name with its own CA bundle, resolves the name once, refuses private, loopback, link-local, CGNAT and multicast addresses, and connects to the address it checked. Every redirect is checked the same way. A redirect to a domain approved as proxy is refused: point the game at the final URL. |
proxy | The GamesDB, and the Link relays the bytes | The admin's choice per domain: a certificate the Link's bundle does not know, a server that must not see the player's IP, or content the project wants to cache (at most 10 minutes). |
| anything else | Nobody | E_DOMAIN: the Link never contacts a host the GamesDB did not approve for that game. |
What your server sees: User-Agent: sd2snes-link-sdk/1 (with (proxy) after it
when the GamesDB fetches), X-SD2SNES-App: <app_id in hex>,
Accept-Encoding: identity and the cache or range headers the game asked for. No cookies,
nothing about the player.
Limits
| What | Limit |
|---|---|
| A download into memory | 8 MB |
| A file on the card | 256 MB by default (set per game in the registry), and the card's free space |
| Opens | 20 per minute per console per game, downloads into memory and into files together |
| Per day | no limit |
| Streams open / reads in flight / one read | 2 / 4 per stream / 32 KB |
| URL / ETag / POST body | 235 bytes / 64 bytes / 4 KB |
| Time | 10 s to the headers, 30 s without data, 30 s idle closes a stream |
| Redirects | 3, only to approved domains |
What a game cannot do
- Reach the local network: private, loopback and link-local addresses are refused, also after a redirect.
- See the card outside its own folder, or another game's folder.
- See the player's account: no name, e-mail or token, and not the Link's IP address or Wi-Fi data. A game whose mailbox is open cannot change the cartridge's configuration either: the firmware never opens that memory to it.
- Run with RetroAchievements: a game with the mailbox open plays with RetroAchievements off (not only hardcore), as in Link Play.
The project can also switch the SDK off for every game (the Link's remote policy) or for one game (the registry), with no firmware update.
Registering a game and asking for a domain
Everything happens on the
GamesDB's developer page
(sd2snes.ludufre.com/gamesdb/developer), signed in with an sd2snes+ account.
- Register a game: its name, a contact (an e-mail or a site) and the CRC32 of its release builds (8 hex digits each, at most 32). With CRCs, only those builds may use the network; leave the list empty while you develop and any build of the game may. The page shows the app_id at once: put it in the descriptor (section 3).
- Public list: the domains every registered game may fetch from, without asking, are listed on the same page.
- Ask for a domain: a host name (with or without its subdomains) and a note. It waits for an admin, who approves it or not and chooses whether the Link fetches it directly or through the project's server. Ask only for what the public list does not cover.
- Each game shows its limits and the last 30 days of use (MB downloaded and opens). One account can register up to 20 games, each with up to 16 domains of its own.
Testing without hardware
The SDK comes with lkmock (in mock/, Python 3), which plays the Link and the
cloud.
- An emulator: build the game in emulator mode (
-D LK_EMU=1in ca65,-DLK_EMU=1in asar,-DLK_EMUin C). The game then skips the detection and keeps a 32 KB mailbox in WRAM at$7F:0000(16 KB of bulk area). Runpython3 -m lkmock usb2snesagainst the usb2snes server of QUsb2Snes or SNI attached to the emulator, with the app_id of your ROM's descriptor; it writes the mailbox's header and serves it (needspip install websockets).--cardnames a folder that stands for the card's/sd2snes/sdk/. - A real console, in developer mode: on the Link's page, Online › Games ›
Developer mode, with the Server URL of a computer on your network running
python3 -m lkmock server --port 8790(for examplehttp://<computer>:8790). Every game's SDK requests then go to that server instead of the GamesDB, without a token; the registry is ignored (app_id zero works) and the game sees theDEVMODEflag. The player's question in the menu still applies.--site <folder>serves the files of a folder ashttps://sdk-test.example/<file>, fetched in proxy mode.
examples/fetch/build.sh; examples/hello/build.sh; examples/dlc/build.sh # ca65/ld65
ASAR=/path/to/asar lib/asar/build.sh # asar 1.8+
cd mock && python3 -m unittest tests.test_host tests.test_fetch tests.test_files tests.test_clib tests.test_transports
python3 -m lkmock usb2snes --url ws://localhost:23074 --app-id 53444b4558414d50 --card /tmp/card
# an emulator through QUsb2Snes/SNI (LK_EMU builds)
python3 -m lkmock server --port 8790 # the developer-mode server for a real Link
- The ca65 examples need
ca65/ld65(cc65) and Python 3; eachbuild.shwrites a cartridge ROM and an emulator-mode ROM (OUT=dirpicks the folder). The asar build needs asar 1.8 or newer inASAR. tools/lksum.py FILEprints the checksum thefetchanddlcexamples show for a download of that file.examples/hellouses the extension services (time, a value kept in the cloud): it runs against the mock only. On a real Link usefetchanddlc.
Code and license
The library, the examples, the mock and the tools are under the MIT license, in the SDK's repository:
- github.com/ludufre/sd2snes-link-sdk: the whole SDK.
- SPEC.md: the specification, draft 3.
| Folder | What |
|---|---|
lib/ca65/ | The library for ca65 (sd2link.s, sd2link.inc) |
lib/asar/ | The same for asar 1.8+ (sd2link.asm; build.sh makes a self-test ROM) |
lib/c/ | The same in C89, for pvsneslib or any 65816 C compiler (sd2link.h, sd2link.c) |
examples/fetch/ | Downloads a text, then a file the fast way, with progress, a checksum and the frames it took |
examples/dlc/ | Downloads a file to the card with progress, lists it, reads it back with a checksum, deletes it on A |
examples/hello/ | Status, time and a value kept in the cloud (extension services, mock only) |
mock/ | lkmock: the Link and the cloud in Python, and its tests |
tools/ | The examples' font, the ROM header check and checksum, the constants generator, lksum.py |
Status
Checked on hardware
On the sd2snes Mk.II and the FXPAK PRO (Mk.III), with the fetch and dlc examples:
- The question in the menu at the first load, and no question at the next one.
- A text and a file downloaded into the game's memory.
- A file downloaded to the card through the proxy (3 MB, with the right checksum), and one fetched by the Link directly from a server with real TLS.
- RetroAchievements off for the game; revoking on the Link's page brings the question back; the player's switch off gives "NOT ALLOWED".
In the specification, not in the Link yet
The SPEC also describes extension services: the Link's clock, the LED, a per-game player id, values
kept in the cloud, leaderboards, Discord presence and rooms between two consoles. The mock serves
them when it plays the Link; the Link that ships with 3.0 answers them E_UNSUPPORTED
("NOT SUPPORTED"), also in developer mode. Build on downloads and files.
Questions and requests: GitHub issues.