Skip to content

Instantly share code, notes, and snippets.

@djg
Created March 15, 2018 06:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save djg/98f857fa0d34e1a95f8ecaf57ed5254b to your computer and use it in GitHub Desktop.
Save djg/98f857fa0d34e1a95f8ecaf57ed5254b to your computer and use it in GitHub Desktop.
verilated-rs generated binds to verilator simulation class.
#include <Vtop.h>
extern "C" {
// CONSTRUCTORS
Vtop*
top_new() {
return new Vtop();
}
void
top_delete(Vtop* __ptr) {
delete __ptr;
}
// API METHODS
void
top_eval(Vtop* __ptr) {
__ptr->eval();
}
void
top_final(Vtop* __ptr) {
__ptr->final();
}
void
top_clk_i(Vtop* __ptr, vluint8_t __v) {
__ptr->clk_i = __v;
}
void
top_rst_i(Vtop* __ptr, vluint8_t __v) {
__ptr->rst_i = __v;
}
// PORTS
vluint8_t
top_get_count_o(Vtop* __ptr) {
return __ptr->count_o;
}
}
mod ffi {
#[allow(non_camel_case_types)]
pub enum top {}
extern {
pub fn top_new() -> *mut top;
pub fn top_delete(top: *mut top);
pub fn top_eval(top: *mut top);
pub fn top_final(top: *mut top);
pub fn top_clk_i(top: *mut top, v: u8);
pub fn top_rst_i(top: *mut top, v: u8);
pub fn top_get_count_o(top: *mut top) -> ::std::os::raw::c_uchar;
}
}
pub struct Top(*mut ffi::top);
impl Default for Top {
fn default() -> Self {
let ptr = unsafe { ffi::top_new() };
assert!(!ptr.is_null());
Top(ptr)
}
}
impl Drop for Top {
fn drop(&mut self) {
unsafe {
ffi::top_delete(self.0);
}
}
}
impl Top {
pub fn count_o(&self) -> u8 {
unsafe { ffi::top_get_count_o(self.0) }
}
}
impl ::verilated::test_bench::Module for Top {
fn eval(&mut self) {
unsafe {
ffi::top_eval(self.0);
}
}
fn finish(&mut self) {
unsafe {
ffi::top_final(self.0);
}
}
fn clock_up(&mut self) {
unsafe {
ffi::top_clk_i(self.0, 1);
}
}
fn clock_down(&mut self) {
unsafe {
ffi::top_clk_i(self.0, 0);
}
}
fn reset_up(&mut self) {
unsafe {
ffi::top_rst_i(self.0, 1);
}
}
fn reset_down(&mut self) {
unsafe {
ffi::top_rst_i(self.0, 0);
}
}
}
#[module(top)]
pub struct Top {
#[port(clock)] pub clk_i: bool,
#[port(reset)] pub rst_i: bool,
#[port(output)] pub count_o: [bool; 4],
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment