1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
|
/*
* Convert an Atari 400/800/... cartridge rom to a CART file for the
* atari800 emulator (atari800.github.io)
*
* Copyright (C) 2001-2010 Piotr Fusik
* Copyright (C) 2001-2020 Atari800 development team (see DOC/CREDITS)
*
* This file is part of the Atari800 emulator project which emulates
* the Atari 400, 800, 800XL, 130XE, and 5200 8-bit computers.
*
* Atari800 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; either version 2 of the License, or
* (at your option) any later version.
*
* Atari800 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 Atari800; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <errno.h>
#include <stdlib.h>
#include "cartridge.h"
#define URL "https://github.com/atari800/atari800/blob/master/DOC/cart.txt"
void minmaxcheck(int len);
long flen(FILE *fp);
void list_cartridges(void);
void list_cartridges_size(long len);
void convert(char *romfile, char *cartfile, int32_t type);
void predict(char *romfile);
typedef struct {
uint8_t cart[4];
uint8_t type[4];
uint8_t csum[4];
uint8_t zero[4];
} header_t;
void minmaxcheck(int len)
{
if (len < CARTRIDGE_MIN_SIZE) {
fprintf(stderr,
"Error: Romfile is too small (<%d bytes)\n",
CARTRIDGE_MIN_SIZE);
exit(1);
}
if (len > CARTRIDGE_MAX_SIZE) {
fprintf(stderr,
"Error: Romfile exceeds maximum cartridge size "
"(>%d KB)\n",
CARTRIDGE_MAX_SIZE >> 10);
exit(1);
}
}
long flen(FILE *fp)
{
long l;
fseek(fp, 0, SEEK_END);
l = ftell(fp);
rewind(fp);
return l;
}
void list_cartridges(void)
{
int i;
printf("Supported cartridge types:\n");
for (i = 1; i < CARTRIDGE_TYPE_COUNT; i++)
printf(" Type %d: %s\n", i, CARTRIDGES[i].description);
}
void list_cartridges_size(long size)
{
int i;
int match = 0;
printf("Romsize: %ld KB\n", size >> 10);
minmaxcheck(size);
printf("Probable Cartridge types:\n");
for (i = 1; i < CARTRIDGE_TYPE_COUNT; i++) {
if (CARTRIDGES[i].kb == (size >> 10)) {
match = 1;
printf(" Type %d: %s\n", i, CARTRIDGES[i].description);
}
}
if (!match)
printf("No match found.\n");
}
void convert(char *romfile, char *cartfile, int type)
{
FILE *fp;
long len;
uint8_t *rom;
uint32_t csum;
header_t h;
if (strcmp(romfile, cartfile) == 0) {
fprintf(stderr, "Error: Romfile and cartfile can't be the "
"same\n");
exit(1);
}
printf("Converting %s to %s (type %d - %s)\n", romfile, cartfile, type,
CARTRIDGES[type].description);
/* Read the original rom file */
fp = fopen(romfile, "rb");
if (fp == NULL) {
fprintf(stderr, "Error opening rom file '%s': %s\n",
romfile, strerror(errno));
exit(1);
}
/* Check the rom length */
len = flen(fp);
minmaxcheck(len);
if (len > CARTRIDGES[type].kb * 1024) {
fprintf(stderr,"Error: Romfile size is too large for cartridge "
"type %d (>%d KB)\n",
type, CARTRIDGES[type].kb);
exit(1);
}
rom = malloc(len);
if (rom == NULL) {
fprintf(stderr, "Memory allocation error: %s\n",
strerror(errno));
exit(1);
}
if (fread(rom, 1, len, fp) < len) {
fprintf(stderr, "Read error: %s\n", strerror(errno));
exit(1);
}
fclose(fp);
/* Create the CART header */
memset(&h, 0, sizeof(h));
h.cart[0] = 'C';
h.cart[1] = 'A';
h.cart[2] = 'R';
h.cart[3] = 'T';
h.type[0] = (type & 0xff000000) >> 24;
h.type[1] = (type & 0xff0000) >> 16;
h.type[2] = (type & 0xff00) >> 8;
h.type[3] = type & 0xff;
csum = CARTRIDGE_Checksum(rom, len);
h.csum[0] = (csum & 0xff000000) >> 24;
h.csum[1] = (csum & 0xff0000) >> 16;
h.csum[2] = (csum & 0xff00) >> 8;
h.csum[3] = csum & 0xff;
/* Create the new file, merge the header and rom data */
fp = fopen(cartfile, "w");
if (fp == NULL) {
fprintf(stderr, "Error creating cartridge file '%s': %s\n",
cartfile, strerror(errno));
exit(1);
}
if (fwrite(&h, sizeof(h), 1, fp) != 1) {
fprintf(stderr, "Error writing CAR header: %s\n",
strerror(errno));
exit(1);
}
if (fwrite(rom, len, 1, fp) != 1) {
fprintf(stderr, "Error writing ROM body: %s\n",
strerror(errno));
exit(1);
}
fclose(fp);
}
void predict(char *romfile)
{
FILE *fp;
long len;
/* Read the original rom file */
fp = fopen(romfile, "rb");
if (fp == NULL) {
fprintf(stderr, "Error opening rom file '%s': %s\n",
romfile, strerror(errno));
exit(1);
}
len = flen(fp);
fclose(fp);
list_cartridges_size(len);
}
int main(int argc, char *argv[])
{
int32_t ctype;
/* List supported cartridges */
if (argc == 2 && strcmp(argv[1], "-l") == 0) {
list_cartridges();
exit(0);
}
/* Try to predict possible cartridge types */
if (argc == 3 && strcmp(argv[1], "-p") == 0) {
predict(argv[2]);
exit(0);
}
/* Help */
if (argc != 4) {
printf("%s - Convert romfile to cartridge file\n", argv[0]);
printf("\t%s <romfile> <cartfile> <carttype>\n", argv[0]);
printf("\t%s -l -- List supported cartridge types\n", argv[0]);
printf("\t%s -p <romfile> -- List probable cartridge types "
"for <romfile>\n", argv[0]);
exit(1);
}
/* Convert */
ctype = atol(argv[3]);
if (ctype <= CARTRIDGE_NONE || ctype >= CARTRIDGE_TYPE_COUNT) {
fprintf(stderr, "Error: invalid cartridge type %s\n", argv[3]);
fprintf(stderr, "Run '%s -l' to see valid cartridge list or "
"visit:\n", argv[0]);
fprintf(stderr, "%s\n", URL);
exit(1);
}
convert(argv[1], argv[2], ctype);
return 0;
}
|