Reputation: 397
I want to know how to a program is working with a hardware device. I want to know basic knowledge not in deep.
All programming codes have the following things.
I want to when we program download to the hardware that data go to Programming memory (ROM). When we run the program all above mention thing go to RAM or is it go specific area like function data only?
When we consider 8bit bus how to more than 8bit data like 10bit char because the bus is small?
What is Stack and how to program work with it?
Why void main
funtion is important? how to hardware identify it?
Please give me basic idea of how to work program with hardware.
Upvotes: 0
Views: 841
Reputation: 111
You've got a lot of questions there. To start with, lets assume a simple situation, like the Arduino, which is a combination of software and hardware. The software part starts with you writing some simple C++ code, such as:
byte ledPin = 13;
void setup(){
pinMode (ledPin, OUTPUT);
}
void loop(){
digitalWrite (ledPin, HIGH);
digitalWrite (ledPin, LOW);
}
Next, you would Compile the code. The Arduino IDE (Integrated Development Environment) does some stuff (in the background, like adding a main() that would call the the setup() function once, and the loop() function repeatedly) to turn that C++ into Assembly code, which might look like this (not the actual output from the above):
; Define pull-ups and set outputs high
; Define directions for port pins
ldi r16,(1<<PB7)|(1<<PB6)|(1<<PB1)|(1<<PB0)
ldi r17,(1<<DDB3)|(1<<DDB2)|(1<<DDB1)|(1<<DDB0)
out PORTB,r16
out DDRB,r17
; Insert nop for synchronization
nop
; Read port pins
in r16,PINB
An Assembler than takes that code and turns it into hexadecimal format information that a Programmer can put into ROM on the Atmega328P microcontroller. Basically, lines of data in a file, with each line containing the start address of a line, perhaps 16 bytes of data, and a checksum to ensure the data did not corrupted while it was being handled:
000100005673495a6b4f5e205673495a6b4f5e24465
This is not real data. 0001000 might be the starting address in ROM, the next 32 characters are the 16 bytes of data to be programmed, and the last 4 could be the checksum that is created by manipulating the data somehow. The Programmer receives the data, performs the same manipulation, and if the checksum is correct it burns it into memory.
The hex code can be put into ROM a couple of ways - a Programmer could take control of the chip and put the code into memory directly, and after a reset the chip would just run the code;, or a Bootloader could be run from an area of ROM on the chip after a reset, and it would talk to a PC over the Serial lines (Rx, Tx) to receive the data and then write it into a different area of ROM. If the bootload code did not detect the PC trying to talk to it, it would jump to the ROM address where the code started and run from there.
8 bit microcontrollers might have some 16 bit registers it can use for things like capturing the results of an ADC conversion, or it might store the results as two 8-bit bytes, with high & low data.
The Stack might be dedicated hardware registers, or it might be an area of SRAM that is used to hold things like the results of math operations. The code takes care of putting things on the stack and reading it back, you wouldn't normally deal with programming it. With a '328P, there is 2048 bytes of SRAM, so you would only need to make sure you didn't have too many variables declared in code (like byte ledPin = 13;) that used it all up and didn't leave room for the code. Often, in 328Ps for example, this is caused by trying access an array outside of it's limits, so one either gets back nonsense results, or it crashes the program when writing past the end of the array overwrites something else. The flexibility of C++ is great, but it will also let you get into trouble if a little care is not taken.
Upvotes: 1
Reputation: 553
As @nos says, it takes large books to explain it.
The thing you miss between the code and the hardware is the compiler. Its role is to translate your code (C, C++ or whatever language it is) to assembly. Assembly is roughly a set of instructions at very low level that the CPU or microcontroller understands.
When the program is translated to assembly, it is uploaded to the memory. Depending on the architecture, it goes in general memory (Von Neumann) or program memory (opposed to data memory with Harvard architecture).
When talking about stack, you also have pointers. The pointer points at (yeah, a pointer that points, awsome huh) a "level" of the stack. For example, you have the "current" pointer that points at the current instruction. Once the current instruction is finished, the pointer is incremented and therefore points at the next level. Another pointer is used when you call a subfunction and points at the level where you stopped the parent function. One the subfunction is done, you go back at where you stopped.
Those pointers are stored in registers, which are little (and fast) memories in the chip.
Coming to the main function, the hardware does not litteraly identify it as it is compiled. For the hardware, there is just a program to execute (ie the main) with calls to subfunctions (ie the functions used in main).
So.. when you program some code, let's say in C:
int main ()
{
int a = 0;
printf("a = %d", a);
return 0;
}
The content of main is translated into assembly and is your "normal" program. First, it puts 0 in a cell of a register. Then it puts it on hold to execute printf (which uses the values in the cell where we saved the 0) and saves where it stopped in a pointer. Once it is finished, the current pointer takes the value of our saved pointer back and continues.
About what goes in the ram: you have different levels of memory. Disk, hard or solid state, slow but big and cheap. Ram, faster but more exepensive and smaller. Cache, built in the CPU, very fast, smaller and more expensive. Registers, also built in, even faster but smaller. When the CPU uses a variable, it goes in the registers. But modern CPUs "guess" (aka prefetch) what variable could be needed in a near future (e.g. you read the two first cells of a table, it will probably load the third one before you ask), and put it in the cache. Otherwise, it is in ram.
Hope that helps, there might be some shortcuts or inaccuracies, but that's roughly how it works. Again, it's hard to summarize hundred of pages in few lines.
Upvotes: 0