mirror of
https://github.com/marcrobledo/RomPatcher.js.git
synced 2025-07-17 16:38:31 +00:00
added support for APS (GBA), fixing #58
This commit is contained in:
parent
0769fae972
commit
1ea536ad27
9 changed files with 147 additions and 133 deletions
133
js/crc.js
133
js/crc.js
|
@ -108,126 +108,19 @@ function adler32(marcFile, offset, len){
|
|||
|
||||
|
||||
|
||||
/* CRC16 */
|
||||
/*
|
||||
const CRC16_TABLE=(function(){
|
||||
var c,crcTable=[];
|
||||
for(var n=0;n<256;n++){
|
||||
c=n;
|
||||
for(var k=0;k<8;k++)
|
||||
c=((c&1)?(0x8408^(c>>>1)):(c>>>1));
|
||||
crcTable[n]=c;
|
||||
/* CRC16/CCITT-FALSE */
|
||||
function crc16(marcFile, offset, len){
|
||||
var crc=0xffff;
|
||||
|
||||
offset=offset? offset : 0;
|
||||
len=len && len>0? len : marcFile.fileSize;
|
||||
|
||||
for(var i=0; i<len; i++){
|
||||
crc ^= marcFile._u8array[offset++] << 8;
|
||||
for (j=0; j<8; ++j) {
|
||||
crc = (crc & 0x8000) >>> 0 ? (crc << 1) ^ 0x1021 : crc << 1;
|
||||
}
|
||||
}
|
||||
return crcTable;
|
||||
}());
|
||||
function crc16(marcFile){
|
||||
var crc=0^(-1);
|
||||
|
||||
for(var i=0;i<marcFile._u8array.length;i++)
|
||||
crc=((crc>>>8)&0x0ff)^CRC16_TABLE[(crc^marcFile._u8array[i])&0xff];
|
||||
|
||||
return ((crc^(-1))>>>0) & 0xffff;
|
||||
return crc & 0xffff;
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/* specific ROM checksums */
|
||||
/* this is unused code, might be used in a future so ROM checksums can be fixed after patching */
|
||||
const CONSOLES=[
|
||||
{
|
||||
title:'Sega Mega Drive/Genesis',
|
||||
MEGADRIVE_LOGO:[0x53, 0x45, 0x47, 0x41, 0x20, 0x4d, 0x45, 0x47, 0x41, 0x20, 0x44, 0x52],
|
||||
GENESIS_LOGO:[0x53, 0x45, 0x47, 0x41, 0x20, 0x47, 0x45, 0x4e, 0x45, 0x53, 0x49, 0x53],
|
||||
checkHeader:function(marcFile){
|
||||
var megadrive=true;
|
||||
var genesis=true;
|
||||
for(var i=0; i<12 && (megadrive || genesis); i++){
|
||||
if(marcFile._u8array[0x100+i]!==this.MEGADRIVE_LOGO[i])
|
||||
megadrive=false;
|
||||
if(marcFile._u8array[0x100+i]!==this.GENESIS_LOGO[i])
|
||||
genesis=false;
|
||||
}
|
||||
return megadrive || genesis;
|
||||
},
|
||||
getChecksum:function(marcFile){
|
||||
return (marcFile._u8array[0x018e]<<8) + marcFile._u8array[0x018f];
|
||||
},
|
||||
recalculateChecksum:function(marcFile){
|
||||
var checksum=0;
|
||||
|
||||
for(var i=0x200; i<marcFile.fileSize; i+=2)
|
||||
checksum=(checksum + (((marcFile._u8array[i]<<8) + marcFile._u8array[i+1])>>>0)) & 0xffff;
|
||||
|
||||
return checksum
|
||||
},
|
||||
updateChecksum:function(marcFile, newChecksum){
|
||||
marcFile._u8array[0x18e]=newChecksum>>8;
|
||||
marcFile._u8array[0x18f]=newChecksum & 0xff;
|
||||
}
|
||||
},{
|
||||
title:'Game Boy',
|
||||
NINTENDO_LOGO:[0xce, 0xed, 0x66, 0x66, 0xcc, 0x0d, 0x00, 0x0b, 0x03, 0x73, 0x00, 0x83, 0x00, 0x0c, 0x00, 0x0d],
|
||||
checkHeader:function(marcFile){
|
||||
for(var i=0; i<this.NINTENDO_LOGO.length; i++){
|
||||
if(marcFile._u8array[0x104+i]!==this.NINTENDO_LOGO[i])
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
},
|
||||
getChecksum:function(marcFile){
|
||||
return marcFile._u8array[0x14d]
|
||||
},
|
||||
recalculateChecksum:function(marcFile){
|
||||
var checksum=0;
|
||||
|
||||
for(var i=0x134; i<0x014d; i++){
|
||||
checksum=(checksum - marcFile._u8array[i] - 1) & 0xff;
|
||||
}
|
||||
|
||||
return checksum
|
||||
},
|
||||
updateChecksum:function(marcFile, newChecksum){
|
||||
marcFile._u8array[0x014d]=newChecksum;
|
||||
|
||||
|
||||
/* global checksum isn't checked by real hw, but fix it anyway */
|
||||
var globalChecksumOld=(marcFile._u8array[0x014e]<<8) + marcFile._u8array[0x014f];
|
||||
var globalChecksumNew=0;
|
||||
for(var i=0x0000; i<0x014e; i++)
|
||||
globalChecksumNew=((globalChecksumNew + marcFile._u8array[i]) >>> 0) & 0xffff;
|
||||
for(i=0x0150;i<marcFile.fileSize; i++)
|
||||
globalChecksumNew=((globalChecksumNew + marcFile._u8array[i]) >>> 0) & 0xffff;
|
||||
if(globalChecksumOld!==globalChecksumNew){
|
||||
marcFile._u8array[0x014e]=globalChecksumNew>>8;
|
||||
marcFile._u8array[0x014f]=globalChecksumNew & 0xff;
|
||||
}
|
||||
}
|
||||
}
|
||||
];
|
||||
function checkConsole(marcFile){
|
||||
return false;
|
||||
}
|
||||
function fixConsoleChecksum(marcFile){
|
||||
var system=false;
|
||||
for(var i=0; i<CONSOLES.length && !system; i++)
|
||||
if(CONSOLES[i].checkHeader(marcFile))
|
||||
system=CONSOLES[i];
|
||||
if(!system)
|
||||
return false;
|
||||
|
||||
var oldChecksum=console.getChecksum(marcFile);
|
||||
var newChecksum=console.recalculateChecksum(marcFile);
|
||||
|
||||
if(oldChecksum!==newChecksum)
|
||||
if(alert('Fix '+console.title+' checksum?')){
|
||||
console.updateChecksum(marcFile, newChecksum);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue