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 - Programming Homebrew with save game fuction

Reply to topic
Author Message
  • Joined: 21 Feb 2017
  • Posts: 11
Reply with quote
Programming Homebrew with save game fuction
Post Posted: Mon Mar 20, 2017 8:54 pm
Hello everyone,

I'm in the process of getting a simple homebrew game going for game gear. I did use the tutorials on here to get started, so thank you very much for those!

Now I'm looking at how I can code in a save game function. Does anyone know if there is any documentation on this? Please let me know where to find this information. I have spent a few hours googling around, but haven't made much progress.

Secondly, does anyone know of any current homebrew games that do have a save game function?

Thank you for any help.
  View user's profile Send private message
  • Site Admin
  • Joined: 19 Oct 1999
  • Posts: 14726
  • Location: London
Reply with quote
Post Posted: Mon Mar 20, 2017 9:14 pm
Last edited by Maxim on Mon Mar 20, 2017 9:15 pm; edited 1 time in total
I've used it a few times. You write to the register at $fffc to map the save RAM into the memory space, then just read and write to it from $8000 onwards. It's good practice to write a marker in there to allow you to detect when it's initialised/present.

http://www.smspower.org/Development/Mappers#RAMMapping

I think it is used in http://www.smspower.org/Homebrew/Picross-SMS and http://www.smspower.org/Homebrew/Ono-SMS .
  View user's profile Send private message Visit poster's website
  • Joined: 23 Mar 2013
  • Posts: 611
  • Location: Copenhagen, Denmark
Reply with quote
Post Posted: Mon Mar 20, 2017 9:14 pm
Hi,

I'm working on saving user inputs to the battery-backed external ram, in order to record player sprite movements, so that I can 'play back' these movements during attract screens. This is the core recorder loop:


  run_recorder:
  ;
  call await_frame_interrupt
  call get_input_ports
  ;
  ; Save the input ports at every 255th frame (4-5 sec).
  ld a,(frame_counter)
  dec a
  ld (frame_counter),a
  or a
  jp nz,+
    SELECT_EXTRAM
    ld hl,extram_header             ; Get extram header address.
    call get_word
    ld a,(InputPorts)               ; Read the variable set by GetInputPorts.
    ld (hl),a                       ; Write the InputPort state to extram.
    ld hl,extram_header             ; Increment the header (word).
    call inc_word
    SELECT_ROM
  +:
  jp main_loop


The key to this save feature lies in switching between external ram and rom. I use these two macros:


; =============================================================================
; M A C R O S
; =============================================================================
; -----------------------------------------------------------------------------
.macro SELECT_EXTRAM
; -----------------------------------------------------------------------------
  ld a,(BANK_CONTROL)
  or SET_EXTRAM_BIT
  ld (BANK_CONTROL),a
.endm
; -----------------------------------------------------------------------------
.macro SELECT_ROM
; -----------------------------------------------------------------------------
  ld a,(BANK_CONTROL)
  and RESET_EXTRAM_BIT
  ld (BANK_CONTROL),a
.endm


With these definitions:

.equ SET_EXTRAM_BIT %00001000
.equ RESET_EXTRAM_BIT %11110111

.equ EXTRAM_START $8000
.equ EXTRAM_SIZE $4000

.equ BANK_CONTROL $fffc


Of course this is not an example on saving a game, but it shows how to select the external ram; once external ram is selected, write savegame stuff to it (level, points, lives, etc.) - emulators will create a file on the disk to emulate the contents of this battery-backed, external ram. You can load from this ram the same way. Select it, then read from it as required, and select the rom again when done (all this stuff happens in slot 2).

#Edit: Argh, Maxim beat me to it :)
  View user's profile Send private message Visit poster's website
  • Joined: 21 Feb 2017
  • Posts: 11
Reply with quote
Post Posted: Tue Mar 21, 2017 4:00 am
Thank you guys. I'll read up on this!

Hang on
Thank you for the code example. Can I ask what RAM chip are you using for hardware or are you doing this all with emulation?
  View user's profile Send private message
  • Joined: 23 Mar 2013
  • Posts: 611
  • Location: Copenhagen, Denmark
Reply with quote
Post Posted: Tue Mar 21, 2017 7:13 am
You are welcome. Regarding ram chip, I don't know. I test my code on emulators (Emulicious and Meka) and on real hardware through an Everdrive (the above code is - for the moment - only tested on emulators).
  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!