generated from chipsalliance/chisel-template
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from shahzaibk23/main
Modified Peripherals Object to Add Peripheral Generically via JSON
- Loading branch information
Showing
15 changed files
with
91 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"peripherals": [ | ||
{ | ||
"name": "GPIO", | ||
"value": 0 | ||
}, | ||
{ | ||
"name": "DCCM", | ||
"value": 1 | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,42 @@ | ||
package caravan.bus.common | ||
|
||
import play.api.libs.json._ | ||
import java.io.File | ||
import java.nio.file.{Files, Paths} | ||
import scala.io.Source | ||
import chisel3._ | ||
import chisel3.experimental.ChiselEnum | ||
import chisel3.util._ | ||
|
||
/* | ||
* USAGE: | ||
- first do `Peripherls.addValuesFromJson(/path/to/json/file)` | ||
- then simply get the value of each peripheral by `Peripheral.get("peripheralName")` | ||
- JSON format is in 'caravan/peripherals.json' | ||
*/ | ||
|
||
object Peripherals { | ||
|
||
private var names: Map[String, UInt] = Map() | ||
|
||
// Read JSON file | ||
def readJsonFile(jsonFilename: String): JsValue = { | ||
val jsonString: String = Source.fromFile(jsonFilename).getLines.mkString | ||
Json.parse(jsonString) | ||
} | ||
|
||
// Parse JSON and add values to Object | ||
def addValuesFromJson(jsonFilename: String): Unit = { | ||
val json: JsValue = readJsonFile(jsonFilename) | ||
val peripherals: List[JsValue] = (json \ "peripherals").as[List[JsValue]] | ||
|
||
peripherals.foreach { peripheral => | ||
val name: String = (peripheral \ "name").as[String] | ||
val value: Int = (peripheral \ "value").as[Int] | ||
names += (name -> value.U) | ||
} | ||
} | ||
|
||
object Peripherals extends ChiselEnum { | ||
val GPIO = Value(0.U) | ||
val DCCM = Value(1.U) | ||
} | ||
// Get the value of a peripheral by its name | ||
def get(value: String): UInt = names.get(value).getOrElse(0.U) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters