Previous 15994 Revisions Next

r11458 Friday 6th May, 2011 at 19:09:34 UTC by Miodrag Milanović
Moved formats to separate lib (no whatsnew)
[ROOT]makefile
[src/emu]emu.mak image.c image.h ioprocs.c ioprocs.h
[src/emu/imagedev]cassette.c cassette.h cassimg.c cassimg.h flopdrv.c flopdrv.h flopimg.c flopimg.h wavfile.c wavfile.h
[src/lib]lib.mak
[src/lib/formats]a26_cas.c* a26_cas.h* ace_tap.c* ace_tap.h* ami_dsk.c* ami_dsk.h* ap2_dsk.c* ap2_dsk.h* ap_dsk35.c* ap_dsk35.h* apf_apt.c* apf_apt.h* apridisk.c* apridisk.h* atarist_dsk.c* atarist_dsk.h* atom_tap.c* atom_tap.h* basicdsk.c* basicdsk.h* cassimg.c* cassimg.h* cbm_tap.c* cbm_tap.h* cgen_cas.c* cgen_cas.h* coco_cas.c* coco_cas.h* coco_dsk.c* coco_dsk.h* comx35_dsk.c* comx35_dsk.h* coupedsk.c* coupedsk.h* cpis_dsk.c* cpis_dsk.h* cqm_dsk.c* csw_cas.c* csw_cas.h* d64_dsk.c* d64_dsk.h* d81_dsk.c* d81_dsk.h* d88_dsk.c* dim_dsk.c* dim_dsk.h* dsk_dsk.c* fdi_dsk.c* flopimg.c* flopimg.h* fm7_cas.c* fm7_cas.h* fmsx_cas.c* fmsx_cas.h* g64_dsk.c* g64_dsk.h* gtp_cas.c* gtp_cas.h* hect_tap.c* hect_tap.h* imageutl.c* imageutl.h* imd_dsk.c* ioprocs.c* ioprocs.h* kim1_cas.c* kim1_cas.h* lviv_lvt.c* lviv_lvt.h* msx_dsk.c* msx_dsk.h* mz_cas.c* mz_cas.h* nes_dsk.c* nes_dsk.h* orao_cas.c* orao_cas.h* oric_dsk.c* oric_dsk.h* oric_tap.c* oric_tap.h* p6001_cas.c* p6001_cas.h* pc_dsk.c* pc_dsk.h* pmd_pmd.c* pmd_pmd.h* primoptp.c* primoptp.h* rk_cas.c* rk_cas.h* smx_dsk.c* smx_dsk.h* sorc_dsk.c* sorc_dsk.h* sord_cas.c* sord_cas.h* svi_cas.c* svi_cas.h* svi_dsk.c* svi_dsk.h* td0_dsk.c* thom_cas.c* thom_cas.h* thom_dsk.c* thom_dsk.h* ti99_dsk.c* ti99_dsk.h* trd_dsk.c* trd_dsk.h* trs_cas.c* trs_cas.h* trs_dsk.c* trs_dsk.h* tzx_cas.c* tzx_cas.h* uef_cas.c* uef_cas.h* vg5k_cas.c* vg5k_cas.h* vt_cas.c* vt_cas.h* vt_dsk.c* vt_dsk.h* wavfile.c* wavfile.h* x1_tap.c* x1_tap.h* z80ne_dsk.c* z80ne_dsk.h* zx81_p.c* zx81_p.h*
[src/lib/util]imageutl.c imageutl.h
[src/mame]mame.mak
[src/mame/formats]basicdsk.c basicdsk.h
[src/mame/machine]md_cart.c
[src/mess]mess.mak
[src/mess/drivers]mbc55x.c mz2000.c mz2500.c rmnimbus.c thomson.c vtech1.c x1.c
[src/mess/formats]a26_cas.c a26_cas.h ace_tap.c ace_tap.h ami_dsk.c ami_dsk.h ap2_dsk.c ap2_dsk.h ap_dsk35.c ap_dsk35.h apf_apt.c apf_apt.h apridisk.c apridisk.h atarist_dsk.c atarist_dsk.h atom_atm.c atom_tap.c atom_tap.h cbm_tap.c cbm_tap.h cgen_cas.c cgen_cas.h coco_cas.c coco_cas.h coco_dsk.c coco_dsk.h comx35_comx.c comx35_dsk.c comx35_dsk.h coupedsk.c coupedsk.h cpis_dsk.c cpis_dsk.h cqm_dsk.c csw_cas.c csw_cas.h d64_dsk.c d64_dsk.h d81_dsk.c d81_dsk.h d88_dsk.c dim_dsk.c dim_dsk.h dsk_dsk.c fdi_dsk.c fm7_cas.c fm7_cas.h fmsx_cas.c fmsx_cas.h g64_dsk.c g64_dsk.h gtp_cas.c gtp_cas.h hect_tap.c hect_tap.h imd_dsk.c kim1_cas.c kim1_cas.h lviv_lvt.c lviv_lvt.h msx_dsk.c msx_dsk.h mz_cas.c mz_cas.h nes_dsk.c nes_dsk.h orao_cas.c orao_cas.h oric_dsk.c oric_dsk.h oric_tap.c oric_tap.h p6001_cas.c p6001_cas.h pc_dsk.c pc_dsk.h pmd_pmd.c pmd_pmd.h primoptp.c primoptp.h rk_cas.c rk_cas.h smx_dsk.c smx_dsk.h sorc_dsk.c sorc_dsk.h sord_cas.c sord_cas.h svi_cas.c svi_cas.h svi_dsk.c svi_dsk.h td0_dsk.c thom_cas.c thom_cas.h thom_dsk.c thom_dsk.h ti99_dsk.c ti99_dsk.h trd_dsk.c trd_dsk.h trs_cas.c trs_cas.h trs_dsk.c trs_dsk.h tzx_cas.c tzx_cas.h uef_cas.c uef_cas.h vg5k_cas.c vg5k_cas.h vt_cas.c vt_cas.h vt_dsk.c vt_dsk.h x1_tap.c x1_tap.h z80ne_dsk.c z80ne_dsk.h zx81_p.c zx81_p.h
[src/mess/includes]msx.h
[src/mess/machine]990_dk.h abc830.h lux10828.h lux21046.h mtx.c smc92x4.c thomflop.h wd17xx.c
[src/mess/machine/ti99]ti99_hd.c
[src/mess/tools/castool]castool.mak
[src/mess/tools/imgtool]filtbas.c iflopimg.c iflopimg.h imgtool.c imgtool.h imgtool.mak
[src/mess/tools/imgtool/modules]amiga.c fat.c mac.c macbin.c os9.c pc_flop.c pc_hard.c prodos.c vzdos.c
[src/mess/tools/imgtool/windows]assocdlg.c wimgtool.mak
[src/mess/tools/messtest]testimgt.c

src/mame/machine/md_cart.c
r11457r11458
4545
4646
4747#include "emu.h"
48#include "imageutl.h"
48#include "formats/imageutl.h"
4949#include "cpu/m68000/m68000.h"
5050
5151#include "imagedev/cartslot.h"
src/mame/mame.mak
r11457r11458
1818LAYOUT = $(MAMEOBJ)/layout
1919MACHINE = $(MAMEOBJ)/machine
2020VIDEO = $(MAMEOBJ)/video
21FORMATS = $(MAMEOBJ)/formats
2221
2322OBJDIRS += \
2423   $(AUDIO) \
r11457r11458
2625   $(LAYOUT) \
2726   $(MACHINE) \
2827   $(VIDEO) \
29   $(FORMATS) \
3028
3129
3230
r11457r11458
356354   $(VIDEO)/avgdvg.o \
357355
358356#-------------------------------------------------
359# file formats, mostly used for MESS software
360#-------------------------------------------------
361
362$(MAMEOBJ)/formats.a: \
363   $(FORMATS)/basicdsk.o   \
364
365#-------------------------------------------------
366357# manufacturer-specific groupings for drivers
367358#-------------------------------------------------
368359
src/mame/formats/basicdsk.c
r11457r11458
1/*********************************************************************
2
3    formats/basicdsk.c
4
5    Floppy format code for basic disks
6
7*********************************************************************/
8
9#include <stdlib.h>
10#include <string.h>
11#include <assert.h>
12
13#include "basicdsk.h"
14
15static floperr_t basicdsk_read_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen);
16static floperr_t basicdsk_write_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam);
17static floperr_t basicdsk_read_indexed_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen);
18static floperr_t basicdsk_write_indexed_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam);
19static floperr_t basicdsk_get_sector_length(floppy_image *floppy, int head, int track, int sector, UINT32 *sector_length);
20static floperr_t basicdsk_get_indexed_sector_info(floppy_image *floppy, int head, int track, int sector_index, int *cylinder, int *side, int *sector, UINT32 *sector_length, unsigned long *flags);
21static int basicdsk_get_heads_per_disk(floppy_image *floppy);
22static int basicdsk_get_tracks_per_disk(floppy_image *floppy);
23static floperr_t basicdsk_format_track(floppy_image *floppy, int head, int track, option_resolution *params);
24
25
26
27#define BASICDSK_TAG   "basicdsktag"
28
29struct basicdsk_tag
30{
31   struct basicdsk_geometry geometry;
32};
33
34
35
36/********************************************************************/
37
38static const struct basicdsk_geometry *get_geometry(floppy_image *floppy)
39{
40   const struct basicdsk_tag *tag;
41   tag = (const basicdsk_tag *)floppy_tag(floppy);
42   return &tag->geometry;
43}
44
45
46
47floperr_t basicdsk_construct(floppy_image *floppy, const struct basicdsk_geometry *geometry)
48{
49   struct basicdsk_tag *tag;
50   struct FloppyCallbacks *format;
51
52   assert(geometry->heads);
53   assert(geometry->tracks);
54   assert(geometry->sectors);
55
56   tag = (struct basicdsk_tag *) floppy_create_tag(floppy, sizeof(struct basicdsk_tag));
57   if (!tag)
58      return FLOPPY_ERROR_OUTOFMEMORY;
59   tag->geometry = *geometry;
60
61   /* set up format callbacks */
62   format = floppy_callbacks(floppy);
63   format->read_sector = basicdsk_read_sector;
64   format->write_sector = basicdsk_write_sector;
65   format->read_indexed_sector = basicdsk_read_indexed_sector;
66   format->write_indexed_sector = basicdsk_write_indexed_sector;
67   format->get_sector_length = basicdsk_get_sector_length;
68   format->get_heads_per_disk = basicdsk_get_heads_per_disk;
69   format->get_tracks_per_disk = basicdsk_get_tracks_per_disk;
70   format->get_indexed_sector_info = basicdsk_get_indexed_sector_info;
71   format->format_track = basicdsk_format_track;
72
73   return FLOPPY_ERROR_SUCCESS;
74}
75
76
77
78static floperr_t get_offset(floppy_image *floppy, int head, int track, int sector, int sector_is_index, UINT64 *offset)
79{
80   const struct basicdsk_geometry *geom;
81   UINT64 offs;
82
83   geom = get_geometry(floppy);
84
85   /* translate the sector to a raw sector */
86   if (!sector_is_index)
87   {
88      sector -= geom->first_sector_id;
89   }
90
91   if (geom->translate_sector)
92      sector = geom->translate_sector(floppy, sector);
93
94   /* check to see if we are out of range */
95   if ((head < 0) || (head >= geom->heads) || (track < 0) || (track >= geom->tracks)
96         || (sector < 0) || (sector >= geom->sectors))
97      return FLOPPY_ERROR_SEEKERROR;
98
99   if (geom->translate_offset)
100      offs = geom->translate_offset(floppy, geom, track, head, sector);
101   else
102   {
103      offs = 0;
104      offs += track;
105      offs *= geom->heads;
106      offs += head;
107      offs *= geom->sectors;
108      offs += sector;
109   }
110   offs *= geom->sector_length;
111   offs += geom->offset;
112
113   if (offset)
114      *offset = offs;
115   return FLOPPY_ERROR_SUCCESS;
116}
117
118
119
120static int internal_basicdsk_translate_sector_interleave(floppy_image *floppy, int sector)
121{
122   const struct basicdsk_geometry *geom = get_geometry(floppy);
123   if (sector >= geom->sectors)
124      return sector;
125   return geom->sector_map[sector];
126}
127
128
129
130static floperr_t internal_basicdsk_read_sector(floppy_image *floppy, int head, int track, int sector, int sector_is_index, void *buffer, size_t buflen)
131{
132   UINT64 offset;
133   floperr_t err;
134
135   err = get_offset(floppy, head, track, sector, sector_is_index, &offset);
136   if (err)
137      return err;
138   floppy_image_read(floppy, buffer, offset, buflen);
139   return FLOPPY_ERROR_SUCCESS;
140}
141
142
143
144static floperr_t internal_basicdsk_write_sector(floppy_image *floppy, int head, int track, int sector, int sector_is_index, const void *buffer, size_t buflen, int ddam)
145{
146   UINT64 offset;
147   floperr_t err;
148
149   err = get_offset(floppy, head, track, sector, sector_is_index, &offset);
150   if (err)
151      return err;
152
153   floppy_image_write(floppy, buffer, offset, buflen);
154   return FLOPPY_ERROR_SUCCESS;
155}
156
157
158
159static floperr_t basicdsk_read_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen)
160{
161   return internal_basicdsk_read_sector(floppy, head, track, sector, FALSE, buffer, buflen);
162}
163
164static floperr_t basicdsk_write_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam)
165{
166   return internal_basicdsk_write_sector(floppy, head, track, sector, FALSE, buffer, buflen, ddam);
167}
168
169static floperr_t basicdsk_read_indexed_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen)
170{
171   return internal_basicdsk_read_sector(floppy, head, track, sector, TRUE, buffer, buflen);
172}
173
174static floperr_t basicdsk_write_indexed_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam)
175{
176   return internal_basicdsk_write_sector(floppy, head, track, sector, TRUE, buffer, buflen, ddam);
177}
178
179
180
181static floperr_t basicdsk_format_track(floppy_image *floppy, int head, int track, option_resolution *params)
182{
183   floperr_t err = FLOPPY_ERROR_SUCCESS;
184   UINT8 local_buffer[512];
185   void *alloc_buffer = NULL;
186   void *buffer;
187   UINT32 sector_length;
188   int sector;
189   const struct basicdsk_geometry *geometry;
190
191   geometry = get_geometry(floppy);
192
193   sector_length = geometry->sector_length;
194
195   if (sector_length > sizeof(local_buffer))
196   {
197      alloc_buffer = malloc(sector_length);
198      if (!alloc_buffer)
199      {
200         err = FLOPPY_ERROR_OUTOFMEMORY;
201         goto done;
202      }
203      buffer = alloc_buffer;
204   }
205   else
206   {
207      alloc_buffer = NULL;
208      buffer = local_buffer;
209   }
210
211   memset(buffer, floppy_get_filler(floppy), sector_length);
212
213   for (sector = 0; sector < geometry->sectors; sector++)
214   {
215      err = basicdsk_write_sector(floppy, head, track, sector + geometry->first_sector_id, buffer, sector_length, 0);
216      if (err)
217         goto done;
218   }
219
220done:
221   if (alloc_buffer)
222      free(alloc_buffer);
223   return err;
224}
225
226
227
228static int basicdsk_get_heads_per_disk(floppy_image *floppy)
229{
230   return get_geometry(floppy)->heads;
231}
232
233
234
235static int basicdsk_get_tracks_per_disk(floppy_image *floppy)
236{
237   return get_geometry(floppy)->tracks;
238}
239
240
241
242static floperr_t basicdsk_get_sector_length(floppy_image *floppy, int head, int track, int sector, UINT32 *sector_length)
243{
244   floperr_t err;
245
246   err = get_offset(floppy, head, track, sector, FALSE, NULL);
247   if (err)
248      return err;
249
250   if (sector_length)
251      *sector_length = get_geometry(floppy)->sector_length;
252   return FLOPPY_ERROR_SUCCESS;
253}
254
255
256
257static floperr_t basicdsk_get_indexed_sector_info(floppy_image *floppy, int head, int track, int sector_index, int *cylinder, int *side, int *sector, UINT32 *sector_length, unsigned long *flags)
258{
259   const struct basicdsk_geometry *geom = get_geometry(floppy);
260
261   if (geom->translate_sector)
262      sector_index = geom->translate_sector(floppy, sector_index);
263
264   sector_index += geom->first_sector_id;
265
266   if (cylinder)
267      *cylinder = track;
268   if (side)
269      *side = head;
270   if (sector)
271      *sector = sector_index;
272   if (flags) {
273      /* TODO: read DAM or DDAM and determine flags */
274      *flags = 0;
275      if (geom->get_ddam)
276         *flags = geom->get_ddam(floppy, geom, track, head, sector_index);
277   }
278   return basicdsk_get_sector_length(floppy, head, track, sector_index, sector_length);
279}
280
281
282
283/********************************************************************
284 * Generic Basicdsk Constructors
285 ********************************************************************/
286
287static void basicdsk_default_geometry(const struct FloppyFormat *format, struct basicdsk_geometry *geometry)
288{
289   optreserr_t err;
290   int sector_length;
291   memset(geometry, 0, sizeof(*geometry));
292
293   err = option_resolution_getdefault(format->param_guidelines, PARAM_HEADS,         &geometry->heads);
294   assert(!err);
295   err = option_resolution_getdefault(format->param_guidelines, PARAM_TRACKS,         &geometry->tracks);
296   assert(!err);
297   err = option_resolution_getdefault(format->param_guidelines, PARAM_SECTORS,         &geometry->sectors);
298   assert(!err);
299   err = option_resolution_getdefault(format->param_guidelines, PARAM_FIRST_SECTOR_ID,   &geometry->first_sector_id);
300   assert(!err);
301   err = option_resolution_getdefault(format->param_guidelines, PARAM_INTERLEAVE,      &geometry->interleave);
302   if (err!=0) {
303      geometry->interleave = 1;
304   }
305   err = option_resolution_getdefault(format->param_guidelines, PARAM_SECTOR_LENGTH,   &sector_length);
306   assert(!err);
307   geometry->sector_length = sector_length;
308
309   if (geometry->interleave > 1)
310   {
311      int sector = 0;
312
313      for (int i = 0; i < geometry->sectors; i++)
314      {
315         geometry->sector_map[i] = sector;
316
317         sector += geometry->interleave;
318
319         if (sector >= geometry->sectors)
320            sector -= geometry->sectors;
321      }
322
323      geometry->translate_sector = internal_basicdsk_translate_sector_interleave;
324   }
325}
326
327
328
329FLOPPY_CONSTRUCT(basicdsk_construct_default)
330{
331   struct basicdsk_geometry geometry;
332   basicdsk_default_geometry(format, &geometry);
333   return basicdsk_construct(floppy, &geometry);
334}
335
336
337
338FLOPPY_IDENTIFY(basicdsk_identify_default)
339{
340   UINT64 expected_size;
341   struct basicdsk_geometry geometry;
342
343   basicdsk_default_geometry(format, &geometry);
344
345   expected_size = geometry.sector_length;
346   expected_size *= geometry.heads;
347   expected_size *= geometry.tracks;
348   expected_size *= geometry.sectors;
349   *vote = (floppy_image_size(floppy) == expected_size) ? 100 : 0;
350   return FLOPPY_ERROR_SUCCESS;
351}
352
src/mame/formats/basicdsk.h
r11457r11458
1/*********************************************************************
2
3    formats/basicdsk.h
4
5    Floppy format code for basic disks
6
7*********************************************************************/
8
9#ifndef BASICDSK_H
10#define BASICDSK_H
11
12#include "imagedev/flopimg.h"
13
14struct basicdsk_geometry
15{
16   int heads;
17   int tracks;
18   int sectors;
19   int first_sector_id;
20   int interleave;
21   int sector_map[256];
22   UINT32 sector_length;
23   UINT64 offset;
24
25   int (*translate_sector)(floppy_image *floppy, int sector);
26   UINT64 (*translate_offset)(floppy_image *floppy, const struct basicdsk_geometry *geom, int track, int head, int sector);
27   UINT64 (*get_ddam)(floppy_image *floppy, const struct basicdsk_geometry *geom, int track, int head, int sector);
28};
29
30floperr_t basicdsk_construct(floppy_image *floppy, const struct basicdsk_geometry *geometry);
31
32FLOPPY_IDENTIFY(basicdsk_identify_default);
33FLOPPY_CONSTRUCT(basicdsk_construct_default);
34
35#endif /* BASICDSK_H */
src/emu/ioprocs.c
r11457r11458
1#include <stdio.h>
2#include <string.h>
3#include "ioprocs.h"
4
5
6/*********************************************************************
7    ioprocs implementation on stdio
8*********************************************************************/
9
10static void stdio_closeproc(void *file)
11{
12   fclose((FILE*)file);
13}
14
15static int stdio_seekproc(void *file, INT64 offset, int whence)
16{
17   return fseek((FILE*)file, (long) offset, whence);
18}
19
20static size_t stdio_readproc(void *file, void *buffer, size_t length)
21{
22   return fread(buffer, 1, length, (FILE*)file);
23}
24
25static size_t stdio_writeproc(void *file, const void *buffer, size_t length)
26{
27   return fwrite(buffer, 1, length, (FILE*)file);
28}
29
30static UINT64 stdio_filesizeproc(void *file)
31{
32   long l, sz;
33   l = ftell((FILE*)file);
34   if (fseek((FILE*)file, 0, SEEK_END))
35      return (size_t) -1;
36   sz = ftell((FILE*)file);
37   if (fseek((FILE*)file, l, SEEK_SET))
38      return (size_t) -1;
39   return (size_t) sz;
40}
41
42const struct io_procs stdio_ioprocs =
43{
44   stdio_closeproc,
45   stdio_seekproc,
46   stdio_readproc,
47   stdio_writeproc,
48   stdio_filesizeproc
49};
50
51const struct io_procs stdio_ioprocs_noclose =
52{
53   NULL,
54   stdio_seekproc,
55   stdio_readproc,
56   stdio_writeproc,
57   stdio_filesizeproc
58};
59
60
61
62/*********************************************************************
63    calls for accessing generic IO
64*********************************************************************/
65
66static void io_generic_seek(struct io_generic *generic, UINT64 offset)
67{
68   generic->procs->seekproc(generic->file, offset, SEEK_SET);
69}
70
71
72
73void io_generic_close(struct io_generic *generic)
74{
75   if (generic->procs->closeproc)
76      generic->procs->closeproc(generic->file);
77}
78
79
80
81void io_generic_read(struct io_generic *generic, void *buffer, UINT64 offset, size_t length)
82{
83   UINT64 size;
84   size_t bytes_read;
85
86   size = io_generic_size(generic);
87   if (size <= offset)
88   {
89      bytes_read = 0;
90   }
91   else
92   {
93      io_generic_seek(generic, offset);
94      bytes_read = generic->procs->readproc(generic->file, buffer, length);
95   }
96   memset(((UINT8 *) buffer) + bytes_read, generic->filler, length - bytes_read);
97}
98
99
100
101void io_generic_write(struct io_generic *generic, const void *buffer, UINT64 offset, size_t length)
102{
103   UINT64 filler_size = 0;
104   char filler_buffer[1024];
105   size_t bytes_to_write;
106   UINT64 size;
107
108   if (offset > 0x29c50)
109   {
110      offset = offset;
111   }
112
113   size = io_generic_size(generic);
114
115   if (size < offset)
116   {
117      filler_size = offset - size;
118      offset = size;
119   }
120
121   io_generic_seek(generic, offset);
122
123   if (filler_size)
124   {
125      memset(filler_buffer, generic->filler, sizeof(buffer));
126      do
127      {
128         bytes_to_write = (filler_size > sizeof(filler_buffer)) ? sizeof(filler_buffer) : (size_t) filler_size;
129         generic->procs->writeproc(generic->file, filler_buffer, bytes_to_write);
130         filler_size -= bytes_to_write;
131      }
132      while(filler_size > 0);
133   }
134
135   if (length > 0)
136      generic->procs->writeproc(generic->file, buffer, length);
137}
138
139
140
141void io_generic_write_filler(struct io_generic *generic, UINT8 filler, UINT64 offset, size_t length)
142{
143   UINT8 buffer[512];
144   size_t this_length;
145
146   memset(buffer, filler, MIN(length, sizeof(buffer)));
147
148   while(length > 0)
149   {
150      this_length = MIN(length, sizeof(buffer));
151      io_generic_write(generic, buffer, offset, this_length);
152      offset += this_length;
153      length -= this_length;
154   }
155}
156
157
158
159UINT64 io_generic_size(struct io_generic *generic)
160{
161   return generic->procs->filesizeproc(generic->file);
162}
163
164
165
src/emu/ioprocs.h
r11457r11458
1/*********************************************************************
2
3    ioprocs.h
4
5    File IO abstraction layer
6
7*********************************************************************/
8
9#ifndef IOPROCS_H
10#define IOPROCS_H
11
12#include <stdlib.h>
13#include "emu.h"
14
15
16
17/***************************************************************************
18
19    Type definitions
20
21***************************************************************************/
22
23struct io_procs
24{
25   void (*closeproc)(void *file);
26   int (*seekproc)(void *file, INT64 offset, int whence);
27   size_t (*readproc)(void *file, void *buffer, size_t length);
28   size_t (*writeproc)(void *file, const void *buffer, size_t length);
29   UINT64 (*filesizeproc)(void *file);
30};
31
32
33
34struct io_generic
35{
36   const struct io_procs *procs;
37   void *file;
38   UINT8 filler;
39};
40
41
42/***************************************************************************
43
44    Globals
45
46***************************************************************************/
47
48extern const struct io_procs stdio_ioprocs;
49extern const struct io_procs stdio_ioprocs_noclose;
50
51
52
53/***************************************************************************
54
55    Prototypes
56
57***************************************************************************/
58
59
60
61void io_generic_close(struct io_generic *generic);
62void io_generic_read(struct io_generic *generic, void *buffer, UINT64 offset, size_t length);
63void io_generic_write(struct io_generic *generic, const void *buffer, UINT64 offset, size_t length);
64void io_generic_write_filler(struct io_generic *generic, UINT8 filler, UINT64 offset, size_t length);
65UINT64 io_generic_size(struct io_generic *generic);
66
67
68
69#endif /* IOPROCS_H */
src/emu/imagedev/wavfile.h
r11457r11458
1/*********************************************************************
2
3    wavfile.h
4
5    Format code for wave (*.wav) files
6
7*********************************************************************/
8
9#ifndef WAVFILE_H
10#define WAVFILE_H
11
12#include "cassimg.h"
13
14extern const struct CassetteFormat wavfile_format;
15
16#endif /* WAVFILE_H */
src/emu/imagedev/flopimg.h
r11457r11458
1/*********************************************************************
2
3    flopimg.h
4
5    Floppy disk image abstraction code
6
7*********************************************************************/
8
9#ifndef FLOPIMG_H
10#define FLOPIMG_H
11
12#include "emu.h"
13#include "opresolv.h"
14
15
16/***************************************************************************
17
18    Constants
19
20***************************************************************************/
21
22#define FLOPPY_FLAGS_READWRITE      0
23#define FLOPPY_FLAGS_READONLY      1
24
25
26
27/***************************************************************************
28
29    Type definitions
30
31***************************************************************************/
32
33typedef enum
34{
35   FLOPPY_ERROR_SUCCESS,         /* no error */
36   FLOPPY_ERROR_INTERNAL,         /* fatal internal error */
37   FLOPPY_ERROR_UNSUPPORTED,      /* this operation is unsupported */
38   FLOPPY_ERROR_OUTOFMEMORY,      /* ran out of memory */
39   FLOPPY_ERROR_SEEKERROR,         /* attempted to seek to nonexistant location */
40   FLOPPY_ERROR_INVALIDIMAGE,      /* this image in invalid */
41   FLOPPY_ERROR_READONLY,         /* attempt to write to read-only image */
42   FLOPPY_ERROR_NOSPACE,
43   FLOPPY_ERROR_PARAMOUTOFRANGE,
44   FLOPPY_ERROR_PARAMNOTSPECIFIED
45}
46floperr_t;
47
48typedef struct _floppy_image floppy_image;
49
50struct FloppyCallbacks
51{
52   floperr_t (*read_sector)(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen);
53   floperr_t (*write_sector)(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam);
54   floperr_t (*read_indexed_sector)(floppy_image *floppy, int head, int track, int sector_index, void *buffer, size_t buflen);
55   floperr_t (*write_indexed_sector)(floppy_image *floppy, int head, int track, int sector_index, const void *buffer, size_t buflen, int ddam);
56   floperr_t (*read_track)(floppy_image *floppy, int head, int track, UINT64 offset, void *buffer, size_t buflen);
57   floperr_t (*write_track)(floppy_image *floppy, int head, int track, UINT64 offset, const void *buffer, size_t buflen);
58   floperr_t (*format_track)(floppy_image *floppy, int head, int track, option_resolution *params);
59   floperr_t (*post_format)(floppy_image *floppy, option_resolution *params);
60   int (*get_heads_per_disk)(floppy_image *floppy);
61   int (*get_tracks_per_disk)(floppy_image *floppy);
62   int (*get_sectors_per_track)(floppy_image *floppy, int head, int track);
63   UINT32 (*get_track_size)(floppy_image *floppy, int head, int track);
64   floperr_t (*get_sector_length)(floppy_image *floppy, int head, int track, int sector, UINT32 *sector_length);
65   floperr_t (*get_indexed_sector_info)(floppy_image *floppy, int head, int track, int sector_index, int *cylinder, int *side, int *sector, UINT32 *sector_length, unsigned long *flags);
66   floperr_t (*get_track_data_offset)(floppy_image *floppy, int head, int track, UINT64 *offset);
67};
68
69
70
71struct FloppyFormat
72{
73   const char *name;
74   const char *extensions;
75   const char *description;
76   floperr_t (*identify)(floppy_image *floppy, const struct FloppyFormat *format, int *vote);
77   floperr_t (*construct)(floppy_image *floppy, const struct FloppyFormat *format, option_resolution *params);
78   floperr_t (*destruct)(floppy_image *floppy, const struct FloppyFormat *format);
79   const char *param_guidelines;
80};
81
82#define FLOPPY_IDENTIFY(name)   floperr_t name(floppy_image *floppy, const struct FloppyFormat *format, int *vote)
83#define FLOPPY_CONSTRUCT(name)   floperr_t name(floppy_image *floppy, const struct FloppyFormat *format, option_resolution *params)
84#define FLOPPY_DESTRUCT(name)   floperr_t name(floppy_image *floppy, const struct FloppyFormat *format)
85
86FLOPPY_IDENTIFY(td0_dsk_identify);
87FLOPPY_CONSTRUCT(td0_dsk_construct);
88FLOPPY_DESTRUCT(td0_dsk_destruct);
89
90FLOPPY_IDENTIFY(imd_dsk_identify);
91FLOPPY_CONSTRUCT(imd_dsk_construct);
92
93FLOPPY_IDENTIFY(cqm_dsk_identify);
94FLOPPY_CONSTRUCT(cqm_dsk_construct);
95
96FLOPPY_IDENTIFY(dsk_dsk_identify);
97FLOPPY_CONSTRUCT(dsk_dsk_construct);
98
99FLOPPY_IDENTIFY(d88_dsk_identify);
100FLOPPY_CONSTRUCT(d88_dsk_construct);
101
102FLOPPY_IDENTIFY(fdi_dsk_identify);
103FLOPPY_CONSTRUCT(fdi_dsk_construct);
104
105#define FLOPPY_OPTIONS_NAME(name)   floppyoptions_##name
106
107#define FLOPPY_OPTIONS_START(name)                                    \
108   const struct FloppyFormat floppyoptions_##name[] =                        \
109   {                                                         \
110
111#define FLOPPY_OPTIONS_END0 \
112      { NULL }                     \
113   };
114
115#define FLOPPY_OPTIONS_EXTERN(name)                                    \
116   extern const struct FloppyFormat floppyoptions_##name[]                     \
117
118#define FLOPPY_OPTION(name, extensions_, description_, identify_, construct_, destruct_, ranges_)\
119   { #name, extensions_, description_, identify_, construct_, destruct_, ranges_ },            \
120
121#define FLOPPY_OPTIONS_END                                          \
122      FLOPPY_OPTION( fdi, "fdi", "Formatted Disk Image", fdi_dsk_identify, fdi_dsk_construct, NULL, NULL) \
123      FLOPPY_OPTION( td0, "td0", "Teledisk floppy disk image",   td0_dsk_identify, td0_dsk_construct, td0_dsk_destruct, NULL) \
124      FLOPPY_OPTION( imd, "imd", "IMD floppy disk image",   imd_dsk_identify, imd_dsk_construct, NULL, NULL) \
125      FLOPPY_OPTION( cqm, "cqm,dsk", "CopyQM floppy disk image",   cqm_dsk_identify, cqm_dsk_construct, NULL, NULL) \
126      FLOPPY_OPTION( dsk, "dsk", "DSK floppy disk image",   dsk_dsk_identify, dsk_dsk_construct, NULL, NULL) \
127      FLOPPY_OPTION( d88, "d77,d88,1dd", "D88 Floppy Disk image", d88_dsk_identify, d88_dsk_construct, NULL, NULL) \
128   FLOPPY_OPTIONS_END0
129
130FLOPPY_OPTIONS_EXTERN(default);
131
132#define PARAM_END            '\0'
133#define PARAM_HEADS            'H'
134#define PARAM_TRACKS         'T'
135#define PARAM_SECTORS         'S'
136#define PARAM_SECTOR_LENGTH      'L'
137#define PARAM_INTERLEAVE      'I'
138#define PARAM_FIRST_SECTOR_ID   'F'
139
140#define HEADS(range)         "H" #range
141#define TRACKS(range)         "T" #range
142#define SECTORS(range)         "S" #range
143#define SECTOR_LENGTH(range)   "L" #range
144#define INTERLEAVE(range)      "I" #range
145#define FIRST_SECTOR_ID(range)   "F" #range
146
147
148/***************************************************************************
149
150    Prototypes
151
152***************************************************************************/
153
154OPTION_GUIDE_EXTERN(floppy_option_guide);
155
156/* opening, closing and creating of floppy images */
157floperr_t floppy_open(void *fp, const struct io_procs *procs, const char *extension, const struct FloppyFormat *format, int flags, floppy_image **outfloppy);
158floperr_t floppy_open_choices(void *fp, const struct io_procs *procs, const char *extension, const struct FloppyFormat *formats, int flags, floppy_image **outfloppy);
159floperr_t floppy_create(void *fp, const struct io_procs *procs, const struct FloppyFormat *format, option_resolution *parameters, floppy_image **outfloppy);
160void floppy_close(floppy_image *floppy);
161
162/* useful for identifying a floppy image */
163floperr_t floppy_identify(void *fp, const struct io_procs *procs, const char *extension,
164   const struct FloppyFormat *formats, int *identified_format);
165
166/* functions useful within format constructors */
167void *floppy_tag(floppy_image *floppy);
168void *floppy_create_tag(floppy_image *floppy, size_t tagsize);
169struct FloppyCallbacks *floppy_callbacks(floppy_image *floppy);
170UINT8 floppy_get_filler(floppy_image *floppy);
171void floppy_set_filler(floppy_image *floppy, UINT8 filler);
172
173/* calls for accessing disk image data */
174floperr_t floppy_read_sector(floppy_image *floppy, int head, int track, int sector, int offset, void *buffer, size_t buffer_len);
175floperr_t floppy_write_sector(floppy_image *floppy, int head, int track, int sector, int offset, const void *buffer, size_t buffer_len, int ddam);
176floperr_t floppy_read_indexed_sector(floppy_image *floppy, int head, int track, int sector_index, int offset, void *buffer, size_t buffer_len);
177floperr_t floppy_write_indexed_sector(floppy_image *floppy, int head, int track, int sector_index, int offset, const void *buffer, size_t buffer_len, int ddam);
178floperr_t floppy_read_track(floppy_image *floppy, int head, int track, void *buffer, size_t buffer_len);
179floperr_t floppy_write_track(floppy_image *floppy, int head, int track, const void *buffer, size_t buffer_len);
180floperr_t floppy_read_track_data(floppy_image *floppy, int head, int track, void *buffer, size_t buffer_len);
181floperr_t floppy_write_track_data(floppy_image *floppy, int head, int track, const void *buffer, size_t buffer_len);
182floperr_t floppy_format_track(floppy_image *floppy, int head, int track, option_resolution *params);
183int floppy_get_tracks_per_disk(floppy_image *floppy);
184int floppy_get_heads_per_disk(floppy_image *floppy);
185UINT32 floppy_get_track_size(floppy_image *floppy, int head, int track);
186floperr_t floppy_get_sector_length(floppy_image *floppy, int head, int track, int sector, UINT32 *sector_length);
187floperr_t floppy_get_indexed_sector_info(floppy_image *floppy, int head, int track, int sector_index, int *cylinder, int *side, int *sector, UINT32 *sector_length, unsigned long *flags);
188floperr_t floppy_get_sector_count(floppy_image *floppy, int head, int track, int *sector_count);
189floperr_t floppy_load_track(floppy_image *floppy, int head, int track, int dirtify, void **track_data, size_t *track_length);
190int floppy_is_read_only(floppy_image *floppy);
191UINT8 floppy_random_byte(floppy_image *floppy);
192
193/* accessors for meta information about the image */
194const char *floppy_format_description(floppy_image *floppy);
195
196/* calls for accessing the raw disk image */
197void floppy_image_read(floppy_image *floppy, void *buffer, UINT64 offset, size_t length);
198void floppy_image_write(floppy_image *floppy, const void *buffer, UINT64 offset, size_t length);
199void floppy_image_write_filler(floppy_image *floppy, UINT8 filler, UINT64 offset, size_t length);
200UINT64 floppy_image_size(floppy_image *floppy);
201
202/* misc */
203const char *floppy_error(floperr_t err);
204
205
206#endif /* FLOPIMG_H */
207
src/emu/imagedev/cassimg.c
r11457r11458
1/*********************************************************************
2
3    cassimg.c
4
5    Cassette tape image abstraction code
6
7*********************************************************************/
8
9
10#include <string.h>
11
12#include "emu.h"
13#include "imageutl.h"
14#include "pool.h"
15#include "cassimg.h"
16
17
18/* debugging parameters */
19#define LOG_PUT_SAMPLES         0
20#define DUMP_CASSETTES         0
21
22#define SAMPLES_PER_BLOCK      0x40000
23#define CASSETTE_FLAG_DIRTY      0x10000
24
25
26CASSETTE_FORMATLIST_START(cassette_default_formats)
27CASSETTE_FORMATLIST_END
28
29
30
31/*********************************************************************
32    helper code
33*********************************************************************/
34
35static double map_double(double d, UINT64 low, UINT64 high, UINT64 value)
36{
37#if defined(_MSC_VER) && (_MSC_VER <= 1200)
38   /* casting unsigned __int64 to double is not supported on VC6 or before */
39   return d * (INT64)(value - low) / (INT64)(high - low);
40#else
41   return d * (value - low) / (high - low);
42#endif
43}
44
45
46
47static size_t waveform_bytes_per_sample(int waveform_flags)
48{
49   return (size_t) (1 << ((waveform_flags & 0x06) / 2));
50}
51
52
53
54/*********************************************************************
55    extrapolation and interpolation
56*********************************************************************/
57
58static INT32 extrapolate8(INT8 value)
59{
60   return ((INT32) value) << 24;
61}
62
63static INT32 extrapolate16(INT16 value)
64{
65   return ((INT32) value) << 16;
66}
67
68static INT8 interpolate8(INT32 value)
69{
70   return (INT8) (value >> 24);
71}
72
73static INT16 interpolate16(INT32 value)
74{
75   return (INT16) (value >> 16);
76}
77
78
79
80/*********************************************************************
81    initialization and termination
82*********************************************************************/
83
84static cassette_image *cassette_init(const struct CassetteFormat *format, void *file, const struct io_procs *procs, int flags)
85{
86   cassette_image *cassette;
87
88   cassette = (cassette_image *)malloc(sizeof(struct _cassette_image));
89   if (!cassette)
90      return NULL;
91
92   memset(cassette, 0, sizeof(*cassette));
93   cassette->format = format;
94   cassette->io.file = file;
95   cassette->io.procs = procs;
96   cassette->flags = flags;
97   cassette->pool = pool_alloc_lib(NULL);
98   return cassette;
99}
100
101
102
103static void cassette_finishinit(casserr_t err, cassette_image *cassette, cassette_image **outcassette)
104{
105   if (cassette && (err || !outcassette))
106   {
107      cassette_close(cassette);
108      cassette = NULL;
109   }
110   if (outcassette)
111      *outcassette = cassette;
112}
113
114
115
116static int good_format(const struct CassetteFormat *format, const char *extension, int flags)
117{
118   if (extension && !image_find_extension(format->extensions, extension))
119      return FALSE;
120   if (((flags & CASSETTE_FLAG_READONLY) == 0) && !format->save)
121      return FALSE;
122   return TRUE;
123}
124
125
126
127casserr_t cassette_open_choices(void *file, const struct io_procs *procs, const char *extension,
128   const struct CassetteFormat *const *formats, int flags, cassette_image **outcassette)
129{
130   casserr_t err;
131   cassette_image *cassette;
132   const struct CassetteFormat *format;
133   struct CassetteOptions opts = {0, };
134   int i;
135
136   /* if not specified, use the dummy arguments */
137   if (!formats)
138      formats = cassette_default_formats;
139
140   /* create the cassette object */
141   cassette = cassette_init(NULL, file, procs, flags);
142   if (!cassette)
143   {
144      err = CASSETTE_ERROR_OUTOFMEMORY;
145      goto done;
146   }
147
148   /* identify the image */
149   format = NULL;
150   for (i = 0; !format && formats[i]; i++)
151   {
152      if (good_format(formats[i], extension, flags))
153      {
154         format = formats[i];
155         memset(&opts, 0, sizeof(opts));
156         err = format->identify(cassette, &opts);
157         if (err == CASSETTE_ERROR_INVALIDIMAGE)
158            format = NULL;
159         else if (err)
160            goto done;
161      }
162   }
163
164   /* have we found a proper format */
165   if (!format)
166   {
167      err = CASSETTE_ERROR_INVALIDIMAGE;
168      goto done;
169   }
170   cassette->format = format;
171
172   /* read the options */
173   cassette->channels = opts.channels;
174   cassette->sample_frequency = opts.sample_frequency;
175
176   /* load the image */
177   err = format->load(cassette);
178   if (err)
179      goto done;
180
181   /* success */
182   cassette->flags &= ~CASSETTE_FLAG_DIRTY;
183   err = CASSETTE_ERROR_SUCCESS;
184
185done:
186   cassette_finishinit(err, cassette, outcassette);
187   return err;
188}
189
190
191
192casserr_t cassette_open(void *file, const struct io_procs *procs,
193   const struct CassetteFormat *format, int flags, cassette_image **outcassette)
194{
195   const struct CassetteFormat *formats[2];
196   formats[0] = format;
197   formats[1] = NULL;
198   return cassette_open_choices(file, procs, NULL, formats, flags, outcassette);
199}
200
201
202
203casserr_t cassette_create(void *file, const struct io_procs *procs, const struct CassetteFormat *format,
204   const struct CassetteOptions *opts, int flags, cassette_image **outcassette)
205{
206   casserr_t err;
207   cassette_image *cassette;
208   static const struct CassetteOptions default_options = { 1, 16, 44100 };
209
210   /* cannot create to a read only image */
211   if (flags & CASSETTE_FLAG_READONLY)
212      return CASSETTE_ERROR_INVALIDIMAGE;
213
214   /* is this a good format? */
215   if (!good_format(format, NULL, flags))
216      return CASSETTE_ERROR_INVALIDIMAGE;
217
218   /* normalize arguments */
219   if (!opts)
220      opts = &default_options;
221
222   /* create the cassette object */
223   cassette = cassette_init(format, file, procs, flags);
224   if (!cassette)
225   {
226      err = CASSETTE_ERROR_OUTOFMEMORY;
227      goto done;
228   }
229
230   /* read the options */
231   cassette->channels = opts->channels;
232   cassette->sample_frequency = opts->sample_frequency;
233
234   err = CASSETTE_ERROR_SUCCESS;
235
236done:
237   cassette_finishinit(err, cassette, outcassette);
238   return err;
239}
240
241
242
243static casserr_t cassette_perform_save(cassette_image *cassette)
244{
245   struct CassetteInfo info;
246   cassette_get_info(cassette, &info);
247   return cassette->format->save(cassette, &info);
248}
249
250
251
252casserr_t cassette_save(cassette_image *cassette)
253{
254   casserr_t err;
255
256   if (!cassette->format || !cassette->format->save)
257      return CASSETTE_ERROR_UNSUPPORTED;
258
259   err = cassette_perform_save(cassette);
260   if (err)
261      return err;
262
263   cassette->flags &= ~CASSETTE_FLAG_DIRTY;
264   return CASSETTE_ERROR_SUCCESS;
265}
266
267
268
269void cassette_get_info(cassette_image *cassette, struct CassetteInfo *info)
270{
271   memset(info, 0, sizeof(*info));
272   info->channels = cassette->channels;
273   info->sample_count = cassette->sample_count;
274   info->sample_frequency = cassette->sample_frequency;
275   info->bits_per_sample = (int) waveform_bytes_per_sample(cassette->flags) * 8;
276}
277
278
279
280void cassette_close(cassette_image *cassette)
281{
282   if (cassette)
283   {
284      if ((cassette->flags & CASSETTE_FLAG_DIRTY) && (cassette->flags & CASSETTE_FLAG_SAVEONEXIT))
285         cassette_save(cassette);
286      pool_free_lib(cassette->pool);
287      free(cassette);
288   }
289}
290
291
292
293void cassette_change(cassette_image *cassette, void *file, const struct io_procs *procs, const struct CassetteFormat *format, int flags)
294{
295   if ((flags & CASSETTE_FLAG_READONLY) == 0)
296      flags |= CASSETTE_FLAG_DIRTY;
297   cassette->io.file = file;
298   cassette->io.procs = procs;
299   cassette->format = format;
300   cassette->flags = flags;
301}
302
303
304
305/*********************************************************************
306    calls for accessing the raw cassette image
307*********************************************************************/
308
309void cassette_image_read(cassette_image *cassette, void *buffer, UINT64 offset, size_t length)
310{
311   io_generic_read(&cassette->io, buffer, offset, length);
312}
313
314
315
316void cassette_image_write(cassette_image *cassette, const void *buffer, UINT64 offset, size_t length)
317{
318   io_generic_write(&cassette->io, buffer, offset, length);
319}
320
321
322
323UINT64 cassette_image_size(cassette_image *cassette)
324{
325   return io_generic_size(&cassette->io);
326}
327
328
329
330/*********************************************************************
331    waveform accesses
332*********************************************************************/
333
334struct manipulation_ranges
335{
336   int channel_first;
337   int channel_last;
338   size_t sample_first;
339   size_t sample_last;
340};
341
342
343
344static size_t my_round(double d)
345{
346   size_t result;
347   d += 0.5;
348   result = (size_t) d;
349   return result;
350}
351
352
353
354static casserr_t compute_manipulation_ranges(cassette_image *cassette, int channel,
355   double time_index, double sample_period, struct manipulation_ranges *ranges)
356{
357   if (channel < 0)
358   {
359      ranges->channel_first = 0;
360      ranges->channel_last = cassette->channels - 1;
361   }
362   else
363   {
364      ranges->channel_first = channel;
365      ranges->channel_last = channel;
366   }
367
368   ranges->sample_first = my_round(time_index * cassette->sample_frequency);
369   ranges->sample_last = my_round((time_index + sample_period) * cassette->sample_frequency);
370
371   if (ranges->sample_last > ranges->sample_first)
372      ranges->sample_last--;
373
374   return CASSETTE_ERROR_SUCCESS;
375}
376
377
378
379static casserr_t lookup_sample(cassette_image *cassette, int channel, size_t sample, int allocate, INT32 **ptr)
380{
381   size_t sample_block;
382   size_t sample_index;
383   size_t sample_size;
384   size_t new_block_count;
385   size_t new_block_sample_count;
386   INT32 *new_block;
387   struct sample_block *new_blocks;
388   struct sample_block *block;
389
390   *ptr = NULL;
391   sample_block = (sample / SAMPLES_PER_BLOCK) * cassette->channels + channel;
392   sample_index = sample % SAMPLES_PER_BLOCK;
393   sample_size = sizeof(block->block[0]);
394
395   /* is this block beyond the edge of our waveform? */
396   if (sample_block >= cassette->block_count)
397   {
398      if (!allocate)
399         return CASSETTE_ERROR_SUCCESS;
400
401      /* allocate new blocks */
402      new_block_count = sample_block + 1;
403      new_blocks = (struct sample_block *)pool_realloc_lib(cassette->pool, cassette->blocks, new_block_count * sizeof(cassette->blocks[0]));
404      if (!new_blocks)
405         return CASSETTE_ERROR_OUTOFMEMORY;
406
407      cassette->blocks = new_blocks;
408      memset(&cassette->blocks[cassette->block_count], 0, (new_block_count - cassette->block_count) * sizeof(cassette->blocks[0]));
409      cassette->block_count = new_block_count;
410   }
411
412   block = &cassette->blocks[sample_block];
413
414   /* is this sample access off the current block? */
415   if (sample_index >= block->sample_count)
416   {
417      if (!allocate)
418         return CASSETTE_ERROR_SUCCESS;
419
420      new_block_sample_count = SAMPLES_PER_BLOCK;
421
422      new_block = (INT32*)pool_realloc_lib(cassette->pool, block->block, new_block_sample_count * sample_size);
423      if (!new_block)
424         return CASSETTE_ERROR_OUTOFMEMORY;
425
426      block->block = new_block;
427      memset(&block->block[block->sample_count], 0, (new_block_sample_count - block->sample_count) * sample_size);
428      block->sample_count = new_block_sample_count;
429   }
430
431   *ptr = &block->block[sample_index];
432   return CASSETTE_ERROR_SUCCESS;
433}
434
435
436
437/*********************************************************************
438    waveform accesses
439*********************************************************************/
440
441casserr_t cassette_get_samples(cassette_image *cassette, int channel,
442   double time_index, double sample_period, size_t sample_count, size_t sample_bytes,
443   void *samples, int waveform_flags)
444{
445   casserr_t err;
446   struct manipulation_ranges ranges;
447   size_t sample_index;
448   size_t cassette_sample_index;
449   UINT8 *dest_ptr;
450   const INT32 *source_ptr;
451   double d;
452   INT16 word;
453   INT32 dword;
454   INT64 sum;
455
456   assert(cassette);
457
458   err = compute_manipulation_ranges(cassette, channel, time_index, sample_period, &ranges);
459   if (err)
460      return err;
461
462   for (sample_index = 0; sample_index < sample_count; sample_index++)
463   {
464      sum = 0;
465
466      for (channel = ranges.channel_first; channel <= ranges.channel_last; channel++)
467      {
468         /* find the sample that we are putting */
469         d = map_double(ranges.sample_last + 1 - ranges.sample_first, 0, sample_count, sample_index) + ranges.sample_first;
470         cassette_sample_index = (size_t) d;
471         err = lookup_sample(cassette, channel, cassette_sample_index, TRUE, (INT32 **) &source_ptr);
472         if (err)
473            return err;
474
475         sum += *source_ptr;
476      }
477
478      /* average out the samples */
479      sum /= (ranges.channel_last + 1 - ranges.channel_first);
480
481      /* and write out the result */
482      dest_ptr = (UINT8*)samples;
483      dest_ptr += waveform_bytes_per_sample(waveform_flags) * sample_index;
484      switch(waveform_bytes_per_sample(waveform_flags))
485      {
486         case 1:
487            *((INT8 *) dest_ptr) = interpolate8(sum);
488            break;
489         case 2:
490            word = interpolate16(sum);
491            if (waveform_flags & CASSETTE_WAVEFORM_ENDIAN_FLIP)
492               word = FLIPENDIAN_INT16(word);
493            *((INT16 *) dest_ptr) = word;
494            break;
495         case 4:
496            dword = sum;
497            if (waveform_flags & CASSETTE_WAVEFORM_ENDIAN_FLIP)
498               dword = FLIPENDIAN_INT32(dword);
499            *((INT32 *) dest_ptr) = dword;
500            break;
501      }
502   }
503   return CASSETTE_ERROR_SUCCESS;
504}
505
506
507
508casserr_t cassette_put_samples(cassette_image *cassette, int channel,
509   double time_index, double sample_period, size_t sample_count, size_t sample_bytes,
510   const void *samples, int waveform_flags)
511{
512   casserr_t err;
513   struct manipulation_ranges ranges;
514   size_t sample_index;
515   INT32 *dest_ptr;
516   INT32 dest_value;
517   INT16 word;
518   INT32 dword;
519   const UINT8 *source_ptr;
520   double d;
521
522   if (!cassette)
523      return CASSETTE_ERROR_SUCCESS;
524
525   if (sample_period == 0)
526      return CASSETTE_ERROR_SUCCESS;
527
528   err = compute_manipulation_ranges(cassette, channel, time_index, sample_period, &ranges);
529   if (err)
530      return err;
531
532   if (cassette->sample_count < ranges.sample_last+1)
533      cassette->sample_count = ranges.sample_last + 1;
534   cassette->flags |= CASSETTE_FLAG_DIRTY;
535
536   if (LOG_PUT_SAMPLES)
537   {
538      logerror("cassette_put_samples(): Putting samples TIME=[%2.6g..%2.6g] INDEX=[%i..%i]\n",
539         time_index,            time_index + sample_period,
540         (int)ranges.sample_first,   (int)ranges.sample_last);
541   }
542
543   for (sample_index = ranges.sample_first; sample_index <= ranges.sample_last; sample_index++)
544   {
545      /* figure out the source pointer */
546      d = map_double(sample_count, ranges.sample_first, ranges.sample_last + 1, sample_index);
547      source_ptr = (const UINT8*)samples;
548      source_ptr += ((size_t) d) * sample_bytes;
549
550      /* compute the value that we are writing */
551      switch(waveform_bytes_per_sample(waveform_flags)) {
552      case 1:
553         dest_value = extrapolate8(*((INT8 *) source_ptr));
554         break;
555      case 2:
556         word = *((INT16 *) source_ptr);
557         if (waveform_flags & CASSETTE_WAVEFORM_ENDIAN_FLIP)
558            word = FLIPENDIAN_INT16(word);
559         dest_value = extrapolate16(word);
560         break;
561      case 4:
562         dword = *((INT32 *) source_ptr);
563         if (waveform_flags & CASSETTE_WAVEFORM_ENDIAN_FLIP)
564            dword = FLIPENDIAN_INT32(dword);
565         dest_value = dword;
566         break;
567      default:
568         return CASSETTE_ERROR_INTERNAL;
569      }
570
571      for (channel = ranges.channel_first; channel <= ranges.channel_last; channel++)
572      {
573         /* find the sample that we are putting */
574         err = lookup_sample(cassette, channel, sample_index, TRUE, &dest_ptr);
575         if (err)
576            return err;
577         *dest_ptr = dest_value;
578      }
579   }
580   return CASSETTE_ERROR_SUCCESS;
581}
582
583
584
585casserr_t cassette_get_sample(cassette_image *cassette, int channel,
586   double time_index, double sample_period, INT32 *sample)
587{
588   return cassette_get_samples(cassette, channel, time_index,
589       sample_period, 1, 0, sample, CASSETTE_WAVEFORM_32BIT);
590}
591
592
593
594casserr_t cassette_put_sample(cassette_image *cassette, int channel,
595   double time_index, double sample_period, INT32 sample)
596{
597   return cassette_put_samples(cassette, channel, time_index,
598       sample_period, 1, 0, &sample, CASSETTE_WAVEFORM_32BIT);
599}
600
601
602
603/*********************************************************************
604    waveform accesses to/from the raw image
605*********************************************************************/
606
607casserr_t cassette_read_samples(cassette_image *cassette, int channels, double time_index,
608   double sample_period, size_t sample_count, UINT64 offset, int waveform_flags)
609{
610   casserr_t err;
611   size_t chunk_sample_count;
612   size_t bytes_per_sample;
613   size_t sample_bytes;
614   size_t samples_loaded = 0;
615   double chunk_time_index;
616   double chunk_sample_period;
617   int channel;
618   UINT8 buffer[8192];
619
620   bytes_per_sample = waveform_bytes_per_sample(waveform_flags);
621   sample_bytes = bytes_per_sample * channels;
622
623   while(samples_loaded < sample_count)
624   {
625      chunk_sample_count = MIN(sizeof(buffer) / sample_bytes, (sample_count - samples_loaded));
626      chunk_sample_period = map_double(sample_period, 0, sample_count, chunk_sample_count);
627      chunk_time_index = time_index + map_double(sample_period, 0, sample_count, samples_loaded);
628
629      cassette_image_read(cassette, buffer, offset, chunk_sample_count * sample_bytes);
630
631      for (channel = 0; channel < channels; channel++)
632      {
633         err = cassette_put_samples(cassette, channel, chunk_time_index, chunk_sample_period,
634            chunk_sample_count, sample_bytes, &buffer[channel * bytes_per_sample], waveform_flags);
635         if (err)
636            return err;
637      }
638
639      offset += chunk_sample_count * sample_bytes;
640      samples_loaded += chunk_sample_count;
641   }
642   return CASSETTE_ERROR_SUCCESS;
643}
644
645
646
647casserr_t cassette_write_samples(cassette_image *cassette, int channels, double time_index,
648   double sample_period, size_t sample_count, UINT64 offset, int waveform_flags)
649{
650   casserr_t err;
651   size_t chunk_sample_count;
652   size_t bytes_per_sample;
653   size_t sample_bytes;
654   size_t samples_saved = 0;
655   double chunk_time_index;
656   double chunk_sample_period;
657   int channel;
658   UINT8 buffer[8192];
659
660   bytes_per_sample = waveform_bytes_per_sample(waveform_flags);
661   sample_bytes = bytes_per_sample * channels;
662
663   while(samples_saved < sample_count)
664   {
665      chunk_sample_count = MIN(sizeof(buffer) / sample_bytes, (sample_count - samples_saved));
666      chunk_sample_period = map_double(sample_period, 0, sample_count, chunk_sample_count);
667      chunk_time_index = time_index + map_double(sample_period, 0, sample_count, samples_saved);
668
669      for (channel = 0; channel < channels; channel++)
670      {
671         err = cassette_get_samples(cassette, channel, chunk_time_index, chunk_sample_period,
672            chunk_sample_count, sample_bytes, &buffer[channel * bytes_per_sample], waveform_flags);
673         if (err)
674            return err;
675      }
676
677      cassette_image_write(cassette, buffer, offset, chunk_sample_count * sample_bytes);
678
679      offset += chunk_sample_count * sample_bytes;
680      samples_saved += chunk_sample_count;
681   }
682   return CASSETTE_ERROR_SUCCESS;
683}
684
685
686
687/*********************************************************************
688    waveform accesses to/from the raw image
689*********************************************************************/
690
691static const INT8 *choose_wave(const struct CassetteModulation *modulation, size_t *wave_bytes_length)
692{
693   static const INT8 square_wave[] = { -128, 127 };
694   static const INT8 sine_wave[] = { 0, 48, 89, 117, 127, 117, 89, 48, 0, -48, -89, -117, -127, -117, -89, -48 };
695
696   if (modulation->flags & CASSETTE_MODULATION_SINEWAVE)
697   {
698      *wave_bytes_length = ARRAY_LENGTH(sine_wave);
699      return sine_wave;
700   }
701   else
702   {
703      *wave_bytes_length = ARRAY_LENGTH(square_wave);
704      return square_wave;
705   }
706}
707
708
709
710casserr_t cassette_modulation_identify(cassette_image *cassette, const struct CassetteModulation *modulation,
711   struct CassetteOptions *opts)
712{
713   size_t wave_bytes_length;
714   choose_wave(modulation, &wave_bytes_length);
715   opts->bits_per_sample = 8;
716   opts->channels = 1;
717   opts->sample_frequency = (UINT32) (MAX(modulation->zero_frequency_high, modulation->one_frequency_high) * wave_bytes_length * 2);
718   return CASSETTE_ERROR_SUCCESS;
719}
720
721
722
723casserr_t cassette_put_modulated_data(cassette_image *cassette, int channel, double time_index,
724   const void *data, size_t data_length, const struct CassetteModulation *modulation,
725   double *time_displacement)
726{
727   casserr_t err;
728   const UINT8 *data_bytes = (const UINT8 *)data;
729   const INT8 *wave_bytes;
730   size_t wave_bytes_length;
731   double total_displacement = 0.0;
732   double pulse_period;
733   double pulse_frequency;
734   UINT8 b;
735   int i;
736
737   wave_bytes = choose_wave(modulation, &wave_bytes_length);
738
739   while(data_length--)
740   {
741      b = *(data_bytes++);
742      for (i = 0; i < 8; i++)
743      {
744         pulse_frequency = (b & (1 << i)) ? modulation->one_frequency_cannonical : modulation->zero_frequency_cannonical;
745         pulse_period = 1 / pulse_frequency;
746         err = cassette_put_samples(cassette, 0, time_index, pulse_period, wave_bytes_length, 1, wave_bytes, CASSETTE_WAVEFORM_8BIT);
747         if (err)
748            goto done;
749         time_index += pulse_period;
750         total_displacement += pulse_period;
751      }
752   }
753   err = CASSETTE_ERROR_SUCCESS;
754
755done:
756   if (time_displacement)
757      *time_displacement = total_displacement;
758   return err;
759}
760
761
762
763casserr_t cassette_put_modulated_filler(cassette_image *cassette, int channel, double time_index,
764   UINT8 filler, size_t filler_length, const struct CassetteModulation *modulation,
765   double *time_displacement)
766{
767   casserr_t err;
768   double delta;
769   double total_displacement = 0.0;
770
771   while(filler_length--)
772   {
773      err = cassette_put_modulated_data(cassette, channel, time_index, &filler, 1, modulation, &delta);
774      if (err)
775         return err;
776      total_displacement += delta;
777      time_index += delta;
778   }
779
780   if (time_displacement)
781      *time_displacement = total_displacement;
782   return CASSETTE_ERROR_SUCCESS;
783}
784
785
786
787casserr_t cassette_read_modulated_data(cassette_image *cassette, int channel, double time_index,
788   UINT64 offset, UINT64 length, const struct CassetteModulation *modulation,
789   double *time_displacement)
790{
791   casserr_t err;
792   UINT8 buffer_stack[1024];
793   UINT8 *buffer;
794   UINT8 *alloc_buffer = NULL;
795   double delta;
796   double total_displacement = 0.0;
797   size_t this_length;
798   size_t buffer_length;
799
800   if (length <= sizeof(buffer_stack))
801   {
802      buffer = buffer_stack;
803      buffer_length = sizeof(buffer_stack);
804   }
805   else
806   {
807      buffer_length = MIN(length, 100000);
808      alloc_buffer = (UINT8*)malloc(buffer_length);
809      if (!alloc_buffer)
810      {
811         err = CASSETTE_ERROR_OUTOFMEMORY;
812         goto done;
813      }
814      buffer = alloc_buffer;
815   }
816
817   while(length > 0)
818   {
819      this_length = (size_t) MIN(length, buffer_length);
820      cassette_image_read(cassette, buffer, offset, this_length);
821
822      err = cassette_put_modulated_data(cassette, channel, time_index, buffer, this_length, modulation, &delta);
823      if (err)
824         goto done;
825      total_displacement += delta;
826      time_index += delta;
827      length -= this_length;
828   }
829
830   if (time_displacement)
831      *time_displacement = total_displacement;
832   err = CASSETTE_ERROR_SUCCESS;
833
834done:
835   if (alloc_buffer)
836      free(alloc_buffer);
837   return err;
838}
839
840
841
842casserr_t cassette_put_modulated_data_bit(cassette_image *cassette, int channel, double time_index,
843   UINT8 data, const struct CassetteModulation *modulation,
844   double *time_displacement)
845{
846   casserr_t err;
847   const INT8 *wave_bytes;
848   size_t wave_bytes_length;
849   double total_displacement = 0.0;
850   double pulse_period;
851   double pulse_frequency;
852
853   wave_bytes = choose_wave(modulation, &wave_bytes_length);
854
855   pulse_frequency = (data) ? modulation->one_frequency_cannonical : modulation->zero_frequency_cannonical;
856   pulse_period = 1 / pulse_frequency;
857   err = cassette_put_samples(cassette, 0, time_index, pulse_period, wave_bytes_length, 1, wave_bytes, CASSETTE_WAVEFORM_8BIT);
858   if (err)
859      goto done;
860   time_index += pulse_period;
861   total_displacement += pulse_period;
862
863   err = CASSETTE_ERROR_SUCCESS;
864
865done:
866   if (time_displacement)
867      *time_displacement = total_displacement;
868   return err;
869}
870
871
872
873/*********************************************************************
874    waveform accesses to/from the raw image
875*********************************************************************/
876
877casserr_t cassette_legacy_identify(cassette_image *cassette, struct CassetteOptions *opts,
878   const struct CassetteLegacyWaveFiller *legacy_args)
879{
880   opts->channels = 1;
881   opts->bits_per_sample = 16;
882   opts->sample_frequency = legacy_args->sample_frequency;
883   return CASSETTE_ERROR_SUCCESS;
884}
885
886
887
888casserr_t cassette_legacy_construct(cassette_image *cassette,
889   const struct CassetteLegacyWaveFiller *legacy_args)
890{
891   casserr_t err;
892   int length;
893   int sample_count;
894   void *bytes = NULL;
895   void *chunk = NULL;
896   INT16 *samples = NULL;
897   int pos = 0;
898   UINT64 offset = 0;
899   UINT64 size;
900   struct CassetteLegacyWaveFiller args;
901
902   /* sanity check the args */
903   assert(legacy_args->header_samples >= -1);
904   assert(legacy_args->trailer_samples >= 0);
905   assert(legacy_args->fill_wave);
906
907   size = cassette_image_size(cassette);
908
909   /* normalize the args */
910   args = *legacy_args;
911   if (args.chunk_size == 0)
912      args.chunk_size = 1;
913   else if (args.chunk_size < 0)
914      args.chunk_size = cassette_image_size(cassette);
915   if (args.sample_frequency == 0)
916      args.sample_frequency = 11025;
917
918   /* allocate a buffer for the binary data */
919   chunk = malloc(args.chunk_size);
920   if (!chunk)
921   {
922      err = CASSETTE_ERROR_OUTOFMEMORY;
923      goto done;
924   }
925
926   /* determine number of samples */
927   if (args.chunk_sample_calc)
928   {
929      if (size > 0x7FFFFFFF)
930      {
931         err = CASSETTE_ERROR_OUTOFMEMORY;
932         goto done;
933      }
934
935      bytes = malloc(size);
936      if (!bytes)
937      {
938         err = CASSETTE_ERROR_OUTOFMEMORY;
939         goto done;
940      }
941      cassette_image_read(cassette, bytes, 0, size);
942      sample_count = args.chunk_sample_calc((const UINT8*)bytes, (int) size);
943
944      if (args.header_samples < 0)
945         args.header_samples = sample_count;
946   }
947   else
948   {
949      sample_count = ((size + args.chunk_size - 1) / args.chunk_size)
950         * args.chunk_samples;
951   }
952   sample_count += args.header_samples + args.trailer_samples;
953
954   /* allocate a buffer for the completed samples */
955   samples = (INT16*)malloc(sample_count * sizeof(*samples));
956   if (!samples)
957   {
958      err = CASSETTE_ERROR_OUTOFMEMORY;
959      goto done;
960   }
961
962   /* if there has to be a header */
963   if (args.header_samples > 0)
964   {
965      length = args.fill_wave(samples + pos, sample_count - pos, CODE_HEADER);
966      if (length < 0)
967      {
968         err = CASSETTE_ERROR_INVALIDIMAGE;
969         goto done;
970      }
971      pos += length;
972   }
973
974   /* convert the file data to samples */
975   while((pos < sample_count) && (offset < size))
976   {
977      cassette_image_read(cassette, chunk, offset, args.chunk_size);
978      offset += args.chunk_size;
979
980      length = args.fill_wave(samples + pos, sample_count - pos, (UINT8*)chunk);
981      if (length < 0)
982      {
983         err = CASSETTE_ERROR_INVALIDIMAGE;
984         goto done;
985      }
986      pos += length;
987      if (length == 0)
988         break;
989   }
990
991   /* if there has to be a trailer */
992   if (args.trailer_samples > 0)
993   {
994      length = args.fill_wave(samples + pos, sample_count - pos, CODE_TRAILER);
995      if (length < 0)
996      {
997         err = CASSETTE_ERROR_INVALIDIMAGE;
998         goto done;
999      }
1000      pos += length;
1001   }
1002
1003   /* specify the wave */
1004   err = cassette_put_samples(cassette, 0, 0.0, ((double) pos) / args.sample_frequency,
1005      pos, 2, samples, CASSETTE_WAVEFORM_16BIT);
1006   if (err)
1007      goto done;
1008
1009   /* success! */
1010   err = CASSETTE_ERROR_SUCCESS;
1011
1012#if DUMP_CASSETTES
1013   cassette_dump(cassette, "C:\\TEMP\\CASDUMP.WAV");
1014#endif
1015
1016done:
1017   if (samples)
1018      free(samples);
1019   if (chunk)
1020      free(chunk);
1021   if (bytes)
1022      free(bytes);
1023   return err;
1024}
1025
1026
1027
1028/*********************************************************************
1029    cassette_dump
1030
1031    A debugging call to dump a casette image to a disk based wave file
1032*********************************************************************/
1033
1034void cassette_dump(cassette_image *image, const char *filename)
1035{
1036   FILE *f;
1037   struct io_generic saved_io;
1038   const struct CassetteFormat *saved_format;
1039
1040   f = fopen(filename, "wb");
1041   if (!f)
1042      return;
1043
1044   memcpy(&saved_io, &image->io, sizeof(saved_io));
1045   saved_format = image->format;
1046
1047   image->io.file = f;
1048   image->io.procs = &stdio_ioprocs_noclose;
1049   image->format = &wavfile_format;
1050   cassette_perform_save(image);
1051
1052   memcpy(&image->io, &saved_io, sizeof(saved_io));
1053   image->format = saved_format;
1054
1055   fclose(f);
1056}
1057
1058
src/emu/imagedev/cassimg.h
r11457r11458
1/*********************************************************************
2
3    cassimg.h
4
5    Cassette tape image abstraction code
6
7*********************************************************************/
8
9#ifndef CASSIMG_H
10#define CASSIMG_H
11
12#include "pool.h"
13#include "emu.h"
14
15/***************************************************************************
16
17    Constants
18
19***************************************************************************/
20
21#define CASSETTE_FLAG_READWRITE         0
22#define CASSETTE_FLAG_READONLY         1
23#define CASSETTE_FLAG_NOSAVEONEXIT      0
24#define CASSETTE_FLAG_SAVEONEXIT      2
25
26#define CASSETTE_WAVEFORM_8BIT         0
27#define CASSETTE_WAVEFORM_16BIT         2
28#define CASSETTE_WAVEFORM_16BIT_FLIP   3
29#define CASSETTE_WAVEFORM_32BIT         4
30#define CASSETTE_WAVEFORM_32BIT_FLIP   5
31#define CASSETTE_WAVEFORM_ENDIAN_FLIP   1
32
33#define CASSETTE_MODULATION_SQUAREWAVE   0
34#define CASSETTE_MODULATION_SINEWAVE   1
35
36
37#ifdef LSB_FIRST
38#define CASSETTE_WAVEFORM_16BITBE      CASSETTE_WAVEFORM_16BIT_FLIP
39#define CASSETTE_WAVEFORM_16BITLE      CASSETTE_WAVEFORM_16BIT
40#define CASSETTE_WAVEFORM_32BITBE      CASSETTE_WAVEFORM_32BIT_FLIP
41#define CASSETTE_WAVEFORM_32BITLE      CASSETTE_WAVEFORM_32BIT
42#else
43#define CASSETTE_WAVEFORM_16BITBE      CASSETTE_WAVEFORM_16BIT
44#define CASSETTE_WAVEFORM_16BITLE      CASSETTE_WAVEFORM_16BIT_FLIP
45#define CASSETTE_WAVEFORM_32BITBE      CASSETTE_WAVEFORM_32BIT
46#define CASSETTE_WAVEFORM_32BITLE      CASSETTE_WAVEFORM_32BIT_FLIP
47#endif
48
49
50/***************************************************************************
51
52    Type definitions
53
54***************************************************************************/
55
56typedef enum
57{
58   CASSETTE_ERROR_SUCCESS,         /* no error */
59   CASSETTE_ERROR_INTERNAL,      /* fatal internal error */
60   CASSETTE_ERROR_UNSUPPORTED,      /* this operation is unsupported */
61   CASSETTE_ERROR_OUTOFMEMORY,      /* ran out of memory */
62   CASSETTE_ERROR_INVALIDIMAGE,   /* invalid image */
63   CASSETTE_ERROR_INVALIDARG
64}
65casserr_t;
66
67struct sample_block
68{
69   INT32 *block;
70   size_t sample_count;
71};
72
73struct CassetteOptions
74{
75   int channels;
76   int bits_per_sample;
77   UINT32 sample_frequency;
78};
79
80struct CassetteInfo
81{
82   int channels;
83   int bits_per_sample;
84   UINT32 sample_frequency;
85   size_t sample_count;
86};
87
88struct _cassette_image
89{
90   const struct CassetteFormat *format;
91   struct io_generic io;
92   object_pool *pool;
93
94   int channels;
95   int flags;
96   UINT32 sample_frequency;
97
98   struct sample_block *blocks;
99   size_t block_count;
100   size_t sample_count;
101};
102
103typedef struct _cassette_image cassette_image;
104
105struct CassetteFormat
106{
107   const char *extensions;
108   casserr_t (*identify)(cassette_image *cassette, struct CassetteOptions *opts);
109   casserr_t (*load)(cassette_image *cassette);
110   casserr_t (*save)(cassette_image *cassette, const struct CassetteInfo *info);
111};
112
113/* used for the core modulation code */
114struct CassetteModulation
115{
116   int flags;
117   double zero_frequency_low;
118   double zero_frequency_cannonical;
119   double zero_frequency_high;
120   double one_frequency_low;
121   double one_frequency_cannonical;
122   double one_frequency_high;
123};
124
125/* code to adapt existing legacy fill_wave functions */
126struct CassetteLegacyWaveFiller
127{
128   int (*fill_wave)(INT16 *, int, UINT8 *);
129   int chunk_size;
130   int chunk_samples;
131   int (*chunk_sample_calc)(const UINT8 *bytes, int length);
132   UINT32 sample_frequency;
133   int header_samples;
134   int trailer_samples;
135};
136
137/* builtin formats */
138extern const struct CassetteFormat wavfile_format;
139
140/* macros for specifying format lists */
141#define CASSETTE_FORMATLIST_EXTERN(name)   \
142   extern const struct CassetteFormat *const name[]
143
144#define CASSETTE_FORMATLIST_START(name)      \
145   const struct CassetteFormat *const name[] =   \
146   {                              \
147      &wavfile_format,               \
148
149#define CASSETTE_FORMAT(name)            \
150      &(name),                     \
151
152#define CASSETTE_FORMATLIST_END            \
153      NULL                        \
154   };
155
156CASSETTE_FORMATLIST_EXTERN(cassette_default_formats);
157
158/***************************************************************************
159
160    Prototypes
161
162***************************************************************************/
163
164casserr_t cassette_open(void *file, const struct io_procs *procs,
165   const struct CassetteFormat *format, int flags, cassette_image **outcassette);
166casserr_t cassette_open_choices(void *file, const struct io_procs *procs, const char *extension,
167   const struct CassetteFormat *const *formats, int flags, cassette_image **outcassette);
168casserr_t cassette_create(void *file, const struct io_procs *procs, const struct CassetteFormat *format,
169   const struct CassetteOptions *opts, int flags, cassette_image **outcassette);
170casserr_t cassette_save(cassette_image *cassette);
171void cassette_close(cassette_image *cassette);
172void cassette_change(cassette_image *cassette, void *file, const struct io_procs *procs, const struct CassetteFormat *format, int flags);
173void cassette_get_info(cassette_image *cassette, struct CassetteInfo *info);
174
175/* calls for accessing the raw cassette image */
176void cassette_image_read(cassette_image *cassette, void *buffer, UINT64 offset, size_t length);
177void cassette_image_write(cassette_image *cassette, const void *buffer, UINT64 offset, size_t length);
178UINT64 cassette_image_size(cassette_image *cassette);
179
180/* waveform accesses */
181casserr_t cassette_get_samples(cassette_image *cassette, int channel,
182   double time_index, double sample_period, size_t sample_count, size_t sample_bytes,
183   void *samples, int waveform_flags);
184casserr_t cassette_put_samples(cassette_image *cassette, int channel,
185   double time_index, double sample_period, size_t sample_count, size_t sample_bytes,
186   const void *samples, int waveform_flags);
187casserr_t cassette_get_sample(cassette_image *cassette, int channel,
188   double time_index, double sample_period, INT32 *sample);
189casserr_t cassette_put_sample(cassette_image *cassette, int channel,
190   double time_index, double sample_period, INT32 sample);
191
192/* waveform accesses to/from the raw image */
193casserr_t cassette_read_samples(cassette_image *cassette, int channels, double time_index,
194   double sample_period, size_t sample_count, UINT64 offset, int waveform_flags);
195casserr_t cassette_write_samples(cassette_image *cassette, int channels, double time_index,
196   double sample_period, size_t sample_count, UINT64 offset, int waveform_flags);
197
198/* modulation support */
199casserr_t cassette_modulation_identify(cassette_image *cassette, const struct CassetteModulation *modulation,
200   struct CassetteOptions *opts);
201casserr_t cassette_put_modulated_data(cassette_image *cassette, int channel, double time_index,
202   const void *data, size_t data_length, const struct CassetteModulation *modulation,
203   double *time_displacement);
204casserr_t cassette_put_modulated_filler(cassette_image *cassette, int channel, double time_index,
205   UINT8 filler, size_t filler_length, const struct CassetteModulation *modulation,
206   double *time_displacement);
207casserr_t cassette_read_modulated_data(cassette_image *cassette, int channel, double time_index,
208   UINT64 offset, UINT64 length, const struct CassetteModulation *modulation,
209   double *time_displacement);
210casserr_t cassette_put_modulated_data_bit(cassette_image *cassette, int channel, double time_index,
211   UINT8 data, const struct CassetteModulation *modulation,
212   double *time_displacement);
213
214/* debug calls */
215void cassette_dump(cassette_image *image, const char *filename);
216
217/* legacy code support */
218#define CODE_HEADER    ((UINT8*)-1)
219#define CODE_TRAILER   ((UINT8*)-2)
220casserr_t cassette_legacy_identify(cassette_image *cassette, struct CassetteOptions *opts,
221   const struct CassetteLegacyWaveFiller *legacy_args);
222casserr_t cassette_legacy_construct(cassette_image *cassette,
223   const struct CassetteLegacyWaveFiller *legacy_args);
224
225#endif /* CASSIMG_H */
src/emu/imagedev/flopimg.c
r11457r11458
1/*********************************************************************
2
3    flopimg.c
4
5    Floppy disk image abstraction code
6
7*********************************************************************/
8
9#include <stdlib.h>
10#include <string.h>
11#include <stdio.h>
12#include <ctype.h>
13#include <assert.h>
14#include <limits.h>
15
16#include "osdepend.h"
17#include "flopimg.h"
18#include "pool.h"
19#include "imageutl.h"
20
21#define TRACK_LOADED      0x01
22#define TRACK_DIRTY         0x02
23
24
25struct _floppy_image
26{
27   struct io_generic io;
28
29   const struct FloppyFormat *floppy_option;
30   struct FloppyCallbacks format;
31
32   /* loaded track stuff */
33   int loaded_track_head;
34   int loaded_track_index;
35   UINT32 loaded_track_size;
36   void *loaded_track_data;
37   UINT8 loaded_track_status;
38   UINT8 flags;
39
40   /* tagging system */
41   object_pool *tags;
42   void *tag_data;
43};
44
45
46
47struct _floppy_params
48{
49   int param;
50   int value;
51};
52
53
54
55static floperr_t floppy_track_unload(floppy_image *floppy);
56
57OPTION_GUIDE_START(floppy_option_guide)
58   OPTION_INT('H', "heads",         "Heads")
59   OPTION_INT('T', "tracks",         "Tracks")
60   OPTION_INT('S', "sectors",         "Sectors")
61   OPTION_INT('L', "sectorlength",      "Sector Bytes")
62   OPTION_INT('I', "interleave",      "Interleave")
63   OPTION_INT('F', "firstsectorid",   "First Sector")
64OPTION_GUIDE_END
65
66
67static void floppy_close_internal(floppy_image *floppy, int close_file);
68
69/*********************************************************************
70    opening, closing and creating of floppy images
71*********************************************************************/
72
73/* basic floppy_image initialization common to floppy_open() and floppy_create() */
74static floppy_image *floppy_init(void *fp, const struct io_procs *procs, int flags)
75{
76   floppy_image *floppy;
77
78   floppy = (floppy_image *)malloc(sizeof(struct _floppy_image));
79   if (!floppy)
80      return NULL;
81
82   memset(floppy, 0, sizeof(*floppy));
83   floppy->tags = pool_alloc_lib(NULL);
84   floppy->tag_data = NULL;
85   floppy->io.file = fp;
86   floppy->io.procs = procs;
87   floppy->io.filler = 0xFF;
88   floppy->flags = (UINT8) flags;
89   return floppy;
90}
91
92
93
94/* main code for identifying and maybe opening a disk image; not exposed
95 * directly because this function is big and hideous */
96static floperr_t floppy_open_internal(void *fp, const struct io_procs *procs, const char *extension,
97   const struct FloppyFormat *floppy_options, int max_options, int flags, floppy_image **outfloppy,
98   int *outoption)
99{
100   floperr_t err;
101   floppy_image *floppy;
102   int best_option = -1;
103   int best_vote = 0;
104   int vote;
105   size_t i;
106
107   floppy = floppy_init(fp, procs, flags);
108   if (!floppy)
109   {
110      err = FLOPPY_ERROR_OUTOFMEMORY;
111      goto done;
112   }
113
114   /* vote on the best format */
115   for (i = 0; (i < max_options) && floppy_options[i].construct; i++)
116   {
117      if (!extension || !floppy_options[i].extensions || image_find_extension(floppy_options[i].extensions, extension))
118      {
119         if (floppy_options[i].identify)
120         {
121            vote = 0;
122            err = floppy_options[i].identify(floppy, &floppy_options[i], &vote);
123            if (err)
124               goto done;
125         }
126         else
127         {
128            vote = 1;
129         }
130
131         /* is this option a better one? */
132         if (vote > best_vote)
133         {
134            best_vote = vote;
135            best_option = i;
136         }
137      }
138   }
139
140   /* did we find a format? */
141   if (best_option == -1)
142   {
143      err = FLOPPY_ERROR_INVALIDIMAGE;
144      goto done;
145   }
146
147   if (outfloppy)
148   {
149      /* call the format constructor */
150      err = floppy_options[best_option].construct(floppy, &floppy_options[best_option], NULL);
151      if (err)
152         goto done;
153
154      floppy->floppy_option = &floppy_options[best_option];
155   }
156
157   err = FLOPPY_ERROR_SUCCESS;
158
159done:
160   /* if we have a floppy disk and we either errored or are not keeping it, close it */
161   if (floppy && (!outfloppy || err))
162   {
163      floppy_close_internal(floppy, FALSE);
164      floppy = NULL;
165   }
166
167   if (outoption)
168      *outoption = err ? -1 : best_option;
169   if (outfloppy)
170      *outfloppy = floppy;
171   return err;
172}
173
174
175
176floperr_t floppy_identify(void *fp, const struct io_procs *procs, const char *extension,
177   const struct FloppyFormat *formats, int *identified_format)
178{
179   return floppy_open_internal(fp, procs, extension, formats, INT_MAX, FLOPPY_FLAGS_READONLY, NULL, identified_format);
180}
181
182
183
184floperr_t floppy_open(void *fp, const struct io_procs *procs, const char *extension,
185   const struct FloppyFormat *format, int flags, floppy_image **outfloppy)
186{
187   return floppy_open_internal(fp, procs, extension, format, 1, flags, outfloppy, NULL);
188}
189
190
191
192floperr_t floppy_open_choices(void *fp, const struct io_procs *procs, const char *extension,
193   const struct FloppyFormat *formats, int flags, floppy_image **outfloppy)
194{
195   return floppy_open_internal(fp, procs, extension, formats, INT_MAX, flags, outfloppy, NULL);
196}
197
198
199
200static floperr_t option_to_floppy_error(optreserr_t oerr)
201{
202   floperr_t err;
203   switch(oerr) {
204   case OPTIONRESOLUTION_ERROR_SUCCESS:
205      err = FLOPPY_ERROR_SUCCESS;
206      break;
207   case OPTIONRESOLUTION_ERROR_OUTOFMEMORY:
208      err = FLOPPY_ERROR_OUTOFMEMORY;
209      break;
210   case OPTIONRESOLUTION_ERROR_PARAMOUTOFRANGE:
211   case OPTIONRESOLUTION_ERROR_PARAMNOTSPECIFIED:
212   case OPTIONRESOLUTION_ERROR_PARAMNOTFOUND:
213   case OPTIONRESOLUTION_ERROR_PARAMALREADYSPECIFIED:
214   case OPTIONRESOLUTION_ERROR_BADPARAM:
215   case OPTIONRESOLUTION_ERROR_SYNTAX:
216   default:
217      err = FLOPPY_ERROR_INTERNAL;
218      break;
219   };
220   return err;
221}
222
223
224
225floperr_t floppy_create(void *fp, const struct io_procs *procs, const struct FloppyFormat *format, option_resolution *parameters, floppy_image **outfloppy)
226{
227   floppy_image *floppy = NULL;
228   optreserr_t oerr;
229   floperr_t err;
230   int heads, tracks, h, t;
231   option_resolution *alloc_resolution = NULL;
232
233   assert(format);
234
235   /* create the new image */
236   floppy = floppy_init(fp, procs, 0);
237   if (!floppy)
238   {
239      err = FLOPPY_ERROR_OUTOFMEMORY;
240      goto done;
241   }
242
243   /* if this format expects creation parameters and none were specified, create some */
244   if (!parameters && format->param_guidelines)
245   {
246      alloc_resolution = option_resolution_create(floppy_option_guide, format->param_guidelines);
247      if (!alloc_resolution)
248      {
249         err = FLOPPY_ERROR_OUTOFMEMORY;
250         goto done;
251      }
252      parameters = alloc_resolution;
253   }
254
255   /* finish the parameters, if specified */
256   if (parameters)
257   {
258      oerr = option_resolution_finish(parameters);
259      if (oerr)
260      {
261         err = option_to_floppy_error(oerr);
262         goto done;
263      }
264   }
265
266   /* call the format constructor */
267   err = format->construct(floppy, format, parameters);
268   if (err)
269      goto done;
270
271   /* format the disk, ignoring if formatting not implemented */
272   if (floppy->format.format_track)
273   {
274      heads = floppy_get_heads_per_disk(floppy);
275      tracks = floppy_get_tracks_per_disk(floppy);
276
277      for (h = 0; h < heads; h++)
278      {
279         for (t = 0; t < tracks; t++)
280         {
281            err = floppy->format.format_track(floppy, h, t, parameters);
282            if (err)
283               goto done;
284         }
285      }
286   }
287
288   /* call the post_format function, if present */
289   if (floppy->format.post_format)
290   {
291      err = floppy->format.post_format(floppy, parameters);
292      if (err)
293         goto done;
294   }
295
296   floppy->floppy_option = format;
297   err = FLOPPY_ERROR_SUCCESS;
298
299done:
300   if (err && floppy)
301   {
302      floppy_close_internal(floppy, FALSE);
303      floppy = NULL;
304   }
305
306   if (outfloppy)
307      *outfloppy = floppy;
308   else if (floppy)
309      floppy_close_internal(floppy, FALSE);
310
311   if (alloc_resolution)
312      option_resolution_close(alloc_resolution);
313   return err;
314}
315
316
317
318static void floppy_close_internal(floppy_image *floppy, int close_file)
319{
320   if (floppy) {
321      floppy_track_unload(floppy);
322     
323      if(floppy->floppy_option->destruct)
324         floppy->floppy_option->destruct(floppy, floppy->floppy_option);
325      if (close_file)
326         io_generic_close(&floppy->io);
327      if (floppy->loaded_track_data)
328         free(floppy->loaded_track_data);
329      pool_free_lib(floppy->tags);
330
331      free(floppy);
332   }
333}
334
335
336
337void floppy_close(floppy_image *floppy)
338{
339   floppy_close_internal(floppy, TRUE);
340}
341
342
343
344/*********************************************************************
345    functions useful in format constructors
346*********************************************************************/
347
348struct FloppyCallbacks *floppy_callbacks(floppy_image *floppy)
349{
350   assert(floppy);
351   return &floppy->format;
352}
353
354
355
356void *floppy_tag(floppy_image *floppy)
357{
358   assert(floppy);
359   return floppy->tag_data;
360}
361
362
363
364void *floppy_create_tag(floppy_image *floppy, size_t tagsize)
365{
366   floppy->tag_data = pool_malloc_lib(floppy->tags,tagsize);
367   return floppy->tag_data;
368}
369
370
371
372UINT8 floppy_get_filler(floppy_image *floppy)
373{
374   return floppy->io.filler;
375}
376
377
378
379void floppy_set_filler(floppy_image *floppy, UINT8 filler)
380{
381   floppy->io.filler = filler;
382}
383
384
385
386/*********************************************************************
387    calls for accessing the raw disk image
388*********************************************************************/
389
390void floppy_image_read(floppy_image *floppy, void *buffer, UINT64 offset, size_t length)
391{
392   io_generic_read(&floppy->io, buffer, offset, length);
393}
394
395
396
397void floppy_image_write(floppy_image *floppy, const void *buffer, UINT64 offset, size_t length)
398{
399   io_generic_write(&floppy->io, buffer, offset, length);
400}
401
402
403
404void floppy_image_write_filler(floppy_image *floppy, UINT8 filler, UINT64 offset, size_t length)
405{
406   io_generic_write_filler(&floppy->io, filler, offset, length);
407}
408
409
410
411UINT64 floppy_image_size(floppy_image *floppy)
412{
413   return io_generic_size(&floppy->io);
414}
415
416
417
418/*********************************************************************
419    calls for accessing disk image data
420*********************************************************************/
421
422static floperr_t floppy_readwrite_sector(floppy_image *floppy, int head, int track, int sector, int offset,
423   void *buffer, size_t buffer_len, int writing, int indexed, int ddam)
424{
425   floperr_t err;
426   const struct FloppyCallbacks *fmt;
427   size_t this_buffer_len;
428   UINT8 *alloc_buf = NULL;
429   UINT32 sector_length;
430   UINT8 *buffer_ptr = (UINT8 *)buffer;
431   floperr_t (*read_sector)(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen);
432   floperr_t (*write_sector)(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam);
433
434   fmt = floppy_callbacks(floppy);
435
436   /* choose proper calls for indexed vs non-indexed */
437   if (indexed)
438   {
439      read_sector = fmt->read_indexed_sector;
440      write_sector = fmt->write_indexed_sector;
441      if (!fmt->get_indexed_sector_info)
442      {
443         err = FLOPPY_ERROR_UNSUPPORTED;
444         goto done;
445      }
446   }
447   else
448   {
449      read_sector = fmt->read_sector;
450      write_sector = fmt->write_sector;
451      if (!fmt->get_sector_length)
452      {
453         err = FLOPPY_ERROR_UNSUPPORTED;
454         goto done;
455      }
456   }
457
458   /* check to make sure that the operation is supported */
459   if (!read_sector || (writing && !write_sector))
460   {
461      err = FLOPPY_ERROR_UNSUPPORTED;
462      goto done;
463   }
464
465   /* main loop */
466   while(buffer_len > 0)
467   {
468      /* find out the size of this sector */
469      if (indexed)
470         err = fmt->get_indexed_sector_info(floppy, head, track, sector, NULL, NULL, NULL, &sector_length, NULL);
471      else
472         err = fmt->get_sector_length(floppy, head, track, sector, &sector_length);
473      if (err)
474         goto done;
475
476      /* do we even do anything with this sector? */
477      if (offset < sector_length)
478      {
479         /* ok we will be doing something */
480         if ((offset > 0) || (buffer_len < sector_length))
481         {
482            /* we will be doing an partial read/write; in other words we
483                 * will not be reading/writing a full sector */
484            if (alloc_buf) free(alloc_buf);
485            alloc_buf = (UINT8*)malloc(sector_length);
486            if (!alloc_buf)
487            {
488               err = FLOPPY_ERROR_OUTOFMEMORY;
489               goto done;
490            }
491
492            /* read the sector (we need to do this even when writing */
493            err = read_sector(floppy, head, track, sector, alloc_buf, sector_length);
494            if (err)
495               goto done;
496
497            this_buffer_len = MIN(buffer_len, sector_length - offset);
498
499            if (writing)
500            {
501               memcpy(alloc_buf + offset, buffer_ptr, this_buffer_len);
502
503               err = write_sector(floppy, head, track, sector, alloc_buf, sector_length, ddam);
504               if (err)
505                  goto done;
506            }
507            else
508            {
509               memcpy(buffer_ptr, alloc_buf + offset, this_buffer_len);
510            }
511            offset += this_buffer_len;
512            offset %= sector_length;
513         }
514         else
515         {
516            this_buffer_len = sector_length;
517
518            if (writing)
519               err = write_sector(floppy, head, track, sector, buffer_ptr, sector_length, ddam);
520            else
521               err = read_sector(floppy, head, track, sector, buffer_ptr, sector_length);
522            if (err)
523               goto done;
524         }
525      }
526      else
527      {
528         /* skip this sector */
529         offset -= sector_length;
530         this_buffer_len = 0;
531      }
532
533      buffer_ptr += this_buffer_len;
534      buffer_len -= this_buffer_len;
535      sector++;
536   }
537
538   err = FLOPPY_ERROR_SUCCESS;
539
540done:
541   if (alloc_buf)
542      free(alloc_buf);
543   return err;
544}
545
546
547
548floperr_t floppy_read_sector(floppy_image *floppy, int head, int track, int sector, int offset,   void *buffer, size_t buffer_len)
549{
550   return floppy_readwrite_sector(floppy, head, track, sector, offset, buffer, buffer_len, FALSE, FALSE, 0);
551}
552
553
554
555floperr_t floppy_write_sector(floppy_image *floppy, int head, int track, int sector, int offset, const void *buffer, size_t buffer_len, int ddam)
556{
557   return floppy_readwrite_sector(floppy, head, track, sector, offset, (void *) buffer, buffer_len, TRUE, FALSE, ddam);
558}
559
560
561
562floperr_t floppy_read_indexed_sector(floppy_image *floppy, int head, int track, int sector_index, int offset,   void *buffer, size_t buffer_len)
563{
564   return floppy_readwrite_sector(floppy, head, track, sector_index, offset, buffer, buffer_len, FALSE, TRUE, 0);
565}
566
567
568
569floperr_t floppy_write_indexed_sector(floppy_image *floppy, int head, int track, int sector_index, int offset, const void *buffer, size_t buffer_len, int ddam)
570{
571   return floppy_readwrite_sector(floppy, head, track, sector_index, offset, (void *) buffer, buffer_len, TRUE, TRUE, ddam);
572}
573
574
575static floperr_t floppy_get_track_data_offset(floppy_image *floppy, int head, int track, UINT64 *offset)
576{
577   floperr_t err;
578   const struct FloppyCallbacks *callbacks;
579
580   *offset = 0;
581   callbacks = floppy_callbacks(floppy);
582   if (callbacks->get_track_data_offset)
583   {
584      err = callbacks->get_track_data_offset(floppy, head, track, offset);
585      if (err)
586         return err;
587   }
588   return FLOPPY_ERROR_SUCCESS;
589}
590
591
592
593static floperr_t floppy_read_track_offset(floppy_image *floppy, int head, int track, UINT64 offset, void *buffer, size_t buffer_len)
594{
595   floperr_t err;
596   const struct FloppyCallbacks *format;
597
598   format = floppy_callbacks(floppy);
599
600   if (!format->read_track)
601      return FLOPPY_ERROR_UNSUPPORTED;
602
603   err = floppy_track_unload(floppy);
604   if (err)
605      return err;
606
607   err = format->read_track(floppy, head, track, offset, buffer, buffer_len);
608   if (err)
609      return err;
610
611   return FLOPPY_ERROR_SUCCESS;
612}
613
614
615
616floperr_t floppy_read_track(floppy_image *floppy, int head, int track, void *buffer, size_t buffer_len)
617{
618   return floppy_read_track_offset(floppy, head, track, 0, buffer, buffer_len);
619}
620
621
622
623floperr_t floppy_read_track_data(floppy_image *floppy, int head, int track, void *buffer, size_t buffer_len)
624{
625   floperr_t err;
626   UINT64 offset;
627
628   err = floppy_get_track_data_offset(floppy, head, track, &offset);
629   if (err)
630      return err;
631
632   return floppy_read_track_offset(floppy, head, track, offset, buffer, buffer_len);
633}
634
635
636
637static floperr_t floppy_write_track_offset(floppy_image *floppy, int head, int track, UINT64 offset, const void *buffer, size_t buffer_len)
638{
639   floperr_t err;
640
641   /* track writing supported? */
642   if (!floppy_callbacks(floppy)->write_track)
643      return FLOPPY_ERROR_UNSUPPORTED;
644
645   /* read only? */
646   if (floppy->flags & FLOPPY_FLAGS_READONLY)
647      return FLOPPY_ERROR_READONLY;
648
649   err = floppy_track_unload(floppy);
650   if (err)
651      return err;
652
653   err = floppy_callbacks(floppy)->write_track(floppy, head, track, offset, buffer, buffer_len);
654   if (err)
655      return err;
656
657   return FLOPPY_ERROR_SUCCESS;
658}
659
660
661
662floperr_t floppy_write_track(floppy_image *floppy, int head, int track, const void *buffer, size_t buffer_len)
663{
664   return floppy_write_track_offset(floppy, head, track, 0, buffer, buffer_len);
665}
666
667
668
669floperr_t floppy_write_track_data(floppy_image *floppy, int head, int track, const void *buffer, size_t buffer_len)
670{
671   floperr_t err;
672   UINT64 offset;
673
674   err = floppy_get_track_data_offset(floppy, head, track, &offset);
675   if (err)
676      return err;
677
678   return floppy_write_track_offset(floppy, head, track, offset, buffer, buffer_len);
679}
680
681
682
683floperr_t floppy_format_track(floppy_image *floppy, int head, int track, option_resolution *parameters)
684{
685   floperr_t err;
686   struct FloppyCallbacks *format;
687   option_resolution *alloc_resolution = NULL;
688   optreserr_t oerr;
689
690   /* supported? */
691   format = floppy_callbacks(floppy);
692   if (!format->format_track)
693   {
694      err = FLOPPY_ERROR_UNSUPPORTED;
695      goto done;
696   }
697
698   /* create a dummy resolution; if no parameters were specified */
699   if (!parameters)
700   {
701      alloc_resolution = option_resolution_create(floppy_option_guide, floppy->floppy_option->param_guidelines);
702      if (!alloc_resolution)
703      {
704         err = FLOPPY_ERROR_OUTOFMEMORY;
705         goto done;
706      }
707      parameters = alloc_resolution;
708   }
709
710   oerr = option_resolution_finish(parameters);
711   if (oerr)
712   {
713      err = option_to_floppy_error(oerr);
714      goto done;
715   }
716
717   err = format->format_track(floppy, head, track, parameters);
718   if (err)
719      goto done;
720
721done:
722   if (alloc_resolution)
723      option_resolution_close(alloc_resolution);
724   return err;
725}
726
727
728
729int floppy_get_tracks_per_disk(floppy_image *floppy)
730{
731   return floppy_callbacks(floppy)->get_tracks_per_disk(floppy);
732}
733
734
735
736int floppy_get_heads_per_disk(floppy_image *floppy)
737{
738   return floppy_callbacks(floppy)->get_heads_per_disk(floppy);
739}
740
741
742
743UINT32 floppy_get_track_size(floppy_image *floppy, int head, int track)
744{
745   const struct FloppyCallbacks *fmt;
746
747   fmt = floppy_callbacks(floppy);
748   if (!fmt->get_track_size)
749      return 0;
750
751   return fmt->get_track_size(floppy, head, track);
752}
753
754
755
756floperr_t floppy_get_sector_length(floppy_image *floppy, int head, int track, int sector, UINT32 *sector_length)
757{
758   const struct FloppyCallbacks *fmt;
759
760   fmt = floppy_callbacks(floppy);
761   if (!fmt->get_sector_length)
762      return FLOPPY_ERROR_UNSUPPORTED;
763
764   return fmt->get_sector_length(floppy, head, track, sector, sector_length);
765}
766
767
768
769floperr_t floppy_get_indexed_sector_info(floppy_image *floppy, int head, int track, int sector_index, int *cylinder, int *side, int *sector, UINT32 *sector_length, unsigned long *flags)
770{
771   const struct FloppyCallbacks *fmt;
772
773   fmt = floppy_callbacks(floppy);
774   if (!fmt->get_indexed_sector_info)
775      return FLOPPY_ERROR_UNSUPPORTED;
776
777   return fmt->get_indexed_sector_info(floppy, head, track, sector_index, cylinder, side, sector, sector_length, flags);
778}
779
780
781
782floperr_t floppy_get_sector_count(floppy_image *floppy, int head, int track, int *sector_count)
783{
784   floperr_t err;
785   int sector_index = 0;
786
787   do
788   {
789      err = floppy_get_indexed_sector_info(floppy, head, track, sector_index, NULL, NULL, NULL, NULL, NULL);
790      if (!err)
791         sector_index++;
792   }
793   while(!err);
794
795   if (sector_index && (err == FLOPPY_ERROR_SEEKERROR))
796      err = FLOPPY_ERROR_SUCCESS;
797   if (sector_count)
798      *sector_count = err ? 0 : sector_index;
799   return err;
800}
801
802
803
804int floppy_is_read_only(floppy_image *floppy)
805{
806   return floppy->flags & FLOPPY_FLAGS_READONLY;
807}
808
809
810
811UINT8 floppy_random_byte(floppy_image *floppy)
812{
813   /* can't use mame_rand(); this might not be in the core */
814#ifdef rand
815#undef rand
816#endif
817   return rand();
818}
819
820
821
822/*********************************************************************
823    calls for track based IO
824*********************************************************************/
825
826floperr_t floppy_load_track(floppy_image *floppy, int head, int track, int dirtify, void **track_data, size_t *track_length)
827{
828   floperr_t err;
829   void *new_loaded_track_data;
830   UINT32 track_size;
831
832   /* have we already loaded this track? */
833   if (((floppy->loaded_track_status & TRACK_LOADED) == 0) || (head != floppy->loaded_track_head) || (track != floppy->loaded_track_index))
834   {
835      err = floppy_track_unload(floppy);
836      if (err)
837         goto error;
838
839      track_size = floppy_callbacks(floppy)->get_track_size(floppy, head, track);
840
841      if (floppy->loaded_track_data) free(floppy->loaded_track_data);
842      new_loaded_track_data = malloc(track_size);
843      if (!new_loaded_track_data)
844      {
845         err = FLOPPY_ERROR_OUTOFMEMORY;
846         goto error;
847      }
848
849      floppy->loaded_track_data = new_loaded_track_data;
850      floppy->loaded_track_size = track_size;
851      floppy->loaded_track_head = head;
852      floppy->loaded_track_index = track;
853
854      err = floppy_callbacks(floppy)->read_track(floppy, floppy->loaded_track_head, floppy->loaded_track_index, 0, floppy->loaded_track_data, floppy->loaded_track_size);
855      if (err)
856         goto error;
857
858      floppy->loaded_track_status |= TRACK_LOADED | (dirtify ? TRACK_DIRTY : 0);
859   }
860   else
861      floppy->loaded_track_status |= (dirtify ? TRACK_DIRTY : 0);
862
863   if (track_data)
864      *track_data = floppy->loaded_track_data;
865   if (track_length)
866      *track_length = floppy->loaded_track_size;
867   return FLOPPY_ERROR_SUCCESS;
868
869error:
870   if (track_data)
871      *track_data = NULL;
872   if (track_length)
873      *track_length = 0;
874   return err;
875}
876
877
878
879static floperr_t floppy_track_unload(floppy_image *floppy)
880{
881   int err;
882   if (floppy->loaded_track_status & TRACK_DIRTY)
883   {
884      err = floppy_callbacks(floppy)->write_track(floppy, floppy->loaded_track_head, floppy->loaded_track_index, 0, floppy->loaded_track_data, floppy->loaded_track_size);
885      if (err)
886         return (floperr_t)err;
887   }
888
889   floppy->loaded_track_status &= ~(TRACK_LOADED | TRACK_DIRTY);
890   return FLOPPY_ERROR_SUCCESS;
891}
892
893
894
895/*********************************************************************
896    accessors for meta information about the image
897*********************************************************************/
898
899const char *floppy_format_description(floppy_image *floppy)
900{
901   return floppy->floppy_option->description;
902}
903
904
905
906/*********************************************************************
907    misc calls
908*********************************************************************/
909
910const char *floppy_error(floperr_t err)
911{
912   static const char *const error_messages[] =
913   {
914      "The operation completed successfully",
915      "Fatal internal error",
916      "This operation is unsupported",
917      "Out of memory",
918      "Seek error",
919      "Invalid image",
920      "Attempted to write to read only image",
921      "No space left on image",
922      "Parameter out of range",
923      "Required parameter not specified"
924   };
925
926   if ((err < 0) || (err >= ARRAY_LENGTH(error_messages)))
927      return NULL;
928   return error_messages[err];
929}
930
931
932FLOPPY_OPTIONS_START(default)
933FLOPPY_OPTIONS_END
src/emu/imagedev/wavfile.c
r11457r11458
1/*********************************************************************
2
3    wavfile.c
4
5    Format code for wave (*.wav) files
6
7*********************************************************************/
8
9#include <stdio.h>
10#include <assert.h>
11
12#include "wavfile.h"
13#include "cassimg.h"
14
15static const char magic1[4] = { 'R', 'I', 'F', 'F' };
16static const char magic2[4] = { 'W', 'A', 'V', 'E' };
17static const char format_tag_id[4] = { 'f', 'm', 't', ' ' };
18static const char data_tag_id[4] = { 'd', 'a', 't', 'a' };
19
20#define WAV_FORMAT_PCM      1
21
22
23
24static UINT32 get_leuint32(const void *ptr)
25{
26   UINT32 value;
27   memcpy(&value, ptr, sizeof(value));
28   return LITTLE_ENDIANIZE_INT32(value);
29}
30
31
32
33static UINT16 get_leuint16(const void *ptr)
34{
35   UINT16 value;
36   memcpy(&value, ptr, sizeof(value));
37   return LITTLE_ENDIANIZE_INT16(value);
38}
39
40
41
42static void put_leuint32(void *ptr, UINT32 value)
43{
44   value = LITTLE_ENDIANIZE_INT32(value);
45   memcpy(ptr, &value, sizeof(value));
46}
47
48
49
50static void put_leuint16(void *ptr, UINT16 value)
51{
52   value = LITTLE_ENDIANIZE_INT16(value);
53   memcpy(ptr, &value, sizeof(value));
54}
55
56
57
58static casserr_t wavfile_process(cassette_image *cassette, struct CassetteOptions *opts,
59   int read_waveform)
60{
61   UINT8 file_header[12];
62   UINT8 tag_header[8];
63   UINT8 format_tag[16];
64   UINT32 stated_size;
65   UINT64 file_size;
66   UINT32 tag_size;
67   UINT32 tag_samples;
68   UINT64 offset;
69   int format_specified = FALSE;
70
71   UINT16 format_type = 0;
72   UINT32 bytes_per_second = 0;
73   UINT16 block_align = 0;
74   int waveform_flags = 0;
75
76   /* read header */
77   cassette_image_read(cassette, file_header, 0, sizeof(file_header));
78   offset = sizeof(file_header);
79
80   /* check magic numbers */
81   if (memcmp(&file_header[0], magic1, 4))
82      return CASSETTE_ERROR_INVALIDIMAGE;
83   if (memcmp(&file_header[8], magic2, 4))
84      return CASSETTE_ERROR_INVALIDIMAGE;
85
86   /* read and sanity check size */
87   stated_size = get_leuint32(&file_header[4]) + 8;
88   file_size = cassette_image_size(cassette);
89   if (stated_size > file_size)
90      stated_size = (UINT32) file_size;
91
92   while(offset < stated_size)
93   {
94      cassette_image_read(cassette, tag_header, offset, sizeof(tag_header));
95      tag_size = get_leuint32(&tag_header[4]);
96      offset += sizeof(tag_header);
97
98      if (!memcmp(tag_header, format_tag_id, 4))
99      {
100         /* format tag */
101         if (format_specified || (tag_size < sizeof(format_tag)))
102            return CASSETTE_ERROR_INVALIDIMAGE;
103         format_specified = TRUE;
104
105         cassette_image_read(cassette, format_tag, offset, sizeof(format_tag));
106
107         format_type            = get_leuint16(&format_tag[0]);
108         opts->channels         = get_leuint16(&format_tag[2]);
109         opts->sample_frequency   = get_leuint32(&format_tag[4]);
110         bytes_per_second      = get_leuint32(&format_tag[8]);
111         block_align            = get_leuint16(&format_tag[12]);
112         opts->bits_per_sample   = get_leuint16(&format_tag[14]);
113
114         if (format_type != WAV_FORMAT_PCM)
115            return CASSETTE_ERROR_INVALIDIMAGE;
116         if (opts->sample_frequency * opts->bits_per_sample * opts->channels / 8 != bytes_per_second)
117            return CASSETTE_ERROR_INVALIDIMAGE;
118
119         switch(opts->bits_per_sample)
120         {
121            case 8:
122               waveform_flags = CASSETTE_WAVEFORM_8BIT;
123               break;
124            case 16:
125               waveform_flags = CASSETTE_WAVEFORM_16BITLE;
126               break;
127            case 32:
128               waveform_flags = CASSETTE_WAVEFORM_32BITLE;
129               break;
130            default:
131               return CASSETTE_ERROR_INVALIDIMAGE;
132         }
133      }
134      else if (!memcmp(tag_header, data_tag_id, 4))
135      {
136         /* data tag */
137         if (!format_specified)
138            return CASSETTE_ERROR_INVALIDIMAGE;
139
140         if (read_waveform)
141         {
142            tag_samples = tag_size / (opts->bits_per_sample / 8) / opts->channels;
143            cassette_read_samples(cassette, opts->channels, 0.0, tag_samples / ((double) opts->sample_frequency),
144               tag_samples, offset, waveform_flags);
145         }
146      }
147      else
148      {
149         /* ignore other tags */
150      }
151      offset += tag_size;
152   }
153
154   return CASSETTE_ERROR_SUCCESS;
155}
156
157
158
159static casserr_t wavfile_identify(cassette_image *cassette, struct CassetteOptions *opts)
160{
161   return wavfile_process(cassette, opts, FALSE);
162}
163
164
165
166static casserr_t wavfile_load(cassette_image *cassette)
167{
168   struct CassetteOptions opts;
169   memset(&opts, 0, sizeof(opts));
170   return wavfile_process(cassette, &opts, TRUE);
171}
172
173
174
175static casserr_t wavfile_save(cassette_image *cassette, const struct CassetteInfo *info)
176{
177   casserr_t err;
178   UINT8 consolidated_header[12 + 8 + 16 + 8];
179   UINT8 *header            = &consolidated_header[0];
180   UINT8 *format_tag_header   = &consolidated_header[12];
181   UINT8 *format_tag_data      = &consolidated_header[12 + 8];
182   UINT8 *data_tag_header      = &consolidated_header[12 + 8 + 16];
183   UINT32 file_size;
184   UINT32 bytes_per_second;
185   UINT16 bits_per_sample;
186   UINT32 data_size;
187   size_t bytes_per_sample = 2;
188   int waveform_flags = CASSETTE_WAVEFORM_16BITLE;
189   UINT16 block_align;
190
191   bits_per_sample = (UINT16) (bytes_per_sample * 8);
192   bytes_per_second = info->sample_frequency * bytes_per_sample * info->channels;
193   data_size = (UINT32) (info->sample_count * bytes_per_sample * info->channels);
194   file_size = data_size + sizeof(consolidated_header) - 8;
195   block_align = (UINT16) (bytes_per_sample * info->channels);
196
197   /* set up header */
198   memcpy(&header[0],               magic1, 4);
199   memcpy(&header[8],               magic2, 4);
200   put_leuint32(&header[4],         file_size);
201
202   /* set up format tag */
203   memcpy(&format_tag_header[0],      format_tag_id, 4);
204   put_leuint32(&format_tag_header[4],   16);
205   put_leuint16(&format_tag_data[0],   WAV_FORMAT_PCM);
206   put_leuint16(&format_tag_data[2],   info->channels);
207   put_leuint32(&format_tag_data[4],   info->sample_frequency);
208   put_leuint32(&format_tag_data[8],   bytes_per_second);
209   put_leuint16(&format_tag_data[12],   block_align);
210   put_leuint16(&format_tag_data[14],   bits_per_sample);
211
212   /* set up data tag */
213   memcpy(&data_tag_header[0],         data_tag_id, 4);
214   put_leuint32(&data_tag_header[4],   data_size);
215
216   /* write consolidated header */
217   cassette_image_write(cassette, consolidated_header, 0, sizeof(consolidated_header));
218
219   /* write out the actual data */
220   err = cassette_write_samples(cassette, info->channels, 0.0, info->sample_count
221      / (double) info->sample_frequency, info->sample_count, sizeof(consolidated_header),
222      waveform_flags);
223   if (err)
224      return err;
225
226   return CASSETTE_ERROR_SUCCESS;
227}
228
229
230
231const struct CassetteFormat wavfile_format =
232{
233   "wav",
234   wavfile_identify,
235   wavfile_load,
236   wavfile_save
237};
238
239
240
241/*********************************************************************
242    wavfile_testload()
243
244    This is a hokey function used to test the cassette wave loading
245    system, specifically to test that when one loads a WAV file image
246    that the resulting info queried will be the same data in the WAV.
247
248    This code has already identified some rounding errors
249*********************************************************************/
250
251#ifdef UNUSED_FUNCTION
252void wavfile_testload(const char *fname)
253{
254   cassette_image *cassette;
255   FILE *f;
256   long offset;
257   int freq, samples, i;
258   INT32 cassamp;
259   INT16 wavsamp;
260
261   f = fopen(fname, "rb");
262   if (!f)
263      return;
264
265   if (cassette_open(f, &stdio_ioprocs, &wavfile_format, CASSETTE_FLAG_READONLY, &cassette))
266   {
267      fclose(f);
268      return;
269   }
270
271   offset = 44;
272   freq = 44100;
273   samples = 5667062;
274
275   for (i = 0; i < samples; i++)
276   {
277      cassette_get_sample(cassette, 0, i / (double) freq, 0.0, &cassamp);
278
279      fseek(f, offset + i * 2, SEEK_SET);
280      fread(&wavsamp, 1, 2, f);
281      assert(cassamp == (((UINT32) wavsamp) << 16));
282   }
283
284   cassette_close(cassette);
285
286   fclose(f);
287}
288#endif
289
290
291
src/emu/imagedev/cassette.c
r11457r11458
77*********************************************************************/
88
99#include "emu.h"
10#include "imageutl.h"
10#include "formats/imageutl.h"
1111#include "cassette.h"
12#include "cassimg.h"
1312#include "ui.h"
1413
1514
src/emu/imagedev/flopdrv.c
r11457r11458
1212*/
1313
1414#include "emu.h"
15#include "imageutl.h"
15#include "formats/imageutl.h"
1616#include "flopdrv.h"
1717
1818#define VERBOSE      0
src/emu/imagedev/cassette.h
r11457r11458
1010#define CASSETTE_H
1111
1212#include "image.h"
13#include "cassimg.h"
13#include "formats/cassimg.h"
1414
1515
1616enum _cassette_state
src/emu/imagedev/flopdrv.h
r11457r11458
66
77#include "devcb.h"
88#include "image.h"
9#include "flopimg.h"
9#include "formats/flopimg.h"
1010
1111#define FLOPPY_TYPE_REGULAR 0
1212#define FLOPPY_TYPE_APPLE   1
src/emu/image.c
r11457r11458
1515#include "image.h"
1616#include "config.h"
1717#include "xmlfile.h"
18#include "formats/ioprocs.h"
1819
1920/* ----------------------------------------------------------------------- */
2021
src/emu/image.h
r11457r11458
1818#ifndef __IMAGE_H__
1919#define __IMAGE_H__
2020
21#include "ioprocs.h"
22
2321void image_init(running_machine &machine);
2422void image_postdevice_init(running_machine &machine);
2523
src/emu/emu.mak
r11457r11458
8181   $(EMUOBJ)/input.o \
8282   $(EMUOBJ)/inputseq.o \
8383   $(EMUOBJ)/inptport.o \
84   $(EMUOBJ)/ioprocs.o \
8584   $(EMUOBJ)/mame.o \
8685   $(EMUOBJ)/machine.o \
8786   $(EMUOBJ)/mconfig.o \
r11457r11458
284283   $(EMUIMAGEDEV)/bitbngr.o   \
285284   $(EMUIMAGEDEV)/cartslot.o   \
286285   $(EMUIMAGEDEV)/cassette.o   \
287   $(EMUIMAGEDEV)/cassimg.o   \
288286   $(EMUIMAGEDEV)/chd_cd.o      \
289287   $(EMUIMAGEDEV)/flopdrv.o   \
290   $(EMUIMAGEDEV)/flopimg.o   \
291288   $(EMUIMAGEDEV)/harddriv.o   \
292289   $(EMUIMAGEDEV)/multcart.o   \
293290   $(EMUIMAGEDEV)/printer.o   \
294291   $(EMUIMAGEDEV)/snapquik.o   \
295   $(EMUIMAGEDEV)/wavfile.o   \
296292
297293
298294LIBEMUOBJS = $(EMUOBJS) $(EMUSOUNDOBJS) $(EMUAUDIOOBJS) $(EMUDRIVEROBJS) $(EMUMACHINEOBJS) $(EMUIMAGEDEVOBJS) $(EMUVIDEOOBJS)
src/lib/formats/thom_cas.h
r0r11458
1/**********************************************************************
2
3  Copyright (C) Antoine Mine' 2006
4
5  Thomson 8-bit computers
6
7**********************************************************************/
8
9#ifndef THOM_CAS
10#define THOM_CAS
11
12#include "cassimg.h"
13
14/******************** TO7 ************************/
15
16CASSETTE_FORMATLIST_EXTERN(to7_cassette_formats);
17
18/******************** MO5 ************************/
19
20CASSETTE_FORMATLIST_EXTERN(mo5_cassette_formats);
21
22#endif
src/lib/formats/fmsx_cas.c
r0r11458
1#include "formats/fmsx_cas.h"
2
3
4#define CAS_PERIOD        (16)
5#define CAS_HEADER_PERIODS (4000)
6#define CAS_EMPTY_PERIODS (1000)
7static const UINT8 CasHeader[8] = { 0x1F,0xA6,0xDE,0xBA,0xCC,0x13,0x7D,0x74 };
8
9static int cas_size;
10
11
12/*******************************************************************
13   Calculate the number of samples needed for this tape image
14********************************************************************/
15static int fmsx_cas_to_wav_size (const UINT8 *casdata, int caslen)
16{
17   int    pos, size;
18
19   if (caslen < 8) return -1;
20   if (memcmp (casdata, CasHeader, sizeof (CasHeader) ) ) return -1;
21
22   pos = size = 0;
23
24   while (pos < caslen)
25   {
26      if ( (pos + 8) < caslen)
27         if (!memcmp (casdata + pos, CasHeader, 8) )
28         {
29            size += (CAS_EMPTY_PERIODS + CAS_HEADER_PERIODS) * CAS_PERIOD;
30            pos += 8;
31            continue;
32         }
33
34      size += CAS_PERIOD * 12;
35      pos++;
36   }
37
38   cas_size = caslen;
39
40   return size;
41}
42
43
44/*******************************************************************
45   Generate samples for the tape image
46********************************************************************/
47static int fmsx_cas_fill_wave(INT16 *buffer, int sample_count, UINT8 *bytes)
48{
49   int cas_pos, bit, state = 1, samples_pos, size, n, i, p;
50
51   cas_pos = 0;
52   samples_pos = 0;
53
54    while (samples_pos < sample_count && cas_pos < cas_size)
55   {
56      /* Check if we need to output a header */
57      if ( cas_pos + 8 < cas_size )
58      {
59         if ( ! memcmp( bytes + cas_pos, CasHeader, 8 ) )
60         {
61            /* Write CAS_EMPTY_PERIODS of silence */
62            n = CAS_EMPTY_PERIODS * CAS_PERIOD; while (n--) buffer[samples_pos++] = 0;
63
64            /* Write CAS_HEADER_PERIODS of header (high frequency) */
65            for (i=0;i<CAS_HEADER_PERIODS*4;i++)
66            {
67               for (n=0;n<CAS_PERIOD / 4;n++)
68                  buffer[samples_pos + n] = (state ? 32767 : -32767);
69
70               samples_pos += CAS_PERIOD / 4 ;
71               state = !state;
72            }
73
74            cas_pos += 8;
75         }
76      }
77
78      for (i=0;i<=11;i++)
79      {
80         if (i == 0) bit = 0;
81         else if (i < 9) bit = (bytes[cas_pos] & (1 << (i - 1) ) );
82         else bit = 1;
83
84         /* write this one bit */
85         for (n=0;n<(bit ? 4 : 2);n++)
86         {
87            size = (bit ? CAS_PERIOD / 4 : CAS_PERIOD / 2);
88            for (p=0;p<size;p++)
89            {
90               buffer[samples_pos + p] = (state ? 32767 : -32767);
91            }
92            state = !state;
93            samples_pos += size;
94         }
95      }
96      cas_pos++;
97   }
98
99   return samples_pos;
100}
101
102
103static const struct CassetteLegacyWaveFiller fmsx_legacy_fill_wave =
104{
105   fmsx_cas_fill_wave,                  /* fill_wave */
106   -1,                              /* chunk_size */
107   0,                              /* chunk_samples */
108   fmsx_cas_to_wav_size,               /* chunk_sample_calc */
109   22050,                           /* sample_frequency */
110   0,                              /* header_samples */
111   0                              /* trailer_samples */
112};
113
114
115
116static casserr_t fmsx_cas_identify(cassette_image *cassette, struct CassetteOptions *opts)
117{
118   return cassette_legacy_identify(cassette, opts, &fmsx_legacy_fill_wave);
119}
120
121
122
123static casserr_t fmsx_cas_load(cassette_image *cassette)
124{
125   return cassette_legacy_construct(cassette, &fmsx_legacy_fill_wave);
126}
127
128
129
130static const struct CassetteFormat fmsx_cas_format =
131{
132   "tap,cas",
133   fmsx_cas_identify,
134   fmsx_cas_load,
135   NULL
136};
137
138
139
140CASSETTE_FORMATLIST_START(fmsx_cassette_formats)
141   CASSETTE_FORMAT(fmsx_cas_format)
142CASSETTE_FORMATLIST_END
src/lib/formats/d88_dsk.c
r0r11458
1/*
2 *   D77 and D88 disk images
3 *
4 *
5 *   Header (total size = 0x2b0 bytes):
6 *     0x00 - Disk label
7 *     0x1a - Write protect (bit 3)
8 *     0x1b - 2D/2DD format (bit 3)
9 *     0x1c-1f - image size (should match file size)
10 *     0x20 - offsets for each track (max 164)
11 *
12 *   Sectors (0x110 bytes each, typically)
13 *     0x00 - Sector info
14 *          byte 0 - track number
15 *          byte 1 - side (0 or 1)
16 *          byte 2 - sector number
17 *     0x10 - sector data
18 *
19 *   Images can be concatenated together.
20 *   Sectors can be in any order.
21 *   Tracks are in the order:
22 *          Track 0 side 0
23 *          Track 0 side 1
24 *          Track 1 side 0
25 *          ...
26 *
27 *
28 */
29
30#include "flopimg.h"
31#include "imageutl.h"
32
33#define D88_HEADER_LEN 0x2b0
34
35struct d88_tag
36{
37   UINT32 image_size;
38   UINT32 trackoffset[164];
39   UINT8 write_protect;
40   UINT8 disk_type;
41   UINT8 heads;
42};
43
44static struct d88_tag *get_d88_tag(floppy_image *floppy)
45{
46   return (d88_tag *)floppy_tag(floppy);
47}
48
49static int d88_get_sector_id(floppy_image *floppy, int head, int track, int sector_index)
50{
51   struct d88_tag* tag = get_d88_tag(floppy);
52   UINT32 offset;
53   UINT8 sector_hdr[16];
54   int x;
55
56   offset = tag->trackoffset[(track*tag->heads)+head];
57
58   if(offset == 0)
59      return 0;
60
61   floppy_image_read(floppy,sector_hdr,offset,16);
62
63   // get to sector indexed
64   x=0;
65   while(x<sector_index)
66   {
67      offset += ((sector_hdr[15] << 8) | sector_hdr[14]);
68      offset += 16;
69      floppy_image_read(floppy,sector_hdr,offset,16);
70      x++;
71   }
72
73   return sector_hdr[2];
74}
75
76static int d88_get_tracks_per_disk(floppy_image *floppy)
77{
78   return 82;  // 82 tracks per side
79}
80
81static int d88_get_heads_per_disk(floppy_image *floppy)
82{
83   struct d88_tag* tag = get_d88_tag(floppy);
84   return tag->heads;
85}
86
87static int d88_get_sectors_per_track(floppy_image *floppy, int head, int track)
88{
89   struct d88_tag* tag = get_d88_tag(floppy);
90   UINT32 offset;
91   UINT8 sector_hdr[16];
92
93   offset = tag->trackoffset[(track*tag->heads)+head];
94
95   floppy_image_read(floppy,sector_hdr,offset,16);
96
97   return sector_hdr[4];
98}
99
100static floperr_t d88_get_sector_length(floppy_image *floppy, int head, int track, int sector, UINT32 *sector_length)
101{
102   struct d88_tag* tag = get_d88_tag(floppy);
103   UINT32 offset;
104   UINT8 sector_hdr[16];
105   UINT32 len;
106   int count,secs;
107
108   offset = tag->trackoffset[(track*tag->heads)+head];
109
110   floppy_image_read(floppy,sector_hdr,offset,16);
111   secs = sector_hdr[4];
112
113   for(count=0;count<secs;count++)
114   {
115      floppy_image_read(floppy,sector_hdr,offset,16);
116      if(sector == sector_hdr[2])
117      {
118         if(sector_length)
119            *sector_length = (sector_hdr[15] << 8) | sector_hdr[14];
120         return FLOPPY_ERROR_SUCCESS;
121      }
122      len = (sector_hdr[15] << 8) | sector_hdr[14];
123      len += 16;
124      offset += len;
125   }
126
127   return FLOPPY_ERROR_SEEKERROR;
128}
129
130static floperr_t d88_read_track(floppy_image *floppy, int head, int track, UINT64 offset, void *buffer, size_t buflen)
131{
132//  floperr_t err;
133
134   return FLOPPY_ERROR_UNSUPPORTED;
135}
136
137static UINT32 d88_get_sector_offset(floppy_image* floppy, int head, int track, int sector)
138{
139   struct d88_tag* tag = get_d88_tag(floppy);
140   UINT32 offset = 0;
141   UINT8 sector_hdr[16];
142   UINT32 len;
143   UINT32 secs;
144   int count;
145
146   // get offset of the beginning of the track
147   offset = tag->trackoffset[(track*tag->heads)+head];
148
149   floppy_image_read(floppy,sector_hdr,offset,16);
150   secs = sector_hdr[4];
151
152   for(count=0;count<secs;count++)
153   {
154      floppy_image_read(floppy,sector_hdr,offset,16);
155      if(sector == sector_hdr[2])
156      {
157         logerror("d88_get_sector_offset - track %i, side %i, sector %02x, returns %08x\n",track,head,sector,offset+16);
158         return offset + 16;
159      }
160      len = (sector_hdr[15] << 8) | sector_hdr[14];
161      len += 16;
162      offset += len;
163   }
164   logerror("d88_get_sector_offset - track %i, side %i, sector %02x, not found\n",track,head,sector);
165   return 0;
166}
167
168static floperr_t d88_get_indexed_sector_info(floppy_image *floppy, int head, int track, int sector_index, int *cylinder, int *side, int *sector, UINT32 *sector_length, unsigned long *flags)
169{
170   struct d88_tag* tag = get_d88_tag(floppy);
171   UINT32 offset;
172   UINT8 sector_hdr[16];
173   int x;
174
175   offset = tag->trackoffset[(track*tag->heads)+head];
176
177   if(offset == 0)
178      return FLOPPY_ERROR_SEEKERROR;
179
180
181   floppy_image_read(floppy,sector_hdr,offset,16);
182
183   if(sector_index >= sector_hdr[4])
184      return FLOPPY_ERROR_SEEKERROR;
185
186   // get to sector indexed
187   x=0;
188   while(x<sector_index)
189   {
190      offset += ((sector_hdr[15] << 8) | sector_hdr[14]);
191      offset += 16;
192      floppy_image_read(floppy,sector_hdr,offset,16);
193      x++;
194   }
195
196   if(offset > tag->image_size || offset == 0)
197      return FLOPPY_ERROR_SEEKERROR;
198
199   if(sector_length)
200      *sector_length = (sector_hdr[15] << 8) | sector_hdr[14];
201   if(cylinder)
202      *cylinder = sector_hdr[0];
203   if(side)
204      *side = sector_hdr[1];
205   if(sector)
206      *sector = sector_hdr[2];
207   if(flags)
208      *flags = 0;
209
210   return FLOPPY_ERROR_SUCCESS;
211}
212
213static floperr_t d88_read_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen)
214{
215   UINT64 offset;
216   UINT32 sector_length;
217
218   offset = d88_get_sector_offset(floppy,head,track,sector);
219
220   if(d88_get_sector_length(floppy,head,track,sector,&sector_length) != FLOPPY_ERROR_SUCCESS)
221      return FLOPPY_ERROR_SEEKERROR;
222
223   if(offset == 0)
224      return FLOPPY_ERROR_SEEKERROR;
225
226   if(buflen > sector_length)
227      return FLOPPY_ERROR_INTERNAL;
228
229   floppy_image_read(floppy,buffer,offset,sector_length);
230
231   return FLOPPY_ERROR_SUCCESS;
232}
233
234static floperr_t d88_read_indexed_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buffer_len)
235{
236   int sec;
237
238   sec = d88_get_sector_id(floppy,head,track,sector);
239   return d88_read_sector(floppy,head,track,sec,buffer,buffer_len);
240}
241
242static floperr_t d88_write_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam)
243{
244   UINT64 offset;
245   UINT32 sector_length;
246
247   offset = d88_get_sector_offset(floppy,head,track,sector);
248
249   if(d88_get_sector_length(floppy,head,track,sector,&sector_length) != FLOPPY_ERROR_SUCCESS)
250      return FLOPPY_ERROR_SEEKERROR;
251
252   if(offset == 0)
253      return FLOPPY_ERROR_SEEKERROR;
254
255   if(buflen > sector_length)
256      return FLOPPY_ERROR_INTERNAL;
257
258   floppy_image_write(floppy,buffer,offset,sector_length);
259
260   return FLOPPY_ERROR_SUCCESS;
261}
262
263static floperr_t d88_write_indexed_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam)
264{
265   int sec;
266
267   sec = d88_get_sector_id(floppy,head,track,sector);
268   return d88_write_sector(floppy,head,track,sec,buffer,buflen,ddam);
269}
270
271static void d88_get_header(floppy_image* floppy,UINT32* size, UINT8* prot, UINT8* type, UINT32* offsets)
272{
273   UINT8 header[D88_HEADER_LEN];
274   int x,s;
275
276   floppy_image_read(floppy,header,0,D88_HEADER_LEN);
277
278#ifdef SPOT_DUPLICATES
279      // there exist many .d88 files with same data and different headers and
280      // this allows to spot duplicates, making easier to debug softlists.
281      UINT32 temp_size = floppy_image_size(floppy);
282      UINT8 tmp_copy[temp_size - D88_HEADER_LEN];
283      floppy_image_read(floppy,tmp_copy,D88_HEADER_LEN,temp_size - D88_HEADER_LEN);
284      printf("CRC16: %d\n", ccitt_crc16(0xffff, tmp_copy, temp_size - D88_HEADER_LEN));
285#endif
286
287   if(prot)
288      *prot = header[0x1a];
289   if(type)
290      *type = header[0x1b];
291   if(size)
292   {
293      s = 0;
294      s |= header[0x1f] << 24;
295      s |= header[0x1e] << 16;
296      s |= header[0x1d] << 8;
297      s |= header[0x1c];
298      *size = s;
299   }
300   if(offsets)
301   {
302      for(x=0;x<164;x++)
303      {
304         s = 0;
305         s |= header[0x23 + (x*4)] << 24;
306         s |= header[0x22 + (x*4)] << 16;
307         s |= header[0x21 + (x*4)] << 8;
308         s |= header[0x20 + (x*4)];
309         *(offsets+x) = s;
310      }
311   }
312}
313
314FLOPPY_IDENTIFY(d88_dsk_identify)
315{
316   UINT32 size;
317
318   d88_get_header(floppy,&size,NULL,NULL,NULL);
319
320   if(floppy_image_size(floppy) == size)
321   {
322      *vote = 100;
323   }
324   else
325   {
326      *vote = 0;
327   }
328   return FLOPPY_ERROR_SUCCESS;
329}
330
331FLOPPY_CONSTRUCT(d88_dsk_construct)
332{
333   struct FloppyCallbacks *callbacks;
334   struct d88_tag *tag;
335   UINT32 size;
336   UINT8 prot,type = 0;
337   UINT32 offs[164];
338   int x;
339
340   if(params)
341   {
342      // create
343      return FLOPPY_ERROR_UNSUPPORTED;
344   }
345   else
346   {
347      // load
348      d88_get_header(floppy,&size,&prot,&type,offs);
349   }
350
351   tag = (d88_tag *)floppy_create_tag(floppy,sizeof(struct d88_tag));
352   if (!tag)
353      return FLOPPY_ERROR_OUTOFMEMORY;
354
355   tag->write_protect = prot;
356   tag->disk_type = type;
357   tag->heads = 2;
358   if (tag->disk_type==0x30 || tag->disk_type==0x40) tag->heads = 1;
359
360   tag->image_size = size;
361   for(x=0;x<164;x++)
362      tag->trackoffset[x] = offs[x];
363
364   callbacks = floppy_callbacks(floppy);
365   callbacks->read_track = d88_read_track;
366   callbacks->get_heads_per_disk = d88_get_heads_per_disk;
367   callbacks->get_tracks_per_disk = d88_get_tracks_per_disk;
368   callbacks->get_sector_length = d88_get_sector_length;
369   callbacks->read_sector = d88_read_sector;
370   callbacks->read_indexed_sector = d88_read_indexed_sector;
371   callbacks->write_sector = d88_write_sector;
372   callbacks->write_indexed_sector = d88_write_indexed_sector;
373   callbacks->get_indexed_sector_info = d88_get_indexed_sector_info;
374   callbacks->get_sectors_per_track = d88_get_sectors_per_track;
375
376
377   return FLOPPY_ERROR_SUCCESS;
378}
src/lib/formats/uef_cas.c
r0r11458
1/*
2
3UEF format support (for electron driver)
4
5The UEF format supports gzipped images, i'm doing the gunzip step during uef_cas_to_wav_size
6because that is when the length of the orignal file is known. This is needed to determine
7the size of memory to alloc for the decoding.
8
9Not nice, but it works...
10
11*/
12
13#include <string.h>
14#include <math.h>
15
16#include "zlib.h"
17#include "uef_cas.h"
18
19
20#define UEF_WAV_FREQUENCY   4800
21#define WAVE_LOW   -32768
22#define WAVE_HIGH   32767
23#define WAVE_NULL   0
24
25static const UINT8 UEF_HEADER[10] = { 0x55, 0x45, 0x46, 0x20, 0x46, 0x69, 0x6c, 0x65, 0x21, 0x00 };
26
27/*
28    bytes are stored as
29    start bit   1 * 0
30    data bits   8 * X
31    stop bit    1 * 1
32*/
33
34/* gzip flag byte */
35#define ASCII_FLAG   0x01 /* bit 0 set: file probably ascii text */
36#define HEAD_CRC   0x02 /* bit 1 set: header CRC present */
37#define EXTRA_FIELD   0x04 /* bit 2 set: extra field present */
38#define ORIG_NAME   0x08 /* bit 3 set: original file name present */
39#define COMMENT      0x10 /* bit 4 set: file comment present */
40#define RESERVED   0xE0 /* bits 5..7: reserved */
41
42static const UINT8* skip_gz_header( const UINT8 *p ) {
43   UINT8 method, flags;
44   /* skip initial 1f 8b header */
45   p += 2;
46   /* get method and flags */
47   method = *p; p++;
48   flags = *p; p++;
49   if ( method != Z_DEFLATED || ( flags & RESERVED ) != 0 ) {
50      return NULL;
51   }
52   /* Skip time, xflags and OS code */
53   p += 6;
54
55   /* Skip the extra field */
56   if ( ( flags & EXTRA_FIELD ) != 0 ) {
57      int len = ( p[1] << 8 ) | p[0];
58      p += 2 + len;
59   }
60   /* Skip the original file name */
61   if ( ( flags & ORIG_NAME ) != 0 ) {
62      for ( ; *p; p++ );
63   }
64   /* Skip the .gz file comment */
65   if ( ( flags & COMMENT ) != 0 ) {
66      for( ; *p; p++ );
67   }
68   /* Skip the header crc */
69   if ( ( flags & HEAD_CRC ) != 0 ) {
70      p += 2;
71   }
72   return p;
73}
74
75static UINT8 *gz_ptr = NULL;
76
77static float get_uef_float( const UINT8 *Float)
78{
79      int Mantissa;
80      float Result;
81      int Exponent;
82
83      /* assume a four byte array named Float exists, where Float[0]
84        was the first byte read from the UEF, Float[1] the second, etc */
85
86      /* decode mantissa */
87      Mantissa = Float[0] | (Float[1] << 8) | ((Float[2]&0x7f)|0x80) << 16;
88
89      Result = (float)Mantissa;
90      Result = (float)ldexp(Result, -23);
91
92      /* decode exponent */
93      Exponent = ((Float[2]&0x80) >> 7) | (Float[3]&0x7f) << 1;
94      Exponent -= 127;
95      Result = (float)ldexp(Result, Exponent);
96
97      /* flip sign if necessary */
98      if(Float[3]&0x80)
99         Result = -Result;
100
101   /* floating point number is now in 'Result' */
102   return Result;
103}
104
105static int uef_cas_to_wav_size( const UINT8 *casdata, int caslen ) {
106   int   pos, size;
107
108   if ( casdata[0] == 0x1f && casdata[1] == 0x8b ) {
109      int err;
110      z_stream d_stream;
111      int inflate_size = ( casdata[ caslen - 1 ] << 24 ) | ( casdata[ caslen - 2 ] << 16 ) | ( casdata[ caslen - 3 ] << 8 ) | casdata[ caslen - 4 ];
112      const UINT8 *in_ptr = skip_gz_header( casdata );
113
114      if ( in_ptr == NULL ) {
115         goto cleanup;
116      }
117      gz_ptr = (UINT8 *)malloc( inflate_size );
118
119      d_stream.zalloc = 0;
120      d_stream.zfree = 0;
121      d_stream.opaque = 0;
122      d_stream.next_in = (unsigned char *)in_ptr;
123      d_stream.avail_in = caslen - ( in_ptr - casdata );
124      d_stream.next_out = gz_ptr;
125      d_stream.avail_out = inflate_size;
126
127      err = inflateInit2( &d_stream, -MAX_WBITS );
128      if ( err != Z_OK ) {
129         logerror( "inflateInit2 error: %d\n", err );
130         goto cleanup;
131      }
132      err = inflate( &d_stream, Z_NO_FLUSH );
133      if ( err != Z_STREAM_END && err != Z_OK ) {
134         logerror( "inflate error: %d\n", err );
135         goto cleanup;
136      }
137      err = inflateEnd( &d_stream );
138      if ( err != Z_OK ) {
139         logerror( "inflateEnd error: %d\n", err );
140         goto cleanup;
141      }
142      caslen = inflate_size;
143      casdata = gz_ptr;
144   }
145
146   if ( caslen < 18 ) {
147      logerror( "uef_cas_to_wav_size: cassette image too small\n" );
148      goto cleanup;
149   }
150   if ( memcmp( casdata, UEF_HEADER, sizeof(UEF_HEADER) ) ) {
151      logerror( "uef_cas_to_wav_size: cassette image has incompatible header\n" );
152      goto cleanup;
153   }
154
155   logerror( "UEF: Determinig tape size\n" );
156   size = 0;
157   pos = sizeof(UEF_HEADER) + 2;
158   while( pos < caslen ) {
159      int chunk_type = ( casdata[pos+1] << 8 ) | casdata[pos];
160      int chunk_length = ( casdata[pos+5] << 24 ) | ( casdata[pos+4] << 16 ) | ( casdata[pos+3] << 8 ) | casdata[pos+2];
161      int baud_length;
162
163      pos += 6;
164      switch( chunk_type ) {
165      case 0x0100:   /* implicit start/stop bit data block */
166         size += ( chunk_length * 10 ) * 4;
167         break;
168      case 0x0101:   /* multiplexed data block */
169      case 0x0103:
170         logerror( "Unsupported chunk type: %04x\n", chunk_type );
171         break;
172      case 0x0102:   /* explicit tape data block */
173         size += ( ( chunk_length * 10 ) - casdata[pos] ) * 4;
174         break;
175      case 0x0104:
176         logerror( "Unsupported chunk type: %04x\n", chunk_type );
177         break;
178      case 0x0110:
179         baud_length = ( casdata[pos+1] << 8 ) | casdata[pos];
180         size += baud_length * 2;
181         break;
182      case 0x0111:
183         logerror( "Unsupported chunk type: %04x\n", chunk_type );
184         break;
185      case 0x0112:
186         baud_length = ( casdata[pos+1] << 8 ) | casdata[pos];
187         size += baud_length * 2 ;
188         break;
189      case 0x0116:
190         size += get_uef_float(casdata+pos)*UEF_WAV_FREQUENCY;
191         break;
192      case 0x0113:
193      case 0x0114:
194      case 0x0115:
195      case 0x0120:
196         logerror( "Unsupported chunk type: %04x\n", chunk_type );
197         break;
198      }
199      pos += chunk_length;
200   }
201   size = 2 * size;
202   return size;
203
204cleanup:
205   if ( gz_ptr ) {
206      free( gz_ptr );
207      gz_ptr = NULL;
208   }
209   return -1;
210}
211
212static INT16* uef_cas_fill_bit( INT16 *buffer, int bit ) {
213   if ( bit ) {
214      *buffer = WAVE_LOW; buffer++;
215      *buffer = WAVE_HIGH; buffer++;
216      *buffer = WAVE_LOW; buffer++;
217      *buffer = WAVE_HIGH; buffer++;
218   } else {
219      *buffer = WAVE_LOW; buffer++;
220      *buffer = WAVE_LOW; buffer++;
221      *buffer = WAVE_HIGH; buffer++;
222      *buffer = WAVE_HIGH; buffer++;
223   }
224   return buffer;
225}
226
227static int uef_cas_fill_wave( INT16 *buffer, int length, UINT8 *bytes ) {
228   int pos;
229   INT16 *p = buffer;
230
231   if ( bytes[0] == 0x1f && bytes[1] == 0x8b ) {
232      if ( gz_ptr == NULL ) {
233         return 1;
234      }
235      bytes = gz_ptr;
236   }
237
238   pos = sizeof(UEF_HEADER) + 2;
239   length = length / 2;
240   while( length > 0 ) {
241      int chunk_type = ( bytes[pos+1] << 8 ) | bytes[pos];
242      int chunk_length = ( bytes[pos+5] << 24 ) | ( bytes[pos+4] << 16 ) | ( bytes[pos+3] << 8 ) | bytes[pos+2];
243
244      int baud_length, i, j;
245      UINT8 *c;
246      pos += 6;
247      switch( chunk_type ) {
248      case 0x0100:    /* implicit start/stop bit data block */
249         for( j = 0; j < chunk_length; j++ ) {
250            UINT8 byte = bytes[pos+j];
251            p = uef_cas_fill_bit( p, 0 );
252            for( i = 0; i < 8; i++ ) {
253               p = uef_cas_fill_bit( p, byte & 1 );
254               byte = byte >> 1;
255            }
256            p = uef_cas_fill_bit( p, 1 );
257            length -= ( 10 * 4 );
258         }
259         break;
260      case 0x0101:    /* multiplexed data block */
261      case 0x0103:
262         logerror( "Unsupported chunk type: %04x\n", chunk_type );
263         break;
264      case 0x0102:    /* explicit tape data block */
265         j = ( chunk_length * 10 ) - bytes[pos];
266         c = bytes + pos;
267         while( j ) {
268            UINT8 byte = *c;
269            for( i = 0; i < 8 && i < j; i++ ) {
270               p = uef_cas_fill_bit( p, byte & 1 );
271               byte = byte >> 1;
272               j--;
273            }
274            c++;
275         }
276         break;
277      case 0x0110:
278         for( baud_length = ( ( bytes[pos+1] << 8 ) | bytes[pos] ) ; baud_length; baud_length-- ) {
279            *p = WAVE_LOW; p++;
280            *p = WAVE_HIGH; p++;
281            length -= 2;
282         }
283         break;
284      case 0x0112:
285         for( baud_length = ( ( bytes[pos+1] << 8 ) | bytes[pos] ) ; baud_length; baud_length-- ) {
286            *p = WAVE_NULL; p++;
287            *p = WAVE_NULL; p++;
288            length -= 2;
289         }
290         break;
291      case 0x0116:
292         for( baud_length = (get_uef_float(bytes+pos)*UEF_WAV_FREQUENCY); baud_length; baud_length-- ) {
293            *p = WAVE_NULL; p++;
294            length -= 1;
295         }
296         break;
297
298
299      }
300      pos += chunk_length;
301   }
302   return p - buffer;
303}
304
305static const struct CassetteLegacyWaveFiller uef_legacy_fill_wave = {
306   uef_cas_fill_wave,            /* fill_wave */
307   -1,                  /* chunk_size */
308   0,                  /* chunk_samples */
309   uef_cas_to_wav_size,            /* chunk_sample_calc */
310   UEF_WAV_FREQUENCY,            /* sample_frequency */
311   0,                  /* header_samples */
312   0                  /* trailer_samples */
313};
314
315static casserr_t uef_cassette_identify( cassette_image *cassette, struct CassetteOptions *opts ) {
316   return cassette_legacy_identify( cassette, opts, &uef_legacy_fill_wave );
317}
318
319static casserr_t uef_cassette_load( cassette_image *cassette ) {
320   return cassette_legacy_construct( cassette, &uef_legacy_fill_wave );
321}
322
323const struct CassetteFormat uef_cassette_format = {
324   "uef",
325   uef_cassette_identify,
326   uef_cassette_load,
327   NULL
328};
329
330CASSETTE_FORMATLIST_START(uef_cassette_formats)
331   CASSETTE_FORMAT(uef_cassette_format)
332CASSETTE_FORMATLIST_END
333
src/lib/formats/imageutl.c
r0r11458
1/***************************************************************************
2
3    imageutl.h
4
5    Image related utilities
6
7****************************************************************************
8
9    Copyright Aaron Giles
10    All rights reserved.
11
12    Redistribution and use in source and binary forms, with or without
13    modification, are permitted provided that the following conditions are
14    met:
15
16        * Redistributions of source code must retain the above copyright
17          notice, this list of conditions and the following disclaimer.
18        * Redistributions in binary form must reproduce the above copyright
19          notice, this list of conditions and the following disclaimer in
20          the documentation and/or other materials provided with the
21          distribution.
22        * Neither the name 'MAME' nor the names of its contributors may be
23          used to endorse or promote products derived from this software
24          without specific prior written permission.
25
26    THIS SOFTWARE IS PROVIDED BY AARON GILES ''AS IS'' AND ANY EXPRESS OR
27    IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
28    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
29    DISCLAIMED. IN NO EVENT SHALL AARON GILES BE LIABLE FOR ANY DIRECT,
30    INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
31    (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
32    SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33    HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
34    STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
35    IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36    POSSIBILITY OF SUCH DAMAGE.
37
38***************************************************************************/
39
40#include <ctype.h>
41#include "imageutl.h"
42
43int compute_log2(int val)
44{
45   int count = 0;
46
47   while(val > 1)
48   {
49      if (val & 1)
50         return -1;
51      val /= 2;
52      count++;
53   }
54   return (val == 0) ? -1 : count;
55}
56
57/*
58   Compute CCITT CRC-16 using the correct bit order for floppy disks.
59   CRC code courtesy of Tim Mann.
60*/
61
62/* Accelerator table to compute the CRC eight bits at a time */
63static const unsigned short ccitt_crc16_table[256] =
64{
65   0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7,
66   0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF,
67   0x1231, 0x0210, 0x3273, 0x2252, 0x52B5, 0x4294, 0x72F7, 0x62D6,
68   0x9339, 0x8318, 0xB37B, 0xA35A, 0xD3BD, 0xC39C, 0xF3FF, 0xE3DE,
69   0x2462, 0x3443, 0x0420, 0x1401, 0x64E6, 0x74C7, 0x44A4, 0x5485,
70   0xA56A, 0xB54B, 0x8528, 0x9509, 0xE5EE, 0xF5CF, 0xC5AC, 0xD58D,
71   0x3653, 0x2672, 0x1611, 0x0630, 0x76D7, 0x66F6, 0x5695, 0x46B4,
72   0xB75B, 0xA77A, 0x9719, 0x8738, 0xF7DF, 0xE7FE, 0xD79D, 0xC7BC,
73   0x48C4, 0x58E5, 0x6886, 0x78A7, 0x0840, 0x1861, 0x2802, 0x3823,
74   0xC9CC, 0xD9ED, 0xE98E, 0xF9AF, 0x8948, 0x9969, 0xA90A, 0xB92B,
75   0x5AF5, 0x4AD4, 0x7AB7, 0x6A96, 0x1A71, 0x0A50, 0x3A33, 0x2A12,
76   0xDBFD, 0xCBDC, 0xFBBF, 0xEB9E, 0x9B79, 0x8B58, 0xBB3B, 0xAB1A,
77   0x6CA6, 0x7C87, 0x4CE4, 0x5CC5, 0x2C22, 0x3C03, 0x0C60, 0x1C41,
78   0xEDAE, 0xFD8F, 0xCDEC, 0xDDCD, 0xAD2A, 0xBD0B, 0x8D68, 0x9D49,
79   0x7E97, 0x6EB6, 0x5ED5, 0x4EF4, 0x3E13, 0x2E32, 0x1E51, 0x0E70,
80   0xFF9F, 0xEFBE, 0xDFDD, 0xCFFC, 0xBF1B, 0xAF3A, 0x9F59, 0x8F78,
81   0x9188, 0x81A9, 0xB1CA, 0xA1EB, 0xD10C, 0xC12D, 0xF14E, 0xE16F,
82   0x1080, 0x00A1, 0x30C2, 0x20E3, 0x5004, 0x4025, 0x7046, 0x6067,
83   0x83B9, 0x9398, 0xA3FB, 0xB3DA, 0xC33D, 0xD31C, 0xE37F, 0xF35E,
84   0x02B1, 0x1290, 0x22F3, 0x32D2, 0x4235, 0x5214, 0x6277, 0x7256,
85   0xB5EA, 0xA5CB, 0x95A8, 0x8589, 0xF56E, 0xE54F, 0xD52C, 0xC50D,
86   0x34E2, 0x24C3, 0x14A0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
87   0xA7DB, 0xB7FA, 0x8799, 0x97B8, 0xE75F, 0xF77E, 0xC71D, 0xD73C,
88   0x26D3, 0x36F2, 0x0691, 0x16B0, 0x6657, 0x7676, 0x4615, 0x5634,
89   0xD94C, 0xC96D, 0xF90E, 0xE92F, 0x99C8, 0x89E9, 0xB98A, 0xA9AB,
90   0x5844, 0x4865, 0x7806, 0x6827, 0x18C0, 0x08E1, 0x3882, 0x28A3,
91   0xCB7D, 0xDB5C, 0xEB3F, 0xFB1E, 0x8BF9, 0x9BD8, 0xABBB, 0xBB9A,
92   0x4A75, 0x5A54, 0x6A37, 0x7A16, 0x0AF1, 0x1AD0, 0x2AB3, 0x3A92,
93   0xFD2E, 0xED0F, 0xDD6C, 0xCD4D, 0xBDAA, 0xAD8B, 0x9DE8, 0x8DC9,
94   0x7C26, 0x6C07, 0x5C64, 0x4C45, 0x3CA2, 0x2C83, 0x1CE0, 0x0CC1,
95   0xEF1F, 0xFF3E, 0xCF5D, 0xDF7C, 0xAF9B, 0xBFBA, 0x8FD9, 0x9FF8,
96   0x6E17, 0x7E36, 0x4E55, 0x5E74, 0x2E93, 0x3EB2, 0x0ED1, 0x1EF0
97};
98
99unsigned short ccitt_crc16(unsigned short crc, const unsigned char *buffer, size_t buffer_len)
100{
101   size_t i;
102   for (i = 0; i < buffer_len; i++)
103      crc = (crc << 8) ^ ccitt_crc16_table[(crc >> 8) ^ buffer[i]];
104   return crc;
105}
106
107unsigned short ccitt_crc16_one( unsigned short crc, const unsigned char data )
108{
109    return (crc << 8) ^ ccitt_crc16_table[(crc >> 8) ^ data];
110}
111
112
113/*-------------------------------------------------
114    is_delim - returns whether c is a comma or \0
115-------------------------------------------------*/
116
117static int is_delim(char c)
118{
119   return (c == '\0') || (c == ',');
120}
121
122
123
124/*-------------------------------------------------
125    image_internal_find_extension - find an extension in
126    an extension list
127-------------------------------------------------*/
128
129static int image_internal_find_extension(const char *extension_list, const char *target_extension)
130{
131   /* this version allows target_extension to be delimited with a comma */
132   int pos = 0;
133   int i;
134
135   while(extension_list[pos] != '\0')
136   {
137      /* compare a file extension */
138      i = 0;
139      while(!is_delim(extension_list[pos + i])
140         && !is_delim(target_extension[i])
141         && (tolower(extension_list[pos + i]) == tolower(target_extension[i])))
142      {
143         i++;
144      }
145
146      /* check to see if it was found */
147      if (is_delim(extension_list[pos + i]) && is_delim(target_extension[i]))
148         return TRUE;
149
150      /* move to next position in the buffer */
151      pos += i;
152      while(!is_delim(extension_list[pos]))
153         pos++;
154      while(extension_list[pos] == ',')
155         pos++;
156   }
157
158   /* not found */
159   return FALSE;
160}
161
162
163
164/*-------------------------------------------------
165    image_find_extension - find an extension in an extension
166    list
167-------------------------------------------------*/
168
169int image_find_extension(const char *extension_list, const char *target_extension)
170{
171   /* the internal function allows something that we do not */
172   if (strchr(target_extension, ','))
173      return FALSE;
174
175   /* special case to allow ext to be in the form '.EXT' */
176   if (*target_extension == '.')
177      target_extension++;
178
179   /* do the actual work */
180   return image_internal_find_extension(extension_list, target_extension);
181}
182
183
184
185/*-------------------------------------------------
186    image_specify_extension - merge a comma-delimited
187    list of file extensions onto an existing list
188-------------------------------------------------*/
189
190void image_specify_extension(char *buffer, size_t buffer_len, const char *extension)
191{
192   int extension_pos = 0;
193   int len;
194   int found;
195
196   /* determine the length of the buffer */
197   len = strlen(buffer);
198
199   /* be aware that extension can be NULL */
200   if (extension != NULL)
201   {
202      while(extension[extension_pos] != '\0')
203      {
204         /* try to find the file extension */
205         found = image_internal_find_extension(buffer, &extension[extension_pos]);
206
207         /* append a delimiter if we have to */
208         if (!found && (len > 0))
209            len += snprintf(&buffer[len], buffer_len - len, ",");
210
211         /* move to the next extension, appending the extension if not found */
212         while(!is_delim(extension[extension_pos]))
213         {
214            if (!found)
215               len += snprintf(&buffer[len], buffer_len - len, "%c", extension[extension_pos]);
216            extension_pos++;
217         }
218         while(extension[extension_pos] == ',')
219            extension_pos++;
220      }
221   }
222}
223
src/lib/formats/fmsx_cas.h
r0r11458
1/*********************************************************************
2
3    fmsx_cas.h
4
5    Format code for MSX casette files
6
7*********************************************************************/
8
9#ifndef FMSX_CAS_H
10#define FMSX_CAS_H
11
12#include "cassimg.h"
13
14CASSETTE_FORMATLIST_EXTERN(fmsx_cassette_formats);
15
16#endif /* FMSX_CAS_H */
src/lib/formats/uef_cas.h
r0r11458
1/*
2    uef_cas.h
3
4    Format code for uef cassette files
5*/
6
7#ifndef UEF_CAS_H
8#define UEF_CAS_H
9
10#include "cassimg.h"
11
12extern const struct CassetteFormat uef_cassette_format;
13
14CASSETTE_FORMATLIST_EXTERN(uef_cassette_formats);
15
16#endif /* UEF_CAS_H */
src/lib/formats/dim_dsk.c
r0r11458
1/*********************************************************************
2
3    formats/dim_dsk.c
4
5    DIM disk images
6
7*********************************************************************/
8
9#include <string.h>
10
11#include "dim_dsk.h"
12#include "basicdsk.h"
13
14FLOPPY_IDENTIFY(dim_dsk_identify)
15{
16   UINT8 dim_header[16];
17
18   floppy_image_read(floppy, (UINT8*)dim_header,0xab,16);
19
20   if(strncmp((const char*)dim_header,"DIFC HEADER",11) == 0)
21      *vote = 100;
22   else
23      *vote = 0;
24   return FLOPPY_ERROR_SUCCESS;
25}
26
27FLOPPY_CONSTRUCT(dim_dsk_construct)
28{
29   struct basicdsk_geometry geometry;
30   // DIM disk image header, most of this is guesswork
31   int tracks = 77;
32   int heads = 2;
33   int sectors = 8;  // per track
34   int sectorlen = 1024;
35   int firstsector = 0x01;
36   UINT8 format_tmp;
37   int x;
38   UINT16 temp;
39
40   if(params)
41   {
42      // create
43      return FLOPPY_ERROR_UNSUPPORTED;
44   }
45
46   // Offset + 0 : disk format type (1 byte):
47    //  0 = 2HD / 2HDA (8 sector/track, 1024 bytes/sector, GAP#3 = 0x74)
48    //  1 = 2HS        (9 sector/track, 1024 bytes/sector, GAP#3 = 0x39)
49    //  2 = 2HC        (15 sector/track, 512 bytes/sector, GAP#3 = 0x54)
50    //  3 = 2HDE(68)   (9 sector/track, 1024 bytes/sector, GAP#3 = 0x39)
51    //  9 = 2HQ        (18 sector/track, 512 bytes/sector, GAP#3 = 0x54)
52    //  17 = N88-BASIC (26 sector/track, 256 bytes/sector, GAP#3 = 0x33)
53    //              or (26 sector/track, 128 bytes/sector, GAP#3 = 0x1a)
54    //
55   floppy_image_read(floppy, &format_tmp,0,1);
56
57   switch(format_tmp)
58   {
59   case 0x00:
60      sectors = 8;
61      sectorlen = 1024;
62      break;
63   case 0x01:
64   case 0x03:
65      sectors = 9;
66      sectorlen = 1024;
67      break;
68   case 0x02:
69      sectors = 15;
70      sectorlen = 512;
71      break;
72   case 0x09:
73      sectors = 18;
74      sectorlen = 512;
75      break;
76   case 0x11:
77      sectors = 26;
78      sectorlen = 256;
79      break;
80   }
81   tracks = 0;
82   for (x=0;x<86;x++)
83   {
84      floppy_image_read(floppy,&temp,(x*2)+1,2);
85      if(temp == 0x0101)
86         tracks++;
87   }
88   // TODO: expand on this basic implementation
89
90   logerror("FDD: DIM image loaded - type %i, %i tracks, %i sectors per track, %i bytes per sector\n", format_tmp,tracks, sectors,sectorlen);
91
92   memset(&geometry, 0, sizeof(geometry));
93   geometry.heads = heads;
94   geometry.first_sector_id = firstsector;
95   geometry.sector_length = sectorlen;
96   geometry.tracks = tracks+1;
97   geometry.sectors = sectors;
98   geometry.offset = 0x100;
99   return basicdsk_construct(floppy, &geometry);
100}
101
src/lib/formats/tzx_cas.c
r0r11458
1/*
2
3TZX (currently spectrum only) and spectrum TAP cassette format support by Wilbert Pol
4
5TODO:
6    Add support for the remaining block types:
7        case 0x15:  Direct Recording
8        case 0x18:  CSW Recording
9        case 0x19:  Generalized Data Block
10        case 0x21:  Group Start
11        case 0x22:  Group End
12        case 0x23:  Jump To Block
13        case 0x24:  Loop Start
14        case 0x25:  Loop End
15        case 0x26:  Call Sequence
16        case 0x27:  Return From Sequence
17        case 0x28:  Select Block
18        case 0x2A:  Stop Tape if in 48K Mode
19        case 0x2B:  Set signal level
20        case 0x5A:  Merge Block
21    Add support for the deprecated block types? Only if there is some image which need them:
22        case 0x16:  C64 ROM type data block
23        case 0x17:  C64 turbo tape data block
24        case 0x34:  Emulation info
25        case 0x40:  Snapshot block
26
27Notes:
28
29TZX format specification lists
308064 pulses for a header block and 3220 for a data block
31
32but the documentaiton on worldofspectrum lists
338063 pulses for a header block and 3223 for a data block
34
35see http://www.worldofspectrum.org/faq/reference/48kreference.htm#TapeDataStructure
36
37We are currently using the numbers from the TZX specification...
38
39*/
40
41#include "tzx_cas.h"
42
43
44#define TZX_WAV_FREQUENCY   44100
45#define WAVE_LOW      -0x5a9e
46#define WAVE_HIGH      0x5a9e
47#define WAVE_NULL      0
48
49#define SUPPORTED_VERSION_MAJOR   0x01
50
51#define INITIAL_MAX_BLOCK_COUNT   256
52#define BLOCK_COUNT_INCREMENTS   256
53
54static const UINT8 TZX_HEADER[8] = { 'Z','X','T','a','p','e','!',0x1a };
55
56/*
57  Global variables
58
59  Initialized by tzx_cas_get_wave_size, used (and cleaned up) by tzx_cas_fill_wave
60 */
61
62static INT16   wave_data = 0;
63static int   block_count = 0;
64static UINT8**   blocks = NULL;
65static float t_scale = 1;  /* for scaling T-states to the 4MHz CPC */
66
67static void toggle_wave_data(void)
68{
69   if (wave_data == WAVE_LOW)
70   {
71      wave_data = WAVE_HIGH;
72   }
73   else
74   {
75      wave_data = WAVE_LOW;
76   }
77}
78
79static void tzx_cas_get_blocks( const UINT8 *casdata, int caslen )
80{
81   int pos = sizeof(TZX_HEADER) + 2;
82   int max_block_count = INITIAL_MAX_BLOCK_COUNT;
83
84   blocks = (UINT8**)malloc(max_block_count);
85   memset(blocks,0,max_block_count);
86   block_count = 0;
87
88   while (pos < caslen)
89   {
90      UINT32 datasize;
91      UINT8 blocktype = casdata[pos];
92
93      if (block_count == max_block_count)
94      {
95         void   *old_blocks = blocks;
96         int   old_max_block_count = max_block_count;
97         printf("realoc");
98         max_block_count = max_block_count + BLOCK_COUNT_INCREMENTS;
99         blocks = (UINT8**)malloc(max_block_count);   // SHOULD NOT BE USING auto_alloc_array()
100         memset(blocks,0,max_block_count);
101         memcpy(blocks, old_blocks, old_max_block_count * sizeof(UINT8*));
102         if (blocks) free(old_blocks);
103      }
104
105      blocks[block_count] = (UINT8*)&casdata[pos];
106
107      pos += 1;
108
109      switch (blocktype)
110      {
111      case 0x10:
112         pos += 2;
113         datasize = casdata[pos] + (casdata[pos + 1] << 8);
114         pos += 2 + datasize;
115         break;
116      case 0x11:
117         pos += 0x0f;
118         datasize = casdata[pos] + (casdata[pos + 1] << 8) + (casdata[pos + 2] << 16);
119         pos += 3 + datasize;
120         break;
121      case 0x12:
122         pos += 4;
123         break;
124      case 0x13:
125         datasize = casdata[pos];
126         pos += 1 + 2 * datasize;
127         break;
128      case 0x14:
129         pos += 7;
130         datasize = casdata[pos] + (casdata[pos + 1] << 8) + (casdata[pos + 2] << 16);
131         pos += 3 + datasize;
132         break;
133      case 0x15:
134         pos += 5;
135         datasize = casdata[pos] + (casdata[pos + 1] << 8) + (casdata[pos + 2] << 16);
136         pos += 3 + datasize;
137         break;
138      case 0x20: case 0x23: case 0x24:
139         pos += 2;
140         break;
141      case 0x21: case 0x30:
142         datasize = casdata[pos];
143         pos += 1 + datasize;
144         break;
145      case 0x22: case 0x25: case 0x27:
146         break;
147      case 0x26:
148         datasize = casdata[pos] + (casdata[pos + 1] << 8);
149         pos += 2 + 2 * datasize;
150         break;
151      case 0x28: case 0x32:
152         datasize = casdata[pos] + (casdata[pos + 1] << 8);
153         pos += 2 + datasize;
154         break;
155      case 0x31:
156         pos += 1;
157         datasize = casdata[pos];
158         pos += 1 + datasize;
159         break;
160      case 0x33:
161         datasize = casdata[pos];
162         pos += 1 + 3 * datasize;
163         break;
164      case 0x34:
165         pos += 8;
166         break;
167      case 0x35:
168         pos += 0x10;
169         datasize = casdata[pos] + (casdata[pos + 1] << 8) + (casdata[pos + 2] << 16) + (casdata[pos + 3] << 24);
170         pos += 4 + datasize;
171         break;
172      case 0x40:
173         pos += 1;
174         datasize = casdata[pos] + (casdata[pos + 1] << 8) + (casdata[pos + 2] << 16);
175         pos += 3 + datasize;
176         break;
177      case 0x5A:
178         pos += 9;
179         break;
180      default:
181         datasize = casdata[pos] + (casdata[pos + 1] << 8) + (casdata[pos + 2] << 16) + (casdata[pos + 3] << 24);
182         pos += 4 + datasize;
183         break;
184      }
185
186      block_count++;
187   }
188}
189
190INLINE int millisec_to_samplecount( int millisec )
191{
192   return (int) (millisec * ((double)TZX_WAV_FREQUENCY / 1000.0));
193}
194
195INLINE int tcycles_to_samplecount( int tcycles )
196{
197   return (int) ((0.5 + (((double)TZX_WAV_FREQUENCY / 3500000) * (double)tcycles)) * t_scale);
198}
199
200static void tzx_output_wave( INT16 **buffer, int length )
201{
202   if (buffer == NULL)
203   {
204      return;
205   }
206
207   for ( ; length > 0; length--)
208   {
209      **buffer = wave_data;
210      *buffer = *buffer + 1;
211   }
212}
213
214static int tzx_cas_handle_block( INT16 **buffer, const UINT8 *bytes, int pause, int data_size, int pilot, int pilot_length, int sync1, int sync2, int bit0, int bit1, int bits_in_last_byte )
215{
216   int pilot_samples = tcycles_to_samplecount(pilot);
217   int sync1_samples = tcycles_to_samplecount(sync1);
218   int sync2_samples = tcycles_to_samplecount(sync2);
219   int bit0_samples = tcycles_to_samplecount(bit0);
220   int bit1_samples = tcycles_to_samplecount(bit1);
221   int data_index;
222   int size = 0;
223
224   /* Uncomment this to include into error.log a fully detailed analysis of each block */
225//  logerror("tzx_cas_block_size: pilot_length = %d, pilot_samples = %d, sync1_samples = %d, sync2_samples = %d, bit0_samples = %d, bit1_samples = %d\n", pilot_length, pilot_samples, sync1_samples, sync2_samples, bit0_samples, bit1_samples);
226
227   /* PILOT */
228   for ( ; pilot_length > 0; pilot_length--)
229   {
230      tzx_output_wave(buffer, pilot_samples);
231      size += pilot_samples;
232      toggle_wave_data();
233   }
234   /* SYNC1 */
235   if (sync1_samples > 0)
236   {
237      tzx_output_wave(buffer, sync1_samples);
238      size += sync1_samples;
239      toggle_wave_data();
240   }
241   /* SYNC2 */
242   if (sync2_samples > 0)
243   {
244      tzx_output_wave(buffer, sync2_samples);
245      size += sync2_samples;
246      toggle_wave_data();
247   }
248   /* data */
249   for (data_index = 0; data_index < data_size; data_index++)
250   {
251      UINT8 byte = bytes[data_index];
252      int   bits_to_go = (data_index == (data_size - 1)) ? bits_in_last_byte : 8;
253
254      for ( ; bits_to_go > 0; byte <<= 1, bits_to_go--)
255      {
256         int   bit_samples = (byte & 0x80) ? bit1_samples : bit0_samples;
257         tzx_output_wave(buffer, bit_samples);
258         size += bit_samples;
259         toggle_wave_data();
260         tzx_output_wave(buffer, bit_samples);
261         size += bit_samples;
262         toggle_wave_data();
263      }
264   }
265   /* pause */
266   if (pause > 0)
267   {
268      int start_pause_samples = millisec_to_samplecount(1);
269      int rest_pause_samples = millisec_to_samplecount(pause - 1);
270
271      tzx_output_wave(buffer, start_pause_samples);
272      size += start_pause_samples;
273      wave_data = WAVE_LOW;
274      tzx_output_wave(buffer, rest_pause_samples);
275      size += rest_pause_samples;
276   }
277   return size;
278}
279
280static void ascii_block_common_log( const char *block_type_string, UINT8 block_type )
281{
282   logerror("%s (type %02x) encountered.\n", block_type_string, block_type);
283   logerror("This block contains info on the .tzx file you are loading.\n");
284   logerror("Please include the following info in your bug reports, if the image has issue in M.E.S.S.\n");
285}
286
287static const char *const archive_ident[] =
288{
289   "Full title",
290   "Software house/publisher",
291   "Author(s)",
292   "Year of publication",
293   "Language",
294   "Game/utility type",
295   "Price",
296   "Protection scheme/loader",
297   "Origin",
298};
299
300static const char *const hw_info[] =
301{
302   "Tape runs on this machine / this hardware",
303   "Tape needs this machine / this hardware",
304   "Tape runs on this machine / this hardware, but does not require its special features",
305   "Tape does not run on this machine / this hardware",
306};
307
308/*  Will go through blocks and calculate number of samples needed.
309If buffer is not NULL the sample data will also be written. */
310static int tzx_cas_do_work( INT16 **buffer )
311{
312   int   current_block = 0;
313   int   size = 0;
314
315   wave_data = WAVE_LOW;
316
317   while (current_block < block_count)
318   {
319      int   pause_time;
320      int   data_size, text_size, total_size, i;
321      int   pilot, pilot_length, sync1, sync2;
322      int   bit0, bit1, bits_in_last_byte;
323      UINT8 *cur_block = blocks[current_block];
324      UINT8 block_type = cur_block[0];
325
326   /* Uncomment this to include into error.log a list of the types each block */
327//      logerror("tzx_cas_fill_wave: block %d, block_type %02x\n", current_block, block_type);
328
329      switch (block_type)
330      {
331      case 0x10:   /* Standard Speed Data Block (.TAP block) */
332         pause_time = cur_block[1] + (cur_block[2] << 8);
333         data_size = cur_block[3] + (cur_block[4] << 8);
334         pilot_length = (cur_block[5] == 0x00) ?  8064 : 3220;
335         size += tzx_cas_handle_block(buffer, &cur_block[5], pause_time, data_size, 2168, pilot_length, 667, 735, 855, 1710, 8);
336         current_block++;
337         break;
338      case 0x11:   /* Turbo Loading Data Block */
339         pilot = cur_block[1] + (cur_block[2] << 8);
340         sync1 = cur_block[3] + (cur_block[4] << 8);
341         sync2 = cur_block[5] + (cur_block[6] << 8);
342         bit0 = cur_block[7] + (cur_block[8] << 8);
343         bit1 = cur_block[9] + (cur_block[10] << 8);
344         pilot_length = cur_block[11] + (cur_block[12] << 8);
345         bits_in_last_byte = cur_block[13];
346         pause_time = cur_block[14] + (cur_block[15] << 8);
347         data_size = cur_block[16] + (cur_block[17] << 8) + (cur_block[18] << 16);
348         size += tzx_cas_handle_block(buffer, &cur_block[19], pause_time, data_size, pilot, pilot_length, sync1, sync2, bit0, bit1, bits_in_last_byte);
349         current_block++;
350         break;
351      case 0x12:   /* Pure Tone */
352         pilot = cur_block[1] + (cur_block[2] << 8);
353         pilot_length = cur_block[3] + (cur_block[4] << 8);
354         size += tzx_cas_handle_block(buffer, cur_block, 0, 0, pilot, pilot_length, 0, 0, 0, 0, 0);
355         current_block++;
356         break;
357      case 0x13:   /* Sequence of Pulses of Different Lengths */
358         for (data_size = 0; data_size < cur_block[1]; data_size++)
359         {
360            pilot = cur_block[2 + 2 * data_size] + (cur_block[3 + 2 * data_size] << 8);
361            size += tzx_cas_handle_block(buffer, cur_block, 0, 0, pilot, 1, 0, 0, 0, 0, 0);
362         }
363         current_block++;
364         break;
365      case 0x14:   /* Pure Data Block */
366         bit0 = cur_block[1] + (cur_block[2] << 8);
367         bit1 = cur_block[3] + (cur_block[4] << 8);
368         bits_in_last_byte = cur_block[5];
369         pause_time = cur_block[6] + (cur_block[7] << 8);
370         data_size = cur_block[8] + (cur_block[9] << 8) + (cur_block[10] << 16);
371         size += tzx_cas_handle_block(buffer, &cur_block[11], pause_time, data_size, 0, 0, 0, 0, bit0, bit1, bits_in_last_byte);
372         current_block++;
373         break;
374      case 0x20:   /* Pause (Silence) or 'Stop the Tape' Command */
375         pause_time = cur_block[1] + (cur_block[2] << 8);
376         if (pause_time == 0)
377         {
378            /* pause = 0 is used to let an emulator automagically stop the tape
379                   in MESS we do not do that, so we insert a 5 second pause. */
380            pause_time = 5000;
381         }
382         size += tzx_cas_handle_block(buffer, cur_block, pause_time, 0, 0, 0, 0, 0, 0, 0, 0);
383         current_block++;
384         break;
385      case 0x16:   /* C64 ROM Type Data Block */      // Deprecated in TZX 1.20
386      case 0x17:   /* C64 Turbo Tape Data Block */      // Deprecated in TZX 1.20
387      case 0x34:   /* Emulation Info */            // Deprecated in TZX 1.20
388      case 0x40:   /* Snapshot Block */            // Deprecated in TZX 1.20
389         logerror("Deprecated block type (%02x) encountered.\n", block_type);
390         logerror("Please look for an updated .tzx file.\n");
391         current_block++;
392         break;
393      case 0x30:   /* Text Description */
394         ascii_block_common_log("Text Description Block", block_type);
395         for (data_size = 0; data_size < cur_block[1]; data_size++)
396            logerror("%c", cur_block[2 + data_size]);
397         logerror("\n");
398         current_block++;
399         break;
400      case 0x31:   /* Message Block */
401         ascii_block_common_log("Message Block", block_type);
402         logerror("Expected duration of the message display: %02x\n", cur_block[1]);
403         logerror("Message: \n");
404         for (data_size = 0; data_size < cur_block[2]; data_size++)
405         {
406            logerror("%c", cur_block[3 + data_size]);
407            if (cur_block[3 + data_size] == 0x0d)
408               logerror("\n");
409         }
410         logerror("\n");
411         current_block++;
412         break;
413      case 0x32:   /* Archive Info */
414         ascii_block_common_log("Archive Info Block", block_type);
415         total_size = cur_block[1] + (cur_block[2] << 8);
416         text_size = 0;
417         for (data_size = 0; data_size < cur_block[3]; data_size++)   // data_size = number of text blocks, in this case
418         {
419            if (cur_block[4 + text_size] < 0x09)
420               logerror("%s: \n", archive_ident[cur_block[4 + text_size]]);
421            else
422               logerror("Comment(s): \n");
423
424            for (i = 0; i < cur_block[4 + text_size + 1]; i++)
425            {
426               logerror("%c", cur_block[4 + text_size + 2 + i]);
427            }
428            text_size += 2 + i;
429         }
430         logerror("\n");
431         if (text_size != total_size)
432            logerror("Malformed Archive Info Block (Text length different from the declared one).\n Please verify your tape image.\n");
433         current_block++;
434         break;
435      case 0x33:   /* Hardware Type */
436         ascii_block_common_log("Hardware Type Block", block_type);
437         for (data_size = 0; data_size < cur_block[1]; data_size++)   // data_size = number of hardware blocks, in this case
438         {
439            logerror("Hardware Type %02x - Hardware ID %02x - ", cur_block[2 + data_size * 3], cur_block[2 + data_size * 3 + 1]);
440            logerror("%s \n ", hw_info[cur_block[2 + data_size * 3 + 2]]);
441         }
442         current_block++;
443         break;
444      case 0x35:   /* Custom Info Block */
445         ascii_block_common_log("Custom Info Block", block_type);
446         for (data_size = 0; data_size < 10; data_size++)
447         {
448            logerror("%c", cur_block[1 + data_size]);
449         }
450         logerror(":\n");
451         text_size = cur_block[11] + (cur_block[12] << 8) + (cur_block[13] << 16) + (cur_block[14] << 24);
452         for (data_size = 0; data_size < text_size; data_size++)
453            logerror("%c", cur_block[15 + data_size]);
454         logerror("\n");
455         current_block++;
456         break;
457      case 0x5A:   /* "Glue" Block */
458         logerror("Glue Block (type %02x) encountered.\n", block_type);
459         logerror("Please use a .tzx handling utility to split the merged tape files.\n");
460         current_block++;
461         break;
462      case 0x15:   /* Direct Recording */
463      case 0x18:   /* CSW Recording */
464      case 0x19:   /* Generalized Data Block */
465      case 0x21:   /* Group Start */
466      case 0x22:   /* Group End */
467      case 0x23:   /* Jump To Block */
468      case 0x24:   /* Loop Start */
469      case 0x25:   /* Loop End */
470      case 0x26:   /* Call Sequence */
471      case 0x27:   /* Return From Sequence */
472      case 0x28:   /* Select Block */
473      case 0x2A:   /* Stop Tape if in 48K Mode */
474      case 0x2B:   /* Set signal level */
475      default:
476         logerror("Unsupported block type (%02x) encountered.\n", block_type);
477         current_block++;
478         break;
479      }
480   }
481   return size;
482}
483
484static int tzx_cas_to_wav_size( const UINT8 *casdata, int caslen )
485{
486   int size = 0;
487
488   /* Header size plus major and minor version number */
489   if (caslen < 10)
490   {
491      logerror("tzx_cas_to_wav_size: cassette image too small\n");
492      goto cleanup;
493   }
494
495   /* Check for correct header */
496   if (memcmp(casdata, TZX_HEADER, sizeof(TZX_HEADER)))
497   {
498      logerror("tzx_cas_to_wav_size: cassette image has incompatible header\n");
499      goto cleanup;
500   }
501
502   /* Check major version number in header */
503   if (casdata[0x08] > SUPPORTED_VERSION_MAJOR)
504   {
505      logerror("tzx_cas_to_wav_size: unsupported version\n");
506      goto cleanup;
507   }
508
509   tzx_cas_get_blocks(casdata, caslen);
510
511   logerror("tzx_cas_to_wav_size: %d blocks found\n", block_count);
512
513   if (block_count == 0)
514   {
515      logerror("tzx_cas_to_wav_size: no blocks found!\n");
516      goto cleanup;
517   }
518
519   size = tzx_cas_do_work(NULL);
520
521   return size;
522
523cleanup:
524   return -1;
525}
526
527static int tzx_cas_fill_wave( INT16 *buffer, int length, UINT8 *bytes )
528{
529   INT16 *p = buffer;
530   int   size = 0;
531   t_scale = 1.0;
532   size = tzx_cas_do_work(&p);
533   return size;
534}
535
536static int cdt_cas_fill_wave( INT16 *buffer, int length, UINT8 *bytes )
537{
538   INT16 *p = buffer;
539   int   size = 0;
540   t_scale = (40 / 35);  /* scale to 4MHz */
541   size = tzx_cas_do_work(&p);
542   return size;
543}
544
545static int tap_cas_to_wav_size( const UINT8 *casdata, int caslen )
546{
547   int size = 0;
548   const UINT8 *p = casdata;
549
550   while (p < casdata + caslen)
551   {
552      int data_size = p[0] + (p[1] << 8);
553      int pilot_length = (p[2] == 0x00) ? 8064 : 3220;   /* TZX specification */
554//      int pilot_length = (p[2] == 0x00) ? 8063 : 3223;    /* worldofspectrum */
555      logerror("tap_cas_to_wav_size: Handling TAP block containing 0x%X bytes", data_size);
556      p += 2;
557      size += tzx_cas_handle_block(NULL, p, 1000, data_size, 2168, pilot_length, 667, 735, 855, 1710, 8);
558      logerror(", total size is now: %d\n", size);
559      p += data_size;
560   }
561   return size;
562}
563
564static int tap_cas_fill_wave( INT16 *buffer, int length, UINT8 *bytes )
565{
566   INT16 *p = buffer;
567   int size = 0;
568
569   while (size < length)
570   {
571      int data_size = bytes[0] + (bytes[1] << 8);
572      int pilot_length = (bytes[2] == 0x00) ? 8064 : 3220;   /* TZX specification */
573//      int pilot_length = (bytes[2] == 0x00) ? 8063 : 3223;    /* worldofspectrum */
574      logerror("tap_cas_fill_wave: Handling TAP block containing 0x%X bytes\n", data_size);
575      bytes += 2;
576      size += tzx_cas_handle_block(&p, bytes, 1000, data_size, 2168, pilot_length, 667, 735, 855, 1710, 8);
577      bytes += data_size;
578   }
579   return size;
580}
581
582static const struct CassetteLegacyWaveFiller tzx_legacy_fill_wave =
583{
584   tzx_cas_fill_wave,         /* fill_wave */
585   -1,               /* chunk_size */
586   0,               /* chunk_samples */
587   tzx_cas_to_wav_size,         /* chunk_sample_calc */
588   TZX_WAV_FREQUENCY,         /* sample_frequency */
589   0,               /* header_samples */
590   0               /* trailer_samples */
591};
592
593static const struct CassetteLegacyWaveFiller tap_legacy_fill_wave =
594{
595   tap_cas_fill_wave,         /* fill_wave */
596   -1,               /* chunk_size */
597   0,               /* chunk_samples */
598   tap_cas_to_wav_size,         /* chunk_sample_calc */
599   TZX_WAV_FREQUENCY,         /* sample_frequency */
600   0,               /* header_samples */
601   0               /* trailer_samples */
602};
603
604static const struct CassetteLegacyWaveFiller cdt_legacy_fill_wave =
605{
606   cdt_cas_fill_wave,         /* fill_wave */
607   -1,               /* chunk_size */
608   0,               /* chunk_samples */
609   tzx_cas_to_wav_size,         /* chunk_sample_calc */
610   TZX_WAV_FREQUENCY,         /* sample_frequency */
611   0,               /* header_samples */
612   0               /* trailer_samples */
613};
614
615static casserr_t tzx_cassette_identify( cassette_image *cassette, struct CassetteOptions *opts )
616{
617   return cassette_legacy_identify(cassette, opts, &tzx_legacy_fill_wave);
618}
619
620static casserr_t tap_cassette_identify( cassette_image *cassette, struct CassetteOptions *opts )
621{
622   return cassette_legacy_identify(cassette, opts, &tap_legacy_fill_wave);
623}
624
625static casserr_t cdt_cassette_identify( cassette_image *cassette, struct CassetteOptions *opts )
626{
627   return cassette_legacy_identify(cassette, opts, &cdt_legacy_fill_wave);
628}
629
630static casserr_t tzx_cassette_load( cassette_image *cassette )
631{
632   return cassette_legacy_construct(cassette, &tzx_legacy_fill_wave);
633}
634
635static casserr_t tap_cassette_load( cassette_image *cassette )
636{
637   return cassette_legacy_construct(cassette, &tap_legacy_fill_wave);
638}
639
640static casserr_t cdt_cassette_load( cassette_image *cassette )
641{
642   return cassette_legacy_construct(cassette, &cdt_legacy_fill_wave);
643}
644
645static const struct CassetteFormat tzx_cassette_format =
646{
647   "tzx",
648   tzx_cassette_identify,
649   tzx_cassette_load,
650   NULL
651};
652
653static const struct CassetteFormat tap_cassette_format =
654{
655   "tap,blk",
656   tap_cassette_identify,
657   tap_cassette_load,
658   NULL
659};
660
661static const struct CassetteFormat cdt_cassette_format =
662{
663   "cdt",
664   cdt_cassette_identify,
665   cdt_cassette_load,
666   NULL
667};
668
669CASSETTE_FORMATLIST_START(tzx_cassette_formats)
670   CASSETTE_FORMAT(tzx_cassette_format)
671   CASSETTE_FORMAT(tap_cassette_format)
672CASSETTE_FORMATLIST_END
673
674CASSETTE_FORMATLIST_START(cdt_cassette_formats)
675   CASSETTE_FORMAT(cdt_cassette_format)
676CASSETTE_FORMATLIST_END
677
src/lib/formats/imageutl.h
r0r11458
1/***************************************************************************
2
3    imageutl.h
4
5    Image related utilities
6
7****************************************************************************
8
9    Copyright Aaron Giles
10    All rights reserved.
11
12    Redistribution and use in source and binary forms, with or without
13    modification, are permitted provided that the following conditions are
14    met:
15
16        * Redistributions of source code must retain the above copyright
17          notice, this list of conditions and the following disclaimer.
18        * Redistributions in binary form must reproduce the above copyright
19          notice, this list of conditions and the following disclaimer in
20          the documentation and/or other materials provided with the
21          distribution.
22        * Neither the name 'MAME' nor the names of its contributors may be
23          used to endorse or promote products derived from this software
24          without specific prior written permission.
25
26    THIS SOFTWARE IS PROVIDED BY AARON GILES ''AS IS'' AND ANY EXPRESS OR
27    IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
28    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
29    DISCLAIMED. IN NO EVENT SHALL AARON GILES BE LIABLE FOR ANY DIRECT,
30    INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
31    (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
32    SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33    HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
34    STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
35    IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36    POSSIBILITY OF SUCH DAMAGE.
37
38***************************************************************************/
39
40#pragma once
41
42#ifndef __IMAGEUTL_H__
43#define __IMAGEUTL_H__
44
45#include "osdcore.h"
46
47/* -----------------------------------------------------------------------
48 * CRC stuff
49 * ----------------------------------------------------------------------- */
50unsigned short ccitt_crc16(unsigned short crc, const unsigned char *buffer, size_t buffer_len);
51unsigned short ccitt_crc16_one( unsigned short crc, const unsigned char data );
52
53/* -----------------------------------------------------------------------
54 * Alignment-friendly integer placement
55 * ----------------------------------------------------------------------- */
56
57INLINE void place_integer_be(void *ptr, size_t offset, size_t size, UINT64 value)
58{
59   UINT8 *byte_ptr = ((UINT8 *) ptr) + offset;
60   UINT16 val16;
61   UINT32 val32;
62
63   switch(size)
64   {
65      case 2:
66         val16 = BIG_ENDIANIZE_INT16((UINT16) value);
67         memcpy(byte_ptr, &val16, sizeof(val16));
68         break;
69
70      case 4:
71         val32 = BIG_ENDIANIZE_INT32((UINT32) value);
72         memcpy(byte_ptr, &val32, sizeof(val32));
73         break;
74
75      default:
76         if (size >= 1)   byte_ptr[0] = (UINT8) (value >> ((size - 1) * 8));
77         if (size >= 2)   byte_ptr[1] = (UINT8) (value >> ((size - 2) * 8));
78         if (size >= 3)   byte_ptr[2] = (UINT8) (value >> ((size - 3) * 8));
79         if (size >= 4)   byte_ptr[3] = (UINT8) (value >> ((size - 4) * 8));
80         if (size >= 5)   byte_ptr[4] = (UINT8) (value >> ((size - 5) * 8));
81         if (size >= 6)   byte_ptr[5] = (UINT8) (value >> ((size - 6) * 8));
82         if (size >= 7)   byte_ptr[6] = (UINT8) (value >> ((size - 7) * 8));
83         if (size >= 8)   byte_ptr[7] = (UINT8) (value >> ((size - 8) * 8));
84         break;
85   }
86}
87
88INLINE UINT64 pick_integer_be(const void *ptr, size_t offset, size_t size)
89{
90   UINT64 result = 0;
91   const UINT8 *byte_ptr = ((const UINT8 *) ptr) + offset;
92   UINT16 val16;
93   UINT32 val32;
94
95   switch(size)
96   {
97      case 1:
98         result = *byte_ptr;
99         break;
100
101      case 2:
102         memcpy(&val16, byte_ptr, sizeof(val16));
103         result = BIG_ENDIANIZE_INT16(val16);
104         break;
105
106      case 4:
107         memcpy(&val32, byte_ptr, sizeof(val32));
108         result = BIG_ENDIANIZE_INT32(val32);
109         break;
110
111      default:
112         if (size >= 1)   result |= ((UINT64) byte_ptr[0]) << ((size - 1) * 8);
113         if (size >= 2)   result |= ((UINT64) byte_ptr[1]) << ((size - 2) * 8);
114         if (size >= 3)   result |= ((UINT64) byte_ptr[2]) << ((size - 3) * 8);
115         if (size >= 4)   result |= ((UINT64) byte_ptr[3]) << ((size - 4) * 8);
116         if (size >= 5)   result |= ((UINT64) byte_ptr[4]) << ((size - 5) * 8);
117         if (size >= 6)   result |= ((UINT64) byte_ptr[5]) << ((size - 6) * 8);
118         if (size >= 7)   result |= ((UINT64) byte_ptr[6]) << ((size - 7) * 8);
119         if (size >= 8)   result |= ((UINT64) byte_ptr[7]) << ((size - 8) * 8);
120         break;
121   }
122   return result;
123}
124
125INLINE void place_integer_le(void *ptr, size_t offset, size_t size, UINT64 value)
126{
127   UINT8 *byte_ptr = ((UINT8 *) ptr) + offset;
128   UINT16 val16;
129   UINT32 val32;
130
131   switch(size)
132   {
133      case 2:
134         val16 = LITTLE_ENDIANIZE_INT16((UINT16) value);
135         memcpy(byte_ptr, &val16, sizeof(val16));
136         break;
137
138      case 4:
139         val32 = LITTLE_ENDIANIZE_INT32((UINT32) value);
140         memcpy(byte_ptr, &val32, sizeof(val32));
141         break;
142
143      default:
144         if (size >= 1)   byte_ptr[0] = (UINT8) (value >> (0 * 8));
145         if (size >= 2)   byte_ptr[1] = (UINT8) (value >> (1 * 8));
146         if (size >= 3)   byte_ptr[2] = (UINT8) (value >> (2 * 8));
147         if (size >= 4)   byte_ptr[3] = (UINT8) (value >> (3 * 8));
148         if (size >= 5)   byte_ptr[4] = (UINT8) (value >> (4 * 8));
149         if (size >= 6)   byte_ptr[5] = (UINT8) (value >> (5 * 8));
150         if (size >= 7)   byte_ptr[6] = (UINT8) (value >> (6 * 8));
151         if (size >= 8)   byte_ptr[7] = (UINT8) (value >> (7 * 8));
152         break;
153   }
154}
155
156INLINE UINT64 pick_integer_le(const void *ptr, size_t offset, size_t size)
157{
158   UINT64 result = 0;
159   const UINT8 *byte_ptr = ((const UINT8 *) ptr) + offset;
160   UINT16 val16;
161   UINT32 val32;
162
163   switch(size)
164   {
165      case 1:
166         result = *byte_ptr;
167         break;
168
169      case 2:
170         memcpy(&val16, byte_ptr, sizeof(val16));
171         result = LITTLE_ENDIANIZE_INT16(val16);
172         break;
173
174      case 4:
175         memcpy(&val32, byte_ptr, sizeof(val32));
176         result = LITTLE_ENDIANIZE_INT32(val32);
177         break;
178
179      default:
180         if (size >= 1)   result |= ((UINT64) byte_ptr[0]) << (0 * 8);
181         if (size >= 2)   result |= ((UINT64) byte_ptr[1]) << (1 * 8);
182         if (size >= 3)   result |= ((UINT64) byte_ptr[2]) << (2 * 8);
183         if (size >= 4)   result |= ((UINT64) byte_ptr[3]) << (3 * 8);
184         if (size >= 5)   result |= ((UINT64) byte_ptr[4]) << (4 * 8);
185         if (size >= 6)   result |= ((UINT64) byte_ptr[5]) << (5 * 8);
186         if (size >= 7)   result |= ((UINT64) byte_ptr[6]) << (6 * 8);
187         if (size >= 8)   result |= ((UINT64) byte_ptr[7]) << (7 * 8);
188         break;
189   }
190   return result;
191}
192
193/* -----------------------------------------------------------------------
194 * Miscellaneous
195 * ----------------------------------------------------------------------- */
196
197/* miscellaneous functions */
198int compute_log2(int val);
199
200/* -----------------------------------------------------------------------
201 * Extension list handling
202 * ----------------------------------------------------------------------- */
203
204int image_find_extension(const char *extensions, const char *ext);
205void image_specify_extension(char *buffer, size_t buffer_len, const char *extension);
206
207#endif /* __IMAGEUTL_H__ */
src/lib/formats/ioprocs.c
r0r11458
1#include <stdio.h>
2#include <string.h>
3#include "osdcore.h"
4#include "ioprocs.h"
5
6
7/*********************************************************************
8    ioprocs implementation on stdio
9*********************************************************************/
10
11static void stdio_closeproc(void *file)
12{
13   fclose((FILE*)file);
14}
15
16static int stdio_seekproc(void *file, INT64 offset, int whence)
17{
18   return fseek((FILE*)file, (long) offset, whence);
19}
20
21static size_t stdio_readproc(void *file, void *buffer, size_t length)
22{
23   return fread(buffer, 1, length, (FILE*)file);
24}
25
26static size_t stdio_writeproc(void *file, const void *buffer, size_t length)
27{
28   return fwrite(buffer, 1, length, (FILE*)file);
29}
30
31static UINT64 stdio_filesizeproc(void *file)
32{
33   long l, sz;
34   l = ftell((FILE*)file);
35   if (fseek((FILE*)file, 0, SEEK_END))
36      return (size_t) -1;
37   sz = ftell((FILE*)file);
38   if (fseek((FILE*)file, l, SEEK_SET))
39      return (size_t) -1;
40   return (size_t) sz;
41}
42
43const struct io_procs stdio_ioprocs =
44{
45   stdio_closeproc,
46   stdio_seekproc,
47   stdio_readproc,
48   stdio_writeproc,
49   stdio_filesizeproc
50};
51
52const struct io_procs stdio_ioprocs_noclose =
53{
54   NULL,
55   stdio_seekproc,
56   stdio_readproc,
57   stdio_writeproc,
58   stdio_filesizeproc
59};
60
61
62
63/*********************************************************************
64    calls for accessing generic IO
65*********************************************************************/
66
67static void io_generic_seek(struct io_generic *generic, UINT64 offset)
68{
69   generic->procs->seekproc(generic->file, offset, SEEK_SET);
70}
71
72
73
74void io_generic_close(struct io_generic *generic)
75{
76   if (generic->procs->closeproc)
77      generic->procs->closeproc(generic->file);
78}
79
80
81
82void io_generic_read(struct io_generic *generic, void *buffer, UINT64 offset, size_t length)
83{
84   UINT64 size;
85   size_t bytes_read;
86
87   size = io_generic_size(generic);
88   if (size <= offset)
89   {
90      bytes_read = 0;
91   }
92   else
93   {
94      io_generic_seek(generic, offset);
95      bytes_read = generic->procs->readproc(generic->file, buffer, length);
96   }
97   memset(((UINT8 *) buffer) + bytes_read, generic->filler, length - bytes_read);
98}
99
100
101
102void io_generic_write(struct io_generic *generic, const void *buffer, UINT64 offset, size_t length)
103{
104   UINT64 filler_size = 0;
105   char filler_buffer[1024];
106   size_t bytes_to_write;
107   UINT64 size;
108
109   if (offset > 0x29c50)
110   {
111      offset = offset;
112   }
113
114   size = io_generic_size(generic);
115
116   if (size < offset)
117   {
118      filler_size = offset - size;
119      offset = size;
120   }
121
122   io_generic_seek(generic, offset);
123
124   if (filler_size)
125   {
126      memset(filler_buffer, generic->filler, sizeof(buffer));
127      do
128      {
129         bytes_to_write = (filler_size > sizeof(filler_buffer)) ? sizeof(filler_buffer) : (size_t) filler_size;
130         generic->procs->writeproc(generic->file, filler_buffer, bytes_to_write);
131         filler_size -= bytes_to_write;
132      }
133      while(filler_size > 0);
134   }
135
136   if (length > 0)
137      generic->procs->writeproc(generic->file, buffer, length);
138}
139
140
141
142void io_generic_write_filler(struct io_generic *generic, UINT8 filler, UINT64 offset, size_t length)
143{
144   UINT8 buffer[512];
145   size_t this_length;
146
147   memset(buffer, filler, MIN(length, sizeof(buffer)));
148
149   while(length > 0)
150   {
151      this_length = MIN(length, sizeof(buffer));
152      io_generic_write(generic, buffer, offset, this_length);
153      offset += this_length;
154      length -= this_length;
155   }
156}
157
158
159
160UINT64 io_generic_size(struct io_generic *generic)
161{
162   return generic->procs->filesizeproc(generic->file);
163}
164
165
166
src/lib/formats/vt_cas.c
r0r11458
1#include "formats/vt_cas.h"
2
3/*********************************************************************
4    vtech 1/2 agnostic
5*********************************************************************/
6
7#define SILENCE 8000
8
9static int generic_fill_wave(INT16 *buffer, int length, UINT8 *code, int bitsamples, int bytesamples, int lo, INT16 *(*fill_wave_byte)(INT16 *buffer, int byte))
10{
11   static int nullbyte;
12
13    if( code == CODE_HEADER )
14    {
15      int i;
16
17        if( length < SILENCE )
18         return -1;
19      for( i = 0; i < SILENCE; i++ )
20         *buffer++ = 0;
21
22      nullbyte = 0;
23
24        return SILENCE;
25    }
26
27   if( code == CODE_TRAILER )
28    {
29      int i;
30
31        /* silence at the end */
32      for( i = 0; i < length; i++ )
33         *buffer++ = 0;
34        return length;
35    }
36
37    if( length < bytesamples )
38      return -1;
39
40   buffer = fill_wave_byte(buffer, *code);
41
42    if( !nullbyte && *code == 0 )
43   {
44      int i;
45      for( i = 0; i < 2*bitsamples; i++ )
46         *buffer++ = lo;
47        nullbyte = 1;
48      return bytesamples + 2 * bitsamples;
49   }
50
51    return bytesamples;
52}
53
54
55/*********************************************************************
56    vtech 1
57*********************************************************************/
58
59#define V1_LO   -32768
60#define V1_HI   +32767
61
62#define V1_BITSAMPLES   6
63#define V1_BYTESAMPLES   8*V1_BITSAMPLES
64
65static INT16 *vtech1_fill_wave_byte(INT16 *buffer, int byte)
66{
67   int i;
68
69    for( i = 7; i >= 0; i-- )
70   {
71      *buffer++ = V1_HI;   /* initial cycle */
72      *buffer++ = V1_LO;
73      if( (byte >> i) & 1 )
74      {
75         *buffer++ = V1_HI; /* two more cycles */
76            *buffer++ = V1_LO;
77         *buffer++ = V1_HI;
78            *buffer++ = V1_LO;
79        }
80      else
81      {
82         *buffer++ = V1_HI; /* one slow cycle */
83         *buffer++ = V1_HI;
84         *buffer++ = V1_LO;
85         *buffer++ = V1_LO;
86        }
87   }
88    return buffer;
89}
90
91static int vtech1_cassette_fill_wave(INT16 *buffer, int length, UINT8 *code)
92{
93   return generic_fill_wave(buffer, length, code, V1_BITSAMPLES, V1_BYTESAMPLES, V1_LO, vtech1_fill_wave_byte);
94}
95
96static const struct CassetteLegacyWaveFiller vtech1_legacy_fill_wave =
97{
98   vtech1_cassette_fill_wave,   /* fill_wave */
99   1,                     /* chunk_size */
100   V1_BYTESAMPLES,            /* chunk_samples */
101   NULL,                  /* chunk_sample_calc */
102   600*V1_BITSAMPLES,         /* sample_frequency */
103   600*V1_BITSAMPLES,         /* header_samples */
104   600*V1_BITSAMPLES         /* trailer_samples */
105};
106
107static casserr_t vtech1_cas_identify(cassette_image *cassette, struct CassetteOptions *opts)
108{
109   return cassette_legacy_identify(cassette, opts, &vtech1_legacy_fill_wave);
110}
111
112static casserr_t vtech1_cas_load(cassette_image *cassette)
113{
114   return cassette_legacy_construct(cassette, &vtech1_legacy_fill_wave);
115}
116
117static const struct CassetteFormat vtech1_cas_format =
118{
119   "cas",
120   vtech1_cas_identify,
121   vtech1_cas_load,
122   NULL
123};
124
125CASSETTE_FORMATLIST_START(vtech1_cassette_formats)
126   CASSETTE_FORMAT(vtech1_cas_format)
127CASSETTE_FORMATLIST_END
128
129/*********************************************************************
130    vtech 2
131*********************************************************************/
132
133#define VT2_LO   -20000
134#define VT2_HI   +20000
135
136#define VT2_BITSAMPLES   18
137#define VT2_BYTESAMPLES 8*VT2_BITSAMPLES
138
139static const INT16 vtech2_bit0[VT2_BITSAMPLES] =
140{
141   /* short cycle, long cycles */
142   VT2_HI,VT2_HI,VT2_HI,VT2_LO,VT2_LO,VT2_LO,
143   VT2_HI,VT2_HI,VT2_HI,VT2_HI,VT2_HI,VT2_HI,
144   VT2_LO,VT2_LO,VT2_LO,VT2_LO,VT2_LO,VT2_LO
145};
146
147static const INT16 vtech2_bit1[VT2_BITSAMPLES] =
148{
149   /* three short cycle */
150   VT2_HI,VT2_HI,VT2_HI,VT2_LO,VT2_LO,VT2_LO,
151   VT2_HI,VT2_HI,VT2_HI,VT2_LO,VT2_LO,VT2_LO,
152   VT2_HI,VT2_HI,VT2_HI,VT2_LO,VT2_LO,VT2_LO
153};
154
155
156static INT16 *vtech2_fill_wave_bit(INT16 *buffer, int bit)
157{
158   int i;
159   if( bit )
160   {
161      for( i = 0; i < VT2_BITSAMPLES; i++ )
162         *buffer++ = vtech2_bit1[i];
163    }
164   else
165   {
166      for( i = 0; i < VT2_BITSAMPLES; i++ )
167         *buffer++ = vtech2_bit0[i];
168    }
169   return buffer;
170}
171
172
173static INT16 *vtech2_fill_wave_byte(INT16 *buffer, int byte)
174{
175   buffer = vtech2_fill_wave_bit(buffer, (byte >> 7) & 1);
176   buffer = vtech2_fill_wave_bit(buffer, (byte >> 6) & 1);
177   buffer = vtech2_fill_wave_bit(buffer, (byte >> 5) & 1);
178   buffer = vtech2_fill_wave_bit(buffer, (byte >> 4) & 1);
179   buffer = vtech2_fill_wave_bit(buffer, (byte >> 3) & 1);
180   buffer = vtech2_fill_wave_bit(buffer, (byte >> 2) & 1);
181   buffer = vtech2_fill_wave_bit(buffer, (byte >> 1) & 1);
182   buffer = vtech2_fill_wave_bit(buffer, (byte >> 0) & 1);
183    return buffer;
184}
185
186static int vtech2_cassette_fill_wave(INT16 *buffer, int length, UINT8 *code)
187{
188   return generic_fill_wave(buffer, length, code, VT2_BITSAMPLES, VT2_BYTESAMPLES, VT2_LO, vtech2_fill_wave_byte);
189}
190
191static const struct CassetteLegacyWaveFiller vtech2_legacy_fill_wave =
192{
193   vtech2_cassette_fill_wave,   /* fill_wave */
194   1,                     /* chunk_size */
195   VT2_BYTESAMPLES,         /* chunk_samples */
196   NULL,                  /* chunk_sample_calc */
197   600*VT2_BITSAMPLES,         /* sample_frequency */
198   600*VT2_BITSAMPLES,         /* header_samples */
199   600*VT2_BITSAMPLES         /* trailer_samples */
200};
201
202static casserr_t vtech2_cas_identify(cassette_image *cassette, struct CassetteOptions *opts)
203{
204   return cassette_legacy_identify(cassette, opts, &vtech2_legacy_fill_wave);
205}
206
207static casserr_t vtech2_cas_load(cassette_image *cassette)
208{
209   return cassette_legacy_construct(cassette, &vtech2_legacy_fill_wave);
210}
211
212static const struct CassetteFormat vtech2_cas_format =
213{
214   "cas",
215   vtech2_cas_identify,
216   vtech2_cas_load,
217   NULL
218};
219
220CASSETTE_FORMATLIST_START(vtech2_cassette_formats)
221   CASSETTE_FORMAT(vtech2_cas_format)
222CASSETTE_FORMATLIST_END
src/lib/formats/dim_dsk.h
r0r11458
1/*********************************************************************
2
3    formats/dim_dsk.h
4
5    DIM disk images
6
7*********************************************************************/
8
9#ifndef DIM_DSK_H
10#define DIM_DSK_H
11
12#include "flopimg.h"
13
14/**************************************************************************/
15
16FLOPPY_IDENTIFY(dim_dsk_identify);
17FLOPPY_CONSTRUCT(dim_dsk_construct);
18
19#endif /* DIM_DSK_H */
src/lib/formats/tzx_cas.h
r0r11458
1/*
2
3       tzx_cas.h
4
5       Format code for tzx cassette files
6*/
7
8#ifndef TZX_CAS_H
9#define TZX_CAS_H
10
11#include "cassimg.h"
12
13CASSETTE_FORMATLIST_EXTERN(tzx_cassette_formats);
14CASSETTE_FORMATLIST_EXTERN(cdt_cassette_formats);
15
16#endif /* TZX_CAS_H */
src/lib/formats/cqm_dsk.c
r0r11458
1/*********************************************************************
2
3    formats/cqm_dsk.c
4
5    CopyQM disk images
6
7*********************************************************************/
8
9#include <string.h>
10#include "flopimg.h"
11
12#define CQM_HEADER_SIZE 133
13
14struct cqmdsk_tag
15{
16   int heads;
17   int tracks;
18   int sector_size;
19   int sector_per_track;
20   int sector_base;
21   int interleave;
22   int skew;
23
24   UINT8* buf;
25   UINT64 track_offsets[84*2]; /* offset within data for each track */
26};
27
28
29static struct cqmdsk_tag *get_tag(floppy_image *floppy)
30{
31   struct cqmdsk_tag *tag;
32   tag = (cqmdsk_tag *)floppy_tag(floppy );
33   return tag;
34}
35
36
37
38FLOPPY_IDENTIFY( cqm_dsk_identify )
39{
40   UINT8 header[2];
41
42   floppy_image_read(floppy, header, 0, 2);
43   if (header[0]=='C' && header[1]=='Q') {
44      *vote = 100;
45   } else {
46      *vote = 0;
47   }
48   return FLOPPY_ERROR_SUCCESS;
49}
50
51static int cqm_get_heads_per_disk(floppy_image *floppy)
52{
53   return get_tag(floppy)->heads;
54}
55
56static int cqm_get_tracks_per_disk(floppy_image *floppy)
57{
58   return get_tag(floppy)->tracks;
59}
60
61static UINT64 cqm_get_track_offset(floppy_image *floppy, int head, int track)
62{
63   return get_tag(floppy)->track_offsets[(track<<1) + head];
64}
65
66static floperr_t get_offset(floppy_image *floppy, int head, int track, int sector, int sector_is_index, UINT64 *offset)
67{
68   UINT64 pos = 0;
69   UINT8 data;
70   INT16 len;
71   int s;
72
73   if ((head < 0) || (head >= get_tag(floppy)->heads) || (track < 0) || (track >= get_tag(floppy)->tracks)
74         || (sector < 0) )
75      return FLOPPY_ERROR_SEEKERROR;
76
77   pos = cqm_get_track_offset(floppy,head,track);
78   s = 0;
79   do {
80      floppy_image_read(floppy, &len, pos, 2);
81      pos+=2;
82      if(len<0) {
83         floppy_image_read(floppy, &data, pos, 1);
84         memset(get_tag(floppy)->buf + s,data, -len);
85         pos++;
86         s += -len;
87      } else {
88         floppy_image_read(floppy, get_tag(floppy)->buf + s, pos, len);
89         pos+=len;
90         s += len;
91      }
92   } while(s<get_tag(floppy)->sector_size*get_tag(floppy)->sector_per_track);
93
94   if (offset)
95      *offset = sector * get_tag(floppy)->sector_size;
96   return FLOPPY_ERROR_SUCCESS;
97}
98
99
100
101static floperr_t internal_cqm_read_sector(floppy_image *floppy, int head, int track, int sector, int sector_is_index, void *buffer, size_t buflen)
102{
103   UINT64 offset;
104   floperr_t err;
105
106   // take sector offset
107   err = get_offset(floppy, head, track, sector, sector_is_index, &offset);
108   if (err)
109      return err;
110   memcpy(buffer,get_tag(floppy)->buf+offset,buflen);
111   return FLOPPY_ERROR_SUCCESS;
112}
113
114
115static floperr_t cqm_read_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen)
116{
117   return internal_cqm_read_sector(floppy, head, track, sector, FALSE, buffer, buflen);
118}
119
120static floperr_t cqm_read_indexed_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen)
121{
122   return internal_cqm_read_sector(floppy, head, track, sector, TRUE, buffer, buflen);
123}
124
125static floperr_t cqm_get_sector_length(floppy_image *floppy, int head, int track, int sector, UINT32 *sector_length)
126{
127   floperr_t err;
128   err = get_offset(floppy, head, track, sector, FALSE, NULL);
129   if (err)
130      return err;
131
132   if (sector_length) {
133      *sector_length = get_tag(floppy)->sector_size;
134   }
135   return FLOPPY_ERROR_SUCCESS;
136}
137
138static floperr_t cqm_get_indexed_sector_info(floppy_image *floppy, int head, int track, int sector_index, int *cylinder, int *side, int *sector, UINT32 *sector_length, unsigned long *flags)
139{
140   if (sector_index >= get_tag(floppy)->sector_per_track) return FLOPPY_ERROR_SEEKERROR;
141
142   if (cylinder) {
143      *cylinder = track;
144   }
145   if (side) {
146      *side = head;
147   }
148   if (sector) {
149      *sector = get_tag(floppy)->sector_base + sector_index;
150   }
151   if (sector_length) {
152      *sector_length = get_tag(floppy)->sector_size;
153   }
154   if (flags)
155      *flags = 0;
156   return FLOPPY_ERROR_SUCCESS;
157}
158
159FLOPPY_CONSTRUCT( cqm_dsk_construct )
160{
161   struct FloppyCallbacks *callbacks;
162   struct cqmdsk_tag *tag;
163   UINT8 header[CQM_HEADER_SIZE];
164   UINT64 pos = 0;
165   INT16 len;
166   int head;
167   int track;
168   int s;
169   if(params)
170   {
171      // create
172      return FLOPPY_ERROR_UNSUPPORTED;
173   }
174
175   tag = (struct cqmdsk_tag *) floppy_create_tag(floppy, sizeof(struct cqmdsk_tag));
176   if (!tag)
177      return FLOPPY_ERROR_OUTOFMEMORY;
178
179   floppy_image_read(floppy, header, 0, CQM_HEADER_SIZE);
180
181   tag->sector_size      = (header[0x04] << 8) + header[0x03];
182   tag->sector_per_track = header[0x10];
183   tag->heads           = header[0x12];
184   tag->tracks           = header[0x5b];
185   tag->sector_base     = header[0x71] + 1;
186   tag->interleave        = header[0x74];
187   tag->skew           = header[0x75];
188
189   // header + comment size to position on first data block
190
191   pos = CQM_HEADER_SIZE + (header[0x70] << 8) + header[0x6f];
192   track = 0;
193   head = 0;
194   tag->buf = (UINT8*)malloc(tag->sector_size*tag->sector_per_track);
195   do {
196      tag->track_offsets[(track<<1) + head] = pos;
197      s = 0;
198      do {
199         floppy_image_read(floppy, &len, pos, 2);
200         pos+=2;
201         if(len<0) {
202            pos++;
203            s += -len;
204         } else {
205            pos+=len;
206            s += len;
207         }
208      } while(s<tag->sector_size*tag->sector_per_track);
209      if(head ==0 && tag->heads > 1) {
210         head = 1;
211      } else {
212         head = 0;
213         track++;
214      }
215   } while(pos < floppy_image_size(floppy));
216
217
218   callbacks = floppy_callbacks(floppy);
219   callbacks->read_sector = cqm_read_sector;
220   callbacks->read_indexed_sector = cqm_read_indexed_sector;
221   callbacks->get_sector_length = cqm_get_sector_length;
222   callbacks->get_heads_per_disk = cqm_get_heads_per_disk;
223   callbacks->get_tracks_per_disk = cqm_get_tracks_per_disk;
224   callbacks->get_indexed_sector_info = cqm_get_indexed_sector_info;
225
226   return FLOPPY_ERROR_SUCCESS;
227}
228
src/lib/formats/csw_cas.c
r0r11458
1/*
2CSW format
3----------
4Header Description
5
6Offset  Value   Type    Description
70x00    (note)  ASCII   22 bytes "Compressed Square Wave"  signature
80x16    0x1A    BYTE    Terminator code
90x17    0x02    BYTE    CSW major revision number
100x18    0x00    BYTE    CSW minor revision number
110x19            DWORD   Sample rate
120x1D            DWORD   Total number of pulses (after decompression)
130x21            BYTE    Compression type  0x01: RLE    0x02: Z-RLE
140x22            BYTE    Flags   b0: initial polarity: if set, the signal starts at logical high
150x23    HDR     BYTE    Header extension length in bytes (0x00)
160x24            ASCII   16 bytes free use
170x34            BYTE    Start of Header is HDR>0
18
19
20
21*/
22
23#include <string.h>
24
25#include "zlib.h"
26#include "uef_cas.h"
27#include "csw_cas.h"
28
29
30#define CSW_WAV_FREQUENCY   44100
31
32static const UINT8 CSW_HEADER[] = { "Compressed Square Wave" };
33
34static UINT32 get_leuint32(const void *ptr)
35{
36   UINT32 value;
37   memcpy(&value, ptr, sizeof(value));
38   return LITTLE_ENDIANIZE_INT32(value);
39}
40
41static int mycaslen;
42
43static int csw_cas_to_wav_size( const UINT8 *casdata, int caslen ) {
44UINT32 SampleRate;
45UINT32 NumberOfPulses;
46UINT8  MajorRevision;
47UINT8  MinorRevision;
48UINT8  CompressionType;
49UINT8  Flags;
50UINT8  HeaderExtensionLength;
51UINT8 *gz_ptr = NULL;
52
53int         total_size;
54z_stream   d_stream;
55int       err;
56UINT8      *in_ptr;
57int       bsize=0;
58
59   if ( memcmp( casdata, CSW_HEADER, sizeof(CSW_HEADER) ) ) {
60      logerror( "csw_cas_to_wav_size: cassette image has incompatible header\n" );
61      goto cleanup;
62   }
63
64   if (casdata[0x16]!=0x1a) {
65      logerror( "csw_cas_to_wav_size: Terminator Code Not Found\n" );
66      goto cleanup;
67   }
68
69   MajorRevision=casdata[0x17];
70   MinorRevision=casdata[0x18];
71
72   logerror("Version %d : %d\n",MajorRevision,MinorRevision);
73
74   if (casdata[0x17]!=2){
75      logerror( "csw_cas_to_wav_size: Unsuported Major Version\n" );
76      goto cleanup;
77   }
78
79   SampleRate=get_leuint32(casdata+0x19);
80   logerror("Sample rate %d\n",SampleRate);
81
82   NumberOfPulses=get_leuint32(casdata+0x1d);
83   logerror("Number Of Pulses %d\n",NumberOfPulses);
84
85
86   CompressionType=casdata[0x21];
87   Flags=casdata[0x22];
88   HeaderExtensionLength=casdata[0x23];
89
90   logerror("CompressionType %d   Flast %d   HeaderExtensionLength %d\n",CompressionType,Flags,HeaderExtensionLength);
91
92   mycaslen=caslen;
93   //from here on down for now I am assuming it is compressed csw file.
94   in_ptr = (UINT8*) casdata+0x34+HeaderExtensionLength;
95
96   gz_ptr = (UINT8*)malloc( 8 );
97
98
99   d_stream.next_in = (unsigned char *)in_ptr;
100   d_stream.avail_in = caslen - ( in_ptr - casdata );
101   d_stream.total_in=0;
102
103   d_stream.next_out = gz_ptr;
104   d_stream.avail_out = 1;
105   d_stream.total_out=0;
106
107   d_stream.zalloc = 0;
108   d_stream.zfree = 0;
109   d_stream.opaque = 0;
110   d_stream.data_type=0;
111
112   err = inflateInit( &d_stream );
113   if ( err != Z_OK ) {
114      logerror( "inflateInit2 error: %d\n", err );
115      goto cleanup;
116   }
117
118
119   total_size=1;
120   do
121   {
122      d_stream.next_out = gz_ptr;
123      d_stream.avail_out=1;
124      err=inflate( &d_stream, Z_SYNC_FLUSH );
125      if (err==Z_OK) {
126         bsize=gz_ptr[0];
127         if (bsize==0) {
128            d_stream.avail_out=4;
129            d_stream.next_out = gz_ptr;
130            err=inflate( &d_stream, Z_SYNC_FLUSH );
131            bsize=get_leuint32(gz_ptr);
132         }
133         total_size=total_size+bsize;
134      }
135   }
136   while (err==Z_OK);
137
138   if ( err != Z_STREAM_END ) {
139      logerror( "inflate error: %d\n", err );
140      goto cleanup;
141   }
142
143   err = inflateEnd( &d_stream );
144   if ( err != Z_OK ) {
145      logerror( "inflateEnd error: %d\n", err );
146      goto cleanup;
147   }
148
149
150
151
152
153
154   if ( gz_ptr ) {
155      free( gz_ptr );
156      gz_ptr = NULL;
157   }
158
159
160
161   return total_size;
162
163cleanup:
164   if ( gz_ptr ) {
165      free( gz_ptr );
166      gz_ptr = NULL;
167   }
168   return -1;
169
170}
171
172static int csw_cas_fill_wave( INT16 *buffer, int length, UINT8 *bytes ) {
173UINT32 SampleRate;
174UINT32 NumberOfPulses;
175UINT8  CompressionType;
176UINT8  Flags;
177UINT8  HeaderExtensionLength;
178INT8   Bit;
179
180UINT8 *gz_ptr = NULL;
181int         total_size;
182z_stream   d_stream;
183int       err;
184UINT8      *in_ptr;
185int       bsize=0;
186int      i;
187
188
189   logerror("Length %d\n",length);
190
191   SampleRate=get_leuint32(bytes+0x19);
192   logerror("Sample rate %d\n",SampleRate);
193
194   NumberOfPulses=get_leuint32(bytes+0x1d);
195   logerror("Number Of Pulses %d\n",NumberOfPulses);
196
197   CompressionType=bytes[0x21];
198   Flags=bytes[0x22];
199   HeaderExtensionLength=bytes[0x23];
200
201   if ((Flags&0)==0)
202   {
203      Bit=-100;
204   } else {
205      Bit=100;
206   }
207
208   logerror("CompressionType %d   Flast %d   HeaderExtensionLength %d\n",CompressionType,Flags,HeaderExtensionLength);
209
210
211   //from here on down for now I am assuming it is compressed csw file.
212   in_ptr = (UINT8*) bytes+0x34+HeaderExtensionLength;
213
214   gz_ptr = (UINT8*)malloc( 8 );
215
216
217   d_stream.next_in = (unsigned char *)in_ptr;
218   d_stream.avail_in = mycaslen - ( in_ptr - bytes );
219   d_stream.total_in=0;
220
221   d_stream.next_out = gz_ptr;
222   d_stream.avail_out = 1;
223   d_stream.total_out=0;
224
225   d_stream.zalloc = 0;
226   d_stream.zfree = 0;
227   d_stream.opaque = 0;
228   d_stream.data_type=0;
229
230   err = inflateInit( &d_stream );
231   if ( err != Z_OK ) {
232      logerror( "inflateInit2 error: %d\n", err );
233      goto cleanup;
234   }
235
236   total_size=0;
237
238   do
239   {
240      d_stream.next_out = gz_ptr;
241      d_stream.avail_out=1;
242      err=inflate( &d_stream, Z_SYNC_FLUSH );
243      if (err==Z_OK) {
244         bsize=gz_ptr[0];
245         if (bsize==0) {
246            d_stream.avail_out=4;
247            d_stream.next_out = gz_ptr;
248            err=inflate( &d_stream, Z_SYNC_FLUSH );
249            bsize=get_leuint32(gz_ptr);
250         }
251         for (i=0;i<bsize;i++)
252         {
253            buffer[total_size++]=Bit;
254         }
255         Bit=-Bit;
256      }
257   }
258   while (err==Z_OK);
259
260   if ( err != Z_STREAM_END ) {
261      logerror( "inflate error: %d\n", err );
262      goto cleanup;
263   }
264
265   err = inflateEnd( &d_stream );
266   if ( err != Z_OK ) {
267      logerror( "inflateEnd error: %d\n", err );
268      goto cleanup;
269   }
270
271
272
273
274
275
276   if ( gz_ptr ) {
277      free( gz_ptr );
278      gz_ptr = NULL;
279   }
280
281
282
283
284   return length;
285
286
287
288   cleanup:
289      if ( gz_ptr ) {
290         free( gz_ptr );
291         gz_ptr = NULL;
292      }
293   return -1;
294}
295
296
297static const struct CassetteLegacyWaveFiller csw_legacy_fill_wave = {
298   csw_cas_fill_wave,      /* fill_wave */
299   -1,                  /* chunk_size */
300   0,                  /* chunk_samples */
301   csw_cas_to_wav_size,   /* chunk_sample_calc */
302   CSW_WAV_FREQUENCY,      /* sample_frequency */
303   0,                  /* header_samples */
304   0                  /* trailer_samples */
305};
306
307static casserr_t csw_cassette_identify( cassette_image *cassette, struct CassetteOptions *opts ) {
308   return cassette_legacy_identify( cassette, opts, &csw_legacy_fill_wave );
309}
310
311static casserr_t csw_cassette_load( cassette_image *cassette ) {
312   return cassette_legacy_construct( cassette, &csw_legacy_fill_wave );
313}
314
315static const struct CassetteFormat csw_cassette_format = {
316   "csw",
317   csw_cassette_identify,
318   csw_cassette_load,
319   NULL
320};
321
322CASSETTE_FORMATLIST_START(csw_cassette_formats)
323   CASSETTE_FORMAT(csw_cassette_format)
324CASSETTE_FORMATLIST_END
325
326CASSETTE_FORMATLIST_START(bbc_cassette_formats)
327   CASSETTE_FORMAT(csw_cassette_format)
328   CASSETTE_FORMAT(uef_cassette_format)
329CASSETTE_FORMATLIST_END
src/lib/formats/ioprocs.h
r0r11458
1/*********************************************************************
2
3    ioprocs.h
4
5    File IO abstraction layer
6
7*********************************************************************/
8
9#ifndef IOPROCS_H
10#define IOPROCS_H
11
12#include <stdlib.h>
13
14
15
16/***************************************************************************
17
18    Type definitions
19
20***************************************************************************/
21
22struct io_procs
23{
24   void (*closeproc)(void *file);
25   int (*seekproc)(void *file, INT64 offset, int whence);
26   size_t (*readproc)(void *file, void *buffer, size_t length);
27   size_t (*writeproc)(void *file, const void *buffer, size_t length);
28   UINT64 (*filesizeproc)(void *file);
29};
30
31
32
33struct io_generic
34{
35   const struct io_procs *procs;
36   void *file;
37   UINT8 filler;
38};
39
40
41/***************************************************************************
42
43    Globals
44
45***************************************************************************/
46
47extern const struct io_procs stdio_ioprocs;
48extern const struct io_procs stdio_ioprocs_noclose;
49
50
51
52/***************************************************************************
53
54    Prototypes
55
56***************************************************************************/
57
58
59
60void io_generic_close(struct io_generic *generic);
61void io_generic_read(struct io_generic *generic, void *buffer, UINT64 offset, size_t length);
62void io_generic_write(struct io_generic *generic, const void *buffer, UINT64 offset, size_t length);
63void io_generic_write_filler(struct io_generic *generic, UINT8 filler, UINT64 offset, size_t length);
64UINT64 io_generic_size(struct io_generic *generic);
65
66
67
68#endif /* IOPROCS_H */
src/lib/formats/coco_dsk.c
r0r11458
1/*********************************************************************
2
3    formats/coco_dsk.c
4
5    Tandy Color Computer / Dragon disk images
6
7*********************************************************************/
8
9#include <string.h>
10#include <assert.h>
11#include <time.h>
12
13#include "osdepend.h"
14#include "formats/coco_dsk.h"
15#include "formats/basicdsk.h"
16#include "imageutl.h"
17
18/* -----------------------------------------------------------------------
19 * JVC (Jeff Vavasour CoCo) format
20 *
21 * Used by Jeff Vavasour's CoCo Emulators
22 *
23 *  Documentation taken from Tim Linder's web site:
24 *      http://home.netcom.com/~tlindner/JVC.html
25 *
26 *  A. Header length
27 *      The header length is determined by the file length modulo 256:
28 *          headerSize = fileLength % 256;
29 *      This means that the header is variable length and the minimum size
30 *      is zero bytes, and the maximum size of 255 bytes.
31 *
32 * B. Header
33 *      Here is a description of the header bytes:
34 *          Byte Offset     Description            Default
35 *          -----------     -----------------      -------
36 *                    0     Sectors per track      18
37 *                    1     Side count              1
38 *                    2     Sector size code        1
39 *                    3     First sector ID         1
40 *                    4     Sector attribute flag   0
41 *
42 *  If the sector attribute flag is zero then the track count is determined
43 *  by the formula:
44 *
45 *      (fileLength - headerSize) / (sectorsPerTrack * (128 <<
46 *          sectorSizeCode)) / sideCount
47 *
48 *  If the sector attribute flag is non zero then the track count is
49 *  determined by the more complex formula:
50 *
51 *      (fileLength - headerSize) / (sectorsPerTrack * ((128 <<
52 *          sectorSizeCode) + 1) ) / sideCount
53 *
54 *  If the length of the header is to short to contain the geometry desired,
55 *  then the default values are assumed. If the header length is zero the all
56 *  of the geometry is assumed. When creating disk images it is desirable to
57 *  make the header length as short as possible. The header should only be
58 *  used to deviate from the default values.
59 *
60 *  The sector data begins immediately after the header. If the header length
61 *  is zero then the sector data is at the beginning file.
62 *
63 *  C. Sectors per track
64 *      This is the number of sectors per track (ones based). A value of 18
65 *      means there are 18 sectors per track
66 *
67 *  D. Side Count
68 *      This is the number of sides in the disk image. Values of 1 or 2 are
69 *      acceptable. If there are two sides then the tracks are interleaved.
70 *      The first track in the image file is track zero side 1, the second
71 *      track in the image file is track zero side 2.
72 *
73 *  E. Sector size
74 *      The is the same value that is stored in the wd179x ID field to
75 *      determine sector size:
76 *
77 *          0x00         128 bytes
78 *          0x01         256 bytes
79 *          0x02         512 bytes
80 *          0x03        1024 bytes
81 *
82 *  Other values are undefined. Every sector in the disk image must be the
83 *  same size.
84 *
85 *  F. First sector ID
86 *      This determines the first sector ID for each track. Each successive
87 *      sector adds one to the previous ID. If the first sector ID is 1, then
88 *      the second sector has an ID of 2, and the third has an ID of 3.
89 *
90 *  G. Sector Attribute Flag
91 *      If this byte is non zero, then each sector contains an additional
92 *      byte prepended to the sector data. If the attribute flag is zero then
93 *      there are no extra bytes in front of the sector data.
94 *
95 *  H. Sector attribute byte
96 *      This byte is put at the begining of every sector if the header flag
97 *      is turned on. The information this byte contains is the same as the
98 *      status register (of the wd179x) would contain when a 'Read Sector'
99 *      command was issued. The bit fields are defined as:
100 *
101 *      Bit position:
102 *      ---------------
103 *      7 6 5 4 3 2 1 0
104 *      | | | | | | | |
105 *      | | | | | | | +--- Not used. Set to zero.
106 *      | | | | | | +----- Not used. Set to zero.
107 *      | | | | | +------- Not used. Set to zero.
108 *      | | | | +--------- Set on CRC error.
109 *      | | | +----------- Set if sector not found.
110 *      | | +------------- Record type: 1 - Deleted Data Mark, 0 - Data Mark.
111 *      | +--------------- Not Used. Set to zero.
112 *      +----------------- Not Used. Set to zero.
113 *
114 * ----------------------------------------------------------------------- */
115
116static int coco_jvc_decode_header(floppy_image *floppy, UINT64 size,
117   struct basicdsk_geometry *geometry)
118{
119   UINT8 header[256];
120   UINT8 sector_attribute_flag;
121   UINT16 physical_bytes_per_sector;
122   UINT32 header_size, file_size;
123   struct basicdsk_geometry dummy_geometry;
124
125   if (geometry)
126      memset(geometry, 0, sizeof(*geometry));
127   else
128      geometry = &dummy_geometry;
129
130   if (size > 0xFFFFFFFF)
131      return -1;
132   file_size = (UINT32) size;
133
134   /* read the header */
135   header_size = (UINT32) file_size % 0x100;
136   floppy_image_read(floppy, header, 0, header_size);
137   geometry->offset = header_size;
138
139   /* byte offset 0 - sectors per track */
140   geometry->sectors = (header_size > 0) ? header[0] : 18;
141   if (geometry->sectors <= 0)
142      return -1;
143
144   /* byte offset 1 - side count */
145   geometry->heads = (header_size > 1) ? header[1] : 1;
146   if (geometry->heads <= 0)
147      return -1;
148
149   /* byte offset 2 - sector size code */
150   geometry->sector_length = 128 << ((header_size > 2) ? header[2] : 1);
151   if (geometry->sector_length <= 0)
152      return -1;
153
154   /* byte offset 3 - first sector ID */
155   geometry->first_sector_id = (header_size > 3) ? header[3] : 1;
156
157   /* byte offset 4 - sector attribute flag */
158   sector_attribute_flag = (header_size > 4) ? header[4] : 0;
159   if (sector_attribute_flag != 0)
160      return -1;   /* we do not support sector attribute flags */
161
162   physical_bytes_per_sector = geometry->sector_length;
163   if (sector_attribute_flag)
164      physical_bytes_per_sector++;
165
166   geometry->tracks = (file_size - header_size) / geometry->sectors / geometry->heads / physical_bytes_per_sector;
167
168   /* do we have an oddball size?  reject this file if not */
169   if ((file_size - header_size) % physical_bytes_per_sector)
170      return -1;
171
172   /* minimum of 35 tracks; support degenerate JVC files */
173   if (geometry->tracks < 35)
174      geometry->tracks = 35;
175
176   return 0;
177}
178
179
180
181static FLOPPY_IDENTIFY(coco_jvc_identify)
182{
183   UINT64 size;
184   size = floppy_image_size(floppy);
185   *vote = coco_jvc_decode_header(floppy, size, NULL) ? 0 : 100;
186   return FLOPPY_ERROR_SUCCESS;
187}
188
189
190
191static FLOPPY_CONSTRUCT(coco_jvc_construct)
192{
193   struct basicdsk_geometry geometry;
194   UINT8 header[5];
195   size_t header_size;
196
197   if (params)
198   {
199      /* create */
200      memset(&geometry, 0, sizeof(geometry));
201      geometry.heads            = option_resolution_lookup_int(params, PARAM_HEADS);
202      geometry.tracks            = option_resolution_lookup_int(params, PARAM_TRACKS);
203      geometry.sectors         = option_resolution_lookup_int(params, PARAM_SECTORS);
204      geometry.first_sector_id   = option_resolution_lookup_int(params, PARAM_FIRST_SECTOR_ID);
205      geometry.sector_length      = option_resolution_lookup_int(params, PARAM_SECTOR_LENGTH);
206
207      header[0] = (UINT8) geometry.sectors;
208      header[1] = (UINT8) geometry.heads;
209      header[2] = (UINT8) compute_log2(geometry.sector_length) - 7;
210      header[3] = (UINT8) geometry.first_sector_id;
211      header[4] = 0;
212
213      /* now that we have the header computed, figure out the header size */
214      header_size = 0;
215      if (header[0] != 18)
216         header_size = 1;
217      if (header[1] != 1)
218         header_size = 2;
219      if (header[2] != 1)
220         header_size = 3;
221      if (header[3] != 1)
222         header_size = 4;
223      if (header[4] != 0)
224         header_size = 5;
225
226      geometry.offset = header_size;
227
228      floppy_image_write(floppy, header, 0, header_size);
229   }
230   else
231   {
232      /* load */
233      if (coco_jvc_decode_header(floppy, floppy_image_size(floppy), &geometry))
234         return FLOPPY_ERROR_INVALIDIMAGE;
235   }
236   return basicdsk_construct(floppy, &geometry);
237}
238
239
240/* -----------------------------------------------------------------------
241 * OS-9 file format
242 *
243 * This file format is largely a hack because there are a large amount of
244 * disk images that do not have geometry image separate from the disk image
245 * itself.  So we support OS-9 images with are simply basic disks whose
246 * geometry is determined by the disk image.
247 *
248 * OS-9 images identified by an LSN; which are simply blocks of 256 bytes
249 *
250 * LSN0
251 * Byte    size    use
252 * $00     3       sectors on disk
253 * $03     1       track size in sectors
254 * $04     2       bytes in allocation bit map; typically 1bit/sector so for
255 *                 35 tracks of 18 sectors each that's $4E (single sided disk)
256 *                 40 tracks per side, 18 sectors each = $B4
257 * $06     2       sectors per bit in allocation map; normally 1
258 * $08     3       LSN of root directory; normally 2 but depends on size of
259 *                 $04 value
260 * $0B     2       owner's user number; normally 0
261 * $0D     1       disk attributes; normally $FF
262 * $0E     2       psuedo random number for identification
263 * $10     1       disk format; typical is 3
264 *                 %00000001 0=single side 1=double side
265 *                 %00000010 0=single density (non Coco) 1=double density
266 *                 %00000100 0=48tracks/inch 1=96tracks/inch
267 * $11     2       sectors per track; normal is $12 skip several not needed
268 *                 for Format
269 * $1A     5       date of creation Y:M:D:H:M
270 * $1F     32      ASCII name of disk, last letter has $80 added to it,
271 *                 the full 32 bytes do not need to be used.
272 *
273 * Allocation bit map, fill with zeros and set bits from low to high as
274 * sectors are used. So, for a fresh disk sectors LSN0,LSN1,LSN2, and LSN3
275 * will be in use so the first byte will be $FF $C0 and all others
276 * in the map are $00
277 *
278 * Root directory LSN2
279 * Byte    size     use
280 * $00      1       attributes will be $BF
281 * $01      2       owners ID will be $0000
282 * $03      5       date last modified will be creation date  Y:M:D:H:M
283 * $08      1       link count; set to $02
284 * $09      4       file size in bytes, set to $40
285 * $0D      3       date created Y:M:D
286 * $10      3       block LSN set to current sector number+1 ie $03 in this case
287 * $13      2       size in sectors of directory block, set to $07
288 * All other bytes in sector set to $00
289 * LSN3 first sector of directory with names
290 * Fill sector with all $00 and then set listed bytes
291 * $00      2       value $2EAE   which is .. with last byte+$80
292 * $1F      1       value $02     LSN for start of this directory as there is
293 *                  none higher in tree
294 * $20      1       value $AE     which is . with $80 added
295 * $3F      1       value $02     LSN for start of this directory
296 * ----------------------------------------------------------------------- */
297
298static floperr_t coco_os9_readheader(floppy_image *floppy, struct basicdsk_geometry *geometry)
299{
300   UINT8 header[0x20];
301   int total_sectors;
302
303   floppy_image_read(floppy, header, 0, sizeof(header));
304
305   total_sectors = (header[0x00] << 16) | (header[0x01] << 8) | header[0x02];
306
307   memset(geometry, 0, sizeof(*geometry));
308   geometry->first_sector_id = 1;
309   geometry->sector_length = 256;
310   geometry->sectors = (header[0x11] << 8) + header[0x12];
311   geometry->heads = (header[0x10] & 0x01) ? 2 : 1;
312
313   if (!geometry->sectors)
314      return FLOPPY_ERROR_INVALIDIMAGE;
315
316   geometry->tracks = total_sectors / geometry->sectors / geometry->heads;
317
318   if (total_sectors != geometry->tracks * geometry->sectors * geometry->heads)
319      return FLOPPY_ERROR_INVALIDIMAGE;
320
321   return FLOPPY_ERROR_SUCCESS;
322}
323
324
325
326static floperr_t coco_os9_post_format(floppy_image *floppy, option_resolution *params)
327{
328   UINT8 header[0x0400];
329   floperr_t err;
330   time_t t;
331   struct tm *ltime;
332   int heads, tracks, sectors, total_sectors;
333
334   heads   = option_resolution_lookup_int(params, PARAM_HEADS);
335   tracks   = option_resolution_lookup_int(params, PARAM_TRACKS);
336   sectors   = option_resolution_lookup_int(params, PARAM_SECTORS);
337   total_sectors = heads * tracks * sectors;
338
339   /* write the initial header */
340   time(&t);
341   ltime = localtime(&t);
342
343   memset(&header, 0, sizeof(header));
344   header[0x0000] = (UINT8) (total_sectors >> 16);
345   header[0x0001] = (UINT8) (total_sectors >>  8);
346   header[0x0002] = (UINT8) (total_sectors >>  0);
347   header[0x0003] = (UINT8) sectors;
348   header[0x0004] = (UINT8) (((total_sectors + 7) / 8) >> 8);
349   header[0x0005] = (UINT8) (((total_sectors + 7) / 8) >> 0);
350   header[0x0006] = 0x00;
351   header[0x0007] = 0x01;
352   header[0x0008] = 0x00;
353   header[0x0009] = 0x00;
354   header[0x000a] = 0x02;
355   header[0x000b] = 0x00;
356   header[0x000c] = 0x00;
357   header[0x000d] = 0xff;
358   header[0x000e] = floppy_random_byte(floppy);
359   header[0x000f] = floppy_random_byte(floppy);
360   header[0x0010] = (heads == 2) ? 3 : 2;
361   header[0x0011] = (UINT8) (sectors >> 8);
362   header[0x0012] = (UINT8) (sectors >> 0);
363   header[0x001A] = (UINT8) ltime->tm_year;
364   header[0x001B] = (UINT8) ltime->tm_mon + 1;
365   header[0x001C] = (UINT8) ltime->tm_mday;
366   header[0x001D] = (UINT8) ltime->tm_hour;
367   header[0x001E] = (UINT8) ltime->tm_min;
368   header[0x001F] = 0xA0;
369   header[0x0100] = 0xFF;
370   header[0x0101] = 0xC0;
371   header[0x0200] = 0xBF;
372   header[0x0201] = 0x00;
373   header[0x0202] = 0x00;
374   header[0x0203] = (UINT8) ltime->tm_year;
375   header[0x0204] = (UINT8) ltime->tm_mon + 1;
376   header[0x0205] = (UINT8) ltime->tm_mday;
377   header[0x0206] = (UINT8) ltime->tm_hour;
378   header[0x0207] = (UINT8) ltime->tm_min;
379   header[0x0208] = 0x02;
380   header[0x0209] = 0x00;
381   header[0x020A] = 0x00;
382   header[0x020B] = 0x00;
383   header[0x020C] = 0x40;
384   header[0x020D] = (UINT8) (ltime->tm_year % 100);
385   header[0x020E] = (UINT8) ltime->tm_mon;
386   header[0x020F] = (UINT8) ltime->tm_mday;
387   header[0x0210] = 0x00;
388   header[0x0211] = 0x00;
389   header[0x0212] = 0x03;
390   header[0x0213] = 0x00;
391   header[0x0214] = 0x07;
392   header[0x0300] = 0x2E;
393   header[0x0301] = 0xAE;
394   header[0x031F] = 0x02;
395   header[0x0320] = 0xAE;
396   header[0x033F] = 0x02;
397
398   if (total_sectors % 8)
399      header[0x0100 + (total_sectors / 8)] = 0xFF >> (total_sectors % 8);
400
401   err = floppy_write_sector(floppy, 0, 0, 1, 0, &header[0x0000], 256, 0);
402   if (err)
403      return err;
404
405   err = floppy_write_sector(floppy, 0, 0, 2, 0, &header[0x0100], 256, 0);
406   if (err)
407      return err;
408
409   err = floppy_write_sector(floppy, 0, 0, 3, 0, &header[0x0200], 256, 0);
410   if (err)
411      return err;
412
413   err = floppy_write_sector(floppy, 0, 0, 4, 0, &header[0x0300], 256, 0);
414   if (err)
415      return err;
416
417   return FLOPPY_ERROR_SUCCESS;
418}
419
420
421
422static FLOPPY_IDENTIFY(coco_os9_identify)
423{
424   struct basicdsk_geometry geometry;
425   *vote = coco_os9_readheader(floppy, &geometry) ? 0 : 100;
426   return FLOPPY_ERROR_SUCCESS;
427}
428
429
430
431static FLOPPY_CONSTRUCT(coco_os9_construct)
432{
433   floperr_t err;
434   struct basicdsk_geometry geometry;
435
436   if (params)
437   {
438      /* create */
439      memset(&geometry, 0, sizeof(geometry));
440      geometry.heads            = option_resolution_lookup_int(params, PARAM_HEADS);
441      geometry.tracks            = option_resolution_lookup_int(params, PARAM_TRACKS);
442      geometry.sectors         = option_resolution_lookup_int(params, PARAM_SECTORS);
443      geometry.first_sector_id   = option_resolution_lookup_int(params, PARAM_FIRST_SECTOR_ID);
444      geometry.sector_length      = option_resolution_lookup_int(params, PARAM_SECTOR_LENGTH);
445   }
446   else
447   {
448      /* open */
449      err = coco_os9_readheader(floppy, &geometry);
450      if (err)
451         return err;
452   }
453
454   /* actually construct the image */
455   err = basicdsk_construct(floppy, &geometry);
456   floppy_callbacks(floppy)->post_format = coco_os9_post_format;
457   return err;
458}
459
460
461
462/* -----------------------------------------------------------------------
463 * VDK file format
464 *
465 * Used by Paul Burgin's PC-Dragon emulator
466 *
467 *  Offset  Bytes   Field           Description
468 *  ------  -----   ------------    -----------
469 *       0      1   magic1          Signature byte 1 ('d')
470 *       1      1   magic2          Signature byte 2 ('k')
471 *       2      2   header_len      Total header length (little endian)
472 *       4      1   ver_actual      Version of the VDK format (0x10)
473 *       5      1   ver_compat      Backwards compatibility version (0x10)
474 *       6      1   source_id       Identify of the file source
475 *       7      1   source_ver      Version of the file source
476 *       8      1   tracks          Number of tracks
477 *       9      1   sides           Number of sides (1-2)
478 *      10      1   flags           Various flags
479 *                                      bit 0: Write protect
480 *                                      bit 1: A Lock
481 *                                      bit 2: F Lock
482 *                                      bit 3: Disk set
483 *      11      1   compression     Compression flags (bits 0-2) and name length
484 * ----------------------------------------------------------------------- */
485
486
487
488static int coco_vdk_decode_header(floppy_image *floppy, struct basicdsk_geometry *geometry)
489{
490   UINT8 header[12];
491   int heads, tracks, sectors, sector_length, offset;
492   UINT64 size;
493
494   size = floppy_image_size(floppy);
495
496   floppy_image_read(floppy, header, 0, sizeof(header));
497
498   if (header[0] != 'd')
499      return -1;
500   if (header[1] != 'k')
501      return -1;
502   if (header[5] != 0x10)
503      return -1;
504   if (header[11] & 0x07)
505      return -1;
506
507   heads = header[9];
508   tracks = header[8];
509   sectors = 18;
510   sector_length = 0x100;
511
512   offset = ((int) header[3]) * 0x100 + header[2];
513
514   if (size != (heads * tracks * sectors * sector_length + offset))
515      return -1;
516
517   if (geometry)
518   {
519      memset(geometry, 0, sizeof(*geometry));
520      geometry->heads = heads;
521      geometry->tracks = tracks;
522      geometry->sectors = sectors;
523      geometry->first_sector_id = 1;
524      geometry->sector_length = sector_length;
525      geometry->offset = offset;
526   }
527   return 0;
528}
529
530
531
532static FLOPPY_IDENTIFY(coco_vdk_identify)
533{
534   *vote = coco_vdk_decode_header(floppy, NULL) ? 0 : 100;
535   return FLOPPY_ERROR_SUCCESS;
536}
537
538
539
540static FLOPPY_CONSTRUCT(coco_vdk_construct)
541{
542   struct basicdsk_geometry geometry;
543   UINT8 header[12];
544
545   if (params)
546   {
547      /* create */
548      memset(&geometry, 0, sizeof(geometry));
549      geometry.heads = option_resolution_lookup_int(params, PARAM_HEADS);
550      geometry.tracks = option_resolution_lookup_int(params, PARAM_TRACKS);
551      geometry.sectors = 18;
552      geometry.first_sector_id = 1;
553      geometry.sector_length = 256;
554      geometry.offset = sizeof(header);
555
556      memset(&header, 0, sizeof(header));
557      header[0] = 'd';
558      header[1] = 'k';
559      header[2] = (UINT8) (sizeof(header) >> 0);
560      header[3] = (UINT8) (sizeof(header) >> 8);
561      header[4] = 0x10;
562      header[5] = 0x10;
563      header[8] = (UINT8) geometry.tracks;
564      header[9] = (UINT8) geometry.heads;
565
566      floppy_image_write(floppy, header, 0, sizeof(header));
567   }
568   else
569   {
570      /* load */
571      if (coco_vdk_decode_header(floppy, &geometry))
572         return FLOPPY_ERROR_INVALIDIMAGE;
573   }
574   return basicdsk_construct(floppy, &geometry);
575}
576
577
578
579/* -----------------------------------------------------------------------
580 * DMK file format
581 *
582 * David M. Keil's disk image format is aptly called an 'on disk' image
583 * format. This means that whatever written to the disk is enocded into
584 * the image file. IDAMS, sector headers, traling CRCs, and intra sector
585 * spacing.                                                         *
586 *
587 *  HEADER DESCRIPTION:
588 *
589 *  Offset  Bytes   Field           Description
590 *  ------  -----   ------------    -----------
591 *       0      1   write_prot      0xff = Writed Protected, 0x00 = R/W
592 *       1      1   tracks          Number of tracks
593 *       2      2   track_length    Bytes per track (little endian)
594 *       4      1   disk_options    Miscellaneous flags
595 *                                      bit 0-3:  Unused
596 *                                      bit   4:  1=single sided 0=dbl
597 *                                      bit   5:  Unused
598 *                                      bit   6:  Single density?
599 *                                      bit   6:  Ignore density flags?
600 *       5      7   reserved        Reserved for future use
601 *      12      4   real_disk_code  If this is 0x12345678 (little endian)
602 *                                  then access a real disk drive
603 *                                  (unsupported)
604 *
605 * Each track begins with a track TOC, consisting of 64 little endian 16-bit
606 * integers.  Each integer has the following format:
607 *      bit 0-13:   Offset from begining of track to 'FE' byte of IDAM
608 *                  Note these are always sorted from first to last. All empty
609 *                  entries are 0x00
610 *      bit 14:     Undefined (reserved)
611 *      bit 15:     Sector double density (0=SD 1=DD)
612 * ----------------------------------------------------------------------- */
613
614struct dmk_tag
615{
616   int heads;
617   int tracks;
618   UINT32 track_size;
619};
620
621#define DMK_HEADER_LEN         16
622#define DMK_TOC_LEN            64
623#define DMK_IDAM_LENGTH         7
624#define DMK_DATA_GAP         80
625#define DMK_LEAD_IN            32
626#define DMK_EXTRA_TRACK_LENGTH   156
627
628#define dmk_idam_type(x)         (x)[0]
629#define dmk_idam_track(x)         (x)[1]
630#define dmk_idam_side(x)         (x)[2]
631#define dmk_idam_sector(x)         (x)[3]
632#define dmk_idam_sectorlength(x)   (x)[4]
633#define dmk_idam_crc(x)            (((x)[5] << 8) + (x)[6])
634#define dmk_idam_set_crc(x, crc)   (x)[5] = ((crc) >> 8); (x)[6] = ((crc) >> 0);
635
636
637static struct dmk_tag *get_dmk_tag(floppy_image *floppy)
638{
639   return (dmk_tag *)floppy_tag(floppy);
640}
641
642
643static floperr_t coco_dmk_get_offset(floppy_image *floppy, int head, int track, UINT64 *offset)
644{
645   struct dmk_tag *tag = get_dmk_tag(floppy);
646
647   if ((head < 0) || (head >= tag->heads) || (track < 0) || (track >= tag->tracks))
648      return FLOPPY_ERROR_SEEKERROR;
649
650   *offset = track;
651   *offset *= tag->heads;
652   *offset += head;
653   *offset *= tag->track_size;
654   *offset += DMK_HEADER_LEN;
655   return FLOPPY_ERROR_SUCCESS;
656}
657
658
659
660static UINT32 coco_dmk_min_track_size(int sectors, int sector_length)
661{
662   int sector_physical_length;
663   sector_physical_length = 8 + 3 + DMK_IDAM_LENGTH + 22 + 12 + 3 + 1 + sector_length + 2 + 24;
664   return DMK_TOC_LEN * 2 + DMK_LEAD_IN + (sectors * sector_physical_length);
665}
666
667
668
669static floperr_t coco_dmk_read_track(floppy_image *floppy, int head, int track, UINT64 offset, void *buffer, size_t buflen)
670{
671   floperr_t err;
672   UINT64 track_offset;
673
674   err = coco_dmk_get_offset(floppy, head, track, &track_offset);
675   if (err)
676      return err;
677
678   floppy_image_read(floppy, buffer, offset + track_offset, buflen);
679   return FLOPPY_ERROR_SUCCESS;
680}
681
682
683
684static floperr_t coco_dmk_write_track(floppy_image *floppy, int head, int track, UINT64 offset, const void *buffer, size_t buflen)
685{
686   floperr_t err;
687   UINT64 track_offset;
688
689   err = coco_dmk_get_offset(floppy, head, track, &track_offset);
690   if (err)
691      return err;
692
693   floppy_image_write(floppy, buffer, offset + track_offset, buflen);
694   return FLOPPY_ERROR_SUCCESS;
695}
696
697
698
699static floperr_t coco_dmk_get_track_data_offset(floppy_image *floppy, int head, int track, UINT64 *offset)
700{
701   *offset = DMK_TOC_LEN + 1;
702   return FLOPPY_ERROR_SUCCESS;
703}
704
705
706
707static floperr_t coco_dmk_format_track(floppy_image *floppy, int head, int track, option_resolution *params)
708{
709   int sectors;
710   int sector_length;
711   int interleave;
712   int first_sector_id;
713   floperr_t err;
714   int physical_sector;
715   int logical_sector;
716   int track_position;
717   UINT16 idam_offset;
718   UINT16 crc;
719   UINT8 *track_data;
720   void *track_data_v;
721   UINT32 max_track_size;
722   int *sector_map = NULL;
723
724   sectors         = option_resolution_lookup_int(params, PARAM_SECTORS);
725   sector_length   = option_resolution_lookup_int(params, PARAM_SECTOR_LENGTH);
726   interleave      = option_resolution_lookup_int(params, PARAM_INTERLEAVE);
727   first_sector_id   = option_resolution_lookup_int(params, PARAM_FIRST_SECTOR_ID);
728
729   max_track_size = get_dmk_tag(floppy)->track_size;
730
731   if (sectors > DMK_TOC_LEN)
732   {
733        err = FLOPPY_ERROR_INTERNAL;
734      goto done;
735   }
736
737   if (max_track_size < coco_dmk_min_track_size(sectors, sector_length))
738   {
739      err = FLOPPY_ERROR_NOSPACE;
740      goto done;
741   }
742
743   err = floppy_load_track(floppy, head, track, TRUE, &track_data_v, NULL);
744   if (err)
745      goto done;
746   track_data = (UINT8 *) track_data_v;
747
748   /* set up sector map */
749   sector_map = (int*)malloc(sectors * sizeof(*sector_map));
750   if (!sector_map)
751   {
752      err = FLOPPY_ERROR_OUTOFMEMORY;
753      goto done;
754   }
755   memset(sector_map, 0xFF, sectors * sizeof(*sector_map));
756
757   physical_sector = 0;
758   for (logical_sector = 0; logical_sector < sectors; logical_sector++)
759   {
760      while(sector_map[physical_sector] >= 0)
761      {
762         physical_sector++;
763         physical_sector %= sectors;
764      }
765
766      sector_map[physical_sector] = logical_sector + first_sector_id;
767      physical_sector += interleave + 1;
768      physical_sector %= sectors;
769   }
770
771   /* set up track table of contents */
772   physical_sector = 0;
773   track_position = DMK_TOC_LEN * 2 + DMK_LEAD_IN;
774   while(physical_sector < DMK_TOC_LEN)
775   {
776      if (physical_sector >= sectors)
777      {
778         /* no more sectors */
779         idam_offset = 0;
780      }
781      else
782      {
783         /* this is a sector */
784         logical_sector = sector_map[physical_sector];
785
786         /* write the sector */
787         memset(&track_data[track_position], 0x00, 8);
788         track_position += 8;
789
790         memset(&track_data[track_position], 0xA1, 3);
791         track_position += 3;
792
793         idam_offset = track_position | 0x8000;
794         dmk_idam_type(            &track_data[track_position]) = 0xFE;
795         dmk_idam_track(            &track_data[track_position]) = track;
796         dmk_idam_side(            &track_data[track_position]) = head;
797         dmk_idam_sector(         &track_data[track_position]) = logical_sector;
798         dmk_idam_sectorlength(      &track_data[track_position]) = compute_log2(sector_length / 128);
799         crc = ccitt_crc16(0xcdb4,   &track_data[track_position], DMK_IDAM_LENGTH - 2);
800         dmk_idam_set_crc(         &track_data[track_position], crc);
801         track_position += DMK_IDAM_LENGTH;
802
803         memset(&track_data[track_position], 0x4E, 22);
804         track_position += 22;
805
806         memset(&track_data[track_position], 0x00, 12);
807         track_position += 12;
808
809         memset(&track_data[track_position], 0xA1, 3);
810         track_position += 3;
811
812         /* write sector body */
813         track_data[track_position] = 0xFB;
814         memset(&track_data[track_position + 1], floppy_get_filler(floppy), sector_length);
815         crc = ccitt_crc16(0xcdb4, &track_data[track_position], sector_length + 1);
816         track_data[track_position + sector_length + 1] = (UINT8) (crc >> 8);
817         track_data[track_position + sector_length + 2] = (UINT8) (crc >> 0);
818         track_position += sector_length + 3;
819
820         /* write sector footer */
821         memset(&track_data[track_position], 0x4E, 24);
822         track_position += 24;
823      }
824
825      /* write the TOC entry */
826      track_data[physical_sector * 2 + 0] = (UINT8) (idam_offset >> 0);
827      track_data[physical_sector * 2 + 1] = (UINT8) (idam_offset >> 8);
828
829      physical_sector++;
830   }
831
832   /* write track lead in */
833   memset(&track_data[physical_sector * 2], 0x4e, DMK_LEAD_IN);
834
835   /* write track footer */
836   assert(max_track_size >= (UINT32)track_position);
837   memset(&track_data[track_position], 0x4e, max_track_size - track_position);
838
839done:
840   if (sector_map)
841      free(sector_map);
842   return FLOPPY_ERROR_SUCCESS;
843}
844
845
846
847static int coco_dmk_get_heads_per_disk(floppy_image *floppy)
848{
849   return get_dmk_tag(floppy)->heads;
850}
851
852
853
854static int coco_dmk_get_tracks_per_disk(floppy_image *floppy)
855{
856   return get_dmk_tag(floppy)->tracks;
857}
858
859
860
861static UINT32 coco_dmk_get_track_size(floppy_image *floppy, int head, int track)
862{
863   return get_dmk_tag(floppy)->track_size;
864}
865
866
867
868static floperr_t coco_dmk_seek_sector_in_track(floppy_image *floppy, int head, int track, int sector, int sector_is_index, int dirtify, UINT8 **sector_data, UINT32 *sector_length)
869{
870   struct dmk_tag *tag = get_dmk_tag(floppy);
871   floperr_t err;
872   UINT32 idam_offset = 0;
873   UINT16 calculated_crc;
874   size_t i;
875   size_t offs;
876   int state;
877   UINT8 *track_data;
878   void *track_data_v;
879   size_t track_length;
880   size_t sec_len;
881
882   err = floppy_load_track(floppy, head, track, dirtify, &track_data_v, &track_length);
883   if (err)
884      return err;
885   track_data = (UINT8 *) track_data_v;
886
887   /* search for matching IDAM */
888   for (i = 0; i < DMK_TOC_LEN; i++)
889   {
890      idam_offset = track_data[i * 2 + 1];
891      idam_offset <<= 8;
892      idam_offset |= track_data[i * 2 + 0];
893      idam_offset &= 0x3FFF;
894
895      if (idam_offset == 0)
896      {
897         /* we've reached the end of the road */
898         i = DMK_TOC_LEN;
899         break;
900      }
901
902      if ((idam_offset + DMK_IDAM_LENGTH) >= tag->track_size)
903         continue;
904
905      calculated_crc = ccitt_crc16(0xCDB4, &track_data[idam_offset], DMK_IDAM_LENGTH - 2);
906
907      if (calculated_crc == dmk_idam_crc(&track_data[idam_offset]))
908      {
909         if (sector_is_index)
910         {
911            /* the sector is indexed; decrement the index and go */
912            if (sector-- == 0)
913               break;
914         }
915         else
916         {
917            /* check IDAM integrity and check for matching sector */
918            if ((sector == dmk_idam_sector(&track_data[idam_offset]))
919/*                  && (track == dmk_idam_track(&track_data[idam_offset]))  */
920/*                  && (head == dmk_idam_side(&track_data[idam_offset]))    */
921               )
922               break;
923         }
924      }
925   }
926
927   if (i >= DMK_TOC_LEN)
928      return FLOPPY_ERROR_SEEKERROR;
929
930   /* we found a matching sector ID */
931   state = 0;
932   offs = idam_offset + DMK_IDAM_LENGTH;
933
934   /* find pattern 0xA1A1FB; this represents the start of a data sector */
935   for (i = 0; i < DMK_DATA_GAP; i++)
936   {
937      /* overflowing the track? */
938      if ((i + offs) >= tag->track_size)
939         return FLOPPY_ERROR_SEEKERROR;
940
941      if (track_data[offs + i] == 0xA1)
942         state++;
943      else if ((track_data[offs + i] == 0xFB) && state)
944         break;
945      else
946         state = 0;
947   }
948   if (i >= DMK_DATA_GAP)
949      return FLOPPY_ERROR_SEEKERROR;
950
951   offs += i + 1;
952   sec_len = 128 << dmk_idam_sectorlength(&track_data[idam_offset]);
953
954   if ((offs + sec_len) > track_length)
955      return FLOPPY_ERROR_INVALIDIMAGE;
956
957   if (sector_data)
958      *sector_data = track_data + offs;
959   if (sector_length)
960      *sector_length = sec_len;
961   return FLOPPY_ERROR_SUCCESS;
962}
963
964
965
966static floperr_t coco_dmk_get_sector_length(floppy_image *floppy, int head, int track, int sector, UINT32 *sector_length)
967{
968   return coco_dmk_seek_sector_in_track(floppy, head, track, sector, FALSE, FALSE, NULL, sector_length);
969}
970
971
972
973static floperr_t coco_dmk_get_indexed_sector_info(floppy_image *floppy, int head, int track, int sector_index, int *cylinder, int *side, int *sector, UINT32 *sector_length, unsigned long *flags)
974{
975   floperr_t err;
976   UINT32 idam_offset;
977   const UINT8 *track_data;
978   void *track_data_v;
979
980   if (sector_index*2 >= DMK_TOC_LEN)
981      return FLOPPY_ERROR_SEEKERROR;
982
983   err = floppy_load_track(floppy, head, track, FALSE, &track_data_v, NULL);
984   if (err)
985      return err;
986   track_data = (UINT8 *) track_data_v;
987
988   idam_offset = track_data[sector_index * 2 + 1];
989   idam_offset <<= 8;
990   idam_offset |= track_data[sector_index * 2 + 0];
991   idam_offset &= 0x3FFF;
992
993   if (idam_offset == 0)
994      return FLOPPY_ERROR_SEEKERROR;
995
996   if (cylinder)
997      *cylinder = dmk_idam_track(&track_data[idam_offset]);
998   if (side)
999      *side = dmk_idam_side(&track_data[idam_offset]);
1000   if (sector)
1001      *sector = dmk_idam_sector(&track_data[idam_offset]);
1002   if (sector_length)
1003      *sector_length = 128 << dmk_idam_sectorlength(&track_data[idam_offset]);
1004   if (flags)
1005      /* TODO: read DAM or DDAM and determine flags */
1006      *flags = 0;
1007   return FLOPPY_ERROR_SUCCESS;
1008}
1009
1010
1011
1012static floperr_t internal_coco_dmk_read_sector(floppy_image *floppy, int head, int track, int sector, int sector_is_index, void *buffer, size_t buflen)
1013{
1014   floperr_t err;
1015   UINT32 sector_length;
1016   UINT16 crc_on_disk;
1017   UINT16 calculated_crc;
1018   UINT8 *sector_data;
1019
1020   err = coco_dmk_seek_sector_in_track(floppy, head, track, sector, sector_is_index, FALSE, &sector_data, &sector_length);
1021   if (err)
1022      return err;
1023
1024   crc_on_disk = sector_data[sector_length + 0];
1025   crc_on_disk <<= 8;
1026   crc_on_disk += sector_data[sector_length + 1];
1027
1028   calculated_crc = ccitt_crc16(0xE295, sector_data, sector_length);
1029   if (calculated_crc != crc_on_disk)
1030      return FLOPPY_ERROR_INVALIDIMAGE;
1031
1032   memcpy(buffer, sector_data, MIN(sector_length, buflen));
1033
1034   return FLOPPY_ERROR_SUCCESS;
1035}
1036
1037
1038
1039static floperr_t internal_coco_dmk_write_sector(floppy_image *floppy, int head, int track, int sector, int sector_is_index, const void *buffer, size_t buflen, int ddam)
1040{
1041   floperr_t err;
1042   UINT32 sector_length;
1043   UINT8 *sector_data;
1044   UINT16 crc;
1045
1046   err = coco_dmk_seek_sector_in_track(floppy, head, track, sector, sector_is_index, TRUE, &sector_data, &sector_length);
1047   if (err)
1048      return err;
1049
1050   if (buflen > sector_length)
1051      return FLOPPY_ERROR_INTERNAL;
1052
1053   memcpy(sector_data, buffer, buflen);
1054
1055   crc = ccitt_crc16(0xE295, sector_data, sector_length);
1056   sector_data[sector_length + 0] = crc >> 8;
1057   sector_data[sector_length + 1] = crc >> 0;
1058   return FLOPPY_ERROR_SUCCESS;
1059}
1060
1061
1062
1063static floperr_t coco_dmk_read_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen)
1064{
1065   return internal_coco_dmk_read_sector(floppy, head, track, sector, FALSE, buffer, buflen);
1066}
1067
1068static floperr_t coco_dmk_write_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam)
1069{
1070   return internal_coco_dmk_write_sector(floppy, head, track, sector, FALSE, buffer, buflen, ddam);
1071}
1072
1073static floperr_t coco_dmk_read_indexed_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen)
1074{
1075   return internal_coco_dmk_read_sector(floppy, head, track, sector, TRUE, buffer, buflen);
1076}
1077
1078static floperr_t coco_dmk_write_indexed_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam)
1079{
1080   return internal_coco_dmk_write_sector(floppy, head, track, sector, TRUE, buffer, buflen, ddam);
1081}
1082
1083
1084
1085static void coco_dmk_interpret_header(floppy_image *floppy, int *heads, int *tracks, int *track_size)
1086{
1087   UINT8 header[DMK_HEADER_LEN];
1088
1089   floppy_image_read(floppy, header, 0, DMK_HEADER_LEN);
1090
1091   if (tracks)
1092      *tracks = header[1];
1093   if (heads)
1094      *heads = (header[4] & 0x10) ? 1 : 2;
1095   if (track_size)
1096      *track_size = ((int) header[3]) * 0x100 + header[2];
1097}
1098
1099
1100
1101FLOPPY_CONSTRUCT(coco_dmk_construct)
1102{
1103   struct FloppyCallbacks *callbacks;
1104   struct dmk_tag *tag;
1105   UINT8 header[DMK_HEADER_LEN];
1106   int heads, tracks, track_size, sectors, sector_length;
1107
1108   if (params)
1109   {
1110      heads         = option_resolution_lookup_int(params, PARAM_HEADS);
1111      tracks         = option_resolution_lookup_int(params, PARAM_TRACKS);
1112      sectors         = option_resolution_lookup_int(params, PARAM_SECTORS);
1113      sector_length   = option_resolution_lookup_int(params, PARAM_SECTOR_LENGTH);
1114
1115      track_size = coco_dmk_min_track_size(sectors, sector_length) + DMK_EXTRA_TRACK_LENGTH;
1116
1117      memset(header, 0, sizeof(header));
1118      header[1] = tracks;
1119      header[2] = track_size >> 0;
1120      header[3] = track_size >> 8;
1121      header[4] = (heads == 2) ? 0x00 : 0x10;
1122
1123      floppy_image_write(floppy, header, 0, sizeof(header));
1124   }
1125   else
1126   {
1127      coco_dmk_interpret_header(floppy, &heads, &tracks, &track_size);
1128   }
1129
1130   tag = (dmk_tag *)floppy_create_tag(floppy, sizeof(struct dmk_tag));
1131   if (!tag)
1132      return FLOPPY_ERROR_OUTOFMEMORY;
1133   tag->heads = heads;
1134   tag->track_size = track_size;
1135   tag->tracks = tracks;
1136
1137   callbacks = floppy_callbacks(floppy);
1138   callbacks->read_track = coco_dmk_read_track;
1139   callbacks->write_track = coco_dmk_write_track;
1140   callbacks->get_track_data_offset = coco_dmk_get_track_data_offset;
1141   callbacks->format_track = coco_dmk_format_track;
1142   callbacks->get_heads_per_disk = coco_dmk_get_heads_per_disk;
1143   callbacks->get_tracks_per_disk = coco_dmk_get_tracks_per_disk;
1144   callbacks->get_track_size = coco_dmk_get_track_size;
1145   callbacks->get_sector_length = coco_dmk_get_sector_length;
1146   callbacks->get_indexed_sector_info = coco_dmk_get_indexed_sector_info;
1147   callbacks->read_sector = coco_dmk_read_sector;
1148   callbacks->write_sector = coco_dmk_write_sector;
1149   callbacks->read_indexed_sector = coco_dmk_read_indexed_sector;
1150   callbacks->write_indexed_sector = coco_dmk_write_indexed_sector;
1151
1152   return FLOPPY_ERROR_SUCCESS;
1153}
1154
1155
1156
1157FLOPPY_IDENTIFY(coco_dmk_identify)
1158{
1159   int heads, tracks, track_size;
1160   UINT64 size, expected_size;
1161
1162   size = floppy_image_size(floppy);
1163   coco_dmk_interpret_header(floppy, &heads, &tracks, &track_size);
1164   expected_size = DMK_HEADER_LEN + (heads * tracks * track_size);
1165   *vote = (size == expected_size) ? 100 : 0;
1166   return FLOPPY_ERROR_SUCCESS;
1167}
1168
1169
1170
1171/* ----------------------------------------------------------------------- */
1172
1173FLOPPY_OPTIONS_START( coco )
1174   FLOPPY_OPTION( coco_jvc, "dsk",         "CoCo JVC disk image",   coco_jvc_identify,   coco_jvc_construct, NULL,
1175      HEADS([1]-2)
1176      TRACKS([35]-255)
1177      SECTORS(1-[18]-255)
1178      SECTOR_LENGTH(128/[256]/512/1024)
1179      FIRST_SECTOR_ID(0-[1]))
1180   FLOPPY_OPTION( coco_os9, "os9",         "CoCo OS-9 disk image",   coco_os9_identify,   coco_os9_construct, NULL,
1181      HEADS([1]-2)
1182      TRACKS([35]-255)
1183      SECTORS(1-[18]-255)
1184      SECTOR_LENGTH([256])
1185      FIRST_SECTOR_ID([1]))
1186   FLOPPY_OPTION( coco_vdk, "vdk",         "CoCo VDK disk image",   coco_vdk_identify,   coco_vdk_construct, NULL,
1187      HEADS([1]-2)
1188      TRACKS([35]-255)
1189      SECTORS([18])
1190      SECTOR_LENGTH([256])
1191      FIRST_SECTOR_ID([1]))
1192   FLOPPY_OPTION( coco_dmk, "dsk,dmk",   "CoCo DMK disk image",   coco_dmk_identify,   coco_dmk_construct, NULL,
1193      HEADS([1]-2)
1194      TRACKS([35]-255)
1195      SECTORS(1-[18])
1196      SECTOR_LENGTH(128/[256]/512/1024/2048/4096/8192)
1197      INTERLEAVE(0-[6]-17)
1198      FIRST_SECTOR_ID(0-[1]))
1199FLOPPY_OPTIONS_END
src/lib/formats/vt_cas.h
r0r11458
1/*********************************************************************
2
3    vt_cas.h
4
5    Format code for VTech casette files
6
7*********************************************************************/
8
9#ifndef VT_CAS_H
10#define VT_CAS_H
11
12#include "cassimg.h"
13
14CASSETTE_FORMATLIST_EXTERN(vtech1_cassette_formats);
15CASSETTE_FORMATLIST_EXTERN(vtech2_cassette_formats);
16
17#endif /* VT_CAS_H */
src/lib/formats/nes_dsk.c
r0r11458
1/*********************************************************************
2
3    formats/nes_dsk.c
4
5    NES disk images
6
7*********************************************************************/
8
9
10#include <string.h>
11
12#include "formats/nes_dsk.h"
13#include "formats/basicdsk.h"
14
15
16
17/*****************************************************************************
18 NES floppy core functions
19*****************************************************************************/
20
21
22static FLOPPY_IDENTIFY( nes_dsk_identify )
23{
24   UINT64 size;
25   UINT8 header[3];
26
27   *vote = 0;
28
29   /* get first 3 bytes */
30   floppy_image_read(floppy, &header, 0, sizeof(header));
31
32   /* first check the size of the image */
33   size = floppy_image_size(floppy);
34
35   if ((size == 65516) || (size == 131016) || (size == 262016))
36   {
37      /* the image has an header, hence check the first sector for the magic string */
38      if (!memcmp(header, "FDS", 3))
39         *vote = 100;
40   }
41
42   if ((size == 65500) || (size == 131000) || (size == 262000))
43   {
44      /* the image has no header, hence let's trust the extension and load the file */
45      *vote = 100;
46   }
47
48   return FLOPPY_ERROR_SUCCESS;
49}
50
51
52static FLOPPY_CONSTRUCT( nes_dsk_construct )
53{
54   return FLOPPY_ERROR_SUCCESS;
55}
56
57
58/*****************************************************************************
59 NES floppy options
60*****************************************************************************/
61
62FLOPPY_OPTIONS_START( nes_only )
63   FLOPPY_OPTION(
64      fds_dsk,
65      "fds",
66      "NES floppy disk image",
67      nes_dsk_identify,
68      nes_dsk_construct,
69      NULL,
70      NULL
71   )
72FLOPPY_OPTIONS_END0
src/lib/formats/csw_cas.h
r0r11458
1/*
2    csw_cas.h
3
4    Format code for csw cassette files
5*/
6
7#ifndef CSW_CAS_H
8#define CSW_CAS_H
9
10#include "cassimg.h"
11
12CASSETTE_FORMATLIST_EXTERN(csw_cassette_formats);
13CASSETTE_FORMATLIST_EXTERN(bbc_cassette_formats);
14
15#endif /* CSW_CAS_H */
src/lib/formats/coco_dsk.h
r0r11458
1/*********************************************************************
2
3    formats/coco_dsk.h
4
5    Tandy Color Computer / Dragon disk images
6
7*********************************************************************/
8
9#ifndef COCO_DSK_H
10#define COCO_DSK_H
11
12#include "flopimg.h"
13
14
15/**************************************************************************/
16
17FLOPPY_OPTIONS_EXTERN(coco);
18
19FLOPPY_IDENTIFY(coco_dmk_identify);
20FLOPPY_CONSTRUCT(coco_dmk_construct);
21
22#endif /* COCO_DSK_H */
src/lib/formats/nes_dsk.h
r0r11458
1/*********************************************************************
2
3    formats/nes_dsk.h
4
5    NES disk images
6
7*********************************************************************/
8
9#ifndef NES_DSK_H_
10#define NES_DSK_H_
11
12#include "flopimg.h"
13
14FLOPPY_OPTIONS_EXTERN(nes_only);
15
16#endif /*NES_DSK_H_*/
src/lib/formats/x1_tap.c
r0r11458
1/*
2 *
3 *   Sharp X1 TAP image format
4 *
5 *   "New" header format:
6 *   0x00: Index - must be "TAPE" (4 bytes)
7 *   0x04: Image title. (null-terminated string, 17 bytes)
8 *   0x15: Reserved (5 bytes)
9 *   0x1a: Write protect (bit 4, 1 byte)
10 *   0x1b: Format (bit 0, 1 byte)
11 *            if bit 0 is high, uses "speed limit sampling method"
12 *   0x1c: Sample rate, per bit (in Hz, 4 bytes)
13 *   0x20: Tape data size, in bits (4 bytes)
14 *   0x24: Tape position (4 bytes, usually 0)
15 *   0x28: Tape data (data size / 8)
16 *
17 *   "Old" header format:
18 *   0x00: Sampling rate (4 bytes)
19 *
20 */
21
22#include "x1_tap.h"
23
24#define WAVE_HIGH       0x5a9e
25#define WAVE_LOW      -0x5a9e
26
27static int cas_size;
28static int samplerate;
29
30static int x1_fill_wave(INT16* buffer, UINT8 data, int sample_pos)
31{
32   int x;
33   int sample_count = 0;
34
35   // one byte = 8 samples
36   for(x=0;x<8;x++)
37   {
38      if(buffer)
39         buffer[sample_pos+x] = (data & (1 << (7-x))) ? WAVE_HIGH : WAVE_LOW;
40      sample_count++;
41   }
42
43   return sample_count;
44}
45
46static int x1_handle_tap(INT16* buffer, const UINT8* casdata)
47{
48   int sample_count = 0;
49   int data_pos = 0x28;
50
51   if(memcmp(casdata, "TAPE",4))  // header check
52   {
53      logerror("TAP: image is not a 'new' format TAP image\n");
54      return -1;
55   }
56   if(samplerate != 8000)
57   {
58      logerror("TAP: images that are not 8000Hz are not yet supported\n");
59      return -1;
60   }
61
62   while(sample_count < cas_size)
63   {
64      sample_count += x1_fill_wave(buffer,casdata[data_pos],sample_count);
65      data_pos++;
66   }
67
68   return sample_count;
69}
70
71/*******************************************************************
72   Calculate the number of samples needed for this tape image
73********************************************************************/
74static int x1_cas_to_wav_size (const UINT8 *casdata, int caslen)
75{
76   UINT32 ret;
77
78   ret = casdata[0x20] | (casdata[0x21] << 8) | (casdata[0x22] << 16) | (casdata[0x23] << 24);
79   cas_size = ret;
80
81   samplerate = casdata[0x1c] | (casdata[0x1d] << 8) | (casdata[0x1e] << 16) | (casdata[0x1f] << 24);
82
83   return ret;
84}
85
86/*******************************************************************
87   Generate samples for the tape image
88********************************************************************/
89static int x1_cas_fill_wave(INT16 *buffer, int sample_count, UINT8 *bytes)
90{
91   return x1_handle_tap(buffer,bytes);
92}
93
94static const struct CassetteLegacyWaveFiller x1_legacy_fill_wave =
95{
96   x1_cas_fill_wave,                  /* fill_wave */
97   -1,                              /* chunk_size */
98   0,                              /* chunk_samples */
99   x1_cas_to_wav_size,               /* chunk_sample_calc */
100   8000,                           /* sample_frequency */
101   0,                              /* header_samples */
102   0                              /* trailer_samples */
103};
104
105static casserr_t x1_cas_identify(cassette_image *cassette, struct CassetteOptions *opts)
106{
107   return cassette_legacy_identify(cassette, opts, &x1_legacy_fill_wave);
108}
109
110
111
112static casserr_t x1_cas_load(cassette_image *cassette)
113{
114   return cassette_legacy_construct(cassette, &x1_legacy_fill_wave);
115}
116
117
118static const struct CassetteFormat x1_cassette_format = {
119   "tap",
120   x1_cas_identify,
121   x1_cas_load,
122   NULL
123};
124
125CASSETTE_FORMATLIST_START(x1_cassette_formats)
126   CASSETTE_FORMAT(x1_cassette_format)
127CASSETTE_FORMATLIST_END
src/lib/formats/flopimg.c
r0r11458
1/*********************************************************************
2
3    flopimg.c
4
5    Floppy disk image abstraction code
6
7*********************************************************************/
8
9#include <stdlib.h>
10#include <string.h>
11#include <stdio.h>
12#include <ctype.h>
13#include <assert.h>
14#include <limits.h>
15
16#include "osdcore.h"
17#include "ioprocs.h"
18#include "flopimg.h"
19#include "pool.h"
20#include "imageutl.h"
21
22#define TRACK_LOADED      0x01
23#define TRACK_DIRTY         0x02
24
25
26struct _floppy_image
27{
28   struct io_generic io;
29
30   const struct FloppyFormat *floppy_option;
31   struct FloppyCallbacks format;
32
33   /* loaded track stuff */
34   int loaded_track_head;
35   int loaded_track_index;
36   UINT32 loaded_track_size;
37   void *loaded_track_data;
38   UINT8 loaded_track_status;
39   UINT8 flags;
40
41   /* tagging system */
42   object_pool *tags;
43   void *tag_data;
44};
45
46
47
48struct _floppy_params
49{
50   int param;
51   int value;
52};
53
54
55
56static floperr_t floppy_track_unload(floppy_image *floppy);
57
58OPTION_GUIDE_START(floppy_option_guide)
59   OPTION_INT('H', "heads",         "Heads")
60   OPTION_INT('T', "tracks",         "Tracks")
61   OPTION_INT('S', "sectors",         "Sectors")
62   OPTION_INT('L', "sectorlength",      "Sector Bytes")
63   OPTION_INT('I', "interleave",      "Interleave")
64   OPTION_INT('F', "firstsectorid",   "First Sector")
65OPTION_GUIDE_END
66
67
68static void floppy_close_internal(floppy_image *floppy, int close_file);
69
70/*********************************************************************
71    opening, closing and creating of floppy images
72*********************************************************************/
73
74/* basic floppy_image initialization common to floppy_open() and floppy_create() */
75static floppy_image *floppy_init(void *fp, const struct io_procs *procs, int flags)
76{
77   floppy_image *floppy;
78
79   floppy = (floppy_image *)malloc(sizeof(struct _floppy_image));
80   if (!floppy)
81      return NULL;
82
83   memset(floppy, 0, sizeof(*floppy));
84   floppy->tags = pool_alloc_lib(NULL);
85   floppy->tag_data = NULL;
86   floppy->io.file = fp;
87   floppy->io.procs = procs;
88   floppy->io.filler = 0xFF;
89   floppy->flags = (UINT8) flags;
90   return floppy;
91}
92
93
94
95/* main code for identifying and maybe opening a disk image; not exposed
96 * directly because this function is big and hideous */
97static floperr_t floppy_open_internal(void *fp, const struct io_procs *procs, const char *extension,
98   const struct FloppyFormat *floppy_options, int max_options, int flags, floppy_image **outfloppy,
99   int *outoption)
100{
101   floperr_t err;
102   floppy_image *floppy;
103   int best_option = -1;
104   int best_vote = 0;
105   int vote;
106   size_t i;
107
108   floppy = floppy_init(fp, procs, flags);
109   if (!floppy)
110   {
111      err = FLOPPY_ERROR_OUTOFMEMORY;
112      goto done;
113   }
114
115   /* vote on the best format */
116   for (i = 0; (i < max_options) && floppy_options[i].construct; i++)
117   {
118      if (!extension || !floppy_options[i].extensions || image_find_extension(floppy_options[i].extensions, extension))
119      {
120         if (floppy_options[i].identify)
121         {
122            vote = 0;
123            err = floppy_options[i].identify(floppy, &floppy_options[i], &vote);
124            if (err)
125               goto done;
126         }
127         else
128         {
129            vote = 1;
130         }
131
132         /* is this option a better one? */
133         if (vote > best_vote)
134         {
135            best_vote = vote;
136            best_option = i;
137         }
138      }
139   }
140
141   /* did we find a format? */
142   if (best_option == -1)
143   {
144      err = FLOPPY_ERROR_INVALIDIMAGE;
145      goto done;
146   }
147
148   if (outfloppy)
149   {
150      /* call the format constructor */
151      err = floppy_options[best_option].construct(floppy, &floppy_options[best_option], NULL);
152      if (err)
153         goto done;
154
155      floppy->floppy_option = &floppy_options[best_option];
156   }
157
158   err = FLOPPY_ERROR_SUCCESS;
159
160done:
161   /* if we have a floppy disk and we either errored or are not keeping it, close it */
162   if (floppy && (!outfloppy || err))
163   {
164      floppy_close_internal(floppy, FALSE);
165      floppy = NULL;
166   }
167
168   if (outoption)
169      *outoption = err ? -1 : best_option;
170   if (outfloppy)
171      *outfloppy = floppy;
172   return err;
173}
174
175
176
177floperr_t floppy_identify(void *fp, const struct io_procs *procs, const char *extension,
178   const struct FloppyFormat *formats, int *identified_format)
179{
180   return floppy_open_internal(fp, procs, extension, formats, INT_MAX, FLOPPY_FLAGS_READONLY, NULL, identified_format);
181}
182
183
184
185floperr_t floppy_open(void *fp, const struct io_procs *procs, const char *extension,
186   const struct FloppyFormat *format, int flags, floppy_image **outfloppy)
187{
188   return floppy_open_internal(fp, procs, extension, format, 1, flags, outfloppy, NULL);
189}
190
191
192
193floperr_t floppy_open_choices(void *fp, const struct io_procs *procs, const char *extension,
194   const struct FloppyFormat *formats, int flags, floppy_image **outfloppy)
195{
196   return floppy_open_internal(fp, procs, extension, formats, INT_MAX, flags, outfloppy, NULL);
197}
198
199
200
201static floperr_t option_to_floppy_error(optreserr_t oerr)
202{
203   floperr_t err;
204   switch(oerr) {
205   case OPTIONRESOLUTION_ERROR_SUCCESS:
206      err = FLOPPY_ERROR_SUCCESS;
207      break;
208   case OPTIONRESOLUTION_ERROR_OUTOFMEMORY:
209      err = FLOPPY_ERROR_OUTOFMEMORY;
210      break;
211   case OPTIONRESOLUTION_ERROR_PARAMOUTOFRANGE:
212   case OPTIONRESOLUTION_ERROR_PARAMNOTSPECIFIED:
213   case OPTIONRESOLUTION_ERROR_PARAMNOTFOUND:
214   case OPTIONRESOLUTION_ERROR_PARAMALREADYSPECIFIED:
215   case OPTIONRESOLUTION_ERROR_BADPARAM:
216   case OPTIONRESOLUTION_ERROR_SYNTAX:
217   default:
218      err = FLOPPY_ERROR_INTERNAL;
219      break;
220   };
221   return err;
222}
223
224
225
226floperr_t floppy_create(void *fp, const struct io_procs *procs, const struct FloppyFormat *format, option_resolution *parameters, floppy_image **outfloppy)
227{
228   floppy_image *floppy = NULL;
229   optreserr_t oerr;
230   floperr_t err;
231   int heads, tracks, h, t;
232   option_resolution *alloc_resolution = NULL;
233
234   assert(format);
235
236   /* create the new image */
237   floppy = floppy_init(fp, procs, 0);
238   if (!floppy)
239   {
240      err = FLOPPY_ERROR_OUTOFMEMORY;
241      goto done;
242   }
243
244   /* if this format expects creation parameters and none were specified, create some */
245   if (!parameters && format->param_guidelines)
246   {
247      alloc_resolution = option_resolution_create(floppy_option_guide, format->param_guidelines);
248      if (!alloc_resolution)
249      {
250         err = FLOPPY_ERROR_OUTOFMEMORY;
251         goto done;
252      }
253      parameters = alloc_resolution;
254   }
255
256   /* finish the parameters, if specified */
257   if (parameters)
258   {
259      oerr = option_resolution_finish(parameters);
260      if (oerr)
261      {
262         err = option_to_floppy_error(oerr);
263         goto done;
264      }
265   }
266
267   /* call the format constructor */
268   err = format->construct(floppy, format, parameters);
269   if (err)
270      goto done;
271
272   /* format the disk, ignoring if formatting not implemented */
273   if (floppy->format.format_track)
274   {
275      heads = floppy_get_heads_per_disk(floppy);
276      tracks = floppy_get_tracks_per_disk(floppy);
277
278      for (h = 0; h < heads; h++)
279      {
280         for (t = 0; t < tracks; t++)
281         {
282            err = floppy->format.format_track(floppy, h, t, parameters);
283            if (err)
284               goto done;
285         }
286      }
287   }
288
289   /* call the post_format function, if present */
290   if (floppy->format.post_format)
291   {
292      err = floppy->format.post_format(floppy, parameters);
293      if (err)
294         goto done;
295   }
296
297   floppy->floppy_option = format;
298   err = FLOPPY_ERROR_SUCCESS;
299
300done:
301   if (err && floppy)
302   {
303      floppy_close_internal(floppy, FALSE);
304      floppy = NULL;
305   }
306
307   if (outfloppy)
308      *outfloppy = floppy;
309   else if (floppy)
310      floppy_close_internal(floppy, FALSE);
311
312   if (alloc_resolution)
313      option_resolution_close(alloc_resolution);
314   return err;
315}
316
317
318
319static void floppy_close_internal(floppy_image *floppy, int close_file)
320{
321   if (floppy) {
322      floppy_track_unload(floppy);
323     
324      if(floppy->floppy_option->destruct)
325         floppy->floppy_option->destruct(floppy, floppy->floppy_option);
326      if (close_file)
327         io_generic_close(&floppy->io);
328      if (floppy->loaded_track_data)
329         free(floppy->loaded_track_data);
330      pool_free_lib(floppy->tags);
331
332      free(floppy);
333   }
334}
335
336
337
338void floppy_close(floppy_image *floppy)
339{
340   floppy_close_internal(floppy, TRUE);
341}
342
343
344
345/*********************************************************************
346    functions useful in format constructors
347*********************************************************************/
348
349struct FloppyCallbacks *floppy_callbacks(floppy_image *floppy)
350{
351   assert(floppy);
352   return &floppy->format;
353}
354
355
356
357void *floppy_tag(floppy_image *floppy)
358{
359   assert(floppy);
360   return floppy->tag_data;
361}
362
363
364
365void *floppy_create_tag(floppy_image *floppy, size_t tagsize)
366{
367   floppy->tag_data = pool_malloc_lib(floppy->tags,tagsize);
368   return floppy->tag_data;
369}
370
371
372
373UINT8 floppy_get_filler(floppy_image *floppy)
374{
375   return floppy->io.filler;
376}
377
378
379
380void floppy_set_filler(floppy_image *floppy, UINT8 filler)
381{
382   floppy->io.filler = filler;
383}
384
385
386
387/*********************************************************************
388    calls for accessing the raw disk image
389*********************************************************************/
390
391void floppy_image_read(floppy_image *floppy, void *buffer, UINT64 offset, size_t length)
392{
393   io_generic_read(&floppy->io, buffer, offset, length);
394}
395
396
397
398void floppy_image_write(floppy_image *floppy, const void *buffer, UINT64 offset, size_t length)
399{
400   io_generic_write(&floppy->io, buffer, offset, length);
401}
402
403
404
405void floppy_image_write_filler(floppy_image *floppy, UINT8 filler, UINT64 offset, size_t length)
406{
407   io_generic_write_filler(&floppy->io, filler, offset, length);
408}
409
410
411
412UINT64 floppy_image_size(floppy_image *floppy)
413{
414   return io_generic_size(&floppy->io);
415}
416
417
418
419/*********************************************************************
420    calls for accessing disk image data
421*********************************************************************/
422
423static floperr_t floppy_readwrite_sector(floppy_image *floppy, int head, int track, int sector, int offset,
424   void *buffer, size_t buffer_len, int writing, int indexed, int ddam)
425{
426   floperr_t err;
427   const struct FloppyCallbacks *fmt;
428   size_t this_buffer_len;
429   UINT8 *alloc_buf = NULL;
430   UINT32 sector_length;
431   UINT8 *buffer_ptr = (UINT8 *)buffer;
432   floperr_t (*read_sector)(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen);
433   floperr_t (*write_sector)(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam);
434
435   fmt = floppy_callbacks(floppy);
436
437   /* choose proper calls for indexed vs non-indexed */
438   if (indexed)
439   {
440      read_sector = fmt->read_indexed_sector;
441      write_sector = fmt->write_indexed_sector;
442      if (!fmt->get_indexed_sector_info)
443      {
444         err = FLOPPY_ERROR_UNSUPPORTED;
445         goto done;
446      }
447   }
448   else
449   {
450      read_sector = fmt->read_sector;
451      write_sector = fmt->write_sector;
452      if (!fmt->get_sector_length)
453      {
454         err = FLOPPY_ERROR_UNSUPPORTED;
455         goto done;
456      }
457   }
458
459   /* check to make sure that the operation is supported */
460   if (!read_sector || (writing && !write_sector))
461   {
462      err = FLOPPY_ERROR_UNSUPPORTED;
463      goto done;
464   }
465
466   /* main loop */
467   while(buffer_len > 0)
468   {
469      /* find out the size of this sector */
470      if (indexed)
471         err = fmt->get_indexed_sector_info(floppy, head, track, sector, NULL, NULL, NULL, &sector_length, NULL);
472      else
473         err = fmt->get_sector_length(floppy, head, track, sector, &sector_length);
474      if (err)
475         goto done;
476
477      /* do we even do anything with this sector? */
478      if (offset < sector_length)
479      {
480         /* ok we will be doing something */
481         if ((offset > 0) || (buffer_len < sector_length))
482         {
483            /* we will be doing an partial read/write; in other words we
484                 * will not be reading/writing a full sector */
485            if (alloc_buf) free(alloc_buf);
486            alloc_buf = (UINT8*)malloc(sector_length);
487            if (!alloc_buf)
488            {
489               err = FLOPPY_ERROR_OUTOFMEMORY;
490               goto done;
491            }
492
493            /* read the sector (we need to do this even when writing */
494            err = read_sector(floppy, head, track, sector, alloc_buf, sector_length);
495            if (err)
496               goto done;
497
498            this_buffer_len = MIN(buffer_len, sector_length - offset);
499
500            if (writing)
501            {
502               memcpy(alloc_buf + offset, buffer_ptr, this_buffer_len);
503
504               err = write_sector(floppy, head, track, sector, alloc_buf, sector_length, ddam);
505               if (err)
506                  goto done;
507            }
508            else
509            {
510               memcpy(buffer_ptr, alloc_buf + offset, this_buffer_len);
511            }
512            offset += this_buffer_len;
513            offset %= sector_length;
514         }
515         else
516         {
517            this_buffer_len = sector_length;
518
519            if (writing)
520               err = write_sector(floppy, head, track, sector, buffer_ptr, sector_length, ddam);
521            else
522               err = read_sector(floppy, head, track, sector, buffer_ptr, sector_length);
523            if (err)
524               goto done;
525         }
526      }
527      else
528      {
529         /* skip this sector */
530         offset -= sector_length;
531         this_buffer_len = 0;
532      }
533
534      buffer_ptr += this_buffer_len;
535      buffer_len -= this_buffer_len;
536      sector++;
537   }
538
539   err = FLOPPY_ERROR_SUCCESS;
540
541done:
542   if (alloc_buf)
543      free(alloc_buf);
544   return err;
545}
546
547
548
549floperr_t floppy_read_sector(floppy_image *floppy, int head, int track, int sector, int offset,   void *buffer, size_t buffer_len)
550{
551   return floppy_readwrite_sector(floppy, head, track, sector, offset, buffer, buffer_len, FALSE, FALSE, 0);
552}
553
554
555
556floperr_t floppy_write_sector(floppy_image *floppy, int head, int track, int sector, int offset, const void *buffer, size_t buffer_len, int ddam)
557{
558   return floppy_readwrite_sector(floppy, head, track, sector, offset, (void *) buffer, buffer_len, TRUE, FALSE, ddam);
559}
560
561
562
563floperr_t floppy_read_indexed_sector(floppy_image *floppy, int head, int track, int sector_index, int offset,   void *buffer, size_t buffer_len)
564{
565   return floppy_readwrite_sector(floppy, head, track, sector_index, offset, buffer, buffer_len, FALSE, TRUE, 0);
566}
567
568
569
570floperr_t floppy_write_indexed_sector(floppy_image *floppy, int head, int track, int sector_index, int offset, const void *buffer, size_t buffer_len, int ddam)
571{
572   return floppy_readwrite_sector(floppy, head, track, sector_index, offset, (void *) buffer, buffer_len, TRUE, TRUE, ddam);
573}
574
575
576static floperr_t floppy_get_track_data_offset(floppy_image *floppy, int head, int track, UINT64 *offset)
577{
578   floperr_t err;
579   const struct FloppyCallbacks *callbacks;
580
581   *offset = 0;
582   callbacks = floppy_callbacks(floppy);
583   if (callbacks->get_track_data_offset)
584   {
585      err = callbacks->get_track_data_offset(floppy, head, track, offset);
586      if (err)
587         return err;
588   }
589   return FLOPPY_ERROR_SUCCESS;
590}
591
592
593
594static floperr_t floppy_read_track_offset(floppy_image *floppy, int head, int track, UINT64 offset, void *buffer, size_t buffer_len)
595{
596   floperr_t err;
597   const struct FloppyCallbacks *format;
598
599   format = floppy_callbacks(floppy);
600
601   if (!format->read_track)
602      return FLOPPY_ERROR_UNSUPPORTED;
603
604   err = floppy_track_unload(floppy);
605   if (err)
606      return err;
607
608   err = format->read_track(floppy, head, track, offset, buffer, buffer_len);
609   if (err)
610      return err;
611
612   return FLOPPY_ERROR_SUCCESS;
613}
614
615
616
617floperr_t floppy_read_track(floppy_image *floppy, int head, int track, void *buffer, size_t buffer_len)
618{
619   return floppy_read_track_offset(floppy, head, track, 0, buffer, buffer_len);
620}
621
622
623
624floperr_t floppy_read_track_data(floppy_image *floppy, int head, int track, void *buffer, size_t buffer_len)
625{
626   floperr_t err;
627   UINT64 offset;
628
629   err = floppy_get_track_data_offset(floppy, head, track, &offset);
630   if (err)
631      return err;
632
633   return floppy_read_track_offset(floppy, head, track, offset, buffer, buffer_len);
634}
635
636
637
638static floperr_t floppy_write_track_offset(floppy_image *floppy, int head, int track, UINT64 offset, const void *buffer, size_t buffer_len)
639{
640   floperr_t err;
641
642   /* track writing supported? */
643   if (!floppy_callbacks(floppy)->write_track)
644      return FLOPPY_ERROR_UNSUPPORTED;
645
646   /* read only? */
647   if (floppy->flags & FLOPPY_FLAGS_READONLY)
648      return FLOPPY_ERROR_READONLY;
649
650   err = floppy_track_unload(floppy);
651   if (err)
652      return err;
653
654   err = floppy_callbacks(floppy)->write_track(floppy, head, track, offset, buffer, buffer_len);
655   if (err)
656      return err;
657
658   return FLOPPY_ERROR_SUCCESS;
659}
660
661
662
663floperr_t floppy_write_track(floppy_image *floppy, int head, int track, const void *buffer, size_t buffer_len)
664{
665   return floppy_write_track_offset(floppy, head, track, 0, buffer, buffer_len);
666}
667
668
669
670floperr_t floppy_write_track_data(floppy_image *floppy, int head, int track, const void *buffer, size_t buffer_len)
671{
672   floperr_t err;
673   UINT64 offset;
674
675   err = floppy_get_track_data_offset(floppy, head, track, &offset);
676   if (err)
677      return err;
678
679   return floppy_write_track_offset(floppy, head, track, offset, buffer, buffer_len);
680}
681
682
683
684floperr_t floppy_format_track(floppy_image *floppy, int head, int track, option_resolution *parameters)
685{
686   floperr_t err;
687   struct FloppyCallbacks *format;
688   option_resolution *alloc_resolution = NULL;
689   optreserr_t oerr;
690
691   /* supported? */
692   format = floppy_callbacks(floppy);
693   if (!format->format_track)
694   {
695      err = FLOPPY_ERROR_UNSUPPORTED;
696      goto done;
697   }
698
699   /* create a dummy resolution; if no parameters were specified */
700   if (!parameters)
701   {
702      alloc_resolution = option_resolution_create(floppy_option_guide, floppy->floppy_option->param_guidelines);
703      if (!alloc_resolution)
704      {
705         err = FLOPPY_ERROR_OUTOFMEMORY;
706         goto done;
707      }
708      parameters = alloc_resolution;
709   }
710
711   oerr = option_resolution_finish(parameters);
712   if (oerr)
713   {
714      err = option_to_floppy_error(oerr);
715      goto done;
716   }
717
718   err = format->format_track(floppy, head, track, parameters);
719   if (err)
720      goto done;
721
722done:
723   if (alloc_resolution)
724      option_resolution_close(alloc_resolution);
725   return err;
726}
727
728
729
730int floppy_get_tracks_per_disk(floppy_image *floppy)
731{
732   return floppy_callbacks(floppy)->get_tracks_per_disk(floppy);
733}
734
735
736
737int floppy_get_heads_per_disk(floppy_image *floppy)
738{
739   return floppy_callbacks(floppy)->get_heads_per_disk(floppy);
740}
741
742
743
744UINT32 floppy_get_track_size(floppy_image *floppy, int head, int track)
745{
746   const struct FloppyCallbacks *fmt;
747
748   fmt = floppy_callbacks(floppy);
749   if (!fmt->get_track_size)
750      return 0;
751
752   return fmt->get_track_size(floppy, head, track);
753}
754
755
756
757floperr_t floppy_get_sector_length(floppy_image *floppy, int head, int track, int sector, UINT32 *sector_length)
758{
759   const struct FloppyCallbacks *fmt;
760
761   fmt = floppy_callbacks(floppy);
762   if (!fmt->get_sector_length)
763      return FLOPPY_ERROR_UNSUPPORTED;
764
765   return fmt->get_sector_length(floppy, head, track, sector, sector_length);
766}
767
768
769
770floperr_t floppy_get_indexed_sector_info(floppy_image *floppy, int head, int track, int sector_index, int *cylinder, int *side, int *sector, UINT32 *sector_length, unsigned long *flags)
771{
772   const struct FloppyCallbacks *fmt;
773
774   fmt = floppy_callbacks(floppy);
775   if (!fmt->get_indexed_sector_info)
776      return FLOPPY_ERROR_UNSUPPORTED;
777
778   return fmt->get_indexed_sector_info(floppy, head, track, sector_index, cylinder, side, sector, sector_length, flags);
779}
780
781
782
783floperr_t floppy_get_sector_count(floppy_image *floppy, int head, int track, int *sector_count)
784{
785   floperr_t err;
786   int sector_index = 0;
787
788   do
789   {
790      err = floppy_get_indexed_sector_info(floppy, head, track, sector_index, NULL, NULL, NULL, NULL, NULL);
791      if (!err)
792         sector_index++;
793   }
794   while(!err);
795
796   if (sector_index && (err == FLOPPY_ERROR_SEEKERROR))
797      err = FLOPPY_ERROR_SUCCESS;
798   if (sector_count)
799      *sector_count = err ? 0 : sector_index;
800   return err;
801}
802
803
804
805int floppy_is_read_only(floppy_image *floppy)
806{
807   return floppy->flags & FLOPPY_FLAGS_READONLY;
808}
809
810
811
812UINT8 floppy_random_byte(floppy_image *floppy)
813{
814   /* can't use mame_rand(); this might not be in the core */
815#ifdef rand
816#undef rand
817#endif
818   return rand();
819}
820
821
822
823/*********************************************************************
824    calls for track based IO
825*********************************************************************/
826
827floperr_t floppy_load_track(floppy_image *floppy, int head, int track, int dirtify, void **track_data, size_t *track_length)
828{
829   floperr_t err;
830   void *new_loaded_track_data;
831   UINT32 track_size;
832
833   /* have we already loaded this track? */
834   if (((floppy->loaded_track_status & TRACK_LOADED) == 0) || (head != floppy->loaded_track_head) || (track != floppy->loaded_track_index))
835   {
836      err = floppy_track_unload(floppy);
837      if (err)
838         goto error;
839
840      track_size = floppy_callbacks(floppy)->get_track_size(floppy, head, track);
841
842      if (floppy->loaded_track_data) free(floppy->loaded_track_data);
843      new_loaded_track_data = malloc(track_size);
844      if (!new_loaded_track_data)
845      {
846         err = FLOPPY_ERROR_OUTOFMEMORY;
847         goto error;
848      }
849
850      floppy->loaded_track_data = new_loaded_track_data;
851      floppy->loaded_track_size = track_size;
852      floppy->loaded_track_head = head;
853      floppy->loaded_track_index = track;
854
855      err = floppy_callbacks(floppy)->read_track(floppy, floppy->loaded_track_head, floppy->loaded_track_index, 0, floppy->loaded_track_data, floppy->loaded_track_size);
856      if (err)
857         goto error;
858
859      floppy->loaded_track_status |= TRACK_LOADED | (dirtify ? TRACK_DIRTY : 0);
860   }
861   else
862      floppy->loaded_track_status |= (dirtify ? TRACK_DIRTY : 0);
863
864   if (track_data)
865      *track_data = floppy->loaded_track_data;
866   if (track_length)
867      *track_length = floppy->loaded_track_size;
868   return FLOPPY_ERROR_SUCCESS;
869
870error:
871   if (track_data)
872      *track_data = NULL;
873   if (track_length)
874      *track_length = 0;
875   return err;
876}
877
878
879
880static floperr_t floppy_track_unload(floppy_image *floppy)
881{
882   int err;
883   if (floppy->loaded_track_status & TRACK_DIRTY)
884   {
885      err = floppy_callbacks(floppy)->write_track(floppy, floppy->loaded_track_head, floppy->loaded_track_index, 0, floppy->loaded_track_data, floppy->loaded_track_size);
886      if (err)
887         return (floperr_t)err;
888   }
889
890   floppy->loaded_track_status &= ~(TRACK_LOADED | TRACK_DIRTY);
891   return FLOPPY_ERROR_SUCCESS;
892}
893
894
895
896/*********************************************************************
897    accessors for meta information about the image
898*********************************************************************/
899
900const char *floppy_format_description(floppy_image *floppy)
901{
902   return floppy->floppy_option->description;
903}
904
905
906
907/*********************************************************************
908    misc calls
909*********************************************************************/
910
911const char *floppy_error(floperr_t err)
912{
913   static const char *const error_messages[] =
914   {
915      "The operation completed successfully",
916      "Fatal internal error",
917      "This operation is unsupported",
918      "Out of memory",
919      "Seek error",
920      "Invalid image",
921      "Attempted to write to read only image",
922      "No space left on image",
923      "Parameter out of range",
924      "Required parameter not specified"
925   };
926
927   if ((err < 0) || (err >= ARRAY_LENGTH(error_messages)))
928      return NULL;
929   return error_messages[err];
930}
931
932
933FLOPPY_OPTIONS_START(default)
934FLOPPY_OPTIONS_END
src/lib/formats/x1_tap.h
r0r11458
1/*
2 *
3 *   Sharp X1 TAP image format
4 *
5 */
6
7#ifndef X1_TAP_H_
8#define X1_TAP_H_
9
10#include "cassimg.h"
11
12CASSETTE_FORMATLIST_EXTERN( x1_cassette_formats );
13
14#endif /*X1_TAP_H_*/
src/lib/formats/flopimg.h
r0r11458
1/*********************************************************************
2
3    flopimg.h
4
5    Floppy disk image abstraction code
6
7*********************************************************************/
8
9#ifndef FLOPIMG_H
10#define FLOPIMG_H
11
12#include "osdcore.h"
13#include "ioprocs.h"
14#include "opresolv.h"
15
16#ifndef logerror
17#define logerror printf
18#endif
19
20/***************************************************************************
21
22    Constants
23
24***************************************************************************/
25
26#define FLOPPY_FLAGS_READWRITE      0
27#define FLOPPY_FLAGS_READONLY      1
28
29/* sector has a deleted data address mark */
30#define ID_FLAG_DELETED_DATA   0x0001
31/* CRC error in id field */
32#define ID_FLAG_CRC_ERROR_IN_ID_FIELD 0x0002
33/* CRC error in data field */
34#define ID_FLAG_CRC_ERROR_IN_DATA_FIELD 0x0004
35
36
37/***************************************************************************
38
39    Type definitions
40
41***************************************************************************/
42
43typedef enum
44{
45   FLOPPY_ERROR_SUCCESS,         /* no error */
46   FLOPPY_ERROR_INTERNAL,         /* fatal internal error */
47   FLOPPY_ERROR_UNSUPPORTED,      /* this operation is unsupported */
48   FLOPPY_ERROR_OUTOFMEMORY,      /* ran out of memory */
49   FLOPPY_ERROR_SEEKERROR,         /* attempted to seek to nonexistant location */
50   FLOPPY_ERROR_INVALIDIMAGE,      /* this image in invalid */
51   FLOPPY_ERROR_READONLY,         /* attempt to write to read-only image */
52   FLOPPY_ERROR_NOSPACE,
53   FLOPPY_ERROR_PARAMOUTOFRANGE,
54   FLOPPY_ERROR_PARAMNOTSPECIFIED
55}
56floperr_t;
57
58typedef struct _floppy_image floppy_image;
59
60struct FloppyCallbacks
61{
62   floperr_t (*read_sector)(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen);
63   floperr_t (*write_sector)(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam);
64   floperr_t (*read_indexed_sector)(floppy_image *floppy, int head, int track, int sector_index, void *buffer, size_t buflen);
65   floperr_t (*write_indexed_sector)(floppy_image *floppy, int head, int track, int sector_index, const void *buffer, size_t buflen, int ddam);
66   floperr_t (*read_track)(floppy_image *floppy, int head, int track, UINT64 offset, void *buffer, size_t buflen);
67   floperr_t (*write_track)(floppy_image *floppy, int head, int track, UINT64 offset, const void *buffer, size_t buflen);
68   floperr_t (*format_track)(floppy_image *floppy, int head, int track, option_resolution *params);
69   floperr_t (*post_format)(floppy_image *floppy, option_resolution *params);
70   int (*get_heads_per_disk)(floppy_image *floppy);
71   int (*get_tracks_per_disk)(floppy_image *floppy);
72   int (*get_sectors_per_track)(floppy_image *floppy, int head, int track);
73   UINT32 (*get_track_size)(floppy_image *floppy, int head, int track);
74   floperr_t (*get_sector_length)(floppy_image *floppy, int head, int track, int sector, UINT32 *sector_length);
75   floperr_t (*get_indexed_sector_info)(floppy_image *floppy, int head, int track, int sector_index, int *cylinder, int *side, int *sector, UINT32 *sector_length, unsigned long *flags);
76   floperr_t (*get_track_data_offset)(floppy_image *floppy, int head, int track, UINT64 *offset);
77};
78
79
80
81struct FloppyFormat
82{
83   const char *name;
84   const char *extensions;
85   const char *description;
86   floperr_t (*identify)(floppy_image *floppy, const struct FloppyFormat *format, int *vote);
87   floperr_t (*construct)(floppy_image *floppy, const struct FloppyFormat *format, option_resolution *params);
88   floperr_t (*destruct)(floppy_image *floppy, const struct FloppyFormat *format);
89   const char *param_guidelines;
90};
91
92#define FLOPPY_IDENTIFY(name)   floperr_t name(floppy_image *floppy, const struct FloppyFormat *format, int *vote)
93#define FLOPPY_CONSTRUCT(name)   floperr_t name(floppy_image *floppy, const struct FloppyFormat *format, option_resolution *params)
94#define FLOPPY_DESTRUCT(name)   floperr_t name(floppy_image *floppy, const struct FloppyFormat *format)
95
96FLOPPY_IDENTIFY(td0_dsk_identify);
97FLOPPY_CONSTRUCT(td0_dsk_construct);
98FLOPPY_DESTRUCT(td0_dsk_destruct);
99
100FLOPPY_IDENTIFY(imd_dsk_identify);
101FLOPPY_CONSTRUCT(imd_dsk_construct);
102
103FLOPPY_IDENTIFY(cqm_dsk_identify);
104FLOPPY_CONSTRUCT(cqm_dsk_construct);
105
106FLOPPY_IDENTIFY(dsk_dsk_identify);
107FLOPPY_CONSTRUCT(dsk_dsk_construct);
108
109FLOPPY_IDENTIFY(d88_dsk_identify);
110FLOPPY_CONSTRUCT(d88_dsk_construct);
111
112FLOPPY_IDENTIFY(fdi_dsk_identify);
113FLOPPY_CONSTRUCT(fdi_dsk_construct);
114
115#define FLOPPY_OPTIONS_NAME(name)   floppyoptions_##name
116
117#define FLOPPY_OPTIONS_START(name)                                    \
118   const struct FloppyFormat floppyoptions_##name[] =                        \
119   {                                                         \
120
121#define FLOPPY_OPTIONS_END0 \
122      { NULL }                     \
123   };
124
125#define FLOPPY_OPTIONS_EXTERN(name)                                    \
126   extern const struct FloppyFormat floppyoptions_##name[]                     \
127
128#define FLOPPY_OPTION(name, extensions_, description_, identify_, construct_, destruct_, ranges_)\
129   { #name, extensions_, description_, identify_, construct_, destruct_, ranges_ },            \
130
131#define FLOPPY_OPTIONS_END                                          \
132      FLOPPY_OPTION( fdi, "fdi", "Formatted Disk Image", fdi_dsk_identify, fdi_dsk_construct, NULL, NULL) \
133      FLOPPY_OPTION( td0, "td0", "Teledisk floppy disk image",   td0_dsk_identify, td0_dsk_construct, td0_dsk_destruct, NULL) \
134      FLOPPY_OPTION( imd, "imd", "IMD floppy disk image",   imd_dsk_identify, imd_dsk_construct, NULL, NULL) \
135      FLOPPY_OPTION( cqm, "cqm,dsk", "CopyQM floppy disk image",   cqm_dsk_identify, cqm_dsk_construct, NULL, NULL) \
136      FLOPPY_OPTION( dsk, "dsk", "DSK floppy disk image",   dsk_dsk_identify, dsk_dsk_construct, NULL, NULL) \
137      FLOPPY_OPTION( d88, "d77,d88,1dd", "D88 Floppy Disk image", d88_dsk_identify, d88_dsk_construct, NULL, NULL) \
138   FLOPPY_OPTIONS_END0
139
140FLOPPY_OPTIONS_EXTERN(default);
141
142#define PARAM_END            '\0'
143#define PARAM_HEADS            'H'
144#define PARAM_TRACKS         'T'
145#define PARAM_SECTORS         'S'
146#define PARAM_SECTOR_LENGTH      'L'
147#define PARAM_INTERLEAVE      'I'
148#define PARAM_FIRST_SECTOR_ID   'F'
149
150#define HEADS(range)         "H" #range
151#define TRACKS(range)         "T" #range
152#define SECTORS(range)         "S" #range
153#define SECTOR_LENGTH(range)   "L" #range
154#define INTERLEAVE(range)      "I" #range
155#define FIRST_SECTOR_ID(range)   "F" #range
156
157
158/***************************************************************************
159
160    Prototypes
161
162***************************************************************************/
163
164OPTION_GUIDE_EXTERN(floppy_option_guide);
165
166/* opening, closing and creating of floppy images */
167floperr_t floppy_open(void *fp, const struct io_procs *procs, const char *extension, const struct FloppyFormat *format, int flags, floppy_image **outfloppy);
168floperr_t floppy_open_choices(void *fp, const struct io_procs *procs, const char *extension, const struct FloppyFormat *formats, int flags, floppy_image **outfloppy);
169floperr_t floppy_create(void *fp, const struct io_procs *procs, const struct FloppyFormat *format, option_resolution *parameters, floppy_image **outfloppy);
170void floppy_close(floppy_image *floppy);
171
172/* useful for identifying a floppy image */
173floperr_t floppy_identify(void *fp, const struct io_procs *procs, const char *extension,
174   const struct FloppyFormat *formats, int *identified_format);
175
176/* functions useful within format constructors */
177void *floppy_tag(floppy_image *floppy);
178void *floppy_create_tag(floppy_image *floppy, size_t tagsize);
179struct FloppyCallbacks *floppy_callbacks(floppy_image *floppy);
180UINT8 floppy_get_filler(floppy_image *floppy);
181void floppy_set_filler(floppy_image *floppy, UINT8 filler);
182
183/* calls for accessing disk image data */
184floperr_t floppy_read_sector(floppy_image *floppy, int head, int track, int sector, int offset, void *buffer, size_t buffer_len);
185floperr_t floppy_write_sector(floppy_image *floppy, int head, int track, int sector, int offset, const void *buffer, size_t buffer_len, int ddam);
186floperr_t floppy_read_indexed_sector(floppy_image *floppy, int head, int track, int sector_index, int offset, void *buffer, size_t buffer_len);
187floperr_t floppy_write_indexed_sector(floppy_image *floppy, int head, int track, int sector_index, int offset, const void *buffer, size_t buffer_len, int ddam);
188floperr_t floppy_read_track(floppy_image *floppy, int head, int track, void *buffer, size_t buffer_len);
189floperr_t floppy_write_track(floppy_image *floppy, int head, int track, const void *buffer, size_t buffer_len);
190floperr_t floppy_read_track_data(floppy_image *floppy, int head, int track, void *buffer, size_t buffer_len);
191floperr_t floppy_write_track_data(floppy_image *floppy, int head, int track, const void *buffer, size_t buffer_len);
192floperr_t floppy_format_track(floppy_image *floppy, int head, int track, option_resolution *params);
193int floppy_get_tracks_per_disk(floppy_image *floppy);
194int floppy_get_heads_per_disk(floppy_image *floppy);
195UINT32 floppy_get_track_size(floppy_image *floppy, int head, int track);
196floperr_t floppy_get_sector_length(floppy_image *floppy, int head, int track, int sector, UINT32 *sector_length);
197floperr_t floppy_get_indexed_sector_info(floppy_image *floppy, int head, int track, int sector_index, int *cylinder, int *side, int *sector, UINT32 *sector_length, unsigned long *flags);
198floperr_t floppy_get_sector_count(floppy_image *floppy, int head, int track, int *sector_count);
199floperr_t floppy_load_track(floppy_image *floppy, int head, int track, int dirtify, void **track_data, size_t *track_length);
200int floppy_is_read_only(floppy_image *floppy);
201UINT8 floppy_random_byte(floppy_image *floppy);
202
203/* accessors for meta information about the image */
204const char *floppy_format_description(floppy_image *floppy);
205
206/* calls for accessing the raw disk image */
207void floppy_image_read(floppy_image *floppy, void *buffer, UINT64 offset, size_t length);
208void floppy_image_write(floppy_image *floppy, const void *buffer, UINT64 offset, size_t length);
209void floppy_image_write_filler(floppy_image *floppy, UINT8 filler, UINT64 offset, size_t length);
210UINT64 floppy_image_size(floppy_image *floppy);
211
212/* misc */
213const char *floppy_error(floperr_t err);
214
215
216#endif /* FLOPIMG_H */
217
src/lib/formats/apridisk.c
r0r11458
1/***************************************************************************
2
3    APRIDISK disk image format
4
5***************************************************************************/
6
7#include "apridisk.h"
8#include "imageutl.h"
9
10/***************************************************************************
11    CONSTANTS
12***************************************************************************/
13
14#define APR_HEADER_SIZE      128
15
16/* sector types */
17#define APR_DELETED      0xe31d0000
18#define APR_MAGIC      0xe31d0001
19#define APR_COMMENT      0xe31d0002
20#define APR_CREATOR      0xe31d0003
21
22/* compression type */
23#define APR_UNCOMPRESSED   0x9e90
24#define APR_COMPRESSED      0x3e5a
25
26static const char *apr_magic = "ACT Apricot disk image\x1a\x04";
27
28
29/***************************************************************************
30    TYPE DEFINITIONS
31***************************************************************************/
32
33struct apr_sector
34{
35   int head;
36   int track;
37   int sector;
38   UINT8 data[512];
39};
40
41struct apr_tag
42{
43   int heads;
44   int tracks;
45   int sectors_per_track;
46   struct apr_sector sectors[2880];
47   char *comment;
48};
49
50
51/***************************************************************************
52    IMPLEMENTATION
53***************************************************************************/
54
55/* sector format:
56 *
57 * 00-03: sector type
58 * 04-05: compression type
59 * 06-07: header size
60 * 08-11: data size
61 * 12-13: track
62 *    14: head
63 *    15: sector
64 */
65
66static floperr_t apr_read_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen)
67{
68   struct apr_tag *tag = (apr_tag *)floppy_tag(floppy);
69//  printf("apr_read_sector %d %d %d\n", head, track, sector);
70   memcpy(buffer, tag->sectors[head * track * sector].data, buflen);
71   return FLOPPY_ERROR_SUCCESS;
72}
73
74static floperr_t apr_read_indexed_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen)
75{
76   struct apr_tag *tag = (apr_tag *)floppy_tag(floppy);
77//  printf("apr_read_indexed_sector %d %d %d\n", head, track, sector);
78   memcpy(buffer, tag->sectors[head * track * sector].data, buflen);
79   return FLOPPY_ERROR_SUCCESS;
80}
81
82/* sector length is always 512 byte */
83static floperr_t apr_get_sector_length(floppy_image *floppy, int head, int track, int sector, UINT32 *sector_length)
84{
85   *sector_length = 512;
86   return FLOPPY_ERROR_SUCCESS;
87}
88
89static int apr_get_heads_per_disk(floppy_image *floppy)
90{
91   struct apr_tag *tag = (apr_tag *)floppy_tag(floppy);
92   return tag->heads;
93}
94
95static int apr_get_tracks_per_disk(floppy_image *floppy)
96{
97   struct apr_tag *tag = (apr_tag *)floppy_tag(floppy);
98   return tag->tracks;
99}
100
101static floperr_t apr_get_indexed_sector_info(floppy_image *floppy, int head, int track, int sector_index, int *cylinder, int *side, int *sector, UINT32 *sector_length, unsigned long *flags)
102{
103   struct apr_tag *tag = (apr_tag *)floppy_tag(floppy);
104
105//  printf("apr_get_indexed_sector_info %d %d %d\n", head, track, sector_index);
106
107   /* sanity checks */
108   if (head         < 0 || head         > (tag->heads - 1))             return FLOPPY_ERROR_SEEKERROR;
109   if (track        < 0 || track        > (tag->tracks - 1))            return FLOPPY_ERROR_SEEKERROR;
110   if (sector_index < 0 || sector_index > (tag->sectors_per_track - 1)) return FLOPPY_ERROR_SEEKERROR;
111
112   if (cylinder)      *cylinder = tag->sectors[head * track * sector_index].track;
113   if (side)          *side = tag->sectors[head * track * sector_index].head;
114   if (sector)        *sector = tag->sectors[head * track * sector_index].sector;
115   if (sector_length) *sector_length = 512;
116
117   return FLOPPY_ERROR_SUCCESS;
118}
119
120FLOPPY_IDENTIFY( apridisk_identify )
121{
122   UINT8 header[APR_HEADER_SIZE];
123
124   /* get header */
125   floppy_image_read(floppy, &header, 0, sizeof(header));
126
127   /* look for the magic string */
128   if (memcmp(header, apr_magic, sizeof(apr_magic)) == 0)
129      *vote = 100;
130   else
131      *vote = 0;
132
133   return FLOPPY_ERROR_SUCCESS;
134}
135
136FLOPPY_CONSTRUCT( apridisk_construct )
137{
138   struct FloppyCallbacks *callbacks;
139   struct apr_tag *tag;
140
141   int cur_sector = 0;
142   UINT8 sector_header[16];
143   UINT64 pos = 128;
144   UINT32 type;
145
146   tag = (apr_tag *)floppy_create_tag(floppy, sizeof(struct apr_tag));
147
148   if (!tag)
149      return FLOPPY_ERROR_OUTOFMEMORY;
150
151   tag->heads = 0;
152   tag->tracks = 0;
153   tag->sectors_per_track = 0;
154
155   floppy_image_read(floppy, &sector_header, pos, 16);
156   type = pick_integer_le(&sector_header, 0, 4);
157
158   while (type == APR_DELETED || type == APR_MAGIC || type == APR_COMMENT || type == APR_CREATOR)
159   {
160      UINT16 compression = pick_integer_le(&sector_header, 4, 2);
161      UINT16 header_size = pick_integer_le(&sector_header, 6, 2);
162      UINT32 data_size = pick_integer_le(&sector_header, 8, 4);
163
164      tag->sectors[cur_sector].head = pick_integer_le(&sector_header, 12, 1);
165      tag->sectors[cur_sector].sector = pick_integer_le(&sector_header, 13, 1);
166      tag->sectors[cur_sector].track = pick_integer_le(&sector_header, 14, 1);
167
168      pos += header_size;
169
170      if (type == APR_MAGIC)
171      {
172         if (compression == APR_UNCOMPRESSED)
173         {
174            floppy_image_read(floppy, &tag->sectors[cur_sector].data, pos, data_size);
175
176         }
177         else if (compression == APR_COMPRESSED)
178         {
179            UINT8 *buffer = (UINT8 *)osd_malloc(data_size * sizeof(UINT8));
180            UINT16 length;
181            UINT8 value;
182
183            floppy_image_read(floppy, buffer, pos, data_size);
184
185            length = pick_integer_le(buffer, 0, 2);
186            value = pick_integer_le(buffer, 2, 1);
187
188            osd_free(buffer);
189
190            /* not sure if this is possible */
191            if (length != 512) {
192               printf("Compression unsupported");
193               exit(-1);
194            }
195
196            memset(&tag->sectors[cur_sector].data, value, length);
197         }
198         else
199            return FLOPPY_ERROR_INVALIDIMAGE;
200
201         tag->heads = MAX(tag->sectors[cur_sector].head, tag->heads);
202         tag->tracks = MAX(tag->sectors[cur_sector].track, tag->tracks);
203         tag->sectors_per_track = MAX(tag->sectors[cur_sector].sector, tag->sectors_per_track);
204      }
205
206      /* seek to next sector */
207      pos += data_size;
208      cur_sector++;
209
210      floppy_image_read(floppy, &sector_header, pos, 16);
211      type = pick_integer_le(&sector_header, 0, 4);
212   }
213
214   tag->heads++;
215   tag->tracks++;
216
217   callbacks = floppy_callbacks(floppy);
218   callbacks->read_sector = apr_read_sector;
219   callbacks->read_indexed_sector = apr_read_indexed_sector;
220   callbacks->get_sector_length = apr_get_sector_length;
221   callbacks->get_heads_per_disk = apr_get_heads_per_disk;
222   callbacks->get_tracks_per_disk = apr_get_tracks_per_disk;
223   callbacks->get_indexed_sector_info = apr_get_indexed_sector_info;
224
225   return FLOPPY_ERROR_SUCCESS;
226}
src/lib/formats/msx_dsk.c
r0r11458
1/*********************************************************************
2
3    formats/msx_dsk.c
4
5    MSX disk images
6
7*********************************************************************/
8
9#include "msx_dsk.h"
10#include "formats/basicdsk.h"
11
12FLOPPY_OPTIONS_START(msx)
13   FLOPPY_OPTION(msx, "dsk", "MSX SS", basicdsk_identify_default, basicdsk_construct_default, NULL,
14      HEADS([1])
15      TRACKS([80])
16      SECTORS([9])
17      SECTOR_LENGTH([512])
18      FIRST_SECTOR_ID([1]))
19   FLOPPY_OPTION(msx, "dsk", "MSX DS", basicdsk_identify_default, basicdsk_construct_default, NULL,
20      HEADS([2])
21      TRACKS([80])
22      SECTORS([9])
23      SECTOR_LENGTH([512])
24      FIRST_SECTOR_ID([1]))
25FLOPPY_OPTIONS_END
src/lib/formats/ap_dsk35.c
r0r11458
1/*********************************************************************
2
3    ap_dsk35.c
4
5    Apple 3.5" disk images
6
7    This code supports 3.5" 400k/800k disks used in early Macintoshes
8    and the Apple IIgs, and 3.5" 1440k MFM disks used on most Macintoshes.
9
10    These disks have the following properties:
11
12        400k:   80 tracks, 1 head
13        800k:   80 tracks, 2 heads
14
15        Tracks  0-15 have 12 sectors each
16        Tracks 16-31 have 11 sectors each
17        Tracks 32-47 have 10 sectors each
18        Tracks 48-63 have  9 sectors each
19        Tracks 64-79 have  8 sectors each
20
21        1440k disks are simply 80 tracks, 2 heads and 18 sectors per track.
22
23    Each sector on 400/800k disks has 524 bytes, 512 of which are really used by the Macintosh
24
25    (80 tracks) * (avg of 10 sectors) * (512 bytes) * (2 sides) = 800 kB
26
27    Data is nibblized : 3 data bytes -> 4 bytes on disk.
28
29    In addition to 512 logical bytes, each sector contains 800 physical
30    bytes.  Here is the layout of the physical sector:
31
32        Pos
33        0       0xFF (pad byte where head is turned on ???)
34        1-35    self synch 0xFFs (7*5) (42 bytes actually written to media)
35        36      0xD5
36        37      0xAA
37        38      0x96
38        39      diskbytes[(track number) & 0x3F]
39        40      diskbytes[(sector number)]
40        41      diskbytes[("side")]
41        42      diskbytes[("format byte")]
42        43      diskbytes[("sum")]
43        44      0xDE
44        45      0xAA
45        46      pad byte where head is turned off/on (0xFF here)
46        47-51   self synch 0xFFs (6 bytes actually written to media)
47        52      0xD5
48        53      0xAA
49        54      0xAD
50        55      spare byte, generally diskbytes[(sector number)]
51        56-754  "nibblized" sector data ...
52        755-758 checksum
53        759     0xDE
54        760     0xAA
55        761     pad byte where head is turned off (0xFF here)
56
57    MFM Track layout for 1440K disks:
58        Pos
59    --------- track ID
60    0   0x4E (x80)
61    80  00 (x12)
62    92  C2 (x3) Mark byte
63    93  FC Index mark
64    94  4E (x50)
65    --------- sector ID
66    144 00 (x12)
67    156 A1 (x3) Mark byte
68    159 FE Address mark
69    160 xx Track number
70    161 xx Side number
71    162 xx Sector number
72    163 xx Sector length
73    164/165 xx CRC
74    166 4E (x22)
75    --------- sector data
76    188 00 (x12)
77    200 A1 (x3) Mark byte
78    203 FB Data address mark
79    204 xx (x256) data
80    460 4E (x54)
81    (repeat from sector ID to fill track; end of track is padded with 4E)
82
83    Note : "Self synch refers to a technique whereby two zeroes are inserted
84    between each synch byte written to the disk.", i.e. "0xFF, 0xFF, 0xFF,
85    0xFF, 0xFF" is actually "0xFF, 0x3F, 0xCF, 0xF3, 0xFC, 0xFF" on disk.
86    Since the IWM assumes the data transfer is complete when the MSBit of its
87    shift register is 1, we do read 4 0xFF, even though they are not
88    contiguous on the disk media.  Some reflexion shows that 4 synch bytes
89    allow the IWM to synchronize with the trailing data.
90
91    Format byte codes:
92        0x00    Apple II
93        0x01    Lisa
94        0x02    Mac MFS (single sided)?
95        0x22    Mac MFS (double sided)?
96
97*********************************************************************/
98
99#include <stdio.h>
100#include <assert.h>
101
102#include "formats/ap_dsk35.h"
103
104struct apple35_tag
105{
106   UINT32 data_offset;
107   UINT32 data_size;
108   UINT8 format_byte;
109   UINT8 sides;
110   unsigned int is_1440k : 1;
111
112   /* stuff used in DiskCopy images */
113   UINT32 tag_offset;
114   UINT32 tag_size;
115};
116
117
118
119/* normal number of sector for each track */
120static const UINT8 apple35_tracklen_800kb[80] =
121{
122   12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
123   11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
124   10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
125    9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,
126    8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8
127};
128
129/* blocks of data used to nibblize tracks */
130static const UINT8 diskbytes[] =
131{
132   0x96, 0x97, 0x9A, 0x9B,  0x9D, 0x9E, 0x9F, 0xA6, /* 0x00 */
133   0xA7, 0xAB, 0xAC, 0xAD,  0xAE, 0xAF, 0xB2, 0xB3,
134   0xB4, 0xB5, 0xB6, 0xB7,  0xB9, 0xBA, 0xBB, 0xBC, /* 0x10 */
135   0xBD, 0xBE, 0xBF, 0xCB,  0xCD, 0xCE, 0xCF, 0xD3,
136   0xD6, 0xD7, 0xD9, 0xDA,  0xDB, 0xDC, 0xDD, 0xDE, /* 0x20 */
137   0xDF, 0xE5, 0xE6, 0xE7,  0xE9, 0xEA, 0xEB, 0xEC,
138   0xED, 0xEE, 0xEF, 0xF2,  0xF3, 0xF4, 0xF5, 0xF6, /* 0x30 */
139   0xF7, 0xF9, 0xFA, 0xFB,  0xFC, 0xFD, 0xFE, 0xFF
140};
141
142/* reverse lookup of diskbytes */
143static const INT16 rev_diskbytes[] =
144{
145   -1,   -1,   -1,   -1,    -1,   -1,   -1,   -1,   /* 0x00 */
146   -1,   -1,   -1,   -1,    -1,   -1,   -1,   -1,
147   -1,   -1,   -1,   -1,    -1,   -1,   -1,   -1,   /* 0x10 */
148   -1,   -1,   -1,   -1,    -1,   -1,   -1,   -1,
149   -1,   -1,   -1,   -1,    -1,   -1,   -1,   -1,   /* 0x20 */
150   -1,   -1,   -1,   -1,    -1,   -1,   -1,   -1,
151   -1,   -1,   -1,   -1,    -1,   -1,   -1,   -1,   /* 0x30 */
152   -1,   -1,   -1,   -1,    -1,   -1,   -1,   -1,
153   -1,   -1,   -1,   -1,    -1,   -1,   -1,   -1,   /* 0x40 */
154   -1,   -1,   -1,   -1,    -1,   -1,   -1,   -1,
155   -1,   -1,   -1,   -1,    -1,   -1,   -1,   -1,   /* 0x50 */
156   -1,   -1,   -1,   -1,    -1,   -1,   -1,   -1,
157   -1,   -1,   -1,   -1,    -1,   -1,   -1,   -1,   /* 0x60 */
158   -1,   -1,   -1,   -1,    -1,   -1,   -1,   -1,
159   -1,   -1,   -1,   -1,    -1,   -1,   -1,   -1,   /* 0x70 */
160   -1,   -1,   -1,   -1,    -1,   -1,   -1,   -1,
161   -1,   -1,   -1,   -1,    -1,   -1,   -1,   -1,   /* 0x80 */
162   -1,   -1,   -1,   -1,    -1,   -1,   -1,   -1,
163   -1,   -1,   -1,   -1,    -1,   -1,   0x00, 0x01, /* 0x90 */
164   -1,   -1,   0x02, 0x03,  -1,   0x04, 0x05, 0x06,
165   -1,   -1,   -1,   -1,    -1,   -1,   0x07, 0x08, /* 0xA0 */
166   -1,   -1,   -1,   0x09,  0x0A, 0x0B, 0x0C, 0x0D,
167   -1,   -1,   0x0E, 0x0F,  0x10, 0x11, 0x12, 0x13, /* 0xB0 */
168   -1,   0x14, 0x15, 0x16,  0x17, 0x18, 0x19, 0x1A,
169   -1,   -1,   -1,   -1,    -1,   -1,   -1,   -1,   /* 0xC0 */
170   -1,   -1,   -1,   0x1B,  -1,   0x1C, 0x1D, 0x1E,
171   -1,   -1,   -1,   0x1F,  -1,   -1,   0x20, 0x21, /* 0xD0 */
172   -1,   0x22, 0x23, 0x24,  0x25, 0x26, 0x27, 0x28,
173   -1,   -1,   -1,   -1,    -1,   0x29, 0x2A, 0x2B, /* 0xE0 */
174   -1,   0x2C, 0x2D, 0x2E,  0x2F, 0x30, 0x31, 0x32,
175   -1,   -1,   0x33, 0x34,  0x35, 0x36, 0x37, 0x38, /* 0xF0 */
176   -1,   0x39, 0x3A, 0x3B,  0x3C, 0x3D, 0x3E, 0x3F
177};
178
179static const UINT8 blk1[] =
180{
181   /*0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
182    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
183    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
184    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
185    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
186    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
187    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
188    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
189    0xFF, 0xFF, 0xD5, 0xAA, 0x96*/
190   0xFF,
191   0xFF, 0x3F, 0xCF, 0xF3, 0xFC, 0xFF,
192   0xFF, 0x3F, 0xCF, 0xF3, 0xFC, 0xFF,
193   0xFF, 0x3F, 0xCF, 0xF3, 0xFC, 0xFF,
194   0xFF, 0x3F, 0xCF, 0xF3, 0xFC, 0xFF,
195   0xFF, 0x3F, 0xCF, 0xF3, 0xFC, 0xFF,
196   0xFF, 0x3F, 0xCF, 0xF3, 0xFC, 0xFF,
197   0xFF, 0x3F, 0xCF, 0xF3, 0xFC, 0xFF,
198   0xD5, 0xAA, 0x96
199};
200static const UINT8 blk2[] =
201{
202   0xDE, 0xAA, 0xFF, 0xFF, 0x3F, 0xCF, 0xF3, 0xFC, 0xFF, 0xD5, 0xAA, 0xAD
203};
204static const UINT8 blk3[] =
205{
206   0xDE, 0xAA, 0xFF
207};
208
209
210
211static struct apple35_tag *get_apple35_tag(floppy_image *floppy)
212{
213   struct apple35_tag *tag;
214   tag = (struct apple35_tag *) floppy_tag(floppy);
215   return tag;
216}
217
218
219
220int apple35_sectors_per_track(floppy_image *image, int track)
221{
222   int sectors;
223
224   assert(track >= 0);
225   assert(track < ARRAY_LENGTH(apple35_tracklen_800kb));
226
227   if (get_apple35_tag(image)->is_1440k)
228      sectors = 18;
229   else
230      sectors = apple35_tracklen_800kb[track];
231   return sectors;
232}
233
234
235
236/*
237    converts data to its nibblized representation, and generate checksum
238
239    now handles 524-byte-long sectors
240
241    tag data IS important, since it allows data recovery when the catalog is trashed
242*/
243static void sony_nibblize35(const UINT8 *in, UINT8 *nib_ptr, UINT8 *csum)
244{
245   int   i, j;
246   UINT32 c1, c2, c3, c4;
247   UINT8 val;
248   UINT8 w1, w2, w3, w4;
249   UINT8 b1[175], b2[175], b3[175];
250
251   /* Copy from the user's buffer to our buffer, while computing
252     * the three-byte data checksum
253     */
254
255   i = 0;
256   j = 0;
257   c1 = 0;
258   c2 = 0;
259   c3 = 0;
260   while (1)
261   {
262      c1 = (c1 & 0xFF) << 1;
263      if (c1 & 0x0100)
264         c1++;
265
266      val = in[i++];
267      c3 += val;
268      if (c1 & 0x0100)
269      {
270         c3++;
271         c1 &= 0xFF;
272      }
273      b1[j] = (val ^ c1) & 0xFF;
274
275      val = in[i++];
276      c2 += val;
277      if (c3 > 0xFF)
278      {
279         c2++;
280         c3 &= 0xFF;
281      }
282      b2[j] = (val ^ c3) & 0xFF;
283
284      if (i == 524) break;
285
286      val = in[i++];
287      c1 += val;
288      if (c2 > 0xFF)
289      {
290         c1++;
291         c2 &= 0xFF;
292      }
293      b3[j] = (val ^ c2) & 0xFF;
294      j++;
295   }
296   c4 =  ((c1 & 0xC0) >> 6) | ((c2 & 0xC0) >> 4) | ((c3 & 0xC0) >> 2);
297   b3[174] = 0;
298
299   j = 0;
300   for (i = 0; i <= 174; i++)
301   {
302      w1 = b1[i] & 0x3F;
303      w2 = b2[i] & 0x3F;
304      w3 = b3[i] & 0x3F;
305      w4 =  ((b1[i] & 0xC0) >> 2);
306      w4 |= ((b2[i] & 0xC0) >> 4);
307      w4 |= ((b3[i] & 0xC0) >> 6);
308
309      nib_ptr[j++] = w4;
310      nib_ptr[j++] = w1;
311      nib_ptr[j++] = w2;
312
313      if (i != 174) nib_ptr[j++] = w3;
314   }
315
316   csum[0] = c1 & 0x3F;
317   csum[1] = c2 & 0x3F;
318   csum[2] = c3 & 0x3F;
319   csum[3] = c4 & 0x3F;
320}
321
322
323
324/*
325    does the reverse process of sony_nibblize35
326*/
327static void sony_denibblize35(UINT8 *out, const UINT8 *nib_ptr, UINT8 *checksum)
328{
329   int   i, j;
330   UINT32 c1,c2,c3,c4;
331   UINT8 val;
332   UINT8 w1,w2,w3=0,w4;
333   UINT8 b1[175],b2[175],b3[175];
334
335   j = 0;
336   for (i=0; i<=174; i++)
337   {
338      w4 = nib_ptr[j++];
339      w1 = nib_ptr[j++];
340      w2 = nib_ptr[j++];
341
342      if (i != 174) w3 = nib_ptr[j++];
343
344      b1[i] = (w1 & 0x3F) | ((w4 << 2) & 0xC0);
345      b2[i] = (w2 & 0x3F) | ((w4 << 4) & 0xC0);
346      b3[i] = (w3 & 0x3F) | ((w4 << 6) & 0xC0);
347   }
348
349   /* Copy from the user's buffer to our buffer, while computing
350     * the three-byte data checksum
351     */
352
353   i = 0;
354   j = 0;
355   c1 = 0;
356   c2 = 0;
357   c3 = 0;
358   while (1)
359   {
360      c1 = (c1 & 0xFF) << 1;
361      if (c1 & 0x0100)
362         c1++;
363
364      val = (b1[j] ^ c1) & 0xFF;
365      c3 += val;
366      if (c1 & 0x0100)
367      {
368         c3++;
369         c1 &= 0xFF;
370      }
371      out[i++] = val;
372
373      val = (b2[j] ^ c3) & 0xFF;
374      c2 += val;
375      if (c3 > 0xFF)
376      {
377         c2++;
378         c3 &= 0xFF;
379      }
380      out[i++] = val;
381
382      if (i == 524) break;
383
384      val = (b3[j] ^ c2) & 0xFF;
385      c1 += val;
386      if (c2 > 0xFF)
387      {
388         c1++;
389         c2 &= 0xFF;
390      }
391      out[i++] = val;
392      j++;
393   }
394   c4 =  ((c1 & 0xC0) >> 6) | ((c2 & 0xC0) >> 4) | ((c3 & 0xC0) >> 2);
395   b3[174] = 0;
396
397   checksum[0] = c1 & 0x3F;
398   checksum[1] = c2 & 0x3F;
399   checksum[2] = c3 & 0x3F;
400   checksum[3] = c4 & 0x3F;
401}
402
403
404
405void sony_filltrack(UINT8 *buffer, size_t buffer_len, size_t *pos, UINT8 data)
406{
407   buffer[*pos / 8] &= 0xFF << (8 - (*pos % 8));
408   buffer[*pos / 8] |= data >> (*pos % 8);
409
410   *pos += 8;
411   *pos %= buffer_len * 8;
412
413   buffer[*pos / 8] &= 0xFF >> (*pos % 8);
414   buffer[*pos / 8] |= data << (8 - (*pos % 8));
415}
416
417
418
419UINT8 sony_fetchtrack(const UINT8 *buffer, size_t buffer_len, size_t *pos)
420{
421   UINT8 data;
422
423   data = buffer[*pos / 8] << (*pos % 8);
424   *pos += 8;
425   *pos %= (buffer_len * 8);
426   data |= buffer[*pos / 8] >> (8 - (*pos % 8));
427
428   while ((data & 0x80) == 0)
429   {
430      /* this code looks weird because it isn't simply rotating the new bit
431         * in, but for some reason it won't work if I rotate the bit in; I
432         * have to match the algorithm used by the old code */
433      data <<= 1;
434      data |= (buffer[*pos / 8] >> (8 - ((*pos % 8) + 1)));
435      (*pos)++;
436      *pos %= (buffer_len * 8);
437   }
438   return data;
439}
440
441
442
443static UINT32 apple35_get_offset(floppy_image *floppy, int head, int track, int sector, UINT32 *tag_offset)
444{
445   int i;
446   UINT32 sector_index = 0;
447   struct apple35_tag *tag;
448
449   tag = get_apple35_tag(floppy);
450
451   if (track >= ARRAY_LENGTH(apple35_tracklen_800kb))
452      return ~0;
453   if (head >= tag->sides)
454      return ~0;
455   if (sector >= apple35_sectors_per_track(floppy, track))
456      return ~0;
457
458   for (i = 0; i < track; i++)
459      sector_index += apple35_sectors_per_track(floppy, i);
460   sector_index *= tag->sides;
461   if (head)
462      sector_index += apple35_sectors_per_track(floppy, i);
463   sector_index += sector;
464
465   if (tag_offset)
466   {
467      *tag_offset = sector_index * 12 + tag->tag_offset;
468      if (*tag_offset >= tag->tag_size)
469         *tag_offset = ~0;
470   }
471   return sector_index * 0x200 + tag->data_offset;
472}
473
474
475
476static floperr_t apple35_read_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen)
477{
478   UINT32 data_offset;
479//  printf("Read sector: T %d S %d H %d\n", track, sector, head);
480   data_offset = apple35_get_offset(floppy, head, track, sector, NULL);
481   if (data_offset == ~0)
482      return FLOPPY_ERROR_SEEKERROR;
483   floppy_image_read(floppy, buffer, data_offset, buflen);
484   return FLOPPY_ERROR_SUCCESS;
485}
486
487
488
489static floperr_t apple35_write_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam)
490{
491   UINT32 data_offset;
492
493   data_offset = apple35_get_offset(floppy, head, track, sector, NULL);
494   if (data_offset == ~0)
495      return FLOPPY_ERROR_SEEKERROR;
496   floppy_image_write(floppy, buffer, data_offset, buflen);
497   return FLOPPY_ERROR_SUCCESS;
498}
499
500
501
502static floperr_t apple35_read_sector_td(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen)
503{
504   floperr_t err;
505   UINT32 tag_offset = 0;
506
507   assert(buflen == 524);
508
509//  printf("Read sector TD: T %d S %d H %d\n", track, sector, head);
510
511   /* first read the sector */
512   err = apple35_read_sector(floppy, head, track, sector, ((UINT8 *) buffer) + 12, 512);
513   if (err)
514      return err;
515
516   /* read the tag data, if possible */
517   memset(buffer, '\0', 12);
518   apple35_get_offset(floppy, head, track, sector, &tag_offset);
519   if (tag_offset != ~0)
520      floppy_image_read(floppy, buffer, tag_offset, 12);
521
522   return FLOPPY_ERROR_SUCCESS;
523}
524
525
526
527static floperr_t apple35_write_sector_td(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam)
528{
529   floperr_t err;
530   UINT32 tag_offset = 0;
531
532   assert(buflen == 524);
533
534   /* first write the sector */
535   err = apple35_write_sector(floppy, head, track, sector, ((const UINT8 *) buffer) + 12, 512, 0);
536   if (err)
537      return err;
538
539   /* write the tag data, if possible */
540   apple35_get_offset(floppy, head, track, sector, &tag_offset);
541   if (tag_offset != ~0)
542      floppy_image_write(floppy, buffer, tag_offset, 12);
543
544   return FLOPPY_ERROR_SUCCESS;
545}
546
547
548
549static floperr_t apple35_get_sector_length(floppy_image *floppy, int head, int track, int sector, UINT32 *sector_length)
550{
551   *sector_length = 512;
552   return FLOPPY_ERROR_SUCCESS;
553}
554
555
556
557static int apple35_get_heads_per_disk(floppy_image *floppy)
558{
559   return get_apple35_tag(floppy)->sides;
560}
561
562
563
564static int apple35_get_tracks_per_disk(floppy_image *floppy)
565{
566   return 80;
567}
568
569
570
571static UINT32 apple35_get_track_size(floppy_image *floppy, int head, int track)
572{
573   if ((track < 0) || (track >= 80))
574      return 0;
575   return apple35_sectors_per_track(floppy, track) * 800;
576}
577
578
579
580static UINT8 calculate_side(int head, int track)
581{
582   UINT8 side;
583   side = head ? 0x20 : 0x00;
584   if (track & 0x40)
585      side |= 0x01;
586   return side;
587}
588
589
590
591static floperr_t apple35_read_track(floppy_image *floppy, int head, int track, UINT64 offset, void *buffer, size_t buflen)
592{
593   floperr_t err;
594   size_t pos = 0;
595   int sector_count, sector, i;
596   UINT8 sum, side;
597   struct apple35_tag *tag;
598   UINT8 sector_data[524];
599   UINT8 nibble_data[699];
600   UINT8 checksum[4];
601
602   tag = get_apple35_tag(floppy);
603
604   if (track >= ARRAY_LENGTH(apple35_tracklen_800kb))
605      return FLOPPY_ERROR_SEEKERROR;
606   if (offset != 0)
607      return FLOPPY_ERROR_UNSUPPORTED;
608
609   memset(buffer, 0xFF, buflen);
610   sector_count = apple35_sectors_per_track(floppy, track);
611   side = calculate_side(head, track);
612
613   for (sector = 0; sector < sector_count; sector++)
614   {
615      /* read the sector */
616      err = apple35_read_sector_td(floppy, head, track, sector, sector_data, ARRAY_LENGTH(sector_data));
617      if (err)
618         return err;
619      sony_nibblize35(sector_data, nibble_data, checksum);
620
621      for (i = 0; i < ARRAY_LENGTH(blk1); i++)
622         sony_filltrack((UINT8*)buffer, buflen, &pos, blk1[i]);
623
624      sum = (track ^ sector ^ side ^ tag->format_byte) & 0x3F;
625
626      sony_filltrack((UINT8*)buffer, buflen, &pos, diskbytes[track & 0x3f]);
627      sony_filltrack((UINT8*)buffer, buflen, &pos, diskbytes[sector]);
628      sony_filltrack((UINT8*)buffer, buflen, &pos, diskbytes[side]);
629      sony_filltrack((UINT8*)buffer, buflen, &pos, diskbytes[tag->format_byte]);
630      sony_filltrack((UINT8*)buffer, buflen, &pos, diskbytes[sum]);
631
632      for (i = 0; i < ARRAY_LENGTH(blk2); i++)
633         sony_filltrack((UINT8*)buffer, buflen, &pos, blk2[i]);
634
635      sony_filltrack((UINT8*)buffer, buflen, &pos, diskbytes[sector]);
636
637      for (i = 0; i < ARRAY_LENGTH(nibble_data); i++)
638         sony_filltrack((UINT8*)buffer, buflen, &pos, diskbytes[nibble_data[i]]);
639
640      for (i = 3; i >= 0; i--)
641         sony_filltrack((UINT8*)buffer, buflen, &pos, diskbytes[checksum[i]]);
642
643      for (i = 0; i < ARRAY_LENGTH(blk3); i++)
644         sony_filltrack((UINT8*)buffer, buflen, &pos, blk3[i]);
645   }
646   return FLOPPY_ERROR_SUCCESS;
647}
648
649
650
651static floperr_t apple35_write_track(floppy_image *floppy, int head, int track, UINT64 offset, const void *buffer, size_t buflen)
652{
653   floperr_t err;
654   size_t pos = 0;
655   int sector_count, sector, i, j;
656   struct apple35_tag *tag;
657   UINT8 sum, format_byte, val, side;
658   UINT32 found_sectors = 0;
659   UINT8 sector_data[524];
660   UINT8 nibble_data[699];
661   UINT8 checksum[4];
662
663   tag = get_apple35_tag(floppy);
664
665   if (track >= ARRAY_LENGTH(apple35_tracklen_800kb))
666      return FLOPPY_ERROR_SEEKERROR;
667   if (offset != 0)
668      return FLOPPY_ERROR_UNSUPPORTED;
669
670   sector_count = apple35_sectors_per_track(floppy, track);
671   side = calculate_side(head, track);
672
673   /* do 2 rotations, in case the bit slip stuff prevent us to read the first sector */
674   for (j = 0; j < (buflen * 2); j++)
675   {
676      if (sony_fetchtrack((UINT8*)buffer, buflen, &pos) != 0xD5)
677         continue;
678      j++;
679
680      if (sony_fetchtrack((UINT8*)buffer, buflen, &pos) != 0xAA)
681         continue;
682      j++;
683
684      if (sony_fetchtrack((UINT8*)buffer, buflen, &pos) != 0x96)
685         continue;
686      j++;
687
688      if (rev_diskbytes[sony_fetchtrack((UINT8*)buffer, buflen, &pos)] != (track & 0x3F))
689         continue;
690      j++;
691
692      sector = rev_diskbytes[sony_fetchtrack((UINT8*)buffer, buflen, &pos)];
693      if ((sector < 0) || (sector >= sector_count))
694         continue;
695      j++;
696
697      if (rev_diskbytes[sony_fetchtrack((UINT8*)buffer, buflen, &pos)] != side)
698         continue;
699      j++;
700
701      format_byte = rev_diskbytes[sony_fetchtrack((UINT8*)buffer, buflen, &pos)];
702      if (format_byte != tag->format_byte)
703      {
704         /* this is an error, but not THAT critical, I guess */
705      }
706      j++;
707
708      sum = track ^ sector ^ side ^ format_byte;
709      if (rev_diskbytes[sony_fetchtrack((UINT8*)buffer, buflen, &pos)] != sum)
710         continue;
711      j++;
712
713      if (sony_fetchtrack((UINT8*)buffer, buflen, &pos) != 0xDE)
714         continue;
715      j++;
716
717      if (sony_fetchtrack((UINT8*)buffer, buflen, &pos) != 0xAA)
718         continue;
719      j++;
720
721      while((val = sony_fetchtrack((UINT8*)buffer, buflen, &pos)) == 0xFF)
722         j++;
723      if (val != 0xD5)
724         continue;   /* lost bit slip mark! */
725      j++;
726
727      if (sony_fetchtrack((UINT8*)buffer, buflen, &pos) != 0xAA)
728         continue;
729      j++;
730
731      if (sony_fetchtrack((UINT8*)buffer, buflen, &pos) != 0xAD)
732         continue;
733      j++;
734
735      /* should this be regarded as a critical error ??? */
736      if (rev_diskbytes[sony_fetchtrack((UINT8*)buffer, buflen, &pos)] != sector)
737         continue;
738      j++;
739
740      for (i = 0; i < ARRAY_LENGTH(nibble_data); i++)
741      {
742         nibble_data[i] = rev_diskbytes[sony_fetchtrack((UINT8*)buffer, buflen, &pos)];
743         j++;
744      }
745
746      for (i = 3; i >= 0; i--)
747      {
748         /* should be checking checksum */
749         sony_fetchtrack((UINT8*)buffer, buflen, &pos);
750      }
751
752      sony_fetchtrack((UINT8*)buffer, buflen, &pos);   /* should get 0xDE */
753      sony_fetchtrack((UINT8*)buffer, buflen, &pos);   /* should get 0xAA */
754      sony_fetchtrack((UINT8*)buffer, buflen, &pos);   /* should get 0xFF */
755
756      /* did we already write this sector? */
757      if ((found_sectors & (1 << sector)) == 0)
758      {
759         sony_denibblize35(sector_data, nibble_data, checksum);
760
761         /* write the sector */
762         err = apple35_write_sector_td(floppy, head, track, sector, sector_data, ARRAY_LENGTH(sector_data), 0);
763         if (err)
764            return err;
765
766         found_sectors |= 1 << sector;
767      }
768   }
769   return FLOPPY_ERROR_SUCCESS;
770}
771
772
773
774static floperr_t apple35_construct(floppy_image *floppy, UINT32 data_offset, UINT32 data_size,
775   UINT32 tag_offset, UINT32 tag_size, INT16 format_byte, UINT8 sides, int is_1440k)
776{
777   struct apple35_tag *tag;
778   struct FloppyCallbacks *format;
779
780   /* figure out format byte if not specified */
781   if (format_byte < 0)
782   {
783      switch(sides)
784      {
785         case 1:
786            format_byte = 0x02;
787            break;
788         case 2:
789            format_byte = 0x22;
790            break;
791         default:
792            return FLOPPY_ERROR_INVALIDIMAGE;
793      }
794   }
795
796   /* create tag */
797   tag = (struct apple35_tag *) floppy_create_tag(floppy, sizeof(struct apple35_tag));
798   if (!tag)
799      return FLOPPY_ERROR_OUTOFMEMORY;
800   tag->data_offset = data_offset;
801   tag->data_size = data_size;
802   tag->tag_offset = tag_offset;
803   tag->tag_size = tag_size;
804   tag->format_byte = (UINT8) format_byte;
805   tag->sides = sides;
806   tag->is_1440k = is_1440k ? 1 : 0;
807
808   /* set up format callbacks */
809   format = floppy_callbacks(floppy);
810   format->read_sector = apple35_read_sector;
811   format->write_sector = apple35_write_sector;
812   format->read_track = apple35_read_track;
813   format->write_track = apple35_write_track;
814   format->get_sector_length = apple35_get_sector_length;
815   format->get_heads_per_disk = apple35_get_heads_per_disk;
816   format->get_tracks_per_disk = apple35_get_tracks_per_disk;
817   format->get_track_size = apple35_get_track_size;
818   return FLOPPY_ERROR_SUCCESS;
819}
820
821
822
823/* ----------------------------------------------------------------------- */
824
825static FLOPPY_IDENTIFY(apple35_raw_identify)
826{
827   UINT64 size;
828   size = floppy_image_size(floppy);
829   *vote = ((size == 80*1*10*512) || (size == 80*2*10*512) || (size == (80*2*18*512)+84)
830      || (size == 80*2*18*512)) ? 100 : 0;
831   return FLOPPY_ERROR_SUCCESS;
832}
833
834
835
836
837static FLOPPY_CONSTRUCT(apple35_raw_construct)
838{
839   UINT64 size;
840   UINT8 sides;
841   int is_1440k;
842
843   if (params)
844   {
845      /* create */
846      sides = option_resolution_lookup_int(params, PARAM_HEADS);
847      size = 80*sides*10*512;
848      is_1440k = FALSE;
849   }
850   else
851   {
852      /* load */
853      size = floppy_image_size(floppy);
854      if (size == 80*1*10*512)
855      {
856         sides = 1;
857         is_1440k = FALSE;
858      }
859      else if ((size == 80*2*10*512) || (size == 80*2*18*512) || (size == (80*2*18*512)+84))
860      {
861         sides = 2;
862         is_1440k = (size == 80*2*18*512) || (size == (80*2*18*512)+84);
863      }
864      else
865         return FLOPPY_ERROR_INVALIDIMAGE;
866   }
867
868   return apple35_construct(floppy, 0, (UINT32) size, 0, 0, -1, sides, is_1440k);
869}
870
871
872
873/* ----------------------------------------------------------------------- */
874
875struct header_diskcopy
876{
877   UINT8 disk_name[64];   /* name of the disk */
878   UINT32 data_size;      /* total size of data for all sectors (512*number_of_sectors) */
879   UINT32 tag_size;      /* total size of tag data for all sectors (12*number_of_sectors for GCR 3.5" floppies, 20*number_of_sectors for HD20, 0 otherwise) */
880   UINT32 data_checksum;   /* CRC32 checksum of all sector data */
881   UINT32 tag_checksum;   /* CRC32 checksum of all tag data */
882   UINT8 disk_format;      /* 0 = 400K, 1 = 800K, 2 = 720K, 3 = 1440K  (other values reserved) */
883   UINT8 format_byte;      /* should be $00 Apple II, $01 Lisa, $02 Mac MFS ??? */
884                     /* $12 = 400K, $22 = >400K Macintosh (DiskCopy uses this value for
885                               all Apple II disks not 800K in size, and even for some of those),
886                               $24 = 800K Apple II disk */
887   UINT16 magic;         /* always $0100 (otherwise, the file may be in a different format. */
888};
889
890
891
892static floperr_t apple35_diskcopy_headerdecode(floppy_image *floppy, UINT32 *data_offset,
893   UINT32 *data_size, UINT32 *tag_offset, UINT32 *tag_size, UINT8 *format_byte, UINT8 *sides)
894{
895   UINT64 size;
896   struct header_diskcopy header;
897
898   if (data_offset)
899      *data_offset = 0;
900   if (data_size)
901      *data_size = 0;
902   if (tag_offset)
903      *tag_offset = 0;
904   if (tag_size)
905      *tag_size = 0;
906   if (format_byte)
907      *format_byte = 0;
908   if (sides)
909      *sides = 0;
910
911   size = floppy_image_size(floppy);
912   if (size < sizeof(struct header_diskcopy))
913      return FLOPPY_ERROR_INVALIDIMAGE;
914
915   floppy_image_read(floppy, &header, 0, sizeof(header));
916
917   header.data_size = BIG_ENDIANIZE_INT32(header.data_size);
918   header.tag_size = BIG_ENDIANIZE_INT32(header.tag_size);
919   header.data_checksum = BIG_ENDIANIZE_INT32(header.data_checksum);
920   header.tag_checksum = BIG_ENDIANIZE_INT32(header.tag_checksum);
921   header.magic = BIG_ENDIANIZE_INT16(header.magic);
922
923   if (header.disk_name[0] >= sizeof(header.disk_name))
924      return FLOPPY_ERROR_INVALIDIMAGE;
925   if (header.magic != 0x0100)
926      return FLOPPY_ERROR_INVALIDIMAGE;
927   if (size != (header.data_size + header.tag_size + sizeof(header)))
928      return FLOPPY_ERROR_INVALIDIMAGE;
929
930   if (header.data_size == 80*1*10*512)
931   {
932      if (sides)
933         *sides = 1;
934   }
935   else if (header.data_size == 80*2*10*512)
936   {
937      if (sides)
938         *sides = 2;
939   }
940   else
941      return FLOPPY_ERROR_INVALIDIMAGE;
942
943   if (data_offset)
944      *data_offset = sizeof(header);
945   if (data_size)
946      *data_size = header.data_size;
947   if (tag_offset)
948      *tag_offset = sizeof(header) + header.data_size;
949   if (tag_size)
950      *tag_size = header.tag_size;
951   if (format_byte)
952      *format_byte = header.format_byte;
953   return FLOPPY_ERROR_SUCCESS;
954}
955
956
957
958static FLOPPY_IDENTIFY(apple35_diskcopy_identify)
959{
960   *vote = apple35_diskcopy_headerdecode(floppy, NULL, NULL, NULL, NULL, NULL, NULL) ? 0 : 100;
961   return FLOPPY_ERROR_SUCCESS;
962}
963
964
965
966
967static FLOPPY_CONSTRUCT(apple35_diskcopy_construct)
968{
969   floperr_t err;
970   UINT8 format_byte, sides;
971   UINT32 data_offset, data_size;
972   UINT32 tag_offset, tag_size;
973   INT16 format_byte_param = -1;
974   struct header_diskcopy header;
975
976   if (params)
977   {
978      /* create */
979      sides = option_resolution_lookup_int(params, PARAM_HEADS);
980
981      data_size = 80*sides*10*512;
982      tag_size = 80*sides*10*12;
983
984      memset(&header, 0, sizeof(header));
985      header.data_size = BIG_ENDIANIZE_INT32(data_size);
986      header.tag_size = BIG_ENDIANIZE_INT32(tag_size);
987      header.disk_format = (sides > 1) ? 1 : 0;
988      header.magic = BIG_ENDIANIZE_INT16(0x100);
989
990      floppy_image_write(floppy, &header, 0, sizeof(header));
991      floppy_image_write_filler(floppy, 0, sizeof(header), data_size + tag_size);
992   }
993
994   /* load */
995   err = apple35_diskcopy_headerdecode(floppy, &data_offset, &data_size,
996      &tag_offset, &tag_size, &format_byte, &sides);
997   if (err)
998      return err;
999
1000   format_byte_param = format_byte;
1001
1002   return apple35_construct(floppy, data_offset, data_size,
1003      tag_offset, tag_size, format_byte_param, sides, FALSE);
1004}
1005
1006
1007
1008/* ----------------------------------------------------------------------- */
1009
1010struct header_2img
1011{
1012   char magic[4];         /* '2IMG' */
1013   char creator[4];      /* signature; 'MESS' for MESS */
1014   UINT16 header_length;
1015   UINT16 version;
1016   UINT32 image_format;
1017   UINT32 flags;
1018   UINT32 block_count;
1019   UINT32 data_offset;
1020   UINT32 data_length;
1021   UINT32 comment_offset;
1022   UINT32 comment_length;
1023   UINT32 creator_offset;
1024   UINT32 creator_length;
1025   UINT32 padding[4];
1026};
1027
1028#define IMAGE_FORMAT_DO      0
1029#define IMAGE_FORMAT_PO      1
1030#define IMAGE_FORMAT_NIB   2
1031#define IMAGE_FLAGS_LOCKED   0x80000000
1032
1033
1034
1035static floperr_t apple35_2img_decode(floppy_image *floppy, UINT32 *image_format,
1036   UINT32 *data_offset, UINT32 *data_length)
1037{
1038   struct header_2img header;
1039   UINT64 size;
1040
1041   if (image_format)
1042      *image_format = 0;
1043   if (data_offset)
1044      *data_offset = 0;
1045   if (data_length)
1046      *data_length = 0;
1047
1048   size = floppy_image_size(floppy);
1049   if (size < sizeof(header))
1050   {
1051      return FLOPPY_ERROR_INVALIDIMAGE;
1052   }
1053
1054   floppy_image_read(floppy, &header, 0, sizeof(header));
1055
1056   if (memcmp(header.magic, "2IMG", 4))
1057   {
1058      return FLOPPY_ERROR_INVALIDIMAGE;
1059   }
1060
1061   header.header_length   = LITTLE_ENDIANIZE_INT16(header.header_length);
1062   header.version         = LITTLE_ENDIANIZE_INT16(header.version);
1063   header.image_format      = LITTLE_ENDIANIZE_INT32(header.image_format);
1064   header.flags         = LITTLE_ENDIANIZE_INT32(header.flags);
1065   header.block_count      = LITTLE_ENDIANIZE_INT32(header.block_count);
1066   header.data_offset      = LITTLE_ENDIANIZE_INT32(header.data_offset);
1067   header.data_length      = LITTLE_ENDIANIZE_INT32(header.data_length);
1068   header.comment_offset   = LITTLE_ENDIANIZE_INT32(header.comment_offset);
1069   header.comment_length   = LITTLE_ENDIANIZE_INT32(header.comment_length);
1070   header.creator_offset   = LITTLE_ENDIANIZE_INT32(header.creator_offset);
1071   header.creator_length   = LITTLE_ENDIANIZE_INT32(header.creator_length);
1072
1073   // at least some images "in the wild" (e.g. TOSEC Minor Set 1) have big-endian data sizes
1074   // even though that's against the .2mg spec
1075   if (header.data_length == 0x800c00)
1076   {
1077      logerror("ap_dsk35: corrected bad-endian data length\n");
1078      header.data_length = 0x0c8000;
1079   }
1080
1081   if ((((UINT64) header.data_offset) + header.data_length) > size)
1082      return FLOPPY_ERROR_INVALIDIMAGE;
1083   if ((((UINT64) header.comment_offset) + header.comment_length) > size)
1084      return FLOPPY_ERROR_INVALIDIMAGE;
1085   if ((((UINT64) header.creator_offset) + header.creator_length) > size)
1086      return FLOPPY_ERROR_INVALIDIMAGE;
1087   if ((header.image_format != IMAGE_FORMAT_DO) &&
1088         (header.image_format != IMAGE_FORMAT_PO) &&
1089         (header.image_format != IMAGE_FORMAT_NIB))
1090      return FLOPPY_ERROR_INVALIDIMAGE;
1091
1092   if (image_format)
1093      *image_format = header.image_format;
1094   if (data_offset)
1095      *data_offset = header.data_offset;
1096   if (data_length)
1097      *data_length = header.data_length;
1098   return FLOPPY_ERROR_SUCCESS;
1099}
1100
1101
1102
1103static FLOPPY_IDENTIFY(apple35_2img_identify)
1104{
1105   *vote = apple35_2img_decode(floppy, NULL, NULL, NULL) ? 0 : 100;
1106   return FLOPPY_ERROR_SUCCESS;
1107}
1108
1109
1110
1111static FLOPPY_CONSTRUCT(apple35_2img_construct)
1112{
1113   floperr_t err;
1114   UINT32 image_format;
1115   UINT32 data_offset;
1116   UINT32 data_size;
1117   UINT8 sides = 2;
1118   struct header_2img header;
1119
1120   if (params)
1121   {
1122      /* create */
1123      sides = option_resolution_lookup_int(params, PARAM_HEADS);
1124
1125      data_offset = sizeof(header);
1126      data_size = 80*sides*10*512;
1127
1128      memset(&header, 0, sizeof(header));
1129      header.header_length   = LITTLE_ENDIANIZE_INT16(sizeof(header));
1130      header.block_count      = LITTLE_ENDIANIZE_INT32(80*sides*10);
1131      header.data_offset      = LITTLE_ENDIANIZE_INT32(data_offset);
1132      header.data_length      = LITTLE_ENDIANIZE_INT32(data_size);
1133
1134      floppy_image_write(floppy, &header, 0, sizeof(header));
1135      floppy_image_write_filler(floppy, 0, sizeof(header), data_size);
1136   }
1137   else
1138   {
1139      /* load */
1140      err = apple35_2img_decode(floppy, &image_format, &data_offset, &data_size);
1141      if (err)
1142         return err;
1143
1144      if (data_size == 80*1*10*512)
1145         sides = 1;   /* single sided */
1146      else if (data_size == 80*2*10*512)
1147         sides = 2;   /* double sided */
1148      else
1149         sides = 2;   /* unknown... what to do... */
1150   }
1151
1152   return apple35_construct(floppy, data_offset, data_size,
1153      0, 0, -1, sides, FALSE);
1154}
1155
1156
1157
1158FLOPPY_OPTIONS_START( apple35_mac )
1159   FLOPPY_OPTION( apple35_raw, "dsk,img,image",   "Apple raw 3.5\" disk image",   apple35_raw_identify,      apple35_raw_construct, NULL,
1160      HEADS([1]-2)
1161      TRACKS([80])
1162      SECTOR_LENGTH([512])
1163      FIRST_SECTOR_ID([0]))
1164   FLOPPY_OPTION( apple35_dc, "dc,dsk,img,image",   "Apple DiskCopy disk image",   apple35_diskcopy_identify,   apple35_diskcopy_construct, NULL,
1165      HEADS([1]-2)
1166      TRACKS([80])
1167      SECTOR_LENGTH([512])
1168      FIRST_SECTOR_ID([0]))
1169FLOPPY_OPTIONS_END
1170
1171FLOPPY_OPTIONS_START( apple35_iigs )
1172   FLOPPY_OPTION( apple35_raw, "dsk,img,image",   "Apple raw 3.5\" disk image",   apple35_raw_identify,      apple35_raw_construct, NULL,
1173      HEADS([1]-2)
1174      TRACKS([80])
1175      SECTOR_LENGTH([512])
1176      FIRST_SECTOR_ID([0]))
1177   FLOPPY_OPTION( apple35_dc, "dc,dsk,img,image",   "Apple DiskCopy disk image",   apple35_diskcopy_identify,   apple35_diskcopy_construct, NULL,
1178      HEADS([1]-2)
1179      TRACKS([80])
1180      SECTOR_LENGTH([512])
1181      FIRST_SECTOR_ID([0]))
1182   FLOPPY_OPTION( apple35_2img, "2img,2mg",      "Apple ][gs 2IMG disk image",   apple35_2img_identify,      apple35_2img_construct, NULL,
1183      HEADS([1]-2)
1184      TRACKS([80])
1185      SECTOR_LENGTH([512])
1186      FIRST_SECTOR_ID([0]))
1187FLOPPY_OPTIONS_END
src/lib/formats/apridisk.h
r0r11458
1/***************************************************************************
2
3    APRIDISK disk image format
4
5***************************************************************************/
6
7#ifndef __APRIDISK_H__
8#define __APRIDISK_H__
9
10#include "flopimg.h"
11
12FLOPPY_IDENTIFY( apridisk_identify );
13FLOPPY_CONSTRUCT( apridisk_construct );
14
15#endif /* __APRIDISK_H__ */
src/lib/formats/msx_dsk.h
r0r11458
1/*********************************************************************
2
3    msx_dsk.h
4
5    MSX disk images
6
7*********************************************************************/
8
9#ifndef MSX_DSK_H
10#define MSX_DSK_H
11
12#include "flopimg.h"
13
14/**************************************************************************/
15
16FLOPPY_OPTIONS_EXTERN(msx);
17
18#endif /* MSX_DSK_H */
src/lib/formats/ap_dsk35.h
r0r11458
1/*********************************************************************
2
3    ap_dsk35.h
4
5    Apple 3.5" disk images
6
7*********************************************************************/
8
9#ifndef AP_DSK35_H
10#define AP_DSK35_H
11
12#include "flopimg.h"
13
14void sony_filltrack(UINT8 *buffer, size_t buffer_len, size_t *pos, UINT8 data);
15UINT8 sony_fetchtrack(const UINT8 *buffer, size_t buffer_len, size_t *pos);
16
17int apple35_sectors_per_track(floppy_image *image, int track);
18
19/**************************************************************************/
20
21FLOPPY_OPTIONS_EXTERN(apple35_mac);
22FLOPPY_OPTIONS_EXTERN(apple35_iigs);
23
24
25#endif /* AP_DSK35_H */
src/lib/formats/comx35_dsk.c
r0r11458
1/*********************************************************************
2
3    formats/comx35_dsk.c
4
5    COMX35 disk images
6
7*********************************************************************/
8
9#include "formats/comx35_dsk.h"
10#include "formats/basicdsk.h"
11
12/***************************************************************************
13    IMPLEMENTATION
14***************************************************************************/
15
16/*-------------------------------------------------
17    FLOPPY_IDENTIFY( comx35_dsk_identify )
18-------------------------------------------------*/
19
20static FLOPPY_IDENTIFY( comx35_dsk_identify )
21{
22   *vote = ((floppy_image_size(floppy) == (35*1*16*128)) || (floppy_image_size(floppy) == (35*2*16*128))) ? 100 : 0;
23
24   return FLOPPY_ERROR_SUCCESS;
25}
26
27/*-------------------------------------------------
28    FLOPPY_CONSTRUCT( comx35_dsk_construct )
29-------------------------------------------------*/
30
31static FLOPPY_CONSTRUCT( comx35_dsk_construct )
32{
33   UINT8 header[1];
34   int heads = 1;
35   int cylinders = 35;
36
37   switch (floppy_image_size(floppy))
38   {
39   case 35*1*16*128:
40      heads = 1;
41      cylinders = 35;
42      break;
43
44   case 35*2*16*128:
45      floppy_image_read(floppy, header, 0x12, 1);
46
47      if (header[0] == 0x01)
48      {
49         heads = 1;
50         cylinders = 70;
51      }
52      else
53      {
54         heads = 2;
55         cylinders = 35;
56      }
57      break;
58   }
59
60   struct basicdsk_geometry geometry;
61   memset(&geometry, 0, sizeof(geometry));
62
63   geometry.heads = heads;
64   geometry.first_sector_id = 0;
65   geometry.sector_length = 128;
66   geometry.tracks = cylinders;
67   geometry.sectors = 16;
68
69   return basicdsk_construct(floppy, &geometry);
70}
71
72/*-------------------------------------------------
73    FLOPPY_OPTIONS( comx35 )
74-------------------------------------------------*/
75
76FLOPPY_OPTIONS_START( comx35 )
77   FLOPPY_OPTION( comx35, "img", "COMX35 floppy disk image", comx35_dsk_identify, comx35_dsk_construct, NULL, NULL )
78FLOPPY_OPTIONS_END
src/lib/formats/comx35_dsk.h
r0r11458
1/*********************************************************************
2
3    formats/comx35_dsk.h
4
5    COMX35 disk images
6
7*********************************************************************/
8
9#ifndef COMX35_DSK_H
10#define COMX35_DSK_H
11
12#include "flopimg.h"
13
14/**************************************************************************/
15
16FLOPPY_OPTIONS_EXTERN(comx35);
17
18#endif /* COMX35_DSK_H */
src/lib/formats/svi_cas.c
r0r11458
1#include "svi_cas.h"
2
3#define CAS_PERIOD_0      (37)
4#define CAS_PERIOD_1      (18)
5#define CAS_HEADER_PERIODS (1600)
6#define CAS_EMPTY_SAMPLES (24220)
7#define CAS_INIT_SAMPLES   (200)
8
9static const UINT8 CasHeader[17] =
10{
11   0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
12   0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x7f
13};
14
15#define SMPLO   -32768
16#define SMPHI   32767
17
18static int cas_size;
19
20/*******************************************************************
21   Generate samples for the tape image
22********************************************************************/
23static int svi_cas_fill_wave(INT16 *buffer, int sample_count, UINT8 *bytes)
24{
25   int cas_pos, samples_pos, n, i;
26
27   cas_pos = 17;
28   samples_pos = 0;
29
30   /* write CAS_INIT_SAMPLES of silence */
31   n = CAS_INIT_SAMPLES; while (n--) buffer[samples_pos++] = 0;
32
33   while (samples_pos < sample_count && cas_pos < cas_size)
34   {
35      /* write CAS_HEADER_PERIODS of header */
36      for (i=0;i<CAS_HEADER_PERIODS;i++)
37      {
38         /* write a "0" */
39         n = !(i % 4) ? 21 : 18;
40         while (n--) buffer[samples_pos++] = SMPHI;
41         n = 19; while (n--) buffer[samples_pos++] = SMPLO;
42         /* write a "1" */
43         n = 9; while (n--) buffer[samples_pos++] = SMPHI;
44         n = 9; while (n--) buffer[samples_pos++] = SMPLO;
45      }
46
47      /* write "0x7f" */
48      /* write a "0" */
49      n = 21; while (n--) buffer[samples_pos++] = SMPHI;
50      n = 19; while (n--) buffer[samples_pos++] = SMPLO;
51
52      for (i=0;i<7;i++)
53      {
54         /* write a "1" */
55         n = 9; while (n--) buffer[samples_pos++] = SMPHI;
56         n = 9; while (n--) buffer[samples_pos++] = SMPLO;
57      }
58
59      while (samples_pos < sample_count && cas_pos < cas_size)
60      {
61         n = 21; while (n--) buffer[samples_pos++] = SMPHI;
62         n = 19; while (n--) buffer[samples_pos++] = SMPLO;
63
64         for (i=0;i<8;i++)
65         {
66            int bit = (bytes[cas_pos] & (0x80 >> i) );
67
68            /* write this one bit */
69            if (bit)
70            {
71               /* write a "1" */
72               n = 9; while (n--) buffer[samples_pos++] = SMPHI;
73               n = 9; while (n--) buffer[samples_pos++] = SMPLO;
74            }
75            else
76            {
77               /* write a "0" */
78               n = 18; while (n--) buffer[samples_pos++] = SMPHI;
79               n = 19; while (n--) buffer[samples_pos++] = SMPLO;
80            }
81         }
82
83         cas_pos++;
84
85         /* check if we've hit a new header (or end of block) */
86         if ( (cas_pos + 17) < cas_size)
87         {
88            if (!memcmp (bytes + cas_pos, CasHeader, 17) )
89            {
90               cas_pos += 17;
91
92               /* end of block marker */
93               n = CAS_EMPTY_SAMPLES; while (n--) buffer[samples_pos++] = SMPHI;
94
95               break; /* falls back to loop above; plays header again */
96            }
97         }
98      }
99   }
100
101   /* end of block marker */
102   n = CAS_EMPTY_SAMPLES; while (n--) buffer[samples_pos++] = SMPHI;
103
104   return samples_pos;
105}
106
107
108/*******************************************************************
109   Calculate the number of samples needed for this tape image
110********************************************************************/
111static int svi_cas_to_wav_size(const UINT8 *casdata, int caslen)
112{
113   int cas_pos, samples_pos, size, i;
114
115   if (caslen < 17) return -1;
116   if (memcmp (casdata, CasHeader, sizeof (CasHeader) ) ) return -1;
117
118   cas_size = caslen;
119
120   cas_pos = 17;
121
122   samples_pos = CAS_INIT_SAMPLES;
123
124   while (cas_pos < caslen)
125   {
126      size = CAS_HEADER_PERIODS * ( CAS_PERIOD_0 + CAS_PERIOD_1 ) +
127            ( CAS_HEADER_PERIODS / 4 ) * 3;
128
129      samples_pos += size;
130
131      samples_pos += 21 + 19 + 7 * CAS_PERIOD_1;
132
133      while (cas_pos < caslen)
134      {
135         samples_pos += 21;
136         samples_pos += 19;
137
138         for (i=0;i<8;i++)
139         {
140            int bit = (casdata[cas_pos] & (0x80 >> i) );
141
142            samples_pos += bit ? CAS_PERIOD_1 : CAS_PERIOD_0;
143         }
144
145         cas_pos++;
146
147         /* check if we've hit a new header (or end of block) */
148         if ( (cas_pos + 17) < caslen)
149         {
150            if (!memcmp (casdata + cas_pos, CasHeader, 17) )
151            {
152               cas_pos += 17;
153
154               /* end of block marker */
155               samples_pos += CAS_EMPTY_SAMPLES;
156
157               break; /* falls back to loop above; plays header again */
158            }
159         }
160      }
161   }
162
163   /* end of block marker */
164   samples_pos += CAS_EMPTY_SAMPLES;
165
166   return samples_pos;
167}
168
169
170static const struct CassetteLegacyWaveFiller svi_legacy_fill_wave =
171{
172   svi_cas_fill_wave,                  /* fill_wave */
173   -1,                              /* chunk_size */
174   0,                              /* chunk_samples */
175   svi_cas_to_wav_size,               /* chunk_sample_calc */
176   44100,                           /* sample_frequency */
177   0,                              /* header_samples */
178   0                              /* trailer_samples */
179};
180
181
182
183static casserr_t svi_cas_identify(cassette_image *cassette, struct CassetteOptions *opts)
184{
185   return cassette_legacy_identify(cassette, opts, &svi_legacy_fill_wave);
186}
187
188
189
190static casserr_t svi_cas_load(cassette_image *cassette)
191{
192   return cassette_legacy_construct(cassette, &svi_legacy_fill_wave);
193}
194
195
196
197static const struct CassetteFormat svi_cas_format =
198{
199   "cas",
200   svi_cas_identify,
201   svi_cas_load,
202   NULL
203};
204
205
206
207CASSETTE_FORMATLIST_START(svi_cassette_formats)
208   CASSETTE_FORMAT(svi_cas_format)
209CASSETTE_FORMATLIST_END
src/lib/formats/trd_dsk.c
r0r11458
1/*********************************************************************
2
3    formats/trd_dsk.c
4
5    TRD disk images
6
7*********************************************************************/
8
9#include <string.h>
10
11#include "trd_dsk.h"
12#include "basicdsk.h"
13
14
15static FLOPPY_IDENTIFY(trd_dsk_identify)
16{
17   *vote = 100;
18   return FLOPPY_ERROR_SUCCESS;
19}
20
21static FLOPPY_CONSTRUCT(trd_dsk_construct)
22{
23   struct basicdsk_geometry geometry;
24   UINT8 data[1];
25   int heads;
26   int cylinders;
27
28   floppy_image_read( floppy, data, 0x8e3 , 1 );
29
30   /* guess geometry of disk */
31   heads =  data[0] & 0x08 ? 1 : 2;
32   cylinders = data[0] & 0x01 ? 40 : 80;
33
34   memset(&geometry, 0, sizeof(geometry));
35   geometry.heads = heads;
36   geometry.first_sector_id = 1;
37   geometry.sector_length = 256;
38   geometry.tracks = cylinders;
39   geometry.sectors = 16;
40   return basicdsk_construct(floppy, &geometry);
41}
42
43
44
45/* ----------------------------------------------------------------------- */
46
47FLOPPY_OPTIONS_START( trd )
48   FLOPPY_OPTION( trd_dsk, "trd",      "TRD floppy disk image",   trd_dsk_identify, trd_dsk_construct, NULL, NULL)
49FLOPPY_OPTIONS_END
50
src/lib/formats/svi_cas.h
r0r11458
1/*********************************************************************
2
3    svi_cas.h
4
5    Format code for SVI casette files
6
7*********************************************************************/
8
9#ifndef SVI_CAS_H
10#define SVI_CAS_H
11
12#include "cassimg.h"
13
14CASSETTE_FORMATLIST_EXTERN(svi_cassette_formats);
15
16#endif /* SVI_CAS_H */
17
src/lib/formats/ace_tap.c
r0r11458
1/********************************************************************
2
3Support for Jupiter Ace .tap cassette images
4
5For more information see:
6- http://www.jupiter-ace.co.uk/faq_ace_tap_format.html
7- http://www.jupiter-ace.co.uk/doc_AceTapeFormat.html
8
9********************************************************************/
10#include "ace_tap.h"
11
12
13#define SMPLO   -32768
14#define SILENCE   0
15#define SMPHI   32767
16
17
18static int cas_size;
19
20
21/*******************************************************************
22   Generate one high-low cycle of sample data
23********************************************************************/
24INLINE int ace_tap_cycle(INT16 *buffer, int sample_pos, int high, int low)
25{
26   int i = 0;
27
28   if ( buffer )
29   {
30      while( i < high)
31      {
32         buffer[ sample_pos + i ] = SMPHI;
33         i++;
34      }
35
36      while( i < high + low )
37      {
38         buffer[ sample_pos + i ] = SMPLO;
39         i++;
40      }
41   }
42   return high + low;
43}
44
45
46INLINE int ace_tap_silence(INT16 *buffer, int sample_pos, int samples)
47{
48   int i = 0;
49
50   if ( buffer )
51   {
52      while( i < samples )
53      {
54         buffer[ sample_pos + i ] = SILENCE;
55         i++;
56      }
57   }
58   return samples;
59}
60
61
62INLINE int ace_tap_byte(INT16 *buffer, int sample_pos, UINT8 data)
63{
64   int i, samples;
65
66   samples = 0;
67   for ( i = 0; i < 8; i++ )
68   {
69      if ( data & 0x80 )
70         samples += ace_tap_cycle( buffer, sample_pos + samples, 21, 22 );
71      else
72         samples += ace_tap_cycle( buffer, sample_pos + samples, 10, 11 );
73
74      data <<= 1;
75   }
76   return samples;
77}
78
79
80static int ace_handle_tap(INT16 *buffer, const UINT8 *casdata)
81{
82   int   data_pos, sample_count;
83
84   /* Make sure the file starts with a valid header */
85   if ( cas_size < 0x1C )
86      return -1;
87   if ( casdata[0] != 0x1A || casdata[1] != 0x00 )
88      return -1;
89
90   data_pos = 0;
91   sample_count = 0;
92
93   while( data_pos < cas_size )
94   {
95      UINT16   block_size;
96      int      i;
97
98      /* Handle a block of tape data */
99      block_size = casdata[data_pos] + ( casdata[data_pos + 1] << 8 );
100      data_pos += 2;
101
102      /* Make sure there are enough bytes left */
103      if ( data_pos > cas_size )
104         return -1;
105
106      /* 2 seconds silence */
107      sample_count += ace_tap_silence( buffer, sample_count, 2 * 44100 );
108
109      /* Add pilot tone samples: 4096 for header, 512 for data */
110      for( i = ( block_size == 0x001A ) ? 4096 : 512; i; i-- )
111         sample_count += ace_tap_cycle( buffer, sample_count, 27, 27 );
112
113      /* Sync samples */
114      sample_count += ace_tap_cycle( buffer, sample_count, 8, 11 );
115
116      /* Output block type identification byte */
117      sample_count += ace_tap_byte( buffer, sample_count, ( block_size != 0x001A ) ? 0xFF : 0x00 );
118
119      /* Data samples */
120      for ( ; block_size ; data_pos++, block_size-- )
121         sample_count += ace_tap_byte( buffer, sample_count, casdata[data_pos] );
122
123      /* End mark samples */
124      sample_count += ace_tap_cycle( buffer, sample_count, 12, 57 );
125
126      /* 3 seconds silence */
127      sample_count += ace_tap_silence( buffer, sample_count, 3 * 44100 );
128   }
129   return sample_count;
130}
131
132
133/*******************************************************************
134   Generate samples for the tape image
135********************************************************************/
136static int ace_tap_fill_wave(INT16 *buffer, int sample_count, UINT8 *bytes)
137{
138   return ace_handle_tap( buffer, bytes );
139}
140
141
142/*******************************************************************
143   Calculate the number of samples needed for this tape image
144********************************************************************/
145static int ace_tap_to_wav_size(const UINT8 *casdata, int caslen)
146{
147   cas_size = caslen;
148
149   return ace_handle_tap( NULL, casdata );
150}
151
152
153static const struct CassetteLegacyWaveFiller ace_legacy_fill_wave =
154{
155   ace_tap_fill_wave,               /* fill_wave */
156   -1,                              /* chunk_size */
157   0,                              /* chunk_samples */
158   ace_tap_to_wav_size,            /* chunk_sample_calc */
159   44100,                           /* sample_frequency */
160   0,                              /* header_samples */
161   0                              /* trailer_samples */
162};
163
164
165static casserr_t ace_tap_identify(cassette_image *cassette, struct CassetteOptions *opts)
166{
167   return cassette_legacy_identify(cassette, opts, &ace_legacy_fill_wave);
168}
169
170
171static casserr_t ace_tap_load(cassette_image *cassette)
172{
173   return cassette_legacy_construct(cassette, &ace_legacy_fill_wave);
174}
175
176
177static const struct CassetteFormat ace_tap_format =
178{
179   "tap",
180   ace_tap_identify,
181   ace_tap_load,
182   NULL
183};
184
185
186CASSETTE_FORMATLIST_START(ace_cassette_formats)
187   CASSETTE_FORMAT(ace_tap_format)
188CASSETTE_FORMATLIST_END
src/lib/formats/trs_cas.c
r0r11458
1/********************************************************************
2
3Support for TRS80 .cas cassette images
4
5********************************************************************/
6
7#include "formats/trs_cas.h"
8
9#define SILENCE   0
10#define SMPLO   -32768
11#define SMPHI   32767
12
13
14static int cas_size;
15
16
17/*******************************************************************
18   Generate one high-low cycle of sample data
19********************************************************************/
20INLINE int trs80l2_cas_cycle(INT16 *buffer, int sample_pos, int silence, int high, int low)
21{
22   int i = 0;
23
24   if ( buffer )
25   {
26      while( i < silence )
27      {
28         buffer[ sample_pos + i ] = SILENCE;
29         i++;
30      }
31      while( i < silence + high)
32      {
33         buffer[ sample_pos + i ] = SMPHI;
34         i++;
35      }
36
37      while( i < silence + high + low )
38      {
39         buffer[ sample_pos + i ] = SMPLO;
40         i++;
41      }
42   }
43   return silence + high + low;
44}
45
46
47static int trs80l2_handle_cas(INT16 *buffer, const UINT8 *casdata)
48{
49   int   data_pos, sample_count;
50
51   data_pos = 0;
52   sample_count = 0;
53
54   while( data_pos < cas_size )
55   {
56      UINT8   data = casdata[data_pos];
57      int      i;
58
59      for ( i = 0; i < 8; i++ )
60      {
61         /* Signal code */
62         sample_count += trs80l2_cas_cycle( buffer, sample_count, 33, 6, 6 );
63
64         /* Bit code */
65         if ( data & 0x80 )
66            sample_count += trs80l2_cas_cycle( buffer, sample_count, 33, 6, 6 );
67         else
68            sample_count += trs80l2_cas_cycle( buffer, sample_count, 33 + 6 + 6, 0, 0 );
69
70         data <<= 1;
71      }
72
73      data_pos++;
74   }
75   return sample_count;
76}
77
78
79/*******************************************************************
80   Generate samples for the tape image
81********************************************************************/
82static int trs80l2_cas_fill_wave(INT16 *buffer, int sample_count, UINT8 *bytes)
83{
84   return trs80l2_handle_cas( buffer, bytes );
85}
86
87
88/*******************************************************************
89   Calculate the number of samples needed for this tape image
90********************************************************************/
91static int trs80l2_cas_to_wav_size(const UINT8 *casdata, int caslen)
92{
93   cas_size = caslen;
94
95   return trs80l2_handle_cas( NULL, casdata );
96}
97
98static const struct CassetteLegacyWaveFiller trs80l2_cas_legacy_fill_wave =
99{
100   trs80l2_cas_fill_wave,               /* fill_wave */
101   -1,                              /* chunk_size */
102   0,                              /* chunk_samples */
103   trs80l2_cas_to_wav_size,            /* chunk_sample_calc */
104   44100,                           /* sample_frequency */
105   0,                              /* header_samples */
106   0                              /* trailer_samples */
107};
108
109
110static casserr_t trs80l2_cas_identify(cassette_image *cassette, struct CassetteOptions *opts)
111{
112   return cassette_legacy_identify(cassette, opts, &trs80l2_cas_legacy_fill_wave);
113}
114
115
116static casserr_t trs80l2_cas_load(cassette_image *cassette)
117{
118   return cassette_legacy_construct(cassette, &trs80l2_cas_legacy_fill_wave);
119}
120
121
122static const struct CassetteFormat trs80l2_cas_format =
123{
124   "cas",
125   trs80l2_cas_identify,
126   trs80l2_cas_load,
127   NULL
128};
129
130
131CASSETTE_FORMATLIST_START(trs80l2_cassette_formats)
132   CASSETTE_FORMAT(trs80l2_cas_format)
133CASSETTE_FORMATLIST_END
134
src/lib/formats/d64_dsk.c
r0r11458
1/*********************************************************************
2
3    formats/d64_dsk.c
4
5    Floppy format code for Commodore 1541/2040/8050 disk images
6
7*********************************************************************/
8
9/*
10
11    TODO:
12
13    - fix MAX_ERROR_SECTORS, it is too small for d80/d82
14    - write to disk
15    - disk errors 24, 25, 26, 28, 74
16    - variable gaps
17
18*/
19
20#define XTAL_16MHz      16000000
21#define XTAL_12MHz      12000000
22#include "g64_dsk.h"
23#include "flopimg.h"
24#include "d64_dsk.h"
25
26/***************************************************************************
27    PARAMETERS
28***************************************************************************/
29
30#define LOG 1
31
32#define MAX_HEADS         2
33#define MAX_TRACKS         84
34#define MAX_ERROR_SECTORS   802 // TODO this is too small for .d80 files
35#define SECTOR_SIZE         256
36#define SECTOR_SIZE_GCR      368
37
38#define INVALID_OFFSET      0xbadbad
39
40#define D64_SIZE_35_TRACKS             174848
41#define D64_SIZE_35_TRACKS_WITH_ERRORS    175531
42#define D64_SIZE_40_TRACKS             196608
43#define D64_SIZE_40_TRACKS_WITH_ERRORS    197376
44#define D64_SIZE_42_TRACKS             205312
45#define D64_SIZE_42_TRACKS_WITH_ERRORS    206114
46#define D67_SIZE_35_TRACKS             176640
47#define D71_SIZE_70_TRACKS             349696
48#define D71_SIZE_70_TRACKS_WITH_ERRORS    351062
49#define D80_SIZE_77_TRACKS             533248
50#define D82_SIZE_154_TRACKS            1066496
51
52enum
53{
54   DOS1,
55   DOS2,
56   DOS25
57};
58
59static const char *const DOS_VERSION[] = { "1.0", "2.0", "2.5" };
60
61enum
62{
63   ERROR_00 = 1,
64   ERROR_20,      /* header block not found */
65   ERROR_21,      /* no sync character */
66   ERROR_22,      /* data block not present */
67   ERROR_23,      /* checksum error in data block */
68   ERROR_24,      /* write verify (on format) UNIMPLEMENTED */
69   ERROR_25,      /* write verify error UNIMPLEMENTED */
70   ERROR_26,      /* write protect on UNIMPLEMENTED */
71   ERROR_27,      /* checksum error in header block */
72   ERROR_28,      /* write error UNIMPLEMENTED */
73   ERROR_29,      /* disk ID mismatch */
74   ERROR_74,      /* disk not ready (no device 1) UNIMPLEMENTED */
75};
76
77static const char *const ERROR_CODE[] = { "00", "00", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "74" };
78
79static const UINT8 bin_2_gcr[] =
80{
81   0x0a, 0x0b, 0x12, 0x13, 0x0e, 0x0f, 0x16, 0x17,
82   0x09, 0x19, 0x1a, 0x1b, 0x0d, 0x1d, 0x1e, 0x15
83};
84
85/* This could be of use if we ever implement saving in .d64 format, to convert back GCR -> d64 */
86/*
87static const int gcr_2_bin[] =
88{
89    -1, -1,   -1,   -1,
90    -1, -1,   -1,   -1,
91    -1, 0x08, 0x00, 0x01,
92    -1, 0x0c, 0x04, 0x05,
93    -1, -1,   0x02, 0x03,
94    -1, 0x0f, 0x06, 0x07,
95    -1, 0x09, 0x0a, 0x0b,
96    -1, 0x0d, 0x0e, -1
97};
98*/
99
100static const int DOS1_SECTORS_PER_TRACK[] =
101{
102   21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21,
103   20, 20, 20, 20, 20, 20, 20,
104   18, 18, 18, 18, 18, 18,
105   17, 17, 17, 17, 17,
106   17, 17, 17, 17, 17,
107   17, 17
108};
109
110static const int DOS1_SPEED_ZONE[] =
111{
112   3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
113   2, 2, 2, 2, 2, 2, 2,
114   1, 1, 1, 1, 1, 1,
115   0, 0, 0, 0, 0,
116   0, 0, 0, 0, 0,
117   0, 0
118};
119
120static const int DOS2_SECTORS_PER_TRACK[] =
121{
122   21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21,
123   19, 19, 19, 19, 19, 19, 19,
124   18, 18, 18, 18, 18, 18,
125   17, 17, 17, 17, 17,
126   17, 17, 17, 17, 17,
127   17, 17
128};
129
130static const int DOS25_SECTORS_PER_TRACK[] =
131{
132   29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
133   29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,      /* 1-39 */
134   27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,                     /* 40-53 */
135   25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,                              /* 54-64 */
136   23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23,                        /* 65-77 */
137   23, 23, 23, 23, 23, 23, 23                                          /* 78-84 */
138};
139
140static const int DOS25_SPEED_ZONE[] =
141{
142   3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
143   3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,   /* 1-39 */
144   2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,               /* 40-53 */
145   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,                     /* 54-64 */
146   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,                  /* 65-77 */
147   0, 0, 0, 0, 0, 0, 0                                 /* 78-84 */
148};
149
150/***************************************************************************
151    TYPE DEFINITIONS
152***************************************************************************/
153
154struct d64dsk_tag
155{
156   int dos;                           /* CBM DOS version */
157   int heads;                           /* number of physical heads */
158   int tracks;                           /* number of physical tracks */
159   int dos_tracks;                        /* number of logical tracks */
160   int track_offset[MAX_HEADS][MAX_TRACKS];   /* offset within image for each physical track */
161   UINT32 speed_zone[MAX_TRACKS];            /* speed zone for each physical track */
162   bool has_errors;                     /* flag to check for available error codes */
163   UINT8 error[MAX_ERROR_SECTORS];            /* error code for each logical sector */
164   int error_offset[MAX_HEADS][MAX_TRACKS];   /* offset within error array for sector 0 of each logical track */
165
166   UINT8 id1, id2;                        /* DOS disk format ID */
167};
168
169/***************************************************************************
170    INLINE FUNCTIONS
171***************************************************************************/
172
173INLINE struct d64dsk_tag *get_tag(floppy_image *floppy)
174{
175   return (d64dsk_tag *)floppy_tag(floppy);
176}
177
178INLINE float get_dos_track(int track)
179{
180   return ((float)track / 2) + 1;
181}
182
183/***************************************************************************
184    IMPLEMENTATION
185***************************************************************************/
186
187/*-------------------------------------------------
188    d64_get_heads_per_disk - returns the number
189    of heads in the disk image
190-------------------------------------------------*/
191
192static int d64_get_heads_per_disk(floppy_image *floppy)
193{
194   return get_tag(floppy)->heads;
195}
196
197/*-------------------------------------------------
198    d64_get_tracks_per_disk - returns the number
199    of DOS tracks in the disk image
200-------------------------------------------------*/
201
202static int d64_get_tracks_per_disk(floppy_image *floppy)
203{
204   return get_tag(floppy)->tracks;
205}
206
207/*-------------------------------------------------
208    d64_get_sectors_per_track - returns the number
209    of sectors per given track
210-------------------------------------------------*/
211
212static int d64_get_sectors_per_track(floppy_image *floppy, int head, int track)
213{
214   int sectors_per_track = 0;
215
216   switch (get_tag(floppy)->dos)
217   {
218   case DOS1:   sectors_per_track = DOS1_SECTORS_PER_TRACK[track / 2]; break;
219   case DOS2:   sectors_per_track = DOS2_SECTORS_PER_TRACK[track / 2]; break;
220   case DOS25:   sectors_per_track = DOS25_SECTORS_PER_TRACK[track];    break;
221   }
222
223   return sectors_per_track;
224}
225
226/*-------------------------------------------------
227    get_track_offset - returns the offset within
228    the disk image for a given track
229-------------------------------------------------*/
230
231static floperr_t get_track_offset(floppy_image *floppy, int head, int track, UINT64 *offset)
232{
233   struct d64dsk_tag *tag = get_tag(floppy);
234   UINT64 offs = 0;
235
236   if ((track < 0) || (track >= tag->tracks))
237      return FLOPPY_ERROR_SEEKERROR;
238
239   offs = tag->track_offset[head][track];
240
241   if (offset)
242      *offset = offs;
243
244   return FLOPPY_ERROR_SUCCESS;
245}
246
247/*-------------------------------------------------
248    d64_get_track_size - returns the track size
249-------------------------------------------------*/
250
251static UINT32 d64_get_track_size(floppy_image *floppy, int head, int track)
252{
253   struct d64dsk_tag *tag = get_tag(floppy);
254
255   if (tag->track_offset[head][track] == INVALID_OFFSET)
256      return 0;
257
258   /* determine number of sectors per track */
259   int sectors_per_track = d64_get_sectors_per_track(floppy, head, track);
260
261   /* allocate temporary GCR track data buffer */
262   UINT32 track_length = sectors_per_track * SECTOR_SIZE_GCR;
263
264   return track_length;
265}
266
267/*-------------------------------------------------
268    get_sector_error_code - returns the error
269    code for the given sector
270-------------------------------------------------*/
271
272static int get_sector_error_code(floppy_image *floppy, int head, int dos_track, int sector)
273{
274   struct d64dsk_tag *tag = get_tag(floppy);
275
276   if (!tag->has_errors)
277      return ERROR_00;
278
279   int sector_error = tag->error[tag->error_offset[head][dos_track] + sector]; // TODO index out of bounds!!!
280
281   if (sector_error != ERROR_00)
282   {
283      logerror("D64 error %s head %u track %u sector %u\n", ERROR_CODE[sector_error], head, dos_track, sector);
284   }
285
286   return sector_error;
287}
288
289/*-------------------------------------------------
290    gcr_double_2_gcr - GCR decodes given data
291-------------------------------------------------*/
292
293/* gcr_double_2_gcr takes 4 bytes (a, b, c, d) and shuffles their nibbles to obtain 5 bytes in dest */
294/* The result is basically res = (enc(a) << 15) | (enc(b) << 10) | (enc(c) << 5) | enc(d)
295 * with res being 5 bytes long and enc(x) being the GCR encode of x.
296 * In fact, we store the result as five separate bytes in the dest argument
297 */
298
299/*
300
301 Commodore GCR format
302
303Original    Encoded
3044 bits      5 bits
305
3060000    ->  01010 = 0x0a
3070001    ->  01011 = 0x0b
3080010    ->  10010 = 0x12
3090011    ->  10011 = 0x13
3100100    ->  01110 = 0x0e
3110101    ->  01111 = 0x0f
3120110    ->  10110 = 0x16
3130111    ->  10111 = 0x17
3141000    ->  01001 = 0x09
3151001    ->  11001 = 0x19
3161010    ->  11010 = 0x1a
3171011    ->  11011 = 0x1b
3181100    ->  01101 = 0x0d
3191101    ->  11101 = 0x1d
3201110    ->  11110 = 0x1e
3211111    ->  10101 = 0x15
322
323We use the encoded values in bytes because we use them to encode
324groups of 4 bytes into groups of 5 bytes, below.
325
326*/
327
328static void gcr_double_2_gcr(UINT8 a, UINT8 b, UINT8 c, UINT8 d, UINT8 *dest)
329{
330   UINT8 gcr[8];
331
332   /* Encode each nibble to 5 bits */
333   gcr[0] = bin_2_gcr[a >> 4];
334   gcr[1] = bin_2_gcr[a & 0x0f];
335   gcr[2] = bin_2_gcr[b >> 4];
336   gcr[3] = bin_2_gcr[b & 0x0f];
337   gcr[4] = bin_2_gcr[c >> 4];
338   gcr[5] = bin_2_gcr[c & 0x0f];
339   gcr[6] = bin_2_gcr[d >> 4];
340   gcr[7] = bin_2_gcr[d & 0x0f];
341
342   /* Re-order the encoded data to only keep the 5 lower bits of each byte */
343   dest[0] = (gcr[0] << 3) | (gcr[1] >> 2);
344   dest[1] = (gcr[1] << 6) | (gcr[2] << 1) | (gcr[3] >> 4);
345   dest[2] = (gcr[3] << 4) | (gcr[4] >> 1);
346   dest[3] = (gcr[4] << 7) | (gcr[5] << 2) | (gcr[6] >> 3);
347   dest[4] = (gcr[6] << 5) | gcr[7];
348}
349
350/*-------------------------------------------------
351    d64_read_track - reads a full track from the
352    disk image
353-------------------------------------------------*/
354
355/*
356
357    Commodore sector format
358
359    SYNC                        FF * 5
360    08
361    CHECKSUM                    sector ^ track ^ id2 ^ id1
362    SECTOR                      0..20 (2040), 0..28 (8050)
363    TRACK                       1..35 (2040), 1..77 (8050), 1..70 (1571)
364    ID2
365    ID1
366    GAP 1                       55 * 9 (2040), 55 * 8 (1541)
367
368    SYNC                        FF * 5
369    07
370    NEXT TRACK
371    NEXT SECTOR
372    254 BYTES OF DATA
373    CHECKSUM
374    GAP 2                       55 * 8..19
375
376*/
377
378static floperr_t d64_read_track(floppy_image *floppy, int head, int track, UINT64 offset, void *buffer, size_t buflen)
379{
380   struct d64dsk_tag *tag = get_tag(floppy);
381   floperr_t err;
382   UINT64 track_offset;
383
384   /* get track offset */
385   err = get_track_offset(floppy, head, track, &track_offset);
386
387   if (err)
388      return err;
389
390   if (track_offset != INVALID_OFFSET)
391   {
392      UINT8 id1 = tag->id1;
393      UINT8 id2 = tag->id2;
394      int sectors_per_track;
395      UINT16 d64_track_size;
396      UINT8 *d64_track_data;
397      UINT16 gcr_track_size;
398      UINT8 *gcr_track_data;
399      UINT64 gcr_pos = 0;
400
401      /* determine logical track number */
402      int dos_track = get_dos_track(track);
403
404      if (tag->dos == DOS25)
405      {
406         dos_track = track + 1;
407      }
408
409      /* logical track numbers continue on the flip side */
410      if (head == 1) dos_track += tag->dos_tracks;
411
412      /* determine number of sectors per track */
413      sectors_per_track = d64_get_sectors_per_track(floppy, head, track);
414
415      /* allocate D64 track data buffer */
416      d64_track_size = sectors_per_track * SECTOR_SIZE;
417      d64_track_data = (UINT8 *)alloca(d64_track_size);
418
419      /* allocate temporary GCR track data buffer */
420      gcr_track_size = sectors_per_track * SECTOR_SIZE_GCR;
421      gcr_track_data = (UINT8 *)alloca(gcr_track_size);
422
423      if (buflen < gcr_track_size) { printf("D64 track buffer too small: %u!", (UINT32)buflen); exit(-1); }
424
425      /* read D64 track data */
426      floppy_image_read(floppy, d64_track_data, track_offset, d64_track_size);
427
428      /* GCR encode D64 sector data */
429      for (int sector = 0; sector < sectors_per_track; sector++)
430      {
431         // here we convert the sector data to gcr directly!
432         // IMPORTANT: errors in reading sectors can modify e.g. header info $01 & $05
433         int sector_error = get_sector_error_code(floppy, head, track, sector);
434
435         /* first we set the position at which sector data starts in the image */
436         UINT64 d64_pos = sector * SECTOR_SIZE;
437         int i;
438
439         /*
440                1. Header sync       FF FF FF FF FF (40 'on' bits, not GCR encoded)
441                2. Header info       52 54 B5 29 4B 7A 5E 95 55 55 (10 GCR bytes)
442                3. Header gap        55 55 55 55 55 55 55 55 55 (9 bytes, never read)
443                4. Data sync         FF FF FF FF FF (40 'on' bits, not GCR encoded)
444                5. Data block        55...4A (325 GCR bytes)
445                6. Inter-sector gap  55 55 55 55...55 55 (4 to 19 bytes, never read)
446            */
447
448         if (sector_error == ERROR_29)
449            id1 ^= 0xff;
450
451         /* Header sync */
452         if (sector_error != ERROR_21)
453         {
454            for (i = 0; i < 5; i++)
455               gcr_track_data[gcr_pos + i] = 0xff;
456            gcr_pos += 5;
457         }
458
459         /* Header info */
460         /* These are 8 bytes unencoded, which become 10 bytes encoded */
461         // $00 - header block ID ($08)                      // this byte can be modified by error code 20 -> 0xff
462         // $01 - header block checksum (EOR of $02-$05)     // this byte can be modified by error code 27 -> ^ 0xff
463         // $02 - Sector# of data block
464         // $03 - Track# of data block
465         UINT8 header_block_id = (sector_error == ERROR_20) ? 0xff : 0x08;
466         UINT8 header_block_checksum = sector ^ dos_track ^ id2 ^ id1;
467
468         if (sector_error == ERROR_27)
469            header_block_checksum ^= 0xff;
470
471         gcr_double_2_gcr(header_block_id, header_block_checksum, sector, dos_track, gcr_track_data + gcr_pos);
472         gcr_pos += 5;
473
474         // $04 - Format ID byte #2
475         // $05 - Format ID byte #1
476         // $06 - $0F ("off" byte)
477         // $07 - $0F ("off" byte)
478         gcr_double_2_gcr(id2, id1, 0x0f, 0x0f, gcr_track_data + gcr_pos);
479         gcr_pos += 5;
480
481         /* Header gap */
482         for (i = 0; i < 9; i++)
483            gcr_track_data[gcr_pos + i] = 0x55;
484         gcr_pos += 9;
485
486         /* Data sync */
487         for (i = 0; i < 5; i++)
488            gcr_track_data[gcr_pos + i] = 0xff;
489         gcr_pos += 5;
490
491         /* Data block */
492         UINT8 data_block_id = (sector_error == ERROR_22) ? 0xff : 0x07;
493
494         // we first need to calculate the checksum of the 256 bytes of the sector
495         UINT8 sector_checksum = d64_track_data[d64_pos];
496         for (i = 1; i < 256; i++)
497            sector_checksum ^= d64_track_data[d64_pos + i];
498
499         if (sector_error == ERROR_23)
500            sector_checksum ^= 0xff;
501
502         /*
503                $00      - data block ID ($07)
504                $01-100  - 256 bytes sector data
505                $101     - data block checksum (EOR of $01-100)
506                $102-103 - $00 ("off" bytes, to make the sector size a multiple of 5)
507            */
508         gcr_double_2_gcr(data_block_id, d64_track_data[d64_pos], d64_track_data[d64_pos + 1], d64_track_data[d64_pos + 2], gcr_track_data + gcr_pos);
509         gcr_pos += 5;
510
511         for (i = 1; i < 64; i++)
512         {
513            gcr_double_2_gcr(d64_track_data[d64_pos + 4 * i - 1], d64_track_data[d64_pos + 4 * i],
514                           d64_track_data[d64_pos + 4 * i + 1], d64_track_data[d64_pos + 4 * i + 2], gcr_track_data + gcr_pos);
515            gcr_pos += 5;
516         }
517
518         gcr_double_2_gcr(d64_track_data[d64_pos + 255], sector_checksum, 0x00, 0x00, gcr_track_data + gcr_pos);
519         gcr_pos += 5;
520
521         /* Inter-sector gap */
522         // "In tests that the author conducted on a real 1541 disk, gap sizes of 8 to 19 bytes were seen."
523         // Here we put 14 as an average...
524         for (i = 0; i < 14; i++)
525            gcr_track_data[gcr_pos + i] = 0x55;
526         gcr_pos += 14;
527      }
528
529      /* copy GCR track data to buffer */
530      memcpy(buffer, gcr_track_data, gcr_track_size);
531   }
532   else   /* half tracks */
533   {
534      /* set track length to 0 */
535      memset(buffer, 0, buflen);
536   }
537
538   return FLOPPY_ERROR_SUCCESS;
539}
540
541/*-------------------------------------------------
542    d64_write_track - writes a full track to the
543    disk image
544-------------------------------------------------*/
545
546static floperr_t d64_write_track(floppy_image *floppy, int head, int track, UINT64 offset, const void *buffer, size_t buflen)
547{
548   return FLOPPY_ERROR_UNSUPPORTED;
549}
550
551/*-------------------------------------------------
552    d64_identify - identifies the disk image
553-------------------------------------------------*/
554
555static void d64_identify(floppy_image *floppy, int *dos, int *heads, int *tracks, bool *has_errors)
556{
557   switch (floppy_image_size(floppy))
558   {
559   /* 2040/3040 */
560   case D67_SIZE_35_TRACKS:            *dos = DOS1;  *heads = 1; *tracks = 35; *has_errors = false; break;
561
562   /* 4040/2031/1541/1551 */
563   case D64_SIZE_35_TRACKS:            *dos = DOS2;  *heads = 1; *tracks = 35; *has_errors = false; break;
564   case D64_SIZE_35_TRACKS_WITH_ERRORS:   *dos = DOS2;  *heads = 1; *tracks = 35; *has_errors = true;  break;
565   case D64_SIZE_40_TRACKS:            *dos = DOS2;  *heads = 1; *tracks = 40; *has_errors = false; break;
566   case D64_SIZE_40_TRACKS_WITH_ERRORS:   *dos = DOS2;  *heads = 1; *tracks = 40; *has_errors = true;  break;
567   case D64_SIZE_42_TRACKS:            *dos = DOS2;  *heads = 1; *tracks = 42; *has_errors = false; break;
568   case D64_SIZE_42_TRACKS_WITH_ERRORS:   *dos = DOS2;  *heads = 1; *tracks = 42; *has_errors = true;  break;
569
570   /* 1571 */
571   case D71_SIZE_70_TRACKS:            *dos = DOS2;  *heads = 2; *tracks = 35; *has_errors = false; break;
572   case D71_SIZE_70_TRACKS_WITH_ERRORS:   *dos = DOS2;  *heads = 2; *tracks = 35; *has_errors = true;  break;
573
574   /* 8050 */
575   case D80_SIZE_77_TRACKS:            *dos = DOS25; *heads = 1; *tracks = 77; *has_errors = false; break;
576
577   /* 8250/SFD1001 */
578   case D82_SIZE_154_TRACKS:            *dos = DOS25; *heads = 2; *tracks = 77; *has_errors = false; break;
579   }
580}
581
582/*-------------------------------------------------
583    FLOPPY_IDENTIFY( d64_dsk_identify )
584-------------------------------------------------*/
585
586FLOPPY_IDENTIFY( d64_dsk_identify )
587{
588   int dos = 0, heads, tracks;
589   bool has_errors = false;
590
591   *vote = 0;
592
593   d64_identify(floppy, &dos, &heads, &tracks, &has_errors);
594
595   if (dos == DOS2 && heads == 1)
596   {
597      *vote = 100;
598   }
599
600   return FLOPPY_ERROR_SUCCESS;
601}
602
603/*-------------------------------------------------
604    FLOPPY_IDENTIFY( d67_dsk_identify )
605-------------------------------------------------*/
606
607FLOPPY_IDENTIFY( d67_dsk_identify )
608{
609   *vote = 0;
610
611   if (floppy_image_size(floppy) == D67_SIZE_35_TRACKS)
612   {
613      *vote = 100;
614   }
615
616   return FLOPPY_ERROR_SUCCESS;
617}
618
619/*-------------------------------------------------
620    FLOPPY_IDENTIFY( d71_dsk_identify )
621-------------------------------------------------*/
622
623FLOPPY_IDENTIFY( d71_dsk_identify )
624{
625   int heads = 0, tracks = 0, dos = -1;
626   bool has_errors = false;
627
628   *vote = 0;
629
630   d64_identify(floppy, &dos, &heads, &tracks, &has_errors);
631
632   if (dos == DOS2 && heads == 2)
633   {
634      *vote = 100;
635   }
636
637   return FLOPPY_ERROR_SUCCESS;
638}
639
640/*-------------------------------------------------
641    FLOPPY_IDENTIFY( d80_dsk_identify )
642-------------------------------------------------*/
643
644FLOPPY_IDENTIFY( d80_dsk_identify )
645{
646   *vote = 0;
647
648   if (floppy_image_size(floppy) == D80_SIZE_77_TRACKS)
649   {
650      *vote = 100;
651   }
652
653   return FLOPPY_ERROR_SUCCESS;
654}
655
656/*-------------------------------------------------
657    FLOPPY_IDENTIFY( d82_dsk_identify )
658-------------------------------------------------*/
659
660FLOPPY_IDENTIFY( d82_dsk_identify )
661{
662   *vote = 0;
663
664   if (floppy_image_size(floppy) == D82_SIZE_154_TRACKS)
665   {
666      *vote = 100;
667   }
668
669   return FLOPPY_ERROR_SUCCESS;
670}
671
672/*-------------------------------------------------
673    FLOPPY_CONSTRUCT( d64_dsk_construct )
674-------------------------------------------------*/
675
676FLOPPY_CONSTRUCT( d64_dsk_construct )
677{
678   struct FloppyCallbacks *callbacks;
679   struct d64dsk_tag *tag;
680   UINT8 id[2];
681
682   int track_offset = 0;
683   int head, track;
684
685   int heads = 0, dos_tracks = 0, dos = 0;
686   bool has_errors = 0;
687   int errors_size = 0;
688
689   if (params)
690   {
691      /* create not supported */
692      return FLOPPY_ERROR_UNSUPPORTED;
693   }
694
695   tag = (struct d64dsk_tag *) floppy_create_tag(floppy, sizeof(struct d64dsk_tag));
696
697   if (!tag) return FLOPPY_ERROR_OUTOFMEMORY;
698
699   /* identify image type */
700   d64_identify(floppy, &dos, &heads, &dos_tracks, &has_errors);
701
702   tag->dos = dos;
703   tag->heads = heads;
704   tag->tracks = MAX_TRACKS;
705   tag->dos_tracks = dos_tracks;
706   tag->has_errors = has_errors;
707
708   if (LOG)
709   {
710      logerror("D64 size: %04x\n", (UINT32)floppy_image_size(floppy));
711      logerror("D64 heads: %u\n", heads);
712      logerror("D64 tracks: %u\n", dos_tracks);
713      logerror("D64 DOS version: %s\n", DOS_VERSION[dos]);
714      logerror("D64 error codes: %s\n", has_errors ? "yes" : "no");
715   }
716
717   /* clear track data offsets */
718   for (head = 0; head < MAX_HEADS; head++)
719   {
720      for (track = 0; track < MAX_TRACKS; track++)
721      {
722         tag->track_offset[head][track] = INVALID_OFFSET;
723      }
724   }
725
726   /* determine track data offsets */
727   for (head = 0; head < heads; head++)
728   {
729      for (track = 0; track < tag->tracks; track++)
730      {
731         if (dos == DOS25)
732         {
733            if (track >= dos_tracks)
734            {
735               /* track out of range */
736               tag->track_offset[head][track] = INVALID_OFFSET;
737            }
738            else
739            {
740               tag->track_offset[head][track] = track_offset;
741               tag->error_offset[head][track] = errors_size;
742
743               track_offset += DOS25_SECTORS_PER_TRACK[track] * SECTOR_SIZE;
744               /* also store an error entry for each sector */
745               errors_size += DOS25_SECTORS_PER_TRACK[track];
746
747               if (LOG)
748               {
749                  logerror("D64 head %u track %u data offset: %04x\n", head, track + 1, tag->track_offset[head][track]);
750                  if (has_errors) logerror("D64 head %u track %u error offset: %04x\n", head, track + 1, tag->error_offset[head][track]);
751               }
752            }
753         }
754         else
755         {
756            if ((track % 2) || ((track / 2) >= dos_tracks))
757            {
758               /* half track or out of range */
759               tag->track_offset[head][track] = INVALID_OFFSET;
760            }
761            else
762            {
763               /* full track */
764               tag->track_offset[head][track] = track_offset;
765               tag->error_offset[head][track] = errors_size;
766
767               if (dos == DOS1)
768               {
769                  track_offset += DOS1_SECTORS_PER_TRACK[track / 2] * SECTOR_SIZE;
770                  /* also store an error entry for each sector */
771                  errors_size += DOS1_SECTORS_PER_TRACK[track / 2];
772               }
773               else
774               {
775                  track_offset += DOS2_SECTORS_PER_TRACK[track / 2] * SECTOR_SIZE;
776                  /* also store an error entry for each sector */
777                  errors_size += DOS2_SECTORS_PER_TRACK[track / 2];
778               }
779
780               if (LOG)
781               {
782                  logerror("D64 head %u track %.1f data offset: %04x\n", head, get_dos_track(track), tag->track_offset[head][track]);
783                  if (has_errors) logerror("D64 head %u track %.1f error offset: %04x\n", head, get_dos_track(track), tag->error_offset[head][track]);
784               }
785            }
786         }
787      }
788   }
789
790   /* determine speed zones */
791   for (track = 0; track < tag->tracks; track++)
792   {
793      if (dos == DOS25)
794      {
795         tag->speed_zone[track] = DOS25_SPEED_ZONE[track];
796
797         if (LOG) logerror("D64 track %u speed zone: %u\n", track + 1, tag->speed_zone[track]);
798      }
799      else
800      {
801         tag->speed_zone[track] = DOS1_SPEED_ZONE[track / 2];
802
803         if (LOG) logerror("D64 track %.1f speed zone: %u\n", get_dos_track(track), tag->speed_zone[track]);
804      }
805   }
806
807   /* read format ID from directory */
808   /*
809    id1, id2 are the same for extended d64 (i.e. with error tables), for d67 and for d71
810
811    for d81 they are at track 40 bytes 0x17 & 0x18
812    for d80 & d82 they are at track 39 bytes 0x18 & 0x19
813    */
814   if (dos == DOS25)
815      floppy_image_read(floppy, id, tag->track_offset[0][38] + 0x18, 2);
816   else
817      floppy_image_read(floppy, id, tag->track_offset[0][34] + 0xa2, 2);
818
819   tag->id1 = id[0];
820   tag->id2 = id[1];
821
822   if (LOG) logerror("D64 format ID: %02x%02x\n", id[0], id[1]);
823
824   /* read errors */
825   if (tag->has_errors)
826   {
827      if (LOG) logerror("D64 error blocks: %u %u\n", errors_size, track_offset);
828      floppy_image_read(floppy, tag->error, track_offset, errors_size);
829   }
830   else
831   {
832      memset(tag->error, ERROR_00, MAX_ERROR_SECTORS);
833   }
834
835   /* set callbacks */
836   callbacks = floppy_callbacks(floppy);
837
838   callbacks->read_track = d64_read_track;
839   callbacks->write_track = d64_write_track;
840   callbacks->get_heads_per_disk = d64_get_heads_per_disk;
841   callbacks->get_tracks_per_disk = d64_get_tracks_per_disk;
842   callbacks->get_track_size = d64_get_track_size;
843
844   return FLOPPY_ERROR_SUCCESS;
845}
src/lib/formats/trd_dsk.h
r0r11458
1/*********************************************************************
2
3    formats/trd_dsk.h
4
5    TRD disk images
6
7*********************************************************************/
8
9#ifndef TRD_DSK_H
10#define TRD_DSK_H
11
12#include "flopimg.h"
13
14/**************************************************************************/
15
16FLOPPY_OPTIONS_EXTERN(trd);
17
18#endif /* TRD_DSK_H */
src/lib/formats/ace_tap.h
r0r11458
1/*********************************************************************
2
3    ace_tap.h
4
5    Format code for Jupiter Ace casette files
6
7*********************************************************************/
8
9#ifndef ACE_TAP_H
10#define ACE_TAP_H
11
12#include "cassimg.h"
13
14typedef struct
15{
16   UINT8 hdr_type;
17   UINT8 hdr_name[10];
18   UINT16 hdr_len;
19   UINT16 hdr_addr;
20   UINT8 hdr_vars[8];
21   UINT8 hdr_3c4c;
22   UINT8 hdr_3c4d;
23   UINT16 dat_len;
24} ace_tape_t;
25
26CASSETTE_FORMATLIST_EXTERN(ace_cassette_formats);
27
28#endif /* ACE_TAP_H */
29
src/lib/formats/g64_dsk.c
r0r11458
1/*********************************************************************
2
3    formats/g64_dsk.c
4
5    Floppy format code for Commodore 1541 GCR disk images
6
7*********************************************************************/
8
9/*
10
11    TODO:
12
13    - speed zones
14    - writing
15
16*/
17
18#define XTAL_16MHz      16000000
19#define XTAL_12MHz      12000000
20#include "g64_dsk.h"
21#include "flopimg.h"
22
23/***************************************************************************
24    PARAMETERS
25***************************************************************************/
26
27#define LOG 0
28
29#define HEADER_LENGTH   0x2ac            /* standard length for 84 half tracks */
30#define MAX_TRACKS      84
31
32/***************************************************************************
33    TYPE DEFINITIONS
34***************************************************************************/
35
36struct g64dsk_tag
37{
38   int version;
39   int heads;                        /* number of physical heads */
40   int tracks;                        /* number of physical tracks */
41   UINT16 track_size;                  /* size of each track */
42   UINT32 track_offset[MAX_TRACKS];      /* offset within data for each track */
43   UINT32 speed_zone_offset[MAX_TRACKS];   /* offset within data for each track */
44};
45
46/***************************************************************************
47    INLINE FUNCTIONS
48***************************************************************************/
49
50INLINE struct g64dsk_tag *get_tag(floppy_image *floppy)
51{
52   return (g64dsk_tag *)floppy_tag(floppy);
53}
54
55INLINE float get_dos_track(int track)
56{
57   return ((float)track / 2) + 1;
58}
59
60/***************************************************************************
61    IMPLEMENTATION
62***************************************************************************/
63
64/*-------------------------------------------------
65    g64_get_heads_per_disk - returns the number
66    of heads in the disk image
67-------------------------------------------------*/
68
69static int g64_get_heads_per_disk(floppy_image *floppy)
70{
71   return get_tag(floppy)->heads;
72}
73
74/*-------------------------------------------------
75    g64_get_tracks_per_disk - returns the number
76    of DOS tracks in the disk image
77-------------------------------------------------*/
78
79static int g64_get_tracks_per_disk(floppy_image *floppy)
80{
81   return get_tag(floppy)->tracks;
82}
83
84/*-------------------------------------------------
85    get_track_offset - returns the offset within
86    the disk image for a given track
87-------------------------------------------------*/
88
89static floperr_t get_track_offset(floppy_image *floppy, int head, int track, UINT64 *offset)
90{
91   struct g64dsk_tag *tag = get_tag(floppy);
92   UINT64 offs = 0;
93
94   if ((track < 0) || (track >= tag->tracks))
95      return FLOPPY_ERROR_SEEKERROR;
96
97   offs = tag->track_offset[track];
98
99   if (offset)
100      *offset = offs;
101
102   return FLOPPY_ERROR_SUCCESS;
103}
104
105/*-------------------------------------------------
106    g64_get_track_size - returns the track size
107-------------------------------------------------*/
108
109static UINT32 g64_get_track_size(floppy_image *floppy, int head, int track)
110{
111   /* get track offset */
112   UINT64 track_offset;
113   floperr_t err = get_track_offset(floppy, head, track, &track_offset);
114
115   if (err)
116      return 0;
117
118   /* read track length */
119   UINT8 size[2];
120   floppy_image_read(floppy, &size, track_offset, 2);
121   UINT32 track_length = (size[1] << 8) | size[0];
122
123   return track_length;
124}
125
126/*-------------------------------------------------
127    g64_read_track - reads a full track from the
128    disk image
129-------------------------------------------------*/
130
131static floperr_t g64_read_track(floppy_image *floppy, int head, int track, UINT64 offset, void *buffer, size_t buflen)
132{
133   struct g64dsk_tag *tag = get_tag(floppy);
134   floperr_t err;
135   UINT64 track_offset;
136   UINT16 track_length = 0;
137
138   /* get track offset */
139   err = get_track_offset(floppy, head, track, &track_offset);
140
141   if (err)
142      return err;
143
144   if (!head && track_offset)
145   {
146      if (buflen < tag->track_size) { printf("G64 track buffer too small: %u!", (UINT32)buflen); exit(-1); }
147
148      /* read track */
149      floppy_image_read(floppy, buffer, track_offset + 2, tag->track_size);
150   }
151   else
152   {
153      /* set track length to 0 */
154      memset(buffer, 0, buflen);
155   }
156
157   if (LOG) logerror("G64 track %.1f length %u\n", get_dos_track(track), track_length);
158
159   return FLOPPY_ERROR_SUCCESS;
160}
161
162/*-------------------------------------------------
163    g64_write_track - writes a full track to the
164    disk image
165-------------------------------------------------*/
166
167static floperr_t g64_write_track(floppy_image *floppy, int head, int track, UINT64 offset, const void *buffer, size_t buflen)
168{
169   return FLOPPY_ERROR_UNSUPPORTED;
170}
171
172/*-------------------------------------------------
173    FLOPPY_IDENTIFY( g64_dsk_identify )
174-------------------------------------------------*/
175
176FLOPPY_IDENTIFY( g64_dsk_identify )
177{
178   UINT8 header[8];
179
180   floppy_image_read(floppy, header, 0, 8);
181
182   if (!strncmp((const char *)header, "GCR-1541", 8))
183   {
184      *vote = 100;
185   }
186   else
187   {
188      *vote = 0;
189   }
190
191   return FLOPPY_ERROR_SUCCESS;
192}
193
194/*-------------------------------------------------
195    FLOPPY_CONSTRUCT( g64_dsk_construct )
196-------------------------------------------------*/
197
198/*
199
200    G64 File Header Format
201
202  Bytes: $0000-0007: File signature "GCR-1541"
203               0008: G64 version (presently only $00 defined)
204               0009: Number of tracks in image (usually $54, decimal 84)
205          000A-000B: Size of each stored track in bytes (usually  7928,  or
206                     $1EF8 in LO/HI format.
207
208  Bytes: $000C-000F: Offset  to  stored  track  1.0  ($000002AC,  in  LO/HI
209                     format, see below for more)
210          0010-0013: Offset to stored track 1.5 ($00000000)
211          0014-0017: Offset to stored track 2.0 ($000021A6)
212             ...
213          0154-0157: Offset to stored track 42.0 ($0004F8B6)
214          0158-015B: Offset to stored track 42.5 ($00000000)
215
216  Bytes: $015C-015F: Speed zone entry for track 1 ($03,  in  LO/HI  format,
217                     see below for more)
218          0160-0163: Speed zone entry for track 1.5 ($03)
219             ...
220          02A4-02A7: Speed zone entry for track 42 ($00)
221          02A8-02AB: Speed zone entry for track 42.5 ($00)
222
223  Bytes: $02AC-02AD: Actual size of stored track (7692 or $1E0C,  in  LO/HI
224                     format)
225          02AE-02AE+$1E0C: Track data
226
227*/
228
229FLOPPY_CONSTRUCT( g64_dsk_construct )
230{
231   struct FloppyCallbacks *callbacks;
232   struct g64dsk_tag *tag;
233   UINT8 header[HEADER_LENGTH];
234   UINT64 pos = 0;
235   int i;
236
237   if (params)
238   {
239      /* create not supported */
240      return FLOPPY_ERROR_UNSUPPORTED;
241   }
242
243   tag = (struct g64dsk_tag *) floppy_create_tag(floppy, sizeof(struct g64dsk_tag));
244
245   if (!tag) return FLOPPY_ERROR_OUTOFMEMORY;
246
247   /* version */
248   floppy_image_read(floppy, header, pos, 0x0c); pos += 0xc;
249   tag->version = header[8];
250   if (LOG) logerror("G64 version: %u\n", tag->version);
251
252   /* number of half tracks */
253   tag->heads = 1;
254   tag->tracks = header[9];
255   if (LOG) logerror("G64 tracks: %u\n", tag->tracks);
256
257   /* size of each stored half track */
258   tag->track_size = (header[11] << 8) | header[10];
259   if (LOG) logerror("G64 track size: %04x\n", tag->track_size);
260
261   /* data offsets */
262   for (i = 0; i < tag->tracks; i++)
263   {
264      floppy_image_read(floppy, header, pos, 4); pos += 4;
265      tag->track_offset[i] = (header[3] << 24) | (header[2] << 16) | (header[1] << 8) | header[0];
266
267      if (LOG) logerror("G64 track %.1f data offset: %04x\n", get_dos_track(i), tag->track_offset[i]);
268   }
269
270   /* speed zone offsets */
271   for (i = 0; i < tag->tracks; i++)
272   {
273      floppy_image_read(floppy, header, pos, 4); pos += 4;
274      tag->speed_zone_offset[i] = (header[3] << 24) | (header[2] << 16) | (header[1] << 8) | header[0];
275
276      if (LOG)
277      {
278         if (tag->speed_zone_offset[i] < 4)
279            logerror("G64 track %.1f speed zone: %u\n", get_dos_track(i), tag->speed_zone_offset[i]);
280         else
281            logerror("G64 track %.1f speed zone offset: %04x\n", get_dos_track(i), tag->speed_zone_offset[i]);
282      }
283   }
284
285   /* set callbacks */
286   callbacks = floppy_callbacks(floppy);
287
288   callbacks->read_track = g64_read_track;
289   callbacks->write_track = g64_write_track;
290   callbacks->get_heads_per_disk = g64_get_heads_per_disk;
291   callbacks->get_tracks_per_disk = g64_get_tracks_per_disk;
292   callbacks->get_track_size = g64_get_track_size;
293
294   return FLOPPY_ERROR_SUCCESS;
295}
src/lib/formats/trs_cas.h
r0r11458
1/*********************************************************************
2
3    trs_cas.h
4
5    Format code for TRS80 (level 2) casette files.
6
7*********************************************************************/
8
9#ifndef __TRS_CAS_H
10#define __TRS_CAS_H
11
12#include "cassimg.h"
13
14CASSETTE_FORMATLIST_EXTERN(trs80l2_cassette_formats);
15
16#endif /* __TRS_CAS_H */
17
src/lib/formats/pc_dsk.c
r0r11458
1/*********************************************************************
2
3    formats/pc_dsk.c
4
5    PC disk images
6
7*********************************************************************/
8
9#include <string.h>
10
11#include "formats/pc_dsk.h"
12#include "formats/basicdsk.h"
13
14struct pc_disk_sizes
15{
16   UINT32 image_size;
17   int sectors;
18   int heads;
19};
20
21
22
23static const struct pc_disk_sizes disk_sizes[] =
24{
25   { 8*1*40*512,  8, 1},   /* 5 1/4 inch double density single sided */
26   { 8*2*40*512,  8, 2},   /* 5 1/4 inch double density */
27   { 9*1*40*512,  9, 1},   /* 5 1/4 inch double density single sided */
28   { 9*2*40*512,  9, 2},   /* 5 1/4 inch double density */
29   {10*2*40*512, 10, 2},   /* 5 1/4 inch double density single sided */
30   { 9*2*80*512,  9, 2},   /* 80 tracks 5 1/4 inch drives rare in PCs */
31   { 9*2*80*512,  9, 2},   /* 3 1/2 inch double density */
32   {15*2*80*512, 15, 2},   /* 5 1/4 inch high density (or japanese 3 1/2 inch high density) */
33   {18*2*80*512, 18, 2},   /* 3 1/2 inch high density */
34   {36*2*80*512, 36, 2}   /* 3 1/2 inch enhanced density */
35};
36
37
38
39static floperr_t pc_dsk_compute_geometry(floppy_image *floppy, struct basicdsk_geometry *geometry)
40{
41   int i;
42   UINT64 size;
43
44   memset(geometry, 0, sizeof(*geometry));
45   size = floppy_image_size(floppy);
46
47   for (i = 0; i < ARRAY_LENGTH(disk_sizes); i++)
48   {
49      if (disk_sizes[i].image_size == size)
50      {
51         geometry->sectors = disk_sizes[i].sectors;
52         geometry->heads = disk_sizes[i].heads;
53         geometry->sector_length = 512;
54         geometry->first_sector_id = 1;
55         geometry->tracks = (int) (size / disk_sizes[i].sectors / disk_sizes[i].heads / geometry->sector_length);
56         return FLOPPY_ERROR_SUCCESS;
57      }
58   }
59
60   if (size >= 0x1a)
61   {
62      /*
63         * get info from boot sector.
64         * not correct on all disks
65         */
66      UINT8 scl, spt, heads;
67      floppy_image_read(floppy, &scl, 0x0c, 1);
68      floppy_image_read(floppy, &spt, 0x18, 1);
69      floppy_image_read(floppy, &heads, 0x1A, 1);
70
71      if (size == ((UINT64) scl) * spt * heads * 0x200)
72      {
73         geometry->sectors = spt;
74         geometry->heads = heads;
75         geometry->sector_length = 512;
76         geometry->first_sector_id = 1;
77         geometry->tracks = scl;
78         return FLOPPY_ERROR_SUCCESS;
79      }
80   }
81
82   return FLOPPY_ERROR_SUCCESS;
83}
84
85
86
87static FLOPPY_IDENTIFY(pc_dsk_identify)
88{
89   floperr_t err;
90   struct basicdsk_geometry geometry;
91
92   err = pc_dsk_compute_geometry(floppy, &geometry);
93   if (err)
94      return err;
95
96   *vote = geometry.heads ? 100 : 0;
97   return FLOPPY_ERROR_SUCCESS;
98}
99
100
101
102static FLOPPY_CONSTRUCT(pc_dsk_construct)
103{
104   floperr_t err;
105   struct basicdsk_geometry geometry;
106
107   if (params)
108   {
109      /* create */
110      memset(&geometry, 0, sizeof(geometry));
111      geometry.heads = option_resolution_lookup_int(params, PARAM_HEADS);
112      geometry.tracks = option_resolution_lookup_int(params, PARAM_TRACKS);
113      geometry.sectors = option_resolution_lookup_int(params, PARAM_SECTORS);
114      geometry.first_sector_id = 1;
115      geometry.sector_length = 512;
116   }
117   else
118   {
119      /* open */
120      err = pc_dsk_compute_geometry(floppy, &geometry);
121      if (err)
122         return err;
123   }
124
125   return basicdsk_construct(floppy, &geometry);
126}
127
128
129
130/* ----------------------------------------------------------------------- */
131
132FLOPPY_OPTIONS_START( pc )
133   FLOPPY_OPTION( pc_dsk, "dsk,ima,img,ufi,360",      "PC floppy disk image",   pc_dsk_identify, pc_dsk_construct, NULL,
134      HEADS([1]-2)
135      TRACKS(40/[80])
136      SECTORS(8/[9]/10/15/18/36))
137FLOPPY_OPTIONS_END
src/lib/formats/d64_dsk.h
r0r11458
1/*********************************************************************
2
3    formats/d64_dsk.h
4
5    Floppy format code for Commodore 1541/2040/8050 disk images
6
7*********************************************************************/
8
9#ifndef __D64_DSK__
10#define __D64_DSK__
11
12#include "flopimg.h"
13
14/***************************************************************************
15    PROTOTYPES
16***************************************************************************/
17
18FLOPPY_IDENTIFY( d64_dsk_identify );
19FLOPPY_IDENTIFY( d67_dsk_identify );
20FLOPPY_IDENTIFY( d71_dsk_identify );
21FLOPPY_IDENTIFY( d80_dsk_identify );
22FLOPPY_IDENTIFY( d82_dsk_identify );
23
24FLOPPY_CONSTRUCT( d64_dsk_construct );
25
26#endif
src/lib/formats/lviv_lvt.c
r0r11458
1/* .LVT tape images */
2#include "lviv_lvt.h"
3
4#define WAVEENTRY_LOW  -32768
5#define WAVEENTRY_HIGH  32767
6
7#define LVIV_LVT_BIT_SAMPLES            60
8#define LVIV_LVT_HEADER_PILOT_SAMPLES      5190*60
9#define LVIV_LVT_HEADER_DATA_SAMPLES      16*11*60
10#define LVIV_LVT_PAUSE_SAMPLES            69370
11#define LVIV_LVT_BLOCK_PILOT_SAMPLES      1298*60
12
13#define LVIV_LVT_HEADER_PILOT_LENGTH      5190
14#define LVIV_LVT_BLOCK_PILOT_LENGTH         1298
15
16static INT16 *lviv_emit_level(INT16 *p, int count, int level)
17{
18   int i;
19
20   for (i=0; i<count; i++)
21   {
22      *(p++) = level;
23   }
24   return p;
25}
26
27static INT16* lviv_output_bit(INT16 *p, UINT8 b)
28{
29   if (b)
30   {
31      p = lviv_emit_level (p, 15, WAVEENTRY_HIGH);
32      p = lviv_emit_level (p, 15, WAVEENTRY_LOW);
33      p = lviv_emit_level (p, 15, WAVEENTRY_HIGH);
34      p = lviv_emit_level (p, 15, WAVEENTRY_LOW);
35   }
36   else
37   {
38      p = lviv_emit_level (p, 30, WAVEENTRY_HIGH);
39      p = lviv_emit_level (p, 30, WAVEENTRY_LOW);
40   }
41       return p;
42}
43
44static INT16* lviv_output_byte(INT16 *p, UINT8 byte)
45{
46   int i;
47
48   p = lviv_output_bit (p, 0);
49
50   for (i=0; i<8; i++)
51      p = lviv_output_bit(p,(byte>>i) & 0x01);
52
53   p = lviv_output_bit (p, 1);
54   p = lviv_output_bit (p, 1);
55   return p;
56}
57
58/*************************************************************************************/
59
60static int lviv_cassette_calculate_size_in_samples(const UINT8 *bytes, int length)
61{
62   int size;
63
64   size =  LVIV_LVT_HEADER_PILOT_SAMPLES +
65      LVIV_LVT_HEADER_DATA_SAMPLES +
66      LVIV_LVT_PAUSE_SAMPLES +
67      LVIV_LVT_BLOCK_PILOT_SAMPLES +
68      (length-0x10)*11*LVIV_LVT_BIT_SAMPLES;
69
70   return size;
71}
72
73/*************************************************************************************/
74
75static int lviv_cassette_fill_wave(INT16 *buffer, int length, UINT8 *bytes)
76{
77   int i;
78   INT16 * p = buffer;
79
80   int data_size;
81
82   for (i=0; i<LVIV_LVT_HEADER_PILOT_LENGTH; i++)
83      p = lviv_output_bit (p, 1);
84
85   for (i=0; i<10; i++)
86      p = lviv_output_byte (p, bytes[0x09]);
87
88   for (i=0; i<6; i++)
89      p = lviv_output_byte (p, bytes[0x0a+i]);
90
91   p = lviv_emit_level (p, LVIV_LVT_PAUSE_SAMPLES, WAVEENTRY_HIGH);
92
93   for (i=0; i<LVIV_LVT_BLOCK_PILOT_LENGTH; i++)
94      p = lviv_output_bit (p, 1);
95
96   data_size = length - ( LVIV_LVT_HEADER_PILOT_SAMPLES +
97                LVIV_LVT_HEADER_DATA_SAMPLES +
98                     LVIV_LVT_PAUSE_SAMPLES +
99                LVIV_LVT_BLOCK_PILOT_SAMPLES );
100   data_size/=660;
101
102   for (i=0; i<data_size; i++)
103      p = lviv_output_byte (p, bytes[0x10+i]);
104
105   return p - buffer;
106}
107
108
109
110static const struct CassetteLegacyWaveFiller lviv_legacy_fill_wave =
111{
112   lviv_cassette_fill_wave,               /* fill_wave */
113   -1,                                 /* chunk_size */
114   0,                                 /* chunk_samples */
115   lviv_cassette_calculate_size_in_samples,   /* chunk_sample_calc */
116   44100,                              /* sample_frequency */
117   0,                                 /* header_samples */
118   0                                 /* trailer_samples */
119};
120
121
122
123static casserr_t lviv_lvt_identify(cassette_image *cassette, struct CassetteOptions *opts)
124{
125   return cassette_legacy_identify(cassette, opts, &lviv_legacy_fill_wave);
126}
127
128
129
130static casserr_t lviv_lvt_load(cassette_image *cassette)
131{
132   return cassette_legacy_construct(cassette, &lviv_legacy_fill_wave);
133}
134
135
136
137static const struct CassetteFormat lviv_lvt_image_format =
138{
139   "lvt",
140   lviv_lvt_identify,
141   lviv_lvt_load,
142   NULL
143};
144
145
146
147CASSETTE_FORMATLIST_START(lviv_lvt_format)
148   CASSETTE_FORMAT(lviv_lvt_image_format)
149CASSETTE_FORMATLIST_END
src/lib/formats/g64_dsk.h
r0r11458
1/*********************************************************************
2
3    formats/g64_dsk.h
4
5    Floppy format code for Commodore 1541 GCR disk images
6
7*********************************************************************/
8
9#ifndef __G64_DSK__
10#define __G64_DSK__
11
12#include "flopimg.h"
13
14/***************************************************************************
15    MACROS / CONSTANTS
16***************************************************************************/
17
18#define G64_SYNC_MARK      0x3ff      /* 10 consecutive 1-bits */
19
20#define G64_BUFFER_SIZE      16384
21
22const int C2040_BITRATE[] =
23{
24   XTAL_16MHz/16,   /* tracks  1-17 */
25   XTAL_16MHz/15,   /* tracks 18-24 */
26   XTAL_16MHz/14,   /* tracks 25-30 */
27   XTAL_16MHz/13   /* tracks 31-42 */
28};
29
30const int C8050_BITRATE[] =
31{
32   XTAL_12MHz/2/16,   /* tracks  1-39 */
33   XTAL_12MHz/2/15,   /* tracks 40-53 */
34   XTAL_12MHz/2/14,   /* tracks 54-65 */
35   XTAL_12MHz/2/13      /* tracks 65-84 */
36};
37
38/***************************************************************************
39    PROTOTYPES
40***************************************************************************/
41
42FLOPPY_IDENTIFY( g64_dsk_identify );
43FLOPPY_CONSTRUCT( g64_dsk_construct );
44
45#endif
src/lib/formats/sorc_dsk.c
r0r11458
1/*********************************************************************
2
3    formats/exi_dsk.c
4
5    Exidy Sorcerer floppy-disk images
6
7*********************************************************************/
8
9#include <string.h>
10
11#include "sorc_dsk.h"
12#include "basicdsk.h"
13
14static FLOPPY_IDENTIFY(exi_dsk_identify)
15{
16   *vote = (floppy_image_size(floppy) == 332640) ? 100 : 0;
17   return FLOPPY_ERROR_SUCCESS;
18}
19
20static int exi_get_heads_per_disk(floppy_image *floppy)
21{
22   return 1;
23}
24
25static int exi_get_tracks_per_disk(floppy_image *floppy)
26{
27   return 77;
28}
29
30static UINT64 exi_translate_offset(floppy_image *floppy, int track, int head, int sector)
31{
32   return 13+270*(16*track+sector);
33}
34
35static floperr_t get_offset(floppy_image *floppy, int head, int track, int sector, int sector_is_index, UINT64 *offset)
36{
37   UINT64 offs;
38   /* translate the sector to a raw sector */
39   if (!sector_is_index)
40   {
41      sector -= 1;
42   }
43   /* check to see if we are out of range */
44   if ((head != 0) || (track < 0) || (track >= 77) || (sector < 0) || (sector >= 16))
45      return FLOPPY_ERROR_SEEKERROR;
46
47   offs = exi_translate_offset(floppy, track, head, sector);
48   if (offset)
49      *offset = offs;
50   return FLOPPY_ERROR_SUCCESS;
51}
52
53
54
55static floperr_t internal_exi_read_sector(floppy_image *floppy, int head, int track, int sector, int sector_is_index, void *buffer, size_t buflen)
56{
57   UINT64 offset;
58   floperr_t err;
59   err = get_offset(floppy, head, track, sector, sector_is_index, &offset);
60   if (err)
61      return err;
62
63   floppy_image_read(floppy, buffer, offset, buflen);
64   return FLOPPY_ERROR_SUCCESS;
65}
66
67
68
69static floperr_t internal_exi_write_sector(floppy_image *floppy, int head, int track, int sector, int sector_is_index, const void *buffer, size_t buflen, int ddam)
70{
71   UINT64 offset;
72   floperr_t err;
73
74   err = get_offset(floppy, head, track, sector, sector_is_index, &offset);
75   if (err)
76      return err;
77
78   floppy_image_write(floppy, buffer, offset, buflen);
79   return FLOPPY_ERROR_SUCCESS;
80}
81
82
83
84static floperr_t exi_read_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen)
85{
86   return internal_exi_read_sector(floppy, head, track, sector, FALSE, buffer, buflen);
87}
88
89static floperr_t exi_write_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam)
90{
91   return internal_exi_write_sector(floppy, head, track, sector, FALSE, buffer, buflen, ddam);
92}
93
94static floperr_t exi_read_indexed_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen)
95{
96   return internal_exi_read_sector(floppy, head, track, sector, TRUE, buffer, buflen);
97}
98
99static floperr_t exi_write_indexed_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam)
100{
101   return internal_exi_write_sector(floppy, head, track, sector, TRUE, buffer, buflen, ddam);
102}
103
104static floperr_t exi_get_sector_length(floppy_image *floppy, int head, int track, int sector, UINT32 *sector_length)
105{
106   floperr_t err;
107   err = get_offset(floppy, head, track, sector, FALSE, NULL);
108   if (err)
109      return err;
110
111   if (sector_length) {
112      *sector_length = 256;
113   }
114   return FLOPPY_ERROR_SUCCESS;
115}
116
117
118
119static floperr_t exi_get_indexed_sector_info(floppy_image *floppy, int head, int track, int sector_index, int *cylinder, int *side, int *sector, UINT32 *sector_length, unsigned long *flags)
120{
121   sector_index += 1;
122   if (cylinder)
123      *cylinder = track;
124   if (side)
125      *side = head;
126   if (sector)
127      *sector = sector_index;
128   if (flags)
129      /* TODO: read DAM or DDAM and determine flags */
130      *flags = 0;
131   return exi_get_sector_length(floppy, head, track, sector_index, sector_length);
132}
133
134
135static FLOPPY_CONSTRUCT(exi_dsk_construct)
136{
137   struct FloppyCallbacks *callbacks;
138   callbacks = floppy_callbacks(floppy);
139   callbacks->read_sector = exi_read_sector;
140   callbacks->write_sector = exi_write_sector;
141   callbacks->read_indexed_sector = exi_read_indexed_sector;
142   callbacks->write_indexed_sector = exi_write_indexed_sector;
143   callbacks->get_sector_length = exi_get_sector_length;
144   callbacks->get_heads_per_disk = exi_get_heads_per_disk;
145   callbacks->get_tracks_per_disk = exi_get_tracks_per_disk;
146   callbacks->get_indexed_sector_info = exi_get_indexed_sector_info;
147
148   return FLOPPY_ERROR_SUCCESS;
149}
150
151
152
153/* ----------------------------------------------------------------------- */
154
155FLOPPY_OPTIONS_START( exidy )
156   FLOPPY_OPTION( exi_dsk, "dsk", "Exidy Sorcerer floppy disk image", exi_dsk_identify, exi_dsk_construct, NULL, NULL)
157FLOPPY_OPTIONS_END
src/lib/formats/thom_dsk.c
r0r11458
1/*********************************************************************
2
3    formats/thom_dsk.c
4
5    Thomson disk images
6
7    Based on work of  Antoine Mine'
8
9*********************************************************************/
10
11#include <string.h>
12#include "thom_dsk.h"
13#include "basicdsk.h"
14
15static const int sap_magic_num = 0xB3; /* simple XOR crypt */
16
17
18static const char sap_header[] =
19   "\001SYSTEME D'ARCHIVAGE PUKALL S.A.P. "
20   "(c) Alexandre PUKALL Avril 1998";
21
22
23static const UINT16 sap_crc[] =
24{
25   0x0000, 0x1081, 0x2102, 0x3183,   0x4204, 0x5285, 0x6306, 0x7387,
26   0x8408, 0x9489, 0xa50a, 0xb58b,   0xc60c, 0xd68d, 0xe70e, 0xf78f,
27};
28
29struct sap_dsk_tag
30{
31   int tracks;
32   int sector_size;
33        int sector_pos[80][16]; /* remember sector position in file */
34};
35
36static UINT16 thom_sap_crc( UINT8* data, int size )
37{
38        int i;
39        UINT16 crc = 0xffff, crc2;
40        for ( i = 0; i < size; i++ )
41        {
42                crc2 = ( crc >> 4 ) ^ sap_crc[ ( crc ^ data[i] ) & 15 ];
43                crc = ( crc2 >> 4 ) ^ sap_crc[ ( crc2 ^ (data[i] >> 4) ) & 15 ];
44        }
45        return crc;
46}
47
48static struct sap_dsk_tag *get_tag(floppy_image *floppy)
49{
50   struct sap_dsk_tag *tag;
51   tag = (sap_dsk_tag *)floppy_tag(floppy);
52   return tag;
53}
54
55
56static FLOPPY_IDENTIFY(sap_dsk_identify)
57{
58   char header[0x100];
59   floppy_image_read(floppy, header, 0, sizeof(sap_header));
60   if (!memcmp( header, sap_header, sizeof(sap_header) ) )
61   {
62      *vote= 100;
63   } else {
64      *vote = 0;
65   }
66   return FLOPPY_ERROR_SUCCESS;
67}
68static int sap_get_heads_per_disk(floppy_image *floppy)
69{
70   return 1;
71}
72
73static int sap_get_tracks_per_disk(floppy_image *floppy)
74{
75   return get_tag(floppy)->tracks;
76}
77
78
79static floperr_t get_offset(floppy_image *floppy, int head, int track, int sector, int sector_is_index, UINT64 *offset)
80{
81   UINT64 offs;
82        struct sap_dsk_tag *tag = get_tag(floppy);
83   /* translate the sector to a raw sector */
84   if (!sector_is_index)
85   {
86      sector -= 1;
87   }
88   /* check to see if we are out of range */
89   if ((head < 0) || (head >= 1) || (track < 0) || (track >=tag->tracks)
90         || (sector < 0) || (sector >= 16))
91      return FLOPPY_ERROR_SEEKERROR;
92
93        offs = tag->sector_pos[track][sector];
94       if (offs <= 0 )
95                return FLOPPY_ERROR_SEEKERROR;
96
97   if (offset)
98      *offset = offs;
99   return FLOPPY_ERROR_SUCCESS;
100}
101
102
103
104static floperr_t internal_sap_read_sector(floppy_image *floppy, int head, int track, int sector, int sector_is_index, void *buffer, size_t buflen)
105{
106   UINT64 offset;
107   floperr_t err;
108   int i;
109   UINT8 *buf;
110   err = get_offset(floppy, head, track, sector, sector_is_index, &offset);
111   if (err)
112      return err;
113
114   floppy_image_read(floppy, buffer, offset+4, buflen);
115   buf = (UINT8*)buffer;
116   for (i=0;i<buflen;i++) {
117      buf[i] ^= sap_magic_num;
118   }
119   return FLOPPY_ERROR_SUCCESS;
120}
121
122
123
124static floperr_t internal_sap_write_sector(floppy_image *floppy, int head, int track, int sector, int sector_is_index, const void *buffer, size_t buflen, int ddam)
125{
126   UINT64 offset;
127   floperr_t err;
128   UINT8 buf[256+6];
129        UINT16 crc;
130   int i;
131   err = get_offset(floppy, head, track, sector, sector_is_index, &offset);
132   if (err)
133      return err;
134
135        /* buf = 4-byte header + sector + 2-byte CRC */
136   floppy_image_read(floppy, buf, offset, 4);
137   for (i=0;i<buflen;i++) {
138      buf[i+4] = ((UINT8*)buffer)[i];
139   }
140        crc = thom_sap_crc( buf, buflen+4 );
141        buf[buflen+4] = crc >> 8;
142        buf[buflen+5] = crc & 0xff;
143   for (i=0;i<buflen;i++) {
144      buf[i+4] ^= sap_magic_num;
145   }
146   floppy_image_write(floppy, buf, offset, buflen+6);
147
148   return FLOPPY_ERROR_SUCCESS;
149}
150
151
152static floperr_t sap_read_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen)
153{
154   return internal_sap_read_sector(floppy, head, track, sector, FALSE, buffer, buflen);
155}
156
157static floperr_t sap_write_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam)
158{
159   return internal_sap_write_sector(floppy, head, track, sector, FALSE, buffer, buflen, ddam);
160}
161
162static floperr_t sap_read_indexed_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen)
163{
164   return internal_sap_read_sector(floppy, head, track, sector, TRUE, buffer, buflen);
165}
166
167static floperr_t sap_write_indexed_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam)
168{
169   return internal_sap_write_sector(floppy, head, track, sector, TRUE, buffer, buflen, ddam);
170}
171
172static floperr_t sap_get_sector_length(floppy_image *floppy, int head, int track, int sector, UINT32 *sector_length)
173{
174   floperr_t err;
175   err = get_offset(floppy, head, track, sector, FALSE, NULL);
176   if (err)
177      return err;
178
179   if (sector_length) {
180      *sector_length = get_tag(floppy)->sector_size;
181   }
182   return FLOPPY_ERROR_SUCCESS;
183}
184
185
186
187static floperr_t sap_get_indexed_sector_info(floppy_image *floppy, int head, int track, int sector_index, int *cylinder, int *side, int *sector, UINT32 *sector_length, unsigned long *flags)
188{
189   floperr_t err;
190   UINT8 header[4];
191   UINT64 offset = 0;
192   sector_index += 1;
193   err = get_offset(floppy, head, track, sector_index, FALSE, &offset);
194
195   floppy_image_read(floppy, header, offset, 4);
196   if (cylinder)
197      *cylinder = header[2];
198   if (side)
199      *side = head;
200   if (sector)
201      *sector = header[3];
202   if (sector_length)
203      *sector_length = get_tag(floppy)->sector_size;
204   if (flags)
205      /* TODO: read DAM or DDAM and determine flags */
206      *flags = 0;
207   return err;
208}
209
210static floperr_t sap_post_format(floppy_image *floppy, option_resolution *params)
211{
212        int track,sector;
213        int pos;
214        UINT8 buf[256], header[4];
215   struct sap_dsk_tag *tag;
216        tag = (struct sap_dsk_tag *) floppy_create_tag(floppy, sizeof(struct sap_dsk_tag));
217
218        /* default options */
219        if ( !tag->tracks )
220        {
221                tag->tracks = 80;
222                tag->sector_size = 256;
223        }
224
225        /* create SAP file header */
226        floppy_image_write( floppy, sap_header, 0, 66 );
227
228        for ( track = 0; track < 80; track++ )
229                for ( sector = 0; sector < 16; sector++ )
230                        tag->sector_pos[track][sector] = 0;
231
232        /* create all sectors with valid header and CRC */
233   memset(buf, 0xe5, 256);
234        pos = 0x42;
235        header[0] = (tag->sector_size==128) ? 1 : 0;
236        header[1] = 0;
237        for ( track = 0, pos = 0x42; track < tag->tracks; track++ )
238                for ( sector = 0; sector < 16; sector++, pos += tag->sector_size + 6 ) {
239                        tag->sector_pos[track][sector] = pos;
240                        header[2] = track;
241                        header[3] = sector + 1;
242                        floppy_image_write(floppy, header, pos, 4);
243                        sap_write_indexed_sector( floppy, 0, track, sector, buf, tag->sector_size, 0 );
244                }
245
246        return FLOPPY_ERROR_SUCCESS;
247}
248
249
250static FLOPPY_CONSTRUCT(sap_dsk_construct)
251{
252   struct FloppyCallbacks *callbacks;
253   struct sap_dsk_tag *tag;
254   int i,j;
255   UINT8 fmt;
256   tag = (struct sap_dsk_tag *) floppy_create_tag(floppy, sizeof(struct sap_dsk_tag));
257   if (!tag)
258      return FLOPPY_ERROR_OUTOFMEMORY;
259
260        /* guess format */
261   floppy_image_read(floppy, &fmt, 0x42, 1);
262   if ( fmt==1 ) tag->sector_size = 128; else tag->sector_size = 256;
263
264        /* start with an empty offset table */
265        tag->tracks = 0;
266        for ( i = 0; i < 80; i++ )
267                for ( j = 0; j < 16; j++ )
268                        tag->sector_pos[i][j] = 0;
269
270        /* count tracks & fill sector offset table */
271   for ( i = 0x42; i+4 < floppy_image_size(floppy); i += tag->sector_size + 6 ) // CRC 2 bytes + 4 bytes sector header
272   {
273                UINT8 sector, track;
274      floppy_image_read(floppy, &track, i+2, 1);
275      floppy_image_read(floppy, &sector, i+3, 1);
276                if ( track >= 80 || sector < 1 || sector > 16 ) continue;
277                if ( track > tag->tracks ) tag->tracks = track+1;
278                tag->sector_pos[track][sector-1] = i;
279   }
280   callbacks = floppy_callbacks(floppy);
281   callbacks->read_sector = sap_read_sector;
282   callbacks->write_sector = sap_write_sector;
283   callbacks->read_indexed_sector = sap_read_indexed_sector;
284   callbacks->write_indexed_sector = sap_write_indexed_sector;
285   callbacks->get_sector_length = sap_get_sector_length;
286   callbacks->get_heads_per_disk = sap_get_heads_per_disk;
287   callbacks->get_tracks_per_disk = sap_get_tracks_per_disk;
288   callbacks->get_indexed_sector_info = sap_get_indexed_sector_info;
289   callbacks->post_format = sap_post_format;
290
291   return FLOPPY_ERROR_SUCCESS;
292}
293
294static FLOPPY_IDENTIFY(qdd_dsk_identify)
295{
296   *vote = (floppy_image_size(floppy) == (51200)) ? 100 : 0;
297   return FLOPPY_ERROR_SUCCESS;
298}
299
300/* fixed interlacing map for QDDs */
301static int thom_qdd_map[400];
302
303static int qdd_translate_sector(floppy_image *floppy, int sector)
304{
305   return thom_qdd_map[sector-1];
306}
307
308static void thom_qdd_compute_map ( void )
309{
310   /* this map is hardcoded in the QDD BIOS */
311   static const int p[6][4] =
312   {
313      { 20,  2, 14,  8 }, { 21, 19, 13,  7 },
314      { 22, 18, 12,  6 }, { 23, 17, 11,  5 },
315      { 24, 16, 10,  4 }, {  1, 15,  9,  3 }
316   };
317   static const int q[4] = { 0, 8, 4, 12 };
318   int t, s;
319   for ( t = 0; t < 24; t++ )
320   {
321      for ( s = 0; s < 16; s++ )
322      {
323         thom_qdd_map[ t*16 + s ] = p[ t/4 ][ s%4 ] * 16 + (s/4) + 4*(t%4);
324      }
325   }
326   for ( s = 0; s < 16; s++ )
327   {
328      thom_qdd_map[ 24*16 + s ] = q[ s%4 ] + (s/4);
329   }
330}
331
332static FLOPPY_CONSTRUCT(qdd_dsk_construct)
333{
334   struct basicdsk_geometry geometry;
335
336   thom_qdd_compute_map();
337
338   memset(&geometry, 0, sizeof(geometry));
339   geometry.heads = 1;
340   geometry.first_sector_id = 1;
341   geometry.sector_length = 128;
342   geometry.tracks = 1;
343   geometry.sectors = 400;
344   geometry.translate_sector =  qdd_translate_sector;
345   return basicdsk_construct(floppy, &geometry);
346}
347
348
349/* ----------------------------------------------------------------------- */
350
351FLOPPY_OPTIONS_START(thomson)
352
353   FLOPPY_OPTION(fdmfm2, "fd", "Thomson FD (MFM) 80 tracks disk image (3\"1/2 DD)", basicdsk_identify_default, basicdsk_construct_default, NULL,
354      HEADS([1])
355      TRACKS([80])
356      SECTORS([16])
357      SECTOR_LENGTH([256])
358      FIRST_SECTOR_ID([1]))
359
360   FLOPPY_OPTION(fdmfm, "fd", "Thomson FD (MFM) 40 tracks disk image (5\"1/4 DD)", basicdsk_identify_default, basicdsk_construct_default, NULL,
361      HEADS([1])
362      TRACKS([40])
363      SECTORS([16])
364      SECTOR_LENGTH([256])
365      FIRST_SECTOR_ID([1]))
366
367   FLOPPY_OPTION(fd2, "fd", "Thomson FD (FM) 80 tracks disk image (3\"1/2 SD)", basicdsk_identify_default, basicdsk_construct_default, NULL,
368      HEADS([1])
369      TRACKS([80])
370      SECTORS([16])
371      SECTOR_LENGTH([128])
372      FIRST_SECTOR_ID([1]))
373
374   FLOPPY_OPTION(fd, "fd", "Thomson FD (FM) 40 tracks disk image (5\"1/4 SD)", basicdsk_identify_default, basicdsk_construct_default, NULL,
375      HEADS([1])
376      TRACKS([40])
377      SECTORS([16])
378      SECTOR_LENGTH([128])
379      FIRST_SECTOR_ID([1]))
380
381        FLOPPY_OPTION(sap,"sap", "Thomson SAP floppy disk image",   sap_dsk_identify, sap_dsk_construct, NULL, NULL)
382
383   FLOPPY_OPTION(qdd,"qd", "Thomson QDD floppy disk image (2\"8 SD)",   qdd_dsk_identify, qdd_dsk_construct, NULL,
384      HEADS([1])
385      TRACKS([1])
386      SECTORS([400])
387      SECTOR_LENGTH([128])
388      FIRST_SECTOR_ID([0]))
389
390FLOPPY_OPTIONS_END
391
src/lib/formats/pc_dsk.h
r0r11458
1/*********************************************************************
2
3    formats/pc_dsk.h
4
5    PC disk images
6
7*********************************************************************/
8
9#ifndef PC_DSK_H
10#define PC_DSK_H
11
12#include "flopimg.h"
13
14
15/**************************************************************************/
16
17FLOPPY_OPTIONS_EXTERN(pc);
18
19#endif /* PC_DSK_H */
src/lib/formats/atarist_dsk.c
r0r11458
1/*********************************************************************
2
3    formats/atarist_dsk.c
4
5    Atari ST disk images
6
7*********************************************************************/
8
9#include "formats/atarist_dsk.h"
10#include "formats/basicdsk.h"
11
12/*
13
14    TODO:
15
16    - MSA format
17    - STT format
18    - DIM format
19
20*/
21
22/***************************************************************************
23    CONSTANTS / MACROS
24***************************************************************************/
25
26#define LOG 0
27
28/***************************************************************************
29    IMPLEMENTATION
30***************************************************************************/
31
32/*-------------------------------------------------
33    FLOPPY_IDENTIFY( atarist_st_identify )
34-------------------------------------------------*/
35
36static FLOPPY_IDENTIFY( atarist_st_identify )
37{
38   *vote = 100;
39
40   return FLOPPY_ERROR_SUCCESS;
41}
42
43/*-------------------------------------------------
44    FLOPPY_CONSTRUCT( atarist_st_construct )
45-------------------------------------------------*/
46
47static FLOPPY_CONSTRUCT( atarist_st_construct )
48{
49   int heads = 0;
50   int tracks = 0;
51   int sectors = 0;
52   UINT8 bootsector[512];
53
54   floppy_image_read(floppy, bootsector, 0, 512);
55   sectors = bootsector[0x18];
56   heads = bootsector[0x1a];
57   tracks = (bootsector[0x13] | (bootsector[0x14] << 8)) / sectors / heads;
58
59   struct basicdsk_geometry geometry;
60   memset(&geometry, 0, sizeof(geometry));
61
62   geometry.heads = heads;
63   geometry.first_sector_id = 1;
64   geometry.sector_length = 512;
65   geometry.tracks = tracks;
66   geometry.sectors = sectors;
67
68   if (LOG) logerror("ST Heads %u Tracks %u Sectors %u\n", heads, tracks, sectors);
69
70   return basicdsk_construct(floppy, &geometry);
71}
72
73/*-------------------------------------------------
74    FLOPPY_CONSTRUCT(atarist_dsk_construct)
75-------------------------------------------------*/
76
77FLOPPY_OPTIONS_START( atarist )
78   FLOPPY_OPTION( atarist, "st", "Atari ST floppy disk image", atarist_st_identify, atarist_st_construct, NULL, NULL )
79/*  FLOPPY_OPTION( atarist, "stt", "Atari ST floppy disk image", atarist_stt_identify, atarist_stt_construct, NULL, NULL )
80    FLOPPY_OPTION( atarist, "msa", "Atari ST floppy disk image", atarist_msa_identify, atarist_msa_construct, NULL, NULL )
81    FLOPPY_OPTION( atarist, "dim", "Atari ST floppy disk image", atarist_dim_identify, atarist_dim_construct, NULL, NULL )*/
82FLOPPY_OPTIONS_END
src/lib/formats/primoptp.c
r0r11458
1/* .PTP Microkey Primo tape images */
2
3
4#include "primoptp.h"
5
6
7#define PRIMO_WAVEENTRY_LOW      -32768
8#define PRIMO_WAVEENTRY_HIGH      32767
9#define PRIMO_WAVEENTRY_ZERO      0
10
11#define PRIMO_WAV_FREQUENCY      22050
12#define PRIMO_BIT_1_PERIOD      (312*2*0.000001)
13#define PRIMO_BIT_0_PERIOD              (3*PRIMO_BIT_1_PERIOD)
14
15#define PRIMO_BIT_1_LENGTH      (PRIMO_BIT_1_PERIOD*PRIMO_WAV_FREQUENCY)
16#define PRIMO_BIT_0_LENGTH      (PRIMO_BIT_0_PERIOD*PRIMO_WAV_FREQUENCY)
17/*
18#define PRIMO_BIT_1_LENGTH      6
19#define PRIMO_BIT_0_LENGTH      16
20*/
21#define PRIMO_PAUSE_LENGTH      2000
22#define PRIMO_FILE_PILOT_LENGTH      ((4*PRIMO_BIT_1_LENGTH + 4*PRIMO_BIT_0_LENGTH)*512)
23#define PRIMO_BLOCK_PILOT_LENGTH   ((8*PRIMO_BIT_1_LENGTH)*96 + (5*PRIMO_BIT_1_LENGTH + 3*PRIMO_BIT_0_LENGTH)*3)
24
25static UINT32 primo_tape_image_length;
26
27static INT16 *primo_emit_level(INT16 *p, int count, int level)
28{
29   int i;
30
31   for (i=0; i<count; i++)   *(p++) = level;
32
33   return p;
34}
35
36static INT16* primo_output_bit(INT16 *p, UINT8 bit)
37{
38   if (bit)
39   {
40      p = primo_emit_level (p, PRIMO_BIT_1_LENGTH/2, PRIMO_WAVEENTRY_HIGH);
41      p = primo_emit_level (p, PRIMO_BIT_1_LENGTH/2, PRIMO_WAVEENTRY_LOW);
42   }
43   else
44   {
45      p = primo_emit_level (p, PRIMO_BIT_0_LENGTH/2, PRIMO_WAVEENTRY_HIGH);
46      p = primo_emit_level (p, PRIMO_BIT_0_LENGTH/2, PRIMO_WAVEENTRY_LOW);
47   }
48       return p;
49}
50
51static INT16* primo_output_byte(INT16 *p, UINT8 byte)
52{
53   int i;
54
55   for (i=0; i<8; i++)
56      p = primo_output_bit(p,(byte>>(7-i)) & 0x01);
57
58   return p;
59}
60
61static UINT32 primo_cassette_calculate_number_of_1(const UINT8 *bytes, UINT16 length)
62{
63   int i,j;
64
65   UINT32 number_of_1 = 0;
66
67   for (i=0; i<length; i++)
68      for (j=0; j<8; j++)
69         if ((bytes[i]>>j)&0x01)
70            number_of_1++;
71
72   return number_of_1;
73}
74
75static int primo_cassette_calculate_size_in_samples(const UINT8 *bytes, int length)
76{
77   int i = 0, j = 0;
78
79   UINT8 *b = (UINT8*) bytes;
80
81   UINT32 file_size = 0;
82   UINT16 block_size = 0;
83
84   UINT32 number_of_1 = 0;
85   UINT32 number_of_0 = 0;
86   UINT32 size_in_samples = 0;
87
88   primo_tape_image_length = length;
89
90   while (i < length)
91   {
92      size_in_samples += PRIMO_PAUSE_LENGTH;
93      logerror ("Samples (pause): %d\n", size_in_samples);
94
95      size_in_samples += PRIMO_FILE_PILOT_LENGTH;
96      logerror ("Samples (file pilot): %d\n", size_in_samples);
97
98      /* file size with header */
99      file_size = *(b+1) + *(b+2)*256;
100      b += 3;
101      logerror ("File size (with header): %d\n", file_size);
102
103      /* b is now set on the first data byte of file
104           it means first byte (type) of block */
105
106      j = 0;
107      while (j < file_size-3)
108      {
109         size_in_samples += PRIMO_BLOCK_PILOT_LENGTH;
110         logerror ("Samples (block pilot): %d\n", size_in_samples);
111
112         /* block size without header but including CRC byte */
113         block_size = *(b+1) + *(b+2)*256;
114
115         /* b is set on the first byte of block data */
116         b += 3;
117
118         number_of_1 = primo_cassette_calculate_number_of_1(b, block_size);
119         number_of_0 = (block_size)*8-number_of_1;
120
121         size_in_samples += number_of_1 * PRIMO_BIT_1_LENGTH;
122         size_in_samples += number_of_0 * PRIMO_BIT_0_LENGTH;
123
124         logerror ("Samples (block data): %d\n", size_in_samples);
125
126         logerror ("\tBlock size: %d\n", block_size);
127
128         b += block_size;
129
130         /* b is set on the first header byte of next block or file */
131
132         j += block_size+3;
133      }
134
135      i += file_size;
136   }
137
138   return size_in_samples;
139}
140
141static int primo_cassette_fill_wave(INT16 *buffer, int length, UINT8 *bytes)
142{
143
144   int i = 0, j = 0, k;
145
146   INT16 *p = buffer;
147   UINT8 *b = bytes;
148
149   UINT32 file_size = 0;
150   UINT16 block_size = 0;
151
152   logerror ("Image size: %d\n", length);
153
154   while (i < primo_tape_image_length)
155   {
156      logerror ("Beginning Primo file\n");
157      /* pause */
158      p = primo_emit_level (p, PRIMO_PAUSE_LENGTH, PRIMO_WAVEENTRY_ZERO);
159
160      logerror ("Samples (pause): %ld\n", (long int)(p-buffer));
161
162      /* file pilot */
163      for (k=0; k<512; k++)
164         p = primo_output_byte (p, 0xaa);
165
166      logerror ("Samples (file pilot): %ld\n", (long int)(p-buffer));
167
168      /* file size with header */
169      file_size = *(b+1) + *(b+2)*256;
170      b += 3;
171
172      logerror ("File size: %d\n", file_size);
173
174      /* b is now set on the first data byte of file
175           it means first byte (block type) of block header */
176
177      j = 0;
178      while (j < file_size-3)
179      {
180
181         /* block pilot */
182         for (k=0; k<96; k++)
183            p = primo_output_byte (p, 0xff);
184         for (k=0; k<3; k++)
185            p = primo_output_byte (p, 0xd3);
186
187         logerror ("Samples (block pilot): %ld\n", (long int)(p-buffer));
188
189         /* block size without header but including CRC byte */
190         block_size = *(b+1) + *(b+2)*256;
191         b += 3;
192
193         for (k=0; k<block_size; k++)
194            p = primo_output_byte (p, *(b+k));
195
196         logerror ("Samples (block data): %ld\n", (long int)(p-buffer));
197
198         b += block_size;
199
200         /* b is now set on the first header byte of next block or file */
201
202         j += block_size+3;
203      }
204
205      logerror ("Primo file finished\n");
206      logerror ("i = %d\n", i);
207      i += file_size;
208      logerror ("i = %d\n", i);
209   }
210
211   logerror ("End of fill_wave/n");
212
213   return p - buffer;
214}
215
216static const struct CassetteLegacyWaveFiller primo_legacy_fill_wave =
217{
218   primo_cassette_fill_wave,         /* fill_wave */
219   -1,                                 /* chunk_size */
220   0,                                 /* chunk_samples */
221   primo_cassette_calculate_size_in_samples,   /* chunk_sample_calc */
222   PRIMO_WAV_FREQUENCY,                              /* sample_frequency */
223   0,                                 /* header_samples */
224   0                                 /* trailer_samples */
225};
226
227static casserr_t primo_ptp_identify(cassette_image *cassette, struct CassetteOptions *opts)
228{
229   return cassette_legacy_identify(cassette, opts, &primo_legacy_fill_wave);
230}
231
232static casserr_t primo_ptp_load(cassette_image *cassette)
233{
234   return cassette_legacy_construct(cassette, &primo_legacy_fill_wave);
235}
236
237static const struct CassetteFormat primo_ptp_image_format =
238{
239   "ptp",
240   primo_ptp_identify,
241   primo_ptp_load,
242   NULL
243};
244
245CASSETTE_FORMATLIST_START(primo_ptp_format)
246   CASSETTE_FORMAT(primo_ptp_image_format)
247CASSETTE_FORMATLIST_END
src/lib/formats/lviv_lvt.h
r0r11458
1/*********************************************************************
2
3    lviv_lvt.h
4
5    Format code for Lviv casette files
6
7*********************************************************************/
8
9#ifndef LVIV_LVT_H
10#define LVIV_LVT_H
11
12#include "cassimg.h"
13
14CASSETTE_FORMATLIST_EXTERN(lviv_lvt_format);
15
16#endif /* LVIV_LVT_H */
src/lib/formats/sorc_dsk.h
r0r11458
1/*********************************************************************
2
3    formats/sorc_dsk.h
4
5    Exidy Sorcerer disk images
6
7*********************************************************************/
8
9#ifndef EXI_DSK_H
10#define EXI_DSK_H
11
12#include "flopimg.h"
13
14/**************************************************************************/
15
16FLOPPY_OPTIONS_EXTERN(exidy);
17
18#endif /* EXI_DSK_H */
src/lib/formats/thom_dsk.h
r0r11458
1#ifndef THOM_DSK
2#define THOM_DSK
3
4#include "flopimg.h"
5
6/* recognized image formats:
7   - .fd    one-side 5"1/4 (single and double density), 3"1/2
8   - .qd    one-side QDD
9   - .sap   one-side double-density 5"1/4, 3"1/2
10*/
11
12FLOPPY_OPTIONS_EXTERN(thomson);
13
14#endif
src/lib/formats/atarist_dsk.h
r0r11458
1/*********************************************************************
2
3    formats/atarist_dsk.h
4
5    atarist disk images
6
7*********************************************************************/
8
9#ifndef ATARIST_DSK_H
10#define ATARIST_DSK_H
11
12#include "flopimg.h"
13
14/**************************************************************************/
15
16FLOPPY_OPTIONS_EXTERN(atarist);
17
18#endif /* ATARIST_DSK_H */
src/lib/formats/primoptp.h
r0r11458
1/*********************************************************************
2
3    primoptp.h
4
5    Format code for Primo casette files
6
7*********************************************************************/
8
9#ifndef PRIMOPTP_H
10#define PRIMOPTP_H
11
12#include "cassimg.h"
13
14CASSETTE_FORMATLIST_EXTERN( primo_ptp_format );
15
16#endif /* PRIMOPTP_H */
src/lib/formats/dsk_dsk.c
r0r11458
1/*********************************************************************
2
3    formats/dsk_dsk.c
4
5    DSK disk images
6
7*********************************************************************/
8
9#include <string.h>
10
11#include "imageutl.h"
12#include "flopimg.h"
13
14#define MV_CPC     "MV - CPC"
15#define EXTENDED   "EXTENDED"
16
17struct dskdsk_tag
18{
19   int disk_image_type;  /* image type: standard or extended */
20   int heads;
21   int tracks;
22   int sector_size;
23   UINT64 track_offsets[84*2]; /* offset within data for each track */
24
25};
26
27
28static struct dskdsk_tag *get_tag(floppy_image *floppy)
29{
30   struct dskdsk_tag *tag;
31   tag = (dskdsk_tag *)floppy_tag(floppy);
32   return tag;
33}
34
35
36
37FLOPPY_IDENTIFY( dsk_dsk_identify )
38{
39   UINT8 header[8];
40
41   floppy_image_read(floppy, header, 0, 8);
42   if ( memcmp( header, MV_CPC, 8 ) ==0) {
43      *vote = 100;
44   } else
45   if ( memcmp( header, EXTENDED, 8 ) ==0) {
46      *vote = 100;
47   } else {
48      *vote = 0;
49   }
50   return FLOPPY_ERROR_SUCCESS;
51}
52
53static int dsk_get_heads_per_disk(floppy_image *floppy)
54{
55   return get_tag(floppy)->heads;
56}
57
58static int dsk_get_tracks_per_disk(floppy_image *floppy)
59{
60   return get_tag(floppy)->tracks;
61}
62
63static UINT64 dsk_get_track_offset(floppy_image *floppy, int head, int track)
64{
65   return get_tag(floppy)->track_offsets[(track<<1) + head];
66}
67
68static floperr_t get_offset(floppy_image *floppy, int head, int track, int sector, int sector_is_index, UINT64 *offset)
69{
70   UINT64 offs;
71   UINT64 track_offset;
72   UINT8 track_info[0x100];
73   UINT8 sectors_per_track;
74   int i;
75   /* translate the sector to a raw sector */
76
77   /* check to see if we are out of range */
78   if ((head < 0) || (head >= get_tag(floppy)->heads) || (track < 0) || (track >= get_tag(floppy)->tracks)
79         || (sector < 0) )
80      return FLOPPY_ERROR_SEEKERROR;
81
82   track_offset = dsk_get_track_offset(floppy, head, track);
83
84   floppy_image_read(floppy, track_info, track_offset, 0x100);
85
86   sectors_per_track = track_info[0x015];
87   if (!sector_is_index) {
88      if (sector >= sectors_per_track) {
89         return FLOPPY_ERROR_SEEKERROR;
90      }
91   }
92
93   if (get_tag(floppy)->disk_image_type==0) {
94      get_tag(floppy)->sector_size = (1<<(track_info[0x014]+7));
95      offs = track_offset + 0x100 +sector * get_tag(floppy)->sector_size;
96   } else {
97      get_tag(floppy)->sector_size = track_info[0x18 + (sector<<3) + 6] + (track_info[0x18+(sector<<3) + 7]<<8);
98      offs = track_offset + 0x100;
99      for (i=0;i<sector;i++) {
100         offs += track_info[0x18 + (i<<3) + 6] + (track_info[0x18+(i<<3) + 7]<<8);
101      }
102   }
103
104   if (offset)
105      *offset = offs;
106   return FLOPPY_ERROR_SUCCESS;
107}
108
109
110
111static floperr_t internal_dsk_read_sector(floppy_image *floppy, int head, int track, int sector, int sector_is_index, void *buffer, size_t buflen)
112{
113   UINT64 offset;
114   floperr_t err;
115   err = get_offset(floppy, head, track, sector, sector_is_index, &offset);
116   if (err)
117      return err;
118   floppy_image_read(floppy, buffer, offset, buflen);
119   return FLOPPY_ERROR_SUCCESS;
120}
121
122
123
124static floperr_t internal_dsk_write_sector(floppy_image *floppy, int head, int track, int sector, int sector_is_index, const void *buffer, size_t buflen, int ddam)
125{
126   UINT64 offset;
127   floperr_t err;
128
129   err = get_offset(floppy, head, track, sector, sector_is_index, &offset);
130   if (err)
131      return err;
132
133   floppy_image_write(floppy, buffer, offset, buflen);
134   return FLOPPY_ERROR_SUCCESS;
135}
136
137
138
139static floperr_t dsk_read_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen)
140{
141   return internal_dsk_read_sector(floppy, head, track, sector, FALSE, buffer, buflen);
142}
143
144static floperr_t dsk_write_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam)
145{
146   return internal_dsk_write_sector(floppy, head, track, sector, FALSE, buffer, buflen, ddam);
147}
148
149static floperr_t dsk_read_indexed_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen)
150{
151   return internal_dsk_read_sector(floppy, head, track, sector, TRUE, buffer, buflen);
152}
153
154static floperr_t dsk_write_indexed_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam)
155{
156   return internal_dsk_write_sector(floppy, head, track, sector, TRUE, buffer, buflen, ddam);
157}
158
159static floperr_t dsk_get_sector_length(floppy_image *floppy, int head, int track, int sector, UINT32 *sector_length)
160{
161   floperr_t err;
162   err = get_offset(floppy, head, track, sector, FALSE, NULL);
163   if (err)
164      return err;
165
166   if (sector_length) {
167      *sector_length = get_tag(floppy)->sector_size;
168   }
169   return FLOPPY_ERROR_SUCCESS;
170}
171
172static floperr_t dsk_get_indexed_sector_info(floppy_image *floppy, int head, int track, int sector_index, int *cylinder, int *side, int *sector, UINT32 *sector_length, unsigned long *flags)
173{
174   floperr_t retVal;
175   UINT64 offset;
176   UINT8 sector_info[0x100];
177   int pos;
178
179   retVal = get_offset(floppy, head, track, sector_index, FALSE, NULL);
180   offset = dsk_get_track_offset(floppy,head,track);
181   pos = 0x18 + (sector_index << 3);
182   floppy_image_read(floppy, sector_info, offset, 0x100);
183   if (cylinder)
184      *cylinder = sector_info[pos + 0];
185   if (side)
186      *side = sector_info[pos + 1];
187   if (sector)
188      *sector = sector_info[pos + 2];
189   if (sector_length) {
190      *sector_length = 1 << (sector_info[pos + 3] + 7);
191   }
192   if (flags)
193      *flags = (sector_info[pos + 5] & 0x40) ? ID_FLAG_DELETED_DATA : 0;
194   return retVal;
195}
196
197
198FLOPPY_CONSTRUCT( dsk_dsk_construct )
199{
200   struct FloppyCallbacks *callbacks;
201   struct dskdsk_tag *tag;
202   UINT8 header[0x100];
203   UINT64 tmp = 0;
204   int i;
205   int skip,cnt;
206
207   if(params)
208   {
209      // create
210      return FLOPPY_ERROR_UNSUPPORTED;
211   }
212
213   floppy_image_read(floppy, header, 0, 0x100);
214
215   tag = (struct dskdsk_tag *) floppy_create_tag(floppy, sizeof(struct dskdsk_tag));
216   if (!tag)
217      return FLOPPY_ERROR_OUTOFMEMORY;
218
219   tag->heads   = header[0x31];
220   if (tag->heads==1) {
221      skip = 2;
222   } else {
223      skip = 1;
224   }
225   tag->tracks  = header[0x30];
226   cnt =0;
227   if ( memcmp( header, MV_CPC, 8 ) ==0) {
228      tag->disk_image_type = 0;
229      tmp = 0x100;
230      for (i=0; i<tag->tracks * tag->heads; i++)
231      {
232         tag->track_offsets[cnt] = tmp;
233         tmp += pick_integer_le(header, 0x32, 2);
234         cnt += skip;
235      }
236   } else  {
237      tag->disk_image_type = 1;
238      tmp = 0x100;
239      for (i=0; i<tag->tracks * tag->heads; i++)
240      {
241         tag->track_offsets[cnt] = tmp;
242         tmp += header[0x34 + i] << 8;
243         cnt += skip;
244      }
245   }
246
247   callbacks = floppy_callbacks(floppy);
248   callbacks->read_sector = dsk_read_sector;
249   callbacks->write_sector = dsk_write_sector;
250   callbacks->read_indexed_sector = dsk_read_indexed_sector;
251   callbacks->write_indexed_sector = dsk_write_indexed_sector;
252   callbacks->get_sector_length = dsk_get_sector_length;
253   callbacks->get_heads_per_disk = dsk_get_heads_per_disk;
254   callbacks->get_tracks_per_disk = dsk_get_tracks_per_disk;
255   callbacks->get_indexed_sector_info = dsk_get_indexed_sector_info;
256   return FLOPPY_ERROR_SUCCESS;
257}
src/lib/formats/cassimg.c
r0r11458
1/*********************************************************************
2
3    cassimg.c
4
5    Cassette tape image abstraction code
6
7*********************************************************************/
8
9
10#include <string.h>
11
12#include "imageutl.h"
13#include "pool.h"
14#include "cassimg.h"
15
16
17/* debugging parameters */
18#define LOG_PUT_SAMPLES         0
19#define DUMP_CASSETTES         0
20
21#define SAMPLES_PER_BLOCK      0x40000
22#define CASSETTE_FLAG_DIRTY      0x10000
23
24
25CASSETTE_FORMATLIST_START(cassette_default_formats)
26CASSETTE_FORMATLIST_END
27
28
29
30/*********************************************************************
31    helper code
32*********************************************************************/
33
34static double map_double(double d, UINT64 low, UINT64 high, UINT64 value)
35{
36#if defined(_MSC_VER) && (_MSC_VER <= 1200)
37   /* casting unsigned __int64 to double is not supported on VC6 or before */
38   return d * (INT64)(value - low) / (INT64)(high - low);
39#else
40   return d * (value - low) / (high - low);
41#endif
42}
43
44
45
46static size_t waveform_bytes_per_sample(int waveform_flags)
47{
48   return (size_t) (1 << ((waveform_flags & 0x06) / 2));
49}
50
51
52
53/*********************************************************************
54    extrapolation and interpolation
55*********************************************************************/
56
57static INT32 extrapolate8(INT8 value)
58{
59   return ((INT32) value) << 24;
60}
61
62static INT32 extrapolate16(INT16 value)
63{
64   return ((INT32) value) << 16;
65}
66
67static INT8 interpolate8(INT32 value)
68{
69   return (INT8) (value >> 24);
70}
71
72static INT16 interpolate16(INT32 value)
73{
74   return (INT16) (value >> 16);
75}
76
77
78
79/*********************************************************************
80    initialization and termination
81*********************************************************************/
82
83static cassette_image *cassette_init(const struct CassetteFormat *format, void *file, const struct io_procs *procs, int flags)
84{
85   cassette_image *cassette;
86
87   cassette = (cassette_image *)malloc(sizeof(struct _cassette_image));
88   if (!cassette)
89      return NULL;
90
91   memset(cassette, 0, sizeof(*cassette));
92   cassette->format = format;
93   cassette->io.file = file;
94   cassette->io.procs = procs;
95   cassette->flags = flags;
96   cassette->pool = pool_alloc_lib(NULL);
97   return cassette;
98}
99
100
101
102static void cassette_finishinit(casserr_t err, cassette_image *cassette, cassette_image **outcassette)
103{
104   if (cassette && (err || !outcassette))
105   {
106      cassette_close(cassette);
107      cassette = NULL;
108   }
109   if (outcassette)
110      *outcassette = cassette;
111}
112
113
114
115static int good_format(const struct CassetteFormat *format, const char *extension, int flags)
116{
117   if (extension && !image_find_extension(format->extensions, extension))
118      return FALSE;
119   if (((flags & CASSETTE_FLAG_READONLY) == 0) && !format->save)
120      return FALSE;
121   return TRUE;
122}
123
124
125
126casserr_t cassette_open_choices(void *file, const struct io_procs *procs, const char *extension,
127   const struct CassetteFormat *const *formats, int flags, cassette_image **outcassette)
128{
129   casserr_t err;
130   cassette_image *cassette;
131   const struct CassetteFormat *format;
132   struct CassetteOptions opts = {0, };
133   int i;
134
135   /* if not specified, use the dummy arguments */
136   if (!formats)
137      formats = cassette_default_formats;
138
139   /* create the cassette object */
140   cassette = cassette_init(NULL, file, procs, flags);
141   if (!cassette)
142   {
143      err = CASSETTE_ERROR_OUTOFMEMORY;
144      goto done;
145   }
146
147   /* identify the image */
148   format = NULL;
149   for (i = 0; !format && formats[i]; i++)
150   {
151      if (good_format(formats[i], extension, flags))
152      {
153         format = formats[i];
154         memset(&opts, 0, sizeof(opts));
155         err = format->identify(cassette, &opts);
156         if (err == CASSETTE_ERROR_INVALIDIMAGE)
157            format = NULL;
158         else if (err)
159            goto done;
160      }
161   }
162
163   /* have we found a proper format */
164   if (!format)
165   {
166      err = CASSETTE_ERROR_INVALIDIMAGE;
167      goto done;
168   }
169   cassette->format = format;
170
171   /* read the options */
172   cassette->channels = opts.channels;
173   cassette->sample_frequency = opts.sample_frequency;
174
175   /* load the image */
176   err = format->load(cassette);
177   if (err)
178      goto done;
179
180   /* success */
181   cassette->flags &= ~CASSETTE_FLAG_DIRTY;
182   err = CASSETTE_ERROR_SUCCESS;
183
184done:
185   cassette_finishinit(err, cassette, outcassette);
186   return err;
187}
188
189
190
191casserr_t cassette_open(void *file, const struct io_procs *procs,
192   const struct CassetteFormat *format, int flags, cassette_image **outcassette)
193{
194   const struct CassetteFormat *formats[2];
195   formats[0] = format;
196   formats[1] = NULL;
197   return cassette_open_choices(file, procs, NULL, formats, flags, outcassette);
198}
199
200
201
202casserr_t cassette_create(void *file, const struct io_procs *procs, const struct CassetteFormat *format,
203   const struct CassetteOptions *opts, int flags, cassette_image **outcassette)
204{
205   casserr_t err;
206   cassette_image *cassette;
207   static const struct CassetteOptions default_options = { 1, 16, 44100 };
208
209   /* cannot create to a read only image */
210   if (flags & CASSETTE_FLAG_READONLY)
211      return CASSETTE_ERROR_INVALIDIMAGE;
212
213   /* is this a good format? */
214   if (!good_format(format, NULL, flags))
215      return CASSETTE_ERROR_INVALIDIMAGE;
216
217   /* normalize arguments */
218   if (!opts)
219      opts = &default_options;
220
221   /* create the cassette object */
222   cassette = cassette_init(format, file, procs, flags);
223   if (!cassette)
224   {
225      err = CASSETTE_ERROR_OUTOFMEMORY;
226      goto done;
227   }
228
229   /* read the options */
230   cassette->channels = opts->channels;
231   cassette->sample_frequency = opts->sample_frequency;
232
233   err = CASSETTE_ERROR_SUCCESS;
234
235done:
236   cassette_finishinit(err, cassette, outcassette);
237   return err;
238}
239
240
241
242static casserr_t cassette_perform_save(cassette_image *cassette)
243{
244   struct CassetteInfo info;
245   cassette_get_info(cassette, &info);
246   return cassette->format->save(cassette, &info);
247}
248
249
250
251casserr_t cassette_save(cassette_image *cassette)
252{
253   casserr_t err;
254
255   if (!cassette->format || !cassette->format->save)
256      return CASSETTE_ERROR_UNSUPPORTED;
257
258   err = cassette_perform_save(cassette);
259   if (err)
260      return err;
261
262   cassette->flags &= ~CASSETTE_FLAG_DIRTY;
263   return CASSETTE_ERROR_SUCCESS;
264}
265
266
267
268void cassette_get_info(cassette_image *cassette, struct CassetteInfo *info)
269{
270   memset(info, 0, sizeof(*info));
271   info->channels = cassette->channels;
272   info->sample_count = cassette->sample_count;
273   info->sample_frequency = cassette->sample_frequency;
274   info->bits_per_sample = (int) waveform_bytes_per_sample(cassette->flags) * 8;
275}
276
277
278
279void cassette_close(cassette_image *cassette)
280{
281   if (cassette)
282   {
283      if ((cassette->flags & CASSETTE_FLAG_DIRTY) && (cassette->flags & CASSETTE_FLAG_SAVEONEXIT))
284         cassette_save(cassette);
285      pool_free_lib(cassette->pool);
286      free(cassette);
287   }
288}
289
290
291
292void cassette_change(cassette_image *cassette, void *file, const struct io_procs *procs, const struct CassetteFormat *format, int flags)
293{
294   if ((flags & CASSETTE_FLAG_READONLY) == 0)
295      flags |= CASSETTE_FLAG_DIRTY;
296   cassette->io.file = file;
297   cassette->io.procs = procs;
298   cassette->format = format;
299   cassette->flags = flags;
300}
301
302
303
304/*********************************************************************
305    calls for accessing the raw cassette image
306*********************************************************************/
307
308void cassette_image_read(cassette_image *cassette, void *buffer, UINT64 offset, size_t length)
309{
310   io_generic_read(&cassette->io, buffer, offset, length);
311}
312
313
314
315void cassette_image_write(cassette_image *cassette, const void *buffer, UINT64 offset, size_t length)
316{
317   io_generic_write(&cassette->io, buffer, offset, length);
318}
319
320
321
322UINT64 cassette_image_size(cassette_image *cassette)
323{
324   return io_generic_size(&cassette->io);
325}
326
327
328
329/*********************************************************************
330    waveform accesses
331*********************************************************************/
332
333struct manipulation_ranges
334{
335   int channel_first;
336   int channel_last;
337   size_t sample_first;
338   size_t sample_last;
339};
340
341
342
343static size_t my_round(double d)
344{
345   size_t result;
346   d += 0.5;
347   result = (size_t) d;
348   return result;
349}
350
351
352
353static casserr_t compute_manipulation_ranges(cassette_image *cassette, int channel,
354   double time_index, double sample_period, struct manipulation_ranges *ranges)
355{
356   if (channel < 0)
357   {
358      ranges->channel_first = 0;
359      ranges->channel_last = cassette->channels - 1;
360   }
361   else
362   {
363      ranges->channel_first = channel;
364      ranges->channel_last = channel;
365   }
366
367   ranges->sample_first = my_round(time_index * cassette->sample_frequency);
368   ranges->sample_last = my_round((time_index + sample_period) * cassette->sample_frequency);
369
370   if (ranges->sample_last > ranges->sample_first)
371      ranges->sample_last--;
372
373   return CASSETTE_ERROR_SUCCESS;
374}
375
376
377
378static casserr_t lookup_sample(cassette_image *cassette, int channel, size_t sample, int allocate, INT32 **ptr)
379{
380   size_t sample_block;
381   size_t sample_index;
382   size_t sample_size;
383   size_t new_block_count;
384   size_t new_block_sample_count;
385   INT32 *new_block;
386   struct sample_block *new_blocks;
387   struct sample_block *block;
388
389   *ptr = NULL;
390   sample_block = (sample / SAMPLES_PER_BLOCK) * cassette->channels + channel;
391   sample_index = sample % SAMPLES_PER_BLOCK;
392   sample_size = sizeof(block->block[0]);
393
394   /* is this block beyond the edge of our waveform? */
395   if (sample_block >= cassette->block_count)
396   {
397      if (!allocate)
398         return CASSETTE_ERROR_SUCCESS;
399
400      /* allocate new blocks */
401      new_block_count = sample_block + 1;
402      new_blocks = (struct sample_block *)pool_realloc_lib(cassette->pool, cassette->blocks, new_block_count * sizeof(cassette->blocks[0]));
403      if (!new_blocks)
404         return CASSETTE_ERROR_OUTOFMEMORY;
405
406      cassette->blocks = new_blocks;
407      memset(&cassette->blocks[cassette->block_count], 0, (new_block_count - cassette->block_count) * sizeof(cassette->blocks[0]));
408      cassette->block_count = new_block_count;
409   }
410
411   block = &cassette->blocks[sample_block];
412
413   /* is this sample access off the current block? */
414   if (sample_index >= block->sample_count)
415   {
416      if (!allocate)
417         return CASSETTE_ERROR_SUCCESS;
418
419      new_block_sample_count = SAMPLES_PER_BLOCK;
420
421      new_block = (INT32*)pool_realloc_lib(cassette->pool, block->block, new_block_sample_count * sample_size);
422      if (!new_block)
423         return CASSETTE_ERROR_OUTOFMEMORY;
424
425      block->block = new_block;
426      memset(&block->block[block->sample_count], 0, (new_block_sample_count - block->sample_count) * sample_size);
427      block->sample_count = new_block_sample_count;
428   }
429
430   *ptr = &block->block[sample_index];
431   return CASSETTE_ERROR_SUCCESS;
432}
433
434
435
436/*********************************************************************
437    waveform accesses
438*********************************************************************/
439
440casserr_t cassette_get_samples(cassette_image *cassette, int channel,
441   double time_index, double sample_period, size_t sample_count, size_t sample_bytes,
442   void *samples, int waveform_flags)
443{
444   casserr_t err;
445   struct manipulation_ranges ranges;
446   size_t sample_index;
447   size_t cassette_sample_index;
448   UINT8 *dest_ptr;
449   const INT32 *source_ptr;
450   double d;
451   INT16 word;
452   INT32 dword;
453   INT64 sum;
454
455//   assert(cassette);
456
457   err = compute_manipulation_ranges(cassette, channel, time_index, sample_period, &ranges);
458   if (err)
459      return err;
460
461   for (sample_index = 0; sample_index < sample_count; sample_index++)
462   {
463      sum = 0;
464
465      for (channel = ranges.channel_first; channel <= ranges.channel_last; channel++)
466      {
467         /* find the sample that we are putting */
468         d = map_double(ranges.sample_last + 1 - ranges.sample_first, 0, sample_count, sample_index) + ranges.sample_first;
469         cassette_sample_index = (size_t) d;
470         err = lookup_sample(cassette, channel, cassette_sample_index, TRUE, (INT32 **) &source_ptr);
471         if (err)
472            return err;
473
474         sum += *source_ptr;
475      }
476
477      /* average out the samples */
478      sum /= (ranges.channel_last + 1 - ranges.channel_first);
479
480      /* and write out the result */
481      dest_ptr = (UINT8*)samples;
482      dest_ptr += waveform_bytes_per_sample(waveform_flags) * sample_index;
483      switch(waveform_bytes_per_sample(waveform_flags))
484      {
485         case 1:
486            *((INT8 *) dest_ptr) = interpolate8(sum);
487            break;
488         case 2:
489            word = interpolate16(sum);
490            if (waveform_flags & CASSETTE_WAVEFORM_ENDIAN_FLIP)
491               word = FLIPENDIAN_INT16(word);
492            *((INT16 *) dest_ptr) = word;
493            break;
494         case 4:
495            dword = sum;
496            if (waveform_flags & CASSETTE_WAVEFORM_ENDIAN_FLIP)
497               dword = FLIPENDIAN_INT32(dword);
498            *((INT32 *) dest_ptr) = dword;
499            break;
500      }
501   }
502   return CASSETTE_ERROR_SUCCESS;
503}
504
505
506
507casserr_t cassette_put_samples(cassette_image *cassette, int channel,
508   double time_index, double sample_period, size_t sample_count, size_t sample_bytes,
509   const void *samples, int waveform_flags)
510{
511   casserr_t err;
512   struct manipulation_ranges ranges;
513   size_t sample_index;
514   INT32 *dest_ptr;
515   INT32 dest_value;
516   INT16 word;
517   INT32 dword;
518   const UINT8 *source_ptr;
519   double d;
520
521   if (!cassette)
522      return CASSETTE_ERROR_SUCCESS;
523
524   if (sample_period == 0)
525      return CASSETTE_ERROR_SUCCESS;
526
527   err = compute_manipulation_ranges(cassette, channel, time_index, sample_period, &ranges);
528   if (err)
529      return err;
530
531   if (cassette->sample_count < ranges.sample_last+1)
532      cassette->sample_count = ranges.sample_last + 1;
533   cassette->flags |= CASSETTE_FLAG_DIRTY;
534
535   if (LOG_PUT_SAMPLES)
536   {
537      logerror("cassette_put_samples(): Putting samples TIME=[%2.6g..%2.6g] INDEX=[%i..%i]\n",
538         time_index,            time_index + sample_period,
539         (int)ranges.sample_first,   (int)ranges.sample_last);
540   }
541
542   for (sample_index = ranges.sample_first; sample_index <= ranges.sample_last; sample_index++)
543   {
544      /* figure out the source pointer */
545      d = map_double(sample_count, ranges.sample_first, ranges.sample_last + 1, sample_index);
546      source_ptr = (const UINT8*)samples;
547      source_ptr += ((size_t) d) * sample_bytes;
548
549      /* compute the value that we are writing */
550      switch(waveform_bytes_per_sample(waveform_flags)) {
551      case 1:
552         dest_value = extrapolate8(*((INT8 *) source_ptr));
553         break;
554      case 2:
555         word = *((INT16 *) source_ptr);
556         if (waveform_flags & CASSETTE_WAVEFORM_ENDIAN_FLIP)
557            word = FLIPENDIAN_INT16(word);
558         dest_value = extrapolate16(word);
559         break;
560      case 4:
561         dword = *((INT32 *) source_ptr);
562         if (waveform_flags & CASSETTE_WAVEFORM_ENDIAN_FLIP)
563            dword = FLIPENDIAN_INT32(dword);
564         dest_value = dword;
565         break;
566      default:
567         return CASSETTE_ERROR_INTERNAL;
568      }
569
570      for (channel = ranges.channel_first; channel <= ranges.channel_last; channel++)
571      {
572         /* find the sample that we are putting */
573         err = lookup_sample(cassette, channel, sample_index, TRUE, &dest_ptr);
574         if (err)
575            return err;
576         *dest_ptr = dest_value;
577      }
578   }
579   return CASSETTE_ERROR_SUCCESS;
580}
581
582
583
584casserr_t cassette_get_sample(cassette_image *cassette, int channel,
585   double time_index, double sample_period, INT32 *sample)
586{
587   return cassette_get_samples(cassette, channel, time_index,
588       sample_period, 1, 0, sample, CASSETTE_WAVEFORM_32BIT);
589}
590
591
592
593casserr_t cassette_put_sample(cassette_image *cassette, int channel,
594   double time_index, double sample_period, INT32 sample)
595{
596   return cassette_put_samples(cassette, channel, time_index,
597       sample_period, 1, 0, &sample, CASSETTE_WAVEFORM_32BIT);
598}
599
600
601
602/*********************************************************************
603    waveform accesses to/from the raw image
604*********************************************************************/
605
606casserr_t cassette_read_samples(cassette_image *cassette, int channels, double time_index,
607   double sample_period, size_t sample_count, UINT64 offset, int waveform_flags)
608{
609   casserr_t err;
610   size_t chunk_sample_count;
611   size_t bytes_per_sample;
612   size_t sample_bytes;
613   size_t samples_loaded = 0;
614   double chunk_time_index;
615   double chunk_sample_period;
616   int channel;
617   UINT8 buffer[8192];
618
619   bytes_per_sample = waveform_bytes_per_sample(waveform_flags);
620   sample_bytes = bytes_per_sample * channels;
621
622   while(samples_loaded < sample_count)
623   {
624      chunk_sample_count = MIN(sizeof(buffer) / sample_bytes, (sample_count - samples_loaded));
625      chunk_sample_period = map_double(sample_period, 0, sample_count, chunk_sample_count);
626      chunk_time_index = time_index + map_double(sample_period, 0, sample_count, samples_loaded);
627
628      cassette_image_read(cassette, buffer, offset, chunk_sample_count * sample_bytes);
629
630      for (channel = 0; channel < channels; channel++)
631      {
632         err = cassette_put_samples(cassette, channel, chunk_time_index, chunk_sample_period,
633            chunk_sample_count, sample_bytes, &buffer[channel * bytes_per_sample], waveform_flags);
634         if (err)
635            return err;
636      }
637
638      offset += chunk_sample_count * sample_bytes;
639      samples_loaded += chunk_sample_count;
640   }
641   return CASSETTE_ERROR_SUCCESS;
642}
643
644
645
646casserr_t cassette_write_samples(cassette_image *cassette, int channels, double time_index,
647   double sample_period, size_t sample_count, UINT64 offset, int waveform_flags)
648{
649   casserr_t err;
650   size_t chunk_sample_count;
651   size_t bytes_per_sample;
652   size_t sample_bytes;
653   size_t samples_saved = 0;
654   double chunk_time_index;
655   double chunk_sample_period;
656   int channel;
657   UINT8 buffer[8192];
658
659   bytes_per_sample = waveform_bytes_per_sample(waveform_flags);
660   sample_bytes = bytes_per_sample * channels;
661
662   while(samples_saved < sample_count)
663   {
664      chunk_sample_count = MIN(sizeof(buffer) / sample_bytes, (sample_count - samples_saved));
665      chunk_sample_period = map_double(sample_period, 0, sample_count, chunk_sample_count);
666      chunk_time_index = time_index + map_double(sample_period, 0, sample_count, samples_saved);
667
668      for (channel = 0; channel < channels; channel++)
669      {
670         err = cassette_get_samples(cassette, channel, chunk_time_index, chunk_sample_period,
671            chunk_sample_count, sample_bytes, &buffer[channel * bytes_per_sample], waveform_flags);
672         if (err)
673            return err;
674      }
675
676      cassette_image_write(cassette, buffer, offset, chunk_sample_count * sample_bytes);
677
678      offset += chunk_sample_count * sample_bytes;
679      samples_saved += chunk_sample_count;
680   }
681   return CASSETTE_ERROR_SUCCESS;
682}
683
684
685
686/*********************************************************************
687    waveform accesses to/from the raw image
688*********************************************************************/
689
690static const INT8 *choose_wave(const struct CassetteModulation *modulation, size_t *wave_bytes_length)
691{
692   static const INT8 square_wave[] = { -128, 127 };
693   static const INT8 sine_wave[] = { 0, 48, 89, 117, 127, 117, 89, 48, 0, -48, -89, -117, -127, -117, -89, -48 };
694
695   if (modulation->flags & CASSETTE_MODULATION_SINEWAVE)
696   {
697      *wave_bytes_length = ARRAY_LENGTH(sine_wave);
698      return sine_wave;
699   }
700   else
701   {
702      *wave_bytes_length = ARRAY_LENGTH(square_wave);
703      return square_wave;
704   }
705}
706
707
708
709casserr_t cassette_modulation_identify(cassette_image *cassette, const struct CassetteModulation *modulation,
710   struct CassetteOptions *opts)
711{
712   size_t wave_bytes_length;
713   choose_wave(modulation, &wave_bytes_length);
714   opts->bits_per_sample = 8;
715   opts->channels = 1;
716   opts->sample_frequency = (UINT32) (MAX(modulation->zero_frequency_high, modulation->one_frequency_high) * wave_bytes_length * 2);
717   return CASSETTE_ERROR_SUCCESS;
718}
719
720
721
722casserr_t cassette_put_modulated_data(cassette_image *cassette, int channel, double time_index,
723   const void *data, size_t data_length, const struct CassetteModulation *modulation,
724   double *time_displacement)
725{
726   casserr_t err;
727   const UINT8 *data_bytes = (const UINT8 *)data;
728   const INT8 *wave_bytes;
729   size_t wave_bytes_length;
730   double total_displacement = 0.0;
731   double pulse_period;
732   double pulse_frequency;
733   UINT8 b;
734   int i;
735
736   wave_bytes = choose_wave(modulation, &wave_bytes_length);
737
738   while(data_length--)
739   {
740      b = *(data_bytes++);
741      for (i = 0; i < 8; i++)
742      {
743         pulse_frequency = (b & (1 << i)) ? modulation->one_frequency_cannonical : modulation->zero_frequency_cannonical;
744         pulse_period = 1 / pulse_frequency;
745         err = cassette_put_samples(cassette, 0, time_index, pulse_period, wave_bytes_length, 1, wave_bytes, CASSETTE_WAVEFORM_8BIT);
746         if (err)
747            goto done;
748         time_index += pulse_period;
749         total_displacement += pulse_period;
750      }
751   }
752   err = CASSETTE_ERROR_SUCCESS;
753
754done:
755   if (time_displacement)
756      *time_displacement = total_displacement;
757   return err;
758}
759
760
761
762casserr_t cassette_put_modulated_filler(cassette_image *cassette, int channel, double time_index,
763   UINT8 filler, size_t filler_length, const struct CassetteModulation *modulation,
764   double *time_displacement)
765{
766   casserr_t err;
767   double delta;
768   double total_displacement = 0.0;
769
770   while(filler_length--)
771   {
772      err = cassette_put_modulated_data(cassette, channel, time_index, &filler, 1, modulation, &delta);
773      if (err)
774         return err;
775      total_displacement += delta;
776      time_index += delta;
777   }
778
779   if (time_displacement)
780      *time_displacement = total_displacement;
781   return CASSETTE_ERROR_SUCCESS;
782}
783
784
785
786casserr_t cassette_read_modulated_data(cassette_image *cassette, int channel, double time_index,
787   UINT64 offset, UINT64 length, const struct CassetteModulation *modulation,
788   double *time_displacement)
789{
790   casserr_t err;
791   UINT8 buffer_stack[1024];
792   UINT8 *buffer;
793   UINT8 *alloc_buffer = NULL;
794   double delta;
795   double total_displacement = 0.0;
796   size_t this_length;
797   size_t buffer_length;
798
799   if (length <= sizeof(buffer_stack))
800   {
801      buffer = buffer_stack;
802      buffer_length = sizeof(buffer_stack);
803   }
804   else
805   {
806      buffer_length = MIN(length, 100000);
807      alloc_buffer = (UINT8*)malloc(buffer_length);
808      if (!alloc_buffer)
809      {
810         err = CASSETTE_ERROR_OUTOFMEMORY;
811         goto done;
812      }
813      buffer = alloc_buffer;
814   }
815
816   while(length > 0)
817   {
818      this_length = (size_t) MIN(length, buffer_length);
819      cassette_image_read(cassette, buffer, offset, this_length);
820
821      err = cassette_put_modulated_data(cassette, channel, time_index, buffer, this_length, modulation, &delta);
822      if (err)
823         goto done;
824      total_displacement += delta;
825      time_index += delta;
826      length -= this_length;
827   }
828
829   if (time_displacement)
830      *time_displacement = total_displacement;
831   err = CASSETTE_ERROR_SUCCESS;
832
833done:
834   if (alloc_buffer)
835      free(alloc_buffer);
836   return err;
837}
838
839
840
841casserr_t cassette_put_modulated_data_bit(cassette_image *cassette, int channel, double time_index,
842   UINT8 data, const struct CassetteModulation *modulation,
843   double *time_displacement)
844{
845   casserr_t err;
846   const INT8 *wave_bytes;
847   size_t wave_bytes_length;
848   double total_displacement = 0.0;
849   double pulse_period;
850   double pulse_frequency;
851
852   wave_bytes = choose_wave(modulation, &wave_bytes_length);
853
854   pulse_frequency = (data) ? modulation->one_frequency_cannonical : modulation->zero_frequency_cannonical;
855   pulse_period = 1 / pulse_frequency;
856   err = cassette_put_samples(cassette, 0, time_index, pulse_period, wave_bytes_length, 1, wave_bytes, CASSETTE_WAVEFORM_8BIT);
857   if (err)
858      goto done;
859   time_index += pulse_period;
860   total_displacement += pulse_period;
861
862   err = CASSETTE_ERROR_SUCCESS;
863
864done:
865   if (time_displacement)
866      *time_displacement = total_displacement;
867   return err;
868}
869
870
871
872/*********************************************************************
873    waveform accesses to/from the raw image
874*********************************************************************/
875
876casserr_t cassette_legacy_identify(cassette_image *cassette, struct CassetteOptions *opts,
877   const struct CassetteLegacyWaveFiller *legacy_args)
878{
879   opts->channels = 1;
880   opts->bits_per_sample = 16;
881   opts->sample_frequency = legacy_args->sample_frequency;
882   return CASSETTE_ERROR_SUCCESS;
883}
884
885
886
887casserr_t cassette_legacy_construct(cassette_image *cassette,
888   const struct CassetteLegacyWaveFiller *legacy_args)
889{
890   casserr_t err;
891   int length;
892   int sample_count;
893   void *bytes = NULL;
894   void *chunk = NULL;
895   INT16 *samples = NULL;
896   int pos = 0;
897   UINT64 offset = 0;
898   UINT64 size;
899   struct CassetteLegacyWaveFiller args;
900
901   /* sanity check the args */
902   //assert(legacy_args->header_samples >= -1);
903   //assert(legacy_args->trailer_samples >= 0);
904   //assert(legacy_args->fill_wave);
905
906   size = cassette_image_size(cassette);
907
908   /* normalize the args */
909   args = *legacy_args;
910   if (args.chunk_size == 0)
911      args.chunk_size = 1;
912   else if (args.chunk_size < 0)
913      args.chunk_size = cassette_image_size(cassette);
914   if (args.sample_frequency == 0)
915      args.sample_frequency = 11025;
916
917   /* allocate a buffer for the binary data */
918   chunk = malloc(args.chunk_size);
919   if (!chunk)
920   {
921      err = CASSETTE_ERROR_OUTOFMEMORY;
922      goto done;
923   }
924
925   /* determine number of samples */
926   if (args.chunk_sample_calc)
927   {
928      if (size > 0x7FFFFFFF)
929      {
930         err = CASSETTE_ERROR_OUTOFMEMORY;
931         goto done;
932      }
933
934      bytes = malloc(size);
935      if (!bytes)
936      {
937         err = CASSETTE_ERROR_OUTOFMEMORY;
938         goto done;
939      }
940      cassette_image_read(cassette, bytes, 0, size);
941      sample_count = args.chunk_sample_calc((const UINT8*)bytes, (int) size);
942
943      if (args.header_samples < 0)
944         args.header_samples = sample_count;
945   }
946   else
947   {
948      sample_count = ((size + args.chunk_size - 1) / args.chunk_size)
949         * args.chunk_samples;
950   }
951   sample_count += args.header_samples + args.trailer_samples;
952
953   /* allocate a buffer for the completed samples */
954   samples = (INT16*)malloc(sample_count * sizeof(*samples));
955   if (!samples)
956   {
957      err = CASSETTE_ERROR_OUTOFMEMORY;
958      goto done;
959   }
960
961   /* if there has to be a header */
962   if (args.header_samples > 0)
963   {
964      length = args.fill_wave(samples + pos, sample_count - pos, CODE_HEADER);
965      if (length < 0)
966      {
967         err = CASSETTE_ERROR_INVALIDIMAGE;
968         goto done;
969      }
970      pos += length;
971   }
972
973   /* convert the file data to samples */
974   while((pos < sample_count) && (offset < size))
975   {
976      cassette_image_read(cassette, chunk, offset, args.chunk_size);
977      offset += args.chunk_size;
978
979      length = args.fill_wave(samples + pos, sample_count - pos, (UINT8*)chunk);
980      if (length < 0)
981      {
982         err = CASSETTE_ERROR_INVALIDIMAGE;
983         goto done;
984      }
985      pos += length;
986      if (length == 0)
987         break;
988   }
989
990   /* if there has to be a trailer */
991   if (args.trailer_samples > 0)
992   {
993      length = args.fill_wave(samples + pos, sample_count - pos, CODE_TRAILER);
994      if (length < 0)
995      {
996         err = CASSETTE_ERROR_INVALIDIMAGE;
997         goto done;
998      }
999      pos += length;
1000   }
1001
1002   /* specify the wave */
1003   err = cassette_put_samples(cassette, 0, 0.0, ((double) pos) / args.sample_frequency,
1004      pos, 2, samples, CASSETTE_WAVEFORM_16BIT);
1005   if (err)
1006      goto done;
1007
1008   /* success! */
1009   err = CASSETTE_ERROR_SUCCESS;
1010
1011#if DUMP_CASSETTES
1012   cassette_dump(cassette, "C:\\TEMP\\CASDUMP.WAV");
1013#endif
1014
1015done:
1016   if (samples)
1017      free(samples);
1018   if (chunk)
1019      free(chunk);
1020   if (bytes)
1021      free(bytes);
1022   return err;
1023}
1024
1025
1026
1027/*********************************************************************
1028    cassette_dump
1029
1030    A debugging call to dump a casette image to a disk based wave file
1031*********************************************************************/
1032
1033void cassette_dump(cassette_image *image, const char *filename)
1034{
1035   FILE *f;
1036   struct io_generic saved_io;
1037   const struct CassetteFormat *saved_format;
1038
1039   f = fopen(filename, "wb");
1040   if (!f)
1041      return;
1042
1043   memcpy(&saved_io, &image->io, sizeof(saved_io));
1044   saved_format = image->format;
1045
1046   image->io.file = f;
1047   image->io.procs = &stdio_ioprocs_noclose;
1048   image->format = &wavfile_format;
1049   cassette_perform_save(image);
1050
1051   memcpy(&image->io, &saved_io, sizeof(saved_io));
1052   image->format = saved_format;
1053
1054   fclose(f);
1055}
1056
1057
src/lib/formats/zx81_p.c
r0r11458
1/*****************************************************************************
2
3Taken from nocash ZX81 docs by Martin Korth.
4
5ZX81 Cassette File Structure
6  5 seconds    pilot
7  1-127 bytes  name (bit7 set in last char)
8  LEN bytes    data, loaded to address 4009h, LEN=(4014h)-4009h.
9  1 pulse      video retrace signal (only if display was enabled)
10The data field contains the system area, the basic program, the video memory,
11and VARS area.
12
13ZX80 Cassette File Structure
14  5 seconds    pilot
15  LEN bytes    data, loaded to address 4000h, LEN=(400Ah)-4000h.
16ZX80 files do not have filenames, and video memory is not included in the file.
17
18Bits and Bytes
19Each byte consists of 8 bits (MSB first) without any start and stop bits,
20directly followed by the next byte. A "0" bit consists of four high pulses,
21a "1" bit of nine pulses, either one followed by a silence period.
22  0:  /\/\/\/\________
23  1:  /\/\/\/\/\/\/\/\/\________
24Each pulse is split into a 150us High period, and 150us Low period. The
25duration of the silence between each bit is 1300us. The baud rate is thus 400
26bps (for a "0" filled area) downto 250 bps (for a "1" filled area). Average
27medium transfer rate is approx. 307 bps (38 bytes/sec) for files that contain
2850% of "0" and "1" bits each.
29
30*****************************************************************************/
31
32#include "zx81_p.h"
33
34
35#define WAVEENTRY_LOW   -32768
36#define WAVEENTRY_HIGH    32767
37#define WAVEENTRY_ZERO        0
38
39#define ZX81_WAV_FREQUENCY   44100
40
41/* all following are in samples */
42#define ZX81_PULSE_LENGTH   16
43#define ZX81_PAUSE_LENGTH   56
44#define ZX81_PILOT_LENGTH   220500
45
46#define ZX81_LOW_BIT_LENGTH   (ZX81_PULSE_LENGTH*4+ZX81_PAUSE_LENGTH)
47#define ZX81_HIGH_BIT_LENGTH   (ZX81_PULSE_LENGTH*9+ZX81_PAUSE_LENGTH)
48
49#define ZX81_START_LOAD_ADDRESS   0x4009
50#define ZX80_START_LOAD_ADDRESS   0x4000
51#define ZX81_DATA_LENGTH_OFFSET   0x0b
52#define ZX80_DATA_LENGTH_OFFSET   0x04
53
54static UINT8 zx_file_name[128];
55static UINT16 real_data_length = 0;
56static UINT8 zx_file_name_length = 0;
57
58/* common functions */
59
60static INT16 *zx81_emit_level(INT16 *p, int count, int level)
61{
62   int i;
63
64   for (i=0; i<count; i++)   *(p++) = level;
65
66   return p;
67}
68
69static INT16* zx81_emit_pulse(INT16 *p)
70{
71   p = zx81_emit_level (p, ZX81_PULSE_LENGTH/8, WAVEENTRY_LOW);
72   p = zx81_emit_level (p, ZX81_PULSE_LENGTH/8, WAVEENTRY_LOW);
73   p = zx81_emit_level (p, ZX81_PULSE_LENGTH/8, WAVEENTRY_ZERO);
74   p = zx81_emit_level (p, ZX81_PULSE_LENGTH/8, WAVEENTRY_HIGH);
75   p = zx81_emit_level (p, ZX81_PULSE_LENGTH/8, WAVEENTRY_HIGH);
76   p = zx81_emit_level (p, ZX81_PULSE_LENGTH/8, WAVEENTRY_ZERO);
77   p = zx81_emit_level (p, ZX81_PULSE_LENGTH/8, WAVEENTRY_LOW);
78   p = zx81_emit_level (p, ZX81_PULSE_LENGTH/8, WAVEENTRY_LOW);
79
80   return p;
81}
82
83static INT16* zx81_emit_pause(INT16 *p)
84{
85   p = zx81_emit_level (p, ZX81_PAUSE_LENGTH, WAVEENTRY_ZERO);
86
87   return p;
88}
89
90static INT16* zx81_output_bit(INT16 *p, UINT8 bit)
91{
92   int i;
93
94   if (bit)
95      for (i=0; i<9; i++)
96         p = zx81_emit_pulse (p);
97   else
98      for (i=0; i<4; i++)
99         p = zx81_emit_pulse (p);
100
101   p = zx81_emit_pause(p);
102
103       return p;
104}
105
106static INT16* zx81_output_byte(INT16 *p, UINT8 byte)
107{
108   int i;
109
110   for (i=0; i<8; i++)
111      p = zx81_output_bit(p,(byte>>(7-i)) & 0x01);
112
113   return p;
114}
115
116static UINT16 zx81_cassette_calculate_number_of_1(const UINT8 *bytes, UINT16 length)
117{
118   UINT16 number_of_1 = 0;
119   int i,j;
120
121   for (i=0; i<length; i++)
122      for (j=0; j<8; j++)
123         if ((bytes[i]>>j)&0x01)
124            number_of_1++;
125
126   return number_of_1;
127}
128
129/* ZX-81 functions */
130
131static const UINT8 zx81_chars[]={
132           0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /*00h-07h*/
133           0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /*08h-0fh*/
134           0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /*10h-17h*/
135           0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /*18h-1fh*/
136           0x00, 0x00, 0x0b, 0x00, 0x0d, 0x00, 0x00, 0x00, /*20h-27h*/
137           0x10, 0x11, 0x17, 0x15, 0x1a, 0x16, 0x1b, 0x18, /*28h-2fh*/
138           0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, /*30h-37h*/
139           0x24, 0x25, 0x0e, 0x19, 0x13, 0x14, 0x12, 0x0f, /*38h-3fh*/
140           0x00, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, /*40h-47h*/
141           0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, /*48h-4fh*/
142           0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, /*50h-57h*/
143           0x3d, 0x3e, 0x3f, 0x00, 0x00, 0x00, 0x00, 0x00, /*58h-5fh*/
144           0x00, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, /*60h-67h*/
145           0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, /*68h-6fh*/
146           0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, /*70h-77h*/
147           0x3d, 0x3e, 0x3f, 0x00, 0x00, 0x00, 0x00, 0x00, /*78h-7fh*/
148};
149
150static void zx81_fill_file_name(const char* name)
151{
152   for (zx_file_name_length=0; (zx_file_name_length<128) && name[zx_file_name_length]; zx_file_name_length++)
153      zx_file_name[zx_file_name_length] = ((UINT8) name[zx_file_name_length]<0x80) ? zx81_chars[(UINT8) name[zx_file_name_length]] : 0x00;
154   zx_file_name[zx_file_name_length-1] |= 0x80;
155}
156
157static int zx81_cassette_calculate_size_in_samples(const UINT8 *bytes, int length)
158{
159   unsigned int number_of_0_data = 0;
160   unsigned int number_of_1_data = 0;
161   unsigned int number_of_0_name = 0;
162   unsigned int number_of_1_name = 0;
163
164   real_data_length = bytes[ZX81_DATA_LENGTH_OFFSET] + bytes[ZX81_DATA_LENGTH_OFFSET+1]*256 - ZX81_START_LOAD_ADDRESS;
165
166   number_of_1_data = zx81_cassette_calculate_number_of_1(bytes, real_data_length);
167   number_of_0_data = length*8-number_of_1_data;
168
169   number_of_1_name = zx81_cassette_calculate_number_of_1(zx_file_name, zx_file_name_length);
170   number_of_0_name = zx_file_name_length*8-number_of_1_name;
171
172   return (number_of_0_data+number_of_0_name)*ZX81_LOW_BIT_LENGTH + (number_of_1_data+number_of_1_name)*ZX81_HIGH_BIT_LENGTH + ZX81_PILOT_LENGTH;
173}
174
175static int zx81_cassette_fill_wave(INT16 *buffer, int length, UINT8 *bytes)
176{
177   INT16 * p = buffer;
178   int i;
179
180   /* pilot */
181   p = zx81_emit_level (p, ZX81_PILOT_LENGTH, WAVEENTRY_ZERO);
182
183   /* name */
184   for (i=0; i<zx_file_name_length; i++)
185      p = zx81_output_byte(p, zx_file_name[i]);
186
187   /* data */
188   for (i=0; i<real_data_length; i++)
189      p = zx81_output_byte(p, bytes[i]);
190
191   return p - buffer;
192}
193
194static const struct CassetteLegacyWaveFiller zx81_legacy_fill_wave =
195{
196   zx81_cassette_fill_wave,         /* fill_wave */
197   -1,                  /* chunk_size */
198   0,                  /* chunk_samples */
199   zx81_cassette_calculate_size_in_samples,   /* chunk_sample_calc */
200   ZX81_WAV_FREQUENCY,            /* sample_frequency */
201   0,                  /* header_samples */
202   0                  /* trailer_samples */
203};
204
205static casserr_t zx81_p_identify(cassette_image *cassette, struct CassetteOptions *opts)
206{
207   return cassette_legacy_identify(cassette, opts, &zx81_legacy_fill_wave);
208}
209
210static casserr_t zx81_p_load(cassette_image *cassette)
211{
212   /* The filename of the file is used to create the wave stream for the emulated machine. Why is this information not
213       part of the image file itself?
214       Hardcoding this to "cassette".
215    */
216   zx81_fill_file_name ("cassette" /*image_basename_noext(device_list_find_by_tag( Machine->config->m_devicelist, CASSETTE, "cassette" ))*/ );
217   return cassette_legacy_construct(cassette, &zx81_legacy_fill_wave);
218}
219
220static const struct CassetteFormat zx81_p_image_format =
221{
222   "p,81",
223   zx81_p_identify,
224   zx81_p_load,
225   NULL
226};
227
228CASSETTE_FORMATLIST_START(zx81_p_format)
229   CASSETTE_FORMAT(zx81_p_image_format)
230CASSETTE_FORMATLIST_END
231
232/* ZX-80 functions */
233
234static int zx80_cassette_calculate_size_in_samples(const UINT8 *bytes, int length)
235{
236   unsigned int number_of_0_data = 0;
237   unsigned int number_of_1_data = 0;
238
239   real_data_length = bytes[ZX80_DATA_LENGTH_OFFSET] + bytes[ZX80_DATA_LENGTH_OFFSET+1]*256 - ZX80_START_LOAD_ADDRESS - 1;
240
241   number_of_1_data = zx81_cassette_calculate_number_of_1(bytes, real_data_length);
242   number_of_0_data = length*8-number_of_1_data;
243
244   return number_of_0_data*ZX81_LOW_BIT_LENGTH + number_of_1_data*ZX81_HIGH_BIT_LENGTH + ZX81_PILOT_LENGTH;
245}
246
247static int zx80_cassette_fill_wave(INT16 *buffer, int length, UINT8 *bytes)
248{
249   INT16 * p = buffer;
250   int i;
251
252   /* pilot */
253   p = zx81_emit_level (p, ZX81_PILOT_LENGTH, WAVEENTRY_ZERO);
254
255   /* data */
256   for (i=0; i<real_data_length; i++)
257      p = zx81_output_byte(p, bytes[i]);
258
259   return p - buffer;
260}
261
262static const struct CassetteLegacyWaveFiller zx80_legacy_fill_wave =
263{
264   zx80_cassette_fill_wave,         /* fill_wave */
265   -1,                                 /* chunk_size */
266   0,                                 /* chunk_samples */
267   zx80_cassette_calculate_size_in_samples,   /* chunk_sample_calc */
268   ZX81_WAV_FREQUENCY,                              /* sample_frequency */
269   0,                                 /* header_samples */
270   0                                 /* trailer_samples */
271};
272
273static casserr_t zx80_o_identify(cassette_image *cassette, struct CassetteOptions *opts)
274{
275   return cassette_legacy_identify(cassette, opts, &zx80_legacy_fill_wave);
276}
277
278static casserr_t zx80_o_load(cassette_image *cassette)
279{
280   return cassette_legacy_construct(cassette, &zx80_legacy_fill_wave);
281}
282
283static const struct CassetteFormat zx80_o_image_format =
284{
285   "o,80",
286   zx80_o_identify,
287   zx80_o_load,
288   NULL
289};
290
291CASSETTE_FORMATLIST_START(zx80_o_format)
292   CASSETTE_FORMAT(zx80_o_image_format)
293CASSETTE_FORMATLIST_END
src/lib/formats/vt_dsk.c
r0r11458
1/*********************************************************************
2
3    formats/vt_dsk.c
4
5    VTech Laser/VZ disk images
6
7*********************************************************************/
8
9#include <stdio.h>
10#include <string.h>
11#include "formats/vt_dsk.h"
12#include "formats/basicdsk.h"
13
14static FLOPPY_IDENTIFY(vz_identify)
15{
16   UINT64 size = floppy_image_size(floppy);
17   *vote = ((size == 98560) || (size == 99200) || (size == 99184)) ? 100 : 0;
18   return FLOPPY_ERROR_SUCCESS;
19}
20
21static FLOPPY_CONSTRUCT(vz_construct)
22{
23   struct basicdsk_geometry geometry;
24   memset(&geometry, 0, sizeof(geometry));
25
26   if (params)
27   {
28      geometry.heads           = option_resolution_lookup_int(params, PARAM_HEADS);
29      geometry.tracks          = option_resolution_lookup_int(params, PARAM_TRACKS);
30      geometry.sectors         = option_resolution_lookup_int(params, PARAM_SECTORS);
31      geometry.first_sector_id = option_resolution_lookup_int(params, PARAM_FIRST_SECTOR_ID);
32      geometry.sector_length   = option_resolution_lookup_int(params, PARAM_SECTOR_LENGTH);
33   }
34   else
35   {
36      geometry.heads           = 1;
37      geometry.tracks          = 40;
38      geometry.sectors         = 16;
39      geometry.first_sector_id = 0;
40      geometry.sector_length   = floppy_image_size(floppy)/geometry.tracks/geometry.sectors;
41   }
42
43   return basicdsk_construct(floppy, &geometry);
44}
45
46
47FLOPPY_OPTIONS_START(vz)
48   FLOPPY_OPTION(vtech1, "dsk", "Laser/VZ disk image", vz_identify, vz_construct, NULL,
49      HEADS([1])
50      TRACKS([40])
51      SECTORS([16])
52      SECTOR_LENGTH([154])
53      FIRST_SECTOR_ID([0]))
54FLOPPY_OPTIONS_END
src/lib/formats/cassimg.h
r0r11458
1/*********************************************************************
2
3    cassimg.h
4
5    Cassette tape image abstraction code
6
7*********************************************************************/
8
9#ifndef CASSIMG_H
10#define CASSIMG_H
11
12#include "osdcore.h"
13#include "pool.h"
14#include "ioprocs.h"
15
16#ifndef logerror
17#define logerror printf
18#endif
19
20/***************************************************************************
21
22    Constants
23
24***************************************************************************/
25
26#define CASSETTE_FLAG_READWRITE         0
27#define CASSETTE_FLAG_READONLY         1
28#define CASSETTE_FLAG_NOSAVEONEXIT      0
29#define CASSETTE_FLAG_SAVEONEXIT      2
30
31#define CASSETTE_WAVEFORM_8BIT         0
32#define CASSETTE_WAVEFORM_16BIT         2
33#define CASSETTE_WAVEFORM_16BIT_FLIP   3
34#define CASSETTE_WAVEFORM_32BIT         4
35#define CASSETTE_WAVEFORM_32BIT_FLIP   5
36#define CASSETTE_WAVEFORM_ENDIAN_FLIP   1
37
38#define CASSETTE_MODULATION_SQUAREWAVE   0
39#define CASSETTE_MODULATION_SINEWAVE   1
40
41
42#ifdef LSB_FIRST
43#define CASSETTE_WAVEFORM_16BITBE      CASSETTE_WAVEFORM_16BIT_FLIP
44#define CASSETTE_WAVEFORM_16BITLE      CASSETTE_WAVEFORM_16BIT
45#define CASSETTE_WAVEFORM_32BITBE      CASSETTE_WAVEFORM_32BIT_FLIP
46#define CASSETTE_WAVEFORM_32BITLE      CASSETTE_WAVEFORM_32BIT
47#else
48#define CASSETTE_WAVEFORM_16BITBE      CASSETTE_WAVEFORM_16BIT
49#define CASSETTE_WAVEFORM_16BITLE      CASSETTE_WAVEFORM_16BIT_FLIP
50#define CASSETTE_WAVEFORM_32BITBE      CASSETTE_WAVEFORM_32BIT
51#define CASSETTE_WAVEFORM_32BITLE      CASSETTE_WAVEFORM_32BIT_FLIP
52#endif
53
54
55/***************************************************************************
56
57    Type definitions
58
59***************************************************************************/
60
61typedef enum
62{
63   CASSETTE_ERROR_SUCCESS,         /* no error */
64   CASSETTE_ERROR_INTERNAL,      /* fatal internal error */
65   CASSETTE_ERROR_UNSUPPORTED,      /* this operation is unsupported */
66   CASSETTE_ERROR_OUTOFMEMORY,      /* ran out of memory */
67   CASSETTE_ERROR_INVALIDIMAGE,   /* invalid image */
68   CASSETTE_ERROR_INVALIDARG
69}
70casserr_t;
71
72struct sample_block
73{
74   INT32 *block;
75   size_t sample_count;
76};
77
78struct CassetteOptions
79{
80   int channels;
81   int bits_per_sample;
82   UINT32 sample_frequency;
83};
84
85struct CassetteInfo
86{
87   int channels;
88   int bits_per_sample;
89   UINT32 sample_frequency;
90   size_t sample_count;
91};
92
93struct _cassette_image
94{
95   const struct CassetteFormat *format;
96   struct io_generic io;
97   object_pool *pool;
98
99   int channels;
100   int flags;
101   UINT32 sample_frequency;
102
103   struct sample_block *blocks;
104   size_t block_count;
105   size_t sample_count;
106};
107
108typedef struct _cassette_image cassette_image;
109
110struct CassetteFormat
111{
112   const char *extensions;
113   casserr_t (*identify)(cassette_image *cassette, struct CassetteOptions *opts);
114   casserr_t (*load)(cassette_image *cassette);
115   casserr_t (*save)(cassette_image *cassette, const struct CassetteInfo *info);
116};
117
118/* used for the core modulation code */
119struct CassetteModulation
120{
121   int flags;
122   double zero_frequency_low;
123   double zero_frequency_cannonical;
124   double zero_frequency_high;
125   double one_frequency_low;
126   double one_frequency_cannonical;
127   double one_frequency_high;
128};
129
130/* code to adapt existing legacy fill_wave functions */
131struct CassetteLegacyWaveFiller
132{
133   int (*fill_wave)(INT16 *, int, UINT8 *);
134   int chunk_size;
135   int chunk_samples;
136   int (*chunk_sample_calc)(const UINT8 *bytes, int length);
137   UINT32 sample_frequency;
138   int header_samples;
139   int trailer_samples;
140};
141
142/* builtin formats */
143extern const struct CassetteFormat wavfile_format;
144
145/* macros for specifying format lists */
146#define CASSETTE_FORMATLIST_EXTERN(name)   \
147   extern const struct CassetteFormat *const name[]
148
149#define CASSETTE_FORMATLIST_START(name)      \
150   const struct CassetteFormat *const name[] =   \
151   {                              \
152      &wavfile_format,               \
153
154#define CASSETTE_FORMAT(name)            \
155      &(name),                     \
156
157#define CASSETTE_FORMATLIST_END            \
158      NULL                        \
159   };
160
161CASSETTE_FORMATLIST_EXTERN(cassette_default_formats);
162
163/***************************************************************************
164
165    Prototypes
166
167***************************************************************************/
168
169casserr_t cassette_open(void *file, const struct io_procs *procs,
170   const struct CassetteFormat *format, int flags, cassette_image **outcassette);
171casserr_t cassette_open_choices(void *file, const struct io_procs *procs, const char *extension,
172   const struct CassetteFormat *const *formats, int flags, cassette_image **outcassette);
173casserr_t cassette_create(void *file, const struct io_procs *procs, const struct CassetteFormat *format,
174   const struct CassetteOptions *opts, int flags, cassette_image **outcassette);
175casserr_t cassette_save(cassette_image *cassette);
176void cassette_close(cassette_image *cassette);
177void cassette_change(cassette_image *cassette, void *file, const struct io_procs *procs, const struct CassetteFormat *format, int flags);
178void cassette_get_info(cassette_image *cassette, struct CassetteInfo *info);
179
180/* calls for accessing the raw cassette image */
181void cassette_image_read(cassette_image *cassette, void *buffer, UINT64 offset, size_t length);
182void cassette_image_write(cassette_image *cassette, const void *buffer, UINT64 offset, size_t length);
183UINT64 cassette_image_size(cassette_image *cassette);
184
185/* waveform accesses */
186casserr_t cassette_get_samples(cassette_image *cassette, int channel,
187   double time_index, double sample_period, size_t sample_count, size_t sample_bytes,
188   void *samples, int waveform_flags);
189casserr_t cassette_put_samples(cassette_image *cassette, int channel,
190   double time_index, double sample_period, size_t sample_count, size_t sample_bytes,
191   const void *samples, int waveform_flags);
192casserr_t cassette_get_sample(cassette_image *cassette, int channel,
193   double time_index, double sample_period, INT32 *sample);
194casserr_t cassette_put_sample(cassette_image *cassette, int channel,
195   double time_index, double sample_period, INT32 sample);
196
197/* waveform accesses to/from the raw image */
198casserr_t cassette_read_samples(cassette_image *cassette, int channels, double time_index,
199   double sample_period, size_t sample_count, UINT64 offset, int waveform_flags);
200casserr_t cassette_write_samples(cassette_image *cassette, int channels, double time_index,
201   double sample_period, size_t sample_count, UINT64 offset, int waveform_flags);
202
203/* modulation support */
204casserr_t cassette_modulation_identify(cassette_image *cassette, const struct CassetteModulation *modulation,
205   struct CassetteOptions *opts);
206casserr_t cassette_put_modulated_data(cassette_image *cassette, int channel, double time_index,
207   const void *data, size_t data_length, const struct CassetteModulation *modulation,
208   double *time_displacement);
209casserr_t cassette_put_modulated_filler(cassette_image *cassette, int channel, double time_index,
210   UINT8 filler, size_t filler_length, const struct CassetteModulation *modulation,
211   double *time_displacement);
212casserr_t cassette_read_modulated_data(cassette_image *cassette, int channel, double time_index,
213   UINT64 offset, UINT64 length, const struct CassetteModulation *modulation,
214   double *time_displacement);
215casserr_t cassette_put_modulated_data_bit(cassette_image *cassette, int channel, double time_index,
216   UINT8 data, const struct CassetteModulation *modulation,
217   double *time_displacement);
218
219/* debug calls */
220void cassette_dump(cassette_image *image, const char *filename);
221
222/* legacy code support */
223#define CODE_HEADER    ((UINT8*)-1)
224#define CODE_TRAILER   ((UINT8*)-2)
225casserr_t cassette_legacy_identify(cassette_image *cassette, struct CassetteOptions *opts,
226   const struct CassetteLegacyWaveFiller *legacy_args);
227casserr_t cassette_legacy_construct(cassette_image *cassette,
228   const struct CassetteLegacyWaveFiller *legacy_args);
229
230#endif /* CASSIMG_H */
src/lib/formats/zx81_p.h
r0r11458
1/*********************************************************************
2
3    zx81_p.h
4
5    Format code for ZX-81 casette files
6
7*********************************************************************/
8
9#ifndef ZX81_P_H
10#define ZX81_P_H
11
12#include "cassimg.h"
13
14CASSETTE_FORMATLIST_EXTERN(zx81_p_format);
15CASSETTE_FORMATLIST_EXTERN(zx80_o_format);
16
17#endif /* ZX81_P_H */
src/lib/formats/cpis_dsk.c
r0r11458
1/*********************************************************************
2
3    formats/cpis_dsk.c
4
5    Telenova Compis disk images
6
7*********************************************************************/
8
9#include <string.h>
10
11#include "formats/cpis_dsk.h"
12#include "formats/basicdsk.h"
13
14
15static int compis_get_tracks_and_sectors(floppy_image *floppy, int *tracks, int *sectors)
16{
17   switch(floppy_image_size(floppy)) {
18   case 0x50000:   /* 320 KB */
19      *tracks = 40;
20      *sectors = 8;
21      break;
22
23   case 0x5a000:   /* 360 KB */
24      *tracks = 40;
25      *sectors = 9;
26      break;
27
28   case 0xa0000:   /* 640 KB */
29      *tracks = 80;
30      *sectors = 8;
31      break;
32
33   case 0xb4000:   /* 720 KB */
34      *tracks = 80;
35      *sectors = 9;
36      break;
37
38   case 0x12c000:   /* 1200 KB */
39      *tracks = 80;
40      *sectors = 15;
41      break;
42
43   default:
44      return 0;
45   }
46   return 1;
47}
48
49
50
51static FLOPPY_IDENTIFY(compis_dsk_identify)
52{
53   int dummy;
54   *vote = compis_get_tracks_and_sectors(floppy, &dummy, &dummy) ? 100 : 0;
55   return FLOPPY_ERROR_SUCCESS;
56}
57
58
59
60static FLOPPY_CONSTRUCT(compis_dsk_construct)
61{
62   struct basicdsk_geometry geometry;
63
64   memset(&geometry, 0, sizeof(geometry));
65   geometry.heads = 1;
66   geometry.first_sector_id = 1;
67   geometry.sector_length = 512;
68
69   if (params)
70   {
71      /* create */
72      geometry.tracks = option_resolution_lookup_int(params, PARAM_TRACKS);
73      geometry.sectors = option_resolution_lookup_int(params, PARAM_SECTORS);
74   }
75   else
76   {
77      /* open */
78      if (!compis_get_tracks_and_sectors(floppy, &geometry.tracks, &geometry.sectors))
79         return FLOPPY_ERROR_INVALIDIMAGE;
80   }
81
82   return basicdsk_construct(floppy, &geometry);
83}
84
85
86
87/* ----------------------------------------------------------------------- */
88
89FLOPPY_OPTIONS_START( compis )
90   FLOPPY_OPTION( compis_dsk, "dsk",      "Compis floppy disk image",   compis_dsk_identify, compis_dsk_construct, NULL,
91      TRACKS(40/[80])
92      SECTORS(8/[9]/15))
93FLOPPY_OPTIONS_END
src/lib/formats/vt_dsk.h
r0r11458
1/*********************************************************************
2
3    formats/vt_dsk.h
4
5    Tandy Color Computer / Dragon disk images
6
7*********************************************************************/
8
9#ifndef VT_DSK_H
10#define VT_DSK_H
11
12#include "flopimg.h"
13
14FLOPPY_OPTIONS_EXTERN(vz);
15
16#endif /* VT_DSK_H */
src/lib/formats/oric_tap.c
r0r11458
1#include "oric_tap.h"
2
3#define ORIC_WAV_DEBUG 0
4#define LOG(x) do { if (ORIC_WAV_DEBUG) printf x; } while (0)
5
6/* this code based heavily on tap2wav by Fabrice Frances */
7#define ORIC_SYNC_BYTE   0x016
8
9/* frequency of wave */
10/* tapes use 1200Hz and 2400Hz samples */
11#define ORIC_WAV_FREQUENCY   4800
12
13/* 13 bits define a byte on the cassette */
14/* 1 start bit, 8 data bits, 1 parity bit and 3 stop bits */
15#define ORIC_BYTE_TO_BITS_ON_CASSETTE 13
16
17#define ORIC_WAVESAMPLES_HEADER  3000
18#define ORIC_WAVESAMPLES_TRAILER 1000
19
20enum
21{
22   ORIC_CASSETTE_SEARCHING_FOR_SYNC_BYTE,
23   ORIC_CASSETTE_GOT_SYNC_BYTE,
24   ORIC_CASSETTE_READ_HEADER,
25   ORIC_CASSETTE_READ_FILENAME,
26   ORIC_CASSETTE_WRITE_DATA
27};
28
29#define WAVEENTRY_LOW  -32768
30#define WAVEENTRY_HIGH  32767
31#define WAVEENTRY_NULL  0
32
33#define ORIC_LEADER_LENGTH 512
34
35struct oric_t
36{
37   int cassette_state;
38   int data_count;
39   int data_length;
40   int tap_size;
41};
42static oric_t oric;
43
44/* to write a bit to the tape, the rom routines output either 4 periods at 1200 Hz for a 0 or 8 periods at 2400 Hz for a 1  */
45/* 4800 is twice 2400Hz */
46
47/* 8 periods at 2400Hz */
48/* hi,lo, hi,lo, hi,lo, hi,lo */
49
50static INT16 *oric_emit_level(INT16 *p, int count, INT16 wave_state)
51{
52   int i;
53
54   for (i=0; i<count; i++)
55   {
56      *(p++) = wave_state;
57   }
58   return p;
59}
60
61/* 4 periods at 1200Hz */
62static INT16* oric_output_bit(INT16 *p, UINT8 b)
63{
64   p = oric_emit_level(p, 1, WAVEENTRY_HIGH);
65   p = oric_emit_level(p, b ? 1 : 2, WAVEENTRY_LOW);
66
67   return p;
68}
69
70static int oric_get_bit_size_in_samples(UINT8 b)
71{
72   int count;
73
74   count = 1;
75
76   if (b)
77   {
78      count++;
79   }
80   else
81   {
82      count+=2;
83   }
84
85   return count;
86}
87
88
89/*  each byte on cassette is stored as:
90
91    start bit       0 * 1
92    data bits       8 * x (x is 0 or 1, and depends on data-bit value)
93    parity bit      1 * x (x is 0 or 1, and depends on the parity of the data bits)
94    stop bits       1 * 3
95
96    if data has even parity, parity bit will be 1.
97    if data has odd parity, parity bit will be 0.
98*/
99
100/*
101    512 * data byte 0x016       -> leader
102    1   * data byte 0x024       -> sync byte
103    9   * data byte             -> header
104    delay (of last pulse written)
105    x   * data byte         -> length
106
107
108    header structure:
109    3   * ?         ->      ???
110    1   * ?         ->      ???
111    1   * x         ->      end address high byte
112    1   * x         ->      end address low byte
113    1   * x         ->      start address high byte
114    1   * x         ->      start address low byte
115    1   * ?         ->      ???
116*/
117
118static int oric_calculate_byte_size_in_samples(UINT8 byte)
119{
120   int count;
121   int i;
122   UINT8 parity;
123   UINT8 data;
124
125   count = 0;
126
127
128   /* start bit */
129   count+=oric_get_bit_size_in_samples(0);
130
131   /* set initial parity */
132   parity = 1;
133
134   /* data bits, written bit 0, bit 1...bit 7 */
135   data = byte;
136   for (i=0; i<8; i++)
137   {
138      UINT8 data_bit;
139
140      data_bit = data & 0x01;
141
142      parity = parity + data_bit;
143
144      count+=oric_get_bit_size_in_samples(data_bit);
145      data = data>>1;
146   }
147
148   /* parity */
149   count+=oric_get_bit_size_in_samples((parity & 0x01));
150
151   /* stop bits */
152   count+=oric_get_bit_size_in_samples(1);
153   count+=oric_get_bit_size_in_samples(1);
154   count+=oric_get_bit_size_in_samples(1);
155   count+=oric_get_bit_size_in_samples(1);
156
157   return count;
158}
159
160
161static INT16 *oric_output_byte(INT16 *p, UINT8 byte)
162{
163   int i;
164   UINT8 parity;
165   UINT8 data;
166
167   /* start bit */
168   p = oric_output_bit(p, 0);
169
170   /* set initial parity */
171   parity = 1;
172
173   /* data bits, written bit 0, bit 1...bit 7 */
174   data = byte;
175   for (i=0; i<8; i++)
176   {
177      UINT8 data_bit;
178
179      data_bit = data & 0x01;
180
181      parity = parity + data_bit;
182
183      p = oric_output_bit(p, data_bit);
184      data = data>>1;
185   }
186
187   /* parity */
188   p = oric_output_bit(p, parity & 0x01);
189
190   /* stop bits */
191   p = oric_output_bit(p, 1);
192   p = oric_output_bit(p, 1);
193   p = oric_output_bit(p, 1);
194   p = oric_output_bit(p, 1);
195
196   return p;
197}
198
199static INT16 *oric_fill_pause(INT16 *p, int sample_count)
200{
201   int i;
202
203   for (i=0; i<sample_count; i++)
204   {
205      *(p++) = WAVEENTRY_NULL;
206   }
207
208   return p;
209}
210
211static int oric_seconds_to_samples(float seconds)
212{
213   return (int)((float)seconds*(float)ORIC_WAV_FREQUENCY);
214}
215
216/* length is length of .tap file! */
217static int oric_cassette_calculate_size_in_samples(const UINT8 *bytes, int length)
218{
219   unsigned char header[9];
220   int count;
221
222   const UINT8 *data_ptr;
223   int i;
224   UINT8 data;
225
226   oric.tap_size = length;
227
228   oric.cassette_state = ORIC_CASSETTE_SEARCHING_FOR_SYNC_BYTE;
229   count = 0;
230   data_ptr = bytes;
231
232   while (data_ptr<(bytes+length))
233   {
234      data = data_ptr[0];
235      data_ptr++;
236
237      switch (oric.cassette_state)
238      {
239         case ORIC_CASSETTE_SEARCHING_FOR_SYNC_BYTE:
240         {
241            if (data==ORIC_SYNC_BYTE)
242            {
243               logerror("found sync byte!\n");
244               /* found first sync byte */
245               oric.cassette_state = ORIC_CASSETTE_GOT_SYNC_BYTE;
246            }
247         }
248         break;
249
250         case ORIC_CASSETTE_GOT_SYNC_BYTE:
251         {
252            if (data!=ORIC_SYNC_BYTE)
253            {
254               /* 0.25 second pause */
255               count += oric_seconds_to_samples(0.25);
256
257               logerror("found end of sync bytes!\n");
258
259               /* oric writes approx 512 bytes */
260               /* found end of sync bytes */
261               for (i=0; i<ORIC_LEADER_LENGTH; i++)
262               {
263                  count+=oric_calculate_byte_size_in_samples(0x016);
264               }
265
266               if (data==0x024)
267               {
268                  //logerror("reading header!\n");
269                  count+=oric_calculate_byte_size_in_samples(0x024);
270
271                  oric.cassette_state = ORIC_CASSETTE_READ_HEADER;
272                  oric.data_count = 0;
273                  oric.data_length = 9;
274               }
275            }
276         }
277         break;
278
279         case ORIC_CASSETTE_READ_HEADER:
280         {
281            header[oric.data_count] = data;
282            count+=oric_calculate_byte_size_in_samples(data);
283
284            oric.data_count++;
285
286            if (oric.data_count==oric.data_length)
287            {
288               //logerror("finished reading header!\n");
289               oric.cassette_state = ORIC_CASSETTE_READ_FILENAME;
290            }
291         }
292         break;
293
294         case ORIC_CASSETTE_READ_FILENAME:
295         {
296            count+=oric_calculate_byte_size_in_samples(data);
297
298            /* got end of filename? */
299            if (data==0)
300            {
301               UINT16 end, start;
302               logerror("got end of filename\n");
303
304               /* 100 1 bits to seperate header from data */
305                                        for (i=0; i<100; i++)
306                                        {
307                                            count+=oric_get_bit_size_in_samples(1);
308                                        }
309
310               oric.cassette_state = ORIC_CASSETTE_WRITE_DATA;
311               oric.data_count = 0;
312
313               end = (((header[4] & 0x0ff)<<8) | (header[5] & 0x0ff));
314               start = (((header[6] & 0x0ff)<<8) | (header[7] & 0x0ff));
315               LOG(("start (from header): %02x\n",start));
316               LOG(("end (from header): %02x\n",end));
317                                        oric.data_length = end - start + 1;
318            }
319         }
320         break;
321
322         case ORIC_CASSETTE_WRITE_DATA:
323         {
324            count+=oric_calculate_byte_size_in_samples(data);
325            oric.data_count++;
326
327            if (oric.data_count==oric.data_length)
328            {
329               logerror("finished writing data!\n");
330               oric.cassette_state = ORIC_CASSETTE_SEARCHING_FOR_SYNC_BYTE;
331            }
332         }
333         break;
334
335      }
336   }
337
338   return count;
339}
340
341/* length is length of sample buffer to fill! */
342static int oric_cassette_fill_wave(INT16 *buffer, int length, UINT8 *bytes)
343{
344   unsigned char header[9];
345   UINT8 *data_ptr;
346   INT16 *p;
347   int i;
348   UINT8 data;
349
350   p = buffer;
351
352
353   /* header and trailer act as pauses */
354   /* the trailer is required so that the via sees the last bit of the last
355        byte */
356   if (bytes == CODE_HEADER) {
357      for (i = 0; i < ORIC_WAVESAMPLES_HEADER; i++)
358         *(p++) = WAVEENTRY_NULL;
359   }
360   else if (bytes == CODE_TRAILER) {
361      for (i = 0; i < ORIC_WAVESAMPLES_TRAILER; i++)
362         *(p++) = WAVEENTRY_NULL;
363   }
364   else
365{
366   /* the length is the number of samples left in the buffer and NOT the number of bytes for the input file */
367   length = length - ORIC_WAVESAMPLES_TRAILER;
368
369   oric.cassette_state = ORIC_CASSETTE_SEARCHING_FOR_SYNC_BYTE;
370   data_ptr = bytes;
371
372   while ((data_ptr<(bytes + oric.tap_size)) && (p < (buffer+length)) )
373   {
374      data = data_ptr[0];
375      data_ptr++;
376
377      switch (oric.cassette_state)
378      {
379         case ORIC_CASSETTE_SEARCHING_FOR_SYNC_BYTE:
380         {
381            if (data==ORIC_SYNC_BYTE)
382            {
383               logerror("found sync byte!\n");
384               /* found first sync byte */
385               oric.cassette_state = ORIC_CASSETTE_GOT_SYNC_BYTE;
386            }
387         }
388         break;
389
390         case ORIC_CASSETTE_GOT_SYNC_BYTE:
391         {
392            if (data!=ORIC_SYNC_BYTE)
393            {
394               /* 0.25 second pause */
395               p = oric_fill_pause(p, oric_seconds_to_samples(0.25));
396
397               logerror("found end of sync bytes!\n");
398               /* found end of sync bytes */
399               for (i=0; i<ORIC_LEADER_LENGTH; i++)
400               {
401                  p = oric_output_byte(p,0x016);
402               }
403
404               if (data==0x024)
405               {
406                  //logerror("reading header!\n");
407                  p = oric_output_byte(p,data);
408                  oric.cassette_state = ORIC_CASSETTE_READ_HEADER;
409                  oric.data_count = 0;
410                  oric.data_length = 9;
411               }
412            }
413         }
414         break;
415
416         case ORIC_CASSETTE_READ_HEADER:
417         {
418            header[oric.data_count] = data;
419            p = oric_output_byte(p, data);
420            oric.data_count++;
421
422            if (oric.data_count==oric.data_length)
423            {
424               //logerror("finished reading header!\n");
425               oric.cassette_state = ORIC_CASSETTE_READ_FILENAME;
426            }
427         }
428         break;
429
430         case ORIC_CASSETTE_READ_FILENAME:
431         {
432            p = oric_output_byte(p, data);
433
434            /* got end of filename? */
435            if (data==0)
436            {
437               UINT16 end, start;
438               logerror("got end of filename\n");
439
440               /* oric includes a small delay, but I don't see
441                    it being 1 bits */
442               for (i=0; i<100; i++)
443               {
444                                            p = oric_output_bit(p,1);
445               }
446
447               oric.cassette_state = ORIC_CASSETTE_WRITE_DATA;
448               oric.data_count = 0;
449
450               end = (((header[4] & 0x0ff)<<8) | (header[5] & 0x0ff));
451               start = (((header[6] & 0x0ff)<<8) | (header[7] & 0x0ff));
452               LOG(("start (from header): %02x\n",start));
453               LOG(("end (from header): %02x\n",end));
454                                        oric.data_length = end - start + 1;
455            }
456         }
457         break;
458
459         case ORIC_CASSETTE_WRITE_DATA:
460         {
461            p = oric_output_byte(p, data);
462            oric.data_count++;
463
464            if (oric.data_count==oric.data_length)
465            {
466               logerror("finished writing data!\n");
467               oric.cassette_state = ORIC_CASSETTE_SEARCHING_FOR_SYNC_BYTE;
468            }
469         }
470         break;
471
472      }
473   }
474}
475   return p - buffer;
476}
477
478
479
480static const struct CassetteLegacyWaveFiller oric_legacy_fill_wave =
481{
482   oric_cassette_fill_wave,               /* fill_wave */
483   -1,                                 /* chunk_size */
484   0,                                 /* chunk_samples */
485   oric_cassette_calculate_size_in_samples,   /* chunk_sample_calc */
486   ORIC_WAV_FREQUENCY,                     /* sample_frequency */
487   ORIC_WAVESAMPLES_HEADER,               /* header_samples */
488   ORIC_WAVESAMPLES_TRAILER               /* trailer_samples */
489};
490
491
492
493static casserr_t oric_tap_identify(cassette_image *cassette, struct CassetteOptions *opts)
494{
495   return cassette_legacy_identify(cassette, opts, &oric_legacy_fill_wave);
496}
497
498
499
500static casserr_t oric_tap_load(cassette_image *cassette)
501{
502   return cassette_legacy_construct(cassette, &oric_legacy_fill_wave);
503}
504
505
506
507static const struct CassetteFormat oric_tap_format =
508{
509   "tap",
510   oric_tap_identify,
511   oric_tap_load,
512   NULL
513};
514
515
516
517CASSETTE_FORMATLIST_START(oric_cassette_formats)
518   CASSETTE_FORMAT(oric_tap_format)
519CASSETTE_FORMATLIST_END
src/lib/formats/cpis_dsk.h
r0r11458
1/*********************************************************************
2
3    formats/cpis_dsk.h
4
5    Telenova Compis disk images
6
7*********************************************************************/
8
9#ifndef CPIS_DSK_H
10#define CPIS_DSK_H
11
12#include "flopimg.h"
13
14
15/**************************************************************************/
16
17FLOPPY_OPTIONS_EXTERN(compis);
18
19#endif /* CPIS_DSK_H */
src/lib/formats/atom_tap.c
r0r11458
1/*********************************************************************
2
3    formats/atom_tap.c
4
5    Cassette code for Acorn Atom tap files (Kansas City Standard)
6
7*********************************************************************/
8
9/*
10
11    http://beebwiki.jonripley.com/Acorn_cassette_format#Atom
12
13    The Atom supports the System series format (as a 'nameless file') and its own format. The data consists of files separated
14    by carrier breaks and the appropriate leaders and trailers, and further subdivided into blocks separated by inter-block gaps.
15    An Atom block consists of the following sequence of bytes:
16
17        Four synchronisation bytes (&2A).
18        File name (one to thirteen characters).
19        One end of file name marker byte (&0D).
20        Block flag, one byte.
21        Block number, two bytes, high byte first.
22        Data block length ? 1, one byte.
23        Execution address, two bytes, high byte first.
24        Load address, two bytes, high byte first.
25        Data, 0 to 256 bytes.
26        Checksum, one byte.
27
28    The block flag is set as follows:
29
30        Bit 7 is set if this block is not the last block of a file.
31        Bit 6 is set if the block contains data. If clear then the 'data block length' field is invalid.
32        Bit 5 is set if this block is not the first block of a file.
33        Bits 4 to 0 are undefined. Normally their contents are:
34        in the first block, bits 15 to 11 of the end address (=1 + the last address saved in the file);
35        in subsequent blocks, bits 6 to 2 of the previous block flag.
36
37    The checksum is a simple sum (modulo 256) of all bytes from the start of the filename to the end of the data.
38
39*/
40
41#include "atom_tap.h"
42
43/***************************************************************************
44    PARAMETERS
45***************************************************************************/
46
47#define LOG 1
48
49/***************************************************************************
50    IMPLEMENTATION
51***************************************************************************/
52
53/*-------------------------------------------------
54    CassetteModulation atom_tap_modulation
55-------------------------------------------------*/
56
57static const struct CassetteModulation atom_tap_modulation =
58{
59   CASSETTE_MODULATION_SINEWAVE,
60   1200.0 - 300, 1200.0, 1200.0 + 300,
61   2400.0 - 600, 2400.0, 2400.0 + 600
62};
63
64/*-------------------------------------------------
65    cassette_image_read_uint8 - read tape data
66-------------------------------------------------*/
67
68static UINT8 cassette_image_read_uint8(cassette_image *cassette, UINT64 offset)
69{
70   UINT8 data;
71   cassette_image_read(cassette, &data, offset, 1);
72   return data;
73}
74
75/*-------------------------------------------------
76    atom_tap_identify - identify cassette
77-------------------------------------------------*/
78
79static casserr_t atom_tap_identify(cassette_image *cassette, struct CassetteOptions *opts)
80{
81   return cassette_modulation_identify( cassette, &atom_tap_modulation, opts);
82}
83
84/*-------------------------------------------------
85    atom_tap_load - load cassette
86-------------------------------------------------*/
87
88#define MODULATE(_value) \
89   for (int i = 0; i < (_value ? 8 : 4); i++) { \
90      err = cassette_put_modulated_data_bit(cassette, 0, time_index, _value, &atom_tap_modulation, &time_displacement);\
91      if (err) return err;\
92      time_index += time_displacement;\
93   }
94
95#define BIT(x,n) (((x)>>(n))&1)
96
97static casserr_t atom_tap_load(cassette_image *cassette)
98{
99   casserr_t err;
100   UINT64 image_size = cassette_image_size(cassette);
101   UINT64 image_pos = 0;
102   double time_index = 0.0;
103   double time_displacement;
104
105   while (image_pos < image_size)
106   {
107      UINT8 data = cassette_image_read_uint8(cassette, image_pos);
108
109      /* start bit */
110      MODULATE(0);
111
112      /* data bits */
113      for (int bit = 0; bit < 8; bit++)
114      {
115         MODULATE(BIT(data, bit));
116      }
117
118      /* stop bits */
119      MODULATE(1);
120      MODULATE(1);
121
122      image_pos++;
123   }
124
125   return CASSETTE_ERROR_SUCCESS;
126}
127
128/*-------------------------------------------------
129    CassetteFormat atom_tap_cassette_format
130-------------------------------------------------*/
131
132const struct CassetteFormat atom_tap_format =
133{
134   "tap",
135   atom_tap_identify,
136   atom_tap_load,
137   NULL
138};
src/lib/formats/z80ne_dsk.c
r0r11458
1/*********************************************************************
2
3    formats/z80ne_dsk.c
4
5    Nuova Elettronica Z80NE disk images
6
7*********************************************************************/
8
9#include "z80ne_dsk.h"
10#include "basicdsk.h"
11#include "imageutl.h"
12
13/* -----------------------------------------------------------------------
14 * DMK file format
15 *
16 * David M. Keil's disk image format is aptly called an 'on disk' image
17 * format. This means that whatever written to the disk is encoded into
18 * the image file. IDAMS, sector headers, trailing CRCs, and intra-sector
19 * spacing.                                                         *
20 *
21 *  HEADER DESCRIPTION:
22 *
23 *  Offset  Bytes   Field           Description
24 *  ------  -----   ------------    -----------
25 *       0      1   write_prot      0xff = Write Protected, 0x00 = R/W
26 *       1      1   tracks          Number of tracks
27 *       2      2   track_length    Bytes per track on DMK file (little endian)
28 *       4      1   disk_options    Miscellaneous flags
29 *                                      bit 0-3:  Unused
30 *                                      bit   4:  Sides on DMK file
31 *                                                1=single, 0=double
32 *                                      bit   5:  Unused
33 *                                      bit   6:  Single Density DMK file
34 *                                                1=single (bytes written once
35 *                                                  on IDAMs)
36 *                                                0=double (bytes written
37 *                                                  two times for SD IDAMs)
38 *       5      7   reserved        Reserved for future use
39 *      12      4   real_disk_code  If this is 0x12345678 (little endian)
40 *                                  then access a real disk drive
41 *                                  (unsupported)
42 *
43 * Each track begins with a track TOC, consisting of 64 little endian 16-bit
44 * integers.  Each integer has the following format:
45 *      bit 0-13:   Offset from beginning of track to 'FE' byte of IDAM
46 *                  Note these are always sorted from first to last. All empty
47 *                  entries are 0x00
48 *      bit 14:     Undefined (reserved)
49 *      bit 15:     Sector double density (0=SD 1=DD)
50 *                  If not set and SD DMK flag is set then bytes are written
51 *                  once.
52 * ----------------------------------------------------------------------- */
53
54struct dmk_tag
55{
56   int heads;
57   int tracks;
58   UINT32 track_size;
59};
60
61/* DMK file structure
62 * See WD1711 documentation
63 */
64/* DMK file header */
65#define DMK_HEADER_LEN         16         /* DMK header */
66   /*  For every track (40) */
67   #define DMK_TOC_LEN            128      /* 64 16-bit relative offsets to IDAMs */
68   #define DMK_GAP1_LEN         32      /* GAP1 (26 bytes 0xFF and 6 bytes 0x00) */
69
70      /* For every sector (10) 301 bytes */
71      #define DMK_IDAM_LEN         7   /* IDAM (0xFE, Track, 0x00, Sector, 0x00, CRChi, CRClo) */
72      #define DMK_ID_GAP_LEN         17   /* GAP2 (11 bytes 0xFF and 6 bytes 0x00) */
73      #define DMK_DAM_LEN          1   /* Data Address Mark (0xFB) */
74      #define DMK_DATA_LEN         256 /* Data */
75      #define DMK_DATA_CRC_LEN      2   /* Data CRC (CRChi, CRClo) */
76      #define DMK_DATA_GAP_LEN      18  /* GAP3 (12 bytes 0xFF and 6 bytes 0x00) (not for last sector) */
77
78   #define DMK_EXTRA_TRACK_LENGTH   (176)   /* GAP4 (192 bytes 0xFF) */
79/* End of DMK file structure */
80
81#define dmk_idam_type(x)         (x)[0]
82#define dmk_idam_track(x)         (x)[1]
83#define dmk_idam_side(x)         (x)[2]
84#define dmk_idam_sector(x)         (x)[3]
85#define dmk_idam_sectorlength(x)   (x)[4]
86#define dmk_idam_crc(x)            (((x)[5] << 8) + (x)[6])
87#define dmk_idam_set_crc(x, crc)   (x)[5] = ((crc) >> 8); (x)[6] = ((crc) >> 0);
88
89static const char needle_data[] = "\x00\x00\x00\x00\x00\x00\x00\x00\xFB\xFB";
90static const char needle_deleted_data_fa[] = "\x00\x00\x00\x00\x00\x00\x00\x00\xFA\xFA";
91static const char needle_deleted_data_f8[] = "\x00\x00\x00\x00\x00\x00\x00\x00\xF8\xF8";
92
93
94static struct dmk_tag *get_dmk_tag(floppy_image *floppy)
95{
96   return (dmk_tag *)floppy_tag(floppy);
97}
98
99
100static floperr_t z80ne_dmk_get_track_offset(floppy_image *floppy, int head, int track, UINT64 *offset)
101{
102   struct dmk_tag *tag = get_dmk_tag(floppy);
103
104   if ((head < 0) || (head >= tag->heads) || (track < 0) || (track >= tag->tracks))
105      return FLOPPY_ERROR_SEEKERROR;
106
107   *offset = track;
108   *offset *= tag->heads;
109   *offset += head;
110   *offset *= tag->track_size;
111   *offset += DMK_HEADER_LEN;
112   return FLOPPY_ERROR_SUCCESS;
113}
114
115
116static UINT32 z80ne_dmk_min_track_size(int sectors, int sector_length)
117{
118   int sector_physical_length;
119   sector_physical_length = (DMK_IDAM_LEN + DMK_ID_GAP_LEN + DMK_DAM_LEN + sector_length + DMK_DATA_CRC_LEN + DMK_DATA_GAP_LEN);
120   return DMK_TOC_LEN + 2 * (DMK_GAP1_LEN + (sectors * sector_physical_length) - DMK_DATA_GAP_LEN + DMK_EXTRA_TRACK_LENGTH);
121}
122
123
124
125static floperr_t z80ne_dmk_read_track(floppy_image *floppy, int head, int track, UINT64 offset, void *buffer, size_t buflen)
126{
127   floperr_t err;
128   UINT64 track_offset;
129
130   err = z80ne_dmk_get_track_offset(floppy, head, track, &track_offset);
131   if (err)
132      return err;
133
134   floppy_image_read(floppy, buffer, offset + track_offset, buflen);
135   return FLOPPY_ERROR_SUCCESS;
136}
137
138
139
140static floperr_t z80ne_dmk_write_track(floppy_image *floppy, int head, int track, UINT64 offset, const void *buffer, size_t buflen)
141{
142   floperr_t err;
143   UINT64 track_offset;
144
145   err = z80ne_dmk_get_track_offset(floppy, head, track, &track_offset);
146   if (err)
147      return err;
148
149   floppy_image_write(floppy, buffer, offset + track_offset, buflen);
150   return FLOPPY_ERROR_SUCCESS;
151}
152
153
154
155static floperr_t z80ne_dmk_get_track_data_offset(floppy_image *floppy, int head, int track, UINT64 *offset)
156{
157   *offset = DMK_TOC_LEN + 1;
158   return FLOPPY_ERROR_SUCCESS;
159}
160
161
162static floperr_t z80ne_dmk_format_track(floppy_image *floppy, int head, int track, option_resolution *params)
163{
164   int sectors;
165   int sector_length;
166   int interleave;
167   int first_sector_id;
168   floperr_t err;
169   int physical_sector;
170   int logical_sector;
171   int track_position;
172   UINT16 idam_offset;
173   UINT16 crc;
174   UINT8 *track_data;
175   void *track_data_v;
176   UINT32 max_track_size;
177   int *sector_map = NULL;
178   UINT8 *local_sector = NULL;
179   int local_sector_size;
180   int sector_position;
181   int i;
182
183   err = FLOPPY_ERROR_SUCCESS;
184
185   sectors         = option_resolution_lookup_int(params, PARAM_SECTORS);
186   sector_length   = option_resolution_lookup_int(params, PARAM_SECTOR_LENGTH);
187   interleave      = option_resolution_lookup_int(params, PARAM_INTERLEAVE);
188   first_sector_id   = option_resolution_lookup_int(params, PARAM_FIRST_SECTOR_ID);
189
190   max_track_size = get_dmk_tag(floppy)->track_size;
191
192   if (sectors > DMK_TOC_LEN/2)
193   {
194        err = FLOPPY_ERROR_INTERNAL;
195      goto done;
196   }
197
198   if (max_track_size < z80ne_dmk_min_track_size(sectors, sector_length))
199   {
200      err = FLOPPY_ERROR_NOSPACE;
201      goto done;
202   }
203
204   err = floppy_load_track(floppy, head, track, TRUE, &track_data_v, NULL);
205   if (err)
206      goto done;
207   track_data = (UINT8 *) track_data_v;
208
209   /* set up sector map */
210   sector_map = (int*)malloc(sectors * sizeof(*sector_map));
211   if (!sector_map)
212   {
213      err = FLOPPY_ERROR_OUTOFMEMORY;
214      goto done;
215   }
216   memset(sector_map, 0xFF, sectors * sizeof(*sector_map));
217
218   physical_sector = 0;
219   for (logical_sector = 0; logical_sector < sectors; logical_sector++)
220   {
221      while(sector_map[physical_sector] >= 0)
222      {
223         physical_sector++;
224         physical_sector %= sectors;
225      }
226
227      sector_map[physical_sector] = logical_sector + first_sector_id;
228      physical_sector += interleave + 1;
229      physical_sector %= sectors;
230   }
231
232   /* set up a local physical sector template */
233   local_sector_size = (DMK_IDAM_LEN +
234                      DMK_ID_GAP_LEN +
235                      DMK_DAM_LEN +
236                      sector_length +
237                      DMK_DATA_CRC_LEN +
238                      DMK_DATA_GAP_LEN);
239   local_sector = (UINT8*)malloc(local_sector_size);
240   if (!local_sector)
241   {
242      err = FLOPPY_ERROR_OUTOFMEMORY;
243      goto done;
244   }
245   memset(local_sector, 0x00, local_sector_size);
246
247   /* set up track table of contents */
248   physical_sector = 0;
249
250   /* write GAP1 (track lead in) */
251   memset(&track_data[DMK_TOC_LEN],    0xFF, 52);
252   memset(&track_data[DMK_TOC_LEN+52], 0x00, 12);
253   track_position = DMK_TOC_LEN + DMK_GAP1_LEN * 2;
254
255   /*
256     *  prepare template in local sector
257     */
258
259   /* IDAM */
260   sector_position = 0;
261   dmk_idam_type(            &local_sector[sector_position]) = 0xFE;
262   dmk_idam_track(            &local_sector[sector_position]) = track;
263   dmk_idam_side(            &local_sector[sector_position]) = head;
264   dmk_idam_sector(         &local_sector[sector_position]) = 0;      /* replace in sector instances */
265   dmk_idam_sectorlength(      &local_sector[sector_position]) = compute_log2(sector_length / 128);
266   dmk_idam_set_crc(         &local_sector[sector_position], 0);       /* replace in sector instances */
267   sector_position += 7;
268
269   /* GAP2 (ID GAP) */
270   memset(&local_sector[sector_position], 0xFF, 11);
271   sector_position += 11;
272   memset(&local_sector[sector_position], 0x00, 6);
273   sector_position += 6;
274
275   /* sector body */
276   local_sector[sector_position] = 0xFB;
277   memset(&local_sector[sector_position + 1], floppy_get_filler(floppy), sector_length);
278   crc = ccitt_crc16(0xffff, &local_sector[sector_position], sector_length + 1);
279   local_sector[sector_position + sector_length + 1] = (UINT8) (crc >> 8);
280   local_sector[sector_position + sector_length + 2] = (UINT8) (crc >> 0);
281   sector_position += sector_length + 3;
282
283   /* GAP3 (Data GAP) */
284   memset(&local_sector[sector_position], 0xFF, 12);
285   sector_position += 12;
286   memset(&local_sector[sector_position], 0x00, 6);
287   sector_position += 6;
288
289   /*
290     *  start of track data
291     */
292   while(physical_sector < DMK_TOC_LEN/2)
293   {
294      if (physical_sector >= sectors)
295      {
296         /* no more sectors */
297         idam_offset = 0;
298      }
299      else
300      {
301         /* this is a sector */
302         idam_offset = track_position;
303         logical_sector = sector_map[physical_sector];
304
305         /* update IDAM in local sector template */
306         dmk_idam_sector(         &local_sector[0]) = logical_sector;      /* update sector number in sector instance  */
307         crc = ccitt_crc16(0xffff,   &local_sector[0], DMK_IDAM_LEN - 2);   /* and recalculate crc */
308         dmk_idam_set_crc(         &local_sector[0], crc);
309
310         /* write local sector in track doubling every byte (DMK double density mode) */
311         for(i=0; i<local_sector_size; i++)
312            track_data[track_position + i*2] = track_data[track_position + (i*2) + 1] = local_sector[i];
313         track_position += (2 * local_sector_size);
314         if (physical_sector == (sectors-1)) /* on last sector remove GAP3 */
315            track_position -= (2 * DMK_DATA_GAP_LEN);
316      }
317
318      /* write the TOC entry */
319      track_data[physical_sector * 2 + 0] = (UINT8) (idam_offset >> 0);
320      track_data[physical_sector * 2 + 1] = (UINT8) (idam_offset >> 8);
321
322      physical_sector++;
323   }
324
325   /* write track footer GAP4 */
326   //assert(max_track_size >= (UINT32)track_position);
327   memset(&track_data[track_position], 0xFF, max_track_size - track_position);
328
329done:
330   if (sector_map)
331      free(sector_map);
332   if (local_sector)
333      free(local_sector);
334
335   return err;
336}
337
338
339
340static int z80ne_dmk_get_heads_per_disk(floppy_image *floppy)
341{
342   return get_dmk_tag(floppy)->heads;
343}
344
345
346
347static int z80ne_dmk_get_tracks_per_disk(floppy_image *floppy)
348{
349   return get_dmk_tag(floppy)->tracks;
350}
351
352
353
354static UINT32 z80ne_dmk_get_track_size(floppy_image *floppy, int head, int track)
355{
356   return get_dmk_tag(floppy)->track_size;
357}
358
359static UINT8 * my_memmem(UINT8 *haystack, size_t haystacklen,
360      UINT8 *needle, size_t needlelen)
361{
362        register UINT8 *h = haystack;
363        register UINT8 *n = needle;
364        register size_t hl = haystacklen;
365        register size_t nl = needlelen;
366        register size_t i;
367
368        if (nl == 0) return haystack;    /* The first occurrence of the empty string is deemed to occur at
369                                                     the beginning of the string.  */
370        if (hl < nl)
371           return NULL;
372
373        for( i = 0; (i < hl) && (i + nl <= hl ); i++ )
374           if (h[i] == *n) /* first match */
375              if ( !memcmp(&h[i]+1, n + 1, nl - 1) )
376                 return (haystack+i);    /* returns a pointer to the substring */
377
378        return (UINT8 *)NULL;    /* not found */
379}
380
381
382
383static floperr_t z80ne_dmk_seek_sector_in_track(floppy_image *floppy, int head, int track, int sector, int sector_is_index, int dirtify, UINT8 **sector_data, UINT32 *sector_length)
384{
385   struct dmk_tag *tag = get_dmk_tag(floppy);
386   floperr_t err;
387   size_t idam_offset = 0;
388   UINT16 calculated_crc;
389   size_t i, j;
390   size_t offs;
391   /*int state;*/
392   UINT8 *track_data;
393   void *track_data_v;
394   size_t track_length;
395   size_t sec_len;
396   UINT8 *local_idam = NULL;
397   int local_idam_size;
398   UINT8 *sec_data;
399
400
401   err = FLOPPY_ERROR_SUCCESS;
402
403   err = floppy_load_track(floppy, head, track, dirtify, &track_data_v, &track_length);
404   if (err)
405      goto done;
406   track_data = (UINT8 *) track_data_v;
407
408   /* set up a local IDAM space */
409   local_idam_size = (DMK_IDAM_LEN +
410                      DMK_ID_GAP_LEN +
411                      DMK_DAM_LEN +
412                      DMK_DATA_GAP_LEN);
413   local_idam = (UINT8*)malloc(local_idam_size);
414   if (!local_idam)
415   {
416      err = FLOPPY_ERROR_OUTOFMEMORY;
417      goto done;
418   }
419   memset(local_idam, 0x00, local_idam_size);
420
421   /* search for matching IDAM */
422   for (i = 0; i < DMK_TOC_LEN / 2; i++)
423   {
424      idam_offset = track_data[i * 2 + 1];
425      idam_offset <<= 8;
426      idam_offset |= track_data[i * 2 + 0];
427      idam_offset &= 0x3FFF;
428
429      if (idam_offset == 0)
430      {
431         /* we've reached the end of the road */
432         i = DMK_TOC_LEN;
433         break;
434      }
435
436      if ((idam_offset + DMK_IDAM_LEN * 2) >= tag->track_size)
437         continue;
438
439      /* overflowing the track? */
440      if ((idam_offset + local_idam_size * 2) >= tag->track_size)
441      {
442         err = FLOPPY_ERROR_SEEKERROR;
443         goto done;
444      }
445
446      /* create a local copy of sector of IDAM */
447      for(j = 0; j < (DMK_IDAM_LEN + DMK_ID_GAP_LEN + DMK_DAM_LEN); j++)
448         local_idam[j] = track_data[idam_offset + j*2];
449
450      /* from now on all operations are done in local copy of IDAM */
451      calculated_crc = ccitt_crc16(0xffff, &local_idam[0], DMK_IDAM_LEN - 2);
452
453      /* check IDAM integrity and check for matching sector */
454      if (calculated_crc == dmk_idam_crc(&local_idam[0]))
455      {
456         if (sector_is_index)
457         {
458            /* the sector is indexed; decrement the index and go */
459            if (sector-- == 0)
460               break;
461         }
462         else
463         {
464            /* check for matching sector ID */
465            if (sector == dmk_idam_sector(&local_idam[0]))
466               break;
467         }
468      }
469   }
470
471   if (i >= DMK_TOC_LEN / 2)
472   {
473      err = FLOPPY_ERROR_SEEKERROR;
474      goto done;
475   }
476
477   /* we found a matching sector ID */
478   /*state = 0;*/
479   offs = idam_offset + 2 * DMK_IDAM_LEN;
480
481   /* find pattern 0x0000FB; this represents the start of a data sector */
482   sec_data = my_memmem(&track_data[offs], 4 * (DMK_ID_GAP_LEN + DMK_DAM_LEN), (UINT8 *)needle_data, 10);
483   if(!sec_data)
484   {
485      /* find pattern 0x0000FA; this represents the start of a deleted data sector (used for system sectors) */
486      sec_data = my_memmem(&track_data[offs], 4 * (DMK_ID_GAP_LEN + DMK_DAM_LEN), (UINT8 *)needle_deleted_data_fa, 10);
487   }
488   if(!sec_data)
489   {
490      /* find pattern 0x0000F8; this represents the start of a deleted data sector (used for system sectors) */
491      sec_data = my_memmem(&track_data[offs], 4 * (DMK_ID_GAP_LEN + DMK_DAM_LEN), (UINT8 *)needle_deleted_data_f8, 10);
492   }
493
494   if(sec_data)
495      sec_data += 10;
496   else
497   {
498      err = FLOPPY_ERROR_SEEKERROR;
499      goto done;
500   }
501   offs = sec_data - &track_data[offs];
502
503   sec_len = 128 << dmk_idam_sectorlength(&local_idam[0]);
504
505   if ((offs + 2 * sec_len) > track_length)
506   {
507      err = FLOPPY_ERROR_INVALIDIMAGE;
508      goto done;
509   }
510
511   if (sector_data)
512      *sector_data = sec_data;
513   if (sector_length)
514      *sector_length = sec_len;
515done:
516   if (local_idam)
517      free(local_idam);
518   return err;
519}
520
521
522
523static floperr_t z80ne_dmk_get_sector_length(floppy_image *floppy, int head, int track, int sector, UINT32 *sector_length)
524{
525   return z80ne_dmk_seek_sector_in_track(floppy, head, track, sector, FALSE, FALSE, NULL, sector_length);
526}
527
528
529
530static floperr_t z80ne_dmk_get_indexed_sector_info(floppy_image *floppy, int head, int track, int sector_index, int *cylinder, int *side, int *sector, UINT32 *sector_length, unsigned long *flags)
531{
532   floperr_t err;
533   size_t idam_offset;
534   UINT8 *track_data;
535   void *track_data_v;
536   unsigned long dam;
537
538   if (sector_index * 2 >= DMK_TOC_LEN)
539      return FLOPPY_ERROR_SEEKERROR;
540
541   err = floppy_load_track(floppy, head, track, FALSE, &track_data_v, NULL);
542   if (err)
543      return err;
544   track_data = (UINT8 *) track_data_v;
545
546   idam_offset = track_data[sector_index * 2 + 1];
547   idam_offset <<= 8;
548   idam_offset |= track_data[sector_index * 2 + 0];
549   idam_offset &= 0x3FFF;
550
551   if (idam_offset == 0)
552      return FLOPPY_ERROR_SEEKERROR;
553
554   /* determine type of data sector */
555   /* find pattern 0x0000FB; this represents the start of a data sector */
556   if ( my_memmem(&track_data[idam_offset+14], 4 * (DMK_ID_GAP_LEN + DMK_DAM_LEN), (UINT8 *)needle_data, 10) )
557      dam = 0;
558   /* find pattern 0x0000FA; this represents the start of a deleted data sector (used for system sectors) */
559   else if ( my_memmem(&track_data[idam_offset+14], 4 * (DMK_ID_GAP_LEN + DMK_DAM_LEN), (UINT8 *)needle_deleted_data_fa, 10) )
560      dam = ID_FLAG_DELETED_DATA;
561   /* find pattern 0x0000F8; this represents the start of a deleted data sector (used for system sectors) */
562   else if ( my_memmem(&track_data[idam_offset+14], 4 * (DMK_ID_GAP_LEN + DMK_DAM_LEN), (UINT8 *)needle_deleted_data_f8, 10) )
563      dam = ID_FLAG_DELETED_DATA;
564   else
565      return FLOPPY_ERROR_SEEKERROR;
566
567   if (cylinder)
568      *cylinder = (&track_data[idam_offset])[2];
569   if (side)
570      *side = (&track_data[idam_offset])[4];
571   if (sector)
572      *sector = (&track_data[idam_offset])[6];
573   if (sector_length)
574      *sector_length = 128 << (&track_data[idam_offset])[8];
575   if (flags)
576      *flags = dam;
577   return FLOPPY_ERROR_SUCCESS;
578}
579
580
581
582static floperr_t internal_z80ne_dmk_read_sector(floppy_image *floppy, int head, int track, int sector, int sector_is_index, void *buffer, size_t buflen)
583{
584   floperr_t err;
585   UINT32 sector_length;
586   UINT16 crc_on_disk;
587   UINT16 calculated_crc;
588   UINT8 *sector_data = NULL;
589   UINT8 *local_sector = NULL;
590   int local_sector_size;
591   int i;
592
593   err = FLOPPY_ERROR_SUCCESS;
594
595   /* get sector length and data pointer */
596   err = z80ne_dmk_seek_sector_in_track(floppy, head, track, sector, sector_is_index, FALSE, &sector_data, &sector_length);
597   if (err)
598      goto done;
599
600   /* set up a local physical sector space (DAM + data + crc + GAP) */
601   local_sector_size = (DMK_DAM_LEN + sector_length + DMK_DATA_CRC_LEN + DMK_DATA_GAP_LEN);
602   local_sector = (UINT8*)malloc(local_sector_size);
603   if (!local_sector)
604   {
605      err = FLOPPY_ERROR_OUTOFMEMORY;
606      goto done;
607   }
608   memset(local_sector, 0x00, local_sector_size);
609
610   /* get sector data */
611   /* create a local copy of sector data including DAM (for crc calculation) */
612   for(i = 0; i < (DMK_DAM_LEN + sector_length + DMK_DATA_CRC_LEN); i++)
613      local_sector[i] = (sector_data - 2)[i*2];
614
615   crc_on_disk = local_sector[sector_length + 0 + 1];
616   crc_on_disk <<= 8;
617   crc_on_disk += local_sector[sector_length + 1 + 1];
618
619   /* crc calculated including DAM */
620   calculated_crc = ccitt_crc16(0xffff, local_sector, sector_length+1);
621   if (calculated_crc != crc_on_disk)
622   {
623      err = FLOPPY_ERROR_INVALIDIMAGE;
624      goto done;
625   }
626
627   /* copy local sector data excluding DAM */
628   memcpy(buffer, local_sector+1, MIN(sector_length, buflen));
629
630   done:
631   if (local_sector)
632      free(local_sector);
633   return err;
634}
635
636
637
638static floperr_t internal_z80ne_dmk_write_sector(floppy_image *floppy, int head, int track, int sector, int sector_is_index, const void *buffer, size_t buflen, int ddam)
639{
640   floperr_t err;
641   UINT32 sector_length;
642   UINT8 *sector_data;
643   UINT16 crc;
644   UINT8 *local_sector = NULL;
645   int local_sector_size;
646   int i;
647
648   err = FLOPPY_ERROR_SUCCESS;
649
650   /* get sector length */
651   err = z80ne_dmk_seek_sector_in_track(floppy, head, track, sector, sector_is_index, TRUE, &sector_data, &sector_length);
652   if (err)
653      goto done;
654
655   /* set up a local physical sector space */
656   local_sector_size = (DMK_DAM_LEN + sector_length + DMK_DATA_CRC_LEN + DMK_DATA_GAP_LEN);
657   local_sector = (UINT8*)malloc(local_sector_size);
658   if (!local_sector)
659   {
660      err = FLOPPY_ERROR_OUTOFMEMORY;
661      goto done;
662   }
663   memset(local_sector, 0x00, local_sector_size);
664   if(!ddam)
665      local_sector[0] = 0xFB;
666   else
667      local_sector[0] = 0xFA;
668
669   if (buflen > sector_length)
670   {
671      err = FLOPPY_ERROR_INTERNAL;
672      goto done;
673   }
674
675   memcpy(&local_sector[1], buffer, buflen);
676
677   crc = ccitt_crc16(0xffff, local_sector, DMK_DAM_LEN + sector_length);
678   local_sector[DMK_DAM_LEN + sector_length + 0] = crc >> 8;
679   local_sector[DMK_DAM_LEN + sector_length + 1] = crc >> 0;
680
681   /* write local sector in track doubling every byte (DMK double density mode) */
682   /* write DAM or DDAM too */
683   sector_data -= 2;
684   for(i=0; i < (DMK_DAM_LEN + sector_length + DMK_DATA_CRC_LEN); i++)
685      sector_data[i*2] = sector_data[(i*2)+1] = local_sector[i];
686
687   done:
688   if (local_sector)
689      free(local_sector);
690   return err;
691}
692
693
694
695static floperr_t z80ne_dmk_read_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen)
696{
697   return internal_z80ne_dmk_read_sector(floppy, head, track, sector, FALSE, buffer, buflen);
698}
699
700static floperr_t z80ne_dmk_write_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam)
701{
702   return internal_z80ne_dmk_write_sector(floppy, head, track, sector, FALSE, buffer, buflen, ddam);
703}
704
705static floperr_t z80ne_dmk_read_indexed_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen)
706{
707   return internal_z80ne_dmk_read_sector(floppy, head, track, sector, TRUE, buffer, buflen);
708}
709
710static floperr_t z80ne_dmk_write_indexed_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam)
711{
712   return internal_z80ne_dmk_write_sector(floppy, head, track, sector, TRUE, buffer, buflen, ddam);
713}
714
715
716
717static void z80ne_dmk_interpret_header(floppy_image *floppy, int *heads, int *tracks, int * sectors, int *track_size)
718{
719   UINT8 header[DMK_HEADER_LEN + DMK_TOC_LEN];
720   int i;
721   int idam_offset;
722   int sects;
723
724   floppy_image_read(floppy, header, 0, DMK_HEADER_LEN + DMK_TOC_LEN);
725
726   if (tracks)
727      *tracks = header[1];
728   if (heads)
729      *heads = (header[4] & 0x10) ? 1 : 2;
730   if (track_size)
731      *track_size = ((int) header[3]) * 0x100 + header[2];
732   if(sectors)
733   {
734      /* count sectors on first track */
735      sects = 0;
736      for (i = 0; i < DMK_TOC_LEN / 2; i++)
737      {
738         idam_offset = header[DMK_HEADER_LEN + i * 2 + 1];
739         idam_offset <<= 8;
740         idam_offset |= header[DMK_HEADER_LEN + i * 2 + 0];
741         idam_offset &= 0x3FFF;
742
743         if (idam_offset == 0)
744            break;
745         else
746            sects++;
747      }
748      *sectors = sects;
749   }
750
751}
752
753
754
755FLOPPY_CONSTRUCT(z80ne_dmk_construct)
756{
757   struct FloppyCallbacks *callbacks;
758   struct dmk_tag *tag;
759   UINT8 header[DMK_HEADER_LEN];
760   int heads, tracks, track_size, sectors, sector_length;
761
762   if (params)
763   {
764      heads         = option_resolution_lookup_int(params, PARAM_HEADS);
765      tracks         = option_resolution_lookup_int(params, PARAM_TRACKS);
766      sectors         = option_resolution_lookup_int(params, PARAM_SECTORS);
767      sector_length   = option_resolution_lookup_int(params, PARAM_SECTOR_LENGTH);
768
769      track_size = z80ne_dmk_min_track_size(sectors, sector_length);
770
771      memset(header, 0, sizeof(header));
772      header[1] = tracks;
773      header[2] = track_size >> 0;
774      header[3] = track_size >> 8;
775      header[4] = (heads == 2) ? 0x00 : 0x10;
776
777      floppy_image_write(floppy, header, 0, sizeof(header));
778   }
779   else
780   {
781      z80ne_dmk_interpret_header(floppy, &heads, &tracks, &sectors, &track_size);
782   }
783
784   tag = (dmk_tag*)floppy_create_tag(floppy, sizeof(struct dmk_tag));
785   if (!tag)
786      return FLOPPY_ERROR_OUTOFMEMORY;
787   tag->heads = heads;
788   tag->track_size = track_size;
789   tag->tracks = tracks;
790
791   callbacks = floppy_callbacks(floppy);
792   callbacks->read_track = z80ne_dmk_read_track;
793   callbacks->write_track = z80ne_dmk_write_track;
794   callbacks->get_track_data_offset = z80ne_dmk_get_track_data_offset;
795   callbacks->format_track = z80ne_dmk_format_track;
796   callbacks->get_heads_per_disk = z80ne_dmk_get_heads_per_disk;
797   callbacks->get_tracks_per_disk = z80ne_dmk_get_tracks_per_disk;
798   callbacks->get_track_size = z80ne_dmk_get_track_size;
799   callbacks->get_sector_length = z80ne_dmk_get_sector_length;
800   callbacks->get_indexed_sector_info = z80ne_dmk_get_indexed_sector_info;
801   callbacks->read_sector = z80ne_dmk_read_sector;
802   callbacks->write_sector = z80ne_dmk_write_sector;
803   callbacks->read_indexed_sector = z80ne_dmk_read_indexed_sector;
804   callbacks->write_indexed_sector = z80ne_dmk_write_indexed_sector;
805
806   return FLOPPY_ERROR_SUCCESS;
807}
808
809
810
811FLOPPY_IDENTIFY(z80ne_dmk_identify)
812{
813   int heads, tracks, sectors, track_size;
814   UINT64 size, expected_size;
815
816   size = floppy_image_size(floppy);
817   z80ne_dmk_interpret_header(floppy, &heads, &tracks, &sectors, &track_size);
818   expected_size = DMK_HEADER_LEN + (heads * tracks * track_size);
819   *vote = (size == expected_size) ? 90 : 0;
820   *vote += (sectors == 10) ? 10 : 0;
821   return FLOPPY_ERROR_SUCCESS;
822}
823
824
825
826/* ----------------------------------------------------------------------- */
827
828FLOPPY_OPTIONS_START( z80ne )
829   /*
830     * Single side
831     * Single density
832     * FM, 125 kbit/s,
833     * 40 tracks, track ID: 0..39
834     * 10 sectors/track, sector ID: 0..9
835     * 256 bytes/sector
836     * Interleave 1 (sector ID sequence on track: 0 5 1 6 2 7 3 8 4 9)
837     * 3125 byte/track unformatted
838     * Rotation speed 300 rpm (5 rps)
839     */
840   FLOPPY_OPTION( z80ne_dmk, "zmk",      "Z80NE DMK disk image",   z80ne_dmk_identify,   z80ne_dmk_construct, NULL,
841      HEADS([1])
842      TRACKS([40])
843      SECTORS([10])
844      SECTOR_LENGTH([256])
845      INTERLEAVE([1])
846      FIRST_SECTOR_ID([0]))
847FLOPPY_OPTIONS_END
src/lib/formats/cbm_tap.c
r0r11458
1/*
2
3    Tape support for C16 / C64 / VIC20 TAP format
4
5
6    Credits to:
7
8        - Peter Schepers for the informations on C64 formats
9        - Vice Team for the source of their very complete emulator
10        - Attila G. for tap2wav (both the source and the actual tool)
11
12
13    TODO:
14    * to verify and fix (if needed) support for .TAP v2
15    * to implement reading (and logging) the remaining part of the header
16    * to verify if it is more accurate to use a different HIGH_WAVE value
17      when the pulse corresponds to a 0x00 in the .TAP file
18    (...)
19    * (far away in the future) can this code be merged with TZX code?
20
21*/
22
23/* Info based on http://ist.uwaterloo.ca/~schepers/formats/TAP.TXT      */
24/* Please refer to the webpage for the latest version and for a very
25   complete listing of various cart types and their bankswitch tricks   */
26/*
27
28  Designed by Per Hakan Sundell (author of the CCS64 C64 emulator) in 1997,
29this format attempts to duplicate the data stored on a C64  cassette  tape,
30bit for bit. Since it is simply a representation of  the  raw  serial  data
31from a tape, it should handle *any* custom tape loaders that exist.
32
33  The TAP images are generally very large, being a minimum of eight  times,
34and up to sixteen times as large as what a raw PRG file would be.  This  is
35due to the way the data is stored, with each bit of the original  file  now
36being one byte large in the TAP file. The layout is fairly simple,  with  a
37small 14-byte header followed by file data.
38
39    Bytes: $0000-000B: File signature "C64-TAPE-RAW"
40                 000C: TAP version (see below for description)
41                        $00 - Original layout
42                         01 - Updated
43            000D-000F: Future expansion
44            0010-0013: File  data  size  (not  including  this  header,  in
45                       LOW/HIGH format).
46            0014-xxxx: File data
47
48  In TAP version $00 files, each data byte in the file data area represents
49the length of a pulse, when the C64's hardware  will  trigger  again.  This
50pulse length is determined by the following formula:
51
52    pulse length (in seconds) = (8 * data byte) / (clock cycles)
53
54  Therefore, a data value of $2F (47 in decimal) would be:
55
56    (47 * 8) / 985248 = .00038975 seconds.
57
58  A data value of $00 represents an "overflow" condition, any pulse  length
59which is more that 255 * 8 in length.
60
61  The value of "clock cylces" from above  (985248)  is  based  on  the  PAL
62value.  Since  this  file  format  was  developed  in  Europe,   which   is
63predominantly PAL video, this is only logical.  The  NTSC  value  would  be
641022730, which is very close to  the  PAL,  and  therefore  won't  cause  a
65compatability problem converting European and NTSC tapes. I would stick  to
66using the PAL value just in case.
67
68
69  In TAP version $01 files, the data value of  $00  has  been  re-coded  to
70represent values greater than 255 * 8. When a  $00  is  encountered,  three
71bytes will follow which are the actual time (in cycles) of a pulse, and the
72above formula does not apply.  The  three  bytes  are  stored  in  LOW/HIGH
73format.
74
75
76  The actual interpretation of the serial data takes a little more work  to
77explain.  The  typical  ROM  tape  loader  (and  the  turbo  loaders)  will
78initialize a timer with a specified value and start it  counting  down.  If
79either the tape data changes or the timer runs out, an IRQ will occur.  The
80loader will determine which condition caused the  IRQ.  If  the  tape  data
81changed before the timer ran out, we have a short pulse, or a "0"  bit.  If
82the timer ran out first, we have a long pulse, or a  "1"  bit.  Doing  this
83continuously and we decode the entire file.
84
85
86[ Additional notes on v.2:
87
88  I found no documents about this around, but it seems an expansion of the
89format specifically thought for C16 tapes. In a .TAP version 2, each byte
90only stores informations on half of the wave...
91Unfortunately, I have no such a .tap file to test, so my implementation
92below could be not working.  FP ]
93*/
94
95#include "cbm_tap.h"
96
97
98#define CBM_WAV_FREQUENCY   44100
99
100/* Systems */
101#define C64      0
102#define VIC20   1
103#define C16      2
104
105/* Video standards */
106#define PAL      0
107#define NTSC   1
108
109/* Frequencies in [Hz] to determine the length of each pulse */
110#define C64_PAL      123156      /*  985248 / 8 */
111#define C64_NTSC   127841      /* 1022727 / 8 */
112#define VIC20_PAL   138551      /* 1108405 / 8 */
113#define VIC20_NTSC   127841      /* 1022727 / 8 */
114#define C16_PAL      110840      /*  886724 / 8 */
115#define C16_NTSC   111860      /*  894886 / 8 */
116
117#define PAUSE (CBM_WAV_FREQUENCY / 50)      /* tap2wav uses this value for 0x00 in .TAP v0, instead of 0x100 */
118
119/* These values do not really matter, as long as the produced pulses
120  go above & below 0. However, for documentation purpose it would be
121  nice to find out which values were used by Commodore tapes. I was
122  not able to find any reference on the subject. */
123#define WAVE_HIGH      (0x5a9e >> 1)
124#define WAVE_LOW      -(0x5a9e >> 1)
125#define WAVE_PAUSE      0x80
126
127#define CBM_HEADER_SIZE 20
128
129static INT16   wave_data = 0;
130static int     len;
131
132
133/* This in fact gives the number of samples for half of the pulse */
134INLINE int tap_data_to_samplecount(int data, int frequency)
135{
136//  return (int) (0.5 * (0.5 + (((double)CBM_WAV_FREQUENCY / frequency) * (double)data)));      // MESS TZX formula
137   return (int) (0.5 * (((double)CBM_WAV_FREQUENCY / frequency) * (double)((data) + 0.5)));   // tap2wav formula
138}
139
140/* The version with parameters could be handy if we decide to implement a
141different values for byte == 0x00 below (the WAVE_PAUSE) as tap2wav does. */
142#if 0
143static void toggle_wave_data(int low, int high)
144{
145   wave_data = (wave_data == low) ? high : low;
146}
147#endif
148
149static void toggle_wave_data(void )
150{
151   wave_data = (wave_data == WAVE_HIGH) ? WAVE_LOW : WAVE_HIGH;
152}
153
154static void cbm_output_wave( INT16 **buffer, int length )
155{
156   if (buffer == NULL)
157      return;
158
159   for( ; length > 0; length-- )
160   {
161      **buffer = wave_data;
162      *buffer = *buffer + 1;
163   }
164}
165
166
167static int cbm_tap_do_work( INT16 **buffer, int length, const UINT8 *data )
168{
169   int i, j = 0;
170   int size = 0;
171
172   int version = data[0x0c];
173   int system = data[0x0d];
174   int video_standard = data[0x0e];
175   int tap_frequency = 0;
176
177   int byte_samples = 0;
178   UINT8 over_pulse_bytes[3] = {0 , 0, 0 };
179   int over_pulse_length = 0;
180   /* These waveamp_* values are currently stored but not used.
181      Further investigations are needed to find real pulse amplitude
182      in Commodore tapes. Implementation here would follow */
183   /* int waveamp_high, waveamp_low; */
184
185   /* Log .TAP info but only once */
186   if (!(buffer == NULL))
187   {
188      logerror("TAP version    : %d\n", version);
189      logerror("Machine type   : %d\n", system);
190      logerror("Video standard : %d\n", video_standard);
191      logerror("Tape frequency : %d\n", (tap_frequency) << 3);
192   }
193
194
195   /* is this a supported version? */
196   if ((version < 0) || (version > 2))
197   {
198      logerror("Unsupported .tap version: %d \n", version);
199      return -1;
200   }
201
202   /* is the .tap file corrupted? */
203   if ((data == NULL) || (length <= CBM_HEADER_SIZE))
204      return -1;
205
206
207   /* read the frequency from the .tap header */
208    switch (system)
209    {
210        case VIC20:
211            tap_frequency = (video_standard == NTSC) ? VIC20_NTSC : VIC20_PAL;
212            break;
213
214        case C16:
215            tap_frequency = (video_standard == NTSC) ? C16_NTSC : C16_PAL;
216            break;
217
218        case C64:
219        default:
220            tap_frequency = (video_standard == NTSC) ? C64_NTSC : C64_PAL;
221            break;
222    }
223
224
225   for (i = CBM_HEADER_SIZE; i < length; i++)
226   {
227      UINT8 byte = data[i];
228
229      /* .TAP v0 */
230      /* Here is simple:
231          if byte is != 0 -> length = byte
232          otherwise -> length =  0x100 (i.e. 0xff + 1) */
233      if (!version)
234      {
235         if (byte != 0x00)
236         {
237            byte_samples = tap_data_to_samplecount(byte, tap_frequency);
238            /* waveamp_high = WAVE_HIGH; */
239         }
240         else
241         {
242            byte_samples = tap_data_to_samplecount(PAUSE, tap_frequency);   // tap2wav value
243//              byte_samples = tap_data_to_samplecount(0x100, tap_frequency);   // vice value
244            /* waveamp_high = WAVE_PAUSE; */
245         }
246         /* waveamp_low = WAVE_LOW; */
247      }
248
249      /* .TAP v1 & v2 */
250      /* Here is a bit more complicate:
251          if byte is != 0 -> length = byte
252          otherwise -> the length of the pulse is stored as a 24bit value in the 3 bytes after the 0.
253          See below for comments on the implementation of this mechanism */
254      if (version)
255      {
256         if ((byte != 0x00) && !j)
257         {
258            byte_samples = tap_data_to_samplecount(byte, tap_frequency);
259            /* waveamp_high = WAVE_HIGH; */
260         }
261         else
262         {
263            /* If we have a long pulse close to the end of the .TAP, check that bytes still
264                  to be read are enough to complete it. */
265            if (length - i + j >= 4)
266            {
267               /* Here we read the 3 following bytes, using an index j
268                      j = 0 -> The 0x00 byte: we simply skip everything and go on
269                      j = 1,2 -> The 1st and 2nd bytes after 0x00: we store them and go on
270                      j = 3 -> The final byte of the pulse length: we store it,
271                      and then we pass to finally output the wave */
272               if (j > 0)
273               {
274                  over_pulse_bytes[j-1] = byte;
275                  j += 1;
276
277                  if (j >= 4)
278                  {
279                     over_pulse_length = ((over_pulse_bytes[2] << 16) | (over_pulse_bytes[1] << 8) | over_pulse_bytes[0]) >> 3;
280                     byte_samples = tap_data_to_samplecount(over_pulse_length, tap_frequency);
281                     /* waveamp_high = WAVE_PAUSE; */
282                     j = 0;
283                  }
284               }
285               else
286               {
287                  j += 1;
288                  logerror("Found a 00 byte close to the end of the .tap file.\n");
289                  logerror("This is not allowed by the format specs. \n");
290                  logerror("Check if your .tap file got corrupted when you created it!\n");
291               }
292            }
293            else j = 1;
294         }
295         /* waveamp_low = WAVE_LOW; */
296      }
297
298      if (j == 0)
299      {
300         cbm_output_wave( buffer, byte_samples );
301         size += byte_samples;
302//          toggle_wave_data(waveamp_low, waveamp_high);
303         toggle_wave_data();
304         if (version < 2)
305         {
306            cbm_output_wave( buffer, byte_samples );
307            size += byte_samples;
308//              toggle_wave_data(waveamp_low, waveamp_high);
309            toggle_wave_data();
310         }
311      }
312   }
313
314   return size;
315}
316
317
318static int cbm_tap_to_wav_size( const UINT8 *tapdata, int taplen )
319{
320   int size = cbm_tap_do_work(NULL, taplen, tapdata);
321   len = taplen;
322
323   return size;
324}
325
326static int cbm_tap_fill_wave( INT16 *buffer, int length, UINT8 *bytes )
327{
328   INT16 *p = buffer;
329
330   return cbm_tap_do_work(&p, len, (const UINT8 *)bytes);
331}
332
333
334
335
336static const struct CassetteLegacyWaveFiller cbm_legacy_fill_wave = {
337   cbm_tap_fill_wave,      /* fill_wave */
338   -1,                  /* chunk_size */
339   0,                  /* chunk_samples */
340   cbm_tap_to_wav_size,   /* chunk_sample_calc */
341   CBM_WAV_FREQUENCY,      /* sample_frequency */
342   0,                  /* header_samples */
343   0                  /* trailer_samples */
344};
345
346
347static casserr_t cbm_cassette_identify( cassette_image *cassette, struct CassetteOptions *opts )
348{
349   return cassette_legacy_identify( cassette, opts, &cbm_legacy_fill_wave );
350}
351
352
353static casserr_t cbm_cassette_load( cassette_image *cassette )
354{
355   return cassette_legacy_construct( cassette, &cbm_legacy_fill_wave );
356}
357
358
359static const struct CassetteFormat cbm_tap_cassette_format = {
360   "tap",
361   cbm_cassette_identify,
362   cbm_cassette_load,
363   NULL
364};
365
366
367CASSETTE_FORMATLIST_START(cbm_cassette_formats)
368   CASSETTE_FORMAT(cbm_tap_cassette_format)
369CASSETTE_FORMATLIST_END
src/lib/formats/oric_tap.h
r0r11458
1/*********************************************************************
2
3    oric_tap.h
4
5    Format code for Oric TAP (*.tap) files
6
7*********************************************************************/
8
9#ifndef ORIC_TAP_H
10#define ORIC_TAP_H
11
12#include "cassimg.h"
13
14CASSETTE_FORMATLIST_EXTERN(oric_cassette_formats);
15
16#endif /* ORIC_TAP_H */
src/lib/formats/atom_tap.h
r0r11458
1/*********************************************************************
2
3    formats/atom_tap.h
4
5    Cassette code for Acorn Atom tap files
6
7*********************************************************************/
8
9#pragma once
10
11#ifndef __ATOM_TAP__
12#define __ATOM_TAP__
13
14#include "cassimg.h"
15
16extern const struct CassetteFormat atom_tap_format;
17
18#endif
src/lib/formats/z80ne_dsk.h
r0r11458
1/*********************************************************************
2
3    formats/z80ne_dsk.h
4
5    Nuova Elettronica Z80NE disk images
6
7*********************************************************************/
8
9#ifndef Z80NE_DSK_H
10#define Z80NE_DSK_H
11
12#include "flopimg.h"
13
14
15/**************************************************************************/
16
17FLOPPY_OPTIONS_EXTERN(z80ne);
18
19FLOPPY_IDENTIFY(z80ne_dmk_identify);
20FLOPPY_CONSTRUCT(z80ne_dmk_construct);
21
22#endif /* Z80NE_DSK_H */
src/lib/formats/cbm_tap.h
r0r11458
1/*********************************************************************
2
3    cbm_tap.h
4
5    Format code for CBM .tap files
6
7*********************************************************************/
8
9#ifndef CBM_TAP_H
10#define CBM_TAP_H
11
12#include "cassimg.h"
13
14CASSETTE_FORMATLIST_EXTERN(cbm_cassette_formats);
15
16#endif /*  CBM_TAP_H */
src/lib/formats/coupedsk.c
r0r11458
1/*************************************************************************
2
3    formats/coupedsk.c
4
5    SAM Coupe disk image formats
6
7**************************************************************************/
8
9#include "coupedsk.h"
10#include "formats/basicdsk.h"
11
12
13#define SAD_HEADER_LEN  22
14#define SAD_SIGNATURE   "Aley's disk backup"
15
16#define SDF_TRACKSIZE   (512 * 12)
17
18
19struct sdf_tag
20{
21   UINT8 heads;
22   UINT8 tracks;
23};
24
25
26
27/*************************************
28 *
29 *  MGT disk image format
30 *
31 *************************************/
32
33/*
34    Straight rip without any header
35
36    Data:
37
38    Side 0 Track 0
39    Side 1 Track 0
40    Side 0 Track 1
41    Side 1 Track 1
42    ...
43*/
44
45
46FLOPPY_CONSTRUCT( coupe_mgt_construct )
47{
48   struct basicdsk_geometry geometry;
49
50   memset(&geometry, 0, sizeof(geometry));
51   geometry.heads = 2;
52   geometry.tracks = 80;
53   geometry.sector_length = 512;
54   geometry.first_sector_id = 1;
55
56   if (params)
57   {
58      /* create */
59      geometry.sectors = option_resolution_lookup_int(params, PARAM_SECTORS);
60   }
61   else
62   {
63      /* load */
64      UINT64 size = floppy_image_size(floppy);
65
66      /* verify size */
67      if (size != 737280 && size != 819200)
68         return FLOPPY_ERROR_INVALIDIMAGE;
69
70      geometry.sectors = size / 81920;
71
72   }
73
74   return basicdsk_construct(floppy, &geometry);
75}
76
77
78FLOPPY_IDENTIFY( coupe_mgt_identify )
79{
80   UINT64 size;
81
82   size = floppy_image_size(floppy);
83
84   *vote = (size == 737280 || size == 819200) ? 100 : 0;
85   return FLOPPY_ERROR_SUCCESS;
86}
87
88
89
90/*************************************
91 *
92 *  SAD disk image format
93 *
94 *************************************/
95
96/*
97    Header (22 bytes):
98
99    0-17 "Aley's disk backup"
100    18   number of sides
101    19   number of tracks
102    20   number of sectors per track
103    21   sector size divided by 64
104
105    Data:
106
107    Side 0 Track 0
108    Side 0 Track 1
109    Side 0 Track 2
110    ...
111    Side 1 Track 0
112    Side 1 Track 1
113    ...
114*/
115
116
117static UINT64 coupe_sad_translate_offset(floppy_image *floppy,
118   const struct basicdsk_geometry *geom, int track, int head, int sector)
119{
120   return head * geom->tracks * geom->sectors + geom->sectors * track + sector;
121}
122
123
124static void coupe_sad_interpret_header(floppy_image *floppy,
125   int *heads, int *tracks, int *sectors, int *sector_size)
126{
127   UINT8 header[SAD_HEADER_LEN];
128
129   floppy_image_read(floppy, header, 0, SAD_HEADER_LEN);
130
131   *heads = header[18];
132   *tracks = header[19];
133   *sectors = header[20];
134   *sector_size = header[21] << 6;
135}
136
137
138FLOPPY_CONSTRUCT( coupe_sad_construct )
139{
140   struct basicdsk_geometry geometry;
141   int heads, tracks, sectors, sector_length;
142
143   memset(&geometry, 0, sizeof(geometry));
144
145   if (params)
146   {
147      /* create */
148      UINT8 header[SAD_HEADER_LEN];
149
150      /* get format options */
151      heads = option_resolution_lookup_int(params, PARAM_HEADS);
152      tracks = option_resolution_lookup_int(params, PARAM_TRACKS);
153      sectors = option_resolution_lookup_int(params, PARAM_SECTORS);
154      sector_length = option_resolution_lookup_int(params, PARAM_SECTOR_LENGTH);
155
156      /* build the header */
157      memset(header, 0, SAD_HEADER_LEN);
158      memcpy(header, SAD_SIGNATURE, 18);
159      header[18] = heads;
160      header[19] = tracks;
161      header[20] = sectors;
162      header[21] = sector_length >> 6;
163
164      /* and write it to disk */
165      floppy_image_write(floppy, header, 0, sizeof(header));
166   }
167   else
168   {
169      /* load */
170      coupe_sad_interpret_header(floppy, &heads, &tracks, &sectors, &sector_length);
171   }
172
173   /* fill in the data */
174   geometry.offset = SAD_HEADER_LEN;
175   geometry.heads = heads;
176   geometry.tracks = tracks;
177   geometry.sectors = sectors;
178   geometry.first_sector_id = 1;
179   geometry.sector_length = sector_length;
180   geometry.translate_offset = coupe_sad_translate_offset;
181
182   return basicdsk_construct(floppy, &geometry);
183}
184
185
186FLOPPY_IDENTIFY( coupe_sad_identify )
187{
188   int heads, tracks, sectors, sector_size;
189   UINT64 size, calculated_size;
190
191   size = floppy_image_size(floppy);
192
193   /* read values from SAD header */
194   coupe_sad_interpret_header(floppy, &heads, &tracks, &sectors, &sector_size);
195
196   /* calculate expected disk image size */
197   calculated_size = SAD_HEADER_LEN + heads * tracks * sectors * sector_size;
198
199   *vote = (size == calculated_size) ? 100 : 0;
200   return FLOPPY_ERROR_SUCCESS;
201}
202
203
204
205/*************************************
206 *
207 *  SDF disk image format
208 *
209 *  TODO: wd17xx status codes are
210 *        currently ignored
211 *
212 *************************************/
213
214static int coupe_sdf_get_heads_per_disk(floppy_image *floppy)
215{
216   struct sdf_tag *tag = (sdf_tag *)floppy_tag(floppy);
217   return tag->heads;
218}
219
220
221static int coupe_sdf_get_tracks_per_disk(floppy_image *floppy)
222{
223   struct sdf_tag *tag = (sdf_tag *)floppy_tag(floppy);
224   return tag->tracks;
225}
226
227
228static UINT32 coupe_sdf_get_track_size(floppy_image *floppy, int head, int track)
229{
230   return SDF_TRACKSIZE;
231}
232
233
234static floperr_t coupe_sdf_get_offset(floppy_image *floppy,
235   int head, int track, UINT64 *offset)
236{
237   struct sdf_tag *tag = (sdf_tag *)floppy_tag(floppy);
238
239   if (head > tag->heads || track > tag->tracks)
240      return FLOPPY_ERROR_SEEKERROR;
241
242   *offset = (head * tag->tracks + track) * SDF_TRACKSIZE;
243
244   return FLOPPY_ERROR_SUCCESS;
245}
246
247
248static floperr_t coupe_sdf_read_track(floppy_image *floppy,
249   int head, int track, UINT64 offset, void *buffer, size_t buflen)
250{
251   floperr_t err;
252   UINT64 track_offset;
253
254   /* get the offset to this track */
255   err = coupe_sdf_get_offset(floppy, head, track, &track_offset);
256   if (err) return err;
257
258   /* read track data into buffer */
259   floppy_image_read(floppy, buffer, offset + track_offset, buflen);
260
261   return FLOPPY_ERROR_SUCCESS;
262}
263
264
265static floperr_t coupe_sdf_write_track(floppy_image *floppy,
266   int head, int track, UINT64 offset, const void *buffer, size_t buflen)
267{
268   floperr_t err;
269   UINT64 track_offset;
270
271   /* get the offset to this track */
272   err = coupe_sdf_get_offset(floppy, head, track, &track_offset);
273   if (err) return err;
274
275   /* write buffer to image */
276   floppy_image_write(floppy, buffer, offset + track_offset, buflen);
277
278   return FLOPPY_ERROR_SUCCESS;
279}
280
281
282static floperr_t coupe_sdf_get_sector_offset(floppy_image *floppy,
283   int head, int track, int sector, UINT64 *offset)
284{
285   floperr_t err;
286   UINT8 buffer[SDF_TRACKSIZE];
287   int i, buffer_pos = 1;
288
289   /* get track data */
290   err = coupe_sdf_read_track(floppy, head, track, 0, &buffer, SDF_TRACKSIZE);
291   if (err) return err;
292
293   /* check if the sector is available in this track */
294   if (sector >= buffer[0])
295      return FLOPPY_ERROR_SEEKERROR;
296
297   /* find the right sector in this track */
298   for (i = 0; i < buffer[0]; i++)
299   {
300      int sector_number = buffer[buffer_pos + 4];
301
302      if (sector_number - 1 == sector)
303      {
304         *offset = buffer_pos;
305         return FLOPPY_ERROR_SUCCESS;
306      }
307
308      buffer_pos += (128 << buffer[buffer_pos + 5]) + 8;
309   }
310
311   return FLOPPY_ERROR_INVALIDIMAGE;
312}
313
314
315static floperr_t coupe_sdf_get_total_sector_offset(floppy_image *floppy,
316   int head, int track, int sector, UINT64 *offset)
317{
318   floperr_t err;
319   UINT64 track_offset, sector_offset;
320
321   /* get offset to the track start */
322   err = coupe_sdf_get_offset(floppy, head, track, &track_offset);
323   if (err) return err;
324
325   /* get offset to the start of the sector */
326   err = coupe_sdf_get_sector_offset(floppy, head, track, sector, &sector_offset);
327   if (err) return err;
328
329   *offset = track_offset + sector_offset;
330
331   return FLOPPY_ERROR_SUCCESS;
332}
333
334
335static floperr_t coupe_sdf_get_sector_length(floppy_image *floppy,
336   int head, int track, int sector, UINT32 *sector_length)
337{
338   floperr_t err;
339   UINT8 buffer[SDF_TRACKSIZE];
340   UINT64 offset;
341
342   /* get track data */
343   err = coupe_sdf_read_track(floppy, head, track, 0, &buffer, SDF_TRACKSIZE);
344   if (err) return err;
345
346   /* get offset to the start of the sector */
347   err = coupe_sdf_get_sector_offset(floppy, head, track, sector, &offset);
348   if (err) return err;
349
350   /* get size */
351   *sector_length = 128 << buffer[offset + 5];
352
353   return FLOPPY_ERROR_SUCCESS;
354}
355
356
357static floperr_t coupe_sdf_get_indexed_sector_info(floppy_image *floppy,
358   int head, int track, int sector_index,
359   int *cylinder, int *side, int *sector, UINT32 *sector_length,
360   unsigned long *flags)
361{
362   floperr_t err;
363   UINT8 buffer[SDF_TRACKSIZE];
364   UINT64 offset;
365
366   /* get track data */
367   err = coupe_sdf_read_track(floppy, head, track, 0, &buffer, SDF_TRACKSIZE);
368   if (err) return err;
369
370   /* get offset to the start of the sector */
371   err = coupe_sdf_get_sector_offset(floppy, head, track, sector_index, &offset);
372   if (err) return err;
373
374   /* extract data */
375   if (cylinder)      *cylinder      = buffer[offset + 2];
376   if (side)          *side          = buffer[offset + 3];
377   if (sector)        *sector        = buffer[offset + 4];
378   if (sector_length) *sector_length = 128 << buffer[offset + 5];
379   if (flags)         *flags        = 0;   /* TODO: read DAM or DDAM and determine flags */
380
381   return FLOPPY_ERROR_SUCCESS;
382}
383
384
385static floperr_t coupe_sdf_read_indexed_sector(floppy_image *floppy,
386   int head, int track, int sector, void *buffer, size_t buflen)
387{
388   floperr_t err;
389   UINT64 offset;
390
391   err = coupe_sdf_get_total_sector_offset(floppy, head, track, sector, &offset);
392   if (err) return err;
393
394   /* read sector data into buffer */
395   floppy_image_read(floppy, buffer, offset + 8, buflen);
396
397   return FLOPPY_ERROR_SUCCESS;
398}
399
400
401static floperr_t coupe_sdf_write_indexed_sector(floppy_image *floppy,
402   int head, int track, int sector, const void *buffer, size_t buflen, int ddam)
403{
404   floperr_t err;
405   UINT64 offset;
406
407   err = coupe_sdf_get_total_sector_offset(floppy, head, track, sector, &offset);
408   if (err) return err;
409
410   /* write buffer into image */
411   floppy_image_write(floppy, buffer, offset + 8, buflen);
412
413   return FLOPPY_ERROR_SUCCESS;
414}
415
416
417static void coupe_sdf_interpret_header(floppy_image *floppy, int *heads, int *tracks)
418{
419   UINT64 size = floppy_image_size(floppy);
420
421   if (size % SDF_TRACKSIZE == 0)
422   {
423      *heads = 2;
424      *tracks = size / (SDF_TRACKSIZE * 2);
425   }
426   else
427   {
428      *heads = 0;
429      *tracks = 0;
430   }
431}
432
433
434FLOPPY_CONSTRUCT( coupe_sdf_construct )
435{
436   struct FloppyCallbacks *callbacks;
437   struct sdf_tag *tag;
438   int heads, tracks;
439
440   if (params)
441   {
442      /* create */
443      return FLOPPY_ERROR_UNSUPPORTED;
444   }
445   else
446   {
447      /* load */
448      coupe_sdf_interpret_header(floppy, &heads, &tracks);
449   }
450
451   tag = (sdf_tag *)floppy_create_tag(floppy, sizeof(struct sdf_tag));
452
453   if (!tag)
454      return FLOPPY_ERROR_OUTOFMEMORY;
455
456   tag->heads = heads;
457   tag->tracks = tracks;
458
459   callbacks = floppy_callbacks(floppy);
460   callbacks->read_track = coupe_sdf_read_track;
461   callbacks->write_track = coupe_sdf_write_track;
462   callbacks->get_heads_per_disk = coupe_sdf_get_heads_per_disk;
463   callbacks->get_tracks_per_disk = coupe_sdf_get_tracks_per_disk;
464   callbacks->get_track_size = coupe_sdf_get_track_size;
465   callbacks->get_sector_length = coupe_sdf_get_sector_length;
466   callbacks->read_indexed_sector = coupe_sdf_read_indexed_sector;
467   callbacks->write_indexed_sector = coupe_sdf_write_indexed_sector;
468   callbacks->get_indexed_sector_info = coupe_sdf_get_indexed_sector_info;
469
470   return FLOPPY_ERROR_SUCCESS;
471}
472
473
474FLOPPY_IDENTIFY( coupe_sdf_identify )
475{
476   int heads, tracks;
477
478   /* read header values */
479   coupe_sdf_interpret_header(floppy, &heads, &tracks);
480
481   /* check for sensible values */
482   if (heads > 0 && heads < 3 && tracks > 0 && tracks < 84)
483      *vote = 100;
484   else
485      *vote = 0;
486
487   return FLOPPY_ERROR_SUCCESS;
488}
src/lib/formats/coupedsk.h
r0r11458
1/*************************************************************************
2
3    formats/coupedsk.h
4
5    SAM Coupe disk image formats
6
7**************************************************************************/
8
9#ifndef __COUPEDSK_H__
10#define __COUPEDSK_H__
11
12#include "flopimg.h"
13
14
15FLOPPY_CONSTRUCT( coupe_mgt_construct );
16FLOPPY_IDENTIFY( coupe_mgt_identify );
17FLOPPY_CONSTRUCT( coupe_sad_construct );
18FLOPPY_IDENTIFY( coupe_sad_identify );
19FLOPPY_CONSTRUCT( coupe_sdf_construct );
20FLOPPY_IDENTIFY( coupe_sdf_identify );
21
22
23#endif /* __COUPEDSK_H__ */
src/lib/formats/svi_dsk.c
r0r11458
1/*********************************************************************
2
3    formats/svi_dsk.c
4
5    SVI318 disk images
6
7*********************************************************************/
8
9#include <string.h>
10
11#include "svi_dsk.h"
12#include "basicdsk.h"
13
14static FLOPPY_IDENTIFY(svi_dsk_identify)
15{
16   *vote = ((floppy_image_size(floppy) == (172032)) || (floppy_image_size(floppy) == (346112))) ? 100 : 0;
17   return FLOPPY_ERROR_SUCCESS;
18}
19
20static int svi_get_heads_per_disk(floppy_image *floppy)
21{
22   return (floppy_image_size(floppy) == (172032)) ? 1 : 2;
23}
24
25static int svi_get_tracks_per_disk(floppy_image *floppy)
26{
27   return 40;
28}
29
30static UINT64 svi_translate_offset(floppy_image *floppy,
31    int track, int head, int sector)
32{
33   UINT64 o;
34   if ((track==0) && (head==0))
35      o = sector*128;
36   else
37      o = ((track*((floppy_image_size(floppy) == (172032)) ? 1 : 2)+head)*17+sector)*256-2048; /* (17*256)-(18*128)=2048 */
38
39   return o;
40}
41
42static floperr_t get_offset(floppy_image *floppy, int head, int track, int sector, int sector_is_index, UINT64 *offset)
43{
44   UINT64 offs;
45   /* translate the sector to a raw sector */
46   if (!sector_is_index)
47   {
48      sector -= 1;
49   }
50   /* check to see if we are out of range */
51   if ((head < 0) || (head >= ((floppy_image_size(floppy) == (172032)) ? 1 : 2)) || (track < 0) || (track >= 40)
52         || (sector < 0) || (sector >= ((head==0 && track==0) ? 18 : 17)))
53      return FLOPPY_ERROR_SEEKERROR;
54
55   offs = svi_translate_offset(floppy, track, head, sector);
56   if (offset)
57      *offset = offs;
58   return FLOPPY_ERROR_SUCCESS;
59}
60
61
62
63static floperr_t internal_svi_read_sector(floppy_image *floppy, int head, int track, int sector, int sector_is_index, void *buffer, size_t buflen)
64{
65   UINT64 offset;
66   floperr_t err;
67   err = get_offset(floppy, head, track, sector, sector_is_index, &offset);
68   if (err)
69      return err;
70
71   floppy_image_read(floppy, buffer, offset, buflen);
72   return FLOPPY_ERROR_SUCCESS;
73}
74
75
76
77static floperr_t internal_svi_write_sector(floppy_image *floppy, int head, int track, int sector, int sector_is_index, const void *buffer, size_t buflen, int ddam)
78{
79   UINT64 offset;
80   floperr_t err;
81
82   err = get_offset(floppy, head, track, sector, sector_is_index, &offset);
83   if (err)
84      return err;
85
86   floppy_image_write(floppy, buffer, offset, buflen);
87   return FLOPPY_ERROR_SUCCESS;
88}
89
90
91
92static floperr_t svi_read_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen)
93{
94   return internal_svi_read_sector(floppy, head, track, sector, FALSE, buffer, buflen);
95}
96
97static floperr_t svi_write_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam)
98{
99   return internal_svi_write_sector(floppy, head, track, sector, FALSE, buffer, buflen, ddam);
100}
101
102static floperr_t svi_read_indexed_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen)
103{
104   return internal_svi_read_sector(floppy, head, track, sector, TRUE, buffer, buflen);
105}
106
107static floperr_t svi_write_indexed_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam)
108{
109   return internal_svi_write_sector(floppy, head, track, sector, TRUE, buffer, buflen, ddam);
110}
111
112static floperr_t svi_get_sector_length(floppy_image *floppy, int head, int track, int sector, UINT32 *sector_length)
113{
114   floperr_t err;
115   err = get_offset(floppy, head, track, sector, FALSE, NULL);
116   if (err)
117      return err;
118
119   if (sector_length) {
120      *sector_length = (head==0 && track==0) ? 128 : 256;
121   }
122   return FLOPPY_ERROR_SUCCESS;
123}
124
125
126
127static floperr_t svi_get_indexed_sector_info(floppy_image *floppy, int head, int track, int sector_index, int *cylinder, int *side, int *sector, UINT32 *sector_length, unsigned long *flags)
128{
129   sector_index += 1;
130   if (cylinder)
131      *cylinder = track;
132   if (side)
133      *side = head;
134   if (sector)
135      *sector = sector_index;
136   if (flags)
137      /* TODO: read DAM or DDAM and determine flags */
138      *flags = 0;
139   return svi_get_sector_length(floppy, head, track, sector_index, sector_length);
140}
141
142
143static FLOPPY_CONSTRUCT(svi_dsk_construct)
144{
145   struct FloppyCallbacks *callbacks;
146   callbacks = floppy_callbacks(floppy);
147   callbacks->read_sector = svi_read_sector;
148   callbacks->write_sector = svi_write_sector;
149   callbacks->read_indexed_sector = svi_read_indexed_sector;
150   callbacks->write_indexed_sector = svi_write_indexed_sector;
151   callbacks->get_sector_length = svi_get_sector_length;
152   callbacks->get_heads_per_disk = svi_get_heads_per_disk;
153   callbacks->get_tracks_per_disk = svi_get_tracks_per_disk;
154   callbacks->get_indexed_sector_info = svi_get_indexed_sector_info;
155
156   return FLOPPY_ERROR_SUCCESS;
157}
158
159
160
161/* ----------------------------------------------------------------------- */
162
163FLOPPY_OPTIONS_START( svi318 )
164   FLOPPY_OPTION( svi_dsk, "dsk", "SVI-318 floppy disk image",   svi_dsk_identify, svi_dsk_construct, NULL, NULL)
165   FLOPPY_OPTION( svi_cpm, "dsk", "SVI-728 DSDD CP/M disk image", basicdsk_identify_default, basicdsk_construct_default, NULL,
166      HEADS([2])
167      TRACKS([40])
168      SECTORS([17])
169      SECTOR_LENGTH([256])
170      FIRST_SECTOR_ID([1]))
171FLOPPY_OPTIONS_END
src/lib/formats/ami_dsk.c
r0r11458
1/*********************************************************************
2
3    formats/ami_dsk.c
4
5    Amiga disk images
6
7*********************************************************************/
8
9
10#include <string.h>
11
12#include "formats/ami_dsk.h"
13#include "formats/basicdsk.h"
14
15
16
17/*****************************************************************************
18 Amiga floppy core functions
19*****************************************************************************/
20
21
22static FLOPPY_IDENTIFY( amiga_dsk_identify )
23{
24   UINT64 size;
25
26   *vote = 100;
27
28   /* first check the size of the image */
29   size = floppy_image_size(floppy);
30   if ((size != 901120) && (size != 1802240))
31      *vote = 0;
32
33   return FLOPPY_ERROR_SUCCESS;
34}
35
36
37static FLOPPY_CONSTRUCT( amiga_dsk_construct )
38{
39   struct basicdsk_geometry geometry;
40
41   /* setup geometry with standard values */
42   memset(&geometry, 0, sizeof(geometry));
43   geometry.heads = 2;
44   geometry.tracks = 80;
45   geometry.first_sector_id = 0;
46   geometry.sector_length = 512;
47
48   if (params)
49   {
50      /* create */
51      geometry.sectors = option_resolution_lookup_int(params, PARAM_SECTORS);
52   }
53   else
54   {
55      /* open */
56      UINT64 size = floppy_image_size(floppy);
57      geometry.sectors = size/512/80/2;
58      if (geometry.sectors != 11 && geometry.sectors != 22)
59         return FLOPPY_ERROR_INVALIDIMAGE;
60   }
61
62   return basicdsk_construct(floppy, &geometry);
63}
64
65
66
67/*****************************************************************************
68 Amiga floppy options
69*****************************************************************************/
70
71
72FLOPPY_OPTIONS_START( amiga )
73   FLOPPY_OPTION(
74      ami_dsk,
75      "adf",
76      "Amiga floppy disk image",
77      amiga_dsk_identify,
78      amiga_dsk_construct,
79      NULL,
80      HEADS([2])
81      TRACKS([80])
82      SECTORS([11]/22)
83   )
84FLOPPY_OPTIONS_END
85
86FLOPPY_OPTIONS_START( amiga_only )
87   FLOPPY_OPTION(
88      ami_dsk,
89      "adf",
90      "Amiga floppy disk image",
91      amiga_dsk_identify,
92      amiga_dsk_construct,
93      NULL,
94      HEADS([2])
95      TRACKS([80])
96      SECTORS([11]/22)
97   )
98FLOPPY_OPTIONS_END0
src/lib/formats/svi_dsk.h
r0r11458
1/*********************************************************************
2
3    formats/svi_dsk.h
4
5    SVI318 disk images
6
7*********************************************************************/
8
9#ifndef SVI_DSK_H
10#define SVI_DSK_H
11
12#include "flopimg.h"
13
14/**************************************************************************/
15
16FLOPPY_OPTIONS_EXTERN(svi318);
17
18#endif /* SVI_DSK_H */
src/lib/formats/trs_dsk.c
r0r11458
1#include <string.h>
2#include "trs_dsk.h"
3#include "basicdsk.h"
4#include "coco_dsk.h"
5
6/* -----------------------------------------------------------------------
7 * JV1 (Jeff Vavasour 1) format
8 *
9 * Used by Jeff Vavasour's TRS-80 Emulators
10 *
11 * Very straight basic disk; 1 head, 10 sectors, 256 sector length
12 * ----------------------------------------------------------------------- */
13
14#define TRS80_JV1_HEADS            1
15#define TRS80_JV1_SECTORS         10
16#define TRS80_JV1_SECTORLENGTH      256
17#define TRS80_JV1_FIRSTSECTORID      0
18
19static FLOPPY_IDENTIFY( trs80_jv1_identify )
20{
21   UINT64 size;
22   size = floppy_image_size(floppy);
23   *vote = (size % (TRS80_JV1_HEADS * TRS80_JV1_SECTORS * TRS80_JV1_SECTORLENGTH))
24      ? 0 : 100;
25   return FLOPPY_ERROR_SUCCESS;
26}
27
28
29static UINT64 trs80_jv1_get_ddam(floppy_image *floppy, const struct basicdsk_geometry *geom, int track, int head, int sector)
30{
31   // directory track is protected
32   if ((track==17) && (head==0)) {
33      return ID_FLAG_DELETED_DATA;
34   }
35   return 0;
36}
37
38static FLOPPY_CONSTRUCT( trs80_jv1_construct )
39{
40   struct basicdsk_geometry geometry;
41
42   memset(&geometry, 0, sizeof(geometry));
43   geometry.heads            = TRS80_JV1_HEADS;
44   geometry.sectors         = TRS80_JV1_SECTORS;
45   geometry.first_sector_id   = TRS80_JV1_FIRSTSECTORID;
46   geometry.sector_length      = TRS80_JV1_SECTORLENGTH;
47   geometry.get_ddam         = trs80_jv1_get_ddam;
48
49   if (params)
50   {
51      /* create */
52      geometry.tracks = option_resolution_lookup_int(params, PARAM_TRACKS);
53   }
54   else
55   {
56      /* load */
57      geometry.tracks = (int) (floppy_image_size(floppy) / geometry.heads
58         / geometry.sectors / geometry.sector_length);
59   }
60   return basicdsk_construct(floppy, &geometry);
61}
62
63
64
65/* ----------------------------------------------------------------------- */
66
67FLOPPY_OPTIONS_START( trs80 )
68   FLOPPY_OPTION( trs80_jv1, "dsk",         "TRS-80 JV1 disk image",   trs80_jv1_identify,   trs80_jv1_construct, NULL,
69      TRACKS([35]-255))
70   FLOPPY_OPTION( trs80_dmk, "dsk,dmk",      "TRS-80 DMK disk image",   coco_dmk_identify,   coco_dmk_construct, NULL,
71      HEADS([1]-2)
72      TRACKS([35]-255)
73      SECTORS(1-[10]-18)
74      SECTOR_LENGTH(128/[256]/512/1024/2048/4096/8192)
75      INTERLEAVE(0-[6]-17)
76      FIRST_SECTOR_ID([0]-1))
77FLOPPY_OPTIONS_END
src/lib/formats/apf_apt.c
r0r11458
1/* .APT tape images */
2
3
4#include "formats/apf_apt.h"
5
6
7#define WAVEENTRY_LOW  -32768
8#define WAVEENTRY_HIGH  32767
9#define WAVEENTRY_NULL  0
10
11/* frequency of wave */
12#define APF_WAV_FREQUENCY   11050
13
14#define APF_APT_BIT_0_LENGTH ((int)(APF_WAV_FREQUENCY*0.0005))
15#define APF_APT_BIT_1_LENGTH ((int)(APF_WAV_FREQUENCY*0.001))
16
17struct apf_t
18{
19   int cassette_length;
20};
21static apf_t apf;
22
23
24/* 500 microsecond of bit 0 and 1000 microsecond of bit 1 */
25
26static INT16 *apf_emit_level(INT16 *p, int count, INT16 wave_state)
27{
28   int i;
29
30   for (i=0; i<count; i++)
31   {
32      *(p++) = wave_state;
33   }
34   return p;
35}
36
37/* 4 periods at 1200Hz */
38static INT16* apf_output_bit(INT16 *p, UINT8 b)
39{
40   if (b)
41   {
42      p = apf_emit_level(p, (APF_APT_BIT_1_LENGTH>>1), WAVEENTRY_HIGH);
43      p = apf_emit_level(p, (APF_APT_BIT_1_LENGTH>>1), WAVEENTRY_LOW);
44   }
45   else
46   {
47      p = apf_emit_level(p, (APF_APT_BIT_0_LENGTH>>1), WAVEENTRY_HIGH);
48      p = apf_emit_level(p, (APF_APT_BIT_0_LENGTH>>1), WAVEENTRY_LOW);
49   }
50
51   return p;
52}
53
54static int apf_get_bit_size_in_samples(UINT8 b)
55{
56   if (b)
57   {
58      return APF_APT_BIT_1_LENGTH;
59   }
60
61   return APF_APT_BIT_0_LENGTH;
62}
63
64
65/*************************************************************************************/
66
67/* each bit in the data represents a "1" or "0" waveform */
68static int apf_cassette_calculate_size_in_samples(const UINT8 *bytes, int length)
69{
70   unsigned i;
71   int size;
72   int b;
73   UINT8 data;
74
75   size = 0;
76   apf.cassette_length = length;
77
78
79   for (i=0; i<length; i++)
80   {
81      data = bytes[i];
82
83      for (b=0; b<8; b++)
84      {
85         size += apf_get_bit_size_in_samples((data>>7) & 0x01);
86         data = data<<1;
87      }
88   }
89
90   return size;
91}
92
93/*************************************************************************************/
94
95static int apf_cassette_fill_wave(INT16 *buffer, int length, UINT8 *bytes)
96{
97   int i;
98   INT16 *p;
99   UINT8 data;
100   int b;
101
102   p = buffer;
103
104   for (i=0; i<apf.cassette_length; i++)
105   {
106      data = bytes[i];
107      for (b=0; b<8; b++)
108      {
109         p = apf_output_bit(p,(data>>7) & 0x01);
110         data = data<<1;
111      }
112   }
113   return p - buffer;
114}
115
116
117
118static const struct CassetteLegacyWaveFiller apf_legacy_fill_wave =
119{
120   apf_cassette_fill_wave,               /* fill_wave */
121   -1,                              /* chunk_size */
122   0,                              /* chunk_samples */
123   apf_cassette_calculate_size_in_samples,   /* chunk_sample_calc */
124   APF_WAV_FREQUENCY,                  /* sample_frequency */
125   0,                              /* header_samples */
126   0                              /* trailer_samples */
127};
128
129
130
131static casserr_t apf_apt_identify(cassette_image *cassette, struct CassetteOptions *opts)
132{
133   return cassette_legacy_identify(cassette, opts, &apf_legacy_fill_wave);
134}
135
136
137
138static casserr_t apf_apt_load(cassette_image *cassette)
139{
140   return cassette_legacy_construct(cassette, &apf_legacy_fill_wave);
141}
142
143
144
145static const struct CassetteFormat apf_apt_format =
146{
147   "apt",
148   apf_apt_identify,
149   apf_apt_load,
150   NULL
151};
152
153
154
155CASSETTE_FORMATLIST_START(apf_cassette_formats)
156   CASSETTE_FORMAT(apf_apt_format)
157CASSETTE_FORMATLIST_END
src/lib/formats/ami_dsk.h
r0r11458
1/*********************************************************************
2
3    formats/ami_dsk.h
4
5    Amiga disk images
6
7*********************************************************************/
8
9#ifndef AMI_DSK_H_
10#define AMI_DSK_H_
11
12#include "flopimg.h"
13
14FLOPPY_OPTIONS_EXTERN(amiga);
15FLOPPY_OPTIONS_EXTERN(amiga_only);
16
17#endif /*AMI_DSK_H_*/
src/lib/formats/trs_dsk.h
r0r11458
1/*********************************************************************
2
3    formats/trs_dsk.h
4
5    TRS-80 disk images
6
7*********************************************************************/
8
9#ifndef TRS_DSK_H
10#define TRS_DSK_H
11
12#include "flopimg.h"
13
14/**************************************************************************/
15
16FLOPPY_OPTIONS_EXTERN( trs80 );
17
18#endif /* TRS_DSK_H */
src/lib/formats/fm7_cas.c
r0r11458
1/*
2 * Fujitsu FM-7 series cassette handling
3 */
4
5#include "fm7_cas.h"
6
7#define WAVE_HIGH       0x5a9e
8#define WAVE_LOW      -0x5a9e
9
10static int cas_size;
11
12static int fm7_fill_wave(INT16* buffer, UINT8 high, UINT8 low, int sample_pos)
13{
14   UINT16 data = (high << 8) + low;
15   int sample_count = 0;
16   int x = 0;
17   int count = (data & 0x7fff);
18
19   if(data & 0x8000)
20   {
21      for(x=0;x<count;x++)
22      {
23         if(buffer)
24            buffer[sample_pos+x] = WAVE_HIGH;
25      }
26   }
27   else
28   {
29      for(x=0;x<count;x++)
30      {
31         if(buffer)
32            buffer[sample_pos+x] = WAVE_LOW;
33      }
34   }
35
36   sample_count += count;
37   return sample_count;
38}
39
40static int fm7_handle_t77(INT16* buffer, const UINT8* casdata)
41{
42   int sample_count = 0;
43   int data_pos = 16;
44
45   if(memcmp(casdata, "XM7 TAPE IMAGE 0",16))  // header check
46      return -1;
47
48   while(data_pos < cas_size)
49   {
50      sample_count += fm7_fill_wave(buffer,casdata[data_pos],casdata[data_pos+1],sample_count);
51      data_pos+=2;
52   }
53
54   return sample_count;
55}
56
57/*******************************************************************
58   Calculate the number of samples needed for this tape image
59********************************************************************/
60static int fm7_cas_to_wav_size (const UINT8 *casdata, int caslen)
61{
62   cas_size = caslen;
63
64   return fm7_handle_t77(NULL,casdata);
65}
66
67/*******************************************************************
68   Generate samples for the tape image
69********************************************************************/
70static int fm7_cas_fill_wave(INT16 *buffer, int sample_count, UINT8 *bytes)
71{
72   return fm7_handle_t77(buffer,bytes);
73}
74
75static const struct CassetteLegacyWaveFiller fm7_legacy_fill_wave =
76{
77   fm7_cas_fill_wave,                  /* fill_wave */
78   -1,                              /* chunk_size */
79   0,                              /* chunk_samples */
80   fm7_cas_to_wav_size,               /* chunk_sample_calc */
81   125000,                           /* sample_frequency */
82   0,                              /* header_samples */
83   0                              /* trailer_samples */
84};
85
86static casserr_t fm7_cas_identify(cassette_image *cassette, struct CassetteOptions *opts)
87{
88   return cassette_legacy_identify(cassette, opts, &fm7_legacy_fill_wave);
89}
90
91
92
93static casserr_t fm7_cas_load(cassette_image *cassette)
94{
95   return cassette_legacy_construct(cassette, &fm7_legacy_fill_wave);
96}
97
98
99static const struct CassetteFormat fm7_cassette_format = {
100   "t77",
101   fm7_cas_identify,
102   fm7_cas_load,
103   NULL
104};
105
106CASSETTE_FORMATLIST_START(fm7_cassette_formats)
107   CASSETTE_FORMAT(fm7_cassette_format)
108CASSETTE_FORMATLIST_END
src/lib/formats/apf_apt.h
r0r11458
1#ifndef APFCAS_H
2#define APFCAS_H
3
4#include "cassimg.h"
5
6CASSETTE_FORMATLIST_EXTERN(apf_cassette_formats);
7
8
9#endif /* APFCAS_H */
src/lib/formats/fm7_cas.h
r0r11458
1/*
2 * Fujitsu FM-7 cassette format handling
3 */
4
5#ifndef FM7_CAS_H_
6#define FM7_CAS_H_
7
8#include "cassimg.h"
9
10CASSETTE_FORMATLIST_EXTERN( fm7_cassette_formats );
11
12#endif /*FM7_CAS_H_*/
src/lib/formats/orao_cas.c
r0r11458
1/*
2
3    Tape support for Orao  TAP format
4
5*/
6#include "orao_cas.h"
7
8
9#define ORAO_WAV_FREQUENCY   44100
10#define WAVE_HIGH      -32768
11#define WAVE_LOW      0
12
13#define ORAO_WAVE_ONE   24
14#define ORAO_WAVE_ZERO   11
15
16#define ORAO_HEADER_SIZE 360
17
18static INT16   wave_data;
19static int     len;
20
21static void orao_output_wave( INT16 **buffer, int length ) {
22   if ( buffer == NULL ) {
23      return;
24   }
25
26   for( ; length > 0; length-- ) {
27      **buffer = wave_data;
28      *buffer = *buffer + 1;
29   }
30}
31static int orao_cas_to_wav_size( const UINT8 *casdata, int caslen ) {
32   int i,j,size;
33   UINT8 b;
34
35   if (casdata == NULL) return -1;
36   if (caslen <= ORAO_HEADER_SIZE) {
37      return -1;
38   }
39   size = 0;
40   for (i=ORAO_HEADER_SIZE;i<caslen-ORAO_HEADER_SIZE;i++) {
41      for (j=0;j<8;j++) {
42         b = (casdata[i] >> j) & 1;
43         if (b==0) {
44            size += 2*ORAO_WAVE_ZERO;
45         } else {
46            size += 2*ORAO_WAVE_ONE;
47         }
48      }
49   }
50   len = caslen;
51   return size;
52}
53
54static int orao_cas_fill_wave( INT16 *buffer, int length, UINT8 *bytes ) {
55   int i,j,size;
56   UINT8 b;
57   size = 0;
58   if (bytes == NULL) return -1;
59   for (i=ORAO_HEADER_SIZE;i<len-ORAO_HEADER_SIZE;i++) {
60      for (j=0;j<8;j++) {
61         b = (bytes[i] >> j) & 1;
62         if (b==0) {
63            wave_data = WAVE_LOW;
64            orao_output_wave(&buffer,ORAO_WAVE_ZERO);
65            wave_data = WAVE_HIGH;
66            orao_output_wave(&buffer,ORAO_WAVE_ZERO);
67            size += 2 *ORAO_WAVE_ZERO;
68         } else {
69            wave_data = WAVE_LOW;
70            orao_output_wave(&buffer,ORAO_WAVE_ONE);
71            wave_data = WAVE_HIGH;
72            orao_output_wave(&buffer,ORAO_WAVE_ONE);
73            size += 2 * ORAO_WAVE_ONE;
74         }
75      }
76   }
77
78   return size;
79}
80
81
82
83
84static const struct CassetteLegacyWaveFiller orao_legacy_fill_wave = {
85   orao_cas_fill_wave,         /* fill_wave */
86   -1,               /* chunk_size */
87   0,               /* chunk_samples */
88   orao_cas_to_wav_size,         /* chunk_sample_calc */
89   ORAO_WAV_FREQUENCY,         /* sample_frequency */
90   0,               /* header_samples */
91   0               /* trailer_samples */
92};
93
94
95
96static casserr_t orao_cassette_identify( cassette_image *cassette, struct CassetteOptions *opts ) {
97   return cassette_legacy_identify( cassette, opts, &orao_legacy_fill_wave );
98}
99
100
101
102static casserr_t orao_cassette_load( cassette_image *cassette ) {
103   return cassette_legacy_construct( cassette, &orao_legacy_fill_wave );
104}
105
106
107
108static const struct CassetteFormat orao_cassette_format = {
109   "tap",
110   orao_cassette_identify,
111   orao_cassette_load,
112   NULL
113};
114
115
116CASSETTE_FORMATLIST_START(orao_cassette_formats)
117   CASSETTE_FORMAT(orao_cassette_format)
118CASSETTE_FORMATLIST_END
119
120
src/lib/formats/orao_cas.h
r0r11458
1/*
2
3       orao_cas.h
4
5       Format code for orao cassette files
6*/
7
8#ifndef ORAO_CAS_H
9#define ORAO_CAS_H
10
11#include "cassimg.h"
12
13CASSETTE_FORMATLIST_EXTERN(orao_cassette_formats);
14
15#endif /* ORAO_CAS_H */
src/lib/formats/p6001_cas.c
r0r11458
1/*
2 * Nec PC-6001 cassette format handling
3 */
4
5#include "p6001_cas.h"
6
7#define WAVE_HIGH       0x5a9e
8#define WAVE_LOW      -0x5a9e
9
10static int cas_size;
11
12static int pc6001_fill_wave(INT16* buffer, UINT8 data, int sample_pos)
13{
14   int x;
15   int sample_count = 0;
16
17   // one byte = 8 samples
18   for(x=0;x<8;x++)
19   {
20      if(buffer)
21         buffer[sample_pos+x] = ((data >> (7-x)) & 1) ? WAVE_HIGH : WAVE_LOW;
22   }
23
24   sample_count+=8;
25   return sample_count;
26}
27
28static int pc6001_handle_cas(INT16* buffer, const UINT8* casdata)
29{
30   int sample_count = 0;
31   int data_pos = 0;
32
33   while(sample_count < cas_size)
34   {
35      sample_count += pc6001_fill_wave(buffer,casdata[data_pos],sample_count);
36      data_pos++;
37   }
38
39   return sample_count;
40}
41
42/*******************************************************************
43   Calculate the number of samples needed for this tape image
44********************************************************************/
45static int pc6001_cas_to_wav_size (const UINT8 *casdata, int caslen)
46{
47   cas_size = caslen*8;
48
49   return pc6001_handle_cas(NULL,casdata);
50}
51
52/*******************************************************************
53   Generate samples for the tape image
54********************************************************************/
55static int pc6001_cas_fill_wave(INT16 *buffer, int sample_count, UINT8 *bytes)
56{
57   return pc6001_handle_cas(buffer,bytes);
58}
59
60static const struct CassetteLegacyWaveFiller pc6001_legacy_fill_wave =
61{
62   pc6001_cas_fill_wave,               /* fill_wave */
63   -1,                              /* chunk_size */
64   0,                              /* chunk_samples */
65   pc6001_cas_to_wav_size,               /* chunk_sample_calc */
66   8000,                           /* sample_frequency */
67   0,                              /* header_samples */
68   0                              /* trailer_samples */
69};
70
71static casserr_t pc6001_cas_identify(cassette_image *cassette, struct CassetteOptions *opts)
72{
73   return cassette_legacy_identify(cassette, opts, &pc6001_legacy_fill_wave);
74}
75
76
77
78static casserr_t pc6001_cas_load(cassette_image *cassette)
79{
80   return cassette_legacy_construct(cassette, &pc6001_legacy_fill_wave);
81}
82
83
84static const struct CassetteFormat pc6001_cassette_format = {
85   "cas",
86   pc6001_cas_identify,
87   pc6001_cas_load,
88   NULL
89};
90
91CASSETTE_FORMATLIST_START(pc6001_cassette_formats)
92   CASSETTE_FORMAT(pc6001_cassette_format)
93CASSETTE_FORMATLIST_END
src/lib/formats/wavfile.c
r0r11458
1/*********************************************************************
2
3    wavfile.c
4
5    Format code for wave (*.wav) files
6
7*********************************************************************/
8
9#include <stdio.h>
10#include <assert.h>
11
12#include "wavfile.h"
13#include "cassimg.h"
14
15static const char magic1[4] = { 'R', 'I', 'F', 'F' };
16static const char magic2[4] = { 'W', 'A', 'V', 'E' };
17static const char format_tag_id[4] = { 'f', 'm', 't', ' ' };
18static const char data_tag_id[4] = { 'd', 'a', 't', 'a' };
19
20#define WAV_FORMAT_PCM      1
21
22
23
24static UINT32 get_leuint32(const void *ptr)
25{
26   UINT32 value;
27   memcpy(&value, ptr, sizeof(value));
28   return LITTLE_ENDIANIZE_INT32(value);
29}
30
31
32
33static UINT16 get_leuint16(const void *ptr)
34{
35   UINT16 value;
36   memcpy(&value, ptr, sizeof(value));
37   return LITTLE_ENDIANIZE_INT16(value);
38}
39
40
41
42static void put_leuint32(void *ptr, UINT32 value)
43{
44   value = LITTLE_ENDIANIZE_INT32(value);
45   memcpy(ptr, &value, sizeof(value));
46}
47
48
49
50static void put_leuint16(void *ptr, UINT16 value)
51{
52   value = LITTLE_ENDIANIZE_INT16(value);
53   memcpy(ptr, &value, sizeof(value));
54}
55
56
57
58static casserr_t wavfile_process(cassette_image *cassette, struct CassetteOptions *opts,
59   int read_waveform)
60{
61   UINT8 file_header[12];
62   UINT8 tag_header[8];
63   UINT8 format_tag[16];
64   UINT32 stated_size;
65   UINT64 file_size;
66   UINT32 tag_size;
67   UINT32 tag_samples;
68   UINT64 offset;
69   int format_specified = FALSE;
70
71   UINT16 format_type = 0;
72   UINT32 bytes_per_second = 0;
73   UINT16 block_align = 0;
74   int waveform_flags = 0;
75
76   /* read header */
77   cassette_image_read(cassette, file_header, 0, sizeof(file_header));
78   offset = sizeof(file_header);
79
80   /* check magic numbers */
81   if (memcmp(&file_header[0], magic1, 4))
82      return CASSETTE_ERROR_INVALIDIMAGE;
83   if (memcmp(&file_header[8], magic2, 4))
84      return CASSETTE_ERROR_INVALIDIMAGE;
85
86   /* read and sanity check size */
87   stated_size = get_leuint32(&file_header[4]) + 8;
88   file_size = cassette_image_size(cassette);
89   if (stated_size > file_size)
90      stated_size = (UINT32) file_size;
91
92   while(offset < stated_size)
93   {
94      cassette_image_read(cassette, tag_header, offset, sizeof(tag_header));
95      tag_size = get_leuint32(&tag_header[4]);
96      offset += sizeof(tag_header);
97
98      if (!memcmp(tag_header, format_tag_id, 4))
99      {
100         /* format tag */
101         if (format_specified || (tag_size < sizeof(format_tag)))
102            return CASSETTE_ERROR_INVALIDIMAGE;
103         format_specified = TRUE;
104
105         cassette_image_read(cassette, format_tag, offset, sizeof(format_tag));
106
107         format_type            = get_leuint16(&format_tag[0]);
108         opts->channels         = get_leuint16(&format_tag[2]);
109         opts->sample_frequency   = get_leuint32(&format_tag[4]);
110         bytes_per_second      = get_leuint32(&format_tag[8]);
111         block_align            = get_leuint16(&format_tag[12]);
112         opts->bits_per_sample   = get_leuint16(&format_tag[14]);
113
114         if (format_type != WAV_FORMAT_PCM)
115            return CASSETTE_ERROR_INVALIDIMAGE;
116         if (opts->sample_frequency * opts->bits_per_sample * opts->channels / 8 != bytes_per_second)
117            return CASSETTE_ERROR_INVALIDIMAGE;
118
119         switch(opts->bits_per_sample)
120         {
121            case 8:
122               waveform_flags = CASSETTE_WAVEFORM_8BIT;
123               break;
124            case 16:
125               waveform_flags = CASSETTE_WAVEFORM_16BITLE;
126               break;
127            case 32:
128               waveform_flags = CASSETTE_WAVEFORM_32BITLE;
129               break;
130            default:
131               return CASSETTE_ERROR_INVALIDIMAGE;
132         }
133      }
134      else if (!memcmp(tag_header, data_tag_id, 4))
135      {
136         /* data tag */
137         if (!format_specified)
138            return CASSETTE_ERROR_INVALIDIMAGE;
139
140         if (read_waveform)
141         {
142            tag_samples = tag_size / (opts->bits_per_sample / 8) / opts->channels;
143            cassette_read_samples(cassette, opts->channels, 0.0, tag_samples / ((double) opts->sample_frequency),
144               tag_samples, offset, waveform_flags);
145         }
146      }
147      else
148      {
149         /* ignore other tags */
150      }
151      offset += tag_size;
152   }
153
154   return CASSETTE_ERROR_SUCCESS;
155}
156
157
158
159static casserr_t wavfile_identify(cassette_image *cassette, struct CassetteOptions *opts)
160{
161   return wavfile_process(cassette, opts, FALSE);
162}
163
164
165
166static casserr_t wavfile_load(cassette_image *cassette)
167{
168   struct CassetteOptions opts;
169   memset(&opts, 0, sizeof(opts));
170   return wavfile_process(cassette, &opts, TRUE);
171}
172
173
174
175static casserr_t wavfile_save(cassette_image *cassette, const struct CassetteInfo *info)
176{
177   casserr_t err;
178   UINT8 consolidated_header[12 + 8 + 16 + 8];
179   UINT8 *header            = &consolidated_header[0];
180   UINT8 *format_tag_header   = &consolidated_header[12];
181   UINT8 *format_tag_data      = &consolidated_header[12 + 8];
182   UINT8 *data_tag_header      = &consolidated_header[12 + 8 + 16];
183   UINT32 file_size;
184   UINT32 bytes_per_second;
185   UINT16 bits_per_sample;
186   UINT32 data_size;
187   size_t bytes_per_sample = 2;
188   int waveform_flags = CASSETTE_WAVEFORM_16BITLE;
189   UINT16 block_align;
190
191   bits_per_sample = (UINT16) (bytes_per_sample * 8);
192   bytes_per_second = info->sample_frequency * bytes_per_sample * info->channels;
193   data_size = (UINT32) (info->sample_count * bytes_per_sample * info->channels);
194   file_size = data_size + sizeof(consolidated_header) - 8;
195   block_align = (UINT16) (bytes_per_sample * info->channels);
196
197   /* set up header */
198   memcpy(&header[0],               magic1, 4);
199   memcpy(&header[8],               magic2, 4);
200   put_leuint32(&header[4],         file_size);
201
202   /* set up format tag */
203   memcpy(&format_tag_header[0],      format_tag_id, 4);
204   put_leuint32(&format_tag_header[4],   16);
205   put_leuint16(&format_tag_data[0],   WAV_FORMAT_PCM);
206   put_leuint16(&format_tag_data[2],   info->channels);
207   put_leuint32(&format_tag_data[4],   info->sample_frequency);
208   put_leuint32(&format_tag_data[8],   bytes_per_second);
209   put_leuint16(&format_tag_data[12],   block_align);
210   put_leuint16(&format_tag_data[14],   bits_per_sample);
211
212   /* set up data tag */
213   memcpy(&data_tag_header[0],         data_tag_id, 4);
214   put_leuint32(&data_tag_header[4],   data_size);
215
216   /* write consolidated header */
217   cassette_image_write(cassette, consolidated_header, 0, sizeof(consolidated_header));
218
219   /* write out the actual data */
220   err = cassette_write_samples(cassette, info->channels, 0.0, info->sample_count
221      / (double) info->sample_frequency, info->sample_count, sizeof(consolidated_header),
222      waveform_flags);
223   if (err)
224      return err;
225
226   return CASSETTE_ERROR_SUCCESS;
227}
228
229
230
231const struct CassetteFormat wavfile_format =
232{
233   "wav",
234   wavfile_identify,
235   wavfile_load,
236   wavfile_save
237};
238
239
240
241/*********************************************************************
242    wavfile_testload()
243
244    This is a hokey function used to test the cassette wave loading
245    system, specifically to test that when one loads a WAV file image
246    that the resulting info queried will be the same data in the WAV.
247
248    This code has already identified some rounding errors
249*********************************************************************/
250
251#ifdef UNUSED_FUNCTION
252void wavfile_testload(const char *fname)
253{
254   cassette_image *cassette;
255   FILE *f;
256   long offset;
257   int freq, samples, i;
258   INT32 cassamp;
259   INT16 wavsamp;
260
261   f = fopen(fname, "rb");
262   if (!f)
263      return;
264
265   if (cassette_open(f, &stdio_ioprocs, &wavfile_format, CASSETTE_FLAG_READONLY, &cassette))
266   {
267      fclose(f);
268      return;
269   }
270
271   offset = 44;
272   freq = 44100;
273   samples = 5667062;
274
275   for (i = 0; i < samples; i++)
276   {
277      cassette_get_sample(cassette, 0, i / (double) freq, 0.0, &cassamp);
278
279      fseek(f, offset + i * 2, SEEK_SET);
280      fread(&wavsamp, 1, 2, f);
281      assert(cassamp == (((UINT32) wavsamp) << 16));
282   }
283
284   cassette_close(cassette);
285
286   fclose(f);
287}
288#endif
289
290
291
src/lib/formats/ti99_dsk.c
r0r11458
1/*********************************************************************
2 *
3 * formats/ti99_dsk.c
4 *
5 * TI99 and Geneve disk images
6 * Sector Dump and Track Dump format
7 *
8 * used by TI-99/4, TI-99/4A, TI-99/8, SGCPU ("TI-99/4P"), and Geneve
9 *
10 * The Sector Dump Format is also known as v9t9 (named after the first TI
11 * emulator to use this format). It is a contiguous sequence of sector contents
12 * without track data. The first sector of the disk is located at the start of
13 * the image, while the last sector is at its end.
14 *
15 * The Track Dump Format is also known as pc99 (again, named after the first
16 * TI emulator to use this format). It is a contiguous sequence of track
17 * contents, containing all information including address marks and CRC, but it
18 * does not contain clock signals. Therefore, the format requires that the
19 * address marks be at the same positions within each track.
20 *
21 * Both formats allow for a broad range of medium sizes. All sectors are 256
22 * bytes long. The most common formats are 9 sectors per track, single-sided,
23 * 40 tracks, which yields 90 KiB of sector data (known as SSSD), and 18
24 * sectors per track, double-sided, and 40 tracks, which is 360 KiB (known as
25 * DSDD). There are rare occurances of 8/16 sectors/track
26 * (prototypical TI double-density controller) and 35 track media. Newer
27 * controllers and ROMs allow for up to 36 sectors per track and 80 tracks on
28 * both sides, which is 2,88 MiB (DSHD80).
29 *
30 * Different to earlier implementations, these format implementations provide
31 * full track read/write capabilities. For the SDF format, the missing track
32 * information is recreated, and a common interleave is assumed.
33 *
34 * Note that in principle, writing tracks may change track sizes.
35 * As neither one of the formats contains meta-information about track lengths,
36 * and as the image consists of a contiguous sequence of the tracks, we
37 * cannot allow different track sizes currently. Accordingly, the track size
38 * cannot be changed after the image has been created by imgtool.
39 * Hence, also sector count per tracks and recording type (FM or MFM) cannot be
40 * changed without breaking the image.
41 * For example, MFM disks with 18 sectors per track can only be reformatted to
42 * MFM/18. Moreover, while this could be handled in a future revision,
43 * the number of tracks should also remain unchanged.
44 *
45 * You can use blank image files (filled with zeros or arbitrary data) and use
46 * the Disk Manager cartridge to properly format them. Also, this can be done
47 * by imgtool. The image file must match the format length (representing the
48 * space on the physical medium).
49 *
50 * 80 track drive handling
51 *
52 * For the TI system there may be a situation where 40 track disks are used
53 * in 80 track drives. In these cases, the disk driver applies double-stepping,
54 * that is, it advances the head twice in order to seek the next track. We
55 * emulate this by dividing the track number by 2, which means that two tracks
56 * are mapped to one (0,1 -> 0; 2,3 -> 1; 4,5 -> 2; ...; 78,79 -> 39)
57 *
58 * Technical detail: The size of the track must be given before actually
59 * starting write_track as the controller implementation uses buffered
60 * transfer.
61 *
62 * Michael Zapf, Feb 2010
63 *
64 * TODO: Link to imgtool. imgtool still uses its own format creation
65 *
66 * FIXME: If image is broken with good first track and defect higher tracks,
67 *        tdf_guess_geometry fails to define a geometry. The guessing should
68 *        always be done.
69 ********************************************************************/
70
71#include <string.h>
72#include <assert.h>
73#include <time.h>
74
75#include "osdepend.h"
76#include "imageutl.h"
77#include "ti99_dsk.h"
78
79#define SECTOR_SIZE 256
80#define TI99_IDAM_LENGTH 7
81
82/*
83    Determines whether we are using 80 track drives. This variable is
84    necessary unless we get information about the dip
85    switch settings. It is set by the disk controller implementations
86    (bwg, hfdc, ti_fdc).
87*/
88static int use_80_track_drives = FALSE;
89
90struct ti99dsk_geometry
91{
92   UINT8 sides;
93   UINT8 tracksperside;
94   UINT8 secspertrack;
95   UINT8 density;
96};
97
98struct ti99dsk_tag
99{
100   int heads;
101   int tracks;
102   int sectors;
103   int track_size;  // Complete track; to be calculated
104   int format;
105   int first_idam;
106   int dam_offset;
107};
108
109/*
110    Parametrizes the format to use 80 track drives. For TI systems we
111    need to be able to emulate 40 track drives and 80 track drives.
112    This is set in the configuration, but the format has no direct access.
113    So as a preliminary solution we use this global function which sets the
114    flag. This function is called by the disk controller implementations
115    (bwg, hfdc, ti_fdc).
116*/
117void ti99_set_80_track_drives(int use80)
118{
119   use_80_track_drives = use80;
120}
121
122#define TI99_DSK_TAG   "ti99dsktag"
123#define TI99DSK_BLOCKNOTFOUND -1
124
125/*
126    Searches a block containing number * byte, starting at the given
127    position. Returns the position of the first byte of the block.
128*/
129static int find_block(const UINT8 *buffer, int start, int stop, UINT8 byte, size_t number)
130{
131   int i = start;
132   size_t current = number;
133   while (i < stop && current > 0)
134   {
135      if (buffer[i++] != byte)
136      {
137         current = number;
138      }
139      else
140      {
141         current--;
142      }
143   }
144   if (current==0)
145   {
146      return i - number;
147   }
148   else
149      return TI99DSK_BLOCKNOTFOUND;
150}
151
152/*
153    Queried by flopdrv. Instead of the image tracks, we return the tracks
154    of the floppy drive. We assume we always have two heads.
155*/
156static int ti99_get_heads_per_disk(floppy_image *floppy)
157{
158//  struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy);
159//  return tag->heads;
160   return 2;
161}
162
163/*
164    Queried by flopdrv. Instead of the image tracks, we return the tracks
165    of the floppy drive.
166*/
167static int ti99_get_tracks_per_disk(floppy_image *floppy)
168{
169   int drivetracks = 42;
170
171   // This may fail on startup; therefore we use a special function in
172   // in the floppy controller implementations to explicitly set the geometry.
173   if (use_80_track_drives)
174   {
175      drivetracks = 83;
176   }
177   return drivetracks;
178}
179
180static floperr_t ti99_get_sector_length(floppy_image *floppy, int head, int track, int sector, UINT32 *sector_length)
181{
182   *sector_length=SECTOR_SIZE;
183   return FLOPPY_ERROR_SUCCESS;
184}
185
186/*
187    Return the track size. We do not allow different track
188    sizes, so we store the track size in the tag.
189*/
190static UINT32 ti99_get_track_size(floppy_image *floppy, int head, int track)
191{
192   struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy);
193   return tag->track_size;
194}
195
196/*
197static void create_vib(UINT8 *sector0, option_resolution *params)
198{
199    const char *name = "UNNAMED   ";
200    int sides, tracksperside,secspertrack,totalsectors,ausize,allocbytes,i;
201
202    enum { NAME=0, TOTALSECS=10, SECSPERTRACK=11, SIG=12, PROT=16, TRACKS=17, SIDES=18, DENSITY=19, EXT=20, ALLOC=56 };
203
204    sides       = option_resolution_lookup_int(params, PARAM_HEADS);
205    tracksperside   = option_resolution_lookup_int(params, PARAM_TRACKS);
206    secspertrack    = option_resolution_lookup_int(params, PARAM_SECTORS);
207
208    totalsectors = sides * tracksperside * secspertrack;
209
210    memcpy(&sector0[NAME], name, 10);
211
212    sector0[TOTALSECS] = (totalsectors>>8)&0xff;
213    sector0[TOTALSECS+1] = (totalsectors&0xff);
214
215    sector0[SECSPERTRACK] = secspertrack;
216    sector0[SIG] = 'D';
217    sector0[SIG+1] = 'S';
218    sector0[SIG+2] = 'K';
219
220    sector0[PROT] = ' ';
221    sector0[TRACKS] = tracksperside;
222    sector0[SIDES] = sides;
223    sector0[DENSITY] = (secspertrack<10)? 0x01 : 0x02;
224
225    for (i=EXT; i < ALLOC; i++)
226        sector0[i] = 0;
227
228    ausize = (totalsectors / 1441) + 1;
229
230    allocbytes = totalsectors / ausize / 8;
231
232    for (i=ALLOC; i < ALLOC+allocbytes; i++)
233        sector0[i] = 0;
234
235    while (i++ < SECTOR_SIZE)
236        sector0[i] = 0xff;
237}
238*/
239/* =========================================================================
240             Sector dump format
241   =========================================================================*/
242
243static floperr_t ti99_sdf_read_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen);
244static floperr_t ti99_sdf_write_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam);
245
246/* -----------------------------------------------------------------------
247 * Guess the geometry of the sector dump disk.
248 *
249 * Sector size is always 256 bytes.
250 *
251 * Common formats:
252 * 90 KiB = 40 tracks / 1 side / 9 sectors = SSSD
253 * 180 KiB = 40 tracks / 2 side / 9 sectors = DSSD (most common)
254 *         = 40 tracks / 1 side / 18 sectors = SSDD (rare)
255 *         = 80 tracks / 1 side / 9 sectors = SSSD80 (rare)
256 * 360 KiB = 40 tracks / 2 side / 18 sectors = DSDD (most common)
257 *         = 80 tracks / 2 side / 9 sectors = DSSD80 (rare)
258 *         = 80 tracks / 1 side / 18 sectors = SSDD80 (rare)
259 *         = 40 tracks / 1 side / 36 sectors = SSHD (rare)
260 * 720 KiB = 80 tracks / 2 side / 18 sectors = DSDD80 (most common)
261 * 1440 KiB= 80 tracks / 2 side / 36 sectors = DSHD80 (most common)
262 *
263 * Moreover, there may be formats with 35 tracks (ancient) and 8 sectors/track.
264 * We only check for the 8 sector formats.
265 * 160 KiB = 40 tracks / 1 side / 16 sectors = SSDD8
266 * 320 KiB = 40 tracks / 2 side / 16 sectors = DSDD8
267 *
268 * The Volume Information Block may contain suitable information, but not
269 * before the disk is formatted and initialized.
270 *
271 * returns 100 if recognized, 0 if not recognized
272 * ----------------------------------------------------------------------- */
273
274static int ti99_sdf_guess_geometry(floppy_image *floppy, UINT64 size,
275   struct ti99dsk_geometry *geometry)
276{
277   int totsecs;
278   typedef struct ti99_vib
279   {
280      char   name[10];      // volume name (10 characters, pad with spaces)
281      UINT8   totsecsMSB;      // disk length in sectors (big-endian) (usually 360, 720 or 1440)
282      UINT8   totsecsLSB;
283      UINT8   secspertrack;   // sectors per track (usually 9 (FM) or 18 (MFM))
284      UINT8   id[3];         // String "DSK"
285      UINT8   protection;      // 'P' if disk is protected, ' ' otherwise.
286      UINT8   tracksperside;   // tracks per side (usually 40)
287      UINT8   sides;         // sides (1 or 2)
288      UINT8   density;      // 0,1 (FM) or 2,3,4 (MFM)
289      UINT8   res[36];      // Empty for traditional disks, or up to 3 directory pointers
290      UINT8   abm[200];      // allocation bitmap: a 1 for each sector in use (sector 0 is LSBit of byte 0,
291                        // sector 7 is MSBit of byte 0, sector 8 is LSBit of byte 1, etc.)
292   } ti99_vib;
293
294   ti99_vib vib;
295   UINT32 file_size;
296   struct ti99dsk_geometry dummy_geometry;
297
298   if (geometry)
299      memset(geometry, 0, sizeof(*geometry));
300   else
301      geometry = &dummy_geometry;
302
303   if (size > 0xFFFFFFFF)
304      return 0;
305
306   file_size = (UINT32) size;
307
308   // Read the Volume Information Block
309   floppy_image_read(floppy, &vib, 0, sizeof(vib));
310
311   // If we have read the sector successfully, let us parse it
312   totsecs = (vib.totsecsMSB << 8) | vib.totsecsLSB;
313   geometry->secspertrack = vib.secspertrack;
314   if (geometry->secspertrack == 0)
315      // Some images might be like this, because the original SSSD
316      // TI controller always assumes 9.
317      geometry->secspertrack = 9;
318   geometry->tracksperside = vib.tracksperside;
319   if (geometry->tracksperside == 0)
320      // Some images are like this, because the original SSSD TI controller
321      // always assumes 40.
322      geometry->tracksperside = 40;
323   geometry->sides = vib.sides;
324   if (geometry->sides == 0)
325      // Some images are like this, because the original SSSD TI controller
326      // always assumes that tracks beyond 40 are on side 2. */
327      geometry->sides = totsecs / (geometry->secspertrack * geometry->tracksperside);
328   geometry->density = vib.density;
329   // check that the format makes sense
330   if (((geometry->secspertrack * geometry->tracksperside * geometry->sides) == totsecs)
331      && (geometry->density <= 4) && (totsecs >= 2) && (! memcmp(vib.id, "DSK", 3))
332      && (file_size == totsecs*256))
333   {
334      logerror("SDF/VIB consistent; tracks = %d, heads = %d, sectors = %d\n", geometry->tracksperside, geometry->sides, geometry->secspertrack);
335      return 100;
336   }
337   logerror("SDF/VIB not consistent; guessing format\n");
338
339   // So that was not consistent. We guess the size from the file size
340   // and assume that the VIB did not contain reliable data. For the
341   // ambiguous case we choose the most common format.
342
343   switch (file_size)
344   {
345   case 1*40*9*256:
346      // 90kbytes: SSSD
347   case 0:
348   /*default:*/
349      geometry->sides = 1;
350      geometry->tracksperside = 40;
351      geometry->secspertrack = 9;
352      geometry->density = 1;
353      break;
354
355   case 2*40*9*256:
356      // 180kbytes: either DSSD or 18-sector-per-track SSDD.
357      // We assume DSSD since DSSD is more common and is supported by
358      // the original TI SD disk controller.
359      geometry->sides = 2;
360      geometry->tracksperside = 40;
361      geometry->secspertrack = 9;
362      geometry->density = 1;
363      break;
364
365   case 1*40*16*256:
366      // 160kbytes: 16-sector-per-track SSDD (standard format for TI
367      // DD disk controller prototype, and the TI hexbus disk
368      // controller?) */
369      geometry->sides = 1;
370      geometry->tracksperside = 40;
371      geometry->secspertrack = 16;
372      geometry->density = 2;
373      break;
374
375   case 2*40*16*256:
376      // 320kbytes: 16-sector-per-track DSDD (standard format for TI
377      // DD disk controller prototype, and TI hexbus disk
378      // controller?)
379      geometry->sides = 2;
380      geometry->tracksperside = 40;
381      geometry->secspertrack = 16;
382      geometry->density = 2;
383      break;
384
385   case 2*40*18*256:
386      //  360kbytes: 18-sector-per-track DSDD (standard format for most
387      // third-party DD disk controllers, but reportedly not supported by
388      // the original TI DD disk controller prototype)
389      geometry->sides = 2;
390      geometry->tracksperside = 40;
391      geometry->secspertrack = 18;
392      geometry->density = 2;
393      break;
394
395   case 2*80*18*256:
396      // 720kbytes: 18-sector-per-track 80-track DSDD (Myarc only)
397      geometry->sides = 2;
398      geometry->tracksperside = 80;
399      geometry->secspertrack = 18;
400      geometry->density = 2;
401      break;
402
403   case 2*80*36*256:
404      // 1.44Mbytes: DSHD (Myarc only)
405      geometry->sides = 2;
406      geometry->tracksperside = 80;
407      geometry->secspertrack = 36;
408      geometry->density = 3;
409      break;
410
411   default:
412      logerror("Unrecognized disk image geometry\n");
413      return 0;
414   }
415
416   logerror("SDF geometry guess: tracks = %d, heads = %d, sectors = %d\n", geometry->tracksperside, geometry->sides, geometry->secspertrack);
417   return 100;
418}
419
420/*
421    Figure out the interleave for the sector arrangement in the track. For
422    SDF format this is required for the read_track function.
423
424    Interleave:
425        0 7 5 3 1 8 6 4 2;
426             start with 0, 3, 6, 0, 3, 6, ...
427            0 11 4 15 8 1 12 5 16 9 2 13 6 17 10 3 14 7
428             start with 0 for every track
429
430    Rare formats: 8 sectors
431        0 3 6 1 4 7 2 5   (i*3)%8
432
433        16 sectors
434        0 9 2 11 4 13 6 15 8 1 10 3 12 5 14 7  (i*9)%16
435
436        36 sectors
437        0 11 22 ... 3 14 25 (i*11)%36
438*/
439static void guess_interleave(int sectors, int *step, int *initial)
440{
441   switch (sectors)
442   {
443   case 8:
444      *step = 3;
445      /* initial is the offset from track to track+1 of the starting sector number */
446      *initial = 0;
447      break;
448   case 9:
449      *step = 7;
450      *initial = 3;
451      break;
452   case 16:
453      *step = 9;
454      *initial = 0;
455      break;
456   case 18:
457      *step = 11;
458      *initial = 0;
459      break;
460   case 36:
461      *step = 11;
462      *initial = 0;
463      break;
464   default:
465      *step = 11;
466      *initial = 0;
467      break;
468   }
469}
470
471/*
472    Delivers a whole track. As the image consists only of
473    sector data, we must reconstruct the track data and
474    then insert the sector data.
475
476    The track layout is exactly the Track Dump Format, except that the
477    CRC values must be correctly calculated.
478
479    Total length (FM)  = 247 + sectors*334; (sectors=8 or 9)
480    Total length (MFM) = 752 + sectors*340; (sectors=16 or 18)
481*/
482static floperr_t ti99_sdf_read_track(floppy_image *floppy, int head, int track, UINT64 offset, void *buffer, size_t buflen)
483{
484   int sector;
485   int startstep;
486   int step;
487   int position=0;
488   unsigned short crc;
489   int i;
490   floperr_t err;
491   int imgtrack = track;
492
493   UINT8 *trackdata = (UINT8*)buffer;
494
495   /* Determine from the image geometry whether we have FM or MFM */
496   struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy);
497
498   // Do we have a 40 track disk in an 80 track drive? In that case we
499   // double the "width" of the tracks. */
500   if (use_80_track_drives && tag->tracks<=40)
501   {
502      imgtrack = track / 2;
503   }
504
505   guess_interleave(tag->sectors, &step, &startstep);
506
507   if (tag->sectors < 10)  /* must be FM */
508   {
509      /* Write lead-in. */
510      memset(trackdata, 0x00, 16);
511      position += 16;
512      for (i=0; i < tag->sectors; i++)
513      {
514         /* Create interleaving */
515         sector = (i*step + imgtrack*startstep) % tag->sectors;
516
517         memset(&trackdata[position], 0x00, 6);
518         position += 6;
519         /* Set IDAM */
520         trackdata[position++] = 0xfe;
521         trackdata[position++] = imgtrack;
522         trackdata[position++] = head;
523         trackdata[position++] = sector;
524         trackdata[position++] = 0x01;
525         /* Calculate CRC16. Preset to 0xffff (what is 0xcdb4?) */
526         crc = ccitt_crc16(0xffff, &trackdata[position-(TI99_IDAM_LENGTH-2)], TI99_IDAM_LENGTH - 2);
527         trackdata[position++] = (crc>>8)&0xff;
528         trackdata[position++] = crc & 0xff;
529
530         /* Write Gap2 */
531         memset(&trackdata[position], 0xff, 11);
532         position += 11;
533         memset(&trackdata[position], 0x00, 6);
534         position += 6;
535         /* Write DAM */
536         trackdata[position++] = 0xfb;
537
538         /* Locate the sector content in the image and load it. */
539         err = ti99_sdf_read_sector(floppy, head, imgtrack, sector, &trackdata[position], SECTOR_SIZE);
540         if (err)
541            return err;
542
543         position += SECTOR_SIZE;
544
545         /* Set CRC16 */
546         crc = ccitt_crc16(0xffff, &trackdata[position-(SECTOR_SIZE+1)], SECTOR_SIZE+1);
547         trackdata[position++] = (crc>>8)&0xff;
548         trackdata[position++] = crc & 0xff;
549
550         /* Write Gap3 */
551         memset(&trackdata[position], 0xff, 45);
552         position += 45;
553      }
554      /* Write lead-out */
555      memset(&trackdata[position], 0xff, 231);
556      position += 231;
557   }
558   else /* we have MFM */
559   {
560      /* Write lead-in. */
561      memset(trackdata, 0x4e, 40);
562      position += 40;
563      for (i=0; i < tag->sectors; i++)
564      {
565         sector = (i*step + imgtrack*startstep) % tag->sectors;
566
567         memset(&trackdata[position], 0x00, 10);
568         position += 10;
569         /* Write sync */
570         memset(&trackdata[position], 0xa1, 3);
571         position += 3;
572         /* Set IDAM */
573         trackdata[position++] = 0xfe;
574         trackdata[position++] = imgtrack;
575         trackdata[position++] = head;
576         trackdata[position++] = sector;
577         trackdata[position++] = 0x01;
578         /* Set CRC16 */
579         crc = ccitt_crc16(0xffff, &trackdata[position-TI99_IDAM_LENGTH+2], TI99_IDAM_LENGTH - 2);
580         trackdata[position++] = (crc>>8)&0xff;
581         trackdata[position++] = crc & 0xff;
582
583         /* Write Gap2 */
584         memset(&trackdata[position], 0x4e, 22);
585         position += 22;
586         memset(&trackdata[position], 0x00, 12);
587         position += 12;
588         /* Write sync */
589         memset(&trackdata[position], 0xa1, 3);
590         position += 3;
591         /* Write DAM */
592         trackdata[position++] = 0xfb;
593
594         /* Locate the sector content in the image and load it. */
595         err = ti99_sdf_read_sector(floppy, head, track, sector, &trackdata[position], SECTOR_SIZE);
596         if (err)
597            return err;
598
599         position += SECTOR_SIZE;
600
601         /* Set CRC16 */
602         crc = ccitt_crc16(0xffff, &trackdata[position-SECTOR_SIZE-1], SECTOR_SIZE+1);
603         trackdata[position++] = (crc>>8)&0xff;
604         trackdata[position++] = crc & 0xff;
605
606         /* Write Gap3 */
607         memset(&trackdata[position], 0x4e, 24);
608         position += 24;
609      }
610      /* Write lead-out */
611      memset(&trackdata[position], 0x4e, 712);
612      position += 712;
613   }
614   return FLOPPY_ERROR_SUCCESS;
615}
616
617/*
618    Writes a whole track. As the sector dump format does not include track
619    data, we have to extract the sector contents and to write them
620    in sequence into the image. All the rest (gaps, crc, sync bytes) are
621    dropped here.
622    Bytes written to locations beyond the image end will be droppped
623    silently (as if the medium is unwritable from that point).
624*/
625static floperr_t ti99_sdf_write_track(floppy_image *floppy, int head, int track, UINT64 offset, const void *buffer, size_t buflen)
626{
627   int current_pos = 0;
628   UINT8 *track_image;
629   int leadin, gap1, gap2;
630   int is_fm, found;
631   struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy);
632   int imgtrack = track;
633
634   // Find out if we are going to write an FM or MFM track. Note that the
635   // original TI controller is known to have a bug in the formatting
636   // routine (using a MOVB instead of a MOV instruction when setting the
637   // pointer to the track data), outputting false bytes at the
638   // beginning of the track. So we do not rely on absolute positions
639   // (and neither does the controller).
640   track_image = (UINT8*)buffer;
641
642   if (use_80_track_drives && tag->tracks<=40)
643   {
644      imgtrack = track / 2;
645   }
646
647   /* Only search in the first 100 bytes for the start. */
648   leadin = 40;
649   gap1 = 10;
650   gap2 = 12;
651   is_fm = FALSE;
652
653   current_pos = find_block(track_image, 0, 100, 0x4e, leadin);
654
655   // In case of defect formats, we continue as far as possible. This
656   // may lead to sectors not being written. */
657   if (current_pos==TI99DSK_BLOCKNOTFOUND)
658   {
659      /* Not MFM, possibly FM image */
660      /* Again, find Lead-in */
661      leadin = 16;
662      current_pos = find_block(track_image, 0, 100, 0x00, leadin);
663      if (current_pos==TI99DSK_BLOCKNOTFOUND)
664      {
665         /* If neither, forget about this process completely. */
666         logerror("Cannot find lead-in for track %d, head %d.\n", track, head);
667         return FLOPPY_ERROR_INVALIDIMAGE;
668      }
669      gap1 = 6;
670      gap2 = 6;
671      is_fm = TRUE;
672   }
673
674   found = FALSE;
675   /* Get behind lead-in */
676   current_pos += leadin;
677   while (current_pos < buflen)
678   {
679      /* We must find the address block to determine the sector. */
680      int new_pos = find_block(track_image, current_pos, buflen, 0x00, gap1);
681      if (new_pos==TI99DSK_BLOCKNOTFOUND)
682      {
683         /* Forget about the rest. */
684         if (found) break;  /* we were already successful, so all ok */
685         logerror("Cannot find gap1 for track %d, head %d.\n", track, head);
686         return FLOPPY_ERROR_INVALIDIMAGE;
687      }
688      found = TRUE;
689
690      if (!is_fm)
691         new_pos += 3; /* skip sync bytes in MFM */
692
693      if (track_image[new_pos + gap1]==0xfe)
694      {
695         current_pos = new_pos + gap1  + 1;
696         /* IDAM found. */
697         int wtrack = track_image[current_pos];
698         int whead = track_image[current_pos+1];
699         int sector = track_image[current_pos+2];
700         if (wtrack == imgtrack && whead == head)
701         {
702            /* We skip the first part of gap2. */
703            new_pos = find_block(track_image, current_pos, buflen, 0x00, gap2);
704            if (current_pos==TI99DSK_BLOCKNOTFOUND)
705            {
706               logerror("Cannot find gap2 for track %d, head %d.\n", imgtrack, head);
707               return FLOPPY_ERROR_INVALIDIMAGE;
708            }
709            else
710            {
711               new_pos += gap2;
712               if (!is_fm)
713                  new_pos += 3; /* skip sync */
714               current_pos = new_pos;
715               if (track_image[current_pos]==0xfb)
716               {
717                  /* DAM found. We now write the sector content to the image. */
718                  ti99_sdf_write_sector(floppy, head, track, sector, &track_image[current_pos+1], SECTOR_SIZE, 0);
719                  current_pos += SECTOR_SIZE;
720               }
721               else
722                  logerror("DAM not found. Not writing write sector %d\n", sector);
723               /* else not found, ignore this sector. */
724            }
725         }
726         else
727         {
728            // else the sector head data do not match the
729            // current track and head. Ignore the sector.
730            logerror("Wrong track: wtrack=%d, imgtrack=%d, whead=%d, imghead=%d\n",wtrack,imgtrack, whead,head);
731         }
732      }
733      else
734      {
735         /* One step forward. Retry. */
736         current_pos++;
737      }
738   }
739   return FLOPPY_ERROR_SUCCESS;
740}
741
742/*
743    This method creates a properly formatted image.
744
745    For a proper image, we need to format all tracks, and to create a
746    minimal filesystem:
747
748    name = "UNNAMED   "
749    number of sectors
750    sectors/track
751    "DSK"
752    protection = 0x00
753    tracks/side
754    sides
755    density (0,1 = FM, 2 = MFM)
756    0x14 - 0x37 : 0x00
757    Allocation bitmap: 0xFF except for the first #sectors/AU size
758
759    AU size = 1 (360 - 1440 sectors)
760            = 2 (1441 - 2880 sectors)
761        = 4 (2881 - 5760 sectors)
762        = 8 (5760 - 11520 sectors)
763
764    sector 0 =
765    sector 1 = 256* 0x00
766*/
767/* static floperr_t ti99_sdf_format_track(floppy_image *floppy, int head, int track, option_resolution *params)
768{
769    UINT8 *sector0 = (UINT8*)malloc(SECTOR_SIZE);
770    create_vib(sector0, params);
771    free(sector0);
772    if (true)
773    {
774        printf("not implemented\n");
775        return FLOPPY_ERROR_SEEKERROR;
776    }
777    return FLOPPY_ERROR_SUCCESS;
778}
779*/
780
781/*
782    Get the offset from the start of the image file. In this format, logical
783    tracks on two-sided disks are arranged as follows (example for 40
784    tracks; accordingly for 35 or 80)
785
786    head-track
787    0-0
788    0-1
789    0-2
790    0-3
791    ...
792    0-39
793    1-39
794    1-38
795    1-37
796    ...
797    1-0
798    Note that we take the imgtrack, which may be half of the drive track.
799*/
800static floperr_t ti99_sdf_get_offset(floppy_image *floppy, int head, int imgtrack, int sector, UINT64 *offset)
801{
802   struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy);
803
804   if ((head < 0) || (head >= tag->heads)
805      || (imgtrack < 0) || (imgtrack >= tag->tracks)
806      || (sector < 0) || (sector > tag->sectors))
807      return FLOPPY_ERROR_SEEKERROR;
808
809   if (head == 0)  /* track numbers increasing towards inner track */
810   {
811      *offset = (imgtrack * tag->sectors + sector) * SECTOR_SIZE;
812   }
813   else      /* track numbers increasing towards outer track */
814   {
815      *offset = (((2*tag->tracks)-1-imgtrack) * tag->sectors + sector) * SECTOR_SIZE;
816   }
817   return FLOPPY_ERROR_SUCCESS;
818}
819
820/*
821    Read one sector at the specified position.
822*/
823static floperr_t ti99_sdf_read_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen)
824{
825   floperr_t err;
826   UINT64 offset;
827   int imgtrack = track;
828   struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy);
829
830   if (use_80_track_drives && tag->tracks<=40)
831   {
832      imgtrack = track / 2;
833   }
834
835   err = ti99_sdf_get_offset(floppy, head, imgtrack, sector, &offset);
836   if (err)
837   {
838      return err;
839   }
840   floppy_image_read(floppy, buffer, offset, SECTOR_SIZE);
841
842   return FLOPPY_ERROR_SUCCESS;
843}
844
845/*
846    For the SDF format, sectors are always numbered in ascending order,
847    starting with the lowest numbered sector in each track. In other words,
848    we have nothing to calculate here.
849*/
850static floperr_t ti99_sdf_read_indexed_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen)
851{
852   return ti99_sdf_read_sector(floppy, head, track, sector, buffer, buflen);
853}
854
855/*
856    Write one sector at the specified position.
857*/
858static floperr_t ti99_sdf_write_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam)
859{
860   UINT64 offset;
861   floperr_t err;
862   int imgtrack = track;
863
864   struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy);
865
866   if (use_80_track_drives && tag->tracks<=40)
867   {
868      imgtrack = track / 2;
869   }
870
871   err = ti99_sdf_get_offset(floppy, head, imgtrack, sector, &offset);
872   if (err)
873   {
874      return err;
875   }
876   floppy_image_write(floppy, buffer, offset, SECTOR_SIZE);
877
878   return FLOPPY_ERROR_SUCCESS;
879}
880
881/*
882    See above; indexing and numbering coincide.
883*/
884static floperr_t ti99_sdf_write_indexed_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam)
885{
886   return ti99_sdf_write_sector(floppy, head, track, sector, buffer, buflen, ddam);
887}
888
889/*
890    Required for ReadAddress command
891*/
892static floperr_t ti99_sdf_get_indexed_sector_info(floppy_image *floppy, int head, int track, int sector_index, int *cylinder, int *side, int *sector, UINT32 *sector_length, unsigned long *flags)
893{
894   struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy);
895
896   if (use_80_track_drives && tag->tracks<=40)
897   {
898      track = track / 2;
899   }
900
901   if (sector_length)
902      *sector_length=SECTOR_SIZE;
903   if (cylinder)
904      *cylinder = track;
905   if (side)
906      *side = head;
907   if (sector)
908      *sector = sector_index;
909   if (flags)
910      *flags = 0;
911   if (sector_index > tag->sectors)
912   {
913      return FLOPPY_ERROR_SEEKERROR;
914   }
915
916   return FLOPPY_ERROR_SUCCESS;
917}
918
919/*
920    Try to identify the image as an SDF format image.
921*/
922static FLOPPY_IDENTIFY(ti99_sdf_identify)
923{
924   UINT64 size;
925   size = floppy_image_size(floppy);
926   *vote = ti99_sdf_guess_geometry(floppy, size, NULL);
927   logerror("SDF voting %d\n", *vote);
928   return FLOPPY_ERROR_SUCCESS;
929}
930
931/*
932    Create the SDF data structures and set the callbacks.
933*/
934static FLOPPY_CONSTRUCT(ti99_sdf_construct)
935{
936   struct ti99dsk_geometry geometry;
937   struct FloppyCallbacks *callbacks;
938   struct ti99dsk_tag *tag;
939   logerror("Reading image as SDF\n");
940
941   if (params)
942   {
943      /* create */
944      memset(&geometry, 0, sizeof(geometry));
945      geometry.sides            = option_resolution_lookup_int(params, PARAM_HEADS);
946      geometry.tracksperside         = option_resolution_lookup_int(params, PARAM_TRACKS);
947      geometry.secspertrack         = option_resolution_lookup_int(params, PARAM_SECTORS);
948
949      /* We don't have headers for geometry */
950      /* check for usage in imgtool - we want to be able to create useful disks */
951   }
952   else
953   {
954      /* load */
955      if (ti99_sdf_guess_geometry(floppy, floppy_image_size(floppy), &geometry)<50)
956         return FLOPPY_ERROR_INVALIDIMAGE;
957   }
958
959   assert(geometry.sides);
960   assert(geometry.tracksperside);
961   assert(geometry.secspertrack);
962
963   tag = (struct ti99dsk_tag *) floppy_create_tag(floppy, sizeof(struct ti99dsk_tag));
964
965   if (!tag)
966      return FLOPPY_ERROR_OUTOFMEMORY;
967
968   tag->heads = geometry.sides;
969   tag->tracks = geometry.tracksperside;
970   tag->sectors = geometry.secspertrack;
971   if (tag->sectors < 10)
972      /* FM mode */
973      tag->track_size = 16 + tag->sectors*334 + 231;
974   else
975      tag->track_size = 40 + tag->sectors*340 + 712;
976
977   /* set up format callbacks */
978   callbacks = floppy_callbacks(floppy);
979   callbacks->read_sector = ti99_sdf_read_sector;
980   callbacks->write_sector = ti99_sdf_write_sector;
981   callbacks->read_indexed_sector = ti99_sdf_read_indexed_sector;
982   callbacks->write_indexed_sector = ti99_sdf_write_indexed_sector;
983   callbacks->read_track = ti99_sdf_read_track;
984   callbacks->write_track = ti99_sdf_write_track;
985/*  callbacks->format_track = ti99_sdf_format_track; */
986   callbacks->get_track_size = ti99_get_track_size;
987   /* post format unset */
988   callbacks->get_heads_per_disk = ti99_get_heads_per_disk;
989   callbacks->get_tracks_per_disk = ti99_get_tracks_per_disk;
990   callbacks->get_sector_length = ti99_get_sector_length;
991   callbacks->get_indexed_sector_info = ti99_sdf_get_indexed_sector_info;
992
993   return FLOPPY_ERROR_SUCCESS;
994}
995
996/* -----------------------------------------------------------------------
997 * Track Dump Format, aka PC99 format
998 * This format includes all track data, including address marks and gaps.
999 * However, CRC is not stored; instead, a f7f7 is stored at that location.
1000 * Two track images exist, one for FM and one for MFM format.
1001 *
1002 * In this implementation, however, we do calculate the CRC.
1003 *
1004 * All tracks have exactly the same size, so offsets are calculated easily.
1005 *
1006 * This format is a precise map of the data contents of the track;
1007 * however, it does not store clock patterns.
1008 *
1009 * The TDF format does not have a special index part, so any sector on it
1010 * must be searched (as done by the disk controller)
1011 *
1012 * Interesting detail: The original TI disk controller has a bug which has
1013 * just recently been discovered (using MESS!): The lead-in has another 9
1014 * bytes as a prefix; this is due to a wrongly set pointer in the format
1015 * routine. Strictly speaking, it produces invalid track images.
1016 * The original hardware does not care, however, so neither should we.
1017 * To keep the emulation realistic (and to allow for a
1018 * possible future extension with clock bits), we search for the
1019 * beginning of the track. From there on, however, we accept no further
1020 * deviations. This may make the resulting format unusable with the PC99
1021 * emulator, but it only appears when formatting with the TI disk
1022 * controller anyway. If the image is created with another disk controller or
1023 * using imgtool, the format is correctly created.
1024 *
1025 * FM track image
1026 *
1027 * Lead-in:           16*  00
1028 * Pre-ID gap:         6*  00    ---+
1029 *       IDAM:             fe       |
1030 *      Track:             tt       |
1031 *       Head:             hh       |
1032 *     Sector:             ss       |
1033 *       Size:             01       |
1034 *        CRC:         2*  f7       +--- repeat 9 times (8 in rare cases)
1035 *       Gap2:        11*  ff       |
1036 *                     6*  00       |
1037 *        DAM:             fb       |
1038 *    Content:       256*  xx       |
1039 *        CRC:         2*  f7       |
1040 *       Gap3:        45*  ff    ---+
1041 * Lead-out:         231*  ff
1042 *
1043 * --------------
1044 *
1045 * MFM track image
1046 *
1047 * Lead-in:           40*  4e
1048 * Pre-ID gap:        10*  00    ---+
1049 *       Sync:         3*  a1       |
1050 *       IDAM:             fe       |
1051 *      Track:             tt       |
1052 *       Head:             hh       |
1053 *     Sector:             ss       |
1054 *       Size:             01       |
1055 *        CRC:         2*  f7       +--- repeat 18 times (16 in rare cases)
1056 *       Gap2:        22*  4e       |
1057 *                    12*  00       |
1058 *       Sync:         3*  a1       |
1059 *        DAM:             fb       |
1060 *    Content:       256*  xx       |
1061 *        CRC:         2*  f7       |
1062 *       Gap3:        24*  4e    ---+
1063 * Lead-out:         712*  4e
1064 *
1065 * ------------------
1066 * The tracks are located on the image as follows (different to SDF!):
1067 *
1068 *   head-track
1069 *      0-0
1070 *      0-1
1071 *      0-2
1072 *      0-3
1073 *      ...
1074 *      0-39
1075 *      1-0
1076 *      1-1
1077 *      1-2
1078 *      ...
1079 *      1-39
1080 * ----------------------------------------------------------------------- */
1081
1082#define TI99_FM 1
1083#define TI99_MFM 2
1084
1085/* For the emulation of the clock pattern, we add a 9th bit. */
1086#define TI99_DAM 0x1fb
1087#define TI99_IDAM 0x1fe
1088
1089#define TI99_FM_DAM_OFFSET 24
1090#define TI99_MFM_DAM_OFFSET 44
1091
1092#define TI99_FM_TRACK_ADD 247
1093#define TI99_MFM_TRACK_ADD 752
1094
1095#define TI99_FM_TOTAL_SECTOR 334
1096#define TI99_MFM_TOTAL_SECTOR 340
1097
1098/*
1099    Determine offset of the first IDAM. This is the base of the simulation
1100    of the clock bytes. We use a heuristic to guess where the track begins
1101    and simply check whether we can locate the first IDAM and DAM. Return
1102    value is the offset to the first IDAM.
1103*/
1104static floperr_t determine_offset(int format, UINT8 *track, int *offset)
1105{
1106   floperr_t retval;
1107   int current_pos;
1108
1109   if (format==TI99_FM)
1110   {
1111//      printf("Trying FM\n");
1112      // We are seeking the lead-in and pre-id gap from
1113      // position 0 to position 50. This is just to make sure that
1114      // if there are bad bytes at the beginning (original TI controller)
1115      // we will skip them.
1116      current_pos = find_block(track, 0, 50, 0x00, 22);
1117      if (current_pos==TI99DSK_BLOCKNOTFOUND)
1118      {
1119         logerror("Lead-in not found\n");
1120         retval = FLOPPY_ERROR_SEEKERROR;
1121      }
1122      else
1123      {
1124         current_pos += 22;
1125         if (track[current_pos]==0xfe /* IDAM */ &&
1126            track[current_pos+4]==0x01 /* sector length, always 256 for TI */ &&
1127            track[current_pos+24]==0xfb) /* DAM */
1128         {
1129            /* We're pretty sure this is the beginning. */
1130            *offset = current_pos;
1131            retval = FLOPPY_ERROR_SUCCESS;
1132         }
1133         else
1134         {
1135            logerror("IDAM or DAM not found\n");
1136            retval = FLOPPY_ERROR_SEEKERROR;
1137         }
1138      }
1139   }
1140   else
1141   {
1142      /* MFM */
1143//      printf("Trying MFM\n");
1144      int track_start = find_block(track, 0, 100, 0x4e, 40);
1145      if (track_start==TI99DSK_BLOCKNOTFOUND)
1146      {
1147         logerror("Lead-in not found\n");
1148         retval = FLOPPY_ERROR_SEEKERROR;
1149      }
1150      else
1151      {
1152         track_start += 40;
1153         current_pos = find_block(track, track_start, track_start+10, 0x00, 10);
1154         if (current_pos==TI99DSK_BLOCKNOTFOUND)
1155         {
1156            logerror("Pre-gap not found\n");
1157            retval = FLOPPY_ERROR_SEEKERROR;
1158         }
1159         else
1160         {
1161            current_pos += 10;
1162            if (track[current_pos] == 0xa1 /* First sync */ &&
1163               track[current_pos+3] == 0xfe /* IDAM */ &&
1164               track[current_pos+44] == 0xa1 /* First sync */ &&
1165               track[current_pos+47] == 0xfb) /* DAM */
1166            {
1167            /* We're pretty sure this is the beginning. */
1168               *offset = current_pos + 3;
1169               retval = FLOPPY_ERROR_SUCCESS;
1170            }
1171            else
1172            {
1173               logerror("Sync/IDAM/DAM not found\n");
1174               retval = FLOPPY_ERROR_SEEKERROR;
1175            }
1176         }
1177      }
1178   }
1179/*  printf("TDF offset = 0x%x\n", *offset); */
1180   return retval;
1181}
1182
1183/*
1184    Gets a byte from the track. In this format we do not have a clock
1185    signal, so we will simulate it. Thus, this function returns a 9-bit
1186    value, with the first bit set as a marker for an address mark.
1187
1188    This may be a little more complicated than necessary, but it correctly
1189    emulates the behavior of the controller (and it allows for an
1190    alternative format which actually supports clock and data).
1191*/
1192static int read_byte(int format, int first_idam, UINT8 *track, int position)
1193{
1194   int totalseclen;
1195   int dam_offset;
1196   int byte;
1197
1198   /* Check whether we are at any IDAM or DAM in this track. */
1199   if (format==TI99_FM)
1200   {
1201      totalseclen = TI99_FM_TOTAL_SECTOR;
1202      dam_offset = TI99_FM_DAM_OFFSET;
1203   }
1204   else
1205   {
1206      totalseclen = TI99_MFM_TOTAL_SECTOR;
1207      dam_offset = TI99_MFM_DAM_OFFSET;
1208   }
1209
1210   int curpos = (position - first_idam) % totalseclen;
1211   if (curpos==0 && track[position]==0xfe)
1212      byte = TI99_IDAM;
1213   else
1214   {
1215      if (curpos==dam_offset && track[position]==0xfb)
1216         byte = TI99_DAM;
1217      else
1218         byte = track[position] & 0xff;
1219   }
1220   return byte;
1221}
1222
1223/*
1224    Determine the position of this track in the image.
1225*/
1226static floperr_t ti99_tdf_get_offset(floppy_image *floppy, int head, int imgtrack, UINT64 *offset)
1227{
1228   struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy);
1229
1230   if ((head < 0) || (head >= tag->heads) || (imgtrack < 0) || (imgtrack >= tag->tracks))
1231      return FLOPPY_ERROR_SEEKERROR;
1232
1233   // For head 0, tracks increase from 0 to tracks-1; then for head 1,
1234   // tracks also increase from 0 to tracks-1. */
1235   *offset = head * (tag->tracks * tag->track_size) +  imgtrack * tag->track_size;
1236
1237   return FLOPPY_ERROR_SUCCESS;
1238}
1239
1240/*
1241    Reads a track. We just copy the contents from the format and recreate
1242    the CRC if it is the "blank" CRC as F7F7.
1243    This method assumes that the track division be done already.
1244*/
1245static floperr_t ti99_tdf_read_track_internal(floppy_image *floppy, int head, int imgtrack, UINT64 offset, void *buffer, size_t buflen)
1246{
1247   floperr_t err;
1248   UINT64 track_offset;
1249   int first_idam = 0;
1250   struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy);
1251   int i, byte, crc;
1252
1253   UINT8 *track_data = (UINT8*)buffer;
1254
1255   err = ti99_tdf_get_offset(floppy, head, imgtrack, &track_offset);
1256   if (err)
1257      return err;
1258
1259   floppy_image_read(floppy, buffer, track_offset, buflen);
1260
1261   // Rebuild the CRCs. PC99 did not store the CRC but put
1262   // F7F7 in its place.
1263   // The first CRC is at position IDAM + 5
1264   // The second CRC is at position IDAM + 0x119 (FM) or +0x12d (MFM)
1265   // All this is repeated for each sector in the track
1266
1267   if (determine_offset(tag->format, track_data, &first_idam)==FLOPPY_ERROR_SEEKERROR)
1268      return FLOPPY_ERROR_SEEKERROR;
1269
1270   i = 0;
1271   while (i < buflen)
1272   {
1273      byte = read_byte(tag->format, first_idam, track_data, i);
1274      if (byte == TI99_IDAM)
1275      {
1276         // Do we have a valid CRCs already? Then this image
1277         // already contains proper CRCs handling. Do not recreate them.
1278         if (track_data[i+5] != 0xf7 || track_data[i+6] != 0xf7)
1279            return FLOPPY_ERROR_SUCCESS;
1280
1281         crc = ccitt_crc16(0xffff, &track_data[i], 5);
1282         track_data[i+5] = (crc>>8) & 0xff;
1283         track_data[i+6] = (crc & 0xff);
1284      }
1285      else
1286      {
1287         if (byte == TI99_DAM)
1288         {
1289            crc = ccitt_crc16(0xffff, &track_data[i], SECTOR_SIZE+1);
1290            track_data[i+SECTOR_SIZE+1] = (crc>>8) & 0xff;
1291            track_data[i+SECTOR_SIZE+2] = (crc & 0xff);
1292         }
1293      }
1294      i++;
1295   }
1296//  dump_contents(track_data, buflen);
1297   return FLOPPY_ERROR_SUCCESS;
1298}
1299
1300/*
1301    Reads a track. We just copy the contents from the format, without
1302    changes.
1303*/
1304static floperr_t ti99_tdf_read_track(floppy_image *floppy, int head, int track, UINT64 offset, void *buffer, size_t buflen)
1305{
1306   int imgtrack = track;
1307
1308   struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy);
1309   if (use_80_track_drives && tag->tracks<=40)
1310   {
1311      imgtrack = track / 2;
1312   }
1313   return ti99_tdf_read_track_internal(floppy, head, imgtrack, offset, buffer, buflen);
1314}
1315
1316/*
1317    Writes a track.
1318*/
1319static floperr_t ti99_tdf_write_track(floppy_image *floppy, int head, int track, UINT64 offset, const void *buffer, size_t buflen)
1320{
1321   floperr_t err;
1322   UINT64 track_offset;
1323   int imgtrack = track;
1324
1325   struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy);
1326   if (use_80_track_drives && tag->tracks<=40)
1327   {
1328      imgtrack = track / 2;
1329   }
1330
1331   err = ti99_tdf_get_offset(floppy, head, imgtrack, &track_offset);
1332   if (err)
1333      return err;
1334
1335   // According to the TDF format, we don't need the CRC but replace it
1336   // with F7F7. For now we keep it and see whether PC99 can cope with
1337   // that. (Otherwise we would have to copy the const buffer). */
1338   floppy_image_write(floppy, buffer, offset + track_offset, buflen);
1339   return FLOPPY_ERROR_SUCCESS;
1340}
1341
1342/*
1343static floperr_t ti99_tdf_format_track(floppy_image *floppy, int head, int track, option_resolution *params)
1344{
1345    int sectors;
1346    int sector_length;
1347    int interleave;
1348    int first_sector_id;
1349    sectors         = option_resolution_lookup_int(params, PARAM_SECTORS);
1350    sector_length   = option_resolution_lookup_int(params, PARAM_SECTOR_LENGTH);
1351    interleave      = option_resolution_lookup_int(params, PARAM_INTERLEAVE);
1352    first_sector_id = option_resolution_lookup_int(params, PARAM_FIRST_SECTOR_ID);
1353    return FLOPPY_ERROR_SUCCESS;
1354}
1355*/
1356
1357/*
1358    Get a sector from a track. This function takes a complete track,
1359    searches for the sector, and passes a pointer to the sector contents
1360    back.
1361    As the original TI controller creates bad bytes at the start of a track
1362    we do *not* assume a simple layout. The strategy is to search the
1363    lead-in of the track, and from this offset, step through the track
1364    in search for the sector. Since we do not have index marks (we cannot
1365    distinguish them from ordinary data, as we do not store the clock), we
1366    do not attempt to detect IDAM or DAM.
1367*/
1368static floperr_t ti99_tdf_seek_sector_in_track(floppy_image *floppy, int head, int track, int sector, UINT8 *track_data, UINT8 **sector_data)
1369{
1370   struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy);
1371
1372   int byte;
1373   int state;
1374   enum { SEARCHIDAM, SEARCHDAM };
1375
1376   state = SEARCHIDAM;
1377   if (tag->first_idam==0) /* should we check for every track? */
1378   {
1379      if (determine_offset(tag->format, track_data, &tag->first_idam)==FLOPPY_ERROR_SEEKERROR)
1380      {
1381         logerror("could not determine offset\n");
1382         return FLOPPY_ERROR_SEEKERROR;
1383      }
1384   }
1385
1386   for (int i=0; i < tag->track_size; i++)
1387   {
1388      byte = read_byte(tag->format, tag->first_idam, track_data, i);
1389
1390      switch (state)
1391      {
1392      case SEARCHIDAM:
1393         /* search for the IDAM */
1394         if (byte==TI99_IDAM)
1395         {
1396            if ((track_data[i+1]==track) && (track_data[i+2]==head) && (track_data[i+3]==sector))
1397               state = SEARCHDAM;
1398            else
1399               state = SEARCHIDAM;
1400         }
1401         break;
1402      case SEARCHDAM:
1403         if (byte==TI99_DAM)
1404         {
1405            *sector_data = &track_data[i+1];
1406            return FLOPPY_ERROR_SUCCESS;
1407         }
1408         break;
1409      }
1410   }
1411   logerror("Sector not found.\n");
1412   return FLOPPY_ERROR_SEEKERROR;
1413}
1414
1415static floperr_t ti99_tdf_read_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen)
1416{
1417   floperr_t err;
1418   UINT8 *sector_data;
1419   UINT8 *track_data;
1420   int imgtrack = track;
1421   struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy);
1422
1423   track_data = (UINT8*)malloc(tag->track_size);
1424
1425   if (use_80_track_drives && tag->tracks<=40)
1426   {
1427      imgtrack = track / 2;
1428   }
1429
1430   err = ti99_tdf_read_track_internal(floppy, head, imgtrack, 0, track_data, tag->track_size);
1431   if (err)
1432      return err;
1433
1434   err = ti99_tdf_seek_sector_in_track(floppy, head, imgtrack, sector, track_data, &sector_data);
1435   if (err)
1436      return err;
1437   /* verify CRC? */
1438
1439   memcpy(buffer, sector_data, SECTOR_SIZE);
1440   free(track_data);
1441   return FLOPPY_ERROR_SUCCESS;
1442}
1443
1444/*
1445    Writes a sector into the track. Note that the indexed_write method
1446    may be used instead of this method, according to the implementation
1447    of flopimg/flopdrv.
1448*/
1449static floperr_t ti99_tdf_write_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam)
1450{
1451   floperr_t err;
1452   UINT8 *sector_data;
1453   UINT8 *track_data;
1454   UINT64 track_offset;
1455   int imgtrack = track;
1456   UINT64 offset;
1457   UINT8 crc_field[2];
1458   int crc;
1459
1460   struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy);
1461
1462   track_data = (UINT8*)malloc(tag->track_size);
1463   if (use_80_track_drives && tag->tracks<=40)
1464   {
1465      imgtrack = track / 2;
1466   }
1467
1468   err = ti99_tdf_read_track_internal(floppy, head, imgtrack, 0, track_data, tag->track_size);
1469   if (err)
1470      return err;
1471   err = ti99_tdf_seek_sector_in_track(floppy, head, imgtrack, sector, track_data, &sector_data);
1472   if (err)
1473      return err;
1474
1475   err = ti99_tdf_get_offset(floppy, head, imgtrack, &track_offset);
1476   if (err)
1477      return err;
1478
1479   offset = track_offset + (sector_data - track_data);
1480
1481   floppy_image_write(floppy, buffer, offset, SECTOR_SIZE);
1482
1483   // Recalculate CRC. We init the CRC with bf84 (DAM fb is
1484   // already included; would be ffff without)
1485   crc = ccitt_crc16(0xbf84, (UINT8*)buffer, SECTOR_SIZE);
1486   crc_field[0] = (crc>>8) & 0xff;
1487   crc_field[1] = (crc & 0xff);
1488   floppy_image_write(floppy, crc_field, offset+SECTOR_SIZE, 2);
1489
1490   return FLOPPY_ERROR_SUCCESS;
1491}
1492
1493static floperr_t ti99_tdf_write_indexed_sector(floppy_image *floppy, int head, int track, int sector_index, const void *buffer, size_t buflen, int ddam)
1494{
1495   /* Read track, head */
1496   int byte;
1497   struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy);
1498   UINT8 *track_data;
1499   UINT8 *sector_data;
1500   UINT64 track_offset;
1501   UINT64 offset;
1502   UINT8 crc_field[2];
1503
1504   int crc;
1505   int imgtrack = track;
1506
1507   int i;
1508   floperr_t err;
1509
1510   track_data = (UINT8*)malloc(tag->track_size);
1511
1512   if (use_80_track_drives && tag->tracks<=40)
1513   {
1514      imgtrack = track / 2;
1515   }
1516
1517   err = ti99_tdf_read_track_internal(floppy, head, imgtrack, 0, track_data, tag->track_size);
1518   if (err)
1519   {
1520      free(track_data);
1521      return err;
1522   }
1523
1524   /* Search for the sector_index-th sector. */
1525   if (tag->first_idam==0) /* should we check for every track? */
1526   {
1527      if (determine_offset(tag->format, track_data, &tag->first_idam)==FLOPPY_ERROR_SEEKERROR)
1528      {
1529         free(track_data);
1530         return FLOPPY_ERROR_SEEKERROR;
1531      }
1532   }
1533
1534   /* Search for the IDAM of the n-th sector */
1535   i=0;
1536   while (i < tag->track_size && sector_index>=0)
1537   {
1538      byte = read_byte(tag->format, tag->first_idam, track_data, i);
1539      if (byte == TI99_IDAM)
1540      {
1541         sector_index--;
1542      }
1543      i++;
1544   }
1545   i--;
1546
1547   /* Not that many sectors in this track? */
1548   if (sector_index>0)
1549   {
1550      free(track_data);
1551      return FLOPPY_ERROR_SEEKERROR;
1552   }
1553
1554   /* Find the DAM. */
1555   while (i < tag->track_size)
1556   {
1557      byte = read_byte(tag->format, tag->first_idam, track_data, i);
1558      if (byte == TI99_DAM)
1559         break;
1560      else
1561         i++;
1562   }
1563
1564   /* There was no DAM. Image seems to be broken. */
1565   if (i == tag->track_size)
1566   {
1567      free(track_data);
1568      return FLOPPY_ERROR_SEEKERROR;
1569   }
1570
1571   /* Sector data is right after the DAM. */
1572   sector_data = &track_data[i+1];
1573
1574   /* Get the position of the track in the image. */
1575   err = ti99_tdf_get_offset(floppy, head, imgtrack, &track_offset);
1576   if (err)
1577      return err;
1578
1579   offset = track_offset + (sector_data - track_data);
1580
1581   /* Write the sector data at that position. */
1582   floppy_image_write(floppy, buffer, offset, SECTOR_SIZE);
1583
1584   // Recalculate CRC. We init the CRC with bf84 (DAM fb is
1585   // already included; would be ffff without)
1586   crc = ccitt_crc16(0xbf84, (UINT8*)buffer, SECTOR_SIZE);
1587   crc_field[0] = (crc>>8) & 0xff;
1588   crc_field[1] = (crc & 0xff);
1589   floppy_image_write(floppy, crc_field, offset+SECTOR_SIZE, 2);
1590
1591   /* Free the temporary buffer */
1592   free(track_data);
1593
1594   return FLOPPY_ERROR_SUCCESS;
1595}
1596
1597
1598static floperr_t ti99_tdf_find_indexed_sector(floppy_image *floppy, int head, int track, int sector_index, int *cylinder, int *side, int *sector, UINT32 *sector_length, void *buffer, unsigned long *flags)
1599{
1600   /* Read track, head */
1601   int byte = 0;
1602   struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy);
1603   UINT8 *track_data;
1604   int imgtrack = track;
1605   int i;
1606   floperr_t err;
1607   floperr_t retval;
1608   track_data = (UINT8*)malloc(tag->track_size);
1609
1610   if (use_80_track_drives && tag->tracks<=40)
1611   {
1612      imgtrack = track / 2;
1613   }
1614
1615   err = ti99_tdf_read_track_internal(floppy, head, imgtrack, 0, track_data, tag->track_size);
1616   if (err)
1617   {
1618      free(track_data);
1619      return err;
1620   }
1621
1622   /* Search for the sector_index-th sector. */
1623
1624   if (tag->first_idam==0) /* should we check for every track? */
1625   {
1626      if (determine_offset(tag->format, track_data, &tag->first_idam)==FLOPPY_ERROR_SEEKERROR)
1627      {
1628         free(track_data);
1629         return FLOPPY_ERROR_SEEKERROR;
1630      }
1631   }
1632
1633   i=0;
1634   sector_index = sector_index+1;
1635   while (i++ < tag->track_size && sector_index>0)
1636   {
1637      byte = read_byte(tag->format, tag->first_idam, track_data, i);
1638      if (byte == TI99_IDAM)
1639      {
1640         sector_index--;
1641      }
1642   }
1643
1644   retval = FLOPPY_ERROR_SEEKERROR;
1645   if (sector_index==0)
1646   {
1647      /* If desired, return the ID field */
1648      if (cylinder)
1649      {
1650         *cylinder = track_data[i];
1651         //      printf("cylinder=%d", *cylinder);
1652
1653      }
1654      if (side)
1655      {
1656         *side = track_data[i+1];
1657         //      printf(", side=%d", *side);
1658      }
1659      if (sector)
1660      {
1661         *sector = track_data[i+2];
1662         //      printf(", sector=%d", *sector);
1663      }
1664      if (sector_length)
1665      {
1666         *sector_length = 128 << track_data[i+3];
1667         //      printf(", sector_length=%d\n", *sector_length);
1668      }
1669      if (flags)
1670      {
1671         *flags = 0;
1672      }
1673      retval = FLOPPY_ERROR_SUCCESS;
1674
1675      /* If desired, return the sector contents. */
1676      if (buffer)
1677      {
1678         while (i++ < tag->track_size)
1679         {
1680            byte = read_byte(tag->format, tag->first_idam, track_data, i);
1681            if (byte == TI99_DAM)
1682               break;
1683         }
1684         if (byte == TI99_DAM)
1685         {
1686            memcpy(buffer, &track_data[i+1], SECTOR_SIZE);
1687         }
1688         else
1689         {
1690            retval = FLOPPY_ERROR_SEEKERROR;
1691         }
1692      }
1693   }
1694   free(track_data);
1695   return retval;
1696}
1697
1698/*
1699    Required for ReadAddress command. This function gets the 1st, 2nd, 3rd,
1700    sector (and so on) from the track, not sector 1, 2, 3. That is, the
1701    returned sector info depends on the interleave and the start sector.
1702*/
1703static floperr_t ti99_tdf_get_indexed_sector_info(floppy_image *floppy, int head, int track, int sector_index, int *cylinder, int *side, int *sector, UINT32 *sector_length, unsigned long *flags)
1704{
1705   return ti99_tdf_find_indexed_sector(floppy, head, track, sector_index, cylinder, side, sector, sector_length, NULL, flags);
1706}
1707
1708static floperr_t ti99_tdf_read_indexed_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen)
1709{
1710   return ti99_tdf_find_indexed_sector(floppy, head, track, sector, (int*)NULL, (int*)NULL, (int*)NULL, (UINT32*)NULL, buffer, (unsigned long*)NULL);
1711}
1712
1713
1714/*
1715    Guess the geometry of the disk.
1716    1. Check whether the disk is formatted, and if so, whether it is
1717       a FM or a MFM disk
1718    2. If this fails, guess from the file size which format it should have
1719
1720    SDF formats have sizes which are multiples of 10KiB.
1721    TDF: full sector size FM (MFM) = 334 (340) bytes
1722             additional track data FM (MFM) = 247 (752) bytes
1723
1724         => check whether size is dividable by 40 (or 35) and by 2
1725         => get track size
1726         => subtract track data
1727         => divide by 334 or 340
1728         => number of sectors should be 8, 9, 16, 18, 32, or 36
1729
1730     SSDD is 274880, but DSSD is 260240, so they differ (unlike in the SDF format)
1731     Still, we cannot tell apart single-sided 80 track formats from double-sided
1732     40 tracks. We assume the latter. Moreover, we only attempt to detect
1733     9/18/36 sectors (no 8/16) and 40 tracks (no 35).
1734
1735     SSSD40 =  130120
1736     DSSD   =  260240
1737     SSDD   =  274880
1738     DSDD   =  549760
1739     DSHD   = 1039360
1740     DSDD80 = 1099520
1741     DSHD80 = 2078720
1742*/
1743static int ti99_tdf_guess_geometry(floppy_image *floppy, UINT64 size,
1744   struct ti99dsk_geometry *geometry)
1745{
1746   int idamcnt, state, track, head, i, totalseclen = 0, format = 0, byte, tracklength, trackadd = 0;
1747   int first_idam = 0;
1748   UINT8 *track_data;
1749   /*int offset;*/
1750
1751   struct ti99dsk_geometry dummy_geometry;
1752
1753   if (geometry)
1754      memset(geometry, 0, sizeof(*geometry));
1755   else
1756      geometry = &dummy_geometry;
1757
1758   /* Allocate enough bytes to hold the longest supported track. */
1759   track_data = (UINT8*)malloc(13000);
1760   floppy_image_read(floppy, track_data, 0, 13000);
1761
1762   if (determine_offset(TI99_MFM, track_data, &first_idam)==FLOPPY_ERROR_SEEKERROR)
1763   {
1764      /* MFM failed. */
1765      if (determine_offset(TI99_FM, track_data, &first_idam)==FLOPPY_ERROR_SEEKERROR)
1766      {
1767         logerror("IDAM not found. Unformatted disk.\n");
1768
1769         // FM failed as well. Disk is not formatted. We assume
1770         // a format that fits into the space provided by the file.
1771         // We only give a moderate vote in this case, so SDF
1772         // make take it.
1773
1774         free(track_data);
1775         if (size < 130120 || size > 2078720)
1776         {
1777            logerror("Unknown format size: %d\n", (int)size);
1778            return 0;
1779         }
1780         if (size >= 130120 && size < 260240)
1781         {
1782            /* SSSD */
1783            geometry->sides = 1;
1784            geometry->tracksperside = 40;
1785            geometry->secspertrack = 9;
1786            geometry->density = 1;
1787            return 50;
1788         }
1789         if (size >= 260240 && size < 274880)
1790         {
1791            /* DSSD */
1792            geometry->sides = 2;
1793            geometry->tracksperside = 40;
1794            geometry->secspertrack = 9;
1795            geometry->density = 1;
1796            return 50;
1797         }
1798         if (size >= 274880 && size < 549760)
1799         {
1800            /* SSDD */
1801            geometry->sides = 1;
1802            geometry->tracksperside = 40;
1803            geometry->secspertrack = 18;
1804            geometry->density = 2;
1805            return 50;
1806         }
1807         if (size >= 549760 && size < 1039360)
1808         {
1809            /* DSDD */
1810            geometry->sides = 2;
1811            geometry->tracksperside = 40;
1812            geometry->secspertrack = 18;
1813            geometry->density = 2;
1814            return 50;
1815         }
1816         if (size >= 1039360 && size < 1099520)
1817         {
1818            /* DSHD */
1819            geometry->sides = 2;
1820            geometry->tracksperside = 40;
1821            geometry->secspertrack = 36;
1822            geometry->density = 2;
1823            return 50;
1824         }
1825         if (size >= 1099520 && size < 2078720)
1826         {
1827            /* DSDD80 */
1828            geometry->sides = 2;
1829            geometry->tracksperside = 80;
1830            geometry->secspertrack = 18;
1831            geometry->density = 2;
1832            return 50;
1833         }
1834         if (size == 2078720)
1835         {
1836            /* DSHD80 */
1837            geometry->sides = 2;
1838            geometry->tracksperside = 80;
1839            geometry->secspertrack = 36;
1840            geometry->density = 2;
1841            return 50;
1842         }
1843      }
1844      else
1845      {
1846         /* FM format */
1847         totalseclen = TI99_FM_TOTAL_SECTOR;
1848         trackadd = TI99_FM_TRACK_ADD; /* additional track data (gaps etc.) */
1849         geometry->density = 1;
1850         format = TI99_FM;
1851      }
1852   }
1853   else
1854   {
1855      /* MFM format */
1856      totalseclen = TI99_MFM_TOTAL_SECTOR;
1857      trackadd = TI99_MFM_TRACK_ADD;
1858      geometry->density = 2;
1859      format = TI99_MFM;
1860   }
1861
1862   /* Count the number of IDAMs on track 0, head 0 */
1863   idamcnt = 0;
1864   state = 0;
1865   track = 0;
1866   head = 0;
1867   i=0;
1868
1869   while (i++ < 13000 && head == 0 && track == 0)
1870   {
1871      byte = read_byte(format, first_idam, track_data, i);
1872      switch (state)
1873      {
1874      case 0:
1875         if (byte==TI99_IDAM)
1876            state = 1;
1877         break;
1878      case 1:
1879         track = byte;
1880         state = 2;
1881         break;
1882      case 2:
1883         head = byte;
1884         state = 3;
1885         break;
1886      case 3:
1887         idamcnt++;
1888         state = 0;
1889      }
1890   }
1891   logerror("Determined %d sectors\n", idamcnt);
1892
1893   // Now calculate the geometry
1894   // The track size, in theory, may change due to reformatting,
1895   // and it may also vary between tracks, but the TDF does not support
1896   // varying track lengths. So every track has the same length as track 0.
1897   // Also, we don't yet know whether the drive and the disk have the
1898   // same track count. For 80 track drives and 40 track disks, we need
1899   // double-stepping; this will be known as soon as the controller is
1900   // initialized; we need to check that in the access methods.
1901
1902   tracklength = idamcnt * totalseclen + trackadd;
1903   /*offset = 0;*/
1904
1905   // Read the last track. We assume that all tracks are properly
1906   // formatted; otherwise, the above calculations would already fail.
1907   // That is, the last track in the image tells us whether it is for
1908   // head 0 or for head 1.
1909
1910   geometry->sides = 1;
1911   floppy_image_read(floppy, track_data, size-tracklength, tracklength);
1912   if (determine_offset(format, track_data, &first_idam)==FLOPPY_ERROR_SEEKERROR)
1913   {
1914      /* error ... what now? */
1915      logerror("Error when reading last track. Image broken.\n");
1916      free(track_data);
1917      return 50;
1918   }
1919   else
1920   {
1921      if (track_data[first_idam+2]==0)
1922      {
1923         geometry->sides = 1;
1924      }
1925      else
1926      {
1927         if (track_data[first_idam+2]==1)
1928         {
1929            geometry->sides = 2;
1930         }
1931         else
1932         {
1933            logerror("Error: Last track has invalid first IDAM: head = %d.\n", track_data[first_idam+2]);
1934         }
1935
1936      }
1937   }
1938
1939
1940   geometry->tracksperside = (size / tracklength) / geometry->sides;
1941   geometry->secspertrack = idamcnt;
1942
1943   if (geometry->tracksperside < 35 || geometry->tracksperside > 80)
1944   {
1945      logerror("Unsupported track count: %d\n", geometry->tracksperside);
1946      free(track_data);
1947      return 0;
1948   }
1949
1950   free(track_data);
1951   return 100;
1952}
1953
1954static FLOPPY_IDENTIFY(ti99_tdf_identify)
1955{
1956   UINT64 size;
1957   size = floppy_image_size(floppy);
1958   *vote = ti99_tdf_guess_geometry(floppy, size, NULL);
1959   logerror("TDF voting %d\n", *vote);
1960   return FLOPPY_ERROR_SUCCESS;
1961}
1962
1963static FLOPPY_CONSTRUCT(ti99_tdf_construct)
1964{
1965   struct ti99dsk_geometry geometry;
1966   struct FloppyCallbacks *callbacks;
1967   struct ti99dsk_tag *tag;
1968   logerror("Reading image as TDF\n");
1969   if (params)
1970   {
1971      /* create */
1972      memset(&geometry, 0, sizeof(geometry));
1973/*
1974        geometry.sides              = option_resolution_lookup_int(params, PARAM_HEADS);
1975        geometry.tracksperside          = option_resolution_lookup_int(params, PARAM_TRACKS);
1976        geometry.secspertrack           = option_resolution_lookup_int(params, PARAM_SECTORS);
1977*/
1978   }
1979   else
1980   {
1981      /* load */
1982      if (ti99_tdf_guess_geometry(floppy, floppy_image_size(floppy), &geometry)<50)
1983         return FLOPPY_ERROR_INVALIDIMAGE;
1984   }
1985
1986   assert(geometry.sides);
1987   assert(geometry.tracksperside);
1988   assert(geometry.secspertrack);
1989
1990   tag = (struct ti99dsk_tag *) floppy_create_tag(floppy, sizeof(struct ti99dsk_tag));
1991
1992   tag->heads = geometry.sides;
1993   tag->tracks = geometry.tracksperside;
1994
1995   if (geometry.density==TI99_FM)
1996      tag->track_size = geometry.secspertrack * TI99_FM_TOTAL_SECTOR + TI99_FM_TRACK_ADD;
1997   else
1998      tag->track_size = geometry.secspertrack * TI99_MFM_TOTAL_SECTOR + TI99_MFM_TRACK_ADD;
1999
2000   tag->format = geometry.density;
2001   tag->first_idam = 0; /* let's find out later */
2002   tag->dam_offset = 0;
2003
2004   /* set up format callbacks */
2005   callbacks = floppy_callbacks(floppy);
2006   callbacks->read_sector = ti99_tdf_read_sector;
2007   callbacks->write_sector = ti99_tdf_write_sector;
2008   callbacks->read_indexed_sector = ti99_tdf_read_indexed_sector;
2009   callbacks->write_indexed_sector = ti99_tdf_write_indexed_sector;
2010   callbacks->read_track = ti99_tdf_read_track;
2011   callbacks->write_track = ti99_tdf_write_track;
2012/*  callbacks->format_track = ti99_tdf_format_track; */
2013   callbacks->get_track_size = ti99_get_track_size;
2014   /* post format unset */
2015   callbacks->get_heads_per_disk = ti99_get_heads_per_disk;
2016   callbacks->get_tracks_per_disk = ti99_get_tracks_per_disk;
2017   callbacks->get_sector_length = ti99_get_sector_length;
2018   callbacks->get_indexed_sector_info = ti99_tdf_get_indexed_sector_info;
2019
2020   return FLOPPY_ERROR_SUCCESS;
2021}
2022
2023/* ----------------------------------------------------------------------- */
2024
2025FLOPPY_OPTIONS_START( ti99 )
2026   FLOPPY_OPTION( ti99_sdf, "dsk",         "TI99 sector dump (v9t9)",   ti99_sdf_identify,   ti99_sdf_construct, NULL,
2027      HEADS([1]-2)
2028      TRACKS(35-[40]-80)
2029      SECTORS(8/9/16/[18]/36)
2030      SECTOR_LENGTH([256])
2031      FIRST_SECTOR_ID(0))
2032   FLOPPY_OPTION( ti99_tdf, "dsk,dtk",         "TI99 track dump (pc99)",   ti99_tdf_identify,   ti99_tdf_construct, NULL,
2033      TRACKS(35-[40]-80)
2034      SECTORS(8/9/16/[18]/36)
2035      SECTOR_LENGTH([256])
2036      FIRST_SECTOR_ID(0))
2037FLOPPY_OPTIONS_END
src/lib/formats/p6001_cas.h
r0r11458
1/*
2 * Nec PC-6001 cassette format handling
3 */
4
5#ifndef PC6001_CAS_H_
6#define PC6001_CAS_H_
7
8#include "cassimg.h"
9
10CASSETTE_FORMATLIST_EXTERN( pc6001_cassette_formats );
11
12#endif /*PC6001_CAS_H_*/
src/lib/formats/wavfile.h
r0r11458
1/*********************************************************************
2
3    wavfile.h
4
5    Format code for wave (*.wav) files
6
7*********************************************************************/
8
9#ifndef WAVFILE_H
10#define WAVFILE_H
11
12#include "cassimg.h"
13
14extern const struct CassetteFormat wavfile_format;
15
16#endif /* WAVFILE_H */
src/lib/formats/fdi_dsk.c
r0r11458
1/*********************************************************************
2
3    formats/fdi_dsk.h
4
5    Floppy format code for Formatted Disk Image (FDI) v2.0 images
6
7*********************************************************************/
8
9/*
10
11    TODO:
12
13    - everything
14
15*/
16
17#include "imageutl.h"
18#include "flopimg.h"
19
20/***************************************************************************
21    PARAMETERS
22***************************************************************************/
23
24#define LOG 1
25
26#define MAX_TRACKS   180
27
28#define FLAGS_WRITE_PROTECTED   0x01
29#define FLAGS_INDEX_SYNC      0x02
30
31/* media type */
32enum
33{
34   TYPE_8_INCH = 0,
35   TYPE_5_25_INCH,
36   TYPE_3_5_INCH,
37   TYPE_3_INCH
38};
39
40static const char *const MEDIA_TYPE[] = { "8", "5.25", "3.5", "3" };
41
42/* tracks per inch */
43enum
44{
45   TPI_48 = 0,
46   TPI_67,
47   TPI_96,
48   TPI_100,
49   TPI_135,
50   TPI_192
51};
52
53static const int TRACKS_PER_INCH[] = { 48, 67, 96, 100, 135, 192 };
54
55/* high level track types */
56enum
57{
58   TRACK_BLANK = 0,   /* not supported */
59   TRACK_AMIGA_DD,      /* not supported */
60   TRACK_AMIGA_HD,      /* not supported */
61   TRACK_ST_DD_9,      /* not supported */
62   TRACK_ST_DD_10,      /* not supported */
63   TRACK_PC_DD_8,      /* not supported */
64   TRACK_PC_DD_9,      /* not supported */
65   TRACK_PC_HD_15,      /* not supported */
66   TRACK_IBM_HD_18,   /* not supported */
67   TRACK_IBM_ED_36,   /* not supported */
68   TRACK_CBM_1541,      /* not supported */
69   TRACK_APPLE_DOS_32,   /* not supported */
70   TRACK_APPLE_DOS_33,   /* not supported */
71   TRACK_APPLE_GCR,   /* not supported */
72   TRACK_IBM_SD_10      /* not supported */
73};
74
75//static const int SECTORS_PER_TRACK[] = { 0, 11, 22, 9, 10, 8, 9, 15, 18, 36, -1, -1, -1, -1, 10 };
76
77/***************************************************************************
78    TYPE DEFINITIONS
79***************************************************************************/
80
81struct fdidsk_tag
82{
83   int version;
84   int heads;                        /* number of physical heads */
85   int tracks;                        /* number of physical tracks */
86   UINT32 track_offset[MAX_TRACKS];      /* offset within data for each track */
87   UINT8 track_type[MAX_TRACKS];
88};
89
90struct fdi_track
91{
92   UINT8 type;
93   UINT8 size;
94};
95
96struct fdi_header
97{
98   UINT8 signature[27];
99   UINT8 creator[32];
100   UINT8 comment[80];
101   UINT8 eof;
102   UINT8 version[2];
103   UINT8 ltrack[2];
104   UINT8 lhead;
105   UINT8 type;
106   UINT8 rotspeed;
107   UINT8 flags;
108   UINT8 tpi;
109   UINT8 headwidth;
110   UINT8 reserved[2];
111   struct fdi_track track[MAX_TRACKS];
112};
113
114struct fdi_pulse_track_header
115{
116   UINT8 numpulses[4];
117   UINT8 averagesz[3];
118   UINT8 minsize[3];
119   UINT8 maxsize[3];
120   UINT8 indexsize[3];
121//  UINT8 averagedt[averagesz];
122//  UINT8 mindata[minsize];
123//  UINT8 maxdata[maxsize];
124//  UINT8 indexdata[indexsize];
125};
126
127/***************************************************************************
128    INLINE FUNCTIONS
129***************************************************************************/
130
131INLINE struct fdidsk_tag *get_tag(floppy_image *floppy)
132{
133   return (fdidsk_tag *)floppy_tag(floppy);
134}
135
136/***************************************************************************
137    IMPLEMENTATION
138***************************************************************************/
139
140static int fdi_get_heads_per_disk(floppy_image *floppy)
141{
142   return get_tag(floppy)->heads;
143}
144
145static int fdi_get_tracks_per_disk(floppy_image *floppy)
146{
147   return get_tag(floppy)->tracks;
148}
149
150static int fdi_get_sectors_per_track(floppy_image *floppy, int head, int track)
151{
152   return 0;
153}
154
155static floperr_t fdi_get_sector_length(floppy_image *floppy, int head, int track, int sector, UINT32 *sector_length)
156{
157   return FLOPPY_ERROR_SUCCESS;
158}
159/*
160static UINT32 fdi_get_sector_offset(floppy_image* floppy, int head, int track, int sector)
161{
162    return 0;
163}
164*/
165static floperr_t fdi_get_indexed_sector_info(floppy_image *floppy, int head, int track, int sector_index, int *cylinder, int *side, int *sector, UINT32 *sector_length, unsigned long *flags)
166{
167   return FLOPPY_ERROR_UNSUPPORTED;
168}
169
170static floperr_t fdi_read_track(floppy_image *floppy, int head, int track, UINT64 offset, void *buffer, size_t buflen)
171{
172   return FLOPPY_ERROR_UNSUPPORTED;
173}
174
175static floperr_t fdi_read_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen)
176{
177   return FLOPPY_ERROR_UNSUPPORTED;
178}
179
180static floperr_t fdi_read_indexed_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buffer_len)
181{
182   return FLOPPY_ERROR_UNSUPPORTED;
183}
184
185static floperr_t fdi_write_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam)
186{
187   return FLOPPY_ERROR_UNSUPPORTED;
188}
189
190static floperr_t fdi_write_indexed_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam)
191{
192   return FLOPPY_ERROR_UNSUPPORTED;
193}
194
195/*-------------------------------------------------
196    FLOPPY_IDENTIFY( fdi_dsk_identify )
197-------------------------------------------------*/
198
199FLOPPY_IDENTIFY( fdi_dsk_identify )
200{
201   UINT8 header[25];
202
203   floppy_image_read(floppy, header, 0, 25);
204
205   if (!strncmp((const char *)header, "Formatted Disk Image file", 25))
206   {
207      *vote = 100;
208   }
209   else
210   {
211      *vote = 0;
212   }
213
214   return FLOPPY_ERROR_SUCCESS;
215}
216
217/*-------------------------------------------------
218    FLOPPY_CONSTRUCT( fdi_dsk_construct )
219-------------------------------------------------*/
220
221FLOPPY_CONSTRUCT( fdi_dsk_construct )
222{
223   if (params)
224   {
225      /* create not supported */
226      return FLOPPY_ERROR_UNSUPPORTED;
227   }
228
229   struct fdidsk_tag *tag = (struct fdidsk_tag *) floppy_create_tag(floppy, sizeof(struct fdidsk_tag));
230
231   if (!tag) return FLOPPY_ERROR_OUTOFMEMORY;
232
233   /* read header */
234   struct fdi_header header;
235   floppy_image_read(floppy, &header, 0, sizeof(header));
236
237   tag->version = header.version[0];
238   tag->tracks = pick_integer_be(header.ltrack, 0, 2) + 1;
239   tag->heads = header.lhead + 1;
240
241   if (LOG)
242   {
243      logerror("FDI creator: %s\n", header.creator);
244      logerror("FDI comment: %s\n", header.comment);
245      logerror("FDI version: %u\n", tag->version);
246      logerror("FDI tracks: %u\n", tag->tracks);
247      logerror("FDI heads: %u\n", tag->heads);
248      logerror("FDI media type: %s\"\n", MEDIA_TYPE[header.type]);
249      logerror("FDI rotation speed: %u\n", header.rotspeed + 128);
250      if (header.flags & FLAGS_WRITE_PROTECTED) logerror("FDI is write protected\n");
251      if (header.flags & FLAGS_INDEX_SYNC) logerror("FDI is index synchronized\n");
252      logerror("FDI media TPI: %u\n", TRACKS_PER_INCH[header.tpi]);
253      logerror("FDI head TPI: %u\n", TRACKS_PER_INCH[header.headwidth]);
254   }
255
256   /* find track offsets */
257   int offset = sizeof(header);
258
259   for (int track = 0; track < tag->tracks; track++)
260   {
261      UINT8 type = header.track[track].type;
262      int size = header.track[track].size * 256;
263
264      if (LOG) logerror("FDI track %u type %02x size %u offset %u\n", track, type, size, offset);
265
266      tag->track_offset[track] = offset;
267      tag->track_type[track] = type;
268
269      offset += size;
270   }
271
272   /* set callbacks */
273   struct FloppyCallbacks *callbacks = floppy_callbacks(floppy);
274
275   callbacks->read_track = fdi_read_track;
276   callbacks->get_heads_per_disk = fdi_get_heads_per_disk;
277   callbacks->get_tracks_per_disk = fdi_get_tracks_per_disk;
278   callbacks->get_sector_length = fdi_get_sector_length;
279   callbacks->read_sector = fdi_read_sector;
280   callbacks->read_indexed_sector = fdi_read_indexed_sector;
281//  callbacks->write_track = fdi_write_track;
282   callbacks->write_sector = fdi_write_sector;
283   callbacks->write_indexed_sector = fdi_write_indexed_sector;
284   callbacks->get_indexed_sector_info = fdi_get_indexed_sector_info;
285   callbacks->get_sectors_per_track = fdi_get_sectors_per_track;
286
287   return FLOPPY_ERROR_SUCCESS;
288}
src/lib/formats/ti99_dsk.h
r0r11458
1/*********************************************************************
2
3    formats/ti99_dsk.c
4
5    TI99 and Geneve disk images
6
7    Header file to be included by drivers which use these floppy options
8*********************************************************************/
9
10#ifndef TI99_DSK_H
11#define TI99_DSK_H
12
13#include "flopimg.h"
14
15FLOPPY_OPTIONS_EXTERN(ti99);
16
17void ti99_set_80_track_drives(int use80);
18
19#endif /* TI99_DSK_H */
src/lib/formats/mz_cas.c
r0r11458
1#include <string.h>
2#include "mz_cas.h"
3
4#ifndef VERBOSE
5#define VERBOSE 0
6#endif
7
8//#define LOG(N,M,A)
9//  if(VERBOSE>=N){ if( M )logerror("%11.6f: %-24s",machine.time().as_double(), (const char*)M ); logerror A; }
10
11#define LOG(N,M,A)   \
12   if(VERBOSE>=N){ if( M )printf("%-24s",(const char*)M ); printf A; }
13
14#define LO  -32768
15#define HI   +32767
16
17#define SHORT_PULSE 2
18#define LONG_PULSE   4
19
20#define BYTE_SAMPLES (LONG_PULSE+8*LONG_PULSE)
21
22#define SILENCE    8000
23
24/* long gap and tape mark */
25#define LGAP        22000
26#define LTM_1      40
27#define LTM_0      40
28#define LTM_L      1
29
30/* short gap and tape mark */
31#define SGAP        11000
32#define STM_1      20
33#define STM_0      20
34#define STM_L      1
35
36static int fill_wave_1(INT16 *buffer, int offs)
37{
38   buffer[offs++] = HI;
39   buffer[offs++] = HI;
40   buffer[offs++] = LO;
41    buffer[offs++] = LO;
42    return LONG_PULSE;
43}
44
45static int fill_wave_0(INT16 *buffer, int offs)
46{
47   buffer[offs++] = HI;
48   buffer[offs++] = LO;
49   return SHORT_PULSE;
50}
51
52static int fill_wave_b(INT16 *buffer, int offs, int byte)
53{
54    int i, count = 0;
55
56   /* data bits are preceded by a long pulse */
57    count += fill_wave_1(buffer, offs + count);
58
59    for( i = 7; i >= 0; i-- )
60   {
61      if( (byte >> i) & 1 )
62         count += fill_wave_1(buffer, offs + count);
63      else
64         count += fill_wave_0(buffer, offs + count);
65   }
66   return count;
67}
68
69static int fill_wave(INT16 *buffer, int length, UINT8 *code)
70{
71   static INT16 *beg;
72    static UINT16 csum = 0;
73   static int header = 1, bytecount = 0;
74   int count = 0;
75
76    if( code == CODE_HEADER )
77    {
78      int i;
79
80      /* is there insufficient space for the LGAP? */
81      if( count + LGAP * SHORT_PULSE > length )
82            return -1;
83      LOG(1,"mz700 fill_wave",("LGAP %d samples\n", LGAP * SHORT_PULSE));
84        /* fill long gap */
85      for( i = 0; i < LGAP; i++ )
86         count += fill_wave_0(buffer, count);
87
88        /* make a long tape mark */
89      /* is there insufficient space for the LTM 1? */
90      if( count + LTM_1 * LONG_PULSE > length )
91            return -1;
92      LOG(1,"mz700_fill_wave",("LTM 1 %d samples\n", LTM_1 * LONG_PULSE));
93      for( i = 0; i < LTM_1; i++ )
94         count += fill_wave_1(buffer, count);
95
96      /* is there insufficient space for the LTM 0? */
97      if( count + LTM_0 * SHORT_PULSE > length )
98            return -1;
99      LOG(1,"mz700_fill_wave",("LTM 0 %d samples\n", LTM_0 * SHORT_PULSE));
100      for( i = 0; i < LTM_0; i++ )
101         count += fill_wave_0(buffer, count);
102
103      /* is there insufficient space for the L? */
104      if( count + LTM_L * LONG_PULSE > length )
105            return -1;
106      LOG(1,"mz700_fill_wave",("L %d samples\n", LONG_PULSE));
107        count += fill_wave_1(buffer, count);
108
109      /* reset header, bytecount and checksum */
110        header = 1;
111        bytecount = 0;
112        csum = 0;
113
114      /* HDR begins here */
115        beg = buffer + count;
116
117        return count;
118    }
119
120   if( code == CODE_TRAILER )
121    {
122      int i, file_length;
123      INT16 *end = buffer;
124
125      /* is there insufficient space for the CHKF? */
126      if( count + 2 * BYTE_SAMPLES > length )
127            return -1;
128      LOG(1,"mz700_fill_wave",("CHKF 0x%04X\n", csum));
129      count += fill_wave_b(buffer, count, csum >> 8);
130      count += fill_wave_b(buffer, count, csum & 0xff);
131
132        /* is there insufficient space for the L */
133      if( count + LTM_L * LONG_PULSE > length )
134            return -1;
135      LOG(1,"mz700_fill_wave",("L\n"));
136        count += fill_wave_1(buffer, count);
137
138      /* is there insufficient space for the 256S pulses? */
139      if( count + 256 * SHORT_PULSE > length )
140            return -1;
141      LOG(1,"mz700_fill_wave",("256S\n"));
142        for (i = 0; i < 256; i++)
143         count += fill_wave_0(buffer, count);
144
145      file_length = (int)(end - beg) / sizeof(INT16);
146      /* is there insufficient space for the FILEC ? */
147      if( count + file_length > length )
148            return -1;
149      LOG(1,"mz700_fill_wave",("FILEC %d samples\n", file_length));
150      memcpy(buffer + count, beg, file_length * sizeof(INT16));
151      count += file_length;
152
153      /* is there insufficient space for the CHKF ? */
154      if( count + 2 * BYTE_SAMPLES > length )
155            return -1;
156      LOG(1,"mz700_fill_wave",("CHKF 0x%04X\n", csum));
157      count += fill_wave_b(buffer, count, csum >> 8);
158      count += fill_wave_b(buffer, count, csum & 0xff);
159
160      /* is there insufficient space for the L ? */
161      if( count + STM_L * LONG_PULSE > length )
162            return -1;
163      LOG(1,"mz700_fill_wave",("L %d samples\n", LONG_PULSE));
164        count += fill_wave_1(buffer, count);
165
166      LOG(1,"mz700_fill_wave",("silence %d samples\n", SILENCE));
167        /* silence at the end */
168      for( i = 0; i < SILENCE; i++ )
169         buffer[count++] = 0;
170
171      return count;
172    }
173
174   if( header == 1 && bytecount == 128 )
175   {
176      int i, hdr_length;
177      INT16 *end = buffer;
178
179      /* is there insufficient space for the CHKH ? */
180      if( count + 2 * BYTE_SAMPLES > length )
181            return -1;
182      LOG(1,"mz700_fill_wave",("CHKH 0x%04X\n", csum & 0xffff));
183      count += fill_wave_b(buffer, count, (csum >> 8) & 0xff);
184      count += fill_wave_b(buffer, count, csum & 0xff);
185
186      /* is there insufficient space for the L ? */
187      if( count + LONG_PULSE > length )
188            return -1;
189      LOG(1,"mz700_fill_wave",("L %d samples\n", LONG_PULSE));
190        count += fill_wave_1(buffer, count);
191
192      /* is there insufficient space for the 256S ? */
193      if( count + 256 * SHORT_PULSE > length )
194            return -1;
195      LOG(1,"mz700_fill_wave",("256S\n"));
196        for (i = 0; i < 256; i++)
197         count += fill_wave_0(buffer, count);
198
199      hdr_length = (int)(end - beg) / sizeof(INT16);
200      /* is there insufficient space for the HDRC ? */
201      if( count + hdr_length > length )
202            return -1;
203      LOG(1,"mz700_fill_wave",("HDRC %d samples\n", hdr_length));
204      memcpy(buffer + count, beg, hdr_length * sizeof(INT16));
205      count += hdr_length;
206
207      /* is there insufficient space for CHKH ? */
208      if( count + 2 * BYTE_SAMPLES > length )
209            return -1;
210      LOG(1,"mz700_fill_wave",("CHKH 0x%04X\n", csum & 0xffff));
211      count += fill_wave_b(buffer, count, (csum >> 8) & 0xff);
212      count += fill_wave_b(buffer, count, csum & 0xff);
213
214      /* is there insufficient space for the L ? */
215      if( count + LONG_PULSE > length )
216            return -1;
217      LOG(1,"mz700_fill_wave",("L %d samples\n", LONG_PULSE));
218        count += fill_wave_1(buffer, count);
219
220      /* is there sufficient space for the SILENCE? */
221      if( count + SILENCE > length )
222            return -1;
223      LOG(1,"mz700_fill_wave",("SILENCE %d samples\n", SILENCE));
224      /* fill silence */
225      for( i = 0; i < SILENCE; i++ )
226         buffer[count++] = 0;
227
228        /* is there sufficient space for the SGAP? */
229      if( count + SGAP * SHORT_PULSE > length )
230            return -1;
231      LOG(1,"mz700_fill_wave",("SGAP %d samples\n", SGAP * SHORT_PULSE));
232      /* fill short gap */
233      for( i = 0; i < SGAP; i++ )
234         count += fill_wave_0(buffer, count);
235
236        /* make a short tape mark */
237
238        /* is there sufficient space for the STM 1? */
239      if( count + STM_1 * LONG_PULSE > length )
240            return -1;
241      LOG(1,"mz700_fill_wave",("STM 1 %d samples\n", STM_1 * LONG_PULSE));
242      for( i = 0; i < STM_1; i++ )
243         count += fill_wave_1(buffer, count);
244
245      /* is there sufficient space for the STM 0? */
246      if( count + STM_0 * SHORT_PULSE > length )
247            return -1;
248      LOG(1,"mz700_fill_wave",("STM 0 %d samples\n", STM_0 * SHORT_PULSE));
249      for( i = 0; i < STM_0; i++ )
250         count += fill_wave_0(buffer, count);
251
252      /* is there sufficient space for the L? */
253      if( count + STM_L * LONG_PULSE > length )
254            return -1;
255      LOG(1,"mz700_fill_wave",("L %d samples\n", LONG_PULSE));
256        count += fill_wave_1(buffer, count);
257
258      bytecount = 0;
259      header = 0;
260      csum = 0;
261
262      /* FILE begins here */
263        beg = buffer + count;
264   }
265
266   if( length < BYTE_SAMPLES )
267        return -1;
268
269    if (*code & 0x01) csum++;
270    if (*code & 0x02) csum++;
271    if (*code & 0x04) csum++;
272    if (*code & 0x08) csum++;
273    if (*code & 0x10) csum++;
274    if (*code & 0x20) csum++;
275    if (*code & 0x40) csum++;
276    if (*code & 0x80) csum++;
277
278    bytecount++;
279
280   count += fill_wave_b(buffer, count, *code);
281
282    return count;
283}
284
285#define MZ700_WAVESAMPLES_HEADER   (   \
286      LGAP * SHORT_PULSE +         \
287      LTM_1 * LONG_PULSE +         \
288      LTM_0 * SHORT_PULSE +         \
289      LTM_L * LONG_PULSE +         \
290      2 * 2 * BYTE_SAMPLES +         \
291   SILENCE +                     \
292   SGAP * SHORT_PULSE +            \
293      STM_1 * LONG_PULSE +         \
294      STM_0 * SHORT_PULSE +         \
295      STM_L * LONG_PULSE +         \
296      2 * 2 * BYTE_SAMPLES)
297
298
299
300
301static const struct CassetteLegacyWaveFiller mz700_legacy_fill_wave =
302{
303   fill_wave,               /* fill_wave */
304   1,                     /* chunk_size */
305   2 * BYTE_SAMPLES,         /* chunk_samples */
306   NULL,                  /* chunk_sample_calc */
307   5120,                  /* sample_frequency */
308   MZ700_WAVESAMPLES_HEADER,   /* header_samples */
309   1                     /* trailer_samples */
310};
311
312
313
314static casserr_t mz700_cas_identify(cassette_image *cassette, struct CassetteOptions *opts)
315{
316   return cassette_legacy_identify(cassette, opts, &mz700_legacy_fill_wave);
317}
318
319
320
321static casserr_t mz700_cas_load(cassette_image *cassette)
322{
323   return cassette_legacy_construct(cassette, &mz700_legacy_fill_wave);
324}
325
326
327
328static const struct CassetteFormat mz700_cas_format =
329{
330   "m12,mzf",
331   mz700_cas_identify,
332   mz700_cas_load,
333   NULL
334};
335
336
337
338CASSETTE_FORMATLIST_START(mz700_cassette_formats)
339   CASSETTE_FORMAT(mz700_cas_format)
340CASSETTE_FORMATLIST_END
src/lib/formats/gtp_cas.c
r0r11458
1/*
2
3    Tape support for Glaksija GTP format
4
5    Miodrag Milanovic
6*/
7#include "gtp_cas.h"
8
9
10#define GTP_WAV_FREQUENCY   44100
11#define WAVE_LOW      -0x5a9e
12#define WAVE_HIGH      0x5a9e
13#define WAVE_NULL      0
14
15#define   GTP_BLOCK_STANDARD   0x00
16#define   GTP_BLOCK_TURBO      0x01
17#define   GTP_BLOCK_NAME      0x10
18
19static INT16   wave_data;
20static INT16   len;
21
22#define PULSE_WIDTH      30
23#define PERIOD_BASE      150
24#define PERIOD_1      75
25#define PERIOD_0      150
26
27#define INTERBYTE_PAUSE      225
28#define INTERBLOCK_PAUSE   100000
29
30
31static void gtp_output_wave( INT16 **buffer, int length ) {
32   if ( buffer == NULL ) {
33      return;
34   }
35
36   for( ; length > 0; length-- ) {
37      **buffer = wave_data;
38      *buffer = *buffer + 1;
39   }
40}
41
42
43
44static int gtp_mod_1( INT16 **buffer )
45{
46   wave_data = WAVE_LOW;
47   gtp_output_wave(buffer,PULSE_WIDTH);
48   wave_data = WAVE_HIGH;
49   gtp_output_wave(buffer,PULSE_WIDTH);
50   wave_data = WAVE_NULL;
51   gtp_output_wave(buffer,PERIOD_1 - 2 * PULSE_WIDTH);
52   wave_data = WAVE_LOW;
53   gtp_output_wave(buffer,PULSE_WIDTH);
54   wave_data = WAVE_HIGH;
55   gtp_output_wave(buffer,PULSE_WIDTH);
56   wave_data = WAVE_NULL;
57   gtp_output_wave(buffer,PERIOD_1 - 2 * PULSE_WIDTH);
58
59   return PERIOD_1 * 2;
60}
61
62static int gtp_mod_0( INT16 **buffer )
63{
64   wave_data = WAVE_LOW;
65   gtp_output_wave(buffer,PULSE_WIDTH);
66   wave_data = WAVE_HIGH;
67   gtp_output_wave(buffer,PULSE_WIDTH);
68   wave_data = WAVE_NULL;
69   gtp_output_wave(buffer,PERIOD_0 - 2 * PULSE_WIDTH);
70
71   return PERIOD_0;
72}
73
74static int gtp_byte( INT16 **buffer, UINT8 val )
75{
76   UINT8 b;
77   int j,size = 0;
78   for (j=0;j<8;j++) {
79      b = (val >> j) & 1;
80      if (b==0) {
81         size += gtp_mod_0(buffer);
82      } else {
83         size += gtp_mod_1(buffer);
84      }
85   }
86   return size;
87}
88
89static int gtp_sync( INT16 **buffer )
90{
91   int i;
92   int size = 0;
93
94   for(i=0;i<100;i++) {
95      if (i!=0) {
96         // Interbyte pause
97         wave_data = WAVE_NULL;
98         gtp_output_wave(buffer,INTERBYTE_PAUSE);
99         size += INTERBYTE_PAUSE;
100      }
101      size += gtp_byte(buffer,0);
102   }
103   return size;
104}
105
106static int gtp_cas_to_wav_size( const UINT8 *casdata, int caslen ) {
107   int size,n;
108   size = 0;
109   n = 0;
110   if (casdata == NULL) return -1;
111   while(n<caslen) {
112      int block_type = casdata[n];
113      int block_size = casdata[n+2]*256 + casdata[n+1];
114      n+=5;
115      if (block_type==GTP_BLOCK_STANDARD) {
116         // Interblock pause
117         size += INTERBLOCK_PAUSE;
118         size += 100 * (PERIOD_0 * 8 + INTERBYTE_PAUSE) - INTERBYTE_PAUSE;
119         size += (PERIOD_0 * 8 + INTERBYTE_PAUSE) * block_size;
120      }
121      n += block_size;
122   }
123   len = caslen;
124   return size;
125}
126
127static int gtp_cas_fill_wave( INT16 *buffer, int length, UINT8 *bytes ) {
128   int i,size,n;
129   size = 0;
130   n = 0;
131   if (bytes == NULL) return -1;
132   while(n<len)
133   {
134      int block_type = bytes[n];
135      int block_size = bytes[n+2]*256 + bytes[n+1];
136      n+=5;
137      if (block_type==GTP_BLOCK_STANDARD) {
138
139         // Interblock pause
140         wave_data = WAVE_NULL;
141         gtp_output_wave(&buffer,INTERBLOCK_PAUSE);
142         size += INTERBLOCK_PAUSE;
143         size += gtp_sync(&buffer);
144
145         for (i=0;i<block_size;i++) {
146            // Interbyte pause
147            wave_data = WAVE_NULL;
148            gtp_output_wave(&buffer,INTERBYTE_PAUSE);
149            size += INTERBYTE_PAUSE;
150
151            size += gtp_byte(&buffer,bytes[n]);
152            n++;
153         }
154      } else {
155         n += block_size;
156      }
157   }
158   return size;
159}
160
161
162
163
164static const struct CassetteLegacyWaveFiller gtp_legacy_fill_wave = {
165   gtp_cas_fill_wave,         /* fill_wave */
166   -1,               /* chunk_size */
167   0,               /* chunk_samples */
168   gtp_cas_to_wav_size,         /* chunk_sample_calc */
169   GTP_WAV_FREQUENCY,         /* sample_frequency */
170   0,               /* header_samples */
171   0               /* trailer_samples */
172};
173
174
175
176static casserr_t gtp_cassette_identify( cassette_image *cassette, struct CassetteOptions *opts ) {
177   return cassette_legacy_identify( cassette, opts, &gtp_legacy_fill_wave );
178}
179
180
181
182static casserr_t gtp_cassette_load( cassette_image *cassette ) {
183   return cassette_legacy_construct( cassette, &gtp_legacy_fill_wave );
184}
185
186
187
188static const struct CassetteFormat gtp_cassette_format = {
189   "gtp",
190   gtp_cassette_identify,
191   gtp_cassette_load,
192   NULL
193};
194
195
196CASSETTE_FORMATLIST_START(gtp_cassette_formats)
197   CASSETTE_FORMAT(gtp_cassette_format)
198CASSETTE_FORMATLIST_END
199
200
src/lib/formats/mz_cas.h
r0r11458
1/*********************************************************************
2
3    mz_cas.h
4
5    Format code for MZ700 casette files
6
7*********************************************************************/
8
9#ifndef MZ_CAS_H
10#define MZ_CAS_H
11
12#include "cassimg.h"
13
14CASSETTE_FORMATLIST_EXTERN(mz700_cassette_formats);
15
16#endif /* MZ_CAS_H */
src/lib/formats/ap2_dsk.c
r0r11458
1/*********************************************************************
2
3    ap2_dsk.c
4
5    Apple II disk images
6
7*********************************************************************/
8
9#include <stdlib.h>
10#include <string.h>
11#include <assert.h>
12
13#include "ap2_dsk.h"
14#include "basicdsk.h"
15
16
17#define APPLE2_IMAGE_DO      0
18#define APPLE2_IMAGE_PO      1
19#define APPLE2_IMAGE_NIB   2
20
21
22/* used in for all Apple II images */
23static UINT32 apple2_get_track_size(floppy_image *floppy, int head, int track);
24static int disk_decode_nib(UINT8 *data, const UINT8 *nibble, int *volume, int *track, int *sector);
25static void disk_encode_nib(UINT8 *nibble, const UINT8 *data, int volume, int track, int sector);
26
27/* used in DOS/ProDOS order images */
28static int apple2_do_translate_sector(floppy_image *floppy, int sector);
29static int apple2_po_translate_sector(floppy_image *floppy, int sector);
30static floperr_t apple2_dsk_read_track(floppy_image *floppy, int head, int track, UINT64 offset, void *buffer, size_t buflen);
31static floperr_t apple2_dsk_write_track(floppy_image *floppy, int head, int track, UINT64 offset, const void *buffer, size_t buflen);
32
33/* used in nibble order images */
34static floperr_t apple2_nib_read_track(floppy_image *floppy, int head, int track, UINT64 offset, void *buffer, size_t buflen);
35static floperr_t apple2_nib_write_track(floppy_image *floppy, int head, int track, UINT64 offset, const void *buffer, size_t buflen);
36static floperr_t apple2_nib_read_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen);
37static floperr_t apple2_nib_write_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam);
38static floperr_t apple2_nib_get_sector_length(floppy_image *floppy, int head, int track, int sector, UINT32 *sector_length);
39
40
41static const UINT8 translate6[0x40] =
42{
43   0x96, 0x97, 0x9a, 0x9b, 0x9d, 0x9e, 0x9f, 0xa6,
44   0xa7, 0xab, 0xac, 0xad, 0xae, 0xaf, 0xb2, 0xb3,
45   0xb4, 0xb5, 0xb6, 0xb7, 0xb9, 0xba, 0xbb, 0xbc,
46   0xbd, 0xbe, 0xbf, 0xcb, 0xcd, 0xce, 0xcf, 0xd3,
47   0xd6, 0xd7, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde,
48   0xdf, 0xe5, 0xe6, 0xe7, 0xe9, 0xea, 0xeb, 0xec,
49   0xed, 0xee, 0xef, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6,
50   0xf7, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff
51};
52
53
54
55/* -----------------------------------------------------------------------
56 * Utility code
57 * ----------------------------------------------------------------------- */
58
59static const UINT8 *get_untranslate6_map(void)
60{
61   static UINT8 map[256];
62   static int map_inited = 0;
63   UINT8 i;
64
65   if (!map_inited)
66   {
67      memset(map, 0xff, sizeof(map));
68      for (i = 0; i < ARRAY_LENGTH(translate6); i++)
69         map[translate6[i]] = i;
70      map_inited = 1;
71   }
72   return map;
73}
74
75
76
77/* -----------------------------------------------------------------------
78 * Core constructor
79 * ----------------------------------------------------------------------- */
80
81
82static floperr_t apple2_general_construct(floppy_image *floppy, int floppy_type)
83{
84   floperr_t err;
85   struct basicdsk_geometry geometry;
86   struct FloppyCallbacks *format;
87
88   format = floppy_callbacks(floppy);
89
90   switch(floppy_type) {
91   case APPLE2_IMAGE_DO:
92   case APPLE2_IMAGE_PO:
93      memset(&geometry, 0, sizeof(geometry));
94      geometry.heads = 1;
95      geometry.tracks = APPLE2_TRACK_COUNT;
96      geometry.sectors = APPLE2_SECTOR_COUNT;
97      geometry.sector_length = APPLE2_SECTOR_SIZE;
98      geometry.translate_sector = (floppy_type == APPLE2_IMAGE_DO) ? apple2_do_translate_sector : apple2_po_translate_sector;
99
100      err = basicdsk_construct(floppy, &geometry);
101      if (err)
102         return err;
103
104      format->read_track = apple2_dsk_read_track;
105      format->write_track = apple2_dsk_write_track;
106      break;
107
108   case APPLE2_IMAGE_NIB:
109      format->read_track = apple2_nib_read_track;
110      format->write_track = apple2_nib_write_track;
111      format->read_sector = apple2_nib_read_sector;
112      format->write_sector = apple2_nib_write_sector;
113      format->get_sector_length = apple2_nib_get_sector_length;
114      break;
115
116   default:
117      assert(0);
118      return FLOPPY_ERROR_INTERNAL;
119   }
120
121   format->get_track_size = apple2_get_track_size;
122
123   return FLOPPY_ERROR_SUCCESS;
124}
125
126
127
128/* -----------------------------------------------------------------------
129 * DOS order and ProDOS order code
130 * ----------------------------------------------------------------------- */
131
132static FLOPPY_IDENTIFY(apple2_dsk_identify)
133{
134   UINT64 size;
135   UINT64 expected_size;
136
137   size = floppy_image_size(floppy);
138   expected_size = APPLE2_TRACK_COUNT * APPLE2_SECTOR_COUNT * APPLE2_SECTOR_SIZE;
139
140   if (size == expected_size)
141      *vote = 100;
142   else if (size - expected_size < 8 && size - expected_size > -8)
143      *vote = 90;      /* tolerate images with up to eight fewer/extra bytes (bug #638) */
144   else
145      *vote = 0;
146   return FLOPPY_ERROR_SUCCESS;
147}
148
149
150
151static int apple2_do_translate_sector(floppy_image *floppy, int sector)
152{
153   static const UINT8 skewing[] =
154   {
155      /* DOS order (*.do) */
156      0x00, 0x07, 0x0E, 0x06, 0x0D, 0x05, 0x0C, 0x04,
157      0x0B, 0x03, 0x0A, 0x02, 0x09, 0x01, 0x08, 0x0F
158   };
159   return skewing[sector];
160}
161
162
163
164static int apple2_po_translate_sector(floppy_image *floppy, int sector)
165{
166   static const UINT8 skewing[] =
167   {
168      /* ProDOS order (*.po) */
169      0x00, 0x08, 0x01, 0x09, 0x02, 0x0A, 0x03, 0x0B,
170      0x04, 0x0C, 0x05, 0x0D, 0x06, 0x0E, 0x07, 0x0F
171   };
172   return skewing[sector];
173}
174
175
176
177static FLOPPY_CONSTRUCT(apple2_do_construct)
178{
179   return apple2_general_construct(floppy, APPLE2_IMAGE_DO);
180}
181
182
183
184static FLOPPY_CONSTRUCT(apple2_po_construct)
185{
186   return apple2_general_construct(floppy, APPLE2_IMAGE_PO);
187}
188
189
190
191static floperr_t apple2_dsk_read_track(floppy_image *floppy, int head, int track, UINT64 offset, void *buffer, size_t buflen)
192{
193   UINT8 sector_buffer[APPLE2_SECTOR_SIZE];
194   int sector;
195   UINT8 *nibble;
196
197   if (buflen < APPLE2_NIBBLE_SIZE*APPLE2_SECTOR_COUNT)
198      return FLOPPY_ERROR_INTERNAL;
199   if (offset != 0)
200      return FLOPPY_ERROR_UNSUPPORTED;
201
202   memset(buffer, 0, buflen);
203
204   for (sector = 0; sector < APPLE2_SECTOR_COUNT; sector++)
205   {
206      nibble = (UINT8 *)buffer;
207      nibble += sector * APPLE2_SMALL_NIBBLE_SIZE;
208
209      floppy_read_sector(floppy, head, track, sector, 0, sector_buffer, sizeof(sector_buffer));
210      disk_encode_nib(nibble, sector_buffer, 254, track, sector);
211   }
212   return FLOPPY_ERROR_SUCCESS;
213}
214
215
216
217static floperr_t apple2_dsk_write_track(floppy_image *floppy, int head, int track, UINT64 offset, const void *buffer, size_t buflen)
218{
219   int sector;
220   UINT8 sector_buffer[APPLE2_SECTOR_SIZE];
221   const UINT8 *nibble;
222
223   if (offset != 0)
224      return FLOPPY_ERROR_UNSUPPORTED;
225
226   for (sector = 0; sector < APPLE2_SECTOR_COUNT; sector++)
227   {
228      nibble = (UINT8 *)buffer;
229      nibble += sector * APPLE2_SMALL_NIBBLE_SIZE;
230
231      disk_decode_nib(sector_buffer, nibble, NULL, NULL, NULL);
232      floppy_write_sector(floppy, head, track, sector, 0, sector_buffer, sizeof(sector_buffer), 0);
233   }
234
235   return FLOPPY_ERROR_SUCCESS;
236}
237
238
239
240/* -----------------------------------------------------------------------
241 * Nibble order code
242 * ----------------------------------------------------------------------- */
243
244static FLOPPY_IDENTIFY(apple2_nib_identify)
245{
246   UINT64 size;
247   size = floppy_image_size(floppy);
248   *vote = (size == APPLE2_TRACK_COUNT * APPLE2_SECTOR_COUNT * APPLE2_NIBBLE_SIZE) ? 100 : 0;
249   return FLOPPY_ERROR_SUCCESS;
250}
251
252
253
254static FLOPPY_CONSTRUCT(apple2_nib_construct)
255{
256   return apple2_general_construct(floppy, APPLE2_IMAGE_NIB);
257}
258
259
260
261static floperr_t apple2_nib_read_track(floppy_image *floppy, int head, int track, UINT64 offset, void *buffer, size_t buflen)
262{
263   if ((head != 0) || (track < 0) || (track >= APPLE2_TRACK_COUNT))
264      return FLOPPY_ERROR_SEEKERROR;
265   if (offset != 0)
266      return FLOPPY_ERROR_UNSUPPORTED;
267   floppy_image_read(floppy, buffer, track * APPLE2_NIBBLE_SIZE * APPLE2_SECTOR_COUNT, buflen);
268   return FLOPPY_ERROR_SUCCESS;
269}
270
271
272
273static floperr_t apple2_nib_write_track(floppy_image *floppy, int head, int track, UINT64 offset, const void *buffer, size_t buflen)
274{
275   if ((head != 0) || (track < 0) || (track >= APPLE2_TRACK_COUNT))
276      return FLOPPY_ERROR_SEEKERROR;
277   if (offset != 0)
278      return FLOPPY_ERROR_UNSUPPORTED;
279   floppy_image_write(floppy, buffer, track * APPLE2_NIBBLE_SIZE * APPLE2_SECTOR_COUNT, buflen);
280   return FLOPPY_ERROR_SUCCESS;
281}
282
283
284
285/* -----------------------------------------------------------------------
286 * Track conversion
287 * ----------------------------------------------------------------------- */
288
289static int decode_nibbyte(UINT8 *nibint, const UINT8 *nibdata)
290{
291   if ((nibdata[0] & 0xAA) != 0xAA)
292      return 1;
293   if ((nibdata[1] & 0xAA) != 0xAA)
294      return 1;
295
296   *nibint =  (nibdata[0] & ~0xAA) << 1;
297   *nibint |= (nibdata[1] & ~0xAA) << 0;
298   return 0;
299}
300
301
302
303static int disk_decode_nib(UINT8 *data, const UINT8 *nibble, int *volume, int *track, int *sector)
304{
305   UINT8 read_volume;
306   UINT8 read_track;
307   UINT8 read_sector;
308   UINT8 read_checksum;
309   int i;
310   UINT8 b, xorvalue, newvalue;
311
312   const UINT8 *untranslate6 = get_untranslate6_map();
313
314   /* pick apart the volume/track/sector info and checksum */
315   if (decode_nibbyte(&read_volume, &nibble[10]))
316      return 1;
317   if (decode_nibbyte(&read_track, &nibble[12]))
318      return 1;
319   if (decode_nibbyte(&read_sector, &nibble[14]))
320      return 1;
321   if (decode_nibbyte(&read_checksum, &nibble[16]))
322      return 1;
323   if (read_checksum != (read_volume ^ read_track ^ read_sector))
324      return 1;
325
326   /* decode the nibble core */
327   xorvalue = 0;
328   for (i = 0; i < 342; i++)
329   {
330      b = untranslate6[nibble[i+28]];
331      if (b == 0xff)
332         return 1;
333      newvalue = b ^ xorvalue;
334
335      if (i >= 0x56)
336      {
337         /* 6 bit */
338         data[i - 0x56] |= (newvalue << 2);
339      }
340      else
341      {
342         /* 3 * 2 bit */
343         data[i + 0x00] = ((newvalue >> 1) & 0x01) | ((newvalue << 1) & 0x02);
344         data[i + 0x56] = ((newvalue >> 3) & 0x01) | ((newvalue >> 1) & 0x02);
345         if (i + 0xAC < APPLE2_SECTOR_SIZE)
346            data[i + 0xAC] = ((newvalue >> 5) & 0x01) | ((newvalue >> 3) & 0x02);
347      }
348      xorvalue = newvalue;
349   }
350
351   /* success; write out values if pointers not null */
352   if (volume)
353      *volume = read_volume;
354   if (track)
355      *track = read_track;
356   if (sector)
357      *sector = read_sector;
358   return 0;
359}
360
361
362
363static floperr_t apple2_nib_read_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen)
364{
365   floperr_t err;
366   const UINT8 *nibble;
367   UINT8 *track_data;
368   void *track_data_v;
369
370   if ((sector < 0) || (sector >= APPLE2_SECTOR_COUNT))
371      return FLOPPY_ERROR_SEEKERROR;
372   if (buflen != APPLE2_SECTOR_SIZE)
373      return FLOPPY_ERROR_INTERNAL;
374
375   err = floppy_load_track(floppy, head, track, FALSE, &track_data_v, NULL);
376   if (err)
377      return err;
378   track_data = (UINT8 *) track_data_v;
379
380   nibble = track_data + (sector * APPLE2_NIBBLE_SIZE);
381
382   if (disk_decode_nib((UINT8 *)buffer, nibble, NULL, NULL, NULL))
383      return FLOPPY_ERROR_INVALIDIMAGE;
384
385   return FLOPPY_ERROR_SUCCESS;
386}
387
388
389
390static void disk_encode_nib(UINT8 *nibble, const UINT8 *data, int volume, int track, int sector)
391{
392   int checksum, oldvalue, xorvalue, i;
393
394   /* setup header values */
395   checksum = volume ^ track ^ sector;
396
397   memset(nibble, 0xFF, APPLE2_NIBBLE_SIZE);
398   nibble[ 7]     = 0xD5;
399   nibble[ 8]     = 0xAA;
400   nibble[ 9]     = 0x96;
401   nibble[10]     = (volume >> 1) | 0xAA;
402   nibble[11]     = volume | 0xAA;
403   nibble[12]     = (track >> 1) | 0xAA;
404   nibble[13]     = track | 0xAA;
405   nibble[14]     = (sector >> 1) | 0xAA;
406   nibble[15]     = sector | 0xAA;
407   nibble[16]     = (checksum >> 1) | 0xAA;
408   nibble[17]     = (checksum) | 0xAA;
409   nibble[18]     = 0xDE;
410   nibble[19]     = 0xAA;
411   nibble[20]     = 0xEB;
412   nibble[25]     = 0xD5;
413   nibble[26]     = 0xAA;
414   nibble[27]     = 0xAD;
415   nibble[27+344] = 0xDE;
416   nibble[27+345] = 0xAA;
417   nibble[27+346] = 0xEB;
418   xorvalue = 0;
419
420   for (i = 0; i < 342; i++)
421   {
422      if (i >= 0x56)
423      {
424         /* 6 bit */
425         oldvalue = data[i - 0x56];
426         oldvalue = oldvalue >> 2;
427      }
428      else
429      {
430         /* 3 * 2 bit */
431         oldvalue = 0;
432         oldvalue |= (data[i + 0x00] & 0x01) << 1;
433         oldvalue |= (data[i + 0x00] & 0x02) >> 1;
434         oldvalue |= (data[i + 0x56] & 0x01) << 3;
435         oldvalue |= (data[i + 0x56] & 0x02) << 1;
436         if (i + 0xAC < APPLE2_SECTOR_SIZE)
437         {
438            oldvalue |= (data[i + 0xAC] & 0x01) << 5;
439            oldvalue |= (data[i + 0xAC] & 0x02) << 3;
440         }
441
442      }
443      xorvalue ^= oldvalue;
444      nibble[28+i] = translate6[xorvalue & 0x3F];
445      xorvalue = oldvalue;
446   }
447
448   nibble[27+343] = translate6[xorvalue & 0x3F];
449}
450
451
452
453static floperr_t apple2_nib_write_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam)
454{
455   floperr_t err;
456   UINT8 *track_data;
457   void *track_data_v;
458
459   if ((sector < 0) || (sector >= APPLE2_SECTOR_COUNT))
460      return FLOPPY_ERROR_SEEKERROR;
461   if (buflen != APPLE2_SECTOR_SIZE)
462      return FLOPPY_ERROR_INTERNAL;
463
464   err = floppy_load_track(floppy, head, track, TRUE, &track_data_v, NULL);
465   if (err)
466      return err;
467   track_data = (UINT8 *) track_data_v;
468
469   disk_encode_nib(track_data + sector * APPLE2_NIBBLE_SIZE, (const UINT8 *)buffer, 254, track, sector);
470   return FLOPPY_ERROR_SUCCESS;
471}
472
473
474
475static floperr_t apple2_nib_get_sector_length(floppy_image *floppy, int head, int track, int sector, UINT32 *sector_length)
476{
477   *sector_length = APPLE2_SECTOR_SIZE;
478   return FLOPPY_ERROR_SUCCESS;
479}
480
481
482
483static UINT32 apple2_get_track_size(floppy_image *floppy, int head, int track)
484{
485   return APPLE2_NIBBLE_SIZE * APPLE2_SECTOR_COUNT;
486}
487
488
489
490/* ----------------------------------------------------------------------- */
491
492FLOPPY_OPTIONS_START( apple2 )
493   FLOPPY_OPTION( apple2_do, "do,dsk,bin",   "Apple ][ DOS order disk image",   apple2_dsk_identify,   apple2_do_construct, NULL,
494      HEADS([1])
495      TRACKS([35])
496      SECTORS([16])
497      SECTOR_LENGTH([256])
498      FIRST_SECTOR_ID([0]))
499   FLOPPY_OPTION( apple2_po, "po,dsk,bin",   "Apple ][ ProDOS order disk image",   apple2_dsk_identify,   apple2_po_construct, NULL,
500      HEADS([1])
501      TRACKS([35])
502      SECTORS([16])
503      SECTOR_LENGTH([256])
504      FIRST_SECTOR_ID([0]))
505   FLOPPY_OPTION( apple2_nib, "dsk,nib",   "Apple ][ Nibble order disk image",   apple2_nib_identify,   apple2_nib_construct, NULL,
506      HEADS([1])
507      TRACKS([35])
508      SECTORS([16])
509      SECTOR_LENGTH([256])
510      FIRST_SECTOR_ID([0]))
511FLOPPY_OPTIONS_END
src/lib/formats/gtp_cas.h
r0r11458
1/*
2
3       gtp_cas.h
4
5       Format code for gtp cassette files
6*/
7
8#ifndef GTP_CAS_H
9#define GTP_CAS_H
10
11#include "cassimg.h"
12
13CASSETTE_FORMATLIST_EXTERN(gtp_cassette_formats);
14
15#endif /* GTP_CAS_H */
src/lib/formats/ap2_dsk.h
r0r11458
1/*********************************************************************
2
3    ap2_dsk.h
4
5    Apple II disk images
6
7*********************************************************************/
8
9#ifndef AP2_DISK_H
10#define AP2_DISK_H
11
12#include "flopimg.h"
13
14
15/***************************************************************************
16
17    Constants
18
19***************************************************************************/
20
21#define APPLE2_NIBBLE_SIZE         416
22#define APPLE2_SMALL_NIBBLE_SIZE   374
23#define APPLE2_TRACK_COUNT         35
24#define APPLE2_SECTOR_COUNT         16
25#define APPLE2_SECTOR_SIZE         256
26
27
28
29/**************************************************************************/
30
31FLOPPY_OPTIONS_EXTERN(apple2);
32
33#endif /* AP2_DISK_H */
src/lib/formats/td0_dsk.c
r0r11458
1/*********************************************************************
2
3    formats/td0_dsk.c
4
5    TD0 disk images
6
7*********************************************************************/
8/*
9 * Based on Japanese version 29-NOV-1988
10 * LZSS coded by Haruhiko OKUMURA
11 * Adaptive Huffman Coding coded by Haruyasu YOSHIZAKI
12 * Edited and translated to English by Kenji RIKITAKE
13 */
14
15#include <string.h>
16#include "flopimg.h"
17
18#define BUFSZ           512     // new input buffer
19
20/* LZSS Parameters */
21
22#define N        4096    /* Size of string buffer */
23#define F        60    /* Size of look-ahead buffer */
24#define THRESHOLD    2
25#define NIL        N    /* End of tree's node  */
26
27
28/* Huffman coding parameters */
29
30#define N_CHAR      (256 - THRESHOLD + F)
31                /* character code (= 0..N_CHAR-1) */
32#define T         (N_CHAR * 2 - 1)    /* Size of table */
33#define R         (T - 1)            /* root position */
34#define MAX_FREQ    0x8000
35                    /* update when cumulative frequency */
36                    /* reaches to this value */
37
38struct td0dsk_tag
39{
40   int heads;
41   int tracks;
42   int sector_size;
43   UINT64 track_offsets[84*2]; /* offset within data for each track */
44   UINT8 *data;
45};
46
47struct tdlzhuf {
48   UINT16 r,
49               bufcnt,bufndx,bufpos,  // string buffer
50          // the following to allow block reads from input in next_word()
51               ibufcnt,ibufndx; // input buffer counters
52   UINT8  inbuf[BUFSZ];    // input buffer
53};
54
55
56struct td0dsk_t
57{
58   floppy_image *floppy_file;
59   UINT64 floppy_file_offset;
60
61   struct tdlzhuf tdctl;
62   UINT8 text_buf[N + F - 1];
63   UINT16 freq[T + 1];    /* cumulative freq table */
64
65/*
66 * pointing parent nodes.
67 * area [T..(T + N_CHAR - 1)] are pointers for leaves
68 */
69   INT16 prnt[T + N_CHAR];
70
71   /* pointing children nodes (son[], son[] + 1)*/
72   INT16 son[T];
73
74   UINT16 getbuf;
75   UINT8 getlen;
76
77   int data_read(UINT8 *buf, UINT16 size);
78   int next_word();
79   int GetBit();
80   int GetByte();
81   void StartHuff();
82   void reconst();
83   void update(int c);
84   INT16 DecodeChar();
85   INT16 DecodePosition();
86   void init_Decode();
87   int Decode(UINT8 *buf, int len);
88};
89
90static td0dsk_t td0dsk;
91
92
93static struct td0dsk_tag *get_tag(floppy_image *floppy)
94{
95   struct td0dsk_tag *tag;
96   tag = (td0dsk_tag *)floppy_tag(floppy);
97   return tag;
98}
99
100
101
102FLOPPY_IDENTIFY( td0_dsk_identify )
103{
104   UINT8 header[2];
105
106   floppy_image_read(floppy, header, 0, 2);
107   if (header[0]=='T' && header[1]=='D') {
108      *vote = 100;
109   } else if (header[0]=='t' && header[1]=='d') {
110      *vote = 100;
111   } else {
112      *vote = 0;
113   }
114   return FLOPPY_ERROR_SUCCESS;
115}
116
117static int td0_get_heads_per_disk(floppy_image *floppy)
118{
119   return get_tag(floppy)->heads;
120}
121
122static int td0_get_tracks_per_disk(floppy_image *floppy)
123{
124   return get_tag(floppy)->tracks;
125}
126
127static UINT64 td0_get_track_offset(floppy_image *floppy, int head, int track)
128{
129   return get_tag(floppy)->track_offsets[(track<<1) + head];
130}
131
132static floperr_t get_offset(floppy_image *floppy, int head, int track, int sector, int sector_is_index, UINT64 *offset)
133{
134   UINT64 offs;
135   UINT8 *header;
136   UINT8 sectors_per_track;
137   int i;
138
139   if ((head < 0) || (head >= get_tag(floppy)->heads) || (track < 0) || (track >= get_tag(floppy)->tracks)
140         || (sector < 0) )
141      return FLOPPY_ERROR_SEEKERROR;
142
143   // position on begining of track data
144   offs = td0_get_track_offset(floppy, head, track);
145
146   // read track header
147   header = get_tag(floppy)->data + offs - 4;
148
149   // take number of sectors per track
150   sectors_per_track = header[0];
151
152   if (!sector_is_index) {
153      // when taking ID's return seek error if number is over counter
154      if (sector > sectors_per_track) {
155         return FLOPPY_ERROR_SEEKERROR;
156      }
157   }
158
159   // move trought sectors
160   for(i=0;i < sector-1;i++) {
161      header = get_tag(floppy)->data + offs;
162      offs+= 6;
163      if ((header[4] & 0x30)==0) {
164         offs+= 2;
165         offs+= header[6] + (header[7]<<8);
166      }
167   }
168   // read size of sector
169   header = get_tag(floppy)->data + offs;
170   get_tag(floppy)->sector_size = 1 << (header[3] + 7);
171
172   if (offset)
173      *offset = offs;
174   return FLOPPY_ERROR_SUCCESS;
175}
176
177
178
179static floperr_t internal_td0_read_sector(floppy_image *floppy, int head, int track, int sector, int sector_is_index, void *buffer, size_t buflen)
180{
181   UINT64 offset;
182   floperr_t err;
183   UINT8 *header;
184   int size,realsize,i;
185   int buff_pos;
186   int data_pos;
187   UINT8 *data;
188   UINT8 *buf;
189
190   buf = (UINT8*)buffer;
191   // take sector offset
192   err = get_offset(floppy, head, track, sector, sector_is_index, &offset);
193   if (err)
194      return err;
195
196   // read sector header
197   header = get_tag(floppy)->data + offset;
198   offset+=6;
199   // if there is no date just jump out
200   if ((header[4] & 0x30)!=0) return FLOPPY_ERROR_SUCCESS;
201
202   offset+=3;
203   // take data size
204   size = header[6] + (header[7]<<8)-1;
205   // take real sector size
206   realsize =  1 << (header[3] + 7);
207
208   // read sector data
209   data = get_tag(floppy)->data + offset;
210   buff_pos = 0;
211   data_pos = 0;
212
213   switch(header[8]) {
214      case 0:
215            // encoding type 0
216            //  - plain data
217            memcpy(buffer,data,size);
218            break;
219      case 1:
220            // encoding type 1
221            //  - 2 bytes size
222            //  - 2 bytes of data
223            //  data is reapeted specified number of times
224            while(buff_pos<realsize) {
225               for (i=0;i<data[data_pos]+(data[data_pos+1] << 8);i++) {
226                  buf[buff_pos] = data[data_pos+2];buff_pos++;
227                  buf[buff_pos] = data[data_pos+3];buff_pos++;
228               }
229               data_pos+=4;
230            }
231            break;
232      case 2:
233            // encoding type 2
234            //  - if first byte is zero next byte represent size of
235            //      plain data after it
236            //  - if different then zero when multiply by 2 represent
237            //      size of data that should be reapeted next byte times
238            while(buff_pos<realsize) {
239               if (data[data_pos]==0x00) {
240                  int size_ = data[data_pos+1];
241                  memcpy(buf+buff_pos,data + data_pos + 2,size_);
242                  data_pos += 2 + size_;
243                  buff_pos += size_;
244               } else {
245                  int size_  = 2*data[data_pos];
246                  int repeat = data[data_pos+1];
247                  data_pos+=2;
248
249                  for (i=0;i<repeat;i++) {
250                     memcpy(buf + buff_pos,data + data_pos,size_);
251                     buff_pos += size_;
252                  }
253                  data_pos += size_;
254               }
255            }
256            break;
257      default:
258            return FLOPPY_ERROR_INTERNAL;
259   }
260   return FLOPPY_ERROR_SUCCESS;
261}
262
263
264static floperr_t td0_read_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen)
265{
266   return internal_td0_read_sector(floppy, head, track, sector, FALSE, buffer, buflen);
267}
268
269static floperr_t td0_read_indexed_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen)
270{
271   return internal_td0_read_sector(floppy, head, track, sector, TRUE, buffer, buflen);
272}
273
274static floperr_t td0_get_sector_length(floppy_image *floppy, int head, int track, int sector, UINT32 *sector_length)
275{
276   floperr_t err;
277   err = get_offset(floppy, head, track, sector, FALSE, NULL);
278   if (err)
279      return err;
280
281   if (sector_length) {
282      *sector_length = get_tag(floppy)->sector_size;
283   }
284   return FLOPPY_ERROR_SUCCESS;
285}
286
287static floperr_t td0_get_indexed_sector_info(floppy_image *floppy, int head, int track, int sector_index, int *cylinder, int *side, int *sector, UINT32 *sector_length, unsigned long *flags)
288{
289   floperr_t retVal;
290   UINT64 offset;
291   UINT8 *sector_info;
292
293   retVal = get_offset(floppy, head, track, sector_index, FALSE, &offset);
294   sector_info = get_tag(floppy)->data + offset;
295   if (cylinder)
296      *cylinder = sector_info[0];
297   if (side)
298      *side = sector_info[1];
299   if (sector)
300      *sector = sector_info[2];
301   if (sector_length) {
302      *sector_length = 1 << (sector_info[3] + 7);
303   }
304   if (flags) {
305      *flags = 0;
306      if (sector_info[4] & 0x02) *flags |= ID_FLAG_CRC_ERROR_IN_DATA_FIELD;
307      if (sector_info[4] & 0x04) *flags |= ID_FLAG_DELETED_DATA;
308   }
309
310   return retVal;
311}
312
313int td0dsk_t::data_read(UINT8 *buf, UINT16 size)
314{
315   if (floppy_file_offset + size > floppy_image_size(floppy_file) ) {
316      size = floppy_image_size(floppy_file) - floppy_file_offset;
317   }
318   floppy_image_read(floppy_file,buf,floppy_file_offset,size);
319   floppy_file_offset += size;
320   return size;
321}
322
323
324/*
325 * Tables for encoding/decoding upper 6 bits of
326 * sliding dictionary pointer
327 */
328
329/* decoder table */
330static const UINT8 d_code[256] = {
331    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
332    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
333    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
334    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
335    0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
336    0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
337    0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
338    0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
339    0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
340    0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
341    0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
342    0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
343    0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
344    0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
345    0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
346    0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09,
347    0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A,
348    0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B,
349    0x0C, 0x0C, 0x0C, 0x0C, 0x0D, 0x0D, 0x0D, 0x0D,
350    0x0E, 0x0E, 0x0E, 0x0E, 0x0F, 0x0F, 0x0F, 0x0F,
351    0x10, 0x10, 0x10, 0x10, 0x11, 0x11, 0x11, 0x11,
352    0x12, 0x12, 0x12, 0x12, 0x13, 0x13, 0x13, 0x13,
353    0x14, 0x14, 0x14, 0x14, 0x15, 0x15, 0x15, 0x15,
354    0x16, 0x16, 0x16, 0x16, 0x17, 0x17, 0x17, 0x17,
355    0x18, 0x18, 0x19, 0x19, 0x1A, 0x1A, 0x1B, 0x1B,
356    0x1C, 0x1C, 0x1D, 0x1D, 0x1E, 0x1E, 0x1F, 0x1F,
357    0x20, 0x20, 0x21, 0x21, 0x22, 0x22, 0x23, 0x23,
358    0x24, 0x24, 0x25, 0x25, 0x26, 0x26, 0x27, 0x27,
359    0x28, 0x28, 0x29, 0x29, 0x2A, 0x2A, 0x2B, 0x2B,
360    0x2C, 0x2C, 0x2D, 0x2D, 0x2E, 0x2E, 0x2F, 0x2F,
361    0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
362    0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F,
363};
364
365static const UINT8 d_len[256] = {
366    0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
367    0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
368    0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
369    0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
370    0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
371    0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
372    0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
373    0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
374    0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
375    0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
376    0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
377    0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
378    0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
379    0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
380    0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
381    0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
382    0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
383    0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
384    0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
385    0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
386    0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
387    0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
388    0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
389    0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
390    0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
391    0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
392    0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
393    0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
394    0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
395    0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
396    0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
397    0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
398};
399
400int td0dsk_t::next_word()
401{
402    if(tdctl.ibufndx >= tdctl.ibufcnt)
403    {
404        tdctl.ibufndx = 0;
405        tdctl.ibufcnt = data_read(tdctl.inbuf,BUFSZ);
406        if(tdctl.ibufcnt <= 0)
407            return(-1);
408    }
409    while (getlen <= 8) { // typically reads a word at a time
410        getbuf |= tdctl.inbuf[tdctl.ibufndx++] << (8 - getlen);
411        getlen += 8;
412    }
413    return(0);
414}
415
416
417int td0dsk_t::GetBit()    /* get one bit */
418{
419    INT16 i;
420    if(next_word() < 0)
421        return(-1);
422    i = getbuf;
423    getbuf <<= 1;
424    getlen--;
425        if(i < 0)
426        return(1);
427    else
428        return(0);
429}
430
431int td0dsk_t::GetByte()    /* get a byte */
432{
433    UINT16 i;
434    if(next_word() != 0)
435        return(-1);
436    i = getbuf;
437    getbuf <<= 8;
438    getlen -= 8;
439    i = i >> 8;
440    return((int) i);
441}
442
443
444
445/* initialize freq tree */
446
447void td0dsk_t::StartHuff()
448{
449    int i, j;
450
451    for (i = 0; i < N_CHAR; i++) {
452        freq[i] = 1;
453        son[i] = i + T;
454        prnt[i + T] = i;
455    }
456    i = 0; j = N_CHAR;
457    while (j <= R) {
458        freq[j] = freq[i] + freq[i + 1];
459        son[j] = i;
460        prnt[i] = prnt[i + 1] = j;
461        i += 2; j++;
462    }
463    freq[T] = 0xffff;
464    prnt[R] = 0;
465}
466
467
468/* reconstruct freq tree */
469
470void td0dsk_t::reconst()
471{
472    INT16 i, j, k;
473    UINT16 f, l;
474
475    /* halven cumulative freq for leaf nodes */
476    j = 0;
477    for (i = 0; i < T; i++) {
478        if (son[i] >= T) {
479            freq[j] = (freq[i] + 1) / 2;
480            son[j] = son[i];
481            j++;
482        }
483    }
484    /* make a tree : first, connect children nodes */
485    for (i = 0, j = N_CHAR; j < T; i += 2, j++) {
486        k = i + 1;
487        f = freq[j] = freq[i] + freq[k];
488        for (k = j - 1; f < freq[k]; k--);
489        k++;
490        l = (j - k) * 2;
491
492        /* movmem() is Turbo-C dependent
493           rewritten to memmove() by Kenji */
494
495        /* movmem(&freq[k], &freq[k + 1], l); */
496        (void)memmove(&freq[k + 1], &freq[k], l);
497        freq[k] = f;
498        /* movmem(&son[k], &son[k + 1], l); */
499        (void)memmove(&son[k + 1], &son[k], l);
500        son[k] = i;
501    }
502    /* connect parent nodes */
503    for (i = 0; i < T; i++) {
504        if ((k = son[i]) >= T) {
505            prnt[k] = i;
506        } else {
507            prnt[k] = prnt[k + 1] = i;
508        }
509    }
510}
511
512
513/* update freq tree */
514
515void td0dsk_t::update(int c)
516{
517    int i, j, k, l;
518
519    if (freq[R] == MAX_FREQ) {
520        reconst();
521    }
522    c = prnt[c + T];
523    do {
524        k = ++freq[c];
525
526        /* swap nodes to keep the tree freq-ordered */
527        if (k > freq[l = c + 1]) {
528            while (k > freq[++l]);
529            l--;
530            freq[c] = freq[l];
531            freq[l] = k;
532
533            i = son[c];
534            prnt[i] = l;
535            if (i < T) prnt[i + 1] = l;
536
537            j = son[l];
538            son[l] = i;
539
540            prnt[j] = c;
541            if (j < T) prnt[j + 1] = c;
542            son[c] = j;
543
544            c = l;
545        }
546    } while ((c = prnt[c]) != 0);    /* do it until reaching the root */
547}
548
549
550INT16 td0dsk_t::DecodeChar()
551{
552    int ret;
553    UINT16 c;
554
555    c = son[R];
556
557    /*
558     * start searching tree from the root to leaves.
559     * choose node #(son[]) if input bit == 0
560     * else choose #(son[]+1) (input bit == 1)
561     */
562    while (c < T) {
563        if((ret = GetBit()) < 0)
564            return(-1);
565        c += (unsigned) ret;
566        c = son[c];
567    }
568    c -= T;
569    update(c);
570    return c;
571}
572
573INT16 td0dsk_t::DecodePosition()
574{
575    INT16 bit;
576    UINT16 i, j, c;
577
578    /* decode upper 6 bits from given table */
579    if((bit=GetByte()) < 0)
580        return(-1);
581    i = (UINT16) bit;
582    c = (UINT16)d_code[i] << 6;
583    j = d_len[i];
584
585    /* input lower 6 bits directly */
586    j -= 2;
587    while (j--) {
588        if((bit = GetBit()) < 0)
589            return(-1);
590        i = (i << 1) + bit;
591    }
592    return(c | (i & 0x3f));
593}
594
595/* DeCompression
596
597split out initialization code to init_Decode()
598
599*/
600
601void td0dsk_t::init_Decode()
602{
603    int i;
604    getbuf = 0;
605    getlen = 0;
606    tdctl.ibufcnt= tdctl.ibufndx = 0; // input buffer is empty
607    tdctl.bufcnt = 0;
608    StartHuff();
609    for (i = 0; i < N - F; i++)
610        text_buf[i] = ' ';
611    tdctl.r = N - F;
612}
613
614
615int td0dsk_t::Decode(UINT8 *buf, int len)  /* Decoding/Uncompressing */
616{
617    INT16 c,pos;
618    int  count;  // was an unsigned long, seems unnecessary
619    for (count = 0; count < len; ) {
620            if(tdctl.bufcnt == 0) {
621                if((c = DecodeChar()) < 0)
622                    return(count); // fatal error
623                if (c < 256) {
624                    *(buf++) = c;
625                    text_buf[tdctl.r++] = c;
626                    tdctl.r &= (N - 1);
627                    count++;
628                }
629                else {
630                    if((pos = DecodePosition()) < 0)
631                           return(count); // fatal error
632                    tdctl.bufpos = (tdctl.r - pos - 1) & (N - 1);
633                    tdctl.bufcnt = c - 255 + THRESHOLD;
634                    tdctl.bufndx = 0;
635                }
636            }
637            else { // still chars from last string
638                while( tdctl.bufndx < tdctl.bufcnt && count < len ) {
639                    c = text_buf[(tdctl.bufpos + tdctl.bufndx) & (N - 1)];
640                    *(buf++) = c;
641                    tdctl.bufndx++;
642                    text_buf[tdctl.r++] = c;
643                    tdctl.r &= (N - 1);
644                    count++;
645                }
646                // reset bufcnt after copy string from text_buf[]
647                if(tdctl.bufndx >= tdctl.bufcnt)
648                    tdctl.bufndx = tdctl.bufcnt = 0;
649            }
650    }
651    return(count); // count == len, success
652}
653
654
655FLOPPY_CONSTRUCT( td0_dsk_construct )
656{
657   td0dsk_t *state = &td0dsk;
658   struct FloppyCallbacks *callbacks;
659   struct td0dsk_tag *tag;
660   UINT8 *header;
661   int number_of_sectors;
662   int position;
663   int i;
664   int track;
665
666   if(params)
667   {
668      // create
669      return FLOPPY_ERROR_UNSUPPORTED;
670   }
671
672   tag = (struct td0dsk_tag *) floppy_create_tag(floppy, sizeof(struct td0dsk_tag));
673   if (!tag)
674      return FLOPPY_ERROR_OUTOFMEMORY;
675
676   tag->data = (UINT8*)malloc(floppy_image_size(floppy));
677   if (tag->data==NULL) {
678      return FLOPPY_ERROR_OUTOFMEMORY;
679   }
680   floppy_image_read(floppy, tag->data, 0, floppy_image_size(floppy));
681   header = tag->data;
682
683   if (header[0]=='t') {
684      UINT8 obuf[BUFSZ];
685      int rd;
686      int off = 12;
687      int size = 0;
688      state->floppy_file = floppy;
689      state->init_Decode();
690      state->floppy_file_offset = 12;
691      do
692      {
693         if((rd = state->Decode(obuf, BUFSZ)) > 0) size += rd;
694      } while(rd == BUFSZ);
695      memcpy(obuf,tag->data,12);
696      free(tag->data);
697      tag->data = (UINT8*)malloc(size+12);
698      if (tag->data==NULL) {
699         return FLOPPY_ERROR_OUTOFMEMORY;
700      }
701      memcpy(tag->data,obuf,12);
702      state->floppy_file_offset = 12;
703      state->init_Decode();
704      do
705      {
706         if((rd = state->Decode(obuf, BUFSZ)) > 0) {
707            memcpy(tag->data+off,obuf,rd);
708            off += rd;
709         }
710      }  while(rd == BUFSZ);
711   }
712   header = tag->data;
713   tag->heads   = header[9];
714   if (tag->heads > 1) {
715      tag->heads  = 2;
716   }
717
718   //  header len + comment header + comment len
719   position = 12;
720   if (header[7] & 0x80) {
721      position += 10 + header[14] + (header[15]<<8);
722   }
723   tag->tracks = 0;
724   do {
725      // read track header
726      header = tag->data + position;
727      track = header[1];
728      number_of_sectors = header[0];
729      if (number_of_sectors!=0xff){
730         position+=4;
731         tag->track_offsets[(track<<1) + (header[2] & 1)] = position;
732         for(i=0;i<number_of_sectors;i++) {
733            // read sector header
734            header = tag->data + position;
735            position+=6;
736            // read sector size
737            if ((header[4] & 0x30)==0) {
738               // if there is sector data
739               header = tag->data + position;
740               position+=2;
741               // skip sector data
742               position+= header[0] + (header[1]<<8);
743            }
744         }
745         tag->tracks++;
746      }
747   } while(number_of_sectors!=0xff);
748   tag->tracks++;
749
750   callbacks = floppy_callbacks(floppy);
751   callbacks->read_sector = td0_read_sector;
752   callbacks->read_indexed_sector = td0_read_indexed_sector;
753   callbacks->get_sector_length = td0_get_sector_length;
754   callbacks->get_heads_per_disk = td0_get_heads_per_disk;
755   callbacks->get_tracks_per_disk = td0_get_tracks_per_disk;
756   callbacks->get_indexed_sector_info = td0_get_indexed_sector_info;
757   return FLOPPY_ERROR_SUCCESS;
758}
759
760FLOPPY_DESTRUCT( td0_dsk_destruct )
761{
762   struct td0dsk_tag *tag = get_tag(floppy);
763   free(tag->data);
764   tag->data = NULL;
765   return FLOPPY_ERROR_SUCCESS;
766}
src/lib/formats/d81_dsk.c
r0r11458
1/*********************************************************************
2
3    formats/d81_dsk.c
4
5    Floppy format code for Commodore 1581 disk images
6
7*********************************************************************/
8
9#include "basicdsk.h"
10#include "d81_dsk.h"
11
12/***************************************************************************
13    PARAMETERS
14***************************************************************************/
15
16#define D81_SIZE   819200
17
18/***************************************************************************
19    IMPLEMENTATION
20***************************************************************************/
21
22/*-------------------------------------------------
23    d81_translate_offset - translates the
24    physical offset to a logical offset within
25    the disk image
26-------------------------------------------------*/
27
28/*
29
30     file offset       | CBM logical  |  drive physical   |   specials
31     decimal sedecimal | track/sector | cyl head sec offs |
32     ------------------+--------------+-------------------+--------------
33           0 0x000000  |    01;00     |   00;01;01        | first block
34         256 0x000100  |    01;01     |   00;01;01   +256 |
35           .     .     |      .       |     .  .          |
36        4864 0x001300  |    01;19     |   00;01;10   +256 |
37        5120 0x001400  |    01;20     |   00;00;01        |
38           .     .     |      .       |     .  .          |
39        9984 0x002700  |    01;39     |   00;00;10   +256 |
40       10240 0x002800  |    02;00     |   01;01;01        |
41           .     .     |      .       |     .  .          |
42       15360 0x003C00  |    02;20     |   01;00;01        |
43           .     .     |      .       |     .  .          |
44       20480 0x005000  |    03;00     |   02;01;01        |
45           .     .     |      .       |     .  .          |
46           .     .     |      .       |     .  .          |
47       30729 0x007800  |    04;00     |   03;01;01        |
48           .     .     |      .       |     .  .          |
49           .     .     |      .       |     .  .          |
50           .     .     |      .       |     .  .          |
51      399360 0x061800  |    40;00     |   39;01;01        | disk header
52      399616 0x061900  |    40;01     |   39;01;01   +256 | 1st BAM block
53      399872 0x061A00  |    40;02     |   39;01;02        | 2nd BAM block
54      400128 0x061B00  |    40;03     |   39;01;02   +256 | 1st dir block
55           .     .     |      .       |     .  .          |
56      409600 0x064000  |    41;00     |   40;01;01        |
57           .     .     |      .       |     .  .          |
58           .     .     |      .       |     .  .          |
59           .     .     |      .       |     .  .          |
60      808960 0x0C5800  |    80;00     |   79;01;01        |
61           .     .     |      .       |     .  .          |
62      813824 0x0C6B00  |    80;19     |   79;01;10   +256 |
63      814080 0x0C6C00  |    80;20     |   79;00;01        |
64           .     .     |      .       |     .  .          |
65      818688 0x0C7E00  |    80;38     |   79;00;10        |
66      818944 0x0C7F00  |    80;39     |   79;00;10   +256 | last block
67
68*/
69
70static UINT64 d81_translate_offset(floppy_image *floppy, const struct basicdsk_geometry *geom, int track, int head, int sector)
71{
72   UINT64 offset = (track * 20) + (!head * 10) + sector;
73
74   return offset;
75}
76
77/*-------------------------------------------------
78    FLOPPY_IDENTIFY( d81_dsk_identify )
79-------------------------------------------------*/
80
81FLOPPY_IDENTIFY( d81_dsk_identify )
82{
83   *vote = (floppy_image_size(floppy) == D81_SIZE) ? 100 : 0;
84
85   return FLOPPY_ERROR_SUCCESS;
86}
87
88/*-------------------------------------------------
89    FLOPPY_CONSTRUCT( d81_dsk_construct )
90-------------------------------------------------*/
91
92/*
93PER TRACK ORGANIZATION:
94
95      Hex 4E written as a gap, with 10 sectors of data, with full gaps written for motor speed variation.
96
97PER SECTOR ORGANIZATION:
98
99      MFM Encoding
100      12 Bytes of Hex 00
101      3 Bytes of Hex A1 (Data Hex A1, Clock Hex 0A)
102      1 Byte of Hex FE (ID Address Mark)
103      1 Byte (Track Number)
104      1 Byte (Side Number)
105      1 Byte (Sector Number)
106      1 Byte (Sector Length, 02 for 512 Byte Sectors)
107      2 Bytes CRC (Cyclic Redundancy Check)
108      22 Bytes of Hex 22
109      12 Bytes of Hex 00
110      3 Bytes of Hex A1 (Data Hex A1, Clock Hex 0A)
111      1 Byte of Hex FB (Data Address Mark)
112      512 Bytes of Data
113      2 Bytes of CRC (Cyclic Redundancy Check)
114      38 Bytes of Hex 4E
115*/
116
117FLOPPY_CONSTRUCT( d81_dsk_construct )
118{
119   struct basicdsk_geometry geometry;
120
121   memset(&geometry, 0, sizeof(geometry));
122
123   geometry.heads = 2;
124   geometry.first_sector_id = 1;
125   geometry.sector_length = 512;
126   geometry.tracks = 80;
127   geometry.sectors = 10;
128   geometry.translate_offset = d81_translate_offset;
129
130   return basicdsk_construct(floppy, &geometry);
131}
src/lib/formats/sord_cas.c
r0r11458
1/**************************************************************************
2
3    sord_cas.c
4
5    Format code for Sord M5 cassette files
6
7**************************************************************************/
8
9#include <string.h>
10#include "sord_cas.h"
11
12#define SORDM5_WAVESAMPLES_HEADER  1
13#define SORDM5_WAVESAMPLES_TRAILER 1
14#define SORDM5_WAVESAMPLES_BLOCK   1
15
16static const UINT8 SORDM5_CAS_HEADER[6] = { 'S', 'O', 'R', 'D', 'M', '5'};
17
18static const struct CassetteModulation sordm5_cas_modulation =
19{
20   CASSETTE_MODULATION_SINEWAVE,
21   1575.0 - 300, 1575.0, 1575.0 + 300,
22   3150.0 - 600, 3150.0, 3150.0 + 600
23};
24
25static UINT8 cassette_image_read_uint8( cassette_image *cassette, UINT64 offset)
26{
27   UINT8 data;
28   cassette_image_read( cassette, &data, offset, 1);
29   return data;
30}
31
32static casserr_t sordm5_tap_identify( cassette_image *cassette, struct CassetteOptions *opts)
33{
34   return cassette_modulation_identify( cassette, &sordm5_cas_modulation, opts);
35}
36
37static casserr_t sordm5_tap_load( cassette_image *cassette)
38{
39   casserr_t err;
40   UINT64 image_size;
41   double time_index = 0.0;
42   double time_displacement;
43   UINT8 header[16];
44   UINT64 image_pos;
45   UINT8 block_type, byte, bit;
46   UINT32 block_size, i, j;
47   size_t filler_length;
48   // init
49   image_size = cassette_image_size(cassette);
50   image_pos = 0;
51   // read/check header
52   if (image_size < 16) return CASSETTE_ERROR_INVALIDIMAGE;
53   cassette_image_read( cassette, &header, image_pos, 16);
54   image_pos += 16;
55   if (memcmp( header, SORDM5_CAS_HEADER, sizeof(SORDM5_CAS_HEADER)) != 0) return CASSETTE_ERROR_INVALIDIMAGE;
56   // add silence (header)
57   err = cassette_put_sample( cassette, 0, time_index, SORDM5_WAVESAMPLES_HEADER, 0);
58   if (err) return err;
59   time_index += SORDM5_WAVESAMPLES_HEADER;
60   // process blocks
61   while (image_pos < image_size)
62   {
63      // read block type
64      block_type = cassette_image_read_uint8( cassette, image_pos + 0);
65      if ((block_type != 'H') && (block_type != 'D')) return CASSETTE_ERROR_INVALIDIMAGE;
66      // read block size
67      block_size = cassette_image_read_uint8( cassette, image_pos + 1);
68      if (block_size == 0) block_size = 0x100;
69      block_size += 3;
70      // add silence (header block)
71      if (block_type == 'H')
72      {
73         err = cassette_put_sample( cassette, 0, time_index, SORDM5_WAVESAMPLES_BLOCK, 0);
74         if (err) return err;
75         time_index += SORDM5_WAVESAMPLES_BLOCK;
76      }
77      // add sync
78      if (block_type == 'H') filler_length = 2.4 * (3150 / 8); else filler_length = 0.15 * (3150 / 8);
79      err = cassette_put_modulated_filler(cassette, 0, time_index, 0xFF, filler_length, &sordm5_cas_modulation, &time_displacement);
80      if (err) return err;
81      time_index += time_displacement;
82      // process block
83      for (i=0;i<block_size;i++)
84      {
85         // read byte
86         byte = cassette_image_read_uint8( cassette, image_pos + i);
87         // calc/check checksum
88         #if 0
89         if (i == block_size)
90         {
91            if (byte != (crc & 0xFF)) return CASSETTE_ERROR_INVALIDIMAGE;
92         }
93         if (i > 2) crc += byte;
94         #endif
95         // process byte
96         for (j=0;j<10;j++)
97         {
98            // get bit
99            if (j < 2)
100            {
101               bit = ((j==0)?1:0);
102            }
103            else
104            {
105               bit = (byte >> (j-2)) & 1;
106            }
107            // add bit
108            err = cassette_put_modulated_data_bit( cassette, 0, time_index, bit, &sordm5_cas_modulation, &time_displacement);
109            if (err) return err;
110            time_index += time_displacement;
111         }
112      }
113      // mark end of block
114      err = cassette_put_modulated_data_bit( cassette, 0, time_index, 1, &sordm5_cas_modulation, &time_displacement);
115      if (err) return err;
116      time_index += time_displacement;
117      // next block
118      image_pos += block_size;
119   }
120   // add silence (trailer)
121   err = cassette_put_sample( cassette, 0, time_index, SORDM5_WAVESAMPLES_TRAILER, 0);
122   if (err) return err;
123   time_index += SORDM5_WAVESAMPLES_TRAILER;
124   //
125   return CASSETTE_ERROR_SUCCESS;
126}
127
128static const struct CassetteFormat sordm5_cas_format =
129{
130   "cas",
131   sordm5_tap_identify,
132   sordm5_tap_load,
133   NULL
134};
135
136CASSETTE_FORMATLIST_START(sordm5_cassette_formats)
137   CASSETTE_FORMAT(sordm5_cas_format)
138CASSETTE_FORMATLIST_END
src/lib/formats/d81_dsk.h
r0r11458
1/*********************************************************************
2
3    formats/d81_dsk.h
4
5    Floppy format code for Commodore 1581 disk images
6
7*********************************************************************/
8
9#ifndef __D81_DSK__
10#define __D81_DSK__
11
12#include "flopimg.h"
13
14/***************************************************************************
15    PROTOTYPES
16***************************************************************************/
17
18FLOPPY_IDENTIFY( d81_dsk_identify );
19FLOPPY_CONSTRUCT( d81_dsk_construct );
20
21#endif
src/lib/formats/cgen_cas.c
r0r11458
1/********************************************************************
2
3Support for EACA Colour Genie .cas cassette images
4
5Current state: Not working. Only the sync signal and 0x66 byte get
6               recognized.
7
8********************************************************************/
9
10#include "formats/cgen_cas.h"
11
12#define TAPE_HEADER "Colour Genie - Virtual Tape File"
13
14#define SMPLO   -32768
15#define SMPHI   32767
16
17
18static int cas_size;
19static int level;
20
21
22static int cgenie_output_byte(INT16 *buffer, int sample_count, UINT8 data)
23{
24   int i;
25   int samples = 0;
26
27   for ( i = 0; i < 8; i++ )
28   {
29      /* Output bit boundary */
30      level ^= 1;
31      if ( buffer )
32         buffer[ sample_count + samples ] = level ? SMPHI : SMPLO;
33      samples++;
34
35      /* Output bit */
36      if ( data & 0x80 )
37         level ^= 1;
38      if ( buffer )
39         buffer[ sample_count + samples ] = level ? SMPHI : SMPLO;
40      samples++;
41
42      data <<= 1;
43   }
44   return samples;
45}
46
47
48static int cgenie_handle_cas(INT16 *buffer, const UINT8 *casdata)
49{
50   int   i, data_pos, sample_count;
51
52   data_pos = 0;
53   sample_count = 0;
54   level = 0;
55
56   /* Check for presence of optional header */
57   if ( ! memcmp( casdata, TAPE_HEADER, sizeof(TAPE_HEADER) - 1 ) )
58   {
59      /* Search for 0x00 or end of file */
60      while ( data_pos < cas_size && casdata[data_pos] )
61         data_pos++;
62
63      /* If we're at the end of the file it's not a valid .cas file */
64      if ( data_pos == cas_size )
65         return -1;
66
67      /* Skip the 0x00 byte */
68      data_pos++;
69   }
70
71   /* If we're at the end of the file it's not a valid .cas file */
72   if ( data_pos == cas_size )
73      return -1;
74
75   /* Check for beginning of tape file marker */
76   if ( casdata[data_pos] != 0x66 )
77      return -1;
78
79   /* Create header */
80   for ( i = 0; i < 256; i++ )
81      sample_count += cgenie_output_byte( buffer, sample_count, 0xaa );
82
83   /* Start outputting data */
84   while( data_pos < cas_size )
85   {
86      sample_count += cgenie_output_byte( buffer, sample_count, casdata[data_pos] );
87      data_pos++;
88   }
89   sample_count += cgenie_output_byte( buffer, sample_count, 0x00 );
90
91   return sample_count;
92}
93
94/*******************************************************************
95   Generate samples for the tape image
96********************************************************************/
97static int cgenie_cas_fill_wave(INT16 *buffer, int sample_count, UINT8 *bytes)
98{
99   return cgenie_handle_cas( buffer, bytes );
100}
101
102
103/*******************************************************************
104   Calculate the number of samples needed for this tape image
105********************************************************************/
106static int cgenie_cas_to_wav_size(const UINT8 *casdata, int caslen)
107{
108   cas_size = caslen;
109
110   return cgenie_handle_cas( NULL, casdata );
111}
112
113static const struct CassetteLegacyWaveFiller cgenie_cas_legacy_fill_wave =
114{
115   cgenie_cas_fill_wave,               /* fill_wave */
116   -1,                              /* chunk_size */
117   0,                              /* chunk_samples */
118   cgenie_cas_to_wav_size,               /* chunk_sample_calc */
119   2400,                           /* sample_frequency */
120   0,                              /* header_samples */
121   0                              /* trailer_samples */
122};
123
124
125static casserr_t cgenie_cas_identify(cassette_image *cassette, struct CassetteOptions *opts)
126{
127   return cassette_legacy_identify(cassette, opts, &cgenie_cas_legacy_fill_wave);
128}
129
130
131static casserr_t cgenie_cas_load(cassette_image *cassette)
132{
133   return cassette_legacy_construct(cassette, &cgenie_cas_legacy_fill_wave);
134}
135
136
137static const struct CassetteFormat cgenie_cas_format =
138{
139   "cas",
140   cgenie_cas_identify,
141   cgenie_cas_load,
142   NULL
143};
144
145
146CASSETTE_FORMATLIST_START(cgenie_cassette_formats)
147   CASSETTE_FORMAT(cgenie_cas_format)
148CASSETTE_FORMATLIST_END
149
src/lib/formats/rk_cas.c
r0r11458
1/*
2
3    Tape support for RK format
4
5*/
6#include "rk_cas.h"
7
8
9#define RK_WAV_FREQUENCY   44000
10#define WAVE_HIGH      32767
11#define WAVE_LOW      -32768
12
13#define RK_HEADER_LEN   256
14
15#define RK_SIZE_20 20
16#define RK_SIZE_22 22
17#define RK_SIZE_60 60
18
19static int     data_size;
20
21static INT16 *rk_emit_level(INT16 *p, int count, int level)
22{
23   int i;
24
25   for (i=0; i<count; i++)
26   {
27      *(p++) = level;
28   }
29   return p;
30}
31
32static INT16* rk_output_bit(INT16 *p, UINT8 b,int bitsize)
33{
34   if (b)
35   {
36      p = rk_emit_level (p, bitsize, WAVE_HIGH);
37      p = rk_emit_level (p, bitsize, WAVE_LOW);
38   }
39   else
40   {
41      p = rk_emit_level (p, bitsize, WAVE_LOW);
42      p = rk_emit_level (p, bitsize, WAVE_HIGH);
43
44   }
45    return p;
46}
47
48static INT16* rk_output_byte(INT16 *p, UINT8 byte,int bitsize)
49{
50   int i;
51   for (i=7; i>=0; i--)
52      p = rk_output_bit(p,(byte>>i) & 0x01, bitsize);
53
54   return p;
55}
56
57
58static int rk20_cas_to_wav_size( const UINT8 *casdata, int caslen ) {
59   data_size = caslen;
60   return  (RK_HEADER_LEN  * 8 * 2 +  8*2 + caslen * 8 * 2) * RK_SIZE_20;
61}
62
63static int rk22_cas_to_wav_size( const UINT8 *casdata, int caslen ) {
64   data_size = caslen;
65   return  (RK_HEADER_LEN  * 8 * 2 +  8*2 + caslen * 8 * 2) * RK_SIZE_22;
66}
67
68static int rk60_cas_to_wav_size( const UINT8 *casdata, int caslen ) {
69   data_size = caslen;
70   return  (RK_HEADER_LEN  * 8 * 2 +  8*2 + caslen * 8 * 2) * RK_SIZE_60;
71}
72
73static int gam_cas_to_wav_size( const UINT8 *casdata, int caslen ) {
74   data_size = caslen;
75   return  (RK_HEADER_LEN  * 8 * 2 +  caslen * 8 * 2) * RK_SIZE_20;
76}
77
78static int rk20_cas_fill_wave( INT16 *buffer, int length, UINT8 *bytes ) {
79   int i;
80   INT16 * p = buffer;
81
82   for (i=0; i<RK_HEADER_LEN; i++) {
83      p = rk_output_byte (p, 0x00, RK_SIZE_20 );
84   }
85
86   p = rk_output_byte (p, 0xE6, RK_SIZE_20);
87
88   for (i=0; i<data_size; i++)
89      p = rk_output_byte (p, bytes[i], RK_SIZE_20);
90
91   return p - buffer;
92}
93
94static int rk22_cas_fill_wave( INT16 *buffer, int length, UINT8 *bytes ) {
95   int i;
96   INT16 * p = buffer;
97
98   for (i=0; i<RK_HEADER_LEN; i++) {
99      p = rk_output_byte (p, 0x00, RK_SIZE_22 );
100   }
101
102   p = rk_output_byte (p, 0xE6, RK_SIZE_22);
103
104   for (i=0; i<data_size; i++)
105      p = rk_output_byte (p, bytes[i], RK_SIZE_22);
106
107   return p - buffer;
108}
109
110static int rk60_cas_fill_wave( INT16 *buffer, int length, UINT8 *bytes ) {
111   int i;
112   INT16 * p = buffer;
113
114   for (i=0; i<RK_HEADER_LEN; i++) {
115      p = rk_output_byte (p, 0x00, RK_SIZE_60 );
116   }
117
118   p = rk_output_byte (p, 0xE6, RK_SIZE_60);
119
120   for (i=0; i<data_size; i++)
121      p = rk_output_byte (p, bytes[i], RK_SIZE_60);
122
123   return p - buffer;
124}
125
126static int gam_cas_fill_wave( INT16 *buffer, int length, UINT8 *bytes ) {
127   int i;
128   INT16 * p = buffer;
129
130   for (i=0; i<RK_HEADER_LEN; i++) {
131      p = rk_output_byte (p, 0x00, RK_SIZE_20 );
132   }
133
134   for (i=0; i<data_size; i++)
135      p = rk_output_byte (p, bytes[i], RK_SIZE_20);
136
137   return p - buffer;
138}
139
140static const struct CassetteLegacyWaveFiller rk20_legacy_fill_wave = {
141   rk20_cas_fill_wave,         /* fill_wave */
142   -1,               /* chunk_size */
143   0,               /* chunk_samples */
144   rk20_cas_to_wav_size,         /* chunk_sample_calc */
145   RK_WAV_FREQUENCY,         /* sample_frequency */
146   0,               /* header_samples */
147   0               /* trailer_samples */
148};
149
150static casserr_t rk20_cassette_identify( cassette_image *cassette, struct CassetteOptions *opts ) {
151   return cassette_legacy_identify( cassette, opts, &rk20_legacy_fill_wave );
152}
153
154static casserr_t rk20_cassette_load( cassette_image *cassette ) {
155   return cassette_legacy_construct( cassette, &rk20_legacy_fill_wave );
156}
157
158static const struct CassetteLegacyWaveFiller rk22_legacy_fill_wave = {
159   rk22_cas_fill_wave,         /* fill_wave */
160   -1,               /* chunk_size */
161   0,               /* chunk_samples */
162   rk22_cas_to_wav_size,         /* chunk_sample_calc */
163   RK_WAV_FREQUENCY,         /* sample_frequency */
164   0,               /* header_samples */
165   0               /* trailer_samples */
166};
167
168static casserr_t rk22_cassette_identify( cassette_image *cassette, struct CassetteOptions *opts ) {
169   return cassette_legacy_identify( cassette, opts, &rk22_legacy_fill_wave );
170}
171
172static casserr_t rk22_cassette_load( cassette_image *cassette ) {
173   return cassette_legacy_construct( cassette, &rk22_legacy_fill_wave );
174}
175
176static const struct CassetteLegacyWaveFiller gam_legacy_fill_wave = {
177   gam_cas_fill_wave,         /* fill_wave */
178   -1,               /* chunk_size */
179   0,               /* chunk_samples */
180   gam_cas_to_wav_size,         /* chunk_sample_calc */
181   RK_WAV_FREQUENCY,         /* sample_frequency */
182   0,               /* header_samples */
183   0               /* trailer_samples */
184};
185
186static casserr_t gam_cassette_identify( cassette_image *cassette, struct CassetteOptions *opts ) {
187   return cassette_legacy_identify( cassette, opts, &gam_legacy_fill_wave );
188}
189
190static casserr_t gam_cassette_load( cassette_image *cassette ) {
191   return cassette_legacy_construct( cassette, &gam_legacy_fill_wave );
192}
193
194static const struct CassetteLegacyWaveFiller rk60_legacy_fill_wave = {
195   rk60_cas_fill_wave,         /* fill_wave */
196   -1,               /* chunk_size */
197   0,               /* chunk_samples */
198   rk60_cas_to_wav_size,         /* chunk_sample_calc */
199   RK_WAV_FREQUENCY,         /* sample_frequency */
200   0,               /* header_samples */
201   0               /* trailer_samples */
202};
203
204static casserr_t rk60_cassette_identify( cassette_image *cassette, struct CassetteOptions *opts ) {
205   return cassette_legacy_identify( cassette, opts, &rk60_legacy_fill_wave );
206}
207
208static casserr_t rk60_cassette_load( cassette_image *cassette ) {
209   return cassette_legacy_construct( cassette, &rk60_legacy_fill_wave );
210}
211
212static const struct CassetteFormat rku_cassette_format = {
213   "rku",
214   rk20_cassette_identify,
215   rk20_cassette_load,
216   NULL
217};
218
219static const struct CassetteFormat rk8_cassette_format = {
220   "rk8",
221   rk60_cassette_identify,
222   rk60_cassette_load,
223   NULL
224};
225
226static const struct CassetteFormat rks_cassette_format = {
227   "rks",
228   rk20_cassette_identify,
229   rk20_cassette_load,
230   NULL
231};
232
233static const struct CassetteFormat rko_cassette_format = {
234   "rko",
235   rk20_cassette_identify,
236   rk20_cassette_load,
237   NULL
238};
239
240static const struct CassetteFormat rkr_cassette_format = {
241   "rk,rkr",
242   rk20_cassette_identify,
243   rk20_cassette_load,
244   NULL
245};
246
247static const struct CassetteFormat rka_cassette_format = {
248   "rka",
249   rk20_cassette_identify,
250   rk20_cassette_load,
251   NULL
252};
253
254static const struct CassetteFormat rkm_cassette_format = {
255   "rkm",
256   rk22_cassette_identify,
257   rk22_cassette_load,
258   NULL
259};
260
261static const struct CassetteFormat rkp_cassette_format = {
262   "rkp",
263   rk20_cassette_identify,
264   rk20_cassette_load,
265   NULL
266};
267
268static const struct CassetteFormat gam_cassette_format = {
269   "gam,g16,pki",
270   gam_cassette_identify,
271   gam_cassette_load,
272   NULL
273};
274
275CASSETTE_FORMATLIST_START(rku_cassette_formats)
276   CASSETTE_FORMAT(rku_cassette_format)
277CASSETTE_FORMATLIST_END
278
279CASSETTE_FORMATLIST_START(rk8_cassette_formats)
280   CASSETTE_FORMAT(rk8_cassette_format)
281CASSETTE_FORMATLIST_END
282
283CASSETTE_FORMATLIST_START(rks_cassette_formats)
284   CASSETTE_FORMAT(rks_cassette_format)
285CASSETTE_FORMATLIST_END
286
287CASSETTE_FORMATLIST_START(rko_cassette_formats)
288   CASSETTE_FORMAT(rko_cassette_format)
289CASSETTE_FORMATLIST_END
290
291CASSETTE_FORMATLIST_START(rkr_cassette_formats)
292   CASSETTE_FORMAT(rkr_cassette_format)
293   CASSETTE_FORMAT(gam_cassette_format)
294CASSETTE_FORMATLIST_END
295
296CASSETTE_FORMATLIST_START(rka_cassette_formats)
297   CASSETTE_FORMAT(rka_cassette_format)
298CASSETTE_FORMATLIST_END
299
300CASSETTE_FORMATLIST_START(rkm_cassette_formats)
301   CASSETTE_FORMAT(rkm_cassette_format)
302CASSETTE_FORMATLIST_END
303
304CASSETTE_FORMATLIST_START(rkp_cassette_formats)
305   CASSETTE_FORMAT(rkp_cassette_format)
306CASSETTE_FORMATLIST_END
307
308CASSETTE_FORMATLIST_START(gam_cassette_formats)
309   CASSETTE_FORMAT(gam_cassette_format)
310CASSETTE_FORMATLIST_END
src/lib/formats/sord_cas.h
r0r11458
1/*********************************************************************
2
3    sord_cas.h
4
5    Format code for Sord M5 cassette files
6
7*********************************************************************/
8
9#ifndef SORD_CAS_H
10#define SORD_CAS_H
11
12#include "cassimg.h"
13
14CASSETTE_FORMATLIST_EXTERN(sordm5_cassette_formats);
15
16#endif /* SORD_CAS_H */
src/lib/formats/vg5k_cas.c
r0r11458
1/********************************************************************
2
3    Support for VG-5000 .k7 cassette images
4
5********************************************************************/
6#include "vg5k_cas.h"
7
8
9#define SMPLO   -32768
10#define SILENCE   0
11#define SMPHI   32767
12
13
14static int k7_size;
15
16/*******************************************************************
17   Generate one high-low cycle of sample data
18********************************************************************/
19INLINE int vg5k_cas_cycle(INT16 *buffer, int sample_pos, int len)
20{
21   int i = 0;
22
23   if (buffer)
24   {
25      while( i < len)
26      {
27         buffer[ sample_pos + i ] = SMPHI;
28         i++;
29      }
30
31      while( i < len * 2 )
32      {
33         buffer[ sample_pos + i ] = SMPLO;
34         i++;
35      }
36   }
37   return len * 2;
38}
39
40
41/*******************************************************************
42   Generate n samples of silence
43********************************************************************/
44INLINE int vg5k_cas_silence(INT16 *buffer, int sample_pos, int len)
45{
46   int i = 0;
47
48   if ( buffer )
49      for( i = 0; i < len; i++)
50         buffer[ sample_pos + i ] = SILENCE;
51
52   return len;
53}
54
55
56/*******************************************************************
57   Generate the end-byte samples
58********************************************************************/
59INLINE int vg5k_cas_eob(INT16 *buffer, int sample_pos)
60{
61   int i, samples = 0;
62
63   for (i = 0; i < 4; i++)
64      samples += vg5k_cas_cycle( buffer, sample_pos + samples, 5);
65
66   samples += vg5k_cas_cycle( buffer, sample_pos + samples, 10);
67
68   return samples;
69}
70
71
72INLINE int vg5k_cas_byte(INT16 *buffer, int sample_pos, UINT8 data)
73{
74/* Writing an entire byte */
75   int i, samples;
76
77   samples = 0;
78   for ( i = 0; i < 8; i++ )
79   {
80      if ( data & 0x01 )
81      {
82         samples += vg5k_cas_cycle( buffer, sample_pos + samples, 5 );
83         samples += vg5k_cas_cycle( buffer, sample_pos + samples, 5 );
84      }
85      else
86      {
87         samples += vg5k_cas_cycle( buffer, sample_pos + samples, 10 );
88      }
89
90      data >>= 1;
91   }
92   return samples;
93}
94
95
96/*******************************************************************
97   Generate n sample of synchro
98********************************************************************/
99INLINE int vg5k_k7_synchro(INT16 *buffer, int sample_pos, int len)
100{
101
102   int i, samples = 0;
103
104   for ( i = 0; i < len ; i++ )
105         samples += vg5k_cas_cycle( buffer, sample_pos + samples, 5);
106
107   samples += vg5k_cas_eob( buffer, sample_pos + samples);
108
109   return samples;
110}
111
112
113static int vg5k_handle_tap(INT16 *buffer, const UINT8 *casdata)
114{
115   int   data_pos, sample_count;
116
117   data_pos = 0;
118   sample_count = 0;
119
120   /* file has to start with an head block */
121   if (casdata[0] != 0xd3  || casdata[1] != 0xd3 || casdata[2] != 0xd3)
122      return -1;
123
124   /* on the entire file*/
125    while( data_pos < k7_size )
126   {
127      UINT16   block_size = 0;
128
129      /* Identify type of block */
130      if (casdata[data_pos] == 0xd3)
131      {
132         /* head block have fixed size of 32 byte */
133         block_size = 0x20;
134
135         /* 1 sec of silence before the head block */
136         sample_count += vg5k_cas_silence(buffer, sample_count, 44100);
137
138         /* head block starts with 30000 samples of synchro */
139         sample_count += vg5k_k7_synchro( buffer, sample_count, 30000 );
140      }
141      else if (casdata[data_pos] == 0xd6)
142      {
143         /* data block size is defined in head block */
144         block_size = (casdata[data_pos - 4] | casdata[data_pos - 3]<<8) +  20;
145
146         /* 10000 samples of silence before the data block */
147         sample_count += vg5k_cas_silence(buffer, sample_count, 10000);
148
149         /* data block starts with 7200 samples of synchro */
150         sample_count += vg5k_k7_synchro( buffer, sample_count, 7200);
151      }
152      else
153      {
154         /* tries to handle files that do not respect the size declared in the head block */
155         while (data_pos < k7_size && casdata[data_pos] != 0xd3 && casdata[data_pos] != 0xd6)
156            data_pos++;
157      }
158
159      /* Data samples */
160      for ( ; block_size ; data_pos++, block_size-- )
161        {
162          /* Make sure there are enough bytes left */
163          if (data_pos > k7_size)
164            return -1;
165
166          sample_count += vg5k_cas_byte( buffer, sample_count, casdata[data_pos] );
167
168         /* generate the end-byte samples */
169         sample_count += vg5k_cas_eob( buffer, sample_count);
170      }
171    }
172
173   /* Finish with 10000 samples of silence */
174   sample_count += vg5k_cas_silence(buffer, sample_count, 10000);
175
176   return sample_count;
177}
178
179
180/*******************************************************************
181   Generate samples for the tape image
182********************************************************************/
183static int vg5k_k7_fill_wave(INT16 *buffer, int sample_count, UINT8 *bytes)
184{
185   return vg5k_handle_tap(buffer, bytes);
186}
187
188
189/*******************************************************************
190   Calculate the number of samples needed for this tape image classical
191********************************************************************/
192static int vg5k_k7_to_wav_size(const UINT8 *casdata, int caslen)
193{
194   k7_size = caslen ;
195
196   return vg5k_handle_tap( NULL, casdata );
197}
198
199
200static const struct CassetteLegacyWaveFiller vg5k_legacy_fill_wave =
201{
202   vg5k_k7_fill_wave,                  /* fill_wave */
203   -1,                              /* chunk_size */
204   0,                              /* chunk_samples */
205   vg5k_k7_to_wav_size,                /* chunk_sample_calc */
206   44100,                           /* sample_frequency */
207   0,                              /* header_samples */
208   0                              /* trailer_samples */
209};
210
211static casserr_t vg5k_k7_identify(cassette_image *cassette, struct CassetteOptions *opts)
212{
213   return cassette_legacy_identify(cassette, opts, &vg5k_legacy_fill_wave);
214}
215
216
217static casserr_t vg5k_k7_load(cassette_image *cassette)
218{
219   return cassette_legacy_construct(cassette, &vg5k_legacy_fill_wave);
220}
221
222
223static const struct CassetteFormat vg5k_k7_format =
224{
225   "k7",
226   vg5k_k7_identify,
227   vg5k_k7_load,
228   NULL
229};
230
231
232CASSETTE_FORMATLIST_START(vg5k_cassette_formats)
233   CASSETTE_FORMAT(vg5k_k7_format)
234CASSETTE_FORMATLIST_END
src/lib/formats/cgen_cas.h
r0r11458
1/*********************************************************************
2
3    cgen_cas.h
4
5    Format code for EACA Colour Genie .cas casette files.
6
7*********************************************************************/
8
9#ifndef __CGEN_CAS_H
10#define __CGEN_CAS_H
11
12#include "cassimg.h"
13
14CASSETTE_FORMATLIST_EXTERN(cgenie_cassette_formats);
15
16#endif /* __CGEN_CAS_H */
17
src/lib/formats/rk_cas.h
r0r11458
1/*
2
3       rk_cas.h
4
5       Format code for rk cassette files
6*/
7
8#ifndef RK_CAS_H
9#define RK_CAS_H
10
11#include "cassimg.h"
12
13CASSETTE_FORMATLIST_EXTERN(rku_cassette_formats);
14CASSETTE_FORMATLIST_EXTERN(rk8_cassette_formats);
15CASSETTE_FORMATLIST_EXTERN(rks_cassette_formats);
16CASSETTE_FORMATLIST_EXTERN(rko_cassette_formats);
17CASSETTE_FORMATLIST_EXTERN(rkr_cassette_formats);
18CASSETTE_FORMATLIST_EXTERN(rka_cassette_formats);
19CASSETTE_FORMATLIST_EXTERN(rkm_cassette_formats);
20CASSETTE_FORMATLIST_EXTERN(rkp_cassette_formats);
21CASSETTE_FORMATLIST_EXTERN(gam_cassette_formats);
22
23#endif /* RK_CAS_H */
24
src/lib/formats/vg5k_cas.h
r0r11458
1/*********************************************************************
2
3    vg5k_cas.h
4
5*********************************************************************/
6
7#ifndef VG5K_CAS_H
8#define VG5K_CAS_H
9
10#include "cassimg.h"
11
12CASSETTE_FORMATLIST_EXTERN(vg5k_cassette_formats);
13
14#endif /* VG5K_CAS_H */
15
src/lib/formats/coco_cas.c
r0r11458
1/**************************************************************************
2
3    coco_cas.c
4
5    Format code for CoCo CAS (*.cas) files
6
7    This is the actual code that processes the CAS data.  CAS files are a
8    problem because they are a legacy of previous CoCo emulators, and most
9    of these emulators patch the system as a shortcut.  Thus, the ones and
10    zeroes in the file do not truly represent modulated pulses in the
11    waveform, but what the BIOS reads and writes.
12
13    In doing so, they make the CoCo more tolerant of short headers and lack
14    of delays between blocks.  This legacy is reflected in most CAS files in
15    use, and thus presents a problem for a pure hardware emulation like MESS.
16
17    One alternative is to preprocess the data on the CAS, file by file, but
18    this proves to be problematic because in the process, legitimate data
19    that is unrecognized by the preprocessor may get dropped.
20
21    The approach taken here is a hybrid approach - it retrieves the data
22    block by block until an error occurs; be it the end of the CAS or a
23    corrupt (?!) block.  When "legitimate" blocks are done processing, the
24    remainder of the data is added to the waveform in a traditional manner.
25    The result has proven to work quite well.
26
27    2005-May-04, P.Harvey-Smith, improved handling of some "copy protected"
28    Dragon games that have odd blocks in odd places, this has slightly
29    increased loading time but does mean games like "Rommel's Revenge" will
30    now load.
31
32**************************************************************************/
33
34#include "coco_cas.h"
35
36#define COCO_WAVESAMPLES_HEADER      (1.0)
37#define COCO_WAVESAMPLES_TRAILER   (1.0)
38#define COCO_LONGSILENCE         (5.0)
39
40//some games load with only 5s, but most games need 15s
41#define ALICE32_WAVESAMPLES_HEADER   (15.0)
42
43// PAIR is an endian-safe union useful for representing 32-bit CPU registers
44union PAIR
45{
46#ifdef LSB_FIRST
47   struct { UINT8 l,h,h2,h3; } b;
48   struct { UINT16 l,h; } w;
49   struct { INT8 l,h,h2,h3; } sb;
50   struct { INT16 l,h; } sw;
51#else
52   struct { UINT8 h3,h2,h,l; } b;
53   struct { INT8 h3,h2,h,l; } sb;
54   struct { UINT16 h,l; } w;
55   struct { INT16 h,l; } sw;
56#endif
57   UINT32 d;
58   INT32 sd;
59};
60
61static int synccount;
62
63const struct CassetteModulation coco_cas_modulation =
64{
65   CASSETTE_MODULATION_SINEWAVE,
66   600.0,   1200.0,   1500.0,
67   1500.0,   2400.0,   3000.0
68};
69
70
71
72static casserr_t coco_cas_identify(cassette_image *cassette, struct CassetteOptions *opts)
73{
74   return cassette_modulation_identify(cassette, &coco_cas_modulation, opts);
75}
76
77
78
79static int get_cas_block(cassette_image *cassette, UINT64 *offset, UINT8 *block, int *block_len)
80{
81   UINT8 block_length = 0;
82   UINT8 block_checksum = 0;
83   UINT64 current_offset;
84   UINT64 image_size;
85   PAIR p;
86   int i;
87   int state = 0;
88   int phase = 0;
89
90   synccount = 0;
91   p.w.l = 0;
92   image_size = cassette_image_size(cassette);
93   current_offset = *offset;
94
95   while(current_offset < image_size)
96   {
97      cassette_image_read(cassette, &p.b.h, current_offset, 1);
98      current_offset++;
99
100      for (i = 0; i < 8; i++)
101      {
102         p.w.l >>= 1;
103
104         if (state == 0)
105         {
106            /* searching for a block */
107            if (p.b.l == 0x3C)
108            {
109               /* found one! */
110               phase = i;
111               state++;
112            }
113            else if (p.b.l == 0x55)
114            {
115               synccount++;
116            }
117         }
118         else if (i == phase)
119         {
120            *(block++) = p.b.l;
121            switch(state) {
122            case 1:
123               /* found file type */
124               block_checksum = p.b.l;
125               state++;
126               break;
127            case 2:
128               /* found file size */
129               block_length = p.b.l;
130               *block_len = ((int) block_length) + 3;
131               block_checksum += p.b.l;
132               state++;
133               break;
134
135            case 3:
136               /* data byte */
137               if (block_length)
138               {
139                  block_length--;
140                  block_checksum += p.b.l;
141               }
142               else
143               {
144                  /* end of block */
145                  if (p.b.l != block_checksum)
146                  {
147                     /* checksum failure */
148                     return FALSE;
149                  }
150                  else
151                  {
152                     /* checksum success */
153                     *offset = current_offset;
154                     return TRUE;
155                  }
156               }
157            }
158         }
159      }
160   }
161
162   /* no more blocks */
163   return FALSE;
164}
165
166
167
168static casserr_t cas_load(cassette_image *cassette, UINT8 silence)
169{
170   casserr_t err;
171   UINT64 offset;
172   UINT64 image_size;
173   UINT8 block[258];   /* 255 bytes per block + 3 (type, length, checksum) */
174   int block_length = 0;
175   UINT8 last_blocktype;
176   double time_index = 0.0;
177   double time_displacement;
178   static const UINT8 magic_bytes[2] = { 0x55, 0x3C };
179
180#if 0
181   {
182      static const UINT8 dummy_bytes[] =
183      {
184         0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
185         0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
186         0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
187         0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
188         0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
189         0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
190         0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
191         0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
192         0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
193         0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
194         0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
195         0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
196         0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
197         0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
198         0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
199         0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
200         0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
201         0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
202         0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
203         0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
204         0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
205         0x3C, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A
206      };
207      time_index = 10.0;
208      return cassette_put_modulated_data(cassette, 0, time_index, dummy_bytes, sizeof(dummy_bytes), &coco_cas_modulation, &time_displacement);
209   }
210#endif
211
212   err = cassette_put_sample(cassette, 0, time_index, COCO_WAVESAMPLES_HEADER, 0);
213   if (err)
214      return err;
215   time_index += COCO_WAVESAMPLES_HEADER;
216
217   offset = 0;
218   last_blocktype = 0;
219   image_size = cassette_image_size(cassette);
220
221   /* try to find a block that we can untangle */
222   while(get_cas_block(cassette, &offset, block, &block_length))
223   {
224      /* Forcing a silence before a filename block, improves the ability to load some */
225      /* copy protected Dragon games, e.g. Rommel's Revenge */
226      /* was the last block a filename block? */
227      if ((last_blocktype == 0) || (last_blocktype == 0xFF) || (block[0] == 0))
228      {
229         /* silence */
230         err = cassette_put_sample(cassette, 0, time_index, silence, 0);
231         if (err)
232            return err;
233         time_index += silence;
234
235         /* sync data */
236         err = cassette_put_modulated_filler(cassette, 0, time_index, 0x55, 128, &coco_cas_modulation, &time_displacement);
237         if (err)
238            return err;
239         time_index += time_displacement;
240      }
241      else if (synccount != 0)      /* If we have multiple sync bytes in cas file, make sure they */
242      {            /* are passed through */
243         /* sync data */
244         err = cassette_put_modulated_filler(cassette, 0, time_index, 0x55, synccount, &coco_cas_modulation, &time_displacement);
245         if (err)
246            return err;
247         time_index += time_displacement;
248      }
249
250      /* now fill in the magic bytes */
251      err = cassette_put_modulated_data(cassette, 0, time_index, magic_bytes, sizeof(magic_bytes), &coco_cas_modulation, &time_displacement);
252      if (err)
253         return err;
254      time_index += time_displacement;
255
256      /* now fill in the block */
257      err = cassette_put_modulated_data(cassette, 0, time_index, block, block_length, &coco_cas_modulation, &time_displacement);
258      if (err)
259         return err;
260      time_index += time_displacement;
261
262      /* and the last magic byte */
263      err = cassette_put_modulated_filler(cassette, 0, time_index, 0x55, 1, &coco_cas_modulation, &time_displacement);
264      if (err)
265         return err;
266      time_index += time_displacement;
267
268      last_blocktype = block[0];
269   }
270
271   /* all futher data is undecipherable, so output it verbatim */
272   err = cassette_read_modulated_data(cassette, 0, time_index, offset, image_size - offset, &coco_cas_modulation, &time_displacement);
273   if (err)
274      return err;
275   time_index += time_displacement;
276
277   return CASSETTE_ERROR_SUCCESS;
278}
279
280static casserr_t coco_cas_load(cassette_image *cassette)
281{
282   return cas_load(cassette, COCO_WAVESAMPLES_HEADER);
283}
284
285static casserr_t alice32_cas_load(cassette_image *cassette)
286{
287   return cas_load(cassette, ALICE32_WAVESAMPLES_HEADER);
288}
289
290const struct CassetteFormat coco_cas_format =
291{
292   "cas",
293   coco_cas_identify,
294   coco_cas_load,
295   NULL
296};
297
298const struct CassetteFormat alice32_cas_format =
299{
300   "cas,c10,k7",
301   coco_cas_identify,
302   alice32_cas_load,
303   NULL
304};
305
306
307CASSETTE_FORMATLIST_START(coco_cassette_formats)
308   CASSETTE_FORMAT(coco_cas_format)
309CASSETTE_FORMATLIST_END
310
311CASSETTE_FORMATLIST_START(alice32_cassette_formats)
312   CASSETTE_FORMAT(alice32_cas_format)
313CASSETTE_FORMATLIST_END
src/lib/formats/imd_dsk.c
r0r11458
1/*********************************************************************
2
3    formats/imd_dsk.c
4
5    IMD disk images
6
7*********************************************************************/
8
9#include <string.h>
10#include "flopimg.h"
11
12struct imddsk_tag
13{
14   int heads;
15   int tracks;
16   int sector_size;
17   UINT64 track_offsets[84*2]; /* offset within data for each track */
18};
19
20
21static struct imddsk_tag *get_tag(floppy_image *floppy)
22{
23   struct imddsk_tag *tag;
24   tag = (imddsk_tag *)floppy_tag(floppy);
25   return tag;
26}
27
28
29
30FLOPPY_IDENTIFY( imd_dsk_identify )
31{
32   UINT8 header[3];
33
34   floppy_image_read(floppy, header, 0, 3);
35   if (header[0]=='I' && header[1]=='M' && header[2]=='D') {
36      *vote = 100;
37   } else {
38      *vote = 0;
39   }
40   return FLOPPY_ERROR_SUCCESS;
41}
42
43static int imd_get_heads_per_disk(floppy_image *floppy)
44{
45   return get_tag(floppy)->heads;
46}
47
48static int imd_get_tracks_per_disk(floppy_image *floppy)
49{
50   return get_tag(floppy)->tracks;
51}
52
53static UINT64 imd_get_track_offset(floppy_image *floppy, int head, int track)
54{
55   return get_tag(floppy)->track_offsets[(track<<1) + head];
56}
57
58static floperr_t get_offset(floppy_image *floppy, int head, int track, int sector, int sector_is_index, UINT64 *offset)
59{
60   UINT64 offs = 0;
61   UINT8 header[5];
62   UINT8 sector_num;
63   int i;
64
65
66   if ((head < 0) || (head >= get_tag(floppy)->heads) || (track < 0) || (track >= get_tag(floppy)->tracks)
67         || (sector < 0) )
68      return FLOPPY_ERROR_SEEKERROR;
69
70   offs = imd_get_track_offset(floppy,head,track);
71   floppy_image_read(floppy, header, offs, 5);
72
73   sector_num = header[3];
74   offs += 5 + sector_num; // skip header and sector numbering map
75   if(header[2] & 0x80) offs += sector_num; // skip cylinder numbering map
76   if(header[2] & 0x40) offs += sector_num; // skip head numbering map
77   get_tag(floppy)->sector_size = 1 << (header[4] + 7);
78   for(i=0;i<sector;i++) {
79      floppy_image_read(floppy, header, offs, 1); // take sector data type
80      switch(header[0]) {
81         case 0: offs++; break;
82         case 1:
83         case 3:
84         case 5:
85         case 7: offs += get_tag(floppy)->sector_size + 1; break;
86         default: offs += 2;
87      }
88   }
89   if (offset)
90      *offset = offs;
91   return FLOPPY_ERROR_SUCCESS;
92}
93
94
95
96static floperr_t internal_imd_read_sector(floppy_image *floppy, int head, int track, int sector, int sector_is_index, void *buffer, size_t buflen)
97{
98   UINT64 offset;
99   floperr_t err;
100   UINT8 header[1];
101
102   // take sector offset
103   err = get_offset(floppy, head, track, sector, sector_is_index, &offset);
104   if (err)
105      return err;
106
107   floppy_image_read(floppy, header, offset, 1);
108   switch(header[0]) {
109      case 0: break;
110      case 1:
111      case 3:
112      case 5:
113      case 7:
114            floppy_image_read(floppy, buffer, offset+1, buflen);
115            break;
116
117      default: // all data same
118            floppy_image_read(floppy, header, offset+1, 1);
119            memset(buffer,header[0],buflen);
120            break;
121   }
122
123   return FLOPPY_ERROR_SUCCESS;
124}
125
126
127static floperr_t imd_read_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen)
128{
129   return internal_imd_read_sector(floppy, head, track, sector, FALSE, buffer, buflen);
130}
131
132static floperr_t imd_read_indexed_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen)
133{
134   return internal_imd_read_sector(floppy, head, track, sector, TRUE, buffer, buflen);
135}
136
137static floperr_t imd_get_sector_length(floppy_image *floppy, int head, int track, int sector, UINT32 *sector_length)
138{
139   floperr_t err;
140   err = get_offset(floppy, head, track, sector, FALSE, NULL);
141   if (err)
142      return err;
143
144   if (sector_length) {
145      *sector_length = get_tag(floppy)->sector_size;
146   }
147   return FLOPPY_ERROR_SUCCESS;
148}
149
150static floperr_t imd_get_indexed_sector_info(floppy_image *floppy, int head, int track, int sector_index, int *cylinder, int *side, int *sector, UINT32 *sector_length, unsigned long *flags)
151{
152   UINT64 offset;
153   UINT8 header[5];
154   UINT8 hd;
155   UINT8 tr;
156   UINT32 sector_size;
157   UINT8 sector_num;
158
159   offset = imd_get_track_offset(floppy,head,track);
160   floppy_image_read(floppy, header, offset, 5);
161   tr = header[1];
162   hd = header[2];
163   sector_num = header[3];
164   sector_size = 1 << (header[4] + 7);
165   if (sector_index >= sector_num) return FLOPPY_ERROR_SEEKERROR;
166   if (cylinder) {
167      if (head & 0x80) {
168         floppy_image_read(floppy, header, offset + 5 + sector_num+ sector_index, 1);
169         *cylinder = header[0];
170      } else {
171         *cylinder = tr;
172      }
173   }
174   if (side) {
175      if (head & 0x40) {
176         if (head & 0x80) {
177            floppy_image_read(floppy, header, offset + 5 + 2 * sector_num+sector_index, 1);
178         } else {
179            floppy_image_read(floppy, header, offset + 5 + sector_num+sector_index, 1);
180         }
181         *side = header[0];
182      } else {
183         *side = hd & 1;
184      }
185   }
186   if (sector) {
187      floppy_image_read(floppy, header, offset + 5 + sector_index, 1);
188      *sector = header[0];
189   }
190   if (sector_length) {
191      *sector_length = sector_size;
192   }
193   if (flags) {
194      UINT8 skip;
195      if (head & 0x40) {
196         if (head & 0x80) {
197            skip = 3;
198         } else {
199            skip = 2;
200         }
201      } else {
202         skip = 1;
203      }
204      floppy_image_read(floppy, header, offset + 5 + skip * sector_num, 1);
205      *flags = 0;
206      if ((header[0]-1) & 0x02) *flags |= ID_FLAG_DELETED_DATA;
207      if ((header[0]-1) & 0x04) *flags |= ID_FLAG_CRC_ERROR_IN_DATA_FIELD;
208   }
209   return FLOPPY_ERROR_SUCCESS;
210}
211
212
213FLOPPY_CONSTRUCT( imd_dsk_construct )
214{
215   struct FloppyCallbacks *callbacks;
216   struct imddsk_tag *tag;
217   UINT8 header[0x100];
218   UINT64 pos = 0;
219   int sector_size = 0;
220   int sector_num;
221   int i;
222   if(params)
223   {
224      // create
225      return FLOPPY_ERROR_UNSUPPORTED;
226   }
227
228   tag = (struct imddsk_tag *) floppy_create_tag(floppy, sizeof(struct imddsk_tag));
229   if (!tag)
230      return FLOPPY_ERROR_OUTOFMEMORY;
231
232   floppy_image_read(floppy, header, pos, 1);
233   while(header[0]!=0x1a) {
234      pos++;
235      floppy_image_read(floppy, header, pos, 1);
236   }
237   pos++;
238   tag->tracks = 0;
239   tag->heads = 1;
240   do {
241      floppy_image_read(floppy, header, pos, 5);
242      if ((header[2] & 1)==1) tag->heads = 2;
243      tag->track_offsets[(header[1]<<1) + (header[2] & 1)] = pos;
244      sector_num = header[3];
245      pos += 5 + sector_num; // skip header and sector numbering map
246      if(header[2] & 0x80) pos += sector_num; // skip cylinder numbering map
247      if(header[2] & 0x40) pos += sector_num; // skip head numbering map
248      sector_size = 1 << (header[4] + 7);
249      for(i=0;i<sector_num;i++) {
250         floppy_image_read(floppy, header, pos, 1); // take sector data type
251         switch(header[0]) {
252            case 0: pos++; break;
253            case 1:
254            case 3:
255            case 5:
256            case 7: pos += sector_size + 1; break;
257            default: pos += 2;break;
258         }
259      }
260      tag->tracks += 1;
261   } while(pos < floppy_image_size(floppy));
262   if (tag->heads==2) {
263      tag->tracks = tag->tracks / 2;
264   }
265   callbacks = floppy_callbacks(floppy);
266   callbacks->read_sector = imd_read_sector;
267   callbacks->read_indexed_sector = imd_read_indexed_sector;
268   callbacks->get_sector_length = imd_get_sector_length;
269   callbacks->get_heads_per_disk = imd_get_heads_per_disk;
270   callbacks->get_tracks_per_disk = imd_get_tracks_per_disk;
271   callbacks->get_indexed_sector_info = imd_get_indexed_sector_info;
272
273   return FLOPPY_ERROR_SUCCESS;
274}
275
src/lib/formats/coco_cas.h
r0r11458
1/*********************************************************************
2
3    coco_cas.h
4
5    Format code for CoCo CAS (*.cas) files
6
7*********************************************************************/
8
9#ifndef COCO_CAS_H
10#define COCO_CAS_H
11
12#include "cassimg.h"
13
14extern const struct CassetteFormat coco_cas_format;
15extern const struct CassetteModulation coco_cas_modulation;
16
17CASSETTE_FORMATLIST_EXTERN(coco_cassette_formats);
18CASSETTE_FORMATLIST_EXTERN(alice32_cassette_formats);
19
20#endif /* COCO_CAS_H */
src/lib/formats/pmd_pmd.c
r0r11458
1/* .PMD tape images */
2#include "pmd_pmd.h"
3
4#define WAVEENTRY_LOW  -32768
5#define WAVEENTRY_HIGH  32767
6
7#define PMD85_WAV_FREQUENCY   7200
8#define PMD85_TIMER_FREQUENCY   1200
9#define PMD85_BIT_LENGTH   (PMD85_WAV_FREQUENCY/PMD85_TIMER_FREQUENCY)
10#define PMD85_PILOT_BITS   (PMD85_TIMER_FREQUENCY*3)
11#define PMD85_PAUSE_BITS   (PMD85_TIMER_FREQUENCY/2)
12#define PMD85_HEADER_BYTES   63
13#define PMD85_BITS_PER_BYTE   11
14
15static INT16 *pmd85_emit_level(INT16 *p, int count, int level)
16{
17   int i;
18
19   for (i=0; i<count; i++)   *(p++) = level;
20
21   return p;
22}
23
24static INT16* pmd85_output_bit(INT16 *p, UINT8 bit)
25{
26   if (bit)
27   {
28      p = pmd85_emit_level (p, PMD85_BIT_LENGTH/2, WAVEENTRY_LOW);
29      p = pmd85_emit_level (p, PMD85_BIT_LENGTH/2, WAVEENTRY_HIGH);
30   }
31   else
32   {
33      p = pmd85_emit_level (p, PMD85_BIT_LENGTH/2, WAVEENTRY_HIGH);
34      p = pmd85_emit_level (p, PMD85_BIT_LENGTH/2, WAVEENTRY_LOW);
35   }
36       return p;
37}
38
39static INT16* pmd85_output_byte(INT16 *p, UINT8 byte)
40{
41   int i;
42
43   /* start */
44   p = pmd85_output_bit (p, 0);
45
46   /* data */
47   for (i=0; i<8; i++)
48      p = pmd85_output_bit(p,(byte>>(7-i)) & 0x01);
49
50   /* stop */
51   p = pmd85_output_bit (p, 1);
52   p = pmd85_output_bit (p, 1);
53
54   return p;
55}
56
57static int pmd85_cassette_calculate_size_in_samples(const UINT8 *bytes, int length)
58{
59   return PMD85_BIT_LENGTH * (length * PMD85_BITS_PER_BYTE + PMD85_PILOT_BITS + PMD85_PAUSE_BITS);
60}
61
62static int pmd85_cassette_fill_wave(INT16 *buffer, int length, UINT8 *bytes)
63{
64   int i;
65   INT16 * p = buffer;
66
67   int data_size = ((length/PMD85_BIT_LENGTH-PMD85_PILOT_BITS-PMD85_PAUSE_BITS)/PMD85_BITS_PER_BYTE)-PMD85_HEADER_BYTES;
68
69   /* pilot */
70   for (i=0; i<PMD85_PILOT_BITS; i++)
71      p = pmd85_output_bit(p, 1);
72
73   /* header */
74   for (i=0; i<PMD85_HEADER_BYTES; i++)
75      p = pmd85_output_byte(p, bytes[i]);
76
77   /* pause */
78   for (i=0; i<PMD85_PAUSE_BITS; i++)
79      p = pmd85_output_bit(p, 1);
80
81   /* data */
82   for (i=0; i<data_size; i++)
83      p = pmd85_output_byte(p, bytes[i+PMD85_HEADER_BYTES]);
84
85   return p - buffer;
86}
87
88static const struct CassetteLegacyWaveFiller pmd85_legacy_fill_wave =
89{
90   pmd85_cassette_fill_wave,         /* fill_wave */
91   -1,                                 /* chunk_size */
92   0,                                 /* chunk_samples */
93   pmd85_cassette_calculate_size_in_samples,   /* chunk_sample_calc */
94   PMD85_WAV_FREQUENCY,                              /* sample_frequency */
95   0,                                 /* header_samples */
96   0                                 /* trailer_samples */
97};
98
99static casserr_t pmd85_pmd_identify(cassette_image *cassette, struct CassetteOptions *opts)
100{
101   return cassette_legacy_identify(cassette, opts, &pmd85_legacy_fill_wave);
102}
103
104static casserr_t pmd85_pmd_load(cassette_image *cassette)
105{
106   return cassette_legacy_construct(cassette, &pmd85_legacy_fill_wave);
107}
108
109static const struct CassetteFormat pmd85_pmd_image_format =
110{
111   "pmd",
112   pmd85_pmd_identify,
113   pmd85_pmd_load,
114   NULL
115};
116
117CASSETTE_FORMATLIST_START(pmd85_pmd_format)
118   CASSETTE_FORMAT(pmd85_pmd_image_format)
119CASSETTE_FORMATLIST_END
src/lib/formats/pmd_pmd.h
r0r11458
1/*********************************************************************
2
3    pmd_pmd.h
4
5    Format code for PMD-85 casette files
6
7*********************************************************************/
8
9#ifndef PMD_PMD_H
10#define PMD_PMD_H
11
12#include "cassimg.h"
13
14CASSETTE_FORMATLIST_EXTERN(pmd85_pmd_format);
15
16#endif /* PMD_PMD_H */
src/lib/formats/kim1_cas.c
r0r11458
1#include "kim1_cas.h"
2
3#define SMPLO   -32768
4#define SMPHI   32767
5
6
7static int cas_size;
8
9
10INLINE int kim1_output_signal( INT16 *buffer, int sample_pos, int high )
11{
12   int sample_count, i, j;
13
14   sample_count = 0;
15
16   if ( high )
17   {
18      /* high frequency (~3600Hz) */
19      for ( i = 0; i < 9; i++ )
20      {
21         if ( buffer )
22         {
23            for ( j = 0; j < 6; j++ )
24               buffer[ sample_pos + sample_count + j ] = SMPHI;
25         }
26         sample_count += 6;
27
28         if ( buffer )
29         {
30            for ( j = 0; j < 6; j++ )
31               buffer[ sample_pos + sample_count + j ] = SMPLO;
32         }
33         sample_count += 6;
34      }
35   }
36   else
37   {
38      /* low frequency (~2400Hz) */
39      for ( i = 0; i < 6; i++ )
40      {
41         if ( buffer )
42         {
43            for ( j = 0; j < 9; j++ )
44               buffer[ sample_pos + sample_count + j ] = SMPHI;
45         }
46         sample_count += 9;
47
48         if ( buffer )
49         {
50            for ( j = 0; j < 9; j++ )
51               buffer[ sample_pos + sample_count + j ] = SMPLO;
52         }
53         sample_count += 9;
54      }
55   }
56
57   return sample_count;
58}
59
60
61INLINE int kim1_output_byte( INT16 *buffer, int sample_pos, UINT8 byte )
62{
63   int i;
64   int sample_count = 0;
65
66   for ( i = 0; i < 8; i++ )
67   {
68      sample_count += kim1_output_signal( buffer, sample_pos + sample_count, 1 );
69      sample_count += kim1_output_signal( buffer, sample_pos + sample_count, byte & 0x01 ? 0 : 1 );
70      sample_count += kim1_output_signal( buffer, sample_pos + sample_count, 0 );
71      byte >>= 1;
72   }
73   return sample_count;
74}
75
76
77static int kim1_handle_kim(INT16 *buffer, const UINT8 *casdata)
78{
79   static const UINT8 encoding[16] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46 };
80   int   i, data_pos, sample_count;
81   UINT16 size, address, file_id, checksum;
82
83   if ( cas_size < 9 )   return -1;
84   if ( memcmp( casdata, "KIM1", 4 ) ) return -1;
85
86   address = casdata[4] | ( casdata[5] << 8 );
87   size = casdata[6] | ( casdata[7] << 8 );
88   file_id = casdata[8];
89
90   data_pos = 9;
91   sample_count = 0;
92   checksum = casdata[4] + casdata[5];
93
94   /* First output a sync header: 100 x 16h */
95   for ( i = 0; i < 100; i++ )
96      sample_count += kim1_output_byte( buffer, sample_count, 0x16 );
97
98   /* Output end of sync: 2Ah */
99   sample_count += kim1_output_byte( buffer, sample_count, 0x2A );
100
101   /* Output ID */
102   sample_count += kim1_output_byte( buffer, sample_count, encoding[ file_id >> 4 ] );
103   sample_count += kim1_output_byte( buffer, sample_count, encoding[ file_id & 0x0f ] );
104
105   /* Output starting address */
106   sample_count += kim1_output_byte( buffer, sample_count, encoding[ ( address & 0xff ) >> 4 ] );
107   sample_count += kim1_output_byte( buffer, sample_count, encoding[ address & 0x0f ] );
108   sample_count += kim1_output_byte( buffer, sample_count, encoding[ address >> 12 ] );
109   sample_count += kim1_output_byte( buffer, sample_count, encoding[ ( address >> 8 ) & 0x0f ] );
110
111   /* Output the data */
112   while( data_pos < cas_size && data_pos < ( size + 9 ) )
113   {
114      UINT8 data = casdata[data_pos];
115
116      sample_count += kim1_output_byte( buffer, sample_count, encoding[ data >> 4 ] );
117      sample_count += kim1_output_byte( buffer, sample_count, encoding[ data & 0x0f ] );
118      checksum += data;
119      data_pos++;
120   }
121
122   /* Output end of data marker: 2Fh */
123   sample_count += kim1_output_byte( buffer, sample_count, 0x2F );
124
125   /* Output checksum */
126   sample_count += kim1_output_byte( buffer, sample_count, encoding[ ( checksum & 0xff ) >> 4 ] );
127   sample_count += kim1_output_byte( buffer, sample_count, encoding[ checksum & 0x0f ] );
128   sample_count += kim1_output_byte( buffer, sample_count, encoding[ checksum >> 12 ] );
129   sample_count += kim1_output_byte( buffer, sample_count, encoding[ ( checksum >> 8 ) & 0x0f ] );
130
131   /* Output end of transmission marker: 2 x 04h */
132   sample_count += kim1_output_byte( buffer, sample_count, 0x04 );
133   sample_count += kim1_output_byte( buffer, sample_count, 0x04 );
134
135   return sample_count;
136}
137
138
139/*******************************************************************
140   Generate samples for the tape image
141********************************************************************/
142static int kim1_kim_fill_wave(INT16 *buffer, int sample_count, UINT8 *bytes)
143{
144   return kim1_handle_kim( buffer, bytes );
145}
146
147
148/*******************************************************************
149   Calculate the number of samples needed for this tape image
150********************************************************************/
151static int kim1_kim_to_wav_size(const UINT8 *casdata, int caslen)
152{
153   cas_size = caslen;
154
155   return kim1_handle_kim( NULL, casdata );
156}
157
158static const struct CassetteLegacyWaveFiller kim1_kim_legacy_fill_wave =
159{
160   kim1_kim_fill_wave,                  /* fill_wave */
161   -1,                              /* chunk_size */
162   0,                              /* chunk_samples */
163   kim1_kim_to_wav_size,               /* chunk_sample_calc */
164   44100,                           /* sample_frequency */
165   0,                              /* header_samples */
166   0                              /* trailer_samples */
167};
168
169
170static casserr_t kim1_kim_identify(cassette_image *cassette, struct CassetteOptions *opts)
171{
172   return cassette_legacy_identify(cassette, opts, &kim1_kim_legacy_fill_wave);
173}
174
175
176static casserr_t kim1_kim_load(cassette_image *cassette)
177{
178   return cassette_legacy_construct(cassette, &kim1_kim_legacy_fill_wave);
179}
180
181
182static const struct CassetteFormat kim1_kim_format =
183{
184   "kim,kim1",
185   kim1_kim_identify,
186   kim1_kim_load,
187   NULL
188};
189
190
191CASSETTE_FORMATLIST_START(kim1_cassette_formats)
192   CASSETTE_FORMAT(kim1_kim_format)
193CASSETTE_FORMATLIST_END
194
src/lib/formats/kim1_cas.h
r0r11458
1/*********************************************************************
2
3    kim1_cas.h
4
5    Format code for KIM-1 casette files.
6
7*********************************************************************/
8
9#ifndef __KIM1_CAS_H
10#define __KIM1_CAS_H
11
12#include "cassimg.h"
13
14CASSETTE_FORMATLIST_EXTERN(kim1_cassette_formats);
15
16#endif /* __KIM1_CAS_H */
17
src/lib/formats/oric_dsk.c
r0r11458
1/*********************************************************************
2
3    formats/oric_dsk.c
4
5    Oric disk images
6
7*********************************************************************/
8
9#include <string.h>
10
11#include "imageutl.h"
12#include "flopimg.h"
13#include "oric_dsk.h"
14#include "basicdsk.h"
15
16
17#define mfm_disk_header_size   0x0100
18#define MFM_ID   "MFM_DISK"
19
20#define TRACK_SIZE_MFM 0x1900
21
22struct mfm_disk_sector_info
23{
24   int id_ptr;
25   int data_ptr;
26   int sector_size;
27   UINT8 ddam;
28};
29
30struct oricdsk_tag
31{
32   int tracks;
33   int heads;
34   int geometry;
35   int tracksize;
36   int num_sectors;
37   struct mfm_disk_sector_info   sector_data[32];
38};
39
40
41static struct oricdsk_tag *get_tag(floppy_image *floppy)
42{
43   struct oricdsk_tag *tag;
44   tag = (oricdsk_tag *)floppy_tag(floppy);
45   return tag;
46}
47
48
49static FLOPPY_IDENTIFY(oric_dsk_identify)
50{
51   UINT8 header[mfm_disk_header_size];
52
53   floppy_image_read(floppy, header, 0, mfm_disk_header_size);
54   if ( memcmp( header, MFM_ID, 8 ) ==0) {
55      int heads  = pick_integer_le(header, 8, 4);
56      int tracks = pick_integer_le(header, 12, 4);
57
58      if (floppy_image_size(floppy)==((tracks*heads*TRACK_SIZE_MFM)+mfm_disk_header_size)) {
59         *vote = 100;
60      } else {
61         *vote = 0;
62      }
63   } else {
64      *vote = 0;
65   }
66   return FLOPPY_ERROR_SUCCESS;
67}
68static int oric_get_track_offset(floppy_image *floppy,int track, int head)
69{
70   if (get_tag(floppy)->geometry==1) {
71      return mfm_disk_header_size + (get_tag(floppy)->tracksize * track) + (head * get_tag(floppy)->tracksize * get_tag(floppy)->tracks);
72   } else {
73      return mfm_disk_header_size + (get_tag(floppy)->tracksize*((track * get_tag(floppy)->heads)+head));
74   }
75}
76
77static int oric_get_heads_per_disk(floppy_image *floppy)
78{
79   return get_tag(floppy)->heads;
80}
81
82static int oric_get_tracks_per_disk(floppy_image *floppy)
83{
84   return get_tag(floppy)->tracks;
85}
86
87static void mfm_info_cache_sector_info(floppy_image *floppy,int track,int head)
88{
89   UINT8 track_data[TRACK_SIZE_MFM];
90
91   /* initialise these with single density values if single density */
92   UINT8 IdMark = 0x0fe;
93   UINT8 DataMark = 0x0fb;
94   UINT8 DeletedDataMark = 0x0f8;
95
96   UINT8 SectorCount;
97   UINT8 SearchCode = 0;
98   UINT8 sector_number = 0;
99   int ptr = 0;
100   int track_offset = oric_get_track_offset(floppy,track,head);
101   floppy_image_read(floppy, track_data, track_offset, TRACK_SIZE_MFM);
102   SectorCount = 0;
103
104   do
105   {
106      switch (SearchCode)
107      {
108         /* searching for id's */
109         case 0:
110         {
111            /* found id mark? */
112            if (track_data[ptr] == IdMark)
113            {
114               sector_number  = track_data[ptr+3]-1;
115               /* store pointer to id mark */
116               get_tag(floppy)->sector_data[sector_number].id_ptr = ptr + track_offset;
117               SectorCount++;
118
119               /* grab N value - used to skip data in data field */
120               get_tag(floppy)->sector_data[sector_number].sector_size = (1<< (track_data[ptr+4]+7));
121
122               /* skip past id field and crc */
123               ptr+=7;
124
125               /* now looking for data field */
126               SearchCode = 1;
127            }
128            else
129            {
130               /* update position */
131               ptr++;
132            }
133         }
134         break;
135
136         /* searching for data id's */
137         case 1:
138         {
139            /* found data or deleted data? */
140            if ((track_data[ptr] == DataMark) || (track_data[ptr] == DeletedDataMark))
141            {
142               /* yes */
143               get_tag(floppy)->sector_data[sector_number].data_ptr = ptr + track_offset + 1;
144               get_tag(floppy)->sector_data[sector_number].ddam = (track_data[ptr] == DeletedDataMark) ? ID_FLAG_DELETED_DATA : 0;
145
146               /* skip data field and id */
147               ptr += get_tag(floppy)->sector_data[sector_number].sector_size + 3;
148
149               /* now looking for id field */
150               SearchCode = 0;
151            }
152            else
153            {
154               ptr++;
155            }
156         }
157         break;
158
159         default:
160            break;
161      }
162   }
163   while (ptr < TRACK_SIZE_MFM);
164   get_tag(floppy)->num_sectors = SectorCount;
165
166}
167
168static floperr_t get_offset(floppy_image *floppy, int head, int track, int sector, int sector_is_index, UINT64 *offset)
169{
170   UINT64 offs;
171
172   /* translate the sector to a raw sector */
173   if (!sector_is_index)
174   {
175      sector -= 1;
176   }
177   mfm_info_cache_sector_info(floppy,track,head);
178
179   /* check to see if we are out of range */
180   if ((head < 0) || (head >= get_tag(floppy)->heads) || (track < 0) || (track >= get_tag(floppy)->tracks)
181         || (sector < 0) || (sector >=get_tag(floppy)->num_sectors))
182      return FLOPPY_ERROR_SEEKERROR;
183
184   offs = get_tag(floppy)->sector_data[sector].data_ptr;
185   if (offset)
186      *offset = offs;
187   return FLOPPY_ERROR_SUCCESS;
188}
189
190
191
192static floperr_t internal_oric_read_sector(floppy_image *floppy, int head, int track, int sector, int sector_is_index, void *buffer, size_t buflen)
193{
194   UINT64 offset;
195   floperr_t err;
196   err = get_offset(floppy, head, track, sector, sector_is_index, &offset);
197   if (err)
198      return err;
199   floppy_image_read(floppy, buffer, offset, buflen);
200   return FLOPPY_ERROR_SUCCESS;
201}
202
203
204
205static floperr_t internal_oric_write_sector(floppy_image *floppy, int head, int track, int sector, int sector_is_index, const void *buffer, size_t buflen, int ddam)
206{
207   UINT64 offset;
208   floperr_t err;
209
210   err = get_offset(floppy, head, track, sector, sector_is_index, &offset);
211   if (err)
212      return err;
213
214   floppy_image_write(floppy, buffer, offset, buflen);
215   return FLOPPY_ERROR_SUCCESS;
216}
217
218
219
220static floperr_t oric_read_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen)
221{
222   return internal_oric_read_sector(floppy, head, track, sector, FALSE, buffer, buflen);
223}
224
225static floperr_t oric_write_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam)
226{
227   return internal_oric_write_sector(floppy, head, track, sector, FALSE, buffer, buflen, ddam);
228}
229
230static floperr_t oric_read_indexed_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen)
231{
232   return internal_oric_read_sector(floppy, head, track, sector, TRUE, buffer, buflen);
233}
234
235static floperr_t oric_write_indexed_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam)
236{
237   return internal_oric_write_sector(floppy, head, track, sector, TRUE, buffer, buflen, ddam);
238}
239
240static floperr_t oric_get_sector_length(floppy_image *floppy, int head, int track, int sector, UINT32 *sector_length)
241{
242   floperr_t err;
243   err = get_offset(floppy, head, track, sector, FALSE, NULL);
244   if (err)
245      return err;
246
247   if (sector_length) {
248      *sector_length = get_tag(floppy)->sector_data[sector].sector_size;
249   }
250   return FLOPPY_ERROR_SUCCESS;
251}
252
253
254
255static floperr_t oric_get_indexed_sector_info(floppy_image *floppy, int head, int track, int sector_index, int *cylinder, int *side, int *sector, UINT32 *sector_length, unsigned long *flags)
256{
257   floperr_t retVal;
258
259   sector_index += 1;
260
261   retVal = oric_get_sector_length(floppy, head, track, sector_index, sector_length);
262   if (sector_length!=NULL) {
263      *sector_length =  get_tag(floppy)->sector_data[sector_index-1].sector_size;
264   }
265   if (cylinder)
266      *cylinder = track;
267   if (side)
268      *side = head;
269   if (sector)
270      *sector = sector_index;
271   if (flags)
272      *flags = get_tag(floppy)->sector_data[sector_index].ddam;
273   return retVal;
274}
275
276
277static FLOPPY_CONSTRUCT(oric_dsk_construct)
278{
279   struct FloppyCallbacks *callbacks;
280   struct oricdsk_tag *tag;
281   UINT8 header[mfm_disk_header_size];
282
283   floppy_image_read(floppy, header, 0, mfm_disk_header_size);
284
285   tag = (struct oricdsk_tag *) floppy_create_tag(floppy, sizeof(struct oricdsk_tag));
286   if (!tag)
287      return FLOPPY_ERROR_OUTOFMEMORY;
288
289   tag->heads   = pick_integer_le(header, 8, 4);
290   tag->tracks  = pick_integer_le(header, 12, 4);
291   tag->geometry = pick_integer_le(header, 16, 4);
292   tag->tracksize = TRACK_SIZE_MFM;
293   memset(tag->sector_data,0,sizeof(tag->sector_data));
294
295   callbacks = floppy_callbacks(floppy);
296   callbacks->read_sector = oric_read_sector;
297   callbacks->write_sector = oric_write_sector;
298   callbacks->read_indexed_sector = oric_read_indexed_sector;
299   callbacks->write_indexed_sector = oric_write_indexed_sector;
300   callbacks->get_sector_length = oric_get_sector_length;
301   callbacks->get_heads_per_disk = oric_get_heads_per_disk;
302   callbacks->get_tracks_per_disk = oric_get_tracks_per_disk;
303   callbacks->get_indexed_sector_info = oric_get_indexed_sector_info;
304   return FLOPPY_ERROR_SUCCESS;
305}
306/* ----------------------------------------------------------------------- */
307
308FLOPPY_OPTIONS_START( oric )
309   FLOPPY_OPTION( oricmfm, "dsk", "Oric MFM floppy disk image",   oric_dsk_identify, oric_dsk_construct, NULL, NULL)
310   FLOPPY_OPTION( oric, "dsk", "Oric disk image", basicdsk_identify_default, basicdsk_construct_default, NULL,
311      HEADS([2])
312      TRACKS([80])
313      SECTORS([9])
314      SECTOR_LENGTH([512])
315      FIRST_SECTOR_ID([1]))
316FLOPPY_OPTIONS_END
src/lib/formats/oric_dsk.h
r0r11458
1/*********************************************************************
2
3    formats/oric_dsk.h
4
5    Oric disk images
6
7*********************************************************************/
8
9#ifndef ORIC_DSK_H
10#define ORIC_DSK_H
11
12#include "flopimg.h"
13
14/**************************************************************************/
15
16FLOPPY_OPTIONS_EXTERN(oric);
17
18#endif /* ORIC_DSK_H */
src/lib/formats/smx_dsk.c
r0r11458
1/*********************************************************************
2
3    formats/smx_dsk.c
4
5    Specialist MX disk images
6
7*********************************************************************/
8
9#include <string.h>
10
11#include "smx_dsk.h"
12#include "basicdsk.h"
13
14static FLOPPY_IDENTIFY(smx_dsk_identify)
15{
16   *vote = (floppy_image_size(floppy) == 819200) ? 100 : 0;
17   return FLOPPY_ERROR_SUCCESS;
18}
19
20static int smx_get_heads_per_disk(floppy_image *floppy)
21{
22   return 2;
23}
24
25static int smx_get_tracks_per_disk(floppy_image *floppy)
26{
27   return 80;
28}
29
30static UINT64 smx_translate_offset(floppy_image *floppy,
31    int track, int head, int sector)
32{
33   return (track * 1024 * 5 * 2) + (head * 1024 * 5) + 1024 * sector;
34}
35
36static floperr_t get_offset(floppy_image *floppy, int head, int track, int sector, int sector_is_index, UINT64 *offset)
37{
38   UINT64 offs;
39   /* translate the sector to a raw sector */
40   if (!sector_is_index)
41   {
42      sector -= 1;
43   }
44   /* check to see if we are out of range */
45   if ((head < 0) || (head >= 2) || (track < 0) || (track >= 80)
46         || (sector < 0) || (sector >= 6))
47      return FLOPPY_ERROR_SEEKERROR;
48
49   offs = smx_translate_offset(floppy, track, head, sector);
50   if (offset)
51      *offset = offs;
52   return FLOPPY_ERROR_SUCCESS;
53}
54
55
56
57static floperr_t internal_smx_read_sector(floppy_image *floppy, int head, int track, int sector, int sector_is_index, void *buffer, size_t buflen)
58{
59   UINT64 offset;
60   floperr_t err;
61   err = get_offset(floppy, head, track, sector, sector_is_index, &offset);
62   if (err)
63      return err;
64
65   floppy_image_read(floppy, buffer, offset, buflen);
66   return FLOPPY_ERROR_SUCCESS;
67}
68
69
70
71static floperr_t internal_smx_write_sector(floppy_image *floppy, int head, int track, int sector, int sector_is_index, const void *buffer, size_t buflen, int ddam)
72{
73   UINT64 offset;
74   floperr_t err;
75
76   err = get_offset(floppy, head, track, sector, sector_is_index, &offset);
77   if (err)
78      return err;
79
80   floppy_image_write(floppy, buffer, offset, buflen);
81   return FLOPPY_ERROR_SUCCESS;
82}
83
84
85
86static floperr_t smx_read_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen)
87{
88   return internal_smx_read_sector(floppy, head, track, sector, FALSE, buffer, buflen);
89}
90
91static floperr_t smx_write_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam)
92{
93   return internal_smx_write_sector(floppy, head, track, sector, FALSE, buffer, buflen, ddam);
94}
95
96static floperr_t smx_read_indexed_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen)
97{
98   return internal_smx_read_sector(floppy, head, track, sector, TRUE, buffer, buflen);
99}
100
101static floperr_t smx_write_indexed_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam)
102{
103   return internal_smx_write_sector(floppy, head, track, sector, TRUE, buffer, buflen, ddam);
104}
105
106static floperr_t smx_get_sector_length(floppy_image *floppy, int head, int track, int sector, UINT32 *sector_length)
107{
108   floperr_t err;
109   err = get_offset(floppy, head, track, sector, FALSE, NULL);
110   if (err)
111      return err;
112
113   if (sector_length) {
114      *sector_length = 1024;
115   }
116   return FLOPPY_ERROR_SUCCESS;
117}
118
119
120
121static floperr_t smx_get_indexed_sector_info(floppy_image *floppy, int head, int track, int sector_index, int *cylinder, int *side, int *sector, UINT32 *sector_length, unsigned long *flags)
122{
123   sector_index += 1;
124   if (cylinder)
125      *cylinder = track;
126   if (side)
127      *side = head;
128   if (sector)
129      *sector = sector_index;
130   if (flags)
131      /* TODO: read DAM or DDAM and determine flags */
132      *flags = 0;
133   return smx_get_sector_length(floppy, head, track, sector_index, sector_length);
134}
135
136
137static FLOPPY_CONSTRUCT(smx_dsk_construct)
138{
139   struct FloppyCallbacks *callbacks;
140   callbacks = floppy_callbacks(floppy);
141   callbacks->read_sector = smx_read_sector;
142   callbacks->write_sector = smx_write_sector;
143   callbacks->read_indexed_sector = smx_read_indexed_sector;
144   callbacks->write_indexed_sector = smx_write_indexed_sector;
145   callbacks->get_sector_length = smx_get_sector_length;
146   callbacks->get_heads_per_disk = smx_get_heads_per_disk;
147   callbacks->get_tracks_per_disk = smx_get_tracks_per_disk;
148   callbacks->get_indexed_sector_info = smx_get_indexed_sector_info;
149
150   return FLOPPY_ERROR_SUCCESS;
151}
152
153
154
155/* ----------------------------------------------------------------------- */
156
157FLOPPY_OPTIONS_START( specimx )
158   FLOPPY_OPTION( smx_dsk, "odi", "Specialist MX floppy disk image",   smx_dsk_identify, smx_dsk_construct, NULL, NULL)
159FLOPPY_OPTIONS_END
src/lib/formats/smx_dsk.h
r0r11458
1/*********************************************************************
2
3    formats/smx_dsk.h
4
5    SVI318 disk images
6
7*********************************************************************/
8
9#ifndef SMX_DSK_H
10#define SMX_DSK_H
11
12#include "flopimg.h"
13
14/**************************************************************************/
15
16FLOPPY_OPTIONS_EXTERN(specimx);
17
18#endif /* SVI_DSK_H */
src/lib/formats/a26_cas.c
r0r11458
1/*
2
3Atari 2600 SuperCharger support
4
5*/
6
7
8#include "formats/a26_cas.h"
9
10
11#define A26_CAS_SIZE         8448
12#define A26_WAV_FREQUENCY      44100
13#define BIT_ZERO_LENGTH         10
14#define BIT_ONE_LENGTH         15
15#define ZEROS_ONES            2755
16
17static const UINT16 one_wave[BIT_ONE_LENGTH] = {
18   0x2AE5, 0x4E60, 0x644E, 0x68E4, 0x5B56, 0x3DFE, 0x15ED, 0xEA13, 0xC202, 0xA4AA,
19   0x971C, 0x9BB2, 0xB1A0, 0xD51B, 0x0000
20};
21
22static const UINT16 zero_wave[BIT_ZERO_LENGTH] = {
23   0x3DFE, 0x644E, 0x644E, 0x3DFE, 0x0000, 0xC202, 0x9BB2, 0x9BB2, 0xC202, 0x0000
24};
25
26static int a26_cas_output_wave( INT16 **buffer, INT16 wave_data, int length ) {
27   int i;
28
29   if ( buffer ) {
30      for ( i = 0 ; i < length; i++ ) {
31         **buffer = wave_data;
32         *buffer = *buffer + 1;
33      }
34   }
35   return length;
36}
37
38static int a26_cas_output_bit( INT16 **buffer, int bit ) {
39   int size = 0;
40   int   bit_size = bit ? BIT_ONE_LENGTH : BIT_ZERO_LENGTH;
41   const INT16 *p = bit ? (const INT16 *)one_wave : (const INT16 *)zero_wave;
42   int i;
43
44   for ( i = 0; i < bit_size; i++ ) {
45      size += a26_cas_output_wave( buffer, p[i], 1 );
46   }
47
48   return size;
49}
50
51static int a26_cas_output_byte( INT16 **buffer, UINT8 byte ) {
52   int      size = 0;
53   int      i;
54
55   for( i = 0; i < 8; i++, byte <<= 1 ) {
56      size += a26_cas_output_bit( buffer, ( ( byte & 0x80 ) ? 1 : 0 ) );
57   }
58   return size;
59}
60
61static int a26_cas_clearing_tone( INT16 **buffer ) {
62   int      size = 0;
63
64   size += a26_cas_output_wave( buffer, 0, 44100 );
65   return size;
66}
67
68static int a26_cas_zeros_ones( INT16 **buffer ) {
69   int      size = 0;
70   int      i;
71
72   for ( i = 0; i < ZEROS_ONES; i++ ) {
73      size += a26_cas_output_bit( buffer, 0 );
74      size += a26_cas_output_bit( buffer, 1 );
75   }
76   size += a26_cas_output_bit( buffer, 0 );
77   size += a26_cas_output_bit( buffer, 0 );
78   return size;
79}
80
81static int a26_cas_output_contents( INT16 **buffer, const UINT8 *bytes ) {
82   int      size = 0;
83   int      i, pages, j;
84
85   /* There are 8 header bytes */
86   for( i = 0; i < 8; i++ ) {
87      size += a26_cas_output_byte( buffer, bytes[ 0x2000 + i ] );
88   }
89
90   pages = bytes[0x2003];
91
92   /* Output each page prefixed with a small page header */
93   for ( i = 0; i < pages; i++ ) {
94      size += a26_cas_output_byte( buffer, bytes[ 0x2010 + i ] );
95      size += a26_cas_output_byte( buffer, bytes[ 0x2040 + i ] );
96      for ( j = 0; j < 256; j++ ) {
97         size += a26_cas_output_byte( buffer, bytes[ i * 256 + j ] );
98      }
99   }
100
101   return size;
102}
103
104static int a26_cas_do_work( INT16 **buffer, const UINT8 *bytes ) {
105   int      size = 0;
106
107   /* Output clearing tone */
108   size += a26_cas_clearing_tone( buffer );
109
110   /* Output header tone, alternating 1s and 0s for about a second ending with two 0s */
111   size += a26_cas_zeros_ones( buffer );
112
113   /* Output the actual contents of the tape */
114   size += a26_cas_output_contents( buffer, bytes );
115
116   /* Output footer tone, alternating 1s and 0s for about a second ending with two 0s */
117   size += a26_cas_zeros_ones( buffer );
118
119   return size;
120}
121
122static int a26_cas_fill_wave( INT16 *buffer, int length, UINT8 *bytes ) {
123   INT16   *p = buffer;
124
125   return a26_cas_do_work( &p, (const UINT8 *)bytes );
126}
127
128static int a26_cas_to_wav_size( const UINT8 *casdata, int caslen ) {
129   return a26_cas_do_work( NULL, casdata );
130}
131
132static const struct CassetteLegacyWaveFiller a26_legacy_fill_wave = {
133   a26_cas_fill_wave,
134   -1,
135   0,
136   a26_cas_to_wav_size,
137   A26_WAV_FREQUENCY,
138   0,
139   0
140};
141
142static casserr_t a26_cassette_identify( cassette_image *cassette, struct CassetteOptions *opts ) {
143   UINT64 size;
144
145   size = cassette_image_size( cassette );
146   if ( size == A26_CAS_SIZE ) {
147      return cassette_legacy_identify( cassette, opts, &a26_legacy_fill_wave );
148   }
149   return CASSETTE_ERROR_INVALIDIMAGE;
150}
151
152static casserr_t a26_cassette_load( cassette_image *cassette ) {
153   return cassette_legacy_construct( cassette, &a26_legacy_fill_wave );
154}
155
156static const struct CassetteFormat a26_cassette_format = {
157   "a26",
158   a26_cassette_identify,
159   a26_cassette_load,
160   NULL
161};
162
163CASSETTE_FORMATLIST_START(a26_cassette_formats)
164   CASSETTE_FORMAT(a26_cassette_format)
165CASSETTE_FORMATLIST_END
166
src/lib/formats/a26_cas.h
r0r11458
1/*
2    a26_cas.h
3
4    Format code for a2600 SuperCharger cassette files
5*/
6
7#ifndef A26_CAS_H
8#define A26_CAS_H
9
10#include "cassimg.h"
11
12CASSETTE_FORMATLIST_EXTERN(a26_cassette_formats);
13
14#endif /* A26_CAS_H */
src/lib/formats/basicdsk.c
r0r11458
1/*********************************************************************
2
3    formats/basicdsk.c
4
5    Floppy format code for basic disks
6
7*********************************************************************/
8
9#include <stdlib.h>
10#include <string.h>
11#include <assert.h>
12
13#include "basicdsk.h"
14
15static floperr_t basicdsk_read_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen);
16static floperr_t basicdsk_write_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam);
17static floperr_t basicdsk_read_indexed_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen);
18static floperr_t basicdsk_write_indexed_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam);
19static floperr_t basicdsk_get_sector_length(floppy_image *floppy, int head, int track, int sector, UINT32 *sector_length);
20static floperr_t basicdsk_get_indexed_sector_info(floppy_image *floppy, int head, int track, int sector_index, int *cylinder, int *side, int *sector, UINT32 *sector_length, unsigned long *flags);
21static int basicdsk_get_heads_per_disk(floppy_image *floppy);
22static int basicdsk_get_tracks_per_disk(floppy_image *floppy);
23static floperr_t basicdsk_format_track(floppy_image *floppy, int head, int track, option_resolution *params);
24
25
26
27#define BASICDSK_TAG   "basicdsktag"
28
29struct basicdsk_tag
30{
31   struct basicdsk_geometry geometry;
32};
33
34
35
36/********************************************************************/
37
38static const struct basicdsk_geometry *get_geometry(floppy_image *floppy)
39{
40   const struct basicdsk_tag *tag;
41   tag = (const basicdsk_tag *)floppy_tag(floppy);
42   return &tag->geometry;
43}
44
45
46
47floperr_t basicdsk_construct(floppy_image *floppy, const struct basicdsk_geometry *geometry)
48{
49   struct basicdsk_tag *tag;
50   struct FloppyCallbacks *format;
51
52   assert(geometry->heads);
53   assert(geometry->tracks);
54   assert(geometry->sectors);
55
56   tag = (struct basicdsk_tag *) floppy_create_tag(floppy, sizeof(struct basicdsk_tag));
57   if (!tag)
58      return FLOPPY_ERROR_OUTOFMEMORY;
59   tag->geometry = *geometry;
60
61   /* set up format callbacks */
62   format = floppy_callbacks(floppy);
63   format->read_sector = basicdsk_read_sector;
64   format->write_sector = basicdsk_write_sector;
65   format->read_indexed_sector = basicdsk_read_indexed_sector;
66   format->write_indexed_sector = basicdsk_write_indexed_sector;
67   format->get_sector_length = basicdsk_get_sector_length;
68   format->get_heads_per_disk = basicdsk_get_heads_per_disk;
69   format->get_tracks_per_disk = basicdsk_get_tracks_per_disk;
70   format->get_indexed_sector_info = basicdsk_get_indexed_sector_info;
71   format->format_track = basicdsk_format_track;
72
73   return FLOPPY_ERROR_SUCCESS;
74}
75
76
77
78static floperr_t get_offset(floppy_image *floppy, int head, int track, int sector, int sector_is_index, UINT64 *offset)
79{
80   const struct basicdsk_geometry *geom;
81   UINT64 offs;
82
83   geom = get_geometry(floppy);
84
85   /* translate the sector to a raw sector */
86   if (!sector_is_index)
87   {
88      sector -= geom->first_sector_id;
89   }
90
91   if (geom->translate_sector)
92      sector = geom->translate_sector(floppy, sector);
93
94   /* check to see if we are out of range */
95   if ((head < 0) || (head >= geom->heads) || (track < 0) || (track >= geom->tracks)
96         || (sector < 0) || (sector >= geom->sectors))
97      return FLOPPY_ERROR_SEEKERROR;
98
99   if (geom->translate_offset)
100      offs = geom->translate_offset(floppy, geom, track, head, sector);
101   else
102   {
103      offs = 0;
104      offs += track;
105      offs *= geom->heads;
106      offs += head;
107      offs *= geom->sectors;
108      offs += sector;
109   }
110   offs *= geom->sector_length;
111   offs += geom->offset;
112
113   if (offset)
114      *offset = offs;
115   return FLOPPY_ERROR_SUCCESS;
116}
117
118
119
120static int internal_basicdsk_translate_sector_interleave(floppy_image *floppy, int sector)
121{
122   const struct basicdsk_geometry *geom = get_geometry(floppy);
123   if (sector >= geom->sectors)
124      return sector;
125   return geom->sector_map[sector];
126}
127
128
129
130static floperr_t internal_basicdsk_read_sector(floppy_image *floppy, int head, int track, int sector, int sector_is_index, void *buffer, size_t buflen)
131{
132   UINT64 offset;
133   floperr_t err;
134
135   err = get_offset(floppy, head, track, sector, sector_is_index, &offset);
136   if (err)
137      return err;
138   floppy_image_read(floppy, buffer, offset, buflen);
139   return FLOPPY_ERROR_SUCCESS;
140}
141
142
143
144static floperr_t internal_basicdsk_write_sector(floppy_image *floppy, int head, int track, int sector, int sector_is_index, const void *buffer, size_t buflen, int ddam)
145{
146   UINT64 offset;
147   floperr_t err;
148
149   err = get_offset(floppy, head, track, sector, sector_is_index, &offset);
150   if (err)
151      return err;
152
153   floppy_image_write(floppy, buffer, offset, buflen);
154   return FLOPPY_ERROR_SUCCESS;
155}
156
157
158
159static floperr_t basicdsk_read_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen)
160{
161   return internal_basicdsk_read_sector(floppy, head, track, sector, FALSE, buffer, buflen);
162}
163
164static floperr_t basicdsk_write_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam)
165{
166   return internal_basicdsk_write_sector(floppy, head, track, sector, FALSE, buffer, buflen, ddam);
167}
168
169static floperr_t basicdsk_read_indexed_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen)
170{
171   return internal_basicdsk_read_sector(floppy, head, track, sector, TRUE, buffer, buflen);
172}
173
174static floperr_t basicdsk_write_indexed_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam)
175{
176   return internal_basicdsk_write_sector(floppy, head, track, sector, TRUE, buffer, buflen, ddam);
177}
178
179
180
181static floperr_t basicdsk_format_track(floppy_image *floppy, int head, int track, option_resolution *params)
182{
183   floperr_t err = FLOPPY_ERROR_SUCCESS;
184   UINT8 local_buffer[512];
185   void *alloc_buffer = NULL;
186   void *buffer;
187   UINT32 sector_length;
188   int sector;
189   const struct basicdsk_geometry *geometry;
190
191   geometry = get_geometry(floppy);
192
193   sector_length = geometry->sector_length;
194
195   if (sector_length > sizeof(local_buffer))
196   {
197      alloc_buffer = malloc(sector_length);
198      if (!alloc_buffer)
199      {
200         err = FLOPPY_ERROR_OUTOFMEMORY;
201         goto done;
202      }
203      buffer = alloc_buffer;
204   }
205   else
206   {
207      alloc_buffer = NULL;
208      buffer = local_buffer;
209   }
210
211   memset(buffer, floppy_get_filler(floppy), sector_length);
212
213   for (sector = 0; sector < geometry->sectors; sector++)
214   {
215      err = basicdsk_write_sector(floppy, head, track, sector + geometry->first_sector_id, buffer, sector_length, 0);
216      if (err)
217         goto done;
218   }
219
220done:
221   if (alloc_buffer)
222      free(alloc_buffer);
223   return err;
224}
225
226
227
228static int basicdsk_get_heads_per_disk(floppy_image *floppy)
229{
230   return get_geometry(floppy)->heads;
231}
232
233
234
235static int basicdsk_get_tracks_per_disk(floppy_image *floppy)
236{
237   return get_geometry(floppy)->tracks;
238}
239
240
241
242static floperr_t basicdsk_get_sector_length(floppy_image *floppy, int head, int track, int sector, UINT32 *sector_length)
243{
244   floperr_t err;
245
246   err = get_offset(floppy, head, track, sector, FALSE, NULL);
247   if (err)
248      return err;
249
250   if (sector_length)
251      *sector_length = get_geometry(floppy)->sector_length;
252   return FLOPPY_ERROR_SUCCESS;
253}
254
255
256
257static floperr_t basicdsk_get_indexed_sector_info(floppy_image *floppy, int head, int track, int sector_index, int *cylinder, int *side, int *sector, UINT32 *sector_length, unsigned long *flags)
258{
259   const struct basicdsk_geometry *geom = get_geometry(floppy);
260
261   if (geom->translate_sector)
262      sector_index = geom->translate_sector(floppy, sector_index);
263
264   sector_index += geom->first_sector_id;
265
266   if (cylinder)
267      *cylinder = track;
268   if (side)
269      *side = head;
270   if (sector)
271      *sector = sector_index;
272   if (flags) {
273      /* TODO: read DAM or DDAM and determine flags */
274      *flags = 0;
275      if (geom->get_ddam)
276         *flags = geom->get_ddam(floppy, geom, track, head, sector_index);
277   }
278   return basicdsk_get_sector_length(floppy, head, track, sector_index, sector_length);
279}
280
281
282
283/********************************************************************
284 * Generic Basicdsk Constructors
285 ********************************************************************/
286
287static void basicdsk_default_geometry(const struct FloppyFormat *format, struct basicdsk_geometry *geometry)
288{
289   optreserr_t err;
290   int sector_length;
291   memset(geometry, 0, sizeof(*geometry));
292
293   err = option_resolution_getdefault(format->param_guidelines, PARAM_HEADS,         &geometry->heads);
294   assert(!err);
295   err = option_resolution_getdefault(format->param_guidelines, PARAM_TRACKS,         &geometry->tracks);
296   assert(!err);
297   err = option_resolution_getdefault(format->param_guidelines, PARAM_SECTORS,         &geometry->sectors);
298   assert(!err);
299   err = option_resolution_getdefault(format->param_guidelines, PARAM_FIRST_SECTOR_ID,   &geometry->first_sector_id);
300   assert(!err);
301   err = option_resolution_getdefault(format->param_guidelines, PARAM_INTERLEAVE,      &geometry->interleave);
302   if (err!=0) {
303      geometry->interleave = 1;
304   }
305   err = option_resolution_getdefault(format->param_guidelines, PARAM_SECTOR_LENGTH,   &sector_length);
306   assert(!err);
307   geometry->sector_length = sector_length;
308
309   if (geometry->interleave > 1)
310   {
311      int sector = 0;
312
313      for (int i = 0; i < geometry->sectors; i++)
314      {
315         geometry->sector_map[i] = sector;
316
317         sector += geometry->interleave;
318
319         if (sector >= geometry->sectors)
320            sector -= geometry->sectors;
321      }
322
323      geometry->translate_sector = internal_basicdsk_translate_sector_interleave;
324   }
325}
326
327
328
329FLOPPY_CONSTRUCT(basicdsk_construct_default)
330{
331   struct basicdsk_geometry geometry;
332   basicdsk_default_geometry(format, &geometry);
333   return basicdsk_construct(floppy, &geometry);
334}
335
336
337
338FLOPPY_IDENTIFY(basicdsk_identify_default)
339{
340   UINT64 expected_size;
341   struct basicdsk_geometry geometry;
342
343   basicdsk_default_geometry(format, &geometry);
344
345   expected_size = geometry.sector_length;
346   expected_size *= geometry.heads;
347   expected_size *= geometry.tracks;
348   expected_size *= geometry.sectors;
349   *vote = (floppy_image_size(floppy) == expected_size) ? 100 : 0;
350   return FLOPPY_ERROR_SUCCESS;
351}
352
src/lib/formats/hect_tap.c
r0r11458
1/********************************************************************
2
3Support for Micronique machine .K7 and *.FOR cassette images
4
5Note that the usual type for hector cassette is *.K7,
6     the *.FOR type is only for programming screen in forth format.
7
8You can find some *.K7 file on serveral server in France
9(Micronique is a French factory) Enjoy !
10
11 jj.stacino@aliceadsl.fr
12
13Updated 3/1/10 : use real value for timing.
14********************************************************************/
15#include "hect_tap.h"
16
17
18#define SMPLO   -32768
19#define SILENCE   0
20#define SMPHI   32767
21
22
23static int cas_size;
24
25
26enum
27{
28   Header_cycles = 77, /* Valeur Th?orique 66 = 44100 * 1.5 / 1000  // mesur? sur jeu Formule1 = 1,75ms*/
29   Zero_cycles =   27, /* Valeur Th?orique 17 = 44100 * 0.4 / 1000  // mesur? sur jeu Formule1 = 0,61ms*/
30   Un_cycles =     50, /* Valeur Th?orique 40 = 44100 * 0.9 / 1000  // mesur? sur jeu Formule1 = 1,13ms*/
31};
32
33/* Here I prefer use the value that I read on a real tape, and not the theorical value; note that these
34   value work best on my HRX...    Yo_fr   (jj.stacino@aliceadsl.fr)  */
35
36/*******************************************************************
37   Generate one high-low cycle of sample data
38********************************************************************/
39INLINE int hector_tap_cycle(INT16 *buffer, int sample_pos, int high, int low)
40{
41   int i = 0;
42
43   if ( buffer )
44   {
45      while( i < high)
46      {
47         buffer[ sample_pos + i ] = SMPHI;
48         i++;
49      }
50
51      while( i < high + low )
52      {
53         buffer[ sample_pos + i ] = SMPLO;
54         i++;
55      }
56   }
57   return high + low;
58}
59
60
61INLINE int hector_tap_byte(INT16 *buffer, int sample_pos, UINT8 data)
62{
63/* Writing an entire byte */
64   int i, samples;
65
66   samples = 0;
67   for ( i = 0; i < 8; i++ )
68   {
69      if ( data & 0x01 )
70         samples += hector_tap_cycle( buffer, sample_pos + samples, Un_cycles/2, Un_cycles/2 );
71      else
72         samples += hector_tap_cycle( buffer, sample_pos + samples, Zero_cycles/2, Zero_cycles/2 );
73
74      data >>= 1;
75   }
76   return samples;
77}
78
79
80INLINE int hector_tap_synchro(INT16 *buffer, int sample_pos, int nb_synchro)
81{
82/* Writing an entire byte */
83   int i, samples;
84
85   samples = 0;
86   for ( i = 0; i < nb_synchro ; i++ )
87         samples += hector_tap_cycle( buffer, sample_pos + samples, Header_cycles/2, Header_cycles/2 );
88
89   return samples;
90}
91
92
93static int hector_handle_tap(INT16 *buffer, const UINT8 *casdata)
94{
95   int   data_pos, sample_count/*, block_count*/;
96    int previous_block=0;
97
98   data_pos = 0;
99   sample_count = 0;
100    /*block_count = 0;*/
101    previous_block = 0;
102
103   /* First 768 cycle of synchro */
104   sample_count += hector_tap_synchro( buffer, sample_count, 768-4 );
105
106   /* on the entire file*/
107    while( data_pos < cas_size )
108   {
109      UINT16   block_size;
110
111        if (previous_block == 0xFE)
112               /* Starting a block with 150 cycle of synchro to let time to Hector to do the job ! */
113              sample_count += hector_tap_synchro( buffer, sample_count, 150 );
114        else
115                  /* Starting a block with 4 cycle of synchro */
116              sample_count += hector_tap_synchro( buffer, sample_count, 4 );
117
118       if (data_pos>1)
119                previous_block = casdata[data_pos-1];
120
121      /* Handle block lenght on tape data */
122      block_size = casdata[data_pos] ;
123      if (block_size==0)
124         block_size=256;
125
126      /*block_count++;*/
127        sample_count += hector_tap_byte(buffer, sample_count, casdata[data_pos] );
128        data_pos++;
129
130      /* Data samples */
131      for ( ; block_size ; data_pos++, block_size-- )
132        {
133          /* Make sure there are enough bytes left */
134          if ( data_pos > cas_size )
135            return -1;
136
137         sample_count += hector_tap_byte( buffer, sample_count, casdata[data_pos] );
138
139        }
140    }
141    /*Finish by a zero*/
142    sample_count += hector_tap_byte( buffer, sample_count, 0 );
143
144   return sample_count;
145}
146/*******************************************************************
147////  FORTH DATA CASSETTE
148*******************************************************************/
149
150
151static int hector_handle_forth_tap(INT16 *buffer, const UINT8 *casdata)
152{
153   int   data_pos, sample_count/*, block_count*/;
154    /*int previous_block=0;*/
155
156   data_pos = 0;
157   sample_count = 0;
158    /*block_count = 0;*/
159    /*previous_block = 0;*/
160
161    /* Out if len of file not modulo 822 octets    */
162   if ( (cas_size % 822) != 0 )
163      return -1;
164
165   /* on the entire file*/
166    while( data_pos < cas_size )
167   {
168      UINT16   block_size;
169
170       /* Starting a block with 768 cycle of synchro*/
171       sample_count += hector_tap_synchro( buffer, sample_count, 768 );
172
173      /* Handle block lenght on tape data */
174      block_size = 822 ; /* Fixed size for the forth*/
175
176      /*block_count=0;*/
177
178      /* Data samples */
179      for ( ; block_size ; data_pos++, block_size-- )
180        {
181          /* Make sure there are enough bytes left */
182          if ( data_pos > cas_size )
183            return -1;
184
185         sample_count += hector_tap_byte( buffer, sample_count, casdata[data_pos] );
186
187        }
188    }
189
190    /*Finish by a zero*/
191    sample_count += hector_tap_byte( buffer, sample_count, 0 );
192
193   return sample_count;
194}
195/*******************************************************************
196/////  END FORTH DATA CASSETTE
197*******************************************************************/
198
199
200/*******************************************************************
201   Generate samples for the tape image
202********************************************************************/
203static int hector_tap_fill_wave(INT16 *buffer, int sample_count, UINT8 *bytes)
204{
205   return hector_handle_tap( buffer, bytes );
206}
207
208
209/*******************************************************************
210   Calculate the number of samples needed for this tape image  FORTH
211********************************************************************/
212static int hector_tap_forth_to_wav_size(const UINT8 *casdata, int caslen)
213{
214   cas_size = caslen ;
215
216   return hector_handle_forth_tap( NULL, casdata );
217}
218
219/*******************************************************************
220   Generate samples for the tape image FORTH
221********************************************************************/
222static int hector_tap_forth_fill_wave(INT16 *buffer, int sample_count, UINT8 *bytes)
223{
224   return hector_handle_forth_tap( buffer, bytes ); //forth removed here !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
225}
226
227
228/*******************************************************************
229   Calculate the number of samples needed for this tape image classical
230********************************************************************/
231static int hector_tap_to_wav_size(const UINT8 *casdata, int caslen)
232{
233   cas_size = caslen ;
234
235   return hector_handle_tap( NULL, casdata );//forth removed here !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
236}
237
238
239static const struct CassetteLegacyWaveFiller hector_legacy_fill_wave =
240{
241   hector_tap_fill_wave,               /* fill_wave */
242   -1,                              /* chunk_size */
243   0,                              /* chunk_samples */
244   hector_tap_to_wav_size,                /* chunk_sample_calc */
245   44100,                           /* sample_frequency */
246   0,                              /* header_samples */
247   0                              /* trailer_samples */
248};
249
250static const struct CassetteLegacyWaveFiller hector_forth_legacy_fill_wave =
251{
252   hector_tap_forth_fill_wave,            /* fill_wave */
253   -1,                              /* chunk_size */
254   0,                              /* chunk_samples */
255   hector_tap_forth_to_wav_size,          /* chunk_sample_calc */
256   44100,                           /* sample_frequency */
257   0,                              /* header_samples */
258   0                              /* trailer_samples */
259};
260
261
262static casserr_t hector_k7_identify(cassette_image *cassette, struct CassetteOptions *opts)
263{
264   return cassette_legacy_identify(cassette, opts, &hector_legacy_fill_wave);
265}
266
267
268static casserr_t hector_k7_load(cassette_image *cassette)
269{
270   return cassette_legacy_construct(cassette, &hector_legacy_fill_wave);
271}
272
273static casserr_t hector_k7forth_identify(cassette_image *cassette, struct CassetteOptions *opts)
274{
275   return cassette_legacy_identify(cassette, opts, &hector_forth_legacy_fill_wave);
276}
277
278
279static casserr_t hector_k7forth_load(cassette_image *cassette)
280{
281   return cassette_legacy_construct(cassette, &hector_forth_legacy_fill_wave);
282}
283
284
285static const struct CassetteFormat hector_k7_format =
286{
287   "k7,cin",
288   hector_k7_identify,
289   hector_k7_load,
290   NULL
291};
292
293static const struct CassetteFormat hector_k7Forth_format =
294{
295   "for",
296   hector_k7forth_identify,
297   hector_k7forth_load,
298   NULL
299};
300
301
302CASSETTE_FORMATLIST_START(hector_cassette_formats)
303   CASSETTE_FORMAT(hector_k7_format)
304   CASSETTE_FORMAT(hector_k7Forth_format)
305CASSETTE_FORMATLIST_END
src/lib/formats/basicdsk.h
r0r11458
1/*********************************************************************
2
3    formats/basicdsk.h
4
5    Floppy format code for basic disks
6
7*********************************************************************/
8
9#ifndef BASICDSK_H
10#define BASICDSK_H
11
12#include "flopimg.h"
13
14struct basicdsk_geometry
15{
16   int heads;
17   int tracks;
18   int sectors;
19   int first_sector_id;
20   int interleave;
21   int sector_map[256];
22   UINT32 sector_length;
23   UINT64 offset;
24
25   int (*translate_sector)(floppy_image *floppy, int sector);
26   UINT64 (*translate_offset)(floppy_image *floppy, const struct basicdsk_geometry *geom, int track, int head, int sector);
27   UINT64 (*get_ddam)(floppy_image *floppy, const struct basicdsk_geometry *geom, int track, int head, int sector);
28};
29
30floperr_t basicdsk_construct(floppy_image *floppy, const struct basicdsk_geometry *geometry);
31
32FLOPPY_IDENTIFY(basicdsk_identify_default);
33FLOPPY_CONSTRUCT(basicdsk_construct_default);
34
35#endif /* BASICDSK_H */
src/lib/formats/hect_tap.h
r0r11458
1/*********************************************************************
2
3    hect_tap.h
4
5    Format code for Micronique cassette files
6
7*********************************************************************/
8
9#ifndef HECT_TAP_H
10#define HECT_TAP_H
11
12#include "cassimg.h"
13
14CASSETTE_FORMATLIST_EXTERN(hector_cassette_formats);
15
16#endif /* HECT_TAP_H */
17
src/lib/formats/thom_cas.c
r0r11458
1/**********************************************************************
2
3  Copyright (C) Antoine Mine' 2006
4
5  Thomson 8-bit computers
6
7**********************************************************************/
8
9#include <math.h>
10
11#include "pool.h"
12#include "cassimg.h"
13#include "thom_cas.h"
14
15
16/***************************** configuration **************************/
17
18#define K7_SPEED_HACK 0
19/* When set to 1, bits extracted from k7 files are not transformed into a
20   raw wave. It saves some time & memory but disables the ability to "hear" the
21   cassette.
22   This does not affect raw wave files.
23   Also, we do not do this for k5 files because the the corresponding wave
24   files are more compact anyway. */
25/* It must be set accordingly in machine/thomson.c */
26
27#define VERBOSE 0 /* 0, 1 or 2 */
28
29
30/***************************** utilities **************************/
31
32#define PRINT(x) printf x
33
34#define LOG(x)   do { if (VERBOSE > 0) printf x; } while (0)
35#define VLOG(x)   do { if (VERBOSE > 1) printf x; } while (0)
36
37
38#define TO7_BIT_LENGTH 0.001114
39
40#define TO7_FREQ_CASS_0 4500.
41#define TO7_FREQ_CASS_1 6300.
42
43#define TO7_PERIOD_CASS_0 ( 1. / TO7_FREQ_CASS_0 )
44#define TO7_PERIOD_CASS_1 ( 1. / TO7_FREQ_CASS_1 )
45
46static UINT32 to7_k7_bitsize;
47static UINT8* to7_k7_bits;
48
49#define MO5_BIT_LENGTH   0.000833
50#define MO5_HBIT_LENGTH (MO5_BIT_LENGTH / 2.)
51
52/************************* k7 format *************************/
53
54/* The k7 format is widespread among existing Thomson emulators.
55   It represents the stream of bytes returned by the BIOS cassette routine,
56   and so, is used by emulators that bypass the BIOS routine.
57   It is generally hacked from the raw image more or less by hand
58   (in particular, to by-pass special loaders and copy-protection schemes).
59
60   As we run the original BIOS routine, we need to hack back the k7 file
61   into the original stream.
62
63   In theory, the byte stream is a sequence of blocks of the form:
64   - 2-byte header: 01 3C
65   - 1-byte type
66   00 = start of file, the block contains the filename
67   01 = chunk of file data
68   FF = end of file
69   - 1-byte size
70   - size bytes of data
71   - one byte of CRC
72
73   These are separated by 0xff fillers.
74
75   There are also a variety of non-standard blocks, and we try to handle them.
76
77   Our main problem is to guess where the places where the motor will be
78   cut-off and introduce there sequences of 1 bit for synchronization.
79
80   Note: as a nice effect, if our routine succeeds, it constructs a wave
81   file that should be usable on a real computer.
82*/
83
84
85
86static const struct CassetteModulation to7_k7_modulation =
87{
88   CASSETTE_MODULATION_SQUAREWAVE,
89   4000.0,  4500.0, 5000.0,
90   5500.0,  6300.0, 7500.0
91};
92
93
94
95static casserr_t to7_k7_identify ( cassette_image *cass, struct CassetteOptions *opts )
96{
97   casserr_t e = cassette_modulation_identify( cass, &to7_k7_modulation, opts );
98   return e;
99}
100
101
102
103static casserr_t to7_k7_load( cassette_image *cass )
104{
105#if ! K7_SPEED_HACK
106   static const INT8 square_wave[] = { -128, 127 };
107   double time = 0.;
108#endif
109   size_t size = cassette_image_size( cass ), pos = 0;
110   int i, sz, sz2, bitmax = 1024, invalid = 0;
111   UINT8 in, typ, block[264];
112
113   LOG (( "to7_k7_load: start conversion, size=%li\n", (long)size ));
114   PRINT (( "to7_k7_load: open cassette, length: %li bytes\n", (long) size ));
115
116   if ( to7_k7_bits )
117   {
118      free( to7_k7_bits );
119      to7_k7_bits = NULL;
120   }
121
122   to7_k7_bitsize = 0;
123   to7_k7_bits = (UINT8*)malloc(bitmax );
124
125/* store one period */
126#if K7_SPEED_HACK
127#define K7_PUT( PERIOD )
128#else
129#define K7_PUT( PERIOD ) \
130   do                        \
131   {                        \
132      casserr_t err;                  \
133      err = cassette_put_samples( cass, 0, time, (PERIOD), 2, 1, \
134                   square_wave, CASSETTE_WAVEFORM_8BIT ); \
135      if ( err )                  \
136         return err;               \
137      time += (PERIOD);               \
138   } while (0)
139#endif
140
141/* store one bit */
142#define K7_PUT_BIT( BIT ) \
143   do                        \
144   {                        \
145      int b;                     \
146      if ( BIT )                  \
147      {                     \
148         for ( b = 0; b < 7; b++ )         \
149            K7_PUT( TO7_PERIOD_CASS_1 );      \
150      }                     \
151      else                     \
152      {                     \
153         for ( b = 0; b < 5; b++ )         \
154            K7_PUT( TO7_PERIOD_CASS_0 );      \
155      }                     \
156      if ( to7_k7_bitsize + 1 >= bitmax )         \
157      {                     \
158         UINT8* a = (UINT8*)malloc(bitmax * 2);      \
159         memcpy ( a, to7_k7_bits, bitmax );      \
160         bitmax *= 2;               \
161         to7_k7_bits = a;            \
162      }                     \
163      to7_k7_bits[ to7_k7_bitsize++ ] = (BIT);      \
164   } while (0)
165
166/* store one byte, with start / stop bits */
167#define K7_PUT_BYTE( BYTE ) \
168   do                     \
169   {                     \
170      UINT8 x;               \
171      K7_PUT_BIT( 0 );            \
172      for ( x = 0; x < 8; x++ )         \
173         K7_PUT_BIT( ( (BYTE) >> x ) & 1 );   \
174      K7_PUT_BIT( 1 );            \
175      K7_PUT_BIT( 1 );            \
176   } while (0)
177
178#define K7_FILL_1( SIZE ) \
179   do                        \
180   {                        \
181      if ( (SIZE) > 0 ) {               \
182         int ii;                  \
183         LOG (( "to7_k7_load: 1-filler size=%i bitstart=%i\n", \
184                (SIZE), to7_k7_bitsize ));      \
185         for ( ii = 0; ii < (SIZE); ii++ ) K7_PUT_BIT( 1 ); \
186      }                     \
187   } while (0)
188
189#define K7_FILL_ff( SIZE ) \
190   do                        \
191   {                        \
192      if ( (SIZE) > 0 )               \
193      {                     \
194         int ii;                  \
195         LOG (( "to7_k7_load: 0xff filler size=%i bitstart=%i\n",  (SIZE), to7_k7_bitsize )); \
196         for ( ii = 0; ii < (SIZE); ii++ )      \
197            K7_PUT_BYTE( 0xff );         \
198      }                     \
199   } while (0)
200
201   /* check format */
202   cassette_image_read( cass, block, 0, 64 );
203   for ( i = 3; ; i++ )
204   {
205      if ( ( i >= size ) || ( i >= 64 ) )
206      {
207         /* ? */
208         PRINT (( "to7_k7_load: WARNING: this does not look like a MO or TO cassette.\n" ));
209         break;
210      }
211      else if ( ( block[i-3] == 0x01 ) && ( block[i-2] == 0x3c ) && ( block[i-1] == 0x5a ) && ! block[i] )
212      {
213         /* MO */
214         PRINT (( "to7_k7_load: WARNING: this looks like a MO cassette, not a TO one.\n" ));
215         break;
216      }
217      else if ( ( block[i-3] == 0xff ) && ( block[i-2] == 0x01 ) && ( block[i-1] == 0x3c ) && ! block[i] )
218      {
219         /* TO */
220         break;
221      }
222   }
223
224   /* skip to first 0xff filler */
225   for ( sz = 0; pos < size; pos++, sz++ )
226   {
227      cassette_image_read( cass, &in, pos, 1 );
228      if ( in == 0xff )
229         break;
230   }
231   if ( sz > 0 )
232      LOG (( "to7_k7_load: skip %i trash bytes\n", sz ));
233
234   /* loop over regular blocks */
235   while ( pos < size )
236   {
237
238   rebounce:
239      /* skip 0xff filler */
240      for ( sz = 0; pos < size; pos++, sz++ )
241      {
242         cassette_image_read( cass, &in, pos, 1 );
243         /* actually, we are bit laxist and treat as 0xff bytes with at least
244               5 bits out of 8 set to 1
245            */
246         for ( i = 0; in; in >>= 1 )
247            i += (in & 1);
248         if ( i < 5 )
249            break;
250      }
251
252      /* get block header */
253      if ( pos + 4 > size )
254      {
255         pos -= sz;
256         break;
257      }
258      cassette_image_read( cass, block, pos, 4 );
259      typ = block[2];
260      sz2 = block[3]+1;
261      if ( block[0] != 0x01 || block[1] != 0x3c || ( typ != 0x00 && typ != 0x01 && typ !=  0xff ) )
262      {
263         pos -= sz;
264         break;
265      }
266      pos += 4;
267
268      /* get block */
269      cassette_image_read( cass, block+4, pos, sz2 );
270      pos += sz2;
271
272      /* 1-filler and 0xff-filler */
273      if ( typ == 0 || typ == 0xff )
274         K7_FILL_1( 1000 );
275      K7_FILL_ff( sz );
276
277      /* put block */
278      LOG (( "to7_k7_load: block off=$%x type=$%02X size=%i bitstart=%i\n", (int) pos-sz2-4, typ, sz2, to7_k7_bitsize ));
279      VLOG (( "to7_k7_load: data:" ));
280      for ( i = 0; i < sz2 + 4; i ++)
281      {
282         VLOG (( " $%02X", block[i] ));
283         K7_PUT_BYTE( block[i] );
284      }
285      VLOG (( "\n" ));
286
287      /* if it is a directory enty, says so to the user */
288      if ( typ == 0 )
289      {
290         char name[] = "01234567.ABC";
291         UINT8 t = block[15];
292         UINT8 u = block[16];
293         int p = (to7_k7_bitsize - sz2 - 4 - sz) * TO7_BIT_LENGTH;
294         memcpy( name, block+4, 8 );
295         memcpy( name+9, block+12, 3 );
296         for ( i = 0; name[i]; i++)
297         {
298            if ( name[i] < ' ' || name[i] >= 127 )
299               name[i] = '?';
300         }
301         PRINT (( "to7_k7_load: file \"%s\" type=%s,%s at %imn %is\n",
302             name,
303             (t==0) ? "bas" : (t==1) ? "dat" : (t==2) ? "bin" : "???",
304             (u == 0) ? "a" : (u == 0xff) ? "b" : "?",
305             p / 60, p % 60 ));
306      }
307
308      /* extra 1-fillers */
309      if ( typ == 0 || typ == 0xff )
310         K7_FILL_1( 1000 );
311   }
312
313   /* trailing data with invalid block structure
314       => dump it in a raw form, but stay alert for hidden block starts
315    */
316   if ( pos < size )
317   {
318      invalid++;
319      LOG (( "to7_k7_load: trailing raw bytes off=$%x bitstart=%i\n", (int) pos, to7_k7_bitsize ));
320
321      /* put block */
322      for (; pos < size; pos++ )
323      {
324         cassette_image_read( cass, &in, pos, 1 );
325         for ( sz = 0; pos < size && in == 0xff; sz++ )
326         {
327            pos++;
328            cassette_image_read( cass, &in, pos, 1 );
329         }
330         if ( invalid < 10 && sz > 4 && in == 0x01 && pos + 4 <= size )
331         {
332            UINT8 in1,in2;
333            cassette_image_read( cass, &in1,   pos+1, 1 );
334            cassette_image_read( cass, &in2, pos+2, 1 );
335            if ( (in1 == 0x3c) && ((in2 == 0x00) || (in2 == 0x01) ) )
336            {
337               /* seems we have a regular block hidden in raw data => rebounce */
338               LOG (( "to7_k7_load: hidden regular block found\n" ));
339               pos -= sz;
340               goto rebounce;
341            }
342            if ( ( ( in1 == 0x3d ) && ( in2 == 0 ) ) || ( ( in1 == 0x57 ) && ( in2 == 0x49 ) ) )
343            {
344               /* special block (Infogrames) => just prepend filler */
345               K7_FILL_1 ( 500 );
346               LOG (( "to7_k7_load: special $%02X $%02X $%02X block found off=$%x bitstart=%i\n", in, in1, in2, (int) pos, to7_k7_bitsize ));
347            }
348         }
349         for ( i = 0; i < sz; i++ )
350            K7_PUT_BYTE( 0xff );
351         K7_PUT_BYTE( in );
352      }
353   }
354
355   if ( invalid )
356      PRINT (( "to7_k7_load: WARNING: the k7 has an unknown structure and may not work\n" ));
357
358   sz = to7_k7_bitsize * TO7_BIT_LENGTH;
359   PRINT (( "to7_k7_load: cassette length: %imn %is (%i samples)\n", sz / 60, sz % 60, to7_k7_bitsize ));
360
361   return CASSETTE_ERROR_SUCCESS;
362}
363
364
365
366static const struct CassetteFormat to7_k7 =
367{ "k7", to7_k7_identify, to7_k7_load, NULL /* no save */ };
368
369
370
371/********************* TO7 WAV format ************************/
372
373
374
375static casserr_t to7_wav_identify ( cassette_image *cass,
376                struct CassetteOptions *opts )
377{
378   casserr_t e = wavfile_format.identify( cass, opts );
379   return e;
380}
381
382
383
384static casserr_t to7_wav_load ( cassette_image *cass )
385{
386   casserr_t e = wavfile_format.load( cass );
387   struct CassetteInfo info;
388   double len;
389
390   if ( to7_k7_bits )
391   {
392      free( to7_k7_bits );
393      to7_k7_bits = NULL;
394   }
395
396   if ( e != CASSETTE_ERROR_SUCCESS )
397      return e;
398
399   cassette_get_info( cass, &info );
400
401   len = (double) info.sample_count / info.sample_frequency;
402
403   PRINT (( "to7_wav_load: loading cassette, length %imn %is, %i Hz, %i bps, %i bits\n",
404       (int) len / 60, (int) len % 60,
405       info.sample_frequency, info.bits_per_sample,
406       (int) (len / TO7_BIT_LENGTH) ));
407
408   return CASSETTE_ERROR_SUCCESS;
409}
410
411
412
413static casserr_t to7_wav_save ( cassette_image *cass, const struct CassetteInfo *info )
414{
415   int len = info->sample_count / info->sample_frequency;
416   PRINT (( "to7_wav_save: saving cassette, length %imn %is, %i Hz, %i bps\n", len / 60, len % 60, info->sample_frequency, info->bits_per_sample ));
417   return wavfile_format.save( cass, info );
418}
419
420
421
422/* overloaded wav: dump info */
423static const struct CassetteFormat to7_wav =
424{ "wav", to7_wav_identify, to7_wav_load, to7_wav_save };
425
426
427
428/************************* k5 format *************************/
429
430
431
432static casserr_t mo5_k5_identify ( cassette_image *cass,
433               struct CassetteOptions *opts )
434{
435   opts -> bits_per_sample = 8;
436   opts -> channels = 1;
437   opts -> sample_frequency = 22100;//11050;
438   return CASSETTE_ERROR_SUCCESS;
439}
440
441
442
443/* As the k7 format, the k5 format represents the stream of bytes returned by
444   the BIOS cassette routine and need to be hacked back into the original
445   cassette wave.
446
447   The format of blocks is a bit different from that of TO7 cassettes:
448   - 2-byte header is: 3C 5A
449   - 1-byte type
450   00 = start of file, the block contains the filename
451   01 = chunk of file data
452   FF = end of file
453   - 1-byte size
454   - size - 1 bytes of data (255 bytes of data if size = 0)
455   - one byte of CRC: sum from (and including) header up to (and including)
456   CRC byte should be 0 modulo 256
457
458   They are separated by 0x01 filler bytes (generally 10 of them),
459   for synchronisation. There are also extra 0 filler bits between certain
460   kinds of blocks, when the motor is supposed to go off and back on.
461*/
462
463static casserr_t mo5_k5_load( cassette_image *cass )
464{
465   size_t size = cassette_image_size( cass ), pos = 0;
466   int i, sz, sz2, hbit = 0;
467   UINT8 in, in2, in3, typ, block[264], sum;
468   int invalid = 0, hbitsize = 0, dcmoto = 0;
469
470   LOG (( "mo5_k5_load: start conversion, size=%li\n", (long)size ));
471   PRINT (( "mo5_k5_load: open cassette, length: %li bytes\n", (long) size ));
472
473   /* store a half-bit */
474#define K5_PUT_HBIT                     \
475   do                        \
476   {                        \
477      cassette_put_sample ( cass, 0, hbitsize * MO5_HBIT_LENGTH, MO5_HBIT_LENGTH, (hbit ? 1 : -1) << 30 ); \
478      hbitsize++;                  \
479   } while ( 0 )
480
481   /* store one bit */
482#define K5_PUT_BIT( BIT )         \
483   do               \
484   {               \
485      if ( BIT )         \
486      {            \
487         K5_PUT_HBIT;      \
488         hbit = !hbit;      \
489         K5_PUT_HBIT;      \
490      }            \
491      else            \
492      {            \
493         K5_PUT_HBIT;      \
494         K5_PUT_HBIT;      \
495      }            \
496      hbit = !hbit;         \
497   } while (0)
498
499   /* store one byte, no start / stop bit, converse order from TO7 */
500#define K5_PUT_BYTE( BYTE ) \
501   do                     \
502   {                     \
503      UINT8 b = BYTE;               \
504      int x;                  \
505      for ( x = 0; x < 8; x++ )         \
506         K5_PUT_BIT( (b >> (7 - x)) & 1 );   \
507   } while (0)
508
509   /* store filler */
510#define K5_FILL_0( SIZE ) \
511   do                        \
512   {                        \
513      if ( (SIZE) > 0 )               \
514      {                     \
515         int j;                  \
516         LOG (( "mo5_k5_load: 0-filler size=%i hbitstart=%i\n", (SIZE), hbitsize )); \
517         for ( j = 0; j < (SIZE); j++ )         \
518            K5_PUT_BIT( 0 );         \
519      }                     \
520   } while (0)
521
522#define K5_FILL_01( SIZE )                  \
523   do                        \
524   {                        \
525      if ( (SIZE) > 0 )               \
526      {                     \
527         int j;                  \
528         LOG (( "mo5_k5_load: 0x01 filler size=%i bitstart=%i\n", (SIZE), hbitsize )); \
529         for ( j = 0; j < (SIZE); j++ )         \
530            K5_PUT_BYTE( 0x01 );         \
531      }                     \
532   } while (0)
533
534   /* check format */
535   cassette_image_read( cass, block, 0, 64 );
536   for ( i = 3; ; i++ )
537   {
538      if ( ( i >= size ) || ( i >= 64 ) )
539      {
540         /* ? */
541         PRINT (( "to5_k5_load: WARNING: this does not look like a MO or TO cassette.\n" ));
542         break;
543      }
544      else if ( ( block[i-3] == 0x01 ) && ( block[i-2] == 0x3c ) && ( block[i-1] == 0x5a ) && ! block[i] )
545      {
546         /* MO */
547         break;
548      }
549      else if ( ( block[i-3] == 0xff ) && ( block[i-2] == 0x01 ) && ( block[i-1] == 0x3c ) && ! block[i] )
550      {
551         /* TO */
552         PRINT (( "to5_k5_load: WARNING: this looks like a TO cassette, not a MO one.\n" ));
553         break;
554      }
555   }
556
557   cassette_image_read( cass, block, pos, 6 );
558   if ( ! memcmp( block, "DCMOTO", 6 ) || ! memcmp( block, "DCMO5", 5 ) || ! memcmp( block, "DCMO6", 5 ) )
559      dcmoto = 1;
560
561   /* loop over regular blocks */
562   while ( pos < size )
563   {
564
565   rebounce:
566      /* skip DCMOTO header*/
567      if ( dcmoto )
568      {
569         cassette_image_read( cass, block, pos, 6 );
570         if ( ! memcmp( block, "DCMOTO", 6 ) )
571         {
572            LOG (( "mo5_k5_load: DCMOTO signature found at off=$%x\n", (int)pos ));
573            pos += 6;
574         }
575         else if ( ! memcmp( block, "DCMO", 4 ) )
576         {
577            LOG (( "mo5_k5_load: DCMO* signature found at off=$%x\n", (int)pos ));
578            pos += 5;
579         }
580      }
581
582      /* skip 0x01 filler */
583      for ( sz = 0; pos < size; pos++, sz++ )
584      {
585         cassette_image_read( cass, &in, pos, 1 );
586         if ( in != 0x01 )
587            break;
588      }
589
590      /* get block header */
591      if ( pos + 4 > size )
592      {
593         pos -= sz;
594         break;
595      }
596      cassette_image_read( cass, block, pos, 4 );
597      typ = block[2];
598      sz2 = (UINT8) (block[3]-1);
599      if ( block[0] != 0x3c || block[1] != 0x5a || ( typ != 0x00 && typ != 0x01 && typ !=  0xff ) || pos+sz2 > size )
600      {
601         pos -= sz;
602         break;
603      }
604      pos += 4;
605
606      /* get block */
607      cassette_image_read( cass, block+4, pos, sz2 );
608      pos += sz2;
609
610      /* 0-fillers and 0x01-fillers */
611      if ( typ == 0 || typ == 0xff )
612         K5_FILL_0( 1200 );
613      else
614         K5_FILL_0( 300 ); /* for MO6 */
615      K5_FILL_01( sz < 10 ? 10 : sz );
616
617      /* put block */
618      LOG (( "mo5_k5_load: block off=$%x type=$%02X size=%i hbitstart=%i\n", (int) pos-sz2-4, typ, sz2, hbitsize ));
619      VLOG (( "mo5_k5_load: data:" ));
620      for ( i = 0; i < sz2 + 4; i ++)
621      {
622         VLOG (( " $%02X", block[i] ));
623         K5_PUT_BYTE( block[i] );
624      }
625      VLOG (( "\n" ));
626
627      /* checksum */
628      for ( i = 0, sum = 0; i < sz2; i++ )
629         sum += block[i+4];
630      if ( sum )
631         LOG(( "mo5_k5_load: invalid checksum $%02X (should be 0)\n", sum ));
632
633      /* if it is a directory enty, says so to the user */
634      if ( typ == 0 )
635      {
636         char name[] = "01234567.ABC";
637         UINT8 t = block[15];
638         UINT8 u = block[16];
639         int p = (hbitsize - sz2 - 4 - sz) * MO5_HBIT_LENGTH;
640         memcpy( name, block+4, 8 );
641         memcpy( name+9, block+12, 3 );
642         for ( i = 0; name[i]; i++)
643         {
644            if ( name[i] < ' ' || name[i] >= 127 )
645               name[i] = '?';
646         }
647         PRINT (( "mo5_k5_load: file \"%s\" type=%s,%s at %imn %is\n",
648             name,
649             (t==0) ? "bas" : (t==1) ? "dat" : (t==2) ? "bin" : "???",
650             (u == 0) ? "a" : (u == 0xff) ? "b" : "?",
651             p / 60, p % 60 ));
652      }
653
654      /* 0-fillers */
655      if ( typ == 0xff || typ == 0x00 )
656         K5_FILL_0( 1800 );
657   }
658
659   /* dump trailing bytes, but also look for beginings of blocks */
660   if ( pos < size )
661   {
662      invalid++;
663      K5_FILL_0( 1200 );
664      LOG (( "mo5_k5_load: trailing trash off=$%x size=%i hbitstart=%i\n", (int) pos, (int) (size-pos), hbitsize ));
665      for ( ; pos < size; pos++ )
666      {
667         cassette_image_read( cass, &in, pos, 1 );
668         if ( dcmoto && in=='D' )
669         {
670            /* skip DCMOTO header*/
671            cassette_image_read( cass, block, pos, 6 );
672            if ( ! memcmp( block, "DCMOTO", 6 ) )
673            {
674               LOG (( "mo5_k5_load: DCMOTO signature found at off=$%x\n", (int)pos ));
675               pos += 6;
676               cassette_image_read( cass, &in, pos, 1 );
677            }
678            else if ( ! memcmp( block, "DCMO", 4 ) )
679            {
680               LOG (( "mo5_k5_load: DCMO* signature found at off=$%x\n", (int)pos ));
681               pos += 5;
682               cassette_image_read( cass, &in, pos, 1 );
683            }
684         }
685         for ( sz = 0; pos < size && in == 0x01; sz++ )
686         {
687            pos++;
688            cassette_image_read( cass, &in, pos, 1 );
689         }
690         if ( sz > 6 )
691         {
692            cassette_image_read( cass, &in2, pos+1, 1 );
693            cassette_image_read( cass, &in3, pos+2, 1 );
694            if ( invalid < 10 &&  in == 0x3c && in2 == 0x5a && (in3 == 0x00 || in3 == 0x01 || in3 == 0xff ) )
695            {
696               /* regular block found */
697               LOG (( "mo5_k5_load: hidden regular block found\n" ));
698               pos -= sz;
699               goto rebounce;
700            }
701            if ( invalid < 10 && sz > 6 && ( (in == 0x3c && in2 == 0x5a) || (in == 0xc3 && in2 == 0x5a) || (in == 0xc3 && in2 == 0x3c) || (in == 0x87 && in2 == 0x4a)  ) )
702            {
703               /* special block found */
704               K5_FILL_0( 1200 );
705               LOG (( "mo5_k5_load: special block $%02X $%02X found off=$%x hbitstart=%i\n", in, in2, (int) pos-sz, hbitsize ));
706            }
707         }
708
709         VLOG (( "mo5_k5_load: special data:" ));
710         for ( i = 0; i < sz; i++ )
711         {
712            K5_PUT_BYTE( 0x01 );
713            VLOG (( " $01" ));
714         }
715         K5_PUT_BYTE( in );
716         VLOG (( " $%02X\n", in ));
717      }
718   }
719
720   if ( invalid )
721      PRINT (( "mo5_k5_load: WARNING: the k5 has an unknown structure and may not work\n" ));
722
723   sz = hbitsize * MO5_HBIT_LENGTH;
724   PRINT (( "mo5_k5_load: cassette length: %imn %is (%i half-bits)\n", sz / 60, sz % 60, hbitsize ));
725
726   return CASSETTE_ERROR_SUCCESS;
727}
728
729
730
731static const struct CassetteFormat mo5_k5 =
732{ "k5,k7", mo5_k5_identify, mo5_k5_load, NULL /* no save */ };
733
734
735/********************* MO5 WAV format ************************/
736
737
738
739static casserr_t mo5_wav_identify ( cassette_image *cass,
740              struct CassetteOptions *opts )
741{
742   casserr_t e = wavfile_format.identify( cass, opts );
743   return e;
744}
745
746
747
748static casserr_t mo5_wav_load ( cassette_image *cass )
749{
750   casserr_t e = wavfile_format.load( cass );
751   struct CassetteInfo info;
752   int len;
753   if ( e == CASSETTE_ERROR_SUCCESS )
754   {
755      cassette_get_info( cass, &info );
756      len = info.sample_count / info.sample_frequency;
757      PRINT (( "mo5_wav_load: loading cassette, length %imn %is, %i Hz, %i bps\n", len / 60, len % 60, info.sample_frequency, info.bits_per_sample ));
758   }
759   return e;
760}
761
762
763
764static casserr_t mo5_wav_save ( cassette_image *cass, const struct CassetteInfo *info )
765{
766   int len = info->sample_count / info->sample_frequency;
767   PRINT (( "mo5_wav_save: saving cassette, length %imn %is, %i Hz, %i bps\n", len / 60, len % 60, info->sample_frequency, info->bits_per_sample ));
768   return wavfile_format.save( cass, info );
769}
770
771
772
773/* overloaded wav: dump info */
774static const struct CassetteFormat mo5_wav =
775{ "wav", mo5_wav_identify, mo5_wav_load, mo5_wav_save };
776
777
778
779/********************* formats ************************/
780
781
782const struct CassetteFormat *const to7_cassette_formats[] =
783{ &to7_wav, &to7_k7, NULL };
784
785
786const struct CassetteFormat *const mo5_cassette_formats[] =
787{ &mo5_wav, &mo5_k5, NULL };
src/lib/lib.mak
r11457r11458
1616OBJDIRS += \
1717   $(LIBOBJ)/util \
1818   $(LIBOBJ)/expat \
19   $(LIBOBJ)/formats \
1920   $(LIBOBJ)/zlib \
2021   $(LIBOBJ)/softfloat \
2122
r11457r11458
3738   $(LIBOBJ)/util/coreutil.o \
3839   $(LIBOBJ)/util/harddisk.o \
3940   $(LIBOBJ)/util/huffman.o \
40   $(LIBOBJ)/util/imageutl.o \
4141   $(LIBOBJ)/util/jedparse.o \
4242   $(LIBOBJ)/util/md5.o \
4343   $(LIBOBJ)/util/opresolv.o \
r11457r11458
7575
7676
7777#-------------------------------------------------
78# formats library objects
79#-------------------------------------------------
80
81EXPATOBJS = \
82   $(LIBOBJ)/formats/cassimg.o    \
83   $(LIBOBJ)/formats/flopimg.o      \
84   $(LIBOBJ)/formats/imageutl.o   \
85   $(LIBOBJ)/formats/ioprocs.o      \
86   $(LIBOBJ)/formats/basicdsk.o   \
87   $(LIBOBJ)/formats/a26_cas.o      \
88   $(LIBOBJ)/formats/ace_tap.o      \
89   $(LIBOBJ)/formats/ami_dsk.o      \
90   $(LIBOBJ)/formats/ap2_dsk.o      \
91   $(LIBOBJ)/formats/apf_apt.o      \
92   $(LIBOBJ)/formats/apridisk.o   \
93   $(LIBOBJ)/formats/ap_dsk35.o   \
94   $(LIBOBJ)/formats/atarist_dsk.o   \
95   $(LIBOBJ)/formats/atom_tap.o   \
96   $(LIBOBJ)/formats/cbm_tap.o      \
97   $(LIBOBJ)/formats/cgen_cas.o   \
98   $(LIBOBJ)/formats/coco_cas.o   \
99   $(LIBOBJ)/formats/coco_dsk.o   \
100   $(LIBOBJ)/formats/comx35_dsk.o   \
101   $(LIBOBJ)/formats/coupedsk.o   \
102   $(LIBOBJ)/formats/cpis_dsk.o   \
103   $(LIBOBJ)/formats/cqm_dsk.o      \
104   $(LIBOBJ)/formats/csw_cas.o      \
105   $(LIBOBJ)/formats/d64_dsk.o      \
106   $(LIBOBJ)/formats/d81_dsk.o      \
107   $(LIBOBJ)/formats/d88_dsk.o      \
108   $(LIBOBJ)/formats/dim_dsk.o      \
109   $(LIBOBJ)/formats/dsk_dsk.o      \
110   $(LIBOBJ)/formats/fdi_dsk.o      \
111   $(LIBOBJ)/formats/fm7_cas.o      \
112   $(LIBOBJ)/formats/fmsx_cas.o   \
113   $(LIBOBJ)/formats/g64_dsk.o      \
114   $(LIBOBJ)/formats/gtp_cas.o      \
115   $(LIBOBJ)/formats/hect_tap.o   \
116   $(LIBOBJ)/formats/imd_dsk.o      \
117   $(LIBOBJ)/formats/kim1_cas.o   \
118   $(LIBOBJ)/formats/lviv_lvt.o   \
119   $(LIBOBJ)/formats/msx_dsk.o      \
120   $(LIBOBJ)/formats/mz_cas.o      \
121   $(LIBOBJ)/formats/nes_dsk.o      \
122   $(LIBOBJ)/formats/orao_cas.o   \
123   $(LIBOBJ)/formats/oric_dsk.o   \
124   $(LIBOBJ)/formats/oric_tap.o   \
125   $(LIBOBJ)/formats/p6001_cas.o   \
126   $(LIBOBJ)/formats/pc_dsk.o      \
127   $(LIBOBJ)/formats/pmd_pmd.o      \
128   $(LIBOBJ)/formats/primoptp.o   \
129   $(LIBOBJ)/formats/rk_cas.o      \
130   $(LIBOBJ)/formats/smx_dsk.o      \
131   $(LIBOBJ)/formats/sorc_dsk.o   \
132   $(LIBOBJ)/formats/sord_cas.o   \
133   $(LIBOBJ)/formats/svi_cas.o      \
134   $(LIBOBJ)/formats/svi_dsk.o      \
135   $(LIBOBJ)/formats/td0_dsk.o      \
136   $(LIBOBJ)/formats/thom_cas.o   \
137   $(LIBOBJ)/formats/thom_dsk.o   \
138   $(LIBOBJ)/formats/ti99_dsk.o   \
139   $(LIBOBJ)/formats/trd_dsk.o      \
140   $(LIBOBJ)/formats/trs_cas.o      \
141   $(LIBOBJ)/formats/trs_dsk.o      \
142   $(LIBOBJ)/formats/tzx_cas.o      \
143   $(LIBOBJ)/formats/uef_cas.o      \
144   $(LIBOBJ)/formats/vg5k_cas.o   \
145   $(LIBOBJ)/formats/vt_cas.o      \
146   $(LIBOBJ)/formats/vt_dsk.o      \
147   $(LIBOBJ)/formats/wavfile.o      \
148   $(LIBOBJ)/formats/x1_tap.o      \
149   $(LIBOBJ)/formats/z80ne_dsk.o   \
150   $(LIBOBJ)/formats/zx81_p.o      \
151
152
153$(OBJ)/libformats.a: $(EXPATOBJS)
154
155
156#-------------------------------------------------
78157# zlib library objects
79158#-------------------------------------------------
80159
src/lib/util/imageutl.c
r11457r11458
1/***************************************************************************
2
3    imageutl.h
4
5    Image related utilities
6
7****************************************************************************
8
9    Copyright Aaron Giles
10    All rights reserved.
11
12    Redistribution and use in source and binary forms, with or without
13    modification, are permitted provided that the following conditions are
14    met:
15
16        * Redistributions of source code must retain the above copyright
17          notice, this list of conditions and the following disclaimer.
18        * Redistributions in binary form must reproduce the above copyright
19          notice, this list of conditions and the following disclaimer in
20          the documentation and/or other materials provided with the
21          distribution.
22        * Neither the name 'MAME' nor the names of its contributors may be
23          used to endorse or promote products derived from this software
24          without specific prior written permission.
25
26    THIS SOFTWARE IS PROVIDED BY AARON GILES ''AS IS'' AND ANY EXPRESS OR
27    IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
28    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
29    DISCLAIMED. IN NO EVENT SHALL AARON GILES BE LIABLE FOR ANY DIRECT,
30    INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
31    (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
32    SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33    HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
34    STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
35    IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36    POSSIBILITY OF SUCH DAMAGE.
37
38***************************************************************************/
39
40#include <ctype.h>
41#include "imageutl.h"
42
43int compute_log2(int val)
44{
45   int count = 0;
46
47   while(val > 1)
48   {
49      if (val & 1)
50         return -1;
51      val /= 2;
52      count++;
53   }
54   return (val == 0) ? -1 : count;
55}
56
57/*
58   Compute CCITT CRC-16 using the correct bit order for floppy disks.
59   CRC code courtesy of Tim Mann.
60*/
61
62/* Accelerator table to compute the CRC eight bits at a time */
63static const unsigned short ccitt_crc16_table[256] =
64{
65   0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7,
66   0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF,
67   0x1231, 0x0210, 0x3273, 0x2252, 0x52B5, 0x4294, 0x72F7, 0x62D6,
68   0x9339, 0x8318, 0xB37B, 0xA35A, 0xD3BD, 0xC39C, 0xF3FF, 0xE3DE,
69   0x2462, 0x3443, 0x0420, 0x1401, 0x64E6, 0x74C7, 0x44A4, 0x5485,
70   0xA56A, 0xB54B, 0x8528, 0x9509, 0xE5EE, 0xF5CF, 0xC5AC, 0xD58D,
71   0x3653, 0x2672, 0x1611, 0x0630, 0x76D7, 0x66F6, 0x5695, 0x46B4,
72   0xB75B, 0xA77A, 0x9719, 0x8738, 0xF7DF, 0xE7FE, 0xD79D, 0xC7BC,
73   0x48C4, 0x58E5, 0x6886, 0x78A7, 0x0840, 0x1861, 0x2802, 0x3823,
74   0xC9CC, 0xD9ED, 0xE98E, 0xF9AF, 0x8948, 0x9969, 0xA90A, 0xB92B,
75   0x5AF5, 0x4AD4, 0x7AB7, 0x6A96, 0x1A71, 0x0A50, 0x3A33, 0x2A12,
76   0xDBFD, 0xCBDC, 0xFBBF, 0xEB9E, 0x9B79, 0x8B58, 0xBB3B, 0xAB1A,
77   0x6CA6, 0x7C87, 0x4CE4, 0x5CC5, 0x2C22, 0x3C03, 0x0C60, 0x1C41,
78   0xEDAE, 0xFD8F, 0xCDEC, 0xDDCD, 0xAD2A, 0xBD0B, 0x8D68, 0x9D49,
79   0x7E97, 0x6EB6, 0x5ED5, 0x4EF4, 0x3E13, 0x2E32, 0x1E51, 0x0E70,
80   0xFF9F, 0xEFBE, 0xDFDD, 0xCFFC, 0xBF1B, 0xAF3A, 0x9F59, 0x8F78,
81   0x9188, 0x81A9, 0xB1CA, 0xA1EB, 0xD10C, 0xC12D, 0xF14E, 0xE16F,
82   0x1080, 0x00A1, 0x30C2, 0x20E3, 0x5004, 0x4025, 0x7046, 0x6067,
83   0x83B9, 0x9398, 0xA3FB, 0xB3DA, 0xC33D, 0xD31C, 0xE37F, 0xF35E,
84   0x02B1, 0x1290, 0x22F3, 0x32D2, 0x4235, 0x5214, 0x6277, 0x7256,
85   0xB5EA, 0xA5CB, 0x95A8, 0x8589, 0xF56E, 0xE54F, 0xD52C, 0xC50D,
86   0x34E2, 0x24C3, 0x14A0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
87   0xA7DB, 0xB7FA, 0x8799, 0x97B8, 0xE75F, 0xF77E, 0xC71D, 0xD73C,
88   0x26D3, 0x36F2, 0x0691, 0x16B0, 0x6657, 0x7676, 0x4615, 0x5634,
89   0xD94C, 0xC96D, 0xF90E, 0xE92F, 0x99C8, 0x89E9, 0xB98A, 0xA9AB,
90   0x5844, 0x4865, 0x7806, 0x6827, 0x18C0, 0x08E1, 0x3882, 0x28A3,
91   0xCB7D, 0xDB5C, 0xEB3F, 0xFB1E, 0x8BF9, 0x9BD8, 0xABBB, 0xBB9A,
92   0x4A75, 0x5A54, 0x6A37, 0x7A16, 0x0AF1, 0x1AD0, 0x2AB3, 0x3A92,
93   0xFD2E, 0xED0F, 0xDD6C, 0xCD4D, 0xBDAA, 0xAD8B, 0x9DE8, 0x8DC9,
94   0x7C26, 0x6C07, 0x5C64, 0x4C45, 0x3CA2, 0x2C83, 0x1CE0, 0x0CC1,
95   0xEF1F, 0xFF3E, 0xCF5D, 0xDF7C, 0xAF9B, 0xBFBA, 0x8FD9, 0x9FF8,
96   0x6E17, 0x7E36, 0x4E55, 0x5E74, 0x2E93, 0x3EB2, 0x0ED1, 0x1EF0
97};
98
99unsigned short ccitt_crc16(unsigned short crc, const unsigned char *buffer, size_t buffer_len)
100{
101   size_t i;
102   for (i = 0; i < buffer_len; i++)
103      crc = (crc << 8) ^ ccitt_crc16_table[(crc >> 8) ^ buffer[i]];
104   return crc;
105}
106
107unsigned short ccitt_crc16_one( unsigned short crc, const unsigned char data )
108{
109    return (crc << 8) ^ ccitt_crc16_table[(crc >> 8) ^ data];
110}
111
112
113/*-------------------------------------------------
114    is_delim - returns whether c is a comma or \0
115-------------------------------------------------*/
116
117static int is_delim(char c)
118{
119   return (c == '\0') || (c == ',');
120}
121
122
123
124/*-------------------------------------------------
125    image_internal_find_extension - find an extension in
126    an extension list
127-------------------------------------------------*/
128
129static int image_internal_find_extension(const char *extension_list, const char *target_extension)
130{
131   /* this version allows target_extension to be delimited with a comma */
132   int pos = 0;
133   int i;
134
135   while(extension_list[pos] != '\0')
136   {
137      /* compare a file extension */
138      i = 0;
139      while(!is_delim(extension_list[pos + i])
140         && !is_delim(target_extension[i])
141         && (tolower(extension_list[pos + i]) == tolower(target_extension[i])))
142      {
143         i++;
144      }
145
146      /* check to see if it was found */
147      if (is_delim(extension_list[pos + i]) && is_delim(target_extension[i]))
148         return TRUE;
149
150      /* move to next position in the buffer */
151      pos += i;
152      while(!is_delim(extension_list[pos]))
153         pos++;
154      while(extension_list[pos] == ',')
155         pos++;
156   }
157
158   /* not found */
159   return FALSE;
160}
161
162
163
164/*-------------------------------------------------
165    image_find_extension - find an extension in an extension
166    list
167-------------------------------------------------*/
168
169int image_find_extension(const char *extension_list, const char *target_extension)
170{
171   /* the internal function allows something that we do not */
172   if (strchr(target_extension, ','))
173      return FALSE;
174
175   /* special case to allow ext to be in the form '.EXT' */
176   if (*target_extension == '.')
177      target_extension++;
178
179   /* do the actual work */
180   return image_internal_find_extension(extension_list, target_extension);
181}
182
183
184
185/*-------------------------------------------------
186    image_specify_extension - merge a comma-delimited
187    list of file extensions onto an existing list
188-------------------------------------------------*/
189
190void image_specify_extension(char *buffer, size_t buffer_len, const char *extension)
191{
192   int extension_pos = 0;
193   int len;
194   int found;
195
196   /* determine the length of the buffer */
197   len = strlen(buffer);
198
199   /* be aware that extension can be NULL */
200   if (extension != NULL)
201   {
202      while(extension[extension_pos] != '\0')
203      {
204         /* try to find the file extension */
205         found = image_internal_find_extension(buffer, &extension[extension_pos]);
206
207         /* append a delimiter if we have to */
208         if (!found && (len > 0))
209            len += snprintf(&buffer[len], buffer_len - len, ",");
210
211         /* move to the next extension, appending the extension if not found */
212         while(!is_delim(extension[extension_pos]))
213         {
214            if (!found)
215               len += snprintf(&buffer[len], buffer_len - len, "%c", extension[extension_pos]);
216            extension_pos++;
217         }
218         while(extension[extension_pos] == ',')
219            extension_pos++;
220      }
221   }
222}
223
src/lib/util/imageutl.h
r11457r11458
1/***************************************************************************
2
3    imageutl.h
4
5    Image related utilities
6
7****************************************************************************
8
9    Copyright Aaron Giles
10    All rights reserved.
11
12    Redistribution and use in source and binary forms, with or without
13    modification, are permitted provided that the following conditions are
14    met:
15
16        * Redistributions of source code must retain the above copyright
17          notice, this list of conditions and the following disclaimer.
18        * Redistributions in binary form must reproduce the above copyright
19          notice, this list of conditions and the following disclaimer in
20          the documentation and/or other materials provided with the
21          distribution.
22        * Neither the name 'MAME' nor the names of its contributors may be
23          used to endorse or promote products derived from this software
24          without specific prior written permission.
25
26    THIS SOFTWARE IS PROVIDED BY AARON GILES ''AS IS'' AND ANY EXPRESS OR
27    IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
28    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
29    DISCLAIMED. IN NO EVENT SHALL AARON GILES BE LIABLE FOR ANY DIRECT,
30    INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
31    (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
32    SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33    HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
34    STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
35    IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36    POSSIBILITY OF SUCH DAMAGE.
37
38***************************************************************************/
39
40#pragma once
41
42#ifndef __IMAGEUTL_H__
43#define __IMAGEUTL_H__
44
45#include "osdcore.h"
46
47/* -----------------------------------------------------------------------
48 * CRC stuff
49 * ----------------------------------------------------------------------- */
50unsigned short ccitt_crc16(unsigned short crc, const unsigned char *buffer, size_t buffer_len);
51unsigned short ccitt_crc16_one( unsigned short crc, const unsigned char data );
52
53/* -----------------------------------------------------------------------
54 * Alignment-friendly integer placement
55 * ----------------------------------------------------------------------- */
56
57INLINE void place_integer_be(void *ptr, size_t offset, size_t size, UINT64 value)
58{
59   UINT8 *byte_ptr = ((UINT8 *) ptr) + offset;
60   UINT16 val16;
61   UINT32 val32;
62
63   switch(size)
64   {
65      case 2:
66         val16 = BIG_ENDIANIZE_INT16((UINT16) value);
67         memcpy(byte_ptr, &val16, sizeof(val16));
68         break;
69
70      case 4:
71         val32 = BIG_ENDIANIZE_INT32((UINT32) value);
72         memcpy(byte_ptr, &val32, sizeof(val32));
73         break;
74
75      default:
76         if (size >= 1)   byte_ptr[0] = (UINT8) (value >> ((size - 1) * 8));
77         if (size >= 2)   byte_ptr[1] = (UINT8) (value >> ((size - 2) * 8));
78         if (size >= 3)   byte_ptr[2] = (UINT8) (value >> ((size - 3) * 8));
79         if (size >= 4)   byte_ptr[3] = (UINT8) (value >> ((size - 4) * 8));
80         if (size >= 5)   byte_ptr[4] = (UINT8) (value >> ((size - 5) * 8));
81         if (size >= 6)   byte_ptr[5] = (UINT8) (value >> ((size - 6) * 8));
82         if (size >= 7)   byte_ptr[6] = (UINT8) (value >> ((size - 7) * 8));
83         if (size >= 8)   byte_ptr[7] = (UINT8) (value >> ((size - 8) * 8));
84         break;
85   }
86}
87
88INLINE UINT64 pick_integer_be(const void *ptr, size_t offset, size_t size)
89{
90   UINT64 result = 0;
91   const UINT8 *byte_ptr = ((const UINT8 *) ptr) + offset;
92   UINT16 val16;
93   UINT32 val32;
94
95   switch(size)
96   {
97      case 1:
98         result = *byte_ptr;
99         break;
100
101      case 2:
102         memcpy(&val16, byte_ptr, sizeof(val16));
103         result = BIG_ENDIANIZE_INT16(val16);
104         break;
105
106      case 4:
107         memcpy(&val32, byte_ptr, sizeof(val32));
108         result = BIG_ENDIANIZE_INT32(val32);
109         break;
110
111      default:
112         if (size >= 1)   result |= ((UINT64) byte_ptr[0]) << ((size - 1) * 8);
113         if (size >= 2)   result |= ((UINT64) byte_ptr[1]) << ((size - 2) * 8);
114         if (size >= 3)   result |= ((UINT64) byte_ptr[2]) << ((size - 3) * 8);
115         if (size >= 4)   result |= ((UINT64) byte_ptr[3]) << ((size - 4) * 8);
116         if (size >= 5)   result |= ((UINT64) byte_ptr[4]) << ((size - 5) * 8);
117         if (size >= 6)   result |= ((UINT64) byte_ptr[5]) << ((size - 6) * 8);
118         if (size >= 7)   result |= ((UINT64) byte_ptr[6]) << ((size - 7) * 8);
119         if (size >= 8)   result |= ((UINT64) byte_ptr[7]) << ((size - 8) * 8);
120         break;
121   }
122   return result;
123}
124
125INLINE void place_integer_le(void *ptr, size_t offset, size_t size, UINT64 value)
126{
127   UINT8 *byte_ptr = ((UINT8 *) ptr) + offset;
128   UINT16 val16;
129   UINT32 val32;
130
131   switch(size)
132   {
133      case 2:
134         val16 = LITTLE_ENDIANIZE_INT16((UINT16) value);
135         memcpy(byte_ptr, &val16, sizeof(val16));
136         break;
137
138      case 4:
139         val32 = LITTLE_ENDIANIZE_INT32((UINT32) value);
140         memcpy(byte_ptr, &val32, sizeof(val32));
141         break;
142
143      default:
144         if (size >= 1)   byte_ptr[0] = (UINT8) (value >> (0 * 8));
145         if (size >= 2)   byte_ptr[1] = (UINT8) (value >> (1 * 8));
146         if (size >= 3)   byte_ptr[2] = (UINT8) (value >> (2 * 8));
147         if (size >= 4)   byte_ptr[3] = (UINT8) (value >> (3 * 8));
148         if (size >= 5)   byte_ptr[4] = (UINT8) (value >> (4 * 8));
149         if (size >= 6)   byte_ptr[5] = (UINT8) (value >> (5 * 8));
150         if (size >= 7)   byte_ptr[6] = (UINT8) (value >> (6 * 8));
151         if (size >= 8)   byte_ptr[7] = (UINT8) (value >> (7 * 8));
152         break;
153   }
154}
155
156INLINE UINT64 pick_integer_le(const void *ptr, size_t offset, size_t size)
157{
158   UINT64 result = 0;
159   const UINT8 *byte_ptr = ((const UINT8 *) ptr) + offset;
160   UINT16 val16;
161   UINT32 val32;
162
163   switch(size)
164   {
165      case 1:
166         result = *byte_ptr;
167         break;
168
169      case 2:
170         memcpy(&val16, byte_ptr, sizeof(val16));
171         result = LITTLE_ENDIANIZE_INT16(val16);
172         break;
173
174      case 4:
175         memcpy(&val32, byte_ptr, sizeof(val32));
176         result = LITTLE_ENDIANIZE_INT32(val32);
177         break;
178
179      default:
180         if (size >= 1)   result |= ((UINT64) byte_ptr[0]) << (0 * 8);
181         if (size >= 2)   result |= ((UINT64) byte_ptr[1]) << (1 * 8);
182         if (size >= 3)   result |= ((UINT64) byte_ptr[2]) << (2 * 8);
183         if (size >= 4)   result |= ((UINT64) byte_ptr[3]) << (3 * 8);
184         if (size >= 5)   result |= ((UINT64) byte_ptr[4]) << (4 * 8);
185         if (size >= 6)   result |= ((UINT64) byte_ptr[5]) << (5 * 8);
186         if (size >= 7)   result |= ((UINT64) byte_ptr[6]) << (6 * 8);
187         if (size >= 8)   result |= ((UINT64) byte_ptr[7]) << (7 * 8);
188         break;
189   }
190   return result;
191}
192
193/* -----------------------------------------------------------------------
194 * Miscellaneous
195 * ----------------------------------------------------------------------- */
196
197/* miscellaneous functions */
198int compute_log2(int val);
199
200/* -----------------------------------------------------------------------
201 * Extension list handling
202 * ----------------------------------------------------------------------- */
203
204int image_find_extension(const char *extensions, const char *ext);
205void image_specify_extension(char *buffer, size_t buffer_len, const char *extension);
206
207#endif /* __IMAGEUTL_H__ */
src/mess/tools/castool/castool.mak
r11457r11458
3434   $(EMUOBJ)/emucore.o               \
3535   $(EMUOBJ)/emuopts.o             \
3636   $(EMUOBJ)/memory.o               \
37   $(EMUOBJ)/ioprocs.o               \
38   $(MESS_FORMATS)/a26_cas.o           \
39   $(MESS_FORMATS)/apf_apt.o           \
40   $(MESS_FORMATS)/cbm_tap.o           \
41   $(MESS_FORMATS)/cgen_cas.o          \
42   $(MESS_FORMATS)/coco_cas.o          \
43   $(MESS_FORMATS)/csw_cas.o           \
44   $(MESS_FORMATS)/fmsx_cas.o          \
45   $(MESS_FORMATS)/gtp_cas.o           \
46   $(MESS_FORMATS)/ace_tap.o          \
47   $(MESS_FORMATS)/hect_tap.o          \
48   $(MESS_FORMATS)/kim1_cas.o          \
49   $(MESS_FORMATS)/lviv_lvt.o          \
50   $(MESS_FORMATS)/mz_cas.o            \
51   $(MESS_FORMATS)/orao_cas.o          \
52   $(MESS_FORMATS)/oric_tap.o          \
53   $(MESS_FORMATS)/pmd_pmd.o           \
54   $(MESS_FORMATS)/primoptp.o          \
55   $(MESS_FORMATS)/rk_cas.o            \
56   $(MESS_FORMATS)/sord_cas.o          \
57   $(MESS_FORMATS)/svi_cas.o           \
58   $(MESS_FORMATS)/trs_cas.o           \
59   $(MESS_FORMATS)/tzx_cas.o           \
60   $(MESS_FORMATS)/uef_cas.o           \
61   $(MESS_FORMATS)/vg5k_cas.o          \
62   $(MESS_FORMATS)/vt_cas.o            \
63   $(MESS_FORMATS)/zx81_p.o          \
64   $(EMUOBJ)/imagedev/cassimg.o         \
65   $(EMUOBJ)/imagedev/wavfile.o         \
6637
6738
6839$(LIBCASTOOL): $(CASTOOL_LIB_OBJS)
r11457r11458
7849# rules to build the castool executable
7950#-------------------------------------------------
8051
81$(CASTOOL): $(CASTOOL_OBJS) $(LIBCASTOOL) $(LIBUTIL) $(EXPAT) $(ZLIB) $(LIBOCORE)
52$(CASTOOL): $(CASTOOL_OBJS) $(LIBCASTOOL) $(LIBUTIL) $(EXPAT) $(FORMATS_LIB) $(ZLIB) $(LIBOCORE)
8253   @echo Linking $@...
8354   $(LD) $(LDFLAGS) $^ $(LIBS) -o $@
src/mess/tools/messtest/testimgt.c
r11457r11458
77*********************************************************************/
88
99#include "testimgt.h"
10#include "imageutl.h"
10#include "formats/imageutl.h"
1111#include "../imgtool/imgtool.h"
1212#include "../imgtool/modules.h"
1313
src/mess/tools/imgtool/imgtool.mak
r11457r11458
3636   $(EMUOBJ)/emucore.o            \
3737   $(EMUOBJ)/emuopts.o             \
3838   $(EMUOBJ)/memory.o            \
39   $(EMUOBJ)/ioprocs.o         \
40   $(EMUOBJ)/imagedev/flopimg.o         \
41   $(EMUOBJ)/imagedev/cassimg.o         \
42   $(MESS_FORMATS)/basicdsk.o         \
43   $(MESS_FORMATS)/coco_dsk.o         \
44   $(MESS_FORMATS)/coco_cas.o         \
45   $(MESS_FORMATS)/imd_dsk.o         \
46   $(MESS_FORMATS)/td0_dsk.o         \
47   $(MESS_FORMATS)/cqm_dsk.o         \
48   $(MESS_FORMATS)/dsk_dsk.o         \
49   $(MESS_FORMATS)/d88_dsk.o         \
50   $(MESS_FORMATS)/pc_dsk.o         \
51   $(MESS_FORMATS)/ap2_dsk.o         \
52   $(MESS_FORMATS)/ap_dsk35.o         \
53   $(EMUOBJ)/imagedev/wavfile.o         \
54   $(MESS_FORMATS)/vt_dsk.o         \
55   $(MESS_FORMATS)/fdi_dsk.o         \
5639   $(IMGTOOLOBJ)/stream.o            \
5740   $(IMGTOOLOBJ)/library.o            \
5841   $(IMGTOOLOBJ)/modules.o            \
r11457r11458
11598# rules to build the imgtool executable
11699#-------------------------------------------------
117100
118$(IMGTOOL): $(IMGTOOL_OBJS) $(LIBIMGTOOL) $(LIBUTIL) $(EXPAT) $(ZLIB) $(LIBOCORE)
101$(IMGTOOL): $(IMGTOOL_OBJS) $(LIBIMGTOOL) $(LIBUTIL) $(EXPAT) $(ZLIB) $(FORMATS_LIB) $(LIBOCORE)
119102   @echo Linking $@...
120103   $(LD) $(LDFLAGS) $^ $(LIBS) -o $@
src/mess/tools/imgtool/iflopimg.c
r11457r11458
77*********************************************************************/
88
99#include "emu.h"
10#include "imagedev/flopimg.h"
10#include "formats/flopimg.h"
1111#include "imgtoolx.h"
1212#include "library.h"
1313#include "iflopimg.h"
src/mess/tools/imgtool/windows/assocdlg.c
r11457r11458
1717#include "wimgres.h"
1818#include "strconv.h"
1919#include "../imgtool.h"
20#include "imageutl.h"
20#include "formats/imageutl.h"
2121
2222//============================================================
2323//  PARAMETERS
src/mess/tools/imgtool/windows/wimgtool.mak
r11457r11458
5959   @echo Compiling resources $<...
6060   $(RC) $(RCDEFS) $(RCFLAGS) --include-dir $(WIMGTOOLSRC) -o $@ -i $<
6161
62$(WIMGTOOL): $(WIMGTOOL_OBJS) $(LIBIMGTOOL) $(LIBUTIL) $(EXPAT) $(ZLIB) $(LIBOCORE_NOMAIN)
62$(WIMGTOOL): $(WIMGTOOL_OBJS) $(LIBIMGTOOL) $(LIBUTIL) $(EXPAT) $(ZLIB) $(FORMATS_LIB) $(LIBOCORE_NOMAIN)
6363   @echo Linking $@...
6464   $(LD) $(LDFLAGS_WIN) -municode -mwindows $^ $(LIBS_WIN) -o $@
src/mess/tools/imgtool/imgtool.c
r11457r11458
1212
1313#include "osdepend.h"
1414#include "imgtoolx.h"
15#include "imageutl.h"
15#include "formats/imageutl.h"
1616#include "library.h"
1717#include "modules.h"
1818#include "pool.h"
src/mess/tools/imgtool/iflopimg.h
r11457r11458
99#ifndef IFLOPIMG_H
1010#define IFLOPIMG_H
1111
12#include "imagedev/flopimg.h"
12#include "formats/flopimg.h"
1313#include "imgtoolx.h"
1414#include "library.h"
1515
src/mess/tools/imgtool/imgtool.h
r11457r11458
1414
1515#include "emu.h"
1616#include "osdepend.h"
17#include "imagedev/flopimg.h"
17#include "formats/flopimg.h"
1818#include "opresolv.h"
1919#include "library.h"
2020#include "filter.h"
src/mess/tools/imgtool/filtbas.c
r11457r11458
2222#include <ctype.h>
2323
2424#include "imgtool.h"
25#include "imageutl.h"
25#include "formats/imageutl.h"
2626
2727
2828/***************************************************************************
src/mess/tools/imgtool/modules/pc_hard.c
r11457r11458
4949****************************************************************************/
5050
5151#include "imgtool.h"
52#include "imageutl.h"
52#include "formats/imageutl.h"
5353#include "imghd.h"
5454
5555#define FAT_SECLEN   512
src/mess/tools/imgtool/modules/os9.c
r11457r11458
99#include <time.h>
1010
1111#include "imgtool.h"
12#include "imageutl.h"
12#include "formats/imageutl.h"
1313#include "formats/coco_dsk.h"
1414#include "iflopimg.h"
1515
src/mess/tools/imgtool/modules/amiga.c
r11457r11458
1818
1919#include "imgtool.h"
2020#include "iflopimg.h"
21#include "imageutl.h"
21#include "formats/imageutl.h"
2222#include "formats/ami_dsk.h"
2323
2424
src/mess/tools/imgtool/modules/macbin.c
r11457r11458
4747#include <string.h>
4848
4949#include "imgtool.h"
50#include "imageutl.h"
50#include "formats/imageutl.h"
5151#include "macutil.h"
5252
5353
src/mess/tools/imgtool/modules/mac.c
r11457r11458
9494#include <stddef.h>
9595
9696#include "emu.h"
97#include "imageutl.h"
97#include "formats/imageutl.h"
9898#include "osdepend.h"
9999#include "imgtoolx.h"
100100#include "macutil.h"
src/mess/tools/imgtool/modules/vzdos.c
r11457r11458
1313#include "osdepend.h"
1414#include "imgtool.h"
1515#include "imgtoolx.h"
16#include "imageutl.h"
16#include "formats/imageutl.h"
1717#include "formats/vt_dsk.h"
1818#include "iflopimg.h"
1919
src/mess/tools/imgtool/modules/prodos.c
r11457r11458
122122*****************************************************************************/
123123
124124#include "imgtool.h"
125#include "imageutl.h"
125#include "formats/imageutl.h"
126126#include "formats/ap2_dsk.h"
127127#include "formats/ap_dsk35.h"
128128#include "iflopimg.h"
src/mess/tools/imgtool/modules/fat.c
r11457r11458
136136#include <ctype.h>
137137
138138#include "imgtool.h"
139#include "imageutl.h"
139#include "formats/imageutl.h"
140140#include "unicode.h"
141141#include "fat.h"
142142
src/mess/tools/imgtool/modules/pc_flop.c
r11457r11458
77****************************************************************************/
88
99#include "imgtool.h"
10#include "imageutl.h"
10#include "formats/imageutl.h"
1111#include "formats/pc_dsk.h"
1212#include "fat.h"
1313#include "iflopimg.h"
src/mess/machine/ti99/ti99_hd.c
r11457r11458
1212**************************************************************************/
1313
1414#include "emu.h"
15#include "imageutl.h"
15#include "formats/imageutl.h"
1616#include "harddisk.h"
1717#include "imagedev/harddriv.h"
1818#include "machine/smc92x4.h"
src/mess/machine/thomflop.h
r11457r11458
1010#define THOMFLOP_H_
1111
1212#include "image.h"
13#include "imagedev/flopdrv.h"
1314#include "machine/mc6843.h"
1415#include "machine/mc6854.h"
1516
src/mess/machine/990_dk.h
r11457r11458
11/*
22    990_dk.h: include file for 990_dk.c
33*/
4#include "imagedev/flopimg.h"
4#include "formats/flopimg.h"
55
66FLOPPY_OPTIONS_EXTERN(fd800);
77
src/mess/machine/wd17xx.c
r11457r11458
145145
146146
147147#include "emu.h"
148#include "imageutl.h"
148#include "formats/imageutl.h"
149149#include "machine/wd17xx.h"
150150#include "imagedev/flopdrv.h"
151#include "imagedev/flopimg.h"
151#include "formats/flopimg.h"
152152
153153/***************************************************************************
154154    CONSTANTS
src/mess/machine/lux21046.h
r11457r11458
1010#include "cpu/z80/z80daisy.h"
1111#include "formats/basicdsk.h"
1212#include "imagedev/flopdrv.h"
13#include "imagedev/flopimg.h"
13#include "formats/flopimg.h"
1414#include "machine/abcbus.h"
1515#include "machine/devhelpr.h"
1616#include "machine/wd17xx.h"
src/mess/machine/abc830.h
r11457r11458
88#include "emu.h"
99#include "formats/basicdsk.h"
1010#include "imagedev/flopdrv.h"
11#include "imagedev/flopimg.h"
11#include "formats/flopimg.h"
1212#include "machine/lux10828.h"
1313#include "machine/lux21046.h"
1414
src/mess/machine/mtx.c
r11457r11458
77**************************************************************************/
88
99#include "emu.h"
10#include "imageutl.h"
10#include "formats/imageutl.h"
1111#include "includes/mtx.h"
1212#include "cpu/z80/z80.h"
1313#include "imagedev/cassette.h"
src/mess/machine/lux10828.h
r11457r11458
1010#include "cpu/z80/z80daisy.h"
1111#include "formats/basicdsk.h"
1212#include "imagedev/flopdrv.h"
13#include "imagedev/flopimg.h"
13#include "formats/flopimg.h"
1414#include "machine/abcbus.h"
1515#include "machine/devhelpr.h"
1616#include "machine/wd17xx.h"
src/mess/machine/smc92x4.c
r11457r11458
1616*/
1717
1818#include "emu.h"
19#include "imageutl.h"
19#include "formats/imageutl.h"
2020#include "imagedev/flopdrv.h"
2121#include "imagedev/harddriv.h"
2222#include "harddisk.h"
src/mess/includes/msx.h
r11457r11458
88#define __MSX_H__
99
1010#include "machine/wd17xx.h"
11#include "imagedev/flopimg.h"
11#include "formats/flopimg.h"
1212
1313#define MSX_MAX_CARTS   (2)
1414
src/mess/formats/thom_cas.h
r11457r11458
1/**********************************************************************
2
3  Copyright (C) Antoine Mine' 2006
4
5  Thomson 8-bit computers
6
7**********************************************************************/
8
9#ifndef THOM_CAS
10#define THOM_CAS
11
12
13
14/******************** TO7 ************************/
15
16extern const cassette_config to7_cassette_config;
17
18
19/******************** MO5 ************************/
20
21extern const cassette_config mo5_cassette_config;
22
23#endif
src/mess/formats/fmsx_cas.c
r11457r11458
1#include "formats/fmsx_cas.h"
2
3
4#define CAS_PERIOD        (16)
5#define CAS_HEADER_PERIODS (4000)
6#define CAS_EMPTY_PERIODS (1000)
7static const UINT8 CasHeader[8] = { 0x1F,0xA6,0xDE,0xBA,0xCC,0x13,0x7D,0x74 };
8
9static int cas_size;
10
11
12/*******************************************************************
13   Calculate the number of samples needed for this tape image
14********************************************************************/
15static int fmsx_cas_to_wav_size (const UINT8 *casdata, int caslen)
16{
17   int    pos, size;
18
19   if (caslen < 8) return -1;
20   if (memcmp (casdata, CasHeader, sizeof (CasHeader) ) ) return -1;
21
22   pos = size = 0;
23
24   while (pos < caslen)
25   {
26      if ( (pos + 8) < caslen)
27         if (!memcmp (casdata + pos, CasHeader, 8) )
28         {
29            size += (CAS_EMPTY_PERIODS + CAS_HEADER_PERIODS) * CAS_PERIOD;
30            pos += 8;
31            continue;
32         }
33
34      size += CAS_PERIOD * 12;
35      pos++;
36   }
37
38   cas_size = caslen;
39
40   return size;
41}
42
43
44/*******************************************************************
45   Generate samples for the tape image
46********************************************************************/
47static int fmsx_cas_fill_wave(INT16 *buffer, int sample_count, UINT8 *bytes)
48{
49   int cas_pos, bit, state = 1, samples_pos, size, n, i, p;
50
51   cas_pos = 0;
52   samples_pos = 0;
53
54    while (samples_pos < sample_count && cas_pos < cas_size)
55   {
56      /* Check if we need to output a header */
57      if ( cas_pos + 8 < cas_size )
58      {
59         if ( ! memcmp( bytes + cas_pos, CasHeader, 8 ) )
60         {
61            /* Write CAS_EMPTY_PERIODS of silence */
62            n = CAS_EMPTY_PERIODS * CAS_PERIOD; while (n--) buffer[samples_pos++] = 0;
63
64            /* Write CAS_HEADER_PERIODS of header (high frequency) */
65            for (i=0;i<CAS_HEADER_PERIODS*4;i++)
66            {
67               for (n=0;n<CAS_PERIOD / 4;n++)
68                  buffer[samples_pos + n] = (state ? 32767 : -32767);
69
70               samples_pos += CAS_PERIOD / 4 ;
71               state = !state;
72            }
73
74            cas_pos += 8;
75         }
76      }
77
78      for (i=0;i<=11;i++)
79      {
80         if (i == 0) bit = 0;
81         else if (i < 9) bit = (bytes[cas_pos] & (1 << (i - 1) ) );
82         else bit = 1;
83
84         /* write this one bit */
85         for (n=0;n<(bit ? 4 : 2);n++)
86         {
87            size = (bit ? CAS_PERIOD / 4 : CAS_PERIOD / 2);
88            for (p=0;p<size;p++)
89            {
90               buffer[samples_pos + p] = (state ? 32767 : -32767);
91            }
92            state = !state;
93            samples_pos += size;
94         }
95      }
96      cas_pos++;
97   }
98
99   return samples_pos;
100}
101
102
103static const struct CassetteLegacyWaveFiller fmsx_legacy_fill_wave =
104{
105   fmsx_cas_fill_wave,                  /* fill_wave */
106   -1,                              /* chunk_size */
107   0,                              /* chunk_samples */
108   fmsx_cas_to_wav_size,               /* chunk_sample_calc */
109   22050,                           /* sample_frequency */
110   0,                              /* header_samples */
111   0                              /* trailer_samples */
112};
113
114
115
116static casserr_t fmsx_cas_identify(cassette_image *cassette, struct CassetteOptions *opts)
117{
118   return cassette_legacy_identify(cassette, opts, &fmsx_legacy_fill_wave);
119}
120
121
122
123static casserr_t fmsx_cas_load(cassette_image *cassette)
124{
125   return cassette_legacy_construct(cassette, &fmsx_legacy_fill_wave);
126}
127
128
129
130static const struct CassetteFormat fmsx_cas_format =
131{
132   "tap,cas",
133   fmsx_cas_identify,
134   fmsx_cas_load,
135   NULL
136};
137
138
139
140CASSETTE_FORMATLIST_START(fmsx_cassette_formats)
141   CASSETTE_FORMAT(fmsx_cas_format)
142CASSETTE_FORMATLIST_END
src/mess/formats/d88_dsk.c
r11457r11458
1/*
2 *   D77 and D88 disk images
3 *
4 *
5 *   Header (total size = 0x2b0 bytes):
6 *     0x00 - Disk label
7 *     0x1a - Write protect (bit 3)
8 *     0x1b - 2D/2DD format (bit 3)
9 *     0x1c-1f - image size (should match file size)
10 *     0x20 - offsets for each track (max 164)
11 *
12 *   Sectors (0x110 bytes each, typically)
13 *     0x00 - Sector info
14 *          byte 0 - track number
15 *          byte 1 - side (0 or 1)
16 *          byte 2 - sector number
17 *     0x10 - sector data
18 *
19 *   Images can be concatenated together.
20 *   Sectors can be in any order.
21 *   Tracks are in the order:
22 *          Track 0 side 0
23 *          Track 0 side 1
24 *          Track 1 side 0
25 *          ...
26 *
27 *
28 */
29
30#include "emu.h"
31#include "imagedev/flopimg.h"
32#include "imagedev/flopdrv.h"
33#include "imageutl.h"
34
35#define D88_HEADER_LEN 0x2b0
36
37struct d88_tag
38{
39   UINT32 image_size;
40   UINT32 trackoffset[164];
41   UINT8 write_protect;
42   UINT8 disk_type;
43   UINT8 heads;
44};
45
46static struct d88_tag *get_d88_tag(floppy_image *floppy)
47{
48   return (d88_tag *)floppy_tag(floppy);
49}
50
51static int d88_get_sector_id(floppy_image *floppy, int head, int track, int sector_index)
52{
53   struct d88_tag* tag = get_d88_tag(floppy);
54   UINT32 offset;
55   UINT8 sector_hdr[16];
56   int x;
57
58   offset = tag->trackoffset[(track*tag->heads)+head];
59
60   if(offset == 0)
61      return 0;
62
63   floppy_image_read(floppy,sector_hdr,offset,16);
64
65   // get to sector indexed
66   x=0;
67   while(x<sector_index)
68   {
69      offset += ((sector_hdr[15] << 8) | sector_hdr[14]);
70      offset += 16;
71      floppy_image_read(floppy,sector_hdr,offset,16);
72      x++;
73   }
74
75   return sector_hdr[2];
76}
77
78static int d88_get_tracks_per_disk(floppy_image *floppy)
79{
80   return 82;  // 82 tracks per side
81}
82
83static int d88_get_heads_per_disk(floppy_image *floppy)
84{
85   struct d88_tag* tag = get_d88_tag(floppy);
86   return tag->heads;
87}
88
89static int d88_get_sectors_per_track(floppy_image *floppy, int head, int track)
90{
91   struct d88_tag* tag = get_d88_tag(floppy);
92   UINT32 offset;
93   UINT8 sector_hdr[16];
94
95   offset = tag->trackoffset[(track*tag->heads)+head];
96
97   floppy_image_read(floppy,sector_hdr,offset,16);
98
99   return sector_hdr[4];
100}
101
102static floperr_t d88_get_sector_length(floppy_image *floppy, int head, int track, int sector, UINT32 *sector_length)
103{
104   struct d88_tag* tag = get_d88_tag(floppy);
105   UINT32 offset;
106   UINT8 sector_hdr[16];
107   UINT32 len;
108   int count,secs;
109
110   offset = tag->trackoffset[(track*tag->heads)+head];
111
112   floppy_image_read(floppy,sector_hdr,offset,16);
113   secs = sector_hdr[4];
114
115   for(count=0;count<secs;count++)
116   {
117      floppy_image_read(floppy,sector_hdr,offset,16);
118      if(sector == sector_hdr[2])
119      {
120         if(sector_length)
121            *sector_length = (sector_hdr[15] << 8) | sector_hdr[14];
122         return FLOPPY_ERROR_SUCCESS;
123      }
124      len = (sector_hdr[15] << 8) | sector_hdr[14];
125      len += 16;
126      offset += len;
127   }
128
129   return FLOPPY_ERROR_SEEKERROR;
130}
131
132static floperr_t d88_read_track(floppy_image *floppy, int head, int track, UINT64 offset, void *buffer, size_t buflen)
133{
134//  floperr_t err;
135
136   return FLOPPY_ERROR_UNSUPPORTED;
137}
138
139static UINT32 d88_get_sector_offset(floppy_image* floppy, int head, int track, int sector)
140{
141   struct d88_tag* tag = get_d88_tag(floppy);
142   UINT32 offset = 0;
143   UINT8 sector_hdr[16];
144   UINT32 len;
145   UINT32 secs;
146   int count;
147
148   // get offset of the beginning of the track
149   offset = tag->trackoffset[(track*tag->heads)+head];
150
151   floppy_image_read(floppy,sector_hdr,offset,16);
152   secs = sector_hdr[4];
153
154   for(count=0;count<secs;count++)
155   {
156      floppy_image_read(floppy,sector_hdr,offset,16);
157      if(sector == sector_hdr[2])
158      {
159         logerror("d88_get_sector_offset - track %i, side %i, sector %02x, returns %08x\n",track,head,sector,offset+16);
160         return offset + 16;
161      }
162      len = (sector_hdr[15] << 8) | sector_hdr[14];
163      len += 16;
164      offset += len;
165   }
166   logerror("d88_get_sector_offset - track %i, side %i, sector %02x, not found\n",track,head,sector);
167   return 0;
168}
169
170static floperr_t d88_get_indexed_sector_info(floppy_image *floppy, int head, int track, int sector_index, int *cylinder, int *side, int *sector, UINT32 *sector_length, unsigned long *flags)
171{
172   struct d88_tag* tag = get_d88_tag(floppy);
173   UINT32 offset;
174   UINT8 sector_hdr[16];
175   int x;
176
177   offset = tag->trackoffset[(track*tag->heads)+head];
178
179   if(offset == 0)
180      return FLOPPY_ERROR_SEEKERROR;
181
182
183   floppy_image_read(floppy,sector_hdr,offset,16);
184
185   if(sector_index >= sector_hdr[4])
186      return FLOPPY_ERROR_SEEKERROR;
187
188   // get to sector indexed
189   x=0;
190   while(x<sector_index)
191   {
192      offset += ((sector_hdr[15] << 8) | sector_hdr[14]);
193      offset += 16;
194      floppy_image_read(floppy,sector_hdr,offset,16);
195      x++;
196   }
197
198   if(offset > tag->image_size || offset == 0)
199      return FLOPPY_ERROR_SEEKERROR;
200
201   if(sector_length)
202      *sector_length = (sector_hdr[15] << 8) | sector_hdr[14];
203   if(cylinder)
204      *cylinder = sector_hdr[0];
205   if(side)
206      *side = sector_hdr[1];
207   if(sector)
208      *sector = sector_hdr[2];
209   if(flags)
210      *flags = 0;
211
212   return FLOPPY_ERROR_SUCCESS;
213}
214
215static floperr_t d88_read_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen)
216{
217   UINT64 offset;
218   UINT32 sector_length;
219
220   offset = d88_get_sector_offset(floppy,head,track,sector);
221
222   if(d88_get_sector_length(floppy,head,track,sector,&sector_length) != FLOPPY_ERROR_SUCCESS)
223      return FLOPPY_ERROR_SEEKERROR;
224
225   if(offset == 0)
226      return FLOPPY_ERROR_SEEKERROR;
227
228   if(buflen > sector_length)
229      return FLOPPY_ERROR_INTERNAL;
230
231   floppy_image_read(floppy,buffer,offset,sector_length);
232
233   return FLOPPY_ERROR_SUCCESS;
234}
235
236static floperr_t d88_read_indexed_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buffer_len)
237{
238   int sec;
239
240   sec = d88_get_sector_id(floppy,head,track,sector);
241   return d88_read_sector(floppy,head,track,sec,buffer,buffer_len);
242}
243
244static floperr_t d88_write_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam)
245{
246   UINT64 offset;
247   UINT32 sector_length;
248
249   offset = d88_get_sector_offset(floppy,head,track,sector);
250
251   if(d88_get_sector_length(floppy,head,track,sector,&sector_length) != FLOPPY_ERROR_SUCCESS)
252      return FLOPPY_ERROR_SEEKERROR;
253
254   if(offset == 0)
255      return FLOPPY_ERROR_SEEKERROR;
256
257   if(buflen > sector_length)
258      return FLOPPY_ERROR_INTERNAL;
259
260   floppy_image_write(floppy,buffer,offset,sector_length);
261
262   return FLOPPY_ERROR_SUCCESS;
263}
264
265static floperr_t d88_write_indexed_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam)
266{
267   int sec;
268
269   sec = d88_get_sector_id(floppy,head,track,sector);
270   return d88_write_sector(floppy,head,track,sec,buffer,buflen,ddam);
271}
272
273static void d88_get_header(floppy_image* floppy,UINT32* size, UINT8* prot, UINT8* type, UINT32* offsets)
274{
275   UINT8 header[D88_HEADER_LEN];
276   int x,s;
277
278   floppy_image_read(floppy,header,0,D88_HEADER_LEN);
279
280#ifdef SPOT_DUPLICATES
281      // there exist many .d88 files with same data and different headers and
282      // this allows to spot duplicates, making easier to debug softlists.
283      UINT32 temp_size = floppy_image_size(floppy);
284      UINT8 tmp_copy[temp_size - D88_HEADER_LEN];
285      floppy_image_read(floppy,tmp_copy,D88_HEADER_LEN,temp_size - D88_HEADER_LEN);
286      printf("CRC16: %d\n", ccitt_crc16(0xffff, tmp_copy, temp_size - D88_HEADER_LEN));
287#endif
288
289   if(prot)
290      *prot = header[0x1a];
291   if(type)
292      *type = header[0x1b];
293   if(size)
294   {
295      s = 0;
296      s |= header[0x1f] << 24;
297      s |= header[0x1e] << 16;
298      s |= header[0x1d] << 8;
299      s |= header[0x1c];
300      *size = s;
301   }
302   if(offsets)
303   {
304      for(x=0;x<164;x++)
305      {
306         s = 0;
307         s |= header[0x23 + (x*4)] << 24;
308         s |= header[0x22 + (x*4)] << 16;
309         s |= header[0x21 + (x*4)] << 8;
310         s |= header[0x20 + (x*4)];
311         *(offsets+x) = s;
312      }
313   }
314}
315
316FLOPPY_IDENTIFY(d88_dsk_identify)
317{
318   UINT32 size;
319
320   d88_get_header(floppy,&size,NULL,NULL,NULL);
321
322   if(floppy_image_size(floppy) == size)
323   {
324      *vote = 100;
325   }
326   else
327   {
328      *vote = 0;
329   }
330   return FLOPPY_ERROR_SUCCESS;
331}
332
333FLOPPY_CONSTRUCT(d88_dsk_construct)
334{
335   struct FloppyCallbacks *callbacks;
336   struct d88_tag *tag;
337   UINT32 size;
338   UINT8 prot,type = 0;
339   UINT32 offs[164];
340   int x;
341
342   if(params)
343   {
344      // create
345      return FLOPPY_ERROR_UNSUPPORTED;
346   }
347   else
348   {
349      // load
350      d88_get_header(floppy,&size,&prot,&type,offs);
351   }
352
353   tag = (d88_tag *)floppy_create_tag(floppy,sizeof(struct d88_tag));
354   if (!tag)
355      return FLOPPY_ERROR_OUTOFMEMORY;
356
357   tag->write_protect = prot;
358   tag->disk_type = type;
359   tag->heads = 2;
360   if (tag->disk_type==0x30 || tag->disk_type==0x40) tag->heads = 1;
361
362   tag->image_size = size;
363   for(x=0;x<164;x++)
364      tag->trackoffset[x] = offs[x];
365
366   callbacks = floppy_callbacks(floppy);
367   callbacks->read_track = d88_read_track;
368   callbacks->get_heads_per_disk = d88_get_heads_per_disk;
369   callbacks->get_tracks_per_disk = d88_get_tracks_per_disk;
370   callbacks->get_sector_length = d88_get_sector_length;
371   callbacks->read_sector = d88_read_sector;
372   callbacks->read_indexed_sector = d88_read_indexed_sector;
373   callbacks->write_sector = d88_write_sector;
374   callbacks->write_indexed_sector = d88_write_indexed_sector;
375   callbacks->get_indexed_sector_info = d88_get_indexed_sector_info;
376   callbacks->get_sectors_per_track = d88_get_sectors_per_track;
377
378
379   return FLOPPY_ERROR_SUCCESS;
380}
src/mess/formats/uef_cas.c
r11457r11458
1/*
2
3UEF format support (for electron driver)
4
5The UEF format supports gzipped images, i'm doing the gunzip step during uef_cas_to_wav_size
6because that is when the length of the orignal file is known. This is needed to determine
7the size of memory to alloc for the decoding.
8
9Not nice, but it works...
10
11*/
12
13#include <string.h>
14
15#include "emu.h"
16#include "zlib.h"
17#include "formats/uef_cas.h"
18
19
20#define UEF_WAV_FREQUENCY   4800
21#define WAVE_LOW   -32768
22#define WAVE_HIGH   32767
23#define WAVE_NULL   0
24
25static const UINT8 UEF_HEADER[10] = { 0x55, 0x45, 0x46, 0x20, 0x46, 0x69, 0x6c, 0x65, 0x21, 0x00 };
26
27/*
28    bytes are stored as
29    start bit   1 * 0
30    data bits   8 * X
31    stop bit    1 * 1
32*/
33
34/* gzip flag byte */
35#define ASCII_FLAG   0x01 /* bit 0 set: file probably ascii text */
36#define HEAD_CRC   0x02 /* bit 1 set: header CRC present */
37#define EXTRA_FIELD   0x04 /* bit 2 set: extra field present */
38#define ORIG_NAME   0x08 /* bit 3 set: original file name present */
39#define COMMENT      0x10 /* bit 4 set: file comment present */
40#define RESERVED   0xE0 /* bits 5..7: reserved */
41
42static const UINT8* skip_gz_header( const UINT8 *p ) {
43   UINT8 method, flags;
44   /* skip initial 1f 8b header */
45   p += 2;
46   /* get method and flags */
47   method = *p; p++;
48   flags = *p; p++;
49   if ( method != Z_DEFLATED || ( flags & RESERVED ) != 0 ) {
50      return NULL;
51   }
52   /* Skip time, xflags and OS code */
53   p += 6;
54
55   /* Skip the extra field */
56   if ( ( flags & EXTRA_FIELD ) != 0 ) {
57      int len = ( p[1] << 8 ) | p[0];
58      p += 2 + len;
59   }
60   /* Skip the original file name */
61   if ( ( flags & ORIG_NAME ) != 0 ) {
62      for ( ; *p; p++ );
63   }
64   /* Skip the .gz file comment */
65   if ( ( flags & COMMENT ) != 0 ) {
66      for( ; *p; p++ );
67   }
68   /* Skip the header crc */
69   if ( ( flags & HEAD_CRC ) != 0 ) {
70      p += 2;
71   }
72   return p;
73}
74
75static UINT8 *gz_ptr = NULL;
76
77static float get_uef_float( const UINT8 *Float)
78{
79      int Mantissa;
80      float Result;
81      int Exponent;
82
83      /* assume a four byte array named Float exists, where Float[0]
84        was the first byte read from the UEF, Float[1] the second, etc */
85
86      /* decode mantissa */
87      Mantissa = Float[0] | (Float[1] << 8) | ((Float[2]&0x7f)|0x80) << 16;
88
89      Result = (float)Mantissa;
90      Result = (float)ldexp(Result, -23);
91
92      /* decode exponent */
93      Exponent = ((Float[2]&0x80) >> 7) | (Float[3]&0x7f) << 1;
94      Exponent -= 127;
95      Result = (float)ldexp(Result, Exponent);
96
97      /* flip sign if necessary */
98      if(Float[3]&0x80)
99         Result = -Result;
100
101   /* floating point number is now in 'Result' */
102   return Result;
103}
104
105static int uef_cas_to_wav_size( const UINT8 *casdata, int caslen ) {
106   int   pos, size;
107
108   if ( casdata[0] == 0x1f && casdata[1] == 0x8b ) {
109      int err;
110      z_stream d_stream;
111      int inflate_size = ( casdata[ caslen - 1 ] << 24 ) | ( casdata[ caslen - 2 ] << 16 ) | ( casdata[ caslen - 3 ] << 8 ) | casdata[ caslen - 4 ];
112      const UINT8 *in_ptr = skip_gz_header( casdata );
113
114      if ( in_ptr == NULL ) {
115         goto cleanup;
116      }
117      gz_ptr = (UINT8 *)malloc( inflate_size );
118
119      d_stream.zalloc = 0;
120      d_stream.zfree = 0;
121      d_stream.opaque = 0;
122      d_stream.next_in = (unsigned char *)in_ptr;
123      d_stream.avail_in = caslen - ( in_ptr - casdata );
124      d_stream.next_out = gz_ptr;
125      d_stream.avail_out = inflate_size;
126
127      err = inflateInit2( &d_stream, -MAX_WBITS );
128      if ( err != Z_OK ) {
129         logerror( "inflateInit2 error: %d\n", err );
130         goto cleanup;
131      }
132      err = inflate( &d_stream, Z_NO_FLUSH );
133      if ( err != Z_STREAM_END && err != Z_OK ) {
134         logerror( "inflate error: %d\n", err );
135         goto cleanup;
136      }
137      err = inflateEnd( &d_stream );
138      if ( err != Z_OK ) {
139         logerror( "inflateEnd error: %d\n", err );
140         goto cleanup;
141      }
142      caslen = inflate_size;
143      casdata = gz_ptr;
144   }
145
146   if ( caslen < 18 ) {
147      logerror( "uef_cas_to_wav_size: cassette image too small\n" );
148      goto cleanup;
149   }
150   if ( memcmp( casdata, UEF_HEADER, sizeof(UEF_HEADER) ) ) {
151      logerror( "uef_cas_to_wav_size: cassette image has incompatible header\n" );
152      goto cleanup;
153   }
154
155   logerror( "UEF: Determinig tape size\n" );
156   size = 0;
157   pos = sizeof(UEF_HEADER) + 2;
158   while( pos < caslen ) {
159      int chunk_type = ( casdata[pos+1] << 8 ) | casdata[pos];
160      int chunk_length = ( casdata[pos+5] << 24 ) | ( casdata[pos+4] << 16 ) | ( casdata[pos+3] << 8 ) | casdata[pos+2];
161      int baud_length;
162
163      pos += 6;
164      switch( chunk_type ) {
165      case 0x0100:   /* implicit start/stop bit data block */
166         size += ( chunk_length * 10 ) * 4;
167         break;
168      case 0x0101:   /* multiplexed data block */
169      case 0x0103:
170         logerror( "Unsupported chunk type: %04x\n", chunk_type );
171         break;
172      case 0x0102:   /* explicit tape data block */
173         size += ( ( chunk_length * 10 ) - casdata[pos] ) * 4;
174         break;
175      case 0x0104:
176         logerror( "Unsupported chunk type: %04x\n", chunk_type );
177         break;
178      case 0x0110:
179         baud_length = ( casdata[pos+1] << 8 ) | casdata[pos];
180         size += baud_length * 2;
181         break;
182      case 0x0111:
183         logerror( "Unsupported chunk type: %04x\n", chunk_type );
184         break;
185      case 0x0112:
186         baud_length = ( casdata[pos+1] << 8 ) | casdata[pos];
187         size += baud_length * 2 ;
188         break;
189      case 0x0116:
190         size += get_uef_float(casdata+pos)*UEF_WAV_FREQUENCY;
191         break;
192      case 0x0113:
193      case 0x0114:
194      case 0x0115:
195      case 0x0120:
196         logerror( "Unsupported chunk type: %04x\n", chunk_type );
197         break;
198      }
199      pos += chunk_length;
200   }
201   size = 2 * size;
202   return size;
203
204cleanup:
205   if ( gz_ptr ) {
206      free( gz_ptr );
207      gz_ptr = NULL;
208   }
209   return -1;
210}
211
212static INT16* uef_cas_fill_bit( INT16 *buffer, int bit ) {
213   if ( bit ) {
214      *buffer = WAVE_LOW; buffer++;
215      *buffer = WAVE_HIGH; buffer++;
216      *buffer = WAVE_LOW; buffer++;
217      *buffer = WAVE_HIGH; buffer++;
218   } else {
219      *buffer = WAVE_LOW; buffer++;
220      *buffer = WAVE_LOW; buffer++;
221      *buffer = WAVE_HIGH; buffer++;
222      *buffer = WAVE_HIGH; buffer++;
223   }
224   return buffer;
225}
226
227static int uef_cas_fill_wave( INT16 *buffer, int length, UINT8 *bytes ) {
228   int pos;
229   INT16 *p = buffer;
230
231   if ( bytes[0] == 0x1f && bytes[1] == 0x8b ) {
232      if ( gz_ptr == NULL ) {
233         return 1;
234      }
235      bytes = gz_ptr;
236   }
237
238   pos = sizeof(UEF_HEADER) + 2;
239   length = length / 2;
240   while( length > 0 ) {
241      int chunk_type = ( bytes[pos+1] << 8 ) | bytes[pos];
242      int chunk_length = ( bytes[pos+5] << 24 ) | ( bytes[pos+4] << 16 ) | ( bytes[pos+3] << 8 ) | bytes[pos+2];
243
244      int baud_length, i, j;
245      UINT8 *c;
246      pos += 6;
247      switch( chunk_type ) {
248      case 0x0100:    /* implicit start/stop bit data block */
249         for( j = 0; j < chunk_length; j++ ) {
250            UINT8 byte = bytes[pos+j];
251            p = uef_cas_fill_bit( p, 0 );
252            for( i = 0; i < 8; i++ ) {
253               p = uef_cas_fill_bit( p, byte & 1 );
254               byte = byte >> 1;
255            }
256            p = uef_cas_fill_bit( p, 1 );
257            length -= ( 10 * 4 );
258         }
259         break;
260      case 0x0101:    /* multiplexed data block */
261      case 0x0103:
262         logerror( "Unsupported chunk type: %04x\n", chunk_type );
263         break;
264      case 0x0102:    /* explicit tape data block */
265         j = ( chunk_length * 10 ) - bytes[pos];
266         c = bytes + pos;
267         while( j ) {
268            UINT8 byte = *c;
269            for( i = 0; i < 8 && i < j; i++ ) {
270               p = uef_cas_fill_bit( p, byte & 1 );
271               byte = byte >> 1;
272               j--;
273            }
274            c++;
275         }
276         break;
277      case 0x0110:
278         for( baud_length = ( ( bytes[pos+1] << 8 ) | bytes[pos] ) ; baud_length; baud_length-- ) {
279            *p = WAVE_LOW; p++;
280            *p = WAVE_HIGH; p++;
281            length -= 2;
282         }
283         break;
284      case 0x0112:
285         for( baud_length = ( ( bytes[pos+1] << 8 ) | bytes[pos] ) ; baud_length; baud_length-- ) {
286            *p = WAVE_NULL; p++;
287            *p = WAVE_NULL; p++;
288            length -= 2;
289         }
290         break;
291      case 0x0116:
292         for( baud_length = (get_uef_float(bytes+pos)*UEF_WAV_FREQUENCY); baud_length; baud_length-- ) {
293            *p = WAVE_NULL; p++;
294            length -= 1;
295         }
296         break;
297
298
299      }
300      pos += chunk_length;
301   }
302   return p - buffer;
303}
304
305static const struct CassetteLegacyWaveFiller uef_legacy_fill_wave = {
306   uef_cas_fill_wave,            /* fill_wave */
307   -1,                  /* chunk_size */
308   0,                  /* chunk_samples */
309   uef_cas_to_wav_size,            /* chunk_sample_calc */
310   UEF_WAV_FREQUENCY,            /* sample_frequency */
311   0,                  /* header_samples */
312   0                  /* trailer_samples */
313};
314
315static casserr_t uef_cassette_identify( cassette_image *cassette, struct CassetteOptions *opts ) {
316   return cassette_legacy_identify( cassette, opts, &uef_legacy_fill_wave );
317}
318
319static casserr_t uef_cassette_load( cassette_image *cassette ) {
320   return cassette_legacy_construct( cassette, &uef_legacy_fill_wave );
321}
322
323const struct CassetteFormat uef_cassette_format = {
324   "uef",
325   uef_cassette_identify,
326   uef_cassette_load,
327   NULL
328};
329
330CASSETTE_FORMATLIST_START(uef_cassette_formats)
331   CASSETTE_FORMAT(uef_cassette_format)
332CASSETTE_FORMATLIST_END
333
src/mess/formats/fmsx_cas.h
r11457r11458
1/*********************************************************************
2
3    fmsx_cas.h
4
5    Format code for MSX casette files
6
7*********************************************************************/
8
9#ifndef FMSX_CAS_H
10#define FMSX_CAS_H
11
12#include "imagedev/cassimg.h"
13
14CASSETTE_FORMATLIST_EXTERN(fmsx_cassette_formats);
15
16#endif /* FMSX_CAS_H */
src/mess/formats/dim_dsk.c
r11457r11458
1/*********************************************************************
2
3    formats/dim_dsk.c
4
5    DIM disk images
6
7*********************************************************************/
8
9#include <string.h>
10
11#include "formats/dim_dsk.h"
12#include "formats/basicdsk.h"
13#include "emu.h"
14
15FLOPPY_IDENTIFY(dim_dsk_identify)
16{
17   UINT8 dim_header[16];
18
19   floppy_image_read(floppy, (UINT8*)dim_header,0xab,16);
20
21   if(strncmp((const char*)dim_header,"DIFC HEADER",11) == 0)
22      *vote = 100;
23   else
24      *vote = 0;
25   return FLOPPY_ERROR_SUCCESS;
26}
27
28FLOPPY_CONSTRUCT(dim_dsk_construct)
29{
30   struct basicdsk_geometry geometry;
31   // DIM disk image header, most of this is guesswork
32   int tracks = 77;
33   int heads = 2;
34   int sectors = 8;  // per track
35   int sectorlen = 1024;
36   int firstsector = 0x01;
37   UINT8 format_tmp;
38   int x;
39   UINT16 temp;
40
41   if(params)
42   {
43      // create
44      return FLOPPY_ERROR_UNSUPPORTED;
45   }
46
47   // Offset + 0 : disk format type (1 byte):
48    //  0 = 2HD / 2HDA (8 sector/track, 1024 bytes/sector, GAP#3 = 0x74)
49    //  1 = 2HS        (9 sector/track, 1024 bytes/sector, GAP#3 = 0x39)
50    //  2 = 2HC        (15 sector/track, 512 bytes/sector, GAP#3 = 0x54)
51    //  3 = 2HDE(68)   (9 sector/track, 1024 bytes/sector, GAP#3 = 0x39)
52    //  9 = 2HQ        (18 sector/track, 512 bytes/sector, GAP#3 = 0x54)
53    //  17 = N88-BASIC (26 sector/track, 256 bytes/sector, GAP#3 = 0x33)
54    //              or (26 sector/track, 128 bytes/sector, GAP#3 = 0x1a)
55    //
56   floppy_image_read(floppy, &format_tmp,0,1);
57
58   switch(format_tmp)
59   {
60   case 0x00:
61      sectors = 8;
62      sectorlen = 1024;
63      break;
64   case 0x01:
65   case 0x03:
66      sectors = 9;
67      sectorlen = 1024;
68      break;
69   case 0x02:
70      sectors = 15;
71      sectorlen = 512;
72      break;
73   case 0x09:
74      sectors = 18;
75      sectorlen = 512;
76      break;
77   case 0x11:
78      sectors = 26;
79      sectorlen = 256;
80      break;
81   }
82   tracks = 0;
83   for (x=0;x<86;x++)
84   {
85      floppy_image_read(floppy,&temp,(x*2)+1,2);
86      if(temp == 0x0101)
87         tracks++;
88   }
89   // TODO: expand on this basic implementation
90
91   logerror("FDD: DIM image loaded - type %i, %i tracks, %i sectors per track, %i bytes per sector\n", format_tmp,tracks, sectors,sectorlen);
92
93   memset(&geometry, 0, sizeof(geometry));
94   geometry.heads = heads;
95   geometry.first_sector_id = firstsector;
96   geometry.sector_length = sectorlen;
97   geometry.tracks = tracks+1;
98   geometry.sectors = sectors;
99   geometry.offset = 0x100;
100   return basicdsk_construct(floppy, &geometry);
101}
102
src/mess/formats/uef_cas.h
r11457r11458
1/*
2    uef_cas.h
3
4    Format code for uef cassette files
5*/
6
7#ifndef UEF_CAS_H
8#define UEF_CAS_H
9
10#include "imagedev/cassimg.h"
11
12extern const struct CassetteFormat uef_cassette_format;
13
14CASSETTE_FORMATLIST_EXTERN(uef_cassette_formats);
15
16#endif /* UEF_CAS_H */
src/mess/formats/tzx_cas.c
r11457r11458
1/*
2
3TZX (currently spectrum only) and spectrum TAP cassette format support by Wilbert Pol
4
5TODO:
6    Add support for the remaining block types:
7        case 0x15:  Direct Recording
8        case 0x18:  CSW Recording
9        case 0x19:  Generalized Data Block
10        case 0x21:  Group Start
11        case 0x22:  Group End
12        case 0x23:  Jump To Block
13        case 0x24:  Loop Start
14        case 0x25:  Loop End
15        case 0x26:  Call Sequence
16        case 0x27:  Return From Sequence
17        case 0x28:  Select Block
18        case 0x2A:  Stop Tape if in 48K Mode
19        case 0x2B:  Set signal level
20        case 0x5A:  Merge Block
21    Add support for the deprecated block types? Only if there is some image which need them:
22        case 0x16:  C64 ROM type data block
23        case 0x17:  C64 turbo tape data block
24        case 0x34:  Emulation info
25        case 0x40:  Snapshot block
26
27Notes:
28
29TZX format specification lists
308064 pulses for a header block and 3220 for a data block
31
32but the documentaiton on worldofspectrum lists
338063 pulses for a header block and 3223 for a data block
34
35see http://www.worldofspectrum.org/faq/reference/48kreference.htm#TapeDataStructure
36
37We are currently using the numbers from the TZX specification...
38
39*/
40
41#include "emu.h"
42#include "formats/tzx_cas.h"
43
44
45#define TZX_WAV_FREQUENCY   44100
46#define WAVE_LOW      -0x5a9e
47#define WAVE_HIGH      0x5a9e
48#define WAVE_NULL      0
49
50#define SUPPORTED_VERSION_MAJOR   0x01
51
52#define INITIAL_MAX_BLOCK_COUNT   256
53#define BLOCK_COUNT_INCREMENTS   256
54
55static const UINT8 TZX_HEADER[8] = { 'Z','X','T','a','p','e','!',0x1a };
56
57/*
58  Global variables
59
60  Initialized by tzx_cas_get_wave_size, used (and cleaned up) by tzx_cas_fill_wave
61 */
62
63static INT16   wave_data = 0;
64static int   block_count = 0;
65static UINT8**   blocks = NULL;
66static float t_scale = 1;  /* for scaling T-states to the 4MHz CPC */
67
68static void toggle_wave_data(void)
69{
70   if (wave_data == WAVE_LOW)
71   {
72      wave_data = WAVE_HIGH;
73   }
74   else
75   {
76      wave_data = WAVE_LOW;
77   }
78}
79
80static void tzx_cas_get_blocks( const UINT8 *casdata, int caslen )
81{
82   int pos = sizeof(TZX_HEADER) + 2;
83   int max_block_count = INITIAL_MAX_BLOCK_COUNT;
84
85   blocks = (UINT8**)malloc(max_block_count);
86   memset(blocks,0,max_block_count);
87   block_count = 0;
88
89   while (pos < caslen)
90   {
91      UINT32 datasize;
92      UINT8 blocktype = casdata[pos];
93
94      if (block_count == max_block_count)
95      {
96         void   *old_blocks = blocks;
97         int   old_max_block_count = max_block_count;
98         printf("realoc");
99         max_block_count = max_block_count + BLOCK_COUNT_INCREMENTS;
100         blocks = (UINT8**)malloc(max_block_count);   // SHOULD NOT BE USING auto_alloc_array()
101         memset(blocks,0,max_block_count);
102         memcpy(blocks, old_blocks, old_max_block_count * sizeof(UINT8*));
103         if (blocks) free(old_blocks);
104      }
105
106      blocks[block_count] = (UINT8*)&casdata[pos];
107
108      pos += 1;
109
110      switch (blocktype)
111      {
112      case 0x10:
113         pos += 2;
114         datasize = casdata[pos] + (casdata[pos + 1] << 8);
115         pos += 2 + datasize;
116         break;
117      case 0x11:
118         pos += 0x0f;
119         datasize = casdata[pos] + (casdata[pos + 1] << 8) + (casdata[pos + 2] << 16);
120         pos += 3 + datasize;
121         break;
122      case 0x12:
123         pos += 4;
124         break;
125      case 0x13:
126         datasize = casdata[pos];
127         pos += 1 + 2 * datasize;
128         break;
129      case 0x14:
130         pos += 7;
131         datasize = casdata[pos] + (casdata[pos + 1] << 8) + (casdata[pos + 2] << 16);
132         pos += 3 + datasize;
133         break;
134      case 0x15:
135         pos += 5;
136         datasize = casdata[pos] + (casdata[pos + 1] << 8) + (casdata[pos + 2] << 16);
137         pos += 3 + datasize;
138         break;
139      case 0x20: case 0x23: case 0x24:
140         pos += 2;
141         break;
142      case 0x21: case 0x30:
143         datasize = casdata[pos];
144         pos += 1 + datasize;
145         break;
146      case 0x22: case 0x25: case 0x27:
147         break;
148      case 0x26:
149         datasize = casdata[pos] + (casdata[pos + 1] << 8);
150         pos += 2 + 2 * datasize;
151         break;
152      case 0x28: case 0x32:
153         datasize = casdata[pos] + (casdata[pos + 1] << 8);
154         pos += 2 + datasize;
155         break;
156      case 0x31:
157         pos += 1;
158         datasize = casdata[pos];
159         pos += 1 + datasize;
160         break;
161      case 0x33:
162         datasize = casdata[pos];
163         pos += 1 + 3 * datasize;
164         break;
165      case 0x34:
166         pos += 8;
167         break;
168      case 0x35:
169         pos += 0x10;
170         datasize = casdata[pos] + (casdata[pos + 1] << 8) + (casdata[pos + 2] << 16) + (casdata[pos + 3] << 24);
171         pos += 4 + datasize;
172         break;
173      case 0x40:
174         pos += 1;
175         datasize = casdata[pos] + (casdata[pos + 1] << 8) + (casdata[pos + 2] << 16);
176         pos += 3 + datasize;
177         break;
178      case 0x5A:
179         pos += 9;
180         break;
181      default:
182         datasize = casdata[pos] + (casdata[pos + 1] << 8) + (casdata[pos + 2] << 16) + (casdata[pos + 3] << 24);
183         pos += 4 + datasize;
184         break;
185      }
186
187      block_count++;
188   }
189}
190
191INLINE int millisec_to_samplecount( int millisec )
192{
193   return (int) (millisec * ((double)TZX_WAV_FREQUENCY / 1000.0));
194}
195
196INLINE int tcycles_to_samplecount( int tcycles )
197{
198   return (int) ((0.5 + (((double)TZX_WAV_FREQUENCY / 3500000) * (double)tcycles)) * t_scale);
199}
200
201static void tzx_output_wave( INT16 **buffer, int length )
202{
203   if (buffer == NULL)
204   {
205      return;
206   }
207
208   for ( ; length > 0; length--)
209   {
210      **buffer = wave_data;
211      *buffer = *buffer + 1;
212   }
213}
214
215static int tzx_cas_handle_block( INT16 **buffer, const UINT8 *bytes, int pause, int data_size, int pilot, int pilot_length, int sync1, int sync2, int bit0, int bit1, int bits_in_last_byte )
216{
217   int pilot_samples = tcycles_to_samplecount(pilot);
218   int sync1_samples = tcycles_to_samplecount(sync1);
219   int sync2_samples = tcycles_to_samplecount(sync2);
220   int bit0_samples = tcycles_to_samplecount(bit0);
221   int bit1_samples = tcycles_to_samplecount(bit1);
222   int data_index;
223   int size = 0;
224
225   /* Uncomment this to include into error.log a fully detailed analysis of each block */
226//  logerror("tzx_cas_block_size: pilot_length = %d, pilot_samples = %d, sync1_samples = %d, sync2_samples = %d, bit0_samples = %d, bit1_samples = %d\n", pilot_length, pilot_samples, sync1_samples, sync2_samples, bit0_samples, bit1_samples);
227
228   /* PILOT */
229   for ( ; pilot_length > 0; pilot_length--)
230   {
231      tzx_output_wave(buffer, pilot_samples);
232      size += pilot_samples;
233      toggle_wave_data();
234   }
235   /* SYNC1 */
236   if (sync1_samples > 0)
237   {
238      tzx_output_wave(buffer, sync1_samples);
239      size += sync1_samples;
240      toggle_wave_data();
241   }
242   /* SYNC2 */
243   if (sync2_samples > 0)
244   {
245      tzx_output_wave(buffer, sync2_samples);
246      size += sync2_samples;
247      toggle_wave_data();
248   }
249   /* data */
250   for (data_index = 0; data_index < data_size; data_index++)
251   {
252      UINT8 byte = bytes[data_index];
253      int   bits_to_go = (data_index == (data_size - 1)) ? bits_in_last_byte : 8;
254
255      for ( ; bits_to_go > 0; byte <<= 1, bits_to_go--)
256      {
257         int   bit_samples = (byte & 0x80) ? bit1_samples : bit0_samples;
258         tzx_output_wave(buffer, bit_samples);
259         size += bit_samples;
260         toggle_wave_data();
261         tzx_output_wave(buffer, bit_samples);
262         size += bit_samples;
263         toggle_wave_data();
264      }
265   }
266   /* pause */
267   if (pause > 0)
268   {
269      int start_pause_samples = millisec_to_samplecount(1);
270      int rest_pause_samples = millisec_to_samplecount(pause - 1);
271
272      tzx_output_wave(buffer, start_pause_samples);
273      size += start_pause_samples;
274      wave_data = WAVE_LOW;
275      tzx_output_wave(buffer, rest_pause_samples);
276      size += rest_pause_samples;
277   }
278   return size;
279}
280
281static void ascii_block_common_log( const char *block_type_string, UINT8 block_type )
282{
283   logerror("%s (type %02x) encountered.\n", block_type_string, block_type);
284   logerror("This block contains info on the .tzx file you are loading.\n");
285   logerror("Please include the following info in your bug reports, if the image has issue in M.E.S.S.\n");
286}
287
288static const char *const archive_ident[] =
289{
290   "Full title",
291   "Software house/publisher",
292   "Author(s)",
293   "Year of publication",
294   "Language",
295   "Game/utility type",
296   "Price",
297   "Protection scheme/loader",
298   "Origin",
299};
300
301static const char *const hw_info[] =
302{
303   "Tape runs on this machine / this hardware",
304   "Tape needs this machine / this hardware",
305   "Tape runs on this machine / this hardware, but does not require its special features",
306   "Tape does not run on this machine / this hardware",
307};
308
309/*  Will go through blocks and calculate number of samples needed.
310If buffer is not NULL the sample data will also be written. */
311static int tzx_cas_do_work( INT16 **buffer )
312{
313   int   current_block = 0;
314   int   size = 0;
315
316   wave_data = WAVE_LOW;
317
318   while (current_block < block_count)
319   {
320      int   pause_time;
321      int   data_size, text_size, total_size, i;
322      int   pilot, pilot_length, sync1, sync2;
323      int   bit0, bit1, bits_in_last_byte;
324      UINT8 *cur_block = blocks[current_block];
325      UINT8 block_type = cur_block[0];
326
327   /* Uncomment this to include into error.log a list of the types each block */
328//      logerror("tzx_cas_fill_wave: block %d, block_type %02x\n", current_block, block_type);
329
330      switch (block_type)
331      {
332      case 0x10:   /* Standard Speed Data Block (.TAP block) */
333         pause_time = cur_block[1] + (cur_block[2] << 8);
334         data_size = cur_block[3] + (cur_block[4] << 8);
335         pilot_length = (cur_block[5] == 0x00) ?  8064 : 3220;
336         size += tzx_cas_handle_block(buffer, &cur_block[5], pause_time, data_size, 2168, pilot_length, 667, 735, 855, 1710, 8);
337         current_block++;
338         break;
339      case 0x11:   /* Turbo Loading Data Block */
340         pilot = cur_block[1] + (cur_block[2] << 8);
341         sync1 = cur_block[3] + (cur_block[4] << 8);
342         sync2 = cur_block[5] + (cur_block[6] << 8);
343         bit0 = cur_block[7] + (cur_block[8] << 8);
344         bit1 = cur_block[9] + (cur_block[10] << 8);
345         pilot_length = cur_block[11] + (cur_block[12] << 8);
346         bits_in_last_byte = cur_block[13];
347         pause_time = cur_block[14] + (cur_block[15] << 8);
348         data_size = cur_block[16] + (cur_block[17] << 8) + (cur_block[18] << 16);
349         size += tzx_cas_handle_block(buffer, &cur_block[19], pause_time, data_size, pilot, pilot_length, sync1, sync2, bit0, bit1, bits_in_last_byte);
350         current_block++;
351         break;
352      case 0x12:   /* Pure Tone */
353         pilot = cur_block[1] + (cur_block[2] << 8);
354         pilot_length = cur_block[3] + (cur_block[4] << 8);
355         size += tzx_cas_handle_block(buffer, cur_block, 0, 0, pilot, pilot_length, 0, 0, 0, 0, 0);
356         current_block++;
357         break;
358      case 0x13:   /* Sequence of Pulses of Different Lengths */
359         for (data_size = 0; data_size < cur_block[1]; data_size++)
360         {
361            pilot = cur_block[2 + 2 * data_size] + (cur_block[3 + 2 * data_size] << 8);
362            size += tzx_cas_handle_block(buffer, cur_block, 0, 0, pilot, 1, 0, 0, 0, 0, 0);
363         }
364         current_block++;
365         break;
366      case 0x14:   /* Pure Data Block */
367         bit0 = cur_block[1] + (cur_block[2] << 8);
368         bit1 = cur_block[3] + (cur_block[4] << 8);
369         bits_in_last_byte = cur_block[5];
370         pause_time = cur_block[6] + (cur_block[7] << 8);
371         data_size = cur_block[8] + (cur_block[9] << 8) + (cur_block[10] << 16);
372         size += tzx_cas_handle_block(buffer, &cur_block[11], pause_time, data_size, 0, 0, 0, 0, bit0, bit1, bits_in_last_byte);
373         current_block++;
374         break;
375      case 0x20:   /* Pause (Silence) or 'Stop the Tape' Command */
376         pause_time = cur_block[1] + (cur_block[2] << 8);
377         if (pause_time == 0)
378         {
379            /* pause = 0 is used to let an emulator automagically stop the tape
380                   in MESS we do not do that, so we insert a 5 second pause. */
381            pause_time = 5000;
382         }
383         size += tzx_cas_handle_block(buffer, cur_block, pause_time, 0, 0, 0, 0, 0, 0, 0, 0);
384         current_block++;
385         break;
386      case 0x16:   /* C64 ROM Type Data Block */      // Deprecated in TZX 1.20
387      case 0x17:   /* C64 Turbo Tape Data Block */      // Deprecated in TZX 1.20
388      case 0x34:   /* Emulation Info */            // Deprecated in TZX 1.20
389      case 0x40:   /* Snapshot Block */            // Deprecated in TZX 1.20
390         logerror("Deprecated block type (%02x) encountered.\n", block_type);
391         logerror("Please look for an updated .tzx file.\n");
392         current_block++;
393         break;
394      case 0x30:   /* Text Description */
395         ascii_block_common_log("Text Description Block", block_type);
396         for (data_size = 0; data_size < cur_block[1]; data_size++)
397            logerror("%c", cur_block[2 + data_size]);
398         logerror("\n");
399         current_block++;
400         break;
401      case 0x31:   /* Message Block */
402         ascii_block_common_log("Message Block", block_type);
403         logerror("Expected duration of the message display: %02x\n", cur_block[1]);
404         logerror("Message: \n");
405         for (data_size = 0; data_size < cur_block[2]; data_size++)
406         {
407            logerror("%c", cur_block[3 + data_size]);
408            if (cur_block[3 + data_size] == 0x0d)
409               logerror("\n");
410         }
411         logerror("\n");
412         current_block++;
413         break;
414      case 0x32:   /* Archive Info */
415         ascii_block_common_log("Archive Info Block", block_type);
416         total_size = cur_block[1] + (cur_block[2] << 8);
417         text_size = 0;
418         for (data_size = 0; data_size < cur_block[3]; data_size++)   // data_size = number of text blocks, in this case
419         {
420            if (cur_block[4 + text_size] < 0x09)
421               logerror("%s: \n", archive_ident[cur_block[4 + text_size]]);
422            else
423               logerror("Comment(s): \n");
424
425            for (i = 0; i < cur_block[4 + text_size + 1]; i++)
426            {
427               logerror("%c", cur_block[4 + text_size + 2 + i]);
428            }
429            text_size += 2 + i;
430         }
431         logerror("\n");
432         if (text_size != total_size)
433            logerror("Malformed Archive Info Block (Text length different from the declared one).\n Please verify your tape image.\n");
434         current_block++;
435         break;
436      case 0x33:   /* Hardware Type */
437         ascii_block_common_log("Hardware Type Block", block_type);
438         for (data_size = 0; data_size < cur_block[1]; data_size++)   // data_size = number of hardware blocks, in this case
439         {
440            logerror("Hardware Type %02x - Hardware ID %02x - ", cur_block[2 + data_size * 3], cur_block[2 + data_size * 3 + 1]);
441            logerror("%s \n ", hw_info[cur_block[2 + data_size * 3 + 2]]);
442         }
443         current_block++;
444         break;
445      case 0x35:   /* Custom Info Block */
446         ascii_block_common_log("Custom Info Block", block_type);
447         for (data_size = 0; data_size < 10; data_size++)
448         {
449            logerror("%c", cur_block[1 + data_size]);
450         }
451         logerror(":\n");
452         text_size = cur_block[11] + (cur_block[12] << 8) + (cur_block[13] << 16) + (cur_block[14] << 24);
453         for (data_size = 0; data_size < text_size; data_size++)
454            logerror("%c", cur_block[15 + data_size]);
455         logerror("\n");
456         current_block++;
457         break;
458      case 0x5A:   /* "Glue" Block */
459         logerror("Glue Block (type %02x) encountered.\n", block_type);
460         logerror("Please use a .tzx handling utility to split the merged tape files.\n");
461         current_block++;
462         break;
463      case 0x15:   /* Direct Recording */
464      case 0x18:   /* CSW Recording */
465      case 0x19:   /* Generalized Data Block */
466      case 0x21:   /* Group Start */
467      case 0x22:   /* Group End */
468      case 0x23:   /* Jump To Block */
469      case 0x24:   /* Loop Start */
470      case 0x25:   /* Loop End */
471      case 0x26:   /* Call Sequence */
472      case 0x27:   /* Return From Sequence */
473      case 0x28:   /* Select Block */
474      case 0x2A:   /* Stop Tape if in 48K Mode */
475      case 0x2B:   /* Set signal level */
476      default:
477         logerror("Unsupported block type (%02x) encountered.\n", block_type);
478         current_block++;
479         break;
480      }
481   }
482   return size;
483}
484
485static int tzx_cas_to_wav_size( const UINT8 *casdata, int caslen )
486{
487   int size = 0;
488
489   /* Header size plus major and minor version number */
490   if (caslen < 10)
491   {
492      logerror("tzx_cas_to_wav_size: cassette image too small\n");
493      goto cleanup;
494   }
495
496   /* Check for correct header */
497   if (memcmp(casdata, TZX_HEADER, sizeof(TZX_HEADER)))
498   {
499      logerror("tzx_cas_to_wav_size: cassette image has incompatible header\n");
500      goto cleanup;
501   }
502
503   /* Check major version number in header */
504   if (casdata[0x08] > SUPPORTED_VERSION_MAJOR)
505   {
506      logerror("tzx_cas_to_wav_size: unsupported version\n");
507      goto cleanup;
508   }
509
510   tzx_cas_get_blocks(casdata, caslen);
511
512   logerror("tzx_cas_to_wav_size: %d blocks found\n", block_count);
513
514   if (block_count == 0)
515   {
516      logerror("tzx_cas_to_wav_size: no blocks found!\n");
517      goto cleanup;
518   }
519
520   size = tzx_cas_do_work(NULL);
521
522   return size;
523
524cleanup:
525   return -1;
526}
527
528static int tzx_cas_fill_wave( INT16 *buffer, int length, UINT8 *bytes )
529{
530   INT16 *p = buffer;
531   int   size = 0;
532   t_scale = 1.0;
533   size = tzx_cas_do_work(&p);
534   return size;
535}
536
537static int cdt_cas_fill_wave( INT16 *buffer, int length, UINT8 *bytes )
538{
539   INT16 *p = buffer;
540   int   size = 0;
541   t_scale = (40 / 35);  /* scale to 4MHz */
542   size = tzx_cas_do_work(&p);
543   return size;
544}
545
546static int tap_cas_to_wav_size( const UINT8 *casdata, int caslen )
547{
548   int size = 0;
549   const UINT8 *p = casdata;
550
551   while (p < casdata + caslen)
552   {
553      int data_size = p[0] + (p[1] << 8);
554      int pilot_length = (p[2] == 0x00) ? 8064 : 3220;   /* TZX specification */
555//      int pilot_length = (p[2] == 0x00) ? 8063 : 3223;    /* worldofspectrum */
556      logerror("tap_cas_to_wav_size: Handling TAP block containing 0x%X bytes", data_size);
557      p += 2;
558      size += tzx_cas_handle_block(NULL, p, 1000, data_size, 2168, pilot_length, 667, 735, 855, 1710, 8);
559      logerror(", total size is now: %d\n", size);
560      p += data_size;
561   }
562   return size;
563}
564
565static int tap_cas_fill_wave( INT16 *buffer, int length, UINT8 *bytes )
566{
567   INT16 *p = buffer;
568   int size = 0;
569
570   while (size < length)
571   {
572      int data_size = bytes[0] + (bytes[1] << 8);
573      int pilot_length = (bytes[2] == 0x00) ? 8064 : 3220;   /* TZX specification */
574//      int pilot_length = (bytes[2] == 0x00) ? 8063 : 3223;    /* worldofspectrum */
575      logerror("tap_cas_fill_wave: Handling TAP block containing 0x%X bytes\n", data_size);
576      bytes += 2;
577      size += tzx_cas_handle_block(&p, bytes, 1000, data_size, 2168, pilot_length, 667, 735, 855, 1710, 8);
578      bytes += data_size;
579   }
580   return size;
581}
582
583static const struct CassetteLegacyWaveFiller tzx_legacy_fill_wave =
584{
585   tzx_cas_fill_wave,         /* fill_wave */
586   -1,               /* chunk_size */
587   0,               /* chunk_samples */
588   tzx_cas_to_wav_size,         /* chunk_sample_calc */
589   TZX_WAV_FREQUENCY,         /* sample_frequency */
590   0,               /* header_samples */
591   0               /* trailer_samples */
592};
593
594static const struct CassetteLegacyWaveFiller tap_legacy_fill_wave =
595{
596   tap_cas_fill_wave,         /* fill_wave */
597   -1,               /* chunk_size */
598   0,               /* chunk_samples */
599   tap_cas_to_wav_size,         /* chunk_sample_calc */
600   TZX_WAV_FREQUENCY,         /* sample_frequency */
601   0,               /* header_samples */
602   0               /* trailer_samples */
603};
604
605static const struct CassetteLegacyWaveFiller cdt_legacy_fill_wave =
606{
607   cdt_cas_fill_wave,         /* fill_wave */
608   -1,               /* chunk_size */
609   0,               /* chunk_samples */
610   tzx_cas_to_wav_size,         /* chunk_sample_calc */
611   TZX_WAV_FREQUENCY,         /* sample_frequency */
612   0,               /* header_samples */
613   0               /* trailer_samples */
614};
615
616static casserr_t tzx_cassette_identify( cassette_image *cassette, struct CassetteOptions *opts )
617{
618   return cassette_legacy_identify(cassette, opts, &tzx_legacy_fill_wave);
619}
620
621static casserr_t tap_cassette_identify( cassette_image *cassette, struct CassetteOptions *opts )
622{
623   return cassette_legacy_identify(cassette, opts, &tap_legacy_fill_wave);
624}
625
626static casserr_t cdt_cassette_identify( cassette_image *cassette, struct CassetteOptions *opts )
627{
628   return cassette_legacy_identify(cassette, opts, &cdt_legacy_fill_wave);
629}
630
631static casserr_t tzx_cassette_load( cassette_image *cassette )
632{
633   return cassette_legacy_construct(cassette, &tzx_legacy_fill_wave);
634}
635
636static casserr_t tap_cassette_load( cassette_image *cassette )
637{
638   return cassette_legacy_construct(cassette, &tap_legacy_fill_wave);
639}
640
641static casserr_t cdt_cassette_load( cassette_image *cassette )
642{
643   return cassette_legacy_construct(cassette, &cdt_legacy_fill_wave);
644}
645
646static const struct CassetteFormat tzx_cassette_format =
647{
648   "tzx",
649   tzx_cassette_identify,
650   tzx_cassette_load,
651   NULL
652};
653
654static const struct CassetteFormat tap_cassette_format =
655{
656   "tap,blk",
657   tap_cassette_identify,
658   tap_cassette_load,
659   NULL
660};
661
662static const struct CassetteFormat cdt_cassette_format =
663{
664   "cdt",
665   cdt_cassette_identify,
666   cdt_cassette_load,
667   NULL
668};
669
670CASSETTE_FORMATLIST_START(tzx_cassette_formats)
671   CASSETTE_FORMAT(tzx_cassette_format)
672   CASSETTE_FORMAT(tap_cassette_format)
673CASSETTE_FORMATLIST_END
674
675CASSETTE_FORMATLIST_START(cdt_cassette_formats)
676   CASSETTE_FORMAT(cdt_cassette_format)
677CASSETTE_FORMATLIST_END
678
src/mess/formats/dim_dsk.h
r11457r11458
1/*********************************************************************
2
3    formats/dim_dsk.h
4
5    DIM disk images
6
7*********************************************************************/
8
9#ifndef DIM_DSK_H
10#define DIM_DSK_H
11
12#include "imagedev/flopimg.h"
13
14/**************************************************************************/
15
16FLOPPY_IDENTIFY(dim_dsk_identify);
17FLOPPY_CONSTRUCT(dim_dsk_construct);
18
19#endif /* DIM_DSK_H */
src/mess/formats/vt_cas.c
r11457r11458
1#include "formats/vt_cas.h"
2
3/*********************************************************************
4    vtech 1/2 agnostic
5*********************************************************************/
6
7#define SILENCE 8000
8
9static int generic_fill_wave(INT16 *buffer, int length, UINT8 *code, int bitsamples, int bytesamples, int lo, INT16 *(*fill_wave_byte)(INT16 *buffer, int byte))
10{
11   static int nullbyte;
12
13    if( code == CODE_HEADER )
14    {
15      int i;
16
17        if( length < SILENCE )
18         return -1;
19      for( i = 0; i < SILENCE; i++ )
20         *buffer++ = 0;
21
22      nullbyte = 0;
23
24        return SILENCE;
25    }
26
27   if( code == CODE_TRAILER )
28    {
29      int i;
30
31        /* silence at the end */
32      for( i = 0; i < length; i++ )
33         *buffer++ = 0;
34        return length;
35    }
36
37    if( length < bytesamples )
38      return -1;
39
40   buffer = fill_wave_byte(buffer, *code);
41
42    if( !nullbyte && *code == 0 )
43   {
44      int i;
45      for( i = 0; i < 2*bitsamples; i++ )
46         *buffer++ = lo;
47        nullbyte = 1;
48      return bytesamples + 2 * bitsamples;
49   }
50
51    return bytesamples;
52}
53
54
55/*********************************************************************
56    vtech 1
57*********************************************************************/
58
59#define V1_LO   -32768
60#define V1_HI   +32767
61
62#define V1_BITSAMPLES   6
63#define V1_BYTESAMPLES   8*V1_BITSAMPLES
64
65static INT16 *vtech1_fill_wave_byte(INT16 *buffer, int byte)
66{
67   int i;
68
69    for( i = 7; i >= 0; i-- )
70   {
71      *buffer++ = V1_HI;   /* initial cycle */
72      *buffer++ = V1_LO;
73      if( (byte >> i) & 1 )
74      {
75         *buffer++ = V1_HI; /* two more cycles */
76            *buffer++ = V1_LO;
77         *buffer++ = V1_HI;
78            *buffer++ = V1_LO;
79        }
80      else
81      {
82         *buffer++ = V1_HI; /* one slow cycle */
83         *buffer++ = V1_HI;
84         *buffer++ = V1_LO;
85         *buffer++ = V1_LO;
86        }
87   }
88    return buffer;
89}
90
91static int vtech1_cassette_fill_wave(INT16 *buffer, int length, UINT8 *code)
92{
93   return generic_fill_wave(buffer, length, code, V1_BITSAMPLES, V1_BYTESAMPLES, V1_LO, vtech1_fill_wave_byte);
94}
95
96static const struct CassetteLegacyWaveFiller vtech1_legacy_fill_wave =
97{
98   vtech1_cassette_fill_wave,   /* fill_wave */
99   1,                     /* chunk_size */
100   V1_BYTESAMPLES,            /* chunk_samples */
101   NULL,                  /* chunk_sample_calc */
102   600*V1_BITSAMPLES,         /* sample_frequency */
103   600*V1_BITSAMPLES,         /* header_samples */
104   600*V1_BITSAMPLES         /* trailer_samples */
105};
106
107static casserr_t vtech1_cas_identify(cassette_image *cassette, struct CassetteOptions *opts)
108{
109   return cassette_legacy_identify(cassette, opts, &vtech1_legacy_fill_wave);
110}
111
112static casserr_t vtech1_cas_load(cassette_image *cassette)
113{
114   return cassette_legacy_construct(cassette, &vtech1_legacy_fill_wave);
115}
116
117static const struct CassetteFormat vtech1_cas_format =
118{
119   "cas",
120   vtech1_cas_identify,
121   vtech1_cas_load,
122   NULL
123};
124
125CASSETTE_FORMATLIST_START(vtech1_cassette_formats)
126   CASSETTE_FORMAT(vtech1_cas_format)
127CASSETTE_FORMATLIST_END
128
129/*********************************************************************
130    vtech 2
131*********************************************************************/
132
133#define VT2_LO   -20000
134#define VT2_HI   +20000
135
136#define VT2_BITSAMPLES   18
137#define VT2_BYTESAMPLES 8*VT2_BITSAMPLES
138
139static const INT16 vtech2_bit0[VT2_BITSAMPLES] =
140{
141   /* short cycle, long cycles */
142   VT2_HI,VT2_HI,VT2_HI,VT2_LO,VT2_LO,VT2_LO,
143   VT2_HI,VT2_HI,VT2_HI,VT2_HI,VT2_HI,VT2_HI,
144   VT2_LO,VT2_LO,VT2_LO,VT2_LO,VT2_LO,VT2_LO
145};
146
147static const INT16 vtech2_bit1[VT2_BITSAMPLES] =
148{
149   /* three short cycle */
150   VT2_HI,VT2_HI,VT2_HI,VT2_LO,VT2_LO,VT2_LO,
151   VT2_HI,VT2_HI,VT2_HI,VT2_LO,VT2_LO,VT2_LO,
152   VT2_HI,VT2_HI,VT2_HI,VT2_LO,VT2_LO,VT2_LO
153};
154
155
156static INT16 *vtech2_fill_wave_bit(INT16 *buffer, int bit)
157{
158   int i;
159   if( bit )
160   {
161      for( i = 0; i < VT2_BITSAMPLES; i++ )
162         *buffer++ = vtech2_bit1[i];
163    }
164   else
165   {
166      for( i = 0; i < VT2_BITSAMPLES; i++ )
167         *buffer++ = vtech2_bit0[i];
168    }
169   return buffer;
170}
171
172
173static INT16 *vtech2_fill_wave_byte(INT16 *buffer, int byte)
174{
175   buffer = vtech2_fill_wave_bit(buffer, (byte >> 7) & 1);
176   buffer = vtech2_fill_wave_bit(buffer, (byte >> 6) & 1);
177   buffer = vtech2_fill_wave_bit(buffer, (byte >> 5) & 1);
178   buffer = vtech2_fill_wave_bit(buffer, (byte >> 4) & 1);
179   buffer = vtech2_fill_wave_bit(buffer, (byte >> 3) & 1);
180   buffer = vtech2_fill_wave_bit(buffer, (byte >> 2) & 1);
181   buffer = vtech2_fill_wave_bit(buffer, (byte >> 1) & 1);
182   buffer = vtech2_fill_wave_bit(buffer, (byte >> 0) & 1);
183    return buffer;
184}
185
186static int vtech2_cassette_fill_wave(INT16 *buffer, int length, UINT8 *code)
187{
188   return generic_fill_wave(buffer, length, code, VT2_BITSAMPLES, VT2_BYTESAMPLES, VT2_LO, vtech2_fill_wave_byte);
189}
190
191static const struct CassetteLegacyWaveFiller vtech2_legacy_fill_wave =
192{
193   vtech2_cassette_fill_wave,   /* fill_wave */
194   1,                     /* chunk_size */
195   VT2_BYTESAMPLES,         /* chunk_samples */
196   NULL,                  /* chunk_sample_calc */
197   600*VT2_BITSAMPLES,         /* sample_frequency */
198   600*VT2_BITSAMPLES,         /* header_samples */
199   600*VT2_BITSAMPLES         /* trailer_samples */
200};
201
202static casserr_t vtech2_cas_identify(cassette_image *cassette, struct CassetteOptions *opts)
203{
204   return cassette_legacy_identify(cassette, opts, &vtech2_legacy_fill_wave);
205}
206
207static casserr_t vtech2_cas_load(cassette_image *cassette)
208{
209   return cassette_legacy_construct(cassette, &vtech2_legacy_fill_wave);
210}
211
212static const struct CassetteFormat vtech2_cas_format =
213{
214   "cas",
215   vtech2_cas_identify,
216   vtech2_cas_load,
217   NULL
218};
219
220CASSETTE_FORMATLIST_START(vtech2_cassette_formats)
221   CASSETTE_FORMAT(vtech2_cas_format)
222CASSETTE_FORMATLIST_END
src/mess/formats/tzx_cas.h
r11457r11458
1/*
2
3       tzx_cas.h
4
5       Format code for tzx cassette files
6*/
7
8#ifndef TZX_CAS_H
9#define TZX_CAS_H
10
11#include "imagedev/cassimg.h"
12
13CASSETTE_FORMATLIST_EXTERN(tzx_cassette_formats);
14CASSETTE_FORMATLIST_EXTERN(cdt_cassette_formats);
15
16#endif /* TZX_CAS_H */
src/mess/formats/cqm_dsk.c
r11457r11458
1/*********************************************************************
2
3    formats/cqm_dsk.c
4
5    CopyQM disk images
6
7*********************************************************************/
8
9#include <string.h>
10#include "emu.h"
11#include "imagedev/flopimg.h"
12#include "imagedev/flopdrv.h"
13
14#define CQM_HEADER_SIZE 133
15
16struct cqmdsk_tag
17{
18   int heads;
19   int tracks;
20   int sector_size;
21   int sector_per_track;
22   int sector_base;
23   int interleave;
24   int skew;
25
26   UINT8* buf;
27   UINT64 track_offsets[84*2]; /* offset within data for each track */
28};
29
30
31static struct cqmdsk_tag *get_tag(floppy_image *floppy)
32{
33   struct cqmdsk_tag *tag;
34   tag = (cqmdsk_tag *)floppy_tag(floppy );
35   return tag;
36}
37
38
39
40FLOPPY_IDENTIFY( cqm_dsk_identify )
41{
42   UINT8 header[2];
43
44   floppy_image_read(floppy, header, 0, 2);
45   if (header[0]=='C' && header[1]=='Q') {
46      *vote = 100;
47   } else {
48      *vote = 0;
49   }
50   return FLOPPY_ERROR_SUCCESS;
51}
52
53static int cqm_get_heads_per_disk(floppy_image *floppy)
54{
55   return get_tag(floppy)->heads;
56}
57
58static int cqm_get_tracks_per_disk(floppy_image *floppy)
59{
60   return get_tag(floppy)->tracks;
61}
62
63static UINT64 cqm_get_track_offset(floppy_image *floppy, int head, int track)
64{
65   return get_tag(floppy)->track_offsets[(track<<1) + head];
66}
67
68static floperr_t get_offset(floppy_image *floppy, int head, int track, int sector, int sector_is_index, UINT64 *offset)
69{
70   UINT64 pos = 0;
71   UINT8 data;
72   INT16 len;
73   int s;
74
75   if ((head < 0) || (head >= get_tag(floppy)->heads) || (track < 0) || (track >= get_tag(floppy)->tracks)
76         || (sector < 0) )
77      return FLOPPY_ERROR_SEEKERROR;
78
79   pos = cqm_get_track_offset(floppy,head,track);
80   s = 0;
81   do {
82      floppy_image_read(floppy, &len, pos, 2);
83      pos+=2;
84      if(len<0) {
85         floppy_image_read(floppy, &data, pos, 1);
86         memset(get_tag(floppy)->buf + s,data, -len);
87         pos++;
88         s += -len;
89      } else {
90         floppy_image_read(floppy, get_tag(floppy)->buf + s, pos, len);
91         pos+=len;
92         s += len;
93      }
94   } while(s<get_tag(floppy)->sector_size*get_tag(floppy)->sector_per_track);
95
96   if (offset)
97      *offset = sector * get_tag(floppy)->sector_size;
98   return FLOPPY_ERROR_SUCCESS;
99}
100
101
102
103static floperr_t internal_cqm_read_sector(floppy_image *floppy, int head, int track, int sector, int sector_is_index, void *buffer, size_t buflen)
104{
105   UINT64 offset;
106   floperr_t err;
107
108   // take sector offset
109   err = get_offset(floppy, head, track, sector, sector_is_index, &offset);
110   if (err)
111      return err;
112   memcpy(buffer,get_tag(floppy)->buf+offset,buflen);
113   return FLOPPY_ERROR_SUCCESS;
114}
115
116
117static floperr_t cqm_read_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen)
118{
119   return internal_cqm_read_sector(floppy, head, track, sector, FALSE, buffer, buflen);
120}
121
122static floperr_t cqm_read_indexed_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen)
123{
124   return internal_cqm_read_sector(floppy, head, track, sector, TRUE, buffer, buflen);
125}
126
127static floperr_t cqm_get_sector_length(floppy_image *floppy, int head, int track, int sector, UINT32 *sector_length)
128{
129   floperr_t err;
130   err = get_offset(floppy, head, track, sector, FALSE, NULL);
131   if (err)
132      return err;
133
134   if (sector_length) {
135      *sector_length = get_tag(floppy)->sector_size;
136   }
137   return FLOPPY_ERROR_SUCCESS;
138}
139
140static floperr_t cqm_get_indexed_sector_info(floppy_image *floppy, int head, int track, int sector_index, int *cylinder, int *side, int *sector, UINT32 *sector_length, unsigned long *flags)
141{
142   if (sector_index >= get_tag(floppy)->sector_per_track) return FLOPPY_ERROR_SEEKERROR;
143
144   if (cylinder) {
145      *cylinder = track;
146   }
147   if (side) {
148      *side = head;
149   }
150   if (sector) {
151      *sector = get_tag(floppy)->sector_base + sector_index;
152   }
153   if (sector_length) {
154      *sector_length = get_tag(floppy)->sector_size;
155   }
156   if (flags)
157      *flags = 0;
158   return FLOPPY_ERROR_SUCCESS;
159}
160
161FLOPPY_CONSTRUCT( cqm_dsk_construct )
162{
163   struct FloppyCallbacks *callbacks;
164   struct cqmdsk_tag *tag;
165   UINT8 header[CQM_HEADER_SIZE];
166   UINT64 pos = 0;
167   INT16 len;
168   int head;
169   int track;
170   int s;
171   if(params)
172   {
173      // create
174      return FLOPPY_ERROR_UNSUPPORTED;
175   }
176
177   tag = (struct cqmdsk_tag *) floppy_create_tag(floppy, sizeof(struct cqmdsk_tag));
178   if (!tag)
179      return FLOPPY_ERROR_OUTOFMEMORY;
180
181   floppy_image_read(floppy, header, 0, CQM_HEADER_SIZE);
182
183   tag->sector_size      = (header[0x04] << 8) + header[0x03];
184   tag->sector_per_track = header[0x10];
185   tag->heads           = header[0x12];
186   tag->tracks           = header[0x5b];
187   tag->sector_base     = header[0x71] + 1;
188   tag->interleave        = header[0x74];
189   tag->skew           = header[0x75];
190
191   // header + comment size to position on first data block
192
193   pos = CQM_HEADER_SIZE + (header[0x70] << 8) + header[0x6f];
194   track = 0;
195   head = 0;
196   tag->buf = (UINT8*)malloc(tag->sector_size*tag->sector_per_track);
197   do {
198      tag->track_offsets[(track<<1) + head] = pos;
199      s = 0;
200      do {
201         floppy_image_read(floppy, &len, pos, 2);
202         pos+=2;
203         if(len<0) {
204            pos++;
205            s += -len;
206         } else {
207            pos+=len;
208            s += len;
209         }
210      } while(s<tag->sector_size*tag->sector_per_track);
211      if(head ==0 && tag->heads > 1) {
212         head = 1;
213      } else {
214         head = 0;
215         track++;
216      }
217   } while(pos < floppy_image_size(floppy));
218
219
220   callbacks = floppy_callbacks(floppy);
221   callbacks->read_sector = cqm_read_sector;
222   callbacks->read_indexed_sector = cqm_read_indexed_sector;
223   callbacks->get_sector_length = cqm_get_sector_length;
224   callbacks->get_heads_per_disk = cqm_get_heads_per_disk;
225   callbacks->get_tracks_per_disk = cqm_get_tracks_per_disk;
226   callbacks->get_indexed_sector_info = cqm_get_indexed_sector_info;
227
228   return FLOPPY_ERROR_SUCCESS;
229}
230
src/mess/formats/csw_cas.c
r11457r11458
1/*
2CSW format
3----------
4Header Description
5
6Offset  Value   Type    Description
70x00    (note)  ASCII   22 bytes "Compressed Square Wave"  signature
80x16    0x1A    BYTE    Terminator code
90x17    0x02    BYTE    CSW major revision number
100x18    0x00    BYTE    CSW minor revision number
110x19            DWORD   Sample rate
120x1D            DWORD   Total number of pulses (after decompression)
130x21            BYTE    Compression type  0x01: RLE    0x02: Z-RLE
140x22            BYTE    Flags   b0: initial polarity: if set, the signal starts at logical high
150x23    HDR     BYTE    Header extension length in bytes (0x00)
160x24            ASCII   16 bytes free use
170x34            BYTE    Start of Header is HDR>0
18
19
20
21*/
22
23#include <string.h>
24
25#include "emu.h"
26#include "zlib.h"
27#include "formats/uef_cas.h"
28#include "formats/csw_cas.h"
29
30
31#define CSW_WAV_FREQUENCY   44100
32
33static const UINT8 CSW_HEADER[] = { "Compressed Square Wave" };
34
35static UINT32 get_leuint32(const void *ptr)
36{
37   UINT32 value;
38   memcpy(&value, ptr, sizeof(value));
39   return LITTLE_ENDIANIZE_INT32(value);
40}
41
42static int mycaslen;
43
44static int csw_cas_to_wav_size( const UINT8 *casdata, int caslen ) {
45UINT32 SampleRate;
46UINT32 NumberOfPulses;
47UINT8  MajorRevision;
48UINT8  MinorRevision;
49UINT8  CompressionType;
50UINT8  Flags;
51UINT8  HeaderExtensionLength;
52UINT8 *gz_ptr = NULL;
53
54int         total_size;
55z_stream   d_stream;
56int       err;
57UINT8      *in_ptr;
58int       bsize=0;
59
60   if ( memcmp( casdata, CSW_HEADER, sizeof(CSW_HEADER) ) ) {
61      logerror( "csw_cas_to_wav_size: cassette image has incompatible header\n" );
62      goto cleanup;
63   }
64
65   if (casdata[0x16]!=0x1a) {
66      logerror( "csw_cas_to_wav_size: Terminator Code Not Found\n" );
67      goto cleanup;
68   }
69
70   MajorRevision=casdata[0x17];
71   MinorRevision=casdata[0x18];
72
73   logerror("Version %d : %d\n",MajorRevision,MinorRevision);
74
75   if (casdata[0x17]!=2){
76      logerror( "csw_cas_to_wav_size: Unsuported Major Version\n" );
77      goto cleanup;
78   }
79
80   SampleRate=get_leuint32(casdata+0x19);
81   logerror("Sample rate %d\n",SampleRate);
82
83   NumberOfPulses=get_leuint32(casdata+0x1d);
84   logerror("Number Of Pulses %d\n",NumberOfPulses);
85
86
87   CompressionType=casdata[0x21];
88   Flags=casdata[0x22];
89   HeaderExtensionLength=casdata[0x23];
90
91   logerror("CompressionType %d   Flast %d   HeaderExtensionLength %d\n",CompressionType,Flags,HeaderExtensionLength);
92
93   mycaslen=caslen;
94   //from here on down for now I am assuming it is compressed csw file.
95   in_ptr = (UINT8*) casdata+0x34+HeaderExtensionLength;
96
97   gz_ptr = (UINT8*)malloc( 8 );
98
99
100   d_stream.next_in = (unsigned char *)in_ptr;
101   d_stream.avail_in = caslen - ( in_ptr - casdata );
102   d_stream.total_in=0;
103
104   d_stream.next_out = gz_ptr;
105   d_stream.avail_out = 1;
106   d_stream.total_out=0;
107
108   d_stream.zalloc = 0;
109   d_stream.zfree = 0;
110   d_stream.opaque = 0;
111   d_stream.data_type=0;
112
113   err = inflateInit( &d_stream );
114   if ( err != Z_OK ) {
115      logerror( "inflateInit2 error: %d\n", err );
116      goto cleanup;
117   }
118
119
120   total_size=1;
121   do
122   {
123      d_stream.next_out = gz_ptr;
124      d_stream.avail_out=1;
125      err=inflate( &d_stream, Z_SYNC_FLUSH );
126      if (err==Z_OK) {
127         bsize=gz_ptr[0];
128         if (bsize==0) {
129            d_stream.avail_out=4;
130            d_stream.next_out = gz_ptr;
131            err=inflate( &d_stream, Z_SYNC_FLUSH );
132            bsize=get_leuint32(gz_ptr);
133         }
134         total_size=total_size+bsize;
135      }
136   }
137   while (err==Z_OK);
138
139   if ( err != Z_STREAM_END ) {
140      logerror( "inflate error: %d\n", err );
141      goto cleanup;
142   }
143
144   err = inflateEnd( &d_stream );
145   if ( err != Z_OK ) {
146      logerror( "inflateEnd error: %d\n", err );
147      goto cleanup;
148   }
149
150
151
152
153
154
155   if ( gz_ptr ) {
156      free( gz_ptr );
157      gz_ptr = NULL;
158   }
159
160
161
162   return total_size;
163
164cleanup:
165   if ( gz_ptr ) {
166      free( gz_ptr );
167      gz_ptr = NULL;
168   }
169   return -1;
170
171}
172
173static int csw_cas_fill_wave( INT16 *buffer, int length, UINT8 *bytes ) {
174UINT32 SampleRate;
175UINT32 NumberOfPulses;
176UINT8  CompressionType;
177UINT8  Flags;
178UINT8  HeaderExtensionLength;
179INT8   Bit;
180
181UINT8 *gz_ptr = NULL;
182int         total_size;
183z_stream   d_stream;
184int       err;
185UINT8      *in_ptr;
186int       bsize=0;
187int      i;
188
189
190   logerror("Length %d\n",length);
191
192   SampleRate=get_leuint32(bytes+0x19);
193   logerror("Sample rate %d\n",SampleRate);
194
195   NumberOfPulses=get_leuint32(bytes+0x1d);
196   logerror("Number Of Pulses %d\n",NumberOfPulses);
197
198   CompressionType=bytes[0x21];
199   Flags=bytes[0x22];
200   HeaderExtensionLength=bytes[0x23];
201
202   if ((Flags&0)==0)
203   {
204      Bit=-100;
205   } else {
206      Bit=100;
207   }
208
209   logerror("CompressionType %d   Flast %d   HeaderExtensionLength %d\n",CompressionType,Flags,HeaderExtensionLength);
210
211
212   //from here on down for now I am assuming it is compressed csw file.
213   in_ptr = (UINT8*) bytes+0x34+HeaderExtensionLength;
214
215   gz_ptr = (UINT8*)malloc( 8 );
216
217
218   d_stream.next_in = (unsigned char *)in_ptr;
219   d_stream.avail_in = mycaslen - ( in_ptr - bytes );
220   d_stream.total_in=0;
221
222   d_stream.next_out = gz_ptr;
223   d_stream.avail_out = 1;
224   d_stream.total_out=0;
225
226   d_stream.zalloc = 0;
227   d_stream.zfree = 0;
228   d_stream.opaque = 0;
229   d_stream.data_type=0;
230
231   err = inflateInit( &d_stream );
232   if ( err != Z_OK ) {
233      logerror( "inflateInit2 error: %d\n", err );
234      goto cleanup;
235   }
236
237   total_size=0;
238
239   do
240   {
241      d_stream.next_out = gz_ptr;
242      d_stream.avail_out=1;
243      err=inflate( &d_stream, Z_SYNC_FLUSH );
244      if (err==Z_OK) {
245         bsize=gz_ptr[0];
246         if (bsize==0) {
247            d_stream.avail_out=4;
248            d_stream.next_out = gz_ptr;
249            err=inflate( &d_stream, Z_SYNC_FLUSH );
250            bsize=get_leuint32(gz_ptr);
251         }
252         for (i=0;i<bsize;i++)
253         {
254            buffer[total_size++]=Bit;
255         }
256         Bit=-Bit;
257      }
258   }
259   while (err==Z_OK);
260
261   if ( err != Z_STREAM_END ) {
262      logerror( "inflate error: %d\n", err );
263      goto cleanup;
264   }
265
266   err = inflateEnd( &d_stream );
267   if ( err != Z_OK ) {
268      logerror( "inflateEnd error: %d\n", err );
269      goto cleanup;
270   }
271
272
273
274
275
276
277   if ( gz_ptr ) {
278      free( gz_ptr );
279      gz_ptr = NULL;
280   }
281
282
283
284
285   return length;
286
287
288
289   cleanup:
290      if ( gz_ptr ) {
291         free( gz_ptr );
292         gz_ptr = NULL;
293      }
294   return -1;
295}
296
297
298static const struct CassetteLegacyWaveFiller csw_legacy_fill_wave = {
299   csw_cas_fill_wave,      /* fill_wave */
300   -1,                  /* chunk_size */
301   0,                  /* chunk_samples */
302   csw_cas_to_wav_size,   /* chunk_sample_calc */
303   CSW_WAV_FREQUENCY,      /* sample_frequency */
304   0,                  /* header_samples */
305   0                  /* trailer_samples */
306};
307
308static casserr_t csw_cassette_identify( cassette_image *cassette, struct CassetteOptions *opts ) {
309   return cassette_legacy_identify( cassette, opts, &csw_legacy_fill_wave );
310}
311
312static casserr_t csw_cassette_load( cassette_image *cassette ) {
313   return cassette_legacy_construct( cassette, &csw_legacy_fill_wave );
314}
315
316static const struct CassetteFormat csw_cassette_format = {
317   "csw",
318   csw_cassette_identify,
319   csw_cassette_load,
320   NULL
321};
322
323CASSETTE_FORMATLIST_START(csw_cassette_formats)
324   CASSETTE_FORMAT(csw_cassette_format)
325CASSETTE_FORMATLIST_END
326
327CASSETTE_FORMATLIST_START(bbc_cassette_formats)
328   CASSETTE_FORMAT(csw_cassette_format)
329   CASSETTE_FORMAT(uef_cassette_format)
330CASSETTE_FORMATLIST_END
src/mess/formats/vt_cas.h
r11457r11458
1/*********************************************************************
2
3    vt_cas.h
4
5    Format code for VTech casette files
6
7*********************************************************************/
8
9#ifndef VT_CAS_H
10#define VT_CAS_H
11
12#include "imagedev/cassimg.h"
13
14CASSETTE_FORMATLIST_EXTERN(vtech1_cassette_formats);
15CASSETTE_FORMATLIST_EXTERN(vtech2_cassette_formats);
16
17#endif /* VT_CAS_H */
src/mess/formats/coco_dsk.c
r11457r11458
1/*********************************************************************
2
3    formats/coco_dsk.c
4
5    Tandy Color Computer / Dragon disk images
6
7*********************************************************************/
8
9#include <string.h>
10#include <assert.h>
11#include <time.h>
12
13#include "osdepend.h"
14#include "formats/coco_dsk.h"
15#include "formats/basicdsk.h"
16#include "imageutl.h"
17
18/* -----------------------------------------------------------------------
19 * JVC (Jeff Vavasour CoCo) format
20 *
21 * Used by Jeff Vavasour's CoCo Emulators
22 *
23 *  Documentation taken from Tim Linder's web site:
24 *      http://home.netcom.com/~tlindner/JVC.html
25 *
26 *  A. Header length
27 *      The header length is determined by the file length modulo 256:
28 *          headerSize = fileLength % 256;
29 *      This means that the header is variable length and the minimum size
30 *      is zero bytes, and the maximum size of 255 bytes.
31 *
32 * B. Header
33 *      Here is a description of the header bytes:
34 *          Byte Offset     Description            Default
35 *          -----------     -----------------      -------
36 *                    0     Sectors per track      18
37 *                    1     Side count              1
38 *                    2     Sector size code        1
39 *                    3     First sector ID         1
40 *                    4     Sector attribute flag   0
41 *
42 *  If the sector attribute flag is zero then the track count is determined
43 *  by the formula:
44 *
45 *      (fileLength - headerSize) / (sectorsPerTrack * (128 <<
46 *          sectorSizeCode)) / sideCount
47 *
48 *  If the sector attribute flag is non zero then the track count is
49 *  determined by the more complex formula:
50 *
51 *      (fileLength - headerSize) / (sectorsPerTrack * ((128 <<
52 *          sectorSizeCode) + 1) ) / sideCount
53 *
54 *  If the length of the header is to short to contain the geometry desired,
55 *  then the default values are assumed. If the header length is zero the all
56 *  of the geometry is assumed. When creating disk images it is desirable to
57 *  make the header length as short as possible. The header should only be
58 *  used to deviate from the default values.
59 *
60 *  The sector data begins immediately after the header. If the header length
61 *  is zero then the sector data is at the beginning file.
62 *
63 *  C. Sectors per track
64 *      This is the number of sectors per track (ones based). A value of 18
65 *      means there are 18 sectors per track
66 *
67 *  D. Side Count
68 *      This is the number of sides in the disk image. Values of 1 or 2 are
69 *      acceptable. If there are two sides then the tracks are interleaved.
70 *      The first track in the image file is track zero side 1, the second
71 *      track in the image file is track zero side 2.
72 *
73 *  E. Sector size
74 *      The is the same value that is stored in the wd179x ID field to
75 *      determine sector size:
76 *
77 *          0x00         128 bytes
78 *          0x01         256 bytes
79 *          0x02         512 bytes
80 *          0x03        1024 bytes
81 *
82 *  Other values are undefined. Every sector in the disk image must be the
83 *  same size.
84 *
85 *  F. First sector ID
86 *      This determines the first sector ID for each track. Each successive
87 *      sector adds one to the previous ID. If the first sector ID is 1, then
88 *      the second sector has an ID of 2, and the third has an ID of 3.
89 *
90 *  G. Sector Attribute Flag
91 *      If this byte is non zero, then each sector contains an additional
92 *      byte prepended to the sector data. If the attribute flag is zero then
93 *      there are no extra bytes in front of the sector data.
94 *
95 *  H. Sector attribute byte
96 *      This byte is put at the begining of every sector if the header flag
97 *      is turned on. The information this byte contains is the same as the
98 *      status register (of the wd179x) would contain when a 'Read Sector'
99 *      command was issued. The bit fields are defined as:
100 *
101 *      Bit position:
102 *      ---------------
103 *      7 6 5 4 3 2 1 0
104 *      | | | | | | | |
105 *      | | | | | | | +--- Not used. Set to zero.
106 *      | | | | | | +----- Not used. Set to zero.
107 *      | | | | | +------- Not used. Set to zero.
108 *      | | | | +--------- Set on CRC error.
109 *      | | | +----------- Set if sector not found.
110 *      | | +------------- Record type: 1 - Deleted Data Mark, 0 - Data Mark.
111 *      | +--------------- Not Used. Set to zero.
112 *      +----------------- Not Used. Set to zero.
113 *
114 * ----------------------------------------------------------------------- */
115
116static int coco_jvc_decode_header(floppy_image *floppy, UINT64 size,
117   struct basicdsk_geometry *geometry)
118{
119   UINT8 header[256];
120   UINT8 sector_attribute_flag;
121   UINT16 physical_bytes_per_sector;
122   UINT32 header_size, file_size;
123   struct basicdsk_geometry dummy_geometry;
124
125   if (geometry)
126      memset(geometry, 0, sizeof(*geometry));
127   else
128      geometry = &dummy_geometry;
129
130   if (size > 0xFFFFFFFF)
131      return -1;
132   file_size = (UINT32) size;
133
134   /* read the header */
135   header_size = (UINT32) file_size % 0x100;
136   floppy_image_read(floppy, header, 0, header_size);
137   geometry->offset = header_size;
138
139   /* byte offset 0 - sectors per track */
140   geometry->sectors = (header_size > 0) ? header[0] : 18;
141   if (geometry->sectors <= 0)
142      return -1;
143
144   /* byte offset 1 - side count */
145   geometry->heads = (header_size > 1) ? header[1] : 1;
146   if (geometry->heads <= 0)
147      return -1;
148
149   /* byte offset 2 - sector size code */
150   geometry->sector_length = 128 << ((header_size > 2) ? header[2] : 1);
151   if (geometry->sector_length <= 0)
152      return -1;
153
154   /* byte offset 3 - first sector ID */
155   geometry->first_sector_id = (header_size > 3) ? header[3] : 1;
156
157   /* byte offset 4 - sector attribute flag */
158   sector_attribute_flag = (header_size > 4) ? header[4] : 0;
159   if (sector_attribute_flag != 0)
160      return -1;   /* we do not support sector attribute flags */
161
162   physical_bytes_per_sector = geometry->sector_length;
163   if (sector_attribute_flag)
164      physical_bytes_per_sector++;
165
166   geometry->tracks = (file_size - header_size) / geometry->sectors / geometry->heads / physical_bytes_per_sector;
167
168   /* do we have an oddball size?  reject this file if not */
169   if ((file_size - header_size) % physical_bytes_per_sector)
170      return -1;
171
172   /* minimum of 35 tracks; support degenerate JVC files */
173   if (geometry->tracks < 35)
174      geometry->tracks = 35;
175
176   return 0;
177}
178
179
180
181static FLOPPY_IDENTIFY(coco_jvc_identify)
182{
183   UINT64 size;
184   size = floppy_image_size(floppy);
185   *vote = coco_jvc_decode_header(floppy, size, NULL) ? 0 : 100;
186   return FLOPPY_ERROR_SUCCESS;
187}
188
189
190
191static FLOPPY_CONSTRUCT(coco_jvc_construct)
192{
193   struct basicdsk_geometry geometry;
194   UINT8 header[5];
195   size_t header_size;
196
197   if (params)
198   {
199      /* create */
200      memset(&geometry, 0, sizeof(geometry));
201      geometry.heads            = option_resolution_lookup_int(params, PARAM_HEADS);
202      geometry.tracks            = option_resolution_lookup_int(params, PARAM_TRACKS);
203      geometry.sectors         = option_resolution_lookup_int(params, PARAM_SECTORS);
204      geometry.first_sector_id   = option_resolution_lookup_int(params, PARAM_FIRST_SECTOR_ID);
205      geometry.sector_length      = option_resolution_lookup_int(params, PARAM_SECTOR_LENGTH);
206
207      header[0] = (UINT8) geometry.sectors;
208      header[1] = (UINT8) geometry.heads;
209      header[2] = (UINT8) compute_log2(geometry.sector_length) - 7;
210      header[3] = (UINT8) geometry.first_sector_id;
211      header[4] = 0;
212
213      /* now that we have the header computed, figure out the header size */
214      header_size = 0;
215      if (header[0] != 18)
216         header_size = 1;
217      if (header[1] != 1)
218         header_size = 2;
219      if (header[2] != 1)
220         header_size = 3;
221      if (header[3] != 1)
222         header_size = 4;
223      if (header[4] != 0)
224         header_size = 5;
225
226      geometry.offset = header_size;
227
228      floppy_image_write(floppy, header, 0, header_size);
229   }
230   else
231   {
232      /* load */
233      if (coco_jvc_decode_header(floppy, floppy_image_size(floppy), &geometry))
234         return FLOPPY_ERROR_INVALIDIMAGE;
235   }
236   return basicdsk_construct(floppy, &geometry);
237}
238
239
240/* -----------------------------------------------------------------------
241 * OS-9 file format
242 *
243 * This file format is largely a hack because there are a large amount of
244 * disk images that do not have geometry image separate from the disk image
245 * itself.  So we support OS-9 images with are simply basic disks whose
246 * geometry is determined by the disk image.
247 *
248 * OS-9 images identified by an LSN; which are simply blocks of 256 bytes
249 *
250 * LSN0
251 * Byte    size    use
252 * $00     3       sectors on disk
253 * $03     1       track size in sectors
254 * $04     2       bytes in allocation bit map; typically 1bit/sector so for
255 *                 35 tracks of 18 sectors each that's $4E (single sided disk)
256 *                 40 tracks per side, 18 sectors each = $B4
257 * $06     2       sectors per bit in allocation map; normally 1
258 * $08     3       LSN of root directory; normally 2 but depends on size of
259 *                 $04 value
260 * $0B     2       owner's user number; normally 0
261 * $0D     1       disk attributes; normally $FF
262 * $0E     2       psuedo random number for identification
263 * $10     1       disk format; typical is 3
264 *                 %00000001 0=single side 1=double side
265 *                 %00000010 0=single density (non Coco) 1=double density
266 *                 %00000100 0=48tracks/inch 1=96tracks/inch
267 * $11     2       sectors per track; normal is $12 skip several not needed
268 *                 for Format
269 * $1A     5       date of creation Y:M:D:H:M
270 * $1F     32      ASCII name of disk, last letter has $80 added to it,
271 *                 the full 32 bytes do not need to be used.
272 *
273 * Allocation bit map, fill with zeros and set bits from low to high as
274 * sectors are used. So, for a fresh disk sectors LSN0,LSN1,LSN2, and LSN3
275 * will be in use so the first byte will be $FF $C0 and all others
276 * in the map are $00
277 *
278 * Root directory LSN2
279 * Byte    size     use
280 * $00      1       attributes will be $BF
281 * $01      2       owners ID will be $0000
282 * $03      5       date last modified will be creation date  Y:M:D:H:M
283 * $08      1       link count; set to $02
284 * $09      4       file size in bytes, set to $40
285 * $0D      3       date created Y:M:D
286 * $10      3       block LSN set to current sector number+1 ie $03 in this case
287 * $13      2       size in sectors of directory block, set to $07
288 * All other bytes in sector set to $00
289 * LSN3 first sector of directory with names
290 * Fill sector with all $00 and then set listed bytes
291 * $00      2       value $2EAE   which is .. with last byte+$80
292 * $1F      1       value $02     LSN for start of this directory as there is
293 *                  none higher in tree
294 * $20      1       value $AE     which is . with $80 added
295 * $3F      1       value $02     LSN for start of this directory
296 * ----------------------------------------------------------------------- */
297
298static floperr_t coco_os9_readheader(floppy_image *floppy, struct basicdsk_geometry *geometry)
299{
300   UINT8 header[0x20];
301   int total_sectors;
302
303   floppy_image_read(floppy, header, 0, sizeof(header));
304
305   total_sectors = (header[0x00] << 16) | (header[0x01] << 8) | header[0x02];
306
307   memset(geometry, 0, sizeof(*geometry));
308   geometry->first_sector_id = 1;
309   geometry->sector_length = 256;
310   geometry->sectors = (header[0x11] << 8) + header[0x12];
311   geometry->heads = (header[0x10] & 0x01) ? 2 : 1;
312
313   if (!geometry->sectors)
314      return FLOPPY_ERROR_INVALIDIMAGE;
315
316   geometry->tracks = total_sectors / geometry->sectors / geometry->heads;
317
318   if (total_sectors != geometry->tracks * geometry->sectors * geometry->heads)
319      return FLOPPY_ERROR_INVALIDIMAGE;
320
321   return FLOPPY_ERROR_SUCCESS;
322}
323
324
325
326static floperr_t coco_os9_post_format(floppy_image *floppy, option_resolution *params)
327{
328   UINT8 header[0x0400];
329   floperr_t err;
330   time_t t;
331   struct tm *ltime;
332   int heads, tracks, sectors, total_sectors;
333
334   heads   = option_resolution_lookup_int(params, PARAM_HEADS);
335   tracks   = option_resolution_lookup_int(params, PARAM_TRACKS);
336   sectors   = option_resolution_lookup_int(params, PARAM_SECTORS);
337   total_sectors = heads * tracks * sectors;
338
339   /* write the initial header */
340   time(&t);
341   ltime = localtime(&t);
342
343   memset(&header, 0, sizeof(header));
344   header[0x0000] = (UINT8) (total_sectors >> 16);
345   header[0x0001] = (UINT8) (total_sectors >>  8);
346   header[0x0002] = (UINT8) (total_sectors >>  0);
347   header[0x0003] = (UINT8) sectors;
348   header[0x0004] = (UINT8) (((total_sectors + 7) / 8) >> 8);
349   header[0x0005] = (UINT8) (((total_sectors + 7) / 8) >> 0);
350   header[0x0006] = 0x00;
351   header[0x0007] = 0x01;
352   header[0x0008] = 0x00;
353   header[0x0009] = 0x00;
354   header[0x000a] = 0x02;
355   header[0x000b] = 0x00;
356   header[0x000c] = 0x00;
357   header[0x000d] = 0xff;
358   header[0x000e] = floppy_random_byte(floppy);
359   header[0x000f] = floppy_random_byte(floppy);
360   header[0x0010] = (heads == 2) ? 3 : 2;
361   header[0x0011] = (UINT8) (sectors >> 8);
362   header[0x0012] = (UINT8) (sectors >> 0);
363   header[0x001A] = (UINT8) ltime->tm_year;
364   header[0x001B] = (UINT8) ltime->tm_mon + 1;
365   header[0x001C] = (UINT8) ltime->tm_mday;
366   header[0x001D] = (UINT8) ltime->tm_hour;
367   header[0x001E] = (UINT8) ltime->tm_min;
368   header[0x001F] = 0xA0;
369   header[0x0100] = 0xFF;
370   header[0x0101] = 0xC0;
371   header[0x0200] = 0xBF;
372   header[0x0201] = 0x00;
373   header[0x0202] = 0x00;
374   header[0x0203] = (UINT8) ltime->tm_year;
375   header[0x0204] = (UINT8) ltime->tm_mon + 1;
376   header[0x0205] = (UINT8) ltime->tm_mday;
377   header[0x0206] = (UINT8) ltime->tm_hour;
378   header[0x0207] = (UINT8) ltime->tm_min;
379   header[0x0208] = 0x02;
380   header[0x0209] = 0x00;
381   header[0x020A] = 0x00;
382   header[0x020B] = 0x00;
383   header[0x020C] = 0x40;
384   header[0x020D] = (UINT8) (ltime->tm_year % 100);
385   header[0x020E] = (UINT8) ltime->tm_mon;
386   header[0x020F] = (UINT8) ltime->tm_mday;
387   header[0x0210] = 0x00;
388   header[0x0211] = 0x00;
389   header[0x0212] = 0x03;
390   header[0x0213] = 0x00;
391   header[0x0214] = 0x07;
392   header[0x0300] = 0x2E;
393   header[0x0301] = 0xAE;
394   header[0x031F] = 0x02;
395   header[0x0320] = 0xAE;
396   header[0x033F] = 0x02;
397
398   if (total_sectors % 8)
399      header[0x0100 + (total_sectors / 8)] = 0xFF >> (total_sectors % 8);
400
401   err = floppy_write_sector(floppy, 0, 0, 1, 0, &header[0x0000], 256, 0);
402   if (err)
403      return err;
404
405   err = floppy_write_sector(floppy, 0, 0, 2, 0, &header[0x0100], 256, 0);
406   if (err)
407      return err;
408
409   err = floppy_write_sector(floppy, 0, 0, 3, 0, &header[0x0200], 256, 0);
410   if (err)
411      return err;
412
413   err = floppy_write_sector(floppy, 0, 0, 4, 0, &header[0x0300], 256, 0);
414   if (err)
415      return err;
416
417   return FLOPPY_ERROR_SUCCESS;
418}
419
420
421
422static FLOPPY_IDENTIFY(coco_os9_identify)
423{
424   struct basicdsk_geometry geometry;
425   *vote = coco_os9_readheader(floppy, &geometry) ? 0 : 100;
426   return FLOPPY_ERROR_SUCCESS;
427}
428
429
430
431static FLOPPY_CONSTRUCT(coco_os9_construct)
432{
433   floperr_t err;
434   struct basicdsk_geometry geometry;
435
436   if (params)
437   {
438      /* create */
439      memset(&geometry, 0, sizeof(geometry));
440      geometry.heads            = option_resolution_lookup_int(params, PARAM_HEADS);
441      geometry.tracks            = option_resolution_lookup_int(params, PARAM_TRACKS);
442      geometry.sectors         = option_resolution_lookup_int(params, PARAM_SECTORS);
443      geometry.first_sector_id   = option_resolution_lookup_int(params, PARAM_FIRST_SECTOR_ID);
444      geometry.sector_length      = option_resolution_lookup_int(params, PARAM_SECTOR_LENGTH);
445   }
446   else
447   {
448      /* open */
449      err = coco_os9_readheader(floppy, &geometry);
450      if (err)
451         return err;
452   }
453
454   /* actually construct the image */
455   err = basicdsk_construct(floppy, &geometry);
456   floppy_callbacks(floppy)->post_format = coco_os9_post_format;
457   return err;
458}
459
460
461
462/* -----------------------------------------------------------------------
463 * VDK file format
464 *
465 * Used by Paul Burgin's PC-Dragon emulator
466 *
467 *  Offset  Bytes   Field           Description
468 *  ------  -----   ------------    -----------
469 *       0      1   magic1          Signature byte 1 ('d')
470 *       1      1   magic2          Signature byte 2 ('k')
471 *       2      2   header_len      Total header length (little endian)
472 *       4      1   ver_actual      Version of the VDK format (0x10)
473 *       5      1   ver_compat      Backwards compatibility version (0x10)
474 *       6      1   source_id       Identify of the file source
475 *       7      1   source_ver      Version of the file source
476 *       8      1   tracks          Number of tracks
477 *       9      1   sides           Number of sides (1-2)
478 *      10      1   flags           Various flags
479 *                                      bit 0: Write protect
480 *                                      bit 1: A Lock
481 *                                      bit 2: F Lock
482 *                                      bit 3: Disk set
483 *      11      1   compression     Compression flags (bits 0-2) and name length
484 * ----------------------------------------------------------------------- */
485
486
487
488static int coco_vdk_decode_header(floppy_image *floppy, struct basicdsk_geometry *geometry)
489{
490   UINT8 header[12];
491   int heads, tracks, sectors, sector_length, offset;
492   UINT64 size;
493
494   size = floppy_image_size(floppy);
495
496   floppy_image_read(floppy, header, 0, sizeof(header));
497
498   if (header[0] != 'd')
499      return -1;
500   if (header[1] != 'k')
501      return -1;
502   if (header[5] != 0x10)
503      return -1;
504   if (header[11] & 0x07)
505      return -1;
506
507   heads = header[9];
508   tracks = header[8];
509   sectors = 18;
510   sector_length = 0x100;
511
512   offset = ((int) header[3]) * 0x100 + header[2];
513
514   if (size != (heads * tracks * sectors * sector_length + offset))
515      return -1;
516
517   if (geometry)
518   {
519      memset(geometry, 0, sizeof(*geometry));
520      geometry->heads = heads;
521      geometry->tracks = tracks;
522      geometry->sectors = sectors;
523      geometry->first_sector_id = 1;
524      geometry->sector_length = sector_length;
525      geometry->offset = offset;
526   }
527   return 0;
528}
529
530
531
532static FLOPPY_IDENTIFY(coco_vdk_identify)
533{
534   *vote = coco_vdk_decode_header(floppy, NULL) ? 0 : 100;
535   return FLOPPY_ERROR_SUCCESS;
536}
537
538
539
540static FLOPPY_CONSTRUCT(coco_vdk_construct)
541{
542   struct basicdsk_geometry geometry;
543   UINT8 header[12];
544
545   if (params)
546   {
547      /* create */
548      memset(&geometry, 0, sizeof(geometry));
549      geometry.heads = option_resolution_lookup_int(params, PARAM_HEADS);
550      geometry.tracks = option_resolution_lookup_int(params, PARAM_TRACKS);
551      geometry.sectors = 18;
552      geometry.first_sector_id = 1;
553      geometry.sector_length = 256;
554      geometry.offset = sizeof(header);
555
556      memset(&header, 0, sizeof(header));
557      header[0] = 'd';
558      header[1] = 'k';
559      header[2] = (UINT8) (sizeof(header) >> 0);
560      header[3] = (UINT8) (sizeof(header) >> 8);
561      header[4] = 0x10;
562      header[5] = 0x10;
563      header[8] = (UINT8) geometry.tracks;
564      header[9] = (UINT8) geometry.heads;
565
566      floppy_image_write(floppy, header, 0, sizeof(header));
567   }
568   else
569   {
570      /* load */
571      if (coco_vdk_decode_header(floppy, &geometry))
572         return FLOPPY_ERROR_INVALIDIMAGE;
573   }
574   return basicdsk_construct(floppy, &geometry);
575}
576
577
578
579/* -----------------------------------------------------------------------
580 * DMK file format
581 *
582 * David M. Keil's disk image format is aptly called an 'on disk' image
583 * format. This means that whatever written to the disk is enocded into
584 * the image file. IDAMS, sector headers, traling CRCs, and intra sector
585 * spacing.                                                         *
586 *
587 *  HEADER DESCRIPTION:
588 *
589 *  Offset  Bytes   Field           Description
590 *  ------  -----   ------------    -----------
591 *       0      1   write_prot      0xff = Writed Protected, 0x00 = R/W
592 *       1      1   tracks          Number of tracks
593 *       2      2   track_length    Bytes per track (little endian)
594 *       4      1   disk_options    Miscellaneous flags
595 *                                      bit 0-3:  Unused
596 *                                      bit   4:  1=single sided 0=dbl
597 *                                      bit   5:  Unused
598 *                                      bit   6:  Single density?
599 *                                      bit   6:  Ignore density flags?
600 *       5      7   reserved        Reserved for future use
601 *      12      4   real_disk_code  If this is 0x12345678 (little endian)
602 *                                  then access a real disk drive
603 *                                  (unsupported)
604 *
605 * Each track begins with a track TOC, consisting of 64 little endian 16-bit
606 * integers.  Each integer has the following format:
607 *      bit 0-13:   Offset from begining of track to 'FE' byte of IDAM
608 *                  Note these are always sorted from first to last. All empty
609 *                  entries are 0x00
610 *      bit 14:     Undefined (reserved)
611 *      bit 15:     Sector double density (0=SD 1=DD)
612 * ----------------------------------------------------------------------- */
613
614struct dmk_tag
615{
616   int heads;
617   int tracks;
618   UINT32 track_size;
619};
620
621#define DMK_HEADER_LEN         16
622#define DMK_TOC_LEN            64
623#define DMK_IDAM_LENGTH         7
624#define DMK_DATA_GAP         80
625#define DMK_LEAD_IN            32
626#define DMK_EXTRA_TRACK_LENGTH   156
627
628#define dmk_idam_type(x)         (x)[0]
629#define dmk_idam_track(x)         (x)[1]
630#define dmk_idam_side(x)         (x)[2]
631#define dmk_idam_sector(x)         (x)[3]
632#define dmk_idam_sectorlength(x)   (x)[4]
633#define dmk_idam_crc(x)            (((x)[5] << 8) + (x)[6])
634#define dmk_idam_set_crc(x, crc)   (x)[5] = ((crc) >> 8); (x)[6] = ((crc) >> 0);
635
636
637static struct dmk_tag *get_dmk_tag(floppy_image *floppy)
638{
639   return (dmk_tag *)floppy_tag(floppy);
640}
641
642
643static floperr_t coco_dmk_get_offset(floppy_image *floppy, int head, int track, UINT64 *offset)
644{
645   struct dmk_tag *tag = get_dmk_tag(floppy);
646
647   if ((head < 0) || (head >= tag->heads) || (track < 0) || (track >= tag->tracks))
648      return FLOPPY_ERROR_SEEKERROR;
649
650   *offset = track;
651   *offset *= tag->heads;
652   *offset += head;
653   *offset *= tag->track_size;
654   *offset += DMK_HEADER_LEN;
655   return FLOPPY_ERROR_SUCCESS;
656}
657
658
659
660static UINT32 coco_dmk_min_track_size(int sectors, int sector_length)
661{
662   int sector_physical_length;
663   sector_physical_length = 8 + 3 + DMK_IDAM_LENGTH + 22 + 12 + 3 + 1 + sector_length + 2 + 24;
664   return DMK_TOC_LEN * 2 + DMK_LEAD_IN + (sectors * sector_physical_length);
665}
666
667
668
669static floperr_t coco_dmk_read_track(floppy_image *floppy, int head, int track, UINT64 offset, void *buffer, size_t buflen)
670{
671   floperr_t err;
672   UINT64 track_offset;
673
674   err = coco_dmk_get_offset(floppy, head, track, &track_offset);
675   if (err)
676      return err;
677
678   floppy_image_read(floppy, buffer, offset + track_offset, buflen);
679   return FLOPPY_ERROR_SUCCESS;
680}
681
682
683
684static floperr_t coco_dmk_write_track(floppy_image *floppy, int head, int track, UINT64 offset, const void *buffer, size_t buflen)
685{
686   floperr_t err;
687   UINT64 track_offset;
688
689   err = coco_dmk_get_offset(floppy, head, track, &track_offset);
690   if (err)
691      return err;
692
693   floppy_image_write(floppy, buffer, offset + track_offset, buflen);
694   return FLOPPY_ERROR_SUCCESS;
695}
696
697
698
699static floperr_t coco_dmk_get_track_data_offset(floppy_image *floppy, int head, int track, UINT64 *offset)
700{
701   *offset = DMK_TOC_LEN + 1;
702   return FLOPPY_ERROR_SUCCESS;
703}
704
705
706
707static floperr_t coco_dmk_format_track(floppy_image *floppy, int head, int track, option_resolution *params)
708{
709   int sectors;
710   int sector_length;
711   int interleave;
712   int first_sector_id;
713   floperr_t err;
714   int physical_sector;
715   int logical_sector;
716   int track_position;
717   UINT16 idam_offset;
718   UINT16 crc;
719   UINT8 *track_data;
720   void *track_data_v;
721   UINT32 max_track_size;
722   int *sector_map = NULL;
723
724   sectors         = option_resolution_lookup_int(params, PARAM_SECTORS);
725   sector_length   = option_resolution_lookup_int(params, PARAM_SECTOR_LENGTH);
726   interleave      = option_resolution_lookup_int(params, PARAM_INTERLEAVE);
727   first_sector_id   = option_resolution_lookup_int(params, PARAM_FIRST_SECTOR_ID);
728
729   max_track_size = get_dmk_tag(floppy)->track_size;
730
731   if (sectors > DMK_TOC_LEN)
732   {
733        err = FLOPPY_ERROR_INTERNAL;
734      goto done;
735   }
736
737   if (max_track_size < coco_dmk_min_track_size(sectors, sector_length))
738   {
739      err = FLOPPY_ERROR_NOSPACE;
740      goto done;
741   }
742
743   err = floppy_load_track(floppy, head, track, TRUE, &track_data_v, NULL);
744   if (err)
745      goto done;
746   track_data = (UINT8 *) track_data_v;
747
748   /* set up sector map */
749   sector_map = (int*)malloc(sectors * sizeof(*sector_map));
750   if (!sector_map)
751   {
752      err = FLOPPY_ERROR_OUTOFMEMORY;
753      goto done;
754   }
755   memset(sector_map, 0xFF, sectors * sizeof(*sector_map));
756
757   physical_sector = 0;
758   for (logical_sector = 0; logical_sector < sectors; logical_sector++)
759   {
760      while(sector_map[physical_sector] >= 0)
761      {
762         physical_sector++;
763         physical_sector %= sectors;
764      }
765
766      sector_map[physical_sector] = logical_sector + first_sector_id;
767      physical_sector += interleave + 1;
768      physical_sector %= sectors;
769   }
770
771   /* set up track table of contents */
772   physical_sector = 0;
773   track_position = DMK_TOC_LEN * 2 + DMK_LEAD_IN;
774   while(physical_sector < DMK_TOC_LEN)
775   {
776      if (physical_sector >= sectors)
777      {
778         /* no more sectors */
779         idam_offset = 0;
780      }
781      else
782      {
783         /* this is a sector */
784         logical_sector = sector_map[physical_sector];
785
786         /* write the sector */
787         memset(&track_data[track_position], 0x00, 8);
788         track_position += 8;
789
790         memset(&track_data[track_position], 0xA1, 3);
791         track_position += 3;
792
793         idam_offset = track_position | 0x8000;
794         dmk_idam_type(            &track_data[track_position]) = 0xFE;
795         dmk_idam_track(            &track_data[track_position]) = track;
796         dmk_idam_side(            &track_data[track_position]) = head;
797         dmk_idam_sector(         &track_data[track_position]) = logical_sector;
798         dmk_idam_sectorlength(      &track_data[track_position]) = compute_log2(sector_length / 128);
799         crc = ccitt_crc16(0xcdb4,   &track_data[track_position], DMK_IDAM_LENGTH - 2);
800         dmk_idam_set_crc(         &track_data[track_position], crc);
801         track_position += DMK_IDAM_LENGTH;
802
803         memset(&track_data[track_position], 0x4E, 22);
804         track_position += 22;
805
806         memset(&track_data[track_position], 0x00, 12);
807         track_position += 12;
808
809         memset(&track_data[track_position], 0xA1, 3);
810         track_position += 3;
811
812         /* write sector body */
813         track_data[track_position] = 0xFB;
814         memset(&track_data[track_position + 1], floppy_get_filler(floppy), sector_length);
815         crc = ccitt_crc16(0xcdb4, &track_data[track_position], sector_length + 1);
816         track_data[track_position + sector_length + 1] = (UINT8) (crc >> 8);
817         track_data[track_position + sector_length + 2] = (UINT8) (crc >> 0);
818         track_position += sector_length + 3;
819
820         /* write sector footer */
821         memset(&track_data[track_position], 0x4E, 24);
822         track_position += 24;
823      }
824
825      /* write the TOC entry */
826      track_data[physical_sector * 2 + 0] = (UINT8) (idam_offset >> 0);
827      track_data[physical_sector * 2 + 1] = (UINT8) (idam_offset >> 8);
828
829      physical_sector++;
830   }
831
832   /* write track lead in */
833   memset(&track_data[physical_sector * 2], 0x4e, DMK_LEAD_IN);
834
835   /* write track footer */
836   assert(max_track_size >= (UINT32)track_position);
837   memset(&track_data[track_position], 0x4e, max_track_size - track_position);
838
839done:
840   if (sector_map)
841      free(sector_map);
842   return FLOPPY_ERROR_SUCCESS;
843}
844
845
846
847static int coco_dmk_get_heads_per_disk(floppy_image *floppy)
848{
849   return get_dmk_tag(floppy)->heads;
850}
851
852
853
854static int coco_dmk_get_tracks_per_disk(floppy_image *floppy)
855{
856   return get_dmk_tag(floppy)->tracks;
857}
858
859
860
861static UINT32 coco_dmk_get_track_size(floppy_image *floppy, int head, int track)
862{
863   return get_dmk_tag(floppy)->track_size;
864}
865
866
867
868static floperr_t coco_dmk_seek_sector_in_track(floppy_image *floppy, int head, int track, int sector, int sector_is_index, int dirtify, UINT8 **sector_data, UINT32 *sector_length)
869{
870   struct dmk_tag *tag = get_dmk_tag(floppy);
871   floperr_t err;
872   UINT32 idam_offset = 0;
873   UINT16 calculated_crc;
874   size_t i;
875   size_t offs;
876   int state;
877   UINT8 *track_data;
878   void *track_data_v;
879   size_t track_length;
880   size_t sec_len;
881
882   err = floppy_load_track(floppy, head, track, dirtify, &track_data_v, &track_length);
883   if (err)
884      return err;
885   track_data = (UINT8 *) track_data_v;
886
887   /* search for matching IDAM */
888   for (i = 0; i < DMK_TOC_LEN; i++)
889   {
890      idam_offset = track_data[i * 2 + 1];
891      idam_offset <<= 8;
892      idam_offset |= track_data[i * 2 + 0];
893      idam_offset &= 0x3FFF;
894
895      if (idam_offset == 0)
896      {
897         /* we've reached the end of the road */
898         i = DMK_TOC_LEN;
899         break;
900      }
901
902      if ((idam_offset + DMK_IDAM_LENGTH) >= tag->track_size)
903         continue;
904
905      calculated_crc = ccitt_crc16(0xCDB4, &track_data[idam_offset], DMK_IDAM_LENGTH - 2);
906
907      if (calculated_crc == dmk_idam_crc(&track_data[idam_offset]))
908      {
909         if (sector_is_index)
910         {
911            /* the sector is indexed; decrement the index and go */
912            if (sector-- == 0)
913               break;
914         }
915         else
916         {
917            /* check IDAM integrity and check for matching sector */
918            if ((sector == dmk_idam_sector(&track_data[idam_offset]))
919/*                  && (track == dmk_idam_track(&track_data[idam_offset]))  */
920/*                  && (head == dmk_idam_side(&track_data[idam_offset]))    */
921               )
922               break;
923         }
924      }
925   }
926
927   if (i >= DMK_TOC_LEN)
928      return FLOPPY_ERROR_SEEKERROR;
929
930   /* we found a matching sector ID */
931   state = 0;
932   offs = idam_offset + DMK_IDAM_LENGTH;
933
934   /* find pattern 0xA1A1FB; this represents the start of a data sector */
935   for (i = 0; i < DMK_DATA_GAP; i++)
936   {
937      /* overflowing the track? */
938      if ((i + offs) >= tag->track_size)
939         return FLOPPY_ERROR_SEEKERROR;
940
941      if (track_data[offs + i] == 0xA1)
942         state++;
943      else if ((track_data[offs + i] == 0xFB) && state)
944         break;
945      else
946         state = 0;
947   }
948   if (i >= DMK_DATA_GAP)
949      return FLOPPY_ERROR_SEEKERROR;
950
951   offs += i + 1;
952   sec_len = 128 << dmk_idam_sectorlength(&track_data[idam_offset]);
953
954   if ((offs + sec_len) > track_length)
955      return FLOPPY_ERROR_INVALIDIMAGE;
956
957   if (sector_data)
958      *sector_data = track_data + offs;
959   if (sector_length)
960      *sector_length = sec_len;
961   return FLOPPY_ERROR_SUCCESS;
962}
963
964
965
966static floperr_t coco_dmk_get_sector_length(floppy_image *floppy, int head, int track, int sector, UINT32 *sector_length)
967{
968   return coco_dmk_seek_sector_in_track(floppy, head, track, sector, FALSE, FALSE, NULL, sector_length);
969}
970
971
972
973static floperr_t coco_dmk_get_indexed_sector_info(floppy_image *floppy, int head, int track, int sector_index, int *cylinder, int *side, int *sector, UINT32 *sector_length, unsigned long *flags)
974{
975   floperr_t err;
976   UINT32 idam_offset;
977   const UINT8 *track_data;
978   void *track_data_v;
979
980   if (sector_index*2 >= DMK_TOC_LEN)
981      return FLOPPY_ERROR_SEEKERROR;
982
983   err = floppy_load_track(floppy, head, track, FALSE, &track_data_v, NULL);
984   if (err)
985      return err;
986   track_data = (UINT8 *) track_data_v;
987
988   idam_offset = track_data[sector_index * 2 + 1];
989   idam_offset <<= 8;
990   idam_offset |= track_data[sector_index * 2 + 0];
991   idam_offset &= 0x3FFF;
992
993   if (idam_offset == 0)
994      return FLOPPY_ERROR_SEEKERROR;
995
996   if (cylinder)
997      *cylinder = dmk_idam_track(&track_data[idam_offset]);
998   if (side)
999      *side = dmk_idam_side(&track_data[idam_offset]);
1000   if (sector)
1001      *sector = dmk_idam_sector(&track_data[idam_offset]);
1002   if (sector_length)
1003      *sector_length = 128 << dmk_idam_sectorlength(&track_data[idam_offset]);
1004   if (flags)
1005      /* TODO: read DAM or DDAM and determine flags */
1006      *flags = 0;
1007   return FLOPPY_ERROR_SUCCESS;
1008}
1009
1010
1011
1012static floperr_t internal_coco_dmk_read_sector(floppy_image *floppy, int head, int track, int sector, int sector_is_index, void *buffer, size_t buflen)
1013{
1014   floperr_t err;
1015   UINT32 sector_length;
1016   UINT16 crc_on_disk;
1017   UINT16 calculated_crc;
1018   UINT8 *sector_data;
1019
1020   err = coco_dmk_seek_sector_in_track(floppy, head, track, sector, sector_is_index, FALSE, &sector_data, &sector_length);
1021   if (err)
1022      return err;
1023
1024   crc_on_disk = sector_data[sector_length + 0];
1025   crc_on_disk <<= 8;
1026   crc_on_disk += sector_data[sector_length + 1];
1027
1028   calculated_crc = ccitt_crc16(0xE295, sector_data, sector_length);
1029   if (calculated_crc != crc_on_disk)
1030      return FLOPPY_ERROR_INVALIDIMAGE;
1031
1032   memcpy(buffer, sector_data, MIN(sector_length, buflen));
1033
1034   return FLOPPY_ERROR_SUCCESS;
1035}
1036
1037
1038
1039static floperr_t internal_coco_dmk_write_sector(floppy_image *floppy, int head, int track, int sector, int sector_is_index, const void *buffer, size_t buflen, int ddam)
1040{
1041   floperr_t err;
1042   UINT32 sector_length;
1043   UINT8 *sector_data;
1044   UINT16 crc;
1045
1046   err = coco_dmk_seek_sector_in_track(floppy, head, track, sector, sector_is_index, TRUE, &sector_data, &sector_length);
1047   if (err)
1048      return err;
1049
1050   if (buflen > sector_length)
1051      return FLOPPY_ERROR_INTERNAL;
1052
1053   memcpy(sector_data, buffer, buflen);
1054
1055   crc = ccitt_crc16(0xE295, sector_data, sector_length);
1056   sector_data[sector_length + 0] = crc >> 8;
1057   sector_data[sector_length + 1] = crc >> 0;
1058   return FLOPPY_ERROR_SUCCESS;
1059}
1060
1061
1062
1063static floperr_t coco_dmk_read_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen)
1064{
1065   return internal_coco_dmk_read_sector(floppy, head, track, sector, FALSE, buffer, buflen);
1066}
1067
1068static floperr_t coco_dmk_write_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam)
1069{
1070   return internal_coco_dmk_write_sector(floppy, head, track, sector, FALSE, buffer, buflen, ddam);
1071}
1072
1073static floperr_t coco_dmk_read_indexed_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen)
1074{
1075   return internal_coco_dmk_read_sector(floppy, head, track, sector, TRUE, buffer, buflen);
1076}
1077
1078static floperr_t coco_dmk_write_indexed_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam)
1079{
1080   return internal_coco_dmk_write_sector(floppy, head, track, sector, TRUE, buffer, buflen, ddam);
1081}
1082
1083
1084
1085static void coco_dmk_interpret_header(floppy_image *floppy, int *heads, int *tracks, int *track_size)
1086{
1087   UINT8 header[DMK_HEADER_LEN];
1088
1089   floppy_image_read(floppy, header, 0, DMK_HEADER_LEN);
1090
1091   if (tracks)
1092      *tracks = header[1];
1093   if (heads)
1094      *heads = (header[4] & 0x10) ? 1 : 2;
1095   if (track_size)
1096      *track_size = ((int) header[3]) * 0x100 + header[2];
1097}
1098
1099
1100
1101FLOPPY_CONSTRUCT(coco_dmk_construct)
1102{
1103   struct FloppyCallbacks *callbacks;
1104   struct dmk_tag *tag;
1105   UINT8 header[DMK_HEADER_LEN];
1106   int heads, tracks, track_size, sectors, sector_length;
1107
1108   if (params)
1109   {
1110      heads         = option_resolution_lookup_int(params, PARAM_HEADS);
1111      tracks         = option_resolution_lookup_int(params, PARAM_TRACKS);
1112      sectors         = option_resolution_lookup_int(params, PARAM_SECTORS);
1113      sector_length   = option_resolution_lookup_int(params, PARAM_SECTOR_LENGTH);
1114
1115      track_size = coco_dmk_min_track_size(sectors, sector_length) + DMK_EXTRA_TRACK_LENGTH;
1116
1117      memset(header, 0, sizeof(header));
1118      header[1] = tracks;
1119      header[2] = track_size >> 0;
1120      header[3] = track_size >> 8;
1121      header[4] = (heads == 2) ? 0x00 : 0x10;
1122
1123      floppy_image_write(floppy, header, 0, sizeof(header));
1124   }
1125   else
1126   {
1127      coco_dmk_interpret_header(floppy, &heads, &tracks, &track_size);
1128   }
1129
1130   tag = (dmk_tag *)floppy_create_tag(floppy, sizeof(struct dmk_tag));
1131   if (!tag)
1132      return FLOPPY_ERROR_OUTOFMEMORY;
1133   tag->heads = heads;
1134   tag->track_size = track_size;
1135   tag->tracks = tracks;
1136
1137   callbacks = floppy_callbacks(floppy);
1138   callbacks->read_track = coco_dmk_read_track;
1139   callbacks->write_track = coco_dmk_write_track;
1140   callbacks->get_track_data_offset = coco_dmk_get_track_data_offset;
1141   callbacks->format_track = coco_dmk_format_track;
1142   callbacks->get_heads_per_disk = coco_dmk_get_heads_per_disk;
1143   callbacks->get_tracks_per_disk = coco_dmk_get_tracks_per_disk;
1144   callbacks->get_track_size = coco_dmk_get_track_size;
1145   callbacks->get_sector_length = coco_dmk_get_sector_length;
1146   callbacks->get_indexed_sector_info = coco_dmk_get_indexed_sector_info;
1147   callbacks->read_sector = coco_dmk_read_sector;
1148   callbacks->write_sector = coco_dmk_write_sector;
1149   callbacks->read_indexed_sector = coco_dmk_read_indexed_sector;
1150   callbacks->write_indexed_sector = coco_dmk_write_indexed_sector;
1151
1152   return FLOPPY_ERROR_SUCCESS;
1153}
1154
1155
1156
1157FLOPPY_IDENTIFY(coco_dmk_identify)
1158{
1159   int heads, tracks, track_size;
1160   UINT64 size, expected_size;
1161
1162   size = floppy_image_size(floppy);
1163   coco_dmk_interpret_header(floppy, &heads, &tracks, &track_size);
1164   expected_size = DMK_HEADER_LEN + (heads * tracks * track_size);
1165   *vote = (size == expected_size) ? 100 : 0;
1166   return FLOPPY_ERROR_SUCCESS;
1167}
1168
1169
1170
1171/* ----------------------------------------------------------------------- */
1172
1173FLOPPY_OPTIONS_START( coco )
1174   FLOPPY_OPTION( coco_jvc, "dsk",         "CoCo JVC disk image",   coco_jvc_identify,   coco_jvc_construct, NULL,
1175      HEADS([1]-2)
1176      TRACKS([35]-255)
1177      SECTORS(1-[18]-255)
1178      SECTOR_LENGTH(128/[256]/512/1024)
1179      FIRST_SECTOR_ID(0-[1]))
1180   FLOPPY_OPTION( coco_os9, "os9",         "CoCo OS-9 disk image",   coco_os9_identify,   coco_os9_construct, NULL,
1181      HEADS([1]-2)
1182      TRACKS([35]-255)
1183      SECTORS(1-[18]-255)
1184      SECTOR_LENGTH([256])
1185      FIRST_SECTOR_ID([1]))
1186   FLOPPY_OPTION( coco_vdk, "vdk",         "CoCo VDK disk image",   coco_vdk_identify,   coco_vdk_construct, NULL,
1187      HEADS([1]-2)
1188      TRACKS([35]-255)
1189      SECTORS([18])
1190      SECTOR_LENGTH([256])
1191      FIRST_SECTOR_ID([1]))
1192   FLOPPY_OPTION( coco_dmk, "dsk,dmk",   "CoCo DMK disk image",   coco_dmk_identify,   coco_dmk_construct, NULL,
1193      HEADS([1]-2)
1194      TRACKS([35]-255)
1195      SECTORS(1-[18])
1196      SECTOR_LENGTH(128/[256]/512/1024/2048/4096/8192)
1197      INTERLEAVE(0-[6]-17)
1198      FIRST_SECTOR_ID(0-[1]))
1199FLOPPY_OPTIONS_END
src/mess/formats/nes_dsk.c
r11457r11458
1/*********************************************************************
2
3    formats/nes_dsk.c
4
5    NES disk images
6
7*********************************************************************/
8
9
10#include <string.h>
11
12#include "formats/nes_dsk.h"
13#include "formats/basicdsk.h"
14
15
16
17/*****************************************************************************
18 NES floppy core functions
19*****************************************************************************/
20
21
22static FLOPPY_IDENTIFY( nes_dsk_identify )
23{
24   UINT64 size;
25   UINT8 header[3];
26
27   *vote = 0;
28
29   /* get first 3 bytes */
30   floppy_image_read(floppy, &header, 0, sizeof(header));
31
32   /* first check the size of the image */
33   size = floppy_image_size(floppy);
34
35   if ((size == 65516) || (size == 131016) || (size == 262016))
36   {
37      /* the image has an header, hence check the first sector for the magic string */
38      if (!memcmp(header, "FDS", 3))
39         *vote = 100;
40   }
41
42   if ((size == 65500) || (size == 131000) || (size == 262000))
43   {
44      /* the image has no header, hence let's trust the extension and load the file */
45      *vote = 100;
46   }
47
48   return FLOPPY_ERROR_SUCCESS;
49}
50
51
52static FLOPPY_CONSTRUCT( nes_dsk_construct )
53{
54   return FLOPPY_ERROR_SUCCESS;
55}
56
57
58/*****************************************************************************
59 NES floppy options
60*****************************************************************************/
61
62FLOPPY_OPTIONS_START( nes_only )
63   FLOPPY_OPTION(
64      fds_dsk,
65      "fds",
66      "NES floppy disk image",
67      nes_dsk_identify,
68      nes_dsk_construct,
69      NULL,
70      NULL
71   )
72FLOPPY_OPTIONS_END0
src/mess/formats/csw_cas.h
r11457r11458
1/*
2    csw_cas.h
3
4    Format code for csw cassette files
5*/
6
7#ifndef CSW_CAS_H
8#define CSW_CAS_H
9
10#include "imagedev/cassimg.h"
11
12CASSETTE_FORMATLIST_EXTERN(csw_cassette_formats);
13CASSETTE_FORMATLIST_EXTERN(bbc_cassette_formats);
14
15#endif /* CSW_CAS_H */
src/mess/formats/coco_dsk.h
r11457r11458
1/*********************************************************************
2
3    formats/coco_dsk.h
4
5    Tandy Color Computer / Dragon disk images
6
7*********************************************************************/
8
9#ifndef COCO_DSK_H
10#define COCO_DSK_H
11
12#include "imagedev/flopimg.h"
13
14
15/**************************************************************************/
16
17FLOPPY_OPTIONS_EXTERN(coco);
18
19FLOPPY_IDENTIFY(coco_dmk_identify);
20FLOPPY_CONSTRUCT(coco_dmk_construct);
21
22#endif /* COCO_DSK_H */
src/mess/formats/nes_dsk.h
r11457r11458
1/*********************************************************************
2
3    formats/nes_dsk.h
4
5    NES disk images
6
7*********************************************************************/
8
9#ifndef NES_DSK_H_
10#define NES_DSK_H_
11
12#include "imagedev/flopimg.h"
13
14FLOPPY_OPTIONS_EXTERN(nes_only);
15
16#endif /*NES_DSK_H_*/
src/mess/formats/x1_tap.c
r11457r11458
1/*
2 *
3 *   Sharp X1 TAP image format
4 *
5 *   "New" header format:
6 *   0x00: Index - must be "TAPE" (4 bytes)
7 *   0x04: Image title. (null-terminated string, 17 bytes)
8 *   0x15: Reserved (5 bytes)
9 *   0x1a: Write protect (bit 4, 1 byte)
10 *   0x1b: Format (bit 0, 1 byte)
11 *            if bit 0 is high, uses "speed limit sampling method"
12 *   0x1c: Sample rate, per bit (in Hz, 4 bytes)
13 *   0x20: Tape data size, in bits (4 bytes)
14 *   0x24: Tape position (4 bytes, usually 0)
15 *   0x28: Tape data (data size / 8)
16 *
17 *   "Old" header format:
18 *   0x00: Sampling rate (4 bytes)
19 *
20 */
21
22#include "emu.h"
23#include "x1_tap.h"
24
25#define WAVE_HIGH       0x5a9e
26#define WAVE_LOW      -0x5a9e
27
28static int cas_size;
29static int samplerate;
30
31static int x1_fill_wave(INT16* buffer, UINT8 data, int sample_pos)
32{
33   int x;
34   int sample_count = 0;
35
36   // one byte = 8 samples
37   for(x=0;x<8;x++)
38   {
39      if(buffer)
40         buffer[sample_pos+x] = (data & (1 << (7-x))) ? WAVE_HIGH : WAVE_LOW;
41      sample_count++;
42   }
43
44   return sample_count;
45}
46
47static int x1_handle_tap(INT16* buffer, const UINT8* casdata)
48{
49   int sample_count = 0;
50   int data_pos = 0x28;
51
52   if(memcmp(casdata, "TAPE",4))  // header check
53   {
54      logerror("TAP: image is not a 'new' format TAP image\n");
55      return -1;
56   }
57   if(samplerate != 8000)
58   {
59      logerror("TAP: images that are not 8000Hz are not yet supported\n");
60      return -1;
61   }
62
63   while(sample_count < cas_size)
64   {
65      sample_count += x1_fill_wave(buffer,casdata[data_pos],sample_count);
66      data_pos++;
67   }
68
69   return sample_count;
70}
71
72/*******************************************************************
73   Calculate the number of samples needed for this tape image
74********************************************************************/
75static int x1_cas_to_wav_size (const UINT8 *casdata, int caslen)
76{
77   UINT32 ret;
78
79   ret = casdata[0x20] | (casdata[0x21] << 8) | (casdata[0x22] << 16) | (casdata[0x23] << 24);
80   cas_size = ret;
81
82   samplerate = casdata[0x1c] | (casdata[0x1d] << 8) | (casdata[0x1e] << 16) | (casdata[0x1f] << 24);
83
84   return ret;
85}
86
87/*******************************************************************
88   Generate samples for the tape image
89********************************************************************/
90static int x1_cas_fill_wave(INT16 *buffer, int sample_count, UINT8 *bytes)
91{
92   return x1_handle_tap(buffer,bytes);
93}
94
95static const struct CassetteLegacyWaveFiller x1_legacy_fill_wave =
96{
97   x1_cas_fill_wave,                  /* fill_wave */
98   -1,                              /* chunk_size */
99   0,                              /* chunk_samples */
100   x1_cas_to_wav_size,               /* chunk_sample_calc */
101   8000,                           /* sample_frequency */
102   0,                              /* header_samples */
103   0                              /* trailer_samples */
104};
105
106static casserr_t x1_cas_identify(cassette_image *cassette, struct CassetteOptions *opts)
107{
108   return cassette_legacy_identify(cassette, opts, &x1_legacy_fill_wave);
109}
110
111
112
113static casserr_t x1_cas_load(cassette_image *cassette)
114{
115   return cassette_legacy_construct(cassette, &x1_legacy_fill_wave);
116}
117
118
119static const struct CassetteFormat x1_cassette_format = {
120   "tap",
121   x1_cas_identify,
122   x1_cas_load,
123   NULL
124};
125
126CASSETTE_FORMATLIST_START(x1_cassette_formats)
127   CASSETTE_FORMAT(x1_cassette_format)
128CASSETTE_FORMATLIST_END
src/mess/formats/x1_tap.h
r11457r11458
1/*
2 *
3 *   Sharp X1 TAP image format
4 *
5 */
6
7#ifndef X1_TAP_H_
8#define X1_TAP_H_
9
10#include "imagedev/cassimg.h"
11
12CASSETTE_FORMATLIST_EXTERN( x1_cassette_formats );
13
14#endif /*X1_TAP_H_*/
src/mess/formats/apridisk.c
r11457r11458
1/***************************************************************************
2
3    APRIDISK disk image format
4
5***************************************************************************/
6
7#include "apridisk.h"
8#include "imageutl.h"
9
10/***************************************************************************
11    CONSTANTS
12***************************************************************************/
13
14#define APR_HEADER_SIZE      128
15
16/* sector types */
17#define APR_DELETED      0xe31d0000
18#define APR_MAGIC      0xe31d0001
19#define APR_COMMENT      0xe31d0002
20#define APR_CREATOR      0xe31d0003
21
22/* compression type */
23#define APR_UNCOMPRESSED   0x9e90
24#define APR_COMPRESSED      0x3e5a
25
26static const char *apr_magic = "ACT Apricot disk image\x1a\x04";
27
28
29/***************************************************************************
30    TYPE DEFINITIONS
31***************************************************************************/
32
33struct apr_sector
34{
35   int head;
36   int track;
37   int sector;
38   UINT8 data[512];
39};
40
41struct apr_tag
42{
43   int heads;
44   int tracks;
45   int sectors_per_track;
46   struct apr_sector sectors[2880];
47   char *comment;
48};
49
50
51/***************************************************************************
52    IMPLEMENTATION
53***************************************************************************/
54
55/* sector format:
56 *
57 * 00-03: sector type
58 * 04-05: compression type
59 * 06-07: header size
60 * 08-11: data size
61 * 12-13: track
62 *    14: head
63 *    15: sector
64 */
65
66static floperr_t apr_read_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen)
67{
68   struct apr_tag *tag = (apr_tag *)floppy_tag(floppy);
69//  printf("apr_read_sector %d %d %d\n", head, track, sector);
70   memcpy(buffer, tag->sectors[head * track * sector].data, buflen);
71   return FLOPPY_ERROR_SUCCESS;
72}
73
74static floperr_t apr_read_indexed_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen)
75{
76   struct apr_tag *tag = (apr_tag *)floppy_tag(floppy);
77//  printf("apr_read_indexed_sector %d %d %d\n", head, track, sector);
78   memcpy(buffer, tag->sectors[head * track * sector].data, buflen);
79   return FLOPPY_ERROR_SUCCESS;
80}
81
82/* sector length is always 512 byte */
83static floperr_t apr_get_sector_length(floppy_image *floppy, int head, int track, int sector, UINT32 *sector_length)
84{
85   *sector_length = 512;
86   return FLOPPY_ERROR_SUCCESS;
87}
88
89static int apr_get_heads_per_disk(floppy_image *floppy)
90{
91   struct apr_tag *tag = (apr_tag *)floppy_tag(floppy);
92   return tag->heads;
93}
94
95static int apr_get_tracks_per_disk(floppy_image *floppy)
96{
97   struct apr_tag *tag = (apr_tag *)floppy_tag(floppy);
98   return tag->tracks;
99}
100
101static floperr_t apr_get_indexed_sector_info(floppy_image *floppy, int head, int track, int sector_index, int *cylinder, int *side, int *sector, UINT32 *sector_length, unsigned long *flags)
102{
103   struct apr_tag *tag = (apr_tag *)floppy_tag(floppy);
104
105//  printf("apr_get_indexed_sector_info %d %d %d\n", head, track, sector_index);
106
107   /* sanity checks */
108   if (head         < 0 || head         > (tag->heads - 1))             return FLOPPY_ERROR_SEEKERROR;
109   if (track        < 0 || track        > (tag->tracks - 1))            return FLOPPY_ERROR_SEEKERROR;
110   if (sector_index < 0 || sector_index > (tag->sectors_per_track - 1)) return FLOPPY_ERROR_SEEKERROR;
111
112   if (cylinder)      *cylinder = tag->sectors[head * track * sector_index].track;
113   if (side)          *side = tag->sectors[head * track * sector_index].head;
114   if (sector)        *sector = tag->sectors[head * track * sector_index].sector;
115   if (sector_length) *sector_length = 512;
116
117   return FLOPPY_ERROR_SUCCESS;
118}
119
120FLOPPY_IDENTIFY( apridisk_identify )
121{
122   UINT8 header[APR_HEADER_SIZE];
123
124   /* get header */
125   floppy_image_read(floppy, &header, 0, sizeof(header));
126
127   /* look for the magic string */
128   if (memcmp(header, apr_magic, sizeof(apr_magic)) == 0)
129      *vote = 100;
130   else
131      *vote = 0;
132
133   return FLOPPY_ERROR_SUCCESS;
134}
135
136FLOPPY_CONSTRUCT( apridisk_construct )
137{
138   struct FloppyCallbacks *callbacks;
139   struct apr_tag *tag;
140
141   int cur_sector = 0;
142   UINT8 sector_header[16];
143   UINT64 pos = 128;
144   UINT32 type;
145
146   tag = (apr_tag *)floppy_create_tag(floppy, sizeof(struct apr_tag));
147
148   if (!tag)
149      return FLOPPY_ERROR_OUTOFMEMORY;
150
151   tag->heads = 0;
152   tag->tracks = 0;
153   tag->sectors_per_track = 0;
154
155   floppy_image_read(floppy, &sector_header, pos, 16);
156   type = pick_integer_le(&sector_header, 0, 4);
157
158   while (type == APR_DELETED || type == APR_MAGIC || type == APR_COMMENT || type == APR_CREATOR)
159   {
160      UINT16 compression = pick_integer_le(&sector_header, 4, 2);
161      UINT16 header_size = pick_integer_le(&sector_header, 6, 2);
162      UINT32 data_size = pick_integer_le(&sector_header, 8, 4);
163
164      tag->sectors[cur_sector].head = pick_integer_le(&sector_header, 12, 1);
165      tag->sectors[cur_sector].sector = pick_integer_le(&sector_header, 13, 1);
166      tag->sectors[cur_sector].track = pick_integer_le(&sector_header, 14, 1);
167
168      pos += header_size;
169
170      if (type == APR_MAGIC)
171      {
172         if (compression == APR_UNCOMPRESSED)
173         {
174            floppy_image_read(floppy, &tag->sectors[cur_sector].data, pos, data_size);
175
176         }
177         else if (compression == APR_COMPRESSED)
178         {
179            UINT8 *buffer = (UINT8 *)osd_malloc(data_size * sizeof(UINT8));
180            UINT16 length;
181            UINT8 value;
182
183            floppy_image_read(floppy, buffer, pos, data_size);
184
185            length = pick_integer_le(buffer, 0, 2);
186            value = pick_integer_le(buffer, 2, 1);
187
188            osd_free(buffer);
189
190            /* not sure if this is possible */
191            if (length != 512)
192               fatalerror("Compression unsupported");
193
194            memset(&tag->sectors[cur_sector].data, value, length);
195         }
196         else
197            return FLOPPY_ERROR_INVALIDIMAGE;
198
199         tag->heads = MAX(tag->sectors[cur_sector].head, tag->heads);
200         tag->tracks = MAX(tag->sectors[cur_sector].track, tag->tracks);
201         tag->sectors_per_track = MAX(tag->sectors[cur_sector].sector, tag->sectors_per_track);
202      }
203
204      /* seek to next sector */
205      pos += data_size;
206      cur_sector++;
207
208      floppy_image_read(floppy, &sector_header, pos, 16);
209      type = pick_integer_le(&sector_header, 0, 4);
210   }
211
212   tag->heads++;
213   tag->tracks++;
214
215   callbacks = floppy_callbacks(floppy);
216   callbacks->read_sector = apr_read_sector;
217   callbacks->read_indexed_sector = apr_read_indexed_sector;
218   callbacks->get_sector_length = apr_get_sector_length;
219   callbacks->get_heads_per_disk = apr_get_heads_per_disk;
220   callbacks->get_tracks_per_disk = apr_get_tracks_per_disk;
221   callbacks->get_indexed_sector_info = apr_get_indexed_sector_info;
222
223   return FLOPPY_ERROR_SUCCESS;
224}
src/mess/formats/msx_dsk.c
r11457r11458
1/*********************************************************************
2
3    formats/msx_dsk.c
4
5    MSX disk images
6
7*********************************************************************/
8
9#include "msx_dsk.h"
10#include "formats/basicdsk.h"
11
12FLOPPY_OPTIONS_START(msx)
13   FLOPPY_OPTION(msx, "dsk", "MSX SS", basicdsk_identify_default, basicdsk_construct_default, NULL,
14      HEADS([1])
15      TRACKS([80])
16      SECTORS([9])
17      SECTOR_LENGTH([512])
18      FIRST_SECTOR_ID([1]))
19   FLOPPY_OPTION(msx, "dsk", "MSX DS", basicdsk_identify_default, basicdsk_construct_default, NULL,
20      HEADS([2])
21      TRACKS([80])
22      SECTORS([9])
23      SECTOR_LENGTH([512])
24      FIRST_SECTOR_ID([1]))
25FLOPPY_OPTIONS_END
src/mess/formats/ap_dsk35.c
r11457r11458
1/*********************************************************************
2
3    ap_dsk35.c
4
5    Apple 3.5" disk images
6
7    This code supports 3.5" 400k/800k disks used in early Macintoshes
8    and the Apple IIgs, and 3.5" 1440k MFM disks used on most Macintoshes.
9
10    These disks have the following properties:
11
12        400k:   80 tracks, 1 head
13        800k:   80 tracks, 2 heads
14
15        Tracks  0-15 have 12 sectors each
16        Tracks 16-31 have 11 sectors each
17        Tracks 32-47 have 10 sectors each
18        Tracks 48-63 have  9 sectors each
19        Tracks 64-79 have  8 sectors each
20
21        1440k disks are simply 80 tracks, 2 heads and 18 sectors per track.
22
23    Each sector on 400/800k disks has 524 bytes, 512 of which are really used by the Macintosh
24
25    (80 tracks) * (avg of 10 sectors) * (512 bytes) * (2 sides) = 800 kB
26
27    Data is nibblized : 3 data bytes -> 4 bytes on disk.
28
29    In addition to 512 logical bytes, each sector contains 800 physical
30    bytes.  Here is the layout of the physical sector:
31
32        Pos
33        0       0xFF (pad byte where head is turned on ???)
34        1-35    self synch 0xFFs (7*5) (42 bytes actually written to media)
35        36      0xD5
36        37      0xAA
37        38      0x96
38        39      diskbytes[(track number) & 0x3F]
39        40      diskbytes[(sector number)]
40        41      diskbytes[("side")]
41        42      diskbytes[("format byte")]
42        43      diskbytes[("sum")]
43        44      0xDE
44        45      0xAA
45        46      pad byte where head is turned off/on (0xFF here)
46        47-51   self synch 0xFFs (6 bytes actually written to media)
47        52      0xD5
48        53      0xAA
49        54      0xAD
50        55      spare byte, generally diskbytes[(sector number)]
51        56-754  "nibblized" sector data ...
52        755-758 checksum
53        759     0xDE
54        760     0xAA
55        761     pad byte where head is turned off (0xFF here)
56
57    MFM Track layout for 1440K disks:
58        Pos
59    --------- track ID
60    0   0x4E (x80)
61    80  00 (x12)
62    92  C2 (x3) Mark byte
63    93  FC Index mark
64    94  4E (x50)
65    --------- sector ID
66    144 00 (x12)
67    156 A1 (x3) Mark byte
68    159 FE Address mark
69    160 xx Track number
70    161 xx Side number
71    162 xx Sector number
72    163 xx Sector length
73    164/165 xx CRC
74    166 4E (x22)
75    --------- sector data
76    188 00 (x12)
77    200 A1 (x3) Mark byte
78    203 FB Data address mark
79    204 xx (x256) data
80    460 4E (x54)
81    (repeat from sector ID to fill track; end of track is padded with 4E)
82
83    Note : "Self synch refers to a technique whereby two zeroes are inserted
84    between each synch byte written to the disk.", i.e. "0xFF, 0xFF, 0xFF,
85    0xFF, 0xFF" is actually "0xFF, 0x3F, 0xCF, 0xF3, 0xFC, 0xFF" on disk.
86    Since the IWM assumes the data transfer is complete when the MSBit of its
87    shift register is 1, we do read 4 0xFF, even though they are not
88    contiguous on the disk media.  Some reflexion shows that 4 synch bytes
89    allow the IWM to synchronize with the trailing data.
90
91    Format byte codes:
92        0x00    Apple II
93        0x01    Lisa
94        0x02    Mac MFS (single sided)?
95        0x22    Mac MFS (double sided)?
96
97*********************************************************************/
98
99#include <stdio.h>
100#include <assert.h>
101
102#include "formats/ap_dsk35.h"
103
104struct apple35_tag
105{
106   UINT32 data_offset;
107   UINT32 data_size;
108   UINT8 format_byte;
109   UINT8 sides;
110   unsigned int is_1440k : 1;
111
112   /* stuff used in DiskCopy images */
113   UINT32 tag_offset;
114   UINT32 tag_size;
115};
116
117
118
119/* normal number of sector for each track */
120static const UINT8 apple35_tracklen_800kb[80] =
121{
122   12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
123   11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
124   10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
125    9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,
126    8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8
127};
128
129/* blocks of data used to nibblize tracks */
130static const UINT8 diskbytes[] =
131{
132   0x96, 0x97, 0x9A, 0x9B,  0x9D, 0x9E, 0x9F, 0xA6, /* 0x00 */
133   0xA7, 0xAB, 0xAC, 0xAD,  0xAE, 0xAF, 0xB2, 0xB3,
134   0xB4, 0xB5, 0xB6, 0xB7,  0xB9, 0xBA, 0xBB, 0xBC, /* 0x10 */
135   0xBD, 0xBE, 0xBF, 0xCB,  0xCD, 0xCE, 0xCF, 0xD3,
136   0xD6, 0xD7, 0xD9, 0xDA,  0xDB, 0xDC, 0xDD, 0xDE, /* 0x20 */
137   0xDF, 0xE5, 0xE6, 0xE7,  0xE9, 0xEA, 0xEB, 0xEC,
138   0xED, 0xEE, 0xEF, 0xF2,  0xF3, 0xF4, 0xF5, 0xF6, /* 0x30 */
139   0xF7, 0xF9, 0xFA, 0xFB,  0xFC, 0xFD, 0xFE, 0xFF
140};
141
142/* reverse lookup of diskbytes */
143static const INT16 rev_diskbytes[] =
144{
145   -1,   -1,   -1,   -1,    -1,   -1,   -1,   -1,   /* 0x00 */
146   -1,   -1,   -1,   -1,    -1,   -1,   -1,   -1,
147   -1,   -1,   -1,   -1,    -1,   -1,   -1,   -1,   /* 0x10 */
148   -1,   -1,   -1,   -1,    -1,   -1,   -1,   -1,
149   -1,   -1,   -1,   -1,    -1,   -1,   -1,   -1,   /* 0x20 */
150   -1,   -1,   -1,   -1,    -1,   -1,   -1,   -1,
151   -1,   -1,   -1,   -1,    -1,   -1,   -1,   -1,   /* 0x30 */
152   -1,   -1,   -1,   -1,    -1,   -1,   -1,   -1,
153   -1,   -1,   -1,   -1,    -1,   -1,   -1,   -1,   /* 0x40 */
154   -1,   -1,   -1,   -1,    -1,   -1,   -1,   -1,
155   -1,   -1,   -1,   -1,    -1,   -1,   -1,   -1,   /* 0x50 */
156   -1,   -1,   -1,   -1,    -1,   -1,   -1,   -1,
157   -1,   -1,   -1,   -1,    -1,   -1,   -1,   -1,   /* 0x60 */
158   -1,   -1,   -1,   -1,    -1,   -1,   -1,   -1,
159   -1,   -1,   -1,   -1,    -1,   -1,   -1,   -1,   /* 0x70 */
160   -1,   -1,   -1,   -1,    -1,   -1,   -1,   -1,
161   -1,   -1,   -1,   -1,    -1,   -1,   -1,   -1,   /* 0x80 */
162   -1,   -1,   -1,   -1,    -1,   -1,   -1,   -1,
163   -1,   -1,   -1,   -1,    -1,   -1,   0x00, 0x01, /* 0x90 */
164   -1,   -1,   0x02, 0x03,  -1,   0x04, 0x05, 0x06,
165   -1,   -1,   -1,   -1,    -1,   -1,   0x07, 0x08, /* 0xA0 */
166   -1,   -1,   -1,   0x09,  0x0A, 0x0B, 0x0C, 0x0D,
167   -1,   -1,   0x0E, 0x0F,  0x10, 0x11, 0x12, 0x13, /* 0xB0 */
168   -1,   0x14, 0x15, 0x16,  0x17, 0x18, 0x19, 0x1A,
169   -1,   -1,   -1,   -1,    -1,   -1,   -1,   -1,   /* 0xC0 */
170   -1,   -1,   -1,   0x1B,  -1,   0x1C, 0x1D, 0x1E,
171   -1,   -1,   -1,   0x1F,  -1,   -1,   0x20, 0x21, /* 0xD0 */
172   -1,   0x22, 0x23, 0x24,  0x25, 0x26, 0x27, 0x28,
173   -1,   -1,   -1,   -1,    -1,   0x29, 0x2A, 0x2B, /* 0xE0 */
174   -1,   0x2C, 0x2D, 0x2E,  0x2F, 0x30, 0x31, 0x32,
175   -1,   -1,   0x33, 0x34,  0x35, 0x36, 0x37, 0x38, /* 0xF0 */
176   -1,   0x39, 0x3A, 0x3B,  0x3C, 0x3D, 0x3E, 0x3F
177};
178
179static const UINT8 blk1[] =
180{
181   /*0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
182    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
183    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
184    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
185    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
186    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
187    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
188    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
189    0xFF, 0xFF, 0xD5, 0xAA, 0x96*/
190   0xFF,
191   0xFF, 0x3F, 0xCF, 0xF3, 0xFC, 0xFF,
192   0xFF, 0x3F, 0xCF, 0xF3, 0xFC, 0xFF,
193   0xFF, 0x3F, 0xCF, 0xF3, 0xFC, 0xFF,
194   0xFF, 0x3F, 0xCF, 0xF3, 0xFC, 0xFF,
195   0xFF, 0x3F, 0xCF, 0xF3, 0xFC, 0xFF,
196   0xFF, 0x3F, 0xCF, 0xF3, 0xFC, 0xFF,
197   0xFF, 0x3F, 0xCF, 0xF3, 0xFC, 0xFF,
198   0xD5, 0xAA, 0x96
199};
200static const UINT8 blk2[] =
201{
202   0xDE, 0xAA, 0xFF, 0xFF, 0x3F, 0xCF, 0xF3, 0xFC, 0xFF, 0xD5, 0xAA, 0xAD
203};
204static const UINT8 blk3[] =
205{
206   0xDE, 0xAA, 0xFF
207};
208
209
210
211static struct apple35_tag *get_apple35_tag(floppy_image *floppy)
212{
213   struct apple35_tag *tag;
214   tag = (struct apple35_tag *) floppy_tag(floppy);
215   return tag;
216}
217
218
219
220int apple35_sectors_per_track(floppy_image *image, int track)
221{
222   int sectors;
223
224   assert(track >= 0);
225   assert(track < ARRAY_LENGTH(apple35_tracklen_800kb));
226
227   if (get_apple35_tag(image)->is_1440k)
228      sectors = 18;
229   else
230      sectors = apple35_tracklen_800kb[track];
231   return sectors;
232}
233
234
235
236/*
237    converts data to its nibblized representation, and generate checksum
238
239    now handles 524-byte-long sectors
240
241    tag data IS important, since it allows data recovery when the catalog is trashed
242*/
243static void sony_nibblize35(const UINT8 *in, UINT8 *nib_ptr, UINT8 *csum)
244{
245   int   i, j;
246   UINT32 c1, c2, c3, c4;
247   UINT8 val;
248   UINT8 w1, w2, w3, w4;
249   UINT8 b1[175], b2[175], b3[175];
250
251   /* Copy from the user's buffer to our buffer, while computing
252     * the three-byte data checksum
253     */
254
255   i = 0;
256   j = 0;
257   c1 = 0;
258   c2 = 0;
259   c3 = 0;
260   while (1)
261   {
262      c1 = (c1 & 0xFF) << 1;
263      if (c1 & 0x0100)
264         c1++;
265
266      val = in[i++];
267      c3 += val;
268      if (c1 & 0x0100)
269      {
270         c3++;
271         c1 &= 0xFF;
272      }
273      b1[j] = (val ^ c1) & 0xFF;
274
275      val = in[i++];
276      c2 += val;
277      if (c3 > 0xFF)
278      {
279         c2++;
280         c3 &= 0xFF;
281      }
282      b2[j] = (val ^ c3) & 0xFF;
283
284      if (i == 524) break;
285
286      val = in[i++];
287      c1 += val;
288      if (c2 > 0xFF)
289      {
290         c1++;
291         c2 &= 0xFF;
292      }
293      b3[j] = (val ^ c2) & 0xFF;
294      j++;
295   }
296   c4 =  ((c1 & 0xC0) >> 6) | ((c2 & 0xC0) >> 4) | ((c3 & 0xC0) >> 2);
297   b3[174] = 0;
298
299   j = 0;
300   for (i = 0; i <= 174; i++)
301   {
302      w1 = b1[i] & 0x3F;
303      w2 = b2[i] & 0x3F;
304      w3 = b3[i] & 0x3F;
305      w4 =  ((b1[i] & 0xC0) >> 2);
306      w4 |= ((b2[i] & 0xC0) >> 4);
307      w4 |= ((b3[i] & 0xC0) >> 6);
308
309      nib_ptr[j++] = w4;
310      nib_ptr[j++] = w1;
311      nib_ptr[j++] = w2;
312
313      if (i != 174) nib_ptr[j++] = w3;
314   }
315
316   csum[0] = c1 & 0x3F;
317   csum[1] = c2 & 0x3F;
318   csum[2] = c3 & 0x3F;
319   csum[3] = c4 & 0x3F;
320}
321
322
323
324/*
325    does the reverse process of sony_nibblize35
326*/
327static void sony_denibblize35(UINT8 *out, const UINT8 *nib_ptr, UINT8 *checksum)
328{
329   int   i, j;
330   UINT32 c1,c2,c3,c4;
331   UINT8 val;
332   UINT8 w1,w2,w3=0,w4;
333   UINT8 b1[175],b2[175],b3[175];
334
335   j = 0;
336   for (i=0; i<=174; i++)
337   {
338      w4 = nib_ptr[j++];
339      w1 = nib_ptr[j++];
340      w2 = nib_ptr[j++];
341
342      if (i != 174) w3 = nib_ptr[j++];
343
344      b1[i] = (w1 & 0x3F) | ((w4 << 2) & 0xC0);
345      b2[i] = (w2 & 0x3F) | ((w4 << 4) & 0xC0);
346      b3[i] = (w3 & 0x3F) | ((w4 << 6) & 0xC0);
347   }
348
349   /* Copy from the user's buffer to our buffer, while computing
350     * the three-byte data checksum
351     */
352
353   i = 0;
354   j = 0;
355   c1 = 0;
356   c2 = 0;
357   c3 = 0;
358   while (1)
359   {
360      c1 = (c1 & 0xFF) << 1;
361      if (c1 & 0x0100)
362         c1++;
363
364      val = (b1[j] ^ c1) & 0xFF;
365      c3 += val;
366      if (c1 & 0x0100)
367      {
368         c3++;
369         c1 &= 0xFF;
370      }
371      out[i++] = val;
372
373      val = (b2[j] ^ c3) & 0xFF;
374      c2 += val;
375      if (c3 > 0xFF)
376      {
377         c2++;
378         c3 &= 0xFF;
379      }
380      out[i++] = val;
381
382      if (i == 524) break;
383
384      val = (b3[j] ^ c2) & 0xFF;
385      c1 += val;
386      if (c2 > 0xFF)
387      {
388         c1++;
389         c2 &= 0xFF;
390      }
391      out[i++] = val;
392      j++;
393   }
394   c4 =  ((c1 & 0xC0) >> 6) | ((c2 & 0xC0) >> 4) | ((c3 & 0xC0) >> 2);
395   b3[174] = 0;
396
397   checksum[0] = c1 & 0x3F;
398   checksum[1] = c2 & 0x3F;
399   checksum[2] = c3 & 0x3F;
400   checksum[3] = c4 & 0x3F;
401}
402
403
404
405void sony_filltrack(UINT8 *buffer, size_t buffer_len, size_t *pos, UINT8 data)
406{
407   buffer[*pos / 8] &= 0xFF << (8 - (*pos % 8));
408   buffer[*pos / 8] |= data >> (*pos % 8);
409
410   *pos += 8;
411   *pos %= buffer_len * 8;
412
413   buffer[*pos / 8] &= 0xFF >> (*pos % 8);
414   buffer[*pos / 8] |= data << (8 - (*pos % 8));
415}
416
417
418
419UINT8 sony_fetchtrack(const UINT8 *buffer, size_t buffer_len, size_t *pos)
420{
421   UINT8 data;
422
423   data = buffer[*pos / 8] << (*pos % 8);
424   *pos += 8;
425   *pos %= (buffer_len * 8);
426   data |= buffer[*pos / 8] >> (8 - (*pos % 8));
427
428   while ((data & 0x80) == 0)
429   {
430      /* this code looks weird because it isn't simply rotating the new bit
431         * in, but for some reason it won't work if I rotate the bit in; I
432         * have to match the algorithm used by the old code */
433      data <<= 1;
434      data |= (buffer[*pos / 8] >> (8 - ((*pos % 8) + 1)));
435      (*pos)++;
436      *pos %= (buffer_len * 8);
437   }
438   return data;
439}
440
441
442
443static UINT32 apple35_get_offset(floppy_image *floppy, int head, int track, int sector, UINT32 *tag_offset)
444{
445   int i;
446   UINT32 sector_index = 0;
447   struct apple35_tag *tag;
448
449   tag = get_apple35_tag(floppy);
450
451   if (track >= ARRAY_LENGTH(apple35_tracklen_800kb))
452      return ~0;
453   if (head >= tag->sides)
454      return ~0;
455   if (sector >= apple35_sectors_per_track(floppy, track))
456      return ~0;
457
458   for (i = 0; i < track; i++)
459      sector_index += apple35_sectors_per_track(floppy, i);
460   sector_index *= tag->sides;
461   if (head)
462      sector_index += apple35_sectors_per_track(floppy, i);
463   sector_index += sector;
464
465   if (tag_offset)
466   {
467      *tag_offset = sector_index * 12 + tag->tag_offset;
468      if (*tag_offset >= tag->tag_size)
469         *tag_offset = ~0;
470   }
471   return sector_index * 0x200 + tag->data_offset;
472}
473
474
475
476static floperr_t apple35_read_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen)
477{
478   UINT32 data_offset;
479//  printf("Read sector: T %d S %d H %d\n", track, sector, head);
480   data_offset = apple35_get_offset(floppy, head, track, sector, NULL);
481   if (data_offset == ~0)
482      return FLOPPY_ERROR_SEEKERROR;
483   floppy_image_read(floppy, buffer, data_offset, buflen);
484   return FLOPPY_ERROR_SUCCESS;
485}
486
487
488
489static floperr_t apple35_write_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam)
490{
491   UINT32 data_offset;
492
493   data_offset = apple35_get_offset(floppy, head, track, sector, NULL);
494   if (data_offset == ~0)
495      return FLOPPY_ERROR_SEEKERROR;
496   floppy_image_write(floppy, buffer, data_offset, buflen);
497   return FLOPPY_ERROR_SUCCESS;
498}
499
500
501
502static floperr_t apple35_read_sector_td(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen)
503{
504   floperr_t err;
505   UINT32 tag_offset = 0;
506
507   assert(buflen == 524);
508
509//  printf("Read sector TD: T %d S %d H %d\n", track, sector, head);
510
511   /* first read the sector */
512   err = apple35_read_sector(floppy, head, track, sector, ((UINT8 *) buffer) + 12, 512);
513   if (err)
514      return err;
515
516   /* read the tag data, if possible */
517   memset(buffer, '\0', 12);
518   apple35_get_offset(floppy, head, track, sector, &tag_offset);
519   if (tag_offset != ~0)
520      floppy_image_read(floppy, buffer, tag_offset, 12);
521
522   return FLOPPY_ERROR_SUCCESS;
523}
524
525
526
527static floperr_t apple35_write_sector_td(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam)
528{
529   floperr_t err;
530   UINT32 tag_offset = 0;
531
532   assert(buflen == 524);
533
534   /* first write the sector */
535   err = apple35_write_sector(floppy, head, track, sector, ((const UINT8 *) buffer) + 12, 512, 0);
536   if (err)
537      return err;
538
539   /* write the tag data, if possible */
540   apple35_get_offset(floppy, head, track, sector, &tag_offset);
541   if (tag_offset != ~0)
542      floppy_image_write(floppy, buffer, tag_offset, 12);
543
544   return FLOPPY_ERROR_SUCCESS;
545}
546
547
548
549static floperr_t apple35_get_sector_length(floppy_image *floppy, int head, int track, int sector, UINT32 *sector_length)
550{
551   *sector_length = 512;
552   return FLOPPY_ERROR_SUCCESS;
553}
554
555
556
557static int apple35_get_heads_per_disk(floppy_image *floppy)
558{
559   return get_apple35_tag(floppy)->sides;
560}
561
562
563
564static int apple35_get_tracks_per_disk(floppy_image *floppy)
565{
566   return 80;
567}
568
569
570
571static UINT32 apple35_get_track_size(floppy_image *floppy, int head, int track)
572{
573   if ((track < 0) || (track >= 80))
574      return 0;
575   return apple35_sectors_per_track(floppy, track) * 800;
576}
577
578
579
580static UINT8 calculate_side(int head, int track)
581{
582   UINT8 side;
583   side = head ? 0x20 : 0x00;
584   if (track & 0x40)
585      side |= 0x01;
586   return side;
587}
588
589
590
591static floperr_t apple35_read_track(floppy_image *floppy, int head, int track, UINT64 offset, void *buffer, size_t buflen)
592{
593   floperr_t err;
594   size_t pos = 0;
595   int sector_count, sector, i;
596   UINT8 sum, side;
597   struct apple35_tag *tag;
598   UINT8 sector_data[524];
599   UINT8 nibble_data[699];
600   UINT8 checksum[4];
601
602   tag = get_apple35_tag(floppy);
603
604   if (track >= ARRAY_LENGTH(apple35_tracklen_800kb))
605      return FLOPPY_ERROR_SEEKERROR;
606   if (offset != 0)
607      return FLOPPY_ERROR_UNSUPPORTED;
608
609   memset(buffer, 0xFF, buflen);
610   sector_count = apple35_sectors_per_track(floppy, track);
611   side = calculate_side(head, track);
612
613   for (sector = 0; sector < sector_count; sector++)
614   {
615      /* read the sector */
616      err = apple35_read_sector_td(floppy, head, track, sector, sector_data, ARRAY_LENGTH(sector_data));
617      if (err)
618         return err;
619      sony_nibblize35(sector_data, nibble_data, checksum);
620
621      for (i = 0; i < ARRAY_LENGTH(blk1); i++)
622         sony_filltrack((UINT8*)buffer, buflen, &pos, blk1[i]);
623
624      sum = (track ^ sector ^ side ^ tag->format_byte) & 0x3F;
625
626      sony_filltrack((UINT8*)buffer, buflen, &pos, diskbytes[track & 0x3f]);
627      sony_filltrack((UINT8*)buffer, buflen, &pos, diskbytes[sector]);
628      sony_filltrack((UINT8*)buffer, buflen, &pos, diskbytes[side]);
629      sony_filltrack((UINT8*)buffer, buflen, &pos, diskbytes[tag->format_byte]);
630      sony_filltrack((UINT8*)buffer, buflen, &pos, diskbytes[sum]);
631
632      for (i = 0; i < ARRAY_LENGTH(blk2); i++)
633         sony_filltrack((UINT8*)buffer, buflen, &pos, blk2[i]);
634
635      sony_filltrack((UINT8*)buffer, buflen, &pos, diskbytes[sector]);
636
637      for (i = 0; i < ARRAY_LENGTH(nibble_data); i++)
638         sony_filltrack((UINT8*)buffer, buflen, &pos, diskbytes[nibble_data[i]]);
639
640      for (i = 3; i >= 0; i--)
641         sony_filltrack((UINT8*)buffer, buflen, &pos, diskbytes[checksum[i]]);
642
643      for (i = 0; i < ARRAY_LENGTH(blk3); i++)
644         sony_filltrack((UINT8*)buffer, buflen, &pos, blk3[i]);
645   }
646   return FLOPPY_ERROR_SUCCESS;
647}
648
649
650
651static floperr_t apple35_write_track(floppy_image *floppy, int head, int track, UINT64 offset, const void *buffer, size_t buflen)
652{
653   floperr_t err;
654   size_t pos = 0;
655   int sector_count, sector, i, j;
656   struct apple35_tag *tag;
657   UINT8 sum, format_byte, val, side;
658   UINT32 found_sectors = 0;
659   UINT8 sector_data[524];
660   UINT8 nibble_data[699];
661   UINT8 checksum[4];
662
663   tag = get_apple35_tag(floppy);
664
665   if (track >= ARRAY_LENGTH(apple35_tracklen_800kb))
666      return FLOPPY_ERROR_SEEKERROR;
667   if (offset != 0)
668      return FLOPPY_ERROR_UNSUPPORTED;
669
670   sector_count = apple35_sectors_per_track(floppy, track);
671   side = calculate_side(head, track);
672
673   /* do 2 rotations, in case the bit slip stuff prevent us to read the first sector */
674   for (j = 0; j < (buflen * 2); j++)
675   {
676      if (sony_fetchtrack((UINT8*)buffer, buflen, &pos) != 0xD5)
677         continue;
678      j++;
679
680      if (sony_fetchtrack((UINT8*)buffer, buflen, &pos) != 0xAA)
681         continue;
682      j++;
683
684      if (sony_fetchtrack((UINT8*)buffer, buflen, &pos) != 0x96)
685         continue;
686      j++;
687
688      if (rev_diskbytes[sony_fetchtrack((UINT8*)buffer, buflen, &pos)] != (track & 0x3F))
689         continue;
690      j++;
691
692      sector = rev_diskbytes[sony_fetchtrack((UINT8*)buffer, buflen, &pos)];
693      if ((sector < 0) || (sector >= sector_count))
694         continue;
695      j++;
696
697      if (rev_diskbytes[sony_fetchtrack((UINT8*)buffer, buflen, &pos)] != side)
698         continue;
699      j++;
700
701      format_byte = rev_diskbytes[sony_fetchtrack((UINT8*)buffer, buflen, &pos)];
702      if (format_byte != tag->format_byte)
703      {
704         /* this is an error, but not THAT critical, I guess */
705      }
706      j++;
707
708      sum = track ^ sector ^ side ^ format_byte;
709      if (rev_diskbytes[sony_fetchtrack((UINT8*)buffer, buflen, &pos)] != sum)
710         continue;
711      j++;
712
713      if (sony_fetchtrack((UINT8*)buffer, buflen, &pos) != 0xDE)
714         continue;
715      j++;
716
717      if (sony_fetchtrack((UINT8*)buffer, buflen, &pos) != 0xAA)
718         continue;
719      j++;
720
721      while((val = sony_fetchtrack((UINT8*)buffer, buflen, &pos)) == 0xFF)
722         j++;
723      if (val != 0xD5)
724         continue;   /* lost bit slip mark! */
725      j++;
726
727      if (sony_fetchtrack((UINT8*)buffer, buflen, &pos) != 0xAA)
728         continue;
729      j++;
730
731      if (sony_fetchtrack((UINT8*)buffer, buflen, &pos) != 0xAD)
732         continue;
733      j++;
734
735      /* should this be regarded as a critical error ??? */
736      if (rev_diskbytes[sony_fetchtrack((UINT8*)buffer, buflen, &pos)] != sector)
737         continue;
738      j++;
739
740      for (i = 0; i < ARRAY_LENGTH(nibble_data); i++)
741      {
742         nibble_data[i] = rev_diskbytes[sony_fetchtrack((UINT8*)buffer, buflen, &pos)];
743         j++;
744      }
745
746      for (i = 3; i >= 0; i--)
747      {
748         /* should be checking checksum */
749         sony_fetchtrack((UINT8*)buffer, buflen, &pos);
750      }
751
752      sony_fetchtrack((UINT8*)buffer, buflen, &pos);   /* should get 0xDE */
753      sony_fetchtrack((UINT8*)buffer, buflen, &pos);   /* should get 0xAA */
754      sony_fetchtrack((UINT8*)buffer, buflen, &pos);   /* should get 0xFF */
755
756      /* did we already write this sector? */
757      if ((found_sectors & (1 << sector)) == 0)
758      {
759         sony_denibblize35(sector_data, nibble_data, checksum);
760
761         /* write the sector */
762         err = apple35_write_sector_td(floppy, head, track, sector, sector_data, ARRAY_LENGTH(sector_data), 0);
763         if (err)
764            return err;
765
766         found_sectors |= 1 << sector;
767      }
768   }
769   return FLOPPY_ERROR_SUCCESS;
770}
771
772
773
774static floperr_t apple35_construct(floppy_image *floppy, UINT32 data_offset, UINT32 data_size,
775   UINT32 tag_offset, UINT32 tag_size, INT16 format_byte, UINT8 sides, int is_1440k)
776{
777   struct apple35_tag *tag;
778   struct FloppyCallbacks *format;
779
780   /* figure out format byte if not specified */
781   if (format_byte < 0)
782   {
783      switch(sides)
784      {
785         case 1:
786            format_byte = 0x02;
787            break;
788         case 2:
789            format_byte = 0x22;
790            break;
791         default:
792            return FLOPPY_ERROR_INVALIDIMAGE;
793      }
794   }
795
796   /* create tag */
797   tag = (struct apple35_tag *) floppy_create_tag(floppy, sizeof(struct apple35_tag));
798   if (!tag)
799      return FLOPPY_ERROR_OUTOFMEMORY;
800   tag->data_offset = data_offset;
801   tag->data_size = data_size;
802   tag->tag_offset = tag_offset;
803   tag->tag_size = tag_size;
804   tag->format_byte = (UINT8) format_byte;
805   tag->sides = sides;
806   tag->is_1440k = is_1440k ? 1 : 0;
807
808   /* set up format callbacks */
809   format = floppy_callbacks(floppy);
810   format->read_sector = apple35_read_sector;
811   format->write_sector = apple35_write_sector;
812   format->read_track = apple35_read_track;
813   format->write_track = apple35_write_track;
814   format->get_sector_length = apple35_get_sector_length;
815   format->get_heads_per_disk = apple35_get_heads_per_disk;
816   format->get_tracks_per_disk = apple35_get_tracks_per_disk;
817   format->get_track_size = apple35_get_track_size;
818   return FLOPPY_ERROR_SUCCESS;
819}
820
821
822
823/* ----------------------------------------------------------------------- */
824
825static FLOPPY_IDENTIFY(apple35_raw_identify)
826{
827   UINT64 size;
828   size = floppy_image_size(floppy);
829   *vote = ((size == 80*1*10*512) || (size == 80*2*10*512) || (size == (80*2*18*512)+84)
830      || (size == 80*2*18*512)) ? 100 : 0;
831   return FLOPPY_ERROR_SUCCESS;
832}
833
834
835
836
837static FLOPPY_CONSTRUCT(apple35_raw_construct)
838{
839   UINT64 size;
840   UINT8 sides;
841   int is_1440k;
842
843   if (params)
844   {
845      /* create */
846      sides = option_resolution_lookup_int(params, PARAM_HEADS);
847      size = 80*sides*10*512;
848      is_1440k = FALSE;
849   }
850   else
851   {
852      /* load */
853      size = floppy_image_size(floppy);
854      if (size == 80*1*10*512)
855      {
856         sides = 1;
857         is_1440k = FALSE;
858      }
859      else if ((size == 80*2*10*512) || (size == 80*2*18*512) || (size == (80*2*18*512)+84))
860      {
861         sides = 2;
862         is_1440k = (size == 80*2*18*512) || (size == (80*2*18*512)+84);
863      }
864      else
865         return FLOPPY_ERROR_INVALIDIMAGE;
866   }
867
868   return apple35_construct(floppy, 0, (UINT32) size, 0, 0, -1, sides, is_1440k);
869}
870
871
872
873/* ----------------------------------------------------------------------- */
874
875struct header_diskcopy
876{
877   UINT8 disk_name[64];   /* name of the disk */
878   UINT32 data_size;      /* total size of data for all sectors (512*number_of_sectors) */
879   UINT32 tag_size;      /* total size of tag data for all sectors (12*number_of_sectors for GCR 3.5" floppies, 20*number_of_sectors for HD20, 0 otherwise) */
880   UINT32 data_checksum;   /* CRC32 checksum of all sector data */
881   UINT32 tag_checksum;   /* CRC32 checksum of all tag data */
882   UINT8 disk_format;      /* 0 = 400K, 1 = 800K, 2 = 720K, 3 = 1440K  (other values reserved) */
883   UINT8 format_byte;      /* should be $00 Apple II, $01 Lisa, $02 Mac MFS ??? */
884                     /* $12 = 400K, $22 = >400K Macintosh (DiskCopy uses this value for
885                               all Apple II disks not 800K in size, and even for some of those),
886                               $24 = 800K Apple II disk */
887   UINT16 magic;         /* always $0100 (otherwise, the file may be in a different format. */
888};
889
890
891
892static floperr_t apple35_diskcopy_headerdecode(floppy_image *floppy, UINT32 *data_offset,
893   UINT32 *data_size, UINT32 *tag_offset, UINT32 *tag_size, UINT8 *format_byte, UINT8 *sides)
894{
895   UINT64 size;
896   struct header_diskcopy header;
897
898   if (data_offset)
899      *data_offset = 0;
900   if (data_size)
901      *data_size = 0;
902   if (tag_offset)
903      *tag_offset = 0;
904   if (tag_size)
905      *tag_size = 0;
906   if (format_byte)
907      *format_byte = 0;
908   if (sides)
909      *sides = 0;
910
911   size = floppy_image_size(floppy);
912   if (size < sizeof(struct header_diskcopy))
913      return FLOPPY_ERROR_INVALIDIMAGE;
914
915   floppy_image_read(floppy, &header, 0, sizeof(header));
916
917   header.data_size = BIG_ENDIANIZE_INT32(header.data_size);
918   header.tag_size = BIG_ENDIANIZE_INT32(header.tag_size);
919   header.data_checksum = BIG_ENDIANIZE_INT32(header.data_checksum);
920   header.tag_checksum = BIG_ENDIANIZE_INT32(header.tag_checksum);
921   header.magic = BIG_ENDIANIZE_INT16(header.magic);
922
923   if (header.disk_name[0] >= sizeof(header.disk_name))
924      return FLOPPY_ERROR_INVALIDIMAGE;
925   if (header.magic != 0x0100)
926      return FLOPPY_ERROR_INVALIDIMAGE;
927   if (size != (header.data_size + header.tag_size + sizeof(header)))
928      return FLOPPY_ERROR_INVALIDIMAGE;
929
930   if (header.data_size == 80*1*10*512)
931   {
932      if (sides)
933         *sides = 1;
934   }
935   else if (header.data_size == 80*2*10*512)
936   {
937      if (sides)
938         *sides = 2;
939   }
940   else
941      return FLOPPY_ERROR_INVALIDIMAGE;
942
943   if (data_offset)
944      *data_offset = sizeof(header);
945   if (data_size)
946      *data_size = header.data_size;
947   if (tag_offset)
948      *tag_offset = sizeof(header) + header.data_size;
949   if (tag_size)
950      *tag_size = header.tag_size;
951   if (format_byte)
952      *format_byte = header.format_byte;
953   return FLOPPY_ERROR_SUCCESS;
954}
955
956
957
958static FLOPPY_IDENTIFY(apple35_diskcopy_identify)
959{
960   *vote = apple35_diskcopy_headerdecode(floppy, NULL, NULL, NULL, NULL, NULL, NULL) ? 0 : 100;
961   return FLOPPY_ERROR_SUCCESS;
962}
963
964
965
966
967static FLOPPY_CONSTRUCT(apple35_diskcopy_construct)
968{
969   floperr_t err;
970   UINT8 format_byte, sides;
971   UINT32 data_offset, data_size;
972   UINT32 tag_offset, tag_size;
973   INT16 format_byte_param = -1;
974   struct header_diskcopy header;
975
976   if (params)
977   {
978      /* create */
979      sides = option_resolution_lookup_int(params, PARAM_HEADS);
980
981      data_size = 80*sides*10*512;
982      tag_size = 80*sides*10*12;
983
984      memset(&header, 0, sizeof(header));
985      header.data_size = BIG_ENDIANIZE_INT32(data_size);
986      header.tag_size = BIG_ENDIANIZE_INT32(tag_size);
987      header.disk_format = (sides > 1) ? 1 : 0;
988      header.magic = BIG_ENDIANIZE_INT16(0x100);
989
990      floppy_image_write(floppy, &header, 0, sizeof(header));
991      floppy_image_write_filler(floppy, 0, sizeof(header), data_size + tag_size);
992   }
993
994   /* load */
995   err = apple35_diskcopy_headerdecode(floppy, &data_offset, &data_size,
996      &tag_offset, &tag_size, &format_byte, &sides);
997   if (err)
998      return err;
999
1000   format_byte_param = format_byte;
1001
1002   return apple35_construct(floppy, data_offset, data_size,
1003      tag_offset, tag_size, format_byte_param, sides, FALSE);
1004}
1005
1006
1007
1008/* ----------------------------------------------------------------------- */
1009
1010struct header_2img
1011{
1012   char magic[4];         /* '2IMG' */
1013   char creator[4];      /* signature; 'MESS' for MESS */
1014   UINT16 header_length;
1015   UINT16 version;
1016   UINT32 image_format;
1017   UINT32 flags;
1018   UINT32 block_count;
1019   UINT32 data_offset;
1020   UINT32 data_length;
1021   UINT32 comment_offset;
1022   UINT32 comment_length;
1023   UINT32 creator_offset;
1024   UINT32 creator_length;
1025   UINT32 padding[4];
1026};
1027
1028#define IMAGE_FORMAT_DO      0
1029#define IMAGE_FORMAT_PO      1
1030#define IMAGE_FORMAT_NIB   2
1031#define IMAGE_FLAGS_LOCKED   0x80000000
1032
1033
1034
1035static floperr_t apple35_2img_decode(floppy_image *floppy, UINT32 *image_format,
1036   UINT32 *data_offset, UINT32 *data_length)
1037{
1038   struct header_2img header;
1039   UINT64 size;
1040
1041   if (image_format)
1042      *image_format = 0;
1043   if (data_offset)
1044      *data_offset = 0;
1045   if (data_length)
1046      *data_length = 0;
1047
1048   size = floppy_image_size(floppy);
1049   if (size < sizeof(header))
1050   {
1051      return FLOPPY_ERROR_INVALIDIMAGE;
1052   }
1053
1054   floppy_image_read(floppy, &header, 0, sizeof(header));
1055
1056   if (memcmp(header.magic, "2IMG", 4))
1057   {
1058      return FLOPPY_ERROR_INVALIDIMAGE;
1059   }
1060
1061   header.header_length   = LITTLE_ENDIANIZE_INT16(header.header_length);
1062   header.version         = LITTLE_ENDIANIZE_INT16(header.version);
1063   header.image_format      = LITTLE_ENDIANIZE_INT32(header.image_format);
1064   header.flags         = LITTLE_ENDIANIZE_INT32(header.flags);
1065   header.block_count      = LITTLE_ENDIANIZE_INT32(header.block_count);
1066   header.data_offset      = LITTLE_ENDIANIZE_INT32(header.data_offset);
1067   header.data_length      = LITTLE_ENDIANIZE_INT32(header.data_length);
1068   header.comment_offset   = LITTLE_ENDIANIZE_INT32(header.comment_offset);
1069   header.comment_length   = LITTLE_ENDIANIZE_INT32(header.comment_length);
1070   header.creator_offset   = LITTLE_ENDIANIZE_INT32(header.creator_offset);
1071   header.creator_length   = LITTLE_ENDIANIZE_INT32(header.creator_length);
1072
1073   // at least some images "in the wild" (e.g. TOSEC Minor Set 1) have big-endian data sizes
1074   // even though that's against the .2mg spec
1075   if (header.data_length == 0x800c00)
1076   {
1077      logerror("ap_dsk35: corrected bad-endian data length\n");
1078      header.data_length = 0x0c8000;
1079   }
1080
1081   if ((((UINT64) header.data_offset) + header.data_length) > size)
1082      return FLOPPY_ERROR_INVALIDIMAGE;
1083   if ((((UINT64) header.comment_offset) + header.comment_length) > size)
1084      return FLOPPY_ERROR_INVALIDIMAGE;
1085   if ((((UINT64) header.creator_offset) + header.creator_length) > size)
1086      return FLOPPY_ERROR_INVALIDIMAGE;
1087   if ((header.image_format != IMAGE_FORMAT_DO) &&
1088         (header.image_format != IMAGE_FORMAT_PO) &&
1089         (header.image_format != IMAGE_FORMAT_NIB))
1090      return FLOPPY_ERROR_INVALIDIMAGE;
1091
1092   if (image_format)
1093      *image_format = header.image_format;
1094   if (data_offset)
1095      *data_offset = header.data_offset;
1096   if (data_length)
1097      *data_length = header.data_length;
1098   return FLOPPY_ERROR_SUCCESS;
1099}
1100
1101
1102
1103static FLOPPY_IDENTIFY(apple35_2img_identify)
1104{
1105   *vote = apple35_2img_decode(floppy, NULL, NULL, NULL) ? 0 : 100;
1106   return FLOPPY_ERROR_SUCCESS;
1107}
1108
1109
1110
1111static FLOPPY_CONSTRUCT(apple35_2img_construct)
1112{
1113   floperr_t err;
1114   UINT32 image_format;
1115   UINT32 data_offset;
1116   UINT32 data_size;
1117   UINT8 sides = 2;
1118   struct header_2img header;
1119
1120   if (params)
1121   {
1122      /* create */
1123      sides = option_resolution_lookup_int(params, PARAM_HEADS);
1124
1125      data_offset = sizeof(header);
1126      data_size = 80*sides*10*512;
1127
1128      memset(&header, 0, sizeof(header));
1129      header.header_length   = LITTLE_ENDIANIZE_INT16(sizeof(header));
1130      header.block_count      = LITTLE_ENDIANIZE_INT32(80*sides*10);
1131      header.data_offset      = LITTLE_ENDIANIZE_INT32(data_offset);
1132      header.data_length      = LITTLE_ENDIANIZE_INT32(data_size);
1133
1134      floppy_image_write(floppy, &header, 0, sizeof(header));
1135      floppy_image_write_filler(floppy, 0, sizeof(header), data_size);
1136   }
1137   else
1138   {
1139      /* load */
1140      err = apple35_2img_decode(floppy, &image_format, &data_offset, &data_size);
1141      if (err)
1142         return err;
1143
1144      if (data_size == 80*1*10*512)
1145         sides = 1;   /* single sided */
1146      else if (data_size == 80*2*10*512)
1147         sides = 2;   /* double sided */
1148      else
1149         sides = 2;   /* unknown... what to do... */
1150   }
1151
1152   return apple35_construct(floppy, data_offset, data_size,
1153      0, 0, -1, sides, FALSE);
1154}
1155
1156
1157
1158FLOPPY_OPTIONS_START( apple35_mac )
1159   FLOPPY_OPTION( apple35_raw, "dsk,img,image",   "Apple raw 3.5\" disk image",   apple35_raw_identify,      apple35_raw_construct, NULL,
1160      HEADS([1]-2)
1161      TRACKS([80])
1162      SECTOR_LENGTH([512])
1163      FIRST_SECTOR_ID([0]))
1164   FLOPPY_OPTION( apple35_dc, "dc,dsk,img,image",   "Apple DiskCopy disk image",   apple35_diskcopy_identify,   apple35_diskcopy_construct, NULL,
1165      HEADS([1]-2)
1166      TRACKS([80])
1167      SECTOR_LENGTH([512])
1168      FIRST_SECTOR_ID([0]))
1169FLOPPY_OPTIONS_END
1170
1171FLOPPY_OPTIONS_START( apple35_iigs )
1172   FLOPPY_OPTION( apple35_raw, "dsk,img,image",   "Apple raw 3.5\" disk image",   apple35_raw_identify,      apple35_raw_construct, NULL,
1173      HEADS([1]-2)
1174      TRACKS([80])
1175      SECTOR_LENGTH([512])
1176      FIRST_SECTOR_ID([0]))
1177   FLOPPY_OPTION( apple35_dc, "dc,dsk,img,image",   "Apple DiskCopy disk image",   apple35_diskcopy_identify,   apple35_diskcopy_construct, NULL,
1178      HEADS([1]-2)
1179      TRACKS([80])
1180      SECTOR_LENGTH([512])
1181      FIRST_SECTOR_ID([0]))
1182   FLOPPY_OPTION( apple35_2img, "2img,2mg",      "Apple ][gs 2IMG disk image",   apple35_2img_identify,      apple35_2img_construct, NULL,
1183      HEADS([1]-2)
1184      TRACKS([80])
1185      SECTOR_LENGTH([512])
1186      FIRST_SECTOR_ID([0]))
1187FLOPPY_OPTIONS_END
src/mess/formats/apridisk.h
r11457r11458
1/***************************************************************************
2
3    APRIDISK disk image format
4
5***************************************************************************/
6
7#ifndef __APRIDISK_H__
8#define __APRIDISK_H__
9
10#include "imagedev/flopimg.h"
11
12FLOPPY_IDENTIFY( apridisk_identify );
13FLOPPY_CONSTRUCT( apridisk_construct );
14
15#endif /* __APRIDISK_H__ */
src/mess/formats/msx_dsk.h
r11457r11458
1/*********************************************************************
2
3    msx_dsk.h
4
5    MSX disk images
6
7*********************************************************************/
8
9#ifndef MSX_DSK_H
10#define MSX_DSK_H
11
12#include "imagedev/flopimg.h"
13
14/**************************************************************************/
15
16FLOPPY_OPTIONS_EXTERN(msx);
17
18#endif /* MSX_DSK_H */
src/mess/formats/ap_dsk35.h
r11457r11458
1/*********************************************************************
2
3    ap_dsk35.h
4
5    Apple 3.5" disk images
6
7*********************************************************************/
8
9#ifndef AP_DSK35_H
10#define AP_DSK35_H
11
12#include "imagedev/flopimg.h"
13
14void sony_filltrack(UINT8 *buffer, size_t buffer_len, size_t *pos, UINT8 data);
15UINT8 sony_fetchtrack(const UINT8 *buffer, size_t buffer_len, size_t *pos);
16
17int apple35_sectors_per_track(floppy_image *image, int track);
18
19/**************************************************************************/
20
21FLOPPY_OPTIONS_EXTERN(apple35_mac);
22FLOPPY_OPTIONS_EXTERN(apple35_iigs);
23
24
25#endif /* AP_DSK35_H */
src/mess/formats/comx35_dsk.c
r11457r11458
1/*********************************************************************
2
3    formats/comx35_dsk.c
4
5    COMX35 disk images
6
7*********************************************************************/
8
9#include "formats/comx35_dsk.h"
10#include "formats/basicdsk.h"
11
12/***************************************************************************
13    IMPLEMENTATION
14***************************************************************************/
15
16/*-------------------------------------------------
17    FLOPPY_IDENTIFY( comx35_dsk_identify )
18-------------------------------------------------*/
19
20static FLOPPY_IDENTIFY( comx35_dsk_identify )
21{
22   *vote = ((floppy_image_size(floppy) == (35*1*16*128)) || (floppy_image_size(floppy) == (35*2*16*128))) ? 100 : 0;
23
24   return FLOPPY_ERROR_SUCCESS;
25}
26
27/*-------------------------------------------------
28    FLOPPY_CONSTRUCT( comx35_dsk_construct )
29-------------------------------------------------*/
30
31static FLOPPY_CONSTRUCT( comx35_dsk_construct )
32{
33   UINT8 header[1];
34   int heads = 1;
35   int cylinders = 35;
36
37   switch (floppy_image_size(floppy))
38   {
39   case 35*1*16*128:
40      heads = 1;
41      cylinders = 35;
42      break;
43
44   case 35*2*16*128:
45      floppy_image_read(floppy, header, 0x12, 1);
46
47      if (header[0] == 0x01)
48      {
49         heads = 1;
50         cylinders = 70;
51      }
52      else
53      {
54         heads = 2;
55         cylinders = 35;
56      }
57      break;
58   }
59
60   struct basicdsk_geometry geometry;
61   memset(&geometry, 0, sizeof(geometry));
62
63   geometry.heads = heads;
64   geometry.first_sector_id = 0;
65   geometry.sector_length = 128;
66   geometry.tracks = cylinders;
67   geometry.sectors = 16;
68
69   return basicdsk_construct(floppy, &geometry);
70}
71
72/*-------------------------------------------------
73    FLOPPY_OPTIONS( comx35 )
74-------------------------------------------------*/
75
76FLOPPY_OPTIONS_START( comx35 )
77   FLOPPY_OPTION( comx35, "img", "COMX35 floppy disk image", comx35_dsk_identify, comx35_dsk_construct, NULL, NULL )
78FLOPPY_OPTIONS_END
src/mess/formats/comx35_dsk.h
r11457r11458
1/*********************************************************************
2
3    formats/comx35_dsk.h
4
5    COMX35 disk images
6
7*********************************************************************/
8
9#ifndef COMX35_DSK_H
10#define COMX35_DSK_H
11
12#include "imagedev/flopimg.h"
13
14/**************************************************************************/
15
16FLOPPY_OPTIONS_EXTERN(comx35);
17
18#endif /* COMX35_DSK_H */
src/mess/formats/svi_cas.c
r11457r11458
1#include "svi_cas.h"
2
3#define CAS_PERIOD_0      (37)
4#define CAS_PERIOD_1      (18)
5#define CAS_HEADER_PERIODS (1600)
6#define CAS_EMPTY_SAMPLES (24220)
7#define CAS_INIT_SAMPLES   (200)
8
9static const UINT8 CasHeader[17] =
10{
11   0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
12   0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x7f
13};
14
15#define SMPLO   -32768
16#define SMPHI   32767
17
18static int cas_size;
19
20/*******************************************************************
21   Generate samples for the tape image
22********************************************************************/
23static int svi_cas_fill_wave(INT16 *buffer, int sample_count, UINT8 *bytes)
24{
25   int cas_pos, samples_pos, n, i;
26
27   cas_pos = 17;
28   samples_pos = 0;
29
30   /* write CAS_INIT_SAMPLES of silence */
31   n = CAS_INIT_SAMPLES; while (n--) buffer[samples_pos++] = 0;
32
33   while (samples_pos < sample_count && cas_pos < cas_size)
34   {
35      /* write CAS_HEADER_PERIODS of header */
36      for (i=0;i<CAS_HEADER_PERIODS;i++)
37      {
38         /* write a "0" */
39         n = !(i % 4) ? 21 : 18;
40         while (n--) buffer[samples_pos++] = SMPHI;
41         n = 19; while (n--) buffer[samples_pos++] = SMPLO;
42         /* write a "1" */
43         n = 9; while (n--) buffer[samples_pos++] = SMPHI;
44         n = 9; while (n--) buffer[samples_pos++] = SMPLO;
45      }
46
47      /* write "0x7f" */
48      /* write a "0" */
49      n = 21; while (n--) buffer[samples_pos++] = SMPHI;
50      n = 19; while (n--) buffer[samples_pos++] = SMPLO;
51
52      for (i=0;i<7;i++)
53      {
54         /* write a "1" */
55         n = 9; while (n--) buffer[samples_pos++] = SMPHI;
56         n = 9; while (n--) buffer[samples_pos++] = SMPLO;
57      }
58
59      while (samples_pos < sample_count && cas_pos < cas_size)
60      {
61         n = 21; while (n--) buffer[samples_pos++] = SMPHI;
62         n = 19; while (n--) buffer[samples_pos++] = SMPLO;
63
64         for (i=0;i<8;i++)
65         {
66            int bit = (bytes[cas_pos] & (0x80 >> i) );
67
68            /* write this one bit */
69            if (bit)
70            {
71               /* write a "1" */
72               n = 9; while (n--) buffer[samples_pos++] = SMPHI;
73               n = 9; while (n--) buffer[samples_pos++] = SMPLO;
74            }
75            else
76            {
77               /* write a "0" */
78               n = 18; while (n--) buffer[samples_pos++] = SMPHI;
79               n = 19; while (n--) buffer[samples_pos++] = SMPLO;
80            }
81         }
82
83         cas_pos++;
84
85         /* check if we've hit a new header (or end of block) */
86         if ( (cas_pos + 17) < cas_size)
87         {
88            if (!memcmp (bytes + cas_pos, CasHeader, 17) )
89            {
90               cas_pos += 17;
91
92               /* end of block marker */
93               n = CAS_EMPTY_SAMPLES; while (n--) buffer[samples_pos++] = SMPHI;
94
95               break; /* falls back to loop above; plays header again */
96            }
97         }
98      }
99   }
100
101   /* end of block marker */
102   n = CAS_EMPTY_SAMPLES; while (n--) buffer[samples_pos++] = SMPHI;
103
104   return samples_pos;
105}
106
107
108/*******************************************************************
109   Calculate the number of samples needed for this tape image
110********************************************************************/
111static int svi_cas_to_wav_size(const UINT8 *casdata, int caslen)
112{
113   int cas_pos, samples_pos, size, i;
114
115   if (caslen < 17) return -1;
116   if (memcmp (casdata, CasHeader, sizeof (CasHeader) ) ) return -1;
117
118   cas_size = caslen;
119
120   cas_pos = 17;
121
122   samples_pos = CAS_INIT_SAMPLES;
123
124   while (cas_pos < caslen)
125   {
126      size = CAS_HEADER_PERIODS * ( CAS_PERIOD_0 + CAS_PERIOD_1 ) +
127            ( CAS_HEADER_PERIODS / 4 ) * 3;
128
129      samples_pos += size;
130
131      samples_pos += 21 + 19 + 7 * CAS_PERIOD_1;
132
133      while (cas_pos < caslen)
134      {
135         samples_pos += 21;
136         samples_pos += 19;
137
138         for (i=0;i<8;i++)
139         {
140            int bit = (casdata[cas_pos] & (0x80 >> i) );
141
142            samples_pos += bit ? CAS_PERIOD_1 : CAS_PERIOD_0;
143         }
144
145         cas_pos++;
146
147         /* check if we've hit a new header (or end of block) */
148         if ( (cas_pos + 17) < caslen)
149         {
150            if (!memcmp (casdata + cas_pos, CasHeader, 17) )
151            {
152               cas_pos += 17;
153
154               /* end of block marker */
155               samples_pos += CAS_EMPTY_SAMPLES;
156
157               break; /* falls back to loop above; plays header again */
158            }
159         }
160      }
161   }
162
163   /* end of block marker */
164   samples_pos += CAS_EMPTY_SAMPLES;
165
166   return samples_pos;
167}
168
169
170static const struct CassetteLegacyWaveFiller svi_legacy_fill_wave =
171{
172   svi_cas_fill_wave,                  /* fill_wave */
173   -1,                              /* chunk_size */
174   0,                              /* chunk_samples */
175   svi_cas_to_wav_size,               /* chunk_sample_calc */
176   44100,                           /* sample_frequency */
177   0,                              /* header_samples */
178   0                              /* trailer_samples */
179};
180
181
182
183static casserr_t svi_cas_identify(cassette_image *cassette, struct CassetteOptions *opts)
184{
185   return cassette_legacy_identify(cassette, opts, &svi_legacy_fill_wave);
186}
187
188
189
190static casserr_t svi_cas_load(cassette_image *cassette)
191{
192   return cassette_legacy_construct(cassette, &svi_legacy_fill_wave);
193}
194
195
196
197static const struct CassetteFormat svi_cas_format =
198{
199   "cas",
200   svi_cas_identify,
201   svi_cas_load,
202   NULL
203};
204
205
206
207CASSETTE_FORMATLIST_START(svi_cassette_formats)
208   CASSETTE_FORMAT(svi_cas_format)
209CASSETTE_FORMATLIST_END
src/mess/formats/trd_dsk.c
r11457r11458
1/*********************************************************************
2
3    formats/trd_dsk.c
4
5    TRD disk images
6
7*********************************************************************/
8
9#include <string.h>
10
11#include "formats/trd_dsk.h"
12#include "formats/basicdsk.h"
13#include "emu.h"
14
15
16static FLOPPY_IDENTIFY(trd_dsk_identify)
17{
18   *vote = 100;
19   return FLOPPY_ERROR_SUCCESS;
20}
21
22static FLOPPY_CONSTRUCT(trd_dsk_construct)
23{
24   struct basicdsk_geometry geometry;
25   UINT8 data[1];
26   int heads;
27   int cylinders;
28
29   floppy_image_read( floppy, data, 0x8e3 , 1 );
30
31   /* guess geometry of disk */
32   heads =  data[0] & 0x08 ? 1 : 2;
33   cylinders = data[0] & 0x01 ? 40 : 80;
34
35   memset(&geometry, 0, sizeof(geometry));
36   geometry.heads = heads;
37   geometry.first_sector_id = 1;
38   geometry.sector_length = 256;
39   geometry.tracks = cylinders;
40   geometry.sectors = 16;
41   return basicdsk_construct(floppy, &geometry);
42}
43
44
45
46/* ----------------------------------------------------------------------- */
47
48FLOPPY_OPTIONS_START( trd )
49   FLOPPY_OPTION( trd_dsk, "trd",      "TRD floppy disk image",   trd_dsk_identify, trd_dsk_construct, NULL, NULL)
50FLOPPY_OPTIONS_END
51
src/mess/formats/svi_cas.h
r11457r11458
1/*********************************************************************
2
3    svi_cas.h
4
5    Format code for SVI casette files
6
7*********************************************************************/
8
9#ifndef SVI_CAS_H
10#define SVI_CAS_H
11
12#include "imagedev/cassimg.h"
13
14CASSETTE_FORMATLIST_EXTERN(svi_cassette_formats);
15
16#endif /* SVI_CAS_H */
17
src/mess/formats/ace_tap.c
r11457r11458
1/********************************************************************
2
3Support for Jupiter Ace .tap cassette images
4
5For more information see:
6- http://www.jupiter-ace.co.uk/faq_ace_tap_format.html
7- http://www.jupiter-ace.co.uk/doc_AceTapeFormat.html
8
9********************************************************************/
10#include "ace_tap.h"
11
12
13#define SMPLO   -32768
14#define SILENCE   0
15#define SMPHI   32767
16
17
18static int cas_size;
19
20
21/*******************************************************************
22   Generate one high-low cycle of sample data
23********************************************************************/
24INLINE int ace_tap_cycle(INT16 *buffer, int sample_pos, int high, int low)
25{
26   int i = 0;
27
28   if ( buffer )
29   {
30      while( i < high)
31      {
32         buffer[ sample_pos + i ] = SMPHI;
33         i++;
34      }
35
36      while( i < high + low )
37      {
38         buffer[ sample_pos + i ] = SMPLO;
39         i++;
40      }
41   }
42   return high + low;
43}
44
45
46INLINE int ace_tap_silence(INT16 *buffer, int sample_pos, int samples)
47{
48   int i = 0;
49
50   if ( buffer )
51   {
52      while( i < samples )
53      {
54         buffer[ sample_pos + i ] = SILENCE;
55         i++;
56      }
57   }
58   return samples;
59}
60
61
62INLINE int ace_tap_byte(INT16 *buffer, int sample_pos, UINT8 data)
63{
64   int i, samples;
65
66   samples = 0;
67   for ( i = 0; i < 8; i++ )
68   {
69      if ( data & 0x80 )
70         samples += ace_tap_cycle( buffer, sample_pos + samples, 21, 22 );
71      else
72         samples += ace_tap_cycle( buffer, sample_pos + samples, 10, 11 );
73
74      data <<= 1;
75   }
76   return samples;
77}
78
79
80static int ace_handle_tap(INT16 *buffer, const UINT8 *casdata)
81{
82   int   data_pos, sample_count;
83
84   /* Make sure the file starts with a valid header */
85   if ( cas_size < 0x1C )
86      return -1;
87   if ( casdata[0] != 0x1A || casdata[1] != 0x00 )
88      return -1;
89
90   data_pos = 0;
91   sample_count = 0;
92
93   while( data_pos < cas_size )
94   {
95      UINT16   block_size;
96      int      i;
97
98      /* Handle a block of tape data */
99      block_size = casdata[data_pos] + ( casdata[data_pos + 1] << 8 );
100      data_pos += 2;
101
102      /* Make sure there are enough bytes left */
103      if ( data_pos > cas_size )
104         return -1;
105
106      /* 2 seconds silence */
107      sample_count += ace_tap_silence( buffer, sample_count, 2 * 44100 );
108
109      /* Add pilot tone samples: 4096 for header, 512 for data */
110      for( i = ( block_size == 0x001A ) ? 4096 : 512; i; i-- )
111         sample_count += ace_tap_cycle( buffer, sample_count, 27, 27 );
112
113      /* Sync samples */
114      sample_count += ace_tap_cycle( buffer, sample_count, 8, 11 );
115
116      /* Output block type identification byte */
117      sample_count += ace_tap_byte( buffer, sample_count, ( block_size != 0x001A ) ? 0xFF : 0x00 );
118
119      /* Data samples */
120      for ( ; block_size ; data_pos++, block_size-- )
121         sample_count += ace_tap_byte( buffer, sample_count, casdata[data_pos] );
122
123      /* End mark samples */
124      sample_count += ace_tap_cycle( buffer, sample_count, 12, 57 );
125
126      /* 3 seconds silence */
127      sample_count += ace_tap_silence( buffer, sample_count, 3 * 44100 );
128   }
129   return sample_count;
130}
131
132
133/*******************************************************************
134   Generate samples for the tape image
135********************************************************************/
136static int ace_tap_fill_wave(INT16 *buffer, int sample_count, UINT8 *bytes)
137{
138   return ace_handle_tap( buffer, bytes );
139}
140
141
142/*******************************************************************
143   Calculate the number of samples needed for this tape image
144********************************************************************/
145static int ace_tap_to_wav_size(const UINT8 *casdata, int caslen)
146{
147   cas_size = caslen;
148
149   return ace_handle_tap( NULL, casdata );
150}
151
152
153static const struct CassetteLegacyWaveFiller ace_legacy_fill_wave =
154{
155   ace_tap_fill_wave,               /* fill_wave */
156   -1,                              /* chunk_size */
157   0,                              /* chunk_samples */
158   ace_tap_to_wav_size,            /* chunk_sample_calc */
159   44100,                           /* sample_frequency */
160   0,                              /* header_samples */
161   0                              /* trailer_samples */
162};
163
164
165static casserr_t ace_tap_identify(cassette_image *cassette, struct CassetteOptions *opts)
166{
167   return cassette_legacy_identify(cassette, opts, &ace_legacy_fill_wave);
168}
169
170
171static casserr_t ace_tap_load(cassette_image *cassette)
172{
173   return cassette_legacy_construct(cassette, &ace_legacy_fill_wave);
174}
175
176
177static const struct CassetteFormat ace_tap_format =
178{
179   "tap",
180   ace_tap_identify,
181   ace_tap_load,
182   NULL
183};
184
185
186CASSETTE_FORMATLIST_START(ace_cassette_formats)
187   CASSETTE_FORMAT(ace_tap_format)
188CASSETTE_FORMATLIST_END
src/mess/formats/trs_cas.c
r11457r11458
1/********************************************************************
2
3Support for TRS80 .cas cassette images
4
5********************************************************************/
6
7#include "formats/trs_cas.h"
8
9#define SILENCE   0
10#define SMPLO   -32768
11#define SMPHI   32767
12
13
14static int cas_size;
15
16
17/*******************************************************************
18   Generate one high-low cycle of sample data
19********************************************************************/
20INLINE int trs80l2_cas_cycle(INT16 *buffer, int sample_pos, int silence, int high, int low)
21{
22   int i = 0;
23
24   if ( buffer )
25   {
26      while( i < silence )
27      {
28         buffer[ sample_pos + i ] = SILENCE;
29         i++;
30      }
31      while( i < silence + high)
32      {
33         buffer[ sample_pos + i ] = SMPHI;
34         i++;
35      }
36
37      while( i < silence + high + low )
38      {
39         buffer[ sample_pos + i ] = SMPLO;
40         i++;
41      }
42   }
43   return silence + high + low;
44}
45
46
47static int trs80l2_handle_cas(INT16 *buffer, const UINT8 *casdata)
48{
49   int   data_pos, sample_count;
50
51   data_pos = 0;
52   sample_count = 0;
53
54   while( data_pos < cas_size )
55   {
56      UINT8   data = casdata[data_pos];
57      int      i;
58
59      for ( i = 0; i < 8; i++ )
60      {
61         /* Signal code */
62         sample_count += trs80l2_cas_cycle( buffer, sample_count, 33, 6, 6 );
63
64         /* Bit code */
65         if ( data & 0x80 )
66            sample_count += trs80l2_cas_cycle( buffer, sample_count, 33, 6, 6 );
67         else
68            sample_count += trs80l2_cas_cycle( buffer, sample_count, 33 + 6 + 6, 0, 0 );
69
70         data <<= 1;
71      }
72
73      data_pos++;
74   }
75   return sample_count;
76}
77
78
79/*******************************************************************
80   Generate samples for the tape image
81********************************************************************/
82static int trs80l2_cas_fill_wave(INT16 *buffer, int sample_count, UINT8 *bytes)
83{
84   return trs80l2_handle_cas( buffer, bytes );
85}
86
87
88/*******************************************************************
89   Calculate the number of samples needed for this tape image
90********************************************************************/
91static int trs80l2_cas_to_wav_size(const UINT8 *casdata, int caslen)
92{
93   cas_size = caslen;
94
95   return trs80l2_handle_cas( NULL, casdata );
96}
97
98static const struct CassetteLegacyWaveFiller trs80l2_cas_legacy_fill_wave =
99{
100   trs80l2_cas_fill_wave,               /* fill_wave */
101   -1,                              /* chunk_size */
102   0,                              /* chunk_samples */
103   trs80l2_cas_to_wav_size,            /* chunk_sample_calc */
104   44100,                           /* sample_frequency */
105   0,                              /* header_samples */
106   0                              /* trailer_samples */
107};
108
109
110static casserr_t trs80l2_cas_identify(cassette_image *cassette, struct CassetteOptions *opts)
111{
112   return cassette_legacy_identify(cassette, opts, &trs80l2_cas_legacy_fill_wave);
113}
114
115
116static casserr_t trs80l2_cas_load(cassette_image *cassette)
117{
118   return cassette_legacy_construct(cassette, &trs80l2_cas_legacy_fill_wave);
119}
120
121
122static const struct CassetteFormat trs80l2_cas_format =
123{
124   "cas",
125   trs80l2_cas_identify,
126   trs80l2_cas_load,
127   NULL
128};
129
130
131CASSETTE_FORMATLIST_START(trs80l2_cassette_formats)
132   CASSETTE_FORMAT(trs80l2_cas_format)
133CASSETTE_FORMATLIST_END
134
src/mess/formats/d64_dsk.c
r11457r11458
1/*********************************************************************
2
3    formats/d64_dsk.c
4
5    Floppy format code for Commodore 1541/2040/8050 disk images
6
7*********************************************************************/
8
9/*
10
11    TODO:
12
13    - fix MAX_ERROR_SECTORS, it is too small for d80/d82
14    - write to disk
15    - disk errors 24, 25, 26, 28, 74
16    - variable gaps
17
18*/
19
20#include "emu.h"
21#include "g64_dsk.h"
22#include "imagedev/flopimg.h"
23#include "formats/d64_dsk.h"
24#include "imagedev/flopdrv.h"
25
26/***************************************************************************
27    PARAMETERS
28***************************************************************************/
29
30#define LOG 1
31
32#define MAX_HEADS         2
33#define MAX_TRACKS         84
34#define MAX_ERROR_SECTORS   802 // TODO this is too small for .d80 files
35#define SECTOR_SIZE         256
36#define SECTOR_SIZE_GCR      368
37
38#define INVALID_OFFSET      0xbadbad
39
40#define D64_SIZE_35_TRACKS             174848
41#define D64_SIZE_35_TRACKS_WITH_ERRORS    175531
42#define D64_SIZE_40_TRACKS             196608
43#define D64_SIZE_40_TRACKS_WITH_ERRORS    197376
44#define D64_SIZE_42_TRACKS             205312
45#define D64_SIZE_42_TRACKS_WITH_ERRORS    206114
46#define D67_SIZE_35_TRACKS             176640
47#define D71_SIZE_70_TRACKS             349696
48#define D71_SIZE_70_TRACKS_WITH_ERRORS    351062
49#define D80_SIZE_77_TRACKS             533248
50#define D82_SIZE_154_TRACKS            1066496
51
52enum
53{
54   DOS1,
55   DOS2,
56   DOS25
57};
58
59static const char *const DOS_VERSION[] = { "1.0", "2.0", "2.5" };
60
61enum
62{
63   ERROR_00 = 1,
64   ERROR_20,      /* header block not found */
65   ERROR_21,      /* no sync character */
66   ERROR_22,      /* data block not present */
67   ERROR_23,      /* checksum error in data block */
68   ERROR_24,      /* write verify (on format) UNIMPLEMENTED */
69   ERROR_25,      /* write verify error UNIMPLEMENTED */
70   ERROR_26,      /* write protect on UNIMPLEMENTED */
71   ERROR_27,      /* checksum error in header block */
72   ERROR_28,      /* write error UNIMPLEMENTED */
73   ERROR_29,      /* disk ID mismatch */
74   ERROR_74,      /* disk not ready (no device 1) UNIMPLEMENTED */
75};
76
77static const char *const ERROR_CODE[] = { "00", "00", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "74" };
78
79static const UINT8 bin_2_gcr[] =
80{
81   0x0a, 0x0b, 0x12, 0x13, 0x0e, 0x0f, 0x16, 0x17,
82   0x09, 0x19, 0x1a, 0x1b, 0x0d, 0x1d, 0x1e, 0x15
83};
84
85/* This could be of use if we ever implement saving in .d64 format, to convert back GCR -> d64 */
86/*
87static const int gcr_2_bin[] =
88{
89    -1, -1,   -1,   -1,
90    -1, -1,   -1,   -1,
91    -1, 0x08, 0x00, 0x01,
92    -1, 0x0c, 0x04, 0x05,
93    -1, -1,   0x02, 0x03,
94    -1, 0x0f, 0x06, 0x07,
95    -1, 0x09, 0x0a, 0x0b,
96    -1, 0x0d, 0x0e, -1
97};
98*/
99
100static const int DOS1_SECTORS_PER_TRACK[] =
101{
102   21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21,
103   20, 20, 20, 20, 20, 20, 20,
104   18, 18, 18, 18, 18, 18,
105   17, 17, 17, 17, 17,
106   17, 17, 17, 17, 17,
107   17, 17
108};
109
110static const int DOS1_SPEED_ZONE[] =
111{
112   3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
113   2, 2, 2, 2, 2, 2, 2,
114   1, 1, 1, 1, 1, 1,
115   0, 0, 0, 0, 0,
116   0, 0, 0, 0, 0,
117   0, 0
118};
119
120static const int DOS2_SECTORS_PER_TRACK[] =
121{
122   21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21,
123   19, 19, 19, 19, 19, 19, 19,
124   18, 18, 18, 18, 18, 18,
125   17, 17, 17, 17, 17,
126   17, 17, 17, 17, 17,
127   17, 17
128};
129
130static const int DOS25_SECTORS_PER_TRACK[] =
131{
132   29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
133   29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,      /* 1-39 */
134   27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,                     /* 40-53 */
135   25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,                              /* 54-64 */
136   23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23,                        /* 65-77 */
137   23, 23, 23, 23, 23, 23, 23                                          /* 78-84 */
138};
139
140static const int DOS25_SPEED_ZONE[] =
141{
142   3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
143   3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,   /* 1-39 */
144   2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,               /* 40-53 */
145   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,                     /* 54-64 */
146   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,                  /* 65-77 */
147   0, 0, 0, 0, 0, 0, 0                                 /* 78-84 */
148};
149
150/***************************************************************************
151    TYPE DEFINITIONS
152***************************************************************************/
153
154struct d64dsk_tag
155{
156   int dos;                           /* CBM DOS version */
157   int heads;                           /* number of physical heads */
158   int tracks;                           /* number of physical tracks */
159   int dos_tracks;                        /* number of logical tracks */
160   int track_offset[MAX_HEADS][MAX_TRACKS];   /* offset within image for each physical track */
161   UINT32 speed_zone[MAX_TRACKS];            /* speed zone for each physical track */
162   bool has_errors;                     /* flag to check for available error codes */
163   UINT8 error[MAX_ERROR_SECTORS];            /* error code for each logical sector */
164   int error_offset[MAX_HEADS][MAX_TRACKS];   /* offset within error array for sector 0 of each logical track */
165
166   UINT8 id1, id2;                        /* DOS disk format ID */
167};
168
169/***************************************************************************
170    INLINE FUNCTIONS
171***************************************************************************/
172
173INLINE struct d64dsk_tag *get_tag(floppy_image *floppy)
174{
175   return (d64dsk_tag *)floppy_tag(floppy);
176}
177
178INLINE float get_dos_track(int track)
179{
180   return ((float)track / 2) + 1;
181}
182
183/***************************************************************************
184    IMPLEMENTATION
185***************************************************************************/
186
187/*-------------------------------------------------
188    d64_get_heads_per_disk - returns the number
189    of heads in the disk image
190-------------------------------------------------*/
191
192static int d64_get_heads_per_disk(floppy_image *floppy)
193{
194   return get_tag(floppy)->heads;
195}
196
197/*-------------------------------------------------
198    d64_get_tracks_per_disk - returns the number
199    of DOS tracks in the disk image
200-------------------------------------------------*/
201
202static int d64_get_tracks_per_disk(floppy_image *floppy)
203{
204   return get_tag(floppy)->tracks;
205}
206
207/*-------------------------------------------------
208    d64_get_sectors_per_track - returns the number
209    of sectors per given track
210-------------------------------------------------*/
211
212static int d64_get_sectors_per_track(floppy_image *floppy, int head, int track)
213{
214   int sectors_per_track = 0;
215
216   switch (get_tag(floppy)->dos)
217   {
218   case DOS1:   sectors_per_track = DOS1_SECTORS_PER_TRACK[track / 2]; break;
219   case DOS2:   sectors_per_track = DOS2_SECTORS_PER_TRACK[track / 2]; break;
220   case DOS25:   sectors_per_track = DOS25_SECTORS_PER_TRACK[track];    break;
221   }
222
223   return sectors_per_track;
224}
225
226/*-------------------------------------------------
227    get_track_offset - returns the offset within
228    the disk image for a given track
229-------------------------------------------------*/
230
231static floperr_t get_track_offset(floppy_image *floppy, int head, int track, UINT64 *offset)
232{
233   struct d64dsk_tag *tag = get_tag(floppy);
234   UINT64 offs = 0;
235
236   if ((track < 0) || (track >= tag->tracks))
237      return FLOPPY_ERROR_SEEKERROR;
238
239   offs = tag->track_offset[head][track];
240
241   if (offset)
242      *offset = offs;
243
244   return FLOPPY_ERROR_SUCCESS;
245}
246
247/*-------------------------------------------------
248    d64_get_track_size - returns the track size
249-------------------------------------------------*/
250
251static UINT32 d64_get_track_size(floppy_image *floppy, int head, int track)
252{
253   struct d64dsk_tag *tag = get_tag(floppy);
254
255   if (tag->track_offset[head][track] == INVALID_OFFSET)
256      return 0;
257
258   /* determine number of sectors per track */
259   int sectors_per_track = d64_get_sectors_per_track(floppy, head, track);
260
261   /* allocate temporary GCR track data buffer */
262   UINT32 track_length = sectors_per_track * SECTOR_SIZE_GCR;
263
264   return track_length;
265}
266
267/*-------------------------------------------------
268    get_sector_error_code - returns the error
269    code for the given sector
270-------------------------------------------------*/
271
272static int get_sector_error_code(floppy_image *floppy, int head, int dos_track, int sector)
273{
274   struct d64dsk_tag *tag = get_tag(floppy);
275
276   if (!tag->has_errors)
277      return ERROR_00;
278
279   int sector_error = tag->error[tag->error_offset[head][dos_track] + sector]; // TODO index out of bounds!!!
280
281   if (sector_error != ERROR_00)
282   {
283      logerror("D64 error %s head %u track %u sector %u\n", ERROR_CODE[sector_error], head, dos_track, sector);
284   }
285
286   return sector_error;
287}
288
289/*-------------------------------------------------
290    gcr_double_2_gcr - GCR decodes given data
291-------------------------------------------------*/
292
293/* gcr_double_2_gcr takes 4 bytes (a, b, c, d) and shuffles their nibbles to obtain 5 bytes in dest */
294/* The result is basically res = (enc(a) << 15) | (enc(b) << 10) | (enc(c) << 5) | enc(d)
295 * with res being 5 bytes long and enc(x) being the GCR encode of x.
296 * In fact, we store the result as five separate bytes in the dest argument
297 */
298
299/*
300
301 Commodore GCR format
302
303Original    Encoded
3044 bits      5 bits
305
3060000    ->  01010 = 0x0a
3070001    ->  01011 = 0x0b
3080010    ->  10010 = 0x12
3090011    ->  10011 = 0x13
3100100    ->  01110 = 0x0e
3110101    ->  01111 = 0x0f
3120110    ->  10110 = 0x16
3130111    ->  10111 = 0x17
3141000    ->  01001 = 0x09
3151001    ->  11001 = 0x19
3161010    ->  11010 = 0x1a
3171011    ->  11011 = 0x1b
3181100    ->  01101 = 0x0d
3191101    ->  11101 = 0x1d
3201110    ->  11110 = 0x1e
3211111    ->  10101 = 0x15
322
323We use the encoded values in bytes because we use them to encode
324groups of 4 bytes into groups of 5 bytes, below.
325
326*/
327
328static void gcr_double_2_gcr(UINT8 a, UINT8 b, UINT8 c, UINT8 d, UINT8 *dest)
329{
330   UINT8 gcr[8];
331
332   /* Encode each nibble to 5 bits */
333   gcr[0] = bin_2_gcr[a >> 4];
334   gcr[1] = bin_2_gcr[a & 0x0f];
335   gcr[2] = bin_2_gcr[b >> 4];
336   gcr[3] = bin_2_gcr[b & 0x0f];
337   gcr[4] = bin_2_gcr[c >> 4];
338   gcr[5] = bin_2_gcr[c & 0x0f];
339   gcr[6] = bin_2_gcr[d >> 4];
340   gcr[7] = bin_2_gcr[d & 0x0f];
341
342   /* Re-order the encoded data to only keep the 5 lower bits of each byte */
343   dest[0] = (gcr[0] << 3) | (gcr[1] >> 2);
344   dest[1] = (gcr[1] << 6) | (gcr[2] << 1) | (gcr[3] >> 4);
345   dest[2] = (gcr[3] << 4) | (gcr[4] >> 1);
346   dest[3] = (gcr[4] << 7) | (gcr[5] << 2) | (gcr[6] >> 3);
347   dest[4] = (gcr[6] << 5) | gcr[7];
348}
349
350/*-------------------------------------------------
351    d64_read_track - reads a full track from the
352    disk image
353-------------------------------------------------*/
354
355/*
356
357    Commodore sector format
358
359    SYNC                        FF * 5
360    08
361    CHECKSUM                    sector ^ track ^ id2 ^ id1
362    SECTOR                      0..20 (2040), 0..28 (8050)
363    TRACK                       1..35 (2040), 1..77 (8050), 1..70 (1571)
364    ID2
365    ID1
366    GAP 1                       55 * 9 (2040), 55 * 8 (1541)
367
368    SYNC                        FF * 5
369    07
370    NEXT TRACK
371    NEXT SECTOR
372    254 BYTES OF DATA
373    CHECKSUM
374    GAP 2                       55 * 8..19
375
376*/
377
378static floperr_t d64_read_track(floppy_image *floppy, int head, int track, UINT64 offset, void *buffer, size_t buflen)
379{
380   struct d64dsk_tag *tag = get_tag(floppy);
381   floperr_t err;
382   UINT64 track_offset;
383
384   /* get track offset */
385   err = get_track_offset(floppy, head, track, &track_offset);
386
387   if (err)
388      return err;
389
390   if (track_offset != INVALID_OFFSET)
391   {
392      UINT8 id1 = tag->id1;
393      UINT8 id2 = tag->id2;
394      int sectors_per_track;
395      UINT16 d64_track_size;
396      UINT8 *d64_track_data;
397      UINT16 gcr_track_size;
398      UINT8 *gcr_track_data;
399      UINT64 gcr_pos = 0;
400
401      /* determine logical track number */
402      int dos_track = get_dos_track(track);
403
404      if (tag->dos == DOS25)
405      {
406         dos_track = track + 1;
407      }
408
409      /* logical track numbers continue on the flip side */
410      if (head == 1) dos_track += tag->dos_tracks;
411
412      /* determine number of sectors per track */
413      sectors_per_track = d64_get_sectors_per_track(floppy, head, track);
414
415      /* allocate D64 track data buffer */
416      d64_track_size = sectors_per_track * SECTOR_SIZE;
417      d64_track_data = (UINT8 *)alloca(d64_track_size);
418
419      /* allocate temporary GCR track data buffer */
420      gcr_track_size = sectors_per_track * SECTOR_SIZE_GCR;
421      gcr_track_data = (UINT8 *)alloca(gcr_track_size);
422
423      if (buflen < gcr_track_size) fatalerror("D64 track buffer too small: %u!", (UINT32)buflen);
424
425      /* read D64 track data */
426      floppy_image_read(floppy, d64_track_data, track_offset, d64_track_size);
427
428      /* GCR encode D64 sector data */
429      for (int sector = 0; sector < sectors_per_track; sector++)
430      {
431         // here we convert the sector data to gcr directly!
432         // IMPORTANT: errors in reading sectors can modify e.g. header info $01 & $05
433         int sector_error = get_sector_error_code(floppy, head, track, sector);
434
435         /* first we set the position at which sector data starts in the image */
436         UINT64 d64_pos = sector * SECTOR_SIZE;
437         int i;
438
439         /*
440                1. Header sync       FF FF FF FF FF (40 'on' bits, not GCR encoded)
441                2. Header info       52 54 B5 29 4B 7A 5E 95 55 55 (10 GCR bytes)
442                3. Header gap        55 55 55 55 55 55 55 55 55 (9 bytes, never read)
443                4. Data sync         FF FF FF FF FF (40 'on' bits, not GCR encoded)
444                5. Data block        55...4A (325 GCR bytes)
445                6. Inter-sector gap  55 55 55 55...55 55 (4 to 19 bytes, never read)
446            */
447
448         if (sector_error == ERROR_29)
449            id1 ^= 0xff;
450
451         /* Header sync */
452         if (sector_error != ERROR_21)
453         {
454            for (i = 0; i < 5; i++)
455               gcr_track_data[gcr_pos + i] = 0xff;
456            gcr_pos += 5;
457         }
458
459         /* Header info */
460         /* These are 8 bytes unencoded, which become 10 bytes encoded */
461         // $00 - header block ID ($08)                      // this byte can be modified by error code 20 -> 0xff
462         // $01 - header block checksum (EOR of $02-$05)     // this byte can be modified by error code 27 -> ^ 0xff
463         // $02 - Sector# of data block
464         // $03 - Track# of data block
465         UINT8 header_block_id = (sector_error == ERROR_20) ? 0xff : 0x08;
466         UINT8 header_block_checksum = sector ^ dos_track ^ id2 ^ id1;
467
468         if (sector_error == ERROR_27)
469            header_block_checksum ^= 0xff;
470
471         gcr_double_2_gcr(header_block_id, header_block_checksum, sector, dos_track, gcr_track_data + gcr_pos);
472         gcr_pos += 5;
473
474         // $04 - Format ID byte #2
475         // $05 - Format ID byte #1
476         // $06 - $0F ("off" byte)
477         // $07 - $0F ("off" byte)
478         gcr_double_2_gcr(id2, id1, 0x0f, 0x0f, gcr_track_data + gcr_pos);
479         gcr_pos += 5;
480
481         /* Header gap */
482         for (i = 0; i < 9; i++)
483            gcr_track_data[gcr_pos + i] = 0x55;
484         gcr_pos += 9;
485
486         /* Data sync */
487         for (i = 0; i < 5; i++)
488            gcr_track_data[gcr_pos + i] = 0xff;
489         gcr_pos += 5;
490
491         /* Data block */
492         UINT8 data_block_id = (sector_error == ERROR_22) ? 0xff : 0x07;
493
494         // we first need to calculate the checksum of the 256 bytes of the sector
495         UINT8 sector_checksum = d64_track_data[d64_pos];
496         for (i = 1; i < 256; i++)
497            sector_checksum ^= d64_track_data[d64_pos + i];
498
499         if (sector_error == ERROR_23)
500            sector_checksum ^= 0xff;
501
502         /*
503                $00      - data block ID ($07)
504                $01-100  - 256 bytes sector data
505                $101     - data block checksum (EOR of $01-100)
506                $102-103 - $00 ("off" bytes, to make the sector size a multiple of 5)
507            */
508         gcr_double_2_gcr(data_block_id, d64_track_data[d64_pos], d64_track_data[d64_pos + 1], d64_track_data[d64_pos + 2], gcr_track_data + gcr_pos);
509         gcr_pos += 5;
510
511         for (i = 1; i < 64; i++)
512         {
513            gcr_double_2_gcr(d64_track_data[d64_pos + 4 * i - 1], d64_track_data[d64_pos + 4 * i],
514                           d64_track_data[d64_pos + 4 * i + 1], d64_track_data[d64_pos + 4 * i + 2], gcr_track_data + gcr_pos);
515            gcr_pos += 5;
516         }
517
518         gcr_double_2_gcr(d64_track_data[d64_pos + 255], sector_checksum, 0x00, 0x00, gcr_track_data + gcr_pos);
519         gcr_pos += 5;
520
521         /* Inter-sector gap */
522         // "In tests that the author conducted on a real 1541 disk, gap sizes of 8 to 19 bytes were seen."
523         // Here we put 14 as an average...
524         for (i = 0; i < 14; i++)
525            gcr_track_data[gcr_pos + i] = 0x55;
526         gcr_pos += 14;
527      }
528
529      /* copy GCR track data to buffer */
530      memcpy(buffer, gcr_track_data, gcr_track_size);
531   }
532   else   /* half tracks */
533   {
534      /* set track length to 0 */
535      memset(buffer, 0, buflen);
536   }
537
538   return FLOPPY_ERROR_SUCCESS;
539}
540
541/*-------------------------------------------------
542    d64_write_track - writes a full track to the
543    disk image
544-------------------------------------------------*/
545
546static floperr_t d64_write_track(floppy_image *floppy, int head, int track, UINT64 offset, const void *buffer, size_t buflen)
547{
548   return FLOPPY_ERROR_UNSUPPORTED;
549}
550
551/*-------------------------------------------------
552    d64_identify - identifies the disk image
553-------------------------------------------------*/
554
555static void d64_identify(floppy_image *floppy, int *dos, int *heads, int *tracks, bool *has_errors)
556{
557   switch (floppy_image_size(floppy))
558   {
559   /* 2040/3040 */
560   case D67_SIZE_35_TRACKS:            *dos = DOS1;  *heads = 1; *tracks = 35; *has_errors = false; break;
561
562   /* 4040/2031/1541/1551 */
563   case D64_SIZE_35_TRACKS:            *dos = DOS2;  *heads = 1; *tracks = 35; *has_errors = false; break;
564   case D64_SIZE_35_TRACKS_WITH_ERRORS:   *dos = DOS2;  *heads = 1; *tracks = 35; *has_errors = true;  break;
565   case D64_SIZE_40_TRACKS:            *dos = DOS2;  *heads = 1; *tracks = 40; *has_errors = false; break;
566   case D64_SIZE_40_TRACKS_WITH_ERRORS:   *dos = DOS2;  *heads = 1; *tracks = 40; *has_errors = true;  break;
567   case D64_SIZE_42_TRACKS:            *dos = DOS2;  *heads = 1; *tracks = 42; *has_errors = false; break;
568   case D64_SIZE_42_TRACKS_WITH_ERRORS:   *dos = DOS2;  *heads = 1; *tracks = 42; *has_errors = true;  break;
569
570   /* 1571 */
571   case D71_SIZE_70_TRACKS:            *dos = DOS2;  *heads = 2; *tracks = 35; *has_errors = false; break;
572   case D71_SIZE_70_TRACKS_WITH_ERRORS:   *dos = DOS2;  *heads = 2; *tracks = 35; *has_errors = true;  break;
573
574   /* 8050 */
575   case D80_SIZE_77_TRACKS:            *dos = DOS25; *heads = 1; *tracks = 77; *has_errors = false; break;
576
577   /* 8250/SFD1001 */
578   case D82_SIZE_154_TRACKS:            *dos = DOS25; *heads = 2; *tracks = 77; *has_errors = false; break;
579   }
580}
581
582/*-------------------------------------------------
583    FLOPPY_IDENTIFY( d64_dsk_identify )
584-------------------------------------------------*/
585
586FLOPPY_IDENTIFY( d64_dsk_identify )
587{
588   int dos = 0, heads, tracks;
589   bool has_errors = false;
590
591   *vote = 0;
592
593   d64_identify(floppy, &dos, &heads, &tracks, &has_errors);
594
595   if (dos == DOS2 && heads == 1)
596   {
597      *vote = 100;
598   }
599
600   return FLOPPY_ERROR_SUCCESS;
601}
602
603/*-------------------------------------------------
604    FLOPPY_IDENTIFY( d67_dsk_identify )
605-------------------------------------------------*/
606
607FLOPPY_IDENTIFY( d67_dsk_identify )
608{
609   *vote = 0;
610
611   if (floppy_image_size(floppy) == D67_SIZE_35_TRACKS)
612   {
613      *vote = 100;
614   }
615
616   return FLOPPY_ERROR_SUCCESS;
617}
618
619/*-------------------------------------------------
620    FLOPPY_IDENTIFY( d71_dsk_identify )
621-------------------------------------------------*/
622
623FLOPPY_IDENTIFY( d71_dsk_identify )
624{
625   int heads = 0, tracks = 0, dos = -1;
626   bool has_errors = false;
627
628   *vote = 0;
629
630   d64_identify(floppy, &dos, &heads, &tracks, &has_errors);
631
632   if (dos == DOS2 && heads == 2)
633   {
634      *vote = 100;
635   }
636
637   return FLOPPY_ERROR_SUCCESS;
638}
639
640/*-------------------------------------------------
641    FLOPPY_IDENTIFY( d80_dsk_identify )
642-------------------------------------------------*/
643
644FLOPPY_IDENTIFY( d80_dsk_identify )
645{
646   *vote = 0;
647
648   if (floppy_image_size(floppy) == D80_SIZE_77_TRACKS)
649   {
650      *vote = 100;
651   }
652
653   return FLOPPY_ERROR_SUCCESS;
654}
655
656/*-------------------------------------------------
657    FLOPPY_IDENTIFY( d82_dsk_identify )
658-------------------------------------------------*/
659
660FLOPPY_IDENTIFY( d82_dsk_identify )
661{
662   *vote = 0;
663
664   if (floppy_image_size(floppy) == D82_SIZE_154_TRACKS)
665   {
666      *vote = 100;
667   }
668
669   return FLOPPY_ERROR_SUCCESS;
670}
671
672/*-------------------------------------------------
673    FLOPPY_CONSTRUCT( d64_dsk_construct )
674-------------------------------------------------*/
675
676FLOPPY_CONSTRUCT( d64_dsk_construct )
677{
678   struct FloppyCallbacks *callbacks;
679   struct d64dsk_tag *tag;
680   UINT8 id[2];
681
682   int track_offset = 0;
683   int head, track;
684
685   int heads = 0, dos_tracks = 0, dos = 0;
686   bool has_errors = 0;
687   int errors_size = 0;
688
689   if (params)
690   {
691      /* create not supported */
692      return FLOPPY_ERROR_UNSUPPORTED;
693   }
694
695   tag = (struct d64dsk_tag *) floppy_create_tag(floppy, sizeof(struct d64dsk_tag));
696
697   if (!tag) return FLOPPY_ERROR_OUTOFMEMORY;
698
699   /* identify image type */
700   d64_identify(floppy, &dos, &heads, &dos_tracks, &has_errors);
701
702   tag->dos = dos;
703   tag->heads = heads;
704   tag->tracks = MAX_TRACKS;
705   tag->dos_tracks = dos_tracks;
706   tag->has_errors = has_errors;
707
708   if (LOG)
709   {
710      logerror("D64 size: %04x\n", (UINT32)floppy_image_size(floppy));
711      logerror("D64 heads: %u\n", heads);
712      logerror("D64 tracks: %u\n", dos_tracks);
713      logerror("D64 DOS version: %s\n", DOS_VERSION[dos]);
714      logerror("D64 error codes: %s\n", has_errors ? "yes" : "no");
715   }
716
717   /* clear track data offsets */
718   for (head = 0; head < MAX_HEADS; head++)
719   {
720      for (track = 0; track < MAX_TRACKS; track++)
721      {
722         tag->track_offset[head][track] = INVALID_OFFSET;
723      }
724   }
725
726   /* determine track data offsets */
727   for (head = 0; head < heads; head++)
728   {
729      for (track = 0; track < tag->tracks; track++)
730      {
731         if (dos == DOS25)
732         {
733            if (track >= dos_tracks)
734            {
735               /* track out of range */
736               tag->track_offset[head][track] = INVALID_OFFSET;
737            }
738            else
739            {
740               tag->track_offset[head][track] = track_offset;
741               tag->error_offset[head][track] = errors_size;
742
743               track_offset += DOS25_SECTORS_PER_TRACK[track] * SECTOR_SIZE;
744               /* also store an error entry for each sector */
745               errors_size += DOS25_SECTORS_PER_TRACK[track];
746
747               if (LOG)
748               {
749                  logerror("D64 head %u track %u data offset: %04x\n", head, track + 1, tag->track_offset[head][track]);
750                  if (has_errors) logerror("D64 head %u track %u error offset: %04x\n", head, track + 1, tag->error_offset[head][track]);
751               }
752            }
753         }
754         else
755         {
756            if ((track % 2) || ((track / 2) >= dos_tracks))
757            {
758               /* half track or out of range */
759               tag->track_offset[head][track] = INVALID_OFFSET;
760            }
761            else
762            {
763               /* full track */
764               tag->track_offset[head][track] = track_offset;
765               tag->error_offset[head][track] = errors_size;
766
767               if (dos == DOS1)
768               {
769                  track_offset += DOS1_SECTORS_PER_TRACK[track / 2] * SECTOR_SIZE;
770                  /* also store an error entry for each sector */
771                  errors_size += DOS1_SECTORS_PER_TRACK[track / 2];
772               }
773               else
774               {
775                  track_offset += DOS2_SECTORS_PER_TRACK[track / 2] * SECTOR_SIZE;
776                  /* also store an error entry for each sector */
777                  errors_size += DOS2_SECTORS_PER_TRACK[track / 2];
778               }
779
780               if (LOG)
781               {
782                  logerror("D64 head %u track %.1f data offset: %04x\n", head, get_dos_track(track), tag->track_offset[head][track]);
783                  if (has_errors) logerror("D64 head %u track %.1f error offset: %04x\n", head, get_dos_track(track), tag->error_offset[head][track]);
784               }
785            }
786         }
787      }
788   }
789
790   /* determine speed zones */
791   for (track = 0; track < tag->tracks; track++)
792   {
793      if (dos == DOS25)
794      {
795         tag->speed_zone[track] = DOS25_SPEED_ZONE[track];
796
797         if (LOG) logerror("D64 track %u speed zone: %u\n", track + 1, tag->speed_zone[track]);
798      }
799      else
800      {
801         tag->speed_zone[track] = DOS1_SPEED_ZONE[track / 2];
802
803         if (LOG) logerror("D64 track %.1f speed zone: %u\n", get_dos_track(track), tag->speed_zone[track]);
804      }
805   }
806
807   /* read format ID from directory */
808   /*
809    id1, id2 are the same for extended d64 (i.e. with error tables), for d67 and for d71
810
811    for d81 they are at track 40 bytes 0x17 & 0x18
812    for d80 & d82 they are at track 39 bytes 0x18 & 0x19
813    */
814   if (dos == DOS25)
815      floppy_image_read(floppy, id, tag->track_offset[0][38] + 0x18, 2);
816   else
817      floppy_image_read(floppy, id, tag->track_offset[0][34] + 0xa2, 2);
818
819   tag->id1 = id[0];
820   tag->id2 = id[1];
821
822   if (LOG) logerror("D64 format ID: %02x%02x\n", id[0], id[1]);
823
824   /* read errors */
825   if (tag->has_errors)
826   {
827      if (LOG) logerror("D64 error blocks: %u %u\n", errors_size, track_offset);
828      floppy_image_read(floppy, tag->error, track_offset, errors_size);
829   }
830   else
831   {
832      memset(tag->error, ERROR_00, MAX_ERROR_SECTORS);
833   }
834
835   /* set callbacks */
836   callbacks = floppy_callbacks(floppy);
837
838   callbacks->read_track = d64_read_track;
839   callbacks->write_track = d64_write_track;
840   callbacks->get_heads_per_disk = d64_get_heads_per_disk;
841   callbacks->get_tracks_per_disk = d64_get_tracks_per_disk;
842   callbacks->get_track_size = d64_get_track_size;
843
844   return FLOPPY_ERROR_SUCCESS;
845}
src/mess/formats/trd_dsk.h
r11457r11458
1/*********************************************************************
2
3    formats/trd_dsk.h
4
5    TRD disk images
6
7*********************************************************************/
8
9#ifndef TRD_DSK_H
10#define TRD_DSK_H
11
12#include "imagedev/flopimg.h"
13
14/**************************************************************************/
15
16FLOPPY_OPTIONS_EXTERN(trd);
17
18#endif /* TRD_DSK_H */
src/mess/formats/ace_tap.h
r11457r11458
1/*********************************************************************
2
3    ace_tap.h
4
5    Format code for Jupiter Ace casette files
6
7*********************************************************************/
8
9#ifndef ACE_TAP_H
10#define ACE_TAP_H
11
12#include "imagedev/cassimg.h"
13
14typedef struct
15{
16   UINT8 hdr_type;
17   UINT8 hdr_name[10];
18   UINT16 hdr_len;
19   UINT16 hdr_addr;
20   UINT8 hdr_vars[8];
21   UINT8 hdr_3c4c;
22   UINT8 hdr_3c4d;
23   UINT16 dat_len;
24} ace_tape_t;
25
26CASSETTE_FORMATLIST_EXTERN(ace_cassette_formats);
27
28#endif /* ACE_TAP_H */
29
src/mess/formats/g64_dsk.c
r11457r11458
1/*********************************************************************
2
3    formats/g64_dsk.c
4
5    Floppy format code for Commodore 1541 GCR disk images
6
7*********************************************************************/
8
9/*
10
11    TODO:
12
13    - speed zones
14    - writing
15
16*/
17
18#include "emu.h"
19#include "g64_dsk.h"
20#include "imagedev/flopimg.h"
21#include "imagedev/flopdrv.h"
22
23/***************************************************************************
24    PARAMETERS
25***************************************************************************/
26
27#define LOG 0
28
29#define HEADER_LENGTH   0x2ac            /* standard length for 84 half tracks */
30#define MAX_TRACKS      84
31
32/***************************************************************************
33    TYPE DEFINITIONS
34***************************************************************************/
35
36struct g64dsk_tag
37{
38   int version;
39   int heads;                        /* number of physical heads */
40   int tracks;                        /* number of physical tracks */
41   UINT16 track_size;                  /* size of each track */
42   UINT32 track_offset[MAX_TRACKS];      /* offset within data for each track */
43   UINT32 speed_zone_offset[MAX_TRACKS];   /* offset within data for each track */
44};
45
46/***************************************************************************
47    INLINE FUNCTIONS
48***************************************************************************/
49
50INLINE struct g64dsk_tag *get_tag(floppy_image *floppy)
51{
52   return (g64dsk_tag *)floppy_tag(floppy);
53}
54
55INLINE float get_dos_track(int track)
56{
57   return ((float)track / 2) + 1;
58}
59
60/***************************************************************************
61    IMPLEMENTATION
62***************************************************************************/
63
64/*-------------------------------------------------
65    g64_get_heads_per_disk - returns the number
66    of heads in the disk image
67-------------------------------------------------*/
68
69static int g64_get_heads_per_disk(floppy_image *floppy)
70{
71   return get_tag(floppy)->heads;
72}
73
74/*-------------------------------------------------
75    g64_get_tracks_per_disk - returns the number
76    of DOS tracks in the disk image
77-------------------------------------------------*/
78
79static int g64_get_tracks_per_disk(floppy_image *floppy)
80{
81   return get_tag(floppy)->tracks;
82}
83
84/*-------------------------------------------------
85    get_track_offset - returns the offset within
86    the disk image for a given track
87-------------------------------------------------*/
88
89static floperr_t get_track_offset(floppy_image *floppy, int head, int track, UINT64 *offset)
90{
91   struct g64dsk_tag *tag = get_tag(floppy);
92   UINT64 offs = 0;
93
94   if ((track < 0) || (track >= tag->tracks))
95      return FLOPPY_ERROR_SEEKERROR;
96
97   offs = tag->track_offset[track];
98
99   if (offset)
100      *offset = offs;
101
102   return FLOPPY_ERROR_SUCCESS;
103}
104
105/*-------------------------------------------------
106    g64_get_track_size - returns the track size
107-------------------------------------------------*/
108
109static UINT32 g64_get_track_size(floppy_image *floppy, int head, int track)
110{
111   /* get track offset */
112   UINT64 track_offset;
113   floperr_t err = get_track_offset(floppy, head, track, &track_offset);
114
115   if (err)
116      return 0;
117
118   /* read track length */
119   UINT8 size[2];
120   floppy_image_read(floppy, &size, track_offset, 2);
121   UINT32 track_length = (size[1] << 8) | size[0];
122
123   return track_length;
124}
125
126/*-------------------------------------------------
127    g64_read_track - reads a full track from the
128    disk image
129-------------------------------------------------*/
130
131static floperr_t g64_read_track(floppy_image *floppy, int head, int track, UINT64 offset, void *buffer, size_t buflen)
132{
133   struct g64dsk_tag *tag = get_tag(floppy);
134   floperr_t err;
135   UINT64 track_offset;
136   UINT16 track_length = 0;
137
138   /* get track offset */
139   err = get_track_offset(floppy, head, track, &track_offset);
140
141   if (err)
142      return err;
143
144   if (!head && track_offset)
145   {
146      if (buflen < tag->track_size) fatalerror("G64 track buffer too small: %u!", (UINT32)buflen);
147
148      /* read track */
149      floppy_image_read(floppy, buffer, track_offset + 2, tag->track_size);
150   }
151   else
152   {
153      /* set track length to 0 */
154      memset(buffer, 0, buflen);
155   }
156
157   if (LOG) logerror("G64 track %.1f length %u\n", get_dos_track(track), track_length);
158
159   return FLOPPY_ERROR_SUCCESS;
160}
161
162/*-------------------------------------------------
163    g64_write_track - writes a full track to the
164    disk image
165-------------------------------------------------*/
166
167static floperr_t g64_write_track(floppy_image *floppy, int head, int track, UINT64 offset, const void *buffer, size_t buflen)
168{
169   return FLOPPY_ERROR_UNSUPPORTED;
170}
171
172/*-------------------------------------------------
173    FLOPPY_IDENTIFY( g64_dsk_identify )
174-------------------------------------------------*/
175
176FLOPPY_IDENTIFY( g64_dsk_identify )
177{
178   UINT8 header[8];
179
180   floppy_image_read(floppy, header, 0, 8);
181
182   if (!strncmp((const char *)header, "GCR-1541", 8))
183   {
184      *vote = 100;
185   }
186   else
187   {
188      *vote = 0;
189   }
190
191   return FLOPPY_ERROR_SUCCESS;
192}
193
194/*-------------------------------------------------
195    FLOPPY_CONSTRUCT( g64_dsk_construct )
196-------------------------------------------------*/
197
198/*
199
200    G64 File Header Format
201
202  Bytes: $0000-0007: File signature "GCR-1541"
203               0008: G64 version (presently only $00 defined)
204               0009: Number of tracks in image (usually $54, decimal 84)
205          000A-000B: Size of each stored track in bytes (usually  7928,  or
206                     $1EF8 in LO/HI format.
207
208  Bytes: $000C-000F: Offset  to  stored  track  1.0  ($000002AC,  in  LO/HI
209                     format, see below for more)
210          0010-0013: Offset to stored track 1.5 ($00000000)
211          0014-0017: Offset to stored track 2.0 ($000021A6)
212             ...
213          0154-0157: Offset to stored track 42.0 ($0004F8B6)
214          0158-015B: Offset to stored track 42.5 ($00000000)
215
216  Bytes: $015C-015F: Speed zone entry for track 1 ($03,  in  LO/HI  format,
217                     see below for more)
218          0160-0163: Speed zone entry for track 1.5 ($03)
219             ...
220          02A4-02A7: Speed zone entry for track 42 ($00)
221          02A8-02AB: Speed zone entry for track 42.5 ($00)
222
223  Bytes: $02AC-02AD: Actual size of stored track (7692 or $1E0C,  in  LO/HI
224                     format)
225          02AE-02AE+$1E0C: Track data
226
227*/
228
229FLOPPY_CONSTRUCT( g64_dsk_construct )
230{
231   struct FloppyCallbacks *callbacks;
232   struct g64dsk_tag *tag;
233   UINT8 header[HEADER_LENGTH];
234   UINT64 pos = 0;
235   int i;
236
237   if (params)
238   {
239      /* create not supported */
240      return FLOPPY_ERROR_UNSUPPORTED;
241   }
242
243   tag = (struct g64dsk_tag *) floppy_create_tag(floppy, sizeof(struct g64dsk_tag));
244
245   if (!tag) return FLOPPY_ERROR_OUTOFMEMORY;
246
247   /* version */
248   floppy_image_read(floppy, header, pos, 0x0c); pos += 0xc;
249   tag->version = header[8];
250   if (LOG) logerror("G64 version: %u\n", tag->version);
251
252   /* number of half tracks */
253   tag->heads = 1;
254   tag->tracks = header[9];
255   if (LOG) logerror("G64 tracks: %u\n", tag->tracks);
256
257   /* size of each stored half track */
258   tag->track_size = (header[11] << 8) | header[10];
259   if (LOG) logerror("G64 track size: %04x\n", tag->track_size);
260
261   /* data offsets */
262   for (i = 0; i < tag->tracks; i++)
263   {
264      floppy_image_read(floppy, header, pos, 4); pos += 4;
265      tag->track_offset[i] = (header[3] << 24) | (header[2] << 16) | (header[1] << 8) | header[0];
266
267      if (LOG) logerror("G64 track %.1f data offset: %04x\n", get_dos_track(i), tag->track_offset[i]);
268   }
269
270   /* speed zone offsets */
271   for (i = 0; i < tag->tracks; i++)
272   {
273      floppy_image_read(floppy, header, pos, 4); pos += 4;
274      tag->speed_zone_offset[i] = (header[3] << 24) | (header[2] << 16) | (header[1] << 8) | header[0];
275
276      if (LOG)
277      {
278         if (tag->speed_zone_offset[i] < 4)
279            logerror("G64 track %.1f speed zone: %u\n", get_dos_track(i), tag->speed_zone_offset[i]);
280         else
281            logerror("G64 track %.1f speed zone offset: %04x\n", get_dos_track(i), tag->speed_zone_offset[i]);
282      }
283   }
284
285   /* set callbacks */
286   callbacks = floppy_callbacks(floppy);
287
288   callbacks->read_track = g64_read_track;
289   callbacks->write_track = g64_write_track;
290   callbacks->get_heads_per_disk = g64_get_heads_per_disk;
291   callbacks->get_tracks_per_disk = g64_get_tracks_per_disk;
292   callbacks->get_track_size = g64_get_track_size;
293
294   return FLOPPY_ERROR_SUCCESS;
295}
src/mess/formats/trs_cas.h
r11457r11458
1/*********************************************************************
2
3    trs_cas.h
4
5    Format code for TRS80 (level 2) casette files.
6
7*********************************************************************/
8
9#ifndef __TRS_CAS_H
10#define __TRS_CAS_H
11
12#include "imagedev/cassimg.h"
13
14CASSETTE_FORMATLIST_EXTERN(trs80l2_cassette_formats);
15
16#endif /* __TRS_CAS_H */
17
src/mess/formats/pc_dsk.c
r11457r11458
1/*********************************************************************
2
3    formats/pc_dsk.c
4
5    PC disk images
6
7*********************************************************************/
8
9#include <string.h>
10
11#include "formats/pc_dsk.h"
12#include "formats/basicdsk.h"
13
14struct pc_disk_sizes
15{
16   UINT32 image_size;
17   int sectors;
18   int heads;
19};
20
21
22
23static const struct pc_disk_sizes disk_sizes[] =
24{
25   { 8*1*40*512,  8, 1},   /* 5 1/4 inch double density single sided */
26   { 8*2*40*512,  8, 2},   /* 5 1/4 inch double density */
27   { 9*1*40*512,  9, 1},   /* 5 1/4 inch double density single sided */
28   { 9*2*40*512,  9, 2},   /* 5 1/4 inch double density */
29   {10*2*40*512, 10, 2},   /* 5 1/4 inch double density single sided */
30   { 9*2*80*512,  9, 2},   /* 80 tracks 5 1/4 inch drives rare in PCs */
31   { 9*2*80*512,  9, 2},   /* 3 1/2 inch double density */
32   {15*2*80*512, 15, 2},   /* 5 1/4 inch high density (or japanese 3 1/2 inch high density) */
33   {18*2*80*512, 18, 2},   /* 3 1/2 inch high density */
34   {36*2*80*512, 36, 2}   /* 3 1/2 inch enhanced density */
35};
36
37
38
39static floperr_t pc_dsk_compute_geometry(floppy_image *floppy, struct basicdsk_geometry *geometry)
40{
41   int i;
42   UINT64 size;
43
44   memset(geometry, 0, sizeof(*geometry));
45   size = floppy_image_size(floppy);
46
47   for (i = 0; i < ARRAY_LENGTH(disk_sizes); i++)
48   {
49      if (disk_sizes[i].image_size == size)
50      {
51         geometry->sectors = disk_sizes[i].sectors;
52         geometry->heads = disk_sizes[i].heads;
53         geometry->sector_length = 512;
54         geometry->first_sector_id = 1;
55         geometry->tracks = (int) (size / disk_sizes[i].sectors / disk_sizes[i].heads / geometry->sector_length);
56         return FLOPPY_ERROR_SUCCESS;
57      }
58   }
59
60   if (size >= 0x1a)
61   {
62      /*
63         * get info from boot sector.
64         * not correct on all disks
65         */
66      UINT8 scl, spt, heads;
67      floppy_image_read(floppy, &scl, 0x0c, 1);
68      floppy_image_read(floppy, &spt, 0x18, 1);
69      floppy_image_read(floppy, &heads, 0x1A, 1);
70
71      if (size == ((UINT64) scl) * spt * heads * 0x200)
72      {
73         geometry->sectors = spt;
74         geometry->heads = heads;
75         geometry->sector_length = 512;
76         geometry->first_sector_id = 1;
77         geometry->tracks = scl;
78         return FLOPPY_ERROR_SUCCESS;
79      }
80   }
81
82   return FLOPPY_ERROR_SUCCESS;
83}
84
85
86
87static FLOPPY_IDENTIFY(pc_dsk_identify)
88{
89   floperr_t err;
90   struct basicdsk_geometry geometry;
91
92   err = pc_dsk_compute_geometry(floppy, &geometry);
93   if (err)
94      return err;
95
96   *vote = geometry.heads ? 100 : 0;
97   return FLOPPY_ERROR_SUCCESS;
98}
99
100
101
102static FLOPPY_CONSTRUCT(pc_dsk_construct)
103{
104   floperr_t err;
105   struct basicdsk_geometry geometry;
106
107   if (params)
108   {
109      /* create */
110      memset(&geometry, 0, sizeof(geometry));
111      geometry.heads = option_resolution_lookup_int(params, PARAM_HEADS);
112      geometry.tracks = option_resolution_lookup_int(params, PARAM_TRACKS);
113      geometry.sectors = option_resolution_lookup_int(params, PARAM_SECTORS);
114      geometry.first_sector_id = 1;
115      geometry.sector_length = 512;
116   }
117   else
118   {
119      /* open */
120      err = pc_dsk_compute_geometry(floppy, &geometry);
121      if (err)
122         return err;
123   }
124
125   return basicdsk_construct(floppy, &geometry);
126}
127
128
129
130/* ----------------------------------------------------------------------- */
131
132FLOPPY_OPTIONS_START( pc )
133   FLOPPY_OPTION( pc_dsk, "dsk,ima,img,ufi,360",      "PC floppy disk image",   pc_dsk_identify, pc_dsk_construct, NULL,
134      HEADS([1]-2)
135      TRACKS(40/[80])
136      SECTORS(8/[9]/10/15/18/36))
137FLOPPY_OPTIONS_END
src/mess/formats/d64_dsk.h
r11457r11458
1/*********************************************************************
2
3    formats/d64_dsk.h
4
5    Floppy format code for Commodore 1541/2040/8050 disk images
6
7*********************************************************************/
8
9#ifndef __D64_DSK__
10#define __D64_DSK__
11
12#include "imagedev/flopimg.h"
13
14/***************************************************************************
15    PROTOTYPES
16***************************************************************************/
17
18FLOPPY_IDENTIFY( d64_dsk_identify );
19FLOPPY_IDENTIFY( d67_dsk_identify );
20FLOPPY_IDENTIFY( d71_dsk_identify );
21FLOPPY_IDENTIFY( d80_dsk_identify );
22FLOPPY_IDENTIFY( d82_dsk_identify );
23
24FLOPPY_CONSTRUCT( d64_dsk_construct );
25
26#endif
src/mess/formats/lviv_lvt.c
r11457r11458
1/* .LVT tape images */
2#include "lviv_lvt.h"
3
4#define WAVEENTRY_LOW  -32768
5#define WAVEENTRY_HIGH  32767
6
7#define LVIV_LVT_BIT_SAMPLES            60
8#define LVIV_LVT_HEADER_PILOT_SAMPLES      5190*60
9#define LVIV_LVT_HEADER_DATA_SAMPLES      16*11*60
10#define LVIV_LVT_PAUSE_SAMPLES            69370
11#define LVIV_LVT_BLOCK_PILOT_SAMPLES      1298*60
12
13#define LVIV_LVT_HEADER_PILOT_LENGTH      5190
14#define LVIV_LVT_BLOCK_PILOT_LENGTH         1298
15
16static INT16 *lviv_emit_level(INT16 *p, int count, int level)
17{
18   int i;
19
20   for (i=0; i<count; i++)
21   {
22      *(p++) = level;
23   }
24   return p;
25}
26
27static INT16* lviv_output_bit(INT16 *p, UINT8 b)
28{
29   if (b)
30   {
31      p = lviv_emit_level (p, 15, WAVEENTRY_HIGH);
32      p = lviv_emit_level (p, 15, WAVEENTRY_LOW);
33      p = lviv_emit_level (p, 15, WAVEENTRY_HIGH);
34      p = lviv_emit_level (p, 15, WAVEENTRY_LOW);
35   }
36   else
37   {
38      p = lviv_emit_level (p, 30, WAVEENTRY_HIGH);
39      p = lviv_emit_level (p, 30, WAVEENTRY_LOW);
40   }
41       return p;
42}
43
44static INT16* lviv_output_byte(INT16 *p, UINT8 byte)
45{
46   int i;
47
48   p = lviv_output_bit (p, 0);
49
50   for (i=0; i<8; i++)
51      p = lviv_output_bit(p,(byte>>i) & 0x01);
52
53   p = lviv_output_bit (p, 1);
54   p = lviv_output_bit (p, 1);
55   return p;
56}
57
58/*************************************************************************************/
59
60static int lviv_cassette_calculate_size_in_samples(const UINT8 *bytes, int length)
61{
62   int size;
63
64   size =  LVIV_LVT_HEADER_PILOT_SAMPLES +
65      LVIV_LVT_HEADER_DATA_SAMPLES +
66      LVIV_LVT_PAUSE_SAMPLES +
67      LVIV_LVT_BLOCK_PILOT_SAMPLES +
68      (length-0x10)*11*LVIV_LVT_BIT_SAMPLES;
69
70   return size;
71}
72
73/*************************************************************************************/
74
75static int lviv_cassette_fill_wave(INT16 *buffer, int length, UINT8 *bytes)
76{
77   int i;
78   INT16 * p = buffer;
79
80   int data_size;
81
82   for (i=0; i<LVIV_LVT_HEADER_PILOT_LENGTH; i++)
83      p = lviv_output_bit (p, 1);
84
85   for (i=0; i<10; i++)
86      p = lviv_output_byte (p, bytes[0x09]);
87
88   for (i=0; i<6; i++)
89      p = lviv_output_byte (p, bytes[0x0a+i]);
90
91   p = lviv_emit_level (p, LVIV_LVT_PAUSE_SAMPLES, WAVEENTRY_HIGH);
92
93   for (i=0; i<LVIV_LVT_BLOCK_PILOT_LENGTH; i++)
94      p = lviv_output_bit (p, 1);
95
96   data_size = length - ( LVIV_LVT_HEADER_PILOT_SAMPLES +
97                LVIV_LVT_HEADER_DATA_SAMPLES +
98                     LVIV_LVT_PAUSE_SAMPLES +
99                LVIV_LVT_BLOCK_PILOT_SAMPLES );
100   data_size/=660;
101
102   for (i=0; i<data_size; i++)
103      p = lviv_output_byte (p, bytes[0x10+i]);
104
105   return p - buffer;
106}
107
108
109
110static const struct CassetteLegacyWaveFiller lviv_legacy_fill_wave =
111{
112   lviv_cassette_fill_wave,               /* fill_wave */
113   -1,                                 /* chunk_size */
114   0,                                 /* chunk_samples */
115   lviv_cassette_calculate_size_in_samples,   /* chunk_sample_calc */
116   44100,                              /* sample_frequency */
117   0,                                 /* header_samples */
118   0                                 /* trailer_samples */
119};
120
121
122
123static casserr_t lviv_lvt_identify(cassette_image *cassette, struct CassetteOptions *opts)
124{
125   return cassette_legacy_identify(cassette, opts, &lviv_legacy_fill_wave);
126}
127
128
129
130static casserr_t lviv_lvt_load(cassette_image *cassette)
131{
132   return cassette_legacy_construct(cassette, &lviv_legacy_fill_wave);
133}
134
135
136
137static const struct CassetteFormat lviv_lvt_image_format =
138{
139   "lvt",
140   lviv_lvt_identify,
141   lviv_lvt_load,
142   NULL
143};
144
145
146
147CASSETTE_FORMATLIST_START(lviv_lvt_format)
148   CASSETTE_FORMAT(lviv_lvt_image_format)
149CASSETTE_FORMATLIST_END
src/mess/formats/g64_dsk.h
r11457r11458
1/*********************************************************************
2
3    formats/g64_dsk.h
4
5    Floppy format code for Commodore 1541 GCR disk images
6
7*********************************************************************/
8
9#ifndef __G64_DSK__
10#define __G64_DSK__
11
12#include "imagedev/flopimg.h"
13
14/***************************************************************************
15    MACROS / CONSTANTS
16***************************************************************************/
17
18#define G64_SYNC_MARK      0x3ff      /* 10 consecutive 1-bits */
19
20#define G64_BUFFER_SIZE      16384
21
22const int C2040_BITRATE[] =
23{
24   XTAL_16MHz/16,   /* tracks  1-17 */
25   XTAL_16MHz/15,   /* tracks 18-24 */
26   XTAL_16MHz/14,   /* tracks 25-30 */
27   XTAL_16MHz/13   /* tracks 31-42 */
28};
29
30const int C8050_BITRATE[] =
31{
32   XTAL_12MHz/2/16,   /* tracks  1-39 */
33   XTAL_12MHz/2/15,   /* tracks 40-53 */
34   XTAL_12MHz/2/14,   /* tracks 54-65 */
35   XTAL_12MHz/2/13      /* tracks 65-84 */
36};
37
38/***************************************************************************
39    PROTOTYPES
40***************************************************************************/
41
42FLOPPY_IDENTIFY( g64_dsk_identify );
43FLOPPY_CONSTRUCT( g64_dsk_construct );
44
45#endif
src/mess/formats/sorc_dsk.c
r11457r11458
1/*********************************************************************
2
3    formats/exi_dsk.c
4
5    Exidy Sorcerer floppy-disk images
6
7*********************************************************************/
8
9#include <string.h>
10
11#include "formats/sorc_dsk.h"
12#include "formats/basicdsk.h"
13#include "emu.h"
14
15static FLOPPY_IDENTIFY(exi_dsk_identify)
16{
17   *vote = (floppy_image_size(floppy) == 332640) ? 100 : 0;
18   return FLOPPY_ERROR_SUCCESS;
19}
20
21static int exi_get_heads_per_disk(floppy_image *floppy)
22{
23   return 1;
24}
25
26static int exi_get_tracks_per_disk(floppy_image *floppy)
27{
28   return 77;
29}
30
31static UINT64 exi_translate_offset(floppy_image *floppy, int track, int head, int sector)
32{
33   return 13+270*(16*track+sector);
34}
35
36static floperr_t get_offset(floppy_image *floppy, int head, int track, int sector, int sector_is_index, UINT64 *offset)
37{
38   UINT64 offs;
39   /* translate the sector to a raw sector */
40   if (!sector_is_index)
41   {
42      sector -= 1;
43   }
44   /* check to see if we are out of range */
45   if ((head != 0) || (track < 0) || (track >= 77) || (sector < 0) || (sector >= 16))
46      return FLOPPY_ERROR_SEEKERROR;
47
48   offs = exi_translate_offset(floppy, track, head, sector);
49   if (offset)
50      *offset = offs;
51   return FLOPPY_ERROR_SUCCESS;
52}
53
54
55
56static floperr_t internal_exi_read_sector(floppy_image *floppy, int head, int track, int sector, int sector_is_index, void *buffer, size_t buflen)
57{
58   UINT64 offset;
59   floperr_t err;
60   err = get_offset(floppy, head, track, sector, sector_is_index, &offset);
61   if (err)
62      return err;
63
64   floppy_image_read(floppy, buffer, offset, buflen);
65   return FLOPPY_ERROR_SUCCESS;
66}
67
68
69
70static floperr_t internal_exi_write_sector(floppy_image *floppy, int head, int track, int sector, int sector_is_index, const void *buffer, size_t buflen, int ddam)
71{
72   UINT64 offset;
73   floperr_t err;
74
75   err = get_offset(floppy, head, track, sector, sector_is_index, &offset);
76   if (err)
77      return err;
78
79   floppy_image_write(floppy, buffer, offset, buflen);
80   return FLOPPY_ERROR_SUCCESS;
81}
82
83
84
85static floperr_t exi_read_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen)
86{
87   return internal_exi_read_sector(floppy, head, track, sector, FALSE, buffer, buflen);
88}
89
90static floperr_t exi_write_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam)
91{
92   return internal_exi_write_sector(floppy, head, track, sector, FALSE, buffer, buflen, ddam);
93}
94
95static floperr_t exi_read_indexed_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen)
96{
97   return internal_exi_read_sector(floppy, head, track, sector, TRUE, buffer, buflen);
98}
99
100static floperr_t exi_write_indexed_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam)
101{
102   return internal_exi_write_sector(floppy, head, track, sector, TRUE, buffer, buflen, ddam);
103}
104
105static floperr_t exi_get_sector_length(floppy_image *floppy, int head, int track, int sector, UINT32 *sector_length)
106{
107   floperr_t err;
108   err = get_offset(floppy, head, track, sector, FALSE, NULL);
109   if (err)
110      return err;
111
112   if (sector_length) {
113      *sector_length = 256;
114   }
115   return FLOPPY_ERROR_SUCCESS;
116}
117
118
119
120static floperr_t exi_get_indexed_sector_info(floppy_image *floppy, int head, int track, int sector_index, int *cylinder, int *side, int *sector, UINT32 *sector_length, unsigned long *flags)
121{
122   sector_index += 1;
123   if (cylinder)
124      *cylinder = track;
125   if (side)
126      *side = head;
127   if (sector)
128      *sector = sector_index;
129   if (flags)
130      /* TODO: read DAM or DDAM and determine flags */
131      *flags = 0;
132   return exi_get_sector_length(floppy, head, track, sector_index, sector_length);
133}
134
135
136static FLOPPY_CONSTRUCT(exi_dsk_construct)
137{
138   struct FloppyCallbacks *callbacks;
139   callbacks = floppy_callbacks(floppy);
140   callbacks->read_sector = exi_read_sector;
141   callbacks->write_sector = exi_write_sector;
142   callbacks->read_indexed_sector = exi_read_indexed_sector;
143   callbacks->write_indexed_sector = exi_write_indexed_sector;
144   callbacks->get_sector_length = exi_get_sector_length;
145   callbacks->get_heads_per_disk = exi_get_heads_per_disk;
146   callbacks->get_tracks_per_disk = exi_get_tracks_per_disk;
147   callbacks->get_indexed_sector_info = exi_get_indexed_sector_info;
148
149   return FLOPPY_ERROR_SUCCESS;
150}
151
152
153
154/* ----------------------------------------------------------------------- */
155
156FLOPPY_OPTIONS_START( exidy )
157   FLOPPY_OPTION( exi_dsk, "dsk", "Exidy Sorcerer floppy disk image", exi_dsk_identify, exi_dsk_construct, NULL, NULL)
158FLOPPY_OPTIONS_END
src/mess/formats/thom_dsk.c
r11457r11458
1/*********************************************************************
2
3    formats/thom_dsk.c
4
5    Thomson disk images
6
7    Based on work of  Antoine Mine'
8
9*********************************************************************/
10
11#include <string.h>
12#include "emu.h"
13#include "formats/thom_dsk.h"
14#include "formats/basicdsk.h"
15
16static const int sap_magic_num = 0xB3; /* simple XOR crypt */
17
18
19static const char sap_header[] =
20   "\001SYSTEME D'ARCHIVAGE PUKALL S.A.P. "
21   "(c) Alexandre PUKALL Avril 1998";
22
23
24static const UINT16 sap_crc[] =
25{
26   0x0000, 0x1081, 0x2102, 0x3183,   0x4204, 0x5285, 0x6306, 0x7387,
27   0x8408, 0x9489, 0xa50a, 0xb58b,   0xc60c, 0xd68d, 0xe70e, 0xf78f,
28};
29
30struct sap_dsk_tag
31{
32   int tracks;
33   int sector_size;
34        int sector_pos[80][16]; /* remember sector position in file */
35};
36
37static UINT16 thom_sap_crc( UINT8* data, int size )
38{
39        int i;
40        UINT16 crc = 0xffff, crc2;
41        for ( i = 0; i < size; i++ )
42        {
43                crc2 = ( crc >> 4 ) ^ sap_crc[ ( crc ^ data[i] ) & 15 ];
44                crc = ( crc2 >> 4 ) ^ sap_crc[ ( crc2 ^ (data[i] >> 4) ) & 15 ];
45        }
46        return crc;
47}
48
49static struct sap_dsk_tag *get_tag(floppy_image *floppy)
50{
51   struct sap_dsk_tag *tag;
52   tag = (sap_dsk_tag *)floppy_tag(floppy);
53   return tag;
54}
55
56
57static FLOPPY_IDENTIFY(sap_dsk_identify)
58{
59   char header[0x100];
60   floppy_image_read(floppy, header, 0, sizeof(sap_header));
61   if (!memcmp( header, sap_header, sizeof(sap_header) ) )
62   {
63      *vote= 100;
64   } else {
65      *vote = 0;
66   }
67   return FLOPPY_ERROR_SUCCESS;
68}
69static int sap_get_heads_per_disk(floppy_image *floppy)
70{
71   return 1;
72}
73
74static int sap_get_tracks_per_disk(floppy_image *floppy)
75{
76   return get_tag(floppy)->tracks;
77}
78
79
80static floperr_t get_offset(floppy_image *floppy, int head, int track, int sector, int sector_is_index, UINT64 *offset)
81{
82   UINT64 offs;
83        struct sap_dsk_tag *tag = get_tag(floppy);
84   /* translate the sector to a raw sector */
85   if (!sector_is_index)
86   {
87      sector -= 1;
88   }
89   /* check to see if we are out of range */
90   if ((head < 0) || (head >= 1) || (track < 0) || (track >=tag->tracks)
91         || (sector < 0) || (sector >= 16))
92      return FLOPPY_ERROR_SEEKERROR;
93
94        offs = tag->sector_pos[track][sector];
95       if (offs <= 0 )
96                return FLOPPY_ERROR_SEEKERROR;
97
98   if (offset)
99      *offset = offs;
100   return FLOPPY_ERROR_SUCCESS;
101}
102
103
104
105static floperr_t internal_sap_read_sector(floppy_image *floppy, int head, int track, int sector, int sector_is_index, void *buffer, size_t buflen)
106{
107   UINT64 offset;
108   floperr_t err;
109   int i;
110   UINT8 *buf;
111   err = get_offset(floppy, head, track, sector, sector_is_index, &offset);
112   if (err)
113      return err;
114
115   floppy_image_read(floppy, buffer, offset+4, buflen);
116   buf = (UINT8*)buffer;
117   for (i=0;i<buflen;i++) {
118      buf[i] ^= sap_magic_num;
119   }
120   return FLOPPY_ERROR_SUCCESS;
121}
122
123
124
125static floperr_t internal_sap_write_sector(floppy_image *floppy, int head, int track, int sector, int sector_is_index, const void *buffer, size_t buflen, int ddam)
126{
127   UINT64 offset;
128   floperr_t err;
129   UINT8 buf[256+6];
130        UINT16 crc;
131   int i;
132   err = get_offset(floppy, head, track, sector, sector_is_index, &offset);
133   if (err)
134      return err;
135
136        /* buf = 4-byte header + sector + 2-byte CRC */
137   floppy_image_read(floppy, buf, offset, 4);
138   for (i=0;i<buflen;i++) {
139      buf[i+4] = ((UINT8*)buffer)[i];
140   }
141        crc = thom_sap_crc( buf, buflen+4 );
142        buf[buflen+4] = crc >> 8;
143        buf[buflen+5] = crc & 0xff;
144   for (i=0;i<buflen;i++) {
145      buf[i+4] ^= sap_magic_num;
146   }
147   floppy_image_write(floppy, buf, offset, buflen+6);
148
149   return FLOPPY_ERROR_SUCCESS;
150}
151
152
153static floperr_t sap_read_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen)
154{
155   return internal_sap_read_sector(floppy, head, track, sector, FALSE, buffer, buflen);
156}
157
158static floperr_t sap_write_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam)
159{
160   return internal_sap_write_sector(floppy, head, track, sector, FALSE, buffer, buflen, ddam);
161}
162
163static floperr_t sap_read_indexed_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen)
164{
165   return internal_sap_read_sector(floppy, head, track, sector, TRUE, buffer, buflen);
166}
167
168static floperr_t sap_write_indexed_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam)
169{
170   return internal_sap_write_sector(floppy, head, track, sector, TRUE, buffer, buflen, ddam);
171}
172
173static floperr_t sap_get_sector_length(floppy_image *floppy, int head, int track, int sector, UINT32 *sector_length)
174{
175   floperr_t err;
176   err = get_offset(floppy, head, track, sector, FALSE, NULL);
177   if (err)
178      return err;
179
180   if (sector_length) {
181      *sector_length = get_tag(floppy)->sector_size;
182   }
183   return FLOPPY_ERROR_SUCCESS;
184}
185
186
187
188static floperr_t sap_get_indexed_sector_info(floppy_image *floppy, int head, int track, int sector_index, int *cylinder, int *side, int *sector, UINT32 *sector_length, unsigned long *flags)
189{
190   floperr_t err;
191   UINT8 header[4];
192   UINT64 offset = 0;
193   sector_index += 1;
194   err = get_offset(floppy, head, track, sector_index, FALSE, &offset);
195
196   floppy_image_read(floppy, header, offset, 4);
197   if (cylinder)
198      *cylinder = header[2];
199   if (side)
200      *side = head;
201   if (sector)
202      *sector = header[3];
203   if (sector_length)
204      *sector_length = get_tag(floppy)->sector_size;
205   if (flags)
206      /* TODO: read DAM or DDAM and determine flags */
207      *flags = 0;
208   return err;
209}
210
211static floperr_t sap_post_format(floppy_image *floppy, option_resolution *params)
212{
213        int track,sector;
214        int pos;
215        UINT8 buf[256], header[4];
216   struct sap_dsk_tag *tag;
217        tag = (struct sap_dsk_tag *) floppy_create_tag(floppy, sizeof(struct sap_dsk_tag));
218
219        /* default options */
220        if ( !tag->tracks )
221        {
222                tag->tracks = 80;
223                tag->sector_size = 256;
224        }
225
226        /* create SAP file header */
227        floppy_image_write( floppy, sap_header, 0, 66 );
228
229        for ( track = 0; track < 80; track++ )
230                for ( sector = 0; sector < 16; sector++ )
231                        tag->sector_pos[track][sector] = 0;
232
233        /* create all sectors with valid header and CRC */
234   memset(buf, 0xe5, 256);
235        pos = 0x42;
236        header[0] = (tag->sector_size==128) ? 1 : 0;
237        header[1] = 0;
238        for ( track = 0, pos = 0x42; track < tag->tracks; track++ )
239                for ( sector = 0; sector < 16; sector++, pos += tag->sector_size + 6 ) {
240                        tag->sector_pos[track][sector] = pos;
241                        header[2] = track;
242                        header[3] = sector + 1;
243                        floppy_image_write(floppy, header, pos, 4);
244                        sap_write_indexed_sector( floppy, 0, track, sector, buf, tag->sector_size, 0 );
245                }
246
247        return FLOPPY_ERROR_SUCCESS;
248}
249
250
251static FLOPPY_CONSTRUCT(sap_dsk_construct)
252{
253   struct FloppyCallbacks *callbacks;
254   struct sap_dsk_tag *tag;
255   int i,j;
256   UINT8 fmt;
257   tag = (struct sap_dsk_tag *) floppy_create_tag(floppy, sizeof(struct sap_dsk_tag));
258   if (!tag)
259      return FLOPPY_ERROR_OUTOFMEMORY;
260
261        /* guess format */
262   floppy_image_read(floppy, &fmt, 0x42, 1);
263   if ( fmt==1 ) tag->sector_size = 128; else tag->sector_size = 256;
264
265        /* start with an empty offset table */
266        tag->tracks = 0;
267        for ( i = 0; i < 80; i++ )
268                for ( j = 0; j < 16; j++ )
269                        tag->sector_pos[i][j] = 0;
270
271        /* count tracks & fill sector offset table */
272   for ( i = 0x42; i+4 < floppy_image_size(floppy); i += tag->sector_size + 6 ) // CRC 2 bytes + 4 bytes sector header
273   {
274                UINT8 sector, track;
275      floppy_image_read(floppy, &track, i+2, 1);
276      floppy_image_read(floppy, &sector, i+3, 1);
277                if ( track >= 80 || sector < 1 || sector > 16 ) continue;
278                if ( track > tag->tracks ) tag->tracks = track+1;
279                tag->sector_pos[track][sector-1] = i;
280   }
281   callbacks = floppy_callbacks(floppy);
282   callbacks->read_sector = sap_read_sector;
283   callbacks->write_sector = sap_write_sector;
284   callbacks->read_indexed_sector = sap_read_indexed_sector;
285   callbacks->write_indexed_sector = sap_write_indexed_sector;
286   callbacks->get_sector_length = sap_get_sector_length;
287   callbacks->get_heads_per_disk = sap_get_heads_per_disk;
288   callbacks->get_tracks_per_disk = sap_get_tracks_per_disk;
289   callbacks->get_indexed_sector_info = sap_get_indexed_sector_info;
290   callbacks->post_format = sap_post_format;
291
292   return FLOPPY_ERROR_SUCCESS;
293}
294
295static FLOPPY_IDENTIFY(qdd_dsk_identify)
296{
297   *vote = (floppy_image_size(floppy) == (51200)) ? 100 : 0;
298   return FLOPPY_ERROR_SUCCESS;
299}
300
301/* fixed interlacing map for QDDs */
302static int thom_qdd_map[400];
303
304static int qdd_translate_sector(floppy_image *floppy, int sector)
305{
306   return thom_qdd_map[sector-1];
307}
308
309static void thom_qdd_compute_map ( void )
310{
311   /* this map is hardcoded in the QDD BIOS */
312   static const int p[6][4] =
313   {
314      { 20,  2, 14,  8 }, { 21, 19, 13,  7 },
315      { 22, 18, 12,  6 }, { 23, 17, 11,  5 },
316      { 24, 16, 10,  4 }, {  1, 15,  9,  3 }
317   };
318   static const int q[4] = { 0, 8, 4, 12 };
319   int t, s;
320   for ( t = 0; t < 24; t++ )
321   {
322      for ( s = 0; s < 16; s++ )
323      {
324         thom_qdd_map[ t*16 + s ] = p[ t/4 ][ s%4 ] * 16 + (s/4) + 4*(t%4);
325      }
326   }
327   for ( s = 0; s < 16; s++ )
328   {
329      thom_qdd_map[ 24*16 + s ] = q[ s%4 ] + (s/4);
330   }
331}
332
333static FLOPPY_CONSTRUCT(qdd_dsk_construct)
334{
335   struct basicdsk_geometry geometry;
336
337   thom_qdd_compute_map();
338
339   memset(&geometry, 0, sizeof(geometry));
340   geometry.heads = 1;
341   geometry.first_sector_id = 1;
342   geometry.sector_length = 128;
343   geometry.tracks = 1;
344   geometry.sectors = 400;
345   geometry.translate_sector =  qdd_translate_sector;
346   return basicdsk_construct(floppy, &geometry);
347}
348
349
350/* ----------------------------------------------------------------------- */
351
352FLOPPY_OPTIONS_START(thomson)
353
354   FLOPPY_OPTION(fdmfm2, "fd", "Thomson FD (MFM) 80 tracks disk image (3\"1/2 DD)", basicdsk_identify_default, basicdsk_construct_default, NULL,
355      HEADS([1])
356      TRACKS([80])
357      SECTORS([16])
358      SECTOR_LENGTH([256])
359      FIRST_SECTOR_ID([1]))
360
361   FLOPPY_OPTION(fdmfm, "fd", "Thomson FD (MFM) 40 tracks disk image (5\"1/4 DD)", basicdsk_identify_default, basicdsk_construct_default, NULL,
362      HEADS([1])
363      TRACKS([40])
364      SECTORS([16])
365      SECTOR_LENGTH([256])
366      FIRST_SECTOR_ID([1]))
367
368   FLOPPY_OPTION(fd2, "fd", "Thomson FD (FM) 80 tracks disk image (3\"1/2 SD)", basicdsk_identify_default, basicdsk_construct_default, NULL,
369      HEADS([1])
370      TRACKS([80])
371      SECTORS([16])
372      SECTOR_LENGTH([128])
373      FIRST_SECTOR_ID([1]))
374
375   FLOPPY_OPTION(fd, "fd", "Thomson FD (FM) 40 tracks disk image (5\"1/4 SD)", basicdsk_identify_default, basicdsk_construct_default, NULL,
376      HEADS([1])
377      TRACKS([40])
378      SECTORS([16])
379      SECTOR_LENGTH([128])
380      FIRST_SECTOR_ID([1]))
381
382        FLOPPY_OPTION(sap,"sap", "Thomson SAP floppy disk image",   sap_dsk_identify, sap_dsk_construct, NULL, NULL)
383
384   FLOPPY_OPTION(qdd,"qd", "Thomson QDD floppy disk image (2\"8 SD)",   qdd_dsk_identify, qdd_dsk_construct, NULL,
385      HEADS([1])
386      TRACKS([1])
387      SECTORS([400])
388      SECTOR_LENGTH([128])
389      FIRST_SECTOR_ID([0]))
390
391FLOPPY_OPTIONS_END
392
src/mess/formats/pc_dsk.h
r11457r11458
1/*********************************************************************
2
3    formats/pc_dsk.h
4
5    PC disk images
6
7*********************************************************************/
8
9#ifndef PC_DSK_H
10#define PC_DSK_H
11
12#include "imagedev/flopimg.h"
13
14
15/**************************************************************************/
16
17FLOPPY_OPTIONS_EXTERN(pc);
18
19#endif /* PC_DSK_H */
src/mess/formats/atarist_dsk.c
r11457r11458
1/*********************************************************************
2
3    formats/atarist_dsk.c
4
5    Atari ST disk images
6
7*********************************************************************/
8
9#include "formats/atarist_dsk.h"
10#include "formats/basicdsk.h"
11
12/*
13
14    TODO:
15
16    - MSA format
17    - STT format
18    - DIM format
19
20*/
21
22/***************************************************************************
23    CONSTANTS / MACROS
24***************************************************************************/
25
26#define LOG 0
27
28/***************************************************************************
29    IMPLEMENTATION
30***************************************************************************/
31
32/*-------------------------------------------------
33    FLOPPY_IDENTIFY( atarist_st_identify )
34-------------------------------------------------*/
35
36static FLOPPY_IDENTIFY( atarist_st_identify )
37{
38   *vote = 100;
39
40   return FLOPPY_ERROR_SUCCESS;
41}
42
43/*-------------------------------------------------
44    FLOPPY_CONSTRUCT( atarist_st_construct )
45-------------------------------------------------*/
46
47static FLOPPY_CONSTRUCT( atarist_st_construct )
48{
49   int heads = 0;
50   int tracks = 0;
51   int sectors = 0;
52   UINT8 bootsector[512];
53
54   floppy_image_read(floppy, bootsector, 0, 512);
55   sectors = bootsector[0x18];
56   heads = bootsector[0x1a];
57   tracks = (bootsector[0x13] | (bootsector[0x14] << 8)) / sectors / heads;
58
59   struct basicdsk_geometry geometry;
60   memset(&geometry, 0, sizeof(geometry));
61
62   geometry.heads = heads;
63   geometry.first_sector_id = 1;
64   geometry.sector_length = 512;
65   geometry.tracks = tracks;
66   geometry.sectors = sectors;
67
68   if (LOG) logerror("ST Heads %u Tracks %u Sectors %u\n", heads, tracks, sectors);
69
70   return basicdsk_construct(floppy, &geometry);
71}
72
73/*-------------------------------------------------
74    FLOPPY_CONSTRUCT(atarist_dsk_construct)
75-------------------------------------------------*/
76
77FLOPPY_OPTIONS_START( atarist )
78   FLOPPY_OPTION( atarist, "st", "Atari ST floppy disk image", atarist_st_identify, atarist_st_construct, NULL, NULL )
79/*  FLOPPY_OPTION( atarist, "stt", "Atari ST floppy disk image", atarist_stt_identify, atarist_stt_construct, NULL, NULL )
80    FLOPPY_OPTION( atarist, "msa", "Atari ST floppy disk image", atarist_msa_identify, atarist_msa_construct, NULL, NULL )
81    FLOPPY_OPTION( atarist, "dim", "Atari ST floppy disk image", atarist_dim_identify, atarist_dim_construct, NULL, NULL )*/
82FLOPPY_OPTIONS_END
src/mess/formats/primoptp.c
r11457r11458
1/* .PTP Microkey Primo tape images */
2
3
4#include "emu.h"
5#include "primoptp.h"
6
7
8#define PRIMO_WAVEENTRY_LOW      -32768
9#define PRIMO_WAVEENTRY_HIGH      32767
10#define PRIMO_WAVEENTRY_ZERO      0
11
12#define PRIMO_WAV_FREQUENCY      22050
13#define PRIMO_BIT_1_PERIOD      (312*2*0.000001)
14#define PRIMO_BIT_0_PERIOD              (3*PRIMO_BIT_1_PERIOD)
15
16#define PRIMO_BIT_1_LENGTH      (PRIMO_BIT_1_PERIOD*PRIMO_WAV_FREQUENCY)
17#define PRIMO_BIT_0_LENGTH      (PRIMO_BIT_0_PERIOD*PRIMO_WAV_FREQUENCY)
18/*
19#define PRIMO_BIT_1_LENGTH      6
20#define PRIMO_BIT_0_LENGTH      16
21*/
22#define PRIMO_PAUSE_LENGTH      2000
23#define PRIMO_FILE_PILOT_LENGTH      ((4*PRIMO_BIT_1_LENGTH + 4*PRIMO_BIT_0_LENGTH)*512)
24#define PRIMO_BLOCK_PILOT_LENGTH   ((8*PRIMO_BIT_1_LENGTH)*96 + (5*PRIMO_BIT_1_LENGTH + 3*PRIMO_BIT_0_LENGTH)*3)
25
26static UINT32 primo_tape_image_length;
27
28static INT16 *primo_emit_level(INT16 *p, int count, int level)
29{
30   int i;
31
32   for (i=0; i<count; i++)   *(p++) = level;
33
34   return p;
35}
36
37static INT16* primo_output_bit(INT16 *p, UINT8 bit)
38{
39   if (bit)
40   {
41      p = primo_emit_level (p, PRIMO_BIT_1_LENGTH/2, PRIMO_WAVEENTRY_HIGH);
42      p = primo_emit_level (p, PRIMO_BIT_1_LENGTH/2, PRIMO_WAVEENTRY_LOW);
43   }
44   else
45   {
46      p = primo_emit_level (p, PRIMO_BIT_0_LENGTH/2, PRIMO_WAVEENTRY_HIGH);
47      p = primo_emit_level (p, PRIMO_BIT_0_LENGTH/2, PRIMO_WAVEENTRY_LOW);
48   }
49       return p;
50}
51
52static INT16* primo_output_byte(INT16 *p, UINT8 byte)
53{
54   int i;
55
56   for (i=0; i<8; i++)
57      p = primo_output_bit(p,(byte>>(7-i)) & 0x01);
58
59   return p;
60}
61
62static UINT32 primo_cassette_calculate_number_of_1(const UINT8 *bytes, UINT16 length)
63{
64   int i,j;
65
66   UINT32 number_of_1 = 0;
67
68   for (i=0; i<length; i++)
69      for (j=0; j<8; j++)
70         if ((bytes[i]>>j)&0x01)
71            number_of_1++;
72
73   return number_of_1;
74}
75
76static int primo_cassette_calculate_size_in_samples(const UINT8 *bytes, int length)
77{
78   int i = 0, j = 0;
79
80   UINT8 *b = (UINT8*) bytes;
81
82   UINT32 file_size = 0;
83   UINT16 block_size = 0;
84
85   UINT32 number_of_1 = 0;
86   UINT32 number_of_0 = 0;
87   UINT32 size_in_samples = 0;
88
89   primo_tape_image_length = length;
90
91   while (i < length)
92   {
93      size_in_samples += PRIMO_PAUSE_LENGTH;
94      logerror ("Samples (pause): %d\n", size_in_samples);
95
96      size_in_samples += PRIMO_FILE_PILOT_LENGTH;
97      logerror ("Samples (file pilot): %d\n", size_in_samples);
98
99      /* file size with header */
100      file_size = *(b+1) + *(b+2)*256;
101      b += 3;
102      logerror ("File size (with header): %d\n", file_size);
103
104      /* b is now set on the first data byte of file
105           it means first byte (type) of block */
106
107      j = 0;
108      while (j < file_size-3)
109      {
110         size_in_samples += PRIMO_BLOCK_PILOT_LENGTH;
111         logerror ("Samples (block pilot): %d\n", size_in_samples);
112
113         /* block size without header but including CRC byte */
114         block_size = *(b+1) + *(b+2)*256;
115
116         /* b is set on the first byte of block data */
117         b += 3;
118
119         number_of_1 = primo_cassette_calculate_number_of_1(b, block_size);
120         number_of_0 = (block_size)*8-number_of_1;
121
122         size_in_samples += number_of_1 * PRIMO_BIT_1_LENGTH;
123         size_in_samples += number_of_0 * PRIMO_BIT_0_LENGTH;
124
125         logerror ("Samples (block data): %d\n", size_in_samples);
126
127         logerror ("\tBlock size: %d\n", block_size);
128
129         b += block_size;
130
131         /* b is set on the first header byte of next block or file */
132
133         j += block_size+3;
134      }
135
136      i += file_size;
137   }
138
139   return size_in_samples;
140}
141
142static int primo_cassette_fill_wave(INT16 *buffer, int length, UINT8 *bytes)
143{
144
145   int i = 0, j = 0, k;
146
147   INT16 *p = buffer;
148   UINT8 *b = bytes;
149
150   UINT32 file_size = 0;
151   UINT16 block_size = 0;
152
153   logerror ("Image size: %d\n", length);
154
155   while (i < primo_tape_image_length)
156   {
157      logerror ("Beginning Primo file\n");
158      /* pause */
159      p = primo_emit_level (p, PRIMO_PAUSE_LENGTH, PRIMO_WAVEENTRY_ZERO);
160
161      logerror ("Samples (pause): %ld\n", (long int)(p-buffer));
162
163      /* file pilot */
164      for (k=0; k<512; k++)
165         p = primo_output_byte (p, 0xaa);
166
167      logerror ("Samples (file pilot): %ld\n", (long int)(p-buffer));
168
169      /* file size with header */
170      file_size = *(b+1) + *(b+2)*256;
171      b += 3;
172
173      logerror ("File size: %d\n", file_size);
174
175      /* b is now set on the first data byte of file
176           it means first byte (block type) of block header */
177
178      j = 0;
179      while (j < file_size-3)
180      {
181
182         /* block pilot */
183         for (k=0; k<96; k++)
184            p = primo_output_byte (p, 0xff);
185         for (k=0; k<3; k++)
186            p = primo_output_byte (p, 0xd3);
187
188         logerror ("Samples (block pilot): %ld\n", (long int)(p-buffer));
189
190         /* block size without header but including CRC byte */
191         block_size = *(b+1) + *(b+2)*256;
192         b += 3;
193
194         for (k=0; k<block_size; k++)
195            p = primo_output_byte (p, *(b+k));
196
197         logerror ("Samples (block data): %ld\n", (long int)(p-buffer));
198
199         b += block_size;
200
201         /* b is now set on the first header byte of next block or file */
202
203         j += block_size+3;
204      }
205
206      logerror ("Primo file finished\n");
207      logerror ("i = %d\n", i);
208      i += file_size;
209      logerror ("i = %d\n", i);
210   }
211
212   logerror ("End of fill_wave/n");
213
214   return p - buffer;
215}
216
217static const struct CassetteLegacyWaveFiller primo_legacy_fill_wave =
218{
219   primo_cassette_fill_wave,         /* fill_wave */
220   -1,                                 /* chunk_size */
221   0,                                 /* chunk_samples */
222   primo_cassette_calculate_size_in_samples,   /* chunk_sample_calc */
223   PRIMO_WAV_FREQUENCY,                              /* sample_frequency */
224   0,                                 /* header_samples */
225   0                                 /* trailer_samples */
226};
227
228static casserr_t primo_ptp_identify(cassette_image *cassette, struct CassetteOptions *opts)
229{
230   return cassette_legacy_identify(cassette, opts, &primo_legacy_fill_wave);
231}
232
233static casserr_t primo_ptp_load(cassette_image *cassette)
234{
235   return cassette_legacy_construct(cassette, &primo_legacy_fill_wave);
236}
237
238static const struct CassetteFormat primo_ptp_image_format =
239{
240   "ptp",
241   primo_ptp_identify,
242   primo_ptp_load,
243   NULL
244};
245
246CASSETTE_FORMATLIST_START(primo_ptp_format)
247   CASSETTE_FORMAT(primo_ptp_image_format)
248CASSETTE_FORMATLIST_END
src/mess/formats/lviv_lvt.h
r11457r11458
1/*********************************************************************
2
3    lviv_lvt.h
4
5    Format code for Lviv casette files
6
7*********************************************************************/
8
9#ifndef LVIV_LVT_H
10#define LVIV_LVT_H
11
12#include "imagedev/cassimg.h"
13
14CASSETTE_FORMATLIST_EXTERN(lviv_lvt_format);
15
16#endif /* LVIV_LVT_H */
src/mess/formats/sorc_dsk.h
r11457r11458
1/*********************************************************************
2
3    formats/sorc_dsk.h
4
5    Exidy Sorcerer disk images
6
7*********************************************************************/
8
9#ifndef EXI_DSK_H
10#define EXI_DSK_H
11
12#include "imagedev/flopimg.h"
13
14/**************************************************************************/
15
16FLOPPY_OPTIONS_EXTERN(exidy);
17
18#endif /* EXI_DSK_H */
src/mess/formats/thom_dsk.h
r11457r11458
1#ifndef THOM_DSK
2#define THOM_DSK
3
4#include "imagedev/flopdrv.h"
5
6/* recognized image formats:
7   - .fd    one-side 5"1/4 (single and double density), 3"1/2
8   - .qd    one-side QDD
9   - .sap   one-side double-density 5"1/4, 3"1/2
10*/
11
12FLOPPY_OPTIONS_EXTERN(thomson);
13
14#endif
src/mess/formats/atarist_dsk.h
r11457r11458
1/*********************************************************************
2
3    formats/atarist_dsk.h
4
5    atarist disk images
6
7*********************************************************************/
8
9#ifndef ATARIST_DSK_H
10#define ATARIST_DSK_H
11
12#include "imagedev/flopimg.h"
13
14/**************************************************************************/
15
16FLOPPY_OPTIONS_EXTERN(atarist);
17
18#endif /* ATARIST_DSK_H */
src/mess/formats/primoptp.h
r11457r11458
1/*********************************************************************
2
3    primoptp.h
4
5    Format code for Primo casette files
6
7*********************************************************************/
8
9#ifndef PRIMOPTP_H
10#define PRIMOPTP_H
11
12#include "imagedev/cassimg.h"
13
14CASSETTE_FORMATLIST_EXTERN( primo_ptp_format );
15
16#endif /* PRIMOPTP_H */
src/mess/formats/dsk_dsk.c
r11457r11458
1/*********************************************************************
2
3    formats/dsk_dsk.c
4
5    DSK disk images
6
7*********************************************************************/
8
9#include <string.h>
10
11#include "emu.h"
12#include "imageutl.h"
13#include "imagedev/flopimg.h"
14#include "imagedev/flopdrv.h"
15
16#define MV_CPC     "MV - CPC"
17#define EXTENDED   "EXTENDED"
18
19struct dskdsk_tag
20{
21   int disk_image_type;  /* image type: standard or extended */
22   int heads;
23   int tracks;
24   int sector_size;
25   UINT64 track_offsets[84*2]; /* offset within data for each track */
26
27};
28
29
30static struct dskdsk_tag *get_tag(floppy_image *floppy)
31{
32   struct dskdsk_tag *tag;
33   tag = (dskdsk_tag *)floppy_tag(floppy);
34   return tag;
35}
36
37
38
39FLOPPY_IDENTIFY( dsk_dsk_identify )
40{
41   UINT8 header[8];
42
43   floppy_image_read(floppy, header, 0, 8);
44   if ( memcmp( header, MV_CPC, 8 ) ==0) {
45      *vote = 100;
46   } else
47   if ( memcmp( header, EXTENDED, 8 ) ==0) {
48      *vote = 100;
49   } else {
50      *vote = 0;
51   }
52   return FLOPPY_ERROR_SUCCESS;
53}
54
55static int dsk_get_heads_per_disk(floppy_image *floppy)
56{
57   return get_tag(floppy)->heads;
58}
59
60static int dsk_get_tracks_per_disk(floppy_image *floppy)
61{
62   return get_tag(floppy)->tracks;
63}
64
65static UINT64 dsk_get_track_offset(floppy_image *floppy, int head, int track)
66{
67   return get_tag(floppy)->track_offsets[(track<<1) + head];
68}
69
70static floperr_t get_offset(floppy_image *floppy, int head, int track, int sector, int sector_is_index, UINT64 *offset)
71{
72   UINT64 offs;
73   UINT64 track_offset;
74   UINT8 track_info[0x100];
75   UINT8 sectors_per_track;
76   int i;
77   /* translate the sector to a raw sector */
78
79   /* check to see if we are out of range */
80   if ((head < 0) || (head >= get_tag(floppy)->heads) || (track < 0) || (track >= get_tag(floppy)->tracks)
81         || (sector < 0) )
82      return FLOPPY_ERROR_SEEKERROR;
83
84   track_offset = dsk_get_track_offset(floppy, head, track);
85
86   floppy_image_read(floppy, track_info, track_offset, 0x100);
87
88   sectors_per_track = track_info[0x015];
89   if (!sector_is_index) {
90      if (sector >= sectors_per_track) {
91         return FLOPPY_ERROR_SEEKERROR;
92      }
93   }
94
95   if (get_tag(floppy)->disk_image_type==0) {
96      get_tag(floppy)->sector_size = (1<<(track_info[0x014]+7));
97      offs = track_offset + 0x100 +sector * get_tag(floppy)->sector_size;
98   } else {
99      get_tag(floppy)->sector_size = track_info[0x18 + (sector<<3) + 6] + (track_info[0x18+(sector<<3) + 7]<<8);
100      offs = track_offset + 0x100;
101      for (i=0;i<sector;i++) {
102         offs += track_info[0x18 + (i<<3) + 6] + (track_info[0x18+(i<<3) + 7]<<8);
103      }
104   }
105
106   if (offset)
107      *offset = offs;
108   return FLOPPY_ERROR_SUCCESS;
109}
110
111
112
113static floperr_t internal_dsk_read_sector(floppy_image *floppy, int head, int track, int sector, int sector_is_index, void *buffer, size_t buflen)
114{
115   UINT64 offset;
116   floperr_t err;
117   err = get_offset(floppy, head, track, sector, sector_is_index, &offset);
118   if (err)
119      return err;
120   floppy_image_read(floppy, buffer, offset, buflen);
121   return FLOPPY_ERROR_SUCCESS;
122}
123
124
125
126static floperr_t internal_dsk_write_sector(floppy_image *floppy, int head, int track, int sector, int sector_is_index, const void *buffer, size_t buflen, int ddam)
127{
128   UINT64 offset;
129   floperr_t err;
130
131   err = get_offset(floppy, head, track, sector, sector_is_index, &offset);
132   if (err)
133      return err;
134
135   floppy_image_write(floppy, buffer, offset, buflen);
136   return FLOPPY_ERROR_SUCCESS;
137}
138
139
140
141static floperr_t dsk_read_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen)
142{
143   return internal_dsk_read_sector(floppy, head, track, sector, FALSE, buffer, buflen);
144}
145
146static floperr_t dsk_write_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam)
147{
148   return internal_dsk_write_sector(floppy, head, track, sector, FALSE, buffer, buflen, ddam);
149}
150
151static floperr_t dsk_read_indexed_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen)
152{
153   return internal_dsk_read_sector(floppy, head, track, sector, TRUE, buffer, buflen);
154}
155
156static floperr_t dsk_write_indexed_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam)
157{
158   return internal_dsk_write_sector(floppy, head, track, sector, TRUE, buffer, buflen, ddam);
159}
160
161static floperr_t dsk_get_sector_length(floppy_image *floppy, int head, int track, int sector, UINT32 *sector_length)
162{
163   floperr_t err;
164   err = get_offset(floppy, head, track, sector, FALSE, NULL);
165   if (err)
166      return err;
167
168   if (sector_length) {
169      *sector_length = get_tag(floppy)->sector_size;
170   }
171   return FLOPPY_ERROR_SUCCESS;
172}
173
174static floperr_t dsk_get_indexed_sector_info(floppy_image *floppy, int head, int track, int sector_index, int *cylinder, int *side, int *sector, UINT32 *sector_length, unsigned long *flags)
175{
176   floperr_t retVal;
177   UINT64 offset;
178   UINT8 sector_info[0x100];
179   int pos;
180
181   retVal = get_offset(floppy, head, track, sector_index, FALSE, NULL);
182   offset = dsk_get_track_offset(floppy,head,track);
183   pos = 0x18 + (sector_index << 3);
184   floppy_image_read(floppy, sector_info, offset, 0x100);
185   if (cylinder)
186      *cylinder = sector_info[pos + 0];
187   if (side)
188      *side = sector_info[pos + 1];
189   if (sector)
190      *sector = sector_info[pos + 2];
191   if (sector_length) {
192      *sector_length = 1 << (sector_info[pos + 3] + 7);
193   }
194   if (flags)
195      *flags = (sector_info[pos + 5] & 0x40) ? ID_FLAG_DELETED_DATA : 0;
196   return retVal;
197}
198
199
200FLOPPY_CONSTRUCT( dsk_dsk_construct )
201{
202   struct FloppyCallbacks *callbacks;
203   struct dskdsk_tag *tag;
204   UINT8 header[0x100];
205   UINT64 tmp = 0;
206   int i;
207   int skip,cnt;
208
209   if(params)
210   {
211      // create
212      return FLOPPY_ERROR_UNSUPPORTED;
213   }
214
215   floppy_image_read(floppy, header, 0, 0x100);
216
217   tag = (struct dskdsk_tag *) floppy_create_tag(floppy, sizeof(struct dskdsk_tag));
218   if (!tag)
219      return FLOPPY_ERROR_OUTOFMEMORY;
220
221   tag->heads   = header[0x31];
222   if (tag->heads==1) {
223      skip = 2;
224   } else {
225      skip = 1;
226   }
227   tag->tracks  = header[0x30];
228   cnt =0;
229   if ( memcmp( header, MV_CPC, 8 ) ==0) {
230      tag->disk_image_type = 0;
231      tmp = 0x100;
232      for (i=0; i<tag->tracks * tag->heads; i++)
233      {
234         tag->track_offsets[cnt] = tmp;
235         tmp += pick_integer_le(header, 0x32, 2);
236         cnt += skip;
237      }
238   } else  {
239      tag->disk_image_type = 1;
240      tmp = 0x100;
241      for (i=0; i<tag->tracks * tag->heads; i++)
242      {
243         tag->track_offsets[cnt] = tmp;
244         tmp += header[0x34 + i] << 8;
245         cnt += skip;
246      }
247   }
248
249   callbacks = floppy_callbacks(floppy);
250   callbacks->read_sector = dsk_read_sector;
251   callbacks->write_sector = dsk_write_sector;
252   callbacks->read_indexed_sector = dsk_read_indexed_sector;
253   callbacks->write_indexed_sector = dsk_write_indexed_sector;
254   callbacks->get_sector_length = dsk_get_sector_length;
255   callbacks->get_heads_per_disk = dsk_get_heads_per_disk;
256   callbacks->get_tracks_per_disk = dsk_get_tracks_per_disk;
257   callbacks->get_indexed_sector_info = dsk_get_indexed_sector_info;
258   return FLOPPY_ERROR_SUCCESS;
259}
src/mess/formats/zx81_p.c
r11457r11458
1/*****************************************************************************
2
3Taken from nocash ZX81 docs by Martin Korth.
4
5ZX81 Cassette File Structure
6  5 seconds    pilot
7  1-127 bytes  name (bit7 set in last char)
8  LEN bytes    data, loaded to address 4009h, LEN=(4014h)-4009h.
9  1 pulse      video retrace signal (only if display was enabled)
10The data field contains the system area, the basic program, the video memory,
11and VARS area.
12
13ZX80 Cassette File Structure
14  5 seconds    pilot
15  LEN bytes    data, loaded to address 4000h, LEN=(400Ah)-4000h.
16ZX80 files do not have filenames, and video memory is not included in the file.
17
18Bits and Bytes
19Each byte consists of 8 bits (MSB first) without any start and stop bits,
20directly followed by the next byte. A "0" bit consists of four high pulses,
21a "1" bit of nine pulses, either one followed by a silence period.
22  0:  /\/\/\/\________
23  1:  /\/\/\/\/\/\/\/\/\________
24Each pulse is split into a 150us High period, and 150us Low period. The
25duration of the silence between each bit is 1300us. The baud rate is thus 400
26bps (for a "0" filled area) downto 250 bps (for a "1" filled area). Average
27medium transfer rate is approx. 307 bps (38 bytes/sec) for files that contain
2850% of "0" and "1" bits each.
29
30*****************************************************************************/
31
32#include "emu.h"
33#include "zx81_p.h"
34#include "imagedev/cassette.h"
35
36
37#define WAVEENTRY_LOW   -32768
38#define WAVEENTRY_HIGH    32767
39#define WAVEENTRY_ZERO        0
40
41#define ZX81_WAV_FREQUENCY   44100
42
43/* all following are in samples */
44#define ZX81_PULSE_LENGTH   16
45#define ZX81_PAUSE_LENGTH   56
46#define ZX81_PILOT_LENGTH   220500
47
48#define ZX81_LOW_BIT_LENGTH   (ZX81_PULSE_LENGTH*4+ZX81_PAUSE_LENGTH)
49#define ZX81_HIGH_BIT_LENGTH   (ZX81_PULSE_LENGTH*9+ZX81_PAUSE_LENGTH)
50
51#define ZX81_START_LOAD_ADDRESS   0x4009
52#define ZX80_START_LOAD_ADDRESS   0x4000
53#define ZX81_DATA_LENGTH_OFFSET   0x0b
54#define ZX80_DATA_LENGTH_OFFSET   0x04
55
56static UINT8 zx_file_name[128];
57static UINT16 real_data_length = 0;
58static UINT8 zx_file_name_length = 0;
59
60/* common functions */
61
62static INT16 *zx81_emit_level(INT16 *p, int count, int level)
63{
64   int i;
65
66   for (i=0; i<count; i++)   *(p++) = level;
67
68   return p;
69}
70
71static INT16* zx81_emit_pulse(INT16 *p)
72{
73   p = zx81_emit_level (p, ZX81_PULSE_LENGTH/8, WAVEENTRY_LOW);
74   p = zx81_emit_level (p, ZX81_PULSE_LENGTH/8, WAVEENTRY_LOW);
75   p = zx81_emit_level (p, ZX81_PULSE_LENGTH/8, WAVEENTRY_ZERO);
76   p = zx81_emit_level (p, ZX81_PULSE_LENGTH/8, WAVEENTRY_HIGH);
77   p = zx81_emit_level (p, ZX81_PULSE_LENGTH/8, WAVEENTRY_HIGH);
78   p = zx81_emit_level (p, ZX81_PULSE_LENGTH/8, WAVEENTRY_ZERO);
79   p = zx81_emit_level (p, ZX81_PULSE_LENGTH/8, WAVEENTRY_LOW);
80   p = zx81_emit_level (p, ZX81_PULSE_LENGTH/8, WAVEENTRY_LOW);
81
82   return p;
83}
84
85static INT16* zx81_emit_pause(INT16 *p)
86{
87   p = zx81_emit_level (p, ZX81_PAUSE_LENGTH, WAVEENTRY_ZERO);
88
89   return p;
90}
91
92static INT16* zx81_output_bit(INT16 *p, UINT8 bit)
93{
94   int i;
95
96   if (bit)
97      for (i=0; i<9; i++)
98         p = zx81_emit_pulse (p);
99   else
100      for (i=0; i<4; i++)
101         p = zx81_emit_pulse (p);
102
103   p = zx81_emit_pause(p);
104
105       return p;
106}
107
108static INT16* zx81_output_byte(INT16 *p, UINT8 byte)
109{
110   int i;
111
112   for (i=0; i<8; i++)
113      p = zx81_output_bit(p,(byte>>(7-i)) & 0x01);
114
115   return p;
116}
117
118static UINT16 zx81_cassette_calculate_number_of_1(const UINT8 *bytes, UINT16 length)
119{
120   UINT16 number_of_1 = 0;
121   int i,j;
122
123   for (i=0; i<length; i++)
124      for (j=0; j<8; j++)
125         if ((bytes[i]>>j)&0x01)
126            number_of_1++;
127
128   return number_of_1;
129}
130
131/* ZX-81 functions */
132
133static const UINT8 zx81_chars[]={
134           0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /*00h-07h*/
135           0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /*08h-0fh*/
136           0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /*10h-17h*/
137           0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /*18h-1fh*/
138           0x00, 0x00, 0x0b, 0x00, 0x0d, 0x00, 0x00, 0x00, /*20h-27h*/
139           0x10, 0x11, 0x17, 0x15, 0x1a, 0x16, 0x1b, 0x18, /*28h-2fh*/
140           0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, /*30h-37h*/
141           0x24, 0x25, 0x0e, 0x19, 0x13, 0x14, 0x12, 0x0f, /*38h-3fh*/
142           0x00, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, /*40h-47h*/
143           0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, /*48h-4fh*/
144           0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, /*50h-57h*/
145           0x3d, 0x3e, 0x3f, 0x00, 0x00, 0x00, 0x00, 0x00, /*58h-5fh*/
146           0x00, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, /*60h-67h*/
147           0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, /*68h-6fh*/
148           0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, /*70h-77h*/
149           0x3d, 0x3e, 0x3f, 0x00, 0x00, 0x00, 0x00, 0x00, /*78h-7fh*/
150};
151
152static void zx81_fill_file_name(const char* name)
153{
154   for (zx_file_name_length=0; (zx_file_name_length<128) && name[zx_file_name_length]; zx_file_name_length++)
155      zx_file_name[zx_file_name_length] = ((UINT8) name[zx_file_name_length]<0x80) ? zx81_chars[(UINT8) name[zx_file_name_length]] : 0x00;
156   zx_file_name[zx_file_name_length-1] |= 0x80;
157}
158
159static int zx81_cassette_calculate_size_in_samples(const UINT8 *bytes, int length)
160{
161   unsigned int number_of_0_data = 0;
162   unsigned int number_of_1_data = 0;
163   unsigned int number_of_0_name = 0;
164   unsigned int number_of_1_name = 0;
165
166   real_data_length = bytes[ZX81_DATA_LENGTH_OFFSET] + bytes[ZX81_DATA_LENGTH_OFFSET+1]*256 - ZX81_START_LOAD_ADDRESS;
167
168   number_of_1_data = zx81_cassette_calculate_number_of_1(bytes, real_data_length);
169   number_of_0_data = length*8-number_of_1_data;
170
171   number_of_1_name = zx81_cassette_calculate_number_of_1(zx_file_name, zx_file_name_length);
172   number_of_0_name = zx_file_name_length*8-number_of_1_name;
173
174   return (number_of_0_data+number_of_0_name)*ZX81_LOW_BIT_LENGTH + (number_of_1_data+number_of_1_name)*ZX81_HIGH_BIT_LENGTH + ZX81_PILOT_LENGTH;
175}
176
177static int zx81_cassette_fill_wave(INT16 *buffer, int length, UINT8 *bytes)
178{
179   INT16 * p = buffer;
180   int i;
181
182   /* pilot */
183   p = zx81_emit_level (p, ZX81_PILOT_LENGTH, WAVEENTRY_ZERO);
184
185   /* name */
186   for (i=0; i<zx_file_name_length; i++)
187      p = zx81_output_byte(p, zx_file_name[i]);
188
189   /* data */
190   for (i=0; i<real_data_length; i++)
191      p = zx81_output_byte(p, bytes[i]);
192
193   return p - buffer;
194}
195
196static const struct CassetteLegacyWaveFiller zx81_legacy_fill_wave =
197{
198   zx81_cassette_fill_wave,         /* fill_wave */
199   -1,                  /* chunk_size */
200   0,                  /* chunk_samples */
201   zx81_cassette_calculate_size_in_samples,   /* chunk_sample_calc */
202   ZX81_WAV_FREQUENCY,            /* sample_frequency */
203   0,                  /* header_samples */
204   0                  /* trailer_samples */
205};
206
207static casserr_t zx81_p_identify(cassette_image *cassette, struct CassetteOptions *opts)
208{
209   return cassette_legacy_identify(cassette, opts, &zx81_legacy_fill_wave);
210}
211
212static casserr_t zx81_p_load(cassette_image *cassette)
213{
214   /* The filename of the file is used to create the wave stream for the emulated machine. Why is this information not
215       part of the image file itself?
216       Hardcoding this to "cassette".
217    */
218   zx81_fill_file_name ("cassette" /*image_basename_noext(device_list_find_by_tag( Machine->config->m_devicelist, CASSETTE, "cassette" ))*/ );
219   return cassette_legacy_construct(cassette, &zx81_legacy_fill_wave);
220}
221
222static const struct CassetteFormat zx81_p_image_format =
223{
224   "p,81",
225   zx81_p_identify,
226   zx81_p_load,
227   NULL
228};
229
230CASSETTE_FORMATLIST_START(zx81_p_format)
231   CASSETTE_FORMAT(zx81_p_image_format)
232CASSETTE_FORMATLIST_END
233
234/* ZX-80 functions */
235
236static int zx80_cassette_calculate_size_in_samples(const UINT8 *bytes, int length)
237{
238   unsigned int number_of_0_data = 0;
239   unsigned int number_of_1_data = 0;
240
241   real_data_length = bytes[ZX80_DATA_LENGTH_OFFSET] + bytes[ZX80_DATA_LENGTH_OFFSET+1]*256 - ZX80_START_LOAD_ADDRESS - 1;
242
243   number_of_1_data = zx81_cassette_calculate_number_of_1(bytes, real_data_length);
244   number_of_0_data = length*8-number_of_1_data;
245
246   return number_of_0_data*ZX81_LOW_BIT_LENGTH + number_of_1_data*ZX81_HIGH_BIT_LENGTH + ZX81_PILOT_LENGTH;
247}
248
249static int zx80_cassette_fill_wave(INT16 *buffer, int length, UINT8 *bytes)
250{
251   INT16 * p = buffer;
252   int i;
253
254   /* pilot */
255   p = zx81_emit_level (p, ZX81_PILOT_LENGTH, WAVEENTRY_ZERO);
256
257   /* data */
258   for (i=0; i<real_data_length; i++)
259      p = zx81_output_byte(p, bytes[i]);
260
261   return p - buffer;
262}
263
264static const struct CassetteLegacyWaveFiller zx80_legacy_fill_wave =
265{
266   zx80_cassette_fill_wave,         /* fill_wave */
267   -1,                                 /* chunk_size */
268   0,                                 /* chunk_samples */
269   zx80_cassette_calculate_size_in_samples,   /* chunk_sample_calc */
270   ZX81_WAV_FREQUENCY,                              /* sample_frequency */
271   0,                                 /* header_samples */
272   0                                 /* trailer_samples */
273};
274
275static casserr_t zx80_o_identify(cassette_image *cassette, struct CassetteOptions *opts)
276{
277   return cassette_legacy_identify(cassette, opts, &zx80_legacy_fill_wave);
278}
279
280static casserr_t zx80_o_load(cassette_image *cassette)
281{
282   return cassette_legacy_construct(cassette, &zx80_legacy_fill_wave);
283}
284
285static const struct CassetteFormat zx80_o_image_format =
286{
287   "o,80",
288   zx80_o_identify,
289   zx80_o_load,
290   NULL
291};
292
293CASSETTE_FORMATLIST_START(zx80_o_format)
294   CASSETTE_FORMAT(zx80_o_image_format)
295CASSETTE_FORMATLIST_END
src/mess/formats/vt_dsk.c
r11457r11458
1/*********************************************************************
2
3    formats/vt_dsk.c
4
5    VTech Laser/VZ disk images
6
7*********************************************************************/
8
9#include <stdio.h>
10#include <string.h>
11#include "formats/vt_dsk.h"
12#include "formats/basicdsk.h"
13
14static FLOPPY_IDENTIFY(vz_identify)
15{
16   UINT64 size = floppy_image_size(floppy);
17   *vote = ((size == 98560) || (size == 99200) || (size == 99184)) ? 100 : 0;
18   return FLOPPY_ERROR_SUCCESS;
19}
20
21static FLOPPY_CONSTRUCT(vz_construct)
22{
23   struct basicdsk_geometry geometry;
24   memset(&geometry, 0, sizeof(geometry));
25
26   if (params)
27   {
28      geometry.heads           = option_resolution_lookup_int(params, PARAM_HEADS);
29      geometry.tracks          = option_resolution_lookup_int(params, PARAM_TRACKS);
30      geometry.sectors         = option_resolution_lookup_int(params, PARAM_SECTORS);
31      geometry.first_sector_id = option_resolution_lookup_int(params, PARAM_FIRST_SECTOR_ID);
32      geometry.sector_length   = option_resolution_lookup_int(params, PARAM_SECTOR_LENGTH);
33   }
34   else
35   {
36      geometry.heads           = 1;
37      geometry.tracks          = 40;
38      geometry.sectors         = 16;
39      geometry.first_sector_id = 0;
40      geometry.sector_length   = floppy_image_size(floppy)/geometry.tracks/geometry.sectors;
41   }
42
43   return basicdsk_construct(floppy, &geometry);
44}
45
46
47FLOPPY_OPTIONS_START(vz)
48   FLOPPY_OPTION(vtech1, "dsk", "Laser/VZ disk image", vz_identify, vz_construct, NULL,
49      HEADS([1])
50      TRACKS([40])
51      SECTORS([16])
52      SECTOR_LENGTH([154])
53      FIRST_SECTOR_ID([0]))
54FLOPPY_OPTIONS_END
src/mess/formats/zx81_p.h
r11457r11458
1/*********************************************************************
2
3    zx81_p.h
4
5    Format code for ZX-81 casette files
6
7*********************************************************************/
8
9#ifndef ZX81_P_H
10#define ZX81_P_H
11
12#include "imagedev/cassimg.h"
13
14CASSETTE_FORMATLIST_EXTERN(zx81_p_format);
15CASSETTE_FORMATLIST_EXTERN(zx80_o_format);
16
17#endif /* ZX81_P_H */
src/mess/formats/cpis_dsk.c
r11457r11458
1/*********************************************************************
2
3    formats/cpis_dsk.c
4
5    Telenova Compis disk images
6
7*********************************************************************/
8
9#include <string.h>
10
11#include "formats/cpis_dsk.h"
12#include "formats/basicdsk.h"
13
14
15static int compis_get_tracks_and_sectors(floppy_image *floppy, int *tracks, int *sectors)
16{
17   switch(floppy_image_size(floppy)) {
18   case 0x50000:   /* 320 KB */
19      *tracks = 40;
20      *sectors = 8;
21      break;
22
23   case 0x5a000:   /* 360 KB */
24      *tracks = 40;
25      *sectors = 9;
26      break;
27
28   case 0xa0000:   /* 640 KB */
29      *tracks = 80;
30      *sectors = 8;
31      break;
32
33   case 0xb4000:   /* 720 KB */
34      *tracks = 80;
35      *sectors = 9;
36      break;
37
38   case 0x12c000:   /* 1200 KB */
39      *tracks = 80;
40      *sectors = 15;
41      break;
42
43   default:
44      return 0;
45   }
46   return 1;
47}
48
49
50
51static FLOPPY_IDENTIFY(compis_dsk_identify)
52{
53   int dummy;
54   *vote = compis_get_tracks_and_sectors(floppy, &dummy, &dummy) ? 100 : 0;
55   return FLOPPY_ERROR_SUCCESS;
56}
57
58
59
60static FLOPPY_CONSTRUCT(compis_dsk_construct)
61{
62   struct basicdsk_geometry geometry;
63
64   memset(&geometry, 0, sizeof(geometry));
65   geometry.heads = 1;
66   geometry.first_sector_id = 1;
67   geometry.sector_length = 512;
68
69   if (params)
70   {
71      /* create */
72      geometry.tracks = option_resolution_lookup_int(params, PARAM_TRACKS);
73      geometry.sectors = option_resolution_lookup_int(params, PARAM_SECTORS);
74   }
75   else
76   {
77      /* open */
78      if (!compis_get_tracks_and_sectors(floppy, &geometry.tracks, &geometry.sectors))
79         return FLOPPY_ERROR_INVALIDIMAGE;
80   }
81
82   return basicdsk_construct(floppy, &geometry);
83}
84
85
86
87/* ----------------------------------------------------------------------- */
88
89FLOPPY_OPTIONS_START( compis )
90   FLOPPY_OPTION( compis_dsk, "dsk",      "Compis floppy disk image",   compis_dsk_identify, compis_dsk_construct, NULL,
91      TRACKS(40/[80])
92      SECTORS(8/[9]/15))
93FLOPPY_OPTIONS_END
src/mess/formats/vt_dsk.h
r11457r11458
1/*********************************************************************
2
3    formats/vt_dsk.h
4
5    Tandy Color Computer / Dragon disk images
6
7*********************************************************************/
8
9#ifndef VT_DSK_H
10#define VT_DSK_H
11
12#include "imagedev/flopimg.h"
13
14FLOPPY_OPTIONS_EXTERN(vz);
15
16#endif /* VT_DSK_H */
src/mess/formats/oric_tap.c
r11457r11458
1#include "oric_tap.h"
2#include "emu.h"
3
4#define ORIC_WAV_DEBUG 1
5#define LOG(x) do { if (ORIC_WAV_DEBUG) logerror x; } while (0)
6
7/* this code based heavily on tap2wav by Fabrice Frances */
8#define ORIC_SYNC_BYTE   0x016
9
10/* frequency of wave */
11/* tapes use 1200Hz and 2400Hz samples */
12#define ORIC_WAV_FREQUENCY   4800
13
14/* 13 bits define a byte on the cassette */
15/* 1 start bit, 8 data bits, 1 parity bit and 3 stop bits */
16#define ORIC_BYTE_TO_BITS_ON_CASSETTE 13
17
18#define ORIC_WAVESAMPLES_HEADER  3000
19#define ORIC_WAVESAMPLES_TRAILER 1000
20
21enum
22{
23   ORIC_CASSETTE_SEARCHING_FOR_SYNC_BYTE,
24   ORIC_CASSETTE_GOT_SYNC_BYTE,
25   ORIC_CASSETTE_READ_HEADER,
26   ORIC_CASSETTE_READ_FILENAME,
27   ORIC_CASSETTE_WRITE_DATA
28};
29
30#define WAVEENTRY_LOW  -32768
31#define WAVEENTRY_HIGH  32767
32#define WAVEENTRY_NULL  0
33
34#define ORIC_LEADER_LENGTH 512
35
36struct oric_t
37{
38   int cassette_state;
39   int data_count;
40   int data_length;
41   int tap_size;
42};
43static oric_t oric;
44
45/* to write a bit to the tape, the rom routines output either 4 periods at 1200 Hz for a 0 or 8 periods at 2400 Hz for a 1  */
46/* 4800 is twice 2400Hz */
47
48/* 8 periods at 2400Hz */
49/* hi,lo, hi,lo, hi,lo, hi,lo */
50
51static INT16 *oric_emit_level(INT16 *p, int count, INT16 wave_state)
52{
53   int i;
54
55   for (i=0; i<count; i++)
56   {
57      *(p++) = wave_state;
58   }
59   return p;
60}
61
62/* 4 periods at 1200Hz */
63static INT16* oric_output_bit(INT16 *p, UINT8 b)
64{
65   p = oric_emit_level(p, 1, WAVEENTRY_HIGH);
66   p = oric_emit_level(p, b ? 1 : 2, WAVEENTRY_LOW);
67
68   return p;
69}
70
71static int oric_get_bit_size_in_samples(UINT8 b)
72{
73   int count;
74
75   count = 1;
76
77   if (b)
78   {
79      count++;
80   }
81   else
82   {
83      count+=2;
84   }
85
86   return count;
87}
88
89
90/*  each byte on cassette is stored as:
91
92    start bit       0 * 1
93    data bits       8 * x (x is 0 or 1, and depends on data-bit value)
94    parity bit      1 * x (x is 0 or 1, and depends on the parity of the data bits)
95    stop bits       1 * 3
96
97    if data has even parity, parity bit will be 1.
98    if data has odd parity, parity bit will be 0.
99*/
100
101/*
102    512 * data byte 0x016       -> leader
103    1   * data byte 0x024       -> sync byte
104    9   * data byte             -> header
105    delay (of last pulse written)
106    x   * data byte         -> length
107
108
109    header structure:
110    3   * ?         ->      ???
111    1   * ?         ->      ???
112    1   * x         ->      end address high byte
113    1   * x         ->      end address low byte
114    1   * x         ->      start address high byte
115    1   * x         ->      start address low byte
116    1   * ?         ->      ???
117*/
118
119static int oric_calculate_byte_size_in_samples(UINT8 byte)
120{
121   int count;
122   int i;
123   UINT8 parity;
124   UINT8 data;
125
126   count = 0;
127
128
129   /* start bit */
130   count+=oric_get_bit_size_in_samples(0);
131
132   /* set initial parity */
133   parity = 1;
134
135   /* data bits, written bit 0, bit 1...bit 7 */
136   data = byte;
137   for (i=0; i<8; i++)
138   {
139      UINT8 data_bit;
140
141      data_bit = data & 0x01;
142
143      parity = parity + data_bit;
144
145      count+=oric_get_bit_size_in_samples(data_bit);
146      data = data>>1;
147   }
148
149   /* parity */
150   count+=oric_get_bit_size_in_samples((parity & 0x01));
151
152   /* stop bits */
153   count+=oric_get_bit_size_in_samples(1);
154   count+=oric_get_bit_size_in_samples(1);
155   count+=oric_get_bit_size_in_samples(1);
156   count+=oric_get_bit_size_in_samples(1);
157
158   return count;
159}
160
161
162static INT16 *oric_output_byte(INT16 *p, UINT8 byte)
163{
164   int i;
165   UINT8 parity;
166   UINT8 data;
167
168   /* start bit */
169   p = oric_output_bit(p, 0);
170
171   /* set initial parity */
172   parity = 1;
173
174   /* data bits, written bit 0, bit 1...bit 7 */
175   data = byte;
176   for (i=0; i<8; i++)
177   {
178      UINT8 data_bit;
179
180      data_bit = data & 0x01;
181
182      parity = parity + data_bit;
183
184      p = oric_output_bit(p, data_bit);
185      data = data>>1;
186   }
187
188   /* parity */
189   p = oric_output_bit(p, parity & 0x01);
190
191   /* stop bits */
192   p = oric_output_bit(p, 1);
193   p = oric_output_bit(p, 1);
194   p = oric_output_bit(p, 1);
195   p = oric_output_bit(p, 1);
196
197   return p;
198}
199
200static INT16 *oric_fill_pause(INT16 *p, int sample_count)
201{
202   int i;
203
204   for (i=0; i<sample_count; i++)
205   {
206      *(p++) = WAVEENTRY_NULL;
207   }
208
209   return p;
210}
211
212static int oric_seconds_to_samples(float seconds)
213{
214   return (int)((float)seconds*(float)ORIC_WAV_FREQUENCY);
215}
216
217/* length is length of .tap file! */
218static int oric_cassette_calculate_size_in_samples(const UINT8 *bytes, int length)
219{
220   unsigned char header[9];
221   int count;
222
223   const UINT8 *data_ptr;
224   int i;
225   UINT8 data;
226
227   oric.tap_size = length;
228
229   oric.cassette_state = ORIC_CASSETTE_SEARCHING_FOR_SYNC_BYTE;
230   count = 0;
231   data_ptr = bytes;
232
233   while (data_ptr<(bytes+length))
234   {
235      data = data_ptr[0];
236      data_ptr++;
237
238      switch (oric.cassette_state)
239      {
240         case ORIC_CASSETTE_SEARCHING_FOR_SYNC_BYTE:
241         {
242            if (data==ORIC_SYNC_BYTE)
243            {
244               logerror("found sync byte!\n");
245               /* found first sync byte */
246               oric.cassette_state = ORIC_CASSETTE_GOT_SYNC_BYTE;
247            }
248         }
249         break;
250
251         case ORIC_CASSETTE_GOT_SYNC_BYTE:
252         {
253            if (data!=ORIC_SYNC_BYTE)
254            {
255               /* 0.25 second pause */
256               count += oric_seconds_to_samples(0.25);
257
258               logerror("found end of sync bytes!\n");
259
260               /* oric writes approx 512 bytes */
261               /* found end of sync bytes */
262               for (i=0; i<ORIC_LEADER_LENGTH; i++)
263               {
264                  count+=oric_calculate_byte_size_in_samples(0x016);
265               }
266
267               if (data==0x024)
268               {
269                  //logerror("reading header!\n");
270                  count+=oric_calculate_byte_size_in_samples(0x024);
271
272                  oric.cassette_state = ORIC_CASSETTE_READ_HEADER;
273                  oric.data_count = 0;
274                  oric.data_length = 9;
275               }
276            }
277         }
278         break;
279
280         case ORIC_CASSETTE_READ_HEADER:
281         {
282            header[oric.data_count] = data;
283            count+=oric_calculate_byte_size_in_samples(data);
284
285            oric.data_count++;
286
287            if (oric.data_count==oric.data_length)
288            {
289               //logerror("finished reading header!\n");
290               oric.cassette_state = ORIC_CASSETTE_READ_FILENAME;
291            }
292         }
293         break;
294
295         case ORIC_CASSETTE_READ_FILENAME:
296         {
297            count+=oric_calculate_byte_size_in_samples(data);
298
299            /* got end of filename? */
300            if (data==0)
301            {
302               UINT16 end, start;
303               logerror("got end of filename\n");
304
305               /* 100 1 bits to seperate header from data */
306                                        for (i=0; i<100; i++)
307                                        {
308                                            count+=oric_get_bit_size_in_samples(1);
309                                        }
310
311               oric.cassette_state = ORIC_CASSETTE_WRITE_DATA;
312               oric.data_count = 0;
313
314               end = (((header[4] & 0x0ff)<<8) | (header[5] & 0x0ff));
315               start = (((header[6] & 0x0ff)<<8) | (header[7] & 0x0ff));
316               LOG(("start (from header): %02x\n",start));
317               LOG(("end (from header): %02x\n",end));
318                                        oric.data_length = end - start + 1;
319            }
320         }
321         break;
322
323         case ORIC_CASSETTE_WRITE_DATA:
324         {
325            count+=oric_calculate_byte_size_in_samples(data);
326            oric.data_count++;
327
328            if (oric.data_count==oric.data_length)
329            {
330               logerror("finished writing data!\n");
331               oric.cassette_state = ORIC_CASSETTE_SEARCHING_FOR_SYNC_BYTE;
332            }
333         }
334         break;
335
336      }
337   }
338
339   return count;
340}
341
342/* length is length of sample buffer to fill! */
343static int oric_cassette_fill_wave(INT16 *buffer, int length, UINT8 *bytes)
344{
345   unsigned char header[9];
346   UINT8 *data_ptr;
347   INT16 *p;
348   int i;
349   UINT8 data;
350
351   p = buffer;
352
353
354   /* header and trailer act as pauses */
355   /* the trailer is required so that the via sees the last bit of the last
356        byte */
357   if (bytes == CODE_HEADER) {
358      for (i = 0; i < ORIC_WAVESAMPLES_HEADER; i++)
359         *(p++) = WAVEENTRY_NULL;
360   }
361   else if (bytes == CODE_TRAILER) {
362      for (i = 0; i < ORIC_WAVESAMPLES_TRAILER; i++)
363         *(p++) = WAVEENTRY_NULL;
364   }
365   else
366{
367   /* the length is the number of samples left in the buffer and NOT the number of bytes for the input file */
368   length = length - ORIC_WAVESAMPLES_TRAILER;
369
370   oric.cassette_state = ORIC_CASSETTE_SEARCHING_FOR_SYNC_BYTE;
371   data_ptr = bytes;
372
373   while ((data_ptr<(bytes + oric.tap_size)) && (p < (buffer+length)) )
374   {
375      data = data_ptr[0];
376      data_ptr++;
377
378      switch (oric.cassette_state)
379      {
380         case ORIC_CASSETTE_SEARCHING_FOR_SYNC_BYTE:
381         {
382            if (data==ORIC_SYNC_BYTE)
383            {
384               logerror("found sync byte!\n");
385               /* found first sync byte */
386               oric.cassette_state = ORIC_CASSETTE_GOT_SYNC_BYTE;
387            }
388         }
389         break;
390
391         case ORIC_CASSETTE_GOT_SYNC_BYTE:
392         {
393            if (data!=ORIC_SYNC_BYTE)
394            {
395               /* 0.25 second pause */
396               p = oric_fill_pause(p, oric_seconds_to_samples(0.25));
397
398               logerror("found end of sync bytes!\n");
399               /* found end of sync bytes */
400               for (i=0; i<ORIC_LEADER_LENGTH; i++)
401               {
402                  p = oric_output_byte(p,0x016);
403               }
404
405               if (data==0x024)
406               {
407                  //logerror("reading header!\n");
408                  p = oric_output_byte(p,data);
409                  oric.cassette_state = ORIC_CASSETTE_READ_HEADER;
410                  oric.data_count = 0;
411                  oric.data_length = 9;
412               }
413            }
414         }
415         break;
416
417         case ORIC_CASSETTE_READ_HEADER:
418         {
419            header[oric.data_count] = data;
420            p = oric_output_byte(p, data);
421            oric.data_count++;
422
423            if (oric.data_count==oric.data_length)
424            {
425               //logerror("finished reading header!\n");
426               oric.cassette_state = ORIC_CASSETTE_READ_FILENAME;
427            }
428         }
429         break;
430
431         case ORIC_CASSETTE_READ_FILENAME:
432         {
433            p = oric_output_byte(p, data);
434
435            /* got end of filename? */
436            if (data==0)
437            {
438               UINT16 end, start;
439               logerror("got end of filename\n");
440
441               /* oric includes a small delay, but I don't see
442                    it being 1 bits */
443               for (i=0; i<100; i++)
444               {
445                                            p = oric_output_bit(p,1);
446               }
447
448               oric.cassette_state = ORIC_CASSETTE_WRITE_DATA;
449               oric.data_count = 0;
450
451               end = (((header[4] & 0x0ff)<<8) | (header[5] & 0x0ff));
452               start = (((header[6] & 0x0ff)<<8) | (header[7] & 0x0ff));
453               LOG(("start (from header): %02x\n",start));
454               LOG(("end (from header): %02x\n",end));
455                                        oric.data_length = end - start + 1;
456            }
457         }
458         break;
459
460         case ORIC_CASSETTE_WRITE_DATA:
461         {
462            p = oric_output_byte(p, data);
463            oric.data_count++;
464
465            if (oric.data_count==oric.data_length)
466            {
467               logerror("finished writing data!\n");
468               oric.cassette_state = ORIC_CASSETTE_SEARCHING_FOR_SYNC_BYTE;
469            }
470         }
471         break;
472
473      }
474   }
475}
476   return p - buffer;
477}
478
479
480
481static const struct CassetteLegacyWaveFiller oric_legacy_fill_wave =
482{
483   oric_cassette_fill_wave,               /* fill_wave */
484   -1,                                 /* chunk_size */
485   0,                                 /* chunk_samples */
486   oric_cassette_calculate_size_in_samples,   /* chunk_sample_calc */
487   ORIC_WAV_FREQUENCY,                     /* sample_frequency */
488   ORIC_WAVESAMPLES_HEADER,               /* header_samples */
489   ORIC_WAVESAMPLES_TRAILER               /* trailer_samples */
490};
491
492
493
494static casserr_t oric_tap_identify(cassette_image *cassette, struct CassetteOptions *opts)
495{
496   return cassette_legacy_identify(cassette, opts, &oric_legacy_fill_wave);
497}
498
499
500
501static casserr_t oric_tap_load(cassette_image *cassette)
502{
503   return cassette_legacy_construct(cassette, &oric_legacy_fill_wave);
504}
505
506
507
508static const struct CassetteFormat oric_tap_format =
509{
510   "tap",
511   oric_tap_identify,
512   oric_tap_load,
513   NULL
514};
515
516
517
518CASSETTE_FORMATLIST_START(oric_cassette_formats)
519   CASSETTE_FORMAT(oric_tap_format)
520CASSETTE_FORMATLIST_END
src/mess/formats/cpis_dsk.h
r11457r11458
1/*********************************************************************
2
3    formats/cpis_dsk.h
4
5    Telenova Compis disk images
6
7*********************************************************************/
8
9#ifndef CPIS_DSK_H
10#define CPIS_DSK_H
11
12#include "imagedev/flopimg.h"
13
14
15/**************************************************************************/
16
17FLOPPY_OPTIONS_EXTERN(compis);
18
19#endif /* CPIS_DSK_H */
src/mess/formats/atom_tap.c
r11457r11458
1/*********************************************************************
2
3    formats/atom_tap.c
4
5    Cassette code for Acorn Atom tap files (Kansas City Standard)
6
7*********************************************************************/
8
9/*
10
11    http://beebwiki.jonripley.com/Acorn_cassette_format#Atom
12
13    The Atom supports the System series format (as a 'nameless file') and its own format. The data consists of files separated
14    by carrier breaks and the appropriate leaders and trailers, and further subdivided into blocks separated by inter-block gaps.
15    An Atom block consists of the following sequence of bytes:
16
17        Four synchronisation bytes (&2A).
18        File name (one to thirteen characters).
19        One end of file name marker byte (&0D).
20        Block flag, one byte.
21        Block number, two bytes, high byte first.
22        Data block length ? 1, one byte.
23        Execution address, two bytes, high byte first.
24        Load address, two bytes, high byte first.
25        Data, 0 to 256 bytes.
26        Checksum, one byte.
27
28    The block flag is set as follows:
29
30        Bit 7 is set if this block is not the last block of a file.
31        Bit 6 is set if the block contains data. If clear then the 'data block length' field is invalid.
32        Bit 5 is set if this block is not the first block of a file.
33        Bits 4 to 0 are undefined. Normally their contents are:
34        in the first block, bits 15 to 11 of the end address (=1 + the last address saved in the file);
35        in subsequent blocks, bits 6 to 2 of the previous block flag.
36
37    The checksum is a simple sum (modulo 256) of all bytes from the start of the filename to the end of the data.
38
39*/
40
41#include "emu.h"
42#include "formats/atom_tap.h"
43
44/***************************************************************************
45    PARAMETERS
46***************************************************************************/
47
48#define LOG 1
49
50/***************************************************************************
51    IMPLEMENTATION
52***************************************************************************/
53
54/*-------------------------------------------------
55    CassetteModulation atom_tap_modulation
56-------------------------------------------------*/
57
58static const struct CassetteModulation atom_tap_modulation =
59{
60   CASSETTE_MODULATION_SINEWAVE,
61   1200.0 - 300, 1200.0, 1200.0 + 300,
62   2400.0 - 600, 2400.0, 2400.0 + 600
63};
64
65/*-------------------------------------------------
66    cassette_image_read_uint8 - read tape data
67-------------------------------------------------*/
68
69static UINT8 cassette_image_read_uint8(cassette_image *cassette, UINT64 offset)
70{
71   UINT8 data;
72   cassette_image_read(cassette, &data, offset, 1);
73   return data;
74}
75
76/*-------------------------------------------------
77    atom_tap_identify - identify cassette
78-------------------------------------------------*/
79
80static casserr_t atom_tap_identify(cassette_image *cassette, struct CassetteOptions *opts)
81{
82   return cassette_modulation_identify( cassette, &atom_tap_modulation, opts);
83}
84
85/*-------------------------------------------------
86    atom_tap_load - load cassette
87-------------------------------------------------*/
88
89#define MODULATE(_value) \
90   for (int i = 0; i < (_value ? 8 : 4); i++) { \
91      err = cassette_put_modulated_data_bit(cassette, 0, time_index, _value, &atom_tap_modulation, &time_displacement);\
92      if (err) return err;\
93      time_index += time_displacement;\
94   }
95
96static casserr_t atom_tap_load(cassette_image *cassette)
97{
98   casserr_t err;
99   UINT64 image_size = cassette_image_size(cassette);
100   UINT64 image_pos = 0;
101   double time_index = 0.0;
102   double time_displacement;
103
104   while (image_pos < image_size)
105   {
106      UINT8 data = cassette_image_read_uint8(cassette, image_pos);
107
108      /* start bit */
109      MODULATE(0);
110
111      /* data bits */
112      for (int bit = 0; bit < 8; bit++)
113      {
114         MODULATE(BIT(data, bit));
115      }
116
117      /* stop bits */
118      MODULATE(1);
119      MODULATE(1);
120
121      image_pos++;
122   }
123
124   return CASSETTE_ERROR_SUCCESS;
125}
126
127/*-------------------------------------------------
128    CassetteFormat atom_tap_cassette_format
129-------------------------------------------------*/
130
131const struct CassetteFormat atom_tap_format =
132{
133   "tap",
134   atom_tap_identify,
135   atom_tap_load,
136   NULL
137};
src/mess/formats/z80ne_dsk.c
r11457r11458
1/*********************************************************************
2
3    formats/z80ne_dsk.c
4
5    Nuova Elettronica Z80NE disk images
6
7*********************************************************************/
8
9#include "emu.h"
10#include "formats/z80ne_dsk.h"
11#include "formats/basicdsk.h"
12#include "imagedev/flopdrv.h"
13#include "imageutl.h"
14
15/* -----------------------------------------------------------------------
16 * DMK file format
17 *
18 * David M. Keil's disk image format is aptly called an 'on disk' image
19 * format. This means that whatever written to the disk is encoded into
20 * the image file. IDAMS, sector headers, trailing CRCs, and intra-sector
21 * spacing.                                                         *
22 *
23 *  HEADER DESCRIPTION:
24 *
25 *  Offset  Bytes   Field           Description
26 *  ------  -----   ------------    -----------
27 *       0      1   write_prot      0xff = Write Protected, 0x00 = R/W
28 *       1      1   tracks          Number of tracks
29 *       2      2   track_length    Bytes per track on DMK file (little endian)
30 *       4      1   disk_options    Miscellaneous flags
31 *                                      bit 0-3:  Unused
32 *                                      bit   4:  Sides on DMK file
33 *                                                1=single, 0=double
34 *                                      bit   5:  Unused
35 *                                      bit   6:  Single Density DMK file
36 *                                                1=single (bytes written once
37 *                                                  on IDAMs)
38 *                                                0=double (bytes written
39 *                                                  two times for SD IDAMs)
40 *       5      7   reserved        Reserved for future use
41 *      12      4   real_disk_code  If this is 0x12345678 (little endian)
42 *                                  then access a real disk drive
43 *                                  (unsupported)
44 *
45 * Each track begins with a track TOC, consisting of 64 little endian 16-bit
46 * integers.  Each integer has the following format:
47 *      bit 0-13:   Offset from beginning of track to 'FE' byte of IDAM
48 *                  Note these are always sorted from first to last. All empty
49 *                  entries are 0x00
50 *      bit 14:     Undefined (reserved)
51 *      bit 15:     Sector double density (0=SD 1=DD)
52 *                  If not set and SD DMK flag is set then bytes are written
53 *                  once.
54 * ----------------------------------------------------------------------- */
55
56struct dmk_tag
57{
58   int heads;
59   int tracks;
60   UINT32 track_size;
61};
62
63/* DMK file structure
64 * See WD1711 documentation
65 */
66/* DMK file header */
67#define DMK_HEADER_LEN         16         /* DMK header */
68   /*  For every track (40) */
69   #define DMK_TOC_LEN            128      /* 64 16-bit relative offsets to IDAMs */
70   #define DMK_GAP1_LEN         32      /* GAP1 (26 bytes 0xFF and 6 bytes 0x00) */
71
72      /* For every sector (10) 301 bytes */
73      #define DMK_IDAM_LEN         7   /* IDAM (0xFE, Track, 0x00, Sector, 0x00, CRChi, CRClo) */
74      #define DMK_ID_GAP_LEN         17   /* GAP2 (11 bytes 0xFF and 6 bytes 0x00) */
75      #define DMK_DAM_LEN          1   /* Data Address Mark (0xFB) */
76      #define DMK_DATA_LEN         256 /* Data */
77      #define DMK_DATA_CRC_LEN      2   /* Data CRC (CRChi, CRClo) */
78      #define DMK_DATA_GAP_LEN      18  /* GAP3 (12 bytes 0xFF and 6 bytes 0x00) (not for last sector) */
79
80   #define DMK_EXTRA_TRACK_LENGTH   (176)   /* GAP4 (192 bytes 0xFF) */
81/* End of DMK file structure */
82
83#define dmk_idam_type(x)         (x)[0]
84#define dmk_idam_track(x)         (x)[1]
85#define dmk_idam_side(x)         (x)[2]
86#define dmk_idam_sector(x)         (x)[3]
87#define dmk_idam_sectorlength(x)   (x)[4]
88#define dmk_idam_crc(x)            (((x)[5] << 8) + (x)[6])
89#define dmk_idam_set_crc(x, crc)   (x)[5] = ((crc) >> 8); (x)[6] = ((crc) >> 0);
90
91static const char needle_data[] = "\x00\x00\x00\x00\x00\x00\x00\x00\xFB\xFB";
92static const char needle_deleted_data_fa[] = "\x00\x00\x00\x00\x00\x00\x00\x00\xFA\xFA";
93static const char needle_deleted_data_f8[] = "\x00\x00\x00\x00\x00\x00\x00\x00\xF8\xF8";
94
95
96static struct dmk_tag *get_dmk_tag(floppy_image *floppy)
97{
98   return (dmk_tag *)floppy_tag(floppy);
99}
100
101
102static floperr_t z80ne_dmk_get_track_offset(floppy_image *floppy, int head, int track, UINT64 *offset)
103{
104   struct dmk_tag *tag = get_dmk_tag(floppy);
105
106   if ((head < 0) || (head >= tag->heads) || (track < 0) || (track >= tag->tracks))
107      return FLOPPY_ERROR_SEEKERROR;
108
109   *offset = track;
110   *offset *= tag->heads;
111   *offset += head;
112   *offset *= tag->track_size;
113   *offset += DMK_HEADER_LEN;
114   return FLOPPY_ERROR_SUCCESS;
115}
116
117
118static UINT32 z80ne_dmk_min_track_size(int sectors, int sector_length)
119{
120   int sector_physical_length;
121   sector_physical_length = (DMK_IDAM_LEN + DMK_ID_GAP_LEN + DMK_DAM_LEN + sector_length + DMK_DATA_CRC_LEN + DMK_DATA_GAP_LEN);
122   return DMK_TOC_LEN + 2 * (DMK_GAP1_LEN + (sectors * sector_physical_length) - DMK_DATA_GAP_LEN + DMK_EXTRA_TRACK_LENGTH);
123}
124
125
126
127static floperr_t z80ne_dmk_read_track(floppy_image *floppy, int head, int track, UINT64 offset, void *buffer, size_t buflen)
128{
129   floperr_t err;
130   UINT64 track_offset;
131
132   err = z80ne_dmk_get_track_offset(floppy, head, track, &track_offset);
133   if (err)
134      return err;
135
136   floppy_image_read(floppy, buffer, offset + track_offset, buflen);
137   return FLOPPY_ERROR_SUCCESS;
138}
139
140
141
142static floperr_t z80ne_dmk_write_track(floppy_image *floppy, int head, int track, UINT64 offset, const void *buffer, size_t buflen)
143{
144   floperr_t err;
145   UINT64 track_offset;
146
147   err = z80ne_dmk_get_track_offset(floppy, head, track, &track_offset);
148   if (err)
149      return err;
150
151   floppy_image_write(floppy, buffer, offset + track_offset, buflen);
152   return FLOPPY_ERROR_SUCCESS;
153}
154
155
156
157static floperr_t z80ne_dmk_get_track_data_offset(floppy_image *floppy, int head, int track, UINT64 *offset)
158{
159   *offset = DMK_TOC_LEN + 1;
160   return FLOPPY_ERROR_SUCCESS;
161}
162
163
164static floperr_t z80ne_dmk_format_track(floppy_image *floppy, int head, int track, option_resolution *params)
165{
166   int sectors;
167   int sector_length;
168   int interleave;
169   int first_sector_id;
170   floperr_t err;
171   int physical_sector;
172   int logical_sector;
173   int track_position;
174   UINT16 idam_offset;
175   UINT16 crc;
176   UINT8 *track_data;
177   void *track_data_v;
178   UINT32 max_track_size;
179   int *sector_map = NULL;
180   UINT8 *local_sector = NULL;
181   int local_sector_size;
182   int sector_position;
183   int i;
184
185   err = FLOPPY_ERROR_SUCCESS;
186
187   sectors         = option_resolution_lookup_int(params, PARAM_SECTORS);
188   sector_length   = option_resolution_lookup_int(params, PARAM_SECTOR_LENGTH);
189   interleave      = option_resolution_lookup_int(params, PARAM_INTERLEAVE);
190   first_sector_id   = option_resolution_lookup_int(params, PARAM_FIRST_SECTOR_ID);
191
192   max_track_size = get_dmk_tag(floppy)->track_size;
193
194   if (sectors > DMK_TOC_LEN/2)
195   {
196        err = FLOPPY_ERROR_INTERNAL;
197      goto done;
198   }
199
200   if (max_track_size < z80ne_dmk_min_track_size(sectors, sector_length))
201   {
202      err = FLOPPY_ERROR_NOSPACE;
203      goto done;
204   }
205
206   err = floppy_load_track(floppy, head, track, TRUE, &track_data_v, NULL);
207   if (err)
208      goto done;
209   track_data = (UINT8 *) track_data_v;
210
211   /* set up sector map */
212   sector_map = (int*)malloc(sectors * sizeof(*sector_map));
213   if (!sector_map)
214   {
215      err = FLOPPY_ERROR_OUTOFMEMORY;
216      goto done;
217   }
218   memset(sector_map, 0xFF, sectors * sizeof(*sector_map));
219
220   physical_sector = 0;
221   for (logical_sector = 0; logical_sector < sectors; logical_sector++)
222   {
223      while(sector_map[physical_sector] >= 0)
224      {
225         physical_sector++;
226         physical_sector %= sectors;
227      }
228
229      sector_map[physical_sector] = logical_sector + first_sector_id;
230      physical_sector += interleave + 1;
231      physical_sector %= sectors;
232   }
233
234   /* set up a local physical sector template */
235   local_sector_size = (DMK_IDAM_LEN +
236                      DMK_ID_GAP_LEN +
237                      DMK_DAM_LEN +
238                      sector_length +
239                      DMK_DATA_CRC_LEN +
240                      DMK_DATA_GAP_LEN);
241   local_sector = (UINT8*)malloc(local_sector_size);
242   if (!local_sector)
243   {
244      err = FLOPPY_ERROR_OUTOFMEMORY;
245      goto done;
246   }
247   memset(local_sector, 0x00, local_sector_size);
248
249   /* set up track table of contents */
250   physical_sector = 0;
251
252   /* write GAP1 (track lead in) */
253   memset(&track_data[DMK_TOC_LEN],    0xFF, 52);
254   memset(&track_data[DMK_TOC_LEN+52], 0x00, 12);
255   track_position = DMK_TOC_LEN + DMK_GAP1_LEN * 2;
256
257   /*
258     *  prepare template in local sector
259     */
260
261   /* IDAM */
262   sector_position = 0;
263   dmk_idam_type(            &local_sector[sector_position]) = 0xFE;
264   dmk_idam_track(            &local_sector[sector_position]) = track;
265   dmk_idam_side(            &local_sector[sector_position]) = head;
266   dmk_idam_sector(         &local_sector[sector_position]) = 0;      /* replace in sector instances */
267   dmk_idam_sectorlength(      &local_sector[sector_position]) = compute_log2(sector_length / 128);
268   dmk_idam_set_crc(         &local_sector[sector_position], 0);       /* replace in sector instances */
269   sector_position += 7;
270
271   /* GAP2 (ID GAP) */
272   memset(&local_sector[sector_position], 0xFF, 11);
273   sector_position += 11;
274   memset(&local_sector[sector_position], 0x00, 6);
275   sector_position += 6;
276
277   /* sector body */
278   local_sector[sector_position] = 0xFB;
279   memset(&local_sector[sector_position + 1], floppy_get_filler(floppy), sector_length);
280   crc = ccitt_crc16(0xffff, &local_sector[sector_position], sector_length + 1);
281   local_sector[sector_position + sector_length + 1] = (UINT8) (crc >> 8);
282   local_sector[sector_position + sector_length + 2] = (UINT8) (crc >> 0);
283   sector_position += sector_length + 3;
284
285   /* GAP3 (Data GAP) */
286   memset(&local_sector[sector_position], 0xFF, 12);
287   sector_position += 12;
288   memset(&local_sector[sector_position], 0x00, 6);
289   sector_position += 6;
290
291   /*
292     *  start of track data
293     */
294   while(physical_sector < DMK_TOC_LEN/2)
295   {
296      if (physical_sector >= sectors)
297      {
298         /* no more sectors */
299         idam_offset = 0;
300      }
301      else
302      {
303         /* this is a sector */
304         idam_offset = track_position;
305         logical_sector = sector_map[physical_sector];
306
307         /* update IDAM in local sector template */
308         dmk_idam_sector(         &local_sector[0]) = logical_sector;      /* update sector number in sector instance  */
309         crc = ccitt_crc16(0xffff,   &local_sector[0], DMK_IDAM_LEN - 2);   /* and recalculate crc */
310         dmk_idam_set_crc(         &local_sector[0], crc);
311
312         /* write local sector in track doubling every byte (DMK double density mode) */
313         for(i=0; i<local_sector_size; i++)
314            track_data[track_position + i*2] = track_data[track_position + (i*2) + 1] = local_sector[i];
315         track_position += (2 * local_sector_size);
316         if (physical_sector == (sectors-1)) /* on last sector remove GAP3 */
317            track_position -= (2 * DMK_DATA_GAP_LEN);
318      }
319
320      /* write the TOC entry */
321      track_data[physical_sector * 2 + 0] = (UINT8) (idam_offset >> 0);
322      track_data[physical_sector * 2 + 1] = (UINT8) (idam_offset >> 8);
323
324      physical_sector++;
325   }
326
327   /* write track footer GAP4 */
328   assert(max_track_size >= (UINT32)track_position);
329   memset(&track_data[track_position], 0xFF, max_track_size - track_position);
330
331done:
332   if (sector_map)
333      free(sector_map);
334   if (local_sector)
335      free(local_sector);
336
337   return err;
338}
339
340
341
342static int z80ne_dmk_get_heads_per_disk(floppy_image *floppy)
343{
344   return get_dmk_tag(floppy)->heads;
345}
346
347
348
349static int z80ne_dmk_get_tracks_per_disk(floppy_image *floppy)
350{
351   return get_dmk_tag(floppy)->tracks;
352}
353
354
355
356static UINT32 z80ne_dmk_get_track_size(floppy_image *floppy, int head, int track)
357{
358   return get_dmk_tag(floppy)->track_size;
359}
360
361static UINT8 * my_memmem(UINT8 *haystack, size_t haystacklen,
362      UINT8 *needle, size_t needlelen)
363{
364        register UINT8 *h = haystack;
365        register UINT8 *n = needle;
366        register size_t hl = haystacklen;
367        register size_t nl = needlelen;
368        register size_t i;
369
370        if (nl == 0) return haystack;    /* The first occurrence of the empty string is deemed to occur at
371                                                     the beginning of the string.  */
372        if (hl < nl)
373           return NULL;
374
375        for( i = 0; (i < hl) && (i + nl <= hl ); i++ )
376           if (h[i] == *n) /* first match */
377              if ( !memcmp(&h[i]+1, n + 1, nl - 1) )
378                 return (haystack+i);    /* returns a pointer to the substring */
379
380        return (UINT8 *)NULL;    /* not found */
381}
382
383
384
385static floperr_t z80ne_dmk_seek_sector_in_track(floppy_image *floppy, int head, int track, int sector, int sector_is_index, int dirtify, UINT8 **sector_data, UINT32 *sector_length)
386{
387   struct dmk_tag *tag = get_dmk_tag(floppy);
388   floperr_t err;
389   size_t idam_offset = 0;
390   UINT16 calculated_crc;
391   size_t i, j;
392   size_t offs;
393   /*int state;*/
394   UINT8 *track_data;
395   void *track_data_v;
396   size_t track_length;
397   size_t sec_len;
398   UINT8 *local_idam = NULL;
399   int local_idam_size;
400   UINT8 *sec_data;
401
402
403   err = FLOPPY_ERROR_SUCCESS;
404
405   err = floppy_load_track(floppy, head, track, dirtify, &track_data_v, &track_length);
406   if (err)
407      goto done;
408   track_data = (UINT8 *) track_data_v;
409
410   /* set up a local IDAM space */
411   local_idam_size = (DMK_IDAM_LEN +
412                      DMK_ID_GAP_LEN +
413                      DMK_DAM_LEN +
414                      DMK_DATA_GAP_LEN);
415   local_idam = (UINT8*)malloc(local_idam_size);
416   if (!local_idam)
417   {
418      err = FLOPPY_ERROR_OUTOFMEMORY;
419      goto done;
420   }
421   memset(local_idam, 0x00, local_idam_size);
422
423   /* search for matching IDAM */
424   for (i = 0; i < DMK_TOC_LEN / 2; i++)
425   {
426      idam_offset = track_data[i * 2 + 1];
427      idam_offset <<= 8;
428      idam_offset |= track_data[i * 2 + 0];
429      idam_offset &= 0x3FFF;
430
431      if (idam_offset == 0)
432      {
433         /* we've reached the end of the road */
434         i = DMK_TOC_LEN;
435         break;
436      }
437
438      if ((idam_offset + DMK_IDAM_LEN * 2) >= tag->track_size)
439         continue;
440
441      /* overflowing the track? */
442      if ((idam_offset + local_idam_size * 2) >= tag->track_size)
443      {
444         err = FLOPPY_ERROR_SEEKERROR;
445         goto done;
446      }
447
448      /* create a local copy of sector of IDAM */
449      for(j = 0; j < (DMK_IDAM_LEN + DMK_ID_GAP_LEN + DMK_DAM_LEN); j++)
450         local_idam[j] = track_data[idam_offset + j*2];
451
452      /* from now on all operations are done in local copy of IDAM */
453      calculated_crc = ccitt_crc16(0xffff, &local_idam[0], DMK_IDAM_LEN - 2);
454
455      /* check IDAM integrity and check for matching sector */
456      if (calculated_crc == dmk_idam_crc(&local_idam[0]))
457      {
458         if (sector_is_index)
459         {
460            /* the sector is indexed; decrement the index and go */
461            if (sector-- == 0)
462               break;
463         }
464         else
465         {
466            /* check for matching sector ID */
467            if (sector == dmk_idam_sector(&local_idam[0]))
468               break;
469         }
470      }
471   }
472
473   if (i >= DMK_TOC_LEN / 2)
474   {
475      err = FLOPPY_ERROR_SEEKERROR;
476      goto done;
477   }
478
479   /* we found a matching sector ID */
480   /*state = 0;*/
481   offs = idam_offset + 2 * DMK_IDAM_LEN;
482
483   /* find pattern 0x0000FB; this represents the start of a data sector */
484   sec_data = my_memmem(&track_data[offs], 4 * (DMK_ID_GAP_LEN + DMK_DAM_LEN), (UINT8 *)needle_data, 10);
485   if(!sec_data)
486   {
487      /* find pattern 0x0000FA; this represents the start of a deleted data sector (used for system sectors) */
488      sec_data = my_memmem(&track_data[offs], 4 * (DMK_ID_GAP_LEN + DMK_DAM_LEN), (UINT8 *)needle_deleted_data_fa, 10);
489   }
490   if(!sec_data)
491   {
492      /* find pattern 0x0000F8; this represents the start of a deleted data sector (used for system sectors) */
493      sec_data = my_memmem(&track_data[offs], 4 * (DMK_ID_GAP_LEN + DMK_DAM_LEN), (UINT8 *)needle_deleted_data_f8, 10);
494   }
495
496   if(sec_data)
497      sec_data += 10;
498   else
499   {
500      err = FLOPPY_ERROR_SEEKERROR;
501      goto done;
502   }
503   offs = sec_data - &track_data[offs];
504
505   sec_len = 128 << dmk_idam_sectorlength(&local_idam[0]);
506
507   if ((offs + 2 * sec_len) > track_length)
508   {
509      err = FLOPPY_ERROR_INVALIDIMAGE;
510      goto done;
511   }
512
513   if (sector_data)
514      *sector_data = sec_data;
515   if (sector_length)
516      *sector_length = sec_len;
517done:
518   if (local_idam)
519      free(local_idam);
520   return err;
521}
522
523
524
525static floperr_t z80ne_dmk_get_sector_length(floppy_image *floppy, int head, int track, int sector, UINT32 *sector_length)
526{
527   return z80ne_dmk_seek_sector_in_track(floppy, head, track, sector, FALSE, FALSE, NULL, sector_length);
528}
529
530
531
532static floperr_t z80ne_dmk_get_indexed_sector_info(floppy_image *floppy, int head, int track, int sector_index, int *cylinder, int *side, int *sector, UINT32 *sector_length, unsigned long *flags)
533{
534   floperr_t err;
535   size_t idam_offset;
536   UINT8 *track_data;
537   void *track_data_v;
538   unsigned long dam;
539
540   if (sector_index * 2 >= DMK_TOC_LEN)
541      return FLOPPY_ERROR_SEEKERROR;
542
543   err = floppy_load_track(floppy, head, track, FALSE, &track_data_v, NULL);
544   if (err)
545      return err;
546   track_data = (UINT8 *) track_data_v;
547
548   idam_offset = track_data[sector_index * 2 + 1];
549   idam_offset <<= 8;
550   idam_offset |= track_data[sector_index * 2 + 0];
551   idam_offset &= 0x3FFF;
552
553   if (idam_offset == 0)
554      return FLOPPY_ERROR_SEEKERROR;
555
556   /* determine type of data sector */
557   /* find pattern 0x0000FB; this represents the start of a data sector */
558   if ( my_memmem(&track_data[idam_offset+14], 4 * (DMK_ID_GAP_LEN + DMK_DAM_LEN), (UINT8 *)needle_data, 10) )
559      dam = 0;
560   /* find pattern 0x0000FA; this represents the start of a deleted data sector (used for system sectors) */
561   else if ( my_memmem(&track_data[idam_offset+14], 4 * (DMK_ID_GAP_LEN + DMK_DAM_LEN), (UINT8 *)needle_deleted_data_fa, 10) )
562      dam = ID_FLAG_DELETED_DATA;
563   /* find pattern 0x0000F8; this represents the start of a deleted data sector (used for system sectors) */
564   else if ( my_memmem(&track_data[idam_offset+14], 4 * (DMK_ID_GAP_LEN + DMK_DAM_LEN), (UINT8 *)needle_deleted_data_f8, 10) )
565      dam = ID_FLAG_DELETED_DATA;
566   else
567      return FLOPPY_ERROR_SEEKERROR;
568
569   if (cylinder)
570      *cylinder = (&track_data[idam_offset])[2];
571   if (side)
572      *side = (&track_data[idam_offset])[4];
573   if (sector)
574      *sector = (&track_data[idam_offset])[6];
575   if (sector_length)
576      *sector_length = 128 << (&track_data[idam_offset])[8];
577   if (flags)
578      *flags = dam;
579   return FLOPPY_ERROR_SUCCESS;
580}
581
582
583
584static floperr_t internal_z80ne_dmk_read_sector(floppy_image *floppy, int head, int track, int sector, int sector_is_index, void *buffer, size_t buflen)
585{
586   floperr_t err;
587   UINT32 sector_length;
588   UINT16 crc_on_disk;
589   UINT16 calculated_crc;
590   UINT8 *sector_data = NULL;
591   UINT8 *local_sector = NULL;
592   int local_sector_size;
593   int i;
594
595   err = FLOPPY_ERROR_SUCCESS;
596
597   /* get sector length and data pointer */
598   err = z80ne_dmk_seek_sector_in_track(floppy, head, track, sector, sector_is_index, FALSE, &sector_data, &sector_length);
599   if (err)
600      goto done;
601
602   /* set up a local physical sector space (DAM + data + crc + GAP) */
603   local_sector_size = (DMK_DAM_LEN + sector_length + DMK_DATA_CRC_LEN + DMK_DATA_GAP_LEN);
604   local_sector = (UINT8*)malloc(local_sector_size);
605   if (!local_sector)
606   {
607      err = FLOPPY_ERROR_OUTOFMEMORY;
608      goto done;
609   }
610   memset(local_sector, 0x00, local_sector_size);
611
612   /* get sector data */
613   /* create a local copy of sector data including DAM (for crc calculation) */
614   for(i = 0; i < (DMK_DAM_LEN + sector_length + DMK_DATA_CRC_LEN); i++)
615      local_sector[i] = (sector_data - 2)[i*2];
616
617   crc_on_disk = local_sector[sector_length + 0 + 1];
618   crc_on_disk <<= 8;
619   crc_on_disk += local_sector[sector_length + 1 + 1];
620
621   /* crc calculated including DAM */
622   calculated_crc = ccitt_crc16(0xffff, local_sector, sector_length+1);
623   if (calculated_crc != crc_on_disk)
624   {
625      err = FLOPPY_ERROR_INVALIDIMAGE;
626      goto done;
627   }
628
629   /* copy local sector data excluding DAM */
630   memcpy(buffer, local_sector+1, MIN(sector_length, buflen));
631
632   done:
633   if (local_sector)
634      free(local_sector);
635   return err;
636}
637
638
639
640static floperr_t internal_z80ne_dmk_write_sector(floppy_image *floppy, int head, int track, int sector, int sector_is_index, const void *buffer, size_t buflen, int ddam)
641{
642   floperr_t err;
643   UINT32 sector_length;
644   UINT8 *sector_data;
645   UINT16 crc;
646   UINT8 *local_sector = NULL;
647   int local_sector_size;
648   int i;
649
650   err = FLOPPY_ERROR_SUCCESS;
651
652   /* get sector length */
653   err = z80ne_dmk_seek_sector_in_track(floppy, head, track, sector, sector_is_index, TRUE, &sector_data, &sector_length);
654   if (err)
655      goto done;
656
657   /* set up a local physical sector space */
658   local_sector_size = (DMK_DAM_LEN + sector_length + DMK_DATA_CRC_LEN + DMK_DATA_GAP_LEN);
659   local_sector = (UINT8*)malloc(local_sector_size);
660   if (!local_sector)
661   {
662      err = FLOPPY_ERROR_OUTOFMEMORY;
663      goto done;
664   }
665   memset(local_sector, 0x00, local_sector_size);
666   if(!ddam)
667      local_sector[0] = 0xFB;
668   else
669      local_sector[0] = 0xFA;
670
671   if (buflen > sector_length)
672   {
673      err = FLOPPY_ERROR_INTERNAL;
674      goto done;
675   }
676
677   memcpy(&local_sector[1], buffer, buflen);
678
679   crc = ccitt_crc16(0xffff, local_sector, DMK_DAM_LEN + sector_length);
680   local_sector[DMK_DAM_LEN + sector_length + 0] = crc >> 8;
681   local_sector[DMK_DAM_LEN + sector_length + 1] = crc >> 0;
682
683   /* write local sector in track doubling every byte (DMK double density mode) */
684   /* write DAM or DDAM too */
685   sector_data -= 2;
686   for(i=0; i < (DMK_DAM_LEN + sector_length + DMK_DATA_CRC_LEN); i++)
687      sector_data[i*2] = sector_data[(i*2)+1] = local_sector[i];
688
689   done:
690   if (local_sector)
691      free(local_sector);
692   return err;
693}
694
695
696
697static floperr_t z80ne_dmk_read_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen)
698{
699   return internal_z80ne_dmk_read_sector(floppy, head, track, sector, FALSE, buffer, buflen);
700}
701
702static floperr_t z80ne_dmk_write_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam)
703{
704   return internal_z80ne_dmk_write_sector(floppy, head, track, sector, FALSE, buffer, buflen, ddam);
705}
706
707static floperr_t z80ne_dmk_read_indexed_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen)
708{
709   return internal_z80ne_dmk_read_sector(floppy, head, track, sector, TRUE, buffer, buflen);
710}
711
712static floperr_t z80ne_dmk_write_indexed_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam)
713{
714   return internal_z80ne_dmk_write_sector(floppy, head, track, sector, TRUE, buffer, buflen, ddam);
715}
716
717
718
719static void z80ne_dmk_interpret_header(floppy_image *floppy, int *heads, int *tracks, int * sectors, int *track_size)
720{
721   UINT8 header[DMK_HEADER_LEN + DMK_TOC_LEN];
722   int i;
723   int idam_offset;
724   int sects;
725
726   floppy_image_read(floppy, header, 0, DMK_HEADER_LEN + DMK_TOC_LEN);
727
728   if (tracks)
729      *tracks = header[1];
730   if (heads)
731      *heads = (header[4] & 0x10) ? 1 : 2;
732   if (track_size)
733      *track_size = ((int) header[3]) * 0x100 + header[2];
734   if(sectors)
735   {
736      /* count sectors on first track */
737      sects = 0;
738      for (i = 0; i < DMK_TOC_LEN / 2; i++)
739      {
740         idam_offset = header[DMK_HEADER_LEN + i * 2 + 1];
741         idam_offset <<= 8;
742         idam_offset |= header[DMK_HEADER_LEN + i * 2 + 0];
743         idam_offset &= 0x3FFF;
744
745         if (idam_offset == 0)
746            break;
747         else
748            sects++;
749      }
750      *sectors = sects;
751   }
752
753}
754
755
756
757FLOPPY_CONSTRUCT(z80ne_dmk_construct)
758{
759   struct FloppyCallbacks *callbacks;
760   struct dmk_tag *tag;
761   UINT8 header[DMK_HEADER_LEN];
762   int heads, tracks, track_size, sectors, sector_length;
763
764   if (params)
765   {
766      heads         = option_resolution_lookup_int(params, PARAM_HEADS);
767      tracks         = option_resolution_lookup_int(params, PARAM_TRACKS);
768      sectors         = option_resolution_lookup_int(params, PARAM_SECTORS);
769      sector_length   = option_resolution_lookup_int(params, PARAM_SECTOR_LENGTH);
770
771      track_size = z80ne_dmk_min_track_size(sectors, sector_length);
772
773      memset(header, 0, sizeof(header));
774      header[1] = tracks;
775      header[2] = track_size >> 0;
776      header[3] = track_size >> 8;
777      header[4] = (heads == 2) ? 0x00 : 0x10;
778
779      floppy_image_write(floppy, header, 0, sizeof(header));
780   }
781   else
782   {
783      z80ne_dmk_interpret_header(floppy, &heads, &tracks, &sectors, &track_size);
784   }
785
786   tag = (dmk_tag*)floppy_create_tag(floppy, sizeof(struct dmk_tag));
787   if (!tag)
788      return FLOPPY_ERROR_OUTOFMEMORY;
789   tag->heads = heads;
790   tag->track_size = track_size;
791   tag->tracks = tracks;
792
793   callbacks = floppy_callbacks(floppy);
794   callbacks->read_track = z80ne_dmk_read_track;
795   callbacks->write_track = z80ne_dmk_write_track;
796   callbacks->get_track_data_offset = z80ne_dmk_get_track_data_offset;
797   callbacks->format_track = z80ne_dmk_format_track;
798   callbacks->get_heads_per_disk = z80ne_dmk_get_heads_per_disk;
799   callbacks->get_tracks_per_disk = z80ne_dmk_get_tracks_per_disk;
800   callbacks->get_track_size = z80ne_dmk_get_track_size;
801   callbacks->get_sector_length = z80ne_dmk_get_sector_length;
802   callbacks->get_indexed_sector_info = z80ne_dmk_get_indexed_sector_info;
803   callbacks->read_sector = z80ne_dmk_read_sector;
804   callbacks->write_sector = z80ne_dmk_write_sector;
805   callbacks->read_indexed_sector = z80ne_dmk_read_indexed_sector;
806   callbacks->write_indexed_sector = z80ne_dmk_write_indexed_sector;
807
808   return FLOPPY_ERROR_SUCCESS;
809}
810
811
812
813FLOPPY_IDENTIFY(z80ne_dmk_identify)
814{
815   int heads, tracks, sectors, track_size;
816   UINT64 size, expected_size;
817
818   size = floppy_image_size(floppy);
819   z80ne_dmk_interpret_header(floppy, &heads, &tracks, &sectors, &track_size);
820   expected_size = DMK_HEADER_LEN + (heads * tracks * track_size);
821   *vote = (size == expected_size) ? 90 : 0;
822   *vote += (sectors == 10) ? 10 : 0;
823   return FLOPPY_ERROR_SUCCESS;
824}
825
826
827
828/* ----------------------------------------------------------------------- */
829
830FLOPPY_OPTIONS_START( z80ne )
831   /*
832     * Single side
833     * Single density
834     * FM, 125 kbit/s,
835     * 40 tracks, track ID: 0..39
836     * 10 sectors/track, sector ID: 0..9
837     * 256 bytes/sector
838     * Interleave 1 (sector ID sequence on track: 0 5 1 6 2 7 3 8 4 9)
839     * 3125 byte/track unformatted
840     * Rotation speed 300 rpm (5 rps)
841     */
842   FLOPPY_OPTION( z80ne_dmk, "zmk",      "Z80NE DMK disk image",   z80ne_dmk_identify,   z80ne_dmk_construct, NULL,
843      HEADS([1])
844      TRACKS([40])
845      SECTORS([10])
846      SECTOR_LENGTH([256])
847      INTERLEAVE([1])
848      FIRST_SECTOR_ID([0]))
849FLOPPY_OPTIONS_END
src/mess/formats/cbm_tap.c
r11457r11458
1/*
2
3    Tape support for C16 / C64 / VIC20 TAP format
4
5
6    Credits to:
7
8        - Peter Schepers for the informations on C64 formats
9        - Vice Team for the source of their very complete emulator
10        - Attila G. for tap2wav (both the source and the actual tool)
11
12
13    TODO:
14    * to verify and fix (if needed) support for .TAP v2
15    * to implement reading (and logging) the remaining part of the header
16    * to verify if it is more accurate to use a different HIGH_WAVE value
17      when the pulse corresponds to a 0x00 in the .TAP file
18    (...)
19    * (far away in the future) can this code be merged with TZX code?
20
21*/
22
23/* Info based on http://ist.uwaterloo.ca/~schepers/formats/TAP.TXT      */
24/* Please refer to the webpage for the latest version and for a very
25   complete listing of various cart types and their bankswitch tricks   */
26/*
27
28  Designed by Per Hakan Sundell (author of the CCS64 C64 emulator) in 1997,
29this format attempts to duplicate the data stored on a C64  cassette  tape,
30bit for bit. Since it is simply a representation of  the  raw  serial  data
31from a tape, it should handle *any* custom tape loaders that exist.
32
33  The TAP images are generally very large, being a minimum of eight  times,
34and up to sixteen times as large as what a raw PRG file would be.  This  is
35due to the way the data is stored, with each bit of the original  file  now
36being one byte large in the TAP file. The layout is fairly simple,  with  a
37small 14-byte header followed by file data.
38
39    Bytes: $0000-000B: File signature "C64-TAPE-RAW"
40                 000C: TAP version (see below for description)
41                        $00 - Original layout
42                         01 - Updated
43            000D-000F: Future expansion
44            0010-0013: File  data  size  (not  including  this  header,  in
45                       LOW/HIGH format).
46            0014-xxxx: File data
47
48  In TAP version $00 files, each data byte in the file data area represents
49the length of a pulse, when the C64's hardware  will  trigger  again.  This
50pulse length is determined by the following formula:
51
52    pulse length (in seconds) = (8 * data byte) / (clock cycles)
53
54  Therefore, a data value of $2F (47 in decimal) would be:
55
56    (47 * 8) / 985248 = .00038975 seconds.
57
58  A data value of $00 represents an "overflow" condition, any pulse  length
59which is more that 255 * 8 in length.
60
61  The value of "clock cylces" from above  (985248)  is  based  on  the  PAL
62value.  Since  this  file  format  was  developed  in  Europe,   which   is
63predominantly PAL video, this is only logical.  The  NTSC  value  would  be
641022730, which is very close to  the  PAL,  and  therefore  won't  cause  a
65compatability problem converting European and NTSC tapes. I would stick  to
66using the PAL value just in case.
67
68
69  In TAP version $01 files, the data value of  $00  has  been  re-coded  to
70represent values greater than 255 * 8. When a  $00  is  encountered,  three
71bytes will follow which are the actual time (in cycles) of a pulse, and the
72above formula does not apply.  The  three  bytes  are  stored  in  LOW/HIGH
73format.
74
75
76  The actual interpretation of the serial data takes a little more work  to
77explain.  The  typical  ROM  tape  loader  (and  the  turbo  loaders)  will
78initialize a timer with a specified value and start it  counting  down.  If
79either the tape data changes or the timer runs out, an IRQ will occur.  The
80loader will determine which condition caused the  IRQ.  If  the  tape  data
81changed before the timer ran out, we have a short pulse, or a "0"  bit.  If
82the timer ran out first, we have a long pulse, or a  "1"  bit.  Doing  this
83continuously and we decode the entire file.
84
85
86[ Additional notes on v.2:
87
88  I found no documents about this around, but it seems an expansion of the
89format specifically thought for C16 tapes. In a .TAP version 2, each byte
90only stores informations on half of the wave...
91Unfortunately, I have no such a .tap file to test, so my implementation
92below could be not working.  FP ]
93*/
94
95#include "emu.h"
96#include "formats/cbm_tap.h"
97
98
99#define CBM_WAV_FREQUENCY   44100
100
101/* Systems */
102#define C64      0
103#define VIC20   1
104#define C16      2
105
106/* Video standards */
107#define PAL      0
108#define NTSC   1
109
110/* Frequencies in [Hz] to determine the length of each pulse */
111#define C64_PAL      123156      /*  985248 / 8 */
112#define C64_NTSC   127841      /* 1022727 / 8 */
113#define VIC20_PAL   138551      /* 1108405 / 8 */
114#define VIC20_NTSC   127841      /* 1022727 / 8 */
115#define C16_PAL      110840      /*  886724 / 8 */
116#define C16_NTSC   111860      /*  894886 / 8 */
117
118#define PAUSE (CBM_WAV_FREQUENCY / 50)      /* tap2wav uses this value for 0x00 in .TAP v0, instead of 0x100 */
119
120/* These values do not really matter, as long as the produced pulses
121  go above & below 0. However, for documentation purpose it would be
122  nice to find out which values were used by Commodore tapes. I was
123  not able to find any reference on the subject. */
124#define WAVE_HIGH      (0x5a9e >> 1)
125#define WAVE_LOW      -(0x5a9e >> 1)
126#define WAVE_PAUSE      0x80
127
128#define CBM_HEADER_SIZE 20
129
130static INT16   wave_data = 0;
131static int     len;
132
133
134/* This in fact gives the number of samples for half of the pulse */
135INLINE int tap_data_to_samplecount(int data, int frequency)
136{
137//  return (int) (0.5 * (0.5 + (((double)CBM_WAV_FREQUENCY / frequency) * (double)data)));      // MESS TZX formula
138   return (int) (0.5 * (((double)CBM_WAV_FREQUENCY / frequency) * (double)((data) + 0.5)));   // tap2wav formula
139}
140
141/* The version with parameters could be handy if we decide to implement a
142different values for byte == 0x00 below (the WAVE_PAUSE) as tap2wav does. */
143#if 0
144static void toggle_wave_data(int low, int high)
145{
146   wave_data = (wave_data == low) ? high : low;
147}
148#endif
149
150static void toggle_wave_data(void )
151{
152   wave_data = (wave_data == WAVE_HIGH) ? WAVE_LOW : WAVE_HIGH;
153}
154
155static void cbm_output_wave( INT16 **buffer, int length )
156{
157   if (buffer == NULL)
158      return;
159
160   for( ; length > 0; length-- )
161   {
162      **buffer = wave_data;
163      *buffer = *buffer + 1;
164   }
165}
166
167
168static int cbm_tap_do_work( INT16 **buffer, int length, const UINT8 *data )
169{
170   int i, j = 0;
171   int size = 0;
172
173   int version = data[0x0c];
174   int system = data[0x0d];
175   int video_standard = data[0x0e];
176   int tap_frequency = 0;
177
178   int byte_samples = 0;
179   UINT8 over_pulse_bytes[3] = {0 , 0, 0 };
180   int over_pulse_length = 0;
181   /* These waveamp_* values are currently stored but not used.
182      Further investigations are needed to find real pulse amplitude
183      in Commodore tapes. Implementation here would follow */
184   /* int waveamp_high, waveamp_low; */
185
186   /* Log .TAP info but only once */
187   if (!(buffer == NULL))
188   {
189      logerror("TAP version    : %d\n", version);
190      logerror("Machine type   : %d\n", system);
191      logerror("Video standard : %d\n", video_standard);
192      logerror("Tape frequency : %d\n", (tap_frequency) << 3);
193   }
194
195
196   /* is this a supported version? */
197   if ((version < 0) || (version > 2))
198   {
199      logerror("Unsupported .tap version: %d \n", version);
200      return -1;
201   }
202
203   /* is the .tap file corrupted? */
204   if ((data == NULL) || (length <= CBM_HEADER_SIZE))
205      return -1;
206
207
208   /* read the frequency from the .tap header */
209    switch (system)
210    {
211        case VIC20:
212            tap_frequency = (video_standard == NTSC) ? VIC20_NTSC : VIC20_PAL;
213            break;
214
215        case C16:
216            tap_frequency = (video_standard == NTSC) ? C16_NTSC : C16_PAL;
217            break;
218
219        case C64:
220        default:
221            tap_frequency = (video_standard == NTSC) ? C64_NTSC : C64_PAL;
222            break;
223    }
224
225
226   for (i = CBM_HEADER_SIZE; i < length; i++)
227   {
228      UINT8 byte = data[i];
229
230      /* .TAP v0 */
231      /* Here is simple:
232          if byte is != 0 -> length = byte
233          otherwise -> length =  0x100 (i.e. 0xff + 1) */
234      if (!version)
235      {
236         if (byte != 0x00)
237         {
238            byte_samples = tap_data_to_samplecount(byte, tap_frequency);
239            /* waveamp_high = WAVE_HIGH; */
240         }
241         else
242         {
243            byte_samples = tap_data_to_samplecount(PAUSE, tap_frequency);   // tap2wav value
244//              byte_samples = tap_data_to_samplecount(0x100, tap_frequency);   // vice value
245            /* waveamp_high = WAVE_PAUSE; */
246         }
247         /* waveamp_low = WAVE_LOW; */
248      }
249
250      /* .TAP v1 & v2 */
251      /* Here is a bit more complicate:
252          if byte is != 0 -> length = byte
253          otherwise -> the length of the pulse is stored as a 24bit value in the 3 bytes after the 0.
254          See below for comments on the implementation of this mechanism */
255      if (version)
256      {
257         if ((byte != 0x00) && !j)
258         {
259            byte_samples = tap_data_to_samplecount(byte, tap_frequency);
260            /* waveamp_high = WAVE_HIGH; */
261         }
262         else
263         {
264            /* If we have a long pulse close to the end of the .TAP, check that bytes still
265                  to be read are enough to complete it. */
266            if (length - i + j >= 4)
267            {
268               /* Here we read the 3 following bytes, using an index j
269                      j = 0 -> The 0x00 byte: we simply skip everything and go on
270                      j = 1,2 -> The 1st and 2nd bytes after 0x00: we store them and go on
271                      j = 3 -> The final byte of the pulse length: we store it,
272                      and then we pass to finally output the wave */
273               if (j > 0)
274               {
275                  over_pulse_bytes[j-1] = byte;
276                  j += 1;
277
278                  if (j >= 4)
279                  {
280                     over_pulse_length = ((over_pulse_bytes[2] << 16) | (over_pulse_bytes[1] << 8) | over_pulse_bytes[0]) >> 3;
281                     byte_samples = tap_data_to_samplecount(over_pulse_length, tap_frequency);
282                     /* waveamp_high = WAVE_PAUSE; */
283                     j = 0;
284                  }
285               }
286               else
287               {
288                  j += 1;
289                  logerror("Found a 00 byte close to the end of the .tap file.\n");
290                  logerror("This is not allowed by the format specs. \n");
291                  logerror("Check if your .tap file got corrupted when you created it!\n");
292               }
293            }
294            else j = 1;
295         }
296         /* waveamp_low = WAVE_LOW; */
297      }
298
299      if (j == 0)
300      {
301         cbm_output_wave( buffer, byte_samples );
302         size += byte_samples;
303//          toggle_wave_data(waveamp_low, waveamp_high);
304         toggle_wave_data();
305         if (version < 2)
306         {
307            cbm_output_wave( buffer, byte_samples );
308            size += byte_samples;
309//              toggle_wave_data(waveamp_low, waveamp_high);
310            toggle_wave_data();
311         }
312      }
313   }
314
315   return size;
316}
317
318
319static int cbm_tap_to_wav_size( const UINT8 *tapdata, int taplen )
320{
321   int size = cbm_tap_do_work(NULL, taplen, tapdata);
322   len = taplen;
323
324   return size;
325}
326
327static int cbm_tap_fill_wave( INT16 *buffer, int length, UINT8 *bytes )
328{
329   INT16 *p = buffer;
330
331   return cbm_tap_do_work(&p, len, (const UINT8 *)bytes);
332}
333
334
335
336
337static const struct CassetteLegacyWaveFiller cbm_legacy_fill_wave = {
338   cbm_tap_fill_wave,      /* fill_wave */
339   -1,                  /* chunk_size */
340   0,                  /* chunk_samples */
341   cbm_tap_to_wav_size,   /* chunk_sample_calc */
342   CBM_WAV_FREQUENCY,      /* sample_frequency */
343   0,                  /* header_samples */
344   0                  /* trailer_samples */
345};
346
347
348static casserr_t cbm_cassette_identify( cassette_image *cassette, struct CassetteOptions *opts )
349{
350   return cassette_legacy_identify( cassette, opts, &cbm_legacy_fill_wave );
351}
352
353
354static casserr_t cbm_cassette_load( cassette_image *cassette )
355{
356   return cassette_legacy_construct( cassette, &cbm_legacy_fill_wave );
357}
358
359
360static const struct CassetteFormat cbm_tap_cassette_format = {
361   "tap",
362   cbm_cassette_identify,
363   cbm_cassette_load,
364   NULL
365};
366
367
368CASSETTE_FORMATLIST_START(cbm_cassette_formats)
369   CASSETTE_FORMAT(cbm_tap_cassette_format)
370CASSETTE_FORMATLIST_END
src/mess/formats/oric_tap.h
r11457r11458
1/*********************************************************************
2
3    oric_tap.h
4
5    Format code for Oric TAP (*.tap) files
6
7*********************************************************************/
8
9#ifndef ORIC_TAP_H
10#define ORIC_TAP_H
11
12#include "imagedev/cassimg.h"
13
14CASSETTE_FORMATLIST_EXTERN(oric_cassette_formats);
15
16#endif /* ORIC_TAP_H */
src/mess/formats/atom_tap.h
r11457r11458
1/*********************************************************************
2
3    formats/atom_tap.h
4
5    Cassette code for Acorn Atom tap files
6
7*********************************************************************/
8
9#pragma once
10
11#ifndef __ATOM_TAP__
12#define __ATOM_TAP__
13
14#include "imagedev/cassimg.h"
15
16extern const struct CassetteFormat atom_tap_format;
17
18#endif
src/mess/formats/z80ne_dsk.h
r11457r11458
1/*********************************************************************
2
3    formats/z80ne_dsk.h
4
5    Nuova Elettronica Z80NE disk images
6
7*********************************************************************/
8
9#ifndef Z80NE_DSK_H
10#define Z80NE_DSK_H
11
12#include "imagedev/flopimg.h"
13
14
15/**************************************************************************/
16
17FLOPPY_OPTIONS_EXTERN(z80ne);
18
19FLOPPY_IDENTIFY(z80ne_dmk_identify);
20FLOPPY_CONSTRUCT(z80ne_dmk_construct);
21
22#endif /* Z80NE_DSK_H */
src/mess/formats/cbm_tap.h
r11457r11458
1/*********************************************************************
2
3    cbm_tap.h
4
5    Format code for CBM .tap files
6
7*********************************************************************/
8
9#ifndef CBM_TAP_H
10#define CBM_TAP_H
11
12#include "imagedev/cassimg.h"
13
14CASSETTE_FORMATLIST_EXTERN(cbm_cassette_formats);
15
16#endif /*  CBM_TAP_H */
src/mess/formats/coupedsk.c
r11457r11458
1/*************************************************************************
2
3    formats/coupedsk.c
4
5    SAM Coupe disk image formats
6
7**************************************************************************/
8
9#include "coupedsk.h"
10#include "formats/basicdsk.h"
11
12
13#define SAD_HEADER_LEN  22
14#define SAD_SIGNATURE   "Aley's disk backup"
15
16#define SDF_TRACKSIZE   (512 * 12)
17
18
19struct sdf_tag
20{
21   UINT8 heads;
22   UINT8 tracks;
23};
24
25
26
27/*************************************
28 *
29 *  MGT disk image format
30 *
31 *************************************/
32
33/*
34    Straight rip without any header
35
36    Data:
37
38    Side 0 Track 0
39    Side 1 Track 0
40    Side 0 Track 1
41    Side 1 Track 1
42    ...
43*/
44
45
46FLOPPY_CONSTRUCT( coupe_mgt_construct )
47{
48   struct basicdsk_geometry geometry;
49
50   memset(&geometry, 0, sizeof(geometry));
51   geometry.heads = 2;
52   geometry.tracks = 80;
53   geometry.sector_length = 512;
54   geometry.first_sector_id = 1;
55
56   if (params)
57   {
58      /* create */
59      geometry.sectors = option_resolution_lookup_int(params, PARAM_SECTORS);
60   }
61   else
62   {
63      /* load */
64      UINT64 size = floppy_image_size(floppy);
65
66      /* verify size */
67      if (size != 737280 && size != 819200)
68         return FLOPPY_ERROR_INVALIDIMAGE;
69
70      geometry.sectors = size / 81920;
71
72   }
73
74   return basicdsk_construct(floppy, &geometry);
75}
76
77
78FLOPPY_IDENTIFY( coupe_mgt_identify )
79{
80   UINT64 size;
81
82   size = floppy_image_size(floppy);
83
84   *vote = (size == 737280 || size == 819200) ? 100 : 0;
85   return FLOPPY_ERROR_SUCCESS;
86}
87
88
89
90/*************************************
91 *
92 *  SAD disk image format
93 *
94 *************************************/
95
96/*
97    Header (22 bytes):
98
99    0-17 "Aley's disk backup"
100    18   number of sides
101    19   number of tracks
102    20   number of sectors per track
103    21   sector size divided by 64
104
105    Data:
106
107    Side 0 Track 0
108    Side 0 Track 1
109    Side 0 Track 2
110    ...
111    Side 1 Track 0
112    Side 1 Track 1
113    ...
114*/
115
116
117static UINT64 coupe_sad_translate_offset(floppy_image *floppy,
118   const struct basicdsk_geometry *geom, int track, int head, int sector)
119{
120   return head * geom->tracks * geom->sectors + geom->sectors * track + sector;
121}
122
123
124static void coupe_sad_interpret_header(floppy_image *floppy,
125   int *heads, int *tracks, int *sectors, int *sector_size)
126{
127   UINT8 header[SAD_HEADER_LEN];
128
129   floppy_image_read(floppy, header, 0, SAD_HEADER_LEN);
130
131   *heads = header[18];
132   *tracks = header[19];
133   *sectors = header[20];
134   *sector_size = header[21] << 6;
135}
136
137
138FLOPPY_CONSTRUCT( coupe_sad_construct )
139{
140   struct basicdsk_geometry geometry;
141   int heads, tracks, sectors, sector_length;
142
143   memset(&geometry, 0, sizeof(geometry));
144
145   if (params)
146   {
147      /* create */
148      UINT8 header[SAD_HEADER_LEN];
149
150      /* get format options */
151      heads = option_resolution_lookup_int(params, PARAM_HEADS);
152      tracks = option_resolution_lookup_int(params, PARAM_TRACKS);
153      sectors = option_resolution_lookup_int(params, PARAM_SECTORS);
154      sector_length = option_resolution_lookup_int(params, PARAM_SECTOR_LENGTH);
155
156      /* build the header */
157      memset(header, 0, SAD_HEADER_LEN);
158      memcpy(header, SAD_SIGNATURE, 18);
159      header[18] = heads;
160      header[19] = tracks;
161      header[20] = sectors;
162      header[21] = sector_length >> 6;
163
164      /* and write it to disk */
165      floppy_image_write(floppy, header, 0, sizeof(header));
166   }
167   else
168   {
169      /* load */
170      coupe_sad_interpret_header(floppy, &heads, &tracks, &sectors, &sector_length);
171   }
172
173   /* fill in the data */
174   geometry.offset = SAD_HEADER_LEN;
175   geometry.heads = heads;
176   geometry.tracks = tracks;
177   geometry.sectors = sectors;
178   geometry.first_sector_id = 1;
179   geometry.sector_length = sector_length;
180   geometry.translate_offset = coupe_sad_translate_offset;
181
182   return basicdsk_construct(floppy, &geometry);
183}
184
185
186FLOPPY_IDENTIFY( coupe_sad_identify )
187{
188   int heads, tracks, sectors, sector_size;
189   UINT64 size, calculated_size;
190
191   size = floppy_image_size(floppy);
192
193   /* read values from SAD header */
194   coupe_sad_interpret_header(floppy, &heads, &tracks, &sectors, &sector_size);
195
196   /* calculate expected disk image size */
197   calculated_size = SAD_HEADER_LEN + heads * tracks * sectors * sector_size;
198
199   *vote = (size == calculated_size) ? 100 : 0;
200   return FLOPPY_ERROR_SUCCESS;
201}
202
203
204
205/*************************************
206 *
207 *  SDF disk image format
208 *
209 *  TODO: wd17xx status codes are
210 *        currently ignored
211 *
212 *************************************/
213
214static int coupe_sdf_get_heads_per_disk(floppy_image *floppy)
215{
216   struct sdf_tag *tag = (sdf_tag *)floppy_tag(floppy);
217   return tag->heads;
218}
219
220
221static int coupe_sdf_get_tracks_per_disk(floppy_image *floppy)
222{
223   struct sdf_tag *tag = (sdf_tag *)floppy_tag(floppy);
224   return tag->tracks;
225}
226
227
228static UINT32 coupe_sdf_get_track_size(floppy_image *floppy, int head, int track)
229{
230   return SDF_TRACKSIZE;
231}
232
233
234static floperr_t coupe_sdf_get_offset(floppy_image *floppy,
235   int head, int track, UINT64 *offset)
236{
237   struct sdf_tag *tag = (sdf_tag *)floppy_tag(floppy);
238
239   if (head > tag->heads || track > tag->tracks)
240      return FLOPPY_ERROR_SEEKERROR;
241
242   *offset = (head * tag->tracks + track) * SDF_TRACKSIZE;
243
244   return FLOPPY_ERROR_SUCCESS;
245}
246
247
248static floperr_t coupe_sdf_read_track(floppy_image *floppy,
249   int head, int track, UINT64 offset, void *buffer, size_t buflen)
250{
251   floperr_t err;
252   UINT64 track_offset;
253
254   /* get the offset to this track */
255   err = coupe_sdf_get_offset(floppy, head, track, &track_offset);
256   if (err) return err;
257
258   /* read track data into buffer */
259   floppy_image_read(floppy, buffer, offset + track_offset, buflen);
260
261   return FLOPPY_ERROR_SUCCESS;
262}
263
264
265static floperr_t coupe_sdf_write_track(floppy_image *floppy,
266   int head, int track, UINT64 offset, const void *buffer, size_t buflen)
267{
268   floperr_t err;
269   UINT64 track_offset;
270
271   /* get the offset to this track */
272   err = coupe_sdf_get_offset(floppy, head, track, &track_offset);
273   if (err) return err;
274
275   /* write buffer to image */
276   floppy_image_write(floppy, buffer, offset + track_offset, buflen);
277
278   return FLOPPY_ERROR_SUCCESS;
279}
280
281
282static floperr_t coupe_sdf_get_sector_offset(floppy_image *floppy,
283   int head, int track, int sector, UINT64 *offset)
284{
285   floperr_t err;
286   UINT8 buffer[SDF_TRACKSIZE];
287   int i, buffer_pos = 1;
288
289   /* get track data */
290   err = coupe_sdf_read_track(floppy, head, track, 0, &buffer, SDF_TRACKSIZE);
291   if (err) return err;
292
293   /* check if the sector is available in this track */
294   if (sector >= buffer[0])
295      return FLOPPY_ERROR_SEEKERROR;
296
297   /* find the right sector in this track */
298   for (i = 0; i < buffer[0]; i++)
299   {
300      int sector_number = buffer[buffer_pos + 4];
301
302      if (sector_number - 1 == sector)
303      {
304         *offset = buffer_pos;
305         return FLOPPY_ERROR_SUCCESS;
306      }
307
308      buffer_pos += (128 << buffer[buffer_pos + 5]) + 8;
309   }
310
311   return FLOPPY_ERROR_INVALIDIMAGE;
312}
313
314
315static floperr_t coupe_sdf_get_total_sector_offset(floppy_image *floppy,
316   int head, int track, int sector, UINT64 *offset)
317{
318   floperr_t err;
319   UINT64 track_offset, sector_offset;
320
321   /* get offset to the track start */
322   err = coupe_sdf_get_offset(floppy, head, track, &track_offset);
323   if (err) return err;
324
325   /* get offset to the start of the sector */
326   err = coupe_sdf_get_sector_offset(floppy, head, track, sector, &sector_offset);
327   if (err) return err;
328
329   *offset = track_offset + sector_offset;
330
331   return FLOPPY_ERROR_SUCCESS;
332}
333
334
335static floperr_t coupe_sdf_get_sector_length(floppy_image *floppy,
336   int head, int track, int sector, UINT32 *sector_length)
337{
338   floperr_t err;
339   UINT8 buffer[SDF_TRACKSIZE];
340   UINT64 offset;
341
342   /* get track data */
343   err = coupe_sdf_read_track(floppy, head, track, 0, &buffer, SDF_TRACKSIZE);
344   if (err) return err;
345
346   /* get offset to the start of the sector */
347   err = coupe_sdf_get_sector_offset(floppy, head, track, sector, &offset);
348   if (err) return err;
349
350   /* get size */
351   *sector_length = 128 << buffer[offset + 5];
352
353   return FLOPPY_ERROR_SUCCESS;
354}
355
356
357static floperr_t coupe_sdf_get_indexed_sector_info(floppy_image *floppy,
358   int head, int track, int sector_index,
359   int *cylinder, int *side, int *sector, UINT32 *sector_length,
360   unsigned long *flags)
361{
362   floperr_t err;
363   UINT8 buffer[SDF_TRACKSIZE];
364   UINT64 offset;
365
366   /* get track data */
367   err = coupe_sdf_read_track(floppy, head, track, 0, &buffer, SDF_TRACKSIZE);
368   if (err) return err;
369
370   /* get offset to the start of the sector */
371   err = coupe_sdf_get_sector_offset(floppy, head, track, sector_index, &offset);
372   if (err) return err;
373
374   /* extract data */
375   if (cylinder)      *cylinder      = buffer[offset + 2];
376   if (side)          *side          = buffer[offset + 3];
377   if (sector)        *sector        = buffer[offset + 4];
378   if (sector_length) *sector_length = 128 << buffer[offset + 5];
379   if (flags)         *flags        = 0;   /* TODO: read DAM or DDAM and determine flags */
380
381   return FLOPPY_ERROR_SUCCESS;
382}
383
384
385static floperr_t coupe_sdf_read_indexed_sector(floppy_image *floppy,
386   int head, int track, int sector, void *buffer, size_t buflen)
387{
388   floperr_t err;
389   UINT64 offset;
390
391   err = coupe_sdf_get_total_sector_offset(floppy, head, track, sector, &offset);
392   if (err) return err;
393
394   /* read sector data into buffer */
395   floppy_image_read(floppy, buffer, offset + 8, buflen);
396
397   return FLOPPY_ERROR_SUCCESS;
398}
399
400
401static floperr_t coupe_sdf_write_indexed_sector(floppy_image *floppy,
402   int head, int track, int sector, const void *buffer, size_t buflen, int ddam)
403{
404   floperr_t err;
405   UINT64 offset;
406
407   err = coupe_sdf_get_total_sector_offset(floppy, head, track, sector, &offset);
408   if (err) return err;
409
410   /* write buffer into image */
411   floppy_image_write(floppy, buffer, offset + 8, buflen);
412
413   return FLOPPY_ERROR_SUCCESS;
414}
415
416
417static void coupe_sdf_interpret_header(floppy_image *floppy, int *heads, int *tracks)
418{
419   UINT64 size = floppy_image_size(floppy);
420
421   if (size % SDF_TRACKSIZE == 0)
422   {
423      *heads = 2;
424      *tracks = size / (SDF_TRACKSIZE * 2);
425   }
426   else
427   {
428      *heads = 0;
429      *tracks = 0;
430   }
431}
432
433
434FLOPPY_CONSTRUCT( coupe_sdf_construct )
435{
436   struct FloppyCallbacks *callbacks;
437   struct sdf_tag *tag;
438   int heads, tracks;
439
440   if (params)
441   {
442      /* create */
443      return FLOPPY_ERROR_UNSUPPORTED;
444   }
445   else
446   {
447      /* load */
448      coupe_sdf_interpret_header(floppy, &heads, &tracks);
449   }
450
451   tag = (sdf_tag *)floppy_create_tag(floppy, sizeof(struct sdf_tag));
452
453   if (!tag)
454      return FLOPPY_ERROR_OUTOFMEMORY;
455
456   tag->heads = heads;
457   tag->tracks = tracks;
458
459   callbacks = floppy_callbacks(floppy);
460   callbacks->read_track = coupe_sdf_read_track;
461   callbacks->write_track = coupe_sdf_write_track;
462   callbacks->get_heads_per_disk = coupe_sdf_get_heads_per_disk;
463   callbacks->get_tracks_per_disk = coupe_sdf_get_tracks_per_disk;
464   callbacks->get_track_size = coupe_sdf_get_track_size;
465   callbacks->get_sector_length = coupe_sdf_get_sector_length;
466   callbacks->read_indexed_sector = coupe_sdf_read_indexed_sector;
467   callbacks->write_indexed_sector = coupe_sdf_write_indexed_sector;
468   callbacks->get_indexed_sector_info = coupe_sdf_get_indexed_sector_info;
469
470   return FLOPPY_ERROR_SUCCESS;
471}
472
473
474FLOPPY_IDENTIFY( coupe_sdf_identify )
475{
476   int heads, tracks;
477
478   /* read header values */
479   coupe_sdf_interpret_header(floppy, &heads, &tracks);
480
481   /* check for sensible values */
482   if (heads > 0 && heads < 3 && tracks > 0 && tracks < 84)
483      *vote = 100;
484   else
485      *vote = 0;
486
487   return FLOPPY_ERROR_SUCCESS;
488}
src/mess/formats/coupedsk.h
r11457r11458
1/*************************************************************************
2
3    formats/coupedsk.h
4
5    SAM Coupe disk image formats
6
7**************************************************************************/
8
9#ifndef __COUPEDSK_H__
10#define __COUPEDSK_H__
11
12#include "imagedev/flopimg.h"
13
14
15FLOPPY_CONSTRUCT( coupe_mgt_construct );
16FLOPPY_IDENTIFY( coupe_mgt_identify );
17FLOPPY_CONSTRUCT( coupe_sad_construct );
18FLOPPY_IDENTIFY( coupe_sad_identify );
19FLOPPY_CONSTRUCT( coupe_sdf_construct );
20FLOPPY_IDENTIFY( coupe_sdf_identify );
21
22
23#endif /* __COUPEDSK_H__ */
src/mess/formats/svi_dsk.c
r11457r11458
1/*********************************************************************
2
3    formats/svi_dsk.c
4
5    SVI318 disk images
6
7*********************************************************************/
8
9#include <string.h>
10
11#include "formats/svi_dsk.h"
12#include "formats/basicdsk.h"
13#include "emu.h"
14
15static FLOPPY_IDENTIFY(svi_dsk_identify)
16{
17   *vote = ((floppy_image_size(floppy) == (172032)) || (floppy_image_size(floppy) == (346112))) ? 100 : 0;
18   return FLOPPY_ERROR_SUCCESS;
19}
20
21static int svi_get_heads_per_disk(floppy_image *floppy)
22{
23   return (floppy_image_size(floppy) == (172032)) ? 1 : 2;
24}
25
26static int svi_get_tracks_per_disk(floppy_image *floppy)
27{
28   return 40;
29}
30
31static UINT64 svi_translate_offset(floppy_image *floppy,
32    int track, int head, int sector)
33{
34   UINT64 o;
35   if ((track==0) && (head==0))
36      o = sector*128;
37   else
38      o = ((track*((floppy_image_size(floppy) == (172032)) ? 1 : 2)+head)*17+sector)*256-2048; /* (17*256)-(18*128)=2048 */
39
40   return o;
41}
42
43static floperr_t get_offset(floppy_image *floppy, int head, int track, int sector, int sector_is_index, UINT64 *offset)
44{
45   UINT64 offs;
46   /* translate the sector to a raw sector */
47   if (!sector_is_index)
48   {
49      sector -= 1;
50   }
51   /* check to see if we are out of range */
52   if ((head < 0) || (head >= ((floppy_image_size(floppy) == (172032)) ? 1 : 2)) || (track < 0) || (track >= 40)
53         || (sector < 0) || (sector >= ((head==0 && track==0) ? 18 : 17)))
54      return FLOPPY_ERROR_SEEKERROR;
55
56   offs = svi_translate_offset(floppy, track, head, sector);
57   if (offset)
58      *offset = offs;
59   return FLOPPY_ERROR_SUCCESS;
60}
61
62
63
64static floperr_t internal_svi_read_sector(floppy_image *floppy, int head, int track, int sector, int sector_is_index, void *buffer, size_t buflen)
65{
66   UINT64 offset;
67   floperr_t err;
68   err = get_offset(floppy, head, track, sector, sector_is_index, &offset);
69   if (err)
70      return err;
71
72   floppy_image_read(floppy, buffer, offset, buflen);
73   return FLOPPY_ERROR_SUCCESS;
74}
75
76
77
78static floperr_t internal_svi_write_sector(floppy_image *floppy, int head, int track, int sector, int sector_is_index, const void *buffer, size_t buflen, int ddam)
79{
80   UINT64 offset;
81   floperr_t err;
82
83   err = get_offset(floppy, head, track, sector, sector_is_index, &offset);
84   if (err)
85      return err;
86
87   floppy_image_write(floppy, buffer, offset, buflen);
88   return FLOPPY_ERROR_SUCCESS;
89}
90
91
92
93static floperr_t svi_read_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen)
94{
95   return internal_svi_read_sector(floppy, head, track, sector, FALSE, buffer, buflen);
96}
97
98static floperr_t svi_write_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam)
99{
100   return internal_svi_write_sector(floppy, head, track, sector, FALSE, buffer, buflen, ddam);
101}
102
103static floperr_t svi_read_indexed_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen)
104{
105   return internal_svi_read_sector(floppy, head, track, sector, TRUE, buffer, buflen);
106}
107
108static floperr_t svi_write_indexed_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam)
109{
110   return internal_svi_write_sector(floppy, head, track, sector, TRUE, buffer, buflen, ddam);
111}
112
113static floperr_t svi_get_sector_length(floppy_image *floppy, int head, int track, int sector, UINT32 *sector_length)
114{
115   floperr_t err;
116   err = get_offset(floppy, head, track, sector, FALSE, NULL);
117   if (err)
118      return err;
119
120   if (sector_length) {
121      *sector_length = (head==0 && track==0) ? 128 : 256;
122   }
123   return FLOPPY_ERROR_SUCCESS;
124}
125
126
127
128static floperr_t svi_get_indexed_sector_info(floppy_image *floppy, int head, int track, int sector_index, int *cylinder, int *side, int *sector, UINT32 *sector_length, unsigned long *flags)
129{
130   sector_index += 1;
131   if (cylinder)
132      *cylinder = track;
133   if (side)
134      *side = head;
135   if (sector)
136      *sector = sector_index;
137   if (flags)
138      /* TODO: read DAM or DDAM and determine flags */
139      *flags = 0;
140   return svi_get_sector_length(floppy, head, track, sector_index, sector_length);
141}
142
143
144static FLOPPY_CONSTRUCT(svi_dsk_construct)
145{
146   struct FloppyCallbacks *callbacks;
147   callbacks = floppy_callbacks(floppy);
148   callbacks->read_sector = svi_read_sector;
149   callbacks->write_sector = svi_write_sector;
150   callbacks->read_indexed_sector = svi_read_indexed_sector;
151   callbacks->write_indexed_sector = svi_write_indexed_sector;
152   callbacks->get_sector_length = svi_get_sector_length;
153   callbacks->get_heads_per_disk = svi_get_heads_per_disk;
154   callbacks->get_tracks_per_disk = svi_get_tracks_per_disk;
155   callbacks->get_indexed_sector_info = svi_get_indexed_sector_info;
156
157   return FLOPPY_ERROR_SUCCESS;
158}
159
160
161
162/* ----------------------------------------------------------------------- */
163
164FLOPPY_OPTIONS_START( svi318 )
165   FLOPPY_OPTION( svi_dsk, "dsk", "SVI-318 floppy disk image",   svi_dsk_identify, svi_dsk_construct, NULL, NULL)
166   FLOPPY_OPTION( svi_cpm, "dsk", "SVI-728 DSDD CP/M disk image", basicdsk_identify_default, basicdsk_construct_default, NULL,
167      HEADS([2])
168      TRACKS([40])
169      SECTORS([17])
170      SECTOR_LENGTH([256])
171      FIRST_SECTOR_ID([1]))
172FLOPPY_OPTIONS_END
src/mess/formats/ami_dsk.c
r11457r11458
1/*********************************************************************
2
3    formats/ami_dsk.c
4
5    Amiga disk images
6
7*********************************************************************/
8
9
10#include <string.h>
11
12#include "formats/ami_dsk.h"
13#include "formats/basicdsk.h"
14
15
16
17/*****************************************************************************
18 Amiga floppy core functions
19*****************************************************************************/
20
21
22static FLOPPY_IDENTIFY( amiga_dsk_identify )
23{
24   UINT64 size;
25
26   *vote = 100;
27
28   /* first check the size of the image */
29   size = floppy_image_size(floppy);
30   if ((size != 901120) && (size != 1802240))
31      *vote = 0;
32
33   return FLOPPY_ERROR_SUCCESS;
34}
35
36
37static FLOPPY_CONSTRUCT( amiga_dsk_construct )
38{
39   struct basicdsk_geometry geometry;
40
41   /* setup geometry with standard values */
42   memset(&geometry, 0, sizeof(geometry));
43   geometry.heads = 2;
44   geometry.tracks = 80;
45   geometry.first_sector_id = 0;
46   geometry.sector_length = 512;
47
48   if (params)
49   {
50      /* create */
51      geometry.sectors = option_resolution_lookup_int(params, PARAM_SECTORS);
52   }
53   else
54   {
55      /* open */
56      UINT64 size = floppy_image_size(floppy);
57      geometry.sectors = size/512/80/2;
58      if (geometry.sectors != 11 && geometry.sectors != 22)
59         return FLOPPY_ERROR_INVALIDIMAGE;
60   }
61
62   return basicdsk_construct(floppy, &geometry);
63}
64
65
66
67/*****************************************************************************
68 Amiga floppy options
69*****************************************************************************/
70
71
72FLOPPY_OPTIONS_START( amiga )
73   FLOPPY_OPTION(
74      ami_dsk,
75      "adf",
76      "Amiga floppy disk image",
77      amiga_dsk_identify,
78      amiga_dsk_construct,
79      NULL,
80      HEADS([2])
81      TRACKS([80])
82      SECTORS([11]/22)
83   )
84FLOPPY_OPTIONS_END
85
86FLOPPY_OPTIONS_START( amiga_only )
87   FLOPPY_OPTION(
88      ami_dsk,
89      "adf",
90      "Amiga floppy disk image",
91      amiga_dsk_identify,
92      amiga_dsk_construct,
93      NULL,
94      HEADS([2])
95      TRACKS([80])
96      SECTORS([11]/22)
97   )
98FLOPPY_OPTIONS_END0
src/mess/formats/svi_dsk.h
r11457r11458
1/*********************************************************************
2
3    formats/svi_dsk.h
4
5    SVI318 disk images
6
7*********************************************************************/
8
9#ifndef SVI_DSK_H
10#define SVI_DSK_H
11
12#include "imagedev/flopimg.h"
13
14/**************************************************************************/
15
16FLOPPY_OPTIONS_EXTERN(svi318);
17
18#endif /* SVI_DSK_H */
src/mess/formats/trs_dsk.c
r11457r11458
1#include <string.h>
2#include "emu.h"
3#include "formats/trs_dsk.h"
4#include "formats/basicdsk.h"
5#include "formats/coco_dsk.h"
6#include "imagedev/flopdrv.h"
7
8/* -----------------------------------------------------------------------
9 * JV1 (Jeff Vavasour 1) format
10 *
11 * Used by Jeff Vavasour's TRS-80 Emulators
12 *
13 * Very straight basic disk; 1 head, 10 sectors, 256 sector length
14 * ----------------------------------------------------------------------- */
15
16#define TRS80_JV1_HEADS            1
17#define TRS80_JV1_SECTORS         10
18#define TRS80_JV1_SECTORLENGTH      256
19#define TRS80_JV1_FIRSTSECTORID      0
20
21static FLOPPY_IDENTIFY( trs80_jv1_identify )
22{
23   UINT64 size;
24   size = floppy_image_size(floppy);
25   *vote = (size % (TRS80_JV1_HEADS * TRS80_JV1_SECTORS * TRS80_JV1_SECTORLENGTH))
26      ? 0 : 100;
27   return FLOPPY_ERROR_SUCCESS;
28}
29
30
31static UINT64 trs80_jv1_get_ddam(floppy_image *floppy, const struct basicdsk_geometry *geom, int track, int head, int sector)
32{
33   // directory track is protected
34   if ((track==17) && (head==0)) {
35      return ID_FLAG_DELETED_DATA;
36   }
37   return 0;
38}
39
40static FLOPPY_CONSTRUCT( trs80_jv1_construct )
41{
42   struct basicdsk_geometry geometry;
43
44   memset(&geometry, 0, sizeof(geometry));
45   geometry.heads            = TRS80_JV1_HEADS;
46   geometry.sectors         = TRS80_JV1_SECTORS;
47   geometry.first_sector_id   = TRS80_JV1_FIRSTSECTORID;
48   geometry.sector_length      = TRS80_JV1_SECTORLENGTH;
49   geometry.get_ddam         = trs80_jv1_get_ddam;
50
51   if (params)
52   {
53      /* create */
54      geometry.tracks = option_resolution_lookup_int(params, PARAM_TRACKS);
55   }
56   else
57   {
58      /* load */
59      geometry.tracks = (int) (floppy_image_size(floppy) / geometry.heads
60         / geometry.sectors / geometry.sector_length);
61   }
62   return basicdsk_construct(floppy, &geometry);
63}
64
65
66
67/* ----------------------------------------------------------------------- */
68
69FLOPPY_OPTIONS_START( trs80 )
70   FLOPPY_OPTION( trs80_jv1, "dsk",         "TRS-80 JV1 disk image",   trs80_jv1_identify,   trs80_jv1_construct, NULL,
71      TRACKS([35]-255))
72   FLOPPY_OPTION( trs80_dmk, "dsk,dmk",      "TRS-80 DMK disk image",   coco_dmk_identify,   coco_dmk_construct, NULL,
73      HEADS([1]-2)
74      TRACKS([35]-255)
75      SECTORS(1-[10]-18)
76      SECTOR_LENGTH(128/[256]/512/1024/2048/4096/8192)
77      INTERLEAVE(0-[6]-17)
78      FIRST_SECTOR_ID([0]-1))
79FLOPPY_OPTIONS_END
src/mess/formats/apf_apt.c
r11457r11458
1/* .APT tape images */
2
3
4#include "formats/apf_apt.h"
5
6
7#define WAVEENTRY_LOW  -32768
8#define WAVEENTRY_HIGH  32767
9#define WAVEENTRY_NULL  0
10
11/* frequency of wave */
12#define APF_WAV_FREQUENCY   11050
13
14#define APF_APT_BIT_0_LENGTH ((int)(APF_WAV_FREQUENCY*0.0005))
15#define APF_APT_BIT_1_LENGTH ((int)(APF_WAV_FREQUENCY*0.001))
16
17struct apf_t
18{
19   int cassette_length;
20};
21static apf_t apf;
22
23
24/* 500 microsecond of bit 0 and 1000 microsecond of bit 1 */
25
26static INT16 *apf_emit_level(INT16 *p, int count, INT16 wave_state)
27{
28   int i;
29
30   for (i=0; i<count; i++)
31   {
32      *(p++) = wave_state;
33   }
34   return p;
35}
36
37/* 4 periods at 1200Hz */
38static INT16* apf_output_bit(INT16 *p, UINT8 b)
39{
40   if (b)
41   {
42      p = apf_emit_level(p, (APF_APT_BIT_1_LENGTH>>1), WAVEENTRY_HIGH);
43      p = apf_emit_level(p, (APF_APT_BIT_1_LENGTH>>1), WAVEENTRY_LOW);
44   }
45   else
46   {
47      p = apf_emit_level(p, (APF_APT_BIT_0_LENGTH>>1), WAVEENTRY_HIGH);
48      p = apf_emit_level(p, (APF_APT_BIT_0_LENGTH>>1), WAVEENTRY_LOW);
49   }
50
51   return p;
52}
53
54static int apf_get_bit_size_in_samples(UINT8 b)
55{
56   if (b)
57   {
58      return APF_APT_BIT_1_LENGTH;
59   }
60
61   return APF_APT_BIT_0_LENGTH;
62}
63
64
65/*************************************************************************************/
66
67/* each bit in the data represents a "1" or "0" waveform */
68static int apf_cassette_calculate_size_in_samples(const UINT8 *bytes, int length)
69{
70   unsigned i;
71   int size;
72   int b;
73   UINT8 data;
74
75   size = 0;
76   apf.cassette_length = length;
77
78
79   for (i=0; i<length; i++)
80   {
81      data = bytes[i];
82
83      for (b=0; b<8; b++)
84      {
85         size += apf_get_bit_size_in_samples((data>>7) & 0x01);
86         data = data<<1;
87      }
88   }
89
90   return size;
91}
92
93/*************************************************************************************/
94
95static int apf_cassette_fill_wave(INT16 *buffer, int length, UINT8 *bytes)
96{
97   int i;
98   INT16 *p;
99   UINT8 data;
100   int b;
101
102   p = buffer;
103
104   for (i=0; i<apf.cassette_length; i++)
105   {
106      data = bytes[i];
107      for (b=0; b<8; b++)
108      {
109         p = apf_output_bit(p,(data>>7) & 0x01);
110         data = data<<1;
111      }
112   }
113   return p - buffer;
114}
115
116
117
118static const struct CassetteLegacyWaveFiller apf_legacy_fill_wave =
119{
120   apf_cassette_fill_wave,               /* fill_wave */
121   -1,                              /* chunk_size */
122   0,                              /* chunk_samples */
123   apf_cassette_calculate_size_in_samples,   /* chunk_sample_calc */
124   APF_WAV_FREQUENCY,                  /* sample_frequency */
125   0,                              /* header_samples */
126   0                              /* trailer_samples */
127};
128
129
130
131static casserr_t apf_apt_identify(cassette_image *cassette, struct CassetteOptions *opts)
132{
133   return cassette_legacy_identify(cassette, opts, &apf_legacy_fill_wave);
134}
135
136
137
138static casserr_t apf_apt_load(cassette_image *cassette)
139{
140   return cassette_legacy_construct(cassette, &apf_legacy_fill_wave);
141}
142
143
144
145static const struct CassetteFormat apf_apt_format =
146{
147   "apt",
148   apf_apt_identify,
149   apf_apt_load,
150   NULL
151};
152
153
154
155CASSETTE_FORMATLIST_START(apf_cassette_formats)
156   CASSETTE_FORMAT(apf_apt_format)
157CASSETTE_FORMATLIST_END
src/mess/formats/ami_dsk.h
r11457r11458
1/*********************************************************************
2
3    formats/ami_dsk.h
4
5    Amiga disk images
6
7*********************************************************************/
8
9#ifndef AMI_DSK_H_
10#define AMI_DSK_H_
11
12#include "imagedev/flopimg.h"
13
14FLOPPY_OPTIONS_EXTERN(amiga);
15FLOPPY_OPTIONS_EXTERN(amiga_only);
16
17#endif /*AMI_DSK_H_*/
src/mess/formats/trs_dsk.h
r11457r11458
1/*********************************************************************
2
3    formats/trs_dsk.h
4
5    TRS-80 disk images
6
7*********************************************************************/
8
9#ifndef TRS_DSK_H
10#define TRS_DSK_H
11
12#include "imagedev/flopimg.h"
13
14/**************************************************************************/
15
16FLOPPY_OPTIONS_EXTERN( trs80 );
17
18#endif /* TRS_DSK_H */
src/mess/formats/fm7_cas.c
r11457r11458
1/*
2 * Fujitsu FM-7 series cassette handling
3 */
4
5#include "emu.h"
6#include "fm7_cas.h"
7
8#define WAVE_HIGH       0x5a9e
9#define WAVE_LOW      -0x5a9e
10
11static int cas_size;
12
13static int fm7_fill_wave(INT16* buffer, UINT8 high, UINT8 low, int sample_pos)
14{
15   UINT16 data = (high << 8) + low;
16   int sample_count = 0;
17   int x = 0;
18   int count = (data & 0x7fff);
19
20   if(data & 0x8000)
21   {
22      for(x=0;x<count;x++)
23      {
24         if(buffer)
25            buffer[sample_pos+x] = WAVE_HIGH;
26      }
27   }
28   else
29   {
30      for(x=0;x<count;x++)
31      {
32         if(buffer)
33            buffer[sample_pos+x] = WAVE_LOW;
34      }
35   }
36
37   sample_count += count;
38   return sample_count;
39}
40
41static int fm7_handle_t77(INT16* buffer, const UINT8* casdata)
42{
43   int sample_count = 0;
44   int data_pos = 16;
45
46   if(memcmp(casdata, "XM7 TAPE IMAGE 0",16))  // header check
47      return -1;
48
49   while(data_pos < cas_size)
50   {
51      sample_count += fm7_fill_wave(buffer,casdata[data_pos],casdata[data_pos+1],sample_count);
52      data_pos+=2;
53   }
54
55   return sample_count;
56}
57
58/*******************************************************************
59   Calculate the number of samples needed for this tape image
60********************************************************************/
61static int fm7_cas_to_wav_size (const UINT8 *casdata, int caslen)
62{
63   cas_size = caslen;
64
65   return fm7_handle_t77(NULL,casdata);
66}
67
68/*******************************************************************
69   Generate samples for the tape image
70********************************************************************/
71static int fm7_cas_fill_wave(INT16 *buffer, int sample_count, UINT8 *bytes)
72{
73   return fm7_handle_t77(buffer,bytes);
74}
75
76static const struct CassetteLegacyWaveFiller fm7_legacy_fill_wave =
77{
78   fm7_cas_fill_wave,                  /* fill_wave */
79   -1,                              /* chunk_size */
80   0,                              /* chunk_samples */
81   fm7_cas_to_wav_size,               /* chunk_sample_calc */
82   125000,                           /* sample_frequency */
83   0,                              /* header_samples */
84   0                              /* trailer_samples */
85};
86
87static casserr_t fm7_cas_identify(cassette_image *cassette, struct CassetteOptions *opts)
88{
89   return cassette_legacy_identify(cassette, opts, &fm7_legacy_fill_wave);
90}
91
92
93
94static casserr_t fm7_cas_load(cassette_image *cassette)
95{
96   return cassette_legacy_construct(cassette, &fm7_legacy_fill_wave);
97}
98
99
100static const struct CassetteFormat fm7_cassette_format = {
101   "t77",
102   fm7_cas_identify,
103   fm7_cas_load,
104   NULL
105};
106
107CASSETTE_FORMATLIST_START(fm7_cassette_formats)
108   CASSETTE_FORMAT(fm7_cassette_format)
109CASSETTE_FORMATLIST_END
src/mess/formats/apf_apt.h
r11457r11458
1#ifndef APFCAS_H
2#define APFCAS_H
3
4#include "imagedev/cassimg.h"
5
6CASSETTE_FORMATLIST_EXTERN(apf_cassette_formats);
7
8
9#endif /* APFCAS_H */
src/mess/formats/fm7_cas.h
r11457r11458
1/*
2 * Fujitsu FM-7 cassette format handling
3 */
4
5#ifndef FM7_CAS_H_
6#define FM7_CAS_H_
7
8#include "imagedev/cassimg.h"
9
10CASSETTE_FORMATLIST_EXTERN( fm7_cassette_formats );
11
12#endif /*FM7_CAS_H_*/
src/mess/formats/orao_cas.c
r11457r11458
1/*
2
3    Tape support for Orao  TAP format
4
5*/
6#include "emu.h"
7#include "formats/orao_cas.h"
8
9
10#define ORAO_WAV_FREQUENCY   44100
11#define WAVE_HIGH      -32768
12#define WAVE_LOW      0
13
14#define ORAO_WAVE_ONE   24
15#define ORAO_WAVE_ZERO   11
16
17#define ORAO_HEADER_SIZE 360
18
19static INT16   wave_data;
20static int     len;
21
22static void orao_output_wave( INT16 **buffer, int length ) {
23   if ( buffer == NULL ) {
24      return;
25   }
26
27   for( ; length > 0; length-- ) {
28      **buffer = wave_data;
29      *buffer = *buffer + 1;
30   }
31}
32static int orao_cas_to_wav_size( const UINT8 *casdata, int caslen ) {
33   int i,j,size;
34   UINT8 b;
35
36   if (casdata == NULL) return -1;
37   if (caslen <= ORAO_HEADER_SIZE) {
38      return -1;
39   }
40   size = 0;
41   for (i=ORAO_HEADER_SIZE;i<caslen-ORAO_HEADER_SIZE;i++) {
42      for (j=0;j<8;j++) {
43         b = (casdata[i] >> j) & 1;
44         if (b==0) {
45            size += 2*ORAO_WAVE_ZERO;
46         } else {
47            size += 2*ORAO_WAVE_ONE;
48         }
49      }
50   }
51   len = caslen;
52   return size;
53}
54
55static int orao_cas_fill_wave( INT16 *buffer, int length, UINT8 *bytes ) {
56   int i,j,size;
57   UINT8 b;
58   size = 0;
59   if (bytes == NULL) return -1;
60   for (i=ORAO_HEADER_SIZE;i<len-ORAO_HEADER_SIZE;i++) {
61      for (j=0;j<8;j++) {
62         b = (bytes[i] >> j) & 1;
63         if (b==0) {
64            wave_data = WAVE_LOW;
65            orao_output_wave(&buffer,ORAO_WAVE_ZERO);
66            wave_data = WAVE_HIGH;
67            orao_output_wave(&buffer,ORAO_WAVE_ZERO);
68            size += 2 *ORAO_WAVE_ZERO;
69         } else {
70            wave_data = WAVE_LOW;
71            orao_output_wave(&buffer,ORAO_WAVE_ONE);
72            wave_data = WAVE_HIGH;
73            orao_output_wave(&buffer,ORAO_WAVE_ONE);
74            size += 2 * ORAO_WAVE_ONE;
75         }
76      }
77   }
78
79   return size;
80}
81
82
83
84
85static const struct CassetteLegacyWaveFiller orao_legacy_fill_wave = {
86   orao_cas_fill_wave,         /* fill_wave */
87   -1,               /* chunk_size */
88   0,               /* chunk_samples */
89   orao_cas_to_wav_size,         /* chunk_sample_calc */
90   ORAO_WAV_FREQUENCY,         /* sample_frequency */
91   0,               /* header_samples */
92   0               /* trailer_samples */
93};
94
95
96
97static casserr_t orao_cassette_identify( cassette_image *cassette, struct CassetteOptions *opts ) {
98   return cassette_legacy_identify( cassette, opts, &orao_legacy_fill_wave );
99}
100
101
102
103static casserr_t orao_cassette_load( cassette_image *cassette ) {
104   return cassette_legacy_construct( cassette, &orao_legacy_fill_wave );
105}
106
107
108
109static const struct CassetteFormat orao_cassette_format = {
110   "tap",
111   orao_cassette_identify,
112   orao_cassette_load,
113   NULL
114};
115
116
117CASSETTE_FORMATLIST_START(orao_cassette_formats)
118   CASSETTE_FORMAT(orao_cassette_format)
119CASSETTE_FORMATLIST_END
120
121
src/mess/formats/orao_cas.h
r11457r11458
1/*
2
3       orao_cas.h
4
5       Format code for orao cassette files
6*/
7
8#ifndef ORAO_CAS_H
9#define ORAO_CAS_H
10
11#include "imagedev/cassimg.h"
12
13CASSETTE_FORMATLIST_EXTERN(orao_cassette_formats);
14
15#endif /* ORAO_CAS_H */
src/mess/formats/p6001_cas.c
r11457r11458
1/*
2 * Nec PC-6001 cassette format handling
3 */
4
5#include "emu.h"
6#include "p6001_cas.h"
7
8#define WAVE_HIGH       0x5a9e
9#define WAVE_LOW      -0x5a9e
10
11static int cas_size;
12
13static int pc6001_fill_wave(INT16* buffer, UINT8 data, int sample_pos)
14{
15   int x;
16   int sample_count = 0;
17
18   // one byte = 8 samples
19   for(x=0;x<8;x++)
20   {
21      if(buffer)
22         buffer[sample_pos+x] = ((data >> (7-x)) & 1) ? WAVE_HIGH : WAVE_LOW;
23   }
24
25   sample_count+=8;
26   return sample_count;
27}
28
29static int pc6001_handle_cas(INT16* buffer, const UINT8* casdata)
30{
31   int sample_count = 0;
32   int data_pos = 0;
33
34   while(sample_count < cas_size)
35   {
36      sample_count += pc6001_fill_wave(buffer,casdata[data_pos],sample_count);
37      data_pos++;
38   }
39
40   return sample_count;
41}
42
43/*******************************************************************
44   Calculate the number of samples needed for this tape image
45********************************************************************/
46static int pc6001_cas_to_wav_size (const UINT8 *casdata, int caslen)
47{
48   cas_size = caslen*8;
49
50   return pc6001_handle_cas(NULL,casdata);
51}
52
53/*******************************************************************
54   Generate samples for the tape image
55********************************************************************/
56static int pc6001_cas_fill_wave(INT16 *buffer, int sample_count, UINT8 *bytes)
57{
58   return pc6001_handle_cas(buffer,bytes);
59}
60
61static const struct CassetteLegacyWaveFiller pc6001_legacy_fill_wave =
62{
63   pc6001_cas_fill_wave,               /* fill_wave */
64   -1,                              /* chunk_size */
65   0,                              /* chunk_samples */
66   pc6001_cas_to_wav_size,               /* chunk_sample_calc */
67   8000,                           /* sample_frequency */
68   0,                              /* header_samples */
69   0                              /* trailer_samples */
70};
71
72static casserr_t pc6001_cas_identify(cassette_image *cassette, struct CassetteOptions *opts)
73{
74   return cassette_legacy_identify(cassette, opts, &pc6001_legacy_fill_wave);
75}
76
77
78
79static casserr_t pc6001_cas_load(cassette_image *cassette)
80{
81   return cassette_legacy_construct(cassette, &pc6001_legacy_fill_wave);
82}
83
84
85static const struct CassetteFormat pc6001_cassette_format = {
86   "cas",
87   pc6001_cas_identify,
88   pc6001_cas_load,
89   NULL
90};
91
92CASSETTE_FORMATLIST_START(pc6001_cassette_formats)
93   CASSETTE_FORMAT(pc6001_cassette_format)
94CASSETTE_FORMATLIST_END
src/mess/formats/ti99_dsk.c
r11457r11458
1/*********************************************************************
2 *
3 * formats/ti99_dsk.c
4 *
5 * TI99 and Geneve disk images
6 * Sector Dump and Track Dump format
7 *
8 * used by TI-99/4, TI-99/4A, TI-99/8, SGCPU ("TI-99/4P"), and Geneve
9 *
10 * The Sector Dump Format is also known as v9t9 (named after the first TI
11 * emulator to use this format). It is a contiguous sequence of sector contents
12 * without track data. The first sector of the disk is located at the start of
13 * the image, while the last sector is at its end.
14 *
15 * The Track Dump Format is also known as pc99 (again, named after the first
16 * TI emulator to use this format). It is a contiguous sequence of track
17 * contents, containing all information including address marks and CRC, but it
18 * does not contain clock signals. Therefore, the format requires that the
19 * address marks be at the same positions within each track.
20 *
21 * Both formats allow for a broad range of medium sizes. All sectors are 256
22 * bytes long. The most common formats are 9 sectors per track, single-sided,
23 * 40 tracks, which yields 90 KiB of sector data (known as SSSD), and 18
24 * sectors per track, double-sided, and 40 tracks, which is 360 KiB (known as
25 * DSDD). There are rare occurances of 8/16 sectors/track
26 * (prototypical TI double-density controller) and 35 track media. Newer
27 * controllers and ROMs allow for up to 36 sectors per track and 80 tracks on
28 * both sides, which is 2,88 MiB (DSHD80).
29 *
30 * Different to earlier implementations, these format implementations provide
31 * full track read/write capabilities. For the SDF format, the missing track
32 * information is recreated, and a common interleave is assumed.
33 *
34 * Note that in principle, writing tracks may change track sizes.
35 * As neither one of the formats contains meta-information about track lengths,
36 * and as the image consists of a contiguous sequence of the tracks, we
37 * cannot allow different track sizes currently. Accordingly, the track size
38 * cannot be changed after the image has been created by imgtool.
39 * Hence, also sector count per tracks and recording type (FM or MFM) cannot be
40 * changed without breaking the image.
41 * For example, MFM disks with 18 sectors per track can only be reformatted to
42 * MFM/18. Moreover, while this could be handled in a future revision,
43 * the number of tracks should also remain unchanged.
44 *
45 * You can use blank image files (filled with zeros or arbitrary data) and use
46 * the Disk Manager cartridge to properly format them. Also, this can be done
47 * by imgtool. The image file must match the format length (representing the
48 * space on the physical medium).
49 *
50 * 80 track drive handling
51 *
52 * For the TI system there may be a situation where 40 track disks are used
53 * in 80 track drives. In these cases, the disk driver applies double-stepping,
54 * that is, it advances the head twice in order to seek the next track. We
55 * emulate this by dividing the track number by 2, which means that two tracks
56 * are mapped to one (0,1 -> 0; 2,3 -> 1; 4,5 -> 2; ...; 78,79 -> 39)
57 *
58 * Technical detail: The size of the track must be given before actually
59 * starting write_track as the controller implementation uses buffered
60 * transfer.
61 *
62 * Michael Zapf, Feb 2010
63 *
64 * TODO: Link to imgtool. imgtool still uses its own format creation
65 *
66 * FIXME: If image is broken with good first track and defect higher tracks,
67 *        tdf_guess_geometry fails to define a geometry. The guessing should
68 *        always be done.
69 ********************************************************************/
70
71#include <string.h>
72#include <assert.h>
73#include <time.h>
74
75#include "osdepend.h"
76#include "imageutl.h"
77#include "formats/ti99_dsk.h"
78
79#define SECTOR_SIZE 256
80#define TI99_IDAM_LENGTH 7
81
82/*
83    Determines whether we are using 80 track drives. This variable is
84    necessary unless we get information about the dip
85    switch settings. It is set by the disk controller implementations
86    (bwg, hfdc, ti_fdc).
87*/
88static int use_80_track_drives = FALSE;
89
90struct ti99dsk_geometry
91{
92   UINT8 sides;
93   UINT8 tracksperside;
94   UINT8 secspertrack;
95   UINT8 density;
96};
97
98struct ti99dsk_tag
99{
100   int heads;
101   int tracks;
102   int sectors;
103   int track_size;  // Complete track; to be calculated
104   int format;
105   int first_idam;
106   int dam_offset;
107};
108
109/*
110    Parametrizes the format to use 80 track drives. For TI systems we
111    need to be able to emulate 40 track drives and 80 track drives.
112    This is set in the configuration, but the format has no direct access.
113    So as a preliminary solution we use this global function which sets the
114    flag. This function is called by the disk controller implementations
115    (bwg, hfdc, ti_fdc).
116*/
117void ti99_set_80_track_drives(int use80)
118{
119   use_80_track_drives = use80;
120}
121
122#define TI99_DSK_TAG   "ti99dsktag"
123#define TI99DSK_BLOCKNOTFOUND -1
124
125/*
126    Searches a block containing number * byte, starting at the given
127    position. Returns the position of the first byte of the block.
128*/
129static int find_block(const UINT8 *buffer, int start, int stop, UINT8 byte, size_t number)
130{
131   int i = start;
132   size_t current = number;
133   while (i < stop && current > 0)
134   {
135      if (buffer[i++] != byte)
136      {
137         current = number;
138      }
139      else
140      {
141         current--;
142      }
143   }
144   if (current==0)
145   {
146      return i - number;
147   }
148   else
149      return TI99DSK_BLOCKNOTFOUND;
150}
151
152/*
153    Queried by flopdrv. Instead of the image tracks, we return the tracks
154    of the floppy drive. We assume we always have two heads.
155*/
156static int ti99_get_heads_per_disk(floppy_image *floppy)
157{
158//  struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy);
159//  return tag->heads;
160   return 2;
161}
162
163/*
164    Queried by flopdrv. Instead of the image tracks, we return the tracks
165    of the floppy drive.
166*/
167static int ti99_get_tracks_per_disk(floppy_image *floppy)
168{
169   int drivetracks = 42;
170
171   // This may fail on startup; therefore we use a special function in
172   // in the floppy controller implementations to explicitly set the geometry.
173   if (use_80_track_drives)
174   {
175      drivetracks = 83;
176   }
177   return drivetracks;
178}
179
180static floperr_t ti99_get_sector_length(floppy_image *floppy, int head, int track, int sector, UINT32 *sector_length)
181{
182   *sector_length=SECTOR_SIZE;
183   return FLOPPY_ERROR_SUCCESS;
184}
185
186/*
187    Return the track size. We do not allow different track
188    sizes, so we store the track size in the tag.
189*/
190static UINT32 ti99_get_track_size(floppy_image *floppy, int head, int track)
191{
192   struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy);
193   return tag->track_size;
194}
195
196/*
197static void create_vib(UINT8 *sector0, option_resolution *params)
198{
199    const char *name = "UNNAMED   ";
200    int sides, tracksperside,secspertrack,totalsectors,ausize,allocbytes,i;
201
202    enum { NAME=0, TOTALSECS=10, SECSPERTRACK=11, SIG=12, PROT=16, TRACKS=17, SIDES=18, DENSITY=19, EXT=20, ALLOC=56 };
203
204    sides       = option_resolution_lookup_int(params, PARAM_HEADS);
205    tracksperside   = option_resolution_lookup_int(params, PARAM_TRACKS);
206    secspertrack    = option_resolution_lookup_int(params, PARAM_SECTORS);
207
208    totalsectors = sides * tracksperside * secspertrack;
209
210    memcpy(&sector0[NAME], name, 10);
211
212    sector0[TOTALSECS] = (totalsectors>>8)&0xff;
213    sector0[TOTALSECS+1] = (totalsectors&0xff);
214
215    sector0[SECSPERTRACK] = secspertrack;
216    sector0[SIG] = 'D';
217    sector0[SIG+1] = 'S';
218    sector0[SIG+2] = 'K';
219
220    sector0[PROT] = ' ';
221    sector0[TRACKS] = tracksperside;
222    sector0[SIDES] = sides;
223    sector0[DENSITY] = (secspertrack<10)? 0x01 : 0x02;
224
225    for (i=EXT; i < ALLOC; i++)
226        sector0[i] = 0;
227
228    ausize = (totalsectors / 1441) + 1;
229
230    allocbytes = totalsectors / ausize / 8;
231
232    for (i=ALLOC; i < ALLOC+allocbytes; i++)
233        sector0[i] = 0;
234
235    while (i++ < SECTOR_SIZE)
236        sector0[i] = 0xff;
237}
238*/
239/* =========================================================================
240             Sector dump format
241   =========================================================================*/
242
243static floperr_t ti99_sdf_read_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen);
244static floperr_t ti99_sdf_write_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam);
245
246/* -----------------------------------------------------------------------
247 * Guess the geometry of the sector dump disk.
248 *
249 * Sector size is always 256 bytes.
250 *
251 * Common formats:
252 * 90 KiB = 40 tracks / 1 side / 9 sectors = SSSD
253 * 180 KiB = 40 tracks / 2 side / 9 sectors = DSSD (most common)
254 *         = 40 tracks / 1 side / 18 sectors = SSDD (rare)
255 *         = 80 tracks / 1 side / 9 sectors = SSSD80 (rare)
256 * 360 KiB = 40 tracks / 2 side / 18 sectors = DSDD (most common)
257 *         = 80 tracks / 2 side / 9 sectors = DSSD80 (rare)
258 *         = 80 tracks / 1 side / 18 sectors = SSDD80 (rare)
259 *         = 40 tracks / 1 side / 36 sectors = SSHD (rare)
260 * 720 KiB = 80 tracks / 2 side / 18 sectors = DSDD80 (most common)
261 * 1440 KiB= 80 tracks / 2 side / 36 sectors = DSHD80 (most common)
262 *
263 * Moreover, there may be formats with 35 tracks (ancient) and 8 sectors/track.
264 * We only check for the 8 sector formats.
265 * 160 KiB = 40 tracks / 1 side / 16 sectors = SSDD8
266 * 320 KiB = 40 tracks / 2 side / 16 sectors = DSDD8
267 *
268 * The Volume Information Block may contain suitable information, but not
269 * before the disk is formatted and initialized.
270 *
271 * returns 100 if recognized, 0 if not recognized
272 * ----------------------------------------------------------------------- */
273
274static int ti99_sdf_guess_geometry(floppy_image *floppy, UINT64 size,
275   struct ti99dsk_geometry *geometry)
276{
277   int totsecs;
278   typedef struct ti99_vib
279   {
280      char   name[10];      // volume name (10 characters, pad with spaces)
281      UINT8   totsecsMSB;      // disk length in sectors (big-endian) (usually 360, 720 or 1440)
282      UINT8   totsecsLSB;
283      UINT8   secspertrack;   // sectors per track (usually 9 (FM) or 18 (MFM))
284      UINT8   id[3];         // String "DSK"
285      UINT8   protection;      // 'P' if disk is protected, ' ' otherwise.
286      UINT8   tracksperside;   // tracks per side (usually 40)
287      UINT8   sides;         // sides (1 or 2)
288      UINT8   density;      // 0,1 (FM) or 2,3,4 (MFM)
289      UINT8   res[36];      // Empty for traditional disks, or up to 3 directory pointers
290      UINT8   abm[200];      // allocation bitmap: a 1 for each sector in use (sector 0 is LSBit of byte 0,
291                        // sector 7 is MSBit of byte 0, sector 8 is LSBit of byte 1, etc.)
292   } ti99_vib;
293
294   ti99_vib vib;
295   UINT32 file_size;
296   struct ti99dsk_geometry dummy_geometry;
297
298   if (geometry)
299      memset(geometry, 0, sizeof(*geometry));
300   else
301      geometry = &dummy_geometry;
302
303   if (size > 0xFFFFFFFF)
304      return 0;
305
306   file_size = (UINT32) size;
307
308   // Read the Volume Information Block
309   floppy_image_read(floppy, &vib, 0, sizeof(vib));
310
311   // If we have read the sector successfully, let us parse it
312   totsecs = (vib.totsecsMSB << 8) | vib.totsecsLSB;
313   geometry->secspertrack = vib.secspertrack;
314   if (geometry->secspertrack == 0)
315      // Some images might be like this, because the original SSSD
316      // TI controller always assumes 9.
317      geometry->secspertrack = 9;
318   geometry->tracksperside = vib.tracksperside;
319   if (geometry->tracksperside == 0)
320      // Some images are like this, because the original SSSD TI controller
321      // always assumes 40.
322      geometry->tracksperside = 40;
323   geometry->sides = vib.sides;
324   if (geometry->sides == 0)
325      // Some images are like this, because the original SSSD TI controller
326      // always assumes that tracks beyond 40 are on side 2. */
327      geometry->sides = totsecs / (geometry->secspertrack * geometry->tracksperside);
328   geometry->density = vib.density;
329   // check that the format makes sense
330   if (((geometry->secspertrack * geometry->tracksperside * geometry->sides) == totsecs)
331      && (geometry->density <= 4) && (totsecs >= 2) && (! memcmp(vib.id, "DSK", 3))
332      && (file_size == totsecs*256))
333   {
334      logerror("SDF/VIB consistent; tracks = %d, heads = %d, sectors = %d\n", geometry->tracksperside, geometry->sides, geometry->secspertrack);
335      return 100;
336   }
337   logerror("SDF/VIB not consistent; guessing format\n");
338
339   // So that was not consistent. We guess the size from the file size
340   // and assume that the VIB did not contain reliable data. For the
341   // ambiguous case we choose the most common format.
342
343   switch (file_size)
344   {
345   case 1*40*9*256:
346      // 90kbytes: SSSD
347   case 0:
348   /*default:*/
349      geometry->sides = 1;
350      geometry->tracksperside = 40;
351      geometry->secspertrack = 9;
352      geometry->density = 1;
353      break;
354
355   case 2*40*9*256:
356      // 180kbytes: either DSSD or 18-sector-per-track SSDD.
357      // We assume DSSD since DSSD is more common and is supported by
358      // the original TI SD disk controller.
359      geometry->sides = 2;
360      geometry->tracksperside = 40;
361      geometry->secspertrack = 9;
362      geometry->density = 1;
363      break;
364
365   case 1*40*16*256:
366      // 160kbytes: 16-sector-per-track SSDD (standard format for TI
367      // DD disk controller prototype, and the TI hexbus disk
368      // controller?) */
369      geometry->sides = 1;
370      geometry->tracksperside = 40;
371      geometry->secspertrack = 16;
372      geometry->density = 2;
373      break;
374
375   case 2*40*16*256:
376      // 320kbytes: 16-sector-per-track DSDD (standard format for TI
377      // DD disk controller prototype, and TI hexbus disk
378      // controller?)
379      geometry->sides = 2;
380      geometry->tracksperside = 40;
381      geometry->secspertrack = 16;
382      geometry->density = 2;
383      break;
384
385   case 2*40*18*256:
386      //  360kbytes: 18-sector-per-track DSDD (standard format for most
387      // third-party DD disk controllers, but reportedly not supported by
388      // the original TI DD disk controller prototype)
389      geometry->sides = 2;
390      geometry->tracksperside = 40;
391      geometry->secspertrack = 18;
392      geometry->density = 2;
393      break;
394
395   case 2*80*18*256:
396      // 720kbytes: 18-sector-per-track 80-track DSDD (Myarc only)
397      geometry->sides = 2;
398      geometry->tracksperside = 80;
399      geometry->secspertrack = 18;
400      geometry->density = 2;
401      break;
402
403   case 2*80*36*256:
404      // 1.44Mbytes: DSHD (Myarc only)
405      geometry->sides = 2;
406      geometry->tracksperside = 80;
407      geometry->secspertrack = 36;
408      geometry->density = 3;
409      break;
410
411   default:
412      logerror("Unrecognized disk image geometry\n");
413      return 0;
414   }
415
416   logerror("SDF geometry guess: tracks = %d, heads = %d, sectors = %d\n", geometry->tracksperside, geometry->sides, geometry->secspertrack);
417   return 100;
418}
419
420/*
421    Figure out the interleave for the sector arrangement in the track. For
422    SDF format this is required for the read_track function.
423
424    Interleave:
425        0 7 5 3 1 8 6 4 2;
426             start with 0, 3, 6, 0, 3, 6, ...
427            0 11 4 15 8 1 12 5 16 9 2 13 6 17 10 3 14 7
428             start with 0 for every track
429
430    Rare formats: 8 sectors
431        0 3 6 1 4 7 2 5   (i*3)%8
432
433        16 sectors
434        0 9 2 11 4 13 6 15 8 1 10 3 12 5 14 7  (i*9)%16
435
436        36 sectors
437        0 11 22 ... 3 14 25 (i*11)%36
438*/
439static void guess_interleave(int sectors, int *step, int *initial)
440{
441   switch (sectors)
442   {
443   case 8:
444      *step = 3;
445      /* initial is the offset from track to track+1 of the starting sector number */
446      *initial = 0;
447      break;
448   case 9:
449      *step = 7;
450      *initial = 3;
451      break;
452   case 16:
453      *step = 9;
454      *initial = 0;
455      break;
456   case 18:
457      *step = 11;
458      *initial = 0;
459      break;
460   case 36:
461      *step = 11;
462      *initial = 0;
463      break;
464   default:
465      *step = 11;
466      *initial = 0;
467      break;
468   }
469}
470
471/*
472    Delivers a whole track. As the image consists only of
473    sector data, we must reconstruct the track data and
474    then insert the sector data.
475
476    The track layout is exactly the Track Dump Format, except that the
477    CRC values must be correctly calculated.
478
479    Total length (FM)  = 247 + sectors*334; (sectors=8 or 9)
480    Total length (MFM) = 752 + sectors*340; (sectors=16 or 18)
481*/
482static floperr_t ti99_sdf_read_track(floppy_image *floppy, int head, int track, UINT64 offset, void *buffer, size_t buflen)
483{
484   int sector;
485   int startstep;
486   int step;
487   int position=0;
488   unsigned short crc;
489   int i;
490   floperr_t err;
491   int imgtrack = track;
492
493   UINT8 *trackdata = (UINT8*)buffer;
494
495   /* Determine from the image geometry whether we have FM or MFM */
496   struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy);
497
498   // Do we have a 40 track disk in an 80 track drive? In that case we
499   // double the "width" of the tracks. */
500   if (use_80_track_drives && tag->tracks<=40)
501   {
502      imgtrack = track / 2;
503   }
504
505   guess_interleave(tag->sectors, &step, &startstep);
506
507   if (tag->sectors < 10)  /* must be FM */
508   {
509      /* Write lead-in. */
510      memset(trackdata, 0x00, 16);
511      position += 16;
512      for (i=0; i < tag->sectors; i++)
513      {
514         /* Create interleaving */
515         sector = (i*step + imgtrack*startstep) % tag->sectors;
516
517         memset(&trackdata[position], 0x00, 6);
518         position += 6;
519         /* Set IDAM */
520         trackdata[position++] = 0xfe;
521         trackdata[position++] = imgtrack;
522         trackdata[position++] = head;
523         trackdata[position++] = sector;
524         trackdata[position++] = 0x01;
525         /* Calculate CRC16. Preset to 0xffff (what is 0xcdb4?) */
526         crc = ccitt_crc16(0xffff, &trackdata[position-(TI99_IDAM_LENGTH-2)], TI99_IDAM_LENGTH - 2);
527         trackdata[position++] = (crc>>8)&0xff;
528         trackdata[position++] = crc & 0xff;
529
530         /* Write Gap2 */
531         memset(&trackdata[position], 0xff, 11);
532         position += 11;
533         memset(&trackdata[position], 0x00, 6);
534         position += 6;
535         /* Write DAM */
536         trackdata[position++] = 0xfb;
537
538         /* Locate the sector content in the image and load it. */
539         err = ti99_sdf_read_sector(floppy, head, imgtrack, sector, &trackdata[position], SECTOR_SIZE);
540         if (err)
541            return err;
542
543         position += SECTOR_SIZE;
544
545         /* Set CRC16 */
546         crc = ccitt_crc16(0xffff, &trackdata[position-(SECTOR_SIZE+1)], SECTOR_SIZE+1);
547         trackdata[position++] = (crc>>8)&0xff;
548         trackdata[position++] = crc & 0xff;
549
550         /* Write Gap3 */
551         memset(&trackdata[position], 0xff, 45);
552         position += 45;
553      }
554      /* Write lead-out */
555      memset(&trackdata[position], 0xff, 231);
556      position += 231;
557   }
558   else /* we have MFM */
559   {
560      /* Write lead-in. */
561      memset(trackdata, 0x4e, 40);
562      position += 40;
563      for (i=0; i < tag->sectors; i++)
564      {
565         sector = (i*step + imgtrack*startstep) % tag->sectors;
566
567         memset(&trackdata[position], 0x00, 10);
568         position += 10;
569         /* Write sync */
570         memset(&trackdata[position], 0xa1, 3);
571         position += 3;
572         /* Set IDAM */
573         trackdata[position++] = 0xfe;
574         trackdata[position++] = imgtrack;
575         trackdata[position++] = head;
576         trackdata[position++] = sector;
577         trackdata[position++] = 0x01;
578         /* Set CRC16 */
579         crc = ccitt_crc16(0xffff, &trackdata[position-TI99_IDAM_LENGTH+2], TI99_IDAM_LENGTH - 2);
580         trackdata[position++] = (crc>>8)&0xff;
581         trackdata[position++] = crc & 0xff;
582
583         /* Write Gap2 */
584         memset(&trackdata[position], 0x4e, 22);
585         position += 22;
586         memset(&trackdata[position], 0x00, 12);
587         position += 12;
588         /* Write sync */
589         memset(&trackdata[position], 0xa1, 3);
590         position += 3;
591         /* Write DAM */
592         trackdata[position++] = 0xfb;
593
594         /* Locate the sector content in the image and load it. */
595         err = ti99_sdf_read_sector(floppy, head, track, sector, &trackdata[position], SECTOR_SIZE);
596         if (err)
597            return err;
598
599         position += SECTOR_SIZE;
600
601         /* Set CRC16 */
602         crc = ccitt_crc16(0xffff, &trackdata[position-SECTOR_SIZE-1], SECTOR_SIZE+1);
603         trackdata[position++] = (crc>>8)&0xff;
604         trackdata[position++] = crc & 0xff;
605
606         /* Write Gap3 */
607         memset(&trackdata[position], 0x4e, 24);
608         position += 24;
609      }
610      /* Write lead-out */
611      memset(&trackdata[position], 0x4e, 712);
612      position += 712;
613   }
614   return FLOPPY_ERROR_SUCCESS;
615}
616
617/*
618    Writes a whole track. As the sector dump format does not include track
619    data, we have to extract the sector contents and to write them
620    in sequence into the image. All the rest (gaps, crc, sync bytes) are
621    dropped here.
622    Bytes written to locations beyond the image end will be droppped
623    silently (as if the medium is unwritable from that point).
624*/
625static floperr_t ti99_sdf_write_track(floppy_image *floppy, int head, int track, UINT64 offset, const void *buffer, size_t buflen)
626{
627   int current_pos = 0;
628   UINT8 *track_image;
629   int leadin, gap1, gap2;
630   int is_fm, found;
631   struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy);
632   int imgtrack = track;
633
634   // Find out if we are going to write an FM or MFM track. Note that the
635   // original TI controller is known to have a bug in the formatting
636   // routine (using a MOVB instead of a MOV instruction when setting the
637   // pointer to the track data), outputting false bytes at the
638   // beginning of the track. So we do not rely on absolute positions
639   // (and neither does the controller).
640   track_image = (UINT8*)buffer;
641
642   if (use_80_track_drives && tag->tracks<=40)
643   {
644      imgtrack = track / 2;
645   }
646
647   /* Only search in the first 100 bytes for the start. */
648   leadin = 40;
649   gap1 = 10;
650   gap2 = 12;
651   is_fm = FALSE;
652
653   current_pos = find_block(track_image, 0, 100, 0x4e, leadin);
654
655   // In case of defect formats, we continue as far as possible. This
656   // may lead to sectors not being written. */
657   if (current_pos==TI99DSK_BLOCKNOTFOUND)
658   {
659      /* Not MFM, possibly FM image */
660      /* Again, find Lead-in */
661      leadin = 16;
662      current_pos = find_block(track_image, 0, 100, 0x00, leadin);
663      if (current_pos==TI99DSK_BLOCKNOTFOUND)
664      {
665         /* If neither, forget about this process completely. */
666         logerror("Cannot find lead-in for track %d, head %d.\n", track, head);
667         return FLOPPY_ERROR_INVALIDIMAGE;
668      }
669      gap1 = 6;
670      gap2 = 6;
671      is_fm = TRUE;
672   }
673
674   found = FALSE;
675   /* Get behind lead-in */
676   current_pos += leadin;
677   while (current_pos < buflen)
678   {
679      /* We must find the address block to determine the sector. */
680      int new_pos = find_block(track_image, current_pos, buflen, 0x00, gap1);
681      if (new_pos==TI99DSK_BLOCKNOTFOUND)
682      {
683         /* Forget about the rest. */
684         if (found) break;  /* we were already successful, so all ok */
685         logerror("Cannot find gap1 for track %d, head %d.\n", track, head);
686         return FLOPPY_ERROR_INVALIDIMAGE;
687      }
688      found = TRUE;
689
690      if (!is_fm)
691         new_pos += 3; /* skip sync bytes in MFM */
692
693      if (track_image[new_pos + gap1]==0xfe)
694      {
695         current_pos = new_pos + gap1  + 1;
696         /* IDAM found. */
697         int wtrack = track_image[current_pos];
698         int whead = track_image[current_pos+1];
699         int sector = track_image[current_pos+2];
700         if (wtrack == imgtrack && whead == head)
701         {
702            /* We skip the first part of gap2. */
703            new_pos = find_block(track_image, current_pos, buflen, 0x00, gap2);
704            if (current_pos==TI99DSK_BLOCKNOTFOUND)
705            {
706               logerror("Cannot find gap2 for track %d, head %d.\n", imgtrack, head);
707               return FLOPPY_ERROR_INVALIDIMAGE;
708            }
709            else
710            {
711               new_pos += gap2;
712               if (!is_fm)
713                  new_pos += 3; /* skip sync */
714               current_pos = new_pos;
715               if (track_image[current_pos]==0xfb)
716               {
717                  /* DAM found. We now write the sector content to the image. */
718                  ti99_sdf_write_sector(floppy, head, track, sector, &track_image[current_pos+1], SECTOR_SIZE, 0);
719                  current_pos += SECTOR_SIZE;
720               }
721               else
722                  logerror("DAM not found. Not writing write sector %d\n", sector);
723               /* else not found, ignore this sector. */
724            }
725         }
726         else
727         {
728            // else the sector head data do not match the
729            // current track and head. Ignore the sector.
730            logerror("Wrong track: wtrack=%d, imgtrack=%d, whead=%d, imghead=%d\n",wtrack,imgtrack, whead,head);
731         }
732      }
733      else
734      {
735         /* One step forward. Retry. */
736         current_pos++;
737      }
738   }
739   return FLOPPY_ERROR_SUCCESS;
740}
741
742/*
743    This method creates a properly formatted image.
744
745    For a proper image, we need to format all tracks, and to create a
746    minimal filesystem:
747
748    name = "UNNAMED   "
749    number of sectors
750    sectors/track
751    "DSK"
752    protection = 0x00
753    tracks/side
754    sides
755    density (0,1 = FM, 2 = MFM)
756    0x14 - 0x37 : 0x00
757    Allocation bitmap: 0xFF except for the first #sectors/AU size
758
759    AU size = 1 (360 - 1440 sectors)
760            = 2 (1441 - 2880 sectors)
761        = 4 (2881 - 5760 sectors)
762        = 8 (5760 - 11520 sectors)
763
764    sector 0 =
765    sector 1 = 256* 0x00
766*/
767/* static floperr_t ti99_sdf_format_track(floppy_image *floppy, int head, int track, option_resolution *params)
768{
769    UINT8 *sector0 = (UINT8*)malloc(SECTOR_SIZE);
770    create_vib(sector0, params);
771    free(sector0);
772    if (true)
773    {
774        printf("not implemented\n");
775        return FLOPPY_ERROR_SEEKERROR;
776    }
777    return FLOPPY_ERROR_SUCCESS;
778}
779*/
780
781/*
782    Get the offset from the start of the image file. In this format, logical
783    tracks on two-sided disks are arranged as follows (example for 40
784    tracks; accordingly for 35 or 80)
785
786    head-track
787    0-0
788    0-1
789    0-2
790    0-3
791    ...
792    0-39
793    1-39
794    1-38
795    1-37
796    ...
797    1-0
798    Note that we take the imgtrack, which may be half of the drive track.
799*/
800static floperr_t ti99_sdf_get_offset(floppy_image *floppy, int head, int imgtrack, int sector, UINT64 *offset)
801{
802   struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy);
803
804   if ((head < 0) || (head >= tag->heads)
805      || (imgtrack < 0) || (imgtrack >= tag->tracks)
806      || (sector < 0) || (sector > tag->sectors))
807      return FLOPPY_ERROR_SEEKERROR;
808
809   if (head == 0)  /* track numbers increasing towards inner track */
810   {
811      *offset = (imgtrack * tag->sectors + sector) * SECTOR_SIZE;
812   }
813   else      /* track numbers increasing towards outer track */
814   {
815      *offset = (((2*tag->tracks)-1-imgtrack) * tag->sectors + sector) * SECTOR_SIZE;
816   }
817   return FLOPPY_ERROR_SUCCESS;
818}
819
820/*
821    Read one sector at the specified position.
822*/
823static floperr_t ti99_sdf_read_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen)
824{
825   floperr_t err;
826   UINT64 offset;
827   int imgtrack = track;
828   struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy);
829
830   if (use_80_track_drives && tag->tracks<=40)
831   {
832      imgtrack = track / 2;
833   }
834
835   err = ti99_sdf_get_offset(floppy, head, imgtrack, sector, &offset);
836   if (err)
837   {
838      return err;
839   }
840   floppy_image_read(floppy, buffer, offset, SECTOR_SIZE);
841
842   return FLOPPY_ERROR_SUCCESS;
843}
844
845/*
846    For the SDF format, sectors are always numbered in ascending order,
847    starting with the lowest numbered sector in each track. In other words,
848    we have nothing to calculate here.
849*/
850static floperr_t ti99_sdf_read_indexed_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen)
851{
852   return ti99_sdf_read_sector(floppy, head, track, sector, buffer, buflen);
853}
854
855/*
856    Write one sector at the specified position.
857*/
858static floperr_t ti99_sdf_write_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam)
859{
860   UINT64 offset;
861   floperr_t err;
862   int imgtrack = track;
863
864   struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy);
865
866   if (use_80_track_drives && tag->tracks<=40)
867   {
868      imgtrack = track / 2;
869   }
870
871   err = ti99_sdf_get_offset(floppy, head, imgtrack, sector, &offset);
872   if (err)
873   {
874      return err;
875   }
876   floppy_image_write(floppy, buffer, offset, SECTOR_SIZE);
877
878   return FLOPPY_ERROR_SUCCESS;
879}
880
881/*
882    See above; indexing and numbering coincide.
883*/
884static floperr_t ti99_sdf_write_indexed_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam)
885{
886   return ti99_sdf_write_sector(floppy, head, track, sector, buffer, buflen, ddam);
887}
888
889/*
890    Required for ReadAddress command
891*/
892static floperr_t ti99_sdf_get_indexed_sector_info(floppy_image *floppy, int head, int track, int sector_index, int *cylinder, int *side, int *sector, UINT32 *sector_length, unsigned long *flags)
893{
894   struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy);
895
896   if (use_80_track_drives && tag->tracks<=40)
897   {
898      track = track / 2;
899   }
900
901   if (sector_length)
902      *sector_length=SECTOR_SIZE;
903   if (cylinder)
904      *cylinder = track;
905   if (side)
906      *side = head;
907   if (sector)
908      *sector = sector_index;
909   if (flags)
910      *flags = 0;
911   if (sector_index > tag->sectors)
912   {
913      return FLOPPY_ERROR_SEEKERROR;
914   }
915
916   return FLOPPY_ERROR_SUCCESS;
917}
918
919/*
920    Try to identify the image as an SDF format image.
921*/
922static FLOPPY_IDENTIFY(ti99_sdf_identify)
923{
924   UINT64 size;
925   size = floppy_image_size(floppy);
926   *vote = ti99_sdf_guess_geometry(floppy, size, NULL);
927   logerror("SDF voting %d\n", *vote);
928   return FLOPPY_ERROR_SUCCESS;
929}
930
931/*
932    Create the SDF data structures and set the callbacks.
933*/
934static FLOPPY_CONSTRUCT(ti99_sdf_construct)
935{
936   struct ti99dsk_geometry geometry;
937   struct FloppyCallbacks *callbacks;
938   struct ti99dsk_tag *tag;
939   logerror("Reading image as SDF\n");
940
941   if (params)
942   {
943      /* create */
944      memset(&geometry, 0, sizeof(geometry));
945      geometry.sides            = option_resolution_lookup_int(params, PARAM_HEADS);
946      geometry.tracksperside         = option_resolution_lookup_int(params, PARAM_TRACKS);
947      geometry.secspertrack         = option_resolution_lookup_int(params, PARAM_SECTORS);
948
949      /* We don't have headers for geometry */
950      /* check for usage in imgtool - we want to be able to create useful disks */
951   }
952   else
953   {
954      /* load */
955      if (ti99_sdf_guess_geometry(floppy, floppy_image_size(floppy), &geometry)<50)
956         return FLOPPY_ERROR_INVALIDIMAGE;
957   }
958
959   assert(geometry.sides);
960   assert(geometry.tracksperside);
961   assert(geometry.secspertrack);
962
963   tag = (struct ti99dsk_tag *) floppy_create_tag(floppy, sizeof(struct ti99dsk_tag));
964
965   if (!tag)
966      return FLOPPY_ERROR_OUTOFMEMORY;
967
968   tag->heads = geometry.sides;
969   tag->tracks = geometry.tracksperside;
970   tag->sectors = geometry.secspertrack;
971   if (tag->sectors < 10)
972      /* FM mode */
973      tag->track_size = 16 + tag->sectors*334 + 231;
974   else
975      tag->track_size = 40 + tag->sectors*340 + 712;
976
977   /* set up format callbacks */
978   callbacks = floppy_callbacks(floppy);
979   callbacks->read_sector = ti99_sdf_read_sector;
980   callbacks->write_sector = ti99_sdf_write_sector;
981   callbacks->read_indexed_sector = ti99_sdf_read_indexed_sector;
982   callbacks->write_indexed_sector = ti99_sdf_write_indexed_sector;
983   callbacks->read_track = ti99_sdf_read_track;
984   callbacks->write_track = ti99_sdf_write_track;
985/*  callbacks->format_track = ti99_sdf_format_track; */
986   callbacks->get_track_size = ti99_get_track_size;
987   /* post format unset */
988   callbacks->get_heads_per_disk = ti99_get_heads_per_disk;
989   callbacks->get_tracks_per_disk = ti99_get_tracks_per_disk;
990   callbacks->get_sector_length = ti99_get_sector_length;
991   callbacks->get_indexed_sector_info = ti99_sdf_get_indexed_sector_info;
992
993   return FLOPPY_ERROR_SUCCESS;
994}
995
996/* -----------------------------------------------------------------------
997 * Track Dump Format, aka PC99 format
998 * This format includes all track data, including address marks and gaps.
999 * However, CRC is not stored; instead, a f7f7 is stored at that location.
1000 * Two track images exist, one for FM and one for MFM format.
1001 *
1002 * In this implementation, however, we do calculate the CRC.
1003 *
1004 * All tracks have exactly the same size, so offsets are calculated easily.
1005 *
1006 * This format is a precise map of the data contents of the track;
1007 * however, it does not store clock patterns.
1008 *
1009 * The TDF format does not have a special index part, so any sector on it
1010 * must be searched (as done by the disk controller)
1011 *
1012 * Interesting detail: The original TI disk controller has a bug which has
1013 * just recently been discovered (using MESS!): The lead-in has another 9
1014 * bytes as a prefix; this is due to a wrongly set pointer in the format
1015 * routine. Strictly speaking, it produces invalid track images.
1016 * The original hardware does not care, however, so neither should we.
1017 * To keep the emulation realistic (and to allow for a
1018 * possible future extension with clock bits), we search for the
1019 * beginning of the track. From there on, however, we accept no further
1020 * deviations. This may make the resulting format unusable with the PC99
1021 * emulator, but it only appears when formatting with the TI disk
1022 * controller anyway. If the image is created with another disk controller or
1023 * using imgtool, the format is correctly created.
1024 *
1025 * FM track image
1026 *
1027 * Lead-in:           16*  00
1028 * Pre-ID gap:         6*  00    ---+
1029 *       IDAM:             fe       |
1030 *      Track:             tt       |
1031 *       Head:             hh       |
1032 *     Sector:             ss       |
1033 *       Size:             01       |
1034 *        CRC:         2*  f7       +--- repeat 9 times (8 in rare cases)
1035 *       Gap2:        11*  ff       |
1036 *                     6*  00       |
1037 *        DAM:             fb       |
1038 *    Content:       256*  xx       |
1039 *        CRC:         2*  f7       |
1040 *       Gap3:        45*  ff    ---+
1041 * Lead-out:         231*  ff
1042 *
1043 * --------------
1044 *
1045 * MFM track image
1046 *
1047 * Lead-in:           40*  4e
1048 * Pre-ID gap:        10*  00    ---+
1049 *       Sync:         3*  a1       |
1050 *       IDAM:             fe       |
1051 *      Track:             tt       |
1052 *       Head:             hh       |
1053 *     Sector:             ss       |
1054 *       Size:             01       |
1055 *        CRC:         2*  f7       +--- repeat 18 times (16 in rare cases)
1056 *       Gap2:        22*  4e       |
1057 *                    12*  00       |
1058 *       Sync:         3*  a1       |
1059 *        DAM:             fb       |
1060 *    Content:       256*  xx       |
1061 *        CRC:         2*  f7       |
1062 *       Gap3:        24*  4e    ---+
1063 * Lead-out:         712*  4e
1064 *
1065 * ------------------
1066 * The tracks are located on the image as follows (different to SDF!):
1067 *
1068 *   head-track
1069 *      0-0
1070 *      0-1
1071 *      0-2
1072 *      0-3
1073 *      ...
1074 *      0-39
1075 *      1-0
1076 *      1-1
1077 *      1-2
1078 *      ...
1079 *      1-39
1080 * ----------------------------------------------------------------------- */
1081
1082#define TI99_FM 1
1083#define TI99_MFM 2
1084
1085/* For the emulation of the clock pattern, we add a 9th bit. */
1086#define TI99_DAM 0x1fb
1087#define TI99_IDAM 0x1fe
1088
1089#define TI99_FM_DAM_OFFSET 24
1090#define TI99_MFM_DAM_OFFSET 44
1091
1092#define TI99_FM_TRACK_ADD 247
1093#define TI99_MFM_TRACK_ADD 752
1094
1095#define TI99_FM_TOTAL_SECTOR 334
1096#define TI99_MFM_TOTAL_SECTOR 340
1097
1098/*
1099    Determine offset of the first IDAM. This is the base of the simulation
1100    of the clock bytes. We use a heuristic to guess where the track begins
1101    and simply check whether we can locate the first IDAM and DAM. Return
1102    value is the offset to the first IDAM.
1103*/
1104static floperr_t determine_offset(int format, UINT8 *track, int *offset)
1105{
1106   floperr_t retval;
1107   int current_pos;
1108
1109   if (format==TI99_FM)
1110   {
1111//      printf("Trying FM\n");
1112      // We are seeking the lead-in and pre-id gap from
1113      // position 0 to position 50. This is just to make sure that
1114      // if there are bad bytes at the beginning (original TI controller)
1115      // we will skip them.
1116      current_pos = find_block(track, 0, 50, 0x00, 22);
1117      if (current_pos==TI99DSK_BLOCKNOTFOUND)
1118      {
1119         logerror("Lead-in not found\n");
1120         retval = FLOPPY_ERROR_SEEKERROR;
1121      }
1122      else
1123      {
1124         current_pos += 22;
1125         if (track[current_pos]==0xfe /* IDAM */ &&
1126            track[current_pos+4]==0x01 /* sector length, always 256 for TI */ &&
1127            track[current_pos+24]==0xfb) /* DAM */
1128         {
1129            /* We're pretty sure this is the beginning. */
1130            *offset = current_pos;
1131            retval = FLOPPY_ERROR_SUCCESS;
1132         }
1133         else
1134         {
1135            logerror("IDAM or DAM not found\n");
1136            retval = FLOPPY_ERROR_SEEKERROR;
1137         }
1138      }
1139   }
1140   else
1141   {
1142      /* MFM */
1143//      printf("Trying MFM\n");
1144      int track_start = find_block(track, 0, 100, 0x4e, 40);
1145      if (track_start==TI99DSK_BLOCKNOTFOUND)
1146      {
1147         logerror("Lead-in not found\n");
1148         retval = FLOPPY_ERROR_SEEKERROR;
1149      }
1150      else
1151      {
1152         track_start += 40;
1153         current_pos = find_block(track, track_start, track_start+10, 0x00, 10);
1154         if (current_pos==TI99DSK_BLOCKNOTFOUND)
1155         {
1156            logerror("Pre-gap not found\n");
1157            retval = FLOPPY_ERROR_SEEKERROR;
1158         }
1159         else
1160         {
1161            current_pos += 10;
1162            if (track[current_pos] == 0xa1 /* First sync */ &&
1163               track[current_pos+3] == 0xfe /* IDAM */ &&
1164               track[current_pos+44] == 0xa1 /* First sync */ &&
1165               track[current_pos+47] == 0xfb) /* DAM */
1166            {
1167            /* We're pretty sure this is the beginning. */
1168               *offset = current_pos + 3;
1169               retval = FLOPPY_ERROR_SUCCESS;
1170            }
1171            else
1172            {
1173               logerror("Sync/IDAM/DAM not found\n");
1174               retval = FLOPPY_ERROR_SEEKERROR;
1175            }
1176         }
1177      }
1178   }
1179/*  printf("TDF offset = 0x%x\n", *offset); */
1180   return retval;
1181}
1182
1183/*
1184    Gets a byte from the track. In this format we do not have a clock
1185    signal, so we will simulate it. Thus, this function returns a 9-bit
1186    value, with the first bit set as a marker for an address mark.
1187
1188    This may be a little more complicated than necessary, but it correctly
1189    emulates the behavior of the controller (and it allows for an
1190    alternative format which actually supports clock and data).
1191*/
1192static int read_byte(int format, int first_idam, UINT8 *track, int position)
1193{
1194   int totalseclen;
1195   int dam_offset;
1196   int byte;
1197
1198   /* Check whether we are at any IDAM or DAM in this track. */
1199   if (format==TI99_FM)
1200   {
1201      totalseclen = TI99_FM_TOTAL_SECTOR;
1202      dam_offset = TI99_FM_DAM_OFFSET;
1203   }
1204   else
1205   {
1206      totalseclen = TI99_MFM_TOTAL_SECTOR;
1207      dam_offset = TI99_MFM_DAM_OFFSET;
1208   }
1209
1210   int curpos = (position - first_idam) % totalseclen;
1211   if (curpos==0 && track[position]==0xfe)
1212      byte = TI99_IDAM;
1213   else
1214   {
1215      if (curpos==dam_offset && track[position]==0xfb)
1216         byte = TI99_DAM;
1217      else
1218         byte = track[position] & 0xff;
1219   }
1220   return byte;
1221}
1222
1223/*
1224    Determine the position of this track in the image.
1225*/
1226static floperr_t ti99_tdf_get_offset(floppy_image *floppy, int head, int imgtrack, UINT64 *offset)
1227{
1228   struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy);
1229
1230   if ((head < 0) || (head >= tag->heads) || (imgtrack < 0) || (imgtrack >= tag->tracks))
1231      return FLOPPY_ERROR_SEEKERROR;
1232
1233   // For head 0, tracks increase from 0 to tracks-1; then for head 1,
1234   // tracks also increase from 0 to tracks-1. */
1235   *offset = head * (tag->tracks * tag->track_size) +  imgtrack * tag->track_size;
1236
1237   return FLOPPY_ERROR_SUCCESS;
1238}
1239
1240/*
1241    Reads a track. We just copy the contents from the format and recreate
1242    the CRC if it is the "blank" CRC as F7F7.
1243    This method assumes that the track division be done already.
1244*/
1245static floperr_t ti99_tdf_read_track_internal(floppy_image *floppy, int head, int imgtrack, UINT64 offset, void *buffer, size_t buflen)
1246{
1247   floperr_t err;
1248   UINT64 track_offset;
1249   int first_idam = 0;
1250   struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy);
1251   int i, byte, crc;
1252
1253   UINT8 *track_data = (UINT8*)buffer;
1254
1255   err = ti99_tdf_get_offset(floppy, head, imgtrack, &track_offset);
1256   if (err)
1257      return err;
1258
1259   floppy_image_read(floppy, buffer, track_offset, buflen);
1260
1261   // Rebuild the CRCs. PC99 did not store the CRC but put
1262   // F7F7 in its place.
1263   // The first CRC is at position IDAM + 5
1264   // The second CRC is at position IDAM + 0x119 (FM) or +0x12d (MFM)
1265   // All this is repeated for each sector in the track
1266
1267   if (determine_offset(tag->format, track_data, &first_idam)==FLOPPY_ERROR_SEEKERROR)
1268      return FLOPPY_ERROR_SEEKERROR;
1269
1270   i = 0;
1271   while (i < buflen)
1272   {
1273      byte = read_byte(tag->format, first_idam, track_data, i);
1274      if (byte == TI99_IDAM)
1275      {
1276         // Do we have a valid CRCs already? Then this image
1277         // already contains proper CRCs handling. Do not recreate them.
1278         if (track_data[i+5] != 0xf7 || track_data[i+6] != 0xf7)
1279            return FLOPPY_ERROR_SUCCESS;
1280
1281         crc = ccitt_crc16(0xffff, &track_data[i], 5);
1282         track_data[i+5] = (crc>>8) & 0xff;
1283         track_data[i+6] = (crc & 0xff);
1284      }
1285      else
1286      {
1287         if (byte == TI99_DAM)
1288         {
1289            crc = ccitt_crc16(0xffff, &track_data[i], SECTOR_SIZE+1);
1290            track_data[i+SECTOR_SIZE+1] = (crc>>8) & 0xff;
1291            track_data[i+SECTOR_SIZE+2] = (crc & 0xff);
1292         }
1293      }
1294      i++;
1295   }
1296//  dump_contents(track_data, buflen);
1297   return FLOPPY_ERROR_SUCCESS;
1298}
1299
1300/*
1301    Reads a track. We just copy the contents from the format, without
1302    changes.
1303*/
1304static floperr_t ti99_tdf_read_track(floppy_image *floppy, int head, int track, UINT64 offset, void *buffer, size_t buflen)
1305{
1306   int imgtrack = track;
1307
1308   struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy);
1309   if (use_80_track_drives && tag->tracks<=40)
1310   {
1311      imgtrack = track / 2;
1312   }
1313   return ti99_tdf_read_track_internal(floppy, head, imgtrack, offset, buffer, buflen);
1314}
1315
1316/*
1317    Writes a track.
1318*/
1319static floperr_t ti99_tdf_write_track(floppy_image *floppy, int head, int track, UINT64 offset, const void *buffer, size_t buflen)
1320{
1321   floperr_t err;
1322   UINT64 track_offset;
1323   int imgtrack = track;
1324
1325   struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy);
1326   if (use_80_track_drives && tag->tracks<=40)
1327   {
1328      imgtrack = track / 2;
1329   }
1330
1331   err = ti99_tdf_get_offset(floppy, head, imgtrack, &track_offset);
1332   if (err)
1333      return err;
1334
1335   // According to the TDF format, we don't need the CRC but replace it
1336   // with F7F7. For now we keep it and see whether PC99 can cope with
1337   // that. (Otherwise we would have to copy the const buffer). */
1338   floppy_image_write(floppy, buffer, offset + track_offset, buflen);
1339   return FLOPPY_ERROR_SUCCESS;
1340}
1341
1342/*
1343static floperr_t ti99_tdf_format_track(floppy_image *floppy, int head, int track, option_resolution *params)
1344{
1345    int sectors;
1346    int sector_length;
1347    int interleave;
1348    int first_sector_id;
1349    sectors         = option_resolution_lookup_int(params, PARAM_SECTORS);
1350    sector_length   = option_resolution_lookup_int(params, PARAM_SECTOR_LENGTH);
1351    interleave      = option_resolution_lookup_int(params, PARAM_INTERLEAVE);
1352    first_sector_id = option_resolution_lookup_int(params, PARAM_FIRST_SECTOR_ID);
1353    return FLOPPY_ERROR_SUCCESS;
1354}
1355*/
1356
1357/*
1358    Get a sector from a track. This function takes a complete track,
1359    searches for the sector, and passes a pointer to the sector contents
1360    back.
1361    As the original TI controller creates bad bytes at the start of a track
1362    we do *not* assume a simple layout. The strategy is to search the
1363    lead-in of the track, and from this offset, step through the track
1364    in search for the sector. Since we do not have index marks (we cannot
1365    distinguish them from ordinary data, as we do not store the clock), we
1366    do not attempt to detect IDAM or DAM.
1367*/
1368static floperr_t ti99_tdf_seek_sector_in_track(floppy_image *floppy, int head, int track, int sector, UINT8 *track_data, UINT8 **sector_data)
1369{
1370   struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy);
1371
1372   int byte;
1373   int state;
1374   enum { SEARCHIDAM, SEARCHDAM };
1375
1376   state = SEARCHIDAM;
1377   if (tag->first_idam==0) /* should we check for every track? */
1378   {
1379      if (determine_offset(tag->format, track_data, &tag->first_idam)==FLOPPY_ERROR_SEEKERROR)
1380      {
1381         logerror("could not determine offset\n");
1382         return FLOPPY_ERROR_SEEKERROR;
1383      }
1384   }
1385
1386   for (int i=0; i < tag->track_size; i++)
1387   {
1388      byte = read_byte(tag->format, tag->first_idam, track_data, i);
1389
1390      switch (state)
1391      {
1392      case SEARCHIDAM:
1393         /* search for the IDAM */
1394         if (byte==TI99_IDAM)
1395         {
1396            if ((track_data[i+1]==track) && (track_data[i+2]==head) && (track_data[i+3]==sector))
1397               state = SEARCHDAM;
1398            else
1399               state = SEARCHIDAM;
1400         }
1401         break;
1402      case SEARCHDAM:
1403         if (byte==TI99_DAM)
1404         {
1405            *sector_data = &track_data[i+1];
1406            return FLOPPY_ERROR_SUCCESS;
1407         }
1408         break;
1409      }
1410   }
1411   logerror("Sector not found.\n");
1412   return FLOPPY_ERROR_SEEKERROR;
1413}
1414
1415static floperr_t ti99_tdf_read_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen)
1416{
1417   floperr_t err;
1418   UINT8 *sector_data;
1419   UINT8 *track_data;
1420   int imgtrack = track;
1421   struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy);
1422
1423   track_data = (UINT8*)malloc(tag->track_size);
1424
1425   if (use_80_track_drives && tag->tracks<=40)
1426   {
1427      imgtrack = track / 2;
1428   }
1429
1430   err = ti99_tdf_read_track_internal(floppy, head, imgtrack, 0, track_data, tag->track_size);
1431   if (err)
1432      return err;
1433
1434   err = ti99_tdf_seek_sector_in_track(floppy, head, imgtrack, sector, track_data, &sector_data);
1435   if (err)
1436      return err;
1437   /* verify CRC? */
1438
1439   memcpy(buffer, sector_data, SECTOR_SIZE);
1440   free(track_data);
1441   return FLOPPY_ERROR_SUCCESS;
1442}
1443
1444/*
1445    Writes a sector into the track. Note that the indexed_write method
1446    may be used instead of this method, according to the implementation
1447    of flopimg/flopdrv.
1448*/
1449static floperr_t ti99_tdf_write_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam)
1450{
1451   floperr_t err;
1452   UINT8 *sector_data;
1453   UINT8 *track_data;
1454   UINT64 track_offset;
1455   int imgtrack = track;
1456   UINT64 offset;
1457   UINT8 crc_field[2];
1458   int crc;
1459
1460   struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy);
1461
1462   track_data = (UINT8*)malloc(tag->track_size);
1463   if (use_80_track_drives && tag->tracks<=40)
1464   {
1465      imgtrack = track / 2;
1466   }
1467
1468   err = ti99_tdf_read_track_internal(floppy, head, imgtrack, 0, track_data, tag->track_size);
1469   if (err)
1470      return err;
1471   err = ti99_tdf_seek_sector_in_track(floppy, head, imgtrack, sector, track_data, &sector_data);
1472   if (err)
1473      return err;
1474
1475   err = ti99_tdf_get_offset(floppy, head, imgtrack, &track_offset);
1476   if (err)
1477      return err;
1478
1479   offset = track_offset + (sector_data - track_data);
1480
1481   floppy_image_write(floppy, buffer, offset, SECTOR_SIZE);
1482
1483   // Recalculate CRC. We init the CRC with bf84 (DAM fb is
1484   // already included; would be ffff without)
1485   crc = ccitt_crc16(0xbf84, (UINT8*)buffer, SECTOR_SIZE);
1486   crc_field[0] = (crc>>8) & 0xff;
1487   crc_field[1] = (crc & 0xff);
1488   floppy_image_write(floppy, crc_field, offset+SECTOR_SIZE, 2);
1489
1490   return FLOPPY_ERROR_SUCCESS;
1491}
1492
1493static floperr_t ti99_tdf_write_indexed_sector(floppy_image *floppy, int head, int track, int sector_index, const void *buffer, size_t buflen, int ddam)
1494{
1495   /* Read track, head */
1496   int byte;
1497   struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy);
1498   UINT8 *track_data;
1499   UINT8 *sector_data;
1500   UINT64 track_offset;
1501   UINT64 offset;
1502   UINT8 crc_field[2];
1503
1504   int crc;
1505   int imgtrack = track;
1506
1507   int i;
1508   floperr_t err;
1509
1510   track_data = (UINT8*)malloc(tag->track_size);
1511
1512   if (use_80_track_drives && tag->tracks<=40)
1513   {
1514      imgtrack = track / 2;
1515   }
1516
1517   err = ti99_tdf_read_track_internal(floppy, head, imgtrack, 0, track_data, tag->track_size);
1518   if (err)
1519   {
1520      free(track_data);
1521      return err;
1522   }
1523
1524   /* Search for the sector_index-th sector. */
1525   if (tag->first_idam==0) /* should we check for every track? */
1526   {
1527      if (determine_offset(tag->format, track_data, &tag->first_idam)==FLOPPY_ERROR_SEEKERROR)
1528      {
1529         free(track_data);
1530         return FLOPPY_ERROR_SEEKERROR;
1531      }
1532   }
1533
1534   /* Search for the IDAM of the n-th sector */
1535   i=0;
1536   while (i < tag->track_size && sector_index>=0)
1537   {
1538      byte = read_byte(tag->format, tag->first_idam, track_data, i);
1539      if (byte == TI99_IDAM)
1540      {
1541         sector_index--;
1542      }
1543      i++;
1544   }
1545   i--;
1546
1547   /* Not that many sectors in this track? */
1548   if (sector_index>0)
1549   {
1550      free(track_data);
1551      return FLOPPY_ERROR_SEEKERROR;
1552   }
1553
1554   /* Find the DAM. */
1555   while (i < tag->track_size)
1556   {
1557      byte = read_byte(tag->format, tag->first_idam, track_data, i);
1558      if (byte == TI99_DAM)
1559         break;
1560      else
1561         i++;
1562   }
1563
1564   /* There was no DAM. Image seems to be broken. */
1565   if (i == tag->track_size)
1566   {
1567      free(track_data);
1568      return FLOPPY_ERROR_SEEKERROR;
1569   }
1570
1571   /* Sector data is right after the DAM. */
1572   sector_data = &track_data[i+1];
1573
1574   /* Get the position of the track in the image. */
1575   err = ti99_tdf_get_offset(floppy, head, imgtrack, &track_offset);
1576   if (err)
1577      return err;
1578
1579   offset = track_offset + (sector_data - track_data);
1580
1581   /* Write the sector data at that position. */
1582   floppy_image_write(floppy, buffer, offset, SECTOR_SIZE);
1583
1584   // Recalculate CRC. We init the CRC with bf84 (DAM fb is
1585   // already included; would be ffff without)
1586   crc = ccitt_crc16(0xbf84, (UINT8*)buffer, SECTOR_SIZE);
1587   crc_field[0] = (crc>>8) & 0xff;
1588   crc_field[1] = (crc & 0xff);
1589   floppy_image_write(floppy, crc_field, offset+SECTOR_SIZE, 2);
1590
1591   /* Free the temporary buffer */
1592   free(track_data);
1593
1594   return FLOPPY_ERROR_SUCCESS;
1595}
1596
1597
1598static floperr_t ti99_tdf_find_indexed_sector(floppy_image *floppy, int head, int track, int sector_index, int *cylinder, int *side, int *sector, UINT32 *sector_length, void *buffer, unsigned long *flags)
1599{
1600   /* Read track, head */
1601   int byte = 0;
1602   struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy);
1603   UINT8 *track_data;
1604   int imgtrack = track;
1605   int i;
1606   floperr_t err;
1607   floperr_t retval;
1608   track_data = (UINT8*)malloc(tag->track_size);
1609
1610   if (use_80_track_drives && tag->tracks<=40)
1611   {
1612      imgtrack = track / 2;
1613   }
1614
1615   err = ti99_tdf_read_track_internal(floppy, head, imgtrack, 0, track_data, tag->track_size);
1616   if (err)
1617   {
1618      free(track_data);
1619      return err;
1620   }
1621
1622   /* Search for the sector_index-th sector. */
1623
1624   if (tag->first_idam==0) /* should we check for every track? */
1625   {
1626      if (determine_offset(tag->format, track_data, &tag->first_idam)==FLOPPY_ERROR_SEEKERROR)
1627      {
1628         free(track_data);
1629         return FLOPPY_ERROR_SEEKERROR;
1630      }
1631   }
1632
1633   i=0;
1634   sector_index = sector_index+1;
1635   while (i++ < tag->track_size && sector_index>0)
1636   {
1637      byte = read_byte(tag->format, tag->first_idam, track_data, i);
1638      if (byte == TI99_IDAM)
1639      {
1640         sector_index--;
1641      }
1642   }
1643
1644   retval = FLOPPY_ERROR_SEEKERROR;
1645   if (sector_index==0)
1646   {
1647      /* If desired, return the ID field */
1648      if (cylinder)
1649      {
1650         *cylinder = track_data[i];
1651         //      printf("cylinder=%d", *cylinder);
1652
1653      }
1654      if (side)
1655      {
1656         *side = track_data[i+1];
1657         //      printf(", side=%d", *side);
1658      }
1659      if (sector)
1660      {
1661         *sector = track_data[i+2];
1662         //      printf(", sector=%d", *sector);
1663      }
1664      if (sector_length)
1665      {
1666         *sector_length = 128 << track_data[i+3];
1667         //      printf(", sector_length=%d\n", *sector_length);
1668      }
1669      if (flags)
1670      {
1671         *flags = 0;
1672      }
1673      retval = FLOPPY_ERROR_SUCCESS;
1674
1675      /* If desired, return the sector contents. */
1676      if (buffer)
1677      {
1678         while (i++ < tag->track_size)
1679         {
1680            byte = read_byte(tag->format, tag->first_idam, track_data, i);
1681            if (byte == TI99_DAM)
1682               break;
1683         }
1684         if (byte == TI99_DAM)
1685         {
1686            memcpy(buffer, &track_data[i+1], SECTOR_SIZE);
1687         }
1688         else
1689         {
1690            retval = FLOPPY_ERROR_SEEKERROR;
1691         }
1692      }
1693   }
1694   free(track_data);
1695   return retval;
1696}
1697
1698/*
1699    Required for ReadAddress command. This function gets the 1st, 2nd, 3rd,
1700    sector (and so on) from the track, not sector 1, 2, 3. That is, the
1701    returned sector info depends on the interleave and the start sector.
1702*/
1703static floperr_t ti99_tdf_get_indexed_sector_info(floppy_image *floppy, int head, int track, int sector_index, int *cylinder, int *side, int *sector, UINT32 *sector_length, unsigned long *flags)
1704{
1705   return ti99_tdf_find_indexed_sector(floppy, head, track, sector_index, cylinder, side, sector, sector_length, NULL, flags);
1706}
1707
1708static floperr_t ti99_tdf_read_indexed_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen)
1709{
1710   return ti99_tdf_find_indexed_sector(floppy, head, track, sector, (int*)NULL, (int*)NULL, (int*)NULL, (UINT32*)NULL, buffer, (unsigned long*)NULL);
1711}
1712
1713
1714/*
1715    Guess the geometry of the disk.
1716    1. Check whether the disk is formatted, and if so, whether it is
1717       a FM or a MFM disk
1718    2. If this fails, guess from the file size which format it should have
1719
1720    SDF formats have sizes which are multiples of 10KiB.
1721    TDF: full sector size FM (MFM) = 334 (340) bytes
1722             additional track data FM (MFM) = 247 (752) bytes
1723
1724         => check whether size is dividable by 40 (or 35) and by 2
1725         => get track size
1726         => subtract track data
1727         => divide by 334 or 340
1728         => number of sectors should be 8, 9, 16, 18, 32, or 36
1729
1730     SSDD is 274880, but DSSD is 260240, so they differ (unlike in the SDF format)
1731     Still, we cannot tell apart single-sided 80 track formats from double-sided
1732     40 tracks. We assume the latter. Moreover, we only attempt to detect
1733     9/18/36 sectors (no 8/16) and 40 tracks (no 35).
1734
1735     SSSD40 =  130120
1736     DSSD   =  260240
1737     SSDD   =  274880
1738     DSDD   =  549760
1739     DSHD   = 1039360
1740     DSDD80 = 1099520
1741     DSHD80 = 2078720
1742*/
1743static int ti99_tdf_guess_geometry(floppy_image *floppy, UINT64 size,
1744   struct ti99dsk_geometry *geometry)
1745{
1746   int idamcnt, state, track, head, i, totalseclen = 0, format = 0, byte, tracklength, trackadd = 0;
1747   int first_idam = 0;
1748   UINT8 *track_data;
1749   /*int offset;*/
1750
1751   struct ti99dsk_geometry dummy_geometry;
1752
1753   if (geometry)
1754      memset(geometry, 0, sizeof(*geometry));
1755   else
1756      geometry = &dummy_geometry;
1757
1758   /* Allocate enough bytes to hold the longest supported track. */
1759   track_data = (UINT8*)malloc(13000);
1760   floppy_image_read(floppy, track_data, 0, 13000);
1761
1762   if (determine_offset(TI99_MFM, track_data, &first_idam)==FLOPPY_ERROR_SEEKERROR)
1763   {
1764      /* MFM failed. */
1765      if (determine_offset(TI99_FM, track_data, &first_idam)==FLOPPY_ERROR_SEEKERROR)
1766      {
1767         logerror("IDAM not found. Unformatted disk.\n");
1768
1769         // FM failed as well. Disk is not formatted. We assume
1770         // a format that fits into the space provided by the file.
1771         // We only give a moderate vote in this case, so SDF
1772         // make take it.
1773
1774         free(track_data);
1775         if (size < 130120 || size > 2078720)
1776         {
1777            logerror("Unknown format size: %d\n", (int)size);
1778            return 0;
1779         }
1780         if (size >= 130120 && size < 260240)
1781         {
1782            /* SSSD */
1783            geometry->sides = 1;
1784            geometry->tracksperside = 40;
1785            geometry->secspertrack = 9;
1786            geometry->density = 1;
1787            return 50;
1788         }
1789         if (size >= 260240 && size < 274880)
1790         {
1791            /* DSSD */
1792            geometry->sides = 2;
1793            geometry->tracksperside = 40;
1794            geometry->secspertrack = 9;
1795            geometry->density = 1;
1796            return 50;
1797         }
1798         if (size >= 274880 && size < 549760)
1799         {
1800            /* SSDD */
1801            geometry->sides = 1;
1802            geometry->tracksperside = 40;
1803            geometry->secspertrack = 18;
1804            geometry->density = 2;
1805            return 50;
1806         }
1807         if (size >= 549760 && size < 1039360)
1808         {
1809            /* DSDD */
1810            geometry->sides = 2;
1811            geometry->tracksperside = 40;
1812            geometry->secspertrack = 18;
1813            geometry->density = 2;
1814            return 50;
1815         }
1816         if (size >= 1039360 && size < 1099520)
1817         {
1818            /* DSHD */
1819            geometry->sides = 2;
1820            geometry->tracksperside = 40;
1821            geometry->secspertrack = 36;
1822            geometry->density = 2;
1823            return 50;
1824         }
1825         if (size >= 1099520 && size < 2078720)
1826         {
1827            /* DSDD80 */
1828            geometry->sides = 2;
1829            geometry->tracksperside = 80;
1830            geometry->secspertrack = 18;
1831            geometry->density = 2;
1832            return 50;
1833         }
1834         if (size == 2078720)
1835         {
1836            /* DSHD80 */
1837            geometry->sides = 2;
1838            geometry->tracksperside = 80;
1839            geometry->secspertrack = 36;
1840            geometry->density = 2;
1841            return 50;
1842         }
1843      }
1844      else
1845      {
1846         /* FM format */
1847         totalseclen = TI99_FM_TOTAL_SECTOR;
1848         trackadd = TI99_FM_TRACK_ADD; /* additional track data (gaps etc.) */
1849         geometry->density = 1;
1850         format = TI99_FM;
1851      }
1852   }
1853   else
1854   {
1855      /* MFM format */
1856      totalseclen = TI99_MFM_TOTAL_SECTOR;
1857      trackadd = TI99_MFM_TRACK_ADD;
1858      geometry->density = 2;
1859      format = TI99_MFM;
1860   }
1861
1862   /* Count the number of IDAMs on track 0, head 0 */
1863   idamcnt = 0;
1864   state = 0;
1865   track = 0;
1866   head = 0;
1867   i=0;
1868
1869   while (i++ < 13000 && head == 0 && track == 0)
1870   {
1871      byte = read_byte(format, first_idam, track_data, i);
1872      switch (state)
1873      {
1874      case 0:
1875         if (byte==TI99_IDAM)
1876            state = 1;
1877         break;
1878      case 1:
1879         track = byte;
1880         state = 2;
1881         break;
1882      case 2:
1883         head = byte;
1884         state = 3;
1885         break;
1886      case 3:
1887         idamcnt++;
1888         state = 0;
1889      }
1890   }
1891   logerror("Determined %d sectors\n", idamcnt);
1892
1893   // Now calculate the geometry
1894   // The track size, in theory, may change due to reformatting,
1895   // and it may also vary between tracks, but the TDF does not support
1896   // varying track lengths. So every track has the same length as track 0.
1897   // Also, we don't yet know whether the drive and the disk have the
1898   // same track count. For 80 track drives and 40 track disks, we need
1899   // double-stepping; this will be known as soon as the controller is
1900   // initialized; we need to check that in the access methods.
1901
1902   tracklength = idamcnt * totalseclen + trackadd;
1903   /*offset = 0;*/
1904
1905   // Read the last track. We assume that all tracks are properly
1906   // formatted; otherwise, the above calculations would already fail.
1907   // That is, the last track in the image tells us whether it is for
1908   // head 0 or for head 1.
1909
1910   geometry->sides = 1;
1911   floppy_image_read(floppy, track_data, size-tracklength, tracklength);
1912   if (determine_offset(format, track_data, &first_idam)==FLOPPY_ERROR_SEEKERROR)
1913   {
1914      /* error ... what now? */
1915      logerror("Error when reading last track. Image broken.\n");
1916      free(track_data);
1917      return 50;
1918   }
1919   else
1920   {
1921      if (track_data[first_idam+2]==0)
1922      {
1923         geometry->sides = 1;
1924      }
1925      else
1926      {
1927         if (track_data[first_idam+2]==1)
1928         {
1929            geometry->sides = 2;
1930         }
1931         else
1932         {
1933            logerror("Error: Last track has invalid first IDAM: head = %d.\n", track_data[first_idam+2]);
1934         }
1935
1936      }
1937   }
1938
1939
1940   geometry->tracksperside = (size / tracklength) / geometry->sides;
1941   geometry->secspertrack = idamcnt;
1942
1943   if (geometry->tracksperside < 35 || geometry->tracksperside > 80)
1944   {
1945      logerror("Unsupported track count: %d\n", geometry->tracksperside);
1946      free(track_data);
1947      return 0;
1948   }
1949
1950   free(track_data);
1951   return 100;
1952}
1953
1954static FLOPPY_IDENTIFY(ti99_tdf_identify)
1955{
1956   UINT64 size;
1957   size = floppy_image_size(floppy);
1958   *vote = ti99_tdf_guess_geometry(floppy, size, NULL);
1959   logerror("TDF voting %d\n", *vote);
1960   return FLOPPY_ERROR_SUCCESS;
1961}
1962
1963static FLOPPY_CONSTRUCT(ti99_tdf_construct)
1964{
1965   struct ti99dsk_geometry geometry;
1966   struct FloppyCallbacks *callbacks;
1967   struct ti99dsk_tag *tag;
1968   logerror("Reading image as TDF\n");
1969   if (params)
1970   {
1971      /* create */
1972      memset(&geometry, 0, sizeof(geometry));
1973/*
1974        geometry.sides              = option_resolution_lookup_int(params, PARAM_HEADS);
1975        geometry.tracksperside          = option_resolution_lookup_int(params, PARAM_TRACKS);
1976        geometry.secspertrack           = option_resolution_lookup_int(params, PARAM_SECTORS);
1977*/
1978   }
1979   else
1980   {
1981      /* load */
1982      if (ti99_tdf_guess_geometry(floppy, floppy_image_size(floppy), &geometry)<50)
1983         return FLOPPY_ERROR_INVALIDIMAGE;
1984   }
1985
1986   assert(geometry.sides);
1987   assert(geometry.tracksperside);
1988   assert(geometry.secspertrack);
1989
1990   tag = (struct ti99dsk_tag *) floppy_create_tag(floppy, sizeof(struct ti99dsk_tag));
1991
1992   tag->heads = geometry.sides;
1993   tag->tracks = geometry.tracksperside;
1994
1995   if (geometry.density==TI99_FM)
1996      tag->track_size = geometry.secspertrack * TI99_FM_TOTAL_SECTOR + TI99_FM_TRACK_ADD;
1997   else
1998      tag->track_size = geometry.secspertrack * TI99_MFM_TOTAL_SECTOR + TI99_MFM_TRACK_ADD;
1999
2000   tag->format = geometry.density;
2001   tag->first_idam = 0; /* let's find out later */
2002   tag->dam_offset = 0;
2003
2004   /* set up format callbacks */
2005   callbacks = floppy_callbacks(floppy);
2006   callbacks->read_sector = ti99_tdf_read_sector;
2007   callbacks->write_sector = ti99_tdf_write_sector;
2008   callbacks->read_indexed_sector = ti99_tdf_read_indexed_sector;
2009   callbacks->write_indexed_sector = ti99_tdf_write_indexed_sector;
2010   callbacks->read_track = ti99_tdf_read_track;
2011   callbacks->write_track = ti99_tdf_write_track;
2012/*  callbacks->format_track = ti99_tdf_format_track; */
2013   callbacks->get_track_size = ti99_get_track_size;
2014   /* post format unset */
2015   callbacks->get_heads_per_disk = ti99_get_heads_per_disk;
2016   callbacks->get_tracks_per_disk = ti99_get_tracks_per_disk;
2017   callbacks->get_sector_length = ti99_get_sector_length;
2018   callbacks->get_indexed_sector_info = ti99_tdf_get_indexed_sector_info;
2019
2020   return FLOPPY_ERROR_SUCCESS;
2021}
2022
2023/* ----------------------------------------------------------------------- */
2024
2025FLOPPY_OPTIONS_START( ti99 )
2026   FLOPPY_OPTION( ti99_sdf, "dsk",         "TI99 sector dump (v9t9)",   ti99_sdf_identify,   ti99_sdf_construct, NULL,
2027      HEADS([1]-2)
2028      TRACKS(35-[40]-80)
2029      SECTORS(8/9/16/[18]/36)
2030      SECTOR_LENGTH([256])
2031      FIRST_SECTOR_ID(0))
2032   FLOPPY_OPTION( ti99_tdf, "dsk,dtk",         "TI99 track dump (pc99)",   ti99_tdf_identify,   ti99_tdf_construct, NULL,
2033      TRACKS(35-[40]-80)
2034      SECTORS(8/9/16/[18]/36)
2035      SECTOR_LENGTH([256])
2036      FIRST_SECTOR_ID(0))
2037FLOPPY_OPTIONS_END
src/mess/formats/p6001_cas.h
r11457r11458
1/*
2 * Nec PC-6001 cassette format handling
3 */
4
5#ifndef PC6001_CAS_H_
6#define PC6001_CAS_H_
7
8#include "imagedev/cassimg.h"
9
10CASSETTE_FORMATLIST_EXTERN( pc6001_cassette_formats );
11
12#endif /*PC6001_CAS_H_*/
src/mess/formats/fdi_dsk.c
r11457r11458
1/*********************************************************************
2
3    formats/fdi_dsk.h
4
5    Floppy format code for Formatted Disk Image (FDI) v2.0 images
6
7*********************************************************************/
8
9/*
10
11    TODO:
12
13    - everything
14
15*/
16
17#include "emu.h"
18#include "imageutl.h"
19#include "imagedev/flopimg.h"
20#include "imagedev/flopdrv.h"
21
22/***************************************************************************
23    PARAMETERS
24***************************************************************************/
25
26#define LOG 1
27
28#define MAX_TRACKS   180
29
30#define FLAGS_WRITE_PROTECTED   0x01
31#define FLAGS_INDEX_SYNC      0x02
32
33/* media type */
34enum
35{
36   TYPE_8_INCH = 0,
37   TYPE_5_25_INCH,
38   TYPE_3_5_INCH,
39   TYPE_3_INCH
40};
41
42static const char *const MEDIA_TYPE[] = { "8", "5.25", "3.5", "3" };
43
44/* tracks per inch */
45enum
46{
47   TPI_48 = 0,
48   TPI_67,
49   TPI_96,
50   TPI_100,
51   TPI_135,
52   TPI_192
53};
54
55static const int TRACKS_PER_INCH[] = { 48, 67, 96, 100, 135, 192 };
56
57/* high level track types */
58enum
59{
60   TRACK_BLANK = 0,   /* not supported */
61   TRACK_AMIGA_DD,      /* not supported */
62   TRACK_AMIGA_HD,      /* not supported */
63   TRACK_ST_DD_9,      /* not supported */
64   TRACK_ST_DD_10,      /* not supported */
65   TRACK_PC_DD_8,      /* not supported */
66   TRACK_PC_DD_9,      /* not supported */
67   TRACK_PC_HD_15,      /* not supported */
68   TRACK_IBM_HD_18,   /* not supported */
69   TRACK_IBM_ED_36,   /* not supported */
70   TRACK_CBM_1541,      /* not supported */
71   TRACK_APPLE_DOS_32,   /* not supported */
72   TRACK_APPLE_DOS_33,   /* not supported */
73   TRACK_APPLE_GCR,   /* not supported */
74   TRACK_IBM_SD_10      /* not supported */
75};
76
77//static const int SECTORS_PER_TRACK[] = { 0, 11, 22, 9, 10, 8, 9, 15, 18, 36, -1, -1, -1, -1, 10 };
78
79/***************************************************************************
80    TYPE DEFINITIONS
81***************************************************************************/
82
83struct fdidsk_tag
84{
85   int version;
86   int heads;                        /* number of physical heads */
87   int tracks;                        /* number of physical tracks */
88   UINT32 track_offset[MAX_TRACKS];      /* offset within data for each track */
89   UINT8 track_type[MAX_TRACKS];
90};
91
92struct fdi_track
93{
94   UINT8 type;
95   UINT8 size;
96};
97
98struct fdi_header
99{
100   UINT8 signature[27];
101   UINT8 creator[32];
102   UINT8 comment[80];
103   UINT8 eof;
104   UINT8 version[2];
105   UINT8 ltrack[2];
106   UINT8 lhead;
107   UINT8 type;
108   UINT8 rotspeed;
109   UINT8 flags;
110   UINT8 tpi;
111   UINT8 headwidth;
112   UINT8 reserved[2];
113   struct fdi_track track[MAX_TRACKS];
114};
115
116struct fdi_pulse_track_header
117{
118   UINT8 numpulses[4];
119   UINT8 averagesz[3];
120   UINT8 minsize[3];
121   UINT8 maxsize[3];
122   UINT8 indexsize[3];
123//  UINT8 averagedt[averagesz];
124//  UINT8 mindata[minsize];
125//  UINT8 maxdata[maxsize];
126//  UINT8 indexdata[indexsize];
127};
128
129/***************************************************************************
130    INLINE FUNCTIONS
131***************************************************************************/
132
133INLINE struct fdidsk_tag *get_tag(floppy_image *floppy)
134{
135   return (fdidsk_tag *)floppy_tag(floppy);
136}
137
138/***************************************************************************
139    IMPLEMENTATION
140***************************************************************************/
141
142static int fdi_get_heads_per_disk(floppy_image *floppy)
143{
144   return get_tag(floppy)->heads;
145}
146
147static int fdi_get_tracks_per_disk(floppy_image *floppy)
148{
149   return get_tag(floppy)->tracks;
150}
151
152static int fdi_get_sectors_per_track(floppy_image *floppy, int head, int track)
153{
154   return 0;
155}
156
157static floperr_t fdi_get_sector_length(floppy_image *floppy, int head, int track, int sector, UINT32 *sector_length)
158{
159   return FLOPPY_ERROR_SUCCESS;
160}
161/*
162static UINT32 fdi_get_sector_offset(floppy_image* floppy, int head, int track, int sector)
163{
164    return 0;
165}
166*/
167static floperr_t fdi_get_indexed_sector_info(floppy_image *floppy, int head, int track, int sector_index, int *cylinder, int *side, int *sector, UINT32 *sector_length, unsigned long *flags)
168{
169   return FLOPPY_ERROR_UNSUPPORTED;
170}
171
172static floperr_t fdi_read_track(floppy_image *floppy, int head, int track, UINT64 offset, void *buffer, size_t buflen)
173{
174   return FLOPPY_ERROR_UNSUPPORTED;
175}
176
177static floperr_t fdi_read_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen)
178{
179   return FLOPPY_ERROR_UNSUPPORTED;
180}
181
182static floperr_t fdi_read_indexed_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buffer_len)
183{
184   return FLOPPY_ERROR_UNSUPPORTED;
185}
186
187static floperr_t fdi_write_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam)
188{
189   return FLOPPY_ERROR_UNSUPPORTED;
190}
191
192static floperr_t fdi_write_indexed_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam)
193{
194   return FLOPPY_ERROR_UNSUPPORTED;
195}
196
197/*-------------------------------------------------
198    FLOPPY_IDENTIFY( fdi_dsk_identify )
199-------------------------------------------------*/
200
201FLOPPY_IDENTIFY( fdi_dsk_identify )
202{
203   UINT8 header[25];
204
205   floppy_image_read(floppy, header, 0, 25);
206
207   if (!strncmp((const char *)header, "Formatted Disk Image file", 25))
208   {
209      *vote = 100;
210   }
211   else
212   {
213      *vote = 0;
214   }
215
216   return FLOPPY_ERROR_SUCCESS;
217}
218
219/*-------------------------------------------------
220    FLOPPY_CONSTRUCT( fdi_dsk_construct )
221-------------------------------------------------*/
222
223FLOPPY_CONSTRUCT( fdi_dsk_construct )
224{
225   if (params)
226   {
227      /* create not supported */
228      return FLOPPY_ERROR_UNSUPPORTED;
229   }
230
231   struct fdidsk_tag *tag = (struct fdidsk_tag *) floppy_create_tag(floppy, sizeof(struct fdidsk_tag));
232
233   if (!tag) return FLOPPY_ERROR_OUTOFMEMORY;
234
235   /* read header */
236   struct fdi_header header;
237   floppy_image_read(floppy, &header, 0, sizeof(header));
238
239   tag->version = header.version[0];
240   tag->tracks = pick_integer_be(header.ltrack, 0, 2) + 1;
241   tag->heads = header.lhead + 1;
242
243   if (LOG)
244   {
245      logerror("FDI creator: %s\n", header.creator);
246      logerror("FDI comment: %s\n", header.comment);
247      logerror("FDI version: %u\n", tag->version);
248      logerror("FDI tracks: %u\n", tag->tracks);
249      logerror("FDI heads: %u\n", tag->heads);
250      logerror("FDI media type: %s\"\n", MEDIA_TYPE[header.type]);
251      logerror("FDI rotation speed: %u\n", header.rotspeed + 128);
252      if (header.flags & FLAGS_WRITE_PROTECTED) logerror("FDI is write protected\n");
253      if (header.flags & FLAGS_INDEX_SYNC) logerror("FDI is index synchronized\n");
254      logerror("FDI media TPI: %u\n", TRACKS_PER_INCH[header.tpi]);
255      logerror("FDI head TPI: %u\n", TRACKS_PER_INCH[header.headwidth]);
256   }
257
258   /* find track offsets */
259   int offset = sizeof(header);
260
261   for (int track = 0; track < tag->tracks; track++)
262   {
263      UINT8 type = header.track[track].type;
264      int size = header.track[track].size * 256;
265
266      if (LOG) logerror("FDI track %u type %02x size %u offset %u\n", track, type, size, offset);
267
268      tag->track_offset[track] = offset;
269      tag->track_type[track] = type;
270
271      offset += size;
272   }
273
274   /* set callbacks */
275   struct FloppyCallbacks *callbacks = floppy_callbacks(floppy);
276
277   callbacks->read_track = fdi_read_track;
278   callbacks->get_heads_per_disk = fdi_get_heads_per_disk;
279   callbacks->get_tracks_per_disk = fdi_get_tracks_per_disk;
280   callbacks->get_sector_length = fdi_get_sector_length;
281   callbacks->read_sector = fdi_read_sector;
282   callbacks->read_indexed_sector = fdi_read_indexed_sector;
283//  callbacks->write_track = fdi_write_track;
284   callbacks->write_sector = fdi_write_sector;
285   callbacks->write_indexed_sector = fdi_write_indexed_sector;
286   callbacks->get_indexed_sector_info = fdi_get_indexed_sector_info;
287   callbacks->get_sectors_per_track = fdi_get_sectors_per_track;
288
289   return FLOPPY_ERROR_SUCCESS;
290}
src/mess/formats/ti99_dsk.h
r11457r11458
1/*********************************************************************
2
3    formats/ti99_dsk.c
4
5    TI99 and Geneve disk images
6
7    Header file to be included by drivers which use these floppy options
8*********************************************************************/
9
10#ifndef TI99_DSK_H
11#define TI99_DSK_H
12
13#include "imagedev/flopimg.h"
14
15FLOPPY_OPTIONS_EXTERN(ti99);
16
17void ti99_set_80_track_drives(int use80);
18
19#endif /* TI99_DSK_H */
src/mess/formats/mz_cas.c
r11457r11458
1#include <string.h>
2#include "mz_cas.h"
3#include "emu.h"
4
5#ifndef VERBOSE
6#define VERBOSE 0
7#endif
8
9//#define LOG(N,M,A)
10//  if(VERBOSE>=N){ if( M )logerror("%11.6f: %-24s",machine.time().as_double(), (const char*)M ); logerror A; }
11
12#define LOG(N,M,A)   \
13   if(VERBOSE>=N){ if( M )logerror("%-24s",(const char*)M ); logerror A; }
14
15#define LO  -32768
16#define HI   +32767
17
18#define SHORT_PULSE 2
19#define LONG_PULSE   4
20
21#define BYTE_SAMPLES (LONG_PULSE+8*LONG_PULSE)
22
23#define SILENCE    8000
24
25/* long gap and tape mark */
26#define LGAP        22000
27#define LTM_1      40
28#define LTM_0      40
29#define LTM_L      1
30
31/* short gap and tape mark */
32#define SGAP        11000
33#define STM_1      20
34#define STM_0      20
35#define STM_L      1
36
37static int fill_wave_1(INT16 *buffer, int offs)
38{
39   buffer[offs++] = HI;
40   buffer[offs++] = HI;
41   buffer[offs++] = LO;
42    buffer[offs++] = LO;
43    return LONG_PULSE;
44}
45
46static int fill_wave_0(INT16 *buffer, int offs)
47{
48   buffer[offs++] = HI;
49   buffer[offs++] = LO;
50   return SHORT_PULSE;
51}
52
53static int fill_wave_b(INT16 *buffer, int offs, int byte)
54{
55    int i, count = 0;
56
57   /* data bits are preceded by a long pulse */
58    count += fill_wave_1(buffer, offs + count);
59
60    for( i = 7; i >= 0; i-- )
61   {
62      if( (byte >> i) & 1 )
63         count += fill_wave_1(buffer, offs + count);
64      else
65         count += fill_wave_0(buffer, offs + count);
66   }
67   return count;
68}
69
70static int fill_wave(INT16 *buffer, int length, UINT8 *code)
71{
72   static INT16 *beg;
73    static UINT16 csum = 0;
74   static int header = 1, bytecount = 0;
75   int count = 0;
76
77    if( code == CODE_HEADER )
78    {
79      int i;
80
81      /* is there insufficient space for the LGAP? */
82      if( count + LGAP * SHORT_PULSE > length )
83            return -1;
84      LOG(1,"mz700 fill_wave",("LGAP %d samples\n", LGAP * SHORT_PULSE));
85        /* fill long gap */
86      for( i = 0; i < LGAP; i++ )
87         count += fill_wave_0(buffer, count);
88
89        /* make a long tape mark */
90      /* is there insufficient space for the LTM 1? */
91      if( count + LTM_1 * LONG_PULSE > length )
92            return -1;
93      LOG(1,"mz700_fill_wave",("LTM 1 %d samples\n", LTM_1 * LONG_PULSE));
94      for( i = 0; i < LTM_1; i++ )
95         count += fill_wave_1(buffer, count);
96
97      /* is there insufficient space for the LTM 0? */
98      if( count + LTM_0 * SHORT_PULSE > length )
99            return -1;
100      LOG(1,"mz700_fill_wave",("LTM 0 %d samples\n", LTM_0 * SHORT_PULSE));
101      for( i = 0; i < LTM_0; i++ )
102         count += fill_wave_0(buffer, count);
103
104      /* is there insufficient space for the L? */
105      if( count + LTM_L * LONG_PULSE > length )
106            return -1;
107      LOG(1,"mz700_fill_wave",("L %d samples\n", LONG_PULSE));
108        count += fill_wave_1(buffer, count);
109
110      /* reset header, bytecount and checksum */
111        header = 1;
112        bytecount = 0;
113        csum = 0;
114
115      /* HDR begins here */
116        beg = buffer + count;
117
118        return count;
119    }
120
121   if( code == CODE_TRAILER )
122    {
123      int i, file_length;
124      INT16 *end = buffer;
125
126      /* is there insufficient space for the CHKF? */
127      if( count + 2 * BYTE_SAMPLES > length )
128            return -1;
129      LOG(1,"mz700_fill_wave",("CHKF 0x%04X\n", csum));
130      count += fill_wave_b(buffer, count, csum >> 8);
131      count += fill_wave_b(buffer, count, csum & 0xff);
132
133        /* is there insufficient space for the L */
134      if( count + LTM_L * LONG_PULSE > length )
135            return -1;
136      LOG(1,"mz700_fill_wave",("L\n"));
137        count += fill_wave_1(buffer, count);
138
139      /* is there insufficient space for the 256S pulses? */
140      if( count + 256 * SHORT_PULSE > length )
141            return -1;
142      LOG(1,"mz700_fill_wave",("256S\n"));
143        for (i = 0; i < 256; i++)
144         count += fill_wave_0(buffer, count);
145
146      file_length = (int)(end - beg) / sizeof(INT16);
147      /* is there insufficient space for the FILEC ? */
148      if( count + file_length > length )
149            return -1;
150      LOG(1,"mz700_fill_wave",("FILEC %d samples\n", file_length));
151      memcpy(buffer + count, beg, file_length * sizeof(INT16));
152      count += file_length;
153
154      /* is there insufficient space for the CHKF ? */
155      if( count + 2 * BYTE_SAMPLES > length )
156            return -1;
157      LOG(1,"mz700_fill_wave",("CHKF 0x%04X\n", csum));
158      count += fill_wave_b(buffer, count, csum >> 8);
159      count += fill_wave_b(buffer, count, csum & 0xff);
160
161      /* is there insufficient space for the L ? */
162      if( count + STM_L * LONG_PULSE > length )
163            return -1;
164      LOG(1,"mz700_fill_wave",("L %d samples\n", LONG_PULSE));
165        count += fill_wave_1(buffer, count);
166
167      LOG(1,"mz700_fill_wave",("silence %d samples\n", SILENCE));
168        /* silence at the end */
169      for( i = 0; i < SILENCE; i++ )
170         buffer[count++] = 0;
171
172      return count;
173    }
174
175   if( header == 1 && bytecount == 128 )
176   {
177      int i, hdr_length;
178      INT16 *end = buffer;
179
180      /* is there insufficient space for the CHKH ? */
181      if( count + 2 * BYTE_SAMPLES > length )
182            return -1;
183      LOG(1,"mz700_fill_wave",("CHKH 0x%04X\n", csum & 0xffff));
184      count += fill_wave_b(buffer, count, (csum >> 8) & 0xff);
185      count += fill_wave_b(buffer, count, csum & 0xff);
186
187      /* is there insufficient space for the L ? */
188      if( count + LONG_PULSE > length )
189            return -1;
190      LOG(1,"mz700_fill_wave",("L %d samples\n", LONG_PULSE));
191        count += fill_wave_1(buffer, count);
192
193      /* is there insufficient space for the 256S ? */
194      if( count + 256 * SHORT_PULSE > length )
195            return -1;
196      LOG(1,"mz700_fill_wave",("256S\n"));
197        for (i = 0; i < 256; i++)
198         count += fill_wave_0(buffer, count);
199
200      hdr_length = (int)(end - beg) / sizeof(INT16);
201      /* is there insufficient space for the HDRC ? */
202      if( count + hdr_length > length )
203            return -1;
204      LOG(1,"mz700_fill_wave",("HDRC %d samples\n", hdr_length));
205      memcpy(buffer + count, beg, hdr_length * sizeof(INT16));
206      count += hdr_length;
207
208      /* is there insufficient space for CHKH ? */
209      if( count + 2 * BYTE_SAMPLES > length )
210            return -1;
211      LOG(1,"mz700_fill_wave",("CHKH 0x%04X\n", csum & 0xffff));
212      count += fill_wave_b(buffer, count, (csum >> 8) & 0xff);
213      count += fill_wave_b(buffer, count, csum & 0xff);
214
215      /* is there insufficient space for the L ? */
216      if( count + LONG_PULSE > length )
217            return -1;
218      LOG(1,"mz700_fill_wave",("L %d samples\n", LONG_PULSE));
219        count += fill_wave_1(buffer, count);
220
221      /* is there sufficient space for the SILENCE? */
222      if( count + SILENCE > length )
223            return -1;
224      LOG(1,"mz700_fill_wave",("SILENCE %d samples\n", SILENCE));
225      /* fill silence */
226      for( i = 0; i < SILENCE; i++ )
227         buffer[count++] = 0;
228
229        /* is there sufficient space for the SGAP? */
230      if( count + SGAP * SHORT_PULSE > length )
231            return -1;
232      LOG(1,"mz700_fill_wave",("SGAP %d samples\n", SGAP * SHORT_PULSE));
233      /* fill short gap */
234      for( i = 0; i < SGAP; i++ )
235         count += fill_wave_0(buffer, count);
236
237        /* make a short tape mark */
238
239        /* is there sufficient space for the STM 1? */
240      if( count + STM_1 * LONG_PULSE > length )
241            return -1;
242      LOG(1,"mz700_fill_wave",("STM 1 %d samples\n", STM_1 * LONG_PULSE));
243      for( i = 0; i < STM_1; i++ )
244         count += fill_wave_1(buffer, count);
245
246      /* is there sufficient space for the STM 0? */
247      if( count + STM_0 * SHORT_PULSE > length )
248            return -1;
249      LOG(1,"mz700_fill_wave",("STM 0 %d samples\n", STM_0 * SHORT_PULSE));
250      for( i = 0; i < STM_0; i++ )
251         count += fill_wave_0(buffer, count);
252
253      /* is there sufficient space for the L? */
254      if( count + STM_L * LONG_PULSE > length )
255            return -1;
256      LOG(1,"mz700_fill_wave",("L %d samples\n", LONG_PULSE));
257        count += fill_wave_1(buffer, count);
258
259      bytecount = 0;
260      header = 0;
261      csum = 0;
262
263      /* FILE begins here */
264        beg = buffer + count;
265   }
266
267   if( length < BYTE_SAMPLES )
268        return -1;
269
270    if (*code & 0x01) csum++;
271    if (*code & 0x02) csum++;
272    if (*code & 0x04) csum++;
273    if (*code & 0x08) csum++;
274    if (*code & 0x10) csum++;
275    if (*code & 0x20) csum++;
276    if (*code & 0x40) csum++;
277    if (*code & 0x80) csum++;
278
279    bytecount++;
280
281   count += fill_wave_b(buffer, count, *code);
282
283    return count;
284}
285
286#define MZ700_WAVESAMPLES_HEADER   (   \
287      LGAP * SHORT_PULSE +         \
288      LTM_1 * LONG_PULSE +         \
289      LTM_0 * SHORT_PULSE +         \
290      LTM_L * LONG_PULSE +         \
291      2 * 2 * BYTE_SAMPLES +         \
292   SILENCE +                     \
293   SGAP * SHORT_PULSE +            \
294      STM_1 * LONG_PULSE +         \
295      STM_0 * SHORT_PULSE +         \
296      STM_L * LONG_PULSE +         \
297      2 * 2 * BYTE_SAMPLES)
298
299
300
301
302static const struct CassetteLegacyWaveFiller mz700_legacy_fill_wave =
303{
304   fill_wave,               /* fill_wave */
305   1,                     /* chunk_size */
306   2 * BYTE_SAMPLES,         /* chunk_samples */
307   NULL,                  /* chunk_sample_calc */
308   5120,                  /* sample_frequency */
309   MZ700_WAVESAMPLES_HEADER,   /* header_samples */
310   1                     /* trailer_samples */
311};
312
313
314
315static casserr_t mz700_cas_identify(cassette_image *cassette, struct CassetteOptions *opts)
316{
317   return cassette_legacy_identify(cassette, opts, &mz700_legacy_fill_wave);
318}
319
320
321
322static casserr_t mz700_cas_load(cassette_image *cassette)
323{
324   return cassette_legacy_construct(cassette, &mz700_legacy_fill_wave);
325}
326
327
328
329static const struct CassetteFormat mz700_cas_format =
330{
331   "m12,mzf",
332   mz700_cas_identify,
333   mz700_cas_load,
334   NULL
335};
336
337
338
339CASSETTE_FORMATLIST_START(mz700_cassette_formats)
340   CASSETTE_FORMAT(mz700_cas_format)
341CASSETTE_FORMATLIST_END
src/mess/formats/gtp_cas.c
r11457r11458
1/*
2
3    Tape support for Glaksija GTP format
4
5    Miodrag Milanovic
6*/
7#include "emu.h"
8#include "formats/gtp_cas.h"
9
10
11#define GTP_WAV_FREQUENCY   44100
12#define WAVE_LOW      -0x5a9e
13#define WAVE_HIGH      0x5a9e
14#define WAVE_NULL      0
15
16#define   GTP_BLOCK_STANDARD   0x00
17#define   GTP_BLOCK_TURBO      0x01
18#define   GTP_BLOCK_NAME      0x10
19
20static INT16   wave_data;
21static INT16   len;
22
23#define PULSE_WIDTH      30
24#define PERIOD_BASE      150
25#define PERIOD_1      75
26#define PERIOD_0      150
27
28#define INTERBYTE_PAUSE      225
29#define INTERBLOCK_PAUSE   100000
30
31
32static void gtp_output_wave( INT16 **buffer, int length ) {
33   if ( buffer == NULL ) {
34      return;
35   }
36
37   for( ; length > 0; length-- ) {
38      **buffer = wave_data;
39      *buffer = *buffer + 1;
40   }
41}
42
43
44
45static int gtp_mod_1( INT16 **buffer )
46{
47   wave_data = WAVE_LOW;
48   gtp_output_wave(buffer,PULSE_WIDTH);
49   wave_data = WAVE_HIGH;
50   gtp_output_wave(buffer,PULSE_WIDTH);
51   wave_data = WAVE_NULL;
52   gtp_output_wave(buffer,PERIOD_1 - 2 * PULSE_WIDTH);
53   wave_data = WAVE_LOW;
54   gtp_output_wave(buffer,PULSE_WIDTH);
55   wave_data = WAVE_HIGH;
56   gtp_output_wave(buffer,PULSE_WIDTH);
57   wave_data = WAVE_NULL;
58   gtp_output_wave(buffer,PERIOD_1 - 2 * PULSE_WIDTH);
59
60   return PERIOD_1 * 2;
61}
62
63static int gtp_mod_0( INT16 **buffer )
64{
65   wave_data = WAVE_LOW;
66   gtp_output_wave(buffer,PULSE_WIDTH);
67   wave_data = WAVE_HIGH;
68   gtp_output_wave(buffer,PULSE_WIDTH);
69   wave_data = WAVE_NULL;
70   gtp_output_wave(buffer,PERIOD_0 - 2 * PULSE_WIDTH);
71
72   return PERIOD_0;
73}
74
75static int gtp_byte( INT16 **buffer, UINT8 val )
76{
77   UINT8 b;
78   int j,size = 0;
79   for (j=0;j<8;j++) {
80      b = (val >> j) & 1;
81      if (b==0) {
82         size += gtp_mod_0(buffer);
83      } else {
84         size += gtp_mod_1(buffer);
85      }
86   }
87   return size;
88}
89
90static int gtp_sync( INT16 **buffer )
91{
92   int i;
93   int size = 0;
94
95   for(i=0;i<100;i++) {
96      if (i!=0) {
97         // Interbyte pause
98         wave_data = WAVE_NULL;
99         gtp_output_wave(buffer,INTERBYTE_PAUSE);
100         size += INTERBYTE_PAUSE;
101      }
102      size += gtp_byte(buffer,0);
103   }
104   return size;
105}
106
107static int gtp_cas_to_wav_size( const UINT8 *casdata, int caslen ) {
108   int size,n;
109   size = 0;
110   n = 0;
111   if (casdata == NULL) return -1;
112   while(n<caslen) {
113      int block_type = casdata[n];
114      int block_size = casdata[n+2]*256 + casdata[n+1];
115      n+=5;
116      if (block_type==GTP_BLOCK_STANDARD) {
117         // Interblock pause
118         size += INTERBLOCK_PAUSE;
119         size += 100 * (PERIOD_0 * 8 + INTERBYTE_PAUSE) - INTERBYTE_PAUSE;
120         size += (PERIOD_0 * 8 + INTERBYTE_PAUSE) * block_size;
121      }
122      n += block_size;
123   }
124   len = caslen;
125   return size;
126}
127
128static int gtp_cas_fill_wave( INT16 *buffer, int length, UINT8 *bytes ) {
129   int i,size,n;
130   size = 0;
131   n = 0;
132   if (bytes == NULL) return -1;
133   while(n<len)
134   {
135      int block_type = bytes[n];
136      int block_size = bytes[n+2]*256 + bytes[n+1];
137      n+=5;
138      if (block_type==GTP_BLOCK_STANDARD) {
139
140         // Interblock pause
141         wave_data = WAVE_NULL;
142         gtp_output_wave(&buffer,INTERBLOCK_PAUSE);
143         size += INTERBLOCK_PAUSE;
144         size += gtp_sync(&buffer);
145
146         for (i=0;i<block_size;i++) {
147            // Interbyte pause
148            wave_data = WAVE_NULL;
149            gtp_output_wave(&buffer,INTERBYTE_PAUSE);
150            size += INTERBYTE_PAUSE;
151
152            size += gtp_byte(&buffer,bytes[n]);
153            n++;
154         }
155      } else {
156         n += block_size;
157      }
158   }
159   return size;
160}
161
162
163
164
165static const struct CassetteLegacyWaveFiller gtp_legacy_fill_wave = {
166   gtp_cas_fill_wave,         /* fill_wave */
167   -1,               /* chunk_size */
168   0,               /* chunk_samples */
169   gtp_cas_to_wav_size,         /* chunk_sample_calc */
170   GTP_WAV_FREQUENCY,         /* sample_frequency */
171   0,               /* header_samples */
172   0               /* trailer_samples */
173};
174
175
176
177static casserr_t gtp_cassette_identify( cassette_image *cassette, struct CassetteOptions *opts ) {
178   return cassette_legacy_identify( cassette, opts, &gtp_legacy_fill_wave );
179}
180
181
182
183static casserr_t gtp_cassette_load( cassette_image *cassette ) {
184   return cassette_legacy_construct( cassette, &gtp_legacy_fill_wave );
185}
186
187
188
189static const struct CassetteFormat gtp_cassette_format = {
190   "gtp",
191   gtp_cassette_identify,
192   gtp_cassette_load,
193   NULL
194};
195
196
197CASSETTE_FORMATLIST_START(gtp_cassette_formats)
198   CASSETTE_FORMAT(gtp_cassette_format)
199CASSETTE_FORMATLIST_END
200
201
src/mess/formats/mz_cas.h
r11457r11458
1/*********************************************************************
2
3    mz_cas.h
4
5    Format code for MZ700 casette files
6
7*********************************************************************/
8
9#ifndef MZ_CAS_H
10#define MZ_CAS_H
11
12#include "imagedev/cassimg.h"
13
14CASSETTE_FORMATLIST_EXTERN(mz700_cassette_formats);
15
16#endif /* MZ_CAS_H */
src/mess/formats/ap2_dsk.c
r11457r11458
1/*********************************************************************
2
3    ap2_dsk.c
4
5    Apple II disk images
6
7*********************************************************************/
8
9#include <stdlib.h>
10#include <string.h>
11#include <assert.h>
12
13#include "ap2_dsk.h"
14#include "formats/basicdsk.h"
15
16
17#define APPLE2_IMAGE_DO      0
18#define APPLE2_IMAGE_PO      1
19#define APPLE2_IMAGE_NIB   2
20
21
22/* used in for all Apple II images */
23static UINT32 apple2_get_track_size(floppy_image *floppy, int head, int track);
24static int disk_decode_nib(UINT8 *data, const UINT8 *nibble, int *volume, int *track, int *sector);
25static void disk_encode_nib(UINT8 *nibble, const UINT8 *data, int volume, int track, int sector);
26
27/* used in DOS/ProDOS order images */
28static int apple2_do_translate_sector(floppy_image *floppy, int sector);
29static int apple2_po_translate_sector(floppy_image *floppy, int sector);
30static floperr_t apple2_dsk_read_track(floppy_image *floppy, int head, int track, UINT64 offset, void *buffer, size_t buflen);
31static floperr_t apple2_dsk_write_track(floppy_image *floppy, int head, int track, UINT64 offset, const void *buffer, size_t buflen);
32
33/* used in nibble order images */
34static floperr_t apple2_nib_read_track(floppy_image *floppy, int head, int track, UINT64 offset, void *buffer, size_t buflen);
35static floperr_t apple2_nib_write_track(floppy_image *floppy, int head, int track, UINT64 offset, const void *buffer, size_t buflen);
36static floperr_t apple2_nib_read_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen);
37static floperr_t apple2_nib_write_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam);
38static floperr_t apple2_nib_get_sector_length(floppy_image *floppy, int head, int track, int sector, UINT32 *sector_length);
39
40
41static const UINT8 translate6[0x40] =
42{
43   0x96, 0x97, 0x9a, 0x9b, 0x9d, 0x9e, 0x9f, 0xa6,
44   0xa7, 0xab, 0xac, 0xad, 0xae, 0xaf, 0xb2, 0xb3,
45   0xb4, 0xb5, 0xb6, 0xb7, 0xb9, 0xba, 0xbb, 0xbc,
46   0xbd, 0xbe, 0xbf, 0xcb, 0xcd, 0xce, 0xcf, 0xd3,
47   0xd6, 0xd7, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde,
48   0xdf, 0xe5, 0xe6, 0xe7, 0xe9, 0xea, 0xeb, 0xec,
49   0xed, 0xee, 0xef, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6,
50   0xf7, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff
51};
52
53
54
55/* -----------------------------------------------------------------------
56 * Utility code
57 * ----------------------------------------------------------------------- */
58
59static const UINT8 *get_untranslate6_map(void)
60{
61   static UINT8 map[256];
62   static int map_inited = 0;
63   UINT8 i;
64
65   if (!map_inited)
66   {
67      memset(map, 0xff, sizeof(map));
68      for (i = 0; i < ARRAY_LENGTH(translate6); i++)
69         map[translate6[i]] = i;
70      map_inited = 1;
71   }
72   return map;
73}
74
75
76
77/* -----------------------------------------------------------------------
78 * Core constructor
79 * ----------------------------------------------------------------------- */
80
81
82static floperr_t apple2_general_construct(floppy_image *floppy, int floppy_type)
83{
84   floperr_t err;
85   struct basicdsk_geometry geometry;
86   struct FloppyCallbacks *format;
87
88   format = floppy_callbacks(floppy);
89
90   switch(floppy_type) {
91   case APPLE2_IMAGE_DO:
92   case APPLE2_IMAGE_PO:
93      memset(&geometry, 0, sizeof(geometry));
94      geometry.heads = 1;
95      geometry.tracks = APPLE2_TRACK_COUNT;
96      geometry.sectors = APPLE2_SECTOR_COUNT;
97      geometry.sector_length = APPLE2_SECTOR_SIZE;
98      geometry.translate_sector = (floppy_type == APPLE2_IMAGE_DO) ? apple2_do_translate_sector : apple2_po_translate_sector;
99
100      err = basicdsk_construct(floppy, &geometry);
101      if (err)
102         return err;
103
104      format->read_track = apple2_dsk_read_track;
105      format->write_track = apple2_dsk_write_track;
106      break;
107
108   case APPLE2_IMAGE_NIB:
109      format->read_track = apple2_nib_read_track;
110      format->write_track = apple2_nib_write_track;
111      format->read_sector = apple2_nib_read_sector;
112      format->write_sector = apple2_nib_write_sector;
113      format->get_sector_length = apple2_nib_get_sector_length;
114      break;
115
116   default:
117      assert(0);
118      return FLOPPY_ERROR_INTERNAL;
119   }
120
121   format->get_track_size = apple2_get_track_size;
122
123   return FLOPPY_ERROR_SUCCESS;
124}
125
126
127
128/* -----------------------------------------------------------------------
129 * DOS order and ProDOS order code
130 * ----------------------------------------------------------------------- */
131
132static FLOPPY_IDENTIFY(apple2_dsk_identify)
133{
134   UINT64 size;
135   UINT64 expected_size;
136
137   size = floppy_image_size(floppy);
138   expected_size = APPLE2_TRACK_COUNT * APPLE2_SECTOR_COUNT * APPLE2_SECTOR_SIZE;
139
140   if (size == expected_size)
141      *vote = 100;
142   else if (size - expected_size < 8 && size - expected_size > -8)
143      *vote = 90;      /* tolerate images with up to eight fewer/extra bytes (bug #638) */
144   else
145      *vote = 0;
146   return FLOPPY_ERROR_SUCCESS;
147}
148
149
150
151static int apple2_do_translate_sector(floppy_image *floppy, int sector)
152{
153   static const UINT8 skewing[] =
154   {
155      /* DOS order (*.do) */
156      0x00, 0x07, 0x0E, 0x06, 0x0D, 0x05, 0x0C, 0x04,
157      0x0B, 0x03, 0x0A, 0x02, 0x09, 0x01, 0x08, 0x0F
158   };
159   return skewing[sector];
160}
161
162
163
164static int apple2_po_translate_sector(floppy_image *floppy, int sector)
165{
166   static const UINT8 skewing[] =
167   {
168      /* ProDOS order (*.po) */
169      0x00, 0x08, 0x01, 0x09, 0x02, 0x0A, 0x03, 0x0B,
170      0x04, 0x0C, 0x05, 0x0D, 0x06, 0x0E, 0x07, 0x0F
171   };
172   return skewing[sector];
173}
174
175
176
177static FLOPPY_CONSTRUCT(apple2_do_construct)
178{
179   return apple2_general_construct(floppy, APPLE2_IMAGE_DO);
180}
181
182
183
184static FLOPPY_CONSTRUCT(apple2_po_construct)
185{
186   return apple2_general_construct(floppy, APPLE2_IMAGE_PO);
187}
188
189
190
191static floperr_t apple2_dsk_read_track(floppy_image *floppy, int head, int track, UINT64 offset, void *buffer, size_t buflen)
192{
193   UINT8 sector_buffer[APPLE2_SECTOR_SIZE];
194   int sector;
195   UINT8 *nibble;
196
197   if (buflen < APPLE2_NIBBLE_SIZE*APPLE2_SECTOR_COUNT)
198      return FLOPPY_ERROR_INTERNAL;
199   if (offset != 0)
200      return FLOPPY_ERROR_UNSUPPORTED;
201
202   memset(buffer, 0, buflen);
203
204   for (sector = 0; sector < APPLE2_SECTOR_COUNT; sector++)
205   {
206      nibble = (UINT8 *)buffer;
207      nibble += sector * APPLE2_SMALL_NIBBLE_SIZE;
208
209      floppy_read_sector(floppy, head, track, sector, 0, sector_buffer, sizeof(sector_buffer));
210      disk_encode_nib(nibble, sector_buffer, 254, track, sector);
211   }
212   return FLOPPY_ERROR_SUCCESS;
213}
214
215
216
217static floperr_t apple2_dsk_write_track(floppy_image *floppy, int head, int track, UINT64 offset, const void *buffer, size_t buflen)
218{
219   int sector;
220   UINT8 sector_buffer[APPLE2_SECTOR_SIZE];
221   const UINT8 *nibble;
222
223   if (offset != 0)
224      return FLOPPY_ERROR_UNSUPPORTED;
225
226   for (sector = 0; sector < APPLE2_SECTOR_COUNT; sector++)
227   {
228      nibble = (UINT8 *)buffer;
229      nibble += sector * APPLE2_SMALL_NIBBLE_SIZE;
230
231      disk_decode_nib(sector_buffer, nibble, NULL, NULL, NULL);
232      floppy_write_sector(floppy, head, track, sector, 0, sector_buffer, sizeof(sector_buffer), 0);
233   }
234
235   return FLOPPY_ERROR_SUCCESS;
236}
237
238
239
240/* -----------------------------------------------------------------------
241 * Nibble order code
242 * ----------------------------------------------------------------------- */
243
244static FLOPPY_IDENTIFY(apple2_nib_identify)
245{
246   UINT64 size;
247   size = floppy_image_size(floppy);
248   *vote = (size == APPLE2_TRACK_COUNT * APPLE2_SECTOR_COUNT * APPLE2_NIBBLE_SIZE) ? 100 : 0;
249   return FLOPPY_ERROR_SUCCESS;
250}
251
252
253
254static FLOPPY_CONSTRUCT(apple2_nib_construct)
255{
256   return apple2_general_construct(floppy, APPLE2_IMAGE_NIB);
257}
258
259
260
261static floperr_t apple2_nib_read_track(floppy_image *floppy, int head, int track, UINT64 offset, void *buffer, size_t buflen)
262{
263   if ((head != 0) || (track < 0) || (track >= APPLE2_TRACK_COUNT))
264      return FLOPPY_ERROR_SEEKERROR;
265   if (offset != 0)
266      return FLOPPY_ERROR_UNSUPPORTED;
267   floppy_image_read(floppy, buffer, track * APPLE2_NIBBLE_SIZE * APPLE2_SECTOR_COUNT, buflen);
268   return FLOPPY_ERROR_SUCCESS;
269}
270
271
272
273static floperr_t apple2_nib_write_track(floppy_image *floppy, int head, int track, UINT64 offset, const void *buffer, size_t buflen)
274{
275   if ((head != 0) || (track < 0) || (track >= APPLE2_TRACK_COUNT))
276      return FLOPPY_ERROR_SEEKERROR;
277   if (offset != 0)
278      return FLOPPY_ERROR_UNSUPPORTED;
279   floppy_image_write(floppy, buffer, track * APPLE2_NIBBLE_SIZE * APPLE2_SECTOR_COUNT, buflen);
280   return FLOPPY_ERROR_SUCCESS;
281}
282
283
284
285/* -----------------------------------------------------------------------
286 * Track conversion
287 * ----------------------------------------------------------------------- */
288
289static int decode_nibbyte(UINT8 *nibint, const UINT8 *nibdata)
290{
291   if ((nibdata[0] & 0xAA) != 0xAA)
292      return 1;
293   if ((nibdata[1] & 0xAA) != 0xAA)
294      return 1;
295
296   *nibint =  (nibdata[0] & ~0xAA) << 1;
297   *nibint |= (nibdata[1] & ~0xAA) << 0;
298   return 0;
299}
300
301
302
303static int disk_decode_nib(UINT8 *data, const UINT8 *nibble, int *volume, int *track, int *sector)
304{
305   UINT8 read_volume;
306   UINT8 read_track;
307   UINT8 read_sector;
308   UINT8 read_checksum;
309   int i;
310   UINT8 b, xorvalue, newvalue;
311
312   const UINT8 *untranslate6 = get_untranslate6_map();
313
314   /* pick apart the volume/track/sector info and checksum */
315   if (decode_nibbyte(&read_volume, &nibble[10]))
316      return 1;
317   if (decode_nibbyte(&read_track, &nibble[12]))
318      return 1;
319   if (decode_nibbyte(&read_sector, &nibble[14]))
320      return 1;
321   if (decode_nibbyte(&read_checksum, &nibble[16]))
322      return 1;
323   if (read_checksum != (read_volume ^ read_track ^ read_sector))
324      return 1;
325
326   /* decode the nibble core */
327   xorvalue = 0;
328   for (i = 0; i < 342; i++)
329   {
330      b = untranslate6[nibble[i+28]];
331      if (b == 0xff)
332         return 1;
333      newvalue = b ^ xorvalue;
334
335      if (i >= 0x56)
336      {
337         /* 6 bit */
338         data[i - 0x56] |= (newvalue << 2);
339      }
340      else
341      {
342         /* 3 * 2 bit */
343         data[i + 0x00] = ((newvalue >> 1) & 0x01) | ((newvalue << 1) & 0x02);
344         data[i + 0x56] = ((newvalue >> 3) & 0x01) | ((newvalue >> 1) & 0x02);
345         if (i + 0xAC < APPLE2_SECTOR_SIZE)
346            data[i + 0xAC] = ((newvalue >> 5) & 0x01) | ((newvalue >> 3) & 0x02);
347      }
348      xorvalue = newvalue;
349   }
350
351   /* success; write out values if pointers not null */
352   if (volume)
353      *volume = read_volume;
354   if (track)
355      *track = read_track;
356   if (sector)
357      *sector = read_sector;
358   return 0;
359}
360
361
362
363static floperr_t apple2_nib_read_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen)
364{
365   floperr_t err;
366   const UINT8 *nibble;
367   UINT8 *track_data;
368   void *track_data_v;
369
370   if ((sector < 0) || (sector >= APPLE2_SECTOR_COUNT))
371      return FLOPPY_ERROR_SEEKERROR;
372   if (buflen != APPLE2_SECTOR_SIZE)
373      return FLOPPY_ERROR_INTERNAL;
374
375   err = floppy_load_track(floppy, head, track, FALSE, &track_data_v, NULL);
376   if (err)
377      return err;
378   track_data = (UINT8 *) track_data_v;
379
380   nibble = track_data + (sector * APPLE2_NIBBLE_SIZE);
381
382   if (disk_decode_nib((UINT8 *)buffer, nibble, NULL, NULL, NULL))
383      return FLOPPY_ERROR_INVALIDIMAGE;
384
385   return FLOPPY_ERROR_SUCCESS;
386}
387
388
389
390static void disk_encode_nib(UINT8 *nibble, const UINT8 *data, int volume, int track, int sector)
391{
392   int checksum, oldvalue, xorvalue, i;
393
394   /* setup header values */
395   checksum = volume ^ track ^ sector;
396
397   memset(nibble, 0xFF, APPLE2_NIBBLE_SIZE);
398   nibble[ 7]     = 0xD5;
399   nibble[ 8]     = 0xAA;
400   nibble[ 9]     = 0x96;
401   nibble[10]     = (volume >> 1) | 0xAA;
402   nibble[11]     = volume | 0xAA;
403   nibble[12]     = (track >> 1) | 0xAA;
404   nibble[13]     = track | 0xAA;
405   nibble[14]     = (sector >> 1) | 0xAA;
406   nibble[15]     = sector | 0xAA;
407   nibble[16]     = (checksum >> 1) | 0xAA;
408   nibble[17]     = (checksum) | 0xAA;
409   nibble[18]     = 0xDE;
410   nibble[19]     = 0xAA;
411   nibble[20]     = 0xEB;
412   nibble[25]     = 0xD5;
413   nibble[26]     = 0xAA;
414   nibble[27]     = 0xAD;
415   nibble[27+344] = 0xDE;
416   nibble[27+345] = 0xAA;
417   nibble[27+346] = 0xEB;
418   xorvalue = 0;
419
420   for (i = 0; i < 342; i++)
421   {
422      if (i >= 0x56)
423      {
424         /* 6 bit */
425         oldvalue = data[i - 0x56];
426         oldvalue = oldvalue >> 2;
427      }
428      else
429      {
430         /* 3 * 2 bit */
431         oldvalue = 0;
432         oldvalue |= (data[i + 0x00] & 0x01) << 1;
433         oldvalue |= (data[i + 0x00] & 0x02) >> 1;
434         oldvalue |= (data[i + 0x56] & 0x01) << 3;
435         oldvalue |= (data[i + 0x56] & 0x02) << 1;
436         if (i + 0xAC < APPLE2_SECTOR_SIZE)
437         {
438            oldvalue |= (data[i + 0xAC] & 0x01) << 5;
439            oldvalue |= (data[i + 0xAC] & 0x02) << 3;
440         }
441
442      }
443      xorvalue ^= oldvalue;
444      nibble[28+i] = translate6[xorvalue & 0x3F];
445      xorvalue = oldvalue;
446   }
447
448   nibble[27+343] = translate6[xorvalue & 0x3F];
449}
450
451
452
453static floperr_t apple2_nib_write_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam)
454{
455   floperr_t err;
456   UINT8 *track_data;
457   void *track_data_v;
458
459   if ((sector < 0) || (sector >= APPLE2_SECTOR_COUNT))
460      return FLOPPY_ERROR_SEEKERROR;
461   if (buflen != APPLE2_SECTOR_SIZE)
462      return FLOPPY_ERROR_INTERNAL;
463
464   err = floppy_load_track(floppy, head, track, TRUE, &track_data_v, NULL);
465   if (err)
466      return err;
467   track_data = (UINT8 *) track_data_v;
468
469   disk_encode_nib(track_data + sector * APPLE2_NIBBLE_SIZE, (const UINT8 *)buffer, 254, track, sector);
470   return FLOPPY_ERROR_SUCCESS;
471}
472
473
474
475static floperr_t apple2_nib_get_sector_length(floppy_image *floppy, int head, int track, int sector, UINT32 *sector_length)
476{
477   *sector_length = APPLE2_SECTOR_SIZE;
478   return FLOPPY_ERROR_SUCCESS;
479}
480
481
482
483static UINT32 apple2_get_track_size(floppy_image *floppy, int head, int track)
484{
485   return APPLE2_NIBBLE_SIZE * APPLE2_SECTOR_COUNT;
486}
487
488
489
490/* ----------------------------------------------------------------------- */
491
492FLOPPY_OPTIONS_START( apple2 )
493   FLOPPY_OPTION( apple2_do, "do,dsk,bin",   "Apple ][ DOS order disk image",   apple2_dsk_identify,   apple2_do_construct, NULL,
494      HEADS([1])
495      TRACKS([35])
496      SECTORS([16])
497      SECTOR_LENGTH([256])
498      FIRST_SECTOR_ID([0]))
499   FLOPPY_OPTION( apple2_po, "po,dsk,bin",   "Apple ][ ProDOS order disk image",   apple2_dsk_identify,   apple2_po_construct, NULL,
500      HEADS([1])
501      TRACKS([35])
502      SECTORS([16])
503      SECTOR_LENGTH([256])
504      FIRST_SECTOR_ID([0]))
505   FLOPPY_OPTION( apple2_nib, "dsk,nib",   "Apple ][ Nibble order disk image",   apple2_nib_identify,   apple2_nib_construct, NULL,
506      HEADS([1])
507      TRACKS([35])
508      SECTORS([16])
509      SECTOR_LENGTH([256])
510      FIRST_SECTOR_ID([0]))
511FLOPPY_OPTIONS_END
src/mess/formats/gtp_cas.h
r11457r11458
1/*
2
3       gtp_cas.h
4
5       Format code for gtp cassette files
6*/
7
8#ifndef GTP_CAS_H
9#define GTP_CAS_H
10
11#include "imagedev/cassimg.h"
12
13CASSETTE_FORMATLIST_EXTERN(gtp_cassette_formats);
14
15#endif /* GTP_CAS_H */
src/mess/formats/ap2_dsk.h
r11457r11458
1/*********************************************************************
2
3    ap2_dsk.h
4
5    Apple II disk images
6
7*********************************************************************/
8
9#ifndef AP2_DISK_H
10#define AP2_DISK_H
11
12#include "imagedev/flopimg.h"
13
14
15/***************************************************************************
16
17    Constants
18
19***************************************************************************/
20
21#define APPLE2_NIBBLE_SIZE         416
22#define APPLE2_SMALL_NIBBLE_SIZE   374
23#define APPLE2_TRACK_COUNT         35
24#define APPLE2_SECTOR_COUNT         16
25#define APPLE2_SECTOR_SIZE         256
26
27
28
29/**************************************************************************/
30
31FLOPPY_OPTIONS_EXTERN(apple2);
32
33#endif /* AP2_DISK_H */
src/mess/formats/td0_dsk.c
r11457r11458
1/*********************************************************************
2
3    formats/td0_dsk.c
4
5    TD0 disk images
6
7*********************************************************************/
8/*
9 * Based on Japanese version 29-NOV-1988
10 * LZSS coded by Haruhiko OKUMURA
11 * Adaptive Huffman Coding coded by Haruyasu YOSHIZAKI
12 * Edited and translated to English by Kenji RIKITAKE
13 */
14
15#include <string.h>
16#include "emu.h"
17#include "imagedev/flopimg.h"
18#include "imagedev/flopdrv.h"
19
20#define BUFSZ           512     // new input buffer
21
22/* LZSS Parameters */
23
24#define N        4096    /* Size of string buffer */
25#define F        60    /* Size of look-ahead buffer */
26#define THRESHOLD    2
27#define NIL        N    /* End of tree's node  */
28
29
30/* Huffman coding parameters */
31
32#define N_CHAR      (256 - THRESHOLD + F)
33                /* character code (= 0..N_CHAR-1) */
34#define T         (N_CHAR * 2 - 1)    /* Size of table */
35#define R         (T - 1)            /* root position */
36#define MAX_FREQ    0x8000
37                    /* update when cumulative frequency */
38                    /* reaches to this value */
39
40struct td0dsk_tag
41{
42   int heads;
43   int tracks;
44   int sector_size;
45   UINT64 track_offsets[84*2]; /* offset within data for each track */
46   UINT8 *data;
47};
48
49struct tdlzhuf {
50   UINT16 r,
51               bufcnt,bufndx,bufpos,  // string buffer
52          // the following to allow block reads from input in next_word()
53               ibufcnt,ibufndx; // input buffer counters
54   UINT8  inbuf[BUFSZ];    // input buffer
55};
56
57
58struct td0dsk_t
59{
60   floppy_image *floppy_file;
61   UINT64 floppy_file_offset;
62
63   struct tdlzhuf tdctl;
64   UINT8 text_buf[N + F - 1];
65   UINT16 freq[T + 1];    /* cumulative freq table */
66
67/*
68 * pointing parent nodes.
69 * area [T..(T + N_CHAR - 1)] are pointers for leaves
70 */
71   INT16 prnt[T + N_CHAR];
72
73   /* pointing children nodes (son[], son[] + 1)*/
74   INT16 son[T];
75
76   UINT16 getbuf;
77   UINT8 getlen;
78
79   int data_read(UINT8 *buf, UINT16 size);
80   int next_word();
81   int GetBit();
82   int GetByte();
83   void StartHuff();
84   void reconst();
85   void update(int c);
86   INT16 DecodeChar();
87   INT16 DecodePosition();
88   void init_Decode();
89   int Decode(UINT8 *buf, int len);
90};
91
92static td0dsk_t td0dsk;
93
94
95static struct td0dsk_tag *get_tag(floppy_image *floppy)
96{
97   struct td0dsk_tag *tag;
98   tag = (td0dsk_tag *)floppy_tag(floppy);
99   return tag;
100}
101
102
103
104FLOPPY_IDENTIFY( td0_dsk_identify )
105{
106   UINT8 header[2];
107
108   floppy_image_read(floppy, header, 0, 2);
109   if (header[0]=='T' && header[1]=='D') {
110      *vote = 100;
111   } else if (header[0]=='t' && header[1]=='d') {
112      *vote = 100;
113   } else {
114      *vote = 0;
115   }
116   return FLOPPY_ERROR_SUCCESS;
117}
118
119static int td0_get_heads_per_disk(floppy_image *floppy)
120{
121   return get_tag(floppy)->heads;
122}
123
124static int td0_get_tracks_per_disk(floppy_image *floppy)
125{
126   return get_tag(floppy)->tracks;
127}
128
129static UINT64 td0_get_track_offset(floppy_image *floppy, int head, int track)
130{
131   return get_tag(floppy)->track_offsets[(track<<1) + head];
132}
133
134static floperr_t get_offset(floppy_image *floppy, int head, int track, int sector, int sector_is_index, UINT64 *offset)
135{
136   UINT64 offs;
137   UINT8 *header;
138   UINT8 sectors_per_track;
139   int i;
140
141   if ((head < 0) || (head >= get_tag(floppy)->heads) || (track < 0) || (track >= get_tag(floppy)->tracks)
142         || (sector < 0) )
143      return FLOPPY_ERROR_SEEKERROR;
144
145   // position on begining of track data
146   offs = td0_get_track_offset(floppy, head, track);
147
148   // read track header
149   header = get_tag(floppy)->data + offs - 4;
150
151   // take number of sectors per track
152   sectors_per_track = header[0];
153
154   if (!sector_is_index) {
155      // when taking ID's return seek error if number is over counter
156      if (sector > sectors_per_track) {
157         return FLOPPY_ERROR_SEEKERROR;
158      }
159   }
160
161   // move trought sectors
162   for(i=0;i < sector-1;i++) {
163      header = get_tag(floppy)->data + offs;
164      offs+= 6;
165      if ((header[4] & 0x30)==0) {
166         offs+= 2;
167         offs+= header[6] + (header[7]<<8);
168      }
169   }
170   // read size of sector
171   header = get_tag(floppy)->data + offs;
172   get_tag(floppy)->sector_size = 1 << (header[3] + 7);
173
174   if (offset)
175      *offset = offs;
176   return FLOPPY_ERROR_SUCCESS;
177}
178
179
180
181static floperr_t internal_td0_read_sector(floppy_image *floppy, int head, int track, int sector, int sector_is_index, void *buffer, size_t buflen)
182{
183   UINT64 offset;
184   floperr_t err;
185   UINT8 *header;
186   int size,realsize,i;
187   int buff_pos;
188   int data_pos;
189   UINT8 *data;
190   UINT8 *buf;
191
192   buf = (UINT8*)buffer;
193   // take sector offset
194   err = get_offset(floppy, head, track, sector, sector_is_index, &offset);
195   if (err)
196      return err;
197
198   // read sector header
199   header = get_tag(floppy)->data + offset;
200   offset+=6;
201   // if there is no date just jump out
202   if ((header[4] & 0x30)!=0) return FLOPPY_ERROR_SUCCESS;
203
204   offset+=3;
205   // take data size
206   size = header[6] + (header[7]<<8)-1;
207   // take real sector size
208   realsize =  1 << (header[3] + 7);
209
210   // read sector data
211   data = get_tag(floppy)->data + offset;
212   buff_pos = 0;
213   data_pos = 0;
214
215   switch(header[8]) {
216      case 0:
217            // encoding type 0
218            //  - plain data
219            memcpy(buffer,data,size);
220            break;
221      case 1:
222            // encoding type 1
223            //  - 2 bytes size
224            //  - 2 bytes of data
225            //  data is reapeted specified number of times
226            while(buff_pos<realsize) {
227               for (i=0;i<data[data_pos]+(data[data_pos+1] << 8);i++) {
228                  buf[buff_pos] = data[data_pos+2];buff_pos++;
229                  buf[buff_pos] = data[data_pos+3];buff_pos++;
230               }
231               data_pos+=4;
232            }
233            break;
234      case 2:
235            // encoding type 2
236            //  - if first byte is zero next byte represent size of
237            //      plain data after it
238            //  - if different then zero when multiply by 2 represent
239            //      size of data that should be reapeted next byte times
240            while(buff_pos<realsize) {
241               if (data[data_pos]==0x00) {
242                  int size_ = data[data_pos+1];
243                  memcpy(buf+buff_pos,data + data_pos + 2,size_);
244                  data_pos += 2 + size_;
245                  buff_pos += size_;
246               } else {
247                  int size_  = 2*data[data_pos];
248                  int repeat = data[data_pos+1];
249                  data_pos+=2;
250
251                  for (i=0;i<repeat;i++) {
252                     memcpy(buf + buff_pos,data + data_pos,size_);
253                     buff_pos += size_;
254                  }
255                  data_pos += size_;
256               }
257            }
258            break;
259      default:
260            return FLOPPY_ERROR_INTERNAL;
261   }
262   return FLOPPY_ERROR_SUCCESS;
263}
264
265
266static floperr_t td0_read_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen)
267{
268   return internal_td0_read_sector(floppy, head, track, sector, FALSE, buffer, buflen);
269}
270
271static floperr_t td0_read_indexed_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen)
272{
273   return internal_td0_read_sector(floppy, head, track, sector, TRUE, buffer, buflen);
274}
275
276static floperr_t td0_get_sector_length(floppy_image *floppy, int head, int track, int sector, UINT32 *sector_length)
277{
278   floperr_t err;
279   err = get_offset(floppy, head, track, sector, FALSE, NULL);
280   if (err)
281      return err;
282
283   if (sector_length) {
284      *sector_length = get_tag(floppy)->sector_size;
285   }
286   return FLOPPY_ERROR_SUCCESS;
287}
288
289static floperr_t td0_get_indexed_sector_info(floppy_image *floppy, int head, int track, int sector_index, int *cylinder, int *side, int *sector, UINT32 *sector_length, unsigned long *flags)
290{
291   floperr_t retVal;
292   UINT64 offset;
293   UINT8 *sector_info;
294
295   retVal = get_offset(floppy, head, track, sector_index, FALSE, &offset);
296   sector_info = get_tag(floppy)->data + offset;
297   if (cylinder)
298      *cylinder = sector_info[0];
299   if (side)
300      *side = sector_info[1];
301   if (sector)
302      *sector = sector_info[2];
303   if (sector_length) {
304      *sector_length = 1 << (sector_info[3] + 7);
305   }
306   if (flags) {
307      *flags = 0;
308      if (sector_info[4] & 0x02) *flags |= ID_FLAG_CRC_ERROR_IN_DATA_FIELD;
309      if (sector_info[4] & 0x04) *flags |= ID_FLAG_DELETED_DATA;
310   }
311
312   return retVal;
313}
314
315int td0dsk_t::data_read(UINT8 *buf, UINT16 size)
316{
317   if (floppy_file_offset + size > floppy_image_size(floppy_file) ) {
318      size = floppy_image_size(floppy_file) - floppy_file_offset;
319   }
320   floppy_image_read(floppy_file,buf,floppy_file_offset,size);
321   floppy_file_offset += size;
322   return size;
323}
324
325
326/*
327 * Tables for encoding/decoding upper 6 bits of
328 * sliding dictionary pointer
329 */
330
331/* decoder table */
332static const UINT8 d_code[256] = {
333    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
334    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
335    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
336    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
337    0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
338    0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
339    0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
340    0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
341    0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
342    0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
343    0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
344    0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
345    0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
346    0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
347    0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
348    0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09,
349    0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A,
350    0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B,
351    0x0C, 0x0C, 0x0C, 0x0C, 0x0D, 0x0D, 0x0D, 0x0D,
352    0x0E, 0x0E, 0x0E, 0x0E, 0x0F, 0x0F, 0x0F, 0x0F,
353    0x10, 0x10, 0x10, 0x10, 0x11, 0x11, 0x11, 0x11,
354    0x12, 0x12, 0x12, 0x12, 0x13, 0x13, 0x13, 0x13,
355    0x14, 0x14, 0x14, 0x14, 0x15, 0x15, 0x15, 0x15,
356    0x16, 0x16, 0x16, 0x16, 0x17, 0x17, 0x17, 0x17,
357    0x18, 0x18, 0x19, 0x19, 0x1A, 0x1A, 0x1B, 0x1B,
358    0x1C, 0x1C, 0x1D, 0x1D, 0x1E, 0x1E, 0x1F, 0x1F,
359    0x20, 0x20, 0x21, 0x21, 0x22, 0x22, 0x23, 0x23,
360    0x24, 0x24, 0x25, 0x25, 0x26, 0x26, 0x27, 0x27,
361    0x28, 0x28, 0x29, 0x29, 0x2A, 0x2A, 0x2B, 0x2B,
362    0x2C, 0x2C, 0x2D, 0x2D, 0x2E, 0x2E, 0x2F, 0x2F,
363    0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
364    0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F,
365};
366
367static const UINT8 d_len[256] = {
368    0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
369    0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
370    0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
371    0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
372    0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
373    0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
374    0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
375    0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
376    0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
377    0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
378    0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
379    0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
380    0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
381    0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
382    0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
383    0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
384    0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
385    0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
386    0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
387    0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
388    0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
389    0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
390    0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
391    0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
392    0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
393    0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
394    0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
395    0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
396    0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
397    0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
398    0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
399    0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
400};
401
402int td0dsk_t::next_word()
403{
404    if(tdctl.ibufndx >= tdctl.ibufcnt)
405    {
406        tdctl.ibufndx = 0;
407        tdctl.ibufcnt = data_read(tdctl.inbuf,BUFSZ);
408        if(tdctl.ibufcnt <= 0)
409            return(-1);
410    }
411    while (getlen <= 8) { // typically reads a word at a time
412        getbuf |= tdctl.inbuf[tdctl.ibufndx++] << (8 - getlen);
413        getlen += 8;
414    }
415    return(0);
416}
417
418
419int td0dsk_t::GetBit()    /* get one bit */
420{
421    INT16 i;
422    if(next_word() < 0)
423        return(-1);
424    i = getbuf;
425    getbuf <<= 1;
426    getlen--;
427        if(i < 0)
428        return(1);
429    else
430        return(0);
431}
432
433int td0dsk_t::GetByte()    /* get a byte */
434{
435    UINT16 i;
436    if(next_word() != 0)
437        return(-1);
438    i = getbuf;
439    getbuf <<= 8;
440    getlen -= 8;
441    i = i >> 8;
442    return((int) i);
443}
444
445
446
447/* initialize freq tree */
448
449void td0dsk_t::StartHuff()
450{
451    int i, j;
452
453    for (i = 0; i < N_CHAR; i++) {
454        freq[i] = 1;
455        son[i] = i + T;
456        prnt[i + T] = i;
457    }
458    i = 0; j = N_CHAR;
459    while (j <= R) {
460        freq[j] = freq[i] + freq[i + 1];
461        son[j] = i;
462        prnt[i] = prnt[i + 1] = j;
463        i += 2; j++;
464    }
465    freq[T] = 0xffff;
466    prnt[R] = 0;
467}
468
469
470/* reconstruct freq tree */
471
472void td0dsk_t::reconst()
473{
474    INT16 i, j, k;
475    UINT16 f, l;
476
477    /* halven cumulative freq for leaf nodes */
478    j = 0;
479    for (i = 0; i < T; i++) {
480        if (son[i] >= T) {
481            freq[j] = (freq[i] + 1) / 2;
482            son[j] = son[i];
483            j++;
484        }
485    }
486    /* make a tree : first, connect children nodes */
487    for (i = 0, j = N_CHAR; j < T; i += 2, j++) {
488        k = i + 1;
489        f = freq[j] = freq[i] + freq[k];
490        for (k = j - 1; f < freq[k]; k--);
491        k++;
492        l = (j - k) * 2;
493
494        /* movmem() is Turbo-C dependent
495           rewritten to memmove() by Kenji */
496
497        /* movmem(&freq[k], &freq[k + 1], l); */
498        (void)memmove(&freq[k + 1], &freq[k], l);
499        freq[k] = f;
500        /* movmem(&son[k], &son[k + 1], l); */
501        (void)memmove(&son[k + 1], &son[k], l);
502        son[k] = i;
503    }
504    /* connect parent nodes */
505    for (i = 0; i < T; i++) {
506        if ((k = son[i]) >= T) {
507            prnt[k] = i;
508        } else {
509            prnt[k] = prnt[k + 1] = i;
510        }
511    }
512}
513
514
515/* update freq tree */
516
517void td0dsk_t::update(int c)
518{
519    int i, j, k, l;
520
521    if (freq[R] == MAX_FREQ) {
522        reconst();
523    }
524    c = prnt[c + T];
525    do {
526        k = ++freq[c];
527
528        /* swap nodes to keep the tree freq-ordered */
529        if (k > freq[l = c + 1]) {
530            while (k > freq[++l]);
531            l--;
532            freq[c] = freq[l];
533            freq[l] = k;
534
535            i = son[c];
536            prnt[i] = l;
537            if (i < T) prnt[i + 1] = l;
538
539            j = son[l];
540            son[l] = i;
541
542            prnt[j] = c;
543            if (j < T) prnt[j + 1] = c;
544            son[c] = j;
545
546            c = l;
547        }
548    } while ((c = prnt[c]) != 0);    /* do it until reaching the root */
549}
550
551
552INT16 td0dsk_t::DecodeChar()
553{
554    int ret;
555    UINT16 c;
556
557    c = son[R];
558
559    /*
560     * start searching tree from the root to leaves.
561     * choose node #(son[]) if input bit == 0
562     * else choose #(son[]+1) (input bit == 1)
563     */
564    while (c < T) {
565        if((ret = GetBit()) < 0)
566            return(-1);
567        c += (unsigned) ret;
568        c = son[c];
569    }
570    c -= T;
571    update(c);
572    return c;
573}
574
575INT16 td0dsk_t::DecodePosition()
576{
577    INT16 bit;
578    UINT16 i, j, c;
579
580    /* decode upper 6 bits from given table */
581    if((bit=GetByte()) < 0)
582        return(-1);
583    i = (UINT16) bit;
584    c = (UINT16)d_code[i] << 6;
585    j = d_len[i];
586
587    /* input lower 6 bits directly */
588    j -= 2;
589    while (j--) {
590        if((bit = GetBit()) < 0)
591            return(-1);
592        i = (i << 1) + bit;
593    }
594    return(c | (i & 0x3f));
595}
596
597/* DeCompression
598
599split out initialization code to init_Decode()
600
601*/
602
603void td0dsk_t::init_Decode()
604{
605    int i;
606    getbuf = 0;
607    getlen = 0;
608    tdctl.ibufcnt= tdctl.ibufndx = 0; // input buffer is empty
609    tdctl.bufcnt = 0;
610    StartHuff();
611    for (i = 0; i < N - F; i++)
612        text_buf[i] = ' ';
613    tdctl.r = N - F;
614}
615
616
617int td0dsk_t::Decode(UINT8 *buf, int len)  /* Decoding/Uncompressing */
618{
619    INT16 c,pos;
620    int  count;  // was an unsigned long, seems unnecessary
621    for (count = 0; count < len; ) {
622            if(tdctl.bufcnt == 0) {
623                if((c = DecodeChar()) < 0)
624                    return(count); // fatal error
625                if (c < 256) {
626                    *(buf++) = c;
627                    text_buf[tdctl.r++] = c;
628                    tdctl.r &= (N - 1);
629                    count++;
630                }
631                else {
632                    if((pos = DecodePosition()) < 0)
633                           return(count); // fatal error
634                    tdctl.bufpos = (tdctl.r - pos - 1) & (N - 1);
635                    tdctl.bufcnt = c - 255 + THRESHOLD;
636                    tdctl.bufndx = 0;
637                }
638            }
639            else { // still chars from last string
640                while( tdctl.bufndx < tdctl.bufcnt && count < len ) {
641                    c = text_buf[(tdctl.bufpos + tdctl.bufndx) & (N - 1)];
642                    *(buf++) = c;
643                    tdctl.bufndx++;
644                    text_buf[tdctl.r++] = c;
645                    tdctl.r &= (N - 1);
646                    count++;
647                }
648                // reset bufcnt after copy string from text_buf[]
649                if(tdctl.bufndx >= tdctl.bufcnt)
650                    tdctl.bufndx = tdctl.bufcnt = 0;
651            }
652    }
653    return(count); // count == len, success
654}
655
656
657FLOPPY_CONSTRUCT( td0_dsk_construct )
658{
659   td0dsk_t *state = &td0dsk;
660   struct FloppyCallbacks *callbacks;
661   struct td0dsk_tag *tag;
662   UINT8 *header;
663   int number_of_sectors;
664   int position;
665   int i;
666   int track;
667
668   if(params)
669   {
670      // create
671      return FLOPPY_ERROR_UNSUPPORTED;
672   }
673
674   tag = (struct td0dsk_tag *) floppy_create_tag(floppy, sizeof(struct td0dsk_tag));
675   if (!tag)
676      return FLOPPY_ERROR_OUTOFMEMORY;
677
678   tag->data = global_alloc_array_clear(UINT8,floppy_image_size(floppy));
679   if (tag->data==NULL) {
680      return FLOPPY_ERROR_OUTOFMEMORY;
681   }
682   floppy_image_read(floppy, tag->data, 0, floppy_image_size(floppy));
683   header = tag->data;
684
685   if (header[0]=='t') {
686      UINT8 obuf[BUFSZ];
687      int rd;
688      int off = 12;
689      int size = 0;
690      state->floppy_file = floppy;
691      state->init_Decode();
692      state->floppy_file_offset = 12;
693      do
694      {
695         if((rd = state->Decode(obuf, BUFSZ)) > 0) size += rd;
696      } while(rd == BUFSZ);
697      memcpy(obuf,tag->data,12);
698      free(tag->data);
699      tag->data = (UINT8*)malloc(size+12);
700      if (tag->data==NULL) {
701         return FLOPPY_ERROR_OUTOFMEMORY;
702      }
703      memcpy(tag->data,obuf,12);
704      state->floppy_file_offset = 12;
705      state->init_Decode();
706      do
707      {
708         if((rd = state->Decode(obuf, BUFSZ)) > 0) {
709            memcpy(tag->data+off,obuf,rd);
710            off += rd;
711         }
712      }  while(rd == BUFSZ);
713   }
714   header = tag->data;
715   tag->heads   = header[9];
716   if (tag->heads > 1) {
717      tag->heads  = 2;
718   }
719
720   //  header len + comment header + comment len
721   position = 12;
722   if (header[7] & 0x80) {
723      position += 10 + header[14] + (header[15]<<8);
724   }
725   tag->tracks = 0;
726   do {
727      // read track header
728      header = tag->data + position;
729      track = header[1];
730      number_of_sectors = header[0];
731      if (number_of_sectors!=0xff){
732         position+=4;
733         tag->track_offsets[(track<<1) + (header[2] & 1)] = position;
734         for(i=0;i<number_of_sectors;i++) {
735            // read sector header
736            header = tag->data + position;
737            position+=6;
738            // read sector size
739            if ((header[4] & 0x30)==0) {
740               // if there is sector data
741               header = tag->data + position;
742               position+=2;
743               // skip sector data
744               position+= header[0] + (header[1]<<8);
745            }
746         }
747         tag->tracks++;
748      }
749   } while(number_of_sectors!=0xff);
750   tag->tracks++;
751
752   callbacks = floppy_callbacks(floppy);
753   callbacks->read_sector = td0_read_sector;
754   callbacks->read_indexed_sector = td0_read_indexed_sector;
755   callbacks->get_sector_length = td0_get_sector_length;
756   callbacks->get_heads_per_disk = td0_get_heads_per_disk;
757   callbacks->get_tracks_per_disk = td0_get_tracks_per_disk;
758   callbacks->get_indexed_sector_info = td0_get_indexed_sector_info;
759   return FLOPPY_ERROR_SUCCESS;
760}
761
762FLOPPY_DESTRUCT( td0_dsk_destruct )
763{
764   struct td0dsk_tag *tag = get_tag(floppy);
765   global_free(tag->data);
766   tag->data = NULL;
767   return FLOPPY_ERROR_SUCCESS;
768}
No newline at end of file
src/mess/formats/d81_dsk.c
r11457r11458
1/*********************************************************************
2
3    formats/d81_dsk.c
4
5    Floppy format code for Commodore 1581 disk images
6
7*********************************************************************/
8
9#include "emu.h"
10#include "formats/basicdsk.h"
11#include "formats/d81_dsk.h"
12
13/***************************************************************************
14    PARAMETERS
15***************************************************************************/
16
17#define D81_SIZE   819200
18
19/***************************************************************************
20    IMPLEMENTATION
21***************************************************************************/
22
23/*-------------------------------------------------
24    d81_translate_offset - translates the
25    physical offset to a logical offset within
26    the disk image
27-------------------------------------------------*/
28
29/*
30
31     file offset       | CBM logical  |  drive physical   |   specials
32     decimal sedecimal | track/sector | cyl head sec offs |
33     ------------------+--------------+-------------------+--------------
34           0 0x000000  |    01;00     |   00;01;01        | first block
35         256 0x000100  |    01;01     |   00;01;01   +256 |
36           .     .     |      .       |     .  .          |
37        4864 0x001300  |    01;19     |   00;01;10   +256 |
38        5120 0x001400  |    01;20     |   00;00;01        |
39           .     .     |      .       |     .  .          |
40        9984 0x002700  |    01;39     |   00;00;10   +256 |
41       10240 0x002800  |    02;00     |   01;01;01        |
42           .     .     |      .       |     .  .          |
43       15360 0x003C00  |    02;20     |   01;00;01        |
44           .     .     |      .       |     .  .          |
45       20480 0x005000  |    03;00     |   02;01;01        |
46           .     .     |      .       |     .  .          |
47           .     .     |      .       |     .  .          |
48       30729 0x007800  |    04;00     |   03;01;01        |
49           .     .     |      .       |     .  .          |
50           .     .     |      .       |     .  .          |
51           .     .     |      .       |     .  .          |
52      399360 0x061800  |    40;00     |   39;01;01        | disk header
53      399616 0x061900  |    40;01     |   39;01;01   +256 | 1st BAM block
54      399872 0x061A00  |    40;02     |   39;01;02        | 2nd BAM block
55      400128 0x061B00  |    40;03     |   39;01;02   +256 | 1st dir block
56           .     .     |      .       |     .  .          |
57      409600 0x064000  |    41;00     |   40;01;01        |
58           .     .     |      .       |     .  .          |
59           .     .     |      .       |     .  .          |
60           .     .     |      .       |     .  .          |
61      808960 0x0C5800  |    80;00     |   79;01;01        |
62           .     .     |      .       |     .  .          |
63      813824 0x0C6B00  |    80;19     |   79;01;10   +256 |
64      814080 0x0C6C00  |    80;20     |   79;00;01        |
65           .     .     |      .       |     .  .          |
66      818688 0x0C7E00  |    80;38     |   79;00;10        |
67      818944 0x0C7F00  |    80;39     |   79;00;10   +256 | last block
68
69*/
70
71static UINT64 d81_translate_offset(floppy_image *floppy, const struct basicdsk_geometry *geom, int track, int head, int sector)
72{
73   UINT64 offset = (track * 20) + (!head * 10) + sector;
74
75   return offset;
76}
77
78/*-------------------------------------------------
79    FLOPPY_IDENTIFY( d81_dsk_identify )
80-------------------------------------------------*/
81
82FLOPPY_IDENTIFY( d81_dsk_identify )
83{
84   *vote = (floppy_image_size(floppy) == D81_SIZE) ? 100 : 0;
85
86   return FLOPPY_ERROR_SUCCESS;
87}
88
89/*-------------------------------------------------
90    FLOPPY_CONSTRUCT( d81_dsk_construct )
91-------------------------------------------------*/
92
93/*
94PER TRACK ORGANIZATION:
95
96      Hex 4E written as a gap, with 10 sectors of data, with full gaps written for motor speed variation.
97
98PER SECTOR ORGANIZATION:
99
100      MFM Encoding
101      12 Bytes of Hex 00
102      3 Bytes of Hex A1 (Data Hex A1, Clock Hex 0A)
103      1 Byte of Hex FE (ID Address Mark)
104      1 Byte (Track Number)
105      1 Byte (Side Number)
106      1 Byte (Sector Number)
107      1 Byte (Sector Length, 02 for 512 Byte Sectors)
108      2 Bytes CRC (Cyclic Redundancy Check)
109      22 Bytes of Hex 22
110      12 Bytes of Hex 00
111      3 Bytes of Hex A1 (Data Hex A1, Clock Hex 0A)
112      1 Byte of Hex FB (Data Address Mark)
113      512 Bytes of Data
114      2 Bytes of CRC (Cyclic Redundancy Check)
115      38 Bytes of Hex 4E
116*/
117
118FLOPPY_CONSTRUCT( d81_dsk_construct )
119{
120   struct basicdsk_geometry geometry;
121
122   memset(&geometry, 0, sizeof(geometry));
123
124   geometry.heads = 2;
125   geometry.first_sector_id = 1;
126   geometry.sector_length = 512;
127   geometry.tracks = 80;
128   geometry.sectors = 10;
129   geometry.translate_offset = d81_translate_offset;
130
131   return basicdsk_construct(floppy, &geometry);
132}
src/mess/formats/sord_cas.c
r11457r11458
1/**************************************************************************
2
3    sord_cas.c
4
5    Format code for Sord M5 cassette files
6
7**************************************************************************/
8
9#include <string.h>
10#include "sord_cas.h"
11#include "timer.h"
12
13#define SORDM5_WAVESAMPLES_HEADER  1
14#define SORDM5_WAVESAMPLES_TRAILER 1
15#define SORDM5_WAVESAMPLES_BLOCK   1
16
17static const UINT8 SORDM5_CAS_HEADER[6] = { 'S', 'O', 'R', 'D', 'M', '5'};
18
19static const struct CassetteModulation sordm5_cas_modulation =
20{
21   CASSETTE_MODULATION_SINEWAVE,
22   1575.0 - 300, 1575.0, 1575.0 + 300,
23   3150.0 - 600, 3150.0, 3150.0 + 600
24};
25
26static UINT8 cassette_image_read_uint8( cassette_image *cassette, UINT64 offset)
27{
28   UINT8 data;
29   cassette_image_read( cassette, &data, offset, 1);
30   return data;
31}
32
33static casserr_t sordm5_tap_identify( cassette_image *cassette, struct CassetteOptions *opts)
34{
35   return cassette_modulation_identify( cassette, &sordm5_cas_modulation, opts);
36}
37
38static casserr_t sordm5_tap_load( cassette_image *cassette)
39{
40   casserr_t err;
41   UINT64 image_size;
42   double time_index = 0.0;
43   double time_displacement;
44   UINT8 header[16];
45   UINT64 image_pos;
46   UINT8 block_type, byte, bit;
47   UINT32 block_size, i, j;
48   size_t filler_length;
49   // init
50   image_size = cassette_image_size(cassette);
51   image_pos = 0;
52   // read/check header
53   if (image_size < 16) return CASSETTE_ERROR_INVALIDIMAGE;
54   cassette_image_read( cassette, &header, image_pos, 16);
55   image_pos += 16;
56   if (memcmp( header, SORDM5_CAS_HEADER, sizeof(SORDM5_CAS_HEADER)) != 0) return CASSETTE_ERROR_INVALIDIMAGE;
57   // add silence (header)
58   err = cassette_put_sample( cassette, 0, time_index, SORDM5_WAVESAMPLES_HEADER, 0);
59   if (err) return err;
60   time_index += SORDM5_WAVESAMPLES_HEADER;
61   // process blocks
62   while (image_pos < image_size)
63   {
64      // read block type
65      block_type = cassette_image_read_uint8( cassette, image_pos + 0);
66      if ((block_type != 'H') && (block_type != 'D')) return CASSETTE_ERROR_INVALIDIMAGE;
67      // read block size
68      block_size = cassette_image_read_uint8( cassette, image_pos + 1);
69      if (block_size == 0) block_size = 0x100;
70      block_size += 3;
71      // add silence (header block)
72      if (block_type == 'H')
73      {
74         err = cassette_put_sample( cassette, 0, time_index, SORDM5_WAVESAMPLES_BLOCK, 0);
75         if (err) return err;
76         time_index += SORDM5_WAVESAMPLES_BLOCK;
77      }
78      // add sync
79      if (block_type == 'H') filler_length = 2.4 * (3150 / 8); else filler_length = 0.15 * (3150 / 8);
80      err = cassette_put_modulated_filler(cassette, 0, time_index, 0xFF, filler_length, &sordm5_cas_modulation, &time_displacement);
81      if (err) return err;
82      time_index += time_displacement;
83      // process block
84      for (i=0;i<block_size;i++)
85      {
86         // read byte
87         byte = cassette_image_read_uint8( cassette, image_pos + i);
88         // calc/check checksum
89         #if 0
90         if (i == block_size)
91         {
92            if (byte != (crc & 0xFF)) return CASSETTE_ERROR_INVALIDIMAGE;
93         }
94         if (i > 2) crc += byte;
95         #endif
96         // process byte
97         for (j=0;j<10;j++)
98         {
99            // get bit
100            if (j < 2)
101            {
102               bit = ((j==0)?1:0);
103            }
104            else
105            {
106               bit = (byte >> (j-2)) & 1;
107            }
108            // add bit
109            err = cassette_put_modulated_data_bit( cassette, 0, time_index, bit, &sordm5_cas_modulation, &time_displacement);
110            if (err) return err;
111            time_index += time_displacement;
112         }
113      }
114      // mark end of block
115      err = cassette_put_modulated_data_bit( cassette, 0, time_index, 1, &sordm5_cas_modulation, &time_displacement);
116      if (err) return err;
117      time_index += time_displacement;
118      // next block
119      image_pos += block_size;
120   }
121   // add silence (trailer)
122   err = cassette_put_sample( cassette, 0, time_index, SORDM5_WAVESAMPLES_TRAILER, 0);
123   if (err) return err;
124   time_index += SORDM5_WAVESAMPLES_TRAILER;
125   //
126   return CASSETTE_ERROR_SUCCESS;
127}
128
129static const struct CassetteFormat sordm5_cas_format =
130{
131   "cas",
132   sordm5_tap_identify,
133   sordm5_tap_load,
134   NULL
135};
136
137CASSETTE_FORMATLIST_START(sordm5_cassette_formats)
138   CASSETTE_FORMAT(sordm5_cas_format)
139CASSETTE_FORMATLIST_END
src/mess/formats/vg5k_cas.c
r11457r11458
1/********************************************************************
2
3    Support for VG-5000 .k7 cassette images
4
5********************************************************************/
6#include "vg5k_cas.h"
7
8
9#define SMPLO   -32768
10#define SILENCE   0
11#define SMPHI   32767
12
13
14static int k7_size;
15
16/*******************************************************************
17   Generate one high-low cycle of sample data
18********************************************************************/
19INLINE int vg5k_cas_cycle(INT16 *buffer, int sample_pos, int len)
20{
21   int i = 0;
22
23   if (buffer)
24   {
25      while( i < len)
26      {
27         buffer[ sample_pos + i ] = SMPHI;
28         i++;
29      }
30
31      while( i < len * 2 )
32      {
33         buffer[ sample_pos + i ] = SMPLO;
34         i++;
35      }
36   }
37   return len * 2;
38}
39
40
41/*******************************************************************
42   Generate n samples of silence
43********************************************************************/
44INLINE int vg5k_cas_silence(INT16 *buffer, int sample_pos, int len)
45{
46   int i = 0;
47
48   if ( buffer )
49      for( i = 0; i < len; i++)
50         buffer[ sample_pos + i ] = SILENCE;
51
52   return len;
53}
54
55
56/*******************************************************************
57   Generate the end-byte samples
58********************************************************************/
59INLINE int vg5k_cas_eob(INT16 *buffer, int sample_pos)
60{
61   int i, samples = 0;
62
63   for (i = 0; i < 4; i++)
64      samples += vg5k_cas_cycle( buffer, sample_pos + samples, 5);
65
66   samples += vg5k_cas_cycle( buffer, sample_pos + samples, 10);
67
68   return samples;
69}
70
71
72INLINE int vg5k_cas_byte(INT16 *buffer, int sample_pos, UINT8 data)
73{
74/* Writing an entire byte */
75   int i, samples;
76
77   samples = 0;
78   for ( i = 0; i < 8; i++ )
79   {
80      if ( data & 0x01 )
81      {
82         samples += vg5k_cas_cycle( buffer, sample_pos + samples, 5 );
83         samples += vg5k_cas_cycle( buffer, sample_pos + samples, 5 );
84      }
85      else
86      {
87         samples += vg5k_cas_cycle( buffer, sample_pos + samples, 10 );
88      }
89
90      data >>= 1;
91   }
92   return samples;
93}
94
95
96/*******************************************************************
97   Generate n sample of synchro
98********************************************************************/
99INLINE int vg5k_k7_synchro(INT16 *buffer, int sample_pos, int len)
100{
101
102   int i, samples = 0;
103
104   for ( i = 0; i < len ; i++ )
105         samples += vg5k_cas_cycle( buffer, sample_pos + samples, 5);
106
107   samples += vg5k_cas_eob( buffer, sample_pos + samples);
108
109   return samples;
110}
111
112
113static int vg5k_handle_tap(INT16 *buffer, const UINT8 *casdata)
114{
115   int   data_pos, sample_count;
116
117   data_pos = 0;
118   sample_count = 0;
119
120   /* file has to start with an head block */
121   if (casdata[0] != 0xd3  || casdata[1] != 0xd3 || casdata[2] != 0xd3)
122      return -1;
123
124   /* on the entire file*/
125    while( data_pos < k7_size )
126   {
127      UINT16   block_size = 0;
128
129      /* Identify type of block */
130      if (casdata[data_pos] == 0xd3)
131      {
132         /* head block have fixed size of 32 byte */
133         block_size = 0x20;
134
135         /* 1 sec of silence before the head block */
136         sample_count += vg5k_cas_silence(buffer, sample_count, 44100);
137
138         /* head block starts with 30000 samples of synchro */
139         sample_count += vg5k_k7_synchro( buffer, sample_count, 30000 );
140      }
141      else if (casdata[data_pos] == 0xd6)
142      {
143         /* data block size is defined in head block */
144         block_size = (casdata[data_pos - 4] | casdata[data_pos - 3]<<8) +  20;
145
146         /* 10000 samples of silence before the data block */
147         sample_count += vg5k_cas_silence(buffer, sample_count, 10000);
148
149         /* data block starts with 7200 samples of synchro */
150         sample_count += vg5k_k7_synchro( buffer, sample_count, 7200);
151      }
152      else
153      {
154         /* tries to handle files that do not respect the size declared in the head block */
155         while (data_pos < k7_size && casdata[data_pos] != 0xd3 && casdata[data_pos] != 0xd6)
156            data_pos++;
157      }
158
159      /* Data samples */
160      for ( ; block_size ; data_pos++, block_size-- )
161        {
162          /* Make sure there are enough bytes left */
163          if (data_pos > k7_size)
164            return -1;
165
166          sample_count += vg5k_cas_byte( buffer, sample_count, casdata[data_pos] );
167
168         /* generate the end-byte samples */
169         sample_count += vg5k_cas_eob( buffer, sample_count);
170      }
171    }
172
173   /* Finish with 10000 samples of silence */
174   sample_count += vg5k_cas_silence(buffer, sample_count, 10000);
175
176   return sample_count;
177}
178
179
180/*******************************************************************
181   Generate samples for the tape image
182********************************************************************/
183static int vg5k_k7_fill_wave(INT16 *buffer, int sample_count, UINT8 *bytes)
184{
185   return vg5k_handle_tap(buffer, bytes);
186}
187
188
189/*******************************************************************
190   Calculate the number of samples needed for this tape image classical
191********************************************************************/
192static int vg5k_k7_to_wav_size(const UINT8 *casdata, int caslen)
193{
194   k7_size = caslen ;
195
196   return vg5k_handle_tap( NULL, casdata );
197}
198
199
200static const struct CassetteLegacyWaveFiller vg5k_legacy_fill_wave =
201{
202   vg5k_k7_fill_wave,                  /* fill_wave */
203   -1,                              /* chunk_size */
204   0,                              /* chunk_samples */
205   vg5k_k7_to_wav_size,                /* chunk_sample_calc */
206   44100,                           /* sample_frequency */
207   0,                              /* header_samples */
208   0                              /* trailer_samples */
209};
210
211static casserr_t vg5k_k7_identify(cassette_image *cassette, struct CassetteOptions *opts)
212{
213   return cassette_legacy_identify(cassette, opts, &vg5k_legacy_fill_wave);
214}
215
216
217static casserr_t vg5k_k7_load(cassette_image *cassette)
218{
219   return cassette_legacy_construct(cassette, &vg5k_legacy_fill_wave);
220}
221
222
223static const struct CassetteFormat vg5k_k7_format =
224{
225   "k7",
226   vg5k_k7_identify,
227   vg5k_k7_load,
228   NULL
229};
230
231
232CASSETTE_FORMATLIST_START(vg5k_cassette_formats)
233   CASSETTE_FORMAT(vg5k_k7_format)
234CASSETTE_FORMATLIST_END
src/mess/formats/d81_dsk.h
r11457r11458
1/*********************************************************************
2
3    formats/d81_dsk.h
4
5    Floppy format code for Commodore 1581 disk images
6
7*********************************************************************/
8
9#ifndef __D81_DSK__
10#define __D81_DSK__
11
12#include "imagedev/flopimg.h"
13
14/***************************************************************************
15    PROTOTYPES
16***************************************************************************/
17
18FLOPPY_IDENTIFY( d81_dsk_identify );
19FLOPPY_CONSTRUCT( d81_dsk_construct );
20
21#endif
src/mess/formats/cgen_cas.c
r11457r11458
1/********************************************************************
2
3Support for EACA Colour Genie .cas cassette images
4
5Current state: Not working. Only the sync signal and 0x66 byte get
6               recognized.
7
8********************************************************************/
9
10#include "formats/cgen_cas.h"
11
12#define TAPE_HEADER "Colour Genie - Virtual Tape File"
13
14#define SMPLO   -32768
15#define SMPHI   32767
16
17
18static int cas_size;
19static int level;
20
21
22static int cgenie_output_byte(INT16 *buffer, int sample_count, UINT8 data)
23{
24   int i;
25   int samples = 0;
26
27   for ( i = 0; i < 8; i++ )
28   {
29      /* Output bit boundary */
30      level ^= 1;
31      if ( buffer )
32         buffer[ sample_count + samples ] = level ? SMPHI : SMPLO;
33      samples++;
34
35      /* Output bit */
36      if ( data & 0x80 )
37         level ^= 1;
38      if ( buffer )
39         buffer[ sample_count + samples ] = level ? SMPHI : SMPLO;
40      samples++;
41
42      data <<= 1;
43   }
44   return samples;
45}
46
47
48static int cgenie_handle_cas(INT16 *buffer, const UINT8 *casdata)
49{
50   int   i, data_pos, sample_count;
51
52   data_pos = 0;
53   sample_count = 0;
54   level = 0;
55
56   /* Check for presence of optional header */
57   if ( ! memcmp( casdata, TAPE_HEADER, sizeof(TAPE_HEADER) - 1 ) )
58   {
59      /* Search for 0x00 or end of file */
60      while ( data_pos < cas_size && casdata[data_pos] )
61         data_pos++;
62
63      /* If we're at the end of the file it's not a valid .cas file */
64      if ( data_pos == cas_size )
65         return -1;
66
67      /* Skip the 0x00 byte */
68      data_pos++;
69   }
70
71   /* If we're at the end of the file it's not a valid .cas file */
72   if ( data_pos == cas_size )
73      return -1;
74
75   /* Check for beginning of tape file marker */
76   if ( casdata[data_pos] != 0x66 )
77      return -1;
78
79   /* Create header */
80   for ( i = 0; i < 256; i++ )
81      sample_count += cgenie_output_byte( buffer, sample_count, 0xaa );
82
83   /* Start outputting data */
84   while( data_pos < cas_size )
85   {
86      sample_count += cgenie_output_byte( buffer, sample_count, casdata[data_pos] );
87      data_pos++;
88   }
89   sample_count += cgenie_output_byte( buffer, sample_count, 0x00 );
90
91   return sample_count;
92}
93
94/*******************************************************************
95   Generate samples for the tape image
96********************************************************************/
97static int cgenie_cas_fill_wave(INT16 *buffer, int sample_count, UINT8 *bytes)
98{
99   return cgenie_handle_cas( buffer, bytes );
100}
101
102
103/*******************************************************************
104   Calculate the number of samples needed for this tape image
105********************************************************************/
106static int cgenie_cas_to_wav_size(const UINT8 *casdata, int caslen)
107{
108   cas_size = caslen;
109
110   return cgenie_handle_cas( NULL, casdata );
111}
112
113static const struct CassetteLegacyWaveFiller cgenie_cas_legacy_fill_wave =
114{
115   cgenie_cas_fill_wave,               /* fill_wave */
116   -1,                              /* chunk_size */
117   0,                              /* chunk_samples */
118   cgenie_cas_to_wav_size,               /* chunk_sample_calc */
119   2400,                           /* sample_frequency */
120   0,                              /* header_samples */
121   0                              /* trailer_samples */
122};
123
124
125static casserr_t cgenie_cas_identify(cassette_image *cassette, struct CassetteOptions *opts)
126{
127   return cassette_legacy_identify(cassette, opts, &cgenie_cas_legacy_fill_wave);
128}
129
130
131static casserr_t cgenie_cas_load(cassette_image *cassette)
132{
133   return cassette_legacy_construct(cassette, &cgenie_cas_legacy_fill_wave);
134}
135
136
137static const struct CassetteFormat cgenie_cas_format =
138{
139   "cas",
140   cgenie_cas_identify,
141   cgenie_cas_load,
142   NULL
143};
144
145
146CASSETTE_FORMATLIST_START(cgenie_cassette_formats)
147   CASSETTE_FORMAT(cgenie_cas_format)
148CASSETTE_FORMATLIST_END
149
src/mess/formats/rk_cas.c
r11457r11458
1/*
2
3    Tape support for RK format
4
5*/
6#include "emu.h"
7#include "formats/rk_cas.h"
8
9
10#define RK_WAV_FREQUENCY   44000
11#define WAVE_HIGH      32767
12#define WAVE_LOW      -32768
13
14#define RK_HEADER_LEN   256
15
16#define RK_SIZE_20 20
17#define RK_SIZE_22 22
18#define RK_SIZE_60 60
19
20static int     data_size;
21
22static INT16 *rk_emit_level(INT16 *p, int count, int level)
23{
24   int i;
25
26   for (i=0; i<count; i++)
27   {
28      *(p++) = level;
29   }
30   return p;
31}
32
33static INT16* rk_output_bit(INT16 *p, UINT8 b,int bitsize)
34{
35   if (b)
36   {
37      p = rk_emit_level (p, bitsize, WAVE_HIGH);
38      p = rk_emit_level (p, bitsize, WAVE_LOW);
39   }
40   else
41   {
42      p = rk_emit_level (p, bitsize, WAVE_LOW);
43      p = rk_emit_level (p, bitsize, WAVE_HIGH);
44
45   }
46    return p;
47}
48
49static INT16* rk_output_byte(INT16 *p, UINT8 byte,int bitsize)
50{
51   int i;
52   for (i=7; i>=0; i--)
53      p = rk_output_bit(p,(byte>>i) & 0x01, bitsize);
54
55   return p;
56}
57
58
59static int rk20_cas_to_wav_size( const UINT8 *casdata, int caslen ) {
60   data_size = caslen;
61   return  (RK_HEADER_LEN  * 8 * 2 +  8*2 + caslen * 8 * 2) * RK_SIZE_20;
62}
63
64static int rk22_cas_to_wav_size( const UINT8 *casdata, int caslen ) {
65   data_size = caslen;
66   return  (RK_HEADER_LEN  * 8 * 2 +  8*2 + caslen * 8 * 2) * RK_SIZE_22;
67}
68
69static int rk60_cas_to_wav_size( const UINT8 *casdata, int caslen ) {
70   data_size = caslen;
71   return  (RK_HEADER_LEN  * 8 * 2 +  8*2 + caslen * 8 * 2) * RK_SIZE_60;
72}
73
74static int gam_cas_to_wav_size( const UINT8 *casdata, int caslen ) {
75   data_size = caslen;
76   return  (RK_HEADER_LEN  * 8 * 2 +  caslen * 8 * 2) * RK_SIZE_20;
77}
78
79static int rk20_cas_fill_wave( INT16 *buffer, int length, UINT8 *bytes ) {
80   int i;
81   INT16 * p = buffer;
82
83   for (i=0; i<RK_HEADER_LEN; i++) {
84      p = rk_output_byte (p, 0x00, RK_SIZE_20 );
85   }
86
87   p = rk_output_byte (p, 0xE6, RK_SIZE_20);
88
89   for (i=0; i<data_size; i++)
90      p = rk_output_byte (p, bytes[i], RK_SIZE_20);
91
92   return p - buffer;
93}
94
95static int rk22_cas_fill_wave( INT16 *buffer, int length, UINT8 *bytes ) {
96   int i;
97   INT16 * p = buffer;
98
99   for (i=0; i<RK_HEADER_LEN; i++) {
100      p = rk_output_byte (p, 0x00, RK_SIZE_22 );
101   }
102
103   p = rk_output_byte (p, 0xE6, RK_SIZE_22);
104
105   for (i=0; i<data_size; i++)
106      p = rk_output_byte (p, bytes[i], RK_SIZE_22);
107
108   return p - buffer;
109}
110
111static int rk60_cas_fill_wave( INT16 *buffer, int length, UINT8 *bytes ) {
112   int i;
113   INT16 * p = buffer;
114
115   for (i=0; i<RK_HEADER_LEN; i++) {
116      p = rk_output_byte (p, 0x00, RK_SIZE_60 );
117   }
118
119   p = rk_output_byte (p, 0xE6, RK_SIZE_60);
120
121   for (i=0; i<data_size; i++)
122      p = rk_output_byte (p, bytes[i], RK_SIZE_60);
123
124   return p - buffer;
125}
126
127static int gam_cas_fill_wave( INT16 *buffer, int length, UINT8 *bytes ) {
128   int i;
129   INT16 * p = buffer;
130
131   for (i=0; i<RK_HEADER_LEN; i++) {
132      p = rk_output_byte (p, 0x00, RK_SIZE_20 );
133   }
134
135   for (i=0; i<data_size; i++)
136      p = rk_output_byte (p, bytes[i], RK_SIZE_20);
137
138   return p - buffer;
139}
140
141static const struct CassetteLegacyWaveFiller rk20_legacy_fill_wave = {
142   rk20_cas_fill_wave,         /* fill_wave */
143   -1,               /* chunk_size */
144   0,               /* chunk_samples */
145   rk20_cas_to_wav_size,         /* chunk_sample_calc */
146   RK_WAV_FREQUENCY,         /* sample_frequency */
147   0,               /* header_samples */
148   0               /* trailer_samples */
149};
150
151static casserr_t rk20_cassette_identify( cassette_image *cassette, struct CassetteOptions *opts ) {
152   return cassette_legacy_identify( cassette, opts, &rk20_legacy_fill_wave );
153}
154
155static casserr_t rk20_cassette_load( cassette_image *cassette ) {
156   return cassette_legacy_construct( cassette, &rk20_legacy_fill_wave );
157}
158
159static const struct CassetteLegacyWaveFiller rk22_legacy_fill_wave = {
160   rk22_cas_fill_wave,         /* fill_wave */
161   -1,               /* chunk_size */
162   0,               /* chunk_samples */
163   rk22_cas_to_wav_size,         /* chunk_sample_calc */
164   RK_WAV_FREQUENCY,         /* sample_frequency */
165   0,               /* header_samples */
166   0               /* trailer_samples */
167};
168
169static casserr_t rk22_cassette_identify( cassette_image *cassette, struct CassetteOptions *opts ) {
170   return cassette_legacy_identify( cassette, opts, &rk22_legacy_fill_wave );
171}
172
173static casserr_t rk22_cassette_load( cassette_image *cassette ) {
174   return cassette_legacy_construct( cassette, &rk22_legacy_fill_wave );
175}
176
177static const struct CassetteLegacyWaveFiller gam_legacy_fill_wave = {
178   gam_cas_fill_wave,         /* fill_wave */
179   -1,               /* chunk_size */
180   0,               /* chunk_samples */
181   gam_cas_to_wav_size,         /* chunk_sample_calc */
182   RK_WAV_FREQUENCY,         /* sample_frequency */
183   0,               /* header_samples */
184   0               /* trailer_samples */
185};
186
187static casserr_t gam_cassette_identify( cassette_image *cassette, struct CassetteOptions *opts ) {
188   return cassette_legacy_identify( cassette, opts, &gam_legacy_fill_wave );
189}
190
191static casserr_t gam_cassette_load( cassette_image *cassette ) {
192   return cassette_legacy_construct( cassette, &gam_legacy_fill_wave );
193}
194
195static const struct CassetteLegacyWaveFiller rk60_legacy_fill_wave = {
196   rk60_cas_fill_wave,         /* fill_wave */
197   -1,               /* chunk_size */
198   0,               /* chunk_samples */
199   rk60_cas_to_wav_size,         /* chunk_sample_calc */
200   RK_WAV_FREQUENCY,         /* sample_frequency */
201   0,               /* header_samples */
202   0               /* trailer_samples */
203};
204
205static casserr_t rk60_cassette_identify( cassette_image *cassette, struct CassetteOptions *opts ) {
206   return cassette_legacy_identify( cassette, opts, &rk60_legacy_fill_wave );
207}
208
209static casserr_t rk60_cassette_load( cassette_image *cassette ) {
210   return cassette_legacy_construct( cassette, &rk60_legacy_fill_wave );
211}
212
213static const struct CassetteFormat rku_cassette_format = {
214   "rku",
215   rk20_cassette_identify,
216   rk20_cassette_load,
217   NULL
218};
219
220static const struct CassetteFormat rk8_cassette_format = {
221   "rk8",
222   rk60_cassette_identify,
223   rk60_cassette_load,
224   NULL
225};
226
227static const struct CassetteFormat rks_cassette_format = {
228   "rks",
229   rk20_cassette_identify,
230   rk20_cassette_load,
231   NULL
232};
233
234static const struct CassetteFormat rko_cassette_format = {
235   "rko",
236   rk20_cassette_identify,
237   rk20_cassette_load,
238   NULL
239};
240
241static const struct CassetteFormat rkr_cassette_format = {
242   "rk,rkr",
243   rk20_cassette_identify,
244   rk20_cassette_load,
245   NULL
246};
247
248static const struct CassetteFormat rka_cassette_format = {
249   "rka",
250   rk20_cassette_identify,
251   rk20_cassette_load,
252   NULL
253};
254
255static const struct CassetteFormat rkm_cassette_format = {
256   "rkm",
257   rk22_cassette_identify,
258   rk22_cassette_load,
259   NULL
260};
261
262static const struct CassetteFormat rkp_cassette_format = {
263   "rkp",
264   rk20_cassette_identify,
265   rk20_cassette_load,
266   NULL
267};
268
269static const struct CassetteFormat gam_cassette_format = {
270   "gam,g16,pki",
271   gam_cassette_identify,
272   gam_cassette_load,
273   NULL
274};
275
276CASSETTE_FORMATLIST_START(rku_cassette_formats)
277   CASSETTE_FORMAT(rku_cassette_format)
278CASSETTE_FORMATLIST_END
279
280CASSETTE_FORMATLIST_START(rk8_cassette_formats)
281   CASSETTE_FORMAT(rk8_cassette_format)
282CASSETTE_FORMATLIST_END
283
284CASSETTE_FORMATLIST_START(rks_cassette_formats)
285   CASSETTE_FORMAT(rks_cassette_format)
286CASSETTE_FORMATLIST_END
287
288CASSETTE_FORMATLIST_START(rko_cassette_formats)
289   CASSETTE_FORMAT(rko_cassette_format)
290CASSETTE_FORMATLIST_END
291
292CASSETTE_FORMATLIST_START(rkr_cassette_formats)
293   CASSETTE_FORMAT(rkr_cassette_format)
294   CASSETTE_FORMAT(gam_cassette_format)
295CASSETTE_FORMATLIST_END
296
297CASSETTE_FORMATLIST_START(rka_cassette_formats)
298   CASSETTE_FORMAT(rka_cassette_format)
299CASSETTE_FORMATLIST_END
300
301CASSETTE_FORMATLIST_START(rkm_cassette_formats)
302   CASSETTE_FORMAT(rkm_cassette_format)
303CASSETTE_FORMATLIST_END
304
305CASSETTE_FORMATLIST_START(rkp_cassette_formats)
306   CASSETTE_FORMAT(rkp_cassette_format)
307CASSETTE_FORMATLIST_END
308
309CASSETTE_FORMATLIST_START(gam_cassette_formats)
310   CASSETTE_FORMAT(gam_cassette_format)
311CASSETTE_FORMATLIST_END
src/mess/formats/sord_cas.h
r11457r11458
1/*********************************************************************
2
3    sord_cas.h
4
5    Format code for Sord M5 cassette files
6
7*********************************************************************/
8
9#ifndef SORD_CAS_H
10#define SORD_CAS_H
11
12#include "imagedev/cassimg.h"
13
14CASSETTE_FORMATLIST_EXTERN(sordm5_cassette_formats);
15
16#endif /* SORD_CAS_H */
src/mess/formats/vg5k_cas.h
r11457r11458
1/*********************************************************************
2
3    vg5k_cas.h
4
5*********************************************************************/
6
7#ifndef VG5K_CAS_H
8#define VG5K_CAS_H
9
10#include "imagedev/cassimg.h"
11
12CASSETTE_FORMATLIST_EXTERN(vg5k_cassette_formats);
13
14#endif /* VG5K_CAS_H */
15
src/mess/formats/cgen_cas.h
r11457r11458
1/*********************************************************************
2
3    cgen_cas.h
4
5    Format code for EACA Colour Genie .cas casette files.
6
7*********************************************************************/
8
9#ifndef __CGEN_CAS_H
10#define __CGEN_CAS_H
11
12#include "imagedev/cassimg.h"
13
14CASSETTE_FORMATLIST_EXTERN(cgenie_cassette_formats);
15
16#endif /* __CGEN_CAS_H */
17
src/mess/formats/rk_cas.h
r11457r11458
1/*
2
3       rk_cas.h
4
5       Format code for rk cassette files
6*/
7
8#ifndef RK_CAS_H
9#define RK_CAS_H
10
11#include "imagedev/cassimg.h"
12
13CASSETTE_FORMATLIST_EXTERN(rku_cassette_formats);
14CASSETTE_FORMATLIST_EXTERN(rk8_cassette_formats);
15CASSETTE_FORMATLIST_EXTERN(rks_cassette_formats);
16CASSETTE_FORMATLIST_EXTERN(rko_cassette_formats);
17CASSETTE_FORMATLIST_EXTERN(rkr_cassette_formats);
18CASSETTE_FORMATLIST_EXTERN(rka_cassette_formats);
19CASSETTE_FORMATLIST_EXTERN(rkm_cassette_formats);
20CASSETTE_FORMATLIST_EXTERN(rkp_cassette_formats);
21CASSETTE_FORMATLIST_EXTERN(gam_cassette_formats);
22
23#endif /* RK_CAS_H */
24
src/mess/formats/coco_cas.c
r11457r11458
1/**************************************************************************
2
3    coco_cas.c
4
5    Format code for CoCo CAS (*.cas) files
6
7    This is the actual code that processes the CAS data.  CAS files are a
8    problem because they are a legacy of previous CoCo emulators, and most
9    of these emulators patch the system as a shortcut.  Thus, the ones and
10    zeroes in the file do not truly represent modulated pulses in the
11    waveform, but what the BIOS reads and writes.
12
13    In doing so, they make the CoCo more tolerant of short headers and lack
14    of delays between blocks.  This legacy is reflected in most CAS files in
15    use, and thus presents a problem for a pure hardware emulation like MESS.
16
17    One alternative is to preprocess the data on the CAS, file by file, but
18    this proves to be problematic because in the process, legitimate data
19    that is unrecognized by the preprocessor may get dropped.
20
21    The approach taken here is a hybrid approach - it retrieves the data
22    block by block until an error occurs; be it the end of the CAS or a
23    corrupt (?!) block.  When "legitimate" blocks are done processing, the
24    remainder of the data is added to the waveform in a traditional manner.
25    The result has proven to work quite well.
26
27    2005-May-04, P.Harvey-Smith, improved handling of some "copy protected"
28    Dragon games that have odd blocks in odd places, this has slightly
29    increased loading time but does mean games like "Rommel's Revenge" will
30    now load.
31
32**************************************************************************/
33
34#include "coco_cas.h"
35#include "timer.h"
36
37#define COCO_WAVESAMPLES_HEADER      (1.0)
38#define COCO_WAVESAMPLES_TRAILER   (1.0)
39#define COCO_LONGSILENCE         (5.0)
40
41//some games load with only 5s, but most games need 15s
42#define ALICE32_WAVESAMPLES_HEADER   (15.0)
43
44static int synccount;
45
46const struct CassetteModulation coco_cas_modulation =
47{
48   CASSETTE_MODULATION_SINEWAVE,
49   600.0,   1200.0,   1500.0,
50   1500.0,   2400.0,   3000.0
51};
52
53
54
55static casserr_t coco_cas_identify(cassette_image *cassette, struct CassetteOptions *opts)
56{
57   return cassette_modulation_identify(cassette, &coco_cas_modulation, opts);
58}
59
60
61
62static int get_cas_block(cassette_image *cassette, UINT64 *offset, UINT8 *block, int *block_len)
63{
64   UINT8 block_length = 0;
65   UINT8 block_checksum = 0;
66   UINT64 current_offset;
67   UINT64 image_size;
68   PAIR p;
69   int i;
70   int state = 0;
71   int phase = 0;
72
73   synccount = 0;
74   p.w.l = 0;
75   image_size = cassette_image_size(cassette);
76   current_offset = *offset;
77
78   while(current_offset < image_size)
79   {
80      cassette_image_read(cassette, &p.b.h, current_offset, 1);
81      current_offset++;
82
83      for (i = 0; i < 8; i++)
84      {
85         p.w.l >>= 1;
86
87         if (state == 0)
88         {
89            /* searching for a block */
90            if (p.b.l == 0x3C)
91            {
92               /* found one! */
93               phase = i;
94               state++;
95            }
96            else if (p.b.l == 0x55)
97            {
98               synccount++;
99            }
100         }
101         else if (i == phase)
102         {
103            *(block++) = p.b.l;
104            switch(state) {
105            case 1:
106               /* found file type */
107               block_checksum = p.b.l;
108               state++;
109               break;
110            case 2:
111               /* found file size */
112               block_length = p.b.l;
113               *block_len = ((int) block_length) + 3;
114               block_checksum += p.b.l;
115               state++;
116               break;
117
118            case 3:
119               /* data byte */
120               if (block_length)
121               {
122                  block_length--;
123                  block_checksum += p.b.l;
124               }
125               else
126               {
127                  /* end of block */
128                  if (p.b.l != block_checksum)
129                  {
130                     /* checksum failure */
131                     return FALSE;
132                  }
133                  else
134                  {
135                     /* checksum success */
136                     *offset = current_offset;
137                     return TRUE;
138                  }
139               }
140            }
141         }
142      }
143   }
144
145   /* no more blocks */
146   return FALSE;
147}
148
149
150
151static casserr_t cas_load(cassette_image *cassette, UINT8 silence)
152{
153   casserr_t err;
154   UINT64 offset;
155   UINT64 image_size;
156   UINT8 block[258];   /* 255 bytes per block + 3 (type, length, checksum) */
157   int block_length = 0;
158   UINT8 last_blocktype;
159   double time_index = 0.0;
160   double time_displacement;
161   static const UINT8 magic_bytes[2] = { 0x55, 0x3C };
162
163#if 0
164   {
165      static const UINT8 dummy_bytes[] =
166      {
167         0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
168         0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
169         0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
170         0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
171         0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
172         0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
173         0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
174         0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
175         0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
176         0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
177         0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
178         0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
179         0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
180         0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
181         0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
182         0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
183         0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
184         0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
185         0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
186         0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
187         0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
188         0x3C, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A
189      };
190      time_index = 10.0;
191      return cassette_put_modulated_data(cassette, 0, time_index, dummy_bytes, sizeof(dummy_bytes), &coco_cas_modulation, &time_displacement);
192   }
193#endif
194
195   err = cassette_put_sample(cassette, 0, time_index, COCO_WAVESAMPLES_HEADER, 0);
196   if (err)
197      return err;
198   time_index += COCO_WAVESAMPLES_HEADER;
199
200   offset = 0;
201   last_blocktype = 0;
202   image_size = cassette_image_size(cassette);
203
204   /* try to find a block that we can untangle */
205   while(get_cas_block(cassette, &offset, block, &block_length))
206   {
207      /* Forcing a silence before a filename block, improves the ability to load some */
208      /* copy protected Dragon games, e.g. Rommel's Revenge */
209      /* was the last block a filename block? */
210      if ((last_blocktype == 0) || (last_blocktype == 0xFF) || (block[0] == 0))
211      {
212         /* silence */
213         err = cassette_put_sample(cassette, 0, time_index, silence, 0);
214         if (err)
215            return err;
216         time_index += silence;
217
218         /* sync data */
219         err = cassette_put_modulated_filler(cassette, 0, time_index, 0x55, 128, &coco_cas_modulation, &time_displacement);
220         if (err)
221            return err;
222         time_index += time_displacement;
223      }
224      else if (synccount != 0)      /* If we have multiple sync bytes in cas file, make sure they */
225      {            /* are passed through */
226         /* sync data */
227         err = cassette_put_modulated_filler(cassette, 0, time_index, 0x55, synccount, &coco_cas_modulation, &time_displacement);
228         if (err)
229            return err;
230         time_index += time_displacement;
231      }
232
233      /* now fill in the magic bytes */
234      err = cassette_put_modulated_data(cassette, 0, time_index, magic_bytes, sizeof(magic_bytes), &coco_cas_modulation, &time_displacement);
235      if (err)
236         return err;
237      time_index += time_displacement;
238
239      /* now fill in the block */
240      err = cassette_put_modulated_data(cassette, 0, time_index, block, block_length, &coco_cas_modulation, &time_displacement);
241      if (err)
242         return err;
243      time_index += time_displacement;
244
245      /* and the last magic byte */
246      err = cassette_put_modulated_filler(cassette, 0, time_index, 0x55, 1, &coco_cas_modulation, &time_displacement);
247      if (err)
248         return err;
249      time_index += time_displacement;
250
251      last_blocktype = block[0];
252   }
253
254   /* all futher data is undecipherable, so output it verbatim */
255   err = cassette_read_modulated_data(cassette, 0, time_index, offset, image_size - offset, &coco_cas_modulation, &time_displacement);
256   if (err)
257      return err;
258   time_index += time_displacement;
259
260   return CASSETTE_ERROR_SUCCESS;
261}
262
263static casserr_t coco_cas_load(cassette_image *cassette)
264{
265   return cas_load(cassette, COCO_WAVESAMPLES_HEADER);
266}
267
268static casserr_t alice32_cas_load(cassette_image *cassette)
269{
270   return cas_load(cassette, ALICE32_WAVESAMPLES_HEADER);
271}
272
273const struct CassetteFormat coco_cas_format =
274{
275   "cas",
276   coco_cas_identify,
277   coco_cas_load,
278   NULL
279};
280
281const struct CassetteFormat alice32_cas_format =
282{
283   "cas,c10,k7",
284   coco_cas_identify,
285   alice32_cas_load,
286   NULL
287};
288
289
290CASSETTE_FORMATLIST_START(coco_cassette_formats)
291   CASSETTE_FORMAT(coco_cas_format)
292CASSETTE_FORMATLIST_END
293
294CASSETTE_FORMATLIST_START(alice32_cassette_formats)
295   CASSETTE_FORMAT(alice32_cas_format)
296CASSETTE_FORMATLIST_END
src/mess/formats/imd_dsk.c
r11457r11458
1/*********************************************************************
2
3    formats/imd_dsk.c
4
5    IMD disk images
6
7*********************************************************************/
8
9#include <string.h>
10#include "emu.h"
11#include "imagedev/flopimg.h"
12#include "imagedev/flopdrv.h"
13
14struct imddsk_tag
15{
16   int heads;
17   int tracks;
18   int sector_size;
19   UINT64 track_offsets[84*2]; /* offset within data for each track */
20};
21
22
23static struct imddsk_tag *get_tag(floppy_image *floppy)
24{
25   struct imddsk_tag *tag;
26   tag = (imddsk_tag *)floppy_tag(floppy);
27   return tag;
28}
29
30
31
32FLOPPY_IDENTIFY( imd_dsk_identify )
33{
34   UINT8 header[3];
35
36   floppy_image_read(floppy, header, 0, 3);
37   if (header[0]=='I' && header[1]=='M' && header[2]=='D') {
38      *vote = 100;
39   } else {
40      *vote = 0;
41   }
42   return FLOPPY_ERROR_SUCCESS;
43}
44
45static int imd_get_heads_per_disk(floppy_image *floppy)
46{
47   return get_tag(floppy)->heads;
48}
49
50static int imd_get_tracks_per_disk(floppy_image *floppy)
51{
52   return get_tag(floppy)->tracks;
53}
54
55static UINT64 imd_get_track_offset(floppy_image *floppy, int head, int track)
56{
57   return get_tag(floppy)->track_offsets[(track<<1) + head];
58}
59
60static floperr_t get_offset(floppy_image *floppy, int head, int track, int sector, int sector_is_index, UINT64 *offset)
61{
62   UINT64 offs = 0;
63   UINT8 header[5];
64   UINT8 sector_num;
65   int i;
66
67
68   if ((head < 0) || (head >= get_tag(floppy)->heads) || (track < 0) || (track >= get_tag(floppy)->tracks)
69         || (sector < 0) )
70      return FLOPPY_ERROR_SEEKERROR;
71
72   offs = imd_get_track_offset(floppy,head,track);
73   floppy_image_read(floppy, header, offs, 5);
74
75   sector_num = header[3];
76   offs += 5 + sector_num; // skip header and sector numbering map
77   if(header[2] & 0x80) offs += sector_num; // skip cylinder numbering map
78   if(header[2] & 0x40) offs += sector_num; // skip head numbering map
79   get_tag(floppy)->sector_size = 1 << (header[4] + 7);
80   for(i=0;i<sector;i++) {
81      floppy_image_read(floppy, header, offs, 1); // take sector data type
82      switch(header[0]) {
83         case 0: offs++; break;
84         case 1:
85         case 3:
86         case 5:
87         case 7: offs += get_tag(floppy)->sector_size + 1; break;
88         default: offs += 2;
89      }
90   }
91   if (offset)
92      *offset = offs;
93   return FLOPPY_ERROR_SUCCESS;
94}
95
96
97
98static floperr_t internal_imd_read_sector(floppy_image *floppy, int head, int track, int sector, int sector_is_index, void *buffer, size_t buflen)
99{
100   UINT64 offset;
101   floperr_t err;
102   UINT8 header[1];
103
104   // take sector offset
105   err = get_offset(floppy, head, track, sector, sector_is_index, &offset);
106   if (err)
107      return err;
108
109   floppy_image_read(floppy, header, offset, 1);
110   switch(header[0]) {
111      case 0: break;
112      case 1:
113      case 3:
114      case 5:
115      case 7:
116            floppy_image_read(floppy, buffer, offset+1, buflen);
117            break;
118
119      default: // all data same
120            floppy_image_read(floppy, header, offset+1, 1);
121            memset(buffer,header[0],buflen);
122            break;
123   }
124
125   return FLOPPY_ERROR_SUCCESS;
126}
127
128
129static floperr_t imd_read_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen)
130{
131   return internal_imd_read_sector(floppy, head, track, sector, FALSE, buffer, buflen);
132}
133
134static floperr_t imd_read_indexed_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen)
135{
136   return internal_imd_read_sector(floppy, head, track, sector, TRUE, buffer, buflen);
137}
138
139static floperr_t imd_get_sector_length(floppy_image *floppy, int head, int track, int sector, UINT32 *sector_length)
140{
141   floperr_t err;
142   err = get_offset(floppy, head, track, sector, FALSE, NULL);
143   if (err)
144      return err;
145
146   if (sector_length) {
147      *sector_length = get_tag(floppy)->sector_size;
148   }
149   return FLOPPY_ERROR_SUCCESS;
150}
151
152static floperr_t imd_get_indexed_sector_info(floppy_image *floppy, int head, int track, int sector_index, int *cylinder, int *side, int *sector, UINT32 *sector_length, unsigned long *flags)
153{
154   UINT64 offset;
155   UINT8 header[5];
156   UINT8 hd;
157   UINT8 tr;
158   UINT32 sector_size;
159   UINT8 sector_num;
160
161   offset = imd_get_track_offset(floppy,head,track);
162   floppy_image_read(floppy, header, offset, 5);
163   tr = header[1];
164   hd = header[2];
165   sector_num = header[3];
166   sector_size = 1 << (header[4] + 7);
167   if (sector_index >= sector_num) return FLOPPY_ERROR_SEEKERROR;
168   if (cylinder) {
169      if (head & 0x80) {
170         floppy_image_read(floppy, header, offset + 5 + sector_num+ sector_index, 1);
171         *cylinder = header[0];
172      } else {
173         *cylinder = tr;
174      }
175   }
176   if (side) {
177      if (head & 0x40) {
178         if (head & 0x80) {
179            floppy_image_read(floppy, header, offset + 5 + 2 * sector_num+sector_index, 1);
180         } else {
181            floppy_image_read(floppy, header, offset + 5 + sector_num+sector_index, 1);
182         }
183         *side = header[0];
184      } else {
185         *side = hd & 1;
186      }
187   }
188   if (sector) {
189      floppy_image_read(floppy, header, offset + 5 + sector_index, 1);
190      *sector = header[0];
191   }
192   if (sector_length) {
193      *sector_length = sector_size;
194   }
195   if (flags) {
196      UINT8 skip;
197      if (head & 0x40) {
198         if (head & 0x80) {
199            skip = 3;
200         } else {
201            skip = 2;
202         }
203      } else {
204         skip = 1;
205      }
206      floppy_image_read(floppy, header, offset + 5 + skip * sector_num, 1);
207      *flags = 0;
208      if ((header[0]-1) & 0x02) *flags |= ID_FLAG_DELETED_DATA;
209      if ((header[0]-1) & 0x04) *flags |= ID_FLAG_CRC_ERROR_IN_DATA_FIELD;
210   }
211   return FLOPPY_ERROR_SUCCESS;
212}
213
214
215FLOPPY_CONSTRUCT( imd_dsk_construct )
216{
217   struct FloppyCallbacks *callbacks;
218   struct imddsk_tag *tag;
219   UINT8 header[0x100];
220   UINT64 pos = 0;
221   int sector_size = 0;
222   int sector_num;
223   int i;
224   if(params)
225   {
226      // create
227      return FLOPPY_ERROR_UNSUPPORTED;
228   }
229
230   tag = (struct imddsk_tag *) floppy_create_tag(floppy, sizeof(struct imddsk_tag));
231   if (!tag)
232      return FLOPPY_ERROR_OUTOFMEMORY;
233
234   floppy_image_read(floppy, header, pos, 1);
235   while(header[0]!=0x1a) {
236      pos++;
237      floppy_image_read(floppy, header, pos, 1);
238   }
239   pos++;
240   tag->tracks = 0;
241   tag->heads = 1;
242   do {
243      floppy_image_read(floppy, header, pos, 5);
244      if ((header[2] & 1)==1) tag->heads = 2;
245      tag->track_offsets[(header[1]<<1) + (header[2] & 1)] = pos;
246      sector_num = header[3];
247      pos += 5 + sector_num; // skip header and sector numbering map
248      if(header[2] & 0x80) pos += sector_num; // skip cylinder numbering map
249      if(header[2] & 0x40) pos += sector_num; // skip head numbering map
250      sector_size = 1 << (header[4] + 7);
251      for(i=0;i<sector_num;i++) {
252         floppy_image_read(floppy, header, pos, 1); // take sector data type
253         switch(header[0]) {
254            case 0: pos++; break;
255            case 1:
256            case 3:
257            case 5:
258            case 7: pos += sector_size + 1; break;
259            default: pos += 2;break;
260         }
261      }
262      tag->tracks += 1;
263   } while(pos < floppy_image_size(floppy));
264   if (tag->heads==2) {
265      tag->tracks = tag->tracks / 2;
266   }
267   callbacks = floppy_callbacks(floppy);
268   callbacks->read_sector = imd_read_sector;
269   callbacks->read_indexed_sector = imd_read_indexed_sector;
270   callbacks->get_sector_length = imd_get_sector_length;
271   callbacks->get_heads_per_disk = imd_get_heads_per_disk;
272   callbacks->get_tracks_per_disk = imd_get_tracks_per_disk;
273   callbacks->get_indexed_sector_info = imd_get_indexed_sector_info;
274
275   return FLOPPY_ERROR_SUCCESS;
276}
277
src/mess/formats/coco_cas.h
r11457r11458
1/*********************************************************************
2
3    coco_cas.h
4
5    Format code for CoCo CAS (*.cas) files
6
7*********************************************************************/
8
9#ifndef COCO_CAS_H
10#define COCO_CAS_H
11
12#include "imagedev/cassimg.h"
13
14extern const struct CassetteFormat coco_cas_format;
15extern const struct CassetteModulation coco_cas_modulation;
16
17CASSETTE_FORMATLIST_EXTERN(coco_cassette_formats);
18CASSETTE_FORMATLIST_EXTERN(alice32_cassette_formats);
19
20#endif /* COCO_CAS_H */
src/mess/formats/pmd_pmd.c
r11457r11458
1/* .PMD tape images */
2#include "pmd_pmd.h"
3
4#define WAVEENTRY_LOW  -32768
5#define WAVEENTRY_HIGH  32767
6
7#define PMD85_WAV_FREQUENCY   7200
8#define PMD85_TIMER_FREQUENCY   1200
9#define PMD85_BIT_LENGTH   (PMD85_WAV_FREQUENCY/PMD85_TIMER_FREQUENCY)
10#define PMD85_PILOT_BITS   (PMD85_TIMER_FREQUENCY*3)
11#define PMD85_PAUSE_BITS   (PMD85_TIMER_FREQUENCY/2)
12#define PMD85_HEADER_BYTES   63
13#define PMD85_BITS_PER_BYTE   11
14
15static INT16 *pmd85_emit_level(INT16 *p, int count, int level)
16{
17   int i;
18
19   for (i=0; i<count; i++)   *(p++) = level;
20
21   return p;
22}
23
24static INT16* pmd85_output_bit(INT16 *p, UINT8 bit)
25{
26   if (bit)
27   {
28      p = pmd85_emit_level (p, PMD85_BIT_LENGTH/2, WAVEENTRY_LOW);
29      p = pmd85_emit_level (p, PMD85_BIT_LENGTH/2, WAVEENTRY_HIGH);
30   }
31   else
32   {
33      p = pmd85_emit_level (p, PMD85_BIT_LENGTH/2, WAVEENTRY_HIGH);
34      p = pmd85_emit_level (p, PMD85_BIT_LENGTH/2, WAVEENTRY_LOW);
35   }
36       return p;
37}
38
39static INT16* pmd85_output_byte(INT16 *p, UINT8 byte)
40{
41   int i;
42
43   /* start */
44   p = pmd85_output_bit (p, 0);
45
46   /* data */
47   for (i=0; i<8; i++)
48      p = pmd85_output_bit(p,(byte>>(7-i)) & 0x01);
49
50   /* stop */
51   p = pmd85_output_bit (p, 1);
52   p = pmd85_output_bit (p, 1);
53
54   return p;
55}
56
57static int pmd85_cassette_calculate_size_in_samples(const UINT8 *bytes, int length)
58{
59   return PMD85_BIT_LENGTH * (length * PMD85_BITS_PER_BYTE + PMD85_PILOT_BITS + PMD85_PAUSE_BITS);
60}
61
62static int pmd85_cassette_fill_wave(INT16 *buffer, int length, UINT8 *bytes)
63{
64   int i;
65   INT16 * p = buffer;
66
67   int data_size = ((length/PMD85_BIT_LENGTH-PMD85_PILOT_BITS-PMD85_PAUSE_BITS)/PMD85_BITS_PER_BYTE)-PMD85_HEADER_BYTES;
68
69   /* pilot */
70   for (i=0; i<PMD85_PILOT_BITS; i++)
71      p = pmd85_output_bit(p, 1);
72
73   /* header */
74   for (i=0; i<PMD85_HEADER_BYTES; i++)
75      p = pmd85_output_byte(p, bytes[i]);
76
77   /* pause */
78   for (i=0; i<PMD85_PAUSE_BITS; i++)
79      p = pmd85_output_bit(p, 1);
80
81   /* data */
82   for (i=0; i<data_size; i++)
83      p = pmd85_output_byte(p, bytes[i+PMD85_HEADER_BYTES]);
84
85   return p - buffer;
86}
87
88static const struct CassetteLegacyWaveFiller pmd85_legacy_fill_wave =
89{
90   pmd85_cassette_fill_wave,         /* fill_wave */
91   -1,                                 /* chunk_size */
92   0,                                 /* chunk_samples */
93   pmd85_cassette_calculate_size_in_samples,   /* chunk_sample_calc */
94   PMD85_WAV_FREQUENCY,                              /* sample_frequency */
95   0,                                 /* header_samples */
96   0                                 /* trailer_samples */
97};
98
99static casserr_t pmd85_pmd_identify(cassette_image *cassette, struct CassetteOptions *opts)
100{
101   return cassette_legacy_identify(cassette, opts, &pmd85_legacy_fill_wave);
102}
103
104static casserr_t pmd85_pmd_load(cassette_image *cassette)
105{
106   return cassette_legacy_construct(cassette, &pmd85_legacy_fill_wave);
107}
108
109static const struct CassetteFormat pmd85_pmd_image_format =
110{
111   "pmd",
112   pmd85_pmd_identify,
113   pmd85_pmd_load,
114   NULL
115};
116
117CASSETTE_FORMATLIST_START(pmd85_pmd_format)
118   CASSETTE_FORMAT(pmd85_pmd_image_format)
119CASSETTE_FORMATLIST_END
src/mess/formats/pmd_pmd.h
r11457r11458
1/*********************************************************************
2
3    pmd_pmd.h
4
5    Format code for PMD-85 casette files
6
7*********************************************************************/
8
9#ifndef PMD_PMD_H
10#define PMD_PMD_H
11
12#include "imagedev/cassimg.h"
13
14CASSETTE_FORMATLIST_EXTERN(pmd85_pmd_format);
15
16#endif /* PMD_PMD_H */
src/mess/formats/kim1_cas.c
r11457r11458
1#include "kim1_cas.h"
2
3#define SMPLO   -32768
4#define SMPHI   32767
5
6
7static int cas_size;
8
9
10INLINE int kim1_output_signal( INT16 *buffer, int sample_pos, int high )
11{
12   int sample_count, i, j;
13
14   sample_count = 0;
15
16   if ( high )
17   {
18      /* high frequency (~3600Hz) */
19      for ( i = 0; i < 9; i++ )
20      {
21         if ( buffer )
22         {
23            for ( j = 0; j < 6; j++ )
24               buffer[ sample_pos + sample_count + j ] = SMPHI;
25         }
26         sample_count += 6;
27
28         if ( buffer )
29         {
30            for ( j = 0; j < 6; j++ )
31               buffer[ sample_pos + sample_count + j ] = SMPLO;
32         }
33         sample_count += 6;
34      }
35   }
36   else
37   {
38      /* low frequency (~2400Hz) */
39      for ( i = 0; i < 6; i++ )
40      {
41         if ( buffer )
42         {
43            for ( j = 0; j < 9; j++ )
44               buffer[ sample_pos + sample_count + j ] = SMPHI;
45         }
46         sample_count += 9;
47
48         if ( buffer )
49         {
50            for ( j = 0; j < 9; j++ )
51               buffer[ sample_pos + sample_count + j ] = SMPLO;
52         }
53         sample_count += 9;
54      }
55   }
56
57   return sample_count;
58}
59
60
61INLINE int kim1_output_byte( INT16 *buffer, int sample_pos, UINT8 byte )
62{
63   int i;
64   int sample_count = 0;
65
66   for ( i = 0; i < 8; i++ )
67   {
68      sample_count += kim1_output_signal( buffer, sample_pos + sample_count, 1 );
69      sample_count += kim1_output_signal( buffer, sample_pos + sample_count, byte & 0x01 ? 0 : 1 );
70      sample_count += kim1_output_signal( buffer, sample_pos + sample_count, 0 );
71      byte >>= 1;
72   }
73   return sample_count;
74}
75
76
77static int kim1_handle_kim(INT16 *buffer, const UINT8 *casdata)
78{
79   static const UINT8 encoding[16] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46 };
80   int   i, data_pos, sample_count;
81   UINT16 size, address, file_id, checksum;
82
83   if ( cas_size < 9 )   return -1;
84   if ( memcmp( casdata, "KIM1", 4 ) ) return -1;
85
86   address = casdata[4] | ( casdata[5] << 8 );
87   size = casdata[6] | ( casdata[7] << 8 );
88   file_id = casdata[8];
89
90   data_pos = 9;
91   sample_count = 0;
92   checksum = casdata[4] + casdata[5];
93
94   /* First output a sync header: 100 x 16h */
95   for ( i = 0; i < 100; i++ )
96      sample_count += kim1_output_byte( buffer, sample_count, 0x16 );
97
98   /* Output end of sync: 2Ah */
99   sample_count += kim1_output_byte( buffer, sample_count, 0x2A );
100
101   /* Output ID */
102   sample_count += kim1_output_byte( buffer, sample_count, encoding[ file_id >> 4 ] );
103   sample_count += kim1_output_byte( buffer, sample_count, encoding[ file_id & 0x0f ] );
104
105   /* Output starting address */
106   sample_count += kim1_output_byte( buffer, sample_count, encoding[ ( address & 0xff ) >> 4 ] );
107   sample_count += kim1_output_byte( buffer, sample_count, encoding[ address & 0x0f ] );
108   sample_count += kim1_output_byte( buffer, sample_count, encoding[ address >> 12 ] );
109   sample_count += kim1_output_byte( buffer, sample_count, encoding[ ( address >> 8 ) & 0x0f ] );
110
111   /* Output the data */
112   while( data_pos < cas_size && data_pos < ( size + 9 ) )
113   {
114      UINT8 data = casdata[data_pos];
115
116      sample_count += kim1_output_byte( buffer, sample_count, encoding[ data >> 4 ] );
117      sample_count += kim1_output_byte( buffer, sample_count, encoding[ data & 0x0f ] );
118      checksum += data;
119      data_pos++;
120   }
121
122   /* Output end of data marker: 2Fh */
123   sample_count += kim1_output_byte( buffer, sample_count, 0x2F );
124
125   /* Output checksum */
126   sample_count += kim1_output_byte( buffer, sample_count, encoding[ ( checksum & 0xff ) >> 4 ] );
127   sample_count += kim1_output_byte( buffer, sample_count, encoding[ checksum & 0x0f ] );
128   sample_count += kim1_output_byte( buffer, sample_count, encoding[ checksum >> 12 ] );
129   sample_count += kim1_output_byte( buffer, sample_count, encoding[ ( checksum >> 8 ) & 0x0f ] );
130
131   /* Output end of transmission marker: 2 x 04h */
132   sample_count += kim1_output_byte( buffer, sample_count, 0x04 );
133   sample_count += kim1_output_byte( buffer, sample_count, 0x04 );
134
135   return sample_count;
136}
137
138
139/*******************************************************************
140   Generate samples for the tape image
141********************************************************************/
142static int kim1_kim_fill_wave(INT16 *buffer, int sample_count, UINT8 *bytes)
143{
144   return kim1_handle_kim( buffer, bytes );
145}
146
147
148/*******************************************************************
149   Calculate the number of samples needed for this tape image
150********************************************************************/
151static int kim1_kim_to_wav_size(const UINT8 *casdata, int caslen)
152{
153   cas_size = caslen;
154
155   return kim1_handle_kim( NULL, casdata );
156}
157
158static const struct CassetteLegacyWaveFiller kim1_kim_legacy_fill_wave =
159{
160   kim1_kim_fill_wave,                  /* fill_wave */
161   -1,                              /* chunk_size */
162   0,                              /* chunk_samples */
163   kim1_kim_to_wav_size,               /* chunk_sample_calc */
164   44100,                           /* sample_frequency */
165   0,                              /* header_samples */
166   0                              /* trailer_samples */
167};
168
169
170static casserr_t kim1_kim_identify(cassette_image *cassette, struct CassetteOptions *opts)
171{
172   return cassette_legacy_identify(cassette, opts, &kim1_kim_legacy_fill_wave);
173}
174
175
176static casserr_t kim1_kim_load(cassette_image *cassette)
177{
178   return cassette_legacy_construct(cassette, &kim1_kim_legacy_fill_wave);
179}
180
181
182static const struct CassetteFormat kim1_kim_format =
183{
184   "kim,kim1",
185   kim1_kim_identify,
186   kim1_kim_load,
187   NULL
188};
189
190
191CASSETTE_FORMATLIST_START(kim1_cassette_formats)
192   CASSETTE_FORMAT(kim1_kim_format)
193CASSETTE_FORMATLIST_END
194
src/mess/formats/kim1_cas.h
r11457r11458
1/*********************************************************************
2
3    kim1_cas.h
4
5    Format code for KIM-1 casette files.
6
7*********************************************************************/
8
9#ifndef __KIM1_CAS_H
10#define __KIM1_CAS_H
11
12#include "imagedev/cassimg.h"
13
14CASSETTE_FORMATLIST_EXTERN(kim1_cassette_formats);
15
16#endif /* __KIM1_CAS_H */
17
src/mess/formats/oric_dsk.c
r11457r11458
1/*********************************************************************
2
3    formats/oric_dsk.c
4
5    Oric disk images
6
7*********************************************************************/
8
9#include <string.h>
10
11#include "emu.h"
12#include "imageutl.h"
13#include "formats/oric_dsk.h"
14#include "formats/basicdsk.h"
15#include "imagedev/flopdrv.h"
16
17
18#define mfm_disk_header_size   0x0100
19#define MFM_ID   "MFM_DISK"
20
21#define TRACK_SIZE_MFM 0x1900
22
23struct mfm_disk_sector_info
24{
25   int id_ptr;
26   int data_ptr;
27   int sector_size;
28   UINT8 ddam;
29};
30
31struct oricdsk_tag
32{
33   int tracks;
34   int heads;
35   int geometry;
36   int tracksize;
37   int num_sectors;
38   struct mfm_disk_sector_info   sector_data[32];
39};
40
41
42static struct oricdsk_tag *get_tag(floppy_image *floppy)
43{
44   struct oricdsk_tag *tag;
45   tag = (oricdsk_tag *)floppy_tag(floppy);
46   return tag;
47}
48
49
50static FLOPPY_IDENTIFY(oric_dsk_identify)
51{
52   UINT8 header[mfm_disk_header_size];
53
54   floppy_image_read(floppy, header, 0, mfm_disk_header_size);
55   if ( memcmp( header, MFM_ID, 8 ) ==0) {
56      int heads  = pick_integer_le(header, 8, 4);
57      int tracks = pick_integer_le(header, 12, 4);
58
59      if (floppy_image_size(floppy)==((tracks*heads*TRACK_SIZE_MFM)+mfm_disk_header_size)) {
60         *vote = 100;
61      } else {
62         *vote = 0;
63      }
64   } else {
65      *vote = 0;
66   }
67   return FLOPPY_ERROR_SUCCESS;
68}
69static int oric_get_track_offset(floppy_image *floppy,int track, int head)
70{
71   if (get_tag(floppy)->geometry==1) {
72      return mfm_disk_header_size + (get_tag(floppy)->tracksize * track) + (head * get_tag(floppy)->tracksize * get_tag(floppy)->tracks);
73   } else {
74      return mfm_disk_header_size + (get_tag(floppy)->tracksize*((track * get_tag(floppy)->heads)+head));
75   }
76}
77
78static int oric_get_heads_per_disk(floppy_image *floppy)
79{
80   return get_tag(floppy)->heads;
81}
82
83static int oric_get_tracks_per_disk(floppy_image *floppy)
84{
85   return get_tag(floppy)->tracks;
86}
87
88static void mfm_info_cache_sector_info(floppy_image *floppy,int track,int head)
89{
90   UINT8 track_data[TRACK_SIZE_MFM];
91
92   /* initialise these with single density values if single density */
93   UINT8 IdMark = 0x0fe;
94   UINT8 DataMark = 0x0fb;
95   UINT8 DeletedDataMark = 0x0f8;
96
97   UINT8 SectorCount;
98   UINT8 SearchCode = 0;
99   UINT8 sector_number = 0;
100   int ptr = 0;
101   int track_offset = oric_get_track_offset(floppy,track,head);
102   floppy_image_read(floppy, track_data, track_offset, TRACK_SIZE_MFM);
103   SectorCount = 0;
104
105   do
106   {
107      switch (SearchCode)
108      {
109         /* searching for id's */
110         case 0:
111         {
112            /* found id mark? */
113            if (track_data[ptr] == IdMark)
114            {
115               sector_number  = track_data[ptr+3]-1;
116               /* store pointer to id mark */
117               get_tag(floppy)->sector_data[sector_number].id_ptr = ptr + track_offset;
118               SectorCount++;
119
120               /* grab N value - used to skip data in data field */
121               get_tag(floppy)->sector_data[sector_number].sector_size = (1<< (track_data[ptr+4]+7));
122
123               /* skip past id field and crc */
124               ptr+=7;
125
126               /* now looking for data field */
127               SearchCode = 1;
128            }
129            else
130            {
131               /* update position */
132               ptr++;
133            }
134         }
135         break;
136
137         /* searching for data id's */
138         case 1:
139         {
140            /* found data or deleted data? */
141            if ((track_data[ptr] == DataMark) || (track_data[ptr] == DeletedDataMark))
142            {
143               /* yes */
144               get_tag(floppy)->sector_data[sector_number].data_ptr = ptr + track_offset + 1;
145               get_tag(floppy)->sector_data[sector_number].ddam = (track_data[ptr] == DeletedDataMark) ? ID_FLAG_DELETED_DATA : 0;
146
147               /* skip data field and id */
148               ptr += get_tag(floppy)->sector_data[sector_number].sector_size + 3;
149
150               /* now looking for id field */
151               SearchCode = 0;
152            }
153            else
154            {
155               ptr++;
156            }
157         }
158         break;
159
160         default:
161            break;
162      }
163   }
164   while (ptr < TRACK_SIZE_MFM);
165   get_tag(floppy)->num_sectors = SectorCount;
166
167}
168
169static floperr_t get_offset(floppy_image *floppy, int head, int track, int sector, int sector_is_index, UINT64 *offset)
170{
171   UINT64 offs;
172
173   /* translate the sector to a raw sector */
174   if (!sector_is_index)
175   {
176      sector -= 1;
177   }
178   mfm_info_cache_sector_info(floppy,track,head);
179
180   /* check to see if we are out of range */
181   if ((head < 0) || (head >= get_tag(floppy)->heads) || (track < 0) || (track >= get_tag(floppy)->tracks)
182         || (sector < 0) || (sector >=get_tag(floppy)->num_sectors))
183      return FLOPPY_ERROR_SEEKERROR;
184
185   offs = get_tag(floppy)->sector_data[sector].data_ptr;
186   if (offset)
187      *offset = offs;
188   return FLOPPY_ERROR_SUCCESS;
189}
190
191
192
193static floperr_t internal_oric_read_sector(floppy_image *floppy, int head, int track, int sector, int sector_is_index, void *buffer, size_t buflen)
194{
195   UINT64 offset;
196   floperr_t err;
197   err = get_offset(floppy, head, track, sector, sector_is_index, &offset);
198   if (err)
199      return err;
200   floppy_image_read(floppy, buffer, offset, buflen);
201   return FLOPPY_ERROR_SUCCESS;
202}
203
204
205
206static floperr_t internal_oric_write_sector(floppy_image *floppy, int head, int track, int sector, int sector_is_index, const void *buffer, size_t buflen, int ddam)
207{
208   UINT64 offset;
209   floperr_t err;
210
211   err = get_offset(floppy, head, track, sector, sector_is_index, &offset);
212   if (err)
213      return err;
214
215   floppy_image_write(floppy, buffer, offset, buflen);
216   return FLOPPY_ERROR_SUCCESS;
217}
218
219
220
221static floperr_t oric_read_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen)
222{
223   return internal_oric_read_sector(floppy, head, track, sector, FALSE, buffer, buflen);
224}
225
226static floperr_t oric_write_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam)
227{
228   return internal_oric_write_sector(floppy, head, track, sector, FALSE, buffer, buflen, ddam);
229}
230
231static floperr_t oric_read_indexed_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen)
232{
233   return internal_oric_read_sector(floppy, head, track, sector, TRUE, buffer, buflen);
234}
235
236static floperr_t oric_write_indexed_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam)
237{
238   return internal_oric_write_sector(floppy, head, track, sector, TRUE, buffer, buflen, ddam);
239}
240
241static floperr_t oric_get_sector_length(floppy_image *floppy, int head, int track, int sector, UINT32 *sector_length)
242{
243   floperr_t err;
244   err = get_offset(floppy, head, track, sector, FALSE, NULL);
245   if (err)
246      return err;
247
248   if (sector_length) {
249      *sector_length = get_tag(floppy)->sector_data[sector].sector_size;
250   }
251   return FLOPPY_ERROR_SUCCESS;
252}
253
254
255
256static floperr_t oric_get_indexed_sector_info(floppy_image *floppy, int head, int track, int sector_index, int *cylinder, int *side, int *sector, UINT32 *sector_length, unsigned long *flags)
257{
258   floperr_t retVal;
259
260   sector_index += 1;
261
262   retVal = oric_get_sector_length(floppy, head, track, sector_index, sector_length);
263   if (sector_length!=NULL) {
264      *sector_length =  get_tag(floppy)->sector_data[sector_index-1].sector_size;
265   }
266   if (cylinder)
267      *cylinder = track;
268   if (side)
269      *side = head;
270   if (sector)
271      *sector = sector_index;
272   if (flags)
273      *flags = get_tag(floppy)->sector_data[sector_index].ddam;
274   return retVal;
275}
276
277
278static FLOPPY_CONSTRUCT(oric_dsk_construct)
279{
280   struct FloppyCallbacks *callbacks;
281   struct oricdsk_tag *tag;
282   UINT8 header[mfm_disk_header_size];
283
284   floppy_image_read(floppy, header, 0, mfm_disk_header_size);
285
286   tag = (struct oricdsk_tag *) floppy_create_tag(floppy, sizeof(struct oricdsk_tag));
287   if (!tag)
288      return FLOPPY_ERROR_OUTOFMEMORY;
289
290   tag->heads   = pick_integer_le(header, 8, 4);
291   tag->tracks  = pick_integer_le(header, 12, 4);
292   tag->geometry = pick_integer_le(header, 16, 4);
293   tag->tracksize = TRACK_SIZE_MFM;
294   memset(tag->sector_data,0,sizeof(tag->sector_data));
295
296   callbacks = floppy_callbacks(floppy);
297   callbacks->read_sector = oric_read_sector;
298   callbacks->write_sector = oric_write_sector;
299   callbacks->read_indexed_sector = oric_read_indexed_sector;
300   callbacks->write_indexed_sector = oric_write_indexed_sector;
301   callbacks->get_sector_length = oric_get_sector_length;
302   callbacks->get_heads_per_disk = oric_get_heads_per_disk;
303   callbacks->get_tracks_per_disk = oric_get_tracks_per_disk;
304   callbacks->get_indexed_sector_info = oric_get_indexed_sector_info;
305   return FLOPPY_ERROR_SUCCESS;
306}
307/* ----------------------------------------------------------------------- */
308
309FLOPPY_OPTIONS_START( oric )
310   FLOPPY_OPTION( oricmfm, "dsk", "Oric MFM floppy disk image",   oric_dsk_identify, oric_dsk_construct, NULL, NULL)
311   FLOPPY_OPTION( oric, "dsk", "Oric disk image", basicdsk_identify_default, basicdsk_construct_default, NULL,
312      HEADS([2])
313      TRACKS([80])
314      SECTORS([9])
315      SECTOR_LENGTH([512])
316      FIRST_SECTOR_ID([1]))
317FLOPPY_OPTIONS_END
src/mess/formats/oric_dsk.h
r11457r11458
1/*********************************************************************
2
3    formats/oric_dsk.h
4
5    Oric disk images
6
7*********************************************************************/
8
9#ifndef ORIC_DSK_H
10#define ORIC_DSK_H
11
12#include "imagedev/flopimg.h"
13
14/**************************************************************************/
15
16FLOPPY_OPTIONS_EXTERN(oric);
17
18#endif /* ORIC_DSK_H */
src/mess/formats/smx_dsk.c
r11457r11458
1/*********************************************************************
2
3    formats/smx_dsk.c
4
5    Specialist MX disk images
6
7*********************************************************************/
8
9#include <string.h>
10
11#include "formats/smx_dsk.h"
12#include "formats/basicdsk.h"
13#include "emu.h"
14
15static FLOPPY_IDENTIFY(smx_dsk_identify)
16{
17   *vote = (floppy_image_size(floppy) == 819200) ? 100 : 0;
18   return FLOPPY_ERROR_SUCCESS;
19}
20
21static int smx_get_heads_per_disk(floppy_image *floppy)
22{
23   return 2;
24}
25
26static int smx_get_tracks_per_disk(floppy_image *floppy)
27{
28   return 80;
29}
30
31static UINT64 smx_translate_offset(floppy_image *floppy,
32    int track, int head, int sector)
33{
34   return (track * 1024 * 5 * 2) + (head * 1024 * 5) + 1024 * sector;
35}
36
37static floperr_t get_offset(floppy_image *floppy, int head, int track, int sector, int sector_is_index, UINT64 *offset)
38{
39   UINT64 offs;
40   /* translate the sector to a raw sector */
41   if (!sector_is_index)
42   {
43      sector -= 1;
44   }
45   /* check to see if we are out of range */
46   if ((head < 0) || (head >= 2) || (track < 0) || (track >= 80)
47         || (sector < 0) || (sector >= 6))
48      return FLOPPY_ERROR_SEEKERROR;
49
50   offs = smx_translate_offset(floppy, track, head, sector);
51   if (offset)
52      *offset = offs;
53   return FLOPPY_ERROR_SUCCESS;
54}
55
56
57
58static floperr_t internal_smx_read_sector(floppy_image *floppy, int head, int track, int sector, int sector_is_index, void *buffer, size_t buflen)
59{
60   UINT64 offset;
61   floperr_t err;
62   err = get_offset(floppy, head, track, sector, sector_is_index, &offset);
63   if (err)
64      return err;
65
66   floppy_image_read(floppy, buffer, offset, buflen);
67   return FLOPPY_ERROR_SUCCESS;
68}
69
70
71
72static floperr_t internal_smx_write_sector(floppy_image *floppy, int head, int track, int sector, int sector_is_index, const void *buffer, size_t buflen, int ddam)
73{
74   UINT64 offset;
75   floperr_t err;
76
77   err = get_offset(floppy, head, track, sector, sector_is_index, &offset);
78   if (err)
79      return err;
80
81   floppy_image_write(floppy, buffer, offset, buflen);
82   return FLOPPY_ERROR_SUCCESS;
83}
84
85
86
87static floperr_t smx_read_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen)
88{
89   return internal_smx_read_sector(floppy, head, track, sector, FALSE, buffer, buflen);
90}
91
92static floperr_t smx_write_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam)
93{
94   return internal_smx_write_sector(floppy, head, track, sector, FALSE, buffer, buflen, ddam);
95}
96
97static floperr_t smx_read_indexed_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen)
98{
99   return internal_smx_read_sector(floppy, head, track, sector, TRUE, buffer, buflen);
100}
101
102static floperr_t smx_write_indexed_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam)
103{
104   return internal_smx_write_sector(floppy, head, track, sector, TRUE, buffer, buflen, ddam);
105}
106
107static floperr_t smx_get_sector_length(floppy_image *floppy, int head, int track, int sector, UINT32 *sector_length)
108{
109   floperr_t err;
110   err = get_offset(floppy, head, track, sector, FALSE, NULL);
111   if (err)
112      return err;
113
114   if (sector_length) {
115      *sector_length = 1024;
116   }
117   return FLOPPY_ERROR_SUCCESS;
118}
119
120
121
122static floperr_t smx_get_indexed_sector_info(floppy_image *floppy, int head, int track, int sector_index, int *cylinder, int *side, int *sector, UINT32 *sector_length, unsigned long *flags)
123{
124   sector_index += 1;
125   if (cylinder)
126      *cylinder = track;
127   if (side)
128      *side = head;
129   if (sector)
130      *sector = sector_index;
131   if (flags)
132      /* TODO: read DAM or DDAM and determine flags */
133      *flags = 0;
134   return smx_get_sector_length(floppy, head, track, sector_index, sector_length);
135}
136
137
138static FLOPPY_CONSTRUCT(smx_dsk_construct)
139{
140   struct FloppyCallbacks *callbacks;
141   callbacks = floppy_callbacks(floppy);
142   callbacks->read_sector = smx_read_sector;
143   callbacks->write_sector = smx_write_sector;
144   callbacks->read_indexed_sector = smx_read_indexed_sector;
145   callbacks->write_indexed_sector = smx_write_indexed_sector;
146   callbacks->get_sector_length = smx_get_sector_length;
147   callbacks->get_heads_per_disk = smx_get_heads_per_disk;
148   callbacks->get_tracks_per_disk = smx_get_tracks_per_disk;
149   callbacks->get_indexed_sector_info = smx_get_indexed_sector_info;
150
151   return FLOPPY_ERROR_SUCCESS;
152}
153
154
155
156/* ----------------------------------------------------------------------- */
157
158FLOPPY_OPTIONS_START( specimx )
159   FLOPPY_OPTION( smx_dsk, "odi", "Specialist MX floppy disk image",   smx_dsk_identify, smx_dsk_construct, NULL, NULL)
160FLOPPY_OPTIONS_END
src/mess/formats/smx_dsk.h
r11457r11458
1/*********************************************************************
2
3    formats/smx_dsk.h
4
5    SVI318 disk images
6
7*********************************************************************/
8
9#ifndef SMX_DSK_H
10#define SMX_DSK_H
11
12#include "imagedev/flopimg.h"
13
14/**************************************************************************/
15
16FLOPPY_OPTIONS_EXTERN(specimx);
17
18#endif /* SVI_DSK_H */
src/mess/formats/a26_cas.c
r11457r11458
1/*
2
3Atari 2600 SuperCharger support
4
5*/
6
7
8#include "formats/a26_cas.h"
9
10
11#define A26_CAS_SIZE         8448
12#define A26_WAV_FREQUENCY      44100
13#define BIT_ZERO_LENGTH         10
14#define BIT_ONE_LENGTH         15
15#define ZEROS_ONES            2755
16
17static const UINT16 one_wave[BIT_ONE_LENGTH] = {
18   0x2AE5, 0x4E60, 0x644E, 0x68E4, 0x5B56, 0x3DFE, 0x15ED, 0xEA13, 0xC202, 0xA4AA,
19   0x971C, 0x9BB2, 0xB1A0, 0xD51B, 0x0000
20};
21
22static const UINT16 zero_wave[BIT_ZERO_LENGTH] = {
23   0x3DFE, 0x644E, 0x644E, 0x3DFE, 0x0000, 0xC202, 0x9BB2, 0x9BB2, 0xC202, 0x0000
24};
25
26static int a26_cas_output_wave( INT16 **buffer, INT16 wave_data, int length ) {
27   int i;
28
29   if ( buffer ) {
30      for ( i = 0 ; i < length; i++ ) {
31         **buffer = wave_data;
32         *buffer = *buffer + 1;
33      }
34   }
35   return length;
36}
37
38static int a26_cas_output_bit( INT16 **buffer, int bit ) {
39   int size = 0;
40   int   bit_size = bit ? BIT_ONE_LENGTH : BIT_ZERO_LENGTH;
41   const INT16 *p = bit ? (const INT16 *)one_wave : (const INT16 *)zero_wave;
42   int i;
43
44   for ( i = 0; i < bit_size; i++ ) {
45      size += a26_cas_output_wave( buffer, p[i], 1 );
46   }
47
48   return size;
49}
50
51static int a26_cas_output_byte( INT16 **buffer, UINT8 byte ) {
52   int      size = 0;
53   int      i;
54
55   for( i = 0; i < 8; i++, byte <<= 1 ) {
56      size += a26_cas_output_bit( buffer, ( ( byte & 0x80 ) ? 1 : 0 ) );
57   }
58   return size;
59}
60
61static int a26_cas_clearing_tone( INT16 **buffer ) {
62   int      size = 0;
63
64   size += a26_cas_output_wave( buffer, 0, 44100 );
65   return size;
66}
67
68static int a26_cas_zeros_ones( INT16 **buffer ) {
69   int      size = 0;
70   int      i;
71
72   for ( i = 0; i < ZEROS_ONES; i++ ) {
73      size += a26_cas_output_bit( buffer, 0 );
74      size += a26_cas_output_bit( buffer, 1 );
75   }
76   size += a26_cas_output_bit( buffer, 0 );
77   size += a26_cas_output_bit( buffer, 0 );
78   return size;
79}
80
81static int a26_cas_output_contents( INT16 **buffer, const UINT8 *bytes ) {
82   int      size = 0;
83   int      i, pages, j;
84
85   /* There are 8 header bytes */
86   for( i = 0; i < 8; i++ ) {
87      size += a26_cas_output_byte( buffer, bytes[ 0x2000 + i ] );
88   }
89
90   pages = bytes[0x2003];
91
92   /* Output each page prefixed with a small page header */
93   for ( i = 0; i < pages; i++ ) {
94      size += a26_cas_output_byte( buffer, bytes[ 0x2010 + i ] );
95      size += a26_cas_output_byte( buffer, bytes[ 0x2040 + i ] );
96      for ( j = 0; j < 256; j++ ) {
97         size += a26_cas_output_byte( buffer, bytes[ i * 256 + j ] );
98      }
99   }
100
101   return size;
102}
103
104static int a26_cas_do_work( INT16 **buffer, const UINT8 *bytes ) {
105   int      size = 0;
106
107   /* Output clearing tone */
108   size += a26_cas_clearing_tone( buffer );
109
110   /* Output header tone, alternating 1s and 0s for about a second ending with two 0s */
111   size += a26_cas_zeros_ones( buffer );
112
113   /* Output the actual contents of the tape */
114   size += a26_cas_output_contents( buffer, bytes );
115
116   /* Output footer tone, alternating 1s and 0s for about a second ending with two 0s */
117   size += a26_cas_zeros_ones( buffer );
118
119   return size;
120}
121
122static int a26_cas_fill_wave( INT16 *buffer, int length, UINT8 *bytes ) {
123   INT16   *p = buffer;
124
125   return a26_cas_do_work( &p, (const UINT8 *)bytes );
126}
127
128static int a26_cas_to_wav_size( const UINT8 *casdata, int caslen ) {
129   return a26_cas_do_work( NULL, casdata );
130}
131
132static const struct CassetteLegacyWaveFiller a26_legacy_fill_wave = {
133   a26_cas_fill_wave,
134   -1,
135   0,
136   a26_cas_to_wav_size,
137   A26_WAV_FREQUENCY,
138   0,
139   0
140};
141
142static casserr_t a26_cassette_identify( cassette_image *cassette, struct CassetteOptions *opts ) {
143   UINT64 size;
144
145   size = cassette_image_size( cassette );
146   if ( size == A26_CAS_SIZE ) {
147      return cassette_legacy_identify( cassette, opts, &a26_legacy_fill_wave );
148   }
149   return CASSETTE_ERROR_INVALIDIMAGE;
150}
151
152static casserr_t a26_cassette_load( cassette_image *cassette ) {
153   return cassette_legacy_construct( cassette, &a26_legacy_fill_wave );
154}
155
156static const struct CassetteFormat a26_cassette_format = {
157   "a26",
158   a26_cassette_identify,
159   a26_cassette_load,
160   NULL
161};
162
163CASSETTE_FORMATLIST_START(a26_cassette_formats)
164   CASSETTE_FORMAT(a26_cassette_format)
165CASSETTE_FORMATLIST_END
166
src/mess/formats/a26_cas.h
r11457r11458
1/*
2    a26_cas.h
3
4    Format code for a2600 SuperCharger cassette files
5*/
6
7#ifndef A26_CAS_H
8#define A26_CAS_H
9
10#include "imagedev/cassimg.h"
11
12CASSETTE_FORMATLIST_EXTERN(a26_cassette_formats);
13
14#endif /* A26_CAS_H */
src/mess/formats/hect_tap.c
r11457r11458
1/********************************************************************
2
3Support for Micronique machine .K7 and *.FOR cassette images
4
5Note that the usual type for hector cassette is *.K7,
6     the *.FOR type is only for programming screen in forth format.
7
8You can find some *.K7 file on serveral server in France
9(Micronique is a French factory) Enjoy !
10
11 jj.stacino@aliceadsl.fr
12
13Updated 3/1/10 : use real value for timing.
14********************************************************************/
15#include "hect_tap.h"
16
17
18#define SMPLO   -32768
19#define SILENCE   0
20#define SMPHI   32767
21
22
23static int cas_size;
24
25
26enum
27{
28   Header_cycles = 77, /* Valeur Th?orique 66 = 44100 * 1.5 / 1000  // mesur? sur jeu Formule1 = 1,75ms*/
29   Zero_cycles =   27, /* Valeur Th?orique 17 = 44100 * 0.4 / 1000  // mesur? sur jeu Formule1 = 0,61ms*/
30   Un_cycles =     50, /* Valeur Th?orique 40 = 44100 * 0.9 / 1000  // mesur? sur jeu Formule1 = 1,13ms*/
31};
32
33/* Here I prefer use the value that I read on a real tape, and not the theorical value; note that these
34   value work best on my HRX...    Yo_fr   (jj.stacino@aliceadsl.fr)  */
35
36/*******************************************************************
37   Generate one high-low cycle of sample data
38********************************************************************/
39INLINE int hector_tap_cycle(INT16 *buffer, int sample_pos, int high, int low)
40{
41   int i = 0;
42
43   if ( buffer )
44   {
45      while( i < high)
46      {
47         buffer[ sample_pos + i ] = SMPHI;
48         i++;
49      }
50
51      while( i < high + low )
52      {
53         buffer[ sample_pos + i ] = SMPLO;
54         i++;
55      }
56   }
57   return high + low;
58}
59
60
61INLINE int hector_tap_byte(INT16 *buffer, int sample_pos, UINT8 data)
62{
63/* Writing an entire byte */
64   int i, samples;
65
66   samples = 0;
67   for ( i = 0; i < 8; i++ )
68   {
69      if ( data & 0x01 )
70         samples += hector_tap_cycle( buffer, sample_pos + samples, Un_cycles/2, Un_cycles/2 );
71      else
72         samples += hector_tap_cycle( buffer, sample_pos + samples, Zero_cycles/2, Zero_cycles/2 );
73
74      data >>= 1;
75   }
76   return samples;
77}
78
79
80INLINE int hector_tap_synchro(INT16 *buffer, int sample_pos, int nb_synchro)
81{
82/* Writing an entire byte */
83   int i, samples;
84
85   samples = 0;
86   for ( i = 0; i < nb_synchro ; i++ )
87         samples += hector_tap_cycle( buffer, sample_pos + samples, Header_cycles/2, Header_cycles/2 );
88
89   return samples;
90}
91
92
93static int hector_handle_tap(INT16 *buffer, const UINT8 *casdata)
94{
95   int   data_pos, sample_count/*, block_count*/;
96    int previous_block=0;
97
98   data_pos = 0;
99   sample_count = 0;
100    /*block_count = 0;*/
101    previous_block = 0;
102
103   /* First 768 cycle of synchro */
104   sample_count += hector_tap_synchro( buffer, sample_count, 768-4 );
105
106   /* on the entire file*/
107    while( data_pos < cas_size )
108   {
109      UINT16   block_size;
110
111        if (previous_block == 0xFE)
112               /* Starting a block with 150 cycle of synchro to let time to Hector to do the job ! */
113              sample_count += hector_tap_synchro( buffer, sample_count, 150 );
114        else
115                  /* Starting a block with 4 cycle of synchro */
116              sample_count += hector_tap_synchro( buffer, sample_count, 4 );
117
118       if (data_pos>1)
119                previous_block = casdata[data_pos-1];
120
121      /* Handle block lenght on tape data */
122      block_size = casdata[data_pos] ;
123      if (block_size==0)
124         block_size=256;
125
126      /*block_count++;*/
127        sample_count += hector_tap_byte(buffer, sample_count, casdata[data_pos] );
128        data_pos++;
129
130      /* Data samples */
131      for ( ; block_size ; data_pos++, block_size-- )
132        {
133          /* Make sure there are enough bytes left */
134          if ( data_pos > cas_size )
135            return -1;
136
137         sample_count += hector_tap_byte( buffer, sample_count, casdata[data_pos] );
138
139        }
140    }
141    /*Finish by a zero*/
142    sample_count += hector_tap_byte( buffer, sample_count, 0 );
143
144   return sample_count;
145}
146/*******************************************************************
147////  FORTH DATA CASSETTE
148*******************************************************************/
149
150
151static int hector_handle_forth_tap(INT16 *buffer, const UINT8 *casdata)
152{
153   int   data_pos, sample_count/*, block_count*/;
154    /*int previous_block=0;*/
155
156   data_pos = 0;
157   sample_count = 0;
158    /*block_count = 0;*/
159    /*previous_block = 0;*/
160
161    /* Out if len of file not modulo 822 octets    */
162   if ( (cas_size % 822) != 0 )
163      return -1;
164
165   /* on the entire file*/
166    while( data_pos < cas_size )
167   {
168      UINT16   block_size;
169
170       /* Starting a block with 768 cycle of synchro*/
171       sample_count += hector_tap_synchro( buffer, sample_count, 768 );
172
173      /* Handle block lenght on tape data */
174      block_size = 822 ; /* Fixed size for the forth*/
175
176      /*block_count=0;*/
177
178      /* Data samples */
179      for ( ; block_size ; data_pos++, block_size-- )
180        {
181          /* Make sure there are enough bytes left */
182          if ( data_pos > cas_size )
183            return -1;
184
185         sample_count += hector_tap_byte( buffer, sample_count, casdata[data_pos] );
186
187        }
188    }
189
190    /*Finish by a zero*/
191    sample_count += hector_tap_byte( buffer, sample_count, 0 );
192
193   return sample_count;
194}
195/*******************************************************************
196/////  END FORTH DATA CASSETTE
197*******************************************************************/
198
199
200/*******************************************************************
201   Generate samples for the tape image
202********************************************************************/
203static int hector_tap_fill_wave(INT16 *buffer, int sample_count, UINT8 *bytes)
204{
205   return hector_handle_tap( buffer, bytes );
206}
207
208
209/*******************************************************************
210   Calculate the number of samples needed for this tape image  FORTH
211********************************************************************/
212static int hector_tap_forth_to_wav_size(const UINT8 *casdata, int caslen)
213{
214   cas_size = caslen ;
215
216   return hector_handle_forth_tap( NULL, casdata );
217}
218
219/*******************************************************************
220   Generate samples for the tape image FORTH
221********************************************************************/
222static int hector_tap_forth_fill_wave(INT16 *buffer, int sample_count, UINT8 *bytes)
223{
224   return hector_handle_forth_tap( buffer, bytes ); //forth removed here !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
225}
226
227
228/*******************************************************************
229   Calculate the number of samples needed for this tape image classical
230********************************************************************/
231static int hector_tap_to_wav_size(const UINT8 *casdata, int caslen)
232{
233   cas_size = caslen ;
234
235   return hector_handle_tap( NULL, casdata );//forth removed here !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
236}
237
238
239static const struct CassetteLegacyWaveFiller hector_legacy_fill_wave =
240{
241   hector_tap_fill_wave,               /* fill_wave */
242   -1,                              /* chunk_size */
243   0,                              /* chunk_samples */
244   hector_tap_to_wav_size,                /* chunk_sample_calc */
245   44100,                           /* sample_frequency */
246   0,                              /* header_samples */
247   0                              /* trailer_samples */
248};
249
250static const struct CassetteLegacyWaveFiller hector_forth_legacy_fill_wave =
251{
252   hector_tap_forth_fill_wave,            /* fill_wave */
253   -1,                              /* chunk_size */
254   0,                              /* chunk_samples */
255   hector_tap_forth_to_wav_size,          /* chunk_sample_calc */
256   44100,                           /* sample_frequency */
257   0,                              /* header_samples */
258   0                              /* trailer_samples */
259};
260
261
262static casserr_t hector_k7_identify(cassette_image *cassette, struct CassetteOptions *opts)
263{
264   return cassette_legacy_identify(cassette, opts, &hector_legacy_fill_wave);
265}
266
267
268static casserr_t hector_k7_load(cassette_image *cassette)
269{
270   return cassette_legacy_construct(cassette, &hector_legacy_fill_wave);
271}
272
273static casserr_t hector_k7forth_identify(cassette_image *cassette, struct CassetteOptions *opts)
274{
275   return cassette_legacy_identify(cassette, opts, &hector_forth_legacy_fill_wave);
276}
277
278
279static casserr_t hector_k7forth_load(cassette_image *cassette)
280{
281   return cassette_legacy_construct(cassette, &hector_forth_legacy_fill_wave);
282}
283
284
285static const struct CassetteFormat hector_k7_format =
286{
287   "k7,cin",
288   hector_k7_identify,
289   hector_k7_load,
290   NULL
291};
292
293static const struct CassetteFormat hector_k7Forth_format =
294{
295   "for",
296   hector_k7forth_identify,
297   hector_k7forth_load,
298   NULL
299};
300
301
302CASSETTE_FORMATLIST_START(hector_cassette_formats)
303   CASSETTE_FORMAT(hector_k7_format)
304   CASSETTE_FORMAT(hector_k7Forth_format)
305CASSETTE_FORMATLIST_END
src/mess/formats/hect_tap.h
r11457r11458
1/*********************************************************************
2
3    hect_tap.h
4
5    Format code for Micronique cassette files
6
7*********************************************************************/
8
9#ifndef HECT_TAP_H
10#define HECT_TAP_H
11
12#include "imagedev/cassimg.h"
13
14CASSETTE_FORMATLIST_EXTERN(hector_cassette_formats);
15
16#endif /* HECT_TAP_H */
17
src/mess/formats/thom_cas.c
r11457r11458
1/**********************************************************************
2
3  Copyright (C) Antoine Mine' 2006
4
5  Thomson 8-bit computers
6
7**********************************************************************/
8
9#include <math.h>
10
11#include "emu.h"
12#include "imagedev/cassette.h"
13#include "imagedev/cassimg.h"
14#include "includes/thomson.h"
15#include "formats/thom_cas.h"
16
17
18/***************************** configuration **************************/
19
20#define K7_SPEED_HACK 0
21/* When set to 1, bits extracted from k7 files are not transformed into a
22   raw wave. It saves some time & memory but disables the ability to "hear" the
23   cassette.
24   This does not affect raw wave files.
25   Also, we do not do this for k5 files because the the corresponding wave
26   files are more compact anyway. */
27/* It must be set accordingly in machine/thomson.c */
28
29#define VERBOSE 0 /* 0, 1 or 2 */
30
31
32/***************************** utilities **************************/
33
34#define PRINT(x) mame_printf_info x
35
36#define LOG(x)   do { if (VERBOSE > 0) logerror x; } while (0)
37#define VLOG(x)   do { if (VERBOSE > 1) logerror x; } while (0)
38
39
40#define TO7_BIT_LENGTH 0.001114
41
42#define TO7_FREQ_CASS_0 4500.
43#define TO7_FREQ_CASS_1 6300.
44
45#define TO7_PERIOD_CASS_0 ( 1. / TO7_FREQ_CASS_0 )
46#define TO7_PERIOD_CASS_1 ( 1. / TO7_FREQ_CASS_1 )
47
48static UINT32 to7_k7_bitsize;
49static UINT8* to7_k7_bits;
50
51#define MO5_BIT_LENGTH   0.000833
52#define MO5_HBIT_LENGTH (MO5_BIT_LENGTH / 2.)
53
54/************************* k7 format *************************/
55
56/* The k7 format is widespread among existing Thomson emulators.
57   It represents the stream of bytes returned by the BIOS cassette routine,
58   and so, is used by emulators that bypass the BIOS routine.
59   It is generally hacked from the raw image more or less by hand
60   (in particular, to by-pass special loaders and copy-protection schemes).
61
62   As we run the original BIOS routine, we need to hack back the k7 file
63   into the original stream.
64
65   In theory, the byte stream is a sequence of blocks of the form:
66   - 2-byte header: 01 3C
67   - 1-byte type
68   00 = start of file, the block contains the filename
69   01 = chunk of file data
70   FF = end of file
71   - 1-byte size
72   - size bytes of data
73   - one byte of CRC
74
75   These are separated by 0xff fillers.
76
77   There are also a variety of non-standard blocks, and we try to handle them.
78
79   Our main problem is to guess where the places where the motor will be
80   cut-off and introduce there sequences of 1 bit for synchronization.
81
82   Note: as a nice effect, if our routine succeeds, it constructs a wave
83   file that should be usable on a real computer.
84*/
85
86
87
88static const struct CassetteModulation to7_k7_modulation =
89{
90   CASSETTE_MODULATION_SQUAREWAVE,
91   4000.0,  4500.0, 5000.0,
92   5500.0,  6300.0, 7500.0
93};
94
95
96
97static casserr_t to7_k7_identify ( cassette_image *cass, struct CassetteOptions *opts )
98{
99   casserr_t e = cassette_modulation_identify( cass, &to7_k7_modulation, opts );
100   return e;
101}
102
103
104
105static casserr_t to7_k7_load( cassette_image *cass )
106{
107#if ! K7_SPEED_HACK
108   static const INT8 square_wave[] = { -128, 127 };
109   double time = 0.;
110#endif
111   size_t size = cassette_image_size( cass ), pos = 0;
112   int i, sz, sz2, bitmax = 1024, invalid = 0;
113   UINT8 in, typ, block[264];
114
115   LOG (( "to7_k7_load: start conversion, size=%li\n", (long)size ));
116   PRINT (( "to7_k7_load: open cassette, length: %li bytes\n", (long) size ));
117
118   if ( to7_k7_bits )
119   {
120      free( to7_k7_bits );
121      to7_k7_bits = NULL;
122   }
123
124   to7_k7_bitsize = 0;
125   to7_k7_bits = global_alloc_array_clear(UINT8, bitmax );
126
127/* store one period */
128#if K7_SPEED_HACK
129#define K7_PUT( PERIOD )
130#else
131#define K7_PUT( PERIOD ) \
132   do                        \
133   {                        \
134      casserr_t err;                  \
135      err = cassette_put_samples( cass, 0, time, (PERIOD), 2, 1, \
136                   square_wave, CASSETTE_WAVEFORM_8BIT ); \
137      if ( err )                  \
138         return err;               \
139      time += (PERIOD);               \
140   } while (0)
141#endif
142
143/* store one bit */
144#define K7_PUT_BIT( BIT ) \
145   do                        \
146   {                        \
147      int b;                     \
148      if ( BIT )                  \
149      {                     \
150         for ( b = 0; b < 7; b++ )         \
151            K7_PUT( TO7_PERIOD_CASS_1 );      \
152      }                     \
153      else                     \
154      {                     \
155         for ( b = 0; b < 5; b++ )         \
156            K7_PUT( TO7_PERIOD_CASS_0 );      \
157      }                     \
158      if ( to7_k7_bitsize + 1 >= bitmax )         \
159      {                     \
160         UINT8* a = global_alloc_array_clear(UINT8, bitmax * 2);      \
161         memcpy ( a, to7_k7_bits, bitmax );      \
162         bitmax *= 2;               \
163         to7_k7_bits = a;            \
164      }                     \
165      to7_k7_bits[ to7_k7_bitsize++ ] = (BIT);      \
166   } while (0)
167
168/* store one byte, with start / stop bits */
169#define K7_PUT_BYTE( BYTE ) \
170   do                     \
171   {                     \
172      UINT8 x;               \
173      K7_PUT_BIT( 0 );            \
174      for ( x = 0; x < 8; x++ )         \
175         K7_PUT_BIT( ( (BYTE) >> x ) & 1 );   \
176      K7_PUT_BIT( 1 );            \
177      K7_PUT_BIT( 1 );            \
178   } while (0)
179
180#define K7_FILL_1( SIZE ) \
181   do                        \
182   {                        \
183      if ( (SIZE) > 0 ) {               \
184         int ii;                  \
185         LOG (( "to7_k7_load: 1-filler size=%i bitstart=%i\n", \
186                (SIZE), to7_k7_bitsize ));      \
187         for ( ii = 0; ii < (SIZE); ii++ ) K7_PUT_BIT( 1 ); \
188      }                     \
189   } while (0)
190
191#define K7_FILL_ff( SIZE ) \
192   do                        \
193   {                        \
194      if ( (SIZE) > 0 )               \
195      {                     \
196         int ii;                  \
197         LOG (( "to7_k7_load: 0xff filler size=%i bitstart=%i\n",  (SIZE), to7_k7_bitsize )); \
198         for ( ii = 0; ii < (SIZE); ii++ )      \
199            K7_PUT_BYTE( 0xff );         \
200      }                     \
201   } while (0)
202
203   /* check format */
204   cassette_image_read( cass, block, 0, 64 );
205   for ( i = 3; ; i++ )
206   {
207      if ( ( i >= size ) || ( i >= 64 ) )
208      {
209         /* ? */
210         PRINT (( "to7_k7_load: WARNING: this does not look like a MO or TO cassette.\n" ));
211         break;
212      }
213      else if ( ( block[i-3] == 0x01 ) && ( block[i-2] == 0x3c ) && ( block[i-1] == 0x5a ) && ! block[i] )
214      {
215         /* MO */
216         PRINT (( "to7_k7_load: WARNING: this looks like a MO cassette, not a TO one.\n" ));
217         break;
218      }
219      else if ( ( block[i-3] == 0xff ) && ( block[i-2] == 0x01 ) && ( block[i-1] == 0x3c ) && ! block[i] )
220      {
221         /* TO */
222         break;
223      }
224   }
225
226   /* skip to first 0xff filler */
227   for ( sz = 0; pos < size; pos++, sz++ )
228   {
229      cassette_image_read( cass, &in, pos, 1 );
230      if ( in == 0xff )
231         break;
232   }
233   if ( sz > 0 )
234      LOG (( "to7_k7_load: skip %i trash bytes\n", sz ));
235
236   /* loop over regular blocks */
237   while ( pos < size )
238   {
239
240   rebounce:
241      /* skip 0xff filler */
242      for ( sz = 0; pos < size; pos++, sz++ )
243      {
244         cassette_image_read( cass, &in, pos, 1 );
245         /* actually, we are bit laxist and treat as 0xff bytes with at least
246               5 bits out of 8 set to 1
247            */
248         for ( i = 0; in; in >>= 1 )
249            i += (in & 1);
250         if ( i < 5 )
251            break;
252      }
253
254      /* get block header */
255      if ( pos + 4 > size )
256      {
257         pos -= sz;
258         break;
259      }
260      cassette_image_read( cass, block, pos, 4 );
261      typ = block[2];
262      sz2 = block[3]+1;
263      if ( block[0] != 0x01 || block[1] != 0x3c || ( typ != 0x00 && typ != 0x01 && typ !=  0xff ) )
264      {
265         pos -= sz;
266         break;
267      }
268      pos += 4;
269
270      /* get block */
271      cassette_image_read( cass, block+4, pos, sz2 );
272      pos += sz2;
273
274      /* 1-filler and 0xff-filler */
275      if ( typ == 0 || typ == 0xff )
276         K7_FILL_1( 1000 );
277      K7_FILL_ff( sz );
278
279      /* put block */
280      LOG (( "to7_k7_load: block off=$%x type=$%02X size=%i bitstart=%i\n", (int) pos-sz2-4, typ, sz2, to7_k7_bitsize ));
281      VLOG (( "to7_k7_load: data:" ));
282      for ( i = 0; i < sz2 + 4; i ++)
283      {
284         VLOG (( " $%02X", block[i] ));
285         K7_PUT_BYTE( block[i] );
286      }
287      VLOG (( "\n" ));
288
289      /* if it is a directory enty, says so to the user */
290      if ( typ == 0 )
291      {
292         char name[] = "01234567.ABC";
293         UINT8 t = block[15];
294         UINT8 u = block[16];
295         int p = (to7_k7_bitsize - sz2 - 4 - sz) * TO7_BIT_LENGTH;
296         memcpy( name, block+4, 8 );
297         memcpy( name+9, block+12, 3 );
298         for ( i = 0; name[i]; i++)
299         {
300            if ( name[i] < ' ' || name[i] >= 127 )
301               name[i] = '?';
302         }
303         PRINT (( "to7_k7_load: file \"%s\" type=%s,%s at %imn %is\n",
304             name,
305             (t==0) ? "bas" : (t==1) ? "dat" : (t==2) ? "bin" : "???",
306             (u == 0) ? "a" : (u == 0xff) ? "b" : "?",
307             p / 60, p % 60 ));
308      }
309
310      /* extra 1-fillers */
311      if ( typ == 0 || typ == 0xff )
312         K7_FILL_1( 1000 );
313   }
314
315   /* trailing data with invalid block structure
316       => dump it in a raw form, but stay alert for hidden block starts
317    */
318   if ( pos < size )
319   {
320      invalid++;
321      LOG (( "to7_k7_load: trailing raw bytes off=$%x bitstart=%i\n", (int) pos, to7_k7_bitsize ));
322
323      /* put block */
324      for (; pos < size; pos++ )
325      {
326         cassette_image_read( cass, &in, pos, 1 );
327         for ( sz = 0; pos < size && in == 0xff; sz++ )
328         {
329            pos++;
330            cassette_image_read( cass, &in, pos, 1 );
331         }
332         if ( invalid < 10 && sz > 4 && in == 0x01 && pos + 4 <= size )
333         {
334            UINT8 in1,in2;
335            cassette_image_read( cass, &in1,   pos+1, 1 );
336            cassette_image_read( cass, &in2, pos+2, 1 );
337            if ( (in1 == 0x3c) && ((in2 == 0x00) || (in2 == 0x01) ) )
338            {
339               /* seems we have a regular block hidden in raw data => rebounce */
340               LOG (( "to7_k7_load: hidden regular block found\n" ));
341               pos -= sz;
342               goto rebounce;
343            }
344            if ( ( ( in1 == 0x3d ) && ( in2 == 0 ) ) || ( ( in1 == 0x57 ) && ( in2 == 0x49 ) ) )
345            {
346               /* special block (Infogrames) => just prepend filler */
347               K7_FILL_1 ( 500 );
348               LOG (( "to7_k7_load: special $%02X $%02X $%02X block found off=$%x bitstart=%i\n", in, in1, in2, (int) pos, to7_k7_bitsize ));
349            }
350         }
351         for ( i = 0; i < sz; i++ )
352            K7_PUT_BYTE( 0xff );
353         K7_PUT_BYTE( in );
354      }
355   }
356
357   if ( invalid )
358      PRINT (( "to7_k7_load: WARNING: the k7 has an unknown structure and may not work\n" ));
359
360   sz = to7_k7_bitsize * TO7_BIT_LENGTH;
361   PRINT (( "to7_k7_load: cassette length: %imn %is (%i samples)\n", sz / 60, sz % 60, to7_k7_bitsize ));
362
363   return CASSETTE_ERROR_SUCCESS;
364}
365
366
367
368static const struct CassetteFormat to7_k7 =
369{ "k7", to7_k7_identify, to7_k7_load, NULL /* no save */ };
370
371
372
373/********************* TO7 WAV format ************************/
374
375
376
377static casserr_t to7_wav_identify ( cassette_image *cass,
378                struct CassetteOptions *opts )
379{
380   casserr_t e = wavfile_format.identify( cass, opts );
381   return e;
382}
383
384
385
386static casserr_t to7_wav_load ( cassette_image *cass )
387{
388   casserr_t e = wavfile_format.load( cass );
389   struct CassetteInfo info;
390   double len;
391
392   if ( to7_k7_bits )
393   {
394      free( to7_k7_bits );
395      to7_k7_bits = NULL;
396   }
397
398   if ( e != CASSETTE_ERROR_SUCCESS )
399      return e;
400
401   cassette_get_info( cass, &info );
402
403   len = (double) info.sample_count / info.sample_frequency;
404
405   PRINT (( "to7_wav_load: loading cassette, length %imn %is, %i Hz, %i bps, %i bits\n",
406       (int) len / 60, (int) len % 60,
407       info.sample_frequency, info.bits_per_sample,
408       (int) (len / TO7_BIT_LENGTH) ));
409
410   return CASSETTE_ERROR_SUCCESS;
411}
412
413
414
415static casserr_t to7_wav_save ( cassette_image *cass, const struct CassetteInfo *info )
416{
417   int len = info->sample_count / info->sample_frequency;
418   PRINT (( "to7_wav_save: saving cassette, length %imn %is, %i Hz, %i bps\n", len / 60, len % 60, info->sample_frequency, info->bits_per_sample ));
419   return wavfile_format.save( cass, info );
420}
421
422
423
424/* overloaded wav: dump info */
425static const struct CassetteFormat to7_wav =
426{ "wav", to7_wav_identify, to7_wav_load, to7_wav_save };
427
428
429
430/************************* k5 format *************************/
431
432
433
434static casserr_t mo5_k5_identify ( cassette_image *cass,
435               struct CassetteOptions *opts )
436{
437   opts -> bits_per_sample = 8;
438   opts -> channels = 1;
439   opts -> sample_frequency = 22100;//11050;
440   return CASSETTE_ERROR_SUCCESS;
441}
442
443
444
445/* As the k7 format, the k5 format represents the stream of bytes returned by
446   the BIOS cassette routine and need to be hacked back into the original
447   cassette wave.
448
449   The format of blocks is a bit different from that of TO7 cassettes:
450   - 2-byte header is: 3C 5A
451   - 1-byte type
452   00 = start of file, the block contains the filename
453   01 = chunk of file data
454   FF = end of file
455   - 1-byte size
456   - size - 1 bytes of data (255 bytes of data if size = 0)
457   - one byte of CRC: sum from (and including) header up to (and including)
458   CRC byte should be 0 modulo 256
459
460   They are separated by 0x01 filler bytes (generally 10 of them),
461   for synchronisation. There are also extra 0 filler bits between certain
462   kinds of blocks, when the motor is supposed to go off and back on.
463*/
464
465static casserr_t mo5_k5_load( cassette_image *cass )
466{
467   size_t size = cassette_image_size( cass ), pos = 0;
468   int i, sz, sz2, hbit = 0;
469   UINT8 in, in2, in3, typ, block[264], sum;
470   int invalid = 0, hbitsize = 0, dcmoto = 0;
471
472   LOG (( "mo5_k5_load: start conversion, size=%li\n", (long)size ));
473   PRINT (( "mo5_k5_load: open cassette, length: %li bytes\n", (long) size ));
474
475   /* store a half-bit */
476#define K5_PUT_HBIT                     \
477   do                        \
478   {                        \
479      cassette_put_sample ( cass, 0, hbitsize * MO5_HBIT_LENGTH, MO5_HBIT_LENGTH, (hbit ? 1 : -1) << 30 ); \
480      hbitsize++;                  \
481   } while ( 0 )
482
483   /* store one bit */
484#define K5_PUT_BIT( BIT )         \
485   do               \
486   {               \
487      if ( BIT )         \
488      {            \
489         K5_PUT_HBIT;      \
490         hbit = !hbit;      \
491         K5_PUT_HBIT;      \
492      }            \
493      else            \
494      {            \
495         K5_PUT_HBIT;      \
496         K5_PUT_HBIT;      \
497      }            \
498      hbit = !hbit;         \
499   } while (0)
500
501   /* store one byte, no start / stop bit, converse order from TO7 */
502#define K5_PUT_BYTE( BYTE ) \
503   do                     \
504   {                     \
505      UINT8 b = BYTE;               \
506      int x;                  \
507      for ( x = 0; x < 8; x++ )         \
508         K5_PUT_BIT( (b >> (7 - x)) & 1 );   \
509   } while (0)
510
511   /* store filler */
512#define K5_FILL_0( SIZE ) \
513   do                        \
514   {                        \
515      if ( (SIZE) > 0 )               \
516      {                     \
517         int j;                  \
518         LOG (( "mo5_k5_load: 0-filler size=%i hbitstart=%i\n", (SIZE), hbitsize )); \
519         for ( j = 0; j < (SIZE); j++ )         \
520            K5_PUT_BIT( 0 );         \
521      }                     \
522   } while (0)
523
524#define K5_FILL_01( SIZE )                  \
525   do                        \
526   {                        \
527      if ( (SIZE) > 0 )               \
528      {                     \
529         int j;                  \
530         LOG (( "mo5_k5_load: 0x01 filler size=%i bitstart=%i\n", (SIZE), hbitsize )); \
531         for ( j = 0; j < (SIZE); j++ )         \
532            K5_PUT_BYTE( 0x01 );         \
533      }                     \
534   } while (0)
535
536   /* check format */
537   cassette_image_read( cass, block, 0, 64 );
538   for ( i = 3; ; i++ )
539   {
540      if ( ( i >= size ) || ( i >= 64 ) )
541      {
542         /* ? */
543         PRINT (( "to5_k5_load: WARNING: this does not look like a MO or TO cassette.\n" ));
544         break;
545      }
546      else if ( ( block[i-3] == 0x01 ) && ( block[i-2] == 0x3c ) && ( block[i-1] == 0x5a ) && ! block[i] )
547      {
548         /* MO */
549         break;
550      }
551      else if ( ( block[i-3] == 0xff ) && ( block[i-2] == 0x01 ) && ( block[i-1] == 0x3c ) && ! block[i] )
552      {
553         /* TO */
554         PRINT (( "to5_k5_load: WARNING: this looks like a TO cassette, not a MO one.\n" ));
555         break;
556      }
557   }
558
559   cassette_image_read( cass, block, pos, 6 );
560   if ( ! memcmp( block, "DCMOTO", 6 ) || ! memcmp( block, "DCMO5", 5 ) || ! memcmp( block, "DCMO6", 5 ) )
561      dcmoto = 1;
562
563   /* loop over regular blocks */
564   while ( pos < size )
565   {
566
567   rebounce:
568      /* skip DCMOTO header*/
569      if ( dcmoto )
570      {
571         cassette_image_read( cass, block, pos, 6 );
572         if ( ! memcmp( block, "DCMOTO", 6 ) )
573         {
574            LOG (( "mo5_k5_load: DCMOTO signature found at off=$%x\n", (int)pos ));
575            pos += 6;
576         }
577         else if ( ! memcmp( block, "DCMO", 4 ) )
578         {
579            LOG (( "mo5_k5_load: DCMO* signature found at off=$%x\n", (int)pos ));
580            pos += 5;
581         }
582      }
583
584      /* skip 0x01 filler */
585      for ( sz = 0; pos < size; pos++, sz++ )
586      {
587         cassette_image_read( cass, &in, pos, 1 );
588         if ( in != 0x01 )
589            break;
590      }
591
592      /* get block header */
593      if ( pos + 4 > size )
594      {
595         pos -= sz;
596         break;
597      }
598      cassette_image_read( cass, block, pos, 4 );
599      typ = block[2];
600      sz2 = (UINT8) (block[3]-1);
601      if ( block[0] != 0x3c || block[1] != 0x5a || ( typ != 0x00 && typ != 0x01 && typ !=  0xff ) || pos+sz2 > size )
602      {
603         pos -= sz;
604         break;
605      }
606      pos += 4;
607
608      /* get block */
609      cassette_image_read( cass, block+4, pos, sz2 );
610      pos += sz2;
611
612      /* 0-fillers and 0x01-fillers */
613      if ( typ == 0 || typ == 0xff )
614         K5_FILL_0( 1200 );
615      else
616         K5_FILL_0( 300 ); /* for MO6 */
617      K5_FILL_01( sz < 10 ? 10 : sz );
618
619      /* put block */
620      LOG (( "mo5_k5_load: block off=$%x type=$%02X size=%i hbitstart=%i\n", (int) pos-sz2-4, typ, sz2, hbitsize ));
621      VLOG (( "mo5_k5_load: data:" ));
622      for ( i = 0; i < sz2 + 4; i ++)
623      {
624         VLOG (( " $%02X", block[i] ));
625         K5_PUT_BYTE( block[i] );
626      }
627      VLOG (( "\n" ));
628
629      /* checksum */
630      for ( i = 0, sum = 0; i < sz2; i++ )
631         sum += block[i+4];
632      if ( sum )
633         LOG(( "mo5_k5_load: invalid checksum $%02X (should be 0)\n", sum ));
634
635      /* if it is a directory enty, says so to the user */
636      if ( typ == 0 )
637      {
638         char name[] = "01234567.ABC";
639         UINT8 t = block[15];
640         UINT8 u = block[16];
641         int p = (hbitsize - sz2 - 4 - sz) * MO5_HBIT_LENGTH;
642         memcpy( name, block+4, 8 );
643         memcpy( name+9, block+12, 3 );
644         for ( i = 0; name[i]; i++)
645         {
646            if ( name[i] < ' ' || name[i] >= 127 )
647               name[i] = '?';
648         }
649         PRINT (( "mo5_k5_load: file \"%s\" type=%s,%s at %imn %is\n",
650             name,
651             (t==0) ? "bas" : (t==1) ? "dat" : (t==2) ? "bin" : "???",
652             (u == 0) ? "a" : (u == 0xff) ? "b" : "?",
653             p / 60, p % 60 ));
654      }
655
656      /* 0-fillers */
657      if ( typ == 0xff || typ == 0x00 )
658         K5_FILL_0( 1800 );
659   }
660
661   /* dump trailing bytes, but also look for beginings of blocks */
662   if ( pos < size )
663   {
664      invalid++;
665      K5_FILL_0( 1200 );
666      LOG (( "mo5_k5_load: trailing trash off=$%x size=%i hbitstart=%i\n", (int) pos, (int) (size-pos), hbitsize ));
667      for ( ; pos < size; pos++ )
668      {
669         cassette_image_read( cass, &in, pos, 1 );
670         if ( dcmoto && in=='D' )
671         {
672            /* skip DCMOTO header*/
673            cassette_image_read( cass, block, pos, 6 );
674            if ( ! memcmp( block, "DCMOTO", 6 ) )
675            {
676               LOG (( "mo5_k5_load: DCMOTO signature found at off=$%x\n", (int)pos ));
677               pos += 6;
678               cassette_image_read( cass, &in, pos, 1 );
679            }
680            else if ( ! memcmp( block, "DCMO", 4 ) )
681            {
682               LOG (( "mo5_k5_load: DCMO* signature found at off=$%x\n", (int)pos ));
683               pos += 5;
684               cassette_image_read( cass, &in, pos, 1 );
685            }
686         }
687         for ( sz = 0; pos < size && in == 0x01; sz++ )
688         {
689            pos++;
690            cassette_image_read( cass, &in, pos, 1 );
691         }
692         if ( sz > 6 )
693         {
694            cassette_image_read( cass, &in2, pos+1, 1 );
695            cassette_image_read( cass, &in3, pos+2, 1 );
696            if ( invalid < 10 &&  in == 0x3c && in2 == 0x5a && (in3 == 0x00 || in3 == 0x01 || in3 == 0xff ) )
697            {
698               /* regular block found */
699               LOG (( "mo5_k5_load: hidden regular block found\n" ));
700               pos -= sz;
701               goto rebounce;
702            }
703            if ( invalid < 10 && sz > 6 && ( (in == 0x3c && in2 == 0x5a) || (in == 0xc3 && in2 == 0x5a) || (in == 0xc3 && in2 == 0x3c) || (in == 0x87 && in2 == 0x4a)  ) )
704            {
705               /* special block found */
706               K5_FILL_0( 1200 );
707               LOG (( "mo5_k5_load: special block $%02X $%02X found off=$%x hbitstart=%i\n", in, in2, (int) pos-sz, hbitsize ));
708            }
709         }
710
711         VLOG (( "mo5_k5_load: special data:" ));
712         for ( i = 0; i < sz; i++ )
713         {
714            K5_PUT_BYTE( 0x01 );
715            VLOG (( " $01" ));
716         }
717         K5_PUT_BYTE( in );
718         VLOG (( " $%02X\n", in ));
719      }
720   }
721
722   if ( invalid )
723      PRINT (( "mo5_k5_load: WARNING: the k5 has an unknown structure and may not work\n" ));
724
725   sz = hbitsize * MO5_HBIT_LENGTH;
726   PRINT (( "mo5_k5_load: cassette length: %imn %is (%i half-bits)\n", sz / 60, sz % 60, hbitsize ));
727
728   return CASSETTE_ERROR_SUCCESS;
729}
730
731
732
733static const struct CassetteFormat mo5_k5 =
734{ "k5,k7", mo5_k5_identify, mo5_k5_load, NULL /* no save */ };
735
736
737/********************* MO5 WAV format ************************/
738
739
740
741static casserr_t mo5_wav_identify ( cassette_image *cass,
742              struct CassetteOptions *opts )
743{
744   casserr_t e = wavfile_format.identify( cass, opts );
745   return e;
746}
747
748
749
750static casserr_t mo5_wav_load ( cassette_image *cass )
751{
752   casserr_t e = wavfile_format.load( cass );
753   struct CassetteInfo info;
754   int len;
755   if ( e == CASSETTE_ERROR_SUCCESS )
756   {
757      cassette_get_info( cass, &info );
758      len = info.sample_count / info.sample_frequency;
759      PRINT (( "mo5_wav_load: loading cassette, length %imn %is, %i Hz, %i bps\n", len / 60, len % 60, info.sample_frequency, info.bits_per_sample ));
760   }
761   return e;
762}
763
764
765
766static casserr_t mo5_wav_save ( cassette_image *cass, const struct CassetteInfo *info )
767{
768   int len = info->sample_count / info->sample_frequency;
769   PRINT (( "mo5_wav_save: saving cassette, length %imn %is, %i Hz, %i bps\n", len / 60, len % 60, info->sample_frequency, info->bits_per_sample ));
770   return wavfile_format.save( cass, info );
771}
772
773
774
775/* overloaded wav: dump info */
776static const struct CassetteFormat mo5_wav =
777{ "wav", mo5_wav_identify, mo5_wav_load, mo5_wav_save };
778
779
780
781/********************* formats ************************/
782
783
784static const struct CassetteFormat *const to7_cassette_formats[] =
785{ &to7_wav, &to7_k7, NULL };
786
787
788static const struct CassetteFormat *const mo5_cassette_formats[] =
789{ &mo5_wav, &mo5_k5, NULL };
790
791
792
793/********************* devices ************************/
794
795const cassette_config to7_cassette_config =
796{
797   to7_cassette_formats,
798   NULL,
799   (cassette_state)(CASSETTE_PLAY | CASSETTE_MOTOR_DISABLED | CASSETTE_SPEAKER_ENABLED),
800   NULL
801};
802
803const cassette_config mo5_cassette_config =
804{
805   mo5_cassette_formats,
806   NULL,
807   (cassette_state)(CASSETTE_PLAY | CASSETTE_MOTOR_DISABLED | CASSETTE_SPEAKER_ENABLED),
808   NULL
809};
810
src/mess/formats/atom_atm.c
r11457r11458
77*********************************************************************/
88
99#include "emu.h"
10#include "imageutl.h"
10#include "formats/imageutl.h"
1111#include "formats/atom_atm.h"
1212
1313/***************************************************************************
src/mess/formats/comx35_comx.c
r11457r11458
77*********************************************************************/
88
99#include "emu.h"
10#include "imageutl.h"
10#include "formats/imageutl.h"
1111#include "formats/comx35_comx.h"
1212#include "machine/ram.h"
1313
src/mess/drivers/mz2000.c
r11457r11458
2626
2727#include "imagedev/cassette.h"
2828#include "imagedev/flopdrv.h"
29#include "imagedev/flopimg.h"
29#include "formats/flopimg.h"
3030#include "formats/basicdsk.h"
3131
3232
src/mess/drivers/x1.c
r11457r11458
222222#include "machine/wd17xx.h"
223223#include "imagedev/cassette.h"
224224#include "imagedev/flopdrv.h"
225#include "imagedev/flopimg.h"
225#include "formats/flopimg.h"
226226#include "formats/basicdsk.h"
227227#include "formats/x1_tap.h"
228228#include "imagedev/cartslot.h"
src/mess/drivers/thomson.c
r11457r11458
597597   NULL
598598};
599599
600
601/********************* devices ************************/
602const cassette_config to7_cassette_config =
603{
604   to7_cassette_formats,
605   NULL,
606   (cassette_state)(CASSETTE_PLAY | CASSETTE_MOTOR_DISABLED | CASSETTE_SPEAKER_ENABLED),
607   NULL
608};
609
610const cassette_config mo5_cassette_config =
611{
612   mo5_cassette_formats,
613   NULL,
614   (cassette_state)(CASSETTE_PLAY | CASSETTE_MOTOR_DISABLED | CASSETTE_SPEAKER_ENABLED),
615   NULL
616};
617
600618/* ------------ driver ------------ */
601619
602620static MACHINE_CONFIG_START( to7, driver_device )
src/mess/drivers/mz2500.c
r11457r11458
5454
5555//#include "imagedev/cassette.h"
5656#include "imagedev/flopdrv.h"
57#include "imagedev/flopimg.h"
57#include "formats/flopimg.h"
5858#include "formats/basicdsk.h"
5959
6060#define RP5C15_TAG      "rp5c15"
src/mess/drivers/vtech1.c
r11457r11458
112112******************************************************************************/
113113
114114#include "emu.h"
115#include "imageutl.h"
115#include "formats/imageutl.h"
116116#include "cpu/z80/z80.h"
117117#include "video/m6847.h"
118118#include "machine/ctronics.h"
src/mess/drivers/mbc55x.c
r11457r11458
1212#include "cpu/i86/i86.h"
1313#include "imagedev/flopdrv.h"
1414#include "machine/ram.h"
15#include "imagedev/flopimg.h"
15#include "formats/flopimg.h"
1616#include "formats/pc_dsk.h"
1717#include "includes/mbc55x.h"
1818#include "machine/ctronics.h"
src/mess/drivers/rmnimbus.c
r11457r11458
1414#include "imagedev/flopdrv.h"
1515#include "imagedev/harddriv.h"
1616#include "machine/ram.h"
17#include "imagedev/flopimg.h"
17#include "formats/flopimg.h"
1818#include "formats/pc_dsk.h"
1919#include "includes/rmnimbus.h"
2020#include "machine/er59256.h"
src/mess/mess.mak
r11457r11458
502502   $(MAME_DRIVERS)/dc.o       \
503503   $(MAME_MACHINE)/dccons.o    \
504504   $(MAME_MACHINE)/gdrom.o    \
505   $(MAME_FORMATS)/basicdsk.o    \
506505
507506#-------------------------------------------------
508507# the following files are general components and
r11457r11458
512511$(MESSOBJ)/shared.a: \
513512   $(MESS_AUDIO)/mea8000.o      \
514513   $(MESS_AUDIO)/spchroms.o   \
515   $(MESS_FORMATS)/imd_dsk.o   \
516   $(MESS_FORMATS)/ami_dsk.o   \
517   $(MESS_FORMATS)/td0_dsk.o   \
518   $(MESS_FORMATS)/cqm_dsk.o   \
519   $(MESS_FORMATS)/dsk_dsk.o   \
520   $(MESS_FORMATS)/d88_dsk.o   \
521   $(MESS_FORMATS)/fdi_dsk.o   \
522514   $(MESS_DEVICES)/microdrv.o   \
523   $(MESS_FORMATS)/coco_cas.o   \
524   $(MESS_FORMATS)/coco_dsk.o   \
525   $(MESS_FORMATS)/pc_dsk.o   \
526   $(MESS_FORMATS)/rk_cas.o   \
527515   $(MESS_MACHINE)/6551.o      \
528516   $(MESS_MACHINE)/74145.o      \
529517   $(MESS_MACHINE)/8530scc.o   \
r11457r11458
570558   $(MESS_MACHINE)/applefdc.o   \
571559   $(MESS_DEVICES)/sonydriv.o   \
572560   $(MESS_DEVICES)/appldriv.o   \
573   $(MESS_FORMATS)/ap2_dsk.o   \
574   $(MESS_FORMATS)/ap_dsk35.o   \
575   $(MESS_FORMATS)/hect_tap.o   \
576   $(MESS_FORMATS)/ti99_dsk.o   \
577   $(MESS_FORMATS)/msx_dsk.o   \
578561
579562
580563
r11457r11458
589572   $(MESS_DRIVERS)/acrnsys1.o   \
590573   $(MESS_DRIVERS)/atom.o      \
591574   $(MESS_FORMATS)/atom_atm.o   \
592   $(MESS_FORMATS)/atom_tap.o   \
593575   $(MESS_VIDEO)/bbc.o         \
594576   $(MESS_MACHINE)/bbc.o      \
595577   $(MESS_DRIVERS)/bbc.o      \
596578   $(MESS_DRIVERS)/bbcbc.o      \
597   $(MESS_FORMATS)/csw_cas.o   \
598   $(MESS_FORMATS)/uef_cas.o   \
599579   $(MESS_DRIVERS)/e01.o      \
600580   $(MESS_VIDEO)/electron.o   \
601581   $(MESS_MACHINE)/electron.o   \
r11457r11458
606586$(MESSOBJ)/act.a:            \
607587   $(MESS_DRIVERS)/apricot.o   \
608588   $(MESS_DRIVERS)/apricotf.o   \
609   $(MESS_FORMATS)/apridisk.o   \
610589   $(MESS_DRIVERS)/victor9k.o   \
611590
612591$(MESSOBJ)/amiga.a:            \
r11457r11458
633612
634613$(MESSOBJ)/apf.a:            \
635614   $(MESS_DRIVERS)/apf.o      \
636   $(MESS_FORMATS)/apf_apt.o   \
637615
638616$(MESSOBJ)/apple.a:            \
639617   $(MESS_VIDEO)/apple2.o      \
r11457r11458
671649   $(MESS_VIDEO)/arcadia.o      \
672650
673651$(MESSOBJ)/ascii.a:            \
674   $(MESS_FORMATS)/fmsx_cas.o   \
675652   $(MESS_DRIVERS)/msx.o      \
676653   $(MESS_MACHINE)/msx_slot.o   \
677654   $(MESS_MACHINE)/msx.o      \
r11457r11458
692669   $(MESS_VIDEO)/a7800.o      \
693670   $(MESS_DRIVERS)/a2600.o      \
694671   $(MAME_DRIVERS)/jaguar.o   \
695   $(MESS_FORMATS)/a26_cas.o   \
696   $(MESS_FORMATS)/atarist_dsk.o   \
697672   $(MESS_DRIVERS)/atarist.o   \
698673   $(MESS_VIDEO)/atarist.o    \
699674   $(MESS_DRIVERS)/lynx.o      \
r11457r11458
742717$(MESSOBJ)/cantab.a:         \
743718   $(MESS_DRIVERS)/ace.o      \
744719   $(MESS_FORMATS)/ace_ace.o   \
745   $(MESS_FORMATS)/ace_tap.o   \
746720
747721$(MESSOBJ)/capcom.a:         \
748722   $(MAME_DRIVERS)/cps1.o   \
r11457r11458
796770   $(MESS_VIDEO)/vdc8563.o      \
797771   $(MESS_VIDEO)/vic6567.o      \
798772   $(MESS_VIDEO)/vic4567.o      \
799   $(MESS_FORMATS)/d64_dsk.o   \
800   $(MESS_FORMATS)/g64_dsk.o   \
801   $(MESS_FORMATS)/d81_dsk.o   \
802   $(MESS_FORMATS)/cbm_tap.o   \
803773   $(MESS_FORMATS)/cbm_snqk.o   \
804774
805775$(MESSOBJ)/cccp.a:            \
r11457r11458
808778   $(MESS_VIDEO)/lviv.o      \
809779   $(MESS_DRIVERS)/lviv.o      \
810780   $(MESS_MACHINE)/lviv.o      \
811   $(MESS_FORMATS)/lviv_lvt.o   \
812781   $(MESS_DRIVERS)/mikro80.o   \
813782   $(MESS_MACHINE)/mikro80.o   \
814783   $(MESS_VIDEO)/mikro80.o      \
r11457r11458
847816
848817$(MESSOBJ)/comx.a:            \
849818   $(MESS_DRIVERS)/comx35.o   \
850   $(MESS_FORMATS)/comx35_dsk.o   \
851819   $(MESS_FORMATS)/comx35_comx.o   \
852820   $(MESS_MACHINE)/comxpl80.o   \
853821   $(MESS_VIDEO)/comx35.o      \
r11457r11458
924892   $(MESS_DRIVERS)/cgenie.o   \
925893   $(MESS_VIDEO)/cgenie.o      \
926894   $(MESS_MACHINE)/cgenie.o   \
927   $(MESS_FORMATS)/cgen_cas.o   \
928895
929896$(MESSOBJ)/einis.a:            \
930897   $(MESS_DRIVERS)/pecom.o      \
r11457r11458
975942
976943$(MESSOBJ)/exidy.a:            \
977944   $(MESS_MACHINE)/sorcerer.o   \
978   $(MESS_FORMATS)/sorc_dsk.o   \
979945   $(MESS_DRIVERS)/sorcerer.o   \
980946
981947$(MESSOBJ)/fairch.a:         \
r11457r11458
990956   $(MESS_MACHINE)/fm_scsi.o   \
991957   $(MESS_DRIVERS)/fm7.o      \
992958   $(MESS_VIDEO)/fm7.o         \
993   $(MESS_FORMATS)/fm7_cas.o   \
994959
995960$(MESSOBJ)/funtech.a:         \
996961   $(MESS_DRIVERS)/supracan.o   \
r11457r11458
999964   $(MESS_VIDEO)/galaxy.o      \
1000965   $(MESS_DRIVERS)/galaxy.o   \
1001966   $(MESS_MACHINE)/galaxy.o   \
1002   $(MESS_FORMATS)/gtp_cas.o   \
1003967
1004968$(MESSOBJ)/gamepark.a:         \
1005969   $(MESS_DRIVERS)/gp32.o      \
r11457r11458
11501114   $(MESS_DRIVERS)/primo.o      \
11511115   $(MESS_MACHINE)/primo.o      \
11521116   $(MESS_VIDEO)/primo.o      \
1153   $(MESS_FORMATS)/primoptp.o   \
11541117
11551118$(MESSOBJ)/mit.a:            \
11561119   $(MESS_VIDEO)/crt.o         \
r11457r11458
11661129
11671130$(MESSOBJ)/mos.a:            \
11681131   $(MESS_DRIVERS)/kim1.o      \
1169   $(MESS_FORMATS)/kim1_cas.o   \
11701132
11711133$(MESSOBJ)/motorola.a:         \
11721134   $(MESS_DRIVERS)/mekd2.o      \
r11457r11458
11841146
11851147$(MESSOBJ)/ne.a:            \
11861148   $(MESS_DRIVERS)/z80ne.o     \
1187   $(MESS_FORMATS)/z80ne_dsk.o \
11881149   $(MESS_MACHINE)/z80ne.o     \
11891150
11901151$(MESSOBJ)/nec.a:            \
11911152   $(MESS_MACHINE)/pce.o      \
11921153   $(MESS_DRIVERS)/pce.o      \
11931154   $(MESS_DRIVERS)/pcfx.o      \
1194   $(MESS_FORMATS)/p6001_cas.o   \
11951155   $(MESS_DRIVERS)/pc6001.o   \
11961156   $(MESS_DRIVERS)/pc8401a.o   \
11971157   $(MESS_VIDEO)/pc8401a.o      \
r11457r11458
12151175   $(MESS_DRIVERS)/gb.o      \
12161176   $(MESS_MACHINE)/nes_mmc.o   \
12171177   $(MESS_VIDEO)/nes.o         \
1218   $(MESS_FORMATS)/nes_dsk.o   \
12191178   $(MESS_MACHINE)/nes.o      \
12201179   $(MESS_DRIVERS)/nes.o      \
12211180   $(MESS_MACHINE)/snescart.o   \
r11457r11458
13121271$(MESSOBJ)/pel.a:            \
13131272   $(MESS_DRIVERS)/galeb.o      \
13141273   $(MESS_VIDEO)/galeb.o      \
1315   $(MESS_FORMATS)/orao_cas.o   \
13161274   $(MESS_DRIVERS)/orao.o      \
13171275   $(MESS_MACHINE)/orao.o      \
13181276   $(MESS_VIDEO)/orao.o      \
r11457r11458
13231281   $(MESS_MACHINE)/p2000t.o   \
13241282   $(MESS_DRIVERS)/vg5k.o      \
13251283   $(MESS_VIDEO)/ef9345.o      \
1326   $(MESS_FORMATS)/vg5k_cas.o   \
13271284
13281285$(MESSOBJ)/poly88.a:         \
13291286   $(MESS_DRIVERS)/poly88.o   \
r11457r11458
13731330   $(MESS_DRIVERS)/aim65_40.o   \
13741331
13751332$(MESSOBJ)/samcoupe.a:         \
1376   $(MESS_FORMATS)/coupedsk.o   \
13771333   $(MESS_VIDEO)/samcoupe.o   \
13781334   $(MESS_DRIVERS)/samcoupe.o   \
13791335   $(MESS_MACHINE)/samcoupe.o   \
r11457r11458
14011357$(MESSOBJ)/sharp.a:            \
14021358   $(MESS_VIDEO)/mz700.o      \
14031359   $(MESS_DRIVERS)/mz700.o      \
1404   $(MESS_FORMATS)/mz_cas.o   \
14051360   $(MESS_MACHINE)/lh5810.o   \
14061361   $(MESS_DRIVERS)/pc1500.o   \
14071362   $(MESS_DRIVERS)/pocketc.o   \
r11457r11458
14171372   $(MESS_MACHINE)/mz700.o      \
14181373   $(MESS_DRIVERS)/x68k.o      \
14191374   $(MESS_VIDEO)/x68k.o      \
1420   $(MESS_FORMATS)/dim_dsk.o   \
14211375   $(MESS_MACHINE)/x68k_hdc.o   \
14221376   $(MESS_MACHINE)/mb89352.o   \
14231377   $(MESS_DRIVERS)/mz80.o      \
r11457r11458
14261380   $(MESS_DRIVERS)/mz2000.o   \
14271381   $(MESS_DRIVERS)/x1.o      \
14281382   $(MESS_MACHINE)/x1.o      \
1429   $(MESS_FORMATS)/x1_tap.o   \
14301383   $(MESS_DRIVERS)/mz2500.o   \
14311384   $(MESS_DRIVERS)/pce220.o   \
14321385
r11457r11458
14431396   $(MESS_DRIVERS)/scorpion.o   \
14441397   $(MESS_DRIVERS)/atm.o      \
14451398   $(MESS_DRIVERS)/pentagon.o   \
1446   $(MESS_FORMATS)/trd_dsk.o   \
14471399   $(MESS_MACHINE)/beta.o      \
1448   $(MESS_FORMATS)/zx81_p.o   \
1449   $(MESS_FORMATS)/tzx_cas.o   \
14501400   $(MESS_FORMATS)/spec_snqk.o   \
14511401   $(MESS_FORMATS)/timex_dck.o   \
14521402   $(MESS_DRIVERS)/ql.o      \
r11457r11458
14751425
14761426$(MESSOBJ)/sord.a:            \
14771427   $(MESS_DRIVERS)/m5.o      \
1478   $(MESS_FORMATS)/sord_cas.o   \
14791428
14801429$(MESSOBJ)/special.a:         \
14811430   $(MESS_AUDIO)/special.o      \
14821431   $(MESS_DRIVERS)/special.o   \
1483   $(MESS_FORMATS)/smx_dsk.o   \
14841432   $(MESS_MACHINE)/special.o   \
14851433   $(MESS_VIDEO)/special.o      \
14861434
r11457r11458
14901438$(MESSOBJ)/svi.a:            \
14911439   $(MESS_MACHINE)/svi318.o   \
14921440   $(MESS_DRIVERS)/svi318.o   \
1493   $(MESS_FORMATS)/svi_dsk.o   \
1494   $(MESS_FORMATS)/svi_cas.o   \
14951441
14961442$(MESSOBJ)/svision.a:         \
14971443   $(MESS_DRIVERS)/svision.o   \
r11457r11458
15061452   $(MESS_MACHINE)/microtan.o   \
15071453   $(MESS_DRIVERS)/microtan.o   \
15081454   $(MESS_FORMATS)/m65_snqk.o   \
1509   $(MESS_FORMATS)/oric_dsk.o   \
1510   $(MESS_FORMATS)/oric_tap.o   \
15111455   $(MESS_DRIVERS)/oric.o      \
15121456   $(MESS_VIDEO)/oric.o      \
15131457   $(MESS_MACHINE)/oric.o      \
r11457r11458
15271471$(MESSOBJ)/telenova.a:         \
15281472   $(MESS_DRIVERS)/compis.o   \
15291473   $(MESS_MACHINE)/compis.o   \
1530   $(MESS_FORMATS)/cpis_dsk.o   \
15311474
15321475$(MESSOBJ)/telercas.a:         \
15331476   $(MESS_DRIVERS)/tmc1800.o   \
r11457r11458
15461489   $(MESS_VIDEO)/pmd85.o      \
15471490   $(MESS_DRIVERS)/pmd85.o      \
15481491   $(MESS_MACHINE)/pmd85.o      \
1549   $(MESS_FORMATS)/pmd_pmd.o   \
15501492   $(MESS_DRIVERS)/pmi80.o      \
15511493   $(MESS_DRIVERS)/sapi1.o      \
15521494   $(MESS_MACHINE)/sapi1.o      \
r11457r11458
15571499   $(MESS_MACHINE)/thomson.o   \
15581500   $(MESS_VIDEO)/thomson.o      \
15591501   $(MESS_MACHINE)/thomflop.o   \
1560   $(MESS_FORMATS)/thom_dsk.o   \
1561   $(MESS_FORMATS)/thom_cas.o   \
15621502
15631503$(MESSOBJ)/ti.a:            \
15641504   $(MESS_DRIVERS)/ti85.o      \
r11457r11458
16521592   $(MESS_DRIVERS)/mc10.o      \
16531593   $(MESS_MACHINE)/trs80.o      \
16541594   $(MESS_VIDEO)/trs80.o      \
1655   $(MESS_FORMATS)/trs_dsk.o   \
1656   $(MESS_FORMATS)/trs_cas.o   \
16571595   $(MESS_FORMATS)/trs_cmd.o   \
16581596   $(MESS_DRIVERS)/trs80.o      \
16591597   $(MESS_DRIVERS)/trs80m2.o   \
r11457r11458
16961634   $(MESS_VIDEO)/vtech2.o      \
16971635   $(MESS_MACHINE)/vtech2.o   \
16981636   $(MESS_DRIVERS)/vtech2.o   \
1699   $(MESS_FORMATS)/vt_cas.o   \
1700   $(MESS_FORMATS)/vt_dsk.o   \
17011637   $(MESS_DRIVERS)/crvision.o   \
17021638   $(MESS_AUDIO)/socrates.o   \
17031639   $(MESS_DRIVERS)/socrates.o   \
makefile
r11457r11458
500500   -I$(OBJ)/emu \
501501   -I$(OBJ)/emu/layout \
502502   -I$(SRC)/lib/util \
503   -I$(SRC)/lib \
503504   -I$(SRC)/osd \
504505   -I$(SRC)/osd/$(OSD) \
505506
r11457r11458
611612# add SoftFloat floating point emulation library
612613SOFTFLOAT = $(OBJ)/libsoftfloat.a
613614
615# add formats emulation library
616FORMATS_LIB = $(OBJ)/libformats.a
614617
615618
616619#-------------------------------------------------
r11457r11458
722725ifndef EXECUTABLE_DEFINED
723726
724727# always recompile the version string
725$(VERSIONOBJ): $(DRVLIBS) $(LIBOSD) $(LIBCPU) $(LIBEMU) $(LIBSOUND) $(LIBUTIL) $(EXPAT) $(ZLIB) $(SOFTFLOAT) $(LIBOCORE) $(RESFILE)
728$(VERSIONOBJ): $(DRVLIBS) $(LIBOSD) $(LIBCPU) $(LIBEMU) $(LIBSOUND) $(LIBUTIL) $(EXPAT) $(ZLIB) $(SOFTFLOAT) $(FORMATS_LIB) $(LIBOCORE) $(RESFILE)
726729
727$(EMULATOR): $(VERSIONOBJ) $(DRIVLISTOBJ) $(DRVLIBS) $(LIBOSD) $(LIBCPU) $(LIBEMU) $(LIBDASM) $(LIBSOUND) $(LIBUTIL) $(EXPAT) $(SOFTFLOAT) $(ZLIB) $(LIBOCORE) $(RESFILE)
730$(EMULATOR): $(VERSIONOBJ) $(DRIVLISTOBJ) $(DRVLIBS) $(LIBOSD) $(LIBCPU) $(LIBEMU) $(LIBDASM) $(LIBSOUND) $(LIBUTIL) $(EXPAT) $(SOFTFLOAT) $(FORMATS_LIB) $(ZLIB) $(LIBOCORE) $(RESFILE)
728731   @echo Linking $@...
729732   $(LD) $(LDFLAGS) $(LDFLAGSEMULATOR) $^ $(LIBS) -o $@
730733ifeq ($(TARGETOS),win32)

Previous 15994 Revisions Next


© 1998-2018 The MESS Team