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#include <errno.h>
38#include <sys/stat.h>
39#include <sys/types.h>
40
41/****************************************************************************
42 * CODE SECTION
43 ****************************************************************************/
44
45extern char OUTPUT_FILE_TYPE_AS_STRING[][16];
46
47void generate_bin( Environment * _environment ) {
48
49 char commandLine[8*MAX_TEMPORARY_STORAGE];
50 char executableName[MAX_TEMPORARY_STORAGE];
51 char relName1[MAX_TEMPORARY_STORAGE];
52 char relName2[MAX_TEMPORARY_STORAGE];
53 char binaryName[MAX_TEMPORARY_STORAGE];
54 char listingFileName[MAX_TEMPORARY_STORAGE];
55
56 BUILD_SAFE_REMOVE( _environment, _environment->exeFileName );
57
58 BUILD_TOOLCHAIN_ASXV5PXX_GET_EXECUTABLE_AS61860( _environment, executableName );
59
60 BUILD_TOOLCHAIN_ASXV5PXX_EXEC( _environment, "pc1403", executableName );
61
62 char * p;
63
64 strcopy( relName1, _environment->asmFileName );
65 p = strstr( relName1, ".asm" );
66 if ( p ) {
67 *(p+1) = 'r';
68 *(p+2) = 'e';
69 *(p+3) = 'l';
70 *(p+4) = 0;
71 }
72
73 strcopy( relName2, _environment->exeFileName );
74 p = strstr( relName2, ".ram" );
75 if ( p ) {
76 *(p+1) = 'r';
77 *(p+2) = 'e';
78 *(p+3) = 'l';
79 *(p+4) = 0;
80 }
81
82 // @mv $(subst /exe/,/asm/,$(@:.ram=.rel)) $(@:.ram=.rel)
83
84 BUILD_SAFE_MOVE( _environment, relName1, relName2 );
85
86 strcopy( binaryName, _environment->exeFileName );
87 p = strstr( binaryName, ".ram" );
88 if ( p ) {
89 *(p+1) = 'b';
90 *(p+2) = 'i';
91 *(p+3) = 'n';
92 *(p+4) = 0;
93 }
94
95 // @$(ASLINK) -t $(@:.ram=.rel) >/dev/null 2>/dev/null
96
97 BUILD_TOOLCHAIN_ASXV5PXX_GET_EXECUTABLE_ASLINK( _environment, executableName );
98
99 char pipes[256];
100
101 #ifdef _WIN32
102 strcopy( pipes, ">nul 2>nul");
103 #else
104 strcopy( pipes, ">/dev/null 2>/dev/null");
105 #endif
106
107 sprintf( commandLine, "\"%s\" \"-t+%s\" -t \"%s\" %s",
108 executableName,
109 binaryName,
110 relName2,
111 pipes );
112 if ( system_call( _environment, commandLine ) ) {
113 printf("The compilation of assembly program failed.\n\n");
114 printf("Please use option '-I' to install chain tool.\n\n");
115 return;
116 };
117
118}
119
120void generate_ram( Environment * _environment ) {
121
122 char commandLine[8*MAX_TEMPORARY_STORAGE];
123 char executableName[MAX_TEMPORARY_STORAGE];
124 char binaryName[MAX_TEMPORARY_STORAGE];
125
126 strcopy( binaryName, _environment->exeFileName );
127 char * p = strstr( binaryName, ".ram" );
128 if ( p ) {
129 *(p+1) = 'b';
130 *(p+2) = 'i';
131 *(p+3) = 'n';
132 *(p+4) = 0;
133 }
134
135 // @$(PC1403RAM) $(@:.ram=.bin) $(@) >/dev/null 2>/dev/null
136
137 FILE * fin = fopen( binaryName, "rb" );
138 FILE * fout = fopen( _environment->exeFileName, "wb" );
139
140 while( !feof( fin ) ) {
141
142 int header = (int)fgetc(fin);
143 int size = (((int)fgetc(fin))<<8) + (((int)fgetc(fin)));
144 int address = (((int)fgetc(fin))<<8) + (((int)fgetc(fin)));
145
146 if ( header == 0 ) {
147 char * content = malloc( size );
148 memset( content, 0, size );
149 if ( ! fread( content, 1, size, fin ) ) {
150 perror( "Unable to read segment");
151 }
152 fwrite( content, 1, size, fout );
153 free( content );
154 }
155
156 }
157
158 fclose( fout );
159 fclose( fin );
160
161}
162
163void target_linkage( Environment * _environment ) {
164
165 switch( _environment->outputFileType ) {
167 generate_bin( _environment );
168 generate_ram( _environment );
169 break;
170 default:
172 }
173
174}
175
176void target_cleanup( Environment * _environment ) {
177
178 if ( _environment->exeFileName ) {
179
180 char binFileName[MAX_TEMPORARY_STORAGE];
181
182 remove( _environment->configurationFileName );
183 remove( _environment->asmFileName );
184
185 if ( _environment->analysis && _environment->listingFileName ) {
186 target_analysis( _environment );
187 }
188
189 }
190
191}
int system_call(Environment *_environment, char *_commandline)
Call an external executable.
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_bin(Environment *_environment)
Definition _build.c:43
void generate_ram(Environment *_environment)
Definition _build.c:120
char * listingFileName
Definition ugbc.h:2305
int analysis
Definition ugbc.h:2365
char * configurationFileName
Definition ugbc.h:2295
OutputFileType outputFileType
Definition ugbc.h:2452
char * asmFileName
Definition ugbc.h:2285
char * exeFileName
Definition ugbc.h:2290
void * malloc(YYSIZE_T)
void free(void *)
@ OUTPUT_FILE_TYPE_RAM
Definition ugbc.h:270
#define BUILD_SAFE_REMOVE(_environment, filename)
Definition ugbc.h:4748
#define MAX_TEMPORARY_STORAGE
Definition ugbc.h:563
#define BUILD_TOOLCHAIN_ASXV5PXX_EXEC(_environment, target, executableName)
Definition ugbc.h:5000
#define BUILD_TOOLCHAIN_ASXV5PXX_GET_EXECUTABLE_ASLINK(_environment, executableName)
Definition ugbc.h:5019
struct _Environment Environment
Structure of compilation environment.
#define BUILD_TOOLCHAIN_ASXV5PXX_GET_EXECUTABLE_AS61860(_environment, executableName)
Definition ugbc.h:4985
#define BUILD_SAFE_MOVE(_environment, source, destination)
Definition ugbc.h:4751
#define CRITICAL_UNSUPPORTED_OUTPUT_FILE_TYPE(t)
Definition ugbc.h:3537
char OUTPUT_FILE_TYPE_AS_STRING[][16]
char * strcopy(char *_dest, char *_source)