-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpu.c
146 lines (124 loc) · 2.63 KB
/
cpu.c
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
/* Check LICENSE file for copyright and license details. */
/*
* CPU module
*
* Implementation a MIPS processor
*/
#include <errno.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#ifdef INSTRDUMP
#include <err.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#endif
/* Implements */
#include "cpu.h"
CPU *CPU_create(uint32_t, uint32_t);
void CPU_destroy(CPU *);
int64_t CPU_mfc2(CPU *, uint32_t, uint32_t);
int CPU_mtc2(CPU *, uint32_t, uint32_t, uint32_t);
/*
* CPU_create: create CPU object
*
* ncpu: number of CPU to create
* pc: initial program counter
*
* Returns array with ncpu cpus if success, NULL otherwise, errno
* indicates the error. Each cpu on the array have its id on register K0.
*
* This function fails if:
* ENOMEM: Could not allocate CPU.
* ENOENT: Could not create debug file (if debuging is enabled).
*/
CPU *
CPU_create(uint32_t ncpu, uint32_t pc)
{
size_t i;
CPU *cpu;
errno = 0;
if (!(cpu = malloc(sizeof(CPU) * ncpu))) {
errno = ENOMEM;
return NULL;
}
memset(cpu, 0, sizeof(CPU) * ncpu);
for (i = 0; i < ncpu; ++i) {
cpu[i].pc = pc;
cpu[i].running = 1;
cpu[i].gpr[K0] = i;
}
#ifdef INSTRDUMP
snprintf(cpu->debug.fname, 20, "cpu%04d_instrdump", id);
if ((cpu->debug.fd = open(cpu->debug.fname,
O_CREAT | O_WRONLY | O_TRUNC,
S_IRUSR | S_IWUSR)) < 0) {
warn("CPU_create -- open %s", cpu->debug.fname);
errno = ENOENT;
return NULL;
}
#endif
return cpu;
}
/*
* CPU_destroy: free CPU object
*
* cpu: cpu object
*/
void
CPU_destroy(CPU *cpu)
{
#ifdef INSTRDUMP
if (close(cpu->debug.fd)) {
warn("CPU_destroy -- close %s", cpu->debug.fname);
}
#endif
free(cpu);
}
/*
* CPU_mfc2: move from coprocessor 2
*
* cpu: cpu object
* src: source register
* sel: select
*
* Returns the contents of copressor 2 register if success, -1 otherwise, errno
* indicates the error.
*
* This function fails if:
* EINVAL: Either src or sel are invalid values.
*/
int64_t
CPU_mfc2(CPU *cpu, uint32_t src, uint32_t sel)
{
errno = 0;
if (src >= COP2_NREG || sel >= COP2_NSEL) {
errno = EINVAL;
return -1;
}
return cpu->cop2[src][sel];
}
/*
* CPU_mtc2: Move to coprocessor 2
*
* cpu: cpu object
* dest: destination register
* sel: select
* val: value to insert
*
* Returns 0 if success, an error code otherwise.
*
* This function fails if:
* EINVAL: Either src or sel are invalid values.
*/
int
CPU_mtc2(CPU *cpu, uint32_t dest, uint32_t sel, uint32_t val)
{
errno = 0;
if (dest >= COP2_NREG || sel >= COP2_NSEL) {
return EINVAL;
}
cpu->cop2[dest][sel] = val;
return 0;
}