Example #1
0
        public override bool Verify(string[] lines, TheCpu cpu, out string error)
        {
            if (lines.Length > 0)
            {
                if (lines[0].ToUpper() != "ADDRESS")
                {
                    error = "Невалидна инструкция";
                    return(false);
                }
            }

            if (lines.Length == 2)
            {
                if (!cpu.ValidAddress(lines[1]))
                {
                    error = "Първият аргумент на инструкцията трябва да е адрес";
                    return(true);
                }

                error = null;
                return(true);
            }

            error = "Невалидна инструкция";
            return(false);
        }
Example #2
0
        public static void Parse(string lineText, int lineNumber, TheCpu cpu, out string[] lines, out string comment, out string label)
        {
            comment = string.Empty;
            label   = string.Empty;

            var lineWithoutComment = lineText.Trim();

            if (lineWithoutComment.Contains(COMMENT))
            {
                comment            = lineWithoutComment.Substring(lineWithoutComment.IndexOf(COMMENT)).Trim();
                lineWithoutComment = lineWithoutComment.Remove(lineWithoutComment.IndexOf(COMMENT)).Trim();
            }
            if (lineWithoutComment.Contains(LABEL))
            {
                label = lineWithoutComment.Remove(lineWithoutComment.IndexOf(LABEL)).Trim() + ':';
                lineWithoutComment = lineWithoutComment.Substring(lineWithoutComment.IndexOf(LABEL) + 1).Trim();

                var labelName = label.Remove(label.Length - 1).ToUpper();
                if (cpu.Labels.ContainsKey(labelName))
                {
                    cpu.Labels[labelName] = lineNumber;
                }
                else
                {
                    cpu.Labels.Add(labelName, lineNumber);
                }
            }

            lines = lineWithoutComment.Trim().Split(SEPARATORS, StringSplitOptions.RemoveEmptyEntries);
            if (label.Length < FORMAT_LABEL_LENGTH)
            {
                label += new string(' ', FORMAT_LABEL_LENGTH - label.Length);
            }
        }
Example #3
0
        public override bool Verify(string[] lines, TheCpu cpu, out string error)
        {
            if (lines.Length > 0 && lines[0].ToUpper() == commandString)
            {
                if (lines.Length == 1)
                {
                    // CMD Address
                    error = null;
                    return(true);
                }
                else if (lines.Length > 1)
                {
                    if (lines[1] == "C" || lines[1] == "NC" || lines[1] == "Z" || lines[1] == "NZ")
                    {
                        // CMD Condition, Address
                        error = null;
                        return(true);
                    }
                    else
                    {
                        error = "Първият аргумент трябва да е условие";
                        return(true);
                    }
                }
                else
                {
                    error = "Първият аргумент трябва да е условие";
                    return(true);
                }
            }

            error = "Невалидна инструкция";
            return(false);
        }
        public override void Assemble(string[] lines, TheCpu cpu, TheAssembler assembler)
        {
            if (lines.Length == 3)
            {
                switch (lines[1].Remove(lines[1].Length - 1))
                {
                case "C":
                    AssembleInternalC(lines[2], assembler);
                    break;

                case "NC":
                    AssembleInternalNC(lines[2], assembler);
                    break;

                case "Z":
                    AssembleInternalZ(lines[2], assembler);
                    break;

                case "NZ":
                    AssembleInternalNZ(lines[2], assembler);
                    break;

                default:
                    throw new ArgumentException("Невалидно състояние за инструкция JUMP");
                }
            }
            else if (lines.Length == 2)
            {
                AssembleInternal(lines[1], assembler);
            }
            else
            {
                throw new ArgumentException("Невалидна инструкция.");
            }
        }
        public override void Execute(string[] lines, TheCpu cpu)
        {
            if (lines.Length == 3)
            {
                switch (lines[1].Remove(lines[1].Length - 1))
                {
                case "C":
                    ExecuteInternal(lines[2], cpu, cpu.CARRY);
                    break;

                case "NC":
                    ExecuteInternal(lines[2], cpu, !cpu.CARRY);
                    break;

                case "Z":
                    ExecuteInternal(lines[2], cpu, cpu.ZERO);
                    break;

                case "NZ":
                    ExecuteInternal(lines[2], cpu, !cpu.ZERO);
                    break;

                default:
                    throw new ArgumentException("Невалидно състояние за инструкция JUMP");
                }
            }
            else if (lines.Length == 2)
            {
                ExecuteInternal(lines[1], cpu, true);
            }
            else
            {
                throw new ArgumentException("Невалидна инструкция.");
            }
        }
Example #6
0
        public override bool Verify(string[] lines, TheCpu cpu, out string error)
        {
            if (lines.Length > 0)
            {
                if (lines[0].ToUpper() != commandString)
                {
                    error = "Невалидна инструкция";
                    return(false);
                }
            }

            if (lines.Length == 2)
            {
                if (cpu.ValidRegister(lines[1]))
                {
                    // CMD sX
                    error = null;
                    return(true);
                }
                else
                {
                    error = "Първият аргумент на инструкцията трябва да е регистър";
                    return(true);
                }
            }

            error = "Невалидна инструкция";
            return(false);
        }
Example #7
0
        protected override void ExecuteInternal(string register, TheCpu cpu)
        {
            var sX = cpu.GetRegister(register);

            switch (this.commandString)
            {
            case "SL0":
                bit = 0;
                break;

            case "SL1":
                bit = 1;
                break;

            case "SLX":
                bit = (byte)(sX & 0x01);
                break;

            case "SLA":
                bit = (byte)(cpu.CARRY ? 1 : 0);
                break;

            default:
                throw new ArgumentException("Невалидна операция по изместване на ляво");
            }

            cpu.CARRY = (sX & 0x80) == 0x80;

            sX = (byte)((sX << 1) | bit);

            cpu.SetRegister(register, sX);
            cpu.ZERO = sX == 0;
            cpu.PC++;
        }
Example #8
0
        public override void Assemble(string[] lines, TheCpu cpu, TheAssembler assembler)
        {
            //111000000000000000
            var bits = "111000000000000000";

            assembler.Instruction(assembler.address, bits);
            assembler.address++;
        }
Example #9
0
        public void Enable(TheCpu CPU)
        {
            this.scratchpadByteProvider          = new DynamicByteProvider(CPU.Scratchpad);
            this.scratchpadByteProvider.Changed += new EventHandler(scratchpadByteProvider_Changed);
            this.hexBox.ByteProvider             = scratchpadByteProvider;

            this.Enabled = true;
        }
Example #10
0
        protected override void ExecuteV1(string register1, string register2, TheCpu cpu)
        {
            var sX = cpu.GetRegister(register1);
            var sY = cpu.GetRegister(register2);

            cpu.WriteRAM(sY, sX);
            cpu.PC++;
        }
Example #11
0
        protected override void ExecuteV2(string register, string literal, TheCpu cpu)
        {
            var sX = cpu.GetRegister(register);
            var kk = cpu.GetLiteral(literal);

            cpu.WriteRAM(kk, sX);
            cpu.PC++;
        }
Example #12
0
        protected override void ExecuteV2(string register, string literal, TheCpu cpu)
        {
            var kk = cpu.GetLiteral(literal);

            cpu.SetRegister(register, kk);

            cpu.PC++;
        }
Example #13
0
        protected override void ExecuteV1(string register1, string register2, TheCpu cpu)
        {
            var sY = cpu.GetRegister(register2);

            cpu.SetRegister(register1, sY);

            cpu.PC++;
        }
Example #14
0
        public override void Execute(string[] lines, TheCpu cpu)
        {
            cpu.PC = cpu.Stack.Pop();
            cpu.OnPropertyChanged(cpu.PropertyName(() => cpu.Stack));

            cpu.ZERO             = cpu.PRESERVED_ZERO;
            cpu.CARRY            = cpu.PRESERVED_CARRY;
            cpu.INTERRUPT_ENABLE = false;
        }
Example #15
0
        protected override void ExecuteV1(string register1, string register2, TheCpu cpu)
        {
            var sX = cpu.GetRegister(register1);
            var sY = cpu.GetRegister(register2);

            cpu.ZERO  = sX == sY;
            cpu.CARRY = sX > sY;
            cpu.PC++;
        }
Example #16
0
        public override bool Verify(string[] lines, TheCpu cpu, out string error)
        {
            if (string.Join(" ", lines).ToUpper() == commandString)
            {
                error = null;
                return(true);
            }

            error = "Невалидна инструкция";
            return(false);
        }
Example #17
0
        public override bool Verify(string[] lines, TheCpu cpu, out string error)
        {
            if (lines.Length == 0)
            {
                error = null;
                return(true);
            }

            error = "Невалидна инструкция";
            return(false);
        }
Example #18
0
        protected override void ExecuteV2(string register, string literal, TheCpu cpu)
        {
            var sX = cpu.GetRegister(register);
            var kk = cpu.GetLiteral(literal);

            var r = (byte)(sX & kk);

            cpu.ZERO  = sX == kk;
            cpu.CARRY = sX > kk;
            cpu.PC++;
        }
Example #19
0
 protected override void ExecuteInternal(TheCpu cpu, bool value)
 {
     if (value)
     {
         cpu.PC = cpu.Stack.Pop() + 1;
         cpu.OnPropertyChanged(cpu.PropertyName(() => cpu.Stack));
     }
     else
     {
         cpu.PC++;
     }
 }
Example #20
0
 protected override void ExecuteInternal(string address, TheCpu cpu, bool value)
 {
     if (value)
     {
         var addressValue = cpu.GetAddress(address);
         cpu.PC = addressValue;
     }
     else
     {
         cpu.PC++;
     }
 }
Example #21
0
        protected override void ExecuteInternal(string register, TheCpu cpu)
        {
            var sX = cpu.GetRegister(register);

            cpu.CARRY = (sX & 0x01) == 0;

            sX = (byte)((sX >> 1) | (sX << (8 - 1)));

            cpu.SetRegister(register, sX);
            cpu.ZERO = sX == 0;
            cpu.PC++;
        }
Example #22
0
        protected override void ExecuteV2(string register, string literal, TheCpu cpu)
        {
            var sX = cpu.GetRegister(register);
            var kk = cpu.GetLiteral(literal);

            var r = (byte)((sX - kk) % 256);

            cpu.SetRegister(register, r);
            cpu.ZERO  = ((sX - kk) == 0);
            cpu.CARRY = ((sX - kk) < 0);
            cpu.PC++;
        }
Example #23
0
        protected override void ExecuteV1(string register1, string register2, TheCpu cpu)
        {
            var sX = cpu.GetRegister(register1);
            var sY = cpu.GetRegister(register2);

            var r = (byte)((sX - sY) % 256);

            cpu.SetRegister(register1, r);
            cpu.ZERO  = ((sX - sY) == 0);
            cpu.CARRY = ((sX - sY) < 0);
            cpu.PC++;
        }
Example #24
0
        protected override void ExecuteV1(string register1, string register2, TheCpu cpu)
        {
            var sX = cpu.GetRegister(register1);
            var sY = cpu.GetRegister(register2);

            var r = (byte)(sX ^ sY);

            cpu.SetRegister(register1, r);
            cpu.ZERO  = (r == 0);
            cpu.CARRY = false;
            cpu.PC++;
        }
Example #25
0
        protected override void ExecuteV1(string register1, string register2, TheCpu cpu)
        {
            var sX = cpu.GetRegister(register1);
            var sY = cpu.GetRegister(register2);

            var CARRY = (cpu.CARRY ? 1 : 0);
            var r     = (byte)((sX + sY + CARRY) % 256);

            cpu.SetRegister(register1, r);
            cpu.ZERO  = (r == 0);
            cpu.CARRY = ((sX + sY + CARRY) > 0xFF);
            cpu.PC++;
        }
Example #26
0
        protected override void ExecuteV2(string register, string literal, TheCpu cpu)
        {
            var sX = cpu.GetRegister(register);
            var kk = cpu.GetLiteral(literal);

            var CARRY = (cpu.CARRY ? 1 : 0);
            var r     = (byte)((sX + kk + CARRY) % 256);

            cpu.SetRegister(register, r);
            cpu.ZERO  = (r == 0);
            cpu.CARRY = ((sX + kk + CARRY) > 0xFF);
            cpu.PC++;
        }
Example #27
0
        private void CodeOpen(string code)
        {
            this.codeEditor.keywordsLabels.Clear();
            this.codeEditor.keywordsConstants.Clear();
            this.codeEditor.breakpointLines.Clear();
            this.codeEditor.SourceCode = code;

            this.CPU = new TheCpu(this);
            this.stackTool.Prepare(this.CPU);
            this.scratchpadTool.Prepare(this.CPU);

            this.EnableFileControls(true);
        }
Example #28
0
        public override bool Verify(string[] lines, TheCpu cpu, out string error)
        {
            if (lines.Length > 0)
            {
                if (lines[0].ToUpper() != "CONSTANT")
                {
                    error = "Невалидна инструкция";
                    return(false);
                }
            }

            if (lines.Length > 1)
            {
                if (!lines[1].EndsWith(","))
                {
                    error = "Липсваща запетайка";
                    return(true);
                }
            }

            if (lines.Length == 3)
            {
                if (cpu.ValidLiteral(lines[2]))
                {
                    // CONSTANT literal, kk
                    var name = lines[1].Remove(lines[1].Length - 1).ToUpper();

                    if (cpu.Literals.ContainsKey(name))
                    {
                        cpu.Literals[name] = cpu.GetLiteral(lines[2]);
                    }
                    else
                    {
                        cpu.Literals.Add(name, cpu.GetLiteral(lines[2]));
                    }

                    error = null;
                    return(true);
                }
                else
                {
                    error = "Вторият аргумент на инструкцията трябва да е литерал";
                    return(true);
                }
            }

            error = "Невалидна инструкция";
            return(false);
        }
        public override bool Verify(string[] lines, TheCpu cpu, out string error)
        {
            if (lines.Length > 0)
            {
                if (lines[0].ToUpper() != commandString)
                {
                    error = "Невалидна инструкция";
                    return(false);
                }
            }

            if (lines.Length > 1)
            {
                if (!lines[1].EndsWith(","))
                {
                    error = "Липсваща запетайка";
                    return(true);
                }
                if (!cpu.ValidRegister(lines[1].Remove(lines[1].Length - 1)))
                {
                    error = "Първият аргумент на инструкцията трябва да е регистър";
                    return(true);
                }
            }

            if (lines.Length == 3)
            {
                if (cpu.ValidRegister(lines[2]))
                {
                    // CMD sX, sY
                    error = null;
                    return(true);
                }
                else if (cpu.ValidLiteral(lines[2]))
                {
                    // CMD sX, kk
                    error = null;
                    return(true);
                }
                else
                {
                    error = "Вторият аргумент на инструкцията трябва да е регистър или литерал";
                    return(true);
                }
            }

            error = "Невалидна инструкция";
            return(false);
        }
        public override bool Verify(string[] lines, TheCpu cpu, out string error)
        {
            if (lines.Length > 0 && lines[0].ToUpper() == commandString)
            {
                if (lines.Length > 1 && cpu.ValidAddress(lines[1]))
                {
                    // CMD Address
                    error = null;
                    return(true);
                }
                else if (lines.Length > 1)
                {
                    if (!lines[1].EndsWith(","))
                    {
                        error = "Липсваща запетайка";
                        return(true);
                    }

                    var lines1 = lines[1].Remove(lines[1].Length - 1);
                    if (lines1 == "C" || lines1 == "NC" || lines1 == "Z" || lines1 == "NZ")
                    {
                        if (lines.Length > 2 && cpu.ValidAddress(lines[2]))
                        {
                            // CMD Condition, Address
                            error = null;
                            return(true);
                        }
                        else
                        {
                            error = "Вторият аргумент трябва да е адрес или етикет";
                            return(true);
                        }
                    }
                    else
                    {
                        error = "Първият аргумент трябва да е адрес, етикет или условие";
                        return(true);
                    }
                }
                else
                {
                    error = "Първият аргумент трябва да е адрес, етикет или условие";
                    return(true);
                }
            }

            error = "Невалидна инструкция";
            return(false);
        }