2019-05-15 20:06:45 +02:00
|
|
|
// The OS/K Team licenses this file to you under the MIT license.
|
|
|
|
// See the LICENSE file in the project root for more information.
|
2019-05-15 19:26:40 +02:00
|
|
|
|
2019-06-05 12:53:09 +02:00
|
|
|
#include <pc/arch.h>
|
2019-05-15 19:26:40 +02:00
|
|
|
|
2019-06-07 22:23:38 +02:00
|
|
|
//
|
|
|
|
// Imperatively read the "DECD" file before reading this code
|
|
|
|
//
|
2019-05-16 21:42:23 +02:00
|
|
|
|
2019-06-13 22:20:35 +02:00
|
|
|
static void check_param_type(ctx_t *ctx, instr_t *in, uint prm, uchar fmt)
|
2019-05-16 21:42:23 +02:00
|
|
|
{
|
2019-06-07 22:23:38 +02:00
|
|
|
bool ok;
|
2019-05-16 21:42:23 +02:00
|
|
|
|
2019-06-07 22:23:38 +02:00
|
|
|
if (prm == P_REG)
|
|
|
|
ok = (fmt == A_REG);
|
2019-05-30 12:44:56 +02:00
|
|
|
|
2019-06-07 22:23:38 +02:00
|
|
|
else if (prm == P_IMM)
|
|
|
|
ok = (fmt == A_IMM64);
|
2019-05-30 12:44:56 +02:00
|
|
|
|
2019-06-07 23:25:47 +02:00
|
|
|
else /* if (prm == P_MEM) */
|
2019-06-07 22:23:38 +02:00
|
|
|
ok = ACC_FMT_IS_MEM(fmt);
|
2019-05-16 21:42:23 +02:00
|
|
|
|
2019-06-07 22:23:38 +02:00
|
|
|
if (!ok)
|
|
|
|
_except(ctx, E_ILL,
|
2019-06-13 22:20:35 +02:00
|
|
|
"FT1 or FT2 not matching %s's expected parameter types: "
|
|
|
|
"fmt=0x%x prm=0x%x", in->full, fmt, prm);
|
2019-05-16 21:42:23 +02:00
|
|
|
}
|
|
|
|
|
2019-06-07 22:23:38 +02:00
|
|
|
void decode(ctx_t *ctx)
|
2019-05-15 19:26:40 +02:00
|
|
|
{
|
2019-06-07 22:23:38 +02:00
|
|
|
char *illmsg;
|
|
|
|
|
|
|
|
instr_t *in;
|
|
|
|
|
|
|
|
acc_t p1 = { 0 };
|
|
|
|
acc_t p2 = { 0 };
|
|
|
|
|
2019-06-08 09:36:28 +02:00
|
|
|
bool rep = 0;
|
2019-06-07 23:25:47 +02:00
|
|
|
uint cond = 0;
|
2019-06-07 22:23:38 +02:00
|
|
|
bool lock, nomore;
|
|
|
|
|
|
|
|
ushort w1, w2;
|
|
|
|
uchar f1 = 0, f2 = 0;
|
2019-05-15 19:26:40 +02:00
|
|
|
|
2019-06-07 22:23:38 +02:00
|
|
|
ulong pc = rip;
|
2019-06-16 20:31:55 +02:00
|
|
|
|
|
|
|
// Instruction counter
|
|
|
|
R(RX0)++;
|
2019-05-15 19:26:40 +02:00
|
|
|
|
2019-06-07 22:23:38 +02:00
|
|
|
//
|
|
|
|
// Process the first word of the instruction
|
|
|
|
//
|
|
|
|
w1 = ctx->get(ctx);
|
|
|
|
|
|
|
|
// Extract first word flags
|
|
|
|
|
|
|
|
lock = !!(w1 & PREF_LOCK);
|
|
|
|
nomore = !!(w1 & PREF_NOMORE);
|
|
|
|
|
|
|
|
w1 &= ~(PREF_LOCK|PREF_NOMORE);
|
|
|
|
|
|
|
|
// Find instruction
|
|
|
|
|
|
|
|
if (w1 >= NINSTRS)
|
|
|
|
{
|
|
|
|
illmsg = "No such INSTR";
|
|
|
|
goto ill;
|
2019-05-16 16:48:45 +02:00
|
|
|
}
|
2019-05-30 12:44:56 +02:00
|
|
|
|
2019-06-07 22:23:38 +02:00
|
|
|
in = &ctx->i[w1];
|
|
|
|
|
2019-06-12 15:30:35 +02:00
|
|
|
if (nomore)
|
2019-06-07 22:23:38 +02:00
|
|
|
goto skip_w2;
|
|
|
|
|
|
|
|
//
|
|
|
|
// Process second word
|
|
|
|
//
|
2019-06-06 15:06:34 +02:00
|
|
|
|
2019-06-07 22:23:38 +02:00
|
|
|
w2 = ctx->get(ctx);
|
2019-06-06 15:06:34 +02:00
|
|
|
|
2019-06-07 22:23:38 +02:00
|
|
|
// REP and COND
|
2019-06-07 23:25:47 +02:00
|
|
|
rep = !!(w2 & PREF_REP);
|
2019-06-07 22:23:38 +02:00
|
|
|
cond = (w2 & BITS_COND) >> COND_SHIFT;
|
|
|
|
|
|
|
|
// F1 and F2
|
|
|
|
f1 = (w2 >> F1_SHIFT) & Fx_MASK;
|
|
|
|
f2 = w2 & Fx_MASK;
|
|
|
|
|
|
|
|
skip_w2:
|
|
|
|
|
|
|
|
//
|
|
|
|
// Deal with operand 1
|
|
|
|
//
|
|
|
|
|
|
|
|
if (in->prm1 == NOPRM)
|
|
|
|
{
|
|
|
|
if (f1 || f2)
|
|
|
|
{
|
|
|
|
illmsg = "FT1 and/or FT2 filled for 0-param INSTR";
|
|
|
|
goto ill;
|
2019-06-06 15:06:34 +02:00
|
|
|
}
|
|
|
|
|
2019-06-07 22:23:38 +02:00
|
|
|
exec_instr(ctx, in, NULL, NULL, lock, rep, cond, pc);
|
|
|
|
return;
|
|
|
|
}
|
2019-06-06 15:06:34 +02:00
|
|
|
|
2019-06-13 22:20:35 +02:00
|
|
|
check_param_type(ctx, in, in->prm1, f1);
|
2019-06-07 22:23:38 +02:00
|
|
|
extract_param(ctx, &p1, f1);
|
|
|
|
|
|
|
|
//
|
|
|
|
// Deal with operand 2
|
|
|
|
//
|
|
|
|
|
|
|
|
if (in->prm2 == NOPRM)
|
|
|
|
{
|
|
|
|
if (f2)
|
|
|
|
{
|
|
|
|
illmsg = "FT2 filled for 1-param INSTR";
|
|
|
|
goto ill;
|
2019-06-06 15:06:34 +02:00
|
|
|
}
|
|
|
|
|
2019-06-07 22:23:38 +02:00
|
|
|
exec_instr(ctx, in, &p1, NULL, lock, rep, cond, pc);
|
|
|
|
return;
|
2019-05-15 19:26:40 +02:00
|
|
|
}
|
2019-05-30 12:44:56 +02:00
|
|
|
|
2019-06-13 22:20:35 +02:00
|
|
|
check_param_type(ctx, in, in->prm2, f2);
|
2019-06-07 22:23:38 +02:00
|
|
|
extract_param(ctx, &p2, f2);
|
2019-05-16 21:42:23 +02:00
|
|
|
|
2019-06-07 22:23:38 +02:00
|
|
|
exec_instr(ctx, in, &p1, &p2, lock, rep, cond, pc);
|
|
|
|
return;
|
|
|
|
|
|
|
|
ill:
|
|
|
|
_except(ctx, E_ILL, illmsg);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Verify that access to a certain register is legal
|
|
|
|
//
|
|
|
|
static void checkreg(ctx_t *ctx, uint reg, bool inv_is_ok)
|
|
|
|
{
|
|
|
|
if (reg >= NREGS)
|
|
|
|
_except(ctx, E_ILL, "Inexistent register: %u", reg);
|
|
|
|
|
|
|
|
if (reg == INV)
|
|
|
|
{
|
|
|
|
if (!inv_is_ok)
|
|
|
|
_except(ctx, E_ILL, "INV dereference");
|
|
|
|
else
|
|
|
|
return;
|
2019-05-15 19:26:40 +02:00
|
|
|
}
|
|
|
|
|
2019-06-07 22:23:38 +02:00
|
|
|
if (ctx->r[reg].flags & (RES | CTL))
|
2019-06-12 15:30:35 +02:00
|
|
|
//_except(ctx, E_ACC, "Reserved REG: %u", reg);
|
2019-06-07 22:23:38 +02:00
|
|
|
|
|
|
|
if (ctx->r[reg].flags & SYS)
|
|
|
|
if (cr0 & UF)
|
|
|
|
_except(ctx, E_SYS, "User access to SYS REG: %u", reg);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Extract operand according to fmt
|
|
|
|
//
|
|
|
|
void extract_param(ctx_t *ctx, acc_t *p, uchar fmt)
|
|
|
|
{
|
2019-06-12 15:30:35 +02:00
|
|
|
uint mlen, mfmt;
|
2019-06-07 22:23:38 +02:00
|
|
|
ushort temp;
|
|
|
|
|
|
|
|
p->type = fmt;
|
2019-05-30 12:44:56 +02:00
|
|
|
|
2019-06-07 22:23:38 +02:00
|
|
|
if (fmt == A_REG)
|
|
|
|
{
|
|
|
|
p->reg = ctx->get(ctx);
|
|
|
|
checkreg(ctx, p->reg, 0);
|
|
|
|
|
|
|
|
p->val = R(p->reg);
|
2019-06-12 15:30:35 +02:00
|
|
|
|
|
|
|
return;
|
2019-05-15 19:26:40 +02:00
|
|
|
}
|
2019-05-30 12:44:56 +02:00
|
|
|
|
2019-06-07 22:23:38 +02:00
|
|
|
else if (fmt == A_IMM64)
|
|
|
|
{
|
|
|
|
p->val = ctx->get(ctx);
|
|
|
|
p->val |= (ulong)ctx->get(ctx) << 16;
|
|
|
|
p->val |= (ulong)ctx->get(ctx) << 32;
|
|
|
|
p->val |= (ulong)ctx->get(ctx) << 48;
|
2019-06-12 15:30:35 +02:00
|
|
|
|
|
|
|
return;
|
2019-05-15 19:26:40 +02:00
|
|
|
}
|
2019-05-30 12:44:56 +02:00
|
|
|
|
2019-06-07 22:23:38 +02:00
|
|
|
assert(ACC_FMT_IS_MEM(fmt));
|
|
|
|
|
|
|
|
//
|
|
|
|
// Handle a memory access
|
|
|
|
//
|
|
|
|
|
|
|
|
mlen = fmt & AM_MLEN_MASK;
|
|
|
|
mfmt = fmt & AM_MFMT_MASK;
|
|
|
|
|
|
|
|
p->mlen = (mlen == AM_8ACC ? 1
|
|
|
|
: (mlen == AM_16ACC ? 2
|
|
|
|
: (mlen == AM_32ACC ? 4
|
|
|
|
: (mlen == AM_64ACC ? 8 : 0))));
|
|
|
|
|
|
|
|
if (p->mlen == 0)
|
|
|
|
_except(ctx, E_ILL, "Invalid MLEN for access: %x", fmt);
|
|
|
|
|
|
|
|
switch (mfmt)
|
|
|
|
{
|
|
|
|
case AM_IMM64:
|
|
|
|
p->addr = ctx->get(ctx);
|
|
|
|
p->addr |= (ulong)ctx->get(ctx) << 16;
|
|
|
|
p->addr |= (ulong)ctx->get(ctx) << 32;
|
|
|
|
p->addr |= (ulong)ctx->get(ctx) << 48;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AM_RR:
|
|
|
|
case AM_RRI:
|
|
|
|
case AM_RRII:
|
|
|
|
temp = ctx->get(ctx);
|
2019-06-12 15:30:35 +02:00
|
|
|
p->reg1 = temp >> 8;
|
|
|
|
p->reg2 = temp & 0xFF;
|
|
|
|
|
|
|
|
checkreg(ctx, p->reg1, 1);
|
|
|
|
checkreg(ctx, p->reg2, 1);
|
2019-06-07 22:23:38 +02:00
|
|
|
|
2019-06-12 15:30:35 +02:00
|
|
|
if (mfmt == AM_RRI)
|
|
|
|
{
|
|
|
|
p->imm1 = 1;
|
|
|
|
p->imm2 = ctx->get(ctx);
|
|
|
|
}
|
2019-06-07 22:23:38 +02:00
|
|
|
|
2019-06-12 15:30:35 +02:00
|
|
|
else if (mfmt == AM_RRII)
|
2019-06-07 22:23:38 +02:00
|
|
|
{
|
2019-06-12 15:30:35 +02:00
|
|
|
p->imm1 = ctx->get(ctx);
|
|
|
|
p->imm2 = ctx->get(ctx);
|
2019-06-07 22:23:38 +02:00
|
|
|
}
|
|
|
|
|
2019-06-12 15:30:35 +02:00
|
|
|
else
|
2019-06-07 22:23:38 +02:00
|
|
|
{
|
2019-06-12 15:30:35 +02:00
|
|
|
p->imm1 = 1;
|
|
|
|
p->imm2 = 0;
|
2019-06-07 22:23:38 +02:00
|
|
|
}
|
2019-05-15 19:26:40 +02:00
|
|
|
|
2019-06-14 12:40:49 +02:00
|
|
|
p->addr = R(p->reg1) + R(p->reg2) * p->imm1
|
|
|
|
+ (long)(short)p->imm2;
|
2019-06-07 22:23:38 +02:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
_except(ctx, E_ILL, "Invalid MFMT for access: %x", fmt);
|
|
|
|
}
|
|
|
|
}
|
2019-05-30 12:44:56 +02:00
|
|
|
|
2019-06-07 22:23:38 +02:00
|
|
|
static bool eval_cond(ctx_t *ctx, uint cond)
|
|
|
|
{
|
2019-06-12 15:30:35 +02:00
|
|
|
bool neg = cond & (1 << 4);
|
2019-06-07 22:23:38 +02:00
|
|
|
bool ok;
|
|
|
|
|
2019-06-12 15:30:35 +02:00
|
|
|
cond &= ~(1 << 4);
|
2019-06-07 22:23:38 +02:00
|
|
|
|
|
|
|
switch (cond)
|
|
|
|
{
|
2019-06-08 09:36:28 +02:00
|
|
|
case CD_NONE: ok = 1; break;
|
|
|
|
|
2019-06-07 22:23:38 +02:00
|
|
|
case CD_C: ok = flg&CF; break;
|
|
|
|
case CD_O: ok = flg&OF; break;
|
|
|
|
case CD_Z: ok = flg&ZF; break;
|
|
|
|
case CD_S: ok = flg&SF; break;
|
|
|
|
case CD_P: ok = flg&PF; break;
|
|
|
|
|
|
|
|
case CD_A: ok = !(flg&CF || flg&ZF); break;
|
|
|
|
case CD_AE: ok = !(flg&CF); break;
|
|
|
|
|
|
|
|
case CD_B: ok = flg&CF; break;
|
|
|
|
case CD_BE: ok = flg&CF || flg&ZF; break;
|
|
|
|
|
|
|
|
case CD_G: ok = !(flg&ZF) && (!(flg&SF) == !(flg&OF)); break;
|
|
|
|
case CD_GE: ok = !(flg&SF) == !(flg&OF); break;
|
|
|
|
|
|
|
|
case CD_L: ok = !(flg&SF) != !(flg&OF); break;
|
|
|
|
case CD_LE: ok = flg&ZF || (!(flg&SF) != !(flg&OF)); break;
|
|
|
|
|
|
|
|
case CD_CXZ: ok = !rcx; break;
|
|
|
|
|
|
|
|
default:
|
2019-06-12 15:30:35 +02:00
|
|
|
_except(ctx, E_ILL, "Invalid COND value: 0x%x", (neg?cond|(1<<4):cond));
|
2019-05-15 19:26:40 +02:00
|
|
|
}
|
2019-06-07 22:23:38 +02:00
|
|
|
|
|
|
|
return neg ? !ok : !!ok;
|
2019-05-15 19:26:40 +02:00
|
|
|
}
|
|
|
|
|
2019-06-07 22:23:38 +02:00
|
|
|
//
|
|
|
|
// Executes an instruction
|
|
|
|
//
|
2019-06-12 18:43:24 +02:00
|
|
|
void exec_instr(ctx_t *ctx,
|
|
|
|
instr_t *in,
|
|
|
|
acc_t *p1,
|
|
|
|
acc_t *p2,
|
|
|
|
bool lock,
|
|
|
|
bool rep,
|
|
|
|
uint cond,
|
|
|
|
ulong pc)
|
2019-06-07 22:23:38 +02:00
|
|
|
{
|
2019-06-12 15:47:11 +02:00
|
|
|
bool out;
|
|
|
|
ulong r1 = 0, r2 = 0;
|
2019-06-07 22:23:38 +02:00
|
|
|
|
|
|
|
// Debugging
|
|
|
|
dump_instr(ctx, in, p1, p2, lock, rep, cond, pc);
|
2019-06-08 09:36:28 +02:00
|
|
|
|
2019-06-13 15:32:24 +02:00
|
|
|
//
|
|
|
|
// For REPs we evaluate the condition AFTER running the instruction,
|
|
|
|
// in a do ... while(cond) fashion
|
|
|
|
//
|
|
|
|
if (!rep && !eval_cond(ctx, cond))
|
2019-06-07 22:23:38 +02:00
|
|
|
return;
|
|
|
|
|
2019-06-13 15:32:24 +02:00
|
|
|
do_rep:
|
|
|
|
|
2019-06-12 15:47:11 +02:00
|
|
|
out = in->func(ctx, p1, p2, &r1, &r2);
|
2019-06-07 22:23:38 +02:00
|
|
|
|
|
|
|
if (out)
|
|
|
|
{
|
|
|
|
if (p1->type == A_REG)
|
2019-06-12 15:47:11 +02:00
|
|
|
R(p1->reg) = r1;
|
2019-06-07 23:25:47 +02:00
|
|
|
|
2019-06-07 22:23:38 +02:00
|
|
|
else if (p1->type == A_IMM64)
|
2019-06-07 23:25:47 +02:00
|
|
|
_except(ctx, E_ACC, "Trying to output to an IMM64");
|
|
|
|
|
2019-06-07 22:23:38 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
assert(ACC_IS_MEM(p1));
|
2019-06-12 15:47:11 +02:00
|
|
|
writemem(ctx, r1, p1->addr, p1->mlen);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (out == 2)
|
|
|
|
{
|
|
|
|
if (p2->type == A_REG)
|
|
|
|
R(p2->reg) = r2;
|
|
|
|
|
|
|
|
else if (p2->type == A_IMM64)
|
|
|
|
_except(ctx, E_ACC, "Trying to output to an IMM64");
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
assert(ACC_IS_MEM(p2));
|
|
|
|
writemem(ctx, r2, p2->addr, p2->mlen);
|
2019-06-07 22:23:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-13 17:13:59 +02:00
|
|
|
if (rep)
|
2019-06-07 22:23:38 +02:00
|
|
|
{
|
2019-06-13 15:32:24 +02:00
|
|
|
// RCX remains untouched when condition fails
|
|
|
|
if (!eval_cond(ctx, cond))
|
|
|
|
return;
|
|
|
|
|
2019-06-13 17:13:59 +02:00
|
|
|
if (rcx > 0)
|
|
|
|
rcx--;
|
|
|
|
|
|
|
|
if (rcx == 0)
|
|
|
|
return;
|
|
|
|
|
2019-06-13 15:32:24 +02:00
|
|
|
// Show that we're REP'ing
|
|
|
|
dump_instr(ctx, in, p1, p2, lock, rep, cond, pc);
|
|
|
|
|
2019-05-29 16:57:22 +02:00
|
|
|
goto do_rep;
|
|
|
|
}
|
2019-05-15 19:26:40 +02:00
|
|
|
}
|
|
|
|
|