Пример #1
0
        void FirmwareFromCS()
        {
            var csFiles        = Directory.EnumerateFiles(SourceFolder);
            var csFilesContent = csFiles
                                 .Select(path => new FSTextFile()
            {
                Name = path, Content = File.ReadAllText(path)
            })
                                 .ToList();

            if (csFilesContent.Any())
            {
                foreach (var cs in csFilesContent)
                {
                    Console.WriteLine($"Found CS source: {cs.Name}");
                }

                // translate source files
                var tx     = new CSharp2CPPTranslator();
                var source = new FSSnapshot();
                source.Files.AddRange(csFilesContent);
                tx.Run(source);
                var firmwareSource = tx.Result;

                // create soc resource records
                var socGenerator      = new SOCGenerator();
                var socRecordsBuilder = new SOCRecordsBuilder();
                var socRecords        = socRecordsBuilder.ToSOCRecords(0x800, tx.SOCResources);
                firmwareSource.Add(socGenerator.SOCImport(socRecords));

                FileTools.CreateDirectoryRecursive(FirmwareFolder);
                var m = new FSManager(FirmwareFolder);
                m.SaveSnapshot(firmwareSource);
            }

            ModifyMakefile();

            if (File.Exists(MakefileFile))
            {
                if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    var context = RISCVIntegration
                                  .DefaultContext(FirmwareFolder)
                                  .WithMakeTarget("bin");

                    RISCVIntegrationClient.Make(context).Wait();
                }
                else
                {
                    using (new CurrentDirectory(FirmwareFolder))
                    {
                        Toolchain.Make("bin");
                    }
                }
            }
            else
            {
                Console.WriteLine($"Makefile was not found: {MakefileFile}");
            }
        }
Пример #2
0
        public async Task MakeTest()
        {
            // translate source files
            var tx     = new CSharp2CPPTranslator();
            var source = new FSSnapshot();

            source.Files.Add(LoadSource("SOCBlinker.cs"));
            tx.Run(source);
            var firmwareSource = tx.Result;

            // create soc resource records
            var socGenerator      = new SOCGenerator();
            var socRecordsBuilder = new SOCRecordsBuilder();
            var socRecords        = socRecordsBuilder.ToSOCRecords(0x800, tx.SOCResources);

            firmwareSource.Add(socGenerator.SOCImport(socRecords));
            IntermediateData.SaveToMake(firmwareSource);

            // run makefile
            var context = RISCVIntegration
                          .DefaultContext(TestPath.MakeFolder)
                          .WithMakeTarget("bin");

            await RISCVIntegrationClient.Make(context);
        }
        public bool FirmwareFromAppFolder()
        {
            if (!SourceExists)
            {
                return(false);
            }

            // delete old firmware before making new one
            if (File.Exists(FirmwareFile))
            {
                File.Delete(FirmwareFile);
            }

            var appName        = Path.GetFileName(appPath);
            var sourceFolder   = Path.Combine(appPath, "source");
            var firmwareFolder = Path.Combine(appPath, "firmware");

            var csFiles        = Directory.EnumerateFiles(sourceFolder);
            var csFilesContent = csFiles
                                 .Select(path => new FSTextFile()
            {
                Name = path, Content = File.ReadAllText(path)
            })
                                 .ToList();

            if (csFilesContent.Any())
            {
                foreach (var cs in csFilesContent)
                {
                    Console.WriteLine($"Found CS source: {cs.Name}");
                }

                // translate source files
                var tx     = new CSharp2CPPTranslator();
                var source = new FSSnapshot();
                source.Files.AddRange(csFilesContent);
                tx.Run(source);
                var firmwareSource = tx.Result;

                // create soc resource records
                var socGenerator      = new SOCGenerator();
                var socRecordsBuilder = new SOCRecordsBuilder();
                var socRecords        = socRecordsBuilder.ToSOCRecords(0x800, tx.SOCResources);
                firmwareSource.Add(socGenerator.SOCImport(socRecords));

                FileTools.CreateDirectoryRecursive(firmwareFolder);
                var m = new FSManager(firmwareFolder);
                m.SaveSnapshot(firmwareSource);
            }

            ModifyMakefile();

            if (File.Exists(MakefileFile))
            {
                var context = RISCVIntegration
                              .DefaultContext(firmwareFolder)
                              .WithMakeTarget("bin");

                RISCVIntegrationClient.Make(context).Wait();
            }

            if (File.Exists(FirmwareFile))
            {
                var disassembler = new Disassembler();
                File.WriteAllText(FirmwareAsmFile, disassembler.Disassemble(Instructions()));
            }

            return(File.Exists(FirmwareFile));
        }
Пример #4
0
        async Task TranslateSourceFiles(
            IEnumerable <FSTextFile> files,
            Action entryPoint
            )
        {
            var textReplacer = new TextReplacer();
            var generator    = new IntegrationGenerator();

            var firmwareTemplates = IntegrationTemplatesLoader.FirmwareTemplates;
            var hardwareTemplates = IntegrationTemplatesLoader.HardwareTemplates;

            var tx     = new CSharp2CPPTranslator();
            var source = new FSSnapshot();

            source.Files.AddRange(files);

            tx.Run(source);

            var firmwareSource = tx.Result;

            var socGenerator      = new SOCGenerator();
            var socRecords        = new List <SOCRecord>();
            var socRecordsBuilder = new SOCRecordsBuilder();

            // default code block
            socRecords.Add(new SOCRecord()
            {
                DataType     = typeof(uint),
                SegmentBits  = 12,
                HardwareName = "firmware",
                SoftwareName = "firmware",
                Segment      = 0,
                Depth        = 512,
                Template     = "memory32"
            });

            socRecords.AddRange(socRecordsBuilder.ToSOCRecords(0x800, tx.SOCResources));
            firmwareSource.Add(socGenerator.SOCImport(socRecords));

            var generatedSourceFiles = firmwareSource.Files.Where(f => Path.GetExtension(f.Name).ToLower() == ".cpp").ToList();
            var generatedHeaderFiles = firmwareSource.Files.Where(f => Path.GetExtension(f.Name).ToLower() == ".h").ToList();

            firmwareSource.Merge(firmwareTemplates, f => !f.Name.Contains("template"));

            var firmwareTemplate = firmwareTemplates.Get <FSTextFile>("firmware.template.cpp");
            var firmwareMap      = new Dictionary <string, string>()
            {
                { "FIRMWARE_INCLUDES", string.Join(Environment.NewLine, generatedHeaderFiles.Select(f => $"#include \"{f.Name}\"")) },
                { "FIRMWARE_CODE", $"{entryPoint.Method.DeclaringType.Namespace}::{entryPoint.Method.DeclaringType.Name}::{entryPoint.Method.Name}();" },
            };

            firmwareSource.Add("firmware.cpp", textReplacer.ReplaceToken(firmwareTemplate.Content, firmwareMap));

            var makefileTemplate = firmwareTemplates.Get <FSTextFile>("Makefile.template");
            var makefileMap      = new Dictionary <string, string>()
            {
                { "SOURCES_LIST", string.Join(" ", generatedSourceFiles.Select(f => f.Name)) }
            };

            firmwareSource.Add("Makefile", textReplacer.ReplaceToken(makefileTemplate.Content, makefileMap));

            IntermediateData.SaveFirmwareSource(firmwareSource);

            var firmwareOutput = await CompileFromIntermediate();

            IntermediateData.SaveFirmwareOutput(firmwareOutput);

            // generat verilog

            var hardwareTemplate = hardwareTemplates.Get <FSTextFile>("hardware.template.v").Content;
            var replacers        = new Dictionary <string, string>();

            // memory init file
            replacers["MEM_INIT"] = "";
            foreach (var dma in socRecords)
            {
                var binFile = firmwareOutput.Get <FSBinaryFile>($"{dma.HardwareName}.bin");
                if (binFile != null)
                {
                    var words   = TestTools.ReadWords(binFile.Content).ToList();
                    var memInit = generator.MemInit(words, dma.HardwareName, (int)dma.Depth, SizeOfType(dma.DataType));

                    replacers["MEM_INIT"] += memInit;
                }
                else
                {
                    var memInit = generator.MemInit(Enumerable.Range(0, (int)dma.Depth).Select(idx => 0UL).ToList(), dma.HardwareName, (int)dma.Depth, SizeOfType(dma.DataType));

                    replacers["MEM_INIT"] += memInit;
                }
            }

            // data declarations
            replacers["DATA_DECL"] = generator.DataDeclaration(socRecords);

            // data control signals
            var templates = new IntegrationTemplates();

            foreach (var t in hardwareTemplates.Files.OfType <FSTextFile>())
            {
                templates.Templates[t.Name] = t.Content;
            }

            replacers["DATA_CTRL"] = generator.DataControl(socRecords, templates);
            replacers["MEM_READY"] = generator.MemReady(socRecords);
            replacers["MEM_RDATA"] = generator.MemRData(socRecords);

            hardwareTemplate = textReplacer.ReplaceToken(hardwareTemplate, replacers);

            var hardwareSource = new FSSnapshot();

            hardwareSource.Add("hardware.v", hardwareTemplate);

            IntermediateData.SaveHardwareSource(hardwareSource);
        }