In this series of posts, I’m rescuing vintage microcontrollers from their decades-long purgatory in a drawer in my local makerspace. To this end, I’m attempting to use them to drive a LED sign made of WS2812 strips
So far in this series, I found some decades-old Signetics SCN8031AH and OKI MSM83C154 microcontrollers in a drawer, and worked out that they’re 8051 and 8052 derivatives (members of the MCS-51 family) from the 1980s, and calculated that it should be possible to make them control modern WS2812 LED strips.
In this installment, I get one of the microcontrollers set up on breadboards and get it to, at last, fulfill its purpose and BLINK AN LED!
Unfortunately, the makerspace has been closed for the last month or so for a move to a new location (which seems great - seriously check it out if you’re in the Asheville area, monthly open days will be starting again soon).
Happily, I took one of the OKI MSM83C154s home before everything was packed up, so I could still work on this project!
Cool Runnings
So, first thing to check: do these chips work? Perhaps should’ve worked that out before now - although, honestly, there are so many of them that surely some will. Let’s hope that the one I brought home is one of them!
The first thing I tried was to ‘free run’ the processor - also known as ‘NOP testing’1. A ‘NOP test’ is when you connect a processor to a system - sometimes called a “NOP tester” - that’s set up to fool it into thinking that all its code consists of NOP (No Operation) instructions. Whatever address the processor tries to read, it’ll get a NOP and move on to the next address, cycling through all of ‘memory’ before starting at the beginning again.
On MCS-51 processors2 the NOP instruction is conveniently represented by an all-zero byte, so this can be done just by pulling all the data lines down to 0V.
The obvious way to do this is with a simple breadboard - but there’s a practical problem here: the processors from the makerspace drawers are all square PLCC ICs that can’t plug into a breadboard directly. They have no ‘pins’, only metal contacts on the outer/bottom edges.
I ordered some PLCC sockets, thinking that I’d build a convertor myself - but I turned out not to need to! I also ordered an EPROM programmer from eBay, intending to use it with the makerspace’s EPROMs, and I was surprised when it came with a bunch of convertors - one of which converts 44-lead PLCC to 40-pin3 DIP!
Here it is on a breadboard.
In the middle is the MSM83C154 in the blue PLCC-to-DIP adapter. I’ve also connected a 13.875MHz4 crystal with two 22pf capacitors to its XTAL pins - you can see all that on the bottom right of the chip. To the left is a 5V regulator and power LED. The black wire connects EA to ground, telling the processor to run code from ’external’ RAM. I’m not sure why I also seem to have a blue wire connected to nothing on one end…
And here it is in ‘free run’ mode!
The small ribbon cable is connected to the address/data bus, and the resistors at the bottom are connected between the address/data bus and ground. They will drive it to ‘zero’ when it’s not being driven by the processor.
At first, I tried to connect LEDs to the address lines too, so that I could see them pulsing with the cycling of the memory - but they just all lit up. In retrospect, not surprising. Running at 13.875MHz, each ‘machine cycle’ (twelve clocks) is (12 / 13.875) = 0.86µs long. A NOP takes one machine cycle to [not?] execute. The code memory space is 64K long, so running through it all takes (64 * 1024) * (12/13.875) = 56,680µs, or 0.057 seconds. So even the ’top’ address line, which only changes once in a cycle, would be blinking at too fast a rate for the human eye to see.
Oscilloscope to the rescue! Here’s a screenshot with a few probes hooked up to the breadboard. Channel 1 is the 13.875MHz clock. Channel 2 is bit 0 of the address bus, channel 3 is bit 1 of the address bus, and channel 5 is ALE - Address Latch Enable. The 8052 pulses it when it’s put a valid address on the bus - it’s intended that an external component ’latches’ the address when the ALE line transitions from high to low.
If you look from left to right across the waveforms, tracing upwards from the ’transition to low’ on the ALE line, lines three and two (in that order) read5 00, 00, 01, 01, 10, 10, 11, 11, 00, 00, 01, 01, 10, 10… - a nice ascending binary count as you would expect if the processor were just sequentially reading RAM.
Why are the numbers repeating twice each?
Remember that the processor has an odd-to-modern-eyes design where the lower eight bits of the address bus are physically the same eight pins as the eight bit data bus. When the processor accesses ROM, it places the address bits onto the bus, pulses the ALE line to say ‘remember these address bits!’, then removes the lower eight bits in order to allow the ROM to control the data bus to send back the data. It does this twice - two appearances of an address, two ALE pulses, two memory accesses - in each twelve-clock-cycle machine cycle. This is so that it can retrieve multi-byte instructions fast enough to execute them in one machine cycle. For example, the instruction ADD A, direct, which adds an eight-bit number to the accumulator, takes up two bytes in memory: the first byte is the instruction, the second is the byte to be added. But it takes only one machine cycle to execute, so both bytes are required in the same cycle.
The reason we’re seeing the same address being read two times in a row in every machine cycle in this trace is that instructions are not all the same length, and the NOP instruction is just one byte long, so there’s no need to fetch another address. I guess the 8052’s designers thought it easier or simpler to just re-read the byte representing single-byte instructions again than it would be to only sometimes fetch twice in a machine cycle.
Incidentally, I find it fascinating to think that, back when these microcontrollers were being designed, it could be taken for granted that memory access could be twice as fast as processor execution! That’s unthinkable to modern engineers - as long as I’ve been aware of how computers work, memory access has been a bottleneck to execution. Even around the time the MCS-51 was being designed, processors like the Z80 and the 6502 were starting to incorporate so-called ‘wait’ or ‘ready’ pins that the memory system could hold on to while retrieving data to pause the processor while they worked - and things only got more elaborate from there. Today, your computer contains multiple ’levels’ of ever-faster caches to try to paper over the fact that memory is orders of magnitude slower than processors.
…but I’m digressing. It looks like the processor is working! Time to get some real code into it!
Thanks for the memories
Here’s where this project went a bit off the rails. I’m great at over-complicating things setting challenges for myself.
With the makerspace being closed, I had no access to the (historically-accurate) EPROMs that I was thinking I’d use to store code for these processors to run.
So I took a look around in my supplies, and what I did have are a bunch of ATmega microcontrollers. At first, I thought I could write a ‘ROM Emulator’ - as we’ve discussed in previous installments, even semi-modern microcontrollers are an order of magnitude faster than these ’80s systems. The idea was to connect the address and data bus to an ATmega328, and write a program to snoop on the bus lines and serve data from its flash ROM when the 8052 requested a read.
I was pretty sure I’d seen projects that did this before6 - but I still thought it wise to check that this would actually be possible, and it turns out it’s not. With some tight coding, it might be possible to sniff the ROM read requests fast enough - but when you add the time to fetch the requested data from the ATmega’s flash, it’s just not possible to get it onto the data bus in time to satisfy the 8052, even with the ATmega clocked pretty high7.
I also have a couple of 32K SRAM chips, though, so I massaged the idea a bit and came up with this: The ATmega will serve as a ‘manager’ for the system, and will be connected to the address/data bus of the 8052. On reset, the ATmega can hold the 8052 in its ‘reset’ state while it (the ATmega) uploads the ‘ROM’ image to the SRAM chip. The ROM image will be compiled into the program binary that’s running on the ATmega. During development, I can connect my computer to the ATmega in the usual way, and use a serial bootloader to upload both the ATmega code and the 8052 ROM image.
This is, of course, kind of absurd - the ATmega is way more powerful than the 8052. If I’m going to use one, why not just run the LED sign I’m trying to make with it!? I take solace in two ideas. First, it’s not entirely anachronistic to have a more powerful system control an 8052 during development. Even back in the day, this would be a normal thing to do. Second, this project isn’t about making a scrolling LED sign - there are way easier ways to do that! It’s about getting the old 8052s to drive a modern LED display. And, when development is done, I can flash a real EPROM with the ‘final’ code and there will no longer be a need for the ATmega.
I decided to split the 32K of RAM into two sections by making a simple mapping circuit.
The first 16K will be used as RAM. The second 16K will store the ‘ROM’. This is where the ATmega will write the ROM image on power up or reset before allowing the 8052 to take control.
The ‘RAM’ section is also mapped to respond to ROM reads in the second 16K address space. This will allow the 8052 to execute the runtime-generated code I calculated will be necessary to generate signals fast enough to talk to the WS2812 LED strips.
This might be clearer with a table. Here’s what SRAM address ranges are returned to the 8052 when it tries to read from ROM or RAM:
| Address requested by 8052 | ROM Read Returns | RAM Read/Write Returns |
|---|---|---|
0x0000 - 0x3fff |
0x4000 - 0x7fff |
0x0000 - 0x3fff |
0x4000 - 0x7fff |
0x0000 - 0x3fff |
0x0000 - 0x3fff |
Yes, it is correct that the RAM reads ‘wrap around’, with the second half being the same as the first half. This is a side-effect of the mapping circuit - but it does also mean that the 8052 is unable to write to the ‘ROM’ range, which I guess makes it more like ‘real’ ROM.
Here’s the circuit in schematic form!
You don’t need to see the details, really, but if you feel the need to zoom in, here’s a PDF of the circuit schematic.
In addition to the ATmega328 and MSM83C154, there are four 7400 series logic ICs (and the ones I’m using are even older than the MSM83C154!)
The ATmega is on the bottom left. The 8052 is at the top left. The 32K SRAM is on the top right.
The two thick8 blue lines represent busses, so represent multiple wires per line.
The ‘D’ata bus is connected to the lower eight bits of the 8052’s bus - the ones we discussed earlier that are used for both the lower eight bits of the address coming from the 8052, and the data coming back from the RAM.
There are two 7475 latches in the middle - they are attached to the lower eight bits of the bus, and are responsible for storing the bottom eight bits of the address when the 8052’s ALE signal tells them to latch.
The ‘A’ddress bus is connected to the upper lines of the address bus (these always carry the address so it’s not necessary to latch them), and the outputs of the 7475s, which always carry the lower address bits thanks to the latching.
The SRAM chip is on the top right.
The busses are also connected to the ATmega in the same way that they’re connected to the 8052 (when it’s writing to the ROM it simulates the way the 8052 writes, ALE and all).
The A14 bus line (carrying the most significant of the 15 bits required to address 32K) isn’t connected directly to the RAM - it’s connected to a three-input NAND gate from a 7410 IC, along with the RD and WR lines from the 8052 (remember, it has separate ROM and RAM addressing - it has a third line, PSEN, representing a ROM read request).
The only time the output of this three-input NAND will be 0 is when all its inputs are 1. If A14 is low - 0 - it will always output 1 (with that 0 obviously not all the inputs are 1). If A14 is high - 1 - it will output 1 if the 8052 is asserting RD or WR (and so one is low - these are active-low
signals), otherwise it will output 0 because all three inputs are high.
When accessing ROM, RD or WR are always high, so the NAND has the effect of inverting A14.
The collection of AND gates on the right, provided by a 7408 IC, serves two purposes - first, and important logically, they condense all the read (8052 RD, 8052 PSEN, ATmega RD) and write (8052 WR, ATmega ROMWR) signals into OE (“Output Enable”) and WR signals for the SRAM. Second, they introduce a slight delay to the signals. This is needed because it’s important not to tell the RAM to read or write until its address lines are stable - and for RAM access they don’t become stable until the A14-manipulating NAND gate is stable, which is after the 8052’s read or write signals are asserted.
The ATmega is also connected directly to the 8052’s RST (reset) line, so that it can hold it low to keep the 8052 in a reset state while it sets up the ‘ROM’, and its UART lines and reset line are connected to a TTL serial header in the usual Arduino-like manner to allow me to interface with the bootloader9.
Here’s the layout on a breadboard…
I’ve flipped the 8052 around from the earlier tests, just to make wiring easier. It’s on the top breadboard, on the top right.
Next to it, on the left (and, in the photo, somehow Vantablack), is the 32KB SRAM.
The ATMega is on the bottom right, with a USB serial adapter attached to it.
The two latches are in the bottom-center. The middle breadboard is really just to make bus connections easier, but I also placed the 7408 and 7410 logic ICs on the left and right of the middle breadboard.
The jumper wires make quite a mess, so to make things easier to follow, I used my skills-of-an-artist to tint the busses in the picture. The eight-bit data/address bus (the one that is used for data transfer and the lower eight bits of the address) is tinted red. The remainder of the address bus (the one that’s dedicated to the upper eight bits of the address) is tinted green, and the post-latching lower-eight-bits portion of the address bus in blue.
So that’s the circuit. What about the software?
Development environment
I’ve developed an affinity for PlatformIO in recent years, so it was my first stop for getting code into the system.
It does indeed have support for the MCS-51 platform, so I just went ahead and used that without much other research. For MCS-51 it uses the SDCC toolchain10, so code can be written in C as well as MCS-51 assembly. Until now I’d been thinking that most of the development for this project would be in assembly, but perhaps C will be fine for some parts.
PlatformIO also natively supports AVR, so it was easy to set it up to load the bootloader onto the ATmega32811, and to make the AVR ‘host’ program that contains the 8052 binary and uploads it into the SRAM on reset.
I’ve put the project up on GitHub12.
The avr-host directory contains the source code for the ATmega’s side of things.
The mcs51 directory contains the ‘real’ source code for this project.
I’ve set up the PlatformIO build system so that, when the mcs51 environment’s ‘upload’ target is invoked, it will in turn invoke the avr-host’s upload target. This will re-compile the host program with the just-built MCS-51 binary and upload it to the AVR, which will then reset the system and cause the new code to be run. It works very well!
I won’t cover that or the AVR code in detail here, since the spirit of this project is about using the 8052, but the AVR host code is reasonably straightforward13. First, it tests the SRAM by writing all over it - this was really useful when getting the breadboard wired up - then it writes the ROM image, reads it back to verify that it was written correctly, then sets all the AVR pins connected to the bus to ‘input’, and releases the 8052’s RST (reset) line to allow it to run.
For now, the MCS-51 main.c file exists purely to call into the assembly code in the assembly.asm file.
Here’s the assembly code:
.globl _P1
.globl _blink_loop
.area CSEG (CODE)
_blink_loop:
mov _P1, #0 ; Switch off all Port 1 pins.
acall _stall_for_1s
mov _P1, #0xFF ; Switch on all Port 1 pins.
acall _stall_for_1s
sjmp _blink_loop
_stall_for_1s:
; Save the registers we're going to use on the stack.
push a
push 0
push 1
; Need to stall for (13875000 / 12) = 1,156,250 cycles.
; Here, I use three nested loops to accomplish this.
; 9 * 255 * 255 = 585,225 - so, at 2 cycles per jump instruction,
; very roughly correct. Good enough for testing.
mov a, #9
outer:
mov r0, #255
middle:
mov r1, #255
inner:
djnz r1, inner
djnz r0, middle
dec a
jnz outer
; Restore the saved registers before returning.
pop 1
pop 0
pop a
ret
I started this just based on some examples I found on the web and advice from Claude - I now think a more idiomatic style would use ALL CAPS instruction mnemonics (MOV, not mov), and that my indentation is slightly weird - but that’s something I’ll worry about later.
The .globl directives are declarations of global symbols. _P1 is Port 1. It’s a byte (a uint8_t to use modern C parlance) that corresponds to the P1.x pins on the circuit schematic above. Each bit corresponds to one of the pins - P1.0 is the least significant bit in the byte, P1.7 the most significant. Setting the pins to output high or low signals is as easy as writing the corresponding bit pattern to _P114.
Declaring the symbols like this is like declaring an extern symbol in C - they have to be actually defined elsewhere. System headers provided by SDCC take care of that, so I don’t need to do it - although they actually are at specific memory locations. Port 1 is the byte at memory location 0x90. You can just write to this address to set the state of the processor pins. This might seem surprising if you’re used to the levels of abstraction modern computers have built up, but many modern microcontrollers still work like this!
The last .globl is _blink_loop - this tells the assembler that the label we define below should be exposed globally - specifically here it’s called by the C code in main.c.
_blink_loop itself just loops alternately setting _P1 to 0xFF (all pins on) and 0 (all pins off), calling _stall_for_1s between each set.
_stall_for_1s is sort-of interesting. There’s no OS to provide timekeeping routines to me, so this contains three nested loops, calculated to need about a second to execute. It also saves and restores the registers it uses - a, r0, and r1. Not strictly necessary here, but good practice. The 8052 does contain hardware timers, but no need to get into that complexity yet.
It’s alive, Jim
Does it work?
I connected up an LED and resistor to the 8052 P1.0 pin, and…
YES! BLINKING!
I’ve heard it said that making an LED blink is 80% of the way to doing anything with a microcontroller. And it certainly feels good to see it happening after so much effort!
Next time, I hope to get at least a couple of WS2812-controlled LEDs blinking.