ugBASIC 1.18
An isomorphic BASIC language compiler for retrocomputers
Loading...
Searching...
No Matches
_build.c
Go to the documentation of this file.
1/*****************************************************************************
2 * ugBASIC - an isomorphic BASIC language compiler for retrocomputers *
3 *****************************************************************************
4 * Copyright 2021-2026 Marco Spedaletti (asimov@mclink.it)
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *----------------------------------------------------------------------------
18 * Concesso in licenza secondo i termini della Licenza Apache, versione 2.0
19 * (la "Licenza"); è proibito usare questo file se non in conformità alla
20 * Licenza. Una copia della Licenza è disponibile all'indirizzo:
21 *
22 * http://www.apache.org/licenses/LICENSE-2.0
23 *
24 * Se non richiesto dalla legislazione vigente o concordato per iscritto,
25 * il software distribuito nei termini della Licenza è distribuito
26 * "COSÌ COM'È", SENZA GARANZIE O CONDIZIONI DI ALCUN TIPO, esplicite o
27 * implicite. Consultare la Licenza per il testo specifico che regola le
28 * autorizzazioni e le limitazioni previste dalla medesima.
29 ****************************************************************************/
30
31/****************************************************************************
32 * INCLUDE SECTION
33 ****************************************************************************/
34
35#include "../../ugbc.h"
36
37/****************************************************************************
38 * CODE SECTION
39 ****************************************************************************/
40
41extern char OUTPUT_FILE_TYPE_AS_STRING[][16];
42
43void generate_bin( Environment * _environment ) {
44
45 char exeFileName[8*MAX_TEMPORARY_STORAGE];
46 char commandLine[8*MAX_TEMPORARY_STORAGE];
47 char executableName[MAX_TEMPORARY_STORAGE];
48 char listingFileName[MAX_TEMPORARY_STORAGE];
49 char binaryName[MAX_TEMPORARY_STORAGE];
50
51 BUILD_SAFE_REMOVE( _environment, _environment->exeFileName );
52
53 strcopy( exeFileName, _environment->exeFileName );
54
55 strcopy( binaryName, _environment->exeFileName );
56 char * p = strstr( binaryName, ".dsk" );
57 if ( p ) {
58 strcopy( p, ".bin" );
59 }
60 strcopy( _environment->exeFileName, binaryName );
61
62 BUILD_TOOLCHAIN_ASM6809_GET_EXECUTABLE( _environment, executableName );
63
64 BUILD_TOOLCHAIN_ASM6809_GET_LISTING_FILE( _environment, listingFileName );
65
66 BUILD_TOOLCHAIN_ASM6809EXEC( _environment, "-3 -C", 0x2A00, executableName, listingFileName );
67
68 if ( _environment->listingFileName ) {
69
70 if ( _environment->profileFileName && _environment->profileCycles ) {
71 if ( _environment->executerFileName ) {
72 sprintf(executableName, "%s", _environment->executerFileName );
73 } else if( access( "run6809.exe", F_OK ) == 0 ) {
74 sprintf(executableName, "%s", "run6809.exe" );
75 } else {
76 sprintf(executableName, "%s", "run6809" );
77 }
78
79 sprintf( commandLine, "\"%s\" -i \"%s\" -R 2800 -C -l 0 \"%s\" -p \"%s\" %d",
80 executableName,
81 _environment->listingFileName,
82 _environment->exeFileName,
83 _environment->profileFileName,
84 _environment->profileCycles ? _environment->profileCycles : 1000000
85 );
86
87 if ( system_call( _environment, commandLine ) ) {
88 printf("The profiling of assembly program failed.\n\n");
89 return;
90 };
91
92 }
93
94 }
95
96 strcopy( _environment->exeFileName, exeFileName );
97
98}
99
100void generate_dsk( Environment * _environment ) {
101
102 // Let's cut the executable file into multiple segments.
103 // The name of the binary is equal to the disk file name,
104 // but with a different extension.
105 char originalBinaryFile[MAX_TEMPORARY_STORAGE];
106 strcopy( originalBinaryFile, _environment->exeFileName );
107 char * p = strstr( originalBinaryFile, ".dsk" );
108 if ( p ) {
109 strcopy( p, ".bin" );
110 }
111
112 // Calculate the effective size.
113 FILE * fh = fopen( originalBinaryFile, "rb" );
114 int executableBinaryFileSize = 0;
115 if ( fh ) {
116 fseek( fh, 0, SEEK_END );
117 executableBinaryFileSize = ftell( fh );
118 fclose( fh );
119 } else {
120 CRITICAL_BUILD_CANNOT_READ_EXECUTABLE_FOR_DSK( _environment->exeFileName, originalBinaryFile );
121 }
122
123 // The LOADM command is able to read a limited size of binary file.
124 // So we are going to split the original file as follows:
125 // | $2a00 ............. $4d00 ... $4e00 ........ $xx00 .....
126 // +---------------------+---------+---------+....+---------+
127 // | PROGRAM.EXE | P01.DAT | P02.DAT |....| P0n.DAT |
128 // +---------------------+---------+---------+....+---------+
129 //
130
131 executableBinaryFileSize -= 5;
132 char * originalBinaryFileContent = malloc( executableBinaryFileSize );
133 fh = fopen( originalBinaryFile, "rb" );
134 (void)!fread( originalBinaryFileContent, 1, 5, fh);
135 (void)!fread( originalBinaryFileContent, 1, executableBinaryFileSize, fh);
136 fclose( fh );
137
138 int programExeSize = 0x4d00 - 0x2a00; // _environment->program.startingAddress;
139 if ( executableBinaryFileSize < programExeSize ) {
140 programExeSize = executableBinaryFileSize;
141 }
142 char * programExe = malloc( programExeSize );
143 memcpy( programExe, originalBinaryFileContent, programExeSize );
144 executableBinaryFileSize -= programExeSize;
145
146 char * programDats[MAX_TEMPORARY_STORAGE];
147 int programDatsSize[MAX_TEMPORARY_STORAGE];
148 int programDataCount = 0;
149
150 if ( executableBinaryFileSize ) {
151 char * originalBinaryFileContentPtr = originalBinaryFileContent + programExeSize;
152 int blockSize = 0x2000;
153 while( executableBinaryFileSize ) {
154 if ( blockSize > executableBinaryFileSize ) {
155 blockSize = executableBinaryFileSize;
156 executableBinaryFileSize = blockSize;
157 }
158 programDats[programDataCount] = malloc( blockSize );
159 programDatsSize[programDataCount] = blockSize;
160 memcpy( programDats[programDataCount], originalBinaryFileContentPtr, blockSize );
161 executableBinaryFileSize -= blockSize;
162 originalBinaryFileContentPtr += blockSize;
163 ++programDataCount;
164 }
165 }
166
167 char temporaryPath[MAX_TEMPORARY_STORAGE];
168 strcopy( temporaryPath, _environment->temporaryPath );
169 strcat( temporaryPath, " " );
170 temporaryPath[strlen(temporaryPath)-1] = PATH_SEPARATOR;
171
172 char outputFileName[MAX_TEMPORARY_STORAGE*2];
173
174 // Now we are going to write down the effective files, that must
175 // have the LOADM format, as follows:
176 //
177 // +------------+--------------....--------------+---------------+
178 // | PREAMBLE | DATA | POSTAMBLE |
179 // +------------+--------------....--------------+---------------+
180 // |00|LEN |LOAD| .............................. |FF|00|00| EXEC |
181 // +------------+--------------....--------------+---------------+
182 //
183
184 sprintf( outputFileName, "%sprogram.exe", temporaryPath);
185 fh = fopen( outputFileName, "wb" );
186 fputc( 0x00, fh );
187 fputc( programExeSize >> 8, fh );
188 fputc( programExeSize & 0xff, fh );
189 fputc( 0x2a /*_environment->program.startingAddress >> 8*/, fh );
190 fputc( 0x00 /*_environment->program.startingAddress & 0xff*/, fh );
191 fwrite( programExe, 1, programExeSize, fh );
192 fputc( 0xff, fh );
193 fputc( 0x00, fh );
194 fputc( 0x00, fh );
195 fputc( 0x2a /*_environment->program.startingAddress >> 8*/, fh );
196 fputc( 0x00 /*_environment->program.startingAddress & 0xff*/, fh );
197 fclose( fh );
198
199 if ( programDataCount ) {
200 for( int i=0; i<programDataCount; ++i ) {
201
202 // The dat files will be loaded to a fixed position,
203 // because they will be copied under ROM bank
204 // using the LOADER.BAS.
205 //
206 // +------------+--------------....--------------+--------------+
207 // | PREAMBLE | DATA | POSTAMBLE |
208 // +------------+--------------....--------------+--------------+
209 // |00|LEN |3000| .............................. |FF|00|00|00|00|
210 // +------------+--------------....--------------+--------------+
211
212 sprintf( outputFileName, "%sprogram.%03d", temporaryPath, i);
213 fh = fopen( outputFileName, "wb" );
214 fputc( 0x00, fh );
215 fputc( programDatsSize[i] >> 8, fh );
216 fputc( programDatsSize[i] & 0xff, fh );
217 fputc( 0x2a, fh );
218 fputc( 0x00, fh );
219 fwrite( programDats[i], 1, programDatsSize[i], fh );
220 fputc( 0xff, fh );
221 fputc( 0x00, fh );
222 fputc( 0x00, fh );
223 fputc( 0x00, fh );
224 fputc( 0x00, fh );
225 fclose( fh );
226
227 }
228 }
229
230 // Now we are going to create the BASIC loader.
231
232 char basFileName[MAX_TEMPORARY_STORAGE];
233
234 // 206 16 0
235
236 sprintf( basFileName, "%sloader.bas", temporaryPath);
237 fh = fopen( basFileName, "wb" );
238 fprintf( fh, "1REM ugBASIC %s\n", UGBASIC_VERSION );
239 fprintf( fh, "2DATA26,80,52,16,52,6,142,14,0,159,31,31\n" );
240 fprintf( fh, "3DATA65,16,206,15,0,16,223,33,198,255,166\n" );
241 fprintf( fh, "4DATA133,167,229,90,38,249,53,6,53,16,28\n" );
242 fprintf( fh, "5DATA159,57,26,80,142,42,0,16,142,42,0\n" );
243 fprintf( fh, "6DATA183,255,223,206,16,0,166,128,167,160\n" );
244 fprintf( fh, "7DATA51,95,17,131,0,0,38,244,183,255,222\n" );
245 fprintf( fh, "9DATA28,159,57\n" );
246 fprintf( fh, "10FORA=&HE00 TO&HE44:READX:POKEA,X:NEXTA\n" );
247 fprintf( fh, "11POKE27,12:POKE28,0:POKE29,12:POKE30,172\n");
248 fprintf( fh, "12POKE31,13:POKE32,0:POKE33,13:POKE34,64:POKE35,13:POKE36,172\n" );
249 fprintf( fh, "13POKE37,13:POKE38,172:POKE39,13:POKE40,250:EXEC3584:? \"WAIT\";\n" );
250 for( int i=0; i<programDataCount; ++i ) {
251 int lineNr = 14 + i*2;
252 fprintf( fh, "%dLOADM\"P.%02d\":?\".\";\n", lineNr, i);
253 lineNr = 15 + i*2;
254 int address = 0x4d + i*32;
255 int sizeHi = ( programDatsSize[i] >> 8 ) & 0xff;
256 int sizeLo = ( programDatsSize[i] ) & 0xff;
257 fprintf( fh, "%dPOKE3627,%d:POKE3633,%d:POKE3634,%d:EXEC3620\n", lineNr, address, sizeHi, sizeLo );
258 }
259 fprintf( fh, "90?\"*\";:LOADM\"P.\":?\".\":EXEC10752\n" );
260 fclose( fh );
261
262 // char executableName[MAX_TEMPORARY_STORAGE];
263 // char listingFileName[MAX_TEMPORARY_STORAGE];
264 char binaryName[MAX_TEMPORARY_STORAGE];
265 // char originalFileName[MAX_TEMPORARY_STORAGE];
266
267 // int fileSize = 0;
268 // int standardSize = 0;
269 // int ondemandSize = 0;
270 // int blockSize = 0;
271 // int blocks = 0;
272 // int block = 0;
273 // int remainSize = 0;
274
275 char executableName[MAX_TEMPORARY_STORAGE];
276 BUILD_TOOLCHAIN_DECB_GET_EXECUTABLE( _environment, executableName );
277
278 strcopy( binaryName, _environment->exeFileName );
279 Storage * storage = _environment->storage;
280 char buffer[MAX_TEMPORARY_STORAGE];
281 if ( storage ) {
282 char filemask[MAX_TEMPORARY_STORAGE];
283 strcopy( filemask, _environment->exeFileName );
284 char * basePath = find_last_path_separator( filemask );
285 if ( basePath ) {
286 ++basePath;
287 *basePath = 0;
288 if ( storage->fileName ) {
289 strcat( basePath, storage->fileName );
290 } else {
291 strcat( basePath, "disk%d.dsk" );
292 }
293 } else {
294 if ( storage->fileName ) {
295 strcopy( filemask, storage->fileName );
296 } else {
297 strcopy( filemask, "disk%d.dsk" );
298 }
299 }
300 sprintf( buffer, filemask, 0 );
301 if ( !strstr( buffer, ".dsk" ) ) {
302 strcat( buffer, ".dsk" );
303 }
304 _environment->exeFileName = strdup( buffer );
305 } else {
306 strcopy( binaryName, _environment->exeFileName );
307 char * p = strstr( binaryName, ".bin" );
308 if ( p ) {
309 strcopy( p, ".dsk" );
310 }
311 _environment->exeFileName = strdup( binaryName );
312 }
313
314 char commandLine[8*MAX_TEMPORARY_STORAGE];
315 remove( _environment->exeFileName );
316 sprintf( commandLine, "\"%s\" dskini \"%s\"", executableName, _environment->exeFileName );
317 if ( system_call( _environment, commandLine ) ) {
318 printf("The compilation of assembly program failed.\n\n");
319 printf("Please use option '-I' to install chain tool.\n\n");
320 };
321
322 sprintf( commandLine, "\"%s\" copy -0 -t \"%s\" \"%s,LOADER.BAS\"",
323 executableName,
324 basFileName,
325 _environment->exeFileName );
326 if ( system_call( _environment, commandLine ) ) {
327 printf("The compilation of assembly program failed.\n\n");
328 printf("Please use option '-I' to install chain tool.\n\n");
329 };
330
331 remove( basFileName );
332
333 sprintf( commandLine, "\"%s\" copy -2 \"%sprogram.exe\" \"%s,P\"",
334 executableName,
335 temporaryPath,
336 _environment->exeFileName );
337 if ( system_call( _environment, commandLine ) ) {
338 printf("The compilation of assembly program failed.\n\n");
339 printf("Please use option '-I' to install chain tool.\n\n");
340 };
341
342 sprintf( commandLine, "%sprogram.exe", temporaryPath);
343 remove( commandLine );
344
345 if ( programDataCount ) {
346 for( int i=0; i<programDataCount; ++i ) {
347 sprintf( commandLine, "\"%s\" copy -2 \"%sprogram.%03d\" \"%s,P.%02d\"",
348 executableName,
349 temporaryPath,
350 i,
351 _environment->exeFileName,
352 i );
353 if ( system_call( _environment, commandLine ) ) {
354 printf("The compilation of assembly program failed.\n\n");
355 printf("Please use option '-I' to install chain tool.\n\n");
356 };
357 sprintf( commandLine, "%sprogram.%03d", temporaryPath, i);
358 remove( commandLine );
359 }
360 }
361
362 if ( _environment->outputGeneratedFiles ) {
363 printf( "%s\n", _environment->exeFileName );
364 }
365
366 if ( !storage ) {
367
368 } else {
369 strcopy( buffer, _environment->exeFileName );
370 int i=0;
371 while( storage ) {
372 FileStorage * fileStorage = storage->files;
373 while( fileStorage ) {
374 int size;
375 char * buffer;
376
377 if ( fileStorage->content && fileStorage->size ) {
378 size = fileStorage->size + 2;
379 buffer = malloc( size );
380 memset( buffer, 0, size );
381 memcpy( buffer, fileStorage->content, fileStorage->size );
382 } else {
383 FILE * file = fopen( fileStorage->sourceName, "rb" );
384 if ( !file ) {
386 }
387 fseek( file, 0, SEEK_END );
388 size = ftell( file );
389 fseek( file, 0, SEEK_SET );
390 buffer = malloc( size + 2 );
391 memset( buffer, 0, size + 2 );
392 (void)!fread( buffer, size, 1, file );
393 fclose( file );
394 }
395 char dataFilename[MAX_TEMPORARY_STORAGE];
396 sprintf( dataFilename, "%s%s", temporaryPath, fileStorage->targetName );
397 FILE * fileOut = fopen( dataFilename, "wb" );
398 if ( fileOut ) {
399 fwrite( buffer, 1, size, fileOut );
400 fclose(fileOut );
401 }
402 sprintf( commandLine, "\"%s\" copy -1 -b \"%s\" \"%s,%s\"",
403 executableName,
404 dataFilename,
405 _environment->exeFileName,
406 fileStorage->targetName );
407 if ( system_call( _environment, commandLine ) ) {
408 printf("The compilation of assembly program failed.\n\n");
409 printf("Please use option '-I' to install chain tool.\n\n");
410 };
411
412 remove( dataFilename );
413 fileStorage = fileStorage->next;
414 }
415
416 storage = storage->next;
417 ++i;
418
419 if ( storage ) {
420
421 char filemask[MAX_TEMPORARY_STORAGE];
422 strcopy( filemask, _environment->exeFileName );
423 char * basePath = find_last_path_separator( filemask );
424 if ( basePath ) {
425 ++basePath;
426 *basePath = 0;
427 if ( storage->fileName ) {
428 strcat( basePath, storage->fileName );
429 } else {
430 strcat( basePath, "disk%d.dsk" );
431 }
432 } else {
433 if ( storage->fileName ) {
434 strcopy( filemask, storage->fileName );
435 } else {
436 strcopy( filemask, "disk%d.dsk" );
437 }
438 }
439 sprintf( buffer, filemask, i );
440 if ( !strstr( buffer, ".dsk" ) ) {
441 strcat( buffer, ".dsk" );
442 }
443 remove( buffer );
444 sprintf( commandLine, "\"%s\" dskini \"%s\"", executableName, buffer );
445 if ( system_call( _environment, commandLine ) ) {
446 printf("The compilation of assembly program failed.\n\n");
447 printf("Please use option '-I' to install chain tool.\n\n");
448 };
449
450 }
451
452 }
453
454 }
455
456 remove( originalBinaryFile );
457
458}
459
460void target_linkage( Environment * _environment ) {
461
462 switch( _environment->outputFileType ) {
464 generate_bin( _environment );
465 break;
467 generate_bin( _environment );
468 generate_dsk( _environment );
469 break;
470 default:
472 }
473
474}
475
476void target_cleanup( Environment * _environment ) {
477
478 remove( _environment->asmFileName );
479
480 if ( _environment->analysis && _environment->listingFileName ) {
481 target_analysis( _environment );
482 }
483
484}
int system_call(Environment *_environment, char *_commandline)
Call an external executable.
char * find_last_path_separator(char *_path)
void target_linkage(Environment *_environment)
Convert C64's assembly to executable.
Definition _build.c:327
void target_cleanup(Environment *_environment)
Definition _build.c:343
void target_analysis(Environment *_environment)
Definition _cleanup.c:68
int size
Definition _optimizer.c:678
void generate_dsk(Environment *_environment)
Definition _build.c:100
void generate_bin(Environment *_environment)
Definition _build.c:43
Storage * storage
Definition ugbc.h:2526
char * listingFileName
Definition ugbc.h:2305
char * temporaryPath
Definition ugbc.h:2360
int analysis
Definition ugbc.h:2365
char * executerFileName
Definition ugbc.h:2315
OutputFileType outputFileType
Definition ugbc.h:2452
int outputGeneratedFiles
Definition ugbc.h:3173
char * profileFileName
Definition ugbc.h:2310
char * asmFileName
Definition ugbc.h:2285
int profileCycles
Definition ugbc.h:2375
char * exeFileName
Definition ugbc.h:2290
char * targetName
Definition ugbc.h:201
int size
Definition ugbc.h:204
char * sourceName
Definition ugbc.h:198
char * content
Definition ugbc.h:210
struct _FileStorage * next
Definition ugbc.h:213
FileStorage * files
Definition ugbc.h:232
char * fileName
Definition ugbc.h:229
struct _Storage * next
Definition ugbc.h:235
void * malloc(YYSIZE_T)
#define BUILD_TOOLCHAIN_ASM6809_GET_LISTING_FILE(_environment, listingFileName)
Definition ugbc.h:4872
@ OUTPUT_FILE_TYPE_DSK
Definition ugbc.h:267
@ OUTPUT_FILE_TYPE_BIN
Definition ugbc.h:258
#define BUILD_SAFE_REMOVE(_environment, filename)
Definition ugbc.h:4748
#define MAX_TEMPORARY_STORAGE
Definition ugbc.h:563
#define PATH_SEPARATOR
Definition ugbc.h:69
#define CRITICAL_DLOAD_MISSING_FILE(f)
Definition ugbc.h:3578
#define BUILD_TOOLCHAIN_ASM6809EXEC(_environment, flag, startingAddress, executableName, listingFileName)
Definition ugbc.h:4880
#define CRITICAL_BUILD_CANNOT_READ_EXECUTABLE_FOR_DSK(d, f)
Definition ugbc.h:3868
struct _Environment Environment
Structure of compilation environment.
#define BUILD_TOOLCHAIN_DECB_GET_EXECUTABLE(_environment, executableName)
Definition ugbc.h:4894
struct _Storage Storage
Structure of a single storage.
struct _FileStorage FileStorage
Structure of a single file inside a storage.
#define BUILD_TOOLCHAIN_ASM6809_GET_EXECUTABLE(_environment, executableName)
Definition ugbc.h:4855
#define UGBASIC_VERSION
Definition ugbc.h:63
#define CRITICAL_UNSUPPORTED_OUTPUT_FILE_TYPE(t)
Definition ugbc.h:3537
char OUTPUT_FILE_TYPE_AS_STRING[][16]
char * strcopy(char *_dest, char *_source)