aboutsummaryrefslogtreecommitdiff
path: root/SrcShared/Hardware/TRG/EmTRGDiskIO.cpp
blob: 9b4358244788e9144864e79be851431c08353059 (plain)
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
/* -*- mode: C++; tab-width: 4 -*- */
/* ===================================================================== *\
	Copyright (c) 2000-2001 Palm, Inc. or its subsidiaries.
	All rights reserved.

	This file is part of the Palm OS Emulator.

	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; either version 2 of the License, or
	(at your option) any later version.
\* ===================================================================== */

#include "EmCommon.h"
#include "EmTRGDiskIO.h"

/************************************************************************
 * This class handles the generic low level disk access.
 ************************************************************************/
#include <stdio.h>

#include "EmCommon.h"
#include "EmTRGDiskIO.h"

#define CFFILE_NAME   "trgdrv.dat"
#define SDFILE_NAME   "trgdrvsd.dat"

#define SECTOR_SIZE   512


EmTRGDiskIO::EmTRGDiskIO()
{
    m_driveNo = UNKNOWN_DRIVE;
}


EmTRGDiskIO::~EmTRGDiskIO()
{
}


char * EmTRGDiskIO::GetFilePath(int driveNo)
{
#if PLATFORM_WINDOWS
	static char tmp[MAX_PATH];

    if (driveNo == CF_DRIVE)
	    _snprintf(tmp, sizeof(tmp), "%s\\%s", getenv("WINDIR"), CFFILE_NAME);
    else
	    _snprintf(tmp, sizeof(tmp), "%s\\%s", getenv("WINDIR"), SDFILE_NAME);
	return(tmp);
#else
    if (driveNo == CF_DRIVE)
    	return CFFILE_NAME;
    else
    	return SDFILE_NAME;
#endif
}

int EmTRGDiskIO::Format(void)
{
    EmSector *buffer;
    uint32    num, lba;
    FILE      *fp;

   	fp = fopen(GetFilePath(m_driveNo), "wb");
    if (fp == NULL)
        return -1;

    buffer = new EmSector;
	num = m_currDisk.GetNumSectors(m_diskTypeID);
	for (lba=0; lba<num; lba++)
	{
		m_currDisk.GetSector(m_diskTypeID, lba, buffer);		                   

		// The most probable error condition is
		// attempting to write to a full drive ... it could also
		// be write-protected, or on a disconnected network drive.
		if (fwrite(buffer, SECTOR_SIZE, 1, fp) == 0)
        {
            delete buffer;
            fclose(fp);
			return -1;
        }
	}
    delete buffer;

	return 0;
}

int EmTRGDiskIO::Read(uint32 sectorNum, void *buffer)
{
    FILE *fp;

    if ((fp = fopen(GetFilePath(m_driveNo), "rb")) == NULL)
        return -1;

    if (fseek(fp, sectorNum * SECTOR_SIZE, SEEK_SET) == -1)
    {
        fclose(fp);
        return -1;
    }

    if (fread(buffer, SECTOR_SIZE, 1, fp) != 1)
    {
        fclose(fp);
        return -1;
    }
    fclose(fp);

    return 0;
}

int EmTRGDiskIO::Write(uint32 sectorNum, void *buffer)
{
    FILE *fp;

    if ((fp = fopen(GetFilePath(m_driveNo), "r+b")) == NULL)
        return -1;

    if (fseek(fp, sectorNum * SECTOR_SIZE, SEEK_SET) == -1)
    {
        fclose(fp);
        return -1;
    }

    if (fwrite(buffer, SECTOR_SIZE, 1, fp) != 1)
    {
        fclose(fp);
        return -1;
    }
    fclose(fp);

    return 0;
}


void EmTRGDiskIO::Initialize(EmDiskTypeID DiskTypeID, int driveNo)
{
    m_diskTypeID = DiskTypeID;
    m_driveNo = driveNo;
}

void EmTRGDiskIO::Dispose(void)
{
}

int EmTRGDiskIO::ReadSector(uint32 sectorNum, void *buffer)
{
    int retval;

    if ((retval = Read(sectorNum, buffer)) != 0)
    {
        Format();
        retval = Read(sectorNum, buffer);
    }

    return retval;
}



int EmTRGDiskIO::WriteSector(uint32 sectorNum, void *buffer)
{
    int retval;

    if ((retval = Write(sectorNum, buffer)) != 0)
    {
        Format();
        retval = Write(sectorNum, buffer);
    }

    return retval;
}