sdk: added `Bit2Bin` tool

This commit is contained in:
Ivan Tatarinov 2021-04-25 14:12:04 +03:00
parent d58ac43c82
commit 4ff18c37f4
5 changed files with 160 additions and 99 deletions

Binary file not shown.

1
sdk/bin/.gitignore vendored
View File

@ -11,3 +11,4 @@ fpad
fpoke
GenRom
AddItem
Bit2Bin

BIN
sdk/bin/Bit2Bin.exe Executable file

Binary file not shown.

View File

@ -0,0 +1,11 @@
SPDX-FileName: Bit2Bin.exe
SPDX-FileType: BINARY
SPDX-FileChecksum: SHA1: f1484d968816b72f7c1a246e11a468c175e8822a
SPDX-FileCopyrightText: Copyright (C) 2019-2021 Antonio Villena
SPDX-License-Identifier: GPL-3.0-only
SPDX-FileComment: Bit2Bin version 0.05 (2020-02-19) - strip .bit header and align binary to 16K.

View File

@ -1,99 +1,148 @@
#include <stdio.h>
#include <stdlib.h>
FILE *fi, *fo;
int i, length;
unsigned char mem[0x4000];
unsigned short j;
int main(int argc, char *argv[]) {
if( argc==1 )
printf("\n"
"Bit2Bin v0.05, strip .bit header and align binary to 16k, 2020-02-19\n\n"
" Bit2Bin <input_file> <output_file>\n\n"
" <input_file> Input BIT file\n"
" <output_file> Output BIN file\n\n"
"All params are mandatory\n\n"),
exit(0);
if( argc!=3 )
printf("\nInvalid number of parameters\n"),
exit(-1);
fi= fopen(argv[1], "rb");
if( !fi )
printf("\nInput file not found: %s\n", argv[1]),
exit(-1);
fseek(fi, 0, SEEK_END);
i= ftell(fi);
fseek(fi, 0, SEEK_SET);
fread(mem, 1, 2, fi);
i-= (j= mem[1]|mem[0]<<8)+4;
fread(mem, 1, j+2, fi);
i-= (j= mem[j+1]|mem[j]<<8)+3;
fread(mem, 1, j+3, fi);
i-= (j= mem[j+1]|mem[j]<<8)+3;
fread(mem, 1, j+3, fi);
i-= (j= mem[j+1]|mem[j]<<8)+3;
fread(mem, 1, j+3, fi);
i-= (j= mem[j+1]|mem[j]<<8)+3;
fread(mem, 1, j+3, fi);
i-= (j= mem[j+1]|mem[j]<<8)+4;
fread(mem, 1, j+4, fi);
length= mem[j+3]|mem[j+2]<<8|mem[j+1]<<16|mem[j]<<24;
if( i!=length )
printf("\nInvalid file length\n"),
exit(-1);
fo= fopen(argv[2], "wb+");
if( !fo )
printf("\nCannot create output file: %s\n", argv[2]),
exit(-1);
j= i>>14;
if( j>71 ){
for ( i= 0; i<72; i++ )
fread(mem, 1, 0x4000, fi),
fwrite(mem, 1, 0x4000, fo);
fclose(fo);
argv[2][strlen(argv[2])-5]++;
if(argv[2][strlen(argv[2])-5]==':')
if(strlen(argv[2])==9)
argv[2]= "CORE10.ZX3";
else
argv[2][4]++,
argv[2][5]='0';
fo= fopen(argv[2], "wb+");
if( !fo )
printf("\nCannot create output file: %s\n", argv[2]),
exit(-1);
for ( i= 0; i<j-72; i++ )
fread(mem, 1, 0x4000, fi),
fwrite(mem, 1, 0x4000, fo);
memset(mem, 0, 0x4000);
fread(mem, 1, length&0x3fff, fi),
fwrite(mem, 1, 0x4000, fo);
memset(mem, 0, 0x4000);
for ( i= 0; i<143-j; i++ )
fwrite(mem, 1, 0x4000, fo);
}
else{
if( j )
for ( i= 0; i<j; i++ )
fread(mem, 1, 0x4000, fi),
fwrite(mem, 1, 0x4000, fo);
memset(mem, 0, 0x4000);
fread(mem, 1, length&0x3fff, fi),
fwrite(mem, 1, 0x4000, fo);
memset(mem, 0, 0x4000);
if( j>48 )
for ( i= 0; i<71-j; i++ )
fwrite(mem, 1, 0x4000, fo);
else if( j>28 )
for ( i= 0; i<48-j; i++ )
fwrite(mem, 1, 0x4000, fo);
else if( j>20 )
for ( i= 0; i<28-j; i++ )
fwrite(mem, 1, 0x4000, fo);
else
for ( i= 0; i<20-j; i++ )
fwrite(mem, 1, 0x4000, fo);
}
printf("\nFile generated successfully\n");
}
/*
* Bit2Bin - strip .bit header and align binary to 16K.
*
* Copyright (C) 2019-2021 Antonio Villena
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* SPDX-FileCopyrightText: Copyright (C) 2019-2021 Antonio Villena
*
* SPDX-License-Identifier: GPL-3.0-only
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define PROGRAM "Bit2Bin"
#define DESCRIPTION "strip .bit header and align binary to 16K."
#define VERSION "0.05 (2020-02-19)"
#define COPYRIGHT "Copyright (C) 2019-2021 Antonio Villena"
#define LICENSE \
"This program is free software: you can redistribute it and/or modify\n" \
"it under the terms of the GNU General Public License as published by\n" \
"the Free Software Foundation, version 3."
#define HOMEPAGE "https://github.com/zxdos/zxuno/"
FILE *fi, *fo;
int i, length;
unsigned char mem[0x4000];
unsigned short j;
void show_help() {
printf(
PROGRAM " version " VERSION " - " DESCRIPTION "\n"
COPYRIGHT "\n"
LICENSE "\n"
"Home page: " HOMEPAGE "\n"
"\n"
"Usage:\n"
" " PROGRAM " <input_file> <output_file>\n"
"\n"
" <input_file> Input BIT file\n"
" <output_file> Output BIN file\n"
"\n"
"All parameters are mandatory.\n"
);
}
int main(int argc, char *argv[]) {
if( argc==1 )
show_help(),
exit(0);
if( argc!=3 )
printf("Invalid number of parameters\n"),
exit(-1);
fi= fopen(argv[1], "rb");
if( !fi )
printf("Input file not found: %s\n", argv[1]),
exit(-1);
fseek(fi, 0, SEEK_END);
i= ftell(fi);
fseek(fi, 0, SEEK_SET);
fread(mem, 1, 2, fi);
i-= (j= mem[1]|mem[0]<<8)+4;
fread(mem, 1, j+2, fi);
i-= (j= mem[j+1]|mem[j]<<8)+3;
fread(mem, 1, j+3, fi);
i-= (j= mem[j+1]|mem[j]<<8)+3;
fread(mem, 1, j+3, fi);
i-= (j= mem[j+1]|mem[j]<<8)+3;
fread(mem, 1, j+3, fi);
i-= (j= mem[j+1]|mem[j]<<8)+3;
fread(mem, 1, j+3, fi);
i-= (j= mem[j+1]|mem[j]<<8)+4;
fread(mem, 1, j+4, fi);
length= mem[j+3]|mem[j+2]<<8|mem[j+1]<<16|mem[j]<<24;
if( i!=length )
printf("Invalid file length\n"),
exit(-1);
fo= fopen(argv[2], "wb+");
if( !fo )
printf("Cannot create output file: %s\n", argv[2]),
exit(-1);
j= i>>14;
if( j>71 ){
for ( i= 0; i<72; i++ )
fread(mem, 1, 0x4000, fi),
fwrite(mem, 1, 0x4000, fo);
fclose(fo);
argv[2][strlen(argv[2])-5]++;
if(argv[2][strlen(argv[2])-5]==':')
if(strlen(argv[2])==9)
argv[2]= "CORE10.ZX3";
else
argv[2][4]++,
argv[2][5]='0';
fo= fopen(argv[2], "wb+");
if( !fo )
printf("Cannot create output file: %s\n", argv[2]),
exit(-1);
for ( i= 0; i<j-72; i++ )
fread(mem, 1, 0x4000, fi),
fwrite(mem, 1, 0x4000, fo);
memset(mem, 0, 0x4000);
fread(mem, 1, length&0x3fff, fi),
fwrite(mem, 1, 0x4000, fo);
memset(mem, 0, 0x4000);
for ( i= 0; i<143-j; i++ )
fwrite(mem, 1, 0x4000, fo);
}
else{
if( j )
for ( i= 0; i<j; i++ )
fread(mem, 1, 0x4000, fi),
fwrite(mem, 1, 0x4000, fo);
memset(mem, 0, 0x4000);
fread(mem, 1, length&0x3fff, fi),
fwrite(mem, 1, 0x4000, fo);
memset(mem, 0, 0x4000);
if( j>48 )
for ( i= 0; i<71-j; i++ )
fwrite(mem, 1, 0x4000, fo);
else if( j>28 )
for ( i= 0; i<48-j; i++ )
fwrite(mem, 1, 0x4000, fo);
else if( j>20 )
for ( i= 0; i<28-j; i++ )
fwrite(mem, 1, 0x4000, fo);
else
for ( i= 0; i<20-j; i++ )
fwrite(mem, 1, 0x4000, fo);
}
printf("File `%s' successfully created\n", argv[2]);
}