1
0
mirror of https://gitlab.os-k.eu/os-k-team/kvisc.git synced 2023-08-25 14:05:46 +02:00
kvisc/karch/arch.h

116 lines
2.2 KiB
C
Raw Normal View History

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
#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
{
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)
2019-05-15 21:47:08 +02:00
enum { NOPRM, P_REG, P_IMM, P_MEM=4 };
2019-05-15 19:26:40 +02:00
struct instr_t
{
char *name;
2019-05-15 21:47:08 +02:00
char *full;
uint prm1;
uint prm2;
2019-05-15 19:26:40 +02:00
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);
2019-05-15 21:47:08 +02:00
#include "../instr/arch_i.h"