-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBlitzBank.cs
132 lines (114 loc) · 5 KB
/
BlitzBank.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Lic:
// BlitzBank.cs
// Blitz Bank
// version: 21.09.11
// Copyright (C) 2019, 2021 Jeroen P. Broks
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would be
// appreciated but is not required.
// 2. Altered source versions must be plainly marked as such, and must not be
// misrepresented as being the original software.
// 3. This notice may not be removed or altered from any source distribution.
// EndLic
using System;
namespace TrickyUnits {
/*
This is just a fake class, to make conversions from Blitz to C# easier
For now only LittleEndian is supported, however if I need it, this may be expanded in the future.
*/
enum BlitzEndian { None, Little, Big }
class BlitzBank {
byte[] Buffer;
readonly BlitzEndian WantEndian;
BlitzEndian _Endian = BlitzEndian.None;
public BlitzEndian Endian {
get {
if (_Endian == BlitzEndian.None) {
var q = BitConverter.GetBytes((int)200);
if (q[0] == 200)
_Endian = BlitzEndian.Little;
else
_Endian = BlitzEndian.Big;
}
return _Endian;
}
}
public BlitzBank(int size, BlitzEndian Endian = BlitzEndian.Little) { Buffer = new byte[size]; WantEndian = Endian; }
public BlitzBank(byte[] LBuf,BlitzEndian Endian=BlitzEndian.Little, bool cpylink = false) {
WantEndian = Endian;
if (cpylink) { Buffer = LBuf; return; } // Faster, friendlier on your RAM but can get nasty results if you are not sure about what you are doing!
Buffer = new byte[LBuf.Length];
for (int i = 0; i < LBuf.Length; ++i) Buffer[i] = LBuf[i];
}
public void ResizeBank(int size) {
Buffer = new byte[size];
}
public void Poke(int a, byte i) {
if (a < 0 || a >= Buffer.Length) throw new Exception($"Poke({a},{i}): Out of bounds (0-{Buffer.Length - 1})!");
Buffer[a] = i;
}
public void PokeShort(int addr, int i) {
if (Endian != BlitzEndian.Little) throw new Exception($"BlitzBank.PokeShort({addr},{i}): Requires Little Endian processor!");
var bf = BitConverter.GetBytes(i);
Poke(addr + 0, bf[0]);
Poke(addr + 1, bf[1]);
}
public byte Peek(int a) {
if (a < 0 || a >= Buffer.Length) throw new Exception($"Peek({a}): Out of bounds (0-{Buffer.Length - 1})!");
return Buffer[a];
}
public int PeekShort32(int addr) {
if (Endian != BlitzEndian.Little) throw new Exception($"BlitzBank.PeekShort({addr}): Requires Little Endian processor!");
var bf = new byte[4];
bf[0] = Peek(addr + 0);
bf[1] = Peek(addr + 1);
bf[2] = 0;
bf[3] = 0;
return BitConverter.ToInt32(bf, 0);
}
public int PeekInt(int addr) {
if (Endian != BlitzEndian.Little) throw new Exception($"BlitzBank.PeekShort({addr}): Requires Little Endian processor!");
var bf = new byte[4];
bf[0] = Peek(addr + 0);
bf[1] = Peek(addr + 1);
bf[2] = Peek(addr + 2);
bf[3] = Peek(addr + 3);
return BitConverter.ToInt32(bf, 0);
}
public short PeekShort(int addr) {
if (Endian != BlitzEndian.Little) throw new Exception($"BlitzBank.PeekShort({addr}): Requires Little Endian processor!");
var bf = new byte[2];
bf[0] = Peek(addr + 0);
bf[1] = Peek(addr + 1);
return BitConverter.ToInt16(bf, 0);
}
public long PeekLong(int addr) {
if (Endian != BlitzEndian.Little) throw new Exception($"BlitzBank.PeekShort({addr}): Requires Little Endian processor!");
try {
var bf = new byte[8];
bf[0] = Peek(addr + 0);
bf[1] = Peek(addr + 1);
bf[2] = Peek(addr + 2);
bf[3] = Peek(addr + 3);
bf[4] = Peek(addr + 4);
bf[5] = Peek(addr + 5);
bf[6] = Peek(addr + 6);
bf[7] = Peek(addr + 7);
return BitConverter.ToInt64(bf, 0);
} catch (Exception E) {
Console.WriteLine($".NET error! {E.Message}\n{addr}/{Buffer.Length}");
#if DEBUG
Console.WriteLine(E.StackTrace);
#endif
return 0;
}
}
}
}