summaryrefslogtreecommitdiff
path: root/music/pokeredmusicdisasm/Parser.cpp
blob: 9fa0af7f272fbec62c099082b08e61e9c9e67627 (plain)
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
#include <sstream>
#include "Parser.h"
using namespace std;

// Constructors
Parser::Parser()
{
	rawBytes = 0;
	fileLength = 0;
	filePos = 0;
	stop = false;
	stopAddress = 0;
}

Parser::Parser(std::string filename)
{
	rawBytes = 0;
	fileLength = 0;
	filePos = 0;
	stop = false;

	SetFilename(filename);
}

// Deconstructors
Parser::~Parser()
{
	// Clear out temporary buffer
	delete[] rawBytes;

	// Clear out parsed buffer
	for(unsigned int i = 0; i < parsedBytes.size(); i++)
	{
		delete parsedBytes[i];
	}
}

// Getters / Setters
string Parser::GetFilename()
{
	return filename;
}

void Parser::SetFilename(std::string value)
{
	filename = value;
	Read();
}

unsigned int Parser::GetStopAddress()
{
	return stopAddress;
}

void Parser::SetStopAddress(unsigned int value)
{
	stopAddress = value;
}

string Parser::GetParsedAsm()
{
	string tmpStr;

	for(unsigned int i = 0; i < parsedString.size(); i++)
	{
		tmpStr += parsedString[i] + "\n";
	}

	return tmpStr;
}

// File Operations
// Absolutely no error checking at all - likely needs to be done at somepoint
void Parser::Read()
{
	// open File
	fstream tmpFile(filename, ios_base::in | ios_base::binary);

	// Get Length
	tmpFile.seekg(0, ios::end);
	fileLength = tmpFile.tellg();
	tmpFile.seekg(0, ios::beg);

	// Allocate proper memory
	rawBytes = new char[fileLength];

	// Read filedata
	tmpFile.read(rawBytes, fileLength);
	tmpFile.close();
}

// Code Operations
void Parser::Parse(unsigned int offset)
{
	filePos = offset;
	ParseNext();
}

void Parser::ParseNext() // Parses the block immidiately following
{
	stringstream tmpStr;
	unsigned char* rawBytesFixed = (unsigned char*)rawBytes;
	stop = false;

	// Smart generation
	bool indent = false;
	bool firstNonNote = false;	// First byte wasn't a note or octacve switch, add ";Setup" comment
	bool firstNote = false;	// First note or octave

	stringstream pos;
	pos << "; " << hex << uppercase << (unsigned int)filePos;
	parsedString.push_back(pos.str());

	for(unsigned int i = filePos; (i <= fileLength) && (stop == false); i++)
	{
		// There's a way to make this block shorter but for now it does it's job

		// Check to see if it's the correct data type and if so then use it
		if(tmpCall.IsValid(&rawBytesFixed[i])) // Should have made IsValid static
		{
			// Call data type

			// Create data type then move the increment pointer further up as needed
			parsedBytes.push_back(new Call(&rawBytesFixed[i]));
			parsedString.push_back(parsedBytes[parsedBytes.size() - 1]->GenAsm());
			i += tmpCall.Arguments(); // should have made Arguments static

			Call* _tmp = (Call*)parsedBytes[parsedBytes.size() - 1];
		}
		else if(tmpDuty.IsValid(&rawBytesFixed[i]))
		{
			// Duty data type
			parsedBytes.push_back(new Duty(&rawBytesFixed[i]));
			parsedString.push_back(parsedBytes[parsedBytes.size() - 1]->GenAsm());
			i += tmpDuty.Arguments();
		}
		else if(tmpJump.IsValid(&rawBytesFixed[i]))
		{
			// Jump data type
			parsedBytes.push_back(new Jump(&rawBytesFixed[i]));
			parsedString.push_back(parsedBytes[parsedBytes.size() - 1]->GenAsm());
			i += tmpJump.Arguments();

			Jump* _tmp = (Jump*)parsedBytes[parsedBytes.size() - 1];
		}
		else if(tmpModulation.IsValid(&rawBytesFixed[i]))
		{
			// Modulation data type
			parsedBytes.push_back(new Modulation(&rawBytesFixed[i]));
			parsedString.push_back(parsedBytes[parsedBytes.size() - 1]->GenAsm());
			i += tmpModulation.Arguments();
		}
		else if(tmpNote.IsValid(&rawBytesFixed[i]))
		{
			// Note data type
			parsedBytes.push_back(new Note(&rawBytesFixed[i]));
			parsedString.push_back(parsedBytes[parsedBytes.size() - 1]->GenAsm());
			i += tmpNote.Arguments();
		}
		else if(tmpOctave.IsValid(&rawBytesFixed[i]))
		{
			// Octave data type
			parsedBytes.push_back(new Octave(&rawBytesFixed[i]));
			parsedString.push_back("\n" + parsedBytes[parsedBytes.size() - 1]->GenAsm());
			i += tmpOctave.Arguments();
		}
		else if(tmpStop.IsValid(&rawBytesFixed[i]))
		{
			// Stop data type
			parsedBytes.push_back(new Stop(&rawBytesFixed[i]));
			parsedString.push_back(parsedBytes[parsedBytes.size() - 1]->GenAsm());
			i += tmpStop.Arguments();

			stop = true;	// Stop all further processing, we've reached the end of the song
		}
		else if(tmpTempo.IsValid(&rawBytesFixed[i]))
		{
			// Tempo data type
			parsedBytes.push_back(new Tempo(&rawBytesFixed[i]));
			parsedString.push_back(parsedBytes[parsedBytes.size() - 1]->GenAsm());
			i += tmpTempo.Arguments();
		}
		else if(tmpVelocity.IsValid(&rawBytesFixed[i]))
		{
			// Velocity data type
			parsedBytes.push_back(new Velocity(&rawBytesFixed[i]));
			parsedString.push_back(parsedBytes[parsedBytes.size() - 1]->GenAsm());
			i += tmpVelocity.Arguments();
		}
		else if(tmpVolume.IsValid(&rawBytesFixed[i]))
		{
			// Volume data type
			parsedBytes.push_back(new Volume(&rawBytesFixed[i]));
			parsedString.push_back(parsedBytes[parsedBytes.size() - 1]->GenAsm());
			i += tmpVolume.Arguments();
		}
		else
		{
			// Unknown code
			stringstream unkCode;
			short tmpByte = (short)rawBytesFixed[i];
			unkCode << "db $" << hex << uppercase << (short)rawBytesFixed[i];
			parsedString.push_back(unkCode.str());
		}

		filePos = i;

		// If the stop address parameter is set, break when we get there
		if( (stopAddress != 0) && (i >= stopAddress) ) break;
	}

	// Now record the postion we left off
	pos.str("");
	pos << "; " << hex << uppercase << (unsigned int)filePos;
	parsedString.push_back(pos.str());

	filePos += 1;		// increment 1 for the start of the next possible song
}