forge Documentation

fujinet

FujiNet

NOTE: The FujiNet Statements are not available in the integer-only version.

FujiNet is a modern network adapter for the Atari 8-bit computers that provides WiFi connectivity and emulates disk drives over the network.

Overview

These are statements that talk to the FujiNet network adapter, and can be used to open network connections, using any protocol supported.

Each of these statements require a unit number, of which 8 are available, numbered 1-8.

General Flow

The general flow of use is:

  1. NOPEN a connection
  2. In a loop:
    • Check for any traffic with NSTATUS
    • NGET if needed
    • Send any traffic with NPUT
  3. When done, NCLOSE

Opening a Connection

NOPEN unit, mode, trans, url / NO.

Open A Network Connection

Uses N: unit to open a connection to url using the desired mode and trans settings.

Example URLs:

N:HTTPS://www.gnu.org/licenses/gpl-3.0.txt
N:TCP://example.com:80
N:UDP://192.168.1.1:1234

Common modes:

ModeDescriptionHTTP Equivalent
4READGET
6DIRECTORYPROPFIND
8WRITEPUT
12READ/WRITETCP
13-POST

Common trans (translation modes):

ModeDescription
0No translation of characters
1Change CR to ATASCII EOL
2Change LF to ATASCII EOL
3Change CR and LF to EOL

Example:

' Open HTTP connection for reading
NOPEN 1, 4, 0, "N:HTTPS://example.com/file.txt"
IF ERR() <> 1 THEN ? "Error opening connection"

Closing a Connection

NCLOSE unit / NC.

Close A Network Connection

Closes a network connection unit previously opened by NOPEN.

Example:

NCLOSE 1

Checking Connection Status

NSTATUS unit / NS.

Get Network Connection Status

Queries the status of specified network unit. The result is stored in DVSTAT starting at $02EA, and has the format:

AddressDescription
$02EA# of bytes waiting (LO)
$02EB# of bytes waiting (HI)
$02ECConnected? (0 or 1)
$02EDMost recent error #

Example:

NSTATUS 1
BYTES_WAITING = DPEEK($02EA)
CONNECTED = PEEK($02EC)
ERROR = PEEK($02ED)

IF CONNECTED = 1 THEN
  ? "Connected, "; BYTES_WAITING; " bytes waiting"
ELSE
  ? "Not connected, error: "; ERROR
ENDIF

Reading Data

NGET unit, addr, len / NG.

Read Bytes From Network To addr

Reads len bytes from the network connection and stores them at memory address addr.

Important: When reading, len must be less than, or equal to the number of bytes waiting to be received, or an SIO error will result. Therefore, it is a good idea to figure out how many bytes are waiting using the NSTATUS command.

Example:

DIM buffer(1024) BYTE

NSTATUS 1
BYTES = DPEEK($02EA)

IF BYTES > 0 THEN
  IF BYTES > 1024 THEN BYTES = 1024
  NGET 1, ADR(buffer), BYTES
  IF ERR() = 1 THEN
    ? "Read "; BYTES; " bytes"
  ENDIF
ENDIF

Writing Data

NPUT unit, addr, len / NP.

Write Bytes To Network From addr

Writes len bytes to the network connection from memory address addr.

When writing, len must be less than, or equal to the number of bytes in the source buffer.

Example:

DIM data(100) BYTE
' Fill data array...
NPUT 1, ADR(data), 100
IF ERR() <> 1 THEN ? "Error writing data"

Complete Example: HTTP GET

' Open HTTP connection
NOPEN 1, 4, 0, "N:HTTPS://example.com/file.txt"
IF ERR() <> 1 THEN
  ? "Error opening: "; ERR()
  END
ENDIF

' Wait for connection
REPEAT
  NSTATUS 1
  CONNECTED = PEEK($02EC)
  PAUSE 1
UNTIL CONNECTED = 1

' Read data
DIM buffer(4096) BYTE
TOTAL = 0

REPEAT
  NSTATUS 1
  BYTES = DPEEK($02EA)
  IF BYTES > 0 THEN
    IF BYTES > 4096 THEN BYTES = 4096
    NGET 1, ADR(buffer), BYTES
    IF ERR() = 1 THEN
      TOTAL = TOTAL + BYTES
      ' Process buffer...
    ENDIF
  ENDIF
  PAUSE 1
UNTIL BYTES = 0

' Close connection
NCLOSE 1
? "Received "; TOTAL; " bytes total"

Error Handling

Always check ERR() after network operations:

NOPEN 1, 4, 0, "N:HTTPS://example.com/file.txt"
IF ERR() <> 1 THEN
  ? "Open failed: "; ERR()
  END
ENDIF

Common error codes:

  • 1 - Success
  • >127 - Error (check DVSTAT for details)

SIO Commands

For advanced usage, you can use SIO commands directly. See SIO Operations for details.

For complete SIO command documentation, see: SIO Commands for FujiNet Devices

Best Practices

  1. Always check connection status - Use NSTATUS before reading/writing
  2. Handle errors gracefully - Check ERR() after every operation
  3. Use appropriate buffer sizes - Don't read more bytes than available
  4. Close connections properly - Always call NCLOSE when done
  5. Handle timeouts - Network operations can take time
  6. Use HTTPS for secure connections - When available

Troubleshooting

Connection fails:

  • Check WiFi is connected
  • Verify URL is correct
  • Check FujiNet device is responding
  • Try HTTP instead of HTTPS

No data received:

  • Check connection status with NSTATUS
  • Verify bytes are waiting
  • Check error codes in DVSTAT

Data corruption:

  • Verify buffer size matches bytes available
  • Check buffer address is correct
  • Ensure buffer is not overwritten

Related Topics