summaryrefslogtreecommitdiffstatshomepage
path: root/src/lib/formats/svi_dsk.c
blob: e94b2e2f94dbc11684f211f2bf58e83636873c87 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*********************************************************************

    formats/svi_dsk.c

    SVI318 disk images

*********************************************************************/

#include <string.h>

#include "svi_dsk.h"
#include "basicdsk.h"

static FLOPPY_IDENTIFY(svi_dsk_identify)
{
	*vote = ((floppy_image_size(floppy) == (172032)) || (floppy_image_size(floppy) == (346112))) ? 100 : 0;
	return FLOPPY_ERROR_SUCCESS;
}

static int svi_get_heads_per_disk(floppy_image_legacy *floppy)
{
	return (floppy_image_size(floppy) == (172032)) ? 1 : 2;
}

static int svi_get_tracks_per_disk(floppy_image_legacy *floppy)
{
	return 40;
}

static UINT64 svi_translate_offset(floppy_image_legacy *floppy,
		int track, int head, int sector)
{
	UINT64 o;
	if ((track==0) && (head==0))
		o = sector*128;
	else
		o = ((track*((floppy_image_size(floppy) == (172032)) ? 1 : 2)+head)*17+sector)*256-2048; /* (17*256)-(18*128)=2048 */

	return o;
}

static floperr_t get_offset(floppy_image_legacy *floppy, int head, int track, int sector, int sector_is_index, UINT64 *offset)
{
	UINT64 offs;
	/* translate the sector to a raw sector */
	if (!sector_is_index)
	{
		sector -= 1;
	}
	/* check to see if we are out of range */
	if ((head < 0) || (head >= ((floppy_image_size(floppy) == (172032)) ? 1 : 2)) || (track < 0) || (track >= 40)
			|| (sector < 0) || (sector >= ((head==0 && track==0) ? 18 : 17)))
		return FLOPPY_ERROR_SEEKERROR;

	offs = svi_translate_offset(floppy, track, head, sector);
	if (offset)
		*offset = offs;
	return FLOPPY_ERROR_SUCCESS;
}



static floperr_t internal_svi_read_sector(floppy_image_legacy *floppy, int head, int track, int sector, int sector_is_index, void *buffer, size_t buflen)
{
	UINT64 offset;
	floperr_t err;
	err = get_offset(floppy, head, track, sector, sector_is_index, &offset);
	if (err)
		return err;

	floppy_image_read(floppy, buffer, offset, buflen);
	return FLOPPY_ERROR_SUCCESS;
}



static floperr_t internal_svi_write_sector(floppy_image_legacy *floppy, int head, int track, int sector, int sector_is_index, const void *buffer, size_t buflen, int ddam)
{
	UINT64 offset;
	floperr_t err;

	err = get_offset(floppy, head, track, sector, sector_is_index, &offset);
	if (err)
		return err;

	floppy_image_write(floppy, buffer, offset, buflen);
	return FLOPPY_ERROR_SUCCESS;
}



static floperr_t svi_read_sector(floppy_image_legacy *floppy, int head, int track, int sector, void *buffer, size_t buflen)
{
	return internal_svi_read_sector(floppy, head, track, sector, FALSE, buffer, buflen);
}

static floperr_t svi_write_sector(floppy_image_legacy *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam)
{
	return internal_svi_write_sector(floppy, head, track, sector, FALSE, buffer, buflen, ddam);
}

static floperr_t svi_read_indexed_sector(floppy_image_legacy *floppy, int head, int track, int sector, void *buffer, size_t buflen)
{
	return internal_svi_read_sector(floppy, head, track, sector, TRUE, buffer, buflen);
}

static floperr_t svi_write_indexed_sector(floppy_image_legacy *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam)
{
	return internal_svi_write_sector(floppy, head, track, sector, TRUE, buffer, buflen, ddam);
}

static floperr_t svi_get_sector_length(floppy_image_legacy *floppy, int head, int track, int sector, UINT32 *sector_length)
{
	floperr_t err;
	err = get_offset(floppy, head, track, sector, FALSE, NULL);
	if (err)
		return err;

	if (sector_length) {
		*sector_length = (head==0 && track==0) ? 128 : 256;
	}
	return FLOPPY_ERROR_SUCCESS;
}



static floperr_t svi_get_indexed_sector_info(floppy_image_legacy *floppy, int head, int track, int sector_index, int *cylinder, int *side, int *sector, UINT32 *sector_length, unsigned long *flags)
{
	sector_index += 1;
	if (cylinder)
		*cylinder = track;
	if (side)
		*side = head;
	if (sector)
		*sector = sector_index;
	if (flags)
		/* TODO: read DAM or DDAM and determine flags */
		*flags = 0;
	return svi_get_sector_length(floppy, head, track, sector_index, sector_length);
}


static FLOPPY_CONSTRUCT(svi_dsk_construct)
{
	struct FloppyCallbacks *callbacks;
	callbacks = floppy_callbacks(floppy);
	callbacks->read_sector = svi_read_sector;
	callbacks->write_sector = svi_write_sector;
	callbacks->read_indexed_sector = svi_read_indexed_sector;
	callbacks->write_indexed_sector = svi_write_indexed_sector;
	callbacks->get_sector_length = svi_get_sector_length;
	callbacks->get_heads_per_disk = svi_get_heads_per_disk;
	callbacks->get_tracks_per_disk = svi_get_tracks_per_disk;
	callbacks->get_indexed_sector_info = svi_get_indexed_sector_info;

	return FLOPPY_ERROR_SUCCESS;
}



/* ----------------------------------------------------------------------- */

LEGACY_FLOPPY_OPTIONS_START( svi318 )
	LEGACY_FLOPPY_OPTION( svi_dsk, "dsk", "SVI-318 floppy disk image",  svi_dsk_identify, svi_dsk_construct, NULL, NULL)
	LEGACY_FLOPPY_OPTION( svi_cpm, "dsk", "SVI-728 DSDD CP/M disk image", basicdsk_identify_default, basicdsk_construct_default, NULL,
		HEADS([2])
		TRACKS([40])
		SECTORS([17])
		SECTOR_LENGTH([256])
		FIRST_SECTOR_ID([1]))
LEGACY_FLOPPY_OPTIONS_END