Example #1
0
            public void Decode_Instruction(String inst, int address)
            {
                switch (inst)
                {
                case "HLT":
                    registers[2] |= 0x2;
                    break;

                case "LOD":
                    if (registers[1] == 2)
                    {
                        registers[0] = (Bus.Get_Values())[0];
                    }
                    else
                    {
                        registers[0] = mainMemory[registers[1]];
                    }

                    break;

                case "STR":

                    if (registers[1] == 1)
                    {
                        mainMemory[registers[1]] &= 0x3;
                        mainMemory[registers[1]]  = (UInt16)(((UInt16)registers[0] & 0xC) | mainMemory[registers[1]]);
                    }
                    else
                    {
                        mainMemory[registers[1]] = (UInt16)registers[0];
                    }
                    if (registers[1] < 3)
                    {
                        Bus.Update_Bus(mainMemory[2], mainMemory[1], mainMemory[0], 16);
                    }

                    break;

                case "ADD":
                    int temp1  = registers[0];
                    int temp2  = mainMemory[registers[1]];
                    int result = temp1 + temp2;
                    result      += (registers[2] & 0x1);
                    registers[0] = result & 0xF;

                    //Set overflow bit
                    if (((temp1 & 0x8) == 0x8) && ((temp2 & 0x8) == 0x8) && ((result & 0x8) != 0x8))
                    {
                        registers[2] |= 0x8;
                        registers[2] ^= (result & 0x8);
                    }
                    else if (((temp1 & 0x8) != 0x8) && ((temp2 & 0x8) != 0x8) && ((result & 0x8) == 0x8))
                    {
                        registers[2] |= 0x8;
                        registers[2] ^= (result & 0x8);
                    }
                    else
                    {
                        registers[2] &= 0x7;
                        registers[2] ^= (result & 0x8);
                    }

                    //If there was a carry out
                    if ((result & 0x10) == 0x10)
                    {
                        registers[2] |= 0x1;
                    }
                    else
                    {
                        registers[2] &= 0xE;
                    }
                    break;

                case "NOP":
                    break;

                case "NND":
                    registers[0] &= mainMemory[registers[1]];
                    registers[0]  = ~registers[0];
                    registers[0] &= 0xF;
                    break;

                case "JMP":
                    if (registers[0] == 0)
                    {
                        registers[3] = registers[1];
                    }
                    break;

                case "CXA":
                    registers[0] = registers[2];
                    break;

                default:
                    registers[2] |= 0x2;
                    break;
                }
            }