MicroZed Chronicles: GitHub Copilot

Programable logic is, of course, great for the implementation and acceleration of machine learning and artificial intelligence applications. I recently saw a tweet about GitHub Copilot and its ability to help developers write applications and act as an AI pair programmer.

This got me wondering how good it would be (if at all) in creating RTL designs using VHDL, of course there is a much smaller subset of training material. I requested access to GitHub Copilot and was granted access after a few days.

My normal editor is the VSCode with the TerosHDL extension, rather VSCode is one of the supported development environments. For this example, I disabled all other VSCode extensions to ensure what I saw was created by Copilot only.

Setting up Copilot to work with VSCode is straight forward. The first step is enabling the Copilot extension and then following the instructions.

Once the extension is installed, you are prompted to sign in and complete the API interchange to enable Copilot with VSCode.

The final step is to agree to the telemetry terms for the technical preview.

With Copilot enabled in VSCode, we can get started. There are a few ways we can work Copilot. The first is to start typing the code and Copilot will make suggestions about what we are writing to complete the structure. The other approach is to write a comment which describes the desired behavior of the process.

I followed the first approach of letting Copilot make suggestions to complete the code as I write it. After creating the process declaration, Copilot was capable of successfully completing the sensitivity list.

When I started to write the process, I wrote the begin statement and Copilot correctly suggested that I wanted to write a counter. However, the suggested code did not have a terminal count and output element.

If you do not like the suggestion, you can open the Copilot tab to see alternative solutions.

You can see several different ways of implementing the counter in the alternative solutions, all of which are correct per the LRM language.

With the counter implemented, I thought I would re-write it using a comment to declare the desired function. I wrote a 7-bit counter that has a terminal count. In this case it is 87.

Now the initially suggested code does not look quite correct. However, I wondered if the code was not correct because I had not accurately described the behavior in the comment. With that I mind, I updated the comment to be more descriptive of the behavior of the counter.

This time the counter seems to be more appropriate although the counter and count_done assignments outside of the process are not correct.

Having tested the counter, I thought I would look at its ability to create a shift register by shifting in an input into a 7-bit vector. In this approach, I again used a comment to reflect what I wanted the code to do.

The first suggestion for the shift register is not exactly how I would have written it, but it is legal VHDL and functional.

The final test was to see if a Copilot could create a state machine. In this example, I was going to use the state machine to control a counter. To get started, I declared the FSM states and the counter.

The suggested code again looked legal and implements the behavior requested. There are a few changes I would make including setting the counter to 127 in the idle state when the current state changes to decrement.

Now having looked at Copilot a little, I think it’s interesting and does, indeed, act like a pair programmer. It might not always tell you the perfect syntax or approach, but it does make suggestions which can increase productivity.

Of course, we must remember that the VHDL designs provide a much smaller dataset for training the OpenAI Codex that powers Copilot.

I expect performance will get even better as the dataset increases. Thankfully, I think we are still a ways away from the Star Trek model of engineering where we describe the behavior to the computer and the program is created.