-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathvga.vhd
1465 lines (1310 loc) · 46.1 KB
/
vga.vhd
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-------------------------------------------------------------------------------
--| @file vga.vhd
--| @brief Text Mode Video Controller VHDL Module and VT100
--| Terminal Emulator
--| @author Javier Valcarce García
--| @author Richard James Howe (vt100, vt100_tb, atoi, color attributes)
--| @copyright Copyright 2007 Javier Valcarce García, 2017, 2019 Richard James Howe
--| @license LGPL version 3
--| @email javier.valcarce@gmail.com
--| @note (Modifications and repackaging by Richard James Howe)
--|
--| This is a modified version of the text terminal available at
--| <https://opencores.org/project,interface_vga80x40>. Additions include per
--| character attribute information (color, bold, reverse video, blink text)
--| and a VT100 terminal interface.
--|
--| See also <http://www.javiervalcarce.eu/html/vhdl-vga80x40-en.html>.
--|
-------------------------------------------------------------------------------
----- VGA Package -------------------------------------------------------------
library ieee, work;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
use work.util.common_generics;
package vga_pkg is
type vga_physical_interface is record
red: std_ulogic_vector(2 downto 0);
green: std_ulogic_vector(2 downto 0);
blue: std_ulogic_vector(1 downto 0);
hsync: std_ulogic;
vsync: std_ulogic;
end record;
type vga_control_registers_we_interface is record
crx: std_ulogic; -- Write enable for cursor X position register
cry: std_ulogic; -- Write enable for VGA control register
ctl: std_ulogic; -- Write enable for cursor Y position register
end record;
type vga_control_registers_interface is record
crx: std_ulogic_vector(6 downto 0); -- Cursor position X
cry: std_ulogic_vector(5 downto 0); -- Cursor position Y
ctl: std_ulogic_vector(4 downto 0); -- Control register
end record;
constant vga_control_registers_initialize: vga_control_registers_interface := (
cry => (others => '0'),
crx => (others => '0'),
ctl => (others => '0'));
constant vga_control_registers_we_initialize: vga_control_registers_we_interface := (
cry => '0',
crx => '0',
ctl => '0');
component vga_top is
generic(g: common_generics);
port(
clk: in std_ulogic;
clk25MHz: in std_ulogic;
rst: in std_ulogic;
-- VGA Text buffer interface
vga_we_ram: in std_ulogic; -- Write enable RAM
vga_din: in std_ulogic_vector(15 downto 0);
vga_addr: in std_ulogic_vector(12 downto 0);
base: in std_ulogic_vector(12 downto 0);
-- VGA control registers
i_font_sel: in std_ulogic_vector(0 downto 0);
i_vga_control_we: in vga_control_registers_we_interface;
i_vga_control: in vga_control_registers_interface;
o_vga: out vga_physical_interface);
end component;
component vga_core is
generic(g: common_generics);
port (
rst: in std_ulogic;
clk25MHz: in std_ulogic;
text_a: out std_ulogic_vector(11 downto 0); -- text buffer
text_d: in std_ulogic_vector(15 downto 0);
font_a: out std_ulogic_vector(12 downto 0); -- font buffer
font_d: in std_ulogic_vector( 7 downto 0);
font_sel: in std_ulogic_vector(0 downto 0);
--
ocrx: in std_ulogic_vector(6 downto 0);
ocry: in std_ulogic_vector(5 downto 0);
octl: in std_ulogic_vector(4 downto 0);
--
o_vga: out vga_physical_interface);
end component;
component losr is
generic (g: common_generics; N: positive := 4);
port
(
rst: in std_ulogic;
clk: in std_ulogic;
load: in std_ulogic;
ce: in std_ulogic;
do: out std_ulogic := '0';
di: in std_ulogic_vector(N - 1 downto 0));
end component;
component ctrm is
generic (g: common_generics; M: positive := 8);
port (
rst: in std_ulogic; -- asynchronous rst
clk: in std_ulogic;
ce: in std_ulogic; -- enable counting
rs: in std_ulogic; -- synchronous rst
do: out integer range (M-1) downto 0 := 0);
end component;
component vt100 is
generic (g: common_generics);
port(
clk: in std_ulogic;
clk25MHz: in std_ulogic;
rst: in std_ulogic;
we: in std_ulogic;
char: in std_ulogic_vector(7 downto 0);
busy: out std_ulogic;
o_vga: out vga_physical_interface);
end component;
-- VGA test bench, not-synthesizeable
component vt100_tb is
generic(g: common_generics);
end component;
component atoi is
generic(g: common_generics; N: positive := 16);
port(
clk: in std_ulogic;
rst: in std_ulogic;
we: in std_ulogic;
init: in std_ulogic;
char: in std_ulogic_vector(7 downto 0);
char_o: out std_ulogic_vector(7 downto 0);
done_o: out std_ulogic;
-- ready: out std_ulogic;
n_o: out unsigned(N - 1 downto 0));
end component;
end package;
----- VGA Package -------------------------------------------------------------
----- VGA Test Bench ----------------------------------------------------------
library ieee, work;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
use ieee.math_real.all;
use work.vga_pkg.all;
use work.util.all;
entity vt100_tb is
generic(g: common_generics);
end entity;
architecture behav of vt100_tb is
constant clock_period: time := 1000 ms / g.clock_frequency;
signal clk, rst: std_ulogic := '0';
signal stop: std_ulogic := '0';
signal clk25MHz, rst25MHz: std_ulogic := '0';
signal char: std_ulogic_vector(7 downto 0);
signal we: std_ulogic := '0';
signal busy: std_ulogic := '0';
signal physical: vga_physical_interface;
-- NB. 'string' range is (1 to X), not (X-1 downto 0)
-- HT = Horizontal Tab
-- LF = Line Feed
-- CR = Carriage Return
-- ESC = Escape
constant CSI: string := ESC & "[";
constant RESET: string := CSI & "0m";
constant RED: string := CSI & "31m";
constant GREEN: string := CSI & "32m";
constant NL: string := CR & LF;
constant test_string: string := "Hello!" & HT & "World!" & RED & NL & "How are you?" & RESET & NL ;
shared variable test_vector: ulogic_string(test_string'range) := to_std_ulogic_vector(test_string);
shared variable index: integer := 1; -- starts at '1' due to string range
constant g_cs25MHz: common_generics := (clock_frequency => 25_000_000, asynchronous_reset => g.asynchronous_reset, delay => g.delay);
begin
cs: entity work.clock_source_tb
generic map(g => g, hold_rst => 2)
port map(stop => stop, clk => clk, rst => rst);
cs25MHz: entity work.clock_source_tb
generic map(g => g_cs25MHz, hold_rst => 2)
port map(stop => stop, clk => clk25MHz, rst => rst25MHz);
uut: work.vga_pkg.vt100
generic map(g => g)
port map(
clk => clk,
clk25MHz => clk25MHz,
rst => rst,
we => we,
char => char,
busy => busy,
o_vga => physical);
stimulus_process: process
begin
char <= test_vector(index);
we <= '0';
wait for clock_period * 20;
for i in test_vector'range loop
we <= '1';
char <= test_vector(index);
wait for clock_period;
we <= '0';
wait for clock_period;
while busy = '1' loop
wait until busy = '0';
wait for clock_period;
end loop;
-- wait for clock_period * 20;
-- wait until busy = '0';
-- report integer'image(index);
if index < (test_vector'high - 1) then
index := index + 1;
else
index := 1;
end if;
end loop;
stop <= '1';
wait;
end process;
end architecture;
----- VGA Test Bench ----------------------------------------------------------
----- ATOI --------------------------------------------------------------------
-- The purpose of this module is to read in numeric characters ('0' through
-- '9') and convert the string into a binary number ('n_o'). On the first non-
-- numeric character the module stops and outputs that non-numeric character
-- as well as the converted number string. The module waits in the FINISHED
-- state until the module is reset by an external signal ('init').
--
-- The module is named after the C function, 'atoi', for converting a string
-- into an integer.
--
library ieee,work;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
use work.vga_pkg.all;
use work.util.common_generics;
entity atoi is
generic(g: common_generics; N: positive := 16);
port(
clk: in std_ulogic;
rst: in std_ulogic;
we: in std_ulogic;
init: in std_ulogic;
char: in std_ulogic_vector(7 downto 0);
char_o: out std_ulogic_vector(7 downto 0);
done_o: out std_ulogic;
-- ready: out std_ulogic;
n_o: out unsigned(N - 1 downto 0));
end entity;
architecture rlt of atoi is
type state_type is (RESET, WAITING, COMMAND, ACCUMULATE, WRITE, FINISHED);
signal state_c, state_n: state_type := RESET;
signal c_c, c_n: unsigned(char'range) := (others => '0');
signal n_c, n_n: unsigned(n_o'range) := (others => '0');
signal f_c, f_n: boolean := true;
begin
char_o <= std_ulogic_vector(c_c);
n_o <= n_c;
-- ready <= '1' when state_c = WAITING else '0';
process(clk, rst)
variable akk: unsigned((2 * N) - 1 downto 0) := (others => '0');
begin
if rst = '1' and g.asynchronous_reset then
state_n <= RESET;
elsif rising_edge(clk) then
if rst = '1' and not g.asynchronous_reset then
state_n <= RESET;
else
state_c <= state_n;
c_c <= c_n;
n_c <= n_n;
f_c <= f_n;
done_o <= '0';
if state_c = RESET then
c_n <= (others => '0');
n_n <= (others => '0');
f_n <= true;
state_n <= WAITING;
elsif state_c = WAITING then
if we = '1' then
c_n <= unsigned(char);
state_n <= COMMAND;
end if;
elsif state_c = COMMAND then
state_n <= ACCUMULATE;
case c_c is
when x"30" =>
when x"31" =>
when x"32" =>
when x"33" =>
when x"34" =>
when x"35" =>
when x"36" =>
when x"37" =>
when x"38" =>
when x"39" =>
when others =>
state_n <= WRITE;
end case;
elsif state_c = ACCUMULATE then
if f_c then
f_n <= false;
n_n <= n_c + (c_c - x"30");
else
akk := n_c * to_unsigned(10, N);
n_n <= akk(N - 1 downto 0) + (c_c - x"30");
end if;
state_n <= WAITING;
elsif state_c = WRITE then
done_o <= '1';
state_n <= FINISHED;
elsif state_c = FINISHED then
null; -- wait for a reset
else
state_n <= RESET;
report "Invalid State" severity failure;
end if;
if init = '1' then
assert state_c = FINISHED report "Lost Conversion" severity error;
state_n <= RESET;
end if;
end if;
end if;
end process;
end architecture;
----- ATOI --------------------------------------------------------------------
----- VT100 Terminal Emulator -------------------------------------------------
-- This module contains the VGA module and wraps it in a terminal emulator,
-- based on the VT100 <https://en.wikipedia.org/wiki/VT100>, it only implements
-- a subset of the commands supplied by the VT100. This simplifies the usage
-- of the VGA text mode display, other VHDL components only have to write bytes
-- and do not have to worry about cursor position or implementing new lines,
-- tabs, and other very basic features.
--
-- The interface is designed to act like a UART, simply write a byte to it
-- and so long as the interface is not busy, it will be written to the screen
-- (or interpreter as part of a command sequence).
library ieee, work;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
use work.vga_pkg.all;
use work.util.common_generics;
entity vt100 is
generic(g: common_generics);
port(
clk: in std_ulogic;
clk25MHz: in std_ulogic;
rst: in std_ulogic;
we: in std_ulogic;
char: in std_ulogic_vector(7 downto 0);
busy: out std_ulogic;
o_vga: out vga_physical_interface);
end entity;
-- A better way of structuring this would be to process numbers in parallel
-- with different components, one processing potential attributes, one holding
-- one to two numbers - the results are then used. This would deal with runs
-- of attributes, which is allowed.
architecture rtl of vt100 is
constant width: positive := 80;
constant height: positive := 40;
constant number: positive := 8;
type state_type is (RESET, ACCEPT, NORMAL, WRAP, LIMIT, CSI, COMMAND,
NUMBER1, NUMBER2, COMMAND1, COMMAND2, WRITE, ERASING, ATTRIB, ADVANCE);
signal state_c, state_n: state_type := RESET;
constant tab: unsigned(char'range) := x"09";
constant backspace: unsigned(char'range) := x"08";
constant ascii_bell: unsigned(char'range) := x"07";
constant lf: unsigned(char'range) := x"0a";
constant cr: unsigned(char'range) := x"0d";
constant esc: unsigned(char'range) := x"1b";
constant blank: unsigned(char'range) := x"20";
constant asterisk: unsigned(char'range) := x"2A";
constant ascii_7: unsigned(char'range) := x"37"; -- '7'
constant ascii_8: unsigned(char'range) := x"38"; -- '8'
constant lsqb: unsigned(char'range) := x"5b"; -- '['
constant ascii_c: unsigned(char'range) := x"63"; -- 'c'
constant attr_default: unsigned(7 downto 0) := "00111000";
constant ctl_default: unsigned(4 downto 0) := "01111";
signal addr: std_ulogic_vector(12 downto 0) := (others => '0');
signal data_we: std_ulogic := '0';
signal x: std_ulogic_vector(6 downto 0) := (others => '0');
signal y: std_ulogic_vector(5 downto 0) := (others => '0');
signal cursor_we: std_ulogic := '0';
signal x_n, x_c: unsigned(x'range) := (others => '0');
signal y_n, y_c: unsigned(y'range) := (others => '0');
signal c_n, c_c: unsigned(char'range) := (others => '0');
signal attr_c, attr_n: unsigned(attr_default'range) := attr_default;
signal ctl_c, ctl_n: unsigned(ctl_default'range) := ctl_default;
signal n1_n, n1_c: unsigned(7 downto 0) := (others => '0');
signal n2_n, n2_c: unsigned(6 downto 0) := (others => '0');
signal conceal_n, conceal_c: boolean := false;
signal x_minus_one: unsigned(x'range) := (others => '0');
signal x_minus_one_limited: unsigned(x'range) := (others => '0');
signal x_underflow: boolean := false;
signal x_plus_one: unsigned(x'range) := (others => '0');
signal x_plus_one_limited: unsigned(x'range) := (others => '0');
signal x_overflow: boolean := false;
signal y_minus_one: unsigned(y'range) := (others => '0');
signal y_minus_one_limited: unsigned(y'range) := (others => '0');
signal y_underflow: boolean := false;
signal y_plus_one: unsigned(y'range) := (others => '0');
signal y_plus_one_limited: unsigned(y'range) := (others => '0');
signal y_overflow: boolean := false;
signal count_c, count_n: unsigned(addr'range) := (others => '0');
signal limit_c, limit_n: unsigned(addr'high - 3 downto 0) := (others => '0');
signal akk_done_o: std_ulogic := '0';
-- signal akk_ready_o: std_ulogic := '0';
signal akk_init: std_ulogic := '0';
signal n_o: unsigned(number - 1 downto 0) := (others => '0');
signal akk_char_o: std_ulogic_vector(char'range) := (others => '0');
signal font_sel_c, font_sel_n: std_ulogic_vector(0 downto 0) := (others => '0');
signal saved_x_n, saved_x_c: unsigned(x'range) := (others => '0');
signal saved_y_n, saved_y_c: unsigned(y'range) := (others => '0');
signal saved_attr_n, saved_attr_c: unsigned(attr_default'range) := (others => '0');
signal reverse_video_c, reverse_video_n: boolean := false;
signal base_n, base_c: unsigned(addr'high downto 4) := (others => '0');
signal addr_sel: unsigned(addr'range) := (others => '0');
signal saved_base_n, saved_base_c: unsigned(base_c'range) := (others => '0');
signal is_base_saved_n, is_base_saved_c: boolean := false;
begin
accumulator_0: work.vga_pkg.atoi
generic map(g => g, N => number)
port map(
clk => clk,
rst => rst,
we => we,
init => akk_init,
char => char,
char_o => akk_char_o,
done_o => akk_done_o,
-- ready => akk_ready_o,
n_o => n_o);
address: block
signal mul: unsigned(15 downto 0) := (others => '0');
signal addr_int: unsigned(addr'range) := (others => '0');
begin
mul <= to_unsigned(to_integer(y_c) * width, mul'length);
addr_int <= mul(addr_int'range) + ("000000" & x_c);
addr_sel <= addr_int when state_c /= ERASING else count_c;
addr <= std_ulogic_vector(addr_sel + (base_c & "0000"));
end block;
x_minus_one <= x_c - 1;
x_plus_one <= x_c + 1;
x_overflow <= x_c > (width - 1);
x_underflow <= x_minus_one >= (width - 1);
x_minus_one_limited <= (others => '0') when x_underflow else x_minus_one;
x_plus_one_limited <= to_unsigned(width - 1, x_c'length) when x_overflow else x_plus_one;
y_plus_one <= y_c + 1;
y_minus_one <= y_c - 1;
y_overflow <= y_c >= (height - 1); -- NB. > for 1 more row, slightly off edge of screen
y_underflow <= y_minus_one > (height - 1);
y_minus_one_limited <= (others => '0') when y_underflow else y_minus_one;
y_plus_one_limited <= to_unsigned(height - 1, y_c'length) when y_overflow else y_plus_one;
busy <= '1' when state_c = ERASING
or state_c = WRITE
or state_c = RESET
or state_c = WRAP
or state_c = ATTRIB
or state_c = ADVANCE
else '0';
vga_blk: block
signal vga_din: std_ulogic_vector(15 downto 0) := (others => '0');
signal vga_ctr_we: vga_control_registers_we_interface := vga_control_registers_we_initialize;
signal vga_ctr: vga_control_registers_interface := vga_control_registers_initialize;
signal attr: unsigned(attr_c'range) := attr_default;
signal ch: std_ulogic_vector(c_c'range) := (others => '0');
begin
ch <= std_ulogic_vector(asterisk) when conceal_c else std_ulogic_vector(c_c);
attr <= attr_c when state_c /= ERASING else attr_default;
vga_din <= std_ulogic_vector(attr) & ch;
vga_ctr.crx <= std_ulogic_vector(x_plus_one); -- not limited, goes off screen edge
vga_ctr.cry <= std_ulogic_vector(y_c);
vga_ctr.ctl <= std_ulogic_vector(ctl_c);
vga_ctr_we.crx <= cursor_we;
vga_ctr_we.cry <= cursor_we;
vga_ctr_we.ctl <= cursor_we;
vga_0: work.vga_pkg.vga_top
generic map(g => g)
port map(
clk => clk,
clk25MHz => clk25MHz,
rst => rst,
vga_we_ram => data_we,
vga_din => vga_din,
vga_addr => addr,
base(base_c'range)=> std_ulogic_vector(base_c),
base(3 downto 0) => "0000",
i_font_sel => font_sel_c,
i_vga_control_we => vga_ctr_we,
i_vga_control => vga_ctr,
o_vga => o_vga);
end block;
-- Subset of commands implemented:
-- ED - Erase Display, CSI n 'J'
-- RIS - Erase Display, ESC 'c'
-- SGR - Select Graphic Rendition - for colors, CSI n 'm'
-- HVP - Horizontal and Vertical Position - CSI n ; m 'f'
-- The cursor commands are also supported: CUU, CUD, CUF,
-- CUB, CNL, CPL and CHA
fsm: process(clk, rst)
variable limit_value: unsigned(addr'range) := (others => '0');
variable repeat: boolean := false;
variable exit_repeat: state_type := RESET;
procedure reverse_video(a: unsigned(2 downto 0); foreground: boolean) is
begin
if foreground then
if reverse_video_c then
attr_n(2 downto 0) <= a;
else
attr_n(5 downto 3) <= a;
end if;
else
if reverse_video_c then
attr_n(5 downto 3) <= a;
else
attr_n(2 downto 0) <= a;
end if;
end if;
end procedure;
begin
if rst = '1' and g.asynchronous_reset then
state_c <= RESET;
elsif rising_edge(clk) then
if rst = '1' and not g.asynchronous_reset then
state_c <= RESET;
else
x_c <= x_n;
y_c <= y_n;
c_c <= c_n;
count_c <= count_n;
limit_c <= limit_n;
state_c <= state_n;
n1_c <= n1_n;
n2_c <= n2_n;
data_we <= '0';
cursor_we <= '0';
akk_init <= '0';
attr_c <= attr_n;
ctl_c <= ctl_n;
conceal_c <= conceal_n;
font_sel_c <= font_sel_n;
saved_x_c <= saved_x_n;
saved_y_c <= saved_y_n;
saved_attr_c <= saved_attr_n;
reverse_video_c <= reverse_video_n;
base_c <= base_n;
saved_base_c <= saved_base_n;
is_base_saved_c <= is_base_saved_n;
if state_c = RESET then
n1_n <= (others => '0');
n2_n <= (others => '0');
x_n <= (others => '0');
y_n <= (others => '0');
c_n <= (others => '0');
count_n <= (others => '0');
limit_n <= (others => '0');
saved_x_n <= (others => '0');
saved_y_n <= (others => '0');
saved_attr_n <= (others => '0');
font_sel_n <= (others => '0');
state_n <= ACCEPT;
attr_n <= attr_default;
ctl_n <= ctl_default;
conceal_n <= false;
reverse_video_n <= false;
base_n <= (others => '0');
is_base_saved_n <= false;
saved_base_n <= (others => '0');
elsif state_c = ACCEPT then
if we = '1' then
c_n <= unsigned(char);
state_n <= NORMAL;
end if;
-- This behavior does not really mix well
-- with the eForth interpreter.
--
-- if is_base_saved_c then
-- is_base_saved_n <= false;
-- base_n <= saved_base_c;
-- end if;
elsif state_c = NORMAL then
case c_c is
when tab =>
x_n <= (x_c and "1111000") + 8;
state_n <= ERASING;
c_n <= blank;
limit_value := unsigned(addr and "1111111111000") + 8;
limit_n <= limit_value(limit_n'high + 3 downto limit_n'low + 3);
count_n <= unsigned(addr);
when ascii_bell =>
ctl_n(4) <= not ctl_c(4); -- ctr_n(4) goes to edge triggered logic
state_n <= ACCEPT;
when cr =>
y_n <= y_plus_one;
state_n <= WRAP;
when lf =>
x_n <= (others => '0');
state_n <= WRITE;
when backspace =>
x_n <= x_minus_one_limited;
state_n <= WRITE;
when esc =>
state_n <= CSI;
when others =>
data_we <= '1';
state_n <= ADVANCE;
end case;
elsif state_c = ADVANCE then
x_n <= x_plus_one;
state_n <= WRAP;
elsif state_c = CSI then
if we = '1' then
c_n <= unsigned(char);
state_n <= COMMAND;
end if;
elsif state_c = COMMAND then
case c_c is
when ascii_c =>
x_n <= (others => '0');
y_n <= (others => '0');
c_n <= blank;
count_n <= (others => '0');
limit_n <= "1000000000";
state_n <= ERASING;
base_n <= (others => '0');
when lsqb =>
state_n <= NUMBER1;
akk_init <= '1';
when ascii_7 => -- 'ESC 7'; Save Cursor and Attributes
saved_x_n <= x_c;
saved_y_n <= y_c;
saved_attr_n <= attr_c;
state_n <= ACCEPT;
when ascii_8 => -- 'ESC 8'; Restore Cursor and Attributes
x_n <= saved_x_c;
y_n <= saved_y_c;
attr_n <= saved_attr_c;
state_n <= ACCEPT;
when others => -- Error
state_n <= ACCEPT;
end case;
elsif state_c = NUMBER1 then
if akk_done_o = '1' then
state_n <= COMMAND1;
n1_n <= n_o(n1_n'range);
end if;
elsif state_c = COMMAND1 then
repeat := false;
case akk_char_o is
when x"41" => -- CSI n 'A' : CUU Cursor up
y_n <= y_minus_one_limited;
exit_repeat := WRITE;
repeat := true;
when x"42" => -- CSI n 'B' : CUD Cursor Down
y_n <= y_plus_one_limited;
exit_repeat := LIMIT;
repeat := true;
when x"43" => -- CSI n 'C' : CUF Cursor Forward
x_n <= x_plus_one_limited;
exit_repeat := LIMIT;
repeat := true;
when x"44" => -- CSI n 'D' : CUB Cursor Back
x_n <= x_minus_one_limited;
exit_repeat := WRITE;
repeat := true;
when x"45" => -- CSI n 'E'
y_n <= y_minus_one_limited;
x_n <= (others => '0');
exit_repeat := WRITE;
repeat := true;
when x"46" => -- CSI n 'F'
y_n <= y_plus_one_limited;
x_n <= (others => '0');
exit_repeat := LIMIT;
repeat := true;
when x"47" => -- CSI n 'G' : CHA Cursor Horizontal Absolute
x_n <= n1_c(x_n'range);
state_n <= LIMIT;
when x"4a" => -- CSI n 'J'
x_n <= (others => '0');
y_n <= (others => '0');
base_n <= (others => '0');
cursor_we <= '1';
state_n <= ERASING;
c_n <= blank;
count_n <= (others => '0');
limit_n <= "1000000000";
when x"3b" => -- ESC n ';' ...
state_n <= NUMBER2;
akk_init <= '1';
when x"6d" => -- CSI n 'm' : SGR
state_n <= ATTRIB;
-- NB. Number parameter does nothing.
when x"53" => -- CSI n 'S' : scroll up
saved_base_n <= base_c;
is_base_saved_n <= true;
base_n <= (others => '0');
state_n <= WRITE;
when x"54" => -- CSI n 'T' : scroll down
state_n <= WRITE;
if is_base_saved_c then
base_n <= saved_base_c;
is_base_saved_n <= false;
end if;
when x"73" => -- CSI 's': SCP (Secure, Contain, Protect the Cursor Position)
saved_x_n <= x_c;
saved_y_n <= y_c;
state_n <= ACCEPT;
when x"75" => -- CSI 'u': RCP Restore Cursor Position
x_n <= saved_x_c;
y_n <= saved_y_c;
state_n <= ACCEPT;
-- This is an extension, it is for setting the
-- control lines of the VGA module directly.
when x"78" => -- ESC n 'x' : Set VGA control registers directly
ctl_n <= n1_c(ctl_n'range);
state_n <= WRITE;
when others => -- Error
state_n <= ACCEPT;
end case;
if repeat then
if n1_c <= 1 then
state_n <= exit_repeat;
else
n1_n <= n1_c - 1;
end if;
end if;
elsif state_c = NUMBER2 then
if akk_done_o = '1' then
state_n <= COMMAND2;
n2_n <= n_o(n2_n'range);
end if;
elsif state_c = COMMAND2 then
case akk_char_o is
when x"66" => -- f
y_n <= n1_c(y_n'range) - 1;
x_n <= n2_c(x_n'range) - 1;
cursor_we <= '1';
state_n <= LIMIT;
when x"48" => -- H
y_n <= n1_c(y_n'range) - 1;
x_n <= n2_c(x_n'range) - 1;
cursor_we <= '1';
state_n <= LIMIT;
when x"6d" => -- ESC n ';' m 'm'
state_n <= ATTRIB;
when others =>
state_n <= ACCEPT;
end case;
elsif state_c = WRAP then
if x_overflow then
x_n <= (others => '0');
y_n <= y_plus_one;
elsif y_overflow then
x_n <= (others => '0');
y_n <= y_minus_one;
state_n <= ERASING;
c_n <= blank;
count_n <= unsigned(addr_sel);
limit_value := unsigned(addr_sel) + (3*width);
limit_n <= limit_value(limit_n'high + 3 downto limit_n'low + 3);
base_n <= base_c + (width / 16);
else
state_n <= WRITE;
end if;
elsif state_c = LIMIT then
if x_overflow then
x_n <= to_unsigned(width - 1, x_n'high + 1);
end if;
if y_overflow then
y_n <= to_unsigned(height - 1, y_n'high + 1);
end if;
state_n <= WRITE;
elsif state_c = WRITE then
state_n <= ACCEPT;
cursor_we <= '1';
elsif state_c = ERASING then
if count_c(limit_c'high + 3 downto limit_c'low + 3) /= limit_c then
count_n <= count_c + 1;
data_we <= '1';
else
state_n <= WRAP;
end if;
elsif state_c = ATTRIB then
case n1_c is
when x"00" =>
attr_n <= attr_default;
conceal_n <= false;
reverse_video_n <= false;
when x"01" => attr_n(6) <= '1'; -- bold
-- when x"02" => -- faint
when x"16" => attr_n(6) <= '0'; -- normal brightness
when x"05" => attr_n(7) <= '1'; -- slow blink
when x"19" => attr_n(7) <= '0'; -- blink off
when x"07" =>
if not reverse_video_c then
attr_n <= attr_c(7 downto 6) & attr_c(2 downto 0) & attr_c(5 downto 3);
end if;
reverse_video_n <= true;
when x"1B" =>
if reverse_video_c then
attr_n <= attr_c(7 downto 6) & attr_c(2 downto 0) & attr_c(5 downto 3);
end if;
reverse_video_n <= false;
when x"08" => conceal_n <= true;
when x"1C" => conceal_n <= false;
when x"0A" => font_sel_n <= (others => '0');
when x"0B" => font_sel_n <= "1";
when x"1E" => reverse_video("000", true); -- 30
when x"1F" => reverse_video("001", true);
when x"20" => reverse_video("010", true);
when x"21" => reverse_video("011", true);
when x"22" => reverse_video("100", true);
when x"23" => reverse_video("101", true);
when x"24" => reverse_video("110", true);
when x"25" => reverse_video("111", true);
when x"28" => reverse_video("000", false); -- 40
when x"29" => reverse_video("001", false);
when x"2A" => reverse_video("010", false);
when x"2B" => reverse_video("011", false);
when x"2C" => reverse_video("100", false);
when x"2D" => reverse_video("101", false);
when x"2E" => reverse_video("110", false);
when x"2F" => reverse_video("111", false);
when others =>
end case;
state_n <= ACCEPT;
else
state_n <= RESET;
report "Invalid State" severity failure;
end if;
end if;
end if;
end process;
end architecture;
----- VT100 Terminal Emulator -------------------------------------------------
----- VGA Top Level Component -------------------------------------------------
library ieee,work;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
use work.vga_pkg.all;
use work.util.file_format;
use work.util.file_hex;
use work.util.file_binary;
use work.util.common_generics;
entity vga_top is
generic(g: common_generics);
port(
clk: in std_ulogic;
clk25MHz: in std_ulogic;
rst: in std_ulogic;
-- VGA Text buffer interface
vga_we_ram: in std_ulogic; -- Write enable RAM
vga_din: in std_ulogic_vector(15 downto 0);
vga_addr: in std_ulogic_vector(12 downto 0);
base: in std_ulogic_vector(12 downto 0);
-- VGA control registers
i_font_sel: in std_ulogic_vector(0 downto 0);
i_vga_control_we: in vga_control_registers_we_interface;
i_vga_control: in vga_control_registers_interface;
o_vga: out vga_physical_interface);
end;
architecture behav of vga_top is
-- Setup for text buffer memory
constant text_addr_length: positive := 13;
constant text_data_length: positive := 16;
constant text_file_name: string := "text.hex";
constant text_file_type: file_format := FILE_HEX;
-- Setup for font buffer memory
constant font_addr_length: positive := 13;
constant font_data_length: positive := 8;
constant font_file_name: string := "font.bin";
constant font_file_type: file_format := FILE_BINARY;
-- Internal signals for mapping output <--> VGA module
signal foreground_draw_internal: std_ulogic := '0';
-- Text RAM signals, RAM <--> VGA module
signal text_dout: std_ulogic_vector(15 downto 0) := (others => '0');
signal text_din: std_ulogic_vector(15 downto 0) := (others => '0');
signal text_addr: std_ulogic_vector(11 downto 0) := (others => '0');
signal text_addr_full: std_ulogic_vector(12 downto 0) := (others => '0');
-- Font ROM signals, ROM<-->VGA module
signal font_addr: std_ulogic_vector(12 downto 0) := (others => '0');
signal font_dout: std_ulogic_vector( 7 downto 0) := (others => '0');
signal control_c, control_n: vga_control_registers_interface := vga_control_registers_initialize;
signal foreground_draw: std_ulogic := '0';
signal background_draw: std_ulogic := '0';
begin
vga_ns: process(clk, rst)
begin
if rst = '1' and g.asynchronous_reset then
control_c <= vga_control_registers_initialize;
elsif rising_edge(clk) then
if rst = '1' and not g.asynchronous_reset then
control_c <= vga_control_registers_initialize;
else
control_c <= control_n;
end if;
end if;
end process;
vga_creg_we: process(
control_c,
i_vga_control,
i_vga_control_we)
begin
control_n <= control_c;
if i_vga_control_we.crx = '1' then control_n.crx <= i_vga_control.crx; end if;
if i_vga_control_we.cry = '1' then control_n.cry <= i_vga_control.cry; end if;
if i_vga_control_we.ctl = '1' then control_n.ctl <= i_vga_control.ctl; end if;
end process;
u_vga: work.vga_pkg.vga_core
generic map(g => g)
port map (
rst => rst,