;------------------------------------------------------------------------
;		Copyright 2010 Integrated Logic Systems
;                  Source Code is Copyright Stephen J. Carden
;
;
;			Please do not share this source!
;------------------------------------------------------------------------
;                              Cartdump.com
;
; Copy OSS cartridge banks to
; D1:CARTIMAGE.CBx where x is bank
;       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;------------------------------------------------------------------------
; Notes: o This source code MAY NOT be placed for download on any BBS's!
;        o "mea" is a macro that loads the address of the first field
;          into the pointer specified by the second field.
;------------------------------------------------------------------------
; Assembler:  MADMAC (tm) ST Cross Assembler (Atari Corp)
;             XASM   st and IBM VERSIONS
;------------------------------------------------------------------------
;=====================================================================
com_file_name:		.macro
;			dc.b	13,"filename.ext",$9b
			dc.b	12,"Cartdump.com",$9b
			.endm

;
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; File revision history. Version Number in hex
;
file_ver:		equ   $11
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; the Month in dec for the first time this revision 
;was compiled
;
c_month:		equ   8
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; the day in dec for the first time this revision 
;was compiled
c_day:			equ   26
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; the year in dec for the first time this revision 
;was compiled
c_year:			equ   2012
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Type of compiler used in program
;
; 0 = unknown Compiler
; 1 = xasm
; 2 = mac_65
; 3 = basic
; 4 = compiled basic xl
; 5 = C65
; 6 = Action
;
; We can add more as time goes on!
;
;
xasm:			equ	1
mac_65:			equ	2 
basic:			equ	3
basicxl:		equ	4
c65:			equ	5
action:			equ	6

file_compiler:		equ	xasm
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; is this relocatable code ?
; anyother value other than 1 or 2 would be unknown
r..yes:			equ	1
r..no:			equ	2
;
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Gotta define if it can be relocated
l_relocatable:		equ	r..no
;
;
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Gotta define if it can be relocated
r..crl:			equ	$7d
r..crlf:		equ	$9b
r..space:		equ	$20

l_frstscreenbyte:	equ	r..space
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; The language the output file is in. 
;
;
; 0	=	Undifined
; 1	=	English
; 2	=	German
;
r..language:		equ	1
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;ICCOM = $0342
;ICBAL = $0344
;ICBLL = $0348
;ICAX1 = $034A
;CIOV = $E456
;PORTB = $D301
DMACTL = $D400
;SDMCTL = $022F
;GINTLK = $03FA
TRIG3 = $D013


			.include	equates
			.include	globals
			.include	macros

			.org  $2500

win_start:
header_info:
			.include	header			; this reads info out of the header and displays to screen
			jmp	start
;
		.org	$2F00
;
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; The first 64 bytes are dumped to
; the CARIMAGE.MAP file
;
RAMROM:
			ds.b	16   			;0=rom $FF=ram
BANKNO:
			ds.b	16   			;byte at $AFFF
BARRAY:
			ds.b	16   			;0=valid rom bank
TRIG3A:
			ds.b	16   			;trig3 reading for bank

CURBNK:
			ds.b	1    			;Base test bank
TESTBK:
			ds.b	1    			;Bank being tested vs base
CBSAVE:
			ds.b	1    			;Entry cart status
PBSAVE:
			ds.b	1    			;Entry portb status
;
		.org	$3000
START:
			lda	#$60			; loading and rts
			sta	START			; to the start of the file so it can't run twice
			ldx	#0
			txa
ZLOOP:
			sta	$2F00,X   		;Clear data page
			inx	
			bne	ZLOOP
;
			lda	PORTB
			sta	PBSAVE
			dex
			stx	PORTB
			ldy	#$08
			lda	GINTLK
			beq	HAVBNK
			ldy	$AFFF
HAVBNK:
			sty	CBSAVE
;
			sei	         		;Kill vblank 2 before
			ldx	#15     		;playing with cart
LOOP0:
			sta	$D500,X 		;First test
			lda	TRIG3   		;if rom or ram
			sta	TRIG3A,X
			lda	$AFFF   		;Get any bank number
			sta	BANKNO,X
			inc	$AFFF   		;Check ram
			cmp	$AFFF
			beq	ISROM   		;No, it's rom
			sta	$AFFF
			dec	RAMROM,X		;Set rom/ram map to ram
			dec	BARRAY,X		;Show no rom to test
ISROM:
			dex
			bpl	LOOP0
;
			sta	$D500,Y 		;Cartridge normal
			cli	         		;vblank now ok
;
			stx	CURBNK  		;Start base at -1
			ldy	#$FF
LOOP1:
			iny	
			cpy	#16      		;All higher ones tested?
			bcs	NEXTBASE 		;Yes, bump test base

			lda	BARRAY,Y 		;Unique rom this bank?
			bmi	LOOP1    		;No, ram or a dup

			tya	
			tax	

LOOP2:
			inx	         		;Test all higher
			cpx	#16      		;banks for duplicate
			bcs	LOOP1    		;All higher tested.

			lda	BANKNO,X 		;Was there a valid
			cmp	#16      		;bank number?
			bcs	FAIL     		;No

			eor	BANKNO,Y 		;Same as current?
			bne	LOOP2    		;No

FAIL:
			dec	BARRAY,X 		;Yes, flag a duplicate
			bmi	LOOP2    		;Go always

NEXTBASE:
			inc	CURBNK   		;If at bank 15 there's
			ldx	CURBNK   		;none higher to compare
			cpx	#15      		;with
			bcs	MAPWRT   		;So go finish up
;
			lda	BARRAY,X 		;Check if bank valid
			bmi	NEXTBASE 		;No, ram or duplicate

			sta	$D500,X  		;Enable it
			ldy	#0       		;Copy it to $6000
			lda	#$A0
			sta	CLOOP+2
			lda	#$60
			sta	CLOOP+5
CLOOP:
			lda	$A000,Y
			sta	$6000,Y
			iny	
			bne	CLOOP
			inc	CLOOP+2
			inc	CLOOP+5
			bpl	CLOOP
			jsr	WRITE    		;Write it to disk
			jmp	NEXTBASE

MAPWRT:
			ldx	CBSAVE
			sta	$D500,X
			lda	PBSAVE
			sta	PORTB
			ldx	#$10
			lda	# low MSPEC
			sta	ICBAL,X
			lda	# High MSPEC
			sta	ICBAL+1,X
			lda	#3
			jsr	GOCIO
			bmi	CLOSE

			lda	# low RAMROM
			sta	ICBAL,X
			lda	# High RAMROM
			sta	ICBAL+1,X
			lda	#48
			sta	ICBLL,X
			lda	#0
			beq	BPUT

WRITE:
			txa
			clc	
			adc	#'0'
			cmp	#'9'+1
			bcc	ISHEX
			adc	#6
ISHEX:
			sta	BANKID
			ldx	#$10
			jsr	CLOSE
			lda	# low FSPEC
			sta	ICBAL,X
			lda	# High FSPEC
			sta	ICBAL+1,X
			lda	#8
			sta	ICAX1,X
			lda	#0
			sta	ICAX1+1,X
			lda	#3
			jsr	GOCIO
			bmi	CLOSE
			lda	# low $6000
			sta	ICBAL,X			;address
			sta	ICBLL,X			;length
			lda	# High $6000
			sta	ICBAL+1,X		;address
			lda	# High $2000

BPUT:
			sta	ICBLL+1,X		;length
			lda	#11			;bput
			jsr	GOCIO
CLOSE:
			lda	#12
GOCIO:
			sta	ICCOM,X
			jmp	CIOV
;
; Copy cartridge bank to $4000
;
BCOPY:
			sei	
			sta	$D500,X
			lda	# High $A000
			sta	LOOP4+2
			lda	# High $4000
			sta	LOOP4+5
			ldx	#32
			ldy	#0
LOOP4:
			lda	$A000,Y
			sta	$4000,Y
			iny	
			bne	LOOP4
			inc	LOOP4+2
			inc	LOOP4+5
			dex
			bne	LOOP4
			ldx	CBSAVE
			sta	$D500,X
			rts
;
FSPEC:			dc.b	"D1:>CARIMAGE.CB"
BANKID:			dc.b	"0",$9b
MSPEC:			dc.b	"D1:>CARIMAGE.MAP",$9b

win_end:		ds.b	0
;