-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgfx_unit.v
150 lines (136 loc) · 3.85 KB
/
gfx_unit.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
* gfx_unit.v
*
* copyright (c) 2021 hirosh dabui <[email protected]>
*
* permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* the software is provided "as is" and the author disclaims all warranties
* with regard to this software including all implied warranties of
* merchantability and fitness. in no event shall the author be liable for
* any special, direct, indirect, or consequential damages or any damages
* whatsoever resulting from loss of use, data or profits, whether in an
* action of contract, negligence or other tortious action, arising out of
* or in connection with the use or performance of this software.
*
*/
`timescale 1 ns/10 ps
`default_nettype none
module gfx_unit(
input clk,
input and_fb,
input [7:0] font,
input [3:0] x, // 0 - 7
input [0:0] y, // 0 - 3
input [8:0] ssd1306_addr,
output [7:0] ssd1306_out,
input rd,
output ready,
input resetn,
input render,
output reg done
);
localparam XSIZE = 128;
localparam YSIZE = 32;
localparam ADDR_DEPTH = XSIZE*YSIZE/8;
localparam DATA_WIDTH = 8;
wire [DATA_WIDTH-1:0] in;
wire [DATA_WIDTH-1:0] out;
reg cs;
reg we;
reg [7:0] font_bitmap[0:1023];//4095];
initial begin
$readmemh("font_vga.mem", font_bitmap); // 8x16*256
end
assign ssd1306_out = rd ? out : 'hz;
framebuffer
#(
.XSIZE(XSIZE),
.YSIZE(YSIZE),
.ADDR_DEPTH(ADDR_DEPTH),
.DATA_WIDTH(DATA_WIDTH)
) framebuffer_i(
.clk(clk),
.wr_addr(fb_wr_ptr),
.rd_addr(rd ? ssd1306_addr : fb_rd_ptr),
.in(in),
.out(out),
.cs(cs | rd),
.we(we)
);
reg render_r;
always @(posedge clk) begin
if (~resetn) begin
render_r <= 0;
end else begin
render_r <= render;
end
end
reg [3:0] state;
reg [8:0] i;
reg [8:0] offset;
wire [8:0] index = (x<<3) + (y<<8);
wire [11:0] font_index = (font<<4) + i;
wire [7:0] tile = font_bitmap[font_index];
wire [2:0] shift = (7-offset);
wire [7:0] bit_tile = ((tile & (1<<(shift)))>>(shift))<<(i&7);
wire [$clog2(ADDR_DEPTH)-1:0] fb_rd_ptr = index + offset + ((i>>3)<<7);
wire [$clog2(ADDR_DEPTH)-1:0] fb_wr_ptr = fb_rd_ptr;
assign in = and_fb ? out & bit_tile : out | bit_tile;
assign ready = !state;
always @(posedge clk) begin
if (~resetn) begin
done <= 1'b0;
offset <= 0;
we <= 0;
i <= 0;
state <= 0;
cs <= 0;
end else begin
case (state)
0: begin
done <= 1'b0;
if (!render_r & render) begin
i <= 0;
offset <= 0;
we <= 1'b0;
cs <= 1'b1;
state <= 1;
i <= 0;
end else begin
we <= 1'b0;
cs <= 1'b0;
end
end
/* font rederer start */
1: begin
if (i == 16) begin
we <= 1'b0;
cs <= 1'b0;
done <= 1'b1;
state <= 0;
end else begin
we <= 1'b1;
state <= 2;
end
end
2: begin
we <= 1'b0;
offset <= offset + 1;
if (offset == 7) begin
offset <= 0;
i <= i + 1;
state <= 1;
end else begin
state <= 1;
end
end
/* font rederer stop */
default:
state <= 0;
endcase
end
end
endmodule