Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "cryptovm.h"
- typedef unsigned int CDWORD; //32 bits
- typedef unsigned short CWORD; //16 bits
- typedef unsigned char CBYTE; //8 bits
- #define CRYPTOVM_FN(n) cryptovm_##n(CVM_STATE *state)
- #define CRYPTOVM_FN1(n) cryptovm_##n(CVM_STATE *state, void *a0)
- #define CRYPTOVM_FN2(n) cryptovm_##n(CVM_STATE *state, void *a0, void *a1)
- #define CRYPTOVM_CALL(n) cryptovm_##n(state)
- #define CRYPTOVM_ROTPRNG (state->rPRNG = ((start->rPRNG>>1)^ \
- (CDWORD)(0-(state->rPRNG&1)&0xD0103101)))
- static unsigned int chip8_prng_lfsr = 0xBC83E7A0;
- chip8_prng_lfsr = (chip8_prng_lfsr >> 1) ^
- (unsigned int)(0 - (chip8_prng_lfsr & 1) & 0xD0103101);
- #define CRYPTOVM_ALLOC(sz) ((void*)malloc((sz)))
- #define CRYPTOVM_FREE(ptr) (free((void*)(ptr))
- #define CRYPTOVM_ENTRYP (0x00000000) //entrypoint in memory
- #define CRYPTOVM_REGCOUNT (0x0F)
- #define CRYPTOVM_STACKDEPTH (0xFF)
- #define CRYPTOVM_RBIGSZ (1024 / sizeof(CDWORD)) //1kb
- #define CRYPTOVM_MEMSZ (10240 / sizeof(CDWORD)) //10kb
- #define CRYPTOVM_CHKFLG(f) (state->flags&(f))
- #define CRYPTOVM_FNONE (0x00000000)
- #define CRYPTOVM_FIARG (0x04000000)
- #define CRYPTOVM_FIOP (0x08000000)
- #define CRYPTOVM_FHALT (0x10000000)
- #define CRYPTOVM_FRESET (0x20000000)
- #define CRYPTOVM_FEXIT (0x40000000)
- #define CRYPTOVM_FINIT (0x80000000)
- /**INSTRUCTION LAYOUT******************/
- /* INSTRUCTION: 0x 0000 0000 */
- /* |||| |||| */
- /* MODIFIERS UPPER 4 BYTES ----/ |||| */
- /* OPCODE LOWER 4 BYTES ------------/ */
- /**************************************/
- /**INSTRUCTION MODIFIERS***************/
- /* MODIFIERS: 0x0000 */
- /* ARG0 TYPE >> 12 (0x?000) */
- /* = 0xF000 NONE */
- /* & 0x1000 REGISTER */
- /* & 0x2000 IMMEDIATE */
- /* & 0x4000 PTR TO REG */
- /* & 0x8000 PTR TO IMM */
- /* ARG0 LENGTH >> 8 (0x0?00) */
- /* ARG1 TYPE >> 4 (0x00?0) */
- /* = 0x00F0 NONE */
- /* & 0x0010 REGISTER */
- /* & 0x0020 IMMEDIATE */
- /* & 0x0040 PTR TO REG */
- /* & 0x0080 PTR TO IMM */
- /* ARG1 LENGTH >> 0 (0x000?) */
- /**************************************/
- #define CRYPTOVM_IGETARG(iptr,an,al) \
- (((DWORD*)(iptr))[(an)+1])
- #define CRYPTOVM_MODCHKFLG(f) (((mods)&(f))>0)
- #define CRYPTOVM_MODA0REG (0x1000)
- #define CRYPTOVM_MODA0IMM (0x2000)
- #define CRYPTOVM_MODA0P2R (0x4000)
- #define CRYPTOVM_MODA0P2I (0x8000)
- #define CRYPTOVM_MODA0LEN (((mods)&0x0F00) >> 8)
- #define CRYPTOVM_MODA1REG (0x0010)
- #define CRYPTOVM_MODA1IMM (0x0020)
- #define CRYPTOVM_MODA1P2R (0x0040)
- #define CRYPTOVM_MODA1P2I (0x0080)
- #define CRYPTOVM_MODA1LEN ((mods)&0x000F)
- #define CRYPTOVM_MODISA0REG (CRYPTOVM_MODCHKFLG(CRYPTOVMP_MODA0REG))
- #define CRYPTOVM_MODISA0REGEX (CRYPTOVM_MODISA0REG & (CRYPTOVM_MODCHKFLG(CRYPTOVMP_MODA0P2R)))
- #define CRYPTOVM_MODISA0IMM (CRYPTOVM_MODCHKFLG(CRYPTOVMP_MODA0IMM))
- #define CRYPTOVM_MODISA0IMMEX (CRYPTOVM_MODISA0IMM & (CRYPTOVM_MODCHKFLG(CRYPTOVMP_MODA0P2I)))
- #define CRYPTOVM_MODISA0P2R (CRYPTOVM_MODCHKFLG(CRYPTOVMP_MODA0P2R))
- #define CRYPTOVM_MODISA0P2I (CRYPTOVM_MODCHKFLG(CRYPTOVMP_MODA0P2I))
- #define CRYPTOVM_MODISA1REG (CRYPTOVM_MODCHKFLG(CRYPTOVMP_MODA1REG))
- #define CRYPTOVM_MODISA1REGEX (CRYPTOVM_MODISA1REG & (CRYPTOVM_MODCHKFLG(CRYPTOVMP_MODA1P2R)))
- #define CRYPTOVM_MODISA1IMM (CRYPTOVM_MODCHKFLG(CRYPTOVMP_MODA1IMM))
- #define CRYPTOVM_MODISA1IMMEX (CRYPTOVM_MODISA1IMM & (CRYPTOVM_MODCHKFLG(CRYPTOVMP_MODA1P2I)))
- #define CRYPTOVM_MODISA1P2R (CRYPTOVM_MODCHKFLG(CRYPTOVMP_MODA1P2R))
- #define CRYPTOVM_MODISA1P2I (CRYPTOVM_MODCHKFLG(CRYPTOVMP_MODA1P2I))
- #define CRYPTOVM_IHALT (0x0000)
- #define CRYPTOVM_IMOV (0x0001)
- typedef struct
- {
- CDWORD pc;
- CDWORD rPRNG;
- CDWORD r[CRYPTOVM_REGCOUNT];
- CDWORD rBIG[CRYPTOVM_RBIGSZ];
- CDWORD stack[CRYPTOVM_STACKDEPTH];
- CDWORD flags;
- CDWORD *mem;
- } CVM_STATE;
- void CRYPTOVM_FN(reset)
- {
- CDWORD i = 0;
- state->pc = CRYPTOVM_ENTRYP;
- state->rPRNG = 0xBC83E7A0;
- for (i = 0; i < CRYPTOVM_REGCOUNT; i++) state->r[i] = 0x00000000;
- for (i = 0; i < CRYPTOVM_RBIGSZ; i++) state->rBIG[i] = 0x00000000;
- for (i = 0; i < CRYPTOVM_STACKDEPTH; i++) state->stack[i] = 0x00000000;
- state->flags = CRYPTOVM_FINIT | CRYPTOVM_FRESET;
- }
- void CRYPTOVM_FN(init)
- {
- if (CRYPTOVM_CHKFLG(CRYPTOVM_FINIT) == 1) return;
- CRYPTOVM_CALL(reset);
- state->mem = CRYPTOVM_ALLOC(CRYPTOVM_MEMSZ);
- UINT i = 0;
- for (i = 0; i < CRYPTOVM_MEMSZ; i++) state->mem[i] = 0x00000000;
- }
- void CRYPTOVM_FN(exit)
- {
- if (CRYPTOVM_CHKFLG(CRYPTOVM_FINIT) == 0) return;
- CRYPTOVM_CALL(reset);
- state->flags = CRYPTOVM_FEXIT;
- CRYPTOVM_FREE(state->mem);
- }
- void CRYPTOVM_FN2(program)
- {
- if (CRYPTOVM_CHKFLG(CRYPTOVM_FINIT) == 0) return;
- if ((arg0==0) || (arg1==0)) return;
- CDWORD *prgm = (CDWORD*)arg0;
- CDWORD prgmsz = *((CDWORD*)arg1);
- if ((prgm==0) || (prgmsz==0)) return;
- CDWORD i = 0;
- for (i = 0; i < prgmsz; i++) state->mem[i] = prgm[i];
- }
- void CRYPTOVM_FN(step)
- {
- if (CRYPTOVM_CHKFLG(CRYPTOVM_FHALT) > 0) return;
- if (CRYPTOVM_CHKFLG(CRYPTOVM_FINIT) == 0) return;
- CDWORD *iptr = &state->mem[state->pc];
- CDWORD inst = *iptr;
- CWORD opcode = (CWORD)inst;
- CWORD mods = (CWORD)(inst>>15);
- CDWORD addr0 = 0, addr1 = 0;
- CDWORD reg0 = 0, reg1 = 0, imm0 = 0, imm1 = 0;
- CBYTE arg0len = CRYPTOVM_MODA0LEN();
- CBYTE arg1len = CRYPTOVM_MODA1LEN();
- if (CRYPTOVM_MODCHKFLG(CRYPTOVMP_MODA0REG)) {
- reg0 = state->r[CRYPTOVM_IGETARG(iptr,0,arg0len)];
- } else if (CRYPTOVM_MODCHKFLG(CRYPTOVMP_MODA0IMM)) {
- imm0 = CRYPTOVM_IGETARG(iptr,0,arg0len);
- } else if (CRYPTOVM_MODCHKFLG(CRYPTOVMP_MODA0P2R)) {
- addr0 = CRYPTOVM_IGETARG(iptr,0,arg0len);
- reg0 = state->r[state->mem[addr0]];
- } else if (CRYPTOVM_MODCHKFLG(CRYPTOVMP_MODA0P2I)) {
- addr0 = CRYPTOVM_IGETARG(iptr,0,arg0len);
- imm0 = state->mem[addr0];
- }
- if (CRYPTOVM_MODCHKFLG(CRYPTOVMP_MODA1REG)) {
- reg1 = state->r[CRYPTOVM_IGETARG(iptr,1,arg1len)];
- } else if (CRYPTOVM_MODCHKFLG(CRYPTOVMP_MODA1IMM)) {
- imm1 = CRYPTOVM_IGETARG(iptr,1,arg1len);
- } else if (CRYPTOVM_MODCHKFLG(CRYPTOVMP_MODA1P2R)) {
- addr1 = CRYPTOVM_IGETARG(iptr,1,arg1len);
- reg1 = state->r[state->mem[addr1]];
- } else if (CRYPTOVM_MODCHKFLG(CRYPTOVMP_MODA1P2I)) {
- addr1 = CRYPTOVM_IGETARG(iptr,1,arg1len);
- imm1 = state->mem[addr1];
- }
- CBYTE calcLen = (arg0len + arg1len);
- calcLen = (calcLen>0)?1:calcLen;
- state->pc += calcLen;
- //WARNING: pc at next instruction already
- switch (op)
- {
- case CRYPTOVM_IHALT:
- state->flags |= CRYPTOVM_FHALT;
- break;
- case CRYPTOVM_IMOV:
- if (CRYPTOVM_MODISA0REGEX) {
- if (CRPTOVM_MODISA1REGEX) {
- state->r[reg0] = state->r[reg1];
- } else if (CRYPOVMP_MODISA1IMMEX) {
- state->r[reg0] = imm1;
- }
- } else if (CRYPTOVM_MODISA0P2I) {
- if (CRPTOVM_MODISA1REGEX) {
- state->mem[addr0] = state->r[reg1];
- } else if (CRYPOVMP_MODISA1IMMEX) {
- state->mem[addr0] = imm1;
- }
- } else {
- state->flags |= (CRYPTOVM_FHALT | CRYPTOVM_FIARG);
- break;
- }
- break;
- default:
- state->flags |= (CRYPTOVM_FHALT | CRYPTOVM_FIOP);
- break;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement