From 35705c53458f83995e8810ea1a0e5a1b45ea6099 Mon Sep 17 00:00:00 2001 From: Thomas Harte Date: Tue, 3 Oct 2017 19:12:45 -0400 Subject: [PATCH] Factors out bit reversing from the HFE class. --- .../Clock Signal.xcodeproj/project.pbxproj | 6 ++++ Storage/Data/BitReverse.cpp | 25 ++++++++++++++++ Storage/Data/BitReverse.hpp | 30 +++++++++++++++++++ Storage/Disk/DiskImage/Formats/HFE.cpp | 17 ++--------- 4 files changed, 63 insertions(+), 15 deletions(-) create mode 100644 Storage/Data/BitReverse.cpp create mode 100644 Storage/Data/BitReverse.hpp diff --git a/OSBindings/Mac/Clock Signal.xcodeproj/project.pbxproj b/OSBindings/Mac/Clock Signal.xcodeproj/project.pbxproj index 3e198b866..f15c35951 100644 --- a/OSBindings/Mac/Clock Signal.xcodeproj/project.pbxproj +++ b/OSBindings/Mac/Clock Signal.xcodeproj/project.pbxproj @@ -21,6 +21,7 @@ 4B14978F1EE4B4D200CE2596 /* CSZX8081.mm in Sources */ = {isa = PBXBuildFile; fileRef = 4B14978E1EE4B4D200CE2596 /* CSZX8081.mm */; }; 4B1497921EE4B5A800CE2596 /* ZX8081.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4B1497901EE4B5A800CE2596 /* ZX8081.cpp */; }; 4B1497981EE4B97F00CE2596 /* ZX8081Options.xib in Resources */ = {isa = PBXBuildFile; fileRef = 4B1497961EE4B97F00CE2596 /* ZX8081Options.xib */; }; + 4B1558C01F844ECD006E9A97 /* BitReverse.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4B1558BE1F844ECD006E9A97 /* BitReverse.cpp */; }; 4B1D08061E0F7A1100763741 /* TimeTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = 4B1D08051E0F7A1100763741 /* TimeTests.mm */; }; 4B1E85751D170228001EF87D /* Typer.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4B1E85731D170228001EF87D /* Typer.cpp */; }; 4B1E85811D176468001EF87D /* 6532Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4B1E85801D176468001EF87D /* 6532Tests.swift */; }; @@ -504,6 +505,8 @@ 4B1497901EE4B5A800CE2596 /* ZX8081.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = ZX8081.cpp; path = ZX8081/ZX8081.cpp; sourceTree = ""; }; 4B1497911EE4B5A800CE2596 /* ZX8081.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = ZX8081.hpp; path = ZX8081/ZX8081.hpp; sourceTree = ""; }; 4B1497971EE4B97F00CE2596 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = "Clock Signal/Base.lproj/ZX8081Options.xib"; sourceTree = SOURCE_ROOT; }; + 4B1558BE1F844ECD006E9A97 /* BitReverse.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; name = BitReverse.cpp; path = Data/BitReverse.cpp; sourceTree = ""; }; + 4B1558BF1F844ECD006E9A97 /* BitReverse.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; name = BitReverse.hpp; path = Data/BitReverse.hpp; sourceTree = ""; }; 4B1D08051E0F7A1100763741 /* TimeTests.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = TimeTests.mm; sourceTree = ""; }; 4B1E85731D170228001EF87D /* Typer.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Typer.cpp; sourceTree = ""; }; 4B1E85741D170228001EF87D /* Typer.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = Typer.hpp; sourceTree = ""; }; @@ -1741,8 +1744,10 @@ 4B8805F81DCFF6CD003085B1 /* Data */ = { isa = PBXGroup; children = ( + 4B1558BE1F844ECD006E9A97 /* BitReverse.cpp */, 4B8805F51DCFF6C9003085B1 /* Commodore.cpp */, 4BA0F68C1EEA0E8400E9489E /* ZX8081.cpp */, + 4B1558BF1F844ECD006E9A97 /* BitReverse.hpp */, 4B8805F61DCFF6C9003085B1 /* Commodore.hpp */, 4BA0F68D1EEA0E8400E9489E /* ZX8081.hpp */, ); @@ -2917,6 +2922,7 @@ 4B4518A31F75FD1C00926311 /* HFE.cpp in Sources */, 4B8378E21F336920005CA9E4 /* CharacterMapper.cpp in Sources */, 4B4518A11F75FD1C00926311 /* D64.cpp in Sources */, + 4B1558C01F844ECD006E9A97 /* BitReverse.cpp in Sources */, 4BCF1FA41DADC3DD0039D2E7 /* Oric.cpp in Sources */, 4BEA525E1DF33323007E74F2 /* Tape.cpp in Sources */, 4B8334951F5E25B60097E338 /* C1540.cpp in Sources */, diff --git a/Storage/Data/BitReverse.cpp b/Storage/Data/BitReverse.cpp new file mode 100644 index 000000000..2f14ffdc5 --- /dev/null +++ b/Storage/Data/BitReverse.cpp @@ -0,0 +1,25 @@ +// +// BitReverse.cpp +// Clock Signal +// +// Created by Thomas Harte on 03/10/2017. +// Copyright © 2017 Thomas Harte. All rights reserved. +// + +#include "BitReverse.hpp" + +void Storage::Data::BitReverse::reverse(std::vector &vector) { + for(auto &byte : vector) { + byte = + static_cast( + ((byte & 0x01) << 7) | + ((byte & 0x02) << 5) | + ((byte & 0x04) << 3) | + ((byte & 0x08) << 1) | + ((byte & 0x10) >> 1) | + ((byte & 0x20) >> 3) | + ((byte & 0x40) >> 5) | + ((byte & 0x80) >> 7) + ); + } +} diff --git a/Storage/Data/BitReverse.hpp b/Storage/Data/BitReverse.hpp new file mode 100644 index 000000000..6f54a35ab --- /dev/null +++ b/Storage/Data/BitReverse.hpp @@ -0,0 +1,30 @@ +// +// BitReverse.hpp +// Clock Signal +// +// Created by Thomas Harte on 03/10/2017. +// Copyright © 2017 Thomas Harte. All rights reserved. +// + +#ifndef BitReverse_hpp +#define BitReverse_hpp + +#include +#include + +namespace Storage { +namespace Data { +namespace BitReverse { + +/*! + Reverses the order of the bits in every byte of the vector — + bit 7 exchanges with bit 0, bit 6 exchanges with bit 1, 5 with 2, + and 4 with 3. +*/ +void reverse(std::vector &vector); + +} +} +} + +#endif /* BitReverse_hpp */ diff --git a/Storage/Disk/DiskImage/Formats/HFE.cpp b/Storage/Disk/DiskImage/Formats/HFE.cpp index e6ad0f07f..535ac0741 100644 --- a/Storage/Disk/DiskImage/Formats/HFE.cpp +++ b/Storage/Disk/DiskImage/Formats/HFE.cpp @@ -9,6 +9,7 @@ #include "HFE.hpp" #include "../../Track/PCMTrack.hpp" +#include "../../../Data/BitReverse.hpp" using namespace Storage::Disk; @@ -61,21 +62,7 @@ std::shared_ptr HFE::get_track_at_position(unsigned int head, unsigned in // Flip bytes; HFE's preference is that the least-significant bit // is serialised first, but PCMTrack posts the most-significant first. - for(size_t i = 0; i < segment.data.size(); i++) { - uint8_t original = segment.data[i]; - uint8_t flipped_byte = - (uint8_t)( - ((original & 0x01) << 7) | - ((original & 0x02) << 5) | - ((original & 0x04) << 3) | - ((original & 0x08) << 1) | - ((original & 0x10) >> 1) | - ((original & 0x20) >> 3) | - ((original & 0x40) >> 5) | - ((original & 0x80) >> 7) - ); - segment.data[i] = flipped_byte; - } + Storage::Data::BitReverse::reverse(segment.data); std::shared_ptr track(new PCMTrack(segment)); return track;