forked from makarcz/vm6502
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MassStorage.cpp
224 lines (199 loc) · 6.48 KB
/
MassStorage.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
*--------------------------------------------------------------------
* Project: VM65 - Virtual Machine/CPU emulator programming
* framework.
*
* File: MassStorage.cpp
*
* Purpose: Implementation of MassStorage class.
* To emulate disk-like mass storage device for data.
*
* Date: 8/16/2017
*
* Copyright: (C) by Marek Karcz 2016, 2017. All rights reserved.
*
* Contact: [email protected]
*
* License Agreement and Warranty:
This software is provided with No Warranty.
I (Marek Karcz) will not be held responsible for any damage to
computer systems, data or user's health resulting from use.
Please proceed responsibly and apply common sense.
This software is provided in hope that it will be useful.
It is free of charge for non-commercial and educational use.
Distribution of this software in non-commercial and educational
derivative work is permitted under condition that original
copyright notices and comments are preserved. Some 3-rd party work
included with this project may require separate application for
permission from their respective authors/copyright owners.
*--------------------------------------------------------------------
*/
#include <stdio.h>
#include "MassStorage.h"
#include "MKGenException.h"
#include "system.h"
namespace MKBasic {
constexpr int MassStorage::sect_per_track[];
/*
*--------------------------------------------------------------------
* Method: MassStorage()
* Purpose: Class default constructor.
* Arguments:
* Returns:
*--------------------------------------------------------------------
*/
MassStorage::MassStorage()
{
Initialize();
}
/*
*--------------------------------------------------------------------
* Method: ~MassStorage()
* Purpose: Class destructor.
* Arguments:
* Returns:
*--------------------------------------------------------------------
*/
MassStorage::~MassStorage()
{
}
/*
*--------------------------------------------------------------------
* Method: Initialize()
* Purpose: Initialize internal variables.
* Arguments:
* Returns:
*--------------------------------------------------------------------
*/
void MassStorage::Initialize()
{
// TO DO: add code to initialize class
// initialize standard disk images
DiskTrack dtrk;
DiskImage ditbl[] = {
{0, "DISK0", {dtrk}},
{1, "DISK1", {dtrk}},
{2, "DISK2", {dtrk}},
{3, "DISK3", {dtrk}},
{4, "DISK4", {dtrk}},
{5, "DISK5", {dtrk}},
{6, "DISK6", {dtrk}},
{7, "DISK7", {dtrk}},
{8, "DISK8", {dtrk}},
{9, "DISK9", {dtrk}}
};
for (int i=0; i < num_of_images; i++) {
mDiskImages[i].id = ditbl[i].id;
mDiskImages[i].name = ditbl[i].name;
string fname = mDiskImages[i].name + "_" + to_string(mDiskImages[i].id) + ".disk";
FILE *fp = fopen(fname.c_str(), "r");
if (NULL == fp) {
Format(mDiskImages[i].id, mDiskImages[i].name);
Flush(mDiskImages[i].id);
} else {
LoadFromFile(mDiskImages[i].id, mDiskImages[i].name);
fclose(fp);
}
}
}
/*
*--------------------------------------------------------------------
* Method: Format()
* Purpose: Format the disk image.
* Arguments: id - the number / id of the disk image to format
* name - the new name of the disk image
* Returns: int - 0 if OK, less than 0 if error.
*--------------------------------------------------------------------
*/
int MassStorage::Format (int id, string name)
{
int ret = 0;
// TO DO: add code to format disk image
DiskImage dimg;
dimg.name = name;
for (int track=0; track < max_numof_tracks; track++) {
dimg.data[track].num = track+1;
dimg.data[track].num_of_sectors = sect_per_track[track];
if (track+1 == 18) { // BAM + directory
dimg.data[track].data[0x00] = 0x12; // point to track 18
dimg.data[track].data[0x01] = 0x01; // and sector 1 (directory)
dimg.data[track].data[0x02] = 0x41; // DOS version
dimg.data[track].data[0x03] = 0x01; // unused
// BAM
for (int sector = 0x04; sector < 0x90; sector += 4) {
dimg.data[track].data[sector] = dimg.data[track].num_of_sectors;
dimg.data[track].data[sector+1] = 0xff;
dimg.data[track].data[sector+2] = 0xff;
dimg.data[track].data[sector+3] = ~(0xff << (dimg.data[track].num_of_sectors - 16));
}
} else {
}
}
return ret;
}
/*
*--------------------------------------------------------------------
* Method: Flush()
* Purpose: Save image data to disk file.
* Arguments: id - the disk image id
* Returns: int - 0 if OK, less than 0 if error
*--------------------------------------------------------------------
*/
int MassStorage::Flush (int id)
{
int ret = 0;
// TO DO: add code to flush disk image to file
return ret;
}
/*
*--------------------------------------------------------------------
* Method: ReadSectorData()
* Purpose: Read data from disk's image sector.
* Arguments: id - disk image id
* track - track#
* sector - sector#
* Returns: pointer to data
*--------------------------------------------------------------------
*/
unsigned char * MassStorage::ReadSectorData(int id,
int track,
int sector)
{
// TO DO: add code to read data
return mSectorBuf;
}
/*
*--------------------------------------------------------------------
* Method: WriteSectorData()
* Purpose: Write data to disk's image sector.
* Arguments: id - the disk image id
* track - track#
* sector - sector#
* buf - pointer to data
* Returns: int - 0 if OK, less than 0 if error
*--------------------------------------------------------------------
*/
int MassStorage::WriteSectorData (int id,
int track,
int sector,
unsigned char *buf)
{
int ret = 0;
// TO DO: add code to write sector data
return ret;
}
/*
*--------------------------------------------------------------------
* Method: LoadFromFile()
* Purpose: Load image data from file.
* Arguments: id - the disk image id
* name - disk image name
* Returns: int - 0 if OK, less than 0 if error
*--------------------------------------------------------------------
*/
int MassStorage::LoadFromFile(int id, string name)
{
int ret = 0;
return ret;
}
} // namespace MKBasic