Changes
CRTC
,[[File:CRTC Type 0.jpg|thumb|right|CRTC Type 0 (Hitachi)]]
[[File:CRTC UMC Type 0.png|thumb|right|CRTC Type 0 (UMC)]]
[[File:CRTC Type 1.png|thumb|right|CRTC Type 1]]
[[File:CRTC6845-Type2.jpg|thumb|right|CRTC Type 2]]
It's important to note that the CRTC chip is primarily designed for character-based displays. It explains why, on the Amstrad CPC, the video memory is organized into a grid of characters rather than a purely linear bitmap.
== Overview ==
The 6845 Cathode Ray Tube Controller (CRTC) is a programmable IC used to generate video displays. This IC is used in a variety of computers including the Amstrad CPC, Amstrad Plus CPC+ and KC Compact.
The CRTC is a simple chip. It is made up of counters and equality operators, tied by simple logic. All the complexity stems from its multiple independent implementations with their subtle differences.
This table lists the known ICs used, with their part number, manufacturer and type number.
{| class="wikitable"{{Prettytable|width: 700px; font-size: 2em;}}!|''Part number!''||''Manufacturer!''||''Type number (note 3)''
|-
|HD6845S||Hitachi||0
|AMS40489||Amstrad||3 (note 1)
|-
|AMS4022640226||Amstrad||4 (note 2)
|}
'''NOTES'''
1. The CRTC functionality is integrated into the CPC+ ASIC. This type exists only in the 464 PlusCPC464+, 6128 Plus CPC6128+ and GX4000.
2. This type exists in "cost-down" CPC464 and CPC6128 systems. In the "cost-down" the CRTC functionality is integrated into a single ASIC IC. This ASIC is often refered to as the "Pre-ASIC" because it preceeded the CPC+ ASIC. The CRTC functionality of the Pre-ASIC is almost identical to the CRTC within the ASIC.
3. In the Amstrad community each 6845 implementation has been assigned a type number. This type identifies a group of implementations which operate in exactly the same way. The As far as I know, the type number system was originally used by demo programmers.
It is possible to detect the 6845 present using software methods, and this is done to:
* warn that the software was not designed for the detected 6845 and may function incorrectly,
* to adapt the software so that it will run with the detected 6845
* In most cases, the type of the detected 6845 is reported.
==Programming==
The CRTC is not connected to the CPU's RD and WR pins, so it cannot detect the bus's I/O direction. Therefore, executing an IN instruction to the select or write functions causes the CRTC to write the unpredictable data provided by the high-impedance bus to its registers.
The VMA of the [[Gate Array]] is constructed from the CRTC MA and RA signals:
==== Interline border ====
On CRTCs 0/2, R1>R0 generates one byte (0.5µs) of border at the end of the raster line. On CRTCs 1/3/4, it does not.
To see the border bytes with your own eyes, type this BASIC line after reset:
</pre>
=== Signal delay === On all CRTCs, there is a 1µs delay in display between when the CRTC provides a video pointer, and when the Gate Array displays the corresponding 16-bit character. But on CRTCs 0/1/2, there is no delay for HSYNC. On CRTCs 3/4, the Amstrad engineers fixed the issue by adding a 1µs delay for the HSYNC signal to match the display signal. So now we have a bigger issue: on CRTCs 3/4, HSYNC occurs 1µs later than on CRTCs 0/1/2. Interrupts being dependent on HSYNC, this is a serious compatibility issue for time-sensitive code. It also explains why the CTM monitor has to be calibrated differently on CRTCs 3/4. Fortunately, the issue is easy to fix, by adjusting the HSYNC width with CRTC register 3. === Discolouration effect === On CRT monitors from other brands, a colour calibration can happen just after the C-HSYNC pulse. So even when a fully valid C-HSYNC pulse of 4µs is emitted, an HSYNC shorter than usual combined with a coloured border can induce a discolouration effect on those monitors. === Screen wobbling and Fine horizontal hardware scroll === These effects use invalid HSYNC signals, which are poorly supported by modern displays. However, these effects were used in commercial-era CPC games, so it's fine if you choose to use them. When R2 increases by 1, the screen is shifted to the left by 16 mode2 pixels. When R2 decreases by 1, the screen is shifted to the right by 16 mode2 pixels. When R3l increases by 1 (and if <6), the screen is shifted to the left by 8 mode2 pixels. When R3l decreases by 1 (and if >2), the screen is shifted to the right by 8 mode2 pixels. The shift is not instantaneous but follows a logarithmic attenuation across several raster lines. This property can be used to get even finer horizontal control by modifying R2/R3 on each raster line. [[Longshot]] has demonstrated [https://youtu.be/1q7RQykZoKY horizontal hardware scroll] with one-pixel precision in mode1 on all CRTCs. However, because you have to synchronize with each line, it takes a lot of CPU time if it's done in fullscreen (272 lines). <br> == VSYNC == The VSYNC signal from the CRTC is not directly connected to the display. It is passed to the [[Gate Array]] for further modification. See its wiki page. While VSYNC is active, all the CRTC counters continue to increment normally and addresses continue to be generated. On all CRTCs, while a VSYNC is ongoing, the condition VCC=R7 is ignored. So we cannot trigger a new VSYNC during a VSYNC. On CRTCs 0/1/2, the sole condition to trigger a VSYNC is that VCC=R7. While on CRTCs 3/4, it is necessary to have VCC=R7 and HCC=0 and VLC=0 to trigger a VSYNC. === Blocked VSYNC === On CRTC 0, if R7 is modified with the value of VCC when HCC<2, we are in blocked VSYNC. Then, the VSYNC can no longer occur on VCC=R7 until an unblocking condition is produced (VCC or R7 update). Source: §16.4 "Conditions to consider" of the CRTC Compendium === Ghost VSYNC === On CRTC 2, if a VSYNC is triggered during an HSYNC, the CRTC produces a ghost VSYNC. The CRTC then counts the lines as if a VSYNC were taking place by preventing a new VSYNC from occurring, but without the VSYNC pin being enabled. === PPI VSYNC === The VSYNC pin of the CRTC is directly connected to bit0 of port B of the PPI. There is no delay involved. On Amstrad CPC (not Plus!), it is also possible to reverse the direction of PPI port B to output a "fake" VSYNC signal directly from the PPI to the Gate Array. As an exception, the Ghost VSYNC of CRTC 2 overpowers the Fake VSYNC. === Subpixel vertical hardware scroll === By tuning very precisely when the VSYNC signal is sent to the monitor, Longshot has demonstrated [https://youtu.be/bSjRU6Wye00 vertical hardware scroll] with a precision of 1/128th of a pixel. Furthermore, subpixel vertical scrolling consumes very little CPU. === Mid-VSYNC === On all CRTCs, in both interlace modes, a mid-VSYNC is generated when VCC=R7 on the even field. The VSYNC pulse starts in the middle of the raster line, at HCC=R0/2. As an exception, on CRTCs 3/4, if R7=0 then mid-VSYNC will instead occur on the odd field. Source: §19.7 "Mid-VSYNC" of the CRTC Compendium. <br> == Interlace modes == Note: Some details about the CRTC interlace implementations are missing in this article. Consider it as an introduction to the topic. [[File:CRTC Interlace modes.png]] For a CRTC character line with n raster lines, R9 must be set to n-1 on all CRTCs, regardless of the interlace settings. The exception is for CRTCs 0/3/4 in IVM mode, where R9 must be set to n-2. === Interlace sync mode (ISM) === In this mode, the same information is painted in both fields to enhance readability. Reprogramming the CRTC is not necessary. === Interlace sync and video mode (IVM) === In this mode, alternating lines are displayed in the even and odd field to double the resolution. On the even field, the CRTC displays the lines for which VLC is even. On the odd field, the CRTC displays the lines for which VLC is odd. It is necessary to reprogram the CRTC as if we were building a frame of 624 lines. CRTC 2 is the exception: R4, R5, R6, R7 do not need to be reprogrammed as it considers each character line to be a double character line. === Interlace adjustment line === On all CRTCs, in both interlace modes, an additional line (the 625th line) is added automatically by the CRTC at the end of the even field. This line is added after the lines of the vertical adjustment mode. <br> == CRTC registers 6845 Registers ==
The Internal registers of the 6845 are:
{| class="wikitable"{{Prettytable|width: 700px; font-size: 2em;}}|''Register Index''||''Register Name''||''Range''||''CPC Setting''||''Notes''
|-
|-
|-
|02||Horizontal Total (-1)||colspan=3 style="text-align: center;"|WriteSync Position||00000000||6346||Width of When to start the screen, in characters. Should always be 63 (64 characters). 1 character == 1μsHSync signal.
|-
|13||Horizontal Displayedand Vertical Sync Widths||colspan=3 style="text-align: center;"VVVVHHHH|Write|128+14|00000000||40||Number of HSync pulse width in characters displayed. Once horizontal character count (HCC0 means 16 on some CRTC) matches this value, DISPTMG is set to 1should always be more than 8; VSync width in scan-lines.(0 means 16 on some CRTC. Not present on all CRTCs, fixed to 16 lines on these)
|-
|24||Horizontal Sync PositionVertical Total||colspan=3 style="text-align: center;"x0000000|Write|38|00000000||46||When to start Height of the HSync signalscreen, in characters.
|-
|35||Horizontal and Vertical Sync WidthsTotal Adjust||colspan=3 style="text-align: center;"xxx00000|Write|0|VVVVHHHH||128+14||VSync width Measured in scan-lines (Not present on all CRTCsscanlines, fixed to 16 lines can be used for smooth vertical scrolling on these) ; HSync pulse width in charactersCPC.
|-
|46||Vertical Total (-1)||colspan=3 style="text-align: center;"|WriteDisplayed||x0000000||3825||Height of the displayed screen, in characters. Once vertical character count (VCC) matches this value, DISPTMG is set to 1.
|-
|57||Vertical Total AdjustSync position||colspan=3 style="text-align: center;"x0000000|Write|30|xxx00000||0||Measured When to start the VSync signal, in scanlines, can be used for smooth vertical scrolling on CPCcharacters.
|-
|68||Vertical DisplayedInterlace and Skew||colspan=3 style="text-align: center;"xxxxxx00|Write|0|x0000000||25||Height of displayed screen in characters. Once vertical character count (VCC) matches this value, DISPTMG is set to 1.00: No interlace; 01: Interlace Sync Raster Scan Mode; 10: No Interlace; 11: Interlace Sync and Video Raster Scan Mode
|-
|79||Vertical Sync PositionMaximum Raster Address||colspan=3 style="text-align: center;"xxx00000|Write|7|x0000000||30||When to start the VSync signalMaximum scan line address on CPC can hold between 0 and 7, in characters.higher values' upper bits are ignored
|-
|810||Interlace and SkewCursor Start Raster||colspan=3 style="text-align: center;"|Write||CCDDxxIIxBP00000||0||CC: Cursor Skew (Only in CRTCs 0, 3 and 4)not used on CPC. DD: Display Skew B = Blink On/Off; P = Blink Period Control (Only in CRTCs 0, 3 and 4Slow/Fast). II: Interlace ModeSets first raster row of character that cursor is on to invert.
|-
|911||Maximum Cursor End Raster Address (aka Number of Scan Lines) (-1)||colspan=3 style="text-align: center;"|Write||xxx00000||70||Maximum scan line address Sets last raster row of character that cursor is on CPC can hold between 0 and 7, higher values' upper bits are ignored.to invert
|-
|1012||Cursor Display Start RasterAddress (High)||style="text-align: center;"xx000000|Write||style="text-align: center;"|Write||Read/Write||xBP00000||0||B = Blink On/Off; P = Blink Period Control (16 or 32 frames). Sets first raster row of character that cursor is on to invert.48
|-
|1113||Cursor End RasterDisplay Start Address (Low)||style="text-align: center;"|Write||style="text-align: center;"|Write||Read/Write||xxx0000000000000||0||Sets last raster row Allows you to offset the start of character that cursor is on to invertscreen memory for hardware scrolling, and if using memory from address &0000 with the firmware.
|-
|1214||Display Start Cursor Address (High)||Read/Write||style="text-align: center;"|Write||Read/Write||xx000000||48||On Amstrad Plus, bit7 of the printer port is controlled by bit3 of CRTC R12 (ie. bit11 of Display Start Address).0
|-
|1315||Display Start Cursor Address (Low)||Read/Write||style="text-align: center;"|Write||Read/Write||00000000||0||Allows you to offset the start of screen memory for hardware scrolling, and if using memory from address &0000 with the firmware.
|-
|1416||Cursor Light Pen Address (High)||colspan=3 style="text-align: center;"|Read/Write||xx000000||0||Read Only
|-
|-
|}
registers 18-31 read as 0, on type 0 and 2.
registers 18-30 read as 0 on type1, register 31 reads as 0x0ff.
<br>
<br>
=== Register access =CRTC Differences ==
|-
|0||R160||Light Pen Address (High)Select internal 6845 register||Write Only
|-
|0||1||R17Write to selected internal 6845 register||Light Pen Address (Low)Write Only
|-
|21||R100||Cursor Start Raster-||-
|-
|31||R111||Cursor End RasterRead from selected internal 6845 register||Read only
|-
|4} Type 1 {|{{Prettytable|R12width: 700px; font-size: 2em;}}|''b1''|Display Start Address (High)|''b0''||''Function''||''Read/Write'' |-|0||0||Select internal 6845 register||Write Only
|-
|50||R131||Display Start Address (Low)Write to selected internal 6845 register||Write Only
|-
|61||R140||Cursor Address (High)Read Status Register||Read Only
|-
|71||R151||Cursor Address (Low)Read from selected internal 6845 register||Read only
|}
|-
|0||1|C0=R0|2||3||4
|-
|-
|1||Horizontal Displayed||Write Only||Write Only||Write Only||(note 2)|0|C0=R1-1 (if R0>=R1note 3)
|-
|32|0|C0=R2Horizontal Sync Position||Write Only||Write Only||Write Only||(note 2)||(note 3)
|-
|43|0|C0=R2+R3Horizontal and Vertical Sync Widths||Write Only||Write Only||Write Only||(note 2)||(note 3)
|-
|54||Vertical Total||Write Only||Write Only||Write Only|01|R3h>0 : C0=0..R0 on the line R3h from Vsync (C4=R7note 2)R3h=0 : C0=0..R0 over 15 lines from Vsync ||(C4=R7note 3)
|-
|65|1|Always 1Vertical Total Adjust||Write Only||Write Only||Write Only||(note 2)||(note 3)
|-
|76||Vertical Displayed||Write Only||Write Only||Write Only|00|C0=0..R0-1 : VMA.Lsb=0xFFC0=R0 : VMA'.Lsb=0x00 (same cond if C0=R0=0note 2)|} {| class="wikitable"! R11 - Bit number! Bit value! Event(note 3)
|-
|07|0|C4=R4 and C9=R9 and C0=R0 : Last char of screenVertical Sync position||Write Only||Write Only||Write Only||(note 2)||(note 3)
|-
|18|0|C4=R6-1 Interlace and C9=R9 and C0=R0 : Last char displayedSkew||Write Only||Write Only||Write Only||(note 2)||(note 3)
|-
|9||Maximum Raster Address||Write Only||Write Only||Write Only||(note 2)|0|C4=R7-1 and C9=R9 and C0=R0 : Last char before Vsync(note 3)
|-
|310|0/1|Timer 16 CRTC framesCursor Start Raster||Write Only||Write Only||Write Only||(note 2)||(note 3)
|-
|411|1|Always 1Cursor End Raster||Write Only||Write Only||Write Only||(note 2)||(note 3)
|-
|512|0|C9=R9 : C0=0 to R0Display Start Address (High)||Read/Write||Write Only||Write Only||Read/Write (note 2)||(note 3)
|-
|613||Display Start Address (Low)||Read/Write||Write Only||Write Only||Read/Write (note 2)||(note 3) |0-|Always 014||Cursor Address (High)||Read/Write||Read/Write||Read/Write||Read/Write (note 2)||(note 3)|-|15||Cursor Address (Low)||Read/Write||Read/Write||Read/Write||Read/Write (note 2)||(note 3)|-|16||Light Pen Address (High)||Read Only||Read Only||Read Only||Read Only (note 2)||(note 3)|-|17||Light Pen Address (Low)||Read Only||Read Only||Read Only||Read Only (note 2)||(note 3)
|-
|}
=== Horizontal and Vertical Sync (R3) ===
<br>
==UM6845R and R12/R13 = Status register on Type = The UM6845R differs to other CRTC in respect of R12/R13. When VCC=0, R12/R13 is re-read at the start of each line. R12/R13 can therefore be changed for each scanline when VCC=0. Just like other CRTCs when RC==(R9-1 ), the current MA is captured for the next char-line. In demos to make a display compatible with all CRTCs program R12/R13 when VCC!=0. This will then take effect at the next frame start. == UM6845R status register ==
The UM6845R has a status register that can be read using port &BExx.
Bit 6 is set to 1 if there is a strobe input to the /LPEN signal. It is cleared to 0 when either R17 or R16 (LPEN address) of the CRTC are read. It signals there is a valid LPEN input. On Kevin Thacker's my CPC (arnoldemu) with UM6845R, it is triggered at power on, R17 and R16 have the values 0 when read.
Bit 5 is set to 1 when CRTC is in "vertical blanking". Vertical blanking is when the vertical border is active. i.e. VCC>=R6.
All the other bits read as 0 and don't have any function.
<pre>
10 MODE 1:' Reinitialize screen
[[File:CRTC timing small.gif]]
<br>
== Datasheets ==
==== Used by Amstrad ====
* [[Media:hd6845.hitachi.pdf|HD6845S (Hitachi)]] aka Type 0
* [http://en.wikipedia.org/wiki/6845 Wikipedia on the CRTC]
* [[Media:ACCC1.8-EN.pdf]] CPC CRTC Compendium - Latest (04/2024!) document containing in-depth info about CRTC programming on CPC.
* [[Media:CRTC Compendium Podcast.mp3]] The CRTC Compendium digested in podcast format
* [http://www.grimware.org/doku.php/documentations/devices/crtc CRTC documentation from Grimware]
* [http://quasar.cpcscene.net/doku.php?id=assem:crtc Quasar CRTC documentation (in french)]
* [[Media:Dossier CRTC(Ramlaid Mortel).pdf]] Les entrailles du CRTC
* [https://thecheshirec.at/tag/crtc6845/ Leçons CRTC (CheshireCat)]
* [https://martin.hinner.info/vga/pal.html PAL video timing specification]
==Related pages==