00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include "config.h"
00027 #if HAVE_IVERILOG_VPI_USER_H
00028 #include <iverilog/vpi_user.h>
00029 #else
00030 #include <vpi_user.h>
00031 #endif
00032
00033 #include "avrdevice.h"
00034 #include "avrfactory.h"
00035 #include "rwmem.h"
00036 #include "pin.h"
00037 #include "avrerror.h"
00038 #include "cmd/dumpargs.h"
00039 #include "systemclock.h"
00040
00041 static std::vector<AvrDevice*> devices;
00042
00043 static bool checkHandle(int h) {
00044 if (h>=devices.size()) {
00045 vpi_printf("There has never been an AVR device with the handle %d.", h);
00046 vpi_control(vpiFinish, 1);
00047 return false;
00048 } else {
00049 if (!devices[h]) {
00050 vpi_printf("The AVR with handle %d has already been destroyed.", h);
00051 vpi_control(vpiFinish, 1);
00052 return false;
00053 }
00054 }
00055 return true;
00056 }
00057
00058 #define VPI_UNPACKS(name) \
00059 { \
00060 vpiHandle name = vpi_scan(argv); \
00061 if (! name) { \
00062 vpi_printf("%s: " #name " parameter missing.\n", xx);\
00063 vpi_free_object(argv); \
00064 return 0; \
00065 } \
00066 value.format = vpiStringVal; \
00067 vpi_get_value(name, &value); \
00068 } \
00069 std::string name = value.value.str;
00070
00071 #define VPI_UNPACKI(name) \
00072 { \
00073 vpiHandle name = vpi_scan(argv); \
00074 if (! name) { \
00075 vpi_printf("%s: " #name " parameter missing.\n", xx);\
00076 vpi_free_object(argv); \
00077 return 0; \
00078 } \
00079 value.format = vpiIntVal; \
00080 vpi_get_value(name, &value); \
00081 } \
00082 int name = value.value.integer;
00083
00084 #define VPI_UNPACKT(name) \
00085 { \
00086 vpiHandle name = vpi_scan(argv); \
00087 if (! name) { \
00088 vpi_printf("%s: " #name " parameter missing.\n", xx);\
00089 vpi_free_object(argv); \
00090 return 0; \
00091 } \
00092 value.format = vpiTimeVal; \
00093 vpi_get_value(name, &value); \
00094 } \
00095 t_vpi_time *name = value.value.time;
00096
00097 #define VPI_RETURN_INT(val) \
00098 value.format = vpiIntVal; \
00099 value.value.integer = (val); \
00100 vpi_put_value(ch, &value, 0, vpiNoDelay); \
00101 return 0;
00102
00103 #define AVR_HCHECK() \
00104 if (!checkHandle(handle)) { \
00105 vpi_printf("%s: Invalid handle parameter.\n", xx); \
00106 return 0; \
00107 }
00108
00109 #define VPI_BEGIN() \
00110 s_vpi_value value; \
00111 vpiHandle ch = vpi_handle(vpiSysTfCall, 0); \
00112 vpiHandle argv = vpi_iterate(vpiArgument, ch);
00113
00114 #define VPI_END() \
00115 vpi_free_object(argv);
00116
00117 #define VPI_REGISTER_TASK(name) \
00118 { \
00119 s_vpi_systf_data tf_data; \
00120 tf_data.type = vpiSysTask; \
00121 tf_data.tfname = "$" #name; \
00122 tf_data.calltf = name ## _tf; \
00123 tf_data.compiletf = 0; \
00124 tf_data.sizetf = 0; \
00125 tf_data.user_data = "$" #name; \
00126 vpi_register_systf(&tf_data); \
00127 }
00128
00129 #define VPI_REGISTER_FUNC(name) \
00130 { \
00131 s_vpi_systf_data tf_data; \
00132 tf_data.type = vpiSysFunc; \
00133 tf_data.tfname = "$" #name; \
00134 tf_data.calltf = name ## _tf; \
00135 tf_data.compiletf = 0; \
00136 tf_data.sizetf = 0; \
00137 tf_data.user_data = "$" #name; \
00138 vpi_register_systf(&tf_data); \
00139 }
00140
00153 static PLI_INT32 avr_create_tf(char *xx) {
00154 VPI_BEGIN();
00155 VPI_UNPACKS(device);
00156 VPI_UNPACKS(progname);
00157 VPI_END();
00158
00159 AvrDevice* dev=AvrFactory::instance().makeDevice(device.c_str());
00160 devices.push_back(dev);
00161
00162 dev->Load(progname.c_str());
00163
00164 VPI_RETURN_INT(devices.size()-1);
00165 }
00166
00173 static PLI_INT32 avr_reset_tf(char *xx) {
00174 VPI_BEGIN();
00175 VPI_UNPACKI(handle);
00176 VPI_END();
00177
00178 AVR_HCHECK();
00179 devices[handle]->Reset();
00180 return 0;
00181 }
00182
00189 static PLI_INT32 avr_destroy_tf(char *xx) {
00190 VPI_BEGIN();
00191 VPI_UNPACKI(handle);
00192 VPI_END();
00193
00194 AVR_HCHECK();
00195
00196
00197
00198 delete devices[handle];
00199 devices[handle]=0;
00200 return 0;
00201 }
00202
00209 static PLI_INT32 avr_tick_tf(char *xx) {
00210 VPI_BEGIN();
00211 VPI_UNPACKI(handle);
00212 VPI_END();
00213 AVR_HCHECK();
00214
00215 bool no_hw=false;
00216 devices[handle]->Step(no_hw);
00217 return 0;
00218 }
00219
00224 static PLI_INT32 avr_set_time_tf(char *xx) {
00225 VPI_BEGIN();
00226 VPI_UNPACKT(time);
00227 VPI_END();
00228
00229
00230 uint64_t tfull=(time->low)<<32+((uint64_t) time->high);
00231 SystemClock::Instance().SetCurrentTime(tfull);
00232
00233 return 0;
00234 }
00235
00241 static PLI_INT32 avr_get_pin_tf(char *xx) {
00242 VPI_BEGIN();
00243 VPI_UNPACKI(handle);
00244 VPI_UNPACKS(name);
00245 VPI_END();
00246
00247 AVR_HCHECK();
00248
00249 Pin *pin=devices[handle]->GetPin(name.c_str());
00250 int ret(pin->outState);
00251
00252 VPI_RETURN_INT(ret);
00253 }
00254
00260 static PLI_INT32 avr_set_pin_tf(char *xx) {
00261 VPI_BEGIN();
00262 VPI_UNPACKI(handle);
00263 VPI_UNPACKS(name);
00264 VPI_UNPACKI(val);
00265 VPI_END();
00266
00267 AVR_HCHECK();
00268
00269 Pin *pin=devices[handle]->GetPin(name.c_str());
00270
00271
00272
00273
00274 pin->SetInState(Pin::T_Pinstate(val));
00275 return 0;
00276 }
00277
00284 static PLI_INT32 avr_get_pc_tf(char *xx) {
00285 VPI_BEGIN();
00286 VPI_UNPACKI(handle);
00287 VPI_END();
00288
00289 AVR_HCHECK();
00290
00291 VPI_RETURN_INT(devices[handle]->cPC);
00292 }
00293
00303 static PLI_INT32 avr_get_rw_tf(char *xx) {
00304 VPI_BEGIN();
00305 VPI_UNPACKI(handle);
00306 VPI_UNPACKI(address);
00307 VPI_END();
00308
00309 AVR_HCHECK();
00310
00311 VPI_RETURN_INT(devices[handle]->GetRWMem(address));
00312 }
00313
00323 static PLI_INT32 avr_set_rw_tf(char *xx) {
00324 VPI_BEGIN();
00325 VPI_UNPACKI(handle);
00326 VPI_UNPACKI(address);
00327 VPI_UNPACKI(val);
00328 VPI_END();
00329
00330 AVR_HCHECK();
00331
00332 devices[handle]->SetRWMem(address, val);
00333 return 0;
00334 }
00335
00349 static PLI_INT32 avr_trace_tf(char *xx) {
00350 VPI_BEGIN();
00351 VPI_UNPACKS(tracename);
00352 VPI_END();
00353
00354 if (tracename.length()) {
00355 sysConHandler.SetTraceFile(tracename.c_str(), 1000000);
00356 for (size_t i=0; i < devices.size(); i++)
00357 devices[i]->trace_on=1;
00358 } else {
00359 sysConHandler.StopTrace();
00360 for (size_t i=0; i < devices.size(); i++)
00361 devices[i]->trace_on=0;
00362 }
00363 return 0;
00364 }
00365
00369 static PLI_INT32 avr_dump_arg_tf(char *xx) {
00370 VPI_BEGIN();
00371 VPI_UNPACKI(handle);
00372 VPI_UNPACKS(dumparg);
00373 VPI_END();
00374 AVR_HCHECK();
00375 std::vector<std::string> dargs;
00376 dargs.push_back(dumparg);
00377 SetDumpTraceArgs(dargs, devices[handle]);
00378 return 0;
00379 }
00380
00383 static PLI_INT32 avr_dump_start_tf(char *xx) {
00384 DumpManager::Instance()->start();
00385 return 0;
00386 }
00387
00389 static PLI_INT32 avr_dump_stop_tf(char *xx) {
00390 DumpManager::Instance()->stopApplication();
00391 return 0;
00392 }
00393
00394 static void register_tasks() {
00395 VPI_REGISTER_FUNC(avr_create);
00396 VPI_REGISTER_TASK(avr_reset);
00397 VPI_REGISTER_TASK(avr_destroy);
00398 VPI_REGISTER_TASK(avr_tick);
00399 VPI_REGISTER_TASK(avr_set_time);
00400 VPI_REGISTER_FUNC(avr_get_pin);
00401 VPI_REGISTER_TASK(avr_set_pin);
00402 VPI_REGISTER_FUNC(avr_get_pc);
00403 VPI_REGISTER_FUNC(avr_get_rw);
00404 VPI_REGISTER_TASK(avr_set_rw);
00405 VPI_REGISTER_TASK(avr_trace);
00406 VPI_REGISTER_TASK(avr_dump_arg);
00407 VPI_REGISTER_TASK(avr_dump_start);
00408 VPI_REGISTER_TASK(avr_dump_stop);
00409 }
00410
00411
00412
00413 void (*vlog_startup_routines[])() = {
00414 ®ister_tasks,
00415 0
00416 };