/[pearpc]/src/io/ide/idedevice.cc
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /src/io/ide/idedevice.cc

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1 - (show annotations)
Wed Sep 5 17:11:21 2007 UTC (16 years, 6 months ago) by dpavlin
File size: 3557 byte(s)
import upstream CVS
1 /*
2 * PearPC
3 * cd.h
4 *
5 * Copyright (C) 2003 Sebastian Biallas (sb@biallas.net)
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include <cstdio>
22 #include <cstdlib>
23 #include <cstring>
24
25 #include "idedevice.h"
26 #include "tools/snprintf.h"
27 #include "tools/except.h"
28
29 /*
30 *
31 */
32 class IDEDeviceFile: public File {
33 protected:
34 IDEDevice &id;
35 FileOfs tel;
36 public:
37
38 IDEDeviceFile(IDEDevice &aid) : id(aid)
39 {
40 tel = 0;
41 }
42
43 virtual FileOfs getSize() const
44 {
45 return id.getBlockSize() * id.getBlockCount();
46 }
47
48 virtual void seek(FileOfs offset)
49 {
50 if (!id.promSeek(offset)) throw IOException(EIO);
51 tel = offset;
52 }
53
54 virtual FileOfs tell() const
55 {
56 return tel;
57 }
58
59 virtual uint read(void *buf, uint size)
60 {
61 uint r = id.promRead((byte*)buf, size);
62 tel += r;
63 return r;
64 }
65
66 virtual String &getDesc(String &result) const
67 {
68 result.assignFormat("%y", &id);
69 return result;
70 }
71
72 };
73
74 /*
75 *
76 */
77 IDEDevice::IDEDevice(const char *name)
78 {
79 mAcquired = false;
80 mSectorFirst = 0;
81 mError = NULL;
82 sys_create_mutex(&mMutex);
83 mName = strdup(name);
84 }
85
86 IDEDevice::~IDEDevice()
87 {
88 if (mError) free(mError);
89 }
90
91 bool IDEDevice::acquire()
92 {
93 if (mAcquired) {
94 printf("attempt to reacquire IDEDevice\n");
95 exit(-1);
96 }
97 mAcquired = true;
98 mSectorFirst = 0; // acquire clears deblocking
99 sys_lock_mutex(mMutex);
100 return true;
101 }
102
103 bool IDEDevice::release()
104 {
105 if (mAcquired) {
106 sys_unlock_mutex(mMutex);
107 mAcquired = false;
108 return true;
109 } else {
110 return false;
111 }
112 }
113
114 void IDEDevice::setMode(int aMode, int aSectorSize)
115 {
116 mMode = aMode;
117 mSectorSize = aSectorSize;
118 }
119
120 char *IDEDevice::getError()
121 {
122 return mError;
123 }
124
125 void IDEDevice::setError(const char *error)
126 {
127 if (mError) free(mError);
128 mError = strdup(error);
129 }
130
131 int IDEDevice::read(byte *buf, int size)
132 {
133 byte *oldbuf = buf;
134 if (mSectorFirst && mSectorFirst < mSectorSize) {
135 int copy = MIN(mSectorSize-mSectorFirst, size);
136 memcpy(buf, mSector+mSectorFirst, copy);
137 size -= copy;
138 buf += copy;
139 mSectorFirst += copy;
140 }
141 if (size > 0) {
142 while (size >= mSectorSize) {
143 readBlock(buf);
144 size -= mSectorSize;
145 buf += mSectorSize;
146 }
147 if (size > 0) {
148 readBlock(mSector);
149 memcpy(buf, mSector, size);
150 mSectorFirst = size;
151 buf += size;
152 }
153 }
154 return buf-oldbuf;
155 }
156
157 int IDEDevice::write(byte *buf, int size)
158 {
159 byte *oldbuf = buf;
160 if (mSectorFirst && mSectorFirst < mSectorSize) {
161 int copy = MIN(mSectorSize-mSectorFirst, size);
162 memcpy(mSector+mSectorFirst, buf, copy);
163 size -= copy;
164 buf += copy;
165 mSectorFirst += copy;
166 if (mSectorFirst >= mSectorSize) {
167 writeBlock(mSector);
168 }
169 }
170 if (size > 0) {
171 while (size >= mSectorSize) {
172 writeBlock(buf);
173 size -= mSectorSize;
174 buf += mSectorSize;
175 }
176 if (size > 0) {
177 memcpy(mSector, buf, size);
178 mSectorFirst = size;
179 buf += size;
180 }
181 }
182 return buf-oldbuf;
183 }
184
185 File *IDEDevice::promGetRawFile()
186 {
187 return new IDEDeviceFile(*this);
188 }
189
190 int IDEDevice::toString(char *buf, int buflen) const
191 {
192 return ht_snprintf(buf, buflen, "%s", mName);
193 }

  ViewVC Help
Powered by ViewVC 1.1.26