项目作者: aksiksi

项目描述 :
A digital circuit simulator.
高级语言: Scala
项目地址: git://github.com/aksiksi/csim.git
创建时间: 2016-09-18T20:26:38Z
项目社区:https://github.com/aksiksi/csim

开源协议:

下载


csim

Build Status

csim is a simple digital circuit simulator I’m working on for a course project. It is written in pure Scala with no dependencies.

Goals

  • Simulation of digital circuits consisting of basic gates (AND, OR, NAND, NOR, INV, BUF, XOR)
  • Working deductive fault simulator that supports all of the gates above
  • Fully operational PODEM implementation (with XOR/XNOR support!)

Build

Requirements

  1. Scala 2.11.x
    • Scala 2.12.x is supported, but JS target will not compile (yet!)
  2. sbt 0.13.x

To build csim, first navigate to the root of the project directory i.e. where build.sbt is located.

You have two options for building csim:

  • To build a JAR for Scala, run sbt csimJVM/package.
  • To build a “fat” JAR for Java (includes Scala stdlib), run sbt csimJVM/assembly.
  • To build an optimized version targeting JavaScript, run sbt csimJS/fullOptJS.

Look under the directory [jvm|js]/target/scala-2.11 for the output JAR or JS file.

Testing

csim has a growing test suite written using ScalaTest. To run the tests, simply type sbt csimJVM/test. All tests should pass (green).

Note: only the JVM target has a test suite, but since the bulk of the logic is shared between the JVM and JS targets, this is not a huge issue.

Usage

Scala (JVM)

Quick example of a simple simulation. Input vector file is s27.in, circuit description file is s27.ckt (both found in jvm/src/test/resources).

  1. import me.assil.csim
  2. import csim.circuit.Bit
  3. import csim.CircuitHelper
  4. import csim.CircuitSimulator
  5. import java.io.File
  6. object Main extends App {
  7. // Read input file for circuit s27
  8. val inputs: Vector[Vector[Bit]] = CircuitHelper.parseInputFile(new File("s27.in"))
  9. // Read in circuit file
  10. val simFile: List[List[String]] = CircuitHelper.readSimFile(new File("s27.ckt"))
  11. // Create a CircuitSimulator object
  12. val sim = new CircuitSimulator(simFile)
  13. // Run the simulator for each input vector
  14. val outputs: Vector[Vector[Bit]] = inputs.map(sim.run)
  15. }

Example of using PODEM to generate a test vector for the fault 13 s-a-1 for circuit s27.

  1. import me.assil.csim
  2. import csim.circuit.Bit
  3. import csim.fault.Fault
  4. import csim.podem.PODEM
  5. import java.io.File
  6. object Main extends App {
  7. // Read the circuit input file
  8. val simFile: List[List[String]] = CircuitHelper.readSimFile(new File("s27.ckt"))
  9. // Create a PODEM object
  10. val podem = new PODEM(lines)
  11. // Create a Fault object
  12. val fault = Fault(13, Bit.High)
  13. // Run PODEM and store test vector
  14. val v: Vector[Bit] = podem.run(fault)
  15. }

JavaScript (ES6)

To use Csim.js from JavaScript, use the examples below to get an idea of the API.

Example of running a simulation given a circuit and an array of input vectors:

  1. // Sample circuit input file, split into lines
  2. const circuit = ["AND 1 2 3", "INPUT 1 2", "OUTPUT 3"];
  3. // Two input vectors
  4. const inputs = [[1,0], [0,1]];
  5. // Init the simulator, and run it
  6. const c = new Csim(circuit);
  7. const outputs = c.run(inputs);
  8. // Log the result - Array[Array[Int]]
  9. console.log(outputs); // => [[0], [0]]

You can also run the PODEM algorithm in JS:

  1. // Sample circuit input file, split into lines
  2. const circuit = ["AND 1 2 3", "INPUT 1 2", "OUTPUT 3"];
  3. // Fault is net 2 s-a-1
  4. const fault = [2, 1];
  5. // Run PODEM
  6. const c = new Csim(circuit);
  7. const vector = c.podem(fault);
  8. console.log(vector);

For more examples, refer to the tests located in jvm/src/test/scala. The tests only cover the JVM target, but they should be sufficient to understand how to use (and extend) the JS API.