static void Main(string[] args) { String inputFileName = "fib.bin"; String outputFileName = "boopout.txt"; byte[] fileBytes = File.ReadAllBytes(inputFileName); StringBuilder sb = new StringBuilder(); int count = 0; for (int i = 0; i < fileBytes.Length; i += 4) { count++; sb.Insert(0, "\", "); sb.Insert(0, AtlasCPU.IntFromBytes(fileBytes[i + 3], fileBytes[i + 2], fileBytes[i + 1], fileBytes[i + 0]).ToString("X8")); sb.Insert(0, "X\""); } while (count++ < 256) { sb.Insert(0, "\", "); sb.Insert(0, "00000000"); sb.Insert(0, "X\""); } File.WriteAllText(outputFileName, sb.ToString()); }
//utility functions for working with literals private int GetLiteralValue(AtlasParser.LiteralContext literal) { int val = 0; var bytes = BigInteger.Parse(literal.INT().GetText()).ToByteArray(); val = AtlasCPU.IntFromBytes(bytes.ElementAtOrDefault(3), bytes.ElementAtOrDefault(2), bytes.ElementAtOrDefault(1), bytes.ElementAtOrDefault(0)); return(val); }
public override void ExitInstruction(AtlasParser.InstructionContext context) { OpCode opCode = OpcodeFromInstruction(context); int argSize = AtlasCPU.ArgSizeFromOpCode(opCode); bool needsArg = argSize != 0; if (m_currentPass == AssemblerSemanticAnalysisPass.RecordLabels) { //space for opcode m_fileSize++; //does this instruction take an argument? if (!needsArg) { return; } //space for arg m_fileSize += argSize; } else if (m_currentPass == AssemblerSemanticAnalysisPass.CodeGen) { EmitOpCode(opCode); //does this instruction take an argument? if (!needsArg) { return; } //if it does, emit that value int argVal = 0; //argument is a label if (context.ID() != null) { //check that the label exists if (!m_labels.ContainsKey(context.ID().Symbol.Text)) { SematicError(context, "Label " + context.ID().GetText() + " is undefined"); } argVal = m_labels[context.ID().Symbol.Text]; } //argument is a literal else if (context.literal() != null) { argVal = GetLiteralValue(context.literal()); } EmitWord(argVal); } }
//utility functions for working with array initilizations private int GetArrayInitilizationElementCount(AtlasParser.ArrayInitilizerContext context) { if (context.OSQUAREBRACE() != null) { var bytes = BigInteger.Parse(context.INT().GetText()).ToByteArray(); return(AtlasCPU.IntFromBytes(bytes.ElementAtOrDefault(3), bytes.ElementAtOrDefault(2), bytes.ElementAtOrDefault(1), bytes.ElementAtOrDefault(0))); } else { return(context.literal().Count); } }
static void Main(string[] args) { if (args[0] == "-a") { AtlasAssembler assembler = new AtlasAssembler(); string compiled = File.ReadAllText(args[1]); byte[] program = assembler.Assemble(new AntlrInputStream(compiled)); if (program != null) { File.WriteAllBytes(args[2], program); } Environment.Exit(0); } else if (args[0] == "-e") { AtlasCPU cpu = new AtlasCPU(); byte[] program = File.ReadAllBytes(args[1]); cpu.LoadProgram(program); Console.Clear(); while (true) { Console.Clear(); Console.WriteLine("pc : " + cpu.ProgramCounter.ToString("X8")); Console.WriteLine("bp : " + cpu.BasePointer.ToString("X8")); Console.WriteLine("sp : " + cpu.StackPointer.ToString("X8")); Console.WriteLine("inst : " + cpu.GetCurrentInstruction().ToString()); Console.WriteLine("inst code : " + cpu.MemValue(MemSize.WORD, cpu.ProgramCounter).ToString("X8")); Console.WriteLine("stack frame:"); foreach (var item in cpu.GetStackFrame()) { Console.WriteLine(item.ToString("X8")); } Console.ReadLine(); cpu.ClockPulse(); } } }
//lax/parse/assemble inputfile, and write flat binary out to outputFile public byte[] Assemble(ICharStream input) { try { //Antlr boilerplate AtlasLexer lexer = new AtlasLexer(input); CommonTokenStream tokens = new CommonTokenStream(lexer); AtlasParser parser = new AtlasParser(tokens); parser.RemoveErrorListeners(); parser.AddErrorListener(this); AtlasParser.RootContext root = parser.root(); ParseTreeWalker walker = new ParseTreeWalker(); //reset assembler state m_currentPass = AssemblerSemanticAnalysisPass.RecordLabels; m_fileSize = 0; m_labels.Clear(); m_wordBuffer.Clear(); //perform all passes while (m_currentPass != AssemblerSemanticAnalysisPass.Done) { //parse the input file //this will trigger listeners for the current pass //TODO fix it so we dont lex/parse on every single pass walker.Walk(this, root); } return(m_wordBuffer.SelectMany(x => AtlasCPU.BytesFromInt(x)).ToArray()); } catch (AssemblerException e) { m_outStream.WriteLine("Assembly Failed: " + e.Message); return(null); } }