forked from ishe/plus4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathted.v
1754 lines (1564 loc) · 58.8 KB
/
ted.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
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
`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Copyright 2013-2019 Istvan Hegedus
//
// FPGATED is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// FPGATED is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// Create Date: 12/18/2013 - 31/03/2016
// Design Name: MOS 8360 video chip
// Module Name: ted.v
// Version: 1.1
// Project Name: FPGATED
// Description: Cycle exact MOS 8360 TED display chip
//
// Revision history:
// 0.2 12/11/2015 diag 264 runs, all screenmodes implemented, external dram works, scroll bug in diag264
// 0.3 22/01/2016 DRAM refresh horizontal events improved (increment start/stop, counter reset),vertical scroll bug in Invincible, FF1E write bug in New FLI, FLI incorrect
// 0.4 03/02/2016 VertSub counter fixed for Invincible start screen
// 0.5 22/02/2016 Raster interrupt fixed, Invincible does not freeze now
// 0.6 03/03/2016 Multicolor Character mode bug fixed in pixelgenerator. Majesty Of Sprites looks good now
// 0.7 30/03/2016 Audio sound generator and audio D/A implemented
// 1.0 14/07/2016 First public release, functionally equivalent to 0.7, code cleaned up, license information added
// 1.0.1 08/03/2017 pal register added to module output for FPGA clock switching
// 1.1 01/10/2019 DMA position counter fixed (videocounter). MMS FLI demos work now.
//
//////////////////////////////////////////////////////////////////////////////////
module ted(
input wire clk, // clk must be 4*dot clk so 28.375152MHz for PAL (1.6*PAL system's clock) and 28.63636 for NTSC (2*NTSC system's clock)
input wire [15:0] addr_in,
output wire [15:0] addr_out,
input wire [7:0] data_in,
output wire [7:0] data_out,
input wire rw,
output wire cpuclk, // this is a CPU clock for external real CPU
output wire [6:0] color, // 7 bits color code
output wire csync,
output wire irq,
output wire ba,
output reg mux,
output reg ras,
output reg cas,
output reg cs0,
output reg cs1,
output reg aec,
output wire snd,
input wire [7:0] k,
output wire cpuenable, // this TED signals is needed only for FPGA bustiming and FPGA internal cpu. If external CPU is used, it is not needed.
output reg pal
);
// TED register addresses
parameter TIMER1LO=6'h00;
parameter TIMER1HI=6'h01;
parameter TIMER2LO=6'h02;
parameter TIMER2HI=6'h03;
parameter TIMER3LO=6'h04;
parameter TIMER3HI=6'h05;
parameter CONTROL1=6'h06;
parameter CONTROL2=6'h07;
parameter KEYLATCH=6'h08;
parameter IRQ =6'h09;
parameter IRQEN =6'h0A;
parameter RASTER =6'h0B;
parameter CURPOSHI=6'h0C;
parameter CURPOSLO=6'h0D;
parameter CH1FREQLO=6'h0E;
parameter CH2FREQLO=6'h0F;
parameter CH2FREQHI=6'h10;
parameter SOUNDCTRL=6'h11;
parameter BMAPBASE=6'h12;
parameter CHARBASE=6'h13;
parameter VIDEOBASE=6'h14;
parameter BGCOLOR0=6'h15;
parameter BGCOLOR1=6'h16;
parameter BGCOLOR2=6'h17;
parameter BGCOLOR3=6'h18;
parameter EXCOLOR=6'h19;
parameter CHARPOSRELOADHI=6'h1A;
parameter CHARPOSRELOADLO=6'h1B;
parameter VSCANPOSHI=6'h1C;
parameter VSCANPOSLO=6'h1D;
parameter HSCANPOS=6'h1E;
parameter FLASH_VERTSUB=6'h1F;
parameter ROMEN=6'h3E;
parameter RAMEN=6'h3F;
// DMA FSM states
localparam IDLE=3'b000,THALT1=3'b001,THALT2=3'b010,THALT3=3'b011,TDMA=3'b100;
reg [2:0] dma_state=IDLE,dma_nextstate; // DMA FSM state register
// TED user accessible registers
// These registers are the actual TED registers accessible by end users
reg [15:0] timer1=16'b0,timer1_reload=16'b0; // $FF00/01
reg [15:0] timer2=16'b0; // $FF02/03
reg [15:0] timer3=16'b0; // $FF04/05
reg test=1'b0,ecm=1'b0,bmm=1'b0,den=1'b0,rsel=1'b0; // $FF06 control1 register bits
reg [2:0] yscroll=3'b0; // $FF06 bits 0-2, vertical scroll register
reg bmm_reg=1'b0,ecm_reg=1'b0; // delayed registered values of BMM and ECM
reg reverse=1'b0,stop=1'b0,mcm=1'b0,csel=1'b0; // $FF07 control2 register bits
reg [2:0] xscroll=3'b0; // $FF07 bits 0-2, horizontal scroll regitser
reg reverse_reg=1'b0,mcm_reg=1'b0; // delayed registered values of REVERSE and ECM
reg [7:0] keylatch=8'hff; // $FF08 keyboard latch
reg Cnt1Irq=1'b0,Cnt2Irq=1'b0,Cnt3Irq=1'b0,RasterIrq=1'b0,LPIrq=1'b1; // $FF09 IRQ register
reg enCnt1Irq=1'b0,enCnt2Irq=1'b0,enCnt3Irq=1'b0,enRasterIrq=1'b0,enLPIrq=1'b0; // $FF0A IRQ enable register
reg [8:0] RasterCmp=9'b0; // $FF0B
reg [9:0] CursorPos=10'b0; // $FF0C/0D
reg [9:0] Ch1Freq=10'b0; // $FF0E, $FF12 bits 0-1
reg [9:0] Ch2Freq=10'b0; // $FF0F, $FF10 bits 0-1
reg damode=1'b0,ch2noise=1'b0,ch2en=1'b0,ch1en=1'b0; // $FF11 bits 4-7
reg [3:0] volume=4'b0; // $FF11 bits 0-3
reg [2:0] bmapbase=3'b0; // $FF12 bits 3-5, Bitmap base address
reg charrom=1'b0; // $FF12 bit 2
reg [5:0] charbase=6'b0; // $FF13 bits 2-7, Character memory base address
reg clkmode=1'b0; // $FF13 bit 1, force single clock mode
reg [4:0] vmbase=5'b0; // $FF14 bits 3-7, Video RAM base address register
reg [6:0] bgcolor0=7'b0,bgcolor1=7'b0,bgcolor2=7'b0,bgcolor3=7'b0,excolor=7'b0; // $FF15-19 color registers
reg [9:0] CharPosReload=10'b0; // $FF1A/B, Character Position Reload increments by 40 for each character row completed
reg [8:0] vcounter=9'b0; // $FF1C/D, Vertical line counter
reg [8:0] hcounter=9'b0; // $FF1E, Horizontal dot counter. In real TED it is 11bit. Counts from 0 to 455
reg [4:0] FlashCount=5'b0; // $FF1F bits 3-6, Flash counter's 5th bit is the actual flash state and is not user accessible
reg [2:0] VertSubCount=3'b0; // $FF1F bits 0-2, Vertical Character scan line position
// TED internal operational registers
// These are needed for the internal operation
reg [7:0] refreshcounter=8'h00;
reg refresh=1'b0;
reg [3:0] phicounter=4'b0; // CPU single clock generator counter
reg phi=1'b0; // CPU single clock
reg singleclock=1'b0; // signals single clock mode
reg [8:0] hcounter_next;
reg [8:0] vcounter_next;
reg [8:0] videoline=9'b0; // vcounter latched at start of each scanline
reg [7:0] dataout_reg=8'hff; // TED's databus out register
reg [7:0] data_in_reg; // TED databus in register
reg [5:0] addr_in_reg; // TED address in register
reg [6:0] colorreg=7'b0; // video out register
reg ramen=1'b0; // High memory address RAM enable register (above $8000)
reg t1stop=1'b0,t2stop=1'b0,t3stop=1'b0; // Timer disable signals
reg resetRasterIrq,resetLpIrq,resetCnt1Irq,resetCnt2Irq,resetCnt3Irq; // Interrupt reset signals
reg RasterIrqDone=1'b0; // Signals that raster interrupt has already happened in this line
reg enabledisplay=1'b0; // DEN register changes enabledisplay signal on first scanline only
reg badline2=1'b0; // signals 2nd badline (1st badline signal is a wire)
reg ext_fetch=1'b0; // signals external fetch window inside scanline
reg char_fetch=1'b0; // signals character fetch window inside scanline
reg dma_window=1'b0; // signals active dma range inside a scanline
reg char_window=1'b0; // signals when character/pixel data can be latched from data bus inside a scanline
reg inc_flashcount_window=1'b0; // signals flash counter increase window
reg inc_vertsub_window=1'b0; // active for one single clock cycle, signals vertsub register incrementation point (thus actual increment point is delayed with a single clock cycle)
reg inc_vertline_window=1'b0; // active for one single clock cycle, signals vertical line incrementation point (thus actual increment point is delayed with a single clock cycle)
// horizontal event positions used for the horizontal event decoder. They don't necessarily reflect the values seen in documentation
reg hpos_0,hpos_8,hpos_154,hpos_172,hpos_288,hpos_295,hpos_296,hpos_303,hpos_304;
reg hpos_312,hpos_320,hpos_336,hpos_343,hpos_348,hpos_353,hpos_359,hpos_380,hpos_382;
reg hpos_384,hpos_391,hpos_392,hpos_400,hpos_407,hpos_423,hpos_431,hpos_432,hpos_440;
reg inc_charpos=1'b0; // signals internal character position register (not user accessible) increment range inside scanline (not same as $FF1A/$FF1B)
reg [15:0] addr_out_reg; // TED's address out register
reg [15:0] tedaddress; // this is a non registered TED address (although register variable but used in combinational logic)
reg datahold=1'b0; // signals whether TED should hold its data on the databus
reg VertSubActive=1'b0; // signals the scanline ranges when Vertsub counter is active
reg tedwrite_delay=1'b0; // this signal was needed to emulate a one dot clock delay when TED writes data to its internal registers. Although most probably this delay exist at
// all TED register writes, in FPGATED we use it only for hcounter/vcounter and color register writes. This emulates white pixel bug too.
reg csyncreg=1'b0,vsync=1'b0,equalization=1'b0,eq1=1'b1,eq2=1'b1,hsync=1'b1; // PAL/NTSC video screen signals
reg [9:0] videocounter=10'b0; // videocounter is the actual DMA counter.
reg inc_videocounter=1'b0; // signals videocounter increment window
reg [9:0] videocounter_reload=10'b0; // videocounter is reloaded with this value at the beginning of each displayed line
reg [9:0] CharPosition=10'b0; // CharPosition is loaded by $FF1A/$FF1B and is similar to videocounter. It is used for pixel data fetch from memory.
reg CharPosLatch=1'b0; // Signals latching position of CharPosition and videocounter
reg latch_window=1'b0; // CharPosition and videocounter latch delay window
reg latch_charposition=1'b0; // Charposition and videocounter latch position
reg [7:0] attr_buf [0:39]; // TED internal videomatrix attribute memory
reg [7:0] char_buf [0:39]; // TED internal videomatrix character pointer memory
reg [7:0] nextchar=8'b0,currentchar=8'b0,waitingchar=8'b0,pixelchar=8'b0; // next...,current... is a 2 bytes shiftregister to keep data until rendering. waiting... is waiting to be loaded to rendering shiftregister
reg [7:0] nextattr=8'b0,currentattr=8'b0,waitingattr=8'b0,pixelattr=8'b0;
reg [7:0] nextpixels=8'b0,currentpixels=8'b0,waitingpixels=8'b0;
reg [7:0] pixelshiftreg=8'b0; // This register contains pixel data and shifts it during rendering
reg [5:0] shiftcount=6'b0; // Used by the videomatrix shift register to count number of shifts
reg verticalscreen=1'b0; // Signals which lines are in screen area (top/bottom border control)
reg widescreen=1'b0,narrowscreen=1'b0; // Signals horizontal screen area (left/right border control)
reg videoshift=1'b0; // Signals when vide shoft register is active
reg nextcursor=1'b0,currentcursor=1'b0,waitingcursor=1'b0; // cursor state internal storage for 3 signle clock cycles
reg [6:0] pixelcolor; // Color of a pixel
reg doubleshift=1'b0; // During multicolor mode 2 pixels identify one pixel, so this register signals to pixel generator wheter to shift one or two pixels to get color data
reg dotfetch; // Signals when TED is fetching pixeldata from databus
reg dotfetch_reg=1'b0; // Registered version of dotfetch
reg [2:0] xscroll_latch=3'b0; // Registered version of xscroll register
reg [2:0] yscroll_latch=3'b0; // Registered version of yscroll register
reg hblank=1'b0,vblank=1'b0; // Signals blanking area
reg refresh_inc=1'b0; // Dram refresh counter increment window
reg stopreg=1'b0; // This is a latched version of stop register. Latched at single cycle end
// audio part registers
reg [1:0] audiocycle=2'b0; // Audio cycle counter divides single clock by 4 and generates audio clock
reg [9:0] ch1count=10'b0,ch2count=10'b0; // Audio channel1 and channel2 counters
reg ch1state=1'b0,ch2state=1'b0; // State register of audio channels
reg ch1stateclk_prev=1'b0,ch2stateclk_prev=1'b0;
reg [7:0] noisegen=8'b0; // Noise generator register
reg [4:0] pwmcounter1=5'b0,pwmcounter2=5'b0; // PWM D/A counters
reg ch1pwm=0,ch2pwm=0; // continous square wave with proportional duty cycle to volume
reg [4:0] digivolume=5'b0; // A digital value signaling at which pwmclock cycle PWM signal high value starts. A digitized version of volume level
reg [17:0] watchdog_ch1=18'b0,watchdog_ch2=18'b0; // Watchdog timer to emulate sound decay of TED's dynamic latch behaviour
integer i;
integer j;
integer n;
// Internal wires, flags
wire dphi; // double phi clock
wire [8:0] EOS,VS_START,VS_STOP,EQ_START,EQ_STOP,VBLANK_START,VBLANK_STOP; //video signal generation constants. Vertical Sync, Equalization, Blank
wire tick8; // enable tick for pixelclock (8MHz)
wire blanking; // screen blanking area flag
wire lowrom,highrom; // TED low and high rom area flags
wire irqpos; // IRQ position flag inside clock cycle. Emulates real TED's IRQ signal activation position
wire io,tedreg,tedwrite; // IO area flag, TED user registers area flag, TED write cycle flag (signals when TED register is written by CPU)
wire badline; // badline flag
wire attr_fetch_line; // Visible screen area flag (signals active window)
wire tedlatch; // tedlatch simulates at which exact position TED latches value into its internal register from the databus
wire [7:0] charpointer,attrpointer;
wire multicolor; // multicolor mode flag
wire pixelscreen; // visible pixelscreen area flag (excluding borders)
wire ch1clk,ch2clk; // Audio channel clocks
wire ch1stateclk,ch2stateclk; // Audio state register change clock
wire ch1audio,ch2audio; // Audio channel square waves not modulated by volume (before PWM)
wire noise; // Noise
wire watchdog_ch1max,watchdog_ch2max; // Audio watchdog timer maximum values. Actual value is taken from plus4emu
// Initializing internal video matrix
initial
begin
for(i=0;i<=39;i=i+1)
begin
char_buf[i]=0;
attr_buf[i]=0;
end
pal=1'b0;
end
//---------------------------------------------------------------------------
// Often used combinational signals
//---------------------------------------------------------------------------
assign cycle_end=(phicounter==15)?1'b1:1'b0; // high pulse at the end of each double clock cycle
assign single_cycle_end=(cycle_end & phi)?1'b1:1'b0; // high pulse at the end of each single clock cycle
//---------------------------------------------------------------------------
// Clock signal driver phi=Single Clock dphi=Double Clock
//---------------------------------------------------------------------------
always @(posedge clk) // Counting FPGA clock cycles during double clock. phicounter is mod16 counter, 16*clk=half phi
begin
phicounter<=phicounter+1;
end
assign cpuclk = singleclock?phi:dphi; // Generated CPU clock. Used only when real 8501 CPU is connected to FPGA
assign dphi = phicounter[3]; // Internal double clock signal
assign cpuenable=(single_cycle_end)?1'b1: // Generated CPU enable signal. Used only when FPGA CPU is used
(cycle_end && !singleclock)?1'b1:
1'b0;
always @(posedge clk) // Internal single clock signal is always generated
begin
if (cycle_end)
phi<=~phi;
end
always @(posedge clk) // clock mode controller. Single or double clock multiplex for the CPU.
begin
if(single_cycle_end) // clock mode change happens only at single clock boundary
singleclock<=((enabledisplay & ext_fetch) | refresh | clkmode | stop); // there are 4 criterias to generate single clock: display area,dram refresh,forced 1Mhz,TED stop
end
always @(posedge clk)
begin
if(single_cycle_end)
stopreg<=stop;
end
//---------------------------------------------------------------------------
// Attribute Fetch
//---------------------------------------------------------------------------
always @(posedge clk) // flip flop to signal external fetch single clock window, delayed with 1 single clock cycle
begin
if(hpos_296)
ext_fetch<=0;
else if(hpos_400)
ext_fetch<=1;
end
assign attr_fetch_line=(videoline>=0 && videoline<203);
//---------------------------------------------------------------------------
// DRAM Refresh
//---------------------------------------------------------------------------
always @(posedge clk) // refresh single clock control
begin
if(hpos_336)
refresh<=0;
else if(hpos_296)
refresh<=1;
end
always @(posedge clk) // refresh counter increment control
begin
if(hpos_343)
refresh_inc<=0;
else if(hpos_303)
refresh_inc<=1;
end
always @(posedge clk)
begin
if(single_cycle_end & (refresh_inc|stopreg))
refreshcounter<=refreshcounter+1;
else if(hpos_431 & (videoline==0|refresh_inc|stopreg))
refreshcounter<=8'h00;
end
//-------------------------------------------------------------------------------------------
// Horizontal counter running on ~8Mhz and vertical counter qualified by horizontal counter
//-------------------------------------------------------------------------------------------
assign tick8=(phicounter[1:0]==3)?1'b1:1'b0; //8Mhz clock tick for pixelclock. tick8 must activate one fastclk cycle earlier to use it for hcounter
always @(posedge clk)
begin
hcounter<=hcounter_next;
vcounter<=vcounter_next;
if(hpos_392)
videoline<=vcounter;
end
always @* //horizontal counter next state logic
begin
hcounter_next=hcounter;
if (tedlatch & addr_in_reg[5:0]==HSCANPOS) // $ff1e horizontal counter register write
begin
hcounter_next=hcounter+1;
hcounter_next[8:3]=~data_in_reg[7:2]; // bit 0-2 are not modified by user write to prevent clock phase change
end
else if (tick8 & ~stopreg)
begin
if (hcounter==9'd455)
hcounter_next=9'd0;
else
hcounter_next=hcounter+1;
end
end
always @* //vertical counter next state logic
begin
vcounter_next=vcounter;
if(tedwrite & addr_in[5:1]==5'b01110) // $ff1c or $ff1d register write (VSCAN HI and LO)
begin
if(addr_in[0]==0)
vcounter_next={data_in[0],vcounter[7:0]};
else vcounter_next={vcounter[8],data_in};
end
else if(inc_vertline_window & single_cycle_end)
begin
if (vcounter==EOS)
vcounter_next=0;
else vcounter_next=vcounter+1;
end
end
always @(posedge clk)
begin
if(hpos_384)
inc_vertline_window<=1;
else if (single_cycle_end)
inc_vertline_window<=0;
end
//---------------------------------------------------------------------------
// Timer 1
//---------------------------------------------------------------------------
// timer 1 decrements during odd single clock cycle (phi=0)
// exact counter change position is unknown but can be estimated based on IRQ place and reading counter values every cycle on a real hardware
// timer 1 changes approximately at half of phi low cycle after IRQ position (IRQ position is 160ns after phi low cycle start).
//
always @(posedge clk)
begin
if(tedwrite) // load timer 1 at cycle border
begin
if (addr_in[5:0]==TIMER1LO)
begin
timer1[7:0]<=data_in;
timer1_reload[7:0]<=data_in;
t1stop<=1;
end
if (addr_in[5:0]==TIMER1HI)
begin
timer1[15:8]<=data_in;
timer1_reload[15:8]<=data_in;
t1stop<=0;
end
end
if(phicounter==7 && ~phi & ~t1stop & ~stopreg) // decrement or reload timer 1
begin
if(timer1==0)
timer1<=timer1_reload-1;
else
timer1<=timer1-1;
end
end
//---------------------------------------------------------------------------
// Timer 2
//---------------------------------------------------------------------------
// timer 2 decrements during even single clock cycle (phi=1)
// timer 2 changes approximately at odd-even single clock cycle boundary (phi low - high transition)
always @(posedge clk)
begin
if(tedwrite) // load timer 2 at cycle border
begin
if (addr_in[5:0]==TIMER2LO)
begin
timer2[7:0]<=data_in;
t2stop<=1;
end
if (addr_in[5:0]==TIMER2HI)
begin
timer2[15:8]<=data_in;
t2stop<=0;
end
end
else if(phicounter==15 && phi==0 && t2stop==0 && stopreg==0) // if not loaded, decrement timer 2 at odd-even cycle border
begin
timer2<=timer2-1;
end
end
//---------------------------------------------------------------------------
// Timer 3
//---------------------------------------------------------------------------
// timer 3 decrements during even single clock cycle (phi=1)
// timer 3 changes approximately at half of phi high cycle (contrary to timer 1)
always @(posedge clk)
begin
if(tedwrite)
begin
if (addr_in[5:0]==TIMER3LO) // load timer 3 at cycle border
begin
timer3[7:0]<=data_in;
t3stop<=1;
end
if (addr_in[5:0]==TIMER3HI)
begin
timer3[15:8]<=data_in;
t3stop<=0;
end
end
if(phicounter==7 && phi==1 && t3stop==0 && stopreg==0) // decrement timer 3
begin
timer3<=timer3-1;
end
end
//---------------------------------------------------------------------------
// Timer IRQs
//---------------------------------------------------------------------------
//
assign irqpos=(phicounter==4 & ~phi)?1'b1:1'b0;
always @(posedge clk)
begin
if(resetCnt1Irq)
Cnt1Irq<=0;
else if(irqpos && timer1==0)
Cnt1Irq<=1;
end
always @(posedge clk)
begin
if(resetCnt2Irq)
Cnt2Irq<=0;
else if(irqpos && timer2==0)
Cnt2Irq<=1;
end
always @(posedge clk)
begin
if(resetCnt3Irq)
Cnt3Irq<=0;
else if(irqpos && timer3==0)
Cnt3Irq<=1;
end
//---------------------------------------------------------------------------
// Raster IRQ
//---------------------------------------------------------------------------
always @(posedge clk)
begin
if (resetRasterIrq)
RasterIrq<=0;
else if (RasterCmp==vcounter)
begin
if(~RasterIrqDone & tick8) // do raster interrupt only 1 time per raster line and interrupt happens when phi or dphi is low and about 170ns after cycle start
begin
RasterIrq<=1;
RasterIrqDone<=1;
end
end
else RasterIrqDone<=0;
end
//---------------------------------------------------------------------------
// IRQ signal
//---------------------------------------------------------------------------
assign irq=~((enCnt1Irq & Cnt1Irq)|(enCnt2Irq & Cnt2Irq)| (enCnt3Irq & Cnt3Irq) | (enRasterIrq & RasterIrq) | (enLPIrq & LPIrq));
//---------------------------------------------------------------------------
// AEC signal generating
//---------------------------------------------------------------------------
always @(posedge clk)
begin
if((singleclock & ~phi) | (dma_state==TDMA))
aec<=0;
else aec<=1;
end
//---------------------------------------------------------------------------
// BA signal (RDY)
//---------------------------------------------------------------------------
assign ba=(dma_state==IDLE)?1'b1:1'b0;
//---------------------------------------------------------------------------
// badline
//---------------------------------------------------------------------------
assign badline=((yscroll_latch==videoline[2:0]) & enabledisplay & attr_fetch_line)?1'b1:1'b0; // signal 1st badline
always @(posedge clk)
begin
if(inc_vertline_window & single_cycle_end)
begin
if(badline)
badline2<=1;
else if(badline2)
badline2<=0;
end
/* else if(badline & ~hpos_392) //when yscroll changed to generate badline, abort an already started badline2 (except at start of line)
badline2<=0;*/
end
always @(posedge clk) // synchronize yscroll changes to single cycle border
begin
if(single_cycle_end)
yscroll_latch<=yscroll;
end
//---------------------------------------------------------------------------
// EnableDisplay signal
//---------------------------------------------------------------------------
always @(posedge clk)
begin
if(videoline==0 && den==1)
enabledisplay<=1;
if(videoline==204)
enabledisplay<=0;
end
//---------------------------------------------------------------------------
// Bitmapmask fetch signal
//---------------------------------------------------------------------------
always @(posedge clk) // character fetch window starts at first badline2 and stops at line 204. It signals that character fetches can happen in these lines.
begin
if(videoline==9'd204)
char_fetch<=0;
else if(badline2)
char_fetch<=1;
end
//----------------------------------------------------------------------------
// Character Position register $FF1A/$FF1B
//----------------------------------------------------------------------------
always @(posedge clk) // character fetch position increase from horizontal count 432 to horizontal count 296
begin
if(hpos_296)
inc_charpos<=0;
else if(hpos_432)
inc_charpos<=1;
end
always @(posedge clk) // DMA and Charpos latch delay trick
begin
latch_charposition<=0;
if(hpos_288)
latch_window<=1;
else if(single_cycle_end & latch_window)
begin
latch_window<=0;
latch_charposition<=1; // 1 FPGA cycle long latch enable signal used for Character position and videocounter position reload latch
end
end
always @(posedge clk)
begin
if(latch_charposition)
begin
if(VertSubCount==6)
CharPosLatch<=1; // CharPosLatch signal activates in line 6 and signals that videocounter (DMA counter) has been latched. It is used in line 7 for character position latch.
else
CharPosLatch<=0;
end
end
always @(posedge clk) // Character Position Reload register $FF1A/$FF1B
begin
if(tedwrite & addr_in[5:0]==CHARPOSRELOADHI)
CharPosReload[9:8]<=data_in[1:0];
else if(tedwrite & addr_in[5:0]==CHARPOSRELOADLO)
CharPosReload[7:0]<=data_in;
else if(hpos_392 & videoline==EOS) // clear character position reload at last line
CharPosReload<=0;
else if(CharPosLatch & latch_charposition & VertSubActive) // latch character position at 7th line of a character row if videocunter was latched in previous 6th row
CharPosReload<=CharPosition;
end
always @(posedge clk) // Character Position counter (not user accessible)
begin
if(hpos_392) // clear character position in each line at 392
CharPosition<=0;
else
begin
if(hpos_432 & enabledisplay & VertSubActive) // FIXME this might need delay
CharPosition<=CharPosReload;
else if(inc_charpos & single_cycle_end)
CharPosition<=CharPosition+1;
end
end
//---------------------------------------------------------------------------
// Attribute fetch (DMA)
//---------------------------------------------------------------------------
// DMA FSM
always @(posedge clk)
begin
dma_state<=dma_nextstate;
end
always @*
begin
dma_nextstate=dma_state;
case(dma_state)
IDLE: begin
if((badline|badline2) & dma_window)
dma_nextstate=THALT1;
end
THALT1: begin
if((badline|badline2) & dma_window & single_cycle_end)
dma_nextstate=THALT2;
else if (~dma_window | ~(badline|badline2))
dma_nextstate=IDLE;
end
THALT2: begin
if((badline|badline2) & dma_window & single_cycle_end)
dma_nextstate=THALT3;
else if (~dma_window | ~(badline|badline2))
dma_nextstate=IDLE;
end
THALT3: begin
if((badline|badline2) & dma_window & single_cycle_end)
dma_nextstate=TDMA;
else if (~dma_window | ~(badline|badline2))
dma_nextstate=IDLE;
end
TDMA: begin
if (~dma_window | ~(badline|badline2))
dma_nextstate=IDLE;
end
default: dma_nextstate=IDLE;
endcase
end
always @(posedge clk)
begin
if(hpos_407 & tick8)
dma_window<=1;
else if(hpos_295 & tick8)
dma_window<=0;
end
//---------------------------------------------------------------------------
// Attribute fetch address generation (videocounter is DMA position counter)
//---------------------------------------------------------------------------
always @(posedge clk) // videocounter increase window
begin
if(enabledisplay)
begin
if(hpos_296) // | shiftcount==6'd40)
inc_videocounter<=0;
else if(hpos_432)
inc_videocounter<=1;
end
end
always @(posedge clk)
begin
if(hpos_392 & videoline==EOS) // clear videocounter reload register at last line
videocounter_reload<=0;
else if(VertSubCount==6 && latch_charposition && enabledisplay) // Latch videocounter position at 6th line of a character row
videocounter_reload<=videocounter;
end
always @(posedge clk) // videocounter used for attribute and character pointer fetches (DMA counter)
begin
if(enabledisplay)
begin
if(hpos_432)
videocounter<=videocounter_reload;
else if(inc_videocounter & single_cycle_end) // increase videocounter at cycle border
videocounter<=videocounter+1;
end
end
//---------------------------------------------------------------------------
// Internal VideoMatrix (DMA buffers)
//---------------------------------------------------------------------------
always @(posedge clk)
begin
if(single_cycle_end)
begin
if(inc_videocounter)
begin
if(badline) begin // in 1st badline fetch attribute from databus and place to buffer's start
attr_buf[0]<=data_in;
end
else begin
attr_buf[0]<=attr_buf[39];
end
for(i=1;i<40;i=i+1) begin
attr_buf[i]<=attr_buf[i-1];
end
nextattr<=attr_buf[39];
shiftcount<=shiftcount+1;
if(((CursorPos==CharPosition) && VertSubActive) || (CursorPos==0 && CharPosition==0)) // cursor position must be checked here
nextcursor<=1;
else nextcursor<=0;
end
else begin
nextattr<=0;
shiftcount<=0;
end
end
end
always @(posedge clk)
begin
if(single_cycle_end)
begin
if(inc_videocounter)
begin
if(badline) begin // during badline1 load this buffer together with attribute buffer. Needed for FLI trick
char_buf[0]<=data_in;
nextchar<=char_buf[39];
end
else if(badline2) begin
char_buf[0]<=data_in;
nextchar<=data_in;
end
else begin
char_buf[0]<=char_buf[39];
nextchar<=char_buf[39];
end
for(j=1;j<40;j=j+1) begin
char_buf[j]<=char_buf[j-1];
end
end
else begin
nextchar<=0;
end
end
end
always @(posedge clk) // character window flag is needed for fetching pixel data from bus
begin
if(hpos_304)
char_window<=0;
else if(hpos_440 & enabledisplay)
char_window<=1;
end
always @(posedge clk) // latch pixel data from data bus at phi0 change from 0 to 1
begin
if(char_window)
begin
if(hpos_440)
nextpixels<=0;
else if(cycle_end & ~phi)
nextpixels<=data_in;
end
end
//---------------------------------------------------------------------------
// Vertical Sub register represents actual raster line inside character
//---------------------------------------------------------------------------
always @(posedge clk)
begin
if(hpos_392)
inc_vertsub_window<=1;
else if(single_cycle_end)
inc_vertsub_window<=0;
if (hpos_380 & badline) // ... activates at 1st badline of the frame
VertSubActive<=1;
else if (~enabledisplay) // ... inactivates at line 204
VertSubActive<=0;
end
always @(posedge clk)
begin
if(tedwrite && addr_in[5:0]==FLASH_VERTSUB) // if it is written by user
VertSubCount<=data_in[2:0];
else
if(inc_vertsub_window & single_cycle_end) // if it is time to change VertSub
if (videoline==0) // ... changes to 7 at line 0 FIXME: between cycle $C8 and $CA
VertSubCount<=3'd7;
else if(enabledisplay & VertSubActive)
VertSubCount<=VertSubCount+1; // ... increases between line 0 and 204
end
//---------------------------------------------------------------------------
// Flash counter
// 5th bit of FlashCount contains flash status and not accessible via FF1F register
//---------------------------------------------------------------------------
always @(posedge clk)
begin
if(hpos_348)
inc_flashcount_window<=1;
else if(single_cycle_end)
inc_flashcount_window<=0;
if(tedwrite && addr_in[5:0]==FLASH_VERTSUB)
FlashCount[3:0]<=data_in[6:3];
else if(videoline==205 & inc_flashcount_window & single_cycle_end)
FlashCount<=FlashCount+1;
end
//---------------------------------------------------------------------------
// Horizontal event decodes
//---------------------------------------------------------------------------
always @(hcounter)
begin
hpos_0=0;
hpos_8=0;
hpos_154=0;
hpos_172=0;
hpos_288=0;
hpos_295=0;
hpos_296=0;
hpos_303=0;
hpos_304=0;
hpos_312=0;
hpos_320=0;
hpos_336=0;
hpos_343=0;
hpos_348=0;
hpos_353=0;
hpos_359=0;
hpos_380=0;
hpos_382=0;
hpos_384=0;
hpos_391=0;
hpos_392=0;
hpos_400=0;
hpos_407=0;
hpos_423=0;
hpos_431=0;
hpos_432=0;
hpos_440=0;
case (hcounter)
0: hpos_0=1; // Start of 40 column screen
8: hpos_8=1; // Start of 38 column screen
154: hpos_154=1; // Equalization pulse 1 start
172: hpos_172=1; // Equalization pulse 1 end
288: hpos_288=1; // CharPosition and Videocounter latch position delayed by 1 cycle (starts at 296)
295: hpos_295=1; // Attribute fetch (DMA) FSM stop
296: hpos_296=1; // Stop external fetch single clock delayed by 1 cycle
// Start refresh singleclock delayed by 1 cycle (actual start at 304)
303: hpos_303=1; // Start refresh counter increment (304 in real TED)
304: hpos_304=1; // End of character window
312: hpos_312=1; // End of 38 column screen
320: hpos_320=1; // End of 40 column screen
336: hpos_336=1; // Stop refresh singleclock but delayed by 2 cycle (actual stop at 344)
343: hpos_343=1; // Stop refresh counter increment (344 in real TED)
348: hpos_348=1; // Flash (blink) counter increment point delayed by 2 cycles (increments at 352)
353: hpos_353=1; // Horizontal blanking start
359: hpos_359=1; // Horizontal sync start (358 in real TED however line change takes time thus the delay)
380: hpos_380=1;
382: hpos_382=1; // Equalization pulse 2 start
384: hpos_384=1; // End Of Screen. Clear vertical line,refresh counters and character reload register, increase vertical line after 1 cycle delay
391: hpos_391=1;
392: hpos_392=1; // VertSub register increment (delayed), Hsync end
400: hpos_400=1; // Start external fetch single clock (delayed), Equalization pulse 2 end
407: hpos_407=1; // Attribute fetch (DMA) FSM start
423: hpos_423=1; // Horizontal blanking stop
431: hpos_431=1; // Refresh counter reset point
432: hpos_432=1; // Start videocounter increment
440: hpos_440=1; // Start video shiftregister
endcase
end
//---------------------------------------------------------------------------
// Border control
//---------------------------------------------------------------------------
always @(posedge clk) // 25/24 row select and top/bottom borders
begin
if(rsel==1) begin
if(videoline==9'd4) // if 25 rows mode, screen starts at line 4
verticalscreen<=1;
else if (videoline==9'd204) // stops at line 204
verticalscreen<=0;
end
else begin
if(videoline==9'd8) // if 24 rows mode, screen starts at line 8
verticalscreen<=1;
else if(videoline==9'd200) // stops at line 200
verticalscreen<=0;
end
end
always @(posedge clk) // 38/40 columns select and side borders
begin
if(enabledisplay & verticalscreen)
begin
if(hpos_320 & tick8)
widescreen<=0;
else if (hpos_0 & tick8)
widescreen<=1;
if(hpos_312 & tick8)
narrowscreen<=0;
else if (hpos_8 & tick8)
narrowscreen<=1;
end
end
//---------------------------------------------------------------------------
// VideoShift Register
//---------------------------------------------------------------------------
always @(posedge clk)
begin