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, "vic20", executableName, listingFileName, "" );
56
57 if ( _environment->listingFileName ) {
58
59 if ( _environment->profileFileName && _environment->profileCycles ) {
60 if ( _environment->executerFileName ) {
61 sprintf(executableName, "%s", _environment->executerFileName );
62 } else if( access( "run6502.exe", F_OK ) == 0 ) {
63 sprintf(executableName, "%s", "run6502.exe" );
64 } else {
65 sprintf(executableName, "%s", "run6502" );
66 }
67
68 sprintf( commandLine, "\"%s\" -X 0000 -R 2000 -l 11ff \"%s\" -u \"%s\" -p \"%s\" %d",
69 executableName,
70 _environment->exeFileName,
71 _environment->listingFileName,
72 _environment->profileFileName,
73 _environment->profileCycles ? _environment->profileCycles : 1000000
74 );
75
76 if ( system_call( _environment, commandLine ) ) {
77 printf("The profiling of assembly program failed.\n\n");
78 return;
79 };
80
81 }
82
83 }
84
85}
86
87void generate_d64( Environment * _environment ) {
88
89 FILE * prgHandle = fopen(_environment->exeFileName, "rb");
90 fseek( prgHandle, 0, SEEK_END );
91 int prgSize = ftell( prgHandle );
92 fseek( prgHandle, 0, SEEK_SET );
93 unsigned char * prgContent = malloc( prgSize );
94 (void)!fread( prgContent, prgSize, 1, prgHandle );
95 fclose( prgHandle );
96
97 char d64FileName[MAX_TEMPORARY_STORAGE];
98 strcopy( d64FileName, _environment->exeFileName );
99 char * p = strstr( d64FileName, ".d64" );
100 if ( !p ) {
101 strcat( d64FileName, ".d64");
102 }
103
104 remove(d64FileName);
105
106 Storage * storage = _environment->storage;
107
108 if ( !storage ) {
109 D64Handle * handle = d64_create( CBMDOS );
110 d64_write_file( handle, "MAIN", FT_PRG, prgContent, prgSize );
111 d64_output( handle, d64FileName );
112 if ( _environment->outputGeneratedFiles ) {
113 printf( "%s\n",d64FileName );
114 }
115 d64_free( handle );
116 } else {
117 int i=0;
118 while( storage ) {
119 D64Handle * handle = d64_create( CBMDOS );
120 if ( i == 0 ) {
121 d64_write_file( handle, "MAIN", FT_PRG, prgContent, prgSize );
122 }
123 FileStorage * fileStorage = storage->files;
124 while( fileStorage ) {
125 FILE * file = fopen( fileStorage->sourceName, "rb" );
126 if ( !file ) {
128 }
129 fseek( file, 0, SEEK_END );
130 int size = ftell( file );
131 fseek( file, 0, SEEK_SET );
132 char * buffer;
133 buffer = malloc( size );
134 (void)!fread( &buffer[0], size, 1, file );
135 fclose( file );
136 d64_write_file( handle, fileStorage->targetName, FT_PRG, buffer, size );
137 fileStorage = fileStorage->next;
138 }
139 char buffer[MAX_TEMPORARY_STORAGE];
140 char filemask[MAX_TEMPORARY_STORAGE];
141 strcopy( filemask, d64FileName );
142 char * basePath = find_last_path_separator( filemask );
143 if ( basePath ) {
144 ++basePath;
145 *basePath = 0;
146 if ( storage->fileName ) {
147 strcat( basePath, storage->fileName );
148 } else {
149 strcat( basePath, "disk%d.d64" );
150 }
151 } else {
152 if ( storage->fileName ) {
153 strcopy( filemask, storage->fileName );
154 } else {
155 strcopy( filemask, "disk%d.d64" );
156 }
157 }
158 sprintf( buffer, filemask, i );
159 if ( !strstr( buffer, ".d64" ) ) {
160 strcat( buffer, ".d64" );
161 }
162 d64_output( handle, buffer );
163 if ( _environment->outputGeneratedFiles ) {
164 printf( "%s\n",buffer );
165 }
166 d64_free( handle );
167 storage = storage->next;
168 ++i;
169 }
170 }
171
172}
173
179void target_linkage( Environment * _environment ) {
180
181 switch( _environment->outputFileType ) {
183 generate_prg( _environment );
184 break;
186 generate_prg( _environment );
187 generate_d64( _environment );
188 break;
189 default:
191 }
192
193}
194
195void target_cleanup( Environment * _environment ) {
196
197 remove( _environment->configurationFileName );
198 remove( _environment->asmFileName );
199
200 if ( _environment->analysis && _environment->listingFileName ) {
201 target_analysis( _environment );
202 }
203
204}
205
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
char * sourceName
Definition ugbc.h:198
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)