There are several ways to optimize this code for speed. I've ignored the effects of interrupts which would take processor time and slow the code down. However, Listing 1 gives you the picture and may be sufficient. Then, custom hardware is the only solution. Let's begin with an approach that uses a purchased ISA-compatible PC prototype board.
Additional jumpers select the base address of the device and the size of the device.
For controlling our generator, we need a control register to start, stop, and give direction while a status register monitors the counting. Four load outputs load 8 bits of count value into the counter register. And, to synchronize the 40 MHz clock with the ISA slower clock, we need some synchronization registers and bits. The split between control and counting lends itself to be placed in multiple 22V10-type devices. I split my design into to MACH 210s.