 Single-stepping through a program is a great way of not only debugging a non-working program, but also getting a good understanding of what the system is doing, effectively in slow motion. For hardware, you can also single-step things, but sometimes the hardware needs to run at a certain speed in order to work. You can use a logic analyzer to examine the system running at full speed, and while these things have certainly fallen in price, they're still a bit on the costly side. Remember last time I said this TMS-9900 CPU couldn't be single-stepped using the clock? Link down below. Well, there's this hold signal that when you assert it, the processor finishes up whatever memory access it's doing, then asserts its hold acknowledge signal and tri-states its other outputs, and when you un-hold, the processor starts up again. Here's a timing diagram of that. We see that the time from hold to hold acknowledge is between 1 and 9 clock cycles, depending on how many memory reads the current instruction needs to execute. So maybe I can hold the processor, then when you want a single-step, you un-hold and then hold again as soon as you can. I have this retro-looking switch that I can use as a single-step run switch. Flipping it up will stop the CPU, and flipping it up again each time will single-step it. Flipping it down will let the CPU run. Quick! To the state machine! Maybe something like this. This is a mealy machine, right? Or maybe a moor? No, a mealy. Definitely a mealy. Right? Hang on, I gotta go dust off my engineering degree. Okay, quick review. Here's what you'll see when you look up moor state machines. The next state depends on the current state and the inputs, and the outputs depend on the current state. In a mealy machine, the outputs depend on the current state and the inputs. Here, I'm going to pull this output logic block into the next state logic block. Now you can see that the moor is just a special case of the mealy. It's easy to get them confused like I always do, but really, who cares? There's some state and some logic, and sometimes the output depends on the input, and sometimes not. Okay, let's have each state get its own flip-flop. You get a flip-flop, and you get a flip-flop. Anyway, now it's much simpler, and I can tell what's happening. Here's what's going on. We start off in state zero, assuming that the processor is being held. The hold signal to the CPU is asserted for each state with a dot in it. Now, when I flip the switch to the step position, I go to state one, then I release the switch, and we can go to state two, which releases the CPU from hold. Ignoring this bottom part where the processor turns hold acknowledge off, I go to state three, which immediately asserts hold again. The CPU acts hold, and I go back to state zero. That single step's the CPU. Now, if instead I flip to run, I go to state four. When I release run, I go to state five, which releases the hold on the CPU, and when the CPU runs, I go to state six and stay there. I can hit run as much as I want at that point, and nothing will happen. Only when I hit step will I go to state seven, and when I release step, I go to state three, which asserts hold. And there you have it. That's all the gates, and we come up with a total of six chips required, not counting the flip-flop chips. Now, six chips is an awful lot, but if you think about it, we really have just four outputs, hold and three bits of state, and six inputs, step, run, hold acknowledge, and three bits of state. That's basically a 64 by 4 ROM. Now, back in the day, you could get a small programmable ROM chip, but they don't make those anymore. The smallest parallel input-output ROM I've found is 8K by 8, which is way overkill for this. Now, I hate to bring up the A word, but in this case, I think it's appropriate and not cheating at all. I can program an AT tiny microcontroller to act as a ROM. It would be a slow ROM with access times on the order of a microsecond, but for this application, that's perfectly fine. And if you traveled back in time to the 80s, you could just tell them it's a ROM with a funny pinout, and they wouldn't know any better. Heavy. Okay. I have this Adafruit Pro Trinket hooked up, because I don't have an AT tiny yet, and I have a 500 kHz clock on the flip-flops, which is the frequency of a single clock phase. The flip-flops are in this 74175 quad D-type flip-flop chip. I only needed three-quarters of the chip for the three state bits. Okay, power up. These three LEDs show the current state. This LED shows the hold signal that we output, and this LED and button simulates the hold-acknowledge signal from the CPU. Finally, this LED indicates that the switch was put in the run position, and this one indicates the step position. First, I'll reset. Great, we're in state zero, and hold is being asserted. The hold-acknowledge signal is also being asserted. Now I'll step. Great, we went to state one, then two, and hold went off. I'll press this button to simulate hold being unacknowledged. In other words, the CPU is running. Okay, now we're in state three, and hold went on again. I'll acknowledge by releasing the button, and we're back in state zero being held. Great. We can do that over and over, single-stepping each time. Now, from state zero, if I hit run, we go to state four, then state five, and we see that hold has been turned off. I'll push the button to simulate hold unacknowledged, and now we're in state six. As I said earlier, at this point we can hit run as much as we like, and nothing will happen to the state machine. When I press step, we go to state seven, and then state three. Hold is turned on, I simulate the CPU acknowledging hold, and we're back in state zero. From here, I can single-step or run again. Maybe this will work. I'll have to hook it up to the CPU to find out. I also got these displays so I can see what the address and data lines are up to while I'm single-stepping. That will probably be the next video. Anyway, thanks for watching.