Quartus II Ver.15.0以上 でプロジェクトファイルsjr_vga_example_de0_cv.qpfを開いて「Start Compilation」、「Programmer」で転送して実行します。
ソースコード
これらのソースコードはBSD 2-Clauseライセンスで公開します。
vga_vram_8.v : VGA出力モジュール本体
/*
Copyright (c) 2015, miya
All rights reserved.
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.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 COPYRIGHT OWNER OR CONTRIBUTORS 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.
*/
module vga_vram_8
(
input clk,
input reset,
output signed [32-1 : 0] data_length,
input signed [32-1 : 0] data_address,
input signed [8-1 : 0] data_din,
output signed [8-1 : 0] data_dout,
input data_we,
input data_oe,
output vsync,
input signed [32-1 : 0] offset_h,
input signed [32-1 : 0] offset_v,
input ext_clkv,
input ext_resetv,
output ext_vga_hs,
output ext_vga_vs,
output ext_vga_de,
output signed [8-1 : 0] ext_vga_r,
output signed [8-1 : 0] ext_vga_g,
output signed [8-1 : 0] ext_vga_b
);
// H counter
always @(posedge ext_clkv)
begin
if (ext_resetv == 1'b1)
begin
count_h <= 1'd0;
end
else
begin
if (count_h == VGA_MAX_H)
begin
count_h <= 1'd0;
end
else
begin
count_h <= count_h + 1'd1;
end
end
end
// V counter
always @(posedge ext_clkv)
begin
if (ext_resetv == 1'b1)
begin
count_v <= 1'd0;
end
else
begin
if (count_h == VGA_MAX_H)
begin
if (count_v == VGA_MAX_V)
begin
count_v <= 1'd0;
end
else
begin
count_v <= count_v + 1'd1;
end
end
end
end
// viewport
always @(posedge ext_clkv)
begin
if (ext_resetv == 1'b1)
begin
count_hp <= 1'd0;
count_vp <= 1'd0;
end
else
begin
count_hp <= count_h + offset_h_sync;
count_vp <= count_v + offset_v_sync;
end
end
/*
Copyright (c) 2015-2016, miya
All rights reserved.
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.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 COPYRIGHT OWNER OR CONTRIBUTORS 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.
*/
// latency: 2 clk_in cycles + 3 clk_out cycles // data_in value must be held for 4 clk_out cycles
module cdc_synchronizer
#(
parameter DATA_WIDTH=8
)
(
input clk_in,
input clk_out,
input [(DATA_WIDTH-1):0] data_in,
output [(DATA_WIDTH-1):0] data_out,
input reset_in
);
always @(posedge clk_in)
begin
if (reset_in == 1'b1)
begin
change_flag_in <= 1'b0;
end
else if (data_in_reg != data_in)
begin
change_flag_in <= ~change_flag_in;
end
data_in_reg <= data_in;
end
always @(posedge clk_out)
begin
if (change_flag_out[2] == change_flag_out[1])
begin
data_out_reg[2] <= data_out_reg[1];
end
end
always @(posedge clk_out)
begin
change_flag_out <= {change_flag_out[1:0], change_flag_in};
data_out_reg[1] <= data_out_reg[0];
data_out_reg[0] <= data_in_reg;
end
assign data_out = data_out_reg[2];
endmodule
VgaVram8.java : VGA出力モジュールのSynthesijer用ラッパークラス
/*
Copyright (c) 2015, miya
All rights reserved.
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.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 COPYRIGHT OWNER OR CONTRIBUTORS 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.
*/
public class VgaVram8 extends HDLModule
{ // フィールド定義。下のポート仕様で同じ名前のポートを記述するとJava側から読み書きできるレジスタになる。
byte[] data;
boolean vsync;
int offset_h;
int offset_v;
public VgaVram8(String... args)
{ // "vga_vram_8"は実体のHDLのモジュール名
super("vga_vram_8", "clk", "reset");
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.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 COPYRIGHT OWNER OR CONTRIBUTORS 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.
*/
public class Sjr_VGA_Test extends Thread
{
private final VgaVram8 vram = new VgaVram8();
private int calc_page;
private int draw_page;
private int color;
private byte cell[] = new byte[8192 /*all_page_size*/];
private void pset(int x, int y, int color)
{
vram.data[(y << 6) + x] = (byte)color;
}
private int get_index(int x, int y, int page)
{
return (x & 63 /*cell_size_minus_1*/) + ((y & 63 /*cell_size_minus_1*/) << 6 /*cell_size_bits*/) + (page << 12 /*page_bits*/);
}
private byte get_cell(int x, int y, int page)
{
return cell[get_index(x, y, page)];
}
private void set_cell(int x, int y, int page, byte value)
{
cell[get_index(x, y, page)] = value;
}
private int get_neighbor(int x, int y, int p)
{
int neighbor =
get_cell(x-1,y-1,p)+get_cell(x,y-1,p)+get_cell(x+1,y-1,p)+
get_cell(x-1,y,p)+ get_cell(x+1,y,p)+
get_cell(x-1,y+1,p)+get_cell(x,y+1,p)+get_cell(x+1,y+1,p);
return neighbor;
}
private void calc_cells()
{
for (int y = 0; y < 64 /*cell_size*/; y++)
{
for (int x = 0; x < 64 /*cell_size*/; x++)
{
byte me = get_cell(x, y, calc_page);
set_cell(x, y, draw_page, me);
int neighbor = get_neighbor(x, y , calc_page);
if (me == (byte)0)
{
if (neighbor == 3)
{
set_cell(x, y, draw_page, (byte)1);
}
}
else
{
if ((neighbor < 2) || (neighbor > 3))
{
set_cell(x, y, draw_page, (byte)0);
}
}
}
}
}
private void clear_screen()
{
for (int i = 0; i < 4096; i++)
{
vram.data[i] = (byte)0;
}
}
private void draw_cells()
{
for (int y = 0; y < 30 /*screen_height*/; y++)
{
for (int x = 0; x < 40 /*screen_width*/; x++)
{
int col;
if (get_cell(x + 12, y + 16, draw_page) == (byte)1)
{
col = color;
}
else
{
col = 0;
}
pset(x, y, col);
}
}
}
public void run()
{
init();
for (int i = 0; i < 512; i++)
{ // scroll
vram.offset_h = i - 256;
vram.offset_v = i - 256; // color shift
color = i;
calc_cells(); // vsync wait
while (vram.vsync == true)
{
yield();
}
while (vram.vsync == false)
{
yield();
}
draw_cells();
int p = calc_page;
calc_page = draw_page;
draw_page = p;
}
}
}
Sjr_Top.java : Java側トップモジュール
/*
Copyright (c) 2015, miya
All rights reserved.
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.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 COPYRIGHT OWNER OR CONTRIBUTORS 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 synthesijer.rt.*;
@synthesijerhdl
public class Sjr_Top
{
private final Sjr_VGA_Test obj = new Sjr_VGA_Test();
@auto
public void main()
{
obj.run();
}
}
sjr_vga_example_de0_cv.v : HDL側トップモジュール
/*
Copyright (c) 2015, miya
All rights reserved.
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.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 COPYRIGHT OWNER OR CONTRIBUTORS 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.
*/