-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscl2trd.c
200 lines (173 loc) · 4.47 KB
/
scl2trd.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
#include <input.h>
#include <stdlib.h>
#include <string.h>
#include "lib/textUtils.h"
#include "lib/esxdos.h"
#define PATH_SIZE ( 200 )
uint8_t filePath[ PATH_SIZE ];
uint16_t drive;
uint16_t iStream;
uint16_t oStream;
uint8_t buff[256];
unsigned freeTrack = 1;
unsigned freeSec = 0;
unsigned char count;
unsigned char isFull = 0;
unsigned int totalFreeSect = 2544;
void cleanBuffer()
{
for (uint16_t i=0;i<256;buff[i++] = 0);
}
void showMessage(char *e)
{
textUtils_cls();
textUtils_println( e );
textUtils_println( "Press any key..." );
waitKeyPress();
}
void writeDiskData()
{
uint16_t r = ESXDOS_fread(&buff, 256, iStream);
while (r == 256) {
ESXDOS_fwrite(&buff, r, oStream);
r = ESXDOS_fread(&buff, 256, iStream);
}
if (isFull) {
cleanBuffer();
for (r=0;r<totalFreeSect;r++)
ESXDOS_fwrite(&buff, 256, oStream);
}
iferror {
showMessage("Issue with writing disk data to TRD-file");
return;
}
ESXDOS_fclose(iStream);
ESXDOS_fclose(oStream);
textUtils_println(" * All data written!");
textUtils_println("");
textUtils_println(" TRD file is stored and ready to use!");
textUtils_println("");
textUtils_println(" Press any key to convert other SCL-file");
waitKeyPress();
}
void writeDiskInfo()
{
cleanBuffer();
buff[0xe3] = 0x16; // IMPORTANT! 80 track double sided
buff[0xe4] = count;
buff[0xe1] = freeSec;
buff[0xe2] = freeTrack;
if (isFull) {
buff[0xe6] = totalFreeSect / 256;
buff[0xe5] = totalFreeSect & 255;
}
buff[0xe7] = 0x10;
buff[0xf5] = 's';
buff[0xf6] = 'c';
buff[0xf7] = 'l';
buff[0xf8] = '2';
buff[0xf9] = 't';
buff[0xfa] = 'r';
buff[0xfb] = 'd';
ESXDOS_fwrite(&buff, 256, oStream);
ESXDOS_fwrite(0, 1792, oStream); // Any dirt is ok
iferror {
showMessage("Issue with writing disk info to TRD-file");
return;
}
textUtils_println(" * Disk information written");
writeDiskData();
}
void writeCatalog()
{
uint8_t i;
totalFreeSect = 2544;
freeTrack = 1;
freeSec = 0;
count = 0;
oStream = ESXDOS_fopen(filePath, ESXDOS_FILEMODE_WRITE_CREATE, drive);
iferror {
showMessage("Can't open output file");
return ;
}
ESXDOS_fread(&count, 1, iStream);
for (i=0;i<count; i++) {
ESXDOS_fread(&buff, 14, iStream);
buff[14] = freeSec;
buff[15] = freeTrack;
freeSec += buff[0xd];
freeTrack += freeSec / 16;
totalFreeSect -= (int) buff[0xd];
freeSec = freeSec % 16;
ESXDOS_fwrite(&buff, 16, oStream);
}
cleanBuffer();
for (i = count;i<128;i++) ESXDOS_fwrite(&buff, 16, oStream);
iferror {
showMessage("Issue with writing catalog to TRD-file");
return;
}
textUtils_println(" * Disk catalog written");
writeDiskInfo();
}
void validateScl()
{
unsigned char *expected[9] = "SINCLAIR";
drive = ESXDOS_getDefaultDrive();
iStream = ESXDOS_fopen(filePath, ESXDOS_FILEMODE_READ, drive );
iferror {
showMessage("Can't open input file");
return ;
}
cleanBuffer();
ESXDOS_fread(&buff, 8, iStream);
if (strcmp(expected, &buff)) {
showMessage("Wrong file! Select only SCL files");
return;
}
sprintf(strstr(filePath, ".SCL"), ".TRD");
textUtils_println(" * File is valid SCL");
writeCatalog();
}
void selectFile()
{
char c;
textUtils_cls();
textUtils_setAttributes( INK_BLUE | PAPER_BLACK );
fileDialogpathUpOneDir( filePath );
while (
openFileDialog(
"SCL2TRD by Nihirash v. 1.1.0",
"<Cursor/Sincl> - movement <Ent/0> - select file <Space> - exit",
filePath,
PATH_SIZE,
INK_BLUE | PAPER_WHITE,
INK_WHITE | PAPER_BLACK
) == false ) {
__asm
rst 0
__endasm;
}
textUtils_cls();
textUtils_println("");
textUtils_print("Do you want full TRD-file? (y/n) ");
while (!(c == 'y' || c == 'n')) {
c = waitKeyPress();
}
isFull = (c == 'y');
textUtils_println(isFull ? "yes" : "no");
textUtils_println(" Converting file!");
validateScl();
}
void main(void)
{
sprintf( filePath, "/" );
textUtils_64ColumnsMode();
zx_border( INK_BLACK );
while (1)
{
selectFile();
ESXDOS_fclose(iStream);
ESXDOS_fclose(oStream);
}
}