2018-02-27 04:48:36 +00:00
|
|
|
`include "hvsync_generator.v"
|
|
|
|
`include "font_cp437_8x8.v"
|
|
|
|
`include "ram.v"
|
|
|
|
|
2018-02-28 17:09:29 +00:00
|
|
|
module tile_renderer(clk, reset, hpos, vpos,
|
2018-02-27 04:48:36 +00:00
|
|
|
rgb,
|
2018-02-28 03:35:42 +00:00
|
|
|
ram_addr, ram_read, ram_busy,
|
2018-02-27 04:48:36 +00:00
|
|
|
rom_addr, rom_data);
|
|
|
|
|
|
|
|
input clk, reset;
|
|
|
|
input [8:0] hpos;
|
|
|
|
input [8:0] vpos;
|
|
|
|
output [3:0] rgb;
|
|
|
|
|
|
|
|
output reg [15:0] ram_addr;
|
2018-02-28 03:35:42 +00:00
|
|
|
input [15:0] ram_read;
|
|
|
|
output reg ram_busy;
|
2018-02-27 04:48:36 +00:00
|
|
|
|
|
|
|
output [10:0] rom_addr;
|
|
|
|
input [7:0] rom_data;
|
|
|
|
|
2018-02-28 03:35:42 +00:00
|
|
|
reg [7:0] page_base = 8'h7e; // page table base (8 bits)
|
2018-02-27 04:48:36 +00:00
|
|
|
reg [15:0] row_base; // row table base (16 bits)
|
2018-02-28 17:09:29 +00:00
|
|
|
reg [4:0] row;
|
|
|
|
//wire [4:0] row = vpos[7:3]; // 5-bit row, vpos / 8
|
2018-02-27 04:48:36 +00:00
|
|
|
wire [4:0] col = hpos[7:3]; // 5-bit column, hpos / 8
|
|
|
|
wire [2:0] yofs = vpos[2:0]; // scanline of cell (0-7)
|
|
|
|
wire [2:0] xofs = hpos[2:0]; // which pixel to draw (0-7)
|
|
|
|
|
2018-02-28 17:09:29 +00:00
|
|
|
reg [15:0] cur_cell;
|
|
|
|
wire [7:0] cur_char = cur_cell[7:0];
|
|
|
|
wire [7:0] cur_attr = cur_cell[15:8];
|
2018-02-27 04:48:36 +00:00
|
|
|
|
|
|
|
// tile ROM address
|
2018-02-28 17:09:29 +00:00
|
|
|
assign rom_addr = {cur_char, yofs};
|
2018-02-28 03:35:42 +00:00
|
|
|
|
|
|
|
reg [15:0] row_buffer[0:31];
|
2018-02-28 17:09:29 +00:00
|
|
|
|
2018-02-27 04:48:36 +00:00
|
|
|
// lookup char and attr
|
2018-02-28 03:35:42 +00:00
|
|
|
always @(posedge clk) begin
|
2018-02-28 17:09:29 +00:00
|
|
|
// reset row to 0 when last row displayed
|
|
|
|
if (vpos == 248) begin
|
|
|
|
row <= 0;
|
|
|
|
end
|
2018-02-28 03:35:42 +00:00
|
|
|
// time to read a row?
|
|
|
|
if (vpos[2:0] == 7) begin
|
|
|
|
// read row_base from page table (2 bytes)
|
2018-02-27 04:48:36 +00:00
|
|
|
case (hpos[7:0])
|
2018-02-28 17:09:29 +00:00
|
|
|
185: ram_busy <= 1;
|
2018-02-28 03:35:42 +00:00
|
|
|
190: ram_addr <= {page_base, 3'b000, row};
|
|
|
|
192: row_base <= ram_read;
|
2018-02-28 17:09:29 +00:00
|
|
|
192+32: begin
|
|
|
|
ram_busy <= 0;
|
|
|
|
row <= row + 1;
|
|
|
|
end
|
2018-02-27 04:48:36 +00:00
|
|
|
endcase
|
2018-02-28 03:35:42 +00:00
|
|
|
// load row of tile data from RAM
|
2018-02-28 17:09:29 +00:00
|
|
|
// (last two twice)
|
|
|
|
if (hpos >= 192 && hpos < 192+34) begin
|
2018-02-28 03:35:42 +00:00
|
|
|
ram_addr <= row_base + 16'(hpos[4:0]);
|
|
|
|
row_buffer[hpos[4:0]-2] <= ram_read;
|
|
|
|
end
|
|
|
|
end
|
|
|
|
// latch character data
|
|
|
|
if (hpos < 256) begin
|
2018-02-27 04:48:36 +00:00
|
|
|
case (hpos[2:0])
|
|
|
|
7: begin
|
2018-02-28 17:09:29 +00:00
|
|
|
cur_cell <= row_buffer[col+1];
|
2018-02-27 04:48:36 +00:00
|
|
|
end
|
|
|
|
endcase
|
2018-02-28 17:09:29 +00:00
|
|
|
end else if (hpos == 308) begin
|
|
|
|
cur_cell <= row_buffer[0];
|
2018-02-27 04:48:36 +00:00
|
|
|
end
|
2018-02-28 03:35:42 +00:00
|
|
|
end
|
2018-02-27 04:48:36 +00:00
|
|
|
|
|
|
|
// extract bit from ROM output
|
2018-02-28 17:09:29 +00:00
|
|
|
assign rgb = rom_data[~xofs] ? cur_attr[3:0] : cur_attr[7:4];
|
2018-02-27 04:48:36 +00:00
|
|
|
|
|
|
|
endmodule
|
|
|
|
|
|
|
|
module test_tilerender_top(clk, reset, hsync, vsync, rgb);
|
|
|
|
|
|
|
|
input clk, reset;
|
|
|
|
output hsync, vsync;
|
|
|
|
output [3:0] rgb;
|
|
|
|
|
|
|
|
wire display_on;
|
|
|
|
wire [8:0] hpos;
|
|
|
|
wire [8:0] vpos;
|
|
|
|
|
|
|
|
reg [15:0] ram_addr;
|
2018-02-28 03:35:42 +00:00
|
|
|
wire [15:0] ram_read;
|
|
|
|
reg [15:0] ram_write = 0;
|
2018-02-27 04:48:36 +00:00
|
|
|
reg ram_writeenable = 0;
|
|
|
|
|
|
|
|
wire [10:0] rom_addr;
|
|
|
|
wire [7:0] rom_data;
|
2018-02-28 03:35:42 +00:00
|
|
|
wire ram_busy;
|
2018-02-27 04:48:36 +00:00
|
|
|
|
|
|
|
hvsync_generator hvsync_gen(
|
|
|
|
.clk(clk),
|
|
|
|
.reset(reset),
|
|
|
|
.hsync(hsync),
|
|
|
|
.vsync(vsync),
|
|
|
|
.display_on(display_on),
|
|
|
|
.hpos(hpos),
|
|
|
|
.vpos(vpos)
|
|
|
|
);
|
|
|
|
|
|
|
|
// RAM
|
2018-02-28 03:35:42 +00:00
|
|
|
RAM_sync #(16,16) ram(
|
2018-02-27 04:48:36 +00:00
|
|
|
.clk(clk),
|
|
|
|
.dout(ram_read),
|
|
|
|
.din(ram_write),
|
|
|
|
.addr(ram_addr),
|
|
|
|
.we(ram_writeenable)
|
|
|
|
);
|
2018-02-28 17:09:29 +00:00
|
|
|
|
|
|
|
wire [3:0] rgb_tile;
|
|
|
|
|
2018-02-27 04:48:36 +00:00
|
|
|
tile_renderer tile_gen(
|
|
|
|
.clk(clk),
|
|
|
|
.reset(reset),
|
|
|
|
.hpos(hpos),
|
|
|
|
.vpos(vpos),
|
|
|
|
.ram_addr(ram_addr),
|
|
|
|
.ram_read(ram_read),
|
2018-02-28 03:35:42 +00:00
|
|
|
.ram_busy(ram_busy),
|
2018-02-27 04:48:36 +00:00
|
|
|
.rom_addr(rom_addr),
|
|
|
|
.rom_data(rom_data),
|
2018-02-28 17:09:29 +00:00
|
|
|
.rgb(rgb_tile)
|
2018-02-27 04:48:36 +00:00
|
|
|
);
|
2018-02-28 17:09:29 +00:00
|
|
|
|
|
|
|
assign rgb = display_on ? rgb_tile : rgb_tile|8;
|
2018-02-27 04:48:36 +00:00
|
|
|
|
|
|
|
// tile ROM
|
|
|
|
font_cp437_8x8 tile_rom(
|
|
|
|
.addr(rom_addr),
|
|
|
|
.data(rom_data)
|
|
|
|
);
|
2018-02-28 17:09:29 +00:00
|
|
|
|
|
|
|
initial begin
|
|
|
|
for (int i=0; i<32; i++) begin
|
|
|
|
ram.mem[16'h7e00 + 16'(i)] = 16'(i*32);
|
|
|
|
ram.mem[16'(i*32)] = 16'hfa1b;
|
|
|
|
ram.mem[16'(i*32+31)] = 16'hfb1a;
|
|
|
|
ram.mem[16'(i)] = 16'hfc18;
|
|
|
|
ram.mem[16'(28*32+i)] = 16'hfd19;
|
|
|
|
end
|
|
|
|
end
|
2018-02-27 04:48:36 +00:00
|
|
|
|
|
|
|
endmodule
|