<head><script type='text/javascript'>
var Register = function(reg) {
this.store = [0x43,0x42,0x45,0x44,0x4C,0x48,0x46,0x41,0,0,0,0,0,0,0,0];
for(var id in reg) {
var ptr = reg[id].split(/\s+/),
num = ptr.length,
gets = {
"1" : "return this.store[0x_0]",
"2" : "return this.store[0x_0] | this.store[0x_1] << 8",
"3" : "var tmp = this.store[0x_0] | this.store[0x_1] << 8; return 0x_2==1 ? m[tmp] : m[tmp] | m[tmp + 1] << 8"
},
sets = {
"1" : "this.store[0x_0] = x & 255",
"2" : "this.store[0x_0] = x & 255, this.store[0x_1] = x >> 8 & 255",
"3" : "var tmp = this.store[0x_0] | this.store[0x_1] << 8; 0x_2==1 ? m[tmp] = x & 255 : (m[tmp] = x & 255, m[tmp + 1] = x >> 8 & 255)"
};
Object.defineProperty(this, id, {
enumerable : true,
get : new Function(
"",
gets[num].replace(/_(\d)/g, function(s, i) {
return ptr[i];
}
)
),
set : new Function(
"x",
sets[num].replace(/_(\d)/g, function(s, i) {
return ptr[i];
}
)
)
});
}
return this;
};
var r;
var m = (function(n) {
var tmp = [];
while(n --)
tmp.push(Math.random() * 256 & 255);
return tmp;
})(65535);
var regs = {
bc : "0 1",
b : "1",
c : "0",
de : "2 3",
d : "3",
e : "2",
hl : "4 5",
h : "5",
l : "4",
psw : "7 6",
f : "6",
a : "7",
m : "4 5 1",
cx : "0 1",
ch : "1",
cl : "0",
dx : "2 3",
dh : "3",
dl : "2",
bx : "4 5",
bh : "5",
bl : "4",
al : "7"
};
window.onload = function() {
var log = [];
r = new Register(regs);
for(var id in regs)
log.push(id + ": " + r[id].toString(16));
document.getElementsByTagName("pre")[0].innerHTML = log.join("<br />");
}
</script>
</head>
<body>
<pre></pre>
</body>
P.S.: Код работает…
Однако, вопрос к настоящим знатокам: Может что-то можно описать
красивее?