aboutsummaryrefslogtreecommitdiff
path: root/src/nio.c
blob: 61ac8ae0791848dd8edc93a6425f52dd8bc15e61 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/**
 * N: I/O
 */

#include "nio.h"
#include "sio.h"
#include <atari.h>
#include <stddef.h>

#define TIMEOUT 0x1f /* approx 30 seconds */

#define UNIT 1 /* only support one FujiNet! */

static char get_status(char *devicespec) {
	if(OS.dcb.dstats != SUCCESS) {
		// something went wrong
		// do we need to return extended status?
		if(OS.dcb.dstats == DERROR) {
			nstatus(devicespec);
			return OS.dvstat[DVSTAT_EXTENDED_ERROR]; // return extended error.
		}
	}
	return OS.dcb.dstats; // Return SIO error or success
}

static void set_defaults(void) {
  OS.dcb.ddevic   = DFUJI;      // Fuji Device Identifier
  OS.dcb.dunit    = UNIT;       // Unit number integer 1 through 4
  OS.dcb.dtimlo   = TIMEOUT;    // approximately 30 second timeout
}

unsigned char nopen(char* devicespec, unsigned char trans)
{
	set_defaults();
  OS.dcb.dcomnd   = 'O';        // Open
  OS.dcb.dstats   = DWRITE;     // sending to to SIO device
  OS.dcb.dbuf     = devicespec; // eg: N:TCP//
  OS.dcb.dbyt     = 256;        // max size of our device spec
  OS.dcb.daux1    = OUPDATE;    // Read and write
  OS.dcb.daux2    = trans;      // CR/LF translation
  siov();

  return get_status(devicespec);
}

unsigned char nclose(char* devicespec)
{
	set_defaults();
  OS.dcb.dcomnd   = 'C';      // Close
  OS.dcb.dstats   = 0x00;
  OS.dcb.dbuf     = NULL;
  OS.dcb.dbyt     = 0;
  OS.dcb.daux     = 0;
  siov();

  return get_status(devicespec);
}

unsigned char nstatus(char* devicespec)
{
	set_defaults();
  OS.dcb.dcomnd   = 'S';    // status
  OS.dcb.dstats   = DREAD;
  OS.dcb.dbuf     = OS.dvstat;
  OS.dcb.dbyt     = sizeof(OS.dvstat);
  OS.dcb.daux     = 0;
  siov();

  return OS.dvstat[DVSTAT_EXTENDED_ERROR]; // return extended status
}

unsigned char nread(char* devicespec, unsigned char* buf, unsigned short len)
{
	set_defaults();
  OS.dcb.dcomnd   = 'R';    // read
  OS.dcb.dstats   = DREAD;
  OS.dcb.dbuf     = buf;
  OS.dcb.dbyt     = OS.dcb.daux = len; // Set the buffer size AND daux with length
  siov();

  return get_status(devicespec);
}

unsigned char nwrite(char* devicespec, unsigned char* buf, unsigned short len)
{
	set_defaults();
  OS.dcb.dcomnd   = 'W';    // write
  OS.dcb.dstats   = DWRITE;
  OS.dcb.dbuf     = buf;
  OS.dcb.dbyt     = OS.dcb.daux = len;
  siov();

  return get_status(devicespec);
}

/* https://fujinet.online/wiki/?p=SIO-Command-%24FF-Reset-FujiNet */
unsigned char nreset(void) {
	set_defaults();
  OS.dcb.ddevic   = 0x70;
  OS.dcb.dcomnd   = 0xff; /* reset */
  OS.dcb.dstats   = DWRITE;
  OS.dcb.dbuf     = 0;
  OS.dcb.dbyt     = 0;
  OS.dcb.daux     = 0;
  siov();

  return OS.dvstat[DVSTAT_EXTENDED_ERROR]; // return extended status
}