OSDN Git Service

ba7e90cbe8ef8576902e2e430c40ab140fc7920e
[csp-qt/common_source_project-fm7.git] / source / src / vm / disk.h
1 /*
2         Skelton for retropc emulator
3
4         Author : Takeda.Toshiya
5         Date   : 2006.09.16-
6
7         [ d88 image handler ]
8 */
9
10 #ifndef _DISK_H_
11 #define _DISK_H_
12
13 //#ifndef _ANY2D88
14 //#include "vm.h"
15 //#include "../emu.h"
16 #include "../common.h"
17 //#else
18 //#include "../common.h"
19 //#endif
20
21 // d88 media type
22 #define MEDIA_TYPE_2D   0x00
23 #define MEDIA_TYPE_2DD  0x10
24 #define MEDIA_TYPE_2HD  0x20
25 #define MEDIA_TYPE_144  0x30
26 #define MEDIA_TYPE_UNK  0xff
27
28 #define DRIVE_TYPE_2D   MEDIA_TYPE_2D
29 #define DRIVE_TYPE_2DD  MEDIA_TYPE_2DD
30 #define DRIVE_TYPE_2HD  MEDIA_TYPE_2HD
31 #define DRIVE_TYPE_144  MEDIA_TYPE_144
32 #define DRIVE_TYPE_UNK  MEDIA_TYPE_UNK
33
34 // this value will be stored to the state file,
35 // so don't change these definitions
36 #define SPECIAL_DISK_X1TURBO_ALPHA       1
37 #define SPECIAL_DISK_X1_BATTEN           2
38 #define SPECIAL_DISK_FM7_GAMBLER        11
39 #define SPECIAL_DISK_FM7_DEATHFORCE     12
40 #define SPECIAL_DISK_FM77AV_PSYOBLADE   13
41 #define SPECIAL_DISK_FM7_TAIYOU1        14
42 #define SPECIAL_DISK_FM7_TAIYOU2        15
43 #define SPECIAL_DISK_FM7_XANADU2_D      16
44 #define SPECIAL_DISK_FM7_RIGLAS         17
45 #define SPECIAL_DISK_FM7_FLEX           18
46
47 // d88 constant
48 #define DISK_BUFFER_SIZE        0x380000        // 3.5MB
49 #define TRACK_BUFFER_SIZE       0x080000        // 0.5MB
50
51 // physical format table for solid image
52 typedef struct {
53         int type;
54         int ncyl, nside, nsec, size;
55         bool mfm;
56 } fd_format_t;
57
58 class csp_state_utils;
59 class CSP_Logger;
60 class FILEIO;
61 class OSD;
62 class DISK
63 {
64 #ifndef _ANY2D88
65 protected:
66         EMU* emu;
67         OSD* osd;
68 #endif
69         csp_state_utils *state_entry;
70 private:
71         uint8_t buffer[DISK_BUFFER_SIZE + TRACK_BUFFER_SIZE];
72         _TCHAR orig_path[_MAX_PATH];
73         _TCHAR dest_path[_MAX_PATH];
74         fd_format_t fd_formats[32];
75
76         pair_t file_size;
77         int file_bank;
78         uint32_t orig_file_size;
79         uint32_t orig_crc32;
80         bool trim_required;
81         
82         bool is_1dd_image;
83         bool is_solid_image;
84         bool is_fdi_image;
85         uint8_t fdi_header[4096];
86         int solid_ncyl, solid_nside, solid_nsec, solid_size;
87         bool solid_mfm;
88
89         bool type_sc3000;
90         bool type_smc70;
91         bool type_x1;
92         bool type_x1turbo;
93         bool type_m5;
94         bool type_mz80b;
95         bool type_fm7;
96         bool type_fm77;
97         bool type_fm77av;
98         bool type_fm77av_2dd;
99         bool type_yis;
100         bool type_1dd;
101         bool type_any2d88;
102         
103         void set_sector_info(uint8_t *t);
104         void trim_buffer();
105         
106         // teledisk image decoder (td0)
107         bool teledisk_to_d88(FILEIO *fio);
108         
109         // imagedisk image decoder (imd)
110         bool imagedisk_to_d88(FILEIO *fio);
111         
112         // cpdread image decoder (dsk)
113         bool cpdread_to_d88(FILEIO *fio);
114         
115         // nfd r0/r1 image decoder (nfd)
116         bool nfdr0_to_d88(FILEIO *fio);
117         bool nfdr1_to_d88(FILEIO *fio);
118         
119         // solid image decoder (fdi/hdm/xdf/2d/img/sf7/tfd)
120         bool solid_to_d88(FILEIO *fio, int type, int ncyl, int nside, int nsec, int size, bool mfm);
121         void setup_fd_formats(void);
122         
123         // internal routines for track
124         bool get_track_tmp(int trk, int side);
125         bool make_track_tmp(int trk, int side);
126         bool get_sector_tmp(int trk, int side, int index);
127         bool format_track_tmp(int trk, int side);
128         
129 public:
130 #ifndef _ANY2D88
131         DISK(EMU* parent_emu) : emu(parent_emu)
132 #else
133         DISK()
134 #endif
135         {
136                 inserted = ejected = write_protected = changed = false;
137                 file_size.d = 0;
138                 sector_size.sd = sector_num.sd = 0;
139                 sector = NULL;
140                 drive_type = DRIVE_TYPE_UNK;
141                 drive_rpm = 0;
142                 drive_mfm = true;
143                 track_size = 0;
144                 static int num = 0;
145                 is_special_disk = 0;
146                 drive_num = num++;
147                 set_device_name(_T("Floppy Disk Drive #%d"), drive_num + 1);
148                 type_sc3000 = type_smc70 = type_x1 = type_x1turbo = false;
149                 type_m5 = type_mz80b = false;
150                 type_fm7 = type_fm77 = type_fm77av =false;
151                 type_fm77av_2dd = type_1dd = type_any2d88 = false;
152                 type_yis = false;
153                 open_as_1dd = false;
154                 open_as_256 = false;
155                 setup_fd_formats();
156                 //decl_state();
157         }
158         ~DISK()
159         {
160 //#ifndef _ANY2D88
161                 if(!type_any2d88) {
162                         if(inserted) {
163                                 close();
164                         }
165                 }
166 //#endif
167         }
168         
169         void open(const _TCHAR* file_path, int bank);
170         void close();
171 //#ifdef _ANY2D88
172         bool open_as_1dd;
173         bool open_as_256;
174         void save_as_d88(const _TCHAR* file_path);
175 //#endif
176         bool get_track(int trk, int side);
177         bool make_track(int trk, int side);
178         bool get_sector(int trk, int side, int index);
179         void set_deleted(bool value);
180         void set_data_crc_error(bool value);
181         void set_data_mark_missing();
182         
183         bool format_track(int trk, int side);
184         void insert_sector(uint8_t c, uint8_t h, uint8_t r, uint8_t n, bool deleted, bool data_crc_error, uint8_t fill_data, int length);
185         void sync_buffer();
186         
187         int get_max_tracks();
188         int get_rpm();
189         int get_track_size();
190         double get_usec_per_track();
191         double get_usec_per_bytes(int bytes);
192         int get_bytes_per_usec(double usec);
193         bool check_media_type();
194         
195         bool inserted;
196         bool ejected;
197         bool write_protected;
198         bool changed;
199         uint8_t media_type;
200         bool two_side;
201         int is_special_disk;
202         
203         // track
204         uint8_t track[TRACK_BUFFER_SIZE];
205         pair_t sector_num;
206         bool track_mfm;
207         bool invalid_format;
208 //      bool no_skew;
209         int cur_track, cur_side;
210         
211         int sync_position[512];
212         int am1_position[512];
213         int id_position[512];
214         int data_position[512];
215 //      int gap3_size;
216         
217         // sector
218         uint8_t* sector;
219         pair_t sector_size;
220         uint8_t id[6];
221         uint8_t density;
222         bool deleted;
223         bool addr_crc_error;
224         bool data_crc_error;
225         int _tmp_sector_offset;
226         
227         // drive
228         uint8_t drive_type;
229         int drive_rpm;
230         bool drive_mfm;
231         int track_size; // hack for YIS :-(
232         int drive_num;
233         bool correct_timing()
234         {
235                 if(type_any2d88) return false;
236 //#ifndef _ANY2D88
237                 if(type_fm7) {
238 //#if defined(_FM7) || defined(_FM8) || defined(_FM77_VARIANTS) || defined(_FM77AV_VARIANTS)
239                         if((is_special_disk == SPECIAL_DISK_FM7_TAIYOU1) || (is_special_disk == SPECIAL_DISK_FM7_TAIYOU2)) {
240                                 return true;
241                         }
242                 }
243 //#endif
244                 if(drive_num < (int)array_length(config.correct_disk_timing)) {
245                         return config.correct_disk_timing[drive_num];
246                 }
247 //#endif
248                 return false;
249         }
250         bool ignore_crc()
251         {
252                 if(type_any2d88) return false;
253 //#ifndef _ANY2D88
254                 if(drive_num < (int)array_length(config.ignore_disk_crc)) {
255                         return config.ignore_disk_crc[drive_num];
256                 }
257 //#endif
258                 return false;
259         }
260         
261         // state
262         bool process_state(FILEIO* state_fio, bool loading);
263         
264         // device name
265         void set_device_name(const _TCHAR* format, ...)
266         {
267                 if(format != NULL) {
268                         va_list ap;
269                         _TCHAR buffer[1024];
270                         
271                         va_start(ap, format);
272                         my_vstprintf_s(buffer, 1024, format, ap);
273                         va_end(ap);
274                         
275                         my_tcscpy_s(this_device_name, 128, buffer);
276 #ifdef _USE_QT
277 //                      emu->get_osd()->set_vm_node(this_device_id, buffer);
278 #endif
279                 }
280         }
281         const _TCHAR *get_device_name()
282         {
283                 return (const _TCHAR *)this_device_name;
284         }
285         _TCHAR this_device_name[128];
286 };
287
288 #endif
289