1 /*
2 * MythSim
3 *
4 * Copyright (C) 2002-2004 Jason Vroustouris <jasonv@jasonv.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 package org.mythsim.core;
22
23 /***
24 * A Control Word.
25 * @author Jason Vroustouris
26 */
27 public class ControlWord {
28
29 public static final int R0_WRITE = 0;
30 public static final int R1_WRITE = 1;
31 public static final int R2_WRITE = 2;
32 public static final int R3_WRITE = 3;
33 public static final int R4_WRITE = 4;
34 public static final int R5_WRITE = 5;
35 public static final int R6_WRITE = 6;
36 public static final int R7_WRITE = 7;
37 public static final int A_SEL = 8;
38 public static final int B_SEL = 9;
39 public static final int RI_SEL = 10;
40 public static final int RJ_SEL = 11;
41 public static final int RK_SEL = 12;
42 public static final int C_IN = 13;
43 public static final int ALU_SEL = 14;
44 public static final int MDR_SEL = 15;
45 public static final int MAR_SEL = 16;
46 public static final int RESULT_SEL = 17;
47 public static final int IR0_SEL = 18;
48 public static final int IR1_SEL = 19;
49 public static final int READ = 20;
50 public static final int WRITE = 21;
51 public static final int INDEX_SEL = 22;
52 public static final int COND = 23;
53 public static final int ADDRESS_TRUE = 24;
54 public static final int ADDRESS_FALSE = 25;
55 public static final int ADDRESS = 26;
56 public static final int R0_WRITE_SET = 27;
57 public static final int R1_WRITE_SET = 28;
58 public static final int R2_WRITE_SET = 29;
59 public static final int R3_WRITE_SET = 30;
60 public static final int R4_WRITE_SET = 31;
61 public static final int R5_WRITE_SET = 32;
62 public static final int R6_WRITE_SET = 33;
63 public static final int R7_WRITE_SET = 34;
64 public static final int A_SEL_SET = 35;
65 public static final int B_SEL_SET = 36;
66 public static final int RI_SEL_SET = 37;
67 public static final int RJ_SEL_SET = 38;
68 public static final int RK_SEL_SET = 39;
69 public static final int C_IN_SET = 40;
70 public static final int ALU_SEL_SET = 41;
71 public static final int MDR_SEL_SET = 42;
72 public static final int MAR_SEL_SET = 43;
73 public static final int RESULT_SEL_SET = 44;
74 public static final int IR0_SEL_SET = 45;
75 public static final int IR1_SEL_SET = 46;
76 public static final int READ_SET = 47;
77 public static final int WRITE_SET = 48;
78 public static final int INDEX_SEL_SET = 49;
79 public static final int COND_SET = 50;
80 public static final int ADDRESS_TRUE_SET = 51;
81 public static final int ADDRESS_FALSE_SET = 52;
82 public static final int ADDRESS_SET = 53;
83 public static final int CONTROL_WORD_LENGTH = 54;
84
85 int ControlLine[] = new int[CONTROL_WORD_LENGTH];
86 boolean ControlLine_Set[] = new boolean[ControlLine.length];
87 String _original_line = "";
88
89 public int get(int index) {
90 return ControlLine[index];
91 }
92
93 public boolean isSet(int index) {
94 return (ControlLine[index] == 1);
95 }
96
97 public void set(int index) throws MythParserError {
98 set(index, 1);
99 }
100
101 public void print(int index) {
102 System.out.print(ControlLine[index]);
103 }
104
105 public int[] toArray() {
106 return ControlLine;
107 }
108
109 public String line() {
110 return _original_line;
111 }
112
113 //******************************************************************************
114 // EXTENDED INORMATION
115 //*****************************************************************************
116
117 public static final String alu_value[] = {
118 "NOT", "OR", "AND", "XOR", "ADD", "SUB", "ADDA", "SUBA"};
119 public static final String mar_value[] = {
120 "HOLD", "LOAD"};
121 public static final String mdr_value[] = {
122 "HOLD", "LOAD_ALU", "LOAD_MEM"};
123 public static final String result_value[] = {
124 "ALU", "MDR", "IR_CONST4", "IR_CONST8"};
125 public static final String ir_value[] = {
126 "HOLD", "LOAD"};
127 public static final String cond_value[] = {
128 "M_7", "C_OUT", "V", "WAIT"};
129 public static final String control_line_value[] = {
130 "R0_WRITE",
131 "R1_WRITE", "R2_WRITE", "R3_WRITE", "R4_WRITE",
132 "R5_WRITE", "R6_WRITE", "R7_WRITE", "A_SEL", "B_SEL",
133 "RI_SEL", "RJ_SEL", "RK_SEL", "C_IN", "ALU_SEL", "MDR_SEL", "MAR_SEL",
134 "RESULT_SEL", "IR0_SEL", "IR1_SEL", "READ", "WRITE", "INDEX_SEL",
135 "COND", "ADDRESS_TRUE", "ADDRESS_FALSE", "ADDRESS"};
136
137 /* original line from ucode file */
138 String original_line = "";
139
140 public void setAddress(int a) {
141 _address = a;
142 }
143
144 public void setLabel(String s) {
145 _label = s;
146 }
147
148 public void setTrueLabel(String s) {
149 _address_true_string = s;
150 }
151
152 public void setFalseLabel(String s) {
153 _address_false_string = s;
154 }
155
156 public void setLineNumber(int i) {
157 _line_number = i;
158 }
159
160 public int getLineNumber() {
161 return _line_number;
162 }
163
164 public String getTrueLabel() {
165 return _address_true_string;
166 }
167
168 public String getFalseLabel() {
169 return _address_false_string;
170 }
171
172 /* addressing values*/
173 public String address() {
174 String temp = "";
175 if (_address < 10) {
176 temp += "0";
177 }
178 if (_address < 100) {
179 temp += "0";
180 }
181 if (_address < 1000) {
182 temp += "0";
183 }
184 temp += _address;
185 return temp;
186 }
187
188 public void print(String a) {
189 System.out.print(a);
190 }
191
192 public void printAddress() {
193 System.out.print(address());
194 }
195
196 int _address = 0;
197
198 public String label() {
199 return _label;
200 }
201
202 String _label = "";
203 boolean _true_label_found = false;
204 boolean _false_label_found = false;
205 int _line_number = -1;
206 String _address_string = "";
207 String _address_true_string = "";
208 String _address_true_offset_string = "";
209 String _address_false_string = "";
210 String _address_false_offset_string = "";
211
212 /*** creates an empty control word */
213 public ControlWord() {
214 for (int i = 0; i < ControlLine.length; i++) {
215 ControlLine[i] = 0;
216 }
217 }
218
219 /*public ControlWord(int a, String t) throws Exception {
220 _address = a;
221 _original_line = t;
222 for (int i=0; i<ControlLine.length; i++) {
223 ControlLine[i] = 0;
224 ControlLine_Set[i] = false;
225 }
226 String array[] = t.split("[,:]");
227 _label = array[0].toUpperCase().trim();
228 String nospace = "";
229 for (int i=0; i<array.length; i++) {
230 nospace = array[i].replaceAll(" ","");
231 nospace = nospace.toUpperCase();
232 nospace = nospace.trim();
233 //System.out.println(nospace);
234 if (nospace.equalsIgnoreCase("ri_sel")) set(RI_SEL);
235 if (nospace.equalsIgnoreCase("rj_sel")) set(RJ_SEL);
236 if (nospace.equalsIgnoreCase("rk_sel")) set(RK_SEL);
237 if (nospace.equalsIgnoreCase("a_sel=0")) set(A_SEL,0);
238 if (nospace.equalsIgnoreCase("a_sel=1")) set(A_SEL,1);
239 if (nospace.equalsIgnoreCase("a_sel=2")) set(A_SEL,2);
240 if (nospace.equalsIgnoreCase("a_sel=3")) set(A_SEL,3);
241 if (nospace.equalsIgnoreCase("a_sel=4")) set(A_SEL,4);
242 if (nospace.equalsIgnoreCase("a_sel=5")) set(A_SEL,5);
243 if (nospace.equalsIgnoreCase("a_sel=6")) set(A_SEL,6);
244 if (nospace.equalsIgnoreCase("a_sel=7")) set(A_SEL,7);
245 if (nospace.equalsIgnoreCase("a_sel=8")) throw new MythParserError("ucode",11,"this is a test");
246 // B_SEL check
247 for (int j=0; j<8; j++) {
248 if (nospace.equalsIgnoreCase("b_sel=" + j)) {
249 set(B_SEL,j);
250 }
251 }
252 // C_IN check
253 if (nospace.equalsIgnoreCase("c_in")) {
254 set(C_IN);
255 }
256 // R_WRITE check
257 for (int j=0; j<8; j++) {
258 if (nospace.equalsIgnoreCase("r" + j + "_write")) {
259 set(R0_WRITE+j);
260 }
261 }
262 // ALU_SEL check
263 for (int j=0; j<8; j++) {
264 if (nospace.equalsIgnoreCase("alu_sel=" + alu_value[j])) {
265 //System.out.println("alu_sel:" + alu_value[j] + "," + j + "," + nospace);
266 set(ALU_SEL,j);
267 }
268 }
269 // MAR_SEL check
270 for (int j=0; j<2; j++) {
271 if (nospace.equalsIgnoreCase("mar_sel=" + mar_value[j])) {
272 set(MAR_SEL,j);
273 }
274 }
275 // MDR_SEL check
276 for (int j=0; j<3; j++) {
277 if (nospace.equalsIgnoreCase("mdr_sel=" + mdr_value[j])) {
278 set(MDR_SEL,j);
279 }
280 }
281 // RESULT_SEL check
282 for (int j=0; j<4; j++) {
283 if (nospace.equalsIgnoreCase("result_sel=" + result_value[j])) {
284 set(RESULT_SEL,j);
285 }
286 }
287 if (nospace.equalsIgnoreCase("ir0_sel=LOAD")) {set(IR0_SEL);}
288 if (nospace.equalsIgnoreCase("ir1_sel=LOAD")) {set(IR1_SEL);}
289 if (nospace.equalsIgnoreCase("read")) {set(READ);}
290 if (nospace.equalsIgnoreCase("write")) {set(WRITE);}
291 if (nospace.equalsIgnoreCase("index_sel")) {set(INDEX_SEL);}
292 // COND check
293 for (int j=0; j<4; j++) {
294 //System.out.println("cond " + j + "," + cond_value[j] + "," + nospace);
295 if (nospace.startsWith("IF" + cond_value[j])) {
296 //System.out.println("cond set to " + j);
297 set(COND,j);
298 }
299 }
300 // GOTO check
301 if (nospace.startsWith("GOTO")) {
302 String line[] = nospace.split("GOTO");
303 String address_array[] = new String[1];
304 if (line.length == 2) {
305 address_array = line[1].split("//[|//]");
306 if (address_array.length == 1) {
307 _address_true_string = address_array[0].trim();
308 _address_false_string = address_array[0].trim();
309 }
310 if (address_array.length == 2) {
311 //_address_true_string = address_array[0].trim();
312 _address_true_string = line[1].trim();
313 //_address_false_string = address_array[0].trim();
314 _address_false_string = line[1].trim();
315 _address_true_offset_string = address_array[1];
316 _address_false_offset_string = address_array[1];
317 if (_address_true_offset_string.equalsIgnoreCase("IR_OPCODE")) {
318 set(INDEX_SEL);
319 }
320 //System.out.println("goto:" + nospace + "," + _address_true_offset_string);
321 }
322 }
323 }
324 // If check
325 if (nospace.startsWith("IF")) {
326 String line[] = nospace.split("THENGOTO|ELSEGOTO|ENDIF") ;
327 String address_array[] = new String[1];
328 // if [0] then goto [1] endif;
329 if (line.length == 2) {
330 address_array = line[1].split("//[|//]");
331 if (address_array.length == 1) {
332 _address_true_string = address_array[0].trim();
333 }
334 if (address_array.length == 2) {
335 //_address_true_string = address_array[0].trim();
336 _address_true_string = line[1].trim();
337 _address_true_offset_string = address_array[1];
338 }
339 }
340 // if [0] then goto [1] else goto [2] endif;
341 if (line.length == 3) {
342 address_array = line[1].split("//[|//]");
343 if (address_array.length == 1) {
344 _address_true_string = address_array[0].trim();
345 }
346 if (address_array.length == 2) {
347 //_address_true_string = address_array[0].trim();
348 _address_true_string = line[1].trim();
349 _address_true_offset_string = address_array[1];
350 }
351 address_array = line[2].split("//[|//]");
352 if (address_array.length == 1) {
353 _address_false_string = address_array[0].trim();
354 }
355 if (address_array.length == 2) {
356 //_address_false_string = address_array[0].trim();
357 _address_false_string = line[2].trim();
358 _address_false_offset_string = address_array[1];
359 }
360 }
361 }
362 }
363 //System.out.println("==========");
364 //printHead();
365 //print();
366 //System.out.println(t);
367 }*/
368
369 public void set(int index, int value) {
370 ControlLine[index] = value;
371 ControlLine[R0_WRITE_SET+index] = 1;
372 }
373
374 public static void printHead() {
375 System.out.println(" r");
376 System.out.println(
377 " e i");
378 System.out.println(
379 " s n");
380 System.out.println(
381 " a m m u d");
382 System.out.println(
383 " r r r l d a l r r e");
384 System.out.println(
385 " a b i j k u r r t 0 1 w x");
386 System.out.println(
387 " r r c");
388 System.out.println(
389 " s s s s s c s s s s s s e i s o");
390 System.out.println(
391 " r<k>write e e e e e i e e e e e e a t e n addr addr");
392 System.out.println(
393 "addr 7654 3210 l l l l l n l l l l l l d e l d true false");
394 System.out.println(
395 "---------------------------------------------------------------------------");
396 }
397
398 public void print() {
399 printAddress();
400 print(" ");
401 print(R7_WRITE);
402 print(R6_WRITE);
403 print(R5_WRITE);
404 print(R4_WRITE);
405 print(" ");
406 print(R3_WRITE);
407 print(R2_WRITE);
408 print(R1_WRITE);
409 print(R0_WRITE);
410 print(" ");
411 print(A_SEL);
412 print(" ");
413 print(B_SEL);
414 print(" ");
415 print(RI_SEL);
416 print(" ");
417 print(RJ_SEL);
418 print(" ");
419 print(RK_SEL);
420 print(" ");
421 print(C_IN);
422 print(" ");
423 print(ALU_SEL);
424 print(" ");
425 print(MDR_SEL);
426 print(" ");
427 print(MAR_SEL);
428 print(" ");
429 print(RESULT_SEL);
430 print(" ");
431 print(IR0_SEL);
432 print(" ");
433 print(IR1_SEL);
434 print(" ");
435 print(READ);
436 print(" ");
437 print(WRITE);
438 print(" ");
439 print(INDEX_SEL);
440 print(" ");
441 print(COND);
442 print(" ");
443 if (get(ADDRESS_TRUE) < 10) {
444 System.out.print("0");
445 }
446 if (get(ADDRESS_TRUE) < 100) {
447 System.out.print("0");
448 }
449 if (get(ADDRESS_TRUE) < 1000) {
450 System.out.print("0");
451 }
452 print(ADDRESS_TRUE);
453 System.out.print(" ");
454 if (get(ADDRESS_FALSE) < 10) {
455 System.out.print("0");
456 }
457 if (get(ADDRESS_FALSE) < 100) {
458 System.out.print("0");
459 }
460 if (get(ADDRESS_FALSE) < 1000) {
461 System.out.print("0");
462 }
463 print(ADDRESS_FALSE);
464 print("\n");
465 System.out.println("(" + _label + "," + _address_true_string + "," +
466 _address_false_string + ")");
467 }
468
469 public void print_addr() {
470 System.out.print("l=" + _label);
471 System.out.print(" at=" + _address_true_string);
472 System.out.print(" ato=" + _address_true_offset_string);
473 System.out.print(" af=" + _address_false_string);
474 System.out.print(" afo=" + _address_false_offset_string);
475 System.out.println("");
476 }
477
478 public boolean resolve_addr(String a[]) {
479 //System.out.print("ra");
480 for (int i = 0; i < a.length; i++) {
481 //System.out.println(a[i] + "," + _address_true_string + "," + _address_false_string);
482 if (a[i].equalsIgnoreCase(_address_true_string)) {
483 //System.out.println("true_address");
484 ControlLine[ADDRESS_TRUE] = i;
485 _true_label_found = true;
486 }
487 if (a[i].equalsIgnoreCase(_address_false_string)) {
488 //System.out.println("false_address");
489 ControlLine[ADDRESS_FALSE] = i;
490 _false_label_found = true;
491 }
492 }
493 if (_address_true_string.equals("")) {
494 ControlLine[ADDRESS_TRUE] = _address + 1;
495 _true_label_found = true;
496 }
497 if (_address_false_string.equals("")) {
498 ControlLine[ADDRESS_FALSE] = _address + 1;
499 _false_label_found = true;
500 }
501 //System.out.println("(" + _address + "," + ControlLine[ADDRESS_TRUE] + "," + ControlLine[ADDRESS_FALSE] + ")");
502 if (_true_label_found && _false_label_found) {
503 return true;
504 }
505 else {
506 return false;
507 }
508 }
509
510 public boolean validTrueLabel() {
511 return _true_label_found;
512 }
513
514 public boolean validFalseLabel() {
515 return _false_label_found;
516 }
517 }
This page was automatically generated by Maven