OXYGENHUB
◆ main openmsx / Contrib / dmk / dmk-common.hh
1.1 KiB · C++ · last changed chore(): initial commit 1 month ago source blame history raw
e8555c89 Andy McCall 1
#ifndef DMK_COMMON_HH
2
#define DMK_COMMON_HH
3
4
#include <array>
5
#include <cstdint>
6
#include <cstdio>
7
#include <memory>
8
#include <stdexcept>
9
#include <string>
10
11
struct DmkHeader
12
{
13
	uint8_t writeProtected;
14
	uint8_t numTracks;
15
	std::array<uint8_t, 2> trackLen;
16
	uint8_t flags;
17
	std::array<uint8_t, 7> reserved;
18
	std::array<uint8_t, 4> format;
19
};
20
21
struct FClose {
22
	void operator()(FILE* f) const { fclose(f); }
23
};
24
using FILE_t = std::unique_ptr<FILE, FClose>;
25
26
class File
27
{
28
public:
29
	File(const std::string& filename, const char* mode)
30
		: file(fopen(filename.c_str(), mode))
31
	{
32
		if (!file) {
33
			throw std::runtime_error("Couldn't open: " + filename);
34
		}
35
	}
36
37
	void read(void* data, int size) const
38
	{
39
		if (fread(data, size, 1, file.get()) != 1) {
40
			throw std::runtime_error("Couldn't read file");
41
		}
42
	}
43
44
	void write(const void* data, int size) const
45
	{
46
		if (fwrite(data, size, 1, file.get()) != 1) {
47
			throw std::runtime_error("Couldn't write file");
48
		}
49
	}
50
51
private:
52
	FILE_t file;
53
};
54
55
56
inline void updateCrc(uint16_t& crc, uint8_t val)
57
{
58
	for (int i = 8; i < 16; ++i) {
59
		crc = (crc << 1) ^ (((crc ^ (val << i)) & 0x8000) ? 0x1021 : 0);
60
	}
61
}
62
63
#endif