|
| 1 | +#include <stdio.h> |
| 2 | +#include <math.h> |
| 3 | +#include <stdlib.h> |
| 4 | + |
| 5 | +//clock cycles |
| 6 | +long long cycles = 0; |
| 7 | + |
| 8 | +// registers |
| 9 | +long long int regs[32]; |
| 10 | + |
| 11 | +// program counter |
| 12 | +unsigned long pc = 0; |
| 13 | + |
| 14 | +// memory |
| 15 | +#define INST_MEM_SIZE 32*1024 |
| 16 | +#define DATA_MEM_SIZE 32*1024 |
| 17 | +unsigned long inst_mem[INST_MEM_SIZE]; //instruction memory |
| 18 | +unsigned long long data_mem[DATA_MEM_SIZE]; //data memory |
| 19 | + |
| 20 | +//misc. function |
| 21 | +int init(char* filename); |
| 22 | + |
| 23 | +void print_cycles(); |
| 24 | +void print_reg(); |
| 25 | +void print_pc(); |
| 26 | + |
| 27 | +//fetch an instruction from a instruction memory |
| 28 | +void fetch() { |
| 29 | + |
| 30 | +} |
| 31 | + |
| 32 | +//decode the instruction and read data from register file |
| 33 | +void decode() { |
| 34 | + |
| 35 | +} |
| 36 | + |
| 37 | +//perform the appropriate operation |
| 38 | +void exe() { |
| 39 | + |
| 40 | +} |
| 41 | + |
| 42 | +//access the data memory |
| 43 | +void mem() { |
| 44 | + |
| 45 | +} |
| 46 | + |
| 47 | +//write result of arithmetic operation or data read from the data memory if required |
| 48 | +void wb() { |
| 49 | + |
| 50 | +} |
| 51 | + |
| 52 | +int main(int ac, char* av[]) |
| 53 | +{ |
| 54 | + |
| 55 | + if (ac < 3) |
| 56 | + { |
| 57 | + printf("./riscv_sim filename mode\n"); |
| 58 | + return -1; |
| 59 | + } |
| 60 | + |
| 61 | + |
| 62 | + char done = 0; |
| 63 | + if (init(av[1]) != 0) |
| 64 | + return -1; |
| 65 | + while (!done) |
| 66 | + { |
| 67 | + |
| 68 | + fetch(); |
| 69 | + decode(); |
| 70 | + exe(); |
| 71 | + mem(); |
| 72 | + wb(); |
| 73 | + |
| 74 | + |
| 75 | + cycles++; //increase clock cycle |
| 76 | + |
| 77 | + //if debug mode, print clock cycle, pc, reg |
| 78 | + if (*av[2] == '0') { |
| 79 | + print_cycles(); //print clock cycles |
| 80 | + print_pc(); //print pc |
| 81 | + print_reg(); //print registers |
| 82 | + } |
| 83 | + |
| 84 | + // check the exit condition, do not delete!! |
| 85 | + if (regs[9] == 10) //if value in $t1 is 10, finish the simulation |
| 86 | + done = 1; |
| 87 | + } |
| 88 | + |
| 89 | + if (*av[2] == '1') |
| 90 | + { |
| 91 | + print_cycles(); //print clock cycles |
| 92 | + print_pc(); //print pc |
| 93 | + print_reg(); //print registers |
| 94 | + } |
| 95 | + |
| 96 | + return 0; |
| 97 | +} |
| 98 | + |
| 99 | + |
| 100 | +/* initialize all datapat elements |
| 101 | +//fill the instruction and data memory |
| 102 | +//reset the registers |
| 103 | +*/ |
| 104 | +int init(char* filename) |
| 105 | +{ |
| 106 | + FILE* fp = fopen(filename, "r"); |
| 107 | + int i; |
| 108 | + long inst; |
| 109 | + |
| 110 | + if (fp == NULL) |
| 111 | + { |
| 112 | + fprintf(stderr, "Error opening file.\n"); |
| 113 | + return -1; |
| 114 | + } |
| 115 | + |
| 116 | + /* fill instruction memory */ |
| 117 | + i = 0; |
| 118 | + while (fscanf(fp, "%lx", &inst) == 1) |
| 119 | + { |
| 120 | + inst_mem[i++] = inst; |
| 121 | + } |
| 122 | + |
| 123 | + |
| 124 | + /*reset the registers*/ |
| 125 | + for (i = 0; i < 32; i++) |
| 126 | + { |
| 127 | + regs[i] = 0; |
| 128 | + } |
| 129 | + |
| 130 | + /*reset pc*/ |
| 131 | + pc = 0; |
| 132 | + /*reset clock cycles*/ |
| 133 | + cycles = 0; |
| 134 | + return 0; |
| 135 | +} |
| 136 | + |
| 137 | +void print_cycles() |
| 138 | +{ |
| 139 | + printf("---------------------------------------------------\n"); |
| 140 | + |
| 141 | + printf("Clock cycles = %lld\n", cycles); |
| 142 | +} |
| 143 | + |
| 144 | +void print_pc() |
| 145 | +{ |
| 146 | + printf("PC = %ld\n\n", pc); |
| 147 | +} |
| 148 | + |
| 149 | +void print_reg() |
| 150 | +{ |
| 151 | + printf("x0 = %d\n", regs[0]); |
| 152 | + printf("x1 = %d\n", regs[1]); |
| 153 | + printf("x2 = %d\n", regs[2]); |
| 154 | + printf("x3 = %d\n", regs[3]); |
| 155 | + printf("x4 = %d\n", regs[4]); |
| 156 | + printf("x5 = %d\n", regs[5]); |
| 157 | + printf("x6 = %d\n", regs[6]); |
| 158 | + printf("x7 = %d\n", regs[7]); |
| 159 | + printf("x8 = %d\n", regs[8]); |
| 160 | + printf("x9 = %d\n", regs[9]); |
| 161 | + printf("x10 = %d\n", regs[10]); |
| 162 | + printf("x11 = %d\n", regs[11]); |
| 163 | + printf("x12 = %d\n", regs[12]); |
| 164 | + printf("x13 = %d\n", regs[13]); |
| 165 | + printf("x14 = %d\n", regs[14]); |
| 166 | + printf("x15 = %d\n", regs[15]); |
| 167 | + printf("x16 = %d\n", regs[16]); |
| 168 | + printf("x17 = %d\n", regs[17]); |
| 169 | + printf("x18 = %d\n", regs[18]); |
| 170 | + printf("x19 = %d\n", regs[19]); |
| 171 | + printf("x20 = %d\n", regs[20]); |
| 172 | + printf("x21 = %d\n", regs[21]); |
| 173 | + printf("x22 = %d\n", regs[22]); |
| 174 | + printf("x23 = %d\n", regs[23]); |
| 175 | + printf("x24 = %d\n", regs[24]); |
| 176 | + printf("x25 = %d\n", regs[25]); |
| 177 | + printf("x26 = %d\n", regs[26]); |
| 178 | + printf("x27 = %d\n", regs[27]); |
| 179 | + printf("x28 = %d\n", regs[28]); |
| 180 | + printf("x29 = %d\n", regs[29]); |
| 181 | + printf("x30 = %d\n", regs[30]); |
| 182 | + printf("x31 = %d\n", regs[31]); |
| 183 | + printf("\n"); |
| 184 | +} |
0 commit comments