blob: 40479e926bf95de37e992834489b048af22479d1 [file] [log] [blame] [raw]
"use strict";
/*
* string operations
*
* cmp si di
* movs 0 1 1 A4
* cmps 1 1 1 A6
* stos 0 0 1 AA
* lods 0 1 0 AC
* scas 1 0 1 AE
* ins 0 0 1
* outs 0 1 0
*/
#define string_instruction(s, use_cmp, use_di, use_si, fn, aligned_fn)\
var src, dest, data_src, data_dest;\
var size = flags & flag_direction ? -(s >> 3) : s >> 3;\
var ds, es;\
if(use_cmp && !use_si) data_src = reg ## s[reg_eax];\
if(use_di) es = get_seg(reg_es), dest = es + regv[reg_vdi];\
if(use_si) ds = get_seg_prefix(reg_ds), src = ds + regv[reg_vsi];\
if(repeat_string_prefix) {\
if(regv[reg_vcx] === 0) return;\
var aligned = s > 8 && (!use_di || (dest & (s >> 3) - 1) === 0) && (!use_si || (src & (s >> 3) - 1) === 0);\
do {\
if(aligned) {\
aligned_fn;\
} else {\
fn;\
}\
if(use_di) dest += size, regv[reg_vdi] += size;\
if(use_si) src += size, regv[reg_vsi] += size;\
} while(--regv[reg_vcx] && (!use_cmp || (data_src === data_dest) === repeat_string_type));\
} else {\
fn;\
if(use_di) regv[reg_vdi] += size;\
if(use_si) regv[reg_vsi] += size;\
}\
if(use_cmp) cmp ## s(data_src, data_dest);\
function movsb()
{
string_instruction(8, false, true, true,
{
safe_write8(dest, safe_read8(src));
}, {});
}
function movsw()
{
string_instruction(16, false, true, true,
{
safe_write16(dest, safe_read16(src));
}, {
var phys_src = translate_address_read(src);
var phys_dest = translate_address_write(dest);
memory.write_aligned16(phys_dest, memory.read_aligned16(phys_src));
});
}
function movsd()
{
string_instruction(32, false, true, true,
{
safe_write32(dest, safe_read32s(src));
}, {
var phys_src = translate_address_read(src);
var phys_dest = translate_address_write(dest);
memory.write_aligned32(phys_dest, memory.read_aligned32(phys_src));
});
}
function cmpsb()
{
string_instruction(8, true, true, true,
{
data_dest = safe_read8(dest);
data_src = safe_read8(src);
}, {});
}
function cmpsw()
{
string_instruction(16, true, true, true,
{
data_dest = safe_read16(dest);
data_src = safe_read16(src);
}, {
data_dest = memory.read_aligned16(translate_address_read(dest));
data_src = memory.read_aligned16(translate_address_read(src));
});
}
function cmpsd()
{
string_instruction(32, true, true, true,
{
data_dest = safe_read32(dest);
data_src = safe_read32(src);
}, {
data_dest = memory.read_aligned32(translate_address_read(dest)) >>> 0;
data_src = memory.read_aligned32(translate_address_read(src)) >>> 0;
});
}
function stosb()
{
var data = reg8[reg_al];
string_instruction(8, false, true, false,
{
safe_write8(dest, data);
}, {});
}
function stosw()
{
var data = reg16[reg_ax];
string_instruction(16, false, true, false,
{
safe_write16(dest, data);
}, {
memory.write_aligned16(translate_address_write(dest), data);
});
}
function stosd()
{
//dbg_log("stosd " + ((reg32[reg_edi] & 3) ? "mis" : "") + "aligned", LOG_CPU);
var data = reg32[reg_eax];
string_instruction(32, false, true, false,
{
safe_write32(dest, data);
}, {
memory.write_aligned32(translate_address_write(dest), data);
});
}
function lodsb()
{
string_instruction(8, false, false, true,
{
reg8[reg_al] = safe_read8(src);
}, {});
}
function lodsw()
{
string_instruction(16, false, false, true,
{
reg16[reg_ax] = safe_read16(src);
}, {
reg16[reg_ax] = safe_read16(src);
});
}
function lodsd()
{
string_instruction(32, false, false, true,
{
reg32[reg_eax] = safe_read32s(src);
}, {
reg32[reg_eax] = safe_read32s(src);
});
}
function scasb()
{
string_instruction(8, true, true, false,
{
data_dest = safe_read8(dest);
}, {});
}
function scasw()
{
string_instruction(16, true, true, false,
{
data_dest = safe_read16(dest);
}, {
data_dest = memory.read_aligned16(translate_address_read(dest));
});
}
function scasd()
{
string_instruction(32, true, true, false,
{
data_dest = safe_read32(dest);
}, {
data_dest = memory.read_aligned32(translate_address_read(dest)) >>> 0;
});
}
function insb()
{
test_privileges_for_io();
var port = reg16[reg_dx];
string_instruction(8, false, true, false,
{
safe_write8(dest, io.port_read8(port));
}, {
});
}
function insw()
{
test_privileges_for_io();
var port = reg16[reg_dx];
string_instruction(16, false, true, false,
{
safe_write16(dest, io.port_read16(port));
}, {
var phys_dest = translate_address_write(dest);
memory.write_aligned16(phys_dest, io.port_read16(port));
});
}
function insd()
{
test_privileges_for_io();
var port = reg16[reg_dx];
string_instruction(32, false, true, false,
{
safe_write32(dest, io.port_read32(port));
}, {
var phys_dest = translate_address_write(dest);
memory.write_aligned32(phys_dest, io.port_read32(port));
});
}
function outsb()
{
test_privileges_for_io();
var port = reg16[reg_dx];
string_instruction(8, false, false, true,
{
io.port_write8(port, safe_read8(src));
}, {
});
}
function outsw()
{
test_privileges_for_io();
var port = reg16[reg_dx];
string_instruction(16, false, false, true,
{
io.port_write16(port, safe_read16(src));
}, {
io.port_write16(port, safe_read16(src));
});
}
function outsd()
{
test_privileges_for_io();
var port = reg16[reg_dx];
string_instruction(32, false, false, true,
{
io.port_write32(port, safe_read32s(src));
}, {
io.port_write32(port, safe_read32s(src));
});
}