public void TestopCodeLoader() { try { OpCodeLoader.GetSet("SAP1Emu"); OpCodeLoader.GetSet("Malvino"); OpCodeLoader.GetSet("BenEater"); } catch (Exception e) { Assert.Fail(e.ToString()); } }
// ************************************************************************* // Init Engine // ************************************************************************* public void Init(RAMProgram program, IDecoder decoder, string InstructionSetName = DefaultInstructionSetName) { // Get Instruction Set InstructionSet = OpCodeLoader.GetSet(InstructionSetName); _decoder = decoder; // Init RAM if (program == null) { Program = new RAMProgram(new List <string>()); } Program = program; }
// ************************************************************************* // Init Engine // ************************************************************************* public void Init(RAMProgram program, IDecoder decoder, string InstructionSetName = DefaultInstructionSetName) { //string log_name = "runtime_log.txt"; //// Clear Old Log //if (File.Exists(log_name)) //{ // File.Delete(log_name); //} //File.Create(log_name).Close(); // must close the file or the handle will stay open and be locked //// Init Logger //Log.Logger = new LoggerConfiguration() // .WriteTo.File(log_name) // // .WriteTo.Console() // .CreateLogger(); //Serilog.Debugging.SelfLog.Enable(msg => Debug.WriteLine(msg)); //Serilog.Debugging.SelfLog.Enable(Console.Error); //Log.Information("SAP1Emu: Begin Engine Initialization"); // Get Instruction Set //Log.Information($"SAP1Emu: Using Instruction Set: \"{InstructionSetName}\""); InstructionSet = OpCodeLoader.GetSet(InstructionSetName); _decoder = decoder; //_decoder = new InstructionDecoder(); // Init RAM if (program == null) { this.Program = new RAMProgram(new List <string>()); } this.Program = program; //Log.Information("SAP1Emu: Finished Engine Initialization"); }
public static List <string> Parse(List <string> unchecked_assembly, string InstructionSetName = DefaultInstructionSetName) { labels.Clear(); labelDictionary.Clear(); // Get Instruction Set InstructionSet iset = OpCodeLoader.GetSet(InstructionSetName); // ********************************************************************* // Sanitize * // ********************************************************************* // Remove Blank Lines unchecked_assembly.RemoveAll(s => s == null); unchecked_assembly.RemoveAll(s => Regex.IsMatch(s, "^\\s*$")); // Remove Newline Comments unchecked_assembly.RemoveAll(s => s.Trim().First().Equals('#')); for (int i = 0; i < unchecked_assembly.Count; i++) { // Trim Outter Whitespace unchecked_assembly[i] = unchecked_assembly[i].Trim(); // Trim Inner Whitspace unchecked_assembly[i] = Regex.Replace(unchecked_assembly[i], "\\s{2,}", " "); // Remove Inline Comments unchecked_assembly[i] = Regex.Replace(unchecked_assembly[i], "\\s*#.*$", ""); // Update Label addresses if (unchecked_assembly[i].Contains(':')) { labels.Add(new Label { Name = unchecked_assembly[i].Trim().Substring(0, unchecked_assembly[i].IndexOf(':')), LineNumber = i }); } } // ********************************************************************* // Validate // ********************************************************************* // If is not valid, will throw execptions for CLI to catch and display to user _ = IsValid(unchecked_assembly, iset); // Get rid of the labels to make it easier for conversion into binary // They are no longer needed anyway since we store the number they become // based on line numbers. //foreach (Label label in labels) //{ // unchecked_assembly[label.Address - 1] = Regex.Replace(unchecked_assembly[label.Address - 1], "\\s*\\w{1,6}:", "").Trim(); //} // ********************************************************************* // Assemble // ********************************************************************* List <string> binary = new List <string>(); int lines_of_asm = unchecked_assembly.Count; int current_line_number = 0; int index; foreach (string line in unchecked_assembly) { if (line == "...") { int nop_count = (0xFFFF - 0x0800) - lines_of_asm + 1; for (int i = 0; i < nop_count; i++) { binary.Add("00000000"); } } else { StringBuilder instructionString = new StringBuilder(); index = 0; bool instructionComplete = false; if (line.Contains(':')) { labelDictionary.Add(line[0..line.IndexOf(':')], binary.Count);
public static List <string> Parse(List <string> unchecked_assembly, string InstructionSetName = DefaultInstructionSetName) { // Get Instruction Set InstructionSet iset = OpCodeLoader.GetSet(InstructionSetName); // ********************************************************************* // Sanitize * // ********************************************************************* // Remove Blank Lines unchecked_assembly.RemoveAll(s => s == null); unchecked_assembly.RemoveAll(s => Regex.IsMatch(s, "^\\s*$")); // Remove Newline Comments unchecked_assembly.RemoveAll(s => s[0] == '#'); for (int i = 0; i < unchecked_assembly.Count; i++) { // ******************************* // Trim Whitespace * // ******************************* // Outter Whitespace unchecked_assembly[i] = unchecked_assembly[i].Trim(); // Inner Whitspace unchecked_assembly[i] = Regex.Replace(unchecked_assembly[i], "\\s{2,}", " "); // ******************************* // ******************************* // Remove Inline Comments // ******************************* unchecked_assembly[i] = Regex.Replace(unchecked_assembly[i], "\\s*#.*$", ""); // ******************************* } // ********************************************************************* // ********************************************************************* // Validate // ********************************************************************* //if is not valid, will throw execptions for CLI to catch and display to user _ = IsValid(unchecked_assembly, iset); // ********************************************************************* // ********************************************************************* // Assemble // ********************************************************************* List <string> binary = new List <string>(); int lines_of_asm = unchecked_assembly.Count; int current_line_number = 0; foreach (string line in unchecked_assembly) { if (line == "...") { int nop_count = 16 - lines_of_asm + 1; for (int i = 0; i < nop_count; i++) { binary.Add("00000000"); } } else { string upper_nibble_asm = line.Split(" ", 2)[0]; string lower_nibble_asm = line.Split(" ", 2)[1]; string upper_nibble_bin = ""; string lower_nibble_bin = ""; // Convert Upper Nibble if (InstructionValidator.IsValidInstruction(upper_nibble_asm, iset)) // Is instruction { upper_nibble_bin = InstructionValidator.GetUpperNibble(line.Substring(0, 3), iset); } else if (Regex.IsMatch(upper_nibble_asm, "^0[xX][0-9a-fA-F]$")) // Is Data { int value_upper = (int)(Convert.ToUInt32(upper_nibble_asm, 16)); upper_nibble_bin = BinConverter.IntToBin4(value_upper); } // Convert Lower Nibble int value_lower = (int)(Convert.ToUInt32(lower_nibble_asm, 16)); lower_nibble_bin = BinConverter.IntToBin4(value_lower); binary.Add(upper_nibble_bin + lower_nibble_bin); } current_line_number++; } // ********************************************************************* //If a program was executed, but didnt fill in every line of RAM then throw an exception. Must have 16 elements! if (binary.Count != 16) { throw new ParseException($"SAP1ASM: Program must have 16 lines.", new ParseException("Use \"NOP 0x0\" for a no-operation command or the \"...\" macro to fill in the rest with NOP 0x0.")); } return(binary); }
private InstructionSet GetInstructionSet() { string InstructionSetName = "SAP1Emu"; return(OpCodeLoader.GetSet(InstructionSetName)); }