-
Notifications
You must be signed in to change notification settings - Fork 3
/
spectrum.cpp
124 lines (97 loc) · 2.57 KB
/
spectrum.cpp
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
// spectrum.cpp
// Revision 11-aug-2005
#include "spectrum.h"
#include <sstream>
#include <algorithm>
namespace pasmo {
using std::fill;
using std::copy;
spectrum::Plus3Head::Plus3Head ()
{
static const byte ident []= {
'P', 'L', 'U', 'S', '3', 'D', 'O', 'S', // Identifier.
0x1A, // CP/M EOF.
1, // Issue number.
0, // Version number.
};
fill (plus3, plus3 + headsize, byte (0) );
copy (ident, ident + sizeof (ident), plus3);
plus3 [15]= 3; // Type: code.
plus3 [20]= 0x80; // Don't know if used for something.
plus3 [21]= 0x80;
}
void spectrum::Plus3Head::setsize (address size)
{
size_t filesize= size + 128;
// Size of file including code and header.
plus3 [11]= filesize & 0xFF;
plus3 [12]= (filesize >> 8) & 0xFF;
plus3 [13]= (filesize >> 16) & 0xFF;
plus3 [14]= (filesize >> 24) & 0xFF;
// Size of code.
plus3 [16]= lobyte (size);
plus3 [17]= hibyte (size);
}
void spectrum::Plus3Head::setstart (address start)
{
// Start address.
plus3 [18]= lobyte (start);
plus3 [19]= hibyte (start);
}
void spectrum::Plus3Head::write (std::ostream & out)
{
// Checksum
byte check= 0;
for (int i= 0; i < 127; ++i)
check+= plus3 [i];
plus3 [127]= check;
// Write the header.
out.write (reinterpret_cast <char *> (plus3), headsize);
}
// Spectrum Basic generation.
const string spectrum::tokNumPrefix (1, '\x0E');
const string spectrum::tokEndLine (1, '\x0D');
const string spectrum::tokCODE (1, '\xAF');
const string spectrum::tokUSR (1, '\xC0');
const string spectrum::tokLOAD (1, '\xEF');
const string spectrum::tokPOKE (1, '\xF4');
const string spectrum::tokRANDOMIZE (1, '\xF9');
const string spectrum::tokCLEAR (1, '\xFD');
string spectrum::number (address n)
{
std::ostringstream oss;
oss << n;
string str (oss.str () );
str+= tokNumPrefix;
// Special format of Spectrum numbers for integers.
str+= '\x00';
str+= '\x00';
//str+= static_cast <unsigned char> (n & 0xFF);
//str+= static_cast <unsigned char> (n >> 8);
str+= static_cast <unsigned char> (lobyte (n) );
str+= static_cast <unsigned char> (hibyte (n) );
str+= '\x00';
return str;
}
string spectrum::linenumber (address n)
{
string str (1, hibyte (n) );
str+= lobyte (n);
return str;
}
string spectrum::linelength (address n)
{
string str (1, lobyte (n) );
str+= hibyte (n);
return str;
}
string spectrum::basicline (address linenum, const string & line)
{
string result (linenumber (linenum) );
result+= linelength (static_cast <address> (line.size () ) + 1);
result+= line;
result+= tokEndLine;
return result;
}
} // namespace pasmo
// End of spectrum.cpp