diff --git a/Core/Assembler.cpp b/Core/Assembler.cpp index 10cfc623..748171f7 100644 --- a/Core/Assembler.cpp +++ b/Core/Assembler.cpp @@ -8,7 +8,7 @@ #include "DisassemblyInfo.h" #include "LabelManager.h" -static const std::regex instRegex = std::regex("^\\s*([a-zA-Z]{3})[*]{0,1}[\\s]*([#]{0,1})([(]{0,1})[\\s]*([$]{0,1})([^,)(;:]*)[\\s]*((,x\\)|\\),y|,x|,y|\\)){0,1})\\s*(;*)(.*)", std::regex_constants::icase); +static const std::regex instRegex = std::regex("^\\s*([a-zA-Z]{3})[*]{0,1}[\\s]*(#%|#){0,1}([(]{0,1})[\\s]*([$]{0,1})([^,)(;:]*)[\\s]*((,x\\)|\\),y|,x|,y|\\)){0,1})\\s*(;*)(.*)", std::regex_constants::icase); static const std::regex isCommentOrBlank = std::regex("^\\s*([;]+.*$|\\s*$)", std::regex_constants::icase); static const std::regex labelRegex = std::regex("^\\s*([@_a-zA-Z][@_a-zA-Z0-9]*):(.*)", std::regex_constants::icase); static const std::regex byteRegex = std::regex("^\\s*[.]byte\\s+((\\$[a-fA-F0-9]{1,2},)*)(\\$[a-fA-F0-9]{1,2})+\\s*(;*)(.*)$", std::regex_constants::icase); @@ -83,6 +83,8 @@ void Assembler::ProcessLine(string code, uint16_t &instructionAddress, vector &labels, bool firstPass) { + bool isBinary = match.str(2).length() > 1 && match.str(2)[1] == '%'; //Immediate + binary: "#%" + lineData.OpCode = match.str(1); lineData.IsImmediate = !match.str(2).empty(); lineData.IsHex = !match.str(4).empty(); @@ -102,6 +104,8 @@ AssemblerSpecialCodes Assembler::GetLineData(std::smatch match, LineData &lineDa } else if(lineData.IsHex && !((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f'))) { //invalid hex return AssemblerSpecialCodes::InvalidHex; + } else if(isBinary && c != '0' && c != '1') { + return AssemblerSpecialCodes::InvalidBinaryValue; } lineData.Operand.push_back(c); @@ -110,6 +114,23 @@ AssemblerSpecialCodes Assembler::GetLineData(std::smatch match, LineData &lineDa } } + if(isBinary) { + //Convert the binary value to hex + if(lineData.Operand.size() == 0) { + return AssemblerSpecialCodes::MissingOperand; + } else if(lineData.Operand.size() <= 8) { + lineData.IsHex = true; + int value = 0; + for(int i = 0; i < lineData.Operand.size(); i++) { + value <<= 1; + value |= lineData.Operand[i] == '1' ? 1 : 0; + } + lineData.Operand = HexUtilities::ToHex(value, false); + } else { + return AssemblerSpecialCodes::OperandOutOfRange; + } + } + if(!lineData.HasComment && !match.str(9).empty()) { //something is trailing at the end of the line, and it's not a comment return AssemblerSpecialCodes::TrailingText; diff --git a/Core/Assembler.h b/Core/Assembler.h index d78c5be2..5c61c8f1 100644 --- a/Core/Assembler.h +++ b/Core/Assembler.h @@ -34,6 +34,7 @@ enum AssemblerSpecialCodes TrailingText = -9, UnknownLabel = -10, InvalidInstruction = -11, + InvalidBinaryValue = -12, }; class Assembler diff --git a/GUI.NET/Debugger/frmAssembler.cs b/GUI.NET/Debugger/frmAssembler.cs index c367bb3d..7f579f40 100644 --- a/GUI.NET/Debugger/frmAssembler.cs +++ b/GUI.NET/Debugger/frmAssembler.cs @@ -204,6 +204,7 @@ namespace Mesen.GUI.Debugger case AssemblerSpecialCodes.TrailingText: message = "Invalid text trailing at the end of line"; break; case AssemblerSpecialCodes.UnknownLabel: message = "Unknown label"; break; case AssemblerSpecialCodes.InvalidInstruction: message = "Invalid instruction"; break; + case AssemblerSpecialCodes.InvalidBinaryValue: message = "Invalid binary value"; break; } errorList.Add(new ErrorDetail() { Message = message + " - " + codeLines[line-1], LineNumber = line }); line++; @@ -443,6 +444,7 @@ namespace Mesen.GUI.Debugger TrailingText = -9, UnknownLabel = -10, InvalidInstruction = -11, + InvalidBinaryValue = -12 } private void lstErrors_DoubleClick(object sender, EventArgs e)