1; startup == 3
2; msx cartridge rom
3
4; April 2014
5; submitted by Timmy
6
7; For cartridge I am not sure what facilities are available from the MSX
8; system, if any.  So this CRT only provides the bare minimum.
9
10;
11;  Declarations
12;
13
14
15        defc    TAR__clib_exit_stack_size = 0
16        defc    TAR__register_sp = -0xfc4a
17        INCLUDE "crt/classic/crt_rules.inc"
18
19;
20;  Main Code Entrance Point
21;
22IFNDEF CRT_ORG_CODE
23	defc  CRT_ORG_CODE  = $4000
24ENDIF
25	org   CRT_ORG_CODE
26
27; ROM header
28
29	defm	"AB"
30	defw	start
31	defw	0		;CallSTMT handler
32	defw	0		;Device handler
33	defw	0		;basic
34	defs	6
35
36start:
37	di
38	INCLUDE	"crt/classic/crt_init_sp.asm"
39	ei
40
41; port fixing; required for ROMs
42; port fixing = set the memory configuration, must be first!
43
44	in a,($A8)
45	and a, $CF
46	ld d,a
47	in a,($A8)
48	and a, $0C
49	add a,a
50	add a,a
51	or d
52	out ($A8),a
53
54	INCLUDE	"crt/classic/crt_init_atexit.asm"
55	call	crt0_init_bss
56
57       IF DEFINED_USING_amalloc
58                INCLUDE "crt/classic/crt_init_amalloc.asm"
59        ENDIF
60
61	call _main
62
63; end program
64
65cleanup:
66endloop:
67	di
68	halt
69	jr endloop
70
71
72l_dcal:	jp	(hl)		;Used for call by function pointer
73
74	GLOBAL	banked_call
75
76	; Memory address for changing the mapping at $8000 and $a000
77	; If _A000 is 0 then we work with 16kb banks
78	; These values are by default the KONAMI mapper
79IF DEFINED_MAPPER_ASCII16 = 1
80	defc	MAPPER_ADDRESS_4000 = $6000
81	defc	MAPPER_ADDRESS_6000 = $0000
82	defc	MAPPER_ADDRESS_8000 = $7000
83	defc	MAPPER_ADDRESS_A000 = $0000
84ELIF DEFINED_MAPPER_ASCII8 = 1
85	defc	MAPPER_ADDRESS_4000 = $6000
86	defc	MAPPER_ADDRESS_6000 = $6800
87	defc	MAPPER_ADDRESS_8000 = $7000
88	defc	MAPPER_ADDRESS_A000 = $7800
89ELIF DEFINED_MAPPER_KONAMI = 1
90	; Konami Mapper without SCC
91	defc	MAPPER_ADDRESS_4000 = $0000
92	defc	MAPPER_ADDRESS_6000 = $6000
93	defc	MAPPER_ADDRESS_8000 = $8000
94	defc	MAPPER_ADDRESS_A000 = $A000
95ELSE
96	defc	MAPPER_ADDRESS_4000 = $0000
97	defc	MAPPER_ADDRESS_6000 = $0000
98	defc	MAPPER_ADDRESS_8000 = $0000
99	defc	MAPPER_ADDRESS_A000 = $0000
100ENDIF
101
102	; Bias the Mapper detection heuristics to try to overcome any
103	; collisions in data.
104IF MAPPER_ADDRESS_4000 != 0
105	ld	(MAPPER_ADDRESS_4000),a
106ENDIF
107IF MAPPER_ADDRESS_6000 != 0
108	ld	(MAPPER_ADDRESS_6000),a
109ENDIF
110IF MAPPER_ADDRESS_8000 != 0
111	ld	(MAPPER_ADDRESS_8000),a
112ENDIF
113IF MAPPER_ADDRESS_A000 != 0
114	ld	(MAPPER_ADDRESS_A000),a
115ENDIF
116IF MAPPER_ADDRESS_4000 != 0
117	ld	(MAPPER_ADDRESS_4000),a
118ENDIF
119IF MAPPER_ADDRESS_6000 != 0
120	ld	(MAPPER_ADDRESS_6000),a
121ENDIF
122IF MAPPER_ADDRESS_8000 != 0
123	ld	(MAPPER_ADDRESS_8000),a
124ENDIF
125IF MAPPER_ADDRESS_A000 != 0
126	ld	(MAPPER_ADDRESS_A000),a
127ENDIF
128IF MAPPER_ADDRESS_4000 != 0
129	ld	(MAPPER_ADDRESS_4000),a
130ENDIF
131IF MAPPER_ADDRESS_6000 != 0
132	ld	(MAPPER_ADDRESS_6000),a
133ENDIF
134IF MAPPER_ADDRESS_8000 != 0
135	ld	(MAPPER_ADDRESS_8000),a
136ENDIF
137IF MAPPER_ADDRESS_A000 != 0
138	ld	(MAPPER_ADDRESS_A000),a
139ENDIF
140
141IF MAPPER_ADDRESS_8000 != 0
142banked_call:
143        pop     hl              ; Get the return address
144        ld      (mainsp),sp
145        ld      sp,(tempsp)
146        ld      a,(__current_bank)
147        push    af              ; Push the current bank onto the stack
148        ld      e,(hl)          ; Fetch the call address
149        inc     hl
150        ld      d,(hl)
151        inc     hl
152        ld      a,(hl)          ; ...and page
153  IF MAPPER_ADDRESS_A000 != 0
154	add	a		; For 8kb pages we have to double - our
155				; model is for 16kb pages
156  ENDIF
157	inc	hl
158        inc     hl              ; Yes this should be here
159        push    hl              ; Push the real return address
160        ld      (tempsp),sp
161        ld      sp,(mainsp)
162        ld      (__current_bank),a
163	ld	(MAPPER_ADDRESS_8000),a
164  IF MAPPER_ADDRESS_A000 != 0
165	inc	a
166	ld	(MAPPER_ADDRESS_A000),a
167  ENDIF
168        ld      l,e
169        ld      h,d
170	call	l_dcal		; jp(hl)
171        ld      (mainsp),sp
172        ld      sp,(tempsp)
173        pop     bc              ; Get the return address
174        pop     af              ; Pop the old bank
175        ld      (tempsp),sp
176        ld      sp,(mainsp)
177        ld      (__current_bank),a
178	ld	(MAPPER_ADDRESS_8000),a
179  IF MAPPER_ADDRESS_A000 != 0
180	inc	a
181	ld	(MAPPER_ADDRESS_A000),a
182  ENDIF
183        push    bc
184        ret
185ENDIF
186
187
188IFNDEF CRT_ORG_BSS
189	defc CRT_ORG_BSS = $C000   ; Ram variables are kept in RAM in high memory
190ENDIF
191	defc	__crt_org_bss = CRT_ORG_BSS
192
193        ; If we were given a model then use it
194        IFDEF CRT_MODEL
195            defc __crt_model = CRT_MODEL
196        ELSE
197            defc __crt_model = 1
198        ENDIF
199
200        INCLUDE "crt/classic/crt_runtime_selection.asm"
201	INCLUDE "crt/classic/crt_section.asm"
202
203	SECTION	data_driver
204
205IF MAPPER_ADDRESS_8000 != 0
206  IF MAPPER_ADDRESS_A000 == 0
207__current_bank:	defb	1
208  ELSE
209__current_bank:	defb	2
210  ENDIF
211ENDIF
212
213        SECTION bss_driver
214
215mainsp: defw    0
216
217tempstack:      defs    CLIB_BANKING_STACK_SIZE
218
219        SECTION data_driver
220
221tempsp: defw    tempstack + CLIB_BANKING_STACK_SIZE
222
223IF MAPPER_ADDRESS_8000 != 0
224	INCLUDE "target/msx/classic/megarom.asm"
225ENDIF
226