123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657 |
- using NIFPGA.lvbitx;
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Threading.Tasks.Dataflow;
- using FxpConvert.Common;
- namespace NIFPGA
- {
- public sealed class FPGA
- {
- public string BitstreamMD5 { get; private set; } = string.Empty;
- public string BitfileVersion { get; private set; } = string.Empty;
- public string SignatureRegister { get; private set; } = string.Empty;
- public string BitstreamVersion { get; private set; } = string.Empty;
- public string SignatureNames { get; private set; } = string.Empty;
- private List<Fifo> fifos= new List<Fifo>();
- private IFxpConvert _FXPConvert;
- private List<FPGABaseProperty> properties= new List<FPGABaseProperty>();
- public IReadOnlyDictionary<String,Fifo> Fifos => fifos.ToDictionary(x=>x.Name);
- public IReadOnlyDictionary<String,FPGABaseProperty> Properties=> properties.ToDictionary(x=>x.Name);
- private FPGASession session;
- public Boolean Error=> session.Error;
- public NiFpga_Status LastStatus=> session.LastStatus;
- public string Message=>session.Message;
- public FPGA(IFxpConvert convert)
- {
- _FXPConvert = convert;
- session = new FPGASession();
- Irq = new Irq(session);
- }
- public T CreateProperty<T>(string name) where T :FPGABaseProperty
- {
- var pro = properties.FirstOrDefault(x => x.Name == name);
- if (pro == null) throw new Exception($"Property {name} not found");
- return (T)pro;
- }
- public T CreateFifo<T>(string name) where T:Fifo
- {
- var fifo = fifos.FirstOrDefault(x => x.Name == name);
- if (fifo == null) throw new Exception($"Fifo {name} not found");
- return (T)fifo;
- }
- public void Parse(string path)
- {
- var val = lvbitx.Parse.ParseFile(path);
- InitProperies(val.VI.Registers);
- InitFifo(val.Project.DMA);
- }
-
- private void InitFifo(List<DMA> dma)
- {
- if (dma == null || dma.Count == 0) return;
- dma.ForEach(x =>
- {
- switch(x.Direction)
- {
- case "TargetToHost"://read
- switch (x.Datatype)
- {
- case Datatype.Boolean:
- fifos.Add(new ReadFifo<bool>(session, x.ControlSet)
- {
- Name = x.Name,
- });
- break;
- case Datatype.Int8:
- fifos.Add(new ReadFifo<sbyte>(session, x.ControlSet)
- {
- Name = x.Name,
- });
- break;
- case Datatype.Uint8:
- fifos.Add(new ReadFifo<byte>(session, x.ControlSet) { Name = x.Name });
- break;
- case Datatype.Int16:
- fifos.Add(new ReadFifo<short>(session, x.ControlSet) { Name = x.Name });
- break;
- case Datatype.Uint16:
- fifos.Add(new ReadFifo<ushort>(session, x.ControlSet) { Name = x.Name });
- break;
- case Datatype.Int32:
- fifos.Add(new ReadFifo<int>(session, x.ControlSet) { Name = x.Name });
- break;
- case Datatype.Uint32:
- fifos.Add(new ReadFifo<uint>(session, x.ControlSet) { Name = x.Name });
- break;
- case Datatype.Int64:
- fifos.Add(new ReadFifo<long>(session, x.ControlSet) { Name = x.Name });
- break;
- case Datatype.Uint64:
- fifos.Add(new ReadFifo<ulong>(session, x.ControlSet) { Name = x.Name });
- break;
- case Datatype.Float:
- fifos.Add(new ReadFifo<float>(session, x.ControlSet) { Name = x.Name });
- break;
- case Datatype.Double:
- fifos.Add(new ReadFifo<double>(session, x.ControlSet) { Name = x.Name });
- break;
- case Datatype.FXP:
- fifos.Add(new ReadFXPFifo(session, x.ControlSet,x.FxpTypeInfo, _FXPConvert)
- {
- Name = x.Name,
- });
- break;
- case Datatype.Array:
- break;
- }
- break;
- case "HostToTarget"://write
- switch (x.Datatype)
- {
- case Datatype.Boolean:
- fifos.Add(new WriteFifo<bool>(session, x.ControlSet) { Name = x.Name });
- break;
- case Datatype.Int8:
- fifos.Add(new WriteFifo<sbyte>(session, x.ControlSet) { Name = x.Name });
- break;
- case Datatype.Uint8:
- fifos.Add(new WriteFifo<byte>(session, x.ControlSet) { Name = x.Name });
- break;
- case Datatype.Int16:
- fifos.Add(new WriteFifo<short>(session, x.ControlSet) { Name = x.Name });
- break;
- case Datatype.Uint16:
- fifos.Add(new WriteFifo<ushort>(session, x.ControlSet) { Name = x.Name });
- break;
- case Datatype.Int32:
- fifos.Add(new WriteFifo<int>(session, x.ControlSet) { Name = x.Name });
- break;
- case Datatype.Uint32:
- fifos.Add(new WriteFifo<uint>(session, x.ControlSet) { Name = x.Name });
- break;
- case Datatype.Int64:
- fifos.Add(new WriteFifo<long>(session, x.ControlSet) { Name = x.Name });
- break;
- case Datatype.Uint64:
- fifos.Add(new WriteFifo<ulong>(session, x.ControlSet) { Name = x.Name });
- break;
- case Datatype.Float:
- fifos.Add(new WriteFifo<float>(session, x.ControlSet) { Name = x.Name });
- break;
- case Datatype.Double:
- fifos.Add(new WriteFifo<double>(session, x.ControlSet) { Name = x.Name });
- break;
- case Datatype.FXP:
- fifos.Add(new WriteFXPFifo(session, x.ControlSet, x.FxpTypeInfo, _FXPConvert) { Name = x.Name });
- break;
- case Datatype.Array:
- break;
- }
- break;
- }
- });
- }
- public Irq Irq { get; }
- public void Open(string bitfile, string resource, NiFpga_OpenAttribute attribute)
- {
- var lv = lvbitx.Parse.ParseFile(bitfile);
- BitfileVersion = lv.BitfileVersion;
- SignatureNames = lv.SignatureNames;
- SignatureRegister= lv.SignatureRegister;
- BitstreamMD5 = lv.BitstreamMD5;
- BitstreamVersion = lv.BitstreamVersion;
- InitProperies(lv.VI.Registers);
- InitFifo(lv.Project.DMA);
- session.Open(bitfile, lv.SignatureRegister, resource, attribute);
- var indicator = properties.Select(x => x.Indicator).ToArray();
- }
- private void InitProperies(List<lvbitx.Register> registers)
- {
- if (registers == null || registers.Count == 0) return;
- foreach (var reg in registers)
- {
- switch (reg.Datatype)
- {
- case lvbitx.Datatype.Int16:
- if (!reg.Indicator)
- {
- properties.Add(new FPGAWriteProperty<short>(session, reg.Offset)
- {
- Name = reg.Name,
- SizeInBits = reg.SizeInBits,
- });
- }
- else
- {
- properties.Add(new FPGAReadProperty<short>(session, reg.Offset)
- {
- Name = reg.Name,
- SizeInBits = reg.SizeInBits,
- });
- }
- break;
- case lvbitx.Datatype.Boolean:
- if (!reg.Indicator)
- {
- properties.Add(new FPGAWriteProperty<bool>(session, reg.Offset)
- {
- Name = reg.Name,
- SizeInBits = reg.SizeInBits,
- });
- }
- else
- {
- properties.Add(new FPGAReadProperty<bool>(session, reg.Offset)
- {
- Name = reg.Name,
- SizeInBits = reg.SizeInBits,
- });
- }
- break;
- case lvbitx.Datatype.Int8:
- if (!reg.Indicator)
- {
- properties.Add(new FPGAWriteProperty<sbyte>(session, reg.Offset)
- {
- Name = reg.Name,
- SizeInBits = reg.SizeInBits,
- });
- }
- else
- {
- properties.Add(new FPGAReadProperty<sbyte>(session, reg.Offset)
- {
- Name = reg.Name,
- SizeInBits = reg.SizeInBits,
- });
- }
- break;
- case lvbitx.Datatype.Uint8:
- if (!reg.Indicator)
- {
- properties.Add(new FPGAWriteProperty<byte>(session, reg.Offset)
- {
- Name = reg.Name,
- SizeInBits = reg.SizeInBits,
- });
- }
- else
- {
- properties.Add(new FPGAReadProperty<byte>(session, reg.Offset)
- {
- Name = reg.Name,
- SizeInBits = reg.SizeInBits,
- });
- }
- break;
- case lvbitx.Datatype.Uint16:
- if (!reg.Indicator)
- {
- properties.Add(new FPGAWriteProperty<ushort>(session, reg.Offset)
- {
- Name = reg.Name,
- SizeInBits = reg.SizeInBits,
- });
- }
- else
- {
- properties.Add(new FPGAReadProperty<ushort>(session, reg.Offset)
- {
- Name = reg.Name,
- SizeInBits = reg.SizeInBits,
- });
- }
- break;
- case lvbitx.Datatype.Int32:
- if (!reg.Indicator)
- {
- properties.Add(new FPGAWriteProperty<int>(session, reg.Offset)
- {
- Name = reg.Name,
- SizeInBits = reg.SizeInBits,
- });
- }
- else
- {
- properties.Add(new FPGAReadProperty<int>(session, reg.Offset)
- {
- Name = reg.Name,
- SizeInBits = reg.SizeInBits,
- });
- }
- break;
- case lvbitx.Datatype.Uint32:
- if (!reg.Indicator)
- {
- properties.Add(new FPGAWriteProperty<uint>(session, reg.Offset)
- {
- Name = reg.Name,
- SizeInBits = reg.SizeInBits,
- });
- }
- else
- {
- properties.Add(new FPGAReadProperty<uint>(session, reg.Offset)
- {
- Name = reg.Name,
- SizeInBits = reg.SizeInBits,
- });
- }
- break;
- case lvbitx.Datatype.Int64:
- if (!reg.Indicator)
- {
- properties.Add(new FPGAWriteProperty<long>(session, reg.Offset)
- {
- Name = reg.Name,
- SizeInBits = reg.SizeInBits,
- });
- }
- else
- {
- properties.Add(new FPGAReadProperty<long>(session, reg.Offset)
- {
- Name = reg.Name,
- SizeInBits = reg.SizeInBits,
- });
- }
- break;
- case lvbitx.Datatype.Uint64:
- if (!reg.Indicator)
- {
- properties.Add(new FPGAWriteProperty<ulong>(session, reg.Offset)
- {
- Name = reg.Name,
- SizeInBits = reg.SizeInBits,
- });
- }
- else
- {
- properties.Add(new FPGAReadProperty<ulong>(session, reg.Offset)
- {
- Name = reg.Name,
- SizeInBits = reg.SizeInBits,
- });
- }
- break;
- case lvbitx.Datatype.Float:
- if (!reg.Indicator)
- {
- properties.Add(new FPGAWriteProperty<float>(session, reg.Offset)
- {
- Name = reg.Name,
- SizeInBits = reg.SizeInBits,
- });
- }
- else
- {
- properties.Add(new FPGAReadProperty<float>(session, reg.Offset)
- {
- Name = reg.Name,
- SizeInBits = reg.SizeInBits,
- });
- }
- break;
- case lvbitx.Datatype.Double:
- if (!reg.Indicator)
- {
- properties.Add(new FPGAWriteProperty<double>(session, reg.Offset)
- {
- Name = reg.Name,
- SizeInBits = reg.SizeInBits,
- });
- }
- else
- {
- properties.Add(new FPGAReadProperty<double>(session, reg.Offset)
- {
- Name = reg.Name,
- SizeInBits = reg.SizeInBits,
- });
- }
- break;
- case lvbitx.Datatype.FXP:
- if (!reg.Indicator)
- {
- properties.Add(new FPGAFXPWriteProperty(session, reg.Offset, reg.FxpTypeInfo, _FXPConvert)
- {
- Name = reg.Name,
- SizeInBits = reg.SizeInBits,
- });
- }
- else
- {
- properties.Add(new FPGAFXPReadProperty(session, reg.Offset, reg.FxpTypeInfo, _FXPConvert)
- {
- Name = reg.Name,
- SizeInBits = reg.SizeInBits,
- });
- }
- break;
- case lvbitx.Datatype.Array:
- switch (reg.ArrayValueType)
- {
- case lvbitx.Datatype.Boolean:
- if (!reg.Indicator)
- {
- properties.Add(new FPGAArrayWriteProperty<bool>(session, reg.Offset, reg.Size)
- {
- Name = reg.Name,
- SizeInBits = reg.SizeInBits,
- });
- }
- else
- {
- properties.Add(new FPGAArrayReadProperty<bool>(session, reg.Offset, reg.Size)
- {
- Name = reg.Name,
- SizeInBits = reg.SizeInBits,
- });
- }
- break;
- case lvbitx.Datatype.Int8:
- if (!reg.Indicator)
- {
- properties.Add(new FPGAArrayWriteProperty<sbyte>(session, reg.Offset, reg.Size)
- {
- Name = reg.Name,
- SizeInBits = reg.SizeInBits,
- });
- }
- else
- {
- properties.Add(new FPGAArrayReadProperty<sbyte>(session, reg.Offset, reg.Size)
- {
- Name = reg.Name,
- SizeInBits = reg.SizeInBits,
- });
- }
- break;
- case lvbitx.Datatype.Uint8:
- if (!reg.Indicator)
- {
- properties.Add(new FPGAArrayWriteProperty<byte>(session, reg.Offset, reg.Size)
- {
- Name = reg.Name,
- SizeInBits = reg.SizeInBits,
- });
- }
- else
- {
- properties.Add(new FPGAArrayReadProperty<byte>(session, reg.Offset, reg.Size)
- {
- Name = reg.Name,
- SizeInBits = reg.SizeInBits,
- });
- }
- break;
- case lvbitx.Datatype.Int16:
- if (!reg.Indicator)
- {
- properties.Add(new FPGAArrayWriteProperty<short>(session, reg.Offset, reg.Size)
- {
- Name = reg.Name,
- SizeInBits = reg.SizeInBits,
- });
- }
- else
- {
- properties.Add(new FPGAArrayReadProperty<short>(session, reg.Offset, reg.Size)
- {
- Name = reg.Name,
- SizeInBits = reg.SizeInBits,
- });
- }
- break;
- case lvbitx.Datatype.Uint16:
- if (!reg.Indicator)
- {
- properties.Add(new FPGAArrayWriteProperty<ushort>(session, reg.Offset, reg.Size)
- {
- Name = reg.Name,
- SizeInBits = reg.SizeInBits,
- });
- }
- else
- {
- properties.Add(new FPGAArrayReadProperty<ushort>(session, reg.Offset, reg.Size)
- {
- Name = reg.Name,
- SizeInBits = reg.SizeInBits,
- });
- }
- break;
- case lvbitx.Datatype.Int32:
- if (!reg.Indicator)
- {
- properties.Add(new FPGAArrayWriteProperty<int>(session, reg.Offset, reg.Size)
- {
- Name = reg.Name,
- SizeInBits = reg.SizeInBits,
- });
- }
- else
- {
- properties.Add(new FPGAArrayReadProperty<int>(session, reg.Offset, reg.Size)
- {
- Name = reg.Name,
- SizeInBits = reg.SizeInBits,
- });
- }
- break;
- case lvbitx.Datatype.Uint32:
- if (!reg.Indicator)
- {
- properties.Add(new FPGAArrayWriteProperty<uint>(session, reg.Offset, reg.Size)
- {
- Name = reg.Name,
- SizeInBits = reg.SizeInBits,
- });
- }
- else
- {
- properties.Add(new FPGAArrayReadProperty<uint>(session, reg.Offset, reg.Size)
- {
- Name = reg.Name,
- SizeInBits = reg.SizeInBits,
- });
- }
- break;
- case lvbitx.Datatype.Int64:
- if (!reg.Indicator)
- {
- properties.Add(new FPGAArrayWriteProperty<long>(session, reg.Offset, reg.Size)
- {
- Name = reg.Name,
- SizeInBits = reg.SizeInBits,
- });
- }
- else
- {
- properties.Add(new FPGAArrayReadProperty<long>(session, reg.Offset, reg.Size)
- {
- Name = reg.Name,
- SizeInBits = reg.SizeInBits,
- });
- }
- break;
- case lvbitx.Datatype.Uint64:
- if (!reg.Indicator)
- {
- properties.Add(new FPGAArrayWriteProperty<ulong>(session, reg.Offset, reg.Size)
- {
- Name = reg.Name,
- SizeInBits = reg.SizeInBits,
- });
- }
- else
- {
- properties.Add(new FPGAArrayReadProperty<ulong>(session, reg.Offset, reg.Size)
- {
- Name = reg.Name,
- SizeInBits = reg.SizeInBits,
- });
- }
- break;
- case lvbitx.Datatype.Float:
- if (!reg.Indicator)
- {
- properties.Add(new FPGAArrayWriteProperty<float>(session, reg.Offset, reg.Size)
- {
- Name = reg.Name,
- SizeInBits = reg.SizeInBits,
- });
- }
- else
- {
- properties.Add(new FPGAArrayReadProperty<float>(session, reg.Offset, reg.Size)
- {
- Name = reg.Name,
- SizeInBits = reg.SizeInBits,
- });
- }
- break;
- case lvbitx.Datatype.Double:
- if (!reg.Indicator)
- {
- properties.Add(new FPGAArrayWriteProperty<double>(session, reg.Offset, reg.Size)
- {
- Name = reg.Name,
- SizeInBits = reg.SizeInBits,
- });
- }
- else
- {
- properties.Add(new FPGAArrayReadProperty<double>(session, reg.Offset, reg.Size)
- {
- Name = reg.Name,
- SizeInBits = reg.SizeInBits,
- });
- }
- break;
- case lvbitx.Datatype.FXP:
- if (!reg.Indicator)
- {
- properties.Add(new FPGAArrayFXPWriteProperty(session, reg.Offset, reg.Size, reg.FxpTypeInfo, _FXPConvert)
- {
- Name = reg.Name,
- SizeInBits = reg.SizeInBits,
- });
- }
- else
- {
- properties.Add(new FPGAArrayFXPReadProperty(session, reg.Offset, reg.Size, reg.FxpTypeInfo, _FXPConvert)
- {
- Name = reg.Name,
- SizeInBits = reg.SizeInBits,
- });
- }
- break;
- case lvbitx.Datatype.Array:
- break;
- }
- break;
- }
- }
- }
- public void Close()=>session.Close();
- public void Run()
- {
- session.CheckResult(session.GetDelegate<Interop.NiFpgaDll_Run>()(session.Session, Interop.NiFpga_RunAttribute.NiFpga_RunAttribute_None));
- }
- public void Abort()
- {
- session.CheckResult(session.GetDelegate<Interop.NiFpgaDll_Abort>()(session.Session));
- }
- public void Reset()
- {
- session.CheckResult(session.GetDelegate<Interop.NiFpgaDll_Reset>()(session.Session));
- }
- public void Download()
- {
- session.CheckResult(session.GetDelegate<Interop.NiFpgaDll_Download>()(session.Session));
- }
- public void FpgaFinalize()
- {
- session.CheckResult(session.GetDelegate<Interop.NiFpgaDll_Finalize>()());
- }
- }
- }
|