r/beneater•
gfoot360

Potential design for a CRTC chip

VGA

Yesterday I remembered that a while ago I got an ATF2500C (CPLD), but hadn't used it, lacking a suitable programmer. The plan was to use it to make a CRTC, among other things. I figured I'd see if it would fit, and then decide whether or not to buy a new programmer to work with it.

So a CRTC is a chip like the 6845 or the SCN2674 which manages an old-fashioned display. It mostly counts up through memory addresses and generates sync signals - so it's responsible for the bulk of what we implement in homebrew video circuits. Having all this done by one IC, while maintaining the character and 5V-ness of a homebrew breadboard computer, is really appealing.

I think I mostly managed to make it fit in the IC - enough to generate a 640x480 signal, with full vertical resolution and 160-address horizontal resolution, along with CPU clocks and write timing signals to allow the CPU to write to the video memory, and support for vertical hardware scrolling.

This means there are four horizontal pixels per memory access, so you have to figure out what you'll do with that - stick with 160 pixels across the screen, or output two or four pixels per memory read e.g. using shift registers - I've made a few videos about those options in the past. My current 640x480x3bpp build uses this memory access rate, along with shift registers, to output 640 pixels horizontally.

Here's the pinout I ended up with. The main limitation here is the number of output pins available - there are none free, the three unused pins are input-only. Things like the vertical scrolling were basically free, because they didn't require any more output pins and seemed a good way to use up input pins.

                           ______________
                          |     crtc     |
                clkin x---|1           40|---x
                !wreq x---|2           39|---x
                      x---|3           38|---x VI8
                   V0 x---|4           37|---x VI7
                   V1 x---|5           36|---x !srpe
                   V2 x---|6           35|---x !ramwe
                   V3 x---|7           34|---x disen
                   V4 x---|8           33|---x hsync
                   V5 x---|9           32|---x vsync
                  Vcc x---|10          31|---x H7
                   V6 x---|11          30|---x GND
                   V7 x---|12          29|---x H6
                   V8 x---|13          28|---x H5
                 phi1 x---|14          27|---x H4
                 phi2 x---|15          26|---x H3
                   H0 x---|16          25|---x H2
                  VI0 x---|17          24|---x H1
                  VI1 x---|18          23|---x VI6
                  VI2 x---|19          22|---x VI5
                  VI3 x---|20          21|---x VI4
                          |______________|

clkin is the 25.175MHz input clock. !wreq is from the CPU requesting to write to video memory (e.g. decoding some address range plus RWB). VI0..8 are the vertical start address, used for scrolling.

All other pins are outputs. phi2 is the CPU clock as you'd expect; phi1 is the same thing but inverted, for convenience. V0..8 is the vertical coordinate, to be fed to the RAM address pins along with H0..7 for the horizontal coordinate. vsync and hsync go to the monitor. disen is high if RGB output should be enabled (i.e. inside the visible 640x480 region). !ramwe connects to the video RAM's write-enable pin, and goes low briefly during CPU writes to video RAM. !srpe is "shift register parallel enable" - this goes low for just one pixel to tell your shift registers to load data from RAM.

The hardware scrolling is implemented just by resetting the vertical address to the supplied address in VI0..7, at the end of the frame. Under the hood this does require a lot of buried logic, with two separate vertical counters.

To perform writes, the CPU needs its own access to the video address and data buses, e.g. using transceivers, and they should be enabled only during phi2, e.g. using phi1 as the active-low output enable for the transceivers. Potentially you might be able to connect phi1 to the CPU's bus-enable pin, and not use transceivers, but I don't like doing that because it means you don't get to see what the CPU wants to do until phi2 begins.

On the whole this looks pretty good to me, so I'll probably go ahead and buy the programmer and see if it works. It'd be great to hear any thoughts on the interface though! There are three input pins left, and maybe 10-15 buried logic flipflops. The input pins could be used for trivial things like active high/low chip select pins. Possibly supporting text mode with 12-row characters could be optionally enabled by these pins; or having an extra divisor on the horizontal count, so it only increments once every eight pixels? Or one less divisor, as it turns out the phi1 signal is actually the same as "H-1" would be?

It would be really nice if this chip could also do the address bus multiplexing - however I think that would require too much buried logic. It raises the question though of whether some of the per-pixel logic should move back out of this chip, to leave room for the multiplexing. A simple PLD like a 16V8 is very capable of doing the bus timing, and could maybe focus a bit more on the things that happen after the video RAM (clocking latches, loading shift registers, etc).

Another thought is that reducing the scope to just text mode would probably save a few output pins here and there, and maybe allow more interesting features to fit in alongside.

Posted Oct 10, 2022, 8:50 PM UTC

1814
gfoot360OP•

Here's the code in case it's interesting:

Name     crtc ;
PartNo   00 ;
Date     09/10/2022 ;
Revision 01 ;
Designer George Foot ;
Company  gfoot360 ;
Assembly None ;
Location None ;
Device   v2500c ;

/* Pin 1 = clock.  Pins 1-3, 17-23,37-40 are inputs. */
/* Pins 4-9, 11-16, 24-29, 31-36 are outputs (x24) */
/* Virtual pins 41-88 also exist, probably referring to the flipflops inside each macrocell. */


/* Inputs */
pin 1 = clkin ;
pin [2,17..23,37..38] = [!wreq, VI0..8] ;

/* Outputs */
pin [4..9,11..16,24..29,31..36] = [V0..8, phi1, phi2, H0..7, vsync, hsync, disen, !ramwe, !srpe ] ;

/* Buried logic */
pinnode [41..51,53..54] = [vreset, qclk, hclk, VV0..9] ;


Field subclk = [phi1, qclk] ;
Field vert = [VV9..0] ;
Field overt = [V8..0] ;
Field vinit = [VI8..0] ;
Field horiz = [H7..0] ;

hclk.d = subclk:'d'3;
hclk.ck = clkin;
hclk.ar = 'b'0;
hclk.sp = 'b'0;

vclk = hsync;

ramwe = wreq & subclk:'d'1;

srpe = subclk:'d'3;

vreset = vert:'d'524;
hreset = horiz:'d'199;

hsync.t = horiz:'d'163 # horiz:'d'187;
hsync.ck = hclk;
hsync.ar = hreset;
hsync.sp = 'b'0;

horiz.ck = hclk;
horiz.ar = 'b'0;
horiz.sp = 'b'0;

vsync.d = vert:'d'489 # vert:'d'490;
vsync.ck = vclk;
vsync.ar = 'b'0;
vsync.sp = 'b'0;

vert.ck = vclk;
vert.ar = 'b'0;
vert.sp = 'b'0;

overt.ck = vclk;
overt.ar = 'b'0;
overt.sp = 'b'0;

/* 480 = 01 1110 0000 */
/* 524 = 10 0000 1100 */
!vdisen = VV9 # VV8 & VV7 & VV6 & VV5;
disen.t = hreset & vdisen # horiz:'d'159 & vdisen;
disen.ck = hclk;
disen.ar = hsync;
disen.sp = 'b'0;

subclk.t = [qclk, 'b'1];
subclk.ck = clkin;
subclk.ar = 'b'0;
subclk.sp = 'b'0;

H0.t = hreset & H0 # !hreset & 'b'1 ;
H1.t = hreset & H1 # !hreset & H0 ;
H2.t = hreset & H2 # !hreset & [H0..1]:& ;
H3.t = hreset & H3 # !hreset & [H0..2]:& ;
H4.t = hreset & H4 # !hreset & [H0..3]:& ;
H5.t = hreset & H5 # !hreset & [H0..4]:& ;
H6.t = hreset & H6 # !hreset & [H0..5]:& ;
H7.t = hreset & H7 # !hreset & [H0..6]:& ;

V0.t = vreset & (V0 $ VI0) # !vreset & 'b'1 ;
V1.t = vreset & (V1 $ VI1) # !vreset & V0 ;
V2.t = vreset & (V2 $ VI2) # !vreset & [V0..1]:& ;
V3.t = vreset & (V3 $ VI3) # !vreset & [V0..2]:& ;
V4.t = vreset & (V4 $ VI4) # !vreset & [V0..3]:& ;
V5.t = vreset & (V5 $ VI5) # !vreset & [V0..4]:& ;
V6.t = vreset & (V6 $ VI6) # !vreset & [V0..5]:& ;
V7.t = vreset & (V7 $ VI7) # !vreset & [V0..6]:& ;
V8.t = vreset & (V8 $ VI8) # !vreset & [V0..7]:& ;

VV0.t = vreset & VV0 # !vreset & 'b'1 ;
VV1.t = vreset & VV1 # !vreset & VV0 ;
VV2.t = vreset & VV2 # !vreset & [VV0..1]:& ;
VV3.t = vreset & VV3 # !vreset & [VV0..2]:& ;
VV4.t = vreset & VV4 # !vreset & [VV0..3]:& ;
VV5.t = vreset & VV5 # !vreset & [VV0..4]:& ;
VV6.t = vreset & VV6 # !vreset & [VV0..5]:& ;
VV7.t = vreset & VV7 # !vreset & [VV0..6]:& ;
VV8.t = vreset & VV8 # !vreset & [VV0..7]:& ;
VV9.t = vreset & VV9 # !vreset & [VV0..8]:& ;
10
The8BitEnthusiast•

Very interesting! Just to give you an idea of how illiterate I am in the vintage video space, I had to lookup what CRTC actually stood for 😂 Got the gist of it now, thanks for sharing this! First time I get exposed to CPLD coding. Is that kind of chip mostly limited to combinational logic? I won't pretend I understand all the code, but that's the hint I am getting from the counter sections

2
IQueryVisiC•

With the inner workings of the counters exposed, I wonder if it would be faster / precise to count down and check for zero from high to low.

2
gfoot360OP•

I think the cost of counting is the same either way, so it won't be faster, but it can simplify the logic for the comparisons to do this, which could free up resources for other purposes.

However, I want the output horizontal coordinate to increase from zero, otherwise video data will be backwards in memory from the CPU's perspective.

For the vertical counts it is more possible, as there's already a separate counter for the outputs. The vdisen logic actually already relies on the meaning of zero. But changing the phase of the internal vertical counter may still be a benefit, as I had to promote vreset to be a buried logic term in order to reduce the complexity of some of the counter expressions. Having the vertical reset better aligned may allow vreset to not need to pass through the macrocells, freeing up a buried logic flip-flop. It's not completely clear though, and my head hurts!

2
IQueryVisiC•

ah, I stole that idea from the C64 sprites. I think VIC-II compensates for the flic while it buffers a line of sprite. Makes me wonder why C64 can neither flip nor mirror sprites. Counting can only be faster by adding a carry flag between a low and a high part. High part lags by one cycle. 6502 style. It works with sprites (low part is enough to address within sprite buffer .. 32px max), but is too much hassle to deskew for memory addresses.

You can count up negative numbers and add a 1-flag. Ah: Just one counter per axis. After subtraction, calculate the zero flag from msb to lsb. I always forget that despite the carry stuff going on, the zero zeros in from msb to lsb. This may be an argument against using XOR to detect equal. This should even work with a split counter where the high byte is lagging by one cycle.

1
gfoot360OP•

Kind of - you can AND (&) together as many inputs as you like, inverted or not, then OR (#) together up to a certain number of those "product terms". That then feeds into the output macrocells. If you pass it straight out (optionally inverted) then it's called a combinatorial pin and it changes asynchronously whenever its inputs change. But the macrocells also include flip-flops, and in most cases I am feeding the sums-of-products into the flip-flop inputs - ck is the clock, d the input data, ar is async reset, sp is sync preset. The flip-flop results can either be outputted (called "registered outputs") or fed back into the matrix as additional inputs ("buried logic").

The definition language allows you to write more complex boolean expressions, especially XOR ($), but it has to translate them into sums of products, and you can quickly run out of product terms this way.

There's also the notion of a T flip-flop which I hadn't come across before. T stands for Toggle. It behaves like a D flip-flop with a XOR gate on its input, I.e. D = T XOR Q. So the input says whether to toggle its state on the next clock, or stay as it is. It is very useful for signals that need to change state infrequently.

Simpler PLDs like ATF22V10 don't have T flip-flops and buried logic, and don't support as many product terms per sum. Also they have other restrictions like all the flip-flops share the same clock, reset, and preset.

2
The8BitEnthusiast•

Cool. I should have RTFM a bit... the flip-flops are shown right there on the first page of the datasheet, 48 of them. That makes this chip quite capable. This is definitely FPGA-style goodness but with a TTL chip flavour!

2
forstuvning•

I wonder if there are 70s/80s crtcs floating around that would actually support 25mhz in. I’m down to about 4 IC’s dedicated to video now and I’m pretty happy with that.. but that’s only monochrome. Going PLD still feels too much like cheating to me - but on the other hand you certainly showed you already know how to do it the other way around :)

2
gfoot360OP•

Somebody here mentioned the SCN2674 a few years ago, and I got one to experiment with. It is just about VGA-capable - I made a couple of videos about it, just displaying fixed text I think, from ROM rather than RAM. It still requires some glue logic, especially as its outputs are multiplexed. I also don't think they are still in production, I got mine second-hand from France I think where they seem to have been more popular.

2
forstuvning•

Yeah, they don't seem as plentiful on Aliexpress as the TMS9918 for instance - pretty sure that can't handle VGA.
Maybe there are some early PC video cards that'll give us some hints.
I might be happy with what mine does now - but I'm certainly not done with it.

2
gfoot360OP•

As far as I know, CGA and EGA cards used the 6845, which is also out of production. I think from VGA onwards they used custom silicon, integrating the CRTC with the other functionality, like ALUs and so on.

Note that none of these really ran their CRTCs at 25MHz. VGA 640x480 and EGA 640x350 were only 16 colours, and used four bit planes, reading one byte every 8 pixels, i.e. about 3MHz as far as the memory address counters are concerned. 320x200 was 256 colours, and must have been the same rate, but each group of 4 wide pixels was spread across the planes. These are both easy enough to do on a PCB, but tonnes of wires on a breadboard!

2
forstuvning•

I think I'll have to take a closer look at the MC6845 and clones - the marketplaces are overflowing with that one too. 1$ solution to a 5$ problem :)

2