Here is the code that I use to detect SWRAM banks along with the code to copy to it.
I have posted this code before and I would like it to be on RetroSoftware, but I thought that I would post it here in my own thread
It is generated when SWRAM needs to be detected by my game loader generator used by all my games.
501 FOR O = 0 TO 3 STEP 2
502 P%=&2700
503 [OPT O
556.find_swr
557 SEI
558 LDY #15
559 STY &FE62
560 TYA
561.next_val
562 LDX #15
563 ADC #1
564.next_slot
565 STX &FE30
566 CMP &8008
567 BEQ next_val
568 CMP &80,X
569 BEQ next_val
570 DEX
571 BPL next_slot
572 STA &80,Y
573 LDX &8008
574 STX &70,Y
575 DEY
576 BPL next_val
577 LDX #0
578.swap
579 STX &FE60
580 STX &FE32
581 STX &FE30
582 STA &FF30,X
583 LDA &80,X
584 STA &8008
585 INX
586 CPX #16
587 BNE swap
588 LDY #16
589 LDX #15
590.tst_restore
591 STX &FE30
592 LDA &8008
593 CMP &80,X
594 BNE notswr
595 STX &FE60
596 STX &FE32
597 STX &FE30
598 STA &FF30,X
599 LDA &70,X
600 STA &8008
601 DEY
602 STX &70,Y
603.notswr
604 DEX
605 BPL tst_restore
606 STY &70
607 LDA &F4
608 STA &FE30
609 CLI
610 RTS
620 ]
621 NEXT
558-576 try to find 16 unique values that don't occur at &8008 in the SWROMs, these will be written to the bankss and then all read back to see which values won to avoid detecting mirrored banks multiple times - I think this idea came from someone else's code.
559 is actually for the next section to allow writes to the first four bits of the user via, but we have &0F in a register here!
577-587 try to write the corresponding unique number to &8008 in the slot.
579 STX &FE60 - set Solidisk SWRAM index on early boards
580 STX &FE32 - set RAMSEL only on Master and Compact (iirc)
581 STX &FE30 - set ROMSEL, often used by homebrew and some commercial expansions
582 STA &FF30,X - set Watford ROMRAM/2MEG board - write anything
588-605 check if each value in a slot matches the unique one assigned and add to the list if it does using the same writing methods as before
each byte is compared against the unique value stoted in &7x and then after restoring it, &7x is free to store the bank number if required.
606 write the index of the first SWRAM slot to &70 (is all banks are SWRAM, this will be 0, so will effectively point to itself, which is still valid.
If none were found, this will be 16, so number of banks found is 16-?&70 and they &70?(?&70)..?&7F
607-608 just restores the OS selected bank before returning.
Because testing for solidisk boards can confuse an MMC device attached to the user port, causing it to fail the next operation, you need to take account of this after checking.
To have one retry, I use:
10 ON ERROR GOTO 12
11 GOTO 13
12 ON ERROR OFF
13 *LOAD MPatrol
You could omit 559, 579 and 595 if you weren't supporting early Solidisc SWRAM boards.
After deciding which SWRAM banks to use, you then need to use the same mechanism to copy the data from main RAM to SWRAM
504.copy_to_swram
505 LDX &70
506 LDY #0
507.page
508 LDA #15
509 SEI
510 STA &FE62
511 STX &FE60
512 STX &FE32
513 STA &FF30,X
514 STX &FE30
515.loop
516 LDA (&72),Y
517 STA (&74),Y
518 INY
519 BNE loop
520 LDA &F4
521 STA &FE30
522 CLI
523 INC &73
524 INC &75
525 DEC &71
526 BNE page
527 RTS
?&70 contains the SWRAM bank to write to, &71 the number of pages to copy, (&72) the source and (&74) the dest.
Because I call this from BASIC, I re-enable interrupts after each page, just to be polite!
These is another type of SWRAM that requires a different method!
The Watford 12 ROMRAM board only allows its single bank of SWRAM always in bank 14 to be written to by a ROM already on the board and any write from code on the board will be written to the SWRAM.
I usually rely on BASIC to be on the ROMRAM board and load the file into RAM as if I was going to copy it with the copier above but then use a BASIC FOR loop to do the copy X%!&8000 = X%!&3000 or similar.
The other simple option is to try to *LOAD the file at &8000 which will "just work" if the FS is on the ROMRAM board.
I rely on BASIC being on the board, so detect the ROM by using ROM sel to read &8008 from bank 14, EOR it with &FF and write it back with basic, read it again and write the original value back. if the two reads were EOR &FF different (or just different) then BASIC must be able to write to the bank.
I guess I should check that BASIC isn't in a SWRAM bank where ROMSEL is the write select by not doing this if &F4 is 14
PS I also check for the ROM before I start in case it is already loaded or in an actual ROM.
PPS I also check for a B+ explicitly if 12K is enough SWRAM.