num RPM_TABLE			= 0x7C68;		// Self explanetory
num RPMHI_TABLE			= 0x7C7C;		// vars with "_boost_" in their name are the new locations
num IGN_TABLE			= 0x7e0c;
num IGNHI_TABLE			= 0x7ED4;
num IGN_BOOST_TABLE		= 0x6B90;
num IGNHI_BOOST_TABLE	= 0x7060;
num FUEL_TABLE			= 0x7C9A;
num FUEL_BOOST_TABLE	= 0x6770;
num FUELHI_BOOST_TABLE	= 0x71F0;
num FUELHI_TABLE		= 0x7D6C;

num MBAR_TABLE			= 0x7C4A;		// map scaler table address
num TABLELO_X			= 10;			// dimentions of stock tables
num TABLELO_Y			= 20;			//
num TABLEHI_X			= 10;			//
num TABLEHI_Y			= 15;			//

num NEW_TABLELO_X			= 15;		// dimentions to convert respective tables to
num NEW_TABLELO_Y			= 20;		//
num NEW_TABLEHI_X			= 15;		//
num NEW_TABLEHI_Y			= 15;		//

num LAST_MAP_VALUE		= 0x85;			// map value to assign to the last n/a map (column 9)
num VERSION				= 5;			// current uberdata version

num BOOST_FUEL_FLOAT	= 0x7A00;		// location to save editor used variable for boost fuel efficiency
num BOOST_IGN_FLOAT		= 0x7A04;		// location to save editor used variable for ign retard per lbs
num VERSION_BYTE		= 0x7A08;		// location in rom used to store uberdata version
num MAPSENSOR_FLOAT		= 0x7A09;		// location in rom to save current map sensor type

num hBoostFuel			= -1;			// variables to store window handles
num hBoostIgn			= -1;			//


void DisableCRC()
{
	writebyte(0x2a1d+0, 0x03);
	writebyte(0x2a1d+1, 0x2a);
	writebyte(0x2a1d+2, 0x2a);
	writebyte(0x2a1d+3, 0x00);
}

void setlofuelx(num sz)					// sets width of low-cam fuel table in the rom
{
	writebyte(0x1267, sz);
	writebyte(0x0AA9, sz-2);
	writebyte(0x0ABE, sz-2);
	writebyte(0x0AD3, sz-2);
}

num getlofuelx()						// gets width of low-cam fuel table in the rom
{
	return readbyte(0x1267);
}

void setlofuely(num sz)					// sets height of low-cam fuel table in the rom
{
	writebyte(0x1283, sz);
	writebyte(0x0A59, sz-2);
}

num getlofuely()						// gets height of low-cam fuel table in the rom
{
	return readbyte(0x1283);
}

void sethifuelx(num sz)					// sets width of hi-cam fuel table in the rom
{
	writebyte(0x1267, sz);
	writebyte(0x0AA9, sz-2);
	writebyte(0x0ABE, sz-2);
	writebyte(0x0AD3, sz-2);
}

num gethifuelx()						// gets width of hi-cam fuel table in the rom
{
	return readbyte(0x1267);
}

void sethifuely(num sz)					// sets height of hi-cam fuel table in the rom
{
	writebyte(0x1269, sz);
	writebyte(0x0A71, sz-2);
}

num gethifuely()						// gets height of hi-cam fuel table in the rom
{
	return readbyte(0x1269);
}

void setloignx(num sz)					// sets width of lo-cam ign table in the rom
{
	writebyte(0x0B86, sz);
	writebyte(0x0AA9, sz-2);
	writebyte(0x0ABE, sz-2);
	writebyte(0x0AD3, sz-2);
}

num getloignx()							// gets width of lo-cam ign table in the rom
{
	return readbyte(0x0B86);
}

void setloigny(num sz)					// sets height of lo-cam ign table in the rom
{
	writebyte(0x0B88, sz);
	writebyte(0x0A59, sz-2);
}

num getloigny()							// gets height of lo-cam ign table in the rom
{
	return readbyte(0x0B88);
}

void sethiignx(num sz)					// sets width of hi-cam ign table in the rom
{
	writebyte(0x0B86, sz);
	writebyte(0x0AA9, sz-2);
	writebyte(0x0ABE, sz-2);
	writebyte(0x0AD3, sz-2);
}

num gethiignx()							// gets width of hi-cam ign table in the rom
{
	return readbyte(0x0B86);
}

void sethiigny(num sz)					// sets height of hi-cam ign table in the rom
{
	writebyte(0x0BA2, sz);
	writebyte(0x0A71, sz-2);
}

num gethiigny()							// gets height of hi-cam ign table in the rom
{
	return readbyte(0x0BA2);
}

void setlofuel(num addr)				// sets the location of the low-cam fuel table in the rom
{
	writeword(0x128B, addr);
}

num getlofuel()							// gets the location of the low-cam fuel table in the rom
{
	return readword(0x128B);
}

void sethifuel(num addr)				// sets the location of the hi-cam fuel table in the rom
{
	writeword(0x1277, addr);
}

num gethifuel()							// gets the location of the hi-cam fuel table in the rom
{
	return readword(0x1277);
}

void setloign(num addr)					// sets the location of the low-cam ign table in the rom
{
	writeword(0x64DF, addr);
}

num getloign()							// gets the location of the low-cam ign table in the rom
{
	return readword(0x64DF);
}

void sethiign(num addr)					// sets the location of the hi-cam ign table in the rom
{
	writeword(0x64E9, addr);
}

num gethiign()							// gets the location of the hi-cam ign table in the rom
{
	return readword(0x64E9);
}

void setlorpm(num addr)					// sets the location of the lo-cam RPM table in the rom
{
	writeword(0x0A50, addr);
}

num getlorpm()							// gets the location of the lo-cam RPM table in the rom
{
	return readword(0xA50);
}

void sethirpm(num addr)					// sets the location of the hi-cam RPM table in the rom
{
	writeword(0x0A68, addr);
}

num gethirpm()							// gets the location of the hi-cam RPM table in the rom
{
	return readword(0x0A68);
}

num setmapscl(num addr)					// sets the location of the map scaler table in the rom
{
	writeword(0x0AA3, addr);
	writeword(0x0AB8, addr);
	writeword(0x0ACD, addr);
}

num getmapscl()							// gets the location of the map scaler table in the rom
{
	return MBAR_TABLE;
}

void disablemapcheck()					// removes #A1 map check
{
	writebyte(0x0848, 0);
	writebyte(0x0849, 0);
}

void erasemap(num addr, num sz)
{
	num i = 0;
	num new_addr;
	while(i < sz)
	{
		new_addr = addr + i;
		writebyte(new_addr, 0xFF);
		i = i + 1;
	}
}

void copymap(num src_map, num sx, num sy, num dest_map, num dx, num dy) // copies and resizes from one map to another
{
	num x = 0;
	while( x < sx)
	{
		num y = 0;
		while(y < sy)
		{
			num src_byte = readbyte(src_map+(y*sx)+x);
			num dest_addr = dest_map+(y*dx)+x;
			writebyte(dest_addr, src_byte);
			y = y + 1;
		}
		x = x + 1;
	}
}

num scalemap(num map, num scale, num len)		// scales map table by the specified amount
{
	num x;
	x = readbyte(map+0) * scale;
	writebyte(map+0, x);
	x = readbyte(map+1) * scale;
	writebyte(map+1, x);
	x = readbyte(map+2) * scale;
	writebyte(map+2, x);
	x = readbyte(map+3) * scale;
	writebyte(map+3, x);
	x = readbyte(map+4) * scale;
	writebyte(map+4, x);
	x = readbyte(map+5) * scale;
	writebyte(map+5, x);
	x = readbyte(map+6) * scale;
	writebyte(map+6, x);
	x = readbyte(map+7) * scale;
	writebyte(map+7, x);
	x = readbyte(map+8) * scale;
	writebyte(map+8, x);
	x = readbyte(map+9) * scale;
	writebyte(map+9, x);
}

void convert175bar()								// converts rom to 2bar rom
{
	writebyte(0x4DD2, 1);
	scalemap(MBAR_TABLE, (readfloat(MAPSENSOR_FLOAT) / 1.75), 10);
	writefloat(MAPSENSOR_FLOAT, 1.75);
	genextramap();
}

void convert2bar()								// converts rom to 2bar rom
{
	writebyte(0x4DD2, 1);
	scalemap(MBAR_TABLE, (readfloat(MAPSENSOR_FLOAT) / 2), 10);
	writefloat(MAPSENSOR_FLOAT, 2);
	genextramap();
}

void convert25bar()								// converts rom to 2bar rom
{
	writebyte(0x4DD2, 1);
	scalemap(MBAR_TABLE, (readfloat(MAPSENSOR_FLOAT) / 2.5), 10);
	writefloat(MAPSENSOR_FLOAT, 2.5);
	genextramap();
}

void convert3bar()								// converts rom to 3bar rom
{
	writebyte(0x4DD2, 1);
	scalemap(MBAR_TABLE, (readfloat(MAPSENSOR_FLOAT) / 3), 10);
	writefloat(MAPSENSOR_FLOAT, 3);
	genextramap();
}


void genextramap()								// generates new map table values for the extra portion of the map
{
	num i;
	i = 10;
	while(i < NEW_TABLELO_X)
	{
		num x = (i - 10) + 1;
		num xmax = NEW_TABLELO_X - 10;
		num scale = x / xmax;
		num min = LAST_MAP_VALUE * 1.75 / readfloat(MAPSENSOR_FLOAT);
		num max = 255.0f;
		num final = (scale * (max-min))+min;
		writebyte(MBAR_TABLE + i, final);
		i = i + 1;
	}
	writebyte(MBAR_TABLE + 9, LAST_MAP_VALUE * 1.75 / readfloat(MAPSENSOR_FLOAT));
}

num getmapsen()									// retrieves current map sensor, called outside of script
{
	return readfloat(MAPSENSOR_FLOAT);
}

num open()										// called when a ROM is opened
{
	num version = readbyte(VERSION_BYTE);
	if(version == 0xFF)
	{
		DisableCRC();
		writefloat(MAPSENSOR_FLOAT, 1.75)
		copymap(IGN_TABLE, TABLELO_X, TABLELO_Y+0, IGN_BOOST_TABLE, NEW_TABLELO_X, NEW_TABLELO_Y);
		copymap(IGNHI_TABLE, TABLEHI_X, TABLEHI_Y, IGNHI_BOOST_TABLE, NEW_TABLEHI_X, NEW_TABLEHI_Y);

		copymap(FUEL_TABLE, TABLELO_X, TABLELO_Y+1, FUEL_BOOST_TABLE, NEW_TABLELO_X, NEW_TABLELO_Y+1);
		copymap(FUELHI_TABLE, TABLEHI_X, TABLEHI_Y+1, FUELHI_BOOST_TABLE, NEW_TABLEHI_X, NEW_TABLEHI_Y+1);

		setloignx(NEW_TABLELO_X);
		sethiignx(NEW_TABLEHI_X);

		setloigny(NEW_TABLELO_Y);
		sethiigny(NEW_TABLEHI_Y);

		setlofuelx(NEW_TABLELO_X);
		sethifuelx(NEW_TABLEHI_X);

		setlofuely(NEW_TABLELO_Y);
		sethifuely(NEW_TABLEHI_Y);

		setlofuel(FUEL_BOOST_TABLE);
		sethifuel(FUELHI_BOOST_TABLE);

		setloign(IGN_BOOST_TABLE);
		sethiign(IGNHI_BOOST_TABLE);

		writebyte(0x4DD2, 1);
		scalemap(MBAR_TABLE, 0.5, 9);
		disablemapcheck();
		genextramap();

		writefloat(BOOST_FUEL_FLOAT, 120);
		writefloat(BOOST_IGN_FLOAT, 0.9);
		writebyte(VERSION_BYTE, 5);

		setmapscl(MBAR_TABLE);
	}

	num x = 10;
	num y = 10;


	CreateBorder(x, y, 250, 100, 4, 0, "Tables");
	CreateStatic(x+5, y-5, 150, 20, 4, 0, "Boost Table Funcs");

	CreateStatic(120+x+10, y+20, 110, 20, 4, 0, "Boost Fuel Eff");
	CreateStatic(120+x+10, y+40, 110, 20, 4, 0, "Boost Ign Retard");

	hBoostFuel = CreateEdit(x+10, y+20, 40, 20, 4, 0, "Boost Fuel Eff");
	hBoostIgn  = CreateEdit(x+10, y+40, 40, 20, 4, 0, "Boost Ign Retard");

	SetItem(hBoostFuel, readfloat(BOOST_FUEL_FLOAT));
	SetItem(hBoostIgn, readfloat(BOOST_IGN_FLOAT));

	CreateBtn(40+x+10, y+20, 50, 20, 4, "BoostFuelApply", "Apply");
	CreateBtn(40+x+10, y+40, 50, 20, 4, "BoostIgnApply", "Apply");

	y = y + 110;
	CreateBorder(x, y, 250, 120, 4, 0, "MapSensor");
	CreateStatic(x+5, y-5, 150, 20, 4, 0, "Map Sensor");

	CreateRadio(x+10, y+20, 40, 20, 4, "convert175bar", "1.75 Bar");
	CreateRadio(x+10, y+40, 40, 20, 4, "convert2bar", "2.00 Bar");
	CreateRadio(x+10, y+60, 40, 20, 4, "convert25bar", "2.50 Bar");
	CreateRadio(x+10, y+80, 40, 20, 4, "convert3bar", "3.00 Bar");

}

num save()									// called right before a rom is saved
{
}

num close()									// called when a rom is closed
{
}

num BoostFuelApply()						// callback for "Boost Fuel Eff" window
{
	num mult = GetItem(hBoostFuel);
	SetBoostFuelMaps(mult);
	writefloat(BOOST_FUEL_FLOAT, mult);
}

num BoostIgnApply()							// callback for "Boost Ign Retard" window
{
	num mult = GetItem(hBoostIgn);
	SetIgnitionRetard(mult);
	writefloat(BOOST_IGN_FLOAT, mult);
}