-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Start implementation of mass storage device emulation.
- Loading branch information
Showing
5 changed files
with
350 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* | ||
*-------------------------------------------------------------------- | ||
* Project: VM65 - Virtual Machine/CPU emulator programming | ||
* framework. | ||
* | ||
* File: MassStorage.h | ||
* | ||
* Purpose: Prototype of MassStorage class and supporting data | ||
* structures, constants, enumerations and macros. | ||
* | ||
* 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. | ||
*-------------------------------------------------------------------- | ||
*/ | ||
#ifndef MASSSTORAGE_H | ||
#define MASSSTORAGE_H | ||
|
||
#include <string> | ||
|
||
using namespace std; | ||
|
||
namespace MKBasic { | ||
|
||
class MassStorage { | ||
|
||
public: | ||
|
||
// CBM 1541 format | ||
static const int max_numof_sectors = 21; | ||
static const int sector_size = 256; | ||
static const int max_numof_tracks = 35; | ||
static const int num_of_images = 10; | ||
constexpr static int sect_per_track[] = | ||
{21, 21, 21, 21, 21, 21, 21, | ||
21, 21, 21, 21, 21, 21, 21, | ||
21, 21, 21, | ||
19, 19, 19, 19, 19, 19, 19, | ||
18, 18, 18, 18, 18, 18, | ||
17, 17, 17, 17, 17}; | ||
|
||
MassStorage(); | ||
~MassStorage(); | ||
int Format (int id, string name); // format disk image | ||
int Flush (int id); // flush disk image to hard drive | ||
unsigned char *ReadSectorData(int id, | ||
int track, | ||
int sector); // read data from sector | ||
int WriteSectorData (int id, | ||
int track, | ||
int sector, | ||
unsigned char *buf); // write data to sector | ||
|
||
private: | ||
|
||
// definition of a disk track structure | ||
struct DiskTrack { | ||
|
||
int num; | ||
int num_of_sectors; | ||
unsigned char data[max_numof_sectors * sector_size]; | ||
|
||
}; | ||
|
||
// definition of a disk image structure | ||
struct DiskImage { | ||
|
||
int id; | ||
string name; | ||
DiskTrack data[max_numof_tracks]; | ||
}; | ||
|
||
unsigned char mSectorBuf[sector_size]; // buffer for sector data | ||
DiskImage mDiskImages[num_of_images]; // buffer for all disk images | ||
|
||
void Initialize(); | ||
int LoadFromFile(int id, string name); // load image from disk cache | ||
|
||
}; // class MassStorage | ||
|
||
} // namespace MKBasic | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters