1 /*
2  * vdrive.h - Virtual disk-drive implementation.
3  *
4  * Written by
5  *  Andreas Boose <viceteam@t-online.de>
6  *
7  * This file is part of VICE, the Versatile Commodore Emulator.
8  * See README for copyright notice.
9  *
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 2 of the License, or
13  *  (at your option) any later version.
14  *
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License
21  *  along with this program; if not, write to the Free Software
22  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
23  *  02111-1307  USA.
24  *
25  */
26 
27 #ifndef VICE_VDRIVE_H
28 #define VICE_VDRIVE_H
29 
30 #include "types.h"
31 #include "vdrive-dir.h"
32 
33 /* temporarily until the 4000 support is not safe */
34 #define VDRIVE_IMAGE_FORMAT_4000_TEST (vdrive->image_format == VDRIVE_IMAGE_FORMAT_4000)
35 
36 /* High level disk formats.
37    They can be different than the disk image type.  */
38 #define VDRIVE_IMAGE_FORMAT_1541 0
39 #define VDRIVE_IMAGE_FORMAT_1571 1
40 #define VDRIVE_IMAGE_FORMAT_1581 2
41 #define VDRIVE_IMAGE_FORMAT_8050 3 /* Dual Disk Drive */
42 #define VDRIVE_IMAGE_FORMAT_8250 4 /* Dual Disk Drive */
43 #define VDRIVE_IMAGE_FORMAT_2040 5 /* Dual Disk Drive */
44 #define VDRIVE_IMAGE_FORMAT_4000 6
45 
46 #define BUFFER_NOT_IN_USE      0
47 #define BUFFER_DIRECTORY_READ  1
48 #define BUFFER_SEQUENTIAL      2
49 #define BUFFER_MEMORY_BUFFER   3
50 #define BUFFER_RELATIVE        4
51 #define BUFFER_COMMAND_CHANNEL 5
52 
53 #define WRITE_BLOCK 512
54 
55 #define SET_LO_HI(p, val)               \
56     do {                                \
57         *((p)++) = (val) & 0xff;        \
58         *((p)++) = ((val) >> 8) & 0xff; \
59     } while (0)
60 
61 #define DRIVE_RAMSIZE           0x400
62 
63 #define BAM_MAXSIZE (33 * 256)
64 
65 /* Serial Error Codes. */
66 #define SERIAL_OK               0
67 #define SERIAL_WRITE_TIMEOUT    1
68 #define SERIAL_READ_TIMEOUT     2
69 #define SERIAL_FILE_NOT_FOUND   64
70 #define SERIAL_NO_DEVICE        128
71 
72 #define SERIAL_ERROR            (2)
73 #define SERIAL_EOF              (64)
74 
75 typedef struct bufferinfo_s {
76     unsigned int mode;     /* Mode on this buffer */
77     unsigned int readmode; /* Is this channel for reading or writing */
78     uint8_t *buffer;          /* Use this to save data */
79     uint8_t *slot;            /* Save data for directory-slot */
80     unsigned int bufptr;   /* Use this to save/read data to disk */
81     unsigned int track;    /* which track is allocated for this sector */
82     unsigned int sector;   /*   (for write files only) */
83     unsigned int length;   /* Directory-read length */
84     unsigned int record;   /* Current record */
85 
86     /* REL file information stored in buffers since we can have more than
87         one open */
88     uint8_t *side_sector;
89     /* location of the side sectors */
90     uint8_t *side_sector_track;
91     uint8_t *side_sector_sector;
92 
93     uint8_t *super_side_sector;
94     /* location of the super side sector */
95     uint8_t super_side_sector_track;
96     uint8_t super_side_sector_sector;
97 
98     uint8_t *buffer_next;          /* next buffer for rel file */
99     unsigned int track_next;    /* track for the next sector */
100     unsigned int sector_next;   /* sector for the next sector */
101 
102     unsigned int record_max;  /* Max rel file record, inclusive */
103     unsigned int record_next; /* Buffer pointer to beginning of next record */
104     uint8_t needsupdate;         /* true if the current sector needs to be
105                                   written (from REL write) */
106     uint8_t super_side_sector_needsupdate; /* similar to above */
107     uint8_t *side_sector_needsupdate;
108 
109     vdrive_dir_context_t dir; /* directory listing context or directory entry */
110 } bufferinfo_t;
111 
112 struct disk_image_s;
113 
114 /* Run-time data struct for each drive. */
115 typedef struct vdrive_s {
116     struct disk_image_s *image;
117 
118     /* Current image file */
119     unsigned int mode;         /* Read/Write */
120     unsigned int image_format; /* 1541/71/81 */
121     unsigned int unit;
122 
123     unsigned int Bam_Track;
124     unsigned int Bam_Sector;
125     unsigned int bam_name;     /* Offset from start of BAM to disk name.   */
126     unsigned int bam_id;       /* Offset from start of BAM to disk ID.  */
127     unsigned int Header_Track; /* Directory header location */
128     unsigned int Header_Sector;
129     unsigned int Dir_Track;    /* First directory sector location */
130     unsigned int Dir_Sector;
131     unsigned int num_tracks;
132     /* CBM partition first and last track (1581)
133      * Part_Start is 1, Part_End = num_tracks if no partition is used
134      */
135     unsigned int Part_Start, Part_End;
136 
137     unsigned int bam_size;
138     uint8_t *bam;
139     bufferinfo_t buffers[16];
140 
141     /* Memory read command buffer.  */
142     uint8_t mem_buf[256];
143     unsigned int mem_length;
144 
145     /* removed side sector data and placed it in buffer structure */
146     /* BYTE *side_sector; */
147 
148     uint8_t ram[0x800];
149 } vdrive_t;
150 
151 /* Actually, serial-code errors ... */
152 
153 #define FLOPPY_COMMAND_OK       0
154 #define FLOPPY_ERROR            2
155 
156 
157 /* Return values used around. */
158 
159 #define FD_OK           0
160 #define FD_EXIT         1       /* -1,0, 1 are fixed values */
161 
162 #define FD_NOTREADY     -2
163 #define FD_CHANGED      -3      /* File has changed on disk */
164 #define FD_NOTRD        -4
165 #define FD_NOTWRT       -5
166 #define FD_WRTERR       -6
167 #define FD_RDERR        -7
168 #define FD_INCOMP       -8      /* DOS Format Mismatch */
169 #define FD_BADIMAGE     -9      /* ID mismatch (Disk or tape) */
170 #define FD_BADNAME      -10     /* Illegal filename */
171 #define FD_BADVAL       -11     /* Illegal value */
172 #define FD_BADDEV       -12
173 #define FD_BAD_TS       -13     /* Track or sector */
174 #define FD_BAD_TRKNUM   -14     /* Illegal track number */
175 #define FD_BAD_SECNUM   -15     /* Illegal sector number */
176 
177 
178 #define CHK_NUM         0
179 #define CHK_RDY         1
180 
181 /* ------------------------------------------------------------------------- */
182 
183 extern void vdrive_init(void);
184 extern int vdrive_device_setup(vdrive_t *vdrive, unsigned int unit);
185 extern void vdrive_device_shutdown(vdrive_t *vdrive);
186 extern int vdrive_attach_image(struct disk_image_s *image, unsigned int unit, vdrive_t *vdrive);
187 extern void vdrive_detach_image(struct disk_image_s *image, unsigned int unit, vdrive_t *vdrive);
188 extern void vdrive_close_all_channels(vdrive_t *vdrive);
189 extern int vdrive_get_max_sectors(vdrive_t *vdrive, unsigned int track);
190 extern void vdrive_get_last_read(unsigned int *track, unsigned int *sector, uint8_t **buffer);
191 extern void vdrive_set_last_read(unsigned int track, unsigned int sector, uint8_t *buffer);
192 
193 extern void vdrive_alloc_buffer(struct bufferinfo_s *p, int mode);
194 extern void vdrive_free_buffer(struct bufferinfo_s *p);
195 extern void vdrive_set_disk_geometry(vdrive_t *vdrive);
196 extern int vdrive_read_sector(vdrive_t *vdrive, uint8_t *buf, unsigned int track, unsigned int sector);
197 extern int vdrive_write_sector(vdrive_t *vdrive, const uint8_t *buf, unsigned int track, unsigned int sector);
198 
199 #endif
200