Updated the CDROM code to better handle non-English pathnames. CUE file format may still not work if the filenames inside the CUE file contain non-ANSI characters, but this is not normally the case.

This commit is contained in:
waltje
2019-03-05 23:22:18 -05:00
parent e8c527c4ef
commit 28630a5c5b
9 changed files with 490 additions and 357 deletions

View File

@@ -8,13 +8,20 @@
*
* CD-ROM image file handling module.
*
* Version: @(#)cdrom_dosbox.cpp 1.0.10 2018/10/23
* Re-hacked to remove the dirname() function, and to have this
* code using stdio instead of C++ fstream - fstream cannot deal
* with Unicode pathnames, and we need those. --FvK
*
* **NOTE** This code will very soon be replaced with a C variant, so
* no more changes will be done.
*
* Version: @(#)cdrom_dosbox.cpp 1.0.11 2019/03/05
*
* Authors: Fred N. van Kempen, <decwiz@yahoo.com>
* Miran Grca, <mgrca8@gmail.com>
* The DOSBox Team, <unknown>
*
* Copyright 2017,2018 Fred N. van Kempen.
* Copyright 2017-2019 Fred N. van Kempen.
* Copyright 2016-2018 Miran Grca.
* Copyright 2002-2015 The DOSBox Team.
*
@@ -38,50 +45,37 @@
*/
#define _LARGEFILE_SOURCE
#define _LARGEFILE64_SOURCE
#ifdef _WIN32
//FIXME: should not be needed. */
# define _GNU_SOURCE
#endif
#define __STDC_FORMAT_MACROS
#include <inttypes.h>
#include <stdio.h>
#include <stdint.h>
#include <sys/stat.h>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <iostream>
#include <limits>
#include <sstream>
#include <vector>
#ifdef ERROR
# undef ERROR
#include <ctype.h>
#include <wchar.h>
#ifdef _WIN32
# include <string.h>
#else
# include <libgen.h>
#endif
#include <vector>
#include "../../emu.h"
#include "../../plat.h"
#include "cdrom_dosbox.h"
#ifndef _WIN32
# include <libgen.h>
#else
# include <string.h>
#endif
using namespace std;
#define MAX_LINE_LENGTH 512
#define MAX_FILENAME_LENGTH 256
#define CROSS_LEN 512
#define safe_strncpy(a,b,n) do { strncpy((a),(b),(n)-1); (a)[(n)-1] = 0; } while (0)
//FIXME: update to use plat_fopen and wchar!
CDROM_Interface_Image::BinaryFile::BinaryFile(const char *filename, bool &error)
CDROM_Interface_Image::BinaryFile::BinaryFile(const wchar_t *filename, bool &error)
{
memset(fn, 0x00, sizeof(fn));
strcpy(fn, filename);
file = fopen64(fn, "rb");
wcscpy(fn, filename);
file = plat_fopen64(fn, L"rb");
DEBUG("CDROM: binary_open(%ls) = %08lx\n", fn, file);
if (file == NULL)
error = true;
else
@@ -100,12 +94,17 @@ CDROM_Interface_Image::BinaryFile::~BinaryFile(void)
bool
CDROM_Interface_Image::BinaryFile::read(Bit8u *buffer, uint64_t seek, size_t count)
CDROM_Interface_Image::BinaryFile::read(uint8_t *buffer, uint64_t seek, size_t count)
{
DEBUG("CDROM: binary_read(%08lx, pos=%" PRIu64 " count=%lu\n",
file, seek, count);
if (file == NULL) return 0;
fseeko64(file, seek, SEEK_SET);
if (fread(buffer, count, 1, file) != 1) return 0;
if (fread(buffer, count, 1, file) != 1) {
ERRLOG("CDROM: binary_read failed!\n");
return 0;
}
return 1;
}
@@ -114,11 +113,16 @@ CDROM_Interface_Image::BinaryFile::read(Bit8u *buffer, uint64_t seek, size_t cou
uint64_t
CDROM_Interface_Image::BinaryFile::getLength(void)
{
off64_t len;
DEBUG("CDROM: binary_length(%08lx)\n", file);
if (file == NULL) return 0;
fseeko64(file, 0, SEEK_END);
len = ftello64(file);
DEBUG("CDROM: binary_length(%08lx) = %" PRIu64 "\n", file, len);
return ftello64(file);
return len;
}
@@ -140,20 +144,20 @@ CDROM_Interface_Image::InitNewMedia(void)
bool
CDROM_Interface_Image::SetDevice(char* path, int forceCD)
CDROM_Interface_Image::SetDevice(const wchar_t *path, int forceCD)
{
(void)forceCD;
if (LoadCueSheet(path)) return true;
if (LoadIsoFile(path)) return true;
if (CueLoadSheet(path)) return true;
if (IsoLoadFile(path)) return true;
// print error message on dosbox console
//printf("Could not load image file: %s\n", path);
return false;
}
bool
CDROM_Interface_Image::GetUPC(unsigned char& attr, char* upc)
CDROM_Interface_Image::GetUPC(uint8_t& attr, char* upc)
{
attr = 0;
strcpy(upc, this->mcn.c_str());
@@ -174,7 +178,7 @@ CDROM_Interface_Image::GetAudioTracks(int& stTrack, int& end, TMSF& leadOut)
bool
CDROM_Interface_Image::GetAudioTrackInfo(int track, int& track_number, TMSF& start, unsigned char& attr)
CDROM_Interface_Image::GetAudioTrackInfo(int track, int& track_number, TMSF& start, uint8_t& attr)
{
if (track < 1 || track > (int)tracks.size()) return false;
@@ -187,23 +191,19 @@ CDROM_Interface_Image::GetAudioTrackInfo(int track, int& track_number, TMSF& sta
bool
CDROM_Interface_Image::GetAudioSub(int sector, unsigned char& attr, unsigned char& track, unsigned char& index, TMSF& relPos, TMSF& absPos)
CDROM_Interface_Image::GetAudioSub(int sector, uint8_t& attr, uint8_t& track, uint8_t& index, TMSF& relPos, TMSF& absPos)
{
int cur_track = GetTrack(sector);
if (cur_track < 1) return false;
track = (unsigned char)cur_track;
track = (uint8_t)cur_track;
attr = tracks[track - 1].attr;
index = 1;
#if 1
FRAMES_TO_MSF(sector + 150, &absPos.min, &absPos.sec, &absPos.fr);
#else
FRAMES_TO_MSF(sector - tracks[track - 1].start + 150, &relPos.min, &relPos.sec, &relPos.fr);
#endif
/* Yes, the absolute position should be adjusted by 150, but not the relative position. */
/* Absolute position should be adjusted by 150, not the relative ones. */
FRAMES_TO_MSF(sector - tracks[track - 1].start, &relPos.min, &relPos.sec, &relPos.fr);
return true;
@@ -222,16 +222,17 @@ CDROM_Interface_Image::GetMediaTrayStatus(bool& mediaPresent, bool& mediaChanged
bool
CDROM_Interface_Image::ReadSectors(PhysPt buffer, bool raw, unsigned long sector, unsigned long num)
CDROM_Interface_Image::ReadSectors(PhysPt buffer, bool raw, uint32_t sector, uint32_t num)
{
int sectorSize = raw ? RAW_SECTOR_SIZE : COOKED_SECTOR_SIZE;
Bitu buflen = num * sectorSize;
Bit8u* buf = new Bit8u[buflen];
bool success = true; //Gobliiins reads 0 sectors
for (unsigned long i = 0; i < num; i++) {
uint8_t buflen = num * sectorSize;
uint8_t* buf = new uint8_t[buflen];
bool success = true; /* reading 0 sectors is OK */
uint32_t i;
for (i = 0; i < num; i++) {
success = ReadSector(&buf[i * sectorSize], raw, sector + i);
if (!success) break;
if (! success) break;
}
memcpy((void*)buffer, buf, buflen);
@@ -259,7 +260,8 @@ CDROM_Interface_Image::GetTrack(unsigned int sector)
while (i != end) {
Track &curr = *i;
Track &next = *(i + 1);
if (curr.start <= sector && sector < next.start) return curr.number;
if (curr.start <= sector && sector < next.start)
return curr.number;
i++;
}
@@ -268,7 +270,7 @@ CDROM_Interface_Image::GetTrack(unsigned int sector)
bool
CDROM_Interface_Image::ReadSector(Bit8u *buffer, bool raw, unsigned long sector)
CDROM_Interface_Image::ReadSector(uint8_t *buffer, bool raw, uint32_t sector)
{
size_t length;
@@ -290,7 +292,7 @@ CDROM_Interface_Image::ReadSector(Bit8u *buffer, bool raw, unsigned long sector)
bool
CDROM_Interface_Image::ReadSectorSub(Bit8u *buffer, unsigned long sector)
CDROM_Interface_Image::ReadSectorSub(uint8_t *buffer, uint32_t sector)
{
int track = GetTrack(sector) - 1;
if (track < 0) return false;
@@ -304,7 +306,7 @@ CDROM_Interface_Image::ReadSectorSub(Bit8u *buffer, unsigned long sector)
int
CDROM_Interface_Image::GetSectorSize(unsigned long sector)
CDROM_Interface_Image::GetSectorSize(uint32_t sector)
{
int track = GetTrack(sector) - 1;
if (track < 0) return 0;
@@ -314,23 +316,24 @@ CDROM_Interface_Image::GetSectorSize(unsigned long sector)
bool
CDROM_Interface_Image::IsMode2(unsigned long sector)
CDROM_Interface_Image::IsMode2(uint32_t sector)
{
int track = GetTrack(sector) - 1;
if (track < 0) return false;
if (tracks[track].mode2) {
if (tracks[track].mode2)
return true;
} else {
return false;
}
return false;
}
int
CDROM_Interface_Image::GetMode2Form(unsigned long sector)
CDROM_Interface_Image::GetMode2Form(uint32_t sector)
{
int track = GetTrack(sector) - 1;
if (track < 0) return false;
return tracks[track].form;
@@ -338,7 +341,27 @@ CDROM_Interface_Image::GetMode2Form(unsigned long sector)
bool
CDROM_Interface_Image::LoadIsoFile(char* filename)
CDROM_Interface_Image::CanReadPVD(TrackFile *file, uint64_t sectorSize, bool mode2)
{
uint8_t pvd[COOKED_SECTOR_SIZE];
uint64_t seek = 16 * sectorSize; // first vd is located at sector 16
if (sectorSize == RAW_SECTOR_SIZE && !mode2) seek += 16;
if (mode2) seek += 24;
file->read(pvd, seek, COOKED_SECTOR_SIZE);
#if 0
pvd[0] = descriptor type, pvd[1..5] = standard identifier, pvd[6] = iso version (+8 for High Sierra)
#endif
return ((pvd[0] == 1 && !strncmp((char*)(&pvd[1]), "CD001", 5) && pvd[6] == 1) ||
(pvd[8] == 1 && !strncmp((char*)(&pvd[9]), "CDROM", 5) && pvd[14] == 1));
}
bool
CDROM_Interface_Image::IsoLoadFile(const wchar_t *filename)
{
tracks.clear();
@@ -395,88 +418,178 @@ CDROM_Interface_Image::LoadIsoFile(char* filename)
bool
CDROM_Interface_Image::CanReadPVD(TrackFile *file, uint64_t sectorSize, bool mode2)
CDROM_Interface_Image::CueGetBuffer(char *str, char **line, bool up)
{
Bit8u pvd[COOKED_SECTOR_SIZE];
uint64_t seek = 16 * sectorSize; // first vd is located at sector 16
char *s = *line;
char *p = str;
int quote = 0;
int done = 0;
int space = 1;
if (sectorSize == RAW_SECTOR_SIZE && !mode2) seek += 16;
if (mode2) seek += 24;
/* Copy to local buffer until we have end of string or whitespace. */
while (! done) {
switch(*s) {
case '\0':
if (quote) {
/* Ouch, unterminated string.. */
return false;
}
done = 1;
break;
file->read(pvd, seek, COOKED_SECTOR_SIZE);
case '\"':
quote ^= 1;
break;
#if 0
pvd[0] = descriptor type, pvd[1..5] = standard identifier, pvd[6] = iso version (+8 for High Sierra)
#endif
case ' ':
case '\t':
if (space)
break;
return ((pvd[0] == 1 && !strncmp((char*)(&pvd[1]), "CD001", 5) && pvd[6] == 1) ||
(pvd[8] == 1 && !strncmp((char*)(&pvd[9]), "CDROM", 5) && pvd[14] == 1));
}
if (! quote) {
done = 1;
break;
}
/*FALLTHROUGH*/
default:
if (up && islower((int) *s))
*p++ = toupper((int) *s);
else
*p++ = *s;
space = 0;
break;
}
#ifdef _WIN32
static string
dirname(char * file)
{
char *sep = strrchr(file, '\\');
if (sep == NULL)
sep = strrchr(file, '/');
if (sep == NULL)
return "";
else {
int len = (int)(sep - file);
char tmp[MAX_FILENAME_LENGTH];
safe_strncpy(tmp, file, len+1);
return tmp;
if (! done)
s++;
}
*p = '\0';
*line = s;
return true;
}
/* Get a filename string from the input line. */
bool
CDROM_Interface_Image::CueGetString(string &dest, char **line)
{
char temp[1024];
bool success;
success = CueGetBuffer(temp, line, false);
if (success)
dest = temp;
return success;
}
#endif
bool
CDROM_Interface_Image::LoadCueSheet(char *cuefile)
CDROM_Interface_Image::CueGetKeyword(string &dest, char **line)
{
char temp[1024];
bool success;
success = CueGetBuffer(temp, line, true);
if (success)
dest = temp;
return success;
}
/* Get a string from the input line, handling quotes properly. */
uint64_t
CDROM_Interface_Image::CueGetNumber(char **line)
{
char temp[128];
uint64_t num;
if (! CueGetBuffer(temp, line, false))
return 0;
if (sscanf(temp, "%" PRIu64, &num) != 1)
return 0;
return num;
}
bool
CDROM_Interface_Image::CueGetFrame(uint64_t &frames, char **line)
{
char temp[128];
int min, sec, fr;
bool success;
success = CueGetBuffer(temp, line, false);
if (! success) return false;
success = sscanf(temp, "%d:%d:%d", &min, &sec, &fr) == 3;
if (! success) return false;
frames = MSF_TO_FRAMES(min, sec, fr);
return true;
}
bool
CDROM_Interface_Image::CueLoadSheet(const wchar_t *cuefile)
{
Track track = {0, 0, 0, 0, 0, 0, 0, 0, false, NULL};
tracks.clear();
wchar_t pathname[MAX_FILENAME_LENGTH];
uint64_t shift = 0;
uint64_t currPregap = 0;
uint64_t totalPregap = 0;
uint64_t prestart = 0;
bool success;
bool canAddTrack = false;
char tmp[MAX_FILENAME_LENGTH]; // dirname can change its argument
bool success;
FILE *fp;
safe_strncpy(tmp, cuefile, MAX_FILENAME_LENGTH);
string pathname(dirname(tmp));
ifstream in;
in.open(cuefile, ios::in);
if (in.fail()) return false;
tracks.clear();
while (!in.eof()) {
// get next line
/* Get a copy of the filename into pathname, we need it later. */
plat_get_dirname(pathname, cuefile);
/* Open the file. */
fp = plat_fopen(cuefile, L"r");
if (fp == NULL)
return false;
success = false;
for (;;) {
char buf[MAX_LINE_LENGTH];
in.getline(buf, MAX_LINE_LENGTH);
if (in.fail() && !in.eof()) return false; // probably a binary file
istringstream line(buf);
char *line = buf;
/* Read a line from the cuesheet file. */
if (fgets(buf, sizeof(buf), fp) == NULL || ferror(fp) || feof(fp))
break;
buf[strlen(buf) - 1] = '\0'; /* nuke trailing newline */
string command;
GetCueKeyword(command, line);
success = CueGetKeyword(command, &line);
if (command == "TRACK") {
if (canAddTrack) success = AddTrack(track, shift, prestart, totalPregap, currPregap);
else success = true;
if (canAddTrack)
success = AddTrack(track, shift, prestart, totalPregap, currPregap);
else
success = true;
track.start = 0;
track.skip = 0;
currPregap = 0;
prestart = 0;
line >> track.number;
track.number = CueGetNumber(&line);
track.track_number = track.number;
string type;
GetCueKeyword(type, line);
success = CueGetKeyword(type, &line);
if (! success) break;
track.form = 0;
@@ -523,52 +636,87 @@ CDROM_Interface_Image::LoadCueSheet(char *cuefile)
track.sectorSize = RAW_SECTOR_SIZE;
track.attr = DATA_TRACK;
track.mode2 = true;
} else success = false;
} else
success = false;
canAddTrack = true;
} else if (command == "INDEX") {
uint64_t index;
line >> index;
uint64_t frame;
success = GetCueFrame(frame, line);
uint64_t frame, index;
index = CueGetNumber(&line);
success = CueGetFrame(frame, &line);
if (index == 1) track.start = frame;
else if (index == 0) prestart = frame;
// ignore other indices
switch(index) {
case 0:
prestart = frame;
break;
case 1:
track.start = frame;
break;
default:
/* ignore other indices */
break;
}
} else if (command == "FILE") {
if (canAddTrack) success = AddTrack(track, shift, prestart, totalPregap, currPregap);
else success = true;
if (canAddTrack)
success = AddTrack(track, shift, prestart, totalPregap, currPregap);
else
success = true;
canAddTrack = false;
string filename;
GetCueString(filename, line);
GetRealFileName(filename, pathname);
char ansi[MAX_FILENAME_LENGTH];
wchar_t filename[MAX_FILENAME_LENGTH];
string type;
GetCueKeyword(type, line);
success = CueGetBuffer(ansi, &line, false);
if (! success) break;
success = CueGetKeyword(type, &line);
if (! success) break;
track.file = NULL;
bool error = true;
if (type == "BINARY") {
track.file = new BinaryFile(filename.c_str(), error);
wchar_t temp[MAX_FILENAME_LENGTH];
mbstowcs(temp, ansi, sizeof_w(temp));
plat_append_filename(filename, pathname, temp);
track.file = new BinaryFile(filename, error);
}
if (error) {
ERRLOG("CUE: cannot open fille '%ls' in cue sheet!\n",
filename);
delete track.file;
track.file = NULL;
success = false;
}
} else if (command == "PREGAP") success = GetCueFrame(currPregap, line);
else if (command == "CATALOG") success = GetCueString(mcn, line);
} else if (command == "PREGAP")
success = CueGetFrame(currPregap, &line);
else if (command == "CATALOG") {
success = CueGetString(mcn, &line);
// ignored commands
else if (command == "CDTEXTFILE" || command == "FLAGS" || command == "ISRC"
} else if (command == "CDTEXTFILE" || command == "FLAGS" || command == "ISRC"
|| command == "PERFORMER" || command == "POSTGAP" || command == "REM"
|| command == "SONGWRITER" || command == "TITLE" || command == "") success = true;
// failure
else success = false;
else {
ERRLOG("CUE: unsupported command '%s' in cue sheet!\n",
command.c_str());
success = false;
}
if (!success) return false;
if (! success)
break;
}
fclose(fp);
if (! success)
return false;
// add last track
if (!AddTrack(track, shift, prestart, totalPregap, currPregap)) return false;
if (! AddTrack(track, shift, prestart, totalPregap, currPregap))
return false;
// add leadout track
track.number++;
@@ -578,7 +726,8 @@ CDROM_Interface_Image::LoadCueSheet(char *cuefile)
track.start = 0;
track.length = 0;
track.file = NULL;
if(!AddTrack(track, shift, 0, totalPregap, 0)) return false;
if (! AddTrack(track, shift, 0, totalPregap, 0))
return false;
return true;
}
@@ -658,91 +807,6 @@ CDROM_Interface_Image::HasAudioTracks(void)
}
bool
CDROM_Interface_Image::GetRealFileName(string &filename, string &pathname)
{
// check if file exists
struct stat test;
if (stat(filename.c_str(), &test) == 0) return true;
// check if file with path relative to cue file exists
string tmpstr(pathname + "/" + filename);
if (stat(tmpstr.c_str(), &test) == 0) {
filename = tmpstr;
return true;
}
#if defined (_WIN32) || defined(OS2)
//Nothing
#else
//Consider the possibility that the filename has a windows directory seperator (inside the CUE file)
//which is common for some commercial rereleases of DOS games using DOSBox
string copy = filename;
size_t l = copy.size();
for (size_t i = 0; i < l;i++) {
if(copy[i] == '\\') copy[i] = '/';
}
if (stat(copy.c_str(), &test) == 0) {
filename = copy;
return true;
}
tmpstr = pathname + "/" + copy;
if (stat(tmpstr.c_str(), &test) == 0) {
filename = tmpstr;
return true;
}
#endif
return false;
}
bool
CDROM_Interface_Image::GetCueKeyword(string &keyword, istream &in)
{
in >> keyword;
for (Bitu i = 0; i < keyword.size(); i++) keyword[i] = toupper(keyword[i]);
return true;
}
bool
CDROM_Interface_Image::GetCueFrame(uint64_t &frames, istream &in)
{
string msf;
in >> msf;
int min, sec, fr;
bool success = sscanf(msf.c_str(), "%d:%d:%d", &min, &sec, &fr) == 3;
frames = MSF_TO_FRAMES(min, sec, fr);
return success;
}
bool
CDROM_Interface_Image::GetCueString(string &str, istream &in)
{
int pos = (int)in.tellg();
in >> str;
if (str[0] == '\"') {
if (str[str.size() - 1] == '\"') {
str.assign(str, 1, str.size() - 2);
} else {
in.seekg(pos, ios::beg);
char buffer[MAX_FILENAME_LENGTH];
in.getline(buffer, MAX_FILENAME_LENGTH, '\"'); // skip
in.getline(buffer, MAX_FILENAME_LENGTH, '\"');
str = buffer;
}
}
return true;
}
void
CDROM_Interface_Image::ClearTracks(void)
{

View File

@@ -8,13 +8,13 @@
*
* Definitions for the CD-ROM image file handling module.
*
* Version: @(#)cdrom_dosbox.h 1.0.2 2018/03/09
* Version: @(#)cdrom_dosbox.h 1.0.3 2019/03/05
*
* Authors: Fred N. van Kempen, <decwiz@yahoo.com>
* Miran Grca, <mgrca8@gmail.com>
* The DOSBox Team, <unknown>
*
* Copyright 2017,2018 Fred N. van Kempen.
* Copyright 2017-2019 Fred N. van Kempen.
* Copyright 2016-2018 Miran Grca.
* Copyright 2002-2015 The DOSBox Team.
*
@@ -36,12 +36,10 @@
* Boston, MA 02111-1307
* USA.
*/
#ifndef CDROM_INTERFACE
# define CDROM_INTERFACE
/* Modified for use with PCem by bit */
#ifndef __CDROM_INTERFACE__
#define __CDROM_INTERFACE__
#include <stdint.h>
#include <string.h>
#include <string>
#include <iostream>
@@ -49,18 +47,17 @@
#include <fstream>
#include <sstream>
#include <stdint.h>
typedef signed int Bits;
typedef unsigned int Bitu;
typedef int8_t Bit8s;
typedef uint8_t Bit8u;
typedef int16_t Bit16s;
typedef uint16_t Bit16u;
typedef int32_t Bit32s;
typedef uint32_t Bit32u;
//typedef signed int Bits;
//typedef unsigned int Bitu;
//typedef int8_t Bit8s;
//typedef int16_t Bit16s;
//typedef uint16_t Bit16u;
//typedef int32_t Bit32s;
//typedef uint32_t Bit32u;
typedef size_t PhysPt;
#define RAW_SECTOR_SIZE 2352
#define COOKED_SECTOR_SIZE 2048
@@ -80,117 +77,122 @@ typedef size_t PhysPt;
typedef struct SMSF {
unsigned char min;
unsigned char sec;
unsigned char fr;
uint8_t min;
uint8_t sec;
uint8_t fr;
} TMSF;
typedef struct SCtrl {
Bit8u out[4]; // output channel
Bit8u vol[4]; // channel volume
uint8_t out[4]; // output channel
uint8_t vol[4]; // channel volume
} TCtrl;
extern int CDROM_GetMountType(char* path, int force);
class CDROM_Interface
{
class CDROM_Interface {
public:
// CDROM_Interface (void);
virtual ~CDROM_Interface (void) {};
// CDROM_Interface(void);
virtual bool SetDevice (char* path, int forceCD) = 0;
virtual ~CDROM_Interface(void) {};
virtual bool GetUPC (unsigned char& attr, char* upc) = 0;
virtual bool SetDevice(const wchar_t *path, int forceCD) = 0;
virtual bool GetAudioTracks (int& stTrack, int& end, TMSF& leadOut) = 0;
virtual bool GetAudioTrackInfo (int track, int& number, TMSF& start, unsigned char& attr) = 0;
virtual bool GetAudioSub (int sector, unsigned char& attr, unsigned char& track, unsigned char& index, TMSF& relPos, TMSF& absPos) = 0;
virtual bool GetMediaTrayStatus (bool& mediaPresent, bool& mediaChanged, bool& trayOpen) = 0;
virtual bool GetUPC(uint8_t& attr, char* upc) = 0;
virtual bool ReadSectors (PhysPt buffer, bool raw, unsigned long sector, unsigned long num) = 0;
virtual bool GetAudioTracks(int& stTrack, int& end, TMSF& leadOut) = 0;
virtual bool GetAudioTrackInfo(int track, int& number, TMSF& start, uint8_t& attr) = 0;
virtual bool GetAudioSub(int sector, uint8_t& attr, uint8_t& track, uint8_t& index, TMSF& relPos, TMSF& absPos) = 0;
virtual bool GetMediaTrayStatus(bool& mediaPresent, bool& mediaChanged, bool& trayOpen) = 0;
virtual bool LoadUnloadMedia (bool unload) = 0;
virtual bool ReadSectors(PhysPt buffer, bool raw, uint32_t sector, uint32_t num) = 0;
virtual void InitNewMedia (void) {};
virtual bool LoadUnloadMedia(bool unload) = 0;
virtual void InitNewMedia(void) {};
};
class CDROM_Interface_Image : public CDROM_Interface
{
class CDROM_Interface_Image : public CDROM_Interface {
private:
class TrackFile {
class TrackFile {
public:
virtual bool read(Bit8u *buffer, uint64_t seek, size_t count) = 0;
virtual bool read(uint8_t *buffer, uint64_t seek, size_t count) = 0;
virtual uint64_t getLength() = 0;
virtual ~TrackFile() { };
};
};
class BinaryFile : public TrackFile {
class BinaryFile : public TrackFile {
public:
BinaryFile(const char *filename, bool &error);
BinaryFile(const wchar_t *filename, bool &error);
~BinaryFile();
bool read(Bit8u *buffer, uint64_t seek, size_t count);
bool read(uint8_t *buffer, uint64_t seek, size_t count);
uint64_t getLength();
private:
BinaryFile();
char fn[260];
wchar_t fn[260];
FILE *file;
};
};
struct Track {
int number;
int track_number;
int attr;
int form;
uint64_t start;
uint64_t length;
uint64_t skip;
int sectorSize;
bool mode2;
TrackFile *file;
};
struct Track {
int number;
int track_number;
int attr;
int form;
uint64_t start;
uint64_t length;
uint64_t skip;
int sectorSize;
bool mode2;
TrackFile *file;
};
public:
CDROM_Interface_Image ();
virtual ~CDROM_Interface_Image (void);
void InitNewMedia (void);
bool SetDevice (char* path, int forceCD);
bool GetUPC (unsigned char& attr, char* upc);
bool GetAudioTracks (int& stTrack, int& end, TMSF& leadOut);
bool GetAudioTrackInfo (int track, int& number, TMSF& start, unsigned char& attr);
bool GetAudioSub (int sector, unsigned char& attr, unsigned char& track, unsigned char& index, TMSF& relPos, TMSF& absPos);
bool GetMediaTrayStatus (bool& mediaPresent, bool& mediaChanged, bool& trayOpen);
bool ReadSectors (PhysPt buffer, bool raw, unsigned long sector, unsigned long num);
bool LoadUnloadMedia (bool unload);
bool ReadSector (Bit8u *buffer, bool raw, unsigned long sector);
bool ReadSectorSub (Bit8u *buffer, unsigned long sector);
int GetSectorSize (unsigned long sector);
bool IsMode2 (unsigned long sector);
int GetMode2Form (unsigned long sector);
bool HasDataTrack (void);
bool HasAudioTracks (void);
CDROM_Interface_Image();
virtual ~CDROM_Interface_Image(void);
void InitNewMedia(void);
bool SetDevice(const wchar_t* path, int forceCD);
bool GetUPC(uint8_t& attr, char* upc);
bool GetAudioTracks(int& stTrack, int& end, TMSF& leadOut);
bool GetAudioTrackInfo(int track, int& number, TMSF& start, uint8_t& attr);
bool GetAudioSub(int sector, uint8_t& attr, uint8_t& track, uint8_t& index, TMSF& relPos, TMSF& absPos);
bool GetMediaTrayStatus(bool& mediaPresent, bool& mediaChanged, bool& trayOpen);
bool ReadSectors(PhysPt buffer, bool raw, uint32_t sector, uint32_t num);
bool LoadUnloadMedia(bool unload);
bool ReadSector(uint8_t *buffer, bool raw, uint32_t sector);
bool ReadSectorSub(uint8_t *buffer, uint32_t sector);
int GetSectorSize(uint32_t sector);
bool IsMode2(uint32_t sector);
int GetMode2Form(uint32_t sector);
bool HasDataTrack(void);
bool HasAudioTracks(void);
int GetTrack (unsigned int sector);
int GetTrack(unsigned int sector);
private:
// player
static void CDAudioCallBack(Bitu len);
// player
static void CDAudioCallBack(unsigned int len);
void ClearTracks();
bool LoadIsoFile(char *filename);
bool CanReadPVD(TrackFile *file, uint64_t sectorSize, bool mode2);
// cue sheet processing
bool LoadCueSheet(char *cuefile);
bool GetRealFileName(std::string& filename, std::string& pathname);
bool GetCueKeyword(std::string &keyword, std::istream &in);
bool GetCueFrame(uint64_t &frames, std::istream &in);
bool GetCueString(std::string &str, std::istream &in);
bool AddTrack(Track &curr, uint64_t &shift, uint64_t prestart, uint64_t &totalPregap, uint64_t currPregap);
void ClearTracks();
bool IsoLoadFile(const wchar_t *filename);
bool CanReadPVD(TrackFile *file, uint64_t sectorSize, bool mode2);
std::vector<Track> tracks;
// cue sheet processing
bool CueGetBuffer(char *str, char **line, bool up);
bool CueGetString(std::string &str, char **line);
bool CueGetKeyword(std::string &keyword, char **line);
uint64_t CueGetNumber(char **line);
bool CueGetFrame(uint64_t &frames, char **line);
bool CueLoadSheet(const wchar_t *cuefile);
bool AddTrack(Track &curr, uint64_t &shift, uint64_t prestart, uint64_t &totalPregap, uint64_t currPregap);
std::vector<Track> tracks;
typedef std::vector<Track>::iterator track_it;
std::string mcn;
std::string mcn;
};
void cdrom_image_log(const char *format, ...);
extern int CDROM_GetMountType(char* path, int force);
extern void cdrom_image_log(const char *format, ...);
#endif /* __CDROM_INTERFACE__ */

View File

@@ -8,13 +8,13 @@
*
* CD-ROM image support.
*
* Version: @(#)cdrom_image.cpp 1.0.19 2018/10/21
* Version: @(#)cdrom_image.cpp 1.0.20 2019/03/05
*
* Authors: Fred N. van Kempen, <decwiz@yahoo.com>
* Miran Grca, <mgrca8@gmail.com>
* RichardG,
*
* Copyright 2017,2018 Fred N. van Kempen.
* Copyright 2017-2019 Fred N. van Kempen.
* Copyright 2016-2018 Miran Grca.
*
* This program is free software; you can redistribute it and/or modify
@@ -1082,7 +1082,6 @@ static const cdrom_ops_t cdrom_image_ops = {
int
cdrom_image_open(cdrom_t *dev, const wchar_t *fn)
{
char temp[1024];
CDROM_Interface_Image *img;
wcscpy(dev->image_path, fn);
@@ -1096,10 +1095,8 @@ cdrom_image_open(cdrom_t *dev, const wchar_t *fn)
img = new CDROM_Interface_Image();
dev->local = img;
/* Convert filename and open the image. */
memset(temp, '\0', sizeof(temp));
wcstombs(temp, fn, sizeof(temp));
if (! img->SetDevice(temp, false)) {
/* Open the image. */
if (! img->SetDevice(fn, false)) {
image_close(dev);
dev->ops = NULL;
dev->host_drive = 0;

View File

@@ -8,7 +8,7 @@
*
* Implementation of Intel 8259 interrupt controller.
*
* Version: @(#)pic.c 1.0.5 2019/02/28
* Version: @(#)pic.c 1.0.6 2019/03/04
*
* Authors: Fred N. van Kempen, <decwiz@yahoo.com>
* Miran Grca, <mgrca8@gmail.com>
@@ -133,7 +133,7 @@ pic_write(uint16_t addr, uint8_t val, void *priv)
case 1: /* ICW2 */
dev->vector = val & 0xf8;
DEBUG("PIC: vector now: %02X\n", dev->vector);
DBGLOG(1, "PIC: vector now: %02X\n", dev->vector);
if (dev->icw1 & 0x02)
dev->icw = 3;
else
@@ -142,7 +142,7 @@ pic_write(uint16_t addr, uint8_t val, void *priv)
case 2: /* ICW3 */
dev->icw3 = val;
DEBUG("PIC: ICW3 now %02x\n", val);
DBGLOG(1, "PIC: ICW3 now %02x\n", val);
if (dev->icw1 & 0x01)
dev->icw = 3;
else
@@ -228,12 +228,12 @@ pic_read(uint16_t addr, void *priv)
PIC *dev = (PIC *)priv;
if (addr & 1) {
DEBUG("PIC1: read mask %02X\n", dev->mask);
DBGLOG(1, "PIC1: read mask %02X\n", dev->mask);
return(pic.mask);
}
if (dev->read) {
DEBUG("PIC1: read ins %02X\n", dev->ins);
DBGLOG(1, "PIC1: read ins %02X\n", dev->ins);
if (AT)
return(dev->ins | (pic2.ins ? 4 : 0));
else
@@ -288,7 +288,7 @@ pic2_write(uint16_t addr, uint8_t val, void *priv)
case 1: /* ICW2 */
dev->vector = val & 0xf8;
DEBUG("PIC2: vector now: %02X\n", dev->vector);
DBGLOG(1, "PIC2: vector now: %02X\n", dev->vector);
if (dev->icw1 & 0x02)
dev->icw = 3;
else
@@ -297,7 +297,7 @@ pic2_write(uint16_t addr, uint8_t val, void *priv)
case 2: /* ICW3 */
dev->icw3 = val;
DEBUG("PIC2: ICW3 now %02X\n", val);
DBGLOG(1, "PIC2: ICW3 now %02X\n", val);
if (dev->icw1 & 1)
dev->icw = 3;
else

View File

@@ -44,7 +44,7 @@
*
* This is expected to be done shortly.
*
* Version: @(#)vid_pgc.c 1.0.2 2019/03/03
* Version: @(#)vid_pgc.c 1.0.2 2019/03/04
*
* Authors: Fred N. van Kempen, <decwiz@yahoo.com>
* John Elliott, <jce@seasip.info>
@@ -137,7 +137,7 @@ static int
output_byte(pgc_t *dev, uint8_t val)
{
/* If output buffer full, wait for it to empty. */
while (dev->mapram[0x302] == (uint8_t)(dev->mapram[0x303] - 1)) {
while (!dev->stopped && dev->mapram[0x302] == (uint8_t)(dev->mapram[0x303] - 1)) {
DEBUG("PGC: output buffer state: %02x %02x Sleeping\n",
dev->mapram[0x302], dev->mapram[0x303]);
dev->waiting_output_fifo = 1;
@@ -178,7 +178,7 @@ static int
error_byte(pgc_t *dev, uint8_t val)
{
/* If error buffer full, wait for it to empty. */
while (dev->mapram[0x304] == dev->mapram[0x305] - 1) {
while (!dev->stopped && dev->mapram[0x304] == dev->mapram[0x305] - 1) {
dev->waiting_error_fifo = 1;
pgc_sleep(dev);
}
@@ -219,7 +219,7 @@ static int
input_byte(pgc_t *dev, uint8_t *result)
{
/* If input buffer empty, wait for it to fill. */
while (dev->mapram[0x300] == dev->mapram[0x301]) {
while (!dev->stopped && dev->mapram[0x300] == dev->mapram[0x301]) {
dev->waiting_input_fifo = 1;
pgc_sleep(dev);
}
@@ -1419,9 +1419,16 @@ pgc_thread(void *priv)
DEBUG("PGC: thread begins\n");
while (1) {
for (;;) {
if (! parse_command(dev, &cmd)) {
/* PGC has been reset. */
/* Are we shutting down? */
if (dev->stopped) {
INFO("PGC: thread stopping..\n");
dev->stopped = 0;
break;
}
/* Nope, just a reset. */
continue;
}
@@ -1434,6 +1441,8 @@ pgc_thread(void *priv)
} else
pgc_error(dev, PGC_ERROR_OPCODE);
}
DEBUG("PGC: thread stopped\n");
}
@@ -1616,10 +1625,14 @@ pgc_wake(pgc_t *dev)
void
pgc_sleep(pgc_t *dev)
{
DEBUG("PGC: sleeping on %i %i %i 0x%02x 0x%02x\n",
DEBUG("PGC: sleeping on %i %i %i %i 0x%02x 0x%02x\n",
dev->stopped,
dev->waiting_input_fifo, dev->waiting_output_fifo,
dev->waiting_error_fifo, dev->mapram[0x300], dev->mapram[0x301]);
/* Avoid entering waiting state. */
if (dev->stopped) return;
/* Race condition: If host wrote to the PGC during the that
* won't be noticed */
if (dev->waiting_input_fifo &&
@@ -2075,26 +2088,27 @@ pgc_out(uint16_t addr, uint8_t val, void *priv)
DEBUG("PGC: out(%04x, %02x)\n", addr, val);
switch(addr) {
case 0x03d0:
case 0x03d0: /* CRTC Index register */
case 0x03d2:
case 0x03d4:
case 0x03d6:
dev->mapram[0x03d0] = val;
break;
case 0x03d1:
case 0x03d1: /* CRTC Data register */
case 0x03d3:
case 0x03d5:
case 0x03d7:
if (dev->mapram[0x03d0] < 18)
/* We store the CRTC registers in RAM at offset 0x03e0. */
if (dev->mapram[0x03d0] <= 15)
dev->mapram[0x03e0 + dev->mapram[0x03d0]] = val;
break;
case 0x03d8:
case 0x03d8: /* CRTC Mode Control register */
dev->mapram[0x03d8] = val;
break;
case 0x03d9:
case 0x03d9: /* CRTC Color Select register */
dev->mapram[0x03d9] = val;
break;
}
@@ -2109,30 +2123,31 @@ pgc_in(uint16_t addr, void *priv)
uint8_t ret = 0xff;
switch(addr) {
case 0x03d0:
case 0x03d0: /* CRTC Index register */
case 0x03d2:
case 0x03d4:
case 0x03d6:
ret = dev->mapram[0x03d0];
break;
case 0x03d1:
case 0x03d1: /* CRTC Data register */
case 0x03d3:
case 0x03d5:
case 0x03d7:
if (dev->mapram[0x03d0] < 18)
/* We store the CRTC registers in RAM at offset 0x03e0. */
if (dev->mapram[0x03d0] <= 15)
ret = dev->mapram[0x03e0 + dev->mapram[0x03d0]];
break;
case 0x03d8:
case 0x03d8: /* CRTC Mode Control register */
ret = dev->mapram[0x03d8];
break;
case 0x03d9:
case 0x03d9: /* CRTC Color Select register */
ret = dev->mapram[0x03d9];
break;
case 0x03da:
case 0x03da: /* CRTC Status register */
ret = dev->mapram[0x03da];
break;
}
@@ -2205,7 +2220,7 @@ pgc_write(uint32_t addr, uint8_t val, void *priv)
}
}
if (addr >= 0xb8000 && addr < 0xbc000 && dev->cga_selected) {
if (addr >= 0xb8000 && addr < 0xc0000 && dev->cga_selected) {
addr &= 0x3fff;
dev->cga_vram[addr] = val;
}
@@ -2221,7 +2236,7 @@ pgc_read(uint32_t addr, void *priv)
if (addr >= 0xc6000 && addr < 0xc6800) {
addr &= 0x7ff;
ret = dev->mapram[addr];
} else if (addr >= 0xb8000 && addr < 0xbc000 && dev->cga_selected) {
} else if (addr >= 0xb8000 && addr < 0xc0000 && dev->cga_selected) {
addr &= 0x3fff;
ret = dev->cga_vram[addr];
}
@@ -2513,6 +2528,24 @@ pgc_close(void *priv)
{
pgc_t *dev = (pgc_t *)priv;
/*
* Close down the worker thread by setting a
* flag, and then simulating a reset so it
* stops reading data.
*/
INFO("PGC: telling thread to stop..\n");
dev->stopped = 1;
dev->mapram[0x3ff] = 1;
if (dev->waiting_input_fifo || dev->waiting_output_fifo) {
/* Do an immediate wake-up. */
wake_timer(priv);
}
/* Wait for thread to stop. */
INFO("PGC: waiting for thread to stop..\n");
while (dev->stopped);
INFO("PGC: thread stopped, closing up.\n");
if (dev->cga_vram)
free(dev->cga_vram);
if (dev->vram)
@@ -2590,7 +2623,7 @@ pgc_standalone_init(const device_t *info)
memset(dev, 0x00, sizeof(pgc_t));
dev->type = info->local;
/* Framebuffer and screen are both 640x480 */
/* Framebuffer and screen are both 640x480. */
pgc_init(dev, 640, 480, 640, 480, input_byte);
video_inform(VID_TYPE_CGA, info->vid_timing);

View File

@@ -8,7 +8,7 @@
*
* Definitions for the PGC driver.
*
* Version: @(#)vid_pgc.h 1.0.2 2019/03/03
* Version: @(#)vid_pgc.h 1.0.3 2019/03/04
*
* Authors: Fred N. van Kempen, <decwiz@yahoo.com>
* John Elliott, <jce@seasip.info>
@@ -71,7 +71,10 @@ typedef struct pgc_cmd {
} pgc_cmd_t;
typedef struct pgc {
int type; /* board type */
int8_t type; /* board type */
int8_t cga_enabled;
int8_t cga_selected;
volatile int8_t stopped;
mem_map_t mapping;
mem_map_t cga_mapping;
@@ -112,8 +115,6 @@ typedef struct pgc {
int waiting_input_fifo;
int waiting_output_fifo;
int waiting_error_fifo;
int cga_enabled;
int cga_selected;
int ascii_mode;
int result_count;

View File

@@ -156,7 +156,7 @@ const machine_t machines[] = {
#if defined(DEV_BRANCH) && defined(USE_SIS471)
{ "[486 ISA] AMI 486 (SiS471)", "ami_486_sis471", L"sis471/ami", 0, {{"Intel", cpus_i486}, {"AMD", cpus_Am486}, {"Cyrix", cpus_Cx486}, {"", NULL}, {"", NULL}}, 0, MACHINE_ISA | MACHINE_VLB | MACHINE_AT | MACHINE_HDC, 1, 128, 1, 128, m_at_sis471_ami_init, NULL, NULL },
#endif
{ "[486 ISA] AMI WinBIOS486 (ALi1429)", "ami_win486_ali1429", L"ali1429/ami_win", 0, {{"Intel", cpus_i486}, {"AMD", cpus_Am486}, {"Cyrix", cpus_Cx486}, {"", NULL}, {"", NULL}}, 0, MACHINE_ISA | MACHINE_VLB | MACHINE_AT | MACHINE_HDC, 1, 32, 1, 128, m_at_ali1429_init, NULL, NULL },
{ "[486 ISA] AMI WinBIOS486 (ALi1429)", "ami_win486_ali1429", L"ali1429/ami_win", 0, {{"Intel", cpus_i486}, {"AMD", cpus_Am486}, {"Cyrix", cpus_Cx486}, {"", NULL}, {"", NULL}}, 0, MACHINE_ISA | MACHINE_VLB | MACHINE_AT | MACHINE_HDC, 1, 64, 1, 128, m_at_ali1429_init, NULL, NULL },
{ "[486 ISA] Award 486 (Opti495)", "award_486_opti495", L"opti495/award", 0, {{"Intel", cpus_i486}, {"AMD", cpus_Am486}, {"Cyrix", cpus_Cx486}, {"", NULL}, {"", NULL}}, 0, MACHINE_ISA | MACHINE_VLB | MACHINE_AT | MACHINE_HDC, 1, 32, 1, 128, m_at_opti495_award_init, NULL, NULL },
{ "[486 ISA] MR 486 (Opti495)", "mr_486dx_opti495", L"opti495/mr", 0, {{"Intel", cpus_i386DX}, {"AMD", cpus_Am386DX}, {"Cyrix", cpus_486DLC}, {"", NULL}, {"", NULL}}, 0, MACHINE_ISA | MACHINE_VLB | MACHINE_AT | MACHINE_HDC, 1, 32, 1, 128, m_at_opti495_mr_init, NULL, NULL },

View File

@@ -8,11 +8,11 @@
*
* Define the various platform support functions.
*
* Version: @(#)plat.h 1.0.22 2018/11/24
* Version: @(#)plat.h 1.0.23 2019/03/05
*
* Author: Fred N. van Kempen, <decwiz@yahoo.com>
*
* Copyright 2017,2018 Fred N. van Kempen.
* Copyright 2017-2019 Fred N. van Kempen.
*
* Redistribution and use in source and binary forms, with
* or without modification, are permitted provided that the
@@ -52,7 +52,7 @@
#ifdef _WIN32
# ifndef _MSC_VER
# undef swprintf
# define swprintf __swprintf
# define swprintf __swprintf
# endif
# define wcsncasecmp _wcsnicmp
# define wcscasecmp _wcsicmp
@@ -62,12 +62,12 @@
#if defined(UNIX) && defined(FREEBSD)
/* FreeBSD has largefile by default. */
# define fopen64 fopen
//# define fopen64 fopen
# define fseeko64 fseeko
# define ftello64 ftello
# define off64_t off_t
#elif defined(_MSC_VER)
# define fopen64 fopen
//# define fopen64 fopen
# define fseeko64 _fseeki64
# define ftello64 _ftelli64
# define off64_t off_t
@@ -141,12 +141,14 @@ extern void plat_console(int on);
#endif
extern wchar_t *fix_emu_path(const wchar_t *str);
extern FILE *plat_fopen(const wchar_t *path, const wchar_t *mode);
extern FILE *plat_fopen64(const wchar_t *path, const wchar_t *mode);
extern void plat_remove(const wchar_t *path);
extern int plat_getcwd(wchar_t *bufp, int max);
extern int plat_chdir(const wchar_t *path);
extern void plat_tempfile(wchar_t *bufp, const wchar_t *prefix, const wchar_t *suffix);
extern void plat_get_exe_name(wchar_t *path, int size);
extern wchar_t *plat_get_basename(const wchar_t *path);
extern void plat_get_dirname(wchar_t *dest, const wchar_t *path);
extern wchar_t *plat_get_filename(const wchar_t *path);
extern wchar_t *plat_get_extension(const wchar_t *path);
extern void plat_append_filename(wchar_t *dest, const wchar_t *s1, const wchar_t *s2);

View File

@@ -8,7 +8,7 @@
*
* Platform main support module for Windows.
*
* Version: @(#)win.c 1.0.26 2019/02/12
* Version: @(#)win.c 1.0.27 2019/03/05
*
* Authors: Fred N. van Kempen, <decwiz@yahoo.com>
* Miran Grca, <mgrca8@gmail.com>
@@ -402,6 +402,7 @@ plat_chdir(const wchar_t *path)
}
/* Open a file, using Unicode pathname. */
FILE *
plat_fopen(const wchar_t *path, const wchar_t *mode)
{
@@ -409,6 +410,14 @@ plat_fopen(const wchar_t *path, const wchar_t *mode)
}
/* Open a file, using Unicode pathname, with 64bit pointers. */
FILE *
plat_fopen64(const wchar_t *path, const wchar_t *mode)
{
return(_wfopen(path, mode));
}
void
plat_remove(const wchar_t *path)
{
@@ -454,6 +463,30 @@ plat_get_basename(const wchar_t *path)
}
/* Return the 'directory' element of a pathname. */
void
plat_get_dirname(wchar_t *dest, const wchar_t *path)
{
int c = (int)wcslen(path);
wchar_t *ptr;
ptr = (wchar_t *)path;
while (c > 0) {
if (path[c] == L'/' || path[c] == L'\\') {
ptr = (wchar_t *)&path[c];
break;
}
c--;
}
/* Copy to destination. */
while (path < ptr)
*dest++ = *path++;
*dest = L'\0';
}
wchar_t *
plat_get_filename(const wchar_t *path)
{
@@ -491,6 +524,7 @@ void
plat_append_filename(wchar_t *dest, const wchar_t *s1, const wchar_t *s2)
{
wcscat(dest, s1);
plat_append_slash(dest);
wcscat(dest, s2);
}