8051

8051

Tags
Location
Published
Published June 21, 2022
Author
Apoorva Singh

Introduction

The Intel MCS-51 which is more commonly termed as 8051 was a microcontroller developed by Intel in the 1980s. Even after 40 years of its launch there are binary compatible derivatives of 8051 that are in production till this day.
Some of the basic features of this family of microcontrollers are:
  1. 8-bit microcontroller
  1. Four switchable register bank
  1. Hardware Boolean Processing
  1. 128 or 256 bytes of Internal RAM
  1. 8-bit ALU
  1. 8-bit registers
  1. 8-bit data bus and two 16-bit address bus
 
The silicon chip is only half of the story. You need compilers that can generate machine code for your microcontroller and do so efficiently. Here are the list of some embedded compilers for 8051 architecture:
  1. IAR Systems
  1. Keil C51
  1. Altium Tasking
  1. SDCC
The thing with these compilers is that all hardware specific things like different memory regions, interrupts and everything else is handled differently by each of the compiler vendor. So you can’t really port your code from one compiler to the other. And most of the compiler vendors charge you a fortune for the compiler that they give you, with an exception being SDCC which is open source.
 
If you were to look at new 8051 chips in the market, you would find 1T, 6T and 12T microcontrollers. What do they mean?
notion image
 
Original 8051 core ran at 12 clock cycles per machine cycle, with most instructions taking 1 to 2 machine cycles. With a 12 MHz clock frequency, the core could execute 1 million one-cycle instruction per second and 500,000 two-cycle instructions per second.
With the advancement of technology modern day 8051 cores can run six, four, two or even one clock per machine cycle which is denoted by 6T, 4T, 2T and 1T respectively.
 
A lot more information about the 8051 architecture can be found on the Wikipedia page.
 

Compilers

I am currently using Keil C51 compiler to run everything on my 8051 chip, which most of the time is Megawin’s MG82F6D17 chip.
 
It is cheap, feature packed microcontroller available at dirt cheap price. The only downside is lack of good documentation for the code examples. Along with very little support. And their code examples simply assume that you are using Keil C51 for developing your application.
 
It’s a 1T microcontroller with a total of 1 KB SRAM and 16KB Flash memory with In Application Programming capability, DMA engine, Timers, Programmable Counter Arrays, 12-bit ADC, Two UART, SPI, I2C, CRC engine, On Chip Debugging and Brown out detectors.
 
Peculiarities of this architecture

Parameter Passing

Normally in most of the microcontrollers the parameters to the function are passed via stack, the variables are pushed onto the stack and when the function returns, the value is popped. But due to the extremely small size of the internal RAM (128 bytes) this approach becomes really problematic.
So the Keil C51 compiler handles parameter passing in two ways:
  1. Register Passing - Passes upto 3 parameters via CPU registers
  1. Fixed Memory Address - Identify fixed memory location that is not in use by the program and write the parameters there and then call the function.
You can find an example here.
 

Overlay Directive

Since the Keil C51 compiler does not pass function arguments via stack. It needs to make sure efficient use of memory is performed by analysing the call graph of your program during link time. When it does that it analyses which function calls are mutually exclusive to one another and reuses the memory space for passing parameters to the other function.
This obviously brings a set of problems with it. If for some reason the linker is unable to create a correct call graph for your program you may end up with overlapping memory assignments which can cause your program to crash during execution.
This is also the case when using function pointers. Special care has to be taken to use them while compiling you program with Keil C51 compiler. Read more about it here.

Byte Ordering

Most data items are too long to be stored in 8 bits of memory. When storing multiple bytes of information, byte ordering becomes an issue. There are multiple ways of storing multi byte information. Two most common methods are:
  1. Big Endian (Motorola Order)
  1. Little Endian (Intel Order)
 
Let’s say we have a unsigned 32 bit integer 0x578812AB.
 
When following little endian method, the memory would look something like this:
Address
+1
+2
+3
+4
Contents
0xAB
0x12
0x88
0x57
And when following big endian method:
Address
+1
+2
+3
+4
Contents
0x57
0x88
0x12
0xAB
The 8051 is an 8-bit machine and has no instruction for directly manipulating data objects larger than 8 bits. Multibyte data is stored as per the following rules:
  1. LCALL instruction stores the address of the next instruction on the stack which is stored in little-endian format
  1. All other 16-bit and 32-bit numbers are stored in big-endian format
  1. Floating point numbers are stored according to the IEEE-754 format and are stored in big-endian format
 
All of this is also important if you are communicating with other machine over a network with raw binary data.
 
More information about this can be found in Keil C51 User Guide.
 

Reentrant Functions

TBD