1 | /* Author : Rick van der Zwet
|
---|
2 | * S-number : 0433373
|
---|
3 | * Version : $Id: control.c 366 2007-12-04 18:29:07Z rick $
|
---|
4 | * Copyright : FreeBSD Licence
|
---|
5 | * Description : Memory unit
|
---|
6 | */
|
---|
7 |
|
---|
8 | #include <stdlib.h>
|
---|
9 | #include <stdio.h>
|
---|
10 | #include <sysexits.h>
|
---|
11 | #include "control.h"
|
---|
12 | #include "memory.h"
|
---|
13 | #include "datapath.h"
|
---|
14 |
|
---|
15 | /* fcode, ocode, target */
|
---|
16 | #define DECODE2_SIZE 17
|
---|
17 | const decode_t decode2[DECODE2_SIZE] = {
|
---|
18 | { 8, 0, 18}, /* ADDI */
|
---|
19 | { 9, 0, 18}, /* ADDIU */
|
---|
20 | {10, 0, 39}, /* SLTI */
|
---|
21 | {11, 0, 39}, /* SLTIU */
|
---|
22 | {12, 0, 19}, /* ANDI */
|
---|
23 | {13, 0, 21}, /* ORI */
|
---|
24 | {14, 0, 23}, /* XORI */
|
---|
25 | {15, 0, 36}, /* LUI */
|
---|
26 | {32, 0, 59}, /* LB */
|
---|
27 | {33, 0, 65}, /* LH */
|
---|
28 | {35, 0, 71}, /* LW */
|
---|
29 | {36, 0, 62}, /* LBU */
|
---|
30 | {37, 0, 68}, /* LHU */
|
---|
31 | {40, 0, 44}, /* SB */
|
---|
32 | {41, 0, 50}, /* SH */
|
---|
33 | {43, 0, 56}, /* SW */
|
---|
34 | };
|
---|
35 |
|
---|
36 | /* fcode, ocode, target */
|
---|
37 | #define DECODE3_SIZE 20
|
---|
38 | const decode_t decode3[DECODE3_SIZE] = {
|
---|
39 | { 0, 0, 27}, /* SLL */
|
---|
40 | { 2, 0, 28}, /* SRL */
|
---|
41 | { 3, 0, 29}, /* SRA */
|
---|
42 | { 4, 0, 30}, /* SLLV */
|
---|
43 | { 6, 0, 32}, /* SRLV */
|
---|
44 | { 7, 0, 34}, /* SRAV */
|
---|
45 | { 8, 0, 42}, /* JR */
|
---|
46 | { 9, 0, 37}, /* JALR */
|
---|
47 | {13, 0, 43}, /* BREAK */
|
---|
48 | {32, 0, 18}, /* ADD */
|
---|
49 | {33, 0, 18}, /* ADDU */
|
---|
50 | {34, 0, 26}, /* SUB */
|
---|
51 | {35, 0, 26}, /* SUBU */
|
---|
52 | {36, 0, 20}, /* AND */
|
---|
53 | {37, 0, 22}, /* OR */
|
---|
54 | {38, 0, 24}, /* XOR */
|
---|
55 | {39, 0, 25}, /* NOR */
|
---|
56 | {42, 0, 39}, /* SLT */
|
---|
57 | {43, 0, 39}, /* SLTU */
|
---|
58 | };
|
---|
59 |
|
---|
60 | /*
|
---|
61 | * address, s1, s2, mux, constant, imm, alu, memory, a, b, c, temp, temp2, pc,
|
---|
62 | * mar, mdr, ir, reg, register_address_a, register_address_b,
|
---|
63 | * register_address_c, cond, jump
|
---|
64 | * XXX: Needs more pretty writing, the enums defined should be used
|
---|
65 | * XXX: More flexible input
|
---|
66 | */
|
---|
67 | microcode_t microprogram[75] = {
|
---|
68 | { 0, {5, 0, 0, 0, 0, 5, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0}, 2, 0},
|
---|
69 | { 1, {5, 8, 0, 4, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 3, 0, 0, 0}, 3, 0},
|
---|
70 | { 2, {1, 9, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0}, 0, 0},
|
---|
71 | { 3, {6, 8, 0, 3, 0, 2, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 0, 0},
|
---|
72 | { 4, {6, 8, 0,12, 0, 2, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0}, 0, 0},
|
---|
73 | { 5, {0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0}, 2, 5},
|
---|
74 | { 6, {0, 0, 0, 0, 0, 0, 0, 0, 0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0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 0, 0},
|
---|
88 | {20, {1, 3, 0, 0, 0, 2, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 1,72},
|
---|
89 | {21, {0, 9, 0, 0, 4, 6, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 0, 0},
|
---|
90 | {22, {1, 3, 0, 0, 0, 4, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 1,72},
|
---|
91 | {23, {0, 9, 0, 0, 4, 6, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 0, 0},
|
---|
92 | {24, {1, 3, 0, 0, 0,11, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 1,72},
|
---|
93 | {25, {1, 3, 0, 0, 0, 3, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 1,72},
|
---|
94 | {26, {1, 3, 0, 0, 0,10, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 1,72},
|
---|
95 | {27, {3, 9, 0, 0, 2, 7, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 1,72},
|
---|
96 | {28, {3, 9, 0, 0, 2, 9, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 1,72},
|
---|
97 | {29, {3, 9, 0, 0, 2, 8, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 1,72},
|
---|
98 | {30, {1, 8, 0, 8, 0, 2, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0}, 0, 0},
|
---|
99 | {31, {3, 4, 0, 0, 0, 7, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 1,72},
|
---|
100 | {32, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 0, 0},
|
---|
101 | {33, {3, 4, 0, 0, 0, 9, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 1,72},
|
---|
102 | {34, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 0, 0},
|
---|
103 | {35, {3, 4, 0, 0, 0, 8, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 1,72},
|
---|
104 | {36, {3, 8, 0, 6, 0, 7, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 1,72},
|
---|
105 | {37, {5, 8, 0, 5, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 0, 0},
|
---|
106 | {38, {1, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0}, 1,72},
|
---|
107 | {39, {1, 3, 0, 0, 0,10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 7,41},
|
---|
108 | {40, {0, 8, 0, 0, 0, 6, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 1,72},
|
---|
109 | {41, {0, 8, 0, 1, 0, 6, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 1,72},
|
---|
110 | {42, {1, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0}, 1, 0},
|
---|
111 | {43, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 1,43},
|
---|
112 | {44, {3, 8, 0, 3, 0, 7, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0}, 0, 0},
|
---|
113 | {45, {2, 8, 0, 7, 0, 7, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 0, 0},
|
---|
114 | {46, {3, 4, 0, 0, 0, 9, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 0, 0},
|
---|
115 | {47, {8, 4, 0,11, 0, 9, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0}, 0, 0},
|
---|
116 | {48, {7, 4, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0}, 0, 0},
|
---|
117 | {49, {3, 7, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0}, 1,57},
|
---|
118 | {50, {3, 8, 0, 3, 0, 7, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0}, 0, 0},
|
---|
119 | {51, {2, 8, 0, 6, 0, 7, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 0, 0},
|
---|
120 | {52, {3, 4, 0, 0, 0, 9, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 0, 0},
|
---|
121 | {53, {8, 4, 0,10, 0, 9, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0}, 0, 0},
|
---|
122 | {54, {7, 4, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0}, 0, 0},
|
---|
123 | {55, {3, 7, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0}, 1,57},
|
---|
124 | {56, {2, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0}, 0, 0},
|
---|
125 | {57, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 2,57},
|
---|
126 | {58, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 1, 0},
|
---|
127 | {59, {3, 8, 0, 3, 0, 7, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 0, 0},
|
---|
128 | {60, {7, 3, 0, 0, 0, 7, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 0, 0},
|
---|
129 | {61, {3, 8, 0, 7, 0, 8, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 1,72},
|
---|
130 | {62, {3, 8, 0, 3, 0, 7, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 0, 0},
|
---|
131 | {63, {7, 3, 0, 0, 0, 7, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 0, 0},
|
---|
132 | {64, {3, 8, 0, 7, 0, 9, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 1,72},
|
---|
133 | {65, {3, 8, 0, 3, 0, 7, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 0, 0},
|
---|
134 | {66, {7, 3, 0, 0, 0, 7, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 0, 0},
|
---|
135 | {67, {3, 8, 0, 6, 0, 8, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 1,72},
|
---|
136 | {68, {3, 8, 0, 3, 0, 7, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 0, 0},
|
---|
137 | {69, {7, 3, 0, 0, 0, 7, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 0, 0},
|
---|
138 | {70, {3, 8, 0, 6, 0, 9, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 1,72},
|
---|
139 | {71, {7, 0, 0, 0, 0, 5, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 0, 0},
|
---|
140 | {72, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 8,74},
|
---|
141 | {73, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0}, 1, 0},
|
---|
142 | {74, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0}, 1, 0},
|
---|
143 | };
|
---|
144 |
|
---|
145 | void
|
---|
146 | control_init()
|
---|
147 | {
|
---|
148 | int i = 0; /* Loop counter */
|
---|
149 | mpc = 0;
|
---|
150 | a = 0;
|
---|
151 | b = 0;
|
---|
152 | c = 0;
|
---|
153 | temp = 0;
|
---|
154 | temp2 = 0;
|
---|
155 | pc = 0;
|
---|
156 | mar = 0;
|
---|
157 | mdr = 0;
|
---|
158 | ir = 0;
|
---|
159 | imm = 0;
|
---|
160 | constant = 0;
|
---|
161 | for (i = 0; i < 32; i++)
|
---|
162 | reg[i] = 0;
|
---|
163 | }
|
---|
164 |
|
---|
165 |
|
---|
166 | void
|
---|
167 | find_new_mPC()
|
---|
168 | {
|
---|
169 | word tcode = 0;
|
---|
170 | int i; /* Loop counter */
|
---|
171 | micro_address_t next_mpc;
|
---|
172 | switch (microprogram[mpc].cond) {
|
---|
173 | case NEXT:
|
---|
174 | next_mpc = mpc + 1;
|
---|
175 | break;
|
---|
176 | case UNCOND:
|
---|
177 | next_mpc = microprogram[mpc].jump;
|
---|
178 | break;
|
---|
179 | case MEMBUSY:
|
---|
180 | if (mem_busy == FALSE)
|
---|
181 | next_mpc = mpc + 1;
|
---|
182 | else
|
---|
183 | next_mpc = microprogram[mpc].jump;
|
---|
184 | break;
|
---|
185 | case DECODE1:
|
---|
186 | /* XXX: Hardcoded, need something better */
|
---|
187 | tcode = ir >> 26; /* O-code */
|
---|
188 | if (tcode >= 32) /* MEM */
|
---|
189 | next_mpc = 2;
|
---|
190 | else if (tcode >= 8) /* IMM */
|
---|
191 | next_mpc = 17;
|
---|
192 | else if (tcode == 5) /* BNQ */
|
---|
193 | next_mpc = 9;
|
---|
194 | else if (tcode == 4) /* BEQ */
|
---|
195 | next_mpc = 7;
|
---|
196 | else if (tcode == 3) /* JAL */
|
---|
197 | next_mpc = 11;
|
---|
198 | else if (tcode == 2) /* J */
|
---|
199 | next_mpc = 13;
|
---|
200 | else if (tcode == 0) /* REG */
|
---|
201 | next_mpc = 16;
|
---|
202 | break;
|
---|
203 | case DECODE2:
|
---|
204 | tcode = ir >> 26; /* O-code */
|
---|
205 | for (i = 0; i < DECODE2_SIZE; i++) {
|
---|
206 | if (decode2[i].ocode == tcode) {
|
---|
207 | next_mpc = decode2[i].target;
|
---|
208 | break;
|
---|
209 | }
|
---|
210 | }
|
---|
211 | break;
|
---|
212 | case DECODE3:
|
---|
213 | tcode = (ir << 26) >> 26; /* F-code */
|
---|
214 | for (i = 0; i < DECODE3_SIZE; i++) {
|
---|
215 | if (decode3[i].fcode == tcode) {
|
---|
216 | next_mpc = decode2[i].target;
|
---|
217 | break;
|
---|
218 | }
|
---|
219 | }
|
---|
220 | break;
|
---|
221 | case ZEROVALUE:
|
---|
222 | if (zero == TRUE)
|
---|
223 | next_mpc = microprogram[mpc].jump;
|
---|
224 | else
|
---|
225 | next_mpc = mpc + 1;
|
---|
226 | break;
|
---|
227 | case NEGVALUE:
|
---|
228 | if (negative == TRUE)
|
---|
229 | next_mpc = microprogram[mpc].jump;
|
---|
230 | else
|
---|
231 | next_mpc = mpc + 1;
|
---|
232 | break;
|
---|
233 | case RTYPEIR:
|
---|
234 | if ((ir >> 26) == 0)
|
---|
235 | next_mpc = microprogram[mpc].jump;
|
---|
236 | else
|
---|
237 | next_mpc = mpc + 1;
|
---|
238 | break;
|
---|
239 | default:
|
---|
240 | fprintf(stderr, "Control action '%i' not implemented\n",
|
---|
241 | microprogram[mpc].cond);
|
---|
242 | exit(EX_SOFTWARE);
|
---|
243 | break;
|
---|
244 | };
|
---|
245 |
|
---|
246 | mpc = next_mpc;
|
---|
247 | }
|
---|
248 |
|
---|
249 |
|
---|
250 | void
|
---|
251 | set_signals()
|
---|
252 | {
|
---|
253 | /*
|
---|
254 | * XXX: Something like memory_busy or something like it
|
---|
255 | * XXX: Dirty hacking, we better off creating a sperate control
|
---|
256 | * struct which we 'feed' to datapath
|
---|
257 | */
|
---|
258 |
|
---|
259 | switch (mpc) {
|
---|
260 | case 1:
|
---|
261 | mem_busy = TRUE;
|
---|
262 | microprogram[mpc].control.register_address_a = (ir << 6) >> 26;
|
---|
263 | microprogram[mpc].control.register_address_b = (ir << 11) >> 26;
|
---|
264 | break;
|
---|
265 | case 12:
|
---|
266 | microprogram[mpc].control.register_address_c = 31;
|
---|
267 | break;
|
---|
268 | case 73:
|
---|
269 | microprogram[mpc].control.register_address_c = (ir << 11) >> 26;
|
---|
270 | break;
|
---|
271 | case 74:
|
---|
272 | microprogram[mpc].control.register_address_c = (ir << 16) >> 26;
|
---|
273 | break;
|
---|
274 | }
|
---|
275 | update_state(microprogram[mpc].control);
|
---|
276 | }
|
---|