Exemple #1
0
        public void CompileProgram_AddsDefintition_RequiresParsing()
        {
            ushort expectedvalue = 0x7F;
            var    assembler     = new Sharp_LR35902_Assembler.Assembler();

            assembler.CompileProgram(new[] { "#DEFINE X 0x7F" }, null);

            ushort value = 0;

            Assert.IsTrue(assembler.TryParseImmediate("X", ref value));
            Assert.AreEqual(expectedvalue, value);
        }
        public void CompileProgram_AddsDefintition()
        {
            ushort expectedvalue = 0x7F;
            var    assembler     = new Sharp_LR35902_Assembler.Assembler();

            assembler.CompileProgram(new List <string> {
                $"#DEFINE X {expectedvalue}"
            });

            ushort value = 0;

            Assert.IsTrue(assembler.TryParseImmediate("X", ref value));
            Assert.AreEqual(expectedvalue, value);
        }
Exemple #3
0
        public void CompileProgram_OverwriteInstruction_ThrowsWarning()
        {
            var assembler = new Sharp_LR35902_Assembler.Assembler();

            var exceptions = new List <Exception>();

            assembler.CompileProgram(new[]
            {
                ".byte 255",
                ".org 0",
                ".byte 0"
            }, exceptions);

            Assert.AreEqual(1, exceptions.Count);
            Assert.IsInstanceOfType(exceptions[0], typeof(OverwriteException));
        }
Exemple #4
0
        public static void Main(string[] args)
        {
            if (args.Length == 0 || args.Length == 1 && (args[0] == "/?" || args[0] == "-?"))
            {
                Console.WriteLine("Compiles assembly code that uses the Sharp LR35902 instruction-set into a binary.");
                Console.WriteLine();
                Console.WriteLine("Compiler [options] [-in inputfilepath] [-out outputfilepath]");
                Console.WriteLine("Options:");
                Console.WriteLine("-FHS:	Fix currupted HALTs and STOPs by ensuring a following NOP");
                Console.WriteLine("-P n:	Set unset bytes to constant value n");
                Console.WriteLine("-SYM		Export no$gmb format symbol file");
                Console.WriteLine("-SUM		Fix header checksum");
                // TODO: Add any switches here
                return;
            }

            byte   optimizationlevel = 1;
            string inputpath = null, outputpath = null;
            var    fixhaltsandstops  = false;
            ushort padding           = 0;
            var    exportSymbolFile  = false;
            var    fixHeaderChecksum = false;

            for (var i = 0; i < args.Length; i++)
            {
                switch (args[i].ToLower())
                {
                case "-in":
                    inputpath = args[++i];
                    break;

                case "-out":
                    outputpath = args[++i];
                    break;

                case "-o":
                    optimizationlevel = byte.Parse(args[++i]);
                    break;

                case "-fhs":
                    fixhaltsandstops = true;
                    break;

                case "-p":
                    if (!Parser.TryParseImmediate(args[++i], ref padding))
                    {
                        Console.WriteLine("Error: Padding value could not be parsed");
                        return;
                    }
                    break;

                case "-sym":
                    exportSymbolFile = true;
                    break;

                case "-sum":
                    fixHeaderChecksum = true;
                    break;

                default:
                    Console.WriteLine($"Unknown switch '{args[i]}'");
                    return;
                }
            }

            if (inputpath == null)
            {
                Console.WriteLine("Input path not set");
                return;
            }

            if (outputpath == null)
            {
                Console.WriteLine("Output path not set");
                return;
            }


            var instructions = new List <string>(File.ReadAllLines(inputpath));

            var assembler = new Assembler();

            Formatter.Format(instructions);
            if (fixhaltsandstops)
            {
                Formatter.EnsureNOPAfterSTOPOrHALT(instructions);
            }
            //Optimizer.Optimize(instructions, optimizationlevel);
            byte[] bytecode;
            var    exceptions = new List <Exception>();

            try {
                bytecode = assembler.CompileProgram(instructions, exceptions, (byte)padding);
            } catch (Exception e) {
                Console.WriteLine(e.Message);
                Console.WriteLine("Output file has not been written.");
                return;
            }

            if (fixHeaderChecksum)
            {
                byte checksum = 0;
                for (var i = 0x0134; i <= 0x014C; i++)
                {
                    checksum = (byte)(checksum - bytecode[i] - 1);
                }
                bytecode[0x14D] = checksum;
            }

            using (var outputfile = File.Create(outputpath))
                outputfile.Write(bytecode, 0, bytecode.Length);
            if (exportSymbolFile)
            {
                var outputFolder   = Path.GetDirectoryName(outputpath);
                var outputName     = Path.GetFileNameWithoutExtension(outputpath);
                var symbolFilePath = Path.Combine(outputFolder, outputName + ".sym");
                using (var symbolFile = File.CreateText(symbolFilePath))
                    symbolFile.Write(assembler.SymbolTable.CreateSymbolFile());
            }
        }
Exemple #5
0
        public static void Main(string[] args)
        {
            if (args.Length == 0 || args.Length == 1 && (args[0] == "/?" || args[0] == "-?"))
            {
                Console.WriteLine("Compiles assembly code that uses the Sharp LR35902 instruction-set into a binary.");
                Console.WriteLine();
                Console.WriteLine("Compiler [options] [-in inputfilepath] [-out outputfilepath]");
                Console.WriteLine("Options:");
                Console.WriteLine("-FHS:	Fix currupted HALTs and STOPs by ensuring a following NOP");
                // TODO: Add any switches here
                return;
            }

            byte   optimizationlevel = 1;
            string inputpath = null, outputpath = null;
            var    fixhaltsandstops = false;

            for (var i = 0; i < args.Length; i++)
            {
                switch (args[i].ToLower())
                {
                case "-in":
                    inputpath = args[++i];
                    break;

                case "-out":
                    outputpath = args[++i];
                    break;

                case "-o":
                    optimizationlevel = byte.Parse(args[++i]);
                    break;

                case "-fhs":
                    fixhaltsandstops = true;
                    break;

                default:
                    Console.WriteLine($"Unknown switch '{args[i]}'");
                    return;
                }
            }

            if (inputpath == null)
            {
                Console.WriteLine("Input path not set");
                return;
            }

            if (outputpath == null)
            {
                Console.WriteLine("Output path not set");
                return;
            }


            var instructions = new List <string>(File.ReadAllLines(inputpath));

            var assembler = new Assembler();

            Formatter.Format(instructions);
            if (fixhaltsandstops)
            {
                Formatter.EnsureNOPAfterSTOPOrHALT(instructions);
            }
            Optimizer.Optimize(instructions, optimizationlevel);
            byte[] bytecode;
            try {
                bytecode = assembler.CompileProgram(instructions);
            } catch (Exception e) {
                Console.WriteLine(e.Message);
                Console.WriteLine("Output file has not been written.");
                return;
            }

            using (var outputfile = File.Create(outputpath)) {
                outputfile.Write(bytecode, 0, bytecode.Length);
            }
        }