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, "c128", 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 remove(_environment->exeFileName);
106
107 Storage * storage = _environment->storage;
108
109 if ( !storage ) {
110 D64Handle * handle = d64_create( CBMDOS );
111 d64_write_file( handle, "MAIN", FT_PRG, prgContent, prgSize );
112 d64_output( handle, _environment->exeFileName );
113 d64_free( handle );
114 } else {
115 int i=0;
116 while( storage ) {
117 D64Handle * handle = d64_create( CBMDOS );
118 if ( i == 0 ) {
119 d64_write_file( handle, "MAIN", FT_PRG, prgContent, prgSize );
120 }
121 FileStorage * fileStorage = storage->files;
122 while( fileStorage ) {
123 int size;
124 char * buffer;
125
126 if ( fileStorage->content && fileStorage->size ) {
127 size = fileStorage->size + 2;
128 buffer = malloc( size );
129 memset( buffer, 0, size );
130 memcpy( &buffer[2], fileStorage->content, fileStorage->size );
131 } else {
132 FILE * file = fopen( fileStorage->sourceName, "rb" );
133 if ( !file ) {
135 }
136 fseek( file, 0, SEEK_END );
137 size = ftell( file );
138 fseek( file, 0, SEEK_SET );
139 buffer = malloc( size + 2 );
140 memset( buffer, 0, size + 2 );
141 (void)!fread( &buffer[2], size, 1, file );
142 fclose( file );
143 size += 2;
144 }
145 d64_write_file( handle, fileStorage->targetName, FT_PRG, buffer, size );
146 fileStorage = fileStorage->next;
147 }
148 char buffer[MAX_TEMPORARY_STORAGE];
149 char filemask[MAX_TEMPORARY_STORAGE];
150 strcopy( filemask, _environment->exeFileName );
151 char * basePath = find_last_path_separator( filemask );
152 if ( basePath ) {
153 ++basePath;
154 *basePath = 0;
155 if ( storage->fileName ) {
156 strcat( basePath, storage->fileName );
157 } else {
158 strcat( basePath, "disk%d.d64" );
159 }
160 } else {
161 if ( storage->fileName ) {
162 strcopy( filemask, storage->fileName );
163 } else {
164 strcopy( filemask, "disk%d.d64" );
165 }
166 }
167 sprintf( buffer, filemask, i );
168 if ( !strstr( buffer, ".d64" ) ) {
169 strcat( buffer, ".d64" );
170 }
171 d64_output( handle, buffer );
172 if ( _environment->outputGeneratedFiles ) {
173 printf( "%s\n", buffer );
174 }
175 d64_free( handle );
176 storage = storage->next;
177 ++i;
178 }
179 }
180
181}
182
188void target_linkage( Environment * _environment ) {
189
190 switch( _environment->outputFileType ) {
192 generate_prg( _environment );
193 break;
195 generate_prg( _environment );
196 generate_d64( _environment );
197 break;
198 default:
200 }
201
202}
203
204void target_cleanup( Environment * _environment ) {
205
206 remove( _environment->configurationFileName );
207 remove( _environment->asmFileName );
208
209 if ( _environment->analysis && _environment->listingFileName ) {
210 target_analysis( _environment );
211 }
212
213}
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)