<?
$scr = file_get_contents(__DIR__ . "/scr/gfx-duo-to-twister-fixed-zx-4.scr");
$bits = '';

for ($y = 0; $y < 6; $y++) {
	for ($x = 0; $x < 256; $x++) {
		$adr = $y * 4 * 256 + $x;
		$bits .= substr($scr, $adr, 1);
	}
}
file_put_contents(__DIR__ . "/duo-to-twister-bits.bin", $bits);

//1536 - экран
$scrColors = array(
    2, 3, 4, 1
);


function getAttr($index, $c) {
    $attrMap = array(
        0x00 =>	array(0x00, 0x00),

        0x42 => array(0x00, 0x40+8*0+$c),    //#42
        0x52 => array(0x00, 0x40+8*$c+$c),    //#52
        
        0x62 => array(8*$c+0, 0x40+8*7+$c),  //#7A
        0x64 => array(8*$c+$c, 0x40+8*7+7),  //#7F
        0x66 => array(8*$c+7, 0x40+8*7+7),  //#7F
        
        0x76 => array(8*7+7, 0x40+8*7+7),
        0x46 => array(8*0+7, 0x40+8*0+7),
        
        0x56 => array(8*0+7, 0x40+8*$c+7),
    );
    return $attrMap[$index];
}
$scrPrev = false;
$attr0 = array();
$attr1 = array();

for ($i = 1; $i <= 4; $i++) {
	$scr = file_get_contents(__DIR__ . "/scr/gfx-duo-to-twister-fixed-zx-" . $i . ".scr");
    $c = $scrColors[$i-1];
	for ($adr = 6144; $adr < 6912; $adr++) {
		$attr = ord(substr($scr, $adr, 1));
		$attrPrev = $scrPrev===false ? -1 : ord(substr($scrPrev, $adr, 1));
		if ($attr != $attrPrev) {
            $ccc = getAttr($attr, $c);
			$attr0[$adr-6144] = chr($ccc[0]);
			$attr1[$adr-6144] = chr($ccc[1]);
		}
	}
	$scrPrev = $scr;
	file_put_contents(__DIR__ . "/duo-to-twister-attr." . sprintf('%03d',$i*2), implode('', $attr0));
	file_put_contents(__DIR__ . "/duo-to-twister-attr." . sprintf('%03d',$i*2+1), implode('', $attr1));
}
