summaryrefslogtreecommitdiff
path: root/menu.s68
blob: d348ea17049ab22291dfa921913fc7b3550fa1f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
	dc.l $0, start
	dc.l empty_handler
	dc.l empty_handler
	;$10
	dc.l empty_handler
	dc.l empty_handler
	dc.l empty_handler
	dc.l empty_handler
	;$20
	dc.l empty_handler
	dc.l empty_handler
	dc.l empty_handler
	dc.l empty_handler
	;$30
	dc.l empty_handler
	dc.l empty_handler
	dc.l empty_handler
	dc.l empty_handler
	;$40
	dc.l empty_handler
	dc.l empty_handler
	dc.l empty_handler
	dc.l empty_handler
	;$50
	dc.l empty_handler
	dc.l empty_handler
	dc.l empty_handler
	dc.l empty_handler
	;$60
	dc.l empty_handler
	dc.l empty_handler
	dc.l empty_handler
	dc.l empty_handler
	;$70
	dc.l int_4
	dc.l empty_handler
	dc.l int_6
	dc.l empty_handler
	;$80
	dc.l empty_handler
	dc.l empty_handler
	dc.l empty_handler
	dc.l empty_handler
	;$90
	dc.l empty_handler
	dc.l empty_handler
	dc.l empty_handler
	dc.l empty_handler
	;$A0
	dc.l empty_handler
	dc.l empty_handler
	dc.l empty_handler
	dc.l empty_handler
	;$B0
	dc.l empty_handler
	dc.l empty_handler
	dc.l empty_handler
	dc.l empty_handler
	;$C0
	dc.l empty_handler
	dc.l empty_handler
	dc.l empty_handler
	dc.l empty_handler
	;$D0
	dc.l empty_handler
	dc.l empty_handler
	dc.l empty_handler
	dc.l empty_handler
	;$E0
	dc.l empty_handler
	dc.l empty_handler
	dc.l empty_handler
	dc.l empty_handler
	;$F0
	dc.l empty_handler
	dc.l empty_handler
	dc.l empty_handler
	dc.l empty_handler
	dc.b "SEGA GENESIS    "
	dc.b "(c) 2015.JULY   "
	dc.b "Menu            "
	dc.b "                "
	dc.b "                "
	dc.b "Menu            "
	dc.b "                "
	dc.b "                "
	dc.b "MP BlstMenu-00", 0, 0
	dc.b "                "
	dc.l $0, rom_end-1, $FF0000, $FFFFFF
	dc.b "                "
	dc.b "                "
	dc.b "                "
	dc.b "                "
	dc.b "JUE "

;register addresses
VDP_DATA  equ $C00000
VDP_CTRL  equ $C00004
VDP_HV    equ $C00008
Z80_RAM   equ $A00000
IO_AREA   equ $A10000
PAD1_DATA equ (IO_AREA + 3)
PAD2_DATA equ (IO_AREA + 5)
EXT_DATA  equ (IO_AREA + 7)
PAD1_CTRL equ (IO_AREA + 9)
PAD2_CTRL equ (IO_AREA + 11)
EXT_CTRL  equ (IO_AREA + 13)

MODE_1   equ 0
MODE_2   equ 1
SCROLL_A equ 2
WINDOW   equ 3
SCROLL_B equ 4
SAT      equ 5
BG_COLOR equ 7
HINT     equ $A
MODE_3   equ $B
MODE_4   equ $C
HSCROLL  equ $D
AUTOINC  equ $F
SCROLL   EQU $10
WINDOW_H equ $11
WINDOW_V equ $12
DMALEN_L equ $13
DMALEN_H equ $14
DMASRC_L equ $15
DMASRC_M equ $16
DMASRC_H equ $17

VDP_VRAM_WRITE equ $40000000
VDP_CRAM_WRITE equ $C0000000
VDP_VSRAM_WRITE equ $40000010
VDP_DMA_FLAG equ $80

vdpregset macro
	move.w #(((\1) << 8) | $8000 | (\2)), (a1)
	endm

vdpreg macro
	dc.w (((\1) << 8) | $8000 | (\2))
	endm

;Writes a normal VDP command to the control port
;\1 - VDP address
;\2 - Access type
vdpaccess macro
	move.l #((\2) | (\1) << 16 & $3FFF0000 | (\1) >> 14 & 3), (a1)
	endm

;Writes a DMA command to the control port
;\1 - Destination address
;\2 - Destination type
startdma macro
	move.l #(\2 | VDP_DMA_FLAG | (\1 << 16) & $3FFF0000 | (\1 >> 14) & 3), (a1)
	endm

DMA_SRC_68K  equ 0
DMA_SRC_VRAM equ $C0
DMA_SRC_FILL equ $80

dmasrc macro
	move.l #($95009600 + (\1) << 15 & $FF0000 + (\1) >> 9 & $FF), (a1)
	move.w #($9700 + (\1) >> 17 & $7F | (\2)), (a1)
	endm

dir_buffer equ $100000
menu_port  equ $180000

MAX_DISPLAY equ 24

	rsset $FFFF8000
x_pos       rs.w 1
base_cmd    rs.l 1
sprite_list rs.l 160
page_index  rs.l MAX_DISPLAY+1
num_sprites rs.b 1
last_pad    rs.b 1
selected    rs.b 1

int_6:
	dmasrc sprite_list, DMA_SRC_68K
	;set DMA length
	move.l #$94009300, d0
	moveq #0, d1
	move.b num_sprites.w, d1
	add.w d1, d1
	add.w d1, d1
	move.b d1, d0
	swap d0
	lsr.w #8, d1
	move.b d1, d0
	move.l d0, (a1)
	startdma $C000, VDP_VRAM_WRITE

	;read gamepad in port 1
	lea PAD1_DATA, a2
	move.b #$40, (a2)
	move.b (a2), d0
	and.b #$3F, d0
	move.b d0, (a2)
	move.b (a2), d1
	and.b #$30, d1
	add.b d1, d1
	add.b d1, d1
	or.b d1, d0
	not.b d0   ;button state is inveterted
	move.b (last_pad).w, d1
	eor.b d0, d1
	and.b d0, d1
	move.b d0, (last_pad).w

	moveq #16, d2
	;d0 = SACBRLUD
	;d1 = newly pressed buttons
	btst #1, d1
	bne down
	btst #0, d1
	bne up
	btst #7, d1
	bne start_pressed
int_done:
	rte

down:
	;check if we are already at the bottom of the page
	moveq #1, d0
	add.b (selected).w, d0
	move.w d0, d1
	add.w d0, d0
	add.w d0, d0
	lea page_index.w, a2
	tst.l (0, a2, d0.w)
	beq int_done
	move.b d1, (selected).w

	add.w d2, (sprite_list).w
	add.w d2, (sprite_list+8).w
	rte
up:
	;check if we are already at the top of the page
	move.b (selected).w, d0
	beq int_done
	subq #1, d0
	move.b d0, (selected).w

	sub.w d2, (sprite_list).w
	sub.w d2, (sprite_list+8).w
	rte

start_pressed:
	moveq #0, d0
	move.b (selected).w, d0
	add.w d0, d0
	add.w d0, d0
	lea page_index.w, a2
	move.l (0, a2, d0.w), a2
	tst.b (-1, a2)
	bne enter_dir
	;regular file
	rte
enter_dir:
	lea menu_port+4, a3
	move.l a2, (a3)
.wait_complete
	tst.w (a3)
	bne .wait_complete
	addq #6, a7
	bra menu_start
int_4:
empty_handler:
	rte



initial_regs:
	vdpreg MODE_2, $4    ;Mode 5, everything turned off
	vdpreg MODE_1, $4
	vdpreg SCROLL_A, $20 ;Scroll a table $8000
	vdpreg SCROLL_B, $05 ;Scroll b table $A000
	vdpreg SAT, $60      ;SAT table $C000
	vdpreg BG_COLOR, 0
	vdpreg HINT, $FF
	vdpreg MODE_3, 0     ;full screen scroll
	vdpreg MODE_4, $87   ;40 cell mode, double-res interlace
	vdpreg HSCROLL, 0
	vdpreg AUTOINC, 2
	vdpreg SCROLL, 1     ;64x32 scroll size
end_initial_regs

start:
	lea $C00000, a0
	lea $C00004, a1

	moveq #(end_initial_regs-initial_regs-1), d0
	lea initial_regs.w, a2
.regloop
	move.w (a2)+, (a1)
	dbra d0, .regloop

	vdpaccess $0, VDP_CRAM_WRITE
	move.w #$020, (a0)
	move.w #$EEE, (a0)
	move.w #$222, (a0)

	;init scroll table
	vdpaccess $0, VDP_VRAM_WRITE
	move.w #0, (a0)
	move.w #4, (a0)

	;load tiles
	vdpaccess $800, VDP_VRAM_WRITE
	lea font(pc), a2
	move.w #((arrowend-font)/4 - 1), d0
tloop:
	move.l (a2)+, (a0)
	dbra d0, tloop





	;setup SAT
	;;vdpaccess $C000, VDP_VRAM_WRITE

	lea sprite_list.w, a2
	;left arrow
	move.l #$01080501, (a2)+
	move.l #$807F0086, (a2)+

	;right arrow
	move.l #$01080500, (a2)+
	move.l #$887F01AA, (a2)+
	move.b #2, num_sprites.w

menu_start:
	;clear name tables
	vdpaccess $8000, VDP_VRAM_WRITE
	moveq #32, d0
	swap d0
	move.b #32, d0
	move.w #(64*64-1), d1
ploop:
	move.l d0, (a0)
	dbra d1, ploop

	move.l #$40860002, d3
	move.l d3, (a1)
	move.l d3, base_cmd.w

	lea dir_buffer, a6
	lea menu_port, a2
	move.l a6, (a2)

wait_complete:
	tst.w (a2)
	bne wait_complete

	lea page_index.w, a3
	moveq #MAX_DISPLAY-1, d7
file_loop:
	tst.b (a6)+
	beq done_files
	addq #1, a6 ;TODO: Do something with directory flag

	;skip over entries starting with a dot except ..
	cmp.b #$2E, (a6)
	bne normal
	cmp.b #$2E, (1, a6)
	beq normal
	addq #1, a6
.skip_loop:
	tst.b (a6)+
	bne .skip_loop
	addq #1, d7
	move.l a6, d6
	bra skip
normal:
	;save entry pointer to page index
	move.l a6, (a3)+
	;print name on screen
	moveq #0, d0
	bsr print_string
	move.l a6, d6

	lea Newline(pc), a6
	bsr print_string

skip:
	;word align pointer
	addq #1, d6
	and.w #$FFFE, d6
	move.l d6, a6

	dbra d7, file_loop
done_files:

	;null terminate page_index
	moveq #0, d0
	move.l d0, (a3)

	;setup gamepad in port 1
	move.b #$40, PAD1_CTRL

	move.w #$8174, (a1) ;enable display, vertical interrupts, DMA


wait_forever
	stop #2500
	bra wait_forever

Newline:
	dc.b $A, 0

	align 1

;Prints a null terminated string
;a6 - pointer to string
;a0 - VDP data port
;d0 - base tile attribute
;
;Clobbers: d1.w, d2.w, d3.l
print_string:
	lea widths(pc), a5
	move.w x_pos.w, d2
	move.l base_cmd.w, d3
.loop
	moveq #0, d1
	move.b (a6)+, d1
	beq .end
	cmp.b #$A, d1
	beq .newline
	tst.b (-32, a5, d1.w)
	beq .narrow
	add.w d0, d1
	move.w d1, (a0)
	addq #2, d2
	bra .loop
.narrow
	add.w d0, d1
	move.w d1, (a0)
	addq #1, d2
	move.l d2, d1
	;switch to other plane
	and.w #$FFFE, d1
	swap d1
	eor.l #$20000000, d3
	add.l d3, d1
	move.l d1, (a1)
	bra .loop
.newline
	moveq #0, d2
	;switch back to plane A
	and.l #$DFFFFFFF, d3
	;skip to next row
	add.l #$00800000, d3
	move.l d3, (a1)
	bra .loop
.end
	move.w d2, x_pos.w
	move.l d3, base_cmd.w
	rts

	align 1
font:
	incbin font_interlace_variable.tiles
fontend
arrow:
	incbin arrow.tiles
arrowend:

widths:
	dc.b 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1
	dc.b 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0
	dc.b 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1
	dc.b 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
	dc.b 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1

rom_end: