-
Notifications
You must be signed in to change notification settings - Fork 1
/
cpcemu.c
130 lines (95 loc) · 3.32 KB
/
cpcemu.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
#include "cpcemu.h"
#include <assert.h>
#include <string.h>
#include "platformdef.h"
const char *CPCEMU_HEADER_STD = "MV - CPCEMU Disk-File\r\nDisk-Info\r\n";
const char *CPCEMU_HEADER_EX = "EXTENDED CPC DSK File\r\nDisk-Info\r\n";
const char *CPCEMU_CREATOR = "AMS-DSK\r\n";
const char *CPCEMU_TRACK_HEADER = "Track-Info\r\n";
u8 g_sector_skew_table[NUM_SECTOR];
void read_disc_info(FILE *fp, struct cpcemu_disc_info_s *info)
{
assert(fp);
assert(info);
fseek(fp, 0, SEEK_SET);
fread(info, 1, sizeof(*info), fp);
}
void write_disc_info(FILE *fp, struct cpcemu_disc_info_s *info)
{
assert(fp);
assert(info);
fseek(fp, 0, SEEK_SET);
fwrite(info, 1, sizeof(*info), fp);
}
void read_track_info(FILE *fp, u8 track, struct cpcemu_track_info_s *track_info)
{
int offset_track;
assert(fp);
assert(track_info);
offset_track = CPCEMU_INFO_OFFSET + (track * SIZ_TRACK);
fseek(fp, offset_track, SEEK_SET);
fread(track_info, 1, sizeof(struct cpcemu_track_info_s), fp);
}
void write_track_info(FILE *fp, u8 track, struct cpcemu_track_info_s *track_info)
{
int offset_track;
assert(fp);
assert(track_info);
offset_track = CPCEMU_INFO_OFFSET + (track * SIZ_TRACK);
fseek(fp, offset_track, SEEK_SET);
fwrite(track_info, 1, sizeof(struct cpcemu_track_info_s), fp);
}
int check_disk_type(FILE *fp, u8 sector_id)
{
struct cpcemu_track_info_s track_info;
assert(fp);
read_track_info(fp, 0, &track_info);
return sector_id <= track_info.sector_info_table[0].sector_id
&& (sector_id + NUM_SECTOR) >= track_info.sector_info_table[0].sector_id; /* ?? */
}
int check_extended(FILE *fp)
{
struct cpcemu_disc_info_s disc_info;
assert(fp);
read_disc_info(fp, &disc_info);
return strncmp("EXTENDED", disc_info.header, 8) == 0;
}
#if 0
void read_physical_sector(FILE *fp, u8 track, u8 sector, u8 buffer[SIZ_SECTOR])
{
int offset_track;
int offset_sector;
offset_track = CPCEMU_INFO_OFFSET + (track * SIZ_TRACK);
offset_sector = CPCEMU_TRACK_OFFSET + sector * SIZ_SECTOR;
fseek(fp, offset_track + offset_sector, SEEK_SET);
fread(buffer, 1, SIZ_SECTOR, fp);
}
#endif
void read_logical_sector(FILE *fp, u8 track, u8 sector, u8 buffer[SIZ_SECTOR])
{
int offset_track;
int logical_sector;
int offset_sector;
assert(track < NUM_TRACK);
assert(sector < NUM_SECTOR);
/* printf("DEBUG - read_logical_sector (track: %d, sector: %d)\n", track, sector); */
offset_track = CPCEMU_INFO_OFFSET + (track * SIZ_TRACK);
logical_sector = g_sector_skew_table[sector];
offset_sector = CPCEMU_TRACK_OFFSET + logical_sector * SIZ_SECTOR;
fseek(fp, offset_track + offset_sector, SEEK_SET);
fread(buffer, 1, SIZ_SECTOR, fp);
}
void write_logical_sector(FILE *fp, u8 track, u8 sector, u8 buffer[SIZ_SECTOR])
{
int offset_track;
int logical_sector;
int offset_sector;
assert(track < NUM_TRACK);
assert(sector < NUM_SECTOR);
/* printf("DEBUG - write_logical_sector (track: %d, sector: %d)\n", track, sector); */
offset_track = CPCEMU_INFO_OFFSET + (track * SIZ_TRACK);
logical_sector = g_sector_skew_table[sector];
offset_sector = CPCEMU_TRACK_OFFSET + logical_sector * SIZ_SECTOR;
fseek(fp, offset_track + offset_sector, SEEK_SET);
fwrite(buffer, 1, SIZ_SECTOR, fp);
}