How much RAM? Or where does RAM end?

Coding and general discussion relating to the compiler

Moderators: David Barker, Jerry Messina

Post Reply
SHughes_Fusion
Posts: 219
Joined: Wed Sep 11, 2013 1:27 pm
Location: Chesterfield

How much RAM? Or where does RAM end?

Post by SHughes_Fusion » Wed Mar 08, 2017 4:18 pm

I've spent the day trying to find a bug which it turns out is down to a ClearRAM routine I wrote a while back incorrectly clearing some SFRs.

The routine is below, but I use the _maxram value from the device header. In most cases this would be OK, but on some devices you don't actually seem to have the amount of RAM this (and the datasheet) suggests. I'm using the 46K22 which has 3936 bytes, but the top 40 of these bytes are actually SFRs.

So, my main question, how do I find the 'top' RAM address reliably?

A secondary question this has highlighted, where I to write a program which uses all available RAM, does Swordfish know not to use these top 40 bytes, and if so, how? (Or have I misunderstood something here?)

Code: Select all

{
****************************************************************************
* Name    : ClearRAM                                                       *
* Purpose : Clears all processor RAM                                       *
****************************************************************************
}
Public Sub ClearRAM()
  Dim 
   RAMAddress As FSR0,              // address of RAM location to be cleared
   RAMClear As POSTDEC0             // post decrementing pointer to RAM
           
  Const TOPRAM = _maxram            // Get the address of the top of memory
  
  RAMAddress = TOPRAM               // Set FSR to point to top of memory
  Asm-
    loop1
      clrf    postdec0              // Clear RAM location
      tstfsz  fsr0h                 // FSR0H 0?
       bra    loop1                 // No, carry on clearing

    loop2                           // Yes, means we only have $FF to $00 to clear
      clrf    postdec0              // Clear RAM location
      tstfsz  fsr0l                 // FSR0L 0?
       bra    loop2                 // No, carry on clearing

      clrf    postdec0              // Yes, clear last byte then we are done
  End Asm
End Sub  

Jerry Messina
Swordfish Developer
Posts: 1469
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: How much RAM? Or where does RAM end?

Post by Jerry Messina » Wed Mar 08, 2017 7:12 pm

As you found out, the device files don't always tell the truth.

If you use the latest version of the system convert utility (v1.40) you can generate a new set of files with slightly better results,
but the _maxram value is a tricky one and the utility doesn't always get it right. I remember going through and having it check to make sure it stops _maxram before any SFR register addresses.

I just checked and for the 46K22 it sets _maxram to $0F38 (3896 bytes), so at least it skips the SFR registers now.
What it doesn't see is that the ram actually ends at $EFF, so it should really be 3840 (edit: disregard this)

I recall going through all sorts of gyrations trying to get this right but not all the data in the mpasm .info file
database it uses is correct/tells the whole story. I'll try and remember to take another look at it, but a quick glance
seems to show it saying there's 16 banks available when it should only be 15.
(edit: and this too)
where I to write a program which uses all available RAM, does Swordfish know not to use these top 40 bytes
It uses whatever the _maxram setting is, so if it's told it has 3936 bytes it will use them (and in this case stomp over the SFR's). As far as I recall it doesn't check for any other conflicts.

Sorry it caused you grief. There's just too many devices/datasheets to go through each time they release a new database so there's bound to be some issues.
Last edited by Jerry Messina on Wed Mar 08, 2017 7:34 pm, edited 1 time in total.
Reason: _maxram end values ok...used wrong memory map

Jerry Messina
Swordfish Developer
Posts: 1469
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: How much RAM? Or where does RAM end?

Post by Jerry Messina » Wed Mar 08, 2017 7:29 pm

Ooops, I bunged up that last post. I was looking at the wrong memory map page in the datasheet!

SystemConvert v1.40 DOES get it right for the 2x/4xK22 devices.

There's a link at the bottom of the wiki page with some updated files already converted. See http://www.sfcompiler.co.uk/wiki/wiki-u ... _files.zip

SHughes_Fusion
Posts: 219
Joined: Wed Sep 11, 2013 1:27 pm
Location: Chesterfield

Re: How much RAM? Or where does RAM end?

Post by SHughes_Fusion » Thu Mar 09, 2017 8:36 am

Thanks, Jerry, that is very helpful.

Must admit I'm slightly confused - I'm sure I've read of this device having 3936 bytes of RAM but the datasheet now shows 3896 correctly. I wonder if MChip got it wrong in the first place...

User avatar
octal
Registered User
Registered User
Posts: 586
Joined: Thu Jan 11, 2007 12:49 pm
Location: Paris IDF
Contact:

Re: How much RAM? Or where does RAM end?

Post by octal » Thu Mar 09, 2017 9:12 pm

SHughes_Fusion wrote:
I'm sure I've read of this device having 3936 bytes of RAM but the datasheet now shows 3896 correctly.
Maybe you confused the K20 with K22. It's the PIC18F46K20 (twenty) that have 3936 RAM, not the K22.

Post Reply