
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;										;
;	Adventure version 0.2				;
;	by brad slattery 2008				;
;										;
;	An adventure game for the 8x8 Game	;
;	System (not yet completed)									;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

	LIST p=16f648a 					; tell assembler what chip we are using
	include "P16f648a.inc"			; include the defaults for the chip
	__config 0x3f18					; sets the configuration settings (oscillator type etc.)

PC equ 0x02

	cblock 0x20 					; start of general purpose registers
		counta						; Used in the delay routine
		countb 						; Used in the delay routine
		pc_room						; Our program counter for our room data
		pc_column					; Our program counter for our column data
		frame_repeat				; Used to hold our frame repeat data
		active_row
		man_x
		man_y
		bomb_x
		bomb_y
		bomb_timer
		button_timer
		button_adjust
		stop_repeat
		do_bomb
		explode_timer
		bomb_temp_x
		bomb_temp_y
		room_number
		room_x
		room_y
	endc

	org 0x0000						; org sets the origin, 0x0000 for the 16F628,


	movlw h'07'
	movwf CMCON 					; turn comparators off (make it like a 16F84)

	bsf STATUS, RP0 				; select bank 1
	movlw b'00000000' 				; set PORTB all outputs
	movwf TRISB						;
	movlw b'00100000' 				; set PORTA all outputs except for bit 5
	movwf TRISA 					;
	bcf STATUS, RP0 				; select bank 0
	goto setup


room_data
	movf pc_room, w
	incf pc_room, f
	addwf PC, f
	; room 0
	retlw b'11111111'
	retlw b'11000011'
	retlw b'10000001'
	retlw b'00011001'
	retlw b'00011001'	
	retlw b'10000001'
	retlw b'11000011'
	retlw b'11100111'
	; room 1
	retlw b'11100111'
	retlw b'10000001'
	retlw b'11000111'
	retlw b'10001001'
	retlw b'10001001'
	retlw b'11000001'
	retlw b'11100001'
	retlw b'11100111'
	; room 2
	retlw b'11100111'
	retlw b'11000011'
	retlw b'10000001'
	retlw b'10110001'
	retlw b'10111001'
	retlw b'10010001'
	retlw b'11000011'
	retlw b'11100111'
	; room 3
	retlw b'11100111'
	retlw b'11100111'
	retlw b'11100111'
	retlw b'11000011'
	retlw b'11000011'
	retlw b'10000001'
	retlw b'10000001'
	retlw b'11100111'
	; room 4
	retlw b'11100111'
	retlw b'11100001'
	retlw b'10000001'
	retlw b'00000001'
	retlw b'00000001'
	retlw b'11000011'
	retlw b'11100011'
	retlw b'11100111'
	; room 5
	retlw b'11100111'
	retlw b'10000001'
	retlw b'10000001'
	retlw b'11111101'
	retlw b'10000001'
	retlw b'10111111'
	retlw b'10000001'
	retlw b'11100111'
	; room 6
	retlw b'11100111'
	retlw b'10000111'
	retlw b'10000111'
	retlw b'00000011'
	retlw b'00000001'
	retlw b'10000001'
	retlw b'11000001'
	retlw b'11100111'
	; room 7
	retlw b'11100111'
	retlw b'10000111'
	retlw b'10000111'
	retlw b'10000011'
	retlw b'10000011'
	retlw b'10000001'
	retlw b'11110001'
	retlw b'11111111'
	; room 8
	retlw b'11111111'
	retlw b'11000011'
	retlw b'10011001'
	retlw b'10111100'
	retlw b'10111100'
	retlw b'10011001'
	retlw b'11000011'
	retlw b'11100111'
	; room 9
	retlw b'11100111'
	retlw b'10000101'
	retlw b'10100101'
	retlw b'10100101'
	retlw b'10100101'
	retlw b'10100101'
	retlw b'10100001'
	retlw b'11100111'
	; room 10
	retlw b'11100111'
	retlw b'10100011'
	retlw b'10110001'
	retlw b'00111101'
	retlw b'00111101'
	retlw b'10011001'
	retlw b'11000011'
	retlw b'11100111'
	; room 11
	retlw b'11100111'
	retlw b'10000001'
	retlw b'11000011'
	retlw b'11100111'
	retlw b'11100111'
	retlw b'11000011'
	retlw b'10000001'
	retlw b'11100111'
	; room 12
	retlw b'11100111'
	retlw b'10000111'
	retlw b'10110011'
	retlw b'10111000'
	retlw b'10110010'
	retlw b'10100111'
	retlw b'11000001'
	retlw b'11111111'
	; room 13
	retlw b'11111111'
	retlw b'10000001'
	retlw b'10111101'
	retlw b'00111101'
	retlw b'00011101'
	retlw b'10001101'
	retlw b'11000111'
	retlw b'11100111'
	; room 14
	retlw b'11100111'
	retlw b'11100011'
	retlw b'11110001'
	retlw b'10000000'
	retlw b'10111100'
	retlw b'10011101'
	retlw b'10000001'
	retlw b'11100111'
	; room 15
	retlw b'11100111'
	retlw b'10000001'
	retlw b'10011001'
	retlw b'10111101'
	retlw b'10111101'
	retlw b'10011001'
	retlw b'10000001'
	retlw b'11111111'
	return


column_data
	movf pc_column, w	
	incf pc_column, f
	addwf PC, f
	retlw b'10000000'
	retlw b'01000000'
	retlw b'00100000'
	retlw b'00010000'
	retlw b'00001000'
	retlw b'00000100'
	retlw b'00000010'
	retlw b'00000001'
	return


setup
	movlw b'11011010'				; disable everything
	movwf PORTA
	clrf pc_room
	clrf pc_column
	movlw b'00000010'
	movwf man_x
	movlw b'00001000'
	movwf man_y
	movlw d'15'
	movwf button_adjust
	clrf stop_repeat
	movlw d'01'					; start in the first room of course!
	movwf room_x
	movwf room_y
	clrf bomb_timer
	clrf do_bomb
	clrf bomb_temp_x
	clrf bomb_temp_y
	clrf room_number


begin
	call draw_man
	call joystick
	call draw_room
	btfsc do_bomb, 0
	call drop_bomb
	btfsc stop_repeat, 1
	call button_time
	btfsc do_bomb, 1
	call explode_bomb
	goto begin



draw_room
	movlw d'08'
	movwf frame_repeat
		movf man_x, w
		andlw b'00000001'
		btfss STATUS, Z
		call room_left
		movf man_x, w
		andlw b'10000000'
		btfss STATUS, Z
		call room_right
		movf man_y, w
		andlw b'10000000'
		btfss STATUS, Z
		call room_up
		movf man_y, w
		andlw b'00000001'
		btfss STATUS, Z
		call room_down
		call room_x_data
		movwf pc_room
		call room_y_data
		addwf pc_room, f
		clrf pc_column
draw_again
		call room_data
		movwf PORTB
		bsf PORTA, 2
		bcf PORTA, 2
	call column_data
	movwf PORTB
		bsf PORTA, 4
		bcf PORTA, 4
	bcf PORTA, 3
	bcf PORTA, 6		
	call delay
	bsf PORTA, 3
	bsf PORTA, 6
	decfsz frame_repeat, 1
	goto draw_again	
	return


room_left
		rlf room_x, f
		movlw b'01000000'
		movwf man_x
	return


room_right
		rrf room_x, f
		movlw b'00000010'
		movwf man_x
	return

room_up
		rlf room_y, f
		movlw b'00000010'
		movwf man_y
	return


room_down
		rrf room_y, f
		movlw b'01000000'
		movwf man_y
	return


room_x_data
	btfsc room_x, 0
	movlw d'00'
	btfsc room_x, 1
	movlw d'08'
	btfsc room_x, 2
	movlw d'16'
	btfsc room_x, 3
	movlw d'24'
	btfsc room_x, 4
	movlw d'32'
	btfsc room_x, 5
	movlw d'40'
	btfsc room_x, 6
	movlw d'48'
	btfsc room_x, 7
	movlw d'56'
	return


room_y_data
	btfsc room_y, 0
	movlw d'00'
	btfsc room_y, 1
	movlw d'64'
	return


man_up
	movlw b'01000000'
	movwf man_y
	return

man_down
	movlw b'00000010'
	movwf man_y
	return


check_up
	movf man_y, w
	andlw b'10000000'
	return

check_down
	movf man_y, w
	andlw b'00000001'
	return

check_left
	return
	
check_right
	return

draw_man
	movf man_x, w
	movwf PORTB
	bsf PORTA, 4
	bcf PORTA, 4
	movf man_y, w
	movwf PORTB
	bsf PORTA, 0
	bcf PORTA, 0
	bcf PORTA, 1
	bcf PORTA, 6
	call delay
	bsf PORTA, 1
	bsf PORTA, 6
	return

drop_bomb
	movf bomb_x, w
	movwf PORTB
	bsf PORTA, 4
	bcf PORTA, 4
	movf bomb_y, w
	movwf PORTB
	bsf PORTA, 2
	bcf PORTA, 2
	bcf PORTA, 3
	bcf PORTA, 6
	call delay
	bsf PORTA, 3
	bsf PORTA, 6	
	decfsz bomb_timer, 1
	return
	bcf do_bomb, 0
	bsf do_bomb, 1
	movlw d'50'
	movwf explode_timer
	return


explode_bomb
	movf bomb_y, w
	movwf bomb_temp_y
	rlf bomb_temp_y, 1
	addwf bomb_temp_y, w
	movwf PORTB
	bsf PORTA, 0
	bcf PORTA, 0
	movf bomb_x, w
	movwf bomb_temp_x
	rlf bomb_temp_x, 1
	addwf bomb_temp_x, w
	movwf PORTB
	bsf PORTA, 4
	bcf PORTA, 4
	bcf PORTA, 1
	bcf PORTA, 6
	call delay
	bsf PORTA, 1
	bsf PORTA, 6
	decfsz explode_timer, 1
	return
	bcf do_bomb, 1
	return



joystick
	call port_b_input
	bcf PORTA, 7
	btfss PORTB, 0
	call go_up
	btfss PORTB, 1
	call go_down
	btfss PORTB, 2
	call go_left
	btfss PORTB, 3
	call go_right
	btfss PORTB, 4
	call button_a	
	bsf PORTA, 7
	call port_b_output
	return


port_b_input
   	bsf 	STATUS,	RP0		;this needs to be here so you dont get red
	movlw	b'11111111'		;ghosting on the screen that you'd get if
	movwf	TRISB			;you left porta bit-4 as a constant output
	bcf		STATUS,	RP0		;
	return


port_b_output
   	bsf 	STATUS,	RP0		;set porta bit-4 back to an input (high - Z state)
	movlw	b'00000000'		;so it doesnt interfere with our display data
	movwf	TRISB			;
	bcf		STATUS,	RP0		;
	return



go_up
	btfsc stop_repeat, 0		;shifts the car left
	return
	rlf man_y, 1				;by shifting the data
	call button_repeat
	return

go_down
	btfsc stop_repeat, 0		;shifts the car left
	return
	rrf man_y, 1				;by shifting the data
	call button_repeat
	return

go_left
	btfsc stop_repeat, 0		;shifts the car left
	return
	rrf man_x, 1				;by shifting the data
	call button_repeat
	return

go_right
	btfsc stop_repeat, 0		;shifts the car left
	return
	rlf man_x, 1				;by shifting the data
	bsf stop_repeat, 1
	bsf stop_repeat, 0
	movf button_adjust, w
	movwf button_timer
	return

button_a
	btfsc stop_repeat, 0		;shifts the car left
	return
	clrf PORTB
	bsf do_bomb, 0
	movlw d'75'
	movwf bomb_timer
	movf man_x, w
	movwf bomb_x
	movf man_y, w
	movwf bomb_y
	call drop_bomb
	call button_repeat
	return


button_repeat
	bsf stop_repeat, 1
	bsf stop_repeat, 0
	movf button_adjust, w
	movwf button_timer
	return


button_time
	decfsz button_timer, 1
	return
	bcf stop_repeat ,0
	bcf stop_repeat, 1
	return



delay
		movlw d'03'
		movwf counta
		movwf countb
again	decfsz counta, 1
	goto again
		decfsz countb, 1
	goto again
	return



	end
