123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307 |
- /* Copyright (C) 2016 Jeremiah Orians
- * This file is part of stage0.
- *
- * stage0 is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * stage0 is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with stage0. If not, see <http://www.gnu.org/licenses/>.
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdint.h>
- #include <stdbool.h>
- #include <string.h>
- #ifdef VM256
- typedef __int512_t signed_wide_register;
- typedef __uint512_t unsigned_wide_register;
- typedef __int256_t signed_vm_register;
- typedef __uint256_t unsigned_vm_register;
- #define umax 256
- #define imax 255
- #define reg_size 32
- #elif VM128
- typedef __int256_t signed_wide_register;
- typedef __uint256_t unsigned_wide_register;
- typedef __int128_t signed_vm_register;
- typedef __uint128_t unsigned_vm_register;
- #define umax 128
- #define imax 127
- #define reg_size 16
- #elif VM64
- typedef __int128_t signed_wide_register;
- typedef __uint128_t unsigned_wide_register;
- typedef int64_t signed_vm_register;
- typedef uint64_t unsigned_vm_register;
- #define umax 64
- #define imax 63
- #define reg_size 8
- #elif VM32
- typedef int64_t signed_wide_register;
- typedef uint64_t unsigned_wide_register;
- typedef int32_t signed_vm_register;
- typedef uint32_t unsigned_vm_register;
- #define umax 32
- #define imax 31
- #define reg_size 4
- #else
- typedef int32_t signed_wide_register;
- typedef uint32_t unsigned_wide_register;
- typedef int16_t signed_vm_register;
- typedef uint16_t unsigned_vm_register;
- #define umax 16
- #define imax 15
- #define reg_size 2
- #endif
- /* Virtual machine state */
- struct lilith
- {
- uint8_t *memory;
- size_t amount_of_Ram;
- unsigned_vm_register reg[16];
- unsigned_vm_register ip;
- bool halted;
- bool exception;
- };
- /* Unpacked instruction */
- struct Instruction
- {
- unsigned_vm_register ip;
- uint8_t raw0, raw1, raw2, raw3;
- char opcode[3];
- uint32_t raw_XOP;
- char XOP[6];
- char operation[9];
- int16_t raw_Immediate;
- char Immediate[7];
- uint32_t HAL_CODE;
- uint8_t reg0;
- uint8_t reg1;
- uint8_t reg2;
- uint8_t reg3;
- bool invalid;
- };
- /* Prototypes for functions in vm_instructions.c*/
- void vm_FOPEN_READ(struct lilith* vm);
- void vm_FOPEN_WRITE(struct lilith* vm);
- void vm_FCLOSE(struct lilith* vm);
- void vm_FSEEK(struct lilith* vm);
- void vm_REWIND(struct lilith* vm);
- void vm_FGETC(struct lilith* vm);
- void vm_FPUTC(struct lilith* vm);
- void vm_HAL_MEM(struct lilith* vm);
- void ADD_CI(struct lilith* vm, struct Instruction* c);
- void ADD_CO(struct lilith* vm, struct Instruction* c);
- void ADD_CIO(struct lilith* vm, struct Instruction* c);
- void ADDU_CI(struct lilith* vm, struct Instruction* c);
- void ADDU_CO(struct lilith* vm, struct Instruction* c);
- void ADDU_CIO(struct lilith* vm, struct Instruction* c);
- void SUB_BI(struct lilith* vm, struct Instruction* c);
- void SUB_BO(struct lilith* vm, struct Instruction* c);
- void SUB_BIO(struct lilith* vm, struct Instruction* c);
- void SUBU_BI(struct lilith* vm, struct Instruction* c);
- void SUBU_BO(struct lilith* vm, struct Instruction* c);
- void SUBU_BIO(struct lilith* vm, struct Instruction* c);
- void MULTIPLY(struct lilith* vm, struct Instruction* c);
- void MULTIPLYU(struct lilith* vm, struct Instruction* c);
- void DIVIDE(struct lilith* vm, struct Instruction* c);
- void DIVIDEU(struct lilith* vm, struct Instruction* c);
- void MUX(struct lilith* vm, struct Instruction* c);
- void NMUX(struct lilith* vm, struct Instruction* c);
- void SORT(struct lilith* vm, struct Instruction* c);
- void SORTU(struct lilith* vm, struct Instruction* c);
- void ADD(struct lilith* vm, struct Instruction* c);
- void ADDU(struct lilith* vm, struct Instruction* c);
- void SUB(struct lilith* vm, struct Instruction* c);
- void SUBU(struct lilith* vm, struct Instruction* c);
- void CMP(struct lilith* vm, struct Instruction* c);
- void CMPU(struct lilith* vm, struct Instruction* c);
- void MUL(struct lilith* vm, struct Instruction* c);
- void MULH(struct lilith* vm, struct Instruction* c);
- void MULU(struct lilith* vm, struct Instruction* c);
- void MULUH(struct lilith* vm, struct Instruction* c);
- void DIV(struct lilith* vm, struct Instruction* c);
- void MOD(struct lilith* vm, struct Instruction* c);
- void DIVU(struct lilith* vm, struct Instruction* c);
- void MODU(struct lilith* vm, struct Instruction* c);
- void MAX(struct lilith* vm, struct Instruction* c);
- void MAXU(struct lilith* vm, struct Instruction* c);
- void MIN(struct lilith* vm, struct Instruction* c);
- void MINU(struct lilith* vm, struct Instruction* c);
- void AND(struct lilith* vm, struct Instruction* c);
- void OR(struct lilith* vm, struct Instruction* c);
- void XOR(struct lilith* vm, struct Instruction* c);
- void NAND(struct lilith* vm, struct Instruction* c);
- void NOR(struct lilith* vm, struct Instruction* c);
- void XNOR(struct lilith* vm, struct Instruction* c);
- void MPQ(struct lilith* vm, struct Instruction* c);
- void LPQ(struct lilith* vm, struct Instruction* c);
- void CPQ(struct lilith* vm, struct Instruction* c);
- void BPQ(struct lilith* vm, struct Instruction* c);
- void SAL(struct lilith* vm, struct Instruction* c);
- void SAR(struct lilith* vm, struct Instruction* c);
- void SL0(struct lilith* vm, struct Instruction* c);
- void SR0(struct lilith* vm, struct Instruction* c);
- void SL1(struct lilith* vm, struct Instruction* c);
- void SR1(struct lilith* vm, struct Instruction* c);
- void ROL(struct lilith* vm, struct Instruction* c);
- void ROR(struct lilith* vm, struct Instruction* c);
- void LOADX(struct lilith* vm, struct Instruction* c);
- void LOADX8(struct lilith* vm, struct Instruction* c);
- void LOADXU8(struct lilith* vm, struct Instruction* c);
- void LOADX16(struct lilith* vm, struct Instruction* c);
- void LOADXU16(struct lilith* vm, struct Instruction* c);
- void LOADX32(struct lilith* vm, struct Instruction* c);
- void LOADXU32(struct lilith* vm, struct Instruction* c);
- void STOREX(struct lilith* vm, struct Instruction* c);
- void STOREX8(struct lilith* vm, struct Instruction* c);
- void STOREX16(struct lilith* vm, struct Instruction* c);
- void STOREX32(struct lilith* vm, struct Instruction* c);
- void NEG(struct lilith* vm, struct Instruction* c);
- void ABS(struct lilith* vm, struct Instruction* c);
- void NABS(struct lilith* vm, struct Instruction* c);
- void SWAP(struct lilith* vm, struct Instruction* c);
- void COPY(struct lilith* vm, struct Instruction* c);
- void MOVE(struct lilith* vm, struct Instruction* c);
- void BRANCH(struct lilith* vm, struct Instruction* c);
- void CALL(struct lilith* vm, struct Instruction* c);
- void READPC(struct lilith* vm, struct Instruction* c);
- void READSCID(struct lilith* vm, struct Instruction* c);
- void FALSE(struct lilith* vm, struct Instruction* c);
- void TRUE(struct lilith* vm, struct Instruction* c);
- void JSR_COROUTINE(struct lilith* vm, struct Instruction* c);
- void RET(struct lilith* vm, struct Instruction* c);
- void PUSHPC(struct lilith* vm, struct Instruction* c);
- void POPPC(struct lilith* vm, struct Instruction* c);
- void ADDI(struct lilith* vm, struct Instruction* c);
- void ADDUI(struct lilith* vm, struct Instruction* c);
- void SUBI(struct lilith* vm, struct Instruction* c);
- void SUBUI(struct lilith* vm, struct Instruction* c);
- void CMPI(struct lilith* vm, struct Instruction* c);
- void LOAD(struct lilith* vm, struct Instruction* c);
- void LOAD8(struct lilith* vm, struct Instruction* c);
- void LOADU8(struct lilith* vm, struct Instruction* c);
- void LOAD16(struct lilith* vm, struct Instruction* c);
- void LOADU16(struct lilith* vm, struct Instruction* c);
- void LOAD32(struct lilith* vm, struct Instruction* c);
- void LOADU32(struct lilith* vm, struct Instruction* c);
- void CMPUI(struct lilith* vm, struct Instruction* c);
- void STORE(struct lilith* vm, struct Instruction* c);
- void STORE8(struct lilith* vm, struct Instruction* c);
- void STORE16(struct lilith* vm, struct Instruction* c);
- void STORE32(struct lilith* vm, struct Instruction* c);
- void JUMP_C(struct lilith* vm, struct Instruction* c);
- void JUMP_B(struct lilith* vm, struct Instruction* c);
- void JUMP_O(struct lilith* vm, struct Instruction* c);
- void JUMP_G(struct lilith* vm, struct Instruction* c);
- void JUMP_GE(struct lilith* vm, struct Instruction* c);
- void JUMP_E(struct lilith* vm, struct Instruction* c);
- void JUMP_NE(struct lilith* vm, struct Instruction* c);
- void JUMP_LE(struct lilith* vm, struct Instruction* c);
- void JUMP_L(struct lilith* vm, struct Instruction* c);
- void JUMP_Z(struct lilith* vm, struct Instruction* c);
- void JUMP_NZ(struct lilith* vm, struct Instruction* c);
- void CALLI(struct lilith* vm, struct Instruction* c);
- void LOADI(struct lilith* vm, struct Instruction* c);
- void LOADUI(struct lilith* vm, struct Instruction* c);
- void SALI(struct lilith* vm, struct Instruction* c);
- void SARI(struct lilith* vm, struct Instruction* c);
- void SL0I(struct lilith* vm, struct Instruction* c);
- void SR0I(struct lilith* vm, struct Instruction* c);
- void SL1I(struct lilith* vm, struct Instruction* c);
- void SR1I(struct lilith* vm, struct Instruction* c);
- void LOADR(struct lilith* vm, struct Instruction* c);
- void LOADR8(struct lilith* vm, struct Instruction* c);
- void LOADRU8(struct lilith* vm, struct Instruction* c);
- void LOADR16(struct lilith* vm, struct Instruction* c);
- void LOADRU16(struct lilith* vm, struct Instruction* c);
- void LOADR32(struct lilith* vm, struct Instruction* c);
- void LOADRU32(struct lilith* vm, struct Instruction* c);
- void STORER(struct lilith* vm, struct Instruction* c);
- void STORER8(struct lilith* vm, struct Instruction* c);
- void STORER16(struct lilith* vm, struct Instruction* c);
- void STORER32(struct lilith* vm, struct Instruction* c);
- void JUMP(struct lilith* vm, struct Instruction* c);
- void JUMP_P(struct lilith* vm, struct Instruction* c);
- void JUMP_NP(struct lilith* vm, struct Instruction* c);
- void CMPJUMPI_G(struct lilith* vm, struct Instruction* c);
- void CMPJUMPI_GE(struct lilith* vm, struct Instruction* c);
- void CMPJUMPI_E(struct lilith* vm, struct Instruction* c);
- void CMPJUMPI_NE(struct lilith* vm, struct Instruction* c);
- void CMPJUMPI_LE(struct lilith* vm, struct Instruction* c);
- void CMPJUMPI_L(struct lilith* vm, struct Instruction* c);
- void CMPJUMPUI_G(struct lilith* vm, struct Instruction* c);
- void CMPJUMPUI_GE(struct lilith* vm, struct Instruction* c);
- void CMPJUMPUI_LE(struct lilith* vm, struct Instruction* c);
- void CMPJUMPUI_L(struct lilith* vm, struct Instruction* c);
- void CMPSKIPI_G(struct lilith* vm, struct Instruction* c);
- void CMPSKIPI_GE(struct lilith* vm, struct Instruction* c);
- void CMPSKIPI_E(struct lilith* vm, struct Instruction* c);
- void CMPSKIPI_NE(struct lilith* vm, struct Instruction* c);
- void CMPSKIPI_LE(struct lilith* vm, struct Instruction* c);
- void CMPSKIPI_L(struct lilith* vm, struct Instruction* c);
- void CMPSKIPUI_G(struct lilith* vm, struct Instruction* c);
- void CMPSKIPUI_GE(struct lilith* vm, struct Instruction* c);
- void CMPSKIPUI_LE(struct lilith* vm, struct Instruction* c);
- void CMPSKIPUI_L(struct lilith* vm, struct Instruction* c);
- void PUSHR(struct lilith* vm, struct Instruction* c);
- void PUSH8(struct lilith* vm, struct Instruction* c);
- void PUSH16(struct lilith* vm, struct Instruction* c);
- void PUSH32(struct lilith* vm, struct Instruction* c);
- void POPR(struct lilith* vm, struct Instruction* c);
- void POP8(struct lilith* vm, struct Instruction* c);
- void POPU8(struct lilith* vm, struct Instruction* c);
- void POP16(struct lilith* vm, struct Instruction* c);
- void POPU16(struct lilith* vm, struct Instruction* c);
- void POP32(struct lilith* vm, struct Instruction* c);
- void POPU32(struct lilith* vm, struct Instruction* c);
- void ANDI(struct lilith* vm, struct Instruction* c);
- void ORI(struct lilith* vm, struct Instruction* c);
- void XORI(struct lilith* vm, struct Instruction* c);
- void NANDI(struct lilith* vm, struct Instruction* c);
- void NORI(struct lilith* vm, struct Instruction* c);
- void XNORI(struct lilith* vm, struct Instruction* c);
- void NOT(struct lilith* vm, struct Instruction* c);
- void CMPSKIP_G(struct lilith* vm, struct Instruction* c);
- void CMPSKIP_GE(struct lilith* vm, struct Instruction* c);
- void CMPSKIP_E(struct lilith* vm, struct Instruction* c);
- void CMPSKIP_NE(struct lilith* vm, struct Instruction* c);
- void CMPSKIP_LE(struct lilith* vm, struct Instruction* c);
- void CMPSKIP_L(struct lilith* vm, struct Instruction* c);
- void CMPSKIPU_G(struct lilith* vm, struct Instruction* c);
- void CMPSKIPU_GE(struct lilith* vm, struct Instruction* c);
- void CMPSKIPU_LE(struct lilith* vm, struct Instruction* c);
- void CMPSKIPU_L(struct lilith* vm, struct Instruction* c);
- void CMPJUMP_G(struct lilith* vm, struct Instruction* c);
- void CMPJUMP_GE(struct lilith* vm, struct Instruction* c);
- void CMPJUMP_E(struct lilith* vm, struct Instruction* c);
- void CMPJUMP_NE(struct lilith* vm, struct Instruction* c);
- void CMPJUMP_LE(struct lilith* vm, struct Instruction* c);
- void CMPJUMP_L(struct lilith* vm, struct Instruction* c);
- void CMPJUMPU_G(struct lilith* vm, struct Instruction* c);
- void CMPJUMPU_GE(struct lilith* vm, struct Instruction* c);
- void CMPJUMPU_LE(struct lilith* vm, struct Instruction* c);
- void CMPJUMPU_L(struct lilith* vm, struct Instruction* c);
- /* Prototypes for functions in vm_decode.c*/
- struct lilith* create_vm(size_t size);
- void destroy_vm(struct lilith* vm);
- void read_instruction(struct lilith* vm, struct Instruction *current);
- void eval_instruction(struct lilith* vm, struct Instruction* current);
- void outside_of_world(struct lilith* vm, unsigned_vm_register place, char* message);
- /* Allow tape names to be effectively changed */
- char* tape_01_name;
- char* tape_02_name;
|