OpenCores
URL https://opencores.org/ocsvn/linkruncca/linkruncca/trunk

Subversion Repositories linkruncca

[/] [linkruncca/] [trunk/] [sim/] [sim_main.cpp] - Blame information for rev 6

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 6 jaytang
#include <verilated.h>          // Defines common routines
2
#include "VLinkRunCCA.h"        // From Verilator "Top.v"
3
#include <ctype.h>
4
#include <time.h>
5
#include "sim.h"
6
#include "ppm.h"
7
 
8
VLinkRunCCA *top;                      // Instantiation of module
9
unsigned int main_time = 0;     // Current simulation time
10
 
11
int x_bit=(int)ceil(log2(imwidth));
12
int y_bit=(int)ceil(log2(imheight));
13
int latency=4;
14
 
15
int main(int argc, char** argv) {
16
        clock_t time1,time2;
17
        time1=clock();
18
        Verilated::commandArgs(argc, argv);   // Remember args
19
        top = new VLinkRunCCA;             // Create instance
20
 
21
        FILE *finput,*foutput;
22
        int i,j,object=0,minx,maxx,miny,maxy;
23
        int m;int maskx=0,masky=0;
24
        for(m=0;m<y_bit;m=m+1) masky=(masky<<1)+1;
25
        for(m=0;m<x_bit;m=m+1) maskx=(maskx<<1)+1;
26
 
27
        unsigned long long int box;
28
        unsigned char *im;if(readPPM(fname,&im,&imwidth,&imheight))return 1; //read image
29
 
30
        //set original binary image to pixel 0 and 1
31
        for(i=0;i<imwidth;i++)
32
                for(j=0;j<imheight;j++)
33
                        if(im[i+j*imwidth]!=0)
34
                                im[i+j*imwidth]=1;
35
 
36
        //clear image border by setting them to 0
37
        i=0;          for(j=0;j<imheight;j++) im[i+j*imwidth]=0;
38
        i=imwidth-1;  for(j=0;j<imheight;j++) im[i+j*imwidth]=0;
39
        j=0;          for(i=0;i<imwidth;i++) im[i+j*imwidth]=0;
40
        j=imheight-1; for(i=0;i<imwidth;i++) im[i+j*imwidth]=0;
41
 
42
        //initialize input      
43
        unsigned int cycle=0;
44
        top->clk = 0;
45
        top->datavalid=0;
46
        top->rst=1;
47
        top->eval();            // Evaluate model
48
        top->rst = 0;     // Deassert reset
49
 
50
        while (!Verilated::gotFinish())
51
        {
52
                if(cycle==imwidth*imheight+latency-1)break; //simulation end when all pixel are send + latency
53
 
54
                top->datavalid=1;
55
                if(cycle<imwidth*imheight)top->pix_in=im[cycle];
56
                else top->pix_in=0;
57
 
58
                top->clk = 1;       // Toggle clock
59
                top->eval();            // Evaluate model
60
                top->clk = 0;
61
                top->eval();            // Evaluate model
62
 
63
                if(top->datavalid_out) //draw box and display output
64
                {
65
                        object++;
66
                        box=top->box_out;
67
                        maxy=masky&(box);box=box>>y_bit;
68
                        miny=masky&(box);box=box>>y_bit;
69
                        maxx=maskx&(box);box=box>>x_bit;
70
                        minx=maskx&(box);
71
                        printf("minx: %-5umaxx: %-5uminy: %-5umaxy: %-5u ..produced at cycle %u\n",minx,maxx,miny,maxy,cycle);  //Display
72
 
73
                        for(j=minx;j<=maxx;j++) //draw the box with pixel 2
74
                        {       im[j+miny*imwidth]=2;im[j+maxy*imwidth]=2;              }
75
                        for(j=miny;j<=maxy;j++)
76
                        {       im[minx+j*imwidth]=2;im[maxx+j*imwidth]=2;              }
77
                }
78
 
79
                cycle++;
80
        }
81
        top->final();  // Done simulating
82
 
83
        printf("Total object: %d\nTotal Cycle: %u\n",object,cycle);
84
 
85
        for(i=0;i<imwidth*imheight;i++) im[i]=im[i]*127; //increase intensity for viewing
86
        writePPM("box_out.pgm",im,imwidth,imheight,0);
87
 
88
        time2=clock();
89
        printf("Simulation time: %lums\n",(time2-time1)/1000);
90
}
91
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.