I got inspired this weekend and implemented a helper macro library to use with CART.MAC that supports overlapping RAM segments. It's rather simple at the moment. I toyed with the idea of making it more comprehensive--allowing for extending and appending segments or multiple super-segments each containing overlapping sub-segments--but my priority right now is finishing Christmas Carol.
I'll certainly expand it in the future. It will form part of the upcoming P-Machinery v2.0.
Anyway, the library offers the following macros:
;; ======================================================================== ;; RAMSTART Starts RAM segment allocation. ;; RAMEND Ends RAM segment allocation. ;; RAMSEG_START Defines a new RAM segment and opens it for use. ;; RAMSEG_END Closes an open RAM segment. ;; ========================================================================
It works similar to the "ROMSEG" macro in CART.MAC: You call RAMSTART somewhere at the top of your code, to initialize RAM segmentation and mark the beginning of available RAM; and then RAMEND at the end to close any outstanding open segments. In between, you may define as many segments as you like, and each one resets the RAM allocation pointers in CART.MAC to the same base address.
RAMSTART also defines a default segment called "Global" that is never overlapped. This allows you to define global variables that will be shared across all your game states, without the threat of accidental overwrite by your other segments. If you don't need this global segment, just ignore it and call RAMSEG_START() to define your own segments.
Along with the "RAMSEG.MAC" library, I include a modified "STATS.MAC" library from P-Machinery v1.0 to output useful information regarding your RAM and ROM segment usage. You include this library at the very end of your code--after calling ROMEND.
Below is an example usage, taken from Christmas Carol. The game is split into mutually exclusive states, and each one requiring its own set of variables. There are also variables that are shared across all states, so they are perfect candidates for the "Global" segment.
Let me know if you have any questions.
;;==========================================================================;; ;; Title: Christmas Carol vs. The Ghost Of Christmas Presents ;; ;; By: DZ-Jay ;; ;; Description: An original concept game, built on the P-MACH engine, ;; ;; originally developed to port Pac-Man to the Intellivision. ;; ;; ;; ;; Copyright 2010-2012, James Pujals (DZ-Jay), <email@example.com>. ;; ;;==========================================================================;; ;; ===================================================== ;; ROM HEADER SET-UP ;; ===================================================== INCLUDE "cart.mac" INCLUDE "macro/ramseg.mac" ROMSETUP 42K, 2012, "Christmas Carol", BOOT_UP, STACK_SIZE ; ... ; ====================================================== ; GAME WORLD STATE ; ====================================================== RAMSTART ; Start "Global" segment PLAYER_INFO STRUCT 0 @@Rank SCRATCH 1 @@ScoreRevs SCRATCH 1 @@Score SCRATCH 2 @@Lives SCRATCH 1 ENDS ; ====================================================== ; PRACTICE MODE STATE ; ====================================================== RAMSEG_START(Practice) ; Start "Practice" segment SCROLL_INFO STRUCT 0 @@Delay SCRATCH 1 @@State SCRATCH 1 @@Flags SCRATCH 1 @@Column SCRATCH 1 @@PadCol SCRATCH 1 @@RateFrac SCRATCH 1 ENDS ; ... ; ====================================================== ; LEVEL PLAY STATE ; ====================================================== RAMSEG_START(Level) ; Start "Level" segment LEVEL_INFO STRUCT 0 @@CandyCnt SCRATCH 1 @@SnoflkCnt SCRATCH 1 @@Deaths SCRATCH 1 @@Perfect SCRATCH 1 @@PresentCnt SCRATCH 1 @@PresentVec SCRATCH 1 @@BonusMult SCRATCH 1 @@PlayerHits SCRATCH 1 ENDS ; ... ;; ===================================================== ;; END OF LINE. ;; ===================================================== RAMEND ; End RAM segmentation ROMEND INCLUDE "macro/stats.mac"
RAMSEG Lib.zip 4.36KB 49 downloads
Edited by DZ-Jay, Sun Jul 15, 2012 10:38 AM.