spectranext.h
spectranext.h exposes the Z80 interface to controller status, Wi-Fi, DNS,
engine calls, direct XFS-to-Spectranext-RAM reads, and Spectranet flash
configuration. Controller services use the
SPECTRANEXT ($3EF0) jump-table entry with the operation in A. Flash
configuration uses the Spectranet configuration ROM module.
Functions declared here are not linked separately as are provided as part of libspectranet library.
Error convention (assembly): carry set = error; carry clear = success.
Functions
C calling convention for the controller functions below: __z88dk_callee (callee cleans stack), except
spectranext_detect and spectranext_get_version, which use
__FASTCALL__.
spectranext_get_version
int8_t spectranext_get_version(char *version_out)
Returns the firmware build identity as a NUL-terminated ASCII string. The
caller supplies SPECTRANEXT_VERSION_SIZE (32) writable bytes outside
controller page B ($2000–$2FFF). Ignore the buffer if the call fails.
| Return | Meaning |
|---|---|
| 0 | Version string written to version_out. |
| -1 | The ROM does not support operation 15, or the call failed. |
- C
- Assembly
#include <spectranext.h>
char version[SPECTRANEXT_VERSION_SIZE];
if (spectranext_get_version(version) == 0) {
// Example: "1.0-12fb395e"
} else {
// Older ROM: the version is unavailable.
}
Operation ID: 15 (SPECTRANEXT_CMD_GET_VERSION).
| Register | Direction | Role |
|---|---|---|
A | in | 15 — get firmware build version |
HL | in | pointer to a writable 32-byte output buffer outside page B |
(HL) | out on success | NUL-terminated version string |
| carry | out | clear on success; set if unsupported or failed |
ld hl, version_buf ; reserve 32 bytes outside $2000-$2FFF
ld a, 15 ; SPECTRANEXT_CMD_GET_VERSION
ld ix, SPECTRANEXT ; $3EF0
call IXCALL ; $3FFD
jr c, .older_rom
; version_buf now contains the NUL-terminated string
The call preserves no output registers or flags other than carry. It does not need a working Wi-Fi connection or issue a controller mailbox command.
The string contains the contents of rp2350/version.txt, a hyphen, and the
build's eight-character Git hash. It has at most 31 visible bytes and no
newline. The shared controller ROM supplies the value on both cartridge
hardware and Fuse; Fuse reports the version bundled with the emulator.
Compare the major.minor prefix when your application needs a release level.
Compare the complete string when it needs one exact build. The hash identifies
a build but does not order builds with the same prefix. A release-level check
depends on version.txt being advanced for that release. This is firmware
identity, not a list of supported API features; the existence of this call
alone does not establish that any later operation is available.
spectranext_detect
Returns whether the cartridge is Spectranext (see Detection):
| Return | Meaning |
|---|---|
| 1 | Spectranext |
| 0 | Classic Spectranet |
| -1 | No compatible interface |
- C
- Assembly
int result = spectranext_detect();
if (result == 1) {
// Spectranext present
}
Detection is not a SPECTRANEXT operation — it probes CTRLREG ($033B) bit 7. Call the linked routine for Spectranext detection and the classic Spectranet fallback:
call spectranext_detect
; HL = 1: Spectranext; 0: Spectranet; -1: neither
See Detection for a standalone assembly probe. Compatibility mode
uses the separate read/write port $073B: bit 0 requests DivMMC and bit 1 sets
Next compatibility. $033B bits 1/2 are ignored on writes. The
port reference defines the
requests and their reset behavior.
spectranext_get_controller_status
int8_t spectranext_get_controller_status(int8_t *wifi_connection_out, uint32_t *ipv4_out)
Fills Wi-Fi connection state and IPv4 (host byte order).
| Return | Meaning |
|---|---|
| < 0 | ROM/port failure. |
| ≥ 0 | Controller status (compare with WIFI_CONTROLLER_STATUS_*). |
On success, *wifi_connection_out uses WIFI_CONNECT_*; *ipv4_out is the IPv4 address in host order.
- C
- Assembly
uint32_t ipv4;
int8_t wifi_state;
int8_t status = spectranext_get_controller_status(&wifi_state, &ipv4);
if (status >= 0) {
// wifi_state: WIFI_CONNECT_*
// ipv4: address in host order
}
Operation ID: 0 (SPECTRANEXT_CMD_GET_STATUS)
| Register | Direction | Role |
|---|---|---|
A | in | 0 — get controller status |
DE | in | pointer to 4-byte IPv4 output buffer |
B | out | controller status (WIFI_CONTROLLER_STATUS_*) |
C | out | Wi-Fi connection state (WIFI_CONNECT_*) |
(DE..DE+3) | out | IPv4 address, host byte order |
ld de, ipv4_buf ; 4-byte output buffer
ld a, 0 ; SPECTRANEXT_CMD_GET_STATUS (0)
ld ix, SPECTRANEXT ; $3EF0
call IXCALL ; $3FFD — self-paging on Spectranext
jr c, .error
; B = controller status
; C = Wi-Fi connection state
; (ipv4_buf) filled with IPv4 in host order
spectranext_wifi_scan_access_points
int8_t spectranext_wifi_scan_access_points(void)
| Return | Meaning |
|---|---|
| < 0 | Failure. |
| ≥ 0 | Number of networks found. |
- C
- Assembly
int8_t count = spectranext_wifi_scan_access_points();
if (count < 0) {
// error
} else {
// count networks available via spectranext_wifi_get_access_point()
}
Operation ID: 1 (SPECTRANEXT_CMD_WIFI_SCAN_ACCESS_POINTS)
| Register | Direction | Role |
|---|---|---|
A | in | 1 — scan access points |
A | out | number of networks found (carry clear) |
ld a, 1 ; SPECTRANEXT_CMD_WIFI_SCAN_ACCESS_POINTS (1)
ld ix, SPECTRANEXT ; $3EF0
call IXCALL ; $3FFD
jr c, .error
; A = number of networks found
spectranext_wifi_get_access_point
int8_t spectranext_wifi_get_access_point(uint8_t ap, spectranext_wifi_scan_result_t *result)
Copies the extended scan result for index ap (0-based). The result contains a 64-byte NUL-terminated SSID, a 6-byte BSSID, and signed RSSI in dBm. An RSSI of 0 and all-zero BSSID mean that extended metadata was unavailable.
| Return | Meaning |
|---|---|
| 0 | Success. |
| -1 | Failure. |
- C
- Assembly
spectranext_wifi_scan_result_t result;
int8_t r = spectranext_wifi_get_access_point(0, &result);
if (r == 0) {
// result.ssid, result.bssid, and result.rssi are available
}
Operation ID: 2 (SPECTRANEXT_CMD_WIFI_GET_ACCESS_POINT)
| Register | Direction | Role |
|---|---|---|
A | in | 2 — get access point result |
C | in | AP index (0-based) |
DE | in | pointer to output buffer (≥ 71 bytes) |
(DE) | out | SSID [0..63], BSSID [64..69], signed RSSI [70] |
ld c, 0 ; AP index (0-based)
ld de, scan_result ; output buffer, at least 71 bytes
ld a, 2 ; SPECTRANEXT_CMD_WIFI_GET_ACCESS_POINT (2)
ld ix, SPECTRANEXT ; $3EF0
call IXCALL ; $3FFD
jr c, .error
; extended scan result written at (scan_result)
spectranext_wifi_connect_access_point
int8_t spectranext_wifi_connect_access_point(const char *ssid, const char *password,
const uint8_t *bssid)
| Return | Meaning |
|---|---|
| 0 | Success. |
| -1 | Failure. |
- C
- Assembly
int8_t r = spectranext_wifi_connect_access_point(result.ssid, "s3cr3t", result.bssid);
if (r < 0) {
// connection failed
}
Operation ID: 3 (SPECTRANEXT_CMD_WIFI_CONNECT)
| Register | Direction | Role |
|---|---|---|
A | in | 3 — connect |
HL | in | pointer to NUL-terminated SSID |
DE | in | pointer to NUL-terminated password |
BC | in | pointer to 6-byte BSSID, or 0 if unavailable |
ld hl, ssid_str ; NUL-terminated SSID
ld de, pass_str ; NUL-terminated password
ld bc, bssid ; 6-byte BSSID, or 0
ld a, 3 ; SPECTRANEXT_CMD_WIFI_CONNECT (3)
ld ix, SPECTRANEXT ; $3EF0
call IXCALL ; $3FFD
jr c, .error
spectranext_wifi_disconnect
int8_t spectranext_wifi_disconnect(void)
| Return | Meaning |
|---|---|
| 0 | Success. |
| -1 | Failure. |
- C
- Assembly
int8_t r = spectranext_wifi_disconnect();
Operation ID: 4 (SPECTRANEXT_CMD_WIFI_DISCONNECT)
| Register | Direction | Role |
|---|---|---|
A | in | 4 — disconnect |
ld a, 4 ; SPECTRANEXT_CMD_WIFI_DISCONNECT (4)
ld ix, SPECTRANEXT ; $3EF0
call IXCALL ; $3FFD
jr c, .error
spectranext_gethostbyname
int8_t spectranext_gethostbyname(const char *hostname, uint32_t *result_ipv4)
Resolves hostname to an IPv4 address; *result_ipv4 is host byte order.
| Return | Meaning |
|---|---|
| 0 | Success. |
| -1 | Failure (ROM/port or lookup error). |
- C
- Assembly
uint32_t ipv4;
int8_t r = spectranext_gethostbyname("example.com", &ipv4);
if (r == 0) {
// ipv4 contains resolved address in host byte order
}
Operation ID: 5 (SPECTRANEXT_CMD_DNS)
| Register | Direction | Role |
|---|---|---|
A | in | 5 — DNS lookup |
HL | in | pointer to NUL-terminated hostname |
DE | in | pointer to 4-byte IPv4 result buffer |
(DE..DE+3) | out | resolved IPv4, host byte order |
ld hl, hostname_str ; NUL-terminated hostname, e.g. "example.com"
ld de, ipv4_buf ; 4-byte output buffer
ld a, 5 ; SPECTRANEXT_CMD_DNS (5)
ld ix, SPECTRANEXT ; $3EF0
call IXCALL ; $3FFD
jr c, .error
; (ipv4_buf) = resolved IPv4 in host order
spectranext_enginecall
int8_t spectranext_enginecall(const char *input, const char *output, const char *operation)
Runs a named engine call with argv-style arguments in operation (space-separated; see below).
Used to copy mounted files into the default XFS overlay, run JSONPath and XPath queries, or
list and extract ZIP archives.
Spectranext 2.0 limits: input 511 bytes, output 127 bytes, operation 255 bytes, excluding each terminating NUL. The SDK uses command 22. Deprecated command 6 remains supported for existing binaries with its original 127-character input limit and workspace layout. New SDK calls use command 22; do not use the expanded layout with command 6.
Input file (input) — a path on the current default XFS mount, or N:path to select mount slot 0…3 explicitly (e.g. 1:data/app.json).
Output file (output) — plain path through the default XFS overlay (no N: prefix). New output files go to an available USB drive, otherwise to temporary RAM. Use a separate path from the input and allow enough free space on the destination. See Engine output storage.
Operation (operation) — first token is the engine name, such as cp, jsonpath
xpath or zip (json is also accepted for jsonpath). Further tokens
are engine-specific. See Engine calls for usage and supported
input formats.
spectranext_enginecall("1:example.json", "example.bin", "jsonpath $[*].id $[*].name");
The call waits for completion on controller channel 0. To continue running while an operation executes, submit it through direct memory access on controller channel 1. Channel 0 SDK calls remain available.
| Return | Meaning |
|---|---|
| 0 | Success. |
| < 0 | Failure: negative int8_t error code from the engine (e.g. -1 unknown engine, -2 I/O, -3 JSON parse, -4 JSONPath, -5 non-scalar match, -6 bad argv). |
- C
- Assembly
int8_t r = spectranext_enginecall("0:config.json", "out.bin", "jsonpath $.name");
Operation ID: 22 (SPECTRANEXT_CMD_ENGINECALL)
| Register | Direction | Role |
|---|---|---|
A | in | 22 — engine call |
HL | in | pointer to input path (N:…, NUL-terminated) |
DE | in | pointer to output path through the default XFS overlay (NUL-terminated) |
BC | in | pointer to operation string (NUL-terminated) |
ld hl, input_spec ; e.g. "1:file.json"
ld de, output_path ; e.g. "out.bin" in the default overlay
ld bc, op_str ; e.g. "json $[*].id"
ld a, 22 ; SPECTRANEXT_CMD_ENGINECALL
ld ix, SPECTRANEXT
call IXCALL
jr c, .error ; A = error code
spectranext_enginecall_async / spectranext_enginecall_result
int8_t spectranext_enginecall_async(uint8_t channel, const char *input,
const char *output, const char *operation);
int8_t spectranext_enginecall_result(uint8_t channel);
Choose controller channel 0 or 1. Submission copies the strings and returns
immediately: 0 means accepted, -16 means that channel is busy, and -3 means
an invalid channel or string. Requires Spectranext 2.0 or newer with the current SDK. Input paths are limited to 511 bytes, output paths to 127 bytes, and the
operation to 255 bytes, excluding NUL. Code, stack and strings must be in Spectrum
RAM at or above 0x4000. The SDK restores page B and preserves the library's
paged/no-page calling convention.
Poll the same channel: 0 means pending, +1 means success, and a negative value
is an error. Polling before any async submission returns -1; invalid channels
return -3. Completed results remain available until another accepted request
or controller call uses that channel. Do not issue other controller calls on a
channel while its request is pending. The other channel can hold its own request;
this does not guarantee that the underlying engines execute in parallel.
int8_t result = spectranext_enginecall_async(0, "1:page.html", "page.bin",
"xpath \"//h1\"");
if (result == 0) {
while ((result = spectranext_enginecall_result(0)) == 0) {
/* Yield to the next frame, animate, or perform other application work. */
}
/* result > 0: success; result < 0: engine error. */
}
The blocking spectranext_enginecall() retains its existing return convention
(0 on success).
spectranext_xfs_read
int32_t spectranext_xfs_read(const spectranext_xfs_read_request_t *request)
Reads a range from an absolute path in the local XFS overlay directly into Spectranext RAM pages. This avoids an intermediate Z80 buffer and is intended for loading a contiguous binary image or data block into page-addressable Spectranext RAM.
The lookup is controller-owned: the path is resolved through the normal local overlay order—RAM, then flash, then ROMFS—not through the caller's currently selected VFS mount. The path must therefore be absolute, for example "/assets/data.bin".
| Return | Meaning |
|---|---|
>= 0 | Actual number of bytes read. A shorter value than maximum_data is end of file. |
-1 | Invalid request, controller failure, or XFS read failure. |
Request structure
typedef struct spectranext_xfs_read_request_s {
char source_filename[128];
uint32_t source_offset;
uint8_t target_first_page;
uint16_t target_first_page_offset;
uint32_t maximum_data;
} spectranext_xfs_read_request_t;
| Field | Meaning |
|---|---|
source_filename | Absolute, NUL-terminated path in the local XFS overlay. Maximum 127 visible characters plus the terminator. |
source_offset | Zero-based byte offset in the source file. |
target_first_page | First destination Spectranext RAM page, in the inclusive range $C0–$DF. |
target_first_page_offset | Offset within that first 4 KiB page, from 0 to $0FFF. |
maximum_data | Maximum number of bytes to copy. The complete destination range must fit from the chosen page/offset through the end of page $DF; requests do not wrap. |
The copy continues across consecutive 4 KiB RAM pages as required. It never modifies the source file. The routine does not change which pages are presently mapped at page A or page B; map the destination page yourself when you want to inspect or execute the loaded data.
- C
- Assembly
#include <spectranext.h>
spectranext_xfs_read_request_t request = {
"/assets/intro.bin", // source_filename
0, // source_offset
0xD8, // target_first_page
0, // target_first_page_offset
4096 // maximum_data
};
int32_t bytes = spectranext_xfs_read(&request);
if (bytes < 0) {
// Controller or XFS failure
} else if (bytes != 4096) {
// Source file ended early
}
// Expose the result at $1000–$1FFF:
setpagea(0xD8);
Operation ID: 8 (CMD_XFS_READ). HL points to the packed 139-byte request structure above. On success, DEHL contains the actual byte count; carry is set on failure.
ld hl, request ; packed spectranext_xfs_read_request_t
ld a, CMD_XFS_READ
ld ix, SPECTRANEXT
call IXCALL
jr c, .error
; DEHL = actual bytes read
Linking (libspectranet vs libspectranet_np)
- libspectranet — Paged build: use when your program already follows the usual Spectranet paging model (see memory).
- libspectranet_np — No-page stubs: often paired with an explicit
pagein()at startup when your code expects Spectranet ROM to be mapped before calling these helpers.
Exact link lines and pragma redirects match your other Spectranet projects (e.g. libterm, ndos). The SDK copies spectranext.h into sdk/include/.
See also
- Syscalls —
SPECTRANEXToperations,IXCALL, register I/O - I/O Ports —
$3400/$3401command/status - Detection —
spectranext_detect - Spectranext module — high-level module development notes
Protected user settings
CMD_SYS_SETTINGS_READ (9) and CMD_SYS_SETTINGS_WRITE (10) use HL as the
payload buffer and BC as capacity or exact length. READ returns BC=16.
The payload contains DivMMC compatibility (0 or 1) at byte 0, with 15 opaque
reserved bytes. Read the complete payload and preserve its other bytes when
editing. Buffers must not overlap page B ($2000–$2fff). Carry/A report errors:
1 unsupported command, 2 insufficient buffer, 3 invalid payload, 4 unsupported
format, 5 old/unrecognized bootloader, 6 busy, 7 storage/staging failure, 8 lockout.
An unsupported READ does not imply native mode.
C callers use spectranext_settings_read(buffer, capacity) and
spectranext_settings_write(buffer, length). READ returns 16, WRITE returns 0,
and failures return negative error codes. spectranext_get_diagnostics(buffer)
returns ten read-only bytes: bootloader version (LE u32), active policy (u8),
lockout reason (LE u32), and supported safety capabilities (u8).
Enabling requires bootloader version 3, safety ABI 1 and its settings/update capabilities. Enabling and disabling both enter limp mode until a complete power cycle. A paged SDK call exits from host RAM through its normal page-out. A no-page caller must explicitly page out from code and stack above 16 KiB after WRITE returns, and must not return into expansion ROM afterwards. Reset cannot clear a lockout; remove Spectrum power and USB, then remove or disable DivMMC before using native mode. These features remain subject to the hardware release checks described in the design document.
The enable-only force-DivMMC request at $073B
is a separate operation: bit 0 saves and reboots both devices if DivMMC mode is
not active, and does nothing if it already is. It cannot disable DivMMC or clear
an existing power-cycle lockout. Bit 1 independently latches Next compatibility
for the current boot; it is not part of the persistent settings payload.
.spx upgrade / spectranext_upgrade() requests bootloader mode for the OTA
image staged by the controller's autonomous OTA process. It performs no network
check or download. The bootloader owns file validation and reports missing or
invalid content. Entry requires bootloader revision 7, safety ABI 1 and
explicit-update capability (bit 2), alongside its protected-settings capabilities.
The controller prepares a one-shot PSRAM request and acknowledges it. Completed physical page-out authorizes the request, then it resets the Spectrum and reboots the controller. The application ROM returns through the dot command's normal setup stage; the dot frontend only supplies arguments.
In DivMMC mode the bootloader starts paged out. Run .spx after reset, manually
or from esxDOS startup, to probe $033B, explicitly page in, and call $0004.
That entry runs the full bootloader CRT and launches its checking,
confirmation/progress or error UI. While mapped, it serves the full low 16K.
Native bootloader entry remains automatic. Explicit requests cannot bypass
confirmation through /OTA/SILENT.
SDK calls acknowledge preparation only. Callers use their ordinary high-RAM page-out path and must tolerate a full reset afterward; no fingerprint polling or custom RAM continuation is required. Bootloader revision 9 asserts Spectrum RESET before withdrawing the update UI's ROM service on completion, cancellation or error recovery. The reset request survives controller reboot; the application releases the Spectrum once its memory and bus listener are ready. Protected settings are preserved. Ordinary DivMMC startup without a request stays passive.
Flash configuration
The configuration module (ROM ID 0xFE) stores application settings alongside
other Spectranet configuration. This API works on Spectranext and classic
Spectranet with the configuration module installed. Include <spectranext.h>
and link libspectranet or libspectranet_np. The linker pulls in the
configuration object when the program references a configuration function.
All C calls return 0 on success and -1 on failure. The firmware keeps one
shared RAM copy and one current section. Select a section successfully before
reading or writing an item. Call spectranext_config_copy() before editing.
Commit to persist the changes, or abandon the RAM copy if editing fails. Select
the section again after committing or abandoning. Keep code, the stack, and
string buffers in Spectrum RAM at 0x4000 or above because the firmware
remaps the low 16 KiB and uses it for scratch space.
Use a section ID assigned to your application. ROM modules use their module
ID as the low byte with a zero high byte. Applications without a module ID
use a nonzero high byte. 0xFFFF is reserved. String item IDs are
0x00–0x3F.
| C function | ROM call ID in HL | ROM inputs and behavior |
|---|---|---|
spectranext_config_copy() | 0xFE01 | Copies flash configuration into RAM. Reuses the RAM copy if one is already active. |
spectranext_config_find_section(section) | 0xFE02 | DE holds the section ID. Selects the section if it exists. Reads from flash until a RAM copy is active. |
spectranext_config_create_section(section) | 0xFE06 | DE holds the section ID. Selects an existing section or appends an empty one to the RAM copy. |
spectranext_config_get_string(item, out) | 0xFE03 | A holds the item ID and DE points to the output buffer. Copies the stored string and its NUL terminator. |
spectranext_config_set_string(item, value) | 0xFE0A | A holds the item ID and DE points to the input string. Creates or replaces the item in the RAM copy. |
spectranext_config_commit() | 0xFE07 | Writes the RAM copy back to flash and ends the edit. |
spectranext_config_abandon() | 0xFE0B | Discards the RAM copy so subsequent reads use flash. |
The setter accepts 1–254 bytes followed by NUL. Read only items known to contain a nonempty string. The firmware read has no capacity argument, so provide 255 writable bytes or use a smaller buffer only when your application knows the stored maximum length including NUL. Creating a section requires four free bytes in the configuration area and the firmware also needs room for the item. Commit only when a value changes, since the firmware rewrites the configuration flash sector.
- C
- Assembly
#include <spectranext.h>
/* section and item are assigned to the caller's application. */
int8_t save_token(uint16_t section, uint8_t item, const char *token)
{
if (spectranext_config_copy() != 0)
return -1;
if ((spectranext_config_find_section(section) != 0 &&
spectranext_config_create_section(section) != 0) ||
spectranext_config_set_string(item, token) != 0) {
spectranext_config_abandon();
return -1;
}
return spectranext_config_commit();
}
The section calls use __FASTCALL__. The string calls use
__z88dk_callee. With libspectranet_np, page in first and keep Spectranet
mapped while calling the API. The copy operation can leave a configuration
RAM page mapped in area A. Restore your application's page A mapping if
it uses that window.
With the ROM already mapped, use RST 0x28. With the ROM paged out, use
CALL 0x3FF8 for each call. The ROM reports failure with carry set.
; ROM already paged in. Code, token and stack are in Spectrum RAM.
; Input: DE = application section ID. This example uses string item 0x3F.
save_token:
push de
ld hl, 0xFE01 ; Copy flash configuration to RAM
rst 0x28
pop de
ret c
push de
ld hl, 0xFE02 ; Find and select the section
rst 0x28
pop de
jr nc, section_ready
ld hl, 0xFE06 ; Create and select the section
rst 0x28
jr c, abandon
section_ready:
ld a, 0x3F
ld de, token
ld hl, 0xFE0A ; Set the string in RAM
rst 0x28
jr c, abandon
ld hl, 0xFE07 ; Commit to flash
rst 0x28
ret
abandon:
ld hl, 0xFE0B
rst 0x28
scf
ret
token:
defb "example-token", 0