ameetgohil / signalflip-js

verilator testbench w/ Javascript using N-API
MIT License
18 stars 3 forks source link

npm version Build Status CI

signalflip-js

signalflip-js is a verification environment for creating Verilator testbenches using nodejs (Javascript)

Dependencies

Instructions to run

> git clone https://github.com/ameetgohil/create-signalflip-js-tb.git <folder> && rm -rf <folder>/.git
> cd <folder>
> nvm use || nvm install
> npm i

Examples

Counter testbench example
Elastic(valid-ready) testbench example with randomization
APB3 peripheral
Leading zeros counter
Fixed-point Reciprocal

Example sim init

The code below shows how to initialize dut and import useful functions such as RisingEdge, FallingEdge, Sim, and interfaces.

const dut = require('../build/Release/dut.node');
const {Sim, SimUtils, RisingEdge, RisingEdges, FallingEdge, FallingEdges, Interfaces} = require('signalflip-js');
const { Clock, Intf } = SimUtils;
const {Elastic} = Interfaces;
const _ = require('lodash');

const sim = new Sim(dut); 
dut.init();

Create clock

let clk = new Clock(dut.clk, 1);
sim.addClock(clk);

Access and manipulate signals

const rdata = dut.t0_data(); //Reads data on t0_data signal
dut.t0_data(30); //Writes the value 30 to t0_data

Acess and manipulate signals for width > 32 - Use BigInt (see javascript BigInt reference)

const rdata = dut.t0_data(); //Reads t0_data signal as BigInt 
const.t0_data(235023423532523234n); //Sets t0_data signal to the BigInt value (denoted by n at the end of the literal)
const.t0_data(BigInt(235023423532523234)); //Sets t0_data signal to the BigInt value (alternate way to set valule. The result is the same as above)

Example Task

// Task reads t0_data when t0_valid is high on falling edge of the clock
function* read_t0_data() {
    yield* FallingEdge(dut.clk)
    if(dut.t0_valid() == 1)
        console.log('t0_data: ' + dut.t0_data());
}

sim.addTask(read_t0_data()); //initializes task

Example Task waits until t0_data is equal to 64

function* wait_for_64() {
    yield () => {return dut.t0_data() == 64 };
    console.log('t0_data reached the value 64');
}

sim.addTask(wait_for64()); //initializes task

NOTE: function means that it is a generator. yield is used when you want to call another task within a task. yield is used when it you are calling a function that returns a boolean; yield will stop the task until that boolean condition is met.

Run simulation

sim.run(1000); // runs simulation for 1000 clock cycles

Simulation phases

The default simulation phases are:

The simulation phase advances if the tasks executing in the current phase all finish.

When adding tasks to the simulatin using sim.addTask(task), and additional argument can get provided to specify in what simulation phase this task should run. PRE_RUN, POSTRUN, and all tasks with simulation phase prefix of PRE or POST_ should be functions and not generators. See examples below

Show graph

gtkwave logs/vlt_dump.vcd