This is challenge number 13 in the 2021 SANS Holiday Hack Challenge (https://2021.kringlecon.com/). Objective:
Write your first FPGA program to make a doll sing. You might get some suggestions from Grody Goiterson, near Jack’s elevator.
Exercise #4 Objective: Students must prove their design before being allowed to program an actual device. The student’s model must produce a 500Hz, 1KHz, and
2KHz square wave accurately AND accurately produce a square wave of a randomly chosen frequency. This tool will run the model under simulation, passing it the
appropriate register values and measuring the frequency of the resulting square wave.
Important: Students MUST perform all simulation tests with the SAME code. If the code is changed, all tests will need to be re-run.
To get started on this challenge, I listened to Prof. Qwerty Petabyte’s awesome talk on FPGA Design for Embedded Systems https://www.youtube.com/watch?v=GFdG1PJ4QjA. I had never touched Verilog or looked into FPGA designs prior to the challenge, so fortunately I was able to start by taking the professors blinking light code snippet from their presentation.
This code was a good start, but needed to be tweaked. First, I had to increase the size of the counter to 32bits
Secondly, we were working with a 125Mhz system clock so rather than a 100Mhz clock. So instead of using 100000000, we had to use 125000000 cycles per second in our equation. The second problem was ensuring that when building the clock divider code that it would output at a 50% duty cycle at all frequencies. Luckily, fpga4fun (https://www.fpga4fun.com/MusicBox1.html) explained that this could be done by adding a stage that first divides the output by 2.
Another important detail was how the frequency was being formatted. In the description it tells us that 9876.54Hz would be represented as 32’hf1206 or 32’d987654.
This meant that they were multiplying by 100 in order to not lose track of the decimal. So the solution needed to divide by 100 before being able to start producing a square wave.