-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsystem_SMS.cpp
More file actions
115 lines (94 loc) · 3.71 KB
/
system_SMS.cpp
File metadata and controls
115 lines (94 loc) · 3.71 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
/*MASTER TILE CONVERTER
Copyright (C) 2012 JM Muller
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "system_SMS.h"
#include <iostream>
#include "rom.h"
System_SMS::System_SMS():System()
{
}
bool System_SMS::check_ROM()
{
char * signature=(char*)"TMR SEGA";
for (int i=0;i<8;i++)
if (m_rom->get_romdata()[HEADER_ADDRESS+i]!=signature[i])
{
std::cout<<"\"TMR SEGA\" test failed."<<std::endl;
return false;
}
std::cout<<"\"TMR SEGA\" test passed!"<<std::endl;
return true;
}
unsigned short System_SMS::read_checksum()
{
unsigned short checksum=0;
checksum=m_rom->get_romdata()[CHECKSUM_ADDRESS+1];
checksum=checksum<<8;
checksum+=m_rom->get_romdata()[CHECKSUM_ADDRESS];
m_read_checksum=checksum;
return checksum;
}
void System_SMS::read_region_and_size()
{
m_region=m_rom->get_romdata()[REGION_AND_SIZE_ADDRESS]>>4;
m_rom_size=m_rom->get_romdata()[REGION_AND_SIZE_ADDRESS]%0x10;
std::cout<<"Region code: "<<std::hex<<(int)m_region<<"; size code: "<<std::hex<<(int)m_rom_size<<std::endl;
}
//Code taken from:
//---------------------------------------------------------------------------
// SMS Check (c) Omar Cornut (Bock) 2000-2006
// Sega 8-bit ROM tool, showing checksum and various informations
//---------------------------------------------------------------------------
unsigned short System_SMS::compute_checksum()
{
read_region_and_size();
long checksum_len;
switch (m_rom_size)
{
case 0x0A: checksum_len = 0x02000; break;
case 0x0B: checksum_len = 0x04000; break;
case 0x0C: checksum_len = 0x08000; break;
case 0x0D: checksum_len = 0x0C000; break;
case 0x0E: checksum_len = 0x10000; break;
case 0x0F: checksum_len = 0x20000; break;
case 0x00: checksum_len = 0x40000; break;
default: checksum_len = m_rom->get_romlength(); break;
}
m_computed_checksum=0;
if (m_rom->get_romlength() < checksum_len)
{
std::cout<<"File too short for checksum!\n";
}
else
{
// Compute checksum
m_computed_checksum = 0;
for (long i = 0; i < checksum_len; i++)
m_computed_checksum += m_rom->get_romdata()[i];
// Remove header from checksum
if (checksum_len >= 0x8000)
for (long i = 0x8000 - SMS_HEADER_SIZE; i < 0x8000; i++)
m_computed_checksum -= m_rom->get_romdata()[i];
// Print out result
std::cout<<"Computed checksum: "<<std::hex<<m_computed_checksum<<std::endl;
}
return m_computed_checksum;
}
void System_SMS::fix_checksum()
{
compute_checksum();
std::cout<<"Fix checksum:\nreplace 0x"<<std::hex<<(int)m_rom->get_romdata()[CHECKSUM_ADDRESS]<<" by 0x"<<std::hex<<(int)(unsigned char)(m_computed_checksum%0x100)<<std::endl;
std::cout<<"replace 0x"<<std::hex<<(int)m_rom->get_romdata()[CHECKSUM_ADDRESS+1]<<" by 0x"<<std::hex<<(int)(unsigned char)(m_computed_checksum>>8)<<std::endl;
m_rom->get_romdata()[CHECKSUM_ADDRESS] =(unsigned char)(m_computed_checksum%0x100);
m_rom->get_romdata()[CHECKSUM_ADDRESS+1]=(unsigned char)(m_computed_checksum>>8);
}