Пример #1
0
        public static string CommandToStr(COMAMND cmd)
        {
            switch (cmd)
            {
            case COMAMND.ADD: return("ADD");

            case COMAMND.DEC: return("DEC");

            case COMAMND.HALT: return("HALT");

            case COMAMND.IFN: return("IFN");

            case COMAMND.IFZ: return("IFZ");

            case COMAMND.IN: return("IN");

            case COMAMND.INC: return("INC");

            case COMAMND.JMP: return("JMP");

            case COMAMND.LDR: return("LDR");

            case COMAMND.NOP: return("NOP");

            case COMAMND.OUT: return("OUT");

            case COMAMND.STR: return("STR");

            case COMAMND.SUB: return("SUB");
            }
            return("???");
        }
Пример #2
0
        public static Tuple <COMAMND, MODRM, int> UnPack(int memPiece)
        {
            COMAMND cop     = (COMAMND)(memPiece & 0xf);
            MODRM   modrm   = (MODRM)((memPiece >> 4) & 0x3);
            int     operand = (memPiece >> 6) & 0x3ffffff;

            return(new Tuple <COMAMND, MODRM, int>(cop, modrm, operand));
        }
Пример #3
0
 private static int Pack(COMAMND cop, MODRM modrm, int operand)
 {
     return(((int)cop) | ((int)modrm << 4) | (operand << 6));
 }
Пример #4
0
        static void Run()
        {
            int state = 1;

            while (state != 0)
            {
                if (dbgIsOn)
                {
                    if (dbgMemEdges != null)
                    {
                        Debugger.Debug(rx, pc, mem, pc - dbgCodeScope, pc + dbgCodeScope, dbgMemEdges[0], dbgMemEdges[1]);  //debug!!
                    }
                    else
                    {
                        Debugger.Debug(rx, pc, mem, pc - dbgCodeScope, pc + dbgCodeScope);
                    }
                }

                var     unpackRes = UnPack(mem[pc]);
                COMAMND cop       = unpackRes.Item1;
                MODRM   modrm     = unpackRes.Item2;
                int     operand   = unpackRes.Item3;

                #region Выбор по коду операции
                switch (cop)
                {
                case COMAMND.NOP: break;

                case COMAMND.ADD:
                    switch (modrm)
                    {
                    case MODRM.Imed: Add(operand); break;

                    case MODRM.Direct: Add(mem[operand]); break;

                    case MODRM.Indirect: Add(mem[mem[operand]]); break;

                    default:
                        HandleError();
                        break;
                    }
                    break;

                case COMAMND.SUB:
                    switch (modrm)
                    {
                    case MODRM.Imed: Sub(operand); break;

                    case MODRM.Direct: Sub(mem[operand]); break;

                    case MODRM.Indirect: Sub(mem[mem[operand]]); break;

                    default:
                        HandleError();
                        break;
                    }
                    break;


                case COMAMND.INC:
                    if (modrm == MODRM.Direct)
                    {
                        Inc(operand);
                    }
                    else if (modrm == MODRM.Indirect)
                    {
                        Inc(mem[operand]);
                    }
                    else
                    {
                        HandleError();
                    }
                    break;

                case COMAMND.DEC:
                    if (modrm == MODRM.Direct)
                    {
                        Dec(operand);
                    }
                    else if (modrm == MODRM.Indirect)
                    {
                        Dec(mem[operand]);
                    }
                    else
                    {
                        HandleError();
                    }
                    break;

                case COMAMND.LDR:
                    if (modrm == MODRM.Imed)
                    {
                        Ldr(operand);
                    }
                    else if (modrm == MODRM.Direct)
                    {
                        Ldr(mem[operand]);
                    }
                    else if (modrm == MODRM.Indirect)
                    {
                        Ldr(mem[mem[operand]]);
                    }
                    else
                    {
                        HandleError();
                    }
                    break;

                case COMAMND.STR:
                    if (modrm == MODRM.Direct)
                    {
                        Str(operand);
                    }
                    else if (modrm == MODRM.Indirect)
                    {
                        Str(mem[operand]);
                    }
                    else
                    {
                        HandleError();
                    }
                    break;

                case COMAMND.IN:
                    if (modrm == MODRM.NoOperand)
                    {
                        In();
                    }
                    else
                    {
                        HandleError();
                    }
                    break;

                case COMAMND.OUT:
                    if (modrm == MODRM.NoOperand)
                    {
                        Out();
                    }
                    else
                    {
                        HandleError();
                    }
                    break;

                case COMAMND.JMP:
                    if (modrm == MODRM.Imed)
                    {
                        Jmp(operand);
                    }
                    else if (modrm == MODRM.Indirect)
                    {
                        Jmp(mem[operand]);
                    }
                    else
                    {
                        HandleError();
                    }
                    break;

                case COMAMND.IFZ:
                    if (modrm == MODRM.NoOperand)
                    {
                        Ifz();
                    }
                    else
                    {
                        HandleError();
                    }
                    break;

                case COMAMND.IFN:
                    if (modrm == MODRM.NoOperand)
                    {
                        Ifn();
                    }
                    else
                    {
                        HandleError();
                    }
                    break;

                case COMAMND.HALT:
                    if (modrm == MODRM.NoOperand)
                    {
                        state = 0;
                    }
                    else
                    {
                        HandleError();
                    }
                    break;

                default:
                    if (modrm == MODRM.NoOperand)
                    {
                        state = 0;
                    }
                    else
                    {
                        HandleError();
                    }
                    break;
                }//switch
                #endregion
                pc = (pc + 1) % 256;
            }//while
        }