24 #ifdef CONFIG_EXDOS_SUPPORT
30 #include <sys/types.h>
37 #define DEBUGEXDOS DEBUG
39 static int disk_fd = -1;
40 static Uint8 disk_buffer[512];
45 static Uint8 wd_status, wd_data, wd_command, wd_interrupt, wd_DRQ;
46 static int driveSel, diskSide, diskInserted, readOnly;
47 static int diskChanged = 0x40;
48 static int buffer_pos = 0, buffer_size = 1;
51 #define WDINT_OFF 0x3C
54 #define SEEK_ERROR (32 | 16 | 8)
59 static const int disk_formats[][2] = {
73 static int doCRC (
Uint8 *
buf,
int nBytes,
int n)
75 int nBits = nBytes << 3;
84 if ((bitBuf ^ (n >> 8)) & 0x80)
85 n = (n << 1) ^ 0x1021;
89 bitBuf = (bitBuf << 1) & 0xFF;
96 static int guess_geometry (
int fd )
98 off_t disk_size = lseek(fd, 0, SEEK_END);
99 for (
int a = 0; disk_formats[a][0] != -1; a++) {
100 int wd_max_tracks_new = disk_formats[a][0];
101 int wd_max_sectors_new = disk_formats[a][1];
102 if ((wd_max_tracks_new * wd_max_sectors_new) << 10 == disk_size) {
134 char wd_img_path_new[PATH_MAX];
137 if (disk_fd_new <= 0) {
142 INFO_WINDOW(
"Disk image could be opened only in R/O mode\n%s", wd_img_path_new);
143 DEBUGPRINT(
"WD: disk image opened in R/O mode only: %s" NL, wd_img_path_new);
146 DEBUGPRINT(
"WD: disk image opened in R/W mode: %s" NL, wd_img_path_new);
148 if (guess_geometry(disk_fd_new)) {
149 ERROR_WINDOW(
"Cannot figure the EXDOS disk image geometry out, invalid/not supported image size?");
156 disk_fd = disk_fd_new;
175 driveSel = (disk_fd >= 0);
177 diskInserted = (disk_fd < 0) ? 1 : 0;
182 static int read_sector (
void )
191 memset(disk_buffer, 0, 512);
204 if (lseek(disk_fd,
ofs, SEEK_SET) !=
ofs) {
208 ret = read(disk_fd, disk_buffer, 512);
209 DEBUGEXDOS(
"WD: read() for 512 bytes data to read, retval=%d" NL, ret);
212 ERROR_WINDOW(
"WD/EXDOS disk image read error: NO DATA READ");
228 return 128 | wd_status | wd_DRQ;
236 wd_data = disk_buffer[buffer_pos++];
237 if (buffer_pos >= buffer_size)
247 return wd_interrupt | (wd_DRQ << 6) | diskInserted | diskChanged;
257 DEBUGEXDOS(
"WD: command received: 0x%02X driveSel=%d distInserted=%d hasImage=%d" NL,
value, driveSel, diskInserted, disk_fd >= 0);
258 switch (
value >> 4) {
290 disk_buffer[1] = diskSide;
293 i = doCRC(disk_buffer, 4, 0xB230);
294 disk_buffer[4] = i >> 8;
295 disk_buffer[5] = i & 0xFF;
306 wd_status = (
wd_track == 0) ? 4 : 0;
310 wd_status = 4 | 8 | 16 | 64;
327 driveSel = (disk_fd >= 0) && ((
value & 15) == 1);
328 diskSide = (
value >> 4) & 1;
329 diskInserted = driveSel ? 0 : 1;
334 #warning "EXDOS/WD support is not compiled in / not ready"