VirtualC64 v5.0 beta
Commodore 64 Emulator
Loading...
Searching...
No Matches
FSBlock.h
1// -----------------------------------------------------------------------------
2// This file is part of VirtualC64
3//
4// Copyright (C) Dirk W. Hoffmann. www.dirkwhoffmann.de
5// This FILE is dual-licensed. You are free to choose between:
6//
7// - The GNU General Public License v3 (or any later version)
8// - The Mozilla Public License v2
9//
10// SPDX-License-Identifier: GPL-3.0-or-later OR MPL-2.0
11// -----------------------------------------------------------------------------
12
13#pragma once
14
15#include "FSTypes.h"
16#include "Buffer.h"
17#include "CoreObject.h"
18#include "PETName.h"
19
20namespace vc64 {
21
22class FSBlock : CoreObject {
23
24 // The device this block belongs to
25 class FileSystem &device;
26
27public:
28
29 // The number of this block
30 Block nr;
31
32 // Outcome of the last integrity check (0 = OK, n = n-th corrupted block)
33 u32 corrupted = 0;
34
35 // The actual block data
36 u8 data[256];
37
38 // Error code (imported from D64 files, 1 = No error)
39 u8 errorCode = 1;
40
41
42 //
43 // Constructing
44 //
45
46public:
47
48 FSBlock(FileSystem& _device, u32 _nr);
49 virtual ~FSBlock() = default;
50 const char *objectName() const override { return "FSBlock"; }
51
52
53 //
54 // Querying block properties
55 //
56
57public:
58
59 // Returns the type of this block
60 FSBlockType type() const;
61
62 // Returns the track / sector link stored in the fist two bytes
63 TSLink tsLink() { return TSLink { data[0], data[1] }; }
64
65
66 //
67 // Formatting
68 //
69
70 // Writes the Block Availability Map (BAM)
71 void writeBAM(const string &name = "");
72 void writeBAM(PETName<16> &name);
73
74
75 //
76 // Debugging
77 //
78
79public:
80
81 // Prints some debug information for this block
82 void dump() const;
83
84
85 //
86 // Integrity checking
87 //
88
89public:
90
91 // Returns the role of a certain byte in this block
92 FSUsage itemType(u32 byte) const;
93
94 // Checks the integrity of a certain byte in this block
95 ErrorCode check(u32 byte, u8 *expected, bool strict) const;
96
97 // Scans the block data and returns the number of errors
98 isize check(bool strict) const;
99
100
101 //
102 // Importing and exporting
103 //
104
105public:
106
107 // Imports this block from a buffer (bsize must match the volume block size)
108 void importBlock(const u8 *src);
109
110 // Exports this block to a buffer (bsize must match the volume block size)
111 void exportBlock(u8 *dst);
112
113};
114
115typedef FSBlock* BlockPtr;
116
117
118//
119// Convenience macros used inside the check() methods
120//
121
122typedef FSBlock* BlockPtr;
123
124#define EXPECT_BYTE(exp) { \
125if (value != (exp)) { *expected = (u8)(exp); return ERROR_FS_EXPECTED_VAL; } }
126
127#define EXPECT_MIN(min) { \
128if (value < (min)) { *expected = (u8)(min); return ERROR_FS_EXPECTED_MIN; } }
129
130#define EXPECT_MAX(max) { \
131if (value > (max)) { *expected = (u8)(max); return ERROR_FS_EXPECTED_MAX; } }
132
133#define EXPECT_RANGE(min,max) { \
134EXPECT_MIN(min); EXPECT_MAX(max) }
135
136#define EXPECT_TRACK_REF(s) \
137EXPECT_RANGE(0, device.layout.numTracks() + 1)
138
139#define EXPECT_SECTOR_REF(t) { \
140if (isize num = device.layout.numSectors(t)) \
141EXPECT_RANGE(0,num) else if (strict) EXPECT_MAX(254) }
142
143}
VirtualC64 project namespace.
Definition CmdQueue.cpp:16