`timescale 1ns / 1ps // 640x480 @ 60 Hz VGA text display: 80x30 cells of 8x16 pixels. // Runs on the 100 MHz clock with a 25 MHz pixel enable; the character RAM // and font ROM reads finish well inside the four clocks of each pixel. module text_display #( parameter FONT_FILE = "font.mem" ) ( input clk, input rst, input [4:0] top, // physical row shown at the top of the screen input [4:0] cur_row, input [6:0] cur_col, input cur_vis, input [11:0] fg, // 4:4:4 RGB input [11:0] bg, output [11:0] ram_addr, input [8:0] ram_data, // {reverse, code}, one clock after ram_addr output reg [3:0] red, output reg [3:0] green, output reg [3:0] blue, output reg hsync, output reg vsync ); localparam H_VIS = 640, H_FP = 16, H_SYNC = 96, H_TOTAL = 800; localparam V_VIS = 480, V_FP = 10, V_SYNC = 2, V_TOTAL = 525; reg [1:0] div = 2'd0; reg [9:0] h, v; reg [5:0] frame = 6'd0; wire tick = div == 2'd3; always @(posedge clk) begin div <= rst ? 2'd0 : div + 1'b1; if (rst) begin h <= 0; v <= 0; end else if (tick) begin if (h == H_TOTAL - 1) begin h <= 0; if (v == V_TOTAL - 1) begin v <= 0; frame <= frame + 1'b1; end else v <= v + 1'b1; end else h <= h + 1'b1; end end // character cell under the beam wire [6:0] tcol = h[9:3]; wire [4:0] trow = v[8:4]; wire [5:0] psum = trow + top; wire [4:0] prow = psum >= 6'd30 ? psum - 6'd30 : psum; assign ram_addr = {1'b0, prow, 6'b0} + {3'b0, prow, 4'b0} + {5'b0, tcol}; reg [7:0] font [0:4095]; reg [7:0] glyph; initial $readmemh(FONT_FILE, font); always @(posedge clk) glyph <= font[{ram_data[7:0], v[3:0]}]; wire visible = h < H_VIS && v < V_VIS; wire cursor = cur_vis && frame[5] && tcol == cur_col && trow == cur_row; wire on = glyph[~h[2:0]] ^ ram_data[8] ^ cursor; always @(posedge clk) begin if (tick) begin {red, green, blue} <= !visible ? 12'h000 : on ? fg : bg; hsync <= !(h >= H_VIS + H_FP && h < H_VIS + H_FP + H_SYNC); vsync <= !(v >= V_VIS + V_FP && v < V_VIS + V_FP + V_SYNC); end end endmodule // 2400-cell character buffer: write port for the terminal, read port for video. module char_ram ( input clk, input we, input [11:0] waddr, input [8:0] wdata, input [11:0] raddr, output reg [8:0] rdata ); (* ram_style = "block" *) reg [8:0] mem [0:4095]; integer i; initial for (i = 0; i < 4096; i = i + 1) mem[i] = 9'h020; always @(posedge clk) begin if (we) mem[waddr] <= wdata; rdata <= mem[raddr]; end endmodule