VirtualC64 v5.0 beta
Commodore 64 Emulator
Loading...
Searching...
No Matches
MemUtils.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 "Types.h"
16#include "Checksum.h"
17#include <bit>
18
19namespace vc64::util {
20
21// Reverses the byte ordering in an integer value
22#ifdef _MSC_VER
23#define SWAP16 _byteswap_ushort
24#define SWAP32 _byteswap_ulong
25#define SWAP64 _byteswap_uint64
26#else
27#define SWAP16 __builtin_bswap16
28#define SWAP32 __builtin_bswap32
29#define SWAP64 __builtin_bswap64
30#endif
31
32//
33// Byte order
34//
35
36// Returns the big endian representation of an integer value
37template<typename T> T bigEndian(T x);
38
39template<>
40inline u16 bigEndian(u16 x)
41{
42 if constexpr (std::endian::native == std::endian::big) {
43 return x;
44 } else {
45 return SWAP16(x);
46 }
47}
48
49template<>
50inline u32 bigEndian(u32 x)
51{
52 if constexpr (std::endian::native == std::endian::big) {
53 return x;
54 } else {
55 return SWAP32(x);
56 }
57}
58
59template<>
60inline u64 bigEndian(u64 x)
61{
62 if constexpr (std::endian::native == std::endian::big) {
63 return x;
64 } else {
65 return SWAP64(x);
66 }
67}
68
69//
70// Memory content
71//
72
73// Reads a value in big-endian format
74#define R8BE(a) (*(u8 *)(a))
75#define R16BE(a) HI_LO(*(u8 *)(a), *(u8 *)((a)+1))
76#define R32BE(a) HI_HI_LO_LO(*(u8 *)(a), *(u8 *)((a)+1), *(u8 *)((a)+2), *(u8 *)((a)+3))
77
78/*
79#define R8BE_ALIGNED(a) (*(u8 *)(a))
80#define R16BE_ALIGNED(a) (util::bigEndian(*(u16 *)(a)))
81#define R32BE_ALIGNED(a) (util::bigEndian(*(u32 *)(a)))
82*/
83
84// Writes a value in big-endian format
85#define W8BE(a,v) { *(u8 *)(a) = (v); }
86#define W16BE(a,v) { *(u8 *)(a) = HI_BYTE(v); *(u8 *)((a)+1) = LO_BYTE(v); }
87#define W32BE(a,v) { W16BE(a,HI_WORD(v)); W16BE((a)+2,LO_WORD(v)); }
88
89/*
90#define W8BE_ALIGNED(a,v) { *(u8 *)(a) = (u8)(v); }
91#define W16BE_ALIGNED(a,v) { *(u16 *)(a) = util::bigEndian((u16)v); }
92#define W32BE_ALIGNED(a,v) { *(u32 *)(a) = util::bigEndian((u32)v); }
93*/
94
95// Checks if a certain memory area is all zero
96bool isZero(const u8 *ptr, isize size);
97
98// Replaces the first occurence of a byte or character sequence by another one
99void replace(u8 *p, isize size, const u8 *sequence, const u8 *substitute);
100void replace(char *p, isize size, const char *sequence, const char *substitute);
101
102// Extracts all readable ASCII characters from a buffer
103void readAscii(const u8 *buf, isize len, char *result, char fill = '.');
104
105// Prints a hex dump of a buffer to the console
106void hexdump(u8 *p, isize size, isize cols, isize pad);
107void hexdump(u8 *p, isize size, isize cols = 32);
108void hexdumpWords(u8 *p, isize size, isize cols = 32);
109void hexdumpLongwords(u8 *p, isize size, isize cols = 32);
110
111}