/[projet1]/public/pc/tools/osdk/main/header/sources/header.cpp
Defence Force logotype

Contents of /public/pc/tools/osdk/main/header/sources/header.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1132 - (show annotations)
Sun May 11 09:59:38 2014 UTC (5 years, 6 months ago) by dbug
File size: 6805 byte(s)
Header 0.2 - 2014/05/11
- The address can now be specified with either $ or 0x as an hexadecimal prefix

1 /*==============================================================================
2
3 Header
4
5 ==[Description]=================================================================
6
7 This program will add a oric header to any binary file
8
9 ==[History]=====================================================================
10
11
12 ==[ToDo]========================================================================
13
14
15 ==============================================================================*/
16
17 #include "infos.h"
18
19 #include "common.h"
20
21 #include <memory.h>
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <fcntl.h>
25 #include <sys/stat.h>
26
27 #ifdef _WIN32
28 #include <conio.h>
29 #include <io.h>
30 #else
31 #include <sys/stat.h>
32 #define _open open
33 #define _read read
34 #define _write write
35 #define _close close
36 #define _O_TRUNC O_TRUNC
37 #define _O_CREAT O_CREAT
38 #define _S_IREAD S_IREAD
39 #define _S_IWRITE S_IWRITE
40 #endif
41
42 unsigned char Header[]=
43 {
44 0x16, // 0 Synchro
45 0x16, // 1
46 0x16, // 2
47 0x24, // 3
48
49 0x00, // 4
50 0x00, // 5
51
52 0x80, // 6
53
54 0x00, // 7 $80=BASIC Autostart $C7=Assembly autostart
55
56 0xbf, // 8 End adress
57 0x40, // 9
58
59 0xa0, // 10 Start adress
60 0x00, // 11
61
62 0x00, // 12
63 0x00 // 12
64 };
65
66
67
68
69 /**
70 * argv[1] - Original filename (headerless raw binary)
71 * argv[2] - Destination filename (tape file)
72 * argv[3] - Start adress
73 */
74 int main(int argc,char *argv[])
75 {
76 //
77 // Some initialization for the common library
78 //
79 SetApplicationParameters(
80 "Header",
81 TOOL_VERSION_MAJOR,
82 TOOL_VERSION_MINOR,
83 "{ApplicationName} - Version {ApplicationVersion} - This program is a part of the OSDK\r\n"
84 "\r\n"
85 "Author:\r\n"
86 " Mickael Pointier (aka Dbug)\r\n"
87 " dbug@defence-force.org\r\n"
88 " http://www.defence-force.org\r\n"
89 "\r\n"
90 "Purpose:\r\n"
91 " Add a header to a raw binary file in order to make it loadable by an\r\n"
92 " oric using the CLOAD command.\r\n"
93 "\r\n"
94 "Parameters:\r\n"
95 " <options> <sourcefile> <destinationfile> <loadadress>\r\n"
96 " hexadecimal address should be prefixed by a $ or 0x symbol\r\n"
97 " and should not be present in -h0 mode\r\n"
98 "\r\n"
99 "Options:\r\n"
100 " -a[0/1] for autorun (1) or non autorun (0)\r\n"
101 " -h[0/1] for header (1) or no header (0)\r\n"
102 " -s[0/1] for showing size of file (1) or not (0)\r\n"
103 "\r\n"
104 "Exemple:\r\n"
105 " {ApplicationName} -a1 final.out osdk.tap $500\r\n"
106 " {ApplicationName} -a1 h0 final.out osdk.tap\r\n"
107 );
108
109 bool flag_auto=true;
110 bool flag_header=true;
111 bool flag_display_size=true;
112
113 ArgumentParser cArgumentParser(argc,argv);
114
115 while (cArgumentParser.ProcessNextArgument())
116 {
117 if (cArgumentParser.IsSwitch("-a"))
118 {
119 //format: [-a]
120 // 0 => non auto
121 // 1 => perform auto run
122 flag_auto=cArgumentParser.GetBooleanValue(true);
123 }
124 else
125 if (cArgumentParser.IsSwitch("-h"))
126 {
127 //format: [-h]
128 // 0 => suppress header
129 // 1 => save header (default)
130 flag_header=cArgumentParser.GetBooleanValue(true);
131 }
132 else
133 if (cArgumentParser.IsSwitch("-s"))
134 {
135 //format: [-s]
136 // 0 => suppress display of size
137 // 1 => show size of generated file (default)
138 flag_display_size=cArgumentParser.GetBooleanValue(true);
139 }
140 }
141
142
143 int adress_start=0;
144 if (flag_header)
145 {
146 if (cArgumentParser.GetParameterCount()!=3)
147 {
148 //
149 // Wrong number of arguments
150 //
151 ShowError(0);
152 }
153
154 //
155 // Check start address
156 //
157 adress_start=ConvertAdress(cArgumentParser.GetParameter(2));
158 }
159 else
160 {
161 if (cArgumentParser.GetParameterCount()!=2)
162 {
163 //
164 // Wrong number of arguments
165 //
166 ShowError(0);
167 }
168 }
169
170
171
172 //
173 // Read file
174 //
175 const char *filename_src=cArgumentParser.GetParameter(0);
176 int filesize = 0;
177 #ifdef _WIN32
178 struct _finddata_t file_info;
179 if (_findfirst(filename_src, &file_info)== -1)
180 {
181 ShowError("file not found");
182 }
183 filesize = file_info.size;
184 #else
185 struct stat st;
186 stat(filename_src, &st);
187 filesize = st.st_size;
188 #endif
189 int handle_src=_open(filename_src,O_BINARY|O_RDONLY,0);
190 if (handle_src==-1)
191 {
192 ShowError("unable to open source file");
193 }
194
195 void *ptr_buf=malloc(filesize);
196 if (!ptr_buf)
197 {
198 ShowError("not enough memory");
199 }
200
201 //
202 // Read source data
203 //
204 _read(handle_src,ptr_buf,filesize);
205
206
207 //
208 // Write file
209 //
210 const char *filename_dst=cArgumentParser.GetParameter(1);
211 int handle_dst=_open(filename_dst,O_BINARY|O_WRONLY|_O_TRUNC|_O_CREAT,_S_IREAD|_S_IWRITE);
212 if (handle_dst==-1)
213 {
214 ShowError("unable to create destination file");
215 }
216
217 size_t size_header;
218 if (flag_header)
219 {
220 size_header=sizeof(Header);
221
222 //adress_start=0x800;
223 int adress_end =adress_start+filesize-1;
224 //flag_auto=true;
225
226 if (flag_auto) Header[7]=0xC7;
227 else Header[7]=0;
228
229 Header[10]=(adress_start>>8);
230 Header[11]=(adress_start&255);
231
232 Header[8]=(adress_end>>8);
233 Header[9]=(adress_end&255);
234
235 //
236 // Write header
237 //
238 _write(handle_dst,Header,size_header);
239
240 //
241 // Copy the file
242 //
243 _write(handle_dst,ptr_buf,filesize);
244 }
245 else
246 {
247 // Suppress header mode.
248 size_header=0;
249
250 char *ptr_header=(char*)ptr_buf;
251
252 // Skip synchronization bytes
253 // 16 16 16 24
254 while (*ptr_header==0x16) ptr_header++;
255 if (*ptr_header!=0x24)
256 {
257 ShowError("bad header");
258 }
259 ptr_header++;
260
261 // FF FF (or 00 00)
262 ptr_header++;
263 ptr_header++;
264
265 // Flags (80 00)
266 ptr_header++;
267 ptr_header++;
268
269 // End adress
270 ptr_header++;
271 ptr_header++;
272
273 // Start adress
274 ptr_header++;
275 ptr_header++;
276
277 // Separator (FF or 00)
278 ptr_header++;
279
280 // Filename (null terminated)
281 while (*ptr_header!=0) ptr_header++;
282
283 // Null terminator skip
284 ptr_header++;
285
286 // Recompute size (without header data)
287 filesize-=ptr_header-(char*)ptr_buf;
288
289 //
290 // Copy the file
291 //
292 _write(handle_dst,ptr_header,filesize);
293 }
294
295 //
296 // Close everything
297 //
298 _close(handle_dst);
299 _close(handle_src);
300 free(ptr_buf);
301
302
303 //
304 // Eventually show some information
305 //
306 if (flag_display_size)
307 {
308 printf("File '%s' is %d bytes long (%d bytes header and %d bytes of data)\n",filename_dst,size_header+filesize,size_header,filesize);
309 }
310
311
312 exit(0);
313 }
314
315
316
317
318 /*
319 16 16 16 24 // Synchro
320
321 FF FF (ou 00 00)
322
323 80 00
324
325 B8 00 // End adress
326 B4 00 // Start adress
327
328 FF (or 00)
329
330 41 53 4D 32 4B 32 2E 54 41 50 00 // Filename
331 */
332 /*
333 unsigned char Header[]=
334 {
335 0x16, // 0
336 0x16, // 1
337 0x16, // 2
338 0x24, // 3
339
340 0x00, // 4
341 0x00, // 5
342
343 0x80, // 6
344
345 0x00, // 7
346
347 0xbf, // 8
348 0x40, // 9
349
350 0xa0, // 10
351 0x00, // 11
352
353 0x00, // 12
354 0x00 // 12
355 };
356 */
357
358
359

  ViewVC Help
Powered by ViewVC 1.1.26