File indexing completed on 2025-02-02 04:26:02

0001 /* Copyright 2015 the unarr project authors (see AUTHORS file).
0002    License: LGPLv3 */
0003 
0004 /* adapted from https://code.google.com/p/theunarchiver/source/browse/XADMaster/RARVirtualMachine.h */
0005 
0006 #ifndef rar_vm_h
0007 #define rar_vm_h
0008 
0009 #include <stdint.h>
0010 #include <stdbool.h>
0011 
0012 #define RARProgramMemorySize 0x40000
0013 #define RARProgramMemoryMask (RARProgramMemorySize - 1)
0014 #define RARProgramWorkSize 0x3c000
0015 #define RARProgramGlobalSize 0x2000
0016 #define RARProgramSystemGlobalAddress RARProgramWorkSize
0017 #define RARProgramSystemGlobalSize 64
0018 #define RARProgramUserGlobalAddress (RARProgramSystemGlobalAddress + RARProgramSystemGlobalSize)
0019 #define RARProgramUserGlobalSize (RARProgramGlobalSize - RARProgramSystemGlobalSize)
0020 #define RARRuntimeMaxInstructions 250000000
0021 
0022 #define RARRegisterAddressingMode(n) (0 + (n))
0023 #define RARRegisterIndirectAddressingMode(n) (8 + (n))
0024 #define RARIndexedAbsoluteAddressingMode(n) (16 + (n))
0025 #define RARAbsoluteAddressingMode 24
0026 #define RARImmediateAddressingMode 25
0027 #define RARNumberOfAddressingModes 26
0028 
0029 typedef struct RARVirtualMachine RARVirtualMachine;
0030 
0031 struct RARVirtualMachine {
0032     uint32_t registers[8];
0033     uint8_t memory[RARProgramMemorySize + sizeof(uint32_t) /* overflow sentinel */];
0034 };
0035 
0036 typedef struct RARProgram_s RARProgram;
0037 
0038 /* Program building */
0039 
0040 enum {
0041     RARMovInstruction = 0,
0042     RARCmpInstruction = 1,
0043     RARAddInstruction = 2,
0044     RARSubInstruction = 3,
0045     RARJzInstruction = 4,
0046     RARJnzInstruction = 5,
0047     RARIncInstruction = 6,
0048     RARDecInstruction = 7,
0049     RARJmpInstruction = 8,
0050     RARXorInstruction = 9,
0051     RARAndInstruction = 10,
0052     RAROrInstruction = 11,
0053     RARTestInstruction = 12,
0054     RARJsInstruction = 13,
0055     RARJnsInstruction = 14,
0056     RARJbInstruction = 15,
0057     RARJbeInstruction = 16,
0058     RARJaInstruction = 17,
0059     RARJaeInstruction = 18,
0060     RARPushInstruction = 19,
0061     RARPopInstruction = 20,
0062     RARCallInstruction = 21,
0063     RARRetInstruction = 22,
0064     RARNotInstruction = 23,
0065     RARShlInstruction = 24,
0066     RARShrInstruction = 25,
0067     RARSarInstruction = 26,
0068     RARNegInstruction = 27,
0069     RARPushaInstruction = 28,
0070     RARPopaInstruction = 29,
0071     RARPushfInstruction = 30,
0072     RARPopfInstruction = 31,
0073     RARMovzxInstruction = 32,
0074     RARMovsxInstruction = 33,
0075     RARXchgInstruction = 34,
0076     RARMulInstruction = 35,
0077     RARDivInstruction = 36,
0078     RARAdcInstruction = 37,
0079     RARSbbInstruction = 38,
0080     RARPrintInstruction = 39,
0081     RARNumberOfInstructions = 40,
0082 };
0083 
0084 RARProgram *RARCreateProgram();
0085 void RARDeleteProgram(RARProgram *prog);
0086 bool RARProgramAddInstr(RARProgram *prog, uint8_t instruction, bool bytemode);
0087 bool RARSetLastInstrOperands(RARProgram *prog, uint8_t addressingmode1, uint32_t value1, uint8_t addressingmode2, uint32_t value2);
0088 bool RARIsProgramTerminated(RARProgram *prog);
0089 
0090 /* Execution */
0091 
0092 bool RARExecuteProgram(RARVirtualMachine *vm, RARProgram *prog);
0093 
0094 /* Memory and register access (convenience) */
0095 
0096 void RARSetVirtualMachineRegisters(RARVirtualMachine *vm, uint32_t registers[8]);
0097 uint32_t RARVirtualMachineRead32(RARVirtualMachine *vm, uint32_t address);
0098 void RARVirtualMachineWrite32(RARVirtualMachine *vm, uint32_t address, uint32_t val);
0099 uint8_t RARVirtualMachineRead8(RARVirtualMachine *vm, uint32_t address);
0100 void RARVirtualMachineWrite8(RARVirtualMachine *vm, uint32_t address, uint8_t val);
0101 
0102 /* Instruction properties */
0103 
0104 int NumberOfRARInstructionOperands(uint8_t instruction);
0105 bool RARInstructionHasByteMode(uint8_t instruction);
0106 bool RARInstructionIsUnconditionalJump(uint8_t instruction);
0107 bool RARInstructionIsRelativeJump(uint8_t instruction);
0108 bool RARInstructionWritesFirstOperand(uint8_t instruction);
0109 bool RARInstructionWritesSecondOperand(uint8_t instruction);
0110 
0111 /* Program debugging */
0112 
0113 #ifndef NDEBUG
0114 void RARPrintProgram(RARProgram *prog);
0115 #endif
0116 
0117 #endif