*********************gSim51 hands-on tutorial***********************


This tutorial is intended for those who have never had any experience with text based debugging tools. Those who have had experience of using DOS debug, can start exploring the simulator and by reading the online documentation and with a little trial and error can become adept at using gSim51 within a relatively short time. A basic understanding and familiarity of the 8051 family of microcontrolers is implicitly assumed in the tutorial.

First, let us start by running the program. Get past the splash screen by pressing any key.
Now you can notice a prompt like this:
						   Enter the mode[C(onsole)/F(ile)]:
Here type 'C' or 'c' for Console mode (We will explore mode later in this tutorial).

The console mode presents you with the '-' prompt. DOS debug users will notice this similarity. Typing any other key invalidates the response and you are taken back to the mode prompt after another keystroke. This command prompt behaves exactly like the DOS prompt. Pressing the enter key will produce another prompt.

Ok, now we are ready to try out some commands. Type 'r' and then 'enter' at the command prompt. You will notice that all the values of the 8051 registers are displayed on the screen, which now seem to contain zeroes, except of course the SP which is initialized with 7h, just like in the actual 8051.

Now we would begin by entering some code into the simulator. Since the simulator is in console mode, we would have to directly punch in the hexadecimal code into the code area of the microcontroller. Type 'i' for hex input at the prompt (Note that from now onwards I will not mention the 'enter' keystroke after every command). You will be presented with a ':' prompt now. At this prompt, type the starting address for your code, for which type '0' (zero) and press enter.
Now, you will be presented with the address of the code area to punch in your hex code. Type     4 'enter'
5 'enter'
12 'enter'
75 'enter'
23 'enter'
35 'enter'
Here, 4 stands for 'INC A' , 5 12 stand for 'INC 12' while '75 23 35' stands for 'MOV 23,#35'. You have to enter everything byte by byte in this simulator, even though it may be a data byte or an opcode. You cannot enter the last instruction as 75 23 35 'enter'. Now press 'enter' once more to be back at the command prompt. At this stage u have your program loaded into gSim51. Let us unassemble the program now. Unassembling is done by the 'u' command, optionally followed by the starting address of the unsassembled code. In case you do not give any starting address, unassembling will be done from the current position of the unassembling counter, which is equal to zero right now. So type 'u' at the prompt now. You will get 16 unassembled instructions on the screen, something like this,
0001: 05 12     INC 12
0003: 75 23 35  MOV 23,#35
0006: 00        NOP
0007: 00        NOP
0008: 00        NOP
0009: 00        NOP
000A: 00        NOP
...
				
Note that all numbers in gSim51 are in Hexadecimal. So, the 'h' is always implied in all numbers used by gSim51. Debug users note that that range operator 'l' is not implemented in gSim51. that is to say, gSim51 does not understand commands like 'u20 l5'.
Now we are ready to execute our code. To trace one instruction at a time, simply use the 't' command. The 't' command shows the just executed instruction, all the 8051 registers, the corresponding clock cycle used and also the next instruction. You will see that the just executed instruction is 'INC A'. See the value of 'A' register and you will find that it has changed to a '1' now. This can be also verified by the 'r' command. Like other commands, 't' can be also used with a parameter that specifies the start address of the execution. Note that however, this command changes the Program Counter to the parameter passed with the 't' command. Also, parameters may be separated from their commands by any number of spaces. Thus, 't 30', 't30' and '   t    30  ' are all equivalent.

You can also use the 'g' command to execute more than one instruction at a time. The 'g' command must be followed by a parameter otherwise the user is interactively asked to enter its value. After the 'g' command, the screen scrolls several times depending on your parameter size and at the end you are presented with the final state of the registers and the next instruction.
The commands 't' and 'g1' are equivalent to each other, since they each execute one instruction starting from the current value of PC. (Program Counter)

You can also examine the entire data area by typing 'dd' which stands for data dump. Similarly, the code area can be examined by typing 'cd' at the prompt. Unlike dd, cd takes a parameter to control the starting address for the dumping process. Now type 'em' at the prompt. You will be asked to enter a memory address for editing. With this you can view the present value at this address and also edit that data within the 256 bytes of RAM area. To leave this address unchanged, you shall have to type the same value that was contained earlier in this address, the value which has been displayed on the previous line by the 'em' command. By the way, if at any stage of your simulation, you feel that your screen has become cluttered and 'dirty', you can clear your whole screen by the 'cls' command. You can also access the online command reference by simply typing '?' at the prompt. Again, this is a similar feature for 'Debug' users.

The remaining commands of the simulator will be discussed by loading a preassembled hex file called test1.obj, available with this distribution of gSim51.
 Note that once in the console mode you must quit in order to run the simulator in file mode. You can quit the program by simply typing 'q' at the command prompt.

Now restart your program and at the command prompt, type 'f' or 'F' for file mode. Next, you will be asked for the file format like this:
			
					Enter file format[(R)aw/(H)ex]:

Type 'h' at this prompt to go to the hex file mode. The raw format is just what you would type in the console mode, except that all the hex codes will now be contained in a file where all bytes must be separated by a white space character. When a program is loaded in raw form, it is always loaded starting from 0th address of ROM. Hence the raw format is not suitable for loading discontinuos programs. Due to its similarity with the console mode, we will not discuss the console mode but rather continue with our discussion about the hex file mode.

So now you are at a prompt where gSim51 is asking for the hexfile name. Type 'test1.obj' and press enter after that. In case, the file does not exist, you will be shown a error message and taken back to the 
         'Enter file format[(R)aw/(H)ex]: ' prompt, from where you can also go to load a raw file.
So now you have got loaded test1.obj loaded into gSim51, which is a hex file. You can now try out all the previous commands with gSim51 learned earlier. You will find at this stage all commands run identically whether you were in console mode or in file mode.	The same goes for raw file mode too. Try out unassembling and viewing the registers for a start. Now type 't' to execute the 'MOV R0,#05' instruction. You can examine the contents of R0 by the dd command to find that address 0 in the ROM now contains 5 now.
Try the 'g2' command now and see how the screen now contains the register values of two successive executions. Type 't' once again so that one more instruction is executed and the next instruction is 'DJNZ R0,FC' now, visible at the bottom most line before the command prompt.

This is a loop to be executed next. Suppose that loop had 1000 iterations and you wish to get at the instruction that lies beyond the loop. Of course, you can try typing t more than 1000 times, but that would not be a nice idea. You could also try something like 'g1000', but with that the final point before control is handed back to you would depend on the contents of the loop and depending on that the actual paramateter for 'g' would be somewhat larger than 1000. Also, Suppose you want to get just at the next instruction after the loop. Then what? Calculate the exact number for the 'g' command? No, absolutely not! As long as there is the 'p' or 'proceed' command to the rescue. This 'p' command is similar to the one present in DOS 'Debug'. First note the value of the R0 register using the dd command once more before typing 'p' at the prompt. You will find that the loop executed 5 times and the control is passed back to you just after the loop at the 'NOP' instruction. See now the value in R0 register by the dd command. You will find that R0 has been reduced to 0 (zero) now. This was the loop counter and this shows that our 'p' command worked as expected and executed the loop the specified number of times. This is the convenience of the 'p' command. You can also use 'p' with procedure calls to step over them. Using 't' command instead will step into the called procedure rather than stepping over it.

gSim51 also provides you with the facility to change any register of 8051 on the fly during simulation. For editing any register use the r[reg] command. When the 'r' command is suffixed with any register name, like 'a' or 'pc', you are given a prompt to change the register values after showing you the current value contained in that register. Suffix names are same as register names except that of a few registers that are abbreviated as below:

tc   -  TCON
				tm   -  TMOD
				sb   -  SBUF
				pcn  -  PCON
				dp   -  DPTR			

For example, to change the accumulator we would type 'ra' and to change the DATA POINTER register we would type 'rdp' rather than 'rdptr'. This feature is to make it easier to type the commands in the simulator. Another similarity with the DOS 'Debug' program.

gSim51 also supports common debugging facilities during simulation. To see this,
load the test2.obj hex file similarly just like you loaded test1.obj in the beginning. Now type 'b2' to set a breakpoint at the ROM address 2. using 'b' without any arguments would set the breakpoint at the current value of the PC register. Similarly type 'b4' and 'b6'. Note that you will be given a one-line summary saying that
'Breakpoint set at PC=so-and-so'
 Now, there are 3 breakpoints. In order to list the breakpoints, you should give the 'lb' command. This command shows all the current breakpoints, their associated ids and of course, their corresponding PC values. Breakpoint ids will be later useful in removing them. If you now unassemble your code now, you will find a small '' beside the instruction where a breakpoint has been set. Now before proceeding, specify another breakpoint at ROM location 8 by the 'b8' command.

Now you will remove an unwanted breakpoint from location number 6. For this, issue the 'rb' command. At this, you will be presented a prompt like this:

						Remove Breakpoint no. [0-3]?:
Here you will have to specify the breakpoint id of the breakpoint, which you want to remove. Our choice, in this case would be '2', because this is the id of the breakpoint at location 6 of the code ROM. Now if you give the 'lb' command you will find that breakpoint 2 has a 'removed' status near it, which means that this breakpoint is no longer active. You can also note the difference in the 'u0' command. Here, the breakpoint sign '' no longer appears beside the instruction at location number 6 in the code area.

Execute the code now with the 'gn' command, where 'n' is a sufficiently large value that would have carried the execution past the breakpoints had they not been set. You will find the following on the screen now:

						Stopped execution at break point PC=2

Note that execution stops at the instruction just before the breakpoint. Now you can get past the breakpoint by the 't' command as usual, but not with 'g1' command. Also notice now the '' symbol appears beside the next instruction field after each 'r' or 't' command.

gSim51 also supports interrupts. Interrupts can be generated at any point during execution of code in the simulator. To generate interrupts, type the 'v' command at the command prompt. The 'v' stands for 'Vector interrupt'. At this command, you will be presented with a list of all available 8051 interrupts along with their identifying codes. A prompt appears asking you the interrupt you wish to generate.
				Enter the Interrupt Vector no.[0-5]:

Here enter the corresponding number of the interrupt you wish to generate. For example, to generate the timer0 interrupt simply type '3' at the prompt. Immediately, the following line will appear on the screen:

				Transferring control to Timer0 TF0 Vector...

Below, you can also see the current status after control has been transferred to the corresponding interrupt service routines at predefined addresses. See how the program counter has changed to reflect this change. You can also examine the stack starting from 7th location of ROM by default to see the return address as well as the stack pointer register before and after the ISR has finished execution. Execution will now commence from the predefined address until a 'RETI' instruction is encountered inside the ISR. Otherwise execution shall never return to the point where the interrupt was generated. You may also note that how upon returning from the ISR the stack pointer has changed.

That was all in this tutorial where you learned about the various commands of gSim51 and I hope that I have done my best in making you understand all the features of gSim51.

Once again, I would like to thank Dr. A.K Gogoi (HOD of ECE Dept.,IITG) for his guidance in building this simulator. I am greatly indebted to him for all his help.

You are free to send in any suggestions, complaints and criticisms to me at my email id
			seemanta_18@yahoo.com

Your feedback will be much appreciated in this regard. 
HAPPY SIMULATION!!

Seemanta Dutta
Final Year B.E.
Electronics and Communication Engineering
Hostel 5, Room No. 101
MNIT Jaipur, Rajasthan
INDIA	
