1
0
mirror of https://gitlab.os-k.eu/os-k-team/kvisc.git synced 2023-08-25 14:05:46 +02:00
kvisc/pc/decd.c
2019-05-30 12:44:56 +02:00

138 lines
2.5 KiB
C

// The OS/K Team licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
#include "arch.h"
#ifdef _NEED_DISASM
#define _except __except
static inline void _except(ctx_t *ctx, int x, char *fmt, ...)
{
va_list ap;
log("Disassembly error: ");
va_start(ap, fmt);
vlog(fmt, ap);
va_end(ap);
log("\n");
if (ctx->disf)
fclose(ctx->disf);
exit(-1);
}
#endif
static void scan_param(ctx_t *ctx, acc_t *p)
{
ushort c;
reg_t *r;
c = ctx->get(ctx);
if (A_IS_MEM(c)) {
p->mem = 1;
p->off = 0;
p->mlen = c & 0xF;
c = ctx->get(ctx);
}
else if (A_IS_OFF(c)) {
p->mem = 1;
p->mlen = c & 0xF;
p->off = (short)ctx->get(ctx);
c = ctx->get(ctx);
}
if (c >= A_IMM16) {
// Check for double access code
if (p->type != A_REG)
_except(ctx, E_ILL, "Bad access code");
p->type = c;
c = ctx->get(ctx);
}
p->val = c;
if (p->type == A_IMM32) {
p->ilen = 4;
p->val = p->val | ((ulong)ctx->get(ctx) << 16);
}
else if (p->type == A_IMM64) {
p->ilen = 8;
p->val = p->val | ((ulong)ctx->get(ctx) << 16);
p->val = p->val | ((ulong)ctx->get(ctx) << 32);
p->val = p->val | ((ulong)ctx->get(ctx) << 48);
}
else if (p->type == A_REG) {
if (p->val > NREGS) {
_except(ctx, E_ILL, "Inexistent REG");
}
r = &ctx->r[p->val];
if (r->flags & (RES | CTL)) {
_except(ctx, E_ACC, "Reserved REG: %s", r->name);
}
}
}
#ifndef _NEED_DISASM
void decode(ctx_t *ctx)
#else
void disasm(ctx_t *ctx)
#endif
{
instr_t *i;
acc_t p1 = { 0 }, p2 = { 0 };
uint rep = 0;
ulong rip = ctx->r[RIP].val;
ushort c = ctx->get(ctx);
if (ISPREF(c)) {
if (c == PREF_REP) {
rep = 1;
c = ctx->get(ctx);
}
}
else if (!ISINSTR(c)) {
_except(ctx, E_ILL, "Bad opcode: 0x%04hX", c);
}
i = &ctx->i[c];
// Scan for parameters
if (i->prm1 != NOPRM) {
scan_param(ctx, &p1);
if (i->prm2 != NOPRM) {
scan_param(ctx, &p2);
}
}
#ifdef _NEED_DISASM
dumpinstr(ctx, rip, rep, c, &p1, &p2);
#else
do_rep:
dumpinstr(ctx, rip, rep, c, &p1, &p2);
i->func(ctx, &p1, &p2);
if (rep && ctx->r[RCX].val > 0) {
log("rcx::%lu\n", ctx->r[RCX].val);
ctx->r[RCX].val--;
goto do_rep;
}
#endif
}