litex/vpi/main.c

204 lines
4.0 KiB
C
Raw Normal View History

2012-03-23 11:41:30 -04:00
/*
* Copyright (C) 2012 Vermeer Manufacturing Co.
* License: GPLv3 with additional permissions (see README).
*/
2012-03-04 16:33:03 -05:00
#include <assert.h>
2012-03-04 15:27:02 -05:00
#include <stdio.h>
#include <stdlib.h>
#include <vpi_user.h>
#include "ipc.h"
struct migensim_softc {
struct ipc_softc *ipc;
int has_go;
};
static int h_go(void *user)
{
struct migensim_softc *sc = (struct migensim_softc *)user;
sc->has_go = 1;
return 1;
}
2012-03-05 09:40:21 -05:00
static s_vpi_time zero_delay = {
.type = vpiSimTime,
.high = 0,
.low = 0
};
2012-03-06 13:29:39 -05:00
static int h_write(char *name, int index, int nchunks, const unsigned char *chunks, void *user)
2012-03-04 15:27:02 -05:00
{
2012-03-05 09:40:21 -05:00
vpiHandle item;
s_vpi_vecval vector[64];
2012-03-04 15:27:02 -05:00
int i;
2012-03-05 09:40:21 -05:00
s_vpi_value value;
item = vpi_handle_by_name(name, NULL);
if(item == NULL) {
fprintf(stderr, "Attempted to write non-existing signal %s\n", name);
return 0;
}
2012-03-06 13:29:39 -05:00
if(vpi_get(vpiType, item) == vpiMemory)
item = vpi_handle_by_index(item, index);
else
assert(index == 0);
2012-03-04 15:27:02 -05:00
2012-03-05 09:40:21 -05:00
assert(nchunks <= 255);
for(i=0;i<64;i++) {
vector[i].aval = 0;
vector[i].bval = 0;
}
2012-03-04 15:27:02 -05:00
for(i=0;i<nchunks;i++)
2012-03-05 09:40:21 -05:00
vector[i/4].aval |= chunks[i] << 8*(i % 4);
value.format = vpiVectorVal;
value.value.vector = vector;
vpi_put_value(item, &value, &zero_delay, vpiInertialDelay);
2012-03-04 15:27:02 -05:00
return 1;
}
2012-03-06 13:29:39 -05:00
static int h_read(char *name, int index, void *user)
2012-03-04 15:27:02 -05:00
{
2012-03-04 16:33:03 -05:00
struct migensim_softc *sc = (struct migensim_softc *)user;
vpiHandle item;
s_vpi_value value;
int size;
int i;
2012-03-04 16:56:56 -05:00
int nvals;
unsigned int vals[64];
int nchunks;
2012-03-04 16:33:03 -05:00
unsigned char chunks[255];
item = vpi_handle_by_name(name, NULL);
if(item == NULL) {
fprintf(stderr, "Attempted to read non-existing signal %s\n", name);
return 0;
}
2012-03-06 13:29:39 -05:00
if(vpi_get(vpiType, item) == vpiMemory)
item = vpi_handle_by_index(item, index);
else
assert(index == 0);
2012-03-04 16:33:03 -05:00
value.format = vpiVectorVal;
vpi_get_value(item, &value);
size = vpi_get(vpiSize, item);
2012-03-04 16:56:56 -05:00
nvals = (size + 31)/32;
assert(nvals <= 64);
for(i=0;i<nvals;i++)
vals[i] = value.value.vector[i].aval & ~value.value.vector[i].bval;
2012-03-04 16:33:03 -05:00
nchunks = (size + 7)/8;
2012-03-04 16:56:56 -05:00
assert(nchunks <= 255);
2012-03-04 16:33:03 -05:00
for(i=0;i<nchunks;i++) {
switch(i % 4) {
case 0:
2012-03-04 16:56:56 -05:00
chunks[i] = vals[i/4] & 0xff;
2012-03-04 16:33:03 -05:00
break;
case 1:
2012-03-04 16:56:56 -05:00
chunks[i] = (vals[i/4] & 0xff00) >> 8;
2012-03-04 16:33:03 -05:00
break;
case 2:
2012-03-04 16:56:56 -05:00
chunks[i] = (vals[i/4] & 0xff0000) >> 16;
2012-03-04 16:33:03 -05:00
break;
case 3:
2012-03-04 16:56:56 -05:00
chunks[i] = (vals[i/4] & 0xff000000) >> 24;
2012-03-04 16:33:03 -05:00
break;
}
}
if(!ipc_read_reply(sc->ipc, nchunks, chunks)) {
perror("ipc_read_reply");
return 0;
}
2012-03-04 15:27:02 -05:00
return 1;
}
static int process_until_go(struct migensim_softc *sc)
{
2012-03-06 09:00:02 -05:00
int r;
2012-03-04 15:27:02 -05:00
sc->has_go = 0;
while(!sc->has_go) {
2012-03-06 09:00:02 -05:00
r = ipc_receive(sc->ipc);
if(r != 1)
return r;
2012-03-04 15:27:02 -05:00
}
return 1;
}
2012-03-04 16:56:56 -05:00
static PLI_INT32 connect_calltf(PLI_BYTE8 *user)
2012-03-04 15:27:02 -05:00
{
struct migensim_softc *sc = (struct migensim_softc *)user;
vpiHandle sys;
vpiHandle argv;
vpiHandle item;
s_vpi_value value;
sys = vpi_handle(vpiSysTfCall, 0);
argv = vpi_iterate(vpiArgument, sys);
item = vpi_scan(argv);
value.format = vpiStringVal;
vpi_get_value(item, &value);
sc->ipc = ipc_connect(value.value.str, h_go, h_write, h_read, sc);
if(sc->ipc == NULL) {
perror("ipc_connect");
vpi_control(vpiFinish, 1);
return 0;
}
return 0;
}
2012-03-04 16:56:56 -05:00
static PLI_INT32 tick_calltf(PLI_BYTE8 *user)
2012-03-04 15:27:02 -05:00
{
struct migensim_softc *sc = (struct migensim_softc *)user;
2012-03-06 09:00:02 -05:00
int r;
2012-03-04 15:27:02 -05:00
if(!ipc_tick(sc->ipc)) {
perror("ipc_tick");
vpi_control(vpiFinish, 1);
2012-03-06 09:00:02 -05:00
ipc_destroy(sc->ipc);
sc->ipc = NULL;
2012-03-04 15:27:02 -05:00
return 0;
}
2012-03-06 09:00:02 -05:00
r = process_until_go(sc);
if(r != 1) {
vpi_control(vpiFinish, r == 2 ? 0 : 1);
ipc_destroy(sc->ipc);
sc->ipc = NULL;
2012-03-04 15:27:02 -05:00
return 0;
}
return 0;
}
static struct migensim_softc sc;
2012-03-04 16:56:56 -05:00
static void simple_register(const char *tfname, PLI_INT32 (*calltf)(PLI_BYTE8 *))
2012-03-04 15:27:02 -05:00
{
s_vpi_systf_data tf_data;
tf_data.type = vpiSysTask;
tf_data.tfname = tfname;
tf_data.calltf = calltf;
tf_data.compiletf = NULL;
tf_data.sizetf = 0;
tf_data.user_data = (void *)&sc;
vpi_register_systf(&tf_data);
}
static void migensim_register()
{
simple_register("$migensim_connect", connect_calltf);
simple_register("$migensim_tick", tick_calltf);
}
void (*vlog_startup_routines[])() = {
migensim_register,
0
};