all repos — xyw @ 4e816282d361f040ba2e89d9d9afe8875d126e51

A minimal virtual machine and assembler for terminals.

xyw.h

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
#ifndef XYW_H
#define XYW_H

#define XYW_MODE_X 0x20
#define XYW_MODE_Y 0x40
#define XYW_MODE_W 0x80
#define XYW_TOTAL_INSTRUCTIONS 0x20
#define XYW_TOTAL_DEVICES 0x10

// clang-format off
#define XYW_PEEKW(d) (*(d) << 8 | (d)[1])
#define XYW_POKEW(d, v) { *(d) = (v) >> 8; (d)[1] = (v); }
// clang-format on

/* system device */
#define XYW_SYSTEM_STATE 0xff00
#define XYW_SYSTEM_ERROR 0xff01
#define XYW_SYSTEM_S 0xff02
#define XYW_SYSTEM_U 0xff03
#define XYW_SYSTEM_PAGE 0xff04
#define XYW_SYSTEM_ON_ERROR 0xff05

/* console device */
#define XYW_CONSOLE_BASE 0xff10
#define XYW_CONSOLE_INPUT 0xff10
#define XYW_CONSOLE_OUTPUT 0xff11
#define XYW_CONSOLE_ERROR 0xff12
#define XYW_CONSOLE_ON_KEYPRESS 0xff13

typedef unsigned char xyw_byte;
typedef unsigned short xyw_word;

typedef xyw_byte (*xyw_read)(xyw_byte addr, xyw_byte *error);
typedef void (*xyw_write)(xyw_byte addr, xyw_byte val, xyw_byte *error);
typedef xyw_word (*xyw_readw)(xyw_byte addr, xyw_byte *error);
typedef void (*xyw_writew)(xyw_byte addr, xyw_word val, xyw_byte *error);

typedef struct xyw_device
{
    xyw_byte *data;
    xyw_read read;
    xyw_write write;
    xyw_readw readw;
    xyw_writew writew;
} xyw_device;

// clang-format off
#define xyw_dbg(...) do { if (xyw_debug) fprintf(stdout, __VA_ARGS__); } while(0)
// clang-format on

extern const char xyw_instructions[][4];
extern int xyw_debug;
extern xyw_device xyw_devices[XYW_TOTAL_DEVICES];

int xyw_assemble(const char *input, const char *output);
int xyw_run(const char *input);

#endif // XYW_H