summaryrefslogtreecommitdiff
path: root/tb/top/fetch_test.cpp
blob: 06768c4e7b3846bd1250d30d463e66b7aa1bfcd6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <cstdio>

#include <verilated.h>
#include <verilated_vcd_c.h>

#include "Vfetch_test.h"               // From Verilating "top.v"

int main(int argc, char** argv) {
    Verilated::commandArgs(argc, argv);   // Remember args
	Verilated::traceEverOn(true);

	Vfetch_test top;
	VerilatedVcdC trace;

	top.trace(&trace, 0);
	trace.open("fetch_test.vcd");

    top.clk = 0;
    top.stall = 0;               //insn y insn_pc se detienen
    top.branch = 0;              //forma de flush -> instr saltan a la instr de la branch
    top.prefetch_flush = 0;      //limpia prefetch
    top.fetched = 1;             //estado del fetch (ready)
    top.wr_pc = 0;               //cuando hay un write al pc
    top.branch_target = 0;       //direccion a la que se hace salto
    top.wr_current = 0;          //ultimo que se guardo en registros
    top.fetch_data = 0x00000000; //data que se leyó al hacer fetch

    uint32_t rom[] =
    {
        0xea000032,
        0xea000034,
        0xea000000,
        0xeafffffe,
        0xeafffffe,
        0xeafffffe,
        0xeafffffe,
        0xe3500000,
        0xda000001,
        0xe3a00001,
        0xe1a0f00e,
        0x13e00000,
        0x03a00000,
        0xe1a0f00e,
        0xe1a02000,
        0xe92d4010,
        0xe1a00002,
        0xebfffff4,
        0xe1a03000,
        0xe1a00001,
        0xebfffff1,
        0xe0933000,
        0x0a00000c,
        0xe3730001,
        0x0a00000e,
        0xe3530001,
        0x0a00000a,
        0xe1a038a1,
        0xe1a00502,
        0xe1833601,
        0xe02023c2,
        0xe0831001,
        0xe1520001,
        0xcaffffed,
        0xe0820001,
        0xe8bd8010,
        0xe1a02822,
        0xe1822801,
        0xe1a00f62,
        0xe8bd8010,
        0xe3500000,
        0xda000002,
        0xe1a02001,
        0xe2611001,
        0xeafffff2,
        0xe1e02001,
        0xe1a00002,
        0xebffffd6,
        0xe3500000,
        0xdaffffed,
        0xe1a01002,
        0xeafffff5,
        0xe59fd008,
        0xebffffd7,
        0xeafffffe,
        0xeafffffe,
        0x20000000,
    };

    int clk_tick = 0;
	int time = 0;

    std::printf("CPU cycle:\n");
    while (time < 50) 
    {
        top.eval();
        trace.dump(time++);

        if(!top.clk)
        {
            std::printf("insn=0x%08x, insn_pc=0x%08x, addr=0x%08x\n", 
                    top.insn, top.insn_pc, top.addr);
        }

		top.clk = !top.clk;

        top.fetch_data = rom[top.addr];
    }

    std::printf("Branch, flush, stall:\n");
    while (time < 99) 
    {
        top.eval();
        trace.dump(time++);

        if(!top.clk)
        {
            std::printf("insn=0x%08x, insn_pc=0x%08x, addr=0x%08x\n", 
                    top.insn, top.insn_pc, top.addr);
        }

		top.clk = !top.clk;

        if(time == 55)
		{
            std::printf("Se hace un branch:\n");
			top.branch = 1;
            top.branch_target = 3;
		}

        if(time == 59)
		{
            std::printf("Se termina el branch:\n");
			top.branch = 0;

		}

        if(time == 63){
            std::printf("Se hace un flush:\n");
            top.branch = 0;
            top.branch_target = 0;
			top.prefetch_flush = 1;
		}

        if(time == 69){
            std::printf("Se termina el flush:\n");
			top.prefetch_flush = 0;
		}

        if(time == 75)
		{
            std::printf("Se hace un stall:\n");
			top.stall = 1;
		}

        if(time == 81)
		{
            std::printf("Se termina el stall:\n");
			top.stall = 0;
		}

        top.fetch_data = rom[top.addr];
    }

	trace.close();
    top.final();               // Done simulating
}