C64 Display Latency Meter for ESP32-C3
An ESP32 camera project for ESP32-C3.
Parts and sourceBefore you start
Confirm board compatibility and supported firmware targets in the upstream documentation.
Upstream README
Real-Time Video Latency Measurement System for Commodore 64
ESP32 + photodiode based system that measures real-time display latency and visualizes it on a Commodore 64 with a SID soundtrack.
Commodore 64 latency measurement system in action in its natural environment.
Key Features
- Real-time latency measurement using photodiode
- Trigger signal generated from Commodore 64
- ESP32 interrupt-based timing system
- Measurement visualization on Commodore 64
- SID playback during measurement
- PAL sync signal shaping circuit
Project Status
- Prototype is working
- PCB design in progress
- Future improvements: Timing accuracy, PCB integration, bug fixes/glitches.
Introduction
It all started when I got my Commodore 64 Ultimate (which is awesome!) I became curious about the video signal latency comparison on the analog RGB/SVIDEO and digital HDMI video (which is perfect) outputs, so I filmed them both on high speed camera (960 fps) and counted the frames by toggling the screen color by poking 53281. After doing several measurements this way I started wondering how it could be measured automatically. That question eventually created this project.
Content
- Key Features
- Project Status
- Introduction
- Project Overview
- Basic Principle
- Demo Video
- System Overview
- Sequence Diagram
- Hardware Circuit Diagrams
- ESP32 Firmware
- C64 Program
- Compilation
- Video Experiments with C64U RGB Signal and Scaler
- The Analog Sync Signal Shaping Circuit
- Evolving of the C64 UI
- Measurements
- Final Thoughs and Retro
- License
Project Overview
Project measures in real-time the display latency by detecting the moment C64 changes border color and compares it to the moment when the display actually changes brightness and that is detected by the photodiode circuit. During the development, extra investigations were done on the C64 analog RGB video sync signal when experimenting with video scaler and ended up designing a PAL sync signal shaper circuit.
Main components:
- Commodore 64 Ultimate
- ESP32-C3 microcontroller
- Photodiode luminance detector circuit
- GBS-8200 scaler with GBS-Control mod
- Custom PAL sync shaping circuit
The system measures the time difference between:
- Trigger signal generated by the C64
- Trigger signal from actual brightness change on the display
The latency is calculated on ESP32 and then sent back to the C64 and visualized in real-time.
Basic Principle
From the beginning I was thinking to keep the C64 border color change as a simple and reliable trigger event. Detecting the moment when actual screen luminance level change on the screen was required and also calculating the time difference. So some fast photodiode circuit was needed. Earlier I was thinking of playing around with the C64 user port pins and I had a cheap ESP32-C3 board laying around I could use, so I planned to use it for border color trigger signal from C64 and also for the trigger signal for actual screen luminance level change. After that ESP calculates the time difference of the signals. Also that time I started to think it would be ultimate coolness to show the actual value on C64 screen, but that was only a key to unlock the play with madness.
Demo Video
The demo video shows the system in action, including real-time latency visualization on the C64, oscilloscope measurements, and hardware setup. The soundtrack in video is SID tune Messy One is from the Krestage 3 C64 demo.
Link: https://www.youtube.com/watch?v=7gQhzAM_-_c
System Overview

Logical view of the system components and relations.
Sequence Diagram

Sequence diagram of the latency measurement system.
Hardware Circuit Diagrams
Latency Measurement Circuit
Schematics for the latency measurement system.
Bill of Materials
- LM393 Comparator
- ESP32-C3
- Resistors, 220R, 1k, 2k, 3k, 10k, 1M
- 50k trimmer
- Voltage shifters 3.3V/5V
- Some wires
PAL Sync Shaping Circuit
Schematics for the PAL sync shaper circuit.
Bill of Materials
- LM1881 Sync separator
- 2x NPN BJT transistors
- Resistors, 680R, 1k, 10k, 1M
- Capacitors, 0.1uF
- Some wires
ESP32 Firmware
Software on ESP was relying on interrupt routines the trigger signals coming from C64 and from the comparator output of the luminance detector.
- Interrupt from C64 trigger signal --> timestamp stored in microseconds
- Interrupt from photodiode trigger signal --> time difference is calculated
- Result is sent back to C64 via 7-bit parallel I/O interface, range 0-127 ms.
More precise number in microseconds is sent by serial and option to display on the small OLED screen of the ESP.
C64 Program
C64 software was written in assembly language and was relying on raster beam interrupt. It is configured to trigger when the raster beam reaches a specific scanline, sets the border color to white and sends trigger signal by user port PB7 to ESP. After one second border color is set back to black to get ready for the next measurement and finally user port pins PB6-PB0 are read and converted to 7 bit value and displayed on screen and visualised. Sid player interrupt is set to occur once per frame.
In short:
- Raster beam interrupt triggers
- Border color changes to white
- C64 User port PB7 pin sends trigger signal to ESP32
- C64 reads number from User port PB6-PB0 pins and visualises it
Compilation
Place your own favourite SID file to the same directory as the .asm file as tune.sid and compile with Acme.
cp <your_sid_tune.sid> tune.sid
acme c64-lagbar-v5.asm
Video Experiments with C64U RGB Signal and Scaler
Since C64U has an option to output either SVIDEO or RGB signal, I was interested of trying out the RGB and soldered a cable from an old VGA cable which I tested on my good old Benq LCD that accepts 15kHz signal and it worked. Then was thinking if I could be able to use it on my even the older but greater 19" CRT from the last century, but that needed a scaler since it does not accept 15kHz signal. I was interested of trying different options and then somehow was drifting into trying out the cheap GBS-8200 scaler + building GBS-Control modification that was said to give some good latency results and great option for my measurements.
The Analog Sync Signal Shaping Circuit
Introduction
My first try outs with plain GBS-8200 was a great disappointment, it gave really distorted and laggish image for some reason. My hope was that doing a GBS-Control mod would help it out, but appeared it really gave some help, but best result with that was using NTSC and disabling synchwatch so image would stay still, but running C64 in NTSC is not really an option in long term, but that gave opportunity to do some measurements. Conclusion was that the scaler didn't like that signal so much.
Investigations
I couldn't help it but wanted to know what was going on and took oscilloscope out and checked out the CSYNC signal, even though I didn't have clear idea how the hell PAL signal should actually look like. I then was noticing some "garbage" now and then and it appears to be frame sync intermediate signal part and wanted to look at it closer. I did comparison with the Amiga 500 CSYNC signal and it looked simpler to me and with some Googling and AI help, appeared some signal cleanup would be able to do with different options. Option of detecting VSYNC frame synchronisation part from signal with LM1881 sounded tempting and just fun to try out for me.
Solution
So I drifted to trying out LM1881 and it seemed to detect the vertical sync out of the CSYNC, I was thinking maybe with that information could help doing something with the signal. So with just a "hunch" and no idea how PAL frame sync intermediate part should really work, I ended up trying somehow to clean out the "strange looking" part during the VSYNC pulse. It was done with two NPN transistors I had laying around, first one inverting the control signal and second one pulling the CSYNC signal to ground when VSYNC was detected. For my great amazement, it actually worked and the PAL signal was distort free and flowed like a charm. I just could not believe it, but it just kept on working.
After drawing a truth table of the signals, it appears that it looks like just a AND logic operation of signals! But then realized that using of AND IC gate, it would ruin the signal levels.
Comparison images before and after

Comparison before and after PAL shaping circuit with scaler.
Evolving of the C64 UI
One of the most time consuming but rewarding part was the C64 programming, since I had not much experience doing it, but by small babysteps at a time forward and help of AI was not so painful (really painful). During development the visualization on the C64 gradually evolved into something that started to feel like a small demo. Progression was pretty much as follows.
Screen captures of versions: v1-> v2 -> v4
v0: Just a blinker
First version had it's raster beam interrupt working and border color was changing black and white and trigger interrupt on user port PB7 was working.
v1: Just a number
In this version was added displaying a value that was send back from ESP just plain number on the top left corner of the screen.
v2: Just a number with colors
Idea in this was to show number in different colors of the latency ranges:
- Green = low latency (0-5 ms)
- Yellow = medium latency (6-12 ms)
- Red = high latency (12-127 ms)
Those colors suited nicely on this purpose when latency was drifting between 0-20 ms.
v3: History bar graph
Motivation for showing history bar graph was the observations of latency drifting strangely from 0 to 20 ms. Idea was use the same colors on the bars than in the latency number so that level shows if latency threshold is passed. Multicolor bar was showing the all thresholds nicely when for example high latency was occurred.
v4: History bar graph with SID player
Since I was staring just plain numbers and colorbars on screen and I am a fan of C64 demos and sidtunes, idea popped in mind that what about to integrate a sidplayer on the program. Result was a program that any tune.sid could be included and now numbers and colorbars could be stared forever while listening sids. It almost felt like watching a little demo.
Latency drifting note
Probably scaler clock signal inaccuracy was causing actually quite funny latency drift on the graph. Adjusting clock via GBS-Control was causing graph to lean backwards or became steeper! More precise clock signal would probably solve the issue, that's maybe worth of trying out.
Here seen the adjustment of the frequency from the GBS Control UI.
Measurements
Latency Detector on Scope
Latency detector triggers beam and luminance detector trigger. From the oscilloscope we can see latency be here about 2ms when comparing beam trigger signal and detect trigger difference. NOTE: Luminance detector trigger is active LOW.
Photodiode on CRT seen on scope. CRT phosphorous glow fading seen between frames but when the beam hits causes sharp edge.
Photodiode on LCD image: LCD Pixel response seen on the image. Also LCD brightness PWM pulses seen.
Note for trigger signals image: Actual latency is directly seen as time between those two triggers.
PAL Shaper On Scope
Here is seen original CSYNC signal that scaler didn't like and VSYNC signal from LM1881. VSYNC and CSYNC are active LOW.
Here is seen filtered result of the CSYNC signal and VSYNC signal from LM1881. VSYNC and CSYNC are active LOW.
Note for comparison image: almost all middle "short pulses" from the intermediate frame signal filtered away. Some distortion in the middle.
3D view of Kicad drawing sketch for PCB of the PAL shaper.
Measurement Notes
When comparing different display technologies (CRT, LCD, OLED...), measurement includes display's internal characteristics. For example on LCD measurement includes the pixel response time that could be relatively long time to pixel to lit up enough to trigger the luminance detector, while on CRT reacts almost instantly. Adjusting comparator trimmer precisely gets more important on LCD. So that should be kept in mind while doing comparison between those.
Is it Accurate?
I am not able to give currently some absolute numbers of the accuracy in the academic manner, but the results are consistent with oscilloscope timing measurements with earlier from the high fps camera observations. In practice it can be used for comparing latency measurements in millisecond range.
Latency Results
| Device | Technology | Video Mode | Latency(ms) |
|---|---|---|---|
| 23" Benq | LCD | RGB @VGA | 12 |
| 40" TV | LCD | SVIDEO | 70 |
| 19" Mon. | CRT | RGB @scaler | 1 |
| 5" Mon. | LCD | HDMI | 8 |
NOTE: Higher latency values on LCD displays include pixel response time, which affects the photodiode detection threshold. Pixel response time for example on 5" LCD looks to be around 5ms on oscilloscope, which could leave signal path latency of 3ms. Really precise tuning of the treshold adjustment trimmer is required.
Final Thoughs and Retro
Overall this project has been an interesting and fun learning journey, including those unexpected obstacles and really interesting detours. What started as a simple curiosity about video latency turned into a small system combining retro hardware, signal investigation, embedded programming, and even a bit of demo scene vibes. So this was not just about retro, it was interest for different technologies and respect of inventions and people who have done those with limits of the technology available.
License
This project is licensed under the MIT License.
