-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
James Holodnak
committed
Feb 27, 2016
1 parent
5fa9b12
commit 30993c0
Showing
12 changed files
with
1,557 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,9 +13,11 @@ | |
*.D | ||
*.gd | ||
|
||
*.res | ||
*.sdf | ||
*.opendb | ||
*.user | ||
*.aps | ||
|
||
cli/vs2015/Debug | ||
cli/vs2015/Release | ||
|
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,198 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <ctype.h> | ||
#include "Disk.h" | ||
#include "Device.h" | ||
#include "Fdsemu.h" | ||
#include "flashrw.h" | ||
|
||
int detect_format(char *filename) | ||
{ | ||
FILE *fp; | ||
uint8_t buf[128]; | ||
int ret = FORMAT_UNKNOWN; | ||
|
||
printf("Writing disk image '%s'...\n", filename); | ||
if ((fp = fopen(filename, "rb")) == 0) { | ||
printf("error opening file: %s\n", filename); | ||
} | ||
else if (fread(buf, 1, 128, fp) != 128) { | ||
printf("error reading disk image\n"); | ||
} | ||
else { | ||
|
||
//detect fds format | ||
if (buf[0] == 'F' && buf[1] == 'D' && buf[2] == 'S' && buf[3] == 0x1A) { | ||
printf("Detected fwNES format.\n"); | ||
ret = FORMAT_FDS;// FDS_writeDisk(filename); | ||
} | ||
|
||
else if (buf[0] == 0x01 && buf[1] == 0x2A && buf[2] == 0x4E && buf[0x38] == 0x02) { | ||
printf("Detected FDS format.\n"); | ||
ret = FORMAT_FDS;// FDS_writeDisk(filename); | ||
} | ||
|
||
//detect game doctor format | ||
else if (buf[3] == 0x01 || buf[4] == 0x2A || buf[5] == 0x4E || buf[0x3D] == 0x02) { | ||
printf("Detected Game Doctor format.\n"); | ||
ret = FORMAT_GD;// GD_writeDisk(filename); | ||
} | ||
|
||
else { | ||
printf("unknown disk image format\n"); | ||
} | ||
} | ||
fclose(fp); | ||
return(ret); | ||
} | ||
|
||
CDisk::CDisk() | ||
{ | ||
memset(sides, 0, sizeof(CDiskSide*) * MAX_SIDES); | ||
numsides = 0; | ||
} | ||
|
||
CDisk::~CDisk() | ||
{ | ||
FreeSides(); | ||
} | ||
|
||
void CDisk::FreeSides() | ||
{ | ||
int i; | ||
|
||
for (i = 0; i < numsides; i++) { | ||
delete sides[i]; | ||
} | ||
memset(sides, 0, sizeof(CDiskSide*) * MAX_SIDES); | ||
numsides = 0; | ||
} | ||
|
||
bool CDisk::LoadFDS(char *filename) | ||
{ | ||
uint8_t *buf, *ptr; | ||
int filesize, i; | ||
CDiskSide *side; | ||
|
||
//load entire file into a buffer | ||
if (!loadfile(filename, &buf, &filesize)) { | ||
printf("Error loading %s\n", filename); | ||
return(false); | ||
} | ||
|
||
//load success | ||
ptr = buf; | ||
if (ptr[0] == 'F' && ptr[1] == 'D' && ptr[2] == 'S' && ptr[3] == 0x1A) { | ||
ptr += 16; | ||
filesize -= (filesize - 16) % FDSSIZE; //truncate down to whole disk | ||
} | ||
else { | ||
filesize -= filesize % FDSSIZE; //truncate down to whole disk | ||
} | ||
|
||
//fill sides array with disk data | ||
for (i = 0; i < (filesize / FDSSIZE); i++) { | ||
|
||
side = new CDiskSide(); | ||
if (side->LoadFDS(ptr, filesize - (i * FDSSIZE)) == false) { | ||
delete side; | ||
break; | ||
} | ||
|
||
sides[numsides++] = side; | ||
|
||
ptr += FDSSIZE; | ||
} | ||
|
||
free(buf); | ||
return(true); | ||
} | ||
|
||
bool CDisk::LoadGD(char *filename) | ||
{ | ||
const int DISKSIZE = 0x18000; | ||
char *files[MAX_SIDES + 1], *ptr; | ||
int numfiles = 0; | ||
uint8_t *buf = 0; | ||
uint8_t *binbuf = 0; | ||
int i, len; | ||
CDiskSide *side; | ||
|
||
//find files for this disk image | ||
if (find_doctors(filename, files, &numfiles, MAX_SIDES + 1) == false) { | ||
printf("Failed to find doctor disk sides\n"); | ||
return(false); | ||
} | ||
|
||
//allocate temporary space for storing the bins | ||
binbuf = (uint8_t*)malloc(DISKSIZE); | ||
|
||
//loop thru found files | ||
for (i = 0; i < numfiles; i++) { | ||
ptr = files[i]; | ||
printf("Loading %s...", ptr); | ||
|
||
//load game doctor image file | ||
if (loadfile(ptr, &buf, &len) == false) { | ||
printf("Error loading file\n"); | ||
break; | ||
} | ||
|
||
side = new CDiskSide(); | ||
if (side->LoadGD(buf, len) == false) { | ||
delete side; | ||
break; | ||
} | ||
|
||
//check for save disk extension | ||
if (toupper(ptr[strlen(ptr) - 1]) == 'S') { | ||
side->SetSaveDisk(1); | ||
} | ||
|
||
free(buf); | ||
sides[numsides++] = side; | ||
} | ||
|
||
free(binbuf); | ||
|
||
if (i == numfiles) { | ||
return(true); | ||
} | ||
return(false); | ||
} | ||
|
||
bool CDisk::Load(char *filename) | ||
{ | ||
int format = detect_format(filename); | ||
|
||
switch (format) { | ||
case FORMAT_FDS: | ||
return(LoadFDS(filename)); | ||
case FORMAT_GD: | ||
return(LoadGD(filename)); | ||
default: | ||
printf("unknown format for %s\n", filename); | ||
break; | ||
} | ||
return(false); | ||
} | ||
|
||
bool CDisk::Load(CDiskSide *side) | ||
{ | ||
CDiskSide *sidecopy; | ||
|
||
sidecopy = new CDiskSide(); | ||
sidecopy->Duplicate(side); | ||
sides[numsides++] = sidecopy; | ||
return(true); | ||
} | ||
|
||
bool CDisk::GetBin(int side, uint8_t **bin, int *binsize) | ||
{ | ||
if (side >= numsides) { | ||
return(false); | ||
} | ||
|
||
return(sides[side]->GetBin(bin, binsize)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#pragma once | ||
|
||
#include <stdint.h> | ||
#include "DiskSide.h" | ||
|
||
#define MAX_SIDES 16 | ||
|
||
class CDisk { | ||
private: | ||
protected: | ||
CDiskSide *sides[MAX_SIDES]; | ||
int numsides; | ||
public: | ||
CDisk(); | ||
~CDisk(); | ||
|
||
void FreeSides(); | ||
|
||
bool LoadFDS(char *filename); | ||
bool LoadGD(char *filename); | ||
bool Load(char *filename); | ||
bool Load(CDiskSide *side); | ||
|
||
bool GetBin(int side, uint8_t **bin, int *binsize); | ||
|
||
int GetSides() { | ||
return(numsides); | ||
} | ||
|
||
bool HasSaveDisk() { | ||
for (int i = 0; i < numsides; i++) { | ||
if (sides[i]->GetSaveDisk()) | ||
return(true); | ||
} | ||
return(false); | ||
} | ||
|
||
bool IsSaveDisk(int index) { | ||
if (index < numsides) { | ||
if (sides[index]->GetSaveDisk() != 0) { | ||
return(true); | ||
} | ||
} | ||
return(false); | ||
} | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
#include <string.h> | ||
#include <stdlib.h> | ||
#include "DiskSide.h" | ||
#include "diskutil.h" | ||
#include "Device.h" | ||
|
||
CDiskSide::CDiskSide() | ||
{ | ||
raw = 0; | ||
raw03 = 0; | ||
bin = 0; | ||
rawsize = 0; | ||
binsize = 0; | ||
issavedisk = 0; | ||
} | ||
|
||
CDiskSide::~CDiskSide() | ||
{ | ||
if (raw) { | ||
free(raw); | ||
} | ||
|
||
if (raw03) { | ||
free(raw03); | ||
} | ||
|
||
if (bin) { | ||
free(bin); | ||
} | ||
|
||
} | ||
|
||
const int BINSIZE = 0x18000; | ||
const int LEAD_IN = DEFAULT_LEAD_IN / 8; | ||
|
||
bool CDiskSide::LoadFDS(uint8_t *buf, int bufsize) | ||
{ | ||
|
||
//allocate buffer | ||
bin = (uint8_t*)malloc(BINSIZE); | ||
memset(bin, 0, BINSIZE); | ||
|
||
//convert disk to bin format | ||
binsize = fds_to_bin(bin + LEAD_IN, buf, BINSIZE - LEAD_IN); | ||
|
||
//check if conversion was successful | ||
if (binsize == 0) { | ||
free(bin); | ||
bin = 0; | ||
return(false); | ||
} | ||
|
||
//add lead-in size to converted image size and advance input pointer | ||
binsize += LEAD_IN; | ||
|
||
return(true); | ||
} | ||
|
||
bool CDiskSide::LoadGD(uint8_t *buf, int bufsize) | ||
{ | ||
const int TMPSIZE = 0x20000; | ||
uint8_t *tmp; | ||
|
||
tmp = (uint8_t*)malloc(TMPSIZE); | ||
|
||
//copy to the larger data block | ||
memset(tmp, 0, TMPSIZE); | ||
memcpy(tmp, buf, bufsize); | ||
|
||
//allocate buffer and clear it | ||
bin = (uint8_t*)malloc(BINSIZE); | ||
memset(bin, 0, BINSIZE); | ||
|
||
//convert disk to bin format | ||
binsize = gameDoctor_to_bin(bin + LEAD_IN, tmp, BINSIZE - LEAD_IN); | ||
|
||
//free tmp buffer | ||
free(tmp); | ||
|
||
//check if conversion was successful | ||
if (binsize == 0) { | ||
// printf("Error converting gamedoctor disk image bin\n"); | ||
free(bin); | ||
bin = 0; | ||
return(false); | ||
} | ||
|
||
//account for lead-in data | ||
binsize += LEAD_IN; | ||
|
||
return(true); | ||
} | ||
|
||
bool CDiskSide::Duplicate(CDiskSide *side) | ||
{ | ||
rawsize = side->rawsize; | ||
binsize = side->binsize; | ||
if (side->raw) { | ||
raw = (uint8_t*)malloc(rawsize); | ||
memcpy(raw, side->raw, rawsize); | ||
} | ||
if (side->raw03) { | ||
raw03 = (uint8_t*)malloc(rawsize); | ||
memcpy(raw03, side->raw03, rawsize); | ||
} | ||
if (side->bin) { | ||
bin = (uint8_t*)malloc(binsize); | ||
memcpy(bin, side->bin, binsize); | ||
} | ||
return(true); | ||
} | ||
|
||
bool CDiskSide::GetBin(uint8_t **buf, int *bufsize) | ||
{ | ||
//convert raw to bin if necessary | ||
if (bin == 0) { | ||
if (raw03 == 0) { | ||
if (raw == 0) { | ||
return(false); | ||
} | ||
raw03 = (uint8_t*)malloc(rawsize); | ||
memcpy(raw03, raw, rawsize); | ||
raw_to_raw03(raw03, rawsize); | ||
} | ||
raw03 = (uint8_t*)malloc(rawsize); | ||
raw03_to_bin(raw03, rawsize, &bin, &binsize, &datasize); | ||
} | ||
|
||
*buf = bin; | ||
*bufsize = binsize; | ||
return(true); | ||
} |
Oops, something went wrong.