Basilio/js/Byte.ts

146 lines
4.1 KiB
TypeScript
Raw Normal View History

2015-01-22 21:32:46 +00:00
class Byte {
private value: number;
public constructor(init: number = 0) {
this.value = init & 0xFF | (init < 0 ? 0x80 : 0);
2015-01-22 21:32:46 +00:00
}
public Add(val: Byte): Byte;
public Add(val: number): Byte;
public Add(val: any): Byte {
val = typeof val == "number" ? new Byte(val) : val;
var hold = this.value + val.Get();
this.value = hold & 0xFF;
return new Byte(hold > 255 ? hold - 255 : 0);
2015-01-22 21:32:46 +00:00
}
public Sub(val: Byte): Byte;
public Sub(val: number): Byte;
public Sub(val: any): Byte {
val = typeof val == "number" ? new Byte(val) : val;
return this.Add(val.Neg());
2015-01-22 21:32:46 +00:00
}
public Get(): number {
return this.value;
}
public Set(val: Byte);
public Set(val: number);
public Set(val: any) {
this.value = typeof val == "number" ? val & 0xFF : val.Get();
}
public Neg(bind: boolean = false) {
var tmp = new Byte(~this.value + 1);
this.value = bind ? tmp.Get() : this.value;
return tmp;
}
public And(val: Byte);
public And(val: number);
public And(val: any) {
val = typeof val == "number" ? new Byte(val) : val;
this.value = this.value & val.Get();
}
public Or(val: Byte);
public Or(val: number);
public Or(val: any) {
val = typeof val == "number" ? new Byte(val) : val;
this.value = this.value | val.Get();
}
public Xor(val: Byte);
public Xor(val: number);
public Xor(val: any) {
val = typeof val == "number" ? new Byte(val) : val;
this.value = this.value ^ val.Get();
}
public Not() {
this.value = ~this.value;
}
public Bit(pos: number, val: boolean) {
pos = pos % 8;
this.Set(val ? (this.value | Math.pow(2, pos)) : (this.value & ~Math.pow(2, pos)));
}
public Test(pos: number): boolean {
2015-01-23 19:46:23 +00:00
pos = pos % 8;
return (this.value & Math.pow(2, pos)) != 0;
}
public ShiftLeft(carry: boolean = false, value: boolean = false): boolean {
var ret = (this.value & 0x80) != 0;
this.Set(this.value << 1);
this.Bit(0, carry ? ret : value);
return ret;
}
public ShiftRight(carry: boolean = false, value: boolean = false): boolean {
var ret = (this.value & 0x80) != 0;
this.Set(this.value >>> 1);
this.Bit(7, carry ? ret : value);
return ret;
2015-01-22 21:32:46 +00:00
}
}
class Word {
private value: Byte[];
2015-01-25 20:06:11 +00:00
public constructor(high: number);
public constructor(high: Byte, low: Byte);
public constructor(high: any, low?: Byte) {
this.Set(high, low);
}
2015-01-22 21:32:46 +00:00
2015-01-27 20:11:57 +00:00
public Get(): number {
return (this.value[0].Get() << 8) | this.value[1].Get();
}
2015-01-25 20:06:11 +00:00
public High(): Byte {
return this.value[0];
}
public Low(): Byte {
return this.value[1];
}
public Set(high: number);
public Set(high: Byte, low: Byte);
public Set(high: any, low?: Byte) {
this.value = typeof high == "number" ? [new Byte((high & ~0xFF) >> 8), new Byte(high & 0xFF)] : [high, low];
}
2015-01-27 18:49:08 +00:00
public SetHigh(value: number);
public SetHigh(value: Byte);
public SetHigh(value: any) {
this.value[0] = typeof value == "number" ? new Byte(value) : value;
}
public SetLow(value: number);
public SetLow(value: Byte);
public SetLow(value: any) {
this.value[1] = typeof value == "number" ? new Byte(value) : value;
}
public Add(value: number): Word;
public Add(value: Word): Word;
public Add(value: any): Word {
value = typeof value == "number" ? new Word(value) : value;
2015-01-27 20:11:57 +00:00
var hold = this.Get() + value.Get();
this.Set(hold);
return new Word(hold > 0xFFFF ? hold - 0xFFFF : 0);
//var overflow = [this.value[0].Add(value.High()), this.value[1].Add(value.Low())];
//return new Word(overflow[0].Get() + this.value[0].Add(overflow[1]).Get());
}
2015-01-27 18:49:08 +00:00
public AddByte(value: number): Word;
public AddByte(value: Byte): Word;
public AddByte(value: any): Word {
2015-01-27 20:11:57 +00:00
value = typeof value != "number" ? value.Get() : value;
return this.Add(value);
2015-01-27 18:49:08 +00:00
}
2015-01-22 21:32:46 +00:00
}