mirror of
https://github.com/pConst/basic_verilog.git
synced 2025-01-28 07:02:55 +08:00
137 lines
3.5 KiB
Java
137 lines
3.5 KiB
Java
|
/*
|
||
|
Copyright (c) 2004, 2006 Pablo Bleyer Kocik.
|
||
|
|
||
|
Redistribution and use in source and binary forms, with or without
|
||
|
modification, are permitted provided that the following conditions are met:
|
||
|
|
||
|
1. Redistributions of source code must retain the above copyright notice, this
|
||
|
list of conditions and the following disclaimer.
|
||
|
|
||
|
2. Redistributions in binary form must reproduce the above copyright notice,
|
||
|
this list of conditions and the following disclaimer in the documentation
|
||
|
and/or other materials provided with the distribution.
|
||
|
|
||
|
3. The name of the author may not be used to endorse or promote products
|
||
|
derived from this software without specific prior written permission.
|
||
|
|
||
|
THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
|
||
|
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||
|
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
|
||
|
EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||
|
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||
|
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
|
||
|
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
|
||
|
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||
|
POSSIBILITY OF SUCH DAMAGE.
|
||
|
*/
|
||
|
|
||
|
import java.util.Vector;
|
||
|
import java.util.Date;
|
||
|
|
||
|
/** PicoBlaze-1 assembler. */
|
||
|
public class Assembler1 extends Assembler {
|
||
|
|
||
|
/** Instruction factory. */
|
||
|
private static Instruction1 insn;
|
||
|
// 1<<8 program size, 2 char address, 4 char data
|
||
|
public static int
|
||
|
PROGRAM_SIZE = 1<<8,
|
||
|
ADDRESS_CHARS = 2,
|
||
|
CODE_CHARS = 4; // 16/4 nibbles
|
||
|
|
||
|
public Assembler1() {
|
||
|
super(PROGRAM_SIZE, ADDRESS_CHARS, CODE_CHARS);
|
||
|
if (insn == null) insn = new Instruction1();
|
||
|
}
|
||
|
|
||
|
/** Create new assembler object and process expressions.
|
||
|
@param v vector of assembly expressions
|
||
|
*/
|
||
|
public Assembler1(Vector<AsmExpression> v) {
|
||
|
super(PROGRAM_SIZE, ADDRESS_CHARS, CODE_CHARS);
|
||
|
if (insn == null) insn = new Instruction1();
|
||
|
|
||
|
parse(v);
|
||
|
assemble(v);
|
||
|
}
|
||
|
|
||
|
public int code(String n, Vector a) {
|
||
|
return insn.code(n, a, environment);
|
||
|
}
|
||
|
|
||
|
public String toBlockRAM(String name) {
|
||
|
StringBuffer b = new StringBuffer();
|
||
|
|
||
|
b.append(
|
||
|
"/*\n" +
|
||
|
" * " + name + " generated by KCAsm at " + (new Date()).toString() + "\n" +
|
||
|
" */\n\n" +
|
||
|
|
||
|
"module " + name + "(address, instruction, clk);\n"
|
||
|
);
|
||
|
b.append(
|
||
|
"input [7:0] address;\n" +
|
||
|
"output [15:0] instruction;\n" +
|
||
|
"input clk;\n" +
|
||
|
"RAMB4_S16 ram_256_x_16(\n" +
|
||
|
" .DI (16'b0),\n" +
|
||
|
" .EN (1'b1),\n" +
|
||
|
" .WE (1'b0),\n" +
|
||
|
" .RST (1'b0),\n" +
|
||
|
" .CLK (clk),\n" +
|
||
|
" .ADDR (address),\n" +
|
||
|
" .DO (instruction)\n" +
|
||
|
");\n" +
|
||
|
"// synthesis translate_off\n"
|
||
|
);
|
||
|
|
||
|
String[] hn = new String[16];
|
||
|
|
||
|
for (int i=0; i<16; ++i) {
|
||
|
String h = "";
|
||
|
for (int j=0; j<16; ++j) h = String.format("%04X", program[j+i*16]) + h;
|
||
|
hn[i] = h;
|
||
|
}
|
||
|
|
||
|
b.append(
|
||
|
"/*\tsynthesis\n"
|
||
|
);
|
||
|
for (int i=0; i<16; ++i)
|
||
|
b.append(
|
||
|
"\tinit_" +
|
||
|
String.format("%02X", i) + " = \"" + hn[i] + "\"\n"
|
||
|
);
|
||
|
b.append(
|
||
|
"*/\n"
|
||
|
);
|
||
|
|
||
|
b.append(
|
||
|
"// synthesis translate_off\n"
|
||
|
);
|
||
|
|
||
|
for (int i=0; i<16; ++i)
|
||
|
b.append(
|
||
|
"defparam ram_256_x_16.INIT_" +
|
||
|
String.format("%02X", i) + " = 256'h" + hn[i] + ";\n"
|
||
|
);
|
||
|
|
||
|
b.append(
|
||
|
"// synthesis translate_on\n"
|
||
|
);
|
||
|
|
||
|
for (int i=0; i<16; ++i)
|
||
|
b.append(
|
||
|
"// synthesis attribute INIT_" +
|
||
|
String.format("%02X", i) + " of ram_256_x_16 is \"" +
|
||
|
hn[i] + "\"\n"
|
||
|
);
|
||
|
|
||
|
b.append(
|
||
|
"endmodule\n"
|
||
|
);
|
||
|
|
||
|
return b.toString();
|
||
|
}
|
||
|
}
|