Thursday, June 22, 2017

TTL Brainfuck Computer Part 8 - Clocking Progress

Part 1 (first) - Part 7 (prev) - Part 9 (next)

Note: there are plenty of pretty pictures below the fold

My pie-in-the-sky goal for the clock speed of this computer was originally 10 MHz. The LS series chips should be capable of 16 MHz, but that's not accounting for the poorly engineered environment of a hobbyist's breadboard computer. I figured 10 MHz would offer enough head room for the chips to cope.

As it turns out, the limiting factor is the EEPROM, which takes 150ns for the data to be valid after changing the address. If I set the address on the rising edge of the clock and read the data on the falling edge, that would be a period of 300ns or a frequency of 3⅓ MHz. While that's only one third of my original goal, it's well within the range of early personal computers (not that the performance will be remotely comparable).

So now I actually have a realistic target frequency, which likely can be pushed a dozen or two percentage points beyond spec (hello, overclocking!). Unfortunately the trusty 555 just isn't up to the task.

First of all, most sources I've read say it's only good up to about 1 MHz. But as an easily adjustable variable frequency (i.e. a single dial), the range and resolution are extremely limited.

I'm using a 1 M potentiometer right now. In order to have a usable range between, say, 0.5 Hz and 32 KHz, I have to manually switch from a 10 μF capacitor to a 10 nF. I'd need to go down another two orders of magnitude on the capacitor or up two on the resistor to reach 3⅓ MHz, and as I mentioned earlier, that would push the 555 well beyond its limits.

The adjustment resolution is similarly frustrating. Even though the potentiometer changes resistance linearly, the effect on clock rate highly non-linear. At the low speed end of the dial, a tiny movement makes a large difference in the clock speed. At the high speed end, the duty cycle changes more than the frequency.

Duty Dilligence

Speaking of the duty cycle, in order to keep the time spent on as close as possible to the time spent off, the capacitors and resistors need to be in particular ratios. To get a variable 555 timer with an even duty cycle, you would need both a potentiometer and a variable capacitor, each with an abnormally wide range.

As I mentioned in a previous post, the uneven duty cycle means the chips had to respond as if the clock was around 600-700 KHz even though the clock period was only 32 KHz. If I had a 3⅓ MHz clock with an 88% duty cycle, the chips would be seeing a pulse 18ns wide, which is equivalent to ~28 MHz with a 50% duty cycle.

As a quick fix for the duty cycle issue, I switched the inverter on the clock for a 74LS73, using one of the J/K flip-flops in toggle mode as a single-bit counter. Since it changes with each pulse rather than at each clock edge, its output will have a duty cycle as even as the 555's period. However, the period of the flip-flop's output will be half that of the 555, so I replaced the 10 nF capacitor with a 4.7 nF to maintain the frequency range I was used to.

The blue line on the oscilloscope is the output of the 555 timer. You can see how it spends significantly more time on (up) than off (down). The yellow line is the output of the flip-flop. You can see how it rises or falls every time the clock signal falls. Since I'm using a "raw" flip-flop instead of a counter chip, I have access to both the Q and Q' outputs to use as clock and inverted clock. The white and yellow LEDs are connected to them, respectively.

The Crystal Method

One of my first toy projects when I got back into electronics was a slowly shifting RGB mood light on Arduino. The LED itself is on a tiny breadboard and attached to the arduino with 3 signals and ground. I wanted to put the code on an ATTiny on the same board as the LED for a truly minimalist design. The ATTiny requires an external crystal to run at full speed, so I got a crystal assortment from 4 MHz to 25 MHz. I haven't made much progress on that project, but I do have everything I need for a stable, high frequency clock for the BFCPU.

The metal can on the left is an 8.000 MHz crystal. When it's resonating, it generates a near sine wave at that frequency, but it doesn't provide much current. The nearest chip is a 74HC04 hex inverter. As a CMOS chip, its inputs respond to voltage rather than current, but its outputs can provide a wide range of current. This allows it to act as a gateway between the crystal and the TTL chips. Since the output of the first inverter is part of the crystal feedback circuit, it runs through another inverter to actually provide the 8 MHz clock signal for the rest of the system.

The other chips on the board are all 74LS93s, the ripple-carry cousins of the '193 counters that make up most of the computer so far. These act as frequency dividers, giving me a selectable clock speed for any power of ½ of 8.000 MHz (4 MHz, 2 MHz, 1 MHz, 500 KHz, etc.), down to 0.4768 Hz, or about one cycle every two seconds. I'm only interested in a single "bit" at a time, so the ripple carry isn't going to be an issue.

The two 22 pF capacitors on the left corner of the board are part of the crystal resonator. The bypass capacitors straddling the high frequency chips from near to far are 1 nF, 4.7 nF, 10 nF, and 47nF, chosen experimentally to minimize noise on the outputs. There are also a couple of 100 nF capacitors across the power rails closer to the low frequency chips for the same purpose.

With nothing attached to the clock I was getting some pretty significant ringing in the outputs, sometimes as much as +6 V to -1.5 V peak-to-peak. It turned out to mostly be inductance from the oscilloscope probes, and pretty much disappeared when the clock was attached to other parts of the circuit.

Poor 555 clock, blinking away in solitude back there

In this shot, the long yellow lead on the left is connected to the 8 MHz output of the CMOS chip, and into the count enable circuitry of the Data Pointer and Data Register. The blue lead/oscilloscope probe is connected to the same signal.

The Data Pointer is set to count down, as you can see by the red lead going to Vcc just to the left of the blue LEDs. The yellow oscilloscope probe at the top is connected to the low order bit of the Data Pointer, which should be changing at half the period of the clock. Here's the result:

The scope is triggering on the clock signal, and you can see the measurement of 8.00029 MHz in the lower right. As expected, the first counter stage is flipping with every pulse. The edges don't quite line up because of the propagation delay in the counters and the NAND gates combining the clock with the control lines. If I had measured the clock at the counter input, the edges would be much closer. I left this running for an hour or so with "infinite persistence" and there wasn't a single glitch. So far so good for pushing the limits of my components!

One interesting thing to note is that TTL considers anything above 2 V to be a high signal. You can see here that the counter's output reaches just above 3 V, with a slight peak before switching. At lower frequencies, the voltage has some time to build further due to capacitive effects, and generally tops out around 4-4.5 V. You can see the effect of this in the previous image, where the 3 left-most LEDs are noticeably dimmer than the rest. The second bit from the left is the one switching the fastest, and is also the dimmest. You can see the same effect when I enable counting on the data register:

This isn't just an artifact of the camera or variability in LED quality or resistor value (which is also noticeable). If the switching speed were instantaneous, the LED would be spending just as much time on as it is off, so the only reason the brightness would be different is if the amount of current flowing is different. Since the resistance of the LED (plus resistor) is essentially fixed, the only way the current can be different is if the voltage is different.

Switching speed is not instantaneous, and doesn't change very much with clock speed. At high frequencies, instead of 50% on 50% off, it's more like 40% on, 40% off, 20% somewhere in the middle. This amplifies the effect of the already lower voltage.

Other Updates and Plans

The Data Pointer's up/down signals are now connected to the clock and control lines via NAND gates, just like the Data Register. The only buttons left are Clear signals. The Data Pointer doesn't ever load data, the load lines are all tied high.

In a couple of the pictures above you can see the beginnings of the program side. From top to bottom is the Stack Pointer, Stack RAM, Program Pointer, and Program ROM. I'm in the process of wiring up the Program Pointer to the Program ROM.

The milestone I'm shooting for next is to have a program in ROM control the Data Pointer and Data Register counters. But first I need to work the new clock design into the computer.

Part 1 (first) - Part 7 (prev) - Part 9 (next)

Wednesday, June 21, 2017

Complaint Breadboard

As much as I have ranted over the first breadboards I used for my Brainfuck computer, the company behind them has tried to help. After I left a one-star review on Amazon, they admitted that their manufacturing quality varied and sent me new ones. They were indeed a bit better than the first ones in a few specific ways, so I bumped my review up to three stars.

Over time, the experience was more unpleasant than first impressions suggested. They couldn't compete with a few other cheap breadboards I've acquired over the years. I won't list my grievances again here, but I decided to go back to a one-star review.

Pile of sh... re-assembled boards
Earlier this month they sent me an e-mail saying:

Sorry to disturb you again. 
I read from your review that you were dissatisfied with our breadboard. We sincerely apologize for that and we took your comment very seriously. After strict tests, we have developed a new breadboard which is of better quality. We have sent out 3 new breadboards to your address via China Post which will take about 15-20 days to deliver . The tracking number is [REDACTED] and you can track it here: ^.^
We care very much about how you feel and your review means everything to us. So we’d really appreciate it if you could give us an update review based on the new breadboard and our service. ^.^ 
Once again I apologize for all the inconvenience caused in this case and I look forward to your reply. Thank you very much!! 

The cynic in me thinks they mean "we found another supplier" when they say "we have developed", but I can't complain about the effort they're putting into making me a satisfied customer. I'm also a sucker for friendly emoji. :)

After bouncing back and forth between USPS facilities in San Francisco and Oakland for a week, they arrived a couple days ago. Whether the cynic in me turns out to be right or not, these new ones are vastly improved over the originals.

Bottom: old Elegoo board. Middle: new Elegoo board. Top: corner of my Brainfuck Computer built with Jameco boards
Two things are obvious at first glance: the plastic is much higher quality, both in look and feel, and the silk-screening is clearer. On closer inspection, there seems to be much more consistency in the way the contacts line up with the holes.

Here is a close up of the old board. You can see several areas where the contacts are directly under the holes. This would block entry of pins unless you put them in at an angle, which might go off to the side of the clip rather than into the center.

And here is one of the new ones for comparison:

One thing you might notice is that the contacts are much more visible in the new board when looking at an angle. You can see the bright spots on the left side of the holes on the left part of the board but on the right side of the holes on the right side of the board. This shows both that the contacts are more centered with respect to the holes and that they are closer to the surface of the breadboard.

Combined, these two fixes should alleviate most of the problems I had with the old ones. Insertion of components with multiple pins is much less frustrating, and retention of components is much better. In particular, these new boards seem to hold onto my DIP and tactile switches just as well as the Jameco boards.

The new boards also lay much flatter. The old ones had a noticeable curve out of the box, so they would tend to stretch and strain when assembled into a large panel. Here's a video demonstrating the improvements in curvature. Note: I'm using the one old Elegoo board that remained completely intact (I didn't cut the power strip off) to give it the best chances.

To be fair, there are still some problems with consistency. One of the three boards I received has a power rail where one contact in each block of 5 holes is misaligned:

I have not yet done any serious testing of their electrical characteristics since I'm not planning to assemble these into another computer. The contacts don't feel quite as eager to grip as the Jameco ones, so your mileage may vary.

All in all, I'm actually quite glad to have these three new boards. I had been using some of the old ones for a couple side projects, and am eager to transfer them over.

Update 2017-06-22:

All in all I'm happy with their performance. While the power connections feel a bit looser than the Jameco boards, the electrical contact seems pretty stable. The board has no problem handling an 8 MHz crystal clock with a 24 stage (6x4 bit counter) TTL frequency divider. There is virtually no noticeable effect on the clock outputs when I jiggle the power leads coming into or jumping across the board. I'm not sure how they'd behave in the bus strip/massive board configuration I'm using for the computer, but so far they look promising.

Wednesday, June 14, 2017

TTL Brainfuck Computer Part 7 - EEPROM woes

Part 1 (first) - Part 6 (prev) - Part 8 (next)

Another week, another head scratcher. I briefly mentioned past troubles getting an EEPROM writer going with my Arduino. I had another read of the data sheet, and went about rewriting much of the code. I didn't notice any glaring mistakes along the way, but did gain much more confidence that my code was doing what it should be.

Arduino Mega2560 connected to EEPROM

Alas, I still was unable to get any successful data writes onto the chip. I decided to try moving it over to a breadboard with some dip switches for addressing. The address pins are pulled down through 100K resistors, and the other side of the switch is pulled up through 10K resistors. These values should work fine since this is a CMOS chip and I'm not after super fast speeds. They just need to act as a voltage divider not a current source/sink, and I have verified that the voltages are around 4.5 for the high level and 0.3 for the low level.

The I/O pins are connected to one of my LED modules to show the output of the ROM, and to an 8-bit dip switch for entering data. The output enable and chip enable pins are the blue & yellow jumper wires, respectively, which I manually connect to ground or Vdd.

The load signal is connected to the common pole of a DPST switch (tucked above the LEDs next to the ROM), with the normally closed position connected to Vdd and normally open connected to ground.

Manual EEPROM reader/writer after some fixes

The pinout for the EEPROM identical to the RAM chips I'm using (the difference in address line numbers should be immaterial). According to the data sheet, doing a single byte write should have the same steps, as well: Disable output (set OE high), enable the chip (set CE low), and pulse the the Load input low. When the load input goes low, the address is latched; when the load input goes high again, the data is latched.

The main difference with the RAM chip is the timings. When you issue a write, you have to wait ~5ms for the operation to complete before the data will be readable (and before doing another single-byte write). That's definitely not an issue here since I'm doing this all by hand. Unlike the EEPROM in Ben Eater's computer, this one does not specify a maximum pulse width for the write cycle, so I just have a small RC filter to compensate for switch bounce.

Enabling output shows the expected 1s everywhere in the factory-fresh ROM. Before I had the data dip switch, to do a write I would:
  • Disable the ROM output
  • Move the I/O lines from the LED array to ground
  • Press and release the LOAD button
  • Move the I/O lines back to the LED array
  • Enable ROM output
This always resulted in a full bank of lit LEDs, suggesting the write failed. There were no signs of floating inputs to suggest it was rapidly changing addresses. To rule out connection issues, I swapped out the EEPROM for one of the RAM chips. Everything worked as expected.

By this point I was at a complete loss. I'd gone over the data sheet another few times. I added some bypass capacitors across the power strips near where the ROM connects to power & ground, I made sure the voltages make sense on the address pins and control signals. I used the dip switches to control the output enable pin to verify that the ROM senses the dip switch signals correctly.

The fact that 2 different software/microcontroller attempts failed, as well as the manual attempts described above, and with the successful test with a RAM chip, it became hard to stand by the "poor craftsman blames his tools" principle... Maybe the data sheet incorrectly omitted a maximum time for the write pulse? Maybe I got a bad batch? Maybe I zapped all four of them them with static? (not likely; I have a grounding strap I touch every time I sit down and have never even noticed a tickle).

Nearly ready to give up rolling my own, I started googling around for existing Arduino X28C256 EEPROM programming code. I came across a forum thread where someone said the chips come software write-protected from the supplier, despite the data sheet saying "The X28C256 is shipped from Xicor with the software data protection NOT ENABLED" (emphasis in original).

The Software Data protection feature requires sub-millisecond timing, so I headed back to my Arduino code, refactored things a bit, and set it up to send the unlock signal. It did not work. The data was still all 1s. In a desperate attempt to avoid an even more intense rage face, I gave up for the night.

The next day it hit me... While the order of the address and data lines is immaterial for normal usage, it is absolutely critical for sending the magic values to do the software unlock. I rearranged the address lines so that they match the pinout (the data already did) and re-ran the unlock. It still failed!

I need to clarify what I mean by "failing" here: The code that writes some test data reads it back and validates it against the test data. It turns on the Arduino built-in LED if it finds a mismatch. That light came on. I assumed this meant the write failed. Thinking that was a dead end, I put the ROM chip into my manual programmer to do some more investigation.

Lo and behold, when I enabled the chip, some of the bits were off! Not only that, but they seem to be the correct bits for the capital T in my test data: "This is ...". The next 3 addresses had the telltale "lower case" bit set, and the 5th address in the ROM had only the 32 bit set, which is a space. Apparently my write succeeded but my verification failed.

After one more pass over my code I finally realized what happened: the test data string in the original version called itself 64 bytes, but it was actually only 60 bytes. Apparently the 3 bytes beyond the end of the string are used for global variables. The writer would write whatever happened to be in those cells to the ROM, then failed verification when it read that value back but the Arduino's memory had changed. Here is the current code for the EEPROM programmer after fixing that issue.

I can now reliably unlock and write data to the EEPROM, and the unlocked chip works as expected in the manual programmer. Some further improvements I want to make are to use buttons to control the writing/unlocking and add a lock feature. This way it won't clobber the ROM every time it turns on, and I'll be able to use it to easily lock the chips once I have them programmed to avoid any unintended writes.

Until then, I've used my manual programmer to write the first program that will run on this computer:
Or in the CPU's encoding:

Part 1 (first) - Part 6 (prev) - Part 8 (next)

Friday, June 2, 2017

TTL Brainfuck Computer Part 6 - Breadboards and Puzzles, Old and New

Part 1 (first) - Part 5 (prev) - Part 7 (next)


The new breadboards arrived! At more than three times the price of the old ones and with a one year warranty, I wasn't sure exactly what to expect, but damn. Literally everything about them is an order of magnitude better, right down the quality of the packaging.

Here's a non-exhaustive list of the major differences:

  • The plastic looks and feels like ABS (think Legos) rather than HDPE (think plastic milk jugs)
  • The silk screening is much clearer and perfectly aligned with the holes
  • The boards lay flat by default, even when connected together
  • There are more plastic connection tabs along the long edge for better stability
  • There are connection tabs along the short edge so they can connect end-to-end, one in the center, and one on each power rail. This made the bus section of my megaboard much more stable
  • On visual inspection, none of the holes look like they're obstructed by the clips inside
  • They include a metal plate to stick on the back, I'm assuming for both shielding and structural support
  • The warm yellowish color is easier on my eyes under fluorescent/LED lighting. It also makes white wires easier to spot
  • Even though it's not white, it is completely opaque, so it ends up looking brighter than the white ones.
  • The double-sided tape is trimmed so it doesn't get in the way of the connection tabs
  • Pushing leads into the board doesn't push the contacts out through the other side
  • Actually, the contacts themselves deserve an entire section

3-2-1 Contact!

Imagine you grew up eating nothing but USDA choice sirloin. Now imagine going to Morton's or Gallagher's and having, for the first time, 21+ day dry-aged prime ribeye (or filet, if you prefer). This is what it was like the first time I pushed a lead into these breadboards.

At first it takes a little more force than the cheap ones to open the contact. This is a teeny bit annoying for components with long leads, but I found shortening the leads makes the entire breadboard experience better (much less stuff sticking up to accidentally knock over with fat fingers, easier to add more components nearby, etc.).

As long as the lead is going into the plastic hole, it will be captured by the contacts if you apply enough pressure, unlike the old ones. Those would often bend leads even if you push them in straight down & center.

The power rails hold on even more tightly, which again can be somewhat annoying the first time you insert a lead, but the benefits are well worth it. None of the connections show any signs of loosening after working on other pieces of the board. The bus was physically connected to the left half by only 4 wires (2 ground and 2 power) and never showed any signs of shifting.

The corner-to-corner resistance with the minimum number of connections was about 1.5 Ω compared to the 10-20 Ω of the others (I say 10-20, because any slight movement of the old ones would screw up most of the connections).

So yeah. Don't skimp on your breadboards unless you're a masochist.

Copy Pasta

I spent Thursday evening moving/copying the existing circuitry to the new breadboards. From the experience with the first iteration, I decided to make several changes. Here's the current state:

I realized I had been orienting the breadboards the wrong way around. All of the power & ground connections had to jump across the ground & power rails. This doesn't have any material effect on the functionality, but it did mean some modules would have to cross wires with another to reach power (aesthetically unpleasant), and all the power jumpers are 1/10" longer than they would otherwise need to be (marginally less cost-effective).

Because of this change, I had to move the power adapter to the other side of the assembly, which is actually more convenient for the way my desk is set up. I went ahead and brought the clock with it, and I added a couple switches to the clock for manual mode (not connected yet). I also added an inverting buffer to provide both inverted (yellow) & non-inverted (white) clock signals for when that becomes a thing.

Now all of the components I had working are on the left side of the board, which I'm calling the Data Side. The empty boards on the left will likely end up being the I/O areas. The right side of the board will be the Program Side, and house the program ROM, controller logic, etc.

I also made an effort to do much flatter, cleaner-looking routes. The thick bundles of wire in the previous version made it difficult to use the LED modules I made to monitor the counters, and it was much harder to follow individual wires between components.

Speaking of LED modules, I finished cutting the 16-bit one down to size. One of the LEDs has a janky connection. It would be pretty tedious to remove and I'd worry about damaging the ones nearby in the process, so I'm not going to bother for now.

Since minimal power consumption has never been one of my goals (I'd be using CMOS if it were), and since these breadboards are so much more reliable, I decided to remove one of the tri-state buffer chips (74LS244) from the Data Register and leave the counter inputs always connected to the bus.

I also modified the color scheme. I was originally planning to use rainbow colors to represent the bit positions of data lines (you can see that in the jumpers connecting the top & bottom bus sections on the old version), but that would get confusing with the colors being used for more general "kinds" of signals. I didn't end up using that, even in the previous build, but when I went to connect the new bus sections, it was a good time to solidify my plans.

In the previous build, I thought it was pretty clever how the RAM chip had its bi-directional bus interface in green, and the data register had "from bus" as blue and "to bus" as yellow. However, I was disappointed by the lack of orange overall, which you may notice is "my" color (along with blue). So I decided that all connections directly to/from the data bus would be in orange. I was already using orange for the LOAD signal on the counters, so the "load data from the bus" control lines will be the same color as the connections to the bus, which is neat.

I preserved the meaning of blue and yellow by using yellow to connect the outputs of the counters to the inputs of the tri-state buffers, and blue to "copy" bus signals from the buffer outputs (bus connections) to the counter inputs.

The borrow/carry signals for both the data register and pointer now use the same colors: green for up, brown for down (rhymes!). I also reversed the order of the counters, such that the most significant counter is on the right. This fits a bit more logically with how the count signal flows from the buttons and avoids a two long run of wires across all the chips.

This is actually why the RAM address lines look a bit in disarray; there are only 15 address lines and I had the most significant bit of the 16-bit counter connected to the address lines and I DIDN'T have the second-most significant one connected, so the memory would see 2 addresses repeated twice before moving on to the next. The look of the wires is a result of shifting them all over by one.

Old Puzzles

Swinging back around to the loop optimizations from last time, I had a couple fruitful discussions on Reddit and Hacker News.

The reason I hadn't settled on one way or another is that pre-processing allows me to achieve my performance goals during execution, but it makes the machine into a sort of combined compiler+CPU, rather than "just" a CPU. I feel strongly about both goals and have been struggling with a good way to word this newly discovered principle. The best I have so far is:
  • Purity - This is a CPU that runs Brainfuck; strong justification is required to do anything a "normal" CPU or Brainfuck wouldn't do
I really don't want to sacrifice purity for performance, but I also don't want to sacrifice performance for purity. Luckily there is a very precise modification I can make to the principles that can give me (nearly) the best of both worlds:
  • Constant time execution of each instruction - It should never take more than 3 cycles to execute any instruction
  • Amortized constant time execution of each instruction - Let i be some instruction at a particular location in a program. Let n be the number of times that instruction has been executed since the program started. Let En(i) be the average number of cycles required to execute i n times. The limit of En(i) as n approaches infinity must be 3 or less.
The "minimal number of execution steps" principle could also be defined in terms of En(i), but I think it's clear enough as-is.

I'm now planning to use a hybrid of the approaches from the previous post. The first time through a loop, if the loop condition is 0, it will skip over the instructions in the loop one at a time. In the process, it will index the start & end locations of the loop and any loops nested inside it. The next time the loop is entered, the indexed location will be used to jump to the end if the loop condition is 0.

Another advantage of this approach is that it provides an incremental pathway of increasing complexity for implementing the looping logic. I can start with the "pretend NOP" approach for linear time on both [ & ], add the stack-based approach for constant time ], then use the stack RAM as a bi-directional index for amortizing [.

In a similar vein, I'm also planning to use a data zeroing counter instead of erase-on-reset. The first time a memory location is moved to, it will synthesize a 0 (probably by clearing the data register and scheduling a write to RAM). Unless a program passes over memory locations without ever reading/writing them (a rather silly pessimization in the Brainfuck code), I believe this should match the performance characteristics of pre-zeroed RAM.

New Puzzles

So I got everything hooked up, and after fixing the address lines to RAM, I was still getting some weird issues. I could use the buttons to move up and down in RAM, but at some point the output would stop changing when going down. At first I thought this was just a coincidence; that a bunch of the high memory addresses, by quirk of die process, started out filled with ones on power-up.

Then I hooked up my 16-bit LED counter and the strangeness intensified. With the counters all starting at zero, count up seems to work as expected. If I hit the LOAD button to fill the counter with 1s (due to floating TTL inputs pulling high), counting up once sets it to zero, and it continues counting from there. However, counting down from zero leaves it at zero. If I count up past zero, it will count down to zero and stop. If I load all ones, it seemed to count down from there.

Until I realized that it was the most significant bits that were changing each count not the least significant. O.o When those most significant bits counted down below zero, suddenly all the least significant bits lit up, then the count-down would happen in the least-significant counter, until it reached zero. Then the most significant counter would start changing.

I double checked that borrow/carry-out connected to count down/up of the next chip.

I checked the continuity of connections between the chip pin entering the breadboard and the signal line entering the breadboard and found nothing wrong.

I checked the voltage levels of the borrow lines with a multimeter, and they all went low-then-high as expected when I pressed the down button while the data was all zeroes.

I triple-checked the borrow/carry-to-down/up connections by removing and re-inserting them. Same behavior.

I verified that the underflow works with the data register (2 chips) by having it count down. All of my assumptions checked out.

I replaced all the counter chips with brand new ones that came earlier this week and they behaved exactly the same. This also had the effect of quadruple-checking all of the clear, load, borrow, carry, up, & down connections.

Since a lot of the issues seemed to come from the most significant bit, I swapped out the capacitor on my clock to get a range of ~ 1 to 10KHz and watched "count up" work perfectly over all 16 bits. As soon as i switched to count down, it would quickly hit zero and stop.

I removed the LED bank and measured the output voltages directly, in case the LED board was causing issues. The outputs all stayed low when I tried counting down.

I added a 100uF capacitor across the power rail on the board with the counters in case it was a switching issue. Same behavior.

I rearranged all of the clear/load wires, both to reduce overlap and to double-check their placement, even though they both worked as expected.

Nothing even hinted at a cause until I removed the borrow/down connection between the 2nd and 3rd counter. Once I did that, the low order byte started working perfectly (basically a copy of the data register). After another pass with the multimeter, I still could not see anything wrong.

I started googling for issues with cascading counters, and everything I read says what I'm doing should work as expected.

Knowing how much I don't know about electronics yet, I figured there was some minutiae of electrical engineering I missed. Maybe I'm getting voltage spikes? Maybe the signals are ringing in the breadboard? It's time for the oscilloscope...

Since the clock signal is driven by a schmitt triggered inverter, the rising and falling edges are extremely clean. There is a tiny bit of ringing at the detection limit of my oscilloscope (70MHz), but at +/- 600mV, it should be well below the threshold for a false-1.

Triggering on the clock and watching the various inputs & outputs didn't produce anything unexpected until I probed the borrow output of the third chip. Suddenly the high order bits started flashing rapidly as if the probe of my oscilloscope was dropping it to ground and triggering the count. I don't see how that would be possible unless this was a CMOS chip; my probes are set to 10x attenuation and should not draw enough current to register as a 0. Maybe I'm wrong about that?

It seems consistent though. If I touch the probe to the clear line on the 3rd chip, the first two chips start behaving correctly. Wait what?

I started rearranging some of the connections again while the clock was running and the LED module was plugged in. When I removed the clear line between the 3rd and 4th chip, the first 3 counters started working perfectly.

Ok... Now I'm getting somewhere. I had noticed that the voltage on the Clear line was in the 0.6V range earlier. It seemed a little high, but it did make sense since you have to sink some current from TTL inputs to register a zero, and that is going through a 1K pull-down resister.

I replaced the 1K resister with a 470R and everything is peachy in counter-land.

I don't quite have a clear grasp on what's going on, but the gist of it seems that when underflowing, the fourth chip in the chain gets into a state where it drives the clear line so high the other chips register it as a signal.

I'm sure there's something in the 74LS193's data sheet that would've alerted me to this problem. I had problems before with pull-down resistors. My first electronics kits had CMOS chips (one had CD4000 series, the other had both that and 74HC) so I generally went with 10K. I switched to 1K when I was having some not-similar-enough-to-ring-a-bell issues with the Data Register.

Anyway, here's the thing hooked up to life support, running properly at 32 KHz. The fact that the clock is operating at 88% duty cycle means the actual swiching is happening in the hundreds of KHZ (around 700 if I mathed it right in my head). The pie in the sky goal I have in the back of my mind is 10 MHz for the final build, so this is pretty encouraging.

Next Steps

Once I get Now that I have this counter issue sorted out, all of the major pieces of the Data Side are in place for the +, -, <, and > operations (half of the language), so I plan to move onto the Program Side.

Last week I made a half-hearted attempt to program an EEPROM with my Arduino but didn't get very far. The code was quick & dirty so I'm sure it wasn't doing the right thing somewhere. Hopefully my next post will involve a look through program memory.

Part 1 (first) - Part 5 (prev) - Part 7 (next)