OpenCores
URL https://opencores.org/ocsvn/mips_16/mips_16/trunk

Subversion Repositories mips_16

[/] [mips_16/] [trunk/] [sw/] [mips_16_assembler.java] - Blame information for rev 3

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 3 Doyya
import java.io.BufferedReader;
2
import java.io.BufferedWriter;
3
import java.io.File;
4
import java.io.FileNotFoundException;
5
import java.io.FileReader;
6
import java.io.FileWriter;
7
import java.io.IOException;
8
import java.util.Hashtable;
9
 
10
public class mips_16_assembler {
11
 
12
        private static File sourceFile, destFile;
13
        private static Hashtable<String, Integer>  labelList = new Hashtable<String, Integer>();
14
        private static Hashtable<String, Integer>  instructionList = new Hashtable<String, Integer>();
15
        private static int currentLine = 0;
16
        private static int codeLineCnt = 0;
17
 
18
        public static void main(String[] args) {
19
                if(args.length == 0)
20
                {
21
                        System.out.println("no filename specified");
22
                        System.exit(-1);
23
                }
24
                sourceFile = new File(args[0]);
25
 
26
                if(args.length == 1)
27
                {
28
                        if(args[0].equalsIgnoreCase("-h") || args[0].equalsIgnoreCase("--help") )
29
                        {
30
                                System.out.println("Useage:");
31
                                System.out.println("java mips_16_assembler [options]");
32
                                System.out.println("options:");
33
                                System.out.println("<source_code_path> <dest_path>:");
34
                                System.out.println("\tAssemble source code to dest. For exaple .\\bin\\test1.asm .\\bin\\test1.prog");
35
                                System.out.println("<source_code_path>:");
36
                                System.out.println("\tAssemble source code to dest file a.prog");
37
                                System.out.println("-h(or --help):");
38
                                System.out.println("\tShow this help");
39
                                System.exit(0);
40
                        }
41
                        else
42
                        {
43
                                if(sourceFile.getParent() != null)
44
                                        destFile = new File(sourceFile.getParent().concat(File.separator).concat("a.prog"));
45
                                else
46
                                        destFile = new File("a.prog");
47
                        }
48
                }
49
                else if(args.length == 2)
50
                {
51
                        destFile = new File(args[1]);
52
                }
53
 
54
 
55
                if(!destFile.exists())
56
                        try {
57
                                destFile.createNewFile();
58
                        } catch (IOException e) {
59
                                System.out.println("Create machine language file error");
60
                                e.printStackTrace();
61
                        }
62
                System.out.println("assemble filename :"+ sourceFile.getName());
63
                System.out.println("machine language filename : "+destFile.getName());
64
 
65
                initialInstructionList();
66
                findLabels(sourceFile, destFile);
67
                assembleFile(sourceFile, destFile);
68
                //System.out.println(labelList);
69
        }
70
 
71
        private static void initialInstructionList() {
72
                instructionList.put("NOP", 0);
73
                instructionList.put("ADD", 1);
74
                instructionList.put("SUB", 2);
75
                instructionList.put("AND", 3);
76
                instructionList.put("OR", 4);
77
                instructionList.put("XOR", 5);
78
                instructionList.put("SL", 6);
79
                instructionList.put("SR", 7);
80
                instructionList.put("SRU", 8);
81
                instructionList.put("ADDI", 9);
82
                instructionList.put("LD", 10);
83
                instructionList.put("ST", 11);
84
                instructionList.put("BZ", 12);
85
        }
86
 
87
        private static void findLabels(File sourceFile, File destFile) {
88
                String line = new String();
89
                BufferedReader sourceBr;
90
                BufferedWriter destBw;
91
 
92
                String parsedLine[] = new String[5];
93
 
94
                currentLine = 0;
95
                codeLineCnt = 0;
96
                System.out.println("=====Pass one: Finding Labels=====");
97
 
98
                try {
99
                        sourceBr = new BufferedReader(new FileReader(sourceFile));
100
                        destBw = new BufferedWriter(new FileWriter(destFile));
101
                        boolean endOfFile = false;
102
                        while(!endOfFile)
103
                        {
104
                                line = sourceBr.readLine();
105
                                if(line != null)
106
                                {
107
                                        currentLine++;
108
                                        codeLineCnt++;
109
                                        line = line.toUpperCase();
110
                                        parsedLine = parseLine(currentLine, line);
111
                                        if(parsedLine[0] != null)
112
                                        {
113
                                                //translatedLine = translateLine(currentLine, parsedLine);
114
                                                //System.out.print("translatedLine: "+translatedLine);
115
                                                //destBw.write(translatedLine);
116
                                                //destBw.newLine();
117
                                                //System.out.println(" wrote");
118
                                        }
119
                                        else
120
                                        {
121
                                                codeLineCnt--;
122
                                                //System.out.println("Non-Code line, skiped");
123
                                        }
124
                                }
125
                                else
126
                                        endOfFile = true;
127
                        }
128
                        sourceBr.close();
129
                        destBw.close();
130
                        System.out.println("Labels found: ");
131
                        System.out.println(labelList);
132
 
133
 
134
                } catch (FileNotFoundException e) {
135
                        e.printStackTrace();
136
                } catch (IOException e) {
137
                        e.printStackTrace();
138
                }
139
 
140
        }
141
 
142
        private static void assembleFile(File sourceFile, File destFile) {
143
 
144
                String line = new String();
145
                BufferedReader sourceBr;
146
                BufferedWriter destBw;
147
 
148
                String parsedLine[] = new String[5];
149
                String translatedLine;
150
 
151
                currentLine = 0;
152
                codeLineCnt = 0;
153
                System.out.println("=====Pass two: translate=====");
154
 
155
                try {
156
                        sourceBr = new BufferedReader(new FileReader(sourceFile));
157
                        destBw = new BufferedWriter(new FileWriter(destFile));
158
                        boolean endOfFile = false;
159
                        while(!endOfFile)
160
                        {
161
                                line = sourceBr.readLine();
162
                                if(line != null)
163
                                {
164
                                        currentLine++;
165
                                        codeLineCnt++;
166
                                        line = line.toUpperCase();
167
                                        parsedLine = parseLine(currentLine, line);
168
                                        if(parsedLine[0] != null)
169
                                        {
170
                                                translatedLine = translateLine(currentLine, parsedLine);
171
                                                System.out.print("translatedLine: "+translatedLine);
172
                                                destBw.write(translatedLine);
173
                                                destBw.newLine();
174
                                                System.out.println(" wrote");
175
                                        }
176
                                        else
177
                                        {
178
                                                codeLineCnt--;
179
                                                System.out.println("Non-Code line, skiped");
180
                                        }
181
                                }
182
                                else
183
                                        endOfFile = true;
184
                        }
185
                        sourceBr.close();
186
                        destBw.close();
187
                        System.out.println("assemble complete ");
188
 
189
 
190
 
191
                } catch (FileNotFoundException e) {
192
                        e.printStackTrace();
193
                } catch (IOException e) {
194
                        e.printStackTrace();
195
                }
196
 
197
 
198
 
199
        }
200
 
201
        private static String translateLine(int currentLine, String[] parsedLine) {
202
                String translatedLine = null;
203
                String op = parsedLine[0];
204
 
205
                if(instructionList.get(op) == null)
206
                        reportSyntaxError(currentLine, "unkown Instruction:"+op);
207
 
208
                switch(instructionList.get(op))
209
                {
210
                case 0:  //("NOP", 0)
211
                        translatedLine = "0000000000000000";
212
                        break;
213
                case 1: //("ADD", 1)
214
                case 2: //("SUB", 2)
215
                case 3: //("AND", 3)
216
                case 4: //("OR", 4)
217
                case 5: //("XOR", 5)
218
                case 6: //("SL", 6)
219
                case 7: //("SR", 7)
220
                case 8: //("SRU", 8)
221
                        if(checkRType(currentLine, parsedLine))
222
                        {
223
                                translatedLine = convTo4Digits(instructionList.get(op)) +
224
                                                                convTo3Digits(Integer.parseInt(parsedLine[1].replace("R", ""))) +
225
                                                                convTo3Digits(Integer.parseInt(parsedLine[2].replace("R", ""))) +
226
                                                                convTo3Digits(Integer.parseInt(parsedLine[3].replace("R", ""))) +
227
                                                                "000";
228
                        }
229
                        break;
230
                case 9: //("ADDI", 9)
231
                case 10: //("LD", 10)
232
                case 11: //("ST", 11)
233
                        if(checkIType(currentLine, parsedLine))
234
                        {
235
                                translatedLine = convTo4Digits(instructionList.get(op)) +
236
                                                                convTo3Digits(Integer.parseInt(parsedLine[1].replace("R", ""))) +
237
                                                                convTo3Digits(Integer.parseInt(parsedLine[2].replace("R", ""))) +
238
                                                                convTo6Digits(Integer.parseInt(parsedLine[3]));
239
                        }
240
                        break;
241
                case 12: //("BZ", 12)
242
                        if(checkBranchType(currentLine, parsedLine))
243
                        {
244
                                translatedLine = convTo4Digits(instructionList.get(op)) +
245
                                "000"+
246
                                convTo3Digits(Integer.parseInt(parsedLine[1].replace("R", ""))) +
247
                                convTo6Digits(labelList.get(parsedLine[2]) - codeLineCnt -1);
248
                                //System.out.println("br: "+codeLineCnt);
249
                        }
250
                        break;
251
                default:
252
                        reportSyntaxError(currentLine, "unknown Instruction:"+op);
253
                        break;
254
                }
255
                return translatedLine;
256
        }
257
 
258
 
259
 
260
 
261
        private static boolean checkBranchType(int currentLine2, String[] parsedLine) {
262
                if(parsedLine[1].startsWith("R") &&
263
                                labelList.get(parsedLine[2])!=null
264
                )
265
                        return true;
266
                else
267
                        reportSyntaxError(currentLine, "Branch-type instrcution should have 1 registers and line label");
268
 
269
                return false;
270
        }
271
 
272
        private static boolean checkIType(int currentLine2, String[] parsedLine) {
273
                if(parsedLine[1].startsWith("R") &&
274
                                parsedLine[2].startsWith("R")&&
275
                                Integer.parseInt(parsedLine[3])<32 &&
276
                                Integer.parseInt(parsedLine[3])>=-32
277
                )
278
                        return true;
279
                else
280
                        reportSyntaxError(currentLine, "I-type instrcution should have 2 registers and a immediate number(range -32~31)");
281
 
282
                return false;
283
        }
284
 
285
        private static boolean checkRType(int currentLine, String[] parsedLine) {
286
                if(parsedLine[1].startsWith("R") &&
287
                                parsedLine[2].startsWith("R")&&
288
                                parsedLine[3].startsWith("R")
289
                )
290
                        return true;
291
                else
292
                        reportSyntaxError(currentLine, "R-type instrcution should have 3 registers");
293
 
294
                return false;
295
        }
296
 
297
        private static String convTo3Digits(int in) {
298
                String binary3digits;
299
                binary3digits = Integer.toBinaryString(in);
300
                if(binary3digits.length()>3)
301
                        reportSyntaxError(currentLine,"conv To 3 digits wrong");
302
                while(binary3digits.length()<3)
303
                        binary3digits = "0"+binary3digits;
304
                return binary3digits;
305
        }
306
 
307
        private static String convTo4Digits(int in) {
308
                String binary4digits;
309
                binary4digits = Integer.toBinaryString(in);
310
                if(binary4digits.length()>4)
311
                        reportSyntaxError(currentLine,"conv To 4digits wrong");
312
                while(binary4digits.length()<4)
313
                        binary4digits = "0"+binary4digits;
314
                return binary4digits;
315
        }
316
 
317
        private static String convTo6Digits(int in) {
318
                String binary6digits;
319
                binary6digits = Integer.toBinaryString(in);
320
                //System.out.println("binary6digits : "+binary6digits);
321
                if(in>=0)
322
                {
323
                        if(binary6digits.length()>6)
324
                                reportSyntaxError(currentLine,"conv To 6digits wrong");
325
                        else
326
                                while(binary6digits.length()<6)
327
                                        binary6digits = "0"+binary6digits;
328
                }
329
                else
330
                {
331
                        binary6digits = binary6digits.substring(binary6digits.length()-6);
332
                }
333
                //System.out.println("binary6digits : "+binary6digits);
334
                return binary6digits;
335
        }
336
 
337
        private static String[] parseLine(int currentLine, String line) {
338
                String temp[];
339
                int i, p;
340
                String parsedLine[] = new String[5];
341
                //System.out.println("parsing: "+line);
342
                temp = line.split("[\\s,]");
343
                //System.out.println("string length"+temp.length);
344
                p = 0;
345
                boolean labelFound = false;
346
                for(i=0; i< temp.length; i++)
347
                {
348
                        if(temp[i].contains(";"))
349
                                break;
350
                        else if(temp[i].length()>0)
351
                        {
352
                                if(temp[i].contains(":"))
353
                                {
354
                                        String  temp1[] = null;
355
 
356
                                        if(!labelFound)
357
                                                labelFound = true;
358
                                        else
359
                                                reportSyntaxError(currentLine,"found multi-labels");
360
 
361
                                        temp1 = temp[i].split(":");
362
                                        if(temp1.length == 1)
363
                                        {
364
                                                labelList.put(temp1[0], codeLineCnt);
365
                                        }
366
                                        else
367
                                        if(temp1.length == 2)
368
                                        {
369
                                                labelList.put(temp1[0], codeLineCnt);
370
                                                parsedLine[p++] = temp1[1];
371
                                        }
372
                                        else
373
                                        {
374
                                                reportSyntaxError(currentLine,"error when parsing labels");
375
                                        }
376
                                }
377
                                else
378
                                        parsedLine[p++] = temp[i];
379
                        }
380
                }
381
                System.out.print("parsed Line "+currentLine+": ");
382
                for(i=0; i<p; i++)
383
                        System.out.print(parsedLine[i]+" ");
384
                System.out.println("");
385
 
386
                return parsedLine;
387
        }
388
 
389
        private static void reportSyntaxError(int lineNum, String errReason) {
390
                System.out.println("error at line:"+lineNum+", "+ errReason);
391
                System.exit(-1);
392
        }
393
 
394
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.