File: spectrum.cpp

package info (click to toggle)
pasmo 0.5.3-4
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 892 kB
  • ctags: 1,805
  • sloc: cpp: 8,508; asm: 3,020; sh: 790; makefile: 627
file content (121 lines) | stat: -rw-r--r-- 2,751 bytes parent folder | download | duplicates (8)
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
// spectrum.cpp
// Revision 13-dec-2004


#include "spectrum.h"

#include <sstream>
#include <algorithm>

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.
	};
	//memset (plus3, 0, headsize);
	fill (plus3, plus3 + headsize, byte (0) );
	//memcpy (plus3, ident, sizeof (ident) );
	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 std::string spectrum::tokNumPrefix (1, '\x0E');
const std::string spectrum::tokEndLine   (1, '\x0D');
const std::string spectrum::tokCODE      (1, '\xAF');
const std::string spectrum::tokUSR       (1, '\xC0');
const std::string spectrum::tokLOAD      (1, '\xEF');
const std::string spectrum::tokPOKE      (1, '\xF4');
const std::string spectrum::tokRANDOMIZE (1, '\xF9');
const std::string spectrum::tokCLEAR     (1, '\xFD');

std::string spectrum::number (address n)
{
	std::ostringstream oss;
	oss << n;
	std::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;
}

std::string spectrum::linenumber (address n)
{
	std::string str (1, hibyte (n) );
	str+= lobyte (n);
	return str;
}

std::string spectrum::linelength (address n)
{
	std::string str (1, lobyte (n) );
	str+= hibyte (n);
	return str;
}

std::string spectrum::basicline (address linenum, const std::string & line)
{
	std::string result (linenumber (linenum) );
	result+= linelength (static_cast <address> (line.size () ) + 1);
	result+= line;
	result+= tokEndLine;
	return result;
}


// End of spectrum.cpp