-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvga_text_buffer.v
280 lines (261 loc) · 10.7 KB
/
vga_text_buffer.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
* Copyright 2018 Jacob Lifshay
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
`timescale 1ns / 100ps
module vga_text_buffer(
input pixel_clock,
input [15:0] screen_x,
input [15:0] screen_y,
input screen_valid,
output reg [7:0] screen_char,
input clk,
input tty_write,
input [7:0] tty_data,
output reg tty_busy
);
parameter font_x_size = 8;
parameter font_y_size = 8;
parameter screen_x_size = 800;
parameter screen_y_size = 600;
parameter text_x_size = screen_x_size / font_x_size;
parameter text_y_size = screen_y_size / font_y_size;
parameter ram_x_size = 128;
parameter ram_y_size = 128;
// ram_style = "block"
reg [7:0] text_ram[ram_x_size * ram_y_size - 1 : 0];
initial $readmemh("text_initial.hex", text_ram);
initial tty_busy = 1;
initial screen_char = 0;
reg [11:0] scroll_amount;
initial scroll_amount = 0;
always @(posedge pixel_clock) begin
screen_char <= screen_valid ? text_ram[ram_x_size * ((screen_y / font_y_size) + scroll_amount) + (screen_x / font_x_size)] : 0;
end
reg [11:0] cursor_x;
reg [11:0] cursor_y;
initial cursor_x = 0;
initial cursor_y = 0;
reg [2:0] state;
initial state = 0;
wire [11:0] cursor_x_after_tab_unwrapped = ((cursor_x >> 3) + 1) << 3;
wire [11:0] cursor_x_after_tab = (cursor_x_after_tab_unwrapped == text_x_size ? 0 : cursor_x_after_tab_unwrapped);
reg [15:0] text_ram_write_address;
reg text_ram_write_enable;
reg [7:0] text_ram_write_data;
always @(posedge clk) begin
if(text_ram_write_enable)
text_ram[text_ram_write_address] <= text_ram_write_data;
end
parameter space_char = 'h20;
parameter escape_char = 'h1B;
parameter left_bracket_char = 'h5B;
parameter capital_H_char = 'h48;
always @(posedge clk) begin
text_ram_write_enable = 0;
case(state)
0: begin
cursor_x <= 0;
cursor_y <= 0;
tty_busy <= 1;
state <= 1;
scroll_amount <= 0;
end
1: begin
if(cursor_x != ram_x_size - 1) begin
tty_busy <= 1;
text_ram_write_enable = 1;
text_ram_write_address = ram_x_size * (cursor_y + scroll_amount) + cursor_x;
text_ram_write_data = space_char;
cursor_x <= cursor_x + 1;
end
else if(cursor_y != ram_y_size - 1) begin
tty_busy <= 1;
text_ram_write_enable = 1;
text_ram_write_address = ram_x_size * (cursor_y + scroll_amount) + cursor_x;
text_ram_write_data = space_char;
cursor_x <= 0;
cursor_y <= cursor_y + 1;
end
else begin
text_ram_write_enable = 1;
text_ram_write_address = ram_x_size * (cursor_y + scroll_amount) + cursor_x;
text_ram_write_data = space_char;
tty_busy <= 0;
state <= 2;
cursor_x <= 0;
cursor_y <= 0;
end
end
2: begin
if(tty_write) begin
case (tty_data)
'h0A: begin
if(cursor_y != text_y_size - 1) begin
cursor_x <= 0;
cursor_y <= cursor_y + 1;
tty_busy <= 0;
end
else begin
cursor_x <= 0;
tty_busy <= 1;
state <= 3;
scroll_amount <= scroll_amount + 1;
end
end
'h1B: begin
tty_busy <= 0;
state <= 4;
end
'h0D: begin
cursor_x <= 0;
tty_busy <= 0;
end
'h09: begin
cursor_x <= cursor_x_after_tab;
tty_busy <= 0;
end
default: begin
text_ram_write_enable = 1;
text_ram_write_address = ram_x_size * (cursor_y + scroll_amount) + cursor_x;
text_ram_write_data = tty_data;
if(cursor_x != text_x_size - 1) begin
cursor_x <= cursor_x + 1;
tty_busy <= 0;
end
else if(cursor_y != text_y_size - 1) begin
cursor_x <= 0;
cursor_y <= cursor_y + 1;
tty_busy <= 0;
end
else begin
cursor_x <= 0;
cursor_y <= text_y_size - 1;
tty_busy <= 1;
state <= 3;
scroll_amount <= scroll_amount + 1;
end
end
endcase
end
else begin
tty_busy <= 0;
end
end
3: begin
if(cursor_x != ram_x_size - 1) begin
tty_busy <= 1;
text_ram_write_enable = 1;
text_ram_write_address = ram_x_size * (cursor_y + scroll_amount) + cursor_x;
text_ram_write_data = space_char;
cursor_x <= cursor_x + 1;
end
else begin
text_ram_write_enable = 1;
text_ram_write_address = ram_x_size * (cursor_y + scroll_amount) + cursor_x;
text_ram_write_data = space_char;
tty_busy <= 0;
state <= 2;
cursor_x <= 0;
end
end
4: begin
if(tty_write) begin
case (tty_data)
'h52: begin // "R": reset
tty_busy <= 1;
state <= 0;
end
left_bracket_char: begin
tty_busy <= 0;
state <= 5;
end
default: begin
text_ram_write_enable = 1;
text_ram_write_address = ram_x_size * (cursor_y + scroll_amount) + cursor_x;
text_ram_write_data = tty_data;
if(cursor_x != text_x_size - 1) begin
cursor_x <= cursor_x + 1;
tty_busy <= 0;
state <= 2;
end
else if(cursor_y != text_y_size - 1) begin
cursor_x <= 0;
cursor_y <= cursor_y + 1;
tty_busy <= 0;
state <= 2;
end
else begin
cursor_x <= 0;
cursor_y <= text_y_size - 1;
tty_busy <= 1;
state <= 3;
scroll_amount <= scroll_amount + 1;
end
end
endcase
end
else begin
tty_busy <= 0;
end
end
5: begin
if(tty_write) begin
case (tty_data)
capital_H_char: begin // move to top left
tty_busy <= 0;
state <= 2;
cursor_x <= 0;
cursor_y <= 0;
end
default: begin
text_ram_write_enable = 1;
text_ram_write_address = ram_x_size * (cursor_y + scroll_amount) + cursor_x;
text_ram_write_data = tty_data;
if(cursor_x != text_x_size - 1) begin
cursor_x <= cursor_x + 1;
tty_busy <= 0;
state <= 2;
end
else if(cursor_y != text_y_size - 1) begin
cursor_x <= 0;
cursor_y <= cursor_y + 1;
tty_busy <= 0;
state <= 2;
end
else begin
cursor_x <= 0;
cursor_y <= text_y_size - 1;
tty_busy <= 1;
state <= 3;
scroll_amount <= scroll_amount + 1;
end
end
endcase
end
else begin
tty_busy <= 0;
end
end
default: state <= 0;
endcase
end
endmodule