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; }
public void ShouldGenerateImmediateInstruction(CpuInstructions opcodeEnum, byte finalOpcode, int value) { CpuInstructionStatement instruction = new CpuInstructionStatement(); instruction.AddressingMode = CpuAddressingMode.Immediate; instruction.Opcode = opcodeEnum; if (value > byte.MaxValue) { instruction.Arguments.Add(new NumberInstructionArgument(value, ArgumentSize.Word)); } else { instruction.Arguments.Add(new NumberInstructionArgument(value, ArgumentSize.Byte)); } 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]); }
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; }
public void ShouldParseImpliedInstructions(string opcodeText, CpuInstructions opcodeEnum) { string input = String.Format(ProcedureTemplate, opcodeText); ZealCpuDriver driver = new ZealCpuDriver(input.ToMemoryStream()); driver.Parse(); CpuInstructionStatement instructionStatement = driver.GlobalScope.Children[0].Statements[0] as CpuInstructionStatement; Assert.Equal(opcodeEnum, instructionStatement.Opcode); Assert.Equal(CpuAddressingMode.Implied, instructionStatement.AddressingMode); }
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; } }
public void ShouldParseArgumentSizeCorrectly(string instruction, int value, ArgumentSize size) { string input = String.Format(ProcedureTemplate, instruction); ZealCpuDriver driver = new ZealCpuDriver(input.ToMemoryStream()); driver.Parse(); CpuInstructionStatement instructionStatement = driver.GlobalScope.Children[0].Statements[0] as CpuInstructionStatement; var argument = instructionStatement.Arguments[0] as NumberInstructionArgument; Assert.Equal(value, argument.Number); Assert.Equal(size, argument.Size); }
public void ShouldParseAbsoluteInstructions(string instruction, CpuInstructions opcodeEnum, int value) { string input = String.Format(ProcedureTemplate, instruction); ZealCpuDriver driver = new ZealCpuDriver(input.ToMemoryStream()); driver.Parse(); CpuInstructionStatement instructionStatement = driver.GlobalScope.Children[0].Statements[0] as CpuInstructionStatement; Assert.Equal(opcodeEnum, instructionStatement.Opcode); Assert.Equal(CpuAddressingMode.Absolute, instructionStatement.AddressingMode); var numberArgument = instructionStatement.Arguments[0] as NumberInstructionArgument; Assert.Equal(value, numberArgument.Number); }
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]); }