public void ExecuteNextInstructionWhenCalledFiresInstructionWillExecuteIfWired()
        {
            var receivedEvents = new Dictionary<ushort, Instruction>();

            var reader = new StringReader("SET PUSH, 0x10");
            var lexer = new CodeLexer(reader, this.matchers);
            var directOperandFactory = new DirectOperandFactory();
            var indirectOperandFactory = new IndirectOperandFactory();
            var parser = new Parser(lexer, directOperandFactory, indirectOperandFactory);

            var statments = parser.Parse();
            var assembler = new Assembler();
            var program = assembler.AssembleStatments(statments);

            var operandFactory = new InstructionOperandFactory();
            var instructionBuilder = new InstructionBuilder(operandFactory);
            var memory = new Memory();
            memory.LoadData(program);
            var cpu = new Cpu(instructionBuilder) { Memory = memory };
            cpu.InstructionWillExecute += receivedEvents.Add;
            cpu.ExecuteNextInstruction();

            Assert.That(receivedEvents.Keys.First() == program.ToList()[0]);
        }
        public void CanStepThrougthModifiedNotchSample()
        {
            const string Code =
                @"  ;Try some basic stuff
                                    SET A, 0x30              ; 7c01 0030
                                    SET [0x1000], 0x20       ; 7de1 1000 0020
                                    SUB A, [0x1000]          ; 7803 1000
                                    IFN A, 0x10              ; c00d
                                    SET PC, crash            ; 7dc1 001a [*]

                                    ; Do a loopy thing
                                    SET I, 10                ; a861
                                    SET A, 0x2000            ; 7c01 2000
                                    :loop         SET [0x2000+I], [A]      ; 2161 2000
                                    SUB I, 1                 ; 8463
                                    IFN I, 0                 ; 806d
                                    SET PC, loop             ; 7dc1 000d [*]

                                    ; Call a subroutine
                                    SET X, 0x4               ; 9031
                                    JSR testsub              ; 7c10 0018 [*]
                                    SET PC, crash            ; 7dc1 001a [*]

                                    :testsub      SHL X, 4   ; 9037
                                    SET PC, POP              ; 61c1

                                    ; Hang forever. X should now be 0x40 if everything went right.
									:crash        SET A, 0            ; 7dc1 001a [*]";

            var reader = new StringReader(Code);
            var lexer = new CodeLexer(reader, this.matchers);
            var directOperandFactory = new DirectOperandFactory();
            var indirectOperandFactory = new IndirectOperandFactory();
            var parser = new Parser(lexer, directOperandFactory, indirectOperandFactory);

            var statments = parser.Parse();
            var assembler = new Assembler();
            var program = assembler.AssembleStatments(statments);

            var operandFactory = new InstructionOperandFactory();
            var builder = new InstructionBuilder(operandFactory);
            var memory = new Memory();
            memory.LoadData(program);
            var cpu = new Cpu(builder) { Memory = memory };

            var executed = true;

            while (executed)
            {
                executed = cpu.ExecuteNextInstruction();
            }

            Assert.That(cpu.ReadGeneralPursoseRegisterValue((ushort)RegisterIdentifier.RegX), Is.EqualTo(0x40));
        }
        public void CanStepThrougthHelloWorldSample()
        {
            const string Code = @"
; Assembler test for DCPU
; by Markus Persson

             set a, 0xbeef                        ; Assign 0xbeef to register a
             set [0x1000], a                      ; Assign memory at 0x1000 to value of register a
             ifn a, [0x1000]                      ; Compare value of register a to memory at 0x1000 ..
                 set PC, end                      ; .. and jump to end if they don't match

             set i, 0                             ; Init loop counter, for clarity
:nextchar    ife [data+i], 0                      ; If the character is 0 ..
                 set PC, end                      ; .. jump to the end
             set [0x8000+i], [data+i]             ; Video ram starts at 0x8000, copy char there
             add i, 1                             ; Increase loop counter
             set PC, nextchar                     ; Loop
  
:data        dat ""Hello world!"", 0              ; Zero terminated string

:end         SET A, 1                             ; Freeze the CPU forever";

            var reader = new StringReader(Code);
            var lexer = new CodeLexer(reader, this.matchers);
            var directOperandFactory = new DirectOperandFactory();
            var indirectOperandFactory = new IndirectOperandFactory();
            var parser = new Parser(lexer, directOperandFactory, indirectOperandFactory);

            var statments = parser.Parse();
            var assembler = new Assembler();
            var program = assembler.AssembleStatments(statments);

            var operandFactory = new InstructionOperandFactory();
            var builder = new InstructionBuilder(operandFactory);
            var memory = new Memory();
            memory.LoadData(program);
            var cpu = new Cpu(builder) { Memory = memory };
            var receivedEvents = new Dictionary<int, ushort>();
            memory.VideoMemoryDidChange += receivedEvents.Add;

            var executed = true;

            while (executed)
            {
                executed = cpu.ExecuteNextInstruction();
            }

            const string ExpectedValues = "\"Helloworld!\"";

            var i = 0;
            foreach (var expectedValue in ExpectedValues)
            {
                Assert.That(receivedEvents[0x8000 + i], Is.EqualTo((ushort)expectedValue));
                i++;
            }
        }
        public void ExecuteWhenCalledWithJsrSetsProgramCounterToOperandValue()
        {
            var reader = new StringReader("JSR 0x04");
            var lexer = new CodeLexer(reader, this.matchers);
            var directOperandFactory = new DirectOperandFactory();
            var indirectOperandFactory = new IndirectOperandFactory();
            var parser = new Parser(lexer, directOperandFactory, indirectOperandFactory);

            var statments = parser.Parse();
            var assembler = new Assembler();
            var program = assembler.AssembleStatments(statments);

            var operandFactory = new InstructionOperandFactory();
            var builder = new InstructionBuilder(operandFactory);
            var memory = new Memory();
            memory.LoadData(program);
            var cpu = new Cpu(builder) { Memory = memory };
            cpu.SetProgramCounter(0x10);
            var instruction = builder.Build(program.ToList()[0], cpu);
            instruction.Execute();

            Assert.That(cpu.ProgramCounter, Is.EqualTo(0x04));
        }
        public void ExecuteWhenCalledWithIfEqualsAndResultIsFalseSetsIgnoreNextInstruction()
        {
            var reader = new StringReader("IFE A, 1");
            var lexer = new CodeLexer(reader, this.matchers);
            var directOperandFactory = new DirectOperandFactory();
            var indirectOperandFactory = new IndirectOperandFactory();
            var parser = new Parser(lexer, directOperandFactory, indirectOperandFactory);

            var statments = parser.Parse();
            var assembler = new Assembler();
            var program = assembler.AssembleStatments(statments);

            var operandFactory = new InstructionOperandFactory();
            var builder = new InstructionBuilder(operandFactory);
            var memory = new Memory();
            memory.LoadData(program);
            var cpu = new Cpu(builder) { Memory = memory };
            var instruction = builder.Build(program.ToList()[0], cpu);
            instruction.Execute();

            Assert.That(cpu.IgnoreNextInstruction, Is.EqualTo(true));
        }
        public void ExecuteWhenCalledWithSubLiteralToRegisterValueSetsCorrectRegisterValue(
            string code, ushort registerAddress, int expectedValue)
        {
            var reader = new StringReader(code);
            var lexer = new CodeLexer(reader, this.matchers);
            var directOperandFactory = new DirectOperandFactory();
            var indirectOperandFactory = new IndirectOperandFactory();
            var parser = new Parser(lexer, directOperandFactory, indirectOperandFactory);

            var statments = parser.Parse();
            var assembler = new Assembler();
            var program = assembler.AssembleStatments(statments);

            var operandFactory = new InstructionOperandFactory();
            var builder = new InstructionBuilder(operandFactory);
            var memory = new Memory();
            memory.LoadData(program);
            var cpu = new Cpu(builder) { Memory = memory };
            var instruction1 = builder.Build(program.ToList()[0], cpu);
            var instruction2 = builder.Build(program.ToList()[1], cpu);
            instruction1.Execute();
            instruction2.Execute();

            Assert.That(cpu.ReadGeneralPursoseRegisterValue(registerAddress), Is.EqualTo(expectedValue));
        }
        public void ExecuteWhenCalledWithSetRegisterWithPopSetsCorrectRegistryValue()
        {
            const string Code = @"SET PUSH, 0x10
								  SET I, POP";

            var reader = new StringReader(Code);
            var lexer = new CodeLexer(reader, this.matchers);
            var directOperandFactory = new DirectOperandFactory();
            var indirectOperandFactory = new IndirectOperandFactory();
            var parser = new Parser(lexer, directOperandFactory, indirectOperandFactory);

            var statments = parser.Parse();
            var assembler = new Assembler();
            var program = assembler.AssembleStatments(statments);

            var operandFactory = new InstructionOperandFactory();
            var builder = new InstructionBuilder(operandFactory);
            var memory = new Memory();
            memory.LoadData(program);
            var cpu = new Cpu(builder) { Memory = memory };
            var instruction1 = builder.Build(program.ToList()[0], cpu);
            var instruction2 = builder.Build(program.ToList()[1], cpu);
            instruction1.Execute();
            instruction2.Execute();

            Assert.That(cpu.ReadGeneralPursoseRegisterValue((ushort)RegisterIdentifier.RegI), Is.EqualTo(0x10));
        }
        public void BuildWhenCalledForRawInstructionBuildsExpectedInstructionToken(
            string code)
        {
            var reader = new StringReader(code);
            var lexer = new CodeLexer(reader, this.matchers);
            var directOperandFactory = new DirectOperandFactory();
            var indirectOperandFactory = new IndirectOperandFactory();
            var parser = new Parser(lexer, directOperandFactory, indirectOperandFactory);

            var statments = parser.Parse();
            var assembler = new Assembler();
            var program = assembler.AssembleStatments(statments);

            var operandFactory = new InstructionOperandFactory();
            var builder = new InstructionBuilder(operandFactory);
            var memory = new Memory();
            memory.LoadData(program);
            var cpu = new Cpu(builder) { Memory = memory };

            var instruction = builder.Build(program.ToList()[0], cpu);

            Assert.That(instruction.Token, Is.EqualTo(code.Substring(0, 3)));
        }
        public void ExecuteWhenCalledWithSetPushWithLiteralSetsCorrectMemoryValue()
        {
            var reader = new StringReader("SET PUSH, 0x10");
            var lexer = new CodeLexer(reader, this.matchers);
            var directOperandFactory = new DirectOperandFactory();
            var indirectOperandFactory = new IndirectOperandFactory();
            var parser = new Parser(lexer, directOperandFactory, indirectOperandFactory);

            var statments = parser.Parse();
            var assembler = new Assembler();
            var program = assembler.AssembleStatments(statments);

            var operandFactory = new InstructionOperandFactory();
            var builder = new InstructionBuilder(operandFactory);
            var memory = new Memory();
            memory.LoadData(program);
            var cpu = new Cpu(builder) { Memory = memory };
            var instruction = builder.Build(program.ToList()[0], cpu);
            instruction.Execute();

            Assert.That(cpu.ReadMemoryValueAtAddress(cpu.StackPointer), Is.EqualTo(0x10));
        }
        public void ExecuteWhenCalledWithSetOffsetMemoryAddressWithNextWordSetsCorrectMemoryValue(
            string code, ushort memoryAddress, int expectedValue)
        {
            var reader = new StringReader(code);
            var lexer = new CodeLexer(reader, this.matchers);
            var directOperandFactory = new DirectOperandFactory();
            var indirectOperandFactory = new IndirectOperandFactory();
            var parser = new Parser(lexer, directOperandFactory, indirectOperandFactory);

            var statments = parser.Parse();
            var assembler = new Assembler();
            var program = assembler.AssembleStatments(statments);

            var operandFactory = new InstructionOperandFactory();
            var builder = new InstructionBuilder(operandFactory);
            var memory = new Memory();
            memory.LoadData(program);
            var cpu = new Cpu(builder) { Memory = memory };
            var instruction = builder.Build(program.ToList()[0], cpu);
            instruction.Execute();

            Assert.That(cpu.ReadMemoryValueAtAddress(memoryAddress), Is.EqualTo(expectedValue));
        }
        public void ExecuteWhenCalledWithPeekDoesNotChangeStackPointer()
        {
            const string Code = @"SET PUSH, 0x10
								  SET I, PEEK";

            var reader = new StringReader(Code);
            var lexer = new CodeLexer(reader, this.matchers);
            var directOperandFactory = new DirectOperandFactory();
            var indirectOperandFactory = new IndirectOperandFactory();
            var parser = new Parser(lexer, directOperandFactory, indirectOperandFactory);

            var statments = parser.Parse();
            var assembler = new Assembler();
            var program = assembler.AssembleStatments(statments);

            var operandFactory = new InstructionOperandFactory();
            var builder = new InstructionBuilder(operandFactory);
            var memory = new Memory();
            memory.LoadData(program);
            var cpu = new Cpu(builder) { Memory = memory };
            var instruction1 = builder.Build(program.ToList()[0], cpu);
            var instruction2 = builder.Build(program.ToList()[1], cpu);
            instruction1.Execute();
            instruction2.Execute();

            Assert.That(cpu.StackPointer, Is.EqualTo(ushort.MaxValue));
        }
        public void ExecuteWhenCalledAndOperationOverflowsSetsOverflowRegister(string code, int expectedValue)
        {
            var reader = new StringReader(code);
            var lexer = new CodeLexer(reader, this.matchers);
            var directOperandFactory = new DirectOperandFactory();
            var indirectOperandFactory = new IndirectOperandFactory();
            var parser = new Parser(lexer, directOperandFactory, indirectOperandFactory);

            var statments = parser.Parse();
            var assembler = new Assembler();
            var program = assembler.AssembleStatments(statments);

            var operandFactory = new InstructionOperandFactory();
            var builder = new InstructionBuilder(operandFactory);

            var memory = new Memory();
            memory.LoadData(program);
            var cpu = new Cpu(builder) { Memory = memory };
            var instruction1 = builder.Build(program.ToList()[0], cpu);
            var instruction2 = builder.Build(program.ToList()[1], cpu);
            instruction1.Execute();
            cpu.IncrementProgramCounter();
            instruction2.Execute();

            Assert.That(cpu.Overflow, Is.EqualTo(expectedValue));
        }