CoreDefinition.scala 3.21 KB
Newer Older
1
package chisel.packaging
Jens Korinth's avatar
Jens Korinth committed
2
3
4
5
import  scala.io.Source
import  play.api.libs.json._
import  play.api.libs.functional.syntax._
import  chisel3.Module
Jens Korinth's avatar
Jens Korinth committed
6

7
8
9
/** Abstraction for known bus interfaces / pin groups. */
final case class Interface(name: String, kind: String)

Jens Korinth's avatar
Jens Korinth committed
10
11
12
/**
 * Basic definition of a core for IP-XACT packaging.
 **/
13
class CoreDefinition(val name: String, val vendor: String, val library: String, val version: String,
14
15
                     val root: String, val postBuildActions: Seq[Option[Any] => Unit] = Seq(),
                     val interfaces: Seq[Interface] = Seq()) {
Jens Korinth's avatar
Jens Korinth committed
16
17
18
19
20
21
22
23
24
25
26
27
28
29
  import CoreDefinition._
  def write(filename: String) : Boolean = try {
    val fw = new java.io.FileWriter(filename)
    fw.append(Json.toJson(this).toString)
    fw.flush()
    fw.close()
    true
  } catch { case ex: Exception => println("ERROR: " + ex); false }
}

/**
 * Contains methods for reading a core definition from Json.
 **/
object CoreDefinition {
30
31
32
  def apply(name: String, vendor: String, library: String, version: String, root: String,
            interfaces: Seq[Interface] = Seq()): CoreDefinition =
    new CoreDefinition(name, vendor, library, version, root, interfaces = interfaces)
33
34

  def withActions(name: String, vendor: String, library: String, version: String, root: String,
35
                  postBuildActions: Seq[Option[Any] => Unit], interfaces: Seq[Interface] = Seq()): CoreDefinition =
36
37
    new CoreDefinition(name, vendor, library, version, root, postBuildActions)

38
39
  def unapply(cd: CoreDefinition): Option[Tuple6[String, String, String, String, String, Seq[Interface]]] =
    Some((cd.name, cd.vendor, cd.library, cd.version, cd.root, cd.interfaces))
40

Jens Korinth's avatar
Jens Korinth committed
41
42
  /** Provide automatic IP directory for given name. **/
  def root(name: String): String =
Jens Korinth's avatar
Jens Korinth committed
43
44
      java.nio.file.Paths.get(".").toAbsolutePath.resolveSibling("ip").resolve(name).toString

45
46
47
48
49
50
  implicit val interfaceFormat: Format[Interface] = (
    (JsPath \ "name").format[String] ~
    (JsPath \ "kind").format[String]
  ) (Interface.apply _, unlift(Interface.unapply _))

  implicit val coreDefinitionWrites: Writes[CoreDefinition] = (
Jens Korinth's avatar
Jens Korinth committed
51
52
53
54
      (JsPath \ "name").write[String] ~
      (JsPath \ "vendor").write[String] ~
      (JsPath \ "library").write[String] ~
      (JsPath \ "version").write[String] ~
55
56
57
      (JsPath \ "root").write[String] ~
      (JsPath \ "interfaces").write[Seq[Interface]]
    )(unlift(CoreDefinition.unapply _))
Jens Korinth's avatar
Jens Korinth committed
58

59
  implicit val coreDefinitionReads: Reads[CoreDefinition] = (
Jens Korinth's avatar
Jens Korinth committed
60
61
62
63
      (JsPath \ "name").read[String] ~
      (JsPath \ "vendor").read[String] ~
      (JsPath \ "library").read[String] ~
      (JsPath \ "version").read[String] ~
64
65
66
      (JsPath \ "root").read[String] ~
      (JsPath \ "interfaces").readNullable[Seq[Interface]].map(_ getOrElse Seq[Interface]())
    )(apply _)
Jens Korinth's avatar
Jens Korinth committed
67

Jens Korinth's avatar
Jens Korinth committed
68
69
70
71
  /**
   * Read CoreDefinition from file containing Json format.
   * @param filename Name (and path) of file.
   **/
Jens Korinth's avatar
Jens Korinth committed
72
73
74
75
76
77
78
79
80
  def read(filename: String) : Option[CoreDefinition] = try {
    val contents = Source.fromFile(filename).getLines.mkString("\n")
    val json = Json.parse(contents)
    json.validate[CoreDefinition] match {
      case s: JsSuccess[CoreDefinition] => Some(s.get)
      case e: JsError => { println("ERROR: " + e); None }
    }
  } catch { case ex: Exception => println("ERROR: " + ex); None }
}