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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1480 - (show annotations)
Mon Jun 25 17:06:22 2018 UTC (21 months, 2 weeks ago) by dbug
File size: 10653 byte(s)
Bas2Tap 1.2
- Support for pure comments without line numbers (accepts lines starting by ' or ; or //)

1
2 #include "infos.h"
3
4 #include "common.h"
5
6 #include <stdlib.h>
7 #include <stdio.h>
8 #ifdef _WIN32
9 #include <conio.h>
10 #else
11 #define memicmp strncasecmp
12 #endif
13 #include <string.h>
14
15 //
16 // Things to do:
17 // - Handle auto-numeration and labels
18 // - Option to optimize the programs (truncate variables to two characters, remove comments)
19 //
20
21 const char *keywords[]=
22 {
23 // 128-246: BASIC keywords
24 "END","EDIT","STORE","RECALL","TRON","TROFF","POP","PLOT",
25 "PULL","LORES","DOKE","REPEAT","UNTIL","FOR","LLIST","LPRINT","NEXT","DATA",
26 "INPUT","DIM","CLS","READ","LET","GOTO","RUN","IF","RESTORE","GOSUB","RETURN",
27 "REM","HIMEM","GRAB","RELEASE","TEXT","HIRES","SHOOT","EXPLODE","ZAP","PING",
28 "SOUND","MUSIC","PLAY","CURSET","CURMOV","DRAW","CIRCLE","PATTERN","FILL",
29 "CHAR","PAPER","INK","STOP","ON","WAIT","CLOAD","CSAVE","DEF","POKE","PRINT",
30 "CONT","LIST","CLEAR","GET","CALL","!","NEW","TAB(","TO","FN","SPC(","@",
31 "AUTO","ELSE","THEN","NOT","STEP","+","-","*","/","^","AND","OR",">","=","<",
32 "SGN","INT","ABS","USR","FRE","POS","HEX$","&","SQR","RND","LN","EXP","COS",
33 "SIN","TAN","ATN","PEEK","DEEK","LOG","LEN","STR$","VAL","ASC","CHR$","PI",
34 "TRUE","FALSE","KEY$","SCRN","POINT","LEFT$","RIGHT$","MID$"
35 // 247- : Error messages
36 };
37
38 unsigned char head[14]={ 0x16,0x16,0x16,0x24,0,0,0,0,0,0,5,1,0,0 };
39
40
41
42
43 void Tap2Bas(unsigned char *ptr_buffer,size_t file_size,const char *destFile)
44 {
45 unsigned int i, car;
46
47 FILE *out = stdout;
48 if (destFile && (strlen(destFile) != 0))
49 {
50 out = fopen(destFile, "wb");
51 }
52 if (out == NULL)
53 {
54 printf("Can't open file for writing\n");
55 exit(1);
56 }
57
58
59 if (ptr_buffer[0]!=0x16 || ptr_buffer[3]!=0x24)
60 {
61 ShowError("Not an Oric file");
62 }
63 if (ptr_buffer[6])
64 {
65 ShowError("Not a BASIC file");
66 }
67 i=13;
68 while (ptr_buffer[i++]);
69 while (ptr_buffer[i] || ptr_buffer[i+1])
70 {
71 i+=2;
72 fprintf(out," %u ",ptr_buffer[i]+(ptr_buffer[i+1]<<8));
73 i+=2;
74 while ((car=ptr_buffer[i++]))
75 {
76 if (car<128)
77 fputc(car,out);
78 else
79 if (car < 247)
80 {
81 fprintf(out,"%s", keywords[car - 128]);
82 }
83 else
84 {
85 // Probably corrupted listing
86 // 247 : NEXT WITHOUT FOR
87 fprintf(out,"CORRUPTED_ERROR_CODE_%u", car);
88 }
89 }
90 fputc('\r',out);
91 fputc('\n', out);
92 }
93
94 fclose(out);
95 }
96
97
98
99 // tap2bas
100 int search_keyword(const char *str)
101 {
102 for (unsigned int i=0;i<sizeof(keywords)/sizeof(char *);i++)
103 {
104 if (strncmp(keywords[i],str,strlen(keywords[i]))==0)
105 {
106 return i;
107 }
108 }
109 return -1;
110 }
111
112
113
114 void Bas2Tap(const char *sourceFile,const char *destFile,bool autoRun,bool useColor)
115 {
116 unsigned char buf[48192];
117 unsigned int end, lastptr, adr;
118 int j,ptr,keyw,string,rem,data;
119
120 // Mike: Need to improve the parsing of this with a global function to split
121 // a text file in separate lines.
122 std::vector<std::string> textData;
123 if (!LoadText(sourceFile,textData))
124 {
125 ShowError("Unable to load source file");
126 }
127
128 std::string currentFile=sourceFile;
129 int currentLineNumber=0;
130 int i=0;
131 std::vector<std::string>::const_iterator lineIt=textData.begin();
132 while (lineIt!=textData.end())
133 {
134 const std::string& currentLine=StringTrim(*lineIt);
135 ++lineIt;
136 ++currentLineNumber;
137
138 if (!currentLine.empty())
139 {
140 const char* ligne=currentLine.c_str();
141 if (ligne[0]=='#')
142 {
143 // Preprocessor directive
144 if (memicmp(ligne,"#file",5)==0)
145 {
146 //"#file font.BAS""
147 // Very approximative "get the name of the file and reset the line counter" code.
148 // Will clean up that when I will have some more time.
149 ligne+=5;
150 currentFile=ligne;
151 currentLineNumber=0;
152 }
153 else
154 {
155 ShowError("Unknown preprocessor directive in file %s line number line %d",currentFile.c_str(),currentLineNumber);
156 }
157 }
158 else
159 {
160 int number=get_value(ligne,-1);
161 if (number<0)
162 {
163 char car = *ligne++;
164 if (car != 0)
165 {
166 char car2 = *ligne++;
167 if ((car == '\'') || (car == ';') || ( (car == '/') && (car2 == '/')) )
168 {
169 // We accept the usual C, Assembler and BASIC comments are actual comments that do not count as normal lines
170 // Technically we could have used a decent pre-processor, or even a full file filter, but I'm aiming at "more bangs for the bucks" approach.
171 // If necessary we can refactor later
172 continue;
173 }
174 }
175
176 // Mike: Need to add better diagnostic here
177 ShowError("Missing line number in file %s line %d",currentFile.c_str(),currentLineNumber);
178 break;
179 }
180
181 // Standard line
182 buf[i++] = 0;
183 buf[i++] = 0;
184
185 buf[i++]=number&0xFF;
186 buf[i++]=number>>8;
187
188 ptr=0;
189 rem=0;
190 bool color=useColor;
191 string=0;
192 data=0;
193 if (ligne[ptr]==' ') ptr++;
194
195 while (ligne[ptr])
196 {
197 if (rem)
198 {
199 if (color)
200 {
201 color=false;
202 buf[i++]=27; // ESCAPE
203 buf[i++]='B'; // GREEN labels
204 }
205 buf[i++]=ligne[ptr++];
206 }
207 else
208 if (string)
209 {
210 if (ligne[ptr]=='"') string=0;
211 buf[i++]=ligne[ptr++];
212 }
213 else
214 if (data)
215 {
216 if (ligne[ptr]==':') data=0;
217 buf[i++]=ligne[ptr++];
218 }
219 else
220 {
221 const char* pLine=(ligne+ptr);
222 keyw=search_keyword(pLine);
223 if (keyw==29 || ligne[ptr]=='\'') rem=1;
224 if (keyw==17) data=1;
225 if (ligne[ptr]=='"') string=1;
226 if (keyw>=0)
227 {
228 buf[i++]=keyw+128;
229 ptr+=strlen(keywords[keyw]);
230 }
231 else
232 {
233 buf[i++]=ligne[ptr++];
234 }
235 }
236 }
237 buf[i++]=0;
238 }
239 }
240 }
241 buf[i++]=0;
242 buf[i++]=0;
243
244 //following line modified by Wilfrid AVRILLON (Waskol) 06/20/2009
245 //It should follow this rule of computation : End_Address=Start_Address+File_Size-1
246 //Let's assume a 1 byte program, it starts at address #501 and ends at address #501 (Address=Address+1-1) !
247 //It was a blocking issue for various utilities (tap2wav for instance)
248 //end=0x501+i-1; //end=0x501+i;
249 end=0x501+i;
250
251 if (autoRun) head[7]=0x80; // Autorun for basic :)
252 else head[7]=0;
253
254 head[8]=end>>8;
255 head[9]=end&0xFF;
256
257 for(j=4,lastptr=0;j<i;j++)
258 {
259 if (buf[j]==0)
260 {
261 adr=0x501+j+1;
262 buf[lastptr]=adr&0xFF;
263 buf[lastptr+1]=adr>>8;
264 lastptr=j+1;
265 j+=4;
266 }
267 }
268
269 //
270 // Save file
271 //
272 FILE *out=fopen(destFile,"wb");
273 if (out==NULL)
274 {
275 printf("Can't open file for writing\n");
276 exit(1);
277 }
278 fwrite(head,1,13,out);
279 // write the name
280 if (currentFile.length() > 0)
281 {
282 char *currentFileDup = strdup(currentFile.c_str());
283 char *fileName = currentFileDup;
284 // only take the file name from the path
285 // try to find '\\'
286 char *lastsep = strrchr(fileName, '\\');
287 if (lastsep != NULL)
288 {
289 // if there is something after the separator
290 if (lastsep + 1 != 0)
291 fileName = lastsep + 1;
292 }
293 else
294 {
295 // try to find /
296 lastsep = strrchr(fileName, '/');
297 if (lastsep != NULL)
298 {
299 // if there is something after the separator
300 if (lastsep + 1 != 0)
301 fileName = lastsep + 1;
302 }
303 }
304 // remove the extension if there is one
305 char *lastdot = strrchr(fileName, '.');
306 if (lastdot != NULL)
307 *lastdot = 0;
308 fwrite(fileName, 1, strlen(fileName), out);
309 free(currentFileDup);
310 }
311 fwrite("\x00", 1, 1, out);
312 fwrite(buf,1,i+1,out);
313 // oricutron bug work around
314 //fwrite("\x00", 1, 1, out);
315 fclose(out);
316 }
317
318
319
320
321 #define NB_ARG 2
322
323 int main(int argc, char **argv)
324 {
325 //
326 // Some initialization for the common library
327 //
328 SetApplicationParameters(
329 "Bas2Tap",
330 TOOL_VERSION_MAJOR,
331 TOOL_VERSION_MINOR,
332 "{ApplicationName} - Version {ApplicationVersion} - This program is a part of the OSDK\r\n"
333 "\r\n"
334 "Author:\r\n"
335 " Fabrice Frances \r\n"
336 "\r\n"
337 "Purpose:\r\n"
338 " Converting a text file containing a BASIC source code to a binary\r\n"
339 " encoded TAPE file that can be loaded using the CLOAD command.\r\n"
340 " (and the opposite operation as well).\r\n"
341 "\r\n"
342 "Parameters:\r\n"
343 " <options> <sourcefile> <destinationfile>\r\n"
344 "\r\n"
345 "Options:\r\n"
346 " -b2t[0|1] for converting to tape format with autorun (1) or not (0)\r\n"
347 " -t2b for converting from tape format text\r\n"
348 " -color[0|1] for enabling colored comments"
349 "\r\n"
350 "Example:\r\n"
351 " {ApplicationName} -b2t1 final.txt osdk.tap\r\n"
352 " {ApplicationName} -t2b osdk.tap program.txt\r\n"
353 );
354
355 bool basicToTape=true;
356 bool autoRun=true;
357 bool useColor=false;
358
359 ArgumentParser argumentParser(argc,argv);
360
361 while (argumentParser.ProcessNextArgument())
362 {
363 if (argumentParser.IsSwitch("-t2b"))
364 {
365 // Tape to BASIC source code
366 basicToTape=false;
367 }
368 else
369 if (argumentParser.IsSwitch("-b2t"))
370 {
371 // BASIC source code to tape
372 basicToTape=true;
373 autoRun=argumentParser.GetBooleanValue(true);
374 }
375 else
376 if (argumentParser.IsSwitch("-color"))
377 {
378 // Handling of color codes
379 useColor=argumentParser.GetBooleanValue(false);
380 }
381 }
382
383 if (argumentParser.GetParameterCount()!=NB_ARG)
384 {
385 ShowError(0);
386 }
387
388
389 std::string nameSrc(argumentParser.GetParameter(0));
390 std::string nameDst(argumentParser.GetParameter(1));
391
392 if (basicToTape)
393 {
394 Bas2Tap(nameSrc.c_str(),nameDst.c_str(),autoRun,useColor);
395 }
396 else
397 {
398 // Load the source file
399 void* ptr_buffer_void;
400 size_t file_size;
401 if (!LoadFile(nameSrc.c_str(),ptr_buffer_void,file_size))
402 {
403 ShowError("Unable to load the source file");
404 }
405 unsigned char *ptr_buffer=(unsigned char*)ptr_buffer_void;
406
407 Tap2Bas(ptr_buffer,file_size,nameDst.c_str());
408 }
409
410 exit(0);
411 }

  ViewVC Help
Powered by ViewVC 1.1.26