[tech] Marg0t: a Computer who says "Asslema!"

A custom computer who says "Hello, World!" in Tunisian

[tech] Marg0t: a Computer who says "Asslema!"

Today we are going to talk about our first hardware research project, and I am very glad to introduce to the world Marg0t, a 6502 computer that says "Asslema!"*

A 6502 Computer for Fun and Profit

Marg0t is a hand-made computer built from scratch, and which does very basic operations, i.e. display a hard-coded text on a small screen.

The main purpose of the project is not to build a high performance machine, nor to provide a sophisticated approach to hardware and software programming.

The machine built and programmed serves the purpose of building the most abstract architecture of a computer, in order to understand the fundamentals in a slow but precise manner.

Knowledge Base

To sum up the knowledge base acquired, we will decompose the several sections of the project into hardware and software sections, before laying down the possible applications.

In general terms, building the computer covers a significant array of fields, from setting up the environment, to the choice of the right hardware, besides the programming and mathematical concepts that are covered.

Hardware Components

In the following section, we will cover the main components of the systems which form the core "organs" of the machines, if we wanted to use a medical analogy.

1. Central Processing Unit (CPU):

The central Processing Unit is the equivalent of the brains of the computer. As its name indicates, it is the unit that is tasked to interpret the different instructions, i.e. digital signals, and process them according to the programmer's code.

For Marg0t, the CPU is composed of two hardware components:

  1. The W65C02S 8–bit Microprocessor: The current version (from Western Design Center inc.) has a fully static core and officially runs at speeds up to 14 MHz when powered at 5 volts. The Wikipedia page covering the chip provides further information about the chip, its history and features.
  2. The W65C22 Versatile Interface Adapter (VIA): As its name indicates, it's a versatile chip, that works well when coupled with the 65C02 microprocessor. In our case, we used it for latching the data from the processor, to the appropriate I/O ports.

2. Read Only Memory (ROM):

The Read Only Memory, is the place where we can store our program, or instructions, so the processor can fetch and execute them.

For our project, we used the 28C256 256K Parallel EEPROM manufactured by Atmel.

EEPROM (also E2PROM) stands for electrically erasable programmable read-only memory, and is a type of non-volatile memory used in computers, integrated in micro-controllers for smart cards and remote keyless systems, and other electronic devices to store relatively small amounts of data, while allowing individual bytes to be erased and reprogrammed.

This specific chip was a lot of fun to play with, since it is the component that is actually programmable, i.e. where the program lives.

I actually had to purchase an additional pair of the chip, as its handling requires removing it, and re-placing it on the bread board, which can easily break the pins.

Optimization:

With the three previous chips, it was possible to display the text on the screen. However the code, written in Machine code and Assembly language, was far from being optimal in terms of performance, and speed of execution (i.e. number of clock cycles to display the message correctly).

For further optimization we needed to build a stack for the CPU, i.e. a form of memory that we can read from, and write-to. The microprocessor has a built-in stack pointer that can find the relevant address of a value, or in other words point to the location of the value in the stack, and use it (or reuse it).

This has the benefit of pushing "redundant" instructions onto the stack, and/or retrieve them if required. In this manner, the code becomes more efficient by using subroutine calls.

3. Random Access Memory (RAM):

As explained above, The Random Access Memory will constitute the stack (or memory), where we will be able to store parts of the code for optimal performance.

Marg0t uses a HM62256B Series chip, which is a CMOS static RAM manufactured by Hitachi, and where it is possible to store 256K bits (or 32K bytes) of data.

4. Time for a Diagram:

In the following schematics, you can see how these components are supposed to be connected with each other:

Ben Eeater's Schematics

4. Other components and setup:

Of course, there is a bunch of other components that were necessary to build the computer, and make it function properly. However I will not go through all of these elements in this post.

If we had to mention one single component, it would be the "logic gates", which are the fundamental component of any computer, transistor or chip. In my humble opinion, it is the essence of computing.

Personally, the most fun and educative part of the project was the debugging process, using a logic analyzer, as it was possible to diagnose "in-vivo" the machine, by single-stepping the clock.

For this project, I used 3 different machines:

  1. A Debian VM for EEPROM programming.
  2. A Raspberry Pi 4 for debugging, and Arduino programming.
  3. A Windows machine to follow the tutorials, and listen to music.

Software and Programming Languages

In order to make the computer effectively speak, and display messages, the project covered different software stacks, which are leveraged by different programming languages.

The diagram below gives a fair abstraction of the different levels at which any programming would operate, in order to interact with the hardware.

Thanks Stack Overflow!!
Thanks Stack Overflow!!

The diagram itself gives an abstraction of the C programming language, even though we did not use it yet in the project.

Machine code is binary (1's and 0's) code that can be executed directly by the CPU.

Assembly code is plain-text and (somewhat) human read-able source code that mostly has a direct 1:1 analog with machine instructions. This is accomplished using mnemonics for the actual instructions, registers, or other resources. Examples include JMP and MULT for the CPU's jump and multiplication instructions.

Unlike machine code, the CPU does not understand assembly code. You convert assembly code to machine with the use of an assembler or a compiler, though we usually think of compilers in association with high-level programming language that are abstracted further from the CPU instructions.

Building a complete program involves writing source code for the program in either assembly or a higher level language like C++.  

The source code is assembled (for assembly code) or compiled (for higher level languages) to object code, and individual modules are linked together to become the machine code for the final program.

Applications

This section covers the for-profit aspect of the "Project :: Marg0t".

We are still a research and development company, that is focused on financial applications, and state-of-the solutions in this space.

However, given the length of the blog post, I will lightly describe the applications, in which we can use the knowledge acquired.

Customization:

Custom systems are at the core of our value-chain.

"Project :: Marg0t" was built with the intention of being the most abstract form of these systems.

Our scope covers custom operating systems, custom hardware, and the hybrid Systems-on-Chips, which can be comprehended, adapted and developed for the needs of our customers.

Security:

We consider critical systems as the baseline for our products, and we understand the highly exigent requirements in this space.

Marg0t is an abstraction of the novel techniques that are possible to secure critical systems, i.e. trusted execution environments, hardware-assisted cryptography, and embedded system hardening.

Algorithmic trading:

This is my personal favorite application, since it is the space where we can mix and use techniques from the financial, engineering and mathematical realms.

By building Marg0t, I personally learned a lot about computers systems. However, I specifically acquired a more profound knowledge about time-sensitive systems, hardware timing requirements, and software design that relies on hardware to execute time-sensitive operations.  

FIN

At the moment of writing this entry, the "Project :: Marg0t" is still on-going, and I still have to wrap my head around the many topics covered.

I hope you enjoyed reading this blog post, as much as I enjoyed working on it.

Of course, here is a small video of the Marg0t in-action:

*"Asslema!" is a specifically Tunisian word that means "Hello", and in this context it is used as substitute to "Hello World!".

Appendix:

The 65C02 8-bit microprocessor, along with the other components that compose Marg0t, part of the retro programming culture.

The first version of the 65C02 chip, the 6502, was introduced in 1975, and knew an almost immediate success for its affordable price tag of $25, and competitive computation speed.

The 6502 almost single-handedly forced the price of processors to drop, helping launch the personal computer revolution. (Chip Hall of Fame - IEEE)

According to the same article, the chip, and its variants went on to become the main brains of seminal computers like the Apple II, the Commodore 64, and the BBC Micro, not to mention game systems like the Nintendo Entertainment System and the Atari 2600 (also known as the Atari VCS).

The 65C02 chip, still in-production, enjoys a large adoption among hobbyists, and is adopted in commercial embedded systems.