1
0
mirror of https://gitlab.os-k.eu/os-k-team/kvisc.git synced 2023-08-25 14:05:46 +02:00
kvisc/arch.c
2019-05-15 20:06:45 +02:00

129 lines
2.4 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"
void scan_param(ctx_t *ctx, acc_t *p)
{
ushort c;
reg_t *r;
assert(p != 0);
c = ctx->get(ctx);
if (c == A_MEM) {
p->mem = 1;
c = ctx->get(ctx);
}
if (c >= A_IMM16) {
assert(p->type == A_REG);
p->type = c;
c = ctx->get(ctx);
}
p->val = c;
if (p->type == A_IMM32) {
p->val = (p->val << 16) | ctx->get(ctx);
}
else if (p->type == A_IMM64) {
p->val = (p->val << 16) | ctx->get(ctx);
p->val = (p->val << 16) | ctx->get(ctx);
p->val = (p->val << 16) | ctx->get(ctx);
}
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");
}
}
}
void dumpregs(ctx_t *ctx)
{
int i;
reg_t *r;
for (i = 0; i < NREGS; i++) {
if (i > 0 && i % 4 == 0)
log("\n");
r = &ctx->r[i];
log("%s=0x%016lX ", r->name, r->val);
}
log("\n");
}
void dumpinstr(ctx_t *ctx, ushort c, acc_t *p1, acc_t *p2)
{
acc_t *p = 0;
instr_t *i = &ctx->i[c];
log("%s", i->name);
if (i->prms > 0)
p = p1;
lp:
if (p != 0) {
if (p->mem) log("[");
if (p->type == A_REG)
log(" %s", ctx->r[p->val].name);
else
log(" 0x%lX", p->val);
if (p->mem) log("]");
if (p == p1 && i->prms == 2) {
p = p2;
goto lp;
}
}
log("\n");
}
void decode(ctx_t *ctx)
{
instr_t *i;
acc_t p1 = { 0 }, p2 = { 0 };
ushort c = ctx->get(ctx);
if (c == 0xFFFF) {
_except(ctx, E_SHT, "Shutdown INSTR");
}
else if (ISPREF(c)) {
// nothing for now
}
else if (!ISINSTR(c)) {
_except(ctx, E_ILL, "Bad opcode");
}
// Scanning an instruction
i = &ctx->i[c];
// Scan for parameters
if (i->prms > 0) {
scan_param(ctx, &p1);
if (i->prms == 2) {
scan_param(ctx, &p2);
}
}
dumpinstr(ctx, c, &p1, &p2);
i->func(ctx, &p1, &p2);
}