; Rom code for II+ 80 column card
;
; Created:    2009-02-23 21:58:43
; Input file: rom.img


; Scratchpad memory: (n78 - n7f) and (nf8 - nff)
;	    where n = [4-7]

        .setcpu "6502"

L1E		:= $1E	    ; just temp storage

; Slot 3 scratch space
SC_CH		:= $047B    ; horizontal position
SC_CV		:= $04FB    ; vertical position
STATUS  	:= $057B
;   MON_OK	:= 0x80	    ; Clear if monitor not avaliable (CP/M Mode)
;   SHKEY_MOD   := 0x40	    ; Enable shift key mod
;   CH_CASE	:= 0x20	    ; Print one char in uppercase
;   CASE	:= 0x10	    ; 1 = Uppercase
;   DO_FILLIN   := 0x01	    ; Add YSAV to IN buffer on write
STATUS2 	:= $05FB    ; Pointer for write routines
;   ESC_CHAR    := 0x80	    ; Do escape character
DEBUG_LOC	:= $067B    ; only written to?
SC_CURSL	:= $06FB    ; cursor location
SC_CURSH	:= $077B    ; cursor location
CURS_RAST	:= $07FB    ; current cursor style


; Also seems to steal the language cards scratch space
LSCRATCH1	:= $0478    ; Temp storage
LSCRATCH2	:= $04F8    ; Temp storage
RELOC_PTR	:= $07F8    ; RELOC_PTR Contains $C3 for slot 3

; Monitor routines
M_BELL		:= $FBDD    ; Monitor BELL
SETKBD          := $FE89    ; Monitor SETKBD
SETVID          := $FE93    ; Monitor SETVID

; Monitor locations
CH		:= $24	    ; horizontal character position
CV		:= $25	    ; vertical character position
GBASL		:= $26	    ; Monitor's graphics pointer
GBASH		:= $27	    ; 
BAS2L		:= $2A	    ; Secondary text location pointer
BAS2H		:= $2B	    ;
INVFLG		:= $32	    ; Character should be inverted
YSAV1		:= $35	    ; contains character to print
CSWL		:= $36	    ; CHAR out routine location
CSWH		:= $37
KSWL		:= $38	    ; KEY in routine location
KSWH		:= $39
RNDL		:= $4E	    ; key delay based random number generator
RNDH		:= $4F
IN		:= $0200    ; basic input string

; Hardware locations
DIS_EXP		:= $CFFF    ; Disable expansion rom
EN_RAST_MEM	:= $C0B2    ; Enable raster write
EN_CHAR_MEM	:= $C0B4    ; Enable char mem
TOGGLE_B1	:= $C0B6    ; Toggle char mem bank 1
CRT_REG_NO	:= $C0B8    ; Write CRTC register number
CRT_REG_VAL	:= $C0B9    ; Write CRTC register value
KEYB_READ	:= $C000    ; Read keyboard
KEYB_RST	:= $C010    ; Reset keyboard
LC_RST		:= $C080    ; Reset language card
LC_ROM		:= $C081    ; Read from monitor memory
RAST_MEM	:= $C800    ; Location to write glyph info
CHAR_MEM	:= $C800    ; Location to write chars
PB_2		:= $C063    ; Pushbutton 2 (shift key mod)

; Stack usage:
; On an rti instruction, the 6502 continues execution from address:
; (0x(pop2),(pop1) + 1) (little endian).
; A jsr subsequently stores the address as: (0x(push1),(push2) - 1)
; The following function manipulates the stack by effectively jumping
; to the address: 0x(RELOC_PTR,regA) + 1 and storing regA into L1E.
; S3_EXEC:  sta     L1E
;	    lda     RELOC_PTR
;	    pha
;	    lda     L1E
;	    pha
;	    rts
; The next rts will return to the point where S3_EXEC was called.
; This scheme effectively allows for relocatable jumps where RELOC_PTR
; provides the page number of the relocated code.

.macro	BCC_SKIP		; Skip next instruction
	.byte	$90 
.endmacro

.macro JMP_LOC Loc
	.byte	.lobyte(Loc-1)
.endmacro

.macro JMP_IMM Loc
	lda	#.lobyte(Loc-1)
	jmp	S3_EXEC
.endmacro
.macro JSR_IMM Loc
	lda	#.lobyte(Loc-1)
	jsr	S3_EXEC
.endmacro
.macro BNE_IMM Loc
	lda	#.lobyte(Loc-1)
	bne	S3_EXEC
.endmacro
.macro LDA_IMM Loc
	lda	#.lobyte(Loc-1)
.endmacro

.linecont on

.macro check_range Loc
	.assert (((Loc-*-0) <   128) && \
		 ((Loc-*-0) >= -128)), error, "Branch out of range"
.endmacro

; Allow cross segment branches
.macro _BEQ Loc
	check_range Loc
	.byte	$F0
	.byte	.lobyte(Loc-*-1)
.endmacro
.macro _BCC Loc
	check_range Loc
	.byte	$90
	.byte	.lobyte(Loc-*-1)
.endmacro
.macro _BNE Loc
	check_range Loc
	.byte	$D0
	.byte	.lobyte(Loc-*-1)
.endmacro
