Using _protected regions to recoup unusable ram

Coding and general discussion relating to the compiler

Moderators: David Barker, Jerry Messina

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

Using _protected regions to recoup unusable ram

Post by Jerry Messina » Mon Feb 02, 2015 6:33 pm

SF has a _protected memory feature that lets you reserve blocks of ram, which keeps the compiler from using the specified ram region. It's pretty simple to use... you just define the beginning and ending addresses like so:

Code: Select all

#define _protected_a_start = $100       // reserve $100-$1FF
#define _protected_a_end   = $1FF
Here's a little trick for the 18F13K50 that uses that feature to recoup use of 256 bytes of ram that's normally unavailable to SF

Code: Select all

program alloc_13K50

device = 18F13K50

// the 18F13K50 has 512 bytes of total ram, but it's split into two banks with
// an unused block of data memory in between them...
//      000-0FF  256 bytes
//      100-1FF  <unused>
//      200-2FF  256 bytes (gen purpose/usb ram)
//
// by default, the SF device file has '_maxram' set for 256 bytes
//   _maxram = $100
// and the bank of ram from $200-$2FF is unusable by SF
//
// here we're going to tell SF that the part has 768 bytes of ram (000-$2FF),
// but then tell it to reserve the block from $100-$1FF so as not to use it
//
#variable _maxram = $300                // 768 bytes of ram
#define _protected_a_start = $100       // reserve $100-$1FF
#define _protected_a_end   = $1FF


// the SF system variables take up the first 25 bytes of bank0, so allocate
// the remaining part of the bank. normally, this would be the limit...
// 256-25 = 231 bytes of user ram
const SYSVARS = 25
dim bank0(256-SYSVARS) as byte

// now, we can allocate an additional 256 bytes of ram, and this will
// come from the $200-$2FF bank, skipping the unused area
dim bank2(256) as byte

// demonstrate that the memory allocations work
clear(bank0)
clear(bank2)

bank0(0) = $01
bank0(bound(bank0)) = $11

bank2(0) = $02
bank2(bound(bank2)) = $22

end program

bitfogav
Registered User
Registered User
Posts: 169
Joined: Sat Oct 09, 2010 1:39 pm
Location: United Kingdom

Re: Using _protected regions to recoup unusable ram

Post by bitfogav » Mon Feb 02, 2015 7:13 pm

Nice, thank you for sharing this Jerry..

User avatar
RangerBob
Posts: 152
Joined: Thu May 31, 2007 8:52 am
Location: Beds, UK

Re: Using _protected regions to recoup unusable ram

Post by RangerBob » Tue Feb 03, 2015 11:51 am

Good tip.

// the 18F13K50 has 512 bytes of total ram, but it's split into two banks with
// an unused block of data memory in between them...
// 000-0FF 256 bytes
// 100-1FF <unused>
// 200-2FF 256 bytes (gen purpose/usb ram)

WTF Microchip!?! What on earth are they thinking?

Post Reply