Ejemplo n.º 1
0
        public override void EnterInstructionStatement([NotNull] ZealCpuParser.InstructionStatementContext context)
        {
            _currentInstruction = new CpuInstructionStatement();

            _currentInstruction.Line = context.Start.Line;
            _currentInstruction.Column = context.Start.Column;

            CpuInstructions opcode;
            if (Enum.TryParse<CpuInstructions>(context.opcode().GetText(), out opcode))
            {
                _currentInstruction.Opcode = opcode;
            }
        }
Ejemplo n.º 2
0
        public void ShouldGenerateAbsoluteInstruction(CpuInstructions opcodeEnum, byte finalOpcode, int value)
        {
            CpuInstructionStatement instruction = new CpuInstructionStatement();
            instruction.AddressingMode = CpuAddressingMode.Absolute;
            instruction.Opcode = opcodeEnum;
            instruction.Arguments.Add(new NumberInstructionArgument(value, ArgumentSize.Word));

            List<CpuInstructionStatement> instructions = new List<CpuInstructionStatement>();
            instructions.Add(instruction);

            MemoryStream memoryStream = new MemoryStream(8);
            CpuCodeGenerator generator = new CpuCodeGenerator(memoryStream);
            generator.Instructions = instructions;
            generator.Generate();

            Assert.Equal(finalOpcode, memoryStream.GetBuffer()[0]);
            Assert.Equal((byte)(value & 0xFF), memoryStream.GetBuffer()[1]);
            Assert.Equal((byte)(value >> 8), memoryStream.GetBuffer()[2]);
        }
Ejemplo n.º 3
0
        public override void ExitInterruptDeclaration([NotNull] ZealCpuParser.InterruptDeclarationContext context)
        {
            var rtiInstruction = new CpuInstructionStatement();
            rtiInstruction.Opcode = CpuInstructions.rti;
            rtiInstruction.AddressingMode = CpuAddressingMode.Implied;

            _currentScope.Statements.Add(rtiInstruction);

            _driver.GlobalScope.Add(_currentScope);
            _currentScope = null;
        }
Ejemplo n.º 4
0
        public override void ExitInstructionStatement([NotNull] ZealCpuParser.InstructionStatementContext context)
        {
            if (_currentInstruction.Arguments.Count == 0)
            {
                _currentInstruction.AddressingMode = CpuAddressingMode.Implied;
            }

            var assumeAddressingAttribute = _currentInstruction.Opcode.GetAttribute<CpuAssumeAddressingAttribute>();
            if (assumeAddressingAttribute != null)
            {
                _currentInstruction.AddressingMode = assumeAddressingAttribute.Addressing;
            }

            var labelContext = context.label();
            if (labelContext != null)
            {
                _currentInstruction.AssociatedLabel = labelContext.IDENTIFIER().GetText();
            }

            var opcodeAttributes = EnumHelper.GetAttributes<OpcodeAttribute>(_currentInstruction.Opcode).Where(x => x.AddressingMode == _currentInstruction.AddressingMode).ToArray();
            if (opcodeAttributes == null || (opcodeAttributes != null && opcodeAttributes.Length == 0))
            {
                string descriptionAddressingMode = EnumHelper.GetAttribute<DescriptionAttribute>(_currentInstruction.AddressingMode).Description;
                addErrorMesage(String.Format("opcode '{0}' does not support {1} addressing mode.", _currentInstruction.Opcode, descriptionAddressingMode), context.opcode().Start);
            }

            _currentScope.Statements.Add(_currentInstruction);
            _currentInstruction = null;
        }
Ejemplo n.º 5
0
        public void ShouldGenerateImpliedInstruction(CpuInstructions opcodeEnum, byte finalOpcode)
        {
            CpuInstructionStatement instruction = new CpuInstructionStatement();
            instruction.AddressingMode = CpuAddressingMode.Implied;
            instruction.Opcode = opcodeEnum;

            List<CpuInstructionStatement> instructions = new List<CpuInstructionStatement>();
            instructions.Add(instruction);

            MemoryStream memoryStream = new MemoryStream(8);
            CpuCodeGenerator generator = new CpuCodeGenerator(memoryStream);
            generator.Instructions = instructions;
            generator.Generate();

            Assert.Equal(finalOpcode, memoryStream.GetBuffer()[0]);
        }