//////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////
|
//// ////
|
//// ////
|
//// Copyright (C) 2018 Authors and OPENCORES.ORG ////
|
//// Copyright (C) 2018 Authors and OPENCORES.ORG ////
|
//// ////
|
//// ////
|
//// This source file may be used and distributed without ////
|
//// This source file may be used and distributed without ////
|
//// restriction provided that this copyright statement is not ////
|
//// restriction provided that this copyright statement is not ////
|
//// removed from the file and that any derivative work contains ////
|
//// removed from the file and that any derivative work contains ////
|
//// the original copyright notice and the associated disclaimer. ////
|
//// the original copyright notice and the associated disclaimer. ////
|
//// ////
|
//// ////
|
//// This source file is free software; you can redistribute it ////
|
//// This source file is free software; you can redistribute it ////
|
//// and/or modify it under the terms of the GNU Lesser General ////
|
//// and/or modify it under the terms of the GNU Lesser General ////
|
//// Public License as published by the Free Software Foundation; ////
|
//// Public License as published by the Free Software Foundation; ////
|
//// either version 2.1 of the License, or (at your option) any ////
|
//// either version 2.1 of the License, or (at your option) any ////
|
//// later version. ////
|
//// later version. ////
|
//// ////
|
//// ////
|
//// This source is distributed in the hope that it will be ////
|
//// This source is distributed in the hope that it will be ////
|
//// useful, but WITHOUT ANY WARRANTY; without even the implied ////
|
//// useful, but WITHOUT ANY WARRANTY; without even the implied ////
|
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR ////
|
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR ////
|
//// PURPOSE. See the GNU Lesser General Public License for more ////
|
//// PURPOSE. See the GNU Lesser General Public License for more ////
|
//// details. ////
|
//// details. ////
|
//// ////
|
//// ////
|
//// You should have received a copy of the GNU Lesser General ////
|
//// You should have received a copy of the GNU Lesser General ////
|
//// Public License along with this source; if not, download it ////
|
//// Public License along with this source; if not, download it ////
|
//// from http://www.opencores.org/lgpl.shtml ////
|
//// from http://www.opencores.org/lgpl.shtml ////
|
//// ////
|
//// ////
|
//////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////
|
#include <stdio.h>
|
#include <stdio.h>
|
#include <conio.h>
|
#include <conio.h>
|
#include <Python.h>
|
#include <Python.h>
|
#include "vpi_user.h"
|
#include "vpi_user.h"
|
#include "py_dpi.h"
|
#include "py_dpi.h"
|
|
|
// --------------------------------------------------------------------
|
// --------------------------------------------------------------------
|
PyObject *g_dva_i;
|
PyObject *g_dva_i;
|
|
|
// --------------------------------------------------------------------
|
// --------------------------------------------------------------------
|
void c_do_it()
|
void c_do_it()
|
{
|
{
|
printf("^^^ | c_do_it()\n");
|
printf("^^^ | c_do_it()\n");
|
|
|
/* result = instance.method(x,y) */
|
/* result = instance.method(x,y) */
|
PyObject *pmeth = PyObject_GetAttrString(g_dva_i, "do_it");
|
PyObject *pmeth = PyObject_GetAttrString(g_dva_i, "do_it");
|
// Py_DECREF(pinst);
|
// Py_DECREF(pinst);
|
// pargs = Py_BuildValue("(ss)", arg1, arg2); /* convert to Python */
|
// pargs = Py_BuildValue("(ss)", arg1, arg2); /* convert to Python */
|
// PyObject *pres = PyEval_CallObject(pmeth, pargs); /* call method(x,y) */
|
// PyObject *pres = PyEval_CallObject(pmeth, pargs); /* call method(x,y) */
|
PyObject *pres = PyEval_CallObject(pmeth, NULL);
|
PyObject *pres = PyEval_CallObject(pmeth, NULL);
|
Py_DECREF(pmeth);
|
Py_DECREF(pmeth);
|
// Py_DECREF(pargs);
|
// Py_DECREF(pargs);
|
Py_DECREF(pres);
|
Py_DECREF(pres);
|
}
|
}
|
|
|
// --------------------------------------------------------------------
|
// --------------------------------------------------------------------
|
unsigned int *g_va = NULL;
|
unsigned int *g_va = NULL;
|
|
|
void c_get_array(const svOpenArrayHandle va)
|
void c_get_array(const svOpenArrayHandle va)
|
{
|
{
|
int va_y = svSize(va, 1);
|
int va_y = svSize(va, 1);
|
int va_x = svSize(va, 2);
|
int va_x = svSize(va, 2);
|
|
|
printf("^^^ | c_get_array() | %dx%d\n", va_x, va_y);
|
printf("^^^ | c_get_array() | %dx%d\n", va_x, va_y);
|
g_va = (unsigned int *)svGetArrElemPtr2(va, 0, 0);
|
g_va = (unsigned int *)svGetArrElemPtr2(va, 0, 0);
|
|
|
PyObject *pmeth = PyObject_GetAttrString(g_dva_i, "py_get_array");
|
PyObject *pmeth = PyObject_GetAttrString(g_dva_i, "py_get_array");
|
// pargs = Py_BuildValue("(ss)", arg1, arg2); /* convert to Python */
|
// pargs = Py_BuildValue("(ss)", arg1, arg2); /* convert to Python */
|
// PyObject *pres = PyEval_CallObject(pmeth, pargs); /* call method(x,y) */
|
// PyObject *pres = PyEval_CallObject(pmeth, pargs); /* call method(x,y) */
|
PyObject *pres = PyEval_CallObject(pmeth, NULL);
|
PyObject *pres = PyEval_CallObject(pmeth, NULL);
|
|
|
Py_DECREF(pmeth);
|
Py_DECREF(pmeth);
|
// Py_DECREF(pargs);
|
// Py_DECREF(pargs);
|
Py_DECREF(pres);
|
Py_DECREF(pres);
|
}
|
}
|
|
|
// --------------------------------------------------------------------
|
// --------------------------------------------------------------------
|
#define MAX_ARGV 4
|
#define MAX_ARGV 4
|
|
|
int py_run_file
|
int py_run_file
|
( const char *filename
|
( const char *filename
|
, int argc
|
, int argc
|
, const svOpenArrayHandle args
|
, const svOpenArrayHandle args
|
)
|
)
|
{
|
{
|
printf("^^^ | py_run_file | %s |\n", filename);
|
printf("^^^ | py_run_file | %s |\n", filename);
|
|
|
wchar_t *w_argv[MAX_ARGV]; // limit of MAX_ARGV args
|
wchar_t *w_argv[MAX_ARGV]; // limit of MAX_ARGV args
|
int i;
|
int i;
|
for(i = 0; i < argc && i < MAX_ARGV; i++)
|
for(i = 0; i < argc && i < MAX_ARGV; i++)
|
w_argv[i] = Py_DecodeLocale(*((char **)svGetArrElemPtr1(args, i)), NULL);
|
w_argv[i] = Py_DecodeLocale(*((char **)svGetArrElemPtr1(args, i)), NULL);
|
|
|
PySys_SetArgv(argc, w_argv);
|
PySys_SetArgv(argc, w_argv);
|
|
|
char pre_string[] = "exec(open('./";
|
char pre_string[] = "exec(open('./";
|
char post_string[] = "').read())";
|
char post_string[] = "').read())";
|
char v_filename[] = "try_it.py";
|
|
unsigned int size = (sizeof(pre_string) + sizeof(post_string)) * sizeof(char);
|
unsigned int size = (sizeof(pre_string) + sizeof(post_string)) * sizeof(char);
|
size += strlen(filename);
|
size += strlen(filename);
|
char *run_string = (char *)malloc(size);
|
char *run_string = (char *)malloc(size);
|
sprintf(run_string, "%s%s%s", pre_string, filename, post_string);
|
sprintf(run_string, "%s%s%s", pre_string, filename, post_string);
|
|
|
PyRun_SimpleString(run_string);
|
PyRun_SimpleString(run_string);
|
free(run_string);
|
free(run_string);
|
return 0;
|
return 0;
|
}
|
}
|
|
|
// --------------------------------------------------------------------
|
// --------------------------------------------------------------------
|
extern PyMODINIT_FUNC PyInit_py_to_video_frame(void);
|
extern PyMODINIT_FUNC PyInit_py_to_video_frame(void);
|
|
|
void init_py_dpi(int width, int height)
|
void init_py_dpi(int width, int height)
|
{
|
{
|
printf("^^^ | init_py_dpi() | %dx%d\n", width, height);
|
printf("^^^ | init_py_dpi() | %dx%d\n", width, height);
|
|
|
PyImport_AppendInittab("py_to_video_frame", PyInit_py_to_video_frame);
|
PyImport_AppendInittab("py_to_video_frame", PyInit_py_to_video_frame);
|
Py_Initialize();
|
Py_Initialize();
|
|
|
PyObject *sys = PyImport_ImportModule("sys");
|
PyObject *sys = PyImport_ImportModule("sys");
|
PyObject *path = PyObject_GetAttrString(sys, "path");
|
PyObject *path = PyObject_GetAttrString(sys, "path");
|
PyList_Append(path, PyUnicode_FromString("."));
|
PyList_Append(path, PyUnicode_FromString("."));
|
|
|
PyObject *dva_m = PyImport_ImportModule("dpi_video_array");
|
PyObject *dva_m = PyImport_ImportModule("dpi_video_array");
|
if(!dva_m)
|
if(!dva_m)
|
{
|
{
|
PyErr_Print();
|
PyErr_Print();
|
printf("^^^ | init_py_dpi() | dva_m Error\n");
|
printf("^^^ | init_py_dpi() | dva_m Error\n");
|
}
|
}
|
|
|
PyObject *dva_c = PyObject_GetAttrString(dva_m, "dpi_video_array");
|
PyObject *dva_c = PyObject_GetAttrString(dva_m, "dpi_video_array");
|
Py_DECREF(dva_m);
|
Py_DECREF(dva_m);
|
if(!dva_c)
|
if(!dva_c)
|
{
|
{
|
PyErr_Print();
|
PyErr_Print();
|
printf("^^^ | init_py_dpi() | dva_c Error\n");
|
printf("^^^ | init_py_dpi() | dva_c Error\n");
|
}
|
}
|
|
|
PyObject *args = Py_BuildValue("(ii)", width, height);
|
PyObject *args = Py_BuildValue("(ii)", width, height);
|
g_dva_i = PyObject_CallObject(dva_c, args);
|
g_dva_i = PyObject_CallObject(dva_c, args);
|
Py_DECREF(args);
|
Py_DECREF(args);
|
Py_DECREF(dva_c);
|
Py_DECREF(dva_c);
|
if(!g_dva_i)
|
if(!g_dva_i)
|
{
|
{
|
PyErr_Print();
|
PyErr_Print();
|
printf("^^^ | init_py_dpi() | g_dva_i Error\n");
|
printf("^^^ | init_py_dpi() | g_dva_i Error\n");
|
Py_DECREF(g_dva_i);
|
Py_DECREF(g_dva_i);
|
g_dva_i = NULL;
|
g_dva_i = NULL;
|
}
|
}
|
}
|
}
|
|
|
// --------------------------------------------------------------------
|
// --------------------------------------------------------------------
|
void exit_py_dpi()
|
void exit_py_dpi()
|
{
|
{
|
printf("^^^ | exit_py_dpi()\n");
|
printf("^^^ | exit_py_dpi()\n");
|
|
|
if(g_dva_i)
|
if(g_dva_i)
|
Py_DECREF(g_dva_i);
|
Py_DECREF(g_dva_i);
|
|
|
Py_Finalize();
|
Py_Finalize();
|
}
|
}
|
|
|