ugBASIC 1.18
An isomorphic BASIC language compiler for retrocomputers
Loading...
Searching...
No Matches
_cleanup_original.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
41// -------------------------------------------------------------------------
42// CONVERT BINARY TO K7 TAPE FILE
43// FOR THOMSON MO6 / PC128 OLIVETTI PRODEST
44//
45// This code was originally present in the repository
46// "PC 128 Olivetti Prodest (Thomson MO6) tools collection"
47// https://github.com/dinoflorenzi/MO5
48//
49// Code courtesy of Dino Florenzi (https://github.com/dinoflorenzi)
50// Adapted by Marco Spedaletti for ugBASIC
51// -------------------------------------------------------------------------
52
53#include<stdlib.h>
54#include<stdio.h>
55#include<math.h>
56
57static unsigned int sum=0;
58static unsigned char chk=0;
59static unsigned short l;
60static int nblock;
61static int rest;
62static int cnt=0;
63static int frmt( Environment * _environment, char * ptr, char size,char* out)
64{
65 memset(out,0x20,11);
66 char * point=".";
67 int k=0,i=0;
68 int p=strcspn(ptr,point);
69 if(p==size-1||p>8||size>12||p==0)
70 {
72 }
73 for(i=0;i<size;i++)
74 {
75 if(i==p)
76 k+=(8-p);
77 else
78 {
79 out[k]=ptr[i];
80 k++;
81 }
82
83 }
84 return 0;
85}
86
87// FIX #173
88#if ( defined(__BYTE_ORDER) && __BYTE_ORDER == __BIG_ENDIAN ) || \
89 defined(__BIG_ENDIAN__) || \
90 defined(__ARMEB__) || \
91 defined(__THUMBEB__) || \
92 defined(__AARCH64EB__) || \
93 defined(_MIBSEB) || defined(__MIBSEB) || defined(__MIBSEB__)
94
95 static void swapen(unsigned short *num)
96 {
97 return *num;
98 }
99
100#else
101
102 static void swapen(unsigned short *num)
103 {
104 unsigned short swapped;
105 swapped = (*num>>8) | (*num<<8);
106 *num=swapped;
107 }
108
109#endif
110
111static void crc ( unsigned char * data, int l,unsigned int * old)
112{
113
114 cnt+=l;
115 for (int i= 0;i<l;i++)
116 {
117 (*old)+=data[i];
118
119 }
120}
121
122static unsigned char cmpl(unsigned int n)
123{
124 return 0x100- ((unsigned char)n);
125}
126
128{
129 // if(argc==1)
130 // {
131 // printf("Missing arguments !!!!!!\n"
132 // "bin2k7 source pc128_filename K7_filename\n"
133 // "load and exec address is set to &H0000.\n"
134 // "Use offset to load to any address. EX: LOADM\"CASS:\",&H6000 will load starting to &H6000\n"
135 // );
136 // return -1;
137 // }
138 unsigned short start=0x3000;
139 unsigned short size;
140 unsigned short runaddr=0x3000;
141 FILE *fr,*fw;
142 char nome[12];
143 char nomecod[12];
144 char source[20];
145 char destin[100];
146
147 // Rename the output file into a temporary filename
148 char temporaryFileName[MAX_TEMPORARY_STORAGE];
149 sprintf(temporaryFileName, "%s.bin", get_temporary_filename( _environment ) );
150 remove( temporaryFileName );
151 BUILD_SAFE_MOVE( _environment, _environment->exeFileName, temporaryFileName );
152
153 fr=fopen(temporaryFileName,"rb");
154 if(!fr)
155 {
156 CRITICAL_CANNOT_OPEN_EXECUTABLE_FILE( temporaryFileName );
157 // printf("source file not found!!!!");
158 // return -1;
159 }
160
161 if(frmt(_environment, "main.exe",strlen("main.exe"),nome))
162 {
164 }
165
166 unsigned char byt;
167 unsigned short l;
168 unsigned char h[]={1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0x3c,0x5a};
169 unsigned char h1[]={1,0,0};
170 unsigned char h2[]={1,0};
171 unsigned char n[]={0x00,0x10};
172 unsigned char n1[]={0x02,0x00,0x00};
173 unsigned char end[]={0xff,0x02,0x00};
174 unsigned char z=0;
175 unsigned char u=1;
176 unsigned char preend[]={0xff,0x00,0x00};
177 fr=fopen(temporaryFileName,"rb");
178 // if(argv[3]==NULL)
179 // {
180 strcopy(destin,_environment->exeFileName);
181 // strcat(destin,".K7");
182 // }
183 // else
184 // strcopy(destin,argv[3]);
185 fw=fopen(destin,"wb");
186 fseek(fr, 0L, SEEK_END);
187 size = ftell(fr);
188 l=size;
189 nblock=floor((size+12)/254);
190 // printf("n blocks: %d\n",nblock);
191 // printf("size: %d\n",size);
192 rest=size+12-nblock*254;
193
194 // Fix #187
195 unsigned char restByte = (unsigned char)rest;
196
197 rewind(fr);
198 swapen(&size);
199 swapen(&start);
200 swapen(&runaddr);
201 fwrite(&h,sizeof(h),1,fw);
202 fwrite(&n,sizeof(n),1,fw);
203 fwrite(&nome,sizeof(nome)-1,1,fw);
204 fwrite(&n1,3,1,fw);
205 crc(nome,11,&sum);
206 crc(n1,1,&sum);
207
208 chk=cmpl(sum);
209 sum=0;
210 cnt=0;
211 fwrite(&chk,1,1,fw);
212 fwrite(&h,sizeof(h),1,fw);
213 if(nblock>0)
214 {
215 fwrite(&h1,sizeof(h1),1,fw);
216 }
217 else
218 {
219 fwrite(&u,sizeof(u),1,fw);
220 // Fix #187
221 fwrite(&restByte,1,1,fw);
222 fwrite(&z,sizeof(z),1,fw);
223 }
224 cnt++;
225 fwrite(&size,sizeof(size),1,fw);
226 fwrite(&start,sizeof(start),1,fw);
227 crc((unsigned char *)&size,sizeof(size),&sum);
228 crc((unsigned char *)&start,sizeof(start),&sum);
229 for (int i=0;i<l;i++)
230 {
231 if(cnt>253)
232 {
233 nblock--;
234 chk=cmpl(sum);
235 sum=0;
236 //printf("%d\n",chk);
237 fwrite(&chk,1,1,fw);
238 fwrite(&h,sizeof(h),1,fw);
239 if(nblock>0)
240 fwrite(&h2,sizeof(h2),1,fw);
241 else
242 {
243 fwrite(&u,sizeof(u),1,fw);
244 // Fix #187
245 fwrite(&restByte,1,1,fw);
246 // printf("*****\n");
247 }
248 cnt=0;
249
250 }
251 (void)!fread(&byt,1,1,fr);
252 crc(&byt,1,&sum);
253 fwrite(&byt,1,1,fw);
254 }
255 crc((unsigned char *)&preend,3,&sum);
256 fwrite(&preend,3,1,fw);
257 crc((unsigned char *)&runaddr,2,&sum);
258 fwrite(&runaddr,2,1,fw);
259 chk=cmpl(sum);
260 sum=0;
261 fwrite(&chk,1,1,fw);
262 fwrite(&h,sizeof(h),1,fw);
263 fwrite(&end,sizeof(end),1,fw);
264 // printf("chk %x rest %x block %d\n",chk,rest,nblock);
265
266 fclose(fr);
267 fclose(fw);
268
269 remove(temporaryFileName);
270
271 return 0;
272}
char * get_temporary_filename(Environment *_environment)
int size
Definition _optimizer.c:678
Variable * point(Environment *_environment, char *_x, char *_y)
Definition point.c:46
int frmt(Environment *_environment, char *ptr, char size, char *out)
Definition _cleanup.c:59
void end(Environment *_environment)
Emit ASM code for END.
Definition end.c:91
char * exeFileName
Definition ugbc.h:2290
int to8_convertbintok7_original(Environment *_environment)
#define MAX_TEMPORARY_STORAGE
Definition ugbc.h:563
struct _Environment Environment
Structure of compilation environment.
#define CRITICAL_CANNOT_OPEN_EXECUTABLE_FILE(c)
Definition ugbc.h:3520
#define CRITICAL_BUILD_INVALID_FILENAME_K7(f)
Definition ugbc.h:3869
#define BUILD_SAFE_MOVE(_environment, source, destination)
Definition ugbc.h:4751
@ L
Definition ugbc.tab.h:508
char * strcopy(char *_dest, char *_source)