C#实现的虚拟机完整代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;
using Microsoft.DirectX.DirectDraw;
using Microsoft.DirectX.DirectSound;
using Microsoft.DirectX.DirectInput;

namespace VM
{
    public partial class Form1 : Form
    {
        enum Opcode
        {
			nop = 0,
            end = 1,

            mov1 = 2,
            mov2 = 3,
            mov4 = 4,
            mov8 = 5,

            z2r = 6,
            r2z = 7,
            z2char = 8,
            char2z = 9,

            add4 = 10,
            add8 = 11,
            sub4 = 12,
            sub8 = 13,
            mul4 = 14,
            mul8 = 15,
            div4 = 16,
            div8 = 17,
            mod = 18,

            and1 = 19,
            and4 = 20,
            or1 = 21,
            or4 = 22,
            xor1 = 23,
            xor4 = 24,
            not1 = 25,
            not4 = 26,

            shl = 27,
            shr = 28,

            eq1 = 29,
            eq2 = 30,
            eq4 = 31,
            eq8 = 32,
            lt4 = 33,
            lt8 = 34,
            gt4 = 35,
            gt8 = 36,

            jmp = 37,
            jmpc = 38,

            push1 = 39,
            push2 = 40,
            push4 = 41,
            push8 = 42,
            pop1 = 43,
            pop2 = 44,
            pop4 = 45,
            pop8 = 46,

            call = 47,
            ret = 48,

            pow = 52,
            log = 53,
            sin = 54,
            cos = 55,
            tan = 56,
            asin = 57,
            acos = 58,
            atan = 59,

            rnd = 60,
            rndi = 61,

            tick = 62,
            ticksh = 63,

            new_ = 64,
            del = 65,
            size = 66,

            doe = 67,
			debug = 68,

            exdel = 69,

            z2str = 70,
            z2strb = 71,
            z2strh = 72,
            r2str = 73,

            str2z = 74,
            strb2z = 75,
            strh2z = 76,
            str2r = 77,

            arrcpy = 78,
			arrcmp = 79,

            newreader = 80,
            newwriter = 81,
            closereader = 82,
            closewriter = 83,
            readerlen = 84,
            read1 = 85,
            read2 = 86,
            read4 = 87,
            read8 = 88,
            readarray = 89,
            write1 = 90,
            write2 = 91,
            write4 = 92,
            write8 = 93,
            writearray = 94,

            key = 95,
            left = 96,
            right = 97,
            middle = 98,
            mousedz = 99,
            mousex = 100,
            mousey = 101,

            clear = 102,
            present = 103,
            newsurf = 104,
            surfdx = 105,
            surfdy = 106,
            blt = 107,
            drawline = 108,
            drawbox = 109,
            drawrbox = 110,
            drawellipse = 111,
            drawtext = 112,

            newsndbuf = 113,
            getvolume = 114,
            setvolume = 115,
            play = 116,
            stop = 117,
        }
        
        enum Addressing
        {
            Immediate = 0,
            X = 1,
            HP = 2,
            Data = 3,
            Stack = 4,
            Heap = 5,
        }

        byte[][] X;
        uint IP, SP, HP;

        byte[] Code, Data, Stack;
        Dictionary<uint, byte[]> Heap;

        Dictionary<uint, object> ExObjects;

        Random Rnd = new Random();

        bool Exit = false;

        public string SimFilePath, SimFileParentPath;

        Microsoft.DirectX.DirectDraw.Device DevDraw;
        Microsoft.DirectX.DirectSound.Device DevSound;
        Microsoft.DirectX.DirectInput.Device DevKeyboard, DevMouse;

        KeyboardState TheKeyboardState;
        bool MouseLeft, MouseRight, MouseMiddle;
        int MouseDZ, MouseScrX, MouseScrY, MouseX, MouseY;
        Surface FrontSurf, BackSurf;

        [DllImport("winmm.dll")]
        static extern int timeGetTime();
        int GWL_STYLE = -16;
        [DllImport("user32.dll", CharSet = CharSet.Unicode)]
        static extern int GetWindowLong(IntPtr hwnd, int nIndex);
        [StructLayout(LayoutKind.Sequential)]
        struct RECT
        {
            public int Left, Top, Right, Bottom;
        }
        [DllImport("user32.dll", CharSet = CharSet.Unicode)]
        static extern int AdjustWindowRect(ref RECT rc, int style, bool bMenu);
        [StructLayout(LayoutKind.Sequential)]
        struct POINT
        {
            public int X, Y;
        }
        [DllImport("user32.dll", CharSet = CharSet.Unicode)]
        static extern int GetCursorPos(out POINT pt);
        [DllImport("user32.dll", CharSet = CharSet.Unicode)]
        static extern int SetCursorPos(int x, int y);
        [DllImport("user32.dll", CharSet = CharSet.Unicode)]
        static extern IntPtr GetForegroundWindow();
        [DllImport("kernel32", CharSet = CharSet.Unicode)]
        static extern int GetPrivateProfileString(string lpAppName, string lpKeyName, string lpDefault, StringBuilder lpReturnedString, int nSize, string lpFileName);

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Shown(object sender, EventArgs e)
        {
            DevDraw = new Microsoft.DirectX.DirectDraw.Device();
            DevSound = new Microsoft.DirectX.DirectSound.Device();
            DevKeyboard = new Microsoft.DirectX.DirectInput.Device(SystemGuid.Keyboard);
            DevMouse = new Microsoft.DirectX.DirectInput.Device(SystemGuid.Mouse);

            POINT pt;
            GetCursorPos(out pt);
            MouseScrX = pt.X;
            MouseScrY = pt.Y;
            Point point;
            point = PointToClient(new Point(MouseScrX, MouseScrY));
            MouseX = point.X;
            MouseY = point.Y;

            DevKeyboard = new Microsoft.DirectX.DirectInput.Device(SystemGuid.Keyboard);
            DevKeyboard.SetCooperativeLevel(Handle, Microsoft.DirectX.DirectInput.CooperativeLevelFlags.Background
                | Microsoft.DirectX.DirectInput.CooperativeLevelFlags.NonExclusive);
            DevKeyboard.SetDataFormat(DeviceDataFormat.Keyboard);
            DevKeyboard.Acquire();

            DevMouse.SetCooperativeLevel(Handle, Microsoft.DirectX.DirectInput.CooperativeLevelFlags.Background
                | Microsoft.DirectX.DirectInput.CooperativeLevelFlags.NonExclusive);
            DevMouse.SetDataFormat(DeviceDataFormat.Mouse);
            DevMouse.Acquire();

            RECT rc;
            rc.Left = 0;
            rc.Top = 0;
            rc.Right = 1024;
            rc.Bottom = 768;
            AdjustWindowRect(ref rc, GetWindowLong(Handle, GWL_STYLE), false);
            Width = rc.Right - rc.Left;
            Height = rc.Bottom - rc.Top;
            Left = Screen.PrimaryScreen.Bounds.Width / 2 - Width / 2;
            Top = Screen.PrimaryScreen.Bounds.Height / 2 - Height / 2;

            DevDraw.SetCooperativeLevel(this, Microsoft.DirectX.DirectDraw.CooperativeLevelFlags.Normal);
            SurfaceDescription desc;
            desc = new SurfaceDescription();
            desc.SurfaceCaps.PrimarySurface = true;
            FrontSurf = new Surface(desc, DevDraw);
            FrontSurf.Clipper = new Clipper(DevDraw);
            FrontSurf.Clipper.Window = this;
            desc = new SurfaceDescription();
            desc.SurfaceCaps.VideoMemory = true;
            desc.Width = 1024;
            desc.Height = 768;
            BackSurf = new Surface(desc, DevDraw);
            BackSurf.Clipper = new Clipper(DevDraw);
            BackSurf.Clipper.ClipList = new Rectangle[] { new Rectangle(0, 0, 1024, 768) };

            DevSound.SetCooperativeLevel(this, CooperativeLevel.Priority);

            X = new byte[16][];
            for (int i = 0; i < 16; i++)
                X[i] = new byte[8];

            IP = 0;
            SP = 0;
            HP = 0;

            StringBuilder tmp = new StringBuilder(64);
            uint dataSize, stackSize;
            GetPrivateProfileString("VM", "DataSize", "65536", tmp, 64, Application.StartupPath + @"\VM.ini");
            try
            {
                dataSize = uint.Parse(tmp.ToString());
            }
            catch (Exception)
            {
                dataSize = 65536;
            }
            tmp = new StringBuilder(64);
            GetPrivateProfileString("VM", "StackSize", "65536", tmp, 64, Application.StartupPath + @"\VM.ini");
            try
            {
                stackSize = uint.Parse(tmp.ToString());
            }
            catch (Exception)
            {
                stackSize = 65536;
            }
            Data = new byte[dataSize];
            Stack = new byte[stackSize];

            Heap = new Dictionary<uint, byte[]>();

            ExObjects = new Dictionary<uint, object>();

            SimFileParentPath = new FileInfo(SimFilePath).Directory.FullName;

            BinaryReader r = new BinaryReader(new FileStream(SimFilePath, FileMode.Open, FileAccess.Read, FileShare.None));
            Code = r.ReadBytes((int)r.BaseStream.Length);
            r.Close();

            while (true)
            {
                Opcode opcode = (Opcode)Code[IP];
                IP += 1;

                if (opcode == Opcode.end)
                    break;

                switch (opcode)
                {
                    case Opcode.mov1:
                        mov(1);
                        break;
                    case Opcode.mov2:
                        mov(2);
                        break;
                    case Opcode.mov4:
                        mov(4);
                        break;
                    case Opcode.mov8:
                        mov(8);
                        break;

                    case Opcode.z2r:
                        byte destIndexOfX = Code[IP];
                        IP += 1;
                        byte srcIndexOfX = Code[IP];
                        IP += 1;
                        if (destIndexOfX == srcIndexOfX)
                            throw new Exception();
                        BitConverter.GetBytes((double)BitConverter.ToInt32(X[srcIndexOfX], 0)).CopyTo(X[destIndexOfX], 0);
                        break;
                    case Opcode.r2z:
                        destIndexOfX = Code[IP];
                        IP += 1;
                        srcIndexOfX = Code[IP];
                        IP += 1;
                        if (destIndexOfX == srcIndexOfX)
                            throw new Exception();
                        BitConverter.GetBytes((Int32)BitConverter.ToDouble(X[srcIndexOfX], 0)).CopyTo(X[destIndexOfX], 0);
                        break;
                    case Opcode.z2char:
                        destIndexOfX = Code[IP];
                        IP += 1;
                        srcIndexOfX = Code[IP];
                        IP += 1;
                        if (destIndexOfX == srcIndexOfX)
                            throw new Exception();
                        BitConverter.GetBytes((char)BitConverter.ToInt32(X[srcIndexOfX], 0)).CopyTo(X[destIndexOfX], 0);
                        break;
                    case Opcode.char2z:
                        destIndexOfX = Code[IP];
                        IP += 1;
                        srcIndexOfX = Code[IP];
                        IP += 1;
                        if (destIndexOfX == srcIndexOfX)
                            throw new Exception();
                        BitConverter.GetBytes((Int32)BitConverter.ToChar(X[srcIndexOfX], 0)).CopyTo(X[destIndexOfX], 0);
                        break;
                    
                    case Opcode.add4:
                        destIndexOfX = Code[IP];
                        IP += 1;
                        byte srcAIndexOfX = Code[IP];
                        IP += 1;
                        byte srcBIndexOfX = Code[IP];
                        IP += 1;
                        if (srcAIndexOfX == srcBIndexOfX || destIndexOfX == srcAIndexOfX || destIndexOfX == srcBIndexOfX)
                            throw new Exception();
                        BitConverter.GetBytes((BitConverter.ToInt32(X[srcAIndexOfX], 0) + BitConverter.ToInt32(X[srcBIndexOfX], 0))).CopyTo(X[destIndexOfX], 0);
                        break;
                    case Opcode.add8:
                        destIndexOfX = Code[IP];
                        IP += 1;
                        srcAIndexOfX = Code[IP];
                        IP += 1;
                        srcBIndexOfX = Code[IP];
                        IP += 1;
                        if (srcAIndexOfX == srcBIndexOfX || destIndexOfX == srcAIndexOfX || destIndexOfX == srcBIndexOfX)
                            throw new Exception();
                        BitConverter.GetBytes((BitConverter.ToDouble(X[srcAIndexOfX], 0) + BitConverter.ToDouble(X[srcBIndexOfX], 0))).CopyTo(X[destIndexOfX], 0);
                        break;
                    case Opcode.sub4:
                        destIndexOfX = Code[IP];
                        IP += 1;
                        srcAIndexOfX = Code[IP];
                        IP += 1;
                        srcBIndexOfX = Code[IP];
                        IP += 1;
                        if (srcAIndexOfX == srcBIndexOfX || destIndexOfX == srcAIndexOfX || destIndexOfX == srcBIndexOfX)
                            throw new Exception();
                        BitConverter.GetBytes((BitConverter.ToInt32(X[srcAIndexOfX], 0) - BitConverter.ToInt32(X[srcBIndexOfX], 0))).CopyTo(X[destIndexOfX], 0);
                        break;
                    case Opcode.sub8:
                        destIndexOfX = Code[IP];
                        IP += 1;
                        srcAIndexOfX = Code[IP];
                        IP += 1;
                        srcBIndexOfX = Code[IP];
                        IP += 1;
                        if (srcAIndexOfX == srcBIndexOfX || destIndexOfX == srcAIndexOfX || destIndexOfX == srcBIndexOfX)
                            throw new Exception();
                        BitConverter.GetBytes((BitConverter.ToDouble(X[srcAIndexOfX], 0) - BitConverter.ToDouble(X[srcBIndexOfX], 0))).CopyTo(X[destIndexOfX], 0);
                        break;
                    case Opcode.mul4:
                        destIndexOfX = Code[IP];
                        IP += 1;
                        srcAIndexOfX = Code[IP];
                        IP += 1;
                        srcBIndexOfX = Code[IP];
                        IP += 1;
                        if (srcAIndexOfX == srcBIndexOfX || destIndexOfX == srcAIndexOfX || destIndexOfX == srcBIndexOfX)
                            throw new Exception();
                        BitConverter.GetBytes((BitConverter.ToInt32(X[srcAIndexOfX], 0) * BitConverter.ToInt32(X[srcBIndexOfX], 0))).CopyTo(X[destIndexOfX], 0);
                        break;
                    case Opcode.mul8:
                        destIndexOfX = Code[IP];
                        IP += 1;
                        srcAIndexOfX = Code[IP];
                        IP += 1;
                        srcBIndexOfX = Code[IP];
                        IP += 1;
                        if (srcAIndexOfX == srcBIndexOfX || destIndexOfX == srcAIndexOfX || destIndexOfX == srcBIndexOfX)
                            throw new Exception();
                        BitConverter.GetBytes((BitConverter.ToDouble(X[srcAIndexOfX], 0) * BitConverter.ToDouble(X[srcBIndexOfX], 0))).CopyTo(X[destIndexOfX], 0);
                        break;
                    case Opcode.div4:
                        destIndexOfX = Code[IP];
                        IP += 1;
                        srcAIndexOfX = Code[IP];
                        IP += 1;
                        srcBIndexOfX = Code[IP];
                        IP += 1;
                        if (srcAIndexOfX == srcBIndexOfX || destIndexOfX == srcAIndexOfX || destIndexOfX == srcBIndexOfX)
                            throw new Exception();
                        BitConverter.GetBytes((BitConverter.ToInt32(X[srcAIndexOfX], 0) / BitConverter.ToInt32(X[srcBIndexOfX], 0))).CopyTo(X[destIndexOfX], 0);
                        break;
                    case Opcode.div8:
                        destIndexOfX = Code[IP];
                        IP += 1;
                        srcAIndexOfX = Code[IP];
                        IP += 1;
                        srcBIndexOfX = Code[IP];
                        IP += 1;
                        if (srcAIndexOfX == srcBIndexOfX || destIndexOfX == srcAIndexOfX || destIndexOfX == srcBIndexOfX)
                            throw new Exception();
                        BitConverter.GetBytes((BitConverter.ToDouble(X[srcAIndexOfX], 0) / BitConverter.ToDouble(X[srcBIndexOfX], 0))).CopyTo(X[destIndexOfX], 0);
                        break;
                    case Opcode.mod:
                        destIndexOfX = Code[IP];
                        IP += 1;
                        srcAIndexOfX = Code[IP];
                        IP += 1;
                        srcBIndexOfX = Code[IP];
                        IP += 1;
                        if (srcAIndexOfX == srcBIndexOfX || destIndexOfX == srcAIndexOfX || destIndexOfX == srcBIndexOfX)
                            throw new Exception();
                        BitConverter.GetBytes((BitConverter.ToInt32(X[srcAIndexOfX], 0) % BitConverter.ToInt32(X[srcBIndexOfX], 0))).CopyTo(X[destIndexOfX], 0);
                        break;

                    case Opcode.and1:
                        destIndexOfX = Code[IP];
                        IP += 1;
                        srcAIndexOfX = Code[IP];
                        IP += 1;
                        srcBIndexOfX = Code[IP];
                        IP += 1;
                        if (srcAIndexOfX == srcBIndexOfX || destIndexOfX == srcAIndexOfX || destIndexOfX == srcBIndexOfX)
                            throw new Exception();
                        X[destIndexOfX][0] = (byte)(X[srcAIndexOfX][0] & X[srcBIndexOfX][0]);
                        break;
                    case Opcode.and4:
                        destIndexOfX = Code[IP];
                        IP += 1;
                        srcAIndexOfX = Code[IP];
                        IP += 1;
                        srcBIndexOfX = Code[IP];
                        IP += 1;
                        if (srcAIndexOfX == srcBIndexOfX || destIndexOfX == srcAIndexOfX || destIndexOfX == srcBIndexOfX)
                            throw new Exception();
                        for (int i = 0; i < 4; i++)
                            X[destIndexOfX][i] = (byte)(X[srcAIndexOfX][i] & X[srcBIndexOfX][i]);
                        break;
                    case Opcode.or1:
                        destIndexOfX = Code[IP];
                        IP += 1;
                        srcAIndexOfX = Code[IP];
                        IP += 1;
                        srcBIndexOfX = Code[IP];
                        IP += 1;
                        if (srcAIndexOfX == srcBIndexOfX || destIndexOfX == srcAIndexOfX || destIndexOfX == srcBIndexOfX)
                            throw new Exception();
                        X[destIndexOfX][0] = (byte)(X[srcAIndexOfX][0] | X[srcBIndexOfX][0]);
                        break;
                    case Opcode.or4:
                        destIndexOfX = Code[IP];
                        IP += 1;
                        srcAIndexOfX = Code[IP];
                        IP += 1;
                        srcBIndexOfX = Code[IP];
                        IP += 1;
                        if (srcAIndexOfX == srcBIndexOfX || destIndexOfX == srcAIndexOfX || destIndexOfX == srcBIndexOfX)
                            throw new Exception();
                        for (int i = 0; i < 4; i++)
                            X[destIndexOfX][i] = (byte)(X[srcAIndexOfX][i] | X[srcBIndexOfX][i]);
                        break;
                    case Opcode.xor1:
                        destIndexOfX = Code[IP];
                        IP += 1;
                        srcAIndexOfX = Code[IP];
                        IP += 1;
                        srcBIndexOfX = Code[IP];
                        IP += 1;
                        if (srcAIndexOfX == srcBIndexOfX || destIndexOfX == srcAIndexOfX || destIndexOfX == srcBIndexOfX)
                            throw new Exception();
                        X[destIndexOfX][0] = (byte)(X[srcAIndexOfX][0] ^ X[srcBIndexOfX][0]);
                        break;
                    case Opcode.xor4:
                        destIndexOfX = Code[IP];
                        IP += 1;
                        srcAIndexOfX = Code[IP];
                        IP += 1;
                        srcBIndexOfX = Code[IP];
                        IP += 1;
                        if (srcAIndexOfX == srcBIndexOfX || destIndexOfX == srcAIndexOfX || destIndexOfX == srcBIndexOfX)
                            throw new Exception();
                        for (int i = 0; i < 4; i++)
                            X[destIndexOfX][i] = (byte)(X[srcAIndexOfX][i] ^ X[srcBIndexOfX][i]);
                        break;
                    case Opcode.not1:
                        destIndexOfX = Code[IP];
                        IP += 1;
                        srcIndexOfX = Code[IP];
                        IP += 1;
                        if (destIndexOfX == srcIndexOfX)
                            throw new Exception();
                        X[destIndexOfX][0] = (byte)(~ X[srcIndexOfX][0]);
                        break;
                    case Opcode.not4:
                        destIndexOfX = Code[IP];
                        IP += 1;
                        srcIndexOfX = Code[IP];
                        IP += 1;
                        if (destIndexOfX == srcIndexOfX)
                            throw new Exception();
                        for (int i = 0; i < 4; i++)
                            X[destIndexOfX][i] = (byte)(~X[srcIndexOfX][i]);
                        break;

                    case Opcode.shl:
                        destIndexOfX = Code[IP];
                        IP += 1;
                        srcAIndexOfX = Code[IP];
                        IP += 1;
                        srcBIndexOfX = Code[IP];
                        IP += 1;
                        if (srcAIndexOfX == srcBIndexOfX || destIndexOfX == srcAIndexOfX || destIndexOfX == srcBIndexOfX)
                            throw new Exception();
                        BitConverter.GetBytes((BitConverter.ToInt32(X[srcAIndexOfX], 0) << BitConverter.ToInt32(X[srcBIndexOfX], 0))).CopyTo(X[destIndexOfX], 0);
                        break;
                    case Opcode.shr:
                        destIndexOfX = Code[IP];
                        IP += 1;
                        srcAIndexOfX = Code[IP];
                        IP += 1;
                        srcBIndexOfX = Code[IP];
                        IP += 1;
                        if (srcAIndexOfX == srcBIndexOfX || destIndexOfX == srcAIndexOfX || destIndexOfX == srcBIndexOfX)
                            throw new Exception();
                        BitConverter.GetBytes((BitConverter.ToInt32(X[srcAIndexOfX], 0) >> BitConverter.ToInt32(X[srcBIndexOfX], 0))).CopyTo(X[destIndexOfX], 0);
                        break;

                    case Opcode.eq1:
                        destIndexOfX = Code[IP];
                        IP += 1;
                        srcAIndexOfX = Code[IP];
                        IP += 1;
                        srcBIndexOfX = Code[IP];
                        IP += 1;
                        if (srcAIndexOfX == srcBIndexOfX || destIndexOfX == srcAIndexOfX || destIndexOfX == srcBIndexOfX)
                            throw new Exception();
                        X[destIndexOfX][0] = X[srcAIndexOfX][0] == X[srcBIndexOfX][0] ? (byte)0xFF : (byte)0x00;
                        break;
                    case Opcode.eq2:
                        destIndexOfX = Code[IP];
                        IP += 1;
                        srcAIndexOfX = Code[IP];
                        IP += 1;
                        srcBIndexOfX = Code[IP];
                        IP += 1;
                        if (srcAIndexOfX == srcBIndexOfX || destIndexOfX == srcAIndexOfX || destIndexOfX == srcBIndexOfX)
                            throw new Exception();
                        X[destIndexOfX][0] = BitConverter.ToInt16(X[srcAIndexOfX], 0) == BitConverter.ToInt16(X[srcBIndexOfX], 0) ? (byte)0xFF : (byte)0x00;
                        break;
                    case Opcode.eq4:
                        destIndexOfX = Code[IP];
                        IP += 1;
                        srcAIndexOfX = Code[IP];
                        IP += 1;
                        srcBIndexOfX = Code[IP];
                        IP += 1;
                        if (srcAIndexOfX == srcBIndexOfX || destIndexOfX == srcAIndexOfX || destIndexOfX == srcBIndexOfX)
                            throw new Exception();
                        X[destIndexOfX][0] = BitConverter.ToInt32(X[srcAIndexOfX], 0) == BitConverter.ToInt32(X[srcBIndexOfX], 0) ? (byte)0xFF : (byte)0x00;
                        break;
                    case Opcode.eq8:
                        destIndexOfX = Code[IP];
                        IP += 1;
                        srcAIndexOfX = Code[IP];
                        IP += 1;
                        srcBIndexOfX = Code[IP];
                        IP += 1;
                        if (srcAIndexOfX == srcBIndexOfX || destIndexOfX == srcAIndexOfX || destIndexOfX == srcBIndexOfX)
                            throw new Exception();
                        X[destIndexOfX][0] = BitConverter.ToDouble(X[srcAIndexOfX], 0) == BitConverter.ToDouble(X[srcBIndexOfX], 0) ? (byte)0xFF : (byte)0x00;
                        break;
                    case Opcode.lt4:
                        destIndexOfX = Code[IP];
                        IP += 1;
                        srcAIndexOfX = Code[IP];
                        IP += 1;
                        srcBIndexOfX = Code[IP];
                        IP += 1;
                        if (srcAIndexOfX == srcBIndexOfX || destIndexOfX == srcAIndexOfX || destIndexOfX == srcBIndexOfX)
                            throw new Exception();
                        X[destIndexOfX][0] = BitConverter.ToInt32(X[srcAIndexOfX], 0) < BitConverter.ToInt32(X[srcBIndexOfX], 0) ? (byte)0xFF : (byte)0x00;
                        break;
                    case Opcode.lt8:
                        destIndexOfX = Code[IP];
                        IP += 1;
                        srcAIndexOfX = Code[IP];
                        IP += 1;
                        srcBIndexOfX = Code[IP];
                        IP += 1;
                        if (srcAIndexOfX == srcBIndexOfX || destIndexOfX == srcAIndexOfX || destIndexOfX == srcBIndexOfX)
                            throw new Exception();
                        X[destIndexOfX][0] = BitConverter.ToDouble(X[srcAIndexOfX], 0) < BitConverter.ToDouble(X[srcBIndexOfX], 0) ? (byte)0xFF : (byte)0x00;
                        break;
                    case Opcode.gt4:
                        destIndexOfX = Code[IP];
                        IP += 1;
                        srcAIndexOfX = Code[IP];
                        IP += 1;
                        srcBIndexOfX = Code[IP];
                        IP += 1;
                        if (srcAIndexOfX == srcBIndexOfX || destIndexOfX == srcAIndexOfX || destIndexOfX == srcBIndexOfX)
                            throw new Exception();
                        X[destIndexOfX][0] = BitConverter.ToInt32(X[srcAIndexOfX], 0) > BitConverter.ToInt32(X[srcBIndexOfX], 0) ? (byte)0xFF : (byte)0x00;
                        break;
                    case Opcode.gt8:
                        destIndexOfX = Code[IP];
                        IP += 1;
                        srcAIndexOfX = Code[IP];
                        IP += 1;
                        srcBIndexOfX = Code[IP];
                        IP += 1;
                        if (srcAIndexOfX == srcBIndexOfX || destIndexOfX == srcAIndexOfX || destIndexOfX == srcBIndexOfX)
                            throw new Exception();
                        X[destIndexOfX][0] = BitConverter.ToDouble(X[srcAIndexOfX], 0) > BitConverter.ToDouble(X[srcBIndexOfX], 0) ? (byte)0xFF : (byte)0x00;
                        break;

                    case Opcode.jmp:
                        byte addrIndexOfX = Code[IP];
                        IP += 1;
                        IP = BitConverter.ToUInt32(X[addrIndexOfX], 0);
                        break;
                    case Opcode.jmpc:
                        addrIndexOfX = Code[IP];
                        IP += 1;
                        byte conditionIndexOfX = Code[IP];
                        IP += 1;
                        if (addrIndexOfX == conditionIndexOfX)
                            throw new Exception();
                        if (X[conditionIndexOfX][0] != 0x00)
                            IP = BitConverter.ToUInt32(X[addrIndexOfX], 0);
                        break;

                    case Opcode.push1:
                        srcIndexOfX = Code[IP];
                        IP += 1;
                        Stack[SP + 1] = X[srcIndexOfX][0];
                        SP += 1;
                        break;
                    case Opcode.push2:
                        srcIndexOfX = Code[IP];
                        IP += 1;
                        for (int i = 0; i < 2; i++)
                            Stack[SP + 1 + i] = X[srcIndexOfX][i];
                        SP += 2;
                        break;
                    case Opcode.push4:
                        srcIndexOfX = Code[IP];
                        IP += 1;
                        for (int i = 0; i < 4; i++)
                            Stack[SP + 1 + i] = X[srcIndexOfX][i];
                        SP += 4;
                        break;
                    case Opcode.push8:
                        srcIndexOfX = Code[IP];
                        IP += 1;
                        for (int i = 0; i < 8; i++)
                            Stack[SP + 1 + i] = X[srcIndexOfX][i];
                        SP += 8;
                        break;
                    case Opcode.pop1:
                        destIndexOfX = Code[IP];
                        IP += 1;
                        X[destIndexOfX][0] = Stack[SP];
                        SP -= 1;
                        break;
                    case Opcode.pop2:
                        destIndexOfX = Code[IP];
                        IP += 1;
                        Array.Copy(Stack, SP - 1, X[destIndexOfX], 0, 2);
                        SP -= 2;
                        break;
                    case Opcode.pop4:
                        destIndexOfX = Code[IP];
                        IP += 1;
                        Array.Copy(Stack, SP - 3, X[destIndexOfX], 0, 4);
                        SP -= 4;
                        break;
                    case Opcode.pop8:
                        destIndexOfX = Code[IP];
                        IP += 1;
                        Array.Copy(Stack, SP - 7, X[destIndexOfX], 0, 8);
                        SP -= 8;
                        break;

                    case Opcode.call:
                        addrIndexOfX = Code[IP];
                        IP += 1;
                        BitConverter.GetBytes((UInt32)IP).CopyTo(Stack, SP + 1);
                        SP += 4;
                        IP = BitConverter.ToUInt32(X[addrIndexOfX], 0);
                        break;
                    case Opcode.ret:
                        IP = BitConverter.ToUInt32(Stack, (int)SP - 3);
                        SP -= 4;
                        break;

                    case Opcode.pow:
                        destIndexOfX = Code[IP];
                        IP += 1;
                        srcAIndexOfX = Code[IP];
                        IP += 1;
                        srcBIndexOfX = Code[IP];
                        IP += 1;
                        if (srcAIndexOfX == srcBIndexOfX || destIndexOfX == srcAIndexOfX || destIndexOfX == srcBIndexOfX)
                            throw new Exception();
                        BitConverter.GetBytes(Math.Pow(BitConverter.ToDouble(X[srcAIndexOfX], 0), BitConverter.ToDouble(X[srcBIndexOfX], 0))).CopyTo(X[destIndexOfX], 0);
                        break;
                    case Opcode.log:
                        destIndexOfX = Code[IP];
                        IP += 1;
                        srcAIndexOfX = Code[IP];
                        IP += 1;
                        srcBIndexOfX = Code[IP];
                        IP += 1;
                        if (srcAIndexOfX == srcBIndexOfX || destIndexOfX == srcAIndexOfX || destIndexOfX == srcBIndexOfX)
                            throw new Exception();
                        BitConverter.GetBytes(Math.Log(BitConverter.ToDouble(X[srcAIndexOfX], 0), BitConverter.ToDouble(X[srcBIndexOfX], 0))).CopyTo(X[destIndexOfX], 0);
                        break;
                    case Opcode.sin:
                        destIndexOfX = Code[IP];
                        IP += 1;
                        srcIndexOfX = Code[IP];
                        IP += 1;
                        if (destIndexOfX == srcIndexOfX)
                            throw new Exception();
                        BitConverter.GetBytes(Math.Sin(BitConverter.ToDouble(X[srcIndexOfX], 0))).CopyTo(X[destIndexOfX], 0);
                        break;
                    case Opcode.cos:
                        destIndexOfX = Code[IP];
                        IP += 1;
                        srcIndexOfX = Code[IP];
                        IP += 1;
                        if (destIndexOfX == srcIndexOfX)
                            throw new Exception();
                        BitConverter.GetBytes(Math.Cos(BitConverter.ToDouble(X[srcIndexOfX], 0))).CopyTo(X[destIndexOfX], 0);
                        break;
                    case Opcode.tan:
                        destIndexOfX = Code[IP];
                        IP += 1;
                        srcIndexOfX = Code[IP];
                        IP += 1;
                        if (destIndexOfX == srcIndexOfX)
                            throw new Exception();
                        BitConverter.GetBytes(Math.Tan(BitConverter.ToDouble(X[srcIndexOfX], 0))).CopyTo(X[destIndexOfX], 0);
                        break;
                    case Opcode.asin:
                        destIndexOfX = Code[IP];
                        IP += 1;
                        srcIndexOfX = Code[IP];
                        IP += 1;
                        if (destIndexOfX == srcIndexOfX)
                            throw new Exception();
                        BitConverter.GetBytes(Math.Asin(BitConverter.ToDouble(X[srcIndexOfX], 0))).CopyTo(X[destIndexOfX], 0);
                        break;
                    case Opcode.acos:
                        destIndexOfX = Code[IP];
                        IP += 1;
                        srcIndexOfX = Code[IP];
                        IP += 1;
                        if (destIndexOfX == srcIndexOfX)
                            throw new Exception();
                        BitConverter.GetBytes(Math.Acos(BitConverter.ToDouble(X[srcIndexOfX], 0))).CopyTo(X[destIndexOfX], 0);
                        break;
                    case Opcode.atan:
                        destIndexOfX = Code[IP];
                        IP += 1;
                        srcIndexOfX = Code[IP];
                        IP += 1;
                        if (destIndexOfX == srcIndexOfX)
                            throw new Exception();
                        BitConverter.GetBytes(Math.Atan(BitConverter.ToDouble(X[srcIndexOfX], 0))).CopyTo(X[destIndexOfX], 0);
                        break;

                    case Opcode.rnd:
                        destIndexOfX = Code[IP];
                        IP += 1;
                        BitConverter.GetBytes(Rnd.NextDouble()).CopyTo(X[destIndexOfX], 0);
                        break;
                    case Opcode.rndi:
                        Rnd = new Random();
                        break;

                    case Opcode.tick:
                        destIndexOfX = Code[IP];
                        IP += 1;
                        BitConverter.GetBytes((double)timeGetTime() / 1000.0).CopyTo(X[destIndexOfX], 0);
                        break;
                    case Opcode.ticksh:
						destIndexOfX = Code[IP];
                        IP += 1;
                        BitConverter.GetBytes((double)timeGetTime() / 1000.0 / 0.864).CopyTo(X[destIndexOfX], 0);
                        break;

                    case Opcode.new_:
                        srcIndexOfX = Code[IP];
                        IP += 1;
                        for (uint i = 1; i <= uint.MaxValue; i++)
                            if (Heap.ContainsKey(i) == false)
                            {
                                Heap.Add(i, new byte[BitConverter.ToInt32(X[srcIndexOfX], 0)]);
                                HP = i;
                                break;
                            }
                        break;
                    case Opcode.del:
                        Heap.Remove(HP);
                        HP = 0; // "safe" delete!
                        break;
                    case Opcode.size:
                        destIndexOfX = Code[IP];
                        IP += 1;
                        BitConverter.GetBytes((Int32)Heap[HP].Length).CopyTo(X[destIndexOfX], 0);
                        break;

                    case Opcode.doe: // これからは、レジスタ指定しなく、固定なレジスタで処理する!
                        MyDoEvents();
                        break;
			        case Opcode.debug:
						byte[] tBytes = Heap[BitConverter.ToUInt32(X[0], 0)];
                        StringBuilder tStrBuilder = new StringBuilder(tBytes.Length / 2);
                        for (int i = 0; i < tBytes.Length; i += 2)
                            tStrBuilder.Append(BitConverter.ToChar(tBytes, i));
						Text = tStrBuilder.ToString();
						break;

                    case Opcode.exdel:
                        ExObjects.Remove(BitConverter.ToUInt32(X[0], 0));
                        break;

                    case Opcode.z2str:
                        string tStr = BitConverter.ToInt32(X[1], 0).ToString();
                        for (uint i = 1; i <= uint.MaxValue; i++)
                            if (Heap.ContainsKey(i) == false)
                            {
                                Heap.Add(i, new byte[tStr.Length * 2]);
                                BitConverter.GetBytes((UInt32)i).CopyTo(X[0], 0);
                                for (int j = 0; j < tStr.Length; j++)
                                    BitConverter.GetBytes((char)tStr[j]).CopyTo(Heap[i], j * 2);
                                break;
                            }
                        break;
                    case Opcode.z2strb:
                        tStr = Convert.ToString(BitConverter.ToInt32(X[1], 0), 2);
                        for (uint i = 1; i <= uint.MaxValue; i++)
                            if (Heap.ContainsKey(i) == false)
                            {
                                Heap.Add(i, new byte[tStr.Length * 2]);
                                BitConverter.GetBytes((UInt32)i).CopyTo(X[0], 0);
                                for (int j = 0; j < tStr.Length; j++)
                                    BitConverter.GetBytes((char)tStr[j]).CopyTo(Heap[i], j * 2);
                                break;
                            }
                        break;
                    case Opcode.z2strh:
                        tStr = Convert.ToString(BitConverter.ToInt32(X[1], 0), 16);
                        for (uint i = 1; i <= uint.MaxValue; i++)
                            if (Heap.ContainsKey(i) == false)
                            {
                                Heap.Add(i, new byte[tStr.Length * 2]);
                                BitConverter.GetBytes((UInt32)i).CopyTo(X[0], 0);
                                for (int j = 0; j < tStr.Length; j++)
                                    BitConverter.GetBytes((char)tStr[j]).CopyTo(Heap[i], j * 2);
                                break;
                            }
                        break;
                    case Opcode.r2str:
                        tStr = BitConverter.ToDouble(X[1], 0).ToString();
                        for (uint i = 1; i <= uint.MaxValue; i++)
                            if (Heap.ContainsKey(i) == false)
                            {
                                Heap.Add(i, new byte[tStr.Length * 2]);
                                BitConverter.GetBytes((UInt32)i).CopyTo(X[0], 0);
                                for (int j = 0; j < tStr.Length; j++)
                                    BitConverter.GetBytes((char)tStr[j]).CopyTo(Heap[i], j * 2);
                                break;
                            }
                        break;

                    case Opcode.str2z:
                        tBytes = Heap[BitConverter.ToUInt32(X[2], 0)];
                        tStrBuilder = new StringBuilder(tBytes.Length / 2);
                        for (int i = 0; i < tBytes.Length; i += 2)
                            tStrBuilder.Append(BitConverter.ToChar(tBytes, i));
                        X[0][0] = (byte)0xFF;
                        try
                        {
                            BitConverter.GetBytes(Convert.ToInt32(tStrBuilder.ToString())).CopyTo(X[1], 0);
                        }
                        catch (Exception)
                        {
                            X[0][0] = 0;
                        }
                        break;
                    case Opcode.strb2z:
                        tBytes = Heap[BitConverter.ToUInt32(X[2], 0)];
                        tStrBuilder = new StringBuilder(tBytes.Length / 2);
                        for (int i = 0; i < tBytes.Length; i += 2)
                            tStrBuilder.Append(BitConverter.ToChar(tBytes, i));
                        X[0][0] = (byte)0xFF;
                        try
                        {
                            BitConverter.GetBytes(Convert.ToInt32(tStrBuilder.ToString(), 2)).CopyTo(X[1], 0);
                        }
                        catch (Exception)
                        {
                            X[0][0] = 0;
                        }
                        break;
                    case Opcode.strh2z:
                        tBytes = Heap[BitConverter.ToUInt32(X[2], 0)];
                        tStrBuilder = new StringBuilder(tBytes.Length / 2);
                        for (int i = 0; i < tBytes.Length; i += 2)
                            tStrBuilder.Append(BitConverter.ToChar(tBytes, i));
                        X[0][0] = (byte)0xFF;
                        try
                        {
                            BitConverter.GetBytes(Convert.ToInt32(tStrBuilder.ToString(), 16)).CopyTo(X[1], 0);
                        }
                        catch (Exception)
                        {
                            X[0][0] = 0;
                        }
                        break;
                    case Opcode.str2r:
                        tBytes = Heap[BitConverter.ToUInt32(X[2], 0)];
                        tStrBuilder = new StringBuilder(tBytes.Length / 2);
                        for (int i = 0; i < tBytes.Length; i += 2)
                            tStrBuilder.Append(BitConverter.ToChar(tBytes, i));
                        X[0][0] = (byte)0xFF;
                        try
                        {
                            BitConverter.GetBytes(Convert.ToDouble(tStrBuilder.ToString())).CopyTo(X[1], 0);
                        }
                        catch (Exception)
                        {
                            X[0][0] = 0;
                        }
                        break;

                    case Opcode.arrcpy: // x0: dest, x1: destoffset, x2: src, x3: srcoffset, x4: size
                        Array.Copy(Heap[BitConverter.ToUInt32(X[2], 0)], BitConverter.ToUInt32(X[3], 0), Heap[BitConverter.ToUInt32(X[0], 0)], BitConverter.ToUInt32(X[1], 0), BitConverter.ToInt32(X[4], 0));
                        break;
					case Opcode.arrcmp: // x0: result, x1: dest, x2: src
					    X[0][0] = (byte)0xFF;
					    if (Heap[BitConverter.ToUInt32(X[1], 0)].Length != Heap[BitConverter.ToUInt32(X[2], 0)].Length)	
{				X[0][0] = 0;
break;
}

                        for (int i = 0; i < Heap[BitConverter.ToUInt32(X[1], 0)].Length; i++)
				            if (Heap[BitConverter.ToUInt32(X[1], 0)][i] != Heap[BitConverter.ToUInt32(X[2], 0)][i])
								X[0][0] = 0;
					    break;

                    case Opcode.newreader:
                        tBytes = Heap[BitConverter.ToUInt32(X[2], 0)];
                        tStrBuilder = new StringBuilder(tBytes.Length / 2);
                        for (int i = 0; i < tBytes.Length; i += 2)
                            tStrBuilder.Append(BitConverter.ToChar(tBytes, i));
                        X[0][0] = (byte)0xFF;
                        object exObj;
                        try
                        {
                            exObj = new BinaryReader(new FileStream(MapPath(tStrBuilder.ToString()), FileMode.Open, FileAccess.Read, FileShare.None));
                        }
                        catch (Exception)
                        {
                            X[0][0] = 0;
                            break;
                        }
                        for (uint i = 1; i <= uint.MaxValue; i++)
                            if (ExObjects.ContainsKey(i) == false)
                            {
                                ExObjects.Add(i, exObj);
                                BitConverter.GetBytes((UInt32)i).CopyTo(X[1], 0);
                                break;
                            }
                        break;
                    case Opcode.newwriter:
                        tBytes = Heap[BitConverter.ToUInt32(X[2], 0)];
                        tStrBuilder = new StringBuilder(tBytes.Length / 2);
                        for (int i = 0; i < tBytes.Length; i += 2)
                            tStrBuilder.Append(BitConverter.ToChar(tBytes, i));
                        X[0][0] = (byte)0xFF;
                        try
                        {
                            exObj = new BinaryWriter(new FileStream(MapPath(tStrBuilder.ToString()), FileMode.Create, FileAccess.Write, FileShare.None));
                        }
                        catch (Exception)
                        {
                            X[0][0] = 0;
                            break;
                        }
                        for (uint i = 1; i <= uint.MaxValue; i++)
                            if (ExObjects.ContainsKey(i) == false)
                            {
                                ExObjects.Add(i, exObj);
                                BitConverter.GetBytes((UInt32)i).CopyTo(X[1], 0);
                                break;
                            }
                        break;
                    case Opcode.closereader:
                        X[0][0] = (byte)0xFF;
                        try
                        {
                            ((BinaryReader)ExObjects[BitConverter.ToUInt32(X[1], 0)]).Close();
                        }
                        catch (Exception)
                        {
                            X[0][0] = 0;
                        }
                        break;
                    case Opcode.closewriter:
                        X[0][0] = (byte)0xFF;
                        try
                        {
                            ((BinaryWriter)ExObjects[BitConverter.ToUInt32(X[1], 0)]).Close();
                        }
                        catch (Exception)
                        {
                            X[0][0] = 0;
                        }
                        break;
                    case Opcode.readerlen:
                        X[0][0] = (byte)0xFF;
                        try
                        {
                            BitConverter.GetBytes((Int32)((BinaryReader)ExObjects[BitConverter.ToUInt32(X[2], 0)]).BaseStream.Length).CopyTo(X[1], 0);
                        }
                        catch (Exception)
                        {
                            X[0][0] = 0;
                        }
                        break;
                    case Opcode.read1:
                        X[0][0] = (byte)0xFF;
                        try
                        {
                            X[1][0] = ((BinaryReader)ExObjects[BitConverter.ToUInt32(X[2], 0)]).ReadByte();
                        }
                        catch (Exception)
                        {
                            X[0][0] = 0;
                        }
                        break;
                    case Opcode.read2:
                        X[0][0] = (byte)0xFF;
                        try
                        {
                            BitConverter.GetBytes(((BinaryReader)ExObjects[BitConverter.ToUInt32(X[2], 0)]).ReadInt16()).CopyTo(X[1], 0);
                        }
                        catch (Exception)
                        {
                            X[0][0] = 0;
                        }
                        break;
                    case Opcode.read4:
                        X[0][0] = (byte)0xFF;
                        try
                        {
                            BitConverter.GetBytes(((BinaryReader)ExObjects[BitConverter.ToUInt32(X[2], 0)]).ReadInt32()).CopyTo(X[1], 0);
                        }
                        catch (Exception)
                        {
                            X[0][0] = 0;
                        }
                        break;
                    case Opcode.read8:
                        X[0][0] = (byte)0xFF;
                        try
                        {
                            BitConverter.GetBytes(((BinaryReader)ExObjects[BitConverter.ToUInt32(X[2], 0)]).ReadDouble()).CopyTo(X[1], 0);
                        }
                        catch (Exception)
                        {
                            X[0][0] = 0;
                        }
                        break;
                    case Opcode.readarray:
                        X[0][0] = (byte)0xFF;
                        try
                        {
                            tBytes = ((BinaryReader)ExObjects[BitConverter.ToUInt32(X[2], 0)]).ReadBytes(BitConverter.ToInt32(X[3], 0));
                            for (uint i = 1; i <= uint.MaxValue; i++)
                                if (Heap.ContainsKey(i) == false)
                                {
                                    Heap.Add(i, tBytes);
                                    BitConverter.GetBytes((UInt32)i).CopyTo(X[1], 0);
                                    break;
                                }
                        }
                        catch (Exception)
                        {
                            X[0][0] = 0;
                        }
                        break;
                    case Opcode.write1:
                        X[0][0] = (byte)0xFF;
                        try
                        {
                            ((BinaryWriter)ExObjects[BitConverter.ToUInt32(X[1], 0)]).Write(X[2][0]);
                        }
                        catch (Exception)
                        {
                            X[0][0] = 0;
                        }
                        break;
                    case Opcode.write2:
                        X[0][0] = (byte)0xFF;
                        try
                        {
                            ((BinaryWriter)ExObjects[BitConverter.ToUInt32(X[1], 0)]).Write(BitConverter.ToInt16(X[2], 0));
                        }
                        catch (Exception)
                        {
                            X[0][0] = 0;
                        }
                        break;
                    case Opcode.write4:
                        X[0][0] = (byte)0xFF;
                        try
                        {
                            ((BinaryWriter)ExObjects[BitConverter.ToUInt32(X[1], 0)]).Write(BitConverter.ToInt32(X[2], 0));
                        }
                        catch (Exception)
                        {
                            X[0][0] = 0;
                        }
                        break;
                    case Opcode.write8:
                        X[0][0] = (byte)0xFF;
                        try
                        {
                            ((BinaryWriter)ExObjects[BitConverter.ToUInt32(X[1], 0)]).Write(BitConverter.ToDouble(X[2], 0));
                        }
                        catch (Exception)
                        {
                            X[0][0] = 0;
                        }
                        break;
                    case Opcode.writearray:
                        X[0][0] = (byte)0xFF;
                        try
                        {
                            tBytes = Heap[BitConverter.ToUInt32(X[2], 0)];
                            ((BinaryWriter)ExObjects[BitConverter.ToUInt32(X[1], 0)]).Write(tBytes);
                        }
                        catch (Exception)
                        {
                            X[0][0] = 0;
                        }
                        break;

                    case Opcode.key:
                        X[0][0] = TheKeyboardState == null ? (byte)0 : (TheKeyboardState[(Key)X[1][0]] ? (byte)0xFF : (byte)0);
                        break;
                    case Opcode.left:
                        X[0][0] = MouseLeft ? (byte)0xFF : (byte)0;
                        break;
                    case Opcode.right:
                        X[0][0] = MouseRight ? (byte)0xFF : (byte)0;
                        break;
                    case Opcode.middle:
                        X[0][0] = MouseMiddle ? (byte)0xFF : (byte)0;
                        break;
                    case Opcode.mousedz:
                        BitConverter.GetBytes(MouseDZ).CopyTo(X[0], 0);
                        break;
                    case Opcode.mousex:
                        BitConverter.GetBytes(MouseX).CopyTo(X[0], 0);
                        break;
                    case Opcode.mousey:
                        BitConverter.GetBytes(MouseY).CopyTo(X[0], 0);
                        break;

                    case Opcode.clear:
                        X[0][0] = (byte)0xFF;
                        try
                        {
                            BackSurf.ColorFill(0);
                        }
                        catch (Exception)
                        {
                            X[0][0] = 0;
                        }
                        break;
                    case Opcode.present:
                        X[0][0] = (byte)0xFF;
                        try
                        {
                            FrontSurf.Draw(RectangleToScreen(ClientRectangle), BackSurf, DrawFlags.Wait);                            
                        }
                        catch (Exception)
                        {
                            X[0][0] = 0;
                        }
                        break;
                    case Opcode.newsurf: // x0: err, x1: surf, x2: path, x3: videomem
                        tBytes = Heap[BitConverter.ToUInt32(X[2], 0)];
                        tStrBuilder = new StringBuilder(tBytes.Length / 2);
                        for (int i = 0; i < tBytes.Length; i += 2)
                            tStrBuilder.Append(BitConverter.ToChar(tBytes, i));
                        X[0][0] = (byte)0xFF;
                        try
                        {
                            desc = new SurfaceDescription();
                            desc.SurfaceCaps.OffScreenPlain = true;
                            desc.SurfaceCaps.VideoMemory = X[3][0] == 0 ? false : true;
                            FileStream fs = new FileStream(MapPath(tStrBuilder.ToString()), FileMode.Open, FileAccess.Read, FileShare.None);
                            exObj = new Surface(fs, desc, DevDraw);
                            fs.Close();
                            ColorKey colorKey;
                            colorKey.ColorSpaceHighValue = colorKey.ColorSpaceLowValue = 0;
                            ((Surface)exObj).SetColorKey(ColorKeyFlags.SourceDraw, colorKey);
                        }
                        catch (Exception)
                        {
                            X[0][0] = 0;
                            break;
                        }
                        for (uint i = 1; i <= uint.MaxValue; i++)
                            if (ExObjects.ContainsKey(i) == false)
                            {
                                ExObjects.Add(i, exObj);
                                BitConverter.GetBytes((UInt32)i).CopyTo(X[1], 0);
                                break;
                            }
                        break;
                    case Opcode.surfdx: // x0: err, x1: dx, x2: surf
                        X[0][0] = (byte)0xFF;
                        try
                        {
                            BitConverter.GetBytes((Int32)((Surface)ExObjects[BitConverter.ToUInt32(X[2], 0)]).SurfaceDescription.Width).CopyTo(X[1], 0);
                        }
                        catch (Exception)
                        {
                            X[0][0] = 0;
                        }
                        break;
                    case Opcode.surfdy: // x0: err, x1: dy, x2: surf
                        X[0][0] = (byte)0xFF;
                        try
                        {
                            BitConverter.GetBytes((Int32)((Surface)ExObjects[BitConverter.ToUInt32(X[2], 0)]).SurfaceDescription.Height).CopyTo(X[1], 0);
                        }
                        catch (Exception)
                        {
                            X[0][0] = 0;
                        }
                        break;
                    case Opcode.blt: // x0: err, x1: destX, x2: destY, x3: destDX, x4: destDY, x5: surf, x6: srcX, x7: srcY, x8: srcDX, x9: srcDY, x10: useColorKey
                        X[0][0] = (byte)0xFF;
                        try
                        {
                            BackSurf.Draw(
                                new Rectangle(BitConverter.ToInt32(X[1], 0), BitConverter.ToInt32(X[2], 0), BitConverter.ToInt32(X[3], 0), BitConverter.ToInt32(X[4], 0)),
                                (Surface)ExObjects[BitConverter.ToUInt32(X[5], 0)],
                                new Rectangle(BitConverter.ToInt32(X[6], 0), BitConverter.ToInt32(X[7], 0), BitConverter.ToInt32(X[8], 0), BitConverter.ToInt32(X[9], 0)),
                                X[10][0] == 0 ? DrawFlags.Wait : (DrawFlags.Wait | DrawFlags.KeySource));
                        }
                        catch (Exception)
                        {
                            X[0][0] = 0;
                        }
                        break;
                    case Opcode.drawline: // x0: err, x1: x1, x2: y1, x3: x2, x4: y2, x5: color(Z)
                        X[0][0] = (byte)0xFF;
                        try
                        {
                            BackSurf.ForeColor = Color.FromArgb(BitConverter.ToInt32(X[5], 0));
                            BackSurf.DrawLine(BitConverter.ToInt32(X[1], 0), BitConverter.ToInt32(X[2], 0), BitConverter.ToInt32(X[3], 0), BitConverter.ToInt32(X[4], 0));
                        }
                        catch (Exception)
                        {
                            X[0][0] = 0;
                        }
                        break;
                    case Opcode.drawbox: // x0: err, x1: x1, x2: y1, x3: x2, x4: y2, x5: bordercolor(Z), x6: fillcolor(Z), x7: transparent
                        X[0][0] = (byte)0xFF;
                        try
                        {
                            BackSurf.ForeColor = Color.FromArgb(BitConverter.ToInt32(X[5], 0));
                            BackSurf.FillColor = Color.FromArgb(BitConverter.ToInt32(X[6], 0));
                            BackSurf.FillStyle = X[7][0] == 0 ? 0 : 1;
                            BackSurf.DrawBox(BitConverter.ToInt32(X[1], 0), BitConverter.ToInt32(X[2], 0), BitConverter.ToInt32(X[3], 0), BitConverter.ToInt32(X[4], 0));
                        }
                        catch (Exception)
                        {
                            X[0][0] = 0;
                        }
                        break;
                    case Opcode.drawrbox: // x0: err, x1: x1, x2: y1, x3: x2, x4: y2, x5: rw, x6: rh, x7: bordercolor(Z), x8: fillcolor(Z), x9: transparent
                        X[0][0] = (byte)0xFF;
                        try
                        {
                            BackSurf.ForeColor = Color.FromArgb(BitConverter.ToInt32(X[7], 0));
                            BackSurf.FillColor = Color.FromArgb(BitConverter.ToInt32(X[8], 0));
                            BackSurf.FillStyle = X[9][0] == 0 ? 0 : 1;
                            BackSurf.DrawRoundedBox(BitConverter.ToInt32(X[1], 0), BitConverter.ToInt32(X[2], 0), BitConverter.ToInt32(X[3], 0), BitConverter.ToInt32(X[4], 0), BitConverter.ToInt32(X[5], 0), BitConverter.ToInt32(X[6], 0));
                        }
                        catch (Exception)
                        {
                            X[0][0] = 0;
                        }
                        break;
                    case Opcode.drawellipse: // x0: err, x1: x1, x2: y1, x3: x2, x4: y2, x5: bordercolor(Z), x6: fillcolor(Z), x7: transparent
                        X[0][0] = (byte)0xFF;
                        try
                        {
                            BackSurf.ForeColor = Color.FromArgb(BitConverter.ToInt32(X[5], 0));
                            BackSurf.FillColor = Color.FromArgb(BitConverter.ToInt32(X[6], 0));
                            BackSurf.FillStyle = X[7][0] == 0 ? 0 : 1;
                            BackSurf.DrawEllipse(BitConverter.ToInt32(X[1], 0), BitConverter.ToInt32(X[2], 0), BitConverter.ToInt32(X[3], 0), BitConverter.ToInt32(X[4], 0));
                        }
                        catch (Exception)
                        {
                            X[0][0] = 0;
                        }
                        break;
                    case Opcode.drawtext: // x0: err, x1: str, x2: fontsize(R), x3: color(Z), x4: x, x5: y, x6: dx, x7: dy
                        X[0][0] = (byte)0xFF;
                        try
                        {
                            tBytes = Heap[BitConverter.ToUInt32(X[1], 0)];
                            tStrBuilder = new StringBuilder(tBytes.Length / 2);
                            for (int i = 0; i < tBytes.Length; i += 2)
                                tStrBuilder.Append(BitConverter.ToChar(tBytes, i));
                            IntPtr hdc = BackSurf.GetDc();
                            Graphics g = Graphics.FromHdc(hdc);
                            g.DrawString(
                                tStrBuilder.ToString(),
                                new Font("", (float)BitConverter.ToDouble(X[2], 0)),
                                new SolidBrush(Color.FromArgb(0xFF, Color.FromArgb(BitConverter.ToInt32(X[3], 0)))),
                                new RectangleF((float)BitConverter.ToInt32(X[4], 0), (float)BitConverter.ToInt32(X[5], 0), (float)BitConverter.ToInt32(X[6], 0), (float)BitConverter.ToInt32(X[7], 0)));
                            BackSurf.ReleaseDc(hdc);
                        }
                        catch (Exception)
                        {
                            X[0][0] = 0;
                        }
                        break;

                    case Opcode.newsndbuf: // x0: err, x1: sndbuf, x2: path
                        tBytes = Heap[BitConverter.ToUInt32(X[2], 0)];
                        tStrBuilder = new StringBuilder(tBytes.Length / 2);
                        for (int i = 0; i < tBytes.Length; i += 2)
                            tStrBuilder.Append(BitConverter.ToChar(tBytes, i));
                        X[0][0] = (byte)0xFF;
                        try
                        {
                            exObj = new SecondaryBuffer(MapPath(tStrBuilder.ToString()), DevSound);
                        }
                        catch (Exception)
                        {
                            X[0][0] = 0;
                            break;
                        }
                        for (uint i = 1; i <= uint.MaxValue; i++)
                            if (ExObjects.ContainsKey(i) == false)
                            {
                                ExObjects.Add(i, exObj);
                                BitConverter.GetBytes((UInt32)i).CopyTo(X[1], 0);
                                break;
                            }
                        break;
                    case Opcode.getvolume: // x0: err, x1: volume, x2: sndbuf
                        X[0][0] = (byte)0xFF;
                        try
                        {
                            BitConverter.GetBytes((Int32)((SecondaryBuffer)ExObjects[BitConverter.ToUInt32(X[2], 0)]).Volume).CopyTo(X[1], 0);
                        }
                        catch (Exception)
                        {
                            X[0][0] = 0;
                        }
                        break;
                    case Opcode.setvolume: // x0: err, x1: sndbuf, x2: volume
                        X[0][0] = (byte)0xFF;
                        try
                        {
                            ((SecondaryBuffer)ExObjects[BitConverter.ToUInt32(X[1], 0)]).Volume = BitConverter.ToInt32(X[2], 0);
                        }
                        catch (Exception)
                        {
                            X[0][0] = 0;
                        }
                        break;
                    case Opcode.play: // x0: err, x1: sndbuf, x2: looping
                        X[0][0] = (byte)0xFF;
                        try
                        {
                            ((SecondaryBuffer)ExObjects[BitConverter.ToUInt32(X[1], 0)]).Play(0, X[2][0] == 0 ? BufferPlayFlags.Default : BufferPlayFlags.Looping);
                        }
                        catch (Exception)
                        {
                            X[0][0] = 0;
                        }
                        break;
                    case Opcode.stop: // x0: err, x1: sndbuf
                        X[0][0] = (byte)0xFF;
                        try
                        {
                            ((SecondaryBuffer)ExObjects[BitConverter.ToUInt32(X[1], 0)]).Stop();
                        }
                        catch (Exception)
                        {
                            X[0][0] = 0;
                        }
                        break;
                }
            }

            Exit = true;
            Application.Exit();
        }

        void mov(byte n)
        {
            Addressing destAddressing = (Addressing)Code[IP];
            IP += 1;
            switch (destAddressing)
            {
                case Addressing.Immediate:
                    throw new Exception(); // movn 123, ... に対応しない
                    //break;
                case Addressing.X:
                    byte destIndexOfX = Code[IP];
                    IP += 1;
                    /*if (destIndexOfX >= 16)
                        throw new Exception();*/
                    Addressing srcAddressing = (Addressing)Code[IP];
                    IP += 1;
                    switch (srcAddressing)
                    {
                        case Addressing.Immediate:
                            byte[] val = new byte[n];
                            Array.Copy(Code, IP, val, 0, n);
                            IP += n;
                            Array.Copy(val, 0, X[destIndexOfX], 0, n); // movn xi, 123
                            break;
                        case Addressing.X:
                            byte srcIndexOfX = Code[IP];
                            IP += 1;
                            /*if (srcIndexOfX >= 16)
                                throw new Exception();*/
                            Array.Copy(X[srcIndexOfX], 0, X[destIndexOfX], 0, n); // mov1 xj, xi
                            break;
                        case Addressing.HP:
                            if (n != 4)
                                throw new Exception(); // mov"4"! xi, hp にしか対応しない
                            byte[] tHP = BitConverter.GetBytes((UInt32)HP);
                            Array.Copy(tHP, 0, X[destIndexOfX], 0, 4);
                            break;
                        case Addressing.Data:
                            uint srcAddrOfData = BitConverter.ToUInt32(Code, (int)IP);
                            IP += 4;
                            Array.Copy(Data, srcAddrOfData, X[destIndexOfX], 0, n); // movn xi, [addr]
                            break;
                        case Addressing.Stack:
                            uint srcOffsetOfStack = BitConverter.ToUInt32(Code, (int)IP);
                            IP += 4;
                            Array.Copy(Stack, SP - srcOffsetOfStack, X[destIndexOfX], 0, n); // movn xi, [sp-addr]
                            break;
                        case Addressing.Heap:
                            srcIndexOfX = Code[IP];
                            IP += 1;
                            Array.Copy(Heap[HP], BitConverter.ToUInt32(X[srcIndexOfX], 0), X[destIndexOfX], 0, n); // movn xi, [hp->xi]
                            break;
                    }
                    break;
                case Addressing.HP:
                    if (n != 4)
                        throw new Exception(); // mov"4"! hp, ... にしか対応しない
                    srcAddressing = (Addressing)Code[IP];
                    IP += 1;
                    switch (srcAddressing)
                    {
                        case Addressing.Immediate:
                            throw new Exception(); // mov4 hp, 123 に対応しない
                            //break;
                        case Addressing.X:
                            byte srcIndexOfX = Code[IP];
                            IP += 1;
                            /*if (srcIndexOfX >= 16)
                                throw new Exception();*/
                            HP = BitConverter.ToUInt32(X[srcIndexOfX], 0); // mov4 hp, xi
                            break;
                        case Addressing.HP:
                            /*HP = HP*/; // mov4 hp, hp にも対応する!
                            break;
                        case Addressing.Data:
                            uint srcAddrOfData = BitConverter.ToUInt32(Code, (int)IP);
                            IP += 4;
                            HP = BitConverter.ToUInt32(Data, (int)srcAddrOfData); // mov4 hp, [addr]
                            break;
                        case Addressing.Stack:
                            uint srcOffsetOfStack = BitConverter.ToUInt32(Code, (int)IP);
                            IP += 4;
                            HP = BitConverter.ToUInt32(Stack, (int)(SP - srcOffsetOfStack)); // mov4 hp, [sp-addr]
                            break;
                        case Addressing.Heap:
                            srcIndexOfX = Code[IP];
                            IP += 1;
                            HP = BitConverter.ToUInt32(Heap[HP], (int)BitConverter.ToUInt32(X[srcIndexOfX], 0)); // mov4 hp, [hp->xi]
                            break;
                    }
                    break;
                case Addressing.Data:
                    uint destAddrOfData = BitConverter.ToUInt32(Code, (int)IP);
                    IP += 4;
                    srcAddressing = (Addressing)Code[IP];
                    IP += 1;
                    switch (srcAddressing)
                    {
                        case Addressing.Immediate:
                            byte[] val = new byte[n];
                            Array.Copy(Code, IP, val, 0, n);
                            IP += n;
                            Array.Copy(val, 0, Data, destAddrOfData, n); // movn [addr], 123
                            break;
                        case Addressing.X:
                            byte srcIndexOfX = Code[IP];
                            IP += 1;
                            /*if (srcIndexOfX >= 16)
                                throw new Exception();*/
                            Array.Copy(X[srcIndexOfX], 0, Data, destAddrOfData, n);// mov1 [addr], xi
                            break;
                        case Addressing.HP:
                            if (n != 4)
                                throw new Exception(); // mov"4"! [addr], hp にしか対応しない
                            byte[] tHP = BitConverter.GetBytes((UInt32)HP);
                            Array.Copy(tHP, 0, Data, destAddrOfData, 4);
                            break;
                        case Addressing.Data:
                            uint srcAddrOfData = BitConverter.ToUInt32(Code, (int)IP);
                            IP += 4;
                            Array.Copy(Data, srcAddrOfData, Data, destAddrOfData, n); // movn [addr1], [addr2]
                            break;
                        case Addressing.Stack:
                            uint srcOffsetOfStack = BitConverter.ToUInt32(Code, (int)IP);
                            IP += 4;
                            Array.Copy(Stack, SP - srcOffsetOfStack, Data, destAddrOfData, n); // movn [addr], [sp-addr]
                            break;
                        case Addressing.Heap:
                            srcIndexOfX = Code[IP];
                            IP += 1;
                            Array.Copy(Heap[HP], BitConverter.ToUInt32(X[srcIndexOfX], 0), Data, destAddrOfData, n); // movn [addr], [hp->xi]
                            break;
                    }
                    break;
                case Addressing.Stack:
                    uint destOffsetOfStack = BitConverter.ToUInt32(Code, (int)IP);
                    IP += 4;
                    srcAddressing = (Addressing)Code[IP];
                    IP += 1;
                    switch (srcAddressing)
                    {
                        case Addressing.Immediate:
                            byte[] val = new byte[n];
                            Array.Copy(Code, IP, val, 0, n);
                            IP += n;
                            Array.Copy(val, 0, Stack, SP - destOffsetOfStack, n); // movn [sp-addr], 123
                            break;
                        case Addressing.X:
                            byte srcIndexOfX = Code[IP];
                            IP += 1;
                            /*if (srcIndexOfX >= 16)
                                throw new Exception();*/
                            Array.Copy(X[srcIndexOfX], 0, Stack, SP - destOffsetOfStack, n); // movn [sp-addr], xi
                            break;
                        case Addressing.HP:
                            if (n != 4)
                                throw new Exception(); // mov"4"! [sp-addr], hp にしか対応しない
                            byte[] tHP = BitConverter.GetBytes((UInt32)HP);
                            Array.Copy(tHP, 0, Stack, SP - destOffsetOfStack, 4);
                            break;
                        case Addressing.Data:
                            uint srcAddrOfData = BitConverter.ToUInt32(Code, (int)IP);
                            IP += 4;
                            Array.Copy(Data, srcAddrOfData, Stack, SP - destOffsetOfStack, n); // movn [sp-addr1], [addr2]
                            break;
                        case Addressing.Stack:
                            uint srcOffsetOfStack = BitConverter.ToUInt32(Code, (int)IP);
                            IP += 4;
                            Array.Copy(Stack, SP - srcOffsetOfStack, Stack, SP - destOffsetOfStack, n); // movn [sp-addr1], [sp-addr2]
                            break;
                        case Addressing.Heap:
                            srcIndexOfX = Code[IP];
                            IP += 1;
                            Array.Copy(Heap[HP], BitConverter.ToUInt32(X[srcIndexOfX], 0), Stack, SP - destOffsetOfStack, n); // movn [sp-addr1], [hp->addr2]
                            break;
                    }
                    break;
                case Addressing.Heap:
                    destIndexOfX = Code[IP];
                    IP += 1;
                    srcAddressing = (Addressing)Code[IP];
                    IP += 1;
                    switch (srcAddressing)
                    {
                        case Addressing.Immediate:
                            byte[] val = new byte[n];
                            Array.Copy(Code, IP, val, 0, n);
                            IP += n;
                            Array.Copy(val, 0, Heap[HP], BitConverter.ToUInt32(X[destIndexOfX], 0), n); // movn [hp->xi], 123
                            break;
                        case Addressing.X:
                            byte srcIndexOfX = Code[IP];
                            IP += 1;
                            /*if (srcIndexOfX >= 16)
                                throw new Exception();*/
                            Array.Copy(X[srcIndexOfX], 0, Heap[HP], BitConverter.ToUInt32(X[destIndexOfX], 0), n); // mov1 [hp->xi], xi
                            break;
                        case Addressing.HP:
                            if (n != 4)
                                throw new Exception(); // mov"4"! [hp->addr], hp にしか対応しない
                            byte[] tHP = BitConverter.GetBytes((UInt32)HP);
                            Array.Copy(tHP, 0, Heap[HP], BitConverter.ToUInt32(X[destIndexOfX], 0), 4);
                            break;
                        case Addressing.Data:
                            uint srcAddrOfData = BitConverter.ToUInt32(Code, (int)IP);
                            IP += 4;
                            Array.Copy(Data, srcAddrOfData, Heap[HP], BitConverter.ToUInt32(X[destIndexOfX], 0), n); // movn [hp->addr1], [addr2]
                            break;
                        case Addressing.Stack:
                            uint srcOffsetOfStack = BitConverter.ToUInt32(Code, (int)IP);
                            IP += 4;
                            Array.Copy(Stack, SP - srcOffsetOfStack, Heap[HP], BitConverter.ToUInt32(X[destIndexOfX], 0), n); // movn [hp->addr1], [sp-addr2]
                            break;
                        case Addressing.Heap:
                            srcIndexOfX = Code[IP];
                            IP += 1;
                            Array.Copy(Heap[HP], BitConverter.ToUInt32(X[srcIndexOfX], 0), Heap[HP], BitConverter.ToUInt32(X[destIndexOfX], 0), n); // movn [hp->xi], [hp->xj]
                            break;
                    }
                    break;
            }
        }

        string MapPath(string x)
        {
            return SimFileParentPath + @"\" + x;
        }

        void MyDoEvents()
        {
            Application.DoEvents();

            bool isActive = GetForegroundWindow() == Handle;

            TheKeyboardState = isActive ? DevKeyboard.GetCurrentKeyboardState() : null;

            MouseState mouseState = DevMouse.CurrentMouseState;
            MouseScrX += mouseState.X;
            MouseScrY += mouseState.Y;
            if (MouseScrX < 0)
                MouseScrX = 0;
            if (MouseScrX >= Screen.PrimaryScreen.Bounds.Width)
                MouseScrX = Screen.PrimaryScreen.Bounds.Width - 1;
            if (MouseScrY < 0)
                MouseScrY = 0;
            if (MouseScrY >= Screen.PrimaryScreen.Bounds.Height)
                MouseScrY = Screen.PrimaryScreen.Bounds.Height - 1;
            SetCursorPos(MouseScrX, MouseScrY);
            Point point = PointToClient(new Point(MouseScrX, MouseScrY));
            MouseX = point.X;
            MouseY = point.Y;
            if (isActive && MouseX >= 0 && MouseX < 1024 && MouseY >= 0 && MouseY < 768)
            {
                byte[] mouseButtons = mouseState.GetMouseButtons();
                MouseLeft = mouseButtons[0] != 0;
                MouseRight = mouseButtons[1] != 0;
                MouseMiddle = mouseButtons[2] != 0;
                MouseDZ = mouseState.Z;
            }
            else
            {
                MouseLeft = false;
                MouseRight = false;
                MouseMiddle = false;
                MouseDZ = 0;
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (Exit == false)
                e.Cancel = true;
        }
    }
}
  • 13
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值