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_prg( Environment * _environment ) {
44
45 char commandLine[8*MAX_TEMPORARY_STORAGE];
46 char executableName[MAX_TEMPORARY_STORAGE];
47 char listingFileName[MAX_TEMPORARY_STORAGE];
48
49 BUILD_SAFE_REMOVE( _environment, _environment->exeFileName );
50
51 BUILD_TOOLCHAIN_CC65_GET_EXECUTABLE( _environment, executableName );
52
53 BUILD_TOOLCHAIN_CC65_GET_LISTING_FILE( _environment, listingFileName );
54
55 BUILD_TOOLCHAIN_CC65_EXEC( _environment, "c64", executableName, listingFileName, "" );
56
57 char objectFileName[MAX_TEMPORARY_STORAGE];
58 strcopy( objectFileName, _environment->asmFileName );
59 char * p = strstr(objectFileName, ".asm");
60 if ( p ) {
61 strcopy( p, ".o" );
62 remove( objectFileName );
63 }
64
65 if ( _environment->listingFileName ) {
66
67 if ( _environment->profileFileName && _environment->profileCycles ) {
68 if ( _environment->executerFileName ) {
69 sprintf(executableName, "%s", _environment->executerFileName );
70 } else if( access( "run6502.exe", F_OK ) == 0 ) {
71 sprintf(executableName, "%s", "run6502.exe" );
72 } else {
73 sprintf(executableName, "%s", "run6502" );
74 }
75
76 sprintf( commandLine, "\"%s\" -X 0000 -R 080d -l 07ff \"%s\" -u \"%s\" -p \"%s\" %d",
77 executableName,
78 _environment->exeFileName,
79 _environment->listingFileName,
80 _environment->profileFileName,
81 _environment->profileCycles ? _environment->profileCycles : 1000000
82 );
83
84 if ( system_call( _environment, commandLine ) ) {
85 printf("The profiling of assembly program failed.\n\n");
86 return;
87 };
88
89 }
90
91 }
92
93}
94
95void generate_d64( Environment * _environment ) {
96
97 FILE * prgHandle = fopen(_environment->exeFileName, "rb");
98 fseek( prgHandle, 0, SEEK_END );
99 int prgSize = ftell( prgHandle );
100 fseek( prgHandle, 0, SEEK_SET );
101 unsigned char * prgContent = malloc( prgSize );
102 (void)!fread( prgContent, prgSize, 1, prgHandle );
103 fclose( prgHandle );
104
105 char d64FileName[MAX_TEMPORARY_STORAGE];
106 strcopy( d64FileName, _environment->exeFileName );
107 char * p = strstr( d64FileName, ".d64" );
108 if ( !p ) {
109 strcat( d64FileName, ".d64");
110 }
111
112 remove(d64FileName);
113
114 Storage * storage = _environment->storage;
115
116 if ( !storage ) {
117 D64Handle * handle = d64_create( CBMDOS );
118 d64_write_file( handle, "MAIN", FT_PRG, prgContent, prgSize );
119 d64_output( handle, d64FileName );
120 d64_free( handle );
121 if ( _environment->outputGeneratedFiles ) {
122 printf( "%s\n", d64FileName );
123 }
124 } else {
125 int i=0;
126 while( storage ) {
127 D64Handle * handle = d64_create( CBMDOS );
128 if ( i == 0 ) {
129 d64_write_file( handle, "MAIN", FT_PRG, prgContent, prgSize );
130 }
131 FileStorage * fileStorage = storage->files;
132 while( fileStorage ) {
133 int size;
134 char * buffer;
135 if ( fileStorage->content && fileStorage->size ) {
136 size = fileStorage->size + 2;
137 buffer = malloc( size );
138 memset( buffer, 0, size );
139 memcpy( &buffer[2], fileStorage->content, fileStorage->size );
140 } else {
141 FILE * file = fopen( fileStorage->sourceName, "rb" );
142 if ( !file ) {
144 }
145 fseek( file, 0, SEEK_END );
146 size = ftell( file );
147 fseek( file, 0, SEEK_SET );
148 buffer = malloc( size + 2 );
149 memset( buffer, 0, size + 2 );
150 (void)!fread( &buffer[2], size, 1, file );
151 fclose( file );
152 size += 2;
153 }
154 d64_write_file( handle, fileStorage->targetName, FT_PRG, buffer, size );
155 fileStorage = fileStorage->next;
156 }
157 char buffer[MAX_TEMPORARY_STORAGE];
158 char filemask[MAX_TEMPORARY_STORAGE];
159 strcopy( filemask, d64FileName );
160 char * basePath = find_last_path_separator( filemask );
161 if ( basePath ) {
162 ++basePath;
163 *basePath = 0;
164 if ( storage->fileName ) {
165 strcat( basePath, storage->fileName );
166 } else {
167 strcat( basePath, "disk%d.d64" );
168 }
169 } else {
170 if ( storage->fileName ) {
171 strcopy( filemask, storage->fileName );
172 } else {
173 strcopy( filemask, "disk%d.d64" );
174 }
175 }
176 sprintf( buffer, filemask, i );
177 if ( !strstr( buffer, ".d64" ) ) {
178 strcat( buffer, ".d64" );
179 }
180 d64_output( handle, buffer );
181 if ( _environment->outputGeneratedFiles ) {
182 printf( "%s\n", buffer );
183 }
184 d64_free( handle );
185 storage = storage->next;
186 ++i;
187 }
188 }
189
190}
191
197void target_linkage( Environment * _environment ) {
198
199 switch( _environment->outputFileType ) {
201 generate_prg( _environment );
202 break;
204 generate_prg( _environment );
205 generate_d64( _environment );
206 break;
207 default:
209 }
210
211}
212
213void target_cleanup( Environment * _environment ) {
214
215 remove( _environment->configurationFileName );
216 remove( _environment->asmFileName );
217
218 if ( _environment->analysis && _environment->listingFileName ) {
219 target_analysis( _environment );
220 }
221
222}
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_prg(Environment *_environment)
Definition _build.c:43
void generate_d64(Environment *_environment)
Definition _build.c:95
D64Handle * d64_create(D64Format _format)
Create a new D64 disk image.
Definition d64.c:779
void d64_output(D64Handle *_handle, unsigned char *_filename)
Definition d64.c:932
void d64_free(D64Handle *_handle)
Free the disk image resources.
Definition d64.c:924
int d64_write_file(D64Handle *_handle, unsigned char *_filename, D64FileType _type, unsigned char *_buffer, int _size)
Write a block of memory on a file on the D64 disk image.
Definition d64.c:802
@ CBMDOS
Definition d64.h:436
@ FT_PRG
Definition d64.h:126
struct _D64Handle D64Handle
Storage * storage
Definition ugbc.h:2526
char * listingFileName
Definition ugbc.h:2305
int analysis
Definition ugbc.h:2365
char * configurationFileName
Definition ugbc.h:2295
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)
@ OUTPUT_FILE_TYPE_PRG
Definition ugbc.h:259
@ OUTPUT_FILE_TYPE_D64
Definition ugbc.h:266
#define BUILD_SAFE_REMOVE(_environment, filename)
Definition ugbc.h:4748
#define MAX_TEMPORARY_STORAGE
Definition ugbc.h:563
#define CRITICAL_DLOAD_MISSING_FILE(f)
Definition ugbc.h:3578
#define BUILD_TOOLCHAIN_CC65_GET_EXECUTABLE(_environment, executableName)
Definition ugbc.h:4754
#define BUILD_TOOLCHAIN_CC65_GET_LISTING_FILE(_environment, listingFileName)
Definition ugbc.h:4773
struct _Environment Environment
Structure of compilation environment.
struct _Storage Storage
Structure of a single storage.
#define BUILD_TOOLCHAIN_CC65_EXEC(_environment, target, executableName, listingFileName, additionalParameters)
Definition ugbc.h:4781
struct _FileStorage FileStorage
Structure of a single file inside a storage.
#define CRITICAL_UNSUPPORTED_OUTPUT_FILE_TYPE(t)
Definition ugbc.h:3537
char OUTPUT_FILE_TYPE_AS_STRING[][16]
char * strcopy(char *_dest, char *_source)