Forums

Sega Master System / Mark III / Game Gear
SG-1000 / SC-3000 / SF-7000 / OMV
Home - Forums - Games - Scans - Maps - Cheats - Credits
Music - Videos - Development - Hacks - Translations - Homebrew

View topic - Bank switching for a total newbie (explanation)

Reply to topic
Author Message
  • Joined: 23 Sep 2021
  • Posts: 96
  • Location: Poland
Reply with quote
Bank switching for a total newbie (explanation)
Post Posted: Fri Sep 24, 2021 8:57 am
Hello. I'm completely new to the Z80 and SMS architecture. Previously, only 6502 and NES :)
I am not clear about switching banks in SMS. For example I would like to create a 128kb ROM, SLOT 0.1.2 16KB, SLOT 3 8KB RAM and switching banks will ONLY take place in SLOT 2 (SLOT0/1 fixed banks, SLOT 2 swappable / switchable bank).

Defining the banks:


.MEMORYMAP

   DEFAULTSLOT   0
   SLOT      0   START $0000 SIZE $4000      ; ROM 0
   SLOT      1   START $4000 SIZE $4000      ; ROM 1
   SLOT      2   START $8000 SIZE $4000      ; ROM 2
   SLOT      3   START $C000 SIZE $2000      ; 8KB RAM

.ENDME

.ROMBANKMAP

        BANKSTOTAL   8      ; use 8 banks,
        BANKSIZE   $4000      ; each 16 KB in size
        BANKS      8      ; (that's 128 KB)

.ENDRO


Banks:


.BANK 0 SLOT 0
.ORG $0000

.BANK 1 SLOT 1
.ORG $4000

.BANK 2 SLOT 2
.ORG $8000

.BANK 3
.ORG $8000

.BANK 4
.ORG $8000

.BANK 5
.ORG $8000

.BANK 6
.ORG $8000

.BANK 7
.ORG $8000


Does the addition of SLOT 0,1,2 after BANK 0,1,2 inform the assembler that BANK 0 takes SLOT 0 by default, BANK 1 takes SLOT 1 and BANK 2 takes SLOT 2? It is not clear to me. If, for example, I wrote to BANK 7 SLOT 2, then bank 7 would default to SLOT 2 at the start?
  View user's profile Send private message Visit poster's website
  • Site Admin
  • Joined: 19 Oct 1999
  • Posts: 14687
  • Location: London
Reply with quote
Post Posted: Fri Sep 24, 2021 9:19 am
The slot argument to .bank tells the assembler how to generate addresses, so for example if in slot 0 then .org outside 0-$3fff is an error and any labels will be computed in that range. If you omit it then I guess you get the default slot (0 in your code) (unless you issue .slot directives), so I’d always use it.

Defining slot 3 is only needed if you use ramsections.

Otherwise, your setup seems correct. The assembler won’t stop you switching slots 0-1, though - it’s your choice what to use, nothing is automatic.
  View user's profile Send private message Visit poster's website
  • Joined: 14 Apr 2013
  • Posts: 623
Reply with quote
Post Posted: Fri Sep 24, 2021 9:23 am
siudym wrote
Hello. I'm completely new to the Z80 and SMS architecture. Previously, only 6502 and NES :)
I am not clear about switching banks in SMS. For example I would like to create a 128kb ROM, SLOT 0.1.2 16KB, SLOT 3 8KB RAM and switching banks will ONLY take place in SLOT 2 (SLOT0/1 fixed banks, SLOT 2 swappable / switchable bank).

Defining the banks:


.MEMORYMAP

   DEFAULTSLOT   0
   SLOT      0   START $0000 SIZE $4000      ; ROM 0
   SLOT      1   START $4000 SIZE $4000      ; ROM 1
   SLOT      2   START $8000 SIZE $4000      ; ROM 2
   SLOT      3   START $C000 SIZE $2000      ; 8KB RAM

.ENDME

.ROMBANKMAP

        BANKSTOTAL   8      ; use 8 banks,
        BANKSIZE   $4000      ; each 16 KB in size
        BANKS      8      ; (that's 128 KB)

.ENDRO


Banks:


.BANK 0 SLOT 0
.ORG $0000

.BANK 1 SLOT 1
.ORG $4000

.BANK 2 SLOT 2
.ORG $8000

.BANK 3
.ORG $8000

.BANK 4
.ORG $8000

.BANK 5
.ORG $8000

.BANK 6
.ORG $8000

.BANK 7
.ORG $8000


Does the addition of SLOT 0,1,2 after BANK 0,1,2 inform the assembler that BANK 0 takes SLOT 0 by default, BANK 1 takes SLOT 1 and BANK 2 takes SLOT 2? It is not clear to me. If, for example, I wrote to BANK 7 SLOT 2, then bank 7 would default to SLOT 2 at the start?

You might want to set defaultslot to 2 instead of 0. It's what is used when no slot is specified.
Iirc .ORG is relative to the selected slot so $8000 won't work because it is greater than the slotsize of the selected slot. You should be doing .ORG $0000 instead.
If you want to use $0000, $4000, $8000 then you need to use .ORGA instead.

Other than that it looks good to me.

Edit: Maxim has been a bit quicker than me but I believe our information complements eachother quite well :)
  View user's profile Send private message Visit poster's website
  • Joined: 23 Sep 2021
  • Posts: 96
  • Location: Poland
Reply with quote
Post Posted: Fri Sep 24, 2021 12:01 pm
I thought that for WRAM to be active you need to define SLOT 3. ;)

I understand that I can switch SLOT0.1 but I would like to leave them unchanged (fixed slots) and only switch SLOT2.


.MEMORYMAP

   DEFAULTSLOT   0
   SLOT      0   START $0000 SIZE $4000      ; ROM 0
   SLOT      1   START $4000 SIZE $4000      ; ROM 1
   SLOT      2   START $8000 SIZE $4000      ; ROM 2

.ENDME

.ROMBANKMAP

        BANKSTOTAL   8      ; use 8 banks,
        BANKSIZE   $4000      ; each 16 KB in size
        BANKS      8      ; (that's 128 KB)

.ENDRO

.BANK 0 SLOT 0
.ORGA $0000

.BANK 1 SLOT 1
.ORGA $4000

.BANK 2 SLOT 2
.ORGA $8000

.BANK 3
.ORGA $8000

.BANK 4
.ORGA $8000

.BANK 5
.ORGA $8000

.BANK 6
.ORGA $8000

.BANK 7
.ORGA $8000


Correct?

So now, for example, how to switch BANK 2 to 7 in SLOT 2 now?

I have a subroutine in SLOT 0:


subroutine_at_slot0bank0:

   ;switch bank2 to 7 at slot2

   CALL subroutine_at_slot2bank7

   ;switch back bank 7 to 2 at slot2

   RET


For example, in NES with a simple UNROM mapping, this example would look like this:



subroutine:

   lda 7            ;switch bank 2 to 7
   sta banktable+7

   jsr subroutine_at_bank7

   lda 2            ;switch back to bank 2
   sta banktable+2

   RTS
  View user's profile Send private message Visit poster's website
  • Joined: 14 Apr 2013
  • Posts: 623
Reply with quote
Post Posted: Fri Sep 24, 2021 12:08 pm
With the SEGA mapper you would do

subroutine_at_slot0bank0:
ld a, 7
ld ($ffff), a

call subroutine_at_slot2bank7

ld a, 2
ld ($ffff), a

ret


With the SEGA mapper, $ffff is the address for the slot 2 bank.
$fffe and $fffd are for slots 1 and 0, respectively.

But the directives you posted still won't work because
.ORGA $8000
is not within SLOT 0.
Since you kept DEFAULTSLOT as 0, leaving out the SLOT for the BANK directives defaults to SLOT 0.
  View user's profile Send private message Visit poster's website
  • Joined: 23 Sep 2021
  • Posts: 96
  • Location: Poland
Reply with quote
Post Posted: Fri Sep 24, 2021 12:24 pm
Calindro wrote
With the SEGA mapper you would do
But the directives you posted still won't work because
.ORGA $8000
is not within SLOT 0.
Since you kept DEFAULTSLOT as 0, leaving out the SLOT for the BANK directives defaults to SLOT 0.


I think I misunderstood something :(
  View user's profile Send private message Visit poster's website
  • Joined: 14 Apr 2013
  • Posts: 623
Reply with quote
Post Posted: Fri Sep 24, 2021 12:28 pm
siudym wrote
Calindro wrote
With the SEGA mapper you would do
But the directives you posted still won't work because
.ORGA $8000
is not within SLOT 0.
Since you kept DEFAULTSLOT as 0, leaving out the SLOT for the BANK directives defaults to SLOT 0.


I think I misunderstood something :(

In the MEMORYMAP the first non-empty line says
DEFAULTSLOT 0

Just change that to
DEFAULTSLOT 2

This will make your .BANK directives that come without SLOT default to SLOT 2 instead of SLOT 0 and thus your .ORGA $8000 will work.
  View user's profile Send private message Visit poster's website
  • Joined: 23 Sep 2021
  • Posts: 96
  • Location: Poland
Reply with quote
Post Posted: Wed Oct 27, 2021 10:48 am
Okay, thanks, it's all clear now.

I have read the WLA-DX documentation carefully.

"DEFAULTSLOT describes the default slot for banks which aren’t explicitly inserted anywhere. Check .BANK definition for more information."

EDIT:

I don't know if this configuration is possible - can I set slot0/1 as one 32KB bank (slot size $8000), and the last slot as 16KB swappable (size 4000)?



.MEMORYMAP

   DEFAULTSLOT   1          ; 2?
   SLOT      0   START $0000 SIZE $8000      ; ROM 0+1
   SLOT      1   START $8000 SIZE $4000      ; ROM 2 (is it now as SLOT1 or still as SLOT 2?)

.ENDME

.ROMBANKMAP

        BANKSTOTAL   8      ; use 8 banks,
        BANKSIZE   $4000      ; each 16 KB in size (4000? 8000? I'm not sure here because there will be two slots of different sizes)
        BANKS      8      ; (that's 128 KB)

.ENDRO

.BANK 0 SLOT 0
.ORGA $0000

.BANK 1 SLOT 1 (SLOT 2 ?)
.ORGA $8000

.BANK 2
.ORGA $8000

.BANK 3
.ORGA $8000

.BANK 4
.ORGA $8000

.BANK 5
.ORGA $8000

.BANK 6
.ORGA $8000

.BANK 7
.ORGA $8000

  View user's profile Send private message Visit poster's website
  • Site Admin
  • Joined: 19 Oct 1999
  • Posts: 14687
  • Location: London
Reply with quote
Post Posted: Wed Oct 27, 2021 7:48 pm
The trick is to make one bank that is 32KB-16B and a second one that is 16B, so that slot numbering works properly.
  View user's profile Send private message Visit poster's website
Reply to topic



Back to the top of this page

Back to SMS Power!