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/arch.h

161 lines
3.5 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
2019-05-29 16:57:22 +02:00
#include <ctype.h>
2019-05-15 19:26:40 +02:00
#include <stdio.h>
#include <stdlib.h>
2019-05-16 10:03:01 +02:00
#include <string.h>
#include <assert.h>
#include <stdarg.h>
2019-05-15 19:26:40 +02:00
#define log printf
2019-05-16 10:03:01 +02:00
#define vlog vprintf
2019-05-15 19:26:40 +02:00
#define packed __attribute__ ((__packed__))
#define static_assert _Static_assert
2019-05-16 10:03:01 +02:00
#define alignof _Alignof
2019-05-15 19:26:40 +02:00
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;
2019-05-16 16:48:45 +02:00
// A_REG is implicit
// A_MEM denotes a memory access
// A_OFF is A_MEM but a 16-bit offset is expected immediatly next
2019-05-22 18:39:46 +02:00
enum
{
A_REG = 0,
A_MEM8 = 0x7001,
A_MEM16 = 0x7002,
A_MEM32 = 0x7004,
A_MEM64 = 0x7008,
A_OFF8 = 0x7701,
A_OFF16 = 0x7702,
A_OFF32 = 0x7704,
A_OFF64 = 0x7708,
A_IMM16 = 0x7772,
A_IMM32 = 0x7774,
A_IMM64 = 0x7778
};
#define A_IS_MEM(x) ((x) >= A_MEM8 && (x) <= A_MEM64)
#define A_IS_OFF(x) ((x) >= A_OFF8 && (x) <= A_OFF64)
#define A_IS_IMM(x) ((x) >= A_IMM16)
#define A_IS_8(x) ((x) & 0xF == 1)
#define A_IS_16(x) ((x) & 0xF == 2)
#define A_IS_32(x) ((x) & 0xF == 3)
#define A_IS_64(x) ((x) & 0xF == 4)
2019-05-15 19:26:40 +02:00
struct acc_t
{
2019-05-22 18:39:46 +02:00
bool mem;
2019-05-29 16:57:22 +02:00
uint mlen;
short off;
2019-05-22 18:39:46 +02:00
2019-05-29 16:57:22 +02:00
uint type;
uint ilen;
2019-05-15 19:26:40 +02:00
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
2019-05-16 10:03:01 +02:00
ushort *mp;
2019-05-15 19:26:40 +02:00
ulong mz;
// Read next instruction
ushort (*get)(ctx_t *ctx);
2019-05-16 21:42:23 +02:00
// For disassembly
FILE *disf;
2019-05-15 19:26:40 +02:00
};
2019-05-29 16:57:22 +02:00
static inline ushort bswap16(ushort u)
{ return ((u<<8) | (u>>8)); }
static inline char getmempref(ushort len)
{ return (len==1?'b':(len==2?'w':(len==4?'l':(len==8?'q':'x')))); }
2019-05-15 19:26:40 +02:00
enum
{
2019-05-16 10:03:01 +02:00
E_SHT, // Shutdown instruction
2019-05-15 19:26:40 +02:00
E_IMP, // Not implemented
2019-05-16 10:03:01 +02:00
E_ILL, // Ill-formed
2019-05-15 19:26:40 +02:00
E_ACC, // Invalid access
E_SYS, // Supervisor only
2019-05-16 10:03:01 +02:00
E_ALI, // Alignment error
2019-05-16 16:57:59 +02:00
E_STK, // Stack error
2019-05-15 19:26:40 +02:00
NEXCPTS
};
2019-05-16 16:48:45 +02:00
void dumpregs(ctx_t *);
2019-05-29 16:57:22 +02:00
void dumpinstr(ctx_t *, ulong, uint, ushort, acc_t *, acc_t *);
2019-05-16 16:48:45 +02:00
void dumpmem(ctx_t *, ulong, ulong);
void dumpfwstack(ctx_t *);
2019-05-15 19:26:40 +02:00
2019-05-16 21:42:23 +02:00
void _except(ctx_t *, int, char *, ...);
2019-05-15 19:26:40 +02:00
2019-05-16 21:42:23 +02:00
void disasm(ctx_t *ctx);
2019-05-15 19:26:40 +02:00
void decode(ctx_t *ctx);
2019-05-16 10:03:01 +02:00
#define MEMOFF (1 * 1024 * 1024)
#define MEMSIZE (16 * 1024 * 1024) // 16MB
2019-05-22 18:39:46 +02:00
#define addr2real(p) (((p) - MEMOFF) / 2)
#define real2addr(p) (((p) + MEMOFF) / 2)
2019-05-16 10:03:01 +02:00
2019-05-16 16:48:45 +02:00
// Address of boot firmware stack
#define FWSTACK (MEMOFF * 2) // 2MB
2019-05-22 18:39:46 +02:00
ulong readmem8(ctx_t *, ulong addr);
ulong readmem16(ctx_t *, ulong addr);
ulong readmem32(ctx_t *, ulong addr);
ulong readmem64(ctx_t *, ulong addr);
ulong readmem(ctx_t *c, ulong addr, uint len);
void writemem8(ctx_t *, ulong val, ulong addr);
void writemem16(ctx_t *, ulong val, ulong addr);
void writemem32(ctx_t *, ulong val, ulong addr);
void writemem64(ctx_t *, ulong val, ulong addr);
void writemem(ctx_t *, ulong val, ulong addr, uint len);
2019-05-16 16:48:45 +02:00
2019-05-29 16:57:22 +02:00
#include "regs.h"
#include "instrs/arch_i.h"
extern reg_t arch_r[NREGS];
extern instr_t arch_i[NINSTRS];
2019-05-15 21:47:08 +02:00