[stella] Game Framework

Subject: [stella] Game Framework
From: Mark De Smet <de-smet@xxxxxxxxxxxxxxx>
Date: Wed, 21 Jun 2000 22:01:38 -0500 (CDT)
This is a file that I created a while back to use as a frame work for
creating games, or testing stuff out.  I made it because I realised that
most people used the how to draw a playfield, and that is not a very good
framework, and it doesn't make use of the overscan(it wastes it).  This
frame work allows you to put your code into both the overscan, and
vertical blank.(I have setup the timer to time both)  It is also well
commented, and includes recommendation for optimization.

It draws the rainbow, and shows a binary count(that represents the
colors) with a quad width player graphic.

It is not technically done, but I realised that I am probably not going to
finish it, so I might as well post it.

Improvements welcome!

Mark





;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; Basic game framework
; by Mark De Smet 9/9/99
;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
;This file is intended to be used as a basic framework for implementing 
;a game.  All it does is display the scrolling rainbow and the binary
;values of those colors.  To remove the rainbow display, remove the lines 
;with markers "REMOVE".  You will then want to add in your own display 
;kernal/s and your game logic.  Game logic can be placed into GameCalc and 
;GameCalc2
;


;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
;                                         CODE CONFIG
;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
;stuff to setup dasm, and compile for atari.

	processor 6502
	include vcs.h

;Your variables and constants:
	seg.u ramspace
	org $80		;ram space
;REMOVE this next line for your game
linecolor	ds 1  ;a variable for the first line color.



;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
;                                        CODE
;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
;start program at beginning of ROM space
	seg romspace
	org $F000
Start

;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
;                                        INIT
;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
;Initialise your game here.

	SEI  ; Disable interrupts, if there are any.
	CLD  ; Clear BCD math bit.
;Setup the stack
	LDX  #$FF
	TXS  ; Set stack to beginning.

; Since X is already loaded to 0xFF, our task becomes simply to ocunt
; everything off.

	LDA #0
B1      STA 0,X
	DEX
	BNE B1

;REMOVE the next 2 lines for your game
	LDA #$07
	STA NUSIZ0

;load up the timer, not to use, but because we don't start the vertical 
;sync until the timer is zero.  Additionally, if the timer is counting
;down by ones, it is possible that the timer wait loop will never exit
;otherwise.
	LDA #10		;load it with just some small number.
			;actual wait time is irrelevant because
			;we've already wasted a bunch of lines doing that
			;memory blank.
	STA TIM8T

;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
;                                          MAIN LOOP
;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
;add any more subroutines that you may wish.
;
;to save both rom(use less code) and ram(use less stack) space, you
;can remove this entire loop, and simply jmp between each routine(because
;they are only ever called in the same order) such that at the end of
;VerticalBlank, instead of a JSR, put a JMP GameCalc.  And so on.  At the
;end of Overscan, then you would replace RTS with JMP VerticalBlank
MainLoop
	JSR VerticalSync	;start up the vertical blank counter and
				;do vertical sync.
	JSR GameCalc		;Do calculations during Vblank
	JSR DrawScreen		;Draw the screen
	JSR OverScan		;start up the overscan counter
	JSR GameCalc2		;Do More calculations during overscan.
	JMP MainLoop		;Continue forever.


;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
;                                Vertical Sync
;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
;This routine does the three lines of VSYNC, and sets up the timer for
;counting the length of the vertical blanking interval.
;
;Note that there are 3 STA WSYNC's in a row.  Between each, you can add
;extra instructions etc as long as the entire length of the instructions
;between WSYNC's is less than 76 processor cycles.
;However, you would only bother doing this if you ran out of time 
;during the vertical blanking and overscan intervals, or if you had some 
;small bit of code you wanted to take care of, but don't want to fit in 
;elsewhere.
;

VerticalSync

endoverscan
	LDA INTIM
	BNE endoverscan
	LDA  #2
	STA WSYNC
;wrong?:
	STA  WSYNC  
;wrong?:
	STA  WSYNC
;wrong?:
	STA  WSYNC
	STA  VSYNC	;Begin vertical sync.

;clear the graphics registers incase we forget to reset them before the
;first line of our kernal.
	LDA #0
	STA PF0
	STA PF1
	STA PF1
	STA GRP0
	STA GRP1
	STA ENAM0
	STA ENAM1
	STA ENABL

	STA  WSYNC	; First line of VSYNC
	STA  WSYNC	; Second line of VSYNC.
	LDA  #44	;setup the timer for vertical blank.
	STA  TIM64T
	STA  WSYNC	; Third line of VSYNC.
	STA  VSYNC	; (0)

	RTS  



;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
;                                   game calculations
;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
;This routine takes place in the vertical blank period.
;This and everything called from this must be completed in about 2800 cycles.
;If we assume that every instruction is 3 cycles, then that is about 900
;instructions.
;If you add a routine to the main loop that is also in the vertical blank, 
;then that routine, plus this one must fit in the 2800 cycles.
;
GameCalc





;You may wish to clear the collision register before the screen is drawn.
	LDA #0
	STA CXCLR
	RTS




;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
;                                  Draw the screen
;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
;
;
DrawScreen
;wait for end of the vertical blank
	LDA INTIM
	BNE DrawScreen
	STA WSYNC	;start at the beginning of next line.
	STA VBLANK	;End the VBLANK period with a zero.


;REMOVE the next 4 instructions for your game.
;make the rainbow.
	DEC linecolor	;scroll the color.
	DEC linecolor	;lower bit of color register not really used, so 
			;let's up it by two so it will scroll really fast!
	LDX linecolor	;load up the line color.
	STX COLUBK



	LDY #191	;the number of lines in this kernal.
;REMOVE the next line for your game.
	STA RESP0
ScanLoop
	STA WSYNC	;wait for beginning of next line.
;draw this line:


;REMOVE the next 4 instructions for your game.
	INX		;change line color for next line.
	INX		;change color everyline, not everyother.
	STX COLUBK	;set line color
	STX GRP0	;display it's number

	DEY		;move counter to next line
	BNE ScanLoop	;if not done w/ this kernal, do another line.


	LDA #2
	STA WSYNC  ;Finish this scanline.
;begin line '196'
	STA VBLANK ; Make TIA output invisible,
	RTS


;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
;                                      overscan
;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
;start up the timer for counting the length of the overscan.
;
;
OverScan
	STA WSYNC
	LDA #34
	STA TIM64T



	RTS


;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
;                                   game calculations, part 2
;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
;This routine takes place in the overscan period.
;
;This and everything called from this must be completed in about 2100 cycles.
;If we assume that every instruction is 3 cycles, then that is about 700
;instructions.
;If you add a routine to the main loop that is also in the overscan, 
;then that routine, plus this one must fit in the 2100 cycles.
;
GameCalc2


	RTS





	org $FFFC
	.word Start	;power up vector
	.word Start	;interrupt vector
			;The data for this vector is not needed, but you
			;need to add two more bytes here if you want to
			;make it compile to an even 4096 bytes(4K)



--
Archives (includes files) at http://www.biglist.com/lists/stella/archives/
Unsub & more at http://www.biglist.com/lists/stella/

Current Thread