00001 #ifndef RVALS
00002 #define RVALS 2 // max bits in reg/wire divided by 32
00003 #endif
00004
00009
00010
00011 #ifndef _VLIB_H_FILE_
00012 #define _VLIB_H_FILE_
00013
00014 #include <iostream>
00015 #include <fstream>
00016 #include <sstream>
00017 #include <string>
00018 #include <time.h>
00019 #include <stdio.h>
00020
00021 #define MAXARG 2000
00022
00023 typedef unsigned long long int rval;
00024 #define Sizeofrval sizeof(rval)
00025
00026 using namespace std;
00027
00028 class module;
00029
00030 enum SignalMode
00031 {
00032 mnone = 0,
00033 mreg,
00034 mwire,
00035 minput,
00036 moutput,
00037 minout,
00038 mnum,
00039 mtemp
00040 };
00041
00042 class Signal
00043 {
00044 public:
00045 Signal ();
00046 Signal (rval);
00047 Signal (int);
00048 Signal (unsigned int);
00049 Signal (const char*);
00050 Signal (int bits, rval value);
00051 void create();
00052 void init (int, int, const char*);
00053 void init(const char* rname){init(0, 0, rname);};
00054 void init(Signal* shost, int h, int l, const char* rname);
00055 void makemask();
00056 #ifdef VGEN
00057 string& getname(){return name;};
00058 string& getorname(){return orname;};
00059 string& getcatname();
00060 void setname (string& rname){name = rname;};
00061 void setorname (string& rname){orname = rname;};
00062 void setbrackets(const char* l, const char* r){lb = l; rb = r;};
00063 #endif
00064 rval getr(){return r;};
00065 int getint(){return (unsigned int)r;};
00066 int getl(){return l;};
00067 int geth(){return h;};
00068 rval getmask(){return mask;};
00069 void setr(rval rv){r = rv & mask;};
00070 void setrc(rval rv){rc = rv & mask;};
00071 void sethlmask(int high, int low, rval imask){h = high; l = low; mask = imask;};
00072
00073 bool getbool(){return (getval() != 0);};
00074 int getposedge(){return pedge;};
00075 int getnegedge(){return nedge;};
00076 int getchange(){return change;};
00077 void setchange(int ch){change = ch;};
00078 void setposedge(int ch){pedge = ch;};
00079 void setnegedge(int ch){nedge = ch;};
00080 void setalwaysn(int n) {alwaysn = (n == -1) ? 0 : n;};
00081 int getalwaysn(){return alwaysn;};
00082 void setprintable(int p){printable = p;};
00083 rval getval();
00084
00085 Signal set(Signal);
00086 Signal asgn(Signal);
00087 Signal operator= (Signal);
00088
00089 Signal operator+ (Signal);
00090 Signal operator- (Signal);
00091 Signal operator* (Signal);
00092 Signal operator/ (Signal);
00093 Signal operator% (Signal);
00094 Signal operator^ (Signal);
00095 Signal operator>> (Signal);
00096 Signal operator<< (Signal);
00097 Signal operator& (Signal);
00098 Signal operator&& (Signal);
00099 Signal operator| (Signal);
00100 Signal operator|| (Signal);
00101
00102 Signal operator< (Signal);
00103 Signal operator> (Signal);
00104 Signal operator<= (Signal);
00105 Signal operator>= (Signal);
00106 Signal operator== (Signal);
00107 Signal operator!= (Signal);
00108
00109 Signal operator! ();
00110 Signal operator~ ();
00111 Signal* operator& ();
00112 Signal operator++ (){return *this = *this + 1;};
00113 Signal operator-- (){return *this = *this - 1;};
00114 Signal operator++ (int){return *this = *this + 1;};
00115 Signal operator-- (int){return *this = *this - 1;};
00116
00117 friend Signal ror (Signal);
00118 friend Signal rxor (Signal);
00119 friend Signal rand (Signal);
00120
00121
00122 Signal operator()(Signal, Signal);
00123 Signal operator()(Signal);
00124
00125 Signal operator,(Signal);
00126
00127 friend ostream &operator<<(ostream &stream, Signal s);
00128
00129 void input (int, int, const char*);
00130 void input (const char* rname){input(0, 0, rname);};
00131 void clock (const char* rname);
00132
00133 void output (int, int, const char*);
00134 void output (const char* rname){output(0, 0, rname);};
00135 void output (int high, int low, const char* rname, module* parent);
00136 void output (const char* rname, module* parent);
00137
00138 void inout (int, int, const char*);
00139 void inout (const char* rname){inout(0, 0, rname);};
00140
00141 void reg (int, int, const char*);
00142 void initreg (int, int, const char*);
00143 void reg (const char* rname){reg(0, 0, rname);};
00144
00145 void wire (int, int, const char*);
00146 void wire (int, int, const char*, int);
00147 void wire (const char* rname){wire(0, 0, rname);};
00148
00149
00150 protected:
00151 rval r;
00152 rval rc;
00153 int h, l;
00154 int hostl;
00155 rval mask;
00156 #ifdef VGEN
00157 string name;
00158 string orname;
00159 string lb, rb;
00160 string obname;
00161 string catname;
00162 #endif
00163 Signal* host;
00164 Signal *ca1;
00165 Signal *ca2;
00166 int pedge, nedge, change;
00167 Signal* source;
00168 Signal* outhost;
00169 Signal* outreg;
00170 int alwaysn;
00171 int inited;
00172 int printable;
00173 int mode;
00174 };
00175
00176
00177 class parameter : public Signal
00178 {
00179 public:
00180 parameter (const char* rname, Signal arg);
00181 operator int(){return (unsigned int)r;};
00182 protected:
00183 void init (int, int, const char*);
00184 void operator= (Signal arg);
00185 };
00186
00187
00188
00189 class memory
00190 {
00191 public:
00192 memory (){r = NULL;};
00193 ~memory ();
00194 void reg (int, int, int, int, const char*);
00195 void reg (int nup, int ndown, const char* rname) {reg (0, 0, nup, ndown, rname);};
00196 #ifdef VGEN
00197 Signal operator[] (Signal);
00198 #else
00199 Signal& operator[] (Signal);
00200 #endif
00201
00202 protected:
00203 #ifdef VGEN
00204 string name;
00205 #endif
00206 Signal* r;
00207 int up, down;
00208
00209 };
00210
00211 class module
00212 {
00213 public:
00214 module ();
00215 virtual ~module ();
00216 void create();
00217 void vendmodule();
00218 void vbeginmodule();
00219
00220 virtual void operator()(){};
00221
00222 void init (const char*, const char*);
00223 void init (const char*, const char*, module* fixt);
00224 void init (const char*, const char*, int);
00225 Signal posedge (Signal);
00226 Signal negedge (Signal);
00227 void pushswitch (Signal arg){switcharg[switchn] = arg; switchn++;};
00228 Signal getswitch (){return switcharg[switchn - 1];};
00229 void popswitch () {switchn--;}
00230 Signal* AddOutReg(Signal arg);
00231 Signal ifelse(Signal, Signal, Signal);
00232 void setchange(int c){change = c;}
00233 int getchange(){return change;}
00234 #ifdef VGEN
00235 void PrintHeader();
00236 #endif
00237 protected:
00238 #ifdef VGEN
00239 string name;
00240 streambuf *outbuf;
00241 ofstream vfile;
00242 #endif
00243 string instname;
00244 int OuterIndPos;
00245 Signal switcharg[10];
00246 int switchn;
00247 int oldenmarg;
00248 Signal* outreg[1000];
00249 int outregn;
00250 void (*runperiod)();
00251 module* tfixt;
00252 int change;
00253 int itern;
00254 rval passn;
00255
00256 };
00257
00258
00259 class function : public module
00260 {
00261 public:
00262 function(){};
00263 void init (int, int, const char*);
00264 void vendfunction();
00265 void vbeginfunction();
00266 void makemask(int hpar, int lpar);
00267 protected:
00268 #ifdef VGEN
00269 string retname;
00270 #endif
00271 int h, l;
00272 rval mask;
00273 int OldChange;
00274 Signal result;
00275 };
00276
00277 #define MAXSTIM 10000
00278
00279 class globcontrol
00280 {
00281 public:
00282 globcontrol();
00283 #ifdef VGEN
00284 void AddParameter(string ln) {pars[npar] = ln; npar++;};
00285 void AddDeclarator(string ln){decls[ndecl] = ln; ndecl++;};
00286 void AddComment(string ln){ln += "\n"; decls[ndecl] = ln; ndecl++;};
00287 void AddIO(string ln);
00288 void Print ();
00289 string& PrintIO(bool col);
00290 void Indent(){indpos++; PrepMargin();};
00291 void Outdent(){indpos--; PrepMargin();};
00292 string& getmargin(){int t = nomargin; nomargin = 0; return (t) ? zeromargin : margin;};
00293 int getpos(){return indpos;};
00294 void setpos(int pos){indpos = pos; PrepMargin();};
00295 void setprintassign(int y){pa = y;};
00296 int printassign(){return pa;};
00297 void enablemargin (int i){nomargin = !i;};
00298 int getenablemargin (){return !nomargin;};
00299 void setFileOpen(int fo){VFileOpen = fo;};
00300 int getFileOpen(){return VFileOpen;};
00301 #endif
00302 void alwaysstart() {alwayscnt = 0; alwaysn++; if (alwaysn == 0 || alwaysn == -1) alwaysn = 1;};
00303 void alwayspush() {if (alwayscnt >= 0) alwayscnt++;};
00304 void alwayspop() {if (alwayscnt > 0) alwayscnt--; if (alwayscnt == 0) alwayscnt = -1;};
00305 int alwaysget() {return (alwayscnt > 0);};
00306 int getalwaysn() {return (alwayscnt > 0) ? alwaysn : (-1);};
00307
00308 void setchange(int i);
00309 int getchange(){return change;};
00310 void setparent(module *rparent){parent = rparent;};
00311 module* getparent(){return parent;};
00312 void setfunction(int i){functiondecl = i;};
00313 int getfunction(){int t = functiondecl; functiondecl = 0; return t;};
00314 int getce(){return ce;};
00315 int setce(int c){ce = c; return 1;};
00316 char* constant(int bits, char* val);
00317 char* constant(int bits, int val);
00318 void passn_inc(){passn++;};
00319 rval getpassn(){return passn;}
00320
00321 protected:
00322 #ifdef VGEN
00323 string pars[MAXARG];
00324 int npar;
00325
00326 string decls[MAXARG];
00327 int ndecl;
00328
00329 string dios[MAXARG];
00330 int ndio;
00331
00332 int indpos;
00333 string margin, zeromargin;
00334 void PrepMargin();
00335 int nomargin;
00336
00337 int pa;
00338 string outln;
00339 #endif
00340 int functiondecl;
00341
00342 int alwayscnt;
00343 int alwaysn;
00344
00345 int change;
00346 module* parent;
00347
00348 int VFileOpen;
00349 int ce;
00350 char constring[RVALS*32+32];
00351 rval passn;
00352
00353 };
00354
00355 #ifdef VGEN
00356 #define beginsystem
00357 #define endsystem exit(0);
00358 #else
00359 #define beginsystem glc.setce(0); glc.passn_inc(); do { glc.setchange(0);
00360 #define endsystem } while (glc.getchange() ? 1 : glc.getce() ? 0 : glc.setce(1));
00361 #endif
00362
00363
00364
00365 #endif