// -*- C++ -*- //************************************************************************* // // Copyright 2003-2008 by Wilson Snyder. This program is free software; you can // redistribute it and/or modify it under the terms of either the GNU // Lesser General Public License or the Perl Artistic License. // // Verilator is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // //========================================================================= /// /// \file /// \brief Verilator: Linked against all applications using Verilated source. /// /// This file must be compiled and linked against all objects /// created from Verilator. /// /// Code available from: http://www.veripool.org/verilator /// //========================================================================= #include "verilated.h" #include #define VL_VALUE_STRING_MAX_WIDTH 1024 ///< Max static char array for VL_VALUE_STRING //=========================================================================== // Global variables int Verilated::s_randReset = 0; int Verilated::s_debug = 1; bool Verilated::s_calcUnusedSigs = false; bool Verilated::s_gotFinish = false; bool Verilated::s_assertOn = true; //=========================================================================== // User definable functions #ifndef VL_USER_FINISH // Define this to override this function void vl_finish (const char* filename, int linenum, const char* hier) { if (0 && hier) {} VL_PRINTF("- %s:%d: Verilog $finish\n", filename, linenum); if (Verilated::gotFinish()) { VL_PRINTF("- %s:%d: Second verilog $finish, exiting\n", filename, linenum); exit(0); } Verilated::gotFinish(true); } #endif #ifndef VL_USER_STOP // Define this to override this function void vl_stop (const char* filename, int linenum, const char* hier) { Verilated::gotFinish(true); vl_fatal (filename,linenum,hier,"Verilog $stop"); } #endif #ifndef VL_USER_FATAL // Define this to override this function void vl_fatal (const char* filename, int linenum, const char* hier, const char* msg) { if (0 && hier) {} Verilated::gotFinish(true); VL_PRINTF("%%Error: %s:%d: %s\n", filename, linenum, msg); abort(); } #endif //=========================================================================== // Random reset -- Only called at init time, so don't inline. IData VL_RAND32() { #if defined(_WIN32) && !defined(__CYGWIN__) // Windows doesn't have lrand48(), although Cygwin does. return (rand()<<16) | rand(); #else return (lrand48()<<16) | lrand48(); #endif } IData VL_RANDOM_I(int obits) { return VL_RAND32() & VL_MASK_I(obits); } QData VL_RANDOM_Q(int obits) { QData data = ((QData)VL_RAND32()<=0; lsb--) { lsb = (lsb / 8) * 8; // Next digit IData charval = (ld>>VL_BITBIT_Q(lsb)) & 0xff; *strp++ = (charval==0)?' ':charval; } *strp++ = '\0'; return str; case 'b': for (; lsb>=0; lsb--) { *strp++ = ((ld>>VL_BITBIT_Q(lsb)) & 1) + '0'; } *strp++ = '\0'; return str; case 'o': for (; lsb>=0; lsb--) { lsb = (lsb / 3) * 3; // Next digit *strp++ = ((ld>>VL_BITBIT_Q(lsb)) & 7) + '0'; } *strp++ = '\0'; return str; default: for (; lsb>=0; lsb--) { lsb = (lsb / 4) * 4; // Next digit IData charval = (ld>>VL_BITBIT_Q(lsb)) & 0xf; *strp++ = (charval + ((charval < 10) ? '0':('a'-10))); } *strp++ = '\0'; return str; } *strp++ = '\0'; return str; } const char* VL_VALUE_FORMATTED_W(int obits, char fmt, bool drop0, WDataInP lwp) { // Convert value into %b/%o/%x/%s/%u/%d formatted string // Note uses a single buffer; presumes only one call per printf static VL_THREAD char str[VL_VALUE_STRING_MAX_WIDTH]; char* strp = &str[0]; int lsb=obits-1; if (drop0) while (lsb && !VL_BITISSET_W(lwp,lsb)) lsb--; switch (fmt) { case 's': for (; lsb>=0; lsb--) { lsb = (lsb / 8) * 8; // Next digit IData charval = (lwp[VL_BITWORD_I(lsb)]>>VL_BITBIT_I(lsb)) & 0xff; *strp++ = (charval==0)?' ':charval; } *strp++ = '\0'; return str; case 'b': for (; lsb>=0; lsb--) { *strp++ = ((lwp[VL_BITWORD_I(lsb)]>>VL_BITBIT_I(lsb)) & 1) + '0'; } *strp++ = '\0'; return str; case 'o': for (; lsb>=0; lsb--) { lsb = (lsb / 3) * 3; // Next digit *strp++ = ((lwp[VL_BITWORD_I(lsb)]>>VL_BITBIT_I(lsb)) & 7) + '0'; } *strp++ = '\0'; return str; default: for (; lsb>=0; lsb--) { lsb = (lsb / 4) * 4; // Next digit IData charval = (lwp[VL_BITWORD_I(lsb)]>>VL_BITBIT_I(lsb)) & 0xf; *strp++ = (charval + ((charval < 10) ? '0':('a'-10))); } *strp++ = '\0'; return str; } *strp++ = '\0'; return str; } //=========================================================================== // File I/O void _VL_VINT_TO_STRING(int obits, char* destoutp, WDataInP sourcep) { int lsb=obits-1; bool start=true; char* destp = destoutp; for (; lsb>=0; lsb--) { lsb = (lsb / 8) * 8; // Next digit IData charval = (sourcep[VL_BITWORD_I(lsb)]>>VL_BITBIT_I(lsb)) & 0xff; if (!start || charval) { *destp++ = (charval==0)?' ':charval; start = false; // Drop leading 0s } } *destp++ = '\0'; // Terminate while (isspace(*(destp-1)) && destp>destoutp) *--destp = '\0'; // Drop trailing spaces } void _VL_STRING_TO_VINT(int obits, void* destp, int srclen, const char* srcp) { // Convert C string to Verilog format int bytes = VL_BYTES_I(obits); char* op = ((char*)(destp)); if (srclen > bytes) srclen = bytes; // Don't overflow destination int i; for (i=0; i= 'a' ? (c-'a'+10) : (c-'0')); if (!innum) { // Prep for next number if (needinc) { addr++; needinc=false; } } if (reading_addr) { // Decode @ addresses if (!innum) addr=0; addr = (addr<<4) + value; } else { needinc = true; //printf(" Value width=%d @%x = %c\n", width, addr, c); if (addr >= (IData)(depth+array_lsb) || addr < (IData)(array_lsb)) { vl_fatal (ofilenamez, linenum, "", "$readmem file address beyond bounds of array"); } else { int entry = addr - array_lsb; QData shift = hex ? VL_ULL(4) : VL_ULL(1); // Shift value in if (width<=8) { CData* datap = &((CData*)(memp))[entry]; if (!innum) { *datap = 0; } *datap = ((*datap << shift) + value) & VL_MASK_I(width); } else if (width<=16) { SData* datap = &((SData*)(memp))[entry]; if (!innum) { *datap = 0; } *datap = ((*datap << shift) + value) & VL_MASK_I(width); } else if (width<=VL_WORDSIZE) { IData* datap = &((IData*)(memp))[entry]; if (!innum) { *datap = 0; } *datap = ((*datap << shift) + value) & VL_MASK_I(width); } else if (width<=VL_QUADSIZE) { QData* datap = &((QData*)(memp))[entry]; if (!innum) { *datap = 0; } *datap = ((*datap << (QData)(shift)) + (QData)(value)) & VL_MASK_Q(width); } else { WDataOutP datap = &((WDataOutP)(memp))[ entry*VL_WORDS_I(width) ]; if (!innum) { VL_ZERO_RESET_W(width, datap); } _VL_SHIFTL_INPLACE_W(width, datap, shift); datap[0] |= value; } if (value>=(1< len) { if (strp) delete [] strp; strp = new char[newlen]; len = newlen; } strcpy(strp,n1); strcat(strp,n2); return strp; } //=========================================================================== // VerilatedModule:: Methods VerilatedModule::VerilatedModule(const char* namep) : m_namep(strdup(namep)) { } VerilatedModule::~VerilatedModule() { if (m_namep) free((void*)m_namep); m_namep=NULL; } //===========================================================================