1 /* lib765.h - Copyright (c) 2002, 2003, 2004 John Elliott - 2005 Philip Kendall
2  *
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation; either version 2 of the License, or
6  * (at your option) any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
16  */
17 #ifndef __LIB765_H__
18 #define __LIB765_H__
19 
20 #include <stdarg.h>
21 #include <limits.h>
22 #include <libdsk.h>
23 
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
28 /*
29  * ############################################################################
30  * Defines
31  * ############################################################################
32  */
33 
34 #define MAX_SECTOR_LEN  8192
35 #define SHORT_TIMEOUT   1000
36 #define LONGER_TIMEOUT  1333333
37 
38 /* Floppy drive types */
39 #define FD_NONE          ( 0)
40 #define FD_30            ( 1)
41 #define FD_35            ( 2)
42 #define FD_525           ( 3)
43 
44 /* Floppy drive errors */
45 #define FD_E_OK          ( 0)
46 #define FD_E_SEEKFAIL    (-1)
47 #define FD_E_NOADDR      (-2)
48 #define FD_E_NODATA      (-3)
49 #define FD_E_DATAERR     (-4)
50 #define FD_E_NOSECTOR    (-5)
51 #define FD_E_NOTRDY      (-6)
52 #define FD_E_READONLY    (-7)
53 
54 /* Floppy drive dirty flags */
55 #define FD_D_UNAVAILABLE (-1)
56 #define FD_D_CLEAN       ( 0)
57 #define FD_D_DIRTY       ( 1)
58 
59 /*
60  * ############################################################################
61  * Types declarations
62  * ############################################################################
63  */
64 typedef unsigned char fdc_byte;
65 typedef short         fd_err_t;
66 
67 /*
68  * ############################################################################
69  * FDD declarations
70  * ############################################################################
71  */
72 typedef struct fdd_765        FDD_765;
73 typedef struct fdd_765_vtable FDD_765_VTABLE;
74 
75 struct fdd_765 {
76   FDD_765_VTABLE *fd_vtable;
77   int             fd_type;
78   int             fd_heads;
79   int             fd_cylinders;
80   int             fd_readonly;
81   int             fd_motor;
82   int             fd_cylinder;
83   char            fdl_filename[PATH_MAX + 1];
84   const char     *fdl_type;
85   const char     *fdl_compress;
86   DSK_PDRIVER     fdl_diskp;
87   DSK_GEOMETRY    fdl_diskg;
88 };
89 
90 struct fdd_765_vtable {
91   fd_err_t  (*fdv_seek_cylinder)(FDD_765 *fdd, int cylinder);
92   fd_err_t  (*fdv_read_id      )(FDD_765 *fdd, int head, int sector, fdc_byte *buf);
93   fd_err_t  (*fdv_read_sector  )(FDD_765 *fdd, int xcylinder, int xhead, int head, int sector, fdc_byte *buf, int len, int *deleted, int skip_deleted, int mfm, int multi);
94   fd_err_t  (*fdv_read_track   )(FDD_765 *fdd, int xcylinder, int xhead, int head, fdc_byte *buf, int *len);
95   fd_err_t  (*fdv_write_sector )(FDD_765 *fdd, int xcylinder, int xhead, int head, int sector, fdc_byte *buf, int len, int deleted, int skip_deleted, int mfm, int multi);
96   fd_err_t  (*fdv_format_track )(FDD_765 *fdd, int head, int sectors, fdc_byte *buf, fdc_byte filler);
97   fdc_byte  (*fdv_drive_status )(FDD_765 *fdd);
98   int       (*fdv_ready        )(FDD_765 *fdd);
99   void      (*fdv_eject        )(FDD_765 *fdd);
100 };
101 
102 extern void fdd_init_impl (FDD_765 *fdd);
103 extern void fdd_reset_impl(FDD_765 *fdd);
104 
105 extern fd_err_t fdd_seek_cylinder(FDD_765 *fdd, int cylinder);
106 extern fd_err_t fdd_read_id      (FDD_765 *fdd, int head, int sector, fdc_byte *buf);
107 extern fd_err_t fdd_read_sector  (FDD_765 *fdd, int xcylinder, int xhead, int head, int sector, fdc_byte *buf, int len, int *deleted, int skip_deleted, int mfm, int multi);
108 extern fd_err_t fdd_read_track   (FDD_765 *fdd, int xcylinder, int xhead, int head, fdc_byte *buf, int *len);
109 extern fd_err_t fdd_write_sector (FDD_765 *fdd, int xcylinder, int xhead, int head, int sector, fdc_byte *buf, int len, int deleted, int skip_deleted, int mfm, int multi);
110 extern fd_err_t fdd_format_track (FDD_765 *fdd, int head, int sectors, fdc_byte *track, fdc_byte filler);
111 extern int      fdd_ready        (FDD_765 *fdd);
112 
113 /*
114  * ############################################################################
115  * FDC declarations
116  * ############################################################################
117  */
118 typedef struct fdc_765        FDC_765;
119 typedef void (*FDC_ISR)(FDC_765 *fdc, int status);
120 
121 extern void fdc_init_impl (FDC_765 *fdc);
122 extern void fdc_reset_impl(FDC_765 *fdc);
123 
124 extern fdc_byte fdc_rd_stat  (FDC_765 *fdc);
125 extern fdc_byte fdc_rd_data  (FDC_765 *fdc);
126 extern void     fdc_wr_data  (FDC_765 *fdc, fdc_byte value);
127 extern void     fdc_set_motor(FDC_765 *fdc, fdc_byte state);
128 extern void     fdc_tick     (FDC_765 *fdc);
129 
130 struct fdc_765 {
131   FDC_ISR  fdc_isr;
132   FDD_765 *fdc_drive[4];
133   guint8 unit_id;
134   guint8 head_id;
135   struct {
136     gint   cmd;       /* current command           */
137     guint8 msr;       /* main status register      */
138     guint8 st0;       /* status register: ST0      */
139     guint8 st1;       /* status register: ST1      */
140     guint8 st2;       /* status register: ST2      */
141     guint8 st3;       /* status register: ST3      */
142     guint8 srt;       /* Step Rate Time            */
143     guint8 hlt;       /* Head Load Time            */
144     guint8 hut;       /* Head Unload Time          */
145     guint8 ndm;       /* Non-DMA Mode              */
146   } reg;
147   struct {
148     guint8 buf[16];   /* command buffer            */
149     gint   len;       /* command buffer length     */
150     gint   pos;       /* command buffer position   */
151   } cmd;
152   struct {
153     guint8 buf[8192]; /* execution buffer          */
154     gint   len;       /* execution buffer length   */
155     gint   pos;       /* execution buffer position */
156   } exe;
157   struct {
158     guint8 buf[16];   /* result buffer             */
159     gint   len;       /* result buffer length      */
160     gint   pos;       /* result buffer position    */
161   } res;
162   struct {
163     guint state;      /* interrupt state           */
164     guint count;      /* interrupt countdown       */
165   } isr;
166   int fdc_write_deleted;
167   int fdc_lastidread;
168 };
169 
170 #ifdef __cplusplus
171 }
172 #endif
173 
174 #endif /* __LIB765_H__ */
175