Count your RAM banks

Tutorials and help articles.

(Posts require approval. See pinned post.)
Forum rules
Post guides, tutorials, and other instructional content here.

This topic area requires approval, so please be patient while we review content to make sure it fits the expectations for this topic area.

Tech support questions should be asked in Hardware or Software support.
rje
Posts: 1263
Joined: Mon Apr 27, 2020 10:00 pm
Location: Dallas Area

Count your RAM banks

Post by rje »


Here's how I DID it.  See what's wrong?

void determineBankCount()
{
   setRAMbank(1);
   POKE(0xa000,17); // 17 is "a very random number"
bankCount = 256;  // starting assumption; prove me wrong.
   setRAMbank(129);
   if (PEEK(0xa000) == 17) bankCount = 128; // 1024K
   setRAMbank(65);
   if (PEEK(0xa000) == 17) bankCount = 64; // 512K
}

 

Here's a mo' betta' way:

void determineBankCount()
{
   bankCount = 256;

// I could do this with a loop, but meh.

setRAMbank(192);
   POKE(0xb000,17);
   setRAMbank(0);
   if (PEEK(0xb000) == 17) bankCount = 192;

   setRAMbank(128);
   POKE(0xb000,17);
   setRAMbank(0);
   if (PEEK(0xb000) == 17) bankCount = 128;

   setRAMbank(64);
   POKE(0xb000,17);
   setRAMbank(0);
   if (PEEK(0xb000) == 17) bankCount = 64;
}

 

Lorin Millsap
Posts: 193
Joined: Wed Apr 29, 2020 6:46 pm

Count your RAM banks

Post by Lorin Millsap »

That’s not entirely accurate. You can totally have 1536K.


Sent from my iPhone using Tapatalk
User avatar
desertfish
Posts: 1097
Joined: Tue Aug 25, 2020 8:27 pm
Location: Netherlands

Count your RAM banks

Post by desertfish »


If you use MEMTOP kernal routine with carry set, it will return the number of ram banks in A:

sec
jsr MEMTOP ;
$FF99
sta num_banks

 

Lorin Millsap
Posts: 193
Joined: Wed Apr 29, 2020 6:46 pm

Count your RAM banks

Post by Lorin Millsap »

If you use MEMTOP kernal routine with carry set, it will return the number of ram banks in A:
secjsr  MEMTOP     ; $FF99sta  num_banks
 

Question. I’m not as much of a programmer, but is MEMTOP not mirrored in RAM?


Sent from my iPhone using Tapatalk
User avatar
desertfish
Posts: 1097
Joined: Tue Aug 25, 2020 8:27 pm
Location: Netherlands

Count your RAM banks

Post by desertfish »


I don't know what you mean precisely with the question sorry

Lorin Millsap
Posts: 193
Joined: Wed Apr 29, 2020 6:46 pm

Count your RAM banks

Post by Lorin Millsap »

I don't know what you mean precisely with the question sorry
For at least most KERNAL routines they are located in RAM in a vector table. The problem with using the ROM vector table is new routines that supersede the KERNAL functions can be pointed to using the RAM vectors whereas if you use the ROM vectors you don’t get the benefit of the new routines. The commodore systems would copy the KERNAL vectors to RAM locations which meant you could inject patches.  I don’t know if this routine in particular has a RAM vector or not.  Sent from my iPhone using Tapatalk
User avatar
desertfish
Posts: 1097
Joined: Tue Aug 25, 2020 8:27 pm
Location: Netherlands

Count your RAM banks

Post by desertfish »


For what I can see, it is not mirrored via a vector in ram. The entry at $ff99 in the kernal's jump table directly jmps to another location in rom, instead of doing a vectored (indirect) jmp. Indeed a handful of kernal routines do the vectoring (not many, tbh), but  MEMTOP isn't among those.   Does this matter?

 

rje
Posts: 1263
Joined: Mon Apr 27, 2020 10:00 pm
Location: Dallas Area

Count your RAM banks

Post by rje »



3 hours ago, Lorin Millsap said:




That’s not entirely accurate. You can totally have 1536K.





Sent from my iPhone using Tapatalk



Ha, yeah I suspected so.  Actually I do have that line in my code... but the (r38) emulator won't allow that setting for testing.

 

   setRAMbank(193);
   if (PEEK(0xa000) == 17) bankCount = 192;

The r38 Usage says


Quote




-ram <ramsize>



Specify banked RAM size in KB (8, 16, 32, ..., 2048).



The default is 512.



And it poops out when I run it with -ram 1536 ?

rje
Posts: 1263
Joined: Mon Apr 27, 2020 10:00 pm
Location: Dallas Area

Count your RAM banks

Post by rje »



2 hours ago, desertfish said:




If you use MEMTOP kernal routine with carry set, it will return the number of ram banks in A:




sec
jsr MEMTOP ;
$FF99
sta num_banks



 



You're right - I could (should) just use MEMTOP and check the accumulator.  I did it brute force instead, which might actually not work in real life if the X16 doesn't do RAM mirroring.

 

And as far as MEMTOP goes: the KERNAL routine itself has been rejiggered to return the bank count, so I don't think it matters that the vector is in ROM.

 

And... correct me if I'm wrong; I've never really had to think about this, but:  isn't it just the I/O KERNAL routines that COME FROM (for those of you fluent in INTERCAL) an indirect vector that's initialized into RAM at boot time?   I.E. the only things you'd need to "re-vector" are I/O, isn't that correct?     Oh heck, I need to go back to pagetable.com and study harder.

 

Wavicle
Posts: 284
Joined: Sun Feb 21, 2021 2:40 am

Count your RAM banks

Post by Wavicle »



40 minutes ago, rje said:




You're right - I could just use MEMTOP and check the accumulator.  I did it brute force instead, which might actually not work in real life if the X16 doesn't do RAM mirroring.



Your thinking isn't far off though. You can see how KERNAL does it here. The biggest difference between your implementation and KERNAL is that you use the random number 17 and KERNAL uses the random number "1 + whatever is in $a000 of bank 0". If I'm reading the code correctly, KERNAL is making the optimization assumption that the number of banks will always be a power of 2 since the loop uses the accumulator to select the bank and modifies the accumulator with asl. If loop #7 passes (testing bank #128) it calls memtop with 0 in A, which I guess means 256 banks (2 MB).


2 hours ago, desertfish said:




For what I can see, it is not mirrored via a vector in ram. The entry at $ff99 in the kernal's jump table directly jmps to another location in rom, instead of doing a vectored (indirect) jmp. Indeed a handful of kernal routines do the vectoring (not many, tbh), but  MEMTOP isn't among those.   Does this matter?



 



It is stored in the KERNAL variable area (KVAR segment, which is $0200-$02BA I think) but you'd probably need to look at the assembler map file to know where. Most importantly though: doing so would be a bad idea. There is nothing that says the location is guaranteed to be the same across all releases of KERNAL. MEMTOP is guaranteed to always return the correct value.

Post Reply