Repository navigation
Expand file tree
/
Copy pathtest.cpp
More file actions
383 lines (339 loc) · 12 KB
/
Copy pathtest.cpp
File metadata and controls
383 lines (339 loc) · 12 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
//==============================================================================
// PROJECT: zqloader
// FILE: test.cpp
// DESCRIPTION: Various tests
//
// Copyright (c) 2023 Daan Scherft [Oxidaan]
// This project uses the MIT license. See LICENSE.txt for details.
//==============================================================================
#include "datablock.h"
#include <cstdint> // uint16_t
#include <filesystem> // std::filesystem
#include <iostream>
#include "turboblocks.h"
#include "compressor.h"
#include <algorithm>
#include <cstddef>
#include <ios>
#include <vector>
#include "zqloader.h"
#include "memoryblock.h"
template <class TIterator>
uint16_t Crc16(TIterator p_begin, TIterator p_end)
{
uint16_t shiftreg = 0xffff;
uint16_t polynomial = 0x1021;
for (auto it = p_begin; it != p_end; it++)
{
auto b = *it;
shiftreg = shiftreg ^ (uint16_t(b) << 8);
for (int i = 0; i < 8; i++)
{
bool do_xor = shiftreg & 0x8000;
shiftreg <<= 1;
if (do_xor)
{
shiftreg ^= polynomial;
}
}
}
return shiftreg;
}
uint16_t Crc16(const std::vector<std::byte>& p_data)
{
return Crc16(p_data.begin(), p_data.end());
}
namespace fs = std::filesystem;
void DumpBlock(const DataBlock &p_block)
{
for (int n = 0; n < p_block.size(); n++)
{
if (n % 16 == 0)
{
std::cout << std::endl;
}
else
{
std::cout << ", ";
}
std::cout << std::hex << "0x" << int(p_block[n]);
}
std::cout << std::dec;
}
void TestCompressor()
{
using Compressor = Compressor<DataBlock>;
Compressor::RLE_Meta rle{};
DataBlock block;
for(int n = 0; n < 8; n++)
{
block.push_back(std::byte(0));
}
block.push_back(std::byte(1));
block.push_back(std::byte(0));
block.push_back(std::byte(1));
block.push_back(std::byte(1));
for(int n = 0; n < 4; n++)
{
block.push_back(std::byte(2));
}
for(int n = 0; n < 10; n++)
{
block.push_back(std::byte(0));
block.push_back(std::byte(0));
block.push_back(std::byte(3));
}
uint16_t decompress_counter;
DataBlock compressed = Compressor::Compress(block, rle, decompress_counter);
DataBlock decompressed = Compressor::DeCompress(compressed, rle);
std::cout << "Orginal size = " << block.size() << ' ' <<
"Compressed size = " << compressed.size() << ' ' <<
((decompressed == block) ? "OK" : "NOK") << std::endl;
std::cout << rle << std::endl;
DumpBlock(compressed);
std::cout << std::endl;
DumpBlock(decompressed);
std::cout << std::endl;
}
/// returns USR (MC code start) address to go to after loading is done (0 = return to basic)
/// "C:\Projects\Visual Studio\Projects\zqloader\z80\zqloader.tap" "C:\Projects\Visual Studio\Projects\zqloader\z80\zqloader_test.bin"
uint16_t Test(TurboBlocks& p_blocks, const fs::path &p_filename)
{
(void)p_blocks;
std::cout << "Running test... (" << p_filename << ")" << std::endl;
#if 0
(void)p_blocks;
(void)p_filename;
DataBlock text_attr;
text_attr.resize(2000);
WriteTextToAttr(text_attr, ToUpper("AAHELLO WORLD"), 0_byte); // 0_byte: random colors
return 0;
#endif
#if 0
// Copy loader to screen, then use that to load an attribute block
{
// p_blocks.CopyLoaderToScreen(16384);
p_blocks.CopyLoaderToScreen(23700);
//DataBlock empty;
{
DataBlock text_attr;
text_attr.resize(2000);
WriteTextToAttr(text_attr, ToUpper("HELLO WORLD"), 0_byte); // 0_byte: random colors
p_blocks.AddDataBlock(std::move(text_attr), 23690);
}
//p_blocks.AddDataBlock(std::move(empty),50000);
// For fun write a attribute block -> text_attr (last 3rd)
// at the bottom 1/3rd of screen were our loader is.
// We also wont see the loader code then.
for(int col = 0; col > -51; col--)
{
DataBlock text_attr;
text_attr.resize(256);
WriteTextToAttr(text_attr, ToUpper("HELLO WORLD"), 0_byte, false, col); // 0_byte: random colors
// copy fun attributes into 48k data block
p_blocks.SetCompressionType(CompressionType::none);
p_blocks.AddDataBlock(std::move(text_attr), 16384 + 6 * 1024 + 512);
}
return 0;
}
#endif
#if 0
// See zqloader_test.z80asm
// p_blocks.CopyLoaderToScreen(16384); // // can never work cannot print result (SP problems)
// "C:\Projects\Visual Studio\Projects\zqloader\z80\zqloader.tap" "C:\Projects\Visual Studio\Projects\zqloader\z80\zqloader_test.bin"
std::cout << "Reading binary file " << p_filename << std::endl;
DataBlock block;
block.LoadFromFile(p_filename);
auto filelen = block.size();
block.push_back(0_byte); // make space for length filled in later, see below **
block.push_back(0_byte); // ,,
block.push_back(0x10_byte); // poly
block.push_back(0x21_byte); // poly
auto codelen = block.size();
// -----------------------------------------------------------------
DataBlock fileblock1;
fileblock1.LoadFromFile(fs::path("test2.bin"));
block.insert(block.end(), fileblock1.begin(), fileblock1.end()); // append
DataBlock fileblock2;
fileblock2.LoadFromFile(fs::path("test2.bin"));
block.insert(block.end(), fileblock2.begin(), fileblock2.end()); // append
/*
// block.push_back(std::byte(200));
for(int n = 0; n < 4; n ++)
{
for(int i = 0; i < 256; i++)
{
if(i != 100 && i != 200)
{
block.push_back(std::byte(i));
}
}
}
block.push_back(std::byte(100));
block.push_back(std::byte(101));
for(int n = 0; n < 100; n++)
{
block.push_back(0_byte);
}
*/
/*
// Test data payload
for(int n = 0; n < 16384; n++)
{
block.push_back(0_byte);
}
for(int n = 0; n < 16384; n++)
{
block.push_back(255_byte);
}
*/
/*
for (int n = 0; n < 20; n++)
{
block.push_back(10_byte);
}
for (int n = 0; n < 254; n++)
{
block.push_back(std::byte(n));
//block.push_back(10_byte);
}
for (int n = 0; n < 254; n++)
{
block.push_back(std::byte(n));
//block.push_back(10_byte);
}
for (int n = 0; n < 254; n++)
{
block.push_back(std::byte(n));
//block.push_back(10_byte);
}
for (int n = 0; n < 20; n++)
{
block.push_back(10_byte);
}
for (int n = 0; n < 2000; n++)
{
block.push_back(0_byte);
block.push_back(0_byte);
block.push_back(0_byte);
block.push_back(0_byte);
block.push_back(std::byte(Random(0, 255))); // Note: causes different CRC each run!
block.push_back(std::byte(Random(0, 255)));
block.push_back(std::byte(Random(0, 255)));
block.push_back(std::byte(Random(0, 255)));
block.push_back(std::byte(Random(0, 255)));
block.push_back(std::byte(Random(0, 255)));
block.push_back(1_byte);
block.push_back(1_byte);
block.push_back(1_byte);
block.push_back(1_byte);
}
*/
// p_blocks.CopyLoaderToScreen(24023); // can never work cannot print result (SP problems)
// p_blocks.SetCompressionType(CompressionType::none);
/*
// Test split, add in reverse order
DataBlock block1(block.begin(), block.begin() + 5000);
DataBlock block2(block.begin() + 5000, block.begin() + 10000);
DataBlock block3(block.begin() + 10000, block.end());
p_blocks.AddDataBlock(std::move(block3), uint16_t(start+10000));
p_blocks.AddDataBlock(std::move(block2), uint16_t(start+5000));
p_blocks.AddDataBlock(std::move(block1), uint16_t(start));
*/
//auto start = 16384;
auto start = 0xffff - block.size() + 1; // this is better test (overwrites loader @upper)...
//auto start = 32768; // ...than this.
//auto start = 25000; // start of test data
auto len = uint16_t(block.size()) - codelen;
std::cout << "Length of test data = " << std::dec << len << " (crc_code_size = " << filelen << ")" << std::endl;
std::cout << "Start of test data = " << std::dec << (start + codelen) << std::endl;
block[filelen] = std::byte((len & 0xff00) >> 8); // fill in length **
block[filelen + 1] = std::byte(len & 0xff);
auto crc = Crc16(block.begin() + filelen + 4, block.end()); // excluding length and poly
std::cout << "=> CRC of test data = " << std::dec << crc << "; 0x" << std::hex << crc << std::dec << std::endl;
// split 'n mix
DataBlock codeblock(block.begin(), block.begin() + codelen);
auto len1 = fileblock1.size();
DataBlock fblock1(block.begin() + codelen, block.begin() + codelen + len1);
auto len2 = fileblock2.size();
DataBlock fblock2(block.begin() + codelen + len1, block.begin() + codelen + len1 + len2);
p_blocks.AddDataBlock(std::move(codeblock), uint16_t(start));
p_blocks.AddDataBlock(std::move(fblock2), uint16_t(start + codelen + len1));
p_blocks.AddDataBlock(std::move(fblock1), uint16_t(start + codelen));
// p_blocks.AddDataBlock(std::move(block), uint16_t(start));
return uint16_t(start);
#endif
#if 1
(void)p_filename;
p_blocks.SetCompressionType(CompressionType::none);
// screen pattern
DataBlock block;
while (block.size() < 6 * 1024)
{
// block.push_back(0_byte);
// block.push_back(0_byte);
for (int i = 0; i < 64; i++)
block.push_back(255_byte);
for (int i = 0; i < 64; i++)
block.push_back(0_byte);
for (int i = 0; i < 64; i++)
block.push_back(0b11001010_byte);
for (int i = 0; i < 64; i++)
block.push_back(0b01010101_byte);
// block.push_back(0b11001100_byte);
}
//DataBlock attr;
//attr.resize(768);
//ZQLoader::WriteTextToAttr(attr, "Test", 0x01_byte, false, 0);
//block.insert(block.end(), attr.begin(), attr.end());
std::cout << "Size = " << block.size() << std::endl;
// TurboBlocks tblocks(p_loader_length, CompressionType::none);
p_blocks.AddMemoryBlock({std::move(block), uint16_t(16384)});
// p_blocks.MoveToLoader(spectrumloader);
return 0;
#endif
#if 0
TonePulser tone;
tone.SetPattern(80, 80).SetLength(20s).MoveToLoader(m_spectrumloader);
#endif
#if 0
{
PausePulser pause;
pause.SetLength(100ms).
SetEdge(Edge::zero).
MoveToLoader(m_spectrumloader);
m_spectrumloader.Run();
std::cout << "Edge = " << m_spectrumloader.GetLastEdge() << std::endl;
}
{
SpectrumLoader m_spectrumloader;
TonePulser tone;
tone.SetPattern(700, 700).
// SetLength(500ms).
SetLength(1).
MoveToLoader(m_spectrumloader);
m_spectrumloader.Run();
std::cout << "Edge = " << m_spectrumloader.GetLastEdge() << std::endl;
}
{
SpectrumLoader m_spectrumloader;
TonePulser tone;
tone.SetLength(1). // 000ms).
SetPattern(700, 700, 700).SetLength(1).
MoveToLoader(m_spectrumloader);
m_spectrumloader.Run();
std::cout << "Edge = " << m_spectrumloader.GetLastEdge() << std::endl;
}
#endif
}
void Fun()
{
using DataBlock = std::vector<std::byte>;
DataBlock snapshot_data;
snapshot_data.resize(48 * 1024);
DataBlock screenblock(snapshot_data.begin(), snapshot_data.begin() + 6 * 1024 + 768); // split
DataBlock text_attr;
text_attr.resize(256);
std::copy(text_attr.begin(), text_attr.end(), screenblock.begin() + 16384 + 6 * 1024 + 512 - 16384); // at debug asserts on iterator problem
std::copy(text_attr.begin(), text_attr.end(), screenblock.begin() + (16384 + 6 * 1024 + 512 - 16384)); // okay!
}