mirror of
https://github.com/dingusdev/dingusppc.git
synced 2025-02-03 00:29:48 +00:00
Add BlockStorageDevice class.
This commit is contained in:
parent
ac267b3daa
commit
3c5a0ad8c1
@ -15,6 +15,7 @@ file(GLOB SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp"
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/memctrl/*.cpp"
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/serial/*.cpp"
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/sound/*.cpp"
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/storage/*.cpp"
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/video/*.cpp"
|
||||
)
|
||||
|
||||
|
111
devices/storage/blockstoragedevice.cpp
Normal file
111
devices/storage/blockstoragedevice.cpp
Normal file
@ -0,0 +1,111 @@
|
||||
/*
|
||||
DingusPPC - The Experimental PowerPC Macintosh emulator
|
||||
Copyright (C) 2018-23 divingkatae and maximum
|
||||
(theweirdo) spatium
|
||||
|
||||
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
|
||||
|
||||
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 3 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, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/** @file Block storage device implementation. */
|
||||
|
||||
#include <devices/storage/blockstoragedevice.h>
|
||||
#include <loguru.hpp>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
BlockStorageDevice::BlockStorageDevice(const uint32_t cache_blocks, const uint32_t block_size) {
|
||||
this->block_size = block_size;
|
||||
this->cache_size = cache_blocks * this->block_size;
|
||||
|
||||
// allocate device cache and fill it with zeroes
|
||||
this->data_cache = std::unique_ptr<char[]>(new char[this->cache_size] ());
|
||||
}
|
||||
|
||||
BlockStorageDevice::~BlockStorageDevice() {
|
||||
if (this->img_file)
|
||||
this->img_file.close();
|
||||
}
|
||||
|
||||
int BlockStorageDevice::set_host_file(std::string file_path) {
|
||||
this->is_ready = false;
|
||||
|
||||
this->img_file.open(file_path, ios::out | ios::in | ios::binary);
|
||||
|
||||
struct stat stat_buf;
|
||||
int rc = stat(file_path.c_str(), &stat_buf);
|
||||
if (rc)
|
||||
return -1;
|
||||
|
||||
this->size_bytes = stat_buf.st_size;
|
||||
this->size_blocks = this->size_bytes / this->block_size;
|
||||
this->set_fpos(0);
|
||||
|
||||
this->is_ready = true;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int BlockStorageDevice::set_fpos(const uint32_t lba) {
|
||||
this->cur_fpos = lba * this->block_size;
|
||||
this->img_file.seekg(this->cur_fpos, this->img_file.beg);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int BlockStorageDevice::read_begin(int nblocks, uint32_t max_len) {
|
||||
uint32_t xfer_len = std::min(this->cache_size, max_len);
|
||||
uint32_t read_size = nblocks * this->block_size;
|
||||
if (read_size > xfer_len) {
|
||||
this->remain_size = read_size - xfer_len;
|
||||
read_size = xfer_len;
|
||||
} else {
|
||||
this->remain_size = 0;
|
||||
}
|
||||
|
||||
this->img_file.read(this->data_cache.get(), read_size);
|
||||
this->cur_fpos += read_size;
|
||||
|
||||
return read_size;
|
||||
}
|
||||
|
||||
int BlockStorageDevice::read_more() {
|
||||
uint32_t read_size;
|
||||
|
||||
if (!this->remain_size)
|
||||
return 0;
|
||||
|
||||
if (this->remain_size > this->cache_size) {
|
||||
this->remain_size -= this->cache_size;
|
||||
read_size = this->cache_size;
|
||||
} else {
|
||||
read_size = this->remain_size;
|
||||
this->remain_size = 0;
|
||||
}
|
||||
|
||||
this->img_file.read(this->data_cache.get(), read_size);
|
||||
this->cur_fpos += read_size;
|
||||
|
||||
return read_size;
|
||||
}
|
||||
|
||||
int BlockStorageDevice::write_begin(char *buf, int nblocks) {
|
||||
if (!this->is_writeable)
|
||||
return -1;
|
||||
|
||||
return 0;
|
||||
}
|
61
devices/storage/blockstoragedevice.h
Normal file
61
devices/storage/blockstoragedevice.h
Normal file
@ -0,0 +1,61 @@
|
||||
/*
|
||||
DingusPPC - The Experimental PowerPC Macintosh emulator
|
||||
Copyright (C) 2018-23 divingkatae and maximum
|
||||
(theweirdo) spatium
|
||||
|
||||
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
|
||||
|
||||
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 3 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, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/** @file Block storage device definitions. */
|
||||
|
||||
#ifndef BLOCK_STORAGE_DEVICE_H
|
||||
#define BLOCK_STORAGE_DEVICE_H
|
||||
|
||||
#include <cinttypes>
|
||||
#include <fstream>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
class BlockStorageDevice {
|
||||
public:
|
||||
BlockStorageDevice(const uint32_t cache_blocks, const uint32_t block_size=512);
|
||||
~BlockStorageDevice();
|
||||
|
||||
void set_block_size(const int blk_size) { this->block_size = blk_size; };
|
||||
|
||||
int set_host_file(std::string file_path);
|
||||
|
||||
int set_fpos(const uint32_t lba);
|
||||
int read_begin(int nblocks, uint32_t max_len);
|
||||
int data_left() { return this->remain_size; };
|
||||
int read_more();
|
||||
int write_begin(char *buf, int nblocks);
|
||||
|
||||
protected:
|
||||
std::fstream img_file = {};
|
||||
uint64_t size_bytes = 0; // image file size in bytes
|
||||
uint64_t size_blocks = 0; // image file size in blocks
|
||||
uint64_t cur_fpos = 0; // current image file pointer position
|
||||
uint32_t block_size = 512; // physical block size
|
||||
uint32_t cache_size = 0; // cache size
|
||||
uint32_t remain_size = 0;
|
||||
bool is_writeable = false;
|
||||
bool is_ready = false; // ready for operation
|
||||
|
||||
std::unique_ptr<char[]> data_cache;
|
||||
};
|
||||
|
||||
#endif // BLOCK_STORAGE_DEVICE_H
|
Loading…
x
Reference in New Issue
Block a user