-
Notifications
You must be signed in to change notification settings - Fork 0
/
gfx_unit_bresenham.v
225 lines (195 loc) · 5.4 KB
/
gfx_unit_bresenham.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
* gfx_unit_bresenham.v
*
* copyright (c) 2021 hirosh dabui <hirosh@dabui.de>
*
* 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_bresenham(
input clk,
input and_fb,
input signed [7:0] x0,
input signed [7:0] y0,
input signed [7:0] x1,
input signed [7:0] y1,
input [9:0] ssd1306_addr,
output [7:0] ssd1306_out,
input rd,
output ready,
input resetn,
input render,
output reg done
);
localparam XSIZE = 128;
localparam YSIZE = 64;
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;
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 signed [7:0] x;
reg signed [7:0] y;
reg [5:0] state;
/* set pixel start */
wire [9:0] index = (((y>>3)<<7) | x);
//wire [9:0] index = (((y>>3)<<7) | x);
//wire [9:0] index = ((y & (8'hF8))<<4) | x;
//wire [9:0] index = {y[5:3], {7{1'b0}}} | x;
wire [$clog2(ADDR_DEPTH)-1:0] fb_rd_ptr = index;
wire [$clog2(ADDR_DEPTH)-1:0] fb_wr_ptr = fb_rd_ptr;
wire [7:0] pixel = (1<<(y&7));
assign in = and_fb ? out & ~pixel : out | pixel;
/* set pixel stop */
/* bresenham */
reg signed [8:0] dx;
reg signed [8:0] dy;
reg signed [8:0] xstep;
reg signed [8:0] ystep;
reg signed [8:0] eps;
wire signed [8:0] eps_dy = eps + (dy<<1);
wire signed [8:0] eps_dx = eps + (dx<<1);
/* ********* */
assign ready = !state;
always @(posedge clk) begin
if (~resetn) begin
done <= 1'b0;
we <= 0;
state <= 0;
cs <= 0;
x <= 0;
y <= 0;
dx <= 0;
dy <= 0;
eps <= 0;
xstep <= 0;
ystep <= 0;
end else begin
case (state)
0: begin
done <= 1'b0;
if (!render_r & render) begin
we <= 1'b0;
cs <= 1'b1;
dx <= x1 - x0;
dy <= y1 - y0;
xstep <= 1;
ystep <= 1;
state <= 1;
end else begin
we <= 1'b0;
cs <= 1'b0;
end
end
1: begin
if (dx < 0) begin
dx <= -dx;
xstep <= -1;
end
if (dy < 0) begin
dy <= -dy;
ystep <= -1;
end
x <= x0;
y <= y0;
state <= 13;
end
13: begin
if (dy <= dx) begin
eps <= -dx;
state <= 2;
end else begin
eps <= -dy;
state <= 10;
end
end
2: begin
if (x == x1) begin
cs <= 1'b0;
we <= 1'b0;
state <= 20;
end else begin
we <= 1'b0;
state <= 11;
end
if (eps_dy > 0) begin
y <= y + ystep;
eps <= eps_dy - (dx<<1);
end else begin
eps <= eps_dy;
end
x <= x + xstep;
end
11: begin
we <= 1'b1;
state <= 2;
end
10: begin
we <= 1'b1;
if (y == y1) begin
cs <= 1'b0;
we <= 1'b0;
state <= 20;
end else begin
we <= 1'b0;
state <= 12;
end
if (eps_dx > 0) begin
x <= x + ystep;
eps <= eps_dx - (dy<<1);
end else begin
eps <= eps_dx;
end
y <= y + xstep;
end
12: begin
we <= 1'b1;
state <= 10;
end
20: begin
done <= 1'b1;
state <= 0;
end
default:
state <= 0;
endcase
end
end
endmodule