1
0
mirror of https://gitlab.os-k.eu/os-k-team/kvisc.git synced 2023-08-25 14:05:46 +02:00
kvisc/vm/pc/arch.h
2019-06-07 22:23:38 +02:00

119 lines
2.7 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.
#ifndef _ARCH_H
#define _ARCH_H
#define dev_t stddev_t
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <stdarg.h>
#include <limits.h>
#undef dev_t
#define packed __attribute__ ((__packed__))
#define static_assert _Static_assert
#define alignof _Alignof
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;
typedef struct dev_t dev_t;
void log(const char *, ...);
void vlog(const char *, va_list);
struct ctx_t
{
reg_t *r;
instr_t *i;
// Memory and memory size
ushort *mp;
ulong mz;
// Read next instruction
ushort (*get)(ctx_t *ctx);
// For disassembly
FILE *disf;
// Step by step
int step;
// Devices list head
dev_t *dh;
};
#define R(X) ctx->r[X].val
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')))); }
enum
{
E_SHT, // Shutdown instruction
E_IMP, // Not implemented
E_ILL, // Ill-formed
E_ACC, // Invalid access
E_SYS, // Supervisor only
E_ALI, // Alignment error
E_STA, // Stack misalignment
E_STU, // Stack underflow
NEXCPTS
};
void dumpregs(ctx_t *);
void dumpinstr(ctx_t *, ulong, uint, ushort, acc_t *, acc_t *);
void dumpmem(ctx_t *, ulong, ulong);
void _except(ctx_t *, int, char *, ...) __attribute__((__noreturn__));
void decode(ctx_t *ctx);
#define MEMOFF (1 * 1024 * 1024)
#define MEMSIZE (16 * 1024 * 1024) // 16MB
#define addr2real(p) (((p) - MEMOFF) / 2)
#define real2addr(p) (((p) + MEMOFF) / 2)
// Address of boot firmware stack
#define FWSTACK (MEMOFF * 2) // 2MB
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);
#include <pc/regs.h>
#include <pc/decd.h>
#include <in/arch_i.h>
extern reg_t arch_r[NREGS];
extern instr_t arch_i[NINSTRS];
#endif