-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio_dsk.cpp
206 lines (168 loc) · 3.55 KB
/
io_dsk.cpp
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include <Arduino.h>
#include <hardware.h>
#if defined(USE_SD)
#include <SD.h>
#elif defined(USE_SPIFFS)
#include <FS.h>
#include <SPIFFS.h>
#elif defined(USE_LITTLEFS)
#include <FS.h>
#include <LittleFS.h>
#endif
#include <memory.h>
#include <CPU.h>
#include <ports.h>
#include <display.h>
#include <serial_dsp.h>
#include "config.h"
#include "banked_memory.h"
#include "io.h"
static File drive;
#define IMAGE_LEN 20
#define DRIVE_LETTERS 26
#if defined(USE_SD)
#define DISK SD
#elif defined(USE_SPIFFS)
#define DISK SPIFFS
#elif defined(USE_LITTLEFS)
#define DISK LittleFS
#endif
#if defined(ENERGIA_ARCH_tivac)
#define MODE_READ O_READ
#define MODE_READWRITE (O_READ | O_WRITE)
#else
#define MODE_READ "r"
#if defined(USE_LITTLEFS) && defined(LITTLEFS_READ_MODE)
#define MODE_READWRITE LITTLEFS_READ_MODE
#else
#define MODE_READWRITE "r+"
#endif
#endif
typedef struct disk_parameters {
uint8_t tracks, seclen;
uint16_t sectrk;
char image[IMAGE_LEN];
} disk_params_t;
disk_params_t disk_params[DRIVES];
disk_params_t *drive_letters[DRIVE_LETTERS], *dp;
static unsigned read_unsigned(File map) {
unsigned u = 0;
for(;;) {
char ch = map.read();
if (ch < '0' || ch > '9')
break;
u = u * 10 + (ch - '0');
}
return u;
}
void IO::dsk_reset() {
trk = sec = 0xff;
File map = DISK.open(PROGRAMS "drivemap.txt", MODE_READ);
if (!map) {
DBG(println(F("drivemap: open failed")));
return;
}
for (int i = 0; i < DRIVES; i++) {
int ch = map.read();
if (ch == -1)
break;
if (ch == '\n')
continue;
disk_params_t *p = &disk_params[i];
drive_letters[ch - 'A'] = p;
map.read(); // skip ':'
// read image-name
for (int j = 0; j < IMAGE_LEN; j++) {
ch = map.read();
if (ch == ' ') {
p->image[j] = 0;
break;
}
p->image[j] = ch;
}
p->tracks = read_unsigned(map);
p->seclen = read_unsigned(map);
p->sectrk = read_unsigned(map);
DBG(printf("%s: %d %d %d\r\n", p->image, p->tracks, p->seclen, p->sectrk));
}
map.close();
// read boot sector
settrk = 0;
setsec = 1;
setdma = 0;
dsk_select(0);
dsk_seek();
dsk_read();
}
bool IO::dsk_seek() {
if (trk != settrk || sec != setsec) {
trk = settrk;
sec = setsec;
int ok = drive.seek(dp->seclen*(dp->sectrk*trk + sec -1));
return ok == 1;
}
return true;
}
uint8_t IO::dsk_read() {
if (!dsk_seek())
return SEEK_ERROR;
uint8_t buf[dp->seclen];
int n = drive.read(buf, sizeof(buf));
if (n < 0)
return READ_ERROR;
for (int i = 0; i < n; i++)
_mem[setdma + i] = buf[i];
sec++;
return OK;
}
uint8_t IO::dsk_write() {
if (!dsk_seek())
return SEEK_ERROR;
uint8_t buf[dp->seclen];
for (unsigned i = 0; i < sizeof(buf); i++)
buf[i] = _mem[setdma + i];
int n = drive.write(buf, sizeof(buf));
if (n < 0)
return WRITE_ERROR;
sec++;
return OK;
}
uint8_t IO::dsk_select(uint8_t a) {
if (!drive_letters[a]) {
DBG(printf("dsk_select: %d\r\n", a));
return ILLEGAL_DRIVE;
}
if (dp == drive_letters[a])
return OK;
dp = drive_letters[a];
trk = sec = 0xff;
if (drive)
drive.close();
char buf[32];
snprintf(buf, sizeof(buf), PROGRAMS"%s", dp->image);
drive = DISK.open(buf, MODE_READWRITE);
if (!drive) {
DBG(print(buf));
DBG(println(F(": open failed")));
return ILLEGAL_DRIVE;
}
return OK;
}
// tracks are numbered from 0
uint8_t IO::dsk_settrk(uint8_t a) {
if (a >= dp->tracks) {
DBG(printf("settrk: %d\r\n", a));
return ILLEGAL_TRACK;
}
settrk = a;
return OK;
}
// sectors are numbered from 1
uint8_t IO::dsk_setsec(uint8_t a) {
if (a > dp->sectrk) {
DBG(printf("setsec: %d\r\n", a));
return ILLEGAL_SECTOR;
}
setsec = a;
return OK;
}