Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
254 lines (213 loc) · 10.7 KB
/
Copy pathmain.cpp
File metadata and controls
254 lines (213 loc) · 10.7 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
//==============================================================================
// PROJECT: zqloader
// FILE: main.cpp
// DESCRIPTION: Implementation of main() / console version.
//
// Copyright (c) 2023 Daan Scherft [Oxidaan]
// This project uses the MIT license. See LICENSE.txt for details.
//==============================================================================
// This project use the miniaudio sound library by David Read.
#if __cplusplus < 202002L
// At MSVC
// Properties -> C/C++ -> Language -> C++ Language Standard -> ISO c++17 Standard
// also set at C / C++ -> Command Line -> Additional options : /Zc:__cplusplus
// plus C / C++ -> Preprocessor -> Use standard conforming preprocessor
#error "Need c++20 or more"
#endif
#ifdef _WIN32
#include <conio.h>
#endif
#include "tools.h" // CommandLine
#include <iostream>
#include <filesystem>
#include "zqloader.h"
#include "loader_defaults.h"
namespace fs = std::filesystem;
#ifndef _WIN32
#include <termios.h>
#include <unistd.h> // STDIN_FILENO
int Key()
{
struct termios prev, newatt;
tcgetattr(STDIN_FILENO, &prev);
newatt = prev;
newatt.c_lflag &= ~ECHO;
newatt.c_lflag &= ~ICANON;
tcsetattr(STDIN_FILENO, TCSANOW, &newatt);
int ch = getchar();
tcsetattr(STDIN_FILENO, TCSANOW, &prev);
return ch;
}
#else
int Key()
{
return ::_getch();
}
#endif
void Help()
{
ZQLoader::Version();
std::cout << 1 + &*R"(
This is a turbo loader to load machine code (like games) into a *real* ZX Spectrum at high speed.
This loader is capable of loading a 48K game in about 30 seconds. This time includes the time of
loading the loader itself (which uses traditional ROM loader/speed) plus a splash screen.
For it to work it is needed to connect the audio output of your computer to the ZX Spectrum EAR input.
Set the volume of your computer to maximum. Make sure no other sound is playing.
Then type LOAD "" at the ZX spectrum.
This loader generates the sound pulses, that the ZX Spectrum can load as data.
First it will load a turbo loader program to the ZX Spectrum. Then using that it will load
the second given file with turbo speed.
Syntax:
1) zqloader.exe [options] path/to/filename
2) zqloader.exe [options] path/to/zqloader.tap path/to/turbofile
3) zqloader.exe [options] filename="path/to/zqloader.tap" turbofile="path/to/turbofile" option=value
4) zqloader.exe [options] turbofile="path/to/turbofile" option=value
Arguments:
path/to/filename First file: can be a .tap or .tzx file and will be
loaded at normal speed into a real ZX spectrum.
Only when the file here is 'zqloader.tap' it can
load the second file:
path/to/turbofile Second file, also a .tap or .tzx or a .z80 (snapshot) file.
A game for example.
When given will be send to the ZX spectrum at turbo speed.
1) Loads the single given file at normal speed. (cannot be zqloader.tap);
2) Loads zqloader.tap, then using that loads second given file at turbo speed;
3) Same as 2 but with parameter names and named options in any order.
4) auto finds zqloader.tap; given turbofile file is read at turbo speed.
More options can be given with syntax: option=value, or just 'option value' or option="some value" or
'--option=value' :
volume_left = value
volume_right = value A number between -100 and 100: sets volume for left or right
sound (stereo) channel.
Default 100 (max). A negative value eg -100 inverts this channel.
When both are negative both channels are inverted.
samplerate = value Sample rate for audio. Default 0 meaning take device native sample rate.
For TZX, 0 preserves exact T-states; a positive rate explicitly
matches WAV pulse rounding at that rate (normal polarity).
usescreen or -s When loading a snapshot, normally it will try to find empty space for
the loader. Only when not found it uses the lower 2/3 of screen for
that. With this option it will allways use the screen.
fun_attribs or -f When using screen for a snapshot, giving this parameter overwrites
the loader garbage at screen with a funny text attribute text.
zero_tstates = value
one_tstates = value The number of TStates a zero / one pulse will take when using the
zqloader/turboloader. Not giving this (or 0) uses a default that
worked for me (91/241).
These parameters are used at the host side so directly effect
loading speed.
end_of_byte_delay = value
Extra delay in TSTates after each byte. Eg time needed to store
the byte, calculate checksum etc. Not giving this (or 0) uses a
default that worked for me (which depends on z80 code) (64).
zero_max = value Maximum number of IN's (before an edge is seen) to be considered
a 'zero'.
Minimum value is 1 (need at least one IN to see an edge)
Not giving this (or 0) uses a default that worked for me (3).
This parameter ise used at the ZX-spectrum side so must match
zero_tstates/one_tstates.
Eg extra speed: zero_max=3 one_tstates=243
bit_loop_max = value Maximum number of IN's without an edge seen to be considered a
valid 'one'; above this a timeout error will occur.
Not giving this (or 0) uses a default that worked for me (100)
This value can safely been made larger, does not affect speeds.
outputfile="path/to/filename.wav"
When a wav file is given: write result to given WAV (audio) file instead of
playing sound.
outputfile="path/to/filename.tzx"
When a tzx file given: write result as tzx file instead of playing sound. **
wav or -w Write a wav file as above with same as turbo (2nd) filename but with wav extension.
tzx or -t Write a tzx file as above with same as turbo (2nd) filename but with tzx extension. **
overwrite or -o When given allows overwriting above output file when already exists, else gives
error in that case. Eg: -wo create wav file, overwrite previous one.
key = yes/no/error When done wait for key: yes=always, no=never or only when an error
occurred (which is the default).
**) tzx files is experimental and not fully tested. It uses 'ID 19 - Generalized Data Block' a lot but I
can't find a tool that can actually play it.
)" << std::endl;
// Version();
}
int main(int argc, char** argv)
{
int er = -1;
CommandLine cmdline(argc, argv);
try
{
if (!cmdline.HasParameters())
{
Help();
throw std::runtime_error("Please give a .tap or .tzx filename as runtime argument.");
}
if ( cmdline.HasParameter("help") || cmdline.HasParameter("h"))
{
Help();
return 0;
}
ZQLoader::Version();
std::cout << "This project uses the miniaudio library by David Read. (https://miniaud.io/)" << std::endl;
if (cmdline.HasParameter("version") || cmdline.HasParameter("v"))
{
return 0;
}
// either last parameter if 2 or more parameters
// or turbofile="path/to/file" given
fs::path filename2 = cmdline.GetParameter("turbofile", "");
if (filename2.empty() && cmdline.GetNumParameters() >= 2)
{
filename2 = cmdline.GetLastParameter();
}
// either only parameter (if 1 given) or 2nd to last parameter if 2 or more parameters
// or filename="path/to/file" given
fs::path filename = cmdline.GetParameter("filename", "");
if (filename.empty())
{
filename = (cmdline.GetNumParameters() == 1 && filename2.empty()) ? cmdline.GetParameter(1) :
(cmdline.GetNumParameters() >= 2) ? cmdline.GetParameter(cmdline.GetNumParameters() - 1):
"";
}
ZQLoader zqloader;
zqloader.SetSampleRate(cmdline.GetParameter<uint32_t>("samplerate", 0));
// When outputfile="path/to/filename" or -w or -wav given:
// Convert to wav file instead of outputting sound
zqloader.SetOutputFilename(fs::path(cmdline.GetParameter("outputfile", "")), cmdline.HasParameter("overwrite") || cmdline.HasParameter("o"));
if(cmdline.HasParameter("wav") || cmdline.HasParameter("w"))
{
zqloader.SetAction(ZQLoader::Action::write_wav);
}
else if(cmdline.HasParameter("tzx") || cmdline.HasParameter("t"))
{
zqloader.SetAction(ZQLoader::Action::write_tzx);
}
zqloader.SetBitLoopMax(cmdline.GetParameter<int>("bit_loop_max", 0)).
SetZeroMax(cmdline.GetParameter<int>("zero_max", 0)).
SetDurations(cmdline.GetParameter("zero_tstates", 0),
cmdline.GetParameter("one_tstates", 0),
cmdline.GetParameter("end_of_byte_delay", loader_defaults::end_of_byte_delay));
zqloader.SetVolume(cmdline.GetParameter("volume_left", loader_defaults::volume_left),
cmdline.GetParameter("volume_right", loader_defaults::volume_right));
// zqloader.SetCompressionType(CompressionType::none); // @DEBUG
if(cmdline.HasParameter("usescreen") || cmdline.HasParameter("s"))
{
zqloader.SetLoaderCopyTarget(ZQLoader::LoaderLocation::screen);
}
else
{
zqloader.SetLoaderCopyTarget(cmdline.GetParameter<uint16_t>("new_loader_location", 0));
}
zqloader.SetNormalFilename(filename).SetTurboFilename(filename2);
zqloader.Run();
// std::cout << "Edge = " << spectrumloader.GetLastEdge() << std::endl;
er = 0;
}
catch (const std::exception& e)
{
std::cout << "ERROR: " << e.what() << std::endl;
}
auto key = cmdline.GetParameter("key", "error");
if( key == "yes" ||
(key == "error" && er < 0))
{
std::cout << "Key..." << std::endl;
Key();
}
return er;
}