-
Notifications
You must be signed in to change notification settings - Fork 1
/
DiskFile.cpp
201 lines (183 loc) · 6.05 KB
/
DiskFile.cpp
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/* ES40 emulator.
* Copyright (C) 2007-2008 by the ES40 Emulator Project
*
* WWW : http://sourceforge.net/projects/es40
* E-mail : [email protected]
*
* 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.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Although this is not required, the author would appreciate being notified of,
* and receiving any modifications you may make to the source code that might serve
* the general public.
*/
/**
* \file
* Contains code to use a file as a disk image.
*
* $Id: DiskFile.cpp,v 1.17 2008/03/02 09:42:52 iamcamiel Exp $
*
* X-1.17 Camiel Vanderhoeven 02-MAR-2008
* Natural way to specify large numeric values ("10M") in the config file.
*
* X-1.16 David Leonard 20-FEB-2008
* Show disk creation progress.
*
* X-1.15 Camiel Vanderhoeven 25-JAN-2008
* Create file if it doesn't exist and autocreate_size is specified.
*
* X-1.14 Camiel Vanderhoeven 13-JAN-2008
* Use determine_layout in stead of calc_cylinders.
*
* X-1.13 Brian Wheeler 09-JAN-2008
* Put filename in disk model number (without path).
*
* X-1.12 Camiel Vanderhoeven 09-JAN-2008
* Save disk state to state file.
*
* X-1.11 Camiel Vanderhoeven 06-JAN-2008
* Set default blocksize to 2048 for cd-rom devices.
*
* X-1.10 Camiel Vanderhoeven 06-JAN-2008
* Support changing the block size (required for SCSI, ATAPI).
*
* X-1.9 Camiel Vanderhoeven 04-JAN-2008
* 64-bit file I/O.
*
* X-1.8 Camiel Vanderhoeven 02-JAN-2008
* Cleanup.
*
* X-1.7 Camiel Vanderhoeven 28-DEC-2007
* Throw exceptions rather than just exiting when errors occur.
*
* X-1.6 Camiel Vanderhoeven 28-DEC-2007
* Keep the compiler happy.
*
* X-1.5 Camiel Vanderhoeven 20-DEC-2007
* Close files and free memory when the emulator shuts down.
*
* X-1.4 Camiel Vanderhoeven 18-DEC-2007
* Byte-sized transfers for SCSI controller.
*
* X-1.3 Brian wheeler 17-DEC-2007
* Changed last cylinder number.
*
* X-1.2 Brian Wheeler 16-DEC-2007
* Fixed case of StdAfx.h.
*
* X-1.1 Camiel Vanderhoeven 12-DEC-2007
* Initial version in CVS.
**/
#include "StdAfx.h"
#include "DiskFile.h"
CDiskFile::CDiskFile(CConfigurator * cfg, CSystem * sys, CDiskController * c, int idebus, int idedev) : CDisk(cfg,sys,c,idebus,idedev)
{
filename = myCfg->get_text_value("file");
if (!filename)
{
printf("%s: Disk has no file attached!\n",devid_string);
throw((int)1);
}
if (read_only)
handle = fopen(filename,"rb");
else
handle = fopen(filename,"rb+");
if (!handle)
{
printf("%s: Could not open file %s!\n",devid_string,filename);
int sz = myCfg->get_num_value("autocreate_size",0)/1024/1024;
if (!sz)
FAILURE("File does not exist and no autocreate_size set");
void * crt_buf;
handle = fopen(filename,"wb");
if (!handle)
FAILURE("File does not exist and could not be created");
crt_buf = calloc(1024,1024);
printf("%s: writing %d 1kB blocks: 0%%\b\b\b\b", devid_string, sz);
int lastpc = 0;
for (int a = 0; a<sz; a++) {
fwrite(crt_buf,1024,1024,handle);
int pc = a * 100 / sz;
if (pc != lastpc) {
printf("%3d\b\b\b", pc);
lastpc = pc;
}
fflush(stdout);
}
printf("100%%\n");
fclose(handle);
free(crt_buf);
if (read_only)
handle = fopen(filename,"rb");
else
handle = fopen(filename,"rb+");
if (!handle)
FAILURE("File created could not be opened");
printf("%s: %d MB file %s created.\n",devid_string,sz,filename);
}
// determine size...
fseek_large(handle,0,SEEK_END);
byte_size=ftell_large(handle);
fseek_large(handle,0,SEEK_SET);
state.byte_pos = ftell_large(handle);
sectors = 32;
heads = 8;
//calc_cylinders();
determine_layout();
model_number=myCfg->get_text_value("model_number",filename);
// skip to the filename portion of the path.
char *p = model_number;
#ifdef _WIN32
char x = '\\';
#else
char x = '/';
#endif
while(*p) {
if(*p==x) model_number=p+1;
p++;
}
printf("%s: Mounted file %s, %" LL "d %d-byte blocks, %" LL "d/%d/%d.\n",devid_string,filename,byte_size/state.block_size,state.block_size,cylinders,heads,sectors);
}
CDiskFile::~CDiskFile(void)
{
printf("%s: Closing file.\n",devid_string);
fclose(handle);
}
bool CDiskFile::seek_byte(off_t_large byte)
{
if (byte >=byte_size)
{
printf("%s: Seek beyond end of file!\n",devid_string);
throw((int)1);
}
fseek_large(handle,byte,SEEK_SET);
state.byte_pos = ftell_large(handle);
return true;
}
size_t CDiskFile::read_bytes(void *dest, size_t bytes)
{
size_t r;
r = fread(dest,1,bytes,handle);
state.byte_pos = ftell_large(handle);
return r;
}
size_t CDiskFile::write_bytes(void * src, size_t bytes)
{
if (read_only)
return 0;
size_t r;
r = fwrite(src,1,bytes,handle);
state.byte_pos = ftell_large(handle);
return r;
}