mirror of
https://gitlab.os-k.eu/os-k-team/kvisc.git
synced 2023-08-25 14:05:46 +02:00
125 lines
2.3 KiB
C
125 lines
2.3 KiB
C
// The OS/K Team licences this file to you under the MIT license.
|
|
// See the LICENCE file in the project root for more information.
|
|
|
|
#include <assert.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
#define log printf
|
|
|
|
#define packed __attribute__ ((__packed__))
|
|
#define static_assert _Static_assert
|
|
|
|
typedef unsigned int bool;
|
|
typedef unsigned char uchar;
|
|
typedef unsigned short ushort;
|
|
typedef unsigned int uint;
|
|
typedef unsigned long ulong;
|
|
|
|
typedef struct reg_t reg_t;
|
|
typedef struct ctx_t ctx_t;
|
|
typedef struct instr_t instr_t;
|
|
typedef struct acc_t acc_t;
|
|
typedef struct arch_t arch_t;
|
|
|
|
enum
|
|
{
|
|
RAX, RBX, RCX, RDX, RDI, RSI, RBP, RSP,
|
|
R08, R09, R10, R11, R12, R13, R14, R15,
|
|
K00, K01, K02, K03, K04, K05, K06, K07,
|
|
CR0, CR1, CR2, CR3, CR4, CR5, CR6, CR7,
|
|
RIP, RFG,
|
|
|
|
NREGS
|
|
};
|
|
|
|
enum
|
|
{
|
|
I_ADD, I_SUB, I_MUL, I_DIV, I_INC, I_DEC,
|
|
I_AND, I_OR, I_XOR, I_NEG, I_CMD, I_TST,
|
|
|
|
I_PUSH, I_PUSHF, I_PUSHA,
|
|
I_POP, I_POPF, I_POPA,
|
|
|
|
I_MOV, I_XCHG,
|
|
|
|
I_BSWP,
|
|
|
|
I_PSE, I_HLT, I_INT,
|
|
|
|
NINSTRS
|
|
};
|
|
|
|
enum
|
|
{
|
|
GPR = 1 << 0, // General
|
|
CTL = 1 << 1, // Control
|
|
SEG = 1 << 2, // Segment
|
|
RES = 1 << 8, // Reserved for insternal use
|
|
SYS = 1 << 9, // Reserved for supervisor mode
|
|
};
|
|
|
|
struct reg_t
|
|
{
|
|
char *name;
|
|
char *desc;
|
|
ulong val;
|
|
ulong flags;
|
|
};
|
|
|
|
enum { A_MEM=0x7000 };
|
|
struct acc_t
|
|
{
|
|
bool mem;
|
|
enum { A_REG=0, A_IMM16=0x7001, A_IMM32, A_IMM64 } type;
|
|
|
|
ulong val;
|
|
};
|
|
|
|
enum { NOPREF, PREF_REP=0x8000, PREF_LOCK, NPREFS };
|
|
|
|
#define ISPREF(x) ((x) & 0x8000 && (x)<NPREFS)
|
|
#define ISINSTR(x) ((x) < NINSTRS)
|
|
|
|
struct instr_t
|
|
{
|
|
char *name;
|
|
uint prms;
|
|
void (*func)(ctx_t *, acc_t *, acc_t *);
|
|
};
|
|
|
|
struct ctx_t
|
|
{
|
|
reg_t *r;
|
|
instr_t *i;
|
|
|
|
// Memory and memory size
|
|
uchar *mp;
|
|
ulong mz;
|
|
|
|
// Read next instruction
|
|
ushort (*get)(ctx_t *ctx);
|
|
};
|
|
|
|
enum
|
|
{
|
|
E_ILL, // Ill-formed
|
|
E_IMP, // Not implemented
|
|
E_ACC, // Invalid access
|
|
E_SYS, // Supervisor only
|
|
E_SHT, // Shutdown instruction
|
|
NEXCPTS
|
|
};
|
|
|
|
void dumpregs(ctx_t *ctx);
|
|
|
|
static inline void _except(ctx_t *ctx, int code, char *str)
|
|
{
|
|
log("Exception %d - %s\n", code, str);
|
|
dumpregs(ctx);
|
|
exit(code);
|
|
}
|
|
|
|
void decode(ctx_t *ctx);
|
|
|