Building Machines In Code

This entry is part 1 of 9 in the series Building Machines in Code

Post Stastics

  • This post has 1666 words.
  • Estimated read time is 7.93 minute(s).

A Series of Posts on Writing Digital Computer System Simulators and Emulators

In this new series, we will explore the creation of hardware system emulators/simulators. Before we do though, let’s first get a hand on what an emulator and simulator are.

What is an Emulator?

Often you hear the terms emulator and simulator used interchangeably. However, there is a distinct difference between the two. Both seek to mimic a real system in a virtual environment. But, the difference between emulation vs. simulation is quite big!

Simulators create an environment that mimics the behavior and configuration of a real device. They do not make any attempt to model every component and its inner workings. Emulators, however, painstakingly duplicate most active system components of the physical system. While the distinction may seem trivial, it is very important you understand the difference.

Emulator VS. Simulator

Emulation is the process of modeling the inner workings of the system while simulation models the observable behavior of a device for the intended purpose of the simulation. You can think of it this way: An Emulator is a much more accurate representation of the real device or system. While a simulator acts like the real device/system in most circumstances. My favorite example of the difference comes from a 2005 post from Peter Hans van den Muijzenberg on ed-thelen.org. Peter demonstrates the difference this way:

Regarding the difference between simulation and emulation:
Not limited to computers I use this distinction:
- A simulation mimics the outward appearance
- An emulation mimics the cause/process.

If you want to convince people that watching television gives you
stomach-aches, you can simulate this by holding your chest/abdomen and
moan. You can emulate it by eating a kilo of unripe apples.

Most often an emulator is a re-implementation of a target device, system, or platform. The emulator acts exactly as the real device would. A simulator is a partial implementation of the target device/platform, it does just enough for its own purpose.

It is also important to realize that both emulators and simulators are models of the target system. With an emulator you want the output to be exactly as though it was produced on the real system. With the simulator, you want only certain properties of the output to be as expected from the real system.

Emulation is used when you need the system to be a faithful reproduction in almost every aspect of the real system. Simulation is used when you need to model certain aspects of the real system for a specific purpose. For example, it would be impossible to emulate the Pacific Ocean as that would require modeling each rock, each life-form living in the ocean, and every other detail like wind, waves, people swimming, etc. We can however simulate the Pacific Ocean for the effects of current changes as this would only require us to model the pertinent parameters like temperature effects, coastline shapes, ocean depth, etc. With our simulation, we can predict what the results should be. Simulations are very helpful in science and engineering where some aspects of the problem would be untenable if we attempted to model them accurately. Emulation often is not required when you only need to answer a few questions regarding the system. Emulation is needed when you really need the physical system but don’t have one available. It may sound like I am running on about this. The terms are often misused by many. But the distinction is an important one.

What Can We Emulate?

Basically, any system of known finite complexity can be emulated. Realistically, many systems are too complex, requiring far too many components and subsystems to be emulated. When we come across such a system, it is then that simulation can be very helpful. With simulation we can ask ourselves what aspects of the system we are most interested in and model that subset of properties of the target system.

Typical devices and systems for emulation include:

  • Calculators
  • Computer System & Hardware
  • Arcade and Video game consoles
  • Embedded systems
  • Similar systems

Typical devices and systems for Simulation include:

  • Body of Water
  • Geological processes
  • Air and fluid Currents
  • The World (Flight Sims)
  • Weather
  • Space

In this corpus, we will only concern ourselves with writing simulators and emulators for digital hardware systems. In many of the projects presented, we will blur the lines of distinction having some portion(s) of the system emulated while other parts are simulated or ignored altogether. Our primary focus is on simulating/emulating digital computer processors and systems.

Why Emulate/Simulate a System?

Simulation is often required when a system is too costly to acquire or simply unavailable. For example, hardware designers often use tools to simulate new designs that would be too costly or complex to prototype. Using a simulation of the hardware allows the designer to try new designs without the high cost of fabricating a new chip for each iteration of the design process.

Simulators and emulators can also be used as tooling for software development. Apple’s Xcode Developer’s IDE includes simulators for iPhones, Apple Watch, and Apple TV devices. Google’s Android Studio for Android development includes an emulator for many types of Android devices. These simulators/emulators allow the application developer to create and test applications for these devices without the need to own each device.

Another popular use for emulators is Retro-Computing. Computer systems such as the Sinclair Zx81, Commodore C64, Tandy Color Computer, BBC Micro, and other early home computers are getting harder to find and can be expensive when they are located. Many, people enjoyed these systems in their youth and wish to return to them. Others, find these early computer systems easier to understand and seek them out to use as a tool to gain a good understanding of the basics of computer systems.

What tools are required?

To write an emulator or simulator you must have in-depth knowledge of the target system’s hardware and software. Good general knowledge of digital electronics, basic logic, and system architecture is very helpful.

Before you set off to write an emulator or simulator, you will need to research the system completely. For computer hardware emulation/simulation projects it is often helpful to have schematics of the system along with datasheets for the major components of the system. For some systems, you may also need documents regarding standard communications protocols and software.

The major steps needed to complete an emulation/simulation project are:

  • Research your target system and collect all required reference material
  • Select your development tool-chain (compiler, debugger, IDE, etc.)
  • Write some example code for implementing the CPU.
  • Write some example code for implementing the other major hardware device in the system.
  • Do an overall system design with the knowledge you gain from the example code.
  • Write your CPU emulator
  • Test and verify your CPU implementation
  • Write the code to emulate the other major system components
  • Test and verify the implementation of all major system components
  • It is often helpful to write a supporting tool-chain such as debuggers for single-stepping CPU execution, dumping the contents of CPU registers and memory to the host system’s screen, assembler, dis-assembler, etc.
  • If your system is a popular commercial system, try running other people’s software on your implementation.
  • Have fun!

Where can I get information?

Locating the information and documentation you need for a project can be difficult. Performing internet searches on your target system can help. For old computer systems such as the Vic 20 and Commodore 64, you can find lots of information on the web. Datasheets for some of the C-64 chips are getting harder to find however, http://www.archive.org (The Internet Archive) has many old books on many retro computers. Some of these books will contain reprints of datasheets or detailed information on the various components used in the system. Also, the Way Back Machine is a part of the internet archive and you can often search for information found on old websites that no longer exist. This will often bring up articles and documentation.

There are also many retro computing sites on the web as many of these systems still have cult followings for nostalgia, education, and entertainment purposes. Some systems like the C-64 have made a comeback and you can even find new software being created and sold for them.

What to do when your target system is lesser-known? Well, finding people who worked on the target system at one time, or who used the system can be very helpful. Often you can locate documentation in various computer, programming, and electronics forums. Posts requests for information on these forums and often someone will respond with first-hand knowledge of where you can locate the needed information. When all else fails, contacting the various computer museums may help you locate what you’re looking for.

Writing even a simple computer emulator is a difficult task. Expect to have difficulties along the way. When you have issues, take a step back and examine the issue. Read any error messages from your compiler or debugging output. You would be surprised how many people have the answer to their issue right in front of them but don’t read it! If the message is long, or not helpful, then you’ll need to dig deeper. Be patient and take breaks. You’ll eventually get there.

Conclusion

There are many other reasons you might wish to simulate a system. In later posts, we will develop several systems beginning with some very simple CPU simulators and working up to emulators for a retro-computer. But first, we need a basic understanding of what a digital computing system is and how it works. We’ll develop this knowledge by first looking at the “brains” of a computer, the Central Processing Unit or CPU. Then we will develop some simple CPU simulators with each building on previous knowledge while adding new functionality. Once we have a good handle on what a computer is and how it works we’ll begin developing a retro-computer emulator.

Series NavigationBuilding Machines in Code – Part 2 >>

Leave a Reply

Your email address will not be published. Required fields are marked *