Starting out with a CPLD

Something that I haven’t documented here is my project for a simple Z80 computer inspired by the designs for the RC2014 ( https://rc2014.co.uk/ ). I’ll get around to that soon but, here’s what it looks like so far:

The cable in the middle is a FTDI serial-to-usb connection. This means the computer is text-based with no display adapter of its own.

There are a large number of projects out that there that have built VGA-compatible display adapters using FPGAs (Field Programmable Gate Arrays) or CPLD (Complex Programmable Logic Devices). I’m not the person to go into what the differences or benefits are for each class of device but suffice to say that these are not microcontrollers like an ATmega (Arduino) and they’re not full computers like a Raspberry Pi. The reason that a FPGA/CPLD would be used is, typically, for speed.

In the case of a display adapter, where displays are refreshed in terms of MHz rather than KHz, these devices will achieve that. Timing is a critical thing and microcontrollers or Raspberry Pis have overheads that make this more difficult.

With that in mind, I got some CPLDs from eBay in the form of development boards (https://www.ebay.com/itm/5V-MAX-II-EPM240-CPLD-Minimum-System-Core-Board-Development-Board/173615395086). The development board has a voltage regulator to provide the 3.3v for the chip and breaks out all the pins on the chip.

The next step was to work out how to program these things. The CPLD that the eBay devices use is a Altera MaxII EPM240. To get this programmed, I needed to download an IDE called Quartus. This is available at http://fpgasoftware.intel.com/. It’s available for Windows and for Linux. My main workstations are Macs so this caused me to scratch my head for a short while as I have a gaming PC that runs Windows and I’ve installed Quartus on that for a different project but it’s not ergonimically in the right place for me to do extended work on it.

I downloaded the Linux version ( 19.1 Lite ) and spun up a new Ubuntu VM on my VMware Fusion installation.

Note: The Quartus software is supported on Ubuntu 16.04. I know other people have it working on later versions but, I had some library issues with libstdc++ when I first installed on Ubuntu 19.04. For me, it was less of a headache to install Ubuntu 16.04 instead.

For a CPLD at least, it is programmed using a JTAG connector. For those of us who are cheap, a USB Blaster can be used.

There’s a small number of configuration changes that you need to make to allow the USB Blaster to work with Quartus under Linux.

Firstly, when the device is connected, a /dev file is created. By default, the permissions on that file prevents Quartus from accessing it. This can be fixed by adding a udev rule ( remember this is Ubuntu 16.04, your mileage may vary ):

Create a file under /etc/udev/rules.d called 40-usbblaster.rules which contains the following:

SUBSYSTEM=="usb", ATTRS{idVendor}=="09fb", ATTRS{idProduct}=="6001", GROUP="users", MODE:="0666"

You can determine the values for idVendor and idProduct by using lsusb

root@ubuntu:/etc/udev/rules.d# lsusb
Bus 001 Device 002: ID 0e0f:000b VMware, Inc.
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 005: ID 09fb:6001 Altera Blaster
Bus 002 Device 004: ID 0e0f:0008 VMware, Inc.
Bus 002 Device 003: ID 0e0f:0002 VMware, Inc. Virtual USB Hub
Bus 002 Device 002: ID 0e0f:0003 VMware, Inc. Virtual Mouse
Bus 002 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub

In this case, Altera Blaster is the device, the idVendor value is 09fb and the idProduct value is 6001

The GROUP value should be the same as the group that your Linux user is in.

Note: The MODE value is the permissions to use to create the /dev/file. Pay attention to the := instead of =. This is important. If you don’t include the ‘:’, the user permissions will not be set correctly.

The next step is to set up a JTAG server to run. I haven’t researched what this is used for but, if it isn’t running, Quartus can’t talk to the USB Blaster.

Ubuntu has a useful document at https://help.ubuntu.com/community/QuartusII#Setting_up_the_JTAG_server which details how to get that configured.

Once the IDE was set up and I could get it talking to the CPLD, I watched a very instructive set of videos from PyroElectro available on YouTube:

These videos start from the very basics and gave me the grounding I needed to get started with my own device.

I’ll leave you with a couple of very short videos of my tests using the CPLD. The first one shows me lighting a LED from a button press. This tests the input and output on the board.

The next video shows the LED blinking based off the internal 50MHz clock that the dev board has. The internal clock is an important feature that I’ll be using for the VGA display. I used the code from https://bigdanzblog.wordpress.com/2015/01/22/chinese-altera-max-ii-development-boards-and-hello-world/ to achieve this. For this particular dev board, the clock pin is at PIN_64.

Leave a Reply

Your email address will not be published. Required fields are marked *