Skip to content

Commit

Permalink
feat(examples/vhdl/external_buffer): add serve.py and adapt Vue.js fr…
Browse files Browse the repository at this point in the history
…ontend
  • Loading branch information
umarcor committed Oct 7, 2019
1 parent 459d5fe commit 69fc272
Show file tree
Hide file tree
Showing 6 changed files with 197 additions and 16 deletions.
18 changes: 14 additions & 4 deletions examples/vhdl/external_buffer/cosim.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,26 @@

data = [111, 122, 133, 144, 155]

buf = byte_buf(data + [0 for x in range(2*len(data))])
buf = [[] for c in range(2)]
buf[1] = byte_buf(data + [0 for x in range(2*len(data))])

ghdl.set_string_ptr(0, buf)
buf[0] = int_buf([
-2**31+10,
-2**31,
3, # clk_step
0, # update
len(data) # block_length
])

for i, v in enumerate(read_byte_buf(buf)):
for x, v in enumerate(buf):
ghdl.set_string_ptr(x, v)

for i, v in enumerate(read_byte_buf(buf[1])):
print("py " + str(i) + ": " + str(v))

ghdl.ghdl_main(len(xargs)-1, xargs)

for i, v in enumerate(read_byte_buf(buf)):
for i, v in enumerate(read_byte_buf(buf[1])):
print("py " + str(i) + ": " + str(v))

dlclose(ghdl)
69 changes: 69 additions & 0 deletions examples/vhdl/external_buffer/serve.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import ctypes
from os.path import join, dirname
from vunit import VUnit
from websim import *


root = dirname(__file__)


# Allocate and define shared data buffers

data = [3*c for c in range(64)]

buf = [[] for c in range(2)]
buf[1] = byte_buf(data + [0 for x in range(2*len(data))])

buf[0] = int_buf([
-2**31+1,
-2**31,
0, # clk_step
1, # update
len(data) # block_length
])


# Load args and define simulation callbacks

sim = None
args = [line.rstrip('\n') for line in open(join(root, 'args.txt'))]


def load():
g = ctypes.CDLL(args[0])
sim.handler(g)

for idx, val in enumerate(buf):
g.set_string_ptr(idx, val)

xargs = enc_args(args)
return(g.ghdl_main(len(xargs)-1, xargs))


def update_cb():
p = read_int_buf(buf[0])[0:3]
p[0] -= -2**31
p[1] -= -2**31
return {
'name': 'external_buffer',
'params': p,
'data': {
'mem': read_byte_buf(buf[1])
}
}


def unload():
dlclose(sim.handler())


# Instantiate WebSim and run server

sim = WebSim(
dist=join(root, '..', 'vue', 'dist'),
load_cb=load,
unload_cb=unload,
update_cb=update_cb
)

sim.run()
24 changes: 20 additions & 4 deletions examples/vhdl/external_buffer/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ or tb_ext_integer_vector.vhd
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <limits.h>
#include "vhpidirect_user.h"

const uint32_t length = 5;
Expand All @@ -41,7 +42,7 @@ static void exit_handler(void) {
z = (length*j)+i;

expected = (i+1)*11 + k;
got = ((TYPE*)D[0])[z];
got = ((TYPE*)D[1])[z];
if (expected != got) {
printf("check error %d: %d %d\n", z, expected, got);
exit(1);
Expand All @@ -50,26 +51,41 @@ static void exit_handler(void) {
}
}
free(D[0]);
free(D[1]);
}

// Main entrypoint of the application
int main(int argc, char **argv) {
// Allocate a buffer which is three times the number of values
// that we want to copy/modify
D[0] = (uint8_t *) malloc(3*length*sizeof(TYPE));
D[0] = (uint8_t *) malloc(5*sizeof(int32_t));
if ( D[0] == NULL ) {
perror("execution of malloc() failed!\n");
return -1;
}

// Initialize 'params' array
int32_t *P = (int32_t*)D[0];
P[0] = INT_MIN+10;
P[1] = INT_MIN;
P[2] = 3; // clk_step
P[3] = 0; // update
P[4] = length; // block_length

// Initialize the first 1/3 of the buffer
D[1] = (uint8_t *) malloc(3*length*sizeof(TYPE));
if ( D[1] == NULL ) {
perror("execution of malloc() failed!\n");
return -1;
}
int i;
for(i=0; i<length; i++) {
((TYPE*)D[0])[i] = (i+1)*11;
((TYPE*)D[1])[i] = (i+1)*11;
}
// Print all the buffer
printf("sizeof: %lu\n", sizeof(TYPE));
for(i=0; i<3*length; i++) {
printf("%d: %d\n", i, ((TYPE*)D[0])[i]);
printf("%d: %d\n", i, ((TYPE*)D[1])[i]);
}

// Register a function to be called when GHDL exits
Expand Down
50 changes: 43 additions & 7 deletions examples/vhdl/external_buffer/src/tb_ext_byte_vector.vhd
Original file line number Diff line number Diff line change
Expand Up @@ -11,44 +11,80 @@
--library vunit_lib;
--context vunit_lib.vunit_context;

library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;

library vunit_lib;
use vunit_lib.run_pkg.all;
use vunit_lib.logger_pkg.all;
use vunit_lib.types_pkg.all;
use vunit_lib.byte_vector_ptr_pkg.all;
use vunit_lib.integer_vector_ptr_pkg.all;
use vunit_lib.clk_pkg.all;

entity tb_external_byte_vector is
generic ( runner_cfg : string );
end entity;

architecture tb of tb_external_byte_vector is

constant block_len : natural := 5;
constant params: integer_vector_ptr_t := new_integer_vector_ptr(6, extacc, 0);

constant clk_step : natural := get(params, 2);
constant block_len : integer := get(params, 4);

constant ebuf: byte_vector_ptr_t := new_byte_vector_ptr( 3*block_len, extfnc, 0); -- external through VHPIDIRECT functions 'read_char' and 'write_char'
constant abuf: byte_vector_ptr_t := new_byte_vector_ptr( 3*block_len, extacc, 0); -- external through access (requires VHPIDIRECT function 'get_string_ptr')

signal clk, rst, rstn : std_logic := '0';
signal done, tg, start : boolean := false;

begin

main: process
variable val, ind: integer;
begin
rstn <= not rst;

clk_gen: entity vunit_lib.clk_handler generic map ( params ) port map ( rst, clk, tg );

run: process(tg) begin if rising_edge(tg) then
info("UPDATE READY");
set(params, 3, 1);
end if; end process;

main: process begin
test_runner_setup(runner, runner_cfg);
info("Init test");
rst <= '1';
info("Init test: " & to_string(block_len));
wait for 100 ns;
rst <= '0';
wait_load(params);
start <= true;
wait_sync(params, done, tg);
info("Test done");
test_runner_cleanup(runner);
wait;
end process;

stimuli: process
variable val: integer;
begin
wait until start;
wait_for(clk, 1);
for x in 0 to block_len-1 loop
val := get(ebuf, x) + 1;
ind := block_len+x;
set(ebuf, ind, val);
info("SET " & to_string(ind) & ": " & to_string(val));
wait_for(clk, 1);
end loop;
for x in block_len to 2*block_len-1 loop
val := get(abuf, x) + 2;
ind := block_len+x;
set(abuf, ind, val);
info("SET " & to_string(ind) & ": " & to_string(val));
wait_for(clk, 1);
end loop;
info("End test");
test_runner_cleanup(runner);
done <= true;
wait;
end process;

Expand Down
9 changes: 8 additions & 1 deletion examples/vhdl/vue/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
</div>
</div>
</section>

<external_buffer
v-if="loaded && (name=='external_buffer')"
:data.sync="data"/>
</div>
</template>

Expand All @@ -38,10 +42,13 @@ Vue.use(Buefy);
import NavBar from "@/components/NavBar.vue";
import external_buffer from "@/cases/external_buffer.vue";
export default {
name: 'app',
components: {
NavBar
NavBar,
external_buffer
},
data() {
return {
Expand Down
43 changes: 43 additions & 0 deletions examples/vhdl/vue/src/cases/external_buffer.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<template>
<div class="container">
<Memory
name="buffer"
:buf="content.mem"/>
</div>
</template>

<script>
import Memory from "@/components/Memory.vue";
function isEmpty(obj) {
for(var key in obj) {
if(obj.hasOwnProperty(key))
return false;
}
return true;
}
export default {
name: 'external_buffer',
components: {
Memory
},
props: {
data: Object
},
computed: {
content() {
return (isEmpty(this.data)) ? {
mem: [],
} : {
mem: this.data.mem,
};
}
},
methods: {
isEmpty(d) {
return isEmpty(d);
}
}
}
</script>

0 comments on commit 69fc272

Please sign in to comment.