forked from scarybeasts/beebjit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
disc_dfi.c
169 lines (154 loc) · 4.59 KB
/
disc_dfi.c
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
#include "disc_dfi.h"
#include "disc.h"
#include "ibm_disc_format.h"
#include "log.h"
#include "util.h"
#include <assert.h>
#include <string.h>
void
disc_dfi_load(struct disc_struct* p_disc) {
static const size_t k_max_dfi_track_size = (1024 * 1024);
uint32_t len;
uint8_t header[4];
uint8_t chunk[10];
int32_t current_track;
int32_t current_head;
uint8_t* p_dfi_track_data;
float* p_pulses;
uint32_t num_sides = 1;
float pulse_scale_factor = 0.0;
struct util_file* p_file = disc_get_file(p_disc);
assert(p_file != NULL);
len = util_file_read(p_file, &header[0], sizeof(header));
if (len != sizeof(header)) {
util_bail("DFI missing header");
}
if (memcmp(&header[0], "DFE2", 4) != 0) {
util_bail("DFI bad header");
}
p_dfi_track_data = util_malloc(k_max_dfi_track_size);
p_pulses = util_malloc(k_max_dfi_track_size * 4);
current_track = -1;
current_head = -1;
while (1) {
uint16_t track;
uint16_t head;
uint16_t sector;
uint32_t track_length;
uint32_t i_data;
uint16_t sample;
uint32_t current_rev;
uint32_t num_pulses;
len = util_file_read(p_file, &chunk[0], 10);
if (len == 0) {
/* No more chunks, done. */
break;
}
if (len != 10) {
util_bail("DFI can't read track chunk");
}
track = util_read_be16(&chunk[0]);
head = util_read_be16(&chunk[2]);
sector = util_read_be16(&chunk[4]);
if (head == 0) {
/* Start new track. */
current_track++;
current_head = 0;
if (current_track >= k_ibm_disc_tracks_per_disc) {
util_bail("DFI excessive tracks");
}
} else if (head == 1) {
if (current_head != 0) {
util_bail("DFI bad head");
}
current_head = 1;
num_sides = 2;
} else {
util_bail("DFI head out of range");
}
if (track != current_track) {
util_bail("DFI bad track");
}
/* The spec says this is always set to 0 for soft sectored discs but it
* appears to be 1.
*/
if (sector != 1) {
util_bail("DFI hard sectored not supported");
}
track_length = util_read_be32(&chunk[6]);
if (track_length > k_max_dfi_track_size) {
util_bail("DFI track too large");
}
len = util_file_read(p_file, p_dfi_track_data, track_length);
if (len != track_length) {
util_bail("DFI can't read track data");
}
if ((current_track == 0) && (current_head == 0)) {
uint32_t total_ticks = 0;
/* First chunk of data; run across it to find an index pulse to calculate
* how long each pulse base unit really is.
* There's variance in sample rate (100MHz vs. 25MHz) and drive RPM
* (300rpm vs. 360rpm).
*/
for (i_data = 0; i_data < track_length; ++i_data) {
uint8_t byte = p_dfi_track_data[i_data];
if ((byte & 0x80) != 0) {
pulse_scale_factor = (200000.0 / total_ticks);
break;
}
total_ticks += byte;
}
if (pulse_scale_factor == 0.0) {
util_bail("DFI missing index pulse");
}
log_do_log(k_log_disc,
k_log_info,
"DFI: calculated pulse scale factor %f",
pulse_scale_factor);
}
sample = 0;
current_rev = 0;
num_pulses = 0;
for (i_data = 0; i_data < track_length; ++i_data) {
float delta_us;
uint8_t byte = p_dfi_track_data[i_data];
if ((byte & 0x80) != 0) {
/* Index pulse. */
disc_build_track_from_pulses(p_disc,
current_rev,
current_head,
current_track,
p_pulses,
num_pulses);
current_rev++;
num_pulses = 0;
sample += (byte & 0x7f);
continue;
}
if (byte == 0x7f) {
/* Carry. */
sample += 0x7f;
continue;
}
sample += byte;
delta_us = (sample * pulse_scale_factor);
sample = 0;
p_pulses[num_pulses] = delta_us;
num_pulses++;
}
/* May not necessarily be an index pulse at the end of the last rev. */
disc_build_track_from_pulses(p_disc,
current_rev,
current_head,
current_track,
p_pulses,
num_pulses);
}
log_do_log(k_log_disc,
k_log_info,
"DFI: found %d sides, %d tracks",
num_sides,
(current_track + 1));
util_free(p_dfi_track_data);
util_free(p_pulses);
}