⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.cpp

📁 改进的基于6个mips核的NOC网络
💻 CPP
字号:
#include "../mips.h"#include "cache.h"#include "cache_memif.h"#include <list>#include <iomanip>#include <string>#define NOVCD/*void TICK(int t){  for(int i=0; i<t; i++){    clk = 0;    sc_cycle(5);    waited = memwait.read();    clk = 1;    sc_cycle(1);    if(!waited) { addr = 0x0; w = 0; r = 0; din = 0; }    sc_cycle(4);  }}*/struct command {  int nops;  unsigned addr, data, r, w;};struct result {  int issue;  bool nop;  int addr;  unsigned out, data;  result( int i=0, bool n=false, unsigned d=0, int a=-1, unsigned o=0 ):issue(i),nop(n),addr(a),out(o),data(d){}};SC_MODULE(REG){  sc_in< sc_bv<32> > in;  sc_out< sc_bv<32> > out;  sc_in<bool> clk;    void r(){    out.write(in.read());  }    SC_CTOR(REG){    SC_METHOD(r);    sensitive_pos << clk;  }};bool check( list<result>& results, unsigned out ){  if( !results.empty() ){    result res = *results.begin();    results.pop_front();    if( !res.nop && res.out != (unsigned)-1 ){      cout << "\t#" << res.issue << " out=0x" << hex << out << " (0x" << hex << res.out << dec << ") @ " << res.addr << endl;      if( out != res.out ) throw string();      return true;    }    }  return false;}int sc_main(int argc, char *argv[]){  sc_trace_file *tf = sc_create_vcd_trace_file("trace");  CACHE_MEMIF memif("memif");  DATA_CACHE data_cache("data_cache");    sc_signal<bool> clk;  sc_signal< sc_bv<DWORD> > addr;  sc_signal< sc_bv<DWORD> > din;  sc_signal< sc_bv<W_MEMWRITE> > w;  sc_signal< sc_bv<W_MEMREAD> > r;  sc_signal< bool > memwait;  sc_signal< sc_bv<DWORD> > dout;  sc_signal< sc_bv<1> > en;  sc_signal< sc_bv<32> > mem_addr;  sc_signal< sc_bv<32> > mem_din;  sc_signal< sc_bv<32> > mem_dout;  sc_signal< bool > mem_ww, mem_wb;  sc_signal< bool > mem_r;  sc_signal< bool > mem_rdy;  data_cache.addr(addr);  data_cache.din(din);  data_cache.dout(dout);  data_cache.memwait(memwait);  data_cache.w(w);  data_cache.r(r);  data_cache.clk(clk);  data_cache.en(en);  data_cache.mem_addr(mem_addr);  data_cache.mem_din(mem_din);  data_cache.mem_dout(mem_dout);  data_cache.mem_ww(mem_ww);  data_cache.mem_wb(mem_wb);  data_cache.mem_r(mem_r);  data_cache.mem_rdy(mem_rdy);#ifndef NOVCD  sc_trace(tf, addr, "addr");  sc_trace(tf, din, "din");  sc_trace(tf, dout, "dout");  sc_trace(tf, memwait, "memwait");  sc_trace(tf, w, "w");  sc_trace(tf, r, "r");  sc_trace(tf, clk, "clk");  sc_trace(tf, en, "en");  sc_trace(tf, mem_addr, "mem_addr");  sc_trace(tf, mem_din, "mem_din");  sc_trace(tf, mem_dout, "mem_dout");  sc_trace(tf, mem_ww, "mem_ww");  sc_trace(tf, mem_wb, "mem_wb");  sc_trace(tf, mem_r, "mem_r");  sc_trace(tf, mem_rdy, "mem_rdy");  sc_trace(tf, data_cache.addr_reg, "data_cache.addr_reg");  sc_trace(tf, data_cache.din_reg, "data_cache.din_reg");  sc_trace(tf, data_cache.addr_current, "data_cache.addr_current");  sc_trace(tf, data_cache.data_current, "data_cache.data_current");    sc_trace(tf, data_cache.data_mem_in, "data_cache.data_mem_in");  sc_trace(tf, data_cache.valid_select, "data_cache.valid_select");  sc_trace(tf, data_cache.data_fetch_out, "data_cache.data_fetch_out");  sc_trace(tf, data_cache.data_mem_out, "data_cache.data_mem_out");  sc_trace(tf, data_cache.tag_in, "data_cache.tag_in");  sc_trace(tf, data_cache.tag_out, "data_cache.tag_out");  sc_trace(tf, data_cache.tag_reg, "data_cache.tag_reg");  sc_trace(tf, data_cache.index, "data_cache.index");  sc_trace(tf, data_cache.index_reg, "data_cache.index_reg");  sc_trace(tf, data_cache.offset, "data_cache.offset");  sc_trace(tf, data_cache.offset_reg, "data_cache.offset_reg");  sc_trace(tf, data_cache.byte, "data_cache.byte");  sc_trace(tf, data_cache.byte_reg, "data_cache.byte_reg");  sc_trace(tf, data_cache.cache_en, "data_cache.cache_en");  sc_trace(tf, data_cache.cache_we, "data_cache.cache_we");  sc_trace(tf, data_cache.valid, "data_cache.valid");  sc_trace(tf, data_cache.fetch_word, "data_cache.fetch_word");  sc_trace(tf, data_cache.fetch_word_rdy, "data_cache.fetch_word_rdy");  sc_trace(tf, data_cache.rewrite, "data_cache.rewrite");  sc_trace(tf, data_cache.byte_rpl, "data_cache.byte_rpl");  sc_trace(tf, data_cache.current_reg, "data_cache.current_reg");  sc_trace(tf, data_cache.miss_ctrl->current_state, "data_cache.miss_ctrl.current_state");  sc_trace(tf, data_cache.miss_ctrl->operation, "data_cache.miss_ctrl.operation");  sc_trace(tf, data_cache.miss_ctrl->waiting, "data_cache.miss_ctrl.waiting");  sc_trace(tf, data_cache.miss_ctrl->miss, "data_cache.miss_ctrl.miss");  sc_trace(tf, data_cache.miss_ctrl->next_fetch_word, "data_cache.miss_ctrl.next_fetch_word");  sc_trace(tf, data_cache.miss_ctrl->word_inc, "data_cache.miss_ctrl.word_inc");  sc_trace(tf, data_cache.miss_ctrl->word_clr, "data_cache.miss_ctrl.word_clr");  sc_trace(tf, data_cache.miss_ctrl->start_write, "data_cache.miss_ctrl.start_write");  sc_trace(tf, data_cache.miss_ctrl->write_in_progress, "data_cache.miss_ctrl.write_in_progress");    char buf[256];  for( int i = 0 ; i < 256 ; i++ )    {      sprintf(buf, "data_cache.memory.block%02d_tag", i);      sc_trace(tf, data_cache.memory->tags[i], buf );      sprintf(buf, "data_cache.memory.block%02d_valid", i);      sc_trace(tf, data_cache.memory->valid_bits[i], buf );      sprintf(buf, "data_cache.memory.block%02d_w0", i);      sc_trace(tf, data_cache.memory->memW0[i], buf );      sprintf(buf, "data_cache.memory.block%02d_w1", i);      sc_trace(tf, data_cache.memory->memW1[i], buf );      sprintf(buf, "data_cache.memory.block%02d_w2", i);      sc_trace(tf, data_cache.memory->memW2[i], buf );      sprintf(buf, "data_cache.memory.block%02d_w3", i);      sc_trace(tf, data_cache.memory->memW3[i], buf );    }  unsigned addresses [] = { (unsigned)-1 };  for( unsigned *ap = addresses; *ap != (unsigned)-1; ++ap )    {      sprintf(buf, "memif.memory(%05d)", (*ap));      sc_trace(tf, memif.memory[*ap/4], buf );      cout << "addr " << *ap << " 0x" << hex << *ap << dec << " block " << ((*ap&0xfff)>>4) << endl;    }#endif  memif.clk(clk);  memif.addr(mem_addr);  memif.din(mem_din);  memif.dout(mem_dout);  memif.ww(mem_ww);  memif.wb(mem_wb);  memif.r(mem_r);  memif.rdy(mem_rdy);  sc_bv<32> memory_image[16384];  for( int i = 0 ; i < 16384; i++ ) {    unsigned u = rand();    memory_image[i] = u;    memif.memory[i] = u;  }  sc_initialize();  clk = 0; sc_cycle(5); clk = 1; sc_cycle(5);  en = 1;  clk = 0; sc_cycle(5); clk = 1; sc_cycle(1);    int op = atoi(argv[1]);  bool freeze=false, f=false;    bool nop;  int exp_addr;  unsigned exp_out, exp_data;  list<result> results;  result res;    int issued = 0;  while( op>issued || !results.empty() )    {      unsigned rw = rand()%8;      unsigned a = rand() & 0xffff;      unsigned d = rand();            unsigned ur=0, uw=0;      switch(rw){      case 1 :       case 2 :        ur=1; break;      case 3 :         ur=2; break;      case 4 :      case 5 :        uw=1; break;      case 6 :      case 7 :        uw=2; break;      }      try{         freeze = true; f = false;        while( freeze ){          sc_cycle(4); clk = 0; sc_cycle(5);           freeze = memwait.read();          if(!freeze && issued>1) check( results, dout.read().to_uint() );          clk = 1; sc_cycle(1);                }      }      catch(...){        cout << "ERROR\n";        break;      }                    if( issued<op )        {//           if( rand()%4 == 0 )//             {//               en = 0;//               results.push_back( *results.rbegin() );//             }//           else //             {              en = 1;              cout << "issue " << issued                    << " r=" <<  ur << " w=" << uw                   << " addr=" << a << " (0x" << hex << a << ") din=0x" << hex << d << dec                    << " @ " << sc_time_stamp() << endl;              addr = a; din = d; r = ur; w = uw;              nop = false;              if( ur==1 ){                exp_addr = a; exp_data = 0;                exp_out = memory_image[ a>>2 ].to_uint();              }              else if( ur==2 ){                exp_addr = a; exp_data = 0;                sc_bv<32> m = memory_image[ a>>2 ];                switch( a%4 ){                case 0 : break;                case 1 : m.range(31,24) = m.range(23,16); break;                case 2 : m.range(31,24) = m.range(15,8); break;                case 3 : m.range(31,24) = m.range(7,0); break;                }                exp_out = m.to_uint();              }              else if( uw==1 ){                exp_addr = a>>2; exp_data = d; exp_out = (unsigned)-1;                memory_image[exp_addr] = exp_data;              }              else if( uw==2 ){                exp_addr = a>>2; exp_out = (unsigned)-1;                sc_bv<32> m = memory_image[ a>>2 ];                switch( a%4 ){                case 0 : m.range(31,24) = sc_bv<32>(d).range(7,0); break;                case 1 : m.range(23,16) = sc_bv<32>(d).range(7,0); break;                case 2 : m.range(15,8) = sc_bv<32>(d).range(7,0); break;                case 3 : m.range(7,0) = sc_bv<32>(d).range(7,0); break;                }                exp_data = m.to_uint();                        memory_image[exp_addr] = exp_data;              }              else                nop = true;                    results.push_back( result(issued,nop,exp_data,exp_addr,exp_out) );              issued++;            }      //        }                }  //   for( int i = 0 ; i < 32 ; i+=4 )//     {//       sprintf(buf, "memif.memory(%04d)", i);//       sc_trace(tf, memif.memory[i/4], buf );//       sprintf(buf, "memif.memory(%04d)", i+4096);//       sc_trace(tf, memif.memory[1024+i/4], buf );//     }//   memif.memory[0] = sc_bv<32>( 0x00001111 );//   memif.memory[1] = sc_bv<32>( 0x22223333 );//   memif.memory[2] = sc_bv<32>( 0x44445555 );//   memif.memory[3] = sc_bv<32>( 0x66667777 );//   memif.memory[4] = sc_bv<32>( 0x88889999 );//   memif.memory[5] = sc_bv<32>( 0xaaaabbbb );//   memif.memory[6] = sc_bv<32>( 0xccccdddd );//   memif.memory[7] = sc_bv<32>( 0xeeeeffff );//   memif.memory[1024] = sc_bv<32>( 0x12121212 );//   memif.memory[1025] = sc_bv<32>( 0x34343434 );//   memif.memory[1026] = sc_bv<32>( 0x56565656 );//   memif.memory[1027] = sc_bv<32>( 0x78787878 );//   memif.memory[1028] = sc_bv<32>( 0x90909090 );//   memif.memory[1029] = sc_bv<32>( 0xabababab );//   memif.memory[1030] = sc_bv<32>( 0xcdcdcdcd );//   memif.memory[1031] = sc_bv<32>( 0xefefefef );//   command pipeline[] = {//     { 0, 7, 0, 2, 0 },//     { 0, 8, 0xfedcba90, 0, 2},//     { 0, 8, 0, 1, 0},//     { 0, 12, 0, 1, 0},//     { 0, 20, 0, 1, 0},//     { -1, 0, 0, 0, 0 }//   };  //   command pipeline[] = {    //     { 0, 0xC, 0, 1, 0 },//     { 0, 0x7, 0xfedcba90, 0, 2 },//     { 0, 0x8, 0x01234567, 0, 1 },//     { 0, 0x1008, 0x13579bdf, 0, 1 },//     { 0, 0xC, 0xabcdef78, 0, 1 },//     { 0, 0x100C, 0x123654ab, 0, 2 },//     { 0, 0x8, 0, 1, 0 },//     { 0, 0x100F, 0, 2, 0 },//     { -1, 0, 0, 0, 0 }//   };//   command pipeline[] = {    //     { 0, 0x8, 0x01234567, 0, 1 },//     { 0, 0x8, 0, 1, 0 },//     { -1, 0, 0, 0, 0 }//   };  //   sc_initialize();  //   clk = 0; sc_cycle(5); clk = 1; sc_cycle(5);//   en = 1;//   clk = 0; sc_cycle(5); clk = 1; sc_cycle(5);  //   clk = 0; sc_cycle(5); clk = 1; sc_cycle(1);//   bool freeze=false;//   int time=-1;//   if( argc > 1 ) time = atoi(argv[1]);  //   for( command *cmd = pipeline; cmd->nops != -1; ++cmd )//     {//       while( cmd->nops -- ) {//         cout << "issue nop " << " @ " << sc_time_stamp() << endl;//         addr = 0; din = 0; r = 0; w = 0;//         sc_cycle(4); clk = 0; sc_cycle(5); //         freeze = memwait.read();//         clk = 1; sc_cycle(1);//       }//       while( freeze ){//         cout << "frozen " << " @ " << sc_time_stamp() << endl;//         sc_cycle(4); clk = 0; sc_cycle(5); //         freeze = memwait.read();//         clk = 1; sc_cycle(1);        //       }//       cout << "issue " //            << "addr=0x" << hex << cmd->addr << " din=0x" << hex << cmd->data << dec //            << " r=" <<  cmd->r << " w=" << cmd->w//            << " @ " << sc_time_stamp() << endl;//       addr = cmd->addr; din = cmd->data; r = cmd->r; w = cmd->w;//       sc_cycle(4); clk = 0; sc_cycle(5); //       freeze = memwait.read();//       clk = 1; sc_cycle(1);        //     }      //   int cnt = 5;//   while( freeze || cnt-- ){//     if( !freeze ) { addr = 0x0; w = 0; r = 0; din = 0; }//     sc_cycle(4); clk = 0; sc_cycle(5); //     freeze = memwait.read();//     clk = 1; sc_cycle(1);        //   }  cout << "Memory test ";  for( int i = 0 ; i < 16384; i++ ) {    if( i%100 == 0 ) cout << '.' << flush;    if( memory_image[i] != memif.memory[i] )      {        cout << endl << "MEMORY[ " << setw(5) << i*4 << " ] = 0x"              << setfill('0') << hex << setw(8) << memif.memory[i].to_uint()             << " exp. 0x" << setfill('0') << hex << setw(8) << memory_image[i].to_uint() << dec << endl;      }  }  cout << "done\n";    sc_close_vcd_trace_file(tf);  return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -