облом с microW8

 Давно собирался разобраться с этой виртуальной консолью, но никак не доходили руки - эта поделка собрана для 64бит. Сейчас сижу и проклинаю это угребище. Часа два разбирался с кодом:


Верх идиотизма - это приведение чисел к явному типу. Все попытки привели к


Ладно, нашел способ угомонить консоль:


Остался последний шаг - анимация. Анимация так и не заработала. Хочется выматериться и помянуть автора добрыми словами. Оставлю говнокод на память:

include "include/microw8-api.cwa"
global mut aa:i32=0;
export fn upd() {
// cls(0);
aa=aa+1;
let an=aa as f32;
let x=0 as i32;
let x0 = 16+10*cos(an) as i32;
let y0 = 12+10*sin(an) as i32;
let x1 = 160+80*cos(90.5-an) as i32;
let y1 = 120+80*sin(-an) as i32;
    loop lpx {
let y=0 as i32;
    loop lpy {
let c=(x-x0)*(x-x0)+(y-y0)*(y-y0) as i32;
        let d  = sqrt(c as f32);
let c=(d as i32)&8;
let cc=(x-x1)*(x-x1)+(y-y1)*(y-y1) as i32;
        let dd  = sqrt(cc as f32);
let cc=(dd as i32)&8;
// if c>0 {c:=0x7f7f7f}
setPixel(x,y,c);
branch_if (y:=y+1)<240:lpy;
    }
branch_if (x:=x+1)<320:lpx;
}
setPixel(x0,y0,0x7f7f7f);
}

Дополнение. Ничего нового соратники не сказали, стал копаться - как считается синусоида? Никак, строится прямая линия. После просмотра примеров получилось нечто:

include "include/microw8-api.cwa"
export fn upd() {
let x=0;
    loop pixls {
let lazy an=x as f32 *0.1;
let lazy y=sin(an)*10 as f32;
setPixel(x,120+y as i32,0x7f7f7f);

branch_if (x := x + 1)<320 : pixls;
}
}

Вернулся к начальному коду;

include "include/microw8-api.cwa"

//global mut aa:i32=0;
export fn upd() {
// cls(0);

let an=time() as f32;
let x=0 as i32;
let lazy x0 = cos(an)*160 as f32 +160 as f32;
let lazy y0 = sin(an)*120 as f32 +120 as f32;
let lazy x1 = cos(an*3 as f32)*160 as f32 +160 as f32;
let lazy y1 = sin(an*3 as f32)*120 as f32 +120 as f32;

    loop lpx {
let y=0 as i32;
    loop lpy {
let c=(x as f32 -x0)*(x as f32 -x0)+(y as f32 -y0)*(y as f32 -y0) as f32;
        let d  = sqrt(c);
let c=(d as i32)&8;

let cc=(x as f32-x1)*(x as f32-x1)+(y as f32-y1)*(y as f32-y1) as f32;
        let dd  = sqrt(cc);
let cc=(dd as i32)&8;

// if c>0 {c:=0x7f}
setPixel(x,y,c^cc);
branch_if (y:=y+1)<240:lpy;
    }
branch_if (x:=x+1)<320:lpx;
}
}

Дополнение2. В качестве тренировки написал программу

//https://exoticorn.github.io/microw8/docs/#uw8-run

include "include/microw8-api.cwa"
export fn upd() {
let co=8 as f32;
let c=0 as i32;
let x=0 as f32;
    loop lpx {
let y=0 as f32;
    loop lpy {
//rectangle(x: f32, y: f32, w: f32, h: f32, color: i32)
rectangle(16 as f32+y as f32*co*2 as f32, 10 as f32+x as f32*co, co*2 as f32, co, c );
setTextColor(0xBF as i32);
setCursorPosition(3 as i32 + x as i32 * 2 as i32, 0 as i32);
printString(0x20000+2 as i32 * x as i32);

setCursorPosition(1 as i32, x as i32 + 1 as i32);
printString(0x20000+2 as i32 * x as i32);

c:=c+1;

branch_if (y:=y+1 as f32)<16 as f32:lpy;
    }
branch_if (x:=x+1 as f32)<16 as f32:lpx;
}

}

data 0x20000 {
i8(
 0x30,0,
 0x31,0,
 0x32,0,
 0x33,0,
 0x34,0,
 0x35,0,
 0x36,0,
 0x37,0,
 0x38,0,
 0x39,0,

 0x41,0,
 0x42,0,
 0x43,0,
 0x44,0,
 0x45,0,
 0x46,0)
}

Здесь по адресу $20000 расположены значения шестнадцатиричных чисел, которые используются для печати. Координаты setCursorPosition умножаются на 8.

Дополнение3. Написал простую программу для дампа памяти

include "include/microw8-api.cwa"
export fn upd()
{
 cls(0);
 let i:i32=0;
 let c:i32=0xbf;
// let m:i32=0x20004;//0x13000;
    loop lpi {
//To read from memory you specify a memory location as `base?offset`
//`base` can be any expression that evaluates to an `i32` while `offset` has to be a constant `i32` value.
//The effective memory address is the sum of both.
 0?0x20000 = (((i as i32?0x13000)>>4)&15)?0x20004;
 0?0x20001 = ((i as i32?0x13000)&15)?0x20004;

 setTextColor(c);
 c:=c^0x20 as i32;
  printString(0x20000);
//  printChar(0x2c);
branch_if (i:=i+1 as i32)<16 as i32:lpi;
    }

}

data 0x20000 {
i8(
 0,0,0,0,
 0x30,
 0x31,
 0x32,
 0x33,
 0x34,
 0x35,
 0x36,
 0x37,
 0x38,
 0x39,

 0x41,
 0x42,
 0x43,
 0x44,
 0x45,
 0x46)
}

Дополнение4. Офигенный был квест - какого хера не работает условие if?

//https://exoticorn.github.io/microw8/docs/#uw8-run
global mut x0:i32=160;
global mut y0:i32=120;

global mut dx0:i32=-1;
include "include/microw8-api.cwa"
export fn upd()
{
    let i:i32=0;
let an:f32=time();

let lazy x0 = cos(an)*80 as f32 +160 as f32;
let lazy y0 = sin(an)*80 as f32 +120 as f32;
let lazy x1 = cos(an*3 as f32)*40 as f32 +x0;
let lazy y1 = sin(an*3 as f32)*40 as f32 +y0;


    loop pixels {
        let lazy x = (i % 320) as f32;
        let lazy y = (i / 320) as f32;

let r=127 as f32 *pow(x-x0 as f32,2 as f32)/(pow(x-x0 as f32,2 as f32)+pow(y-y0 as f32,2 as f32) +1 as f32);
let rr=127 as f32 *pow(x-x1 as f32,2 as f32)/(pow(x-x1 as f32,2 as f32)+pow(y-y1 as f32,2 as f32) +1 as f32);
let rr:i32=(r as i32+rr as i32)>>4 as i32;
i?FRAMEBUFFER = rr&15;
        branch_if (i := i + 1) < 320*240: pixels;
    }

}
Дополнение5. Этот код заработал с трудом.
include "include/microw8-api.cwa"
export fn upd()
{
    let i:i32=0;
    let x0:f32;
    let y0:f32;

   let x:f32;
   let y:f32;

let an:f32=time();
cls(0);
    loop pixels {
let ra=50 as f32 +10 as f32*sin(7 as f32*i as f32 *0.01745 as f32) + 5 as f32*sin(5 as f32*i as f32*0.01745+an*3 as f32);
let x=ra*cos(i as f32*0.01745 as f32 +an)+160 as f32;
let y=ra*sin(i as f32*0.01745 as f32 +an)+120 as f32;
if i==0
{
x0=x;
y0=y;
}
else
{
line(x,y,x0,y0,0xBF as i32);
x0=x;
y0=y;
}
        branch_if (i := i + 1) <= 360: pixels;
    }
}

Комментарии