diff options
| author | Jim Lawson | 2019-03-18 15:20:51 -0700 |
|---|---|---|
| committer | GitHub | 2019-03-18 15:20:51 -0700 |
| commit | 9911b3467de2ebe92827ddc3288bd6db477cc636 (patch) | |
| tree | 4e791d82069c0e94fd5c34cd674a9b9596042d8b /src | |
| parent | a97a81bc0f717f80bb70733795ac5337653b58c5 (diff) | |
Add serialization support for LoadMemoryFileType in LoadMemoryAnnotation (#1056)
* Add serialization support for LoadMemoryFileType in LoadMemoryAnnotation
Add custom LoadMemoryFileTypeSerializer.
Add test to verify LoadMemoryAnnotation can be correctly serialized/deserialized.
* Simplify and focus LoadMemoryAnnotation serialization/deserialization.
Respond to comments on earlier implementations.
* Add type FileType definition for current chisel3 code.
Diffstat (limited to 'src')
3 files changed, 37 insertions, 9 deletions
diff --git a/src/main/scala/firrtl/annotations/JsonProtocol.scala b/src/main/scala/firrtl/annotations/JsonProtocol.scala index acb2d957..ea29f090 100644 --- a/src/main/scala/firrtl/annotations/JsonProtocol.scala +++ b/src/main/scala/firrtl/annotations/JsonProtocol.scala @@ -45,7 +45,10 @@ object JsonProtocol { }}, { case x: Transform => JString(x.getClass.getName) } )) - + class LoadMemoryFileTypeSerializer extends CustomSerializer[MemoryLoadFileType](format => ( + { case JString(s) => MemoryLoadFileType.deserialize(s) }, + { case named: MemoryLoadFileType => JString(named.serialize) } + )) class TargetSerializer extends CustomSerializer[Target](format => ( { case JString(s) => Target.deserialize(s) }, @@ -78,7 +81,8 @@ object JsonProtocol { new TransformClassSerializer + new NamedSerializer + new CircuitNameSerializer + new ModuleNameSerializer + new ComponentNameSerializer + new TargetSerializer + new GenericTargetSerializer + new CircuitTargetSerializer + new ModuleTargetSerializer + - new InstanceTargetSerializer + new ReferenceTargetSerializer + new TransformSerializer + new InstanceTargetSerializer + new ReferenceTargetSerializer + new TransformSerializer + + new LoadMemoryFileTypeSerializer } /** Serialize annotations to a String for emission */ diff --git a/src/main/scala/firrtl/annotations/LoadMemoryAnnotation.scala b/src/main/scala/firrtl/annotations/LoadMemoryAnnotation.scala index c5dae954..c52bf5f6 100644 --- a/src/main/scala/firrtl/annotations/LoadMemoryAnnotation.scala +++ b/src/main/scala/firrtl/annotations/LoadMemoryAnnotation.scala @@ -4,13 +4,25 @@ package firrtl.annotations import java.io.File -/** Enumeration of the two types of `readmem` statements available in Verilog. +import firrtl.FIRRTLException + +/** Representation of the two types of `readmem` statements available in Verilog. */ -object MemoryLoadFileType extends Enumeration { - type FileType = Value +sealed abstract class MemoryLoadFileType(val value: String) { + def serialize: String = value +} - val Hex: Value = Value("h") - val Binary: Value = Value("b") +object MemoryLoadFileType { + // purely for backwards compatibility with chisel3's ChiselLoadMemoryAnnotation + type FileType = MemoryLoadFileType + + case object Hex extends MemoryLoadFileType("h") + case object Binary extends MemoryLoadFileType("b") + def deserialize(s: String): MemoryLoadFileType = s match { + case "h" => MemoryLoadFileType.Hex + case "b" => MemoryLoadFileType.Binary + case _ => throw new FIRRTLException(s"Unrecognized MemoryLoadFileType: $s") + } } /** Firrtl implementation for load memory @@ -21,7 +33,7 @@ object MemoryLoadFileType extends Enumeration { case class LoadMemoryAnnotation( target: ComponentName, fileName: String, - hexOrBinary: MemoryLoadFileType.FileType = MemoryLoadFileType.Hex, + hexOrBinary: MemoryLoadFileType = MemoryLoadFileType.Hex, originalMemoryNameOpt: Option[String] = None ) extends SingleTargetAnnotation[Named] { diff --git a/src/test/scala/firrtlTests/annotationTests/LoadMemoryAnnotationSpec.scala b/src/test/scala/firrtlTests/annotationTests/LoadMemoryAnnotationSpec.scala index 15b12d52..c702df13 100644 --- a/src/test/scala/firrtlTests/annotationTests/LoadMemoryAnnotationSpec.scala +++ b/src/test/scala/firrtlTests/annotationTests/LoadMemoryAnnotationSpec.scala @@ -2,7 +2,7 @@ package firrtlTests.annotationTests -import firrtl.annotations.{CircuitName, ComponentName, LoadMemoryAnnotation, ModuleName} +import firrtl.annotations._ import org.scalatest.{FreeSpec, Matchers} class LoadMemoryAnnotationSpec extends FreeSpec with Matchers { @@ -26,4 +26,16 @@ class LoadMemoryAnnotationSpec extends FreeSpec with Matchers { lma.getFileName should be("./target/scala-2.12/test-classes/init_mem_subdata") } } + "LoadMemoryAnnotation should be correctly parsed from a string" in { + val lma = new LoadMemoryAnnotation( + ComponentName("ram", ModuleName("ModuleMem", CircuitName("CircuitMem"))), + "CircuitMem.ModuleMem.ram.dat", + hexOrBinary = MemoryLoadFileType.Binary, + originalMemoryNameOpt = Some("memory") + ) + + val annoString = JsonProtocol.serializeTry(Seq(lma)).get + val loadedAnnos = JsonProtocol.deserializeTry(annoString).get + lma should equal(loadedAnnos.head) + } } |
