Deleted
Deleted Member
Posts: 0
|
Post by Deleted on Apr 23, 2015 9:47:00 GMT -5
I've managed to scrape together code that sorta works. However, I vaguely remember someone coming up with a tutorial to deal with such things. The main things is that BEX doesn't seem to natively handle 8-bit numbers. Also, unless you're using an emulator, only some places in battery backed RAM actually store stuff.. forget if it's the even or odd byte locations.
Anybody have some examples of using and manipulating battery backed RAM?
|
|
Deleted
Deleted Member
Posts: 0
|
Post by Deleted on Apr 23, 2015 10:50:47 GMT -5
I made a post a few years back on how to use SRAM. Just search the forums (if you don't find it by the time I get home from work, I'll post a link)
|
|
Deleted
Deleted Member
Posts: 0
|
Post by Deleted on Apr 23, 2015 13:49:47 GMT -5
Even though a number of examples have already been posted on the forum, here's one more ..
One thing you need to be wary of in case you're using a Mega Everdrive, is that SRAM doesn't work when you upload your ROM via USB.
|
|
Deleted
Deleted Member
Posts: 0
|
Post by Deleted on Apr 24, 2015 10:58:25 GMT -5
Thanks for the example. I think I ended up creating a routine to save all of SRAM and another read all of SRAM into an array. I never figured out a good way to convert BEX integers into 8-bit values that could be easily shoved into the ODD locations in SRAM. Hmmn.. elusives post here might be useful for formatting the value to be saved into SRAM: devster.proboards.com/thread/982/binary-decimal-conversions
|
|
Deleted
Deleted Member
Posts: 0
|
Post by Deleted on Apr 24, 2015 11:20:38 GMT -5
Yeah. I think this is my own code churned up from a search. Not sure if it actually works.
option EXTERNALSRAM, &h200000, 8192, ODD
Dim SRAM(8191) AS INTEGER ' Temporary copy of SRAM DIM x AS INTEGER ' Mainly used for loops DIM y AS INTEGER ' Element in SRAM array we want to track DIM j AS INTEGER ' Variable to store joypad readings GLOBAL temp AS INTEGER ' A throw away variable
' Set variables to default values x = 0 y = 8189 a = 0 j = 0 temp = 0
' Copy SRAM to array for x = 0 to 8191 sram(x) = READSRAM(x) next
' Print contents of SRAM at place Y to prove saving to SRAM locate 1,1: print "SRAM Data: "; READSRAM(y) while j.6 != 1 ' Press and HOLD A button for program to continue j = joypad() sleep 1 for x = 0 to 8191 ' Write values to temporary SRAM array sram(x) = x next wend
' Write back contents of array to real SRAM for x = 0 to 8191 WRITESRAM sram(x), x next
' Print out some of the SRAM contents for x = 0 to 191 PRINT READSRAM(sram(x), x) next
' Write 111 to Y element in real SRAM to prove on next boot things are saved.. WRITESRAM 111, y
' Write value to real SRAM based on Elusives code ' Note emulators will write to even elements fine while real genesis ' may be forcing 8 bit values. DECLARE SUB WRITESRAM(w_val AS INTEGER, w_place AS INTEGER) asm "move #$2700,sr" poke &hA130F1, &h01 poke &h200001+w_place, w_val asm "move #$2000,sr" END SUB
' Read value from real SRAM based on Elusives code ' Note emulators will read even elements fine while real genesis ' may be forcing 8 bit values. DECLARE FUNCTION READSRAM(r_place AS INTEGER) asm "move #$2700,sr" temp = peek(&h200001+r_place) asm "move #$2000,sr" RETURN temp END FUNCTION
|
|
Deleted
Deleted Member
Posts: 0
|
Post by Deleted on Apr 24, 2015 16:55:27 GMT -5
Did you test it?
|
|
Deleted
Deleted Member
Posts: 0
|
Post by Deleted on Apr 25, 2015 7:34:21 GMT -5
I never figured out a good way to convert BEX integers into 8-bit values that could be easily shoved into the ODD locations in SRAM. Below is a easy to understand example ( opposed to fastest ) that reads & writes a array of integers to & from SRAM using only ODD addresses.
|
|
|
Post by Tamkis on Apr 26, 2015 12:47:55 GMT -5
|
|
Deleted
Deleted Member
Posts: 0
|
Post by Deleted on Apr 27, 2015 16:31:16 GMT -5
Thanks Titan! Is there a reason your example only goes to 7 instead of 8191? Wouldn't the usual amount of battery backed RAM allow 8191 entries?
|
|
Deleted
Deleted Member
Posts: 0
|
Post by Deleted on Apr 27, 2015 17:09:58 GMT -5
8192 wouldn't work in his example as he set the ram size to 2kb, but it's just an example to show you how to do it. You probably wouldn't want to make an array the size of the SRAM bank, though.
|
|
Deleted
Deleted Member
Posts: 0
|
Post by Deleted on Apr 28, 2015 8:37:45 GMT -5
We'd probably want the de-facto example to use the typical sram size. Is 2k what most carts use? I'm seeing a lot of Google hits for pcbs that take 64 KBIT.
I think in my code I was trying to verify that BEX was handling the battery backed RAM weirdly. Emulators saved values differently because of that even/odd thing and BEX variables were not 8-bit values like the sram expected. Sort of poking and prodding things to see what worked.
Now that I'm starting to remember I had this idea to make a game maker on cart. I'd load everything from sram and then save the game when the player was done editing his work. Kind of a rare case.
|
|
Deleted
Deleted Member
Posts: 0
|
Post by Deleted on Apr 28, 2015 9:00:46 GMT -5
Looking at elusives later comments it looks like a .."STEP 2" was added so that values would only be stored in odd locations. This is my cobbled together code with that change:
option EXTERNALSRAM, &h200000, 8192, ODD
Dim SRAM(8191) AS INTEGER ' Temporary copy of SRAM DIM x AS INTEGER ' Mainly used for loops DIM y AS INTEGER ' Element in SRAM array we want to track DIM j AS INTEGER ' Variable to store joypad readings GLOBAL temp AS INTEGER ' A throw away variable
' Set variables to default values x = 0 y = 8189 a = 0 j = 0 temp = 0
' Copy SRAM to array for x = 0 to 8191 sram(x) = READSRAM(x) next
' Print contents of SRAM at place Y to prove saving to SRAM locate 1,1: print "SRAM Data: "; READSRAM(y) while j.6 != 1 ' Press and HOLD A button for program to continue j = joypad() sleep 1 for x = 0 to 8192 STEP 2 ' Write values to temporary SRAM array sram(x) = x next wend
' Write back contents of array to real SRAM for x = 0 to 8192 STEP 2 WRITESRAM sram(x), x next
' Print out some of the SRAM contents for x = 0 to 191 PRINT READSRAM(sram(x), x) next
' Write 111 to Y element in real SRAM to prove on next boot things are saved.. WRITESRAM 111, y
' Write value to real SRAM based on Elusives code ' Note emulators will write to even elements fine while real genesis ' may be forcing 8 bit values. DECLARE SUB WRITESRAM(w_val AS INTEGER, w_place AS INTEGER) asm "move #$2700,sr" poke &hA130F1, &h01 poke &h200001+w_place, w_val asm "move #$2000,sr" END SUB
' Read value from real SRAM based on Elusives code ' Note emulators will read even elements fine while real genesis ' may be forcing 8 bit values. DECLARE FUNCTION READSRAM(r_place AS INTEGER) asm "move #$2700,sr" temp = peek(&h200001+r_place) asm "move #$2000,sr" RETURN temp END FUNCTION
|
|
Deleted
Deleted Member
Posts: 0
|
Post by Deleted on Apr 28, 2015 9:27:39 GMT -5
If you want to use odd numbers, then your loop should start on an odd number then incremented by 2, otherwise you're writing to even addresses.
|
|
Deleted
Deleted Member
Posts: 0
|
Post by Deleted on Apr 28, 2015 12:12:31 GMT -5
We'd probably want the de-facto example to use the typical sram size. Just checked 10 random games¹ that use SRAM ... and all of them have 8K of SRAM mapped to 0x200001 to 0x203FFF ( ODD only ). However, i also noticed that BEX calculates the SRAM end address slightly different from what i expected, which caused me to use "&h2000" ( 8192 ) instead of "&h1FFF" ( 8191 )². I've corrected this in my examples ¹ Aerobiz, Beyond Oasis, Exile, Light Crusader, MLBPA Baseball, PGA Tour Golf III, Phantasy Star IV, Pirates! Gold, Shining Force II, Ys III. ² When you use "EXTERNALSRAM,&h200000,&h2000,ODD" you end up with a SRAM end address of 0x204001, which is not what you want.
|
|