forked from samkusin/clemens_iigs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clem_2img.c
843 lines (769 loc) · 31.4 KB
/
clem_2img.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
#include "clem_2img.h"
#include "external/cross_endian.h"
#include <assert.h>
#include <string.h>
/*
https://apple2.org.za/gswv/a2zine/Docs/DiskImage_2MG_Info.txt
struct Clemens2IMGDisk {
char creator[4];
uint16_t version;
uint32_t format;
uint32_t dos_volume;
uint32_t block_count;
bool is_write_protected;
uint8_t* creator_data;
uint8_t* creator_data_end;
char* comment;
char* comment_end;
uint8_t* data;
uint8_t* data_end;
};
*/
/* Derived from Beneath Apple DOS and ProDOS 2020
Table 3.1 ProDOS Block Conversion Table for Diskettes
Below documention refers to 5.25" disks
3.5" documentation follows this section, and also refer to clem_disk.h
Prodos logical sectors are defined in documentation as 512 bytes (they are
effectively interleaved on the physical track.)
As seen in the table, physical sectors 0 and 2 are used by logical prodos
sector 0.
Since DOS logical sectors are 256 bytes, we'll divide a logical ProDOS
sector into 2 - this will keep the nibbilization code relatively
consistent between DOS and ProDOS images.
Note for 3.5" drives, the 'sector' size is 512 bytes
*/
// clang-format off
static const uint8_t gcr_6_2_byte[64] = {
0x96, 0x97, 0x9a, 0x9b, 0x9d, 0x9e, 0x9f, 0xa6,
0xa7, 0xab, 0xac, 0xad, 0xae, 0xaf, 0xb2, 0xb3,
0xb4, 0xb5, 0xb6, 0xb7, 0xb9, 0xba, 0xbb, 0xbc,
0xbd, 0xbe, 0xbf, 0xcb, 0xcd, 0xce, 0xcf, 0xd3,
0xd6, 0xd7, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde,
0xdf, 0xe5, 0xe6, 0xe7, 0xe9, 0xea, 0xeb, 0xec,
0xed, 0xee, 0xef, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6,
0xf7, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff};
// clang-format on
static const unsigned prodos_to_logical_sector_map_525[1][16] = {
{0, 8, 1, 9, 2, 10, 3, 11, 4, 12, 5, 13, 6, 14, 7, 15}};
/* only support 16 sector tracks */
static const unsigned dos_to_logical_sector_map_525[1][16] = {
{0, 7, 14, 6, 13, 5, 12, 4, 11, 3, 10, 2, 9, 1, 8, 15}};
/* 3.5" drives have 512 byte sectors (ProDOS assumed) */
static const unsigned prodos_to_logical_sector_map_35[CLEM_DISK_35_NUM_REGIONS][16] = {
{0, 6, 1, 7, 2, 8, 3, 9, 4, 10, 5, 11, -1, -1, -1, -1},
{0, 6, 1, 7, 2, 8, 3, 9, 4, 10, 5, -1, -1, -1, -1, -1},
{0, 5, 1, 6, 2, 7, 3, 8, 4, 9, -1, -1, -1, -1, -1, -1},
{0, 5, 1, 6, 2, 7, 3, 8, 4, -1, -1, -1, -1, -1, -1, -1},
{0, 4, 1, 5, 2, 6, 3, 7, -1, -1, -1, -1, -1, -1, -1, -1}};
static inline size_t _increment_data_ptr(const uint8_t *p, size_t amt, const uint8_t *end) {
const uint8_t *n = p + amt;
return n <= end ? amt : n - end;
}
// all encoding is to little endian order in the final stream
static inline uint16_t _decode_u16(const uint8_t *data) {
return ((uint16_t)data[1] << 8) | data[0];
}
static inline uint32_t _decode_u32(const uint8_t *data) {
return (((uint32_t)data[3] << 24) | ((uint32_t)data[2] << 16) | ((uint32_t)data[1] << 8) |
data[0]);
}
static inline void _encode_u16(uint8_t **data, uint16_t u16) {
(*data)[1] = (uint8_t)(u16 >> 8);
(*data)[0] = (uint8_t)(u16 & 0xff);
*data += 2;
}
static inline void _encode_u32(uint8_t **data, uint32_t u32) {
(*data)[3] = (uint8_t)(u32 >> 24);
(*data)[2] = (uint8_t)(u32 >> 16);
(*data)[1] = (uint8_t)(u32 >> 8);
(*data)[0] = (uint8_t)(u32 & 0xff);
*data += 4;
}
static void _encode_mem(uint8_t **data, uint8_t *mem, uint32_t cnt, bool overlapped) {
if (overlapped) {
memmove(*data, mem, cnt);
} else {
memcpy(*data, mem, cnt);
}
*data += cnt;
}
bool clem_2img_parse_header(struct Clemens2IMGDisk *disk, uint8_t *data, uint8_t *data_end) {
size_t allocation_amt = 0;
int state = 0;
uint32_t param32;
disk->image_buffer = data;
disk->image_buffer_length = data_end - data;
while (data < data_end) {
size_t data_size = 0;
switch (state) {
case 0: // is 2IMG?
data_size = _increment_data_ptr(data, 4, data_end);
if (!memcmp(data, "2IMG", data_size)) {
++state;
} else {
return false;
}
break;
case 1: // creator tag
data_size = _increment_data_ptr(data, 4, data_end);
if (data_size == 4) {
memcpy(disk->creator, data, data_size);
++state;
} else {
return false;
}
break;
case 2: // header size
data_size = _increment_data_ptr(data, 2, data_end);
if (data_size == 2 && _decode_u16(data) == CLEM_2IMG_HEADER_BYTE_SIZE) {
++state;
} else {
return false;
}
break;
case 3: // version number
data_size = _increment_data_ptr(data, 2, data_end);
if (data_size == 2) {
disk->version = _decode_u16(data);
++state;
} else {
return false;
}
break;
case 4: // image format
data_size = _increment_data_ptr(data, 4, data_end);
if (data_size == 4) {
disk->format = _decode_u32(data);
++state;
} else {
return false;
}
break;
case 5: // flags
data_size = _increment_data_ptr(data, 4, data_end);
if (data_size == 4) {
disk->is_nibblized = false;
param32 = _decode_u32(data);
if (param32 & 0x80000000) {
disk->is_write_protected = true;
}
if (disk->format == CLEM_2IMG_FORMAT_DOS) {
if (param32 & 0x100) {
disk->dos_volume = param32 & 0xff;
} else {
disk->dos_volume = 254;
;
}
disk->dos_volume = param32 & 0xff;
} else {
disk->dos_volume = 0;
}
++state;
} else {
return false;
}
break;
case 6: // ProDOS blocks
data_size = _increment_data_ptr(data, 4, data_end);
if (data_size == 4) {
disk->block_count = _decode_u32(data);
++state;
} else {
return false;
}
break;
case 7: // points to the start of the data chunk
data_size = _increment_data_ptr(data, 4, data_end);
if (data_size == 4) {
disk->image_data_offset = _decode_u32(data);
disk->data = disk->image_buffer + disk->image_data_offset;
++state;
} else {
return false;
}
break;
case 8: // data_end - data = size
data_size = _increment_data_ptr(data, 4, data_end);
if (data_size == 4) {
param32 = _decode_u32(data);
if (param32 == 0) {
// this is possible for prodos images
// use blocks * 512
param32 = disk->block_count * 512;
}
disk->data_end = disk->data + param32;
allocation_amt += param32;
++state;
} else {
return false;
}
break;
case 9: // points to the start of the comment chunk
data_size = _increment_data_ptr(data, 4, data_end);
if (data_size == 4) {
disk->comment = (char *)disk->image_buffer + _decode_u32(data);
++state;
} else {
return false;
}
break;
case 10: // data_end - data = size
data_size = _increment_data_ptr(data, 4, data_end);
if (data_size == 4) {
param32 = _decode_u32(data);
disk->comment_end = disk->comment + param32;
allocation_amt += param32;
++state;
} else {
return false;
}
break;
case 11: // points to the start of the creator chunk
data_size = _increment_data_ptr(data, 4, data_end);
if (data_size == 4) {
disk->creator_data = (char *)disk->image_buffer + _decode_u32(data);
++state;
} else {
return false;
}
break;
case 12: // commend_end - comment = size
data_size = _increment_data_ptr(data, 4, data_end);
if (data_size == 4) {
param32 = _decode_u32(data);
disk->creator_data_end = disk->creator + param32;
allocation_amt += param32;
++state;
} else {
return false;
}
break;
case 13: // empty space
data_size = _increment_data_ptr(data, 16, data_end);
if (data_size == 16) {
++state;
} else {
return false;
}
break;
case 14: // if well formed header, we shouldn't get here
return true;
}
data += data_size;
}
return false;
}
bool clem_2img_build_image(struct Clemens2IMGDisk *disk, uint8_t *image, uint8_t *image_end) {
uint8_t *image_cur = image;
size_t image_size = image_end - image;
size_t source_size = disk->data_end - disk->data;
size_t creator_size = disk->creator_data_end - disk->creator_data;
size_t comment_size = disk->comment_end - disk->comment;
uint32_t flags;
bool overlapped;
if (image_end < disk->image_buffer) {
overlapped = false;
} else if (image >= disk->image_buffer + disk->image_buffer_length) {
overlapped = false;
} else if (image < disk->image_buffer ||
(image == disk->image_buffer &&
image_end == disk->image_buffer + disk->image_buffer_length)) {
overlapped = true;
} else {
/* some overlap where data will be corrupted */
return false;
}
if (image_size < source_size + creator_size + comment_size + CLEM_2IMG_HEADER_BYTE_SIZE)
return false;
if (disk->format == CLEM_2IMG_FORMAT_PRODOS) {
if (disk->block_count * 512 != source_size)
return false;
} else if (disk->format == CLEM_2IMG_FORMAT_DOS) {
/* DOS 140K disks*/
if (source_size != 280 * 512)
return false;
} else {
return false;
}
_encode_mem(&image_cur, (uint8_t *)"2IMG", 4, overlapped);
_encode_mem(&image_cur, (uint8_t *)"CLEM", 4, overlapped);
_encode_u16(&image_cur, CLEM_2IMG_HEADER_BYTE_SIZE);
_encode_u16(&image_cur, disk->version);
_encode_u32(&image_cur, disk->format);
flags = 0;
if (disk->is_write_protected)
flags |= 0x80000000;
if (disk->dos_volume < 254)
flags |= (0x0100 + (disk->dos_volume & 0xff));
_encode_u32(&image_cur, flags);
if (disk->format == CLEM_2IMG_FORMAT_PRODOS) {
_encode_u32(&image_cur, disk->block_count);
} else {
_encode_u32(&image_cur, 0);
}
_encode_u32(&image_cur, CLEM_2IMG_HEADER_BYTE_SIZE);
_encode_u32(&image_cur, source_size);
if (creator_size > 0) {
_encode_u32(&image_cur, CLEM_2IMG_HEADER_BYTE_SIZE + source_size);
_encode_u32(&image_cur, (uint32_t)(creator_size));
} else {
_encode_u32(&image_cur, 0);
_encode_u32(&image_cur, 0);
}
if (comment_size > 0) {
_encode_u32(&image_cur, CLEM_2IMG_HEADER_BYTE_SIZE + source_size + creator_size);
_encode_u32(&image_cur, (uint32_t)(comment_size));
} else {
_encode_u32(&image_cur, 0);
_encode_u32(&image_cur, 0);
}
/* empty 16-byte buffer*/
_encode_u32(&image_cur, 0);
_encode_u32(&image_cur, 0);
_encode_u32(&image_cur, 0);
_encode_u32(&image_cur, 0);
if (image_cur - image != CLEM_2IMG_HEADER_BYTE_SIZE)
return false;
_encode_mem(&image_cur, disk->data, source_size, overlapped);
_encode_mem(&image_cur, (uint8_t *)disk->creator_data, creator_size, overlapped);
_encode_mem(&image_cur, (uint8_t *)disk->comment, comment_size, overlapped);
return true;
}
bool clem_2img_generate_header(struct Clemens2IMGDisk *disk, uint32_t format, uint8_t *image,
uint8_t *image_end, uint32_t image_data_offset) {
uint32_t disk_size = (uint32_t)(image_end - image) - image_data_offset;
strncpy(disk->creator, "CLEM", sizeof(disk->creator));
/* validate that the input contains only sector data */
if (format == CLEM_2IMG_FORMAT_PRODOS) {
disk->block_count = disk_size / 512;
if (disk_size % 512) {
return false;
}
} else if (format == CLEM_2IMG_FORMAT_DOS) {
disk->block_count = 0;
if (disk_size % 256) {
return false;
}
}
/* TODO: support creator data and comments */
disk->image_buffer = image;
disk->image_buffer_length = (uint32_t)(image_end - image);
disk->image_data_offset = image_data_offset;
disk->data = disk->image_buffer + disk->image_data_offset;
disk->data_end = disk->data + disk_size;
disk->creator_data = (char *)disk->data_end;
disk->creator_data_end = disk->creator_data;
disk->comment = disk->creator_data_end;
disk->comment_end = disk->comment;
disk->version = 0x0001;
disk->format = format;
if (format == CLEM_2IMG_FORMAT_DOS) {
disk->dos_volume = 0xfe;
} else {
disk->dos_volume = 0x00;
}
disk->is_write_protected = true;
disk->is_nibblized = false;
return true;
}
static void _clem_nib_init_encoder(struct ClemensNibEncoder *encoder, uint8_t *begin,
uint8_t *end) {
encoder->begin = begin;
encoder->end = end;
encoder->bit_index = 0;
encoder->bit_index_end = (end - begin) * 8;
}
static void _clem_nib_write_bytes(struct ClemensNibEncoder *encoder, unsigned cnt, unsigned bit_cnt,
uint8_t value) {
uint8_t *nib_cur = encoder->begin + (encoder->bit_index / 8);
unsigned bit_count = bit_cnt * cnt;
unsigned nib_bit_index_end = encoder->bit_index + bit_count;
unsigned bit_cnt_minus_1 = bit_cnt - 1;
unsigned out_shift = 7 - (encoder->bit_index % 8);
unsigned in_shift = 0;
nib_bit_index_end %= encoder->bit_index_end;
while (encoder->bit_index != nib_bit_index_end) {
if (value & (1 << (bit_cnt_minus_1 - in_shift))) {
nib_cur[0] |= (1 << out_shift);
} else {
nib_cur[0] &= ~(1 << out_shift);
}
encoder->bit_index = (encoder->bit_index + 1) % encoder->bit_index_end;
in_shift = (in_shift + 1) % bit_cnt;
out_shift = 7 - (encoder->bit_index % 8);
nib_cur = encoder->begin + (encoder->bit_index / 8);
}
}
static void _clem_nib_encode_self_sync_ff(struct ClemensNibEncoder *encoder, unsigned cnt) {
_clem_nib_write_bytes(encoder, cnt, 10, 0xff);
}
static void _clem_nib_write_one(struct ClemensNibEncoder *encoder, uint8_t value) {
_clem_nib_write_bytes(encoder, 1, 8, value);
}
static void _clem_nib_encode_one_6_2(struct ClemensNibEncoder *encoder, uint8_t value) {
_clem_nib_write_one(encoder, gcr_6_2_byte[value & 0x3f]);
}
static void _clem_nib_encode_one_4_4(struct ClemensNibEncoder *encoder, uint8_t value) {
/* all unused bits are set to '1', so 4x4 encoding to preserve odd bits
requires shifting the bits to the right */
_clem_nib_write_one(encoder, (value >> 1) | 0xaa);
/* even bits */
_clem_nib_write_one(encoder, value | 0xaa);
}
static void _clem_nib_encode_data_35(struct ClemensNibEncoder *encoder, const uint8_t *buf,
unsigned cnt) {
/* decoded bytes are encoded to GCR 6-2 8-bit bytes*/
uint8_t scratch0[175], scratch1[175], scratch2[175];
uint8_t data[524];
unsigned chksum[3];
unsigned data_idx = 0, scratch_idx = 0;
uint8_t v;
assert(cnt == 512);
/* IIgs - 12 byte tag header is blank, but....
TODO: what if it isn't?? */
data_idx = 12;
memset(data, 0, 12);
memcpy(data + data_idx, buf, 512);
data_idx = 0;
/* split incoming decoded nibble data into parts for encoding into the
final encoded buffer
shamelessly translated from Ciderpress Nibble35.cpp as the encoding
scheme is quite involved - you stand on the shoulders of giants.
*/
chksum[0] = chksum[1] = chksum[2] = 0;
while (data_idx < 524) {
chksum[0] = (chksum[0] & 0xff) << 1;
if (chksum[0] & 0x100) {
++chksum[0];
}
v = data[data_idx++];
chksum[2] += v;
if (chksum[0] > 0xff) {
++chksum[2];
chksum[0] &= 0xff;
}
scratch0[scratch_idx] = (v ^ chksum[0]) & 0xff;
v = data[data_idx++];
chksum[1] += v;
if (chksum[2] > 0xff) {
++chksum[1];
chksum[2] &= 0xff;
}
scratch1[scratch_idx] = (v ^ chksum[2]) & 0xff;
if (data_idx < 524) {
v = data[data_idx++];
chksum[0] += v;
if (chksum[1] > 0xff) {
++chksum[0];
chksum[1] &= 0xff;
}
scratch2[scratch_idx] = (v ^ chksum[1]) & 0xff;
++scratch_idx;
}
}
scratch2[scratch_idx++] = 0;
for (data_idx = 0; data_idx < scratch_idx; ++data_idx) {
v = (scratch0[data_idx] & 0xc0) >> 2;
v |= (scratch1[data_idx] & 0xc0) >> 4;
v |= (scratch2[data_idx] & 0xc0) >> 6;
_clem_nib_encode_one_6_2(encoder, v);
_clem_nib_encode_one_6_2(encoder, scratch0[data_idx]);
_clem_nib_encode_one_6_2(encoder, scratch1[data_idx]);
if (data_idx < scratch_idx - 1) {
_clem_nib_encode_one_6_2(encoder, scratch2[data_idx]);
}
}
/* checksum */
v = (chksum[0] & 0xc0) >> 6;
v |= (chksum[1] & 0xc0) >> 4;
v |= (chksum[2] & 0xc0) >> 2;
_clem_nib_encode_one_6_2(encoder, v);
_clem_nib_encode_one_6_2(encoder, chksum[2]);
_clem_nib_encode_one_6_2(encoder, chksum[1]);
_clem_nib_encode_one_6_2(encoder, chksum[0]);
}
#define CLEM_NIB_ENCODE_525_6_2_RIGHT_BUFFER_SIZE 86
static void _clem_nib_encode_data_525(struct ClemensNibEncoder *encoder, const uint8_t *buf,
unsigned cnt) {
/* cannot support anything by cnt = 256 bytes, with 86 bytes to
contain the 2 bits nibble = 324 bytes, which is the specified data chunk
size of the sector on disk */
uint8_t enc6[256];
uint8_t enc2[86];
uint8_t right;
uint8_t chksum = 0;
unsigned i6, i2;
i6 = 0;
for (i2 = CLEM_NIB_ENCODE_525_6_2_RIGHT_BUFFER_SIZE; i2 > 0;) { /* 256 */
--i2;
enc6[i6] = buf[i6] >> 2; /* 6 bits */
right = (buf[i6] & 1) << 1; /* lower 2 bits flipped */
right |= (buf[i6] & 2) >> 1;
enc2[i2] = right;
++i6;
}
for (i2 = CLEM_NIB_ENCODE_525_6_2_RIGHT_BUFFER_SIZE; i2 > 0;) { /* 170 */
--i2;
enc6[i6] = buf[i6] >> 2; /* 6 bits */
right = (buf[i6] & 1) << 1; /* lower 2 bits flipped */
right |= (buf[i6] & 2) >> 1;
enc2[i2] |= (right << 2);
++i6;
}
for (i2 = CLEM_NIB_ENCODE_525_6_2_RIGHT_BUFFER_SIZE; i2 > 2;) { /* 84 */
--i2;
enc6[i6] = buf[i6] >> 2; /* 6 bits */
right = (buf[i6] & 1) << 1; /* lower 2 bits flipped */
right |= (buf[i6] & 2) >> 1;
enc2[i2] |= (right << 4);
++i6;
}
assert(i6 == 256);
for (i2 = CLEM_NIB_ENCODE_525_6_2_RIGHT_BUFFER_SIZE; i2 > 0;) {
--i2;
_clem_nib_encode_one_6_2(encoder, enc2[i2] ^ chksum);
chksum = enc2[i2];
}
for (i6 = 0; i6 < 256; ++i6) {
_clem_nib_encode_one_6_2(encoder, enc6[i6] ^ chksum);
chksum = enc6[i6];
}
_clem_nib_encode_one_6_2(encoder, chksum);
}
/* Much of this implementation derives from the formatting section detailed in
Beneath Apple DOS and Beneath Apple ProDOS, Chapter 3
- self-sync gaps are encoded as 10-bit nibbles decoded to 0xff by the
emulated system
- these gaps may differ depending on DOS vs ProDOS disk formatting
- sectors are are interleaved during nibbilization from their incoming
sequential order to account for a 'spinning' disk and to give programs
the time to decode data into memory before reading the next sector in
sequence
- in general DOS and ProDOS formatting is interchangeable at this level
*/
bool clem_2img_nibblize_data(struct Clemens2IMGDisk *disk) {
unsigned clem_max_sectors_per_region[CLEM_DISK_35_NUM_REGIONS];
unsigned clem_track_start_per_region[CLEM_DISK_35_NUM_REGIONS + 1];
unsigned self_sync_gap_1_cnt = 0;
unsigned self_sync_gap_2_cnt = 0;
unsigned self_sync_gap_3_cnt = 0;
const unsigned(*to_logical_sector_map)[16] = NULL;
unsigned num_regions = 0, current_region = 0;
unsigned track_increment = 0;
unsigned track_byte_offset;
unsigned logical_sector_index;
unsigned i, qtr_track_index;
uint8_t *nib_begin = disk->nib->bits_data;
uint8_t *nib_out = nib_begin;
unsigned data_in_size = disk->data_end - disk->data;
unsigned in_sector_size;
/* disk->nib->disk_type must be set before this call
gap values are derived from a mix of Beneath Apple DOS and ProDOS
sources and Ciderpress mentions (the 3.5" equivalents, which are
*very* hard to derive from existing documentation) */
switch (disk->nib->disk_type) {
case CLEM_DISK_TYPE_3_5:
if (disk->block_count > 0) {
if (disk->block_count == 280) {
return false;
} else if (disk->block_count == 800) {
disk->nib->is_double_sided = false;
} else if (disk->block_count == 1600) {
disk->nib->is_double_sided = true;
} else {
return false;
}
} else if (data_in_size == 800 * 1024) {
disk->nib->is_double_sided = true;
} else {
return false;
}
/* guesswork based on gap 3 size of 36 10-bit bytes and the */
self_sync_gap_1_cnt = (CLEM_DISK_35_BYTES_TRACK_GAP_1 * 8) / 10;
self_sync_gap_2_cnt = 4;
self_sync_gap_3_cnt = (CLEM_DISK_35_BYTES_TRACK_GAP_3 * 8) / 10;
num_regions = CLEM_DISK_35_NUM_REGIONS;
for (i = 0; i < num_regions; ++i) {
clem_max_sectors_per_region[i] = g_clem_max_sectors_per_region_35[i];
clem_track_start_per_region[i] = g_clem_track_start_per_region_35[i];
}
clem_track_start_per_region[num_regions] = (g_clem_track_start_per_region_35[num_regions]);
/* 80 tracks for single sided - is this ever true?, otherwise the
full track listing for double sided 3.5" disks */
disk->nib->bit_timing_ns = 2000;
disk->nib->track_count = disk->nib->is_double_sided ? 160 : 80;
track_increment = disk->nib->is_double_sided ? 1 : 2;
in_sector_size = 512;
break;
case CLEM_DISK_TYPE_5_25:
num_regions = 1;
clem_max_sectors_per_region[0] = CLEM_DISK_525_NUM_SECTORS_PER_TRACK;
clem_track_start_per_region[0] = 0;
clem_track_start_per_region[1] = CLEM_DISK_LIMIT_QTR_TRACKS;
/* From Beneath Apple DOS/ProDOS - evaluate if DOS values should
reflect those from Beneath Apple DOS. - anyway these are taken
from Ciderpress and ROM 03 ProDOS block formatting disassembly */
self_sync_gap_1_cnt = 64; /* somewhere between 12-85 */
self_sync_gap_2_cnt = 6; /* somewhere between 5- 10 */
self_sync_gap_3_cnt = 24; /* somewhere between 16-28 */
/* 40 tracks total - always 35 for DOS/ProDOS disks */
disk->nib->bit_timing_ns = 4000;
disk->nib->track_count = 35;
track_increment = 4;
in_sector_size = 256;
break;
default:
return false;
}
switch (disk->format) {
case CLEM_2IMG_FORMAT_PRODOS:
if (disk->nib->disk_type == CLEM_DISK_TYPE_3_5) {
to_logical_sector_map = prodos_to_logical_sector_map_35;
} else {
to_logical_sector_map = prodos_to_logical_sector_map_525;
}
break;
case CLEM_2IMG_FORMAT_DOS:
assert(disk->nib->disk_type == CLEM_DISK_TYPE_5_25);
to_logical_sector_map = dos_to_logical_sector_map_525;
break;
case CLEM_2IMG_FORMAT_RAW:
assert(false);
default:
return false;
}
/* encode nibbles using the specified image format layout */
track_byte_offset = 0;
logical_sector_index = 0;
/* clear out meta track map to its defaults, so that we can simply assign
track_indices to the meta track map for 3.5 and 5.25 disks versus
adding extra conditionals in the below loop
*/
memset(disk->nib->meta_track_map, 0xff, sizeof(disk->nib->meta_track_map));
memset(disk->nib->track_bits_count, 0x00, sizeof(disk->nib->track_bits_count));
memset(disk->nib->track_byte_count, 0x00, sizeof(disk->nib->track_byte_count));
memset(disk->nib->track_initialized, 0x00, sizeof(disk->nib->track_initialized));
for (qtr_track_index = 0; qtr_track_index < CLEM_DISK_LIMIT_QTR_TRACKS;) {
// isolate into a function for readability
unsigned sector_count = clem_max_sectors_per_region[current_region];
unsigned track_size;
uint8_t *data_in;
unsigned sector_index;
unsigned in_sector_index;
unsigned logical_track_index = qtr_track_index / 2;
unsigned side_index = qtr_track_index & 1;
unsigned nib_track_index = qtr_track_index / track_increment;
unsigned temp, byte_index;
struct ClemensNibEncoder nib_encoder;
if (disk->nib->disk_type == CLEM_DISK_TYPE_3_5) {
track_size = CLEM_DISK_35_CALC_BYTES_FROM_SECTORS(sector_count);
} else {
track_size = CLEM_DISK_525_BYTES_PER_TRACK;
}
if (disk->nib->bits_data + track_byte_offset + track_size > disk->nib->bits_data_end) {
assert(false);
return false;
}
disk->nib->track_byte_offset[nib_track_index] = track_byte_offset;
disk->nib->track_byte_count[nib_track_index] = track_size;
/* see clem_disk.h for documentation on the 3.5" prodos format */
_clem_nib_init_encoder(&nib_encoder, disk->nib->bits_data + track_byte_offset,
disk->nib->bits_data + track_byte_offset + track_size);
/* pad */
if (disk->nib->disk_type == CLEM_DISK_TYPE_3_5) {
_clem_nib_write_one(&nib_encoder, 0xff);
} else {
logical_track_index /= 2; // tracks 0 - 39
}
_clem_nib_encode_self_sync_ff(&nib_encoder, self_sync_gap_1_cnt);
for (sector_index = 0; sector_index < sector_count; ++sector_index) {
in_sector_index = to_logical_sector_map[current_region][sector_index];
data_in = disk->data + (logical_sector_index + in_sector_index) * in_sector_size;
if (disk->nib->disk_type == CLEM_DISK_TYPE_3_5) {
_clem_nib_write_one(&nib_encoder, 0xff);
}
/* Address */
_clem_nib_write_one(&nib_encoder, 0xd5);
_clem_nib_write_one(&nib_encoder, 0xaa);
_clem_nib_write_one(&nib_encoder, 0x96);
if (disk->nib->disk_type == CLEM_DISK_TYPE_3_5) {
/* track, sector, side, format (0x22 or 0x24 - assume 0x24?) */
/* I THINK 0x22 = 512 byte sectors */
/* - 0x24 is 524 byte sectors to allow for the 12 byte
empty tag
*/
unsigned side_index_35 = (side_index << 5) | (logical_track_index >> 6);
_clem_nib_encode_one_6_2(&nib_encoder, (uint8_t)(logical_track_index));
_clem_nib_encode_one_6_2(&nib_encoder, (uint8_t)(in_sector_index & 0xff));
_clem_nib_encode_one_6_2(&nib_encoder, (uint8_t)(side_index_35));
_clem_nib_encode_one_6_2(&nib_encoder, 0x24);
temp = (logical_track_index ^ in_sector_index ^ side_index_35 ^ 0x24);
_clem_nib_encode_one_6_2(&nib_encoder, (uint8_t)(temp));
_clem_nib_write_one(&nib_encoder, 0xde);
_clem_nib_write_one(&nib_encoder, 0xaa);
_clem_nib_write_one(&nib_encoder, 0xff);
} else {
_clem_nib_encode_one_4_4(&nib_encoder, (uint8_t)(disk->dos_volume & 0xff));
_clem_nib_encode_one_4_4(&nib_encoder, logical_track_index);
_clem_nib_encode_one_4_4(&nib_encoder, sector_index);
_clem_nib_encode_one_4_4(
&nib_encoder, (uint8_t)(disk->dos_volume ^ logical_track_index ^ sector_index));
_clem_nib_write_one(&nib_encoder, 0xde);
_clem_nib_write_one(&nib_encoder, 0xaa);
_clem_nib_write_one(&nib_encoder, 0xeb);
}
_clem_nib_encode_self_sync_ff(&nib_encoder, self_sync_gap_2_cnt);
/* Data */
if (disk->nib->disk_type == CLEM_DISK_TYPE_3_5) {
_clem_nib_write_one(&nib_encoder, 0xff);
}
_clem_nib_write_one(&nib_encoder, 0xd5);
_clem_nib_write_one(&nib_encoder, 0xaa);
_clem_nib_write_one(&nib_encoder, 0xad);
if (disk->nib->disk_type == CLEM_DISK_TYPE_3_5) {
_clem_nib_encode_one_6_2(&nib_encoder, (uint8_t)in_sector_index);
/* TODO: handle cases where input sector data is 524 vs 512 */
_clem_nib_encode_data_35(&nib_encoder, data_in, 512);
_clem_nib_write_one(&nib_encoder, 0xde);
_clem_nib_write_one(&nib_encoder, 0xaa);
if (sector_index < sector_count - 1) {
_clem_nib_write_one(&nib_encoder, 0xff);
_clem_nib_write_one(&nib_encoder, 0xff);
_clem_nib_write_one(&nib_encoder, 0xff);
}
} else {
_clem_nib_encode_data_525(&nib_encoder, data_in, 256);
_clem_nib_write_one(&nib_encoder, 0xde);
_clem_nib_write_one(&nib_encoder, 0xaa);
_clem_nib_write_one(&nib_encoder, 0xeb);
}
if (sector_index + 1 < sector_count) {
_clem_nib_encode_self_sync_ff(&nib_encoder, self_sync_gap_3_cnt);
}
}
logical_sector_index += sector_count;
disk->nib->track_bits_count[nib_track_index] = nib_encoder.bit_index_end;
disk->nib->track_initialized[nib_track_index] = 1;
if (disk->nib->disk_type == CLEM_DISK_TYPE_3_5) {
disk->nib->meta_track_map[qtr_track_index] = nib_track_index;
} else {
disk->nib->meta_track_map[qtr_track_index] = nib_track_index;
if (qtr_track_index > 0) {
/* track is copied onto the one quarter track before and after
this qtr track */
disk->nib->meta_track_map[qtr_track_index - 1] = nib_track_index;
}
disk->nib->meta_track_map[(qtr_track_index + 1) % CLEM_DISK_LIMIT_QTR_TRACKS] =
nib_track_index;
}
if (nib_track_index + 1 >= disk->nib->track_count) {
track_increment = CLEM_DISK_LIMIT_QTR_TRACKS - nib_track_index;
}
qtr_track_index += track_increment;
if (qtr_track_index >= clem_track_start_per_region[current_region + 1]) {
++current_region;
}
track_byte_offset += track_size;
}
return true;
}