Introduction: HackerBox 0069: MEGA Retro

With HackerBox 0069 we will explore the Arduino MEGA for projects requiring a lot more storage and a lot more I/O pins. Configure the MEGA+WiFi board to program both the ATmega2560 and the ESP8266 from the Arduino IDE. Interoperate between the ATmega2560 and the ESP8266 to extend WiFi functionality to the Arduino MEGA platform. Learn about the classic Z80 and 6502 8-bit microprocessors and use the original microprocessor chips in both versions of the RetroShield. Interface the RetroShields to the Arduino MEGA+WiFi to execute original code images including BASIC language ROMs.

HackerBoxes is the monthly subscription box for enthusiasts of electronics and computer technology - Hardware Hackers - The Dreamers of Dreams.

There is a wealth of information for current and prospective members in the HackerBoxes FAQ. Almost all of the non-technical support emails that we receive are already answered there, so we'd really appreciate it if you can take a few minutes to read the FAQ.

Supplies

This Instructable contains information for getting started with HackerBox 0069. The full box contents are listed on the product page for HackerBox 0069 where the box is also available for purchase while supplies last. If you would like to automatically receive a HackerBox like this right in your mailbox each month with a $15 discount, you can subscribe at HackerBoxes.com and join the revolution!

A soldering iron, solder, and basic soldering tools are generally needed to work on the monthly HackerBox. A computer for running software tools is also required. Have a look at the HackerBox Deluxe Starter Workshop for a set of basic tools and a wide array of introductory activities and experiments.

Most importantly, you will need a sense of adventure, hacker spirit, patience, and curiosity. Building and experimenting with electronics, while very rewarding, can be tricky, challenging, and even frustrating at times. The goal is progress, not perfection. When you persist and enjoy the adventure, a great deal of satisfaction can be derived from this hobby. Take each step slowly, mind the details, and don't be afraid to ask for help.

Step 1: Arduino MEGA+WiFi

Just like the Original Arduino Mega2560, the Arduino MEGA+WiFi features an ATmega2560 Microcontroller. But the Arduino MEGA+WiFi also features an ESP8266 Microcontroller for Wi-Fi magic.

The MEGA+WiFi also features a MicroUSB Connector and a CH340 USB chip. Most modern operating systems include a driver for the CH340. You can check here to see if your OS comes with a CH340 driver and learn how to install one if not.

Coupling between the CH340, ESP8266, and ATmega2560 can be configured using various switches. The schematic diagram for the MEGA+WiFi board is attached here in PDF format. The configuration switches are presented in the next step.

The ATmega2560 Microcontroller is big brother to the ATmega328P, which is used in the Arduino UNO and Nano. The ATmega2560 incorporates 256K of Flash Program Storage, while the ATmega328P only provides 32K of Flash. The ATmega2560 Input/Output (IO) ports include 54 Digital IO Pins and 16 Analog IO Pins, while the ATmega328P has only 14 Digital and 6 Analog IO pins. All of the extra IO pins on the MEGA are great when you need to interface a lot of signals as we will do later with the RetroShield boards.

The ATmega2560 supports a 16MHz Clock rate, 8K of SRAM, 4K of EEPROM, and several Digital Communication Peripherals: four UARTs, five SPI, and one I2C.

The ESP8266 provides support for Wi-Fi 802.11 b/g/n on 2.4 GHz.

Step 2: Configure and Program MEGA+WiFi

If you do not have the Arduino IDE installed on your computer, install it first.

Also, configure the Arduino IDE to support ESP8266 boards by following steps 1-5 HERE.

Programming the ATmega2560

Set the 8-position DIP switch to [0011000X]. The X is logic notation for "don't care" because switch eight is not connected to anything. This setting connects the CH340 (USB interface) to the ATmega2560. The CH340 can then be used to program the ATmega2560 from the connected PC.

Run the Arduino IDE

Select Tools > Board > Arduino AVR Boards > Arduino MEGA or MEGA 2560

Open File > Examples > Basics > Blink

Hit the Arrow Icon (UPLOAD)

You should now have a blinking LED on the MEGA+WiFi Board

Feel free to play around with the delay() parameters in the loop function (and re-upload) to change the blink timing in different ways.

Programming the ESP8266

Set the 8-position DIP switch to [0000111X]. This setting connects the CH340 (USB interface) to the ESP8266. The seventh switch being ON (1) puts the ESP8266 into flash mode. The CH340 can then support programming ESP8266 from the connected PC.

Select Tools > Board > ESP8266 Boards > Generic ESP8266 Module

Grab the attached file SSIDscan8266.ino and upload it to the ESP8266 (arrow icon)

Running Code on the ESP8266

Switch to running (7th switch to 0)

Set the 8-position DIP switch to [0000110X]. This setting leaves the CH340 (USB interface) connected to the ESP8266. However, with the seventh switch OFF (0), the ESP8266 is in run mode instead of flash mode.

Open the Arduino IDE Tools > Serial Monitor

Hit the red RESET button on the MEGA+WiFi board

The ESP8266 will search for any 2.4GHz access points and spit their SSID text out to the Serial Monitor (via the CH340)

Running the ATmega2560 and the ESP8266 in Concert

Set the 8-position DIP switch to [1111000X]. This setting connects the CH340 (USB interface) to COM0 of the ATmega2560 and also connects the ESP8266 to COM3 of the ATmega2560.

Make sure that the slide switch on the board is set to COM3 (RXD3 and TXD3) and not COM0 (RXD0 and TXD0). This specifies which COM port of the ATmega2560 is connected to the ESP8266.

Select Tools > Board > Arduino AVR Boards > Arduino MEGA or MEGA 2560

Grab the attached file EchoCOM3toCOM0.ino and upload it to the ATmega2560 (arrow icon)

Open the Arduino IDE Tools > Serial Monitor

Hit the red RESET button on the MEGA+WiFi board. The ESP8266 is still running the code that spits out any identified Wi-Fi SSIDs. However, that serial output is now directed into COM3 of the ATmega2560. The new code we just put on the ATmega2560 will then echo that data out of its COM0 which is now connected to the Serial Monitor via the CH340.

Step 3: MEGA Prototype Shield With Breadboard

The MEGA Prototype Shield V3 mounts atop any Arduino Mega (including the MEGA+WiFi) to support building custom circuits.

Components can be soldered onto the shield or circuits can be configured on the mini solderless breadboard that is included. The hole patterns support DIP package components and there is also a set of SOP28 SMT pads.

A 2x20 Pin Female Header is included in the parts box. This header can be solder onto the Prototype Shield to extend the MEGA's two-row digital IO header (with power pins) if so desired.

Step 4: RetroShield Components

Plastic Component Box Contains:

  • Z80 DIP-40 Microprocessor
  • 6502 DIP-40 Microprocessor
  • TWO DIP40 Chip Sockets
  • 2x20 Pin Female Header
  • FOUR 100nF Capacitors
  • TWO 22pF Capacitors (Tinted Pink)
  • TWO 680 Ohm Resistors (Tinted Orange)
  • TWO 33 Ohm Resistors (Tinted Blue)
  • TWO 5mm RED LEDs

There is also a 2x40 pin male header with a red plastic insulator. That header is packed outside of the plastic box due to the length of the header.

As already discussed, the 2x20 pin female header can be used with the MEGA Prototype Shield. The remaining components are for the two RetroShield kits. All of the components are needed, so be careful to not lose any of them.

A word about the two DIP-40 Microprocessors: Obtaining old-ass chips like these by the thousands is not an easy task. As true hardcore retro, the ones we have are mostly "pulls" which means they were plucked (often unsoldered) from ancient equipment, (somewhat) cleaned up, and then thoroughly tested. They work great, but sometimes there is a bit of a sticker on the housing, some solder residue on a pin, and so on. Most importantly, they need to be treated with care and respect - mechanically speaking. The pins may not be as robust as pins on a new chip.

If you ever need some new stock microprocessors like these (especially in reasonably lower quantities), check these sources:

Z80: ZILOG Z84C0006PEG
Mouser Part No: 692-Z84C0006PEG
Digi-Key Part No: 269-3892-ND

6502: WDC W65C02S6TPG-14
Mouser Part No: 955-W65C02S6TPG-14

Step 5: Microprocessor System Architecture

Let's consider a typical microprocessor system architecture. By this we mean the architecture of a system that includes a microprocessor, and not the architecture within the microprocessor.

The circuit diagram shown here is from Grant Searle's Minimal Chip-Count 6502 Computer design. This is not a design that we are building here, but it is a perfect example to use for this discussion.

Take a look at NAND gate U6:B. When Address lines 14 and 15 are high, the ROM chip's Chip Enable (CE) is activated. This is called "address decoding" and means that any access to addresses that look like "11XX XXXX" (A15 and A14 high) will decode into the ROM chip. In hex that bit pattern maps to C000-FFFF which is where the ROM chip will be located in the "memory map". This is a great place to have ROM because the 6502 comes out of reset and reads addresses FFFC and FFFD to obtain its "reset vector". This vector is the address where execution begins. So those addresses in the ROM are programmed to hold the start address of the actual executable code, which is presumably also somewhere in the ROM. This is how the 6502 boots up.

More address decoding: Note that the Chip Enable (CE) pin on the RAM chip is tied to A15, so when address 15 is low, RAM CE will be low. Low means CE is "active" which enables the RAM chip. The bar over the CE indicates that CE is active low (#CE is another way to indicate an active low signal). Accordingly, binary addresses that look like "0XXX XXXX" map into the RAM space. That is 0000-07FF in hex.

Keep on address decoding: Look at how A13:A15 connect to the chip select pins (CS) of the 68B50 UART (serial communications interface). These three CS pins will activate the UART for address that looks like "101X XXXX" which are hex addresses A000-BFFF. So writing in the range will output to the serial port and reading from that range will read the serial port.

So what does the 6502 Microprocessor see as its universe? When it reads or writes to an address, the address decoding will map the address to one of three chips: ROM, RAM, Serial UART. The 6502 also receives a clock signal from crystal oscillator X2 (optionally divided in half by flip flop U8). That is more or less it. So what if we replaced all of that with IO pins on a microcontroller? We wouldn't need ROM chips, RAM chips, Serial Com chips, or even the clock signal. The microcontroller (assuming we could find one with a lot of IO pins) would just fake all of that functionality for the 6502. Basically we could trick the 6502 into running as though it were connected to everything it needed while we stimulated and observed it like Rene Descartes' Evil Genius. A brain in a vat so to speak. Why do my eyes hurt? You've never used them before.

Step 6: Z80 Microprocessor

The Z80 (Wikipedia) is an 8-bit microprocessor introduced by Zilog as the startup company's first product in July 1976. With the revenue from the Z80, the company built its own chip factories and grew to over a thousand employees over the following two years.

The Zilog Z80 is a software-compatible extension and enhancement of the Intel 8080 and, like it, was mainly aimed at embedded systems. Although used in that role, the Z80 also became one of the most widely used CPUs in desktop computers and home computers from the 1970s to the mid-1980s. It was also common in military applications, musical equipment such as synthesizers (like the Roland Jupiter-8), and coin operated arcade games of the late 1970s and early 1980s including Pac-Man.

Early Z80s were manufactured by Synertek and Mostek, before Zilog had its own manufacturing factory ready, in late 1976. These companies were chosen because they could do the ion implantation needed to create the depletion-mode MOSFETs that the Z80 design used as load transistors in order to cope with a single 5 Volt power supply.

Faggin designed the instruction set to be binary compatible with the Intel 8080 so that most 8080 code, notably the CP/M operating system and Intel's PL/M compiler for 8080 (as well as its generated code), would run unmodified on the new Z80 CPU. Masatoshi Shima designed most of the microarchitecture as well as the gate and transistor levels of the Z80 CPU, assisted by a small number of engineers and layout people. CEO Federico Faggin was actually heavily involved in the chip layout work, together with two dedicated layout people. According to Faggin, he worked 80 hours a week in order to meet the tight schedule given by the financial investors.

Impressive list of Z80-based computers.

Step 7: Z80 RetroShield Kit

The Z80 RetroShield hardware is part of 8BitForce's RetroShield project. The project Wiki has a lot of great information that will be helpful.

RetroShield boards let you execute code on real 8bit microprocessors with the help of an Arduino MEGA which simulates the rest of the hardware environment. 8BitForce refers to it as breadboarding hardware using software.

Assembly of the Z80 RetroShield

Start with the five SMT components. Mind the color codes on the SMD strips to be sure the correct components are placed in each position. Carefully open each SMD strip so that the components do not "fly away". Leave any components not used on the Z80 RetroShield safely inside its color coded strip. These will be needed later for the 6502 RetroShield Kit.

Solder the 40pin DIP socket next. Note the orientation notch on one end of the PCB silkscreen and also on one end of the socket. Be sure these align. Start by soldering two opposite corners only and verify the socket is fully seated by reflowing each corner quickly while gently pressing the socket into position. Once the depth is set, solder the remaining 38 pins.

For the 18x2 (J1) header, use the 2x40 pin male header (with the red plastic insulator). Snap off a section of 18x2 pins from the 2x40 header. Keep the rest of the header handy as it will be needed for the later for the 6502 RetroShield Kit. The J1 headers is mounted on the opposite side of the PCB from the DIP socket. The J1 header pins extend down from the bottom of the PCB.

The Red LED is inserted with its long lead (the anode) in the square pad. The square pad should be the one closest to the J1 header while the round pad (for the short LED lead) is closer to the DIP socket.

Finally the Z80 CPU can be inserted. Be very gentle making sure all 40 pins are aligned over a hole in the DIP socket before pressing the chip into its seated position. The pins are usually angled outwards from the chip package a tiny bit. It often helps to lay the chip sideways on a hard surface so that a tiny amount of force can be applied to the entire row of 20 pins at once very slightly pushing them in towards the centerline of the chip package. This can be done on both sides of the CPU until all 40 pins are straight enough to each align exactly over their holes in the socket.

You may wish to also review the assembly notes from the project wiki.

Connecting with the Arduino MEGA

Arduino MEGA code for use with the Z80 RetroShield can be found here.

Start with kz80_test.ino

Turn on serial debug output in the sketch by changing this #define from 0 to 1:

#define outputDEBUG 1

Set the 8-position DIP switch of the MEGA+WiFi board to [0011000X]

Without the Z80 RetroShield attached, upload the sketch.

Open the serial monitor and notice continuous access to address 0000 without results.

This should be expected since there is no CPU attached.

Remove power from the MEGA and attach the RetroShield. Note that the shield hangs off the end of the MEGA form the dual-for digital header. The RetroShield does not stack up on top of the MEGA like a typical Arduino shield. This leaves room for other shields to be used at the same time.

Reapply power and watch the Serial Monitor to see the Z80 doing its thing.

Z80 BASIC Interpreter

Next, try kz80_grantz80.ino which implements Microsoft Basic v4.7

Again, program the MEGA without the Z80 shield attached, remove the power, and reconnect the shield.

When you see "Memory top?" in the serial monitor, hit enter and wait a few seconds for the memory test.

Now you can enter and run BASIC programs, such as:

10 PRINT "HELLO WORLD"

RUN

Also, try typing in the sine wave demo that comes up on the screen when BASIC starts. That is some powerful "graphics" rendering performance. The 1970s called and they want their ASCII art back.

LINKS

A third party library designed to work with the Z80 RetroShield

Detailed video of Arduino MEGA controlling a Z80 - great theory of operation

Other Z80 homebrew projects

Step 8: 6502 Microcontroller

The MOS Technology 6502 (Wikipedia) is an 8-bit microprocessor that was designed by a small team led by Chuck Peddle for MOS Technology. The design team had formerly worked at Motorola on the Motorola 6800 project; the 6502 is essentially a simplified, less expensive and faster version of that design.

When it was introduced in 1975, the 6502 was the least expensive microprocessor on the market by a considerable margin. It initially sold for less than one-sixth the cost of competing designs from larger companies, such as the 6800 or Intel 8080. Its introduction caused rapid decreases in pricing across the entire processor market.

Along with the Zilog Z80, the 6502 sparked a series of projects that resulted in the home computer revolution of the early 1980s. Popular video game consoles and computers, such as the Atari 2600, Atari 8-bit family, Apple II, Nintendo Entertainment System, Commodore 64, Atari Lynx, BBC Micro and others, use the 6502 or variations of the basic design.

Soon after the 6502's introduction, MOS Technology was purchased outright by Commodore International, who continued to sell the microprocessor and licenses to other manufacturers. In the early days of the 6502, it was second-sourced by Rockwell and Synertek, and later licensed to other companies. In its CMOS form (the 65C02, which was developed by the Western Design Center), the 6502 family continues to be widely used in embedded systems, with estimated production volumes in the hundreds of millions.

Step 9: 6502 RetroShield Kit

The 6502 RetroShield connects a 6502 to an Arduino MEGA. The Arduino will drive the clock signal into the 6502. During each clock cycle, the Arduino will use the control signals (R/W) and the address bus from the 6502 to determine either what data it will need to drive onto the 6502 data bus or what data it must capture from the 6502 data bus. Review the 6502 RetroShield theory of operation for more details.

Assembly of the 6502 RetroShield

The 6502 RetroShield Kit will work with both 6502 chips and W65C02 chips. Figure out which one you have. A 6502 probably has the MOS logo, while a W65C02 will have a WDC logo (or possibly a UMC logo).

The 6502 RetroShield Kit is assembled in the same fashion as the Z80 RetroShield Kit with two important differences:

R2 and R4 are shorted (or not) according to which CPU you noted above.

The LED is oriented the other way around. The long pin still goes in the square hole, but please notice that the square hole is now closest to the DIP socket.

Connecting with the Arduino MEGA

Arduino MEGA code for the 6502 RetroShield can be found here.

Code is first loaded onto the MEGA just as with the Z80 version - without the shield attached.

The k65c02_apple1.ino sketch is a fun place to start. It includes the BIOS and BASIC ROM for the original Apple I Computer (no cassette tape required). Follow the README notes carefully especially the points about "Carriage Return" in the Serial Monitor and entering E000R to launch the BASIC interpreter.

LINKS

A very cool video on how the Apple 1 computer works from the 8-Bit Guy

Build a 6502 computer with Ben Eater - including ten excellent videos

Step 10: Cable Management

We could probably all use a little more cable management, right?

Step 11: Hack the Planet

We hope you are enjoying this month's HackerBox adventure into electronics, computer technology, and hacker culture. Reach out and share your success in the comments below or other social media. Also, remember that you can email support@hackerboxes.com anytime if you have a question or need some help.

What's Next? Join the revolution. Live the HackLife. Get a cool box of hackable gear delivered right to your mailbox each month. Surf over to HackerBoxes.com and sign up for your monthly HackerBox subscription.