2
\$\begingroup\$

I was encountering some quite strange differences in the behaviours of my PIC16F628a microcontroller compared to the MPLAP built-in simulator.

After a long search, it turned out to be different due to the free cells' default values.

In the simulator, when accessing a memory location that has not been initialized, the W register is loaded with 0x00, whereas in the actual device, some other value may be loaded (I don't remember if it was 0xFF or 0x7F).

Is there a way to either make all unused bytes 0x00 on the device or to change the default value of the simulator to the PIC's uninitialized default value?

(It might also be that my question shows that I didn't understand some fundamental principles about PICs... In this case I would be grateful if someone would teach me. ;-) )

\$\endgroup\$
6
  • \$\begingroup\$ If you are programming in C, that would be a C question. It has very clear definitions, when the memory is initialized, to which values and when it is not. \$\endgroup\$ – Eugene Sh. Sep 27 '18 at 15:27
  • \$\begingroup\$ Oh, sorry. I missed that one. I am programming in Assembler. \$\endgroup\$ – TomS Sep 27 '18 at 15:28
  • 5
    \$\begingroup\$ Even in this case, your program should not rely on default memory content. You should initialize it explicitly (unless it is some flash memory, which is known to have specific values when "erased".) \$\endgroup\$ – Eugene Sh. Sep 27 '18 at 15:30
  • \$\begingroup\$ I know that. I guess that I have some kind of "memory leak" in my program. But it is difficult to find if the program always runs fine in the simulator but does not on the actual device. Device does not allow debugging. So it would be great if I could fully simulate the device - including the empty memory locations. \$\endgroup\$ – TomS Sep 27 '18 at 15:32
  • 3
    \$\begingroup\$ You can always fill the simulator memory with nonzero values before running your code. For example, make sure every location contains 0x55 -- if you see this value appearing in any of your CPU registers during the simulation, that means that you forgot to initialize the corresponding memory. \$\endgroup\$ – Dave Tweed Sep 27 '18 at 16:25
4
\$\begingroup\$

My standard 16F PIC-family startup code specifically initializes all lower-bank data memory to 0x00. This is a hangover from when I was first learning to program PIC microcontrollers but I consider this to be good programming practice and the code stays in all of my projects.

The code required to do this zeroing of the data memory is different for each PIC variant. But it's a simple loop and occupies very little code space.

The newer 16F PICs have much more data memory than the legacy parts that I still work with. I zero-out only the first one or two lowest banks of data memory.

Add a comment if you would like me to post the code that I use for the various PICs that I use.

All that said: I consider it to be bad programming practice to make a decision based upon a reading a data location that has not been initialized to some specific value by your program. This is a common cause of programming errors.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ @Eugene: awesome mind reading! I was in the process of editing my answer to say exactly that! \$\endgroup\$ – Dwayne Reid Sep 27 '18 at 16:45
  • \$\begingroup\$ You are absolutely sure! But I'm not reading the uninitialized memory by intention. I guess there's a loop which reads too much memory in certain cases or it reads memory from the wrong bank or so... But your suggestion to initialize all the memory at the very beginning is probably the best solution here... \$\endgroup\$ – TomS Sep 27 '18 at 21:27
1
\$\begingroup\$

I suppose it may not be possible to consistently emulate uninitialized memory content in the simulator, since the said content may change from part to part, or even for the same part under different conditions like supply voltage or temperature.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.