XFS Filesystem
XFS is Spectranext's local filesystem that provides access to RAM-based storage directly from Spectrum programs. It allows you to transfer files from your computer and access them from your Spectrum programs.
Overview
XFS provides:
- RAM Filesystem Access: Access files stored in Spectranext's RAM filesystem
- Standard VFS Interface: Uses Spectranet's standard VFS (Virtual File System) API with
xfs://prefix - File Operations: Read, write, list, create, delete files and directories with standard Spectranet BASIC commands
- Development Workflow: Transfer files via USBFS, access from Spectrum programs
Mounting XFS
To use XFS, you must first mount it. XFS uses the protocol name "xfs" and a hostname to specify which filesystem to mount.
Mounting RAM Filesystem
The RAM filesystem is mounted using the protocol "xfs://ram/":
- BASIC
- C
- Assembly
%mount 0, "xfs://ram/"
#include <spdos.h>
mount(0, NULL, NULL, "ram", NULL, "xfs");
.include "spectranet.inc"
; Mount structure
mount_struct:
defw protocol_str
defw hostname_str
defw path_str
defw user_str
defw password_str
protocol_str: defb "xfs",0
hostname_str: defb "ram",0
path_str: defb 0
user_str: defb 0
password_str: defb 0
; Mount at mount point 0
ld ix, mount_struct
ld a, 0
call MOUNT
Keep in mind, that mounting can be happen automatically upon boot.
Unmounting
- BASIC
- C
- Assembly
%umount 0
umount(0);
ld a, 0
call UMOUNT
Mount Points
Spectranet supports up to 4 mount points (0-3). Each mount point can have a different filesystem mounted:
- Mount Point 0: Typically used for primary filesystem
- Mount Points 1-3: Available for additional filesystems
You can switch between filesystems using:
- BASIC
- C
%fs 0 ; Switch to filesystem 0
setmountpoint(0); // Switch to filesystem 0
Accessing Files from Spectrum
Once XFS is mounted, you can access files using standard Spectranet VFS operations.
Opening Files
- BASIC
- C
- Assembly
%open #4, "program.tap", "r" ; Open for reading
%open #5, "output.txt", "w" ; Open for writing
Streams are numbered starting from #4 (streams #0-#3 are reserved for Spectrum system use).
#include <spdos.h>
#include <fcntl.h>
int fd = open("program.tap", O_RDONLY, 0); // Open for reading
int fd2 = open("output.txt", O_WRONLY | O_CREAT | O_TRUNC, 0); // Open for writing
; Open file for reading
ld hl, filename
ld de, O_RDONLY ; Open flags
call OPEN ; ROM routine at 0x3EB1
; Returns file handle in HL (carry set on error)
File Flags:
O_RDONLY(0x01) - Open for readingO_WRONLY(0x02) - Open for writingO_RDWR(0x03) - Open for reading and writingO_CREAT(0x04) - Create file if it doesn't existO_TRUNC(0x08) - Truncate file to zero length
Reading Files
- BASIC
- C
- Assembly
10 %open #4, "program.tap", "r"
20 %oneof 100
30 INPUT #4, a$
40 PRINT a$
50 GO TO 30
100 %close #4
This example opens a file and reads it line by line. The %oneof command catches end-of-file conditions.
Loading entire files:
%load "program.tap" ; Load BASIC program
%load "image" CODE 16384 ; Load binary data at address
%aload "machinecode" CODE 32768 ; Load arbitrary data (no TAP header)
#include <spdos.h>
#include <fcntl.h>
#include <unistd.h>
int fd = open("program.tap", O_RDONLY, 0);
if (fd >= 0) {
unsigned char buffer[256];
ssize_t bytes_read;
while ((bytes_read = read(fd, buffer, sizeof(buffer))) > 0) {
// Process buffer
}
close(fd);
}
ld hl, file_handle
ld de, buffer
ld bc, 256 ; Number of bytes to read
call READ ; ROM routine at 0x3EC9
; Returns bytes read in HL (carry set on error)
Writing Files
- BASIC
- C
- Assembly
10 %open #4, "output.txt", "w"
20 PRINT #4; "Hello, world!"
30 PRINT #4; "Line 2"
40 %close #4
Saving files:
%save "program" ; Save BASIC program
%save "image" CODE 16384,6912 ; Save binary data
%save "program" LINE 1 ; Save from specific line
#include <spdos.h>
#include <fcntl.h>
#include <unistd.h>
int fd = open("output.txt", O_WRONLY | O_CREAT | O_TRUNC, 0);
if (fd >= 0) {
const char* data = "Hello, world!\nLine 2\n";
write(fd, data, strlen(data));
close(fd);
}
ld hl, file_handle
ld de, buffer
ld bc, 256 ; Number of bytes to write
call WRITE ; ROM routine at 0x3ECC
; Returns bytes written in HL (carry set on error)
Closing Files
- BASIC
- C
- Assembly
%close #4
close(fd);
ld hl, file_handle
call VCLOSE ; ROM routine at 0x3ED2
Important: Always close files when done to free resources.
Listing Directories
- BASIC
- C
- Assembly
%cat ; List current directory
%cat "games" ; List subdirectory
%cat "0:/foo/bar" ; List specific path
The %cat command displays directory contents. Paths work like Unix paths with "/" as the separator. Use "." for current directory and ".." for parent directory.
#include <spdos.h>
#include <dirent.h>
DIR* dir = opendir(".");
if (dir) {
struct dirent* entry;
while ((entry = readdir(dir)) != NULL) {
// Process entry->d_name
}
closedir(dir);
}
; Open directory
ld hl, "." ; Current directory
call OPENDIR ; ROM routine at 0x3EAE
; Returns directory handle in HL
; Read directory entry
ld hl, dir_handle
call READDIR ; ROM routine at 0x3ED8
; Returns entry in buffer (carry set on end of directory)
; Close directory
ld hl, dir_handle
call CLOSEDIR ; ROM routine at 0x3EDB
Changing Directory
- BASIC
- C
- Assembly
%cd "/" ; Change to root directory
%cd "games" ; Change to subdirectory
%cd "/programs/basic" ; Change to specific path
%cd ".." ; Change to parent directory
#include <spdos.h>
#include <unistd.h>
chdir("/"); // Change to root directory
chdir("games"); // Change to subdirectory
chdir("/programs/basic"); // Change to specific path
chdir(".."); // Change to parent directory
ld hl, dirname
call CHDIR ; ROM routine
; Changes current directory (carry set on error)
Getting File Information
- BASIC
- C
- Assembly
REM Use %cat to see file listings
#include <spdos.h>
#include <sys/stat.h>
struct stat st;
if (stat("filename.txt", &st) == 0) {
// Access st.st_size, st.st_mode, etc.
}
ld hl, filename
ld de, stat_buffer
call STAT ; ROM routine at 0x3EC3
; Fills stat structure at DE (carry set on error)
The stat structure contains:
- File size
Complete Examples
- BASIC
- C
- Assembly
Here's a complete example that mounts XFS, lists files, loads a program, and reads a file:
10 REM Mount XFS filesystem
20 %mount 0, "xfs://ram/"
30
40 REM List directory contents
50 %cat
60
70 REM Change to a subdirectory
80 %cd "games"
90 %cat
100
110 REM Load a program
120 %load "program.tap"
130
140 REM Read a text file line by line
150 %open #4, "data.txt", "r"
160 %oneof 200
170 INPUT #4, a$
180 PRINT a$
190 GO TO 160
200 %close #4
210
220 REM Unmount when done
230 %umount 0
Here's a complete C example that mounts XFS, lists files, and reads a file:
#include <spdos.h>
#include <fcntl.h>
#include <unistd.h>
#include <dirent.h>
#include <stdio.h>
int main(void) {
// Mount XFS
if (mount(0, NULL, NULL, "ram", NULL, "xfs") < 0) {
printf("Mount failed\n");
return -1;
}
// List directory
DIR* dir = opendir(".");
if (dir) {
struct dirent* entry;
while ((entry = readdir(dir)) != NULL) {
printf("%s\n", entry->d_name);
}
closedir(dir);
}
// Open and read file
int fd = open("data.txt", O_RDONLY, 0);
if (fd >= 0) {
unsigned char buffer[256];
ssize_t bytes_read;
while ((bytes_read = read(fd, buffer, sizeof(buffer))) > 0) {
// Process buffer
}
close(fd);
}
// Unmount
umount(0);
return 0;
}
Here's a complete assembly example that mounts XFS, lists files, and reads a file:
.include "spectranet.inc"
.data
mount_protocol: defb "xfs",0
mount_hostname: defb "ram",0
mount_source: defb 0
mount_user: defb 0
mount_password: defb 0
mount_struct:
defw mount_protocol
defw mount_hostname
defw mount_source
defw mount_user
defw mount_password
filename: defb "program.tap",0
buffer: defs 256
.text
main:
; Mount XFS
ld ix, mount_struct
ld a, 0
call MOUNT
jr c, mount_error
; Open file
ld hl, filename
ld de, O_RDONLY
call OPEN
jr c, open_error
ld (file_handle), hl
; Read file
ld hl, (file_handle)
ld de, buffer
ld bc, 256
call READ
jr c, read_error
; Close file
ld hl, (file_handle)
call VCLOSE
; Unmount
ld a, 0
call UMOUNT
ret
mount_error:
; Handle mount error
ret
open_error:
; Handle open error
ret
read_error:
; Handle read error
ret
.bss
file_handle: defw 0
Limitations
- RAM Only: Currently only RAM filesystem is supported (hostname
"ram") - Limited Handles: Maximum 4 open files/directories simultaneously
- File Size: Limited by available RAM filesystem space
- Persistence: RAM filesystem is cleared on reboot
Next Steps
- XFS Internals - Learn about XFS registers, commands, and module architecture
- Syncing with Computer - Learn about USBFS tools
- Memory Architecture - Understand memory mapping
- Check Spectranet VFS documentation for complete API reference