140 lines
3.4 KiB
C
140 lines
3.4 KiB
C
#ifndef CREOLE_H
|
|
#define CREOLE_H
|
|
/* Copyright (c) 2023 Peter McGoron <code@mcgoron.com>
|
|
|
|
Permission to use, copy, modify, and/or distribute this software for any
|
|
purpose with or without fee is hereby granted, provided that the above
|
|
copyright notice and this permission notice appear in all copies.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
|
|
|
|
#include <limits.h>
|
|
#include <stddef.h>
|
|
|
|
#define CREOLE_MAJOR 0
|
|
#define CREOLE_MINOR 2
|
|
#define CREOLE_BUGFIX 0
|
|
|
|
#ifndef CREOLE_WORD
|
|
# define CREOLE_WORD unsigned int
|
|
# define CREOLE_WORD_MAX UINT_MAX
|
|
# define CREOLE_SIGNED_WORD int
|
|
# define CREOLE_SIGNED_MAX INT_MAX
|
|
#endif
|
|
|
|
#define CREOLE_MAX_ARG 3
|
|
|
|
typedef CREOLE_WORD creole_word;
|
|
typedef CREOLE_SIGNED_WORD creole_signed;
|
|
|
|
enum creole_opcode {
|
|
CREOLE_NOOP = 0,
|
|
CREOLE_PUSH = 1,
|
|
CREOLE_POP = 2,
|
|
CREOLE_ADD = 3,
|
|
CREOLE_MUL = 4,
|
|
CREOLE_DIV = 5,
|
|
CREOLE_SYS = 6,
|
|
CREOLE_JL = 7,
|
|
CREOLE_JLE = 8,
|
|
CREOLE_JE = 9,
|
|
CREOLE_JNE = 10,
|
|
CREOLE_DB = 11,
|
|
CREOLE_READ_ADC = 12,
|
|
CREOLE_READ_DAC = 13,
|
|
CREOLE_WRITE_DAC = 14,
|
|
CREOLE_SLEEP = 15,
|
|
CREOLE_CLOOP_READ = 16,
|
|
CREOLE_CLOOP_WRITE = 17,
|
|
CREOLE_WF_LOAD = 18,
|
|
CREOLE_WF_ARM = 19,
|
|
CREOLE_SENDVAL = 20,
|
|
CREOLE_SENDDAT = 21,
|
|
CREOLE_WF_DISARM = 22,
|
|
CREOLE_OPCODE_LEN
|
|
};
|
|
|
|
enum creole_word_flag {
|
|
CREOLE_IMMEDIATE,
|
|
CREOLE_REGISTER,
|
|
CREOLE_WORD_FLAGS_LEN
|
|
};
|
|
|
|
enum creole_compiler_ret {
|
|
CREOLE_COMPILE_OK,
|
|
CREOLE_OPCODE_READ_ERROR,
|
|
CREOLE_OPCODE_MALFORMED,
|
|
CREOLE_ARG_READ_ERROR,
|
|
CREOLE_ARG_MALFORMED,
|
|
CREOLE_LAST_READ_ERROR,
|
|
CREOLE_LAST_MALFORMED,
|
|
CREOLE_DATA_OVERFLOW,
|
|
CREOLE_TYPE_ERROR,
|
|
CREOLE_PROGRAM_OVERFLOW,
|
|
CREOLE_COMPILE_RET_LEN
|
|
};
|
|
|
|
enum creole_run_ret {
|
|
CREOLE_STEP_CONTINUE,
|
|
CREOLE_STEP_SYSCALL,
|
|
CREOLE_STEP_STOP,
|
|
CREOLE_STACK_OVERFLOW,
|
|
CREOLE_STACK_UNDERFLOW,
|
|
CREOLE_RUN_DECODE_ERROR,
|
|
CREOLE_REGISTER_OVERFLOW,
|
|
CREOLE_STEP_UNKNOWN_OPCODE,
|
|
CREOLE_DIV_BY_ZERO,
|
|
CREOLE_STEP_HIGH_BIT_MALFORMED,
|
|
CREOLE_JUMP_OVERFLOW,
|
|
CREOLE_RUN_RET_LEN
|
|
};
|
|
|
|
struct creole_word {
|
|
int len;
|
|
int high_bits;
|
|
creole_word word;
|
|
};
|
|
|
|
struct creole_reader {
|
|
unsigned char *p;
|
|
size_t left;
|
|
};
|
|
|
|
struct creole_env {
|
|
struct creole_reader *dats;
|
|
size_t datlen;
|
|
|
|
creole_word *reg;
|
|
size_t reglen;
|
|
|
|
creole_word *stk;
|
|
size_t stkptr, stklen;
|
|
|
|
struct creole_reader r_current;
|
|
struct creole_reader r_start;
|
|
|
|
void *send_ctx;
|
|
};
|
|
|
|
int creole_decode(struct creole_reader *r, struct creole_word *w);
|
|
int creole_encode(creole_word i, unsigned encode_to, unsigned high_bits,
|
|
unsigned char buf[7]);
|
|
enum creole_compiler_ret creole_compile(struct creole_env *env);
|
|
|
|
enum creole_run_ret creole_reg_write(struct creole_env *env, unsigned reg,
|
|
creole_word w);
|
|
enum creole_run_ret creole_reg_read(struct creole_env *env, unsigned reg,
|
|
creole_word *w);
|
|
enum creole_run_ret creole_push(struct creole_env *env, creole_word w);
|
|
enum creole_run_ret creole_pop(struct creole_env *env, creole_word *w);
|
|
int creole_jump(struct creole_env *env, creole_word off);
|
|
enum creole_run_ret creole_step(struct creole_env *env, creole_word *sc);
|
|
|
|
#endif /* CREOLE_H */
|