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

Subversion Repositories leros

[/] [leros/] [trunk/] [java/] [tools/] [src/] [leros/] [asm/] [LerosAsm.java] - Blame information for rev 4

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 3 martin
/*
2
   Copyright 2011 Martin Schoeberl <masca@imm.dtu.dk>,
3
                  Technical University of Denmark, DTU Informatics.
4
   All rights reserved.
5
 
6
   Redistribution and use in source and binary forms, with or without
7
   modification, are permitted provided that the following conditions are met:
8
 
9
      1. Redistributions of source code must retain the above copyright notice,
10
         this list of conditions and the following disclaimer.
11
 
12
      2. Redistributions in binary form must reproduce the above copyright
13
         notice, this list of conditions and the following disclaimer in the
14
         documentation and/or other materials provided with the distribution.
15
 
16
   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER ``AS IS'' AND ANY EXPRESS
17
   OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18
   OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
19
   NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
20
   DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21
   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22
   LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23
   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24
   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25
   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
 
27
   The views and conclusions contained in the software and documentation are
28
   those of the authors and should not be interpreted as representing official
29
   policies, either expressed or implied, of the copyright holder.
30
 */
31
 
32
package leros.asm;
33
 
34
import java.io.File;
35
import java.io.FileInputStream;
36
import java.io.FileWriter;
37
import java.io.IOException;
38
import java.io.InputStream;
39
import java.io.PrintWriter;
40
import java.util.List;
41
 
42
import org.antlr.runtime.ANTLRInputStream;
43
import org.antlr.runtime.CommonTokenStream;
44
 
45
import leros.asm.generated.*;
46
 
47
public class LerosAsm {
48
 
49
        static final int ADDRBITS = 9;
50
        static final int DATABITS = 16;
51
        static final int ROM_LEN = 1 << ADDRBITS;
52
 
53
        String fname;
54
        String dstDir = "./";
55
        String srcDir = "./";
56
 
57
        public LerosAsm(String[] args) {
58
                srcDir = System.getProperty("user.dir");
59
                dstDir = System.getProperty("user.dir");
60
                processOptions(args);
61
                if (!srcDir.endsWith(File.separator))
62
                        srcDir += File.separator;
63
                if (!dstDir.endsWith(File.separator))
64
                        dstDir += File.separator;
65
        }
66
 
67
        String bin(int val, int bits) {
68
 
69
                String s = "";
70
                for (int i = 0; i < bits; ++i) {
71
                        s += (val & (1 << (bits - i - 1))) != 0 ? "1" : "0";
72
                }
73
                return s;
74
        }
75
 
76
        String getRomHeader() {
77
 
78
                String line = "--\n";
79
                line += "--\tleros_rom.vhd\n";
80
                line += "--\n";
81
                line += "--\tgeneric VHDL version of ROM\n";
82
                line += "--\n";
83
                line += "--\t\tDONT edit this file!\n";
84
                line += "--\t\tgenerated by " + this.getClass().getName() + "\n";
85
                line += "--\n";
86
                line += "\n";
87
                line += "library ieee;\n";
88
                line += "use ieee.std_logic_1164.all;\n";
89
                line += "\n";
90
                line += "entity leros_rom is\n";
91
                // line +=
92
                // "generic (width : integer; addr_width : integer);\t-- for compatibility\n";
93
                line += "port (\n";
94
                line += "    address : in std_logic_vector(" + (ADDRBITS - 1)
95
                                + " downto 0);\n";
96
                line += "    q : out std_logic_vector(" + (DATABITS - 1)
97
                                + " downto 0)\n";
98
                line += ");\n";
99
                line += "end leros_rom;\n";
100
                line += "\n";
101
                line += "architecture rtl of leros_rom is\n";
102
                line += "\n";
103
                line += "begin\n";
104
                line += "\n";
105
                line += "process(address) begin\n";
106
                line += "\n";
107
                line += "case address is\n";
108
 
109
                return line;
110
        }
111
 
112
        String getRomFeet() {
113
 
114
                String line = "\n";
115
                line += "    when others => q <= \"" + bin(0, DATABITS) + "\";\n";
116
                line += "end case;\n";
117
                line += "end process;\n";
118
                line += "\n";
119
                line += "end rtl;\n";
120
 
121
                return line;
122
        }
123
 
124
        public void dump(List list) {
125
 
126
                try {
127
 
128
                        FileWriter romvhd = new FileWriter(dstDir + "leros_rom.vhd");
129
                        PrintWriter romdat = new PrintWriter(new FileWriter("rom.txt"));
130
 
131
                        romvhd.write(getRomHeader());
132
 
133
                        Object o[] = list.toArray();
134
                        for (int i = 0; i < o.length; ++i) {
135
                                int val = ((Integer) o[i]).intValue();
136
                                romdat.println(val);
137
                                romvhd.write("    when \"" + bin(i, ADDRBITS) + "\" => q <= \""
138
                                                + bin(val, DATABITS) + "\";");
139
//                              romvhd.write(" -- " + inraw.readLine() + "\n");
140
                                romvhd.write("\n");
141
 
142
                        }
143
 
144
                        romvhd.write(getRomFeet());
145
                        romvhd.close();
146
                        romdat.close();
147
 
148
                        // PrintStream rom_mem = new PrintStream(new FileOutputStream(dstDir
149
                        // + "mem_rom.dat"));
150
                        // for (int i=0; i<ROM_LEN; ++i) {
151
                        // rom_mem.println(romData[i]+" ");
152
                        // }
153
                        // rom_mem.close();
154
 
155
                } catch (IOException e) {
156
                        System.out.println(e.getMessage());
157
                        System.exit(-1);
158
                }
159
        }
160
 
161
        private boolean processOptions(String clist[]) {
162
                boolean success = true;
163
 
164
                for (int i = 0; i < clist.length; i++) {
165
                        if (clist[i].equals("-s")) {
166
                                srcDir = clist[++i];
167
                        } else if (clist[i].equals("-d")) {
168
                                dstDir = clist[++i];
169
                        } else {
170
                                fname = clist[i];
171
                        }
172
                }
173
 
174
                return success;
175
        }
176
 
177
        /**
178
         * @param args
179
         */
180
        public static void main(String[] args) throws Exception {
181
 
182
                if (args.length < 1) {
183
                        System.out
184
                                        .println("usage: java LerosAsm [-s srcDir] [-d dstDir] filename");
185
                        System.exit(-1);
186
                }
187
                LerosAsm la = new LerosAsm(args);
188
 
189
                InputStream istr = new FileInputStream(la.srcDir + la.fname);
190
                ANTLRInputStream input = new ANTLRInputStream(istr);
191
                LerosLexer lexer = new LerosLexer(input);
192
                CommonTokenStream tokens = new CommonTokenStream(lexer);
193
                LerosParser parser = new LerosParser(tokens);
194
                parser.pass1();
195
                // parser.dump();
196
                parser.reset();
197
                List code = parser.pass2();
198
                System.out.println(code);
199
 
200
                la.dump(code);
201
        }
202
 
203
}

powered by: WebSVN 2.1.0

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