-
Notifications
You must be signed in to change notification settings - Fork 6
/
SidFile.cpp
executable file
·151 lines (116 loc) · 2.53 KB
/
SidFile.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
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
#include "SidFile.h"
SidFile::SidFile()
{
}
uint16_t SidFile::Read16(const uint8_t *p, int offset)
{
return (p[offset] << 8) | p[offset + 1];
}
uint32_t SidFile::Read32(const uint8_t *p, int offset)
{
return (p[offset] << 24) | (p[offset + 1] << 16) | (p[offset + 2] << 8) | p[offset + 3];
}
bool SidFile::IsPSIDHeader(const uint8_t *p)
{
uint32_t id = Read32(p, SIDFILE_PSID_ID);
uint16_t version = Read16(p, SIDFILE_PSID_VERSION);
return id == 0x50534944 && (version == 1 || version == 2);
}
int SidFile::Parse(string file)
{
FILE *f = fopen(file.c_str(), "rb");
if(f == NULL)
{
return SIDFILE_ERROR_FILENOTFOUND;
}
uint8_t header[PSID_MAX_HEADER_LENGTH];
memset(header, 0, PSID_MAX_HEADER_LENGTH);
size_t read = fread(header, 1, PSID_MAX_HEADER_LENGTH, f);
if(read < PSID_MIN_HEADER_LENGTH || !IsPSIDHeader(header))
{
fclose(f);
return SIDFILE_ERROR_MALFORMED;
}
numOfSongs = Read16(header, SIDFILE_PSID_NUMBER);
if(numOfSongs == 0)
{
numOfSongs = 1;
}
firstSong = Read16(header, SIDFILE_PSID_DEFSONG);
if(firstSong)
{
firstSong--;
}
if (firstSong >= numOfSongs)
{
firstSong = 0;
}
initAddr = Read16(header, SIDFILE_PSID_INIT);
playAddr = Read16(header, SIDFILE_PSID_MAIN);
speedFlags = Read32(header, SIDFILE_PSID_SPEED);
moduleName = (char *)(header + SIDFILE_PSID_NAME);
authorName = (char *)(header + SIDFILE_PSID_AUTHOR);
string copyrightInfo = (char *)(header + SIDFILE_PSID_COPYRIGHT);
// Seek to start of module data
fseek(f, Read16(header, SIDFILE_PSID_LENGTH), SEEK_SET);
// Find load address
loadAddr = Read16(header, SIDFILE_PSID_START);
if(loadAddr == 0)
{
uint8_t lo = fgetc(f);
uint8_t hi = fgetc(f);
loadAddr = (hi << 8) | lo;
}
if(initAddr == 0)
{
initAddr = loadAddr;
}
// Load module data
dataLength = fread(dataBuffer, 1, 0x10000, f);
fclose(f);
return SIDFILE_OK;
}
string SidFile::GetModuleName()
{
return moduleName;
}
string SidFile::GetAuthorName()
{
return authorName;
}
string SidFile::GetCopyrightInfo()
{
return copyrightInfo;
}
int SidFile::GetSongSpeed(int songNum)
{
return (speedFlags & (1 << songNum)) ? SIDFILE_SPEED_60HZ : SIDFILE_SPEED_50HZ;
}
int SidFile::GetNumOfSongs()
{
return numOfSongs;
}
int SidFile::GetFirstSong()
{
return firstSong;
}
uint8_t *SidFile::GetDataPtr()
{
return dataBuffer;
}
uint16_t SidFile::GetDataLength()
{
return dataLength;
}
uint16_t SidFile::GetLoadAddress()
{
return loadAddr;
}
uint16_t SidFile::GetInitAddress()
{
return initAddr;
}
uint16_t SidFile::GetPlayAddress()
{
return playAddr;
}