- int sharebankAddress = 0x7f;
- if (options.stack_loc != 0) {
- // permanent (?) workaround for pic16f84a-like devices with hardly
- // any memory:
- // 0x00-0x0B SFR
- // 0x0C-0x4F memory,
- // 0x50-0x7F unimplemented (reads as 0),
- // 0x80-0x8B SFRs (partly mapped to 0x0?)
- // 0x8c-0xCF mapped to 0x0C-0x4F
- sharebankAddress = options.stack_loc + pic14_getSharebankSize() - 1;
- } else {
- /* If total RAM is less than 0x7f as with 16f84 then reduce
- * sharebankAddress to fit */
- if ((unsigned)sharebankAddress > pic14_getMaxRam())
- sharebankAddress = (int)pic14_getMaxRam();
- }
- return sharebankAddress;
+ memRange *r;
+
+ assert(pic);
+ r = pic->ram;
+
+ while (r) {
+ //fprintf (stderr, "%s: region %x..%x, bank %x, alias %x, pic->bankmask %x\n", __FUNCTION__, r->start_address, r->end_address, r->bank, r->alias, pic->bankMask);
+ if ((r->alias == pic->bankMask) && (r->start_address <= low) && (r->end_address >= high)) {
+ return 1;
+ } // if
+ r = r->next;
+ } // while
+
+ return 0;
+}
+
+/*
+ * True iff all RAM is aliased in all banks (no BANKSELs required except for
+ * SFRs).
+ */
+int pic14_allRAMShared(void)
+{
+ memRange *r;
+
+ assert(pic);
+ r = pic->ram;
+
+ while (r) {
+ if (r->alias != pic->bankMask) return 0;
+ r = r->next;
+ } // while
+
+ return 1;
+}
+
+/*
+ * True iff the pseudo stack is a sharebank --> let linker place it.
+ * [low, high] denotes a size byte long block of (shared or banked)
+ * memory to be used.
+ */
+int pic14_getSharedStack(int *low, int *high, int *size)
+{
+ int haveShared;
+ int l, h, s;
+
+ haveShared = pic14_hasSharebank(&l, &h, &s);
+ if ((options.stack_loc != 0) || !haveShared)
+ {
+ // sharebank not available or not to be used
+ s = options.stack_size ? options.stack_size : 0x10;
+ l = options.stack_loc ? options.stack_loc : 0x20;
+ h = (options.stack_loc ? options.stack_loc : 0x20) + s - 1;
+ if (low) *low = l;
+ if (high) *high = h;
+ if (size) *size = s;
+ // return 1 iff [low, high] is present in all banks
+ //fprintf(stderr, "%s: low %x, high %x, size %x, shared %d\n", __FUNCTION__, l, h, s, pic14_isShared(l, h));
+ return (pic14_isShared(l, h));
+ } else {
+ // sharebanks available for use by the stack
+ if (options.stack_size) s = options.stack_size;
+ else if (!s || s > 16) s = 16; // limit stack to 16 bytes in SHAREBANK
+
+ // provide addresses for sharebank
+ if (low) *low = l;
+ if (high) *high = l + s - 1;
+ if (size) *size = s;
+ //fprintf(stderr, "%s: low %x, high %x, size %x, shared 1\n", __FUNCTION__, l, h, s);
+ return 1;
+ }