http://lacklustre.net/redmine/projects/ems-flash/wiki
que intenta crear un controlador para linux para este dispositivo
http://store.kitsch-bent.com/product/usb-64m-smart-card
Estoy tratando de crear una biblioteca dinámica para usar en gambas
con funciones EXTERN
pero no logro compilar :S
aca el código.
Spoiler: [ Mostrar ]
Spoiler: [ Ocultar ]
/**
libems is a library for interacting with the
ems flash cart 64M.
*/
#include <err>
#include <ctype>
#include <stdio>
#include <stdlib>
#include <string>
#include "ems.h"
#include "libems.h"
int libems_read_sram(char * file_path){
int r;
int blocksize = BLOCKSIZE_READ;
uint32_t offset = 0;
uint32_t base = 0;
unsigned char *buf = malloc(blocksize);
if (buf == NULL)
err(1, "Malloc Error");
r = ems_init();
if (r < 0) errx(1,"Couldn't initialize ems");
FILE *save_file = fopen(file_path, "w");
if (save_file == NULL)
err(1, "Can't open %s for writing", file_path);
printf("Saving SAVE into %s\n", file_path);
while ((offset + blocksize) <= limits[FROM_SRAM]) {
r = ems_read(FROM_SRAM, offset + base, buf, blocksize);
if (r < 0) {
warnx("can't read %d bytes at offset %u\n", blocksize, offset);
return 1;
}
r = fwrite(buf, blocksize, 1, save_file);
if (r != 1)
err(1, "can't write %d bytes into file at offset %u", blocksize, offset);
offset += blocksize;
}
fclose(save_file);
printf("Successfully wrote %u bytes into %s\n", offset, file_path);
free(buf);
return 0;
}
int libems_read_rom(int bank, char * file_path){
int r;
int blocksize = BLOCKSIZE_READ;
uint32_t offset = 0;
uint32_t base;
unsigned char *buf = malloc(blocksize);
if (buf == NULL)
err(1, "Malloc Error");
if(bank <1>= 2)
bank = 1;
base = bank * BANK_SIZE;
r = ems_init();
if (r < 0) errx(1,"Couldn't initialize ems");
FILE *save_file = fopen(file_path, "w");
if (save_file == NULL)
err(1, "Can't open %s for writing", file_path);
printf("Saving ROM into %s\n", file_path);
while ((offset + blocksize) <= limits[FROM_ROM]) {
r = ems_read(FROM_ROM, offset + base, buf, blocksize);
if (r < 0) {
warnx("can't read %d bytes at offset %u\n", blocksize, offset);
return 1;
}
r = fwrite(buf, blocksize, 1, save_file);
if (r != 1)
err(1, "can't write %d bytes into file at offset %u", blocksize, offset);
offset += blocksize;
}
fclose(save_file);
printf("Successfully wrote %u bytes into %s\n", offset, file_path);
free(buf);
return 0;
}
int libems_write_sram(char * file_path){
int r;
int blocksize = BLOCKSIZE_WRITE;
uint32_t offset = 0;
uint32_t base = 0;
unsigned char *buf = malloc(blocksize);
if (buf == NULL)
err(1, "Malloc Error");
r = ems_init();
if (r < 0) errx(1,"Couldn't initialize ems");
FILE *write_file = fopen(file_path, "r");
if (write_file == NULL)
err(1, "Can't open SAVE file %s", file_path);
printf("Writing SAVE file %s\n", file_path);
while ((offset + blocksize) <= limits[TO_ROM] && fread(buf, blocksize, 1, write_file) == 1) {
r = ems_write(TO_ROM, offset + base, buf, blocksize);
if (r < 0) {
warnx("Can't write %d bytes at offset %u", blocksize, offset);
return 1;
}
offset += blocksize;
}
fclose(write_file);
printf("Successfully wrote %u from %s\n", offset, file_path);
free(buf);
return 0;
}
int libems_write_rom(int bank,char * file_path){
int r;
int blocksize = BLOCKSIZE_WRITE;
uint32_t offset = 0;
uint32_t base;
unsigned char *buf = malloc(blocksize);
if (buf == NULL)
err(1, "Malloc Error");
if(bank <1>= 2)
bank = 1;
base = bank * BANK_SIZE;
r = ems_init();
if (r < 0) errx(1,"Couldn't initialize ems");
FILE *write_file = fopen(file_path, "r");
if (write_file == NULL)
err(1, "Can't open ROM file %s", file_path);
printf("Writing ROM file %s\n", file_path);
while ((offset + blocksize) <= limits[TO_ROM] && fread(buf, blocksize, 1, write_file) == 1) {
r = ems_write(TO_ROM, offset + base, buf, blocksize);
if (r < 0) {
warnx("Can't write %d bytes at offset %u", blocksize, offset);
return 1;
}
offset += blocksize;
}
fclose(write_file);
printf("Successfully wrote %u from %s\n", offset, file_path);
free(buf);
return 0;
}
int libems_hardware(int bank){
unsigned char buf[512];
int r;
r = ems_init();
if (r < 0) errx(1,"Couldn't initialize ems");
if(bank <1>= 2)
bank = 1;
printf("Hardware support:\n");
if(bank <= 1){
r = ems_read(FROM_ROM, 0, buf, 512);
if(r < 0) errx(1, "Couldn't read ROM header at bank 0, offset 0, len 512\n"); // Error when reading
if ((buf[HEADER_CGBFLAG] & 128) && (buf[HEADER_CGBFLAG] & 64)) {
printf("CGB\n");
} else if ((buf[HEADER_CGBFLAG] & 128) && (buf[HEADER_CGBFLAG] & 64) && buf[HEADER_SGBFLAG] == 0x03) {
printf("CGB <SGB>, not real option set\n");
} else if ((buf[HEADER_CGBFLAG] & 128) && buf[HEADER_SGBFLAG] == 0x03) {
printf("DMG <CGB>\n");
} else if ((buf[HEADER_CGBFLAG] & 128)) {
printf("DMG <CGB>\n");
} else if (buf[HEADER_SGBFLAG] == 0x03) {
printf("DMG <SGB>\n");
} else {
printf("DMG\n");
}
} else {
r = ems_read(FROM_ROM, BANK_SIZE, buf, 512);
if (r < 0) errx(1, "Couldn't read ROM header at bank 1, offset 0, len 512\n");
if (buf[HEADER_CGBFLAG] == 0x80 && buf[HEADER_SGBFLAG] == 0x03) {
printf("CGB enhanced, SGB enhanced, DMG compatible\n");
} else if (buf[HEADER_CGBFLAG] == 0x80) {
printf("CGB enhanced, DMG compatible\n");
} else if (buf[HEADER_CGBFLAG] == 0xC0) {
printf("CGB only\n");
} else if (buf[HEADER_CGBFLAG] == 0xC0 && buf[HEADER_SGBFLAG] == 0x03) {
printf("CGB only, SGB enhanced (not a real set of options)\n");
} else if (buf[HEADER_SGBFLAG] == 0x03) {
printf("DMG, SGB enhanced\n");
} else {
printf("DMG\n");
}
}
//free(buf);
return 0;
}
int libems_rom_size(int bank){
unsigned char buf[512];
int r;
r = ems_init();
if (r < 0) errx(1,"Couldn't initialize ems");
//if(bank == NULL) bank = 1;
if(bank <1>= 2)
bank = 2;
printf("Reading ROM from Bank %d", bank);
if(bank <= 1){
r = ems_read(FROM_ROM, 0, buf, 512);
if(r < 0) errx(1, "Couldn't read ROM header at bank 0, offset 0, len 512\n"); // Error when reading
} else {
r = ems_read(FROM_ROM, BANK_SIZE, buf, 512);
if (r < 0) errx(1, "Couldn't read ROM header at bank 1, offset 0, len 512\n");
}
switch (buf[HEADER_ROMSIZE]) {
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
printf("%u KB ROM\n", 32 << buf[HEADER_ROMSIZE]);
break;
case 0x52:
printf("1152 KB ROM\n");
break;
case 0x53:
printf("1280 KB ROM\n");
break;
case 0x54:
printf("1536 KB ROM\n");
break;
default:
printf("Unknown ROM size code\n");
break;
}
//free(buf);
return 0;
}
int libems_checksum(int bank){
unsigned char buf[512];
uint8_t calculated_chk = 0;
int i,r;
//if(bank == NULL) bank = 1;
r = ems_init();
if (r < 0) errx(1,"Couldn't initialize ems");
if(bank <1>= 2)
bank = 2;
printf("Reading ROM from Bank %d", bank);
if(bank <= 1){
r = ems_read(FROM_ROM, 0, buf, 512);
if(r < 0) errx(1, "Couldn't read ROM header at bank 0, offset 0, len 512\n"); // Error when reading
} else {
r = ems_read(FROM_ROM, BANK_SIZE, buf, 512);
if (r < 0) errx(1, "Couldn't read ROM header at bank 1, offset 0, len 512\n");
}
// Verify cartridge header checksum
calculated_chk = 0;
for (i = HEADER_TITLE; i < HEADER_CHKSUM; i++) {
calculated_chk -= buf[i] - 1;
}
if (calculated_chk != buf[HEADER_CHKSUM]) {
printf("Cartridge header checksum invalid. This game will NOT boot on real hardware.\n");
} else {
printf("Cartridge header checksum OK. This game may boot on real hardware.\n");
}
if (buf[HEADER_SGBFLAG] == 0x03 && buf[HEADER_OLDLICENSEE] != 0x33) {
printf("SGB functions were enabled, but Old License field is not set to 33h. This game will not be able to use SGB functions on real hardware.\n");
}
//free(buf);
return 0;
}
void libems_version(void){
printf("EMS-flasher-lib %s\n", VERSION);
}
libems is a library for interacting with the
ems flash cart 64M.
*/
#include <err>
#include <ctype>
#include <stdio>
#include <stdlib>
#include <string>
#include "ems.h"
#include "libems.h"
int libems_read_sram(char * file_path){
int r;
int blocksize = BLOCKSIZE_READ;
uint32_t offset = 0;
uint32_t base = 0;
unsigned char *buf = malloc(blocksize);
if (buf == NULL)
err(1, "Malloc Error");
r = ems_init();
if (r < 0) errx(1,"Couldn't initialize ems");
FILE *save_file = fopen(file_path, "w");
if (save_file == NULL)
err(1, "Can't open %s for writing", file_path);
printf("Saving SAVE into %s\n", file_path);
while ((offset + blocksize) <= limits[FROM_SRAM]) {
r = ems_read(FROM_SRAM, offset + base, buf, blocksize);
if (r < 0) {
warnx("can't read %d bytes at offset %u\n", blocksize, offset);
return 1;
}
r = fwrite(buf, blocksize, 1, save_file);
if (r != 1)
err(1, "can't write %d bytes into file at offset %u", blocksize, offset);
offset += blocksize;
}
fclose(save_file);
printf("Successfully wrote %u bytes into %s\n", offset, file_path);
free(buf);
return 0;
}
int libems_read_rom(int bank, char * file_path){
int r;
int blocksize = BLOCKSIZE_READ;
uint32_t offset = 0;
uint32_t base;
unsigned char *buf = malloc(blocksize);
if (buf == NULL)
err(1, "Malloc Error");
if(bank <1>= 2)
bank = 1;
base = bank * BANK_SIZE;
r = ems_init();
if (r < 0) errx(1,"Couldn't initialize ems");
FILE *save_file = fopen(file_path, "w");
if (save_file == NULL)
err(1, "Can't open %s for writing", file_path);
printf("Saving ROM into %s\n", file_path);
while ((offset + blocksize) <= limits[FROM_ROM]) {
r = ems_read(FROM_ROM, offset + base, buf, blocksize);
if (r < 0) {
warnx("can't read %d bytes at offset %u\n", blocksize, offset);
return 1;
}
r = fwrite(buf, blocksize, 1, save_file);
if (r != 1)
err(1, "can't write %d bytes into file at offset %u", blocksize, offset);
offset += blocksize;
}
fclose(save_file);
printf("Successfully wrote %u bytes into %s\n", offset, file_path);
free(buf);
return 0;
}
int libems_write_sram(char * file_path){
int r;
int blocksize = BLOCKSIZE_WRITE;
uint32_t offset = 0;
uint32_t base = 0;
unsigned char *buf = malloc(blocksize);
if (buf == NULL)
err(1, "Malloc Error");
r = ems_init();
if (r < 0) errx(1,"Couldn't initialize ems");
FILE *write_file = fopen(file_path, "r");
if (write_file == NULL)
err(1, "Can't open SAVE file %s", file_path);
printf("Writing SAVE file %s\n", file_path);
while ((offset + blocksize) <= limits[TO_ROM] && fread(buf, blocksize, 1, write_file) == 1) {
r = ems_write(TO_ROM, offset + base, buf, blocksize);
if (r < 0) {
warnx("Can't write %d bytes at offset %u", blocksize, offset);
return 1;
}
offset += blocksize;
}
fclose(write_file);
printf("Successfully wrote %u from %s\n", offset, file_path);
free(buf);
return 0;
}
int libems_write_rom(int bank,char * file_path){
int r;
int blocksize = BLOCKSIZE_WRITE;
uint32_t offset = 0;
uint32_t base;
unsigned char *buf = malloc(blocksize);
if (buf == NULL)
err(1, "Malloc Error");
if(bank <1>= 2)
bank = 1;
base = bank * BANK_SIZE;
r = ems_init();
if (r < 0) errx(1,"Couldn't initialize ems");
FILE *write_file = fopen(file_path, "r");
if (write_file == NULL)
err(1, "Can't open ROM file %s", file_path);
printf("Writing ROM file %s\n", file_path);
while ((offset + blocksize) <= limits[TO_ROM] && fread(buf, blocksize, 1, write_file) == 1) {
r = ems_write(TO_ROM, offset + base, buf, blocksize);
if (r < 0) {
warnx("Can't write %d bytes at offset %u", blocksize, offset);
return 1;
}
offset += blocksize;
}
fclose(write_file);
printf("Successfully wrote %u from %s\n", offset, file_path);
free(buf);
return 0;
}
int libems_hardware(int bank){
unsigned char buf[512];
int r;
r = ems_init();
if (r < 0) errx(1,"Couldn't initialize ems");
if(bank <1>= 2)
bank = 1;
printf("Hardware support:\n");
if(bank <= 1){
r = ems_read(FROM_ROM, 0, buf, 512);
if(r < 0) errx(1, "Couldn't read ROM header at bank 0, offset 0, len 512\n"); // Error when reading
if ((buf[HEADER_CGBFLAG] & 128) && (buf[HEADER_CGBFLAG] & 64)) {
printf("CGB\n");
} else if ((buf[HEADER_CGBFLAG] & 128) && (buf[HEADER_CGBFLAG] & 64) && buf[HEADER_SGBFLAG] == 0x03) {
printf("CGB <SGB>, not real option set\n");
} else if ((buf[HEADER_CGBFLAG] & 128) && buf[HEADER_SGBFLAG] == 0x03) {
printf("DMG <CGB>\n");
} else if ((buf[HEADER_CGBFLAG] & 128)) {
printf("DMG <CGB>\n");
} else if (buf[HEADER_SGBFLAG] == 0x03) {
printf("DMG <SGB>\n");
} else {
printf("DMG\n");
}
} else {
r = ems_read(FROM_ROM, BANK_SIZE, buf, 512);
if (r < 0) errx(1, "Couldn't read ROM header at bank 1, offset 0, len 512\n");
if (buf[HEADER_CGBFLAG] == 0x80 && buf[HEADER_SGBFLAG] == 0x03) {
printf("CGB enhanced, SGB enhanced, DMG compatible\n");
} else if (buf[HEADER_CGBFLAG] == 0x80) {
printf("CGB enhanced, DMG compatible\n");
} else if (buf[HEADER_CGBFLAG] == 0xC0) {
printf("CGB only\n");
} else if (buf[HEADER_CGBFLAG] == 0xC0 && buf[HEADER_SGBFLAG] == 0x03) {
printf("CGB only, SGB enhanced (not a real set of options)\n");
} else if (buf[HEADER_SGBFLAG] == 0x03) {
printf("DMG, SGB enhanced\n");
} else {
printf("DMG\n");
}
}
//free(buf);
return 0;
}
int libems_rom_size(int bank){
unsigned char buf[512];
int r;
r = ems_init();
if (r < 0) errx(1,"Couldn't initialize ems");
//if(bank == NULL) bank = 1;
if(bank <1>= 2)
bank = 2;
printf("Reading ROM from Bank %d", bank);
if(bank <= 1){
r = ems_read(FROM_ROM, 0, buf, 512);
if(r < 0) errx(1, "Couldn't read ROM header at bank 0, offset 0, len 512\n"); // Error when reading
} else {
r = ems_read(FROM_ROM, BANK_SIZE, buf, 512);
if (r < 0) errx(1, "Couldn't read ROM header at bank 1, offset 0, len 512\n");
}
switch (buf[HEADER_ROMSIZE]) {
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
printf("%u KB ROM\n", 32 << buf[HEADER_ROMSIZE]);
break;
case 0x52:
printf("1152 KB ROM\n");
break;
case 0x53:
printf("1280 KB ROM\n");
break;
case 0x54:
printf("1536 KB ROM\n");
break;
default:
printf("Unknown ROM size code\n");
break;
}
//free(buf);
return 0;
}
int libems_checksum(int bank){
unsigned char buf[512];
uint8_t calculated_chk = 0;
int i,r;
//if(bank == NULL) bank = 1;
r = ems_init();
if (r < 0) errx(1,"Couldn't initialize ems");
if(bank <1>= 2)
bank = 2;
printf("Reading ROM from Bank %d", bank);
if(bank <= 1){
r = ems_read(FROM_ROM, 0, buf, 512);
if(r < 0) errx(1, "Couldn't read ROM header at bank 0, offset 0, len 512\n"); // Error when reading
} else {
r = ems_read(FROM_ROM, BANK_SIZE, buf, 512);
if (r < 0) errx(1, "Couldn't read ROM header at bank 1, offset 0, len 512\n");
}
// Verify cartridge header checksum
calculated_chk = 0;
for (i = HEADER_TITLE; i < HEADER_CHKSUM; i++) {
calculated_chk -= buf[i] - 1;
}
if (calculated_chk != buf[HEADER_CHKSUM]) {
printf("Cartridge header checksum invalid. This game will NOT boot on real hardware.\n");
} else {
printf("Cartridge header checksum OK. This game may boot on real hardware.\n");
}
if (buf[HEADER_SGBFLAG] == 0x03 && buf[HEADER_OLDLICENSEE] != 0x33) {
printf("SGB functions were enabled, but Old License field is not set to 33h. This game will not be able to use SGB functions on real hardware.\n");
}
//free(buf);
return 0;
}
void libems_version(void){
printf("EMS-flasher-lib %s\n", VERSION);
}
Cualquier ayuda será bienvenida.
Gracias y Saludos
libems.tar.gz | ||
Descripción: | Libems | Descargar |
Nombre del archivo: | libems.tar.gz | |
Tamaño: | 4.88 KB | |
Descargado: | 25 veces |
libems.tar.gz | ||
Descripción: | Libems | Descargar |
Nombre del archivo: | libems.tar.gz | |
Tamaño: | 4.88 KB | |
Descargado: | 25 veces |
libems.tar.gz | ||
Descripción: | Libems | Descargar |
Nombre del archivo: | libems.tar.gz | |
Tamaño: | 4.88 KB | |
Descargado: | 25 veces |